Add more targets to the list of architectures not supporting format changing during...
[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
a14ed312 186static void breakpoints_info (char *, int);
c906108c 187
d77f58be
SS
188static void watchpoints_info (char *, int);
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
1042e4c0
SS
282static void tracepoints_info (char *, int);
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 {
2892 char *message
2893 = memory_error_message (TARGET_XFER_E_IO,
2894 bl->gdbarch, bl->address);
2895 struct cleanup *old_chain = make_cleanup (xfree, message);
2896
2897 fprintf_unfiltered (tmp_error_stream,
2898 "Cannot insert breakpoint %d.\n"
2899 "%s\n",
2900 bl->owner->number, message);
2901 do_cleanups (old_chain);
2902 }
2903 else
2904 {
2905 fprintf_unfiltered (tmp_error_stream,
2906 "Cannot insert breakpoint %d: %s\n",
2907 bl->owner->number,
2908 bp_err_message);
2909 }
879bfdc2 2910 }
0000e5cc 2911 return 1;
879bfdc2
DJ
2912
2913 }
2914 }
2915 else
35df4500 2916 bl->inserted = 1;
879bfdc2 2917
0000e5cc 2918 return 0;
879bfdc2
DJ
2919 }
2920
35df4500 2921 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2922 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2923 watchpoints. It's not clear that it's necessary... */
35df4500 2924 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2925 {
0000e5cc
PA
2926 int val;
2927
77b06cd7
TJB
2928 gdb_assert (bl->owner->ops != NULL
2929 && bl->owner->ops->insert_location != NULL);
2930
2931 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2932
2933 /* If trying to set a read-watchpoint, and it turns out it's not
2934 supported, try emulating one with an access watchpoint. */
35df4500 2935 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2936 {
2937 struct bp_location *loc, **loc_temp;
2938
2939 /* But don't try to insert it, if there's already another
2940 hw_access location that would be considered a duplicate
2941 of this one. */
2942 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2943 if (loc != bl
85d721b8 2944 && loc->watchpoint_type == hw_access
35df4500 2945 && watchpoint_locations_match (bl, loc))
85d721b8 2946 {
35df4500
TJB
2947 bl->duplicate = 1;
2948 bl->inserted = 1;
2949 bl->target_info = loc->target_info;
2950 bl->watchpoint_type = hw_access;
85d721b8
PA
2951 val = 0;
2952 break;
2953 }
2954
2955 if (val == 1)
2956 {
77b06cd7
TJB
2957 bl->watchpoint_type = hw_access;
2958 val = bl->owner->ops->insert_location (bl);
2959
2960 if (val)
2961 /* Back to the original value. */
2962 bl->watchpoint_type = hw_read;
85d721b8
PA
2963 }
2964 }
2965
35df4500 2966 bl->inserted = (val == 0);
879bfdc2
DJ
2967 }
2968
35df4500 2969 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2970 {
0000e5cc
PA
2971 int val;
2972
77b06cd7
TJB
2973 gdb_assert (bl->owner->ops != NULL
2974 && bl->owner->ops->insert_location != NULL);
2975
2976 val = bl->owner->ops->insert_location (bl);
2977 if (val)
2978 {
2979 bl->owner->enable_state = bp_disabled;
2980
2981 if (val == 1)
2982 warning (_("\
2983Error inserting catchpoint %d: Your system does not support this type\n\
2984of catchpoint."), bl->owner->number);
2985 else
2986 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2987 }
2988
2989 bl->inserted = (val == 0);
1640b821
DJ
2990
2991 /* We've already printed an error message if there was a problem
2992 inserting this catchpoint, and we've disabled the catchpoint,
2993 so just return success. */
2994 return 0;
879bfdc2
DJ
2995 }
2996
2997 return 0;
2998}
2999
6c95b8df
PA
3000/* This function is called when program space PSPACE is about to be
3001 deleted. It takes care of updating breakpoints to not reference
3002 PSPACE anymore. */
3003
3004void
3005breakpoint_program_space_exit (struct program_space *pspace)
3006{
3007 struct breakpoint *b, *b_temp;
876fa593 3008 struct bp_location *loc, **loc_temp;
6c95b8df
PA
3009
3010 /* Remove any breakpoint that was set through this program space. */
3011 ALL_BREAKPOINTS_SAFE (b, b_temp)
3012 {
3013 if (b->pspace == pspace)
3014 delete_breakpoint (b);
3015 }
3016
3017 /* Breakpoints set through other program spaces could have locations
3018 bound to PSPACE as well. Remove those. */
876fa593 3019 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
3020 {
3021 struct bp_location *tmp;
3022
3023 if (loc->pspace == pspace)
3024 {
2bdf28a0 3025 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3026 if (loc->owner->loc == loc)
3027 loc->owner->loc = loc->next;
3028 else
3029 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3030 if (tmp->next == loc)
3031 {
3032 tmp->next = loc->next;
3033 break;
3034 }
3035 }
3036 }
3037
3038 /* Now update the global location list to permanently delete the
3039 removed locations above. */
44702360 3040 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3041}
3042
74960c60
VP
3043/* Make sure all breakpoints are inserted in inferior.
3044 Throws exception on any error.
3045 A breakpoint that is already inserted won't be inserted
3046 again, so calling this function twice is safe. */
3047void
3048insert_breakpoints (void)
3049{
3050 struct breakpoint *bpt;
3051
3052 ALL_BREAKPOINTS (bpt)
3053 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3054 {
3055 struct watchpoint *w = (struct watchpoint *) bpt;
3056
3057 update_watchpoint (w, 0 /* don't reparse. */);
3058 }
74960c60 3059
04086b45
PA
3060 /* Updating watchpoints creates new locations, so update the global
3061 location list. Explicitly tell ugll to insert locations and
3062 ignore breakpoints_always_inserted_mode. */
3063 update_global_location_list (UGLL_INSERT);
74960c60
VP
3064}
3065
20388dd6
YQ
3066/* Invoke CALLBACK for each of bp_location. */
3067
3068void
3069iterate_over_bp_locations (walk_bp_location_callback callback)
3070{
3071 struct bp_location *loc, **loc_tmp;
3072
3073 ALL_BP_LOCATIONS (loc, loc_tmp)
3074 {
3075 callback (loc, NULL);
3076 }
3077}
3078
b775012e
LM
3079/* This is used when we need to synch breakpoint conditions between GDB and the
3080 target. It is the case with deleting and disabling of breakpoints when using
3081 always-inserted mode. */
3082
3083static void
3084update_inserted_breakpoint_locations (void)
3085{
3086 struct bp_location *bl, **blp_tmp;
3087 int error_flag = 0;
3088 int val = 0;
3089 int disabled_breaks = 0;
3090 int hw_breakpoint_error = 0;
dd61ec5c 3091 int hw_bp_details_reported = 0;
b775012e 3092
d7e74731 3093 string_file tmp_error_stream;
b775012e
LM
3094
3095 /* Explicitly mark the warning -- this will only be printed if
3096 there was an error. */
d7e74731 3097 tmp_error_stream.puts ("Warning:\n");
b775012e 3098
5ed8105e 3099 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
3100
3101 ALL_BP_LOCATIONS (bl, blp_tmp)
3102 {
3103 /* We only want to update software breakpoints and hardware
3104 breakpoints. */
3105 if (!is_breakpoint (bl->owner))
3106 continue;
3107
3108 /* We only want to update locations that are already inserted
3109 and need updating. This is to avoid unwanted insertion during
3110 deletion of breakpoints. */
3111 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3112 continue;
3113
3114 switch_to_program_space_and_thread (bl->pspace);
3115
3116 /* For targets that support global breakpoints, there's no need
3117 to select an inferior to insert breakpoint to. In fact, even
3118 if we aren't attached to any process yet, we should still
3119 insert breakpoints. */
f5656ead 3120 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3121 && ptid_equal (inferior_ptid, null_ptid))
3122 continue;
3123
d7e74731 3124 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3125 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3126 if (val)
3127 error_flag = val;
3128 }
3129
3130 if (error_flag)
3131 {
3132 target_terminal_ours_for_output ();
3133 error_stream (tmp_error_stream);
3134 }
b775012e
LM
3135}
3136
c30eee59 3137/* Used when starting or continuing the program. */
c906108c 3138
74960c60
VP
3139static void
3140insert_breakpoint_locations (void)
c906108c 3141{
a5606eee 3142 struct breakpoint *bpt;
35df4500 3143 struct bp_location *bl, **blp_tmp;
eacd795a 3144 int error_flag = 0;
c906108c 3145 int val = 0;
3fbb6ffa 3146 int disabled_breaks = 0;
81d0cc19 3147 int hw_breakpoint_error = 0;
dd61ec5c 3148 int hw_bp_error_explained_already = 0;
c906108c 3149
d7e74731
PA
3150 string_file tmp_error_stream;
3151
81d0cc19
GS
3152 /* Explicitly mark the warning -- this will only be printed if
3153 there was an error. */
d7e74731 3154 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3155
5ed8105e 3156 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3157
35df4500 3158 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3159 {
b775012e 3160 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3161 continue;
3162
4a64f543
MS
3163 /* There is no point inserting thread-specific breakpoints if
3164 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3165 has BL->OWNER always non-NULL. */
35df4500 3166 if (bl->owner->thread != -1
5d5658a1 3167 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3168 continue;
3169
35df4500 3170 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3171
3172 /* For targets that support global breakpoints, there's no need
3173 to select an inferior to insert breakpoint to. In fact, even
3174 if we aren't attached to any process yet, we should still
3175 insert breakpoints. */
f5656ead 3176 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3177 && ptid_equal (inferior_ptid, null_ptid))
3178 continue;
3179
d7e74731 3180 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3181 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3182 if (val)
eacd795a 3183 error_flag = val;
879bfdc2 3184 }
c906108c 3185
4a64f543
MS
3186 /* If we failed to insert all locations of a watchpoint, remove
3187 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3188 ALL_BREAKPOINTS (bpt)
3189 {
3190 int some_failed = 0;
3191 struct bp_location *loc;
3192
3193 if (!is_hardware_watchpoint (bpt))
3194 continue;
3195
d6b74ac4 3196 if (!breakpoint_enabled (bpt))
a5606eee 3197 continue;
74960c60
VP
3198
3199 if (bpt->disposition == disp_del_at_next_stop)
3200 continue;
a5606eee
VP
3201
3202 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3203 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3204 {
3205 some_failed = 1;
3206 break;
3207 }
3208 if (some_failed)
3209 {
3210 for (loc = bpt->loc; loc; loc = loc->next)
3211 if (loc->inserted)
834c0d03 3212 remove_breakpoint (loc);
a5606eee
VP
3213
3214 hw_breakpoint_error = 1;
d7e74731
PA
3215 tmp_error_stream.printf ("Could not insert "
3216 "hardware watchpoint %d.\n",
3217 bpt->number);
eacd795a 3218 error_flag = -1;
a5606eee
VP
3219 }
3220 }
3221
eacd795a 3222 if (error_flag)
81d0cc19
GS
3223 {
3224 /* If a hardware breakpoint or watchpoint was inserted, add a
3225 message about possibly exhausted resources. */
dd61ec5c 3226 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3227 {
d7e74731 3228 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3229You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3230 }
81d0cc19
GS
3231 target_terminal_ours_for_output ();
3232 error_stream (tmp_error_stream);
3233 }
c906108c
SS
3234}
3235
c30eee59
TJB
3236/* Used when the program stops.
3237 Returns zero if successful, or non-zero if there was a problem
3238 removing a breakpoint location. */
3239
c906108c 3240int
fba45db2 3241remove_breakpoints (void)
c906108c 3242{
35df4500 3243 struct bp_location *bl, **blp_tmp;
3a1bae8e 3244 int val = 0;
c906108c 3245
35df4500 3246 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3247 {
1e4d1764 3248 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3249 val |= remove_breakpoint (bl);
c5aa993b 3250 }
3a1bae8e 3251 return val;
c906108c
SS
3252}
3253
49fa26b0
PA
3254/* When a thread exits, remove breakpoints that are related to
3255 that thread. */
3256
3257static void
3258remove_threaded_breakpoints (struct thread_info *tp, int silent)
3259{
3260 struct breakpoint *b, *b_tmp;
3261
3262 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3263 {
5d5658a1 3264 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3265 {
3266 b->disposition = disp_del_at_next_stop;
3267
3268 printf_filtered (_("\
43792cf0
PA
3269Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3270 b->number, print_thread_id (tp));
49fa26b0
PA
3271
3272 /* Hide it from the user. */
3273 b->number = 0;
3274 }
3275 }
3276}
3277
6c95b8df
PA
3278/* Remove breakpoints of process PID. */
3279
3280int
3281remove_breakpoints_pid (int pid)
3282{
35df4500 3283 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3284 int val;
3285 struct inferior *inf = find_inferior_pid (pid);
3286
35df4500 3287 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3288 {
35df4500 3289 if (bl->pspace != inf->pspace)
6c95b8df
PA
3290 continue;
3291
fc126975 3292 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3293 {
834c0d03 3294 val = remove_breakpoint (bl);
6c95b8df
PA
3295 if (val != 0)
3296 return val;
3297 }
3298 }
3299 return 0;
3300}
3301
c906108c 3302int
fba45db2 3303reattach_breakpoints (int pid)
c906108c 3304{
6c95b8df 3305 struct cleanup *old_chain;
35df4500 3306 struct bp_location *bl, **blp_tmp;
c906108c 3307 int val;
dd61ec5c 3308 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3309 struct inferior *inf;
3310 struct thread_info *tp;
3311
3312 tp = any_live_thread_of_process (pid);
3313 if (tp == NULL)
3314 return 1;
3315
3316 inf = find_inferior_pid (pid);
3317 old_chain = save_inferior_ptid ();
3318
3319 inferior_ptid = tp->ptid;
a4954f26 3320
d7e74731 3321 string_file tmp_error_stream;
c906108c 3322
35df4500 3323 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3324 {
35df4500 3325 if (bl->pspace != inf->pspace)
6c95b8df
PA
3326 continue;
3327
35df4500 3328 if (bl->inserted)
c5aa993b 3329 {
35df4500 3330 bl->inserted = 0;
d7e74731 3331 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3332 if (val != 0)
3333 {
ce696e05 3334 do_cleanups (old_chain);
c5aa993b
JM
3335 return val;
3336 }
3337 }
3338 }
ce696e05 3339 do_cleanups (old_chain);
c906108c
SS
3340 return 0;
3341}
3342
e58b0e63
PA
3343static int internal_breakpoint_number = -1;
3344
84f4c1fe
PM
3345/* Set the breakpoint number of B, depending on the value of INTERNAL.
3346 If INTERNAL is non-zero, the breakpoint number will be populated
3347 from internal_breakpoint_number and that variable decremented.
e5dd4106 3348 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3349 breakpoint_count and that value incremented. Internal breakpoints
3350 do not set the internal var bpnum. */
3351static void
3352set_breakpoint_number (int internal, struct breakpoint *b)
3353{
3354 if (internal)
3355 b->number = internal_breakpoint_number--;
3356 else
3357 {
3358 set_breakpoint_count (breakpoint_count + 1);
3359 b->number = breakpoint_count;
3360 }
3361}
3362
e62c965a 3363static struct breakpoint *
a6d9a66e 3364create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3365 CORE_ADDR address, enum bptype type,
c0a91b2b 3366 const struct breakpoint_ops *ops)
e62c965a 3367{
e62c965a
PP
3368 struct symtab_and_line sal;
3369 struct breakpoint *b;
3370
4a64f543 3371 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3372
3373 sal.pc = address;
3374 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3375 sal.pspace = current_program_space;
e62c965a 3376
06edf0c0 3377 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3378 b->number = internal_breakpoint_number--;
3379 b->disposition = disp_donttouch;
3380
3381 return b;
3382}
3383
17450429
PP
3384static const char *const longjmp_names[] =
3385 {
3386 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3387 };
3388#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3389
3390/* Per-objfile data private to breakpoint.c. */
3391struct breakpoint_objfile_data
3392{
3393 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3394 struct bound_minimal_symbol overlay_msym;
17450429
PP
3395
3396 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3397 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3398
28106bc2
SDJ
3399 /* True if we have looked for longjmp probes. */
3400 int longjmp_searched;
3401
3402 /* SystemTap probe points for longjmp (if any). */
3403 VEC (probe_p) *longjmp_probes;
3404
17450429 3405 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3406 struct bound_minimal_symbol terminate_msym;
17450429
PP
3407
3408 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3409 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3410
3411 /* True if we have looked for exception probes. */
3412 int exception_searched;
3413
3414 /* SystemTap probe points for unwinding (if any). */
3415 VEC (probe_p) *exception_probes;
17450429
PP
3416};
3417
3418static const struct objfile_data *breakpoint_objfile_key;
3419
3420/* Minimal symbol not found sentinel. */
3421static struct minimal_symbol msym_not_found;
3422
3423/* Returns TRUE if MSYM point to the "not found" sentinel. */
3424
3425static int
3426msym_not_found_p (const struct minimal_symbol *msym)
3427{
3428 return msym == &msym_not_found;
3429}
3430
3431/* Return per-objfile data needed by breakpoint.c.
3432 Allocate the data if necessary. */
3433
3434static struct breakpoint_objfile_data *
3435get_breakpoint_objfile_data (struct objfile *objfile)
3436{
3437 struct breakpoint_objfile_data *bp_objfile_data;
3438
9a3c8263
SM
3439 bp_objfile_data = ((struct breakpoint_objfile_data *)
3440 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3441 if (bp_objfile_data == NULL)
3442 {
8d749320
SM
3443 bp_objfile_data =
3444 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3445
3446 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3447 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3448 }
3449 return bp_objfile_data;
3450}
3451
28106bc2
SDJ
3452static void
3453free_breakpoint_probes (struct objfile *obj, void *data)
3454{
9a3c8263
SM
3455 struct breakpoint_objfile_data *bp_objfile_data
3456 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3457
3458 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3459 VEC_free (probe_p, bp_objfile_data->exception_probes);
3460}
3461
e62c965a 3462static void
af02033e 3463create_overlay_event_breakpoint (void)
e62c965a 3464{
69de3c6a 3465 struct objfile *objfile;
af02033e 3466 const char *const func_name = "_ovly_debug_event";
e62c965a 3467
69de3c6a
PP
3468 ALL_OBJFILES (objfile)
3469 {
3470 struct breakpoint *b;
17450429
PP
3471 struct breakpoint_objfile_data *bp_objfile_data;
3472 CORE_ADDR addr;
67994074 3473 struct explicit_location explicit_loc;
69de3c6a 3474
17450429
PP
3475 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3476
3b7344d5 3477 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3478 continue;
3479
3b7344d5 3480 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3481 {
3b7344d5 3482 struct bound_minimal_symbol m;
17450429
PP
3483
3484 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3485 if (m.minsym == NULL)
17450429
PP
3486 {
3487 /* Avoid future lookups in this objfile. */
3b7344d5 3488 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3489 continue;
3490 }
3491 bp_objfile_data->overlay_msym = m;
3492 }
e62c965a 3493
77e371c0 3494 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3495 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3496 bp_overlay_event,
3497 &internal_breakpoint_ops);
67994074
KS
3498 initialize_explicit_location (&explicit_loc);
3499 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3500 b->location = new_explicit_location (&explicit_loc);
e62c965a 3501
69de3c6a
PP
3502 if (overlay_debugging == ovly_auto)
3503 {
3504 b->enable_state = bp_enabled;
3505 overlay_events_enabled = 1;
3506 }
3507 else
3508 {
3509 b->enable_state = bp_disabled;
3510 overlay_events_enabled = 0;
3511 }
e62c965a 3512 }
e62c965a
PP
3513}
3514
0fd8e87f 3515static void
af02033e 3516create_longjmp_master_breakpoint (void)
0fd8e87f 3517{
6c95b8df 3518 struct program_space *pspace;
6c95b8df 3519
5ed8105e 3520 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3521
6c95b8df 3522 ALL_PSPACES (pspace)
af02033e
PP
3523 {
3524 struct objfile *objfile;
3525
3526 set_current_program_space (pspace);
3527
3528 ALL_OBJFILES (objfile)
0fd8e87f 3529 {
af02033e
PP
3530 int i;
3531 struct gdbarch *gdbarch;
17450429 3532 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3533
af02033e 3534 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3535
17450429
PP
3536 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3537
28106bc2
SDJ
3538 if (!bp_objfile_data->longjmp_searched)
3539 {
25f9533e
SDJ
3540 VEC (probe_p) *ret;
3541
3542 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3543 if (ret != NULL)
3544 {
3545 /* We are only interested in checking one element. */
3546 struct probe *p = VEC_index (probe_p, ret, 0);
3547
3548 if (!can_evaluate_probe_arguments (p))
3549 {
3550 /* We cannot use the probe interface here, because it does
3551 not know how to evaluate arguments. */
3552 VEC_free (probe_p, ret);
3553 ret = NULL;
3554 }
3555 }
3556 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3557 bp_objfile_data->longjmp_searched = 1;
3558 }
3559
3560 if (bp_objfile_data->longjmp_probes != NULL)
3561 {
3562 int i;
3563 struct probe *probe;
3564 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3565
3566 for (i = 0;
3567 VEC_iterate (probe_p,
3568 bp_objfile_data->longjmp_probes,
3569 i, probe);
3570 ++i)
3571 {
3572 struct breakpoint *b;
3573
729662a5
TT
3574 b = create_internal_breakpoint (gdbarch,
3575 get_probe_address (probe,
3576 objfile),
28106bc2
SDJ
3577 bp_longjmp_master,
3578 &internal_breakpoint_ops);
d28cd78a 3579 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3580 b->enable_state = bp_disabled;
3581 }
3582
3583 continue;
3584 }
3585
0569175e
TSD
3586 if (!gdbarch_get_longjmp_target_p (gdbarch))
3587 continue;
3588
17450429 3589 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3590 {
3591 struct breakpoint *b;
af02033e 3592 const char *func_name;
17450429 3593 CORE_ADDR addr;
67994074 3594 struct explicit_location explicit_loc;
6c95b8df 3595
3b7344d5 3596 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3597 continue;
0fd8e87f 3598
17450429 3599 func_name = longjmp_names[i];
3b7344d5 3600 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3601 {
3b7344d5 3602 struct bound_minimal_symbol m;
17450429
PP
3603
3604 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3605 if (m.minsym == NULL)
17450429
PP
3606 {
3607 /* Prevent future lookups in this objfile. */
3b7344d5 3608 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3609 continue;
3610 }
3611 bp_objfile_data->longjmp_msym[i] = m;
3612 }
3613
77e371c0 3614 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3615 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3616 &internal_breakpoint_ops);
67994074
KS
3617 initialize_explicit_location (&explicit_loc);
3618 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3619 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3620 b->enable_state = bp_disabled;
3621 }
0fd8e87f 3622 }
af02033e 3623 }
0fd8e87f
UW
3624}
3625
af02033e 3626/* Create a master std::terminate breakpoint. */
aa7d318d 3627static void
af02033e 3628create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3629{
3630 struct program_space *pspace;
af02033e 3631 const char *const func_name = "std::terminate()";
aa7d318d 3632
5ed8105e 3633 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3634
3635 ALL_PSPACES (pspace)
17450429
PP
3636 {
3637 struct objfile *objfile;
3638 CORE_ADDR addr;
3639
3640 set_current_program_space (pspace);
3641
aa7d318d
TT
3642 ALL_OBJFILES (objfile)
3643 {
3644 struct breakpoint *b;
17450429 3645 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3646 struct explicit_location explicit_loc;
aa7d318d 3647
17450429 3648 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3649
3b7344d5 3650 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3651 continue;
3652
3b7344d5 3653 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3654 {
3b7344d5 3655 struct bound_minimal_symbol m;
17450429
PP
3656
3657 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3658 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3659 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3660 {
3661 /* Prevent future lookups in this objfile. */
3b7344d5 3662 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3663 continue;
3664 }
3665 bp_objfile_data->terminate_msym = m;
3666 }
aa7d318d 3667
77e371c0 3668 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3669 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3670 bp_std_terminate_master,
3671 &internal_breakpoint_ops);
67994074
KS
3672 initialize_explicit_location (&explicit_loc);
3673 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3674 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3675 b->enable_state = bp_disabled;
3676 }
17450429 3677 }
aa7d318d
TT
3678}
3679
186c406b
TT
3680/* Install a master breakpoint on the unwinder's debug hook. */
3681
70221824 3682static void
186c406b
TT
3683create_exception_master_breakpoint (void)
3684{
3685 struct objfile *objfile;
17450429 3686 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3687
3688 ALL_OBJFILES (objfile)
3689 {
17450429
PP
3690 struct breakpoint *b;
3691 struct gdbarch *gdbarch;
3692 struct breakpoint_objfile_data *bp_objfile_data;
3693 CORE_ADDR addr;
67994074 3694 struct explicit_location explicit_loc;
17450429
PP
3695
3696 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3697
28106bc2
SDJ
3698 /* We prefer the SystemTap probe point if it exists. */
3699 if (!bp_objfile_data->exception_searched)
3700 {
25f9533e
SDJ
3701 VEC (probe_p) *ret;
3702
3703 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3704
3705 if (ret != NULL)
3706 {
3707 /* We are only interested in checking one element. */
3708 struct probe *p = VEC_index (probe_p, ret, 0);
3709
3710 if (!can_evaluate_probe_arguments (p))
3711 {
3712 /* We cannot use the probe interface here, because it does
3713 not know how to evaluate arguments. */
3714 VEC_free (probe_p, ret);
3715 ret = NULL;
3716 }
3717 }
3718 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3719 bp_objfile_data->exception_searched = 1;
3720 }
3721
3722 if (bp_objfile_data->exception_probes != NULL)
3723 {
3724 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3725 int i;
3726 struct probe *probe;
3727
3728 for (i = 0;
3729 VEC_iterate (probe_p,
3730 bp_objfile_data->exception_probes,
3731 i, probe);
3732 ++i)
3733 {
3734 struct breakpoint *b;
3735
729662a5
TT
3736 b = create_internal_breakpoint (gdbarch,
3737 get_probe_address (probe,
3738 objfile),
28106bc2
SDJ
3739 bp_exception_master,
3740 &internal_breakpoint_ops);
d28cd78a 3741 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3742 b->enable_state = bp_disabled;
3743 }
3744
3745 continue;
3746 }
3747
3748 /* Otherwise, try the hook function. */
3749
3b7344d5 3750 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3751 continue;
3752
3753 gdbarch = get_objfile_arch (objfile);
186c406b 3754
3b7344d5 3755 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3756 {
3b7344d5 3757 struct bound_minimal_symbol debug_hook;
186c406b 3758
17450429 3759 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3760 if (debug_hook.minsym == NULL)
17450429 3761 {
3b7344d5 3762 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3763 continue;
3764 }
3765
3766 bp_objfile_data->exception_msym = debug_hook;
186c406b 3767 }
17450429 3768
77e371c0 3769 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3770 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3771 &current_target);
06edf0c0
PA
3772 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3773 &internal_breakpoint_ops);
67994074
KS
3774 initialize_explicit_location (&explicit_loc);
3775 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3776 b->location = new_explicit_location (&explicit_loc);
17450429 3777 b->enable_state = bp_disabled;
186c406b 3778 }
186c406b
TT
3779}
3780
9ef9e6a6
KS
3781/* Does B have a location spec? */
3782
3783static int
3784breakpoint_event_location_empty_p (const struct breakpoint *b)
3785{
d28cd78a 3786 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3787}
3788
c906108c 3789void
fba45db2 3790update_breakpoints_after_exec (void)
c906108c 3791{
35df4500 3792 struct breakpoint *b, *b_tmp;
876fa593 3793 struct bp_location *bploc, **bplocp_tmp;
c906108c 3794
25b22b0a
PA
3795 /* We're about to delete breakpoints from GDB's lists. If the
3796 INSERTED flag is true, GDB will try to lift the breakpoints by
3797 writing the breakpoints' "shadow contents" back into memory. The
3798 "shadow contents" are NOT valid after an exec, so GDB should not
3799 do that. Instead, the target is responsible from marking
3800 breakpoints out as soon as it detects an exec. We don't do that
3801 here instead, because there may be other attempts to delete
3802 breakpoints after detecting an exec and before reaching here. */
876fa593 3803 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3804 if (bploc->pspace == current_program_space)
3805 gdb_assert (!bploc->inserted);
c906108c 3806
35df4500 3807 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3808 {
6c95b8df
PA
3809 if (b->pspace != current_program_space)
3810 continue;
3811
4a64f543 3812 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3813 if (b->type == bp_shlib_event)
3814 {
3815 delete_breakpoint (b);
3816 continue;
3817 }
c906108c 3818
4a64f543 3819 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3820 if (b->type == bp_jit_event)
3821 {
3822 delete_breakpoint (b);
3823 continue;
3824 }
3825
1900040c 3826 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3827 as must overlay event and longjmp master breakpoints. */
3828 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3829 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3830 || b->type == bp_exception_master)
c4093a6a
JM
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
4a64f543 3836 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3837 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3838 {
3839 delete_breakpoint (b);
3840 continue;
3841 }
3842
7c16b83e
PA
3843 /* Just like single-step breakpoints. */
3844 if (b->type == bp_single_step)
3845 {
3846 delete_breakpoint (b);
3847 continue;
3848 }
3849
611c83ae
PA
3850 /* Longjmp and longjmp-resume breakpoints are also meaningless
3851 after an exec. */
186c406b 3852 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3853 || b->type == bp_longjmp_call_dummy
186c406b 3854 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3855 {
3856 delete_breakpoint (b);
3857 continue;
3858 }
3859
ce78b96d
JB
3860 if (b->type == bp_catchpoint)
3861 {
3862 /* For now, none of the bp_catchpoint breakpoints need to
3863 do anything at this point. In the future, if some of
3864 the catchpoints need to something, we will need to add
3865 a new method, and call this method from here. */
3866 continue;
3867 }
3868
c5aa993b
JM
3869 /* bp_finish is a special case. The only way we ought to be able
3870 to see one of these when an exec() has happened, is if the user
3871 caught a vfork, and then said "finish". Ordinarily a finish just
3872 carries them to the call-site of the current callee, by setting
3873 a temporary bp there and resuming. But in this case, the finish
3874 will carry them entirely through the vfork & exec.
3875
3876 We don't want to allow a bp_finish to remain inserted now. But
3877 we can't safely delete it, 'cause finish_command has a handle to
3878 the bp on a bpstat, and will later want to delete it. There's a
3879 chance (and I've seen it happen) that if we delete the bp_finish
3880 here, that its storage will get reused by the time finish_command
3881 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3882 We really must allow finish_command to delete a bp_finish.
3883
e5dd4106 3884 In the absence of a general solution for the "how do we know
53a5351d
JM
3885 it's safe to delete something others may have handles to?"
3886 problem, what we'll do here is just uninsert the bp_finish, and
3887 let finish_command delete it.
3888
3889 (We know the bp_finish is "doomed" in the sense that it's
3890 momentary, and will be deleted as soon as finish_command sees
3891 the inferior stopped. So it doesn't matter that the bp's
3892 address is probably bogus in the new a.out, unlike e.g., the
3893 solib breakpoints.) */
c5aa993b 3894
c5aa993b
JM
3895 if (b->type == bp_finish)
3896 {
3897 continue;
3898 }
3899
3900 /* Without a symbolic address, we have little hope of the
3901 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3902 a.out. */
9ef9e6a6 3903 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3904 {
3905 delete_breakpoint (b);
3906 continue;
3907 }
c5aa993b 3908 }
c906108c
SS
3909}
3910
3911int
d80ee84f 3912detach_breakpoints (ptid_t ptid)
c906108c 3913{
35df4500 3914 struct bp_location *bl, **blp_tmp;
3a1bae8e 3915 int val = 0;
ce696e05 3916 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3917 struct inferior *inf = current_inferior ();
c5aa993b 3918
dfd4cc63 3919 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3920 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3921
6c95b8df 3922 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3923 inferior_ptid = ptid;
35df4500 3924 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3925 {
35df4500 3926 if (bl->pspace != inf->pspace)
6c95b8df
PA
3927 continue;
3928
bd9673a4
PW
3929 /* This function must physically remove breakpoints locations
3930 from the specified ptid, without modifying the breakpoint
3931 package's state. Locations of type bp_loc_other are only
3932 maintained at GDB side. So, there is no need to remove
3933 these bp_loc_other locations. Moreover, removing these
3934 would modify the breakpoint package's state. */
3935 if (bl->loc_type == bp_loc_other)
3936 continue;
3937
35df4500 3938 if (bl->inserted)
b2b6a7da 3939 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3940 }
d03285ec 3941
ce696e05 3942 do_cleanups (old_chain);
3a1bae8e 3943 return val;
c906108c
SS
3944}
3945
35df4500 3946/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3947 Note that this is used to detach breakpoints from a child fork.
3948 When we get here, the child isn't in the inferior list, and neither
3949 do we have objects to represent its address space --- we should
35df4500 3950 *not* look at bl->pspace->aspace here. */
6c95b8df 3951
c906108c 3952static int
b2b6a7da 3953remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3954{
3955 int val;
c5aa993b 3956
35df4500
TJB
3957 /* BL is never in moribund_locations by our callers. */
3958 gdb_assert (bl->owner != NULL);
2bdf28a0 3959
74960c60
VP
3960 /* The type of none suggests that owner is actually deleted.
3961 This should not ever happen. */
35df4500 3962 gdb_assert (bl->owner->type != bp_none);
0bde7532 3963
35df4500
TJB
3964 if (bl->loc_type == bp_loc_software_breakpoint
3965 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3966 {
c02f5703
MS
3967 /* "Normal" instruction breakpoint: either the standard
3968 trap-instruction bp (bp_breakpoint), or a
3969 bp_hardware_breakpoint. */
3970
3971 /* First check to see if we have to handle an overlay. */
3972 if (overlay_debugging == ovly_off
35df4500
TJB
3973 || bl->section == NULL
3974 || !(section_is_overlay (bl->section)))
c02f5703
MS
3975 {
3976 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3977
3978 /* If we're trying to uninsert a memory breakpoint that we
3979 know is set in a dynamic object that is marked
3980 shlib_disabled, then either the dynamic object was
3981 removed with "remove-symbol-file" or with
3982 "nosharedlibrary". In the former case, we don't know
3983 whether another dynamic object might have loaded over the
3984 breakpoint's address -- the user might well let us know
3985 about it next with add-symbol-file (the whole point of
d03de421 3986 add-symbol-file is letting the user manually maintain a
08351840
PA
3987 list of dynamically loaded objects). If we have the
3988 breakpoint's shadow memory, that is, this is a software
3989 breakpoint managed by GDB, check whether the breakpoint
3990 is still inserted in memory, to avoid overwriting wrong
3991 code with stale saved shadow contents. Note that HW
3992 breakpoints don't have shadow memory, as they're
3993 implemented using a mechanism that is not dependent on
3994 being able to modify the target's memory, and as such
3995 they should always be removed. */
3996 if (bl->shlib_disabled
3997 && bl->target_info.shadow_len != 0
3998 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3999 val = 0;
4000 else
73971819 4001 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 4002 }
c906108c
SS
4003 else
4004 {
4a64f543 4005 /* This breakpoint is in an overlay section.
c02f5703
MS
4006 Did we set a breakpoint at the LMA? */
4007 if (!overlay_events_enabled)
4008 {
4009 /* Yes -- overlay event support is not active, so we
4010 should have set a breakpoint at the LMA. Remove it.
4011 */
c02f5703
MS
4012 /* Ignore any failures: if the LMA is in ROM, we will
4013 have already warned when we failed to insert it. */
35df4500
TJB
4014 if (bl->loc_type == bp_loc_hardware_breakpoint)
4015 target_remove_hw_breakpoint (bl->gdbarch,
4016 &bl->overlay_target_info);
c02f5703 4017 else
35df4500 4018 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4019 &bl->overlay_target_info,
4020 reason);
c02f5703
MS
4021 }
4022 /* Did we set a breakpoint at the VMA?
4023 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4024 if (bl->inserted)
c906108c 4025 {
c02f5703
MS
4026 /* Yes -- remove it. Previously we did not bother to
4027 remove the breakpoint if the section had been
4028 unmapped, but let's not rely on that being safe. We
4029 don't know what the overlay manager might do. */
aa67235e
UW
4030
4031 /* However, we should remove *software* breakpoints only
4032 if the section is still mapped, or else we overwrite
4033 wrong code with the saved shadow contents. */
348d480f
PA
4034 if (bl->loc_type == bp_loc_hardware_breakpoint
4035 || section_is_mapped (bl->section))
73971819 4036 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4037 else
4038 val = 0;
c906108c 4039 }
c02f5703
MS
4040 else
4041 {
4042 /* No -- not inserted, so no need to remove. No error. */
4043 val = 0;
4044 }
c906108c 4045 }
879d1e6b 4046
08351840
PA
4047 /* In some cases, we might not be able to remove a breakpoint in
4048 a shared library that has already been removed, but we have
4049 not yet processed the shlib unload event. Similarly for an
4050 unloaded add-symbol-file object - the user might not yet have
4051 had the chance to remove-symbol-file it. shlib_disabled will
4052 be set if the library/object has already been removed, but
4053 the breakpoint hasn't been uninserted yet, e.g., after
4054 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4055 always-inserted mode. */
076855f9 4056 if (val
08351840
PA
4057 && (bl->loc_type == bp_loc_software_breakpoint
4058 && (bl->shlib_disabled
4059 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4060 || shared_objfile_contains_address_p (bl->pspace,
4061 bl->address))))
879d1e6b
UW
4062 val = 0;
4063
c906108c
SS
4064 if (val)
4065 return val;
b2b6a7da 4066 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4067 }
35df4500 4068 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4069 {
77b06cd7
TJB
4070 gdb_assert (bl->owner->ops != NULL
4071 && bl->owner->ops->remove_location != NULL);
4072
b2b6a7da 4073 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4074 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4075
c906108c 4076 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4077 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4078 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4079 bl->owner->number);
c906108c 4080 }
35df4500
TJB
4081 else if (bl->owner->type == bp_catchpoint
4082 && breakpoint_enabled (bl->owner)
4083 && !bl->duplicate)
ce78b96d 4084 {
77b06cd7
TJB
4085 gdb_assert (bl->owner->ops != NULL
4086 && bl->owner->ops->remove_location != NULL);
ce78b96d 4087
73971819 4088 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4089 if (val)
4090 return val;
77b06cd7 4091
b2b6a7da 4092 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4093 }
c906108c
SS
4094
4095 return 0;
4096}
4097
6c95b8df 4098static int
834c0d03 4099remove_breakpoint (struct bp_location *bl)
6c95b8df 4100{
35df4500
TJB
4101 /* BL is never in moribund_locations by our callers. */
4102 gdb_assert (bl->owner != NULL);
2bdf28a0 4103
6c95b8df
PA
4104 /* The type of none suggests that owner is actually deleted.
4105 This should not ever happen. */
35df4500 4106 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4107
5ed8105e 4108 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4109
35df4500 4110 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4111
5ed8105e 4112 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4113}
4114
c906108c
SS
4115/* Clear the "inserted" flag in all breakpoints. */
4116
25b22b0a 4117void
fba45db2 4118mark_breakpoints_out (void)
c906108c 4119{
35df4500 4120 struct bp_location *bl, **blp_tmp;
c906108c 4121
35df4500 4122 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4123 if (bl->pspace == current_program_space)
35df4500 4124 bl->inserted = 0;
c906108c
SS
4125}
4126
53a5351d
JM
4127/* Clear the "inserted" flag in all breakpoints and delete any
4128 breakpoints which should go away between runs of the program.
c906108c
SS
4129
4130 Plus other such housekeeping that has to be done for breakpoints
4131 between runs.
4132
53a5351d
JM
4133 Note: this function gets called at the end of a run (by
4134 generic_mourn_inferior) and when a run begins (by
4a64f543 4135 init_wait_for_inferior). */
c906108c
SS
4136
4137
4138
4139void
fba45db2 4140breakpoint_init_inferior (enum inf_context context)
c906108c 4141{
35df4500 4142 struct breakpoint *b, *b_tmp;
870f88f7 4143 struct bp_location *bl;
1c5cfe86 4144 int ix;
6c95b8df 4145 struct program_space *pspace = current_program_space;
c906108c 4146
50c71eaf
PA
4147 /* If breakpoint locations are shared across processes, then there's
4148 nothing to do. */
f5656ead 4149 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4150 return;
4151
1a853c52 4152 mark_breakpoints_out ();
075f6582 4153
35df4500 4154 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4155 {
6c95b8df
PA
4156 if (b->loc && b->loc->pspace != pspace)
4157 continue;
4158
c5aa993b
JM
4159 switch (b->type)
4160 {
4161 case bp_call_dummy:
e2e4d78b 4162 case bp_longjmp_call_dummy:
c906108c 4163
c5aa993b 4164 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4165 cause problems when the inferior is rerun, so we better get
4166 rid of it. */
4167
4168 case bp_watchpoint_scope:
4169
4170 /* Also get rid of scope breakpoints. */
4171
4172 case bp_shlib_event:
4173
4174 /* Also remove solib event breakpoints. Their addresses may
4175 have changed since the last time we ran the program.
4176 Actually we may now be debugging against different target;
4177 and so the solib backend that installed this breakpoint may
4178 not be used in by the target. E.g.,
4179
4180 (gdb) file prog-linux
4181 (gdb) run # native linux target
4182 ...
4183 (gdb) kill
4184 (gdb) file prog-win.exe
4185 (gdb) tar rem :9999 # remote Windows gdbserver.
4186 */
c906108c 4187
f59f708a
PA
4188 case bp_step_resume:
4189
4190 /* Also remove step-resume breakpoints. */
4191
7c16b83e
PA
4192 case bp_single_step:
4193
4194 /* Also remove single-step breakpoints. */
4195
c5aa993b
JM
4196 delete_breakpoint (b);
4197 break;
c906108c 4198
c5aa993b
JM
4199 case bp_watchpoint:
4200 case bp_hardware_watchpoint:
4201 case bp_read_watchpoint:
4202 case bp_access_watchpoint:
3a5c3e22
PA
4203 {
4204 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4205
3a5c3e22
PA
4206 /* Likewise for watchpoints on local expressions. */
4207 if (w->exp_valid_block != NULL)
4208 delete_breakpoint (b);
63000888 4209 else
3a5c3e22 4210 {
63000888
PA
4211 /* Get rid of existing locations, which are no longer
4212 valid. New ones will be created in
4213 update_watchpoint, when the inferior is restarted.
4214 The next update_global_location_list call will
4215 garbage collect them. */
4216 b->loc = NULL;
4217
4218 if (context == inf_starting)
4219 {
4220 /* Reset val field to force reread of starting value in
4221 insert_breakpoints. */
4222 if (w->val)
4223 value_free (w->val);
4224 w->val = NULL;
4225 w->val_valid = 0;
4226 }
4227 }
3a5c3e22 4228 }
c5aa993b
JM
4229 break;
4230 default:
c5aa993b
JM
4231 break;
4232 }
4233 }
1c5cfe86
PA
4234
4235 /* Get rid of the moribund locations. */
35df4500
TJB
4236 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4237 decref_bp_location (&bl);
1c5cfe86 4238 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4239}
4240
6c95b8df
PA
4241/* These functions concern about actual breakpoints inserted in the
4242 target --- to e.g. check if we need to do decr_pc adjustment or if
4243 we need to hop over the bkpt --- so we check for address space
4244 match, not program space. */
4245
c2c6d25f
JM
4246/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4247 exists at PC. It returns ordinary_breakpoint_here if it's an
4248 ordinary breakpoint, or permanent_breakpoint_here if it's a
4249 permanent breakpoint.
4250 - When continuing from a location with an ordinary breakpoint, we
4251 actually single step once before calling insert_breakpoints.
e5dd4106 4252 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4253 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4254 the target, to advance the PC past the breakpoint. */
c906108c 4255
c2c6d25f 4256enum breakpoint_here
6c95b8df 4257breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4258{
35df4500 4259 struct bp_location *bl, **blp_tmp;
c2c6d25f 4260 int any_breakpoint_here = 0;
c906108c 4261
35df4500 4262 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4263 {
35df4500
TJB
4264 if (bl->loc_type != bp_loc_software_breakpoint
4265 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4266 continue;
4267
f1310107 4268 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4269 if ((breakpoint_enabled (bl->owner)
1a853c52 4270 || bl->permanent)
f1310107 4271 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4272 {
4273 if (overlay_debugging
35df4500
TJB
4274 && section_is_overlay (bl->section)
4275 && !section_is_mapped (bl->section))
075f6582 4276 continue; /* unmapped overlay -- can't be a match */
1a853c52 4277 else if (bl->permanent)
075f6582
DJ
4278 return permanent_breakpoint_here;
4279 else
4280 any_breakpoint_here = 1;
4281 }
4282 }
c906108c 4283
f486487f 4284 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4285}
4286
d35ae833
PA
4287/* See breakpoint.h. */
4288
4289int
4290breakpoint_in_range_p (struct address_space *aspace,
4291 CORE_ADDR addr, ULONGEST len)
4292{
4293 struct bp_location *bl, **blp_tmp;
4294
4295 ALL_BP_LOCATIONS (bl, blp_tmp)
4296 {
4297 if (bl->loc_type != bp_loc_software_breakpoint
4298 && bl->loc_type != bp_loc_hardware_breakpoint)
4299 continue;
4300
4301 if ((breakpoint_enabled (bl->owner)
4302 || bl->permanent)
4303 && breakpoint_location_address_range_overlap (bl, aspace,
4304 addr, len))
4305 {
4306 if (overlay_debugging
4307 && section_is_overlay (bl->section)
4308 && !section_is_mapped (bl->section))
4309 {
4310 /* Unmapped overlay -- can't be a match. */
4311 continue;
4312 }
4313
4314 return 1;
4315 }
4316 }
4317
4318 return 0;
4319}
4320
1c5cfe86
PA
4321/* Return true if there's a moribund breakpoint at PC. */
4322
4323int
6c95b8df 4324moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4325{
4326 struct bp_location *loc;
4327 int ix;
4328
4329 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4330 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4331 return 1;
4332
4333 return 0;
4334}
c2c6d25f 4335
f7ce857f
PA
4336/* Returns non-zero iff BL is inserted at PC, in address space
4337 ASPACE. */
4338
4339static int
4340bp_location_inserted_here_p (struct bp_location *bl,
4341 struct address_space *aspace, CORE_ADDR pc)
4342{
4343 if (bl->inserted
4344 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4345 aspace, pc))
4346 {
4347 if (overlay_debugging
4348 && section_is_overlay (bl->section)
4349 && !section_is_mapped (bl->section))
4350 return 0; /* unmapped overlay -- can't be a match */
4351 else
4352 return 1;
4353 }
4354 return 0;
4355}
4356
a1fd2fa5 4357/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4358
4359int
a1fd2fa5 4360breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4361{
f7ce857f 4362 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4363
f7ce857f 4364 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4365 {
f7ce857f
PA
4366 struct bp_location *bl = *blp;
4367
35df4500
TJB
4368 if (bl->loc_type != bp_loc_software_breakpoint
4369 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4370 continue;
4371
f7ce857f
PA
4372 if (bp_location_inserted_here_p (bl, aspace, pc))
4373 return 1;
c5aa993b 4374 }
c36b740a
VP
4375 return 0;
4376}
4377
a1fd2fa5
PA
4378/* This function returns non-zero iff there is a software breakpoint
4379 inserted at PC. */
c36b740a
VP
4380
4381int
a1fd2fa5
PA
4382software_breakpoint_inserted_here_p (struct address_space *aspace,
4383 CORE_ADDR pc)
4fa8626c 4384{
f7ce857f 4385 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4386
f7ce857f 4387 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4388 {
f7ce857f
PA
4389 struct bp_location *bl = *blp;
4390
35df4500 4391 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4392 continue;
4393
f7ce857f
PA
4394 if (bp_location_inserted_here_p (bl, aspace, pc))
4395 return 1;
4fa8626c
DJ
4396 }
4397
4398 return 0;
9c02b525
PA
4399}
4400
4401/* See breakpoint.h. */
4402
4403int
4404hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4405 CORE_ADDR pc)
4406{
4407 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4408
4409 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4410 {
4411 struct bp_location *bl = *blp;
4412
4413 if (bl->loc_type != bp_loc_hardware_breakpoint)
4414 continue;
4415
4416 if (bp_location_inserted_here_p (bl, aspace, pc))
4417 return 1;
4418 }
4419
4420 return 0;
4fa8626c
DJ
4421}
4422
9093389c
PA
4423int
4424hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4425 CORE_ADDR addr, ULONGEST len)
4426{
4427 struct breakpoint *bpt;
4428
4429 ALL_BREAKPOINTS (bpt)
4430 {
4431 struct bp_location *loc;
4432
4433 if (bpt->type != bp_hardware_watchpoint
4434 && bpt->type != bp_access_watchpoint)
4435 continue;
4436
4437 if (!breakpoint_enabled (bpt))
4438 continue;
4439
4440 for (loc = bpt->loc; loc; loc = loc->next)
4441 if (loc->pspace->aspace == aspace && loc->inserted)
4442 {
4443 CORE_ADDR l, h;
4444
4445 /* Check for intersection. */
768adc05
PA
4446 l = std::max<CORE_ADDR> (loc->address, addr);
4447 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4448 if (l < h)
4449 return 1;
4450 }
4451 }
4452 return 0;
4453}
c906108c 4454\f
c5aa993b 4455
c906108c
SS
4456/* bpstat stuff. External routines' interfaces are documented
4457 in breakpoint.h. */
4458
4459int
c326b90e 4460is_catchpoint (struct breakpoint *ep)
c906108c 4461{
533be4dd 4462 return (ep->type == bp_catchpoint);
c906108c
SS
4463}
4464
f431efe5
PA
4465/* Frees any storage that is part of a bpstat. Does not walk the
4466 'next' chain. */
4467
4468static void
198757a8
VP
4469bpstat_free (bpstat bs)
4470{
4471 if (bs->old_val != NULL)
4472 value_free (bs->old_val);
9add0f1b 4473 decref_counted_command_line (&bs->commands);
f431efe5 4474 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4475 xfree (bs);
4476}
4477
c906108c
SS
4478/* Clear a bpstat so that it says we are not at any breakpoint.
4479 Also free any storage that is part of a bpstat. */
4480
4481void
fba45db2 4482bpstat_clear (bpstat *bsp)
c906108c
SS
4483{
4484 bpstat p;
4485 bpstat q;
4486
4487 if (bsp == 0)
4488 return;
4489 p = *bsp;
4490 while (p != NULL)
4491 {
4492 q = p->next;
198757a8 4493 bpstat_free (p);
c906108c
SS
4494 p = q;
4495 }
4496 *bsp = NULL;
4497}
4498
4499/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4500 is part of the bpstat is copied as well. */
4501
4502bpstat
fba45db2 4503bpstat_copy (bpstat bs)
c906108c
SS
4504{
4505 bpstat p = NULL;
4506 bpstat tmp;
4507 bpstat retval = NULL;
4508
4509 if (bs == NULL)
4510 return bs;
4511
4512 for (; bs != NULL; bs = bs->next)
4513 {
4514 tmp = (bpstat) xmalloc (sizeof (*tmp));
4515 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4516 incref_counted_command_line (tmp->commands);
f431efe5 4517 incref_bp_location (tmp->bp_location_at);
31cc81e9 4518 if (bs->old_val != NULL)
3c3185ac
JK
4519 {
4520 tmp->old_val = value_copy (bs->old_val);
4521 release_value (tmp->old_val);
4522 }
31cc81e9 4523
c906108c
SS
4524 if (p == NULL)
4525 /* This is the first thing in the chain. */
4526 retval = tmp;
4527 else
4528 p->next = tmp;
4529 p = tmp;
4530 }
4531 p->next = NULL;
4532 return retval;
4533}
4534
4a64f543 4535/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4536
4537bpstat
fba45db2 4538bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4539{
c5aa993b
JM
4540 if (bsp == NULL)
4541 return NULL;
c906108c 4542
c5aa993b
JM
4543 for (; bsp != NULL; bsp = bsp->next)
4544 {
f431efe5 4545 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4546 return bsp;
4547 }
c906108c
SS
4548 return NULL;
4549}
4550
ab04a2af
TT
4551/* See breakpoint.h. */
4552
47591c29 4553int
427cd150 4554bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4555{
ab04a2af
TT
4556 for (; bsp != NULL; bsp = bsp->next)
4557 {
427cd150
TT
4558 if (bsp->breakpoint_at == NULL)
4559 {
4560 /* A moribund location can never explain a signal other than
4561 GDB_SIGNAL_TRAP. */
4562 if (sig == GDB_SIGNAL_TRAP)
47591c29 4563 return 1;
427cd150
TT
4564 }
4565 else
47591c29
PA
4566 {
4567 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4568 sig))
4569 return 1;
4570 }
ab04a2af
TT
4571 }
4572
47591c29 4573 return 0;
ab04a2af
TT
4574}
4575
4a64f543
MS
4576/* Put in *NUM the breakpoint number of the first breakpoint we are
4577 stopped at. *BSP upon return is a bpstat which points to the
4578 remaining breakpoints stopped at (but which is not guaranteed to be
4579 good for anything but further calls to bpstat_num).
4580
8671a17b
PA
4581 Return 0 if passed a bpstat which does not indicate any breakpoints.
4582 Return -1 if stopped at a breakpoint that has been deleted since
4583 we set it.
4584 Return 1 otherwise. */
c906108c
SS
4585
4586int
8671a17b 4587bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4588{
4589 struct breakpoint *b;
4590
4591 if ((*bsp) == NULL)
4592 return 0; /* No more breakpoint values */
8671a17b 4593
4a64f543
MS
4594 /* We assume we'll never have several bpstats that correspond to a
4595 single breakpoint -- otherwise, this function might return the
4596 same number more than once and this will look ugly. */
f431efe5 4597 b = (*bsp)->breakpoint_at;
8671a17b
PA
4598 *bsp = (*bsp)->next;
4599 if (b == NULL)
4600 return -1; /* breakpoint that's been deleted since */
4601
4602 *num = b->number; /* We have its number */
4603 return 1;
c906108c
SS
4604}
4605
e93ca019 4606/* See breakpoint.h. */
c906108c
SS
4607
4608void
e93ca019 4609bpstat_clear_actions (void)
c906108c 4610{
e93ca019
JK
4611 struct thread_info *tp;
4612 bpstat bs;
4613
4614 if (ptid_equal (inferior_ptid, null_ptid))
4615 return;
4616
4617 tp = find_thread_ptid (inferior_ptid);
4618 if (tp == NULL)
4619 return;
4620
4621 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4622 {
9add0f1b 4623 decref_counted_command_line (&bs->commands);
abf85f46 4624
c906108c
SS
4625 if (bs->old_val != NULL)
4626 {
4627 value_free (bs->old_val);
4628 bs->old_val = NULL;
4629 }
4630 }
4631}
4632
f3b1572e
PA
4633/* Called when a command is about to proceed the inferior. */
4634
4635static void
4636breakpoint_about_to_proceed (void)
4637{
4638 if (!ptid_equal (inferior_ptid, null_ptid))
4639 {
4640 struct thread_info *tp = inferior_thread ();
4641
4642 /* Allow inferior function calls in breakpoint commands to not
4643 interrupt the command list. When the call finishes
4644 successfully, the inferior will be standing at the same
4645 breakpoint as if nothing happened. */
16c381f0 4646 if (tp->control.in_infcall)
f3b1572e
PA
4647 return;
4648 }
4649
4650 breakpoint_proceeded = 1;
4651}
4652
4a64f543
MS
4653/* Stub for cleaning up our state if we error-out of a breakpoint
4654 command. */
c906108c 4655static void
4efb68b1 4656cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4657{
4658 executing_breakpoint_commands = 0;
4659}
4660
abf85f46
JK
4661/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4662 or its equivalent. */
4663
4664static int
4665command_line_is_silent (struct command_line *cmd)
4666{
4f45d445 4667 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4668}
4669
4a64f543
MS
4670/* Execute all the commands associated with all the breakpoints at
4671 this location. Any of these commands could cause the process to
4672 proceed beyond this point, etc. We look out for such changes by
4673 checking the global "breakpoint_proceeded" after each command.
c906108c 4674
347bddb7
PA
4675 Returns true if a breakpoint command resumed the inferior. In that
4676 case, it is the caller's responsibility to recall it again with the
4677 bpstat of the current thread. */
4678
4679static int
4680bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4681{
4682 bpstat bs;
4683 struct cleanup *old_chain;
347bddb7 4684 int again = 0;
c906108c
SS
4685
4686 /* Avoid endless recursion if a `source' command is contained
4687 in bs->commands. */
4688 if (executing_breakpoint_commands)
347bddb7 4689 return 0;
c906108c
SS
4690
4691 executing_breakpoint_commands = 1;
4692 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4693
1ac32117 4694 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4695
4a64f543 4696 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4697 bs = *bsp;
4698
4699 breakpoint_proceeded = 0;
4700 for (; bs != NULL; bs = bs->next)
4701 {
9add0f1b 4702 struct counted_command_line *ccmd;
6c50ab1c
JB
4703 struct command_line *cmd;
4704 struct cleanup *this_cmd_tree_chain;
4705
4706 /* Take ownership of the BSP's command tree, if it has one.
4707
4708 The command tree could legitimately contain commands like
4709 'step' and 'next', which call clear_proceed_status, which
4710 frees stop_bpstat's command tree. To make sure this doesn't
4711 free the tree we're executing out from under us, we need to
4712 take ownership of the tree ourselves. Since a given bpstat's
4713 commands are only executed once, we don't need to copy it; we
4714 can clear the pointer in the bpstat, and make sure we free
4715 the tree when we're done. */
9add0f1b
TT
4716 ccmd = bs->commands;
4717 bs->commands = NULL;
abf85f46
JK
4718 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4719 cmd = ccmd ? ccmd->commands : NULL;
4720 if (command_line_is_silent (cmd))
4721 {
4722 /* The action has been already done by bpstat_stop_status. */
4723 cmd = cmd->next;
4724 }
6c50ab1c 4725
c906108c
SS
4726 while (cmd != NULL)
4727 {
4728 execute_control_command (cmd);
4729
4730 if (breakpoint_proceeded)
4731 break;
4732 else
4733 cmd = cmd->next;
4734 }
6c50ab1c
JB
4735
4736 /* We can free this command tree now. */
4737 do_cleanups (this_cmd_tree_chain);
4738
c906108c 4739 if (breakpoint_proceeded)
32c1e744 4740 {
cb814510 4741 if (current_ui->async)
347bddb7
PA
4742 /* If we are in async mode, then the target might be still
4743 running, not stopped at any breakpoint, so nothing for
4744 us to do here -- just return to the event loop. */
4745 ;
32c1e744
VP
4746 else
4747 /* In sync mode, when execute_control_command returns
4748 we're already standing on the next breakpoint.
347bddb7
PA
4749 Breakpoint commands for that stop were not run, since
4750 execute_command does not run breakpoint commands --
4751 only command_line_handler does, but that one is not
4752 involved in execution of breakpoint commands. So, we
4753 can now execute breakpoint commands. It should be
4754 noted that making execute_command do bpstat actions is
4755 not an option -- in this case we'll have recursive
4756 invocation of bpstat for each breakpoint with a
4757 command, and can easily blow up GDB stack. Instead, we
4758 return true, which will trigger the caller to recall us
4759 with the new stop_bpstat. */
4760 again = 1;
4761 break;
32c1e744 4762 }
c906108c 4763 }
c2b8ed2c 4764 do_cleanups (old_chain);
347bddb7
PA
4765 return again;
4766}
4767
4768void
4769bpstat_do_actions (void)
4770{
353d1d73
JK
4771 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4772
347bddb7
PA
4773 /* Do any commands attached to breakpoint we are stopped at. */
4774 while (!ptid_equal (inferior_ptid, null_ptid)
4775 && target_has_execution
4776 && !is_exited (inferior_ptid)
4777 && !is_executing (inferior_ptid))
4778 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4779 and only return when it is stopped at the next breakpoint, we
4780 keep doing breakpoint actions until it returns false to
4781 indicate the inferior was not resumed. */
16c381f0 4782 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4783 break;
353d1d73
JK
4784
4785 discard_cleanups (cleanup_if_error);
c906108c
SS
4786}
4787
fa4727a6
DJ
4788/* Print out the (old or new) value associated with a watchpoint. */
4789
4790static void
4791watchpoint_value_print (struct value *val, struct ui_file *stream)
4792{
4793 if (val == NULL)
4794 fprintf_unfiltered (stream, _("<unreadable>"));
4795 else
79a45b7d
TT
4796 {
4797 struct value_print_options opts;
4798 get_user_print_options (&opts);
4799 value_print (val, stream, &opts);
4800 }
fa4727a6
DJ
4801}
4802
f303dbd6
PA
4803/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4804 debugging multiple threads. */
4805
4806void
4807maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4808{
112e8700 4809 if (uiout->is_mi_like_p ())
f303dbd6
PA
4810 return;
4811
112e8700 4812 uiout->text ("\n");
f303dbd6
PA
4813
4814 if (show_thread_that_caused_stop ())
4815 {
4816 const char *name;
4817 struct thread_info *thr = inferior_thread ();
4818
112e8700
SM
4819 uiout->text ("Thread ");
4820 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4821
4822 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4823 if (name != NULL)
4824 {
112e8700
SM
4825 uiout->text (" \"");
4826 uiout->field_fmt ("name", "%s", name);
4827 uiout->text ("\"");
f303dbd6
PA
4828 }
4829
112e8700 4830 uiout->text (" hit ");
f303dbd6
PA
4831 }
4832}
4833
e514a9d6 4834/* Generic routine for printing messages indicating why we
4a64f543 4835 stopped. The behavior of this function depends on the value
e514a9d6
JM
4836 'print_it' in the bpstat structure. Under some circumstances we
4837 may decide not to print anything here and delegate the task to
4a64f543 4838 normal_stop(). */
e514a9d6
JM
4839
4840static enum print_stop_action
4841print_bp_stop_message (bpstat bs)
4842{
4843 switch (bs->print_it)
4844 {
4845 case print_it_noop:
4a64f543 4846 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4847 return PRINT_UNKNOWN;
4848 break;
4849
4850 case print_it_done:
4851 /* We still want to print the frame, but we already printed the
4a64f543 4852 relevant messages. */
e514a9d6
JM
4853 return PRINT_SRC_AND_LOC;
4854 break;
4855
4856 case print_it_normal:
4f8d1dc6 4857 {
f431efe5
PA
4858 struct breakpoint *b = bs->breakpoint_at;
4859
1a6a67de
TJB
4860 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4861 which has since been deleted. */
4862 if (b == NULL)
4863 return PRINT_UNKNOWN;
4864
348d480f
PA
4865 /* Normal case. Call the breakpoint's print_it method. */
4866 return b->ops->print_it (bs);
4f8d1dc6 4867 }
348d480f 4868 break;
3086aeae 4869
e514a9d6 4870 default:
8e65ff28 4871 internal_error (__FILE__, __LINE__,
e2e0b3e5 4872 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4873 break;
c906108c 4874 }
c906108c
SS
4875}
4876
edcc5120
TT
4877/* A helper function that prints a shared library stopped event. */
4878
4879static void
4880print_solib_event (int is_catchpoint)
4881{
4882 int any_deleted
4883 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4884 int any_added
4885 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4886
4887 if (!is_catchpoint)
4888 {
4889 if (any_added || any_deleted)
112e8700 4890 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4891 else
112e8700
SM
4892 current_uiout->text (_("Stopped due to shared library event (no "
4893 "libraries added or removed)\n"));
edcc5120
TT
4894 }
4895
112e8700
SM
4896 if (current_uiout->is_mi_like_p ())
4897 current_uiout->field_string ("reason",
4898 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4899
4900 if (any_deleted)
4901 {
edcc5120
TT
4902 char *name;
4903 int ix;
4904
112e8700 4905 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4906 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4907 for (ix = 0;
4908 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4909 ix, name);
4910 ++ix)
4911 {
4912 if (ix > 0)
112e8700
SM
4913 current_uiout->text (" ");
4914 current_uiout->field_string ("library", name);
4915 current_uiout->text ("\n");
edcc5120 4916 }
edcc5120
TT
4917 }
4918
4919 if (any_added)
4920 {
4921 struct so_list *iter;
4922 int ix;
edcc5120 4923
112e8700 4924 current_uiout->text (_(" Inferior loaded "));
10f489e5 4925 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4926 for (ix = 0;
4927 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4928 ix, iter);
4929 ++ix)
4930 {
4931 if (ix > 0)
112e8700
SM
4932 current_uiout->text (" ");
4933 current_uiout->field_string ("library", iter->so_name);
4934 current_uiout->text ("\n");
edcc5120 4935 }
edcc5120
TT
4936 }
4937}
4938
e514a9d6
JM
4939/* Print a message indicating what happened. This is called from
4940 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4941 list - a list of the eventpoints that caused this stop. KIND is
4942 the target_waitkind for the stopping event. This
e514a9d6
JM
4943 routine calls the generic print routine for printing a message
4944 about reasons for stopping. This will print (for example) the
4945 "Breakpoint n," part of the output. The return value of this
4946 routine is one of:
c906108c 4947
4a64f543 4948 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4949 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4950 code to print the location. An example is
c5aa993b
JM
4951 "Breakpoint 1, " which should be followed by
4952 the location.
917317f4 4953 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4954 to also print the location part of the message.
4955 An example is the catch/throw messages, which
4a64f543 4956 don't require a location appended to the end.
917317f4 4957 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4958 further info to be printed. */
c906108c 4959
917317f4 4960enum print_stop_action
36dfb11c 4961bpstat_print (bpstat bs, int kind)
c906108c 4962{
f486487f 4963 enum print_stop_action val;
c5aa993b 4964
c906108c 4965 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4966 (Currently all watchpoints go on the bpstat whether hit or not.
4967 That probably could (should) be changed, provided care is taken
c906108c 4968 with respect to bpstat_explains_signal). */
e514a9d6
JM
4969 for (; bs; bs = bs->next)
4970 {
4971 val = print_bp_stop_message (bs);
4972 if (val == PRINT_SRC_ONLY
4973 || val == PRINT_SRC_AND_LOC
4974 || val == PRINT_NOTHING)
4975 return val;
4976 }
c906108c 4977
36dfb11c
TT
4978 /* If we had hit a shared library event breakpoint,
4979 print_bp_stop_message would print out this message. If we hit an
4980 OS-level shared library event, do the same thing. */
4981 if (kind == TARGET_WAITKIND_LOADED)
4982 {
edcc5120 4983 print_solib_event (0);
36dfb11c
TT
4984 return PRINT_NOTHING;
4985 }
4986
e514a9d6 4987 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4988 with and nothing was printed. */
917317f4 4989 return PRINT_UNKNOWN;
c906108c
SS
4990}
4991
c42bd95a
DE
4992/* Evaluate the expression EXP and return 1 if value is zero.
4993 This returns the inverse of the condition because it is called
4994 from catch_errors which returns 0 if an exception happened, and if an
4995 exception happens we want execution to stop.
4a64f543 4996 The argument is a "struct expression *" that has been cast to a
c42bd95a 4997 "void *" to make it pass through catch_errors. */
c906108c
SS
4998
4999static int
4efb68b1 5000breakpoint_cond_eval (void *exp)
c906108c 5001{
278cd55f 5002 struct value *mark = value_mark ();
c5aa993b 5003 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 5004
c906108c
SS
5005 value_free_to_mark (mark);
5006 return i;
5007}
5008
5760d0ab 5009/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5010
5011static bpstat
5760d0ab 5012bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5013{
5014 bpstat bs;
5015
5016 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5017 bs->next = NULL;
5018 **bs_link_pointer = bs;
5019 *bs_link_pointer = &bs->next;
f431efe5
PA
5020 bs->breakpoint_at = bl->owner;
5021 bs->bp_location_at = bl;
5022 incref_bp_location (bl);
c906108c
SS
5023 /* If the condition is false, etc., don't do the commands. */
5024 bs->commands = NULL;
5025 bs->old_val = NULL;
5026 bs->print_it = print_it_normal;
5027 return bs;
5028}
5029\f
d983da9c
DJ
5030/* The target has stopped with waitstatus WS. Check if any hardware
5031 watchpoints have triggered, according to the target. */
5032
5033int
5034watchpoints_triggered (struct target_waitstatus *ws)
5035{
d92524f1 5036 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5037 CORE_ADDR addr;
5038 struct breakpoint *b;
5039
5040 if (!stopped_by_watchpoint)
5041 {
5042 /* We were not stopped by a watchpoint. Mark all watchpoints
5043 as not triggered. */
5044 ALL_BREAKPOINTS (b)
cc60f2e3 5045 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5046 {
5047 struct watchpoint *w = (struct watchpoint *) b;
5048
5049 w->watchpoint_triggered = watch_triggered_no;
5050 }
d983da9c
DJ
5051
5052 return 0;
5053 }
5054
5055 if (!target_stopped_data_address (&current_target, &addr))
5056 {
5057 /* We were stopped by a watchpoint, but we don't know where.
5058 Mark all watchpoints as unknown. */
5059 ALL_BREAKPOINTS (b)
cc60f2e3 5060 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5061 {
5062 struct watchpoint *w = (struct watchpoint *) b;
5063
5064 w->watchpoint_triggered = watch_triggered_unknown;
5065 }
d983da9c 5066
3c4797ba 5067 return 1;
d983da9c
DJ
5068 }
5069
5070 /* The target could report the data address. Mark watchpoints
5071 affected by this data address as triggered, and all others as not
5072 triggered. */
5073
5074 ALL_BREAKPOINTS (b)
cc60f2e3 5075 if (is_hardware_watchpoint (b))
d983da9c 5076 {
3a5c3e22 5077 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5078 struct bp_location *loc;
d983da9c 5079
3a5c3e22 5080 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5081 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5082 {
3a5c3e22 5083 if (is_masked_watchpoint (b))
9c06b0b4 5084 {
3a5c3e22
PA
5085 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5086 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5087
5088 if (newaddr == start)
5089 {
3a5c3e22 5090 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5091 break;
5092 }
5093 }
5094 /* Exact match not required. Within range is sufficient. */
5095 else if (target_watchpoint_addr_within_range (&current_target,
5096 addr, loc->address,
5097 loc->length))
5098 {
3a5c3e22 5099 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5100 break;
5101 }
5102 }
d983da9c
DJ
5103 }
5104
5105 return 1;
5106}
5107
c906108c
SS
5108/* Possible return values for watchpoint_check (this can't be an enum
5109 because of check_errors). */
5110/* The watchpoint has been deleted. */
5111#define WP_DELETED 1
5112/* The value has changed. */
5113#define WP_VALUE_CHANGED 2
5114/* The value has not changed. */
5115#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5116/* Ignore this watchpoint, no matter if the value changed or not. */
5117#define WP_IGNORE 4
c906108c
SS
5118
5119#define BP_TEMPFLAG 1
5120#define BP_HARDWAREFLAG 2
5121
4a64f543
MS
5122/* Evaluate watchpoint condition expression and check if its value
5123 changed.
553e4c11
JB
5124
5125 P should be a pointer to struct bpstat, but is defined as a void *
5126 in order for this function to be usable with catch_errors. */
c906108c
SS
5127
5128static int
4efb68b1 5129watchpoint_check (void *p)
c906108c
SS
5130{
5131 bpstat bs = (bpstat) p;
3a5c3e22 5132 struct watchpoint *b;
c906108c
SS
5133 struct frame_info *fr;
5134 int within_current_scope;
5135
f431efe5 5136 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5137 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5138 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5139
f6bc2008
PA
5140 /* If this is a local watchpoint, we only want to check if the
5141 watchpoint frame is in scope if the current thread is the thread
5142 that was used to create the watchpoint. */
5143 if (!watchpoint_in_thread_scope (b))
60e1c644 5144 return WP_IGNORE;
f6bc2008 5145
c906108c
SS
5146 if (b->exp_valid_block == NULL)
5147 within_current_scope = 1;
5148 else
5149 {
edb3359d
DJ
5150 struct frame_info *frame = get_current_frame ();
5151 struct gdbarch *frame_arch = get_frame_arch (frame);
5152 CORE_ADDR frame_pc = get_frame_pc (frame);
5153
c9cf6e20 5154 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5155 still in the function but the stack frame has already been
5156 invalidated. Since we can't rely on the values of local
5157 variables after the stack has been destroyed, we are treating
5158 the watchpoint in that state as `not changed' without further
5159 checking. Don't mark watchpoints as changed if the current
5160 frame is in an epilogue - even if they are in some other
5161 frame, our view of the stack is likely to be wrong and
5162 frame_find_by_id could error out. */
c9cf6e20 5163 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5164 return WP_IGNORE;
a0f49112 5165
101dcfbe 5166 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5167 within_current_scope = (fr != NULL);
69fbadd5
DJ
5168
5169 /* If we've gotten confused in the unwinder, we might have
5170 returned a frame that can't describe this variable. */
edb3359d
DJ
5171 if (within_current_scope)
5172 {
5173 struct symbol *function;
5174
5175 function = get_frame_function (fr);
5176 if (function == NULL
5177 || !contained_in (b->exp_valid_block,
5178 SYMBOL_BLOCK_VALUE (function)))
5179 within_current_scope = 0;
5180 }
69fbadd5 5181
edb3359d 5182 if (within_current_scope)
c906108c
SS
5183 /* If we end up stopping, the current frame will get selected
5184 in normal_stop. So this call to select_frame won't affect
5185 the user. */
0f7d239c 5186 select_frame (fr);
c906108c 5187 }
c5aa993b 5188
c906108c
SS
5189 if (within_current_scope)
5190 {
4a64f543
MS
5191 /* We use value_{,free_to_}mark because it could be a *long*
5192 time before we return to the command level and call
5193 free_all_values. We can't call free_all_values because we
5194 might be in the middle of evaluating a function call. */
c906108c 5195
0cf6dd15 5196 int pc = 0;
9c06b0b4 5197 struct value *mark;
fa4727a6
DJ
5198 struct value *new_val;
5199
c1fc2657 5200 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5201 /* Since we don't know the exact trigger address (from
5202 stopped_data_address), just tell the user we've triggered
5203 a mask watchpoint. */
5204 return WP_VALUE_CHANGED;
5205
5206 mark = value_mark ();
4d01a485 5207 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5208
bb9d5f81
PP
5209 if (b->val_bitsize != 0)
5210 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5211
4a64f543
MS
5212 /* We use value_equal_contents instead of value_equal because
5213 the latter coerces an array to a pointer, thus comparing just
5214 the address of the array instead of its contents. This is
5215 not what we want. */
fa4727a6 5216 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5217 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5218 {
fa4727a6
DJ
5219 if (new_val != NULL)
5220 {
5221 release_value (new_val);
5222 value_free_to_mark (mark);
5223 }
c906108c
SS
5224 bs->old_val = b->val;
5225 b->val = new_val;
fa4727a6 5226 b->val_valid = 1;
c906108c
SS
5227 return WP_VALUE_CHANGED;
5228 }
5229 else
5230 {
60e1c644 5231 /* Nothing changed. */
c906108c 5232 value_free_to_mark (mark);
c906108c
SS
5233 return WP_VALUE_NOT_CHANGED;
5234 }
5235 }
5236 else
5237 {
5238 /* This seems like the only logical thing to do because
c5aa993b
JM
5239 if we temporarily ignored the watchpoint, then when
5240 we reenter the block in which it is valid it contains
5241 garbage (in the case of a function, it may have two
5242 garbage values, one before and one after the prologue).
5243 So we can't even detect the first assignment to it and
5244 watch after that (since the garbage may or may not equal
5245 the first value assigned). */
348d480f
PA
5246 /* We print all the stop information in
5247 breakpoint_ops->print_it, but in this case, by the time we
5248 call breakpoint_ops->print_it this bp will be deleted
5249 already. So we have no choice but print the information
5250 here. */
468afe6c 5251
0e454242 5252 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5253 {
5254 struct ui_out *uiout = current_uiout;
5255
112e8700
SM
5256 if (uiout->is_mi_like_p ())
5257 uiout->field_string
5258 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5259 uiout->text ("\nWatchpoint ");
c1fc2657 5260 uiout->field_int ("wpnum", b->number);
112e8700 5261 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5262 "which its expression is valid.\n");
5263 }
4ce44c66 5264
cdac0397 5265 /* Make sure the watchpoint's commands aren't executed. */
c1fc2657 5266 decref_counted_command_line (&b->commands);
d0fb5eae 5267 watchpoint_del_at_next_stop (b);
c906108c
SS
5268
5269 return WP_DELETED;
5270 }
5271}
5272
18a18393 5273/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5274 breakpoint location BL. This function does not check if we should
5275 stop, only if BL explains the stop. */
5276
18a18393 5277static int
6c95b8df 5278bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5279 struct address_space *aspace, CORE_ADDR bp_addr,
5280 const struct target_waitstatus *ws)
18a18393
VP
5281{
5282 struct breakpoint *b = bl->owner;
5283
348d480f 5284 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5285 gdb_assert (b != NULL);
5286
09ac7c10 5287 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5288}
5289
3a5c3e22
PA
5290/* Determine if the watched values have actually changed, and we
5291 should stop. If not, set BS->stop to 0. */
5292
18a18393
VP
5293static void
5294bpstat_check_watchpoint (bpstat bs)
5295{
2bdf28a0 5296 const struct bp_location *bl;
3a5c3e22 5297 struct watchpoint *b;
2bdf28a0
JK
5298
5299 /* BS is built for existing struct breakpoint. */
f431efe5 5300 bl = bs->bp_location_at;
2bdf28a0 5301 gdb_assert (bl != NULL);
3a5c3e22 5302 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5303 gdb_assert (b != NULL);
18a18393 5304
18a18393 5305 {
18a18393
VP
5306 int must_check_value = 0;
5307
c1fc2657 5308 if (b->type == bp_watchpoint)
18a18393
VP
5309 /* For a software watchpoint, we must always check the
5310 watched value. */
5311 must_check_value = 1;
5312 else if (b->watchpoint_triggered == watch_triggered_yes)
5313 /* We have a hardware watchpoint (read, write, or access)
5314 and the target earlier reported an address watched by
5315 this watchpoint. */
5316 must_check_value = 1;
5317 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5318 && b->type == bp_hardware_watchpoint)
18a18393
VP
5319 /* We were stopped by a hardware watchpoint, but the target could
5320 not report the data address. We must check the watchpoint's
5321 value. Access and read watchpoints are out of luck; without
5322 a data address, we can't figure it out. */
5323 must_check_value = 1;
3a5c3e22 5324
18a18393
VP
5325 if (must_check_value)
5326 {
3e43a32a
MS
5327 char *message
5328 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
c1fc2657 5329 b->number);
18a18393
VP
5330 struct cleanup *cleanups = make_cleanup (xfree, message);
5331 int e = catch_errors (watchpoint_check, bs, message,
5332 RETURN_MASK_ALL);
5333 do_cleanups (cleanups);
5334 switch (e)
5335 {
5336 case WP_DELETED:
5337 /* We've already printed what needs to be printed. */
5338 bs->print_it = print_it_done;
5339 /* Stop. */
5340 break;
60e1c644
PA
5341 case WP_IGNORE:
5342 bs->print_it = print_it_noop;
5343 bs->stop = 0;
5344 break;
18a18393 5345 case WP_VALUE_CHANGED:
c1fc2657 5346 if (b->type == bp_read_watchpoint)
18a18393 5347 {
85d721b8
PA
5348 /* There are two cases to consider here:
5349
4a64f543 5350 1. We're watching the triggered memory for reads.
85d721b8
PA
5351 In that case, trust the target, and always report
5352 the watchpoint hit to the user. Even though
5353 reads don't cause value changes, the value may
5354 have changed since the last time it was read, and
5355 since we're not trapping writes, we will not see
5356 those, and as such we should ignore our notion of
5357 old value.
5358
4a64f543 5359 2. We're watching the triggered memory for both
85d721b8
PA
5360 reads and writes. There are two ways this may
5361 happen:
5362
4a64f543 5363 2.1. This is a target that can't break on data
85d721b8
PA
5364 reads only, but can break on accesses (reads or
5365 writes), such as e.g., x86. We detect this case
5366 at the time we try to insert read watchpoints.
5367
4a64f543 5368 2.2. Otherwise, the target supports read
85d721b8
PA
5369 watchpoints, but, the user set an access or write
5370 watchpoint watching the same memory as this read
5371 watchpoint.
5372
5373 If we're watching memory writes as well as reads,
5374 ignore watchpoint hits when we find that the
5375 value hasn't changed, as reads don't cause
5376 changes. This still gives false positives when
5377 the program writes the same value to memory as
5378 what there was already in memory (we will confuse
5379 it for a read), but it's much better than
5380 nothing. */
5381
5382 int other_write_watchpoint = 0;
5383
5384 if (bl->watchpoint_type == hw_read)
5385 {
5386 struct breakpoint *other_b;
5387
5388 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5389 if (other_b->type == bp_hardware_watchpoint
5390 || other_b->type == bp_access_watchpoint)
85d721b8 5391 {
3a5c3e22
PA
5392 struct watchpoint *other_w =
5393 (struct watchpoint *) other_b;
5394
5395 if (other_w->watchpoint_triggered
5396 == watch_triggered_yes)
5397 {
5398 other_write_watchpoint = 1;
5399 break;
5400 }
85d721b8
PA
5401 }
5402 }
5403
5404 if (other_write_watchpoint
5405 || bl->watchpoint_type == hw_access)
5406 {
5407 /* We're watching the same memory for writes,
5408 and the value changed since the last time we
5409 updated it, so this trap must be for a write.
5410 Ignore it. */
5411 bs->print_it = print_it_noop;
5412 bs->stop = 0;
5413 }
18a18393
VP
5414 }
5415 break;
5416 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5417 if (b->type == bp_hardware_watchpoint
5418 || b->type == bp_watchpoint)
18a18393
VP
5419 {
5420 /* Don't stop: write watchpoints shouldn't fire if
5421 the value hasn't changed. */
5422 bs->print_it = print_it_noop;
5423 bs->stop = 0;
5424 }
5425 /* Stop. */
5426 break;
5427 default:
5428 /* Can't happen. */
5429 case 0:
5430 /* Error from catch_errors. */
468afe6c 5431 {
0e454242 5432 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5433 {
5434 printf_filtered (_("Watchpoint %d deleted.\n"),
c1fc2657 5435 b->number);
468afe6c
PA
5436 }
5437 watchpoint_del_at_next_stop (b);
5438 /* We've already printed what needs to be printed. */
5439 bs->print_it = print_it_done;
5440 }
18a18393
VP
5441 break;
5442 }
5443 }
5444 else /* must_check_value == 0 */
5445 {
5446 /* This is a case where some watchpoint(s) triggered, but
5447 not at the address of this watchpoint, or else no
5448 watchpoint triggered after all. So don't print
5449 anything for this watchpoint. */
5450 bs->print_it = print_it_noop;
5451 bs->stop = 0;
5452 }
5453 }
5454}
5455
7d4df6a4
DE
5456/* For breakpoints that are currently marked as telling gdb to stop,
5457 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5458 of breakpoint referred to by BS. If we should not stop for this
5459 breakpoint, set BS->stop to 0. */
f431efe5 5460
18a18393
VP
5461static void
5462bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5463{
2bdf28a0
JK
5464 const struct bp_location *bl;
5465 struct breakpoint *b;
7d4df6a4
DE
5466 int value_is_zero = 0;
5467 struct expression *cond;
5468
5469 gdb_assert (bs->stop);
2bdf28a0
JK
5470
5471 /* BS is built for existing struct breakpoint. */
f431efe5 5472 bl = bs->bp_location_at;
2bdf28a0 5473 gdb_assert (bl != NULL);
f431efe5 5474 b = bs->breakpoint_at;
2bdf28a0 5475 gdb_assert (b != NULL);
18a18393 5476
b775012e
LM
5477 /* Even if the target evaluated the condition on its end and notified GDB, we
5478 need to do so again since GDB does not know if we stopped due to a
5479 breakpoint or a single step breakpoint. */
5480
18a18393 5481 if (frame_id_p (b->frame_id)
edb3359d 5482 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5483 {
7d4df6a4
DE
5484 bs->stop = 0;
5485 return;
5486 }
60e1c644 5487
12ab52e9
PA
5488 /* If this is a thread/task-specific breakpoint, don't waste cpu
5489 evaluating the condition if this isn't the specified
5490 thread/task. */
5d5658a1 5491 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5492 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5493
6c1b0f7b
DE
5494 {
5495 bs->stop = 0;
5496 return;
5497 }
5498
6dddc817
DE
5499 /* Evaluate extension language breakpoints that have a "stop" method
5500 implemented. */
5501 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5502
7d4df6a4
DE
5503 if (is_watchpoint (b))
5504 {
5505 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5506
4d01a485 5507 cond = w->cond_exp.get ();
7d4df6a4
DE
5508 }
5509 else
4d01a485 5510 cond = bl->cond.get ();
60e1c644 5511
7d4df6a4
DE
5512 if (cond && b->disposition != disp_del_at_next_stop)
5513 {
5514 int within_current_scope = 1;
5515 struct watchpoint * w;
60e1c644 5516
7d4df6a4
DE
5517 /* We use value_mark and value_free_to_mark because it could
5518 be a long time before we return to the command level and
5519 call free_all_values. We can't call free_all_values
5520 because we might be in the middle of evaluating a
5521 function call. */
5522 struct value *mark = value_mark ();
5523
5524 if (is_watchpoint (b))
5525 w = (struct watchpoint *) b;
5526 else
5527 w = NULL;
5528
5529 /* Need to select the frame, with all that implies so that
5530 the conditions will have the right context. Because we
5531 use the frame, we will not see an inlined function's
5532 variables when we arrive at a breakpoint at the start
5533 of the inlined function; the current frame will be the
5534 call site. */
5535 if (w == NULL || w->cond_exp_valid_block == NULL)
5536 select_frame (get_current_frame ());
5537 else
18a18393 5538 {
7d4df6a4
DE
5539 struct frame_info *frame;
5540
5541 /* For local watchpoint expressions, which particular
5542 instance of a local is being watched matters, so we
5543 keep track of the frame to evaluate the expression
5544 in. To evaluate the condition however, it doesn't
5545 really matter which instantiation of the function
5546 where the condition makes sense triggers the
5547 watchpoint. This allows an expression like "watch
5548 global if q > 10" set in `func', catch writes to
5549 global on all threads that call `func', or catch
5550 writes on all recursive calls of `func' by a single
5551 thread. We simply always evaluate the condition in
5552 the innermost frame that's executing where it makes
5553 sense to evaluate the condition. It seems
5554 intuitive. */
5555 frame = block_innermost_frame (w->cond_exp_valid_block);
5556 if (frame != NULL)
5557 select_frame (frame);
5558 else
5559 within_current_scope = 0;
18a18393 5560 }
7d4df6a4
DE
5561 if (within_current_scope)
5562 value_is_zero
5563 = catch_errors (breakpoint_cond_eval, cond,
5564 "Error in testing breakpoint condition:\n",
5565 RETURN_MASK_ALL);
5566 else
18a18393 5567 {
7d4df6a4
DE
5568 warning (_("Watchpoint condition cannot be tested "
5569 "in the current scope"));
5570 /* If we failed to set the right context for this
5571 watchpoint, unconditionally report it. */
5572 value_is_zero = 0;
18a18393 5573 }
7d4df6a4
DE
5574 /* FIXME-someday, should give breakpoint #. */
5575 value_free_to_mark (mark);
18a18393 5576 }
7d4df6a4
DE
5577
5578 if (cond && value_is_zero)
5579 {
5580 bs->stop = 0;
5581 }
7d4df6a4
DE
5582 else if (b->ignore_count > 0)
5583 {
5584 b->ignore_count--;
5585 bs->stop = 0;
5586 /* Increase the hit count even though we don't stop. */
5587 ++(b->hit_count);
5588 observer_notify_breakpoint_modified (b);
5589 }
18a18393
VP
5590}
5591
1cf4d951
PA
5592/* Returns true if we need to track moribund locations of LOC's type
5593 on the current target. */
5594
5595static int
5596need_moribund_for_location_type (struct bp_location *loc)
5597{
5598 return ((loc->loc_type == bp_loc_software_breakpoint
5599 && !target_supports_stopped_by_sw_breakpoint ())
5600 || (loc->loc_type == bp_loc_hardware_breakpoint
5601 && !target_supports_stopped_by_hw_breakpoint ()));
5602}
5603
18a18393 5604
9709f61c 5605/* Get a bpstat associated with having just stopped at address
d983da9c 5606 BP_ADDR in thread PTID.
c906108c 5607
d983da9c 5608 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5609 don't understand this stop. Result is a chain of bpstat's such
5610 that:
c906108c 5611
c5aa993b 5612 if we don't understand the stop, the result is a null pointer.
c906108c 5613
c5aa993b 5614 if we understand why we stopped, the result is not null.
c906108c 5615
c5aa993b
JM
5616 Each element of the chain refers to a particular breakpoint or
5617 watchpoint at which we have stopped. (We may have stopped for
5618 several reasons concurrently.)
c906108c 5619
c5aa993b
JM
5620 Each element of the chain has valid next, breakpoint_at,
5621 commands, FIXME??? fields. */
c906108c
SS
5622
5623bpstat
6c95b8df 5624bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5625 CORE_ADDR bp_addr, ptid_t ptid,
5626 const struct target_waitstatus *ws)
c906108c 5627{
0d381245 5628 struct breakpoint *b = NULL;
afe38095 5629 struct bp_location *bl;
20874c92 5630 struct bp_location *loc;
5760d0ab
JK
5631 /* First item of allocated bpstat's. */
5632 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5633 /* Pointer to the last thing in the chain currently. */
5760d0ab 5634 bpstat bs;
20874c92 5635 int ix;
429374b8 5636 int need_remove_insert;
f431efe5 5637 int removed_any;
c906108c 5638
f431efe5
PA
5639 /* First, build the bpstat chain with locations that explain a
5640 target stop, while being careful to not set the target running,
5641 as that may invalidate locations (in particular watchpoint
5642 locations are recreated). Resuming will happen here with
5643 breakpoint conditions or watchpoint expressions that include
5644 inferior function calls. */
c5aa993b 5645
429374b8
JK
5646 ALL_BREAKPOINTS (b)
5647 {
1a853c52 5648 if (!breakpoint_enabled (b))
429374b8 5649 continue;
a5606eee 5650
429374b8
JK
5651 for (bl = b->loc; bl != NULL; bl = bl->next)
5652 {
4a64f543
MS
5653 /* For hardware watchpoints, we look only at the first
5654 location. The watchpoint_check function will work on the
5655 entire expression, not the individual locations. For
5656 read watchpoints, the watchpoints_triggered function has
5657 checked all locations already. */
429374b8
JK
5658 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5659 break;
18a18393 5660
f6592439 5661 if (!bl->enabled || bl->shlib_disabled)
429374b8 5662 continue;
c5aa993b 5663
09ac7c10 5664 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5665 continue;
c5aa993b 5666
4a64f543
MS
5667 /* Come here if it's a watchpoint, or if the break address
5668 matches. */
c5aa993b 5669
4a64f543
MS
5670 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5671 explain stop. */
c5aa993b 5672
f431efe5
PA
5673 /* Assume we stop. Should we find a watchpoint that is not
5674 actually triggered, or if the condition of the breakpoint
5675 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5676 bs->stop = 1;
5677 bs->print = 1;
d983da9c 5678
f431efe5
PA
5679 /* If this is a scope breakpoint, mark the associated
5680 watchpoint as triggered so that we will handle the
5681 out-of-scope event. We'll get to the watchpoint next
5682 iteration. */
d0fb5eae 5683 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5684 {
5685 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5686
5687 w->watchpoint_triggered = watch_triggered_yes;
5688 }
f431efe5
PA
5689 }
5690 }
5691
7c16b83e 5692 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5693 if (!target_supports_stopped_by_sw_breakpoint ()
5694 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5695 {
1cf4d951 5696 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5697 {
1cf4d951
PA
5698 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5699 && need_moribund_for_location_type (loc))
5700 {
5701 bs = bpstat_alloc (loc, &bs_link);
5702 /* For hits of moribund locations, we should just proceed. */
5703 bs->stop = 0;
5704 bs->print = 0;
5705 bs->print_it = print_it_noop;
5706 }
f431efe5
PA
5707 }
5708 }
5709
edcc5120
TT
5710 /* A bit of special processing for shlib breakpoints. We need to
5711 process solib loading here, so that the lists of loaded and
5712 unloaded libraries are correct before we handle "catch load" and
5713 "catch unload". */
5714 for (bs = bs_head; bs != NULL; bs = bs->next)
5715 {
5d268276 5716 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5717 {
5718 handle_solib_event ();
5719 break;
5720 }
5721 }
5722
f431efe5
PA
5723 /* Now go through the locations that caused the target to stop, and
5724 check whether we're interested in reporting this stop to higher
5725 layers, or whether we should resume the target transparently. */
5726
5727 removed_any = 0;
5728
5760d0ab 5729 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5730 {
5731 if (!bs->stop)
5732 continue;
5733
f431efe5 5734 b = bs->breakpoint_at;
348d480f
PA
5735 b->ops->check_status (bs);
5736 if (bs->stop)
28010a5d 5737 {
348d480f 5738 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5739
429374b8
JK
5740 if (bs->stop)
5741 {
5742 ++(b->hit_count);
8d3788bd 5743 observer_notify_breakpoint_modified (b);
c906108c 5744
4a64f543 5745 /* We will stop here. */
429374b8
JK
5746 if (b->disposition == disp_disable)
5747 {
816338b5 5748 --(b->enable_count);
1a853c52 5749 if (b->enable_count <= 0)
429374b8 5750 b->enable_state = bp_disabled;
f431efe5 5751 removed_any = 1;
429374b8
JK
5752 }
5753 if (b->silent)
5754 bs->print = 0;
5755 bs->commands = b->commands;
9add0f1b 5756 incref_counted_command_line (bs->commands);
abf85f46
JK
5757 if (command_line_is_silent (bs->commands
5758 ? bs->commands->commands : NULL))
5759 bs->print = 0;
9d6e6e84
HZ
5760
5761 b->ops->after_condition_true (bs);
429374b8
JK
5762 }
5763
348d480f 5764 }
a9b3a50f
PA
5765
5766 /* Print nothing for this entry if we don't stop or don't
5767 print. */
5768 if (!bs->stop || !bs->print)
5769 bs->print_it = print_it_noop;
429374b8 5770 }
876fa593 5771
d983da9c
DJ
5772 /* If we aren't stopping, the value of some hardware watchpoint may
5773 not have changed, but the intermediate memory locations we are
5774 watching may have. Don't bother if we're stopping; this will get
5775 done later. */
d832cb68 5776 need_remove_insert = 0;
5760d0ab
JK
5777 if (! bpstat_causes_stop (bs_head))
5778 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5779 if (!bs->stop
f431efe5
PA
5780 && bs->breakpoint_at
5781 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5782 {
3a5c3e22
PA
5783 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5784
5785 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5786 need_remove_insert = 1;
d983da9c
DJ
5787 }
5788
d832cb68 5789 if (need_remove_insert)
44702360 5790 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5791 else if (removed_any)
44702360 5792 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5793
5760d0ab 5794 return bs_head;
c906108c 5795}
628fe4e4
JK
5796
5797static void
5798handle_jit_event (void)
5799{
5800 struct frame_info *frame;
5801 struct gdbarch *gdbarch;
5802
243a9253
PA
5803 if (debug_infrun)
5804 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5805
628fe4e4
JK
5806 /* Switch terminal for any messages produced by
5807 breakpoint_re_set. */
5808 target_terminal_ours_for_output ();
5809
5810 frame = get_current_frame ();
5811 gdbarch = get_frame_arch (frame);
5812
5813 jit_event_handler (gdbarch);
5814
5815 target_terminal_inferior ();
5816}
5817
5818/* Prepare WHAT final decision for infrun. */
5819
5820/* Decide what infrun needs to do with this bpstat. */
5821
c906108c 5822struct bpstat_what
0e30163f 5823bpstat_what (bpstat bs_head)
c906108c 5824{
c906108c 5825 struct bpstat_what retval;
0e30163f 5826 bpstat bs;
c906108c 5827
628fe4e4 5828 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5829 retval.call_dummy = STOP_NONE;
186c406b 5830 retval.is_longjmp = 0;
628fe4e4 5831
0e30163f 5832 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5833 {
628fe4e4
JK
5834 /* Extract this BS's action. After processing each BS, we check
5835 if its action overrides all we've seem so far. */
5836 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5837 enum bptype bptype;
5838
c906108c 5839 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5840 {
5841 /* I suspect this can happen if it was a momentary
5842 breakpoint which has since been deleted. */
5843 bptype = bp_none;
5844 }
20874c92 5845 else
f431efe5 5846 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5847
5848 switch (bptype)
c906108c
SS
5849 {
5850 case bp_none:
628fe4e4 5851 break;
c906108c
SS
5852 case bp_breakpoint:
5853 case bp_hardware_breakpoint:
7c16b83e 5854 case bp_single_step:
c906108c
SS
5855 case bp_until:
5856 case bp_finish:
a9b3a50f 5857 case bp_shlib_event:
c906108c
SS
5858 if (bs->stop)
5859 {
5860 if (bs->print)
628fe4e4 5861 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5862 else
628fe4e4 5863 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5864 }
5865 else
628fe4e4 5866 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5867 break;
5868 case bp_watchpoint:
5869 case bp_hardware_watchpoint:
5870 case bp_read_watchpoint:
5871 case bp_access_watchpoint:
5872 if (bs->stop)
5873 {
5874 if (bs->print)
628fe4e4 5875 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5876 else
628fe4e4 5877 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5878 }
5879 else
628fe4e4
JK
5880 {
5881 /* There was a watchpoint, but we're not stopping.
5882 This requires no further action. */
5883 }
c906108c
SS
5884 break;
5885 case bp_longjmp:
e2e4d78b 5886 case bp_longjmp_call_dummy:
186c406b 5887 case bp_exception:
0a39bb32
PA
5888 if (bs->stop)
5889 {
5890 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5891 retval.is_longjmp = bptype != bp_exception;
5892 }
5893 else
5894 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5895 break;
5896 case bp_longjmp_resume:
186c406b 5897 case bp_exception_resume:
0a39bb32
PA
5898 if (bs->stop)
5899 {
5900 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5901 retval.is_longjmp = bptype == bp_longjmp_resume;
5902 }
5903 else
5904 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5905 break;
5906 case bp_step_resume:
5907 if (bs->stop)
628fe4e4
JK
5908 this_action = BPSTAT_WHAT_STEP_RESUME;
5909 else
c906108c 5910 {
628fe4e4
JK
5911 /* It is for the wrong frame. */
5912 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5913 }
c906108c 5914 break;
2c03e5be
PA
5915 case bp_hp_step_resume:
5916 if (bs->stop)
5917 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5918 else
5919 {
5920 /* It is for the wrong frame. */
5921 this_action = BPSTAT_WHAT_SINGLE;
5922 }
5923 break;
c906108c 5924 case bp_watchpoint_scope:
c4093a6a 5925 case bp_thread_event:
1900040c 5926 case bp_overlay_event:
0fd8e87f 5927 case bp_longjmp_master:
aa7d318d 5928 case bp_std_terminate_master:
186c406b 5929 case bp_exception_master:
628fe4e4 5930 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5931 break;
ce78b96d 5932 case bp_catchpoint:
c5aa993b
JM
5933 if (bs->stop)
5934 {
5935 if (bs->print)
628fe4e4 5936 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5937 else
628fe4e4 5938 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5939 }
5940 else
628fe4e4
JK
5941 {
5942 /* There was a catchpoint, but we're not stopping.
5943 This requires no further action. */
5944 }
5945 break;
628fe4e4 5946 case bp_jit_event:
628fe4e4 5947 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5948 break;
c906108c 5949 case bp_call_dummy:
53a5351d
JM
5950 /* Make sure the action is stop (silent or noisy),
5951 so infrun.c pops the dummy frame. */
aa7d318d 5952 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5953 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5954 break;
5955 case bp_std_terminate:
5956 /* Make sure the action is stop (silent or noisy),
5957 so infrun.c pops the dummy frame. */
aa7d318d 5958 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5959 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5960 break;
1042e4c0 5961 case bp_tracepoint:
7a697b8d 5962 case bp_fast_tracepoint:
0fb4aa4b 5963 case bp_static_tracepoint:
1042e4c0
SS
5964 /* Tracepoint hits should not be reported back to GDB, and
5965 if one got through somehow, it should have been filtered
5966 out already. */
5967 internal_error (__FILE__, __LINE__,
7a697b8d 5968 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5969 break;
5970 case bp_gnu_ifunc_resolver:
5971 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5972 this_action = BPSTAT_WHAT_SINGLE;
5973 break;
5974 case bp_gnu_ifunc_resolver_return:
5975 /* The breakpoint will be removed, execution will restart from the
5976 PC of the former breakpoint. */
5977 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5978 break;
e7e0cddf
SS
5979
5980 case bp_dprintf:
a11cfd87
HZ
5981 if (bs->stop)
5982 this_action = BPSTAT_WHAT_STOP_SILENT;
5983 else
5984 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5985 break;
5986
628fe4e4
JK
5987 default:
5988 internal_error (__FILE__, __LINE__,
5989 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5990 }
628fe4e4 5991
325fac50 5992 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5993 }
628fe4e4 5994
243a9253
PA
5995 return retval;
5996}
628fe4e4 5997
243a9253
PA
5998void
5999bpstat_run_callbacks (bpstat bs_head)
6000{
6001 bpstat bs;
628fe4e4 6002
0e30163f
JK
6003 for (bs = bs_head; bs != NULL; bs = bs->next)
6004 {
6005 struct breakpoint *b = bs->breakpoint_at;
6006
6007 if (b == NULL)
6008 continue;
6009 switch (b->type)
6010 {
243a9253
PA
6011 case bp_jit_event:
6012 handle_jit_event ();
6013 break;
0e30163f
JK
6014 case bp_gnu_ifunc_resolver:
6015 gnu_ifunc_resolver_stop (b);
6016 break;
6017 case bp_gnu_ifunc_resolver_return:
6018 gnu_ifunc_resolver_return_stop (b);
6019 break;
6020 }
6021 }
c906108c
SS
6022}
6023
6024/* Nonzero if we should step constantly (e.g. watchpoints on machines
6025 without hardware support). This isn't related to a specific bpstat,
6026 just to things like whether watchpoints are set. */
6027
c5aa993b 6028int
fba45db2 6029bpstat_should_step (void)
c906108c
SS
6030{
6031 struct breakpoint *b;
cc59ec59 6032
c906108c 6033 ALL_BREAKPOINTS (b)
717a8278 6034 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6035 return 1;
c906108c
SS
6036 return 0;
6037}
6038
67822962
PA
6039int
6040bpstat_causes_stop (bpstat bs)
6041{
6042 for (; bs != NULL; bs = bs->next)
6043 if (bs->stop)
6044 return 1;
6045
6046 return 0;
6047}
6048
c906108c 6049\f
c5aa993b 6050
170b53b2
UW
6051/* Compute a string of spaces suitable to indent the next line
6052 so it starts at the position corresponding to the table column
6053 named COL_NAME in the currently active table of UIOUT. */
6054
6055static char *
6056wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6057{
6058 static char wrap_indent[80];
6059 int i, total_width, width, align;
c5209615 6060 const char *text;
170b53b2
UW
6061
6062 total_width = 0;
112e8700 6063 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6064 {
6065 if (strcmp (text, col_name) == 0)
6066 {
6067 gdb_assert (total_width < sizeof wrap_indent);
6068 memset (wrap_indent, ' ', total_width);
6069 wrap_indent[total_width] = 0;
6070
6071 return wrap_indent;
6072 }
6073
6074 total_width += width + 1;
6075 }
6076
6077 return NULL;
6078}
6079
b775012e
LM
6080/* Determine if the locations of this breakpoint will have their conditions
6081 evaluated by the target, host or a mix of both. Returns the following:
6082
6083 "host": Host evals condition.
6084 "host or target": Host or Target evals condition.
6085 "target": Target evals condition.
6086*/
6087
6088static const char *
6089bp_condition_evaluator (struct breakpoint *b)
6090{
6091 struct bp_location *bl;
6092 char host_evals = 0;
6093 char target_evals = 0;
6094
6095 if (!b)
6096 return NULL;
6097
6098 if (!is_breakpoint (b))
6099 return NULL;
6100
6101 if (gdb_evaluates_breakpoint_condition_p ()
6102 || !target_supports_evaluation_of_breakpoint_conditions ())
6103 return condition_evaluation_host;
6104
6105 for (bl = b->loc; bl; bl = bl->next)
6106 {
6107 if (bl->cond_bytecode)
6108 target_evals++;
6109 else
6110 host_evals++;
6111 }
6112
6113 if (host_evals && target_evals)
6114 return condition_evaluation_both;
6115 else if (target_evals)
6116 return condition_evaluation_target;
6117 else
6118 return condition_evaluation_host;
6119}
6120
6121/* Determine the breakpoint location's condition evaluator. This is
6122 similar to bp_condition_evaluator, but for locations. */
6123
6124static const char *
6125bp_location_condition_evaluator (struct bp_location *bl)
6126{
6127 if (bl && !is_breakpoint (bl->owner))
6128 return NULL;
6129
6130 if (gdb_evaluates_breakpoint_condition_p ()
6131 || !target_supports_evaluation_of_breakpoint_conditions ())
6132 return condition_evaluation_host;
6133
6134 if (bl && bl->cond_bytecode)
6135 return condition_evaluation_target;
6136 else
6137 return condition_evaluation_host;
6138}
6139
859825b8
JK
6140/* Print the LOC location out of the list of B->LOC locations. */
6141
170b53b2
UW
6142static void
6143print_breakpoint_location (struct breakpoint *b,
6144 struct bp_location *loc)
0d381245 6145{
79a45e25 6146 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6147
6148 scoped_restore_current_program_space restore_pspace;
6c95b8df 6149
859825b8
JK
6150 if (loc != NULL && loc->shlib_disabled)
6151 loc = NULL;
6152
6c95b8df
PA
6153 if (loc != NULL)
6154 set_current_program_space (loc->pspace);
6155
56435ebe 6156 if (b->display_canonical)
d28cd78a 6157 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6158 else if (loc && loc->symtab)
0d381245
VP
6159 {
6160 struct symbol *sym
6161 = find_pc_sect_function (loc->address, loc->section);
6162 if (sym)
6163 {
112e8700
SM
6164 uiout->text ("in ");
6165 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6166 uiout->text (" ");
6167 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6168 uiout->text ("at ");
0d381245 6169 }
112e8700 6170 uiout->field_string ("file",
05cba821 6171 symtab_to_filename_for_display (loc->symtab));
112e8700 6172 uiout->text (":");
05cba821 6173
112e8700
SM
6174 if (uiout->is_mi_like_p ())
6175 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6176
112e8700 6177 uiout->field_int ("line", loc->line_number);
0d381245 6178 }
859825b8 6179 else if (loc)
0d381245 6180 {
d7e74731 6181 string_file stb;
170b53b2 6182
d7e74731 6183 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6184 demangle, "");
112e8700 6185 uiout->field_stream ("at", stb);
0d381245 6186 }
859825b8 6187 else
f00aae0f 6188 {
d28cd78a
TT
6189 uiout->field_string ("pending",
6190 event_location_to_string (b->location.get ()));
f00aae0f
KS
6191 /* If extra_string is available, it could be holding a condition
6192 or dprintf arguments. In either case, make sure it is printed,
6193 too, but only for non-MI streams. */
112e8700 6194 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6195 {
6196 if (b->type == bp_dprintf)
112e8700 6197 uiout->text (",");
f00aae0f 6198 else
112e8700
SM
6199 uiout->text (" ");
6200 uiout->text (b->extra_string);
f00aae0f
KS
6201 }
6202 }
6c95b8df 6203
b775012e
LM
6204 if (loc && is_breakpoint (b)
6205 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6206 && bp_condition_evaluator (b) == condition_evaluation_both)
6207 {
112e8700
SM
6208 uiout->text (" (");
6209 uiout->field_string ("evaluated-by",
b775012e 6210 bp_location_condition_evaluator (loc));
112e8700 6211 uiout->text (")");
b775012e 6212 }
0d381245
VP
6213}
6214
269b11a2
PA
6215static const char *
6216bptype_string (enum bptype type)
c906108c 6217{
c4093a6a
JM
6218 struct ep_type_description
6219 {
6220 enum bptype type;
a121b7c1 6221 const char *description;
c4093a6a
JM
6222 };
6223 static struct ep_type_description bptypes[] =
c906108c 6224 {
c5aa993b
JM
6225 {bp_none, "?deleted?"},
6226 {bp_breakpoint, "breakpoint"},
c906108c 6227 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6228 {bp_single_step, "sw single-step"},
c5aa993b
JM
6229 {bp_until, "until"},
6230 {bp_finish, "finish"},
6231 {bp_watchpoint, "watchpoint"},
c906108c 6232 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6233 {bp_read_watchpoint, "read watchpoint"},
6234 {bp_access_watchpoint, "acc watchpoint"},
6235 {bp_longjmp, "longjmp"},
6236 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6237 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6238 {bp_exception, "exception"},
6239 {bp_exception_resume, "exception resume"},
c5aa993b 6240 {bp_step_resume, "step resume"},
2c03e5be 6241 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6242 {bp_watchpoint_scope, "watchpoint scope"},
6243 {bp_call_dummy, "call dummy"},
aa7d318d 6244 {bp_std_terminate, "std::terminate"},
c5aa993b 6245 {bp_shlib_event, "shlib events"},
c4093a6a 6246 {bp_thread_event, "thread events"},
1900040c 6247 {bp_overlay_event, "overlay events"},
0fd8e87f 6248 {bp_longjmp_master, "longjmp master"},
aa7d318d 6249 {bp_std_terminate_master, "std::terminate master"},
186c406b 6250 {bp_exception_master, "exception master"},
ce78b96d 6251 {bp_catchpoint, "catchpoint"},
1042e4c0 6252 {bp_tracepoint, "tracepoint"},
7a697b8d 6253 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6254 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6255 {bp_dprintf, "dprintf"},
4efc6507 6256 {bp_jit_event, "jit events"},
0e30163f
JK
6257 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6258 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6259 };
269b11a2
PA
6260
6261 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6262 || ((int) type != bptypes[(int) type].type))
6263 internal_error (__FILE__, __LINE__,
6264 _("bptypes table does not describe type #%d."),
6265 (int) type);
6266
6267 return bptypes[(int) type].description;
6268}
6269
998580f1
MK
6270/* For MI, output a field named 'thread-groups' with a list as the value.
6271 For CLI, prefix the list with the string 'inf'. */
6272
6273static void
6274output_thread_groups (struct ui_out *uiout,
6275 const char *field_name,
6276 VEC(int) *inf_num,
6277 int mi_only)
6278{
112e8700 6279 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6280 int inf;
6281 int i;
6282
6283 /* For backward compatibility, don't display inferiors in CLI unless
6284 there are several. Always display them for MI. */
6285 if (!is_mi && mi_only)
6286 return;
6287
10f489e5 6288 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6289
998580f1
MK
6290 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6291 {
6292 if (is_mi)
6293 {
6294 char mi_group[10];
6295
6296 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6297 uiout->field_string (NULL, mi_group);
998580f1
MK
6298 }
6299 else
6300 {
6301 if (i == 0)
112e8700 6302 uiout->text (" inf ");
998580f1 6303 else
112e8700 6304 uiout->text (", ");
998580f1 6305
112e8700 6306 uiout->text (plongest (inf));
998580f1
MK
6307 }
6308 }
998580f1
MK
6309}
6310
269b11a2
PA
6311/* Print B to gdb_stdout. */
6312
6313static void
6314print_one_breakpoint_location (struct breakpoint *b,
6315 struct bp_location *loc,
6316 int loc_number,
6317 struct bp_location **last_loc,
269b11a2
PA
6318 int allflag)
6319{
6320 struct command_line *l;
c2c6d25f 6321 static char bpenables[] = "nynny";
c906108c 6322
79a45e25 6323 struct ui_out *uiout = current_uiout;
0d381245
VP
6324 int header_of_multiple = 0;
6325 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6326 struct value_print_options opts;
6327
6328 get_user_print_options (&opts);
0d381245
VP
6329
6330 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6331 /* See comment in print_one_breakpoint concerning treatment of
6332 breakpoints with single disabled location. */
0d381245
VP
6333 if (loc == NULL
6334 && (b->loc != NULL
6335 && (b->loc->next != NULL || !b->loc->enabled)))
6336 header_of_multiple = 1;
6337 if (loc == NULL)
6338 loc = b->loc;
6339
c4093a6a
JM
6340 annotate_record ();
6341
6342 /* 1 */
6343 annotate_field (0);
0d381245
VP
6344 if (part_of_multiple)
6345 {
6346 char *formatted;
0c6773c1 6347 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6348 uiout->field_string ("number", formatted);
0d381245
VP
6349 xfree (formatted);
6350 }
6351 else
6352 {
112e8700 6353 uiout->field_int ("number", b->number);
0d381245 6354 }
c4093a6a
JM
6355
6356 /* 2 */
6357 annotate_field (1);
0d381245 6358 if (part_of_multiple)
112e8700 6359 uiout->field_skip ("type");
269b11a2 6360 else
112e8700 6361 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6362
6363 /* 3 */
6364 annotate_field (2);
0d381245 6365 if (part_of_multiple)
112e8700 6366 uiout->field_skip ("disp");
0d381245 6367 else
112e8700 6368 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6369
c4093a6a
JM
6370
6371 /* 4 */
6372 annotate_field (3);
0d381245 6373 if (part_of_multiple)
112e8700 6374 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6375 else
112e8700
SM
6376 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6377 uiout->spaces (2);
0d381245 6378
c4093a6a
JM
6379
6380 /* 5 and 6 */
3086aeae 6381 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6382 {
4a64f543
MS
6383 /* Although the print_one can possibly print all locations,
6384 calling it here is not likely to get any nice result. So,
6385 make sure there's just one location. */
0d381245 6386 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6387 b->ops->print_one (b, last_loc);
0d381245 6388 }
3086aeae
DJ
6389 else
6390 switch (b->type)
6391 {
6392 case bp_none:
6393 internal_error (__FILE__, __LINE__,
e2e0b3e5 6394 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6395 break;
c906108c 6396
3086aeae
DJ
6397 case bp_watchpoint:
6398 case bp_hardware_watchpoint:
6399 case bp_read_watchpoint:
6400 case bp_access_watchpoint:
3a5c3e22
PA
6401 {
6402 struct watchpoint *w = (struct watchpoint *) b;
6403
6404 /* Field 4, the address, is omitted (which makes the columns
6405 not line up too nicely with the headers, but the effect
6406 is relatively readable). */
6407 if (opts.addressprint)
112e8700 6408 uiout->field_skip ("addr");
3a5c3e22 6409 annotate_field (5);
112e8700 6410 uiout->field_string ("what", w->exp_string);
3a5c3e22 6411 }
3086aeae
DJ
6412 break;
6413
3086aeae
DJ
6414 case bp_breakpoint:
6415 case bp_hardware_breakpoint:
7c16b83e 6416 case bp_single_step:
3086aeae
DJ
6417 case bp_until:
6418 case bp_finish:
6419 case bp_longjmp:
6420 case bp_longjmp_resume:
e2e4d78b 6421 case bp_longjmp_call_dummy:
186c406b
TT
6422 case bp_exception:
6423 case bp_exception_resume:
3086aeae 6424 case bp_step_resume:
2c03e5be 6425 case bp_hp_step_resume:
3086aeae
DJ
6426 case bp_watchpoint_scope:
6427 case bp_call_dummy:
aa7d318d 6428 case bp_std_terminate:
3086aeae
DJ
6429 case bp_shlib_event:
6430 case bp_thread_event:
6431 case bp_overlay_event:
0fd8e87f 6432 case bp_longjmp_master:
aa7d318d 6433 case bp_std_terminate_master:
186c406b 6434 case bp_exception_master:
1042e4c0 6435 case bp_tracepoint:
7a697b8d 6436 case bp_fast_tracepoint:
0fb4aa4b 6437 case bp_static_tracepoint:
e7e0cddf 6438 case bp_dprintf:
4efc6507 6439 case bp_jit_event:
0e30163f
JK
6440 case bp_gnu_ifunc_resolver:
6441 case bp_gnu_ifunc_resolver_return:
79a45b7d 6442 if (opts.addressprint)
3086aeae
DJ
6443 {
6444 annotate_field (4);
54e52265 6445 if (header_of_multiple)
112e8700 6446 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6447 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6448 uiout->field_string ("addr", "<PENDING>");
0101ce28 6449 else
112e8700 6450 uiout->field_core_addr ("addr",
5af949e3 6451 loc->gdbarch, loc->address);
3086aeae
DJ
6452 }
6453 annotate_field (5);
0d381245 6454 if (!header_of_multiple)
170b53b2 6455 print_breakpoint_location (b, loc);
0d381245 6456 if (b->loc)
a6d9a66e 6457 *last_loc = b->loc;
3086aeae
DJ
6458 break;
6459 }
c906108c 6460
6c95b8df 6461
998580f1 6462 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6463 {
6464 struct inferior *inf;
998580f1
MK
6465 VEC(int) *inf_num = NULL;
6466 int mi_only = 1;
6c95b8df 6467
998580f1 6468 ALL_INFERIORS (inf)
6c95b8df
PA
6469 {
6470 if (inf->pspace == loc->pspace)
998580f1 6471 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6472 }
998580f1
MK
6473
6474 /* For backward compatibility, don't display inferiors in CLI unless
6475 there are several. Always display for MI. */
6476 if (allflag
6477 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6478 && (number_of_program_spaces () > 1
6479 || number_of_inferiors () > 1)
6480 /* LOC is for existing B, it cannot be in
6481 moribund_locations and thus having NULL OWNER. */
6482 && loc->owner->type != bp_catchpoint))
6483 mi_only = 0;
6484 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6485 VEC_free (int, inf_num);
6c95b8df
PA
6486 }
6487
4a306c9a 6488 if (!part_of_multiple)
c4093a6a 6489 {
4a306c9a
JB
6490 if (b->thread != -1)
6491 {
6492 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6493 "stop only in" line a little further down. */
112e8700
SM
6494 uiout->text (" thread ");
6495 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6496 }
6497 else if (b->task != 0)
6498 {
112e8700
SM
6499 uiout->text (" task ");
6500 uiout->field_int ("task", b->task);
4a306c9a 6501 }
c4093a6a 6502 }
f1310107 6503
112e8700 6504 uiout->text ("\n");
f1310107 6505
348d480f 6506 if (!part_of_multiple)
f1310107
TJB
6507 b->ops->print_one_detail (b, uiout);
6508
0d381245 6509 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6510 {
6511 annotate_field (6);
112e8700 6512 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6513 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6514 the frame ID. */
112e8700 6515 uiout->field_core_addr ("frame",
5af949e3 6516 b->gdbarch, b->frame_id.stack_addr);
112e8700 6517 uiout->text ("\n");
c4093a6a
JM
6518 }
6519
28010a5d 6520 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6521 {
6522 annotate_field (7);
d77f58be 6523 if (is_tracepoint (b))
112e8700 6524 uiout->text ("\ttrace only if ");
1042e4c0 6525 else
112e8700
SM
6526 uiout->text ("\tstop only if ");
6527 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6528
6529 /* Print whether the target is doing the breakpoint's condition
6530 evaluation. If GDB is doing the evaluation, don't print anything. */
6531 if (is_breakpoint (b)
6532 && breakpoint_condition_evaluation_mode ()
6533 == condition_evaluation_target)
6534 {
112e8700
SM
6535 uiout->text (" (");
6536 uiout->field_string ("evaluated-by",
b775012e 6537 bp_condition_evaluator (b));
112e8700 6538 uiout->text (" evals)");
b775012e 6539 }
112e8700 6540 uiout->text ("\n");
0101ce28
JJ
6541 }
6542
0d381245 6543 if (!part_of_multiple && b->thread != -1)
c4093a6a 6544 {
4a64f543 6545 /* FIXME should make an annotation for this. */
112e8700
SM
6546 uiout->text ("\tstop only in thread ");
6547 if (uiout->is_mi_like_p ())
6548 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6549 else
6550 {
6551 struct thread_info *thr = find_thread_global_id (b->thread);
6552
112e8700 6553 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6554 }
112e8700 6555 uiout->text ("\n");
c4093a6a
JM
6556 }
6557
556ec64d
YQ
6558 if (!part_of_multiple)
6559 {
6560 if (b->hit_count)
31f56a27
YQ
6561 {
6562 /* FIXME should make an annotation for this. */
6563 if (is_catchpoint (b))
112e8700 6564 uiout->text ("\tcatchpoint");
31f56a27 6565 else if (is_tracepoint (b))
112e8700 6566 uiout->text ("\ttracepoint");
31f56a27 6567 else
112e8700
SM
6568 uiout->text ("\tbreakpoint");
6569 uiout->text (" already hit ");
6570 uiout->field_int ("times", b->hit_count);
31f56a27 6571 if (b->hit_count == 1)
112e8700 6572 uiout->text (" time\n");
31f56a27 6573 else
112e8700 6574 uiout->text (" times\n");
31f56a27 6575 }
556ec64d
YQ
6576 else
6577 {
31f56a27 6578 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6579 if (uiout->is_mi_like_p ())
6580 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6581 }
6582 }
8b93c638 6583
0d381245 6584 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6585 {
6586 annotate_field (8);
112e8700
SM
6587 uiout->text ("\tignore next ");
6588 uiout->field_int ("ignore", b->ignore_count);
6589 uiout->text (" hits\n");
c4093a6a 6590 }
059fb39f 6591
816338b5
SS
6592 /* Note that an enable count of 1 corresponds to "enable once"
6593 behavior, which is reported by the combination of enablement and
6594 disposition, so we don't need to mention it here. */
6595 if (!part_of_multiple && b->enable_count > 1)
6596 {
6597 annotate_field (8);
112e8700 6598 uiout->text ("\tdisable after ");
816338b5
SS
6599 /* Tweak the wording to clarify that ignore and enable counts
6600 are distinct, and have additive effect. */
6601 if (b->ignore_count)
112e8700 6602 uiout->text ("additional ");
816338b5 6603 else
112e8700
SM
6604 uiout->text ("next ");
6605 uiout->field_int ("enable", b->enable_count);
6606 uiout->text (" hits\n");
816338b5
SS
6607 }
6608
f196051f
SS
6609 if (!part_of_multiple && is_tracepoint (b))
6610 {
6611 struct tracepoint *tp = (struct tracepoint *) b;
6612
6613 if (tp->traceframe_usage)
6614 {
112e8700
SM
6615 uiout->text ("\ttrace buffer usage ");
6616 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6617 uiout->text (" bytes\n");
f196051f
SS
6618 }
6619 }
d3ce09f5 6620
9add0f1b 6621 l = b->commands ? b->commands->commands : NULL;
059fb39f 6622 if (!part_of_multiple && l)
c4093a6a
JM
6623 {
6624 annotate_field (9);
2e783024 6625 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6626 print_command_lines (uiout, l, 4);
c4093a6a 6627 }
d24317b4 6628
d9b3f62e 6629 if (is_tracepoint (b))
1042e4c0 6630 {
d9b3f62e
PA
6631 struct tracepoint *t = (struct tracepoint *) b;
6632
6633 if (!part_of_multiple && t->pass_count)
6634 {
6635 annotate_field (10);
112e8700
SM
6636 uiout->text ("\tpass count ");
6637 uiout->field_int ("pass", t->pass_count);
6638 uiout->text (" \n");
d9b3f62e 6639 }
f2a8bc8a
YQ
6640
6641 /* Don't display it when tracepoint or tracepoint location is
6642 pending. */
6643 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6644 {
6645 annotate_field (11);
6646
112e8700
SM
6647 if (uiout->is_mi_like_p ())
6648 uiout->field_string ("installed",
f2a8bc8a
YQ
6649 loc->inserted ? "y" : "n");
6650 else
6651 {
6652 if (loc->inserted)
112e8700 6653 uiout->text ("\t");
f2a8bc8a 6654 else
112e8700
SM
6655 uiout->text ("\tnot ");
6656 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6657 }
6658 }
1042e4c0
SS
6659 }
6660
112e8700 6661 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6662 {
3a5c3e22
PA
6663 if (is_watchpoint (b))
6664 {
6665 struct watchpoint *w = (struct watchpoint *) b;
6666
112e8700 6667 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6668 }
f00aae0f 6669 else if (b->location != NULL
d28cd78a 6670 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6671 uiout->field_string ("original-location",
d28cd78a 6672 event_location_to_string (b->location.get ()));
d24317b4 6673 }
c4093a6a 6674}
c5aa993b 6675
0d381245
VP
6676static void
6677print_one_breakpoint (struct breakpoint *b,
4a64f543 6678 struct bp_location **last_loc,
6c95b8df 6679 int allflag)
0d381245 6680{
79a45e25 6681 struct ui_out *uiout = current_uiout;
8d3788bd 6682
2e783024
TT
6683 {
6684 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6685
2e783024
TT
6686 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6687 }
0d381245
VP
6688
6689 /* If this breakpoint has custom print function,
6690 it's already printed. Otherwise, print individual
6691 locations, if any. */
6692 if (b->ops == NULL || b->ops->print_one == NULL)
6693 {
4a64f543
MS
6694 /* If breakpoint has a single location that is disabled, we
6695 print it as if it had several locations, since otherwise it's
6696 hard to represent "breakpoint enabled, location disabled"
6697 situation.
6698
6699 Note that while hardware watchpoints have several locations
a3be7890 6700 internally, that's not a property exposed to user. */
0d381245 6701 if (b->loc
a5606eee 6702 && !is_hardware_watchpoint (b)
8d3788bd 6703 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6704 {
6705 struct bp_location *loc;
6706 int n = 1;
8d3788bd 6707
0d381245 6708 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6709 {
2e783024 6710 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6711 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6712 }
0d381245
VP
6713 }
6714 }
6715}
6716
a6d9a66e
UW
6717static int
6718breakpoint_address_bits (struct breakpoint *b)
6719{
6720 int print_address_bits = 0;
6721 struct bp_location *loc;
6722
c6d81124
PA
6723 /* Software watchpoints that aren't watching memory don't have an
6724 address to print. */
6725 if (is_no_memory_software_watchpoint (b))
6726 return 0;
6727
a6d9a66e
UW
6728 for (loc = b->loc; loc; loc = loc->next)
6729 {
c7437ca6
PA
6730 int addr_bit;
6731
c7437ca6 6732 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6733 if (addr_bit > print_address_bits)
6734 print_address_bits = addr_bit;
6735 }
6736
6737 return print_address_bits;
6738}
0d381245 6739
c4093a6a
JM
6740struct captured_breakpoint_query_args
6741 {
6742 int bnum;
6743 };
c5aa993b 6744
c4093a6a 6745static int
2b65245e 6746do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6747{
9a3c8263
SM
6748 struct captured_breakpoint_query_args *args
6749 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6750 struct breakpoint *b;
a6d9a66e 6751 struct bp_location *dummy_loc = NULL;
cc59ec59 6752
c4093a6a
JM
6753 ALL_BREAKPOINTS (b)
6754 {
6755 if (args->bnum == b->number)
c5aa993b 6756 {
12c5a436 6757 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6758 return GDB_RC_OK;
c5aa993b 6759 }
c4093a6a
JM
6760 }
6761 return GDB_RC_NONE;
6762}
c5aa993b 6763
c4093a6a 6764enum gdb_rc
4a64f543
MS
6765gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6766 char **error_message)
c4093a6a
JM
6767{
6768 struct captured_breakpoint_query_args args;
cc59ec59 6769
c4093a6a
JM
6770 args.bnum = bnum;
6771 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6772 an error. */
b0b13bb4
DJ
6773 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6774 error_message, RETURN_MASK_ALL) < 0)
6775 return GDB_RC_FAIL;
6776 else
6777 return GDB_RC_OK;
c4093a6a 6778}
c5aa993b 6779
09d682a4
TT
6780/* Return true if this breakpoint was set by the user, false if it is
6781 internal or momentary. */
6782
6783int
6784user_breakpoint_p (struct breakpoint *b)
6785{
46c6471b 6786 return b->number > 0;
09d682a4
TT
6787}
6788
93daf339
TT
6789/* See breakpoint.h. */
6790
6791int
6792pending_breakpoint_p (struct breakpoint *b)
6793{
6794 return b->loc == NULL;
6795}
6796
7f3b0473 6797/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6798 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6799 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6800 FILTER is non-NULL, call it on each breakpoint and only include the
6801 ones for which it returns non-zero. Return the total number of
6802 breakpoints listed. */
c906108c 6803
d77f58be 6804static int
e5a67952 6805breakpoint_1 (char *args, int allflag,
4a64f543 6806 int (*filter) (const struct breakpoint *))
c4093a6a 6807{
52f0bd74 6808 struct breakpoint *b;
a6d9a66e 6809 struct bp_location *last_loc = NULL;
7f3b0473 6810 int nr_printable_breakpoints;
3b31d625 6811 struct cleanup *bkpttbl_chain;
79a45b7d 6812 struct value_print_options opts;
a6d9a66e 6813 int print_address_bits = 0;
269b11a2 6814 int print_type_col_width = 14;
79a45e25 6815 struct ui_out *uiout = current_uiout;
269b11a2 6816
79a45b7d
TT
6817 get_user_print_options (&opts);
6818
4a64f543
MS
6819 /* Compute the number of rows in the table, as well as the size
6820 required for address fields. */
7f3b0473
AC
6821 nr_printable_breakpoints = 0;
6822 ALL_BREAKPOINTS (b)
e5a67952
MS
6823 {
6824 /* If we have a filter, only list the breakpoints it accepts. */
6825 if (filter && !filter (b))
6826 continue;
6827
6828 /* If we have an "args" string, it is a list of breakpoints to
6829 accept. Skip the others. */
6830 if (args != NULL && *args != '\0')
6831 {
6832 if (allflag && parse_and_eval_long (args) != b->number)
6833 continue;
6834 if (!allflag && !number_is_in_list (args, b->number))
6835 continue;
6836 }
269b11a2 6837
e5a67952
MS
6838 if (allflag || user_breakpoint_p (b))
6839 {
6840 int addr_bit, type_len;
a6d9a66e 6841
e5a67952
MS
6842 addr_bit = breakpoint_address_bits (b);
6843 if (addr_bit > print_address_bits)
6844 print_address_bits = addr_bit;
269b11a2 6845
e5a67952
MS
6846 type_len = strlen (bptype_string (b->type));
6847 if (type_len > print_type_col_width)
6848 print_type_col_width = type_len;
6849
6850 nr_printable_breakpoints++;
6851 }
6852 }
7f3b0473 6853
79a45b7d 6854 if (opts.addressprint)
3b31d625 6855 bkpttbl_chain
3e43a32a
MS
6856 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6857 nr_printable_breakpoints,
3b31d625 6858 "BreakpointTable");
8b93c638 6859 else
3b31d625 6860 bkpttbl_chain
3e43a32a
MS
6861 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6862 nr_printable_breakpoints,
3b31d625 6863 "BreakpointTable");
8b93c638 6864
7f3b0473 6865 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6866 annotate_breakpoints_headers ();
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (0);
112e8700 6869 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6870 if (nr_printable_breakpoints > 0)
6871 annotate_field (1);
112e8700 6872 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
d7faa9e7
AC
6873 if (nr_printable_breakpoints > 0)
6874 annotate_field (2);
112e8700 6875 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6876 if (nr_printable_breakpoints > 0)
6877 annotate_field (3);
112e8700 6878 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6879 if (opts.addressprint)
e5a67952
MS
6880 {
6881 if (nr_printable_breakpoints > 0)
6882 annotate_field (4);
6883 if (print_address_bits <= 32)
112e8700 6884 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
e5a67952 6885 else
112e8700 6886 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
e5a67952 6887 }
d7faa9e7
AC
6888 if (nr_printable_breakpoints > 0)
6889 annotate_field (5);
112e8700
SM
6890 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6891 uiout->table_body ();
d7faa9e7
AC
6892 if (nr_printable_breakpoints > 0)
6893 annotate_breakpoints_table ();
7f3b0473 6894
c4093a6a 6895 ALL_BREAKPOINTS (b)
e5a67952
MS
6896 {
6897 QUIT;
6898 /* If we have a filter, only list the breakpoints it accepts. */
6899 if (filter && !filter (b))
6900 continue;
6901
6902 /* If we have an "args" string, it is a list of breakpoints to
6903 accept. Skip the others. */
6904
6905 if (args != NULL && *args != '\0')
6906 {
6907 if (allflag) /* maintenance info breakpoint */
6908 {
6909 if (parse_and_eval_long (args) != b->number)
6910 continue;
6911 }
6912 else /* all others */
6913 {
6914 if (!number_is_in_list (args, b->number))
6915 continue;
6916 }
6917 }
6918 /* We only print out user settable breakpoints unless the
6919 allflag is set. */
6920 if (allflag || user_breakpoint_p (b))
12c5a436 6921 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6922 }
6923
3b31d625 6924 do_cleanups (bkpttbl_chain);
698384cd 6925
7f3b0473 6926 if (nr_printable_breakpoints == 0)
c906108c 6927 {
4a64f543
MS
6928 /* If there's a filter, let the caller decide how to report
6929 empty list. */
d77f58be
SS
6930 if (!filter)
6931 {
e5a67952 6932 if (args == NULL || *args == '\0')
112e8700 6933 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6934 else
112e8700 6935 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6936 args);
d77f58be 6937 }
c906108c
SS
6938 }
6939 else
c4093a6a 6940 {
a6d9a66e
UW
6941 if (last_loc && !server_command)
6942 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6943 }
c906108c 6944
4a64f543 6945 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6946 there have been breakpoints? */
c906108c 6947 annotate_breakpoints_table_end ();
d77f58be
SS
6948
6949 return nr_printable_breakpoints;
c906108c
SS
6950}
6951
ad443146
SS
6952/* Display the value of default-collect in a way that is generally
6953 compatible with the breakpoint list. */
6954
6955static void
6956default_collect_info (void)
6957{
79a45e25
PA
6958 struct ui_out *uiout = current_uiout;
6959
ad443146
SS
6960 /* If it has no value (which is frequently the case), say nothing; a
6961 message like "No default-collect." gets in user's face when it's
6962 not wanted. */
6963 if (!*default_collect)
6964 return;
6965
6966 /* The following phrase lines up nicely with per-tracepoint collect
6967 actions. */
112e8700
SM
6968 uiout->text ("default collect ");
6969 uiout->field_string ("default-collect", default_collect);
6970 uiout->text (" \n");
ad443146
SS
6971}
6972
c906108c 6973static void
e5a67952 6974breakpoints_info (char *args, int from_tty)
c906108c 6975{
e5a67952 6976 breakpoint_1 (args, 0, NULL);
ad443146
SS
6977
6978 default_collect_info ();
d77f58be
SS
6979}
6980
6981static void
e5a67952 6982watchpoints_info (char *args, int from_tty)
d77f58be 6983{
e5a67952 6984 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6985 struct ui_out *uiout = current_uiout;
d77f58be
SS
6986
6987 if (num_printed == 0)
6988 {
e5a67952 6989 if (args == NULL || *args == '\0')
112e8700 6990 uiout->message ("No watchpoints.\n");
d77f58be 6991 else
112e8700 6992 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6993 }
c906108c
SS
6994}
6995
7a292a7a 6996static void
e5a67952 6997maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6998{
e5a67952 6999 breakpoint_1 (args, 1, NULL);
ad443146
SS
7000
7001 default_collect_info ();
c906108c
SS
7002}
7003
0d381245 7004static int
714835d5 7005breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7006 struct program_space *pspace,
714835d5 7007 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7008{
7009 struct bp_location *bl = b->loc;
cc59ec59 7010
0d381245
VP
7011 for (; bl; bl = bl->next)
7012 {
6c95b8df
PA
7013 if (bl->pspace == pspace
7014 && bl->address == pc
0d381245
VP
7015 && (!overlay_debugging || bl->section == section))
7016 return 1;
7017 }
7018 return 0;
7019}
7020
672f9b60 7021/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7022 concerns with logical breakpoints, so we match program spaces, not
7023 address spaces. */
c906108c
SS
7024
7025static void
6c95b8df
PA
7026describe_other_breakpoints (struct gdbarch *gdbarch,
7027 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7028 struct obj_section *section, int thread)
c906108c 7029{
52f0bd74
AC
7030 int others = 0;
7031 struct breakpoint *b;
c906108c
SS
7032
7033 ALL_BREAKPOINTS (b)
672f9b60
KP
7034 others += (user_breakpoint_p (b)
7035 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7036 if (others > 0)
7037 {
a3f17187
AC
7038 if (others == 1)
7039 printf_filtered (_("Note: breakpoint "));
7040 else /* if (others == ???) */
7041 printf_filtered (_("Note: breakpoints "));
c906108c 7042 ALL_BREAKPOINTS (b)
672f9b60 7043 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7044 {
7045 others--;
7046 printf_filtered ("%d", b->number);
7047 if (b->thread == -1 && thread != -1)
7048 printf_filtered (" (all threads)");
7049 else if (b->thread != -1)
7050 printf_filtered (" (thread %d)", b->thread);
7051 printf_filtered ("%s%s ",
059fb39f 7052 ((b->enable_state == bp_disabled
f8eba3c6 7053 || b->enable_state == bp_call_disabled)
0d381245 7054 ? " (disabled)"
0d381245
VP
7055 : ""),
7056 (others > 1) ? ","
7057 : ((others == 1) ? " and" : ""));
7058 }
a3f17187 7059 printf_filtered (_("also set at pc "));
5af949e3 7060 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7061 printf_filtered (".\n");
7062 }
7063}
7064\f
c906108c 7065
e4f237da 7066/* Return true iff it is meaningful to use the address member of
244558af
LM
7067 BPT locations. For some breakpoint types, the locations' address members
7068 are irrelevant and it makes no sense to attempt to compare them to other
7069 addresses (or use them for any other purpose either).
e4f237da 7070
4a64f543 7071 More specifically, each of the following breakpoint types will
244558af 7072 always have a zero valued location address and we don't want to mark
4a64f543 7073 breakpoints of any of these types to be a duplicate of an actual
244558af 7074 breakpoint location at address zero:
e4f237da
KB
7075
7076 bp_watchpoint
2d134ed3
PA
7077 bp_catchpoint
7078
7079*/
e4f237da
KB
7080
7081static int
7082breakpoint_address_is_meaningful (struct breakpoint *bpt)
7083{
7084 enum bptype type = bpt->type;
7085
2d134ed3
PA
7086 return (type != bp_watchpoint && type != bp_catchpoint);
7087}
7088
7089/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7090 true if LOC1 and LOC2 represent the same watchpoint location. */
7091
7092static int
4a64f543
MS
7093watchpoint_locations_match (struct bp_location *loc1,
7094 struct bp_location *loc2)
2d134ed3 7095{
3a5c3e22
PA
7096 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7097 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7098
7099 /* Both of them must exist. */
7100 gdb_assert (w1 != NULL);
7101 gdb_assert (w2 != NULL);
2bdf28a0 7102
4a64f543
MS
7103 /* If the target can evaluate the condition expression in hardware,
7104 then we we need to insert both watchpoints even if they are at
7105 the same place. Otherwise the watchpoint will only trigger when
7106 the condition of whichever watchpoint was inserted evaluates to
7107 true, not giving a chance for GDB to check the condition of the
7108 other watchpoint. */
3a5c3e22 7109 if ((w1->cond_exp
4a64f543
MS
7110 && target_can_accel_watchpoint_condition (loc1->address,
7111 loc1->length,
0cf6dd15 7112 loc1->watchpoint_type,
4d01a485 7113 w1->cond_exp.get ()))
3a5c3e22 7114 || (w2->cond_exp
4a64f543
MS
7115 && target_can_accel_watchpoint_condition (loc2->address,
7116 loc2->length,
0cf6dd15 7117 loc2->watchpoint_type,
4d01a485 7118 w2->cond_exp.get ())))
0cf6dd15
TJB
7119 return 0;
7120
85d721b8
PA
7121 /* Note that this checks the owner's type, not the location's. In
7122 case the target does not support read watchpoints, but does
7123 support access watchpoints, we'll have bp_read_watchpoint
7124 watchpoints with hw_access locations. Those should be considered
7125 duplicates of hw_read locations. The hw_read locations will
7126 become hw_access locations later. */
2d134ed3
PA
7127 return (loc1->owner->type == loc2->owner->type
7128 && loc1->pspace->aspace == loc2->pspace->aspace
7129 && loc1->address == loc2->address
7130 && loc1->length == loc2->length);
e4f237da
KB
7131}
7132
31e77af2 7133/* See breakpoint.h. */
6c95b8df 7134
31e77af2 7135int
6c95b8df
PA
7136breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7137 struct address_space *aspace2, CORE_ADDR addr2)
7138{
f5656ead 7139 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7140 || aspace1 == aspace2)
7141 && addr1 == addr2);
7142}
7143
f1310107
TJB
7144/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7145 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7146 matches ASPACE2. On targets that have global breakpoints, the address
7147 space doesn't really matter. */
7148
7149static int
7150breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7151 int len1, struct address_space *aspace2,
7152 CORE_ADDR addr2)
7153{
f5656ead 7154 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7155 || aspace1 == aspace2)
7156 && addr2 >= addr1 && addr2 < addr1 + len1);
7157}
7158
7159/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7160 a ranged breakpoint. In most targets, a match happens only if ASPACE
7161 matches the breakpoint's address space. On targets that have global
7162 breakpoints, the address space doesn't really matter. */
7163
7164static int
7165breakpoint_location_address_match (struct bp_location *bl,
7166 struct address_space *aspace,
7167 CORE_ADDR addr)
7168{
7169 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7170 aspace, addr)
7171 || (bl->length
7172 && breakpoint_address_match_range (bl->pspace->aspace,
7173 bl->address, bl->length,
7174 aspace, addr)));
7175}
7176
d35ae833
PA
7177/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7178 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7179 match happens only if ASPACE matches the breakpoint's address
7180 space. On targets that have global breakpoints, the address space
7181 doesn't really matter. */
7182
7183static int
7184breakpoint_location_address_range_overlap (struct bp_location *bl,
7185 struct address_space *aspace,
7186 CORE_ADDR addr, int len)
7187{
7188 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7189 || bl->pspace->aspace == aspace)
7190 {
7191 int bl_len = bl->length != 0 ? bl->length : 1;
7192
7193 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7194 return 1;
7195 }
7196 return 0;
7197}
7198
1e4d1764
YQ
7199/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7200 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7201 true, otherwise returns false. */
7202
7203static int
7204tracepoint_locations_match (struct bp_location *loc1,
7205 struct bp_location *loc2)
7206{
7207 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7208 /* Since tracepoint locations are never duplicated with others', tracepoint
7209 locations at the same address of different tracepoints are regarded as
7210 different locations. */
7211 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7212 else
7213 return 0;
7214}
7215
2d134ed3
PA
7216/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7217 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7218 represent the same location. */
7219
7220static int
4a64f543
MS
7221breakpoint_locations_match (struct bp_location *loc1,
7222 struct bp_location *loc2)
2d134ed3 7223{
2bdf28a0
JK
7224 int hw_point1, hw_point2;
7225
7226 /* Both of them must not be in moribund_locations. */
7227 gdb_assert (loc1->owner != NULL);
7228 gdb_assert (loc2->owner != NULL);
7229
7230 hw_point1 = is_hardware_watchpoint (loc1->owner);
7231 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7232
7233 if (hw_point1 != hw_point2)
7234 return 0;
7235 else if (hw_point1)
7236 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7237 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7238 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7239 else
f1310107
TJB
7240 /* We compare bp_location.length in order to cover ranged breakpoints. */
7241 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7242 loc2->pspace->aspace, loc2->address)
7243 && loc1->length == loc2->length);
2d134ed3
PA
7244}
7245
76897487
KB
7246static void
7247breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7248 int bnum, int have_bnum)
7249{
f63fbe86
MS
7250 /* The longest string possibly returned by hex_string_custom
7251 is 50 chars. These must be at least that big for safety. */
7252 char astr1[64];
7253 char astr2[64];
76897487 7254
bb599908
PH
7255 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7256 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7257 if (have_bnum)
8a3fe4f8 7258 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7259 bnum, astr1, astr2);
7260 else
8a3fe4f8 7261 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7262}
7263
4a64f543
MS
7264/* Adjust a breakpoint's address to account for architectural
7265 constraints on breakpoint placement. Return the adjusted address.
7266 Note: Very few targets require this kind of adjustment. For most
7267 targets, this function is simply the identity function. */
76897487
KB
7268
7269static CORE_ADDR
a6d9a66e
UW
7270adjust_breakpoint_address (struct gdbarch *gdbarch,
7271 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7272{
a6d9a66e 7273 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7274 {
7275 /* Very few targets need any kind of breakpoint adjustment. */
7276 return bpaddr;
7277 }
88f7da05
KB
7278 else if (bptype == bp_watchpoint
7279 || bptype == bp_hardware_watchpoint
7280 || bptype == bp_read_watchpoint
7281 || bptype == bp_access_watchpoint
fe798b75 7282 || bptype == bp_catchpoint)
88f7da05
KB
7283 {
7284 /* Watchpoints and the various bp_catch_* eventpoints should not
7285 have their addresses modified. */
7286 return bpaddr;
7287 }
7c16b83e
PA
7288 else if (bptype == bp_single_step)
7289 {
7290 /* Single-step breakpoints should not have their addresses
7291 modified. If there's any architectural constrain that
7292 applies to this address, then it should have already been
7293 taken into account when the breakpoint was created in the
7294 first place. If we didn't do this, stepping through e.g.,
7295 Thumb-2 IT blocks would break. */
7296 return bpaddr;
7297 }
76897487
KB
7298 else
7299 {
7300 CORE_ADDR adjusted_bpaddr;
7301
7302 /* Some targets have architectural constraints on the placement
7303 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7304 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7305
7306 /* An adjusted breakpoint address can significantly alter
7307 a user's expectations. Print a warning if an adjustment
7308 is required. */
7309 if (adjusted_bpaddr != bpaddr)
7310 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7311
7312 return adjusted_bpaddr;
7313 }
7314}
7315
5625a286 7316bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7317{
5625a286 7318 bp_location *loc = this;
7cc221ef 7319
348d480f
PA
7320 gdb_assert (ops != NULL);
7321
28010a5d
PA
7322 loc->ops = ops;
7323 loc->owner = owner;
b775012e 7324 loc->cond_bytecode = NULL;
0d381245
VP
7325 loc->shlib_disabled = 0;
7326 loc->enabled = 1;
e049a4b5 7327
28010a5d 7328 switch (owner->type)
e049a4b5
DJ
7329 {
7330 case bp_breakpoint:
7c16b83e 7331 case bp_single_step:
e049a4b5
DJ
7332 case bp_until:
7333 case bp_finish:
7334 case bp_longjmp:
7335 case bp_longjmp_resume:
e2e4d78b 7336 case bp_longjmp_call_dummy:
186c406b
TT
7337 case bp_exception:
7338 case bp_exception_resume:
e049a4b5 7339 case bp_step_resume:
2c03e5be 7340 case bp_hp_step_resume:
e049a4b5
DJ
7341 case bp_watchpoint_scope:
7342 case bp_call_dummy:
aa7d318d 7343 case bp_std_terminate:
e049a4b5
DJ
7344 case bp_shlib_event:
7345 case bp_thread_event:
7346 case bp_overlay_event:
4efc6507 7347 case bp_jit_event:
0fd8e87f 7348 case bp_longjmp_master:
aa7d318d 7349 case bp_std_terminate_master:
186c406b 7350 case bp_exception_master:
0e30163f
JK
7351 case bp_gnu_ifunc_resolver:
7352 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7353 case bp_dprintf:
e049a4b5 7354 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7355 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7356 break;
7357 case bp_hardware_breakpoint:
7358 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7359 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7360 break;
7361 case bp_hardware_watchpoint:
7362 case bp_read_watchpoint:
7363 case bp_access_watchpoint:
7364 loc->loc_type = bp_loc_hardware_watchpoint;
7365 break;
7366 case bp_watchpoint:
ce78b96d 7367 case bp_catchpoint:
15c3d785
PA
7368 case bp_tracepoint:
7369 case bp_fast_tracepoint:
0fb4aa4b 7370 case bp_static_tracepoint:
e049a4b5
DJ
7371 loc->loc_type = bp_loc_other;
7372 break;
7373 default:
e2e0b3e5 7374 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7375 }
7376
f431efe5 7377 loc->refc = 1;
28010a5d
PA
7378}
7379
7380/* Allocate a struct bp_location. */
7381
7382static struct bp_location *
7383allocate_bp_location (struct breakpoint *bpt)
7384{
348d480f
PA
7385 return bpt->ops->allocate_location (bpt);
7386}
7cc221ef 7387
f431efe5
PA
7388static void
7389free_bp_location (struct bp_location *loc)
fe3f5fa8 7390{
348d480f 7391 loc->ops->dtor (loc);
4d01a485 7392 delete loc;
fe3f5fa8
VP
7393}
7394
f431efe5
PA
7395/* Increment reference count. */
7396
7397static void
7398incref_bp_location (struct bp_location *bl)
7399{
7400 ++bl->refc;
7401}
7402
7403/* Decrement reference count. If the reference count reaches 0,
7404 destroy the bp_location. Sets *BLP to NULL. */
7405
7406static void
7407decref_bp_location (struct bp_location **blp)
7408{
0807b50c
PA
7409 gdb_assert ((*blp)->refc > 0);
7410
f431efe5
PA
7411 if (--(*blp)->refc == 0)
7412 free_bp_location (*blp);
7413 *blp = NULL;
7414}
7415
346774a9 7416/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7417
346774a9
PA
7418static void
7419add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7420{
346774a9 7421 struct breakpoint *b1;
c906108c 7422
346774a9
PA
7423 /* Add this breakpoint to the end of the chain so that a list of
7424 breakpoints will come out in order of increasing numbers. */
7425
7426 b1 = breakpoint_chain;
7427 if (b1 == 0)
7428 breakpoint_chain = b;
7429 else
7430 {
7431 while (b1->next)
7432 b1 = b1->next;
7433 b1->next = b;
7434 }
7435}
7436
7437/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7438
7439static void
7440init_raw_breakpoint_without_location (struct breakpoint *b,
7441 struct gdbarch *gdbarch,
28010a5d 7442 enum bptype bptype,
c0a91b2b 7443 const struct breakpoint_ops *ops)
346774a9 7444{
348d480f
PA
7445 gdb_assert (ops != NULL);
7446
28010a5d 7447 b->ops = ops;
4d28f7a8 7448 b->type = bptype;
a6d9a66e 7449 b->gdbarch = gdbarch;
c906108c
SS
7450 b->language = current_language->la_language;
7451 b->input_radix = input_radix;
d0fb5eae 7452 b->related_breakpoint = b;
346774a9
PA
7453}
7454
7455/* Helper to set_raw_breakpoint below. Creates a breakpoint
7456 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7457
7458static struct breakpoint *
7459set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7460 enum bptype bptype,
c0a91b2b 7461 const struct breakpoint_ops *ops)
346774a9 7462{
3b0871f4 7463 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7464
3b0871f4
SM
7465 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
7466 add_to_breakpoint_chain (b.get ());
7467
7468 return b.release ();
0d381245
VP
7469}
7470
0e30163f
JK
7471/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7472 resolutions should be made as the user specified the location explicitly
7473 enough. */
7474
0d381245 7475static void
0e30163f 7476set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7477{
2bdf28a0
JK
7478 gdb_assert (loc->owner != NULL);
7479
0d381245 7480 if (loc->owner->type == bp_breakpoint
1042e4c0 7481 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7482 || is_tracepoint (loc->owner))
0d381245 7483 {
0e30163f 7484 int is_gnu_ifunc;
2c02bd72 7485 const char *function_name;
6a3a010b 7486 CORE_ADDR func_addr;
0e30163f 7487
2c02bd72 7488 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7489 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7490
7491 if (is_gnu_ifunc && !explicit_loc)
7492 {
7493 struct breakpoint *b = loc->owner;
7494
7495 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7496 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7497 &loc->requested_address))
7498 {
7499 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7500 loc->address = adjust_breakpoint_address (loc->gdbarch,
7501 loc->requested_address,
7502 b->type);
7503 }
7504 else if (b->type == bp_breakpoint && b->loc == loc
7505 && loc->next == NULL && b->related_breakpoint == b)
7506 {
7507 /* Create only the whole new breakpoint of this type but do not
7508 mess more complicated breakpoints with multiple locations. */
7509 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7510 /* Remember the resolver's address for use by the return
7511 breakpoint. */
7512 loc->related_address = func_addr;
0e30163f
JK
7513 }
7514 }
7515
2c02bd72
DE
7516 if (function_name)
7517 loc->function_name = xstrdup (function_name);
0d381245
VP
7518 }
7519}
7520
a6d9a66e 7521/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7522struct gdbarch *
a6d9a66e
UW
7523get_sal_arch (struct symtab_and_line sal)
7524{
7525 if (sal.section)
7526 return get_objfile_arch (sal.section->objfile);
7527 if (sal.symtab)
eb822aa6 7528 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7529
7530 return NULL;
7531}
7532
346774a9
PA
7533/* Low level routine for partially initializing a breakpoint of type
7534 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7535 file name, and line number are provided by SAL.
0d381245
VP
7536
7537 It is expected that the caller will complete the initialization of
7538 the newly created breakpoint struct as well as output any status
c56053d2 7539 information regarding the creation of a new breakpoint. */
0d381245 7540
346774a9
PA
7541static void
7542init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7543 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7544 const struct breakpoint_ops *ops)
0d381245 7545{
28010a5d 7546 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7547
3742cc8b 7548 add_location_to_breakpoint (b, &sal);
0d381245 7549
6c95b8df
PA
7550 if (bptype != bp_catchpoint)
7551 gdb_assert (sal.pspace != NULL);
7552
f8eba3c6
TT
7553 /* Store the program space that was used to set the breakpoint,
7554 except for ordinary breakpoints, which are independent of the
7555 program space. */
7556 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7557 b->pspace = sal.pspace;
346774a9 7558}
c906108c 7559
346774a9
PA
7560/* set_raw_breakpoint is a low level routine for allocating and
7561 partially initializing a breakpoint of type BPTYPE. The newly
7562 created breakpoint's address, section, source file name, and line
7563 number are provided by SAL. The newly created and partially
7564 initialized breakpoint is added to the breakpoint chain and
7565 is also returned as the value of this function.
7566
7567 It is expected that the caller will complete the initialization of
7568 the newly created breakpoint struct as well as output any status
7569 information regarding the creation of a new breakpoint. In
7570 particular, set_raw_breakpoint does NOT set the breakpoint
7571 number! Care should be taken to not allow an error to occur
7572 prior to completing the initialization of the breakpoint. If this
7573 should happen, a bogus breakpoint will be left on the chain. */
7574
7575struct breakpoint *
7576set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7577 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7578 const struct breakpoint_ops *ops)
346774a9 7579{
3b0871f4 7580 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7581
3b0871f4
SM
7582 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
7583 add_to_breakpoint_chain (b.get ());
7584
7585 return b.release ();
c906108c
SS
7586}
7587
53a5351d 7588/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7589 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7590 initiated the operation. */
c906108c
SS
7591
7592void
186c406b 7593set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7594{
35df4500 7595 struct breakpoint *b, *b_tmp;
5d5658a1 7596 int thread = tp->global_num;
0fd8e87f
UW
7597
7598 /* To avoid having to rescan all objfile symbols at every step,
7599 we maintain a list of continually-inserted but always disabled
7600 longjmp "master" breakpoints. Here, we simply create momentary
7601 clones of those and enable them for the requested thread. */
35df4500 7602 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7603 if (b->pspace == current_program_space
186c406b
TT
7604 && (b->type == bp_longjmp_master
7605 || b->type == bp_exception_master))
0fd8e87f 7606 {
06edf0c0
PA
7607 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7608 struct breakpoint *clone;
cc59ec59 7609
e2e4d78b
JK
7610 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7611 after their removal. */
06edf0c0 7612 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7613 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7614 clone->thread = thread;
7615 }
186c406b
TT
7616
7617 tp->initiating_frame = frame;
c906108c
SS
7618}
7619
611c83ae 7620/* Delete all longjmp breakpoints from THREAD. */
c906108c 7621void
611c83ae 7622delete_longjmp_breakpoint (int thread)
c906108c 7623{
35df4500 7624 struct breakpoint *b, *b_tmp;
c906108c 7625
35df4500 7626 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7627 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7628 {
7629 if (b->thread == thread)
7630 delete_breakpoint (b);
7631 }
c906108c
SS
7632}
7633
f59f708a
PA
7634void
7635delete_longjmp_breakpoint_at_next_stop (int thread)
7636{
7637 struct breakpoint *b, *b_tmp;
7638
7639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7640 if (b->type == bp_longjmp || b->type == bp_exception)
7641 {
7642 if (b->thread == thread)
7643 b->disposition = disp_del_at_next_stop;
7644 }
7645}
7646
e2e4d78b
JK
7647/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7648 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7649 pointer to any of them. Return NULL if this system cannot place longjmp
7650 breakpoints. */
7651
7652struct breakpoint *
7653set_longjmp_breakpoint_for_call_dummy (void)
7654{
7655 struct breakpoint *b, *retval = NULL;
7656
7657 ALL_BREAKPOINTS (b)
7658 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7659 {
7660 struct breakpoint *new_b;
7661
7662 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7663 &momentary_breakpoint_ops,
7664 1);
5d5658a1 7665 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7666
7667 /* Link NEW_B into the chain of RETVAL breakpoints. */
7668
7669 gdb_assert (new_b->related_breakpoint == new_b);
7670 if (retval == NULL)
7671 retval = new_b;
7672 new_b->related_breakpoint = retval;
7673 while (retval->related_breakpoint != new_b->related_breakpoint)
7674 retval = retval->related_breakpoint;
7675 retval->related_breakpoint = new_b;
7676 }
7677
7678 return retval;
7679}
7680
7681/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7682 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7683 stack.
7684
7685 You should call this function only at places where it is safe to currently
7686 unwind the whole stack. Failed stack unwind would discard live dummy
7687 frames. */
7688
7689void
b67a2c6f 7690check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7691{
7692 struct breakpoint *b, *b_tmp;
7693
7694 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7695 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7696 {
7697 struct breakpoint *dummy_b = b->related_breakpoint;
7698
7699 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7700 dummy_b = dummy_b->related_breakpoint;
7701 if (dummy_b->type != bp_call_dummy
7702 || frame_find_by_id (dummy_b->frame_id) != NULL)
7703 continue;
7704
b67a2c6f 7705 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7706
7707 while (b->related_breakpoint != b)
7708 {
7709 if (b_tmp == b->related_breakpoint)
7710 b_tmp = b->related_breakpoint->next;
7711 delete_breakpoint (b->related_breakpoint);
7712 }
7713 delete_breakpoint (b);
7714 }
7715}
7716
1900040c
MS
7717void
7718enable_overlay_breakpoints (void)
7719{
52f0bd74 7720 struct breakpoint *b;
1900040c
MS
7721
7722 ALL_BREAKPOINTS (b)
7723 if (b->type == bp_overlay_event)
7724 {
7725 b->enable_state = bp_enabled;
44702360 7726 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7727 overlay_events_enabled = 1;
1900040c
MS
7728 }
7729}
7730
7731void
7732disable_overlay_breakpoints (void)
7733{
52f0bd74 7734 struct breakpoint *b;
1900040c
MS
7735
7736 ALL_BREAKPOINTS (b)
7737 if (b->type == bp_overlay_event)
7738 {
7739 b->enable_state = bp_disabled;
44702360 7740 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7741 overlay_events_enabled = 0;
1900040c
MS
7742 }
7743}
7744
aa7d318d
TT
7745/* Set an active std::terminate breakpoint for each std::terminate
7746 master breakpoint. */
7747void
7748set_std_terminate_breakpoint (void)
7749{
35df4500 7750 struct breakpoint *b, *b_tmp;
aa7d318d 7751
35df4500 7752 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7753 if (b->pspace == current_program_space
7754 && b->type == bp_std_terminate_master)
7755 {
06edf0c0 7756 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7757 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7758 }
7759}
7760
7761/* Delete all the std::terminate breakpoints. */
7762void
7763delete_std_terminate_breakpoint (void)
7764{
35df4500 7765 struct breakpoint *b, *b_tmp;
aa7d318d 7766
35df4500 7767 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7768 if (b->type == bp_std_terminate)
7769 delete_breakpoint (b);
7770}
7771
c4093a6a 7772struct breakpoint *
a6d9a66e 7773create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7774{
7775 struct breakpoint *b;
c4093a6a 7776
06edf0c0
PA
7777 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7778 &internal_breakpoint_ops);
7779
b5de0fa7 7780 b->enable_state = bp_enabled;
f00aae0f 7781 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7782 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7783
44702360 7784 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7785
c4093a6a
JM
7786 return b;
7787}
7788
0101ce28
JJ
7789struct lang_and_radix
7790 {
7791 enum language lang;
7792 int radix;
7793 };
7794
4efc6507
DE
7795/* Create a breakpoint for JIT code registration and unregistration. */
7796
7797struct breakpoint *
7798create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7799{
2a7f3dff
PA
7800 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7801 &internal_breakpoint_ops);
4efc6507 7802}
0101ce28 7803
03673fc7
PP
7804/* Remove JIT code registration and unregistration breakpoint(s). */
7805
7806void
7807remove_jit_event_breakpoints (void)
7808{
7809 struct breakpoint *b, *b_tmp;
7810
7811 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7812 if (b->type == bp_jit_event
7813 && b->loc->pspace == current_program_space)
7814 delete_breakpoint (b);
7815}
7816
cae688ec
JJ
7817void
7818remove_solib_event_breakpoints (void)
7819{
35df4500 7820 struct breakpoint *b, *b_tmp;
cae688ec 7821
35df4500 7822 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7823 if (b->type == bp_shlib_event
7824 && b->loc->pspace == current_program_space)
cae688ec
JJ
7825 delete_breakpoint (b);
7826}
7827
f37f681c
PA
7828/* See breakpoint.h. */
7829
7830void
7831remove_solib_event_breakpoints_at_next_stop (void)
7832{
7833 struct breakpoint *b, *b_tmp;
7834
7835 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7836 if (b->type == bp_shlib_event
7837 && b->loc->pspace == current_program_space)
7838 b->disposition = disp_del_at_next_stop;
7839}
7840
04086b45
PA
7841/* Helper for create_solib_event_breakpoint /
7842 create_and_insert_solib_event_breakpoint. Allows specifying which
7843 INSERT_MODE to pass through to update_global_location_list. */
7844
7845static struct breakpoint *
7846create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7847 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7848{
7849 struct breakpoint *b;
7850
06edf0c0
PA
7851 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7852 &internal_breakpoint_ops);
04086b45 7853 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7854 return b;
7855}
7856
04086b45
PA
7857struct breakpoint *
7858create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7859{
7860 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7861}
7862
f37f681c
PA
7863/* See breakpoint.h. */
7864
7865struct breakpoint *
7866create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7867{
7868 struct breakpoint *b;
7869
04086b45
PA
7870 /* Explicitly tell update_global_location_list to insert
7871 locations. */
7872 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7873 if (!b->loc->inserted)
7874 {
7875 delete_breakpoint (b);
7876 return NULL;
7877 }
7878 return b;
7879}
7880
cae688ec
JJ
7881/* Disable any breakpoints that are on code in shared libraries. Only
7882 apply to enabled breakpoints, disabled ones can just stay disabled. */
7883
7884void
cb851954 7885disable_breakpoints_in_shlibs (void)
cae688ec 7886{
876fa593 7887 struct bp_location *loc, **locp_tmp;
cae688ec 7888
876fa593 7889 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7890 {
2bdf28a0 7891 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7892 struct breakpoint *b = loc->owner;
2bdf28a0 7893
4a64f543
MS
7894 /* We apply the check to all breakpoints, including disabled for
7895 those with loc->duplicate set. This is so that when breakpoint
7896 becomes enabled, or the duplicate is removed, gdb will try to
7897 insert all breakpoints. If we don't set shlib_disabled here,
7898 we'll try to insert those breakpoints and fail. */
1042e4c0 7899 if (((b->type == bp_breakpoint)
508ccb1f 7900 || (b->type == bp_jit_event)
1042e4c0 7901 || (b->type == bp_hardware_breakpoint)
d77f58be 7902 || (is_tracepoint (b)))
6c95b8df 7903 && loc->pspace == current_program_space
0d381245 7904 && !loc->shlib_disabled
6c95b8df 7905 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7906 )
0d381245
VP
7907 {
7908 loc->shlib_disabled = 1;
7909 }
cae688ec
JJ
7910 }
7911}
7912
63644780
NB
7913/* Disable any breakpoints and tracepoints that are in SOLIB upon
7914 notification of unloaded_shlib. Only apply to enabled breakpoints,
7915 disabled ones can just stay disabled. */
84acb35a 7916
75149521 7917static void
84acb35a
JJ
7918disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7919{
876fa593 7920 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7921 int disabled_shlib_breaks = 0;
7922
876fa593 7923 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7924 {
2bdf28a0 7925 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7926 struct breakpoint *b = loc->owner;
cc59ec59 7927
1e4d1764 7928 if (solib->pspace == loc->pspace
e2dd7057 7929 && !loc->shlib_disabled
1e4d1764
YQ
7930 && (((b->type == bp_breakpoint
7931 || b->type == bp_jit_event
7932 || b->type == bp_hardware_breakpoint)
7933 && (loc->loc_type == bp_loc_hardware_breakpoint
7934 || loc->loc_type == bp_loc_software_breakpoint))
7935 || is_tracepoint (b))
e2dd7057 7936 && solib_contains_address_p (solib, loc->address))
84acb35a 7937 {
e2dd7057
PP
7938 loc->shlib_disabled = 1;
7939 /* At this point, we cannot rely on remove_breakpoint
7940 succeeding so we must mark the breakpoint as not inserted
7941 to prevent future errors occurring in remove_breakpoints. */
7942 loc->inserted = 0;
8d3788bd
VP
7943
7944 /* This may cause duplicate notifications for the same breakpoint. */
7945 observer_notify_breakpoint_modified (b);
7946
e2dd7057
PP
7947 if (!disabled_shlib_breaks)
7948 {
7949 target_terminal_ours_for_output ();
3e43a32a
MS
7950 warning (_("Temporarily disabling breakpoints "
7951 "for unloaded shared library \"%s\""),
e2dd7057 7952 solib->so_name);
84acb35a 7953 }
e2dd7057 7954 disabled_shlib_breaks = 1;
84acb35a
JJ
7955 }
7956 }
84acb35a
JJ
7957}
7958
63644780
NB
7959/* Disable any breakpoints and tracepoints in OBJFILE upon
7960 notification of free_objfile. Only apply to enabled breakpoints,
7961 disabled ones can just stay disabled. */
7962
7963static void
7964disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7965{
7966 struct breakpoint *b;
7967
7968 if (objfile == NULL)
7969 return;
7970
d03de421
PA
7971 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7972 managed by the user with add-symbol-file/remove-symbol-file.
7973 Similarly to how breakpoints in shared libraries are handled in
7974 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7975 shlib_disabled so they end up uninserted on the next global
7976 location list update. Shared libraries not loaded by the user
7977 aren't handled here -- they're already handled in
7978 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7979 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7980 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7981 main objfile). */
7982 if ((objfile->flags & OBJF_SHARED) == 0
7983 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7984 return;
7985
7986 ALL_BREAKPOINTS (b)
7987 {
7988 struct bp_location *loc;
7989 int bp_modified = 0;
7990
7991 if (!is_breakpoint (b) && !is_tracepoint (b))
7992 continue;
7993
7994 for (loc = b->loc; loc != NULL; loc = loc->next)
7995 {
7996 CORE_ADDR loc_addr = loc->address;
7997
7998 if (loc->loc_type != bp_loc_hardware_breakpoint
7999 && loc->loc_type != bp_loc_software_breakpoint)
8000 continue;
8001
8002 if (loc->shlib_disabled != 0)
8003 continue;
8004
8005 if (objfile->pspace != loc->pspace)
8006 continue;
8007
8008 if (loc->loc_type != bp_loc_hardware_breakpoint
8009 && loc->loc_type != bp_loc_software_breakpoint)
8010 continue;
8011
8012 if (is_addr_in_objfile (loc_addr, objfile))
8013 {
8014 loc->shlib_disabled = 1;
08351840
PA
8015 /* At this point, we don't know whether the object was
8016 unmapped from the inferior or not, so leave the
8017 inserted flag alone. We'll handle failure to
8018 uninsert quietly, in case the object was indeed
8019 unmapped. */
63644780
NB
8020
8021 mark_breakpoint_location_modified (loc);
8022
8023 bp_modified = 1;
8024 }
8025 }
8026
8027 if (bp_modified)
8028 observer_notify_breakpoint_modified (b);
8029 }
8030}
8031
ce78b96d
JB
8032/* FORK & VFORK catchpoints. */
8033
e29a4733 8034/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
8035 catchpoint. A breakpoint is really of this type iff its ops pointer points
8036 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 8037
c1fc2657 8038struct fork_catchpoint : public breakpoint
e29a4733 8039{
e29a4733
PA
8040 /* Process id of a child process whose forking triggered this
8041 catchpoint. This field is only valid immediately after this
8042 catchpoint has triggered. */
8043 ptid_t forked_inferior_pid;
8044};
8045
4a64f543
MS
8046/* Implement the "insert" breakpoint_ops method for fork
8047 catchpoints. */
ce78b96d 8048
77b06cd7
TJB
8049static int
8050insert_catch_fork (struct bp_location *bl)
ce78b96d 8051{
dfd4cc63 8052 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8053}
8054
4a64f543
MS
8055/* Implement the "remove" breakpoint_ops method for fork
8056 catchpoints. */
ce78b96d
JB
8057
8058static int
73971819 8059remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8060{
dfd4cc63 8061 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8062}
8063
8064/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8065 catchpoints. */
8066
8067static int
f1310107 8068breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8069 struct address_space *aspace, CORE_ADDR bp_addr,
8070 const struct target_waitstatus *ws)
ce78b96d 8071{
e29a4733
PA
8072 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8073
f90263c1
TT
8074 if (ws->kind != TARGET_WAITKIND_FORKED)
8075 return 0;
8076
8077 c->forked_inferior_pid = ws->value.related_pid;
8078 return 1;
ce78b96d
JB
8079}
8080
4a64f543
MS
8081/* Implement the "print_it" breakpoint_ops method for fork
8082 catchpoints. */
ce78b96d
JB
8083
8084static enum print_stop_action
348d480f 8085print_it_catch_fork (bpstat bs)
ce78b96d 8086{
36dfb11c 8087 struct ui_out *uiout = current_uiout;
348d480f
PA
8088 struct breakpoint *b = bs->breakpoint_at;
8089 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8090
ce78b96d 8091 annotate_catchpoint (b->number);
f303dbd6 8092 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8093 if (b->disposition == disp_del)
112e8700 8094 uiout->text ("Temporary catchpoint ");
36dfb11c 8095 else
112e8700
SM
8096 uiout->text ("Catchpoint ");
8097 if (uiout->is_mi_like_p ())
36dfb11c 8098 {
112e8700
SM
8099 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8100 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8101 }
112e8700
SM
8102 uiout->field_int ("bkptno", b->number);
8103 uiout->text (" (forked process ");
8104 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8105 uiout->text ("), ");
ce78b96d
JB
8106 return PRINT_SRC_AND_LOC;
8107}
8108
4a64f543
MS
8109/* Implement the "print_one" breakpoint_ops method for fork
8110 catchpoints. */
ce78b96d
JB
8111
8112static void
a6d9a66e 8113print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8114{
e29a4733 8115 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8116 struct value_print_options opts;
79a45e25 8117 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8118
8119 get_user_print_options (&opts);
8120
4a64f543
MS
8121 /* Field 4, the address, is omitted (which makes the columns not
8122 line up too nicely with the headers, but the effect is relatively
8123 readable). */
79a45b7d 8124 if (opts.addressprint)
112e8700 8125 uiout->field_skip ("addr");
ce78b96d 8126 annotate_field (5);
112e8700 8127 uiout->text ("fork");
e29a4733 8128 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8129 {
112e8700
SM
8130 uiout->text (", process ");
8131 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8132 uiout->spaces (1);
ce78b96d 8133 }
8ac3646f 8134
112e8700
SM
8135 if (uiout->is_mi_like_p ())
8136 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8137}
8138
8139/* Implement the "print_mention" breakpoint_ops method for fork
8140 catchpoints. */
8141
8142static void
8143print_mention_catch_fork (struct breakpoint *b)
8144{
8145 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8146}
8147
6149aea9
PA
8148/* Implement the "print_recreate" breakpoint_ops method for fork
8149 catchpoints. */
8150
8151static void
8152print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8153{
8154 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8155 print_recreate_thread (b, fp);
6149aea9
PA
8156}
8157
ce78b96d
JB
8158/* The breakpoint_ops structure to be used in fork catchpoints. */
8159
2060206e 8160static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8161
4a64f543
MS
8162/* Implement the "insert" breakpoint_ops method for vfork
8163 catchpoints. */
ce78b96d 8164
77b06cd7
TJB
8165static int
8166insert_catch_vfork (struct bp_location *bl)
ce78b96d 8167{
dfd4cc63 8168 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8169}
8170
4a64f543
MS
8171/* Implement the "remove" breakpoint_ops method for vfork
8172 catchpoints. */
ce78b96d
JB
8173
8174static int
73971819 8175remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8176{
dfd4cc63 8177 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8178}
8179
8180/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8181 catchpoints. */
8182
8183static int
f1310107 8184breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8185 struct address_space *aspace, CORE_ADDR bp_addr,
8186 const struct target_waitstatus *ws)
ce78b96d 8187{
e29a4733
PA
8188 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8189
f90263c1
TT
8190 if (ws->kind != TARGET_WAITKIND_VFORKED)
8191 return 0;
8192
8193 c->forked_inferior_pid = ws->value.related_pid;
8194 return 1;
ce78b96d
JB
8195}
8196
4a64f543
MS
8197/* Implement the "print_it" breakpoint_ops method for vfork
8198 catchpoints. */
ce78b96d
JB
8199
8200static enum print_stop_action
348d480f 8201print_it_catch_vfork (bpstat bs)
ce78b96d 8202{
36dfb11c 8203 struct ui_out *uiout = current_uiout;
348d480f 8204 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8205 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8206
ce78b96d 8207 annotate_catchpoint (b->number);
f303dbd6 8208 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8209 if (b->disposition == disp_del)
112e8700 8210 uiout->text ("Temporary catchpoint ");
36dfb11c 8211 else
112e8700
SM
8212 uiout->text ("Catchpoint ");
8213 if (uiout->is_mi_like_p ())
36dfb11c 8214 {
112e8700
SM
8215 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8216 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8217 }
112e8700
SM
8218 uiout->field_int ("bkptno", b->number);
8219 uiout->text (" (vforked process ");
8220 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8221 uiout->text ("), ");
ce78b96d
JB
8222 return PRINT_SRC_AND_LOC;
8223}
8224
4a64f543
MS
8225/* Implement the "print_one" breakpoint_ops method for vfork
8226 catchpoints. */
ce78b96d
JB
8227
8228static void
a6d9a66e 8229print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8230{
e29a4733 8231 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8232 struct value_print_options opts;
79a45e25 8233 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8234
8235 get_user_print_options (&opts);
4a64f543
MS
8236 /* Field 4, the address, is omitted (which makes the columns not
8237 line up too nicely with the headers, but the effect is relatively
8238 readable). */
79a45b7d 8239 if (opts.addressprint)
112e8700 8240 uiout->field_skip ("addr");
ce78b96d 8241 annotate_field (5);
112e8700 8242 uiout->text ("vfork");
e29a4733 8243 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8244 {
112e8700
SM
8245 uiout->text (", process ");
8246 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8247 uiout->spaces (1);
ce78b96d 8248 }
8ac3646f 8249
112e8700
SM
8250 if (uiout->is_mi_like_p ())
8251 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8252}
8253
8254/* Implement the "print_mention" breakpoint_ops method for vfork
8255 catchpoints. */
8256
8257static void
8258print_mention_catch_vfork (struct breakpoint *b)
8259{
8260 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8261}
8262
6149aea9
PA
8263/* Implement the "print_recreate" breakpoint_ops method for vfork
8264 catchpoints. */
8265
8266static void
8267print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8268{
8269 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8270 print_recreate_thread (b, fp);
6149aea9
PA
8271}
8272
ce78b96d
JB
8273/* The breakpoint_ops structure to be used in vfork catchpoints. */
8274
2060206e 8275static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8276
edcc5120 8277/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8278 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8279 CATCH_SOLIB_BREAKPOINT_OPS. */
8280
c1fc2657 8281struct solib_catchpoint : public breakpoint
edcc5120 8282{
c1fc2657 8283 ~solib_catchpoint () override;
edcc5120
TT
8284
8285 /* True for "catch load", false for "catch unload". */
8286 unsigned char is_load;
8287
8288 /* Regular expression to match, if any. COMPILED is only valid when
8289 REGEX is non-NULL. */
8290 char *regex;
2d7cc5c7 8291 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8292};
8293
c1fc2657 8294solib_catchpoint::~solib_catchpoint ()
edcc5120 8295{
c1fc2657 8296 xfree (this->regex);
edcc5120
TT
8297}
8298
8299static int
8300insert_catch_solib (struct bp_location *ignore)
8301{
8302 return 0;
8303}
8304
8305static int
73971819 8306remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8307{
8308 return 0;
8309}
8310
8311static int
8312breakpoint_hit_catch_solib (const struct bp_location *bl,
8313 struct address_space *aspace,
8314 CORE_ADDR bp_addr,
8315 const struct target_waitstatus *ws)
8316{
8317 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8318 struct breakpoint *other;
8319
8320 if (ws->kind == TARGET_WAITKIND_LOADED)
8321 return 1;
8322
8323 ALL_BREAKPOINTS (other)
8324 {
8325 struct bp_location *other_bl;
8326
8327 if (other == bl->owner)
8328 continue;
8329
8330 if (other->type != bp_shlib_event)
8331 continue;
8332
c1fc2657 8333 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8334 continue;
8335
8336 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8337 {
8338 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8339 return 1;
8340 }
8341 }
8342
8343 return 0;
8344}
8345
8346static void
8347check_status_catch_solib (struct bpstats *bs)
8348{
8349 struct solib_catchpoint *self
8350 = (struct solib_catchpoint *) bs->breakpoint_at;
8351 int ix;
8352
8353 if (self->is_load)
8354 {
8355 struct so_list *iter;
8356
8357 for (ix = 0;
8358 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8359 ix, iter);
8360 ++ix)
8361 {
8362 if (!self->regex
2d7cc5c7 8363 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8364 return;
8365 }
8366 }
8367 else
8368 {
8369 char *iter;
8370
8371 for (ix = 0;
8372 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8373 ix, iter);
8374 ++ix)
8375 {
8376 if (!self->regex
2d7cc5c7 8377 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8378 return;
8379 }
8380 }
8381
8382 bs->stop = 0;
8383 bs->print_it = print_it_noop;
8384}
8385
8386static enum print_stop_action
8387print_it_catch_solib (bpstat bs)
8388{
8389 struct breakpoint *b = bs->breakpoint_at;
8390 struct ui_out *uiout = current_uiout;
8391
8392 annotate_catchpoint (b->number);
f303dbd6 8393 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8394 if (b->disposition == disp_del)
112e8700 8395 uiout->text ("Temporary catchpoint ");
edcc5120 8396 else
112e8700
SM
8397 uiout->text ("Catchpoint ");
8398 uiout->field_int ("bkptno", b->number);
8399 uiout->text ("\n");
8400 if (uiout->is_mi_like_p ())
8401 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8402 print_solib_event (1);
8403 return PRINT_SRC_AND_LOC;
8404}
8405
8406static void
8407print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8408{
8409 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8410 struct value_print_options opts;
8411 struct ui_out *uiout = current_uiout;
8412 char *msg;
8413
8414 get_user_print_options (&opts);
8415 /* Field 4, the address, is omitted (which makes the columns not
8416 line up too nicely with the headers, but the effect is relatively
8417 readable). */
8418 if (opts.addressprint)
8419 {
8420 annotate_field (4);
112e8700 8421 uiout->field_skip ("addr");
edcc5120
TT
8422 }
8423
8424 annotate_field (5);
8425 if (self->is_load)
8426 {
8427 if (self->regex)
8428 msg = xstrprintf (_("load of library matching %s"), self->regex);
8429 else
8430 msg = xstrdup (_("load of library"));
8431 }
8432 else
8433 {
8434 if (self->regex)
8435 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8436 else
8437 msg = xstrdup (_("unload of library"));
8438 }
112e8700 8439 uiout->field_string ("what", msg);
edcc5120 8440 xfree (msg);
8ac3646f 8441
112e8700
SM
8442 if (uiout->is_mi_like_p ())
8443 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8444}
8445
8446static void
8447print_mention_catch_solib (struct breakpoint *b)
8448{
8449 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8450
8451 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8452 self->is_load ? "load" : "unload");
8453}
8454
8455static void
8456print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8457{
8458 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8459
8460 fprintf_unfiltered (fp, "%s %s",
8461 b->disposition == disp_del ? "tcatch" : "catch",
8462 self->is_load ? "load" : "unload");
8463 if (self->regex)
8464 fprintf_unfiltered (fp, " %s", self->regex);
8465 fprintf_unfiltered (fp, "\n");
8466}
8467
8468static struct breakpoint_ops catch_solib_breakpoint_ops;
8469
91985142
MG
8470/* Shared helper function (MI and CLI) for creating and installing
8471 a shared object event catchpoint. If IS_LOAD is non-zero then
8472 the events to be caught are load events, otherwise they are
8473 unload events. If IS_TEMP is non-zero the catchpoint is a
8474 temporary one. If ENABLED is non-zero the catchpoint is
8475 created in an enabled state. */
edcc5120 8476
91985142 8477void
a121b7c1 8478add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8479{
8480 struct solib_catchpoint *c;
8481 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8482 struct cleanup *cleanup;
8483
edcc5120
TT
8484 if (!arg)
8485 arg = "";
a121b7c1 8486 arg = skip_spaces_const (arg);
edcc5120 8487
4d01a485 8488 c = new solib_catchpoint ();
edcc5120
TT
8489 cleanup = make_cleanup (xfree, c);
8490
8491 if (*arg != '\0')
8492 {
2d7cc5c7
PA
8493 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8494 _("Invalid regexp")));
edcc5120
TT
8495 c->regex = xstrdup (arg);
8496 }
8497
8498 c->is_load = is_load;
c1fc2657 8499 init_catchpoint (c, gdbarch, is_temp, NULL,
edcc5120
TT
8500 &catch_solib_breakpoint_ops);
8501
c1fc2657 8502 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8503
edcc5120 8504 discard_cleanups (cleanup);
c1fc2657 8505 install_breakpoint (0, c, 1);
edcc5120
TT
8506}
8507
91985142
MG
8508/* A helper function that does all the work for "catch load" and
8509 "catch unload". */
8510
8511static void
8512catch_load_or_unload (char *arg, int from_tty, int is_load,
8513 struct cmd_list_element *command)
8514{
8515 int tempflag;
8516 const int enabled = 1;
8517
8518 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8519
8520 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8521}
8522
edcc5120
TT
8523static void
8524catch_load_command_1 (char *arg, int from_tty,
8525 struct cmd_list_element *command)
8526{
8527 catch_load_or_unload (arg, from_tty, 1, command);
8528}
8529
8530static void
8531catch_unload_command_1 (char *arg, int from_tty,
8532 struct cmd_list_element *command)
8533{
8534 catch_load_or_unload (arg, from_tty, 0, command);
8535}
8536
346774a9
PA
8537/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8538 is non-zero, then make the breakpoint temporary. If COND_STRING is
8539 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8540 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8541
ab04a2af 8542void
346774a9
PA
8543init_catchpoint (struct breakpoint *b,
8544 struct gdbarch *gdbarch, int tempflag,
63160a43 8545 const char *cond_string,
c0a91b2b 8546 const struct breakpoint_ops *ops)
c906108c 8547{
c5aa993b 8548 struct symtab_and_line sal;
346774a9 8549
fe39c653 8550 init_sal (&sal);
6c95b8df 8551 sal.pspace = current_program_space;
c5aa993b 8552
28010a5d 8553 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8554
1b36a34b 8555 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8556 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8557}
8558
28010a5d 8559void
3ea46bff 8560install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8561{
8562 add_to_breakpoint_chain (b);
3a5c3e22 8563 set_breakpoint_number (internal, b);
558a9d82
YQ
8564 if (is_tracepoint (b))
8565 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8566 if (!internal)
8567 mention (b);
c56053d2 8568 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8569
8570 if (update_gll)
44702360 8571 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8572}
8573
9b70b993 8574static void
a6d9a66e 8575create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8576 int tempflag, const char *cond_string,
c0a91b2b 8577 const struct breakpoint_ops *ops)
c906108c 8578{
4d01a485 8579 struct fork_catchpoint *c = new fork_catchpoint ();
ce78b96d 8580
c1fc2657 8581 init_catchpoint (c, gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8582
8583 c->forked_inferior_pid = null_ptid;
8584
c1fc2657 8585 install_breakpoint (0, c, 1);
c906108c
SS
8586}
8587
fe798b75
JB
8588/* Exec catchpoints. */
8589
b4d90040 8590/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8591 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8592 CATCH_EXEC_BREAKPOINT_OPS. */
8593
c1fc2657 8594struct exec_catchpoint : public breakpoint
b4d90040 8595{
c1fc2657 8596 ~exec_catchpoint () override;
b4d90040
PA
8597
8598 /* Filename of a program whose exec triggered this catchpoint.
8599 This field is only valid immediately after this catchpoint has
8600 triggered. */
8601 char *exec_pathname;
8602};
8603
c1fc2657 8604/* Exec catchpoint destructor. */
b4d90040 8605
c1fc2657 8606exec_catchpoint::~exec_catchpoint ()
b4d90040 8607{
c1fc2657 8608 xfree (this->exec_pathname);
b4d90040
PA
8609}
8610
77b06cd7
TJB
8611static int
8612insert_catch_exec (struct bp_location *bl)
c906108c 8613{
dfd4cc63 8614 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8615}
c906108c 8616
fe798b75 8617static int
73971819 8618remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8619{
dfd4cc63 8620 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8621}
c906108c 8622
fe798b75 8623static int
f1310107 8624breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8625 struct address_space *aspace, CORE_ADDR bp_addr,
8626 const struct target_waitstatus *ws)
fe798b75 8627{
b4d90040
PA
8628 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8629
f90263c1
TT
8630 if (ws->kind != TARGET_WAITKIND_EXECD)
8631 return 0;
8632
8633 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8634 return 1;
fe798b75 8635}
c906108c 8636
fe798b75 8637static enum print_stop_action
348d480f 8638print_it_catch_exec (bpstat bs)
fe798b75 8639{
36dfb11c 8640 struct ui_out *uiout = current_uiout;
348d480f 8641 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8642 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8643
fe798b75 8644 annotate_catchpoint (b->number);
f303dbd6 8645 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8646 if (b->disposition == disp_del)
112e8700 8647 uiout->text ("Temporary catchpoint ");
36dfb11c 8648 else
112e8700
SM
8649 uiout->text ("Catchpoint ");
8650 if (uiout->is_mi_like_p ())
36dfb11c 8651 {
112e8700
SM
8652 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8653 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8654 }
112e8700
SM
8655 uiout->field_int ("bkptno", b->number);
8656 uiout->text (" (exec'd ");
8657 uiout->field_string ("new-exec", c->exec_pathname);
8658 uiout->text ("), ");
36dfb11c 8659
fe798b75 8660 return PRINT_SRC_AND_LOC;
c906108c
SS
8661}
8662
fe798b75 8663static void
a6d9a66e 8664print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8665{
b4d90040 8666 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8667 struct value_print_options opts;
79a45e25 8668 struct ui_out *uiout = current_uiout;
fe798b75
JB
8669
8670 get_user_print_options (&opts);
8671
8672 /* Field 4, the address, is omitted (which makes the columns
8673 not line up too nicely with the headers, but the effect
8674 is relatively readable). */
8675 if (opts.addressprint)
112e8700 8676 uiout->field_skip ("addr");
fe798b75 8677 annotate_field (5);
112e8700 8678 uiout->text ("exec");
b4d90040 8679 if (c->exec_pathname != NULL)
fe798b75 8680 {
112e8700
SM
8681 uiout->text (", program \"");
8682 uiout->field_string ("what", c->exec_pathname);
8683 uiout->text ("\" ");
fe798b75 8684 }
8ac3646f 8685
112e8700
SM
8686 if (uiout->is_mi_like_p ())
8687 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8688}
8689
8690static void
8691print_mention_catch_exec (struct breakpoint *b)
8692{
8693 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8694}
8695
6149aea9
PA
8696/* Implement the "print_recreate" breakpoint_ops method for exec
8697 catchpoints. */
8698
8699static void
8700print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8701{
8702 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8703 print_recreate_thread (b, fp);
6149aea9
PA
8704}
8705
2060206e 8706static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8707
c906108c 8708static int
fba45db2 8709hw_breakpoint_used_count (void)
c906108c 8710{
c906108c 8711 int i = 0;
f1310107
TJB
8712 struct breakpoint *b;
8713 struct bp_location *bl;
c906108c
SS
8714
8715 ALL_BREAKPOINTS (b)
c5aa993b 8716 {
d6b74ac4 8717 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8718 for (bl = b->loc; bl; bl = bl->next)
8719 {
8720 /* Special types of hardware breakpoints may use more than
8721 one register. */
348d480f 8722 i += b->ops->resources_needed (bl);
f1310107 8723 }
c5aa993b 8724 }
c906108c
SS
8725
8726 return i;
8727}
8728
a1398e0c
PA
8729/* Returns the resources B would use if it were a hardware
8730 watchpoint. */
8731
c906108c 8732static int
a1398e0c 8733hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8734{
c906108c 8735 int i = 0;
e09342b5 8736 struct bp_location *bl;
c906108c 8737
a1398e0c
PA
8738 if (!breakpoint_enabled (b))
8739 return 0;
8740
8741 for (bl = b->loc; bl; bl = bl->next)
8742 {
8743 /* Special types of hardware watchpoints may use more than
8744 one register. */
8745 i += b->ops->resources_needed (bl);
8746 }
8747
8748 return i;
8749}
8750
8751/* Returns the sum the used resources of all hardware watchpoints of
8752 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8753 the sum of the used resources of all hardware watchpoints of other
8754 types _not_ TYPE. */
8755
8756static int
8757hw_watchpoint_used_count_others (struct breakpoint *except,
8758 enum bptype type, int *other_type_used)
8759{
8760 int i = 0;
8761 struct breakpoint *b;
8762
c906108c
SS
8763 *other_type_used = 0;
8764 ALL_BREAKPOINTS (b)
e09342b5 8765 {
a1398e0c
PA
8766 if (b == except)
8767 continue;
e09342b5
TJB
8768 if (!breakpoint_enabled (b))
8769 continue;
8770
a1398e0c
PA
8771 if (b->type == type)
8772 i += hw_watchpoint_use_count (b);
8773 else if (is_hardware_watchpoint (b))
8774 *other_type_used = 1;
e09342b5
TJB
8775 }
8776
c906108c
SS
8777 return i;
8778}
8779
c906108c 8780void
fba45db2 8781disable_watchpoints_before_interactive_call_start (void)
c906108c 8782{
c5aa993b 8783 struct breakpoint *b;
c906108c
SS
8784
8785 ALL_BREAKPOINTS (b)
c5aa993b 8786 {
cc60f2e3 8787 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8788 {
b5de0fa7 8789 b->enable_state = bp_call_disabled;
44702360 8790 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8791 }
8792 }
c906108c
SS
8793}
8794
8795void
fba45db2 8796enable_watchpoints_after_interactive_call_stop (void)
c906108c 8797{
c5aa993b 8798 struct breakpoint *b;
c906108c
SS
8799
8800 ALL_BREAKPOINTS (b)
c5aa993b 8801 {
cc60f2e3 8802 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8803 {
b5de0fa7 8804 b->enable_state = bp_enabled;
44702360 8805 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8806 }
8807 }
c906108c
SS
8808}
8809
8bea4e01
UW
8810void
8811disable_breakpoints_before_startup (void)
8812{
6c95b8df 8813 current_program_space->executing_startup = 1;
44702360 8814 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8815}
8816
8817void
8818enable_breakpoints_after_startup (void)
8819{
6c95b8df 8820 current_program_space->executing_startup = 0;
f8eba3c6 8821 breakpoint_re_set ();
8bea4e01
UW
8822}
8823
7c16b83e
PA
8824/* Create a new single-step breakpoint for thread THREAD, with no
8825 locations. */
c906108c 8826
7c16b83e
PA
8827static struct breakpoint *
8828new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8829{
4d01a485 8830 struct breakpoint *b = new breakpoint ();
7c16b83e
PA
8831
8832 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8833 &momentary_breakpoint_ops);
8834
8835 b->disposition = disp_donttouch;
8836 b->frame_id = null_frame_id;
8837
8838 b->thread = thread;
8839 gdb_assert (b->thread != 0);
8840
8841 add_to_breakpoint_chain (b);
8842
8843 return b;
8844}
8845
8846/* Set a momentary breakpoint of type TYPE at address specified by
8847 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8848 frame. */
c906108c
SS
8849
8850struct breakpoint *
a6d9a66e
UW
8851set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8852 struct frame_id frame_id, enum bptype type)
c906108c 8853{
52f0bd74 8854 struct breakpoint *b;
edb3359d 8855
193facb3
JK
8856 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8857 tail-called one. */
8858 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8859
06edf0c0 8860 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8861 b->enable_state = bp_enabled;
8862 b->disposition = disp_donttouch;
818dd999 8863 b->frame_id = frame_id;
c906108c 8864
4a64f543
MS
8865 /* If we're debugging a multi-threaded program, then we want
8866 momentary breakpoints to be active in only a single thread of
8867 control. */
39f77062 8868 if (in_thread_list (inferior_ptid))
5d5658a1 8869 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8870
44702360 8871 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8872
c906108c
SS
8873 return b;
8874}
611c83ae 8875
06edf0c0 8876/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8877 The new breakpoint will have type TYPE, use OPS as its
8878 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8879
06edf0c0
PA
8880static struct breakpoint *
8881momentary_breakpoint_from_master (struct breakpoint *orig,
8882 enum bptype type,
a1aa2221
LM
8883 const struct breakpoint_ops *ops,
8884 int loc_enabled)
e58b0e63
PA
8885{
8886 struct breakpoint *copy;
8887
06edf0c0 8888 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8889 copy->loc = allocate_bp_location (copy);
0e30163f 8890 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8891
a6d9a66e 8892 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8893 copy->loc->requested_address = orig->loc->requested_address;
8894 copy->loc->address = orig->loc->address;
8895 copy->loc->section = orig->loc->section;
6c95b8df 8896 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8897 copy->loc->probe = orig->loc->probe;
f8eba3c6 8898 copy->loc->line_number = orig->loc->line_number;
2f202fde 8899 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8900 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8901 copy->frame_id = orig->frame_id;
8902 copy->thread = orig->thread;
6c95b8df 8903 copy->pspace = orig->pspace;
e58b0e63
PA
8904
8905 copy->enable_state = bp_enabled;
8906 copy->disposition = disp_donttouch;
8907 copy->number = internal_breakpoint_number--;
8908
44702360 8909 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8910 return copy;
8911}
8912
06edf0c0
PA
8913/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8914 ORIG is NULL. */
8915
8916struct breakpoint *
8917clone_momentary_breakpoint (struct breakpoint *orig)
8918{
8919 /* If there's nothing to clone, then return nothing. */
8920 if (orig == NULL)
8921 return NULL;
8922
a1aa2221 8923 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8924}
8925
611c83ae 8926struct breakpoint *
a6d9a66e
UW
8927set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8928 enum bptype type)
611c83ae
PA
8929{
8930 struct symtab_and_line sal;
8931
8932 sal = find_pc_line (pc, 0);
8933 sal.pc = pc;
8934 sal.section = find_pc_overlay (pc);
8935 sal.explicit_pc = 1;
8936
a6d9a66e 8937 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8938}
c906108c 8939\f
c5aa993b 8940
c906108c
SS
8941/* Tell the user we have just set a breakpoint B. */
8942
8943static void
fba45db2 8944mention (struct breakpoint *b)
c906108c 8945{
348d480f 8946 b->ops->print_mention (b);
112e8700 8947 if (current_uiout->is_mi_like_p ())
fb40c209 8948 return;
c906108c
SS
8949 printf_filtered ("\n");
8950}
c906108c 8951\f
c5aa993b 8952
1a853c52
PA
8953static int bp_loc_is_permanent (struct bp_location *loc);
8954
0d381245 8955static struct bp_location *
39d61571 8956add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8957 const struct symtab_and_line *sal)
8958{
8959 struct bp_location *loc, **tmp;
3742cc8b
YQ
8960 CORE_ADDR adjusted_address;
8961 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8962
8963 if (loc_gdbarch == NULL)
8964 loc_gdbarch = b->gdbarch;
8965
8966 /* Adjust the breakpoint's address prior to allocating a location.
8967 Once we call allocate_bp_location(), that mostly uninitialized
8968 location will be placed on the location chain. Adjustment of the
8969 breakpoint may cause target_read_memory() to be called and we do
8970 not want its scan of the location chain to find a breakpoint and
8971 location that's only been partially initialized. */
8972 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8973 sal->pc, b->type);
0d381245 8974
d30113d4 8975 /* Sort the locations by their ADDRESS. */
39d61571 8976 loc = allocate_bp_location (b);
d30113d4
JK
8977 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8978 tmp = &((*tmp)->next))
0d381245 8979 ;
d30113d4 8980 loc->next = *tmp;
0d381245 8981 *tmp = loc;
3742cc8b 8982
0d381245 8983 loc->requested_address = sal->pc;
3742cc8b 8984 loc->address = adjusted_address;
6c95b8df 8985 loc->pspace = sal->pspace;
729662a5
TT
8986 loc->probe.probe = sal->probe;
8987 loc->probe.objfile = sal->objfile;
6c95b8df 8988 gdb_assert (loc->pspace != NULL);
0d381245 8989 loc->section = sal->section;
3742cc8b 8990 loc->gdbarch = loc_gdbarch;
f8eba3c6 8991 loc->line_number = sal->line;
2f202fde 8992 loc->symtab = sal->symtab;
f8eba3c6 8993
0e30163f
JK
8994 set_breakpoint_location_function (loc,
8995 sal->explicit_pc || sal->explicit_line);
1a853c52 8996
6ae88661
LM
8997 /* While by definition, permanent breakpoints are already present in the
8998 code, we don't mark the location as inserted. Normally one would expect
8999 that GDB could rely on that breakpoint instruction to stop the program,
9000 thus removing the need to insert its own breakpoint, except that executing
9001 the breakpoint instruction can kill the target instead of reporting a
9002 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9003 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9004 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9005 breakpoint be inserted normally results in QEMU knowing about the GDB
9006 breakpoint, and thus trap before the breakpoint instruction is executed.
9007 (If GDB later needs to continue execution past the permanent breakpoint,
9008 it manually increments the PC, thus avoiding executing the breakpoint
9009 instruction.) */
1a853c52 9010 if (bp_loc_is_permanent (loc))
6ae88661 9011 loc->permanent = 1;
1a853c52 9012
0d381245
VP
9013 return loc;
9014}
514f746b
AR
9015\f
9016
1cf4d951 9017/* See breakpoint.h. */
514f746b 9018
1cf4d951
PA
9019int
9020program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9021{
9022 int len;
9023 CORE_ADDR addr;
1afeeb75 9024 const gdb_byte *bpoint;
514f746b 9025 gdb_byte *target_mem;
939c61fa
JK
9026 struct cleanup *cleanup;
9027 int retval = 0;
514f746b 9028
1cf4d951
PA
9029 addr = address;
9030 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9031
9032 /* Software breakpoints unsupported? */
9033 if (bpoint == NULL)
9034 return 0;
9035
224c3ddb 9036 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9037
9038 /* Enable the automatic memory restoration from breakpoints while
9039 we read the memory. Otherwise we could say about our temporary
9040 breakpoints they are permanent. */
9041 cleanup = make_show_memory_breakpoints_cleanup (0);
9042
9043 if (target_read_memory (address, target_mem, len) == 0
9044 && memcmp (target_mem, bpoint, len) == 0)
9045 retval = 1;
9046
9047 do_cleanups (cleanup);
9048
9049 return retval;
9050}
9051
9052/* Return 1 if LOC is pointing to a permanent breakpoint,
9053 return 0 otherwise. */
9054
9055static int
9056bp_loc_is_permanent (struct bp_location *loc)
9057{
514f746b
AR
9058 gdb_assert (loc != NULL);
9059
244558af
LM
9060 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9061 attempt to read from the addresses the locations of these breakpoint types
9062 point to. program_breakpoint_here_p, below, will attempt to read
9063 memory. */
9064 if (!breakpoint_address_is_meaningful (loc->owner))
9065 return 0;
9066
5ed8105e 9067 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9068 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 9069 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
9070}
9071
e7e0cddf
SS
9072/* Build a command list for the dprintf corresponding to the current
9073 settings of the dprintf style options. */
9074
9075static void
9076update_dprintf_command_list (struct breakpoint *b)
9077{
9078 char *dprintf_args = b->extra_string;
9079 char *printf_line = NULL;
9080
9081 if (!dprintf_args)
9082 return;
9083
9084 dprintf_args = skip_spaces (dprintf_args);
9085
9086 /* Allow a comma, as it may have terminated a location, but don't
9087 insist on it. */
9088 if (*dprintf_args == ',')
9089 ++dprintf_args;
9090 dprintf_args = skip_spaces (dprintf_args);
9091
9092 if (*dprintf_args != '"')
9093 error (_("Bad format string, missing '\"'."));
9094
d3ce09f5 9095 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9096 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9097 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9098 {
9099 if (!dprintf_function)
9100 error (_("No function supplied for dprintf call"));
9101
9102 if (dprintf_channel && strlen (dprintf_channel) > 0)
9103 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9104 dprintf_function,
9105 dprintf_channel,
9106 dprintf_args);
9107 else
9108 printf_line = xstrprintf ("call (void) %s (%s)",
9109 dprintf_function,
9110 dprintf_args);
9111 }
d3ce09f5
SS
9112 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9113 {
9114 if (target_can_run_breakpoint_commands ())
9115 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9116 else
9117 {
9118 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9119 printf_line = xstrprintf ("printf %s", dprintf_args);
9120 }
9121 }
e7e0cddf
SS
9122 else
9123 internal_error (__FILE__, __LINE__,
9124 _("Invalid dprintf style."));
9125
f28045c2 9126 gdb_assert (printf_line != NULL);
9d6e6e84 9127 /* Manufacture a printf sequence. */
f28045c2 9128 {
8d749320 9129 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9130
f28045c2
YQ
9131 printf_cmd_line->control_type = simple_control;
9132 printf_cmd_line->body_count = 0;
9133 printf_cmd_line->body_list = NULL;
9d6e6e84 9134 printf_cmd_line->next = NULL;
f28045c2 9135 printf_cmd_line->line = printf_line;
e7e0cddf 9136
93921405 9137 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9138 }
e7e0cddf
SS
9139}
9140
9141/* Update all dprintf commands, making their command lists reflect
9142 current style settings. */
9143
9144static void
9145update_dprintf_commands (char *args, int from_tty,
9146 struct cmd_list_element *c)
9147{
9148 struct breakpoint *b;
9149
9150 ALL_BREAKPOINTS (b)
9151 {
9152 if (b->type == bp_dprintf)
9153 update_dprintf_command_list (b);
9154 }
9155}
c3f6f71d 9156
f00aae0f
KS
9157/* Create a breakpoint with SAL as location. Use LOCATION
9158 as a description of the location, and COND_STRING
b35a8b2f
DE
9159 as condition expression. If LOCATION is NULL then create an
9160 "address location" from the address in the SAL. */
018d34a4
VP
9161
9162static void
d9b3f62e 9163init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9164 struct symtabs_and_lines sals,
ffc2605c 9165 event_location_up &&location,
e1e01040
PA
9166 gdb::unique_xmalloc_ptr<char> filter,
9167 gdb::unique_xmalloc_ptr<char> cond_string,
9168 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9169 enum bptype type, enum bpdisp disposition,
9170 int thread, int task, int ignore_count,
c0a91b2b 9171 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9172 int enabled, int internal, unsigned flags,
9173 int display_canonical)
018d34a4 9174{
0d381245 9175 int i;
018d34a4
VP
9176
9177 if (type == bp_hardware_breakpoint)
9178 {
fbbd034e
AS
9179 int target_resources_ok;
9180
9181 i = hw_breakpoint_used_count ();
9182 target_resources_ok =
9183 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9184 i + 1, 0);
9185 if (target_resources_ok == 0)
9186 error (_("No hardware breakpoint support in the target."));
9187 else if (target_resources_ok < 0)
9188 error (_("Hardware breakpoints used exceeds limit."));
9189 }
9190
6c95b8df
PA
9191 gdb_assert (sals.nelts > 0);
9192
0d381245
VP
9193 for (i = 0; i < sals.nelts; ++i)
9194 {
9195 struct symtab_and_line sal = sals.sals[i];
9196 struct bp_location *loc;
9197
9198 if (from_tty)
5af949e3
UW
9199 {
9200 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9201 if (!loc_gdbarch)
9202 loc_gdbarch = gdbarch;
9203
9204 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9205 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9206 }
0d381245
VP
9207
9208 if (i == 0)
9209 {
d9b3f62e 9210 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9211 b->thread = thread;
4a306c9a 9212 b->task = task;
855a6e68 9213
e1e01040
PA
9214 b->cond_string = cond_string.release ();
9215 b->extra_string = extra_string.release ();
0d381245 9216 b->ignore_count = ignore_count;
41447f92 9217 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9218 b->disposition = disposition;
6c95b8df 9219
44f238bb
PA
9220 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9221 b->loc->inserted = 1;
9222
0fb4aa4b
PA
9223 if (type == bp_static_tracepoint)
9224 {
d9b3f62e 9225 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9226 struct static_tracepoint_marker marker;
9227
983af33b 9228 if (strace_marker_p (b))
0fb4aa4b
PA
9229 {
9230 /* We already know the marker exists, otherwise, we
9231 wouldn't see a sal for it. */
d28cd78a
TT
9232 const char *p
9233 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9234 const char *endp;
0fb4aa4b 9235 char *marker_str;
0fb4aa4b 9236
f00aae0f 9237 p = skip_spaces_const (p);
0fb4aa4b 9238
f00aae0f 9239 endp = skip_to_space_const (p);
0fb4aa4b
PA
9240
9241 marker_str = savestring (p, endp - p);
d9b3f62e 9242 t->static_trace_marker_id = marker_str;
0fb4aa4b 9243
3e43a32a
MS
9244 printf_filtered (_("Probed static tracepoint "
9245 "marker \"%s\"\n"),
d9b3f62e 9246 t->static_trace_marker_id);
0fb4aa4b
PA
9247 }
9248 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9249 {
d9b3f62e 9250 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9251 release_static_tracepoint_marker (&marker);
9252
3e43a32a
MS
9253 printf_filtered (_("Probed static tracepoint "
9254 "marker \"%s\"\n"),
d9b3f62e 9255 t->static_trace_marker_id);
0fb4aa4b
PA
9256 }
9257 else
3e43a32a
MS
9258 warning (_("Couldn't determine the static "
9259 "tracepoint marker to probe"));
0fb4aa4b
PA
9260 }
9261
0d381245
VP
9262 loc = b->loc;
9263 }
9264 else
018d34a4 9265 {
39d61571 9266 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9267 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9268 loc->inserted = 1;
0d381245
VP
9269 }
9270
9271 if (b->cond_string)
9272 {
bbc13ae3
KS
9273 const char *arg = b->cond_string;
9274
1bb9788d
TT
9275 loc->cond = parse_exp_1 (&arg, loc->address,
9276 block_for_pc (loc->address), 0);
0d381245 9277 if (*arg)
588ae58c 9278 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9279 }
e7e0cddf
SS
9280
9281 /* Dynamic printf requires and uses additional arguments on the
9282 command line, otherwise it's an error. */
9283 if (type == bp_dprintf)
9284 {
9285 if (b->extra_string)
9286 update_dprintf_command_list (b);
9287 else
9288 error (_("Format string required"));
9289 }
9290 else if (b->extra_string)
588ae58c 9291 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9292 }
018d34a4 9293
56435ebe 9294 b->display_canonical = display_canonical;
f00aae0f 9295 if (location != NULL)
d28cd78a 9296 b->location = std::move (location);
018d34a4 9297 else
d28cd78a 9298 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9299 b->filter = filter.release ();
d9b3f62e 9300}
018d34a4 9301
d9b3f62e
PA
9302static void
9303create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9304 struct symtabs_and_lines sals,
ffc2605c 9305 event_location_up &&location,
e1e01040
PA
9306 gdb::unique_xmalloc_ptr<char> filter,
9307 gdb::unique_xmalloc_ptr<char> cond_string,
9308 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9309 enum bptype type, enum bpdisp disposition,
9310 int thread, int task, int ignore_count,
c0a91b2b 9311 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9312 int enabled, int internal, unsigned flags,
9313 int display_canonical)
d9b3f62e 9314{
a5e364af 9315 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9316
a5e364af 9317 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9318 sals, std::move (location),
e1e01040
PA
9319 std::move (filter),
9320 std::move (cond_string),
9321 std::move (extra_string),
d9b3f62e
PA
9322 type, disposition,
9323 thread, task, ignore_count,
9324 ops, from_tty,
44f238bb
PA
9325 enabled, internal, flags,
9326 display_canonical);
d9b3f62e 9327
a5e364af 9328 install_breakpoint (internal, b.release (), 0);
018d34a4
VP
9329}
9330
9331/* Add SALS.nelts breakpoints to the breakpoint table. For each
9332 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9333 value. COND_STRING, if not NULL, specified the condition to be
9334 used for all breakpoints. Essentially the only case where
9335 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9336 function. In that case, it's still not possible to specify
9337 separate conditions for different overloaded functions, so
9338 we take just a single condition string.
9339
c3f6f71d 9340 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9341 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9342 array contents). If the function fails (error() is called), the
9343 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9344 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9345
9346static void
8cdf0e15 9347create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9348 struct linespec_result *canonical,
e1e01040
PA
9349 gdb::unique_xmalloc_ptr<char> cond_string,
9350 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9351 enum bptype type, enum bpdisp disposition,
9352 int thread, int task, int ignore_count,
c0a91b2b 9353 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9354 int enabled, int internal, unsigned flags)
c906108c 9355{
018d34a4 9356 int i;
f8eba3c6 9357 struct linespec_sals *lsal;
cc59ec59 9358
f8eba3c6
TT
9359 if (canonical->pre_expanded)
9360 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9361
9362 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9363 {
f00aae0f 9364 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9365 'break', without arguments. */
ffc2605c 9366 event_location_up location
f00aae0f 9367 = (canonical->location != NULL
8e9e35b1 9368 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040
PA
9369 gdb::unique_xmalloc_ptr<char> filter_string
9370 (lsal->canonical != NULL ? xstrdup (lsal->canonical) : NULL);
0d381245 9371
f8eba3c6 9372 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9373 std::move (location),
e1e01040
PA
9374 std::move (filter_string),
9375 std::move (cond_string),
9376 std::move (extra_string),
e7e0cddf 9377 type, disposition,
84f4c1fe 9378 thread, task, ignore_count, ops,
44f238bb 9379 from_tty, enabled, internal, flags,
56435ebe 9380 canonical->special_display);
c3f6f71d 9381 }
c3f6f71d 9382}
c906108c 9383
f00aae0f 9384/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9385 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9386 addresses found. LOCATION points to the end of the SAL (for
9387 linespec locations).
9998af43
TJB
9388
9389 The array and the line spec strings are allocated on the heap, it is
9390 the caller's responsibility to free them. */
c906108c 9391
b9362cc7 9392static void
f00aae0f 9393parse_breakpoint_sals (const struct event_location *location,
58438ac1 9394 struct linespec_result *canonical)
c3f6f71d 9395{
f00aae0f
KS
9396 struct symtab_and_line cursal;
9397
9398 if (event_location_type (location) == LINESPEC_LOCATION)
9399 {
9400 const char *address = get_linespec_location (location);
9401
9402 if (address == NULL)
9403 {
9404 /* The last displayed codepoint, if it's valid, is our default
9405 breakpoint address. */
9406 if (last_displayed_sal_is_valid ())
9407 {
9408 struct linespec_sals lsal;
9409 struct symtab_and_line sal;
9410 CORE_ADDR pc;
9411
9412 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9413 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9414
9415 /* Set sal's pspace, pc, symtab, and line to the values
9416 corresponding to the last call to print_frame_info.
9417 Be sure to reinitialize LINE with NOTCURRENT == 0
9418 as the breakpoint line number is inappropriate otherwise.
9419 find_pc_line would adjust PC, re-set it back. */
9420 get_last_displayed_sal (&sal);
9421 pc = sal.pc;
9422 sal = find_pc_line (pc, 0);
9423
9424 /* "break" without arguments is equivalent to "break *PC"
9425 where PC is the last displayed codepoint's address. So
9426 make sure to set sal.explicit_pc to prevent GDB from
9427 trying to expand the list of sals to include all other
9428 instances with the same symtab and line. */
9429 sal.pc = pc;
9430 sal.explicit_pc = 1;
9431
9432 lsal.sals.sals[0] = sal;
9433 lsal.sals.nelts = 1;
9434 lsal.canonical = NULL;
9435
9436 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9437 return;
9438 }
9439 else
9440 error (_("No default breakpoint address now."));
c906108c 9441 }
c906108c 9442 }
f00aae0f
KS
9443
9444 /* Force almost all breakpoints to be in terms of the
9445 current_source_symtab (which is decode_line_1's default).
9446 This should produce the results we want almost all of the
9447 time while leaving default_breakpoint_* alone.
9448
9449 ObjC: However, don't match an Objective-C method name which
9450 may have a '+' or '-' succeeded by a '['. */
9451 cursal = get_current_source_symtab_and_line ();
9452 if (last_displayed_sal_is_valid ())
c906108c 9453 {
f00aae0f 9454 const char *address = NULL;
cc80f267 9455
f00aae0f
KS
9456 if (event_location_type (location) == LINESPEC_LOCATION)
9457 address = get_linespec_location (location);
cc80f267 9458
f00aae0f
KS
9459 if (!cursal.symtab
9460 || (address != NULL
9461 && strchr ("+-", address[0]) != NULL
9462 && address[1] != '['))
9463 {
c2f4122d 9464 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9465 get_last_displayed_symtab (),
9466 get_last_displayed_line (),
9467 canonical, NULL, NULL);
9468 return;
9469 }
c906108c 9470 }
f00aae0f 9471
c2f4122d 9472 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9473 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9474}
c906108c 9475
c906108c 9476
c3f6f71d 9477/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9478 inserted as a breakpoint. If it can't throw an error. */
c906108c 9479
b9362cc7 9480static void
23e7acfb 9481breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9482{
9483 int i;
cc59ec59 9484
c3f6f71d 9485 for (i = 0; i < sals->nelts; i++)
ee53e872 9486 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9487}
9488
7a697b8d
SS
9489/* Fast tracepoints may have restrictions on valid locations. For
9490 instance, a fast tracepoint using a jump instead of a trap will
9491 likely have to overwrite more bytes than a trap would, and so can
9492 only be placed where the instruction is longer than the jump, or a
9493 multi-instruction sequence does not have a jump into the middle of
9494 it, etc. */
9495
9496static void
9497check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9498 struct symtabs_and_lines *sals)
9499{
9500 int i, rslt;
9501 struct symtab_and_line *sal;
9502 char *msg;
9503 struct cleanup *old_chain;
9504
9505 for (i = 0; i < sals->nelts; i++)
9506 {
f8eba3c6
TT
9507 struct gdbarch *sarch;
9508
7a697b8d
SS
9509 sal = &sals->sals[i];
9510
f8eba3c6
TT
9511 sarch = get_sal_arch (*sal);
9512 /* We fall back to GDBARCH if there is no architecture
9513 associated with SAL. */
9514 if (sarch == NULL)
9515 sarch = gdbarch;
6b940e6a 9516 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9517 old_chain = make_cleanup (xfree, msg);
9518
9519 if (!rslt)
53c3572a 9520 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9521 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9522
9523 do_cleanups (old_chain);
9524 }
9525}
9526
018d34a4
VP
9527/* Given TOK, a string specification of condition and thread, as
9528 accepted by the 'break' command, extract the condition
9529 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9530 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9531 If no condition is found, *COND_STRING is set to NULL.
9532 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9533
9534static void
bbc13ae3 9535find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9536 char **cond_string, int *thread, int *task,
9537 char **rest)
018d34a4
VP
9538{
9539 *cond_string = NULL;
9540 *thread = -1;
ed1d1739
KS
9541 *task = 0;
9542 *rest = NULL;
9543
018d34a4
VP
9544 while (tok && *tok)
9545 {
bbc13ae3 9546 const char *end_tok;
018d34a4 9547 int toklen;
bbc13ae3
KS
9548 const char *cond_start = NULL;
9549 const char *cond_end = NULL;
cc59ec59 9550
bbc13ae3 9551 tok = skip_spaces_const (tok);
e7e0cddf
SS
9552
9553 if ((*tok == '"' || *tok == ',') && rest)
9554 {
9555 *rest = savestring (tok, strlen (tok));
9556 return;
9557 }
9558
bbc13ae3 9559 end_tok = skip_to_space_const (tok);
d634f2de 9560
018d34a4 9561 toklen = end_tok - tok;
d634f2de 9562
018d34a4
VP
9563 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9564 {
9565 tok = cond_start = end_tok + 1;
4d01a485 9566 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9567 cond_end = tok;
d634f2de 9568 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9569 }
9570 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9571 {
5d5658a1
PA
9572 const char *tmptok;
9573 struct thread_info *thr;
d634f2de 9574
018d34a4 9575 tok = end_tok + 1;
5d5658a1 9576 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9577 if (tok == tmptok)
9578 error (_("Junk after thread keyword."));
5d5658a1 9579 *thread = thr->global_num;
bbc13ae3 9580 tok = tmptok;
018d34a4 9581 }
4a306c9a
JB
9582 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9583 {
9584 char *tmptok;
9585
9586 tok = end_tok + 1;
bbc13ae3 9587 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9588 if (tok == tmptok)
9589 error (_("Junk after task keyword."));
9590 if (!valid_task_id (*task))
b6199126 9591 error (_("Unknown task %d."), *task);
bbc13ae3 9592 tok = tmptok;
4a306c9a 9593 }
e7e0cddf
SS
9594 else if (rest)
9595 {
9596 *rest = savestring (tok, strlen (tok));
ccab2054 9597 return;
e7e0cddf 9598 }
018d34a4
VP
9599 else
9600 error (_("Junk at end of arguments."));
9601 }
9602}
9603
0fb4aa4b
PA
9604/* Decode a static tracepoint marker spec. */
9605
9606static struct symtabs_and_lines
f00aae0f 9607decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9608{
9609 VEC(static_tracepoint_marker_p) *markers = NULL;
9610 struct symtabs_and_lines sals;
0fb4aa4b 9611 struct cleanup *old_chain;
f00aae0f
KS
9612 const char *p = &(*arg_p)[3];
9613 const char *endp;
0fb4aa4b
PA
9614 char *marker_str;
9615 int i;
9616
f00aae0f 9617 p = skip_spaces_const (p);
0fb4aa4b 9618
f00aae0f 9619 endp = skip_to_space_const (p);
0fb4aa4b
PA
9620
9621 marker_str = savestring (p, endp - p);
9622 old_chain = make_cleanup (xfree, marker_str);
9623
9624 markers = target_static_tracepoint_markers_by_strid (marker_str);
9625 if (VEC_empty(static_tracepoint_marker_p, markers))
9626 error (_("No known static tracepoint marker named %s"), marker_str);
9627
9628 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9629 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9630
9631 for (i = 0; i < sals.nelts; i++)
9632 {
9633 struct static_tracepoint_marker *marker;
9634
9635 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9636
9637 init_sal (&sals.sals[i]);
9638
9639 sals.sals[i] = find_pc_line (marker->address, 0);
9640 sals.sals[i].pc = marker->address;
9641
9642 release_static_tracepoint_marker (marker);
9643 }
9644
9645 do_cleanups (old_chain);
9646
9647 *arg_p = endp;
9648 return sals;
9649}
9650
f00aae0f 9651/* See breakpoint.h. */
0101ce28 9652
8cdf0e15
VP
9653int
9654create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9655 const struct event_location *location,
9656 const char *cond_string,
9657 int thread, const char *extra_string,
f00aae0f 9658 int parse_extra,
0fb4aa4b 9659 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9660 int ignore_count,
9661 enum auto_boolean pending_break_support,
c0a91b2b 9662 const struct breakpoint_ops *ops,
44f238bb
PA
9663 int from_tty, int enabled, int internal,
9664 unsigned flags)
c3f6f71d 9665{
7efd8fc2 9666 struct linespec_result canonical;
80c99de1 9667 struct cleanup *bkpt_chain = NULL;
0101ce28 9668 int pending = 0;
4a306c9a 9669 int task = 0;
86b17b60 9670 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9671
348d480f
PA
9672 gdb_assert (ops != NULL);
9673
f00aae0f
KS
9674 /* If extra_string isn't useful, set it to NULL. */
9675 if (extra_string != NULL && *extra_string == '\0')
9676 extra_string = NULL;
9677
492d29ea 9678 TRY
b78a6381 9679 {
f00aae0f 9680 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9681 }
492d29ea 9682 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9683 {
492d29ea
PA
9684 /* If caller is interested in rc value from parse, set
9685 value. */
9686 if (e.error == NOT_FOUND_ERROR)
0101ce28 9687 {
05ff989b
AC
9688 /* If pending breakpoint support is turned off, throw
9689 error. */
fa8d40ab
JJ
9690
9691 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9692 throw_exception (e);
9693
9694 exception_print (gdb_stderr, e);
fa8d40ab 9695
05ff989b
AC
9696 /* If pending breakpoint support is auto query and the user
9697 selects no, then simply return the error code. */
059fb39f 9698 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9699 && !nquery (_("Make %s pending on future shared library load? "),
9700 bptype_string (type_wanted)))
fd9b8c24 9701 return 0;
fa8d40ab 9702
05ff989b
AC
9703 /* At this point, either the user was queried about setting
9704 a pending breakpoint and selected yes, or pending
9705 breakpoint behavior is on and thus a pending breakpoint
9706 is defaulted on behalf of the user. */
f00aae0f 9707 pending = 1;
0101ce28 9708 }
492d29ea
PA
9709 else
9710 throw_exception (e);
0101ce28 9711 }
492d29ea
PA
9712 END_CATCH
9713
f00aae0f 9714 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9715 return 0;
c3f6f71d 9716
c3f6f71d
JM
9717 /* ----------------------------- SNIP -----------------------------
9718 Anything added to the cleanup chain beyond this point is assumed
9719 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9720 then the memory is not reclaimed. */
9721 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9722
c3f6f71d
JM
9723 /* Resolve all line numbers to PC's and verify that the addresses
9724 are ok for the target. */
0101ce28 9725 if (!pending)
f8eba3c6
TT
9726 {
9727 int ix;
9728 struct linespec_sals *iter;
9729
9730 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9731 breakpoint_sals_to_pc (&iter->sals);
9732 }
c3f6f71d 9733
7a697b8d 9734 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9735 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9736 {
9737 int ix;
9738 struct linespec_sals *iter;
9739
9740 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9741 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9742 }
7a697b8d 9743
c3f6f71d
JM
9744 /* Verify that condition can be parsed, before setting any
9745 breakpoints. Allocate a separate condition expression for each
4a64f543 9746 breakpoint. */
0101ce28 9747 if (!pending)
c3f6f71d 9748 {
e1e01040
PA
9749 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9750 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9751
f00aae0f 9752 if (parse_extra)
72b2ff0e 9753 {
0878d0fa 9754 char *rest;
e1e01040 9755 char *cond;
52d361e1
YQ
9756 struct linespec_sals *lsal;
9757
9758 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9759
0878d0fa
YQ
9760 /* Here we only parse 'arg' to separate condition
9761 from thread number, so parsing in context of first
9762 sal is OK. When setting the breakpoint we'll
9763 re-parse it in context of each sal. */
9764
f00aae0f 9765 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
e1e01040
PA
9766 &cond, &thread, &task, &rest);
9767 cond_string_copy.reset (cond);
9768 extra_string_copy.reset (rest);
72b2ff0e 9769 }
2f069f6f 9770 else
72b2ff0e 9771 {
f00aae0f
KS
9772 if (type_wanted != bp_dprintf
9773 && extra_string != NULL && *extra_string != '\0')
9774 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9775
9776 /* Create a private copy of condition string. */
9777 if (cond_string)
e1e01040 9778 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9779 /* Create a private copy of any extra string. */
9780 if (extra_string)
e1e01040 9781 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9782 }
0fb4aa4b 9783
52d361e1 9784 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9785 std::move (cond_string_copy),
9786 std::move (extra_string_copy),
9787 type_wanted,
d9b3f62e
PA
9788 tempflag ? disp_del : disp_donttouch,
9789 thread, task, ignore_count, ops,
44f238bb 9790 from_tty, enabled, internal, flags);
c906108c 9791 }
0101ce28
JJ
9792 else
9793 {
a5e364af 9794 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9795
a5e364af 9796 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9797 b->location = copy_event_location (location);
bfccc43c 9798
f00aae0f
KS
9799 if (parse_extra)
9800 b->cond_string = NULL;
e12c7713
MK
9801 else
9802 {
9803 /* Create a private copy of condition string. */
e1e01040 9804 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9805 b->thread = thread;
e12c7713 9806 }
f00aae0f
KS
9807
9808 /* Create a private copy of any extra string. */
e1e01040 9809 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9810 b->ignore_count = ignore_count;
0101ce28 9811 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9812 b->condition_not_parsed = 1;
41447f92 9813 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9814 if ((type_wanted != bp_breakpoint
9815 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9816 b->pspace = current_program_space;
8bea4e01 9817
a5e364af 9818 install_breakpoint (internal, b.release (), 0);
0101ce28
JJ
9819 }
9820
f8eba3c6 9821 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9822 {
3e43a32a
MS
9823 warning (_("Multiple breakpoints were set.\nUse the "
9824 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9825 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9826 }
9827
80c99de1
PA
9828 /* That's it. Discard the cleanups for data inserted into the
9829 breakpoint. */
9830 discard_cleanups (bkpt_chain);
217dc9e2 9831
80c99de1 9832 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9833 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9834
9835 return 1;
c3f6f71d 9836}
c906108c 9837
348d480f 9838/* Set a breakpoint.
72b2ff0e
VP
9839 ARG is a string describing breakpoint address,
9840 condition, and thread.
9841 FLAG specifies if a breakpoint is hardware on,
9842 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9843 and BP_TEMPFLAG. */
348d480f 9844
98deb0da 9845static void
72b2ff0e 9846break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9847{
72b2ff0e 9848 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9849 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9850 ? bp_hardware_breakpoint
9851 : bp_breakpoint);
55aa24fb 9852 struct breakpoint_ops *ops;
f00aae0f 9853
ffc2605c 9854 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9855
9856 /* Matching breakpoints on probes. */
5b56227b 9857 if (location != NULL
ffc2605c 9858 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9859 ops = &bkpt_probe_breakpoint_ops;
9860 else
9861 ops = &bkpt_breakpoint_ops;
c3f6f71d 9862
8cdf0e15 9863 create_breakpoint (get_current_arch (),
ffc2605c 9864 location.get (),
f00aae0f 9865 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9866 tempflag, type_wanted,
8cdf0e15
VP
9867 0 /* Ignore count */,
9868 pending_break_support,
55aa24fb 9869 ops,
8cdf0e15 9870 from_tty,
84f4c1fe 9871 1 /* enabled */,
44f238bb
PA
9872 0 /* internal */,
9873 0);
c906108c
SS
9874}
9875
c906108c
SS
9876/* Helper function for break_command_1 and disassemble_command. */
9877
9878void
fba45db2 9879resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9880{
9881 CORE_ADDR pc;
9882
9883 if (sal->pc == 0 && sal->symtab != NULL)
9884 {
9885 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9886 error (_("No line %d in file \"%s\"."),
05cba821 9887 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9888 sal->pc = pc;
6a048695 9889
4a64f543
MS
9890 /* If this SAL corresponds to a breakpoint inserted using a line
9891 number, then skip the function prologue if necessary. */
6a048695 9892 if (sal->explicit_line)
059acae7 9893 skip_prologue_sal (sal);
c906108c
SS
9894 }
9895
9896 if (sal->section == 0 && sal->symtab != NULL)
9897 {
346d1dfe 9898 const struct blockvector *bv;
3977b71f 9899 const struct block *b;
c5aa993b 9900 struct symbol *sym;
c906108c 9901
43f3e411
DE
9902 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9903 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9904 if (bv != NULL)
9905 {
7f0df278 9906 sym = block_linkage_function (b);
c906108c
SS
9907 if (sym != NULL)
9908 {
eb822aa6
DE
9909 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9910 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9911 sym);
c906108c
SS
9912 }
9913 else
9914 {
4a64f543
MS
9915 /* It really is worthwhile to have the section, so we'll
9916 just have to look harder. This case can be executed
9917 if we have line numbers but no functions (as can
9918 happen in assembly source). */
c906108c 9919
5ed8105e 9920 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9921 switch_to_program_space_and_thread (sal->pspace);
c906108c 9922
5ed8105e 9923 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9924 if (msym.minsym)
efd66ac6 9925 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9926 }
9927 }
9928 }
9929}
9930
9931void
fba45db2 9932break_command (char *arg, int from_tty)
c906108c 9933{
db107f19 9934 break_command_1 (arg, 0, from_tty);
c906108c
SS
9935}
9936
c906108c 9937void
fba45db2 9938tbreak_command (char *arg, int from_tty)
c906108c 9939{
db107f19 9940 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9941}
9942
c906108c 9943static void
fba45db2 9944hbreak_command (char *arg, int from_tty)
c906108c 9945{
db107f19 9946 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9947}
9948
9949static void
fba45db2 9950thbreak_command (char *arg, int from_tty)
c906108c 9951{
db107f19 9952 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9953}
9954
9955static void
fba45db2 9956stop_command (char *arg, int from_tty)
c906108c 9957{
a3f17187 9958 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9959Usage: stop in <function | address>\n\
a3f17187 9960 stop at <line>\n"));
c906108c
SS
9961}
9962
9963static void
fba45db2 9964stopin_command (char *arg, int from_tty)
c906108c
SS
9965{
9966 int badInput = 0;
9967
c5aa993b 9968 if (arg == (char *) NULL)
c906108c
SS
9969 badInput = 1;
9970 else if (*arg != '*')
9971 {
9972 char *argptr = arg;
9973 int hasColon = 0;
9974
4a64f543 9975 /* Look for a ':'. If this is a line number specification, then
53a5351d 9976 say it is bad, otherwise, it should be an address or
4a64f543 9977 function/method name. */
c906108c 9978 while (*argptr && !hasColon)
c5aa993b
JM
9979 {
9980 hasColon = (*argptr == ':');
9981 argptr++;
9982 }
c906108c
SS
9983
9984 if (hasColon)
c5aa993b 9985 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9986 else
c5aa993b 9987 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9988 }
9989
9990 if (badInput)
a3f17187 9991 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9992 else
db107f19 9993 break_command_1 (arg, 0, from_tty);
c906108c
SS
9994}
9995
9996static void
fba45db2 9997stopat_command (char *arg, int from_tty)
c906108c
SS
9998{
9999 int badInput = 0;
10000
c5aa993b 10001 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10002 badInput = 1;
10003 else
10004 {
10005 char *argptr = arg;
10006 int hasColon = 0;
10007
4a64f543
MS
10008 /* Look for a ':'. If there is a '::' then get out, otherwise
10009 it is probably a line number. */
c906108c 10010 while (*argptr && !hasColon)
c5aa993b
JM
10011 {
10012 hasColon = (*argptr == ':');
10013 argptr++;
10014 }
c906108c
SS
10015
10016 if (hasColon)
c5aa993b 10017 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10018 else
c5aa993b 10019 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10020 }
10021
10022 if (badInput)
a3f17187 10023 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10024 else
db107f19 10025 break_command_1 (arg, 0, from_tty);
c906108c
SS
10026}
10027
e7e0cddf
SS
10028/* The dynamic printf command is mostly like a regular breakpoint, but
10029 with a prewired command list consisting of a single output command,
10030 built from extra arguments supplied on the dprintf command
10031 line. */
10032
da821c7b 10033static void
e7e0cddf
SS
10034dprintf_command (char *arg, int from_tty)
10035{
ffc2605c 10036 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10037
10038 /* If non-NULL, ARG should have been advanced past the location;
10039 the next character must be ','. */
10040 if (arg != NULL)
10041 {
10042 if (arg[0] != ',' || arg[1] == '\0')
10043 error (_("Format string required"));
10044 else
10045 {
10046 /* Skip the comma. */
10047 ++arg;
10048 }
10049 }
10050
e7e0cddf 10051 create_breakpoint (get_current_arch (),
ffc2605c 10052 location.get (),
f00aae0f 10053 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10054 0, bp_dprintf,
10055 0 /* Ignore count */,
10056 pending_break_support,
10057 &dprintf_breakpoint_ops,
10058 from_tty,
10059 1 /* enabled */,
10060 0 /* internal */,
10061 0);
10062}
10063
d3ce09f5
SS
10064static void
10065agent_printf_command (char *arg, int from_tty)
10066{
10067 error (_("May only run agent-printf on the target"));
10068}
10069
f1310107
TJB
10070/* Implement the "breakpoint_hit" breakpoint_ops method for
10071 ranged breakpoints. */
10072
10073static int
10074breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10075 struct address_space *aspace,
09ac7c10
TT
10076 CORE_ADDR bp_addr,
10077 const struct target_waitstatus *ws)
f1310107 10078{
09ac7c10 10079 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10080 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10081 return 0;
10082
f1310107
TJB
10083 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10084 bl->length, aspace, bp_addr);
10085}
10086
10087/* Implement the "resources_needed" breakpoint_ops method for
10088 ranged breakpoints. */
10089
10090static int
10091resources_needed_ranged_breakpoint (const struct bp_location *bl)
10092{
10093 return target_ranged_break_num_registers ();
10094}
10095
10096/* Implement the "print_it" breakpoint_ops method for
10097 ranged breakpoints. */
10098
10099static enum print_stop_action
348d480f 10100print_it_ranged_breakpoint (bpstat bs)
f1310107 10101{
348d480f 10102 struct breakpoint *b = bs->breakpoint_at;
f1310107 10103 struct bp_location *bl = b->loc;
79a45e25 10104 struct ui_out *uiout = current_uiout;
f1310107
TJB
10105
10106 gdb_assert (b->type == bp_hardware_breakpoint);
10107
10108 /* Ranged breakpoints have only one location. */
10109 gdb_assert (bl && bl->next == NULL);
10110
10111 annotate_breakpoint (b->number);
f303dbd6
PA
10112
10113 maybe_print_thread_hit_breakpoint (uiout);
10114
f1310107 10115 if (b->disposition == disp_del)
112e8700 10116 uiout->text ("Temporary ranged breakpoint ");
f1310107 10117 else
112e8700
SM
10118 uiout->text ("Ranged breakpoint ");
10119 if (uiout->is_mi_like_p ())
f1310107 10120 {
112e8700 10121 uiout->field_string ("reason",
f1310107 10122 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10123 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10124 }
112e8700
SM
10125 uiout->field_int ("bkptno", b->number);
10126 uiout->text (", ");
f1310107
TJB
10127
10128 return PRINT_SRC_AND_LOC;
10129}
10130
10131/* Implement the "print_one" breakpoint_ops method for
10132 ranged breakpoints. */
10133
10134static void
10135print_one_ranged_breakpoint (struct breakpoint *b,
10136 struct bp_location **last_loc)
10137{
10138 struct bp_location *bl = b->loc;
10139 struct value_print_options opts;
79a45e25 10140 struct ui_out *uiout = current_uiout;
f1310107
TJB
10141
10142 /* Ranged breakpoints have only one location. */
10143 gdb_assert (bl && bl->next == NULL);
10144
10145 get_user_print_options (&opts);
10146
10147 if (opts.addressprint)
10148 /* We don't print the address range here, it will be printed later
10149 by print_one_detail_ranged_breakpoint. */
112e8700 10150 uiout->field_skip ("addr");
f1310107
TJB
10151 annotate_field (5);
10152 print_breakpoint_location (b, bl);
10153 *last_loc = bl;
10154}
10155
10156/* Implement the "print_one_detail" breakpoint_ops method for
10157 ranged breakpoints. */
10158
10159static void
10160print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10161 struct ui_out *uiout)
10162{
10163 CORE_ADDR address_start, address_end;
10164 struct bp_location *bl = b->loc;
d7e74731 10165 string_file stb;
f1310107
TJB
10166
10167 gdb_assert (bl);
10168
10169 address_start = bl->address;
10170 address_end = address_start + bl->length - 1;
10171
112e8700 10172 uiout->text ("\taddress range: ");
d7e74731
PA
10173 stb.printf ("[%s, %s]",
10174 print_core_address (bl->gdbarch, address_start),
10175 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10176 uiout->field_stream ("addr", stb);
10177 uiout->text ("\n");
f1310107
TJB
10178}
10179
10180/* Implement the "print_mention" breakpoint_ops method for
10181 ranged breakpoints. */
10182
10183static void
10184print_mention_ranged_breakpoint (struct breakpoint *b)
10185{
10186 struct bp_location *bl = b->loc;
79a45e25 10187 struct ui_out *uiout = current_uiout;
f1310107
TJB
10188
10189 gdb_assert (bl);
10190 gdb_assert (b->type == bp_hardware_breakpoint);
10191
112e8700 10192 if (uiout->is_mi_like_p ())
f1310107
TJB
10193 return;
10194
10195 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10196 b->number, paddress (bl->gdbarch, bl->address),
10197 paddress (bl->gdbarch, bl->address + bl->length - 1));
10198}
10199
10200/* Implement the "print_recreate" breakpoint_ops method for
10201 ranged breakpoints. */
10202
10203static void
10204print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10205{
f00aae0f 10206 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10207 event_location_to_string (b->location.get ()),
10208 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10209 print_recreate_thread (b, fp);
f1310107
TJB
10210}
10211
10212/* The breakpoint_ops structure to be used in ranged breakpoints. */
10213
2060206e 10214static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10215
10216/* Find the address where the end of the breakpoint range should be
10217 placed, given the SAL of the end of the range. This is so that if
10218 the user provides a line number, the end of the range is set to the
10219 last instruction of the given line. */
10220
10221static CORE_ADDR
10222find_breakpoint_range_end (struct symtab_and_line sal)
10223{
10224 CORE_ADDR end;
10225
10226 /* If the user provided a PC value, use it. Otherwise,
10227 find the address of the end of the given location. */
10228 if (sal.explicit_pc)
10229 end = sal.pc;
10230 else
10231 {
10232 int ret;
10233 CORE_ADDR start;
10234
10235 ret = find_line_pc_range (sal, &start, &end);
10236 if (!ret)
10237 error (_("Could not find location of the end of the range."));
10238
10239 /* find_line_pc_range returns the start of the next line. */
10240 end--;
10241 }
10242
10243 return end;
10244}
10245
10246/* Implement the "break-range" CLI command. */
10247
10248static void
10249break_range_command (char *arg, int from_tty)
10250{
870f88f7 10251 char *arg_start, *addr_string_start;
f1310107
TJB
10252 struct linespec_result canonical_start, canonical_end;
10253 int bp_count, can_use_bp, length;
10254 CORE_ADDR end;
10255 struct breakpoint *b;
10256 struct symtab_and_line sal_start, sal_end;
f1310107 10257 struct cleanup *cleanup_bkpt;
f8eba3c6 10258 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10259
10260 /* We don't support software ranged breakpoints. */
10261 if (target_ranged_break_num_registers () < 0)
10262 error (_("This target does not support hardware ranged breakpoints."));
10263
10264 bp_count = hw_breakpoint_used_count ();
10265 bp_count += target_ranged_break_num_registers ();
10266 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10267 bp_count, 0);
10268 if (can_use_bp < 0)
10269 error (_("Hardware breakpoints used exceeds limit."));
10270
f8eba3c6 10271 arg = skip_spaces (arg);
f1310107
TJB
10272 if (arg == NULL || arg[0] == '\0')
10273 error(_("No address range specified."));
10274
f8eba3c6 10275 arg_start = arg;
ffc2605c
TT
10276 event_location_up start_location = string_to_event_location (&arg,
10277 current_language);
10278 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10279
10280 if (arg[0] != ',')
10281 error (_("Too few arguments."));
f8eba3c6 10282 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10283 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10284
10285 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10286
10287 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10288 || lsal_start->sals.nelts != 1)
f1310107
TJB
10289 error (_("Cannot create a ranged breakpoint with multiple locations."));
10290
f8eba3c6
TT
10291 sal_start = lsal_start->sals.sals[0];
10292 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10293 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10294
10295 arg++; /* Skip the comma. */
f8eba3c6 10296 arg = skip_spaces (arg);
f1310107
TJB
10297
10298 /* Parse the end location. */
10299
f1310107
TJB
10300 arg_start = arg;
10301
f8eba3c6 10302 /* We call decode_line_full directly here instead of using
f1310107
TJB
10303 parse_breakpoint_sals because we need to specify the start location's
10304 symtab and line as the default symtab and line for the end of the
10305 range. This makes it possible to have ranges like "foo.c:27, +14",
10306 where +14 means 14 lines from the start location. */
ffc2605c
TT
10307 event_location_up end_location = string_to_event_location (&arg,
10308 current_language);
10309 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10310 sal_start.symtab, sal_start.line,
10311 &canonical_end, NULL, NULL);
10312
f8eba3c6 10313 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10314 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10315
10316 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10317 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10318 || lsal_end->sals.nelts != 1)
f1310107
TJB
10319 error (_("Cannot create a ranged breakpoint with multiple locations."));
10320
f8eba3c6 10321 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10322
10323 end = find_breakpoint_range_end (sal_end);
10324 if (sal_start.pc > end)
177b42fe 10325 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10326
10327 length = end - sal_start.pc + 1;
10328 if (length < 0)
10329 /* Length overflowed. */
10330 error (_("Address range too large."));
10331 else if (length == 1)
10332 {
10333 /* This range is simple enough to be handled by
10334 the `hbreak' command. */
10335 hbreak_command (addr_string_start, 1);
10336
10337 do_cleanups (cleanup_bkpt);
10338
10339 return;
10340 }
10341
10342 /* Now set up the breakpoint. */
10343 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10344 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10345 set_breakpoint_count (breakpoint_count + 1);
10346 b->number = breakpoint_count;
10347 b->disposition = disp_donttouch;
d28cd78a
TT
10348 b->location = std::move (start_location);
10349 b->location_range_end = std::move (end_location);
f1310107
TJB
10350 b->loc->length = length;
10351
f8eba3c6 10352 do_cleanups (cleanup_bkpt);
f1310107
TJB
10353
10354 mention (b);
8d3788bd 10355 observer_notify_breakpoint_created (b);
44702360 10356 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10357}
10358
4a64f543
MS
10359/* Return non-zero if EXP is verified as constant. Returned zero
10360 means EXP is variable. Also the constant detection may fail for
10361 some constant expressions and in such case still falsely return
10362 zero. */
2e6e3d9c 10363
65d79d4b
SDJ
10364static int
10365watchpoint_exp_is_const (const struct expression *exp)
10366{
10367 int i = exp->nelts;
10368
10369 while (i > 0)
10370 {
10371 int oplenp, argsp;
10372
10373 /* We are only interested in the descriptor of each element. */
10374 operator_length (exp, i, &oplenp, &argsp);
10375 i -= oplenp;
10376
10377 switch (exp->elts[i].opcode)
10378 {
10379 case BINOP_ADD:
10380 case BINOP_SUB:
10381 case BINOP_MUL:
10382 case BINOP_DIV:
10383 case BINOP_REM:
10384 case BINOP_MOD:
10385 case BINOP_LSH:
10386 case BINOP_RSH:
10387 case BINOP_LOGICAL_AND:
10388 case BINOP_LOGICAL_OR:
10389 case BINOP_BITWISE_AND:
10390 case BINOP_BITWISE_IOR:
10391 case BINOP_BITWISE_XOR:
10392 case BINOP_EQUAL:
10393 case BINOP_NOTEQUAL:
10394 case BINOP_LESS:
10395 case BINOP_GTR:
10396 case BINOP_LEQ:
10397 case BINOP_GEQ:
10398 case BINOP_REPEAT:
10399 case BINOP_COMMA:
10400 case BINOP_EXP:
10401 case BINOP_MIN:
10402 case BINOP_MAX:
10403 case BINOP_INTDIV:
10404 case BINOP_CONCAT:
65d79d4b
SDJ
10405 case TERNOP_COND:
10406 case TERNOP_SLICE:
65d79d4b
SDJ
10407
10408 case OP_LONG:
10409 case OP_DOUBLE:
10410 case OP_DECFLOAT:
10411 case OP_LAST:
10412 case OP_COMPLEX:
10413 case OP_STRING:
65d79d4b
SDJ
10414 case OP_ARRAY:
10415 case OP_TYPE:
608b4967
TT
10416 case OP_TYPEOF:
10417 case OP_DECLTYPE:
6e72ca20 10418 case OP_TYPEID:
65d79d4b
SDJ
10419 case OP_NAME:
10420 case OP_OBJC_NSSTRING:
10421
10422 case UNOP_NEG:
10423 case UNOP_LOGICAL_NOT:
10424 case UNOP_COMPLEMENT:
10425 case UNOP_ADDR:
10426 case UNOP_HIGH:
aeaa2474 10427 case UNOP_CAST:
9eaf6705
TT
10428
10429 case UNOP_CAST_TYPE:
10430 case UNOP_REINTERPRET_CAST:
10431 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10432 /* Unary, binary and ternary operators: We have to check
10433 their operands. If they are constant, then so is the
10434 result of that operation. For instance, if A and B are
10435 determined to be constants, then so is "A + B".
10436
10437 UNOP_IND is one exception to the rule above, because the
10438 value of *ADDR is not necessarily a constant, even when
10439 ADDR is. */
65d79d4b
SDJ
10440 break;
10441
10442 case OP_VAR_VALUE:
10443 /* Check whether the associated symbol is a constant.
4a64f543 10444
65d79d4b 10445 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10446 possible that a buggy compiler could mark a variable as
10447 constant even when it is not, and TYPE_CONST would return
10448 true in this case, while SYMBOL_CLASS wouldn't.
10449
10450 We also have to check for function symbols because they
10451 are always constant. */
65d79d4b
SDJ
10452 {
10453 struct symbol *s = exp->elts[i + 2].symbol;
10454
10455 if (SYMBOL_CLASS (s) != LOC_BLOCK
10456 && SYMBOL_CLASS (s) != LOC_CONST
10457 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10458 return 0;
10459 break;
10460 }
10461
10462 /* The default action is to return 0 because we are using
10463 the optimistic approach here: If we don't know something,
10464 then it is not a constant. */
10465 default:
10466 return 0;
10467 }
10468 }
10469
10470 return 1;
10471}
10472
c1fc2657 10473/* Watchpoint destructor. */
3a5c3e22 10474
c1fc2657 10475watchpoint::~watchpoint ()
3a5c3e22 10476{
c1fc2657
SM
10477 xfree (this->exp_string);
10478 xfree (this->exp_string_reparse);
10479 value_free (this->val);
3a5c3e22
PA
10480}
10481
348d480f
PA
10482/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10483
10484static void
10485re_set_watchpoint (struct breakpoint *b)
10486{
3a5c3e22
PA
10487 struct watchpoint *w = (struct watchpoint *) b;
10488
348d480f
PA
10489 /* Watchpoint can be either on expression using entirely global
10490 variables, or it can be on local variables.
10491
10492 Watchpoints of the first kind are never auto-deleted, and even
10493 persist across program restarts. Since they can use variables
10494 from shared libraries, we need to reparse expression as libraries
10495 are loaded and unloaded.
10496
10497 Watchpoints on local variables can also change meaning as result
10498 of solib event. For example, if a watchpoint uses both a local
10499 and a global variables in expression, it's a local watchpoint,
10500 but unloading of a shared library will make the expression
10501 invalid. This is not a very common use case, but we still
10502 re-evaluate expression, to avoid surprises to the user.
10503
10504 Note that for local watchpoints, we re-evaluate it only if
10505 watchpoints frame id is still valid. If it's not, it means the
10506 watchpoint is out of scope and will be deleted soon. In fact,
10507 I'm not sure we'll ever be called in this case.
10508
10509 If a local watchpoint's frame id is still valid, then
3a5c3e22 10510 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10511
3a5c3e22
PA
10512 Don't do anything about disabled watchpoints, since they will be
10513 reevaluated again when enabled. */
10514 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10515}
10516
77b06cd7
TJB
10517/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10518
10519static int
10520insert_watchpoint (struct bp_location *bl)
10521{
3a5c3e22
PA
10522 struct watchpoint *w = (struct watchpoint *) bl->owner;
10523 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10524
10525 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10526 w->cond_exp.get ());
77b06cd7
TJB
10527}
10528
10529/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10530
10531static int
73971819 10532remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10533{
3a5c3e22
PA
10534 struct watchpoint *w = (struct watchpoint *) bl->owner;
10535 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10536
10537 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10538 w->cond_exp.get ());
e09342b5
TJB
10539}
10540
e09342b5 10541static int
348d480f 10542breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10543 struct address_space *aspace, CORE_ADDR bp_addr,
10544 const struct target_waitstatus *ws)
e09342b5 10545{
348d480f 10546 struct breakpoint *b = bl->owner;
3a5c3e22 10547 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10548
348d480f
PA
10549 /* Continuable hardware watchpoints are treated as non-existent if the
10550 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10551 some data address). Otherwise gdb won't stop on a break instruction
10552 in the code (not from a breakpoint) when a hardware watchpoint has
10553 been defined. Also skip watchpoints which we know did not trigger
10554 (did not match the data address). */
10555 if (is_hardware_watchpoint (b)
3a5c3e22 10556 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10557 return 0;
9c06b0b4 10558
348d480f 10559 return 1;
9c06b0b4
TJB
10560}
10561
348d480f
PA
10562static void
10563check_status_watchpoint (bpstat bs)
9c06b0b4 10564{
348d480f 10565 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10566
348d480f 10567 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10568}
10569
10570/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10571 hardware watchpoints. */
9c06b0b4
TJB
10572
10573static int
348d480f 10574resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10575{
3a5c3e22
PA
10576 struct watchpoint *w = (struct watchpoint *) bl->owner;
10577 int length = w->exact? 1 : bl->length;
348d480f
PA
10578
10579 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10580}
10581
10582/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10583 hardware watchpoints. */
9c06b0b4
TJB
10584
10585static int
348d480f 10586works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10587{
efa80663
PA
10588 /* Read and access watchpoints only work with hardware support. */
10589 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10590}
10591
9c06b0b4 10592static enum print_stop_action
348d480f 10593print_it_watchpoint (bpstat bs)
9c06b0b4 10594{
348d480f
PA
10595 struct cleanup *old_chain;
10596 struct breakpoint *b;
348d480f 10597 enum print_stop_action result;
3a5c3e22 10598 struct watchpoint *w;
79a45e25 10599 struct ui_out *uiout = current_uiout;
348d480f
PA
10600
10601 gdb_assert (bs->bp_location_at != NULL);
10602
348d480f 10603 b = bs->breakpoint_at;
3a5c3e22 10604 w = (struct watchpoint *) b;
348d480f 10605
d7e74731 10606 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10607
f303dbd6
PA
10608 annotate_watchpoint (b->number);
10609 maybe_print_thread_hit_breakpoint (uiout);
10610
d7e74731
PA
10611 string_file stb;
10612
9c06b0b4
TJB
10613 switch (b->type)
10614 {
348d480f 10615 case bp_watchpoint:
9c06b0b4 10616 case bp_hardware_watchpoint:
112e8700
SM
10617 if (uiout->is_mi_like_p ())
10618 uiout->field_string
10619 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10620 mention (b);
10621 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10622 uiout->text ("\nOld value = ");
d7e74731 10623 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10624 uiout->field_stream ("old", stb);
10625 uiout->text ("\nNew value = ");
d7e74731 10626 watchpoint_value_print (w->val, &stb);
112e8700
SM
10627 uiout->field_stream ("new", stb);
10628 uiout->text ("\n");
348d480f
PA
10629 /* More than one watchpoint may have been triggered. */
10630 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10631 break;
10632
10633 case bp_read_watchpoint:
112e8700
SM
10634 if (uiout->is_mi_like_p ())
10635 uiout->field_string
10636 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10637 mention (b);
10638 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10639 uiout->text ("\nValue = ");
d7e74731 10640 watchpoint_value_print (w->val, &stb);
112e8700
SM
10641 uiout->field_stream ("value", stb);
10642 uiout->text ("\n");
348d480f 10643 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10644 break;
10645
10646 case bp_access_watchpoint:
348d480f
PA
10647 if (bs->old_val != NULL)
10648 {
112e8700
SM
10649 if (uiout->is_mi_like_p ())
10650 uiout->field_string
10651 ("reason",
348d480f
PA
10652 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10653 mention (b);
10654 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10655 uiout->text ("\nOld value = ");
d7e74731 10656 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10657 uiout->field_stream ("old", stb);
10658 uiout->text ("\nNew value = ");
348d480f
PA
10659 }
10660 else
10661 {
10662 mention (b);
112e8700
SM
10663 if (uiout->is_mi_like_p ())
10664 uiout->field_string
10665 ("reason",
348d480f
PA
10666 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10667 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10668 uiout->text ("\nValue = ");
348d480f 10669 }
d7e74731 10670 watchpoint_value_print (w->val, &stb);
112e8700
SM
10671 uiout->field_stream ("new", stb);
10672 uiout->text ("\n");
348d480f 10673 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10674 break;
10675 default:
348d480f 10676 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10677 }
10678
348d480f
PA
10679 do_cleanups (old_chain);
10680 return result;
10681}
10682
10683/* Implement the "print_mention" breakpoint_ops method for hardware
10684 watchpoints. */
10685
10686static void
10687print_mention_watchpoint (struct breakpoint *b)
10688{
3a5c3e22 10689 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10690 struct ui_out *uiout = current_uiout;
46b9c129 10691 const char *tuple_name;
348d480f
PA
10692
10693 switch (b->type)
10694 {
10695 case bp_watchpoint:
112e8700 10696 uiout->text ("Watchpoint ");
46b9c129 10697 tuple_name = "wpt";
348d480f
PA
10698 break;
10699 case bp_hardware_watchpoint:
112e8700 10700 uiout->text ("Hardware watchpoint ");
46b9c129 10701 tuple_name = "wpt";
348d480f
PA
10702 break;
10703 case bp_read_watchpoint:
112e8700 10704 uiout->text ("Hardware read watchpoint ");
46b9c129 10705 tuple_name = "hw-rwpt";
348d480f
PA
10706 break;
10707 case bp_access_watchpoint:
112e8700 10708 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10709 tuple_name = "hw-awpt";
348d480f
PA
10710 break;
10711 default:
10712 internal_error (__FILE__, __LINE__,
10713 _("Invalid hardware watchpoint type."));
10714 }
10715
46b9c129 10716 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10717 uiout->field_int ("number", b->number);
10718 uiout->text (": ");
10719 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10720}
10721
10722/* Implement the "print_recreate" breakpoint_ops method for
10723 watchpoints. */
10724
10725static void
10726print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10727{
3a5c3e22
PA
10728 struct watchpoint *w = (struct watchpoint *) b;
10729
348d480f
PA
10730 switch (b->type)
10731 {
10732 case bp_watchpoint:
10733 case bp_hardware_watchpoint:
10734 fprintf_unfiltered (fp, "watch");
10735 break;
10736 case bp_read_watchpoint:
10737 fprintf_unfiltered (fp, "rwatch");
10738 break;
10739 case bp_access_watchpoint:
10740 fprintf_unfiltered (fp, "awatch");
10741 break;
10742 default:
10743 internal_error (__FILE__, __LINE__,
10744 _("Invalid watchpoint type."));
10745 }
10746
3a5c3e22 10747 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10748 print_recreate_thread (b, fp);
348d480f
PA
10749}
10750
427cd150
TT
10751/* Implement the "explains_signal" breakpoint_ops method for
10752 watchpoints. */
10753
47591c29 10754static int
427cd150
TT
10755explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10756{
10757 /* A software watchpoint cannot cause a signal other than
10758 GDB_SIGNAL_TRAP. */
10759 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10760 return 0;
427cd150 10761
47591c29 10762 return 1;
427cd150
TT
10763}
10764
348d480f
PA
10765/* The breakpoint_ops structure to be used in hardware watchpoints. */
10766
2060206e 10767static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10768
10769/* Implement the "insert" breakpoint_ops method for
10770 masked hardware watchpoints. */
10771
10772static int
10773insert_masked_watchpoint (struct bp_location *bl)
10774{
3a5c3e22
PA
10775 struct watchpoint *w = (struct watchpoint *) bl->owner;
10776
10777 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10778 bl->watchpoint_type);
10779}
10780
10781/* Implement the "remove" breakpoint_ops method for
10782 masked hardware watchpoints. */
10783
10784static int
73971819 10785remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10786{
3a5c3e22
PA
10787 struct watchpoint *w = (struct watchpoint *) bl->owner;
10788
10789 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10790 bl->watchpoint_type);
10791}
10792
10793/* Implement the "resources_needed" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796static int
10797resources_needed_masked_watchpoint (const struct bp_location *bl)
10798{
3a5c3e22
PA
10799 struct watchpoint *w = (struct watchpoint *) bl->owner;
10800
10801 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10802}
10803
10804/* Implement the "works_in_software_mode" breakpoint_ops method for
10805 masked hardware watchpoints. */
10806
10807static int
10808works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10809{
10810 return 0;
10811}
10812
10813/* Implement the "print_it" breakpoint_ops method for
10814 masked hardware watchpoints. */
10815
10816static enum print_stop_action
10817print_it_masked_watchpoint (bpstat bs)
10818{
10819 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10820 struct ui_out *uiout = current_uiout;
348d480f
PA
10821
10822 /* Masked watchpoints have only one location. */
10823 gdb_assert (b->loc && b->loc->next == NULL);
10824
f303dbd6
PA
10825 annotate_watchpoint (b->number);
10826 maybe_print_thread_hit_breakpoint (uiout);
10827
348d480f
PA
10828 switch (b->type)
10829 {
10830 case bp_hardware_watchpoint:
112e8700
SM
10831 if (uiout->is_mi_like_p ())
10832 uiout->field_string
10833 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10834 break;
10835
10836 case bp_read_watchpoint:
112e8700
SM
10837 if (uiout->is_mi_like_p ())
10838 uiout->field_string
10839 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10840 break;
10841
10842 case bp_access_watchpoint:
112e8700
SM
10843 if (uiout->is_mi_like_p ())
10844 uiout->field_string
10845 ("reason",
348d480f
PA
10846 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10847 break;
10848 default:
10849 internal_error (__FILE__, __LINE__,
10850 _("Invalid hardware watchpoint type."));
10851 }
10852
10853 mention (b);
112e8700 10854 uiout->text (_("\n\
9c06b0b4
TJB
10855Check the underlying instruction at PC for the memory\n\
10856address and value which triggered this watchpoint.\n"));
112e8700 10857 uiout->text ("\n");
9c06b0b4
TJB
10858
10859 /* More than one watchpoint may have been triggered. */
10860 return PRINT_UNKNOWN;
10861}
10862
10863/* Implement the "print_one_detail" breakpoint_ops method for
10864 masked hardware watchpoints. */
10865
10866static void
10867print_one_detail_masked_watchpoint (const struct breakpoint *b,
10868 struct ui_out *uiout)
10869{
3a5c3e22
PA
10870 struct watchpoint *w = (struct watchpoint *) b;
10871
9c06b0b4
TJB
10872 /* Masked watchpoints have only one location. */
10873 gdb_assert (b->loc && b->loc->next == NULL);
10874
112e8700
SM
10875 uiout->text ("\tmask ");
10876 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10877 uiout->text ("\n");
9c06b0b4
TJB
10878}
10879
10880/* Implement the "print_mention" breakpoint_ops method for
10881 masked hardware watchpoints. */
10882
10883static void
10884print_mention_masked_watchpoint (struct breakpoint *b)
10885{
3a5c3e22 10886 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10887 struct ui_out *uiout = current_uiout;
46b9c129 10888 const char *tuple_name;
9c06b0b4
TJB
10889
10890 switch (b->type)
10891 {
10892 case bp_hardware_watchpoint:
112e8700 10893 uiout->text ("Masked hardware watchpoint ");
46b9c129 10894 tuple_name = "wpt";
9c06b0b4
TJB
10895 break;
10896 case bp_read_watchpoint:
112e8700 10897 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10898 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10899 break;
10900 case bp_access_watchpoint:
112e8700 10901 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10902 tuple_name = "hw-awpt";
9c06b0b4
TJB
10903 break;
10904 default:
10905 internal_error (__FILE__, __LINE__,
10906 _("Invalid hardware watchpoint type."));
10907 }
10908
46b9c129 10909 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10910 uiout->field_int ("number", b->number);
10911 uiout->text (": ");
10912 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10913}
10914
10915/* Implement the "print_recreate" breakpoint_ops method for
10916 masked hardware watchpoints. */
10917
10918static void
10919print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10920{
3a5c3e22 10921 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10922 char tmp[40];
10923
10924 switch (b->type)
10925 {
10926 case bp_hardware_watchpoint:
10927 fprintf_unfiltered (fp, "watch");
10928 break;
10929 case bp_read_watchpoint:
10930 fprintf_unfiltered (fp, "rwatch");
10931 break;
10932 case bp_access_watchpoint:
10933 fprintf_unfiltered (fp, "awatch");
10934 break;
10935 default:
10936 internal_error (__FILE__, __LINE__,
10937 _("Invalid hardware watchpoint type."));
10938 }
10939
3a5c3e22
PA
10940 sprintf_vma (tmp, w->hw_wp_mask);
10941 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10942 print_recreate_thread (b, fp);
9c06b0b4
TJB
10943}
10944
10945/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10946
2060206e 10947static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10948
10949/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10950
10951static int
10952is_masked_watchpoint (const struct breakpoint *b)
10953{
10954 return b->ops == &masked_watchpoint_breakpoint_ops;
10955}
10956
53a5351d
JM
10957/* accessflag: hw_write: watch write,
10958 hw_read: watch read,
10959 hw_access: watch access (read or write) */
c906108c 10960static void
bbc13ae3 10961watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10962 int just_location, int internal)
c906108c 10963{
c1fc2657 10964 struct breakpoint *scope_breakpoint = NULL;
270140bd 10965 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10966 struct value *val, *mark, *result;
bb9d5f81 10967 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10968 const char *exp_start = NULL;
10969 const char *exp_end = NULL;
10970 const char *tok, *end_tok;
9c06b0b4 10971 int toklen = -1;
bbc13ae3
KS
10972 const char *cond_start = NULL;
10973 const char *cond_end = NULL;
c906108c 10974 enum bptype bp_type;
37e4754d 10975 int thread = -1;
0cf6dd15 10976 int pc = 0;
9c06b0b4
TJB
10977 /* Flag to indicate whether we are going to use masks for
10978 the hardware watchpoint. */
10979 int use_mask = 0;
10980 CORE_ADDR mask = 0;
3a5c3e22 10981 struct watchpoint *w;
bbc13ae3
KS
10982 char *expression;
10983 struct cleanup *back_to;
c906108c 10984
37e4754d
LM
10985 /* Make sure that we actually have parameters to parse. */
10986 if (arg != NULL && arg[0] != '\0')
10987 {
bbc13ae3
KS
10988 const char *value_start;
10989
10990 exp_end = arg + strlen (arg);
37e4754d 10991
9c06b0b4
TJB
10992 /* Look for "parameter value" pairs at the end
10993 of the arguments string. */
bbc13ae3 10994 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10995 {
10996 /* Skip whitespace at the end of the argument list. */
10997 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10998 tok--;
10999
11000 /* Find the beginning of the last token.
11001 This is the value of the parameter. */
11002 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11003 tok--;
11004 value_start = tok + 1;
11005
11006 /* Skip whitespace. */
11007 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11008 tok--;
11009
11010 end_tok = tok;
11011
11012 /* Find the beginning of the second to last token.
11013 This is the parameter itself. */
11014 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11015 tok--;
11016 tok++;
11017 toklen = end_tok - tok + 1;
11018
61012eef 11019 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11020 {
5d5658a1 11021 struct thread_info *thr;
9c06b0b4
TJB
11022 /* At this point we've found a "thread" token, which means
11023 the user is trying to set a watchpoint that triggers
11024 only in a specific thread. */
5d5658a1 11025 const char *endp;
37e4754d 11026
9c06b0b4
TJB
11027 if (thread != -1)
11028 error(_("You can specify only one thread."));
37e4754d 11029
9c06b0b4 11030 /* Extract the thread ID from the next token. */
5d5658a1 11031 thr = parse_thread_id (value_start, &endp);
37e4754d 11032
5d5658a1 11033 /* Check if the user provided a valid thread ID. */
9c06b0b4 11034 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11035 invalid_thread_id_error (value_start);
9c06b0b4 11036
5d5658a1 11037 thread = thr->global_num;
9c06b0b4 11038 }
61012eef 11039 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11040 {
11041 /* We've found a "mask" token, which means the user wants to
11042 create a hardware watchpoint that is going to have the mask
11043 facility. */
11044 struct value *mask_value, *mark;
37e4754d 11045
9c06b0b4
TJB
11046 if (use_mask)
11047 error(_("You can specify only one mask."));
37e4754d 11048
9c06b0b4 11049 use_mask = just_location = 1;
37e4754d 11050
9c06b0b4
TJB
11051 mark = value_mark ();
11052 mask_value = parse_to_comma_and_eval (&value_start);
11053 mask = value_as_address (mask_value);
11054 value_free_to_mark (mark);
11055 }
11056 else
11057 /* We didn't recognize what we found. We should stop here. */
11058 break;
37e4754d 11059
9c06b0b4
TJB
11060 /* Truncate the string and get rid of the "parameter value" pair before
11061 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11062 exp_end = tok;
9c06b0b4 11063 }
37e4754d 11064 }
bbc13ae3
KS
11065 else
11066 exp_end = arg;
37e4754d 11067
bbc13ae3
KS
11068 /* Parse the rest of the arguments. From here on out, everything
11069 is in terms of a newly allocated string instead of the original
11070 ARG. */
c906108c 11071 innermost_block = NULL;
bbc13ae3
KS
11072 expression = savestring (arg, exp_end - arg);
11073 back_to = make_cleanup (xfree, expression);
11074 exp_start = arg = expression;
4d01a485 11075 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11076 exp_end = arg;
fa8a61dc
TT
11077 /* Remove trailing whitespace from the expression before saving it.
11078 This makes the eventual display of the expression string a bit
11079 prettier. */
11080 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11081 --exp_end;
11082
65d79d4b 11083 /* Checking if the expression is not constant. */
4d01a485 11084 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11085 {
11086 int len;
11087
11088 len = exp_end - exp_start;
11089 while (len > 0 && isspace (exp_start[len - 1]))
11090 len--;
11091 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11092 }
11093
c906108c
SS
11094 exp_valid_block = innermost_block;
11095 mark = value_mark ();
4d01a485 11096 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11097
bb9d5f81
PP
11098 if (val != NULL && just_location)
11099 {
11100 saved_bitpos = value_bitpos (val);
11101 saved_bitsize = value_bitsize (val);
11102 }
11103
06a64a0b
TT
11104 if (just_location)
11105 {
9c06b0b4
TJB
11106 int ret;
11107
06a64a0b 11108 exp_valid_block = NULL;
a1442452 11109 val = value_addr (result);
06a64a0b
TT
11110 release_value (val);
11111 value_free_to_mark (mark);
9c06b0b4
TJB
11112
11113 if (use_mask)
11114 {
11115 ret = target_masked_watch_num_registers (value_as_address (val),
11116 mask);
11117 if (ret == -1)
11118 error (_("This target does not support masked watchpoints."));
11119 else if (ret == -2)
11120 error (_("Invalid mask or memory region."));
11121 }
06a64a0b
TT
11122 }
11123 else if (val != NULL)
fa4727a6 11124 release_value (val);
c906108c 11125
bbc13ae3
KS
11126 tok = skip_spaces_const (arg);
11127 end_tok = skip_to_space_const (tok);
c906108c
SS
11128
11129 toklen = end_tok - tok;
11130 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11131 {
60e1c644 11132 innermost_block = NULL;
c906108c 11133 tok = cond_start = end_tok + 1;
4d01a485 11134 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11135
11136 /* The watchpoint expression may not be local, but the condition
11137 may still be. E.g.: `watch global if local > 0'. */
11138 cond_exp_valid_block = innermost_block;
11139
c906108c
SS
11140 cond_end = tok;
11141 }
11142 if (*tok)
8a3fe4f8 11143 error (_("Junk at end of command."));
c906108c 11144
441d7c93
PA
11145 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11146
11147 /* Save this because create_internal_breakpoint below invalidates
11148 'wp_frame'. */
11149 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11150
11151 /* If the expression is "local", then set up a "watchpoint scope"
11152 breakpoint at the point where we've left the scope of the watchpoint
11153 expression. Create the scope breakpoint before the watchpoint, so
11154 that we will encounter it first in bpstat_stop_status. */
441d7c93 11155 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11156 {
441d7c93
PA
11157 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11158
11159 if (frame_id_p (caller_frame_id))
edb3359d 11160 {
441d7c93
PA
11161 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11162 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11163
edb3359d 11164 scope_breakpoint
441d7c93 11165 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11166 bp_watchpoint_scope,
11167 &momentary_breakpoint_ops);
d983da9c 11168
441d7c93
PA
11169 /* create_internal_breakpoint could invalidate WP_FRAME. */
11170 wp_frame = NULL;
11171
edb3359d 11172 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11173
edb3359d
DJ
11174 /* Automatically delete the breakpoint when it hits. */
11175 scope_breakpoint->disposition = disp_del;
d983da9c 11176
edb3359d 11177 /* Only break in the proper frame (help with recursion). */
441d7c93 11178 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11179
edb3359d 11180 /* Set the address at which we will stop. */
441d7c93
PA
11181 scope_breakpoint->loc->gdbarch = caller_arch;
11182 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11183 scope_breakpoint->loc->address
a6d9a66e
UW
11184 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11185 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11186 scope_breakpoint->type);
11187 }
d983da9c
DJ
11188 }
11189
e8369a73
AB
11190 /* Now set up the breakpoint. We create all watchpoints as hardware
11191 watchpoints here even if hardware watchpoints are turned off, a call
11192 to update_watchpoint later in this function will cause the type to
11193 drop back to bp_watchpoint (software watchpoint) if required. */
11194
11195 if (accessflag == hw_read)
11196 bp_type = bp_read_watchpoint;
11197 else if (accessflag == hw_access)
11198 bp_type = bp_access_watchpoint;
11199 else
11200 bp_type = bp_hardware_watchpoint;
3a5c3e22 11201
4d01a485 11202 w = new watchpoint ();
c1fc2657 11203
348d480f 11204 if (use_mask)
c1fc2657 11205 init_raw_breakpoint_without_location (w, NULL, bp_type,
3a5c3e22 11206 &masked_watchpoint_breakpoint_ops);
348d480f 11207 else
c1fc2657 11208 init_raw_breakpoint_without_location (w, NULL, bp_type,
3a5c3e22 11209 &watchpoint_breakpoint_ops);
c1fc2657
SM
11210 w->thread = thread;
11211 w->disposition = disp_donttouch;
11212 w->pspace = current_program_space;
b22e99fd 11213 w->exp = std::move (exp);
3a5c3e22
PA
11214 w->exp_valid_block = exp_valid_block;
11215 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11216 if (just_location)
11217 {
11218 struct type *t = value_type (val);
11219 CORE_ADDR addr = value_as_address (val);
06a64a0b 11220
43cc5389
TT
11221 w->exp_string_reparse
11222 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11223
3a5c3e22 11224 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11225 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11226 }
11227 else
3a5c3e22 11228 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11229
11230 if (use_mask)
11231 {
3a5c3e22 11232 w->hw_wp_mask = mask;
9c06b0b4
TJB
11233 }
11234 else
11235 {
3a5c3e22 11236 w->val = val;
bb9d5f81
PP
11237 w->val_bitpos = saved_bitpos;
11238 w->val_bitsize = saved_bitsize;
3a5c3e22 11239 w->val_valid = 1;
9c06b0b4 11240 }
77b06cd7 11241
c906108c 11242 if (cond_start)
c1fc2657 11243 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 11244 else
c1fc2657 11245 w->cond_string = 0;
c5aa993b 11246
441d7c93 11247 if (frame_id_p (watchpoint_frame))
f6bc2008 11248 {
441d7c93 11249 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11250 w->watchpoint_thread = inferior_ptid;
f6bc2008 11251 }
c906108c 11252 else
f6bc2008 11253 {
3a5c3e22
PA
11254 w->watchpoint_frame = null_frame_id;
11255 w->watchpoint_thread = null_ptid;
f6bc2008 11256 }
c906108c 11257
d983da9c 11258 if (scope_breakpoint != NULL)
c906108c 11259 {
d983da9c
DJ
11260 /* The scope breakpoint is related to the watchpoint. We will
11261 need to act on them together. */
c1fc2657
SM
11262 w->related_breakpoint = scope_breakpoint;
11263 scope_breakpoint->related_breakpoint = w;
c906108c 11264 }
d983da9c 11265
06a64a0b
TT
11266 if (!just_location)
11267 value_free_to_mark (mark);
2d134ed3 11268
492d29ea 11269 TRY
a9634178
TJB
11270 {
11271 /* Finally update the new watchpoint. This creates the locations
11272 that should be inserted. */
3a5c3e22 11273 update_watchpoint (w, 1);
a9634178 11274 }
492d29ea 11275 CATCH (e, RETURN_MASK_ALL)
a9634178 11276 {
c1fc2657 11277 delete_breakpoint (w);
a9634178
TJB
11278 throw_exception (e);
11279 }
492d29ea 11280 END_CATCH
a9634178 11281
c1fc2657 11282 install_breakpoint (internal, w, 1);
bbc13ae3 11283 do_cleanups (back_to);
c906108c
SS
11284}
11285
e09342b5 11286/* Return count of debug registers needed to watch the given expression.
e09342b5 11287 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11288
c906108c 11289static int
a9634178 11290can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11291{
11292 int found_memory_cnt = 0;
2e70b7b9 11293 struct value *head = v;
c906108c
SS
11294
11295 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11296 if (!can_use_hw_watchpoints)
c906108c 11297 return 0;
c5aa993b 11298
5c44784c
JM
11299 /* Make sure that the value of the expression depends only upon
11300 memory contents, and values computed from them within GDB. If we
11301 find any register references or function calls, we can't use a
11302 hardware watchpoint.
11303
11304 The idea here is that evaluating an expression generates a series
11305 of values, one holding the value of every subexpression. (The
11306 expression a*b+c has five subexpressions: a, b, a*b, c, and
11307 a*b+c.) GDB's values hold almost enough information to establish
11308 the criteria given above --- they identify memory lvalues,
11309 register lvalues, computed values, etcetera. So we can evaluate
11310 the expression, and then scan the chain of values that leaves
11311 behind to decide whether we can detect any possible change to the
11312 expression's final value using only hardware watchpoints.
11313
11314 However, I don't think that the values returned by inferior
11315 function calls are special in any way. So this function may not
11316 notice that an expression involving an inferior function call
11317 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11318 for (; v; v = value_next (v))
c906108c 11319 {
5c44784c 11320 if (VALUE_LVAL (v) == lval_memory)
c906108c 11321 {
8464be76
DJ
11322 if (v != head && value_lazy (v))
11323 /* A lazy memory lvalue in the chain is one that GDB never
11324 needed to fetch; we either just used its address (e.g.,
11325 `a' in `a.b') or we never needed it at all (e.g., `a'
11326 in `a,b'). This doesn't apply to HEAD; if that is
11327 lazy then it was not readable, but watch it anyway. */
5c44784c 11328 ;
53a5351d 11329 else
5c44784c
JM
11330 {
11331 /* Ahh, memory we actually used! Check if we can cover
11332 it with hardware watchpoints. */
df407dfe 11333 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11334
11335 /* We only watch structs and arrays if user asked for it
11336 explicitly, never if they just happen to appear in a
11337 middle of some value chain. */
11338 if (v == head
11339 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11340 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11341 {
42ae5230 11342 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11343 int len;
11344 int num_regs;
11345
a9634178 11346 len = (target_exact_watchpoints
e09342b5
TJB
11347 && is_scalar_type_recursive (vtype))?
11348 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11349
e09342b5
TJB
11350 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11351 if (!num_regs)
2e70b7b9
MS
11352 return 0;
11353 else
e09342b5 11354 found_memory_cnt += num_regs;
2e70b7b9 11355 }
5c44784c 11356 }
c5aa993b 11357 }
5086187c
AC
11358 else if (VALUE_LVAL (v) != not_lval
11359 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11360 return 0; /* These are values from the history (e.g., $1). */
5086187c 11361 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11362 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11363 }
11364
11365 /* The expression itself looks suitable for using a hardware
11366 watchpoint, but give the target machine a chance to reject it. */
11367 return found_memory_cnt;
11368}
11369
8b93c638 11370void
84f4c1fe 11371watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11372{
84f4c1fe 11373 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11374}
11375
06a64a0b
TT
11376/* A helper function that looks for the "-location" argument and then
11377 calls watch_command_1. */
11378
11379static void
11380watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11381{
11382 int just_location = 0;
11383
11384 if (arg
11385 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11386 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11387 {
e9cafbcc 11388 arg = skip_spaces (arg);
06a64a0b
TT
11389 just_location = 1;
11390 }
11391
84f4c1fe 11392 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11393}
8926118c 11394
c5aa993b 11395static void
fba45db2 11396watch_command (char *arg, int from_tty)
c906108c 11397{
06a64a0b 11398 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11399}
11400
8b93c638 11401void
84f4c1fe 11402rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11403{
84f4c1fe 11404 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11405}
8926118c 11406
c5aa993b 11407static void
fba45db2 11408rwatch_command (char *arg, int from_tty)
c906108c 11409{
06a64a0b 11410 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11411}
11412
8b93c638 11413void
84f4c1fe 11414awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11415{
84f4c1fe 11416 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11417}
8926118c 11418
c5aa993b 11419static void
fba45db2 11420awatch_command (char *arg, int from_tty)
c906108c 11421{
06a64a0b 11422 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11423}
c906108c 11424\f
c5aa993b 11425
cfc31633
PA
11426/* Data for the FSM that manages the until(location)/advance commands
11427 in infcmd.c. Here because it uses the mechanisms of
11428 breakpoints. */
c906108c 11429
cfc31633 11430struct until_break_fsm
bfec99b2 11431{
cfc31633
PA
11432 /* The base class. */
11433 struct thread_fsm thread_fsm;
11434
11435 /* The thread that as current when the command was executed. */
11436 int thread;
11437
11438 /* The breakpoint set at the destination location. */
11439 struct breakpoint *location_breakpoint;
11440
11441 /* Breakpoint set at the return address in the caller frame. May be
11442 NULL. */
11443 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11444};
11445
8980e177
PA
11446static void until_break_fsm_clean_up (struct thread_fsm *self,
11447 struct thread_info *thread);
11448static int until_break_fsm_should_stop (struct thread_fsm *self,
11449 struct thread_info *thread);
cfc31633
PA
11450static enum async_reply_reason
11451 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11452
11453/* until_break_fsm's vtable. */
11454
11455static struct thread_fsm_ops until_break_fsm_ops =
11456{
11457 NULL, /* dtor */
11458 until_break_fsm_clean_up,
11459 until_break_fsm_should_stop,
11460 NULL, /* return_value */
11461 until_break_fsm_async_reply_reason,
11462};
11463
11464/* Allocate a new until_break_command_fsm. */
11465
11466static struct until_break_fsm *
8980e177 11467new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11468 struct breakpoint *location_breakpoint,
11469 struct breakpoint *caller_breakpoint)
11470{
11471 struct until_break_fsm *sm;
11472
11473 sm = XCNEW (struct until_break_fsm);
8980e177 11474 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11475
11476 sm->thread = thread;
11477 sm->location_breakpoint = location_breakpoint;
11478 sm->caller_breakpoint = caller_breakpoint;
11479
11480 return sm;
11481}
11482
11483/* Implementation of the 'should_stop' FSM method for the
11484 until(location)/advance commands. */
11485
11486static int
8980e177
PA
11487until_break_fsm_should_stop (struct thread_fsm *self,
11488 struct thread_info *tp)
cfc31633
PA
11489{
11490 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11491
11492 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11493 sm->location_breakpoint) != NULL
11494 || (sm->caller_breakpoint != NULL
11495 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11496 sm->caller_breakpoint) != NULL))
11497 thread_fsm_set_finished (self);
11498
11499 return 1;
11500}
11501
11502/* Implementation of the 'clean_up' FSM method for the
11503 until(location)/advance commands. */
11504
c2c6d25f 11505static void
8980e177
PA
11506until_break_fsm_clean_up (struct thread_fsm *self,
11507 struct thread_info *thread)
43ff13b4 11508{
cfc31633 11509 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11510
cfc31633
PA
11511 /* Clean up our temporary breakpoints. */
11512 if (sm->location_breakpoint != NULL)
11513 {
11514 delete_breakpoint (sm->location_breakpoint);
11515 sm->location_breakpoint = NULL;
11516 }
11517 if (sm->caller_breakpoint != NULL)
11518 {
11519 delete_breakpoint (sm->caller_breakpoint);
11520 sm->caller_breakpoint = NULL;
11521 }
11522 delete_longjmp_breakpoint (sm->thread);
11523}
11524
11525/* Implementation of the 'async_reply_reason' FSM method for the
11526 until(location)/advance commands. */
11527
11528static enum async_reply_reason
11529until_break_fsm_async_reply_reason (struct thread_fsm *self)
11530{
11531 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11532}
11533
c906108c 11534void
ae66c1fc 11535until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11536{
11537 struct symtabs_and_lines sals;
11538 struct symtab_and_line sal;
8556afb4
PA
11539 struct frame_info *frame;
11540 struct gdbarch *frame_gdbarch;
11541 struct frame_id stack_frame_id;
11542 struct frame_id caller_frame_id;
cfc31633
PA
11543 struct breakpoint *location_breakpoint;
11544 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11545 struct cleanup *old_chain;
186c406b
TT
11546 int thread;
11547 struct thread_info *tp;
cfc31633 11548 struct until_break_fsm *sm;
c906108c 11549
70509625 11550 clear_proceed_status (0);
c906108c
SS
11551
11552 /* Set a breakpoint where the user wants it and at return from
4a64f543 11553 this function. */
c5aa993b 11554
ffc2605c 11555 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11556
1bfeeb0f 11557 if (last_displayed_sal_is_valid ())
ffc2605c 11558 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11559 get_last_displayed_symtab (),
f8eba3c6 11560 get_last_displayed_line ());
c906108c 11561 else
ffc2605c 11562 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11563 NULL, (struct symtab *) NULL, 0);
c5aa993b 11564
c906108c 11565 if (sals.nelts != 1)
8a3fe4f8 11566 error (_("Couldn't get information on specified line."));
c5aa993b 11567
c906108c 11568 sal = sals.sals[0];
4a64f543 11569 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11570
c906108c 11571 if (*arg)
8a3fe4f8 11572 error (_("Junk at end of arguments."));
c5aa993b 11573
c906108c 11574 resolve_sal_pc (&sal);
c5aa993b 11575
186c406b 11576 tp = inferior_thread ();
5d5658a1 11577 thread = tp->global_num;
186c406b 11578
883bc8d1
PA
11579 old_chain = make_cleanup (null_cleanup, NULL);
11580
8556afb4
PA
11581 /* Note linespec handling above invalidates the frame chain.
11582 Installing a breakpoint also invalidates the frame chain (as it
11583 may need to switch threads), so do any frame handling before
11584 that. */
11585
11586 frame = get_selected_frame (NULL);
11587 frame_gdbarch = get_frame_arch (frame);
11588 stack_frame_id = get_stack_frame_id (frame);
11589 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11590
ae66c1fc
EZ
11591 /* Keep within the current frame, or in frames called by the current
11592 one. */
edb3359d 11593
883bc8d1 11594 if (frame_id_p (caller_frame_id))
c906108c 11595 {
883bc8d1 11596 struct symtab_and_line sal2;
cfc31633 11597 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11598
11599 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11600 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11601 caller_gdbarch = frame_unwind_caller_arch (frame);
11602 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11603 sal2,
11604 caller_frame_id,
11605 bp_until);
11606 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11607
883bc8d1 11608 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11609 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11610 }
c5aa993b 11611
c70a6932
JK
11612 /* set_momentary_breakpoint could invalidate FRAME. */
11613 frame = NULL;
11614
883bc8d1
PA
11615 if (anywhere)
11616 /* If the user told us to continue until a specified location,
11617 we don't specify a frame at which we need to stop. */
cfc31633
PA
11618 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11619 null_frame_id, bp_until);
883bc8d1
PA
11620 else
11621 /* Otherwise, specify the selected frame, because we want to stop
11622 only at the very same frame. */
cfc31633
PA
11623 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11624 stack_frame_id, bp_until);
11625 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11626
8980e177 11627 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11628 location_breakpoint, caller_breakpoint);
cfc31633 11629 tp->thread_fsm = &sm->thread_fsm;
f107f563 11630
cfc31633 11631 discard_cleanups (old_chain);
f107f563 11632
cfc31633 11633 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11634}
ae66c1fc 11635
c906108c
SS
11636/* This function attempts to parse an optional "if <cond>" clause
11637 from the arg string. If one is not found, it returns NULL.
c5aa993b 11638
c906108c
SS
11639 Else, it returns a pointer to the condition string. (It does not
11640 attempt to evaluate the string against a particular block.) And,
11641 it updates arg to point to the first character following the parsed
4a64f543 11642 if clause in the arg string. */
53a5351d 11643
63160a43
PA
11644const char *
11645ep_parse_optional_if_clause (const char **arg)
c906108c 11646{
63160a43 11647 const char *cond_string;
c5aa993b
JM
11648
11649 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11650 return NULL;
c5aa993b 11651
4a64f543 11652 /* Skip the "if" keyword. */
c906108c 11653 (*arg) += 2;
c5aa993b 11654
c906108c 11655 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11656 condition string. */
63160a43 11657 *arg = skip_spaces_const (*arg);
c906108c 11658 cond_string = *arg;
c5aa993b 11659
4a64f543
MS
11660 /* Assume that the condition occupies the remainder of the arg
11661 string. */
c906108c 11662 (*arg) += strlen (cond_string);
c5aa993b 11663
c906108c
SS
11664 return cond_string;
11665}
c5aa993b 11666
c906108c
SS
11667/* Commands to deal with catching events, such as signals, exceptions,
11668 process start/exit, etc. */
c5aa993b
JM
11669
11670typedef enum
11671{
44feb3ce
TT
11672 catch_fork_temporary, catch_vfork_temporary,
11673 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11674}
11675catch_fork_kind;
11676
c906108c 11677static void
63160a43 11678catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11679 struct cmd_list_element *command)
c906108c 11680{
63160a43 11681 const char *arg = arg_entry;
a6d9a66e 11682 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11683 const char *cond_string = NULL;
44feb3ce
TT
11684 catch_fork_kind fork_kind;
11685 int tempflag;
11686
11687 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11688 tempflag = (fork_kind == catch_fork_temporary
11689 || fork_kind == catch_vfork_temporary);
c5aa993b 11690
44feb3ce
TT
11691 if (!arg)
11692 arg = "";
63160a43 11693 arg = skip_spaces_const (arg);
c5aa993b 11694
c906108c 11695 /* The allowed syntax is:
c5aa993b
JM
11696 catch [v]fork
11697 catch [v]fork if <cond>
11698
4a64f543 11699 First, check if there's an if clause. */
c906108c 11700 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11701
c906108c 11702 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11703 error (_("Junk at end of arguments."));
c5aa993b 11704
c906108c 11705 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11706 and enable reporting of such events. */
c5aa993b
JM
11707 switch (fork_kind)
11708 {
44feb3ce
TT
11709 case catch_fork_temporary:
11710 case catch_fork_permanent:
a6d9a66e 11711 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11712 &catch_fork_breakpoint_ops);
c906108c 11713 break;
44feb3ce
TT
11714 case catch_vfork_temporary:
11715 case catch_vfork_permanent:
a6d9a66e 11716 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11717 &catch_vfork_breakpoint_ops);
c906108c 11718 break;
c5aa993b 11719 default:
8a3fe4f8 11720 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11721 break;
c5aa993b 11722 }
c906108c
SS
11723}
11724
11725static void
63160a43 11726catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11727 struct cmd_list_element *command)
c906108c 11728{
63160a43 11729 const char *arg = arg_entry;
b4d90040 11730 struct exec_catchpoint *c;
a6d9a66e 11731 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11732 int tempflag;
63160a43 11733 const char *cond_string = NULL;
c906108c 11734
44feb3ce
TT
11735 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11736
11737 if (!arg)
11738 arg = "";
63160a43 11739 arg = skip_spaces_const (arg);
c906108c
SS
11740
11741 /* The allowed syntax is:
c5aa993b
JM
11742 catch exec
11743 catch exec if <cond>
c906108c 11744
4a64f543 11745 First, check if there's an if clause. */
c906108c
SS
11746 cond_string = ep_parse_optional_if_clause (&arg);
11747
11748 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11749 error (_("Junk at end of arguments."));
c906108c 11750
4d01a485 11751 c = new exec_catchpoint ();
c1fc2657 11752 init_catchpoint (c, gdbarch, tempflag, cond_string,
b4d90040
PA
11753 &catch_exec_breakpoint_ops);
11754 c->exec_pathname = NULL;
11755
c1fc2657 11756 install_breakpoint (0, c, 1);
c906108c 11757}
c5aa993b 11758
9ac4176b 11759void
28010a5d
PA
11760init_ada_exception_breakpoint (struct breakpoint *b,
11761 struct gdbarch *gdbarch,
11762 struct symtab_and_line sal,
11763 char *addr_string,
c0a91b2b 11764 const struct breakpoint_ops *ops,
28010a5d 11765 int tempflag,
349774ef 11766 int enabled,
28010a5d 11767 int from_tty)
f7f9143b 11768{
f7f9143b
JB
11769 if (from_tty)
11770 {
5af949e3
UW
11771 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11772 if (!loc_gdbarch)
11773 loc_gdbarch = gdbarch;
11774
6c95b8df
PA
11775 describe_other_breakpoints (loc_gdbarch,
11776 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11777 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11778 version for exception catchpoints, because two catchpoints
11779 used for different exception names will use the same address.
11780 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11781 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11782 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11783 the user what type of catchpoint it is. The above is good
11784 enough for now, though. */
11785 }
11786
28010a5d 11787 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11788
349774ef 11789 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11790 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11791 b->location = string_to_event_location (&addr_string,
11792 language_def (language_ada));
f7f9143b 11793 b->language = language_ada;
f7f9143b
JB
11794}
11795
c906108c 11796static void
fba45db2 11797catch_command (char *arg, int from_tty)
c906108c 11798{
44feb3ce 11799 error (_("Catch requires an event name."));
c906108c
SS
11800}
11801\f
11802
11803static void
fba45db2 11804tcatch_command (char *arg, int from_tty)
c906108c 11805{
44feb3ce 11806 error (_("Catch requires an event name."));
c906108c
SS
11807}
11808
8a2c437b
TT
11809/* A qsort comparison function that sorts breakpoints in order. */
11810
11811static int
11812compare_breakpoints (const void *a, const void *b)
11813{
9a3c8263 11814 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11815 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11816 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11817 uintptr_t ub = (uintptr_t) *bb;
11818
11819 if ((*ba)->number < (*bb)->number)
11820 return -1;
11821 else if ((*ba)->number > (*bb)->number)
11822 return 1;
11823
11824 /* Now sort by address, in case we see, e..g, two breakpoints with
11825 the number 0. */
11826 if (ua < ub)
11827 return -1;
94b0e70d 11828 return ua > ub ? 1 : 0;
8a2c437b
TT
11829}
11830
80f8a6eb 11831/* Delete breakpoints by address or line. */
c906108c
SS
11832
11833static void
fba45db2 11834clear_command (char *arg, int from_tty)
c906108c 11835{
8a2c437b 11836 struct breakpoint *b, *prev;
d6e956e5
VP
11837 VEC(breakpoint_p) *found = 0;
11838 int ix;
c906108c
SS
11839 int default_match;
11840 struct symtabs_and_lines sals;
11841 struct symtab_and_line sal;
c906108c 11842 int i;
8a2c437b 11843 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11844
11845 if (arg)
11846 {
39cf75f7
DE
11847 sals = decode_line_with_current_source (arg,
11848 (DECODE_LINE_FUNFIRSTLINE
11849 | DECODE_LINE_LIST_MODE));
cf4ded82 11850 make_cleanup (xfree, sals.sals);
c906108c
SS
11851 default_match = 0;
11852 }
11853 else
11854 {
8d749320 11855 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11856 make_cleanup (xfree, sals.sals);
4a64f543 11857 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11858
11859 /* Set sal's line, symtab, pc, and pspace to the values
11860 corresponding to the last call to print_frame_info. If the
11861 codepoint is not valid, this will set all the fields to 0. */
11862 get_last_displayed_sal (&sal);
c906108c 11863 if (sal.symtab == 0)
8a3fe4f8 11864 error (_("No source file specified."));
c906108c
SS
11865
11866 sals.sals[0] = sal;
11867 sals.nelts = 1;
11868
11869 default_match = 1;
11870 }
11871
4a64f543
MS
11872 /* We don't call resolve_sal_pc here. That's not as bad as it
11873 seems, because all existing breakpoints typically have both
11874 file/line and pc set. So, if clear is given file/line, we can
11875 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11876
11877 We only support clearing given the address explicitly
11878 present in breakpoint table. Say, we've set breakpoint
4a64f543 11879 at file:line. There were several PC values for that file:line,
ed0616c6 11880 due to optimization, all in one block.
4a64f543
MS
11881
11882 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11883 PC corresponding to the same file:line, the breakpoint won't
11884 be cleared. We probably can still clear the breakpoint, but
11885 since the other PC value is never presented to user, user
11886 can only find it by guessing, and it does not seem important
11887 to support that. */
11888
4a64f543
MS
11889 /* For each line spec given, delete bps which correspond to it. Do
11890 it in two passes, solely to preserve the current behavior that
11891 from_tty is forced true if we delete more than one
11892 breakpoint. */
c906108c 11893
80f8a6eb 11894 found = NULL;
8a2c437b 11895 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11896 for (i = 0; i < sals.nelts; i++)
11897 {
05cba821
JK
11898 const char *sal_fullname;
11899
c906108c 11900 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11901 If line given (pc == 0), clear all bpts on specified line.
11902 If defaulting, clear all bpts on default line
c906108c 11903 or at default pc.
c5aa993b
JM
11904
11905 defaulting sal.pc != 0 tests to do
11906
11907 0 1 pc
11908 1 1 pc _and_ line
11909 0 0 line
11910 1 0 <can't happen> */
c906108c
SS
11911
11912 sal = sals.sals[i];
05cba821
JK
11913 sal_fullname = (sal.symtab == NULL
11914 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11915
4a64f543 11916 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11917 ALL_BREAKPOINTS (b)
c5aa993b 11918 {
0d381245 11919 int match = 0;
4a64f543 11920 /* Are we going to delete b? */
cc60f2e3 11921 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11922 {
11923 struct bp_location *loc = b->loc;
11924 for (; loc; loc = loc->next)
11925 {
f8eba3c6
TT
11926 /* If the user specified file:line, don't allow a PC
11927 match. This matches historical gdb behavior. */
11928 int pc_match = (!sal.explicit_line
11929 && sal.pc
11930 && (loc->pspace == sal.pspace)
11931 && (loc->address == sal.pc)
11932 && (!section_is_overlay (loc->section)
11933 || loc->section == sal.section));
4aac40c8
TT
11934 int line_match = 0;
11935
11936 if ((default_match || sal.explicit_line)
2f202fde 11937 && loc->symtab != NULL
05cba821 11938 && sal_fullname != NULL
4aac40c8 11939 && sal.pspace == loc->pspace
05cba821
JK
11940 && loc->line_number == sal.line
11941 && filename_cmp (symtab_to_fullname (loc->symtab),
11942 sal_fullname) == 0)
11943 line_match = 1;
4aac40c8 11944
0d381245
VP
11945 if (pc_match || line_match)
11946 {
11947 match = 1;
11948 break;
11949 }
11950 }
11951 }
11952
11953 if (match)
d6e956e5 11954 VEC_safe_push(breakpoint_p, found, b);
c906108c 11955 }
80f8a6eb 11956 }
8a2c437b 11957
80f8a6eb 11958 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11959 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11960 {
11961 if (arg)
8a3fe4f8 11962 error (_("No breakpoint at %s."), arg);
80f8a6eb 11963 else
8a3fe4f8 11964 error (_("No breakpoint at this line."));
80f8a6eb 11965 }
c906108c 11966
8a2c437b
TT
11967 /* Remove duplicates from the vec. */
11968 qsort (VEC_address (breakpoint_p, found),
11969 VEC_length (breakpoint_p, found),
11970 sizeof (breakpoint_p),
11971 compare_breakpoints);
11972 prev = VEC_index (breakpoint_p, found, 0);
11973 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11974 {
11975 if (b == prev)
11976 {
11977 VEC_ordered_remove (breakpoint_p, found, ix);
11978 --ix;
11979 }
11980 }
11981
d6e956e5 11982 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11983 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11984 if (from_tty)
a3f17187 11985 {
d6e956e5 11986 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11987 printf_unfiltered (_("Deleted breakpoint "));
11988 else
11989 printf_unfiltered (_("Deleted breakpoints "));
11990 }
d6e956e5
VP
11991
11992 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11993 {
c5aa993b 11994 if (from_tty)
d6e956e5
VP
11995 printf_unfiltered ("%d ", b->number);
11996 delete_breakpoint (b);
c906108c 11997 }
80f8a6eb
MS
11998 if (from_tty)
11999 putchar_unfiltered ('\n');
8a2c437b
TT
12000
12001 do_cleanups (cleanups);
c906108c
SS
12002}
12003\f
12004/* Delete breakpoint in BS if they are `delete' breakpoints and
12005 all breakpoints that are marked for deletion, whether hit or not.
12006 This is called after any breakpoint is hit, or after errors. */
12007
12008void
fba45db2 12009breakpoint_auto_delete (bpstat bs)
c906108c 12010{
35df4500 12011 struct breakpoint *b, *b_tmp;
c906108c
SS
12012
12013 for (; bs; bs = bs->next)
f431efe5
PA
12014 if (bs->breakpoint_at
12015 && bs->breakpoint_at->disposition == disp_del
c906108c 12016 && bs->stop)
f431efe5 12017 delete_breakpoint (bs->breakpoint_at);
c906108c 12018
35df4500 12019 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12020 {
b5de0fa7 12021 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12022 delete_breakpoint (b);
12023 }
c906108c
SS
12024}
12025
4a64f543
MS
12026/* A comparison function for bp_location AP and BP being interfaced to
12027 qsort. Sort elements primarily by their ADDRESS (no matter what
12028 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12029 secondarily by ordering first permanent elements and
4a64f543 12030 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12031 qsort being an unstable algorithm. */
876fa593
JK
12032
12033static int
f5336ca5 12034bp_locations_compare (const void *ap, const void *bp)
876fa593 12035{
9a3c8263
SM
12036 const struct bp_location *a = *(const struct bp_location **) ap;
12037 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12038
12039 if (a->address != b->address)
12040 return (a->address > b->address) - (a->address < b->address);
12041
dea2aa5f
LM
12042 /* Sort locations at the same address by their pspace number, keeping
12043 locations of the same inferior (in a multi-inferior environment)
12044 grouped. */
12045
12046 if (a->pspace->num != b->pspace->num)
12047 return ((a->pspace->num > b->pspace->num)
12048 - (a->pspace->num < b->pspace->num));
12049
876fa593 12050 /* Sort permanent breakpoints first. */
1a853c52
PA
12051 if (a->permanent != b->permanent)
12052 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12053
c56a97f9
JK
12054 /* Make the internal GDB representation stable across GDB runs
12055 where A and B memory inside GDB can differ. Breakpoint locations of
12056 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12057
12058 if (a->owner->number != b->owner->number)
c56a97f9
JK
12059 return ((a->owner->number > b->owner->number)
12060 - (a->owner->number < b->owner->number));
876fa593
JK
12061
12062 return (a > b) - (a < b);
12063}
12064
f5336ca5
PA
12065/* Set bp_locations_placed_address_before_address_max and
12066 bp_locations_shadow_len_after_address_max according to the current
12067 content of the bp_locations array. */
f7545552
TT
12068
12069static void
f5336ca5 12070bp_locations_target_extensions_update (void)
f7545552 12071{
876fa593
JK
12072 struct bp_location *bl, **blp_tmp;
12073
f5336ca5
PA
12074 bp_locations_placed_address_before_address_max = 0;
12075 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12076
12077 ALL_BP_LOCATIONS (bl, blp_tmp)
12078 {
12079 CORE_ADDR start, end, addr;
12080
12081 if (!bp_location_has_shadow (bl))
12082 continue;
12083
12084 start = bl->target_info.placed_address;
12085 end = start + bl->target_info.shadow_len;
12086
12087 gdb_assert (bl->address >= start);
12088 addr = bl->address - start;
f5336ca5
PA
12089 if (addr > bp_locations_placed_address_before_address_max)
12090 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12091
12092 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12093
12094 gdb_assert (bl->address < end);
12095 addr = end - bl->address;
f5336ca5
PA
12096 if (addr > bp_locations_shadow_len_after_address_max)
12097 bp_locations_shadow_len_after_address_max = addr;
876fa593 12098 }
f7545552
TT
12099}
12100
1e4d1764
YQ
12101/* Download tracepoint locations if they haven't been. */
12102
12103static void
12104download_tracepoint_locations (void)
12105{
7ed2c994 12106 struct breakpoint *b;
dd2e65cc 12107 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 12108
5ed8105e 12109 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 12110
7ed2c994 12111 ALL_TRACEPOINTS (b)
1e4d1764 12112 {
7ed2c994 12113 struct bp_location *bl;
1e4d1764 12114 struct tracepoint *t;
f2a8bc8a 12115 int bp_location_downloaded = 0;
1e4d1764 12116
7ed2c994 12117 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12118 ? !may_insert_fast_tracepoints
12119 : !may_insert_tracepoints))
12120 continue;
12121
dd2e65cc
YQ
12122 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12123 {
12124 if (target_can_download_tracepoint ())
12125 can_download_tracepoint = TRIBOOL_TRUE;
12126 else
12127 can_download_tracepoint = TRIBOOL_FALSE;
12128 }
12129
12130 if (can_download_tracepoint == TRIBOOL_FALSE)
12131 break;
12132
7ed2c994
YQ
12133 for (bl = b->loc; bl; bl = bl->next)
12134 {
12135 /* In tracepoint, locations are _never_ duplicated, so
12136 should_be_inserted is equivalent to
12137 unduplicated_should_be_inserted. */
12138 if (!should_be_inserted (bl) || bl->inserted)
12139 continue;
1e4d1764 12140
7ed2c994 12141 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12142
7ed2c994 12143 target_download_tracepoint (bl);
1e4d1764 12144
7ed2c994 12145 bl->inserted = 1;
f2a8bc8a 12146 bp_location_downloaded = 1;
7ed2c994
YQ
12147 }
12148 t = (struct tracepoint *) b;
12149 t->number_on_target = b->number;
f2a8bc8a
YQ
12150 if (bp_location_downloaded)
12151 observer_notify_breakpoint_modified (b);
1e4d1764 12152 }
1e4d1764
YQ
12153}
12154
934709f0
PW
12155/* Swap the insertion/duplication state between two locations. */
12156
12157static void
12158swap_insertion (struct bp_location *left, struct bp_location *right)
12159{
12160 const int left_inserted = left->inserted;
12161 const int left_duplicate = left->duplicate;
b775012e 12162 const int left_needs_update = left->needs_update;
934709f0
PW
12163 const struct bp_target_info left_target_info = left->target_info;
12164
1e4d1764
YQ
12165 /* Locations of tracepoints can never be duplicated. */
12166 if (is_tracepoint (left->owner))
12167 gdb_assert (!left->duplicate);
12168 if (is_tracepoint (right->owner))
12169 gdb_assert (!right->duplicate);
12170
934709f0
PW
12171 left->inserted = right->inserted;
12172 left->duplicate = right->duplicate;
b775012e 12173 left->needs_update = right->needs_update;
934709f0
PW
12174 left->target_info = right->target_info;
12175 right->inserted = left_inserted;
12176 right->duplicate = left_duplicate;
b775012e 12177 right->needs_update = left_needs_update;
934709f0
PW
12178 right->target_info = left_target_info;
12179}
12180
b775012e
LM
12181/* Force the re-insertion of the locations at ADDRESS. This is called
12182 once a new/deleted/modified duplicate location is found and we are evaluating
12183 conditions on the target's side. Such conditions need to be updated on
12184 the target. */
12185
12186static void
12187force_breakpoint_reinsertion (struct bp_location *bl)
12188{
12189 struct bp_location **locp = NULL, **loc2p;
12190 struct bp_location *loc;
12191 CORE_ADDR address = 0;
12192 int pspace_num;
12193
12194 address = bl->address;
12195 pspace_num = bl->pspace->num;
12196
12197 /* This is only meaningful if the target is
12198 evaluating conditions and if the user has
12199 opted for condition evaluation on the target's
12200 side. */
12201 if (gdb_evaluates_breakpoint_condition_p ()
12202 || !target_supports_evaluation_of_breakpoint_conditions ())
12203 return;
12204
12205 /* Flag all breakpoint locations with this address and
12206 the same program space as the location
12207 as "its condition has changed". We need to
12208 update the conditions on the target's side. */
12209 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12210 {
12211 loc = *loc2p;
12212
12213 if (!is_breakpoint (loc->owner)
12214 || pspace_num != loc->pspace->num)
12215 continue;
12216
12217 /* Flag the location appropriately. We use a different state to
12218 let everyone know that we already updated the set of locations
12219 with addr bl->address and program space bl->pspace. This is so
12220 we don't have to keep calling these functions just to mark locations
12221 that have already been marked. */
12222 loc->condition_changed = condition_updated;
12223
12224 /* Free the agent expression bytecode as well. We will compute
12225 it later on. */
833177a4 12226 loc->cond_bytecode.reset ();
b775012e
LM
12227 }
12228}
44702360
PA
12229/* Called whether new breakpoints are created, or existing breakpoints
12230 deleted, to update the global location list and recompute which
12231 locations are duplicate of which.
b775012e 12232
04086b45
PA
12233 The INSERT_MODE flag determines whether locations may not, may, or
12234 shall be inserted now. See 'enum ugll_insert_mode' for more
12235 info. */
b60e7edf 12236
0d381245 12237static void
44702360 12238update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12239{
74960c60 12240 struct breakpoint *b;
876fa593 12241 struct bp_location **locp, *loc;
f7545552 12242 struct cleanup *cleanups;
b775012e
LM
12243 /* Last breakpoint location address that was marked for update. */
12244 CORE_ADDR last_addr = 0;
12245 /* Last breakpoint location program space that was marked for update. */
12246 int last_pspace_num = -1;
f7545552 12247
2d134ed3
PA
12248 /* Used in the duplicates detection below. When iterating over all
12249 bp_locations, points to the first bp_location of a given address.
12250 Breakpoints and watchpoints of different types are never
12251 duplicates of each other. Keep one pointer for each type of
12252 breakpoint/watchpoint, so we only need to loop over all locations
12253 once. */
12254 struct bp_location *bp_loc_first; /* breakpoint */
12255 struct bp_location *wp_loc_first; /* hardware watchpoint */
12256 struct bp_location *awp_loc_first; /* access watchpoint */
12257 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12258
f5336ca5
PA
12259 /* Saved former bp_locations array which we compare against the newly
12260 built bp_locations from the current state of ALL_BREAKPOINTS. */
12261 struct bp_location **old_locations, **old_locp;
12262 unsigned old_locations_count;
876fa593 12263
f5336ca5
PA
12264 old_locations = bp_locations;
12265 old_locations_count = bp_locations_count;
12266 bp_locations = NULL;
12267 bp_locations_count = 0;
12268 cleanups = make_cleanup (xfree, old_locations);
0d381245 12269
74960c60 12270 ALL_BREAKPOINTS (b)
876fa593 12271 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12272 bp_locations_count++;
876fa593 12273
f5336ca5
PA
12274 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12275 locp = bp_locations;
876fa593
JK
12276 ALL_BREAKPOINTS (b)
12277 for (loc = b->loc; loc; loc = loc->next)
12278 *locp++ = loc;
f5336ca5
PA
12279 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12280 bp_locations_compare);
876fa593 12281
f5336ca5 12282 bp_locations_target_extensions_update ();
74960c60 12283
4a64f543
MS
12284 /* Identify bp_location instances that are no longer present in the
12285 new list, and therefore should be freed. Note that it's not
12286 necessary that those locations should be removed from inferior --
12287 if there's another location at the same address (previously
12288 marked as duplicate), we don't need to remove/insert the
12289 location.
876fa593 12290
4a64f543
MS
12291 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12292 and former bp_location array state respectively. */
876fa593 12293
f5336ca5
PA
12294 locp = bp_locations;
12295 for (old_locp = old_locations;
12296 old_locp < old_locations + old_locations_count;
876fa593 12297 old_locp++)
74960c60 12298 {
876fa593 12299 struct bp_location *old_loc = *old_locp;
c7d46a38 12300 struct bp_location **loc2p;
876fa593 12301
e5dd4106 12302 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12303 not, we have to free it. */
c7d46a38 12304 int found_object = 0;
20874c92
VP
12305 /* Tells if the location should remain inserted in the target. */
12306 int keep_in_target = 0;
12307 int removed = 0;
876fa593 12308
4a64f543
MS
12309 /* Skip LOCP entries which will definitely never be needed.
12310 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12311 while (locp < bp_locations + bp_locations_count
c7d46a38 12312 && (*locp)->address < old_loc->address)
876fa593 12313 locp++;
c7d46a38
PA
12314
12315 for (loc2p = locp;
f5336ca5 12316 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12317 && (*loc2p)->address == old_loc->address);
12318 loc2p++)
12319 {
b775012e
LM
12320 /* Check if this is a new/duplicated location or a duplicated
12321 location that had its condition modified. If so, we want to send
12322 its condition to the target if evaluation of conditions is taking
12323 place there. */
12324 if ((*loc2p)->condition_changed == condition_modified
12325 && (last_addr != old_loc->address
12326 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12327 {
b775012e
LM
12328 force_breakpoint_reinsertion (*loc2p);
12329 last_pspace_num = old_loc->pspace->num;
c7d46a38 12330 }
b775012e
LM
12331
12332 if (*loc2p == old_loc)
12333 found_object = 1;
c7d46a38 12334 }
74960c60 12335
b775012e
LM
12336 /* We have already handled this address, update it so that we don't
12337 have to go through updates again. */
12338 last_addr = old_loc->address;
12339
12340 /* Target-side condition evaluation: Handle deleted locations. */
12341 if (!found_object)
12342 force_breakpoint_reinsertion (old_loc);
12343
4a64f543
MS
12344 /* If this location is no longer present, and inserted, look if
12345 there's maybe a new location at the same address. If so,
12346 mark that one inserted, and don't remove this one. This is
12347 needed so that we don't have a time window where a breakpoint
12348 at certain location is not inserted. */
74960c60 12349
876fa593 12350 if (old_loc->inserted)
0d381245 12351 {
4a64f543
MS
12352 /* If the location is inserted now, we might have to remove
12353 it. */
74960c60 12354
876fa593 12355 if (found_object && should_be_inserted (old_loc))
74960c60 12356 {
4a64f543
MS
12357 /* The location is still present in the location list,
12358 and still should be inserted. Don't do anything. */
20874c92 12359 keep_in_target = 1;
74960c60
VP
12360 }
12361 else
12362 {
b775012e
LM
12363 /* This location still exists, but it won't be kept in the
12364 target since it may have been disabled. We proceed to
12365 remove its target-side condition. */
12366
4a64f543
MS
12367 /* The location is either no longer present, or got
12368 disabled. See if there's another location at the
12369 same address, in which case we don't need to remove
12370 this one from the target. */
876fa593 12371
2bdf28a0 12372 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12373 if (breakpoint_address_is_meaningful (old_loc->owner))
12374 {
876fa593 12375 for (loc2p = locp;
f5336ca5 12376 (loc2p < bp_locations + bp_locations_count
c7d46a38 12377 && (*loc2p)->address == old_loc->address);
876fa593
JK
12378 loc2p++)
12379 {
12380 struct bp_location *loc2 = *loc2p;
12381
2d134ed3 12382 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12383 {
85d721b8
PA
12384 /* Read watchpoint locations are switched to
12385 access watchpoints, if the former are not
12386 supported, but the latter are. */
12387 if (is_hardware_watchpoint (old_loc->owner))
12388 {
12389 gdb_assert (is_hardware_watchpoint (loc2->owner));
12390 loc2->watchpoint_type = old_loc->watchpoint_type;
12391 }
12392
934709f0
PW
12393 /* loc2 is a duplicated location. We need to check
12394 if it should be inserted in case it will be
12395 unduplicated. */
12396 if (loc2 != old_loc
12397 && unduplicated_should_be_inserted (loc2))
c7d46a38 12398 {
934709f0 12399 swap_insertion (old_loc, loc2);
c7d46a38
PA
12400 keep_in_target = 1;
12401 break;
12402 }
876fa593
JK
12403 }
12404 }
12405 }
74960c60
VP
12406 }
12407
20874c92
VP
12408 if (!keep_in_target)
12409 {
834c0d03 12410 if (remove_breakpoint (old_loc))
20874c92 12411 {
4a64f543
MS
12412 /* This is just about all we can do. We could keep
12413 this location on the global list, and try to
12414 remove it next time, but there's no particular
12415 reason why we will succeed next time.
20874c92 12416
4a64f543
MS
12417 Note that at this point, old_loc->owner is still
12418 valid, as delete_breakpoint frees the breakpoint
12419 only after calling us. */
3e43a32a
MS
12420 printf_filtered (_("warning: Error removing "
12421 "breakpoint %d\n"),
876fa593 12422 old_loc->owner->number);
20874c92
VP
12423 }
12424 removed = 1;
12425 }
0d381245 12426 }
74960c60
VP
12427
12428 if (!found_object)
1c5cfe86 12429 {
fbea99ea 12430 if (removed && target_is_non_stop_p ()
1cf4d951 12431 && need_moribund_for_location_type (old_loc))
20874c92 12432 {
db82e815
PA
12433 /* This location was removed from the target. In
12434 non-stop mode, a race condition is possible where
12435 we've removed a breakpoint, but stop events for that
12436 breakpoint are already queued and will arrive later.
12437 We apply an heuristic to be able to distinguish such
12438 SIGTRAPs from other random SIGTRAPs: we keep this
12439 breakpoint location for a bit, and will retire it
12440 after we see some number of events. The theory here
12441 is that reporting of events should, "on the average",
12442 be fair, so after a while we'll see events from all
12443 threads that have anything of interest, and no longer
12444 need to keep this breakpoint location around. We
12445 don't hold locations forever so to reduce chances of
12446 mistaking a non-breakpoint SIGTRAP for a breakpoint
12447 SIGTRAP.
12448
12449 The heuristic failing can be disastrous on
12450 decr_pc_after_break targets.
12451
12452 On decr_pc_after_break targets, like e.g., x86-linux,
12453 if we fail to recognize a late breakpoint SIGTRAP,
12454 because events_till_retirement has reached 0 too
12455 soon, we'll fail to do the PC adjustment, and report
12456 a random SIGTRAP to the user. When the user resumes
12457 the inferior, it will most likely immediately crash
2dec564e 12458 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12459 corrupted, because of being resumed e.g., in the
12460 middle of a multi-byte instruction, or skipped a
12461 one-byte instruction. This was actually seen happen
12462 on native x86-linux, and should be less rare on
12463 targets that do not support new thread events, like
12464 remote, due to the heuristic depending on
12465 thread_count.
12466
12467 Mistaking a random SIGTRAP for a breakpoint trap
12468 causes similar symptoms (PC adjustment applied when
12469 it shouldn't), but then again, playing with SIGTRAPs
12470 behind the debugger's back is asking for trouble.
12471
12472 Since hardware watchpoint traps are always
12473 distinguishable from other traps, so we don't need to
12474 apply keep hardware watchpoint moribund locations
12475 around. We simply always ignore hardware watchpoint
12476 traps we can no longer explain. */
12477
876fa593
JK
12478 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12479 old_loc->owner = NULL;
20874c92 12480
876fa593 12481 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12482 }
12483 else
f431efe5
PA
12484 {
12485 old_loc->owner = NULL;
12486 decref_bp_location (&old_loc);
12487 }
20874c92 12488 }
74960c60 12489 }
1c5cfe86 12490
348d480f
PA
12491 /* Rescan breakpoints at the same address and section, marking the
12492 first one as "first" and any others as "duplicates". This is so
12493 that the bpt instruction is only inserted once. If we have a
12494 permanent breakpoint at the same place as BPT, make that one the
12495 official one, and the rest as duplicates. Permanent breakpoints
12496 are sorted first for the same address.
12497
12498 Do the same for hardware watchpoints, but also considering the
12499 watchpoint's type (regular/access/read) and length. */
12500
12501 bp_loc_first = NULL;
12502 wp_loc_first = NULL;
12503 awp_loc_first = NULL;
12504 rwp_loc_first = NULL;
12505 ALL_BP_LOCATIONS (loc, locp)
12506 {
12507 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12508 non-NULL. */
348d480f 12509 struct bp_location **loc_first_p;
d3fbdd86 12510 b = loc->owner;
348d480f 12511
6f380991 12512 if (!unduplicated_should_be_inserted (loc)
348d480f 12513 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12514 /* Don't detect duplicate for tracepoint locations because they are
12515 never duplicated. See the comments in field `duplicate' of
12516 `struct bp_location'. */
348d480f 12517 || is_tracepoint (b))
b775012e
LM
12518 {
12519 /* Clear the condition modification flag. */
12520 loc->condition_changed = condition_unchanged;
12521 continue;
12522 }
348d480f 12523
348d480f
PA
12524 if (b->type == bp_hardware_watchpoint)
12525 loc_first_p = &wp_loc_first;
12526 else if (b->type == bp_read_watchpoint)
12527 loc_first_p = &rwp_loc_first;
12528 else if (b->type == bp_access_watchpoint)
12529 loc_first_p = &awp_loc_first;
12530 else
12531 loc_first_p = &bp_loc_first;
12532
12533 if (*loc_first_p == NULL
12534 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12535 || !breakpoint_locations_match (loc, *loc_first_p))
12536 {
12537 *loc_first_p = loc;
12538 loc->duplicate = 0;
b775012e
LM
12539
12540 if (is_breakpoint (loc->owner) && loc->condition_changed)
12541 {
12542 loc->needs_update = 1;
12543 /* Clear the condition modification flag. */
12544 loc->condition_changed = condition_unchanged;
12545 }
348d480f
PA
12546 continue;
12547 }
12548
934709f0
PW
12549
12550 /* This and the above ensure the invariant that the first location
12551 is not duplicated, and is the inserted one.
12552 All following are marked as duplicated, and are not inserted. */
12553 if (loc->inserted)
12554 swap_insertion (loc, *loc_first_p);
348d480f
PA
12555 loc->duplicate = 1;
12556
b775012e
LM
12557 /* Clear the condition modification flag. */
12558 loc->condition_changed = condition_unchanged;
348d480f
PA
12559 }
12560
a25a5a45 12561 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12562 {
04086b45 12563 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12564 insert_breakpoint_locations ();
12565 else
12566 {
44702360
PA
12567 /* Even though the caller told us to not insert new
12568 locations, we may still need to update conditions on the
12569 target's side of breakpoints that were already inserted
12570 if the target is evaluating breakpoint conditions. We
b775012e
LM
12571 only update conditions for locations that are marked
12572 "needs_update". */
12573 update_inserted_breakpoint_locations ();
12574 }
12575 }
348d480f 12576
04086b45 12577 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12578 download_tracepoint_locations ();
12579
348d480f
PA
12580 do_cleanups (cleanups);
12581}
12582
12583void
12584breakpoint_retire_moribund (void)
12585{
12586 struct bp_location *loc;
12587 int ix;
12588
12589 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12590 if (--(loc->events_till_retirement) == 0)
12591 {
12592 decref_bp_location (&loc);
12593 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12594 --ix;
12595 }
12596}
12597
12598static void
44702360 12599update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12600{
348d480f 12601
492d29ea
PA
12602 TRY
12603 {
12604 update_global_location_list (insert_mode);
12605 }
12606 CATCH (e, RETURN_MASK_ERROR)
12607 {
12608 }
12609 END_CATCH
348d480f
PA
12610}
12611
12612/* Clear BKP from a BPS. */
12613
12614static void
12615bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12616{
12617 bpstat bs;
12618
12619 for (bs = bps; bs; bs = bs->next)
12620 if (bs->breakpoint_at == bpt)
12621 {
12622 bs->breakpoint_at = NULL;
12623 bs->old_val = NULL;
12624 /* bs->commands will be freed later. */
12625 }
12626}
12627
12628/* Callback for iterate_over_threads. */
12629static int
12630bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12631{
9a3c8263 12632 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12633
12634 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12635 return 0;
12636}
12637
12638/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12639 callbacks. */
12640
12641static void
12642say_where (struct breakpoint *b)
12643{
12644 struct value_print_options opts;
12645
12646 get_user_print_options (&opts);
12647
12648 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12649 single string. */
12650 if (b->loc == NULL)
12651 {
f00aae0f
KS
12652 /* For pending locations, the output differs slightly based
12653 on b->extra_string. If this is non-NULL, it contains either
12654 a condition or dprintf arguments. */
12655 if (b->extra_string == NULL)
12656 {
12657 printf_filtered (_(" (%s) pending."),
d28cd78a 12658 event_location_to_string (b->location.get ()));
f00aae0f
KS
12659 }
12660 else if (b->type == bp_dprintf)
12661 {
12662 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12663 event_location_to_string (b->location.get ()),
f00aae0f
KS
12664 b->extra_string);
12665 }
12666 else
12667 {
12668 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12669 event_location_to_string (b->location.get ()),
f00aae0f
KS
12670 b->extra_string);
12671 }
348d480f
PA
12672 }
12673 else
12674 {
2f202fde 12675 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12676 {
12677 printf_filtered (" at ");
12678 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12679 gdb_stdout);
12680 }
2f202fde 12681 if (b->loc->symtab != NULL)
f8eba3c6
TT
12682 {
12683 /* If there is a single location, we can print the location
12684 more nicely. */
12685 if (b->loc->next == NULL)
12686 printf_filtered (": file %s, line %d.",
05cba821
JK
12687 symtab_to_filename_for_display (b->loc->symtab),
12688 b->loc->line_number);
f8eba3c6
TT
12689 else
12690 /* This is not ideal, but each location may have a
12691 different file name, and this at least reflects the
12692 real situation somewhat. */
f00aae0f 12693 printf_filtered (": %s.",
d28cd78a 12694 event_location_to_string (b->location.get ()));
f8eba3c6 12695 }
348d480f
PA
12696
12697 if (b->loc->next)
12698 {
12699 struct bp_location *loc = b->loc;
12700 int n = 0;
12701 for (; loc; loc = loc->next)
12702 ++n;
12703 printf_filtered (" (%d locations)", n);
12704 }
12705 }
12706}
12707
348d480f
PA
12708/* Default bp_location_ops methods. */
12709
12710static void
12711bp_location_dtor (struct bp_location *self)
12712{
348d480f
PA
12713 xfree (self->function_name);
12714}
12715
12716static const struct bp_location_ops bp_location_ops =
12717{
12718 bp_location_dtor
12719};
12720
c1fc2657 12721/* Destructor for the breakpoint base class. */
348d480f 12722
c1fc2657 12723breakpoint::~breakpoint ()
348d480f 12724{
c1fc2657
SM
12725 decref_counted_command_line (&this->commands);
12726 xfree (this->cond_string);
12727 xfree (this->extra_string);
12728 xfree (this->filter);
348d480f
PA
12729}
12730
2060206e
PA
12731static struct bp_location *
12732base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12733{
5625a286 12734 return new bp_location (&bp_location_ops, self);
348d480f
PA
12735}
12736
2060206e
PA
12737static void
12738base_breakpoint_re_set (struct breakpoint *b)
12739{
12740 /* Nothing to re-set. */
12741}
12742
12743#define internal_error_pure_virtual_called() \
12744 gdb_assert_not_reached ("pure virtual function called")
12745
12746static int
12747base_breakpoint_insert_location (struct bp_location *bl)
12748{
12749 internal_error_pure_virtual_called ();
12750}
12751
12752static int
73971819
PA
12753base_breakpoint_remove_location (struct bp_location *bl,
12754 enum remove_bp_reason reason)
2060206e
PA
12755{
12756 internal_error_pure_virtual_called ();
12757}
12758
12759static int
12760base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12761 struct address_space *aspace,
09ac7c10
TT
12762 CORE_ADDR bp_addr,
12763 const struct target_waitstatus *ws)
2060206e
PA
12764{
12765 internal_error_pure_virtual_called ();
12766}
12767
12768static void
12769base_breakpoint_check_status (bpstat bs)
12770{
12771 /* Always stop. */
12772}
12773
12774/* A "works_in_software_mode" breakpoint_ops method that just internal
12775 errors. */
12776
12777static int
12778base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12779{
12780 internal_error_pure_virtual_called ();
12781}
12782
12783/* A "resources_needed" breakpoint_ops method that just internal
12784 errors. */
12785
12786static int
12787base_breakpoint_resources_needed (const struct bp_location *bl)
12788{
12789 internal_error_pure_virtual_called ();
12790}
12791
12792static enum print_stop_action
12793base_breakpoint_print_it (bpstat bs)
12794{
12795 internal_error_pure_virtual_called ();
12796}
12797
12798static void
12799base_breakpoint_print_one_detail (const struct breakpoint *self,
12800 struct ui_out *uiout)
12801{
12802 /* nothing */
12803}
12804
12805static void
12806base_breakpoint_print_mention (struct breakpoint *b)
12807{
12808 internal_error_pure_virtual_called ();
12809}
12810
12811static void
12812base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12813{
12814 internal_error_pure_virtual_called ();
12815}
12816
983af33b 12817static void
f00aae0f
KS
12818base_breakpoint_create_sals_from_location
12819 (const struct event_location *location,
12820 struct linespec_result *canonical,
12821 enum bptype type_wanted)
983af33b
SDJ
12822{
12823 internal_error_pure_virtual_called ();
12824}
12825
12826static void
12827base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12828 struct linespec_result *c,
e1e01040
PA
12829 gdb::unique_xmalloc_ptr<char> cond_string,
12830 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12831 enum bptype type_wanted,
12832 enum bpdisp disposition,
12833 int thread,
12834 int task, int ignore_count,
12835 const struct breakpoint_ops *o,
12836 int from_tty, int enabled,
44f238bb 12837 int internal, unsigned flags)
983af33b
SDJ
12838{
12839 internal_error_pure_virtual_called ();
12840}
12841
12842static void
f00aae0f
KS
12843base_breakpoint_decode_location (struct breakpoint *b,
12844 const struct event_location *location,
c2f4122d 12845 struct program_space *search_pspace,
983af33b
SDJ
12846 struct symtabs_and_lines *sals)
12847{
12848 internal_error_pure_virtual_called ();
12849}
12850
ab04a2af
TT
12851/* The default 'explains_signal' method. */
12852
47591c29 12853static int
427cd150 12854base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12855{
47591c29 12856 return 1;
ab04a2af
TT
12857}
12858
9d6e6e84
HZ
12859/* The default "after_condition_true" method. */
12860
12861static void
12862base_breakpoint_after_condition_true (struct bpstats *bs)
12863{
12864 /* Nothing to do. */
12865}
12866
ab04a2af 12867struct breakpoint_ops base_breakpoint_ops =
2060206e 12868{
2060206e
PA
12869 base_breakpoint_allocate_location,
12870 base_breakpoint_re_set,
12871 base_breakpoint_insert_location,
12872 base_breakpoint_remove_location,
12873 base_breakpoint_breakpoint_hit,
12874 base_breakpoint_check_status,
12875 base_breakpoint_resources_needed,
12876 base_breakpoint_works_in_software_mode,
12877 base_breakpoint_print_it,
12878 NULL,
12879 base_breakpoint_print_one_detail,
12880 base_breakpoint_print_mention,
983af33b 12881 base_breakpoint_print_recreate,
5f700d83 12882 base_breakpoint_create_sals_from_location,
983af33b 12883 base_breakpoint_create_breakpoints_sal,
5f700d83 12884 base_breakpoint_decode_location,
9d6e6e84
HZ
12885 base_breakpoint_explains_signal,
12886 base_breakpoint_after_condition_true,
2060206e
PA
12887};
12888
12889/* Default breakpoint_ops methods. */
12890
12891static void
348d480f
PA
12892bkpt_re_set (struct breakpoint *b)
12893{
06edf0c0 12894 /* FIXME: is this still reachable? */
9ef9e6a6 12895 if (breakpoint_event_location_empty_p (b))
06edf0c0 12896 {
f00aae0f 12897 /* Anything without a location can't be re-set. */
348d480f 12898 delete_breakpoint (b);
06edf0c0 12899 return;
348d480f 12900 }
06edf0c0
PA
12901
12902 breakpoint_re_set_default (b);
348d480f
PA
12903}
12904
2060206e 12905static int
348d480f
PA
12906bkpt_insert_location (struct bp_location *bl)
12907{
cd6c3b4f
YQ
12908 CORE_ADDR addr = bl->target_info.reqstd_address;
12909
579c6ad9 12910 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12911 bl->target_info.placed_address = addr;
12912
348d480f 12913 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12914 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12915 else
7c16b83e 12916 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12917}
12918
2060206e 12919static int
73971819 12920bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12921{
12922 if (bl->loc_type == bp_loc_hardware_breakpoint)
12923 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12924 else
73971819 12925 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12926}
12927
2060206e 12928static int
348d480f 12929bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12930 struct address_space *aspace, CORE_ADDR bp_addr,
12931 const struct target_waitstatus *ws)
348d480f 12932{
09ac7c10 12933 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12934 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12935 return 0;
12936
348d480f
PA
12937 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12938 aspace, bp_addr))
12939 return 0;
12940
12941 if (overlay_debugging /* unmapped overlay section */
12942 && section_is_overlay (bl->section)
12943 && !section_is_mapped (bl->section))
12944 return 0;
12945
12946 return 1;
12947}
12948
cd1608cc
PA
12949static int
12950dprintf_breakpoint_hit (const struct bp_location *bl,
12951 struct address_space *aspace, CORE_ADDR bp_addr,
12952 const struct target_waitstatus *ws)
12953{
12954 if (dprintf_style == dprintf_style_agent
12955 && target_can_run_breakpoint_commands ())
12956 {
12957 /* An agent-style dprintf never causes a stop. If we see a trap
12958 for this address it must be for a breakpoint that happens to
12959 be set at the same address. */
12960 return 0;
12961 }
12962
12963 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12964}
12965
2060206e 12966static int
348d480f
PA
12967bkpt_resources_needed (const struct bp_location *bl)
12968{
12969 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12970
12971 return 1;
12972}
12973
2060206e 12974static enum print_stop_action
348d480f
PA
12975bkpt_print_it (bpstat bs)
12976{
348d480f
PA
12977 struct breakpoint *b;
12978 const struct bp_location *bl;
001c8c33 12979 int bp_temp;
79a45e25 12980 struct ui_out *uiout = current_uiout;
348d480f
PA
12981
12982 gdb_assert (bs->bp_location_at != NULL);
12983
12984 bl = bs->bp_location_at;
12985 b = bs->breakpoint_at;
12986
001c8c33
PA
12987 bp_temp = b->disposition == disp_del;
12988 if (bl->address != bl->requested_address)
12989 breakpoint_adjustment_warning (bl->requested_address,
12990 bl->address,
12991 b->number, 1);
12992 annotate_breakpoint (b->number);
f303dbd6
PA
12993 maybe_print_thread_hit_breakpoint (uiout);
12994
001c8c33 12995 if (bp_temp)
112e8700 12996 uiout->text ("Temporary breakpoint ");
001c8c33 12997 else
112e8700
SM
12998 uiout->text ("Breakpoint ");
12999 if (uiout->is_mi_like_p ())
348d480f 13000 {
112e8700 13001 uiout->field_string ("reason",
001c8c33 13002 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 13003 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 13004 }
112e8700
SM
13005 uiout->field_int ("bkptno", b->number);
13006 uiout->text (", ");
06edf0c0 13007
001c8c33 13008 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13009}
13010
2060206e 13011static void
06edf0c0
PA
13012bkpt_print_mention (struct breakpoint *b)
13013{
112e8700 13014 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
13015 return;
13016
13017 switch (b->type)
13018 {
13019 case bp_breakpoint:
13020 case bp_gnu_ifunc_resolver:
13021 if (b->disposition == disp_del)
13022 printf_filtered (_("Temporary breakpoint"));
13023 else
13024 printf_filtered (_("Breakpoint"));
13025 printf_filtered (_(" %d"), b->number);
13026 if (b->type == bp_gnu_ifunc_resolver)
13027 printf_filtered (_(" at gnu-indirect-function resolver"));
13028 break;
13029 case bp_hardware_breakpoint:
13030 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13031 break;
e7e0cddf
SS
13032 case bp_dprintf:
13033 printf_filtered (_("Dprintf %d"), b->number);
13034 break;
06edf0c0
PA
13035 }
13036
13037 say_where (b);
13038}
13039
2060206e 13040static void
06edf0c0
PA
13041bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13042{
13043 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13044 fprintf_unfiltered (fp, "tbreak");
13045 else if (tp->type == bp_breakpoint)
13046 fprintf_unfiltered (fp, "break");
13047 else if (tp->type == bp_hardware_breakpoint
13048 && tp->disposition == disp_del)
13049 fprintf_unfiltered (fp, "thbreak");
13050 else if (tp->type == bp_hardware_breakpoint)
13051 fprintf_unfiltered (fp, "hbreak");
13052 else
13053 internal_error (__FILE__, __LINE__,
13054 _("unhandled breakpoint type %d"), (int) tp->type);
13055
f00aae0f 13056 fprintf_unfiltered (fp, " %s",
d28cd78a 13057 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13058
13059 /* Print out extra_string if this breakpoint is pending. It might
13060 contain, for example, conditions that were set by the user. */
13061 if (tp->loc == NULL && tp->extra_string != NULL)
13062 fprintf_unfiltered (fp, " %s", tp->extra_string);
13063
dd11a36c 13064 print_recreate_thread (tp, fp);
06edf0c0
PA
13065}
13066
983af33b 13067static void
f00aae0f
KS
13068bkpt_create_sals_from_location (const struct event_location *location,
13069 struct linespec_result *canonical,
13070 enum bptype type_wanted)
983af33b 13071{
f00aae0f 13072 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13073}
13074
13075static void
13076bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13077 struct linespec_result *canonical,
e1e01040
PA
13078 gdb::unique_xmalloc_ptr<char> cond_string,
13079 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13080 enum bptype type_wanted,
13081 enum bpdisp disposition,
13082 int thread,
13083 int task, int ignore_count,
13084 const struct breakpoint_ops *ops,
13085 int from_tty, int enabled,
44f238bb 13086 int internal, unsigned flags)
983af33b 13087{
023fa29b 13088 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13089 std::move (cond_string),
13090 std::move (extra_string),
e7e0cddf 13091 type_wanted,
983af33b
SDJ
13092 disposition, thread, task,
13093 ignore_count, ops, from_tty,
44f238bb 13094 enabled, internal, flags);
983af33b
SDJ
13095}
13096
13097static void
f00aae0f
KS
13098bkpt_decode_location (struct breakpoint *b,
13099 const struct event_location *location,
c2f4122d 13100 struct program_space *search_pspace,
983af33b
SDJ
13101 struct symtabs_and_lines *sals)
13102{
c2f4122d 13103 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13104}
13105
06edf0c0
PA
13106/* Virtual table for internal breakpoints. */
13107
13108static void
13109internal_bkpt_re_set (struct breakpoint *b)
13110{
13111 switch (b->type)
13112 {
13113 /* Delete overlay event and longjmp master breakpoints; they
13114 will be reset later by breakpoint_re_set. */
13115 case bp_overlay_event:
13116 case bp_longjmp_master:
13117 case bp_std_terminate_master:
13118 case bp_exception_master:
13119 delete_breakpoint (b);
13120 break;
13121
13122 /* This breakpoint is special, it's set up when the inferior
13123 starts and we really don't want to touch it. */
13124 case bp_shlib_event:
13125
13126 /* Like bp_shlib_event, this breakpoint type is special. Once
13127 it is set up, we do not want to touch it. */
13128 case bp_thread_event:
13129 break;
13130 }
13131}
13132
13133static void
13134internal_bkpt_check_status (bpstat bs)
13135{
a9b3a50f
PA
13136 if (bs->breakpoint_at->type == bp_shlib_event)
13137 {
13138 /* If requested, stop when the dynamic linker notifies GDB of
13139 events. This allows the user to get control and place
13140 breakpoints in initializer routines for dynamically loaded
13141 objects (among other things). */
13142 bs->stop = stop_on_solib_events;
13143 bs->print = stop_on_solib_events;
13144 }
13145 else
13146 bs->stop = 0;
06edf0c0
PA
13147}
13148
13149static enum print_stop_action
13150internal_bkpt_print_it (bpstat bs)
13151{
06edf0c0 13152 struct breakpoint *b;
06edf0c0 13153
06edf0c0
PA
13154 b = bs->breakpoint_at;
13155
06edf0c0
PA
13156 switch (b->type)
13157 {
348d480f
PA
13158 case bp_shlib_event:
13159 /* Did we stop because the user set the stop_on_solib_events
13160 variable? (If so, we report this as a generic, "Stopped due
13161 to shlib event" message.) */
edcc5120 13162 print_solib_event (0);
348d480f
PA
13163 break;
13164
13165 case bp_thread_event:
13166 /* Not sure how we will get here.
13167 GDB should not stop for these breakpoints. */
13168 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13169 break;
13170
13171 case bp_overlay_event:
13172 /* By analogy with the thread event, GDB should not stop for these. */
13173 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13174 break;
13175
13176 case bp_longjmp_master:
13177 /* These should never be enabled. */
13178 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13179 break;
13180
13181 case bp_std_terminate_master:
13182 /* These should never be enabled. */
13183 printf_filtered (_("std::terminate Master Breakpoint: "
13184 "gdb should not stop!\n"));
348d480f
PA
13185 break;
13186
13187 case bp_exception_master:
13188 /* These should never be enabled. */
13189 printf_filtered (_("Exception Master Breakpoint: "
13190 "gdb should not stop!\n"));
06edf0c0
PA
13191 break;
13192 }
13193
001c8c33 13194 return PRINT_NOTHING;
06edf0c0
PA
13195}
13196
13197static void
13198internal_bkpt_print_mention (struct breakpoint *b)
13199{
13200 /* Nothing to mention. These breakpoints are internal. */
13201}
13202
06edf0c0
PA
13203/* Virtual table for momentary breakpoints */
13204
13205static void
13206momentary_bkpt_re_set (struct breakpoint *b)
13207{
13208 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13209 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13210 Otherwise these should have been blown away via the cleanup chain
13211 or by breakpoint_init_inferior when we rerun the executable. */
13212}
13213
13214static void
13215momentary_bkpt_check_status (bpstat bs)
13216{
13217 /* Nothing. The point of these breakpoints is causing a stop. */
13218}
13219
13220static enum print_stop_action
13221momentary_bkpt_print_it (bpstat bs)
13222{
001c8c33 13223 return PRINT_UNKNOWN;
348d480f
PA
13224}
13225
06edf0c0
PA
13226static void
13227momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13228{
06edf0c0 13229 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13230}
13231
e2e4d78b
JK
13232/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13233
13234 It gets cleared already on the removal of the first one of such placed
13235 breakpoints. This is OK as they get all removed altogether. */
13236
c1fc2657 13237longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 13238{
c1fc2657 13239 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 13240
c1fc2657 13241 if (tp != NULL)
e2e4d78b 13242 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
13243}
13244
55aa24fb
SDJ
13245/* Specific methods for probe breakpoints. */
13246
13247static int
13248bkpt_probe_insert_location (struct bp_location *bl)
13249{
13250 int v = bkpt_insert_location (bl);
13251
13252 if (v == 0)
13253 {
13254 /* The insertion was successful, now let's set the probe's semaphore
13255 if needed. */
0ea5cda8
SDJ
13256 if (bl->probe.probe->pops->set_semaphore != NULL)
13257 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13258 bl->probe.objfile,
13259 bl->gdbarch);
55aa24fb
SDJ
13260 }
13261
13262 return v;
13263}
13264
13265static int
73971819
PA
13266bkpt_probe_remove_location (struct bp_location *bl,
13267 enum remove_bp_reason reason)
55aa24fb
SDJ
13268{
13269 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13270 if (bl->probe.probe->pops->clear_semaphore != NULL)
13271 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13272 bl->probe.objfile,
13273 bl->gdbarch);
55aa24fb 13274
73971819 13275 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13276}
13277
13278static void
f00aae0f 13279bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13280 struct linespec_result *canonical,
f00aae0f 13281 enum bptype type_wanted)
55aa24fb
SDJ
13282{
13283 struct linespec_sals lsal;
13284
c2f4122d 13285 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13286 lsal.canonical
13287 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13288 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13289}
13290
13291static void
f00aae0f
KS
13292bkpt_probe_decode_location (struct breakpoint *b,
13293 const struct event_location *location,
c2f4122d 13294 struct program_space *search_pspace,
55aa24fb
SDJ
13295 struct symtabs_and_lines *sals)
13296{
c2f4122d 13297 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13298 if (!sals->sals)
13299 error (_("probe not found"));
13300}
13301
348d480f 13302/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13303
348d480f
PA
13304static void
13305tracepoint_re_set (struct breakpoint *b)
13306{
13307 breakpoint_re_set_default (b);
13308}
876fa593 13309
348d480f
PA
13310static int
13311tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13312 struct address_space *aspace, CORE_ADDR bp_addr,
13313 const struct target_waitstatus *ws)
348d480f
PA
13314{
13315 /* By definition, the inferior does not report stops at
13316 tracepoints. */
13317 return 0;
74960c60
VP
13318}
13319
13320static void
348d480f
PA
13321tracepoint_print_one_detail (const struct breakpoint *self,
13322 struct ui_out *uiout)
74960c60 13323{
d9b3f62e
PA
13324 struct tracepoint *tp = (struct tracepoint *) self;
13325 if (tp->static_trace_marker_id)
348d480f
PA
13326 {
13327 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13328
112e8700
SM
13329 uiout->text ("\tmarker id is ");
13330 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13331 tp->static_trace_marker_id);
112e8700 13332 uiout->text ("\n");
348d480f 13333 }
0d381245
VP
13334}
13335
a474d7c2 13336static void
348d480f 13337tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13338{
112e8700 13339 if (current_uiout->is_mi_like_p ())
348d480f 13340 return;
cc59ec59 13341
348d480f
PA
13342 switch (b->type)
13343 {
13344 case bp_tracepoint:
13345 printf_filtered (_("Tracepoint"));
13346 printf_filtered (_(" %d"), b->number);
13347 break;
13348 case bp_fast_tracepoint:
13349 printf_filtered (_("Fast tracepoint"));
13350 printf_filtered (_(" %d"), b->number);
13351 break;
13352 case bp_static_tracepoint:
13353 printf_filtered (_("Static tracepoint"));
13354 printf_filtered (_(" %d"), b->number);
13355 break;
13356 default:
13357 internal_error (__FILE__, __LINE__,
13358 _("unhandled tracepoint type %d"), (int) b->type);
13359 }
13360
13361 say_where (b);
a474d7c2
PA
13362}
13363
348d480f 13364static void
d9b3f62e 13365tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13366{
d9b3f62e
PA
13367 struct tracepoint *tp = (struct tracepoint *) self;
13368
13369 if (self->type == bp_fast_tracepoint)
348d480f 13370 fprintf_unfiltered (fp, "ftrace");
c93e8391 13371 else if (self->type == bp_static_tracepoint)
348d480f 13372 fprintf_unfiltered (fp, "strace");
d9b3f62e 13373 else if (self->type == bp_tracepoint)
348d480f
PA
13374 fprintf_unfiltered (fp, "trace");
13375 else
13376 internal_error (__FILE__, __LINE__,
d9b3f62e 13377 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13378
f00aae0f 13379 fprintf_unfiltered (fp, " %s",
d28cd78a 13380 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13381 print_recreate_thread (self, fp);
13382
13383 if (tp->pass_count)
13384 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13385}
13386
983af33b 13387static void
f00aae0f
KS
13388tracepoint_create_sals_from_location (const struct event_location *location,
13389 struct linespec_result *canonical,
13390 enum bptype type_wanted)
983af33b 13391{
f00aae0f 13392 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13393}
13394
13395static void
13396tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13397 struct linespec_result *canonical,
e1e01040
PA
13398 gdb::unique_xmalloc_ptr<char> cond_string,
13399 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13400 enum bptype type_wanted,
13401 enum bpdisp disposition,
13402 int thread,
13403 int task, int ignore_count,
13404 const struct breakpoint_ops *ops,
13405 int from_tty, int enabled,
44f238bb 13406 int internal, unsigned flags)
983af33b 13407{
023fa29b 13408 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13409 std::move (cond_string),
13410 std::move (extra_string),
e7e0cddf 13411 type_wanted,
983af33b
SDJ
13412 disposition, thread, task,
13413 ignore_count, ops, from_tty,
44f238bb 13414 enabled, internal, flags);
983af33b
SDJ
13415}
13416
13417static void
f00aae0f
KS
13418tracepoint_decode_location (struct breakpoint *b,
13419 const struct event_location *location,
c2f4122d 13420 struct program_space *search_pspace,
983af33b
SDJ
13421 struct symtabs_and_lines *sals)
13422{
c2f4122d 13423 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13424}
13425
2060206e 13426struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13427
55aa24fb
SDJ
13428/* The breakpoint_ops structure to be use on tracepoints placed in a
13429 static probe. */
13430
13431static void
f00aae0f
KS
13432tracepoint_probe_create_sals_from_location
13433 (const struct event_location *location,
13434 struct linespec_result *canonical,
13435 enum bptype type_wanted)
55aa24fb
SDJ
13436{
13437 /* We use the same method for breakpoint on probes. */
f00aae0f 13438 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13439}
13440
13441static void
f00aae0f
KS
13442tracepoint_probe_decode_location (struct breakpoint *b,
13443 const struct event_location *location,
c2f4122d 13444 struct program_space *search_pspace,
55aa24fb
SDJ
13445 struct symtabs_and_lines *sals)
13446{
13447 /* We use the same method for breakpoint on probes. */
c2f4122d 13448 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13449}
13450
13451static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13452
5c2b4418
HZ
13453/* Dprintf breakpoint_ops methods. */
13454
13455static void
13456dprintf_re_set (struct breakpoint *b)
13457{
13458 breakpoint_re_set_default (b);
13459
f00aae0f
KS
13460 /* extra_string should never be non-NULL for dprintf. */
13461 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13462
13463 /* 1 - connect to target 1, that can run breakpoint commands.
13464 2 - create a dprintf, which resolves fine.
13465 3 - disconnect from target 1
13466 4 - connect to target 2, that can NOT run breakpoint commands.
13467
13468 After steps #3/#4, you'll want the dprintf command list to
13469 be updated, because target 1 and 2 may well return different
13470 answers for target_can_run_breakpoint_commands().
13471 Given absence of finer grained resetting, we get to do
13472 it all the time. */
13473 if (b->extra_string != NULL)
13474 update_dprintf_command_list (b);
13475}
13476
2d9442cc
HZ
13477/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13478
13479static void
13480dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13481{
f00aae0f 13482 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13483 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13484 tp->extra_string);
13485 print_recreate_thread (tp, fp);
13486}
13487
9d6e6e84
HZ
13488/* Implement the "after_condition_true" breakpoint_ops method for
13489 dprintf.
13490
13491 dprintf's are implemented with regular commands in their command
13492 list, but we run the commands here instead of before presenting the
13493 stop to the user, as dprintf's don't actually cause a stop. This
13494 also makes it so that the commands of multiple dprintfs at the same
13495 address are all handled. */
13496
13497static void
13498dprintf_after_condition_true (struct bpstats *bs)
13499{
13500 struct cleanup *old_chain;
13501 struct bpstats tmp_bs = { NULL };
13502 struct bpstats *tmp_bs_p = &tmp_bs;
13503
13504 /* dprintf's never cause a stop. This wasn't set in the
13505 check_status hook instead because that would make the dprintf's
13506 condition not be evaluated. */
13507 bs->stop = 0;
13508
13509 /* Run the command list here. Take ownership of it instead of
13510 copying. We never want these commands to run later in
13511 bpstat_do_actions, if a breakpoint that causes a stop happens to
13512 be set at same address as this dprintf, or even if running the
13513 commands here throws. */
13514 tmp_bs.commands = bs->commands;
13515 bs->commands = NULL;
13516 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13517
13518 bpstat_do_actions_1 (&tmp_bs_p);
13519
13520 /* 'tmp_bs.commands' will usually be NULL by now, but
13521 bpstat_do_actions_1 may return early without processing the whole
13522 list. */
13523 do_cleanups (old_chain);
13524}
13525
983af33b
SDJ
13526/* The breakpoint_ops structure to be used on static tracepoints with
13527 markers (`-m'). */
13528
13529static void
f00aae0f 13530strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13531 struct linespec_result *canonical,
f00aae0f 13532 enum bptype type_wanted)
983af33b
SDJ
13533{
13534 struct linespec_sals lsal;
f00aae0f
KS
13535 const char *arg_start, *arg;
13536 char *str;
13537 struct cleanup *cleanup;
983af33b 13538
f00aae0f
KS
13539 arg = arg_start = get_linespec_location (location);
13540 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13541
f00aae0f
KS
13542 str = savestring (arg_start, arg - arg_start);
13543 cleanup = make_cleanup (xfree, str);
8e9e35b1 13544 canonical->location = new_linespec_location (&str);
f00aae0f 13545 do_cleanups (cleanup);
983af33b 13546
8e9e35b1
TT
13547 lsal.canonical
13548 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13549 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13550}
13551
13552static void
13553strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13554 struct linespec_result *canonical,
e1e01040
PA
13555 gdb::unique_xmalloc_ptr<char> cond_string,
13556 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13557 enum bptype type_wanted,
13558 enum bpdisp disposition,
13559 int thread,
13560 int task, int ignore_count,
13561 const struct breakpoint_ops *ops,
13562 int from_tty, int enabled,
44f238bb 13563 int internal, unsigned flags)
983af33b
SDJ
13564{
13565 int i;
52d361e1
YQ
13566 struct linespec_sals *lsal = VEC_index (linespec_sals,
13567 canonical->sals, 0);
983af33b
SDJ
13568
13569 /* If the user is creating a static tracepoint by marker id
13570 (strace -m MARKER_ID), then store the sals index, so that
13571 breakpoint_re_set can try to match up which of the newly
13572 found markers corresponds to this one, and, don't try to
13573 expand multiple locations for each sal, given than SALS
13574 already should contain all sals for MARKER_ID. */
13575
13576 for (i = 0; i < lsal->sals.nelts; ++i)
13577 {
13578 struct symtabs_and_lines expanded;
13579 struct tracepoint *tp;
ffc2605c 13580 event_location_up location;
983af33b
SDJ
13581
13582 expanded.nelts = 1;
13583 expanded.sals = &lsal->sals.sals[i];
13584
8e9e35b1 13585 location = copy_event_location (canonical->location.get ());
983af33b 13586
4d01a485 13587 tp = new tracepoint ();
c1fc2657 13588 init_breakpoint_sal (tp, gdbarch, expanded,
ffc2605c 13589 std::move (location), NULL,
e1e01040
PA
13590 std::move (cond_string),
13591 std::move (extra_string),
e7e0cddf 13592 type_wanted, disposition,
983af33b 13593 thread, task, ignore_count, ops,
44f238bb 13594 from_tty, enabled, internal, flags,
983af33b
SDJ
13595 canonical->special_display);
13596 /* Given that its possible to have multiple markers with
13597 the same string id, if the user is creating a static
13598 tracepoint by marker id ("strace -m MARKER_ID"), then
13599 store the sals index, so that breakpoint_re_set can
13600 try to match up which of the newly found markers
13601 corresponds to this one */
13602 tp->static_trace_marker_id_idx = i;
13603
c1fc2657 13604 install_breakpoint (internal, tp, 0);
983af33b
SDJ
13605 }
13606}
13607
13608static void
f00aae0f
KS
13609strace_marker_decode_location (struct breakpoint *b,
13610 const struct event_location *location,
c2f4122d 13611 struct program_space *search_pspace,
983af33b
SDJ
13612 struct symtabs_and_lines *sals)
13613{
13614 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13615 const char *s = get_linespec_location (location);
983af33b 13616
f00aae0f 13617 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13618 if (sals->nelts > tp->static_trace_marker_id_idx)
13619 {
13620 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13621 sals->nelts = 1;
13622 }
13623 else
13624 error (_("marker %s not found"), tp->static_trace_marker_id);
13625}
13626
13627static struct breakpoint_ops strace_marker_breakpoint_ops;
13628
13629static int
13630strace_marker_p (struct breakpoint *b)
13631{
13632 return b->ops == &strace_marker_breakpoint_ops;
13633}
13634
53a5351d 13635/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13636 structures. */
c906108c
SS
13637
13638void
fba45db2 13639delete_breakpoint (struct breakpoint *bpt)
c906108c 13640{
52f0bd74 13641 struct breakpoint *b;
c906108c 13642
8a3fe4f8 13643 gdb_assert (bpt != NULL);
c906108c 13644
4a64f543
MS
13645 /* Has this bp already been deleted? This can happen because
13646 multiple lists can hold pointers to bp's. bpstat lists are
13647 especial culprits.
13648
13649 One example of this happening is a watchpoint's scope bp. When
13650 the scope bp triggers, we notice that the watchpoint is out of
13651 scope, and delete it. We also delete its scope bp. But the
13652 scope bp is marked "auto-deleting", and is already on a bpstat.
13653 That bpstat is then checked for auto-deleting bp's, which are
13654 deleted.
13655
13656 A real solution to this problem might involve reference counts in
13657 bp's, and/or giving them pointers back to their referencing
13658 bpstat's, and teaching delete_breakpoint to only free a bp's
13659 storage when no more references were extent. A cheaper bandaid
13660 was chosen. */
c906108c
SS
13661 if (bpt->type == bp_none)
13662 return;
13663
4a64f543
MS
13664 /* At least avoid this stale reference until the reference counting
13665 of breakpoints gets resolved. */
d0fb5eae 13666 if (bpt->related_breakpoint != bpt)
e5a0a904 13667 {
d0fb5eae 13668 struct breakpoint *related;
3a5c3e22 13669 struct watchpoint *w;
d0fb5eae
JK
13670
13671 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13672 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13673 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13674 w = (struct watchpoint *) bpt;
13675 else
13676 w = NULL;
13677 if (w != NULL)
13678 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13679
13680 /* Unlink bpt from the bpt->related_breakpoint ring. */
13681 for (related = bpt; related->related_breakpoint != bpt;
13682 related = related->related_breakpoint);
13683 related->related_breakpoint = bpt->related_breakpoint;
13684 bpt->related_breakpoint = bpt;
e5a0a904
JK
13685 }
13686
a9634178
TJB
13687 /* watch_command_1 creates a watchpoint but only sets its number if
13688 update_watchpoint succeeds in creating its bp_locations. If there's
13689 a problem in that process, we'll be asked to delete the half-created
13690 watchpoint. In that case, don't announce the deletion. */
13691 if (bpt->number)
13692 observer_notify_breakpoint_deleted (bpt);
c906108c 13693
c906108c
SS
13694 if (breakpoint_chain == bpt)
13695 breakpoint_chain = bpt->next;
13696
c906108c
SS
13697 ALL_BREAKPOINTS (b)
13698 if (b->next == bpt)
c5aa993b
JM
13699 {
13700 b->next = bpt->next;
13701 break;
13702 }
c906108c 13703
f431efe5
PA
13704 /* Be sure no bpstat's are pointing at the breakpoint after it's
13705 been freed. */
13706 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13707 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13708 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13709 commands are associated with the bpstat; if we remove it here,
13710 then the later call to bpstat_do_actions (&stop_bpstat); in
13711 event-top.c won't do anything, and temporary breakpoints with
13712 commands won't work. */
13713
13714 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13715
4a64f543
MS
13716 /* Now that breakpoint is removed from breakpoint list, update the
13717 global location list. This will remove locations that used to
13718 belong to this breakpoint. Do this before freeing the breakpoint
13719 itself, since remove_breakpoint looks at location's owner. It
13720 might be better design to have location completely
13721 self-contained, but it's not the case now. */
44702360 13722 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13723
4a64f543
MS
13724 /* On the chance that someone will soon try again to delete this
13725 same bp, we mark it as deleted before freeing its storage. */
c906108c 13726 bpt->type = bp_none;
4d01a485 13727 delete bpt;
c906108c
SS
13728}
13729
4d6140d9
AC
13730static void
13731do_delete_breakpoint_cleanup (void *b)
13732{
9a3c8263 13733 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13734}
13735
13736struct cleanup *
13737make_cleanup_delete_breakpoint (struct breakpoint *b)
13738{
13739 return make_cleanup (do_delete_breakpoint_cleanup, b);
13740}
13741
51be5b68
PA
13742/* Iterator function to call a user-provided callback function once
13743 for each of B and its related breakpoints. */
13744
13745static void
13746iterate_over_related_breakpoints (struct breakpoint *b,
13747 void (*function) (struct breakpoint *,
13748 void *),
13749 void *data)
13750{
13751 struct breakpoint *related;
13752
13753 related = b;
13754 do
13755 {
13756 struct breakpoint *next;
13757
13758 /* FUNCTION may delete RELATED. */
13759 next = related->related_breakpoint;
13760
13761 if (next == related)
13762 {
13763 /* RELATED is the last ring entry. */
13764 function (related, data);
13765
13766 /* FUNCTION may have deleted it, so we'd never reach back to
13767 B. There's nothing left to do anyway, so just break
13768 out. */
13769 break;
13770 }
13771 else
13772 function (related, data);
13773
13774 related = next;
13775 }
13776 while (related != b);
13777}
95a42b64
TT
13778
13779static void
13780do_delete_breakpoint (struct breakpoint *b, void *ignore)
13781{
13782 delete_breakpoint (b);
13783}
13784
51be5b68
PA
13785/* A callback for map_breakpoint_numbers that calls
13786 delete_breakpoint. */
13787
13788static void
13789do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13790{
13791 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13792}
13793
c906108c 13794void
fba45db2 13795delete_command (char *arg, int from_tty)
c906108c 13796{
35df4500 13797 struct breakpoint *b, *b_tmp;
c906108c 13798
ea9365bb
TT
13799 dont_repeat ();
13800
c906108c
SS
13801 if (arg == 0)
13802 {
13803 int breaks_to_delete = 0;
13804
46c6471b
PA
13805 /* Delete all breakpoints if no argument. Do not delete
13806 internal breakpoints, these have to be deleted with an
13807 explicit breakpoint number argument. */
c5aa993b 13808 ALL_BREAKPOINTS (b)
46c6471b 13809 if (user_breakpoint_p (b))
973d738b
DJ
13810 {
13811 breaks_to_delete = 1;
13812 break;
13813 }
c906108c
SS
13814
13815 /* Ask user only if there are some breakpoints to delete. */
13816 if (!from_tty
e2e0b3e5 13817 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13818 {
35df4500 13819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13820 if (user_breakpoint_p (b))
c5aa993b 13821 delete_breakpoint (b);
c906108c
SS
13822 }
13823 }
13824 else
51be5b68 13825 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13826}
13827
c2f4122d
PA
13828/* Return true if all locations of B bound to PSPACE are pending. If
13829 PSPACE is NULL, all locations of all program spaces are
13830 considered. */
13831
0d381245 13832static int
c2f4122d 13833all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13834{
c2f4122d
PA
13835 struct bp_location *loc;
13836
13837 for (loc = b->loc; loc != NULL; loc = loc->next)
13838 if ((pspace == NULL
13839 || loc->pspace == pspace)
13840 && !loc->shlib_disabled
8645ff69 13841 && !loc->pspace->executing_startup)
0d381245
VP
13842 return 0;
13843 return 1;
fe3f5fa8
VP
13844}
13845
776592bf
DE
13846/* Subroutine of update_breakpoint_locations to simplify it.
13847 Return non-zero if multiple fns in list LOC have the same name.
13848 Null names are ignored. */
13849
13850static int
13851ambiguous_names_p (struct bp_location *loc)
13852{
13853 struct bp_location *l;
13854 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13855 (int (*) (const void *,
13856 const void *)) streq,
776592bf
DE
13857 NULL, xcalloc, xfree);
13858
13859 for (l = loc; l != NULL; l = l->next)
13860 {
13861 const char **slot;
13862 const char *name = l->function_name;
13863
13864 /* Allow for some names to be NULL, ignore them. */
13865 if (name == NULL)
13866 continue;
13867
13868 slot = (const char **) htab_find_slot (htab, (const void *) name,
13869 INSERT);
4a64f543
MS
13870 /* NOTE: We can assume slot != NULL here because xcalloc never
13871 returns NULL. */
776592bf
DE
13872 if (*slot != NULL)
13873 {
13874 htab_delete (htab);
13875 return 1;
13876 }
13877 *slot = name;
13878 }
13879
13880 htab_delete (htab);
13881 return 0;
13882}
13883
0fb4aa4b
PA
13884/* When symbols change, it probably means the sources changed as well,
13885 and it might mean the static tracepoint markers are no longer at
13886 the same address or line numbers they used to be at last we
13887 checked. Losing your static tracepoints whenever you rebuild is
13888 undesirable. This function tries to resync/rematch gdb static
13889 tracepoints with the markers on the target, for static tracepoints
13890 that have not been set by marker id. Static tracepoint that have
13891 been set by marker id are reset by marker id in breakpoint_re_set.
13892 The heuristic is:
13893
13894 1) For a tracepoint set at a specific address, look for a marker at
13895 the old PC. If one is found there, assume to be the same marker.
13896 If the name / string id of the marker found is different from the
13897 previous known name, assume that means the user renamed the marker
13898 in the sources, and output a warning.
13899
13900 2) For a tracepoint set at a given line number, look for a marker
13901 at the new address of the old line number. If one is found there,
13902 assume to be the same marker. If the name / string id of the
13903 marker found is different from the previous known name, assume that
13904 means the user renamed the marker in the sources, and output a
13905 warning.
13906
13907 3) If a marker is no longer found at the same address or line, it
13908 may mean the marker no longer exists. But it may also just mean
13909 the code changed a bit. Maybe the user added a few lines of code
13910 that made the marker move up or down (in line number terms). Ask
13911 the target for info about the marker with the string id as we knew
13912 it. If found, update line number and address in the matching
13913 static tracepoint. This will get confused if there's more than one
13914 marker with the same ID (possible in UST, although unadvised
13915 precisely because it confuses tools). */
13916
13917static struct symtab_and_line
13918update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13919{
d9b3f62e 13920 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13921 struct static_tracepoint_marker marker;
13922 CORE_ADDR pc;
0fb4aa4b
PA
13923
13924 pc = sal.pc;
13925 if (sal.line)
13926 find_line_pc (sal.symtab, sal.line, &pc);
13927
13928 if (target_static_tracepoint_marker_at (pc, &marker))
13929 {
d9b3f62e 13930 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13931 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13932 b->number,
d9b3f62e 13933 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13934
d9b3f62e
PA
13935 xfree (tp->static_trace_marker_id);
13936 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13937 release_static_tracepoint_marker (&marker);
13938
13939 return sal;
13940 }
13941
13942 /* Old marker wasn't found on target at lineno. Try looking it up
13943 by string ID. */
13944 if (!sal.explicit_pc
13945 && sal.line != 0
13946 && sal.symtab != NULL
d9b3f62e 13947 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13948 {
13949 VEC(static_tracepoint_marker_p) *markers;
13950
13951 markers
d9b3f62e 13952 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13953
13954 if (!VEC_empty(static_tracepoint_marker_p, markers))
13955 {
80e1d417 13956 struct symtab_and_line sal2;
0fb4aa4b 13957 struct symbol *sym;
80e1d417 13958 struct static_tracepoint_marker *tpmarker;
79a45e25 13959 struct ui_out *uiout = current_uiout;
67994074 13960 struct explicit_location explicit_loc;
0fb4aa4b 13961
80e1d417 13962 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13963
d9b3f62e 13964 xfree (tp->static_trace_marker_id);
80e1d417 13965 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13966
13967 warning (_("marker for static tracepoint %d (%s) not "
13968 "found at previous line number"),
d9b3f62e 13969 b->number, tp->static_trace_marker_id);
0fb4aa4b 13970
80e1d417 13971 init_sal (&sal2);
0fb4aa4b 13972
80e1d417 13973 sal2.pc = tpmarker->address;
0fb4aa4b 13974
80e1d417
AS
13975 sal2 = find_pc_line (tpmarker->address, 0);
13976 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13977 uiout->text ("Now in ");
0fb4aa4b
PA
13978 if (sym)
13979 {
112e8700
SM
13980 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13981 uiout->text (" at ");
0fb4aa4b 13982 }
112e8700 13983 uiout->field_string ("file",
05cba821 13984 symtab_to_filename_for_display (sal2.symtab));
112e8700 13985 uiout->text (":");
0fb4aa4b 13986
112e8700 13987 if (uiout->is_mi_like_p ())
0fb4aa4b 13988 {
0b0865da 13989 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13990
112e8700 13991 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13992 }
13993
112e8700
SM
13994 uiout->field_int ("line", sal2.line);
13995 uiout->text ("\n");
0fb4aa4b 13996
80e1d417 13997 b->loc->line_number = sal2.line;
2f202fde 13998 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13999
d28cd78a 14000 b->location.reset (NULL);
67994074
KS
14001 initialize_explicit_location (&explicit_loc);
14002 explicit_loc.source_filename
00e52e53 14003 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14004 explicit_loc.line_offset.offset = b->loc->line_number;
14005 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 14006 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14007
14008 /* Might be nice to check if function changed, and warn if
14009 so. */
14010
80e1d417 14011 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14012 }
14013 }
14014 return sal;
14015}
14016
8d3788bd
VP
14017/* Returns 1 iff locations A and B are sufficiently same that
14018 we don't need to report breakpoint as changed. */
14019
14020static int
14021locations_are_equal (struct bp_location *a, struct bp_location *b)
14022{
14023 while (a && b)
14024 {
14025 if (a->address != b->address)
14026 return 0;
14027
14028 if (a->shlib_disabled != b->shlib_disabled)
14029 return 0;
14030
14031 if (a->enabled != b->enabled)
14032 return 0;
14033
14034 a = a->next;
14035 b = b->next;
14036 }
14037
14038 if ((a == NULL) != (b == NULL))
14039 return 0;
14040
14041 return 1;
14042}
14043
c2f4122d
PA
14044/* Split all locations of B that are bound to PSPACE out of B's
14045 location list to a separate list and return that list's head. If
14046 PSPACE is NULL, hoist out all locations of B. */
14047
14048static struct bp_location *
14049hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14050{
14051 struct bp_location head;
14052 struct bp_location *i = b->loc;
14053 struct bp_location **i_link = &b->loc;
14054 struct bp_location *hoisted = &head;
14055
14056 if (pspace == NULL)
14057 {
14058 i = b->loc;
14059 b->loc = NULL;
14060 return i;
14061 }
14062
14063 head.next = NULL;
14064
14065 while (i != NULL)
14066 {
14067 if (i->pspace == pspace)
14068 {
14069 *i_link = i->next;
14070 i->next = NULL;
14071 hoisted->next = i;
14072 hoisted = i;
14073 }
14074 else
14075 i_link = &i->next;
14076 i = *i_link;
14077 }
14078
14079 return head.next;
14080}
14081
14082/* Create new breakpoint locations for B (a hardware or software
14083 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14084 zero, then B is a ranged breakpoint. Only recreates locations for
14085 FILTER_PSPACE. Locations of other program spaces are left
14086 untouched. */
f1310107 14087
0e30163f 14088void
0d381245 14089update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14090 struct program_space *filter_pspace,
f1310107
TJB
14091 struct symtabs_and_lines sals,
14092 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14093{
14094 int i;
c2f4122d 14095 struct bp_location *existing_locations;
0d381245 14096
f8eba3c6
TT
14097 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14098 {
14099 /* Ranged breakpoints have only one start location and one end
14100 location. */
14101 b->enable_state = bp_disabled;
f8eba3c6
TT
14102 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14103 "multiple locations found\n"),
14104 b->number);
14105 return;
14106 }
f1310107 14107
4a64f543
MS
14108 /* If there's no new locations, and all existing locations are
14109 pending, don't do anything. This optimizes the common case where
14110 all locations are in the same shared library, that was unloaded.
14111 We'd like to retain the location, so that when the library is
14112 loaded again, we don't loose the enabled/disabled status of the
14113 individual locations. */
c2f4122d 14114 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14115 return;
14116
c2f4122d 14117 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14118
0d381245 14119 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14120 {
f8eba3c6
TT
14121 struct bp_location *new_loc;
14122
14123 switch_to_program_space_and_thread (sals.sals[i].pspace);
14124
14125 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14126
0d381245
VP
14127 /* Reparse conditions, they might contain references to the
14128 old symtab. */
14129 if (b->cond_string != NULL)
14130 {
bbc13ae3 14131 const char *s;
fe3f5fa8 14132
0d381245 14133 s = b->cond_string;
492d29ea 14134 TRY
0d381245 14135 {
1bb9788d
TT
14136 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14137 block_for_pc (sals.sals[i].pc),
0d381245
VP
14138 0);
14139 }
492d29ea 14140 CATCH (e, RETURN_MASK_ERROR)
0d381245 14141 {
3e43a32a
MS
14142 warning (_("failed to reevaluate condition "
14143 "for breakpoint %d: %s"),
0d381245
VP
14144 b->number, e.message);
14145 new_loc->enabled = 0;
14146 }
492d29ea 14147 END_CATCH
0d381245 14148 }
fe3f5fa8 14149
f1310107
TJB
14150 if (sals_end.nelts)
14151 {
14152 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14153
14154 new_loc->length = end - sals.sals[0].pc + 1;
14155 }
0d381245 14156 }
fe3f5fa8 14157
4a64f543
MS
14158 /* If possible, carry over 'disable' status from existing
14159 breakpoints. */
0d381245
VP
14160 {
14161 struct bp_location *e = existing_locations;
776592bf
DE
14162 /* If there are multiple breakpoints with the same function name,
14163 e.g. for inline functions, comparing function names won't work.
14164 Instead compare pc addresses; this is just a heuristic as things
14165 may have moved, but in practice it gives the correct answer
14166 often enough until a better solution is found. */
14167 int have_ambiguous_names = ambiguous_names_p (b->loc);
14168
0d381245
VP
14169 for (; e; e = e->next)
14170 {
14171 if (!e->enabled && e->function_name)
14172 {
14173 struct bp_location *l = b->loc;
776592bf
DE
14174 if (have_ambiguous_names)
14175 {
14176 for (; l; l = l->next)
f1310107 14177 if (breakpoint_locations_match (e, l))
776592bf
DE
14178 {
14179 l->enabled = 0;
14180 break;
14181 }
14182 }
14183 else
14184 {
14185 for (; l; l = l->next)
14186 if (l->function_name
14187 && strcmp (e->function_name, l->function_name) == 0)
14188 {
14189 l->enabled = 0;
14190 break;
14191 }
14192 }
0d381245
VP
14193 }
14194 }
14195 }
fe3f5fa8 14196
8d3788bd
VP
14197 if (!locations_are_equal (existing_locations, b->loc))
14198 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14199}
14200
f00aae0f 14201/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14202 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14203
14204static struct symtabs_and_lines
f00aae0f 14205location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14206 struct program_space *search_pspace, int *found)
ef23e705 14207{
02d20e4a 14208 struct symtabs_and_lines sals = {0};
492d29ea 14209 struct gdb_exception exception = exception_none;
ef23e705 14210
983af33b 14211 gdb_assert (b->ops != NULL);
ef23e705 14212
492d29ea 14213 TRY
ef23e705 14214 {
c2f4122d 14215 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14216 }
492d29ea 14217 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14218 {
14219 int not_found_and_ok = 0;
492d29ea
PA
14220
14221 exception = e;
14222
ef23e705
TJB
14223 /* For pending breakpoints, it's expected that parsing will
14224 fail until the right shared library is loaded. User has
14225 already told to create pending breakpoints and don't need
14226 extra messages. If breakpoint is in bp_shlib_disabled
14227 state, then user already saw the message about that
14228 breakpoint being disabled, and don't want to see more
14229 errors. */
58438ac1 14230 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14231 && (b->condition_not_parsed
14232 || (b->loc != NULL
14233 && search_pspace != NULL
14234 && b->loc->pspace != search_pspace)
ef23e705 14235 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14236 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14237 || b->enable_state == bp_disabled))
14238 not_found_and_ok = 1;
14239
14240 if (!not_found_and_ok)
14241 {
14242 /* We surely don't want to warn about the same breakpoint
14243 10 times. One solution, implemented here, is disable
14244 the breakpoint on error. Another solution would be to
14245 have separate 'warning emitted' flag. Since this
14246 happens only when a binary has changed, I don't know
14247 which approach is better. */
14248 b->enable_state = bp_disabled;
14249 throw_exception (e);
14250 }
14251 }
492d29ea 14252 END_CATCH
ef23e705 14253
492d29ea 14254 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14255 {
f8eba3c6 14256 int i;
ef23e705 14257
f8eba3c6
TT
14258 for (i = 0; i < sals.nelts; ++i)
14259 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14260 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14261 {
ed1d1739
KS
14262 char *cond_string, *extra_string;
14263 int thread, task;
ef23e705 14264
f00aae0f 14265 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14266 &cond_string, &thread, &task,
14267 &extra_string);
f00aae0f 14268 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14269 if (cond_string)
14270 b->cond_string = cond_string;
14271 b->thread = thread;
14272 b->task = task;
e7e0cddf 14273 if (extra_string)
f00aae0f
KS
14274 {
14275 xfree (b->extra_string);
14276 b->extra_string = extra_string;
14277 }
ef23e705
TJB
14278 b->condition_not_parsed = 0;
14279 }
14280
983af33b 14281 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14282 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14283
58438ac1
TT
14284 *found = 1;
14285 }
14286 else
14287 *found = 0;
ef23e705
TJB
14288
14289 return sals;
14290}
14291
348d480f
PA
14292/* The default re_set method, for typical hardware or software
14293 breakpoints. Reevaluate the breakpoint and recreate its
14294 locations. */
14295
14296static void
28010a5d 14297breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14298{
14299 int found;
f1310107 14300 struct symtabs_and_lines sals, sals_end;
ef23e705 14301 struct symtabs_and_lines expanded = {0};
f1310107 14302 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14303 struct program_space *filter_pspace = current_program_space;
ef23e705 14304
d28cd78a 14305 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14306 if (found)
14307 {
14308 make_cleanup (xfree, sals.sals);
f8eba3c6 14309 expanded = sals;
ef23e705
TJB
14310 }
14311
f00aae0f 14312 if (b->location_range_end != NULL)
f1310107 14313 {
d28cd78a 14314 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14315 filter_pspace, &found);
f1310107
TJB
14316 if (found)
14317 {
14318 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14319 expanded_end = sals_end;
f1310107
TJB
14320 }
14321 }
14322
c2f4122d 14323 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14324}
14325
983af33b
SDJ
14326/* Default method for creating SALs from an address string. It basically
14327 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14328
14329static void
f00aae0f
KS
14330create_sals_from_location_default (const struct event_location *location,
14331 struct linespec_result *canonical,
14332 enum bptype type_wanted)
983af33b 14333{
f00aae0f 14334 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14335}
14336
14337/* Call create_breakpoints_sal for the given arguments. This is the default
14338 function for the `create_breakpoints_sal' method of
14339 breakpoint_ops. */
14340
14341static void
14342create_breakpoints_sal_default (struct gdbarch *gdbarch,
14343 struct linespec_result *canonical,
e1e01040
PA
14344 gdb::unique_xmalloc_ptr<char> cond_string,
14345 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14346 enum bptype type_wanted,
14347 enum bpdisp disposition,
14348 int thread,
14349 int task, int ignore_count,
14350 const struct breakpoint_ops *ops,
14351 int from_tty, int enabled,
44f238bb 14352 int internal, unsigned flags)
983af33b 14353{
e1e01040
PA
14354 create_breakpoints_sal (gdbarch, canonical,
14355 std::move (cond_string),
14356 std::move (extra_string),
983af33b
SDJ
14357 type_wanted, disposition,
14358 thread, task, ignore_count, ops, from_tty,
44f238bb 14359 enabled, internal, flags);
983af33b
SDJ
14360}
14361
14362/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14363 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14364
14365static void
f00aae0f
KS
14366decode_location_default (struct breakpoint *b,
14367 const struct event_location *location,
c2f4122d 14368 struct program_space *search_pspace,
983af33b
SDJ
14369 struct symtabs_and_lines *sals)
14370{
14371 struct linespec_result canonical;
14372
c2f4122d 14373 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14374 (struct symtab *) NULL, 0,
14375 &canonical, multiple_symbols_all,
14376 b->filter);
14377
14378 /* We should get 0 or 1 resulting SALs. */
14379 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14380
14381 if (VEC_length (linespec_sals, canonical.sals) > 0)
14382 {
14383 struct linespec_sals *lsal;
14384
14385 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14386 *sals = lsal->sals;
14387 /* Arrange it so the destructor does not free the
14388 contents. */
14389 lsal->sals.sals = NULL;
14390 }
983af33b
SDJ
14391}
14392
28010a5d
PA
14393/* Prepare the global context for a re-set of breakpoint B. */
14394
14395static struct cleanup *
14396prepare_re_set_context (struct breakpoint *b)
14397{
28010a5d 14398 input_radix = b->input_radix;
28010a5d
PA
14399 set_language (b->language);
14400
c2f4122d 14401 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14402}
14403
c906108c
SS
14404/* Reset a breakpoint given it's struct breakpoint * BINT.
14405 The value we return ends up being the return value from catch_errors.
14406 Unused in this case. */
14407
14408static int
4efb68b1 14409breakpoint_re_set_one (void *bint)
c906108c 14410{
4a64f543 14411 /* Get past catch_errs. */
53a5351d 14412 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14413 struct cleanup *cleanups;
c906108c 14414
348d480f
PA
14415 cleanups = prepare_re_set_context (b);
14416 b->ops->re_set (b);
14417 do_cleanups (cleanups);
c906108c
SS
14418 return 0;
14419}
14420
c2f4122d
PA
14421/* Re-set breakpoint locations for the current program space.
14422 Locations bound to other program spaces are left untouched. */
14423
c906108c 14424void
69de3c6a 14425breakpoint_re_set (void)
c906108c 14426{
35df4500 14427 struct breakpoint *b, *b_tmp;
c906108c
SS
14428 enum language save_language;
14429 int save_input_radix;
c5aa993b 14430
c906108c
SS
14431 save_language = current_language->la_language;
14432 save_input_radix = input_radix;
2a7f3dff 14433
c5aa993b 14434 {
5ed8105e 14435 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14436
5ed8105e
PA
14437 /* Note: we must not try to insert locations until after all
14438 breakpoints have been re-set. Otherwise, e.g., when re-setting
14439 breakpoint 1, we'd insert the locations of breakpoint 2, which
14440 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14441
5ed8105e
PA
14442 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14443 {
14444 /* Format possible error msg. */
14445 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14446 b->number);
14447 struct cleanup *cleanups = make_cleanup (xfree, message);
14448 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14449 do_cleanups (cleanups);
14450 }
14451 set_language (save_language);
14452 input_radix = save_input_radix;
14453
14454 jit_breakpoint_re_set ();
14455 }
6c95b8df 14456
af02033e
PP
14457 create_overlay_event_breakpoint ();
14458 create_longjmp_master_breakpoint ();
14459 create_std_terminate_master_breakpoint ();
186c406b 14460 create_exception_master_breakpoint ();
2a7f3dff
PA
14461
14462 /* Now we can insert. */
14463 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14464}
14465\f
c906108c
SS
14466/* Reset the thread number of this breakpoint:
14467
14468 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14469 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14470void
fba45db2 14471breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14472{
14473 if (b->thread != -1)
14474 {
39f77062 14475 if (in_thread_list (inferior_ptid))
5d5658a1 14476 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14477
14478 /* We're being called after following a fork. The new fork is
14479 selected as current, and unless this was a vfork will have a
14480 different program space from the original thread. Reset that
14481 as well. */
14482 b->loc->pspace = current_program_space;
c906108c
SS
14483 }
14484}
14485
03ac34d5
MS
14486/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14487 If from_tty is nonzero, it prints a message to that effect,
14488 which ends with a period (no newline). */
14489
c906108c 14490void
fba45db2 14491set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14492{
52f0bd74 14493 struct breakpoint *b;
c906108c
SS
14494
14495 if (count < 0)
14496 count = 0;
14497
14498 ALL_BREAKPOINTS (b)
14499 if (b->number == bptnum)
c5aa993b 14500 {
d77f58be
SS
14501 if (is_tracepoint (b))
14502 {
14503 if (from_tty && count != 0)
14504 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14505 bptnum);
14506 return;
14507 }
14508
c5aa993b 14509 b->ignore_count = count;
221ea385
KS
14510 if (from_tty)
14511 {
14512 if (count == 0)
3e43a32a
MS
14513 printf_filtered (_("Will stop next time "
14514 "breakpoint %d is reached."),
221ea385
KS
14515 bptnum);
14516 else if (count == 1)
a3f17187 14517 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14518 bptnum);
14519 else
3e43a32a
MS
14520 printf_filtered (_("Will ignore next %d "
14521 "crossings of breakpoint %d."),
221ea385
KS
14522 count, bptnum);
14523 }
8d3788bd 14524 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14525 return;
14526 }
c906108c 14527
8a3fe4f8 14528 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14529}
14530
c906108c
SS
14531/* Command to set ignore-count of breakpoint N to COUNT. */
14532
14533static void
fba45db2 14534ignore_command (char *args, int from_tty)
c906108c
SS
14535{
14536 char *p = args;
52f0bd74 14537 int num;
c906108c
SS
14538
14539 if (p == 0)
e2e0b3e5 14540 error_no_arg (_("a breakpoint number"));
c5aa993b 14541
c906108c 14542 num = get_number (&p);
5c44784c 14543 if (num == 0)
8a3fe4f8 14544 error (_("bad breakpoint number: '%s'"), args);
c906108c 14545 if (*p == 0)
8a3fe4f8 14546 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14547
14548 set_ignore_count (num,
14549 longest_to_int (value_as_long (parse_and_eval (p))),
14550 from_tty);
221ea385
KS
14551 if (from_tty)
14552 printf_filtered ("\n");
c906108c
SS
14553}
14554\f
14555/* Call FUNCTION on each of the breakpoints
14556 whose numbers are given in ARGS. */
14557
14558static void
896b6bda
PA
14559map_breakpoint_numbers (const char *args,
14560 void (*function) (struct breakpoint *,
14561 void *),
95a42b64 14562 void *data)
c906108c 14563{
52f0bd74
AC
14564 int num;
14565 struct breakpoint *b, *tmp;
c906108c 14566
b9d61307 14567 if (args == 0 || *args == '\0')
e2e0b3e5 14568 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14569
bfd28288 14570 number_or_range_parser parser (args);
197f0a60 14571
bfd28288 14572 while (!parser.finished ())
c906108c 14573 {
bfd28288
PA
14574 const char *p = parser.cur_tok ();
14575 bool match = false;
197f0a60 14576
bfd28288 14577 num = parser.get_number ();
5c44784c 14578 if (num == 0)
c5aa993b 14579 {
8a3fe4f8 14580 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14581 }
14582 else
14583 {
14584 ALL_BREAKPOINTS_SAFE (b, tmp)
14585 if (b->number == num)
14586 {
bfd28288 14587 match = true;
cdac0397 14588 function (b, data);
11cf8741 14589 break;
5c44784c 14590 }
bfd28288 14591 if (!match)
a3f17187 14592 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14593 }
c906108c
SS
14594 }
14595}
14596
0d381245
VP
14597static struct bp_location *
14598find_location_by_number (char *number)
14599{
14600 char *dot = strchr (number, '.');
14601 char *p1;
14602 int bp_num;
14603 int loc_num;
14604 struct breakpoint *b;
14605 struct bp_location *loc;
14606
14607 *dot = '\0';
14608
14609 p1 = number;
197f0a60 14610 bp_num = get_number (&p1);
0d381245
VP
14611 if (bp_num == 0)
14612 error (_("Bad breakpoint number '%s'"), number);
14613
14614 ALL_BREAKPOINTS (b)
14615 if (b->number == bp_num)
14616 {
14617 break;
14618 }
14619
14620 if (!b || b->number != bp_num)
14621 error (_("Bad breakpoint number '%s'"), number);
14622
14623 p1 = dot+1;
197f0a60 14624 loc_num = get_number (&p1);
0d381245
VP
14625 if (loc_num == 0)
14626 error (_("Bad breakpoint location number '%s'"), number);
14627
14628 --loc_num;
14629 loc = b->loc;
14630 for (;loc_num && loc; --loc_num, loc = loc->next)
14631 ;
14632 if (!loc)
14633 error (_("Bad breakpoint location number '%s'"), dot+1);
14634
14635 return loc;
14636}
14637
14638
1900040c
MS
14639/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14640 If from_tty is nonzero, it prints a message to that effect,
14641 which ends with a period (no newline). */
14642
c906108c 14643void
fba45db2 14644disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14645{
14646 /* Never disable a watchpoint scope breakpoint; we want to
14647 hit them when we leave scope so we can delete both the
14648 watchpoint and its scope breakpoint at that time. */
14649 if (bpt->type == bp_watchpoint_scope)
14650 return;
14651
b5de0fa7 14652 bpt->enable_state = bp_disabled;
c906108c 14653
b775012e
LM
14654 /* Mark breakpoint locations modified. */
14655 mark_breakpoint_modified (bpt);
14656
d248b706
KY
14657 if (target_supports_enable_disable_tracepoint ()
14658 && current_trace_status ()->running && is_tracepoint (bpt))
14659 {
14660 struct bp_location *location;
14661
14662 for (location = bpt->loc; location; location = location->next)
14663 target_disable_tracepoint (location);
14664 }
14665
44702360 14666 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14667
8d3788bd 14668 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14669}
14670
51be5b68
PA
14671/* A callback for iterate_over_related_breakpoints. */
14672
14673static void
14674do_disable_breakpoint (struct breakpoint *b, void *ignore)
14675{
14676 disable_breakpoint (b);
14677}
14678
95a42b64
TT
14679/* A callback for map_breakpoint_numbers that calls
14680 disable_breakpoint. */
14681
14682static void
14683do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14684{
51be5b68 14685 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14686}
14687
c906108c 14688static void
fba45db2 14689disable_command (char *args, int from_tty)
c906108c 14690{
c906108c 14691 if (args == 0)
46c6471b
PA
14692 {
14693 struct breakpoint *bpt;
14694
14695 ALL_BREAKPOINTS (bpt)
14696 if (user_breakpoint_p (bpt))
14697 disable_breakpoint (bpt);
14698 }
9eaabc75 14699 else
0d381245 14700 {
9eaabc75
MW
14701 char *num = extract_arg (&args);
14702
14703 while (num)
d248b706 14704 {
9eaabc75 14705 if (strchr (num, '.'))
b775012e 14706 {
9eaabc75
MW
14707 struct bp_location *loc = find_location_by_number (num);
14708
14709 if (loc)
14710 {
14711 if (loc->enabled)
14712 {
14713 loc->enabled = 0;
14714 mark_breakpoint_location_modified (loc);
14715 }
14716 if (target_supports_enable_disable_tracepoint ()
14717 && current_trace_status ()->running && loc->owner
14718 && is_tracepoint (loc->owner))
14719 target_disable_tracepoint (loc);
14720 }
44702360 14721 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14722 }
9eaabc75
MW
14723 else
14724 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14725 num = extract_arg (&args);
d248b706 14726 }
0d381245 14727 }
c906108c
SS
14728}
14729
14730static void
816338b5
SS
14731enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14732 int count)
c906108c 14733{
afe38095 14734 int target_resources_ok;
c906108c
SS
14735
14736 if (bpt->type == bp_hardware_breakpoint)
14737 {
14738 int i;
c5aa993b 14739 i = hw_breakpoint_used_count ();
53a5351d 14740 target_resources_ok =
d92524f1 14741 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14742 i + 1, 0);
c906108c 14743 if (target_resources_ok == 0)
8a3fe4f8 14744 error (_("No hardware breakpoint support in the target."));
c906108c 14745 else if (target_resources_ok < 0)
8a3fe4f8 14746 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14747 }
14748
cc60f2e3 14749 if (is_watchpoint (bpt))
c906108c 14750 {
d07205c2 14751 /* Initialize it just to avoid a GCC false warning. */
f486487f 14752 enum enable_state orig_enable_state = bp_disabled;
dde02812 14753
492d29ea 14754 TRY
c906108c 14755 {
3a5c3e22
PA
14756 struct watchpoint *w = (struct watchpoint *) bpt;
14757
1e718ff1
TJB
14758 orig_enable_state = bpt->enable_state;
14759 bpt->enable_state = bp_enabled;
3a5c3e22 14760 update_watchpoint (w, 1 /* reparse */);
c906108c 14761 }
492d29ea 14762 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14763 {
1e718ff1 14764 bpt->enable_state = orig_enable_state;
dde02812
ES
14765 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14766 bpt->number);
14767 return;
c5aa993b 14768 }
492d29ea 14769 END_CATCH
c906108c 14770 }
0101ce28 14771
b775012e
LM
14772 bpt->enable_state = bp_enabled;
14773
14774 /* Mark breakpoint locations modified. */
14775 mark_breakpoint_modified (bpt);
14776
d248b706
KY
14777 if (target_supports_enable_disable_tracepoint ()
14778 && current_trace_status ()->running && is_tracepoint (bpt))
14779 {
14780 struct bp_location *location;
14781
14782 for (location = bpt->loc; location; location = location->next)
14783 target_enable_tracepoint (location);
14784 }
14785
b4c291bb 14786 bpt->disposition = disposition;
816338b5 14787 bpt->enable_count = count;
44702360 14788 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14789
8d3788bd 14790 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14791}
14792
fe3f5fa8 14793
c906108c 14794void
fba45db2 14795enable_breakpoint (struct breakpoint *bpt)
c906108c 14796{
816338b5 14797 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14798}
14799
14800static void
14801do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14802{
14803 enable_breakpoint (bpt);
c906108c
SS
14804}
14805
95a42b64
TT
14806/* A callback for map_breakpoint_numbers that calls
14807 enable_breakpoint. */
14808
14809static void
14810do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14811{
51be5b68 14812 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14813}
14814
c906108c
SS
14815/* The enable command enables the specified breakpoints (or all defined
14816 breakpoints) so they once again become (or continue to be) effective
1272ad14 14817 in stopping the inferior. */
c906108c 14818
c906108c 14819static void
fba45db2 14820enable_command (char *args, int from_tty)
c906108c 14821{
c906108c 14822 if (args == 0)
46c6471b
PA
14823 {
14824 struct breakpoint *bpt;
14825
14826 ALL_BREAKPOINTS (bpt)
14827 if (user_breakpoint_p (bpt))
14828 enable_breakpoint (bpt);
14829 }
9eaabc75 14830 else
0d381245 14831 {
9eaabc75
MW
14832 char *num = extract_arg (&args);
14833
14834 while (num)
d248b706 14835 {
9eaabc75 14836 if (strchr (num, '.'))
b775012e 14837 {
9eaabc75
MW
14838 struct bp_location *loc = find_location_by_number (num);
14839
14840 if (loc)
14841 {
14842 if (!loc->enabled)
14843 {
14844 loc->enabled = 1;
14845 mark_breakpoint_location_modified (loc);
14846 }
14847 if (target_supports_enable_disable_tracepoint ()
14848 && current_trace_status ()->running && loc->owner
14849 && is_tracepoint (loc->owner))
14850 target_enable_tracepoint (loc);
14851 }
44702360 14852 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14853 }
9eaabc75
MW
14854 else
14855 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14856 num = extract_arg (&args);
d248b706 14857 }
0d381245 14858 }
c906108c
SS
14859}
14860
816338b5
SS
14861/* This struct packages up disposition data for application to multiple
14862 breakpoints. */
14863
14864struct disp_data
14865{
14866 enum bpdisp disp;
14867 int count;
14868};
14869
c906108c 14870static void
51be5b68
PA
14871do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14872{
816338b5 14873 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14874
816338b5 14875 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14876}
14877
14878static void
14879do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14880{
816338b5 14881 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14882
14883 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14884}
14885
c906108c 14886static void
fba45db2 14887enable_once_command (char *args, int from_tty)
c906108c 14888{
51be5b68 14889 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14890}
14891
816338b5
SS
14892static void
14893do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14894{
14895 struct disp_data disp = { disp_disable, *(int *) countptr };
14896
14897 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14898}
14899
14900static void
14901enable_count_command (char *args, int from_tty)
14902{
b9d61307
SM
14903 int count;
14904
14905 if (args == NULL)
14906 error_no_arg (_("hit count"));
14907
14908 count = get_number (&args);
816338b5
SS
14909
14910 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14911}
14912
c906108c 14913static void
51be5b68 14914do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14915{
816338b5 14916 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14917
14918 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14919}
14920
c906108c 14921static void
fba45db2 14922enable_delete_command (char *args, int from_tty)
c906108c 14923{
51be5b68 14924 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14925}
14926\f
fa8d40ab
JJ
14927static void
14928set_breakpoint_cmd (char *args, int from_tty)
14929{
14930}
14931
14932static void
14933show_breakpoint_cmd (char *args, int from_tty)
14934{
14935}
14936
1f3b5d1b
PP
14937/* Invalidate last known value of any hardware watchpoint if
14938 the memory which that value represents has been written to by
14939 GDB itself. */
14940
14941static void
8de0566d
YQ
14942invalidate_bp_value_on_memory_change (struct inferior *inferior,
14943 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14944 const bfd_byte *data)
14945{
14946 struct breakpoint *bp;
14947
14948 ALL_BREAKPOINTS (bp)
14949 if (bp->enable_state == bp_enabled
3a5c3e22 14950 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14951 {
3a5c3e22 14952 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14953
3a5c3e22
PA
14954 if (wp->val_valid && wp->val)
14955 {
14956 struct bp_location *loc;
14957
14958 for (loc = bp->loc; loc != NULL; loc = loc->next)
14959 if (loc->loc_type == bp_loc_hardware_watchpoint
14960 && loc->address + loc->length > addr
14961 && addr + len > loc->address)
14962 {
14963 value_free (wp->val);
14964 wp->val = NULL;
14965 wp->val_valid = 0;
14966 }
14967 }
1f3b5d1b
PP
14968 }
14969}
14970
8181d85f
DJ
14971/* Create and insert a breakpoint for software single step. */
14972
14973void
6c95b8df 14974insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14975 struct address_space *aspace,
14976 CORE_ADDR next_pc)
8181d85f 14977{
7c16b83e
PA
14978 struct thread_info *tp = inferior_thread ();
14979 struct symtab_and_line sal;
14980 CORE_ADDR pc = next_pc;
8181d85f 14981
34b7e8a6
PA
14982 if (tp->control.single_step_breakpoints == NULL)
14983 {
14984 tp->control.single_step_breakpoints
5d5658a1 14985 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14986 }
8181d85f 14987
7c16b83e
PA
14988 sal = find_pc_line (pc, 0);
14989 sal.pc = pc;
14990 sal.section = find_pc_overlay (pc);
14991 sal.explicit_pc = 1;
34b7e8a6 14992 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14993
7c16b83e 14994 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14995}
14996
93f9a11f
YQ
14997/* Insert single step breakpoints according to the current state. */
14998
14999int
15000insert_single_step_breakpoints (struct gdbarch *gdbarch)
15001{
f5ea389a 15002 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 15003 std::vector<CORE_ADDR> next_pcs;
93f9a11f 15004
f5ea389a 15005 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 15006
a0ff9e1a 15007 if (!next_pcs.empty ())
93f9a11f 15008 {
f5ea389a 15009 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
15010 struct address_space *aspace = get_frame_address_space (frame);
15011
a0ff9e1a 15012 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
15013 insert_single_step_breakpoint (gdbarch, aspace, pc);
15014
93f9a11f
YQ
15015 return 1;
15016 }
15017 else
15018 return 0;
15019}
15020
34b7e8a6 15021/* See breakpoint.h. */
f02253f1
HZ
15022
15023int
7c16b83e
PA
15024breakpoint_has_location_inserted_here (struct breakpoint *bp,
15025 struct address_space *aspace,
15026 CORE_ADDR pc)
1aafd4da 15027{
7c16b83e 15028 struct bp_location *loc;
1aafd4da 15029
7c16b83e
PA
15030 for (loc = bp->loc; loc != NULL; loc = loc->next)
15031 if (loc->inserted
15032 && breakpoint_location_address_match (loc, aspace, pc))
15033 return 1;
1aafd4da 15034
7c16b83e 15035 return 0;
ef370185
JB
15036}
15037
15038/* Check whether a software single-step breakpoint is inserted at
15039 PC. */
15040
15041int
15042single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15043 CORE_ADDR pc)
15044{
34b7e8a6
PA
15045 struct breakpoint *bpt;
15046
15047 ALL_BREAKPOINTS (bpt)
15048 {
15049 if (bpt->type == bp_single_step
15050 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15051 return 1;
15052 }
15053 return 0;
1aafd4da
UW
15054}
15055
1042e4c0
SS
15056/* Tracepoint-specific operations. */
15057
15058/* Set tracepoint count to NUM. */
15059static void
15060set_tracepoint_count (int num)
15061{
15062 tracepoint_count = num;
4fa62494 15063 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15064}
15065
70221824 15066static void
1042e4c0
SS
15067trace_command (char *arg, int from_tty)
15068{
55aa24fb 15069 struct breakpoint_ops *ops;
55aa24fb 15070
ffc2605c
TT
15071 event_location_up location = string_to_event_location (&arg,
15072 current_language);
5b56227b 15073 if (location != NULL
ffc2605c 15074 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15075 ops = &tracepoint_probe_breakpoint_ops;
15076 else
15077 ops = &tracepoint_breakpoint_ops;
15078
558a9d82 15079 create_breakpoint (get_current_arch (),
ffc2605c 15080 location.get (),
f00aae0f 15081 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15082 0 /* tempflag */,
15083 bp_tracepoint /* type_wanted */,
15084 0 /* Ignore count */,
15085 pending_break_support,
15086 ops,
15087 from_tty,
15088 1 /* enabled */,
15089 0 /* internal */, 0);
1042e4c0
SS
15090}
15091
70221824 15092static void
7a697b8d
SS
15093ftrace_command (char *arg, int from_tty)
15094{
ffc2605c
TT
15095 event_location_up location = string_to_event_location (&arg,
15096 current_language);
558a9d82 15097 create_breakpoint (get_current_arch (),
ffc2605c 15098 location.get (),
f00aae0f 15099 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15100 0 /* tempflag */,
15101 bp_fast_tracepoint /* type_wanted */,
15102 0 /* Ignore count */,
15103 pending_break_support,
15104 &tracepoint_breakpoint_ops,
15105 from_tty,
15106 1 /* enabled */,
15107 0 /* internal */, 0);
0fb4aa4b
PA
15108}
15109
15110/* strace command implementation. Creates a static tracepoint. */
15111
70221824 15112static void
0fb4aa4b
PA
15113strace_command (char *arg, int from_tty)
15114{
983af33b 15115 struct breakpoint_ops *ops;
ffc2605c 15116 event_location_up location;
f00aae0f 15117 struct cleanup *back_to;
983af33b
SDJ
15118
15119 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15120 or with a normal static tracepoint. */
61012eef 15121 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15122 {
15123 ops = &strace_marker_breakpoint_ops;
15124 location = new_linespec_location (&arg);
15125 }
983af33b 15126 else
f00aae0f
KS
15127 {
15128 ops = &tracepoint_breakpoint_ops;
15129 location = string_to_event_location (&arg, current_language);
15130 }
983af33b 15131
558a9d82 15132 create_breakpoint (get_current_arch (),
ffc2605c 15133 location.get (),
f00aae0f 15134 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15135 0 /* tempflag */,
15136 bp_static_tracepoint /* type_wanted */,
15137 0 /* Ignore count */,
15138 pending_break_support,
15139 ops,
15140 from_tty,
15141 1 /* enabled */,
15142 0 /* internal */, 0);
7a697b8d
SS
15143}
15144
409873ef
SS
15145/* Set up a fake reader function that gets command lines from a linked
15146 list that was acquired during tracepoint uploading. */
15147
15148static struct uploaded_tp *this_utp;
3149d8c1 15149static int next_cmd;
409873ef
SS
15150
15151static char *
15152read_uploaded_action (void)
15153{
15154 char *rslt;
15155
3149d8c1 15156 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15157
3149d8c1 15158 next_cmd++;
409873ef
SS
15159
15160 return rslt;
15161}
15162
00bf0b85
SS
15163/* Given information about a tracepoint as recorded on a target (which
15164 can be either a live system or a trace file), attempt to create an
15165 equivalent GDB tracepoint. This is not a reliable process, since
15166 the target does not necessarily have all the information used when
15167 the tracepoint was originally defined. */
15168
d9b3f62e 15169struct tracepoint *
00bf0b85 15170create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15171{
409873ef 15172 char *addr_str, small_buf[100];
d9b3f62e 15173 struct tracepoint *tp;
fd9b8c24 15174
409873ef
SS
15175 if (utp->at_string)
15176 addr_str = utp->at_string;
15177 else
15178 {
15179 /* In the absence of a source location, fall back to raw
15180 address. Since there is no way to confirm that the address
15181 means the same thing as when the trace was started, warn the
15182 user. */
3e43a32a
MS
15183 warning (_("Uploaded tracepoint %d has no "
15184 "source location, using raw address"),
409873ef 15185 utp->number);
8c042590 15186 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15187 addr_str = small_buf;
15188 }
15189
15190 /* There's not much we can do with a sequence of bytecodes. */
15191 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15192 warning (_("Uploaded tracepoint %d condition "
15193 "has no source form, ignoring it"),
409873ef 15194 utp->number);
d5551862 15195
ffc2605c
TT
15196 event_location_up location = string_to_event_location (&addr_str,
15197 current_language);
8cdf0e15 15198 if (!create_breakpoint (get_current_arch (),
ffc2605c 15199 location.get (),
f00aae0f 15200 utp->cond_string, -1, addr_str,
e7e0cddf 15201 0 /* parse cond/thread */,
8cdf0e15 15202 0 /* tempflag */,
0fb4aa4b 15203 utp->type /* type_wanted */,
8cdf0e15
VP
15204 0 /* Ignore count */,
15205 pending_break_support,
348d480f 15206 &tracepoint_breakpoint_ops,
8cdf0e15 15207 0 /* from_tty */,
84f4c1fe 15208 utp->enabled /* enabled */,
44f238bb
PA
15209 0 /* internal */,
15210 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15211 return NULL;
fd9b8c24 15212
409873ef 15213 /* Get the tracepoint we just created. */
fd9b8c24
PA
15214 tp = get_tracepoint (tracepoint_count);
15215 gdb_assert (tp != NULL);
d5551862 15216
00bf0b85
SS
15217 if (utp->pass > 0)
15218 {
8c042590 15219 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 15220 tp->number);
00bf0b85 15221
409873ef 15222 trace_pass_command (small_buf, 0);
00bf0b85
SS
15223 }
15224
409873ef
SS
15225 /* If we have uploaded versions of the original commands, set up a
15226 special-purpose "reader" function and call the usual command line
15227 reader, then pass the result to the breakpoint command-setting
15228 function. */
3149d8c1 15229 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15230 {
93921405 15231 command_line_up cmd_list;
00bf0b85 15232
409873ef 15233 this_utp = utp;
3149d8c1 15234 next_cmd = 0;
d5551862 15235
409873ef
SS
15236 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15237
c1fc2657 15238 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 15239 }
3149d8c1
SS
15240 else if (!VEC_empty (char_ptr, utp->actions)
15241 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15242 warning (_("Uploaded tracepoint %d actions "
15243 "have no source form, ignoring them"),
409873ef 15244 utp->number);
00bf0b85 15245
f196051f 15246 /* Copy any status information that might be available. */
c1fc2657 15247 tp->hit_count = utp->hit_count;
f196051f
SS
15248 tp->traceframe_usage = utp->traceframe_usage;
15249
00bf0b85 15250 return tp;
d9b3f62e 15251}
00bf0b85 15252
1042e4c0
SS
15253/* Print information on tracepoint number TPNUM_EXP, or all if
15254 omitted. */
15255
15256static void
e5a67952 15257tracepoints_info (char *args, int from_tty)
1042e4c0 15258{
79a45e25 15259 struct ui_out *uiout = current_uiout;
e5a67952 15260 int num_printed;
1042e4c0 15261
e5a67952 15262 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15263
15264 if (num_printed == 0)
1042e4c0 15265 {
e5a67952 15266 if (args == NULL || *args == '\0')
112e8700 15267 uiout->message ("No tracepoints.\n");
d77f58be 15268 else
112e8700 15269 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15270 }
ad443146
SS
15271
15272 default_collect_info ();
1042e4c0
SS
15273}
15274
4a64f543 15275/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15276 Not supported by all targets. */
15277static void
15278enable_trace_command (char *args, int from_tty)
15279{
15280 enable_command (args, from_tty);
15281}
15282
4a64f543 15283/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15284 Not supported by all targets. */
15285static void
15286disable_trace_command (char *args, int from_tty)
15287{
15288 disable_command (args, from_tty);
15289}
15290
4a64f543 15291/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15292static void
15293delete_trace_command (char *arg, int from_tty)
15294{
35df4500 15295 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15296
15297 dont_repeat ();
15298
15299 if (arg == 0)
15300 {
15301 int breaks_to_delete = 0;
15302
15303 /* Delete all breakpoints if no argument.
15304 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15305 have to be deleted with an explicit breakpoint number
15306 argument. */
1042e4c0 15307 ALL_TRACEPOINTS (b)
46c6471b 15308 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15309 {
15310 breaks_to_delete = 1;
15311 break;
15312 }
1042e4c0
SS
15313
15314 /* Ask user only if there are some breakpoints to delete. */
15315 if (!from_tty
15316 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15317 {
35df4500 15318 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15319 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15320 delete_breakpoint (b);
1042e4c0
SS
15321 }
15322 }
15323 else
51be5b68 15324 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15325}
15326
197f0a60
TT
15327/* Helper function for trace_pass_command. */
15328
15329static void
d9b3f62e 15330trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15331{
d9b3f62e 15332 tp->pass_count = count;
c1fc2657 15333 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15334 if (from_tty)
15335 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15336 tp->number, count);
197f0a60
TT
15337}
15338
1042e4c0
SS
15339/* Set passcount for tracepoint.
15340
15341 First command argument is passcount, second is tracepoint number.
15342 If tracepoint number omitted, apply to most recently defined.
15343 Also accepts special argument "all". */
15344
15345static void
15346trace_pass_command (char *args, int from_tty)
15347{
d9b3f62e 15348 struct tracepoint *t1;
1042e4c0 15349 unsigned int count;
1042e4c0
SS
15350
15351 if (args == 0 || *args == 0)
3e43a32a
MS
15352 error (_("passcount command requires an "
15353 "argument (count + optional TP num)"));
1042e4c0 15354
4a64f543 15355 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15356
529480d0 15357 args = skip_spaces (args);
1042e4c0
SS
15358 if (*args && strncasecmp (args, "all", 3) == 0)
15359 {
d9b3f62e
PA
15360 struct breakpoint *b;
15361
1042e4c0 15362 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15363 if (*args)
15364 error (_("Junk at end of arguments."));
1042e4c0 15365
d9b3f62e 15366 ALL_TRACEPOINTS (b)
197f0a60 15367 {
d9b3f62e 15368 t1 = (struct tracepoint *) b;
197f0a60
TT
15369 trace_pass_set_count (t1, count, from_tty);
15370 }
15371 }
15372 else if (*args == '\0')
1042e4c0 15373 {
5fa1d40e 15374 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15375 if (t1)
197f0a60
TT
15376 trace_pass_set_count (t1, count, from_tty);
15377 }
15378 else
15379 {
bfd28288
PA
15380 number_or_range_parser parser (args);
15381 while (!parser.finished ())
1042e4c0 15382 {
bfd28288 15383 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15384 if (t1)
15385 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15386 }
15387 }
1042e4c0
SS
15388}
15389
d9b3f62e 15390struct tracepoint *
1042e4c0
SS
15391get_tracepoint (int num)
15392{
15393 struct breakpoint *t;
15394
15395 ALL_TRACEPOINTS (t)
15396 if (t->number == num)
d9b3f62e 15397 return (struct tracepoint *) t;
1042e4c0
SS
15398
15399 return NULL;
15400}
15401
d5551862
SS
15402/* Find the tracepoint with the given target-side number (which may be
15403 different from the tracepoint number after disconnecting and
15404 reconnecting). */
15405
d9b3f62e 15406struct tracepoint *
d5551862
SS
15407get_tracepoint_by_number_on_target (int num)
15408{
d9b3f62e 15409 struct breakpoint *b;
d5551862 15410
d9b3f62e
PA
15411 ALL_TRACEPOINTS (b)
15412 {
15413 struct tracepoint *t = (struct tracepoint *) b;
15414
15415 if (t->number_on_target == num)
15416 return t;
15417 }
d5551862
SS
15418
15419 return NULL;
15420}
15421
1042e4c0 15422/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15423 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15424 If the argument is missing, the most recent tracepoint
15425 (tracepoint_count) is returned. */
15426
d9b3f62e 15427struct tracepoint *
197f0a60 15428get_tracepoint_by_number (char **arg,
bfd28288 15429 number_or_range_parser *parser)
1042e4c0 15430{
1042e4c0
SS
15431 struct breakpoint *t;
15432 int tpnum;
15433 char *instring = arg == NULL ? NULL : *arg;
15434
bfd28288 15435 if (parser != NULL)
197f0a60 15436 {
bfd28288
PA
15437 gdb_assert (!parser->finished ());
15438 tpnum = parser->get_number ();
197f0a60
TT
15439 }
15440 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15441 tpnum = tracepoint_count;
1042e4c0 15442 else
197f0a60 15443 tpnum = get_number (arg);
1042e4c0
SS
15444
15445 if (tpnum <= 0)
15446 {
15447 if (instring && *instring)
15448 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15449 instring);
15450 else
5fa1d40e 15451 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15452 return NULL;
15453 }
15454
15455 ALL_TRACEPOINTS (t)
15456 if (t->number == tpnum)
15457 {
d9b3f62e 15458 return (struct tracepoint *) t;
1042e4c0
SS
15459 }
15460
1042e4c0
SS
15461 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15462 return NULL;
15463}
15464
d9b3f62e
PA
15465void
15466print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15467{
15468 if (b->thread != -1)
15469 fprintf_unfiltered (fp, " thread %d", b->thread);
15470
15471 if (b->task != 0)
15472 fprintf_unfiltered (fp, " task %d", b->task);
15473
15474 fprintf_unfiltered (fp, "\n");
15475}
15476
6149aea9
PA
15477/* Save information on user settable breakpoints (watchpoints, etc) to
15478 a new script file named FILENAME. If FILTER is non-NULL, call it
15479 on each breakpoint and only include the ones for which it returns
15480 non-zero. */
15481
1042e4c0 15482static void
6149aea9
PA
15483save_breakpoints (char *filename, int from_tty,
15484 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15485{
15486 struct breakpoint *tp;
6149aea9 15487 int any = 0;
1042e4c0 15488 struct cleanup *cleanup;
6149aea9 15489 int extra_trace_bits = 0;
1042e4c0 15490
6149aea9
PA
15491 if (filename == 0 || *filename == 0)
15492 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15493
15494 /* See if we have anything to save. */
6149aea9 15495 ALL_BREAKPOINTS (tp)
1042e4c0 15496 {
6149aea9 15497 /* Skip internal and momentary breakpoints. */
09d682a4 15498 if (!user_breakpoint_p (tp))
6149aea9
PA
15499 continue;
15500
15501 /* If we have a filter, only save the breakpoints it accepts. */
15502 if (filter && !filter (tp))
15503 continue;
15504
15505 any = 1;
15506
15507 if (is_tracepoint (tp))
15508 {
15509 extra_trace_bits = 1;
15510
15511 /* We can stop searching. */
15512 break;
15513 }
1042e4c0 15514 }
6149aea9
PA
15515
15516 if (!any)
1042e4c0 15517 {
6149aea9 15518 warning (_("Nothing to save."));
1042e4c0
SS
15519 return;
15520 }
15521
c718be47
PA
15522 filename = tilde_expand (filename);
15523 cleanup = make_cleanup (xfree, filename);
d7e74731
PA
15524
15525 stdio_file fp;
15526
15527 if (!fp.open (filename, "w"))
6149aea9
PA
15528 error (_("Unable to open file '%s' for saving (%s)"),
15529 filename, safe_strerror (errno));
8bf6485c 15530
6149aea9 15531 if (extra_trace_bits)
d7e74731 15532 save_trace_state_variables (&fp);
8bf6485c 15533
6149aea9 15534 ALL_BREAKPOINTS (tp)
1042e4c0 15535 {
6149aea9 15536 /* Skip internal and momentary breakpoints. */
09d682a4 15537 if (!user_breakpoint_p (tp))
6149aea9 15538 continue;
8bf6485c 15539
6149aea9
PA
15540 /* If we have a filter, only save the breakpoints it accepts. */
15541 if (filter && !filter (tp))
15542 continue;
15543
d7e74731 15544 tp->ops->print_recreate (tp, &fp);
1042e4c0 15545
6149aea9
PA
15546 /* Note, we can't rely on tp->number for anything, as we can't
15547 assume the recreated breakpoint numbers will match. Use $bpnum
15548 instead. */
15549
15550 if (tp->cond_string)
d7e74731 15551 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15552
15553 if (tp->ignore_count)
d7e74731 15554 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15555
2d9442cc 15556 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15557 {
d7e74731 15558 fp.puts (" commands\n");
a7bdde9e 15559
d7e74731 15560 current_uiout->redirect (&fp);
492d29ea 15561 TRY
1042e4c0 15562 {
79a45e25 15563 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15564 }
492d29ea
PA
15565 CATCH (ex, RETURN_MASK_ALL)
15566 {
112e8700 15567 current_uiout->redirect (NULL);
492d29ea
PA
15568 throw_exception (ex);
15569 }
15570 END_CATCH
1042e4c0 15571
112e8700 15572 current_uiout->redirect (NULL);
d7e74731 15573 fp.puts (" end\n");
1042e4c0 15574 }
6149aea9
PA
15575
15576 if (tp->enable_state == bp_disabled)
d7e74731 15577 fp.puts ("disable $bpnum\n");
6149aea9
PA
15578
15579 /* If this is a multi-location breakpoint, check if the locations
15580 should be individually disabled. Watchpoint locations are
15581 special, and not user visible. */
15582 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15583 {
15584 struct bp_location *loc;
15585 int n = 1;
15586
15587 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15588 if (!loc->enabled)
d7e74731 15589 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15590 }
1042e4c0 15591 }
8bf6485c 15592
6149aea9 15593 if (extra_trace_bits && *default_collect)
d7e74731 15594 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15595
1042e4c0 15596 if (from_tty)
6149aea9 15597 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15598 do_cleanups (cleanup);
6149aea9
PA
15599}
15600
15601/* The `save breakpoints' command. */
15602
15603static void
15604save_breakpoints_command (char *args, int from_tty)
15605{
15606 save_breakpoints (args, from_tty, NULL);
15607}
15608
15609/* The `save tracepoints' command. */
15610
15611static void
15612save_tracepoints_command (char *args, int from_tty)
15613{
15614 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15615}
15616
15617/* Create a vector of all tracepoints. */
15618
15619VEC(breakpoint_p) *
eeae04df 15620all_tracepoints (void)
1042e4c0
SS
15621{
15622 VEC(breakpoint_p) *tp_vec = 0;
15623 struct breakpoint *tp;
15624
15625 ALL_TRACEPOINTS (tp)
15626 {
15627 VEC_safe_push (breakpoint_p, tp_vec, tp);
15628 }
15629
15630 return tp_vec;
15631}
15632
c906108c 15633\f
629500fa
KS
15634/* This help string is used to consolidate all the help string for specifying
15635 locations used by several commands. */
15636
15637#define LOCATION_HELP_STRING \
15638"Linespecs are colon-separated lists of location parameters, such as\n\
15639source filename, function name, label name, and line number.\n\
15640Example: To specify the start of a label named \"the_top\" in the\n\
15641function \"fact\" in the file \"factorial.c\", use\n\
15642\"factorial.c:fact:the_top\".\n\
15643\n\
15644Address locations begin with \"*\" and specify an exact address in the\n\
15645program. Example: To specify the fourth byte past the start function\n\
15646\"main\", use \"*main + 4\".\n\
15647\n\
15648Explicit locations are similar to linespecs but use an option/argument\n\
15649syntax to specify location parameters.\n\
15650Example: To specify the start of the label named \"the_top\" in the\n\
15651function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15652-function fact -label the_top\".\n"
15653
4a64f543
MS
15654/* This help string is used for the break, hbreak, tbreak and thbreak
15655 commands. It is defined as a macro to prevent duplication.
15656 COMMAND should be a string constant containing the name of the
15657 command. */
629500fa 15658
31e2b00f 15659#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15660command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15661PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15662probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15663guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15664`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15665LOCATION may be a linespec, address, or explicit location as described\n\
15666below.\n\
15667\n\
dc10affe
PA
15668With no LOCATION, uses current execution address of the selected\n\
15669stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15670\n\
15671THREADNUM is the number from \"info threads\".\n\
15672CONDITION is a boolean expression.\n\
629500fa 15673\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15674Multiple breakpoints at one place are permitted, and useful if their\n\
15675conditions are different.\n\
31e2b00f
AS
15676\n\
15677Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15678
44feb3ce
TT
15679/* List of subcommands for "catch". */
15680static struct cmd_list_element *catch_cmdlist;
15681
15682/* List of subcommands for "tcatch". */
15683static struct cmd_list_element *tcatch_cmdlist;
15684
9ac4176b 15685void
a121b7c1 15686add_catch_command (const char *name, const char *docstring,
82ae6c8d 15687 cmd_sfunc_ftype *sfunc,
625e8578 15688 completer_ftype *completer,
44feb3ce
TT
15689 void *user_data_catch,
15690 void *user_data_tcatch)
15691{
15692 struct cmd_list_element *command;
15693
15694 command = add_cmd (name, class_breakpoint, NULL, docstring,
15695 &catch_cmdlist);
15696 set_cmd_sfunc (command, sfunc);
15697 set_cmd_context (command, user_data_catch);
a96d9b2e 15698 set_cmd_completer (command, completer);
44feb3ce
TT
15699
15700 command = add_cmd (name, class_breakpoint, NULL, docstring,
15701 &tcatch_cmdlist);
15702 set_cmd_sfunc (command, sfunc);
15703 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15704 set_cmd_completer (command, completer);
44feb3ce
TT
15705}
15706
6149aea9
PA
15707static void
15708save_command (char *arg, int from_tty)
15709{
3e43a32a
MS
15710 printf_unfiltered (_("\"save\" must be followed by "
15711 "the name of a save subcommand.\n"));
635c7e8a 15712 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15713}
15714
84f4c1fe
PM
15715struct breakpoint *
15716iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15717 void *data)
15718{
35df4500 15719 struct breakpoint *b, *b_tmp;
84f4c1fe 15720
35df4500 15721 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15722 {
15723 if ((*callback) (b, data))
15724 return b;
15725 }
15726
15727 return NULL;
15728}
15729
0574c78f
GB
15730/* Zero if any of the breakpoint's locations could be a location where
15731 functions have been inlined, nonzero otherwise. */
15732
15733static int
15734is_non_inline_function (struct breakpoint *b)
15735{
15736 /* The shared library event breakpoint is set on the address of a
15737 non-inline function. */
15738 if (b->type == bp_shlib_event)
15739 return 1;
15740
15741 return 0;
15742}
15743
15744/* Nonzero if the specified PC cannot be a location where functions
15745 have been inlined. */
15746
15747int
09ac7c10
TT
15748pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15749 const struct target_waitstatus *ws)
0574c78f
GB
15750{
15751 struct breakpoint *b;
15752 struct bp_location *bl;
15753
15754 ALL_BREAKPOINTS (b)
15755 {
15756 if (!is_non_inline_function (b))
15757 continue;
15758
15759 for (bl = b->loc; bl != NULL; bl = bl->next)
15760 {
15761 if (!bl->shlib_disabled
09ac7c10 15762 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15763 return 1;
15764 }
15765 }
15766
15767 return 0;
15768}
15769
2f202fde
JK
15770/* Remove any references to OBJFILE which is going to be freed. */
15771
15772void
15773breakpoint_free_objfile (struct objfile *objfile)
15774{
15775 struct bp_location **locp, *loc;
15776
15777 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15778 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15779 loc->symtab = NULL;
15780}
15781
2060206e
PA
15782void
15783initialize_breakpoint_ops (void)
15784{
15785 static int initialized = 0;
15786
15787 struct breakpoint_ops *ops;
15788
15789 if (initialized)
15790 return;
15791 initialized = 1;
15792
15793 /* The breakpoint_ops structure to be inherit by all kinds of
15794 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15795 internal and momentary breakpoints, etc.). */
15796 ops = &bkpt_base_breakpoint_ops;
15797 *ops = base_breakpoint_ops;
15798 ops->re_set = bkpt_re_set;
15799 ops->insert_location = bkpt_insert_location;
15800 ops->remove_location = bkpt_remove_location;
15801 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15802 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15803 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15804 ops->decode_location = bkpt_decode_location;
2060206e
PA
15805
15806 /* The breakpoint_ops structure to be used in regular breakpoints. */
15807 ops = &bkpt_breakpoint_ops;
15808 *ops = bkpt_base_breakpoint_ops;
15809 ops->re_set = bkpt_re_set;
15810 ops->resources_needed = bkpt_resources_needed;
15811 ops->print_it = bkpt_print_it;
15812 ops->print_mention = bkpt_print_mention;
15813 ops->print_recreate = bkpt_print_recreate;
15814
15815 /* Ranged breakpoints. */
15816 ops = &ranged_breakpoint_ops;
15817 *ops = bkpt_breakpoint_ops;
15818 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15819 ops->resources_needed = resources_needed_ranged_breakpoint;
15820 ops->print_it = print_it_ranged_breakpoint;
15821 ops->print_one = print_one_ranged_breakpoint;
15822 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15823 ops->print_mention = print_mention_ranged_breakpoint;
15824 ops->print_recreate = print_recreate_ranged_breakpoint;
15825
15826 /* Internal breakpoints. */
15827 ops = &internal_breakpoint_ops;
15828 *ops = bkpt_base_breakpoint_ops;
15829 ops->re_set = internal_bkpt_re_set;
15830 ops->check_status = internal_bkpt_check_status;
15831 ops->print_it = internal_bkpt_print_it;
15832 ops->print_mention = internal_bkpt_print_mention;
15833
15834 /* Momentary breakpoints. */
15835 ops = &momentary_breakpoint_ops;
15836 *ops = bkpt_base_breakpoint_ops;
15837 ops->re_set = momentary_bkpt_re_set;
15838 ops->check_status = momentary_bkpt_check_status;
15839 ops->print_it = momentary_bkpt_print_it;
15840 ops->print_mention = momentary_bkpt_print_mention;
15841
55aa24fb
SDJ
15842 /* Probe breakpoints. */
15843 ops = &bkpt_probe_breakpoint_ops;
15844 *ops = bkpt_breakpoint_ops;
15845 ops->insert_location = bkpt_probe_insert_location;
15846 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15847 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15848 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15849
2060206e
PA
15850 /* Watchpoints. */
15851 ops = &watchpoint_breakpoint_ops;
15852 *ops = base_breakpoint_ops;
15853 ops->re_set = re_set_watchpoint;
15854 ops->insert_location = insert_watchpoint;
15855 ops->remove_location = remove_watchpoint;
15856 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15857 ops->check_status = check_status_watchpoint;
15858 ops->resources_needed = resources_needed_watchpoint;
15859 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15860 ops->print_it = print_it_watchpoint;
15861 ops->print_mention = print_mention_watchpoint;
15862 ops->print_recreate = print_recreate_watchpoint;
427cd150 15863 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15864
15865 /* Masked watchpoints. */
15866 ops = &masked_watchpoint_breakpoint_ops;
15867 *ops = watchpoint_breakpoint_ops;
15868 ops->insert_location = insert_masked_watchpoint;
15869 ops->remove_location = remove_masked_watchpoint;
15870 ops->resources_needed = resources_needed_masked_watchpoint;
15871 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15872 ops->print_it = print_it_masked_watchpoint;
15873 ops->print_one_detail = print_one_detail_masked_watchpoint;
15874 ops->print_mention = print_mention_masked_watchpoint;
15875 ops->print_recreate = print_recreate_masked_watchpoint;
15876
15877 /* Tracepoints. */
15878 ops = &tracepoint_breakpoint_ops;
15879 *ops = base_breakpoint_ops;
15880 ops->re_set = tracepoint_re_set;
15881 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15882 ops->print_one_detail = tracepoint_print_one_detail;
15883 ops->print_mention = tracepoint_print_mention;
15884 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15885 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15886 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15887 ops->decode_location = tracepoint_decode_location;
983af33b 15888
55aa24fb
SDJ
15889 /* Probe tracepoints. */
15890 ops = &tracepoint_probe_breakpoint_ops;
15891 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15892 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15893 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15894
983af33b
SDJ
15895 /* Static tracepoints with marker (`-m'). */
15896 ops = &strace_marker_breakpoint_ops;
15897 *ops = tracepoint_breakpoint_ops;
5f700d83 15898 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15899 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15900 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15901
15902 /* Fork catchpoints. */
15903 ops = &catch_fork_breakpoint_ops;
15904 *ops = base_breakpoint_ops;
15905 ops->insert_location = insert_catch_fork;
15906 ops->remove_location = remove_catch_fork;
15907 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15908 ops->print_it = print_it_catch_fork;
15909 ops->print_one = print_one_catch_fork;
15910 ops->print_mention = print_mention_catch_fork;
15911 ops->print_recreate = print_recreate_catch_fork;
15912
15913 /* Vfork catchpoints. */
15914 ops = &catch_vfork_breakpoint_ops;
15915 *ops = base_breakpoint_ops;
15916 ops->insert_location = insert_catch_vfork;
15917 ops->remove_location = remove_catch_vfork;
15918 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15919 ops->print_it = print_it_catch_vfork;
15920 ops->print_one = print_one_catch_vfork;
15921 ops->print_mention = print_mention_catch_vfork;
15922 ops->print_recreate = print_recreate_catch_vfork;
15923
15924 /* Exec catchpoints. */
15925 ops = &catch_exec_breakpoint_ops;
15926 *ops = base_breakpoint_ops;
2060206e
PA
15927 ops->insert_location = insert_catch_exec;
15928 ops->remove_location = remove_catch_exec;
15929 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15930 ops->print_it = print_it_catch_exec;
15931 ops->print_one = print_one_catch_exec;
15932 ops->print_mention = print_mention_catch_exec;
15933 ops->print_recreate = print_recreate_catch_exec;
15934
edcc5120
TT
15935 /* Solib-related catchpoints. */
15936 ops = &catch_solib_breakpoint_ops;
15937 *ops = base_breakpoint_ops;
edcc5120
TT
15938 ops->insert_location = insert_catch_solib;
15939 ops->remove_location = remove_catch_solib;
15940 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15941 ops->check_status = check_status_catch_solib;
15942 ops->print_it = print_it_catch_solib;
15943 ops->print_one = print_one_catch_solib;
15944 ops->print_mention = print_mention_catch_solib;
15945 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15946
15947 ops = &dprintf_breakpoint_ops;
15948 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15949 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15950 ops->resources_needed = bkpt_resources_needed;
15951 ops->print_it = bkpt_print_it;
15952 ops->print_mention = bkpt_print_mention;
2d9442cc 15953 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15954 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15955 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15956}
15957
8bfd80db
YQ
15958/* Chain containing all defined "enable breakpoint" subcommands. */
15959
15960static struct cmd_list_element *enablebreaklist = NULL;
15961
c906108c 15962void
fba45db2 15963_initialize_breakpoint (void)
c906108c
SS
15964{
15965 struct cmd_list_element *c;
15966
2060206e
PA
15967 initialize_breakpoint_ops ();
15968
84acb35a 15969 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15970 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15971 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15972
55aa24fb
SDJ
15973 breakpoint_objfile_key
15974 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15975
c906108c
SS
15976 breakpoint_chain = 0;
15977 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15978 before a breakpoint is set. */
15979 breakpoint_count = 0;
15980
1042e4c0
SS
15981 tracepoint_count = 0;
15982
1bedd215
AC
15983 add_com ("ignore", class_breakpoint, ignore_command, _("\
15984Set ignore-count of breakpoint number N to COUNT.\n\
15985Usage is `ignore N COUNT'."));
c906108c 15986
1bedd215 15987 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15988Set commands to be executed when the given breakpoints are hit.\n\
15989Give a space-separated breakpoint list as argument after \"commands\".\n\
15990A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15991(e.g. `5-7').\n\
c906108c
SS
15992With no argument, the targeted breakpoint is the last one set.\n\
15993The commands themselves follow starting on the next line.\n\
15994Type a line containing \"end\" to indicate the end of them.\n\
15995Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15996then no output is printed when it is hit, except what the commands print."));
c906108c 15997
d55637df 15998 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15999Specify breakpoint number N to break only if COND is true.\n\
c906108c 16000Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16001expression to be evaluated whenever breakpoint N is reached."));
d55637df 16002 set_cmd_completer (c, condition_completer);
c906108c 16003
1bedd215 16004 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16005Set a temporary breakpoint.\n\
c906108c
SS
16006Like \"break\" except the breakpoint is only temporary,\n\
16007so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16008by using \"enable delete\" on the breakpoint number.\n\
16009\n"
16010BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16011 set_cmd_completer (c, location_completer);
c94fdfd0 16012
1bedd215 16013 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16014Set a hardware assisted breakpoint.\n\
c906108c 16015Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16016some target hardware may not have this support.\n\
16017\n"
16018BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16019 set_cmd_completer (c, location_completer);
c906108c 16020
1bedd215 16021 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16022Set a temporary hardware assisted breakpoint.\n\
c906108c 16023Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16024so it will be deleted when hit.\n\
16025\n"
16026BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16027 set_cmd_completer (c, location_completer);
c906108c 16028
1bedd215
AC
16029 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16030Enable some breakpoints.\n\
c906108c
SS
16031Give breakpoint numbers (separated by spaces) as arguments.\n\
16032With no subcommand, breakpoints are enabled until you command otherwise.\n\
16033This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16034With a subcommand you can enable temporarily."),
c906108c 16035 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16036
16037 add_com_alias ("en", "enable", class_breakpoint, 1);
16038
84951ab5 16039 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16040Enable some breakpoints.\n\
c906108c
SS
16041Give breakpoint numbers (separated by spaces) as arguments.\n\
16042This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16043May be abbreviated to simply \"enable\".\n"),
c5aa993b 16044 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16045
1a966eab
AC
16046 add_cmd ("once", no_class, enable_once_command, _("\
16047Enable breakpoints for one hit. Give breakpoint numbers.\n\
16048If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16049 &enablebreaklist);
16050
1a966eab
AC
16051 add_cmd ("delete", no_class, enable_delete_command, _("\
16052Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16053If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16054 &enablebreaklist);
16055
816338b5
SS
16056 add_cmd ("count", no_class, enable_count_command, _("\
16057Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16058If a breakpoint is hit while enabled in this fashion,\n\
16059the count is decremented; when it reaches zero, the breakpoint is disabled."),
16060 &enablebreaklist);
16061
1a966eab
AC
16062 add_cmd ("delete", no_class, enable_delete_command, _("\
16063Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16064If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16065 &enablelist);
16066
1a966eab
AC
16067 add_cmd ("once", no_class, enable_once_command, _("\
16068Enable breakpoints for one hit. Give breakpoint numbers.\n\
16069If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16070 &enablelist);
16071
16072 add_cmd ("count", no_class, enable_count_command, _("\
16073Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16074If a breakpoint is hit while enabled in this fashion,\n\
16075the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16076 &enablelist);
16077
1bedd215
AC
16078 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16079Disable some breakpoints.\n\
c906108c
SS
16080Arguments are breakpoint numbers with spaces in between.\n\
16081To disable all breakpoints, give no argument.\n\
64b9b334 16082A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16083 &disablelist, "disable ", 1, &cmdlist);
16084 add_com_alias ("dis", "disable", class_breakpoint, 1);
16085 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16086
1a966eab
AC
16087 add_cmd ("breakpoints", class_alias, disable_command, _("\
16088Disable some breakpoints.\n\
c906108c
SS
16089Arguments are breakpoint numbers with spaces in between.\n\
16090To disable all breakpoints, give no argument.\n\
64b9b334 16091A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16092This command may be abbreviated \"disable\"."),
c906108c
SS
16093 &disablelist);
16094
1bedd215
AC
16095 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16096Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16097Arguments are breakpoint numbers with spaces in between.\n\
16098To delete all breakpoints, give no argument.\n\
16099\n\
16100Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16101The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16102 &deletelist, "delete ", 1, &cmdlist);
16103 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16104 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16105
1a966eab
AC
16106 add_cmd ("breakpoints", class_alias, delete_command, _("\
16107Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16108Arguments are breakpoint numbers with spaces in between.\n\
16109To delete all breakpoints, give no argument.\n\
1a966eab 16110This command may be abbreviated \"delete\"."),
c906108c
SS
16111 &deletelist);
16112
1bedd215 16113 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16114Clear breakpoint at specified location.\n\
16115Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16116\n\
16117With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16118is executing in.\n"
16119"\n" LOCATION_HELP_STRING "\n\
1bedd215 16120See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16121 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16122
1bedd215 16123 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16124Set breakpoint at specified location.\n"
31e2b00f 16125BREAK_ARGS_HELP ("break")));
5ba2abeb 16126 set_cmd_completer (c, location_completer);
c94fdfd0 16127
c906108c
SS
16128 add_com_alias ("b", "break", class_run, 1);
16129 add_com_alias ("br", "break", class_run, 1);
16130 add_com_alias ("bre", "break", class_run, 1);
16131 add_com_alias ("brea", "break", class_run, 1);
16132
c906108c
SS
16133 if (dbx_commands)
16134 {
1bedd215
AC
16135 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16136Break in function/address or break at a line in the current file."),
c5aa993b
JM
16137 &stoplist, "stop ", 1, &cmdlist);
16138 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16139 _("Break in function or address."), &stoplist);
c5aa993b 16140 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16141 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16142 add_com ("status", class_info, breakpoints_info, _("\
16143Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16144The \"Type\" column indicates one of:\n\
16145\tbreakpoint - normal breakpoint\n\
16146\twatchpoint - watchpoint\n\
16147The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16148the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16149breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16150address and file/line number respectively.\n\
16151\n\
16152Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16153are set to the address of the last breakpoint listed unless the command\n\
16154is prefixed with \"server \".\n\n\
c906108c 16155Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16156breakpoint set."));
c906108c
SS
16157 }
16158
1bedd215 16159 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16160Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16161The \"Type\" column indicates one of:\n\
16162\tbreakpoint - normal breakpoint\n\
16163\twatchpoint - watchpoint\n\
16164The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16165the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16166breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16167address and file/line number respectively.\n\
16168\n\
16169Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16170are set to the address of the last breakpoint listed unless the command\n\
16171is prefixed with \"server \".\n\n\
c906108c 16172Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16173breakpoint set."));
c906108c 16174
6b04bdb7
MS
16175 add_info_alias ("b", "breakpoints", 1);
16176
1a966eab
AC
16177 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16178Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16179The \"Type\" column indicates one of:\n\
16180\tbreakpoint - normal breakpoint\n\
16181\twatchpoint - watchpoint\n\
16182\tlongjmp - internal breakpoint used to step through longjmp()\n\
16183\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16184\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16185\tfinish - internal breakpoint used by the \"finish\" command\n\
16186The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16187the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16188breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16189address and file/line number respectively.\n\
16190\n\
16191Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16192are set to the address of the last breakpoint listed unless the command\n\
16193is prefixed with \"server \".\n\n\
c906108c 16194Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16195breakpoint set."),
c906108c
SS
16196 &maintenanceinfolist);
16197
44feb3ce
TT
16198 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16199Set catchpoints to catch events."),
16200 &catch_cmdlist, "catch ",
16201 0/*allow-unknown*/, &cmdlist);
16202
16203 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16204Set temporary catchpoints to catch events."),
16205 &tcatch_cmdlist, "tcatch ",
16206 0/*allow-unknown*/, &cmdlist);
16207
44feb3ce
TT
16208 add_catch_command ("fork", _("Catch calls to fork."),
16209 catch_fork_command_1,
a96d9b2e 16210 NULL,
44feb3ce
TT
16211 (void *) (uintptr_t) catch_fork_permanent,
16212 (void *) (uintptr_t) catch_fork_temporary);
16213 add_catch_command ("vfork", _("Catch calls to vfork."),
16214 catch_fork_command_1,
a96d9b2e 16215 NULL,
44feb3ce
TT
16216 (void *) (uintptr_t) catch_vfork_permanent,
16217 (void *) (uintptr_t) catch_vfork_temporary);
16218 add_catch_command ("exec", _("Catch calls to exec."),
16219 catch_exec_command_1,
a96d9b2e
SDJ
16220 NULL,
16221 CATCH_PERMANENT,
16222 CATCH_TEMPORARY);
edcc5120
TT
16223 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16224Usage: catch load [REGEX]\n\
16225If REGEX is given, only stop for libraries matching the regular expression."),
16226 catch_load_command_1,
16227 NULL,
16228 CATCH_PERMANENT,
16229 CATCH_TEMPORARY);
16230 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16231Usage: catch unload [REGEX]\n\
16232If REGEX is given, only stop for libraries matching the regular expression."),
16233 catch_unload_command_1,
16234 NULL,
16235 CATCH_PERMANENT,
16236 CATCH_TEMPORARY);
c5aa993b 16237
1bedd215
AC
16238 c = add_com ("watch", class_breakpoint, watch_command, _("\
16239Set a watchpoint for an expression.\n\
06a64a0b 16240Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16241A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16242an expression changes.\n\
16243If -l or -location is given, this evaluates EXPRESSION and watches\n\
16244the memory to which it refers."));
65d12d83 16245 set_cmd_completer (c, expression_completer);
c906108c 16246
1bedd215
AC
16247 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16248Set a read watchpoint for an expression.\n\
06a64a0b 16249Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16250A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16251an expression is read.\n\
16252If -l or -location is given, this evaluates EXPRESSION and watches\n\
16253the memory to which it refers."));
65d12d83 16254 set_cmd_completer (c, expression_completer);
c906108c 16255
1bedd215
AC
16256 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16257Set a watchpoint for an expression.\n\
06a64a0b 16258Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16259A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16260an expression is either read or written.\n\
16261If -l or -location is given, this evaluates EXPRESSION and watches\n\
16262the memory to which it refers."));
65d12d83 16263 set_cmd_completer (c, expression_completer);
c906108c 16264
d77f58be 16265 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16266Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16267
920d2a44
AC
16268 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16269 respond to changes - contrary to the description. */
85c07804
AC
16270 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16271 &can_use_hw_watchpoints, _("\
16272Set debugger's willingness to use watchpoint hardware."), _("\
16273Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16274If zero, gdb will not use hardware for new watchpoints, even if\n\
16275such is available. (However, any hardware watchpoints that were\n\
16276created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16277hardware.)"),
16278 NULL,
920d2a44 16279 show_can_use_hw_watchpoints,
85c07804 16280 &setlist, &showlist);
c906108c
SS
16281
16282 can_use_hw_watchpoints = 1;
fa8d40ab 16283
1042e4c0
SS
16284 /* Tracepoint manipulation commands. */
16285
16286 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16287Set a tracepoint at specified location.\n\
1042e4c0
SS
16288\n"
16289BREAK_ARGS_HELP ("trace") "\n\
16290Do \"help tracepoints\" for info on other tracepoint commands."));
16291 set_cmd_completer (c, location_completer);
16292
16293 add_com_alias ("tp", "trace", class_alias, 0);
16294 add_com_alias ("tr", "trace", class_alias, 1);
16295 add_com_alias ("tra", "trace", class_alias, 1);
16296 add_com_alias ("trac", "trace", class_alias, 1);
16297
7a697b8d 16298 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16299Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16300\n"
16301BREAK_ARGS_HELP ("ftrace") "\n\
16302Do \"help tracepoints\" for info on other tracepoint commands."));
16303 set_cmd_completer (c, location_completer);
16304
0fb4aa4b 16305 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16306Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16307\n\
16308strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16309LOCATION may be a linespec, explicit, or address location (described below) \n\
16310or -m MARKER_ID.\n\n\
16311If a marker id is specified, probe the marker with that name. With\n\
16312no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16313Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16314This collects arbitrary user data passed in the probe point call to the\n\
16315tracing library. You can inspect it when analyzing the trace buffer,\n\
16316by printing the $_sdata variable like any other convenience variable.\n\
16317\n\
16318CONDITION is a boolean expression.\n\
629500fa 16319\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16320Multiple tracepoints at one place are permitted, and useful if their\n\
16321conditions are different.\n\
0fb4aa4b
PA
16322\n\
16323Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16324Do \"help tracepoints\" for info on other tracepoint commands."));
16325 set_cmd_completer (c, location_completer);
16326
1042e4c0 16327 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16328Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16329Convenience variable \"$tpnum\" contains the number of the\n\
16330last tracepoint set."));
16331
16332 add_info_alias ("tp", "tracepoints", 1);
16333
16334 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16335Delete specified tracepoints.\n\
16336Arguments are tracepoint numbers, separated by spaces.\n\
16337No argument means delete all tracepoints."),
16338 &deletelist);
7e20dfcd 16339 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16340
16341 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16342Disable specified tracepoints.\n\
16343Arguments are tracepoint numbers, separated by spaces.\n\
16344No argument means disable all tracepoints."),
16345 &disablelist);
16346 deprecate_cmd (c, "disable");
16347
16348 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16349Enable specified tracepoints.\n\
16350Arguments are tracepoint numbers, separated by spaces.\n\
16351No argument means enable all tracepoints."),
16352 &enablelist);
16353 deprecate_cmd (c, "enable");
16354
16355 add_com ("passcount", class_trace, trace_pass_command, _("\
16356Set the passcount for a tracepoint.\n\
16357The trace will end when the tracepoint has been passed 'count' times.\n\
16358Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16359if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16360
6149aea9
PA
16361 add_prefix_cmd ("save", class_breakpoint, save_command,
16362 _("Save breakpoint definitions as a script."),
16363 &save_cmdlist, "save ",
16364 0/*allow-unknown*/, &cmdlist);
16365
16366 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16367Save current breakpoint definitions as a script.\n\
cce7e648 16368This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16369catchpoints, tracepoints). Use the 'source' command in another debug\n\
16370session to restore them."),
16371 &save_cmdlist);
16372 set_cmd_completer (c, filename_completer);
16373
16374 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16375Save current tracepoint definitions as a script.\n\
6149aea9
PA
16376Use the 'source' command in another debug session to restore them."),
16377 &save_cmdlist);
1042e4c0
SS
16378 set_cmd_completer (c, filename_completer);
16379
6149aea9
PA
16380 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16381 deprecate_cmd (c, "save tracepoints");
16382
1bedd215 16383 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16384Breakpoint specific settings\n\
16385Configure various breakpoint-specific variables such as\n\
1bedd215 16386pending breakpoint behavior"),
fa8d40ab
JJ
16387 &breakpoint_set_cmdlist, "set breakpoint ",
16388 0/*allow-unknown*/, &setlist);
1bedd215 16389 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16390Breakpoint specific settings\n\
16391Configure various breakpoint-specific variables such as\n\
1bedd215 16392pending breakpoint behavior"),
fa8d40ab
JJ
16393 &breakpoint_show_cmdlist, "show breakpoint ",
16394 0/*allow-unknown*/, &showlist);
16395
7915a72c
AC
16396 add_setshow_auto_boolean_cmd ("pending", no_class,
16397 &pending_break_support, _("\
16398Set debugger's behavior regarding pending breakpoints."), _("\
16399Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16400If on, an unrecognized breakpoint location will cause gdb to create a\n\
16401pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16402an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16403user-query to see if a pending breakpoint should be created."),
2c5b56ce 16404 NULL,
920d2a44 16405 show_pending_break_support,
6e1d7d6c
AC
16406 &breakpoint_set_cmdlist,
16407 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16408
16409 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16410
16411 add_setshow_boolean_cmd ("auto-hw", no_class,
16412 &automatic_hardware_breakpoints, _("\
16413Set automatic usage of hardware breakpoints."), _("\
16414Show automatic usage of hardware breakpoints."), _("\
16415If set, the debugger will automatically use hardware breakpoints for\n\
16416breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16417a warning will be emitted for such breakpoints."),
16418 NULL,
16419 show_automatic_hardware_breakpoints,
16420 &breakpoint_set_cmdlist,
16421 &breakpoint_show_cmdlist);
74960c60 16422
a25a5a45
PA
16423 add_setshow_boolean_cmd ("always-inserted", class_support,
16424 &always_inserted_mode, _("\
74960c60
VP
16425Set mode for inserting breakpoints."), _("\
16426Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16427When this mode is on, breakpoints are inserted immediately as soon as\n\
16428they're created, kept inserted even when execution stops, and removed\n\
16429only when the user deletes them. When this mode is off (the default),\n\
16430breakpoints are inserted only when execution continues, and removed\n\
16431when execution stops."),
72d0e2c5
YQ
16432 NULL,
16433 &show_always_inserted_mode,
16434 &breakpoint_set_cmdlist,
16435 &breakpoint_show_cmdlist);
f1310107 16436
b775012e
LM
16437 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16438 condition_evaluation_enums,
16439 &condition_evaluation_mode_1, _("\
16440Set mode of breakpoint condition evaluation."), _("\
16441Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16442When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16443evaluated on the host's side by GDB. When it is set to \"target\",\n\
16444breakpoint conditions will be downloaded to the target (if the target\n\
16445supports such feature) and conditions will be evaluated on the target's side.\n\
16446If this is set to \"auto\" (default), this will be automatically set to\n\
16447\"target\" if it supports condition evaluation, otherwise it will\n\
16448be set to \"gdb\""),
16449 &set_condition_evaluation_mode,
16450 &show_condition_evaluation_mode,
16451 &breakpoint_set_cmdlist,
16452 &breakpoint_show_cmdlist);
16453
f1310107
TJB
16454 add_com ("break-range", class_breakpoint, break_range_command, _("\
16455Set a breakpoint for an address range.\n\
16456break-range START-LOCATION, END-LOCATION\n\
16457where START-LOCATION and END-LOCATION can be one of the following:\n\
16458 LINENUM, for that line in the current file,\n\
16459 FILE:LINENUM, for that line in that file,\n\
16460 +OFFSET, for that number of lines after the current line\n\
16461 or the start of the range\n\
16462 FUNCTION, for the first line in that function,\n\
16463 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16464 *ADDRESS, for the instruction at that address.\n\
16465\n\
16466The breakpoint will stop execution of the inferior whenever it executes\n\
16467an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16468range (including START-LOCATION and END-LOCATION)."));
16469
e7e0cddf 16470 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16471Set a dynamic printf at specified location.\n\
e7e0cddf 16472dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16473location may be a linespec, explicit, or address location.\n"
16474"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16475 set_cmd_completer (c, location_completer);
16476
16477 add_setshow_enum_cmd ("dprintf-style", class_support,
16478 dprintf_style_enums, &dprintf_style, _("\
16479Set the style of usage for dynamic printf."), _("\
16480Show the style of usage for dynamic printf."), _("\
16481This setting chooses how GDB will do a dynamic printf.\n\
16482If the value is \"gdb\", then the printing is done by GDB to its own\n\
16483console, as with the \"printf\" command.\n\
16484If the value is \"call\", the print is done by calling a function in your\n\
16485program; by default printf(), but you can choose a different function or\n\
16486output stream by setting dprintf-function and dprintf-channel."),
16487 update_dprintf_commands, NULL,
16488 &setlist, &showlist);
16489
16490 dprintf_function = xstrdup ("printf");
16491 add_setshow_string_cmd ("dprintf-function", class_support,
16492 &dprintf_function, _("\
16493Set the function to use for dynamic printf"), _("\
16494Show the function to use for dynamic printf"), NULL,
16495 update_dprintf_commands, NULL,
16496 &setlist, &showlist);
16497
16498 dprintf_channel = xstrdup ("");
16499 add_setshow_string_cmd ("dprintf-channel", class_support,
16500 &dprintf_channel, _("\
16501Set the channel to use for dynamic printf"), _("\
16502Show the channel to use for dynamic printf"), NULL,
16503 update_dprintf_commands, NULL,
16504 &setlist, &showlist);
16505
d3ce09f5
SS
16506 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16507 &disconnected_dprintf, _("\
16508Set whether dprintf continues after GDB disconnects."), _("\
16509Show whether dprintf continues after GDB disconnects."), _("\
16510Use this to let dprintf commands continue to hit and produce output\n\
16511even if GDB disconnects or detaches from the target."),
16512 NULL,
16513 NULL,
16514 &setlist, &showlist);
16515
16516 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16517agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16518(target agent only) This is useful for formatted output in user-defined commands."));
16519
765dc015 16520 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16521
16522 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16523 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16524}
This page took 4.353666 seconds and 4 git commands to generate.