Update PR ld/21964 tests
[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{
35df4500 3305 struct bp_location *bl, **blp_tmp;
c906108c 3306 int val;
dd61ec5c 3307 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3308 struct inferior *inf;
3309 struct thread_info *tp;
3310
3311 tp = any_live_thread_of_process (pid);
3312 if (tp == NULL)
3313 return 1;
3314
3315 inf = find_inferior_pid (pid);
6c95b8df 3316
2989a365 3317 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3318 inferior_ptid = tp->ptid;
a4954f26 3319
d7e74731 3320 string_file tmp_error_stream;
c906108c 3321
35df4500 3322 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3323 {
35df4500 3324 if (bl->pspace != inf->pspace)
6c95b8df
PA
3325 continue;
3326
35df4500 3327 if (bl->inserted)
c5aa993b 3328 {
35df4500 3329 bl->inserted = 0;
d7e74731 3330 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b 3331 if (val != 0)
2989a365 3332 return val;
c5aa993b
JM
3333 }
3334 }
c906108c
SS
3335 return 0;
3336}
3337
e58b0e63
PA
3338static int internal_breakpoint_number = -1;
3339
84f4c1fe
PM
3340/* Set the breakpoint number of B, depending on the value of INTERNAL.
3341 If INTERNAL is non-zero, the breakpoint number will be populated
3342 from internal_breakpoint_number and that variable decremented.
e5dd4106 3343 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3344 breakpoint_count and that value incremented. Internal breakpoints
3345 do not set the internal var bpnum. */
3346static void
3347set_breakpoint_number (int internal, struct breakpoint *b)
3348{
3349 if (internal)
3350 b->number = internal_breakpoint_number--;
3351 else
3352 {
3353 set_breakpoint_count (breakpoint_count + 1);
3354 b->number = breakpoint_count;
3355 }
3356}
3357
e62c965a 3358static struct breakpoint *
a6d9a66e 3359create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3360 CORE_ADDR address, enum bptype type,
c0a91b2b 3361 const struct breakpoint_ops *ops)
e62c965a 3362{
e62c965a
PP
3363 struct symtab_and_line sal;
3364 struct breakpoint *b;
3365
4a64f543 3366 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3367
3368 sal.pc = address;
3369 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3370 sal.pspace = current_program_space;
e62c965a 3371
06edf0c0 3372 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3373 b->number = internal_breakpoint_number--;
3374 b->disposition = disp_donttouch;
3375
3376 return b;
3377}
3378
17450429
PP
3379static const char *const longjmp_names[] =
3380 {
3381 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3382 };
3383#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3384
3385/* Per-objfile data private to breakpoint.c. */
3386struct breakpoint_objfile_data
3387{
3388 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3389 struct bound_minimal_symbol overlay_msym;
17450429
PP
3390
3391 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3392 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3393
28106bc2
SDJ
3394 /* True if we have looked for longjmp probes. */
3395 int longjmp_searched;
3396
3397 /* SystemTap probe points for longjmp (if any). */
3398 VEC (probe_p) *longjmp_probes;
3399
17450429 3400 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3401 struct bound_minimal_symbol terminate_msym;
17450429
PP
3402
3403 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3404 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3405
3406 /* True if we have looked for exception probes. */
3407 int exception_searched;
3408
3409 /* SystemTap probe points for unwinding (if any). */
3410 VEC (probe_p) *exception_probes;
17450429
PP
3411};
3412
3413static const struct objfile_data *breakpoint_objfile_key;
3414
3415/* Minimal symbol not found sentinel. */
3416static struct minimal_symbol msym_not_found;
3417
3418/* Returns TRUE if MSYM point to the "not found" sentinel. */
3419
3420static int
3421msym_not_found_p (const struct minimal_symbol *msym)
3422{
3423 return msym == &msym_not_found;
3424}
3425
3426/* Return per-objfile data needed by breakpoint.c.
3427 Allocate the data if necessary. */
3428
3429static struct breakpoint_objfile_data *
3430get_breakpoint_objfile_data (struct objfile *objfile)
3431{
3432 struct breakpoint_objfile_data *bp_objfile_data;
3433
9a3c8263
SM
3434 bp_objfile_data = ((struct breakpoint_objfile_data *)
3435 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3436 if (bp_objfile_data == NULL)
3437 {
8d749320
SM
3438 bp_objfile_data =
3439 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3440
3441 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3442 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3443 }
3444 return bp_objfile_data;
3445}
3446
28106bc2
SDJ
3447static void
3448free_breakpoint_probes (struct objfile *obj, void *data)
3449{
9a3c8263
SM
3450 struct breakpoint_objfile_data *bp_objfile_data
3451 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3452
3453 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3454 VEC_free (probe_p, bp_objfile_data->exception_probes);
3455}
3456
e62c965a 3457static void
af02033e 3458create_overlay_event_breakpoint (void)
e62c965a 3459{
69de3c6a 3460 struct objfile *objfile;
af02033e 3461 const char *const func_name = "_ovly_debug_event";
e62c965a 3462
69de3c6a
PP
3463 ALL_OBJFILES (objfile)
3464 {
3465 struct breakpoint *b;
17450429
PP
3466 struct breakpoint_objfile_data *bp_objfile_data;
3467 CORE_ADDR addr;
67994074 3468 struct explicit_location explicit_loc;
69de3c6a 3469
17450429
PP
3470 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3471
3b7344d5 3472 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3473 continue;
3474
3b7344d5 3475 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3476 {
3b7344d5 3477 struct bound_minimal_symbol m;
17450429
PP
3478
3479 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3480 if (m.minsym == NULL)
17450429
PP
3481 {
3482 /* Avoid future lookups in this objfile. */
3b7344d5 3483 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3484 continue;
3485 }
3486 bp_objfile_data->overlay_msym = m;
3487 }
e62c965a 3488
77e371c0 3489 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3490 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3491 bp_overlay_event,
3492 &internal_breakpoint_ops);
67994074
KS
3493 initialize_explicit_location (&explicit_loc);
3494 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3495 b->location = new_explicit_location (&explicit_loc);
e62c965a 3496
69de3c6a
PP
3497 if (overlay_debugging == ovly_auto)
3498 {
3499 b->enable_state = bp_enabled;
3500 overlay_events_enabled = 1;
3501 }
3502 else
3503 {
3504 b->enable_state = bp_disabled;
3505 overlay_events_enabled = 0;
3506 }
e62c965a 3507 }
e62c965a
PP
3508}
3509
0fd8e87f 3510static void
af02033e 3511create_longjmp_master_breakpoint (void)
0fd8e87f 3512{
6c95b8df 3513 struct program_space *pspace;
6c95b8df 3514
5ed8105e 3515 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3516
6c95b8df 3517 ALL_PSPACES (pspace)
af02033e
PP
3518 {
3519 struct objfile *objfile;
3520
3521 set_current_program_space (pspace);
3522
3523 ALL_OBJFILES (objfile)
0fd8e87f 3524 {
af02033e
PP
3525 int i;
3526 struct gdbarch *gdbarch;
17450429 3527 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3528
af02033e 3529 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3530
17450429
PP
3531 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3532
28106bc2
SDJ
3533 if (!bp_objfile_data->longjmp_searched)
3534 {
25f9533e
SDJ
3535 VEC (probe_p) *ret;
3536
3537 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3538 if (ret != NULL)
3539 {
3540 /* We are only interested in checking one element. */
3541 struct probe *p = VEC_index (probe_p, ret, 0);
3542
3543 if (!can_evaluate_probe_arguments (p))
3544 {
3545 /* We cannot use the probe interface here, because it does
3546 not know how to evaluate arguments. */
3547 VEC_free (probe_p, ret);
3548 ret = NULL;
3549 }
3550 }
3551 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3552 bp_objfile_data->longjmp_searched = 1;
3553 }
3554
3555 if (bp_objfile_data->longjmp_probes != NULL)
3556 {
3557 int i;
3558 struct probe *probe;
3559 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3560
3561 for (i = 0;
3562 VEC_iterate (probe_p,
3563 bp_objfile_data->longjmp_probes,
3564 i, probe);
3565 ++i)
3566 {
3567 struct breakpoint *b;
3568
729662a5
TT
3569 b = create_internal_breakpoint (gdbarch,
3570 get_probe_address (probe,
3571 objfile),
28106bc2
SDJ
3572 bp_longjmp_master,
3573 &internal_breakpoint_ops);
d28cd78a 3574 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3575 b->enable_state = bp_disabled;
3576 }
3577
3578 continue;
3579 }
3580
0569175e
TSD
3581 if (!gdbarch_get_longjmp_target_p (gdbarch))
3582 continue;
3583
17450429 3584 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3585 {
3586 struct breakpoint *b;
af02033e 3587 const char *func_name;
17450429 3588 CORE_ADDR addr;
67994074 3589 struct explicit_location explicit_loc;
6c95b8df 3590
3b7344d5 3591 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3592 continue;
0fd8e87f 3593
17450429 3594 func_name = longjmp_names[i];
3b7344d5 3595 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3596 {
3b7344d5 3597 struct bound_minimal_symbol m;
17450429
PP
3598
3599 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3600 if (m.minsym == NULL)
17450429
PP
3601 {
3602 /* Prevent future lookups in this objfile. */
3b7344d5 3603 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3604 continue;
3605 }
3606 bp_objfile_data->longjmp_msym[i] = m;
3607 }
3608
77e371c0 3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3610 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3611 &internal_breakpoint_ops);
67994074
KS
3612 initialize_explicit_location (&explicit_loc);
3613 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3614 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3615 b->enable_state = bp_disabled;
3616 }
0fd8e87f 3617 }
af02033e 3618 }
0fd8e87f
UW
3619}
3620
af02033e 3621/* Create a master std::terminate breakpoint. */
aa7d318d 3622static void
af02033e 3623create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3624{
3625 struct program_space *pspace;
af02033e 3626 const char *const func_name = "std::terminate()";
aa7d318d 3627
5ed8105e 3628 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3629
3630 ALL_PSPACES (pspace)
17450429
PP
3631 {
3632 struct objfile *objfile;
3633 CORE_ADDR addr;
3634
3635 set_current_program_space (pspace);
3636
aa7d318d
TT
3637 ALL_OBJFILES (objfile)
3638 {
3639 struct breakpoint *b;
17450429 3640 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3641 struct explicit_location explicit_loc;
aa7d318d 3642
17450429 3643 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3644
3b7344d5 3645 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3646 continue;
3647
3b7344d5 3648 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3649 {
3b7344d5 3650 struct bound_minimal_symbol m;
17450429
PP
3651
3652 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3653 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3654 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3655 {
3656 /* Prevent future lookups in this objfile. */
3b7344d5 3657 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3658 continue;
3659 }
3660 bp_objfile_data->terminate_msym = m;
3661 }
aa7d318d 3662
77e371c0 3663 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3664 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3665 bp_std_terminate_master,
3666 &internal_breakpoint_ops);
67994074
KS
3667 initialize_explicit_location (&explicit_loc);
3668 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3669 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3670 b->enable_state = bp_disabled;
3671 }
17450429 3672 }
aa7d318d
TT
3673}
3674
186c406b
TT
3675/* Install a master breakpoint on the unwinder's debug hook. */
3676
70221824 3677static void
186c406b
TT
3678create_exception_master_breakpoint (void)
3679{
3680 struct objfile *objfile;
17450429 3681 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3682
3683 ALL_OBJFILES (objfile)
3684 {
17450429
PP
3685 struct breakpoint *b;
3686 struct gdbarch *gdbarch;
3687 struct breakpoint_objfile_data *bp_objfile_data;
3688 CORE_ADDR addr;
67994074 3689 struct explicit_location explicit_loc;
17450429
PP
3690
3691 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3692
28106bc2
SDJ
3693 /* We prefer the SystemTap probe point if it exists. */
3694 if (!bp_objfile_data->exception_searched)
3695 {
25f9533e
SDJ
3696 VEC (probe_p) *ret;
3697
3698 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3699
3700 if (ret != NULL)
3701 {
3702 /* We are only interested in checking one element. */
3703 struct probe *p = VEC_index (probe_p, ret, 0);
3704
3705 if (!can_evaluate_probe_arguments (p))
3706 {
3707 /* We cannot use the probe interface here, because it does
3708 not know how to evaluate arguments. */
3709 VEC_free (probe_p, ret);
3710 ret = NULL;
3711 }
3712 }
3713 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3714 bp_objfile_data->exception_searched = 1;
3715 }
3716
3717 if (bp_objfile_data->exception_probes != NULL)
3718 {
3719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3720 int i;
3721 struct probe *probe;
3722
3723 for (i = 0;
3724 VEC_iterate (probe_p,
3725 bp_objfile_data->exception_probes,
3726 i, probe);
3727 ++i)
3728 {
3729 struct breakpoint *b;
3730
729662a5
TT
3731 b = create_internal_breakpoint (gdbarch,
3732 get_probe_address (probe,
3733 objfile),
28106bc2
SDJ
3734 bp_exception_master,
3735 &internal_breakpoint_ops);
d28cd78a 3736 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3737 b->enable_state = bp_disabled;
3738 }
3739
3740 continue;
3741 }
3742
3743 /* Otherwise, try the hook function. */
3744
3b7344d5 3745 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3746 continue;
3747
3748 gdbarch = get_objfile_arch (objfile);
186c406b 3749
3b7344d5 3750 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3751 {
3b7344d5 3752 struct bound_minimal_symbol debug_hook;
186c406b 3753
17450429 3754 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3755 if (debug_hook.minsym == NULL)
17450429 3756 {
3b7344d5 3757 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3758 continue;
3759 }
3760
3761 bp_objfile_data->exception_msym = debug_hook;
186c406b 3762 }
17450429 3763
77e371c0 3764 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3765 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3766 &current_target);
06edf0c0
PA
3767 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3768 &internal_breakpoint_ops);
67994074
KS
3769 initialize_explicit_location (&explicit_loc);
3770 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3771 b->location = new_explicit_location (&explicit_loc);
17450429 3772 b->enable_state = bp_disabled;
186c406b 3773 }
186c406b
TT
3774}
3775
9ef9e6a6
KS
3776/* Does B have a location spec? */
3777
3778static int
3779breakpoint_event_location_empty_p (const struct breakpoint *b)
3780{
d28cd78a 3781 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3782}
3783
c906108c 3784void
fba45db2 3785update_breakpoints_after_exec (void)
c906108c 3786{
35df4500 3787 struct breakpoint *b, *b_tmp;
876fa593 3788 struct bp_location *bploc, **bplocp_tmp;
c906108c 3789
25b22b0a
PA
3790 /* We're about to delete breakpoints from GDB's lists. If the
3791 INSERTED flag is true, GDB will try to lift the breakpoints by
3792 writing the breakpoints' "shadow contents" back into memory. The
3793 "shadow contents" are NOT valid after an exec, so GDB should not
3794 do that. Instead, the target is responsible from marking
3795 breakpoints out as soon as it detects an exec. We don't do that
3796 here instead, because there may be other attempts to delete
3797 breakpoints after detecting an exec and before reaching here. */
876fa593 3798 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3799 if (bploc->pspace == current_program_space)
3800 gdb_assert (!bploc->inserted);
c906108c 3801
35df4500 3802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3803 {
6c95b8df
PA
3804 if (b->pspace != current_program_space)
3805 continue;
3806
4a64f543 3807 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3808 if (b->type == bp_shlib_event)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
c906108c 3813
4a64f543 3814 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3815 if (b->type == bp_jit_event)
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820
1900040c 3821 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3822 as must overlay event and longjmp master breakpoints. */
3823 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3824 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3825 || b->type == bp_exception_master)
c4093a6a
JM
3826 {
3827 delete_breakpoint (b);
3828 continue;
3829 }
3830
4a64f543 3831 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3832 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
7c16b83e
PA
3838 /* Just like single-step breakpoints. */
3839 if (b->type == bp_single_step)
3840 {
3841 delete_breakpoint (b);
3842 continue;
3843 }
3844
611c83ae
PA
3845 /* Longjmp and longjmp-resume breakpoints are also meaningless
3846 after an exec. */
186c406b 3847 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3848 || b->type == bp_longjmp_call_dummy
186c406b 3849 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3850 {
3851 delete_breakpoint (b);
3852 continue;
3853 }
3854
ce78b96d
JB
3855 if (b->type == bp_catchpoint)
3856 {
3857 /* For now, none of the bp_catchpoint breakpoints need to
3858 do anything at this point. In the future, if some of
3859 the catchpoints need to something, we will need to add
3860 a new method, and call this method from here. */
3861 continue;
3862 }
3863
c5aa993b
JM
3864 /* bp_finish is a special case. The only way we ought to be able
3865 to see one of these when an exec() has happened, is if the user
3866 caught a vfork, and then said "finish". Ordinarily a finish just
3867 carries them to the call-site of the current callee, by setting
3868 a temporary bp there and resuming. But in this case, the finish
3869 will carry them entirely through the vfork & exec.
3870
3871 We don't want to allow a bp_finish to remain inserted now. But
3872 we can't safely delete it, 'cause finish_command has a handle to
3873 the bp on a bpstat, and will later want to delete it. There's a
3874 chance (and I've seen it happen) that if we delete the bp_finish
3875 here, that its storage will get reused by the time finish_command
3876 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3877 We really must allow finish_command to delete a bp_finish.
3878
e5dd4106 3879 In the absence of a general solution for the "how do we know
53a5351d
JM
3880 it's safe to delete something others may have handles to?"
3881 problem, what we'll do here is just uninsert the bp_finish, and
3882 let finish_command delete it.
3883
3884 (We know the bp_finish is "doomed" in the sense that it's
3885 momentary, and will be deleted as soon as finish_command sees
3886 the inferior stopped. So it doesn't matter that the bp's
3887 address is probably bogus in the new a.out, unlike e.g., the
3888 solib breakpoints.) */
c5aa993b 3889
c5aa993b
JM
3890 if (b->type == bp_finish)
3891 {
3892 continue;
3893 }
3894
3895 /* Without a symbolic address, we have little hope of the
3896 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3897 a.out. */
9ef9e6a6 3898 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3899 {
3900 delete_breakpoint (b);
3901 continue;
3902 }
c5aa993b 3903 }
c906108c
SS
3904}
3905
3906int
d80ee84f 3907detach_breakpoints (ptid_t ptid)
c906108c 3908{
35df4500 3909 struct bp_location *bl, **blp_tmp;
3a1bae8e 3910 int val = 0;
2989a365 3911 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3912 struct inferior *inf = current_inferior ();
c5aa993b 3913
dfd4cc63 3914 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3915 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3916
6c95b8df 3917 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3918 inferior_ptid = ptid;
35df4500 3919 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3920 {
35df4500 3921 if (bl->pspace != inf->pspace)
6c95b8df
PA
3922 continue;
3923
bd9673a4
PW
3924 /* This function must physically remove breakpoints locations
3925 from the specified ptid, without modifying the breakpoint
3926 package's state. Locations of type bp_loc_other are only
3927 maintained at GDB side. So, there is no need to remove
3928 these bp_loc_other locations. Moreover, removing these
3929 would modify the breakpoint package's state. */
3930 if (bl->loc_type == bp_loc_other)
3931 continue;
3932
35df4500 3933 if (bl->inserted)
b2b6a7da 3934 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3935 }
d03285ec 3936
3a1bae8e 3937 return val;
c906108c
SS
3938}
3939
35df4500 3940/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3941 Note that this is used to detach breakpoints from a child fork.
3942 When we get here, the child isn't in the inferior list, and neither
3943 do we have objects to represent its address space --- we should
35df4500 3944 *not* look at bl->pspace->aspace here. */
6c95b8df 3945
c906108c 3946static int
b2b6a7da 3947remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3948{
3949 int val;
c5aa993b 3950
35df4500
TJB
3951 /* BL is never in moribund_locations by our callers. */
3952 gdb_assert (bl->owner != NULL);
2bdf28a0 3953
74960c60
VP
3954 /* The type of none suggests that owner is actually deleted.
3955 This should not ever happen. */
35df4500 3956 gdb_assert (bl->owner->type != bp_none);
0bde7532 3957
35df4500
TJB
3958 if (bl->loc_type == bp_loc_software_breakpoint
3959 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3960 {
c02f5703
MS
3961 /* "Normal" instruction breakpoint: either the standard
3962 trap-instruction bp (bp_breakpoint), or a
3963 bp_hardware_breakpoint. */
3964
3965 /* First check to see if we have to handle an overlay. */
3966 if (overlay_debugging == ovly_off
35df4500
TJB
3967 || bl->section == NULL
3968 || !(section_is_overlay (bl->section)))
c02f5703
MS
3969 {
3970 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3971
3972 /* If we're trying to uninsert a memory breakpoint that we
3973 know is set in a dynamic object that is marked
3974 shlib_disabled, then either the dynamic object was
3975 removed with "remove-symbol-file" or with
3976 "nosharedlibrary". In the former case, we don't know
3977 whether another dynamic object might have loaded over the
3978 breakpoint's address -- the user might well let us know
3979 about it next with add-symbol-file (the whole point of
d03de421 3980 add-symbol-file is letting the user manually maintain a
08351840
PA
3981 list of dynamically loaded objects). If we have the
3982 breakpoint's shadow memory, that is, this is a software
3983 breakpoint managed by GDB, check whether the breakpoint
3984 is still inserted in memory, to avoid overwriting wrong
3985 code with stale saved shadow contents. Note that HW
3986 breakpoints don't have shadow memory, as they're
3987 implemented using a mechanism that is not dependent on
3988 being able to modify the target's memory, and as such
3989 they should always be removed. */
3990 if (bl->shlib_disabled
3991 && bl->target_info.shadow_len != 0
3992 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3993 val = 0;
3994 else
73971819 3995 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3996 }
c906108c
SS
3997 else
3998 {
4a64f543 3999 /* This breakpoint is in an overlay section.
c02f5703
MS
4000 Did we set a breakpoint at the LMA? */
4001 if (!overlay_events_enabled)
4002 {
4003 /* Yes -- overlay event support is not active, so we
4004 should have set a breakpoint at the LMA. Remove it.
4005 */
c02f5703
MS
4006 /* Ignore any failures: if the LMA is in ROM, we will
4007 have already warned when we failed to insert it. */
35df4500
TJB
4008 if (bl->loc_type == bp_loc_hardware_breakpoint)
4009 target_remove_hw_breakpoint (bl->gdbarch,
4010 &bl->overlay_target_info);
c02f5703 4011 else
35df4500 4012 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4013 &bl->overlay_target_info,
4014 reason);
c02f5703
MS
4015 }
4016 /* Did we set a breakpoint at the VMA?
4017 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4018 if (bl->inserted)
c906108c 4019 {
c02f5703
MS
4020 /* Yes -- remove it. Previously we did not bother to
4021 remove the breakpoint if the section had been
4022 unmapped, but let's not rely on that being safe. We
4023 don't know what the overlay manager might do. */
aa67235e
UW
4024
4025 /* However, we should remove *software* breakpoints only
4026 if the section is still mapped, or else we overwrite
4027 wrong code with the saved shadow contents. */
348d480f
PA
4028 if (bl->loc_type == bp_loc_hardware_breakpoint
4029 || section_is_mapped (bl->section))
73971819 4030 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4031 else
4032 val = 0;
c906108c 4033 }
c02f5703
MS
4034 else
4035 {
4036 /* No -- not inserted, so no need to remove. No error. */
4037 val = 0;
4038 }
c906108c 4039 }
879d1e6b 4040
08351840
PA
4041 /* In some cases, we might not be able to remove a breakpoint in
4042 a shared library that has already been removed, but we have
4043 not yet processed the shlib unload event. Similarly for an
4044 unloaded add-symbol-file object - the user might not yet have
4045 had the chance to remove-symbol-file it. shlib_disabled will
4046 be set if the library/object has already been removed, but
4047 the breakpoint hasn't been uninserted yet, e.g., after
4048 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4049 always-inserted mode. */
076855f9 4050 if (val
08351840
PA
4051 && (bl->loc_type == bp_loc_software_breakpoint
4052 && (bl->shlib_disabled
4053 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4054 || shared_objfile_contains_address_p (bl->pspace,
4055 bl->address))))
879d1e6b
UW
4056 val = 0;
4057
c906108c
SS
4058 if (val)
4059 return val;
b2b6a7da 4060 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4061 }
35df4500 4062 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4063 {
77b06cd7
TJB
4064 gdb_assert (bl->owner->ops != NULL
4065 && bl->owner->ops->remove_location != NULL);
4066
b2b6a7da 4067 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4068 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4069
c906108c 4070 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4071 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4072 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4073 bl->owner->number);
c906108c 4074 }
35df4500
TJB
4075 else if (bl->owner->type == bp_catchpoint
4076 && breakpoint_enabled (bl->owner)
4077 && !bl->duplicate)
ce78b96d 4078 {
77b06cd7
TJB
4079 gdb_assert (bl->owner->ops != NULL
4080 && bl->owner->ops->remove_location != NULL);
ce78b96d 4081
73971819 4082 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4083 if (val)
4084 return val;
77b06cd7 4085
b2b6a7da 4086 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4087 }
c906108c
SS
4088
4089 return 0;
4090}
4091
6c95b8df 4092static int
834c0d03 4093remove_breakpoint (struct bp_location *bl)
6c95b8df 4094{
35df4500
TJB
4095 /* BL is never in moribund_locations by our callers. */
4096 gdb_assert (bl->owner != NULL);
2bdf28a0 4097
6c95b8df
PA
4098 /* The type of none suggests that owner is actually deleted.
4099 This should not ever happen. */
35df4500 4100 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4101
5ed8105e 4102 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4103
35df4500 4104 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4105
5ed8105e 4106 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4107}
4108
c906108c
SS
4109/* Clear the "inserted" flag in all breakpoints. */
4110
25b22b0a 4111void
fba45db2 4112mark_breakpoints_out (void)
c906108c 4113{
35df4500 4114 struct bp_location *bl, **blp_tmp;
c906108c 4115
35df4500 4116 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4117 if (bl->pspace == current_program_space)
35df4500 4118 bl->inserted = 0;
c906108c
SS
4119}
4120
53a5351d
JM
4121/* Clear the "inserted" flag in all breakpoints and delete any
4122 breakpoints which should go away between runs of the program.
c906108c
SS
4123
4124 Plus other such housekeeping that has to be done for breakpoints
4125 between runs.
4126
53a5351d
JM
4127 Note: this function gets called at the end of a run (by
4128 generic_mourn_inferior) and when a run begins (by
4a64f543 4129 init_wait_for_inferior). */
c906108c
SS
4130
4131
4132
4133void
fba45db2 4134breakpoint_init_inferior (enum inf_context context)
c906108c 4135{
35df4500 4136 struct breakpoint *b, *b_tmp;
870f88f7 4137 struct bp_location *bl;
1c5cfe86 4138 int ix;
6c95b8df 4139 struct program_space *pspace = current_program_space;
c906108c 4140
50c71eaf
PA
4141 /* If breakpoint locations are shared across processes, then there's
4142 nothing to do. */
f5656ead 4143 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4144 return;
4145
1a853c52 4146 mark_breakpoints_out ();
075f6582 4147
35df4500 4148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4149 {
6c95b8df
PA
4150 if (b->loc && b->loc->pspace != pspace)
4151 continue;
4152
c5aa993b
JM
4153 switch (b->type)
4154 {
4155 case bp_call_dummy:
e2e4d78b 4156 case bp_longjmp_call_dummy:
c906108c 4157
c5aa993b 4158 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4159 cause problems when the inferior is rerun, so we better get
4160 rid of it. */
4161
4162 case bp_watchpoint_scope:
4163
4164 /* Also get rid of scope breakpoints. */
4165
4166 case bp_shlib_event:
4167
4168 /* Also remove solib event breakpoints. Their addresses may
4169 have changed since the last time we ran the program.
4170 Actually we may now be debugging against different target;
4171 and so the solib backend that installed this breakpoint may
4172 not be used in by the target. E.g.,
4173
4174 (gdb) file prog-linux
4175 (gdb) run # native linux target
4176 ...
4177 (gdb) kill
4178 (gdb) file prog-win.exe
4179 (gdb) tar rem :9999 # remote Windows gdbserver.
4180 */
c906108c 4181
f59f708a
PA
4182 case bp_step_resume:
4183
4184 /* Also remove step-resume breakpoints. */
4185
7c16b83e
PA
4186 case bp_single_step:
4187
4188 /* Also remove single-step breakpoints. */
4189
c5aa993b
JM
4190 delete_breakpoint (b);
4191 break;
c906108c 4192
c5aa993b
JM
4193 case bp_watchpoint:
4194 case bp_hardware_watchpoint:
4195 case bp_read_watchpoint:
4196 case bp_access_watchpoint:
3a5c3e22
PA
4197 {
4198 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4199
3a5c3e22
PA
4200 /* Likewise for watchpoints on local expressions. */
4201 if (w->exp_valid_block != NULL)
4202 delete_breakpoint (b);
63000888 4203 else
3a5c3e22 4204 {
63000888
PA
4205 /* Get rid of existing locations, which are no longer
4206 valid. New ones will be created in
4207 update_watchpoint, when the inferior is restarted.
4208 The next update_global_location_list call will
4209 garbage collect them. */
4210 b->loc = NULL;
4211
4212 if (context == inf_starting)
4213 {
4214 /* Reset val field to force reread of starting value in
4215 insert_breakpoints. */
4216 if (w->val)
4217 value_free (w->val);
4218 w->val = NULL;
4219 w->val_valid = 0;
4220 }
4221 }
3a5c3e22 4222 }
c5aa993b
JM
4223 break;
4224 default:
c5aa993b
JM
4225 break;
4226 }
4227 }
1c5cfe86
PA
4228
4229 /* Get rid of the moribund locations. */
35df4500
TJB
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4231 decref_bp_location (&bl);
1c5cfe86 4232 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4233}
4234
6c95b8df
PA
4235/* These functions concern about actual breakpoints inserted in the
4236 target --- to e.g. check if we need to do decr_pc adjustment or if
4237 we need to hop over the bkpt --- so we check for address space
4238 match, not program space. */
4239
c2c6d25f
JM
4240/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4241 exists at PC. It returns ordinary_breakpoint_here if it's an
4242 ordinary breakpoint, or permanent_breakpoint_here if it's a
4243 permanent breakpoint.
4244 - When continuing from a location with an ordinary breakpoint, we
4245 actually single step once before calling insert_breakpoints.
e5dd4106 4246 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4247 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4248 the target, to advance the PC past the breakpoint. */
c906108c 4249
c2c6d25f 4250enum breakpoint_here
6c95b8df 4251breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4252{
35df4500 4253 struct bp_location *bl, **blp_tmp;
c2c6d25f 4254 int any_breakpoint_here = 0;
c906108c 4255
35df4500 4256 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4257 {
35df4500
TJB
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4260 continue;
4261
f1310107 4262 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4263 if ((breakpoint_enabled (bl->owner)
1a853c52 4264 || bl->permanent)
f1310107 4265 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4266 {
4267 if (overlay_debugging
35df4500
TJB
4268 && section_is_overlay (bl->section)
4269 && !section_is_mapped (bl->section))
075f6582 4270 continue; /* unmapped overlay -- can't be a match */
1a853c52 4271 else if (bl->permanent)
075f6582
DJ
4272 return permanent_breakpoint_here;
4273 else
4274 any_breakpoint_here = 1;
4275 }
4276 }
c906108c 4277
f486487f 4278 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4279}
4280
d35ae833
PA
4281/* See breakpoint.h. */
4282
4283int
4284breakpoint_in_range_p (struct address_space *aspace,
4285 CORE_ADDR addr, ULONGEST len)
4286{
4287 struct bp_location *bl, **blp_tmp;
4288
4289 ALL_BP_LOCATIONS (bl, blp_tmp)
4290 {
4291 if (bl->loc_type != bp_loc_software_breakpoint
4292 && bl->loc_type != bp_loc_hardware_breakpoint)
4293 continue;
4294
4295 if ((breakpoint_enabled (bl->owner)
4296 || bl->permanent)
4297 && breakpoint_location_address_range_overlap (bl, aspace,
4298 addr, len))
4299 {
4300 if (overlay_debugging
4301 && section_is_overlay (bl->section)
4302 && !section_is_mapped (bl->section))
4303 {
4304 /* Unmapped overlay -- can't be a match. */
4305 continue;
4306 }
4307
4308 return 1;
4309 }
4310 }
4311
4312 return 0;
4313}
4314
1c5cfe86
PA
4315/* Return true if there's a moribund breakpoint at PC. */
4316
4317int
6c95b8df 4318moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4319{
4320 struct bp_location *loc;
4321 int ix;
4322
4323 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4324 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4325 return 1;
4326
4327 return 0;
4328}
c2c6d25f 4329
f7ce857f
PA
4330/* Returns non-zero iff BL is inserted at PC, in address space
4331 ASPACE. */
4332
4333static int
4334bp_location_inserted_here_p (struct bp_location *bl,
4335 struct address_space *aspace, CORE_ADDR pc)
4336{
4337 if (bl->inserted
4338 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4339 aspace, pc))
4340 {
4341 if (overlay_debugging
4342 && section_is_overlay (bl->section)
4343 && !section_is_mapped (bl->section))
4344 return 0; /* unmapped overlay -- can't be a match */
4345 else
4346 return 1;
4347 }
4348 return 0;
4349}
4350
a1fd2fa5 4351/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4352
4353int
a1fd2fa5 4354breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4355{
f7ce857f 4356 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4357
f7ce857f 4358 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4359 {
f7ce857f
PA
4360 struct bp_location *bl = *blp;
4361
35df4500
TJB
4362 if (bl->loc_type != bp_loc_software_breakpoint
4363 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4364 continue;
4365
f7ce857f
PA
4366 if (bp_location_inserted_here_p (bl, aspace, pc))
4367 return 1;
c5aa993b 4368 }
c36b740a
VP
4369 return 0;
4370}
4371
a1fd2fa5
PA
4372/* This function returns non-zero iff there is a software breakpoint
4373 inserted at PC. */
c36b740a
VP
4374
4375int
a1fd2fa5
PA
4376software_breakpoint_inserted_here_p (struct address_space *aspace,
4377 CORE_ADDR pc)
4fa8626c 4378{
f7ce857f 4379 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4380
f7ce857f 4381 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4382 {
f7ce857f
PA
4383 struct bp_location *bl = *blp;
4384
35df4500 4385 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4386 continue;
4387
f7ce857f
PA
4388 if (bp_location_inserted_here_p (bl, aspace, pc))
4389 return 1;
4fa8626c
DJ
4390 }
4391
4392 return 0;
9c02b525
PA
4393}
4394
4395/* See breakpoint.h. */
4396
4397int
4398hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4399 CORE_ADDR pc)
4400{
4401 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4402
4403 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4404 {
4405 struct bp_location *bl = *blp;
4406
4407 if (bl->loc_type != bp_loc_hardware_breakpoint)
4408 continue;
4409
4410 if (bp_location_inserted_here_p (bl, aspace, pc))
4411 return 1;
4412 }
4413
4414 return 0;
4fa8626c
DJ
4415}
4416
9093389c
PA
4417int
4418hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4419 CORE_ADDR addr, ULONGEST len)
4420{
4421 struct breakpoint *bpt;
4422
4423 ALL_BREAKPOINTS (bpt)
4424 {
4425 struct bp_location *loc;
4426
4427 if (bpt->type != bp_hardware_watchpoint
4428 && bpt->type != bp_access_watchpoint)
4429 continue;
4430
4431 if (!breakpoint_enabled (bpt))
4432 continue;
4433
4434 for (loc = bpt->loc; loc; loc = loc->next)
4435 if (loc->pspace->aspace == aspace && loc->inserted)
4436 {
4437 CORE_ADDR l, h;
4438
4439 /* Check for intersection. */
768adc05
PA
4440 l = std::max<CORE_ADDR> (loc->address, addr);
4441 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4442 if (l < h)
4443 return 1;
4444 }
4445 }
4446 return 0;
4447}
c906108c 4448\f
c5aa993b 4449
c906108c
SS
4450/* bpstat stuff. External routines' interfaces are documented
4451 in breakpoint.h. */
4452
4453int
c326b90e 4454is_catchpoint (struct breakpoint *ep)
c906108c 4455{
533be4dd 4456 return (ep->type == bp_catchpoint);
c906108c
SS
4457}
4458
f431efe5
PA
4459/* Frees any storage that is part of a bpstat. Does not walk the
4460 'next' chain. */
4461
4462static void
198757a8
VP
4463bpstat_free (bpstat bs)
4464{
4465 if (bs->old_val != NULL)
4466 value_free (bs->old_val);
9add0f1b 4467 decref_counted_command_line (&bs->commands);
f431efe5 4468 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4469 xfree (bs);
4470}
4471
c906108c
SS
4472/* Clear a bpstat so that it says we are not at any breakpoint.
4473 Also free any storage that is part of a bpstat. */
4474
4475void
fba45db2 4476bpstat_clear (bpstat *bsp)
c906108c
SS
4477{
4478 bpstat p;
4479 bpstat q;
4480
4481 if (bsp == 0)
4482 return;
4483 p = *bsp;
4484 while (p != NULL)
4485 {
4486 q = p->next;
198757a8 4487 bpstat_free (p);
c906108c
SS
4488 p = q;
4489 }
4490 *bsp = NULL;
4491}
4492
4493/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4494 is part of the bpstat is copied as well. */
4495
4496bpstat
fba45db2 4497bpstat_copy (bpstat bs)
c906108c
SS
4498{
4499 bpstat p = NULL;
4500 bpstat tmp;
4501 bpstat retval = NULL;
4502
4503 if (bs == NULL)
4504 return bs;
4505
4506 for (; bs != NULL; bs = bs->next)
4507 {
4508 tmp = (bpstat) xmalloc (sizeof (*tmp));
4509 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4510 incref_counted_command_line (tmp->commands);
f431efe5 4511 incref_bp_location (tmp->bp_location_at);
31cc81e9 4512 if (bs->old_val != NULL)
3c3185ac
JK
4513 {
4514 tmp->old_val = value_copy (bs->old_val);
4515 release_value (tmp->old_val);
4516 }
31cc81e9 4517
c906108c
SS
4518 if (p == NULL)
4519 /* This is the first thing in the chain. */
4520 retval = tmp;
4521 else
4522 p->next = tmp;
4523 p = tmp;
4524 }
4525 p->next = NULL;
4526 return retval;
4527}
4528
4a64f543 4529/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4530
4531bpstat
fba45db2 4532bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4533{
c5aa993b
JM
4534 if (bsp == NULL)
4535 return NULL;
c906108c 4536
c5aa993b
JM
4537 for (; bsp != NULL; bsp = bsp->next)
4538 {
f431efe5 4539 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4540 return bsp;
4541 }
c906108c
SS
4542 return NULL;
4543}
4544
ab04a2af
TT
4545/* See breakpoint.h. */
4546
47591c29 4547int
427cd150 4548bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4549{
ab04a2af
TT
4550 for (; bsp != NULL; bsp = bsp->next)
4551 {
427cd150
TT
4552 if (bsp->breakpoint_at == NULL)
4553 {
4554 /* A moribund location can never explain a signal other than
4555 GDB_SIGNAL_TRAP. */
4556 if (sig == GDB_SIGNAL_TRAP)
47591c29 4557 return 1;
427cd150
TT
4558 }
4559 else
47591c29
PA
4560 {
4561 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4562 sig))
4563 return 1;
4564 }
ab04a2af
TT
4565 }
4566
47591c29 4567 return 0;
ab04a2af
TT
4568}
4569
4a64f543
MS
4570/* Put in *NUM the breakpoint number of the first breakpoint we are
4571 stopped at. *BSP upon return is a bpstat which points to the
4572 remaining breakpoints stopped at (but which is not guaranteed to be
4573 good for anything but further calls to bpstat_num).
4574
8671a17b
PA
4575 Return 0 if passed a bpstat which does not indicate any breakpoints.
4576 Return -1 if stopped at a breakpoint that has been deleted since
4577 we set it.
4578 Return 1 otherwise. */
c906108c
SS
4579
4580int
8671a17b 4581bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4582{
4583 struct breakpoint *b;
4584
4585 if ((*bsp) == NULL)
4586 return 0; /* No more breakpoint values */
8671a17b 4587
4a64f543
MS
4588 /* We assume we'll never have several bpstats that correspond to a
4589 single breakpoint -- otherwise, this function might return the
4590 same number more than once and this will look ugly. */
f431efe5 4591 b = (*bsp)->breakpoint_at;
8671a17b
PA
4592 *bsp = (*bsp)->next;
4593 if (b == NULL)
4594 return -1; /* breakpoint that's been deleted since */
4595
4596 *num = b->number; /* We have its number */
4597 return 1;
c906108c
SS
4598}
4599
e93ca019 4600/* See breakpoint.h. */
c906108c
SS
4601
4602void
e93ca019 4603bpstat_clear_actions (void)
c906108c 4604{
e93ca019
JK
4605 struct thread_info *tp;
4606 bpstat bs;
4607
4608 if (ptid_equal (inferior_ptid, null_ptid))
4609 return;
4610
4611 tp = find_thread_ptid (inferior_ptid);
4612 if (tp == NULL)
4613 return;
4614
4615 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4616 {
9add0f1b 4617 decref_counted_command_line (&bs->commands);
abf85f46 4618
c906108c
SS
4619 if (bs->old_val != NULL)
4620 {
4621 value_free (bs->old_val);
4622 bs->old_val = NULL;
4623 }
4624 }
4625}
4626
f3b1572e
PA
4627/* Called when a command is about to proceed the inferior. */
4628
4629static void
4630breakpoint_about_to_proceed (void)
4631{
4632 if (!ptid_equal (inferior_ptid, null_ptid))
4633 {
4634 struct thread_info *tp = inferior_thread ();
4635
4636 /* Allow inferior function calls in breakpoint commands to not
4637 interrupt the command list. When the call finishes
4638 successfully, the inferior will be standing at the same
4639 breakpoint as if nothing happened. */
16c381f0 4640 if (tp->control.in_infcall)
f3b1572e
PA
4641 return;
4642 }
4643
4644 breakpoint_proceeded = 1;
4645}
4646
4a64f543
MS
4647/* Stub for cleaning up our state if we error-out of a breakpoint
4648 command. */
c906108c 4649static void
4efb68b1 4650cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4651{
4652 executing_breakpoint_commands = 0;
4653}
4654
abf85f46
JK
4655/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4656 or its equivalent. */
4657
4658static int
4659command_line_is_silent (struct command_line *cmd)
4660{
4f45d445 4661 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4662}
4663
4a64f543
MS
4664/* Execute all the commands associated with all the breakpoints at
4665 this location. Any of these commands could cause the process to
4666 proceed beyond this point, etc. We look out for such changes by
4667 checking the global "breakpoint_proceeded" after each command.
c906108c 4668
347bddb7
PA
4669 Returns true if a breakpoint command resumed the inferior. In that
4670 case, it is the caller's responsibility to recall it again with the
4671 bpstat of the current thread. */
4672
4673static int
4674bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4675{
4676 bpstat bs;
4677 struct cleanup *old_chain;
347bddb7 4678 int again = 0;
c906108c
SS
4679
4680 /* Avoid endless recursion if a `source' command is contained
4681 in bs->commands. */
4682 if (executing_breakpoint_commands)
347bddb7 4683 return 0;
c906108c
SS
4684
4685 executing_breakpoint_commands = 1;
4686 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4687
1ac32117 4688 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4689
4a64f543 4690 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4691 bs = *bsp;
4692
4693 breakpoint_proceeded = 0;
4694 for (; bs != NULL; bs = bs->next)
4695 {
9add0f1b 4696 struct counted_command_line *ccmd;
6c50ab1c
JB
4697 struct command_line *cmd;
4698 struct cleanup *this_cmd_tree_chain;
4699
4700 /* Take ownership of the BSP's command tree, if it has one.
4701
4702 The command tree could legitimately contain commands like
4703 'step' and 'next', which call clear_proceed_status, which
4704 frees stop_bpstat's command tree. To make sure this doesn't
4705 free the tree we're executing out from under us, we need to
4706 take ownership of the tree ourselves. Since a given bpstat's
4707 commands are only executed once, we don't need to copy it; we
4708 can clear the pointer in the bpstat, and make sure we free
4709 the tree when we're done. */
9add0f1b
TT
4710 ccmd = bs->commands;
4711 bs->commands = NULL;
abf85f46
JK
4712 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4713 cmd = ccmd ? ccmd->commands : NULL;
4714 if (command_line_is_silent (cmd))
4715 {
4716 /* The action has been already done by bpstat_stop_status. */
4717 cmd = cmd->next;
4718 }
6c50ab1c 4719
c906108c
SS
4720 while (cmd != NULL)
4721 {
4722 execute_control_command (cmd);
4723
4724 if (breakpoint_proceeded)
4725 break;
4726 else
4727 cmd = cmd->next;
4728 }
6c50ab1c
JB
4729
4730 /* We can free this command tree now. */
4731 do_cleanups (this_cmd_tree_chain);
4732
c906108c 4733 if (breakpoint_proceeded)
32c1e744 4734 {
cb814510 4735 if (current_ui->async)
347bddb7
PA
4736 /* If we are in async mode, then the target might be still
4737 running, not stopped at any breakpoint, so nothing for
4738 us to do here -- just return to the event loop. */
4739 ;
32c1e744
VP
4740 else
4741 /* In sync mode, when execute_control_command returns
4742 we're already standing on the next breakpoint.
347bddb7
PA
4743 Breakpoint commands for that stop were not run, since
4744 execute_command does not run breakpoint commands --
4745 only command_line_handler does, but that one is not
4746 involved in execution of breakpoint commands. So, we
4747 can now execute breakpoint commands. It should be
4748 noted that making execute_command do bpstat actions is
4749 not an option -- in this case we'll have recursive
4750 invocation of bpstat for each breakpoint with a
4751 command, and can easily blow up GDB stack. Instead, we
4752 return true, which will trigger the caller to recall us
4753 with the new stop_bpstat. */
4754 again = 1;
4755 break;
32c1e744 4756 }
c906108c 4757 }
c2b8ed2c 4758 do_cleanups (old_chain);
347bddb7
PA
4759 return again;
4760}
4761
4762void
4763bpstat_do_actions (void)
4764{
353d1d73
JK
4765 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4766
347bddb7
PA
4767 /* Do any commands attached to breakpoint we are stopped at. */
4768 while (!ptid_equal (inferior_ptid, null_ptid)
4769 && target_has_execution
4770 && !is_exited (inferior_ptid)
4771 && !is_executing (inferior_ptid))
4772 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4773 and only return when it is stopped at the next breakpoint, we
4774 keep doing breakpoint actions until it returns false to
4775 indicate the inferior was not resumed. */
16c381f0 4776 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4777 break;
353d1d73
JK
4778
4779 discard_cleanups (cleanup_if_error);
c906108c
SS
4780}
4781
fa4727a6
DJ
4782/* Print out the (old or new) value associated with a watchpoint. */
4783
4784static void
4785watchpoint_value_print (struct value *val, struct ui_file *stream)
4786{
4787 if (val == NULL)
4788 fprintf_unfiltered (stream, _("<unreadable>"));
4789 else
79a45b7d
TT
4790 {
4791 struct value_print_options opts;
4792 get_user_print_options (&opts);
4793 value_print (val, stream, &opts);
4794 }
fa4727a6
DJ
4795}
4796
f303dbd6
PA
4797/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4798 debugging multiple threads. */
4799
4800void
4801maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4802{
112e8700 4803 if (uiout->is_mi_like_p ())
f303dbd6
PA
4804 return;
4805
112e8700 4806 uiout->text ("\n");
f303dbd6
PA
4807
4808 if (show_thread_that_caused_stop ())
4809 {
4810 const char *name;
4811 struct thread_info *thr = inferior_thread ();
4812
112e8700
SM
4813 uiout->text ("Thread ");
4814 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4815
4816 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4817 if (name != NULL)
4818 {
112e8700
SM
4819 uiout->text (" \"");
4820 uiout->field_fmt ("name", "%s", name);
4821 uiout->text ("\"");
f303dbd6
PA
4822 }
4823
112e8700 4824 uiout->text (" hit ");
f303dbd6
PA
4825 }
4826}
4827
e514a9d6 4828/* Generic routine for printing messages indicating why we
4a64f543 4829 stopped. The behavior of this function depends on the value
e514a9d6
JM
4830 'print_it' in the bpstat structure. Under some circumstances we
4831 may decide not to print anything here and delegate the task to
4a64f543 4832 normal_stop(). */
e514a9d6
JM
4833
4834static enum print_stop_action
4835print_bp_stop_message (bpstat bs)
4836{
4837 switch (bs->print_it)
4838 {
4839 case print_it_noop:
4a64f543 4840 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4841 return PRINT_UNKNOWN;
4842 break;
4843
4844 case print_it_done:
4845 /* We still want to print the frame, but we already printed the
4a64f543 4846 relevant messages. */
e514a9d6
JM
4847 return PRINT_SRC_AND_LOC;
4848 break;
4849
4850 case print_it_normal:
4f8d1dc6 4851 {
f431efe5
PA
4852 struct breakpoint *b = bs->breakpoint_at;
4853
1a6a67de
TJB
4854 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4855 which has since been deleted. */
4856 if (b == NULL)
4857 return PRINT_UNKNOWN;
4858
348d480f
PA
4859 /* Normal case. Call the breakpoint's print_it method. */
4860 return b->ops->print_it (bs);
4f8d1dc6 4861 }
348d480f 4862 break;
3086aeae 4863
e514a9d6 4864 default:
8e65ff28 4865 internal_error (__FILE__, __LINE__,
e2e0b3e5 4866 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4867 break;
c906108c 4868 }
c906108c
SS
4869}
4870
edcc5120
TT
4871/* A helper function that prints a shared library stopped event. */
4872
4873static void
4874print_solib_event (int is_catchpoint)
4875{
4876 int any_deleted
4877 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4878 int any_added
4879 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4880
4881 if (!is_catchpoint)
4882 {
4883 if (any_added || any_deleted)
112e8700 4884 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4885 else
112e8700
SM
4886 current_uiout->text (_("Stopped due to shared library event (no "
4887 "libraries added or removed)\n"));
edcc5120
TT
4888 }
4889
112e8700
SM
4890 if (current_uiout->is_mi_like_p ())
4891 current_uiout->field_string ("reason",
4892 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4893
4894 if (any_deleted)
4895 {
edcc5120
TT
4896 char *name;
4897 int ix;
4898
112e8700 4899 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4900 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4901 for (ix = 0;
4902 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4903 ix, name);
4904 ++ix)
4905 {
4906 if (ix > 0)
112e8700
SM
4907 current_uiout->text (" ");
4908 current_uiout->field_string ("library", name);
4909 current_uiout->text ("\n");
edcc5120 4910 }
edcc5120
TT
4911 }
4912
4913 if (any_added)
4914 {
4915 struct so_list *iter;
4916 int ix;
edcc5120 4917
112e8700 4918 current_uiout->text (_(" Inferior loaded "));
10f489e5 4919 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4920 for (ix = 0;
4921 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4922 ix, iter);
4923 ++ix)
4924 {
4925 if (ix > 0)
112e8700
SM
4926 current_uiout->text (" ");
4927 current_uiout->field_string ("library", iter->so_name);
4928 current_uiout->text ("\n");
edcc5120 4929 }
edcc5120
TT
4930 }
4931}
4932
e514a9d6
JM
4933/* Print a message indicating what happened. This is called from
4934 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4935 list - a list of the eventpoints that caused this stop. KIND is
4936 the target_waitkind for the stopping event. This
e514a9d6
JM
4937 routine calls the generic print routine for printing a message
4938 about reasons for stopping. This will print (for example) the
4939 "Breakpoint n," part of the output. The return value of this
4940 routine is one of:
c906108c 4941
4a64f543 4942 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4943 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4944 code to print the location. An example is
c5aa993b
JM
4945 "Breakpoint 1, " which should be followed by
4946 the location.
917317f4 4947 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4948 to also print the location part of the message.
4949 An example is the catch/throw messages, which
4a64f543 4950 don't require a location appended to the end.
917317f4 4951 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4952 further info to be printed. */
c906108c 4953
917317f4 4954enum print_stop_action
36dfb11c 4955bpstat_print (bpstat bs, int kind)
c906108c 4956{
f486487f 4957 enum print_stop_action val;
c5aa993b 4958
c906108c 4959 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4960 (Currently all watchpoints go on the bpstat whether hit or not.
4961 That probably could (should) be changed, provided care is taken
c906108c 4962 with respect to bpstat_explains_signal). */
e514a9d6
JM
4963 for (; bs; bs = bs->next)
4964 {
4965 val = print_bp_stop_message (bs);
4966 if (val == PRINT_SRC_ONLY
4967 || val == PRINT_SRC_AND_LOC
4968 || val == PRINT_NOTHING)
4969 return val;
4970 }
c906108c 4971
36dfb11c
TT
4972 /* If we had hit a shared library event breakpoint,
4973 print_bp_stop_message would print out this message. If we hit an
4974 OS-level shared library event, do the same thing. */
4975 if (kind == TARGET_WAITKIND_LOADED)
4976 {
edcc5120 4977 print_solib_event (0);
36dfb11c
TT
4978 return PRINT_NOTHING;
4979 }
4980
e514a9d6 4981 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4982 with and nothing was printed. */
917317f4 4983 return PRINT_UNKNOWN;
c906108c
SS
4984}
4985
c42bd95a
DE
4986/* Evaluate the expression EXP and return 1 if value is zero.
4987 This returns the inverse of the condition because it is called
4988 from catch_errors which returns 0 if an exception happened, and if an
4989 exception happens we want execution to stop.
4a64f543 4990 The argument is a "struct expression *" that has been cast to a
c42bd95a 4991 "void *" to make it pass through catch_errors. */
c906108c
SS
4992
4993static int
4efb68b1 4994breakpoint_cond_eval (void *exp)
c906108c 4995{
278cd55f 4996 struct value *mark = value_mark ();
c5aa993b 4997 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4998
c906108c
SS
4999 value_free_to_mark (mark);
5000 return i;
5001}
5002
5760d0ab 5003/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5004
5005static bpstat
5760d0ab 5006bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5007{
5008 bpstat bs;
5009
5010 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5011 bs->next = NULL;
5012 **bs_link_pointer = bs;
5013 *bs_link_pointer = &bs->next;
f431efe5
PA
5014 bs->breakpoint_at = bl->owner;
5015 bs->bp_location_at = bl;
5016 incref_bp_location (bl);
c906108c
SS
5017 /* If the condition is false, etc., don't do the commands. */
5018 bs->commands = NULL;
5019 bs->old_val = NULL;
5020 bs->print_it = print_it_normal;
5021 return bs;
5022}
5023\f
d983da9c
DJ
5024/* The target has stopped with waitstatus WS. Check if any hardware
5025 watchpoints have triggered, according to the target. */
5026
5027int
5028watchpoints_triggered (struct target_waitstatus *ws)
5029{
d92524f1 5030 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5031 CORE_ADDR addr;
5032 struct breakpoint *b;
5033
5034 if (!stopped_by_watchpoint)
5035 {
5036 /* We were not stopped by a watchpoint. Mark all watchpoints
5037 as not triggered. */
5038 ALL_BREAKPOINTS (b)
cc60f2e3 5039 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042
5043 w->watchpoint_triggered = watch_triggered_no;
5044 }
d983da9c
DJ
5045
5046 return 0;
5047 }
5048
5049 if (!target_stopped_data_address (&current_target, &addr))
5050 {
5051 /* We were stopped by a watchpoint, but we don't know where.
5052 Mark all watchpoints as unknown. */
5053 ALL_BREAKPOINTS (b)
cc60f2e3 5054 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_unknown;
5059 }
d983da9c 5060
3c4797ba 5061 return 1;
d983da9c
DJ
5062 }
5063
5064 /* The target could report the data address. Mark watchpoints
5065 affected by this data address as triggered, and all others as not
5066 triggered. */
5067
5068 ALL_BREAKPOINTS (b)
cc60f2e3 5069 if (is_hardware_watchpoint (b))
d983da9c 5070 {
3a5c3e22 5071 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5072 struct bp_location *loc;
d983da9c 5073
3a5c3e22 5074 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5075 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5076 {
3a5c3e22 5077 if (is_masked_watchpoint (b))
9c06b0b4 5078 {
3a5c3e22
PA
5079 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5080 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5081
5082 if (newaddr == start)
5083 {
3a5c3e22 5084 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5085 break;
5086 }
5087 }
5088 /* Exact match not required. Within range is sufficient. */
5089 else if (target_watchpoint_addr_within_range (&current_target,
5090 addr, loc->address,
5091 loc->length))
5092 {
3a5c3e22 5093 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5094 break;
5095 }
5096 }
d983da9c
DJ
5097 }
5098
5099 return 1;
5100}
5101
c906108c
SS
5102/* Possible return values for watchpoint_check (this can't be an enum
5103 because of check_errors). */
5104/* The watchpoint has been deleted. */
5105#define WP_DELETED 1
5106/* The value has changed. */
5107#define WP_VALUE_CHANGED 2
5108/* The value has not changed. */
5109#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5110/* Ignore this watchpoint, no matter if the value changed or not. */
5111#define WP_IGNORE 4
c906108c
SS
5112
5113#define BP_TEMPFLAG 1
5114#define BP_HARDWAREFLAG 2
5115
4a64f543
MS
5116/* Evaluate watchpoint condition expression and check if its value
5117 changed.
553e4c11
JB
5118
5119 P should be a pointer to struct bpstat, but is defined as a void *
5120 in order for this function to be usable with catch_errors. */
c906108c
SS
5121
5122static int
4efb68b1 5123watchpoint_check (void *p)
c906108c
SS
5124{
5125 bpstat bs = (bpstat) p;
3a5c3e22 5126 struct watchpoint *b;
c906108c
SS
5127 struct frame_info *fr;
5128 int within_current_scope;
5129
f431efe5 5130 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5131 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5132 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5133
f6bc2008
PA
5134 /* If this is a local watchpoint, we only want to check if the
5135 watchpoint frame is in scope if the current thread is the thread
5136 that was used to create the watchpoint. */
5137 if (!watchpoint_in_thread_scope (b))
60e1c644 5138 return WP_IGNORE;
f6bc2008 5139
c906108c
SS
5140 if (b->exp_valid_block == NULL)
5141 within_current_scope = 1;
5142 else
5143 {
edb3359d
DJ
5144 struct frame_info *frame = get_current_frame ();
5145 struct gdbarch *frame_arch = get_frame_arch (frame);
5146 CORE_ADDR frame_pc = get_frame_pc (frame);
5147
c9cf6e20 5148 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5149 still in the function but the stack frame has already been
5150 invalidated. Since we can't rely on the values of local
5151 variables after the stack has been destroyed, we are treating
5152 the watchpoint in that state as `not changed' without further
5153 checking. Don't mark watchpoints as changed if the current
5154 frame is in an epilogue - even if they are in some other
5155 frame, our view of the stack is likely to be wrong and
5156 frame_find_by_id could error out. */
c9cf6e20 5157 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5158 return WP_IGNORE;
a0f49112 5159
101dcfbe 5160 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5161 within_current_scope = (fr != NULL);
69fbadd5
DJ
5162
5163 /* If we've gotten confused in the unwinder, we might have
5164 returned a frame that can't describe this variable. */
edb3359d
DJ
5165 if (within_current_scope)
5166 {
5167 struct symbol *function;
5168
5169 function = get_frame_function (fr);
5170 if (function == NULL
5171 || !contained_in (b->exp_valid_block,
5172 SYMBOL_BLOCK_VALUE (function)))
5173 within_current_scope = 0;
5174 }
69fbadd5 5175
edb3359d 5176 if (within_current_scope)
c906108c
SS
5177 /* If we end up stopping, the current frame will get selected
5178 in normal_stop. So this call to select_frame won't affect
5179 the user. */
0f7d239c 5180 select_frame (fr);
c906108c 5181 }
c5aa993b 5182
c906108c
SS
5183 if (within_current_scope)
5184 {
4a64f543
MS
5185 /* We use value_{,free_to_}mark because it could be a *long*
5186 time before we return to the command level and call
5187 free_all_values. We can't call free_all_values because we
5188 might be in the middle of evaluating a function call. */
c906108c 5189
0cf6dd15 5190 int pc = 0;
9c06b0b4 5191 struct value *mark;
fa4727a6
DJ
5192 struct value *new_val;
5193
c1fc2657 5194 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5195 /* Since we don't know the exact trigger address (from
5196 stopped_data_address), just tell the user we've triggered
5197 a mask watchpoint. */
5198 return WP_VALUE_CHANGED;
5199
5200 mark = value_mark ();
4d01a485 5201 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5202
bb9d5f81
PP
5203 if (b->val_bitsize != 0)
5204 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5205
4a64f543
MS
5206 /* We use value_equal_contents instead of value_equal because
5207 the latter coerces an array to a pointer, thus comparing just
5208 the address of the array instead of its contents. This is
5209 not what we want. */
fa4727a6 5210 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5211 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5212 {
fa4727a6
DJ
5213 if (new_val != NULL)
5214 {
5215 release_value (new_val);
5216 value_free_to_mark (mark);
5217 }
c906108c
SS
5218 bs->old_val = b->val;
5219 b->val = new_val;
fa4727a6 5220 b->val_valid = 1;
c906108c
SS
5221 return WP_VALUE_CHANGED;
5222 }
5223 else
5224 {
60e1c644 5225 /* Nothing changed. */
c906108c 5226 value_free_to_mark (mark);
c906108c
SS
5227 return WP_VALUE_NOT_CHANGED;
5228 }
5229 }
5230 else
5231 {
5232 /* This seems like the only logical thing to do because
c5aa993b
JM
5233 if we temporarily ignored the watchpoint, then when
5234 we reenter the block in which it is valid it contains
5235 garbage (in the case of a function, it may have two
5236 garbage values, one before and one after the prologue).
5237 So we can't even detect the first assignment to it and
5238 watch after that (since the garbage may or may not equal
5239 the first value assigned). */
348d480f
PA
5240 /* We print all the stop information in
5241 breakpoint_ops->print_it, but in this case, by the time we
5242 call breakpoint_ops->print_it this bp will be deleted
5243 already. So we have no choice but print the information
5244 here. */
468afe6c 5245
0e454242 5246 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5247 {
5248 struct ui_out *uiout = current_uiout;
5249
112e8700
SM
5250 if (uiout->is_mi_like_p ())
5251 uiout->field_string
5252 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5253 uiout->text ("\nWatchpoint ");
c1fc2657 5254 uiout->field_int ("wpnum", b->number);
112e8700 5255 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5256 "which its expression is valid.\n");
5257 }
4ce44c66 5258
cdac0397 5259 /* Make sure the watchpoint's commands aren't executed. */
c1fc2657 5260 decref_counted_command_line (&b->commands);
d0fb5eae 5261 watchpoint_del_at_next_stop (b);
c906108c
SS
5262
5263 return WP_DELETED;
5264 }
5265}
5266
18a18393 5267/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5268 breakpoint location BL. This function does not check if we should
5269 stop, only if BL explains the stop. */
5270
18a18393 5271static int
6c95b8df 5272bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5273 struct address_space *aspace, CORE_ADDR bp_addr,
5274 const struct target_waitstatus *ws)
18a18393
VP
5275{
5276 struct breakpoint *b = bl->owner;
5277
348d480f 5278 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5279 gdb_assert (b != NULL);
5280
09ac7c10 5281 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5282}
5283
3a5c3e22
PA
5284/* Determine if the watched values have actually changed, and we
5285 should stop. If not, set BS->stop to 0. */
5286
18a18393
VP
5287static void
5288bpstat_check_watchpoint (bpstat bs)
5289{
2bdf28a0 5290 const struct bp_location *bl;
3a5c3e22 5291 struct watchpoint *b;
2bdf28a0
JK
5292
5293 /* BS is built for existing struct breakpoint. */
f431efe5 5294 bl = bs->bp_location_at;
2bdf28a0 5295 gdb_assert (bl != NULL);
3a5c3e22 5296 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5297 gdb_assert (b != NULL);
18a18393 5298
18a18393 5299 {
18a18393
VP
5300 int must_check_value = 0;
5301
c1fc2657 5302 if (b->type == bp_watchpoint)
18a18393
VP
5303 /* For a software watchpoint, we must always check the
5304 watched value. */
5305 must_check_value = 1;
5306 else if (b->watchpoint_triggered == watch_triggered_yes)
5307 /* We have a hardware watchpoint (read, write, or access)
5308 and the target earlier reported an address watched by
5309 this watchpoint. */
5310 must_check_value = 1;
5311 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5312 && b->type == bp_hardware_watchpoint)
18a18393
VP
5313 /* We were stopped by a hardware watchpoint, but the target could
5314 not report the data address. We must check the watchpoint's
5315 value. Access and read watchpoints are out of luck; without
5316 a data address, we can't figure it out. */
5317 must_check_value = 1;
3a5c3e22 5318
18a18393
VP
5319 if (must_check_value)
5320 {
3e43a32a
MS
5321 char *message
5322 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
c1fc2657 5323 b->number);
18a18393
VP
5324 struct cleanup *cleanups = make_cleanup (xfree, message);
5325 int e = catch_errors (watchpoint_check, bs, message,
5326 RETURN_MASK_ALL);
5327 do_cleanups (cleanups);
5328 switch (e)
5329 {
5330 case WP_DELETED:
5331 /* We've already printed what needs to be printed. */
5332 bs->print_it = print_it_done;
5333 /* Stop. */
5334 break;
60e1c644
PA
5335 case WP_IGNORE:
5336 bs->print_it = print_it_noop;
5337 bs->stop = 0;
5338 break;
18a18393 5339 case WP_VALUE_CHANGED:
c1fc2657 5340 if (b->type == bp_read_watchpoint)
18a18393 5341 {
85d721b8
PA
5342 /* There are two cases to consider here:
5343
4a64f543 5344 1. We're watching the triggered memory for reads.
85d721b8
PA
5345 In that case, trust the target, and always report
5346 the watchpoint hit to the user. Even though
5347 reads don't cause value changes, the value may
5348 have changed since the last time it was read, and
5349 since we're not trapping writes, we will not see
5350 those, and as such we should ignore our notion of
5351 old value.
5352
4a64f543 5353 2. We're watching the triggered memory for both
85d721b8
PA
5354 reads and writes. There are two ways this may
5355 happen:
5356
4a64f543 5357 2.1. This is a target that can't break on data
85d721b8
PA
5358 reads only, but can break on accesses (reads or
5359 writes), such as e.g., x86. We detect this case
5360 at the time we try to insert read watchpoints.
5361
4a64f543 5362 2.2. Otherwise, the target supports read
85d721b8
PA
5363 watchpoints, but, the user set an access or write
5364 watchpoint watching the same memory as this read
5365 watchpoint.
5366
5367 If we're watching memory writes as well as reads,
5368 ignore watchpoint hits when we find that the
5369 value hasn't changed, as reads don't cause
5370 changes. This still gives false positives when
5371 the program writes the same value to memory as
5372 what there was already in memory (we will confuse
5373 it for a read), but it's much better than
5374 nothing. */
5375
5376 int other_write_watchpoint = 0;
5377
5378 if (bl->watchpoint_type == hw_read)
5379 {
5380 struct breakpoint *other_b;
5381
5382 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5383 if (other_b->type == bp_hardware_watchpoint
5384 || other_b->type == bp_access_watchpoint)
85d721b8 5385 {
3a5c3e22
PA
5386 struct watchpoint *other_w =
5387 (struct watchpoint *) other_b;
5388
5389 if (other_w->watchpoint_triggered
5390 == watch_triggered_yes)
5391 {
5392 other_write_watchpoint = 1;
5393 break;
5394 }
85d721b8
PA
5395 }
5396 }
5397
5398 if (other_write_watchpoint
5399 || bl->watchpoint_type == hw_access)
5400 {
5401 /* We're watching the same memory for writes,
5402 and the value changed since the last time we
5403 updated it, so this trap must be for a write.
5404 Ignore it. */
5405 bs->print_it = print_it_noop;
5406 bs->stop = 0;
5407 }
18a18393
VP
5408 }
5409 break;
5410 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5411 if (b->type == bp_hardware_watchpoint
5412 || b->type == bp_watchpoint)
18a18393
VP
5413 {
5414 /* Don't stop: write watchpoints shouldn't fire if
5415 the value hasn't changed. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
5419 /* Stop. */
5420 break;
5421 default:
5422 /* Can't happen. */
5423 case 0:
5424 /* Error from catch_errors. */
468afe6c 5425 {
0e454242 5426 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5427 {
5428 printf_filtered (_("Watchpoint %d deleted.\n"),
c1fc2657 5429 b->number);
468afe6c
PA
5430 }
5431 watchpoint_del_at_next_stop (b);
5432 /* We've already printed what needs to be printed. */
5433 bs->print_it = print_it_done;
5434 }
18a18393
VP
5435 break;
5436 }
5437 }
5438 else /* must_check_value == 0 */
5439 {
5440 /* This is a case where some watchpoint(s) triggered, but
5441 not at the address of this watchpoint, or else no
5442 watchpoint triggered after all. So don't print
5443 anything for this watchpoint. */
5444 bs->print_it = print_it_noop;
5445 bs->stop = 0;
5446 }
5447 }
5448}
5449
7d4df6a4
DE
5450/* For breakpoints that are currently marked as telling gdb to stop,
5451 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5452 of breakpoint referred to by BS. If we should not stop for this
5453 breakpoint, set BS->stop to 0. */
f431efe5 5454
18a18393
VP
5455static void
5456bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5457{
2bdf28a0
JK
5458 const struct bp_location *bl;
5459 struct breakpoint *b;
7d4df6a4
DE
5460 int value_is_zero = 0;
5461 struct expression *cond;
5462
5463 gdb_assert (bs->stop);
2bdf28a0
JK
5464
5465 /* BS is built for existing struct breakpoint. */
f431efe5 5466 bl = bs->bp_location_at;
2bdf28a0 5467 gdb_assert (bl != NULL);
f431efe5 5468 b = bs->breakpoint_at;
2bdf28a0 5469 gdb_assert (b != NULL);
18a18393 5470
b775012e
LM
5471 /* Even if the target evaluated the condition on its end and notified GDB, we
5472 need to do so again since GDB does not know if we stopped due to a
5473 breakpoint or a single step breakpoint. */
5474
18a18393 5475 if (frame_id_p (b->frame_id)
edb3359d 5476 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5477 {
7d4df6a4
DE
5478 bs->stop = 0;
5479 return;
5480 }
60e1c644 5481
12ab52e9
PA
5482 /* If this is a thread/task-specific breakpoint, don't waste cpu
5483 evaluating the condition if this isn't the specified
5484 thread/task. */
5d5658a1 5485 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5486 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5487
6c1b0f7b
DE
5488 {
5489 bs->stop = 0;
5490 return;
5491 }
5492
6dddc817
DE
5493 /* Evaluate extension language breakpoints that have a "stop" method
5494 implemented. */
5495 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5496
7d4df6a4
DE
5497 if (is_watchpoint (b))
5498 {
5499 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5500
4d01a485 5501 cond = w->cond_exp.get ();
7d4df6a4
DE
5502 }
5503 else
4d01a485 5504 cond = bl->cond.get ();
60e1c644 5505
7d4df6a4
DE
5506 if (cond && b->disposition != disp_del_at_next_stop)
5507 {
5508 int within_current_scope = 1;
5509 struct watchpoint * w;
60e1c644 5510
7d4df6a4
DE
5511 /* We use value_mark and value_free_to_mark because it could
5512 be a long time before we return to the command level and
5513 call free_all_values. We can't call free_all_values
5514 because we might be in the middle of evaluating a
5515 function call. */
5516 struct value *mark = value_mark ();
5517
5518 if (is_watchpoint (b))
5519 w = (struct watchpoint *) b;
5520 else
5521 w = NULL;
5522
5523 /* Need to select the frame, with all that implies so that
5524 the conditions will have the right context. Because we
5525 use the frame, we will not see an inlined function's
5526 variables when we arrive at a breakpoint at the start
5527 of the inlined function; the current frame will be the
5528 call site. */
5529 if (w == NULL || w->cond_exp_valid_block == NULL)
5530 select_frame (get_current_frame ());
5531 else
18a18393 5532 {
7d4df6a4
DE
5533 struct frame_info *frame;
5534
5535 /* For local watchpoint expressions, which particular
5536 instance of a local is being watched matters, so we
5537 keep track of the frame to evaluate the expression
5538 in. To evaluate the condition however, it doesn't
5539 really matter which instantiation of the function
5540 where the condition makes sense triggers the
5541 watchpoint. This allows an expression like "watch
5542 global if q > 10" set in `func', catch writes to
5543 global on all threads that call `func', or catch
5544 writes on all recursive calls of `func' by a single
5545 thread. We simply always evaluate the condition in
5546 the innermost frame that's executing where it makes
5547 sense to evaluate the condition. It seems
5548 intuitive. */
5549 frame = block_innermost_frame (w->cond_exp_valid_block);
5550 if (frame != NULL)
5551 select_frame (frame);
5552 else
5553 within_current_scope = 0;
18a18393 5554 }
7d4df6a4
DE
5555 if (within_current_scope)
5556 value_is_zero
5557 = catch_errors (breakpoint_cond_eval, cond,
5558 "Error in testing breakpoint condition:\n",
5559 RETURN_MASK_ALL);
5560 else
18a18393 5561 {
7d4df6a4
DE
5562 warning (_("Watchpoint condition cannot be tested "
5563 "in the current scope"));
5564 /* If we failed to set the right context for this
5565 watchpoint, unconditionally report it. */
5566 value_is_zero = 0;
18a18393 5567 }
7d4df6a4
DE
5568 /* FIXME-someday, should give breakpoint #. */
5569 value_free_to_mark (mark);
18a18393 5570 }
7d4df6a4
DE
5571
5572 if (cond && value_is_zero)
5573 {
5574 bs->stop = 0;
5575 }
7d4df6a4
DE
5576 else if (b->ignore_count > 0)
5577 {
5578 b->ignore_count--;
5579 bs->stop = 0;
5580 /* Increase the hit count even though we don't stop. */
5581 ++(b->hit_count);
5582 observer_notify_breakpoint_modified (b);
5583 }
18a18393
VP
5584}
5585
1cf4d951
PA
5586/* Returns true if we need to track moribund locations of LOC's type
5587 on the current target. */
5588
5589static int
5590need_moribund_for_location_type (struct bp_location *loc)
5591{
5592 return ((loc->loc_type == bp_loc_software_breakpoint
5593 && !target_supports_stopped_by_sw_breakpoint ())
5594 || (loc->loc_type == bp_loc_hardware_breakpoint
5595 && !target_supports_stopped_by_hw_breakpoint ()));
5596}
5597
18a18393 5598
9709f61c 5599/* Get a bpstat associated with having just stopped at address
d983da9c 5600 BP_ADDR in thread PTID.
c906108c 5601
d983da9c 5602 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5603 don't understand this stop. Result is a chain of bpstat's such
5604 that:
c906108c 5605
c5aa993b 5606 if we don't understand the stop, the result is a null pointer.
c906108c 5607
c5aa993b 5608 if we understand why we stopped, the result is not null.
c906108c 5609
c5aa993b
JM
5610 Each element of the chain refers to a particular breakpoint or
5611 watchpoint at which we have stopped. (We may have stopped for
5612 several reasons concurrently.)
c906108c 5613
c5aa993b
JM
5614 Each element of the chain has valid next, breakpoint_at,
5615 commands, FIXME??? fields. */
c906108c
SS
5616
5617bpstat
6c95b8df 5618bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5619 CORE_ADDR bp_addr, ptid_t ptid,
5620 const struct target_waitstatus *ws)
c906108c 5621{
0d381245 5622 struct breakpoint *b = NULL;
afe38095 5623 struct bp_location *bl;
20874c92 5624 struct bp_location *loc;
5760d0ab
JK
5625 /* First item of allocated bpstat's. */
5626 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5627 /* Pointer to the last thing in the chain currently. */
5760d0ab 5628 bpstat bs;
20874c92 5629 int ix;
429374b8 5630 int need_remove_insert;
f431efe5 5631 int removed_any;
c906108c 5632
f431efe5
PA
5633 /* First, build the bpstat chain with locations that explain a
5634 target stop, while being careful to not set the target running,
5635 as that may invalidate locations (in particular watchpoint
5636 locations are recreated). Resuming will happen here with
5637 breakpoint conditions or watchpoint expressions that include
5638 inferior function calls. */
c5aa993b 5639
429374b8
JK
5640 ALL_BREAKPOINTS (b)
5641 {
1a853c52 5642 if (!breakpoint_enabled (b))
429374b8 5643 continue;
a5606eee 5644
429374b8
JK
5645 for (bl = b->loc; bl != NULL; bl = bl->next)
5646 {
4a64f543
MS
5647 /* For hardware watchpoints, we look only at the first
5648 location. The watchpoint_check function will work on the
5649 entire expression, not the individual locations. For
5650 read watchpoints, the watchpoints_triggered function has
5651 checked all locations already. */
429374b8
JK
5652 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5653 break;
18a18393 5654
f6592439 5655 if (!bl->enabled || bl->shlib_disabled)
429374b8 5656 continue;
c5aa993b 5657
09ac7c10 5658 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5659 continue;
c5aa993b 5660
4a64f543
MS
5661 /* Come here if it's a watchpoint, or if the break address
5662 matches. */
c5aa993b 5663
4a64f543
MS
5664 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5665 explain stop. */
c5aa993b 5666
f431efe5
PA
5667 /* Assume we stop. Should we find a watchpoint that is not
5668 actually triggered, or if the condition of the breakpoint
5669 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5670 bs->stop = 1;
5671 bs->print = 1;
d983da9c 5672
f431efe5
PA
5673 /* If this is a scope breakpoint, mark the associated
5674 watchpoint as triggered so that we will handle the
5675 out-of-scope event. We'll get to the watchpoint next
5676 iteration. */
d0fb5eae 5677 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5678 {
5679 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5680
5681 w->watchpoint_triggered = watch_triggered_yes;
5682 }
f431efe5
PA
5683 }
5684 }
5685
7c16b83e 5686 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5687 if (!target_supports_stopped_by_sw_breakpoint ()
5688 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5689 {
1cf4d951 5690 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5691 {
1cf4d951
PA
5692 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5693 && need_moribund_for_location_type (loc))
5694 {
5695 bs = bpstat_alloc (loc, &bs_link);
5696 /* For hits of moribund locations, we should just proceed. */
5697 bs->stop = 0;
5698 bs->print = 0;
5699 bs->print_it = print_it_noop;
5700 }
f431efe5
PA
5701 }
5702 }
5703
edcc5120
TT
5704 /* A bit of special processing for shlib breakpoints. We need to
5705 process solib loading here, so that the lists of loaded and
5706 unloaded libraries are correct before we handle "catch load" and
5707 "catch unload". */
5708 for (bs = bs_head; bs != NULL; bs = bs->next)
5709 {
5d268276 5710 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5711 {
5712 handle_solib_event ();
5713 break;
5714 }
5715 }
5716
f431efe5
PA
5717 /* Now go through the locations that caused the target to stop, and
5718 check whether we're interested in reporting this stop to higher
5719 layers, or whether we should resume the target transparently. */
5720
5721 removed_any = 0;
5722
5760d0ab 5723 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5724 {
5725 if (!bs->stop)
5726 continue;
5727
f431efe5 5728 b = bs->breakpoint_at;
348d480f
PA
5729 b->ops->check_status (bs);
5730 if (bs->stop)
28010a5d 5731 {
348d480f 5732 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5733
429374b8
JK
5734 if (bs->stop)
5735 {
5736 ++(b->hit_count);
8d3788bd 5737 observer_notify_breakpoint_modified (b);
c906108c 5738
4a64f543 5739 /* We will stop here. */
429374b8
JK
5740 if (b->disposition == disp_disable)
5741 {
816338b5 5742 --(b->enable_count);
1a853c52 5743 if (b->enable_count <= 0)
429374b8 5744 b->enable_state = bp_disabled;
f431efe5 5745 removed_any = 1;
429374b8
JK
5746 }
5747 if (b->silent)
5748 bs->print = 0;
5749 bs->commands = b->commands;
9add0f1b 5750 incref_counted_command_line (bs->commands);
abf85f46
JK
5751 if (command_line_is_silent (bs->commands
5752 ? bs->commands->commands : NULL))
5753 bs->print = 0;
9d6e6e84
HZ
5754
5755 b->ops->after_condition_true (bs);
429374b8
JK
5756 }
5757
348d480f 5758 }
a9b3a50f
PA
5759
5760 /* Print nothing for this entry if we don't stop or don't
5761 print. */
5762 if (!bs->stop || !bs->print)
5763 bs->print_it = print_it_noop;
429374b8 5764 }
876fa593 5765
d983da9c
DJ
5766 /* If we aren't stopping, the value of some hardware watchpoint may
5767 not have changed, but the intermediate memory locations we are
5768 watching may have. Don't bother if we're stopping; this will get
5769 done later. */
d832cb68 5770 need_remove_insert = 0;
5760d0ab
JK
5771 if (! bpstat_causes_stop (bs_head))
5772 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5773 if (!bs->stop
f431efe5
PA
5774 && bs->breakpoint_at
5775 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5776 {
3a5c3e22
PA
5777 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5778
5779 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5780 need_remove_insert = 1;
d983da9c
DJ
5781 }
5782
d832cb68 5783 if (need_remove_insert)
44702360 5784 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5785 else if (removed_any)
44702360 5786 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5787
5760d0ab 5788 return bs_head;
c906108c 5789}
628fe4e4
JK
5790
5791static void
5792handle_jit_event (void)
5793{
5794 struct frame_info *frame;
5795 struct gdbarch *gdbarch;
5796
243a9253
PA
5797 if (debug_infrun)
5798 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5799
628fe4e4
JK
5800 /* Switch terminal for any messages produced by
5801 breakpoint_re_set. */
5802 target_terminal_ours_for_output ();
5803
5804 frame = get_current_frame ();
5805 gdbarch = get_frame_arch (frame);
5806
5807 jit_event_handler (gdbarch);
5808
5809 target_terminal_inferior ();
5810}
5811
5812/* Prepare WHAT final decision for infrun. */
5813
5814/* Decide what infrun needs to do with this bpstat. */
5815
c906108c 5816struct bpstat_what
0e30163f 5817bpstat_what (bpstat bs_head)
c906108c 5818{
c906108c 5819 struct bpstat_what retval;
0e30163f 5820 bpstat bs;
c906108c 5821
628fe4e4 5822 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5823 retval.call_dummy = STOP_NONE;
186c406b 5824 retval.is_longjmp = 0;
628fe4e4 5825
0e30163f 5826 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5827 {
628fe4e4
JK
5828 /* Extract this BS's action. After processing each BS, we check
5829 if its action overrides all we've seem so far. */
5830 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5831 enum bptype bptype;
5832
c906108c 5833 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5834 {
5835 /* I suspect this can happen if it was a momentary
5836 breakpoint which has since been deleted. */
5837 bptype = bp_none;
5838 }
20874c92 5839 else
f431efe5 5840 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5841
5842 switch (bptype)
c906108c
SS
5843 {
5844 case bp_none:
628fe4e4 5845 break;
c906108c
SS
5846 case bp_breakpoint:
5847 case bp_hardware_breakpoint:
7c16b83e 5848 case bp_single_step:
c906108c
SS
5849 case bp_until:
5850 case bp_finish:
a9b3a50f 5851 case bp_shlib_event:
c906108c
SS
5852 if (bs->stop)
5853 {
5854 if (bs->print)
628fe4e4 5855 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5856 else
628fe4e4 5857 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5858 }
5859 else
628fe4e4 5860 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5861 break;
5862 case bp_watchpoint:
5863 case bp_hardware_watchpoint:
5864 case bp_read_watchpoint:
5865 case bp_access_watchpoint:
5866 if (bs->stop)
5867 {
5868 if (bs->print)
628fe4e4 5869 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5870 else
628fe4e4 5871 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5872 }
5873 else
628fe4e4
JK
5874 {
5875 /* There was a watchpoint, but we're not stopping.
5876 This requires no further action. */
5877 }
c906108c
SS
5878 break;
5879 case bp_longjmp:
e2e4d78b 5880 case bp_longjmp_call_dummy:
186c406b 5881 case bp_exception:
0a39bb32
PA
5882 if (bs->stop)
5883 {
5884 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5885 retval.is_longjmp = bptype != bp_exception;
5886 }
5887 else
5888 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5889 break;
5890 case bp_longjmp_resume:
186c406b 5891 case bp_exception_resume:
0a39bb32
PA
5892 if (bs->stop)
5893 {
5894 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5895 retval.is_longjmp = bptype == bp_longjmp_resume;
5896 }
5897 else
5898 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5899 break;
5900 case bp_step_resume:
5901 if (bs->stop)
628fe4e4
JK
5902 this_action = BPSTAT_WHAT_STEP_RESUME;
5903 else
c906108c 5904 {
628fe4e4
JK
5905 /* It is for the wrong frame. */
5906 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5907 }
c906108c 5908 break;
2c03e5be
PA
5909 case bp_hp_step_resume:
5910 if (bs->stop)
5911 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5912 else
5913 {
5914 /* It is for the wrong frame. */
5915 this_action = BPSTAT_WHAT_SINGLE;
5916 }
5917 break;
c906108c 5918 case bp_watchpoint_scope:
c4093a6a 5919 case bp_thread_event:
1900040c 5920 case bp_overlay_event:
0fd8e87f 5921 case bp_longjmp_master:
aa7d318d 5922 case bp_std_terminate_master:
186c406b 5923 case bp_exception_master:
628fe4e4 5924 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5925 break;
ce78b96d 5926 case bp_catchpoint:
c5aa993b
JM
5927 if (bs->stop)
5928 {
5929 if (bs->print)
628fe4e4 5930 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5931 else
628fe4e4 5932 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5933 }
5934 else
628fe4e4
JK
5935 {
5936 /* There was a catchpoint, but we're not stopping.
5937 This requires no further action. */
5938 }
5939 break;
628fe4e4 5940 case bp_jit_event:
628fe4e4 5941 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5942 break;
c906108c 5943 case bp_call_dummy:
53a5351d
JM
5944 /* Make sure the action is stop (silent or noisy),
5945 so infrun.c pops the dummy frame. */
aa7d318d 5946 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5947 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5948 break;
5949 case bp_std_terminate:
5950 /* Make sure the action is stop (silent or noisy),
5951 so infrun.c pops the dummy frame. */
aa7d318d 5952 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5953 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5954 break;
1042e4c0 5955 case bp_tracepoint:
7a697b8d 5956 case bp_fast_tracepoint:
0fb4aa4b 5957 case bp_static_tracepoint:
1042e4c0
SS
5958 /* Tracepoint hits should not be reported back to GDB, and
5959 if one got through somehow, it should have been filtered
5960 out already. */
5961 internal_error (__FILE__, __LINE__,
7a697b8d 5962 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5963 break;
5964 case bp_gnu_ifunc_resolver:
5965 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5966 this_action = BPSTAT_WHAT_SINGLE;
5967 break;
5968 case bp_gnu_ifunc_resolver_return:
5969 /* The breakpoint will be removed, execution will restart from the
5970 PC of the former breakpoint. */
5971 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5972 break;
e7e0cddf
SS
5973
5974 case bp_dprintf:
a11cfd87
HZ
5975 if (bs->stop)
5976 this_action = BPSTAT_WHAT_STOP_SILENT;
5977 else
5978 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5979 break;
5980
628fe4e4
JK
5981 default:
5982 internal_error (__FILE__, __LINE__,
5983 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5984 }
628fe4e4 5985
325fac50 5986 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5987 }
628fe4e4 5988
243a9253
PA
5989 return retval;
5990}
628fe4e4 5991
243a9253
PA
5992void
5993bpstat_run_callbacks (bpstat bs_head)
5994{
5995 bpstat bs;
628fe4e4 5996
0e30163f
JK
5997 for (bs = bs_head; bs != NULL; bs = bs->next)
5998 {
5999 struct breakpoint *b = bs->breakpoint_at;
6000
6001 if (b == NULL)
6002 continue;
6003 switch (b->type)
6004 {
243a9253
PA
6005 case bp_jit_event:
6006 handle_jit_event ();
6007 break;
0e30163f
JK
6008 case bp_gnu_ifunc_resolver:
6009 gnu_ifunc_resolver_stop (b);
6010 break;
6011 case bp_gnu_ifunc_resolver_return:
6012 gnu_ifunc_resolver_return_stop (b);
6013 break;
6014 }
6015 }
c906108c
SS
6016}
6017
6018/* Nonzero if we should step constantly (e.g. watchpoints on machines
6019 without hardware support). This isn't related to a specific bpstat,
6020 just to things like whether watchpoints are set. */
6021
c5aa993b 6022int
fba45db2 6023bpstat_should_step (void)
c906108c
SS
6024{
6025 struct breakpoint *b;
cc59ec59 6026
c906108c 6027 ALL_BREAKPOINTS (b)
717a8278 6028 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6029 return 1;
c906108c
SS
6030 return 0;
6031}
6032
67822962
PA
6033int
6034bpstat_causes_stop (bpstat bs)
6035{
6036 for (; bs != NULL; bs = bs->next)
6037 if (bs->stop)
6038 return 1;
6039
6040 return 0;
6041}
6042
c906108c 6043\f
c5aa993b 6044
170b53b2
UW
6045/* Compute a string of spaces suitable to indent the next line
6046 so it starts at the position corresponding to the table column
6047 named COL_NAME in the currently active table of UIOUT. */
6048
6049static char *
6050wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6051{
6052 static char wrap_indent[80];
6053 int i, total_width, width, align;
c5209615 6054 const char *text;
170b53b2
UW
6055
6056 total_width = 0;
112e8700 6057 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6058 {
6059 if (strcmp (text, col_name) == 0)
6060 {
6061 gdb_assert (total_width < sizeof wrap_indent);
6062 memset (wrap_indent, ' ', total_width);
6063 wrap_indent[total_width] = 0;
6064
6065 return wrap_indent;
6066 }
6067
6068 total_width += width + 1;
6069 }
6070
6071 return NULL;
6072}
6073
b775012e
LM
6074/* Determine if the locations of this breakpoint will have their conditions
6075 evaluated by the target, host or a mix of both. Returns the following:
6076
6077 "host": Host evals condition.
6078 "host or target": Host or Target evals condition.
6079 "target": Target evals condition.
6080*/
6081
6082static const char *
6083bp_condition_evaluator (struct breakpoint *b)
6084{
6085 struct bp_location *bl;
6086 char host_evals = 0;
6087 char target_evals = 0;
6088
6089 if (!b)
6090 return NULL;
6091
6092 if (!is_breakpoint (b))
6093 return NULL;
6094
6095 if (gdb_evaluates_breakpoint_condition_p ()
6096 || !target_supports_evaluation_of_breakpoint_conditions ())
6097 return condition_evaluation_host;
6098
6099 for (bl = b->loc; bl; bl = bl->next)
6100 {
6101 if (bl->cond_bytecode)
6102 target_evals++;
6103 else
6104 host_evals++;
6105 }
6106
6107 if (host_evals && target_evals)
6108 return condition_evaluation_both;
6109 else if (target_evals)
6110 return condition_evaluation_target;
6111 else
6112 return condition_evaluation_host;
6113}
6114
6115/* Determine the breakpoint location's condition evaluator. This is
6116 similar to bp_condition_evaluator, but for locations. */
6117
6118static const char *
6119bp_location_condition_evaluator (struct bp_location *bl)
6120{
6121 if (bl && !is_breakpoint (bl->owner))
6122 return NULL;
6123
6124 if (gdb_evaluates_breakpoint_condition_p ()
6125 || !target_supports_evaluation_of_breakpoint_conditions ())
6126 return condition_evaluation_host;
6127
6128 if (bl && bl->cond_bytecode)
6129 return condition_evaluation_target;
6130 else
6131 return condition_evaluation_host;
6132}
6133
859825b8
JK
6134/* Print the LOC location out of the list of B->LOC locations. */
6135
170b53b2
UW
6136static void
6137print_breakpoint_location (struct breakpoint *b,
6138 struct bp_location *loc)
0d381245 6139{
79a45e25 6140 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6141
6142 scoped_restore_current_program_space restore_pspace;
6c95b8df 6143
859825b8
JK
6144 if (loc != NULL && loc->shlib_disabled)
6145 loc = NULL;
6146
6c95b8df
PA
6147 if (loc != NULL)
6148 set_current_program_space (loc->pspace);
6149
56435ebe 6150 if (b->display_canonical)
d28cd78a 6151 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6152 else if (loc && loc->symtab)
0d381245
VP
6153 {
6154 struct symbol *sym
6155 = find_pc_sect_function (loc->address, loc->section);
6156 if (sym)
6157 {
112e8700
SM
6158 uiout->text ("in ");
6159 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6160 uiout->text (" ");
6161 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6162 uiout->text ("at ");
0d381245 6163 }
112e8700 6164 uiout->field_string ("file",
05cba821 6165 symtab_to_filename_for_display (loc->symtab));
112e8700 6166 uiout->text (":");
05cba821 6167
112e8700
SM
6168 if (uiout->is_mi_like_p ())
6169 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6170
112e8700 6171 uiout->field_int ("line", loc->line_number);
0d381245 6172 }
859825b8 6173 else if (loc)
0d381245 6174 {
d7e74731 6175 string_file stb;
170b53b2 6176
d7e74731 6177 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6178 demangle, "");
112e8700 6179 uiout->field_stream ("at", stb);
0d381245 6180 }
859825b8 6181 else
f00aae0f 6182 {
d28cd78a
TT
6183 uiout->field_string ("pending",
6184 event_location_to_string (b->location.get ()));
f00aae0f
KS
6185 /* If extra_string is available, it could be holding a condition
6186 or dprintf arguments. In either case, make sure it is printed,
6187 too, but only for non-MI streams. */
112e8700 6188 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6189 {
6190 if (b->type == bp_dprintf)
112e8700 6191 uiout->text (",");
f00aae0f 6192 else
112e8700
SM
6193 uiout->text (" ");
6194 uiout->text (b->extra_string);
f00aae0f
KS
6195 }
6196 }
6c95b8df 6197
b775012e
LM
6198 if (loc && is_breakpoint (b)
6199 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6200 && bp_condition_evaluator (b) == condition_evaluation_both)
6201 {
112e8700
SM
6202 uiout->text (" (");
6203 uiout->field_string ("evaluated-by",
b775012e 6204 bp_location_condition_evaluator (loc));
112e8700 6205 uiout->text (")");
b775012e 6206 }
0d381245
VP
6207}
6208
269b11a2
PA
6209static const char *
6210bptype_string (enum bptype type)
c906108c 6211{
c4093a6a
JM
6212 struct ep_type_description
6213 {
6214 enum bptype type;
a121b7c1 6215 const char *description;
c4093a6a
JM
6216 };
6217 static struct ep_type_description bptypes[] =
c906108c 6218 {
c5aa993b
JM
6219 {bp_none, "?deleted?"},
6220 {bp_breakpoint, "breakpoint"},
c906108c 6221 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6222 {bp_single_step, "sw single-step"},
c5aa993b
JM
6223 {bp_until, "until"},
6224 {bp_finish, "finish"},
6225 {bp_watchpoint, "watchpoint"},
c906108c 6226 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6227 {bp_read_watchpoint, "read watchpoint"},
6228 {bp_access_watchpoint, "acc watchpoint"},
6229 {bp_longjmp, "longjmp"},
6230 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6231 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6232 {bp_exception, "exception"},
6233 {bp_exception_resume, "exception resume"},
c5aa993b 6234 {bp_step_resume, "step resume"},
2c03e5be 6235 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6236 {bp_watchpoint_scope, "watchpoint scope"},
6237 {bp_call_dummy, "call dummy"},
aa7d318d 6238 {bp_std_terminate, "std::terminate"},
c5aa993b 6239 {bp_shlib_event, "shlib events"},
c4093a6a 6240 {bp_thread_event, "thread events"},
1900040c 6241 {bp_overlay_event, "overlay events"},
0fd8e87f 6242 {bp_longjmp_master, "longjmp master"},
aa7d318d 6243 {bp_std_terminate_master, "std::terminate master"},
186c406b 6244 {bp_exception_master, "exception master"},
ce78b96d 6245 {bp_catchpoint, "catchpoint"},
1042e4c0 6246 {bp_tracepoint, "tracepoint"},
7a697b8d 6247 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6248 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6249 {bp_dprintf, "dprintf"},
4efc6507 6250 {bp_jit_event, "jit events"},
0e30163f
JK
6251 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6252 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6253 };
269b11a2
PA
6254
6255 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6256 || ((int) type != bptypes[(int) type].type))
6257 internal_error (__FILE__, __LINE__,
6258 _("bptypes table does not describe type #%d."),
6259 (int) type);
6260
6261 return bptypes[(int) type].description;
6262}
6263
998580f1
MK
6264/* For MI, output a field named 'thread-groups' with a list as the value.
6265 For CLI, prefix the list with the string 'inf'. */
6266
6267static void
6268output_thread_groups (struct ui_out *uiout,
6269 const char *field_name,
6270 VEC(int) *inf_num,
6271 int mi_only)
6272{
112e8700 6273 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6274 int inf;
6275 int i;
6276
6277 /* For backward compatibility, don't display inferiors in CLI unless
6278 there are several. Always display them for MI. */
6279 if (!is_mi && mi_only)
6280 return;
6281
10f489e5 6282 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6283
998580f1
MK
6284 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6285 {
6286 if (is_mi)
6287 {
6288 char mi_group[10];
6289
6290 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6291 uiout->field_string (NULL, mi_group);
998580f1
MK
6292 }
6293 else
6294 {
6295 if (i == 0)
112e8700 6296 uiout->text (" inf ");
998580f1 6297 else
112e8700 6298 uiout->text (", ");
998580f1 6299
112e8700 6300 uiout->text (plongest (inf));
998580f1
MK
6301 }
6302 }
998580f1
MK
6303}
6304
269b11a2
PA
6305/* Print B to gdb_stdout. */
6306
6307static void
6308print_one_breakpoint_location (struct breakpoint *b,
6309 struct bp_location *loc,
6310 int loc_number,
6311 struct bp_location **last_loc,
269b11a2
PA
6312 int allflag)
6313{
6314 struct command_line *l;
c2c6d25f 6315 static char bpenables[] = "nynny";
c906108c 6316
79a45e25 6317 struct ui_out *uiout = current_uiout;
0d381245
VP
6318 int header_of_multiple = 0;
6319 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6320 struct value_print_options opts;
6321
6322 get_user_print_options (&opts);
0d381245
VP
6323
6324 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6325 /* See comment in print_one_breakpoint concerning treatment of
6326 breakpoints with single disabled location. */
0d381245
VP
6327 if (loc == NULL
6328 && (b->loc != NULL
6329 && (b->loc->next != NULL || !b->loc->enabled)))
6330 header_of_multiple = 1;
6331 if (loc == NULL)
6332 loc = b->loc;
6333
c4093a6a
JM
6334 annotate_record ();
6335
6336 /* 1 */
6337 annotate_field (0);
0d381245
VP
6338 if (part_of_multiple)
6339 {
6340 char *formatted;
0c6773c1 6341 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6342 uiout->field_string ("number", formatted);
0d381245
VP
6343 xfree (formatted);
6344 }
6345 else
6346 {
112e8700 6347 uiout->field_int ("number", b->number);
0d381245 6348 }
c4093a6a
JM
6349
6350 /* 2 */
6351 annotate_field (1);
0d381245 6352 if (part_of_multiple)
112e8700 6353 uiout->field_skip ("type");
269b11a2 6354 else
112e8700 6355 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6356
6357 /* 3 */
6358 annotate_field (2);
0d381245 6359 if (part_of_multiple)
112e8700 6360 uiout->field_skip ("disp");
0d381245 6361 else
112e8700 6362 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6363
c4093a6a
JM
6364
6365 /* 4 */
6366 annotate_field (3);
0d381245 6367 if (part_of_multiple)
112e8700 6368 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6369 else
112e8700
SM
6370 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6371 uiout->spaces (2);
0d381245 6372
c4093a6a
JM
6373
6374 /* 5 and 6 */
3086aeae 6375 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6376 {
4a64f543
MS
6377 /* Although the print_one can possibly print all locations,
6378 calling it here is not likely to get any nice result. So,
6379 make sure there's just one location. */
0d381245 6380 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6381 b->ops->print_one (b, last_loc);
0d381245 6382 }
3086aeae
DJ
6383 else
6384 switch (b->type)
6385 {
6386 case bp_none:
6387 internal_error (__FILE__, __LINE__,
e2e0b3e5 6388 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6389 break;
c906108c 6390
3086aeae
DJ
6391 case bp_watchpoint:
6392 case bp_hardware_watchpoint:
6393 case bp_read_watchpoint:
6394 case bp_access_watchpoint:
3a5c3e22
PA
6395 {
6396 struct watchpoint *w = (struct watchpoint *) b;
6397
6398 /* Field 4, the address, is omitted (which makes the columns
6399 not line up too nicely with the headers, but the effect
6400 is relatively readable). */
6401 if (opts.addressprint)
112e8700 6402 uiout->field_skip ("addr");
3a5c3e22 6403 annotate_field (5);
112e8700 6404 uiout->field_string ("what", w->exp_string);
3a5c3e22 6405 }
3086aeae
DJ
6406 break;
6407
3086aeae
DJ
6408 case bp_breakpoint:
6409 case bp_hardware_breakpoint:
7c16b83e 6410 case bp_single_step:
3086aeae
DJ
6411 case bp_until:
6412 case bp_finish:
6413 case bp_longjmp:
6414 case bp_longjmp_resume:
e2e4d78b 6415 case bp_longjmp_call_dummy:
186c406b
TT
6416 case bp_exception:
6417 case bp_exception_resume:
3086aeae 6418 case bp_step_resume:
2c03e5be 6419 case bp_hp_step_resume:
3086aeae
DJ
6420 case bp_watchpoint_scope:
6421 case bp_call_dummy:
aa7d318d 6422 case bp_std_terminate:
3086aeae
DJ
6423 case bp_shlib_event:
6424 case bp_thread_event:
6425 case bp_overlay_event:
0fd8e87f 6426 case bp_longjmp_master:
aa7d318d 6427 case bp_std_terminate_master:
186c406b 6428 case bp_exception_master:
1042e4c0 6429 case bp_tracepoint:
7a697b8d 6430 case bp_fast_tracepoint:
0fb4aa4b 6431 case bp_static_tracepoint:
e7e0cddf 6432 case bp_dprintf:
4efc6507 6433 case bp_jit_event:
0e30163f
JK
6434 case bp_gnu_ifunc_resolver:
6435 case bp_gnu_ifunc_resolver_return:
79a45b7d 6436 if (opts.addressprint)
3086aeae
DJ
6437 {
6438 annotate_field (4);
54e52265 6439 if (header_of_multiple)
112e8700 6440 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6441 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6442 uiout->field_string ("addr", "<PENDING>");
0101ce28 6443 else
112e8700 6444 uiout->field_core_addr ("addr",
5af949e3 6445 loc->gdbarch, loc->address);
3086aeae
DJ
6446 }
6447 annotate_field (5);
0d381245 6448 if (!header_of_multiple)
170b53b2 6449 print_breakpoint_location (b, loc);
0d381245 6450 if (b->loc)
a6d9a66e 6451 *last_loc = b->loc;
3086aeae
DJ
6452 break;
6453 }
c906108c 6454
6c95b8df 6455
998580f1 6456 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6457 {
6458 struct inferior *inf;
998580f1
MK
6459 VEC(int) *inf_num = NULL;
6460 int mi_only = 1;
6c95b8df 6461
998580f1 6462 ALL_INFERIORS (inf)
6c95b8df
PA
6463 {
6464 if (inf->pspace == loc->pspace)
998580f1 6465 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6466 }
998580f1
MK
6467
6468 /* For backward compatibility, don't display inferiors in CLI unless
6469 there are several. Always display for MI. */
6470 if (allflag
6471 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6472 && (number_of_program_spaces () > 1
6473 || number_of_inferiors () > 1)
6474 /* LOC is for existing B, it cannot be in
6475 moribund_locations and thus having NULL OWNER. */
6476 && loc->owner->type != bp_catchpoint))
6477 mi_only = 0;
6478 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6479 VEC_free (int, inf_num);
6c95b8df
PA
6480 }
6481
4a306c9a 6482 if (!part_of_multiple)
c4093a6a 6483 {
4a306c9a
JB
6484 if (b->thread != -1)
6485 {
6486 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6487 "stop only in" line a little further down. */
112e8700
SM
6488 uiout->text (" thread ");
6489 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6490 }
6491 else if (b->task != 0)
6492 {
112e8700
SM
6493 uiout->text (" task ");
6494 uiout->field_int ("task", b->task);
4a306c9a 6495 }
c4093a6a 6496 }
f1310107 6497
112e8700 6498 uiout->text ("\n");
f1310107 6499
348d480f 6500 if (!part_of_multiple)
f1310107
TJB
6501 b->ops->print_one_detail (b, uiout);
6502
0d381245 6503 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6504 {
6505 annotate_field (6);
112e8700 6506 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6507 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6508 the frame ID. */
112e8700 6509 uiout->field_core_addr ("frame",
5af949e3 6510 b->gdbarch, b->frame_id.stack_addr);
112e8700 6511 uiout->text ("\n");
c4093a6a
JM
6512 }
6513
28010a5d 6514 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6515 {
6516 annotate_field (7);
d77f58be 6517 if (is_tracepoint (b))
112e8700 6518 uiout->text ("\ttrace only if ");
1042e4c0 6519 else
112e8700
SM
6520 uiout->text ("\tstop only if ");
6521 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6522
6523 /* Print whether the target is doing the breakpoint's condition
6524 evaluation. If GDB is doing the evaluation, don't print anything. */
6525 if (is_breakpoint (b)
6526 && breakpoint_condition_evaluation_mode ()
6527 == condition_evaluation_target)
6528 {
112e8700
SM
6529 uiout->text (" (");
6530 uiout->field_string ("evaluated-by",
b775012e 6531 bp_condition_evaluator (b));
112e8700 6532 uiout->text (" evals)");
b775012e 6533 }
112e8700 6534 uiout->text ("\n");
0101ce28
JJ
6535 }
6536
0d381245 6537 if (!part_of_multiple && b->thread != -1)
c4093a6a 6538 {
4a64f543 6539 /* FIXME should make an annotation for this. */
112e8700
SM
6540 uiout->text ("\tstop only in thread ");
6541 if (uiout->is_mi_like_p ())
6542 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6543 else
6544 {
6545 struct thread_info *thr = find_thread_global_id (b->thread);
6546
112e8700 6547 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6548 }
112e8700 6549 uiout->text ("\n");
c4093a6a
JM
6550 }
6551
556ec64d
YQ
6552 if (!part_of_multiple)
6553 {
6554 if (b->hit_count)
31f56a27
YQ
6555 {
6556 /* FIXME should make an annotation for this. */
6557 if (is_catchpoint (b))
112e8700 6558 uiout->text ("\tcatchpoint");
31f56a27 6559 else if (is_tracepoint (b))
112e8700 6560 uiout->text ("\ttracepoint");
31f56a27 6561 else
112e8700
SM
6562 uiout->text ("\tbreakpoint");
6563 uiout->text (" already hit ");
6564 uiout->field_int ("times", b->hit_count);
31f56a27 6565 if (b->hit_count == 1)
112e8700 6566 uiout->text (" time\n");
31f56a27 6567 else
112e8700 6568 uiout->text (" times\n");
31f56a27 6569 }
556ec64d
YQ
6570 else
6571 {
31f56a27 6572 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6573 if (uiout->is_mi_like_p ())
6574 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6575 }
6576 }
8b93c638 6577
0d381245 6578 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6579 {
6580 annotate_field (8);
112e8700
SM
6581 uiout->text ("\tignore next ");
6582 uiout->field_int ("ignore", b->ignore_count);
6583 uiout->text (" hits\n");
c4093a6a 6584 }
059fb39f 6585
816338b5
SS
6586 /* Note that an enable count of 1 corresponds to "enable once"
6587 behavior, which is reported by the combination of enablement and
6588 disposition, so we don't need to mention it here. */
6589 if (!part_of_multiple && b->enable_count > 1)
6590 {
6591 annotate_field (8);
112e8700 6592 uiout->text ("\tdisable after ");
816338b5
SS
6593 /* Tweak the wording to clarify that ignore and enable counts
6594 are distinct, and have additive effect. */
6595 if (b->ignore_count)
112e8700 6596 uiout->text ("additional ");
816338b5 6597 else
112e8700
SM
6598 uiout->text ("next ");
6599 uiout->field_int ("enable", b->enable_count);
6600 uiout->text (" hits\n");
816338b5
SS
6601 }
6602
f196051f
SS
6603 if (!part_of_multiple && is_tracepoint (b))
6604 {
6605 struct tracepoint *tp = (struct tracepoint *) b;
6606
6607 if (tp->traceframe_usage)
6608 {
112e8700
SM
6609 uiout->text ("\ttrace buffer usage ");
6610 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6611 uiout->text (" bytes\n");
f196051f
SS
6612 }
6613 }
d3ce09f5 6614
9add0f1b 6615 l = b->commands ? b->commands->commands : NULL;
059fb39f 6616 if (!part_of_multiple && l)
c4093a6a
JM
6617 {
6618 annotate_field (9);
2e783024 6619 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6620 print_command_lines (uiout, l, 4);
c4093a6a 6621 }
d24317b4 6622
d9b3f62e 6623 if (is_tracepoint (b))
1042e4c0 6624 {
d9b3f62e
PA
6625 struct tracepoint *t = (struct tracepoint *) b;
6626
6627 if (!part_of_multiple && t->pass_count)
6628 {
6629 annotate_field (10);
112e8700
SM
6630 uiout->text ("\tpass count ");
6631 uiout->field_int ("pass", t->pass_count);
6632 uiout->text (" \n");
d9b3f62e 6633 }
f2a8bc8a
YQ
6634
6635 /* Don't display it when tracepoint or tracepoint location is
6636 pending. */
6637 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6638 {
6639 annotate_field (11);
6640
112e8700
SM
6641 if (uiout->is_mi_like_p ())
6642 uiout->field_string ("installed",
f2a8bc8a
YQ
6643 loc->inserted ? "y" : "n");
6644 else
6645 {
6646 if (loc->inserted)
112e8700 6647 uiout->text ("\t");
f2a8bc8a 6648 else
112e8700
SM
6649 uiout->text ("\tnot ");
6650 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6651 }
6652 }
1042e4c0
SS
6653 }
6654
112e8700 6655 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6656 {
3a5c3e22
PA
6657 if (is_watchpoint (b))
6658 {
6659 struct watchpoint *w = (struct watchpoint *) b;
6660
112e8700 6661 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6662 }
f00aae0f 6663 else if (b->location != NULL
d28cd78a 6664 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6665 uiout->field_string ("original-location",
d28cd78a 6666 event_location_to_string (b->location.get ()));
d24317b4 6667 }
c4093a6a 6668}
c5aa993b 6669
0d381245
VP
6670static void
6671print_one_breakpoint (struct breakpoint *b,
4a64f543 6672 struct bp_location **last_loc,
6c95b8df 6673 int allflag)
0d381245 6674{
79a45e25 6675 struct ui_out *uiout = current_uiout;
8d3788bd 6676
2e783024
TT
6677 {
6678 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6679
2e783024
TT
6680 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6681 }
0d381245
VP
6682
6683 /* If this breakpoint has custom print function,
6684 it's already printed. Otherwise, print individual
6685 locations, if any. */
6686 if (b->ops == NULL || b->ops->print_one == NULL)
6687 {
4a64f543
MS
6688 /* If breakpoint has a single location that is disabled, we
6689 print it as if it had several locations, since otherwise it's
6690 hard to represent "breakpoint enabled, location disabled"
6691 situation.
6692
6693 Note that while hardware watchpoints have several locations
a3be7890 6694 internally, that's not a property exposed to user. */
0d381245 6695 if (b->loc
a5606eee 6696 && !is_hardware_watchpoint (b)
8d3788bd 6697 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6698 {
6699 struct bp_location *loc;
6700 int n = 1;
8d3788bd 6701
0d381245 6702 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6703 {
2e783024 6704 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6705 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6706 }
0d381245
VP
6707 }
6708 }
6709}
6710
a6d9a66e
UW
6711static int
6712breakpoint_address_bits (struct breakpoint *b)
6713{
6714 int print_address_bits = 0;
6715 struct bp_location *loc;
6716
c6d81124
PA
6717 /* Software watchpoints that aren't watching memory don't have an
6718 address to print. */
6719 if (is_no_memory_software_watchpoint (b))
6720 return 0;
6721
a6d9a66e
UW
6722 for (loc = b->loc; loc; loc = loc->next)
6723 {
c7437ca6
PA
6724 int addr_bit;
6725
c7437ca6 6726 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6727 if (addr_bit > print_address_bits)
6728 print_address_bits = addr_bit;
6729 }
6730
6731 return print_address_bits;
6732}
0d381245 6733
c4093a6a
JM
6734struct captured_breakpoint_query_args
6735 {
6736 int bnum;
6737 };
c5aa993b 6738
c4093a6a 6739static int
2b65245e 6740do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6741{
9a3c8263
SM
6742 struct captured_breakpoint_query_args *args
6743 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6744 struct breakpoint *b;
a6d9a66e 6745 struct bp_location *dummy_loc = NULL;
cc59ec59 6746
c4093a6a
JM
6747 ALL_BREAKPOINTS (b)
6748 {
6749 if (args->bnum == b->number)
c5aa993b 6750 {
12c5a436 6751 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6752 return GDB_RC_OK;
c5aa993b 6753 }
c4093a6a
JM
6754 }
6755 return GDB_RC_NONE;
6756}
c5aa993b 6757
c4093a6a 6758enum gdb_rc
4a64f543
MS
6759gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6760 char **error_message)
c4093a6a
JM
6761{
6762 struct captured_breakpoint_query_args args;
cc59ec59 6763
c4093a6a
JM
6764 args.bnum = bnum;
6765 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6766 an error. */
b0b13bb4
DJ
6767 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6768 error_message, RETURN_MASK_ALL) < 0)
6769 return GDB_RC_FAIL;
6770 else
6771 return GDB_RC_OK;
c4093a6a 6772}
c5aa993b 6773
09d682a4
TT
6774/* Return true if this breakpoint was set by the user, false if it is
6775 internal or momentary. */
6776
6777int
6778user_breakpoint_p (struct breakpoint *b)
6779{
46c6471b 6780 return b->number > 0;
09d682a4
TT
6781}
6782
93daf339
TT
6783/* See breakpoint.h. */
6784
6785int
6786pending_breakpoint_p (struct breakpoint *b)
6787{
6788 return b->loc == NULL;
6789}
6790
7f3b0473 6791/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6792 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6793 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6794 FILTER is non-NULL, call it on each breakpoint and only include the
6795 ones for which it returns non-zero. Return the total number of
6796 breakpoints listed. */
c906108c 6797
d77f58be 6798static int
e5a67952 6799breakpoint_1 (char *args, int allflag,
4a64f543 6800 int (*filter) (const struct breakpoint *))
c4093a6a 6801{
52f0bd74 6802 struct breakpoint *b;
a6d9a66e 6803 struct bp_location *last_loc = NULL;
7f3b0473 6804 int nr_printable_breakpoints;
79a45b7d 6805 struct value_print_options opts;
a6d9a66e 6806 int print_address_bits = 0;
269b11a2 6807 int print_type_col_width = 14;
79a45e25 6808 struct ui_out *uiout = current_uiout;
269b11a2 6809
79a45b7d
TT
6810 get_user_print_options (&opts);
6811
4a64f543
MS
6812 /* Compute the number of rows in the table, as well as the size
6813 required for address fields. */
7f3b0473
AC
6814 nr_printable_breakpoints = 0;
6815 ALL_BREAKPOINTS (b)
e5a67952
MS
6816 {
6817 /* If we have a filter, only list the breakpoints it accepts. */
6818 if (filter && !filter (b))
6819 continue;
6820
6821 /* If we have an "args" string, it is a list of breakpoints to
6822 accept. Skip the others. */
6823 if (args != NULL && *args != '\0')
6824 {
6825 if (allflag && parse_and_eval_long (args) != b->number)
6826 continue;
6827 if (!allflag && !number_is_in_list (args, b->number))
6828 continue;
6829 }
269b11a2 6830
e5a67952
MS
6831 if (allflag || user_breakpoint_p (b))
6832 {
6833 int addr_bit, type_len;
a6d9a66e 6834
e5a67952
MS
6835 addr_bit = breakpoint_address_bits (b);
6836 if (addr_bit > print_address_bits)
6837 print_address_bits = addr_bit;
269b11a2 6838
e5a67952
MS
6839 type_len = strlen (bptype_string (b->type));
6840 if (type_len > print_type_col_width)
6841 print_type_col_width = type_len;
6842
6843 nr_printable_breakpoints++;
6844 }
6845 }
7f3b0473 6846
4a2b031d
TT
6847 {
6848 ui_out_emit_table table_emitter (uiout,
6849 opts.addressprint ? 6 : 5,
6850 nr_printable_breakpoints,
6851 "BreakpointTable");
6852
6853 if (nr_printable_breakpoints > 0)
6854 annotate_breakpoints_headers ();
6855 if (nr_printable_breakpoints > 0)
6856 annotate_field (0);
6857 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6858 if (nr_printable_breakpoints > 0)
6859 annotate_field (1);
6860 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (2);
6863 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6864 if (nr_printable_breakpoints > 0)
6865 annotate_field (3);
6866 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6867 if (opts.addressprint)
6868 {
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (4);
6871 if (print_address_bits <= 32)
6872 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6873 else
6874 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6875 }
6876 if (nr_printable_breakpoints > 0)
6877 annotate_field (5);
6878 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6879 uiout->table_body ();
6880 if (nr_printable_breakpoints > 0)
6881 annotate_breakpoints_table ();
6882
6883 ALL_BREAKPOINTS (b)
6884 {
6885 QUIT;
6886 /* If we have a filter, only list the breakpoints it accepts. */
6887 if (filter && !filter (b))
6888 continue;
e5a67952 6889
4a2b031d
TT
6890 /* If we have an "args" string, it is a list of breakpoints to
6891 accept. Skip the others. */
e5a67952 6892
4a2b031d
TT
6893 if (args != NULL && *args != '\0')
6894 {
6895 if (allflag) /* maintenance info breakpoint */
6896 {
6897 if (parse_and_eval_long (args) != b->number)
6898 continue;
6899 }
6900 else /* all others */
6901 {
6902 if (!number_is_in_list (args, b->number))
6903 continue;
6904 }
6905 }
6906 /* We only print out user settable breakpoints unless the
6907 allflag is set. */
6908 if (allflag || user_breakpoint_p (b))
6909 print_one_breakpoint (b, &last_loc, allflag);
6910 }
6911 }
698384cd 6912
7f3b0473 6913 if (nr_printable_breakpoints == 0)
c906108c 6914 {
4a64f543
MS
6915 /* If there's a filter, let the caller decide how to report
6916 empty list. */
d77f58be
SS
6917 if (!filter)
6918 {
e5a67952 6919 if (args == NULL || *args == '\0')
112e8700 6920 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6921 else
112e8700 6922 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6923 args);
d77f58be 6924 }
c906108c
SS
6925 }
6926 else
c4093a6a 6927 {
a6d9a66e
UW
6928 if (last_loc && !server_command)
6929 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6930 }
c906108c 6931
4a64f543 6932 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6933 there have been breakpoints? */
c906108c 6934 annotate_breakpoints_table_end ();
d77f58be
SS
6935
6936 return nr_printable_breakpoints;
c906108c
SS
6937}
6938
ad443146
SS
6939/* Display the value of default-collect in a way that is generally
6940 compatible with the breakpoint list. */
6941
6942static void
6943default_collect_info (void)
6944{
79a45e25
PA
6945 struct ui_out *uiout = current_uiout;
6946
ad443146
SS
6947 /* If it has no value (which is frequently the case), say nothing; a
6948 message like "No default-collect." gets in user's face when it's
6949 not wanted. */
6950 if (!*default_collect)
6951 return;
6952
6953 /* The following phrase lines up nicely with per-tracepoint collect
6954 actions. */
112e8700
SM
6955 uiout->text ("default collect ");
6956 uiout->field_string ("default-collect", default_collect);
6957 uiout->text (" \n");
ad443146
SS
6958}
6959
c906108c 6960static void
e5a67952 6961breakpoints_info (char *args, int from_tty)
c906108c 6962{
e5a67952 6963 breakpoint_1 (args, 0, NULL);
ad443146
SS
6964
6965 default_collect_info ();
d77f58be
SS
6966}
6967
6968static void
e5a67952 6969watchpoints_info (char *args, int from_tty)
d77f58be 6970{
e5a67952 6971 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6972 struct ui_out *uiout = current_uiout;
d77f58be
SS
6973
6974 if (num_printed == 0)
6975 {
e5a67952 6976 if (args == NULL || *args == '\0')
112e8700 6977 uiout->message ("No watchpoints.\n");
d77f58be 6978 else
112e8700 6979 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6980 }
c906108c
SS
6981}
6982
7a292a7a 6983static void
e5a67952 6984maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6985{
e5a67952 6986 breakpoint_1 (args, 1, NULL);
ad443146
SS
6987
6988 default_collect_info ();
c906108c
SS
6989}
6990
0d381245 6991static int
714835d5 6992breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6993 struct program_space *pspace,
714835d5 6994 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6995{
6996 struct bp_location *bl = b->loc;
cc59ec59 6997
0d381245
VP
6998 for (; bl; bl = bl->next)
6999 {
6c95b8df
PA
7000 if (bl->pspace == pspace
7001 && bl->address == pc
0d381245
VP
7002 && (!overlay_debugging || bl->section == section))
7003 return 1;
7004 }
7005 return 0;
7006}
7007
672f9b60 7008/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7009 concerns with logical breakpoints, so we match program spaces, not
7010 address spaces. */
c906108c
SS
7011
7012static void
6c95b8df
PA
7013describe_other_breakpoints (struct gdbarch *gdbarch,
7014 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7015 struct obj_section *section, int thread)
c906108c 7016{
52f0bd74
AC
7017 int others = 0;
7018 struct breakpoint *b;
c906108c
SS
7019
7020 ALL_BREAKPOINTS (b)
672f9b60
KP
7021 others += (user_breakpoint_p (b)
7022 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7023 if (others > 0)
7024 {
a3f17187
AC
7025 if (others == 1)
7026 printf_filtered (_("Note: breakpoint "));
7027 else /* if (others == ???) */
7028 printf_filtered (_("Note: breakpoints "));
c906108c 7029 ALL_BREAKPOINTS (b)
672f9b60 7030 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7031 {
7032 others--;
7033 printf_filtered ("%d", b->number);
7034 if (b->thread == -1 && thread != -1)
7035 printf_filtered (" (all threads)");
7036 else if (b->thread != -1)
7037 printf_filtered (" (thread %d)", b->thread);
7038 printf_filtered ("%s%s ",
059fb39f 7039 ((b->enable_state == bp_disabled
f8eba3c6 7040 || b->enable_state == bp_call_disabled)
0d381245 7041 ? " (disabled)"
0d381245
VP
7042 : ""),
7043 (others > 1) ? ","
7044 : ((others == 1) ? " and" : ""));
7045 }
a3f17187 7046 printf_filtered (_("also set at pc "));
5af949e3 7047 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7048 printf_filtered (".\n");
7049 }
7050}
7051\f
c906108c 7052
e4f237da 7053/* Return true iff it is meaningful to use the address member of
244558af
LM
7054 BPT locations. For some breakpoint types, the locations' address members
7055 are irrelevant and it makes no sense to attempt to compare them to other
7056 addresses (or use them for any other purpose either).
e4f237da 7057
4a64f543 7058 More specifically, each of the following breakpoint types will
244558af 7059 always have a zero valued location address and we don't want to mark
4a64f543 7060 breakpoints of any of these types to be a duplicate of an actual
244558af 7061 breakpoint location at address zero:
e4f237da
KB
7062
7063 bp_watchpoint
2d134ed3
PA
7064 bp_catchpoint
7065
7066*/
e4f237da
KB
7067
7068static int
7069breakpoint_address_is_meaningful (struct breakpoint *bpt)
7070{
7071 enum bptype type = bpt->type;
7072
2d134ed3
PA
7073 return (type != bp_watchpoint && type != bp_catchpoint);
7074}
7075
7076/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7077 true if LOC1 and LOC2 represent the same watchpoint location. */
7078
7079static int
4a64f543
MS
7080watchpoint_locations_match (struct bp_location *loc1,
7081 struct bp_location *loc2)
2d134ed3 7082{
3a5c3e22
PA
7083 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7084 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7085
7086 /* Both of them must exist. */
7087 gdb_assert (w1 != NULL);
7088 gdb_assert (w2 != NULL);
2bdf28a0 7089
4a64f543
MS
7090 /* If the target can evaluate the condition expression in hardware,
7091 then we we need to insert both watchpoints even if they are at
7092 the same place. Otherwise the watchpoint will only trigger when
7093 the condition of whichever watchpoint was inserted evaluates to
7094 true, not giving a chance for GDB to check the condition of the
7095 other watchpoint. */
3a5c3e22 7096 if ((w1->cond_exp
4a64f543
MS
7097 && target_can_accel_watchpoint_condition (loc1->address,
7098 loc1->length,
0cf6dd15 7099 loc1->watchpoint_type,
4d01a485 7100 w1->cond_exp.get ()))
3a5c3e22 7101 || (w2->cond_exp
4a64f543
MS
7102 && target_can_accel_watchpoint_condition (loc2->address,
7103 loc2->length,
0cf6dd15 7104 loc2->watchpoint_type,
4d01a485 7105 w2->cond_exp.get ())))
0cf6dd15
TJB
7106 return 0;
7107
85d721b8
PA
7108 /* Note that this checks the owner's type, not the location's. In
7109 case the target does not support read watchpoints, but does
7110 support access watchpoints, we'll have bp_read_watchpoint
7111 watchpoints with hw_access locations. Those should be considered
7112 duplicates of hw_read locations. The hw_read locations will
7113 become hw_access locations later. */
2d134ed3
PA
7114 return (loc1->owner->type == loc2->owner->type
7115 && loc1->pspace->aspace == loc2->pspace->aspace
7116 && loc1->address == loc2->address
7117 && loc1->length == loc2->length);
e4f237da
KB
7118}
7119
31e77af2 7120/* See breakpoint.h. */
6c95b8df 7121
31e77af2 7122int
6c95b8df
PA
7123breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7124 struct address_space *aspace2, CORE_ADDR addr2)
7125{
f5656ead 7126 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7127 || aspace1 == aspace2)
7128 && addr1 == addr2);
7129}
7130
f1310107
TJB
7131/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7132 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7133 matches ASPACE2. On targets that have global breakpoints, the address
7134 space doesn't really matter. */
7135
7136static int
7137breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7138 int len1, struct address_space *aspace2,
7139 CORE_ADDR addr2)
7140{
f5656ead 7141 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7142 || aspace1 == aspace2)
7143 && addr2 >= addr1 && addr2 < addr1 + len1);
7144}
7145
7146/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7147 a ranged breakpoint. In most targets, a match happens only if ASPACE
7148 matches the breakpoint's address space. On targets that have global
7149 breakpoints, the address space doesn't really matter. */
7150
7151static int
7152breakpoint_location_address_match (struct bp_location *bl,
7153 struct address_space *aspace,
7154 CORE_ADDR addr)
7155{
7156 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7157 aspace, addr)
7158 || (bl->length
7159 && breakpoint_address_match_range (bl->pspace->aspace,
7160 bl->address, bl->length,
7161 aspace, addr)));
7162}
7163
d35ae833
PA
7164/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7165 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7166 match happens only if ASPACE matches the breakpoint's address
7167 space. On targets that have global breakpoints, the address space
7168 doesn't really matter. */
7169
7170static int
7171breakpoint_location_address_range_overlap (struct bp_location *bl,
7172 struct address_space *aspace,
7173 CORE_ADDR addr, int len)
7174{
7175 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7176 || bl->pspace->aspace == aspace)
7177 {
7178 int bl_len = bl->length != 0 ? bl->length : 1;
7179
7180 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7181 return 1;
7182 }
7183 return 0;
7184}
7185
1e4d1764
YQ
7186/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7187 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7188 true, otherwise returns false. */
7189
7190static int
7191tracepoint_locations_match (struct bp_location *loc1,
7192 struct bp_location *loc2)
7193{
7194 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7195 /* Since tracepoint locations are never duplicated with others', tracepoint
7196 locations at the same address of different tracepoints are regarded as
7197 different locations. */
7198 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7199 else
7200 return 0;
7201}
7202
2d134ed3
PA
7203/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7204 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7205 represent the same location. */
7206
7207static int
4a64f543
MS
7208breakpoint_locations_match (struct bp_location *loc1,
7209 struct bp_location *loc2)
2d134ed3 7210{
2bdf28a0
JK
7211 int hw_point1, hw_point2;
7212
7213 /* Both of them must not be in moribund_locations. */
7214 gdb_assert (loc1->owner != NULL);
7215 gdb_assert (loc2->owner != NULL);
7216
7217 hw_point1 = is_hardware_watchpoint (loc1->owner);
7218 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7219
7220 if (hw_point1 != hw_point2)
7221 return 0;
7222 else if (hw_point1)
7223 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7224 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7225 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7226 else
f1310107
TJB
7227 /* We compare bp_location.length in order to cover ranged breakpoints. */
7228 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7229 loc2->pspace->aspace, loc2->address)
7230 && loc1->length == loc2->length);
2d134ed3
PA
7231}
7232
76897487
KB
7233static void
7234breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7235 int bnum, int have_bnum)
7236{
f63fbe86
MS
7237 /* The longest string possibly returned by hex_string_custom
7238 is 50 chars. These must be at least that big for safety. */
7239 char astr1[64];
7240 char astr2[64];
76897487 7241
bb599908
PH
7242 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7243 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7244 if (have_bnum)
8a3fe4f8 7245 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7246 bnum, astr1, astr2);
7247 else
8a3fe4f8 7248 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7249}
7250
4a64f543
MS
7251/* Adjust a breakpoint's address to account for architectural
7252 constraints on breakpoint placement. Return the adjusted address.
7253 Note: Very few targets require this kind of adjustment. For most
7254 targets, this function is simply the identity function. */
76897487
KB
7255
7256static CORE_ADDR
a6d9a66e
UW
7257adjust_breakpoint_address (struct gdbarch *gdbarch,
7258 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7259{
a6d9a66e 7260 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7261 {
7262 /* Very few targets need any kind of breakpoint adjustment. */
7263 return bpaddr;
7264 }
88f7da05
KB
7265 else if (bptype == bp_watchpoint
7266 || bptype == bp_hardware_watchpoint
7267 || bptype == bp_read_watchpoint
7268 || bptype == bp_access_watchpoint
fe798b75 7269 || bptype == bp_catchpoint)
88f7da05
KB
7270 {
7271 /* Watchpoints and the various bp_catch_* eventpoints should not
7272 have their addresses modified. */
7273 return bpaddr;
7274 }
7c16b83e
PA
7275 else if (bptype == bp_single_step)
7276 {
7277 /* Single-step breakpoints should not have their addresses
7278 modified. If there's any architectural constrain that
7279 applies to this address, then it should have already been
7280 taken into account when the breakpoint was created in the
7281 first place. If we didn't do this, stepping through e.g.,
7282 Thumb-2 IT blocks would break. */
7283 return bpaddr;
7284 }
76897487
KB
7285 else
7286 {
7287 CORE_ADDR adjusted_bpaddr;
7288
7289 /* Some targets have architectural constraints on the placement
7290 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7291 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7292
7293 /* An adjusted breakpoint address can significantly alter
7294 a user's expectations. Print a warning if an adjustment
7295 is required. */
7296 if (adjusted_bpaddr != bpaddr)
7297 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7298
7299 return adjusted_bpaddr;
7300 }
7301}
7302
5625a286 7303bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7304{
5625a286 7305 bp_location *loc = this;
7cc221ef 7306
348d480f
PA
7307 gdb_assert (ops != NULL);
7308
28010a5d
PA
7309 loc->ops = ops;
7310 loc->owner = owner;
b775012e 7311 loc->cond_bytecode = NULL;
0d381245
VP
7312 loc->shlib_disabled = 0;
7313 loc->enabled = 1;
e049a4b5 7314
28010a5d 7315 switch (owner->type)
e049a4b5
DJ
7316 {
7317 case bp_breakpoint:
7c16b83e 7318 case bp_single_step:
e049a4b5
DJ
7319 case bp_until:
7320 case bp_finish:
7321 case bp_longjmp:
7322 case bp_longjmp_resume:
e2e4d78b 7323 case bp_longjmp_call_dummy:
186c406b
TT
7324 case bp_exception:
7325 case bp_exception_resume:
e049a4b5 7326 case bp_step_resume:
2c03e5be 7327 case bp_hp_step_resume:
e049a4b5
DJ
7328 case bp_watchpoint_scope:
7329 case bp_call_dummy:
aa7d318d 7330 case bp_std_terminate:
e049a4b5
DJ
7331 case bp_shlib_event:
7332 case bp_thread_event:
7333 case bp_overlay_event:
4efc6507 7334 case bp_jit_event:
0fd8e87f 7335 case bp_longjmp_master:
aa7d318d 7336 case bp_std_terminate_master:
186c406b 7337 case bp_exception_master:
0e30163f
JK
7338 case bp_gnu_ifunc_resolver:
7339 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7340 case bp_dprintf:
e049a4b5 7341 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7342 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7343 break;
7344 case bp_hardware_breakpoint:
7345 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7346 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7347 break;
7348 case bp_hardware_watchpoint:
7349 case bp_read_watchpoint:
7350 case bp_access_watchpoint:
7351 loc->loc_type = bp_loc_hardware_watchpoint;
7352 break;
7353 case bp_watchpoint:
ce78b96d 7354 case bp_catchpoint:
15c3d785
PA
7355 case bp_tracepoint:
7356 case bp_fast_tracepoint:
0fb4aa4b 7357 case bp_static_tracepoint:
e049a4b5
DJ
7358 loc->loc_type = bp_loc_other;
7359 break;
7360 default:
e2e0b3e5 7361 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7362 }
7363
f431efe5 7364 loc->refc = 1;
28010a5d
PA
7365}
7366
7367/* Allocate a struct bp_location. */
7368
7369static struct bp_location *
7370allocate_bp_location (struct breakpoint *bpt)
7371{
348d480f
PA
7372 return bpt->ops->allocate_location (bpt);
7373}
7cc221ef 7374
f431efe5
PA
7375static void
7376free_bp_location (struct bp_location *loc)
fe3f5fa8 7377{
348d480f 7378 loc->ops->dtor (loc);
4d01a485 7379 delete loc;
fe3f5fa8
VP
7380}
7381
f431efe5
PA
7382/* Increment reference count. */
7383
7384static void
7385incref_bp_location (struct bp_location *bl)
7386{
7387 ++bl->refc;
7388}
7389
7390/* Decrement reference count. If the reference count reaches 0,
7391 destroy the bp_location. Sets *BLP to NULL. */
7392
7393static void
7394decref_bp_location (struct bp_location **blp)
7395{
0807b50c
PA
7396 gdb_assert ((*blp)->refc > 0);
7397
f431efe5
PA
7398 if (--(*blp)->refc == 0)
7399 free_bp_location (*blp);
7400 *blp = NULL;
7401}
7402
346774a9 7403/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7404
b270e6f9
TT
7405static breakpoint *
7406add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7407{
346774a9 7408 struct breakpoint *b1;
b270e6f9 7409 struct breakpoint *result = b.get ();
c906108c 7410
346774a9
PA
7411 /* Add this breakpoint to the end of the chain so that a list of
7412 breakpoints will come out in order of increasing numbers. */
7413
7414 b1 = breakpoint_chain;
7415 if (b1 == 0)
b270e6f9 7416 breakpoint_chain = b.release ();
346774a9
PA
7417 else
7418 {
7419 while (b1->next)
7420 b1 = b1->next;
b270e6f9 7421 b1->next = b.release ();
346774a9 7422 }
b270e6f9
TT
7423
7424 return result;
346774a9
PA
7425}
7426
7427/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7428
7429static void
7430init_raw_breakpoint_without_location (struct breakpoint *b,
7431 struct gdbarch *gdbarch,
28010a5d 7432 enum bptype bptype,
c0a91b2b 7433 const struct breakpoint_ops *ops)
346774a9 7434{
348d480f
PA
7435 gdb_assert (ops != NULL);
7436
28010a5d 7437 b->ops = ops;
4d28f7a8 7438 b->type = bptype;
a6d9a66e 7439 b->gdbarch = gdbarch;
c906108c
SS
7440 b->language = current_language->la_language;
7441 b->input_radix = input_radix;
d0fb5eae 7442 b->related_breakpoint = b;
346774a9
PA
7443}
7444
7445/* Helper to set_raw_breakpoint below. Creates a breakpoint
7446 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7447
7448static struct breakpoint *
7449set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7450 enum bptype bptype,
c0a91b2b 7451 const struct breakpoint_ops *ops)
346774a9 7452{
3b0871f4 7453 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7454
3b0871f4 7455 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7456 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7457}
7458
0e30163f
JK
7459/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7460 resolutions should be made as the user specified the location explicitly
7461 enough. */
7462
0d381245 7463static void
0e30163f 7464set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7465{
2bdf28a0
JK
7466 gdb_assert (loc->owner != NULL);
7467
0d381245 7468 if (loc->owner->type == bp_breakpoint
1042e4c0 7469 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7470 || is_tracepoint (loc->owner))
0d381245 7471 {
0e30163f 7472 int is_gnu_ifunc;
2c02bd72 7473 const char *function_name;
6a3a010b 7474 CORE_ADDR func_addr;
0e30163f 7475
2c02bd72 7476 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7477 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7478
7479 if (is_gnu_ifunc && !explicit_loc)
7480 {
7481 struct breakpoint *b = loc->owner;
7482
7483 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7484 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7485 &loc->requested_address))
7486 {
7487 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7488 loc->address = adjust_breakpoint_address (loc->gdbarch,
7489 loc->requested_address,
7490 b->type);
7491 }
7492 else if (b->type == bp_breakpoint && b->loc == loc
7493 && loc->next == NULL && b->related_breakpoint == b)
7494 {
7495 /* Create only the whole new breakpoint of this type but do not
7496 mess more complicated breakpoints with multiple locations. */
7497 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7498 /* Remember the resolver's address for use by the return
7499 breakpoint. */
7500 loc->related_address = func_addr;
0e30163f
JK
7501 }
7502 }
7503
2c02bd72
DE
7504 if (function_name)
7505 loc->function_name = xstrdup (function_name);
0d381245
VP
7506 }
7507}
7508
a6d9a66e 7509/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7510struct gdbarch *
a6d9a66e
UW
7511get_sal_arch (struct symtab_and_line sal)
7512{
7513 if (sal.section)
7514 return get_objfile_arch (sal.section->objfile);
7515 if (sal.symtab)
eb822aa6 7516 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7517
7518 return NULL;
7519}
7520
346774a9
PA
7521/* Low level routine for partially initializing a breakpoint of type
7522 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7523 file name, and line number are provided by SAL.
0d381245
VP
7524
7525 It is expected that the caller will complete the initialization of
7526 the newly created breakpoint struct as well as output any status
c56053d2 7527 information regarding the creation of a new breakpoint. */
0d381245 7528
346774a9
PA
7529static void
7530init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7531 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7532 const struct breakpoint_ops *ops)
0d381245 7533{
28010a5d 7534 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7535
3742cc8b 7536 add_location_to_breakpoint (b, &sal);
0d381245 7537
6c95b8df
PA
7538 if (bptype != bp_catchpoint)
7539 gdb_assert (sal.pspace != NULL);
7540
f8eba3c6
TT
7541 /* Store the program space that was used to set the breakpoint,
7542 except for ordinary breakpoints, which are independent of the
7543 program space. */
7544 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7545 b->pspace = sal.pspace;
346774a9 7546}
c906108c 7547
346774a9
PA
7548/* set_raw_breakpoint is a low level routine for allocating and
7549 partially initializing a breakpoint of type BPTYPE. The newly
7550 created breakpoint's address, section, source file name, and line
7551 number are provided by SAL. The newly created and partially
7552 initialized breakpoint is added to the breakpoint chain and
7553 is also returned as the value of this function.
7554
7555 It is expected that the caller will complete the initialization of
7556 the newly created breakpoint struct as well as output any status
7557 information regarding the creation of a new breakpoint. In
7558 particular, set_raw_breakpoint does NOT set the breakpoint
7559 number! Care should be taken to not allow an error to occur
7560 prior to completing the initialization of the breakpoint. If this
7561 should happen, a bogus breakpoint will be left on the chain. */
7562
7563struct breakpoint *
7564set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7565 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7566 const struct breakpoint_ops *ops)
346774a9 7567{
3b0871f4 7568 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7569
3b0871f4 7570 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7571 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7572}
7573
53a5351d 7574/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7575 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7576 initiated the operation. */
c906108c
SS
7577
7578void
186c406b 7579set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7580{
35df4500 7581 struct breakpoint *b, *b_tmp;
5d5658a1 7582 int thread = tp->global_num;
0fd8e87f
UW
7583
7584 /* To avoid having to rescan all objfile symbols at every step,
7585 we maintain a list of continually-inserted but always disabled
7586 longjmp "master" breakpoints. Here, we simply create momentary
7587 clones of those and enable them for the requested thread. */
35df4500 7588 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7589 if (b->pspace == current_program_space
186c406b
TT
7590 && (b->type == bp_longjmp_master
7591 || b->type == bp_exception_master))
0fd8e87f 7592 {
06edf0c0
PA
7593 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7594 struct breakpoint *clone;
cc59ec59 7595
e2e4d78b
JK
7596 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7597 after their removal. */
06edf0c0 7598 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7599 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7600 clone->thread = thread;
7601 }
186c406b
TT
7602
7603 tp->initiating_frame = frame;
c906108c
SS
7604}
7605
611c83ae 7606/* Delete all longjmp breakpoints from THREAD. */
c906108c 7607void
611c83ae 7608delete_longjmp_breakpoint (int thread)
c906108c 7609{
35df4500 7610 struct breakpoint *b, *b_tmp;
c906108c 7611
35df4500 7612 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7613 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7614 {
7615 if (b->thread == thread)
7616 delete_breakpoint (b);
7617 }
c906108c
SS
7618}
7619
f59f708a
PA
7620void
7621delete_longjmp_breakpoint_at_next_stop (int thread)
7622{
7623 struct breakpoint *b, *b_tmp;
7624
7625 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7626 if (b->type == bp_longjmp || b->type == bp_exception)
7627 {
7628 if (b->thread == thread)
7629 b->disposition = disp_del_at_next_stop;
7630 }
7631}
7632
e2e4d78b
JK
7633/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7634 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7635 pointer to any of them. Return NULL if this system cannot place longjmp
7636 breakpoints. */
7637
7638struct breakpoint *
7639set_longjmp_breakpoint_for_call_dummy (void)
7640{
7641 struct breakpoint *b, *retval = NULL;
7642
7643 ALL_BREAKPOINTS (b)
7644 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7645 {
7646 struct breakpoint *new_b;
7647
7648 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7649 &momentary_breakpoint_ops,
7650 1);
5d5658a1 7651 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7652
7653 /* Link NEW_B into the chain of RETVAL breakpoints. */
7654
7655 gdb_assert (new_b->related_breakpoint == new_b);
7656 if (retval == NULL)
7657 retval = new_b;
7658 new_b->related_breakpoint = retval;
7659 while (retval->related_breakpoint != new_b->related_breakpoint)
7660 retval = retval->related_breakpoint;
7661 retval->related_breakpoint = new_b;
7662 }
7663
7664 return retval;
7665}
7666
7667/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7668 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7669 stack.
7670
7671 You should call this function only at places where it is safe to currently
7672 unwind the whole stack. Failed stack unwind would discard live dummy
7673 frames. */
7674
7675void
b67a2c6f 7676check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7677{
7678 struct breakpoint *b, *b_tmp;
7679
7680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7681 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7682 {
7683 struct breakpoint *dummy_b = b->related_breakpoint;
7684
7685 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7686 dummy_b = dummy_b->related_breakpoint;
7687 if (dummy_b->type != bp_call_dummy
7688 || frame_find_by_id (dummy_b->frame_id) != NULL)
7689 continue;
7690
b67a2c6f 7691 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7692
7693 while (b->related_breakpoint != b)
7694 {
7695 if (b_tmp == b->related_breakpoint)
7696 b_tmp = b->related_breakpoint->next;
7697 delete_breakpoint (b->related_breakpoint);
7698 }
7699 delete_breakpoint (b);
7700 }
7701}
7702
1900040c
MS
7703void
7704enable_overlay_breakpoints (void)
7705{
52f0bd74 7706 struct breakpoint *b;
1900040c
MS
7707
7708 ALL_BREAKPOINTS (b)
7709 if (b->type == bp_overlay_event)
7710 {
7711 b->enable_state = bp_enabled;
44702360 7712 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7713 overlay_events_enabled = 1;
1900040c
MS
7714 }
7715}
7716
7717void
7718disable_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_disabled;
44702360 7726 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7727 overlay_events_enabled = 0;
1900040c
MS
7728 }
7729}
7730
aa7d318d
TT
7731/* Set an active std::terminate breakpoint for each std::terminate
7732 master breakpoint. */
7733void
7734set_std_terminate_breakpoint (void)
7735{
35df4500 7736 struct breakpoint *b, *b_tmp;
aa7d318d 7737
35df4500 7738 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7739 if (b->pspace == current_program_space
7740 && b->type == bp_std_terminate_master)
7741 {
06edf0c0 7742 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7743 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7744 }
7745}
7746
7747/* Delete all the std::terminate breakpoints. */
7748void
7749delete_std_terminate_breakpoint (void)
7750{
35df4500 7751 struct breakpoint *b, *b_tmp;
aa7d318d 7752
35df4500 7753 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7754 if (b->type == bp_std_terminate)
7755 delete_breakpoint (b);
7756}
7757
c4093a6a 7758struct breakpoint *
a6d9a66e 7759create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7760{
7761 struct breakpoint *b;
c4093a6a 7762
06edf0c0
PA
7763 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7764 &internal_breakpoint_ops);
7765
b5de0fa7 7766 b->enable_state = bp_enabled;
f00aae0f 7767 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7768 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7769
44702360 7770 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7771
c4093a6a
JM
7772 return b;
7773}
7774
0101ce28
JJ
7775struct lang_and_radix
7776 {
7777 enum language lang;
7778 int radix;
7779 };
7780
4efc6507
DE
7781/* Create a breakpoint for JIT code registration and unregistration. */
7782
7783struct breakpoint *
7784create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7785{
2a7f3dff
PA
7786 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7787 &internal_breakpoint_ops);
4efc6507 7788}
0101ce28 7789
03673fc7
PP
7790/* Remove JIT code registration and unregistration breakpoint(s). */
7791
7792void
7793remove_jit_event_breakpoints (void)
7794{
7795 struct breakpoint *b, *b_tmp;
7796
7797 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7798 if (b->type == bp_jit_event
7799 && b->loc->pspace == current_program_space)
7800 delete_breakpoint (b);
7801}
7802
cae688ec
JJ
7803void
7804remove_solib_event_breakpoints (void)
7805{
35df4500 7806 struct breakpoint *b, *b_tmp;
cae688ec 7807
35df4500 7808 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7809 if (b->type == bp_shlib_event
7810 && b->loc->pspace == current_program_space)
cae688ec
JJ
7811 delete_breakpoint (b);
7812}
7813
f37f681c
PA
7814/* See breakpoint.h. */
7815
7816void
7817remove_solib_event_breakpoints_at_next_stop (void)
7818{
7819 struct breakpoint *b, *b_tmp;
7820
7821 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7822 if (b->type == bp_shlib_event
7823 && b->loc->pspace == current_program_space)
7824 b->disposition = disp_del_at_next_stop;
7825}
7826
04086b45
PA
7827/* Helper for create_solib_event_breakpoint /
7828 create_and_insert_solib_event_breakpoint. Allows specifying which
7829 INSERT_MODE to pass through to update_global_location_list. */
7830
7831static struct breakpoint *
7832create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7833 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7834{
7835 struct breakpoint *b;
7836
06edf0c0
PA
7837 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7838 &internal_breakpoint_ops);
04086b45 7839 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7840 return b;
7841}
7842
04086b45
PA
7843struct breakpoint *
7844create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7845{
7846 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7847}
7848
f37f681c
PA
7849/* See breakpoint.h. */
7850
7851struct breakpoint *
7852create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7853{
7854 struct breakpoint *b;
7855
04086b45
PA
7856 /* Explicitly tell update_global_location_list to insert
7857 locations. */
7858 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7859 if (!b->loc->inserted)
7860 {
7861 delete_breakpoint (b);
7862 return NULL;
7863 }
7864 return b;
7865}
7866
cae688ec
JJ
7867/* Disable any breakpoints that are on code in shared libraries. Only
7868 apply to enabled breakpoints, disabled ones can just stay disabled. */
7869
7870void
cb851954 7871disable_breakpoints_in_shlibs (void)
cae688ec 7872{
876fa593 7873 struct bp_location *loc, **locp_tmp;
cae688ec 7874
876fa593 7875 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7876 {
2bdf28a0 7877 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7878 struct breakpoint *b = loc->owner;
2bdf28a0 7879
4a64f543
MS
7880 /* We apply the check to all breakpoints, including disabled for
7881 those with loc->duplicate set. This is so that when breakpoint
7882 becomes enabled, or the duplicate is removed, gdb will try to
7883 insert all breakpoints. If we don't set shlib_disabled here,
7884 we'll try to insert those breakpoints and fail. */
1042e4c0 7885 if (((b->type == bp_breakpoint)
508ccb1f 7886 || (b->type == bp_jit_event)
1042e4c0 7887 || (b->type == bp_hardware_breakpoint)
d77f58be 7888 || (is_tracepoint (b)))
6c95b8df 7889 && loc->pspace == current_program_space
0d381245 7890 && !loc->shlib_disabled
6c95b8df 7891 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7892 )
0d381245
VP
7893 {
7894 loc->shlib_disabled = 1;
7895 }
cae688ec
JJ
7896 }
7897}
7898
63644780
NB
7899/* Disable any breakpoints and tracepoints that are in SOLIB upon
7900 notification of unloaded_shlib. Only apply to enabled breakpoints,
7901 disabled ones can just stay disabled. */
84acb35a 7902
75149521 7903static void
84acb35a
JJ
7904disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7905{
876fa593 7906 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7907 int disabled_shlib_breaks = 0;
7908
876fa593 7909 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7910 {
2bdf28a0 7911 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7912 struct breakpoint *b = loc->owner;
cc59ec59 7913
1e4d1764 7914 if (solib->pspace == loc->pspace
e2dd7057 7915 && !loc->shlib_disabled
1e4d1764
YQ
7916 && (((b->type == bp_breakpoint
7917 || b->type == bp_jit_event
7918 || b->type == bp_hardware_breakpoint)
7919 && (loc->loc_type == bp_loc_hardware_breakpoint
7920 || loc->loc_type == bp_loc_software_breakpoint))
7921 || is_tracepoint (b))
e2dd7057 7922 && solib_contains_address_p (solib, loc->address))
84acb35a 7923 {
e2dd7057
PP
7924 loc->shlib_disabled = 1;
7925 /* At this point, we cannot rely on remove_breakpoint
7926 succeeding so we must mark the breakpoint as not inserted
7927 to prevent future errors occurring in remove_breakpoints. */
7928 loc->inserted = 0;
8d3788bd
VP
7929
7930 /* This may cause duplicate notifications for the same breakpoint. */
7931 observer_notify_breakpoint_modified (b);
7932
e2dd7057
PP
7933 if (!disabled_shlib_breaks)
7934 {
7935 target_terminal_ours_for_output ();
3e43a32a
MS
7936 warning (_("Temporarily disabling breakpoints "
7937 "for unloaded shared library \"%s\""),
e2dd7057 7938 solib->so_name);
84acb35a 7939 }
e2dd7057 7940 disabled_shlib_breaks = 1;
84acb35a
JJ
7941 }
7942 }
84acb35a
JJ
7943}
7944
63644780
NB
7945/* Disable any breakpoints and tracepoints in OBJFILE upon
7946 notification of free_objfile. Only apply to enabled breakpoints,
7947 disabled ones can just stay disabled. */
7948
7949static void
7950disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7951{
7952 struct breakpoint *b;
7953
7954 if (objfile == NULL)
7955 return;
7956
d03de421
PA
7957 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7958 managed by the user with add-symbol-file/remove-symbol-file.
7959 Similarly to how breakpoints in shared libraries are handled in
7960 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7961 shlib_disabled so they end up uninserted on the next global
7962 location list update. Shared libraries not loaded by the user
7963 aren't handled here -- they're already handled in
7964 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7965 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7966 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7967 main objfile). */
7968 if ((objfile->flags & OBJF_SHARED) == 0
7969 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7970 return;
7971
7972 ALL_BREAKPOINTS (b)
7973 {
7974 struct bp_location *loc;
7975 int bp_modified = 0;
7976
7977 if (!is_breakpoint (b) && !is_tracepoint (b))
7978 continue;
7979
7980 for (loc = b->loc; loc != NULL; loc = loc->next)
7981 {
7982 CORE_ADDR loc_addr = loc->address;
7983
7984 if (loc->loc_type != bp_loc_hardware_breakpoint
7985 && loc->loc_type != bp_loc_software_breakpoint)
7986 continue;
7987
7988 if (loc->shlib_disabled != 0)
7989 continue;
7990
7991 if (objfile->pspace != loc->pspace)
7992 continue;
7993
7994 if (loc->loc_type != bp_loc_hardware_breakpoint
7995 && loc->loc_type != bp_loc_software_breakpoint)
7996 continue;
7997
7998 if (is_addr_in_objfile (loc_addr, objfile))
7999 {
8000 loc->shlib_disabled = 1;
08351840
PA
8001 /* At this point, we don't know whether the object was
8002 unmapped from the inferior or not, so leave the
8003 inserted flag alone. We'll handle failure to
8004 uninsert quietly, in case the object was indeed
8005 unmapped. */
63644780
NB
8006
8007 mark_breakpoint_location_modified (loc);
8008
8009 bp_modified = 1;
8010 }
8011 }
8012
8013 if (bp_modified)
8014 observer_notify_breakpoint_modified (b);
8015 }
8016}
8017
ce78b96d
JB
8018/* FORK & VFORK catchpoints. */
8019
e29a4733 8020/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
8021 catchpoint. A breakpoint is really of this type iff its ops pointer points
8022 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 8023
c1fc2657 8024struct fork_catchpoint : public breakpoint
e29a4733 8025{
e29a4733
PA
8026 /* Process id of a child process whose forking triggered this
8027 catchpoint. This field is only valid immediately after this
8028 catchpoint has triggered. */
8029 ptid_t forked_inferior_pid;
8030};
8031
4a64f543
MS
8032/* Implement the "insert" breakpoint_ops method for fork
8033 catchpoints. */
ce78b96d 8034
77b06cd7
TJB
8035static int
8036insert_catch_fork (struct bp_location *bl)
ce78b96d 8037{
dfd4cc63 8038 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8039}
8040
4a64f543
MS
8041/* Implement the "remove" breakpoint_ops method for fork
8042 catchpoints. */
ce78b96d
JB
8043
8044static int
73971819 8045remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8046{
dfd4cc63 8047 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8048}
8049
8050/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8051 catchpoints. */
8052
8053static int
f1310107 8054breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8055 struct address_space *aspace, CORE_ADDR bp_addr,
8056 const struct target_waitstatus *ws)
ce78b96d 8057{
e29a4733
PA
8058 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8059
f90263c1
TT
8060 if (ws->kind != TARGET_WAITKIND_FORKED)
8061 return 0;
8062
8063 c->forked_inferior_pid = ws->value.related_pid;
8064 return 1;
ce78b96d
JB
8065}
8066
4a64f543
MS
8067/* Implement the "print_it" breakpoint_ops method for fork
8068 catchpoints. */
ce78b96d
JB
8069
8070static enum print_stop_action
348d480f 8071print_it_catch_fork (bpstat bs)
ce78b96d 8072{
36dfb11c 8073 struct ui_out *uiout = current_uiout;
348d480f
PA
8074 struct breakpoint *b = bs->breakpoint_at;
8075 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8076
ce78b96d 8077 annotate_catchpoint (b->number);
f303dbd6 8078 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8079 if (b->disposition == disp_del)
112e8700 8080 uiout->text ("Temporary catchpoint ");
36dfb11c 8081 else
112e8700
SM
8082 uiout->text ("Catchpoint ");
8083 if (uiout->is_mi_like_p ())
36dfb11c 8084 {
112e8700
SM
8085 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8086 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8087 }
112e8700
SM
8088 uiout->field_int ("bkptno", b->number);
8089 uiout->text (" (forked process ");
8090 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8091 uiout->text ("), ");
ce78b96d
JB
8092 return PRINT_SRC_AND_LOC;
8093}
8094
4a64f543
MS
8095/* Implement the "print_one" breakpoint_ops method for fork
8096 catchpoints. */
ce78b96d
JB
8097
8098static void
a6d9a66e 8099print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8100{
e29a4733 8101 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8102 struct value_print_options opts;
79a45e25 8103 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8104
8105 get_user_print_options (&opts);
8106
4a64f543
MS
8107 /* Field 4, the address, is omitted (which makes the columns not
8108 line up too nicely with the headers, but the effect is relatively
8109 readable). */
79a45b7d 8110 if (opts.addressprint)
112e8700 8111 uiout->field_skip ("addr");
ce78b96d 8112 annotate_field (5);
112e8700 8113 uiout->text ("fork");
e29a4733 8114 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8115 {
112e8700
SM
8116 uiout->text (", process ");
8117 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8118 uiout->spaces (1);
ce78b96d 8119 }
8ac3646f 8120
112e8700
SM
8121 if (uiout->is_mi_like_p ())
8122 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8123}
8124
8125/* Implement the "print_mention" breakpoint_ops method for fork
8126 catchpoints. */
8127
8128static void
8129print_mention_catch_fork (struct breakpoint *b)
8130{
8131 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8132}
8133
6149aea9
PA
8134/* Implement the "print_recreate" breakpoint_ops method for fork
8135 catchpoints. */
8136
8137static void
8138print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8139{
8140 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8141 print_recreate_thread (b, fp);
6149aea9
PA
8142}
8143
ce78b96d
JB
8144/* The breakpoint_ops structure to be used in fork catchpoints. */
8145
2060206e 8146static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8147
4a64f543
MS
8148/* Implement the "insert" breakpoint_ops method for vfork
8149 catchpoints. */
ce78b96d 8150
77b06cd7
TJB
8151static int
8152insert_catch_vfork (struct bp_location *bl)
ce78b96d 8153{
dfd4cc63 8154 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8155}
8156
4a64f543
MS
8157/* Implement the "remove" breakpoint_ops method for vfork
8158 catchpoints. */
ce78b96d
JB
8159
8160static int
73971819 8161remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8162{
dfd4cc63 8163 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8164}
8165
8166/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8167 catchpoints. */
8168
8169static int
f1310107 8170breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8171 struct address_space *aspace, CORE_ADDR bp_addr,
8172 const struct target_waitstatus *ws)
ce78b96d 8173{
e29a4733
PA
8174 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8175
f90263c1
TT
8176 if (ws->kind != TARGET_WAITKIND_VFORKED)
8177 return 0;
8178
8179 c->forked_inferior_pid = ws->value.related_pid;
8180 return 1;
ce78b96d
JB
8181}
8182
4a64f543
MS
8183/* Implement the "print_it" breakpoint_ops method for vfork
8184 catchpoints. */
ce78b96d
JB
8185
8186static enum print_stop_action
348d480f 8187print_it_catch_vfork (bpstat bs)
ce78b96d 8188{
36dfb11c 8189 struct ui_out *uiout = current_uiout;
348d480f 8190 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8191 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8192
ce78b96d 8193 annotate_catchpoint (b->number);
f303dbd6 8194 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8195 if (b->disposition == disp_del)
112e8700 8196 uiout->text ("Temporary catchpoint ");
36dfb11c 8197 else
112e8700
SM
8198 uiout->text ("Catchpoint ");
8199 if (uiout->is_mi_like_p ())
36dfb11c 8200 {
112e8700
SM
8201 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8202 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8203 }
112e8700
SM
8204 uiout->field_int ("bkptno", b->number);
8205 uiout->text (" (vforked process ");
8206 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8207 uiout->text ("), ");
ce78b96d
JB
8208 return PRINT_SRC_AND_LOC;
8209}
8210
4a64f543
MS
8211/* Implement the "print_one" breakpoint_ops method for vfork
8212 catchpoints. */
ce78b96d
JB
8213
8214static void
a6d9a66e 8215print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8216{
e29a4733 8217 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8218 struct value_print_options opts;
79a45e25 8219 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8220
8221 get_user_print_options (&opts);
4a64f543
MS
8222 /* Field 4, the address, is omitted (which makes the columns not
8223 line up too nicely with the headers, but the effect is relatively
8224 readable). */
79a45b7d 8225 if (opts.addressprint)
112e8700 8226 uiout->field_skip ("addr");
ce78b96d 8227 annotate_field (5);
112e8700 8228 uiout->text ("vfork");
e29a4733 8229 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8230 {
112e8700
SM
8231 uiout->text (", process ");
8232 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8233 uiout->spaces (1);
ce78b96d 8234 }
8ac3646f 8235
112e8700
SM
8236 if (uiout->is_mi_like_p ())
8237 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8238}
8239
8240/* Implement the "print_mention" breakpoint_ops method for vfork
8241 catchpoints. */
8242
8243static void
8244print_mention_catch_vfork (struct breakpoint *b)
8245{
8246 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8247}
8248
6149aea9
PA
8249/* Implement the "print_recreate" breakpoint_ops method for vfork
8250 catchpoints. */
8251
8252static void
8253print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8254{
8255 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8256 print_recreate_thread (b, fp);
6149aea9
PA
8257}
8258
ce78b96d
JB
8259/* The breakpoint_ops structure to be used in vfork catchpoints. */
8260
2060206e 8261static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8262
edcc5120 8263/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8264 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8265 CATCH_SOLIB_BREAKPOINT_OPS. */
8266
c1fc2657 8267struct solib_catchpoint : public breakpoint
edcc5120 8268{
c1fc2657 8269 ~solib_catchpoint () override;
edcc5120
TT
8270
8271 /* True for "catch load", false for "catch unload". */
8272 unsigned char is_load;
8273
8274 /* Regular expression to match, if any. COMPILED is only valid when
8275 REGEX is non-NULL. */
8276 char *regex;
2d7cc5c7 8277 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8278};
8279
c1fc2657 8280solib_catchpoint::~solib_catchpoint ()
edcc5120 8281{
c1fc2657 8282 xfree (this->regex);
edcc5120
TT
8283}
8284
8285static int
8286insert_catch_solib (struct bp_location *ignore)
8287{
8288 return 0;
8289}
8290
8291static int
73971819 8292remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8293{
8294 return 0;
8295}
8296
8297static int
8298breakpoint_hit_catch_solib (const struct bp_location *bl,
8299 struct address_space *aspace,
8300 CORE_ADDR bp_addr,
8301 const struct target_waitstatus *ws)
8302{
8303 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8304 struct breakpoint *other;
8305
8306 if (ws->kind == TARGET_WAITKIND_LOADED)
8307 return 1;
8308
8309 ALL_BREAKPOINTS (other)
8310 {
8311 struct bp_location *other_bl;
8312
8313 if (other == bl->owner)
8314 continue;
8315
8316 if (other->type != bp_shlib_event)
8317 continue;
8318
c1fc2657 8319 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8320 continue;
8321
8322 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8323 {
8324 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8325 return 1;
8326 }
8327 }
8328
8329 return 0;
8330}
8331
8332static void
8333check_status_catch_solib (struct bpstats *bs)
8334{
8335 struct solib_catchpoint *self
8336 = (struct solib_catchpoint *) bs->breakpoint_at;
8337 int ix;
8338
8339 if (self->is_load)
8340 {
8341 struct so_list *iter;
8342
8343 for (ix = 0;
8344 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8345 ix, iter);
8346 ++ix)
8347 {
8348 if (!self->regex
2d7cc5c7 8349 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8350 return;
8351 }
8352 }
8353 else
8354 {
8355 char *iter;
8356
8357 for (ix = 0;
8358 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8359 ix, iter);
8360 ++ix)
8361 {
8362 if (!self->regex
2d7cc5c7 8363 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8364 return;
8365 }
8366 }
8367
8368 bs->stop = 0;
8369 bs->print_it = print_it_noop;
8370}
8371
8372static enum print_stop_action
8373print_it_catch_solib (bpstat bs)
8374{
8375 struct breakpoint *b = bs->breakpoint_at;
8376 struct ui_out *uiout = current_uiout;
8377
8378 annotate_catchpoint (b->number);
f303dbd6 8379 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8380 if (b->disposition == disp_del)
112e8700 8381 uiout->text ("Temporary catchpoint ");
edcc5120 8382 else
112e8700
SM
8383 uiout->text ("Catchpoint ");
8384 uiout->field_int ("bkptno", b->number);
8385 uiout->text ("\n");
8386 if (uiout->is_mi_like_p ())
8387 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8388 print_solib_event (1);
8389 return PRINT_SRC_AND_LOC;
8390}
8391
8392static void
8393print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8394{
8395 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8396 struct value_print_options opts;
8397 struct ui_out *uiout = current_uiout;
8398 char *msg;
8399
8400 get_user_print_options (&opts);
8401 /* Field 4, the address, is omitted (which makes the columns not
8402 line up too nicely with the headers, but the effect is relatively
8403 readable). */
8404 if (opts.addressprint)
8405 {
8406 annotate_field (4);
112e8700 8407 uiout->field_skip ("addr");
edcc5120
TT
8408 }
8409
8410 annotate_field (5);
8411 if (self->is_load)
8412 {
8413 if (self->regex)
8414 msg = xstrprintf (_("load of library matching %s"), self->regex);
8415 else
8416 msg = xstrdup (_("load of library"));
8417 }
8418 else
8419 {
8420 if (self->regex)
8421 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8422 else
8423 msg = xstrdup (_("unload of library"));
8424 }
112e8700 8425 uiout->field_string ("what", msg);
edcc5120 8426 xfree (msg);
8ac3646f 8427
112e8700
SM
8428 if (uiout->is_mi_like_p ())
8429 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8430}
8431
8432static void
8433print_mention_catch_solib (struct breakpoint *b)
8434{
8435 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8436
8437 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8438 self->is_load ? "load" : "unload");
8439}
8440
8441static void
8442print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8443{
8444 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8445
8446 fprintf_unfiltered (fp, "%s %s",
8447 b->disposition == disp_del ? "tcatch" : "catch",
8448 self->is_load ? "load" : "unload");
8449 if (self->regex)
8450 fprintf_unfiltered (fp, " %s", self->regex);
8451 fprintf_unfiltered (fp, "\n");
8452}
8453
8454static struct breakpoint_ops catch_solib_breakpoint_ops;
8455
91985142
MG
8456/* Shared helper function (MI and CLI) for creating and installing
8457 a shared object event catchpoint. If IS_LOAD is non-zero then
8458 the events to be caught are load events, otherwise they are
8459 unload events. If IS_TEMP is non-zero the catchpoint is a
8460 temporary one. If ENABLED is non-zero the catchpoint is
8461 created in an enabled state. */
edcc5120 8462
91985142 8463void
a121b7c1 8464add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8465{
edcc5120 8466 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8467
edcc5120
TT
8468 if (!arg)
8469 arg = "";
a121b7c1 8470 arg = skip_spaces_const (arg);
edcc5120 8471
36bd8eaa 8472 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8473
8474 if (*arg != '\0')
8475 {
2d7cc5c7
PA
8476 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8477 _("Invalid regexp")));
edcc5120
TT
8478 c->regex = xstrdup (arg);
8479 }
8480
8481 c->is_load = is_load;
36bd8eaa 8482 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8483 &catch_solib_breakpoint_ops);
8484
c1fc2657 8485 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8486
b270e6f9 8487 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8488}
8489
91985142
MG
8490/* A helper function that does all the work for "catch load" and
8491 "catch unload". */
8492
8493static void
8494catch_load_or_unload (char *arg, int from_tty, int is_load,
8495 struct cmd_list_element *command)
8496{
8497 int tempflag;
8498 const int enabled = 1;
8499
8500 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8501
8502 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8503}
8504
edcc5120
TT
8505static void
8506catch_load_command_1 (char *arg, int from_tty,
8507 struct cmd_list_element *command)
8508{
8509 catch_load_or_unload (arg, from_tty, 1, command);
8510}
8511
8512static void
8513catch_unload_command_1 (char *arg, int from_tty,
8514 struct cmd_list_element *command)
8515{
8516 catch_load_or_unload (arg, from_tty, 0, command);
8517}
8518
346774a9
PA
8519/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8520 is non-zero, then make the breakpoint temporary. If COND_STRING is
8521 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8522 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8523
ab04a2af 8524void
346774a9
PA
8525init_catchpoint (struct breakpoint *b,
8526 struct gdbarch *gdbarch, int tempflag,
63160a43 8527 const char *cond_string,
c0a91b2b 8528 const struct breakpoint_ops *ops)
c906108c 8529{
c5aa993b 8530 struct symtab_and_line sal;
346774a9 8531
fe39c653 8532 init_sal (&sal);
6c95b8df 8533 sal.pspace = current_program_space;
c5aa993b 8534
28010a5d 8535 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8536
1b36a34b 8537 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8538 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8539}
8540
28010a5d 8541void
b270e6f9 8542install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8543{
b270e6f9 8544 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8545 set_breakpoint_number (internal, b);
558a9d82
YQ
8546 if (is_tracepoint (b))
8547 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8548 if (!internal)
8549 mention (b);
c56053d2 8550 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8551
8552 if (update_gll)
44702360 8553 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8554}
8555
9b70b993 8556static void
a6d9a66e 8557create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8558 int tempflag, const char *cond_string,
c0a91b2b 8559 const struct breakpoint_ops *ops)
c906108c 8560{
b270e6f9 8561 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8562
b270e6f9 8563 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8564
8565 c->forked_inferior_pid = null_ptid;
8566
b270e6f9 8567 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8568}
8569
fe798b75
JB
8570/* Exec catchpoints. */
8571
b4d90040 8572/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8573 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8574 CATCH_EXEC_BREAKPOINT_OPS. */
8575
c1fc2657 8576struct exec_catchpoint : public breakpoint
b4d90040 8577{
c1fc2657 8578 ~exec_catchpoint () override;
b4d90040
PA
8579
8580 /* Filename of a program whose exec triggered this catchpoint.
8581 This field is only valid immediately after this catchpoint has
8582 triggered. */
8583 char *exec_pathname;
8584};
8585
c1fc2657 8586/* Exec catchpoint destructor. */
b4d90040 8587
c1fc2657 8588exec_catchpoint::~exec_catchpoint ()
b4d90040 8589{
c1fc2657 8590 xfree (this->exec_pathname);
b4d90040
PA
8591}
8592
77b06cd7
TJB
8593static int
8594insert_catch_exec (struct bp_location *bl)
c906108c 8595{
dfd4cc63 8596 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8597}
c906108c 8598
fe798b75 8599static int
73971819 8600remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8601{
dfd4cc63 8602 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8603}
c906108c 8604
fe798b75 8605static int
f1310107 8606breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8607 struct address_space *aspace, CORE_ADDR bp_addr,
8608 const struct target_waitstatus *ws)
fe798b75 8609{
b4d90040
PA
8610 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8611
f90263c1
TT
8612 if (ws->kind != TARGET_WAITKIND_EXECD)
8613 return 0;
8614
8615 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8616 return 1;
fe798b75 8617}
c906108c 8618
fe798b75 8619static enum print_stop_action
348d480f 8620print_it_catch_exec (bpstat bs)
fe798b75 8621{
36dfb11c 8622 struct ui_out *uiout = current_uiout;
348d480f 8623 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8624 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8625
fe798b75 8626 annotate_catchpoint (b->number);
f303dbd6 8627 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8628 if (b->disposition == disp_del)
112e8700 8629 uiout->text ("Temporary catchpoint ");
36dfb11c 8630 else
112e8700
SM
8631 uiout->text ("Catchpoint ");
8632 if (uiout->is_mi_like_p ())
36dfb11c 8633 {
112e8700
SM
8634 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8635 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8636 }
112e8700
SM
8637 uiout->field_int ("bkptno", b->number);
8638 uiout->text (" (exec'd ");
8639 uiout->field_string ("new-exec", c->exec_pathname);
8640 uiout->text ("), ");
36dfb11c 8641
fe798b75 8642 return PRINT_SRC_AND_LOC;
c906108c
SS
8643}
8644
fe798b75 8645static void
a6d9a66e 8646print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8647{
b4d90040 8648 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8649 struct value_print_options opts;
79a45e25 8650 struct ui_out *uiout = current_uiout;
fe798b75
JB
8651
8652 get_user_print_options (&opts);
8653
8654 /* Field 4, the address, is omitted (which makes the columns
8655 not line up too nicely with the headers, but the effect
8656 is relatively readable). */
8657 if (opts.addressprint)
112e8700 8658 uiout->field_skip ("addr");
fe798b75 8659 annotate_field (5);
112e8700 8660 uiout->text ("exec");
b4d90040 8661 if (c->exec_pathname != NULL)
fe798b75 8662 {
112e8700
SM
8663 uiout->text (", program \"");
8664 uiout->field_string ("what", c->exec_pathname);
8665 uiout->text ("\" ");
fe798b75 8666 }
8ac3646f 8667
112e8700
SM
8668 if (uiout->is_mi_like_p ())
8669 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8670}
8671
8672static void
8673print_mention_catch_exec (struct breakpoint *b)
8674{
8675 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8676}
8677
6149aea9
PA
8678/* Implement the "print_recreate" breakpoint_ops method for exec
8679 catchpoints. */
8680
8681static void
8682print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8683{
8684 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8685 print_recreate_thread (b, fp);
6149aea9
PA
8686}
8687
2060206e 8688static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8689
c906108c 8690static int
fba45db2 8691hw_breakpoint_used_count (void)
c906108c 8692{
c906108c 8693 int i = 0;
f1310107
TJB
8694 struct breakpoint *b;
8695 struct bp_location *bl;
c906108c
SS
8696
8697 ALL_BREAKPOINTS (b)
c5aa993b 8698 {
d6b74ac4 8699 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8700 for (bl = b->loc; bl; bl = bl->next)
8701 {
8702 /* Special types of hardware breakpoints may use more than
8703 one register. */
348d480f 8704 i += b->ops->resources_needed (bl);
f1310107 8705 }
c5aa993b 8706 }
c906108c
SS
8707
8708 return i;
8709}
8710
a1398e0c
PA
8711/* Returns the resources B would use if it were a hardware
8712 watchpoint. */
8713
c906108c 8714static int
a1398e0c 8715hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8716{
c906108c 8717 int i = 0;
e09342b5 8718 struct bp_location *bl;
c906108c 8719
a1398e0c
PA
8720 if (!breakpoint_enabled (b))
8721 return 0;
8722
8723 for (bl = b->loc; bl; bl = bl->next)
8724 {
8725 /* Special types of hardware watchpoints may use more than
8726 one register. */
8727 i += b->ops->resources_needed (bl);
8728 }
8729
8730 return i;
8731}
8732
8733/* Returns the sum the used resources of all hardware watchpoints of
8734 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8735 the sum of the used resources of all hardware watchpoints of other
8736 types _not_ TYPE. */
8737
8738static int
8739hw_watchpoint_used_count_others (struct breakpoint *except,
8740 enum bptype type, int *other_type_used)
8741{
8742 int i = 0;
8743 struct breakpoint *b;
8744
c906108c
SS
8745 *other_type_used = 0;
8746 ALL_BREAKPOINTS (b)
e09342b5 8747 {
a1398e0c
PA
8748 if (b == except)
8749 continue;
e09342b5
TJB
8750 if (!breakpoint_enabled (b))
8751 continue;
8752
a1398e0c
PA
8753 if (b->type == type)
8754 i += hw_watchpoint_use_count (b);
8755 else if (is_hardware_watchpoint (b))
8756 *other_type_used = 1;
e09342b5
TJB
8757 }
8758
c906108c
SS
8759 return i;
8760}
8761
c906108c 8762void
fba45db2 8763disable_watchpoints_before_interactive_call_start (void)
c906108c 8764{
c5aa993b 8765 struct breakpoint *b;
c906108c
SS
8766
8767 ALL_BREAKPOINTS (b)
c5aa993b 8768 {
cc60f2e3 8769 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8770 {
b5de0fa7 8771 b->enable_state = bp_call_disabled;
44702360 8772 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8773 }
8774 }
c906108c
SS
8775}
8776
8777void
fba45db2 8778enable_watchpoints_after_interactive_call_stop (void)
c906108c 8779{
c5aa993b 8780 struct breakpoint *b;
c906108c
SS
8781
8782 ALL_BREAKPOINTS (b)
c5aa993b 8783 {
cc60f2e3 8784 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8785 {
b5de0fa7 8786 b->enable_state = bp_enabled;
44702360 8787 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8788 }
8789 }
c906108c
SS
8790}
8791
8bea4e01
UW
8792void
8793disable_breakpoints_before_startup (void)
8794{
6c95b8df 8795 current_program_space->executing_startup = 1;
44702360 8796 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8797}
8798
8799void
8800enable_breakpoints_after_startup (void)
8801{
6c95b8df 8802 current_program_space->executing_startup = 0;
f8eba3c6 8803 breakpoint_re_set ();
8bea4e01
UW
8804}
8805
7c16b83e
PA
8806/* Create a new single-step breakpoint for thread THREAD, with no
8807 locations. */
c906108c 8808
7c16b83e
PA
8809static struct breakpoint *
8810new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8811{
b270e6f9 8812 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8813
b270e6f9 8814 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8815 &momentary_breakpoint_ops);
8816
8817 b->disposition = disp_donttouch;
8818 b->frame_id = null_frame_id;
8819
8820 b->thread = thread;
8821 gdb_assert (b->thread != 0);
8822
b270e6f9 8823 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8824}
8825
8826/* Set a momentary breakpoint of type TYPE at address specified by
8827 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8828 frame. */
c906108c
SS
8829
8830struct breakpoint *
a6d9a66e
UW
8831set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8832 struct frame_id frame_id, enum bptype type)
c906108c 8833{
52f0bd74 8834 struct breakpoint *b;
edb3359d 8835
193facb3
JK
8836 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8837 tail-called one. */
8838 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8839
06edf0c0 8840 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8841 b->enable_state = bp_enabled;
8842 b->disposition = disp_donttouch;
818dd999 8843 b->frame_id = frame_id;
c906108c 8844
4a64f543
MS
8845 /* If we're debugging a multi-threaded program, then we want
8846 momentary breakpoints to be active in only a single thread of
8847 control. */
39f77062 8848 if (in_thread_list (inferior_ptid))
5d5658a1 8849 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8850
44702360 8851 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8852
c906108c
SS
8853 return b;
8854}
611c83ae 8855
06edf0c0 8856/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8857 The new breakpoint will have type TYPE, use OPS as its
8858 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8859
06edf0c0
PA
8860static struct breakpoint *
8861momentary_breakpoint_from_master (struct breakpoint *orig,
8862 enum bptype type,
a1aa2221
LM
8863 const struct breakpoint_ops *ops,
8864 int loc_enabled)
e58b0e63
PA
8865{
8866 struct breakpoint *copy;
8867
06edf0c0 8868 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8869 copy->loc = allocate_bp_location (copy);
0e30163f 8870 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8871
a6d9a66e 8872 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8873 copy->loc->requested_address = orig->loc->requested_address;
8874 copy->loc->address = orig->loc->address;
8875 copy->loc->section = orig->loc->section;
6c95b8df 8876 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8877 copy->loc->probe = orig->loc->probe;
f8eba3c6 8878 copy->loc->line_number = orig->loc->line_number;
2f202fde 8879 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8880 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8881 copy->frame_id = orig->frame_id;
8882 copy->thread = orig->thread;
6c95b8df 8883 copy->pspace = orig->pspace;
e58b0e63
PA
8884
8885 copy->enable_state = bp_enabled;
8886 copy->disposition = disp_donttouch;
8887 copy->number = internal_breakpoint_number--;
8888
44702360 8889 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8890 return copy;
8891}
8892
06edf0c0
PA
8893/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8894 ORIG is NULL. */
8895
8896struct breakpoint *
8897clone_momentary_breakpoint (struct breakpoint *orig)
8898{
8899 /* If there's nothing to clone, then return nothing. */
8900 if (orig == NULL)
8901 return NULL;
8902
a1aa2221 8903 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8904}
8905
611c83ae 8906struct breakpoint *
a6d9a66e
UW
8907set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8908 enum bptype type)
611c83ae
PA
8909{
8910 struct symtab_and_line sal;
8911
8912 sal = find_pc_line (pc, 0);
8913 sal.pc = pc;
8914 sal.section = find_pc_overlay (pc);
8915 sal.explicit_pc = 1;
8916
a6d9a66e 8917 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8918}
c906108c 8919\f
c5aa993b 8920
c906108c
SS
8921/* Tell the user we have just set a breakpoint B. */
8922
8923static void
fba45db2 8924mention (struct breakpoint *b)
c906108c 8925{
348d480f 8926 b->ops->print_mention (b);
112e8700 8927 if (current_uiout->is_mi_like_p ())
fb40c209 8928 return;
c906108c
SS
8929 printf_filtered ("\n");
8930}
c906108c 8931\f
c5aa993b 8932
1a853c52
PA
8933static int bp_loc_is_permanent (struct bp_location *loc);
8934
0d381245 8935static struct bp_location *
39d61571 8936add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8937 const struct symtab_and_line *sal)
8938{
8939 struct bp_location *loc, **tmp;
3742cc8b
YQ
8940 CORE_ADDR adjusted_address;
8941 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8942
8943 if (loc_gdbarch == NULL)
8944 loc_gdbarch = b->gdbarch;
8945
8946 /* Adjust the breakpoint's address prior to allocating a location.
8947 Once we call allocate_bp_location(), that mostly uninitialized
8948 location will be placed on the location chain. Adjustment of the
8949 breakpoint may cause target_read_memory() to be called and we do
8950 not want its scan of the location chain to find a breakpoint and
8951 location that's only been partially initialized. */
8952 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8953 sal->pc, b->type);
0d381245 8954
d30113d4 8955 /* Sort the locations by their ADDRESS. */
39d61571 8956 loc = allocate_bp_location (b);
d30113d4
JK
8957 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8958 tmp = &((*tmp)->next))
0d381245 8959 ;
d30113d4 8960 loc->next = *tmp;
0d381245 8961 *tmp = loc;
3742cc8b 8962
0d381245 8963 loc->requested_address = sal->pc;
3742cc8b 8964 loc->address = adjusted_address;
6c95b8df 8965 loc->pspace = sal->pspace;
729662a5
TT
8966 loc->probe.probe = sal->probe;
8967 loc->probe.objfile = sal->objfile;
6c95b8df 8968 gdb_assert (loc->pspace != NULL);
0d381245 8969 loc->section = sal->section;
3742cc8b 8970 loc->gdbarch = loc_gdbarch;
f8eba3c6 8971 loc->line_number = sal->line;
2f202fde 8972 loc->symtab = sal->symtab;
f8eba3c6 8973
0e30163f
JK
8974 set_breakpoint_location_function (loc,
8975 sal->explicit_pc || sal->explicit_line);
1a853c52 8976
6ae88661
LM
8977 /* While by definition, permanent breakpoints are already present in the
8978 code, we don't mark the location as inserted. Normally one would expect
8979 that GDB could rely on that breakpoint instruction to stop the program,
8980 thus removing the need to insert its own breakpoint, except that executing
8981 the breakpoint instruction can kill the target instead of reporting a
8982 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8983 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8984 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8985 breakpoint be inserted normally results in QEMU knowing about the GDB
8986 breakpoint, and thus trap before the breakpoint instruction is executed.
8987 (If GDB later needs to continue execution past the permanent breakpoint,
8988 it manually increments the PC, thus avoiding executing the breakpoint
8989 instruction.) */
1a853c52 8990 if (bp_loc_is_permanent (loc))
6ae88661 8991 loc->permanent = 1;
1a853c52 8992
0d381245
VP
8993 return loc;
8994}
514f746b
AR
8995\f
8996
1cf4d951 8997/* See breakpoint.h. */
514f746b 8998
1cf4d951
PA
8999int
9000program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9001{
9002 int len;
9003 CORE_ADDR addr;
1afeeb75 9004 const gdb_byte *bpoint;
514f746b 9005 gdb_byte *target_mem;
939c61fa
JK
9006 struct cleanup *cleanup;
9007 int retval = 0;
514f746b 9008
1cf4d951
PA
9009 addr = address;
9010 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9011
9012 /* Software breakpoints unsupported? */
9013 if (bpoint == NULL)
9014 return 0;
9015
224c3ddb 9016 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9017
9018 /* Enable the automatic memory restoration from breakpoints while
9019 we read the memory. Otherwise we could say about our temporary
9020 breakpoints they are permanent. */
9021 cleanup = make_show_memory_breakpoints_cleanup (0);
9022
9023 if (target_read_memory (address, target_mem, len) == 0
9024 && memcmp (target_mem, bpoint, len) == 0)
9025 retval = 1;
9026
9027 do_cleanups (cleanup);
9028
9029 return retval;
9030}
9031
9032/* Return 1 if LOC is pointing to a permanent breakpoint,
9033 return 0 otherwise. */
9034
9035static int
9036bp_loc_is_permanent (struct bp_location *loc)
9037{
514f746b
AR
9038 gdb_assert (loc != NULL);
9039
244558af
LM
9040 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9041 attempt to read from the addresses the locations of these breakpoint types
9042 point to. program_breakpoint_here_p, below, will attempt to read
9043 memory. */
9044 if (!breakpoint_address_is_meaningful (loc->owner))
9045 return 0;
9046
5ed8105e 9047 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9048 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 9049 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
9050}
9051
e7e0cddf
SS
9052/* Build a command list for the dprintf corresponding to the current
9053 settings of the dprintf style options. */
9054
9055static void
9056update_dprintf_command_list (struct breakpoint *b)
9057{
9058 char *dprintf_args = b->extra_string;
9059 char *printf_line = NULL;
9060
9061 if (!dprintf_args)
9062 return;
9063
9064 dprintf_args = skip_spaces (dprintf_args);
9065
9066 /* Allow a comma, as it may have terminated a location, but don't
9067 insist on it. */
9068 if (*dprintf_args == ',')
9069 ++dprintf_args;
9070 dprintf_args = skip_spaces (dprintf_args);
9071
9072 if (*dprintf_args != '"')
9073 error (_("Bad format string, missing '\"'."));
9074
d3ce09f5 9075 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9076 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9077 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9078 {
9079 if (!dprintf_function)
9080 error (_("No function supplied for dprintf call"));
9081
9082 if (dprintf_channel && strlen (dprintf_channel) > 0)
9083 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9084 dprintf_function,
9085 dprintf_channel,
9086 dprintf_args);
9087 else
9088 printf_line = xstrprintf ("call (void) %s (%s)",
9089 dprintf_function,
9090 dprintf_args);
9091 }
d3ce09f5
SS
9092 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9093 {
9094 if (target_can_run_breakpoint_commands ())
9095 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9096 else
9097 {
9098 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9099 printf_line = xstrprintf ("printf %s", dprintf_args);
9100 }
9101 }
e7e0cddf
SS
9102 else
9103 internal_error (__FILE__, __LINE__,
9104 _("Invalid dprintf style."));
9105
f28045c2 9106 gdb_assert (printf_line != NULL);
9d6e6e84 9107 /* Manufacture a printf sequence. */
f28045c2 9108 {
8d749320 9109 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9110
f28045c2
YQ
9111 printf_cmd_line->control_type = simple_control;
9112 printf_cmd_line->body_count = 0;
9113 printf_cmd_line->body_list = NULL;
9d6e6e84 9114 printf_cmd_line->next = NULL;
f28045c2 9115 printf_cmd_line->line = printf_line;
e7e0cddf 9116
93921405 9117 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9118 }
e7e0cddf
SS
9119}
9120
9121/* Update all dprintf commands, making their command lists reflect
9122 current style settings. */
9123
9124static void
9125update_dprintf_commands (char *args, int from_tty,
9126 struct cmd_list_element *c)
9127{
9128 struct breakpoint *b;
9129
9130 ALL_BREAKPOINTS (b)
9131 {
9132 if (b->type == bp_dprintf)
9133 update_dprintf_command_list (b);
9134 }
9135}
c3f6f71d 9136
f00aae0f
KS
9137/* Create a breakpoint with SAL as location. Use LOCATION
9138 as a description of the location, and COND_STRING
b35a8b2f
DE
9139 as condition expression. If LOCATION is NULL then create an
9140 "address location" from the address in the SAL. */
018d34a4
VP
9141
9142static void
d9b3f62e 9143init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9144 struct symtabs_and_lines sals,
ffc2605c 9145 event_location_up &&location,
e1e01040
PA
9146 gdb::unique_xmalloc_ptr<char> filter,
9147 gdb::unique_xmalloc_ptr<char> cond_string,
9148 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9149 enum bptype type, enum bpdisp disposition,
9150 int thread, int task, int ignore_count,
c0a91b2b 9151 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9152 int enabled, int internal, unsigned flags,
9153 int display_canonical)
018d34a4 9154{
0d381245 9155 int i;
018d34a4
VP
9156
9157 if (type == bp_hardware_breakpoint)
9158 {
fbbd034e
AS
9159 int target_resources_ok;
9160
9161 i = hw_breakpoint_used_count ();
9162 target_resources_ok =
9163 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9164 i + 1, 0);
9165 if (target_resources_ok == 0)
9166 error (_("No hardware breakpoint support in the target."));
9167 else if (target_resources_ok < 0)
9168 error (_("Hardware breakpoints used exceeds limit."));
9169 }
9170
6c95b8df
PA
9171 gdb_assert (sals.nelts > 0);
9172
0d381245
VP
9173 for (i = 0; i < sals.nelts; ++i)
9174 {
9175 struct symtab_and_line sal = sals.sals[i];
9176 struct bp_location *loc;
9177
9178 if (from_tty)
5af949e3
UW
9179 {
9180 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9181 if (!loc_gdbarch)
9182 loc_gdbarch = gdbarch;
9183
9184 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9185 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9186 }
0d381245
VP
9187
9188 if (i == 0)
9189 {
d9b3f62e 9190 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9191 b->thread = thread;
4a306c9a 9192 b->task = task;
855a6e68 9193
e1e01040
PA
9194 b->cond_string = cond_string.release ();
9195 b->extra_string = extra_string.release ();
0d381245 9196 b->ignore_count = ignore_count;
41447f92 9197 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9198 b->disposition = disposition;
6c95b8df 9199
44f238bb
PA
9200 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9201 b->loc->inserted = 1;
9202
0fb4aa4b
PA
9203 if (type == bp_static_tracepoint)
9204 {
d9b3f62e 9205 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9206 struct static_tracepoint_marker marker;
9207
983af33b 9208 if (strace_marker_p (b))
0fb4aa4b
PA
9209 {
9210 /* We already know the marker exists, otherwise, we
9211 wouldn't see a sal for it. */
d28cd78a
TT
9212 const char *p
9213 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9214 const char *endp;
0fb4aa4b 9215 char *marker_str;
0fb4aa4b 9216
f00aae0f 9217 p = skip_spaces_const (p);
0fb4aa4b 9218
f00aae0f 9219 endp = skip_to_space_const (p);
0fb4aa4b
PA
9220
9221 marker_str = savestring (p, endp - p);
d9b3f62e 9222 t->static_trace_marker_id = marker_str;
0fb4aa4b 9223
3e43a32a
MS
9224 printf_filtered (_("Probed static tracepoint "
9225 "marker \"%s\"\n"),
d9b3f62e 9226 t->static_trace_marker_id);
0fb4aa4b
PA
9227 }
9228 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9229 {
d9b3f62e 9230 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9231 release_static_tracepoint_marker (&marker);
9232
3e43a32a
MS
9233 printf_filtered (_("Probed static tracepoint "
9234 "marker \"%s\"\n"),
d9b3f62e 9235 t->static_trace_marker_id);
0fb4aa4b
PA
9236 }
9237 else
3e43a32a
MS
9238 warning (_("Couldn't determine the static "
9239 "tracepoint marker to probe"));
0fb4aa4b
PA
9240 }
9241
0d381245
VP
9242 loc = b->loc;
9243 }
9244 else
018d34a4 9245 {
39d61571 9246 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9247 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9248 loc->inserted = 1;
0d381245
VP
9249 }
9250
9251 if (b->cond_string)
9252 {
bbc13ae3
KS
9253 const char *arg = b->cond_string;
9254
1bb9788d
TT
9255 loc->cond = parse_exp_1 (&arg, loc->address,
9256 block_for_pc (loc->address), 0);
0d381245 9257 if (*arg)
588ae58c 9258 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9259 }
e7e0cddf
SS
9260
9261 /* Dynamic printf requires and uses additional arguments on the
9262 command line, otherwise it's an error. */
9263 if (type == bp_dprintf)
9264 {
9265 if (b->extra_string)
9266 update_dprintf_command_list (b);
9267 else
9268 error (_("Format string required"));
9269 }
9270 else if (b->extra_string)
588ae58c 9271 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9272 }
018d34a4 9273
56435ebe 9274 b->display_canonical = display_canonical;
f00aae0f 9275 if (location != NULL)
d28cd78a 9276 b->location = std::move (location);
018d34a4 9277 else
d28cd78a 9278 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9279 b->filter = filter.release ();
d9b3f62e 9280}
018d34a4 9281
d9b3f62e
PA
9282static void
9283create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9284 struct symtabs_and_lines sals,
ffc2605c 9285 event_location_up &&location,
e1e01040
PA
9286 gdb::unique_xmalloc_ptr<char> filter,
9287 gdb::unique_xmalloc_ptr<char> cond_string,
9288 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9289 enum bptype type, enum bpdisp disposition,
9290 int thread, int task, int ignore_count,
c0a91b2b 9291 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9292 int enabled, int internal, unsigned flags,
9293 int display_canonical)
d9b3f62e 9294{
a5e364af 9295 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9296
a5e364af 9297 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9298 sals, std::move (location),
e1e01040
PA
9299 std::move (filter),
9300 std::move (cond_string),
9301 std::move (extra_string),
d9b3f62e
PA
9302 type, disposition,
9303 thread, task, ignore_count,
9304 ops, from_tty,
44f238bb
PA
9305 enabled, internal, flags,
9306 display_canonical);
d9b3f62e 9307
b270e6f9 9308 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9309}
9310
9311/* Add SALS.nelts breakpoints to the breakpoint table. For each
9312 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9313 value. COND_STRING, if not NULL, specified the condition to be
9314 used for all breakpoints. Essentially the only case where
9315 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9316 function. In that case, it's still not possible to specify
9317 separate conditions for different overloaded functions, so
9318 we take just a single condition string.
9319
c3f6f71d 9320 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9321 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9322 array contents). If the function fails (error() is called), the
9323 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9324 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9325
9326static void
8cdf0e15 9327create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9328 struct linespec_result *canonical,
e1e01040
PA
9329 gdb::unique_xmalloc_ptr<char> cond_string,
9330 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9331 enum bptype type, enum bpdisp disposition,
9332 int thread, int task, int ignore_count,
c0a91b2b 9333 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9334 int enabled, int internal, unsigned flags)
c906108c 9335{
018d34a4 9336 int i;
f8eba3c6 9337 struct linespec_sals *lsal;
cc59ec59 9338
f8eba3c6
TT
9339 if (canonical->pre_expanded)
9340 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9341
9342 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9343 {
f00aae0f 9344 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9345 'break', without arguments. */
ffc2605c 9346 event_location_up location
f00aae0f 9347 = (canonical->location != NULL
8e9e35b1 9348 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040
PA
9349 gdb::unique_xmalloc_ptr<char> filter_string
9350 (lsal->canonical != NULL ? xstrdup (lsal->canonical) : NULL);
0d381245 9351
f8eba3c6 9352 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9353 std::move (location),
e1e01040
PA
9354 std::move (filter_string),
9355 std::move (cond_string),
9356 std::move (extra_string),
e7e0cddf 9357 type, disposition,
84f4c1fe 9358 thread, task, ignore_count, ops,
44f238bb 9359 from_tty, enabled, internal, flags,
56435ebe 9360 canonical->special_display);
c3f6f71d 9361 }
c3f6f71d 9362}
c906108c 9363
f00aae0f 9364/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9365 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9366 addresses found. LOCATION points to the end of the SAL (for
9367 linespec locations).
9998af43
TJB
9368
9369 The array and the line spec strings are allocated on the heap, it is
9370 the caller's responsibility to free them. */
c906108c 9371
b9362cc7 9372static void
f00aae0f 9373parse_breakpoint_sals (const struct event_location *location,
58438ac1 9374 struct linespec_result *canonical)
c3f6f71d 9375{
f00aae0f
KS
9376 struct symtab_and_line cursal;
9377
9378 if (event_location_type (location) == LINESPEC_LOCATION)
9379 {
9380 const char *address = get_linespec_location (location);
9381
9382 if (address == NULL)
9383 {
9384 /* The last displayed codepoint, if it's valid, is our default
9385 breakpoint address. */
9386 if (last_displayed_sal_is_valid ())
9387 {
9388 struct linespec_sals lsal;
9389 struct symtab_and_line sal;
9390 CORE_ADDR pc;
9391
9392 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9393 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9394
9395 /* Set sal's pspace, pc, symtab, and line to the values
9396 corresponding to the last call to print_frame_info.
9397 Be sure to reinitialize LINE with NOTCURRENT == 0
9398 as the breakpoint line number is inappropriate otherwise.
9399 find_pc_line would adjust PC, re-set it back. */
9400 get_last_displayed_sal (&sal);
9401 pc = sal.pc;
9402 sal = find_pc_line (pc, 0);
9403
9404 /* "break" without arguments is equivalent to "break *PC"
9405 where PC is the last displayed codepoint's address. So
9406 make sure to set sal.explicit_pc to prevent GDB from
9407 trying to expand the list of sals to include all other
9408 instances with the same symtab and line. */
9409 sal.pc = pc;
9410 sal.explicit_pc = 1;
9411
9412 lsal.sals.sals[0] = sal;
9413 lsal.sals.nelts = 1;
9414 lsal.canonical = NULL;
9415
9416 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9417 return;
9418 }
9419 else
9420 error (_("No default breakpoint address now."));
c906108c 9421 }
c906108c 9422 }
f00aae0f
KS
9423
9424 /* Force almost all breakpoints to be in terms of the
9425 current_source_symtab (which is decode_line_1's default).
9426 This should produce the results we want almost all of the
9427 time while leaving default_breakpoint_* alone.
9428
9429 ObjC: However, don't match an Objective-C method name which
9430 may have a '+' or '-' succeeded by a '['. */
9431 cursal = get_current_source_symtab_and_line ();
9432 if (last_displayed_sal_is_valid ())
c906108c 9433 {
f00aae0f 9434 const char *address = NULL;
cc80f267 9435
f00aae0f
KS
9436 if (event_location_type (location) == LINESPEC_LOCATION)
9437 address = get_linespec_location (location);
cc80f267 9438
f00aae0f
KS
9439 if (!cursal.symtab
9440 || (address != NULL
9441 && strchr ("+-", address[0]) != NULL
9442 && address[1] != '['))
9443 {
c2f4122d 9444 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9445 get_last_displayed_symtab (),
9446 get_last_displayed_line (),
9447 canonical, NULL, NULL);
9448 return;
9449 }
c906108c 9450 }
f00aae0f 9451
c2f4122d 9452 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9453 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9454}
c906108c 9455
c906108c 9456
c3f6f71d 9457/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9458 inserted as a breakpoint. If it can't throw an error. */
c906108c 9459
b9362cc7 9460static void
23e7acfb 9461breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9462{
9463 int i;
cc59ec59 9464
c3f6f71d 9465 for (i = 0; i < sals->nelts; i++)
ee53e872 9466 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9467}
9468
7a697b8d
SS
9469/* Fast tracepoints may have restrictions on valid locations. For
9470 instance, a fast tracepoint using a jump instead of a trap will
9471 likely have to overwrite more bytes than a trap would, and so can
9472 only be placed where the instruction is longer than the jump, or a
9473 multi-instruction sequence does not have a jump into the middle of
9474 it, etc. */
9475
9476static void
9477check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9478 struct symtabs_and_lines *sals)
9479{
9480 int i, rslt;
9481 struct symtab_and_line *sal;
9482 char *msg;
9483 struct cleanup *old_chain;
9484
9485 for (i = 0; i < sals->nelts; i++)
9486 {
f8eba3c6
TT
9487 struct gdbarch *sarch;
9488
7a697b8d
SS
9489 sal = &sals->sals[i];
9490
f8eba3c6
TT
9491 sarch = get_sal_arch (*sal);
9492 /* We fall back to GDBARCH if there is no architecture
9493 associated with SAL. */
9494 if (sarch == NULL)
9495 sarch = gdbarch;
6b940e6a 9496 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9497 old_chain = make_cleanup (xfree, msg);
9498
9499 if (!rslt)
53c3572a 9500 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9501 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9502
9503 do_cleanups (old_chain);
9504 }
9505}
9506
018d34a4
VP
9507/* Given TOK, a string specification of condition and thread, as
9508 accepted by the 'break' command, extract the condition
9509 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9510 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9511 If no condition is found, *COND_STRING is set to NULL.
9512 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9513
9514static void
bbc13ae3 9515find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9516 char **cond_string, int *thread, int *task,
9517 char **rest)
018d34a4
VP
9518{
9519 *cond_string = NULL;
9520 *thread = -1;
ed1d1739
KS
9521 *task = 0;
9522 *rest = NULL;
9523
018d34a4
VP
9524 while (tok && *tok)
9525 {
bbc13ae3 9526 const char *end_tok;
018d34a4 9527 int toklen;
bbc13ae3
KS
9528 const char *cond_start = NULL;
9529 const char *cond_end = NULL;
cc59ec59 9530
bbc13ae3 9531 tok = skip_spaces_const (tok);
e7e0cddf
SS
9532
9533 if ((*tok == '"' || *tok == ',') && rest)
9534 {
9535 *rest = savestring (tok, strlen (tok));
9536 return;
9537 }
9538
bbc13ae3 9539 end_tok = skip_to_space_const (tok);
d634f2de 9540
018d34a4 9541 toklen = end_tok - tok;
d634f2de 9542
018d34a4
VP
9543 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9544 {
9545 tok = cond_start = end_tok + 1;
4d01a485 9546 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9547 cond_end = tok;
d634f2de 9548 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9549 }
9550 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9551 {
5d5658a1
PA
9552 const char *tmptok;
9553 struct thread_info *thr;
d634f2de 9554
018d34a4 9555 tok = end_tok + 1;
5d5658a1 9556 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9557 if (tok == tmptok)
9558 error (_("Junk after thread keyword."));
5d5658a1 9559 *thread = thr->global_num;
bbc13ae3 9560 tok = tmptok;
018d34a4 9561 }
4a306c9a
JB
9562 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9563 {
9564 char *tmptok;
9565
9566 tok = end_tok + 1;
bbc13ae3 9567 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9568 if (tok == tmptok)
9569 error (_("Junk after task keyword."));
9570 if (!valid_task_id (*task))
b6199126 9571 error (_("Unknown task %d."), *task);
bbc13ae3 9572 tok = tmptok;
4a306c9a 9573 }
e7e0cddf
SS
9574 else if (rest)
9575 {
9576 *rest = savestring (tok, strlen (tok));
ccab2054 9577 return;
e7e0cddf 9578 }
018d34a4
VP
9579 else
9580 error (_("Junk at end of arguments."));
9581 }
9582}
9583
0fb4aa4b
PA
9584/* Decode a static tracepoint marker spec. */
9585
9586static struct symtabs_and_lines
f00aae0f 9587decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9588{
9589 VEC(static_tracepoint_marker_p) *markers = NULL;
9590 struct symtabs_and_lines sals;
0fb4aa4b 9591 struct cleanup *old_chain;
f00aae0f
KS
9592 const char *p = &(*arg_p)[3];
9593 const char *endp;
0fb4aa4b
PA
9594 char *marker_str;
9595 int i;
9596
f00aae0f 9597 p = skip_spaces_const (p);
0fb4aa4b 9598
f00aae0f 9599 endp = skip_to_space_const (p);
0fb4aa4b
PA
9600
9601 marker_str = savestring (p, endp - p);
9602 old_chain = make_cleanup (xfree, marker_str);
9603
9604 markers = target_static_tracepoint_markers_by_strid (marker_str);
9605 if (VEC_empty(static_tracepoint_marker_p, markers))
9606 error (_("No known static tracepoint marker named %s"), marker_str);
9607
9608 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9609 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9610
9611 for (i = 0; i < sals.nelts; i++)
9612 {
9613 struct static_tracepoint_marker *marker;
9614
9615 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9616
9617 init_sal (&sals.sals[i]);
9618
9619 sals.sals[i] = find_pc_line (marker->address, 0);
9620 sals.sals[i].pc = marker->address;
9621
9622 release_static_tracepoint_marker (marker);
9623 }
9624
9625 do_cleanups (old_chain);
9626
9627 *arg_p = endp;
9628 return sals;
9629}
9630
f00aae0f 9631/* See breakpoint.h. */
0101ce28 9632
8cdf0e15
VP
9633int
9634create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9635 const struct event_location *location,
9636 const char *cond_string,
9637 int thread, const char *extra_string,
f00aae0f 9638 int parse_extra,
0fb4aa4b 9639 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9640 int ignore_count,
9641 enum auto_boolean pending_break_support,
c0a91b2b 9642 const struct breakpoint_ops *ops,
44f238bb
PA
9643 int from_tty, int enabled, int internal,
9644 unsigned flags)
c3f6f71d 9645{
7efd8fc2 9646 struct linespec_result canonical;
80c99de1 9647 struct cleanup *bkpt_chain = NULL;
0101ce28 9648 int pending = 0;
4a306c9a 9649 int task = 0;
86b17b60 9650 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9651
348d480f
PA
9652 gdb_assert (ops != NULL);
9653
f00aae0f
KS
9654 /* If extra_string isn't useful, set it to NULL. */
9655 if (extra_string != NULL && *extra_string == '\0')
9656 extra_string = NULL;
9657
492d29ea 9658 TRY
b78a6381 9659 {
f00aae0f 9660 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9661 }
492d29ea 9662 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9663 {
492d29ea
PA
9664 /* If caller is interested in rc value from parse, set
9665 value. */
9666 if (e.error == NOT_FOUND_ERROR)
0101ce28 9667 {
05ff989b
AC
9668 /* If pending breakpoint support is turned off, throw
9669 error. */
fa8d40ab
JJ
9670
9671 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9672 throw_exception (e);
9673
9674 exception_print (gdb_stderr, e);
fa8d40ab 9675
05ff989b
AC
9676 /* If pending breakpoint support is auto query and the user
9677 selects no, then simply return the error code. */
059fb39f 9678 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9679 && !nquery (_("Make %s pending on future shared library load? "),
9680 bptype_string (type_wanted)))
fd9b8c24 9681 return 0;
fa8d40ab 9682
05ff989b
AC
9683 /* At this point, either the user was queried about setting
9684 a pending breakpoint and selected yes, or pending
9685 breakpoint behavior is on and thus a pending breakpoint
9686 is defaulted on behalf of the user. */
f00aae0f 9687 pending = 1;
0101ce28 9688 }
492d29ea
PA
9689 else
9690 throw_exception (e);
0101ce28 9691 }
492d29ea
PA
9692 END_CATCH
9693
f00aae0f 9694 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9695 return 0;
c3f6f71d 9696
c3f6f71d
JM
9697 /* ----------------------------- SNIP -----------------------------
9698 Anything added to the cleanup chain beyond this point is assumed
9699 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9700 then the memory is not reclaimed. */
9701 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9702
c3f6f71d
JM
9703 /* Resolve all line numbers to PC's and verify that the addresses
9704 are ok for the target. */
0101ce28 9705 if (!pending)
f8eba3c6
TT
9706 {
9707 int ix;
9708 struct linespec_sals *iter;
9709
9710 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9711 breakpoint_sals_to_pc (&iter->sals);
9712 }
c3f6f71d 9713
7a697b8d 9714 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9715 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9716 {
9717 int ix;
9718 struct linespec_sals *iter;
9719
9720 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9721 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9722 }
7a697b8d 9723
c3f6f71d
JM
9724 /* Verify that condition can be parsed, before setting any
9725 breakpoints. Allocate a separate condition expression for each
4a64f543 9726 breakpoint. */
0101ce28 9727 if (!pending)
c3f6f71d 9728 {
e1e01040
PA
9729 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9730 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9731
f00aae0f 9732 if (parse_extra)
72b2ff0e 9733 {
0878d0fa 9734 char *rest;
e1e01040 9735 char *cond;
52d361e1
YQ
9736 struct linespec_sals *lsal;
9737
9738 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9739
0878d0fa
YQ
9740 /* Here we only parse 'arg' to separate condition
9741 from thread number, so parsing in context of first
9742 sal is OK. When setting the breakpoint we'll
9743 re-parse it in context of each sal. */
9744
f00aae0f 9745 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
e1e01040
PA
9746 &cond, &thread, &task, &rest);
9747 cond_string_copy.reset (cond);
9748 extra_string_copy.reset (rest);
72b2ff0e 9749 }
2f069f6f 9750 else
72b2ff0e 9751 {
f00aae0f
KS
9752 if (type_wanted != bp_dprintf
9753 && extra_string != NULL && *extra_string != '\0')
9754 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9755
9756 /* Create a private copy of condition string. */
9757 if (cond_string)
e1e01040 9758 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9759 /* Create a private copy of any extra string. */
9760 if (extra_string)
e1e01040 9761 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9762 }
0fb4aa4b 9763
52d361e1 9764 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9765 std::move (cond_string_copy),
9766 std::move (extra_string_copy),
9767 type_wanted,
d9b3f62e
PA
9768 tempflag ? disp_del : disp_donttouch,
9769 thread, task, ignore_count, ops,
44f238bb 9770 from_tty, enabled, internal, flags);
c906108c 9771 }
0101ce28
JJ
9772 else
9773 {
a5e364af 9774 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9775
a5e364af 9776 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9777 b->location = copy_event_location (location);
bfccc43c 9778
f00aae0f
KS
9779 if (parse_extra)
9780 b->cond_string = NULL;
e12c7713
MK
9781 else
9782 {
9783 /* Create a private copy of condition string. */
e1e01040 9784 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9785 b->thread = thread;
e12c7713 9786 }
f00aae0f
KS
9787
9788 /* Create a private copy of any extra string. */
e1e01040 9789 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9790 b->ignore_count = ignore_count;
0101ce28 9791 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9792 b->condition_not_parsed = 1;
41447f92 9793 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9794 if ((type_wanted != bp_breakpoint
9795 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9796 b->pspace = current_program_space;
8bea4e01 9797
b270e6f9 9798 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9799 }
9800
f8eba3c6 9801 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9802 {
3e43a32a
MS
9803 warning (_("Multiple breakpoints were set.\nUse the "
9804 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9805 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9806 }
9807
80c99de1
PA
9808 /* That's it. Discard the cleanups for data inserted into the
9809 breakpoint. */
9810 discard_cleanups (bkpt_chain);
217dc9e2 9811
80c99de1 9812 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9813 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9814
9815 return 1;
c3f6f71d 9816}
c906108c 9817
348d480f 9818/* Set a breakpoint.
72b2ff0e
VP
9819 ARG is a string describing breakpoint address,
9820 condition, and thread.
9821 FLAG specifies if a breakpoint is hardware on,
9822 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9823 and BP_TEMPFLAG. */
348d480f 9824
98deb0da 9825static void
72b2ff0e 9826break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9827{
72b2ff0e 9828 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9829 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9830 ? bp_hardware_breakpoint
9831 : bp_breakpoint);
55aa24fb 9832 struct breakpoint_ops *ops;
f00aae0f 9833
ffc2605c 9834 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9835
9836 /* Matching breakpoints on probes. */
5b56227b 9837 if (location != NULL
ffc2605c 9838 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9839 ops = &bkpt_probe_breakpoint_ops;
9840 else
9841 ops = &bkpt_breakpoint_ops;
c3f6f71d 9842
8cdf0e15 9843 create_breakpoint (get_current_arch (),
ffc2605c 9844 location.get (),
f00aae0f 9845 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9846 tempflag, type_wanted,
8cdf0e15
VP
9847 0 /* Ignore count */,
9848 pending_break_support,
55aa24fb 9849 ops,
8cdf0e15 9850 from_tty,
84f4c1fe 9851 1 /* enabled */,
44f238bb
PA
9852 0 /* internal */,
9853 0);
c906108c
SS
9854}
9855
c906108c
SS
9856/* Helper function for break_command_1 and disassemble_command. */
9857
9858void
fba45db2 9859resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9860{
9861 CORE_ADDR pc;
9862
9863 if (sal->pc == 0 && sal->symtab != NULL)
9864 {
9865 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9866 error (_("No line %d in file \"%s\"."),
05cba821 9867 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9868 sal->pc = pc;
6a048695 9869
4a64f543
MS
9870 /* If this SAL corresponds to a breakpoint inserted using a line
9871 number, then skip the function prologue if necessary. */
6a048695 9872 if (sal->explicit_line)
059acae7 9873 skip_prologue_sal (sal);
c906108c
SS
9874 }
9875
9876 if (sal->section == 0 && sal->symtab != NULL)
9877 {
346d1dfe 9878 const struct blockvector *bv;
3977b71f 9879 const struct block *b;
c5aa993b 9880 struct symbol *sym;
c906108c 9881
43f3e411
DE
9882 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9883 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9884 if (bv != NULL)
9885 {
7f0df278 9886 sym = block_linkage_function (b);
c906108c
SS
9887 if (sym != NULL)
9888 {
eb822aa6
DE
9889 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9890 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9891 sym);
c906108c
SS
9892 }
9893 else
9894 {
4a64f543
MS
9895 /* It really is worthwhile to have the section, so we'll
9896 just have to look harder. This case can be executed
9897 if we have line numbers but no functions (as can
9898 happen in assembly source). */
c906108c 9899
5ed8105e 9900 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9901 switch_to_program_space_and_thread (sal->pspace);
c906108c 9902
5ed8105e 9903 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9904 if (msym.minsym)
efd66ac6 9905 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9906 }
9907 }
9908 }
9909}
9910
9911void
fba45db2 9912break_command (char *arg, int from_tty)
c906108c 9913{
db107f19 9914 break_command_1 (arg, 0, from_tty);
c906108c
SS
9915}
9916
c906108c 9917void
fba45db2 9918tbreak_command (char *arg, int from_tty)
c906108c 9919{
db107f19 9920 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9921}
9922
c906108c 9923static void
fba45db2 9924hbreak_command (char *arg, int from_tty)
c906108c 9925{
db107f19 9926 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9927}
9928
9929static void
fba45db2 9930thbreak_command (char *arg, int from_tty)
c906108c 9931{
db107f19 9932 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9933}
9934
9935static void
fba45db2 9936stop_command (char *arg, int from_tty)
c906108c 9937{
a3f17187 9938 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9939Usage: stop in <function | address>\n\
a3f17187 9940 stop at <line>\n"));
c906108c
SS
9941}
9942
9943static void
fba45db2 9944stopin_command (char *arg, int from_tty)
c906108c
SS
9945{
9946 int badInput = 0;
9947
c5aa993b 9948 if (arg == (char *) NULL)
c906108c
SS
9949 badInput = 1;
9950 else if (*arg != '*')
9951 {
9952 char *argptr = arg;
9953 int hasColon = 0;
9954
4a64f543 9955 /* Look for a ':'. If this is a line number specification, then
53a5351d 9956 say it is bad, otherwise, it should be an address or
4a64f543 9957 function/method name. */
c906108c 9958 while (*argptr && !hasColon)
c5aa993b
JM
9959 {
9960 hasColon = (*argptr == ':');
9961 argptr++;
9962 }
c906108c
SS
9963
9964 if (hasColon)
c5aa993b 9965 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9966 else
c5aa993b 9967 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9968 }
9969
9970 if (badInput)
a3f17187 9971 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9972 else
db107f19 9973 break_command_1 (arg, 0, from_tty);
c906108c
SS
9974}
9975
9976static void
fba45db2 9977stopat_command (char *arg, int from_tty)
c906108c
SS
9978{
9979 int badInput = 0;
9980
c5aa993b 9981 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9982 badInput = 1;
9983 else
9984 {
9985 char *argptr = arg;
9986 int hasColon = 0;
9987
4a64f543
MS
9988 /* Look for a ':'. If there is a '::' then get out, otherwise
9989 it is probably a line number. */
c906108c 9990 while (*argptr && !hasColon)
c5aa993b
JM
9991 {
9992 hasColon = (*argptr == ':');
9993 argptr++;
9994 }
c906108c
SS
9995
9996 if (hasColon)
c5aa993b 9997 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9998 else
c5aa993b 9999 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10000 }
10001
10002 if (badInput)
a3f17187 10003 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10004 else
db107f19 10005 break_command_1 (arg, 0, from_tty);
c906108c
SS
10006}
10007
e7e0cddf
SS
10008/* The dynamic printf command is mostly like a regular breakpoint, but
10009 with a prewired command list consisting of a single output command,
10010 built from extra arguments supplied on the dprintf command
10011 line. */
10012
da821c7b 10013static void
e7e0cddf
SS
10014dprintf_command (char *arg, int from_tty)
10015{
ffc2605c 10016 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10017
10018 /* If non-NULL, ARG should have been advanced past the location;
10019 the next character must be ','. */
10020 if (arg != NULL)
10021 {
10022 if (arg[0] != ',' || arg[1] == '\0')
10023 error (_("Format string required"));
10024 else
10025 {
10026 /* Skip the comma. */
10027 ++arg;
10028 }
10029 }
10030
e7e0cddf 10031 create_breakpoint (get_current_arch (),
ffc2605c 10032 location.get (),
f00aae0f 10033 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10034 0, bp_dprintf,
10035 0 /* Ignore count */,
10036 pending_break_support,
10037 &dprintf_breakpoint_ops,
10038 from_tty,
10039 1 /* enabled */,
10040 0 /* internal */,
10041 0);
10042}
10043
d3ce09f5
SS
10044static void
10045agent_printf_command (char *arg, int from_tty)
10046{
10047 error (_("May only run agent-printf on the target"));
10048}
10049
f1310107
TJB
10050/* Implement the "breakpoint_hit" breakpoint_ops method for
10051 ranged breakpoints. */
10052
10053static int
10054breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10055 struct address_space *aspace,
09ac7c10
TT
10056 CORE_ADDR bp_addr,
10057 const struct target_waitstatus *ws)
f1310107 10058{
09ac7c10 10059 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10060 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10061 return 0;
10062
f1310107
TJB
10063 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10064 bl->length, aspace, bp_addr);
10065}
10066
10067/* Implement the "resources_needed" breakpoint_ops method for
10068 ranged breakpoints. */
10069
10070static int
10071resources_needed_ranged_breakpoint (const struct bp_location *bl)
10072{
10073 return target_ranged_break_num_registers ();
10074}
10075
10076/* Implement the "print_it" breakpoint_ops method for
10077 ranged breakpoints. */
10078
10079static enum print_stop_action
348d480f 10080print_it_ranged_breakpoint (bpstat bs)
f1310107 10081{
348d480f 10082 struct breakpoint *b = bs->breakpoint_at;
f1310107 10083 struct bp_location *bl = b->loc;
79a45e25 10084 struct ui_out *uiout = current_uiout;
f1310107
TJB
10085
10086 gdb_assert (b->type == bp_hardware_breakpoint);
10087
10088 /* Ranged breakpoints have only one location. */
10089 gdb_assert (bl && bl->next == NULL);
10090
10091 annotate_breakpoint (b->number);
f303dbd6
PA
10092
10093 maybe_print_thread_hit_breakpoint (uiout);
10094
f1310107 10095 if (b->disposition == disp_del)
112e8700 10096 uiout->text ("Temporary ranged breakpoint ");
f1310107 10097 else
112e8700
SM
10098 uiout->text ("Ranged breakpoint ");
10099 if (uiout->is_mi_like_p ())
f1310107 10100 {
112e8700 10101 uiout->field_string ("reason",
f1310107 10102 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10103 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10104 }
112e8700
SM
10105 uiout->field_int ("bkptno", b->number);
10106 uiout->text (", ");
f1310107
TJB
10107
10108 return PRINT_SRC_AND_LOC;
10109}
10110
10111/* Implement the "print_one" breakpoint_ops method for
10112 ranged breakpoints. */
10113
10114static void
10115print_one_ranged_breakpoint (struct breakpoint *b,
10116 struct bp_location **last_loc)
10117{
10118 struct bp_location *bl = b->loc;
10119 struct value_print_options opts;
79a45e25 10120 struct ui_out *uiout = current_uiout;
f1310107
TJB
10121
10122 /* Ranged breakpoints have only one location. */
10123 gdb_assert (bl && bl->next == NULL);
10124
10125 get_user_print_options (&opts);
10126
10127 if (opts.addressprint)
10128 /* We don't print the address range here, it will be printed later
10129 by print_one_detail_ranged_breakpoint. */
112e8700 10130 uiout->field_skip ("addr");
f1310107
TJB
10131 annotate_field (5);
10132 print_breakpoint_location (b, bl);
10133 *last_loc = bl;
10134}
10135
10136/* Implement the "print_one_detail" breakpoint_ops method for
10137 ranged breakpoints. */
10138
10139static void
10140print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10141 struct ui_out *uiout)
10142{
10143 CORE_ADDR address_start, address_end;
10144 struct bp_location *bl = b->loc;
d7e74731 10145 string_file stb;
f1310107
TJB
10146
10147 gdb_assert (bl);
10148
10149 address_start = bl->address;
10150 address_end = address_start + bl->length - 1;
10151
112e8700 10152 uiout->text ("\taddress range: ");
d7e74731
PA
10153 stb.printf ("[%s, %s]",
10154 print_core_address (bl->gdbarch, address_start),
10155 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10156 uiout->field_stream ("addr", stb);
10157 uiout->text ("\n");
f1310107
TJB
10158}
10159
10160/* Implement the "print_mention" breakpoint_ops method for
10161 ranged breakpoints. */
10162
10163static void
10164print_mention_ranged_breakpoint (struct breakpoint *b)
10165{
10166 struct bp_location *bl = b->loc;
79a45e25 10167 struct ui_out *uiout = current_uiout;
f1310107
TJB
10168
10169 gdb_assert (bl);
10170 gdb_assert (b->type == bp_hardware_breakpoint);
10171
112e8700 10172 if (uiout->is_mi_like_p ())
f1310107
TJB
10173 return;
10174
10175 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10176 b->number, paddress (bl->gdbarch, bl->address),
10177 paddress (bl->gdbarch, bl->address + bl->length - 1));
10178}
10179
10180/* Implement the "print_recreate" breakpoint_ops method for
10181 ranged breakpoints. */
10182
10183static void
10184print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10185{
f00aae0f 10186 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10187 event_location_to_string (b->location.get ()),
10188 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10189 print_recreate_thread (b, fp);
f1310107
TJB
10190}
10191
10192/* The breakpoint_ops structure to be used in ranged breakpoints. */
10193
2060206e 10194static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10195
10196/* Find the address where the end of the breakpoint range should be
10197 placed, given the SAL of the end of the range. This is so that if
10198 the user provides a line number, the end of the range is set to the
10199 last instruction of the given line. */
10200
10201static CORE_ADDR
10202find_breakpoint_range_end (struct symtab_and_line sal)
10203{
10204 CORE_ADDR end;
10205
10206 /* If the user provided a PC value, use it. Otherwise,
10207 find the address of the end of the given location. */
10208 if (sal.explicit_pc)
10209 end = sal.pc;
10210 else
10211 {
10212 int ret;
10213 CORE_ADDR start;
10214
10215 ret = find_line_pc_range (sal, &start, &end);
10216 if (!ret)
10217 error (_("Could not find location of the end of the range."));
10218
10219 /* find_line_pc_range returns the start of the next line. */
10220 end--;
10221 }
10222
10223 return end;
10224}
10225
10226/* Implement the "break-range" CLI command. */
10227
10228static void
10229break_range_command (char *arg, int from_tty)
10230{
870f88f7 10231 char *arg_start, *addr_string_start;
f1310107
TJB
10232 struct linespec_result canonical_start, canonical_end;
10233 int bp_count, can_use_bp, length;
10234 CORE_ADDR end;
10235 struct breakpoint *b;
10236 struct symtab_and_line sal_start, sal_end;
f1310107 10237 struct cleanup *cleanup_bkpt;
f8eba3c6 10238 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10239
10240 /* We don't support software ranged breakpoints. */
10241 if (target_ranged_break_num_registers () < 0)
10242 error (_("This target does not support hardware ranged breakpoints."));
10243
10244 bp_count = hw_breakpoint_used_count ();
10245 bp_count += target_ranged_break_num_registers ();
10246 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10247 bp_count, 0);
10248 if (can_use_bp < 0)
10249 error (_("Hardware breakpoints used exceeds limit."));
10250
f8eba3c6 10251 arg = skip_spaces (arg);
f1310107
TJB
10252 if (arg == NULL || arg[0] == '\0')
10253 error(_("No address range specified."));
10254
f8eba3c6 10255 arg_start = arg;
ffc2605c
TT
10256 event_location_up start_location = string_to_event_location (&arg,
10257 current_language);
10258 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10259
10260 if (arg[0] != ',')
10261 error (_("Too few arguments."));
f8eba3c6 10262 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10263 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10264
10265 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10266
10267 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10268 || lsal_start->sals.nelts != 1)
f1310107
TJB
10269 error (_("Cannot create a ranged breakpoint with multiple locations."));
10270
f8eba3c6
TT
10271 sal_start = lsal_start->sals.sals[0];
10272 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10273 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10274
10275 arg++; /* Skip the comma. */
f8eba3c6 10276 arg = skip_spaces (arg);
f1310107
TJB
10277
10278 /* Parse the end location. */
10279
f1310107
TJB
10280 arg_start = arg;
10281
f8eba3c6 10282 /* We call decode_line_full directly here instead of using
f1310107
TJB
10283 parse_breakpoint_sals because we need to specify the start location's
10284 symtab and line as the default symtab and line for the end of the
10285 range. This makes it possible to have ranges like "foo.c:27, +14",
10286 where +14 means 14 lines from the start location. */
ffc2605c
TT
10287 event_location_up end_location = string_to_event_location (&arg,
10288 current_language);
10289 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10290 sal_start.symtab, sal_start.line,
10291 &canonical_end, NULL, NULL);
10292
f8eba3c6 10293 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10294 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10295
10296 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10297 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10298 || lsal_end->sals.nelts != 1)
f1310107
TJB
10299 error (_("Cannot create a ranged breakpoint with multiple locations."));
10300
f8eba3c6 10301 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10302
10303 end = find_breakpoint_range_end (sal_end);
10304 if (sal_start.pc > end)
177b42fe 10305 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10306
10307 length = end - sal_start.pc + 1;
10308 if (length < 0)
10309 /* Length overflowed. */
10310 error (_("Address range too large."));
10311 else if (length == 1)
10312 {
10313 /* This range is simple enough to be handled by
10314 the `hbreak' command. */
10315 hbreak_command (addr_string_start, 1);
10316
10317 do_cleanups (cleanup_bkpt);
10318
10319 return;
10320 }
10321
10322 /* Now set up the breakpoint. */
10323 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10324 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10325 set_breakpoint_count (breakpoint_count + 1);
10326 b->number = breakpoint_count;
10327 b->disposition = disp_donttouch;
d28cd78a
TT
10328 b->location = std::move (start_location);
10329 b->location_range_end = std::move (end_location);
f1310107
TJB
10330 b->loc->length = length;
10331
f8eba3c6 10332 do_cleanups (cleanup_bkpt);
f1310107
TJB
10333
10334 mention (b);
8d3788bd 10335 observer_notify_breakpoint_created (b);
44702360 10336 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10337}
10338
4a64f543
MS
10339/* Return non-zero if EXP is verified as constant. Returned zero
10340 means EXP is variable. Also the constant detection may fail for
10341 some constant expressions and in such case still falsely return
10342 zero. */
2e6e3d9c 10343
65d79d4b
SDJ
10344static int
10345watchpoint_exp_is_const (const struct expression *exp)
10346{
10347 int i = exp->nelts;
10348
10349 while (i > 0)
10350 {
10351 int oplenp, argsp;
10352
10353 /* We are only interested in the descriptor of each element. */
10354 operator_length (exp, i, &oplenp, &argsp);
10355 i -= oplenp;
10356
10357 switch (exp->elts[i].opcode)
10358 {
10359 case BINOP_ADD:
10360 case BINOP_SUB:
10361 case BINOP_MUL:
10362 case BINOP_DIV:
10363 case BINOP_REM:
10364 case BINOP_MOD:
10365 case BINOP_LSH:
10366 case BINOP_RSH:
10367 case BINOP_LOGICAL_AND:
10368 case BINOP_LOGICAL_OR:
10369 case BINOP_BITWISE_AND:
10370 case BINOP_BITWISE_IOR:
10371 case BINOP_BITWISE_XOR:
10372 case BINOP_EQUAL:
10373 case BINOP_NOTEQUAL:
10374 case BINOP_LESS:
10375 case BINOP_GTR:
10376 case BINOP_LEQ:
10377 case BINOP_GEQ:
10378 case BINOP_REPEAT:
10379 case BINOP_COMMA:
10380 case BINOP_EXP:
10381 case BINOP_MIN:
10382 case BINOP_MAX:
10383 case BINOP_INTDIV:
10384 case BINOP_CONCAT:
65d79d4b
SDJ
10385 case TERNOP_COND:
10386 case TERNOP_SLICE:
65d79d4b
SDJ
10387
10388 case OP_LONG:
10389 case OP_DOUBLE:
10390 case OP_DECFLOAT:
10391 case OP_LAST:
10392 case OP_COMPLEX:
10393 case OP_STRING:
65d79d4b
SDJ
10394 case OP_ARRAY:
10395 case OP_TYPE:
608b4967
TT
10396 case OP_TYPEOF:
10397 case OP_DECLTYPE:
6e72ca20 10398 case OP_TYPEID:
65d79d4b
SDJ
10399 case OP_NAME:
10400 case OP_OBJC_NSSTRING:
10401
10402 case UNOP_NEG:
10403 case UNOP_LOGICAL_NOT:
10404 case UNOP_COMPLEMENT:
10405 case UNOP_ADDR:
10406 case UNOP_HIGH:
aeaa2474 10407 case UNOP_CAST:
9eaf6705
TT
10408
10409 case UNOP_CAST_TYPE:
10410 case UNOP_REINTERPRET_CAST:
10411 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10412 /* Unary, binary and ternary operators: We have to check
10413 their operands. If they are constant, then so is the
10414 result of that operation. For instance, if A and B are
10415 determined to be constants, then so is "A + B".
10416
10417 UNOP_IND is one exception to the rule above, because the
10418 value of *ADDR is not necessarily a constant, even when
10419 ADDR is. */
65d79d4b
SDJ
10420 break;
10421
10422 case OP_VAR_VALUE:
10423 /* Check whether the associated symbol is a constant.
4a64f543 10424
65d79d4b 10425 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10426 possible that a buggy compiler could mark a variable as
10427 constant even when it is not, and TYPE_CONST would return
10428 true in this case, while SYMBOL_CLASS wouldn't.
10429
10430 We also have to check for function symbols because they
10431 are always constant. */
65d79d4b
SDJ
10432 {
10433 struct symbol *s = exp->elts[i + 2].symbol;
10434
10435 if (SYMBOL_CLASS (s) != LOC_BLOCK
10436 && SYMBOL_CLASS (s) != LOC_CONST
10437 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10438 return 0;
10439 break;
10440 }
10441
10442 /* The default action is to return 0 because we are using
10443 the optimistic approach here: If we don't know something,
10444 then it is not a constant. */
10445 default:
10446 return 0;
10447 }
10448 }
10449
10450 return 1;
10451}
10452
c1fc2657 10453/* Watchpoint destructor. */
3a5c3e22 10454
c1fc2657 10455watchpoint::~watchpoint ()
3a5c3e22 10456{
c1fc2657
SM
10457 xfree (this->exp_string);
10458 xfree (this->exp_string_reparse);
10459 value_free (this->val);
3a5c3e22
PA
10460}
10461
348d480f
PA
10462/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10463
10464static void
10465re_set_watchpoint (struct breakpoint *b)
10466{
3a5c3e22
PA
10467 struct watchpoint *w = (struct watchpoint *) b;
10468
348d480f
PA
10469 /* Watchpoint can be either on expression using entirely global
10470 variables, or it can be on local variables.
10471
10472 Watchpoints of the first kind are never auto-deleted, and even
10473 persist across program restarts. Since they can use variables
10474 from shared libraries, we need to reparse expression as libraries
10475 are loaded and unloaded.
10476
10477 Watchpoints on local variables can also change meaning as result
10478 of solib event. For example, if a watchpoint uses both a local
10479 and a global variables in expression, it's a local watchpoint,
10480 but unloading of a shared library will make the expression
10481 invalid. This is not a very common use case, but we still
10482 re-evaluate expression, to avoid surprises to the user.
10483
10484 Note that for local watchpoints, we re-evaluate it only if
10485 watchpoints frame id is still valid. If it's not, it means the
10486 watchpoint is out of scope and will be deleted soon. In fact,
10487 I'm not sure we'll ever be called in this case.
10488
10489 If a local watchpoint's frame id is still valid, then
3a5c3e22 10490 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10491
3a5c3e22
PA
10492 Don't do anything about disabled watchpoints, since they will be
10493 reevaluated again when enabled. */
10494 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10495}
10496
77b06cd7
TJB
10497/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10498
10499static int
10500insert_watchpoint (struct bp_location *bl)
10501{
3a5c3e22
PA
10502 struct watchpoint *w = (struct watchpoint *) bl->owner;
10503 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10504
10505 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10506 w->cond_exp.get ());
77b06cd7
TJB
10507}
10508
10509/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10510
10511static int
73971819 10512remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10513{
3a5c3e22
PA
10514 struct watchpoint *w = (struct watchpoint *) bl->owner;
10515 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10516
10517 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10518 w->cond_exp.get ());
e09342b5
TJB
10519}
10520
e09342b5 10521static int
348d480f 10522breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10523 struct address_space *aspace, CORE_ADDR bp_addr,
10524 const struct target_waitstatus *ws)
e09342b5 10525{
348d480f 10526 struct breakpoint *b = bl->owner;
3a5c3e22 10527 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10528
348d480f
PA
10529 /* Continuable hardware watchpoints are treated as non-existent if the
10530 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10531 some data address). Otherwise gdb won't stop on a break instruction
10532 in the code (not from a breakpoint) when a hardware watchpoint has
10533 been defined. Also skip watchpoints which we know did not trigger
10534 (did not match the data address). */
10535 if (is_hardware_watchpoint (b)
3a5c3e22 10536 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10537 return 0;
9c06b0b4 10538
348d480f 10539 return 1;
9c06b0b4
TJB
10540}
10541
348d480f
PA
10542static void
10543check_status_watchpoint (bpstat bs)
9c06b0b4 10544{
348d480f 10545 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10546
348d480f 10547 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10548}
10549
10550/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10551 hardware watchpoints. */
9c06b0b4
TJB
10552
10553static int
348d480f 10554resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10555{
3a5c3e22
PA
10556 struct watchpoint *w = (struct watchpoint *) bl->owner;
10557 int length = w->exact? 1 : bl->length;
348d480f
PA
10558
10559 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10560}
10561
10562/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10563 hardware watchpoints. */
9c06b0b4
TJB
10564
10565static int
348d480f 10566works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10567{
efa80663
PA
10568 /* Read and access watchpoints only work with hardware support. */
10569 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10570}
10571
9c06b0b4 10572static enum print_stop_action
348d480f 10573print_it_watchpoint (bpstat bs)
9c06b0b4 10574{
348d480f
PA
10575 struct cleanup *old_chain;
10576 struct breakpoint *b;
348d480f 10577 enum print_stop_action result;
3a5c3e22 10578 struct watchpoint *w;
79a45e25 10579 struct ui_out *uiout = current_uiout;
348d480f
PA
10580
10581 gdb_assert (bs->bp_location_at != NULL);
10582
348d480f 10583 b = bs->breakpoint_at;
3a5c3e22 10584 w = (struct watchpoint *) b;
348d480f 10585
d7e74731 10586 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10587
f303dbd6
PA
10588 annotate_watchpoint (b->number);
10589 maybe_print_thread_hit_breakpoint (uiout);
10590
d7e74731
PA
10591 string_file stb;
10592
9c06b0b4
TJB
10593 switch (b->type)
10594 {
348d480f 10595 case bp_watchpoint:
9c06b0b4 10596 case bp_hardware_watchpoint:
112e8700
SM
10597 if (uiout->is_mi_like_p ())
10598 uiout->field_string
10599 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10600 mention (b);
10601 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10602 uiout->text ("\nOld value = ");
d7e74731 10603 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10604 uiout->field_stream ("old", stb);
10605 uiout->text ("\nNew value = ");
d7e74731 10606 watchpoint_value_print (w->val, &stb);
112e8700
SM
10607 uiout->field_stream ("new", stb);
10608 uiout->text ("\n");
348d480f
PA
10609 /* More than one watchpoint may have been triggered. */
10610 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10611 break;
10612
10613 case bp_read_watchpoint:
112e8700
SM
10614 if (uiout->is_mi_like_p ())
10615 uiout->field_string
10616 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10617 mention (b);
10618 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10619 uiout->text ("\nValue = ");
d7e74731 10620 watchpoint_value_print (w->val, &stb);
112e8700
SM
10621 uiout->field_stream ("value", stb);
10622 uiout->text ("\n");
348d480f 10623 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10624 break;
10625
10626 case bp_access_watchpoint:
348d480f
PA
10627 if (bs->old_val != NULL)
10628 {
112e8700
SM
10629 if (uiout->is_mi_like_p ())
10630 uiout->field_string
10631 ("reason",
348d480f
PA
10632 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10633 mention (b);
10634 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10635 uiout->text ("\nOld value = ");
d7e74731 10636 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10637 uiout->field_stream ("old", stb);
10638 uiout->text ("\nNew value = ");
348d480f
PA
10639 }
10640 else
10641 {
10642 mention (b);
112e8700
SM
10643 if (uiout->is_mi_like_p ())
10644 uiout->field_string
10645 ("reason",
348d480f
PA
10646 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10647 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10648 uiout->text ("\nValue = ");
348d480f 10649 }
d7e74731 10650 watchpoint_value_print (w->val, &stb);
112e8700
SM
10651 uiout->field_stream ("new", stb);
10652 uiout->text ("\n");
348d480f 10653 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10654 break;
10655 default:
348d480f 10656 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10657 }
10658
348d480f
PA
10659 do_cleanups (old_chain);
10660 return result;
10661}
10662
10663/* Implement the "print_mention" breakpoint_ops method for hardware
10664 watchpoints. */
10665
10666static void
10667print_mention_watchpoint (struct breakpoint *b)
10668{
3a5c3e22 10669 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10670 struct ui_out *uiout = current_uiout;
46b9c129 10671 const char *tuple_name;
348d480f
PA
10672
10673 switch (b->type)
10674 {
10675 case bp_watchpoint:
112e8700 10676 uiout->text ("Watchpoint ");
46b9c129 10677 tuple_name = "wpt";
348d480f
PA
10678 break;
10679 case bp_hardware_watchpoint:
112e8700 10680 uiout->text ("Hardware watchpoint ");
46b9c129 10681 tuple_name = "wpt";
348d480f
PA
10682 break;
10683 case bp_read_watchpoint:
112e8700 10684 uiout->text ("Hardware read watchpoint ");
46b9c129 10685 tuple_name = "hw-rwpt";
348d480f
PA
10686 break;
10687 case bp_access_watchpoint:
112e8700 10688 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10689 tuple_name = "hw-awpt";
348d480f
PA
10690 break;
10691 default:
10692 internal_error (__FILE__, __LINE__,
10693 _("Invalid hardware watchpoint type."));
10694 }
10695
46b9c129 10696 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10697 uiout->field_int ("number", b->number);
10698 uiout->text (": ");
10699 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10700}
10701
10702/* Implement the "print_recreate" breakpoint_ops method for
10703 watchpoints. */
10704
10705static void
10706print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10707{
3a5c3e22
PA
10708 struct watchpoint *w = (struct watchpoint *) b;
10709
348d480f
PA
10710 switch (b->type)
10711 {
10712 case bp_watchpoint:
10713 case bp_hardware_watchpoint:
10714 fprintf_unfiltered (fp, "watch");
10715 break;
10716 case bp_read_watchpoint:
10717 fprintf_unfiltered (fp, "rwatch");
10718 break;
10719 case bp_access_watchpoint:
10720 fprintf_unfiltered (fp, "awatch");
10721 break;
10722 default:
10723 internal_error (__FILE__, __LINE__,
10724 _("Invalid watchpoint type."));
10725 }
10726
3a5c3e22 10727 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10728 print_recreate_thread (b, fp);
348d480f
PA
10729}
10730
427cd150
TT
10731/* Implement the "explains_signal" breakpoint_ops method for
10732 watchpoints. */
10733
47591c29 10734static int
427cd150
TT
10735explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10736{
10737 /* A software watchpoint cannot cause a signal other than
10738 GDB_SIGNAL_TRAP. */
10739 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10740 return 0;
427cd150 10741
47591c29 10742 return 1;
427cd150
TT
10743}
10744
348d480f
PA
10745/* The breakpoint_ops structure to be used in hardware watchpoints. */
10746
2060206e 10747static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10748
10749/* Implement the "insert" breakpoint_ops method for
10750 masked hardware watchpoints. */
10751
10752static int
10753insert_masked_watchpoint (struct bp_location *bl)
10754{
3a5c3e22
PA
10755 struct watchpoint *w = (struct watchpoint *) bl->owner;
10756
10757 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10758 bl->watchpoint_type);
10759}
10760
10761/* Implement the "remove" breakpoint_ops method for
10762 masked hardware watchpoints. */
10763
10764static int
73971819 10765remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10766{
3a5c3e22
PA
10767 struct watchpoint *w = (struct watchpoint *) bl->owner;
10768
10769 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10770 bl->watchpoint_type);
10771}
10772
10773/* Implement the "resources_needed" breakpoint_ops method for
10774 masked hardware watchpoints. */
10775
10776static int
10777resources_needed_masked_watchpoint (const struct bp_location *bl)
10778{
3a5c3e22
PA
10779 struct watchpoint *w = (struct watchpoint *) bl->owner;
10780
10781 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10782}
10783
10784/* Implement the "works_in_software_mode" breakpoint_ops method for
10785 masked hardware watchpoints. */
10786
10787static int
10788works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10789{
10790 return 0;
10791}
10792
10793/* Implement the "print_it" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796static enum print_stop_action
10797print_it_masked_watchpoint (bpstat bs)
10798{
10799 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10800 struct ui_out *uiout = current_uiout;
348d480f
PA
10801
10802 /* Masked watchpoints have only one location. */
10803 gdb_assert (b->loc && b->loc->next == NULL);
10804
f303dbd6
PA
10805 annotate_watchpoint (b->number);
10806 maybe_print_thread_hit_breakpoint (uiout);
10807
348d480f
PA
10808 switch (b->type)
10809 {
10810 case bp_hardware_watchpoint:
112e8700
SM
10811 if (uiout->is_mi_like_p ())
10812 uiout->field_string
10813 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10814 break;
10815
10816 case bp_read_watchpoint:
112e8700
SM
10817 if (uiout->is_mi_like_p ())
10818 uiout->field_string
10819 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10820 break;
10821
10822 case bp_access_watchpoint:
112e8700
SM
10823 if (uiout->is_mi_like_p ())
10824 uiout->field_string
10825 ("reason",
348d480f
PA
10826 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10827 break;
10828 default:
10829 internal_error (__FILE__, __LINE__,
10830 _("Invalid hardware watchpoint type."));
10831 }
10832
10833 mention (b);
112e8700 10834 uiout->text (_("\n\
9c06b0b4
TJB
10835Check the underlying instruction at PC for the memory\n\
10836address and value which triggered this watchpoint.\n"));
112e8700 10837 uiout->text ("\n");
9c06b0b4
TJB
10838
10839 /* More than one watchpoint may have been triggered. */
10840 return PRINT_UNKNOWN;
10841}
10842
10843/* Implement the "print_one_detail" breakpoint_ops method for
10844 masked hardware watchpoints. */
10845
10846static void
10847print_one_detail_masked_watchpoint (const struct breakpoint *b,
10848 struct ui_out *uiout)
10849{
3a5c3e22
PA
10850 struct watchpoint *w = (struct watchpoint *) b;
10851
9c06b0b4
TJB
10852 /* Masked watchpoints have only one location. */
10853 gdb_assert (b->loc && b->loc->next == NULL);
10854
112e8700
SM
10855 uiout->text ("\tmask ");
10856 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10857 uiout->text ("\n");
9c06b0b4
TJB
10858}
10859
10860/* Implement the "print_mention" breakpoint_ops method for
10861 masked hardware watchpoints. */
10862
10863static void
10864print_mention_masked_watchpoint (struct breakpoint *b)
10865{
3a5c3e22 10866 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10867 struct ui_out *uiout = current_uiout;
46b9c129 10868 const char *tuple_name;
9c06b0b4
TJB
10869
10870 switch (b->type)
10871 {
10872 case bp_hardware_watchpoint:
112e8700 10873 uiout->text ("Masked hardware watchpoint ");
46b9c129 10874 tuple_name = "wpt";
9c06b0b4
TJB
10875 break;
10876 case bp_read_watchpoint:
112e8700 10877 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10878 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10879 break;
10880 case bp_access_watchpoint:
112e8700 10881 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10882 tuple_name = "hw-awpt";
9c06b0b4
TJB
10883 break;
10884 default:
10885 internal_error (__FILE__, __LINE__,
10886 _("Invalid hardware watchpoint type."));
10887 }
10888
46b9c129 10889 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10890 uiout->field_int ("number", b->number);
10891 uiout->text (": ");
10892 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10893}
10894
10895/* Implement the "print_recreate" breakpoint_ops method for
10896 masked hardware watchpoints. */
10897
10898static void
10899print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10900{
3a5c3e22 10901 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10902 char tmp[40];
10903
10904 switch (b->type)
10905 {
10906 case bp_hardware_watchpoint:
10907 fprintf_unfiltered (fp, "watch");
10908 break;
10909 case bp_read_watchpoint:
10910 fprintf_unfiltered (fp, "rwatch");
10911 break;
10912 case bp_access_watchpoint:
10913 fprintf_unfiltered (fp, "awatch");
10914 break;
10915 default:
10916 internal_error (__FILE__, __LINE__,
10917 _("Invalid hardware watchpoint type."));
10918 }
10919
3a5c3e22
PA
10920 sprintf_vma (tmp, w->hw_wp_mask);
10921 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10922 print_recreate_thread (b, fp);
9c06b0b4
TJB
10923}
10924
10925/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10926
2060206e 10927static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10928
10929/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10930
10931static int
10932is_masked_watchpoint (const struct breakpoint *b)
10933{
10934 return b->ops == &masked_watchpoint_breakpoint_ops;
10935}
10936
53a5351d
JM
10937/* accessflag: hw_write: watch write,
10938 hw_read: watch read,
10939 hw_access: watch access (read or write) */
c906108c 10940static void
bbc13ae3 10941watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10942 int just_location, int internal)
c906108c 10943{
c1fc2657 10944 struct breakpoint *scope_breakpoint = NULL;
270140bd 10945 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10946 struct value *val, *mark, *result;
bb9d5f81 10947 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10948 const char *exp_start = NULL;
10949 const char *exp_end = NULL;
10950 const char *tok, *end_tok;
9c06b0b4 10951 int toklen = -1;
bbc13ae3
KS
10952 const char *cond_start = NULL;
10953 const char *cond_end = NULL;
c906108c 10954 enum bptype bp_type;
37e4754d 10955 int thread = -1;
0cf6dd15 10956 int pc = 0;
9c06b0b4
TJB
10957 /* Flag to indicate whether we are going to use masks for
10958 the hardware watchpoint. */
10959 int use_mask = 0;
10960 CORE_ADDR mask = 0;
bbc13ae3
KS
10961 char *expression;
10962 struct cleanup *back_to;
c906108c 10963
37e4754d
LM
10964 /* Make sure that we actually have parameters to parse. */
10965 if (arg != NULL && arg[0] != '\0')
10966 {
bbc13ae3
KS
10967 const char *value_start;
10968
10969 exp_end = arg + strlen (arg);
37e4754d 10970
9c06b0b4
TJB
10971 /* Look for "parameter value" pairs at the end
10972 of the arguments string. */
bbc13ae3 10973 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10974 {
10975 /* Skip whitespace at the end of the argument list. */
10976 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10977 tok--;
10978
10979 /* Find the beginning of the last token.
10980 This is the value of the parameter. */
10981 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10982 tok--;
10983 value_start = tok + 1;
10984
10985 /* Skip whitespace. */
10986 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10987 tok--;
10988
10989 end_tok = tok;
10990
10991 /* Find the beginning of the second to last token.
10992 This is the parameter itself. */
10993 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10994 tok--;
10995 tok++;
10996 toklen = end_tok - tok + 1;
10997
61012eef 10998 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10999 {
5d5658a1 11000 struct thread_info *thr;
9c06b0b4
TJB
11001 /* At this point we've found a "thread" token, which means
11002 the user is trying to set a watchpoint that triggers
11003 only in a specific thread. */
5d5658a1 11004 const char *endp;
37e4754d 11005
9c06b0b4
TJB
11006 if (thread != -1)
11007 error(_("You can specify only one thread."));
37e4754d 11008
9c06b0b4 11009 /* Extract the thread ID from the next token. */
5d5658a1 11010 thr = parse_thread_id (value_start, &endp);
37e4754d 11011
5d5658a1 11012 /* Check if the user provided a valid thread ID. */
9c06b0b4 11013 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11014 invalid_thread_id_error (value_start);
9c06b0b4 11015
5d5658a1 11016 thread = thr->global_num;
9c06b0b4 11017 }
61012eef 11018 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11019 {
11020 /* We've found a "mask" token, which means the user wants to
11021 create a hardware watchpoint that is going to have the mask
11022 facility. */
11023 struct value *mask_value, *mark;
37e4754d 11024
9c06b0b4
TJB
11025 if (use_mask)
11026 error(_("You can specify only one mask."));
37e4754d 11027
9c06b0b4 11028 use_mask = just_location = 1;
37e4754d 11029
9c06b0b4
TJB
11030 mark = value_mark ();
11031 mask_value = parse_to_comma_and_eval (&value_start);
11032 mask = value_as_address (mask_value);
11033 value_free_to_mark (mark);
11034 }
11035 else
11036 /* We didn't recognize what we found. We should stop here. */
11037 break;
37e4754d 11038
9c06b0b4
TJB
11039 /* Truncate the string and get rid of the "parameter value" pair before
11040 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11041 exp_end = tok;
9c06b0b4 11042 }
37e4754d 11043 }
bbc13ae3
KS
11044 else
11045 exp_end = arg;
37e4754d 11046
bbc13ae3
KS
11047 /* Parse the rest of the arguments. From here on out, everything
11048 is in terms of a newly allocated string instead of the original
11049 ARG. */
c906108c 11050 innermost_block = NULL;
bbc13ae3
KS
11051 expression = savestring (arg, exp_end - arg);
11052 back_to = make_cleanup (xfree, expression);
11053 exp_start = arg = expression;
4d01a485 11054 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11055 exp_end = arg;
fa8a61dc
TT
11056 /* Remove trailing whitespace from the expression before saving it.
11057 This makes the eventual display of the expression string a bit
11058 prettier. */
11059 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11060 --exp_end;
11061
65d79d4b 11062 /* Checking if the expression is not constant. */
4d01a485 11063 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11064 {
11065 int len;
11066
11067 len = exp_end - exp_start;
11068 while (len > 0 && isspace (exp_start[len - 1]))
11069 len--;
11070 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11071 }
11072
c906108c
SS
11073 exp_valid_block = innermost_block;
11074 mark = value_mark ();
4d01a485 11075 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11076
bb9d5f81
PP
11077 if (val != NULL && just_location)
11078 {
11079 saved_bitpos = value_bitpos (val);
11080 saved_bitsize = value_bitsize (val);
11081 }
11082
06a64a0b
TT
11083 if (just_location)
11084 {
9c06b0b4
TJB
11085 int ret;
11086
06a64a0b 11087 exp_valid_block = NULL;
a1442452 11088 val = value_addr (result);
06a64a0b
TT
11089 release_value (val);
11090 value_free_to_mark (mark);
9c06b0b4
TJB
11091
11092 if (use_mask)
11093 {
11094 ret = target_masked_watch_num_registers (value_as_address (val),
11095 mask);
11096 if (ret == -1)
11097 error (_("This target does not support masked watchpoints."));
11098 else if (ret == -2)
11099 error (_("Invalid mask or memory region."));
11100 }
06a64a0b
TT
11101 }
11102 else if (val != NULL)
fa4727a6 11103 release_value (val);
c906108c 11104
bbc13ae3
KS
11105 tok = skip_spaces_const (arg);
11106 end_tok = skip_to_space_const (tok);
c906108c
SS
11107
11108 toklen = end_tok - tok;
11109 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11110 {
60e1c644 11111 innermost_block = NULL;
c906108c 11112 tok = cond_start = end_tok + 1;
4d01a485 11113 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11114
11115 /* The watchpoint expression may not be local, but the condition
11116 may still be. E.g.: `watch global if local > 0'. */
11117 cond_exp_valid_block = innermost_block;
11118
c906108c
SS
11119 cond_end = tok;
11120 }
11121 if (*tok)
8a3fe4f8 11122 error (_("Junk at end of command."));
c906108c 11123
441d7c93
PA
11124 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11125
11126 /* Save this because create_internal_breakpoint below invalidates
11127 'wp_frame'. */
11128 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11129
11130 /* If the expression is "local", then set up a "watchpoint scope"
11131 breakpoint at the point where we've left the scope of the watchpoint
11132 expression. Create the scope breakpoint before the watchpoint, so
11133 that we will encounter it first in bpstat_stop_status. */
441d7c93 11134 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11135 {
441d7c93
PA
11136 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11137
11138 if (frame_id_p (caller_frame_id))
edb3359d 11139 {
441d7c93
PA
11140 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11141 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11142
edb3359d 11143 scope_breakpoint
441d7c93 11144 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11145 bp_watchpoint_scope,
11146 &momentary_breakpoint_ops);
d983da9c 11147
441d7c93
PA
11148 /* create_internal_breakpoint could invalidate WP_FRAME. */
11149 wp_frame = NULL;
11150
edb3359d 11151 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11152
edb3359d
DJ
11153 /* Automatically delete the breakpoint when it hits. */
11154 scope_breakpoint->disposition = disp_del;
d983da9c 11155
edb3359d 11156 /* Only break in the proper frame (help with recursion). */
441d7c93 11157 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11158
edb3359d 11159 /* Set the address at which we will stop. */
441d7c93
PA
11160 scope_breakpoint->loc->gdbarch = caller_arch;
11161 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11162 scope_breakpoint->loc->address
a6d9a66e
UW
11163 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11164 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11165 scope_breakpoint->type);
11166 }
d983da9c
DJ
11167 }
11168
e8369a73
AB
11169 /* Now set up the breakpoint. We create all watchpoints as hardware
11170 watchpoints here even if hardware watchpoints are turned off, a call
11171 to update_watchpoint later in this function will cause the type to
11172 drop back to bp_watchpoint (software watchpoint) if required. */
11173
11174 if (accessflag == hw_read)
11175 bp_type = bp_read_watchpoint;
11176 else if (accessflag == hw_access)
11177 bp_type = bp_access_watchpoint;
11178 else
11179 bp_type = bp_hardware_watchpoint;
3a5c3e22 11180
b270e6f9 11181 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 11182
348d480f 11183 if (use_mask)
b270e6f9 11184 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11185 &masked_watchpoint_breakpoint_ops);
348d480f 11186 else
b270e6f9 11187 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11188 &watchpoint_breakpoint_ops);
c1fc2657
SM
11189 w->thread = thread;
11190 w->disposition = disp_donttouch;
11191 w->pspace = current_program_space;
b22e99fd 11192 w->exp = std::move (exp);
3a5c3e22
PA
11193 w->exp_valid_block = exp_valid_block;
11194 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11195 if (just_location)
11196 {
11197 struct type *t = value_type (val);
11198 CORE_ADDR addr = value_as_address (val);
06a64a0b 11199
43cc5389
TT
11200 w->exp_string_reparse
11201 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11202
3a5c3e22 11203 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11204 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11205 }
11206 else
3a5c3e22 11207 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11208
11209 if (use_mask)
11210 {
3a5c3e22 11211 w->hw_wp_mask = mask;
9c06b0b4
TJB
11212 }
11213 else
11214 {
3a5c3e22 11215 w->val = val;
bb9d5f81
PP
11216 w->val_bitpos = saved_bitpos;
11217 w->val_bitsize = saved_bitsize;
3a5c3e22 11218 w->val_valid = 1;
9c06b0b4 11219 }
77b06cd7 11220
c906108c 11221 if (cond_start)
c1fc2657 11222 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 11223 else
c1fc2657 11224 w->cond_string = 0;
c5aa993b 11225
441d7c93 11226 if (frame_id_p (watchpoint_frame))
f6bc2008 11227 {
441d7c93 11228 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11229 w->watchpoint_thread = inferior_ptid;
f6bc2008 11230 }
c906108c 11231 else
f6bc2008 11232 {
3a5c3e22
PA
11233 w->watchpoint_frame = null_frame_id;
11234 w->watchpoint_thread = null_ptid;
f6bc2008 11235 }
c906108c 11236
d983da9c 11237 if (scope_breakpoint != NULL)
c906108c 11238 {
d983da9c
DJ
11239 /* The scope breakpoint is related to the watchpoint. We will
11240 need to act on them together. */
c1fc2657 11241 w->related_breakpoint = scope_breakpoint;
b270e6f9 11242 scope_breakpoint->related_breakpoint = w.get ();
c906108c 11243 }
d983da9c 11244
06a64a0b
TT
11245 if (!just_location)
11246 value_free_to_mark (mark);
2d134ed3 11247
b270e6f9
TT
11248 /* Finally update the new watchpoint. This creates the locations
11249 that should be inserted. */
11250 update_watchpoint (w.get (), 1);
a9634178 11251
b270e6f9 11252 install_breakpoint (internal, std::move (w), 1);
bbc13ae3 11253 do_cleanups (back_to);
c906108c
SS
11254}
11255
e09342b5 11256/* Return count of debug registers needed to watch the given expression.
e09342b5 11257 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11258
c906108c 11259static int
a9634178 11260can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11261{
11262 int found_memory_cnt = 0;
2e70b7b9 11263 struct value *head = v;
c906108c
SS
11264
11265 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11266 if (!can_use_hw_watchpoints)
c906108c 11267 return 0;
c5aa993b 11268
5c44784c
JM
11269 /* Make sure that the value of the expression depends only upon
11270 memory contents, and values computed from them within GDB. If we
11271 find any register references or function calls, we can't use a
11272 hardware watchpoint.
11273
11274 The idea here is that evaluating an expression generates a series
11275 of values, one holding the value of every subexpression. (The
11276 expression a*b+c has five subexpressions: a, b, a*b, c, and
11277 a*b+c.) GDB's values hold almost enough information to establish
11278 the criteria given above --- they identify memory lvalues,
11279 register lvalues, computed values, etcetera. So we can evaluate
11280 the expression, and then scan the chain of values that leaves
11281 behind to decide whether we can detect any possible change to the
11282 expression's final value using only hardware watchpoints.
11283
11284 However, I don't think that the values returned by inferior
11285 function calls are special in any way. So this function may not
11286 notice that an expression involving an inferior function call
11287 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11288 for (; v; v = value_next (v))
c906108c 11289 {
5c44784c 11290 if (VALUE_LVAL (v) == lval_memory)
c906108c 11291 {
8464be76
DJ
11292 if (v != head && value_lazy (v))
11293 /* A lazy memory lvalue in the chain is one that GDB never
11294 needed to fetch; we either just used its address (e.g.,
11295 `a' in `a.b') or we never needed it at all (e.g., `a'
11296 in `a,b'). This doesn't apply to HEAD; if that is
11297 lazy then it was not readable, but watch it anyway. */
5c44784c 11298 ;
53a5351d 11299 else
5c44784c
JM
11300 {
11301 /* Ahh, memory we actually used! Check if we can cover
11302 it with hardware watchpoints. */
df407dfe 11303 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11304
11305 /* We only watch structs and arrays if user asked for it
11306 explicitly, never if they just happen to appear in a
11307 middle of some value chain. */
11308 if (v == head
11309 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11310 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11311 {
42ae5230 11312 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11313 int len;
11314 int num_regs;
11315
a9634178 11316 len = (target_exact_watchpoints
e09342b5
TJB
11317 && is_scalar_type_recursive (vtype))?
11318 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11319
e09342b5
TJB
11320 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11321 if (!num_regs)
2e70b7b9
MS
11322 return 0;
11323 else
e09342b5 11324 found_memory_cnt += num_regs;
2e70b7b9 11325 }
5c44784c 11326 }
c5aa993b 11327 }
5086187c
AC
11328 else if (VALUE_LVAL (v) != not_lval
11329 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11330 return 0; /* These are values from the history (e.g., $1). */
5086187c 11331 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11332 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11333 }
11334
11335 /* The expression itself looks suitable for using a hardware
11336 watchpoint, but give the target machine a chance to reject it. */
11337 return found_memory_cnt;
11338}
11339
8b93c638 11340void
84f4c1fe 11341watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11342{
84f4c1fe 11343 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11344}
11345
06a64a0b
TT
11346/* A helper function that looks for the "-location" argument and then
11347 calls watch_command_1. */
11348
11349static void
11350watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11351{
11352 int just_location = 0;
11353
11354 if (arg
11355 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11356 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11357 {
e9cafbcc 11358 arg = skip_spaces (arg);
06a64a0b
TT
11359 just_location = 1;
11360 }
11361
84f4c1fe 11362 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11363}
8926118c 11364
c5aa993b 11365static void
fba45db2 11366watch_command (char *arg, int from_tty)
c906108c 11367{
06a64a0b 11368 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11369}
11370
8b93c638 11371void
84f4c1fe 11372rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11373{
84f4c1fe 11374 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11375}
8926118c 11376
c5aa993b 11377static void
fba45db2 11378rwatch_command (char *arg, int from_tty)
c906108c 11379{
06a64a0b 11380 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11381}
11382
8b93c638 11383void
84f4c1fe 11384awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11385{
84f4c1fe 11386 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11387}
8926118c 11388
c5aa993b 11389static void
fba45db2 11390awatch_command (char *arg, int from_tty)
c906108c 11391{
06a64a0b 11392 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11393}
c906108c 11394\f
c5aa993b 11395
cfc31633
PA
11396/* Data for the FSM that manages the until(location)/advance commands
11397 in infcmd.c. Here because it uses the mechanisms of
11398 breakpoints. */
c906108c 11399
cfc31633 11400struct until_break_fsm
bfec99b2 11401{
cfc31633
PA
11402 /* The base class. */
11403 struct thread_fsm thread_fsm;
11404
11405 /* The thread that as current when the command was executed. */
11406 int thread;
11407
11408 /* The breakpoint set at the destination location. */
11409 struct breakpoint *location_breakpoint;
11410
11411 /* Breakpoint set at the return address in the caller frame. May be
11412 NULL. */
11413 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11414};
11415
8980e177
PA
11416static void until_break_fsm_clean_up (struct thread_fsm *self,
11417 struct thread_info *thread);
11418static int until_break_fsm_should_stop (struct thread_fsm *self,
11419 struct thread_info *thread);
cfc31633
PA
11420static enum async_reply_reason
11421 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11422
11423/* until_break_fsm's vtable. */
11424
11425static struct thread_fsm_ops until_break_fsm_ops =
11426{
11427 NULL, /* dtor */
11428 until_break_fsm_clean_up,
11429 until_break_fsm_should_stop,
11430 NULL, /* return_value */
11431 until_break_fsm_async_reply_reason,
11432};
11433
11434/* Allocate a new until_break_command_fsm. */
11435
11436static struct until_break_fsm *
8980e177 11437new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11438 struct breakpoint *location_breakpoint,
11439 struct breakpoint *caller_breakpoint)
11440{
11441 struct until_break_fsm *sm;
11442
11443 sm = XCNEW (struct until_break_fsm);
8980e177 11444 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11445
11446 sm->thread = thread;
11447 sm->location_breakpoint = location_breakpoint;
11448 sm->caller_breakpoint = caller_breakpoint;
11449
11450 return sm;
11451}
11452
11453/* Implementation of the 'should_stop' FSM method for the
11454 until(location)/advance commands. */
11455
11456static int
8980e177
PA
11457until_break_fsm_should_stop (struct thread_fsm *self,
11458 struct thread_info *tp)
cfc31633
PA
11459{
11460 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11461
11462 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11463 sm->location_breakpoint) != NULL
11464 || (sm->caller_breakpoint != NULL
11465 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11466 sm->caller_breakpoint) != NULL))
11467 thread_fsm_set_finished (self);
11468
11469 return 1;
11470}
11471
11472/* Implementation of the 'clean_up' FSM method for the
11473 until(location)/advance commands. */
11474
c2c6d25f 11475static void
8980e177
PA
11476until_break_fsm_clean_up (struct thread_fsm *self,
11477 struct thread_info *thread)
43ff13b4 11478{
cfc31633 11479 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11480
cfc31633
PA
11481 /* Clean up our temporary breakpoints. */
11482 if (sm->location_breakpoint != NULL)
11483 {
11484 delete_breakpoint (sm->location_breakpoint);
11485 sm->location_breakpoint = NULL;
11486 }
11487 if (sm->caller_breakpoint != NULL)
11488 {
11489 delete_breakpoint (sm->caller_breakpoint);
11490 sm->caller_breakpoint = NULL;
11491 }
11492 delete_longjmp_breakpoint (sm->thread);
11493}
11494
11495/* Implementation of the 'async_reply_reason' FSM method for the
11496 until(location)/advance commands. */
11497
11498static enum async_reply_reason
11499until_break_fsm_async_reply_reason (struct thread_fsm *self)
11500{
11501 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11502}
11503
c906108c 11504void
ae66c1fc 11505until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11506{
11507 struct symtabs_and_lines sals;
11508 struct symtab_and_line sal;
8556afb4
PA
11509 struct frame_info *frame;
11510 struct gdbarch *frame_gdbarch;
11511 struct frame_id stack_frame_id;
11512 struct frame_id caller_frame_id;
cfc31633
PA
11513 struct breakpoint *location_breakpoint;
11514 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11515 struct cleanup *old_chain;
186c406b
TT
11516 int thread;
11517 struct thread_info *tp;
cfc31633 11518 struct until_break_fsm *sm;
c906108c 11519
70509625 11520 clear_proceed_status (0);
c906108c
SS
11521
11522 /* Set a breakpoint where the user wants it and at return from
4a64f543 11523 this function. */
c5aa993b 11524
ffc2605c 11525 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11526
1bfeeb0f 11527 if (last_displayed_sal_is_valid ())
ffc2605c 11528 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11529 get_last_displayed_symtab (),
f8eba3c6 11530 get_last_displayed_line ());
c906108c 11531 else
ffc2605c 11532 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11533 NULL, (struct symtab *) NULL, 0);
c5aa993b 11534
c906108c 11535 if (sals.nelts != 1)
8a3fe4f8 11536 error (_("Couldn't get information on specified line."));
c5aa993b 11537
c906108c 11538 sal = sals.sals[0];
4a64f543 11539 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11540
c906108c 11541 if (*arg)
8a3fe4f8 11542 error (_("Junk at end of arguments."));
c5aa993b 11543
c906108c 11544 resolve_sal_pc (&sal);
c5aa993b 11545
186c406b 11546 tp = inferior_thread ();
5d5658a1 11547 thread = tp->global_num;
186c406b 11548
883bc8d1
PA
11549 old_chain = make_cleanup (null_cleanup, NULL);
11550
8556afb4
PA
11551 /* Note linespec handling above invalidates the frame chain.
11552 Installing a breakpoint also invalidates the frame chain (as it
11553 may need to switch threads), so do any frame handling before
11554 that. */
11555
11556 frame = get_selected_frame (NULL);
11557 frame_gdbarch = get_frame_arch (frame);
11558 stack_frame_id = get_stack_frame_id (frame);
11559 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11560
ae66c1fc
EZ
11561 /* Keep within the current frame, or in frames called by the current
11562 one. */
edb3359d 11563
883bc8d1 11564 if (frame_id_p (caller_frame_id))
c906108c 11565 {
883bc8d1 11566 struct symtab_and_line sal2;
cfc31633 11567 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11568
11569 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11570 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11571 caller_gdbarch = frame_unwind_caller_arch (frame);
11572 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11573 sal2,
11574 caller_frame_id,
11575 bp_until);
11576 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11577
883bc8d1 11578 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11579 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11580 }
c5aa993b 11581
c70a6932
JK
11582 /* set_momentary_breakpoint could invalidate FRAME. */
11583 frame = NULL;
11584
883bc8d1
PA
11585 if (anywhere)
11586 /* If the user told us to continue until a specified location,
11587 we don't specify a frame at which we need to stop. */
cfc31633
PA
11588 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11589 null_frame_id, bp_until);
883bc8d1
PA
11590 else
11591 /* Otherwise, specify the selected frame, because we want to stop
11592 only at the very same frame. */
cfc31633
PA
11593 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11594 stack_frame_id, bp_until);
11595 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11596
8980e177 11597 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11598 location_breakpoint, caller_breakpoint);
cfc31633 11599 tp->thread_fsm = &sm->thread_fsm;
f107f563 11600
cfc31633 11601 discard_cleanups (old_chain);
f107f563 11602
cfc31633 11603 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11604}
ae66c1fc 11605
c906108c
SS
11606/* This function attempts to parse an optional "if <cond>" clause
11607 from the arg string. If one is not found, it returns NULL.
c5aa993b 11608
c906108c
SS
11609 Else, it returns a pointer to the condition string. (It does not
11610 attempt to evaluate the string against a particular block.) And,
11611 it updates arg to point to the first character following the parsed
4a64f543 11612 if clause in the arg string. */
53a5351d 11613
63160a43
PA
11614const char *
11615ep_parse_optional_if_clause (const char **arg)
c906108c 11616{
63160a43 11617 const char *cond_string;
c5aa993b
JM
11618
11619 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11620 return NULL;
c5aa993b 11621
4a64f543 11622 /* Skip the "if" keyword. */
c906108c 11623 (*arg) += 2;
c5aa993b 11624
c906108c 11625 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11626 condition string. */
63160a43 11627 *arg = skip_spaces_const (*arg);
c906108c 11628 cond_string = *arg;
c5aa993b 11629
4a64f543
MS
11630 /* Assume that the condition occupies the remainder of the arg
11631 string. */
c906108c 11632 (*arg) += strlen (cond_string);
c5aa993b 11633
c906108c
SS
11634 return cond_string;
11635}
c5aa993b 11636
c906108c
SS
11637/* Commands to deal with catching events, such as signals, exceptions,
11638 process start/exit, etc. */
c5aa993b
JM
11639
11640typedef enum
11641{
44feb3ce
TT
11642 catch_fork_temporary, catch_vfork_temporary,
11643 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11644}
11645catch_fork_kind;
11646
c906108c 11647static void
63160a43 11648catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11649 struct cmd_list_element *command)
c906108c 11650{
63160a43 11651 const char *arg = arg_entry;
a6d9a66e 11652 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11653 const char *cond_string = NULL;
44feb3ce
TT
11654 catch_fork_kind fork_kind;
11655 int tempflag;
11656
11657 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11658 tempflag = (fork_kind == catch_fork_temporary
11659 || fork_kind == catch_vfork_temporary);
c5aa993b 11660
44feb3ce
TT
11661 if (!arg)
11662 arg = "";
63160a43 11663 arg = skip_spaces_const (arg);
c5aa993b 11664
c906108c 11665 /* The allowed syntax is:
c5aa993b
JM
11666 catch [v]fork
11667 catch [v]fork if <cond>
11668
4a64f543 11669 First, check if there's an if clause. */
c906108c 11670 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11671
c906108c 11672 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11673 error (_("Junk at end of arguments."));
c5aa993b 11674
c906108c 11675 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11676 and enable reporting of such events. */
c5aa993b
JM
11677 switch (fork_kind)
11678 {
44feb3ce
TT
11679 case catch_fork_temporary:
11680 case catch_fork_permanent:
a6d9a66e 11681 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11682 &catch_fork_breakpoint_ops);
c906108c 11683 break;
44feb3ce
TT
11684 case catch_vfork_temporary:
11685 case catch_vfork_permanent:
a6d9a66e 11686 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11687 &catch_vfork_breakpoint_ops);
c906108c 11688 break;
c5aa993b 11689 default:
8a3fe4f8 11690 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11691 break;
c5aa993b 11692 }
c906108c
SS
11693}
11694
11695static void
63160a43 11696catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11697 struct cmd_list_element *command)
c906108c 11698{
63160a43 11699 const char *arg = arg_entry;
a6d9a66e 11700 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11701 int tempflag;
63160a43 11702 const char *cond_string = NULL;
c906108c 11703
44feb3ce
TT
11704 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11705
11706 if (!arg)
11707 arg = "";
63160a43 11708 arg = skip_spaces_const (arg);
c906108c
SS
11709
11710 /* The allowed syntax is:
c5aa993b
JM
11711 catch exec
11712 catch exec if <cond>
c906108c 11713
4a64f543 11714 First, check if there's an if clause. */
c906108c
SS
11715 cond_string = ep_parse_optional_if_clause (&arg);
11716
11717 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11718 error (_("Junk at end of arguments."));
c906108c 11719
b270e6f9
TT
11720 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11721 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11722 &catch_exec_breakpoint_ops);
11723 c->exec_pathname = NULL;
11724
b270e6f9 11725 install_breakpoint (0, std::move (c), 1);
c906108c 11726}
c5aa993b 11727
9ac4176b 11728void
28010a5d
PA
11729init_ada_exception_breakpoint (struct breakpoint *b,
11730 struct gdbarch *gdbarch,
11731 struct symtab_and_line sal,
11732 char *addr_string,
c0a91b2b 11733 const struct breakpoint_ops *ops,
28010a5d 11734 int tempflag,
349774ef 11735 int enabled,
28010a5d 11736 int from_tty)
f7f9143b 11737{
f7f9143b
JB
11738 if (from_tty)
11739 {
5af949e3
UW
11740 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11741 if (!loc_gdbarch)
11742 loc_gdbarch = gdbarch;
11743
6c95b8df
PA
11744 describe_other_breakpoints (loc_gdbarch,
11745 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11746 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11747 version for exception catchpoints, because two catchpoints
11748 used for different exception names will use the same address.
11749 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11750 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11751 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11752 the user what type of catchpoint it is. The above is good
11753 enough for now, though. */
11754 }
11755
28010a5d 11756 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11757
349774ef 11758 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11759 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11760 b->location = string_to_event_location (&addr_string,
11761 language_def (language_ada));
f7f9143b 11762 b->language = language_ada;
f7f9143b
JB
11763}
11764
c906108c 11765static void
fba45db2 11766catch_command (char *arg, int from_tty)
c906108c 11767{
44feb3ce 11768 error (_("Catch requires an event name."));
c906108c
SS
11769}
11770\f
11771
11772static void
fba45db2 11773tcatch_command (char *arg, int from_tty)
c906108c 11774{
44feb3ce 11775 error (_("Catch requires an event name."));
c906108c
SS
11776}
11777
8a2c437b
TT
11778/* A qsort comparison function that sorts breakpoints in order. */
11779
11780static int
11781compare_breakpoints (const void *a, const void *b)
11782{
9a3c8263 11783 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11784 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11785 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11786 uintptr_t ub = (uintptr_t) *bb;
11787
11788 if ((*ba)->number < (*bb)->number)
11789 return -1;
11790 else if ((*ba)->number > (*bb)->number)
11791 return 1;
11792
11793 /* Now sort by address, in case we see, e..g, two breakpoints with
11794 the number 0. */
11795 if (ua < ub)
11796 return -1;
94b0e70d 11797 return ua > ub ? 1 : 0;
8a2c437b
TT
11798}
11799
80f8a6eb 11800/* Delete breakpoints by address or line. */
c906108c
SS
11801
11802static void
fba45db2 11803clear_command (char *arg, int from_tty)
c906108c 11804{
8a2c437b 11805 struct breakpoint *b, *prev;
d6e956e5
VP
11806 VEC(breakpoint_p) *found = 0;
11807 int ix;
c906108c
SS
11808 int default_match;
11809 struct symtabs_and_lines sals;
11810 struct symtab_and_line sal;
c906108c 11811 int i;
8a2c437b 11812 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11813
11814 if (arg)
11815 {
39cf75f7
DE
11816 sals = decode_line_with_current_source (arg,
11817 (DECODE_LINE_FUNFIRSTLINE
11818 | DECODE_LINE_LIST_MODE));
cf4ded82 11819 make_cleanup (xfree, sals.sals);
c906108c
SS
11820 default_match = 0;
11821 }
11822 else
11823 {
8d749320 11824 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11825 make_cleanup (xfree, sals.sals);
4a64f543 11826 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11827
11828 /* Set sal's line, symtab, pc, and pspace to the values
11829 corresponding to the last call to print_frame_info. If the
11830 codepoint is not valid, this will set all the fields to 0. */
11831 get_last_displayed_sal (&sal);
c906108c 11832 if (sal.symtab == 0)
8a3fe4f8 11833 error (_("No source file specified."));
c906108c
SS
11834
11835 sals.sals[0] = sal;
11836 sals.nelts = 1;
11837
11838 default_match = 1;
11839 }
11840
4a64f543
MS
11841 /* We don't call resolve_sal_pc here. That's not as bad as it
11842 seems, because all existing breakpoints typically have both
11843 file/line and pc set. So, if clear is given file/line, we can
11844 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11845
11846 We only support clearing given the address explicitly
11847 present in breakpoint table. Say, we've set breakpoint
4a64f543 11848 at file:line. There were several PC values for that file:line,
ed0616c6 11849 due to optimization, all in one block.
4a64f543
MS
11850
11851 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11852 PC corresponding to the same file:line, the breakpoint won't
11853 be cleared. We probably can still clear the breakpoint, but
11854 since the other PC value is never presented to user, user
11855 can only find it by guessing, and it does not seem important
11856 to support that. */
11857
4a64f543
MS
11858 /* For each line spec given, delete bps which correspond to it. Do
11859 it in two passes, solely to preserve the current behavior that
11860 from_tty is forced true if we delete more than one
11861 breakpoint. */
c906108c 11862
80f8a6eb 11863 found = NULL;
8a2c437b 11864 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11865 for (i = 0; i < sals.nelts; i++)
11866 {
05cba821
JK
11867 const char *sal_fullname;
11868
c906108c 11869 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11870 If line given (pc == 0), clear all bpts on specified line.
11871 If defaulting, clear all bpts on default line
c906108c 11872 or at default pc.
c5aa993b
JM
11873
11874 defaulting sal.pc != 0 tests to do
11875
11876 0 1 pc
11877 1 1 pc _and_ line
11878 0 0 line
11879 1 0 <can't happen> */
c906108c
SS
11880
11881 sal = sals.sals[i];
05cba821
JK
11882 sal_fullname = (sal.symtab == NULL
11883 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11884
4a64f543 11885 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11886 ALL_BREAKPOINTS (b)
c5aa993b 11887 {
0d381245 11888 int match = 0;
4a64f543 11889 /* Are we going to delete b? */
cc60f2e3 11890 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11891 {
11892 struct bp_location *loc = b->loc;
11893 for (; loc; loc = loc->next)
11894 {
f8eba3c6
TT
11895 /* If the user specified file:line, don't allow a PC
11896 match. This matches historical gdb behavior. */
11897 int pc_match = (!sal.explicit_line
11898 && sal.pc
11899 && (loc->pspace == sal.pspace)
11900 && (loc->address == sal.pc)
11901 && (!section_is_overlay (loc->section)
11902 || loc->section == sal.section));
4aac40c8
TT
11903 int line_match = 0;
11904
11905 if ((default_match || sal.explicit_line)
2f202fde 11906 && loc->symtab != NULL
05cba821 11907 && sal_fullname != NULL
4aac40c8 11908 && sal.pspace == loc->pspace
05cba821
JK
11909 && loc->line_number == sal.line
11910 && filename_cmp (symtab_to_fullname (loc->symtab),
11911 sal_fullname) == 0)
11912 line_match = 1;
4aac40c8 11913
0d381245
VP
11914 if (pc_match || line_match)
11915 {
11916 match = 1;
11917 break;
11918 }
11919 }
11920 }
11921
11922 if (match)
d6e956e5 11923 VEC_safe_push(breakpoint_p, found, b);
c906108c 11924 }
80f8a6eb 11925 }
8a2c437b 11926
80f8a6eb 11927 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11928 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11929 {
11930 if (arg)
8a3fe4f8 11931 error (_("No breakpoint at %s."), arg);
80f8a6eb 11932 else
8a3fe4f8 11933 error (_("No breakpoint at this line."));
80f8a6eb 11934 }
c906108c 11935
8a2c437b
TT
11936 /* Remove duplicates from the vec. */
11937 qsort (VEC_address (breakpoint_p, found),
11938 VEC_length (breakpoint_p, found),
11939 sizeof (breakpoint_p),
11940 compare_breakpoints);
11941 prev = VEC_index (breakpoint_p, found, 0);
11942 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11943 {
11944 if (b == prev)
11945 {
11946 VEC_ordered_remove (breakpoint_p, found, ix);
11947 --ix;
11948 }
11949 }
11950
d6e956e5 11951 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11952 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11953 if (from_tty)
a3f17187 11954 {
d6e956e5 11955 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11956 printf_unfiltered (_("Deleted breakpoint "));
11957 else
11958 printf_unfiltered (_("Deleted breakpoints "));
11959 }
d6e956e5
VP
11960
11961 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11962 {
c5aa993b 11963 if (from_tty)
d6e956e5
VP
11964 printf_unfiltered ("%d ", b->number);
11965 delete_breakpoint (b);
c906108c 11966 }
80f8a6eb
MS
11967 if (from_tty)
11968 putchar_unfiltered ('\n');
8a2c437b
TT
11969
11970 do_cleanups (cleanups);
c906108c
SS
11971}
11972\f
11973/* Delete breakpoint in BS if they are `delete' breakpoints and
11974 all breakpoints that are marked for deletion, whether hit or not.
11975 This is called after any breakpoint is hit, or after errors. */
11976
11977void
fba45db2 11978breakpoint_auto_delete (bpstat bs)
c906108c 11979{
35df4500 11980 struct breakpoint *b, *b_tmp;
c906108c
SS
11981
11982 for (; bs; bs = bs->next)
f431efe5
PA
11983 if (bs->breakpoint_at
11984 && bs->breakpoint_at->disposition == disp_del
c906108c 11985 && bs->stop)
f431efe5 11986 delete_breakpoint (bs->breakpoint_at);
c906108c 11987
35df4500 11988 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11989 {
b5de0fa7 11990 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11991 delete_breakpoint (b);
11992 }
c906108c
SS
11993}
11994
4a64f543
MS
11995/* A comparison function for bp_location AP and BP being interfaced to
11996 qsort. Sort elements primarily by their ADDRESS (no matter what
11997 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11998 secondarily by ordering first permanent elements and
4a64f543 11999 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12000 qsort being an unstable algorithm. */
876fa593
JK
12001
12002static int
f5336ca5 12003bp_locations_compare (const void *ap, const void *bp)
876fa593 12004{
9a3c8263
SM
12005 const struct bp_location *a = *(const struct bp_location **) ap;
12006 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12007
12008 if (a->address != b->address)
12009 return (a->address > b->address) - (a->address < b->address);
12010
dea2aa5f
LM
12011 /* Sort locations at the same address by their pspace number, keeping
12012 locations of the same inferior (in a multi-inferior environment)
12013 grouped. */
12014
12015 if (a->pspace->num != b->pspace->num)
12016 return ((a->pspace->num > b->pspace->num)
12017 - (a->pspace->num < b->pspace->num));
12018
876fa593 12019 /* Sort permanent breakpoints first. */
1a853c52
PA
12020 if (a->permanent != b->permanent)
12021 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12022
c56a97f9
JK
12023 /* Make the internal GDB representation stable across GDB runs
12024 where A and B memory inside GDB can differ. Breakpoint locations of
12025 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12026
12027 if (a->owner->number != b->owner->number)
c56a97f9
JK
12028 return ((a->owner->number > b->owner->number)
12029 - (a->owner->number < b->owner->number));
876fa593
JK
12030
12031 return (a > b) - (a < b);
12032}
12033
f5336ca5
PA
12034/* Set bp_locations_placed_address_before_address_max and
12035 bp_locations_shadow_len_after_address_max according to the current
12036 content of the bp_locations array. */
f7545552
TT
12037
12038static void
f5336ca5 12039bp_locations_target_extensions_update (void)
f7545552 12040{
876fa593
JK
12041 struct bp_location *bl, **blp_tmp;
12042
f5336ca5
PA
12043 bp_locations_placed_address_before_address_max = 0;
12044 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12045
12046 ALL_BP_LOCATIONS (bl, blp_tmp)
12047 {
12048 CORE_ADDR start, end, addr;
12049
12050 if (!bp_location_has_shadow (bl))
12051 continue;
12052
12053 start = bl->target_info.placed_address;
12054 end = start + bl->target_info.shadow_len;
12055
12056 gdb_assert (bl->address >= start);
12057 addr = bl->address - start;
f5336ca5
PA
12058 if (addr > bp_locations_placed_address_before_address_max)
12059 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12060
12061 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12062
12063 gdb_assert (bl->address < end);
12064 addr = end - bl->address;
f5336ca5
PA
12065 if (addr > bp_locations_shadow_len_after_address_max)
12066 bp_locations_shadow_len_after_address_max = addr;
876fa593 12067 }
f7545552
TT
12068}
12069
1e4d1764
YQ
12070/* Download tracepoint locations if they haven't been. */
12071
12072static void
12073download_tracepoint_locations (void)
12074{
7ed2c994 12075 struct breakpoint *b;
dd2e65cc 12076 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 12077
5ed8105e 12078 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 12079
7ed2c994 12080 ALL_TRACEPOINTS (b)
1e4d1764 12081 {
7ed2c994 12082 struct bp_location *bl;
1e4d1764 12083 struct tracepoint *t;
f2a8bc8a 12084 int bp_location_downloaded = 0;
1e4d1764 12085
7ed2c994 12086 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12087 ? !may_insert_fast_tracepoints
12088 : !may_insert_tracepoints))
12089 continue;
12090
dd2e65cc
YQ
12091 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12092 {
12093 if (target_can_download_tracepoint ())
12094 can_download_tracepoint = TRIBOOL_TRUE;
12095 else
12096 can_download_tracepoint = TRIBOOL_FALSE;
12097 }
12098
12099 if (can_download_tracepoint == TRIBOOL_FALSE)
12100 break;
12101
7ed2c994
YQ
12102 for (bl = b->loc; bl; bl = bl->next)
12103 {
12104 /* In tracepoint, locations are _never_ duplicated, so
12105 should_be_inserted is equivalent to
12106 unduplicated_should_be_inserted. */
12107 if (!should_be_inserted (bl) || bl->inserted)
12108 continue;
1e4d1764 12109
7ed2c994 12110 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12111
7ed2c994 12112 target_download_tracepoint (bl);
1e4d1764 12113
7ed2c994 12114 bl->inserted = 1;
f2a8bc8a 12115 bp_location_downloaded = 1;
7ed2c994
YQ
12116 }
12117 t = (struct tracepoint *) b;
12118 t->number_on_target = b->number;
f2a8bc8a
YQ
12119 if (bp_location_downloaded)
12120 observer_notify_breakpoint_modified (b);
1e4d1764 12121 }
1e4d1764
YQ
12122}
12123
934709f0
PW
12124/* Swap the insertion/duplication state between two locations. */
12125
12126static void
12127swap_insertion (struct bp_location *left, struct bp_location *right)
12128{
12129 const int left_inserted = left->inserted;
12130 const int left_duplicate = left->duplicate;
b775012e 12131 const int left_needs_update = left->needs_update;
934709f0
PW
12132 const struct bp_target_info left_target_info = left->target_info;
12133
1e4d1764
YQ
12134 /* Locations of tracepoints can never be duplicated. */
12135 if (is_tracepoint (left->owner))
12136 gdb_assert (!left->duplicate);
12137 if (is_tracepoint (right->owner))
12138 gdb_assert (!right->duplicate);
12139
934709f0
PW
12140 left->inserted = right->inserted;
12141 left->duplicate = right->duplicate;
b775012e 12142 left->needs_update = right->needs_update;
934709f0
PW
12143 left->target_info = right->target_info;
12144 right->inserted = left_inserted;
12145 right->duplicate = left_duplicate;
b775012e 12146 right->needs_update = left_needs_update;
934709f0
PW
12147 right->target_info = left_target_info;
12148}
12149
b775012e
LM
12150/* Force the re-insertion of the locations at ADDRESS. This is called
12151 once a new/deleted/modified duplicate location is found and we are evaluating
12152 conditions on the target's side. Such conditions need to be updated on
12153 the target. */
12154
12155static void
12156force_breakpoint_reinsertion (struct bp_location *bl)
12157{
12158 struct bp_location **locp = NULL, **loc2p;
12159 struct bp_location *loc;
12160 CORE_ADDR address = 0;
12161 int pspace_num;
12162
12163 address = bl->address;
12164 pspace_num = bl->pspace->num;
12165
12166 /* This is only meaningful if the target is
12167 evaluating conditions and if the user has
12168 opted for condition evaluation on the target's
12169 side. */
12170 if (gdb_evaluates_breakpoint_condition_p ()
12171 || !target_supports_evaluation_of_breakpoint_conditions ())
12172 return;
12173
12174 /* Flag all breakpoint locations with this address and
12175 the same program space as the location
12176 as "its condition has changed". We need to
12177 update the conditions on the target's side. */
12178 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12179 {
12180 loc = *loc2p;
12181
12182 if (!is_breakpoint (loc->owner)
12183 || pspace_num != loc->pspace->num)
12184 continue;
12185
12186 /* Flag the location appropriately. We use a different state to
12187 let everyone know that we already updated the set of locations
12188 with addr bl->address and program space bl->pspace. This is so
12189 we don't have to keep calling these functions just to mark locations
12190 that have already been marked. */
12191 loc->condition_changed = condition_updated;
12192
12193 /* Free the agent expression bytecode as well. We will compute
12194 it later on. */
833177a4 12195 loc->cond_bytecode.reset ();
b775012e
LM
12196 }
12197}
44702360
PA
12198/* Called whether new breakpoints are created, or existing breakpoints
12199 deleted, to update the global location list and recompute which
12200 locations are duplicate of which.
b775012e 12201
04086b45
PA
12202 The INSERT_MODE flag determines whether locations may not, may, or
12203 shall be inserted now. See 'enum ugll_insert_mode' for more
12204 info. */
b60e7edf 12205
0d381245 12206static void
44702360 12207update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12208{
74960c60 12209 struct breakpoint *b;
876fa593 12210 struct bp_location **locp, *loc;
f7545552 12211 struct cleanup *cleanups;
b775012e
LM
12212 /* Last breakpoint location address that was marked for update. */
12213 CORE_ADDR last_addr = 0;
12214 /* Last breakpoint location program space that was marked for update. */
12215 int last_pspace_num = -1;
f7545552 12216
2d134ed3
PA
12217 /* Used in the duplicates detection below. When iterating over all
12218 bp_locations, points to the first bp_location of a given address.
12219 Breakpoints and watchpoints of different types are never
12220 duplicates of each other. Keep one pointer for each type of
12221 breakpoint/watchpoint, so we only need to loop over all locations
12222 once. */
12223 struct bp_location *bp_loc_first; /* breakpoint */
12224 struct bp_location *wp_loc_first; /* hardware watchpoint */
12225 struct bp_location *awp_loc_first; /* access watchpoint */
12226 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12227
f5336ca5
PA
12228 /* Saved former bp_locations array which we compare against the newly
12229 built bp_locations from the current state of ALL_BREAKPOINTS. */
12230 struct bp_location **old_locations, **old_locp;
12231 unsigned old_locations_count;
876fa593 12232
f5336ca5
PA
12233 old_locations = bp_locations;
12234 old_locations_count = bp_locations_count;
12235 bp_locations = NULL;
12236 bp_locations_count = 0;
12237 cleanups = make_cleanup (xfree, old_locations);
0d381245 12238
74960c60 12239 ALL_BREAKPOINTS (b)
876fa593 12240 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12241 bp_locations_count++;
876fa593 12242
f5336ca5
PA
12243 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12244 locp = bp_locations;
876fa593
JK
12245 ALL_BREAKPOINTS (b)
12246 for (loc = b->loc; loc; loc = loc->next)
12247 *locp++ = loc;
f5336ca5
PA
12248 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12249 bp_locations_compare);
876fa593 12250
f5336ca5 12251 bp_locations_target_extensions_update ();
74960c60 12252
4a64f543
MS
12253 /* Identify bp_location instances that are no longer present in the
12254 new list, and therefore should be freed. Note that it's not
12255 necessary that those locations should be removed from inferior --
12256 if there's another location at the same address (previously
12257 marked as duplicate), we don't need to remove/insert the
12258 location.
876fa593 12259
4a64f543
MS
12260 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12261 and former bp_location array state respectively. */
876fa593 12262
f5336ca5
PA
12263 locp = bp_locations;
12264 for (old_locp = old_locations;
12265 old_locp < old_locations + old_locations_count;
876fa593 12266 old_locp++)
74960c60 12267 {
876fa593 12268 struct bp_location *old_loc = *old_locp;
c7d46a38 12269 struct bp_location **loc2p;
876fa593 12270
e5dd4106 12271 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12272 not, we have to free it. */
c7d46a38 12273 int found_object = 0;
20874c92
VP
12274 /* Tells if the location should remain inserted in the target. */
12275 int keep_in_target = 0;
12276 int removed = 0;
876fa593 12277
4a64f543
MS
12278 /* Skip LOCP entries which will definitely never be needed.
12279 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12280 while (locp < bp_locations + bp_locations_count
c7d46a38 12281 && (*locp)->address < old_loc->address)
876fa593 12282 locp++;
c7d46a38
PA
12283
12284 for (loc2p = locp;
f5336ca5 12285 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12286 && (*loc2p)->address == old_loc->address);
12287 loc2p++)
12288 {
b775012e
LM
12289 /* Check if this is a new/duplicated location or a duplicated
12290 location that had its condition modified. If so, we want to send
12291 its condition to the target if evaluation of conditions is taking
12292 place there. */
12293 if ((*loc2p)->condition_changed == condition_modified
12294 && (last_addr != old_loc->address
12295 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12296 {
b775012e
LM
12297 force_breakpoint_reinsertion (*loc2p);
12298 last_pspace_num = old_loc->pspace->num;
c7d46a38 12299 }
b775012e
LM
12300
12301 if (*loc2p == old_loc)
12302 found_object = 1;
c7d46a38 12303 }
74960c60 12304
b775012e
LM
12305 /* We have already handled this address, update it so that we don't
12306 have to go through updates again. */
12307 last_addr = old_loc->address;
12308
12309 /* Target-side condition evaluation: Handle deleted locations. */
12310 if (!found_object)
12311 force_breakpoint_reinsertion (old_loc);
12312
4a64f543
MS
12313 /* If this location is no longer present, and inserted, look if
12314 there's maybe a new location at the same address. If so,
12315 mark that one inserted, and don't remove this one. This is
12316 needed so that we don't have a time window where a breakpoint
12317 at certain location is not inserted. */
74960c60 12318
876fa593 12319 if (old_loc->inserted)
0d381245 12320 {
4a64f543
MS
12321 /* If the location is inserted now, we might have to remove
12322 it. */
74960c60 12323
876fa593 12324 if (found_object && should_be_inserted (old_loc))
74960c60 12325 {
4a64f543
MS
12326 /* The location is still present in the location list,
12327 and still should be inserted. Don't do anything. */
20874c92 12328 keep_in_target = 1;
74960c60
VP
12329 }
12330 else
12331 {
b775012e
LM
12332 /* This location still exists, but it won't be kept in the
12333 target since it may have been disabled. We proceed to
12334 remove its target-side condition. */
12335
4a64f543
MS
12336 /* The location is either no longer present, or got
12337 disabled. See if there's another location at the
12338 same address, in which case we don't need to remove
12339 this one from the target. */
876fa593 12340
2bdf28a0 12341 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12342 if (breakpoint_address_is_meaningful (old_loc->owner))
12343 {
876fa593 12344 for (loc2p = locp;
f5336ca5 12345 (loc2p < bp_locations + bp_locations_count
c7d46a38 12346 && (*loc2p)->address == old_loc->address);
876fa593
JK
12347 loc2p++)
12348 {
12349 struct bp_location *loc2 = *loc2p;
12350
2d134ed3 12351 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12352 {
85d721b8
PA
12353 /* Read watchpoint locations are switched to
12354 access watchpoints, if the former are not
12355 supported, but the latter are. */
12356 if (is_hardware_watchpoint (old_loc->owner))
12357 {
12358 gdb_assert (is_hardware_watchpoint (loc2->owner));
12359 loc2->watchpoint_type = old_loc->watchpoint_type;
12360 }
12361
934709f0
PW
12362 /* loc2 is a duplicated location. We need to check
12363 if it should be inserted in case it will be
12364 unduplicated. */
12365 if (loc2 != old_loc
12366 && unduplicated_should_be_inserted (loc2))
c7d46a38 12367 {
934709f0 12368 swap_insertion (old_loc, loc2);
c7d46a38
PA
12369 keep_in_target = 1;
12370 break;
12371 }
876fa593
JK
12372 }
12373 }
12374 }
74960c60
VP
12375 }
12376
20874c92
VP
12377 if (!keep_in_target)
12378 {
834c0d03 12379 if (remove_breakpoint (old_loc))
20874c92 12380 {
4a64f543
MS
12381 /* This is just about all we can do. We could keep
12382 this location on the global list, and try to
12383 remove it next time, but there's no particular
12384 reason why we will succeed next time.
20874c92 12385
4a64f543
MS
12386 Note that at this point, old_loc->owner is still
12387 valid, as delete_breakpoint frees the breakpoint
12388 only after calling us. */
3e43a32a
MS
12389 printf_filtered (_("warning: Error removing "
12390 "breakpoint %d\n"),
876fa593 12391 old_loc->owner->number);
20874c92
VP
12392 }
12393 removed = 1;
12394 }
0d381245 12395 }
74960c60
VP
12396
12397 if (!found_object)
1c5cfe86 12398 {
fbea99ea 12399 if (removed && target_is_non_stop_p ()
1cf4d951 12400 && need_moribund_for_location_type (old_loc))
20874c92 12401 {
db82e815
PA
12402 /* This location was removed from the target. In
12403 non-stop mode, a race condition is possible where
12404 we've removed a breakpoint, but stop events for that
12405 breakpoint are already queued and will arrive later.
12406 We apply an heuristic to be able to distinguish such
12407 SIGTRAPs from other random SIGTRAPs: we keep this
12408 breakpoint location for a bit, and will retire it
12409 after we see some number of events. The theory here
12410 is that reporting of events should, "on the average",
12411 be fair, so after a while we'll see events from all
12412 threads that have anything of interest, and no longer
12413 need to keep this breakpoint location around. We
12414 don't hold locations forever so to reduce chances of
12415 mistaking a non-breakpoint SIGTRAP for a breakpoint
12416 SIGTRAP.
12417
12418 The heuristic failing can be disastrous on
12419 decr_pc_after_break targets.
12420
12421 On decr_pc_after_break targets, like e.g., x86-linux,
12422 if we fail to recognize a late breakpoint SIGTRAP,
12423 because events_till_retirement has reached 0 too
12424 soon, we'll fail to do the PC adjustment, and report
12425 a random SIGTRAP to the user. When the user resumes
12426 the inferior, it will most likely immediately crash
2dec564e 12427 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12428 corrupted, because of being resumed e.g., in the
12429 middle of a multi-byte instruction, or skipped a
12430 one-byte instruction. This was actually seen happen
12431 on native x86-linux, and should be less rare on
12432 targets that do not support new thread events, like
12433 remote, due to the heuristic depending on
12434 thread_count.
12435
12436 Mistaking a random SIGTRAP for a breakpoint trap
12437 causes similar symptoms (PC adjustment applied when
12438 it shouldn't), but then again, playing with SIGTRAPs
12439 behind the debugger's back is asking for trouble.
12440
12441 Since hardware watchpoint traps are always
12442 distinguishable from other traps, so we don't need to
12443 apply keep hardware watchpoint moribund locations
12444 around. We simply always ignore hardware watchpoint
12445 traps we can no longer explain. */
12446
876fa593
JK
12447 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12448 old_loc->owner = NULL;
20874c92 12449
876fa593 12450 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12451 }
12452 else
f431efe5
PA
12453 {
12454 old_loc->owner = NULL;
12455 decref_bp_location (&old_loc);
12456 }
20874c92 12457 }
74960c60 12458 }
1c5cfe86 12459
348d480f
PA
12460 /* Rescan breakpoints at the same address and section, marking the
12461 first one as "first" and any others as "duplicates". This is so
12462 that the bpt instruction is only inserted once. If we have a
12463 permanent breakpoint at the same place as BPT, make that one the
12464 official one, and the rest as duplicates. Permanent breakpoints
12465 are sorted first for the same address.
12466
12467 Do the same for hardware watchpoints, but also considering the
12468 watchpoint's type (regular/access/read) and length. */
12469
12470 bp_loc_first = NULL;
12471 wp_loc_first = NULL;
12472 awp_loc_first = NULL;
12473 rwp_loc_first = NULL;
12474 ALL_BP_LOCATIONS (loc, locp)
12475 {
12476 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12477 non-NULL. */
348d480f 12478 struct bp_location **loc_first_p;
d3fbdd86 12479 b = loc->owner;
348d480f 12480
6f380991 12481 if (!unduplicated_should_be_inserted (loc)
348d480f 12482 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12483 /* Don't detect duplicate for tracepoint locations because they are
12484 never duplicated. See the comments in field `duplicate' of
12485 `struct bp_location'. */
348d480f 12486 || is_tracepoint (b))
b775012e
LM
12487 {
12488 /* Clear the condition modification flag. */
12489 loc->condition_changed = condition_unchanged;
12490 continue;
12491 }
348d480f 12492
348d480f
PA
12493 if (b->type == bp_hardware_watchpoint)
12494 loc_first_p = &wp_loc_first;
12495 else if (b->type == bp_read_watchpoint)
12496 loc_first_p = &rwp_loc_first;
12497 else if (b->type == bp_access_watchpoint)
12498 loc_first_p = &awp_loc_first;
12499 else
12500 loc_first_p = &bp_loc_first;
12501
12502 if (*loc_first_p == NULL
12503 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12504 || !breakpoint_locations_match (loc, *loc_first_p))
12505 {
12506 *loc_first_p = loc;
12507 loc->duplicate = 0;
b775012e
LM
12508
12509 if (is_breakpoint (loc->owner) && loc->condition_changed)
12510 {
12511 loc->needs_update = 1;
12512 /* Clear the condition modification flag. */
12513 loc->condition_changed = condition_unchanged;
12514 }
348d480f
PA
12515 continue;
12516 }
12517
934709f0
PW
12518
12519 /* This and the above ensure the invariant that the first location
12520 is not duplicated, and is the inserted one.
12521 All following are marked as duplicated, and are not inserted. */
12522 if (loc->inserted)
12523 swap_insertion (loc, *loc_first_p);
348d480f
PA
12524 loc->duplicate = 1;
12525
b775012e
LM
12526 /* Clear the condition modification flag. */
12527 loc->condition_changed = condition_unchanged;
348d480f
PA
12528 }
12529
a25a5a45 12530 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12531 {
04086b45 12532 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12533 insert_breakpoint_locations ();
12534 else
12535 {
44702360
PA
12536 /* Even though the caller told us to not insert new
12537 locations, we may still need to update conditions on the
12538 target's side of breakpoints that were already inserted
12539 if the target is evaluating breakpoint conditions. We
b775012e
LM
12540 only update conditions for locations that are marked
12541 "needs_update". */
12542 update_inserted_breakpoint_locations ();
12543 }
12544 }
348d480f 12545
04086b45 12546 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12547 download_tracepoint_locations ();
12548
348d480f
PA
12549 do_cleanups (cleanups);
12550}
12551
12552void
12553breakpoint_retire_moribund (void)
12554{
12555 struct bp_location *loc;
12556 int ix;
12557
12558 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12559 if (--(loc->events_till_retirement) == 0)
12560 {
12561 decref_bp_location (&loc);
12562 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12563 --ix;
12564 }
12565}
12566
12567static void
44702360 12568update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12569{
348d480f 12570
492d29ea
PA
12571 TRY
12572 {
12573 update_global_location_list (insert_mode);
12574 }
12575 CATCH (e, RETURN_MASK_ERROR)
12576 {
12577 }
12578 END_CATCH
348d480f
PA
12579}
12580
12581/* Clear BKP from a BPS. */
12582
12583static void
12584bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12585{
12586 bpstat bs;
12587
12588 for (bs = bps; bs; bs = bs->next)
12589 if (bs->breakpoint_at == bpt)
12590 {
12591 bs->breakpoint_at = NULL;
12592 bs->old_val = NULL;
12593 /* bs->commands will be freed later. */
12594 }
12595}
12596
12597/* Callback for iterate_over_threads. */
12598static int
12599bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12600{
9a3c8263 12601 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12602
12603 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12604 return 0;
12605}
12606
12607/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12608 callbacks. */
12609
12610static void
12611say_where (struct breakpoint *b)
12612{
12613 struct value_print_options opts;
12614
12615 get_user_print_options (&opts);
12616
12617 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12618 single string. */
12619 if (b->loc == NULL)
12620 {
f00aae0f
KS
12621 /* For pending locations, the output differs slightly based
12622 on b->extra_string. If this is non-NULL, it contains either
12623 a condition or dprintf arguments. */
12624 if (b->extra_string == NULL)
12625 {
12626 printf_filtered (_(" (%s) pending."),
d28cd78a 12627 event_location_to_string (b->location.get ()));
f00aae0f
KS
12628 }
12629 else if (b->type == bp_dprintf)
12630 {
12631 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12632 event_location_to_string (b->location.get ()),
f00aae0f
KS
12633 b->extra_string);
12634 }
12635 else
12636 {
12637 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12638 event_location_to_string (b->location.get ()),
f00aae0f
KS
12639 b->extra_string);
12640 }
348d480f
PA
12641 }
12642 else
12643 {
2f202fde 12644 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12645 {
12646 printf_filtered (" at ");
12647 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12648 gdb_stdout);
12649 }
2f202fde 12650 if (b->loc->symtab != NULL)
f8eba3c6
TT
12651 {
12652 /* If there is a single location, we can print the location
12653 more nicely. */
12654 if (b->loc->next == NULL)
12655 printf_filtered (": file %s, line %d.",
05cba821
JK
12656 symtab_to_filename_for_display (b->loc->symtab),
12657 b->loc->line_number);
f8eba3c6
TT
12658 else
12659 /* This is not ideal, but each location may have a
12660 different file name, and this at least reflects the
12661 real situation somewhat. */
f00aae0f 12662 printf_filtered (": %s.",
d28cd78a 12663 event_location_to_string (b->location.get ()));
f8eba3c6 12664 }
348d480f
PA
12665
12666 if (b->loc->next)
12667 {
12668 struct bp_location *loc = b->loc;
12669 int n = 0;
12670 for (; loc; loc = loc->next)
12671 ++n;
12672 printf_filtered (" (%d locations)", n);
12673 }
12674 }
12675}
12676
348d480f
PA
12677/* Default bp_location_ops methods. */
12678
12679static void
12680bp_location_dtor (struct bp_location *self)
12681{
348d480f
PA
12682 xfree (self->function_name);
12683}
12684
12685static const struct bp_location_ops bp_location_ops =
12686{
12687 bp_location_dtor
12688};
12689
c1fc2657 12690/* Destructor for the breakpoint base class. */
348d480f 12691
c1fc2657 12692breakpoint::~breakpoint ()
348d480f 12693{
c1fc2657
SM
12694 decref_counted_command_line (&this->commands);
12695 xfree (this->cond_string);
12696 xfree (this->extra_string);
12697 xfree (this->filter);
348d480f
PA
12698}
12699
2060206e
PA
12700static struct bp_location *
12701base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12702{
5625a286 12703 return new bp_location (&bp_location_ops, self);
348d480f
PA
12704}
12705
2060206e
PA
12706static void
12707base_breakpoint_re_set (struct breakpoint *b)
12708{
12709 /* Nothing to re-set. */
12710}
12711
12712#define internal_error_pure_virtual_called() \
12713 gdb_assert_not_reached ("pure virtual function called")
12714
12715static int
12716base_breakpoint_insert_location (struct bp_location *bl)
12717{
12718 internal_error_pure_virtual_called ();
12719}
12720
12721static int
73971819
PA
12722base_breakpoint_remove_location (struct bp_location *bl,
12723 enum remove_bp_reason reason)
2060206e
PA
12724{
12725 internal_error_pure_virtual_called ();
12726}
12727
12728static int
12729base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12730 struct address_space *aspace,
09ac7c10
TT
12731 CORE_ADDR bp_addr,
12732 const struct target_waitstatus *ws)
2060206e
PA
12733{
12734 internal_error_pure_virtual_called ();
12735}
12736
12737static void
12738base_breakpoint_check_status (bpstat bs)
12739{
12740 /* Always stop. */
12741}
12742
12743/* A "works_in_software_mode" breakpoint_ops method that just internal
12744 errors. */
12745
12746static int
12747base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12748{
12749 internal_error_pure_virtual_called ();
12750}
12751
12752/* A "resources_needed" breakpoint_ops method that just internal
12753 errors. */
12754
12755static int
12756base_breakpoint_resources_needed (const struct bp_location *bl)
12757{
12758 internal_error_pure_virtual_called ();
12759}
12760
12761static enum print_stop_action
12762base_breakpoint_print_it (bpstat bs)
12763{
12764 internal_error_pure_virtual_called ();
12765}
12766
12767static void
12768base_breakpoint_print_one_detail (const struct breakpoint *self,
12769 struct ui_out *uiout)
12770{
12771 /* nothing */
12772}
12773
12774static void
12775base_breakpoint_print_mention (struct breakpoint *b)
12776{
12777 internal_error_pure_virtual_called ();
12778}
12779
12780static void
12781base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12782{
12783 internal_error_pure_virtual_called ();
12784}
12785
983af33b 12786static void
f00aae0f
KS
12787base_breakpoint_create_sals_from_location
12788 (const struct event_location *location,
12789 struct linespec_result *canonical,
12790 enum bptype type_wanted)
983af33b
SDJ
12791{
12792 internal_error_pure_virtual_called ();
12793}
12794
12795static void
12796base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12797 struct linespec_result *c,
e1e01040
PA
12798 gdb::unique_xmalloc_ptr<char> cond_string,
12799 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12800 enum bptype type_wanted,
12801 enum bpdisp disposition,
12802 int thread,
12803 int task, int ignore_count,
12804 const struct breakpoint_ops *o,
12805 int from_tty, int enabled,
44f238bb 12806 int internal, unsigned flags)
983af33b
SDJ
12807{
12808 internal_error_pure_virtual_called ();
12809}
12810
12811static void
f00aae0f
KS
12812base_breakpoint_decode_location (struct breakpoint *b,
12813 const struct event_location *location,
c2f4122d 12814 struct program_space *search_pspace,
983af33b
SDJ
12815 struct symtabs_and_lines *sals)
12816{
12817 internal_error_pure_virtual_called ();
12818}
12819
ab04a2af
TT
12820/* The default 'explains_signal' method. */
12821
47591c29 12822static int
427cd150 12823base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12824{
47591c29 12825 return 1;
ab04a2af
TT
12826}
12827
9d6e6e84
HZ
12828/* The default "after_condition_true" method. */
12829
12830static void
12831base_breakpoint_after_condition_true (struct bpstats *bs)
12832{
12833 /* Nothing to do. */
12834}
12835
ab04a2af 12836struct breakpoint_ops base_breakpoint_ops =
2060206e 12837{
2060206e
PA
12838 base_breakpoint_allocate_location,
12839 base_breakpoint_re_set,
12840 base_breakpoint_insert_location,
12841 base_breakpoint_remove_location,
12842 base_breakpoint_breakpoint_hit,
12843 base_breakpoint_check_status,
12844 base_breakpoint_resources_needed,
12845 base_breakpoint_works_in_software_mode,
12846 base_breakpoint_print_it,
12847 NULL,
12848 base_breakpoint_print_one_detail,
12849 base_breakpoint_print_mention,
983af33b 12850 base_breakpoint_print_recreate,
5f700d83 12851 base_breakpoint_create_sals_from_location,
983af33b 12852 base_breakpoint_create_breakpoints_sal,
5f700d83 12853 base_breakpoint_decode_location,
9d6e6e84
HZ
12854 base_breakpoint_explains_signal,
12855 base_breakpoint_after_condition_true,
2060206e
PA
12856};
12857
12858/* Default breakpoint_ops methods. */
12859
12860static void
348d480f
PA
12861bkpt_re_set (struct breakpoint *b)
12862{
06edf0c0 12863 /* FIXME: is this still reachable? */
9ef9e6a6 12864 if (breakpoint_event_location_empty_p (b))
06edf0c0 12865 {
f00aae0f 12866 /* Anything without a location can't be re-set. */
348d480f 12867 delete_breakpoint (b);
06edf0c0 12868 return;
348d480f 12869 }
06edf0c0
PA
12870
12871 breakpoint_re_set_default (b);
348d480f
PA
12872}
12873
2060206e 12874static int
348d480f
PA
12875bkpt_insert_location (struct bp_location *bl)
12876{
cd6c3b4f
YQ
12877 CORE_ADDR addr = bl->target_info.reqstd_address;
12878
579c6ad9 12879 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12880 bl->target_info.placed_address = addr;
12881
348d480f 12882 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12883 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12884 else
7c16b83e 12885 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12886}
12887
2060206e 12888static int
73971819 12889bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12890{
12891 if (bl->loc_type == bp_loc_hardware_breakpoint)
12892 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12893 else
73971819 12894 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12895}
12896
2060206e 12897static int
348d480f 12898bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12899 struct address_space *aspace, CORE_ADDR bp_addr,
12900 const struct target_waitstatus *ws)
348d480f 12901{
09ac7c10 12902 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12903 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12904 return 0;
12905
348d480f
PA
12906 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12907 aspace, bp_addr))
12908 return 0;
12909
12910 if (overlay_debugging /* unmapped overlay section */
12911 && section_is_overlay (bl->section)
12912 && !section_is_mapped (bl->section))
12913 return 0;
12914
12915 return 1;
12916}
12917
cd1608cc
PA
12918static int
12919dprintf_breakpoint_hit (const struct bp_location *bl,
12920 struct address_space *aspace, CORE_ADDR bp_addr,
12921 const struct target_waitstatus *ws)
12922{
12923 if (dprintf_style == dprintf_style_agent
12924 && target_can_run_breakpoint_commands ())
12925 {
12926 /* An agent-style dprintf never causes a stop. If we see a trap
12927 for this address it must be for a breakpoint that happens to
12928 be set at the same address. */
12929 return 0;
12930 }
12931
12932 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12933}
12934
2060206e 12935static int
348d480f
PA
12936bkpt_resources_needed (const struct bp_location *bl)
12937{
12938 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12939
12940 return 1;
12941}
12942
2060206e 12943static enum print_stop_action
348d480f
PA
12944bkpt_print_it (bpstat bs)
12945{
348d480f
PA
12946 struct breakpoint *b;
12947 const struct bp_location *bl;
001c8c33 12948 int bp_temp;
79a45e25 12949 struct ui_out *uiout = current_uiout;
348d480f
PA
12950
12951 gdb_assert (bs->bp_location_at != NULL);
12952
12953 bl = bs->bp_location_at;
12954 b = bs->breakpoint_at;
12955
001c8c33
PA
12956 bp_temp = b->disposition == disp_del;
12957 if (bl->address != bl->requested_address)
12958 breakpoint_adjustment_warning (bl->requested_address,
12959 bl->address,
12960 b->number, 1);
12961 annotate_breakpoint (b->number);
f303dbd6
PA
12962 maybe_print_thread_hit_breakpoint (uiout);
12963
001c8c33 12964 if (bp_temp)
112e8700 12965 uiout->text ("Temporary breakpoint ");
001c8c33 12966 else
112e8700
SM
12967 uiout->text ("Breakpoint ");
12968 if (uiout->is_mi_like_p ())
348d480f 12969 {
112e8700 12970 uiout->field_string ("reason",
001c8c33 12971 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12972 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12973 }
112e8700
SM
12974 uiout->field_int ("bkptno", b->number);
12975 uiout->text (", ");
06edf0c0 12976
001c8c33 12977 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12978}
12979
2060206e 12980static void
06edf0c0
PA
12981bkpt_print_mention (struct breakpoint *b)
12982{
112e8700 12983 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12984 return;
12985
12986 switch (b->type)
12987 {
12988 case bp_breakpoint:
12989 case bp_gnu_ifunc_resolver:
12990 if (b->disposition == disp_del)
12991 printf_filtered (_("Temporary breakpoint"));
12992 else
12993 printf_filtered (_("Breakpoint"));
12994 printf_filtered (_(" %d"), b->number);
12995 if (b->type == bp_gnu_ifunc_resolver)
12996 printf_filtered (_(" at gnu-indirect-function resolver"));
12997 break;
12998 case bp_hardware_breakpoint:
12999 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13000 break;
e7e0cddf
SS
13001 case bp_dprintf:
13002 printf_filtered (_("Dprintf %d"), b->number);
13003 break;
06edf0c0
PA
13004 }
13005
13006 say_where (b);
13007}
13008
2060206e 13009static void
06edf0c0
PA
13010bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13011{
13012 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13013 fprintf_unfiltered (fp, "tbreak");
13014 else if (tp->type == bp_breakpoint)
13015 fprintf_unfiltered (fp, "break");
13016 else if (tp->type == bp_hardware_breakpoint
13017 && tp->disposition == disp_del)
13018 fprintf_unfiltered (fp, "thbreak");
13019 else if (tp->type == bp_hardware_breakpoint)
13020 fprintf_unfiltered (fp, "hbreak");
13021 else
13022 internal_error (__FILE__, __LINE__,
13023 _("unhandled breakpoint type %d"), (int) tp->type);
13024
f00aae0f 13025 fprintf_unfiltered (fp, " %s",
d28cd78a 13026 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13027
13028 /* Print out extra_string if this breakpoint is pending. It might
13029 contain, for example, conditions that were set by the user. */
13030 if (tp->loc == NULL && tp->extra_string != NULL)
13031 fprintf_unfiltered (fp, " %s", tp->extra_string);
13032
dd11a36c 13033 print_recreate_thread (tp, fp);
06edf0c0
PA
13034}
13035
983af33b 13036static void
f00aae0f
KS
13037bkpt_create_sals_from_location (const struct event_location *location,
13038 struct linespec_result *canonical,
13039 enum bptype type_wanted)
983af33b 13040{
f00aae0f 13041 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13042}
13043
13044static void
13045bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13046 struct linespec_result *canonical,
e1e01040
PA
13047 gdb::unique_xmalloc_ptr<char> cond_string,
13048 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13049 enum bptype type_wanted,
13050 enum bpdisp disposition,
13051 int thread,
13052 int task, int ignore_count,
13053 const struct breakpoint_ops *ops,
13054 int from_tty, int enabled,
44f238bb 13055 int internal, unsigned flags)
983af33b 13056{
023fa29b 13057 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13058 std::move (cond_string),
13059 std::move (extra_string),
e7e0cddf 13060 type_wanted,
983af33b
SDJ
13061 disposition, thread, task,
13062 ignore_count, ops, from_tty,
44f238bb 13063 enabled, internal, flags);
983af33b
SDJ
13064}
13065
13066static void
f00aae0f
KS
13067bkpt_decode_location (struct breakpoint *b,
13068 const struct event_location *location,
c2f4122d 13069 struct program_space *search_pspace,
983af33b
SDJ
13070 struct symtabs_and_lines *sals)
13071{
c2f4122d 13072 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13073}
13074
06edf0c0
PA
13075/* Virtual table for internal breakpoints. */
13076
13077static void
13078internal_bkpt_re_set (struct breakpoint *b)
13079{
13080 switch (b->type)
13081 {
13082 /* Delete overlay event and longjmp master breakpoints; they
13083 will be reset later by breakpoint_re_set. */
13084 case bp_overlay_event:
13085 case bp_longjmp_master:
13086 case bp_std_terminate_master:
13087 case bp_exception_master:
13088 delete_breakpoint (b);
13089 break;
13090
13091 /* This breakpoint is special, it's set up when the inferior
13092 starts and we really don't want to touch it. */
13093 case bp_shlib_event:
13094
13095 /* Like bp_shlib_event, this breakpoint type is special. Once
13096 it is set up, we do not want to touch it. */
13097 case bp_thread_event:
13098 break;
13099 }
13100}
13101
13102static void
13103internal_bkpt_check_status (bpstat bs)
13104{
a9b3a50f
PA
13105 if (bs->breakpoint_at->type == bp_shlib_event)
13106 {
13107 /* If requested, stop when the dynamic linker notifies GDB of
13108 events. This allows the user to get control and place
13109 breakpoints in initializer routines for dynamically loaded
13110 objects (among other things). */
13111 bs->stop = stop_on_solib_events;
13112 bs->print = stop_on_solib_events;
13113 }
13114 else
13115 bs->stop = 0;
06edf0c0
PA
13116}
13117
13118static enum print_stop_action
13119internal_bkpt_print_it (bpstat bs)
13120{
06edf0c0 13121 struct breakpoint *b;
06edf0c0 13122
06edf0c0
PA
13123 b = bs->breakpoint_at;
13124
06edf0c0
PA
13125 switch (b->type)
13126 {
348d480f
PA
13127 case bp_shlib_event:
13128 /* Did we stop because the user set the stop_on_solib_events
13129 variable? (If so, we report this as a generic, "Stopped due
13130 to shlib event" message.) */
edcc5120 13131 print_solib_event (0);
348d480f
PA
13132 break;
13133
13134 case bp_thread_event:
13135 /* Not sure how we will get here.
13136 GDB should not stop for these breakpoints. */
13137 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13138 break;
13139
13140 case bp_overlay_event:
13141 /* By analogy with the thread event, GDB should not stop for these. */
13142 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13143 break;
13144
13145 case bp_longjmp_master:
13146 /* These should never be enabled. */
13147 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13148 break;
13149
13150 case bp_std_terminate_master:
13151 /* These should never be enabled. */
13152 printf_filtered (_("std::terminate Master Breakpoint: "
13153 "gdb should not stop!\n"));
348d480f
PA
13154 break;
13155
13156 case bp_exception_master:
13157 /* These should never be enabled. */
13158 printf_filtered (_("Exception Master Breakpoint: "
13159 "gdb should not stop!\n"));
06edf0c0
PA
13160 break;
13161 }
13162
001c8c33 13163 return PRINT_NOTHING;
06edf0c0
PA
13164}
13165
13166static void
13167internal_bkpt_print_mention (struct breakpoint *b)
13168{
13169 /* Nothing to mention. These breakpoints are internal. */
13170}
13171
06edf0c0
PA
13172/* Virtual table for momentary breakpoints */
13173
13174static void
13175momentary_bkpt_re_set (struct breakpoint *b)
13176{
13177 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13178 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13179 Otherwise these should have been blown away via the cleanup chain
13180 or by breakpoint_init_inferior when we rerun the executable. */
13181}
13182
13183static void
13184momentary_bkpt_check_status (bpstat bs)
13185{
13186 /* Nothing. The point of these breakpoints is causing a stop. */
13187}
13188
13189static enum print_stop_action
13190momentary_bkpt_print_it (bpstat bs)
13191{
001c8c33 13192 return PRINT_UNKNOWN;
348d480f
PA
13193}
13194
06edf0c0
PA
13195static void
13196momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13197{
06edf0c0 13198 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13199}
13200
e2e4d78b
JK
13201/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13202
13203 It gets cleared already on the removal of the first one of such placed
13204 breakpoints. This is OK as they get all removed altogether. */
13205
c1fc2657 13206longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 13207{
c1fc2657 13208 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 13209
c1fc2657 13210 if (tp != NULL)
e2e4d78b 13211 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
13212}
13213
55aa24fb
SDJ
13214/* Specific methods for probe breakpoints. */
13215
13216static int
13217bkpt_probe_insert_location (struct bp_location *bl)
13218{
13219 int v = bkpt_insert_location (bl);
13220
13221 if (v == 0)
13222 {
13223 /* The insertion was successful, now let's set the probe's semaphore
13224 if needed. */
0ea5cda8
SDJ
13225 if (bl->probe.probe->pops->set_semaphore != NULL)
13226 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13227 bl->probe.objfile,
13228 bl->gdbarch);
55aa24fb
SDJ
13229 }
13230
13231 return v;
13232}
13233
13234static int
73971819
PA
13235bkpt_probe_remove_location (struct bp_location *bl,
13236 enum remove_bp_reason reason)
55aa24fb
SDJ
13237{
13238 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13239 if (bl->probe.probe->pops->clear_semaphore != NULL)
13240 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13241 bl->probe.objfile,
13242 bl->gdbarch);
55aa24fb 13243
73971819 13244 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13245}
13246
13247static void
f00aae0f 13248bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13249 struct linespec_result *canonical,
f00aae0f 13250 enum bptype type_wanted)
55aa24fb
SDJ
13251{
13252 struct linespec_sals lsal;
13253
c2f4122d 13254 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13255 lsal.canonical
13256 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13257 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13258}
13259
13260static void
f00aae0f
KS
13261bkpt_probe_decode_location (struct breakpoint *b,
13262 const struct event_location *location,
c2f4122d 13263 struct program_space *search_pspace,
55aa24fb
SDJ
13264 struct symtabs_and_lines *sals)
13265{
c2f4122d 13266 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13267 if (!sals->sals)
13268 error (_("probe not found"));
13269}
13270
348d480f 13271/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13272
348d480f
PA
13273static void
13274tracepoint_re_set (struct breakpoint *b)
13275{
13276 breakpoint_re_set_default (b);
13277}
876fa593 13278
348d480f
PA
13279static int
13280tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13281 struct address_space *aspace, CORE_ADDR bp_addr,
13282 const struct target_waitstatus *ws)
348d480f
PA
13283{
13284 /* By definition, the inferior does not report stops at
13285 tracepoints. */
13286 return 0;
74960c60
VP
13287}
13288
13289static void
348d480f
PA
13290tracepoint_print_one_detail (const struct breakpoint *self,
13291 struct ui_out *uiout)
74960c60 13292{
d9b3f62e
PA
13293 struct tracepoint *tp = (struct tracepoint *) self;
13294 if (tp->static_trace_marker_id)
348d480f
PA
13295 {
13296 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13297
112e8700
SM
13298 uiout->text ("\tmarker id is ");
13299 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13300 tp->static_trace_marker_id);
112e8700 13301 uiout->text ("\n");
348d480f 13302 }
0d381245
VP
13303}
13304
a474d7c2 13305static void
348d480f 13306tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13307{
112e8700 13308 if (current_uiout->is_mi_like_p ())
348d480f 13309 return;
cc59ec59 13310
348d480f
PA
13311 switch (b->type)
13312 {
13313 case bp_tracepoint:
13314 printf_filtered (_("Tracepoint"));
13315 printf_filtered (_(" %d"), b->number);
13316 break;
13317 case bp_fast_tracepoint:
13318 printf_filtered (_("Fast tracepoint"));
13319 printf_filtered (_(" %d"), b->number);
13320 break;
13321 case bp_static_tracepoint:
13322 printf_filtered (_("Static tracepoint"));
13323 printf_filtered (_(" %d"), b->number);
13324 break;
13325 default:
13326 internal_error (__FILE__, __LINE__,
13327 _("unhandled tracepoint type %d"), (int) b->type);
13328 }
13329
13330 say_where (b);
a474d7c2
PA
13331}
13332
348d480f 13333static void
d9b3f62e 13334tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13335{
d9b3f62e
PA
13336 struct tracepoint *tp = (struct tracepoint *) self;
13337
13338 if (self->type == bp_fast_tracepoint)
348d480f 13339 fprintf_unfiltered (fp, "ftrace");
c93e8391 13340 else if (self->type == bp_static_tracepoint)
348d480f 13341 fprintf_unfiltered (fp, "strace");
d9b3f62e 13342 else if (self->type == bp_tracepoint)
348d480f
PA
13343 fprintf_unfiltered (fp, "trace");
13344 else
13345 internal_error (__FILE__, __LINE__,
d9b3f62e 13346 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13347
f00aae0f 13348 fprintf_unfiltered (fp, " %s",
d28cd78a 13349 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13350 print_recreate_thread (self, fp);
13351
13352 if (tp->pass_count)
13353 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13354}
13355
983af33b 13356static void
f00aae0f
KS
13357tracepoint_create_sals_from_location (const struct event_location *location,
13358 struct linespec_result *canonical,
13359 enum bptype type_wanted)
983af33b 13360{
f00aae0f 13361 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13362}
13363
13364static void
13365tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13366 struct linespec_result *canonical,
e1e01040
PA
13367 gdb::unique_xmalloc_ptr<char> cond_string,
13368 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13369 enum bptype type_wanted,
13370 enum bpdisp disposition,
13371 int thread,
13372 int task, int ignore_count,
13373 const struct breakpoint_ops *ops,
13374 int from_tty, int enabled,
44f238bb 13375 int internal, unsigned flags)
983af33b 13376{
023fa29b 13377 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13378 std::move (cond_string),
13379 std::move (extra_string),
e7e0cddf 13380 type_wanted,
983af33b
SDJ
13381 disposition, thread, task,
13382 ignore_count, ops, from_tty,
44f238bb 13383 enabled, internal, flags);
983af33b
SDJ
13384}
13385
13386static void
f00aae0f
KS
13387tracepoint_decode_location (struct breakpoint *b,
13388 const struct event_location *location,
c2f4122d 13389 struct program_space *search_pspace,
983af33b
SDJ
13390 struct symtabs_and_lines *sals)
13391{
c2f4122d 13392 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13393}
13394
2060206e 13395struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13396
55aa24fb
SDJ
13397/* The breakpoint_ops structure to be use on tracepoints placed in a
13398 static probe. */
13399
13400static void
f00aae0f
KS
13401tracepoint_probe_create_sals_from_location
13402 (const struct event_location *location,
13403 struct linespec_result *canonical,
13404 enum bptype type_wanted)
55aa24fb
SDJ
13405{
13406 /* We use the same method for breakpoint on probes. */
f00aae0f 13407 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13408}
13409
13410static void
f00aae0f
KS
13411tracepoint_probe_decode_location (struct breakpoint *b,
13412 const struct event_location *location,
c2f4122d 13413 struct program_space *search_pspace,
55aa24fb
SDJ
13414 struct symtabs_and_lines *sals)
13415{
13416 /* We use the same method for breakpoint on probes. */
c2f4122d 13417 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13418}
13419
13420static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13421
5c2b4418
HZ
13422/* Dprintf breakpoint_ops methods. */
13423
13424static void
13425dprintf_re_set (struct breakpoint *b)
13426{
13427 breakpoint_re_set_default (b);
13428
f00aae0f
KS
13429 /* extra_string should never be non-NULL for dprintf. */
13430 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13431
13432 /* 1 - connect to target 1, that can run breakpoint commands.
13433 2 - create a dprintf, which resolves fine.
13434 3 - disconnect from target 1
13435 4 - connect to target 2, that can NOT run breakpoint commands.
13436
13437 After steps #3/#4, you'll want the dprintf command list to
13438 be updated, because target 1 and 2 may well return different
13439 answers for target_can_run_breakpoint_commands().
13440 Given absence of finer grained resetting, we get to do
13441 it all the time. */
13442 if (b->extra_string != NULL)
13443 update_dprintf_command_list (b);
13444}
13445
2d9442cc
HZ
13446/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13447
13448static void
13449dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13450{
f00aae0f 13451 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13452 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13453 tp->extra_string);
13454 print_recreate_thread (tp, fp);
13455}
13456
9d6e6e84
HZ
13457/* Implement the "after_condition_true" breakpoint_ops method for
13458 dprintf.
13459
13460 dprintf's are implemented with regular commands in their command
13461 list, but we run the commands here instead of before presenting the
13462 stop to the user, as dprintf's don't actually cause a stop. This
13463 also makes it so that the commands of multiple dprintfs at the same
13464 address are all handled. */
13465
13466static void
13467dprintf_after_condition_true (struct bpstats *bs)
13468{
13469 struct cleanup *old_chain;
13470 struct bpstats tmp_bs = { NULL };
13471 struct bpstats *tmp_bs_p = &tmp_bs;
13472
13473 /* dprintf's never cause a stop. This wasn't set in the
13474 check_status hook instead because that would make the dprintf's
13475 condition not be evaluated. */
13476 bs->stop = 0;
13477
13478 /* Run the command list here. Take ownership of it instead of
13479 copying. We never want these commands to run later in
13480 bpstat_do_actions, if a breakpoint that causes a stop happens to
13481 be set at same address as this dprintf, or even if running the
13482 commands here throws. */
13483 tmp_bs.commands = bs->commands;
13484 bs->commands = NULL;
13485 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13486
13487 bpstat_do_actions_1 (&tmp_bs_p);
13488
13489 /* 'tmp_bs.commands' will usually be NULL by now, but
13490 bpstat_do_actions_1 may return early without processing the whole
13491 list. */
13492 do_cleanups (old_chain);
13493}
13494
983af33b
SDJ
13495/* The breakpoint_ops structure to be used on static tracepoints with
13496 markers (`-m'). */
13497
13498static void
f00aae0f 13499strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13500 struct linespec_result *canonical,
f00aae0f 13501 enum bptype type_wanted)
983af33b
SDJ
13502{
13503 struct linespec_sals lsal;
f00aae0f
KS
13504 const char *arg_start, *arg;
13505 char *str;
13506 struct cleanup *cleanup;
983af33b 13507
f00aae0f
KS
13508 arg = arg_start = get_linespec_location (location);
13509 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13510
f00aae0f
KS
13511 str = savestring (arg_start, arg - arg_start);
13512 cleanup = make_cleanup (xfree, str);
8e9e35b1 13513 canonical->location = new_linespec_location (&str);
f00aae0f 13514 do_cleanups (cleanup);
983af33b 13515
8e9e35b1
TT
13516 lsal.canonical
13517 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13518 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13519}
13520
13521static void
13522strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13523 struct linespec_result *canonical,
e1e01040
PA
13524 gdb::unique_xmalloc_ptr<char> cond_string,
13525 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13526 enum bptype type_wanted,
13527 enum bpdisp disposition,
13528 int thread,
13529 int task, int ignore_count,
13530 const struct breakpoint_ops *ops,
13531 int from_tty, int enabled,
44f238bb 13532 int internal, unsigned flags)
983af33b
SDJ
13533{
13534 int i;
52d361e1
YQ
13535 struct linespec_sals *lsal = VEC_index (linespec_sals,
13536 canonical->sals, 0);
983af33b
SDJ
13537
13538 /* If the user is creating a static tracepoint by marker id
13539 (strace -m MARKER_ID), then store the sals index, so that
13540 breakpoint_re_set can try to match up which of the newly
13541 found markers corresponds to this one, and, don't try to
13542 expand multiple locations for each sal, given than SALS
13543 already should contain all sals for MARKER_ID. */
13544
13545 for (i = 0; i < lsal->sals.nelts; ++i)
13546 {
13547 struct symtabs_and_lines expanded;
ffc2605c 13548 event_location_up location;
983af33b
SDJ
13549
13550 expanded.nelts = 1;
13551 expanded.sals = &lsal->sals.sals[i];
13552
8e9e35b1 13553 location = copy_event_location (canonical->location.get ());
983af33b 13554
b270e6f9
TT
13555 std::unique_ptr<tracepoint> tp (new tracepoint ());
13556 init_breakpoint_sal (tp.get (), gdbarch, expanded,
ffc2605c 13557 std::move (location), NULL,
e1e01040
PA
13558 std::move (cond_string),
13559 std::move (extra_string),
e7e0cddf 13560 type_wanted, disposition,
983af33b 13561 thread, task, ignore_count, ops,
44f238bb 13562 from_tty, enabled, internal, flags,
983af33b
SDJ
13563 canonical->special_display);
13564 /* Given that its possible to have multiple markers with
13565 the same string id, if the user is creating a static
13566 tracepoint by marker id ("strace -m MARKER_ID"), then
13567 store the sals index, so that breakpoint_re_set can
13568 try to match up which of the newly found markers
13569 corresponds to this one */
13570 tp->static_trace_marker_id_idx = i;
13571
b270e6f9 13572 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13573 }
13574}
13575
13576static void
f00aae0f
KS
13577strace_marker_decode_location (struct breakpoint *b,
13578 const struct event_location *location,
c2f4122d 13579 struct program_space *search_pspace,
983af33b
SDJ
13580 struct symtabs_and_lines *sals)
13581{
13582 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13583 const char *s = get_linespec_location (location);
983af33b 13584
f00aae0f 13585 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13586 if (sals->nelts > tp->static_trace_marker_id_idx)
13587 {
13588 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13589 sals->nelts = 1;
13590 }
13591 else
13592 error (_("marker %s not found"), tp->static_trace_marker_id);
13593}
13594
13595static struct breakpoint_ops strace_marker_breakpoint_ops;
13596
13597static int
13598strace_marker_p (struct breakpoint *b)
13599{
13600 return b->ops == &strace_marker_breakpoint_ops;
13601}
13602
53a5351d 13603/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13604 structures. */
c906108c
SS
13605
13606void
fba45db2 13607delete_breakpoint (struct breakpoint *bpt)
c906108c 13608{
52f0bd74 13609 struct breakpoint *b;
c906108c 13610
8a3fe4f8 13611 gdb_assert (bpt != NULL);
c906108c 13612
4a64f543
MS
13613 /* Has this bp already been deleted? This can happen because
13614 multiple lists can hold pointers to bp's. bpstat lists are
13615 especial culprits.
13616
13617 One example of this happening is a watchpoint's scope bp. When
13618 the scope bp triggers, we notice that the watchpoint is out of
13619 scope, and delete it. We also delete its scope bp. But the
13620 scope bp is marked "auto-deleting", and is already on a bpstat.
13621 That bpstat is then checked for auto-deleting bp's, which are
13622 deleted.
13623
13624 A real solution to this problem might involve reference counts in
13625 bp's, and/or giving them pointers back to their referencing
13626 bpstat's, and teaching delete_breakpoint to only free a bp's
13627 storage when no more references were extent. A cheaper bandaid
13628 was chosen. */
c906108c
SS
13629 if (bpt->type == bp_none)
13630 return;
13631
4a64f543
MS
13632 /* At least avoid this stale reference until the reference counting
13633 of breakpoints gets resolved. */
d0fb5eae 13634 if (bpt->related_breakpoint != bpt)
e5a0a904 13635 {
d0fb5eae 13636 struct breakpoint *related;
3a5c3e22 13637 struct watchpoint *w;
d0fb5eae
JK
13638
13639 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13640 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13641 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13642 w = (struct watchpoint *) bpt;
13643 else
13644 w = NULL;
13645 if (w != NULL)
13646 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13647
13648 /* Unlink bpt from the bpt->related_breakpoint ring. */
13649 for (related = bpt; related->related_breakpoint != bpt;
13650 related = related->related_breakpoint);
13651 related->related_breakpoint = bpt->related_breakpoint;
13652 bpt->related_breakpoint = bpt;
e5a0a904
JK
13653 }
13654
a9634178
TJB
13655 /* watch_command_1 creates a watchpoint but only sets its number if
13656 update_watchpoint succeeds in creating its bp_locations. If there's
13657 a problem in that process, we'll be asked to delete the half-created
13658 watchpoint. In that case, don't announce the deletion. */
13659 if (bpt->number)
13660 observer_notify_breakpoint_deleted (bpt);
c906108c 13661
c906108c
SS
13662 if (breakpoint_chain == bpt)
13663 breakpoint_chain = bpt->next;
13664
c906108c
SS
13665 ALL_BREAKPOINTS (b)
13666 if (b->next == bpt)
c5aa993b
JM
13667 {
13668 b->next = bpt->next;
13669 break;
13670 }
c906108c 13671
f431efe5
PA
13672 /* Be sure no bpstat's are pointing at the breakpoint after it's
13673 been freed. */
13674 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13675 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13676 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13677 commands are associated with the bpstat; if we remove it here,
13678 then the later call to bpstat_do_actions (&stop_bpstat); in
13679 event-top.c won't do anything, and temporary breakpoints with
13680 commands won't work. */
13681
13682 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13683
4a64f543
MS
13684 /* Now that breakpoint is removed from breakpoint list, update the
13685 global location list. This will remove locations that used to
13686 belong to this breakpoint. Do this before freeing the breakpoint
13687 itself, since remove_breakpoint looks at location's owner. It
13688 might be better design to have location completely
13689 self-contained, but it's not the case now. */
44702360 13690 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13691
4a64f543
MS
13692 /* On the chance that someone will soon try again to delete this
13693 same bp, we mark it as deleted before freeing its storage. */
c906108c 13694 bpt->type = bp_none;
4d01a485 13695 delete bpt;
c906108c
SS
13696}
13697
4d6140d9
AC
13698static void
13699do_delete_breakpoint_cleanup (void *b)
13700{
9a3c8263 13701 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13702}
13703
13704struct cleanup *
13705make_cleanup_delete_breakpoint (struct breakpoint *b)
13706{
13707 return make_cleanup (do_delete_breakpoint_cleanup, b);
13708}
13709
51be5b68
PA
13710/* Iterator function to call a user-provided callback function once
13711 for each of B and its related breakpoints. */
13712
13713static void
13714iterate_over_related_breakpoints (struct breakpoint *b,
13715 void (*function) (struct breakpoint *,
13716 void *),
13717 void *data)
13718{
13719 struct breakpoint *related;
13720
13721 related = b;
13722 do
13723 {
13724 struct breakpoint *next;
13725
13726 /* FUNCTION may delete RELATED. */
13727 next = related->related_breakpoint;
13728
13729 if (next == related)
13730 {
13731 /* RELATED is the last ring entry. */
13732 function (related, data);
13733
13734 /* FUNCTION may have deleted it, so we'd never reach back to
13735 B. There's nothing left to do anyway, so just break
13736 out. */
13737 break;
13738 }
13739 else
13740 function (related, data);
13741
13742 related = next;
13743 }
13744 while (related != b);
13745}
95a42b64
TT
13746
13747static void
13748do_delete_breakpoint (struct breakpoint *b, void *ignore)
13749{
13750 delete_breakpoint (b);
13751}
13752
51be5b68
PA
13753/* A callback for map_breakpoint_numbers that calls
13754 delete_breakpoint. */
13755
13756static void
13757do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13758{
13759 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13760}
13761
c906108c 13762void
fba45db2 13763delete_command (char *arg, int from_tty)
c906108c 13764{
35df4500 13765 struct breakpoint *b, *b_tmp;
c906108c 13766
ea9365bb
TT
13767 dont_repeat ();
13768
c906108c
SS
13769 if (arg == 0)
13770 {
13771 int breaks_to_delete = 0;
13772
46c6471b
PA
13773 /* Delete all breakpoints if no argument. Do not delete
13774 internal breakpoints, these have to be deleted with an
13775 explicit breakpoint number argument. */
c5aa993b 13776 ALL_BREAKPOINTS (b)
46c6471b 13777 if (user_breakpoint_p (b))
973d738b
DJ
13778 {
13779 breaks_to_delete = 1;
13780 break;
13781 }
c906108c
SS
13782
13783 /* Ask user only if there are some breakpoints to delete. */
13784 if (!from_tty
e2e0b3e5 13785 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13786 {
35df4500 13787 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13788 if (user_breakpoint_p (b))
c5aa993b 13789 delete_breakpoint (b);
c906108c
SS
13790 }
13791 }
13792 else
51be5b68 13793 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13794}
13795
c2f4122d
PA
13796/* Return true if all locations of B bound to PSPACE are pending. If
13797 PSPACE is NULL, all locations of all program spaces are
13798 considered. */
13799
0d381245 13800static int
c2f4122d 13801all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13802{
c2f4122d
PA
13803 struct bp_location *loc;
13804
13805 for (loc = b->loc; loc != NULL; loc = loc->next)
13806 if ((pspace == NULL
13807 || loc->pspace == pspace)
13808 && !loc->shlib_disabled
8645ff69 13809 && !loc->pspace->executing_startup)
0d381245
VP
13810 return 0;
13811 return 1;
fe3f5fa8
VP
13812}
13813
776592bf
DE
13814/* Subroutine of update_breakpoint_locations to simplify it.
13815 Return non-zero if multiple fns in list LOC have the same name.
13816 Null names are ignored. */
13817
13818static int
13819ambiguous_names_p (struct bp_location *loc)
13820{
13821 struct bp_location *l;
13822 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13823 (int (*) (const void *,
13824 const void *)) streq,
776592bf
DE
13825 NULL, xcalloc, xfree);
13826
13827 for (l = loc; l != NULL; l = l->next)
13828 {
13829 const char **slot;
13830 const char *name = l->function_name;
13831
13832 /* Allow for some names to be NULL, ignore them. */
13833 if (name == NULL)
13834 continue;
13835
13836 slot = (const char **) htab_find_slot (htab, (const void *) name,
13837 INSERT);
4a64f543
MS
13838 /* NOTE: We can assume slot != NULL here because xcalloc never
13839 returns NULL. */
776592bf
DE
13840 if (*slot != NULL)
13841 {
13842 htab_delete (htab);
13843 return 1;
13844 }
13845 *slot = name;
13846 }
13847
13848 htab_delete (htab);
13849 return 0;
13850}
13851
0fb4aa4b
PA
13852/* When symbols change, it probably means the sources changed as well,
13853 and it might mean the static tracepoint markers are no longer at
13854 the same address or line numbers they used to be at last we
13855 checked. Losing your static tracepoints whenever you rebuild is
13856 undesirable. This function tries to resync/rematch gdb static
13857 tracepoints with the markers on the target, for static tracepoints
13858 that have not been set by marker id. Static tracepoint that have
13859 been set by marker id are reset by marker id in breakpoint_re_set.
13860 The heuristic is:
13861
13862 1) For a tracepoint set at a specific address, look for a marker at
13863 the old PC. If one is found there, assume to be the same marker.
13864 If the name / string id of the marker found is different from the
13865 previous known name, assume that means the user renamed the marker
13866 in the sources, and output a warning.
13867
13868 2) For a tracepoint set at a given line number, look for a marker
13869 at the new address of the old line number. If one is found there,
13870 assume to be the same marker. If the name / string id of the
13871 marker found is different from the previous known name, assume that
13872 means the user renamed the marker in the sources, and output a
13873 warning.
13874
13875 3) If a marker is no longer found at the same address or line, it
13876 may mean the marker no longer exists. But it may also just mean
13877 the code changed a bit. Maybe the user added a few lines of code
13878 that made the marker move up or down (in line number terms). Ask
13879 the target for info about the marker with the string id as we knew
13880 it. If found, update line number and address in the matching
13881 static tracepoint. This will get confused if there's more than one
13882 marker with the same ID (possible in UST, although unadvised
13883 precisely because it confuses tools). */
13884
13885static struct symtab_and_line
13886update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13887{
d9b3f62e 13888 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13889 struct static_tracepoint_marker marker;
13890 CORE_ADDR pc;
0fb4aa4b
PA
13891
13892 pc = sal.pc;
13893 if (sal.line)
13894 find_line_pc (sal.symtab, sal.line, &pc);
13895
13896 if (target_static_tracepoint_marker_at (pc, &marker))
13897 {
d9b3f62e 13898 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13899 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13900 b->number,
d9b3f62e 13901 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13902
d9b3f62e
PA
13903 xfree (tp->static_trace_marker_id);
13904 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13905 release_static_tracepoint_marker (&marker);
13906
13907 return sal;
13908 }
13909
13910 /* Old marker wasn't found on target at lineno. Try looking it up
13911 by string ID. */
13912 if (!sal.explicit_pc
13913 && sal.line != 0
13914 && sal.symtab != NULL
d9b3f62e 13915 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13916 {
13917 VEC(static_tracepoint_marker_p) *markers;
13918
13919 markers
d9b3f62e 13920 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13921
13922 if (!VEC_empty(static_tracepoint_marker_p, markers))
13923 {
80e1d417 13924 struct symtab_and_line sal2;
0fb4aa4b 13925 struct symbol *sym;
80e1d417 13926 struct static_tracepoint_marker *tpmarker;
79a45e25 13927 struct ui_out *uiout = current_uiout;
67994074 13928 struct explicit_location explicit_loc;
0fb4aa4b 13929
80e1d417 13930 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13931
d9b3f62e 13932 xfree (tp->static_trace_marker_id);
80e1d417 13933 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13934
13935 warning (_("marker for static tracepoint %d (%s) not "
13936 "found at previous line number"),
d9b3f62e 13937 b->number, tp->static_trace_marker_id);
0fb4aa4b 13938
80e1d417 13939 init_sal (&sal2);
0fb4aa4b 13940
80e1d417 13941 sal2.pc = tpmarker->address;
0fb4aa4b 13942
80e1d417
AS
13943 sal2 = find_pc_line (tpmarker->address, 0);
13944 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13945 uiout->text ("Now in ");
0fb4aa4b
PA
13946 if (sym)
13947 {
112e8700
SM
13948 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13949 uiout->text (" at ");
0fb4aa4b 13950 }
112e8700 13951 uiout->field_string ("file",
05cba821 13952 symtab_to_filename_for_display (sal2.symtab));
112e8700 13953 uiout->text (":");
0fb4aa4b 13954
112e8700 13955 if (uiout->is_mi_like_p ())
0fb4aa4b 13956 {
0b0865da 13957 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13958
112e8700 13959 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13960 }
13961
112e8700
SM
13962 uiout->field_int ("line", sal2.line);
13963 uiout->text ("\n");
0fb4aa4b 13964
80e1d417 13965 b->loc->line_number = sal2.line;
2f202fde 13966 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13967
d28cd78a 13968 b->location.reset (NULL);
67994074
KS
13969 initialize_explicit_location (&explicit_loc);
13970 explicit_loc.source_filename
00e52e53 13971 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13972 explicit_loc.line_offset.offset = b->loc->line_number;
13973 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13974 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13975
13976 /* Might be nice to check if function changed, and warn if
13977 so. */
13978
80e1d417 13979 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13980 }
13981 }
13982 return sal;
13983}
13984
8d3788bd
VP
13985/* Returns 1 iff locations A and B are sufficiently same that
13986 we don't need to report breakpoint as changed. */
13987
13988static int
13989locations_are_equal (struct bp_location *a, struct bp_location *b)
13990{
13991 while (a && b)
13992 {
13993 if (a->address != b->address)
13994 return 0;
13995
13996 if (a->shlib_disabled != b->shlib_disabled)
13997 return 0;
13998
13999 if (a->enabled != b->enabled)
14000 return 0;
14001
14002 a = a->next;
14003 b = b->next;
14004 }
14005
14006 if ((a == NULL) != (b == NULL))
14007 return 0;
14008
14009 return 1;
14010}
14011
c2f4122d
PA
14012/* Split all locations of B that are bound to PSPACE out of B's
14013 location list to a separate list and return that list's head. If
14014 PSPACE is NULL, hoist out all locations of B. */
14015
14016static struct bp_location *
14017hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14018{
14019 struct bp_location head;
14020 struct bp_location *i = b->loc;
14021 struct bp_location **i_link = &b->loc;
14022 struct bp_location *hoisted = &head;
14023
14024 if (pspace == NULL)
14025 {
14026 i = b->loc;
14027 b->loc = NULL;
14028 return i;
14029 }
14030
14031 head.next = NULL;
14032
14033 while (i != NULL)
14034 {
14035 if (i->pspace == pspace)
14036 {
14037 *i_link = i->next;
14038 i->next = NULL;
14039 hoisted->next = i;
14040 hoisted = i;
14041 }
14042 else
14043 i_link = &i->next;
14044 i = *i_link;
14045 }
14046
14047 return head.next;
14048}
14049
14050/* Create new breakpoint locations for B (a hardware or software
14051 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14052 zero, then B is a ranged breakpoint. Only recreates locations for
14053 FILTER_PSPACE. Locations of other program spaces are left
14054 untouched. */
f1310107 14055
0e30163f 14056void
0d381245 14057update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14058 struct program_space *filter_pspace,
f1310107
TJB
14059 struct symtabs_and_lines sals,
14060 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14061{
14062 int i;
c2f4122d 14063 struct bp_location *existing_locations;
0d381245 14064
f8eba3c6
TT
14065 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14066 {
14067 /* Ranged breakpoints have only one start location and one end
14068 location. */
14069 b->enable_state = bp_disabled;
f8eba3c6
TT
14070 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14071 "multiple locations found\n"),
14072 b->number);
14073 return;
14074 }
f1310107 14075
4a64f543
MS
14076 /* If there's no new locations, and all existing locations are
14077 pending, don't do anything. This optimizes the common case where
14078 all locations are in the same shared library, that was unloaded.
14079 We'd like to retain the location, so that when the library is
14080 loaded again, we don't loose the enabled/disabled status of the
14081 individual locations. */
c2f4122d 14082 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14083 return;
14084
c2f4122d 14085 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14086
0d381245 14087 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14088 {
f8eba3c6
TT
14089 struct bp_location *new_loc;
14090
14091 switch_to_program_space_and_thread (sals.sals[i].pspace);
14092
14093 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14094
0d381245
VP
14095 /* Reparse conditions, they might contain references to the
14096 old symtab. */
14097 if (b->cond_string != NULL)
14098 {
bbc13ae3 14099 const char *s;
fe3f5fa8 14100
0d381245 14101 s = b->cond_string;
492d29ea 14102 TRY
0d381245 14103 {
1bb9788d
TT
14104 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14105 block_for_pc (sals.sals[i].pc),
0d381245
VP
14106 0);
14107 }
492d29ea 14108 CATCH (e, RETURN_MASK_ERROR)
0d381245 14109 {
3e43a32a
MS
14110 warning (_("failed to reevaluate condition "
14111 "for breakpoint %d: %s"),
0d381245
VP
14112 b->number, e.message);
14113 new_loc->enabled = 0;
14114 }
492d29ea 14115 END_CATCH
0d381245 14116 }
fe3f5fa8 14117
f1310107
TJB
14118 if (sals_end.nelts)
14119 {
14120 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14121
14122 new_loc->length = end - sals.sals[0].pc + 1;
14123 }
0d381245 14124 }
fe3f5fa8 14125
4a64f543
MS
14126 /* If possible, carry over 'disable' status from existing
14127 breakpoints. */
0d381245
VP
14128 {
14129 struct bp_location *e = existing_locations;
776592bf
DE
14130 /* If there are multiple breakpoints with the same function name,
14131 e.g. for inline functions, comparing function names won't work.
14132 Instead compare pc addresses; this is just a heuristic as things
14133 may have moved, but in practice it gives the correct answer
14134 often enough until a better solution is found. */
14135 int have_ambiguous_names = ambiguous_names_p (b->loc);
14136
0d381245
VP
14137 for (; e; e = e->next)
14138 {
14139 if (!e->enabled && e->function_name)
14140 {
14141 struct bp_location *l = b->loc;
776592bf
DE
14142 if (have_ambiguous_names)
14143 {
14144 for (; l; l = l->next)
f1310107 14145 if (breakpoint_locations_match (e, l))
776592bf
DE
14146 {
14147 l->enabled = 0;
14148 break;
14149 }
14150 }
14151 else
14152 {
14153 for (; l; l = l->next)
14154 if (l->function_name
14155 && strcmp (e->function_name, l->function_name) == 0)
14156 {
14157 l->enabled = 0;
14158 break;
14159 }
14160 }
0d381245
VP
14161 }
14162 }
14163 }
fe3f5fa8 14164
8d3788bd
VP
14165 if (!locations_are_equal (existing_locations, b->loc))
14166 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14167}
14168
f00aae0f 14169/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14170 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14171
14172static struct symtabs_and_lines
f00aae0f 14173location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14174 struct program_space *search_pspace, int *found)
ef23e705 14175{
02d20e4a 14176 struct symtabs_and_lines sals = {0};
492d29ea 14177 struct gdb_exception exception = exception_none;
ef23e705 14178
983af33b 14179 gdb_assert (b->ops != NULL);
ef23e705 14180
492d29ea 14181 TRY
ef23e705 14182 {
c2f4122d 14183 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14184 }
492d29ea 14185 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14186 {
14187 int not_found_and_ok = 0;
492d29ea
PA
14188
14189 exception = e;
14190
ef23e705
TJB
14191 /* For pending breakpoints, it's expected that parsing will
14192 fail until the right shared library is loaded. User has
14193 already told to create pending breakpoints and don't need
14194 extra messages. If breakpoint is in bp_shlib_disabled
14195 state, then user already saw the message about that
14196 breakpoint being disabled, and don't want to see more
14197 errors. */
58438ac1 14198 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14199 && (b->condition_not_parsed
14200 || (b->loc != NULL
14201 && search_pspace != NULL
14202 && b->loc->pspace != search_pspace)
ef23e705 14203 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14204 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14205 || b->enable_state == bp_disabled))
14206 not_found_and_ok = 1;
14207
14208 if (!not_found_and_ok)
14209 {
14210 /* We surely don't want to warn about the same breakpoint
14211 10 times. One solution, implemented here, is disable
14212 the breakpoint on error. Another solution would be to
14213 have separate 'warning emitted' flag. Since this
14214 happens only when a binary has changed, I don't know
14215 which approach is better. */
14216 b->enable_state = bp_disabled;
14217 throw_exception (e);
14218 }
14219 }
492d29ea 14220 END_CATCH
ef23e705 14221
492d29ea 14222 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14223 {
f8eba3c6 14224 int i;
ef23e705 14225
f8eba3c6
TT
14226 for (i = 0; i < sals.nelts; ++i)
14227 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14228 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14229 {
ed1d1739
KS
14230 char *cond_string, *extra_string;
14231 int thread, task;
ef23e705 14232
f00aae0f 14233 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14234 &cond_string, &thread, &task,
14235 &extra_string);
f00aae0f 14236 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14237 if (cond_string)
14238 b->cond_string = cond_string;
14239 b->thread = thread;
14240 b->task = task;
e7e0cddf 14241 if (extra_string)
f00aae0f
KS
14242 {
14243 xfree (b->extra_string);
14244 b->extra_string = extra_string;
14245 }
ef23e705
TJB
14246 b->condition_not_parsed = 0;
14247 }
14248
983af33b 14249 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14250 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14251
58438ac1
TT
14252 *found = 1;
14253 }
14254 else
14255 *found = 0;
ef23e705
TJB
14256
14257 return sals;
14258}
14259
348d480f
PA
14260/* The default re_set method, for typical hardware or software
14261 breakpoints. Reevaluate the breakpoint and recreate its
14262 locations. */
14263
14264static void
28010a5d 14265breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14266{
14267 int found;
f1310107 14268 struct symtabs_and_lines sals, sals_end;
ef23e705 14269 struct symtabs_and_lines expanded = {0};
f1310107 14270 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14271 struct program_space *filter_pspace = current_program_space;
ef23e705 14272
d28cd78a 14273 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14274 if (found)
14275 {
14276 make_cleanup (xfree, sals.sals);
f8eba3c6 14277 expanded = sals;
ef23e705
TJB
14278 }
14279
f00aae0f 14280 if (b->location_range_end != NULL)
f1310107 14281 {
d28cd78a 14282 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14283 filter_pspace, &found);
f1310107
TJB
14284 if (found)
14285 {
14286 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14287 expanded_end = sals_end;
f1310107
TJB
14288 }
14289 }
14290
c2f4122d 14291 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14292}
14293
983af33b
SDJ
14294/* Default method for creating SALs from an address string. It basically
14295 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14296
14297static void
f00aae0f
KS
14298create_sals_from_location_default (const struct event_location *location,
14299 struct linespec_result *canonical,
14300 enum bptype type_wanted)
983af33b 14301{
f00aae0f 14302 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14303}
14304
14305/* Call create_breakpoints_sal for the given arguments. This is the default
14306 function for the `create_breakpoints_sal' method of
14307 breakpoint_ops. */
14308
14309static void
14310create_breakpoints_sal_default (struct gdbarch *gdbarch,
14311 struct linespec_result *canonical,
e1e01040
PA
14312 gdb::unique_xmalloc_ptr<char> cond_string,
14313 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14314 enum bptype type_wanted,
14315 enum bpdisp disposition,
14316 int thread,
14317 int task, int ignore_count,
14318 const struct breakpoint_ops *ops,
14319 int from_tty, int enabled,
44f238bb 14320 int internal, unsigned flags)
983af33b 14321{
e1e01040
PA
14322 create_breakpoints_sal (gdbarch, canonical,
14323 std::move (cond_string),
14324 std::move (extra_string),
983af33b
SDJ
14325 type_wanted, disposition,
14326 thread, task, ignore_count, ops, from_tty,
44f238bb 14327 enabled, internal, flags);
983af33b
SDJ
14328}
14329
14330/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14331 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14332
14333static void
f00aae0f
KS
14334decode_location_default (struct breakpoint *b,
14335 const struct event_location *location,
c2f4122d 14336 struct program_space *search_pspace,
983af33b
SDJ
14337 struct symtabs_and_lines *sals)
14338{
14339 struct linespec_result canonical;
14340
c2f4122d 14341 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14342 (struct symtab *) NULL, 0,
14343 &canonical, multiple_symbols_all,
14344 b->filter);
14345
14346 /* We should get 0 or 1 resulting SALs. */
14347 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14348
14349 if (VEC_length (linespec_sals, canonical.sals) > 0)
14350 {
14351 struct linespec_sals *lsal;
14352
14353 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14354 *sals = lsal->sals;
14355 /* Arrange it so the destructor does not free the
14356 contents. */
14357 lsal->sals.sals = NULL;
14358 }
983af33b
SDJ
14359}
14360
28010a5d
PA
14361/* Prepare the global context for a re-set of breakpoint B. */
14362
14363static struct cleanup *
14364prepare_re_set_context (struct breakpoint *b)
14365{
28010a5d 14366 input_radix = b->input_radix;
28010a5d
PA
14367 set_language (b->language);
14368
c2f4122d 14369 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14370}
14371
c906108c
SS
14372/* Reset a breakpoint given it's struct breakpoint * BINT.
14373 The value we return ends up being the return value from catch_errors.
14374 Unused in this case. */
14375
14376static int
4efb68b1 14377breakpoint_re_set_one (void *bint)
c906108c 14378{
4a64f543 14379 /* Get past catch_errs. */
53a5351d 14380 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14381 struct cleanup *cleanups;
c906108c 14382
348d480f
PA
14383 cleanups = prepare_re_set_context (b);
14384 b->ops->re_set (b);
14385 do_cleanups (cleanups);
c906108c
SS
14386 return 0;
14387}
14388
c2f4122d
PA
14389/* Re-set breakpoint locations for the current program space.
14390 Locations bound to other program spaces are left untouched. */
14391
c906108c 14392void
69de3c6a 14393breakpoint_re_set (void)
c906108c 14394{
35df4500 14395 struct breakpoint *b, *b_tmp;
c906108c
SS
14396 enum language save_language;
14397 int save_input_radix;
c5aa993b 14398
c906108c
SS
14399 save_language = current_language->la_language;
14400 save_input_radix = input_radix;
2a7f3dff 14401
c5aa993b 14402 {
5ed8105e 14403 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14404
5ed8105e
PA
14405 /* Note: we must not try to insert locations until after all
14406 breakpoints have been re-set. Otherwise, e.g., when re-setting
14407 breakpoint 1, we'd insert the locations of breakpoint 2, which
14408 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14409
5ed8105e
PA
14410 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14411 {
14412 /* Format possible error msg. */
14413 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14414 b->number);
14415 struct cleanup *cleanups = make_cleanup (xfree, message);
14416 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14417 do_cleanups (cleanups);
14418 }
14419 set_language (save_language);
14420 input_radix = save_input_radix;
14421
14422 jit_breakpoint_re_set ();
14423 }
6c95b8df 14424
af02033e
PP
14425 create_overlay_event_breakpoint ();
14426 create_longjmp_master_breakpoint ();
14427 create_std_terminate_master_breakpoint ();
186c406b 14428 create_exception_master_breakpoint ();
2a7f3dff
PA
14429
14430 /* Now we can insert. */
14431 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14432}
14433\f
c906108c
SS
14434/* Reset the thread number of this breakpoint:
14435
14436 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14437 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14438void
fba45db2 14439breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14440{
14441 if (b->thread != -1)
14442 {
39f77062 14443 if (in_thread_list (inferior_ptid))
5d5658a1 14444 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14445
14446 /* We're being called after following a fork. The new fork is
14447 selected as current, and unless this was a vfork will have a
14448 different program space from the original thread. Reset that
14449 as well. */
14450 b->loc->pspace = current_program_space;
c906108c
SS
14451 }
14452}
14453
03ac34d5
MS
14454/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14455 If from_tty is nonzero, it prints a message to that effect,
14456 which ends with a period (no newline). */
14457
c906108c 14458void
fba45db2 14459set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14460{
52f0bd74 14461 struct breakpoint *b;
c906108c
SS
14462
14463 if (count < 0)
14464 count = 0;
14465
14466 ALL_BREAKPOINTS (b)
14467 if (b->number == bptnum)
c5aa993b 14468 {
d77f58be
SS
14469 if (is_tracepoint (b))
14470 {
14471 if (from_tty && count != 0)
14472 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14473 bptnum);
14474 return;
14475 }
14476
c5aa993b 14477 b->ignore_count = count;
221ea385
KS
14478 if (from_tty)
14479 {
14480 if (count == 0)
3e43a32a
MS
14481 printf_filtered (_("Will stop next time "
14482 "breakpoint %d is reached."),
221ea385
KS
14483 bptnum);
14484 else if (count == 1)
a3f17187 14485 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14486 bptnum);
14487 else
3e43a32a
MS
14488 printf_filtered (_("Will ignore next %d "
14489 "crossings of breakpoint %d."),
221ea385
KS
14490 count, bptnum);
14491 }
8d3788bd 14492 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14493 return;
14494 }
c906108c 14495
8a3fe4f8 14496 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14497}
14498
c906108c
SS
14499/* Command to set ignore-count of breakpoint N to COUNT. */
14500
14501static void
fba45db2 14502ignore_command (char *args, int from_tty)
c906108c
SS
14503{
14504 char *p = args;
52f0bd74 14505 int num;
c906108c
SS
14506
14507 if (p == 0)
e2e0b3e5 14508 error_no_arg (_("a breakpoint number"));
c5aa993b 14509
c906108c 14510 num = get_number (&p);
5c44784c 14511 if (num == 0)
8a3fe4f8 14512 error (_("bad breakpoint number: '%s'"), args);
c906108c 14513 if (*p == 0)
8a3fe4f8 14514 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14515
14516 set_ignore_count (num,
14517 longest_to_int (value_as_long (parse_and_eval (p))),
14518 from_tty);
221ea385
KS
14519 if (from_tty)
14520 printf_filtered ("\n");
c906108c
SS
14521}
14522\f
14523/* Call FUNCTION on each of the breakpoints
14524 whose numbers are given in ARGS. */
14525
14526static void
896b6bda
PA
14527map_breakpoint_numbers (const char *args,
14528 void (*function) (struct breakpoint *,
14529 void *),
95a42b64 14530 void *data)
c906108c 14531{
52f0bd74
AC
14532 int num;
14533 struct breakpoint *b, *tmp;
c906108c 14534
b9d61307 14535 if (args == 0 || *args == '\0')
e2e0b3e5 14536 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14537
bfd28288 14538 number_or_range_parser parser (args);
197f0a60 14539
bfd28288 14540 while (!parser.finished ())
c906108c 14541 {
bfd28288
PA
14542 const char *p = parser.cur_tok ();
14543 bool match = false;
197f0a60 14544
bfd28288 14545 num = parser.get_number ();
5c44784c 14546 if (num == 0)
c5aa993b 14547 {
8a3fe4f8 14548 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14549 }
14550 else
14551 {
14552 ALL_BREAKPOINTS_SAFE (b, tmp)
14553 if (b->number == num)
14554 {
bfd28288 14555 match = true;
cdac0397 14556 function (b, data);
11cf8741 14557 break;
5c44784c 14558 }
bfd28288 14559 if (!match)
a3f17187 14560 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14561 }
c906108c
SS
14562 }
14563}
14564
0d381245
VP
14565static struct bp_location *
14566find_location_by_number (char *number)
14567{
14568 char *dot = strchr (number, '.');
14569 char *p1;
14570 int bp_num;
14571 int loc_num;
14572 struct breakpoint *b;
14573 struct bp_location *loc;
14574
14575 *dot = '\0';
14576
14577 p1 = number;
197f0a60 14578 bp_num = get_number (&p1);
0d381245
VP
14579 if (bp_num == 0)
14580 error (_("Bad breakpoint number '%s'"), number);
14581
14582 ALL_BREAKPOINTS (b)
14583 if (b->number == bp_num)
14584 {
14585 break;
14586 }
14587
14588 if (!b || b->number != bp_num)
14589 error (_("Bad breakpoint number '%s'"), number);
14590
14591 p1 = dot+1;
197f0a60 14592 loc_num = get_number (&p1);
0d381245
VP
14593 if (loc_num == 0)
14594 error (_("Bad breakpoint location number '%s'"), number);
14595
14596 --loc_num;
14597 loc = b->loc;
14598 for (;loc_num && loc; --loc_num, loc = loc->next)
14599 ;
14600 if (!loc)
14601 error (_("Bad breakpoint location number '%s'"), dot+1);
14602
14603 return loc;
14604}
14605
14606
1900040c
MS
14607/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14608 If from_tty is nonzero, it prints a message to that effect,
14609 which ends with a period (no newline). */
14610
c906108c 14611void
fba45db2 14612disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14613{
14614 /* Never disable a watchpoint scope breakpoint; we want to
14615 hit them when we leave scope so we can delete both the
14616 watchpoint and its scope breakpoint at that time. */
14617 if (bpt->type == bp_watchpoint_scope)
14618 return;
14619
b5de0fa7 14620 bpt->enable_state = bp_disabled;
c906108c 14621
b775012e
LM
14622 /* Mark breakpoint locations modified. */
14623 mark_breakpoint_modified (bpt);
14624
d248b706
KY
14625 if (target_supports_enable_disable_tracepoint ()
14626 && current_trace_status ()->running && is_tracepoint (bpt))
14627 {
14628 struct bp_location *location;
14629
14630 for (location = bpt->loc; location; location = location->next)
14631 target_disable_tracepoint (location);
14632 }
14633
44702360 14634 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14635
8d3788bd 14636 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14637}
14638
51be5b68
PA
14639/* A callback for iterate_over_related_breakpoints. */
14640
14641static void
14642do_disable_breakpoint (struct breakpoint *b, void *ignore)
14643{
14644 disable_breakpoint (b);
14645}
14646
95a42b64
TT
14647/* A callback for map_breakpoint_numbers that calls
14648 disable_breakpoint. */
14649
14650static void
14651do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14652{
51be5b68 14653 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14654}
14655
c906108c 14656static void
fba45db2 14657disable_command (char *args, int from_tty)
c906108c 14658{
c906108c 14659 if (args == 0)
46c6471b
PA
14660 {
14661 struct breakpoint *bpt;
14662
14663 ALL_BREAKPOINTS (bpt)
14664 if (user_breakpoint_p (bpt))
14665 disable_breakpoint (bpt);
14666 }
9eaabc75 14667 else
0d381245 14668 {
9eaabc75
MW
14669 char *num = extract_arg (&args);
14670
14671 while (num)
d248b706 14672 {
9eaabc75 14673 if (strchr (num, '.'))
b775012e 14674 {
9eaabc75
MW
14675 struct bp_location *loc = find_location_by_number (num);
14676
14677 if (loc)
14678 {
14679 if (loc->enabled)
14680 {
14681 loc->enabled = 0;
14682 mark_breakpoint_location_modified (loc);
14683 }
14684 if (target_supports_enable_disable_tracepoint ()
14685 && current_trace_status ()->running && loc->owner
14686 && is_tracepoint (loc->owner))
14687 target_disable_tracepoint (loc);
14688 }
44702360 14689 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14690 }
9eaabc75
MW
14691 else
14692 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14693 num = extract_arg (&args);
d248b706 14694 }
0d381245 14695 }
c906108c
SS
14696}
14697
14698static void
816338b5
SS
14699enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14700 int count)
c906108c 14701{
afe38095 14702 int target_resources_ok;
c906108c
SS
14703
14704 if (bpt->type == bp_hardware_breakpoint)
14705 {
14706 int i;
c5aa993b 14707 i = hw_breakpoint_used_count ();
53a5351d 14708 target_resources_ok =
d92524f1 14709 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14710 i + 1, 0);
c906108c 14711 if (target_resources_ok == 0)
8a3fe4f8 14712 error (_("No hardware breakpoint support in the target."));
c906108c 14713 else if (target_resources_ok < 0)
8a3fe4f8 14714 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14715 }
14716
cc60f2e3 14717 if (is_watchpoint (bpt))
c906108c 14718 {
d07205c2 14719 /* Initialize it just to avoid a GCC false warning. */
f486487f 14720 enum enable_state orig_enable_state = bp_disabled;
dde02812 14721
492d29ea 14722 TRY
c906108c 14723 {
3a5c3e22
PA
14724 struct watchpoint *w = (struct watchpoint *) bpt;
14725
1e718ff1
TJB
14726 orig_enable_state = bpt->enable_state;
14727 bpt->enable_state = bp_enabled;
3a5c3e22 14728 update_watchpoint (w, 1 /* reparse */);
c906108c 14729 }
492d29ea 14730 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14731 {
1e718ff1 14732 bpt->enable_state = orig_enable_state;
dde02812
ES
14733 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14734 bpt->number);
14735 return;
c5aa993b 14736 }
492d29ea 14737 END_CATCH
c906108c 14738 }
0101ce28 14739
b775012e
LM
14740 bpt->enable_state = bp_enabled;
14741
14742 /* Mark breakpoint locations modified. */
14743 mark_breakpoint_modified (bpt);
14744
d248b706
KY
14745 if (target_supports_enable_disable_tracepoint ()
14746 && current_trace_status ()->running && is_tracepoint (bpt))
14747 {
14748 struct bp_location *location;
14749
14750 for (location = bpt->loc; location; location = location->next)
14751 target_enable_tracepoint (location);
14752 }
14753
b4c291bb 14754 bpt->disposition = disposition;
816338b5 14755 bpt->enable_count = count;
44702360 14756 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14757
8d3788bd 14758 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14759}
14760
fe3f5fa8 14761
c906108c 14762void
fba45db2 14763enable_breakpoint (struct breakpoint *bpt)
c906108c 14764{
816338b5 14765 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14766}
14767
14768static void
14769do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14770{
14771 enable_breakpoint (bpt);
c906108c
SS
14772}
14773
95a42b64
TT
14774/* A callback for map_breakpoint_numbers that calls
14775 enable_breakpoint. */
14776
14777static void
14778do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14779{
51be5b68 14780 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14781}
14782
c906108c
SS
14783/* The enable command enables the specified breakpoints (or all defined
14784 breakpoints) so they once again become (or continue to be) effective
1272ad14 14785 in stopping the inferior. */
c906108c 14786
c906108c 14787static void
fba45db2 14788enable_command (char *args, int from_tty)
c906108c 14789{
c906108c 14790 if (args == 0)
46c6471b
PA
14791 {
14792 struct breakpoint *bpt;
14793
14794 ALL_BREAKPOINTS (bpt)
14795 if (user_breakpoint_p (bpt))
14796 enable_breakpoint (bpt);
14797 }
9eaabc75 14798 else
0d381245 14799 {
9eaabc75
MW
14800 char *num = extract_arg (&args);
14801
14802 while (num)
d248b706 14803 {
9eaabc75 14804 if (strchr (num, '.'))
b775012e 14805 {
9eaabc75
MW
14806 struct bp_location *loc = find_location_by_number (num);
14807
14808 if (loc)
14809 {
14810 if (!loc->enabled)
14811 {
14812 loc->enabled = 1;
14813 mark_breakpoint_location_modified (loc);
14814 }
14815 if (target_supports_enable_disable_tracepoint ()
14816 && current_trace_status ()->running && loc->owner
14817 && is_tracepoint (loc->owner))
14818 target_enable_tracepoint (loc);
14819 }
44702360 14820 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14821 }
9eaabc75
MW
14822 else
14823 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14824 num = extract_arg (&args);
d248b706 14825 }
0d381245 14826 }
c906108c
SS
14827}
14828
816338b5
SS
14829/* This struct packages up disposition data for application to multiple
14830 breakpoints. */
14831
14832struct disp_data
14833{
14834 enum bpdisp disp;
14835 int count;
14836};
14837
c906108c 14838static void
51be5b68
PA
14839do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14840{
816338b5 14841 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14842
816338b5 14843 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14844}
14845
14846static void
14847do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14848{
816338b5 14849 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14850
14851 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14852}
14853
c906108c 14854static void
fba45db2 14855enable_once_command (char *args, int from_tty)
c906108c 14856{
51be5b68 14857 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14858}
14859
816338b5
SS
14860static void
14861do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14862{
14863 struct disp_data disp = { disp_disable, *(int *) countptr };
14864
14865 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14866}
14867
14868static void
14869enable_count_command (char *args, int from_tty)
14870{
b9d61307
SM
14871 int count;
14872
14873 if (args == NULL)
14874 error_no_arg (_("hit count"));
14875
14876 count = get_number (&args);
816338b5
SS
14877
14878 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14879}
14880
c906108c 14881static void
51be5b68 14882do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14883{
816338b5 14884 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14885
14886 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14887}
14888
c906108c 14889static void
fba45db2 14890enable_delete_command (char *args, int from_tty)
c906108c 14891{
51be5b68 14892 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14893}
14894\f
fa8d40ab
JJ
14895static void
14896set_breakpoint_cmd (char *args, int from_tty)
14897{
14898}
14899
14900static void
14901show_breakpoint_cmd (char *args, int from_tty)
14902{
14903}
14904
1f3b5d1b
PP
14905/* Invalidate last known value of any hardware watchpoint if
14906 the memory which that value represents has been written to by
14907 GDB itself. */
14908
14909static void
8de0566d
YQ
14910invalidate_bp_value_on_memory_change (struct inferior *inferior,
14911 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14912 const bfd_byte *data)
14913{
14914 struct breakpoint *bp;
14915
14916 ALL_BREAKPOINTS (bp)
14917 if (bp->enable_state == bp_enabled
3a5c3e22 14918 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14919 {
3a5c3e22 14920 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14921
3a5c3e22
PA
14922 if (wp->val_valid && wp->val)
14923 {
14924 struct bp_location *loc;
14925
14926 for (loc = bp->loc; loc != NULL; loc = loc->next)
14927 if (loc->loc_type == bp_loc_hardware_watchpoint
14928 && loc->address + loc->length > addr
14929 && addr + len > loc->address)
14930 {
14931 value_free (wp->val);
14932 wp->val = NULL;
14933 wp->val_valid = 0;
14934 }
14935 }
1f3b5d1b
PP
14936 }
14937}
14938
8181d85f
DJ
14939/* Create and insert a breakpoint for software single step. */
14940
14941void
6c95b8df 14942insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14943 struct address_space *aspace,
14944 CORE_ADDR next_pc)
8181d85f 14945{
7c16b83e
PA
14946 struct thread_info *tp = inferior_thread ();
14947 struct symtab_and_line sal;
14948 CORE_ADDR pc = next_pc;
8181d85f 14949
34b7e8a6
PA
14950 if (tp->control.single_step_breakpoints == NULL)
14951 {
14952 tp->control.single_step_breakpoints
5d5658a1 14953 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14954 }
8181d85f 14955
7c16b83e
PA
14956 sal = find_pc_line (pc, 0);
14957 sal.pc = pc;
14958 sal.section = find_pc_overlay (pc);
14959 sal.explicit_pc = 1;
34b7e8a6 14960 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14961
7c16b83e 14962 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14963}
14964
93f9a11f
YQ
14965/* Insert single step breakpoints according to the current state. */
14966
14967int
14968insert_single_step_breakpoints (struct gdbarch *gdbarch)
14969{
f5ea389a 14970 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14971 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14972
f5ea389a 14973 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14974
a0ff9e1a 14975 if (!next_pcs.empty ())
93f9a11f 14976 {
f5ea389a 14977 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14978 struct address_space *aspace = get_frame_address_space (frame);
14979
a0ff9e1a 14980 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14981 insert_single_step_breakpoint (gdbarch, aspace, pc);
14982
93f9a11f
YQ
14983 return 1;
14984 }
14985 else
14986 return 0;
14987}
14988
34b7e8a6 14989/* See breakpoint.h. */
f02253f1
HZ
14990
14991int
7c16b83e
PA
14992breakpoint_has_location_inserted_here (struct breakpoint *bp,
14993 struct address_space *aspace,
14994 CORE_ADDR pc)
1aafd4da 14995{
7c16b83e 14996 struct bp_location *loc;
1aafd4da 14997
7c16b83e
PA
14998 for (loc = bp->loc; loc != NULL; loc = loc->next)
14999 if (loc->inserted
15000 && breakpoint_location_address_match (loc, aspace, pc))
15001 return 1;
1aafd4da 15002
7c16b83e 15003 return 0;
ef370185
JB
15004}
15005
15006/* Check whether a software single-step breakpoint is inserted at
15007 PC. */
15008
15009int
15010single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15011 CORE_ADDR pc)
15012{
34b7e8a6
PA
15013 struct breakpoint *bpt;
15014
15015 ALL_BREAKPOINTS (bpt)
15016 {
15017 if (bpt->type == bp_single_step
15018 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15019 return 1;
15020 }
15021 return 0;
1aafd4da
UW
15022}
15023
1042e4c0
SS
15024/* Tracepoint-specific operations. */
15025
15026/* Set tracepoint count to NUM. */
15027static void
15028set_tracepoint_count (int num)
15029{
15030 tracepoint_count = num;
4fa62494 15031 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15032}
15033
70221824 15034static void
1042e4c0
SS
15035trace_command (char *arg, int from_tty)
15036{
55aa24fb 15037 struct breakpoint_ops *ops;
55aa24fb 15038
ffc2605c
TT
15039 event_location_up location = string_to_event_location (&arg,
15040 current_language);
5b56227b 15041 if (location != NULL
ffc2605c 15042 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15043 ops = &tracepoint_probe_breakpoint_ops;
15044 else
15045 ops = &tracepoint_breakpoint_ops;
15046
558a9d82 15047 create_breakpoint (get_current_arch (),
ffc2605c 15048 location.get (),
f00aae0f 15049 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15050 0 /* tempflag */,
15051 bp_tracepoint /* type_wanted */,
15052 0 /* Ignore count */,
15053 pending_break_support,
15054 ops,
15055 from_tty,
15056 1 /* enabled */,
15057 0 /* internal */, 0);
1042e4c0
SS
15058}
15059
70221824 15060static void
7a697b8d
SS
15061ftrace_command (char *arg, int from_tty)
15062{
ffc2605c
TT
15063 event_location_up location = string_to_event_location (&arg,
15064 current_language);
558a9d82 15065 create_breakpoint (get_current_arch (),
ffc2605c 15066 location.get (),
f00aae0f 15067 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15068 0 /* tempflag */,
15069 bp_fast_tracepoint /* type_wanted */,
15070 0 /* Ignore count */,
15071 pending_break_support,
15072 &tracepoint_breakpoint_ops,
15073 from_tty,
15074 1 /* enabled */,
15075 0 /* internal */, 0);
0fb4aa4b
PA
15076}
15077
15078/* strace command implementation. Creates a static tracepoint. */
15079
70221824 15080static void
0fb4aa4b
PA
15081strace_command (char *arg, int from_tty)
15082{
983af33b 15083 struct breakpoint_ops *ops;
ffc2605c 15084 event_location_up location;
f00aae0f 15085 struct cleanup *back_to;
983af33b
SDJ
15086
15087 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15088 or with a normal static tracepoint. */
61012eef 15089 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15090 {
15091 ops = &strace_marker_breakpoint_ops;
15092 location = new_linespec_location (&arg);
15093 }
983af33b 15094 else
f00aae0f
KS
15095 {
15096 ops = &tracepoint_breakpoint_ops;
15097 location = string_to_event_location (&arg, current_language);
15098 }
983af33b 15099
558a9d82 15100 create_breakpoint (get_current_arch (),
ffc2605c 15101 location.get (),
f00aae0f 15102 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15103 0 /* tempflag */,
15104 bp_static_tracepoint /* type_wanted */,
15105 0 /* Ignore count */,
15106 pending_break_support,
15107 ops,
15108 from_tty,
15109 1 /* enabled */,
15110 0 /* internal */, 0);
7a697b8d
SS
15111}
15112
409873ef
SS
15113/* Set up a fake reader function that gets command lines from a linked
15114 list that was acquired during tracepoint uploading. */
15115
15116static struct uploaded_tp *this_utp;
3149d8c1 15117static int next_cmd;
409873ef
SS
15118
15119static char *
15120read_uploaded_action (void)
15121{
15122 char *rslt;
15123
3149d8c1 15124 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15125
3149d8c1 15126 next_cmd++;
409873ef
SS
15127
15128 return rslt;
15129}
15130
00bf0b85
SS
15131/* Given information about a tracepoint as recorded on a target (which
15132 can be either a live system or a trace file), attempt to create an
15133 equivalent GDB tracepoint. This is not a reliable process, since
15134 the target does not necessarily have all the information used when
15135 the tracepoint was originally defined. */
15136
d9b3f62e 15137struct tracepoint *
00bf0b85 15138create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15139{
409873ef 15140 char *addr_str, small_buf[100];
d9b3f62e 15141 struct tracepoint *tp;
fd9b8c24 15142
409873ef
SS
15143 if (utp->at_string)
15144 addr_str = utp->at_string;
15145 else
15146 {
15147 /* In the absence of a source location, fall back to raw
15148 address. Since there is no way to confirm that the address
15149 means the same thing as when the trace was started, warn the
15150 user. */
3e43a32a
MS
15151 warning (_("Uploaded tracepoint %d has no "
15152 "source location, using raw address"),
409873ef 15153 utp->number);
8c042590 15154 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15155 addr_str = small_buf;
15156 }
15157
15158 /* There's not much we can do with a sequence of bytecodes. */
15159 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15160 warning (_("Uploaded tracepoint %d condition "
15161 "has no source form, ignoring it"),
409873ef 15162 utp->number);
d5551862 15163
ffc2605c
TT
15164 event_location_up location = string_to_event_location (&addr_str,
15165 current_language);
8cdf0e15 15166 if (!create_breakpoint (get_current_arch (),
ffc2605c 15167 location.get (),
f00aae0f 15168 utp->cond_string, -1, addr_str,
e7e0cddf 15169 0 /* parse cond/thread */,
8cdf0e15 15170 0 /* tempflag */,
0fb4aa4b 15171 utp->type /* type_wanted */,
8cdf0e15
VP
15172 0 /* Ignore count */,
15173 pending_break_support,
348d480f 15174 &tracepoint_breakpoint_ops,
8cdf0e15 15175 0 /* from_tty */,
84f4c1fe 15176 utp->enabled /* enabled */,
44f238bb
PA
15177 0 /* internal */,
15178 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15179 return NULL;
fd9b8c24 15180
409873ef 15181 /* Get the tracepoint we just created. */
fd9b8c24
PA
15182 tp = get_tracepoint (tracepoint_count);
15183 gdb_assert (tp != NULL);
d5551862 15184
00bf0b85
SS
15185 if (utp->pass > 0)
15186 {
8c042590 15187 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 15188 tp->number);
00bf0b85 15189
409873ef 15190 trace_pass_command (small_buf, 0);
00bf0b85
SS
15191 }
15192
409873ef
SS
15193 /* If we have uploaded versions of the original commands, set up a
15194 special-purpose "reader" function and call the usual command line
15195 reader, then pass the result to the breakpoint command-setting
15196 function. */
3149d8c1 15197 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15198 {
93921405 15199 command_line_up cmd_list;
00bf0b85 15200
409873ef 15201 this_utp = utp;
3149d8c1 15202 next_cmd = 0;
d5551862 15203
409873ef
SS
15204 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15205
c1fc2657 15206 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 15207 }
3149d8c1
SS
15208 else if (!VEC_empty (char_ptr, utp->actions)
15209 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15210 warning (_("Uploaded tracepoint %d actions "
15211 "have no source form, ignoring them"),
409873ef 15212 utp->number);
00bf0b85 15213
f196051f 15214 /* Copy any status information that might be available. */
c1fc2657 15215 tp->hit_count = utp->hit_count;
f196051f
SS
15216 tp->traceframe_usage = utp->traceframe_usage;
15217
00bf0b85 15218 return tp;
d9b3f62e 15219}
00bf0b85 15220
1042e4c0
SS
15221/* Print information on tracepoint number TPNUM_EXP, or all if
15222 omitted. */
15223
15224static void
e5a67952 15225tracepoints_info (char *args, int from_tty)
1042e4c0 15226{
79a45e25 15227 struct ui_out *uiout = current_uiout;
e5a67952 15228 int num_printed;
1042e4c0 15229
e5a67952 15230 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15231
15232 if (num_printed == 0)
1042e4c0 15233 {
e5a67952 15234 if (args == NULL || *args == '\0')
112e8700 15235 uiout->message ("No tracepoints.\n");
d77f58be 15236 else
112e8700 15237 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15238 }
ad443146
SS
15239
15240 default_collect_info ();
1042e4c0
SS
15241}
15242
4a64f543 15243/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15244 Not supported by all targets. */
15245static void
15246enable_trace_command (char *args, int from_tty)
15247{
15248 enable_command (args, from_tty);
15249}
15250
4a64f543 15251/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15252 Not supported by all targets. */
15253static void
15254disable_trace_command (char *args, int from_tty)
15255{
15256 disable_command (args, from_tty);
15257}
15258
4a64f543 15259/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15260static void
15261delete_trace_command (char *arg, int from_tty)
15262{
35df4500 15263 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15264
15265 dont_repeat ();
15266
15267 if (arg == 0)
15268 {
15269 int breaks_to_delete = 0;
15270
15271 /* Delete all breakpoints if no argument.
15272 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15273 have to be deleted with an explicit breakpoint number
15274 argument. */
1042e4c0 15275 ALL_TRACEPOINTS (b)
46c6471b 15276 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15277 {
15278 breaks_to_delete = 1;
15279 break;
15280 }
1042e4c0
SS
15281
15282 /* Ask user only if there are some breakpoints to delete. */
15283 if (!from_tty
15284 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15285 {
35df4500 15286 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15287 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15288 delete_breakpoint (b);
1042e4c0
SS
15289 }
15290 }
15291 else
51be5b68 15292 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15293}
15294
197f0a60
TT
15295/* Helper function for trace_pass_command. */
15296
15297static void
d9b3f62e 15298trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15299{
d9b3f62e 15300 tp->pass_count = count;
c1fc2657 15301 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15302 if (from_tty)
15303 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15304 tp->number, count);
197f0a60
TT
15305}
15306
1042e4c0
SS
15307/* Set passcount for tracepoint.
15308
15309 First command argument is passcount, second is tracepoint number.
15310 If tracepoint number omitted, apply to most recently defined.
15311 Also accepts special argument "all". */
15312
15313static void
15314trace_pass_command (char *args, int from_tty)
15315{
d9b3f62e 15316 struct tracepoint *t1;
1042e4c0 15317 unsigned int count;
1042e4c0
SS
15318
15319 if (args == 0 || *args == 0)
3e43a32a
MS
15320 error (_("passcount command requires an "
15321 "argument (count + optional TP num)"));
1042e4c0 15322
4a64f543 15323 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15324
529480d0 15325 args = skip_spaces (args);
1042e4c0
SS
15326 if (*args && strncasecmp (args, "all", 3) == 0)
15327 {
d9b3f62e
PA
15328 struct breakpoint *b;
15329
1042e4c0 15330 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15331 if (*args)
15332 error (_("Junk at end of arguments."));
1042e4c0 15333
d9b3f62e 15334 ALL_TRACEPOINTS (b)
197f0a60 15335 {
d9b3f62e 15336 t1 = (struct tracepoint *) b;
197f0a60
TT
15337 trace_pass_set_count (t1, count, from_tty);
15338 }
15339 }
15340 else if (*args == '\0')
1042e4c0 15341 {
5fa1d40e 15342 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15343 if (t1)
197f0a60
TT
15344 trace_pass_set_count (t1, count, from_tty);
15345 }
15346 else
15347 {
bfd28288
PA
15348 number_or_range_parser parser (args);
15349 while (!parser.finished ())
1042e4c0 15350 {
bfd28288 15351 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15352 if (t1)
15353 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15354 }
15355 }
1042e4c0
SS
15356}
15357
d9b3f62e 15358struct tracepoint *
1042e4c0
SS
15359get_tracepoint (int num)
15360{
15361 struct breakpoint *t;
15362
15363 ALL_TRACEPOINTS (t)
15364 if (t->number == num)
d9b3f62e 15365 return (struct tracepoint *) t;
1042e4c0
SS
15366
15367 return NULL;
15368}
15369
d5551862
SS
15370/* Find the tracepoint with the given target-side number (which may be
15371 different from the tracepoint number after disconnecting and
15372 reconnecting). */
15373
d9b3f62e 15374struct tracepoint *
d5551862
SS
15375get_tracepoint_by_number_on_target (int num)
15376{
d9b3f62e 15377 struct breakpoint *b;
d5551862 15378
d9b3f62e
PA
15379 ALL_TRACEPOINTS (b)
15380 {
15381 struct tracepoint *t = (struct tracepoint *) b;
15382
15383 if (t->number_on_target == num)
15384 return t;
15385 }
d5551862
SS
15386
15387 return NULL;
15388}
15389
1042e4c0 15390/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15391 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15392 If the argument is missing, the most recent tracepoint
15393 (tracepoint_count) is returned. */
15394
d9b3f62e 15395struct tracepoint *
197f0a60 15396get_tracepoint_by_number (char **arg,
bfd28288 15397 number_or_range_parser *parser)
1042e4c0 15398{
1042e4c0
SS
15399 struct breakpoint *t;
15400 int tpnum;
15401 char *instring = arg == NULL ? NULL : *arg;
15402
bfd28288 15403 if (parser != NULL)
197f0a60 15404 {
bfd28288
PA
15405 gdb_assert (!parser->finished ());
15406 tpnum = parser->get_number ();
197f0a60
TT
15407 }
15408 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15409 tpnum = tracepoint_count;
1042e4c0 15410 else
197f0a60 15411 tpnum = get_number (arg);
1042e4c0
SS
15412
15413 if (tpnum <= 0)
15414 {
15415 if (instring && *instring)
15416 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15417 instring);
15418 else
5fa1d40e 15419 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15420 return NULL;
15421 }
15422
15423 ALL_TRACEPOINTS (t)
15424 if (t->number == tpnum)
15425 {
d9b3f62e 15426 return (struct tracepoint *) t;
1042e4c0
SS
15427 }
15428
1042e4c0
SS
15429 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15430 return NULL;
15431}
15432
d9b3f62e
PA
15433void
15434print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15435{
15436 if (b->thread != -1)
15437 fprintf_unfiltered (fp, " thread %d", b->thread);
15438
15439 if (b->task != 0)
15440 fprintf_unfiltered (fp, " task %d", b->task);
15441
15442 fprintf_unfiltered (fp, "\n");
15443}
15444
6149aea9
PA
15445/* Save information on user settable breakpoints (watchpoints, etc) to
15446 a new script file named FILENAME. If FILTER is non-NULL, call it
15447 on each breakpoint and only include the ones for which it returns
15448 non-zero. */
15449
1042e4c0 15450static void
6149aea9
PA
15451save_breakpoints (char *filename, int from_tty,
15452 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15453{
15454 struct breakpoint *tp;
6149aea9 15455 int any = 0;
6149aea9 15456 int extra_trace_bits = 0;
1042e4c0 15457
6149aea9
PA
15458 if (filename == 0 || *filename == 0)
15459 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15460
15461 /* See if we have anything to save. */
6149aea9 15462 ALL_BREAKPOINTS (tp)
1042e4c0 15463 {
6149aea9 15464 /* Skip internal and momentary breakpoints. */
09d682a4 15465 if (!user_breakpoint_p (tp))
6149aea9
PA
15466 continue;
15467
15468 /* If we have a filter, only save the breakpoints it accepts. */
15469 if (filter && !filter (tp))
15470 continue;
15471
15472 any = 1;
15473
15474 if (is_tracepoint (tp))
15475 {
15476 extra_trace_bits = 1;
15477
15478 /* We can stop searching. */
15479 break;
15480 }
1042e4c0 15481 }
6149aea9
PA
15482
15483 if (!any)
1042e4c0 15484 {
6149aea9 15485 warning (_("Nothing to save."));
1042e4c0
SS
15486 return;
15487 }
15488
ee0c3293 15489 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15490
15491 stdio_file fp;
15492
ee0c3293 15493 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15494 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15495 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15496
6149aea9 15497 if (extra_trace_bits)
d7e74731 15498 save_trace_state_variables (&fp);
8bf6485c 15499
6149aea9 15500 ALL_BREAKPOINTS (tp)
1042e4c0 15501 {
6149aea9 15502 /* Skip internal and momentary breakpoints. */
09d682a4 15503 if (!user_breakpoint_p (tp))
6149aea9 15504 continue;
8bf6485c 15505
6149aea9
PA
15506 /* If we have a filter, only save the breakpoints it accepts. */
15507 if (filter && !filter (tp))
15508 continue;
15509
d7e74731 15510 tp->ops->print_recreate (tp, &fp);
1042e4c0 15511
6149aea9
PA
15512 /* Note, we can't rely on tp->number for anything, as we can't
15513 assume the recreated breakpoint numbers will match. Use $bpnum
15514 instead. */
15515
15516 if (tp->cond_string)
d7e74731 15517 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15518
15519 if (tp->ignore_count)
d7e74731 15520 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15521
2d9442cc 15522 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15523 {
d7e74731 15524 fp.puts (" commands\n");
a7bdde9e 15525
d7e74731 15526 current_uiout->redirect (&fp);
492d29ea 15527 TRY
1042e4c0 15528 {
79a45e25 15529 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15530 }
492d29ea
PA
15531 CATCH (ex, RETURN_MASK_ALL)
15532 {
112e8700 15533 current_uiout->redirect (NULL);
492d29ea
PA
15534 throw_exception (ex);
15535 }
15536 END_CATCH
1042e4c0 15537
112e8700 15538 current_uiout->redirect (NULL);
d7e74731 15539 fp.puts (" end\n");
1042e4c0 15540 }
6149aea9
PA
15541
15542 if (tp->enable_state == bp_disabled)
d7e74731 15543 fp.puts ("disable $bpnum\n");
6149aea9
PA
15544
15545 /* If this is a multi-location breakpoint, check if the locations
15546 should be individually disabled. Watchpoint locations are
15547 special, and not user visible. */
15548 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15549 {
15550 struct bp_location *loc;
15551 int n = 1;
15552
15553 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15554 if (!loc->enabled)
d7e74731 15555 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15556 }
1042e4c0 15557 }
8bf6485c 15558
6149aea9 15559 if (extra_trace_bits && *default_collect)
d7e74731 15560 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15561
1042e4c0 15562 if (from_tty)
ee0c3293 15563 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15564}
15565
15566/* The `save breakpoints' command. */
15567
15568static void
15569save_breakpoints_command (char *args, int from_tty)
15570{
15571 save_breakpoints (args, from_tty, NULL);
15572}
15573
15574/* The `save tracepoints' command. */
15575
15576static void
15577save_tracepoints_command (char *args, int from_tty)
15578{
15579 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15580}
15581
15582/* Create a vector of all tracepoints. */
15583
15584VEC(breakpoint_p) *
eeae04df 15585all_tracepoints (void)
1042e4c0
SS
15586{
15587 VEC(breakpoint_p) *tp_vec = 0;
15588 struct breakpoint *tp;
15589
15590 ALL_TRACEPOINTS (tp)
15591 {
15592 VEC_safe_push (breakpoint_p, tp_vec, tp);
15593 }
15594
15595 return tp_vec;
15596}
15597
c906108c 15598\f
629500fa
KS
15599/* This help string is used to consolidate all the help string for specifying
15600 locations used by several commands. */
15601
15602#define LOCATION_HELP_STRING \
15603"Linespecs are colon-separated lists of location parameters, such as\n\
15604source filename, function name, label name, and line number.\n\
15605Example: To specify the start of a label named \"the_top\" in the\n\
15606function \"fact\" in the file \"factorial.c\", use\n\
15607\"factorial.c:fact:the_top\".\n\
15608\n\
15609Address locations begin with \"*\" and specify an exact address in the\n\
15610program. Example: To specify the fourth byte past the start function\n\
15611\"main\", use \"*main + 4\".\n\
15612\n\
15613Explicit locations are similar to linespecs but use an option/argument\n\
15614syntax to specify location parameters.\n\
15615Example: To specify the start of the label named \"the_top\" in the\n\
15616function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15617-function fact -label the_top\".\n"
15618
4a64f543
MS
15619/* This help string is used for the break, hbreak, tbreak and thbreak
15620 commands. It is defined as a macro to prevent duplication.
15621 COMMAND should be a string constant containing the name of the
15622 command. */
629500fa 15623
31e2b00f 15624#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15625command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15626PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15627probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15628guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15629`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15630LOCATION may be a linespec, address, or explicit location as described\n\
15631below.\n\
15632\n\
dc10affe
PA
15633With no LOCATION, uses current execution address of the selected\n\
15634stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15635\n\
15636THREADNUM is the number from \"info threads\".\n\
15637CONDITION is a boolean expression.\n\
629500fa 15638\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15639Multiple breakpoints at one place are permitted, and useful if their\n\
15640conditions are different.\n\
31e2b00f
AS
15641\n\
15642Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15643
44feb3ce
TT
15644/* List of subcommands for "catch". */
15645static struct cmd_list_element *catch_cmdlist;
15646
15647/* List of subcommands for "tcatch". */
15648static struct cmd_list_element *tcatch_cmdlist;
15649
9ac4176b 15650void
a121b7c1 15651add_catch_command (const char *name, const char *docstring,
82ae6c8d 15652 cmd_sfunc_ftype *sfunc,
625e8578 15653 completer_ftype *completer,
44feb3ce
TT
15654 void *user_data_catch,
15655 void *user_data_tcatch)
15656{
15657 struct cmd_list_element *command;
15658
15659 command = add_cmd (name, class_breakpoint, NULL, docstring,
15660 &catch_cmdlist);
15661 set_cmd_sfunc (command, sfunc);
15662 set_cmd_context (command, user_data_catch);
a96d9b2e 15663 set_cmd_completer (command, completer);
44feb3ce
TT
15664
15665 command = add_cmd (name, class_breakpoint, NULL, docstring,
15666 &tcatch_cmdlist);
15667 set_cmd_sfunc (command, sfunc);
15668 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15669 set_cmd_completer (command, completer);
44feb3ce
TT
15670}
15671
6149aea9
PA
15672static void
15673save_command (char *arg, int from_tty)
15674{
3e43a32a
MS
15675 printf_unfiltered (_("\"save\" must be followed by "
15676 "the name of a save subcommand.\n"));
635c7e8a 15677 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15678}
15679
84f4c1fe
PM
15680struct breakpoint *
15681iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15682 void *data)
15683{
35df4500 15684 struct breakpoint *b, *b_tmp;
84f4c1fe 15685
35df4500 15686 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15687 {
15688 if ((*callback) (b, data))
15689 return b;
15690 }
15691
15692 return NULL;
15693}
15694
0574c78f
GB
15695/* Zero if any of the breakpoint's locations could be a location where
15696 functions have been inlined, nonzero otherwise. */
15697
15698static int
15699is_non_inline_function (struct breakpoint *b)
15700{
15701 /* The shared library event breakpoint is set on the address of a
15702 non-inline function. */
15703 if (b->type == bp_shlib_event)
15704 return 1;
15705
15706 return 0;
15707}
15708
15709/* Nonzero if the specified PC cannot be a location where functions
15710 have been inlined. */
15711
15712int
09ac7c10
TT
15713pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15714 const struct target_waitstatus *ws)
0574c78f
GB
15715{
15716 struct breakpoint *b;
15717 struct bp_location *bl;
15718
15719 ALL_BREAKPOINTS (b)
15720 {
15721 if (!is_non_inline_function (b))
15722 continue;
15723
15724 for (bl = b->loc; bl != NULL; bl = bl->next)
15725 {
15726 if (!bl->shlib_disabled
09ac7c10 15727 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15728 return 1;
15729 }
15730 }
15731
15732 return 0;
15733}
15734
2f202fde
JK
15735/* Remove any references to OBJFILE which is going to be freed. */
15736
15737void
15738breakpoint_free_objfile (struct objfile *objfile)
15739{
15740 struct bp_location **locp, *loc;
15741
15742 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15743 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15744 loc->symtab = NULL;
15745}
15746
2060206e
PA
15747void
15748initialize_breakpoint_ops (void)
15749{
15750 static int initialized = 0;
15751
15752 struct breakpoint_ops *ops;
15753
15754 if (initialized)
15755 return;
15756 initialized = 1;
15757
15758 /* The breakpoint_ops structure to be inherit by all kinds of
15759 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15760 internal and momentary breakpoints, etc.). */
15761 ops = &bkpt_base_breakpoint_ops;
15762 *ops = base_breakpoint_ops;
15763 ops->re_set = bkpt_re_set;
15764 ops->insert_location = bkpt_insert_location;
15765 ops->remove_location = bkpt_remove_location;
15766 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15767 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15768 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15769 ops->decode_location = bkpt_decode_location;
2060206e
PA
15770
15771 /* The breakpoint_ops structure to be used in regular breakpoints. */
15772 ops = &bkpt_breakpoint_ops;
15773 *ops = bkpt_base_breakpoint_ops;
15774 ops->re_set = bkpt_re_set;
15775 ops->resources_needed = bkpt_resources_needed;
15776 ops->print_it = bkpt_print_it;
15777 ops->print_mention = bkpt_print_mention;
15778 ops->print_recreate = bkpt_print_recreate;
15779
15780 /* Ranged breakpoints. */
15781 ops = &ranged_breakpoint_ops;
15782 *ops = bkpt_breakpoint_ops;
15783 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15784 ops->resources_needed = resources_needed_ranged_breakpoint;
15785 ops->print_it = print_it_ranged_breakpoint;
15786 ops->print_one = print_one_ranged_breakpoint;
15787 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15788 ops->print_mention = print_mention_ranged_breakpoint;
15789 ops->print_recreate = print_recreate_ranged_breakpoint;
15790
15791 /* Internal breakpoints. */
15792 ops = &internal_breakpoint_ops;
15793 *ops = bkpt_base_breakpoint_ops;
15794 ops->re_set = internal_bkpt_re_set;
15795 ops->check_status = internal_bkpt_check_status;
15796 ops->print_it = internal_bkpt_print_it;
15797 ops->print_mention = internal_bkpt_print_mention;
15798
15799 /* Momentary breakpoints. */
15800 ops = &momentary_breakpoint_ops;
15801 *ops = bkpt_base_breakpoint_ops;
15802 ops->re_set = momentary_bkpt_re_set;
15803 ops->check_status = momentary_bkpt_check_status;
15804 ops->print_it = momentary_bkpt_print_it;
15805 ops->print_mention = momentary_bkpt_print_mention;
15806
55aa24fb
SDJ
15807 /* Probe breakpoints. */
15808 ops = &bkpt_probe_breakpoint_ops;
15809 *ops = bkpt_breakpoint_ops;
15810 ops->insert_location = bkpt_probe_insert_location;
15811 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15812 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15813 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15814
2060206e
PA
15815 /* Watchpoints. */
15816 ops = &watchpoint_breakpoint_ops;
15817 *ops = base_breakpoint_ops;
15818 ops->re_set = re_set_watchpoint;
15819 ops->insert_location = insert_watchpoint;
15820 ops->remove_location = remove_watchpoint;
15821 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15822 ops->check_status = check_status_watchpoint;
15823 ops->resources_needed = resources_needed_watchpoint;
15824 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15825 ops->print_it = print_it_watchpoint;
15826 ops->print_mention = print_mention_watchpoint;
15827 ops->print_recreate = print_recreate_watchpoint;
427cd150 15828 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15829
15830 /* Masked watchpoints. */
15831 ops = &masked_watchpoint_breakpoint_ops;
15832 *ops = watchpoint_breakpoint_ops;
15833 ops->insert_location = insert_masked_watchpoint;
15834 ops->remove_location = remove_masked_watchpoint;
15835 ops->resources_needed = resources_needed_masked_watchpoint;
15836 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15837 ops->print_it = print_it_masked_watchpoint;
15838 ops->print_one_detail = print_one_detail_masked_watchpoint;
15839 ops->print_mention = print_mention_masked_watchpoint;
15840 ops->print_recreate = print_recreate_masked_watchpoint;
15841
15842 /* Tracepoints. */
15843 ops = &tracepoint_breakpoint_ops;
15844 *ops = base_breakpoint_ops;
15845 ops->re_set = tracepoint_re_set;
15846 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15847 ops->print_one_detail = tracepoint_print_one_detail;
15848 ops->print_mention = tracepoint_print_mention;
15849 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15850 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15851 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15852 ops->decode_location = tracepoint_decode_location;
983af33b 15853
55aa24fb
SDJ
15854 /* Probe tracepoints. */
15855 ops = &tracepoint_probe_breakpoint_ops;
15856 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15857 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15858 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15859
983af33b
SDJ
15860 /* Static tracepoints with marker (`-m'). */
15861 ops = &strace_marker_breakpoint_ops;
15862 *ops = tracepoint_breakpoint_ops;
5f700d83 15863 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15864 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15865 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15866
15867 /* Fork catchpoints. */
15868 ops = &catch_fork_breakpoint_ops;
15869 *ops = base_breakpoint_ops;
15870 ops->insert_location = insert_catch_fork;
15871 ops->remove_location = remove_catch_fork;
15872 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15873 ops->print_it = print_it_catch_fork;
15874 ops->print_one = print_one_catch_fork;
15875 ops->print_mention = print_mention_catch_fork;
15876 ops->print_recreate = print_recreate_catch_fork;
15877
15878 /* Vfork catchpoints. */
15879 ops = &catch_vfork_breakpoint_ops;
15880 *ops = base_breakpoint_ops;
15881 ops->insert_location = insert_catch_vfork;
15882 ops->remove_location = remove_catch_vfork;
15883 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15884 ops->print_it = print_it_catch_vfork;
15885 ops->print_one = print_one_catch_vfork;
15886 ops->print_mention = print_mention_catch_vfork;
15887 ops->print_recreate = print_recreate_catch_vfork;
15888
15889 /* Exec catchpoints. */
15890 ops = &catch_exec_breakpoint_ops;
15891 *ops = base_breakpoint_ops;
2060206e
PA
15892 ops->insert_location = insert_catch_exec;
15893 ops->remove_location = remove_catch_exec;
15894 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15895 ops->print_it = print_it_catch_exec;
15896 ops->print_one = print_one_catch_exec;
15897 ops->print_mention = print_mention_catch_exec;
15898 ops->print_recreate = print_recreate_catch_exec;
15899
edcc5120
TT
15900 /* Solib-related catchpoints. */
15901 ops = &catch_solib_breakpoint_ops;
15902 *ops = base_breakpoint_ops;
edcc5120
TT
15903 ops->insert_location = insert_catch_solib;
15904 ops->remove_location = remove_catch_solib;
15905 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15906 ops->check_status = check_status_catch_solib;
15907 ops->print_it = print_it_catch_solib;
15908 ops->print_one = print_one_catch_solib;
15909 ops->print_mention = print_mention_catch_solib;
15910 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15911
15912 ops = &dprintf_breakpoint_ops;
15913 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15914 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15915 ops->resources_needed = bkpt_resources_needed;
15916 ops->print_it = bkpt_print_it;
15917 ops->print_mention = bkpt_print_mention;
2d9442cc 15918 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15919 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15920 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15921}
15922
8bfd80db
YQ
15923/* Chain containing all defined "enable breakpoint" subcommands. */
15924
15925static struct cmd_list_element *enablebreaklist = NULL;
15926
c906108c 15927void
fba45db2 15928_initialize_breakpoint (void)
c906108c
SS
15929{
15930 struct cmd_list_element *c;
15931
2060206e
PA
15932 initialize_breakpoint_ops ();
15933
84acb35a 15934 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15935 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15936 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15937
55aa24fb
SDJ
15938 breakpoint_objfile_key
15939 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15940
c906108c
SS
15941 breakpoint_chain = 0;
15942 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15943 before a breakpoint is set. */
15944 breakpoint_count = 0;
15945
1042e4c0
SS
15946 tracepoint_count = 0;
15947
1bedd215
AC
15948 add_com ("ignore", class_breakpoint, ignore_command, _("\
15949Set ignore-count of breakpoint number N to COUNT.\n\
15950Usage is `ignore N COUNT'."));
c906108c 15951
1bedd215 15952 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15953Set commands to be executed when the given breakpoints are hit.\n\
15954Give a space-separated breakpoint list as argument after \"commands\".\n\
15955A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15956(e.g. `5-7').\n\
c906108c
SS
15957With no argument, the targeted breakpoint is the last one set.\n\
15958The commands themselves follow starting on the next line.\n\
15959Type a line containing \"end\" to indicate the end of them.\n\
15960Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15961then no output is printed when it is hit, except what the commands print."));
c906108c 15962
d55637df 15963 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15964Specify breakpoint number N to break only if COND is true.\n\
c906108c 15965Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15966expression to be evaluated whenever breakpoint N is reached."));
d55637df 15967 set_cmd_completer (c, condition_completer);
c906108c 15968
1bedd215 15969 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15970Set a temporary breakpoint.\n\
c906108c
SS
15971Like \"break\" except the breakpoint is only temporary,\n\
15972so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15973by using \"enable delete\" on the breakpoint number.\n\
15974\n"
15975BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15976 set_cmd_completer (c, location_completer);
c94fdfd0 15977
1bedd215 15978 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15979Set a hardware assisted breakpoint.\n\
c906108c 15980Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15981some target hardware may not have this support.\n\
15982\n"
15983BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15984 set_cmd_completer (c, location_completer);
c906108c 15985
1bedd215 15986 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15987Set a temporary hardware assisted breakpoint.\n\
c906108c 15988Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15989so it will be deleted when hit.\n\
15990\n"
15991BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15992 set_cmd_completer (c, location_completer);
c906108c 15993
1bedd215
AC
15994 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15995Enable some breakpoints.\n\
c906108c
SS
15996Give breakpoint numbers (separated by spaces) as arguments.\n\
15997With no subcommand, breakpoints are enabled until you command otherwise.\n\
15998This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15999With a subcommand you can enable temporarily."),
c906108c 16000 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16001
16002 add_com_alias ("en", "enable", class_breakpoint, 1);
16003
84951ab5 16004 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16005Enable some breakpoints.\n\
c906108c
SS
16006Give breakpoint numbers (separated by spaces) as arguments.\n\
16007This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16008May be abbreviated to simply \"enable\".\n"),
c5aa993b 16009 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16010
1a966eab
AC
16011 add_cmd ("once", no_class, enable_once_command, _("\
16012Enable breakpoints for one hit. Give breakpoint numbers.\n\
16013If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16014 &enablebreaklist);
16015
1a966eab
AC
16016 add_cmd ("delete", no_class, enable_delete_command, _("\
16017Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16018If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16019 &enablebreaklist);
16020
816338b5
SS
16021 add_cmd ("count", no_class, enable_count_command, _("\
16022Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16023If a breakpoint is hit while enabled in this fashion,\n\
16024the count is decremented; when it reaches zero, the breakpoint is disabled."),
16025 &enablebreaklist);
16026
1a966eab
AC
16027 add_cmd ("delete", no_class, enable_delete_command, _("\
16028Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16029If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16030 &enablelist);
16031
1a966eab
AC
16032 add_cmd ("once", no_class, enable_once_command, _("\
16033Enable breakpoints for one hit. Give breakpoint numbers.\n\
16034If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16035 &enablelist);
16036
16037 add_cmd ("count", no_class, enable_count_command, _("\
16038Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16039If a breakpoint is hit while enabled in this fashion,\n\
16040the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16041 &enablelist);
16042
1bedd215
AC
16043 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16044Disable some breakpoints.\n\
c906108c
SS
16045Arguments are breakpoint numbers with spaces in between.\n\
16046To disable all breakpoints, give no argument.\n\
64b9b334 16047A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16048 &disablelist, "disable ", 1, &cmdlist);
16049 add_com_alias ("dis", "disable", class_breakpoint, 1);
16050 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16051
1a966eab
AC
16052 add_cmd ("breakpoints", class_alias, disable_command, _("\
16053Disable some breakpoints.\n\
c906108c
SS
16054Arguments are breakpoint numbers with spaces in between.\n\
16055To disable all breakpoints, give no argument.\n\
64b9b334 16056A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16057This command may be abbreviated \"disable\"."),
c906108c
SS
16058 &disablelist);
16059
1bedd215
AC
16060 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16061Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16062Arguments are breakpoint numbers with spaces in between.\n\
16063To delete all breakpoints, give no argument.\n\
16064\n\
16065Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16066The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16067 &deletelist, "delete ", 1, &cmdlist);
16068 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16069 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16070
1a966eab
AC
16071 add_cmd ("breakpoints", class_alias, delete_command, _("\
16072Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16073Arguments are breakpoint numbers with spaces in between.\n\
16074To delete all breakpoints, give no argument.\n\
1a966eab 16075This command may be abbreviated \"delete\"."),
c906108c
SS
16076 &deletelist);
16077
1bedd215 16078 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16079Clear breakpoint at specified location.\n\
16080Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16081\n\
16082With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16083is executing in.\n"
16084"\n" LOCATION_HELP_STRING "\n\
1bedd215 16085See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16086 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16087
1bedd215 16088 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16089Set breakpoint at specified location.\n"
31e2b00f 16090BREAK_ARGS_HELP ("break")));
5ba2abeb 16091 set_cmd_completer (c, location_completer);
c94fdfd0 16092
c906108c
SS
16093 add_com_alias ("b", "break", class_run, 1);
16094 add_com_alias ("br", "break", class_run, 1);
16095 add_com_alias ("bre", "break", class_run, 1);
16096 add_com_alias ("brea", "break", class_run, 1);
16097
c906108c
SS
16098 if (dbx_commands)
16099 {
1bedd215
AC
16100 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16101Break in function/address or break at a line in the current file."),
c5aa993b
JM
16102 &stoplist, "stop ", 1, &cmdlist);
16103 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16104 _("Break in function or address."), &stoplist);
c5aa993b 16105 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16106 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16107 add_com ("status", class_info, breakpoints_info, _("\
16108Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16109The \"Type\" column indicates one of:\n\
16110\tbreakpoint - normal breakpoint\n\
16111\twatchpoint - watchpoint\n\
16112The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16113the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16114breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16115address and file/line number respectively.\n\
16116\n\
16117Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16118are set to the address of the last breakpoint listed unless the command\n\
16119is prefixed with \"server \".\n\n\
c906108c 16120Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16121breakpoint set."));
c906108c
SS
16122 }
16123
1bedd215 16124 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16125Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16126The \"Type\" column indicates one of:\n\
16127\tbreakpoint - normal breakpoint\n\
16128\twatchpoint - watchpoint\n\
16129The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16130the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16131breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16132address and file/line number respectively.\n\
16133\n\
16134Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16135are set to the address of the last breakpoint listed unless the command\n\
16136is prefixed with \"server \".\n\n\
c906108c 16137Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16138breakpoint set."));
c906108c 16139
6b04bdb7
MS
16140 add_info_alias ("b", "breakpoints", 1);
16141
1a966eab
AC
16142 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16143Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16144The \"Type\" column indicates one of:\n\
16145\tbreakpoint - normal breakpoint\n\
16146\twatchpoint - watchpoint\n\
16147\tlongjmp - internal breakpoint used to step through longjmp()\n\
16148\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16149\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16150\tfinish - internal breakpoint used by the \"finish\" command\n\
16151The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16152the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16153breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16154address and file/line number respectively.\n\
16155\n\
16156Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16157are set to the address of the last breakpoint listed unless the command\n\
16158is prefixed with \"server \".\n\n\
c906108c 16159Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16160breakpoint set."),
c906108c
SS
16161 &maintenanceinfolist);
16162
44feb3ce
TT
16163 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16164Set catchpoints to catch events."),
16165 &catch_cmdlist, "catch ",
16166 0/*allow-unknown*/, &cmdlist);
16167
16168 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16169Set temporary catchpoints to catch events."),
16170 &tcatch_cmdlist, "tcatch ",
16171 0/*allow-unknown*/, &cmdlist);
16172
44feb3ce
TT
16173 add_catch_command ("fork", _("Catch calls to fork."),
16174 catch_fork_command_1,
a96d9b2e 16175 NULL,
44feb3ce
TT
16176 (void *) (uintptr_t) catch_fork_permanent,
16177 (void *) (uintptr_t) catch_fork_temporary);
16178 add_catch_command ("vfork", _("Catch calls to vfork."),
16179 catch_fork_command_1,
a96d9b2e 16180 NULL,
44feb3ce
TT
16181 (void *) (uintptr_t) catch_vfork_permanent,
16182 (void *) (uintptr_t) catch_vfork_temporary);
16183 add_catch_command ("exec", _("Catch calls to exec."),
16184 catch_exec_command_1,
a96d9b2e
SDJ
16185 NULL,
16186 CATCH_PERMANENT,
16187 CATCH_TEMPORARY);
edcc5120
TT
16188 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16189Usage: catch load [REGEX]\n\
16190If REGEX is given, only stop for libraries matching the regular expression."),
16191 catch_load_command_1,
16192 NULL,
16193 CATCH_PERMANENT,
16194 CATCH_TEMPORARY);
16195 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16196Usage: catch unload [REGEX]\n\
16197If REGEX is given, only stop for libraries matching the regular expression."),
16198 catch_unload_command_1,
16199 NULL,
16200 CATCH_PERMANENT,
16201 CATCH_TEMPORARY);
c5aa993b 16202
1bedd215
AC
16203 c = add_com ("watch", class_breakpoint, watch_command, _("\
16204Set a watchpoint for an expression.\n\
06a64a0b 16205Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16206A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16207an expression changes.\n\
16208If -l or -location is given, this evaluates EXPRESSION and watches\n\
16209the memory to which it refers."));
65d12d83 16210 set_cmd_completer (c, expression_completer);
c906108c 16211
1bedd215
AC
16212 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16213Set a read watchpoint for an expression.\n\
06a64a0b 16214Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16215A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16216an expression is read.\n\
16217If -l or -location is given, this evaluates EXPRESSION and watches\n\
16218the memory to which it refers."));
65d12d83 16219 set_cmd_completer (c, expression_completer);
c906108c 16220
1bedd215
AC
16221 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16222Set a watchpoint for an expression.\n\
06a64a0b 16223Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16224A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16225an expression is either read or written.\n\
16226If -l or -location is given, this evaluates EXPRESSION and watches\n\
16227the memory to which it refers."));
65d12d83 16228 set_cmd_completer (c, expression_completer);
c906108c 16229
d77f58be 16230 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16231Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16232
920d2a44
AC
16233 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16234 respond to changes - contrary to the description. */
85c07804
AC
16235 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16236 &can_use_hw_watchpoints, _("\
16237Set debugger's willingness to use watchpoint hardware."), _("\
16238Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16239If zero, gdb will not use hardware for new watchpoints, even if\n\
16240such is available. (However, any hardware watchpoints that were\n\
16241created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16242hardware.)"),
16243 NULL,
920d2a44 16244 show_can_use_hw_watchpoints,
85c07804 16245 &setlist, &showlist);
c906108c
SS
16246
16247 can_use_hw_watchpoints = 1;
fa8d40ab 16248
1042e4c0
SS
16249 /* Tracepoint manipulation commands. */
16250
16251 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16252Set a tracepoint at specified location.\n\
1042e4c0
SS
16253\n"
16254BREAK_ARGS_HELP ("trace") "\n\
16255Do \"help tracepoints\" for info on other tracepoint commands."));
16256 set_cmd_completer (c, location_completer);
16257
16258 add_com_alias ("tp", "trace", class_alias, 0);
16259 add_com_alias ("tr", "trace", class_alias, 1);
16260 add_com_alias ("tra", "trace", class_alias, 1);
16261 add_com_alias ("trac", "trace", class_alias, 1);
16262
7a697b8d 16263 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16264Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16265\n"
16266BREAK_ARGS_HELP ("ftrace") "\n\
16267Do \"help tracepoints\" for info on other tracepoint commands."));
16268 set_cmd_completer (c, location_completer);
16269
0fb4aa4b 16270 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16271Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16272\n\
16273strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16274LOCATION may be a linespec, explicit, or address location (described below) \n\
16275or -m MARKER_ID.\n\n\
16276If a marker id is specified, probe the marker with that name. With\n\
16277no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16278Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16279This collects arbitrary user data passed in the probe point call to the\n\
16280tracing library. You can inspect it when analyzing the trace buffer,\n\
16281by printing the $_sdata variable like any other convenience variable.\n\
16282\n\
16283CONDITION is a boolean expression.\n\
629500fa 16284\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16285Multiple tracepoints at one place are permitted, and useful if their\n\
16286conditions are different.\n\
0fb4aa4b
PA
16287\n\
16288Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16289Do \"help tracepoints\" for info on other tracepoint commands."));
16290 set_cmd_completer (c, location_completer);
16291
1042e4c0 16292 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16293Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16294Convenience variable \"$tpnum\" contains the number of the\n\
16295last tracepoint set."));
16296
16297 add_info_alias ("tp", "tracepoints", 1);
16298
16299 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16300Delete specified tracepoints.\n\
16301Arguments are tracepoint numbers, separated by spaces.\n\
16302No argument means delete all tracepoints."),
16303 &deletelist);
7e20dfcd 16304 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16305
16306 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16307Disable specified tracepoints.\n\
16308Arguments are tracepoint numbers, separated by spaces.\n\
16309No argument means disable all tracepoints."),
16310 &disablelist);
16311 deprecate_cmd (c, "disable");
16312
16313 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16314Enable specified tracepoints.\n\
16315Arguments are tracepoint numbers, separated by spaces.\n\
16316No argument means enable all tracepoints."),
16317 &enablelist);
16318 deprecate_cmd (c, "enable");
16319
16320 add_com ("passcount", class_trace, trace_pass_command, _("\
16321Set the passcount for a tracepoint.\n\
16322The trace will end when the tracepoint has been passed 'count' times.\n\
16323Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16324if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16325
6149aea9
PA
16326 add_prefix_cmd ("save", class_breakpoint, save_command,
16327 _("Save breakpoint definitions as a script."),
16328 &save_cmdlist, "save ",
16329 0/*allow-unknown*/, &cmdlist);
16330
16331 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16332Save current breakpoint definitions as a script.\n\
cce7e648 16333This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16334catchpoints, tracepoints). Use the 'source' command in another debug\n\
16335session to restore them."),
16336 &save_cmdlist);
16337 set_cmd_completer (c, filename_completer);
16338
16339 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16340Save current tracepoint definitions as a script.\n\
6149aea9
PA
16341Use the 'source' command in another debug session to restore them."),
16342 &save_cmdlist);
1042e4c0
SS
16343 set_cmd_completer (c, filename_completer);
16344
6149aea9
PA
16345 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16346 deprecate_cmd (c, "save tracepoints");
16347
1bedd215 16348 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16349Breakpoint specific settings\n\
16350Configure various breakpoint-specific variables such as\n\
1bedd215 16351pending breakpoint behavior"),
fa8d40ab
JJ
16352 &breakpoint_set_cmdlist, "set breakpoint ",
16353 0/*allow-unknown*/, &setlist);
1bedd215 16354 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16355Breakpoint specific settings\n\
16356Configure various breakpoint-specific variables such as\n\
1bedd215 16357pending breakpoint behavior"),
fa8d40ab
JJ
16358 &breakpoint_show_cmdlist, "show breakpoint ",
16359 0/*allow-unknown*/, &showlist);
16360
7915a72c
AC
16361 add_setshow_auto_boolean_cmd ("pending", no_class,
16362 &pending_break_support, _("\
16363Set debugger's behavior regarding pending breakpoints."), _("\
16364Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16365If on, an unrecognized breakpoint location will cause gdb to create a\n\
16366pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16367an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16368user-query to see if a pending breakpoint should be created."),
2c5b56ce 16369 NULL,
920d2a44 16370 show_pending_break_support,
6e1d7d6c
AC
16371 &breakpoint_set_cmdlist,
16372 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16373
16374 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16375
16376 add_setshow_boolean_cmd ("auto-hw", no_class,
16377 &automatic_hardware_breakpoints, _("\
16378Set automatic usage of hardware breakpoints."), _("\
16379Show automatic usage of hardware breakpoints."), _("\
16380If set, the debugger will automatically use hardware breakpoints for\n\
16381breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16382a warning will be emitted for such breakpoints."),
16383 NULL,
16384 show_automatic_hardware_breakpoints,
16385 &breakpoint_set_cmdlist,
16386 &breakpoint_show_cmdlist);
74960c60 16387
a25a5a45
PA
16388 add_setshow_boolean_cmd ("always-inserted", class_support,
16389 &always_inserted_mode, _("\
74960c60
VP
16390Set mode for inserting breakpoints."), _("\
16391Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16392When this mode is on, breakpoints are inserted immediately as soon as\n\
16393they're created, kept inserted even when execution stops, and removed\n\
16394only when the user deletes them. When this mode is off (the default),\n\
16395breakpoints are inserted only when execution continues, and removed\n\
16396when execution stops."),
72d0e2c5
YQ
16397 NULL,
16398 &show_always_inserted_mode,
16399 &breakpoint_set_cmdlist,
16400 &breakpoint_show_cmdlist);
f1310107 16401
b775012e
LM
16402 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16403 condition_evaluation_enums,
16404 &condition_evaluation_mode_1, _("\
16405Set mode of breakpoint condition evaluation."), _("\
16406Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16407When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16408evaluated on the host's side by GDB. When it is set to \"target\",\n\
16409breakpoint conditions will be downloaded to the target (if the target\n\
16410supports such feature) and conditions will be evaluated on the target's side.\n\
16411If this is set to \"auto\" (default), this will be automatically set to\n\
16412\"target\" if it supports condition evaluation, otherwise it will\n\
16413be set to \"gdb\""),
16414 &set_condition_evaluation_mode,
16415 &show_condition_evaluation_mode,
16416 &breakpoint_set_cmdlist,
16417 &breakpoint_show_cmdlist);
16418
f1310107
TJB
16419 add_com ("break-range", class_breakpoint, break_range_command, _("\
16420Set a breakpoint for an address range.\n\
16421break-range START-LOCATION, END-LOCATION\n\
16422where START-LOCATION and END-LOCATION can be one of the following:\n\
16423 LINENUM, for that line in the current file,\n\
16424 FILE:LINENUM, for that line in that file,\n\
16425 +OFFSET, for that number of lines after the current line\n\
16426 or the start of the range\n\
16427 FUNCTION, for the first line in that function,\n\
16428 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16429 *ADDRESS, for the instruction at that address.\n\
16430\n\
16431The breakpoint will stop execution of the inferior whenever it executes\n\
16432an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16433range (including START-LOCATION and END-LOCATION)."));
16434
e7e0cddf 16435 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16436Set a dynamic printf at specified location.\n\
e7e0cddf 16437dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16438location may be a linespec, explicit, or address location.\n"
16439"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16440 set_cmd_completer (c, location_completer);
16441
16442 add_setshow_enum_cmd ("dprintf-style", class_support,
16443 dprintf_style_enums, &dprintf_style, _("\
16444Set the style of usage for dynamic printf."), _("\
16445Show the style of usage for dynamic printf."), _("\
16446This setting chooses how GDB will do a dynamic printf.\n\
16447If the value is \"gdb\", then the printing is done by GDB to its own\n\
16448console, as with the \"printf\" command.\n\
16449If the value is \"call\", the print is done by calling a function in your\n\
16450program; by default printf(), but you can choose a different function or\n\
16451output stream by setting dprintf-function and dprintf-channel."),
16452 update_dprintf_commands, NULL,
16453 &setlist, &showlist);
16454
16455 dprintf_function = xstrdup ("printf");
16456 add_setshow_string_cmd ("dprintf-function", class_support,
16457 &dprintf_function, _("\
16458Set the function to use for dynamic printf"), _("\
16459Show the function to use for dynamic printf"), NULL,
16460 update_dprintf_commands, NULL,
16461 &setlist, &showlist);
16462
16463 dprintf_channel = xstrdup ("");
16464 add_setshow_string_cmd ("dprintf-channel", class_support,
16465 &dprintf_channel, _("\
16466Set the channel to use for dynamic printf"), _("\
16467Show the channel to use for dynamic printf"), NULL,
16468 update_dprintf_commands, NULL,
16469 &setlist, &showlist);
16470
d3ce09f5
SS
16471 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16472 &disconnected_dprintf, _("\
16473Set whether dprintf continues after GDB disconnects."), _("\
16474Show whether dprintf continues after GDB disconnects."), _("\
16475Use this to let dprintf commands continue to hit and produce output\n\
16476even if GDB disconnects or detaches from the target."),
16477 NULL,
16478 NULL,
16479 &setlist, &showlist);
16480
16481 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16482agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16483(target agent only) This is useful for formatted output in user-defined commands."));
16484
765dc015 16485 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16486
16487 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16488 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16489}
This page took 4.798992 seconds and 4 git commands to generate.