Introduce and use new_breakpoint_from_type
[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 *,
e7e0cddf 123 char *, char *, enum bptype,
983af33b
SDJ
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
44f238bb 127 int, int, int, unsigned);
983af33b 128
f00aae0f
KS
129static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
c2f4122d 131 struct program_space *search_pspace,
f00aae0f 132 struct symtabs_and_lines *sals);
983af33b 133
a14ed312 134static void clear_command (char *, int);
c906108c 135
a14ed312 136static void catch_command (char *, int);
c906108c 137
a9634178 138static int can_use_hardware_watchpoint (struct value *);
c906108c 139
98deb0da 140static void break_command_1 (char *, int, int);
c906108c 141
a14ed312 142static void mention (struct breakpoint *);
c906108c 143
348d480f
PA
144static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
c0a91b2b 146 const struct breakpoint_ops *);
3742cc8b
YQ
147static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
4a64f543
MS
150/* This function is used in gdbtk sources and thus can not be made
151 static. */
63c252f8 152struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 153 struct symtab_and_line,
c0a91b2b
TT
154 enum bptype,
155 const struct breakpoint_ops *);
c906108c 156
06edf0c0
PA
157static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
a1aa2221
LM
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
06edf0c0 162
76897487
KB
163static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
a6d9a66e
UW
165static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
88f7da05 167 enum bptype bptype);
76897487 168
6c95b8df
PA
169static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
5af949e3 171 struct obj_section *, int);
c906108c 172
85d721b8
PA
173static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
f1310107
TJB
176static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
d35ae833
PA
180static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
a14ed312 184static void breakpoints_info (char *, int);
c906108c 185
d77f58be
SS
186static void watchpoints_info (char *, int);
187
e5a67952
MS
188static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
c906108c 190
4efb68b1 191static int breakpoint_cond_eval (void *);
c906108c 192
4efb68b1 193static void cleanup_executing_breakpoints (void *);
c906108c 194
a14ed312 195static void commands_command (char *, int);
c906108c 196
a14ed312 197static void condition_command (char *, int);
c906108c 198
834c0d03 199static int remove_breakpoint (struct bp_location *);
b2b6a7da 200static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 201
e514a9d6 202static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 203
4efb68b1 204static int watchpoint_check (void *);
c906108c 205
a14ed312 206static void maintenance_info_breakpoints (char *, int);
c906108c 207
a14ed312 208static int hw_breakpoint_used_count (void);
c906108c 209
a1398e0c
PA
210static int hw_watchpoint_use_count (struct breakpoint *);
211
212static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
c906108c 215
a14ed312 216static void hbreak_command (char *, int);
c906108c 217
a14ed312 218static void thbreak_command (char *, int);
c906108c 219
816338b5
SS
220static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
c906108c 222
a14ed312 223static void stop_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopin_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void stopat_command (char *arg, int from_tty);
7a292a7a 228
a14ed312 229static void tcatch_command (char *arg, int from_tty);
7a292a7a 230
fe3f5fa8 231static void free_bp_location (struct bp_location *loc);
f431efe5
PA
232static void incref_bp_location (struct bp_location *loc);
233static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 234
39d61571 235static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 236
44702360
PA
237/* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239enum ugll_insert_mode
240{
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
a25a5a45
PA
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
04086b45
PA
260 UGLL_MAY_INSERT,
261
a25a5a45
PA
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
04086b45 269 UGLL_INSERT
44702360
PA
270};
271
272static void update_global_location_list (enum ugll_insert_mode);
a5606eee 273
44702360 274static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 275
d77f58be 276static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
277
278static void insert_breakpoint_locations (void);
a5606eee 279
1042e4c0
SS
280static void tracepoints_info (char *, int);
281
282static void delete_trace_command (char *, int);
283
284static void enable_trace_command (char *, int);
285
286static void disable_trace_command (char *, int);
287
288static void trace_pass_command (char *, int);
289
558a9d82
YQ
290static void set_tracepoint_count (int num);
291
9c06b0b4
TJB
292static int is_masked_watchpoint (const struct breakpoint *b);
293
b775012e
LM
294static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
983af33b
SDJ
296/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 300
2060206e
PA
301/* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306/* Internal breakpoints class type. */
06edf0c0 307static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
308
309/* Momentary breakpoints class type. */
06edf0c0
PA
310static struct breakpoint_ops momentary_breakpoint_ops;
311
e2e4d78b
JK
312/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313static struct breakpoint_ops longjmp_breakpoint_ops;
314
2060206e
PA
315/* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317struct breakpoint_ops bkpt_breakpoint_ops;
318
55aa24fb
SDJ
319/* Breakpoints set on probes. */
320static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
e7e0cddf 322/* Dynamic printf class type. */
c5867ab6 323struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 324
d3ce09f5
SS
325/* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332static const char dprintf_style_gdb[] = "gdb";
333static const char dprintf_style_call[] = "call";
334static const char dprintf_style_agent[] = "agent";
335static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340};
341static const char *dprintf_style = dprintf_style_gdb;
342
343/* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
bde6261a 348static char *dprintf_function;
d3ce09f5
SS
349
350/* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
bde6261a 358static char *dprintf_channel;
d3ce09f5
SS
359
360/* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362static int disconnected_dprintf = 1;
363
5cea2a26
PA
364/* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366struct counted_command_line
367{
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373};
374
375struct command_line *
376breakpoint_commands (struct breakpoint *b)
377{
378 return b->commands ? b->commands->commands : NULL;
379}
3daf8fe5 380
f3b1572e
PA
381/* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384static int breakpoint_proceeded;
385
956a9fb9 386const char *
2cec12e5
AR
387bpdisp_text (enum bpdisp disp)
388{
4a64f543
MS
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
bc043ef3 392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 393
2cec12e5
AR
394 return bpdisps[(int) disp];
395}
c906108c 396
4a64f543 397/* Prototypes for exported functions. */
c906108c 398/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 399 if such is available. */
c906108c
SS
400static int can_use_hw_watchpoints;
401
920d2a44
AC
402static void
403show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406{
3e43a32a
MS
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
920d2a44
AC
410 value);
411}
412
fa8d40ab
JJ
413/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 415 for unrecognized breakpoint locations.
fa8d40ab
JJ
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417static enum auto_boolean pending_break_support;
920d2a44
AC
418static void
419show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422{
3e43a32a
MS
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
920d2a44
AC
426 value);
427}
fa8d40ab 428
765dc015 429/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 430 set with "break" but falling in read-only memory.
765dc015
VP
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433static int automatic_hardware_breakpoints;
434static void
435show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438{
3e43a32a
MS
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
441 value);
442}
443
a25a5a45
PA
444/* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451static int always_inserted_mode = 0;
72d0e2c5 452
33e5cbd6 453static void
74960c60 454show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 455 struct cmd_list_element *c, const char *value)
74960c60 456{
a25a5a45
PA
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
74960c60
VP
459}
460
b57bacec
PA
461/* See breakpoint.h. */
462
33e5cbd6 463int
a25a5a45 464breakpoints_should_be_inserted_now (void)
33e5cbd6 465{
a25a5a45
PA
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
372316f1
PA
475 struct thread_info *tp;
476
a25a5a45
PA
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
b57bacec
PA
484 if (threads_are_executing ())
485 return 1;
372316f1
PA
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
a25a5a45
PA
493 }
494 return 0;
33e5cbd6 495}
765dc015 496
b775012e
LM
497static const char condition_evaluation_both[] = "host or target";
498
499/* Modes for breakpoint condition evaluation. */
500static const char condition_evaluation_auto[] = "auto";
501static const char condition_evaluation_host[] = "host";
502static const char condition_evaluation_target[] = "target";
503static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508};
509
510/* Global that holds the current mode for breakpoint condition evaluation. */
511static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513/* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517/* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522static const char *
523translate_condition_evaluation_mode (const char *mode)
524{
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534}
535
536/* Discovers what condition_evaluation_auto translates to. */
537
538static const char *
539breakpoint_condition_evaluation_mode (void)
540{
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542}
543
544/* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547static int
548gdb_evaluates_breakpoint_condition_p (void)
549{
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553}
554
a14ed312 555void _initialize_breakpoint (void);
c906108c 556
c906108c
SS
557/* Are we executing breakpoint commands? */
558static int executing_breakpoint_commands;
559
c02f5703
MS
560/* Are overlay event breakpoints enabled? */
561static int overlay_events_enabled;
562
e09342b5
TJB
563/* See description in breakpoint.h. */
564int target_exact_watchpoints = 0;
565
c906108c 566/* Walk the following statement or block through all breakpoints.
e5dd4106 567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 568 current breakpoint. */
c906108c 569
5c44784c 570#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 571
5c44784c
JM
572#define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
c906108c 576
4a64f543
MS
577/* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 580
876fa593 581#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
582 for (BP_TMP = bp_locations; \
583 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 584 BP_TMP++)
7cc221ef 585
b775012e
LM
586/* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
f5336ca5 596 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
1042e4c0
SS
600/* Iterator for tracepoints only. */
601
602#define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 604 if (is_tracepoint (B))
1042e4c0 605
7cc221ef 606/* Chains of all breakpoints defined. */
c906108c
SS
607
608struct breakpoint *breakpoint_chain;
609
f5336ca5 610/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 611
f5336ca5 612static struct bp_location **bp_locations;
876fa593 613
f5336ca5 614/* Number of elements of BP_LOCATIONS. */
876fa593 615
f5336ca5 616static unsigned bp_locations_count;
876fa593 617
4a64f543 618/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 619 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 620 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 621 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 622 an address you need to read. */
876fa593 623
f5336ca5 624static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 625
4a64f543
MS
626/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
628 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 630 scan for shadow bytes for an address you need to read. */
876fa593 631
f5336ca5 632static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 633
4a64f543 634/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
635 from the bp_locations array, but for which a hit may still be
636 reported by a target. */
20874c92
VP
637VEC(bp_location_p) *moribund_locations = NULL;
638
c906108c
SS
639/* Number of last breakpoint made. */
640
95a42b64
TT
641static int breakpoint_count;
642
86b17b60
PA
643/* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647static int prev_breakpoint_count;
c906108c 648
1042e4c0
SS
649/* Number of last tracepoint made. */
650
95a42b64 651static int tracepoint_count;
1042e4c0 652
6149aea9
PA
653static struct cmd_list_element *breakpoint_set_cmdlist;
654static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 655struct cmd_list_element *save_cmdlist;
6149aea9 656
badd37ce
SDJ
657/* See declaration at breakpoint.h. */
658
659struct breakpoint *
660breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662{
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672}
673
468d015d
JJ
674/* Return whether a breakpoint is an active enabled breakpoint. */
675static int
676breakpoint_enabled (struct breakpoint *b)
677{
0d381245 678 return (b->enable_state == bp_enabled);
468d015d
JJ
679}
680
c906108c
SS
681/* Set breakpoint count to NUM. */
682
95a42b64 683static void
fba45db2 684set_breakpoint_count (int num)
c906108c 685{
86b17b60 686 prev_breakpoint_count = breakpoint_count;
c906108c 687 breakpoint_count = num;
4fa62494 688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
689}
690
86b17b60
PA
691/* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693static int rbreak_start_breakpoint_count;
694
95a42b64
TT
695/* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
86b17b60 697
95a42b64
TT
698void
699start_rbreak_breakpoints (void)
700{
86b17b60 701 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
702}
703
704/* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
86b17b60 706
95a42b64
TT
707void
708end_rbreak_breakpoints (void)
709{
86b17b60 710 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
711}
712
4a64f543 713/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
714
715void
fba45db2 716clear_breakpoint_hit_counts (void)
c906108c
SS
717{
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722}
723
9add0f1b
TT
724/* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727static struct counted_command_line *
728alloc_counted_command_line (struct command_line *commands)
729{
8d749320 730 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 731
9add0f1b
TT
732 result->refc = 1;
733 result->commands = commands;
8d749320 734
9add0f1b
TT
735 return result;
736}
737
738/* Increment reference count. This does nothing if CMD is NULL. */
739
740static void
741incref_counted_command_line (struct counted_command_line *cmd)
742{
743 if (cmd)
744 ++cmd->refc;
745}
746
747/* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751static void
752decref_counted_command_line (struct counted_command_line **cmdp)
753{
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763}
764
765/* A cleanup function that calls decref_counted_command_line. */
766
767static void
768do_cleanup_counted_command_line (void *arg)
769{
9a3c8263 770 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
771}
772
773/* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776static struct cleanup *
777make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778{
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780}
781
c906108c 782\f
48cb2d85
VP
783/* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786struct breakpoint *
787get_breakpoint (int num)
788{
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796}
5c44784c 797
c906108c 798\f
adc36818 799
b775012e
LM
800/* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803static void
804mark_breakpoint_modified (struct breakpoint *b)
805{
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821}
822
823/* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826static void
827mark_breakpoint_location_modified (struct bp_location *loc)
828{
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842}
843
844/* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847static void
848set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850{
b775012e
LM
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
abf1152a
JK
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
b775012e
LM
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
b775012e
LM
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
44702360 898 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
899 }
900
901 return;
902}
903
904/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907static void
908show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910{
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920}
921
922/* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 924 the more general bp_locations_compare function. */
b775012e
LM
925
926static int
f5336ca5 927bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 928{
9a3c8263
SM
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936}
937
938/* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943static struct bp_location **
944get_first_locp_gte_addr (CORE_ADDR address)
945{
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
b775012e
LM
951 dummy_loc.address = address;
952
953 /* Find a close match to the first location at ADDRESS. */
9a3c8263 954 locp_found = ((struct bp_location **)
f5336ca5 955 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 956 sizeof (struct bp_location **),
f5336ca5 957 bp_locations_compare_addrs));
b775012e
LM
958
959 /* Nothing was found, nothing left to do. */
960 if (locp_found == NULL)
961 return NULL;
962
963 /* We may have found a location that is at ADDRESS but is not the first in the
964 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 965 while ((locp_found - 1) >= bp_locations
b775012e
LM
966 && (*(locp_found - 1))->address == address)
967 locp_found--;
968
969 return locp_found;
970}
971
adc36818 972void
7a26bd4d 973set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
974 int from_tty)
975{
3a5c3e22
PA
976 xfree (b->cond_string);
977 b->cond_string = NULL;
adc36818 978
3a5c3e22 979 if (is_watchpoint (b))
adc36818 980 {
3a5c3e22
PA
981 struct watchpoint *w = (struct watchpoint *) b;
982
4d01a485 983 w->cond_exp.reset ();
3a5c3e22
PA
984 }
985 else
986 {
987 struct bp_location *loc;
988
989 for (loc = b->loc; loc; loc = loc->next)
990 {
4d01a485 991 loc->cond.reset ();
b775012e
LM
992
993 /* No need to free the condition agent expression
994 bytecode (if we have one). We will handle this
995 when we go through update_global_location_list. */
3a5c3e22 996 }
adc36818 997 }
adc36818
PM
998
999 if (*exp == 0)
1000 {
1001 if (from_tty)
1002 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1003 }
1004 else
1005 {
bbc13ae3 1006 const char *arg = exp;
cc59ec59 1007
adc36818
PM
1008 /* I don't know if it matters whether this is the string the user
1009 typed in or the decompiled expression. */
1010 b->cond_string = xstrdup (arg);
1011 b->condition_not_parsed = 0;
1012
1013 if (is_watchpoint (b))
1014 {
3a5c3e22
PA
1015 struct watchpoint *w = (struct watchpoint *) b;
1016
adc36818
PM
1017 innermost_block = NULL;
1018 arg = exp;
1bb9788d 1019 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1020 if (*arg)
1021 error (_("Junk at end of expression"));
3a5c3e22 1022 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1023 }
1024 else
1025 {
3a5c3e22
PA
1026 struct bp_location *loc;
1027
adc36818
PM
1028 for (loc = b->loc; loc; loc = loc->next)
1029 {
1030 arg = exp;
1031 loc->cond =
1bb9788d
TT
1032 parse_exp_1 (&arg, loc->address,
1033 block_for_pc (loc->address), 0);
adc36818
PM
1034 if (*arg)
1035 error (_("Junk at end of expression"));
1036 }
1037 }
1038 }
b775012e
LM
1039 mark_breakpoint_modified (b);
1040
8d3788bd 1041 observer_notify_breakpoint_modified (b);
adc36818
PM
1042}
1043
d55637df
TT
1044/* Completion for the "condition" command. */
1045
1046static VEC (char_ptr) *
6f937416
PA
1047condition_completer (struct cmd_list_element *cmd,
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. */
1063 if (isdigit (text[1]))
1064 return NULL;
1065 return complete_internalvar (&text[1]);
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)
1078 VEC_safe_push (char_ptr, result, xstrdup (number));
1079 }
d55637df
TT
1080
1081 return result;
1082 }
1083
1084 /* We're completing the expression part. */
6f937416 1085 text = skip_spaces_const (space);
d55637df
TT
1086 return expression_completer (cmd, text, word);
1087}
1088
c906108c
SS
1089/* condition N EXP -- set break condition of breakpoint N to EXP. */
1090
1091static void
fba45db2 1092condition_command (char *arg, int from_tty)
c906108c 1093{
52f0bd74 1094 struct breakpoint *b;
c906108c 1095 char *p;
52f0bd74 1096 int bnum;
c906108c
SS
1097
1098 if (arg == 0)
e2e0b3e5 1099 error_no_arg (_("breakpoint number"));
c906108c
SS
1100
1101 p = arg;
1102 bnum = get_number (&p);
5c44784c 1103 if (bnum == 0)
8a3fe4f8 1104 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1105
1106 ALL_BREAKPOINTS (b)
1107 if (b->number == bnum)
2f069f6f 1108 {
6dddc817
DE
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
2566ad2d 1121 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1122
1123 if (is_breakpoint (b))
44702360 1124 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1125
2f069f6f
JB
1126 return;
1127 }
c906108c 1128
8a3fe4f8 1129 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1130}
1131
a7bdde9e
VP
1132/* Check that COMMAND do not contain commands that are suitable
1133 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1134 Throw if any such commands is found. */
1135
a7bdde9e
VP
1136static void
1137check_no_tracepoint_commands (struct command_line *commands)
1138{
1139 struct command_line *c;
cc59ec59 1140
a7bdde9e
VP
1141 for (c = commands; c; c = c->next)
1142 {
1143 int i;
1144
1145 if (c->control_type == while_stepping_control)
3e43a32a
MS
1146 error (_("The 'while-stepping' command can "
1147 "only be used for tracepoints"));
a7bdde9e
VP
1148
1149 for (i = 0; i < c->body_count; ++i)
1150 check_no_tracepoint_commands ((c->body_list)[i]);
1151
1152 /* Not that command parsing removes leading whitespace and comment
4a64f543 1153 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1154 command directly. */
1155 if (strstr (c->line, "collect ") == c->line)
1156 error (_("The 'collect' command can only be used for tracepoints"));
1157
51661e93
VP
1158 if (strstr (c->line, "teval ") == c->line)
1159 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1160 }
1161}
1162
d77f58be
SS
1163/* Encapsulate tests for different types of tracepoints. */
1164
d9b3f62e
PA
1165static int
1166is_tracepoint_type (enum bptype type)
1167{
1168 return (type == bp_tracepoint
1169 || type == bp_fast_tracepoint
1170 || type == bp_static_tracepoint);
1171}
1172
a7bdde9e 1173int
d77f58be 1174is_tracepoint (const struct breakpoint *b)
a7bdde9e 1175{
d9b3f62e 1176 return is_tracepoint_type (b->type);
a7bdde9e 1177}
d9b3f62e 1178
a5e364af
SM
1179/* Factory function to create an appropriate instance of breakpoint given
1180 TYPE. */
1181
1182static std::unique_ptr<breakpoint>
1183new_breakpoint_from_type (bptype type)
1184{
1185 breakpoint *b;
1186
1187 if (is_tracepoint_type (type))
1188 b = (breakpoint *) new tracepoint ();
1189 else
1190 b = new breakpoint ();
1191
1192 return std::unique_ptr<breakpoint> (b);
1193}
1194
e5dd4106 1195/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1196 breakpoint. This function will throw an exception if a problem is
1197 found. */
48cb2d85 1198
95a42b64
TT
1199static void
1200validate_commands_for_breakpoint (struct breakpoint *b,
1201 struct command_line *commands)
48cb2d85 1202{
d77f58be 1203 if (is_tracepoint (b))
a7bdde9e 1204 {
c9a6ce02 1205 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1206 struct command_line *c;
1207 struct command_line *while_stepping = 0;
c9a6ce02
PA
1208
1209 /* Reset the while-stepping step count. The previous commands
1210 might have included a while-stepping action, while the new
1211 ones might not. */
1212 t->step_count = 0;
1213
1214 /* We need to verify that each top-level element of commands is
1215 valid for tracepoints, that there's at most one
1216 while-stepping element, and that the while-stepping's body
1217 has valid tracing commands excluding nested while-stepping.
1218 We also need to validate the tracepoint action line in the
1219 context of the tracepoint --- validate_actionline actually
1220 has side effects, like setting the tracepoint's
1221 while-stepping STEP_COUNT, in addition to checking if the
1222 collect/teval actions parse and make sense in the
1223 tracepoint's context. */
a7bdde9e
VP
1224 for (c = commands; c; c = c->next)
1225 {
a7bdde9e
VP
1226 if (c->control_type == while_stepping_control)
1227 {
1228 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1229 error (_("The 'while-stepping' command "
1230 "cannot be used for fast tracepoint"));
0fb4aa4b 1231 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1232 error (_("The 'while-stepping' command "
1233 "cannot be used for static tracepoint"));
a7bdde9e
VP
1234
1235 if (while_stepping)
3e43a32a
MS
1236 error (_("The 'while-stepping' command "
1237 "can be used only once"));
a7bdde9e
VP
1238 else
1239 while_stepping = c;
1240 }
c9a6ce02
PA
1241
1242 validate_actionline (c->line, b);
a7bdde9e
VP
1243 }
1244 if (while_stepping)
1245 {
1246 struct command_line *c2;
1247
1248 gdb_assert (while_stepping->body_count == 1);
1249 c2 = while_stepping->body_list[0];
1250 for (; c2; c2 = c2->next)
1251 {
a7bdde9e
VP
1252 if (c2->control_type == while_stepping_control)
1253 error (_("The 'while-stepping' command cannot be nested"));
1254 }
1255 }
1256 }
1257 else
1258 {
1259 check_no_tracepoint_commands (commands);
1260 }
95a42b64
TT
1261}
1262
0fb4aa4b
PA
1263/* Return a vector of all the static tracepoints set at ADDR. The
1264 caller is responsible for releasing the vector. */
1265
1266VEC(breakpoint_p) *
1267static_tracepoints_here (CORE_ADDR addr)
1268{
1269 struct breakpoint *b;
1270 VEC(breakpoint_p) *found = 0;
1271 struct bp_location *loc;
1272
1273 ALL_BREAKPOINTS (b)
1274 if (b->type == bp_static_tracepoint)
1275 {
1276 for (loc = b->loc; loc; loc = loc->next)
1277 if (loc->address == addr)
1278 VEC_safe_push(breakpoint_p, found, b);
1279 }
1280
1281 return found;
1282}
1283
95a42b64 1284/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1285 validate that only allowed commands are included. */
95a42b64
TT
1286
1287void
4a64f543 1288breakpoint_set_commands (struct breakpoint *b,
93921405 1289 command_line_up &&commands)
95a42b64 1290{
93921405 1291 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1292
9add0f1b 1293 decref_counted_command_line (&b->commands);
93921405 1294 b->commands = alloc_counted_command_line (commands.release ());
8d3788bd 1295 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1296}
1297
45a43567
TT
1298/* Set the internal `silent' flag on the breakpoint. Note that this
1299 is not the same as the "silent" that may appear in the breakpoint's
1300 commands. */
1301
1302void
1303breakpoint_set_silent (struct breakpoint *b, int silent)
1304{
1305 int old_silent = b->silent;
1306
1307 b->silent = silent;
1308 if (old_silent != silent)
8d3788bd 1309 observer_notify_breakpoint_modified (b);
45a43567
TT
1310}
1311
1312/* Set the thread for this breakpoint. If THREAD is -1, make the
1313 breakpoint work for any thread. */
1314
1315void
1316breakpoint_set_thread (struct breakpoint *b, int thread)
1317{
1318 int old_thread = b->thread;
1319
1320 b->thread = thread;
1321 if (old_thread != thread)
8d3788bd 1322 observer_notify_breakpoint_modified (b);
45a43567
TT
1323}
1324
1325/* Set the task for this breakpoint. If TASK is 0, make the
1326 breakpoint work for any task. */
1327
1328void
1329breakpoint_set_task (struct breakpoint *b, int task)
1330{
1331 int old_task = b->task;
1332
1333 b->task = task;
1334 if (old_task != task)
8d3788bd 1335 observer_notify_breakpoint_modified (b);
45a43567
TT
1336}
1337
95a42b64
TT
1338void
1339check_tracepoint_command (char *line, void *closure)
a7bdde9e 1340{
9a3c8263 1341 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1342
6f937416 1343 validate_actionline (line, b);
a7bdde9e
VP
1344}
1345
95a42b64
TT
1346/* A structure used to pass information through
1347 map_breakpoint_numbers. */
1348
1349struct commands_info
1350{
1351 /* True if the command was typed at a tty. */
1352 int from_tty;
86b17b60
PA
1353
1354 /* The breakpoint range spec. */
896b6bda 1355 const char *arg;
86b17b60 1356
95a42b64
TT
1357 /* Non-NULL if the body of the commands are being read from this
1358 already-parsed command. */
1359 struct command_line *control;
86b17b60 1360
95a42b64
TT
1361 /* The command lines read from the user, or NULL if they have not
1362 yet been read. */
1363 struct counted_command_line *cmd;
1364};
1365
1366/* A callback for map_breakpoint_numbers that sets the commands for
1367 commands_command. */
1368
c906108c 1369static void
95a42b64 1370do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1371{
9a3c8263 1372 struct commands_info *info = (struct commands_info *) data;
c906108c 1373
95a42b64
TT
1374 if (info->cmd == NULL)
1375 {
93921405 1376 command_line_up l;
5c44784c 1377
95a42b64
TT
1378 if (info->control != NULL)
1379 l = copy_command_lines (info->control->body_list[0]);
1380 else
86b17b60
PA
1381 {
1382 struct cleanup *old_chain;
1383 char *str;
c5aa993b 1384
3e43a32a
MS
1385 str = xstrprintf (_("Type commands for breakpoint(s) "
1386 "%s, one per line."),
86b17b60
PA
1387 info->arg);
1388
1389 old_chain = make_cleanup (xfree, str);
1390
1391 l = read_command_lines (str,
1392 info->from_tty, 1,
d77f58be 1393 (is_tracepoint (b)
86b17b60
PA
1394 ? check_tracepoint_command : 0),
1395 b);
1396
1397 do_cleanups (old_chain);
1398 }
a7bdde9e 1399
93921405 1400 info->cmd = alloc_counted_command_line (l.release ());
95a42b64
TT
1401 }
1402
1403 /* If a breakpoint was on the list more than once, we don't need to
1404 do anything. */
1405 if (b->commands != info->cmd)
1406 {
1407 validate_commands_for_breakpoint (b, info->cmd->commands);
1408 incref_counted_command_line (info->cmd);
1409 decref_counted_command_line (&b->commands);
1410 b->commands = info->cmd;
8d3788bd 1411 observer_notify_breakpoint_modified (b);
c5aa993b 1412 }
95a42b64
TT
1413}
1414
1415static void
896b6bda 1416commands_command_1 (const char *arg, int from_tty,
4a64f543 1417 struct command_line *control)
95a42b64
TT
1418{
1419 struct cleanup *cleanups;
1420 struct commands_info info;
1421
1422 info.from_tty = from_tty;
1423 info.control = control;
1424 info.cmd = NULL;
1425 /* If we read command lines from the user, then `info' will hold an
1426 extra reference to the commands that we must clean up. */
1427 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1428
896b6bda
PA
1429 std::string new_arg;
1430
95a42b64
TT
1431 if (arg == NULL || !*arg)
1432 {
86b17b60 1433 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1434 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1435 breakpoint_count);
95a42b64 1436 else if (breakpoint_count > 0)
896b6bda 1437 new_arg = string_printf ("%d", breakpoint_count);
95a42b64 1438 }
9766ced4 1439 else
896b6bda 1440 new_arg = arg;
86b17b60 1441
896b6bda 1442 info.arg = new_arg.c_str ();
95a42b64 1443
896b6bda 1444 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
95a42b64
TT
1445
1446 if (info.cmd == NULL)
1447 error (_("No breakpoints specified."));
1448
1449 do_cleanups (cleanups);
1450}
1451
1452static void
1453commands_command (char *arg, int from_tty)
1454{
1455 commands_command_1 (arg, from_tty, NULL);
c906108c 1456}
40c03ae8
EZ
1457
1458/* Like commands_command, but instead of reading the commands from
1459 input stream, takes them from an already parsed command structure.
1460
1461 This is used by cli-script.c to DTRT with breakpoint commands
1462 that are part of if and while bodies. */
1463enum command_control_type
896b6bda 1464commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1465{
95a42b64
TT
1466 commands_command_1 (arg, 0, cmd);
1467 return simple_control;
40c03ae8 1468}
876fa593
JK
1469
1470/* Return non-zero if BL->TARGET_INFO contains valid information. */
1471
1472static int
1473bp_location_has_shadow (struct bp_location *bl)
1474{
1475 if (bl->loc_type != bp_loc_software_breakpoint)
1476 return 0;
1477 if (!bl->inserted)
1478 return 0;
1479 if (bl->target_info.shadow_len == 0)
e5dd4106 1480 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1481 return 0;
1482 return 1;
1483}
1484
9d497a19
PA
1485/* Update BUF, which is LEN bytes read from the target address
1486 MEMADDR, by replacing a memory breakpoint with its shadowed
1487 contents.
1488
1489 If READBUF is not NULL, this buffer must not overlap with the of
1490 the breakpoint location's shadow_contents buffer. Otherwise, a
1491 failed assertion internal error will be raised. */
1492
1493static void
1494one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1495 const gdb_byte *writebuf_org,
1496 ULONGEST memaddr, LONGEST len,
1497 struct bp_target_info *target_info,
1498 struct gdbarch *gdbarch)
1499{
1500 /* Now do full processing of the found relevant range of elements. */
1501 CORE_ADDR bp_addr = 0;
1502 int bp_size = 0;
1503 int bptoffset = 0;
1504
1505 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1506 current_program_space->aspace, 0))
1507 {
1508 /* The breakpoint is inserted in a different address space. */
1509 return;
1510 }
1511
1512 /* Addresses and length of the part of the breakpoint that
1513 we need to copy. */
1514 bp_addr = target_info->placed_address;
1515 bp_size = target_info->shadow_len;
1516
1517 if (bp_addr + bp_size <= memaddr)
1518 {
1519 /* The breakpoint is entirely before the chunk of memory we are
1520 reading. */
1521 return;
1522 }
1523
1524 if (bp_addr >= memaddr + len)
1525 {
1526 /* The breakpoint is entirely after the chunk of memory we are
1527 reading. */
1528 return;
1529 }
1530
1531 /* Offset within shadow_contents. */
1532 if (bp_addr < memaddr)
1533 {
1534 /* Only copy the second part of the breakpoint. */
1535 bp_size -= memaddr - bp_addr;
1536 bptoffset = memaddr - bp_addr;
1537 bp_addr = memaddr;
1538 }
1539
1540 if (bp_addr + bp_size > memaddr + len)
1541 {
1542 /* Only copy the first part of the breakpoint. */
1543 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1544 }
1545
1546 if (readbuf != NULL)
1547 {
1548 /* Verify that the readbuf buffer does not overlap with the
1549 shadow_contents buffer. */
1550 gdb_assert (target_info->shadow_contents >= readbuf + len
1551 || readbuf >= (target_info->shadow_contents
1552 + target_info->shadow_len));
1553
1554 /* Update the read buffer with this inserted breakpoint's
1555 shadow. */
1556 memcpy (readbuf + bp_addr - memaddr,
1557 target_info->shadow_contents + bptoffset, bp_size);
1558 }
1559 else
1560 {
1561 const unsigned char *bp;
0d5ed153
MR
1562 CORE_ADDR addr = target_info->reqstd_address;
1563 int placed_size;
9d497a19
PA
1564
1565 /* Update the shadow with what we want to write to memory. */
1566 memcpy (target_info->shadow_contents + bptoffset,
1567 writebuf_org + bp_addr - memaddr, bp_size);
1568
1569 /* Determine appropriate breakpoint contents and size for this
1570 address. */
0d5ed153 1571 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1572
1573 /* Update the final write buffer with this inserted
1574 breakpoint's INSN. */
1575 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1576 }
1577}
1578
8defab1a 1579/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1580 by replacing any memory breakpoints with their shadowed contents.
1581
35c63cd8
JB
1582 If READBUF is not NULL, this buffer must not overlap with any of
1583 the breakpoint location's shadow_contents buffers. Otherwise,
1584 a failed assertion internal error will be raised.
1585
876fa593 1586 The range of shadowed area by each bp_location is:
f5336ca5
PA
1587 bl->address - bp_locations_placed_address_before_address_max
1588 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1589 The range we were requested to resolve shadows for is:
1590 memaddr ... memaddr + len
1591 Thus the safe cutoff boundaries for performance optimization are
35df4500 1592 memaddr + len <= (bl->address
f5336ca5 1593 - bp_locations_placed_address_before_address_max)
876fa593 1594 and:
f5336ca5 1595 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1596
8defab1a 1597void
f0ba3972
PA
1598breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1599 const gdb_byte *writebuf_org,
1600 ULONGEST memaddr, LONGEST len)
c906108c 1601{
4a64f543
MS
1602 /* Left boundary, right boundary and median element of our binary
1603 search. */
876fa593
JK
1604 unsigned bc_l, bc_r, bc;
1605
4a64f543
MS
1606 /* Find BC_L which is a leftmost element which may affect BUF
1607 content. It is safe to report lower value but a failure to
1608 report higher one. */
876fa593
JK
1609
1610 bc_l = 0;
f5336ca5 1611 bc_r = bp_locations_count;
876fa593
JK
1612 while (bc_l + 1 < bc_r)
1613 {
35df4500 1614 struct bp_location *bl;
876fa593
JK
1615
1616 bc = (bc_l + bc_r) / 2;
f5336ca5 1617 bl = bp_locations[bc];
876fa593 1618
4a64f543
MS
1619 /* Check first BL->ADDRESS will not overflow due to the added
1620 constant. Then advance the left boundary only if we are sure
1621 the BC element can in no way affect the BUF content (MEMADDR
1622 to MEMADDR + LEN range).
876fa593 1623
f5336ca5 1624 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1625 offset so that we cannot miss a breakpoint with its shadow
1626 range tail still reaching MEMADDR. */
c5aa993b 1627
f5336ca5 1628 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1629 >= bl->address)
f5336ca5 1630 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1631 <= memaddr))
876fa593
JK
1632 bc_l = bc;
1633 else
1634 bc_r = bc;
1635 }
1636
128070bb
PA
1637 /* Due to the binary search above, we need to make sure we pick the
1638 first location that's at BC_L's address. E.g., if there are
1639 multiple locations at the same address, BC_L may end up pointing
1640 at a duplicate location, and miss the "master"/"inserted"
1641 location. Say, given locations L1, L2 and L3 at addresses A and
1642 B:
1643
1644 L1@A, L2@A, L3@B, ...
1645
1646 BC_L could end up pointing at location L2, while the "master"
1647 location could be L1. Since the `loc->inserted' flag is only set
1648 on "master" locations, we'd forget to restore the shadow of L1
1649 and L2. */
1650 while (bc_l > 0
f5336ca5 1651 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1652 bc_l--;
1653
876fa593
JK
1654 /* Now do full processing of the found relevant range of elements. */
1655
f5336ca5 1656 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1657 {
f5336ca5 1658 struct bp_location *bl = bp_locations[bc];
876fa593 1659
35df4500
TJB
1660 /* bp_location array has BL->OWNER always non-NULL. */
1661 if (bl->owner->type == bp_none)
8a3fe4f8 1662 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1663 bl->owner->number);
ffce0d52 1664
e5dd4106 1665 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1666 content. */
1667
f5336ca5
PA
1668 if (bl->address >= bp_locations_placed_address_before_address_max
1669 && memaddr + len <= (bl->address
1670 - bp_locations_placed_address_before_address_max))
876fa593
JK
1671 break;
1672
35df4500 1673 if (!bp_location_has_shadow (bl))
c5aa993b 1674 continue;
6c95b8df 1675
9d497a19
PA
1676 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1677 memaddr, len, &bl->target_info, bl->gdbarch);
1678 }
c906108c 1679}
9d497a19 1680
c906108c 1681\f
c5aa993b 1682
b775012e
LM
1683/* Return true if BPT is either a software breakpoint or a hardware
1684 breakpoint. */
1685
1686int
1687is_breakpoint (const struct breakpoint *bpt)
1688{
1689 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1690 || bpt->type == bp_hardware_breakpoint
1691 || bpt->type == bp_dprintf);
b775012e
LM
1692}
1693
60e1c644
PA
1694/* Return true if BPT is of any hardware watchpoint kind. */
1695
a5606eee 1696static int
d77f58be 1697is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1698{
1699 return (bpt->type == bp_hardware_watchpoint
1700 || bpt->type == bp_read_watchpoint
1701 || bpt->type == bp_access_watchpoint);
1702}
7270d8f2 1703
60e1c644
PA
1704/* Return true if BPT is of any watchpoint kind, hardware or
1705 software. */
1706
3a5c3e22 1707int
d77f58be 1708is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1709{
1710 return (is_hardware_watchpoint (bpt)
1711 || bpt->type == bp_watchpoint);
1712}
1713
3a5c3e22
PA
1714/* Returns true if the current thread and its running state are safe
1715 to evaluate or update watchpoint B. Watchpoints on local
1716 expressions need to be evaluated in the context of the thread that
1717 was current when the watchpoint was created, and, that thread needs
1718 to be stopped to be able to select the correct frame context.
1719 Watchpoints on global expressions can be evaluated on any thread,
1720 and in any state. It is presently left to the target allowing
1721 memory accesses when threads are running. */
f6bc2008
PA
1722
1723static int
3a5c3e22 1724watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1725{
d0d8b0c6
JK
1726 return (b->base.pspace == current_program_space
1727 && (ptid_equal (b->watchpoint_thread, null_ptid)
1728 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1729 && !is_executing (inferior_ptid))));
f6bc2008
PA
1730}
1731
d0fb5eae
JK
1732/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1733 associated bp_watchpoint_scope breakpoint. */
1734
1735static void
3a5c3e22 1736watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1737{
3a5c3e22 1738 struct breakpoint *b = &w->base;
d0fb5eae
JK
1739
1740 if (b->related_breakpoint != b)
1741 {
1742 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1743 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1744 b->related_breakpoint->disposition = disp_del_at_next_stop;
1745 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1746 b->related_breakpoint = b;
1747 }
1748 b->disposition = disp_del_at_next_stop;
1749}
1750
bb9d5f81
PP
1751/* Extract a bitfield value from value VAL using the bit parameters contained in
1752 watchpoint W. */
1753
1754static struct value *
1755extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1756{
1757 struct value *bit_val;
1758
1759 if (val == NULL)
1760 return NULL;
1761
1762 bit_val = allocate_value (value_type (val));
1763
1764 unpack_value_bitfield (bit_val,
1765 w->val_bitpos,
1766 w->val_bitsize,
1767 value_contents_for_printing (val),
1768 value_offset (val),
1769 val);
1770
1771 return bit_val;
1772}
1773
c6d81124
PA
1774/* Allocate a dummy location and add it to B, which must be a software
1775 watchpoint. This is required because even if a software watchpoint
1776 is not watching any memory, bpstat_stop_status requires a location
1777 to be able to report stops. */
1778
1779static void
1780software_watchpoint_add_no_memory_location (struct breakpoint *b,
1781 struct program_space *pspace)
1782{
1783 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1784
1785 b->loc = allocate_bp_location (b);
1786 b->loc->pspace = pspace;
1787 b->loc->address = -1;
1788 b->loc->length = -1;
1789}
1790
1791/* Returns true if B is a software watchpoint that is not watching any
1792 memory (e.g., "watch $pc"). */
1793
1794static int
1795is_no_memory_software_watchpoint (struct breakpoint *b)
1796{
1797 return (b->type == bp_watchpoint
1798 && b->loc != NULL
1799 && b->loc->next == NULL
1800 && b->loc->address == -1
1801 && b->loc->length == -1);
1802}
1803
567e1b4e
JB
1804/* Assuming that B is a watchpoint:
1805 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1806 - Evaluate expression and store the result in B->val
567e1b4e
JB
1807 - Evaluate the condition if there is one, and store the result
1808 in b->loc->cond.
a5606eee
VP
1809 - Update the list of values that must be watched in B->loc.
1810
4a64f543
MS
1811 If the watchpoint disposition is disp_del_at_next_stop, then do
1812 nothing. If this is local watchpoint that is out of scope, delete
1813 it.
1814
1815 Even with `set breakpoint always-inserted on' the watchpoints are
1816 removed + inserted on each stop here. Normal breakpoints must
1817 never be removed because they might be missed by a running thread
1818 when debugging in non-stop mode. On the other hand, hardware
1819 watchpoints (is_hardware_watchpoint; processed here) are specific
1820 to each LWP since they are stored in each LWP's hardware debug
1821 registers. Therefore, such LWP must be stopped first in order to
1822 be able to modify its hardware watchpoints.
1823
1824 Hardware watchpoints must be reset exactly once after being
1825 presented to the user. It cannot be done sooner, because it would
1826 reset the data used to present the watchpoint hit to the user. And
1827 it must not be done later because it could display the same single
1828 watchpoint hit during multiple GDB stops. Note that the latter is
1829 relevant only to the hardware watchpoint types bp_read_watchpoint
1830 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1831 not user-visible - its hit is suppressed if the memory content has
1832 not changed.
1833
1834 The following constraints influence the location where we can reset
1835 hardware watchpoints:
1836
1837 * target_stopped_by_watchpoint and target_stopped_data_address are
1838 called several times when GDB stops.
1839
1840 [linux]
1841 * Multiple hardware watchpoints can be hit at the same time,
1842 causing GDB to stop. GDB only presents one hardware watchpoint
1843 hit at a time as the reason for stopping, and all the other hits
1844 are presented later, one after the other, each time the user
1845 requests the execution to be resumed. Execution is not resumed
1846 for the threads still having pending hit event stored in
1847 LWP_INFO->STATUS. While the watchpoint is already removed from
1848 the inferior on the first stop the thread hit event is kept being
1849 reported from its cached value by linux_nat_stopped_data_address
1850 until the real thread resume happens after the watchpoint gets
1851 presented and thus its LWP_INFO->STATUS gets reset.
1852
1853 Therefore the hardware watchpoint hit can get safely reset on the
1854 watchpoint removal from inferior. */
a79d3c27 1855
b40ce68a 1856static void
3a5c3e22 1857update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1858{
a5606eee 1859 int within_current_scope;
a5606eee 1860 struct frame_id saved_frame_id;
66076460 1861 int frame_saved;
a5606eee 1862
f6bc2008
PA
1863 /* If this is a local watchpoint, we only want to check if the
1864 watchpoint frame is in scope if the current thread is the thread
1865 that was used to create the watchpoint. */
1866 if (!watchpoint_in_thread_scope (b))
1867 return;
1868
3a5c3e22 1869 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1870 return;
1871
66076460 1872 frame_saved = 0;
a5606eee
VP
1873
1874 /* Determine if the watchpoint is within scope. */
1875 if (b->exp_valid_block == NULL)
1876 within_current_scope = 1;
1877 else
1878 {
b5db5dfc
UW
1879 struct frame_info *fi = get_current_frame ();
1880 struct gdbarch *frame_arch = get_frame_arch (fi);
1881 CORE_ADDR frame_pc = get_frame_pc (fi);
1882
c9cf6e20
MG
1883 /* If we're at a point where the stack has been destroyed
1884 (e.g. in a function epilogue), unwinding may not work
1885 properly. Do not attempt to recreate locations at this
b5db5dfc 1886 point. See similar comments in watchpoint_check. */
c9cf6e20 1887 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1888 return;
66076460
DJ
1889
1890 /* Save the current frame's ID so we can restore it after
1891 evaluating the watchpoint expression on its own frame. */
1892 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1893 took a frame parameter, so that we didn't have to change the
1894 selected frame. */
1895 frame_saved = 1;
1896 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1897
a5606eee
VP
1898 fi = frame_find_by_id (b->watchpoint_frame);
1899 within_current_scope = (fi != NULL);
1900 if (within_current_scope)
1901 select_frame (fi);
1902 }
1903
b5db5dfc
UW
1904 /* We don't free locations. They are stored in the bp_location array
1905 and update_global_location_list will eventually delete them and
1906 remove breakpoints if needed. */
3a5c3e22 1907 b->base.loc = NULL;
b5db5dfc 1908
a5606eee
VP
1909 if (within_current_scope && reparse)
1910 {
bbc13ae3 1911 const char *s;
d63d0675 1912
4d01a485 1913 b->exp.reset ();
d63d0675 1914 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1915 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1916 /* If the meaning of expression itself changed, the old value is
1917 no longer relevant. We don't want to report a watchpoint hit
1918 to the user when the old value and the new value may actually
1919 be completely different objects. */
1920 value_free (b->val);
fa4727a6
DJ
1921 b->val = NULL;
1922 b->val_valid = 0;
60e1c644
PA
1923
1924 /* Note that unlike with breakpoints, the watchpoint's condition
1925 expression is stored in the breakpoint object, not in the
1926 locations (re)created below. */
3a5c3e22 1927 if (b->base.cond_string != NULL)
60e1c644 1928 {
4d01a485 1929 b->cond_exp.reset ();
60e1c644 1930
3a5c3e22 1931 s = b->base.cond_string;
1bb9788d 1932 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1933 }
a5606eee 1934 }
a5606eee
VP
1935
1936 /* If we failed to parse the expression, for example because
1937 it refers to a global variable in a not-yet-loaded shared library,
1938 don't try to insert watchpoint. We don't automatically delete
1939 such watchpoint, though, since failure to parse expression
1940 is different from out-of-scope watchpoint. */
e8369a73 1941 if (!target_has_execution)
2d134ed3
PA
1942 {
1943 /* Without execution, memory can't change. No use to try and
1944 set watchpoint locations. The watchpoint will be reset when
1945 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1946 if (!can_use_hw_watchpoints)
1947 {
1948 if (b->base.ops->works_in_software_mode (&b->base))
1949 b->base.type = bp_watchpoint;
1950 else
638aa5a1
AB
1951 error (_("Can't set read/access watchpoint when "
1952 "hardware watchpoints are disabled."));
e8369a73 1953 }
2d134ed3
PA
1954 }
1955 else if (within_current_scope && b->exp)
a5606eee 1956 {
0cf6dd15 1957 int pc = 0;
fa4727a6 1958 struct value *val_chain, *v, *result, *next;
2d134ed3 1959 struct program_space *frame_pspace;
a5606eee 1960
4d01a485 1961 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1962
a5606eee
VP
1963 /* Avoid setting b->val if it's already set. The meaning of
1964 b->val is 'the last value' user saw, and we should update
1965 it only if we reported that last value to user. As it
9c06b0b4
TJB
1966 happens, the code that reports it updates b->val directly.
1967 We don't keep track of the memory value for masked
1968 watchpoints. */
3a5c3e22 1969 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1970 {
bb9d5f81
PP
1971 if (b->val_bitsize != 0)
1972 {
1973 v = extract_bitfield_from_watchpoint_value (b, v);
1974 if (v != NULL)
1975 release_value (v);
1976 }
fa4727a6
DJ
1977 b->val = v;
1978 b->val_valid = 1;
1979 }
a5606eee 1980
2d134ed3
PA
1981 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1982
a5606eee 1983 /* Look at each value on the value chain. */
9fa40276 1984 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1985 {
1986 /* If it's a memory location, and GDB actually needed
1987 its contents to evaluate the expression, then we
fa4727a6
DJ
1988 must watch it. If the first value returned is
1989 still lazy, that means an error occurred reading it;
1990 watch it anyway in case it becomes readable. */
a5606eee 1991 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1992 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1993 {
1994 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1995
a5606eee
VP
1996 /* We only watch structs and arrays if user asked
1997 for it explicitly, never if they just happen to
1998 appear in the middle of some value chain. */
fa4727a6 1999 if (v == result
a5606eee
VP
2000 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2001 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2002 {
2003 CORE_ADDR addr;
f486487f 2004 enum target_hw_bp_type type;
a5606eee 2005 struct bp_location *loc, **tmp;
bb9d5f81
PP
2006 int bitpos = 0, bitsize = 0;
2007
2008 if (value_bitsize (v) != 0)
2009 {
2010 /* Extract the bit parameters out from the bitfield
2011 sub-expression. */
2012 bitpos = value_bitpos (v);
2013 bitsize = value_bitsize (v);
2014 }
2015 else if (v == result && b->val_bitsize != 0)
2016 {
2017 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2018 lvalue whose bit parameters are saved in the fields
2019 VAL_BITPOS and VAL_BITSIZE. */
2020 bitpos = b->val_bitpos;
2021 bitsize = b->val_bitsize;
2022 }
a5606eee 2023
42ae5230 2024 addr = value_address (v);
bb9d5f81
PP
2025 if (bitsize != 0)
2026 {
2027 /* Skip the bytes that don't contain the bitfield. */
2028 addr += bitpos / 8;
2029 }
2030
a5606eee 2031 type = hw_write;
3a5c3e22 2032 if (b->base.type == bp_read_watchpoint)
a5606eee 2033 type = hw_read;
3a5c3e22 2034 else if (b->base.type == bp_access_watchpoint)
a5606eee 2035 type = hw_access;
3a5c3e22
PA
2036
2037 loc = allocate_bp_location (&b->base);
2038 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2039 ;
2040 *tmp = loc;
a6d9a66e 2041 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2042
2043 loc->pspace = frame_pspace;
a5606eee 2044 loc->address = addr;
bb9d5f81
PP
2045
2046 if (bitsize != 0)
2047 {
2048 /* Just cover the bytes that make up the bitfield. */
2049 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2050 }
2051 else
2052 loc->length = TYPE_LENGTH (value_type (v));
2053
a5606eee
VP
2054 loc->watchpoint_type = type;
2055 }
2056 }
9fa40276
TJB
2057 }
2058
2059 /* Change the type of breakpoint between hardware assisted or
2060 an ordinary watchpoint depending on the hardware support
2061 and free hardware slots. REPARSE is set when the inferior
2062 is started. */
a9634178 2063 if (reparse)
9fa40276 2064 {
e09342b5 2065 int reg_cnt;
9fa40276
TJB
2066 enum bp_loc_type loc_type;
2067 struct bp_location *bl;
a5606eee 2068
a9634178 2069 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2070
2071 if (reg_cnt)
9fa40276
TJB
2072 {
2073 int i, target_resources_ok, other_type_used;
a1398e0c 2074 enum bptype type;
9fa40276 2075
a9634178
TJB
2076 /* Use an exact watchpoint when there's only one memory region to be
2077 watched, and only one debug register is needed to watch it. */
2078 b->exact = target_exact_watchpoints && reg_cnt == 1;
2079
9fa40276 2080 /* We need to determine how many resources are already
e09342b5
TJB
2081 used for all other hardware watchpoints plus this one
2082 to see if we still have enough resources to also fit
a1398e0c
PA
2083 this watchpoint in as well. */
2084
2085 /* If this is a software watchpoint, we try to turn it
2086 to a hardware one -- count resources as if B was of
2087 hardware watchpoint type. */
2088 type = b->base.type;
2089 if (type == bp_watchpoint)
2090 type = bp_hardware_watchpoint;
2091
2092 /* This watchpoint may or may not have been placed on
2093 the list yet at this point (it won't be in the list
2094 if we're trying to create it for the first time,
2095 through watch_command), so always account for it
2096 manually. */
2097
2098 /* Count resources used by all watchpoints except B. */
2099 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2100
2101 /* Add in the resources needed for B. */
2102 i += hw_watchpoint_use_count (&b->base);
2103
2104 target_resources_ok
2105 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2106 if (target_resources_ok <= 0)
a9634178 2107 {
3a5c3e22 2108 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2109
2110 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2111 error (_("Target does not support this type of "
2112 "hardware watchpoint."));
9c06b0b4
TJB
2113 else if (target_resources_ok < 0 && !sw_mode)
2114 error (_("There are not enough available hardware "
2115 "resources for this watchpoint."));
a1398e0c
PA
2116
2117 /* Downgrade to software watchpoint. */
2118 b->base.type = bp_watchpoint;
2119 }
2120 else
2121 {
2122 /* If this was a software watchpoint, we've just
2123 found we have enough resources to turn it to a
2124 hardware watchpoint. Otherwise, this is a
2125 nop. */
2126 b->base.type = type;
a9634178 2127 }
9fa40276 2128 }
3a5c3e22 2129 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2130 {
2131 if (!can_use_hw_watchpoints)
2132 error (_("Can't set read/access watchpoint when "
2133 "hardware watchpoints are disabled."));
2134 else
2135 error (_("Expression cannot be implemented with "
2136 "read/access watchpoint."));
2137 }
9fa40276 2138 else
3a5c3e22 2139 b->base.type = bp_watchpoint;
9fa40276 2140
3a5c3e22 2141 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2142 : bp_loc_hardware_watchpoint);
3a5c3e22 2143 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2144 bl->loc_type = loc_type;
2145 }
2146
2147 for (v = val_chain; v; v = next)
2148 {
a5606eee
VP
2149 next = value_next (v);
2150 if (v != b->val)
2151 value_free (v);
2152 }
2153
c7437ca6
PA
2154 /* If a software watchpoint is not watching any memory, then the
2155 above left it without any location set up. But,
2156 bpstat_stop_status requires a location to be able to report
2157 stops, so make sure there's at least a dummy one. */
3a5c3e22 2158 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2159 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2160 }
2161 else if (!within_current_scope)
7270d8f2 2162 {
ac74f770
MS
2163 printf_filtered (_("\
2164Watchpoint %d deleted because the program has left the block\n\
2165in which its expression is valid.\n"),
3a5c3e22 2166 b->base.number);
d0fb5eae 2167 watchpoint_del_at_next_stop (b);
7270d8f2 2168 }
a5606eee
VP
2169
2170 /* Restore the selected frame. */
66076460
DJ
2171 if (frame_saved)
2172 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2173}
2174
a5606eee 2175
74960c60 2176/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2177 inserted in the inferior. We don't differentiate the type of BL's owner
2178 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2179 breakpoint_ops is not defined, because in insert_bp_location,
2180 tracepoint's insert_location will not be called. */
74960c60 2181static int
35df4500 2182should_be_inserted (struct bp_location *bl)
74960c60 2183{
35df4500 2184 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2185 return 0;
2186
35df4500 2187 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2188 return 0;
2189
35df4500 2190 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2191 return 0;
2192
f8eba3c6
TT
2193 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2194 return 0;
2195
56710373
PA
2196 /* This is set for example, when we're attached to the parent of a
2197 vfork, and have detached from the child. The child is running
2198 free, and we expect it to do an exec or exit, at which point the
2199 OS makes the parent schedulable again (and the target reports
2200 that the vfork is done). Until the child is done with the shared
2201 memory region, do not insert breakpoints in the parent, otherwise
2202 the child could still trip on the parent's breakpoints. Since
2203 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2204 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2205 return 0;
2206
31e77af2 2207 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2208 location, except if the breakpoint is a single-step breakpoint,
2209 and the breakpoint's thread is the thread which is stepping past
2210 a breakpoint. */
31e77af2
PA
2211 if ((bl->loc_type == bp_loc_software_breakpoint
2212 || bl->loc_type == bp_loc_hardware_breakpoint)
2213 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2214 bl->address)
2215 /* The single-step breakpoint may be inserted at the location
2216 we're trying to step if the instruction branches to itself.
2217 However, the instruction won't be executed at all and it may
2218 break the semantics of the instruction, for example, the
2219 instruction is a conditional branch or updates some flags.
2220 We can't fix it unless GDB is able to emulate the instruction
2221 or switch to displaced stepping. */
2222 && !(bl->owner->type == bp_single_step
2223 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2224 {
2225 if (debug_infrun)
2226 {
2227 fprintf_unfiltered (gdb_stdlog,
2228 "infrun: skipping breakpoint: "
2229 "stepping past insn at: %s\n",
2230 paddress (bl->gdbarch, bl->address));
2231 }
2232 return 0;
2233 }
31e77af2 2234
963f9c80
PA
2235 /* Don't insert watchpoints if we're trying to step past the
2236 instruction that triggered one. */
2237 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2238 && stepping_past_nonsteppable_watchpoint ())
2239 {
2240 if (debug_infrun)
2241 {
2242 fprintf_unfiltered (gdb_stdlog,
2243 "infrun: stepping past non-steppable watchpoint. "
2244 "skipping watchpoint at %s:%d\n",
2245 paddress (bl->gdbarch, bl->address),
2246 bl->length);
2247 }
2248 return 0;
2249 }
2250
74960c60
VP
2251 return 1;
2252}
2253
934709f0
PW
2254/* Same as should_be_inserted but does the check assuming
2255 that the location is not duplicated. */
2256
2257static int
2258unduplicated_should_be_inserted (struct bp_location *bl)
2259{
2260 int result;
2261 const int save_duplicate = bl->duplicate;
2262
2263 bl->duplicate = 0;
2264 result = should_be_inserted (bl);
2265 bl->duplicate = save_duplicate;
2266 return result;
2267}
2268
b775012e
LM
2269/* Parses a conditional described by an expression COND into an
2270 agent expression bytecode suitable for evaluation
2271 by the bytecode interpreter. Return NULL if there was
2272 any error during parsing. */
2273
833177a4 2274static agent_expr_up
b775012e
LM
2275parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2276{
833177a4 2277 if (cond == NULL)
b775012e
LM
2278 return NULL;
2279
833177a4
PA
2280 agent_expr_up aexpr;
2281
b775012e
LM
2282 /* We don't want to stop processing, so catch any errors
2283 that may show up. */
492d29ea 2284 TRY
b775012e 2285 {
036e657b 2286 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2287 }
2288
492d29ea 2289 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2290 {
2291 /* If we got here, it means the condition could not be parsed to a valid
2292 bytecode expression and thus can't be evaluated on the target's side.
2293 It's no use iterating through the conditions. */
b775012e 2294 }
492d29ea 2295 END_CATCH
b775012e
LM
2296
2297 /* We have a valid agent expression. */
2298 return aexpr;
2299}
2300
2301/* Based on location BL, create a list of breakpoint conditions to be
2302 passed on to the target. If we have duplicated locations with different
2303 conditions, we will add such conditions to the list. The idea is that the
2304 target will evaluate the list of conditions and will only notify GDB when
2305 one of them is true. */
2306
2307static void
2308build_target_condition_list (struct bp_location *bl)
2309{
2310 struct bp_location **locp = NULL, **loc2p;
2311 int null_condition_or_parse_error = 0;
2312 int modified = bl->needs_update;
2313 struct bp_location *loc;
2314
8b4f3082 2315 /* Release conditions left over from a previous insert. */
3cde5c42 2316 bl->target_info.conditions.clear ();
8b4f3082 2317
b775012e
LM
2318 /* This is only meaningful if the target is
2319 evaluating conditions and if the user has
2320 opted for condition evaluation on the target's
2321 side. */
2322 if (gdb_evaluates_breakpoint_condition_p ()
2323 || !target_supports_evaluation_of_breakpoint_conditions ())
2324 return;
2325
2326 /* Do a first pass to check for locations with no assigned
2327 conditions or conditions that fail to parse to a valid agent expression
2328 bytecode. If any of these happen, then it's no use to send conditions
2329 to the target since this location will always trigger and generate a
2330 response back to GDB. */
2331 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2332 {
2333 loc = (*loc2p);
2334 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2335 {
2336 if (modified)
2337 {
b775012e
LM
2338 /* Re-parse the conditions since something changed. In that
2339 case we already freed the condition bytecodes (see
2340 force_breakpoint_reinsertion). We just
2341 need to parse the condition to bytecodes again. */
833177a4
PA
2342 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2343 loc->cond.get ());
b775012e
LM
2344 }
2345
2346 /* If we have a NULL bytecode expression, it means something
2347 went wrong or we have a null condition expression. */
2348 if (!loc->cond_bytecode)
2349 {
2350 null_condition_or_parse_error = 1;
2351 break;
2352 }
2353 }
2354 }
2355
2356 /* If any of these happened, it means we will have to evaluate the conditions
2357 for the location's address on gdb's side. It is no use keeping bytecodes
2358 for all the other duplicate locations, thus we free all of them here.
2359
2360 This is so we have a finer control over which locations' conditions are
2361 being evaluated by GDB or the remote stub. */
2362 if (null_condition_or_parse_error)
2363 {
2364 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2365 {
2366 loc = (*loc2p);
2367 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2368 {
2369 /* Only go as far as the first NULL bytecode is
2370 located. */
2371 if (!loc->cond_bytecode)
2372 return;
2373
833177a4 2374 loc->cond_bytecode.reset ();
b775012e
LM
2375 }
2376 }
2377 }
2378
2379 /* No NULL conditions or failed bytecode generation. Build a condition list
2380 for this location's address. */
2381 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2382 {
2383 loc = (*loc2p);
2384 if (loc->cond
2385 && is_breakpoint (loc->owner)
2386 && loc->pspace->num == bl->pspace->num
2387 && loc->owner->enable_state == bp_enabled
2388 && loc->enabled)
3cde5c42
PA
2389 {
2390 /* Add the condition to the vector. This will be used later
2391 to send the conditions to the target. */
2392 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2393 }
b775012e
LM
2394 }
2395
2396 return;
2397}
2398
d3ce09f5
SS
2399/* Parses a command described by string CMD into an agent expression
2400 bytecode suitable for evaluation by the bytecode interpreter.
2401 Return NULL if there was any error during parsing. */
2402
833177a4 2403static agent_expr_up
d3ce09f5
SS
2404parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2405{
2406 struct cleanup *old_cleanups = 0;
4d01a485 2407 struct expression **argvec;
bbc13ae3
KS
2408 const char *cmdrest;
2409 const char *format_start, *format_end;
d3ce09f5
SS
2410 struct format_piece *fpieces;
2411 int nargs;
2412 struct gdbarch *gdbarch = get_current_arch ();
2413
833177a4 2414 if (cmd == NULL)
d3ce09f5
SS
2415 return NULL;
2416
2417 cmdrest = cmd;
2418
2419 if (*cmdrest == ',')
2420 ++cmdrest;
bbc13ae3 2421 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2422
2423 if (*cmdrest++ != '"')
2424 error (_("No format string following the location"));
2425
2426 format_start = cmdrest;
2427
2428 fpieces = parse_format_string (&cmdrest);
2429
2430 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2431
2432 format_end = cmdrest;
2433
2434 if (*cmdrest++ != '"')
2435 error (_("Bad format string, non-terminated '\"'."));
2436
bbc13ae3 2437 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2438
2439 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2440 error (_("Invalid argument syntax"));
2441
2442 if (*cmdrest == ',')
2443 cmdrest++;
bbc13ae3 2444 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2445
2446 /* For each argument, make an expression. */
2447
2448 argvec = (struct expression **) alloca (strlen (cmd)
2449 * sizeof (struct expression *));
2450
2451 nargs = 0;
2452 while (*cmdrest != '\0')
2453 {
bbc13ae3 2454 const char *cmd1;
d3ce09f5
SS
2455
2456 cmd1 = cmdrest;
4d01a485
PA
2457 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2458 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2459 cmdrest = cmd1;
2460 if (*cmdrest == ',')
2461 ++cmdrest;
2462 }
2463
833177a4
PA
2464 agent_expr_up aexpr;
2465
d3ce09f5
SS
2466 /* We don't want to stop processing, so catch any errors
2467 that may show up. */
492d29ea 2468 TRY
d3ce09f5 2469 {
036e657b
JB
2470 aexpr = gen_printf (scope, gdbarch, 0, 0,
2471 format_start, format_end - format_start,
2472 fpieces, nargs, argvec);
d3ce09f5 2473 }
492d29ea 2474 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2475 {
2476 /* If we got here, it means the command could not be parsed to a valid
2477 bytecode expression and thus can't be evaluated on the target's side.
2478 It's no use iterating through the other commands. */
d3ce09f5 2479 }
492d29ea
PA
2480 END_CATCH
2481
2482 do_cleanups (old_cleanups);
d3ce09f5 2483
d3ce09f5
SS
2484 /* We have a valid agent expression, return it. */
2485 return aexpr;
2486}
2487
2488/* Based on location BL, create a list of breakpoint commands to be
2489 passed on to the target. If we have duplicated locations with
2490 different commands, we will add any such to the list. */
2491
2492static void
2493build_target_command_list (struct bp_location *bl)
2494{
2495 struct bp_location **locp = NULL, **loc2p;
2496 int null_command_or_parse_error = 0;
2497 int modified = bl->needs_update;
2498 struct bp_location *loc;
2499
3cde5c42
PA
2500 /* Clear commands left over from a previous insert. */
2501 bl->target_info.tcommands.clear ();
8b4f3082 2502
41fac0cf 2503 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2504 return;
2505
41fac0cf
PA
2506 /* For now, limit to agent-style dprintf breakpoints. */
2507 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2508 return;
2509
41fac0cf
PA
2510 /* For now, if we have any duplicate location that isn't a dprintf,
2511 don't install the target-side commands, as that would make the
2512 breakpoint not be reported to the core, and we'd lose
2513 control. */
2514 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2515 {
2516 loc = (*loc2p);
2517 if (is_breakpoint (loc->owner)
2518 && loc->pspace->num == bl->pspace->num
2519 && loc->owner->type != bp_dprintf)
2520 return;
2521 }
2522
d3ce09f5
SS
2523 /* Do a first pass to check for locations with no assigned
2524 conditions or conditions that fail to parse to a valid agent expression
2525 bytecode. If any of these happen, then it's no use to send conditions
2526 to the target since this location will always trigger and generate a
2527 response back to GDB. */
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2532 {
2533 if (modified)
2534 {
d3ce09f5
SS
2535 /* Re-parse the commands since something changed. In that
2536 case we already freed the command bytecodes (see
2537 force_breakpoint_reinsertion). We just
2538 need to parse the command to bytecodes again. */
833177a4
PA
2539 loc->cmd_bytecode
2540 = parse_cmd_to_aexpr (bl->address,
2541 loc->owner->extra_string);
d3ce09f5
SS
2542 }
2543
2544 /* If we have a NULL bytecode expression, it means something
2545 went wrong or we have a null command expression. */
2546 if (!loc->cmd_bytecode)
2547 {
2548 null_command_or_parse_error = 1;
2549 break;
2550 }
2551 }
2552 }
2553
2554 /* If anything failed, then we're not doing target-side commands,
2555 and so clean up. */
2556 if (null_command_or_parse_error)
2557 {
2558 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2559 {
2560 loc = (*loc2p);
2561 if (is_breakpoint (loc->owner)
2562 && loc->pspace->num == bl->pspace->num)
2563 {
2564 /* Only go as far as the first NULL bytecode is
2565 located. */
40fb6c5e 2566 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2567 return;
2568
833177a4 2569 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2570 }
2571 }
2572 }
2573
2574 /* No NULL commands or failed bytecode generation. Build a command list
2575 for this location's address. */
2576 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2577 {
2578 loc = (*loc2p);
2579 if (loc->owner->extra_string
2580 && is_breakpoint (loc->owner)
2581 && loc->pspace->num == bl->pspace->num
2582 && loc->owner->enable_state == bp_enabled
2583 && loc->enabled)
3cde5c42
PA
2584 {
2585 /* Add the command to the vector. This will be used later
2586 to send the commands to the target. */
2587 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2588 }
d3ce09f5
SS
2589 }
2590
2591 bl->target_info.persist = 0;
2592 /* Maybe flag this location as persistent. */
2593 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2594 bl->target_info.persist = 1;
2595}
2596
833b7ab5
YQ
2597/* Return the kind of breakpoint on address *ADDR. Get the kind
2598 of breakpoint according to ADDR except single-step breakpoint.
2599 Get the kind of single-step breakpoint according to the current
2600 registers state. */
cd6c3b4f
YQ
2601
2602static int
2603breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2604{
833b7ab5
YQ
2605 if (bl->owner->type == bp_single_step)
2606 {
2607 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2608 struct regcache *regcache;
2609
2610 regcache = get_thread_regcache (thr->ptid);
2611
2612 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2613 regcache, addr);
2614 }
2615 else
2616 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2617}
2618
35df4500
TJB
2619/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2620 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2621 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2622 Returns 0 for success, 1 if the bp_location type is not supported or
2623 -1 for failure.
879bfdc2 2624
4a64f543
MS
2625 NOTE drow/2003-09-09: This routine could be broken down to an
2626 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2627static int
35df4500 2628insert_bp_location (struct bp_location *bl,
26bb91f3 2629 struct ui_file *tmp_error_stream,
3fbb6ffa 2630 int *disabled_breaks,
dd61ec5c
MW
2631 int *hw_breakpoint_error,
2632 int *hw_bp_error_explained_already)
879bfdc2 2633{
0000e5cc
PA
2634 enum errors bp_err = GDB_NO_ERROR;
2635 const char *bp_err_message = NULL;
879bfdc2 2636
b775012e 2637 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2638 return 0;
2639
35c63cd8
JB
2640 /* Note we don't initialize bl->target_info, as that wipes out
2641 the breakpoint location's shadow_contents if the breakpoint
2642 is still inserted at that location. This in turn breaks
2643 target_read_memory which depends on these buffers when
2644 a memory read is requested at the breakpoint location:
2645 Once the target_info has been wiped, we fail to see that
2646 we have a breakpoint inserted at that address and thus
2647 read the breakpoint instead of returning the data saved in
2648 the breakpoint location's shadow contents. */
0d5ed153 2649 bl->target_info.reqstd_address = bl->address;
35df4500 2650 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2651 bl->target_info.length = bl->length;
8181d85f 2652
b775012e
LM
2653 /* When working with target-side conditions, we must pass all the conditions
2654 for the same breakpoint address down to the target since GDB will not
2655 insert those locations. With a list of breakpoint conditions, the target
2656 can decide when to stop and notify GDB. */
2657
2658 if (is_breakpoint (bl->owner))
2659 {
2660 build_target_condition_list (bl);
d3ce09f5
SS
2661 build_target_command_list (bl);
2662 /* Reset the modification marker. */
b775012e
LM
2663 bl->needs_update = 0;
2664 }
2665
35df4500
TJB
2666 if (bl->loc_type == bp_loc_software_breakpoint
2667 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2668 {
35df4500 2669 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2670 {
2671 /* If the explicitly specified breakpoint type
2672 is not hardware breakpoint, check the memory map to see
2673 if the breakpoint address is in read only memory or not.
4a64f543 2674
765dc015
VP
2675 Two important cases are:
2676 - location type is not hardware breakpoint, memory
2677 is readonly. We change the type of the location to
2678 hardware breakpoint.
4a64f543
MS
2679 - location type is hardware breakpoint, memory is
2680 read-write. This means we've previously made the
2681 location hardware one, but then the memory map changed,
2682 so we undo.
765dc015 2683
4a64f543
MS
2684 When breakpoints are removed, remove_breakpoints will use
2685 location types we've just set here, the only possible
2686 problem is that memory map has changed during running
2687 program, but it's not going to work anyway with current
2688 gdb. */
765dc015 2689 struct mem_region *mr
0d5ed153 2690 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2691
2692 if (mr)
2693 {
2694 if (automatic_hardware_breakpoints)
2695 {
765dc015
VP
2696 enum bp_loc_type new_type;
2697
2698 if (mr->attrib.mode != MEM_RW)
2699 new_type = bp_loc_hardware_breakpoint;
2700 else
2701 new_type = bp_loc_software_breakpoint;
2702
35df4500 2703 if (new_type != bl->loc_type)
765dc015
VP
2704 {
2705 static int said = 0;
cc59ec59 2706
35df4500 2707 bl->loc_type = new_type;
765dc015
VP
2708 if (!said)
2709 {
3e43a32a
MS
2710 fprintf_filtered (gdb_stdout,
2711 _("Note: automatically using "
2712 "hardware breakpoints for "
2713 "read-only addresses.\n"));
765dc015
VP
2714 said = 1;
2715 }
2716 }
2717 }
35df4500 2718 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2719 && mr->attrib.mode != MEM_RW)
2720 {
2721 fprintf_unfiltered (tmp_error_stream,
2722 _("Cannot insert breakpoint %d.\n"
2723 "Cannot set software breakpoint "
2724 "at read-only address %s\n"),
2725 bl->owner->number,
2726 paddress (bl->gdbarch, bl->address));
2727 return 1;
2728 }
765dc015
VP
2729 }
2730 }
2731
879bfdc2
DJ
2732 /* First check to see if we have to handle an overlay. */
2733 if (overlay_debugging == ovly_off
35df4500
TJB
2734 || bl->section == NULL
2735 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2736 {
2737 /* No overlay handling: just set the breakpoint. */
492d29ea 2738 TRY
dd61ec5c 2739 {
0000e5cc
PA
2740 int val;
2741
dd61ec5c 2742 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2743 if (val)
2744 bp_err = GENERIC_ERROR;
dd61ec5c 2745 }
492d29ea 2746 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2747 {
0000e5cc
PA
2748 bp_err = e.error;
2749 bp_err_message = e.message;
dd61ec5c 2750 }
492d29ea 2751 END_CATCH
879bfdc2
DJ
2752 }
2753 else
2754 {
4a64f543 2755 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2756 Shall we set a breakpoint at the LMA? */
2757 if (!overlay_events_enabled)
2758 {
2759 /* Yes -- overlay event support is not active,
2760 so we must try to set a breakpoint at the LMA.
2761 This will not work for a hardware breakpoint. */
35df4500 2762 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2763 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2764 bl->owner->number);
879bfdc2
DJ
2765 else
2766 {
35df4500
TJB
2767 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2768 bl->section);
879bfdc2 2769 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2770 bl->overlay_target_info = bl->target_info;
0d5ed153 2771 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2772
2773 /* No overlay handling: just set the breakpoint. */
492d29ea 2774 TRY
0000e5cc
PA
2775 {
2776 int val;
2777
579c6ad9 2778 bl->overlay_target_info.kind
cd6c3b4f
YQ
2779 = breakpoint_kind (bl, &addr);
2780 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2781 val = target_insert_breakpoint (bl->gdbarch,
2782 &bl->overlay_target_info);
2783 if (val)
2784 bp_err = GENERIC_ERROR;
2785 }
492d29ea 2786 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2787 {
2788 bp_err = e.error;
2789 bp_err_message = e.message;
2790 }
492d29ea 2791 END_CATCH
0000e5cc
PA
2792
2793 if (bp_err != GDB_NO_ERROR)
99361f52 2794 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2795 "Overlay breakpoint %d "
2796 "failed: in ROM?\n",
35df4500 2797 bl->owner->number);
879bfdc2
DJ
2798 }
2799 }
2800 /* Shall we set a breakpoint at the VMA? */
35df4500 2801 if (section_is_mapped (bl->section))
879bfdc2
DJ
2802 {
2803 /* Yes. This overlay section is mapped into memory. */
492d29ea 2804 TRY
dd61ec5c 2805 {
0000e5cc
PA
2806 int val;
2807
dd61ec5c 2808 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2809 if (val)
2810 bp_err = GENERIC_ERROR;
dd61ec5c 2811 }
492d29ea 2812 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2813 {
0000e5cc
PA
2814 bp_err = e.error;
2815 bp_err_message = e.message;
dd61ec5c 2816 }
492d29ea 2817 END_CATCH
879bfdc2
DJ
2818 }
2819 else
2820 {
2821 /* No. This breakpoint will not be inserted.
2822 No error, but do not mark the bp as 'inserted'. */
2823 return 0;
2824 }
2825 }
2826
0000e5cc 2827 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2828 {
2829 /* Can't set the breakpoint. */
0000e5cc
PA
2830
2831 /* In some cases, we might not be able to insert a
2832 breakpoint in a shared library that has already been
2833 removed, but we have not yet processed the shlib unload
2834 event. Unfortunately, some targets that implement
076855f9
PA
2835 breakpoint insertion themselves can't tell why the
2836 breakpoint insertion failed (e.g., the remote target
2837 doesn't define error codes), so we must treat generic
2838 errors as memory errors. */
0000e5cc 2839 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2840 && bl->loc_type == bp_loc_software_breakpoint
08351840 2841 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2842 || shared_objfile_contains_address_p (bl->pspace,
2843 bl->address)))
879bfdc2 2844 {
4a64f543 2845 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2846 bl->shlib_disabled = 1;
8d3788bd 2847 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2848 if (!*disabled_breaks)
2849 {
2850 fprintf_unfiltered (tmp_error_stream,
2851 "Cannot insert breakpoint %d.\n",
2852 bl->owner->number);
2853 fprintf_unfiltered (tmp_error_stream,
2854 "Temporarily disabling shared "
2855 "library breakpoints:\n");
2856 }
2857 *disabled_breaks = 1;
879bfdc2 2858 fprintf_unfiltered (tmp_error_stream,
35df4500 2859 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2860 return 0;
879bfdc2
DJ
2861 }
2862 else
879bfdc2 2863 {
35df4500 2864 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2865 {
0000e5cc
PA
2866 *hw_breakpoint_error = 1;
2867 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2868 fprintf_unfiltered (tmp_error_stream,
2869 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2870 bl->owner->number, bp_err_message ? ":" : ".\n");
2871 if (bp_err_message != NULL)
2872 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2873 }
2874 else
2875 {
0000e5cc
PA
2876 if (bp_err_message == NULL)
2877 {
2878 char *message
2879 = memory_error_message (TARGET_XFER_E_IO,
2880 bl->gdbarch, bl->address);
2881 struct cleanup *old_chain = make_cleanup (xfree, message);
2882
2883 fprintf_unfiltered (tmp_error_stream,
2884 "Cannot insert breakpoint %d.\n"
2885 "%s\n",
2886 bl->owner->number, message);
2887 do_cleanups (old_chain);
2888 }
2889 else
2890 {
2891 fprintf_unfiltered (tmp_error_stream,
2892 "Cannot insert breakpoint %d: %s\n",
2893 bl->owner->number,
2894 bp_err_message);
2895 }
879bfdc2 2896 }
0000e5cc 2897 return 1;
879bfdc2
DJ
2898
2899 }
2900 }
2901 else
35df4500 2902 bl->inserted = 1;
879bfdc2 2903
0000e5cc 2904 return 0;
879bfdc2
DJ
2905 }
2906
35df4500 2907 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2908 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2909 watchpoints. It's not clear that it's necessary... */
35df4500 2910 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2911 {
0000e5cc
PA
2912 int val;
2913
77b06cd7
TJB
2914 gdb_assert (bl->owner->ops != NULL
2915 && bl->owner->ops->insert_location != NULL);
2916
2917 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2918
2919 /* If trying to set a read-watchpoint, and it turns out it's not
2920 supported, try emulating one with an access watchpoint. */
35df4500 2921 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2922 {
2923 struct bp_location *loc, **loc_temp;
2924
2925 /* But don't try to insert it, if there's already another
2926 hw_access location that would be considered a duplicate
2927 of this one. */
2928 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2929 if (loc != bl
85d721b8 2930 && loc->watchpoint_type == hw_access
35df4500 2931 && watchpoint_locations_match (bl, loc))
85d721b8 2932 {
35df4500
TJB
2933 bl->duplicate = 1;
2934 bl->inserted = 1;
2935 bl->target_info = loc->target_info;
2936 bl->watchpoint_type = hw_access;
85d721b8
PA
2937 val = 0;
2938 break;
2939 }
2940
2941 if (val == 1)
2942 {
77b06cd7
TJB
2943 bl->watchpoint_type = hw_access;
2944 val = bl->owner->ops->insert_location (bl);
2945
2946 if (val)
2947 /* Back to the original value. */
2948 bl->watchpoint_type = hw_read;
85d721b8
PA
2949 }
2950 }
2951
35df4500 2952 bl->inserted = (val == 0);
879bfdc2
DJ
2953 }
2954
35df4500 2955 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2956 {
0000e5cc
PA
2957 int val;
2958
77b06cd7
TJB
2959 gdb_assert (bl->owner->ops != NULL
2960 && bl->owner->ops->insert_location != NULL);
2961
2962 val = bl->owner->ops->insert_location (bl);
2963 if (val)
2964 {
2965 bl->owner->enable_state = bp_disabled;
2966
2967 if (val == 1)
2968 warning (_("\
2969Error inserting catchpoint %d: Your system does not support this type\n\
2970of catchpoint."), bl->owner->number);
2971 else
2972 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2973 }
2974
2975 bl->inserted = (val == 0);
1640b821
DJ
2976
2977 /* We've already printed an error message if there was a problem
2978 inserting this catchpoint, and we've disabled the catchpoint,
2979 so just return success. */
2980 return 0;
879bfdc2
DJ
2981 }
2982
2983 return 0;
2984}
2985
6c95b8df
PA
2986/* This function is called when program space PSPACE is about to be
2987 deleted. It takes care of updating breakpoints to not reference
2988 PSPACE anymore. */
2989
2990void
2991breakpoint_program_space_exit (struct program_space *pspace)
2992{
2993 struct breakpoint *b, *b_temp;
876fa593 2994 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2995
2996 /* Remove any breakpoint that was set through this program space. */
2997 ALL_BREAKPOINTS_SAFE (b, b_temp)
2998 {
2999 if (b->pspace == pspace)
3000 delete_breakpoint (b);
3001 }
3002
3003 /* Breakpoints set through other program spaces could have locations
3004 bound to PSPACE as well. Remove those. */
876fa593 3005 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
3006 {
3007 struct bp_location *tmp;
3008
3009 if (loc->pspace == pspace)
3010 {
2bdf28a0 3011 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3012 if (loc->owner->loc == loc)
3013 loc->owner->loc = loc->next;
3014 else
3015 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3016 if (tmp->next == loc)
3017 {
3018 tmp->next = loc->next;
3019 break;
3020 }
3021 }
3022 }
3023
3024 /* Now update the global location list to permanently delete the
3025 removed locations above. */
44702360 3026 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3027}
3028
74960c60
VP
3029/* Make sure all breakpoints are inserted in inferior.
3030 Throws exception on any error.
3031 A breakpoint that is already inserted won't be inserted
3032 again, so calling this function twice is safe. */
3033void
3034insert_breakpoints (void)
3035{
3036 struct breakpoint *bpt;
3037
3038 ALL_BREAKPOINTS (bpt)
3039 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3040 {
3041 struct watchpoint *w = (struct watchpoint *) bpt;
3042
3043 update_watchpoint (w, 0 /* don't reparse. */);
3044 }
74960c60 3045
04086b45
PA
3046 /* Updating watchpoints creates new locations, so update the global
3047 location list. Explicitly tell ugll to insert locations and
3048 ignore breakpoints_always_inserted_mode. */
3049 update_global_location_list (UGLL_INSERT);
74960c60
VP
3050}
3051
20388dd6
YQ
3052/* Invoke CALLBACK for each of bp_location. */
3053
3054void
3055iterate_over_bp_locations (walk_bp_location_callback callback)
3056{
3057 struct bp_location *loc, **loc_tmp;
3058
3059 ALL_BP_LOCATIONS (loc, loc_tmp)
3060 {
3061 callback (loc, NULL);
3062 }
3063}
3064
b775012e
LM
3065/* This is used when we need to synch breakpoint conditions between GDB and the
3066 target. It is the case with deleting and disabling of breakpoints when using
3067 always-inserted mode. */
3068
3069static void
3070update_inserted_breakpoint_locations (void)
3071{
3072 struct bp_location *bl, **blp_tmp;
3073 int error_flag = 0;
3074 int val = 0;
3075 int disabled_breaks = 0;
3076 int hw_breakpoint_error = 0;
dd61ec5c 3077 int hw_bp_details_reported = 0;
b775012e 3078
d7e74731 3079 string_file tmp_error_stream;
b775012e
LM
3080
3081 /* Explicitly mark the warning -- this will only be printed if
3082 there was an error. */
d7e74731 3083 tmp_error_stream.puts ("Warning:\n");
b775012e 3084
5ed8105e 3085 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
3086
3087 ALL_BP_LOCATIONS (bl, blp_tmp)
3088 {
3089 /* We only want to update software breakpoints and hardware
3090 breakpoints. */
3091 if (!is_breakpoint (bl->owner))
3092 continue;
3093
3094 /* We only want to update locations that are already inserted
3095 and need updating. This is to avoid unwanted insertion during
3096 deletion of breakpoints. */
3097 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3098 continue;
3099
3100 switch_to_program_space_and_thread (bl->pspace);
3101
3102 /* For targets that support global breakpoints, there's no need
3103 to select an inferior to insert breakpoint to. In fact, even
3104 if we aren't attached to any process yet, we should still
3105 insert breakpoints. */
f5656ead 3106 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3107 && ptid_equal (inferior_ptid, null_ptid))
3108 continue;
3109
d7e74731 3110 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3111 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3112 if (val)
3113 error_flag = val;
3114 }
3115
3116 if (error_flag)
3117 {
3118 target_terminal_ours_for_output ();
3119 error_stream (tmp_error_stream);
3120 }
b775012e
LM
3121}
3122
c30eee59 3123/* Used when starting or continuing the program. */
c906108c 3124
74960c60
VP
3125static void
3126insert_breakpoint_locations (void)
c906108c 3127{
a5606eee 3128 struct breakpoint *bpt;
35df4500 3129 struct bp_location *bl, **blp_tmp;
eacd795a 3130 int error_flag = 0;
c906108c 3131 int val = 0;
3fbb6ffa 3132 int disabled_breaks = 0;
81d0cc19 3133 int hw_breakpoint_error = 0;
dd61ec5c 3134 int hw_bp_error_explained_already = 0;
c906108c 3135
d7e74731
PA
3136 string_file tmp_error_stream;
3137
81d0cc19
GS
3138 /* Explicitly mark the warning -- this will only be printed if
3139 there was an error. */
d7e74731 3140 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3141
5ed8105e 3142 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3143
35df4500 3144 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3145 {
b775012e 3146 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3147 continue;
3148
4a64f543
MS
3149 /* There is no point inserting thread-specific breakpoints if
3150 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3151 has BL->OWNER always non-NULL. */
35df4500 3152 if (bl->owner->thread != -1
5d5658a1 3153 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3154 continue;
3155
35df4500 3156 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3157
3158 /* For targets that support global breakpoints, there's no need
3159 to select an inferior to insert breakpoint to. In fact, even
3160 if we aren't attached to any process yet, we should still
3161 insert breakpoints. */
f5656ead 3162 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3163 && ptid_equal (inferior_ptid, null_ptid))
3164 continue;
3165
d7e74731 3166 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3167 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3168 if (val)
eacd795a 3169 error_flag = val;
879bfdc2 3170 }
c906108c 3171
4a64f543
MS
3172 /* If we failed to insert all locations of a watchpoint, remove
3173 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3174 ALL_BREAKPOINTS (bpt)
3175 {
3176 int some_failed = 0;
3177 struct bp_location *loc;
3178
3179 if (!is_hardware_watchpoint (bpt))
3180 continue;
3181
d6b74ac4 3182 if (!breakpoint_enabled (bpt))
a5606eee 3183 continue;
74960c60
VP
3184
3185 if (bpt->disposition == disp_del_at_next_stop)
3186 continue;
a5606eee
VP
3187
3188 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3189 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3190 {
3191 some_failed = 1;
3192 break;
3193 }
3194 if (some_failed)
3195 {
3196 for (loc = bpt->loc; loc; loc = loc->next)
3197 if (loc->inserted)
834c0d03 3198 remove_breakpoint (loc);
a5606eee
VP
3199
3200 hw_breakpoint_error = 1;
d7e74731
PA
3201 tmp_error_stream.printf ("Could not insert "
3202 "hardware watchpoint %d.\n",
3203 bpt->number);
eacd795a 3204 error_flag = -1;
a5606eee
VP
3205 }
3206 }
3207
eacd795a 3208 if (error_flag)
81d0cc19
GS
3209 {
3210 /* If a hardware breakpoint or watchpoint was inserted, add a
3211 message about possibly exhausted resources. */
dd61ec5c 3212 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3213 {
d7e74731 3214 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3215You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3216 }
81d0cc19
GS
3217 target_terminal_ours_for_output ();
3218 error_stream (tmp_error_stream);
3219 }
c906108c
SS
3220}
3221
c30eee59
TJB
3222/* Used when the program stops.
3223 Returns zero if successful, or non-zero if there was a problem
3224 removing a breakpoint location. */
3225
c906108c 3226int
fba45db2 3227remove_breakpoints (void)
c906108c 3228{
35df4500 3229 struct bp_location *bl, **blp_tmp;
3a1bae8e 3230 int val = 0;
c906108c 3231
35df4500 3232 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3233 {
1e4d1764 3234 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3235 val |= remove_breakpoint (bl);
c5aa993b 3236 }
3a1bae8e 3237 return val;
c906108c
SS
3238}
3239
49fa26b0
PA
3240/* When a thread exits, remove breakpoints that are related to
3241 that thread. */
3242
3243static void
3244remove_threaded_breakpoints (struct thread_info *tp, int silent)
3245{
3246 struct breakpoint *b, *b_tmp;
3247
3248 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3249 {
5d5658a1 3250 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3251 {
3252 b->disposition = disp_del_at_next_stop;
3253
3254 printf_filtered (_("\
43792cf0
PA
3255Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3256 b->number, print_thread_id (tp));
49fa26b0
PA
3257
3258 /* Hide it from the user. */
3259 b->number = 0;
3260 }
3261 }
3262}
3263
6c95b8df
PA
3264/* Remove breakpoints of process PID. */
3265
3266int
3267remove_breakpoints_pid (int pid)
3268{
35df4500 3269 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3270 int val;
3271 struct inferior *inf = find_inferior_pid (pid);
3272
35df4500 3273 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3274 {
35df4500 3275 if (bl->pspace != inf->pspace)
6c95b8df
PA
3276 continue;
3277
fc126975 3278 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3279 {
834c0d03 3280 val = remove_breakpoint (bl);
6c95b8df
PA
3281 if (val != 0)
3282 return val;
3283 }
3284 }
3285 return 0;
3286}
3287
c906108c 3288int
fba45db2 3289reattach_breakpoints (int pid)
c906108c 3290{
6c95b8df 3291 struct cleanup *old_chain;
35df4500 3292 struct bp_location *bl, **blp_tmp;
c906108c 3293 int val;
dd61ec5c 3294 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3295 struct inferior *inf;
3296 struct thread_info *tp;
3297
3298 tp = any_live_thread_of_process (pid);
3299 if (tp == NULL)
3300 return 1;
3301
3302 inf = find_inferior_pid (pid);
3303 old_chain = save_inferior_ptid ();
3304
3305 inferior_ptid = tp->ptid;
a4954f26 3306
d7e74731 3307 string_file tmp_error_stream;
c906108c 3308
35df4500 3309 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3310 {
35df4500 3311 if (bl->pspace != inf->pspace)
6c95b8df
PA
3312 continue;
3313
35df4500 3314 if (bl->inserted)
c5aa993b 3315 {
35df4500 3316 bl->inserted = 0;
d7e74731 3317 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3318 if (val != 0)
3319 {
ce696e05 3320 do_cleanups (old_chain);
c5aa993b
JM
3321 return val;
3322 }
3323 }
3324 }
ce696e05 3325 do_cleanups (old_chain);
c906108c
SS
3326 return 0;
3327}
3328
e58b0e63
PA
3329static int internal_breakpoint_number = -1;
3330
84f4c1fe
PM
3331/* Set the breakpoint number of B, depending on the value of INTERNAL.
3332 If INTERNAL is non-zero, the breakpoint number will be populated
3333 from internal_breakpoint_number and that variable decremented.
e5dd4106 3334 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3335 breakpoint_count and that value incremented. Internal breakpoints
3336 do not set the internal var bpnum. */
3337static void
3338set_breakpoint_number (int internal, struct breakpoint *b)
3339{
3340 if (internal)
3341 b->number = internal_breakpoint_number--;
3342 else
3343 {
3344 set_breakpoint_count (breakpoint_count + 1);
3345 b->number = breakpoint_count;
3346 }
3347}
3348
e62c965a 3349static struct breakpoint *
a6d9a66e 3350create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3351 CORE_ADDR address, enum bptype type,
c0a91b2b 3352 const struct breakpoint_ops *ops)
e62c965a 3353{
e62c965a
PP
3354 struct symtab_and_line sal;
3355 struct breakpoint *b;
3356
4a64f543 3357 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3358
3359 sal.pc = address;
3360 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3361 sal.pspace = current_program_space;
e62c965a 3362
06edf0c0 3363 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3364 b->number = internal_breakpoint_number--;
3365 b->disposition = disp_donttouch;
3366
3367 return b;
3368}
3369
17450429
PP
3370static const char *const longjmp_names[] =
3371 {
3372 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3373 };
3374#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3375
3376/* Per-objfile data private to breakpoint.c. */
3377struct breakpoint_objfile_data
3378{
3379 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3380 struct bound_minimal_symbol overlay_msym;
17450429
PP
3381
3382 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3383 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3384
28106bc2
SDJ
3385 /* True if we have looked for longjmp probes. */
3386 int longjmp_searched;
3387
3388 /* SystemTap probe points for longjmp (if any). */
3389 VEC (probe_p) *longjmp_probes;
3390
17450429 3391 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3392 struct bound_minimal_symbol terminate_msym;
17450429
PP
3393
3394 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3395 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3396
3397 /* True if we have looked for exception probes. */
3398 int exception_searched;
3399
3400 /* SystemTap probe points for unwinding (if any). */
3401 VEC (probe_p) *exception_probes;
17450429
PP
3402};
3403
3404static const struct objfile_data *breakpoint_objfile_key;
3405
3406/* Minimal symbol not found sentinel. */
3407static struct minimal_symbol msym_not_found;
3408
3409/* Returns TRUE if MSYM point to the "not found" sentinel. */
3410
3411static int
3412msym_not_found_p (const struct minimal_symbol *msym)
3413{
3414 return msym == &msym_not_found;
3415}
3416
3417/* Return per-objfile data needed by breakpoint.c.
3418 Allocate the data if necessary. */
3419
3420static struct breakpoint_objfile_data *
3421get_breakpoint_objfile_data (struct objfile *objfile)
3422{
3423 struct breakpoint_objfile_data *bp_objfile_data;
3424
9a3c8263
SM
3425 bp_objfile_data = ((struct breakpoint_objfile_data *)
3426 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3427 if (bp_objfile_data == NULL)
3428 {
8d749320
SM
3429 bp_objfile_data =
3430 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3431
3432 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3433 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3434 }
3435 return bp_objfile_data;
3436}
3437
28106bc2
SDJ
3438static void
3439free_breakpoint_probes (struct objfile *obj, void *data)
3440{
9a3c8263
SM
3441 struct breakpoint_objfile_data *bp_objfile_data
3442 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3443
3444 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3445 VEC_free (probe_p, bp_objfile_data->exception_probes);
3446}
3447
e62c965a 3448static void
af02033e 3449create_overlay_event_breakpoint (void)
e62c965a 3450{
69de3c6a 3451 struct objfile *objfile;
af02033e 3452 const char *const func_name = "_ovly_debug_event";
e62c965a 3453
69de3c6a
PP
3454 ALL_OBJFILES (objfile)
3455 {
3456 struct breakpoint *b;
17450429
PP
3457 struct breakpoint_objfile_data *bp_objfile_data;
3458 CORE_ADDR addr;
67994074 3459 struct explicit_location explicit_loc;
69de3c6a 3460
17450429
PP
3461 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3462
3b7344d5 3463 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3464 continue;
3465
3b7344d5 3466 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3467 {
3b7344d5 3468 struct bound_minimal_symbol m;
17450429
PP
3469
3470 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3471 if (m.minsym == NULL)
17450429
PP
3472 {
3473 /* Avoid future lookups in this objfile. */
3b7344d5 3474 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3475 continue;
3476 }
3477 bp_objfile_data->overlay_msym = m;
3478 }
e62c965a 3479
77e371c0 3480 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3481 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3482 bp_overlay_event,
3483 &internal_breakpoint_ops);
67994074
KS
3484 initialize_explicit_location (&explicit_loc);
3485 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3486 b->location = new_explicit_location (&explicit_loc);
e62c965a 3487
69de3c6a
PP
3488 if (overlay_debugging == ovly_auto)
3489 {
3490 b->enable_state = bp_enabled;
3491 overlay_events_enabled = 1;
3492 }
3493 else
3494 {
3495 b->enable_state = bp_disabled;
3496 overlay_events_enabled = 0;
3497 }
e62c965a 3498 }
e62c965a
PP
3499}
3500
0fd8e87f 3501static void
af02033e 3502create_longjmp_master_breakpoint (void)
0fd8e87f 3503{
6c95b8df 3504 struct program_space *pspace;
6c95b8df 3505
5ed8105e 3506 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3507
6c95b8df 3508 ALL_PSPACES (pspace)
af02033e
PP
3509 {
3510 struct objfile *objfile;
3511
3512 set_current_program_space (pspace);
3513
3514 ALL_OBJFILES (objfile)
0fd8e87f 3515 {
af02033e
PP
3516 int i;
3517 struct gdbarch *gdbarch;
17450429 3518 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3519
af02033e 3520 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3521
17450429
PP
3522 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3523
28106bc2
SDJ
3524 if (!bp_objfile_data->longjmp_searched)
3525 {
25f9533e
SDJ
3526 VEC (probe_p) *ret;
3527
3528 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3529 if (ret != NULL)
3530 {
3531 /* We are only interested in checking one element. */
3532 struct probe *p = VEC_index (probe_p, ret, 0);
3533
3534 if (!can_evaluate_probe_arguments (p))
3535 {
3536 /* We cannot use the probe interface here, because it does
3537 not know how to evaluate arguments. */
3538 VEC_free (probe_p, ret);
3539 ret = NULL;
3540 }
3541 }
3542 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3543 bp_objfile_data->longjmp_searched = 1;
3544 }
3545
3546 if (bp_objfile_data->longjmp_probes != NULL)
3547 {
3548 int i;
3549 struct probe *probe;
3550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3551
3552 for (i = 0;
3553 VEC_iterate (probe_p,
3554 bp_objfile_data->longjmp_probes,
3555 i, probe);
3556 ++i)
3557 {
3558 struct breakpoint *b;
3559
729662a5
TT
3560 b = create_internal_breakpoint (gdbarch,
3561 get_probe_address (probe,
3562 objfile),
28106bc2
SDJ
3563 bp_longjmp_master,
3564 &internal_breakpoint_ops);
d28cd78a 3565 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3566 b->enable_state = bp_disabled;
3567 }
3568
3569 continue;
3570 }
3571
0569175e
TSD
3572 if (!gdbarch_get_longjmp_target_p (gdbarch))
3573 continue;
3574
17450429 3575 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3576 {
3577 struct breakpoint *b;
af02033e 3578 const char *func_name;
17450429 3579 CORE_ADDR addr;
67994074 3580 struct explicit_location explicit_loc;
6c95b8df 3581
3b7344d5 3582 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3583 continue;
0fd8e87f 3584
17450429 3585 func_name = longjmp_names[i];
3b7344d5 3586 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3587 {
3b7344d5 3588 struct bound_minimal_symbol m;
17450429
PP
3589
3590 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3591 if (m.minsym == NULL)
17450429
PP
3592 {
3593 /* Prevent future lookups in this objfile. */
3b7344d5 3594 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3595 continue;
3596 }
3597 bp_objfile_data->longjmp_msym[i] = m;
3598 }
3599
77e371c0 3600 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3601 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3602 &internal_breakpoint_ops);
67994074
KS
3603 initialize_explicit_location (&explicit_loc);
3604 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3605 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3606 b->enable_state = bp_disabled;
3607 }
0fd8e87f 3608 }
af02033e 3609 }
0fd8e87f
UW
3610}
3611
af02033e 3612/* Create a master std::terminate breakpoint. */
aa7d318d 3613static void
af02033e 3614create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3615{
3616 struct program_space *pspace;
af02033e 3617 const char *const func_name = "std::terminate()";
aa7d318d 3618
5ed8105e 3619 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3620
3621 ALL_PSPACES (pspace)
17450429
PP
3622 {
3623 struct objfile *objfile;
3624 CORE_ADDR addr;
3625
3626 set_current_program_space (pspace);
3627
aa7d318d
TT
3628 ALL_OBJFILES (objfile)
3629 {
3630 struct breakpoint *b;
17450429 3631 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3632 struct explicit_location explicit_loc;
aa7d318d 3633
17450429 3634 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3635
3b7344d5 3636 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3637 continue;
3638
3b7344d5 3639 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3640 {
3b7344d5 3641 struct bound_minimal_symbol m;
17450429
PP
3642
3643 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3644 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3645 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3646 {
3647 /* Prevent future lookups in this objfile. */
3b7344d5 3648 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3649 continue;
3650 }
3651 bp_objfile_data->terminate_msym = m;
3652 }
aa7d318d 3653
77e371c0 3654 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3655 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3656 bp_std_terminate_master,
3657 &internal_breakpoint_ops);
67994074
KS
3658 initialize_explicit_location (&explicit_loc);
3659 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3660 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3661 b->enable_state = bp_disabled;
3662 }
17450429 3663 }
aa7d318d
TT
3664}
3665
186c406b
TT
3666/* Install a master breakpoint on the unwinder's debug hook. */
3667
70221824 3668static void
186c406b
TT
3669create_exception_master_breakpoint (void)
3670{
3671 struct objfile *objfile;
17450429 3672 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3673
3674 ALL_OBJFILES (objfile)
3675 {
17450429
PP
3676 struct breakpoint *b;
3677 struct gdbarch *gdbarch;
3678 struct breakpoint_objfile_data *bp_objfile_data;
3679 CORE_ADDR addr;
67994074 3680 struct explicit_location explicit_loc;
17450429
PP
3681
3682 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3683
28106bc2
SDJ
3684 /* We prefer the SystemTap probe point if it exists. */
3685 if (!bp_objfile_data->exception_searched)
3686 {
25f9533e
SDJ
3687 VEC (probe_p) *ret;
3688
3689 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3690
3691 if (ret != NULL)
3692 {
3693 /* We are only interested in checking one element. */
3694 struct probe *p = VEC_index (probe_p, ret, 0);
3695
3696 if (!can_evaluate_probe_arguments (p))
3697 {
3698 /* We cannot use the probe interface here, because it does
3699 not know how to evaluate arguments. */
3700 VEC_free (probe_p, ret);
3701 ret = NULL;
3702 }
3703 }
3704 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3705 bp_objfile_data->exception_searched = 1;
3706 }
3707
3708 if (bp_objfile_data->exception_probes != NULL)
3709 {
3710 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3711 int i;
3712 struct probe *probe;
3713
3714 for (i = 0;
3715 VEC_iterate (probe_p,
3716 bp_objfile_data->exception_probes,
3717 i, probe);
3718 ++i)
3719 {
3720 struct breakpoint *b;
3721
729662a5
TT
3722 b = create_internal_breakpoint (gdbarch,
3723 get_probe_address (probe,
3724 objfile),
28106bc2
SDJ
3725 bp_exception_master,
3726 &internal_breakpoint_ops);
d28cd78a 3727 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3728 b->enable_state = bp_disabled;
3729 }
3730
3731 continue;
3732 }
3733
3734 /* Otherwise, try the hook function. */
3735
3b7344d5 3736 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3737 continue;
3738
3739 gdbarch = get_objfile_arch (objfile);
186c406b 3740
3b7344d5 3741 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3742 {
3b7344d5 3743 struct bound_minimal_symbol debug_hook;
186c406b 3744
17450429 3745 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3746 if (debug_hook.minsym == NULL)
17450429 3747 {
3b7344d5 3748 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3749 continue;
3750 }
3751
3752 bp_objfile_data->exception_msym = debug_hook;
186c406b 3753 }
17450429 3754
77e371c0 3755 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3756 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3757 &current_target);
06edf0c0
PA
3758 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3759 &internal_breakpoint_ops);
67994074
KS
3760 initialize_explicit_location (&explicit_loc);
3761 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3762 b->location = new_explicit_location (&explicit_loc);
17450429 3763 b->enable_state = bp_disabled;
186c406b 3764 }
186c406b
TT
3765}
3766
9ef9e6a6
KS
3767/* Does B have a location spec? */
3768
3769static int
3770breakpoint_event_location_empty_p (const struct breakpoint *b)
3771{
d28cd78a 3772 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3773}
3774
c906108c 3775void
fba45db2 3776update_breakpoints_after_exec (void)
c906108c 3777{
35df4500 3778 struct breakpoint *b, *b_tmp;
876fa593 3779 struct bp_location *bploc, **bplocp_tmp;
c906108c 3780
25b22b0a
PA
3781 /* We're about to delete breakpoints from GDB's lists. If the
3782 INSERTED flag is true, GDB will try to lift the breakpoints by
3783 writing the breakpoints' "shadow contents" back into memory. The
3784 "shadow contents" are NOT valid after an exec, so GDB should not
3785 do that. Instead, the target is responsible from marking
3786 breakpoints out as soon as it detects an exec. We don't do that
3787 here instead, because there may be other attempts to delete
3788 breakpoints after detecting an exec and before reaching here. */
876fa593 3789 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3790 if (bploc->pspace == current_program_space)
3791 gdb_assert (!bploc->inserted);
c906108c 3792
35df4500 3793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3794 {
6c95b8df
PA
3795 if (b->pspace != current_program_space)
3796 continue;
3797
4a64f543 3798 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3799 if (b->type == bp_shlib_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
c906108c 3804
4a64f543 3805 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3806 if (b->type == bp_jit_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
3811
1900040c 3812 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3813 as must overlay event and longjmp master breakpoints. */
3814 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3815 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3816 || b->type == bp_exception_master)
c4093a6a
JM
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
4a64f543 3822 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3823 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
7c16b83e
PA
3829 /* Just like single-step breakpoints. */
3830 if (b->type == bp_single_step)
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
611c83ae
PA
3836 /* Longjmp and longjmp-resume breakpoints are also meaningless
3837 after an exec. */
186c406b 3838 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3839 || b->type == bp_longjmp_call_dummy
186c406b 3840 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3841 {
3842 delete_breakpoint (b);
3843 continue;
3844 }
3845
ce78b96d
JB
3846 if (b->type == bp_catchpoint)
3847 {
3848 /* For now, none of the bp_catchpoint breakpoints need to
3849 do anything at this point. In the future, if some of
3850 the catchpoints need to something, we will need to add
3851 a new method, and call this method from here. */
3852 continue;
3853 }
3854
c5aa993b
JM
3855 /* bp_finish is a special case. The only way we ought to be able
3856 to see one of these when an exec() has happened, is if the user
3857 caught a vfork, and then said "finish". Ordinarily a finish just
3858 carries them to the call-site of the current callee, by setting
3859 a temporary bp there and resuming. But in this case, the finish
3860 will carry them entirely through the vfork & exec.
3861
3862 We don't want to allow a bp_finish to remain inserted now. But
3863 we can't safely delete it, 'cause finish_command has a handle to
3864 the bp on a bpstat, and will later want to delete it. There's a
3865 chance (and I've seen it happen) that if we delete the bp_finish
3866 here, that its storage will get reused by the time finish_command
3867 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3868 We really must allow finish_command to delete a bp_finish.
3869
e5dd4106 3870 In the absence of a general solution for the "how do we know
53a5351d
JM
3871 it's safe to delete something others may have handles to?"
3872 problem, what we'll do here is just uninsert the bp_finish, and
3873 let finish_command delete it.
3874
3875 (We know the bp_finish is "doomed" in the sense that it's
3876 momentary, and will be deleted as soon as finish_command sees
3877 the inferior stopped. So it doesn't matter that the bp's
3878 address is probably bogus in the new a.out, unlike e.g., the
3879 solib breakpoints.) */
c5aa993b 3880
c5aa993b
JM
3881 if (b->type == bp_finish)
3882 {
3883 continue;
3884 }
3885
3886 /* Without a symbolic address, we have little hope of the
3887 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3888 a.out. */
9ef9e6a6 3889 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3890 {
3891 delete_breakpoint (b);
3892 continue;
3893 }
c5aa993b 3894 }
c906108c
SS
3895}
3896
3897int
d80ee84f 3898detach_breakpoints (ptid_t ptid)
c906108c 3899{
35df4500 3900 struct bp_location *bl, **blp_tmp;
3a1bae8e 3901 int val = 0;
ce696e05 3902 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3903 struct inferior *inf = current_inferior ();
c5aa993b 3904
dfd4cc63 3905 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3906 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3907
6c95b8df 3908 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3909 inferior_ptid = ptid;
35df4500 3910 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3911 {
35df4500 3912 if (bl->pspace != inf->pspace)
6c95b8df
PA
3913 continue;
3914
bd9673a4
PW
3915 /* This function must physically remove breakpoints locations
3916 from the specified ptid, without modifying the breakpoint
3917 package's state. Locations of type bp_loc_other are only
3918 maintained at GDB side. So, there is no need to remove
3919 these bp_loc_other locations. Moreover, removing these
3920 would modify the breakpoint package's state. */
3921 if (bl->loc_type == bp_loc_other)
3922 continue;
3923
35df4500 3924 if (bl->inserted)
b2b6a7da 3925 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3926 }
d03285ec 3927
ce696e05 3928 do_cleanups (old_chain);
3a1bae8e 3929 return val;
c906108c
SS
3930}
3931
35df4500 3932/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3933 Note that this is used to detach breakpoints from a child fork.
3934 When we get here, the child isn't in the inferior list, and neither
3935 do we have objects to represent its address space --- we should
35df4500 3936 *not* look at bl->pspace->aspace here. */
6c95b8df 3937
c906108c 3938static int
b2b6a7da 3939remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3940{
3941 int val;
c5aa993b 3942
35df4500
TJB
3943 /* BL is never in moribund_locations by our callers. */
3944 gdb_assert (bl->owner != NULL);
2bdf28a0 3945
74960c60
VP
3946 /* The type of none suggests that owner is actually deleted.
3947 This should not ever happen. */
35df4500 3948 gdb_assert (bl->owner->type != bp_none);
0bde7532 3949
35df4500
TJB
3950 if (bl->loc_type == bp_loc_software_breakpoint
3951 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3952 {
c02f5703
MS
3953 /* "Normal" instruction breakpoint: either the standard
3954 trap-instruction bp (bp_breakpoint), or a
3955 bp_hardware_breakpoint. */
3956
3957 /* First check to see if we have to handle an overlay. */
3958 if (overlay_debugging == ovly_off
35df4500
TJB
3959 || bl->section == NULL
3960 || !(section_is_overlay (bl->section)))
c02f5703
MS
3961 {
3962 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3963
3964 /* If we're trying to uninsert a memory breakpoint that we
3965 know is set in a dynamic object that is marked
3966 shlib_disabled, then either the dynamic object was
3967 removed with "remove-symbol-file" or with
3968 "nosharedlibrary". In the former case, we don't know
3969 whether another dynamic object might have loaded over the
3970 breakpoint's address -- the user might well let us know
3971 about it next with add-symbol-file (the whole point of
d03de421 3972 add-symbol-file is letting the user manually maintain a
08351840
PA
3973 list of dynamically loaded objects). If we have the
3974 breakpoint's shadow memory, that is, this is a software
3975 breakpoint managed by GDB, check whether the breakpoint
3976 is still inserted in memory, to avoid overwriting wrong
3977 code with stale saved shadow contents. Note that HW
3978 breakpoints don't have shadow memory, as they're
3979 implemented using a mechanism that is not dependent on
3980 being able to modify the target's memory, and as such
3981 they should always be removed. */
3982 if (bl->shlib_disabled
3983 && bl->target_info.shadow_len != 0
3984 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3985 val = 0;
3986 else
73971819 3987 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3988 }
c906108c
SS
3989 else
3990 {
4a64f543 3991 /* This breakpoint is in an overlay section.
c02f5703
MS
3992 Did we set a breakpoint at the LMA? */
3993 if (!overlay_events_enabled)
3994 {
3995 /* Yes -- overlay event support is not active, so we
3996 should have set a breakpoint at the LMA. Remove it.
3997 */
c02f5703
MS
3998 /* Ignore any failures: if the LMA is in ROM, we will
3999 have already warned when we failed to insert it. */
35df4500
TJB
4000 if (bl->loc_type == bp_loc_hardware_breakpoint)
4001 target_remove_hw_breakpoint (bl->gdbarch,
4002 &bl->overlay_target_info);
c02f5703 4003 else
35df4500 4004 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4005 &bl->overlay_target_info,
4006 reason);
c02f5703
MS
4007 }
4008 /* Did we set a breakpoint at the VMA?
4009 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4010 if (bl->inserted)
c906108c 4011 {
c02f5703
MS
4012 /* Yes -- remove it. Previously we did not bother to
4013 remove the breakpoint if the section had been
4014 unmapped, but let's not rely on that being safe. We
4015 don't know what the overlay manager might do. */
aa67235e
UW
4016
4017 /* However, we should remove *software* breakpoints only
4018 if the section is still mapped, or else we overwrite
4019 wrong code with the saved shadow contents. */
348d480f
PA
4020 if (bl->loc_type == bp_loc_hardware_breakpoint
4021 || section_is_mapped (bl->section))
73971819 4022 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4023 else
4024 val = 0;
c906108c 4025 }
c02f5703
MS
4026 else
4027 {
4028 /* No -- not inserted, so no need to remove. No error. */
4029 val = 0;
4030 }
c906108c 4031 }
879d1e6b 4032
08351840
PA
4033 /* In some cases, we might not be able to remove a breakpoint in
4034 a shared library that has already been removed, but we have
4035 not yet processed the shlib unload event. Similarly for an
4036 unloaded add-symbol-file object - the user might not yet have
4037 had the chance to remove-symbol-file it. shlib_disabled will
4038 be set if the library/object has already been removed, but
4039 the breakpoint hasn't been uninserted yet, e.g., after
4040 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4041 always-inserted mode. */
076855f9 4042 if (val
08351840
PA
4043 && (bl->loc_type == bp_loc_software_breakpoint
4044 && (bl->shlib_disabled
4045 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4046 || shared_objfile_contains_address_p (bl->pspace,
4047 bl->address))))
879d1e6b
UW
4048 val = 0;
4049
c906108c
SS
4050 if (val)
4051 return val;
b2b6a7da 4052 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4053 }
35df4500 4054 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4055 {
77b06cd7
TJB
4056 gdb_assert (bl->owner->ops != NULL
4057 && bl->owner->ops->remove_location != NULL);
4058
b2b6a7da 4059 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4060 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4061
c906108c 4062 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4063 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4064 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4065 bl->owner->number);
c906108c 4066 }
35df4500
TJB
4067 else if (bl->owner->type == bp_catchpoint
4068 && breakpoint_enabled (bl->owner)
4069 && !bl->duplicate)
ce78b96d 4070 {
77b06cd7
TJB
4071 gdb_assert (bl->owner->ops != NULL
4072 && bl->owner->ops->remove_location != NULL);
ce78b96d 4073
73971819 4074 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4075 if (val)
4076 return val;
77b06cd7 4077
b2b6a7da 4078 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4079 }
c906108c
SS
4080
4081 return 0;
4082}
4083
6c95b8df 4084static int
834c0d03 4085remove_breakpoint (struct bp_location *bl)
6c95b8df 4086{
35df4500
TJB
4087 /* BL is never in moribund_locations by our callers. */
4088 gdb_assert (bl->owner != NULL);
2bdf28a0 4089
6c95b8df
PA
4090 /* The type of none suggests that owner is actually deleted.
4091 This should not ever happen. */
35df4500 4092 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4093
5ed8105e 4094 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4095
35df4500 4096 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4097
5ed8105e 4098 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4099}
4100
c906108c
SS
4101/* Clear the "inserted" flag in all breakpoints. */
4102
25b22b0a 4103void
fba45db2 4104mark_breakpoints_out (void)
c906108c 4105{
35df4500 4106 struct bp_location *bl, **blp_tmp;
c906108c 4107
35df4500 4108 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4109 if (bl->pspace == current_program_space)
35df4500 4110 bl->inserted = 0;
c906108c
SS
4111}
4112
53a5351d
JM
4113/* Clear the "inserted" flag in all breakpoints and delete any
4114 breakpoints which should go away between runs of the program.
c906108c
SS
4115
4116 Plus other such housekeeping that has to be done for breakpoints
4117 between runs.
4118
53a5351d
JM
4119 Note: this function gets called at the end of a run (by
4120 generic_mourn_inferior) and when a run begins (by
4a64f543 4121 init_wait_for_inferior). */
c906108c
SS
4122
4123
4124
4125void
fba45db2 4126breakpoint_init_inferior (enum inf_context context)
c906108c 4127{
35df4500 4128 struct breakpoint *b, *b_tmp;
870f88f7 4129 struct bp_location *bl;
1c5cfe86 4130 int ix;
6c95b8df 4131 struct program_space *pspace = current_program_space;
c906108c 4132
50c71eaf
PA
4133 /* If breakpoint locations are shared across processes, then there's
4134 nothing to do. */
f5656ead 4135 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4136 return;
4137
1a853c52 4138 mark_breakpoints_out ();
075f6582 4139
35df4500 4140 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4141 {
6c95b8df
PA
4142 if (b->loc && b->loc->pspace != pspace)
4143 continue;
4144
c5aa993b
JM
4145 switch (b->type)
4146 {
4147 case bp_call_dummy:
e2e4d78b 4148 case bp_longjmp_call_dummy:
c906108c 4149
c5aa993b 4150 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4151 cause problems when the inferior is rerun, so we better get
4152 rid of it. */
4153
4154 case bp_watchpoint_scope:
4155
4156 /* Also get rid of scope breakpoints. */
4157
4158 case bp_shlib_event:
4159
4160 /* Also remove solib event breakpoints. Their addresses may
4161 have changed since the last time we ran the program.
4162 Actually we may now be debugging against different target;
4163 and so the solib backend that installed this breakpoint may
4164 not be used in by the target. E.g.,
4165
4166 (gdb) file prog-linux
4167 (gdb) run # native linux target
4168 ...
4169 (gdb) kill
4170 (gdb) file prog-win.exe
4171 (gdb) tar rem :9999 # remote Windows gdbserver.
4172 */
c906108c 4173
f59f708a
PA
4174 case bp_step_resume:
4175
4176 /* Also remove step-resume breakpoints. */
4177
7c16b83e
PA
4178 case bp_single_step:
4179
4180 /* Also remove single-step breakpoints. */
4181
c5aa993b
JM
4182 delete_breakpoint (b);
4183 break;
c906108c 4184
c5aa993b
JM
4185 case bp_watchpoint:
4186 case bp_hardware_watchpoint:
4187 case bp_read_watchpoint:
4188 case bp_access_watchpoint:
3a5c3e22
PA
4189 {
4190 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4191
3a5c3e22
PA
4192 /* Likewise for watchpoints on local expressions. */
4193 if (w->exp_valid_block != NULL)
4194 delete_breakpoint (b);
63000888 4195 else
3a5c3e22 4196 {
63000888
PA
4197 /* Get rid of existing locations, which are no longer
4198 valid. New ones will be created in
4199 update_watchpoint, when the inferior is restarted.
4200 The next update_global_location_list call will
4201 garbage collect them. */
4202 b->loc = NULL;
4203
4204 if (context == inf_starting)
4205 {
4206 /* Reset val field to force reread of starting value in
4207 insert_breakpoints. */
4208 if (w->val)
4209 value_free (w->val);
4210 w->val = NULL;
4211 w->val_valid = 0;
4212 }
4213 }
3a5c3e22 4214 }
c5aa993b
JM
4215 break;
4216 default:
c5aa993b
JM
4217 break;
4218 }
4219 }
1c5cfe86
PA
4220
4221 /* Get rid of the moribund locations. */
35df4500
TJB
4222 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4223 decref_bp_location (&bl);
1c5cfe86 4224 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4225}
4226
6c95b8df
PA
4227/* These functions concern about actual breakpoints inserted in the
4228 target --- to e.g. check if we need to do decr_pc adjustment or if
4229 we need to hop over the bkpt --- so we check for address space
4230 match, not program space. */
4231
c2c6d25f
JM
4232/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4233 exists at PC. It returns ordinary_breakpoint_here if it's an
4234 ordinary breakpoint, or permanent_breakpoint_here if it's a
4235 permanent breakpoint.
4236 - When continuing from a location with an ordinary breakpoint, we
4237 actually single step once before calling insert_breakpoints.
e5dd4106 4238 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4239 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4240 the target, to advance the PC past the breakpoint. */
c906108c 4241
c2c6d25f 4242enum breakpoint_here
6c95b8df 4243breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4244{
35df4500 4245 struct bp_location *bl, **blp_tmp;
c2c6d25f 4246 int any_breakpoint_here = 0;
c906108c 4247
35df4500 4248 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4249 {
35df4500
TJB
4250 if (bl->loc_type != bp_loc_software_breakpoint
4251 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4252 continue;
4253
f1310107 4254 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4255 if ((breakpoint_enabled (bl->owner)
1a853c52 4256 || bl->permanent)
f1310107 4257 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4258 {
4259 if (overlay_debugging
35df4500
TJB
4260 && section_is_overlay (bl->section)
4261 && !section_is_mapped (bl->section))
075f6582 4262 continue; /* unmapped overlay -- can't be a match */
1a853c52 4263 else if (bl->permanent)
075f6582
DJ
4264 return permanent_breakpoint_here;
4265 else
4266 any_breakpoint_here = 1;
4267 }
4268 }
c906108c 4269
f486487f 4270 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4271}
4272
d35ae833
PA
4273/* See breakpoint.h. */
4274
4275int
4276breakpoint_in_range_p (struct address_space *aspace,
4277 CORE_ADDR addr, ULONGEST len)
4278{
4279 struct bp_location *bl, **blp_tmp;
4280
4281 ALL_BP_LOCATIONS (bl, blp_tmp)
4282 {
4283 if (bl->loc_type != bp_loc_software_breakpoint
4284 && bl->loc_type != bp_loc_hardware_breakpoint)
4285 continue;
4286
4287 if ((breakpoint_enabled (bl->owner)
4288 || bl->permanent)
4289 && breakpoint_location_address_range_overlap (bl, aspace,
4290 addr, len))
4291 {
4292 if (overlay_debugging
4293 && section_is_overlay (bl->section)
4294 && !section_is_mapped (bl->section))
4295 {
4296 /* Unmapped overlay -- can't be a match. */
4297 continue;
4298 }
4299
4300 return 1;
4301 }
4302 }
4303
4304 return 0;
4305}
4306
1c5cfe86
PA
4307/* Return true if there's a moribund breakpoint at PC. */
4308
4309int
6c95b8df 4310moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4311{
4312 struct bp_location *loc;
4313 int ix;
4314
4315 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4316 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4317 return 1;
4318
4319 return 0;
4320}
c2c6d25f 4321
f7ce857f
PA
4322/* Returns non-zero iff BL is inserted at PC, in address space
4323 ASPACE. */
4324
4325static int
4326bp_location_inserted_here_p (struct bp_location *bl,
4327 struct address_space *aspace, CORE_ADDR pc)
4328{
4329 if (bl->inserted
4330 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4331 aspace, pc))
4332 {
4333 if (overlay_debugging
4334 && section_is_overlay (bl->section)
4335 && !section_is_mapped (bl->section))
4336 return 0; /* unmapped overlay -- can't be a match */
4337 else
4338 return 1;
4339 }
4340 return 0;
4341}
4342
a1fd2fa5 4343/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4344
4345int
a1fd2fa5 4346breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4347{
f7ce857f 4348 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4349
f7ce857f 4350 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4351 {
f7ce857f
PA
4352 struct bp_location *bl = *blp;
4353
35df4500
TJB
4354 if (bl->loc_type != bp_loc_software_breakpoint
4355 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4356 continue;
4357
f7ce857f
PA
4358 if (bp_location_inserted_here_p (bl, aspace, pc))
4359 return 1;
c5aa993b 4360 }
c36b740a
VP
4361 return 0;
4362}
4363
a1fd2fa5
PA
4364/* This function returns non-zero iff there is a software breakpoint
4365 inserted at PC. */
c36b740a
VP
4366
4367int
a1fd2fa5
PA
4368software_breakpoint_inserted_here_p (struct address_space *aspace,
4369 CORE_ADDR pc)
4fa8626c 4370{
f7ce857f 4371 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4372
f7ce857f 4373 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4374 {
f7ce857f
PA
4375 struct bp_location *bl = *blp;
4376
35df4500 4377 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4378 continue;
4379
f7ce857f
PA
4380 if (bp_location_inserted_here_p (bl, aspace, pc))
4381 return 1;
4fa8626c
DJ
4382 }
4383
4384 return 0;
9c02b525
PA
4385}
4386
4387/* See breakpoint.h. */
4388
4389int
4390hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4391 CORE_ADDR pc)
4392{
4393 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4394
4395 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4396 {
4397 struct bp_location *bl = *blp;
4398
4399 if (bl->loc_type != bp_loc_hardware_breakpoint)
4400 continue;
4401
4402 if (bp_location_inserted_here_p (bl, aspace, pc))
4403 return 1;
4404 }
4405
4406 return 0;
4fa8626c
DJ
4407}
4408
9093389c
PA
4409int
4410hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4411 CORE_ADDR addr, ULONGEST len)
4412{
4413 struct breakpoint *bpt;
4414
4415 ALL_BREAKPOINTS (bpt)
4416 {
4417 struct bp_location *loc;
4418
4419 if (bpt->type != bp_hardware_watchpoint
4420 && bpt->type != bp_access_watchpoint)
4421 continue;
4422
4423 if (!breakpoint_enabled (bpt))
4424 continue;
4425
4426 for (loc = bpt->loc; loc; loc = loc->next)
4427 if (loc->pspace->aspace == aspace && loc->inserted)
4428 {
4429 CORE_ADDR l, h;
4430
4431 /* Check for intersection. */
768adc05
PA
4432 l = std::max<CORE_ADDR> (loc->address, addr);
4433 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4434 if (l < h)
4435 return 1;
4436 }
4437 }
4438 return 0;
4439}
c906108c 4440\f
c5aa993b 4441
c906108c
SS
4442/* bpstat stuff. External routines' interfaces are documented
4443 in breakpoint.h. */
4444
4445int
c326b90e 4446is_catchpoint (struct breakpoint *ep)
c906108c 4447{
533be4dd 4448 return (ep->type == bp_catchpoint);
c906108c
SS
4449}
4450
f431efe5
PA
4451/* Frees any storage that is part of a bpstat. Does not walk the
4452 'next' chain. */
4453
4454static void
198757a8
VP
4455bpstat_free (bpstat bs)
4456{
4457 if (bs->old_val != NULL)
4458 value_free (bs->old_val);
9add0f1b 4459 decref_counted_command_line (&bs->commands);
f431efe5 4460 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4461 xfree (bs);
4462}
4463
c906108c
SS
4464/* Clear a bpstat so that it says we are not at any breakpoint.
4465 Also free any storage that is part of a bpstat. */
4466
4467void
fba45db2 4468bpstat_clear (bpstat *bsp)
c906108c
SS
4469{
4470 bpstat p;
4471 bpstat q;
4472
4473 if (bsp == 0)
4474 return;
4475 p = *bsp;
4476 while (p != NULL)
4477 {
4478 q = p->next;
198757a8 4479 bpstat_free (p);
c906108c
SS
4480 p = q;
4481 }
4482 *bsp = NULL;
4483}
4484
4485/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4486 is part of the bpstat is copied as well. */
4487
4488bpstat
fba45db2 4489bpstat_copy (bpstat bs)
c906108c
SS
4490{
4491 bpstat p = NULL;
4492 bpstat tmp;
4493 bpstat retval = NULL;
4494
4495 if (bs == NULL)
4496 return bs;
4497
4498 for (; bs != NULL; bs = bs->next)
4499 {
4500 tmp = (bpstat) xmalloc (sizeof (*tmp));
4501 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4502 incref_counted_command_line (tmp->commands);
f431efe5 4503 incref_bp_location (tmp->bp_location_at);
31cc81e9 4504 if (bs->old_val != NULL)
3c3185ac
JK
4505 {
4506 tmp->old_val = value_copy (bs->old_val);
4507 release_value (tmp->old_val);
4508 }
31cc81e9 4509
c906108c
SS
4510 if (p == NULL)
4511 /* This is the first thing in the chain. */
4512 retval = tmp;
4513 else
4514 p->next = tmp;
4515 p = tmp;
4516 }
4517 p->next = NULL;
4518 return retval;
4519}
4520
4a64f543 4521/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4522
4523bpstat
fba45db2 4524bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4525{
c5aa993b
JM
4526 if (bsp == NULL)
4527 return NULL;
c906108c 4528
c5aa993b
JM
4529 for (; bsp != NULL; bsp = bsp->next)
4530 {
f431efe5 4531 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4532 return bsp;
4533 }
c906108c
SS
4534 return NULL;
4535}
4536
ab04a2af
TT
4537/* See breakpoint.h. */
4538
47591c29 4539int
427cd150 4540bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4541{
ab04a2af
TT
4542 for (; bsp != NULL; bsp = bsp->next)
4543 {
427cd150
TT
4544 if (bsp->breakpoint_at == NULL)
4545 {
4546 /* A moribund location can never explain a signal other than
4547 GDB_SIGNAL_TRAP. */
4548 if (sig == GDB_SIGNAL_TRAP)
47591c29 4549 return 1;
427cd150
TT
4550 }
4551 else
47591c29
PA
4552 {
4553 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4554 sig))
4555 return 1;
4556 }
ab04a2af
TT
4557 }
4558
47591c29 4559 return 0;
ab04a2af
TT
4560}
4561
4a64f543
MS
4562/* Put in *NUM the breakpoint number of the first breakpoint we are
4563 stopped at. *BSP upon return is a bpstat which points to the
4564 remaining breakpoints stopped at (but which is not guaranteed to be
4565 good for anything but further calls to bpstat_num).
4566
8671a17b
PA
4567 Return 0 if passed a bpstat which does not indicate any breakpoints.
4568 Return -1 if stopped at a breakpoint that has been deleted since
4569 we set it.
4570 Return 1 otherwise. */
c906108c
SS
4571
4572int
8671a17b 4573bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4574{
4575 struct breakpoint *b;
4576
4577 if ((*bsp) == NULL)
4578 return 0; /* No more breakpoint values */
8671a17b 4579
4a64f543
MS
4580 /* We assume we'll never have several bpstats that correspond to a
4581 single breakpoint -- otherwise, this function might return the
4582 same number more than once and this will look ugly. */
f431efe5 4583 b = (*bsp)->breakpoint_at;
8671a17b
PA
4584 *bsp = (*bsp)->next;
4585 if (b == NULL)
4586 return -1; /* breakpoint that's been deleted since */
4587
4588 *num = b->number; /* We have its number */
4589 return 1;
c906108c
SS
4590}
4591
e93ca019 4592/* See breakpoint.h. */
c906108c
SS
4593
4594void
e93ca019 4595bpstat_clear_actions (void)
c906108c 4596{
e93ca019
JK
4597 struct thread_info *tp;
4598 bpstat bs;
4599
4600 if (ptid_equal (inferior_ptid, null_ptid))
4601 return;
4602
4603 tp = find_thread_ptid (inferior_ptid);
4604 if (tp == NULL)
4605 return;
4606
4607 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4608 {
9add0f1b 4609 decref_counted_command_line (&bs->commands);
abf85f46 4610
c906108c
SS
4611 if (bs->old_val != NULL)
4612 {
4613 value_free (bs->old_val);
4614 bs->old_val = NULL;
4615 }
4616 }
4617}
4618
f3b1572e
PA
4619/* Called when a command is about to proceed the inferior. */
4620
4621static void
4622breakpoint_about_to_proceed (void)
4623{
4624 if (!ptid_equal (inferior_ptid, null_ptid))
4625 {
4626 struct thread_info *tp = inferior_thread ();
4627
4628 /* Allow inferior function calls in breakpoint commands to not
4629 interrupt the command list. When the call finishes
4630 successfully, the inferior will be standing at the same
4631 breakpoint as if nothing happened. */
16c381f0 4632 if (tp->control.in_infcall)
f3b1572e
PA
4633 return;
4634 }
4635
4636 breakpoint_proceeded = 1;
4637}
4638
4a64f543
MS
4639/* Stub for cleaning up our state if we error-out of a breakpoint
4640 command. */
c906108c 4641static void
4efb68b1 4642cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4643{
4644 executing_breakpoint_commands = 0;
4645}
4646
abf85f46
JK
4647/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4648 or its equivalent. */
4649
4650static int
4651command_line_is_silent (struct command_line *cmd)
4652{
4f45d445 4653 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4654}
4655
4a64f543
MS
4656/* Execute all the commands associated with all the breakpoints at
4657 this location. Any of these commands could cause the process to
4658 proceed beyond this point, etc. We look out for such changes by
4659 checking the global "breakpoint_proceeded" after each command.
c906108c 4660
347bddb7
PA
4661 Returns true if a breakpoint command resumed the inferior. In that
4662 case, it is the caller's responsibility to recall it again with the
4663 bpstat of the current thread. */
4664
4665static int
4666bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4667{
4668 bpstat bs;
4669 struct cleanup *old_chain;
347bddb7 4670 int again = 0;
c906108c
SS
4671
4672 /* Avoid endless recursion if a `source' command is contained
4673 in bs->commands. */
4674 if (executing_breakpoint_commands)
347bddb7 4675 return 0;
c906108c
SS
4676
4677 executing_breakpoint_commands = 1;
4678 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4679
1ac32117 4680 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4681
4a64f543 4682 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4683 bs = *bsp;
4684
4685 breakpoint_proceeded = 0;
4686 for (; bs != NULL; bs = bs->next)
4687 {
9add0f1b 4688 struct counted_command_line *ccmd;
6c50ab1c
JB
4689 struct command_line *cmd;
4690 struct cleanup *this_cmd_tree_chain;
4691
4692 /* Take ownership of the BSP's command tree, if it has one.
4693
4694 The command tree could legitimately contain commands like
4695 'step' and 'next', which call clear_proceed_status, which
4696 frees stop_bpstat's command tree. To make sure this doesn't
4697 free the tree we're executing out from under us, we need to
4698 take ownership of the tree ourselves. Since a given bpstat's
4699 commands are only executed once, we don't need to copy it; we
4700 can clear the pointer in the bpstat, and make sure we free
4701 the tree when we're done. */
9add0f1b
TT
4702 ccmd = bs->commands;
4703 bs->commands = NULL;
abf85f46
JK
4704 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4705 cmd = ccmd ? ccmd->commands : NULL;
4706 if (command_line_is_silent (cmd))
4707 {
4708 /* The action has been already done by bpstat_stop_status. */
4709 cmd = cmd->next;
4710 }
6c50ab1c 4711
c906108c
SS
4712 while (cmd != NULL)
4713 {
4714 execute_control_command (cmd);
4715
4716 if (breakpoint_proceeded)
4717 break;
4718 else
4719 cmd = cmd->next;
4720 }
6c50ab1c
JB
4721
4722 /* We can free this command tree now. */
4723 do_cleanups (this_cmd_tree_chain);
4724
c906108c 4725 if (breakpoint_proceeded)
32c1e744 4726 {
cb814510 4727 if (current_ui->async)
347bddb7
PA
4728 /* If we are in async mode, then the target might be still
4729 running, not stopped at any breakpoint, so nothing for
4730 us to do here -- just return to the event loop. */
4731 ;
32c1e744
VP
4732 else
4733 /* In sync mode, when execute_control_command returns
4734 we're already standing on the next breakpoint.
347bddb7
PA
4735 Breakpoint commands for that stop were not run, since
4736 execute_command does not run breakpoint commands --
4737 only command_line_handler does, but that one is not
4738 involved in execution of breakpoint commands. So, we
4739 can now execute breakpoint commands. It should be
4740 noted that making execute_command do bpstat actions is
4741 not an option -- in this case we'll have recursive
4742 invocation of bpstat for each breakpoint with a
4743 command, and can easily blow up GDB stack. Instead, we
4744 return true, which will trigger the caller to recall us
4745 with the new stop_bpstat. */
4746 again = 1;
4747 break;
32c1e744 4748 }
c906108c 4749 }
c2b8ed2c 4750 do_cleanups (old_chain);
347bddb7
PA
4751 return again;
4752}
4753
4754void
4755bpstat_do_actions (void)
4756{
353d1d73
JK
4757 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4758
347bddb7
PA
4759 /* Do any commands attached to breakpoint we are stopped at. */
4760 while (!ptid_equal (inferior_ptid, null_ptid)
4761 && target_has_execution
4762 && !is_exited (inferior_ptid)
4763 && !is_executing (inferior_ptid))
4764 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4765 and only return when it is stopped at the next breakpoint, we
4766 keep doing breakpoint actions until it returns false to
4767 indicate the inferior was not resumed. */
16c381f0 4768 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4769 break;
353d1d73
JK
4770
4771 discard_cleanups (cleanup_if_error);
c906108c
SS
4772}
4773
fa4727a6
DJ
4774/* Print out the (old or new) value associated with a watchpoint. */
4775
4776static void
4777watchpoint_value_print (struct value *val, struct ui_file *stream)
4778{
4779 if (val == NULL)
4780 fprintf_unfiltered (stream, _("<unreadable>"));
4781 else
79a45b7d
TT
4782 {
4783 struct value_print_options opts;
4784 get_user_print_options (&opts);
4785 value_print (val, stream, &opts);
4786 }
fa4727a6
DJ
4787}
4788
f303dbd6
PA
4789/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4790 debugging multiple threads. */
4791
4792void
4793maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4794{
112e8700 4795 if (uiout->is_mi_like_p ())
f303dbd6
PA
4796 return;
4797
112e8700 4798 uiout->text ("\n");
f303dbd6
PA
4799
4800 if (show_thread_that_caused_stop ())
4801 {
4802 const char *name;
4803 struct thread_info *thr = inferior_thread ();
4804
112e8700
SM
4805 uiout->text ("Thread ");
4806 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4807
4808 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4809 if (name != NULL)
4810 {
112e8700
SM
4811 uiout->text (" \"");
4812 uiout->field_fmt ("name", "%s", name);
4813 uiout->text ("\"");
f303dbd6
PA
4814 }
4815
112e8700 4816 uiout->text (" hit ");
f303dbd6
PA
4817 }
4818}
4819
e514a9d6 4820/* Generic routine for printing messages indicating why we
4a64f543 4821 stopped. The behavior of this function depends on the value
e514a9d6
JM
4822 'print_it' in the bpstat structure. Under some circumstances we
4823 may decide not to print anything here and delegate the task to
4a64f543 4824 normal_stop(). */
e514a9d6
JM
4825
4826static enum print_stop_action
4827print_bp_stop_message (bpstat bs)
4828{
4829 switch (bs->print_it)
4830 {
4831 case print_it_noop:
4a64f543 4832 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4833 return PRINT_UNKNOWN;
4834 break;
4835
4836 case print_it_done:
4837 /* We still want to print the frame, but we already printed the
4a64f543 4838 relevant messages. */
e514a9d6
JM
4839 return PRINT_SRC_AND_LOC;
4840 break;
4841
4842 case print_it_normal:
4f8d1dc6 4843 {
f431efe5
PA
4844 struct breakpoint *b = bs->breakpoint_at;
4845
1a6a67de
TJB
4846 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4847 which has since been deleted. */
4848 if (b == NULL)
4849 return PRINT_UNKNOWN;
4850
348d480f
PA
4851 /* Normal case. Call the breakpoint's print_it method. */
4852 return b->ops->print_it (bs);
4f8d1dc6 4853 }
348d480f 4854 break;
3086aeae 4855
e514a9d6 4856 default:
8e65ff28 4857 internal_error (__FILE__, __LINE__,
e2e0b3e5 4858 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4859 break;
c906108c 4860 }
c906108c
SS
4861}
4862
edcc5120
TT
4863/* A helper function that prints a shared library stopped event. */
4864
4865static void
4866print_solib_event (int is_catchpoint)
4867{
4868 int any_deleted
4869 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4870 int any_added
4871 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4872
4873 if (!is_catchpoint)
4874 {
4875 if (any_added || any_deleted)
112e8700 4876 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4877 else
112e8700
SM
4878 current_uiout->text (_("Stopped due to shared library event (no "
4879 "libraries added or removed)\n"));
edcc5120
TT
4880 }
4881
112e8700
SM
4882 if (current_uiout->is_mi_like_p ())
4883 current_uiout->field_string ("reason",
4884 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4885
4886 if (any_deleted)
4887 {
edcc5120
TT
4888 char *name;
4889 int ix;
4890
112e8700 4891 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4892 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4893 for (ix = 0;
4894 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4895 ix, name);
4896 ++ix)
4897 {
4898 if (ix > 0)
112e8700
SM
4899 current_uiout->text (" ");
4900 current_uiout->field_string ("library", name);
4901 current_uiout->text ("\n");
edcc5120 4902 }
edcc5120
TT
4903 }
4904
4905 if (any_added)
4906 {
4907 struct so_list *iter;
4908 int ix;
edcc5120 4909
112e8700 4910 current_uiout->text (_(" Inferior loaded "));
10f489e5 4911 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4912 for (ix = 0;
4913 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4914 ix, iter);
4915 ++ix)
4916 {
4917 if (ix > 0)
112e8700
SM
4918 current_uiout->text (" ");
4919 current_uiout->field_string ("library", iter->so_name);
4920 current_uiout->text ("\n");
edcc5120 4921 }
edcc5120
TT
4922 }
4923}
4924
e514a9d6
JM
4925/* Print a message indicating what happened. This is called from
4926 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4927 list - a list of the eventpoints that caused this stop. KIND is
4928 the target_waitkind for the stopping event. This
e514a9d6
JM
4929 routine calls the generic print routine for printing a message
4930 about reasons for stopping. This will print (for example) the
4931 "Breakpoint n," part of the output. The return value of this
4932 routine is one of:
c906108c 4933
4a64f543 4934 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4935 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4936 code to print the location. An example is
c5aa993b
JM
4937 "Breakpoint 1, " which should be followed by
4938 the location.
917317f4 4939 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4940 to also print the location part of the message.
4941 An example is the catch/throw messages, which
4a64f543 4942 don't require a location appended to the end.
917317f4 4943 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4944 further info to be printed. */
c906108c 4945
917317f4 4946enum print_stop_action
36dfb11c 4947bpstat_print (bpstat bs, int kind)
c906108c 4948{
f486487f 4949 enum print_stop_action val;
c5aa993b 4950
c906108c 4951 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4952 (Currently all watchpoints go on the bpstat whether hit or not.
4953 That probably could (should) be changed, provided care is taken
c906108c 4954 with respect to bpstat_explains_signal). */
e514a9d6
JM
4955 for (; bs; bs = bs->next)
4956 {
4957 val = print_bp_stop_message (bs);
4958 if (val == PRINT_SRC_ONLY
4959 || val == PRINT_SRC_AND_LOC
4960 || val == PRINT_NOTHING)
4961 return val;
4962 }
c906108c 4963
36dfb11c
TT
4964 /* If we had hit a shared library event breakpoint,
4965 print_bp_stop_message would print out this message. If we hit an
4966 OS-level shared library event, do the same thing. */
4967 if (kind == TARGET_WAITKIND_LOADED)
4968 {
edcc5120 4969 print_solib_event (0);
36dfb11c
TT
4970 return PRINT_NOTHING;
4971 }
4972
e514a9d6 4973 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4974 with and nothing was printed. */
917317f4 4975 return PRINT_UNKNOWN;
c906108c
SS
4976}
4977
c42bd95a
DE
4978/* Evaluate the expression EXP and return 1 if value is zero.
4979 This returns the inverse of the condition because it is called
4980 from catch_errors which returns 0 if an exception happened, and if an
4981 exception happens we want execution to stop.
4a64f543 4982 The argument is a "struct expression *" that has been cast to a
c42bd95a 4983 "void *" to make it pass through catch_errors. */
c906108c
SS
4984
4985static int
4efb68b1 4986breakpoint_cond_eval (void *exp)
c906108c 4987{
278cd55f 4988 struct value *mark = value_mark ();
c5aa993b 4989 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4990
c906108c
SS
4991 value_free_to_mark (mark);
4992 return i;
4993}
4994
5760d0ab 4995/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4996
4997static bpstat
5760d0ab 4998bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4999{
5000 bpstat bs;
5001
5002 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5003 bs->next = NULL;
5004 **bs_link_pointer = bs;
5005 *bs_link_pointer = &bs->next;
f431efe5
PA
5006 bs->breakpoint_at = bl->owner;
5007 bs->bp_location_at = bl;
5008 incref_bp_location (bl);
c906108c
SS
5009 /* If the condition is false, etc., don't do the commands. */
5010 bs->commands = NULL;
5011 bs->old_val = NULL;
5012 bs->print_it = print_it_normal;
5013 return bs;
5014}
5015\f
d983da9c
DJ
5016/* The target has stopped with waitstatus WS. Check if any hardware
5017 watchpoints have triggered, according to the target. */
5018
5019int
5020watchpoints_triggered (struct target_waitstatus *ws)
5021{
d92524f1 5022 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5023 CORE_ADDR addr;
5024 struct breakpoint *b;
5025
5026 if (!stopped_by_watchpoint)
5027 {
5028 /* We were not stopped by a watchpoint. Mark all watchpoints
5029 as not triggered. */
5030 ALL_BREAKPOINTS (b)
cc60f2e3 5031 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5032 {
5033 struct watchpoint *w = (struct watchpoint *) b;
5034
5035 w->watchpoint_triggered = watch_triggered_no;
5036 }
d983da9c
DJ
5037
5038 return 0;
5039 }
5040
5041 if (!target_stopped_data_address (&current_target, &addr))
5042 {
5043 /* We were stopped by a watchpoint, but we don't know where.
5044 Mark all watchpoints as unknown. */
5045 ALL_BREAKPOINTS (b)
cc60f2e3 5046 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5047 {
5048 struct watchpoint *w = (struct watchpoint *) b;
5049
5050 w->watchpoint_triggered = watch_triggered_unknown;
5051 }
d983da9c 5052
3c4797ba 5053 return 1;
d983da9c
DJ
5054 }
5055
5056 /* The target could report the data address. Mark watchpoints
5057 affected by this data address as triggered, and all others as not
5058 triggered. */
5059
5060 ALL_BREAKPOINTS (b)
cc60f2e3 5061 if (is_hardware_watchpoint (b))
d983da9c 5062 {
3a5c3e22 5063 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5064 struct bp_location *loc;
d983da9c 5065
3a5c3e22 5066 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5067 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5068 {
3a5c3e22 5069 if (is_masked_watchpoint (b))
9c06b0b4 5070 {
3a5c3e22
PA
5071 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5072 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5073
5074 if (newaddr == start)
5075 {
3a5c3e22 5076 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5077 break;
5078 }
5079 }
5080 /* Exact match not required. Within range is sufficient. */
5081 else if (target_watchpoint_addr_within_range (&current_target,
5082 addr, loc->address,
5083 loc->length))
5084 {
3a5c3e22 5085 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5086 break;
5087 }
5088 }
d983da9c
DJ
5089 }
5090
5091 return 1;
5092}
5093
c906108c
SS
5094/* Possible return values for watchpoint_check (this can't be an enum
5095 because of check_errors). */
5096/* The watchpoint has been deleted. */
5097#define WP_DELETED 1
5098/* The value has changed. */
5099#define WP_VALUE_CHANGED 2
5100/* The value has not changed. */
5101#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5102/* Ignore this watchpoint, no matter if the value changed or not. */
5103#define WP_IGNORE 4
c906108c
SS
5104
5105#define BP_TEMPFLAG 1
5106#define BP_HARDWAREFLAG 2
5107
4a64f543
MS
5108/* Evaluate watchpoint condition expression and check if its value
5109 changed.
553e4c11
JB
5110
5111 P should be a pointer to struct bpstat, but is defined as a void *
5112 in order for this function to be usable with catch_errors. */
c906108c
SS
5113
5114static int
4efb68b1 5115watchpoint_check (void *p)
c906108c
SS
5116{
5117 bpstat bs = (bpstat) p;
3a5c3e22 5118 struct watchpoint *b;
c906108c
SS
5119 struct frame_info *fr;
5120 int within_current_scope;
5121
f431efe5 5122 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5123 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5124 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5125
f6bc2008
PA
5126 /* If this is a local watchpoint, we only want to check if the
5127 watchpoint frame is in scope if the current thread is the thread
5128 that was used to create the watchpoint. */
5129 if (!watchpoint_in_thread_scope (b))
60e1c644 5130 return WP_IGNORE;
f6bc2008 5131
c906108c
SS
5132 if (b->exp_valid_block == NULL)
5133 within_current_scope = 1;
5134 else
5135 {
edb3359d
DJ
5136 struct frame_info *frame = get_current_frame ();
5137 struct gdbarch *frame_arch = get_frame_arch (frame);
5138 CORE_ADDR frame_pc = get_frame_pc (frame);
5139
c9cf6e20 5140 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5141 still in the function but the stack frame has already been
5142 invalidated. Since we can't rely on the values of local
5143 variables after the stack has been destroyed, we are treating
5144 the watchpoint in that state as `not changed' without further
5145 checking. Don't mark watchpoints as changed if the current
5146 frame is in an epilogue - even if they are in some other
5147 frame, our view of the stack is likely to be wrong and
5148 frame_find_by_id could error out. */
c9cf6e20 5149 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5150 return WP_IGNORE;
a0f49112 5151
101dcfbe 5152 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5153 within_current_scope = (fr != NULL);
69fbadd5
DJ
5154
5155 /* If we've gotten confused in the unwinder, we might have
5156 returned a frame that can't describe this variable. */
edb3359d
DJ
5157 if (within_current_scope)
5158 {
5159 struct symbol *function;
5160
5161 function = get_frame_function (fr);
5162 if (function == NULL
5163 || !contained_in (b->exp_valid_block,
5164 SYMBOL_BLOCK_VALUE (function)))
5165 within_current_scope = 0;
5166 }
69fbadd5 5167
edb3359d 5168 if (within_current_scope)
c906108c
SS
5169 /* If we end up stopping, the current frame will get selected
5170 in normal_stop. So this call to select_frame won't affect
5171 the user. */
0f7d239c 5172 select_frame (fr);
c906108c 5173 }
c5aa993b 5174
c906108c
SS
5175 if (within_current_scope)
5176 {
4a64f543
MS
5177 /* We use value_{,free_to_}mark because it could be a *long*
5178 time before we return to the command level and call
5179 free_all_values. We can't call free_all_values because we
5180 might be in the middle of evaluating a function call. */
c906108c 5181
0cf6dd15 5182 int pc = 0;
9c06b0b4 5183 struct value *mark;
fa4727a6
DJ
5184 struct value *new_val;
5185
3a5c3e22 5186 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5187 /* Since we don't know the exact trigger address (from
5188 stopped_data_address), just tell the user we've triggered
5189 a mask watchpoint. */
5190 return WP_VALUE_CHANGED;
5191
5192 mark = value_mark ();
4d01a485 5193 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5194
bb9d5f81
PP
5195 if (b->val_bitsize != 0)
5196 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5197
4a64f543
MS
5198 /* We use value_equal_contents instead of value_equal because
5199 the latter coerces an array to a pointer, thus comparing just
5200 the address of the array instead of its contents. This is
5201 not what we want. */
fa4727a6 5202 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5203 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5204 {
fa4727a6
DJ
5205 if (new_val != NULL)
5206 {
5207 release_value (new_val);
5208 value_free_to_mark (mark);
5209 }
c906108c
SS
5210 bs->old_val = b->val;
5211 b->val = new_val;
fa4727a6 5212 b->val_valid = 1;
c906108c
SS
5213 return WP_VALUE_CHANGED;
5214 }
5215 else
5216 {
60e1c644 5217 /* Nothing changed. */
c906108c 5218 value_free_to_mark (mark);
c906108c
SS
5219 return WP_VALUE_NOT_CHANGED;
5220 }
5221 }
5222 else
5223 {
5224 /* This seems like the only logical thing to do because
c5aa993b
JM
5225 if we temporarily ignored the watchpoint, then when
5226 we reenter the block in which it is valid it contains
5227 garbage (in the case of a function, it may have two
5228 garbage values, one before and one after the prologue).
5229 So we can't even detect the first assignment to it and
5230 watch after that (since the garbage may or may not equal
5231 the first value assigned). */
348d480f
PA
5232 /* We print all the stop information in
5233 breakpoint_ops->print_it, but in this case, by the time we
5234 call breakpoint_ops->print_it this bp will be deleted
5235 already. So we have no choice but print the information
5236 here. */
468afe6c 5237
0e454242 5238 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5239 {
5240 struct ui_out *uiout = current_uiout;
5241
112e8700
SM
5242 if (uiout->is_mi_like_p ())
5243 uiout->field_string
5244 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5245 uiout->text ("\nWatchpoint ");
5246 uiout->field_int ("wpnum", b->base.number);
5247 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5248 "which its expression is valid.\n");
5249 }
4ce44c66 5250
cdac0397 5251 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5252 decref_counted_command_line (&b->base.commands);
d0fb5eae 5253 watchpoint_del_at_next_stop (b);
c906108c
SS
5254
5255 return WP_DELETED;
5256 }
5257}
5258
18a18393 5259/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5260 breakpoint location BL. This function does not check if we should
5261 stop, only if BL explains the stop. */
5262
18a18393 5263static int
6c95b8df 5264bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5265 struct address_space *aspace, CORE_ADDR bp_addr,
5266 const struct target_waitstatus *ws)
18a18393
VP
5267{
5268 struct breakpoint *b = bl->owner;
5269
348d480f 5270 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5271 gdb_assert (b != NULL);
5272
09ac7c10 5273 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5274}
5275
3a5c3e22
PA
5276/* Determine if the watched values have actually changed, and we
5277 should stop. If not, set BS->stop to 0. */
5278
18a18393
VP
5279static void
5280bpstat_check_watchpoint (bpstat bs)
5281{
2bdf28a0 5282 const struct bp_location *bl;
3a5c3e22 5283 struct watchpoint *b;
2bdf28a0
JK
5284
5285 /* BS is built for existing struct breakpoint. */
f431efe5 5286 bl = bs->bp_location_at;
2bdf28a0 5287 gdb_assert (bl != NULL);
3a5c3e22 5288 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5289 gdb_assert (b != NULL);
18a18393 5290
18a18393 5291 {
18a18393
VP
5292 int must_check_value = 0;
5293
3a5c3e22 5294 if (b->base.type == bp_watchpoint)
18a18393
VP
5295 /* For a software watchpoint, we must always check the
5296 watched value. */
5297 must_check_value = 1;
5298 else if (b->watchpoint_triggered == watch_triggered_yes)
5299 /* We have a hardware watchpoint (read, write, or access)
5300 and the target earlier reported an address watched by
5301 this watchpoint. */
5302 must_check_value = 1;
5303 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5304 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5305 /* We were stopped by a hardware watchpoint, but the target could
5306 not report the data address. We must check the watchpoint's
5307 value. Access and read watchpoints are out of luck; without
5308 a data address, we can't figure it out. */
5309 must_check_value = 1;
3a5c3e22 5310
18a18393
VP
5311 if (must_check_value)
5312 {
3e43a32a
MS
5313 char *message
5314 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5315 b->base.number);
18a18393
VP
5316 struct cleanup *cleanups = make_cleanup (xfree, message);
5317 int e = catch_errors (watchpoint_check, bs, message,
5318 RETURN_MASK_ALL);
5319 do_cleanups (cleanups);
5320 switch (e)
5321 {
5322 case WP_DELETED:
5323 /* We've already printed what needs to be printed. */
5324 bs->print_it = print_it_done;
5325 /* Stop. */
5326 break;
60e1c644
PA
5327 case WP_IGNORE:
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 break;
18a18393 5331 case WP_VALUE_CHANGED:
3a5c3e22 5332 if (b->base.type == bp_read_watchpoint)
18a18393 5333 {
85d721b8
PA
5334 /* There are two cases to consider here:
5335
4a64f543 5336 1. We're watching the triggered memory for reads.
85d721b8
PA
5337 In that case, trust the target, and always report
5338 the watchpoint hit to the user. Even though
5339 reads don't cause value changes, the value may
5340 have changed since the last time it was read, and
5341 since we're not trapping writes, we will not see
5342 those, and as such we should ignore our notion of
5343 old value.
5344
4a64f543 5345 2. We're watching the triggered memory for both
85d721b8
PA
5346 reads and writes. There are two ways this may
5347 happen:
5348
4a64f543 5349 2.1. This is a target that can't break on data
85d721b8
PA
5350 reads only, but can break on accesses (reads or
5351 writes), such as e.g., x86. We detect this case
5352 at the time we try to insert read watchpoints.
5353
4a64f543 5354 2.2. Otherwise, the target supports read
85d721b8
PA
5355 watchpoints, but, the user set an access or write
5356 watchpoint watching the same memory as this read
5357 watchpoint.
5358
5359 If we're watching memory writes as well as reads,
5360 ignore watchpoint hits when we find that the
5361 value hasn't changed, as reads don't cause
5362 changes. This still gives false positives when
5363 the program writes the same value to memory as
5364 what there was already in memory (we will confuse
5365 it for a read), but it's much better than
5366 nothing. */
5367
5368 int other_write_watchpoint = 0;
5369
5370 if (bl->watchpoint_type == hw_read)
5371 {
5372 struct breakpoint *other_b;
5373
5374 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5375 if (other_b->type == bp_hardware_watchpoint
5376 || other_b->type == bp_access_watchpoint)
85d721b8 5377 {
3a5c3e22
PA
5378 struct watchpoint *other_w =
5379 (struct watchpoint *) other_b;
5380
5381 if (other_w->watchpoint_triggered
5382 == watch_triggered_yes)
5383 {
5384 other_write_watchpoint = 1;
5385 break;
5386 }
85d721b8
PA
5387 }
5388 }
5389
5390 if (other_write_watchpoint
5391 || bl->watchpoint_type == hw_access)
5392 {
5393 /* We're watching the same memory for writes,
5394 and the value changed since the last time we
5395 updated it, so this trap must be for a write.
5396 Ignore it. */
5397 bs->print_it = print_it_noop;
5398 bs->stop = 0;
5399 }
18a18393
VP
5400 }
5401 break;
5402 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5403 if (b->base.type == bp_hardware_watchpoint
5404 || b->base.type == bp_watchpoint)
18a18393
VP
5405 {
5406 /* Don't stop: write watchpoints shouldn't fire if
5407 the value hasn't changed. */
5408 bs->print_it = print_it_noop;
5409 bs->stop = 0;
5410 }
5411 /* Stop. */
5412 break;
5413 default:
5414 /* Can't happen. */
5415 case 0:
5416 /* Error from catch_errors. */
468afe6c 5417 {
0e454242 5418 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5419 {
5420 printf_filtered (_("Watchpoint %d deleted.\n"),
5421 b->base.number);
5422 }
5423 watchpoint_del_at_next_stop (b);
5424 /* We've already printed what needs to be printed. */
5425 bs->print_it = print_it_done;
5426 }
18a18393
VP
5427 break;
5428 }
5429 }
5430 else /* must_check_value == 0 */
5431 {
5432 /* This is a case where some watchpoint(s) triggered, but
5433 not at the address of this watchpoint, or else no
5434 watchpoint triggered after all. So don't print
5435 anything for this watchpoint. */
5436 bs->print_it = print_it_noop;
5437 bs->stop = 0;
5438 }
5439 }
5440}
5441
7d4df6a4
DE
5442/* For breakpoints that are currently marked as telling gdb to stop,
5443 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5444 of breakpoint referred to by BS. If we should not stop for this
5445 breakpoint, set BS->stop to 0. */
f431efe5 5446
18a18393
VP
5447static void
5448bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5449{
2bdf28a0
JK
5450 const struct bp_location *bl;
5451 struct breakpoint *b;
7d4df6a4
DE
5452 int value_is_zero = 0;
5453 struct expression *cond;
5454
5455 gdb_assert (bs->stop);
2bdf28a0
JK
5456
5457 /* BS is built for existing struct breakpoint. */
f431efe5 5458 bl = bs->bp_location_at;
2bdf28a0 5459 gdb_assert (bl != NULL);
f431efe5 5460 b = bs->breakpoint_at;
2bdf28a0 5461 gdb_assert (b != NULL);
18a18393 5462
b775012e
LM
5463 /* Even if the target evaluated the condition on its end and notified GDB, we
5464 need to do so again since GDB does not know if we stopped due to a
5465 breakpoint or a single step breakpoint. */
5466
18a18393 5467 if (frame_id_p (b->frame_id)
edb3359d 5468 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5469 {
7d4df6a4
DE
5470 bs->stop = 0;
5471 return;
5472 }
60e1c644 5473
12ab52e9
PA
5474 /* If this is a thread/task-specific breakpoint, don't waste cpu
5475 evaluating the condition if this isn't the specified
5476 thread/task. */
5d5658a1 5477 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5478 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5479
6c1b0f7b
DE
5480 {
5481 bs->stop = 0;
5482 return;
5483 }
5484
6dddc817
DE
5485 /* Evaluate extension language breakpoints that have a "stop" method
5486 implemented. */
5487 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5488
7d4df6a4
DE
5489 if (is_watchpoint (b))
5490 {
5491 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5492
4d01a485 5493 cond = w->cond_exp.get ();
7d4df6a4
DE
5494 }
5495 else
4d01a485 5496 cond = bl->cond.get ();
60e1c644 5497
7d4df6a4
DE
5498 if (cond && b->disposition != disp_del_at_next_stop)
5499 {
5500 int within_current_scope = 1;
5501 struct watchpoint * w;
60e1c644 5502
7d4df6a4
DE
5503 /* We use value_mark and value_free_to_mark because it could
5504 be a long time before we return to the command level and
5505 call free_all_values. We can't call free_all_values
5506 because we might be in the middle of evaluating a
5507 function call. */
5508 struct value *mark = value_mark ();
5509
5510 if (is_watchpoint (b))
5511 w = (struct watchpoint *) b;
5512 else
5513 w = NULL;
5514
5515 /* Need to select the frame, with all that implies so that
5516 the conditions will have the right context. Because we
5517 use the frame, we will not see an inlined function's
5518 variables when we arrive at a breakpoint at the start
5519 of the inlined function; the current frame will be the
5520 call site. */
5521 if (w == NULL || w->cond_exp_valid_block == NULL)
5522 select_frame (get_current_frame ());
5523 else
18a18393 5524 {
7d4df6a4
DE
5525 struct frame_info *frame;
5526
5527 /* For local watchpoint expressions, which particular
5528 instance of a local is being watched matters, so we
5529 keep track of the frame to evaluate the expression
5530 in. To evaluate the condition however, it doesn't
5531 really matter which instantiation of the function
5532 where the condition makes sense triggers the
5533 watchpoint. This allows an expression like "watch
5534 global if q > 10" set in `func', catch writes to
5535 global on all threads that call `func', or catch
5536 writes on all recursive calls of `func' by a single
5537 thread. We simply always evaluate the condition in
5538 the innermost frame that's executing where it makes
5539 sense to evaluate the condition. It seems
5540 intuitive. */
5541 frame = block_innermost_frame (w->cond_exp_valid_block);
5542 if (frame != NULL)
5543 select_frame (frame);
5544 else
5545 within_current_scope = 0;
18a18393 5546 }
7d4df6a4
DE
5547 if (within_current_scope)
5548 value_is_zero
5549 = catch_errors (breakpoint_cond_eval, cond,
5550 "Error in testing breakpoint condition:\n",
5551 RETURN_MASK_ALL);
5552 else
18a18393 5553 {
7d4df6a4
DE
5554 warning (_("Watchpoint condition cannot be tested "
5555 "in the current scope"));
5556 /* If we failed to set the right context for this
5557 watchpoint, unconditionally report it. */
5558 value_is_zero = 0;
18a18393 5559 }
7d4df6a4
DE
5560 /* FIXME-someday, should give breakpoint #. */
5561 value_free_to_mark (mark);
18a18393 5562 }
7d4df6a4
DE
5563
5564 if (cond && value_is_zero)
5565 {
5566 bs->stop = 0;
5567 }
7d4df6a4
DE
5568 else if (b->ignore_count > 0)
5569 {
5570 b->ignore_count--;
5571 bs->stop = 0;
5572 /* Increase the hit count even though we don't stop. */
5573 ++(b->hit_count);
5574 observer_notify_breakpoint_modified (b);
5575 }
18a18393
VP
5576}
5577
1cf4d951
PA
5578/* Returns true if we need to track moribund locations of LOC's type
5579 on the current target. */
5580
5581static int
5582need_moribund_for_location_type (struct bp_location *loc)
5583{
5584 return ((loc->loc_type == bp_loc_software_breakpoint
5585 && !target_supports_stopped_by_sw_breakpoint ())
5586 || (loc->loc_type == bp_loc_hardware_breakpoint
5587 && !target_supports_stopped_by_hw_breakpoint ()));
5588}
5589
18a18393 5590
9709f61c 5591/* Get a bpstat associated with having just stopped at address
d983da9c 5592 BP_ADDR in thread PTID.
c906108c 5593
d983da9c 5594 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5595 don't understand this stop. Result is a chain of bpstat's such
5596 that:
c906108c 5597
c5aa993b 5598 if we don't understand the stop, the result is a null pointer.
c906108c 5599
c5aa993b 5600 if we understand why we stopped, the result is not null.
c906108c 5601
c5aa993b
JM
5602 Each element of the chain refers to a particular breakpoint or
5603 watchpoint at which we have stopped. (We may have stopped for
5604 several reasons concurrently.)
c906108c 5605
c5aa993b
JM
5606 Each element of the chain has valid next, breakpoint_at,
5607 commands, FIXME??? fields. */
c906108c
SS
5608
5609bpstat
6c95b8df 5610bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5611 CORE_ADDR bp_addr, ptid_t ptid,
5612 const struct target_waitstatus *ws)
c906108c 5613{
0d381245 5614 struct breakpoint *b = NULL;
afe38095 5615 struct bp_location *bl;
20874c92 5616 struct bp_location *loc;
5760d0ab
JK
5617 /* First item of allocated bpstat's. */
5618 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5619 /* Pointer to the last thing in the chain currently. */
5760d0ab 5620 bpstat bs;
20874c92 5621 int ix;
429374b8 5622 int need_remove_insert;
f431efe5 5623 int removed_any;
c906108c 5624
f431efe5
PA
5625 /* First, build the bpstat chain with locations that explain a
5626 target stop, while being careful to not set the target running,
5627 as that may invalidate locations (in particular watchpoint
5628 locations are recreated). Resuming will happen here with
5629 breakpoint conditions or watchpoint expressions that include
5630 inferior function calls. */
c5aa993b 5631
429374b8
JK
5632 ALL_BREAKPOINTS (b)
5633 {
1a853c52 5634 if (!breakpoint_enabled (b))
429374b8 5635 continue;
a5606eee 5636
429374b8
JK
5637 for (bl = b->loc; bl != NULL; bl = bl->next)
5638 {
4a64f543
MS
5639 /* For hardware watchpoints, we look only at the first
5640 location. The watchpoint_check function will work on the
5641 entire expression, not the individual locations. For
5642 read watchpoints, the watchpoints_triggered function has
5643 checked all locations already. */
429374b8
JK
5644 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5645 break;
18a18393 5646
f6592439 5647 if (!bl->enabled || bl->shlib_disabled)
429374b8 5648 continue;
c5aa993b 5649
09ac7c10 5650 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5651 continue;
c5aa993b 5652
4a64f543
MS
5653 /* Come here if it's a watchpoint, or if the break address
5654 matches. */
c5aa993b 5655
4a64f543
MS
5656 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5657 explain stop. */
c5aa993b 5658
f431efe5
PA
5659 /* Assume we stop. Should we find a watchpoint that is not
5660 actually triggered, or if the condition of the breakpoint
5661 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5662 bs->stop = 1;
5663 bs->print = 1;
d983da9c 5664
f431efe5
PA
5665 /* If this is a scope breakpoint, mark the associated
5666 watchpoint as triggered so that we will handle the
5667 out-of-scope event. We'll get to the watchpoint next
5668 iteration. */
d0fb5eae 5669 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5670 {
5671 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5672
5673 w->watchpoint_triggered = watch_triggered_yes;
5674 }
f431efe5
PA
5675 }
5676 }
5677
7c16b83e 5678 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5679 if (!target_supports_stopped_by_sw_breakpoint ()
5680 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5681 {
1cf4d951 5682 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5683 {
1cf4d951
PA
5684 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5685 && need_moribund_for_location_type (loc))
5686 {
5687 bs = bpstat_alloc (loc, &bs_link);
5688 /* For hits of moribund locations, we should just proceed. */
5689 bs->stop = 0;
5690 bs->print = 0;
5691 bs->print_it = print_it_noop;
5692 }
f431efe5
PA
5693 }
5694 }
5695
edcc5120
TT
5696 /* A bit of special processing for shlib breakpoints. We need to
5697 process solib loading here, so that the lists of loaded and
5698 unloaded libraries are correct before we handle "catch load" and
5699 "catch unload". */
5700 for (bs = bs_head; bs != NULL; bs = bs->next)
5701 {
5d268276 5702 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5703 {
5704 handle_solib_event ();
5705 break;
5706 }
5707 }
5708
f431efe5
PA
5709 /* Now go through the locations that caused the target to stop, and
5710 check whether we're interested in reporting this stop to higher
5711 layers, or whether we should resume the target transparently. */
5712
5713 removed_any = 0;
5714
5760d0ab 5715 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5716 {
5717 if (!bs->stop)
5718 continue;
5719
f431efe5 5720 b = bs->breakpoint_at;
348d480f
PA
5721 b->ops->check_status (bs);
5722 if (bs->stop)
28010a5d 5723 {
348d480f 5724 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5725
429374b8
JK
5726 if (bs->stop)
5727 {
5728 ++(b->hit_count);
8d3788bd 5729 observer_notify_breakpoint_modified (b);
c906108c 5730
4a64f543 5731 /* We will stop here. */
429374b8
JK
5732 if (b->disposition == disp_disable)
5733 {
816338b5 5734 --(b->enable_count);
1a853c52 5735 if (b->enable_count <= 0)
429374b8 5736 b->enable_state = bp_disabled;
f431efe5 5737 removed_any = 1;
429374b8
JK
5738 }
5739 if (b->silent)
5740 bs->print = 0;
5741 bs->commands = b->commands;
9add0f1b 5742 incref_counted_command_line (bs->commands);
abf85f46
JK
5743 if (command_line_is_silent (bs->commands
5744 ? bs->commands->commands : NULL))
5745 bs->print = 0;
9d6e6e84
HZ
5746
5747 b->ops->after_condition_true (bs);
429374b8
JK
5748 }
5749
348d480f 5750 }
a9b3a50f
PA
5751
5752 /* Print nothing for this entry if we don't stop or don't
5753 print. */
5754 if (!bs->stop || !bs->print)
5755 bs->print_it = print_it_noop;
429374b8 5756 }
876fa593 5757
d983da9c
DJ
5758 /* If we aren't stopping, the value of some hardware watchpoint may
5759 not have changed, but the intermediate memory locations we are
5760 watching may have. Don't bother if we're stopping; this will get
5761 done later. */
d832cb68 5762 need_remove_insert = 0;
5760d0ab
JK
5763 if (! bpstat_causes_stop (bs_head))
5764 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5765 if (!bs->stop
f431efe5
PA
5766 && bs->breakpoint_at
5767 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5768 {
3a5c3e22
PA
5769 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5770
5771 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5772 need_remove_insert = 1;
d983da9c
DJ
5773 }
5774
d832cb68 5775 if (need_remove_insert)
44702360 5776 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5777 else if (removed_any)
44702360 5778 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5779
5760d0ab 5780 return bs_head;
c906108c 5781}
628fe4e4
JK
5782
5783static void
5784handle_jit_event (void)
5785{
5786 struct frame_info *frame;
5787 struct gdbarch *gdbarch;
5788
243a9253
PA
5789 if (debug_infrun)
5790 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5791
628fe4e4
JK
5792 /* Switch terminal for any messages produced by
5793 breakpoint_re_set. */
5794 target_terminal_ours_for_output ();
5795
5796 frame = get_current_frame ();
5797 gdbarch = get_frame_arch (frame);
5798
5799 jit_event_handler (gdbarch);
5800
5801 target_terminal_inferior ();
5802}
5803
5804/* Prepare WHAT final decision for infrun. */
5805
5806/* Decide what infrun needs to do with this bpstat. */
5807
c906108c 5808struct bpstat_what
0e30163f 5809bpstat_what (bpstat bs_head)
c906108c 5810{
c906108c 5811 struct bpstat_what retval;
0e30163f 5812 bpstat bs;
c906108c 5813
628fe4e4 5814 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5815 retval.call_dummy = STOP_NONE;
186c406b 5816 retval.is_longjmp = 0;
628fe4e4 5817
0e30163f 5818 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5819 {
628fe4e4
JK
5820 /* Extract this BS's action. After processing each BS, we check
5821 if its action overrides all we've seem so far. */
5822 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 enum bptype bptype;
5824
c906108c 5825 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5826 {
5827 /* I suspect this can happen if it was a momentary
5828 breakpoint which has since been deleted. */
5829 bptype = bp_none;
5830 }
20874c92 5831 else
f431efe5 5832 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5833
5834 switch (bptype)
c906108c
SS
5835 {
5836 case bp_none:
628fe4e4 5837 break;
c906108c
SS
5838 case bp_breakpoint:
5839 case bp_hardware_breakpoint:
7c16b83e 5840 case bp_single_step:
c906108c
SS
5841 case bp_until:
5842 case bp_finish:
a9b3a50f 5843 case bp_shlib_event:
c906108c
SS
5844 if (bs->stop)
5845 {
5846 if (bs->print)
628fe4e4 5847 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5848 else
628fe4e4 5849 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5850 }
5851 else
628fe4e4 5852 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5853 break;
5854 case bp_watchpoint:
5855 case bp_hardware_watchpoint:
5856 case bp_read_watchpoint:
5857 case bp_access_watchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
628fe4e4 5861 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5862 else
628fe4e4 5863 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5864 }
5865 else
628fe4e4
JK
5866 {
5867 /* There was a watchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
c906108c
SS
5870 break;
5871 case bp_longjmp:
e2e4d78b 5872 case bp_longjmp_call_dummy:
186c406b 5873 case bp_exception:
0a39bb32
PA
5874 if (bs->stop)
5875 {
5876 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5877 retval.is_longjmp = bptype != bp_exception;
5878 }
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5881 break;
5882 case bp_longjmp_resume:
186c406b 5883 case bp_exception_resume:
0a39bb32
PA
5884 if (bs->stop)
5885 {
5886 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5887 retval.is_longjmp = bptype == bp_longjmp_resume;
5888 }
5889 else
5890 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5891 break;
5892 case bp_step_resume:
5893 if (bs->stop)
628fe4e4
JK
5894 this_action = BPSTAT_WHAT_STEP_RESUME;
5895 else
c906108c 5896 {
628fe4e4
JK
5897 /* It is for the wrong frame. */
5898 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5899 }
c906108c 5900 break;
2c03e5be
PA
5901 case bp_hp_step_resume:
5902 if (bs->stop)
5903 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5904 else
5905 {
5906 /* It is for the wrong frame. */
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 }
5909 break;
c906108c 5910 case bp_watchpoint_scope:
c4093a6a 5911 case bp_thread_event:
1900040c 5912 case bp_overlay_event:
0fd8e87f 5913 case bp_longjmp_master:
aa7d318d 5914 case bp_std_terminate_master:
186c406b 5915 case bp_exception_master:
628fe4e4 5916 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5917 break;
ce78b96d 5918 case bp_catchpoint:
c5aa993b
JM
5919 if (bs->stop)
5920 {
5921 if (bs->print)
628fe4e4 5922 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5923 else
628fe4e4 5924 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5925 }
5926 else
628fe4e4
JK
5927 {
5928 /* There was a catchpoint, but we're not stopping.
5929 This requires no further action. */
5930 }
5931 break;
628fe4e4 5932 case bp_jit_event:
628fe4e4 5933 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5934 break;
c906108c 5935 case bp_call_dummy:
53a5351d
JM
5936 /* Make sure the action is stop (silent or noisy),
5937 so infrun.c pops the dummy frame. */
aa7d318d 5938 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5939 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5940 break;
5941 case bp_std_terminate:
5942 /* Make sure the action is stop (silent or noisy),
5943 so infrun.c pops the dummy frame. */
aa7d318d 5944 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5945 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5946 break;
1042e4c0 5947 case bp_tracepoint:
7a697b8d 5948 case bp_fast_tracepoint:
0fb4aa4b 5949 case bp_static_tracepoint:
1042e4c0
SS
5950 /* Tracepoint hits should not be reported back to GDB, and
5951 if one got through somehow, it should have been filtered
5952 out already. */
5953 internal_error (__FILE__, __LINE__,
7a697b8d 5954 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5955 break;
5956 case bp_gnu_ifunc_resolver:
5957 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5958 this_action = BPSTAT_WHAT_SINGLE;
5959 break;
5960 case bp_gnu_ifunc_resolver_return:
5961 /* The breakpoint will be removed, execution will restart from the
5962 PC of the former breakpoint. */
5963 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5964 break;
e7e0cddf
SS
5965
5966 case bp_dprintf:
a11cfd87
HZ
5967 if (bs->stop)
5968 this_action = BPSTAT_WHAT_STOP_SILENT;
5969 else
5970 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5971 break;
5972
628fe4e4
JK
5973 default:
5974 internal_error (__FILE__, __LINE__,
5975 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5976 }
628fe4e4 5977
325fac50 5978 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5979 }
628fe4e4 5980
243a9253
PA
5981 return retval;
5982}
628fe4e4 5983
243a9253
PA
5984void
5985bpstat_run_callbacks (bpstat bs_head)
5986{
5987 bpstat bs;
628fe4e4 5988
0e30163f
JK
5989 for (bs = bs_head; bs != NULL; bs = bs->next)
5990 {
5991 struct breakpoint *b = bs->breakpoint_at;
5992
5993 if (b == NULL)
5994 continue;
5995 switch (b->type)
5996 {
243a9253
PA
5997 case bp_jit_event:
5998 handle_jit_event ();
5999 break;
0e30163f
JK
6000 case bp_gnu_ifunc_resolver:
6001 gnu_ifunc_resolver_stop (b);
6002 break;
6003 case bp_gnu_ifunc_resolver_return:
6004 gnu_ifunc_resolver_return_stop (b);
6005 break;
6006 }
6007 }
c906108c
SS
6008}
6009
6010/* Nonzero if we should step constantly (e.g. watchpoints on machines
6011 without hardware support). This isn't related to a specific bpstat,
6012 just to things like whether watchpoints are set. */
6013
c5aa993b 6014int
fba45db2 6015bpstat_should_step (void)
c906108c
SS
6016{
6017 struct breakpoint *b;
cc59ec59 6018
c906108c 6019 ALL_BREAKPOINTS (b)
717a8278 6020 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6021 return 1;
c906108c
SS
6022 return 0;
6023}
6024
67822962
PA
6025int
6026bpstat_causes_stop (bpstat bs)
6027{
6028 for (; bs != NULL; bs = bs->next)
6029 if (bs->stop)
6030 return 1;
6031
6032 return 0;
6033}
6034
c906108c 6035\f
c5aa993b 6036
170b53b2
UW
6037/* Compute a string of spaces suitable to indent the next line
6038 so it starts at the position corresponding to the table column
6039 named COL_NAME in the currently active table of UIOUT. */
6040
6041static char *
6042wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6043{
6044 static char wrap_indent[80];
6045 int i, total_width, width, align;
c5209615 6046 const char *text;
170b53b2
UW
6047
6048 total_width = 0;
112e8700 6049 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6050 {
6051 if (strcmp (text, col_name) == 0)
6052 {
6053 gdb_assert (total_width < sizeof wrap_indent);
6054 memset (wrap_indent, ' ', total_width);
6055 wrap_indent[total_width] = 0;
6056
6057 return wrap_indent;
6058 }
6059
6060 total_width += width + 1;
6061 }
6062
6063 return NULL;
6064}
6065
b775012e
LM
6066/* Determine if the locations of this breakpoint will have their conditions
6067 evaluated by the target, host or a mix of both. Returns the following:
6068
6069 "host": Host evals condition.
6070 "host or target": Host or Target evals condition.
6071 "target": Target evals condition.
6072*/
6073
6074static const char *
6075bp_condition_evaluator (struct breakpoint *b)
6076{
6077 struct bp_location *bl;
6078 char host_evals = 0;
6079 char target_evals = 0;
6080
6081 if (!b)
6082 return NULL;
6083
6084 if (!is_breakpoint (b))
6085 return NULL;
6086
6087 if (gdb_evaluates_breakpoint_condition_p ()
6088 || !target_supports_evaluation_of_breakpoint_conditions ())
6089 return condition_evaluation_host;
6090
6091 for (bl = b->loc; bl; bl = bl->next)
6092 {
6093 if (bl->cond_bytecode)
6094 target_evals++;
6095 else
6096 host_evals++;
6097 }
6098
6099 if (host_evals && target_evals)
6100 return condition_evaluation_both;
6101 else if (target_evals)
6102 return condition_evaluation_target;
6103 else
6104 return condition_evaluation_host;
6105}
6106
6107/* Determine the breakpoint location's condition evaluator. This is
6108 similar to bp_condition_evaluator, but for locations. */
6109
6110static const char *
6111bp_location_condition_evaluator (struct bp_location *bl)
6112{
6113 if (bl && !is_breakpoint (bl->owner))
6114 return NULL;
6115
6116 if (gdb_evaluates_breakpoint_condition_p ()
6117 || !target_supports_evaluation_of_breakpoint_conditions ())
6118 return condition_evaluation_host;
6119
6120 if (bl && bl->cond_bytecode)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124}
6125
859825b8
JK
6126/* Print the LOC location out of the list of B->LOC locations. */
6127
170b53b2
UW
6128static void
6129print_breakpoint_location (struct breakpoint *b,
6130 struct bp_location *loc)
0d381245 6131{
79a45e25 6132 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6133
6134 scoped_restore_current_program_space restore_pspace;
6c95b8df 6135
859825b8
JK
6136 if (loc != NULL && loc->shlib_disabled)
6137 loc = NULL;
6138
6c95b8df
PA
6139 if (loc != NULL)
6140 set_current_program_space (loc->pspace);
6141
56435ebe 6142 if (b->display_canonical)
d28cd78a 6143 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6144 else if (loc && loc->symtab)
0d381245
VP
6145 {
6146 struct symbol *sym
6147 = find_pc_sect_function (loc->address, loc->section);
6148 if (sym)
6149 {
112e8700
SM
6150 uiout->text ("in ");
6151 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6152 uiout->text (" ");
6153 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6154 uiout->text ("at ");
0d381245 6155 }
112e8700 6156 uiout->field_string ("file",
05cba821 6157 symtab_to_filename_for_display (loc->symtab));
112e8700 6158 uiout->text (":");
05cba821 6159
112e8700
SM
6160 if (uiout->is_mi_like_p ())
6161 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6162
112e8700 6163 uiout->field_int ("line", loc->line_number);
0d381245 6164 }
859825b8 6165 else if (loc)
0d381245 6166 {
d7e74731 6167 string_file stb;
170b53b2 6168
d7e74731 6169 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6170 demangle, "");
112e8700 6171 uiout->field_stream ("at", stb);
0d381245 6172 }
859825b8 6173 else
f00aae0f 6174 {
d28cd78a
TT
6175 uiout->field_string ("pending",
6176 event_location_to_string (b->location.get ()));
f00aae0f
KS
6177 /* If extra_string is available, it could be holding a condition
6178 or dprintf arguments. In either case, make sure it is printed,
6179 too, but only for non-MI streams. */
112e8700 6180 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6181 {
6182 if (b->type == bp_dprintf)
112e8700 6183 uiout->text (",");
f00aae0f 6184 else
112e8700
SM
6185 uiout->text (" ");
6186 uiout->text (b->extra_string);
f00aae0f
KS
6187 }
6188 }
6c95b8df 6189
b775012e
LM
6190 if (loc && is_breakpoint (b)
6191 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6192 && bp_condition_evaluator (b) == condition_evaluation_both)
6193 {
112e8700
SM
6194 uiout->text (" (");
6195 uiout->field_string ("evaluated-by",
b775012e 6196 bp_location_condition_evaluator (loc));
112e8700 6197 uiout->text (")");
b775012e 6198 }
0d381245
VP
6199}
6200
269b11a2
PA
6201static const char *
6202bptype_string (enum bptype type)
c906108c 6203{
c4093a6a
JM
6204 struct ep_type_description
6205 {
6206 enum bptype type;
a121b7c1 6207 const char *description;
c4093a6a
JM
6208 };
6209 static struct ep_type_description bptypes[] =
c906108c 6210 {
c5aa993b
JM
6211 {bp_none, "?deleted?"},
6212 {bp_breakpoint, "breakpoint"},
c906108c 6213 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6214 {bp_single_step, "sw single-step"},
c5aa993b
JM
6215 {bp_until, "until"},
6216 {bp_finish, "finish"},
6217 {bp_watchpoint, "watchpoint"},
c906108c 6218 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6219 {bp_read_watchpoint, "read watchpoint"},
6220 {bp_access_watchpoint, "acc watchpoint"},
6221 {bp_longjmp, "longjmp"},
6222 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6223 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6224 {bp_exception, "exception"},
6225 {bp_exception_resume, "exception resume"},
c5aa993b 6226 {bp_step_resume, "step resume"},
2c03e5be 6227 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6228 {bp_watchpoint_scope, "watchpoint scope"},
6229 {bp_call_dummy, "call dummy"},
aa7d318d 6230 {bp_std_terminate, "std::terminate"},
c5aa993b 6231 {bp_shlib_event, "shlib events"},
c4093a6a 6232 {bp_thread_event, "thread events"},
1900040c 6233 {bp_overlay_event, "overlay events"},
0fd8e87f 6234 {bp_longjmp_master, "longjmp master"},
aa7d318d 6235 {bp_std_terminate_master, "std::terminate master"},
186c406b 6236 {bp_exception_master, "exception master"},
ce78b96d 6237 {bp_catchpoint, "catchpoint"},
1042e4c0 6238 {bp_tracepoint, "tracepoint"},
7a697b8d 6239 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6240 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6241 {bp_dprintf, "dprintf"},
4efc6507 6242 {bp_jit_event, "jit events"},
0e30163f
JK
6243 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6244 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6245 };
269b11a2
PA
6246
6247 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6248 || ((int) type != bptypes[(int) type].type))
6249 internal_error (__FILE__, __LINE__,
6250 _("bptypes table does not describe type #%d."),
6251 (int) type);
6252
6253 return bptypes[(int) type].description;
6254}
6255
998580f1
MK
6256/* For MI, output a field named 'thread-groups' with a list as the value.
6257 For CLI, prefix the list with the string 'inf'. */
6258
6259static void
6260output_thread_groups (struct ui_out *uiout,
6261 const char *field_name,
6262 VEC(int) *inf_num,
6263 int mi_only)
6264{
112e8700 6265 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6266 int inf;
6267 int i;
6268
6269 /* For backward compatibility, don't display inferiors in CLI unless
6270 there are several. Always display them for MI. */
6271 if (!is_mi && mi_only)
6272 return;
6273
10f489e5 6274 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6275
998580f1
MK
6276 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6277 {
6278 if (is_mi)
6279 {
6280 char mi_group[10];
6281
6282 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6283 uiout->field_string (NULL, mi_group);
998580f1
MK
6284 }
6285 else
6286 {
6287 if (i == 0)
112e8700 6288 uiout->text (" inf ");
998580f1 6289 else
112e8700 6290 uiout->text (", ");
998580f1 6291
112e8700 6292 uiout->text (plongest (inf));
998580f1
MK
6293 }
6294 }
998580f1
MK
6295}
6296
269b11a2
PA
6297/* Print B to gdb_stdout. */
6298
6299static void
6300print_one_breakpoint_location (struct breakpoint *b,
6301 struct bp_location *loc,
6302 int loc_number,
6303 struct bp_location **last_loc,
269b11a2
PA
6304 int allflag)
6305{
6306 struct command_line *l;
c2c6d25f 6307 static char bpenables[] = "nynny";
c906108c 6308
79a45e25 6309 struct ui_out *uiout = current_uiout;
0d381245
VP
6310 int header_of_multiple = 0;
6311 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6312 struct value_print_options opts;
6313
6314 get_user_print_options (&opts);
0d381245
VP
6315
6316 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6317 /* See comment in print_one_breakpoint concerning treatment of
6318 breakpoints with single disabled location. */
0d381245
VP
6319 if (loc == NULL
6320 && (b->loc != NULL
6321 && (b->loc->next != NULL || !b->loc->enabled)))
6322 header_of_multiple = 1;
6323 if (loc == NULL)
6324 loc = b->loc;
6325
c4093a6a
JM
6326 annotate_record ();
6327
6328 /* 1 */
6329 annotate_field (0);
0d381245
VP
6330 if (part_of_multiple)
6331 {
6332 char *formatted;
0c6773c1 6333 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6334 uiout->field_string ("number", formatted);
0d381245
VP
6335 xfree (formatted);
6336 }
6337 else
6338 {
112e8700 6339 uiout->field_int ("number", b->number);
0d381245 6340 }
c4093a6a
JM
6341
6342 /* 2 */
6343 annotate_field (1);
0d381245 6344 if (part_of_multiple)
112e8700 6345 uiout->field_skip ("type");
269b11a2 6346 else
112e8700 6347 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6348
6349 /* 3 */
6350 annotate_field (2);
0d381245 6351 if (part_of_multiple)
112e8700 6352 uiout->field_skip ("disp");
0d381245 6353 else
112e8700 6354 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6355
c4093a6a
JM
6356
6357 /* 4 */
6358 annotate_field (3);
0d381245 6359 if (part_of_multiple)
112e8700 6360 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6361 else
112e8700
SM
6362 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6363 uiout->spaces (2);
0d381245 6364
c4093a6a
JM
6365
6366 /* 5 and 6 */
3086aeae 6367 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6368 {
4a64f543
MS
6369 /* Although the print_one can possibly print all locations,
6370 calling it here is not likely to get any nice result. So,
6371 make sure there's just one location. */
0d381245 6372 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6373 b->ops->print_one (b, last_loc);
0d381245 6374 }
3086aeae
DJ
6375 else
6376 switch (b->type)
6377 {
6378 case bp_none:
6379 internal_error (__FILE__, __LINE__,
e2e0b3e5 6380 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6381 break;
c906108c 6382
3086aeae
DJ
6383 case bp_watchpoint:
6384 case bp_hardware_watchpoint:
6385 case bp_read_watchpoint:
6386 case bp_access_watchpoint:
3a5c3e22
PA
6387 {
6388 struct watchpoint *w = (struct watchpoint *) b;
6389
6390 /* Field 4, the address, is omitted (which makes the columns
6391 not line up too nicely with the headers, but the effect
6392 is relatively readable). */
6393 if (opts.addressprint)
112e8700 6394 uiout->field_skip ("addr");
3a5c3e22 6395 annotate_field (5);
112e8700 6396 uiout->field_string ("what", w->exp_string);
3a5c3e22 6397 }
3086aeae
DJ
6398 break;
6399
3086aeae
DJ
6400 case bp_breakpoint:
6401 case bp_hardware_breakpoint:
7c16b83e 6402 case bp_single_step:
3086aeae
DJ
6403 case bp_until:
6404 case bp_finish:
6405 case bp_longjmp:
6406 case bp_longjmp_resume:
e2e4d78b 6407 case bp_longjmp_call_dummy:
186c406b
TT
6408 case bp_exception:
6409 case bp_exception_resume:
3086aeae 6410 case bp_step_resume:
2c03e5be 6411 case bp_hp_step_resume:
3086aeae
DJ
6412 case bp_watchpoint_scope:
6413 case bp_call_dummy:
aa7d318d 6414 case bp_std_terminate:
3086aeae
DJ
6415 case bp_shlib_event:
6416 case bp_thread_event:
6417 case bp_overlay_event:
0fd8e87f 6418 case bp_longjmp_master:
aa7d318d 6419 case bp_std_terminate_master:
186c406b 6420 case bp_exception_master:
1042e4c0 6421 case bp_tracepoint:
7a697b8d 6422 case bp_fast_tracepoint:
0fb4aa4b 6423 case bp_static_tracepoint:
e7e0cddf 6424 case bp_dprintf:
4efc6507 6425 case bp_jit_event:
0e30163f
JK
6426 case bp_gnu_ifunc_resolver:
6427 case bp_gnu_ifunc_resolver_return:
79a45b7d 6428 if (opts.addressprint)
3086aeae
DJ
6429 {
6430 annotate_field (4);
54e52265 6431 if (header_of_multiple)
112e8700 6432 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6433 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6434 uiout->field_string ("addr", "<PENDING>");
0101ce28 6435 else
112e8700 6436 uiout->field_core_addr ("addr",
5af949e3 6437 loc->gdbarch, loc->address);
3086aeae
DJ
6438 }
6439 annotate_field (5);
0d381245 6440 if (!header_of_multiple)
170b53b2 6441 print_breakpoint_location (b, loc);
0d381245 6442 if (b->loc)
a6d9a66e 6443 *last_loc = b->loc;
3086aeae
DJ
6444 break;
6445 }
c906108c 6446
6c95b8df 6447
998580f1 6448 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6449 {
6450 struct inferior *inf;
998580f1
MK
6451 VEC(int) *inf_num = NULL;
6452 int mi_only = 1;
6c95b8df 6453
998580f1 6454 ALL_INFERIORS (inf)
6c95b8df
PA
6455 {
6456 if (inf->pspace == loc->pspace)
998580f1 6457 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6458 }
998580f1
MK
6459
6460 /* For backward compatibility, don't display inferiors in CLI unless
6461 there are several. Always display for MI. */
6462 if (allflag
6463 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6464 && (number_of_program_spaces () > 1
6465 || number_of_inferiors () > 1)
6466 /* LOC is for existing B, it cannot be in
6467 moribund_locations and thus having NULL OWNER. */
6468 && loc->owner->type != bp_catchpoint))
6469 mi_only = 0;
6470 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6471 VEC_free (int, inf_num);
6c95b8df
PA
6472 }
6473
4a306c9a 6474 if (!part_of_multiple)
c4093a6a 6475 {
4a306c9a
JB
6476 if (b->thread != -1)
6477 {
6478 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6479 "stop only in" line a little further down. */
112e8700
SM
6480 uiout->text (" thread ");
6481 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6482 }
6483 else if (b->task != 0)
6484 {
112e8700
SM
6485 uiout->text (" task ");
6486 uiout->field_int ("task", b->task);
4a306c9a 6487 }
c4093a6a 6488 }
f1310107 6489
112e8700 6490 uiout->text ("\n");
f1310107 6491
348d480f 6492 if (!part_of_multiple)
f1310107
TJB
6493 b->ops->print_one_detail (b, uiout);
6494
0d381245 6495 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6496 {
6497 annotate_field (6);
112e8700 6498 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6499 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6500 the frame ID. */
112e8700 6501 uiout->field_core_addr ("frame",
5af949e3 6502 b->gdbarch, b->frame_id.stack_addr);
112e8700 6503 uiout->text ("\n");
c4093a6a
JM
6504 }
6505
28010a5d 6506 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6507 {
6508 annotate_field (7);
d77f58be 6509 if (is_tracepoint (b))
112e8700 6510 uiout->text ("\ttrace only if ");
1042e4c0 6511 else
112e8700
SM
6512 uiout->text ("\tstop only if ");
6513 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6514
6515 /* Print whether the target is doing the breakpoint's condition
6516 evaluation. If GDB is doing the evaluation, don't print anything. */
6517 if (is_breakpoint (b)
6518 && breakpoint_condition_evaluation_mode ()
6519 == condition_evaluation_target)
6520 {
112e8700
SM
6521 uiout->text (" (");
6522 uiout->field_string ("evaluated-by",
b775012e 6523 bp_condition_evaluator (b));
112e8700 6524 uiout->text (" evals)");
b775012e 6525 }
112e8700 6526 uiout->text ("\n");
0101ce28
JJ
6527 }
6528
0d381245 6529 if (!part_of_multiple && b->thread != -1)
c4093a6a 6530 {
4a64f543 6531 /* FIXME should make an annotation for this. */
112e8700
SM
6532 uiout->text ("\tstop only in thread ");
6533 if (uiout->is_mi_like_p ())
6534 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6535 else
6536 {
6537 struct thread_info *thr = find_thread_global_id (b->thread);
6538
112e8700 6539 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6540 }
112e8700 6541 uiout->text ("\n");
c4093a6a
JM
6542 }
6543
556ec64d
YQ
6544 if (!part_of_multiple)
6545 {
6546 if (b->hit_count)
31f56a27
YQ
6547 {
6548 /* FIXME should make an annotation for this. */
6549 if (is_catchpoint (b))
112e8700 6550 uiout->text ("\tcatchpoint");
31f56a27 6551 else if (is_tracepoint (b))
112e8700 6552 uiout->text ("\ttracepoint");
31f56a27 6553 else
112e8700
SM
6554 uiout->text ("\tbreakpoint");
6555 uiout->text (" already hit ");
6556 uiout->field_int ("times", b->hit_count);
31f56a27 6557 if (b->hit_count == 1)
112e8700 6558 uiout->text (" time\n");
31f56a27 6559 else
112e8700 6560 uiout->text (" times\n");
31f56a27 6561 }
556ec64d
YQ
6562 else
6563 {
31f56a27 6564 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6565 if (uiout->is_mi_like_p ())
6566 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6567 }
6568 }
8b93c638 6569
0d381245 6570 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6571 {
6572 annotate_field (8);
112e8700
SM
6573 uiout->text ("\tignore next ");
6574 uiout->field_int ("ignore", b->ignore_count);
6575 uiout->text (" hits\n");
c4093a6a 6576 }
059fb39f 6577
816338b5
SS
6578 /* Note that an enable count of 1 corresponds to "enable once"
6579 behavior, which is reported by the combination of enablement and
6580 disposition, so we don't need to mention it here. */
6581 if (!part_of_multiple && b->enable_count > 1)
6582 {
6583 annotate_field (8);
112e8700 6584 uiout->text ("\tdisable after ");
816338b5
SS
6585 /* Tweak the wording to clarify that ignore and enable counts
6586 are distinct, and have additive effect. */
6587 if (b->ignore_count)
112e8700 6588 uiout->text ("additional ");
816338b5 6589 else
112e8700
SM
6590 uiout->text ("next ");
6591 uiout->field_int ("enable", b->enable_count);
6592 uiout->text (" hits\n");
816338b5
SS
6593 }
6594
f196051f
SS
6595 if (!part_of_multiple && is_tracepoint (b))
6596 {
6597 struct tracepoint *tp = (struct tracepoint *) b;
6598
6599 if (tp->traceframe_usage)
6600 {
112e8700
SM
6601 uiout->text ("\ttrace buffer usage ");
6602 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6603 uiout->text (" bytes\n");
f196051f
SS
6604 }
6605 }
d3ce09f5 6606
9add0f1b 6607 l = b->commands ? b->commands->commands : NULL;
059fb39f 6608 if (!part_of_multiple && l)
c4093a6a
JM
6609 {
6610 annotate_field (9);
2e783024 6611 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6612 print_command_lines (uiout, l, 4);
c4093a6a 6613 }
d24317b4 6614
d9b3f62e 6615 if (is_tracepoint (b))
1042e4c0 6616 {
d9b3f62e
PA
6617 struct tracepoint *t = (struct tracepoint *) b;
6618
6619 if (!part_of_multiple && t->pass_count)
6620 {
6621 annotate_field (10);
112e8700
SM
6622 uiout->text ("\tpass count ");
6623 uiout->field_int ("pass", t->pass_count);
6624 uiout->text (" \n");
d9b3f62e 6625 }
f2a8bc8a
YQ
6626
6627 /* Don't display it when tracepoint or tracepoint location is
6628 pending. */
6629 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6630 {
6631 annotate_field (11);
6632
112e8700
SM
6633 if (uiout->is_mi_like_p ())
6634 uiout->field_string ("installed",
f2a8bc8a
YQ
6635 loc->inserted ? "y" : "n");
6636 else
6637 {
6638 if (loc->inserted)
112e8700 6639 uiout->text ("\t");
f2a8bc8a 6640 else
112e8700
SM
6641 uiout->text ("\tnot ");
6642 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6643 }
6644 }
1042e4c0
SS
6645 }
6646
112e8700 6647 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6648 {
3a5c3e22
PA
6649 if (is_watchpoint (b))
6650 {
6651 struct watchpoint *w = (struct watchpoint *) b;
6652
112e8700 6653 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6654 }
f00aae0f 6655 else if (b->location != NULL
d28cd78a 6656 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6657 uiout->field_string ("original-location",
d28cd78a 6658 event_location_to_string (b->location.get ()));
d24317b4 6659 }
c4093a6a 6660}
c5aa993b 6661
0d381245
VP
6662static void
6663print_one_breakpoint (struct breakpoint *b,
4a64f543 6664 struct bp_location **last_loc,
6c95b8df 6665 int allflag)
0d381245 6666{
79a45e25 6667 struct ui_out *uiout = current_uiout;
8d3788bd 6668
2e783024
TT
6669 {
6670 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6671
2e783024
TT
6672 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6673 }
0d381245
VP
6674
6675 /* If this breakpoint has custom print function,
6676 it's already printed. Otherwise, print individual
6677 locations, if any. */
6678 if (b->ops == NULL || b->ops->print_one == NULL)
6679 {
4a64f543
MS
6680 /* If breakpoint has a single location that is disabled, we
6681 print it as if it had several locations, since otherwise it's
6682 hard to represent "breakpoint enabled, location disabled"
6683 situation.
6684
6685 Note that while hardware watchpoints have several locations
a3be7890 6686 internally, that's not a property exposed to user. */
0d381245 6687 if (b->loc
a5606eee 6688 && !is_hardware_watchpoint (b)
8d3788bd 6689 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6690 {
6691 struct bp_location *loc;
6692 int n = 1;
8d3788bd 6693
0d381245 6694 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6695 {
2e783024 6696 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6697 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6698 }
0d381245
VP
6699 }
6700 }
6701}
6702
a6d9a66e
UW
6703static int
6704breakpoint_address_bits (struct breakpoint *b)
6705{
6706 int print_address_bits = 0;
6707 struct bp_location *loc;
6708
c6d81124
PA
6709 /* Software watchpoints that aren't watching memory don't have an
6710 address to print. */
6711 if (is_no_memory_software_watchpoint (b))
6712 return 0;
6713
a6d9a66e
UW
6714 for (loc = b->loc; loc; loc = loc->next)
6715 {
c7437ca6
PA
6716 int addr_bit;
6717
c7437ca6 6718 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6719 if (addr_bit > print_address_bits)
6720 print_address_bits = addr_bit;
6721 }
6722
6723 return print_address_bits;
6724}
0d381245 6725
c4093a6a
JM
6726struct captured_breakpoint_query_args
6727 {
6728 int bnum;
6729 };
c5aa993b 6730
c4093a6a 6731static int
2b65245e 6732do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6733{
9a3c8263
SM
6734 struct captured_breakpoint_query_args *args
6735 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6736 struct breakpoint *b;
a6d9a66e 6737 struct bp_location *dummy_loc = NULL;
cc59ec59 6738
c4093a6a
JM
6739 ALL_BREAKPOINTS (b)
6740 {
6741 if (args->bnum == b->number)
c5aa993b 6742 {
12c5a436 6743 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6744 return GDB_RC_OK;
c5aa993b 6745 }
c4093a6a
JM
6746 }
6747 return GDB_RC_NONE;
6748}
c5aa993b 6749
c4093a6a 6750enum gdb_rc
4a64f543
MS
6751gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6752 char **error_message)
c4093a6a
JM
6753{
6754 struct captured_breakpoint_query_args args;
cc59ec59 6755
c4093a6a
JM
6756 args.bnum = bnum;
6757 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6758 an error. */
b0b13bb4
DJ
6759 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6760 error_message, RETURN_MASK_ALL) < 0)
6761 return GDB_RC_FAIL;
6762 else
6763 return GDB_RC_OK;
c4093a6a 6764}
c5aa993b 6765
09d682a4
TT
6766/* Return true if this breakpoint was set by the user, false if it is
6767 internal or momentary. */
6768
6769int
6770user_breakpoint_p (struct breakpoint *b)
6771{
46c6471b 6772 return b->number > 0;
09d682a4
TT
6773}
6774
93daf339
TT
6775/* See breakpoint.h. */
6776
6777int
6778pending_breakpoint_p (struct breakpoint *b)
6779{
6780 return b->loc == NULL;
6781}
6782
7f3b0473 6783/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6784 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6785 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6786 FILTER is non-NULL, call it on each breakpoint and only include the
6787 ones for which it returns non-zero. Return the total number of
6788 breakpoints listed. */
c906108c 6789
d77f58be 6790static int
e5a67952 6791breakpoint_1 (char *args, int allflag,
4a64f543 6792 int (*filter) (const struct breakpoint *))
c4093a6a 6793{
52f0bd74 6794 struct breakpoint *b;
a6d9a66e 6795 struct bp_location *last_loc = NULL;
7f3b0473 6796 int nr_printable_breakpoints;
3b31d625 6797 struct cleanup *bkpttbl_chain;
79a45b7d 6798 struct value_print_options opts;
a6d9a66e 6799 int print_address_bits = 0;
269b11a2 6800 int print_type_col_width = 14;
79a45e25 6801 struct ui_out *uiout = current_uiout;
269b11a2 6802
79a45b7d
TT
6803 get_user_print_options (&opts);
6804
4a64f543
MS
6805 /* Compute the number of rows in the table, as well as the size
6806 required for address fields. */
7f3b0473
AC
6807 nr_printable_breakpoints = 0;
6808 ALL_BREAKPOINTS (b)
e5a67952
MS
6809 {
6810 /* If we have a filter, only list the breakpoints it accepts. */
6811 if (filter && !filter (b))
6812 continue;
6813
6814 /* If we have an "args" string, it is a list of breakpoints to
6815 accept. Skip the others. */
6816 if (args != NULL && *args != '\0')
6817 {
6818 if (allflag && parse_and_eval_long (args) != b->number)
6819 continue;
6820 if (!allflag && !number_is_in_list (args, b->number))
6821 continue;
6822 }
269b11a2 6823
e5a67952
MS
6824 if (allflag || user_breakpoint_p (b))
6825 {
6826 int addr_bit, type_len;
a6d9a66e 6827
e5a67952
MS
6828 addr_bit = breakpoint_address_bits (b);
6829 if (addr_bit > print_address_bits)
6830 print_address_bits = addr_bit;
269b11a2 6831
e5a67952
MS
6832 type_len = strlen (bptype_string (b->type));
6833 if (type_len > print_type_col_width)
6834 print_type_col_width = type_len;
6835
6836 nr_printable_breakpoints++;
6837 }
6838 }
7f3b0473 6839
79a45b7d 6840 if (opts.addressprint)
3b31d625 6841 bkpttbl_chain
3e43a32a
MS
6842 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6843 nr_printable_breakpoints,
3b31d625 6844 "BreakpointTable");
8b93c638 6845 else
3b31d625 6846 bkpttbl_chain
3e43a32a
MS
6847 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6848 nr_printable_breakpoints,
3b31d625 6849 "BreakpointTable");
8b93c638 6850
7f3b0473 6851 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6852 annotate_breakpoints_headers ();
6853 if (nr_printable_breakpoints > 0)
6854 annotate_field (0);
112e8700 6855 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6856 if (nr_printable_breakpoints > 0)
6857 annotate_field (1);
112e8700 6858 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
d7faa9e7
AC
6859 if (nr_printable_breakpoints > 0)
6860 annotate_field (2);
112e8700 6861 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (3);
112e8700 6864 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6865 if (opts.addressprint)
e5a67952
MS
6866 {
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (4);
6869 if (print_address_bits <= 32)
112e8700 6870 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
e5a67952 6871 else
112e8700 6872 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
e5a67952 6873 }
d7faa9e7
AC
6874 if (nr_printable_breakpoints > 0)
6875 annotate_field (5);
112e8700
SM
6876 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6877 uiout->table_body ();
d7faa9e7
AC
6878 if (nr_printable_breakpoints > 0)
6879 annotate_breakpoints_table ();
7f3b0473 6880
c4093a6a 6881 ALL_BREAKPOINTS (b)
e5a67952
MS
6882 {
6883 QUIT;
6884 /* If we have a filter, only list the breakpoints it accepts. */
6885 if (filter && !filter (b))
6886 continue;
6887
6888 /* If we have an "args" string, it is a list of breakpoints to
6889 accept. Skip the others. */
6890
6891 if (args != NULL && *args != '\0')
6892 {
6893 if (allflag) /* maintenance info breakpoint */
6894 {
6895 if (parse_and_eval_long (args) != b->number)
6896 continue;
6897 }
6898 else /* all others */
6899 {
6900 if (!number_is_in_list (args, b->number))
6901 continue;
6902 }
6903 }
6904 /* We only print out user settable breakpoints unless the
6905 allflag is set. */
6906 if (allflag || user_breakpoint_p (b))
12c5a436 6907 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6908 }
6909
3b31d625 6910 do_cleanups (bkpttbl_chain);
698384cd 6911
7f3b0473 6912 if (nr_printable_breakpoints == 0)
c906108c 6913 {
4a64f543
MS
6914 /* If there's a filter, let the caller decide how to report
6915 empty list. */
d77f58be
SS
6916 if (!filter)
6917 {
e5a67952 6918 if (args == NULL || *args == '\0')
112e8700 6919 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6920 else
112e8700 6921 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6922 args);
d77f58be 6923 }
c906108c
SS
6924 }
6925 else
c4093a6a 6926 {
a6d9a66e
UW
6927 if (last_loc && !server_command)
6928 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6929 }
c906108c 6930
4a64f543 6931 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6932 there have been breakpoints? */
c906108c 6933 annotate_breakpoints_table_end ();
d77f58be
SS
6934
6935 return nr_printable_breakpoints;
c906108c
SS
6936}
6937
ad443146
SS
6938/* Display the value of default-collect in a way that is generally
6939 compatible with the breakpoint list. */
6940
6941static void
6942default_collect_info (void)
6943{
79a45e25
PA
6944 struct ui_out *uiout = current_uiout;
6945
ad443146
SS
6946 /* If it has no value (which is frequently the case), say nothing; a
6947 message like "No default-collect." gets in user's face when it's
6948 not wanted. */
6949 if (!*default_collect)
6950 return;
6951
6952 /* The following phrase lines up nicely with per-tracepoint collect
6953 actions. */
112e8700
SM
6954 uiout->text ("default collect ");
6955 uiout->field_string ("default-collect", default_collect);
6956 uiout->text (" \n");
ad443146
SS
6957}
6958
c906108c 6959static void
e5a67952 6960breakpoints_info (char *args, int from_tty)
c906108c 6961{
e5a67952 6962 breakpoint_1 (args, 0, NULL);
ad443146
SS
6963
6964 default_collect_info ();
d77f58be
SS
6965}
6966
6967static void
e5a67952 6968watchpoints_info (char *args, int from_tty)
d77f58be 6969{
e5a67952 6970 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6971 struct ui_out *uiout = current_uiout;
d77f58be
SS
6972
6973 if (num_printed == 0)
6974 {
e5a67952 6975 if (args == NULL || *args == '\0')
112e8700 6976 uiout->message ("No watchpoints.\n");
d77f58be 6977 else
112e8700 6978 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6979 }
c906108c
SS
6980}
6981
7a292a7a 6982static void
e5a67952 6983maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6984{
e5a67952 6985 breakpoint_1 (args, 1, NULL);
ad443146
SS
6986
6987 default_collect_info ();
c906108c
SS
6988}
6989
0d381245 6990static int
714835d5 6991breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6992 struct program_space *pspace,
714835d5 6993 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6994{
6995 struct bp_location *bl = b->loc;
cc59ec59 6996
0d381245
VP
6997 for (; bl; bl = bl->next)
6998 {
6c95b8df
PA
6999 if (bl->pspace == pspace
7000 && bl->address == pc
0d381245
VP
7001 && (!overlay_debugging || bl->section == section))
7002 return 1;
7003 }
7004 return 0;
7005}
7006
672f9b60 7007/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7008 concerns with logical breakpoints, so we match program spaces, not
7009 address spaces. */
c906108c
SS
7010
7011static void
6c95b8df
PA
7012describe_other_breakpoints (struct gdbarch *gdbarch,
7013 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7014 struct obj_section *section, int thread)
c906108c 7015{
52f0bd74
AC
7016 int others = 0;
7017 struct breakpoint *b;
c906108c
SS
7018
7019 ALL_BREAKPOINTS (b)
672f9b60
KP
7020 others += (user_breakpoint_p (b)
7021 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7022 if (others > 0)
7023 {
a3f17187
AC
7024 if (others == 1)
7025 printf_filtered (_("Note: breakpoint "));
7026 else /* if (others == ???) */
7027 printf_filtered (_("Note: breakpoints "));
c906108c 7028 ALL_BREAKPOINTS (b)
672f9b60 7029 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7030 {
7031 others--;
7032 printf_filtered ("%d", b->number);
7033 if (b->thread == -1 && thread != -1)
7034 printf_filtered (" (all threads)");
7035 else if (b->thread != -1)
7036 printf_filtered (" (thread %d)", b->thread);
7037 printf_filtered ("%s%s ",
059fb39f 7038 ((b->enable_state == bp_disabled
f8eba3c6 7039 || b->enable_state == bp_call_disabled)
0d381245 7040 ? " (disabled)"
0d381245
VP
7041 : ""),
7042 (others > 1) ? ","
7043 : ((others == 1) ? " and" : ""));
7044 }
a3f17187 7045 printf_filtered (_("also set at pc "));
5af949e3 7046 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7047 printf_filtered (".\n");
7048 }
7049}
7050\f
c906108c 7051
e4f237da 7052/* Return true iff it is meaningful to use the address member of
244558af
LM
7053 BPT locations. For some breakpoint types, the locations' address members
7054 are irrelevant and it makes no sense to attempt to compare them to other
7055 addresses (or use them for any other purpose either).
e4f237da 7056
4a64f543 7057 More specifically, each of the following breakpoint types will
244558af 7058 always have a zero valued location address and we don't want to mark
4a64f543 7059 breakpoints of any of these types to be a duplicate of an actual
244558af 7060 breakpoint location at address zero:
e4f237da
KB
7061
7062 bp_watchpoint
2d134ed3
PA
7063 bp_catchpoint
7064
7065*/
e4f237da
KB
7066
7067static int
7068breakpoint_address_is_meaningful (struct breakpoint *bpt)
7069{
7070 enum bptype type = bpt->type;
7071
2d134ed3
PA
7072 return (type != bp_watchpoint && type != bp_catchpoint);
7073}
7074
7075/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7076 true if LOC1 and LOC2 represent the same watchpoint location. */
7077
7078static int
4a64f543
MS
7079watchpoint_locations_match (struct bp_location *loc1,
7080 struct bp_location *loc2)
2d134ed3 7081{
3a5c3e22
PA
7082 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7083 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7084
7085 /* Both of them must exist. */
7086 gdb_assert (w1 != NULL);
7087 gdb_assert (w2 != NULL);
2bdf28a0 7088
4a64f543
MS
7089 /* If the target can evaluate the condition expression in hardware,
7090 then we we need to insert both watchpoints even if they are at
7091 the same place. Otherwise the watchpoint will only trigger when
7092 the condition of whichever watchpoint was inserted evaluates to
7093 true, not giving a chance for GDB to check the condition of the
7094 other watchpoint. */
3a5c3e22 7095 if ((w1->cond_exp
4a64f543
MS
7096 && target_can_accel_watchpoint_condition (loc1->address,
7097 loc1->length,
0cf6dd15 7098 loc1->watchpoint_type,
4d01a485 7099 w1->cond_exp.get ()))
3a5c3e22 7100 || (w2->cond_exp
4a64f543
MS
7101 && target_can_accel_watchpoint_condition (loc2->address,
7102 loc2->length,
0cf6dd15 7103 loc2->watchpoint_type,
4d01a485 7104 w2->cond_exp.get ())))
0cf6dd15
TJB
7105 return 0;
7106
85d721b8
PA
7107 /* Note that this checks the owner's type, not the location's. In
7108 case the target does not support read watchpoints, but does
7109 support access watchpoints, we'll have bp_read_watchpoint
7110 watchpoints with hw_access locations. Those should be considered
7111 duplicates of hw_read locations. The hw_read locations will
7112 become hw_access locations later. */
2d134ed3
PA
7113 return (loc1->owner->type == loc2->owner->type
7114 && loc1->pspace->aspace == loc2->pspace->aspace
7115 && loc1->address == loc2->address
7116 && loc1->length == loc2->length);
e4f237da
KB
7117}
7118
31e77af2 7119/* See breakpoint.h. */
6c95b8df 7120
31e77af2 7121int
6c95b8df
PA
7122breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7123 struct address_space *aspace2, CORE_ADDR addr2)
7124{
f5656ead 7125 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7126 || aspace1 == aspace2)
7127 && addr1 == addr2);
7128}
7129
f1310107
TJB
7130/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7131 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7132 matches ASPACE2. On targets that have global breakpoints, the address
7133 space doesn't really matter. */
7134
7135static int
7136breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7137 int len1, struct address_space *aspace2,
7138 CORE_ADDR addr2)
7139{
f5656ead 7140 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7141 || aspace1 == aspace2)
7142 && addr2 >= addr1 && addr2 < addr1 + len1);
7143}
7144
7145/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7146 a ranged breakpoint. In most targets, a match happens only if ASPACE
7147 matches the breakpoint's address space. On targets that have global
7148 breakpoints, the address space doesn't really matter. */
7149
7150static int
7151breakpoint_location_address_match (struct bp_location *bl,
7152 struct address_space *aspace,
7153 CORE_ADDR addr)
7154{
7155 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7156 aspace, addr)
7157 || (bl->length
7158 && breakpoint_address_match_range (bl->pspace->aspace,
7159 bl->address, bl->length,
7160 aspace, addr)));
7161}
7162
d35ae833
PA
7163/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7164 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7165 match happens only if ASPACE matches the breakpoint's address
7166 space. On targets that have global breakpoints, the address space
7167 doesn't really matter. */
7168
7169static int
7170breakpoint_location_address_range_overlap (struct bp_location *bl,
7171 struct address_space *aspace,
7172 CORE_ADDR addr, int len)
7173{
7174 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7175 || bl->pspace->aspace == aspace)
7176 {
7177 int bl_len = bl->length != 0 ? bl->length : 1;
7178
7179 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7180 return 1;
7181 }
7182 return 0;
7183}
7184
1e4d1764
YQ
7185/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7186 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7187 true, otherwise returns false. */
7188
7189static int
7190tracepoint_locations_match (struct bp_location *loc1,
7191 struct bp_location *loc2)
7192{
7193 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7194 /* Since tracepoint locations are never duplicated with others', tracepoint
7195 locations at the same address of different tracepoints are regarded as
7196 different locations. */
7197 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7198 else
7199 return 0;
7200}
7201
2d134ed3
PA
7202/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7203 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7204 represent the same location. */
7205
7206static int
4a64f543
MS
7207breakpoint_locations_match (struct bp_location *loc1,
7208 struct bp_location *loc2)
2d134ed3 7209{
2bdf28a0
JK
7210 int hw_point1, hw_point2;
7211
7212 /* Both of them must not be in moribund_locations. */
7213 gdb_assert (loc1->owner != NULL);
7214 gdb_assert (loc2->owner != NULL);
7215
7216 hw_point1 = is_hardware_watchpoint (loc1->owner);
7217 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7218
7219 if (hw_point1 != hw_point2)
7220 return 0;
7221 else if (hw_point1)
7222 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7223 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7224 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7225 else
f1310107
TJB
7226 /* We compare bp_location.length in order to cover ranged breakpoints. */
7227 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7228 loc2->pspace->aspace, loc2->address)
7229 && loc1->length == loc2->length);
2d134ed3
PA
7230}
7231
76897487
KB
7232static void
7233breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7234 int bnum, int have_bnum)
7235{
f63fbe86
MS
7236 /* The longest string possibly returned by hex_string_custom
7237 is 50 chars. These must be at least that big for safety. */
7238 char astr1[64];
7239 char astr2[64];
76897487 7240
bb599908
PH
7241 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7242 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7243 if (have_bnum)
8a3fe4f8 7244 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7245 bnum, astr1, astr2);
7246 else
8a3fe4f8 7247 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7248}
7249
4a64f543
MS
7250/* Adjust a breakpoint's address to account for architectural
7251 constraints on breakpoint placement. Return the adjusted address.
7252 Note: Very few targets require this kind of adjustment. For most
7253 targets, this function is simply the identity function. */
76897487
KB
7254
7255static CORE_ADDR
a6d9a66e
UW
7256adjust_breakpoint_address (struct gdbarch *gdbarch,
7257 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7258{
a6d9a66e 7259 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7260 {
7261 /* Very few targets need any kind of breakpoint adjustment. */
7262 return bpaddr;
7263 }
88f7da05
KB
7264 else if (bptype == bp_watchpoint
7265 || bptype == bp_hardware_watchpoint
7266 || bptype == bp_read_watchpoint
7267 || bptype == bp_access_watchpoint
fe798b75 7268 || bptype == bp_catchpoint)
88f7da05
KB
7269 {
7270 /* Watchpoints and the various bp_catch_* eventpoints should not
7271 have their addresses modified. */
7272 return bpaddr;
7273 }
7c16b83e
PA
7274 else if (bptype == bp_single_step)
7275 {
7276 /* Single-step breakpoints should not have their addresses
7277 modified. If there's any architectural constrain that
7278 applies to this address, then it should have already been
7279 taken into account when the breakpoint was created in the
7280 first place. If we didn't do this, stepping through e.g.,
7281 Thumb-2 IT blocks would break. */
7282 return bpaddr;
7283 }
76897487
KB
7284 else
7285 {
7286 CORE_ADDR adjusted_bpaddr;
7287
7288 /* Some targets have architectural constraints on the placement
7289 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7290 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7291
7292 /* An adjusted breakpoint address can significantly alter
7293 a user's expectations. Print a warning if an adjustment
7294 is required. */
7295 if (adjusted_bpaddr != bpaddr)
7296 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7297
7298 return adjusted_bpaddr;
7299 }
7300}
7301
5625a286 7302bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7303{
5625a286 7304 bp_location *loc = this;
7cc221ef 7305
348d480f
PA
7306 gdb_assert (ops != NULL);
7307
28010a5d
PA
7308 loc->ops = ops;
7309 loc->owner = owner;
b775012e 7310 loc->cond_bytecode = NULL;
0d381245
VP
7311 loc->shlib_disabled = 0;
7312 loc->enabled = 1;
e049a4b5 7313
28010a5d 7314 switch (owner->type)
e049a4b5
DJ
7315 {
7316 case bp_breakpoint:
7c16b83e 7317 case bp_single_step:
e049a4b5
DJ
7318 case bp_until:
7319 case bp_finish:
7320 case bp_longjmp:
7321 case bp_longjmp_resume:
e2e4d78b 7322 case bp_longjmp_call_dummy:
186c406b
TT
7323 case bp_exception:
7324 case bp_exception_resume:
e049a4b5 7325 case bp_step_resume:
2c03e5be 7326 case bp_hp_step_resume:
e049a4b5
DJ
7327 case bp_watchpoint_scope:
7328 case bp_call_dummy:
aa7d318d 7329 case bp_std_terminate:
e049a4b5
DJ
7330 case bp_shlib_event:
7331 case bp_thread_event:
7332 case bp_overlay_event:
4efc6507 7333 case bp_jit_event:
0fd8e87f 7334 case bp_longjmp_master:
aa7d318d 7335 case bp_std_terminate_master:
186c406b 7336 case bp_exception_master:
0e30163f
JK
7337 case bp_gnu_ifunc_resolver:
7338 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7339 case bp_dprintf:
e049a4b5 7340 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7341 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7342 break;
7343 case bp_hardware_breakpoint:
7344 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7345 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7346 break;
7347 case bp_hardware_watchpoint:
7348 case bp_read_watchpoint:
7349 case bp_access_watchpoint:
7350 loc->loc_type = bp_loc_hardware_watchpoint;
7351 break;
7352 case bp_watchpoint:
ce78b96d 7353 case bp_catchpoint:
15c3d785
PA
7354 case bp_tracepoint:
7355 case bp_fast_tracepoint:
0fb4aa4b 7356 case bp_static_tracepoint:
e049a4b5
DJ
7357 loc->loc_type = bp_loc_other;
7358 break;
7359 default:
e2e0b3e5 7360 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7361 }
7362
f431efe5 7363 loc->refc = 1;
28010a5d
PA
7364}
7365
7366/* Allocate a struct bp_location. */
7367
7368static struct bp_location *
7369allocate_bp_location (struct breakpoint *bpt)
7370{
348d480f
PA
7371 return bpt->ops->allocate_location (bpt);
7372}
7cc221ef 7373
f431efe5
PA
7374static void
7375free_bp_location (struct bp_location *loc)
fe3f5fa8 7376{
348d480f 7377 loc->ops->dtor (loc);
4d01a485 7378 delete loc;
fe3f5fa8
VP
7379}
7380
f431efe5
PA
7381/* Increment reference count. */
7382
7383static void
7384incref_bp_location (struct bp_location *bl)
7385{
7386 ++bl->refc;
7387}
7388
7389/* Decrement reference count. If the reference count reaches 0,
7390 destroy the bp_location. Sets *BLP to NULL. */
7391
7392static void
7393decref_bp_location (struct bp_location **blp)
7394{
0807b50c
PA
7395 gdb_assert ((*blp)->refc > 0);
7396
f431efe5
PA
7397 if (--(*blp)->refc == 0)
7398 free_bp_location (*blp);
7399 *blp = NULL;
7400}
7401
346774a9 7402/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7403
346774a9
PA
7404static void
7405add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7406{
346774a9 7407 struct breakpoint *b1;
c906108c 7408
346774a9
PA
7409 /* Add this breakpoint to the end of the chain so that a list of
7410 breakpoints will come out in order of increasing numbers. */
7411
7412 b1 = breakpoint_chain;
7413 if (b1 == 0)
7414 breakpoint_chain = b;
7415 else
7416 {
7417 while (b1->next)
7418 b1 = b1->next;
7419 b1->next = b;
7420 }
7421}
7422
7423/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7424
7425static void
7426init_raw_breakpoint_without_location (struct breakpoint *b,
7427 struct gdbarch *gdbarch,
28010a5d 7428 enum bptype bptype,
c0a91b2b 7429 const struct breakpoint_ops *ops)
346774a9 7430{
348d480f
PA
7431 gdb_assert (ops != NULL);
7432
28010a5d 7433 b->ops = ops;
4d28f7a8 7434 b->type = bptype;
a6d9a66e 7435 b->gdbarch = gdbarch;
c906108c
SS
7436 b->language = current_language->la_language;
7437 b->input_radix = input_radix;
d0fb5eae 7438 b->related_breakpoint = b;
346774a9
PA
7439}
7440
7441/* Helper to set_raw_breakpoint below. Creates a breakpoint
7442 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7443
7444static struct breakpoint *
7445set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7446 enum bptype bptype,
c0a91b2b 7447 const struct breakpoint_ops *ops)
346774a9 7448{
4d01a485 7449 struct breakpoint *b = new breakpoint ();
346774a9 7450
348d480f 7451 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7452 add_to_breakpoint_chain (b);
0d381245
VP
7453 return b;
7454}
7455
0e30163f
JK
7456/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7457 resolutions should be made as the user specified the location explicitly
7458 enough. */
7459
0d381245 7460static void
0e30163f 7461set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7462{
2bdf28a0
JK
7463 gdb_assert (loc->owner != NULL);
7464
0d381245 7465 if (loc->owner->type == bp_breakpoint
1042e4c0 7466 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7467 || is_tracepoint (loc->owner))
0d381245 7468 {
0e30163f 7469 int is_gnu_ifunc;
2c02bd72 7470 const char *function_name;
6a3a010b 7471 CORE_ADDR func_addr;
0e30163f 7472
2c02bd72 7473 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7474 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7475
7476 if (is_gnu_ifunc && !explicit_loc)
7477 {
7478 struct breakpoint *b = loc->owner;
7479
7480 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7481 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7482 &loc->requested_address))
7483 {
7484 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7485 loc->address = adjust_breakpoint_address (loc->gdbarch,
7486 loc->requested_address,
7487 b->type);
7488 }
7489 else if (b->type == bp_breakpoint && b->loc == loc
7490 && loc->next == NULL && b->related_breakpoint == b)
7491 {
7492 /* Create only the whole new breakpoint of this type but do not
7493 mess more complicated breakpoints with multiple locations. */
7494 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7495 /* Remember the resolver's address for use by the return
7496 breakpoint. */
7497 loc->related_address = func_addr;
0e30163f
JK
7498 }
7499 }
7500
2c02bd72
DE
7501 if (function_name)
7502 loc->function_name = xstrdup (function_name);
0d381245
VP
7503 }
7504}
7505
a6d9a66e 7506/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7507struct gdbarch *
a6d9a66e
UW
7508get_sal_arch (struct symtab_and_line sal)
7509{
7510 if (sal.section)
7511 return get_objfile_arch (sal.section->objfile);
7512 if (sal.symtab)
eb822aa6 7513 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7514
7515 return NULL;
7516}
7517
346774a9
PA
7518/* Low level routine for partially initializing a breakpoint of type
7519 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7520 file name, and line number are provided by SAL.
0d381245
VP
7521
7522 It is expected that the caller will complete the initialization of
7523 the newly created breakpoint struct as well as output any status
c56053d2 7524 information regarding the creation of a new breakpoint. */
0d381245 7525
346774a9
PA
7526static void
7527init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7528 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7529 const struct breakpoint_ops *ops)
0d381245 7530{
28010a5d 7531 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7532
3742cc8b 7533 add_location_to_breakpoint (b, &sal);
0d381245 7534
6c95b8df
PA
7535 if (bptype != bp_catchpoint)
7536 gdb_assert (sal.pspace != NULL);
7537
f8eba3c6
TT
7538 /* Store the program space that was used to set the breakpoint,
7539 except for ordinary breakpoints, which are independent of the
7540 program space. */
7541 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7542 b->pspace = sal.pspace;
346774a9 7543}
c906108c 7544
346774a9
PA
7545/* set_raw_breakpoint is a low level routine for allocating and
7546 partially initializing a breakpoint of type BPTYPE. The newly
7547 created breakpoint's address, section, source file name, and line
7548 number are provided by SAL. The newly created and partially
7549 initialized breakpoint is added to the breakpoint chain and
7550 is also returned as the value of this function.
7551
7552 It is expected that the caller will complete the initialization of
7553 the newly created breakpoint struct as well as output any status
7554 information regarding the creation of a new breakpoint. In
7555 particular, set_raw_breakpoint does NOT set the breakpoint
7556 number! Care should be taken to not allow an error to occur
7557 prior to completing the initialization of the breakpoint. If this
7558 should happen, a bogus breakpoint will be left on the chain. */
7559
7560struct breakpoint *
7561set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7562 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7563 const struct breakpoint_ops *ops)
346774a9 7564{
4d01a485 7565 struct breakpoint *b = new breakpoint ();
346774a9 7566
348d480f 7567 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7568 add_to_breakpoint_chain (b);
c906108c
SS
7569 return b;
7570}
7571
53a5351d 7572/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7573 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7574 initiated the operation. */
c906108c
SS
7575
7576void
186c406b 7577set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7578{
35df4500 7579 struct breakpoint *b, *b_tmp;
5d5658a1 7580 int thread = tp->global_num;
0fd8e87f
UW
7581
7582 /* To avoid having to rescan all objfile symbols at every step,
7583 we maintain a list of continually-inserted but always disabled
7584 longjmp "master" breakpoints. Here, we simply create momentary
7585 clones of those and enable them for the requested thread. */
35df4500 7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7587 if (b->pspace == current_program_space
186c406b
TT
7588 && (b->type == bp_longjmp_master
7589 || b->type == bp_exception_master))
0fd8e87f 7590 {
06edf0c0
PA
7591 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7592 struct breakpoint *clone;
cc59ec59 7593
e2e4d78b
JK
7594 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7595 after their removal. */
06edf0c0 7596 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7597 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7598 clone->thread = thread;
7599 }
186c406b
TT
7600
7601 tp->initiating_frame = frame;
c906108c
SS
7602}
7603
611c83ae 7604/* Delete all longjmp breakpoints from THREAD. */
c906108c 7605void
611c83ae 7606delete_longjmp_breakpoint (int thread)
c906108c 7607{
35df4500 7608 struct breakpoint *b, *b_tmp;
c906108c 7609
35df4500 7610 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7611 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7612 {
7613 if (b->thread == thread)
7614 delete_breakpoint (b);
7615 }
c906108c
SS
7616}
7617
f59f708a
PA
7618void
7619delete_longjmp_breakpoint_at_next_stop (int thread)
7620{
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_longjmp || b->type == bp_exception)
7625 {
7626 if (b->thread == thread)
7627 b->disposition = disp_del_at_next_stop;
7628 }
7629}
7630
e2e4d78b
JK
7631/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7632 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7633 pointer to any of them. Return NULL if this system cannot place longjmp
7634 breakpoints. */
7635
7636struct breakpoint *
7637set_longjmp_breakpoint_for_call_dummy (void)
7638{
7639 struct breakpoint *b, *retval = NULL;
7640
7641 ALL_BREAKPOINTS (b)
7642 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7643 {
7644 struct breakpoint *new_b;
7645
7646 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7647 &momentary_breakpoint_ops,
7648 1);
5d5658a1 7649 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7650
7651 /* Link NEW_B into the chain of RETVAL breakpoints. */
7652
7653 gdb_assert (new_b->related_breakpoint == new_b);
7654 if (retval == NULL)
7655 retval = new_b;
7656 new_b->related_breakpoint = retval;
7657 while (retval->related_breakpoint != new_b->related_breakpoint)
7658 retval = retval->related_breakpoint;
7659 retval->related_breakpoint = new_b;
7660 }
7661
7662 return retval;
7663}
7664
7665/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7666 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7667 stack.
7668
7669 You should call this function only at places where it is safe to currently
7670 unwind the whole stack. Failed stack unwind would discard live dummy
7671 frames. */
7672
7673void
b67a2c6f 7674check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7675{
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7679 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7680 {
7681 struct breakpoint *dummy_b = b->related_breakpoint;
7682
7683 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7684 dummy_b = dummy_b->related_breakpoint;
7685 if (dummy_b->type != bp_call_dummy
7686 || frame_find_by_id (dummy_b->frame_id) != NULL)
7687 continue;
7688
b67a2c6f 7689 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7690
7691 while (b->related_breakpoint != b)
7692 {
7693 if (b_tmp == b->related_breakpoint)
7694 b_tmp = b->related_breakpoint->next;
7695 delete_breakpoint (b->related_breakpoint);
7696 }
7697 delete_breakpoint (b);
7698 }
7699}
7700
1900040c
MS
7701void
7702enable_overlay_breakpoints (void)
7703{
52f0bd74 7704 struct breakpoint *b;
1900040c
MS
7705
7706 ALL_BREAKPOINTS (b)
7707 if (b->type == bp_overlay_event)
7708 {
7709 b->enable_state = bp_enabled;
44702360 7710 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7711 overlay_events_enabled = 1;
1900040c
MS
7712 }
7713}
7714
7715void
7716disable_overlay_breakpoints (void)
7717{
52f0bd74 7718 struct breakpoint *b;
1900040c
MS
7719
7720 ALL_BREAKPOINTS (b)
7721 if (b->type == bp_overlay_event)
7722 {
7723 b->enable_state = bp_disabled;
44702360 7724 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7725 overlay_events_enabled = 0;
1900040c
MS
7726 }
7727}
7728
aa7d318d
TT
7729/* Set an active std::terminate breakpoint for each std::terminate
7730 master breakpoint. */
7731void
7732set_std_terminate_breakpoint (void)
7733{
35df4500 7734 struct breakpoint *b, *b_tmp;
aa7d318d 7735
35df4500 7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7737 if (b->pspace == current_program_space
7738 && b->type == bp_std_terminate_master)
7739 {
06edf0c0 7740 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7741 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7742 }
7743}
7744
7745/* Delete all the std::terminate breakpoints. */
7746void
7747delete_std_terminate_breakpoint (void)
7748{
35df4500 7749 struct breakpoint *b, *b_tmp;
aa7d318d 7750
35df4500 7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7752 if (b->type == bp_std_terminate)
7753 delete_breakpoint (b);
7754}
7755
c4093a6a 7756struct breakpoint *
a6d9a66e 7757create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7758{
7759 struct breakpoint *b;
c4093a6a 7760
06edf0c0
PA
7761 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7762 &internal_breakpoint_ops);
7763
b5de0fa7 7764 b->enable_state = bp_enabled;
f00aae0f 7765 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7766 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7767
44702360 7768 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7769
c4093a6a
JM
7770 return b;
7771}
7772
0101ce28
JJ
7773struct lang_and_radix
7774 {
7775 enum language lang;
7776 int radix;
7777 };
7778
4efc6507
DE
7779/* Create a breakpoint for JIT code registration and unregistration. */
7780
7781struct breakpoint *
7782create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7783{
2a7f3dff
PA
7784 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7785 &internal_breakpoint_ops);
4efc6507 7786}
0101ce28 7787
03673fc7
PP
7788/* Remove JIT code registration and unregistration breakpoint(s). */
7789
7790void
7791remove_jit_event_breakpoints (void)
7792{
7793 struct breakpoint *b, *b_tmp;
7794
7795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7796 if (b->type == bp_jit_event
7797 && b->loc->pspace == current_program_space)
7798 delete_breakpoint (b);
7799}
7800
cae688ec
JJ
7801void
7802remove_solib_event_breakpoints (void)
7803{
35df4500 7804 struct breakpoint *b, *b_tmp;
cae688ec 7805
35df4500 7806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7807 if (b->type == bp_shlib_event
7808 && b->loc->pspace == current_program_space)
cae688ec
JJ
7809 delete_breakpoint (b);
7810}
7811
f37f681c
PA
7812/* See breakpoint.h. */
7813
7814void
7815remove_solib_event_breakpoints_at_next_stop (void)
7816{
7817 struct breakpoint *b, *b_tmp;
7818
7819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7820 if (b->type == bp_shlib_event
7821 && b->loc->pspace == current_program_space)
7822 b->disposition = disp_del_at_next_stop;
7823}
7824
04086b45
PA
7825/* Helper for create_solib_event_breakpoint /
7826 create_and_insert_solib_event_breakpoint. Allows specifying which
7827 INSERT_MODE to pass through to update_global_location_list. */
7828
7829static struct breakpoint *
7830create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7831 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7832{
7833 struct breakpoint *b;
7834
06edf0c0
PA
7835 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7836 &internal_breakpoint_ops);
04086b45 7837 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7838 return b;
7839}
7840
04086b45
PA
7841struct breakpoint *
7842create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7843{
7844 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7845}
7846
f37f681c
PA
7847/* See breakpoint.h. */
7848
7849struct breakpoint *
7850create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7851{
7852 struct breakpoint *b;
7853
04086b45
PA
7854 /* Explicitly tell update_global_location_list to insert
7855 locations. */
7856 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7857 if (!b->loc->inserted)
7858 {
7859 delete_breakpoint (b);
7860 return NULL;
7861 }
7862 return b;
7863}
7864
cae688ec
JJ
7865/* Disable any breakpoints that are on code in shared libraries. Only
7866 apply to enabled breakpoints, disabled ones can just stay disabled. */
7867
7868void
cb851954 7869disable_breakpoints_in_shlibs (void)
cae688ec 7870{
876fa593 7871 struct bp_location *loc, **locp_tmp;
cae688ec 7872
876fa593 7873 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7874 {
2bdf28a0 7875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7876 struct breakpoint *b = loc->owner;
2bdf28a0 7877
4a64f543
MS
7878 /* We apply the check to all breakpoints, including disabled for
7879 those with loc->duplicate set. This is so that when breakpoint
7880 becomes enabled, or the duplicate is removed, gdb will try to
7881 insert all breakpoints. If we don't set shlib_disabled here,
7882 we'll try to insert those breakpoints and fail. */
1042e4c0 7883 if (((b->type == bp_breakpoint)
508ccb1f 7884 || (b->type == bp_jit_event)
1042e4c0 7885 || (b->type == bp_hardware_breakpoint)
d77f58be 7886 || (is_tracepoint (b)))
6c95b8df 7887 && loc->pspace == current_program_space
0d381245 7888 && !loc->shlib_disabled
6c95b8df 7889 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7890 )
0d381245
VP
7891 {
7892 loc->shlib_disabled = 1;
7893 }
cae688ec
JJ
7894 }
7895}
7896
63644780
NB
7897/* Disable any breakpoints and tracepoints that are in SOLIB upon
7898 notification of unloaded_shlib. Only apply to enabled breakpoints,
7899 disabled ones can just stay disabled. */
84acb35a 7900
75149521 7901static void
84acb35a
JJ
7902disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7903{
876fa593 7904 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7905 int disabled_shlib_breaks = 0;
7906
876fa593 7907 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7908 {
2bdf28a0 7909 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7910 struct breakpoint *b = loc->owner;
cc59ec59 7911
1e4d1764 7912 if (solib->pspace == loc->pspace
e2dd7057 7913 && !loc->shlib_disabled
1e4d1764
YQ
7914 && (((b->type == bp_breakpoint
7915 || b->type == bp_jit_event
7916 || b->type == bp_hardware_breakpoint)
7917 && (loc->loc_type == bp_loc_hardware_breakpoint
7918 || loc->loc_type == bp_loc_software_breakpoint))
7919 || is_tracepoint (b))
e2dd7057 7920 && solib_contains_address_p (solib, loc->address))
84acb35a 7921 {
e2dd7057
PP
7922 loc->shlib_disabled = 1;
7923 /* At this point, we cannot rely on remove_breakpoint
7924 succeeding so we must mark the breakpoint as not inserted
7925 to prevent future errors occurring in remove_breakpoints. */
7926 loc->inserted = 0;
8d3788bd
VP
7927
7928 /* This may cause duplicate notifications for the same breakpoint. */
7929 observer_notify_breakpoint_modified (b);
7930
e2dd7057
PP
7931 if (!disabled_shlib_breaks)
7932 {
7933 target_terminal_ours_for_output ();
3e43a32a
MS
7934 warning (_("Temporarily disabling breakpoints "
7935 "for unloaded shared library \"%s\""),
e2dd7057 7936 solib->so_name);
84acb35a 7937 }
e2dd7057 7938 disabled_shlib_breaks = 1;
84acb35a
JJ
7939 }
7940 }
84acb35a
JJ
7941}
7942
63644780
NB
7943/* Disable any breakpoints and tracepoints in OBJFILE upon
7944 notification of free_objfile. Only apply to enabled breakpoints,
7945 disabled ones can just stay disabled. */
7946
7947static void
7948disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7949{
7950 struct breakpoint *b;
7951
7952 if (objfile == NULL)
7953 return;
7954
d03de421
PA
7955 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7956 managed by the user with add-symbol-file/remove-symbol-file.
7957 Similarly to how breakpoints in shared libraries are handled in
7958 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7959 shlib_disabled so they end up uninserted on the next global
7960 location list update. Shared libraries not loaded by the user
7961 aren't handled here -- they're already handled in
7962 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7963 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7964 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7965 main objfile). */
7966 if ((objfile->flags & OBJF_SHARED) == 0
7967 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7968 return;
7969
7970 ALL_BREAKPOINTS (b)
7971 {
7972 struct bp_location *loc;
7973 int bp_modified = 0;
7974
7975 if (!is_breakpoint (b) && !is_tracepoint (b))
7976 continue;
7977
7978 for (loc = b->loc; loc != NULL; loc = loc->next)
7979 {
7980 CORE_ADDR loc_addr = loc->address;
7981
7982 if (loc->loc_type != bp_loc_hardware_breakpoint
7983 && loc->loc_type != bp_loc_software_breakpoint)
7984 continue;
7985
7986 if (loc->shlib_disabled != 0)
7987 continue;
7988
7989 if (objfile->pspace != loc->pspace)
7990 continue;
7991
7992 if (loc->loc_type != bp_loc_hardware_breakpoint
7993 && loc->loc_type != bp_loc_software_breakpoint)
7994 continue;
7995
7996 if (is_addr_in_objfile (loc_addr, objfile))
7997 {
7998 loc->shlib_disabled = 1;
08351840
PA
7999 /* At this point, we don't know whether the object was
8000 unmapped from the inferior or not, so leave the
8001 inserted flag alone. We'll handle failure to
8002 uninsert quietly, in case the object was indeed
8003 unmapped. */
63644780
NB
8004
8005 mark_breakpoint_location_modified (loc);
8006
8007 bp_modified = 1;
8008 }
8009 }
8010
8011 if (bp_modified)
8012 observer_notify_breakpoint_modified (b);
8013 }
8014}
8015
ce78b96d
JB
8016/* FORK & VFORK catchpoints. */
8017
e29a4733
PA
8018/* An instance of this type is used to represent a fork or vfork
8019 catchpoint. It includes a "struct breakpoint" as a kind of base
8020 class; users downcast to "struct breakpoint *" when needed. A
8021 breakpoint is really of this type iff its ops pointer points to
8022 CATCH_FORK_BREAKPOINT_OPS. */
8023
8024struct fork_catchpoint
8025{
8026 /* The base class. */
8027 struct breakpoint base;
8028
8029 /* Process id of a child process whose forking triggered this
8030 catchpoint. This field is only valid immediately after this
8031 catchpoint has triggered. */
8032 ptid_t forked_inferior_pid;
8033};
8034
4a64f543
MS
8035/* Implement the "insert" breakpoint_ops method for fork
8036 catchpoints. */
ce78b96d 8037
77b06cd7
TJB
8038static int
8039insert_catch_fork (struct bp_location *bl)
ce78b96d 8040{
dfd4cc63 8041 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8042}
8043
4a64f543
MS
8044/* Implement the "remove" breakpoint_ops method for fork
8045 catchpoints. */
ce78b96d
JB
8046
8047static int
73971819 8048remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8049{
dfd4cc63 8050 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8051}
8052
8053/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8054 catchpoints. */
8055
8056static int
f1310107 8057breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8058 struct address_space *aspace, CORE_ADDR bp_addr,
8059 const struct target_waitstatus *ws)
ce78b96d 8060{
e29a4733
PA
8061 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8062
f90263c1
TT
8063 if (ws->kind != TARGET_WAITKIND_FORKED)
8064 return 0;
8065
8066 c->forked_inferior_pid = ws->value.related_pid;
8067 return 1;
ce78b96d
JB
8068}
8069
4a64f543
MS
8070/* Implement the "print_it" breakpoint_ops method for fork
8071 catchpoints. */
ce78b96d
JB
8072
8073static enum print_stop_action
348d480f 8074print_it_catch_fork (bpstat bs)
ce78b96d 8075{
36dfb11c 8076 struct ui_out *uiout = current_uiout;
348d480f
PA
8077 struct breakpoint *b = bs->breakpoint_at;
8078 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8079
ce78b96d 8080 annotate_catchpoint (b->number);
f303dbd6 8081 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8082 if (b->disposition == disp_del)
112e8700 8083 uiout->text ("Temporary catchpoint ");
36dfb11c 8084 else
112e8700
SM
8085 uiout->text ("Catchpoint ");
8086 if (uiout->is_mi_like_p ())
36dfb11c 8087 {
112e8700
SM
8088 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8089 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8090 }
112e8700
SM
8091 uiout->field_int ("bkptno", b->number);
8092 uiout->text (" (forked process ");
8093 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8094 uiout->text ("), ");
ce78b96d
JB
8095 return PRINT_SRC_AND_LOC;
8096}
8097
4a64f543
MS
8098/* Implement the "print_one" breakpoint_ops method for fork
8099 catchpoints. */
ce78b96d
JB
8100
8101static void
a6d9a66e 8102print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8103{
e29a4733 8104 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8105 struct value_print_options opts;
79a45e25 8106 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8107
8108 get_user_print_options (&opts);
8109
4a64f543
MS
8110 /* Field 4, the address, is omitted (which makes the columns not
8111 line up too nicely with the headers, but the effect is relatively
8112 readable). */
79a45b7d 8113 if (opts.addressprint)
112e8700 8114 uiout->field_skip ("addr");
ce78b96d 8115 annotate_field (5);
112e8700 8116 uiout->text ("fork");
e29a4733 8117 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8118 {
112e8700
SM
8119 uiout->text (", process ");
8120 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8121 uiout->spaces (1);
ce78b96d 8122 }
8ac3646f 8123
112e8700
SM
8124 if (uiout->is_mi_like_p ())
8125 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8126}
8127
8128/* Implement the "print_mention" breakpoint_ops method for fork
8129 catchpoints. */
8130
8131static void
8132print_mention_catch_fork (struct breakpoint *b)
8133{
8134 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8135}
8136
6149aea9
PA
8137/* Implement the "print_recreate" breakpoint_ops method for fork
8138 catchpoints. */
8139
8140static void
8141print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8142{
8143 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8144 print_recreate_thread (b, fp);
6149aea9
PA
8145}
8146
ce78b96d
JB
8147/* The breakpoint_ops structure to be used in fork catchpoints. */
8148
2060206e 8149static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8150
4a64f543
MS
8151/* Implement the "insert" breakpoint_ops method for vfork
8152 catchpoints. */
ce78b96d 8153
77b06cd7
TJB
8154static int
8155insert_catch_vfork (struct bp_location *bl)
ce78b96d 8156{
dfd4cc63 8157 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8158}
8159
4a64f543
MS
8160/* Implement the "remove" breakpoint_ops method for vfork
8161 catchpoints. */
ce78b96d
JB
8162
8163static int
73971819 8164remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8165{
dfd4cc63 8166 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8167}
8168
8169/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8170 catchpoints. */
8171
8172static int
f1310107 8173breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8174 struct address_space *aspace, CORE_ADDR bp_addr,
8175 const struct target_waitstatus *ws)
ce78b96d 8176{
e29a4733
PA
8177 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8178
f90263c1
TT
8179 if (ws->kind != TARGET_WAITKIND_VFORKED)
8180 return 0;
8181
8182 c->forked_inferior_pid = ws->value.related_pid;
8183 return 1;
ce78b96d
JB
8184}
8185
4a64f543
MS
8186/* Implement the "print_it" breakpoint_ops method for vfork
8187 catchpoints. */
ce78b96d
JB
8188
8189static enum print_stop_action
348d480f 8190print_it_catch_vfork (bpstat bs)
ce78b96d 8191{
36dfb11c 8192 struct ui_out *uiout = current_uiout;
348d480f 8193 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8194 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8195
ce78b96d 8196 annotate_catchpoint (b->number);
f303dbd6 8197 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8198 if (b->disposition == disp_del)
112e8700 8199 uiout->text ("Temporary catchpoint ");
36dfb11c 8200 else
112e8700
SM
8201 uiout->text ("Catchpoint ");
8202 if (uiout->is_mi_like_p ())
36dfb11c 8203 {
112e8700
SM
8204 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8205 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8206 }
112e8700
SM
8207 uiout->field_int ("bkptno", b->number);
8208 uiout->text (" (vforked process ");
8209 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8210 uiout->text ("), ");
ce78b96d
JB
8211 return PRINT_SRC_AND_LOC;
8212}
8213
4a64f543
MS
8214/* Implement the "print_one" breakpoint_ops method for vfork
8215 catchpoints. */
ce78b96d
JB
8216
8217static void
a6d9a66e 8218print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8219{
e29a4733 8220 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8221 struct value_print_options opts;
79a45e25 8222 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8223
8224 get_user_print_options (&opts);
4a64f543
MS
8225 /* Field 4, the address, is omitted (which makes the columns not
8226 line up too nicely with the headers, but the effect is relatively
8227 readable). */
79a45b7d 8228 if (opts.addressprint)
112e8700 8229 uiout->field_skip ("addr");
ce78b96d 8230 annotate_field (5);
112e8700 8231 uiout->text ("vfork");
e29a4733 8232 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8233 {
112e8700
SM
8234 uiout->text (", process ");
8235 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8236 uiout->spaces (1);
ce78b96d 8237 }
8ac3646f 8238
112e8700
SM
8239 if (uiout->is_mi_like_p ())
8240 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8241}
8242
8243/* Implement the "print_mention" breakpoint_ops method for vfork
8244 catchpoints. */
8245
8246static void
8247print_mention_catch_vfork (struct breakpoint *b)
8248{
8249 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8250}
8251
6149aea9
PA
8252/* Implement the "print_recreate" breakpoint_ops method for vfork
8253 catchpoints. */
8254
8255static void
8256print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8257{
8258 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8259 print_recreate_thread (b, fp);
6149aea9
PA
8260}
8261
ce78b96d
JB
8262/* The breakpoint_ops structure to be used in vfork catchpoints. */
8263
2060206e 8264static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8265
edcc5120
TT
8266/* An instance of this type is used to represent an solib catchpoint.
8267 It includes a "struct breakpoint" as a kind of base class; users
8268 downcast to "struct breakpoint *" when needed. A breakpoint is
8269 really of this type iff its ops pointer points to
8270 CATCH_SOLIB_BREAKPOINT_OPS. */
8271
8272struct solib_catchpoint
8273{
8274 /* The base class. */
8275 struct breakpoint base;
8276
8277 /* True for "catch load", false for "catch unload". */
8278 unsigned char is_load;
8279
8280 /* Regular expression to match, if any. COMPILED is only valid when
8281 REGEX is non-NULL. */
8282 char *regex;
8283 regex_t compiled;
8284};
8285
8286static void
8287dtor_catch_solib (struct breakpoint *b)
8288{
8289 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8290
8291 if (self->regex)
8292 regfree (&self->compiled);
8293 xfree (self->regex);
8294
8295 base_breakpoint_ops.dtor (b);
8296}
8297
8298static int
8299insert_catch_solib (struct bp_location *ignore)
8300{
8301 return 0;
8302}
8303
8304static int
73971819 8305remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8306{
8307 return 0;
8308}
8309
8310static int
8311breakpoint_hit_catch_solib (const struct bp_location *bl,
8312 struct address_space *aspace,
8313 CORE_ADDR bp_addr,
8314 const struct target_waitstatus *ws)
8315{
8316 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8317 struct breakpoint *other;
8318
8319 if (ws->kind == TARGET_WAITKIND_LOADED)
8320 return 1;
8321
8322 ALL_BREAKPOINTS (other)
8323 {
8324 struct bp_location *other_bl;
8325
8326 if (other == bl->owner)
8327 continue;
8328
8329 if (other->type != bp_shlib_event)
8330 continue;
8331
8332 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8333 continue;
8334
8335 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8336 {
8337 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8338 return 1;
8339 }
8340 }
8341
8342 return 0;
8343}
8344
8345static void
8346check_status_catch_solib (struct bpstats *bs)
8347{
8348 struct solib_catchpoint *self
8349 = (struct solib_catchpoint *) bs->breakpoint_at;
8350 int ix;
8351
8352 if (self->is_load)
8353 {
8354 struct so_list *iter;
8355
8356 for (ix = 0;
8357 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8358 ix, iter);
8359 ++ix)
8360 {
8361 if (!self->regex
8362 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8363 return;
8364 }
8365 }
8366 else
8367 {
8368 char *iter;
8369
8370 for (ix = 0;
8371 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8372 ix, iter);
8373 ++ix)
8374 {
8375 if (!self->regex
8376 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8377 return;
8378 }
8379 }
8380
8381 bs->stop = 0;
8382 bs->print_it = print_it_noop;
8383}
8384
8385static enum print_stop_action
8386print_it_catch_solib (bpstat bs)
8387{
8388 struct breakpoint *b = bs->breakpoint_at;
8389 struct ui_out *uiout = current_uiout;
8390
8391 annotate_catchpoint (b->number);
f303dbd6 8392 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8393 if (b->disposition == disp_del)
112e8700 8394 uiout->text ("Temporary catchpoint ");
edcc5120 8395 else
112e8700
SM
8396 uiout->text ("Catchpoint ");
8397 uiout->field_int ("bkptno", b->number);
8398 uiout->text ("\n");
8399 if (uiout->is_mi_like_p ())
8400 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8401 print_solib_event (1);
8402 return PRINT_SRC_AND_LOC;
8403}
8404
8405static void
8406print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8407{
8408 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8409 struct value_print_options opts;
8410 struct ui_out *uiout = current_uiout;
8411 char *msg;
8412
8413 get_user_print_options (&opts);
8414 /* Field 4, the address, is omitted (which makes the columns not
8415 line up too nicely with the headers, but the effect is relatively
8416 readable). */
8417 if (opts.addressprint)
8418 {
8419 annotate_field (4);
112e8700 8420 uiout->field_skip ("addr");
edcc5120
TT
8421 }
8422
8423 annotate_field (5);
8424 if (self->is_load)
8425 {
8426 if (self->regex)
8427 msg = xstrprintf (_("load of library matching %s"), self->regex);
8428 else
8429 msg = xstrdup (_("load of library"));
8430 }
8431 else
8432 {
8433 if (self->regex)
8434 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8435 else
8436 msg = xstrdup (_("unload of library"));
8437 }
112e8700 8438 uiout->field_string ("what", msg);
edcc5120 8439 xfree (msg);
8ac3646f 8440
112e8700
SM
8441 if (uiout->is_mi_like_p ())
8442 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8443}
8444
8445static void
8446print_mention_catch_solib (struct breakpoint *b)
8447{
8448 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8449
8450 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8451 self->is_load ? "load" : "unload");
8452}
8453
8454static void
8455print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8456{
8457 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8458
8459 fprintf_unfiltered (fp, "%s %s",
8460 b->disposition == disp_del ? "tcatch" : "catch",
8461 self->is_load ? "load" : "unload");
8462 if (self->regex)
8463 fprintf_unfiltered (fp, " %s", self->regex);
8464 fprintf_unfiltered (fp, "\n");
8465}
8466
8467static struct breakpoint_ops catch_solib_breakpoint_ops;
8468
91985142
MG
8469/* Shared helper function (MI and CLI) for creating and installing
8470 a shared object event catchpoint. If IS_LOAD is non-zero then
8471 the events to be caught are load events, otherwise they are
8472 unload events. If IS_TEMP is non-zero the catchpoint is a
8473 temporary one. If ENABLED is non-zero the catchpoint is
8474 created in an enabled state. */
edcc5120 8475
91985142 8476void
a121b7c1 8477add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8478{
8479 struct solib_catchpoint *c;
8480 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8481 struct cleanup *cleanup;
8482
edcc5120
TT
8483 if (!arg)
8484 arg = "";
a121b7c1 8485 arg = skip_spaces_const (arg);
edcc5120 8486
4d01a485 8487 c = new solib_catchpoint ();
edcc5120
TT
8488 cleanup = make_cleanup (xfree, c);
8489
8490 if (*arg != '\0')
8491 {
8492 int errcode;
8493
8494 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8495 if (errcode != 0)
8496 {
8497 char *err = get_regcomp_error (errcode, &c->compiled);
8498
8499 make_cleanup (xfree, err);
8500 error (_("Invalid regexp (%s): %s"), err, arg);
8501 }
8502 c->regex = xstrdup (arg);
8503 }
8504
8505 c->is_load = is_load;
91985142 8506 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8507 &catch_solib_breakpoint_ops);
8508
91985142
MG
8509 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8510
edcc5120
TT
8511 discard_cleanups (cleanup);
8512 install_breakpoint (0, &c->base, 1);
8513}
8514
91985142
MG
8515/* A helper function that does all the work for "catch load" and
8516 "catch unload". */
8517
8518static void
8519catch_load_or_unload (char *arg, int from_tty, int is_load,
8520 struct cmd_list_element *command)
8521{
8522 int tempflag;
8523 const int enabled = 1;
8524
8525 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8526
8527 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8528}
8529
edcc5120
TT
8530static void
8531catch_load_command_1 (char *arg, int from_tty,
8532 struct cmd_list_element *command)
8533{
8534 catch_load_or_unload (arg, from_tty, 1, command);
8535}
8536
8537static void
8538catch_unload_command_1 (char *arg, int from_tty,
8539 struct cmd_list_element *command)
8540{
8541 catch_load_or_unload (arg, from_tty, 0, command);
8542}
8543
346774a9
PA
8544/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8545 is non-zero, then make the breakpoint temporary. If COND_STRING is
8546 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8547 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8548
ab04a2af 8549void
346774a9
PA
8550init_catchpoint (struct breakpoint *b,
8551 struct gdbarch *gdbarch, int tempflag,
63160a43 8552 const char *cond_string,
c0a91b2b 8553 const struct breakpoint_ops *ops)
c906108c 8554{
c5aa993b 8555 struct symtab_and_line sal;
346774a9 8556
fe39c653 8557 init_sal (&sal);
6c95b8df 8558 sal.pspace = current_program_space;
c5aa993b 8559
28010a5d 8560 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8561
1b36a34b 8562 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8563 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8564}
8565
28010a5d 8566void
3ea46bff 8567install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8568{
8569 add_to_breakpoint_chain (b);
3a5c3e22 8570 set_breakpoint_number (internal, b);
558a9d82
YQ
8571 if (is_tracepoint (b))
8572 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8573 if (!internal)
8574 mention (b);
c56053d2 8575 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8576
8577 if (update_gll)
44702360 8578 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8579}
8580
9b70b993 8581static void
a6d9a66e 8582create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8583 int tempflag, const char *cond_string,
c0a91b2b 8584 const struct breakpoint_ops *ops)
c906108c 8585{
4d01a485 8586 struct fork_catchpoint *c = new fork_catchpoint ();
ce78b96d 8587
e29a4733
PA
8588 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8589
8590 c->forked_inferior_pid = null_ptid;
8591
3ea46bff 8592 install_breakpoint (0, &c->base, 1);
c906108c
SS
8593}
8594
fe798b75
JB
8595/* Exec catchpoints. */
8596
b4d90040
PA
8597/* An instance of this type is used to represent an exec catchpoint.
8598 It includes a "struct breakpoint" as a kind of base class; users
8599 downcast to "struct breakpoint *" when needed. A breakpoint is
8600 really of this type iff its ops pointer points to
8601 CATCH_EXEC_BREAKPOINT_OPS. */
8602
8603struct exec_catchpoint
8604{
8605 /* The base class. */
8606 struct breakpoint base;
8607
8608 /* Filename of a program whose exec triggered this catchpoint.
8609 This field is only valid immediately after this catchpoint has
8610 triggered. */
8611 char *exec_pathname;
8612};
8613
8614/* Implement the "dtor" breakpoint_ops method for exec
8615 catchpoints. */
8616
8617static void
8618dtor_catch_exec (struct breakpoint *b)
8619{
8620 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8621
8622 xfree (c->exec_pathname);
348d480f 8623
2060206e 8624 base_breakpoint_ops.dtor (b);
b4d90040
PA
8625}
8626
77b06cd7
TJB
8627static int
8628insert_catch_exec (struct bp_location *bl)
c906108c 8629{
dfd4cc63 8630 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8631}
c906108c 8632
fe798b75 8633static int
73971819 8634remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8635{
dfd4cc63 8636 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8637}
c906108c 8638
fe798b75 8639static int
f1310107 8640breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8641 struct address_space *aspace, CORE_ADDR bp_addr,
8642 const struct target_waitstatus *ws)
fe798b75 8643{
b4d90040
PA
8644 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8645
f90263c1
TT
8646 if (ws->kind != TARGET_WAITKIND_EXECD)
8647 return 0;
8648
8649 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8650 return 1;
fe798b75 8651}
c906108c 8652
fe798b75 8653static enum print_stop_action
348d480f 8654print_it_catch_exec (bpstat bs)
fe798b75 8655{
36dfb11c 8656 struct ui_out *uiout = current_uiout;
348d480f 8657 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8658 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8659
fe798b75 8660 annotate_catchpoint (b->number);
f303dbd6 8661 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8662 if (b->disposition == disp_del)
112e8700 8663 uiout->text ("Temporary catchpoint ");
36dfb11c 8664 else
112e8700
SM
8665 uiout->text ("Catchpoint ");
8666 if (uiout->is_mi_like_p ())
36dfb11c 8667 {
112e8700
SM
8668 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8669 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8670 }
112e8700
SM
8671 uiout->field_int ("bkptno", b->number);
8672 uiout->text (" (exec'd ");
8673 uiout->field_string ("new-exec", c->exec_pathname);
8674 uiout->text ("), ");
36dfb11c 8675
fe798b75 8676 return PRINT_SRC_AND_LOC;
c906108c
SS
8677}
8678
fe798b75 8679static void
a6d9a66e 8680print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8681{
b4d90040 8682 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8683 struct value_print_options opts;
79a45e25 8684 struct ui_out *uiout = current_uiout;
fe798b75
JB
8685
8686 get_user_print_options (&opts);
8687
8688 /* Field 4, the address, is omitted (which makes the columns
8689 not line up too nicely with the headers, but the effect
8690 is relatively readable). */
8691 if (opts.addressprint)
112e8700 8692 uiout->field_skip ("addr");
fe798b75 8693 annotate_field (5);
112e8700 8694 uiout->text ("exec");
b4d90040 8695 if (c->exec_pathname != NULL)
fe798b75 8696 {
112e8700
SM
8697 uiout->text (", program \"");
8698 uiout->field_string ("what", c->exec_pathname);
8699 uiout->text ("\" ");
fe798b75 8700 }
8ac3646f 8701
112e8700
SM
8702 if (uiout->is_mi_like_p ())
8703 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8704}
8705
8706static void
8707print_mention_catch_exec (struct breakpoint *b)
8708{
8709 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8710}
8711
6149aea9
PA
8712/* Implement the "print_recreate" breakpoint_ops method for exec
8713 catchpoints. */
8714
8715static void
8716print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8717{
8718 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8719 print_recreate_thread (b, fp);
6149aea9
PA
8720}
8721
2060206e 8722static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8723
c906108c 8724static int
fba45db2 8725hw_breakpoint_used_count (void)
c906108c 8726{
c906108c 8727 int i = 0;
f1310107
TJB
8728 struct breakpoint *b;
8729 struct bp_location *bl;
c906108c
SS
8730
8731 ALL_BREAKPOINTS (b)
c5aa993b 8732 {
d6b74ac4 8733 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8734 for (bl = b->loc; bl; bl = bl->next)
8735 {
8736 /* Special types of hardware breakpoints may use more than
8737 one register. */
348d480f 8738 i += b->ops->resources_needed (bl);
f1310107 8739 }
c5aa993b 8740 }
c906108c
SS
8741
8742 return i;
8743}
8744
a1398e0c
PA
8745/* Returns the resources B would use if it were a hardware
8746 watchpoint. */
8747
c906108c 8748static int
a1398e0c 8749hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8750{
c906108c 8751 int i = 0;
e09342b5 8752 struct bp_location *bl;
c906108c 8753
a1398e0c
PA
8754 if (!breakpoint_enabled (b))
8755 return 0;
8756
8757 for (bl = b->loc; bl; bl = bl->next)
8758 {
8759 /* Special types of hardware watchpoints may use more than
8760 one register. */
8761 i += b->ops->resources_needed (bl);
8762 }
8763
8764 return i;
8765}
8766
8767/* Returns the sum the used resources of all hardware watchpoints of
8768 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8769 the sum of the used resources of all hardware watchpoints of other
8770 types _not_ TYPE. */
8771
8772static int
8773hw_watchpoint_used_count_others (struct breakpoint *except,
8774 enum bptype type, int *other_type_used)
8775{
8776 int i = 0;
8777 struct breakpoint *b;
8778
c906108c
SS
8779 *other_type_used = 0;
8780 ALL_BREAKPOINTS (b)
e09342b5 8781 {
a1398e0c
PA
8782 if (b == except)
8783 continue;
e09342b5
TJB
8784 if (!breakpoint_enabled (b))
8785 continue;
8786
a1398e0c
PA
8787 if (b->type == type)
8788 i += hw_watchpoint_use_count (b);
8789 else if (is_hardware_watchpoint (b))
8790 *other_type_used = 1;
e09342b5
TJB
8791 }
8792
c906108c
SS
8793 return i;
8794}
8795
c906108c 8796void
fba45db2 8797disable_watchpoints_before_interactive_call_start (void)
c906108c 8798{
c5aa993b 8799 struct breakpoint *b;
c906108c
SS
8800
8801 ALL_BREAKPOINTS (b)
c5aa993b 8802 {
cc60f2e3 8803 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8804 {
b5de0fa7 8805 b->enable_state = bp_call_disabled;
44702360 8806 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8807 }
8808 }
c906108c
SS
8809}
8810
8811void
fba45db2 8812enable_watchpoints_after_interactive_call_stop (void)
c906108c 8813{
c5aa993b 8814 struct breakpoint *b;
c906108c
SS
8815
8816 ALL_BREAKPOINTS (b)
c5aa993b 8817 {
cc60f2e3 8818 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8819 {
b5de0fa7 8820 b->enable_state = bp_enabled;
44702360 8821 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8822 }
8823 }
c906108c
SS
8824}
8825
8bea4e01
UW
8826void
8827disable_breakpoints_before_startup (void)
8828{
6c95b8df 8829 current_program_space->executing_startup = 1;
44702360 8830 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8831}
8832
8833void
8834enable_breakpoints_after_startup (void)
8835{
6c95b8df 8836 current_program_space->executing_startup = 0;
f8eba3c6 8837 breakpoint_re_set ();
8bea4e01
UW
8838}
8839
7c16b83e
PA
8840/* Create a new single-step breakpoint for thread THREAD, with no
8841 locations. */
c906108c 8842
7c16b83e
PA
8843static struct breakpoint *
8844new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8845{
4d01a485 8846 struct breakpoint *b = new breakpoint ();
7c16b83e
PA
8847
8848 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8849 &momentary_breakpoint_ops);
8850
8851 b->disposition = disp_donttouch;
8852 b->frame_id = null_frame_id;
8853
8854 b->thread = thread;
8855 gdb_assert (b->thread != 0);
8856
8857 add_to_breakpoint_chain (b);
8858
8859 return b;
8860}
8861
8862/* Set a momentary breakpoint of type TYPE at address specified by
8863 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8864 frame. */
c906108c
SS
8865
8866struct breakpoint *
a6d9a66e
UW
8867set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8868 struct frame_id frame_id, enum bptype type)
c906108c 8869{
52f0bd74 8870 struct breakpoint *b;
edb3359d 8871
193facb3
JK
8872 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8873 tail-called one. */
8874 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8875
06edf0c0 8876 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8877 b->enable_state = bp_enabled;
8878 b->disposition = disp_donttouch;
818dd999 8879 b->frame_id = frame_id;
c906108c 8880
4a64f543
MS
8881 /* If we're debugging a multi-threaded program, then we want
8882 momentary breakpoints to be active in only a single thread of
8883 control. */
39f77062 8884 if (in_thread_list (inferior_ptid))
5d5658a1 8885 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8886
44702360 8887 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8888
c906108c
SS
8889 return b;
8890}
611c83ae 8891
06edf0c0 8892/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8893 The new breakpoint will have type TYPE, use OPS as its
8894 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8895
06edf0c0
PA
8896static struct breakpoint *
8897momentary_breakpoint_from_master (struct breakpoint *orig,
8898 enum bptype type,
a1aa2221
LM
8899 const struct breakpoint_ops *ops,
8900 int loc_enabled)
e58b0e63
PA
8901{
8902 struct breakpoint *copy;
8903
06edf0c0 8904 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8905 copy->loc = allocate_bp_location (copy);
0e30163f 8906 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8907
a6d9a66e 8908 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8909 copy->loc->requested_address = orig->loc->requested_address;
8910 copy->loc->address = orig->loc->address;
8911 copy->loc->section = orig->loc->section;
6c95b8df 8912 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8913 copy->loc->probe = orig->loc->probe;
f8eba3c6 8914 copy->loc->line_number = orig->loc->line_number;
2f202fde 8915 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8916 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8917 copy->frame_id = orig->frame_id;
8918 copy->thread = orig->thread;
6c95b8df 8919 copy->pspace = orig->pspace;
e58b0e63
PA
8920
8921 copy->enable_state = bp_enabled;
8922 copy->disposition = disp_donttouch;
8923 copy->number = internal_breakpoint_number--;
8924
44702360 8925 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8926 return copy;
8927}
8928
06edf0c0
PA
8929/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8930 ORIG is NULL. */
8931
8932struct breakpoint *
8933clone_momentary_breakpoint (struct breakpoint *orig)
8934{
8935 /* If there's nothing to clone, then return nothing. */
8936 if (orig == NULL)
8937 return NULL;
8938
a1aa2221 8939 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8940}
8941
611c83ae 8942struct breakpoint *
a6d9a66e
UW
8943set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8944 enum bptype type)
611c83ae
PA
8945{
8946 struct symtab_and_line sal;
8947
8948 sal = find_pc_line (pc, 0);
8949 sal.pc = pc;
8950 sal.section = find_pc_overlay (pc);
8951 sal.explicit_pc = 1;
8952
a6d9a66e 8953 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8954}
c906108c 8955\f
c5aa993b 8956
c906108c
SS
8957/* Tell the user we have just set a breakpoint B. */
8958
8959static void
fba45db2 8960mention (struct breakpoint *b)
c906108c 8961{
348d480f 8962 b->ops->print_mention (b);
112e8700 8963 if (current_uiout->is_mi_like_p ())
fb40c209 8964 return;
c906108c
SS
8965 printf_filtered ("\n");
8966}
c906108c 8967\f
c5aa993b 8968
1a853c52
PA
8969static int bp_loc_is_permanent (struct bp_location *loc);
8970
0d381245 8971static struct bp_location *
39d61571 8972add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8973 const struct symtab_and_line *sal)
8974{
8975 struct bp_location *loc, **tmp;
3742cc8b
YQ
8976 CORE_ADDR adjusted_address;
8977 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8978
8979 if (loc_gdbarch == NULL)
8980 loc_gdbarch = b->gdbarch;
8981
8982 /* Adjust the breakpoint's address prior to allocating a location.
8983 Once we call allocate_bp_location(), that mostly uninitialized
8984 location will be placed on the location chain. Adjustment of the
8985 breakpoint may cause target_read_memory() to be called and we do
8986 not want its scan of the location chain to find a breakpoint and
8987 location that's only been partially initialized. */
8988 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8989 sal->pc, b->type);
0d381245 8990
d30113d4 8991 /* Sort the locations by their ADDRESS. */
39d61571 8992 loc = allocate_bp_location (b);
d30113d4
JK
8993 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8994 tmp = &((*tmp)->next))
0d381245 8995 ;
d30113d4 8996 loc->next = *tmp;
0d381245 8997 *tmp = loc;
3742cc8b 8998
0d381245 8999 loc->requested_address = sal->pc;
3742cc8b 9000 loc->address = adjusted_address;
6c95b8df 9001 loc->pspace = sal->pspace;
729662a5
TT
9002 loc->probe.probe = sal->probe;
9003 loc->probe.objfile = sal->objfile;
6c95b8df 9004 gdb_assert (loc->pspace != NULL);
0d381245 9005 loc->section = sal->section;
3742cc8b 9006 loc->gdbarch = loc_gdbarch;
f8eba3c6 9007 loc->line_number = sal->line;
2f202fde 9008 loc->symtab = sal->symtab;
f8eba3c6 9009
0e30163f
JK
9010 set_breakpoint_location_function (loc,
9011 sal->explicit_pc || sal->explicit_line);
1a853c52 9012
6ae88661
LM
9013 /* While by definition, permanent breakpoints are already present in the
9014 code, we don't mark the location as inserted. Normally one would expect
9015 that GDB could rely on that breakpoint instruction to stop the program,
9016 thus removing the need to insert its own breakpoint, except that executing
9017 the breakpoint instruction can kill the target instead of reporting a
9018 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9019 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9020 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9021 breakpoint be inserted normally results in QEMU knowing about the GDB
9022 breakpoint, and thus trap before the breakpoint instruction is executed.
9023 (If GDB later needs to continue execution past the permanent breakpoint,
9024 it manually increments the PC, thus avoiding executing the breakpoint
9025 instruction.) */
1a853c52 9026 if (bp_loc_is_permanent (loc))
6ae88661 9027 loc->permanent = 1;
1a853c52 9028
0d381245
VP
9029 return loc;
9030}
514f746b
AR
9031\f
9032
1cf4d951 9033/* See breakpoint.h. */
514f746b 9034
1cf4d951
PA
9035int
9036program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9037{
9038 int len;
9039 CORE_ADDR addr;
1afeeb75 9040 const gdb_byte *bpoint;
514f746b 9041 gdb_byte *target_mem;
939c61fa
JK
9042 struct cleanup *cleanup;
9043 int retval = 0;
514f746b 9044
1cf4d951
PA
9045 addr = address;
9046 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9047
9048 /* Software breakpoints unsupported? */
9049 if (bpoint == NULL)
9050 return 0;
9051
224c3ddb 9052 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9053
9054 /* Enable the automatic memory restoration from breakpoints while
9055 we read the memory. Otherwise we could say about our temporary
9056 breakpoints they are permanent. */
9057 cleanup = make_show_memory_breakpoints_cleanup (0);
9058
9059 if (target_read_memory (address, target_mem, len) == 0
9060 && memcmp (target_mem, bpoint, len) == 0)
9061 retval = 1;
9062
9063 do_cleanups (cleanup);
9064
9065 return retval;
9066}
9067
9068/* Return 1 if LOC is pointing to a permanent breakpoint,
9069 return 0 otherwise. */
9070
9071static int
9072bp_loc_is_permanent (struct bp_location *loc)
9073{
514f746b
AR
9074 gdb_assert (loc != NULL);
9075
244558af
LM
9076 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9077 attempt to read from the addresses the locations of these breakpoint types
9078 point to. program_breakpoint_here_p, below, will attempt to read
9079 memory. */
9080 if (!breakpoint_address_is_meaningful (loc->owner))
9081 return 0;
9082
5ed8105e 9083 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9084 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 9085 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
9086}
9087
e7e0cddf
SS
9088/* Build a command list for the dprintf corresponding to the current
9089 settings of the dprintf style options. */
9090
9091static void
9092update_dprintf_command_list (struct breakpoint *b)
9093{
9094 char *dprintf_args = b->extra_string;
9095 char *printf_line = NULL;
9096
9097 if (!dprintf_args)
9098 return;
9099
9100 dprintf_args = skip_spaces (dprintf_args);
9101
9102 /* Allow a comma, as it may have terminated a location, but don't
9103 insist on it. */
9104 if (*dprintf_args == ',')
9105 ++dprintf_args;
9106 dprintf_args = skip_spaces (dprintf_args);
9107
9108 if (*dprintf_args != '"')
9109 error (_("Bad format string, missing '\"'."));
9110
d3ce09f5 9111 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9112 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9113 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9114 {
9115 if (!dprintf_function)
9116 error (_("No function supplied for dprintf call"));
9117
9118 if (dprintf_channel && strlen (dprintf_channel) > 0)
9119 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9120 dprintf_function,
9121 dprintf_channel,
9122 dprintf_args);
9123 else
9124 printf_line = xstrprintf ("call (void) %s (%s)",
9125 dprintf_function,
9126 dprintf_args);
9127 }
d3ce09f5
SS
9128 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9129 {
9130 if (target_can_run_breakpoint_commands ())
9131 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9132 else
9133 {
9134 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9135 printf_line = xstrprintf ("printf %s", dprintf_args);
9136 }
9137 }
e7e0cddf
SS
9138 else
9139 internal_error (__FILE__, __LINE__,
9140 _("Invalid dprintf style."));
9141
f28045c2 9142 gdb_assert (printf_line != NULL);
9d6e6e84 9143 /* Manufacture a printf sequence. */
f28045c2 9144 {
8d749320 9145 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9146
f28045c2
YQ
9147 printf_cmd_line->control_type = simple_control;
9148 printf_cmd_line->body_count = 0;
9149 printf_cmd_line->body_list = NULL;
9d6e6e84 9150 printf_cmd_line->next = NULL;
f28045c2 9151 printf_cmd_line->line = printf_line;
e7e0cddf 9152
93921405 9153 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9154 }
e7e0cddf
SS
9155}
9156
9157/* Update all dprintf commands, making their command lists reflect
9158 current style settings. */
9159
9160static void
9161update_dprintf_commands (char *args, int from_tty,
9162 struct cmd_list_element *c)
9163{
9164 struct breakpoint *b;
9165
9166 ALL_BREAKPOINTS (b)
9167 {
9168 if (b->type == bp_dprintf)
9169 update_dprintf_command_list (b);
9170 }
9171}
c3f6f71d 9172
f00aae0f
KS
9173/* Create a breakpoint with SAL as location. Use LOCATION
9174 as a description of the location, and COND_STRING
b35a8b2f
DE
9175 as condition expression. If LOCATION is NULL then create an
9176 "address location" from the address in the SAL. */
018d34a4
VP
9177
9178static void
d9b3f62e 9179init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9180 struct symtabs_and_lines sals,
ffc2605c 9181 event_location_up &&location,
f8eba3c6 9182 char *filter, char *cond_string,
e7e0cddf 9183 char *extra_string,
d9b3f62e
PA
9184 enum bptype type, enum bpdisp disposition,
9185 int thread, int task, int ignore_count,
c0a91b2b 9186 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9187 int enabled, int internal, unsigned flags,
9188 int display_canonical)
018d34a4 9189{
0d381245 9190 int i;
018d34a4
VP
9191
9192 if (type == bp_hardware_breakpoint)
9193 {
fbbd034e
AS
9194 int target_resources_ok;
9195
9196 i = hw_breakpoint_used_count ();
9197 target_resources_ok =
9198 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9199 i + 1, 0);
9200 if (target_resources_ok == 0)
9201 error (_("No hardware breakpoint support in the target."));
9202 else if (target_resources_ok < 0)
9203 error (_("Hardware breakpoints used exceeds limit."));
9204 }
9205
6c95b8df
PA
9206 gdb_assert (sals.nelts > 0);
9207
0d381245
VP
9208 for (i = 0; i < sals.nelts; ++i)
9209 {
9210 struct symtab_and_line sal = sals.sals[i];
9211 struct bp_location *loc;
9212
9213 if (from_tty)
5af949e3
UW
9214 {
9215 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9216 if (!loc_gdbarch)
9217 loc_gdbarch = gdbarch;
9218
9219 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9220 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9221 }
0d381245
VP
9222
9223 if (i == 0)
9224 {
d9b3f62e 9225 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9226 b->thread = thread;
4a306c9a 9227 b->task = task;
855a6e68 9228
0d381245 9229 b->cond_string = cond_string;
e7e0cddf 9230 b->extra_string = extra_string;
0d381245 9231 b->ignore_count = ignore_count;
41447f92 9232 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9233 b->disposition = disposition;
6c95b8df 9234
44f238bb
PA
9235 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9236 b->loc->inserted = 1;
9237
0fb4aa4b
PA
9238 if (type == bp_static_tracepoint)
9239 {
d9b3f62e 9240 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9241 struct static_tracepoint_marker marker;
9242
983af33b 9243 if (strace_marker_p (b))
0fb4aa4b
PA
9244 {
9245 /* We already know the marker exists, otherwise, we
9246 wouldn't see a sal for it. */
d28cd78a
TT
9247 const char *p
9248 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9249 const char *endp;
0fb4aa4b 9250 char *marker_str;
0fb4aa4b 9251
f00aae0f 9252 p = skip_spaces_const (p);
0fb4aa4b 9253
f00aae0f 9254 endp = skip_to_space_const (p);
0fb4aa4b
PA
9255
9256 marker_str = savestring (p, endp - p);
d9b3f62e 9257 t->static_trace_marker_id = marker_str;
0fb4aa4b 9258
3e43a32a
MS
9259 printf_filtered (_("Probed static tracepoint "
9260 "marker \"%s\"\n"),
d9b3f62e 9261 t->static_trace_marker_id);
0fb4aa4b
PA
9262 }
9263 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9264 {
d9b3f62e 9265 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9266 release_static_tracepoint_marker (&marker);
9267
3e43a32a
MS
9268 printf_filtered (_("Probed static tracepoint "
9269 "marker \"%s\"\n"),
d9b3f62e 9270 t->static_trace_marker_id);
0fb4aa4b
PA
9271 }
9272 else
3e43a32a
MS
9273 warning (_("Couldn't determine the static "
9274 "tracepoint marker to probe"));
0fb4aa4b
PA
9275 }
9276
0d381245
VP
9277 loc = b->loc;
9278 }
9279 else
018d34a4 9280 {
39d61571 9281 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9282 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9283 loc->inserted = 1;
0d381245
VP
9284 }
9285
9286 if (b->cond_string)
9287 {
bbc13ae3
KS
9288 const char *arg = b->cond_string;
9289
1bb9788d
TT
9290 loc->cond = parse_exp_1 (&arg, loc->address,
9291 block_for_pc (loc->address), 0);
0d381245 9292 if (*arg)
588ae58c 9293 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9294 }
e7e0cddf
SS
9295
9296 /* Dynamic printf requires and uses additional arguments on the
9297 command line, otherwise it's an error. */
9298 if (type == bp_dprintf)
9299 {
9300 if (b->extra_string)
9301 update_dprintf_command_list (b);
9302 else
9303 error (_("Format string required"));
9304 }
9305 else if (b->extra_string)
588ae58c 9306 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9307 }
018d34a4 9308
56435ebe 9309 b->display_canonical = display_canonical;
f00aae0f 9310 if (location != NULL)
d28cd78a 9311 b->location = std::move (location);
018d34a4 9312 else
d28cd78a 9313 b->location = new_address_location (b->loc->address, NULL, 0);
f8eba3c6 9314 b->filter = filter;
d9b3f62e 9315}
018d34a4 9316
d9b3f62e
PA
9317static void
9318create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9319 struct symtabs_and_lines sals,
ffc2605c 9320 event_location_up &&location,
f8eba3c6 9321 char *filter, char *cond_string,
e7e0cddf 9322 char *extra_string,
d9b3f62e
PA
9323 enum bptype type, enum bpdisp disposition,
9324 int thread, int task, int ignore_count,
c0a91b2b 9325 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9326 int enabled, int internal, unsigned flags,
9327 int display_canonical)
d9b3f62e 9328{
a5e364af 9329 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9330
a5e364af 9331 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9332 sals, std::move (location),
e7e0cddf 9333 filter, cond_string, extra_string,
d9b3f62e
PA
9334 type, disposition,
9335 thread, task, ignore_count,
9336 ops, from_tty,
44f238bb
PA
9337 enabled, internal, flags,
9338 display_canonical);
d9b3f62e 9339
a5e364af 9340 install_breakpoint (internal, b.release (), 0);
018d34a4
VP
9341}
9342
9343/* Add SALS.nelts breakpoints to the breakpoint table. For each
9344 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9345 value. COND_STRING, if not NULL, specified the condition to be
9346 used for all breakpoints. Essentially the only case where
9347 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9348 function. In that case, it's still not possible to specify
9349 separate conditions for different overloaded functions, so
9350 we take just a single condition string.
9351
c3f6f71d 9352 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9353 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9354 array contents). If the function fails (error() is called), the
9355 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9356 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9357
9358static void
8cdf0e15 9359create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9360 struct linespec_result *canonical,
e7e0cddf 9361 char *cond_string, char *extra_string,
8cdf0e15
VP
9362 enum bptype type, enum bpdisp disposition,
9363 int thread, int task, int ignore_count,
c0a91b2b 9364 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9365 int enabled, int internal, unsigned flags)
c906108c 9366{
018d34a4 9367 int i;
f8eba3c6 9368 struct linespec_sals *lsal;
cc59ec59 9369
f8eba3c6
TT
9370 if (canonical->pre_expanded)
9371 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9372
9373 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9374 {
f00aae0f 9375 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9376 'break', without arguments. */
ffc2605c 9377 event_location_up location
f00aae0f 9378 = (canonical->location != NULL
8e9e35b1 9379 ? copy_event_location (canonical->location.get ()) : NULL);
f8eba3c6 9380 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
0d381245 9381
f8eba3c6
TT
9382 make_cleanup (xfree, filter_string);
9383 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9384 std::move (location),
f8eba3c6 9385 filter_string,
e7e0cddf
SS
9386 cond_string, extra_string,
9387 type, disposition,
84f4c1fe 9388 thread, task, ignore_count, ops,
44f238bb 9389 from_tty, enabled, internal, flags,
56435ebe 9390 canonical->special_display);
c3f6f71d 9391 }
c3f6f71d 9392}
c906108c 9393
f00aae0f 9394/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9395 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9396 addresses found. LOCATION points to the end of the SAL (for
9397 linespec locations).
9998af43
TJB
9398
9399 The array and the line spec strings are allocated on the heap, it is
9400 the caller's responsibility to free them. */
c906108c 9401
b9362cc7 9402static void
f00aae0f 9403parse_breakpoint_sals (const struct event_location *location,
58438ac1 9404 struct linespec_result *canonical)
c3f6f71d 9405{
f00aae0f
KS
9406 struct symtab_and_line cursal;
9407
9408 if (event_location_type (location) == LINESPEC_LOCATION)
9409 {
9410 const char *address = get_linespec_location (location);
9411
9412 if (address == NULL)
9413 {
9414 /* The last displayed codepoint, if it's valid, is our default
9415 breakpoint address. */
9416 if (last_displayed_sal_is_valid ())
9417 {
9418 struct linespec_sals lsal;
9419 struct symtab_and_line sal;
9420 CORE_ADDR pc;
9421
9422 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9423 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9424
9425 /* Set sal's pspace, pc, symtab, and line to the values
9426 corresponding to the last call to print_frame_info.
9427 Be sure to reinitialize LINE with NOTCURRENT == 0
9428 as the breakpoint line number is inappropriate otherwise.
9429 find_pc_line would adjust PC, re-set it back. */
9430 get_last_displayed_sal (&sal);
9431 pc = sal.pc;
9432 sal = find_pc_line (pc, 0);
9433
9434 /* "break" without arguments is equivalent to "break *PC"
9435 where PC is the last displayed codepoint's address. So
9436 make sure to set sal.explicit_pc to prevent GDB from
9437 trying to expand the list of sals to include all other
9438 instances with the same symtab and line. */
9439 sal.pc = pc;
9440 sal.explicit_pc = 1;
9441
9442 lsal.sals.sals[0] = sal;
9443 lsal.sals.nelts = 1;
9444 lsal.canonical = NULL;
9445
9446 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9447 return;
9448 }
9449 else
9450 error (_("No default breakpoint address now."));
c906108c 9451 }
c906108c 9452 }
f00aae0f
KS
9453
9454 /* Force almost all breakpoints to be in terms of the
9455 current_source_symtab (which is decode_line_1's default).
9456 This should produce the results we want almost all of the
9457 time while leaving default_breakpoint_* alone.
9458
9459 ObjC: However, don't match an Objective-C method name which
9460 may have a '+' or '-' succeeded by a '['. */
9461 cursal = get_current_source_symtab_and_line ();
9462 if (last_displayed_sal_is_valid ())
c906108c 9463 {
f00aae0f 9464 const char *address = NULL;
cc80f267 9465
f00aae0f
KS
9466 if (event_location_type (location) == LINESPEC_LOCATION)
9467 address = get_linespec_location (location);
cc80f267 9468
f00aae0f
KS
9469 if (!cursal.symtab
9470 || (address != NULL
9471 && strchr ("+-", address[0]) != NULL
9472 && address[1] != '['))
9473 {
c2f4122d 9474 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9475 get_last_displayed_symtab (),
9476 get_last_displayed_line (),
9477 canonical, NULL, NULL);
9478 return;
9479 }
c906108c 9480 }
f00aae0f 9481
c2f4122d 9482 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9483 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9484}
c906108c 9485
c906108c 9486
c3f6f71d 9487/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9488 inserted as a breakpoint. If it can't throw an error. */
c906108c 9489
b9362cc7 9490static void
23e7acfb 9491breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9492{
9493 int i;
cc59ec59 9494
c3f6f71d 9495 for (i = 0; i < sals->nelts; i++)
ee53e872 9496 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9497}
9498
7a697b8d
SS
9499/* Fast tracepoints may have restrictions on valid locations. For
9500 instance, a fast tracepoint using a jump instead of a trap will
9501 likely have to overwrite more bytes than a trap would, and so can
9502 only be placed where the instruction is longer than the jump, or a
9503 multi-instruction sequence does not have a jump into the middle of
9504 it, etc. */
9505
9506static void
9507check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9508 struct symtabs_and_lines *sals)
9509{
9510 int i, rslt;
9511 struct symtab_and_line *sal;
9512 char *msg;
9513 struct cleanup *old_chain;
9514
9515 for (i = 0; i < sals->nelts; i++)
9516 {
f8eba3c6
TT
9517 struct gdbarch *sarch;
9518
7a697b8d
SS
9519 sal = &sals->sals[i];
9520
f8eba3c6
TT
9521 sarch = get_sal_arch (*sal);
9522 /* We fall back to GDBARCH if there is no architecture
9523 associated with SAL. */
9524 if (sarch == NULL)
9525 sarch = gdbarch;
6b940e6a 9526 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9527 old_chain = make_cleanup (xfree, msg);
9528
9529 if (!rslt)
53c3572a 9530 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9531 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9532
9533 do_cleanups (old_chain);
9534 }
9535}
9536
018d34a4
VP
9537/* Given TOK, a string specification of condition and thread, as
9538 accepted by the 'break' command, extract the condition
9539 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9540 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9541 If no condition is found, *COND_STRING is set to NULL.
9542 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9543
9544static void
bbc13ae3 9545find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9546 char **cond_string, int *thread, int *task,
9547 char **rest)
018d34a4
VP
9548{
9549 *cond_string = NULL;
9550 *thread = -1;
ed1d1739
KS
9551 *task = 0;
9552 *rest = NULL;
9553
018d34a4
VP
9554 while (tok && *tok)
9555 {
bbc13ae3 9556 const char *end_tok;
018d34a4 9557 int toklen;
bbc13ae3
KS
9558 const char *cond_start = NULL;
9559 const char *cond_end = NULL;
cc59ec59 9560
bbc13ae3 9561 tok = skip_spaces_const (tok);
e7e0cddf
SS
9562
9563 if ((*tok == '"' || *tok == ',') && rest)
9564 {
9565 *rest = savestring (tok, strlen (tok));
9566 return;
9567 }
9568
bbc13ae3 9569 end_tok = skip_to_space_const (tok);
d634f2de 9570
018d34a4 9571 toklen = end_tok - tok;
d634f2de 9572
018d34a4
VP
9573 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9574 {
9575 tok = cond_start = end_tok + 1;
4d01a485 9576 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9577 cond_end = tok;
d634f2de 9578 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9579 }
9580 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9581 {
5d5658a1
PA
9582 const char *tmptok;
9583 struct thread_info *thr;
d634f2de 9584
018d34a4 9585 tok = end_tok + 1;
5d5658a1 9586 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9587 if (tok == tmptok)
9588 error (_("Junk after thread keyword."));
5d5658a1 9589 *thread = thr->global_num;
bbc13ae3 9590 tok = tmptok;
018d34a4 9591 }
4a306c9a
JB
9592 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9593 {
9594 char *tmptok;
9595
9596 tok = end_tok + 1;
bbc13ae3 9597 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9598 if (tok == tmptok)
9599 error (_("Junk after task keyword."));
9600 if (!valid_task_id (*task))
b6199126 9601 error (_("Unknown task %d."), *task);
bbc13ae3 9602 tok = tmptok;
4a306c9a 9603 }
e7e0cddf
SS
9604 else if (rest)
9605 {
9606 *rest = savestring (tok, strlen (tok));
ccab2054 9607 return;
e7e0cddf 9608 }
018d34a4
VP
9609 else
9610 error (_("Junk at end of arguments."));
9611 }
9612}
9613
0fb4aa4b
PA
9614/* Decode a static tracepoint marker spec. */
9615
9616static struct symtabs_and_lines
f00aae0f 9617decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9618{
9619 VEC(static_tracepoint_marker_p) *markers = NULL;
9620 struct symtabs_and_lines sals;
0fb4aa4b 9621 struct cleanup *old_chain;
f00aae0f
KS
9622 const char *p = &(*arg_p)[3];
9623 const char *endp;
0fb4aa4b
PA
9624 char *marker_str;
9625 int i;
9626
f00aae0f 9627 p = skip_spaces_const (p);
0fb4aa4b 9628
f00aae0f 9629 endp = skip_to_space_const (p);
0fb4aa4b
PA
9630
9631 marker_str = savestring (p, endp - p);
9632 old_chain = make_cleanup (xfree, marker_str);
9633
9634 markers = target_static_tracepoint_markers_by_strid (marker_str);
9635 if (VEC_empty(static_tracepoint_marker_p, markers))
9636 error (_("No known static tracepoint marker named %s"), marker_str);
9637
9638 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9639 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9640
9641 for (i = 0; i < sals.nelts; i++)
9642 {
9643 struct static_tracepoint_marker *marker;
9644
9645 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9646
9647 init_sal (&sals.sals[i]);
9648
9649 sals.sals[i] = find_pc_line (marker->address, 0);
9650 sals.sals[i].pc = marker->address;
9651
9652 release_static_tracepoint_marker (marker);
9653 }
9654
9655 do_cleanups (old_chain);
9656
9657 *arg_p = endp;
9658 return sals;
9659}
9660
f00aae0f 9661/* See breakpoint.h. */
0101ce28 9662
8cdf0e15
VP
9663int
9664create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9665 const struct event_location *location, char *cond_string,
e7e0cddf 9666 int thread, char *extra_string,
f00aae0f 9667 int parse_extra,
0fb4aa4b 9668 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9669 int ignore_count,
9670 enum auto_boolean pending_break_support,
c0a91b2b 9671 const struct breakpoint_ops *ops,
44f238bb
PA
9672 int from_tty, int enabled, int internal,
9673 unsigned flags)
c3f6f71d 9674{
7efd8fc2 9675 struct linespec_result canonical;
80c99de1 9676 struct cleanup *bkpt_chain = NULL;
0101ce28 9677 int pending = 0;
4a306c9a 9678 int task = 0;
86b17b60 9679 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9680
348d480f
PA
9681 gdb_assert (ops != NULL);
9682
f00aae0f
KS
9683 /* If extra_string isn't useful, set it to NULL. */
9684 if (extra_string != NULL && *extra_string == '\0')
9685 extra_string = NULL;
9686
492d29ea 9687 TRY
b78a6381 9688 {
f00aae0f 9689 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9690 }
492d29ea 9691 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9692 {
492d29ea
PA
9693 /* If caller is interested in rc value from parse, set
9694 value. */
9695 if (e.error == NOT_FOUND_ERROR)
0101ce28 9696 {
05ff989b
AC
9697 /* If pending breakpoint support is turned off, throw
9698 error. */
fa8d40ab
JJ
9699
9700 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9701 throw_exception (e);
9702
9703 exception_print (gdb_stderr, e);
fa8d40ab 9704
05ff989b
AC
9705 /* If pending breakpoint support is auto query and the user
9706 selects no, then simply return the error code. */
059fb39f 9707 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9708 && !nquery (_("Make %s pending on future shared library load? "),
9709 bptype_string (type_wanted)))
fd9b8c24 9710 return 0;
fa8d40ab 9711
05ff989b
AC
9712 /* At this point, either the user was queried about setting
9713 a pending breakpoint and selected yes, or pending
9714 breakpoint behavior is on and thus a pending breakpoint
9715 is defaulted on behalf of the user. */
f00aae0f 9716 pending = 1;
0101ce28 9717 }
492d29ea
PA
9718 else
9719 throw_exception (e);
0101ce28 9720 }
492d29ea
PA
9721 END_CATCH
9722
f00aae0f 9723 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9724 return 0;
c3f6f71d 9725
c3f6f71d
JM
9726 /* ----------------------------- SNIP -----------------------------
9727 Anything added to the cleanup chain beyond this point is assumed
9728 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9729 then the memory is not reclaimed. */
9730 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9731
c3f6f71d
JM
9732 /* Resolve all line numbers to PC's and verify that the addresses
9733 are ok for the target. */
0101ce28 9734 if (!pending)
f8eba3c6
TT
9735 {
9736 int ix;
9737 struct linespec_sals *iter;
9738
9739 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9740 breakpoint_sals_to_pc (&iter->sals);
9741 }
c3f6f71d 9742
7a697b8d 9743 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9744 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9745 {
9746 int ix;
9747 struct linespec_sals *iter;
9748
9749 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9750 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9751 }
7a697b8d 9752
c3f6f71d
JM
9753 /* Verify that condition can be parsed, before setting any
9754 breakpoints. Allocate a separate condition expression for each
4a64f543 9755 breakpoint. */
0101ce28 9756 if (!pending)
c3f6f71d 9757 {
f00aae0f 9758 if (parse_extra)
72b2ff0e 9759 {
0878d0fa 9760 char *rest;
52d361e1
YQ
9761 struct linespec_sals *lsal;
9762
9763 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9764
0878d0fa
YQ
9765 /* Here we only parse 'arg' to separate condition
9766 from thread number, so parsing in context of first
9767 sal is OK. When setting the breakpoint we'll
9768 re-parse it in context of each sal. */
9769
f00aae0f
KS
9770 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9771 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9772 if (cond_string)
9773 make_cleanup (xfree, cond_string);
9774 if (rest)
9775 make_cleanup (xfree, rest);
9776 if (rest)
9777 extra_string = rest;
f00aae0f
KS
9778 else
9779 extra_string = NULL;
72b2ff0e 9780 }
2f069f6f 9781 else
72b2ff0e 9782 {
f00aae0f
KS
9783 if (type_wanted != bp_dprintf
9784 && extra_string != NULL && *extra_string != '\0')
9785 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9786
9787 /* Create a private copy of condition string. */
9788 if (cond_string)
9789 {
9790 cond_string = xstrdup (cond_string);
9791 make_cleanup (xfree, cond_string);
9792 }
9793 /* Create a private copy of any extra string. */
9794 if (extra_string)
9795 {
9796 extra_string = xstrdup (extra_string);
9797 make_cleanup (xfree, extra_string);
9798 }
72b2ff0e 9799 }
0fb4aa4b 9800
52d361e1 9801 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9802 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9803 tempflag ? disp_del : disp_donttouch,
9804 thread, task, ignore_count, ops,
44f238bb 9805 from_tty, enabled, internal, flags);
c906108c 9806 }
0101ce28
JJ
9807 else
9808 {
a5e364af 9809 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9810
a5e364af 9811 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9812 b->location = copy_event_location (location);
bfccc43c 9813
f00aae0f
KS
9814 if (parse_extra)
9815 b->cond_string = NULL;
e12c7713
MK
9816 else
9817 {
9818 /* Create a private copy of condition string. */
9819 if (cond_string)
9820 {
9821 cond_string = xstrdup (cond_string);
9822 make_cleanup (xfree, cond_string);
9823 }
9824 b->cond_string = cond_string;
15630549 9825 b->thread = thread;
e12c7713 9826 }
f00aae0f
KS
9827
9828 /* Create a private copy of any extra string. */
9829 if (extra_string != NULL)
9830 {
9831 extra_string = xstrdup (extra_string);
9832 make_cleanup (xfree, extra_string);
9833 }
9834 b->extra_string = extra_string;
0101ce28 9835 b->ignore_count = ignore_count;
0101ce28 9836 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9837 b->condition_not_parsed = 1;
41447f92 9838 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9839 if ((type_wanted != bp_breakpoint
9840 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9841 b->pspace = current_program_space;
8bea4e01 9842
a5e364af 9843 install_breakpoint (internal, b.release (), 0);
0101ce28
JJ
9844 }
9845
f8eba3c6 9846 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9847 {
3e43a32a
MS
9848 warning (_("Multiple breakpoints were set.\nUse the "
9849 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9850 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9851 }
9852
80c99de1
PA
9853 /* That's it. Discard the cleanups for data inserted into the
9854 breakpoint. */
9855 discard_cleanups (bkpt_chain);
217dc9e2 9856
80c99de1 9857 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9858 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9859
9860 return 1;
c3f6f71d 9861}
c906108c 9862
348d480f 9863/* Set a breakpoint.
72b2ff0e
VP
9864 ARG is a string describing breakpoint address,
9865 condition, and thread.
9866 FLAG specifies if a breakpoint is hardware on,
9867 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9868 and BP_TEMPFLAG. */
348d480f 9869
98deb0da 9870static void
72b2ff0e 9871break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9872{
72b2ff0e 9873 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9874 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9875 ? bp_hardware_breakpoint
9876 : bp_breakpoint);
55aa24fb 9877 struct breakpoint_ops *ops;
f00aae0f 9878
ffc2605c 9879 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9880
9881 /* Matching breakpoints on probes. */
5b56227b 9882 if (location != NULL
ffc2605c 9883 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9884 ops = &bkpt_probe_breakpoint_ops;
9885 else
9886 ops = &bkpt_breakpoint_ops;
c3f6f71d 9887
8cdf0e15 9888 create_breakpoint (get_current_arch (),
ffc2605c 9889 location.get (),
f00aae0f 9890 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9891 tempflag, type_wanted,
8cdf0e15
VP
9892 0 /* Ignore count */,
9893 pending_break_support,
55aa24fb 9894 ops,
8cdf0e15 9895 from_tty,
84f4c1fe 9896 1 /* enabled */,
44f238bb
PA
9897 0 /* internal */,
9898 0);
c906108c
SS
9899}
9900
c906108c
SS
9901/* Helper function for break_command_1 and disassemble_command. */
9902
9903void
fba45db2 9904resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9905{
9906 CORE_ADDR pc;
9907
9908 if (sal->pc == 0 && sal->symtab != NULL)
9909 {
9910 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9911 error (_("No line %d in file \"%s\"."),
05cba821 9912 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9913 sal->pc = pc;
6a048695 9914
4a64f543
MS
9915 /* If this SAL corresponds to a breakpoint inserted using a line
9916 number, then skip the function prologue if necessary. */
6a048695 9917 if (sal->explicit_line)
059acae7 9918 skip_prologue_sal (sal);
c906108c
SS
9919 }
9920
9921 if (sal->section == 0 && sal->symtab != NULL)
9922 {
346d1dfe 9923 const struct blockvector *bv;
3977b71f 9924 const struct block *b;
c5aa993b 9925 struct symbol *sym;
c906108c 9926
43f3e411
DE
9927 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9928 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9929 if (bv != NULL)
9930 {
7f0df278 9931 sym = block_linkage_function (b);
c906108c
SS
9932 if (sym != NULL)
9933 {
eb822aa6
DE
9934 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9935 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9936 sym);
c906108c
SS
9937 }
9938 else
9939 {
4a64f543
MS
9940 /* It really is worthwhile to have the section, so we'll
9941 just have to look harder. This case can be executed
9942 if we have line numbers but no functions (as can
9943 happen in assembly source). */
c906108c 9944
5ed8105e 9945 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9946 switch_to_program_space_and_thread (sal->pspace);
c906108c 9947
5ed8105e 9948 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9949 if (msym.minsym)
efd66ac6 9950 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9951 }
9952 }
9953 }
9954}
9955
9956void
fba45db2 9957break_command (char *arg, int from_tty)
c906108c 9958{
db107f19 9959 break_command_1 (arg, 0, from_tty);
c906108c
SS
9960}
9961
c906108c 9962void
fba45db2 9963tbreak_command (char *arg, int from_tty)
c906108c 9964{
db107f19 9965 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9966}
9967
c906108c 9968static void
fba45db2 9969hbreak_command (char *arg, int from_tty)
c906108c 9970{
db107f19 9971 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9972}
9973
9974static void
fba45db2 9975thbreak_command (char *arg, int from_tty)
c906108c 9976{
db107f19 9977 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9978}
9979
9980static void
fba45db2 9981stop_command (char *arg, int from_tty)
c906108c 9982{
a3f17187 9983 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9984Usage: stop in <function | address>\n\
a3f17187 9985 stop at <line>\n"));
c906108c
SS
9986}
9987
9988static void
fba45db2 9989stopin_command (char *arg, int from_tty)
c906108c
SS
9990{
9991 int badInput = 0;
9992
c5aa993b 9993 if (arg == (char *) NULL)
c906108c
SS
9994 badInput = 1;
9995 else if (*arg != '*')
9996 {
9997 char *argptr = arg;
9998 int hasColon = 0;
9999
4a64f543 10000 /* Look for a ':'. If this is a line number specification, then
53a5351d 10001 say it is bad, otherwise, it should be an address or
4a64f543 10002 function/method name. */
c906108c 10003 while (*argptr && !hasColon)
c5aa993b
JM
10004 {
10005 hasColon = (*argptr == ':');
10006 argptr++;
10007 }
c906108c
SS
10008
10009 if (hasColon)
c5aa993b 10010 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10011 else
c5aa993b 10012 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10013 }
10014
10015 if (badInput)
a3f17187 10016 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10017 else
db107f19 10018 break_command_1 (arg, 0, from_tty);
c906108c
SS
10019}
10020
10021static void
fba45db2 10022stopat_command (char *arg, int from_tty)
c906108c
SS
10023{
10024 int badInput = 0;
10025
c5aa993b 10026 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10027 badInput = 1;
10028 else
10029 {
10030 char *argptr = arg;
10031 int hasColon = 0;
10032
4a64f543
MS
10033 /* Look for a ':'. If there is a '::' then get out, otherwise
10034 it is probably a line number. */
c906108c 10035 while (*argptr && !hasColon)
c5aa993b
JM
10036 {
10037 hasColon = (*argptr == ':');
10038 argptr++;
10039 }
c906108c
SS
10040
10041 if (hasColon)
c5aa993b 10042 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10043 else
c5aa993b 10044 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10045 }
10046
10047 if (badInput)
a3f17187 10048 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10049 else
db107f19 10050 break_command_1 (arg, 0, from_tty);
c906108c
SS
10051}
10052
e7e0cddf
SS
10053/* The dynamic printf command is mostly like a regular breakpoint, but
10054 with a prewired command list consisting of a single output command,
10055 built from extra arguments supplied on the dprintf command
10056 line. */
10057
da821c7b 10058static void
e7e0cddf
SS
10059dprintf_command (char *arg, int from_tty)
10060{
ffc2605c 10061 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10062
10063 /* If non-NULL, ARG should have been advanced past the location;
10064 the next character must be ','. */
10065 if (arg != NULL)
10066 {
10067 if (arg[0] != ',' || arg[1] == '\0')
10068 error (_("Format string required"));
10069 else
10070 {
10071 /* Skip the comma. */
10072 ++arg;
10073 }
10074 }
10075
e7e0cddf 10076 create_breakpoint (get_current_arch (),
ffc2605c 10077 location.get (),
f00aae0f 10078 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10079 0, bp_dprintf,
10080 0 /* Ignore count */,
10081 pending_break_support,
10082 &dprintf_breakpoint_ops,
10083 from_tty,
10084 1 /* enabled */,
10085 0 /* internal */,
10086 0);
10087}
10088
d3ce09f5
SS
10089static void
10090agent_printf_command (char *arg, int from_tty)
10091{
10092 error (_("May only run agent-printf on the target"));
10093}
10094
f1310107
TJB
10095/* Implement the "breakpoint_hit" breakpoint_ops method for
10096 ranged breakpoints. */
10097
10098static int
10099breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10100 struct address_space *aspace,
09ac7c10
TT
10101 CORE_ADDR bp_addr,
10102 const struct target_waitstatus *ws)
f1310107 10103{
09ac7c10 10104 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10105 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10106 return 0;
10107
f1310107
TJB
10108 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10109 bl->length, aspace, bp_addr);
10110}
10111
10112/* Implement the "resources_needed" breakpoint_ops method for
10113 ranged breakpoints. */
10114
10115static int
10116resources_needed_ranged_breakpoint (const struct bp_location *bl)
10117{
10118 return target_ranged_break_num_registers ();
10119}
10120
10121/* Implement the "print_it" breakpoint_ops method for
10122 ranged breakpoints. */
10123
10124static enum print_stop_action
348d480f 10125print_it_ranged_breakpoint (bpstat bs)
f1310107 10126{
348d480f 10127 struct breakpoint *b = bs->breakpoint_at;
f1310107 10128 struct bp_location *bl = b->loc;
79a45e25 10129 struct ui_out *uiout = current_uiout;
f1310107
TJB
10130
10131 gdb_assert (b->type == bp_hardware_breakpoint);
10132
10133 /* Ranged breakpoints have only one location. */
10134 gdb_assert (bl && bl->next == NULL);
10135
10136 annotate_breakpoint (b->number);
f303dbd6
PA
10137
10138 maybe_print_thread_hit_breakpoint (uiout);
10139
f1310107 10140 if (b->disposition == disp_del)
112e8700 10141 uiout->text ("Temporary ranged breakpoint ");
f1310107 10142 else
112e8700
SM
10143 uiout->text ("Ranged breakpoint ");
10144 if (uiout->is_mi_like_p ())
f1310107 10145 {
112e8700 10146 uiout->field_string ("reason",
f1310107 10147 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10148 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10149 }
112e8700
SM
10150 uiout->field_int ("bkptno", b->number);
10151 uiout->text (", ");
f1310107
TJB
10152
10153 return PRINT_SRC_AND_LOC;
10154}
10155
10156/* Implement the "print_one" breakpoint_ops method for
10157 ranged breakpoints. */
10158
10159static void
10160print_one_ranged_breakpoint (struct breakpoint *b,
10161 struct bp_location **last_loc)
10162{
10163 struct bp_location *bl = b->loc;
10164 struct value_print_options opts;
79a45e25 10165 struct ui_out *uiout = current_uiout;
f1310107
TJB
10166
10167 /* Ranged breakpoints have only one location. */
10168 gdb_assert (bl && bl->next == NULL);
10169
10170 get_user_print_options (&opts);
10171
10172 if (opts.addressprint)
10173 /* We don't print the address range here, it will be printed later
10174 by print_one_detail_ranged_breakpoint. */
112e8700 10175 uiout->field_skip ("addr");
f1310107
TJB
10176 annotate_field (5);
10177 print_breakpoint_location (b, bl);
10178 *last_loc = bl;
10179}
10180
10181/* Implement the "print_one_detail" breakpoint_ops method for
10182 ranged breakpoints. */
10183
10184static void
10185print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10186 struct ui_out *uiout)
10187{
10188 CORE_ADDR address_start, address_end;
10189 struct bp_location *bl = b->loc;
d7e74731 10190 string_file stb;
f1310107
TJB
10191
10192 gdb_assert (bl);
10193
10194 address_start = bl->address;
10195 address_end = address_start + bl->length - 1;
10196
112e8700 10197 uiout->text ("\taddress range: ");
d7e74731
PA
10198 stb.printf ("[%s, %s]",
10199 print_core_address (bl->gdbarch, address_start),
10200 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10201 uiout->field_stream ("addr", stb);
10202 uiout->text ("\n");
f1310107
TJB
10203}
10204
10205/* Implement the "print_mention" breakpoint_ops method for
10206 ranged breakpoints. */
10207
10208static void
10209print_mention_ranged_breakpoint (struct breakpoint *b)
10210{
10211 struct bp_location *bl = b->loc;
79a45e25 10212 struct ui_out *uiout = current_uiout;
f1310107
TJB
10213
10214 gdb_assert (bl);
10215 gdb_assert (b->type == bp_hardware_breakpoint);
10216
112e8700 10217 if (uiout->is_mi_like_p ())
f1310107
TJB
10218 return;
10219
10220 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10221 b->number, paddress (bl->gdbarch, bl->address),
10222 paddress (bl->gdbarch, bl->address + bl->length - 1));
10223}
10224
10225/* Implement the "print_recreate" breakpoint_ops method for
10226 ranged breakpoints. */
10227
10228static void
10229print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10230{
f00aae0f 10231 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10232 event_location_to_string (b->location.get ()),
10233 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10234 print_recreate_thread (b, fp);
f1310107
TJB
10235}
10236
10237/* The breakpoint_ops structure to be used in ranged breakpoints. */
10238
2060206e 10239static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10240
10241/* Find the address where the end of the breakpoint range should be
10242 placed, given the SAL of the end of the range. This is so that if
10243 the user provides a line number, the end of the range is set to the
10244 last instruction of the given line. */
10245
10246static CORE_ADDR
10247find_breakpoint_range_end (struct symtab_and_line sal)
10248{
10249 CORE_ADDR end;
10250
10251 /* If the user provided a PC value, use it. Otherwise,
10252 find the address of the end of the given location. */
10253 if (sal.explicit_pc)
10254 end = sal.pc;
10255 else
10256 {
10257 int ret;
10258 CORE_ADDR start;
10259
10260 ret = find_line_pc_range (sal, &start, &end);
10261 if (!ret)
10262 error (_("Could not find location of the end of the range."));
10263
10264 /* find_line_pc_range returns the start of the next line. */
10265 end--;
10266 }
10267
10268 return end;
10269}
10270
10271/* Implement the "break-range" CLI command. */
10272
10273static void
10274break_range_command (char *arg, int from_tty)
10275{
870f88f7 10276 char *arg_start, *addr_string_start;
f1310107
TJB
10277 struct linespec_result canonical_start, canonical_end;
10278 int bp_count, can_use_bp, length;
10279 CORE_ADDR end;
10280 struct breakpoint *b;
10281 struct symtab_and_line sal_start, sal_end;
f1310107 10282 struct cleanup *cleanup_bkpt;
f8eba3c6 10283 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10284
10285 /* We don't support software ranged breakpoints. */
10286 if (target_ranged_break_num_registers () < 0)
10287 error (_("This target does not support hardware ranged breakpoints."));
10288
10289 bp_count = hw_breakpoint_used_count ();
10290 bp_count += target_ranged_break_num_registers ();
10291 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10292 bp_count, 0);
10293 if (can_use_bp < 0)
10294 error (_("Hardware breakpoints used exceeds limit."));
10295
f8eba3c6 10296 arg = skip_spaces (arg);
f1310107
TJB
10297 if (arg == NULL || arg[0] == '\0')
10298 error(_("No address range specified."));
10299
f8eba3c6 10300 arg_start = arg;
ffc2605c
TT
10301 event_location_up start_location = string_to_event_location (&arg,
10302 current_language);
10303 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10304
10305 if (arg[0] != ',')
10306 error (_("Too few arguments."));
f8eba3c6 10307 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10308 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10309
10310 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10311
10312 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10313 || lsal_start->sals.nelts != 1)
f1310107
TJB
10314 error (_("Cannot create a ranged breakpoint with multiple locations."));
10315
f8eba3c6
TT
10316 sal_start = lsal_start->sals.sals[0];
10317 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10318 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10319
10320 arg++; /* Skip the comma. */
f8eba3c6 10321 arg = skip_spaces (arg);
f1310107
TJB
10322
10323 /* Parse the end location. */
10324
f1310107
TJB
10325 arg_start = arg;
10326
f8eba3c6 10327 /* We call decode_line_full directly here instead of using
f1310107
TJB
10328 parse_breakpoint_sals because we need to specify the start location's
10329 symtab and line as the default symtab and line for the end of the
10330 range. This makes it possible to have ranges like "foo.c:27, +14",
10331 where +14 means 14 lines from the start location. */
ffc2605c
TT
10332 event_location_up end_location = string_to_event_location (&arg,
10333 current_language);
10334 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10335 sal_start.symtab, sal_start.line,
10336 &canonical_end, NULL, NULL);
10337
f8eba3c6 10338 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10339 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10340
10341 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10342 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10343 || lsal_end->sals.nelts != 1)
f1310107
TJB
10344 error (_("Cannot create a ranged breakpoint with multiple locations."));
10345
f8eba3c6 10346 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10347
10348 end = find_breakpoint_range_end (sal_end);
10349 if (sal_start.pc > end)
177b42fe 10350 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10351
10352 length = end - sal_start.pc + 1;
10353 if (length < 0)
10354 /* Length overflowed. */
10355 error (_("Address range too large."));
10356 else if (length == 1)
10357 {
10358 /* This range is simple enough to be handled by
10359 the `hbreak' command. */
10360 hbreak_command (addr_string_start, 1);
10361
10362 do_cleanups (cleanup_bkpt);
10363
10364 return;
10365 }
10366
10367 /* Now set up the breakpoint. */
10368 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10369 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10370 set_breakpoint_count (breakpoint_count + 1);
10371 b->number = breakpoint_count;
10372 b->disposition = disp_donttouch;
d28cd78a
TT
10373 b->location = std::move (start_location);
10374 b->location_range_end = std::move (end_location);
f1310107
TJB
10375 b->loc->length = length;
10376
f8eba3c6 10377 do_cleanups (cleanup_bkpt);
f1310107
TJB
10378
10379 mention (b);
8d3788bd 10380 observer_notify_breakpoint_created (b);
44702360 10381 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10382}
10383
4a64f543
MS
10384/* Return non-zero if EXP is verified as constant. Returned zero
10385 means EXP is variable. Also the constant detection may fail for
10386 some constant expressions and in such case still falsely return
10387 zero. */
2e6e3d9c 10388
65d79d4b
SDJ
10389static int
10390watchpoint_exp_is_const (const struct expression *exp)
10391{
10392 int i = exp->nelts;
10393
10394 while (i > 0)
10395 {
10396 int oplenp, argsp;
10397
10398 /* We are only interested in the descriptor of each element. */
10399 operator_length (exp, i, &oplenp, &argsp);
10400 i -= oplenp;
10401
10402 switch (exp->elts[i].opcode)
10403 {
10404 case BINOP_ADD:
10405 case BINOP_SUB:
10406 case BINOP_MUL:
10407 case BINOP_DIV:
10408 case BINOP_REM:
10409 case BINOP_MOD:
10410 case BINOP_LSH:
10411 case BINOP_RSH:
10412 case BINOP_LOGICAL_AND:
10413 case BINOP_LOGICAL_OR:
10414 case BINOP_BITWISE_AND:
10415 case BINOP_BITWISE_IOR:
10416 case BINOP_BITWISE_XOR:
10417 case BINOP_EQUAL:
10418 case BINOP_NOTEQUAL:
10419 case BINOP_LESS:
10420 case BINOP_GTR:
10421 case BINOP_LEQ:
10422 case BINOP_GEQ:
10423 case BINOP_REPEAT:
10424 case BINOP_COMMA:
10425 case BINOP_EXP:
10426 case BINOP_MIN:
10427 case BINOP_MAX:
10428 case BINOP_INTDIV:
10429 case BINOP_CONCAT:
65d79d4b
SDJ
10430 case TERNOP_COND:
10431 case TERNOP_SLICE:
65d79d4b
SDJ
10432
10433 case OP_LONG:
10434 case OP_DOUBLE:
10435 case OP_DECFLOAT:
10436 case OP_LAST:
10437 case OP_COMPLEX:
10438 case OP_STRING:
65d79d4b
SDJ
10439 case OP_ARRAY:
10440 case OP_TYPE:
608b4967
TT
10441 case OP_TYPEOF:
10442 case OP_DECLTYPE:
6e72ca20 10443 case OP_TYPEID:
65d79d4b
SDJ
10444 case OP_NAME:
10445 case OP_OBJC_NSSTRING:
10446
10447 case UNOP_NEG:
10448 case UNOP_LOGICAL_NOT:
10449 case UNOP_COMPLEMENT:
10450 case UNOP_ADDR:
10451 case UNOP_HIGH:
aeaa2474 10452 case UNOP_CAST:
9eaf6705
TT
10453
10454 case UNOP_CAST_TYPE:
10455 case UNOP_REINTERPRET_CAST:
10456 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10457 /* Unary, binary and ternary operators: We have to check
10458 their operands. If they are constant, then so is the
10459 result of that operation. For instance, if A and B are
10460 determined to be constants, then so is "A + B".
10461
10462 UNOP_IND is one exception to the rule above, because the
10463 value of *ADDR is not necessarily a constant, even when
10464 ADDR is. */
65d79d4b
SDJ
10465 break;
10466
10467 case OP_VAR_VALUE:
10468 /* Check whether the associated symbol is a constant.
4a64f543 10469
65d79d4b 10470 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10471 possible that a buggy compiler could mark a variable as
10472 constant even when it is not, and TYPE_CONST would return
10473 true in this case, while SYMBOL_CLASS wouldn't.
10474
10475 We also have to check for function symbols because they
10476 are always constant. */
65d79d4b
SDJ
10477 {
10478 struct symbol *s = exp->elts[i + 2].symbol;
10479
10480 if (SYMBOL_CLASS (s) != LOC_BLOCK
10481 && SYMBOL_CLASS (s) != LOC_CONST
10482 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10483 return 0;
10484 break;
10485 }
10486
10487 /* The default action is to return 0 because we are using
10488 the optimistic approach here: If we don't know something,
10489 then it is not a constant. */
10490 default:
10491 return 0;
10492 }
10493 }
10494
10495 return 1;
10496}
10497
3a5c3e22
PA
10498/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10499
10500static void
10501dtor_watchpoint (struct breakpoint *self)
10502{
10503 struct watchpoint *w = (struct watchpoint *) self;
10504
3a5c3e22
PA
10505 xfree (w->exp_string);
10506 xfree (w->exp_string_reparse);
10507 value_free (w->val);
10508
10509 base_breakpoint_ops.dtor (self);
10510}
10511
348d480f
PA
10512/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10513
10514static void
10515re_set_watchpoint (struct breakpoint *b)
10516{
3a5c3e22
PA
10517 struct watchpoint *w = (struct watchpoint *) b;
10518
348d480f
PA
10519 /* Watchpoint can be either on expression using entirely global
10520 variables, or it can be on local variables.
10521
10522 Watchpoints of the first kind are never auto-deleted, and even
10523 persist across program restarts. Since they can use variables
10524 from shared libraries, we need to reparse expression as libraries
10525 are loaded and unloaded.
10526
10527 Watchpoints on local variables can also change meaning as result
10528 of solib event. For example, if a watchpoint uses both a local
10529 and a global variables in expression, it's a local watchpoint,
10530 but unloading of a shared library will make the expression
10531 invalid. This is not a very common use case, but we still
10532 re-evaluate expression, to avoid surprises to the user.
10533
10534 Note that for local watchpoints, we re-evaluate it only if
10535 watchpoints frame id is still valid. If it's not, it means the
10536 watchpoint is out of scope and will be deleted soon. In fact,
10537 I'm not sure we'll ever be called in this case.
10538
10539 If a local watchpoint's frame id is still valid, then
3a5c3e22 10540 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10541
3a5c3e22
PA
10542 Don't do anything about disabled watchpoints, since they will be
10543 reevaluated again when enabled. */
10544 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10545}
10546
77b06cd7
TJB
10547/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10548
10549static int
10550insert_watchpoint (struct bp_location *bl)
10551{
3a5c3e22
PA
10552 struct watchpoint *w = (struct watchpoint *) bl->owner;
10553 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10554
10555 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10556 w->cond_exp.get ());
77b06cd7
TJB
10557}
10558
10559/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10560
10561static int
73971819 10562remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10563{
3a5c3e22
PA
10564 struct watchpoint *w = (struct watchpoint *) bl->owner;
10565 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10566
10567 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10568 w->cond_exp.get ());
e09342b5
TJB
10569}
10570
e09342b5 10571static int
348d480f 10572breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10573 struct address_space *aspace, CORE_ADDR bp_addr,
10574 const struct target_waitstatus *ws)
e09342b5 10575{
348d480f 10576 struct breakpoint *b = bl->owner;
3a5c3e22 10577 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10578
348d480f
PA
10579 /* Continuable hardware watchpoints are treated as non-existent if the
10580 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10581 some data address). Otherwise gdb won't stop on a break instruction
10582 in the code (not from a breakpoint) when a hardware watchpoint has
10583 been defined. Also skip watchpoints which we know did not trigger
10584 (did not match the data address). */
10585 if (is_hardware_watchpoint (b)
3a5c3e22 10586 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10587 return 0;
9c06b0b4 10588
348d480f 10589 return 1;
9c06b0b4
TJB
10590}
10591
348d480f
PA
10592static void
10593check_status_watchpoint (bpstat bs)
9c06b0b4 10594{
348d480f 10595 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10596
348d480f 10597 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10598}
10599
10600/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10601 hardware watchpoints. */
9c06b0b4
TJB
10602
10603static int
348d480f 10604resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10605{
3a5c3e22
PA
10606 struct watchpoint *w = (struct watchpoint *) bl->owner;
10607 int length = w->exact? 1 : bl->length;
348d480f
PA
10608
10609 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10610}
10611
10612/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10613 hardware watchpoints. */
9c06b0b4
TJB
10614
10615static int
348d480f 10616works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10617{
efa80663
PA
10618 /* Read and access watchpoints only work with hardware support. */
10619 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10620}
10621
9c06b0b4 10622static enum print_stop_action
348d480f 10623print_it_watchpoint (bpstat bs)
9c06b0b4 10624{
348d480f
PA
10625 struct cleanup *old_chain;
10626 struct breakpoint *b;
348d480f 10627 enum print_stop_action result;
3a5c3e22 10628 struct watchpoint *w;
79a45e25 10629 struct ui_out *uiout = current_uiout;
348d480f
PA
10630
10631 gdb_assert (bs->bp_location_at != NULL);
10632
348d480f 10633 b = bs->breakpoint_at;
3a5c3e22 10634 w = (struct watchpoint *) b;
348d480f 10635
d7e74731 10636 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10637
f303dbd6
PA
10638 annotate_watchpoint (b->number);
10639 maybe_print_thread_hit_breakpoint (uiout);
10640
d7e74731
PA
10641 string_file stb;
10642
9c06b0b4
TJB
10643 switch (b->type)
10644 {
348d480f 10645 case bp_watchpoint:
9c06b0b4 10646 case bp_hardware_watchpoint:
112e8700
SM
10647 if (uiout->is_mi_like_p ())
10648 uiout->field_string
10649 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10650 mention (b);
10651 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10652 uiout->text ("\nOld value = ");
d7e74731 10653 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10654 uiout->field_stream ("old", stb);
10655 uiout->text ("\nNew value = ");
d7e74731 10656 watchpoint_value_print (w->val, &stb);
112e8700
SM
10657 uiout->field_stream ("new", stb);
10658 uiout->text ("\n");
348d480f
PA
10659 /* More than one watchpoint may have been triggered. */
10660 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10661 break;
10662
10663 case bp_read_watchpoint:
112e8700
SM
10664 if (uiout->is_mi_like_p ())
10665 uiout->field_string
10666 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10667 mention (b);
10668 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10669 uiout->text ("\nValue = ");
d7e74731 10670 watchpoint_value_print (w->val, &stb);
112e8700
SM
10671 uiout->field_stream ("value", stb);
10672 uiout->text ("\n");
348d480f 10673 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10674 break;
10675
10676 case bp_access_watchpoint:
348d480f
PA
10677 if (bs->old_val != NULL)
10678 {
112e8700
SM
10679 if (uiout->is_mi_like_p ())
10680 uiout->field_string
10681 ("reason",
348d480f
PA
10682 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10683 mention (b);
10684 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10685 uiout->text ("\nOld value = ");
d7e74731 10686 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10687 uiout->field_stream ("old", stb);
10688 uiout->text ("\nNew value = ");
348d480f
PA
10689 }
10690 else
10691 {
10692 mention (b);
112e8700
SM
10693 if (uiout->is_mi_like_p ())
10694 uiout->field_string
10695 ("reason",
348d480f
PA
10696 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10697 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10698 uiout->text ("\nValue = ");
348d480f 10699 }
d7e74731 10700 watchpoint_value_print (w->val, &stb);
112e8700
SM
10701 uiout->field_stream ("new", stb);
10702 uiout->text ("\n");
348d480f 10703 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10704 break;
10705 default:
348d480f 10706 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10707 }
10708
348d480f
PA
10709 do_cleanups (old_chain);
10710 return result;
10711}
10712
10713/* Implement the "print_mention" breakpoint_ops method for hardware
10714 watchpoints. */
10715
10716static void
10717print_mention_watchpoint (struct breakpoint *b)
10718{
3a5c3e22 10719 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10720 struct ui_out *uiout = current_uiout;
46b9c129 10721 const char *tuple_name;
348d480f
PA
10722
10723 switch (b->type)
10724 {
10725 case bp_watchpoint:
112e8700 10726 uiout->text ("Watchpoint ");
46b9c129 10727 tuple_name = "wpt";
348d480f
PA
10728 break;
10729 case bp_hardware_watchpoint:
112e8700 10730 uiout->text ("Hardware watchpoint ");
46b9c129 10731 tuple_name = "wpt";
348d480f
PA
10732 break;
10733 case bp_read_watchpoint:
112e8700 10734 uiout->text ("Hardware read watchpoint ");
46b9c129 10735 tuple_name = "hw-rwpt";
348d480f
PA
10736 break;
10737 case bp_access_watchpoint:
112e8700 10738 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10739 tuple_name = "hw-awpt";
348d480f
PA
10740 break;
10741 default:
10742 internal_error (__FILE__, __LINE__,
10743 _("Invalid hardware watchpoint type."));
10744 }
10745
46b9c129 10746 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10747 uiout->field_int ("number", b->number);
10748 uiout->text (": ");
10749 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10750}
10751
10752/* Implement the "print_recreate" breakpoint_ops method for
10753 watchpoints. */
10754
10755static void
10756print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10757{
3a5c3e22
PA
10758 struct watchpoint *w = (struct watchpoint *) b;
10759
348d480f
PA
10760 switch (b->type)
10761 {
10762 case bp_watchpoint:
10763 case bp_hardware_watchpoint:
10764 fprintf_unfiltered (fp, "watch");
10765 break;
10766 case bp_read_watchpoint:
10767 fprintf_unfiltered (fp, "rwatch");
10768 break;
10769 case bp_access_watchpoint:
10770 fprintf_unfiltered (fp, "awatch");
10771 break;
10772 default:
10773 internal_error (__FILE__, __LINE__,
10774 _("Invalid watchpoint type."));
10775 }
10776
3a5c3e22 10777 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10778 print_recreate_thread (b, fp);
348d480f
PA
10779}
10780
427cd150
TT
10781/* Implement the "explains_signal" breakpoint_ops method for
10782 watchpoints. */
10783
47591c29 10784static int
427cd150
TT
10785explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10786{
10787 /* A software watchpoint cannot cause a signal other than
10788 GDB_SIGNAL_TRAP. */
10789 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10790 return 0;
427cd150 10791
47591c29 10792 return 1;
427cd150
TT
10793}
10794
348d480f
PA
10795/* The breakpoint_ops structure to be used in hardware watchpoints. */
10796
2060206e 10797static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10798
10799/* Implement the "insert" breakpoint_ops method for
10800 masked hardware watchpoints. */
10801
10802static int
10803insert_masked_watchpoint (struct bp_location *bl)
10804{
3a5c3e22
PA
10805 struct watchpoint *w = (struct watchpoint *) bl->owner;
10806
10807 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10808 bl->watchpoint_type);
10809}
10810
10811/* Implement the "remove" breakpoint_ops method for
10812 masked hardware watchpoints. */
10813
10814static int
73971819 10815remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10816{
3a5c3e22
PA
10817 struct watchpoint *w = (struct watchpoint *) bl->owner;
10818
10819 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10820 bl->watchpoint_type);
10821}
10822
10823/* Implement the "resources_needed" breakpoint_ops method for
10824 masked hardware watchpoints. */
10825
10826static int
10827resources_needed_masked_watchpoint (const struct bp_location *bl)
10828{
3a5c3e22
PA
10829 struct watchpoint *w = (struct watchpoint *) bl->owner;
10830
10831 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10832}
10833
10834/* Implement the "works_in_software_mode" breakpoint_ops method for
10835 masked hardware watchpoints. */
10836
10837static int
10838works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10839{
10840 return 0;
10841}
10842
10843/* Implement the "print_it" breakpoint_ops method for
10844 masked hardware watchpoints. */
10845
10846static enum print_stop_action
10847print_it_masked_watchpoint (bpstat bs)
10848{
10849 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10850 struct ui_out *uiout = current_uiout;
348d480f
PA
10851
10852 /* Masked watchpoints have only one location. */
10853 gdb_assert (b->loc && b->loc->next == NULL);
10854
f303dbd6
PA
10855 annotate_watchpoint (b->number);
10856 maybe_print_thread_hit_breakpoint (uiout);
10857
348d480f
PA
10858 switch (b->type)
10859 {
10860 case bp_hardware_watchpoint:
112e8700
SM
10861 if (uiout->is_mi_like_p ())
10862 uiout->field_string
10863 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10864 break;
10865
10866 case bp_read_watchpoint:
112e8700
SM
10867 if (uiout->is_mi_like_p ())
10868 uiout->field_string
10869 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10870 break;
10871
10872 case bp_access_watchpoint:
112e8700
SM
10873 if (uiout->is_mi_like_p ())
10874 uiout->field_string
10875 ("reason",
348d480f
PA
10876 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10877 break;
10878 default:
10879 internal_error (__FILE__, __LINE__,
10880 _("Invalid hardware watchpoint type."));
10881 }
10882
10883 mention (b);
112e8700 10884 uiout->text (_("\n\
9c06b0b4
TJB
10885Check the underlying instruction at PC for the memory\n\
10886address and value which triggered this watchpoint.\n"));
112e8700 10887 uiout->text ("\n");
9c06b0b4
TJB
10888
10889 /* More than one watchpoint may have been triggered. */
10890 return PRINT_UNKNOWN;
10891}
10892
10893/* Implement the "print_one_detail" breakpoint_ops method for
10894 masked hardware watchpoints. */
10895
10896static void
10897print_one_detail_masked_watchpoint (const struct breakpoint *b,
10898 struct ui_out *uiout)
10899{
3a5c3e22
PA
10900 struct watchpoint *w = (struct watchpoint *) b;
10901
9c06b0b4
TJB
10902 /* Masked watchpoints have only one location. */
10903 gdb_assert (b->loc && b->loc->next == NULL);
10904
112e8700
SM
10905 uiout->text ("\tmask ");
10906 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10907 uiout->text ("\n");
9c06b0b4
TJB
10908}
10909
10910/* Implement the "print_mention" breakpoint_ops method for
10911 masked hardware watchpoints. */
10912
10913static void
10914print_mention_masked_watchpoint (struct breakpoint *b)
10915{
3a5c3e22 10916 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10917 struct ui_out *uiout = current_uiout;
46b9c129 10918 const char *tuple_name;
9c06b0b4
TJB
10919
10920 switch (b->type)
10921 {
10922 case bp_hardware_watchpoint:
112e8700 10923 uiout->text ("Masked hardware watchpoint ");
46b9c129 10924 tuple_name = "wpt";
9c06b0b4
TJB
10925 break;
10926 case bp_read_watchpoint:
112e8700 10927 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10928 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10929 break;
10930 case bp_access_watchpoint:
112e8700 10931 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10932 tuple_name = "hw-awpt";
9c06b0b4
TJB
10933 break;
10934 default:
10935 internal_error (__FILE__, __LINE__,
10936 _("Invalid hardware watchpoint type."));
10937 }
10938
46b9c129 10939 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10940 uiout->field_int ("number", b->number);
10941 uiout->text (": ");
10942 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10943}
10944
10945/* Implement the "print_recreate" breakpoint_ops method for
10946 masked hardware watchpoints. */
10947
10948static void
10949print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10950{
3a5c3e22 10951 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10952 char tmp[40];
10953
10954 switch (b->type)
10955 {
10956 case bp_hardware_watchpoint:
10957 fprintf_unfiltered (fp, "watch");
10958 break;
10959 case bp_read_watchpoint:
10960 fprintf_unfiltered (fp, "rwatch");
10961 break;
10962 case bp_access_watchpoint:
10963 fprintf_unfiltered (fp, "awatch");
10964 break;
10965 default:
10966 internal_error (__FILE__, __LINE__,
10967 _("Invalid hardware watchpoint type."));
10968 }
10969
3a5c3e22
PA
10970 sprintf_vma (tmp, w->hw_wp_mask);
10971 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10972 print_recreate_thread (b, fp);
9c06b0b4
TJB
10973}
10974
10975/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10976
2060206e 10977static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10978
10979/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10980
10981static int
10982is_masked_watchpoint (const struct breakpoint *b)
10983{
10984 return b->ops == &masked_watchpoint_breakpoint_ops;
10985}
10986
53a5351d
JM
10987/* accessflag: hw_write: watch write,
10988 hw_read: watch read,
10989 hw_access: watch access (read or write) */
c906108c 10990static void
bbc13ae3 10991watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10992 int just_location, int internal)
c906108c 10993{
d983da9c 10994 struct breakpoint *b, *scope_breakpoint = NULL;
270140bd 10995 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10996 struct value *val, *mark, *result;
bb9d5f81 10997 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10998 const char *exp_start = NULL;
10999 const char *exp_end = NULL;
11000 const char *tok, *end_tok;
9c06b0b4 11001 int toklen = -1;
bbc13ae3
KS
11002 const char *cond_start = NULL;
11003 const char *cond_end = NULL;
c906108c 11004 enum bptype bp_type;
37e4754d 11005 int thread = -1;
0cf6dd15 11006 int pc = 0;
9c06b0b4
TJB
11007 /* Flag to indicate whether we are going to use masks for
11008 the hardware watchpoint. */
11009 int use_mask = 0;
11010 CORE_ADDR mask = 0;
3a5c3e22 11011 struct watchpoint *w;
bbc13ae3
KS
11012 char *expression;
11013 struct cleanup *back_to;
c906108c 11014
37e4754d
LM
11015 /* Make sure that we actually have parameters to parse. */
11016 if (arg != NULL && arg[0] != '\0')
11017 {
bbc13ae3
KS
11018 const char *value_start;
11019
11020 exp_end = arg + strlen (arg);
37e4754d 11021
9c06b0b4
TJB
11022 /* Look for "parameter value" pairs at the end
11023 of the arguments string. */
bbc13ae3 11024 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11025 {
11026 /* Skip whitespace at the end of the argument list. */
11027 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11028 tok--;
11029
11030 /* Find the beginning of the last token.
11031 This is the value of the parameter. */
11032 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11033 tok--;
11034 value_start = tok + 1;
11035
11036 /* Skip whitespace. */
11037 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11038 tok--;
11039
11040 end_tok = tok;
11041
11042 /* Find the beginning of the second to last token.
11043 This is the parameter itself. */
11044 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11045 tok--;
11046 tok++;
11047 toklen = end_tok - tok + 1;
11048
61012eef 11049 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11050 {
5d5658a1 11051 struct thread_info *thr;
9c06b0b4
TJB
11052 /* At this point we've found a "thread" token, which means
11053 the user is trying to set a watchpoint that triggers
11054 only in a specific thread. */
5d5658a1 11055 const char *endp;
37e4754d 11056
9c06b0b4
TJB
11057 if (thread != -1)
11058 error(_("You can specify only one thread."));
37e4754d 11059
9c06b0b4 11060 /* Extract the thread ID from the next token. */
5d5658a1 11061 thr = parse_thread_id (value_start, &endp);
37e4754d 11062
5d5658a1 11063 /* Check if the user provided a valid thread ID. */
9c06b0b4 11064 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11065 invalid_thread_id_error (value_start);
9c06b0b4 11066
5d5658a1 11067 thread = thr->global_num;
9c06b0b4 11068 }
61012eef 11069 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11070 {
11071 /* We've found a "mask" token, which means the user wants to
11072 create a hardware watchpoint that is going to have the mask
11073 facility. */
11074 struct value *mask_value, *mark;
37e4754d 11075
9c06b0b4
TJB
11076 if (use_mask)
11077 error(_("You can specify only one mask."));
37e4754d 11078
9c06b0b4 11079 use_mask = just_location = 1;
37e4754d 11080
9c06b0b4
TJB
11081 mark = value_mark ();
11082 mask_value = parse_to_comma_and_eval (&value_start);
11083 mask = value_as_address (mask_value);
11084 value_free_to_mark (mark);
11085 }
11086 else
11087 /* We didn't recognize what we found. We should stop here. */
11088 break;
37e4754d 11089
9c06b0b4
TJB
11090 /* Truncate the string and get rid of the "parameter value" pair before
11091 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11092 exp_end = tok;
9c06b0b4 11093 }
37e4754d 11094 }
bbc13ae3
KS
11095 else
11096 exp_end = arg;
37e4754d 11097
bbc13ae3
KS
11098 /* Parse the rest of the arguments. From here on out, everything
11099 is in terms of a newly allocated string instead of the original
11100 ARG. */
c906108c 11101 innermost_block = NULL;
bbc13ae3
KS
11102 expression = savestring (arg, exp_end - arg);
11103 back_to = make_cleanup (xfree, expression);
11104 exp_start = arg = expression;
4d01a485 11105 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11106 exp_end = arg;
fa8a61dc
TT
11107 /* Remove trailing whitespace from the expression before saving it.
11108 This makes the eventual display of the expression string a bit
11109 prettier. */
11110 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11111 --exp_end;
11112
65d79d4b 11113 /* Checking if the expression is not constant. */
4d01a485 11114 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11115 {
11116 int len;
11117
11118 len = exp_end - exp_start;
11119 while (len > 0 && isspace (exp_start[len - 1]))
11120 len--;
11121 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11122 }
11123
c906108c
SS
11124 exp_valid_block = innermost_block;
11125 mark = value_mark ();
4d01a485 11126 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11127
bb9d5f81
PP
11128 if (val != NULL && just_location)
11129 {
11130 saved_bitpos = value_bitpos (val);
11131 saved_bitsize = value_bitsize (val);
11132 }
11133
06a64a0b
TT
11134 if (just_location)
11135 {
9c06b0b4
TJB
11136 int ret;
11137
06a64a0b 11138 exp_valid_block = NULL;
a1442452 11139 val = value_addr (result);
06a64a0b
TT
11140 release_value (val);
11141 value_free_to_mark (mark);
9c06b0b4
TJB
11142
11143 if (use_mask)
11144 {
11145 ret = target_masked_watch_num_registers (value_as_address (val),
11146 mask);
11147 if (ret == -1)
11148 error (_("This target does not support masked watchpoints."));
11149 else if (ret == -2)
11150 error (_("Invalid mask or memory region."));
11151 }
06a64a0b
TT
11152 }
11153 else if (val != NULL)
fa4727a6 11154 release_value (val);
c906108c 11155
bbc13ae3
KS
11156 tok = skip_spaces_const (arg);
11157 end_tok = skip_to_space_const (tok);
c906108c
SS
11158
11159 toklen = end_tok - tok;
11160 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11161 {
60e1c644 11162 innermost_block = NULL;
c906108c 11163 tok = cond_start = end_tok + 1;
4d01a485 11164 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11165
11166 /* The watchpoint expression may not be local, but the condition
11167 may still be. E.g.: `watch global if local > 0'. */
11168 cond_exp_valid_block = innermost_block;
11169
c906108c
SS
11170 cond_end = tok;
11171 }
11172 if (*tok)
8a3fe4f8 11173 error (_("Junk at end of command."));
c906108c 11174
441d7c93
PA
11175 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11176
11177 /* Save this because create_internal_breakpoint below invalidates
11178 'wp_frame'. */
11179 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11180
11181 /* If the expression is "local", then set up a "watchpoint scope"
11182 breakpoint at the point where we've left the scope of the watchpoint
11183 expression. Create the scope breakpoint before the watchpoint, so
11184 that we will encounter it first in bpstat_stop_status. */
441d7c93 11185 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11186 {
441d7c93
PA
11187 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11188
11189 if (frame_id_p (caller_frame_id))
edb3359d 11190 {
441d7c93
PA
11191 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11192 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11193
edb3359d 11194 scope_breakpoint
441d7c93 11195 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11196 bp_watchpoint_scope,
11197 &momentary_breakpoint_ops);
d983da9c 11198
441d7c93
PA
11199 /* create_internal_breakpoint could invalidate WP_FRAME. */
11200 wp_frame = NULL;
11201
edb3359d 11202 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11203
edb3359d
DJ
11204 /* Automatically delete the breakpoint when it hits. */
11205 scope_breakpoint->disposition = disp_del;
d983da9c 11206
edb3359d 11207 /* Only break in the proper frame (help with recursion). */
441d7c93 11208 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11209
edb3359d 11210 /* Set the address at which we will stop. */
441d7c93
PA
11211 scope_breakpoint->loc->gdbarch = caller_arch;
11212 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11213 scope_breakpoint->loc->address
a6d9a66e
UW
11214 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11215 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11216 scope_breakpoint->type);
11217 }
d983da9c
DJ
11218 }
11219
e8369a73
AB
11220 /* Now set up the breakpoint. We create all watchpoints as hardware
11221 watchpoints here even if hardware watchpoints are turned off, a call
11222 to update_watchpoint later in this function will cause the type to
11223 drop back to bp_watchpoint (software watchpoint) if required. */
11224
11225 if (accessflag == hw_read)
11226 bp_type = bp_read_watchpoint;
11227 else if (accessflag == hw_access)
11228 bp_type = bp_access_watchpoint;
11229 else
11230 bp_type = bp_hardware_watchpoint;
3a5c3e22 11231
4d01a485 11232 w = new watchpoint ();
3a5c3e22 11233 b = &w->base;
348d480f 11234 if (use_mask)
3a5c3e22
PA
11235 init_raw_breakpoint_without_location (b, NULL, bp_type,
11236 &masked_watchpoint_breakpoint_ops);
348d480f 11237 else
3a5c3e22
PA
11238 init_raw_breakpoint_without_location (b, NULL, bp_type,
11239 &watchpoint_breakpoint_ops);
37e4754d 11240 b->thread = thread;
b5de0fa7 11241 b->disposition = disp_donttouch;
348d480f 11242 b->pspace = current_program_space;
b22e99fd 11243 w->exp = std::move (exp);
3a5c3e22
PA
11244 w->exp_valid_block = exp_valid_block;
11245 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11246 if (just_location)
11247 {
11248 struct type *t = value_type (val);
11249 CORE_ADDR addr = value_as_address (val);
06a64a0b 11250
43cc5389
TT
11251 w->exp_string_reparse
11252 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11253
3a5c3e22 11254 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11255 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11256 }
11257 else
3a5c3e22 11258 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11259
11260 if (use_mask)
11261 {
3a5c3e22 11262 w->hw_wp_mask = mask;
9c06b0b4
TJB
11263 }
11264 else
11265 {
3a5c3e22 11266 w->val = val;
bb9d5f81
PP
11267 w->val_bitpos = saved_bitpos;
11268 w->val_bitsize = saved_bitsize;
3a5c3e22 11269 w->val_valid = 1;
9c06b0b4 11270 }
77b06cd7 11271
c906108c
SS
11272 if (cond_start)
11273 b->cond_string = savestring (cond_start, cond_end - cond_start);
11274 else
11275 b->cond_string = 0;
c5aa993b 11276
441d7c93 11277 if (frame_id_p (watchpoint_frame))
f6bc2008 11278 {
441d7c93 11279 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11280 w->watchpoint_thread = inferior_ptid;
f6bc2008 11281 }
c906108c 11282 else
f6bc2008 11283 {
3a5c3e22
PA
11284 w->watchpoint_frame = null_frame_id;
11285 w->watchpoint_thread = null_ptid;
f6bc2008 11286 }
c906108c 11287
d983da9c 11288 if (scope_breakpoint != NULL)
c906108c 11289 {
d983da9c
DJ
11290 /* The scope breakpoint is related to the watchpoint. We will
11291 need to act on them together. */
11292 b->related_breakpoint = scope_breakpoint;
11293 scope_breakpoint->related_breakpoint = b;
c906108c 11294 }
d983da9c 11295
06a64a0b
TT
11296 if (!just_location)
11297 value_free_to_mark (mark);
2d134ed3 11298
492d29ea 11299 TRY
a9634178
TJB
11300 {
11301 /* Finally update the new watchpoint. This creates the locations
11302 that should be inserted. */
3a5c3e22 11303 update_watchpoint (w, 1);
a9634178 11304 }
492d29ea 11305 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11306 {
11307 delete_breakpoint (b);
11308 throw_exception (e);
11309 }
492d29ea 11310 END_CATCH
a9634178 11311
3ea46bff 11312 install_breakpoint (internal, b, 1);
bbc13ae3 11313 do_cleanups (back_to);
c906108c
SS
11314}
11315
e09342b5 11316/* Return count of debug registers needed to watch the given expression.
e09342b5 11317 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11318
c906108c 11319static int
a9634178 11320can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11321{
11322 int found_memory_cnt = 0;
2e70b7b9 11323 struct value *head = v;
c906108c
SS
11324
11325 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11326 if (!can_use_hw_watchpoints)
c906108c 11327 return 0;
c5aa993b 11328
5c44784c
JM
11329 /* Make sure that the value of the expression depends only upon
11330 memory contents, and values computed from them within GDB. If we
11331 find any register references or function calls, we can't use a
11332 hardware watchpoint.
11333
11334 The idea here is that evaluating an expression generates a series
11335 of values, one holding the value of every subexpression. (The
11336 expression a*b+c has five subexpressions: a, b, a*b, c, and
11337 a*b+c.) GDB's values hold almost enough information to establish
11338 the criteria given above --- they identify memory lvalues,
11339 register lvalues, computed values, etcetera. So we can evaluate
11340 the expression, and then scan the chain of values that leaves
11341 behind to decide whether we can detect any possible change to the
11342 expression's final value using only hardware watchpoints.
11343
11344 However, I don't think that the values returned by inferior
11345 function calls are special in any way. So this function may not
11346 notice that an expression involving an inferior function call
11347 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11348 for (; v; v = value_next (v))
c906108c 11349 {
5c44784c 11350 if (VALUE_LVAL (v) == lval_memory)
c906108c 11351 {
8464be76
DJ
11352 if (v != head && value_lazy (v))
11353 /* A lazy memory lvalue in the chain is one that GDB never
11354 needed to fetch; we either just used its address (e.g.,
11355 `a' in `a.b') or we never needed it at all (e.g., `a'
11356 in `a,b'). This doesn't apply to HEAD; if that is
11357 lazy then it was not readable, but watch it anyway. */
5c44784c 11358 ;
53a5351d 11359 else
5c44784c
JM
11360 {
11361 /* Ahh, memory we actually used! Check if we can cover
11362 it with hardware watchpoints. */
df407dfe 11363 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11364
11365 /* We only watch structs and arrays if user asked for it
11366 explicitly, never if they just happen to appear in a
11367 middle of some value chain. */
11368 if (v == head
11369 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11370 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11371 {
42ae5230 11372 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11373 int len;
11374 int num_regs;
11375
a9634178 11376 len = (target_exact_watchpoints
e09342b5
TJB
11377 && is_scalar_type_recursive (vtype))?
11378 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11379
e09342b5
TJB
11380 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11381 if (!num_regs)
2e70b7b9
MS
11382 return 0;
11383 else
e09342b5 11384 found_memory_cnt += num_regs;
2e70b7b9 11385 }
5c44784c 11386 }
c5aa993b 11387 }
5086187c
AC
11388 else if (VALUE_LVAL (v) != not_lval
11389 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11390 return 0; /* These are values from the history (e.g., $1). */
5086187c 11391 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11392 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11393 }
11394
11395 /* The expression itself looks suitable for using a hardware
11396 watchpoint, but give the target machine a chance to reject it. */
11397 return found_memory_cnt;
11398}
11399
8b93c638 11400void
84f4c1fe 11401watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11402{
84f4c1fe 11403 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11404}
11405
06a64a0b
TT
11406/* A helper function that looks for the "-location" argument and then
11407 calls watch_command_1. */
11408
11409static void
11410watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11411{
11412 int just_location = 0;
11413
11414 if (arg
11415 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11416 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11417 {
e9cafbcc 11418 arg = skip_spaces (arg);
06a64a0b
TT
11419 just_location = 1;
11420 }
11421
84f4c1fe 11422 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11423}
8926118c 11424
c5aa993b 11425static void
fba45db2 11426watch_command (char *arg, int from_tty)
c906108c 11427{
06a64a0b 11428 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11429}
11430
8b93c638 11431void
84f4c1fe 11432rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11433{
84f4c1fe 11434 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11435}
8926118c 11436
c5aa993b 11437static void
fba45db2 11438rwatch_command (char *arg, int from_tty)
c906108c 11439{
06a64a0b 11440 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11441}
11442
8b93c638 11443void
84f4c1fe 11444awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11445{
84f4c1fe 11446 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11447}
8926118c 11448
c5aa993b 11449static void
fba45db2 11450awatch_command (char *arg, int from_tty)
c906108c 11451{
06a64a0b 11452 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11453}
c906108c 11454\f
c5aa993b 11455
cfc31633
PA
11456/* Data for the FSM that manages the until(location)/advance commands
11457 in infcmd.c. Here because it uses the mechanisms of
11458 breakpoints. */
c906108c 11459
cfc31633 11460struct until_break_fsm
bfec99b2 11461{
cfc31633
PA
11462 /* The base class. */
11463 struct thread_fsm thread_fsm;
11464
11465 /* The thread that as current when the command was executed. */
11466 int thread;
11467
11468 /* The breakpoint set at the destination location. */
11469 struct breakpoint *location_breakpoint;
11470
11471 /* Breakpoint set at the return address in the caller frame. May be
11472 NULL. */
11473 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11474};
11475
8980e177
PA
11476static void until_break_fsm_clean_up (struct thread_fsm *self,
11477 struct thread_info *thread);
11478static int until_break_fsm_should_stop (struct thread_fsm *self,
11479 struct thread_info *thread);
cfc31633
PA
11480static enum async_reply_reason
11481 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11482
11483/* until_break_fsm's vtable. */
11484
11485static struct thread_fsm_ops until_break_fsm_ops =
11486{
11487 NULL, /* dtor */
11488 until_break_fsm_clean_up,
11489 until_break_fsm_should_stop,
11490 NULL, /* return_value */
11491 until_break_fsm_async_reply_reason,
11492};
11493
11494/* Allocate a new until_break_command_fsm. */
11495
11496static struct until_break_fsm *
8980e177 11497new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11498 struct breakpoint *location_breakpoint,
11499 struct breakpoint *caller_breakpoint)
11500{
11501 struct until_break_fsm *sm;
11502
11503 sm = XCNEW (struct until_break_fsm);
8980e177 11504 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11505
11506 sm->thread = thread;
11507 sm->location_breakpoint = location_breakpoint;
11508 sm->caller_breakpoint = caller_breakpoint;
11509
11510 return sm;
11511}
11512
11513/* Implementation of the 'should_stop' FSM method for the
11514 until(location)/advance commands. */
11515
11516static int
8980e177
PA
11517until_break_fsm_should_stop (struct thread_fsm *self,
11518 struct thread_info *tp)
cfc31633
PA
11519{
11520 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11521
11522 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11523 sm->location_breakpoint) != NULL
11524 || (sm->caller_breakpoint != NULL
11525 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11526 sm->caller_breakpoint) != NULL))
11527 thread_fsm_set_finished (self);
11528
11529 return 1;
11530}
11531
11532/* Implementation of the 'clean_up' FSM method for the
11533 until(location)/advance commands. */
11534
c2c6d25f 11535static void
8980e177
PA
11536until_break_fsm_clean_up (struct thread_fsm *self,
11537 struct thread_info *thread)
43ff13b4 11538{
cfc31633 11539 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11540
cfc31633
PA
11541 /* Clean up our temporary breakpoints. */
11542 if (sm->location_breakpoint != NULL)
11543 {
11544 delete_breakpoint (sm->location_breakpoint);
11545 sm->location_breakpoint = NULL;
11546 }
11547 if (sm->caller_breakpoint != NULL)
11548 {
11549 delete_breakpoint (sm->caller_breakpoint);
11550 sm->caller_breakpoint = NULL;
11551 }
11552 delete_longjmp_breakpoint (sm->thread);
11553}
11554
11555/* Implementation of the 'async_reply_reason' FSM method for the
11556 until(location)/advance commands. */
11557
11558static enum async_reply_reason
11559until_break_fsm_async_reply_reason (struct thread_fsm *self)
11560{
11561 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11562}
11563
c906108c 11564void
ae66c1fc 11565until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11566{
11567 struct symtabs_and_lines sals;
11568 struct symtab_and_line sal;
8556afb4
PA
11569 struct frame_info *frame;
11570 struct gdbarch *frame_gdbarch;
11571 struct frame_id stack_frame_id;
11572 struct frame_id caller_frame_id;
cfc31633
PA
11573 struct breakpoint *location_breakpoint;
11574 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11575 struct cleanup *old_chain;
186c406b
TT
11576 int thread;
11577 struct thread_info *tp;
cfc31633 11578 struct until_break_fsm *sm;
c906108c 11579
70509625 11580 clear_proceed_status (0);
c906108c
SS
11581
11582 /* Set a breakpoint where the user wants it and at return from
4a64f543 11583 this function. */
c5aa993b 11584
ffc2605c 11585 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11586
1bfeeb0f 11587 if (last_displayed_sal_is_valid ())
ffc2605c 11588 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11589 get_last_displayed_symtab (),
f8eba3c6 11590 get_last_displayed_line ());
c906108c 11591 else
ffc2605c 11592 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11593 NULL, (struct symtab *) NULL, 0);
c5aa993b 11594
c906108c 11595 if (sals.nelts != 1)
8a3fe4f8 11596 error (_("Couldn't get information on specified line."));
c5aa993b 11597
c906108c 11598 sal = sals.sals[0];
4a64f543 11599 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11600
c906108c 11601 if (*arg)
8a3fe4f8 11602 error (_("Junk at end of arguments."));
c5aa993b 11603
c906108c 11604 resolve_sal_pc (&sal);
c5aa993b 11605
186c406b 11606 tp = inferior_thread ();
5d5658a1 11607 thread = tp->global_num;
186c406b 11608
883bc8d1
PA
11609 old_chain = make_cleanup (null_cleanup, NULL);
11610
8556afb4
PA
11611 /* Note linespec handling above invalidates the frame chain.
11612 Installing a breakpoint also invalidates the frame chain (as it
11613 may need to switch threads), so do any frame handling before
11614 that. */
11615
11616 frame = get_selected_frame (NULL);
11617 frame_gdbarch = get_frame_arch (frame);
11618 stack_frame_id = get_stack_frame_id (frame);
11619 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11620
ae66c1fc
EZ
11621 /* Keep within the current frame, or in frames called by the current
11622 one. */
edb3359d 11623
883bc8d1 11624 if (frame_id_p (caller_frame_id))
c906108c 11625 {
883bc8d1 11626 struct symtab_and_line sal2;
cfc31633 11627 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11628
11629 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11630 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11631 caller_gdbarch = frame_unwind_caller_arch (frame);
11632 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11633 sal2,
11634 caller_frame_id,
11635 bp_until);
11636 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11637
883bc8d1 11638 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11639 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11640 }
c5aa993b 11641
c70a6932
JK
11642 /* set_momentary_breakpoint could invalidate FRAME. */
11643 frame = NULL;
11644
883bc8d1
PA
11645 if (anywhere)
11646 /* If the user told us to continue until a specified location,
11647 we don't specify a frame at which we need to stop. */
cfc31633
PA
11648 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11649 null_frame_id, bp_until);
883bc8d1
PA
11650 else
11651 /* Otherwise, specify the selected frame, because we want to stop
11652 only at the very same frame. */
cfc31633
PA
11653 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11654 stack_frame_id, bp_until);
11655 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11656
8980e177 11657 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11658 location_breakpoint, caller_breakpoint);
cfc31633 11659 tp->thread_fsm = &sm->thread_fsm;
f107f563 11660
cfc31633 11661 discard_cleanups (old_chain);
f107f563 11662
cfc31633 11663 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11664}
ae66c1fc 11665
c906108c
SS
11666/* This function attempts to parse an optional "if <cond>" clause
11667 from the arg string. If one is not found, it returns NULL.
c5aa993b 11668
c906108c
SS
11669 Else, it returns a pointer to the condition string. (It does not
11670 attempt to evaluate the string against a particular block.) And,
11671 it updates arg to point to the first character following the parsed
4a64f543 11672 if clause in the arg string. */
53a5351d 11673
63160a43
PA
11674const char *
11675ep_parse_optional_if_clause (const char **arg)
c906108c 11676{
63160a43 11677 const char *cond_string;
c5aa993b
JM
11678
11679 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11680 return NULL;
c5aa993b 11681
4a64f543 11682 /* Skip the "if" keyword. */
c906108c 11683 (*arg) += 2;
c5aa993b 11684
c906108c 11685 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11686 condition string. */
63160a43 11687 *arg = skip_spaces_const (*arg);
c906108c 11688 cond_string = *arg;
c5aa993b 11689
4a64f543
MS
11690 /* Assume that the condition occupies the remainder of the arg
11691 string. */
c906108c 11692 (*arg) += strlen (cond_string);
c5aa993b 11693
c906108c
SS
11694 return cond_string;
11695}
c5aa993b 11696
c906108c
SS
11697/* Commands to deal with catching events, such as signals, exceptions,
11698 process start/exit, etc. */
c5aa993b
JM
11699
11700typedef enum
11701{
44feb3ce
TT
11702 catch_fork_temporary, catch_vfork_temporary,
11703 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11704}
11705catch_fork_kind;
11706
c906108c 11707static void
63160a43 11708catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11709 struct cmd_list_element *command)
c906108c 11710{
63160a43 11711 const char *arg = arg_entry;
a6d9a66e 11712 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11713 const char *cond_string = NULL;
44feb3ce
TT
11714 catch_fork_kind fork_kind;
11715 int tempflag;
11716
11717 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11718 tempflag = (fork_kind == catch_fork_temporary
11719 || fork_kind == catch_vfork_temporary);
c5aa993b 11720
44feb3ce
TT
11721 if (!arg)
11722 arg = "";
63160a43 11723 arg = skip_spaces_const (arg);
c5aa993b 11724
c906108c 11725 /* The allowed syntax is:
c5aa993b
JM
11726 catch [v]fork
11727 catch [v]fork if <cond>
11728
4a64f543 11729 First, check if there's an if clause. */
c906108c 11730 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11731
c906108c 11732 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11733 error (_("Junk at end of arguments."));
c5aa993b 11734
c906108c 11735 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11736 and enable reporting of such events. */
c5aa993b
JM
11737 switch (fork_kind)
11738 {
44feb3ce
TT
11739 case catch_fork_temporary:
11740 case catch_fork_permanent:
a6d9a66e 11741 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11742 &catch_fork_breakpoint_ops);
c906108c 11743 break;
44feb3ce
TT
11744 case catch_vfork_temporary:
11745 case catch_vfork_permanent:
a6d9a66e 11746 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11747 &catch_vfork_breakpoint_ops);
c906108c 11748 break;
c5aa993b 11749 default:
8a3fe4f8 11750 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11751 break;
c5aa993b 11752 }
c906108c
SS
11753}
11754
11755static void
63160a43 11756catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11757 struct cmd_list_element *command)
c906108c 11758{
63160a43 11759 const char *arg = arg_entry;
b4d90040 11760 struct exec_catchpoint *c;
a6d9a66e 11761 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11762 int tempflag;
63160a43 11763 const char *cond_string = NULL;
c906108c 11764
44feb3ce
TT
11765 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11766
11767 if (!arg)
11768 arg = "";
63160a43 11769 arg = skip_spaces_const (arg);
c906108c
SS
11770
11771 /* The allowed syntax is:
c5aa993b
JM
11772 catch exec
11773 catch exec if <cond>
c906108c 11774
4a64f543 11775 First, check if there's an if clause. */
c906108c
SS
11776 cond_string = ep_parse_optional_if_clause (&arg);
11777
11778 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11779 error (_("Junk at end of arguments."));
c906108c 11780
4d01a485 11781 c = new exec_catchpoint ();
b4d90040
PA
11782 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11783 &catch_exec_breakpoint_ops);
11784 c->exec_pathname = NULL;
11785
3ea46bff 11786 install_breakpoint (0, &c->base, 1);
c906108c 11787}
c5aa993b 11788
9ac4176b 11789void
28010a5d
PA
11790init_ada_exception_breakpoint (struct breakpoint *b,
11791 struct gdbarch *gdbarch,
11792 struct symtab_and_line sal,
11793 char *addr_string,
c0a91b2b 11794 const struct breakpoint_ops *ops,
28010a5d 11795 int tempflag,
349774ef 11796 int enabled,
28010a5d 11797 int from_tty)
f7f9143b 11798{
f7f9143b
JB
11799 if (from_tty)
11800 {
5af949e3
UW
11801 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11802 if (!loc_gdbarch)
11803 loc_gdbarch = gdbarch;
11804
6c95b8df
PA
11805 describe_other_breakpoints (loc_gdbarch,
11806 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11807 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11808 version for exception catchpoints, because two catchpoints
11809 used for different exception names will use the same address.
11810 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11811 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11812 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11813 the user what type of catchpoint it is. The above is good
11814 enough for now, though. */
11815 }
11816
28010a5d 11817 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11818
349774ef 11819 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11820 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11821 b->location = string_to_event_location (&addr_string,
11822 language_def (language_ada));
f7f9143b 11823 b->language = language_ada;
f7f9143b
JB
11824}
11825
c906108c 11826static void
fba45db2 11827catch_command (char *arg, int from_tty)
c906108c 11828{
44feb3ce 11829 error (_("Catch requires an event name."));
c906108c
SS
11830}
11831\f
11832
11833static void
fba45db2 11834tcatch_command (char *arg, int from_tty)
c906108c 11835{
44feb3ce 11836 error (_("Catch requires an event name."));
c906108c
SS
11837}
11838
8a2c437b
TT
11839/* A qsort comparison function that sorts breakpoints in order. */
11840
11841static int
11842compare_breakpoints (const void *a, const void *b)
11843{
9a3c8263 11844 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11845 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11846 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11847 uintptr_t ub = (uintptr_t) *bb;
11848
11849 if ((*ba)->number < (*bb)->number)
11850 return -1;
11851 else if ((*ba)->number > (*bb)->number)
11852 return 1;
11853
11854 /* Now sort by address, in case we see, e..g, two breakpoints with
11855 the number 0. */
11856 if (ua < ub)
11857 return -1;
94b0e70d 11858 return ua > ub ? 1 : 0;
8a2c437b
TT
11859}
11860
80f8a6eb 11861/* Delete breakpoints by address or line. */
c906108c
SS
11862
11863static void
fba45db2 11864clear_command (char *arg, int from_tty)
c906108c 11865{
8a2c437b 11866 struct breakpoint *b, *prev;
d6e956e5
VP
11867 VEC(breakpoint_p) *found = 0;
11868 int ix;
c906108c
SS
11869 int default_match;
11870 struct symtabs_and_lines sals;
11871 struct symtab_and_line sal;
c906108c 11872 int i;
8a2c437b 11873 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11874
11875 if (arg)
11876 {
39cf75f7
DE
11877 sals = decode_line_with_current_source (arg,
11878 (DECODE_LINE_FUNFIRSTLINE
11879 | DECODE_LINE_LIST_MODE));
cf4ded82 11880 make_cleanup (xfree, sals.sals);
c906108c
SS
11881 default_match = 0;
11882 }
11883 else
11884 {
8d749320 11885 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11886 make_cleanup (xfree, sals.sals);
4a64f543 11887 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11888
11889 /* Set sal's line, symtab, pc, and pspace to the values
11890 corresponding to the last call to print_frame_info. If the
11891 codepoint is not valid, this will set all the fields to 0. */
11892 get_last_displayed_sal (&sal);
c906108c 11893 if (sal.symtab == 0)
8a3fe4f8 11894 error (_("No source file specified."));
c906108c
SS
11895
11896 sals.sals[0] = sal;
11897 sals.nelts = 1;
11898
11899 default_match = 1;
11900 }
11901
4a64f543
MS
11902 /* We don't call resolve_sal_pc here. That's not as bad as it
11903 seems, because all existing breakpoints typically have both
11904 file/line and pc set. So, if clear is given file/line, we can
11905 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11906
11907 We only support clearing given the address explicitly
11908 present in breakpoint table. Say, we've set breakpoint
4a64f543 11909 at file:line. There were several PC values for that file:line,
ed0616c6 11910 due to optimization, all in one block.
4a64f543
MS
11911
11912 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11913 PC corresponding to the same file:line, the breakpoint won't
11914 be cleared. We probably can still clear the breakpoint, but
11915 since the other PC value is never presented to user, user
11916 can only find it by guessing, and it does not seem important
11917 to support that. */
11918
4a64f543
MS
11919 /* For each line spec given, delete bps which correspond to it. Do
11920 it in two passes, solely to preserve the current behavior that
11921 from_tty is forced true if we delete more than one
11922 breakpoint. */
c906108c 11923
80f8a6eb 11924 found = NULL;
8a2c437b 11925 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11926 for (i = 0; i < sals.nelts; i++)
11927 {
05cba821
JK
11928 const char *sal_fullname;
11929
c906108c 11930 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11931 If line given (pc == 0), clear all bpts on specified line.
11932 If defaulting, clear all bpts on default line
c906108c 11933 or at default pc.
c5aa993b
JM
11934
11935 defaulting sal.pc != 0 tests to do
11936
11937 0 1 pc
11938 1 1 pc _and_ line
11939 0 0 line
11940 1 0 <can't happen> */
c906108c
SS
11941
11942 sal = sals.sals[i];
05cba821
JK
11943 sal_fullname = (sal.symtab == NULL
11944 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11945
4a64f543 11946 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11947 ALL_BREAKPOINTS (b)
c5aa993b 11948 {
0d381245 11949 int match = 0;
4a64f543 11950 /* Are we going to delete b? */
cc60f2e3 11951 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11952 {
11953 struct bp_location *loc = b->loc;
11954 for (; loc; loc = loc->next)
11955 {
f8eba3c6
TT
11956 /* If the user specified file:line, don't allow a PC
11957 match. This matches historical gdb behavior. */
11958 int pc_match = (!sal.explicit_line
11959 && sal.pc
11960 && (loc->pspace == sal.pspace)
11961 && (loc->address == sal.pc)
11962 && (!section_is_overlay (loc->section)
11963 || loc->section == sal.section));
4aac40c8
TT
11964 int line_match = 0;
11965
11966 if ((default_match || sal.explicit_line)
2f202fde 11967 && loc->symtab != NULL
05cba821 11968 && sal_fullname != NULL
4aac40c8 11969 && sal.pspace == loc->pspace
05cba821
JK
11970 && loc->line_number == sal.line
11971 && filename_cmp (symtab_to_fullname (loc->symtab),
11972 sal_fullname) == 0)
11973 line_match = 1;
4aac40c8 11974
0d381245
VP
11975 if (pc_match || line_match)
11976 {
11977 match = 1;
11978 break;
11979 }
11980 }
11981 }
11982
11983 if (match)
d6e956e5 11984 VEC_safe_push(breakpoint_p, found, b);
c906108c 11985 }
80f8a6eb 11986 }
8a2c437b 11987
80f8a6eb 11988 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11989 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11990 {
11991 if (arg)
8a3fe4f8 11992 error (_("No breakpoint at %s."), arg);
80f8a6eb 11993 else
8a3fe4f8 11994 error (_("No breakpoint at this line."));
80f8a6eb 11995 }
c906108c 11996
8a2c437b
TT
11997 /* Remove duplicates from the vec. */
11998 qsort (VEC_address (breakpoint_p, found),
11999 VEC_length (breakpoint_p, found),
12000 sizeof (breakpoint_p),
12001 compare_breakpoints);
12002 prev = VEC_index (breakpoint_p, found, 0);
12003 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12004 {
12005 if (b == prev)
12006 {
12007 VEC_ordered_remove (breakpoint_p, found, ix);
12008 --ix;
12009 }
12010 }
12011
d6e956e5 12012 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12013 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12014 if (from_tty)
a3f17187 12015 {
d6e956e5 12016 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12017 printf_unfiltered (_("Deleted breakpoint "));
12018 else
12019 printf_unfiltered (_("Deleted breakpoints "));
12020 }
d6e956e5
VP
12021
12022 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12023 {
c5aa993b 12024 if (from_tty)
d6e956e5
VP
12025 printf_unfiltered ("%d ", b->number);
12026 delete_breakpoint (b);
c906108c 12027 }
80f8a6eb
MS
12028 if (from_tty)
12029 putchar_unfiltered ('\n');
8a2c437b
TT
12030
12031 do_cleanups (cleanups);
c906108c
SS
12032}
12033\f
12034/* Delete breakpoint in BS if they are `delete' breakpoints and
12035 all breakpoints that are marked for deletion, whether hit or not.
12036 This is called after any breakpoint is hit, or after errors. */
12037
12038void
fba45db2 12039breakpoint_auto_delete (bpstat bs)
c906108c 12040{
35df4500 12041 struct breakpoint *b, *b_tmp;
c906108c
SS
12042
12043 for (; bs; bs = bs->next)
f431efe5
PA
12044 if (bs->breakpoint_at
12045 && bs->breakpoint_at->disposition == disp_del
c906108c 12046 && bs->stop)
f431efe5 12047 delete_breakpoint (bs->breakpoint_at);
c906108c 12048
35df4500 12049 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12050 {
b5de0fa7 12051 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12052 delete_breakpoint (b);
12053 }
c906108c
SS
12054}
12055
4a64f543
MS
12056/* A comparison function for bp_location AP and BP being interfaced to
12057 qsort. Sort elements primarily by their ADDRESS (no matter what
12058 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12059 secondarily by ordering first permanent elements and
4a64f543 12060 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12061 qsort being an unstable algorithm. */
876fa593
JK
12062
12063static int
f5336ca5 12064bp_locations_compare (const void *ap, const void *bp)
876fa593 12065{
9a3c8263
SM
12066 const struct bp_location *a = *(const struct bp_location **) ap;
12067 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12068
12069 if (a->address != b->address)
12070 return (a->address > b->address) - (a->address < b->address);
12071
dea2aa5f
LM
12072 /* Sort locations at the same address by their pspace number, keeping
12073 locations of the same inferior (in a multi-inferior environment)
12074 grouped. */
12075
12076 if (a->pspace->num != b->pspace->num)
12077 return ((a->pspace->num > b->pspace->num)
12078 - (a->pspace->num < b->pspace->num));
12079
876fa593 12080 /* Sort permanent breakpoints first. */
1a853c52
PA
12081 if (a->permanent != b->permanent)
12082 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12083
c56a97f9
JK
12084 /* Make the internal GDB representation stable across GDB runs
12085 where A and B memory inside GDB can differ. Breakpoint locations of
12086 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12087
12088 if (a->owner->number != b->owner->number)
c56a97f9
JK
12089 return ((a->owner->number > b->owner->number)
12090 - (a->owner->number < b->owner->number));
876fa593
JK
12091
12092 return (a > b) - (a < b);
12093}
12094
f5336ca5
PA
12095/* Set bp_locations_placed_address_before_address_max and
12096 bp_locations_shadow_len_after_address_max according to the current
12097 content of the bp_locations array. */
f7545552
TT
12098
12099static void
f5336ca5 12100bp_locations_target_extensions_update (void)
f7545552 12101{
876fa593
JK
12102 struct bp_location *bl, **blp_tmp;
12103
f5336ca5
PA
12104 bp_locations_placed_address_before_address_max = 0;
12105 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12106
12107 ALL_BP_LOCATIONS (bl, blp_tmp)
12108 {
12109 CORE_ADDR start, end, addr;
12110
12111 if (!bp_location_has_shadow (bl))
12112 continue;
12113
12114 start = bl->target_info.placed_address;
12115 end = start + bl->target_info.shadow_len;
12116
12117 gdb_assert (bl->address >= start);
12118 addr = bl->address - start;
f5336ca5
PA
12119 if (addr > bp_locations_placed_address_before_address_max)
12120 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12121
12122 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12123
12124 gdb_assert (bl->address < end);
12125 addr = end - bl->address;
f5336ca5
PA
12126 if (addr > bp_locations_shadow_len_after_address_max)
12127 bp_locations_shadow_len_after_address_max = addr;
876fa593 12128 }
f7545552
TT
12129}
12130
1e4d1764
YQ
12131/* Download tracepoint locations if they haven't been. */
12132
12133static void
12134download_tracepoint_locations (void)
12135{
7ed2c994 12136 struct breakpoint *b;
dd2e65cc 12137 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 12138
5ed8105e 12139 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 12140
7ed2c994 12141 ALL_TRACEPOINTS (b)
1e4d1764 12142 {
7ed2c994 12143 struct bp_location *bl;
1e4d1764 12144 struct tracepoint *t;
f2a8bc8a 12145 int bp_location_downloaded = 0;
1e4d1764 12146
7ed2c994 12147 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12148 ? !may_insert_fast_tracepoints
12149 : !may_insert_tracepoints))
12150 continue;
12151
dd2e65cc
YQ
12152 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12153 {
12154 if (target_can_download_tracepoint ())
12155 can_download_tracepoint = TRIBOOL_TRUE;
12156 else
12157 can_download_tracepoint = TRIBOOL_FALSE;
12158 }
12159
12160 if (can_download_tracepoint == TRIBOOL_FALSE)
12161 break;
12162
7ed2c994
YQ
12163 for (bl = b->loc; bl; bl = bl->next)
12164 {
12165 /* In tracepoint, locations are _never_ duplicated, so
12166 should_be_inserted is equivalent to
12167 unduplicated_should_be_inserted. */
12168 if (!should_be_inserted (bl) || bl->inserted)
12169 continue;
1e4d1764 12170
7ed2c994 12171 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12172
7ed2c994 12173 target_download_tracepoint (bl);
1e4d1764 12174
7ed2c994 12175 bl->inserted = 1;
f2a8bc8a 12176 bp_location_downloaded = 1;
7ed2c994
YQ
12177 }
12178 t = (struct tracepoint *) b;
12179 t->number_on_target = b->number;
f2a8bc8a
YQ
12180 if (bp_location_downloaded)
12181 observer_notify_breakpoint_modified (b);
1e4d1764 12182 }
1e4d1764
YQ
12183}
12184
934709f0
PW
12185/* Swap the insertion/duplication state between two locations. */
12186
12187static void
12188swap_insertion (struct bp_location *left, struct bp_location *right)
12189{
12190 const int left_inserted = left->inserted;
12191 const int left_duplicate = left->duplicate;
b775012e 12192 const int left_needs_update = left->needs_update;
934709f0
PW
12193 const struct bp_target_info left_target_info = left->target_info;
12194
1e4d1764
YQ
12195 /* Locations of tracepoints can never be duplicated. */
12196 if (is_tracepoint (left->owner))
12197 gdb_assert (!left->duplicate);
12198 if (is_tracepoint (right->owner))
12199 gdb_assert (!right->duplicate);
12200
934709f0
PW
12201 left->inserted = right->inserted;
12202 left->duplicate = right->duplicate;
b775012e 12203 left->needs_update = right->needs_update;
934709f0
PW
12204 left->target_info = right->target_info;
12205 right->inserted = left_inserted;
12206 right->duplicate = left_duplicate;
b775012e 12207 right->needs_update = left_needs_update;
934709f0
PW
12208 right->target_info = left_target_info;
12209}
12210
b775012e
LM
12211/* Force the re-insertion of the locations at ADDRESS. This is called
12212 once a new/deleted/modified duplicate location is found and we are evaluating
12213 conditions on the target's side. Such conditions need to be updated on
12214 the target. */
12215
12216static void
12217force_breakpoint_reinsertion (struct bp_location *bl)
12218{
12219 struct bp_location **locp = NULL, **loc2p;
12220 struct bp_location *loc;
12221 CORE_ADDR address = 0;
12222 int pspace_num;
12223
12224 address = bl->address;
12225 pspace_num = bl->pspace->num;
12226
12227 /* This is only meaningful if the target is
12228 evaluating conditions and if the user has
12229 opted for condition evaluation on the target's
12230 side. */
12231 if (gdb_evaluates_breakpoint_condition_p ()
12232 || !target_supports_evaluation_of_breakpoint_conditions ())
12233 return;
12234
12235 /* Flag all breakpoint locations with this address and
12236 the same program space as the location
12237 as "its condition has changed". We need to
12238 update the conditions on the target's side. */
12239 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12240 {
12241 loc = *loc2p;
12242
12243 if (!is_breakpoint (loc->owner)
12244 || pspace_num != loc->pspace->num)
12245 continue;
12246
12247 /* Flag the location appropriately. We use a different state to
12248 let everyone know that we already updated the set of locations
12249 with addr bl->address and program space bl->pspace. This is so
12250 we don't have to keep calling these functions just to mark locations
12251 that have already been marked. */
12252 loc->condition_changed = condition_updated;
12253
12254 /* Free the agent expression bytecode as well. We will compute
12255 it later on. */
833177a4 12256 loc->cond_bytecode.reset ();
b775012e
LM
12257 }
12258}
44702360
PA
12259/* Called whether new breakpoints are created, or existing breakpoints
12260 deleted, to update the global location list and recompute which
12261 locations are duplicate of which.
b775012e 12262
04086b45
PA
12263 The INSERT_MODE flag determines whether locations may not, may, or
12264 shall be inserted now. See 'enum ugll_insert_mode' for more
12265 info. */
b60e7edf 12266
0d381245 12267static void
44702360 12268update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12269{
74960c60 12270 struct breakpoint *b;
876fa593 12271 struct bp_location **locp, *loc;
f7545552 12272 struct cleanup *cleanups;
b775012e
LM
12273 /* Last breakpoint location address that was marked for update. */
12274 CORE_ADDR last_addr = 0;
12275 /* Last breakpoint location program space that was marked for update. */
12276 int last_pspace_num = -1;
f7545552 12277
2d134ed3
PA
12278 /* Used in the duplicates detection below. When iterating over all
12279 bp_locations, points to the first bp_location of a given address.
12280 Breakpoints and watchpoints of different types are never
12281 duplicates of each other. Keep one pointer for each type of
12282 breakpoint/watchpoint, so we only need to loop over all locations
12283 once. */
12284 struct bp_location *bp_loc_first; /* breakpoint */
12285 struct bp_location *wp_loc_first; /* hardware watchpoint */
12286 struct bp_location *awp_loc_first; /* access watchpoint */
12287 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12288
f5336ca5
PA
12289 /* Saved former bp_locations array which we compare against the newly
12290 built bp_locations from the current state of ALL_BREAKPOINTS. */
12291 struct bp_location **old_locations, **old_locp;
12292 unsigned old_locations_count;
876fa593 12293
f5336ca5
PA
12294 old_locations = bp_locations;
12295 old_locations_count = bp_locations_count;
12296 bp_locations = NULL;
12297 bp_locations_count = 0;
12298 cleanups = make_cleanup (xfree, old_locations);
0d381245 12299
74960c60 12300 ALL_BREAKPOINTS (b)
876fa593 12301 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12302 bp_locations_count++;
876fa593 12303
f5336ca5
PA
12304 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12305 locp = bp_locations;
876fa593
JK
12306 ALL_BREAKPOINTS (b)
12307 for (loc = b->loc; loc; loc = loc->next)
12308 *locp++ = loc;
f5336ca5
PA
12309 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12310 bp_locations_compare);
876fa593 12311
f5336ca5 12312 bp_locations_target_extensions_update ();
74960c60 12313
4a64f543
MS
12314 /* Identify bp_location instances that are no longer present in the
12315 new list, and therefore should be freed. Note that it's not
12316 necessary that those locations should be removed from inferior --
12317 if there's another location at the same address (previously
12318 marked as duplicate), we don't need to remove/insert the
12319 location.
876fa593 12320
4a64f543
MS
12321 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12322 and former bp_location array state respectively. */
876fa593 12323
f5336ca5
PA
12324 locp = bp_locations;
12325 for (old_locp = old_locations;
12326 old_locp < old_locations + old_locations_count;
876fa593 12327 old_locp++)
74960c60 12328 {
876fa593 12329 struct bp_location *old_loc = *old_locp;
c7d46a38 12330 struct bp_location **loc2p;
876fa593 12331
e5dd4106 12332 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12333 not, we have to free it. */
c7d46a38 12334 int found_object = 0;
20874c92
VP
12335 /* Tells if the location should remain inserted in the target. */
12336 int keep_in_target = 0;
12337 int removed = 0;
876fa593 12338
4a64f543
MS
12339 /* Skip LOCP entries which will definitely never be needed.
12340 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12341 while (locp < bp_locations + bp_locations_count
c7d46a38 12342 && (*locp)->address < old_loc->address)
876fa593 12343 locp++;
c7d46a38
PA
12344
12345 for (loc2p = locp;
f5336ca5 12346 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12347 && (*loc2p)->address == old_loc->address);
12348 loc2p++)
12349 {
b775012e
LM
12350 /* Check if this is a new/duplicated location or a duplicated
12351 location that had its condition modified. If so, we want to send
12352 its condition to the target if evaluation of conditions is taking
12353 place there. */
12354 if ((*loc2p)->condition_changed == condition_modified
12355 && (last_addr != old_loc->address
12356 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12357 {
b775012e
LM
12358 force_breakpoint_reinsertion (*loc2p);
12359 last_pspace_num = old_loc->pspace->num;
c7d46a38 12360 }
b775012e
LM
12361
12362 if (*loc2p == old_loc)
12363 found_object = 1;
c7d46a38 12364 }
74960c60 12365
b775012e
LM
12366 /* We have already handled this address, update it so that we don't
12367 have to go through updates again. */
12368 last_addr = old_loc->address;
12369
12370 /* Target-side condition evaluation: Handle deleted locations. */
12371 if (!found_object)
12372 force_breakpoint_reinsertion (old_loc);
12373
4a64f543
MS
12374 /* If this location is no longer present, and inserted, look if
12375 there's maybe a new location at the same address. If so,
12376 mark that one inserted, and don't remove this one. This is
12377 needed so that we don't have a time window where a breakpoint
12378 at certain location is not inserted. */
74960c60 12379
876fa593 12380 if (old_loc->inserted)
0d381245 12381 {
4a64f543
MS
12382 /* If the location is inserted now, we might have to remove
12383 it. */
74960c60 12384
876fa593 12385 if (found_object && should_be_inserted (old_loc))
74960c60 12386 {
4a64f543
MS
12387 /* The location is still present in the location list,
12388 and still should be inserted. Don't do anything. */
20874c92 12389 keep_in_target = 1;
74960c60
VP
12390 }
12391 else
12392 {
b775012e
LM
12393 /* This location still exists, but it won't be kept in the
12394 target since it may have been disabled. We proceed to
12395 remove its target-side condition. */
12396
4a64f543
MS
12397 /* The location is either no longer present, or got
12398 disabled. See if there's another location at the
12399 same address, in which case we don't need to remove
12400 this one from the target. */
876fa593 12401
2bdf28a0 12402 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12403 if (breakpoint_address_is_meaningful (old_loc->owner))
12404 {
876fa593 12405 for (loc2p = locp;
f5336ca5 12406 (loc2p < bp_locations + bp_locations_count
c7d46a38 12407 && (*loc2p)->address == old_loc->address);
876fa593
JK
12408 loc2p++)
12409 {
12410 struct bp_location *loc2 = *loc2p;
12411
2d134ed3 12412 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12413 {
85d721b8
PA
12414 /* Read watchpoint locations are switched to
12415 access watchpoints, if the former are not
12416 supported, but the latter are. */
12417 if (is_hardware_watchpoint (old_loc->owner))
12418 {
12419 gdb_assert (is_hardware_watchpoint (loc2->owner));
12420 loc2->watchpoint_type = old_loc->watchpoint_type;
12421 }
12422
934709f0
PW
12423 /* loc2 is a duplicated location. We need to check
12424 if it should be inserted in case it will be
12425 unduplicated. */
12426 if (loc2 != old_loc
12427 && unduplicated_should_be_inserted (loc2))
c7d46a38 12428 {
934709f0 12429 swap_insertion (old_loc, loc2);
c7d46a38
PA
12430 keep_in_target = 1;
12431 break;
12432 }
876fa593
JK
12433 }
12434 }
12435 }
74960c60
VP
12436 }
12437
20874c92
VP
12438 if (!keep_in_target)
12439 {
834c0d03 12440 if (remove_breakpoint (old_loc))
20874c92 12441 {
4a64f543
MS
12442 /* This is just about all we can do. We could keep
12443 this location on the global list, and try to
12444 remove it next time, but there's no particular
12445 reason why we will succeed next time.
20874c92 12446
4a64f543
MS
12447 Note that at this point, old_loc->owner is still
12448 valid, as delete_breakpoint frees the breakpoint
12449 only after calling us. */
3e43a32a
MS
12450 printf_filtered (_("warning: Error removing "
12451 "breakpoint %d\n"),
876fa593 12452 old_loc->owner->number);
20874c92
VP
12453 }
12454 removed = 1;
12455 }
0d381245 12456 }
74960c60
VP
12457
12458 if (!found_object)
1c5cfe86 12459 {
fbea99ea 12460 if (removed && target_is_non_stop_p ()
1cf4d951 12461 && need_moribund_for_location_type (old_loc))
20874c92 12462 {
db82e815
PA
12463 /* This location was removed from the target. In
12464 non-stop mode, a race condition is possible where
12465 we've removed a breakpoint, but stop events for that
12466 breakpoint are already queued and will arrive later.
12467 We apply an heuristic to be able to distinguish such
12468 SIGTRAPs from other random SIGTRAPs: we keep this
12469 breakpoint location for a bit, and will retire it
12470 after we see some number of events. The theory here
12471 is that reporting of events should, "on the average",
12472 be fair, so after a while we'll see events from all
12473 threads that have anything of interest, and no longer
12474 need to keep this breakpoint location around. We
12475 don't hold locations forever so to reduce chances of
12476 mistaking a non-breakpoint SIGTRAP for a breakpoint
12477 SIGTRAP.
12478
12479 The heuristic failing can be disastrous on
12480 decr_pc_after_break targets.
12481
12482 On decr_pc_after_break targets, like e.g., x86-linux,
12483 if we fail to recognize a late breakpoint SIGTRAP,
12484 because events_till_retirement has reached 0 too
12485 soon, we'll fail to do the PC adjustment, and report
12486 a random SIGTRAP to the user. When the user resumes
12487 the inferior, it will most likely immediately crash
2dec564e 12488 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12489 corrupted, because of being resumed e.g., in the
12490 middle of a multi-byte instruction, or skipped a
12491 one-byte instruction. This was actually seen happen
12492 on native x86-linux, and should be less rare on
12493 targets that do not support new thread events, like
12494 remote, due to the heuristic depending on
12495 thread_count.
12496
12497 Mistaking a random SIGTRAP for a breakpoint trap
12498 causes similar symptoms (PC adjustment applied when
12499 it shouldn't), but then again, playing with SIGTRAPs
12500 behind the debugger's back is asking for trouble.
12501
12502 Since hardware watchpoint traps are always
12503 distinguishable from other traps, so we don't need to
12504 apply keep hardware watchpoint moribund locations
12505 around. We simply always ignore hardware watchpoint
12506 traps we can no longer explain. */
12507
876fa593
JK
12508 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12509 old_loc->owner = NULL;
20874c92 12510
876fa593 12511 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12512 }
12513 else
f431efe5
PA
12514 {
12515 old_loc->owner = NULL;
12516 decref_bp_location (&old_loc);
12517 }
20874c92 12518 }
74960c60 12519 }
1c5cfe86 12520
348d480f
PA
12521 /* Rescan breakpoints at the same address and section, marking the
12522 first one as "first" and any others as "duplicates". This is so
12523 that the bpt instruction is only inserted once. If we have a
12524 permanent breakpoint at the same place as BPT, make that one the
12525 official one, and the rest as duplicates. Permanent breakpoints
12526 are sorted first for the same address.
12527
12528 Do the same for hardware watchpoints, but also considering the
12529 watchpoint's type (regular/access/read) and length. */
12530
12531 bp_loc_first = NULL;
12532 wp_loc_first = NULL;
12533 awp_loc_first = NULL;
12534 rwp_loc_first = NULL;
12535 ALL_BP_LOCATIONS (loc, locp)
12536 {
12537 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12538 non-NULL. */
348d480f 12539 struct bp_location **loc_first_p;
d3fbdd86 12540 b = loc->owner;
348d480f 12541
6f380991 12542 if (!unduplicated_should_be_inserted (loc)
348d480f 12543 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12544 /* Don't detect duplicate for tracepoint locations because they are
12545 never duplicated. See the comments in field `duplicate' of
12546 `struct bp_location'. */
348d480f 12547 || is_tracepoint (b))
b775012e
LM
12548 {
12549 /* Clear the condition modification flag. */
12550 loc->condition_changed = condition_unchanged;
12551 continue;
12552 }
348d480f 12553
348d480f
PA
12554 if (b->type == bp_hardware_watchpoint)
12555 loc_first_p = &wp_loc_first;
12556 else if (b->type == bp_read_watchpoint)
12557 loc_first_p = &rwp_loc_first;
12558 else if (b->type == bp_access_watchpoint)
12559 loc_first_p = &awp_loc_first;
12560 else
12561 loc_first_p = &bp_loc_first;
12562
12563 if (*loc_first_p == NULL
12564 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12565 || !breakpoint_locations_match (loc, *loc_first_p))
12566 {
12567 *loc_first_p = loc;
12568 loc->duplicate = 0;
b775012e
LM
12569
12570 if (is_breakpoint (loc->owner) && loc->condition_changed)
12571 {
12572 loc->needs_update = 1;
12573 /* Clear the condition modification flag. */
12574 loc->condition_changed = condition_unchanged;
12575 }
348d480f
PA
12576 continue;
12577 }
12578
934709f0
PW
12579
12580 /* This and the above ensure the invariant that the first location
12581 is not duplicated, and is the inserted one.
12582 All following are marked as duplicated, and are not inserted. */
12583 if (loc->inserted)
12584 swap_insertion (loc, *loc_first_p);
348d480f
PA
12585 loc->duplicate = 1;
12586
b775012e
LM
12587 /* Clear the condition modification flag. */
12588 loc->condition_changed = condition_unchanged;
348d480f
PA
12589 }
12590
a25a5a45 12591 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12592 {
04086b45 12593 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12594 insert_breakpoint_locations ();
12595 else
12596 {
44702360
PA
12597 /* Even though the caller told us to not insert new
12598 locations, we may still need to update conditions on the
12599 target's side of breakpoints that were already inserted
12600 if the target is evaluating breakpoint conditions. We
b775012e
LM
12601 only update conditions for locations that are marked
12602 "needs_update". */
12603 update_inserted_breakpoint_locations ();
12604 }
12605 }
348d480f 12606
04086b45 12607 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12608 download_tracepoint_locations ();
12609
348d480f
PA
12610 do_cleanups (cleanups);
12611}
12612
12613void
12614breakpoint_retire_moribund (void)
12615{
12616 struct bp_location *loc;
12617 int ix;
12618
12619 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12620 if (--(loc->events_till_retirement) == 0)
12621 {
12622 decref_bp_location (&loc);
12623 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12624 --ix;
12625 }
12626}
12627
12628static void
44702360 12629update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12630{
348d480f 12631
492d29ea
PA
12632 TRY
12633 {
12634 update_global_location_list (insert_mode);
12635 }
12636 CATCH (e, RETURN_MASK_ERROR)
12637 {
12638 }
12639 END_CATCH
348d480f
PA
12640}
12641
12642/* Clear BKP from a BPS. */
12643
12644static void
12645bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12646{
12647 bpstat bs;
12648
12649 for (bs = bps; bs; bs = bs->next)
12650 if (bs->breakpoint_at == bpt)
12651 {
12652 bs->breakpoint_at = NULL;
12653 bs->old_val = NULL;
12654 /* bs->commands will be freed later. */
12655 }
12656}
12657
12658/* Callback for iterate_over_threads. */
12659static int
12660bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12661{
9a3c8263 12662 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12663
12664 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12665 return 0;
12666}
12667
12668/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12669 callbacks. */
12670
12671static void
12672say_where (struct breakpoint *b)
12673{
12674 struct value_print_options opts;
12675
12676 get_user_print_options (&opts);
12677
12678 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12679 single string. */
12680 if (b->loc == NULL)
12681 {
f00aae0f
KS
12682 /* For pending locations, the output differs slightly based
12683 on b->extra_string. If this is non-NULL, it contains either
12684 a condition or dprintf arguments. */
12685 if (b->extra_string == NULL)
12686 {
12687 printf_filtered (_(" (%s) pending."),
d28cd78a 12688 event_location_to_string (b->location.get ()));
f00aae0f
KS
12689 }
12690 else if (b->type == bp_dprintf)
12691 {
12692 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12693 event_location_to_string (b->location.get ()),
f00aae0f
KS
12694 b->extra_string);
12695 }
12696 else
12697 {
12698 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12699 event_location_to_string (b->location.get ()),
f00aae0f
KS
12700 b->extra_string);
12701 }
348d480f
PA
12702 }
12703 else
12704 {
2f202fde 12705 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12706 {
12707 printf_filtered (" at ");
12708 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12709 gdb_stdout);
12710 }
2f202fde 12711 if (b->loc->symtab != NULL)
f8eba3c6
TT
12712 {
12713 /* If there is a single location, we can print the location
12714 more nicely. */
12715 if (b->loc->next == NULL)
12716 printf_filtered (": file %s, line %d.",
05cba821
JK
12717 symtab_to_filename_for_display (b->loc->symtab),
12718 b->loc->line_number);
f8eba3c6
TT
12719 else
12720 /* This is not ideal, but each location may have a
12721 different file name, and this at least reflects the
12722 real situation somewhat. */
f00aae0f 12723 printf_filtered (": %s.",
d28cd78a 12724 event_location_to_string (b->location.get ()));
f8eba3c6 12725 }
348d480f
PA
12726
12727 if (b->loc->next)
12728 {
12729 struct bp_location *loc = b->loc;
12730 int n = 0;
12731 for (; loc; loc = loc->next)
12732 ++n;
12733 printf_filtered (" (%d locations)", n);
12734 }
12735 }
12736}
12737
348d480f
PA
12738/* Default bp_location_ops methods. */
12739
12740static void
12741bp_location_dtor (struct bp_location *self)
12742{
348d480f
PA
12743 xfree (self->function_name);
12744}
12745
12746static const struct bp_location_ops bp_location_ops =
12747{
12748 bp_location_dtor
12749};
12750
2060206e
PA
12751/* Default breakpoint_ops methods all breakpoint_ops ultimately
12752 inherit from. */
348d480f 12753
2060206e
PA
12754static void
12755base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12756{
12757 decref_counted_command_line (&self->commands);
12758 xfree (self->cond_string);
fb81d016 12759 xfree (self->extra_string);
f8eba3c6 12760 xfree (self->filter);
348d480f
PA
12761}
12762
2060206e
PA
12763static struct bp_location *
12764base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12765{
5625a286 12766 return new bp_location (&bp_location_ops, self);
348d480f
PA
12767}
12768
2060206e
PA
12769static void
12770base_breakpoint_re_set (struct breakpoint *b)
12771{
12772 /* Nothing to re-set. */
12773}
12774
12775#define internal_error_pure_virtual_called() \
12776 gdb_assert_not_reached ("pure virtual function called")
12777
12778static int
12779base_breakpoint_insert_location (struct bp_location *bl)
12780{
12781 internal_error_pure_virtual_called ();
12782}
12783
12784static int
73971819
PA
12785base_breakpoint_remove_location (struct bp_location *bl,
12786 enum remove_bp_reason reason)
2060206e
PA
12787{
12788 internal_error_pure_virtual_called ();
12789}
12790
12791static int
12792base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12793 struct address_space *aspace,
09ac7c10
TT
12794 CORE_ADDR bp_addr,
12795 const struct target_waitstatus *ws)
2060206e
PA
12796{
12797 internal_error_pure_virtual_called ();
12798}
12799
12800static void
12801base_breakpoint_check_status (bpstat bs)
12802{
12803 /* Always stop. */
12804}
12805
12806/* A "works_in_software_mode" breakpoint_ops method that just internal
12807 errors. */
12808
12809static int
12810base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12811{
12812 internal_error_pure_virtual_called ();
12813}
12814
12815/* A "resources_needed" breakpoint_ops method that just internal
12816 errors. */
12817
12818static int
12819base_breakpoint_resources_needed (const struct bp_location *bl)
12820{
12821 internal_error_pure_virtual_called ();
12822}
12823
12824static enum print_stop_action
12825base_breakpoint_print_it (bpstat bs)
12826{
12827 internal_error_pure_virtual_called ();
12828}
12829
12830static void
12831base_breakpoint_print_one_detail (const struct breakpoint *self,
12832 struct ui_out *uiout)
12833{
12834 /* nothing */
12835}
12836
12837static void
12838base_breakpoint_print_mention (struct breakpoint *b)
12839{
12840 internal_error_pure_virtual_called ();
12841}
12842
12843static void
12844base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12845{
12846 internal_error_pure_virtual_called ();
12847}
12848
983af33b 12849static void
f00aae0f
KS
12850base_breakpoint_create_sals_from_location
12851 (const struct event_location *location,
12852 struct linespec_result *canonical,
12853 enum bptype type_wanted)
983af33b
SDJ
12854{
12855 internal_error_pure_virtual_called ();
12856}
12857
12858static void
12859base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12860 struct linespec_result *c,
983af33b 12861 char *cond_string,
e7e0cddf 12862 char *extra_string,
983af33b
SDJ
12863 enum bptype type_wanted,
12864 enum bpdisp disposition,
12865 int thread,
12866 int task, int ignore_count,
12867 const struct breakpoint_ops *o,
12868 int from_tty, int enabled,
44f238bb 12869 int internal, unsigned flags)
983af33b
SDJ
12870{
12871 internal_error_pure_virtual_called ();
12872}
12873
12874static void
f00aae0f
KS
12875base_breakpoint_decode_location (struct breakpoint *b,
12876 const struct event_location *location,
c2f4122d 12877 struct program_space *search_pspace,
983af33b
SDJ
12878 struct symtabs_and_lines *sals)
12879{
12880 internal_error_pure_virtual_called ();
12881}
12882
ab04a2af
TT
12883/* The default 'explains_signal' method. */
12884
47591c29 12885static int
427cd150 12886base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12887{
47591c29 12888 return 1;
ab04a2af
TT
12889}
12890
9d6e6e84
HZ
12891/* The default "after_condition_true" method. */
12892
12893static void
12894base_breakpoint_after_condition_true (struct bpstats *bs)
12895{
12896 /* Nothing to do. */
12897}
12898
ab04a2af 12899struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12900{
12901 base_breakpoint_dtor,
12902 base_breakpoint_allocate_location,
12903 base_breakpoint_re_set,
12904 base_breakpoint_insert_location,
12905 base_breakpoint_remove_location,
12906 base_breakpoint_breakpoint_hit,
12907 base_breakpoint_check_status,
12908 base_breakpoint_resources_needed,
12909 base_breakpoint_works_in_software_mode,
12910 base_breakpoint_print_it,
12911 NULL,
12912 base_breakpoint_print_one_detail,
12913 base_breakpoint_print_mention,
983af33b 12914 base_breakpoint_print_recreate,
5f700d83 12915 base_breakpoint_create_sals_from_location,
983af33b 12916 base_breakpoint_create_breakpoints_sal,
5f700d83 12917 base_breakpoint_decode_location,
9d6e6e84
HZ
12918 base_breakpoint_explains_signal,
12919 base_breakpoint_after_condition_true,
2060206e
PA
12920};
12921
12922/* Default breakpoint_ops methods. */
12923
12924static void
348d480f
PA
12925bkpt_re_set (struct breakpoint *b)
12926{
06edf0c0 12927 /* FIXME: is this still reachable? */
9ef9e6a6 12928 if (breakpoint_event_location_empty_p (b))
06edf0c0 12929 {
f00aae0f 12930 /* Anything without a location can't be re-set. */
348d480f 12931 delete_breakpoint (b);
06edf0c0 12932 return;
348d480f 12933 }
06edf0c0
PA
12934
12935 breakpoint_re_set_default (b);
348d480f
PA
12936}
12937
2060206e 12938static int
348d480f
PA
12939bkpt_insert_location (struct bp_location *bl)
12940{
cd6c3b4f
YQ
12941 CORE_ADDR addr = bl->target_info.reqstd_address;
12942
579c6ad9 12943 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12944 bl->target_info.placed_address = addr;
12945
348d480f 12946 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12947 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12948 else
7c16b83e 12949 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12950}
12951
2060206e 12952static int
73971819 12953bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12954{
12955 if (bl->loc_type == bp_loc_hardware_breakpoint)
12956 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12957 else
73971819 12958 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12959}
12960
2060206e 12961static int
348d480f 12962bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12963 struct address_space *aspace, CORE_ADDR bp_addr,
12964 const struct target_waitstatus *ws)
348d480f 12965{
09ac7c10 12966 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12967 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12968 return 0;
12969
348d480f
PA
12970 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12971 aspace, bp_addr))
12972 return 0;
12973
12974 if (overlay_debugging /* unmapped overlay section */
12975 && section_is_overlay (bl->section)
12976 && !section_is_mapped (bl->section))
12977 return 0;
12978
12979 return 1;
12980}
12981
cd1608cc
PA
12982static int
12983dprintf_breakpoint_hit (const struct bp_location *bl,
12984 struct address_space *aspace, CORE_ADDR bp_addr,
12985 const struct target_waitstatus *ws)
12986{
12987 if (dprintf_style == dprintf_style_agent
12988 && target_can_run_breakpoint_commands ())
12989 {
12990 /* An agent-style dprintf never causes a stop. If we see a trap
12991 for this address it must be for a breakpoint that happens to
12992 be set at the same address. */
12993 return 0;
12994 }
12995
12996 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12997}
12998
2060206e 12999static int
348d480f
PA
13000bkpt_resources_needed (const struct bp_location *bl)
13001{
13002 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13003
13004 return 1;
13005}
13006
2060206e 13007static enum print_stop_action
348d480f
PA
13008bkpt_print_it (bpstat bs)
13009{
348d480f
PA
13010 struct breakpoint *b;
13011 const struct bp_location *bl;
001c8c33 13012 int bp_temp;
79a45e25 13013 struct ui_out *uiout = current_uiout;
348d480f
PA
13014
13015 gdb_assert (bs->bp_location_at != NULL);
13016
13017 bl = bs->bp_location_at;
13018 b = bs->breakpoint_at;
13019
001c8c33
PA
13020 bp_temp = b->disposition == disp_del;
13021 if (bl->address != bl->requested_address)
13022 breakpoint_adjustment_warning (bl->requested_address,
13023 bl->address,
13024 b->number, 1);
13025 annotate_breakpoint (b->number);
f303dbd6
PA
13026 maybe_print_thread_hit_breakpoint (uiout);
13027
001c8c33 13028 if (bp_temp)
112e8700 13029 uiout->text ("Temporary breakpoint ");
001c8c33 13030 else
112e8700
SM
13031 uiout->text ("Breakpoint ");
13032 if (uiout->is_mi_like_p ())
348d480f 13033 {
112e8700 13034 uiout->field_string ("reason",
001c8c33 13035 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 13036 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 13037 }
112e8700
SM
13038 uiout->field_int ("bkptno", b->number);
13039 uiout->text (", ");
06edf0c0 13040
001c8c33 13041 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13042}
13043
2060206e 13044static void
06edf0c0
PA
13045bkpt_print_mention (struct breakpoint *b)
13046{
112e8700 13047 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
13048 return;
13049
13050 switch (b->type)
13051 {
13052 case bp_breakpoint:
13053 case bp_gnu_ifunc_resolver:
13054 if (b->disposition == disp_del)
13055 printf_filtered (_("Temporary breakpoint"));
13056 else
13057 printf_filtered (_("Breakpoint"));
13058 printf_filtered (_(" %d"), b->number);
13059 if (b->type == bp_gnu_ifunc_resolver)
13060 printf_filtered (_(" at gnu-indirect-function resolver"));
13061 break;
13062 case bp_hardware_breakpoint:
13063 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13064 break;
e7e0cddf
SS
13065 case bp_dprintf:
13066 printf_filtered (_("Dprintf %d"), b->number);
13067 break;
06edf0c0
PA
13068 }
13069
13070 say_where (b);
13071}
13072
2060206e 13073static void
06edf0c0
PA
13074bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13075{
13076 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13077 fprintf_unfiltered (fp, "tbreak");
13078 else if (tp->type == bp_breakpoint)
13079 fprintf_unfiltered (fp, "break");
13080 else if (tp->type == bp_hardware_breakpoint
13081 && tp->disposition == disp_del)
13082 fprintf_unfiltered (fp, "thbreak");
13083 else if (tp->type == bp_hardware_breakpoint)
13084 fprintf_unfiltered (fp, "hbreak");
13085 else
13086 internal_error (__FILE__, __LINE__,
13087 _("unhandled breakpoint type %d"), (int) tp->type);
13088
f00aae0f 13089 fprintf_unfiltered (fp, " %s",
d28cd78a 13090 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13091
13092 /* Print out extra_string if this breakpoint is pending. It might
13093 contain, for example, conditions that were set by the user. */
13094 if (tp->loc == NULL && tp->extra_string != NULL)
13095 fprintf_unfiltered (fp, " %s", tp->extra_string);
13096
dd11a36c 13097 print_recreate_thread (tp, fp);
06edf0c0
PA
13098}
13099
983af33b 13100static void
f00aae0f
KS
13101bkpt_create_sals_from_location (const struct event_location *location,
13102 struct linespec_result *canonical,
13103 enum bptype type_wanted)
983af33b 13104{
f00aae0f 13105 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13106}
13107
13108static void
13109bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13110 struct linespec_result *canonical,
983af33b 13111 char *cond_string,
e7e0cddf 13112 char *extra_string,
983af33b
SDJ
13113 enum bptype type_wanted,
13114 enum bpdisp disposition,
13115 int thread,
13116 int task, int ignore_count,
13117 const struct breakpoint_ops *ops,
13118 int from_tty, int enabled,
44f238bb 13119 int internal, unsigned flags)
983af33b 13120{
023fa29b 13121 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13122 cond_string, extra_string,
13123 type_wanted,
983af33b
SDJ
13124 disposition, thread, task,
13125 ignore_count, ops, from_tty,
44f238bb 13126 enabled, internal, flags);
983af33b
SDJ
13127}
13128
13129static void
f00aae0f
KS
13130bkpt_decode_location (struct breakpoint *b,
13131 const struct event_location *location,
c2f4122d 13132 struct program_space *search_pspace,
983af33b
SDJ
13133 struct symtabs_and_lines *sals)
13134{
c2f4122d 13135 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13136}
13137
06edf0c0
PA
13138/* Virtual table for internal breakpoints. */
13139
13140static void
13141internal_bkpt_re_set (struct breakpoint *b)
13142{
13143 switch (b->type)
13144 {
13145 /* Delete overlay event and longjmp master breakpoints; they
13146 will be reset later by breakpoint_re_set. */
13147 case bp_overlay_event:
13148 case bp_longjmp_master:
13149 case bp_std_terminate_master:
13150 case bp_exception_master:
13151 delete_breakpoint (b);
13152 break;
13153
13154 /* This breakpoint is special, it's set up when the inferior
13155 starts and we really don't want to touch it. */
13156 case bp_shlib_event:
13157
13158 /* Like bp_shlib_event, this breakpoint type is special. Once
13159 it is set up, we do not want to touch it. */
13160 case bp_thread_event:
13161 break;
13162 }
13163}
13164
13165static void
13166internal_bkpt_check_status (bpstat bs)
13167{
a9b3a50f
PA
13168 if (bs->breakpoint_at->type == bp_shlib_event)
13169 {
13170 /* If requested, stop when the dynamic linker notifies GDB of
13171 events. This allows the user to get control and place
13172 breakpoints in initializer routines for dynamically loaded
13173 objects (among other things). */
13174 bs->stop = stop_on_solib_events;
13175 bs->print = stop_on_solib_events;
13176 }
13177 else
13178 bs->stop = 0;
06edf0c0
PA
13179}
13180
13181static enum print_stop_action
13182internal_bkpt_print_it (bpstat bs)
13183{
06edf0c0 13184 struct breakpoint *b;
06edf0c0 13185
06edf0c0
PA
13186 b = bs->breakpoint_at;
13187
06edf0c0
PA
13188 switch (b->type)
13189 {
348d480f
PA
13190 case bp_shlib_event:
13191 /* Did we stop because the user set the stop_on_solib_events
13192 variable? (If so, we report this as a generic, "Stopped due
13193 to shlib event" message.) */
edcc5120 13194 print_solib_event (0);
348d480f
PA
13195 break;
13196
13197 case bp_thread_event:
13198 /* Not sure how we will get here.
13199 GDB should not stop for these breakpoints. */
13200 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13201 break;
13202
13203 case bp_overlay_event:
13204 /* By analogy with the thread event, GDB should not stop for these. */
13205 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13206 break;
13207
13208 case bp_longjmp_master:
13209 /* These should never be enabled. */
13210 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13211 break;
13212
13213 case bp_std_terminate_master:
13214 /* These should never be enabled. */
13215 printf_filtered (_("std::terminate Master Breakpoint: "
13216 "gdb should not stop!\n"));
348d480f
PA
13217 break;
13218
13219 case bp_exception_master:
13220 /* These should never be enabled. */
13221 printf_filtered (_("Exception Master Breakpoint: "
13222 "gdb should not stop!\n"));
06edf0c0
PA
13223 break;
13224 }
13225
001c8c33 13226 return PRINT_NOTHING;
06edf0c0
PA
13227}
13228
13229static void
13230internal_bkpt_print_mention (struct breakpoint *b)
13231{
13232 /* Nothing to mention. These breakpoints are internal. */
13233}
13234
06edf0c0
PA
13235/* Virtual table for momentary breakpoints */
13236
13237static void
13238momentary_bkpt_re_set (struct breakpoint *b)
13239{
13240 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13241 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13242 Otherwise these should have been blown away via the cleanup chain
13243 or by breakpoint_init_inferior when we rerun the executable. */
13244}
13245
13246static void
13247momentary_bkpt_check_status (bpstat bs)
13248{
13249 /* Nothing. The point of these breakpoints is causing a stop. */
13250}
13251
13252static enum print_stop_action
13253momentary_bkpt_print_it (bpstat bs)
13254{
001c8c33 13255 return PRINT_UNKNOWN;
348d480f
PA
13256}
13257
06edf0c0
PA
13258static void
13259momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13260{
06edf0c0 13261 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13262}
13263
e2e4d78b
JK
13264/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13265
13266 It gets cleared already on the removal of the first one of such placed
13267 breakpoints. This is OK as they get all removed altogether. */
13268
13269static void
13270longjmp_bkpt_dtor (struct breakpoint *self)
13271{
5d5658a1 13272 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13273
13274 if (tp)
13275 tp->initiating_frame = null_frame_id;
13276
13277 momentary_breakpoint_ops.dtor (self);
13278}
13279
55aa24fb
SDJ
13280/* Specific methods for probe breakpoints. */
13281
13282static int
13283bkpt_probe_insert_location (struct bp_location *bl)
13284{
13285 int v = bkpt_insert_location (bl);
13286
13287 if (v == 0)
13288 {
13289 /* The insertion was successful, now let's set the probe's semaphore
13290 if needed. */
0ea5cda8
SDJ
13291 if (bl->probe.probe->pops->set_semaphore != NULL)
13292 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13293 bl->probe.objfile,
13294 bl->gdbarch);
55aa24fb
SDJ
13295 }
13296
13297 return v;
13298}
13299
13300static int
73971819
PA
13301bkpt_probe_remove_location (struct bp_location *bl,
13302 enum remove_bp_reason reason)
55aa24fb
SDJ
13303{
13304 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13305 if (bl->probe.probe->pops->clear_semaphore != NULL)
13306 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13307 bl->probe.objfile,
13308 bl->gdbarch);
55aa24fb 13309
73971819 13310 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13311}
13312
13313static void
f00aae0f 13314bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13315 struct linespec_result *canonical,
f00aae0f 13316 enum bptype type_wanted)
55aa24fb
SDJ
13317{
13318 struct linespec_sals lsal;
13319
c2f4122d 13320 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13321 lsal.canonical
13322 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13323 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13324}
13325
13326static void
f00aae0f
KS
13327bkpt_probe_decode_location (struct breakpoint *b,
13328 const struct event_location *location,
c2f4122d 13329 struct program_space *search_pspace,
55aa24fb
SDJ
13330 struct symtabs_and_lines *sals)
13331{
c2f4122d 13332 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13333 if (!sals->sals)
13334 error (_("probe not found"));
13335}
13336
348d480f 13337/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13338
348d480f
PA
13339static void
13340tracepoint_re_set (struct breakpoint *b)
13341{
13342 breakpoint_re_set_default (b);
13343}
876fa593 13344
348d480f
PA
13345static int
13346tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13347 struct address_space *aspace, CORE_ADDR bp_addr,
13348 const struct target_waitstatus *ws)
348d480f
PA
13349{
13350 /* By definition, the inferior does not report stops at
13351 tracepoints. */
13352 return 0;
74960c60
VP
13353}
13354
13355static void
348d480f
PA
13356tracepoint_print_one_detail (const struct breakpoint *self,
13357 struct ui_out *uiout)
74960c60 13358{
d9b3f62e
PA
13359 struct tracepoint *tp = (struct tracepoint *) self;
13360 if (tp->static_trace_marker_id)
348d480f
PA
13361 {
13362 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13363
112e8700
SM
13364 uiout->text ("\tmarker id is ");
13365 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13366 tp->static_trace_marker_id);
112e8700 13367 uiout->text ("\n");
348d480f 13368 }
0d381245
VP
13369}
13370
a474d7c2 13371static void
348d480f 13372tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13373{
112e8700 13374 if (current_uiout->is_mi_like_p ())
348d480f 13375 return;
cc59ec59 13376
348d480f
PA
13377 switch (b->type)
13378 {
13379 case bp_tracepoint:
13380 printf_filtered (_("Tracepoint"));
13381 printf_filtered (_(" %d"), b->number);
13382 break;
13383 case bp_fast_tracepoint:
13384 printf_filtered (_("Fast tracepoint"));
13385 printf_filtered (_(" %d"), b->number);
13386 break;
13387 case bp_static_tracepoint:
13388 printf_filtered (_("Static tracepoint"));
13389 printf_filtered (_(" %d"), b->number);
13390 break;
13391 default:
13392 internal_error (__FILE__, __LINE__,
13393 _("unhandled tracepoint type %d"), (int) b->type);
13394 }
13395
13396 say_where (b);
a474d7c2
PA
13397}
13398
348d480f 13399static void
d9b3f62e 13400tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13401{
d9b3f62e
PA
13402 struct tracepoint *tp = (struct tracepoint *) self;
13403
13404 if (self->type == bp_fast_tracepoint)
348d480f 13405 fprintf_unfiltered (fp, "ftrace");
c93e8391 13406 else if (self->type == bp_static_tracepoint)
348d480f 13407 fprintf_unfiltered (fp, "strace");
d9b3f62e 13408 else if (self->type == bp_tracepoint)
348d480f
PA
13409 fprintf_unfiltered (fp, "trace");
13410 else
13411 internal_error (__FILE__, __LINE__,
d9b3f62e 13412 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13413
f00aae0f 13414 fprintf_unfiltered (fp, " %s",
d28cd78a 13415 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13416 print_recreate_thread (self, fp);
13417
13418 if (tp->pass_count)
13419 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13420}
13421
983af33b 13422static void
f00aae0f
KS
13423tracepoint_create_sals_from_location (const struct event_location *location,
13424 struct linespec_result *canonical,
13425 enum bptype type_wanted)
983af33b 13426{
f00aae0f 13427 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13428}
13429
13430static void
13431tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13432 struct linespec_result *canonical,
983af33b 13433 char *cond_string,
e7e0cddf 13434 char *extra_string,
983af33b
SDJ
13435 enum bptype type_wanted,
13436 enum bpdisp disposition,
13437 int thread,
13438 int task, int ignore_count,
13439 const struct breakpoint_ops *ops,
13440 int from_tty, int enabled,
44f238bb 13441 int internal, unsigned flags)
983af33b 13442{
023fa29b 13443 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13444 cond_string, extra_string,
13445 type_wanted,
983af33b
SDJ
13446 disposition, thread, task,
13447 ignore_count, ops, from_tty,
44f238bb 13448 enabled, internal, flags);
983af33b
SDJ
13449}
13450
13451static void
f00aae0f
KS
13452tracepoint_decode_location (struct breakpoint *b,
13453 const struct event_location *location,
c2f4122d 13454 struct program_space *search_pspace,
983af33b
SDJ
13455 struct symtabs_and_lines *sals)
13456{
c2f4122d 13457 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13458}
13459
2060206e 13460struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13461
55aa24fb
SDJ
13462/* The breakpoint_ops structure to be use on tracepoints placed in a
13463 static probe. */
13464
13465static void
f00aae0f
KS
13466tracepoint_probe_create_sals_from_location
13467 (const struct event_location *location,
13468 struct linespec_result *canonical,
13469 enum bptype type_wanted)
55aa24fb
SDJ
13470{
13471 /* We use the same method for breakpoint on probes. */
f00aae0f 13472 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13473}
13474
13475static void
f00aae0f
KS
13476tracepoint_probe_decode_location (struct breakpoint *b,
13477 const struct event_location *location,
c2f4122d 13478 struct program_space *search_pspace,
55aa24fb
SDJ
13479 struct symtabs_and_lines *sals)
13480{
13481 /* We use the same method for breakpoint on probes. */
c2f4122d 13482 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13483}
13484
13485static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13486
5c2b4418
HZ
13487/* Dprintf breakpoint_ops methods. */
13488
13489static void
13490dprintf_re_set (struct breakpoint *b)
13491{
13492 breakpoint_re_set_default (b);
13493
f00aae0f
KS
13494 /* extra_string should never be non-NULL for dprintf. */
13495 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13496
13497 /* 1 - connect to target 1, that can run breakpoint commands.
13498 2 - create a dprintf, which resolves fine.
13499 3 - disconnect from target 1
13500 4 - connect to target 2, that can NOT run breakpoint commands.
13501
13502 After steps #3/#4, you'll want the dprintf command list to
13503 be updated, because target 1 and 2 may well return different
13504 answers for target_can_run_breakpoint_commands().
13505 Given absence of finer grained resetting, we get to do
13506 it all the time. */
13507 if (b->extra_string != NULL)
13508 update_dprintf_command_list (b);
13509}
13510
2d9442cc
HZ
13511/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13512
13513static void
13514dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13515{
f00aae0f 13516 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13517 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13518 tp->extra_string);
13519 print_recreate_thread (tp, fp);
13520}
13521
9d6e6e84
HZ
13522/* Implement the "after_condition_true" breakpoint_ops method for
13523 dprintf.
13524
13525 dprintf's are implemented with regular commands in their command
13526 list, but we run the commands here instead of before presenting the
13527 stop to the user, as dprintf's don't actually cause a stop. This
13528 also makes it so that the commands of multiple dprintfs at the same
13529 address are all handled. */
13530
13531static void
13532dprintf_after_condition_true (struct bpstats *bs)
13533{
13534 struct cleanup *old_chain;
13535 struct bpstats tmp_bs = { NULL };
13536 struct bpstats *tmp_bs_p = &tmp_bs;
13537
13538 /* dprintf's never cause a stop. This wasn't set in the
13539 check_status hook instead because that would make the dprintf's
13540 condition not be evaluated. */
13541 bs->stop = 0;
13542
13543 /* Run the command list here. Take ownership of it instead of
13544 copying. We never want these commands to run later in
13545 bpstat_do_actions, if a breakpoint that causes a stop happens to
13546 be set at same address as this dprintf, or even if running the
13547 commands here throws. */
13548 tmp_bs.commands = bs->commands;
13549 bs->commands = NULL;
13550 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13551
13552 bpstat_do_actions_1 (&tmp_bs_p);
13553
13554 /* 'tmp_bs.commands' will usually be NULL by now, but
13555 bpstat_do_actions_1 may return early without processing the whole
13556 list. */
13557 do_cleanups (old_chain);
13558}
13559
983af33b
SDJ
13560/* The breakpoint_ops structure to be used on static tracepoints with
13561 markers (`-m'). */
13562
13563static void
f00aae0f 13564strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13565 struct linespec_result *canonical,
f00aae0f 13566 enum bptype type_wanted)
983af33b
SDJ
13567{
13568 struct linespec_sals lsal;
f00aae0f
KS
13569 const char *arg_start, *arg;
13570 char *str;
13571 struct cleanup *cleanup;
983af33b 13572
f00aae0f
KS
13573 arg = arg_start = get_linespec_location (location);
13574 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13575
f00aae0f
KS
13576 str = savestring (arg_start, arg - arg_start);
13577 cleanup = make_cleanup (xfree, str);
8e9e35b1 13578 canonical->location = new_linespec_location (&str);
f00aae0f 13579 do_cleanups (cleanup);
983af33b 13580
8e9e35b1
TT
13581 lsal.canonical
13582 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13583 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13584}
13585
13586static void
13587strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13588 struct linespec_result *canonical,
983af33b 13589 char *cond_string,
e7e0cddf 13590 char *extra_string,
983af33b
SDJ
13591 enum bptype type_wanted,
13592 enum bpdisp disposition,
13593 int thread,
13594 int task, int ignore_count,
13595 const struct breakpoint_ops *ops,
13596 int from_tty, int enabled,
44f238bb 13597 int internal, unsigned flags)
983af33b
SDJ
13598{
13599 int i;
52d361e1
YQ
13600 struct linespec_sals *lsal = VEC_index (linespec_sals,
13601 canonical->sals, 0);
983af33b
SDJ
13602
13603 /* If the user is creating a static tracepoint by marker id
13604 (strace -m MARKER_ID), then store the sals index, so that
13605 breakpoint_re_set can try to match up which of the newly
13606 found markers corresponds to this one, and, don't try to
13607 expand multiple locations for each sal, given than SALS
13608 already should contain all sals for MARKER_ID. */
13609
13610 for (i = 0; i < lsal->sals.nelts; ++i)
13611 {
13612 struct symtabs_and_lines expanded;
13613 struct tracepoint *tp;
ffc2605c 13614 event_location_up location;
983af33b
SDJ
13615
13616 expanded.nelts = 1;
13617 expanded.sals = &lsal->sals.sals[i];
13618
8e9e35b1 13619 location = copy_event_location (canonical->location.get ());
983af33b 13620
4d01a485 13621 tp = new tracepoint ();
983af33b 13622 init_breakpoint_sal (&tp->base, gdbarch, expanded,
ffc2605c 13623 std::move (location), NULL,
e7e0cddf
SS
13624 cond_string, extra_string,
13625 type_wanted, disposition,
983af33b 13626 thread, task, ignore_count, ops,
44f238bb 13627 from_tty, enabled, internal, flags,
983af33b
SDJ
13628 canonical->special_display);
13629 /* Given that its possible to have multiple markers with
13630 the same string id, if the user is creating a static
13631 tracepoint by marker id ("strace -m MARKER_ID"), then
13632 store the sals index, so that breakpoint_re_set can
13633 try to match up which of the newly found markers
13634 corresponds to this one */
13635 tp->static_trace_marker_id_idx = i;
13636
13637 install_breakpoint (internal, &tp->base, 0);
983af33b
SDJ
13638 }
13639}
13640
13641static void
f00aae0f
KS
13642strace_marker_decode_location (struct breakpoint *b,
13643 const struct event_location *location,
c2f4122d 13644 struct program_space *search_pspace,
983af33b
SDJ
13645 struct symtabs_and_lines *sals)
13646{
13647 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13648 const char *s = get_linespec_location (location);
983af33b 13649
f00aae0f 13650 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13651 if (sals->nelts > tp->static_trace_marker_id_idx)
13652 {
13653 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13654 sals->nelts = 1;
13655 }
13656 else
13657 error (_("marker %s not found"), tp->static_trace_marker_id);
13658}
13659
13660static struct breakpoint_ops strace_marker_breakpoint_ops;
13661
13662static int
13663strace_marker_p (struct breakpoint *b)
13664{
13665 return b->ops == &strace_marker_breakpoint_ops;
13666}
13667
53a5351d 13668/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13669 structures. */
c906108c
SS
13670
13671void
fba45db2 13672delete_breakpoint (struct breakpoint *bpt)
c906108c 13673{
52f0bd74 13674 struct breakpoint *b;
c906108c 13675
8a3fe4f8 13676 gdb_assert (bpt != NULL);
c906108c 13677
4a64f543
MS
13678 /* Has this bp already been deleted? This can happen because
13679 multiple lists can hold pointers to bp's. bpstat lists are
13680 especial culprits.
13681
13682 One example of this happening is a watchpoint's scope bp. When
13683 the scope bp triggers, we notice that the watchpoint is out of
13684 scope, and delete it. We also delete its scope bp. But the
13685 scope bp is marked "auto-deleting", and is already on a bpstat.
13686 That bpstat is then checked for auto-deleting bp's, which are
13687 deleted.
13688
13689 A real solution to this problem might involve reference counts in
13690 bp's, and/or giving them pointers back to their referencing
13691 bpstat's, and teaching delete_breakpoint to only free a bp's
13692 storage when no more references were extent. A cheaper bandaid
13693 was chosen. */
c906108c
SS
13694 if (bpt->type == bp_none)
13695 return;
13696
4a64f543
MS
13697 /* At least avoid this stale reference until the reference counting
13698 of breakpoints gets resolved. */
d0fb5eae 13699 if (bpt->related_breakpoint != bpt)
e5a0a904 13700 {
d0fb5eae 13701 struct breakpoint *related;
3a5c3e22 13702 struct watchpoint *w;
d0fb5eae
JK
13703
13704 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13705 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13706 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13707 w = (struct watchpoint *) bpt;
13708 else
13709 w = NULL;
13710 if (w != NULL)
13711 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13712
13713 /* Unlink bpt from the bpt->related_breakpoint ring. */
13714 for (related = bpt; related->related_breakpoint != bpt;
13715 related = related->related_breakpoint);
13716 related->related_breakpoint = bpt->related_breakpoint;
13717 bpt->related_breakpoint = bpt;
e5a0a904
JK
13718 }
13719
a9634178
TJB
13720 /* watch_command_1 creates a watchpoint but only sets its number if
13721 update_watchpoint succeeds in creating its bp_locations. If there's
13722 a problem in that process, we'll be asked to delete the half-created
13723 watchpoint. In that case, don't announce the deletion. */
13724 if (bpt->number)
13725 observer_notify_breakpoint_deleted (bpt);
c906108c 13726
c906108c
SS
13727 if (breakpoint_chain == bpt)
13728 breakpoint_chain = bpt->next;
13729
c906108c
SS
13730 ALL_BREAKPOINTS (b)
13731 if (b->next == bpt)
c5aa993b
JM
13732 {
13733 b->next = bpt->next;
13734 break;
13735 }
c906108c 13736
f431efe5
PA
13737 /* Be sure no bpstat's are pointing at the breakpoint after it's
13738 been freed. */
13739 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13740 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13741 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13742 commands are associated with the bpstat; if we remove it here,
13743 then the later call to bpstat_do_actions (&stop_bpstat); in
13744 event-top.c won't do anything, and temporary breakpoints with
13745 commands won't work. */
13746
13747 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13748
4a64f543
MS
13749 /* Now that breakpoint is removed from breakpoint list, update the
13750 global location list. This will remove locations that used to
13751 belong to this breakpoint. Do this before freeing the breakpoint
13752 itself, since remove_breakpoint looks at location's owner. It
13753 might be better design to have location completely
13754 self-contained, but it's not the case now. */
44702360 13755 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13756
348d480f 13757 bpt->ops->dtor (bpt);
4a64f543
MS
13758 /* On the chance that someone will soon try again to delete this
13759 same bp, we mark it as deleted before freeing its storage. */
c906108c 13760 bpt->type = bp_none;
4d01a485 13761 delete bpt;
c906108c
SS
13762}
13763
4d6140d9
AC
13764static void
13765do_delete_breakpoint_cleanup (void *b)
13766{
9a3c8263 13767 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13768}
13769
13770struct cleanup *
13771make_cleanup_delete_breakpoint (struct breakpoint *b)
13772{
13773 return make_cleanup (do_delete_breakpoint_cleanup, b);
13774}
13775
51be5b68
PA
13776/* Iterator function to call a user-provided callback function once
13777 for each of B and its related breakpoints. */
13778
13779static void
13780iterate_over_related_breakpoints (struct breakpoint *b,
13781 void (*function) (struct breakpoint *,
13782 void *),
13783 void *data)
13784{
13785 struct breakpoint *related;
13786
13787 related = b;
13788 do
13789 {
13790 struct breakpoint *next;
13791
13792 /* FUNCTION may delete RELATED. */
13793 next = related->related_breakpoint;
13794
13795 if (next == related)
13796 {
13797 /* RELATED is the last ring entry. */
13798 function (related, data);
13799
13800 /* FUNCTION may have deleted it, so we'd never reach back to
13801 B. There's nothing left to do anyway, so just break
13802 out. */
13803 break;
13804 }
13805 else
13806 function (related, data);
13807
13808 related = next;
13809 }
13810 while (related != b);
13811}
95a42b64
TT
13812
13813static void
13814do_delete_breakpoint (struct breakpoint *b, void *ignore)
13815{
13816 delete_breakpoint (b);
13817}
13818
51be5b68
PA
13819/* A callback for map_breakpoint_numbers that calls
13820 delete_breakpoint. */
13821
13822static void
13823do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13824{
13825 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13826}
13827
c906108c 13828void
fba45db2 13829delete_command (char *arg, int from_tty)
c906108c 13830{
35df4500 13831 struct breakpoint *b, *b_tmp;
c906108c 13832
ea9365bb
TT
13833 dont_repeat ();
13834
c906108c
SS
13835 if (arg == 0)
13836 {
13837 int breaks_to_delete = 0;
13838
46c6471b
PA
13839 /* Delete all breakpoints if no argument. Do not delete
13840 internal breakpoints, these have to be deleted with an
13841 explicit breakpoint number argument. */
c5aa993b 13842 ALL_BREAKPOINTS (b)
46c6471b 13843 if (user_breakpoint_p (b))
973d738b
DJ
13844 {
13845 breaks_to_delete = 1;
13846 break;
13847 }
c906108c
SS
13848
13849 /* Ask user only if there are some breakpoints to delete. */
13850 if (!from_tty
e2e0b3e5 13851 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13852 {
35df4500 13853 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13854 if (user_breakpoint_p (b))
c5aa993b 13855 delete_breakpoint (b);
c906108c
SS
13856 }
13857 }
13858 else
51be5b68 13859 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13860}
13861
c2f4122d
PA
13862/* Return true if all locations of B bound to PSPACE are pending. If
13863 PSPACE is NULL, all locations of all program spaces are
13864 considered. */
13865
0d381245 13866static int
c2f4122d 13867all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13868{
c2f4122d
PA
13869 struct bp_location *loc;
13870
13871 for (loc = b->loc; loc != NULL; loc = loc->next)
13872 if ((pspace == NULL
13873 || loc->pspace == pspace)
13874 && !loc->shlib_disabled
8645ff69 13875 && !loc->pspace->executing_startup)
0d381245
VP
13876 return 0;
13877 return 1;
fe3f5fa8
VP
13878}
13879
776592bf
DE
13880/* Subroutine of update_breakpoint_locations to simplify it.
13881 Return non-zero if multiple fns in list LOC have the same name.
13882 Null names are ignored. */
13883
13884static int
13885ambiguous_names_p (struct bp_location *loc)
13886{
13887 struct bp_location *l;
13888 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13889 (int (*) (const void *,
13890 const void *)) streq,
776592bf
DE
13891 NULL, xcalloc, xfree);
13892
13893 for (l = loc; l != NULL; l = l->next)
13894 {
13895 const char **slot;
13896 const char *name = l->function_name;
13897
13898 /* Allow for some names to be NULL, ignore them. */
13899 if (name == NULL)
13900 continue;
13901
13902 slot = (const char **) htab_find_slot (htab, (const void *) name,
13903 INSERT);
4a64f543
MS
13904 /* NOTE: We can assume slot != NULL here because xcalloc never
13905 returns NULL. */
776592bf
DE
13906 if (*slot != NULL)
13907 {
13908 htab_delete (htab);
13909 return 1;
13910 }
13911 *slot = name;
13912 }
13913
13914 htab_delete (htab);
13915 return 0;
13916}
13917
0fb4aa4b
PA
13918/* When symbols change, it probably means the sources changed as well,
13919 and it might mean the static tracepoint markers are no longer at
13920 the same address or line numbers they used to be at last we
13921 checked. Losing your static tracepoints whenever you rebuild is
13922 undesirable. This function tries to resync/rematch gdb static
13923 tracepoints with the markers on the target, for static tracepoints
13924 that have not been set by marker id. Static tracepoint that have
13925 been set by marker id are reset by marker id in breakpoint_re_set.
13926 The heuristic is:
13927
13928 1) For a tracepoint set at a specific address, look for a marker at
13929 the old PC. If one is found there, assume to be the same marker.
13930 If the name / string id of the marker found is different from the
13931 previous known name, assume that means the user renamed the marker
13932 in the sources, and output a warning.
13933
13934 2) For a tracepoint set at a given line number, look for a marker
13935 at the new address of the old line number. If one is found there,
13936 assume to be the same marker. If the name / string id of the
13937 marker found is different from the previous known name, assume that
13938 means the user renamed the marker in the sources, and output a
13939 warning.
13940
13941 3) If a marker is no longer found at the same address or line, it
13942 may mean the marker no longer exists. But it may also just mean
13943 the code changed a bit. Maybe the user added a few lines of code
13944 that made the marker move up or down (in line number terms). Ask
13945 the target for info about the marker with the string id as we knew
13946 it. If found, update line number and address in the matching
13947 static tracepoint. This will get confused if there's more than one
13948 marker with the same ID (possible in UST, although unadvised
13949 precisely because it confuses tools). */
13950
13951static struct symtab_and_line
13952update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13953{
d9b3f62e 13954 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13955 struct static_tracepoint_marker marker;
13956 CORE_ADDR pc;
0fb4aa4b
PA
13957
13958 pc = sal.pc;
13959 if (sal.line)
13960 find_line_pc (sal.symtab, sal.line, &pc);
13961
13962 if (target_static_tracepoint_marker_at (pc, &marker))
13963 {
d9b3f62e 13964 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13965 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13966 b->number,
d9b3f62e 13967 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13968
d9b3f62e
PA
13969 xfree (tp->static_trace_marker_id);
13970 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13971 release_static_tracepoint_marker (&marker);
13972
13973 return sal;
13974 }
13975
13976 /* Old marker wasn't found on target at lineno. Try looking it up
13977 by string ID. */
13978 if (!sal.explicit_pc
13979 && sal.line != 0
13980 && sal.symtab != NULL
d9b3f62e 13981 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13982 {
13983 VEC(static_tracepoint_marker_p) *markers;
13984
13985 markers
d9b3f62e 13986 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13987
13988 if (!VEC_empty(static_tracepoint_marker_p, markers))
13989 {
80e1d417 13990 struct symtab_and_line sal2;
0fb4aa4b 13991 struct symbol *sym;
80e1d417 13992 struct static_tracepoint_marker *tpmarker;
79a45e25 13993 struct ui_out *uiout = current_uiout;
67994074 13994 struct explicit_location explicit_loc;
0fb4aa4b 13995
80e1d417 13996 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13997
d9b3f62e 13998 xfree (tp->static_trace_marker_id);
80e1d417 13999 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14000
14001 warning (_("marker for static tracepoint %d (%s) not "
14002 "found at previous line number"),
d9b3f62e 14003 b->number, tp->static_trace_marker_id);
0fb4aa4b 14004
80e1d417 14005 init_sal (&sal2);
0fb4aa4b 14006
80e1d417 14007 sal2.pc = tpmarker->address;
0fb4aa4b 14008
80e1d417
AS
14009 sal2 = find_pc_line (tpmarker->address, 0);
14010 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 14011 uiout->text ("Now in ");
0fb4aa4b
PA
14012 if (sym)
14013 {
112e8700
SM
14014 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14015 uiout->text (" at ");
0fb4aa4b 14016 }
112e8700 14017 uiout->field_string ("file",
05cba821 14018 symtab_to_filename_for_display (sal2.symtab));
112e8700 14019 uiout->text (":");
0fb4aa4b 14020
112e8700 14021 if (uiout->is_mi_like_p ())
0fb4aa4b 14022 {
0b0865da 14023 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14024
112e8700 14025 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
14026 }
14027
112e8700
SM
14028 uiout->field_int ("line", sal2.line);
14029 uiout->text ("\n");
0fb4aa4b 14030
80e1d417 14031 b->loc->line_number = sal2.line;
2f202fde 14032 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14033
d28cd78a 14034 b->location.reset (NULL);
67994074
KS
14035 initialize_explicit_location (&explicit_loc);
14036 explicit_loc.source_filename
00e52e53 14037 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14038 explicit_loc.line_offset.offset = b->loc->line_number;
14039 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 14040 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14041
14042 /* Might be nice to check if function changed, and warn if
14043 so. */
14044
80e1d417 14045 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14046 }
14047 }
14048 return sal;
14049}
14050
8d3788bd
VP
14051/* Returns 1 iff locations A and B are sufficiently same that
14052 we don't need to report breakpoint as changed. */
14053
14054static int
14055locations_are_equal (struct bp_location *a, struct bp_location *b)
14056{
14057 while (a && b)
14058 {
14059 if (a->address != b->address)
14060 return 0;
14061
14062 if (a->shlib_disabled != b->shlib_disabled)
14063 return 0;
14064
14065 if (a->enabled != b->enabled)
14066 return 0;
14067
14068 a = a->next;
14069 b = b->next;
14070 }
14071
14072 if ((a == NULL) != (b == NULL))
14073 return 0;
14074
14075 return 1;
14076}
14077
c2f4122d
PA
14078/* Split all locations of B that are bound to PSPACE out of B's
14079 location list to a separate list and return that list's head. If
14080 PSPACE is NULL, hoist out all locations of B. */
14081
14082static struct bp_location *
14083hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14084{
14085 struct bp_location head;
14086 struct bp_location *i = b->loc;
14087 struct bp_location **i_link = &b->loc;
14088 struct bp_location *hoisted = &head;
14089
14090 if (pspace == NULL)
14091 {
14092 i = b->loc;
14093 b->loc = NULL;
14094 return i;
14095 }
14096
14097 head.next = NULL;
14098
14099 while (i != NULL)
14100 {
14101 if (i->pspace == pspace)
14102 {
14103 *i_link = i->next;
14104 i->next = NULL;
14105 hoisted->next = i;
14106 hoisted = i;
14107 }
14108 else
14109 i_link = &i->next;
14110 i = *i_link;
14111 }
14112
14113 return head.next;
14114}
14115
14116/* Create new breakpoint locations for B (a hardware or software
14117 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14118 zero, then B is a ranged breakpoint. Only recreates locations for
14119 FILTER_PSPACE. Locations of other program spaces are left
14120 untouched. */
f1310107 14121
0e30163f 14122void
0d381245 14123update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14124 struct program_space *filter_pspace,
f1310107
TJB
14125 struct symtabs_and_lines sals,
14126 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14127{
14128 int i;
c2f4122d 14129 struct bp_location *existing_locations;
0d381245 14130
f8eba3c6
TT
14131 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14132 {
14133 /* Ranged breakpoints have only one start location and one end
14134 location. */
14135 b->enable_state = bp_disabled;
f8eba3c6
TT
14136 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14137 "multiple locations found\n"),
14138 b->number);
14139 return;
14140 }
f1310107 14141
4a64f543
MS
14142 /* If there's no new locations, and all existing locations are
14143 pending, don't do anything. This optimizes the common case where
14144 all locations are in the same shared library, that was unloaded.
14145 We'd like to retain the location, so that when the library is
14146 loaded again, we don't loose the enabled/disabled status of the
14147 individual locations. */
c2f4122d 14148 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14149 return;
14150
c2f4122d 14151 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14152
0d381245 14153 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14154 {
f8eba3c6
TT
14155 struct bp_location *new_loc;
14156
14157 switch_to_program_space_and_thread (sals.sals[i].pspace);
14158
14159 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14160
0d381245
VP
14161 /* Reparse conditions, they might contain references to the
14162 old symtab. */
14163 if (b->cond_string != NULL)
14164 {
bbc13ae3 14165 const char *s;
fe3f5fa8 14166
0d381245 14167 s = b->cond_string;
492d29ea 14168 TRY
0d381245 14169 {
1bb9788d
TT
14170 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14171 block_for_pc (sals.sals[i].pc),
0d381245
VP
14172 0);
14173 }
492d29ea 14174 CATCH (e, RETURN_MASK_ERROR)
0d381245 14175 {
3e43a32a
MS
14176 warning (_("failed to reevaluate condition "
14177 "for breakpoint %d: %s"),
0d381245
VP
14178 b->number, e.message);
14179 new_loc->enabled = 0;
14180 }
492d29ea 14181 END_CATCH
0d381245 14182 }
fe3f5fa8 14183
f1310107
TJB
14184 if (sals_end.nelts)
14185 {
14186 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14187
14188 new_loc->length = end - sals.sals[0].pc + 1;
14189 }
0d381245 14190 }
fe3f5fa8 14191
4a64f543
MS
14192 /* If possible, carry over 'disable' status from existing
14193 breakpoints. */
0d381245
VP
14194 {
14195 struct bp_location *e = existing_locations;
776592bf
DE
14196 /* If there are multiple breakpoints with the same function name,
14197 e.g. for inline functions, comparing function names won't work.
14198 Instead compare pc addresses; this is just a heuristic as things
14199 may have moved, but in practice it gives the correct answer
14200 often enough until a better solution is found. */
14201 int have_ambiguous_names = ambiguous_names_p (b->loc);
14202
0d381245
VP
14203 for (; e; e = e->next)
14204 {
14205 if (!e->enabled && e->function_name)
14206 {
14207 struct bp_location *l = b->loc;
776592bf
DE
14208 if (have_ambiguous_names)
14209 {
14210 for (; l; l = l->next)
f1310107 14211 if (breakpoint_locations_match (e, l))
776592bf
DE
14212 {
14213 l->enabled = 0;
14214 break;
14215 }
14216 }
14217 else
14218 {
14219 for (; l; l = l->next)
14220 if (l->function_name
14221 && strcmp (e->function_name, l->function_name) == 0)
14222 {
14223 l->enabled = 0;
14224 break;
14225 }
14226 }
0d381245
VP
14227 }
14228 }
14229 }
fe3f5fa8 14230
8d3788bd
VP
14231 if (!locations_are_equal (existing_locations, b->loc))
14232 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14233}
14234
f00aae0f 14235/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14236 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14237
14238static struct symtabs_and_lines
f00aae0f 14239location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14240 struct program_space *search_pspace, int *found)
ef23e705 14241{
02d20e4a 14242 struct symtabs_and_lines sals = {0};
492d29ea 14243 struct gdb_exception exception = exception_none;
ef23e705 14244
983af33b 14245 gdb_assert (b->ops != NULL);
ef23e705 14246
492d29ea 14247 TRY
ef23e705 14248 {
c2f4122d 14249 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14250 }
492d29ea 14251 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14252 {
14253 int not_found_and_ok = 0;
492d29ea
PA
14254
14255 exception = e;
14256
ef23e705
TJB
14257 /* For pending breakpoints, it's expected that parsing will
14258 fail until the right shared library is loaded. User has
14259 already told to create pending breakpoints and don't need
14260 extra messages. If breakpoint is in bp_shlib_disabled
14261 state, then user already saw the message about that
14262 breakpoint being disabled, and don't want to see more
14263 errors. */
58438ac1 14264 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14265 && (b->condition_not_parsed
14266 || (b->loc != NULL
14267 && search_pspace != NULL
14268 && b->loc->pspace != search_pspace)
ef23e705 14269 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14270 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14271 || b->enable_state == bp_disabled))
14272 not_found_and_ok = 1;
14273
14274 if (!not_found_and_ok)
14275 {
14276 /* We surely don't want to warn about the same breakpoint
14277 10 times. One solution, implemented here, is disable
14278 the breakpoint on error. Another solution would be to
14279 have separate 'warning emitted' flag. Since this
14280 happens only when a binary has changed, I don't know
14281 which approach is better. */
14282 b->enable_state = bp_disabled;
14283 throw_exception (e);
14284 }
14285 }
492d29ea 14286 END_CATCH
ef23e705 14287
492d29ea 14288 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14289 {
f8eba3c6 14290 int i;
ef23e705 14291
f8eba3c6
TT
14292 for (i = 0; i < sals.nelts; ++i)
14293 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14294 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14295 {
ed1d1739
KS
14296 char *cond_string, *extra_string;
14297 int thread, task;
ef23e705 14298
f00aae0f 14299 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14300 &cond_string, &thread, &task,
14301 &extra_string);
f00aae0f 14302 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14303 if (cond_string)
14304 b->cond_string = cond_string;
14305 b->thread = thread;
14306 b->task = task;
e7e0cddf 14307 if (extra_string)
f00aae0f
KS
14308 {
14309 xfree (b->extra_string);
14310 b->extra_string = extra_string;
14311 }
ef23e705
TJB
14312 b->condition_not_parsed = 0;
14313 }
14314
983af33b 14315 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14316 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14317
58438ac1
TT
14318 *found = 1;
14319 }
14320 else
14321 *found = 0;
ef23e705
TJB
14322
14323 return sals;
14324}
14325
348d480f
PA
14326/* The default re_set method, for typical hardware or software
14327 breakpoints. Reevaluate the breakpoint and recreate its
14328 locations. */
14329
14330static void
28010a5d 14331breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14332{
14333 int found;
f1310107 14334 struct symtabs_and_lines sals, sals_end;
ef23e705 14335 struct symtabs_and_lines expanded = {0};
f1310107 14336 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14337 struct program_space *filter_pspace = current_program_space;
ef23e705 14338
d28cd78a 14339 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14340 if (found)
14341 {
14342 make_cleanup (xfree, sals.sals);
f8eba3c6 14343 expanded = sals;
ef23e705
TJB
14344 }
14345
f00aae0f 14346 if (b->location_range_end != NULL)
f1310107 14347 {
d28cd78a 14348 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14349 filter_pspace, &found);
f1310107
TJB
14350 if (found)
14351 {
14352 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14353 expanded_end = sals_end;
f1310107
TJB
14354 }
14355 }
14356
c2f4122d 14357 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14358}
14359
983af33b
SDJ
14360/* Default method for creating SALs from an address string. It basically
14361 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14362
14363static void
f00aae0f
KS
14364create_sals_from_location_default (const struct event_location *location,
14365 struct linespec_result *canonical,
14366 enum bptype type_wanted)
983af33b 14367{
f00aae0f 14368 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14369}
14370
14371/* Call create_breakpoints_sal for the given arguments. This is the default
14372 function for the `create_breakpoints_sal' method of
14373 breakpoint_ops. */
14374
14375static void
14376create_breakpoints_sal_default (struct gdbarch *gdbarch,
14377 struct linespec_result *canonical,
983af33b 14378 char *cond_string,
e7e0cddf 14379 char *extra_string,
983af33b
SDJ
14380 enum bptype type_wanted,
14381 enum bpdisp disposition,
14382 int thread,
14383 int task, int ignore_count,
14384 const struct breakpoint_ops *ops,
14385 int from_tty, int enabled,
44f238bb 14386 int internal, unsigned flags)
983af33b
SDJ
14387{
14388 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14389 extra_string,
983af33b
SDJ
14390 type_wanted, disposition,
14391 thread, task, ignore_count, ops, from_tty,
44f238bb 14392 enabled, internal, flags);
983af33b
SDJ
14393}
14394
14395/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14396 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14397
14398static void
f00aae0f
KS
14399decode_location_default (struct breakpoint *b,
14400 const struct event_location *location,
c2f4122d 14401 struct program_space *search_pspace,
983af33b
SDJ
14402 struct symtabs_and_lines *sals)
14403{
14404 struct linespec_result canonical;
14405
c2f4122d 14406 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14407 (struct symtab *) NULL, 0,
14408 &canonical, multiple_symbols_all,
14409 b->filter);
14410
14411 /* We should get 0 or 1 resulting SALs. */
14412 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14413
14414 if (VEC_length (linespec_sals, canonical.sals) > 0)
14415 {
14416 struct linespec_sals *lsal;
14417
14418 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14419 *sals = lsal->sals;
14420 /* Arrange it so the destructor does not free the
14421 contents. */
14422 lsal->sals.sals = NULL;
14423 }
983af33b
SDJ
14424}
14425
28010a5d
PA
14426/* Prepare the global context for a re-set of breakpoint B. */
14427
14428static struct cleanup *
14429prepare_re_set_context (struct breakpoint *b)
14430{
28010a5d 14431 input_radix = b->input_radix;
28010a5d
PA
14432 set_language (b->language);
14433
c2f4122d 14434 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14435}
14436
c906108c
SS
14437/* Reset a breakpoint given it's struct breakpoint * BINT.
14438 The value we return ends up being the return value from catch_errors.
14439 Unused in this case. */
14440
14441static int
4efb68b1 14442breakpoint_re_set_one (void *bint)
c906108c 14443{
4a64f543 14444 /* Get past catch_errs. */
53a5351d 14445 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14446 struct cleanup *cleanups;
c906108c 14447
348d480f
PA
14448 cleanups = prepare_re_set_context (b);
14449 b->ops->re_set (b);
14450 do_cleanups (cleanups);
c906108c
SS
14451 return 0;
14452}
14453
c2f4122d
PA
14454/* Re-set breakpoint locations for the current program space.
14455 Locations bound to other program spaces are left untouched. */
14456
c906108c 14457void
69de3c6a 14458breakpoint_re_set (void)
c906108c 14459{
35df4500 14460 struct breakpoint *b, *b_tmp;
c906108c
SS
14461 enum language save_language;
14462 int save_input_radix;
c5aa993b 14463
c906108c
SS
14464 save_language = current_language->la_language;
14465 save_input_radix = input_radix;
2a7f3dff 14466
c5aa993b 14467 {
5ed8105e 14468 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14469
5ed8105e
PA
14470 /* Note: we must not try to insert locations until after all
14471 breakpoints have been re-set. Otherwise, e.g., when re-setting
14472 breakpoint 1, we'd insert the locations of breakpoint 2, which
14473 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14474
5ed8105e
PA
14475 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14476 {
14477 /* Format possible error msg. */
14478 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14479 b->number);
14480 struct cleanup *cleanups = make_cleanup (xfree, message);
14481 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14482 do_cleanups (cleanups);
14483 }
14484 set_language (save_language);
14485 input_radix = save_input_radix;
14486
14487 jit_breakpoint_re_set ();
14488 }
6c95b8df 14489
af02033e
PP
14490 create_overlay_event_breakpoint ();
14491 create_longjmp_master_breakpoint ();
14492 create_std_terminate_master_breakpoint ();
186c406b 14493 create_exception_master_breakpoint ();
2a7f3dff
PA
14494
14495 /* Now we can insert. */
14496 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14497}
14498\f
c906108c
SS
14499/* Reset the thread number of this breakpoint:
14500
14501 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14502 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14503void
fba45db2 14504breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14505{
14506 if (b->thread != -1)
14507 {
39f77062 14508 if (in_thread_list (inferior_ptid))
5d5658a1 14509 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14510
14511 /* We're being called after following a fork. The new fork is
14512 selected as current, and unless this was a vfork will have a
14513 different program space from the original thread. Reset that
14514 as well. */
14515 b->loc->pspace = current_program_space;
c906108c
SS
14516 }
14517}
14518
03ac34d5
MS
14519/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14520 If from_tty is nonzero, it prints a message to that effect,
14521 which ends with a period (no newline). */
14522
c906108c 14523void
fba45db2 14524set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14525{
52f0bd74 14526 struct breakpoint *b;
c906108c
SS
14527
14528 if (count < 0)
14529 count = 0;
14530
14531 ALL_BREAKPOINTS (b)
14532 if (b->number == bptnum)
c5aa993b 14533 {
d77f58be
SS
14534 if (is_tracepoint (b))
14535 {
14536 if (from_tty && count != 0)
14537 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14538 bptnum);
14539 return;
14540 }
14541
c5aa993b 14542 b->ignore_count = count;
221ea385
KS
14543 if (from_tty)
14544 {
14545 if (count == 0)
3e43a32a
MS
14546 printf_filtered (_("Will stop next time "
14547 "breakpoint %d is reached."),
221ea385
KS
14548 bptnum);
14549 else if (count == 1)
a3f17187 14550 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14551 bptnum);
14552 else
3e43a32a
MS
14553 printf_filtered (_("Will ignore next %d "
14554 "crossings of breakpoint %d."),
221ea385
KS
14555 count, bptnum);
14556 }
8d3788bd 14557 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14558 return;
14559 }
c906108c 14560
8a3fe4f8 14561 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14562}
14563
c906108c
SS
14564/* Command to set ignore-count of breakpoint N to COUNT. */
14565
14566static void
fba45db2 14567ignore_command (char *args, int from_tty)
c906108c
SS
14568{
14569 char *p = args;
52f0bd74 14570 int num;
c906108c
SS
14571
14572 if (p == 0)
e2e0b3e5 14573 error_no_arg (_("a breakpoint number"));
c5aa993b 14574
c906108c 14575 num = get_number (&p);
5c44784c 14576 if (num == 0)
8a3fe4f8 14577 error (_("bad breakpoint number: '%s'"), args);
c906108c 14578 if (*p == 0)
8a3fe4f8 14579 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14580
14581 set_ignore_count (num,
14582 longest_to_int (value_as_long (parse_and_eval (p))),
14583 from_tty);
221ea385
KS
14584 if (from_tty)
14585 printf_filtered ("\n");
c906108c
SS
14586}
14587\f
14588/* Call FUNCTION on each of the breakpoints
14589 whose numbers are given in ARGS. */
14590
14591static void
896b6bda
PA
14592map_breakpoint_numbers (const char *args,
14593 void (*function) (struct breakpoint *,
14594 void *),
95a42b64 14595 void *data)
c906108c 14596{
52f0bd74
AC
14597 int num;
14598 struct breakpoint *b, *tmp;
c906108c 14599
b9d61307 14600 if (args == 0 || *args == '\0')
e2e0b3e5 14601 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14602
bfd28288 14603 number_or_range_parser parser (args);
197f0a60 14604
bfd28288 14605 while (!parser.finished ())
c906108c 14606 {
bfd28288
PA
14607 const char *p = parser.cur_tok ();
14608 bool match = false;
197f0a60 14609
bfd28288 14610 num = parser.get_number ();
5c44784c 14611 if (num == 0)
c5aa993b 14612 {
8a3fe4f8 14613 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14614 }
14615 else
14616 {
14617 ALL_BREAKPOINTS_SAFE (b, tmp)
14618 if (b->number == num)
14619 {
bfd28288 14620 match = true;
cdac0397 14621 function (b, data);
11cf8741 14622 break;
5c44784c 14623 }
bfd28288 14624 if (!match)
a3f17187 14625 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14626 }
c906108c
SS
14627 }
14628}
14629
0d381245
VP
14630static struct bp_location *
14631find_location_by_number (char *number)
14632{
14633 char *dot = strchr (number, '.');
14634 char *p1;
14635 int bp_num;
14636 int loc_num;
14637 struct breakpoint *b;
14638 struct bp_location *loc;
14639
14640 *dot = '\0';
14641
14642 p1 = number;
197f0a60 14643 bp_num = get_number (&p1);
0d381245
VP
14644 if (bp_num == 0)
14645 error (_("Bad breakpoint number '%s'"), number);
14646
14647 ALL_BREAKPOINTS (b)
14648 if (b->number == bp_num)
14649 {
14650 break;
14651 }
14652
14653 if (!b || b->number != bp_num)
14654 error (_("Bad breakpoint number '%s'"), number);
14655
14656 p1 = dot+1;
197f0a60 14657 loc_num = get_number (&p1);
0d381245
VP
14658 if (loc_num == 0)
14659 error (_("Bad breakpoint location number '%s'"), number);
14660
14661 --loc_num;
14662 loc = b->loc;
14663 for (;loc_num && loc; --loc_num, loc = loc->next)
14664 ;
14665 if (!loc)
14666 error (_("Bad breakpoint location number '%s'"), dot+1);
14667
14668 return loc;
14669}
14670
14671
1900040c
MS
14672/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14673 If from_tty is nonzero, it prints a message to that effect,
14674 which ends with a period (no newline). */
14675
c906108c 14676void
fba45db2 14677disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14678{
14679 /* Never disable a watchpoint scope breakpoint; we want to
14680 hit them when we leave scope so we can delete both the
14681 watchpoint and its scope breakpoint at that time. */
14682 if (bpt->type == bp_watchpoint_scope)
14683 return;
14684
b5de0fa7 14685 bpt->enable_state = bp_disabled;
c906108c 14686
b775012e
LM
14687 /* Mark breakpoint locations modified. */
14688 mark_breakpoint_modified (bpt);
14689
d248b706
KY
14690 if (target_supports_enable_disable_tracepoint ()
14691 && current_trace_status ()->running && is_tracepoint (bpt))
14692 {
14693 struct bp_location *location;
14694
14695 for (location = bpt->loc; location; location = location->next)
14696 target_disable_tracepoint (location);
14697 }
14698
44702360 14699 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14700
8d3788bd 14701 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14702}
14703
51be5b68
PA
14704/* A callback for iterate_over_related_breakpoints. */
14705
14706static void
14707do_disable_breakpoint (struct breakpoint *b, void *ignore)
14708{
14709 disable_breakpoint (b);
14710}
14711
95a42b64
TT
14712/* A callback for map_breakpoint_numbers that calls
14713 disable_breakpoint. */
14714
14715static void
14716do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14717{
51be5b68 14718 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14719}
14720
c906108c 14721static void
fba45db2 14722disable_command (char *args, int from_tty)
c906108c 14723{
c906108c 14724 if (args == 0)
46c6471b
PA
14725 {
14726 struct breakpoint *bpt;
14727
14728 ALL_BREAKPOINTS (bpt)
14729 if (user_breakpoint_p (bpt))
14730 disable_breakpoint (bpt);
14731 }
9eaabc75 14732 else
0d381245 14733 {
9eaabc75
MW
14734 char *num = extract_arg (&args);
14735
14736 while (num)
d248b706 14737 {
9eaabc75 14738 if (strchr (num, '.'))
b775012e 14739 {
9eaabc75
MW
14740 struct bp_location *loc = find_location_by_number (num);
14741
14742 if (loc)
14743 {
14744 if (loc->enabled)
14745 {
14746 loc->enabled = 0;
14747 mark_breakpoint_location_modified (loc);
14748 }
14749 if (target_supports_enable_disable_tracepoint ()
14750 && current_trace_status ()->running && loc->owner
14751 && is_tracepoint (loc->owner))
14752 target_disable_tracepoint (loc);
14753 }
44702360 14754 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14755 }
9eaabc75
MW
14756 else
14757 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14758 num = extract_arg (&args);
d248b706 14759 }
0d381245 14760 }
c906108c
SS
14761}
14762
14763static void
816338b5
SS
14764enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14765 int count)
c906108c 14766{
afe38095 14767 int target_resources_ok;
c906108c
SS
14768
14769 if (bpt->type == bp_hardware_breakpoint)
14770 {
14771 int i;
c5aa993b 14772 i = hw_breakpoint_used_count ();
53a5351d 14773 target_resources_ok =
d92524f1 14774 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14775 i + 1, 0);
c906108c 14776 if (target_resources_ok == 0)
8a3fe4f8 14777 error (_("No hardware breakpoint support in the target."));
c906108c 14778 else if (target_resources_ok < 0)
8a3fe4f8 14779 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14780 }
14781
cc60f2e3 14782 if (is_watchpoint (bpt))
c906108c 14783 {
d07205c2 14784 /* Initialize it just to avoid a GCC false warning. */
f486487f 14785 enum enable_state orig_enable_state = bp_disabled;
dde02812 14786
492d29ea 14787 TRY
c906108c 14788 {
3a5c3e22
PA
14789 struct watchpoint *w = (struct watchpoint *) bpt;
14790
1e718ff1
TJB
14791 orig_enable_state = bpt->enable_state;
14792 bpt->enable_state = bp_enabled;
3a5c3e22 14793 update_watchpoint (w, 1 /* reparse */);
c906108c 14794 }
492d29ea 14795 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14796 {
1e718ff1 14797 bpt->enable_state = orig_enable_state;
dde02812
ES
14798 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14799 bpt->number);
14800 return;
c5aa993b 14801 }
492d29ea 14802 END_CATCH
c906108c 14803 }
0101ce28 14804
b775012e
LM
14805 bpt->enable_state = bp_enabled;
14806
14807 /* Mark breakpoint locations modified. */
14808 mark_breakpoint_modified (bpt);
14809
d248b706
KY
14810 if (target_supports_enable_disable_tracepoint ()
14811 && current_trace_status ()->running && is_tracepoint (bpt))
14812 {
14813 struct bp_location *location;
14814
14815 for (location = bpt->loc; location; location = location->next)
14816 target_enable_tracepoint (location);
14817 }
14818
b4c291bb 14819 bpt->disposition = disposition;
816338b5 14820 bpt->enable_count = count;
44702360 14821 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14822
8d3788bd 14823 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14824}
14825
fe3f5fa8 14826
c906108c 14827void
fba45db2 14828enable_breakpoint (struct breakpoint *bpt)
c906108c 14829{
816338b5 14830 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14831}
14832
14833static void
14834do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14835{
14836 enable_breakpoint (bpt);
c906108c
SS
14837}
14838
95a42b64
TT
14839/* A callback for map_breakpoint_numbers that calls
14840 enable_breakpoint. */
14841
14842static void
14843do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14844{
51be5b68 14845 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14846}
14847
c906108c
SS
14848/* The enable command enables the specified breakpoints (or all defined
14849 breakpoints) so they once again become (or continue to be) effective
1272ad14 14850 in stopping the inferior. */
c906108c 14851
c906108c 14852static void
fba45db2 14853enable_command (char *args, int from_tty)
c906108c 14854{
c906108c 14855 if (args == 0)
46c6471b
PA
14856 {
14857 struct breakpoint *bpt;
14858
14859 ALL_BREAKPOINTS (bpt)
14860 if (user_breakpoint_p (bpt))
14861 enable_breakpoint (bpt);
14862 }
9eaabc75 14863 else
0d381245 14864 {
9eaabc75
MW
14865 char *num = extract_arg (&args);
14866
14867 while (num)
d248b706 14868 {
9eaabc75 14869 if (strchr (num, '.'))
b775012e 14870 {
9eaabc75
MW
14871 struct bp_location *loc = find_location_by_number (num);
14872
14873 if (loc)
14874 {
14875 if (!loc->enabled)
14876 {
14877 loc->enabled = 1;
14878 mark_breakpoint_location_modified (loc);
14879 }
14880 if (target_supports_enable_disable_tracepoint ()
14881 && current_trace_status ()->running && loc->owner
14882 && is_tracepoint (loc->owner))
14883 target_enable_tracepoint (loc);
14884 }
44702360 14885 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14886 }
9eaabc75
MW
14887 else
14888 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14889 num = extract_arg (&args);
d248b706 14890 }
0d381245 14891 }
c906108c
SS
14892}
14893
816338b5
SS
14894/* This struct packages up disposition data for application to multiple
14895 breakpoints. */
14896
14897struct disp_data
14898{
14899 enum bpdisp disp;
14900 int count;
14901};
14902
c906108c 14903static void
51be5b68
PA
14904do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14905{
816338b5 14906 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14907
816338b5 14908 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14909}
14910
14911static void
14912do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14913{
816338b5 14914 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14915
14916 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14917}
14918
c906108c 14919static void
fba45db2 14920enable_once_command (char *args, int from_tty)
c906108c 14921{
51be5b68 14922 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14923}
14924
816338b5
SS
14925static void
14926do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14927{
14928 struct disp_data disp = { disp_disable, *(int *) countptr };
14929
14930 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14931}
14932
14933static void
14934enable_count_command (char *args, int from_tty)
14935{
b9d61307
SM
14936 int count;
14937
14938 if (args == NULL)
14939 error_no_arg (_("hit count"));
14940
14941 count = get_number (&args);
816338b5
SS
14942
14943 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14944}
14945
c906108c 14946static void
51be5b68 14947do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14948{
816338b5 14949 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14950
14951 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14952}
14953
c906108c 14954static void
fba45db2 14955enable_delete_command (char *args, int from_tty)
c906108c 14956{
51be5b68 14957 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14958}
14959\f
fa8d40ab
JJ
14960static void
14961set_breakpoint_cmd (char *args, int from_tty)
14962{
14963}
14964
14965static void
14966show_breakpoint_cmd (char *args, int from_tty)
14967{
14968}
14969
1f3b5d1b
PP
14970/* Invalidate last known value of any hardware watchpoint if
14971 the memory which that value represents has been written to by
14972 GDB itself. */
14973
14974static void
8de0566d
YQ
14975invalidate_bp_value_on_memory_change (struct inferior *inferior,
14976 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14977 const bfd_byte *data)
14978{
14979 struct breakpoint *bp;
14980
14981 ALL_BREAKPOINTS (bp)
14982 if (bp->enable_state == bp_enabled
3a5c3e22 14983 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14984 {
3a5c3e22 14985 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14986
3a5c3e22
PA
14987 if (wp->val_valid && wp->val)
14988 {
14989 struct bp_location *loc;
14990
14991 for (loc = bp->loc; loc != NULL; loc = loc->next)
14992 if (loc->loc_type == bp_loc_hardware_watchpoint
14993 && loc->address + loc->length > addr
14994 && addr + len > loc->address)
14995 {
14996 value_free (wp->val);
14997 wp->val = NULL;
14998 wp->val_valid = 0;
14999 }
15000 }
1f3b5d1b
PP
15001 }
15002}
15003
8181d85f
DJ
15004/* Create and insert a breakpoint for software single step. */
15005
15006void
6c95b8df 15007insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15008 struct address_space *aspace,
15009 CORE_ADDR next_pc)
8181d85f 15010{
7c16b83e
PA
15011 struct thread_info *tp = inferior_thread ();
15012 struct symtab_and_line sal;
15013 CORE_ADDR pc = next_pc;
8181d85f 15014
34b7e8a6
PA
15015 if (tp->control.single_step_breakpoints == NULL)
15016 {
15017 tp->control.single_step_breakpoints
5d5658a1 15018 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15019 }
8181d85f 15020
7c16b83e
PA
15021 sal = find_pc_line (pc, 0);
15022 sal.pc = pc;
15023 sal.section = find_pc_overlay (pc);
15024 sal.explicit_pc = 1;
34b7e8a6 15025 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15026
7c16b83e 15027 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15028}
15029
93f9a11f
YQ
15030/* Insert single step breakpoints according to the current state. */
15031
15032int
15033insert_single_step_breakpoints (struct gdbarch *gdbarch)
15034{
f5ea389a 15035 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 15036 std::vector<CORE_ADDR> next_pcs;
93f9a11f 15037
f5ea389a 15038 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 15039
a0ff9e1a 15040 if (!next_pcs.empty ())
93f9a11f 15041 {
f5ea389a 15042 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
15043 struct address_space *aspace = get_frame_address_space (frame);
15044
a0ff9e1a 15045 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
15046 insert_single_step_breakpoint (gdbarch, aspace, pc);
15047
93f9a11f
YQ
15048 return 1;
15049 }
15050 else
15051 return 0;
15052}
15053
34b7e8a6 15054/* See breakpoint.h. */
f02253f1
HZ
15055
15056int
7c16b83e
PA
15057breakpoint_has_location_inserted_here (struct breakpoint *bp,
15058 struct address_space *aspace,
15059 CORE_ADDR pc)
1aafd4da 15060{
7c16b83e 15061 struct bp_location *loc;
1aafd4da 15062
7c16b83e
PA
15063 for (loc = bp->loc; loc != NULL; loc = loc->next)
15064 if (loc->inserted
15065 && breakpoint_location_address_match (loc, aspace, pc))
15066 return 1;
1aafd4da 15067
7c16b83e 15068 return 0;
ef370185
JB
15069}
15070
15071/* Check whether a software single-step breakpoint is inserted at
15072 PC. */
15073
15074int
15075single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15076 CORE_ADDR pc)
15077{
34b7e8a6
PA
15078 struct breakpoint *bpt;
15079
15080 ALL_BREAKPOINTS (bpt)
15081 {
15082 if (bpt->type == bp_single_step
15083 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15084 return 1;
15085 }
15086 return 0;
1aafd4da
UW
15087}
15088
1042e4c0
SS
15089/* Tracepoint-specific operations. */
15090
15091/* Set tracepoint count to NUM. */
15092static void
15093set_tracepoint_count (int num)
15094{
15095 tracepoint_count = num;
4fa62494 15096 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15097}
15098
70221824 15099static void
1042e4c0
SS
15100trace_command (char *arg, int from_tty)
15101{
55aa24fb 15102 struct breakpoint_ops *ops;
55aa24fb 15103
ffc2605c
TT
15104 event_location_up location = string_to_event_location (&arg,
15105 current_language);
5b56227b 15106 if (location != NULL
ffc2605c 15107 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15108 ops = &tracepoint_probe_breakpoint_ops;
15109 else
15110 ops = &tracepoint_breakpoint_ops;
15111
558a9d82 15112 create_breakpoint (get_current_arch (),
ffc2605c 15113 location.get (),
f00aae0f 15114 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15115 0 /* tempflag */,
15116 bp_tracepoint /* type_wanted */,
15117 0 /* Ignore count */,
15118 pending_break_support,
15119 ops,
15120 from_tty,
15121 1 /* enabled */,
15122 0 /* internal */, 0);
1042e4c0
SS
15123}
15124
70221824 15125static void
7a697b8d
SS
15126ftrace_command (char *arg, int from_tty)
15127{
ffc2605c
TT
15128 event_location_up location = string_to_event_location (&arg,
15129 current_language);
558a9d82 15130 create_breakpoint (get_current_arch (),
ffc2605c 15131 location.get (),
f00aae0f 15132 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15133 0 /* tempflag */,
15134 bp_fast_tracepoint /* type_wanted */,
15135 0 /* Ignore count */,
15136 pending_break_support,
15137 &tracepoint_breakpoint_ops,
15138 from_tty,
15139 1 /* enabled */,
15140 0 /* internal */, 0);
0fb4aa4b
PA
15141}
15142
15143/* strace command implementation. Creates a static tracepoint. */
15144
70221824 15145static void
0fb4aa4b
PA
15146strace_command (char *arg, int from_tty)
15147{
983af33b 15148 struct breakpoint_ops *ops;
ffc2605c 15149 event_location_up location;
f00aae0f 15150 struct cleanup *back_to;
983af33b
SDJ
15151
15152 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15153 or with a normal static tracepoint. */
61012eef 15154 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15155 {
15156 ops = &strace_marker_breakpoint_ops;
15157 location = new_linespec_location (&arg);
15158 }
983af33b 15159 else
f00aae0f
KS
15160 {
15161 ops = &tracepoint_breakpoint_ops;
15162 location = string_to_event_location (&arg, current_language);
15163 }
983af33b 15164
558a9d82 15165 create_breakpoint (get_current_arch (),
ffc2605c 15166 location.get (),
f00aae0f 15167 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15168 0 /* tempflag */,
15169 bp_static_tracepoint /* type_wanted */,
15170 0 /* Ignore count */,
15171 pending_break_support,
15172 ops,
15173 from_tty,
15174 1 /* enabled */,
15175 0 /* internal */, 0);
7a697b8d
SS
15176}
15177
409873ef
SS
15178/* Set up a fake reader function that gets command lines from a linked
15179 list that was acquired during tracepoint uploading. */
15180
15181static struct uploaded_tp *this_utp;
3149d8c1 15182static int next_cmd;
409873ef
SS
15183
15184static char *
15185read_uploaded_action (void)
15186{
15187 char *rslt;
15188
3149d8c1 15189 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15190
3149d8c1 15191 next_cmd++;
409873ef
SS
15192
15193 return rslt;
15194}
15195
00bf0b85
SS
15196/* Given information about a tracepoint as recorded on a target (which
15197 can be either a live system or a trace file), attempt to create an
15198 equivalent GDB tracepoint. This is not a reliable process, since
15199 the target does not necessarily have all the information used when
15200 the tracepoint was originally defined. */
15201
d9b3f62e 15202struct tracepoint *
00bf0b85 15203create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15204{
409873ef 15205 char *addr_str, small_buf[100];
d9b3f62e 15206 struct tracepoint *tp;
fd9b8c24 15207
409873ef
SS
15208 if (utp->at_string)
15209 addr_str = utp->at_string;
15210 else
15211 {
15212 /* In the absence of a source location, fall back to raw
15213 address. Since there is no way to confirm that the address
15214 means the same thing as when the trace was started, warn the
15215 user. */
3e43a32a
MS
15216 warning (_("Uploaded tracepoint %d has no "
15217 "source location, using raw address"),
409873ef 15218 utp->number);
8c042590 15219 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15220 addr_str = small_buf;
15221 }
15222
15223 /* There's not much we can do with a sequence of bytecodes. */
15224 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15225 warning (_("Uploaded tracepoint %d condition "
15226 "has no source form, ignoring it"),
409873ef 15227 utp->number);
d5551862 15228
ffc2605c
TT
15229 event_location_up location = string_to_event_location (&addr_str,
15230 current_language);
8cdf0e15 15231 if (!create_breakpoint (get_current_arch (),
ffc2605c 15232 location.get (),
f00aae0f 15233 utp->cond_string, -1, addr_str,
e7e0cddf 15234 0 /* parse cond/thread */,
8cdf0e15 15235 0 /* tempflag */,
0fb4aa4b 15236 utp->type /* type_wanted */,
8cdf0e15
VP
15237 0 /* Ignore count */,
15238 pending_break_support,
348d480f 15239 &tracepoint_breakpoint_ops,
8cdf0e15 15240 0 /* from_tty */,
84f4c1fe 15241 utp->enabled /* enabled */,
44f238bb
PA
15242 0 /* internal */,
15243 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15244 return NULL;
fd9b8c24 15245
409873ef 15246 /* Get the tracepoint we just created. */
fd9b8c24
PA
15247 tp = get_tracepoint (tracepoint_count);
15248 gdb_assert (tp != NULL);
d5551862 15249
00bf0b85
SS
15250 if (utp->pass > 0)
15251 {
8c042590
PM
15252 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15253 tp->base.number);
00bf0b85 15254
409873ef 15255 trace_pass_command (small_buf, 0);
00bf0b85
SS
15256 }
15257
409873ef
SS
15258 /* If we have uploaded versions of the original commands, set up a
15259 special-purpose "reader" function and call the usual command line
15260 reader, then pass the result to the breakpoint command-setting
15261 function. */
3149d8c1 15262 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15263 {
93921405 15264 command_line_up cmd_list;
00bf0b85 15265
409873ef 15266 this_utp = utp;
3149d8c1 15267 next_cmd = 0;
d5551862 15268
409873ef
SS
15269 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15270
93921405 15271 breakpoint_set_commands (&tp->base, std::move (cmd_list));
00bf0b85 15272 }
3149d8c1
SS
15273 else if (!VEC_empty (char_ptr, utp->actions)
15274 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15275 warning (_("Uploaded tracepoint %d actions "
15276 "have no source form, ignoring them"),
409873ef 15277 utp->number);
00bf0b85 15278
f196051f
SS
15279 /* Copy any status information that might be available. */
15280 tp->base.hit_count = utp->hit_count;
15281 tp->traceframe_usage = utp->traceframe_usage;
15282
00bf0b85 15283 return tp;
d9b3f62e 15284}
00bf0b85 15285
1042e4c0
SS
15286/* Print information on tracepoint number TPNUM_EXP, or all if
15287 omitted. */
15288
15289static void
e5a67952 15290tracepoints_info (char *args, int from_tty)
1042e4c0 15291{
79a45e25 15292 struct ui_out *uiout = current_uiout;
e5a67952 15293 int num_printed;
1042e4c0 15294
e5a67952 15295 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15296
15297 if (num_printed == 0)
1042e4c0 15298 {
e5a67952 15299 if (args == NULL || *args == '\0')
112e8700 15300 uiout->message ("No tracepoints.\n");
d77f58be 15301 else
112e8700 15302 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15303 }
ad443146
SS
15304
15305 default_collect_info ();
1042e4c0
SS
15306}
15307
4a64f543 15308/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15309 Not supported by all targets. */
15310static void
15311enable_trace_command (char *args, int from_tty)
15312{
15313 enable_command (args, from_tty);
15314}
15315
4a64f543 15316/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15317 Not supported by all targets. */
15318static void
15319disable_trace_command (char *args, int from_tty)
15320{
15321 disable_command (args, from_tty);
15322}
15323
4a64f543 15324/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15325static void
15326delete_trace_command (char *arg, int from_tty)
15327{
35df4500 15328 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15329
15330 dont_repeat ();
15331
15332 if (arg == 0)
15333 {
15334 int breaks_to_delete = 0;
15335
15336 /* Delete all breakpoints if no argument.
15337 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15338 have to be deleted with an explicit breakpoint number
15339 argument. */
1042e4c0 15340 ALL_TRACEPOINTS (b)
46c6471b 15341 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15342 {
15343 breaks_to_delete = 1;
15344 break;
15345 }
1042e4c0
SS
15346
15347 /* Ask user only if there are some breakpoints to delete. */
15348 if (!from_tty
15349 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15350 {
35df4500 15351 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15352 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15353 delete_breakpoint (b);
1042e4c0
SS
15354 }
15355 }
15356 else
51be5b68 15357 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15358}
15359
197f0a60
TT
15360/* Helper function for trace_pass_command. */
15361
15362static void
d9b3f62e 15363trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15364{
d9b3f62e 15365 tp->pass_count = count;
6f6484cd 15366 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15367 if (from_tty)
15368 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15369 tp->base.number, count);
197f0a60
TT
15370}
15371
1042e4c0
SS
15372/* Set passcount for tracepoint.
15373
15374 First command argument is passcount, second is tracepoint number.
15375 If tracepoint number omitted, apply to most recently defined.
15376 Also accepts special argument "all". */
15377
15378static void
15379trace_pass_command (char *args, int from_tty)
15380{
d9b3f62e 15381 struct tracepoint *t1;
1042e4c0 15382 unsigned int count;
1042e4c0
SS
15383
15384 if (args == 0 || *args == 0)
3e43a32a
MS
15385 error (_("passcount command requires an "
15386 "argument (count + optional TP num)"));
1042e4c0 15387
4a64f543 15388 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15389
529480d0 15390 args = skip_spaces (args);
1042e4c0
SS
15391 if (*args && strncasecmp (args, "all", 3) == 0)
15392 {
d9b3f62e
PA
15393 struct breakpoint *b;
15394
1042e4c0 15395 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15396 if (*args)
15397 error (_("Junk at end of arguments."));
1042e4c0 15398
d9b3f62e 15399 ALL_TRACEPOINTS (b)
197f0a60 15400 {
d9b3f62e 15401 t1 = (struct tracepoint *) b;
197f0a60
TT
15402 trace_pass_set_count (t1, count, from_tty);
15403 }
15404 }
15405 else if (*args == '\0')
1042e4c0 15406 {
5fa1d40e 15407 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15408 if (t1)
197f0a60
TT
15409 trace_pass_set_count (t1, count, from_tty);
15410 }
15411 else
15412 {
bfd28288
PA
15413 number_or_range_parser parser (args);
15414 while (!parser.finished ())
1042e4c0 15415 {
bfd28288 15416 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15417 if (t1)
15418 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15419 }
15420 }
1042e4c0
SS
15421}
15422
d9b3f62e 15423struct tracepoint *
1042e4c0
SS
15424get_tracepoint (int num)
15425{
15426 struct breakpoint *t;
15427
15428 ALL_TRACEPOINTS (t)
15429 if (t->number == num)
d9b3f62e 15430 return (struct tracepoint *) t;
1042e4c0
SS
15431
15432 return NULL;
15433}
15434
d5551862
SS
15435/* Find the tracepoint with the given target-side number (which may be
15436 different from the tracepoint number after disconnecting and
15437 reconnecting). */
15438
d9b3f62e 15439struct tracepoint *
d5551862
SS
15440get_tracepoint_by_number_on_target (int num)
15441{
d9b3f62e 15442 struct breakpoint *b;
d5551862 15443
d9b3f62e
PA
15444 ALL_TRACEPOINTS (b)
15445 {
15446 struct tracepoint *t = (struct tracepoint *) b;
15447
15448 if (t->number_on_target == num)
15449 return t;
15450 }
d5551862
SS
15451
15452 return NULL;
15453}
15454
1042e4c0 15455/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15456 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15457 If the argument is missing, the most recent tracepoint
15458 (tracepoint_count) is returned. */
15459
d9b3f62e 15460struct tracepoint *
197f0a60 15461get_tracepoint_by_number (char **arg,
bfd28288 15462 number_or_range_parser *parser)
1042e4c0 15463{
1042e4c0
SS
15464 struct breakpoint *t;
15465 int tpnum;
15466 char *instring = arg == NULL ? NULL : *arg;
15467
bfd28288 15468 if (parser != NULL)
197f0a60 15469 {
bfd28288
PA
15470 gdb_assert (!parser->finished ());
15471 tpnum = parser->get_number ();
197f0a60
TT
15472 }
15473 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15474 tpnum = tracepoint_count;
1042e4c0 15475 else
197f0a60 15476 tpnum = get_number (arg);
1042e4c0
SS
15477
15478 if (tpnum <= 0)
15479 {
15480 if (instring && *instring)
15481 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15482 instring);
15483 else
5fa1d40e 15484 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15485 return NULL;
15486 }
15487
15488 ALL_TRACEPOINTS (t)
15489 if (t->number == tpnum)
15490 {
d9b3f62e 15491 return (struct tracepoint *) t;
1042e4c0
SS
15492 }
15493
1042e4c0
SS
15494 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15495 return NULL;
15496}
15497
d9b3f62e
PA
15498void
15499print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15500{
15501 if (b->thread != -1)
15502 fprintf_unfiltered (fp, " thread %d", b->thread);
15503
15504 if (b->task != 0)
15505 fprintf_unfiltered (fp, " task %d", b->task);
15506
15507 fprintf_unfiltered (fp, "\n");
15508}
15509
6149aea9
PA
15510/* Save information on user settable breakpoints (watchpoints, etc) to
15511 a new script file named FILENAME. If FILTER is non-NULL, call it
15512 on each breakpoint and only include the ones for which it returns
15513 non-zero. */
15514
1042e4c0 15515static void
6149aea9
PA
15516save_breakpoints (char *filename, int from_tty,
15517 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15518{
15519 struct breakpoint *tp;
6149aea9 15520 int any = 0;
1042e4c0 15521 struct cleanup *cleanup;
6149aea9 15522 int extra_trace_bits = 0;
1042e4c0 15523
6149aea9
PA
15524 if (filename == 0 || *filename == 0)
15525 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15526
15527 /* See if we have anything to save. */
6149aea9 15528 ALL_BREAKPOINTS (tp)
1042e4c0 15529 {
6149aea9 15530 /* Skip internal and momentary breakpoints. */
09d682a4 15531 if (!user_breakpoint_p (tp))
6149aea9
PA
15532 continue;
15533
15534 /* If we have a filter, only save the breakpoints it accepts. */
15535 if (filter && !filter (tp))
15536 continue;
15537
15538 any = 1;
15539
15540 if (is_tracepoint (tp))
15541 {
15542 extra_trace_bits = 1;
15543
15544 /* We can stop searching. */
15545 break;
15546 }
1042e4c0 15547 }
6149aea9
PA
15548
15549 if (!any)
1042e4c0 15550 {
6149aea9 15551 warning (_("Nothing to save."));
1042e4c0
SS
15552 return;
15553 }
15554
c718be47
PA
15555 filename = tilde_expand (filename);
15556 cleanup = make_cleanup (xfree, filename);
d7e74731
PA
15557
15558 stdio_file fp;
15559
15560 if (!fp.open (filename, "w"))
6149aea9
PA
15561 error (_("Unable to open file '%s' for saving (%s)"),
15562 filename, safe_strerror (errno));
8bf6485c 15563
6149aea9 15564 if (extra_trace_bits)
d7e74731 15565 save_trace_state_variables (&fp);
8bf6485c 15566
6149aea9 15567 ALL_BREAKPOINTS (tp)
1042e4c0 15568 {
6149aea9 15569 /* Skip internal and momentary breakpoints. */
09d682a4 15570 if (!user_breakpoint_p (tp))
6149aea9 15571 continue;
8bf6485c 15572
6149aea9
PA
15573 /* If we have a filter, only save the breakpoints it accepts. */
15574 if (filter && !filter (tp))
15575 continue;
15576
d7e74731 15577 tp->ops->print_recreate (tp, &fp);
1042e4c0 15578
6149aea9
PA
15579 /* Note, we can't rely on tp->number for anything, as we can't
15580 assume the recreated breakpoint numbers will match. Use $bpnum
15581 instead. */
15582
15583 if (tp->cond_string)
d7e74731 15584 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15585
15586 if (tp->ignore_count)
d7e74731 15587 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15588
2d9442cc 15589 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15590 {
d7e74731 15591 fp.puts (" commands\n");
a7bdde9e 15592
d7e74731 15593 current_uiout->redirect (&fp);
492d29ea 15594 TRY
1042e4c0 15595 {
79a45e25 15596 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15597 }
492d29ea
PA
15598 CATCH (ex, RETURN_MASK_ALL)
15599 {
112e8700 15600 current_uiout->redirect (NULL);
492d29ea
PA
15601 throw_exception (ex);
15602 }
15603 END_CATCH
1042e4c0 15604
112e8700 15605 current_uiout->redirect (NULL);
d7e74731 15606 fp.puts (" end\n");
1042e4c0 15607 }
6149aea9
PA
15608
15609 if (tp->enable_state == bp_disabled)
d7e74731 15610 fp.puts ("disable $bpnum\n");
6149aea9
PA
15611
15612 /* If this is a multi-location breakpoint, check if the locations
15613 should be individually disabled. Watchpoint locations are
15614 special, and not user visible. */
15615 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15616 {
15617 struct bp_location *loc;
15618 int n = 1;
15619
15620 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15621 if (!loc->enabled)
d7e74731 15622 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15623 }
1042e4c0 15624 }
8bf6485c 15625
6149aea9 15626 if (extra_trace_bits && *default_collect)
d7e74731 15627 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15628
1042e4c0 15629 if (from_tty)
6149aea9 15630 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15631 do_cleanups (cleanup);
6149aea9
PA
15632}
15633
15634/* The `save breakpoints' command. */
15635
15636static void
15637save_breakpoints_command (char *args, int from_tty)
15638{
15639 save_breakpoints (args, from_tty, NULL);
15640}
15641
15642/* The `save tracepoints' command. */
15643
15644static void
15645save_tracepoints_command (char *args, int from_tty)
15646{
15647 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15648}
15649
15650/* Create a vector of all tracepoints. */
15651
15652VEC(breakpoint_p) *
eeae04df 15653all_tracepoints (void)
1042e4c0
SS
15654{
15655 VEC(breakpoint_p) *tp_vec = 0;
15656 struct breakpoint *tp;
15657
15658 ALL_TRACEPOINTS (tp)
15659 {
15660 VEC_safe_push (breakpoint_p, tp_vec, tp);
15661 }
15662
15663 return tp_vec;
15664}
15665
c906108c 15666\f
629500fa
KS
15667/* This help string is used to consolidate all the help string for specifying
15668 locations used by several commands. */
15669
15670#define LOCATION_HELP_STRING \
15671"Linespecs are colon-separated lists of location parameters, such as\n\
15672source filename, function name, label name, and line number.\n\
15673Example: To specify the start of a label named \"the_top\" in the\n\
15674function \"fact\" in the file \"factorial.c\", use\n\
15675\"factorial.c:fact:the_top\".\n\
15676\n\
15677Address locations begin with \"*\" and specify an exact address in the\n\
15678program. Example: To specify the fourth byte past the start function\n\
15679\"main\", use \"*main + 4\".\n\
15680\n\
15681Explicit locations are similar to linespecs but use an option/argument\n\
15682syntax to specify location parameters.\n\
15683Example: To specify the start of the label named \"the_top\" in the\n\
15684function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15685-function fact -label the_top\".\n"
15686
4a64f543
MS
15687/* This help string is used for the break, hbreak, tbreak and thbreak
15688 commands. It is defined as a macro to prevent duplication.
15689 COMMAND should be a string constant containing the name of the
15690 command. */
629500fa 15691
31e2b00f 15692#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15693command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15694PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15695probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15696guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15697`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15698LOCATION may be a linespec, address, or explicit location as described\n\
15699below.\n\
15700\n\
dc10affe
PA
15701With no LOCATION, uses current execution address of the selected\n\
15702stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15703\n\
15704THREADNUM is the number from \"info threads\".\n\
15705CONDITION is a boolean expression.\n\
629500fa 15706\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15707Multiple breakpoints at one place are permitted, and useful if their\n\
15708conditions are different.\n\
31e2b00f
AS
15709\n\
15710Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15711
44feb3ce
TT
15712/* List of subcommands for "catch". */
15713static struct cmd_list_element *catch_cmdlist;
15714
15715/* List of subcommands for "tcatch". */
15716static struct cmd_list_element *tcatch_cmdlist;
15717
9ac4176b 15718void
a121b7c1 15719add_catch_command (const char *name, const char *docstring,
82ae6c8d 15720 cmd_sfunc_ftype *sfunc,
625e8578 15721 completer_ftype *completer,
44feb3ce
TT
15722 void *user_data_catch,
15723 void *user_data_tcatch)
15724{
15725 struct cmd_list_element *command;
15726
15727 command = add_cmd (name, class_breakpoint, NULL, docstring,
15728 &catch_cmdlist);
15729 set_cmd_sfunc (command, sfunc);
15730 set_cmd_context (command, user_data_catch);
a96d9b2e 15731 set_cmd_completer (command, completer);
44feb3ce
TT
15732
15733 command = add_cmd (name, class_breakpoint, NULL, docstring,
15734 &tcatch_cmdlist);
15735 set_cmd_sfunc (command, sfunc);
15736 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15737 set_cmd_completer (command, completer);
44feb3ce
TT
15738}
15739
6149aea9
PA
15740static void
15741save_command (char *arg, int from_tty)
15742{
3e43a32a
MS
15743 printf_unfiltered (_("\"save\" must be followed by "
15744 "the name of a save subcommand.\n"));
635c7e8a 15745 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15746}
15747
84f4c1fe
PM
15748struct breakpoint *
15749iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15750 void *data)
15751{
35df4500 15752 struct breakpoint *b, *b_tmp;
84f4c1fe 15753
35df4500 15754 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15755 {
15756 if ((*callback) (b, data))
15757 return b;
15758 }
15759
15760 return NULL;
15761}
15762
0574c78f
GB
15763/* Zero if any of the breakpoint's locations could be a location where
15764 functions have been inlined, nonzero otherwise. */
15765
15766static int
15767is_non_inline_function (struct breakpoint *b)
15768{
15769 /* The shared library event breakpoint is set on the address of a
15770 non-inline function. */
15771 if (b->type == bp_shlib_event)
15772 return 1;
15773
15774 return 0;
15775}
15776
15777/* Nonzero if the specified PC cannot be a location where functions
15778 have been inlined. */
15779
15780int
09ac7c10
TT
15781pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15782 const struct target_waitstatus *ws)
0574c78f
GB
15783{
15784 struct breakpoint *b;
15785 struct bp_location *bl;
15786
15787 ALL_BREAKPOINTS (b)
15788 {
15789 if (!is_non_inline_function (b))
15790 continue;
15791
15792 for (bl = b->loc; bl != NULL; bl = bl->next)
15793 {
15794 if (!bl->shlib_disabled
09ac7c10 15795 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15796 return 1;
15797 }
15798 }
15799
15800 return 0;
15801}
15802
2f202fde
JK
15803/* Remove any references to OBJFILE which is going to be freed. */
15804
15805void
15806breakpoint_free_objfile (struct objfile *objfile)
15807{
15808 struct bp_location **locp, *loc;
15809
15810 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15811 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15812 loc->symtab = NULL;
15813}
15814
2060206e
PA
15815void
15816initialize_breakpoint_ops (void)
15817{
15818 static int initialized = 0;
15819
15820 struct breakpoint_ops *ops;
15821
15822 if (initialized)
15823 return;
15824 initialized = 1;
15825
15826 /* The breakpoint_ops structure to be inherit by all kinds of
15827 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15828 internal and momentary breakpoints, etc.). */
15829 ops = &bkpt_base_breakpoint_ops;
15830 *ops = base_breakpoint_ops;
15831 ops->re_set = bkpt_re_set;
15832 ops->insert_location = bkpt_insert_location;
15833 ops->remove_location = bkpt_remove_location;
15834 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15835 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15836 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15837 ops->decode_location = bkpt_decode_location;
2060206e
PA
15838
15839 /* The breakpoint_ops structure to be used in regular breakpoints. */
15840 ops = &bkpt_breakpoint_ops;
15841 *ops = bkpt_base_breakpoint_ops;
15842 ops->re_set = bkpt_re_set;
15843 ops->resources_needed = bkpt_resources_needed;
15844 ops->print_it = bkpt_print_it;
15845 ops->print_mention = bkpt_print_mention;
15846 ops->print_recreate = bkpt_print_recreate;
15847
15848 /* Ranged breakpoints. */
15849 ops = &ranged_breakpoint_ops;
15850 *ops = bkpt_breakpoint_ops;
15851 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15852 ops->resources_needed = resources_needed_ranged_breakpoint;
15853 ops->print_it = print_it_ranged_breakpoint;
15854 ops->print_one = print_one_ranged_breakpoint;
15855 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15856 ops->print_mention = print_mention_ranged_breakpoint;
15857 ops->print_recreate = print_recreate_ranged_breakpoint;
15858
15859 /* Internal breakpoints. */
15860 ops = &internal_breakpoint_ops;
15861 *ops = bkpt_base_breakpoint_ops;
15862 ops->re_set = internal_bkpt_re_set;
15863 ops->check_status = internal_bkpt_check_status;
15864 ops->print_it = internal_bkpt_print_it;
15865 ops->print_mention = internal_bkpt_print_mention;
15866
15867 /* Momentary breakpoints. */
15868 ops = &momentary_breakpoint_ops;
15869 *ops = bkpt_base_breakpoint_ops;
15870 ops->re_set = momentary_bkpt_re_set;
15871 ops->check_status = momentary_bkpt_check_status;
15872 ops->print_it = momentary_bkpt_print_it;
15873 ops->print_mention = momentary_bkpt_print_mention;
15874
e2e4d78b
JK
15875 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15876 ops = &longjmp_breakpoint_ops;
15877 *ops = momentary_breakpoint_ops;
15878 ops->dtor = longjmp_bkpt_dtor;
15879
55aa24fb
SDJ
15880 /* Probe breakpoints. */
15881 ops = &bkpt_probe_breakpoint_ops;
15882 *ops = bkpt_breakpoint_ops;
15883 ops->insert_location = bkpt_probe_insert_location;
15884 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15885 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15886 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15887
2060206e
PA
15888 /* Watchpoints. */
15889 ops = &watchpoint_breakpoint_ops;
15890 *ops = base_breakpoint_ops;
3a5c3e22 15891 ops->dtor = dtor_watchpoint;
2060206e
PA
15892 ops->re_set = re_set_watchpoint;
15893 ops->insert_location = insert_watchpoint;
15894 ops->remove_location = remove_watchpoint;
15895 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15896 ops->check_status = check_status_watchpoint;
15897 ops->resources_needed = resources_needed_watchpoint;
15898 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15899 ops->print_it = print_it_watchpoint;
15900 ops->print_mention = print_mention_watchpoint;
15901 ops->print_recreate = print_recreate_watchpoint;
427cd150 15902 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15903
15904 /* Masked watchpoints. */
15905 ops = &masked_watchpoint_breakpoint_ops;
15906 *ops = watchpoint_breakpoint_ops;
15907 ops->insert_location = insert_masked_watchpoint;
15908 ops->remove_location = remove_masked_watchpoint;
15909 ops->resources_needed = resources_needed_masked_watchpoint;
15910 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15911 ops->print_it = print_it_masked_watchpoint;
15912 ops->print_one_detail = print_one_detail_masked_watchpoint;
15913 ops->print_mention = print_mention_masked_watchpoint;
15914 ops->print_recreate = print_recreate_masked_watchpoint;
15915
15916 /* Tracepoints. */
15917 ops = &tracepoint_breakpoint_ops;
15918 *ops = base_breakpoint_ops;
15919 ops->re_set = tracepoint_re_set;
15920 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15921 ops->print_one_detail = tracepoint_print_one_detail;
15922 ops->print_mention = tracepoint_print_mention;
15923 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15924 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15925 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15926 ops->decode_location = tracepoint_decode_location;
983af33b 15927
55aa24fb
SDJ
15928 /* Probe tracepoints. */
15929 ops = &tracepoint_probe_breakpoint_ops;
15930 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15931 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15932 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15933
983af33b
SDJ
15934 /* Static tracepoints with marker (`-m'). */
15935 ops = &strace_marker_breakpoint_ops;
15936 *ops = tracepoint_breakpoint_ops;
5f700d83 15937 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15938 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15939 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15940
15941 /* Fork catchpoints. */
15942 ops = &catch_fork_breakpoint_ops;
15943 *ops = base_breakpoint_ops;
15944 ops->insert_location = insert_catch_fork;
15945 ops->remove_location = remove_catch_fork;
15946 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15947 ops->print_it = print_it_catch_fork;
15948 ops->print_one = print_one_catch_fork;
15949 ops->print_mention = print_mention_catch_fork;
15950 ops->print_recreate = print_recreate_catch_fork;
15951
15952 /* Vfork catchpoints. */
15953 ops = &catch_vfork_breakpoint_ops;
15954 *ops = base_breakpoint_ops;
15955 ops->insert_location = insert_catch_vfork;
15956 ops->remove_location = remove_catch_vfork;
15957 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15958 ops->print_it = print_it_catch_vfork;
15959 ops->print_one = print_one_catch_vfork;
15960 ops->print_mention = print_mention_catch_vfork;
15961 ops->print_recreate = print_recreate_catch_vfork;
15962
15963 /* Exec catchpoints. */
15964 ops = &catch_exec_breakpoint_ops;
15965 *ops = base_breakpoint_ops;
15966 ops->dtor = dtor_catch_exec;
15967 ops->insert_location = insert_catch_exec;
15968 ops->remove_location = remove_catch_exec;
15969 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15970 ops->print_it = print_it_catch_exec;
15971 ops->print_one = print_one_catch_exec;
15972 ops->print_mention = print_mention_catch_exec;
15973 ops->print_recreate = print_recreate_catch_exec;
15974
edcc5120
TT
15975 /* Solib-related catchpoints. */
15976 ops = &catch_solib_breakpoint_ops;
15977 *ops = base_breakpoint_ops;
15978 ops->dtor = dtor_catch_solib;
15979 ops->insert_location = insert_catch_solib;
15980 ops->remove_location = remove_catch_solib;
15981 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15982 ops->check_status = check_status_catch_solib;
15983 ops->print_it = print_it_catch_solib;
15984 ops->print_one = print_one_catch_solib;
15985 ops->print_mention = print_mention_catch_solib;
15986 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15987
15988 ops = &dprintf_breakpoint_ops;
15989 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15990 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15991 ops->resources_needed = bkpt_resources_needed;
15992 ops->print_it = bkpt_print_it;
15993 ops->print_mention = bkpt_print_mention;
2d9442cc 15994 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15995 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15996 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15997}
15998
8bfd80db
YQ
15999/* Chain containing all defined "enable breakpoint" subcommands. */
16000
16001static struct cmd_list_element *enablebreaklist = NULL;
16002
c906108c 16003void
fba45db2 16004_initialize_breakpoint (void)
c906108c
SS
16005{
16006 struct cmd_list_element *c;
16007
2060206e
PA
16008 initialize_breakpoint_ops ();
16009
84acb35a 16010 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16011 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16012 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16013
55aa24fb
SDJ
16014 breakpoint_objfile_key
16015 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16016
c906108c
SS
16017 breakpoint_chain = 0;
16018 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16019 before a breakpoint is set. */
16020 breakpoint_count = 0;
16021
1042e4c0
SS
16022 tracepoint_count = 0;
16023
1bedd215
AC
16024 add_com ("ignore", class_breakpoint, ignore_command, _("\
16025Set ignore-count of breakpoint number N to COUNT.\n\
16026Usage is `ignore N COUNT'."));
c906108c 16027
1bedd215 16028 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
16029Set commands to be executed when the given breakpoints are hit.\n\
16030Give a space-separated breakpoint list as argument after \"commands\".\n\
16031A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16032(e.g. `5-7').\n\
c906108c
SS
16033With no argument, the targeted breakpoint is the last one set.\n\
16034The commands themselves follow starting on the next line.\n\
16035Type a line containing \"end\" to indicate the end of them.\n\
16036Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16037then no output is printed when it is hit, except what the commands print."));
c906108c 16038
d55637df 16039 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16040Specify breakpoint number N to break only if COND is true.\n\
c906108c 16041Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16042expression to be evaluated whenever breakpoint N is reached."));
d55637df 16043 set_cmd_completer (c, condition_completer);
c906108c 16044
1bedd215 16045 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16046Set a temporary breakpoint.\n\
c906108c
SS
16047Like \"break\" except the breakpoint is only temporary,\n\
16048so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16049by using \"enable delete\" on the breakpoint number.\n\
16050\n"
16051BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16052 set_cmd_completer (c, location_completer);
c94fdfd0 16053
1bedd215 16054 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16055Set a hardware assisted breakpoint.\n\
c906108c 16056Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16057some target hardware may not have this support.\n\
16058\n"
16059BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16060 set_cmd_completer (c, location_completer);
c906108c 16061
1bedd215 16062 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16063Set a temporary hardware assisted breakpoint.\n\
c906108c 16064Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16065so it will be deleted when hit.\n\
16066\n"
16067BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16068 set_cmd_completer (c, location_completer);
c906108c 16069
1bedd215
AC
16070 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16071Enable some breakpoints.\n\
c906108c
SS
16072Give breakpoint numbers (separated by spaces) as arguments.\n\
16073With no subcommand, breakpoints are enabled until you command otherwise.\n\
16074This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16075With a subcommand you can enable temporarily."),
c906108c 16076 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16077
16078 add_com_alias ("en", "enable", class_breakpoint, 1);
16079
84951ab5 16080 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16081Enable some breakpoints.\n\
c906108c
SS
16082Give breakpoint numbers (separated by spaces) as arguments.\n\
16083This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16084May be abbreviated to simply \"enable\".\n"),
c5aa993b 16085 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16086
1a966eab
AC
16087 add_cmd ("once", no_class, enable_once_command, _("\
16088Enable breakpoints for one hit. Give breakpoint numbers.\n\
16089If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16090 &enablebreaklist);
16091
1a966eab
AC
16092 add_cmd ("delete", no_class, enable_delete_command, _("\
16093Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16094If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16095 &enablebreaklist);
16096
816338b5
SS
16097 add_cmd ("count", no_class, enable_count_command, _("\
16098Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16099If a breakpoint is hit while enabled in this fashion,\n\
16100the count is decremented; when it reaches zero, the breakpoint is disabled."),
16101 &enablebreaklist);
16102
1a966eab
AC
16103 add_cmd ("delete", no_class, enable_delete_command, _("\
16104Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16105If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16106 &enablelist);
16107
1a966eab
AC
16108 add_cmd ("once", no_class, enable_once_command, _("\
16109Enable breakpoints for one hit. Give breakpoint numbers.\n\
16110If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16111 &enablelist);
16112
16113 add_cmd ("count", no_class, enable_count_command, _("\
16114Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16115If a breakpoint is hit while enabled in this fashion,\n\
16116the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16117 &enablelist);
16118
1bedd215
AC
16119 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16120Disable some breakpoints.\n\
c906108c
SS
16121Arguments are breakpoint numbers with spaces in between.\n\
16122To disable all breakpoints, give no argument.\n\
64b9b334 16123A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16124 &disablelist, "disable ", 1, &cmdlist);
16125 add_com_alias ("dis", "disable", class_breakpoint, 1);
16126 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16127
1a966eab
AC
16128 add_cmd ("breakpoints", class_alias, disable_command, _("\
16129Disable some breakpoints.\n\
c906108c
SS
16130Arguments are breakpoint numbers with spaces in between.\n\
16131To disable all breakpoints, give no argument.\n\
64b9b334 16132A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16133This command may be abbreviated \"disable\"."),
c906108c
SS
16134 &disablelist);
16135
1bedd215
AC
16136 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16137Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16138Arguments are breakpoint numbers with spaces in between.\n\
16139To delete all breakpoints, give no argument.\n\
16140\n\
16141Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16142The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16143 &deletelist, "delete ", 1, &cmdlist);
16144 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16145 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16146
1a966eab
AC
16147 add_cmd ("breakpoints", class_alias, delete_command, _("\
16148Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16149Arguments are breakpoint numbers with spaces in between.\n\
16150To delete all breakpoints, give no argument.\n\
1a966eab 16151This command may be abbreviated \"delete\"."),
c906108c
SS
16152 &deletelist);
16153
1bedd215 16154 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16155Clear breakpoint at specified location.\n\
16156Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16157\n\
16158With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16159is executing in.\n"
16160"\n" LOCATION_HELP_STRING "\n\
1bedd215 16161See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16162 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16163
1bedd215 16164 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16165Set breakpoint at specified location.\n"
31e2b00f 16166BREAK_ARGS_HELP ("break")));
5ba2abeb 16167 set_cmd_completer (c, location_completer);
c94fdfd0 16168
c906108c
SS
16169 add_com_alias ("b", "break", class_run, 1);
16170 add_com_alias ("br", "break", class_run, 1);
16171 add_com_alias ("bre", "break", class_run, 1);
16172 add_com_alias ("brea", "break", class_run, 1);
16173
c906108c
SS
16174 if (dbx_commands)
16175 {
1bedd215
AC
16176 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16177Break in function/address or break at a line in the current file."),
c5aa993b
JM
16178 &stoplist, "stop ", 1, &cmdlist);
16179 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16180 _("Break in function or address."), &stoplist);
c5aa993b 16181 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16182 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16183 add_com ("status", class_info, breakpoints_info, _("\
16184Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16185The \"Type\" column indicates one of:\n\
16186\tbreakpoint - normal breakpoint\n\
16187\twatchpoint - watchpoint\n\
16188The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16189the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16190breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16191address and file/line number respectively.\n\
16192\n\
16193Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16194are set to the address of the last breakpoint listed unless the command\n\
16195is prefixed with \"server \".\n\n\
c906108c 16196Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16197breakpoint set."));
c906108c
SS
16198 }
16199
1bedd215 16200 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16201Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16202The \"Type\" column indicates one of:\n\
16203\tbreakpoint - normal breakpoint\n\
16204\twatchpoint - watchpoint\n\
16205The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16206the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16207breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16208address and file/line number respectively.\n\
16209\n\
16210Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16211are set to the address of the last breakpoint listed unless the command\n\
16212is prefixed with \"server \".\n\n\
c906108c 16213Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16214breakpoint set."));
c906108c 16215
6b04bdb7
MS
16216 add_info_alias ("b", "breakpoints", 1);
16217
1a966eab
AC
16218 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16219Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16220The \"Type\" column indicates one of:\n\
16221\tbreakpoint - normal breakpoint\n\
16222\twatchpoint - watchpoint\n\
16223\tlongjmp - internal breakpoint used to step through longjmp()\n\
16224\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16225\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16226\tfinish - internal breakpoint used by the \"finish\" command\n\
16227The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16228the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16229breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16230address and file/line number respectively.\n\
16231\n\
16232Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16233are set to the address of the last breakpoint listed unless the command\n\
16234is prefixed with \"server \".\n\n\
c906108c 16235Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16236breakpoint set."),
c906108c
SS
16237 &maintenanceinfolist);
16238
44feb3ce
TT
16239 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16240Set catchpoints to catch events."),
16241 &catch_cmdlist, "catch ",
16242 0/*allow-unknown*/, &cmdlist);
16243
16244 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16245Set temporary catchpoints to catch events."),
16246 &tcatch_cmdlist, "tcatch ",
16247 0/*allow-unknown*/, &cmdlist);
16248
44feb3ce
TT
16249 add_catch_command ("fork", _("Catch calls to fork."),
16250 catch_fork_command_1,
a96d9b2e 16251 NULL,
44feb3ce
TT
16252 (void *) (uintptr_t) catch_fork_permanent,
16253 (void *) (uintptr_t) catch_fork_temporary);
16254 add_catch_command ("vfork", _("Catch calls to vfork."),
16255 catch_fork_command_1,
a96d9b2e 16256 NULL,
44feb3ce
TT
16257 (void *) (uintptr_t) catch_vfork_permanent,
16258 (void *) (uintptr_t) catch_vfork_temporary);
16259 add_catch_command ("exec", _("Catch calls to exec."),
16260 catch_exec_command_1,
a96d9b2e
SDJ
16261 NULL,
16262 CATCH_PERMANENT,
16263 CATCH_TEMPORARY);
edcc5120
TT
16264 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16265Usage: catch load [REGEX]\n\
16266If REGEX is given, only stop for libraries matching the regular expression."),
16267 catch_load_command_1,
16268 NULL,
16269 CATCH_PERMANENT,
16270 CATCH_TEMPORARY);
16271 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16272Usage: catch unload [REGEX]\n\
16273If REGEX is given, only stop for libraries matching the regular expression."),
16274 catch_unload_command_1,
16275 NULL,
16276 CATCH_PERMANENT,
16277 CATCH_TEMPORARY);
c5aa993b 16278
1bedd215
AC
16279 c = add_com ("watch", class_breakpoint, watch_command, _("\
16280Set a watchpoint for an expression.\n\
06a64a0b 16281Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16282A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16283an expression changes.\n\
16284If -l or -location is given, this evaluates EXPRESSION and watches\n\
16285the memory to which it refers."));
65d12d83 16286 set_cmd_completer (c, expression_completer);
c906108c 16287
1bedd215
AC
16288 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16289Set a read watchpoint for an expression.\n\
06a64a0b 16290Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16291A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16292an expression is read.\n\
16293If -l or -location is given, this evaluates EXPRESSION and watches\n\
16294the memory to which it refers."));
65d12d83 16295 set_cmd_completer (c, expression_completer);
c906108c 16296
1bedd215
AC
16297 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16298Set a watchpoint for an expression.\n\
06a64a0b 16299Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16300A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16301an expression is either read or written.\n\
16302If -l or -location is given, this evaluates EXPRESSION and watches\n\
16303the memory to which it refers."));
65d12d83 16304 set_cmd_completer (c, expression_completer);
c906108c 16305
d77f58be 16306 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16307Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16308
920d2a44
AC
16309 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16310 respond to changes - contrary to the description. */
85c07804
AC
16311 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16312 &can_use_hw_watchpoints, _("\
16313Set debugger's willingness to use watchpoint hardware."), _("\
16314Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16315If zero, gdb will not use hardware for new watchpoints, even if\n\
16316such is available. (However, any hardware watchpoints that were\n\
16317created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16318hardware.)"),
16319 NULL,
920d2a44 16320 show_can_use_hw_watchpoints,
85c07804 16321 &setlist, &showlist);
c906108c
SS
16322
16323 can_use_hw_watchpoints = 1;
fa8d40ab 16324
1042e4c0
SS
16325 /* Tracepoint manipulation commands. */
16326
16327 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16328Set a tracepoint at specified location.\n\
1042e4c0
SS
16329\n"
16330BREAK_ARGS_HELP ("trace") "\n\
16331Do \"help tracepoints\" for info on other tracepoint commands."));
16332 set_cmd_completer (c, location_completer);
16333
16334 add_com_alias ("tp", "trace", class_alias, 0);
16335 add_com_alias ("tr", "trace", class_alias, 1);
16336 add_com_alias ("tra", "trace", class_alias, 1);
16337 add_com_alias ("trac", "trace", class_alias, 1);
16338
7a697b8d 16339 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16340Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16341\n"
16342BREAK_ARGS_HELP ("ftrace") "\n\
16343Do \"help tracepoints\" for info on other tracepoint commands."));
16344 set_cmd_completer (c, location_completer);
16345
0fb4aa4b 16346 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16347Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16348\n\
16349strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16350LOCATION may be a linespec, explicit, or address location (described below) \n\
16351or -m MARKER_ID.\n\n\
16352If a marker id is specified, probe the marker with that name. With\n\
16353no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16354Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16355This collects arbitrary user data passed in the probe point call to the\n\
16356tracing library. You can inspect it when analyzing the trace buffer,\n\
16357by printing the $_sdata variable like any other convenience variable.\n\
16358\n\
16359CONDITION is a boolean expression.\n\
629500fa 16360\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16361Multiple tracepoints at one place are permitted, and useful if their\n\
16362conditions are different.\n\
0fb4aa4b
PA
16363\n\
16364Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16365Do \"help tracepoints\" for info on other tracepoint commands."));
16366 set_cmd_completer (c, location_completer);
16367
1042e4c0 16368 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16369Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16370Convenience variable \"$tpnum\" contains the number of the\n\
16371last tracepoint set."));
16372
16373 add_info_alias ("tp", "tracepoints", 1);
16374
16375 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16376Delete specified tracepoints.\n\
16377Arguments are tracepoint numbers, separated by spaces.\n\
16378No argument means delete all tracepoints."),
16379 &deletelist);
7e20dfcd 16380 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16381
16382 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16383Disable specified tracepoints.\n\
16384Arguments are tracepoint numbers, separated by spaces.\n\
16385No argument means disable all tracepoints."),
16386 &disablelist);
16387 deprecate_cmd (c, "disable");
16388
16389 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16390Enable specified tracepoints.\n\
16391Arguments are tracepoint numbers, separated by spaces.\n\
16392No argument means enable all tracepoints."),
16393 &enablelist);
16394 deprecate_cmd (c, "enable");
16395
16396 add_com ("passcount", class_trace, trace_pass_command, _("\
16397Set the passcount for a tracepoint.\n\
16398The trace will end when the tracepoint has been passed 'count' times.\n\
16399Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16400if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16401
6149aea9
PA
16402 add_prefix_cmd ("save", class_breakpoint, save_command,
16403 _("Save breakpoint definitions as a script."),
16404 &save_cmdlist, "save ",
16405 0/*allow-unknown*/, &cmdlist);
16406
16407 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16408Save current breakpoint definitions as a script.\n\
cce7e648 16409This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16410catchpoints, tracepoints). Use the 'source' command in another debug\n\
16411session to restore them."),
16412 &save_cmdlist);
16413 set_cmd_completer (c, filename_completer);
16414
16415 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16416Save current tracepoint definitions as a script.\n\
6149aea9
PA
16417Use the 'source' command in another debug session to restore them."),
16418 &save_cmdlist);
1042e4c0
SS
16419 set_cmd_completer (c, filename_completer);
16420
6149aea9
PA
16421 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16422 deprecate_cmd (c, "save tracepoints");
16423
1bedd215 16424 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16425Breakpoint specific settings\n\
16426Configure various breakpoint-specific variables such as\n\
1bedd215 16427pending breakpoint behavior"),
fa8d40ab
JJ
16428 &breakpoint_set_cmdlist, "set breakpoint ",
16429 0/*allow-unknown*/, &setlist);
1bedd215 16430 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16431Breakpoint specific settings\n\
16432Configure various breakpoint-specific variables such as\n\
1bedd215 16433pending breakpoint behavior"),
fa8d40ab
JJ
16434 &breakpoint_show_cmdlist, "show breakpoint ",
16435 0/*allow-unknown*/, &showlist);
16436
7915a72c
AC
16437 add_setshow_auto_boolean_cmd ("pending", no_class,
16438 &pending_break_support, _("\
16439Set debugger's behavior regarding pending breakpoints."), _("\
16440Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16441If on, an unrecognized breakpoint location will cause gdb to create a\n\
16442pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16443an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16444user-query to see if a pending breakpoint should be created."),
2c5b56ce 16445 NULL,
920d2a44 16446 show_pending_break_support,
6e1d7d6c
AC
16447 &breakpoint_set_cmdlist,
16448 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16449
16450 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16451
16452 add_setshow_boolean_cmd ("auto-hw", no_class,
16453 &automatic_hardware_breakpoints, _("\
16454Set automatic usage of hardware breakpoints."), _("\
16455Show automatic usage of hardware breakpoints."), _("\
16456If set, the debugger will automatically use hardware breakpoints for\n\
16457breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16458a warning will be emitted for such breakpoints."),
16459 NULL,
16460 show_automatic_hardware_breakpoints,
16461 &breakpoint_set_cmdlist,
16462 &breakpoint_show_cmdlist);
74960c60 16463
a25a5a45
PA
16464 add_setshow_boolean_cmd ("always-inserted", class_support,
16465 &always_inserted_mode, _("\
74960c60
VP
16466Set mode for inserting breakpoints."), _("\
16467Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16468When this mode is on, breakpoints are inserted immediately as soon as\n\
16469they're created, kept inserted even when execution stops, and removed\n\
16470only when the user deletes them. When this mode is off (the default),\n\
16471breakpoints are inserted only when execution continues, and removed\n\
16472when execution stops."),
72d0e2c5
YQ
16473 NULL,
16474 &show_always_inserted_mode,
16475 &breakpoint_set_cmdlist,
16476 &breakpoint_show_cmdlist);
f1310107 16477
b775012e
LM
16478 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16479 condition_evaluation_enums,
16480 &condition_evaluation_mode_1, _("\
16481Set mode of breakpoint condition evaluation."), _("\
16482Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16483When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16484evaluated on the host's side by GDB. When it is set to \"target\",\n\
16485breakpoint conditions will be downloaded to the target (if the target\n\
16486supports such feature) and conditions will be evaluated on the target's side.\n\
16487If this is set to \"auto\" (default), this will be automatically set to\n\
16488\"target\" if it supports condition evaluation, otherwise it will\n\
16489be set to \"gdb\""),
16490 &set_condition_evaluation_mode,
16491 &show_condition_evaluation_mode,
16492 &breakpoint_set_cmdlist,
16493 &breakpoint_show_cmdlist);
16494
f1310107
TJB
16495 add_com ("break-range", class_breakpoint, break_range_command, _("\
16496Set a breakpoint for an address range.\n\
16497break-range START-LOCATION, END-LOCATION\n\
16498where START-LOCATION and END-LOCATION can be one of the following:\n\
16499 LINENUM, for that line in the current file,\n\
16500 FILE:LINENUM, for that line in that file,\n\
16501 +OFFSET, for that number of lines after the current line\n\
16502 or the start of the range\n\
16503 FUNCTION, for the first line in that function,\n\
16504 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16505 *ADDRESS, for the instruction at that address.\n\
16506\n\
16507The breakpoint will stop execution of the inferior whenever it executes\n\
16508an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16509range (including START-LOCATION and END-LOCATION)."));
16510
e7e0cddf 16511 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16512Set a dynamic printf at specified location.\n\
e7e0cddf 16513dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16514location may be a linespec, explicit, or address location.\n"
16515"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16516 set_cmd_completer (c, location_completer);
16517
16518 add_setshow_enum_cmd ("dprintf-style", class_support,
16519 dprintf_style_enums, &dprintf_style, _("\
16520Set the style of usage for dynamic printf."), _("\
16521Show the style of usage for dynamic printf."), _("\
16522This setting chooses how GDB will do a dynamic printf.\n\
16523If the value is \"gdb\", then the printing is done by GDB to its own\n\
16524console, as with the \"printf\" command.\n\
16525If the value is \"call\", the print is done by calling a function in your\n\
16526program; by default printf(), but you can choose a different function or\n\
16527output stream by setting dprintf-function and dprintf-channel."),
16528 update_dprintf_commands, NULL,
16529 &setlist, &showlist);
16530
16531 dprintf_function = xstrdup ("printf");
16532 add_setshow_string_cmd ("dprintf-function", class_support,
16533 &dprintf_function, _("\
16534Set the function to use for dynamic printf"), _("\
16535Show the function to use for dynamic printf"), NULL,
16536 update_dprintf_commands, NULL,
16537 &setlist, &showlist);
16538
16539 dprintf_channel = xstrdup ("");
16540 add_setshow_string_cmd ("dprintf-channel", class_support,
16541 &dprintf_channel, _("\
16542Set the channel to use for dynamic printf"), _("\
16543Show the channel to use for dynamic printf"), NULL,
16544 update_dprintf_commands, NULL,
16545 &setlist, &showlist);
16546
d3ce09f5
SS
16547 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16548 &disconnected_dprintf, _("\
16549Set whether dprintf continues after GDB disconnects."), _("\
16550Show whether dprintf continues after GDB disconnects."), _("\
16551Use this to let dprintf commands continue to hit and produce output\n\
16552even if GDB disconnects or detaches from the target."),
16553 NULL,
16554 NULL,
16555 &setlist, &showlist);
16556
16557 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16558agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16559(target agent only) This is useful for formatted output in user-defined commands."));
16560
765dc015 16561 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16562
16563 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16564 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16565}
This page took 3.436315 seconds and 4 git commands to generate.