Avoid MinGW compilation warning in readline/input.c
[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
e5dd4106 1179/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1180 breakpoint. This function will throw an exception if a problem is
1181 found. */
48cb2d85 1182
95a42b64
TT
1183static void
1184validate_commands_for_breakpoint (struct breakpoint *b,
1185 struct command_line *commands)
48cb2d85 1186{
d77f58be 1187 if (is_tracepoint (b))
a7bdde9e 1188 {
c9a6ce02 1189 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1190 struct command_line *c;
1191 struct command_line *while_stepping = 0;
c9a6ce02
PA
1192
1193 /* Reset the while-stepping step count. The previous commands
1194 might have included a while-stepping action, while the new
1195 ones might not. */
1196 t->step_count = 0;
1197
1198 /* We need to verify that each top-level element of commands is
1199 valid for tracepoints, that there's at most one
1200 while-stepping element, and that the while-stepping's body
1201 has valid tracing commands excluding nested while-stepping.
1202 We also need to validate the tracepoint action line in the
1203 context of the tracepoint --- validate_actionline actually
1204 has side effects, like setting the tracepoint's
1205 while-stepping STEP_COUNT, in addition to checking if the
1206 collect/teval actions parse and make sense in the
1207 tracepoint's context. */
a7bdde9e
VP
1208 for (c = commands; c; c = c->next)
1209 {
a7bdde9e
VP
1210 if (c->control_type == while_stepping_control)
1211 {
1212 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1213 error (_("The 'while-stepping' command "
1214 "cannot be used for fast tracepoint"));
0fb4aa4b 1215 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for static tracepoint"));
a7bdde9e
VP
1218
1219 if (while_stepping)
3e43a32a
MS
1220 error (_("The 'while-stepping' command "
1221 "can be used only once"));
a7bdde9e
VP
1222 else
1223 while_stepping = c;
1224 }
c9a6ce02
PA
1225
1226 validate_actionline (c->line, b);
a7bdde9e
VP
1227 }
1228 if (while_stepping)
1229 {
1230 struct command_line *c2;
1231
1232 gdb_assert (while_stepping->body_count == 1);
1233 c2 = while_stepping->body_list[0];
1234 for (; c2; c2 = c2->next)
1235 {
a7bdde9e
VP
1236 if (c2->control_type == while_stepping_control)
1237 error (_("The 'while-stepping' command cannot be nested"));
1238 }
1239 }
1240 }
1241 else
1242 {
1243 check_no_tracepoint_commands (commands);
1244 }
95a42b64
TT
1245}
1246
0fb4aa4b
PA
1247/* Return a vector of all the static tracepoints set at ADDR. The
1248 caller is responsible for releasing the vector. */
1249
1250VEC(breakpoint_p) *
1251static_tracepoints_here (CORE_ADDR addr)
1252{
1253 struct breakpoint *b;
1254 VEC(breakpoint_p) *found = 0;
1255 struct bp_location *loc;
1256
1257 ALL_BREAKPOINTS (b)
1258 if (b->type == bp_static_tracepoint)
1259 {
1260 for (loc = b->loc; loc; loc = loc->next)
1261 if (loc->address == addr)
1262 VEC_safe_push(breakpoint_p, found, b);
1263 }
1264
1265 return found;
1266}
1267
95a42b64 1268/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1269 validate that only allowed commands are included. */
95a42b64
TT
1270
1271void
4a64f543 1272breakpoint_set_commands (struct breakpoint *b,
93921405 1273 command_line_up &&commands)
95a42b64 1274{
93921405 1275 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1276
9add0f1b 1277 decref_counted_command_line (&b->commands);
93921405 1278 b->commands = alloc_counted_command_line (commands.release ());
8d3788bd 1279 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1280}
1281
45a43567
TT
1282/* Set the internal `silent' flag on the breakpoint. Note that this
1283 is not the same as the "silent" that may appear in the breakpoint's
1284 commands. */
1285
1286void
1287breakpoint_set_silent (struct breakpoint *b, int silent)
1288{
1289 int old_silent = b->silent;
1290
1291 b->silent = silent;
1292 if (old_silent != silent)
8d3788bd 1293 observer_notify_breakpoint_modified (b);
45a43567
TT
1294}
1295
1296/* Set the thread for this breakpoint. If THREAD is -1, make the
1297 breakpoint work for any thread. */
1298
1299void
1300breakpoint_set_thread (struct breakpoint *b, int thread)
1301{
1302 int old_thread = b->thread;
1303
1304 b->thread = thread;
1305 if (old_thread != thread)
8d3788bd 1306 observer_notify_breakpoint_modified (b);
45a43567
TT
1307}
1308
1309/* Set the task for this breakpoint. If TASK is 0, make the
1310 breakpoint work for any task. */
1311
1312void
1313breakpoint_set_task (struct breakpoint *b, int task)
1314{
1315 int old_task = b->task;
1316
1317 b->task = task;
1318 if (old_task != task)
8d3788bd 1319 observer_notify_breakpoint_modified (b);
45a43567
TT
1320}
1321
95a42b64
TT
1322void
1323check_tracepoint_command (char *line, void *closure)
a7bdde9e 1324{
9a3c8263 1325 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1326
6f937416 1327 validate_actionline (line, b);
a7bdde9e
VP
1328}
1329
95a42b64
TT
1330/* A structure used to pass information through
1331 map_breakpoint_numbers. */
1332
1333struct commands_info
1334{
1335 /* True if the command was typed at a tty. */
1336 int from_tty;
86b17b60
PA
1337
1338 /* The breakpoint range spec. */
896b6bda 1339 const char *arg;
86b17b60 1340
95a42b64
TT
1341 /* Non-NULL if the body of the commands are being read from this
1342 already-parsed command. */
1343 struct command_line *control;
86b17b60 1344
95a42b64
TT
1345 /* The command lines read from the user, or NULL if they have not
1346 yet been read. */
1347 struct counted_command_line *cmd;
1348};
1349
1350/* A callback for map_breakpoint_numbers that sets the commands for
1351 commands_command. */
1352
c906108c 1353static void
95a42b64 1354do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1355{
9a3c8263 1356 struct commands_info *info = (struct commands_info *) data;
c906108c 1357
95a42b64
TT
1358 if (info->cmd == NULL)
1359 {
93921405 1360 command_line_up l;
5c44784c 1361
95a42b64
TT
1362 if (info->control != NULL)
1363 l = copy_command_lines (info->control->body_list[0]);
1364 else
86b17b60
PA
1365 {
1366 struct cleanup *old_chain;
1367 char *str;
c5aa993b 1368
3e43a32a
MS
1369 str = xstrprintf (_("Type commands for breakpoint(s) "
1370 "%s, one per line."),
86b17b60
PA
1371 info->arg);
1372
1373 old_chain = make_cleanup (xfree, str);
1374
1375 l = read_command_lines (str,
1376 info->from_tty, 1,
d77f58be 1377 (is_tracepoint (b)
86b17b60
PA
1378 ? check_tracepoint_command : 0),
1379 b);
1380
1381 do_cleanups (old_chain);
1382 }
a7bdde9e 1383
93921405 1384 info->cmd = alloc_counted_command_line (l.release ());
95a42b64
TT
1385 }
1386
1387 /* If a breakpoint was on the list more than once, we don't need to
1388 do anything. */
1389 if (b->commands != info->cmd)
1390 {
1391 validate_commands_for_breakpoint (b, info->cmd->commands);
1392 incref_counted_command_line (info->cmd);
1393 decref_counted_command_line (&b->commands);
1394 b->commands = info->cmd;
8d3788bd 1395 observer_notify_breakpoint_modified (b);
c5aa993b 1396 }
95a42b64
TT
1397}
1398
1399static void
896b6bda 1400commands_command_1 (const char *arg, int from_tty,
4a64f543 1401 struct command_line *control)
95a42b64
TT
1402{
1403 struct cleanup *cleanups;
1404 struct commands_info info;
1405
1406 info.from_tty = from_tty;
1407 info.control = control;
1408 info.cmd = NULL;
1409 /* If we read command lines from the user, then `info' will hold an
1410 extra reference to the commands that we must clean up. */
1411 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1412
896b6bda
PA
1413 std::string new_arg;
1414
95a42b64
TT
1415 if (arg == NULL || !*arg)
1416 {
86b17b60 1417 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1418 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1419 breakpoint_count);
95a42b64 1420 else if (breakpoint_count > 0)
896b6bda 1421 new_arg = string_printf ("%d", breakpoint_count);
95a42b64 1422 }
9766ced4 1423 else
896b6bda 1424 new_arg = arg;
86b17b60 1425
896b6bda 1426 info.arg = new_arg.c_str ();
95a42b64 1427
896b6bda 1428 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
95a42b64
TT
1429
1430 if (info.cmd == NULL)
1431 error (_("No breakpoints specified."));
1432
1433 do_cleanups (cleanups);
1434}
1435
1436static void
1437commands_command (char *arg, int from_tty)
1438{
1439 commands_command_1 (arg, from_tty, NULL);
c906108c 1440}
40c03ae8
EZ
1441
1442/* Like commands_command, but instead of reading the commands from
1443 input stream, takes them from an already parsed command structure.
1444
1445 This is used by cli-script.c to DTRT with breakpoint commands
1446 that are part of if and while bodies. */
1447enum command_control_type
896b6bda 1448commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1449{
95a42b64
TT
1450 commands_command_1 (arg, 0, cmd);
1451 return simple_control;
40c03ae8 1452}
876fa593
JK
1453
1454/* Return non-zero if BL->TARGET_INFO contains valid information. */
1455
1456static int
1457bp_location_has_shadow (struct bp_location *bl)
1458{
1459 if (bl->loc_type != bp_loc_software_breakpoint)
1460 return 0;
1461 if (!bl->inserted)
1462 return 0;
1463 if (bl->target_info.shadow_len == 0)
e5dd4106 1464 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1465 return 0;
1466 return 1;
1467}
1468
9d497a19
PA
1469/* Update BUF, which is LEN bytes read from the target address
1470 MEMADDR, by replacing a memory breakpoint with its shadowed
1471 contents.
1472
1473 If READBUF is not NULL, this buffer must not overlap with the of
1474 the breakpoint location's shadow_contents buffer. Otherwise, a
1475 failed assertion internal error will be raised. */
1476
1477static void
1478one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1479 const gdb_byte *writebuf_org,
1480 ULONGEST memaddr, LONGEST len,
1481 struct bp_target_info *target_info,
1482 struct gdbarch *gdbarch)
1483{
1484 /* Now do full processing of the found relevant range of elements. */
1485 CORE_ADDR bp_addr = 0;
1486 int bp_size = 0;
1487 int bptoffset = 0;
1488
1489 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1490 current_program_space->aspace, 0))
1491 {
1492 /* The breakpoint is inserted in a different address space. */
1493 return;
1494 }
1495
1496 /* Addresses and length of the part of the breakpoint that
1497 we need to copy. */
1498 bp_addr = target_info->placed_address;
1499 bp_size = target_info->shadow_len;
1500
1501 if (bp_addr + bp_size <= memaddr)
1502 {
1503 /* The breakpoint is entirely before the chunk of memory we are
1504 reading. */
1505 return;
1506 }
1507
1508 if (bp_addr >= memaddr + len)
1509 {
1510 /* The breakpoint is entirely after the chunk of memory we are
1511 reading. */
1512 return;
1513 }
1514
1515 /* Offset within shadow_contents. */
1516 if (bp_addr < memaddr)
1517 {
1518 /* Only copy the second part of the breakpoint. */
1519 bp_size -= memaddr - bp_addr;
1520 bptoffset = memaddr - bp_addr;
1521 bp_addr = memaddr;
1522 }
1523
1524 if (bp_addr + bp_size > memaddr + len)
1525 {
1526 /* Only copy the first part of the breakpoint. */
1527 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1528 }
1529
1530 if (readbuf != NULL)
1531 {
1532 /* Verify that the readbuf buffer does not overlap with the
1533 shadow_contents buffer. */
1534 gdb_assert (target_info->shadow_contents >= readbuf + len
1535 || readbuf >= (target_info->shadow_contents
1536 + target_info->shadow_len));
1537
1538 /* Update the read buffer with this inserted breakpoint's
1539 shadow. */
1540 memcpy (readbuf + bp_addr - memaddr,
1541 target_info->shadow_contents + bptoffset, bp_size);
1542 }
1543 else
1544 {
1545 const unsigned char *bp;
0d5ed153
MR
1546 CORE_ADDR addr = target_info->reqstd_address;
1547 int placed_size;
9d497a19
PA
1548
1549 /* Update the shadow with what we want to write to memory. */
1550 memcpy (target_info->shadow_contents + bptoffset,
1551 writebuf_org + bp_addr - memaddr, bp_size);
1552
1553 /* Determine appropriate breakpoint contents and size for this
1554 address. */
0d5ed153 1555 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1556
1557 /* Update the final write buffer with this inserted
1558 breakpoint's INSN. */
1559 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1560 }
1561}
1562
8defab1a 1563/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1564 by replacing any memory breakpoints with their shadowed contents.
1565
35c63cd8
JB
1566 If READBUF is not NULL, this buffer must not overlap with any of
1567 the breakpoint location's shadow_contents buffers. Otherwise,
1568 a failed assertion internal error will be raised.
1569
876fa593 1570 The range of shadowed area by each bp_location is:
f5336ca5
PA
1571 bl->address - bp_locations_placed_address_before_address_max
1572 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1573 The range we were requested to resolve shadows for is:
1574 memaddr ... memaddr + len
1575 Thus the safe cutoff boundaries for performance optimization are
35df4500 1576 memaddr + len <= (bl->address
f5336ca5 1577 - bp_locations_placed_address_before_address_max)
876fa593 1578 and:
f5336ca5 1579 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1580
8defab1a 1581void
f0ba3972
PA
1582breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1583 const gdb_byte *writebuf_org,
1584 ULONGEST memaddr, LONGEST len)
c906108c 1585{
4a64f543
MS
1586 /* Left boundary, right boundary and median element of our binary
1587 search. */
876fa593
JK
1588 unsigned bc_l, bc_r, bc;
1589
4a64f543
MS
1590 /* Find BC_L which is a leftmost element which may affect BUF
1591 content. It is safe to report lower value but a failure to
1592 report higher one. */
876fa593
JK
1593
1594 bc_l = 0;
f5336ca5 1595 bc_r = bp_locations_count;
876fa593
JK
1596 while (bc_l + 1 < bc_r)
1597 {
35df4500 1598 struct bp_location *bl;
876fa593
JK
1599
1600 bc = (bc_l + bc_r) / 2;
f5336ca5 1601 bl = bp_locations[bc];
876fa593 1602
4a64f543
MS
1603 /* Check first BL->ADDRESS will not overflow due to the added
1604 constant. Then advance the left boundary only if we are sure
1605 the BC element can in no way affect the BUF content (MEMADDR
1606 to MEMADDR + LEN range).
876fa593 1607
f5336ca5 1608 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1609 offset so that we cannot miss a breakpoint with its shadow
1610 range tail still reaching MEMADDR. */
c5aa993b 1611
f5336ca5 1612 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1613 >= bl->address)
f5336ca5 1614 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1615 <= memaddr))
876fa593
JK
1616 bc_l = bc;
1617 else
1618 bc_r = bc;
1619 }
1620
128070bb
PA
1621 /* Due to the binary search above, we need to make sure we pick the
1622 first location that's at BC_L's address. E.g., if there are
1623 multiple locations at the same address, BC_L may end up pointing
1624 at a duplicate location, and miss the "master"/"inserted"
1625 location. Say, given locations L1, L2 and L3 at addresses A and
1626 B:
1627
1628 L1@A, L2@A, L3@B, ...
1629
1630 BC_L could end up pointing at location L2, while the "master"
1631 location could be L1. Since the `loc->inserted' flag is only set
1632 on "master" locations, we'd forget to restore the shadow of L1
1633 and L2. */
1634 while (bc_l > 0
f5336ca5 1635 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1636 bc_l--;
1637
876fa593
JK
1638 /* Now do full processing of the found relevant range of elements. */
1639
f5336ca5 1640 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1641 {
f5336ca5 1642 struct bp_location *bl = bp_locations[bc];
876fa593 1643
35df4500
TJB
1644 /* bp_location array has BL->OWNER always non-NULL. */
1645 if (bl->owner->type == bp_none)
8a3fe4f8 1646 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1647 bl->owner->number);
ffce0d52 1648
e5dd4106 1649 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1650 content. */
1651
f5336ca5
PA
1652 if (bl->address >= bp_locations_placed_address_before_address_max
1653 && memaddr + len <= (bl->address
1654 - bp_locations_placed_address_before_address_max))
876fa593
JK
1655 break;
1656
35df4500 1657 if (!bp_location_has_shadow (bl))
c5aa993b 1658 continue;
6c95b8df 1659
9d497a19
PA
1660 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1661 memaddr, len, &bl->target_info, bl->gdbarch);
1662 }
c906108c 1663}
9d497a19 1664
c906108c 1665\f
c5aa993b 1666
b775012e
LM
1667/* Return true if BPT is either a software breakpoint or a hardware
1668 breakpoint. */
1669
1670int
1671is_breakpoint (const struct breakpoint *bpt)
1672{
1673 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1674 || bpt->type == bp_hardware_breakpoint
1675 || bpt->type == bp_dprintf);
b775012e
LM
1676}
1677
60e1c644
PA
1678/* Return true if BPT is of any hardware watchpoint kind. */
1679
a5606eee 1680static int
d77f58be 1681is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1682{
1683 return (bpt->type == bp_hardware_watchpoint
1684 || bpt->type == bp_read_watchpoint
1685 || bpt->type == bp_access_watchpoint);
1686}
7270d8f2 1687
60e1c644
PA
1688/* Return true if BPT is of any watchpoint kind, hardware or
1689 software. */
1690
3a5c3e22 1691int
d77f58be 1692is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1693{
1694 return (is_hardware_watchpoint (bpt)
1695 || bpt->type == bp_watchpoint);
1696}
1697
3a5c3e22
PA
1698/* Returns true if the current thread and its running state are safe
1699 to evaluate or update watchpoint B. Watchpoints on local
1700 expressions need to be evaluated in the context of the thread that
1701 was current when the watchpoint was created, and, that thread needs
1702 to be stopped to be able to select the correct frame context.
1703 Watchpoints on global expressions can be evaluated on any thread,
1704 and in any state. It is presently left to the target allowing
1705 memory accesses when threads are running. */
f6bc2008
PA
1706
1707static int
3a5c3e22 1708watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1709{
d0d8b0c6
JK
1710 return (b->base.pspace == current_program_space
1711 && (ptid_equal (b->watchpoint_thread, null_ptid)
1712 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1713 && !is_executing (inferior_ptid))));
f6bc2008
PA
1714}
1715
d0fb5eae
JK
1716/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1717 associated bp_watchpoint_scope breakpoint. */
1718
1719static void
3a5c3e22 1720watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1721{
3a5c3e22 1722 struct breakpoint *b = &w->base;
d0fb5eae
JK
1723
1724 if (b->related_breakpoint != b)
1725 {
1726 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1727 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1728 b->related_breakpoint->disposition = disp_del_at_next_stop;
1729 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1730 b->related_breakpoint = b;
1731 }
1732 b->disposition = disp_del_at_next_stop;
1733}
1734
bb9d5f81
PP
1735/* Extract a bitfield value from value VAL using the bit parameters contained in
1736 watchpoint W. */
1737
1738static struct value *
1739extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1740{
1741 struct value *bit_val;
1742
1743 if (val == NULL)
1744 return NULL;
1745
1746 bit_val = allocate_value (value_type (val));
1747
1748 unpack_value_bitfield (bit_val,
1749 w->val_bitpos,
1750 w->val_bitsize,
1751 value_contents_for_printing (val),
1752 value_offset (val),
1753 val);
1754
1755 return bit_val;
1756}
1757
c6d81124
PA
1758/* Allocate a dummy location and add it to B, which must be a software
1759 watchpoint. This is required because even if a software watchpoint
1760 is not watching any memory, bpstat_stop_status requires a location
1761 to be able to report stops. */
1762
1763static void
1764software_watchpoint_add_no_memory_location (struct breakpoint *b,
1765 struct program_space *pspace)
1766{
1767 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1768
1769 b->loc = allocate_bp_location (b);
1770 b->loc->pspace = pspace;
1771 b->loc->address = -1;
1772 b->loc->length = -1;
1773}
1774
1775/* Returns true if B is a software watchpoint that is not watching any
1776 memory (e.g., "watch $pc"). */
1777
1778static int
1779is_no_memory_software_watchpoint (struct breakpoint *b)
1780{
1781 return (b->type == bp_watchpoint
1782 && b->loc != NULL
1783 && b->loc->next == NULL
1784 && b->loc->address == -1
1785 && b->loc->length == -1);
1786}
1787
567e1b4e
JB
1788/* Assuming that B is a watchpoint:
1789 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1790 - Evaluate expression and store the result in B->val
567e1b4e
JB
1791 - Evaluate the condition if there is one, and store the result
1792 in b->loc->cond.
a5606eee
VP
1793 - Update the list of values that must be watched in B->loc.
1794
4a64f543
MS
1795 If the watchpoint disposition is disp_del_at_next_stop, then do
1796 nothing. If this is local watchpoint that is out of scope, delete
1797 it.
1798
1799 Even with `set breakpoint always-inserted on' the watchpoints are
1800 removed + inserted on each stop here. Normal breakpoints must
1801 never be removed because they might be missed by a running thread
1802 when debugging in non-stop mode. On the other hand, hardware
1803 watchpoints (is_hardware_watchpoint; processed here) are specific
1804 to each LWP since they are stored in each LWP's hardware debug
1805 registers. Therefore, such LWP must be stopped first in order to
1806 be able to modify its hardware watchpoints.
1807
1808 Hardware watchpoints must be reset exactly once after being
1809 presented to the user. It cannot be done sooner, because it would
1810 reset the data used to present the watchpoint hit to the user. And
1811 it must not be done later because it could display the same single
1812 watchpoint hit during multiple GDB stops. Note that the latter is
1813 relevant only to the hardware watchpoint types bp_read_watchpoint
1814 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1815 not user-visible - its hit is suppressed if the memory content has
1816 not changed.
1817
1818 The following constraints influence the location where we can reset
1819 hardware watchpoints:
1820
1821 * target_stopped_by_watchpoint and target_stopped_data_address are
1822 called several times when GDB stops.
1823
1824 [linux]
1825 * Multiple hardware watchpoints can be hit at the same time,
1826 causing GDB to stop. GDB only presents one hardware watchpoint
1827 hit at a time as the reason for stopping, and all the other hits
1828 are presented later, one after the other, each time the user
1829 requests the execution to be resumed. Execution is not resumed
1830 for the threads still having pending hit event stored in
1831 LWP_INFO->STATUS. While the watchpoint is already removed from
1832 the inferior on the first stop the thread hit event is kept being
1833 reported from its cached value by linux_nat_stopped_data_address
1834 until the real thread resume happens after the watchpoint gets
1835 presented and thus its LWP_INFO->STATUS gets reset.
1836
1837 Therefore the hardware watchpoint hit can get safely reset on the
1838 watchpoint removal from inferior. */
a79d3c27 1839
b40ce68a 1840static void
3a5c3e22 1841update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1842{
a5606eee 1843 int within_current_scope;
a5606eee 1844 struct frame_id saved_frame_id;
66076460 1845 int frame_saved;
a5606eee 1846
f6bc2008
PA
1847 /* If this is a local watchpoint, we only want to check if the
1848 watchpoint frame is in scope if the current thread is the thread
1849 that was used to create the watchpoint. */
1850 if (!watchpoint_in_thread_scope (b))
1851 return;
1852
3a5c3e22 1853 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1854 return;
1855
66076460 1856 frame_saved = 0;
a5606eee
VP
1857
1858 /* Determine if the watchpoint is within scope. */
1859 if (b->exp_valid_block == NULL)
1860 within_current_scope = 1;
1861 else
1862 {
b5db5dfc
UW
1863 struct frame_info *fi = get_current_frame ();
1864 struct gdbarch *frame_arch = get_frame_arch (fi);
1865 CORE_ADDR frame_pc = get_frame_pc (fi);
1866
c9cf6e20
MG
1867 /* If we're at a point where the stack has been destroyed
1868 (e.g. in a function epilogue), unwinding may not work
1869 properly. Do not attempt to recreate locations at this
b5db5dfc 1870 point. See similar comments in watchpoint_check. */
c9cf6e20 1871 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1872 return;
66076460
DJ
1873
1874 /* Save the current frame's ID so we can restore it after
1875 evaluating the watchpoint expression on its own frame. */
1876 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1877 took a frame parameter, so that we didn't have to change the
1878 selected frame. */
1879 frame_saved = 1;
1880 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1881
a5606eee
VP
1882 fi = frame_find_by_id (b->watchpoint_frame);
1883 within_current_scope = (fi != NULL);
1884 if (within_current_scope)
1885 select_frame (fi);
1886 }
1887
b5db5dfc
UW
1888 /* We don't free locations. They are stored in the bp_location array
1889 and update_global_location_list will eventually delete them and
1890 remove breakpoints if needed. */
3a5c3e22 1891 b->base.loc = NULL;
b5db5dfc 1892
a5606eee
VP
1893 if (within_current_scope && reparse)
1894 {
bbc13ae3 1895 const char *s;
d63d0675 1896
4d01a485 1897 b->exp.reset ();
d63d0675 1898 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1899 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1900 /* If the meaning of expression itself changed, the old value is
1901 no longer relevant. We don't want to report a watchpoint hit
1902 to the user when the old value and the new value may actually
1903 be completely different objects. */
1904 value_free (b->val);
fa4727a6
DJ
1905 b->val = NULL;
1906 b->val_valid = 0;
60e1c644
PA
1907
1908 /* Note that unlike with breakpoints, the watchpoint's condition
1909 expression is stored in the breakpoint object, not in the
1910 locations (re)created below. */
3a5c3e22 1911 if (b->base.cond_string != NULL)
60e1c644 1912 {
4d01a485 1913 b->cond_exp.reset ();
60e1c644 1914
3a5c3e22 1915 s = b->base.cond_string;
1bb9788d 1916 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1917 }
a5606eee 1918 }
a5606eee
VP
1919
1920 /* If we failed to parse the expression, for example because
1921 it refers to a global variable in a not-yet-loaded shared library,
1922 don't try to insert watchpoint. We don't automatically delete
1923 such watchpoint, though, since failure to parse expression
1924 is different from out-of-scope watchpoint. */
e8369a73 1925 if (!target_has_execution)
2d134ed3
PA
1926 {
1927 /* Without execution, memory can't change. No use to try and
1928 set watchpoint locations. The watchpoint will be reset when
1929 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1930 if (!can_use_hw_watchpoints)
1931 {
1932 if (b->base.ops->works_in_software_mode (&b->base))
1933 b->base.type = bp_watchpoint;
1934 else
638aa5a1
AB
1935 error (_("Can't set read/access watchpoint when "
1936 "hardware watchpoints are disabled."));
e8369a73 1937 }
2d134ed3
PA
1938 }
1939 else if (within_current_scope && b->exp)
a5606eee 1940 {
0cf6dd15 1941 int pc = 0;
fa4727a6 1942 struct value *val_chain, *v, *result, *next;
2d134ed3 1943 struct program_space *frame_pspace;
a5606eee 1944
4d01a485 1945 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1946
a5606eee
VP
1947 /* Avoid setting b->val if it's already set. The meaning of
1948 b->val is 'the last value' user saw, and we should update
1949 it only if we reported that last value to user. As it
9c06b0b4
TJB
1950 happens, the code that reports it updates b->val directly.
1951 We don't keep track of the memory value for masked
1952 watchpoints. */
3a5c3e22 1953 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1954 {
bb9d5f81
PP
1955 if (b->val_bitsize != 0)
1956 {
1957 v = extract_bitfield_from_watchpoint_value (b, v);
1958 if (v != NULL)
1959 release_value (v);
1960 }
fa4727a6
DJ
1961 b->val = v;
1962 b->val_valid = 1;
1963 }
a5606eee 1964
2d134ed3
PA
1965 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1966
a5606eee 1967 /* Look at each value on the value chain. */
9fa40276 1968 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1969 {
1970 /* If it's a memory location, and GDB actually needed
1971 its contents to evaluate the expression, then we
fa4727a6
DJ
1972 must watch it. If the first value returned is
1973 still lazy, that means an error occurred reading it;
1974 watch it anyway in case it becomes readable. */
a5606eee 1975 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1976 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1977 {
1978 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1979
a5606eee
VP
1980 /* We only watch structs and arrays if user asked
1981 for it explicitly, never if they just happen to
1982 appear in the middle of some value chain. */
fa4727a6 1983 if (v == result
a5606eee
VP
1984 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1985 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1986 {
1987 CORE_ADDR addr;
f486487f 1988 enum target_hw_bp_type type;
a5606eee 1989 struct bp_location *loc, **tmp;
bb9d5f81
PP
1990 int bitpos = 0, bitsize = 0;
1991
1992 if (value_bitsize (v) != 0)
1993 {
1994 /* Extract the bit parameters out from the bitfield
1995 sub-expression. */
1996 bitpos = value_bitpos (v);
1997 bitsize = value_bitsize (v);
1998 }
1999 else if (v == result && b->val_bitsize != 0)
2000 {
2001 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2002 lvalue whose bit parameters are saved in the fields
2003 VAL_BITPOS and VAL_BITSIZE. */
2004 bitpos = b->val_bitpos;
2005 bitsize = b->val_bitsize;
2006 }
a5606eee 2007
42ae5230 2008 addr = value_address (v);
bb9d5f81
PP
2009 if (bitsize != 0)
2010 {
2011 /* Skip the bytes that don't contain the bitfield. */
2012 addr += bitpos / 8;
2013 }
2014
a5606eee 2015 type = hw_write;
3a5c3e22 2016 if (b->base.type == bp_read_watchpoint)
a5606eee 2017 type = hw_read;
3a5c3e22 2018 else if (b->base.type == bp_access_watchpoint)
a5606eee 2019 type = hw_access;
3a5c3e22
PA
2020
2021 loc = allocate_bp_location (&b->base);
2022 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2023 ;
2024 *tmp = loc;
a6d9a66e 2025 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2026
2027 loc->pspace = frame_pspace;
a5606eee 2028 loc->address = addr;
bb9d5f81
PP
2029
2030 if (bitsize != 0)
2031 {
2032 /* Just cover the bytes that make up the bitfield. */
2033 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2034 }
2035 else
2036 loc->length = TYPE_LENGTH (value_type (v));
2037
a5606eee
VP
2038 loc->watchpoint_type = type;
2039 }
2040 }
9fa40276
TJB
2041 }
2042
2043 /* Change the type of breakpoint between hardware assisted or
2044 an ordinary watchpoint depending on the hardware support
2045 and free hardware slots. REPARSE is set when the inferior
2046 is started. */
a9634178 2047 if (reparse)
9fa40276 2048 {
e09342b5 2049 int reg_cnt;
9fa40276
TJB
2050 enum bp_loc_type loc_type;
2051 struct bp_location *bl;
a5606eee 2052
a9634178 2053 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2054
2055 if (reg_cnt)
9fa40276
TJB
2056 {
2057 int i, target_resources_ok, other_type_used;
a1398e0c 2058 enum bptype type;
9fa40276 2059
a9634178
TJB
2060 /* Use an exact watchpoint when there's only one memory region to be
2061 watched, and only one debug register is needed to watch it. */
2062 b->exact = target_exact_watchpoints && reg_cnt == 1;
2063
9fa40276 2064 /* We need to determine how many resources are already
e09342b5
TJB
2065 used for all other hardware watchpoints plus this one
2066 to see if we still have enough resources to also fit
a1398e0c
PA
2067 this watchpoint in as well. */
2068
2069 /* If this is a software watchpoint, we try to turn it
2070 to a hardware one -- count resources as if B was of
2071 hardware watchpoint type. */
2072 type = b->base.type;
2073 if (type == bp_watchpoint)
2074 type = bp_hardware_watchpoint;
2075
2076 /* This watchpoint may or may not have been placed on
2077 the list yet at this point (it won't be in the list
2078 if we're trying to create it for the first time,
2079 through watch_command), so always account for it
2080 manually. */
2081
2082 /* Count resources used by all watchpoints except B. */
2083 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2084
2085 /* Add in the resources needed for B. */
2086 i += hw_watchpoint_use_count (&b->base);
2087
2088 target_resources_ok
2089 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2090 if (target_resources_ok <= 0)
a9634178 2091 {
3a5c3e22 2092 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2093
2094 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2095 error (_("Target does not support this type of "
2096 "hardware watchpoint."));
9c06b0b4
TJB
2097 else if (target_resources_ok < 0 && !sw_mode)
2098 error (_("There are not enough available hardware "
2099 "resources for this watchpoint."));
a1398e0c
PA
2100
2101 /* Downgrade to software watchpoint. */
2102 b->base.type = bp_watchpoint;
2103 }
2104 else
2105 {
2106 /* If this was a software watchpoint, we've just
2107 found we have enough resources to turn it to a
2108 hardware watchpoint. Otherwise, this is a
2109 nop. */
2110 b->base.type = type;
a9634178 2111 }
9fa40276 2112 }
3a5c3e22 2113 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2114 {
2115 if (!can_use_hw_watchpoints)
2116 error (_("Can't set read/access watchpoint when "
2117 "hardware watchpoints are disabled."));
2118 else
2119 error (_("Expression cannot be implemented with "
2120 "read/access watchpoint."));
2121 }
9fa40276 2122 else
3a5c3e22 2123 b->base.type = bp_watchpoint;
9fa40276 2124
3a5c3e22 2125 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2126 : bp_loc_hardware_watchpoint);
3a5c3e22 2127 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2128 bl->loc_type = loc_type;
2129 }
2130
2131 for (v = val_chain; v; v = next)
2132 {
a5606eee
VP
2133 next = value_next (v);
2134 if (v != b->val)
2135 value_free (v);
2136 }
2137
c7437ca6
PA
2138 /* If a software watchpoint is not watching any memory, then the
2139 above left it without any location set up. But,
2140 bpstat_stop_status requires a location to be able to report
2141 stops, so make sure there's at least a dummy one. */
3a5c3e22 2142 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2143 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2144 }
2145 else if (!within_current_scope)
7270d8f2 2146 {
ac74f770
MS
2147 printf_filtered (_("\
2148Watchpoint %d deleted because the program has left the block\n\
2149in which its expression is valid.\n"),
3a5c3e22 2150 b->base.number);
d0fb5eae 2151 watchpoint_del_at_next_stop (b);
7270d8f2 2152 }
a5606eee
VP
2153
2154 /* Restore the selected frame. */
66076460
DJ
2155 if (frame_saved)
2156 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2157}
2158
a5606eee 2159
74960c60 2160/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2161 inserted in the inferior. We don't differentiate the type of BL's owner
2162 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2163 breakpoint_ops is not defined, because in insert_bp_location,
2164 tracepoint's insert_location will not be called. */
74960c60 2165static int
35df4500 2166should_be_inserted (struct bp_location *bl)
74960c60 2167{
35df4500 2168 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2169 return 0;
2170
35df4500 2171 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2172 return 0;
2173
35df4500 2174 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2175 return 0;
2176
f8eba3c6
TT
2177 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2178 return 0;
2179
56710373
PA
2180 /* This is set for example, when we're attached to the parent of a
2181 vfork, and have detached from the child. The child is running
2182 free, and we expect it to do an exec or exit, at which point the
2183 OS makes the parent schedulable again (and the target reports
2184 that the vfork is done). Until the child is done with the shared
2185 memory region, do not insert breakpoints in the parent, otherwise
2186 the child could still trip on the parent's breakpoints. Since
2187 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2188 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2189 return 0;
2190
31e77af2 2191 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2192 location, except if the breakpoint is a single-step breakpoint,
2193 and the breakpoint's thread is the thread which is stepping past
2194 a breakpoint. */
31e77af2
PA
2195 if ((bl->loc_type == bp_loc_software_breakpoint
2196 || bl->loc_type == bp_loc_hardware_breakpoint)
2197 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2198 bl->address)
2199 /* The single-step breakpoint may be inserted at the location
2200 we're trying to step if the instruction branches to itself.
2201 However, the instruction won't be executed at all and it may
2202 break the semantics of the instruction, for example, the
2203 instruction is a conditional branch or updates some flags.
2204 We can't fix it unless GDB is able to emulate the instruction
2205 or switch to displaced stepping. */
2206 && !(bl->owner->type == bp_single_step
2207 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2208 {
2209 if (debug_infrun)
2210 {
2211 fprintf_unfiltered (gdb_stdlog,
2212 "infrun: skipping breakpoint: "
2213 "stepping past insn at: %s\n",
2214 paddress (bl->gdbarch, bl->address));
2215 }
2216 return 0;
2217 }
31e77af2 2218
963f9c80
PA
2219 /* Don't insert watchpoints if we're trying to step past the
2220 instruction that triggered one. */
2221 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2222 && stepping_past_nonsteppable_watchpoint ())
2223 {
2224 if (debug_infrun)
2225 {
2226 fprintf_unfiltered (gdb_stdlog,
2227 "infrun: stepping past non-steppable watchpoint. "
2228 "skipping watchpoint at %s:%d\n",
2229 paddress (bl->gdbarch, bl->address),
2230 bl->length);
2231 }
2232 return 0;
2233 }
2234
74960c60
VP
2235 return 1;
2236}
2237
934709f0
PW
2238/* Same as should_be_inserted but does the check assuming
2239 that the location is not duplicated. */
2240
2241static int
2242unduplicated_should_be_inserted (struct bp_location *bl)
2243{
2244 int result;
2245 const int save_duplicate = bl->duplicate;
2246
2247 bl->duplicate = 0;
2248 result = should_be_inserted (bl);
2249 bl->duplicate = save_duplicate;
2250 return result;
2251}
2252
b775012e
LM
2253/* Parses a conditional described by an expression COND into an
2254 agent expression bytecode suitable for evaluation
2255 by the bytecode interpreter. Return NULL if there was
2256 any error during parsing. */
2257
833177a4 2258static agent_expr_up
b775012e
LM
2259parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2260{
833177a4 2261 if (cond == NULL)
b775012e
LM
2262 return NULL;
2263
833177a4
PA
2264 agent_expr_up aexpr;
2265
b775012e
LM
2266 /* We don't want to stop processing, so catch any errors
2267 that may show up. */
492d29ea 2268 TRY
b775012e 2269 {
036e657b 2270 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2271 }
2272
492d29ea 2273 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2274 {
2275 /* If we got here, it means the condition could not be parsed to a valid
2276 bytecode expression and thus can't be evaluated on the target's side.
2277 It's no use iterating through the conditions. */
b775012e 2278 }
492d29ea 2279 END_CATCH
b775012e
LM
2280
2281 /* We have a valid agent expression. */
2282 return aexpr;
2283}
2284
2285/* Based on location BL, create a list of breakpoint conditions to be
2286 passed on to the target. If we have duplicated locations with different
2287 conditions, we will add such conditions to the list. The idea is that the
2288 target will evaluate the list of conditions and will only notify GDB when
2289 one of them is true. */
2290
2291static void
2292build_target_condition_list (struct bp_location *bl)
2293{
2294 struct bp_location **locp = NULL, **loc2p;
2295 int null_condition_or_parse_error = 0;
2296 int modified = bl->needs_update;
2297 struct bp_location *loc;
2298
8b4f3082 2299 /* Release conditions left over from a previous insert. */
3cde5c42 2300 bl->target_info.conditions.clear ();
8b4f3082 2301
b775012e
LM
2302 /* This is only meaningful if the target is
2303 evaluating conditions and if the user has
2304 opted for condition evaluation on the target's
2305 side. */
2306 if (gdb_evaluates_breakpoint_condition_p ()
2307 || !target_supports_evaluation_of_breakpoint_conditions ())
2308 return;
2309
2310 /* Do a first pass to check for locations with no assigned
2311 conditions or conditions that fail to parse to a valid agent expression
2312 bytecode. If any of these happen, then it's no use to send conditions
2313 to the target since this location will always trigger and generate a
2314 response back to GDB. */
2315 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2316 {
2317 loc = (*loc2p);
2318 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2319 {
2320 if (modified)
2321 {
b775012e
LM
2322 /* Re-parse the conditions since something changed. In that
2323 case we already freed the condition bytecodes (see
2324 force_breakpoint_reinsertion). We just
2325 need to parse the condition to bytecodes again. */
833177a4
PA
2326 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2327 loc->cond.get ());
b775012e
LM
2328 }
2329
2330 /* If we have a NULL bytecode expression, it means something
2331 went wrong or we have a null condition expression. */
2332 if (!loc->cond_bytecode)
2333 {
2334 null_condition_or_parse_error = 1;
2335 break;
2336 }
2337 }
2338 }
2339
2340 /* If any of these happened, it means we will have to evaluate the conditions
2341 for the location's address on gdb's side. It is no use keeping bytecodes
2342 for all the other duplicate locations, thus we free all of them here.
2343
2344 This is so we have a finer control over which locations' conditions are
2345 being evaluated by GDB or the remote stub. */
2346 if (null_condition_or_parse_error)
2347 {
2348 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2349 {
2350 loc = (*loc2p);
2351 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2352 {
2353 /* Only go as far as the first NULL bytecode is
2354 located. */
2355 if (!loc->cond_bytecode)
2356 return;
2357
833177a4 2358 loc->cond_bytecode.reset ();
b775012e
LM
2359 }
2360 }
2361 }
2362
2363 /* No NULL conditions or failed bytecode generation. Build a condition list
2364 for this location's address. */
2365 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2366 {
2367 loc = (*loc2p);
2368 if (loc->cond
2369 && is_breakpoint (loc->owner)
2370 && loc->pspace->num == bl->pspace->num
2371 && loc->owner->enable_state == bp_enabled
2372 && loc->enabled)
3cde5c42
PA
2373 {
2374 /* Add the condition to the vector. This will be used later
2375 to send the conditions to the target. */
2376 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2377 }
b775012e
LM
2378 }
2379
2380 return;
2381}
2382
d3ce09f5
SS
2383/* Parses a command described by string CMD into an agent expression
2384 bytecode suitable for evaluation by the bytecode interpreter.
2385 Return NULL if there was any error during parsing. */
2386
833177a4 2387static agent_expr_up
d3ce09f5
SS
2388parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2389{
2390 struct cleanup *old_cleanups = 0;
4d01a485 2391 struct expression **argvec;
bbc13ae3
KS
2392 const char *cmdrest;
2393 const char *format_start, *format_end;
d3ce09f5
SS
2394 struct format_piece *fpieces;
2395 int nargs;
2396 struct gdbarch *gdbarch = get_current_arch ();
2397
833177a4 2398 if (cmd == NULL)
d3ce09f5
SS
2399 return NULL;
2400
2401 cmdrest = cmd;
2402
2403 if (*cmdrest == ',')
2404 ++cmdrest;
bbc13ae3 2405 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2406
2407 if (*cmdrest++ != '"')
2408 error (_("No format string following the location"));
2409
2410 format_start = cmdrest;
2411
2412 fpieces = parse_format_string (&cmdrest);
2413
2414 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2415
2416 format_end = cmdrest;
2417
2418 if (*cmdrest++ != '"')
2419 error (_("Bad format string, non-terminated '\"'."));
2420
bbc13ae3 2421 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2422
2423 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2424 error (_("Invalid argument syntax"));
2425
2426 if (*cmdrest == ',')
2427 cmdrest++;
bbc13ae3 2428 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2429
2430 /* For each argument, make an expression. */
2431
2432 argvec = (struct expression **) alloca (strlen (cmd)
2433 * sizeof (struct expression *));
2434
2435 nargs = 0;
2436 while (*cmdrest != '\0')
2437 {
bbc13ae3 2438 const char *cmd1;
d3ce09f5
SS
2439
2440 cmd1 = cmdrest;
4d01a485
PA
2441 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2442 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2443 cmdrest = cmd1;
2444 if (*cmdrest == ',')
2445 ++cmdrest;
2446 }
2447
833177a4
PA
2448 agent_expr_up aexpr;
2449
d3ce09f5
SS
2450 /* We don't want to stop processing, so catch any errors
2451 that may show up. */
492d29ea 2452 TRY
d3ce09f5 2453 {
036e657b
JB
2454 aexpr = gen_printf (scope, gdbarch, 0, 0,
2455 format_start, format_end - format_start,
2456 fpieces, nargs, argvec);
d3ce09f5 2457 }
492d29ea 2458 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2459 {
2460 /* If we got here, it means the command could not be parsed to a valid
2461 bytecode expression and thus can't be evaluated on the target's side.
2462 It's no use iterating through the other commands. */
d3ce09f5 2463 }
492d29ea
PA
2464 END_CATCH
2465
2466 do_cleanups (old_cleanups);
d3ce09f5 2467
d3ce09f5
SS
2468 /* We have a valid agent expression, return it. */
2469 return aexpr;
2470}
2471
2472/* Based on location BL, create a list of breakpoint commands to be
2473 passed on to the target. If we have duplicated locations with
2474 different commands, we will add any such to the list. */
2475
2476static void
2477build_target_command_list (struct bp_location *bl)
2478{
2479 struct bp_location **locp = NULL, **loc2p;
2480 int null_command_or_parse_error = 0;
2481 int modified = bl->needs_update;
2482 struct bp_location *loc;
2483
3cde5c42
PA
2484 /* Clear commands left over from a previous insert. */
2485 bl->target_info.tcommands.clear ();
8b4f3082 2486
41fac0cf 2487 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2488 return;
2489
41fac0cf
PA
2490 /* For now, limit to agent-style dprintf breakpoints. */
2491 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2492 return;
2493
41fac0cf
PA
2494 /* For now, if we have any duplicate location that isn't a dprintf,
2495 don't install the target-side commands, as that would make the
2496 breakpoint not be reported to the core, and we'd lose
2497 control. */
2498 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2499 {
2500 loc = (*loc2p);
2501 if (is_breakpoint (loc->owner)
2502 && loc->pspace->num == bl->pspace->num
2503 && loc->owner->type != bp_dprintf)
2504 return;
2505 }
2506
d3ce09f5
SS
2507 /* Do a first pass to check for locations with no assigned
2508 conditions or conditions that fail to parse to a valid agent expression
2509 bytecode. If any of these happen, then it's no use to send conditions
2510 to the target since this location will always trigger and generate a
2511 response back to GDB. */
2512 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2513 {
2514 loc = (*loc2p);
2515 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2516 {
2517 if (modified)
2518 {
d3ce09f5
SS
2519 /* Re-parse the commands since something changed. In that
2520 case we already freed the command bytecodes (see
2521 force_breakpoint_reinsertion). We just
2522 need to parse the command to bytecodes again. */
833177a4
PA
2523 loc->cmd_bytecode
2524 = parse_cmd_to_aexpr (bl->address,
2525 loc->owner->extra_string);
d3ce09f5
SS
2526 }
2527
2528 /* If we have a NULL bytecode expression, it means something
2529 went wrong or we have a null command expression. */
2530 if (!loc->cmd_bytecode)
2531 {
2532 null_command_or_parse_error = 1;
2533 break;
2534 }
2535 }
2536 }
2537
2538 /* If anything failed, then we're not doing target-side commands,
2539 and so clean up. */
2540 if (null_command_or_parse_error)
2541 {
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner)
2546 && loc->pspace->num == bl->pspace->num)
2547 {
2548 /* Only go as far as the first NULL bytecode is
2549 located. */
40fb6c5e 2550 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2551 return;
2552
833177a4 2553 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2554 }
2555 }
2556 }
2557
2558 /* No NULL commands or failed bytecode generation. Build a command list
2559 for this location's address. */
2560 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2561 {
2562 loc = (*loc2p);
2563 if (loc->owner->extra_string
2564 && is_breakpoint (loc->owner)
2565 && loc->pspace->num == bl->pspace->num
2566 && loc->owner->enable_state == bp_enabled
2567 && loc->enabled)
3cde5c42
PA
2568 {
2569 /* Add the command to the vector. This will be used later
2570 to send the commands to the target. */
2571 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2572 }
d3ce09f5
SS
2573 }
2574
2575 bl->target_info.persist = 0;
2576 /* Maybe flag this location as persistent. */
2577 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2578 bl->target_info.persist = 1;
2579}
2580
833b7ab5
YQ
2581/* Return the kind of breakpoint on address *ADDR. Get the kind
2582 of breakpoint according to ADDR except single-step breakpoint.
2583 Get the kind of single-step breakpoint according to the current
2584 registers state. */
cd6c3b4f
YQ
2585
2586static int
2587breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2588{
833b7ab5
YQ
2589 if (bl->owner->type == bp_single_step)
2590 {
2591 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2592 struct regcache *regcache;
2593
2594 regcache = get_thread_regcache (thr->ptid);
2595
2596 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2597 regcache, addr);
2598 }
2599 else
2600 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2601}
2602
35df4500
TJB
2603/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2604 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2605 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2606 Returns 0 for success, 1 if the bp_location type is not supported or
2607 -1 for failure.
879bfdc2 2608
4a64f543
MS
2609 NOTE drow/2003-09-09: This routine could be broken down to an
2610 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2611static int
35df4500 2612insert_bp_location (struct bp_location *bl,
26bb91f3 2613 struct ui_file *tmp_error_stream,
3fbb6ffa 2614 int *disabled_breaks,
dd61ec5c
MW
2615 int *hw_breakpoint_error,
2616 int *hw_bp_error_explained_already)
879bfdc2 2617{
0000e5cc
PA
2618 enum errors bp_err = GDB_NO_ERROR;
2619 const char *bp_err_message = NULL;
879bfdc2 2620
b775012e 2621 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2622 return 0;
2623
35c63cd8
JB
2624 /* Note we don't initialize bl->target_info, as that wipes out
2625 the breakpoint location's shadow_contents if the breakpoint
2626 is still inserted at that location. This in turn breaks
2627 target_read_memory which depends on these buffers when
2628 a memory read is requested at the breakpoint location:
2629 Once the target_info has been wiped, we fail to see that
2630 we have a breakpoint inserted at that address and thus
2631 read the breakpoint instead of returning the data saved in
2632 the breakpoint location's shadow contents. */
0d5ed153 2633 bl->target_info.reqstd_address = bl->address;
35df4500 2634 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2635 bl->target_info.length = bl->length;
8181d85f 2636
b775012e
LM
2637 /* When working with target-side conditions, we must pass all the conditions
2638 for the same breakpoint address down to the target since GDB will not
2639 insert those locations. With a list of breakpoint conditions, the target
2640 can decide when to stop and notify GDB. */
2641
2642 if (is_breakpoint (bl->owner))
2643 {
2644 build_target_condition_list (bl);
d3ce09f5
SS
2645 build_target_command_list (bl);
2646 /* Reset the modification marker. */
b775012e
LM
2647 bl->needs_update = 0;
2648 }
2649
35df4500
TJB
2650 if (bl->loc_type == bp_loc_software_breakpoint
2651 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2652 {
35df4500 2653 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2654 {
2655 /* If the explicitly specified breakpoint type
2656 is not hardware breakpoint, check the memory map to see
2657 if the breakpoint address is in read only memory or not.
4a64f543 2658
765dc015
VP
2659 Two important cases are:
2660 - location type is not hardware breakpoint, memory
2661 is readonly. We change the type of the location to
2662 hardware breakpoint.
4a64f543
MS
2663 - location type is hardware breakpoint, memory is
2664 read-write. This means we've previously made the
2665 location hardware one, but then the memory map changed,
2666 so we undo.
765dc015 2667
4a64f543
MS
2668 When breakpoints are removed, remove_breakpoints will use
2669 location types we've just set here, the only possible
2670 problem is that memory map has changed during running
2671 program, but it's not going to work anyway with current
2672 gdb. */
765dc015 2673 struct mem_region *mr
0d5ed153 2674 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2675
2676 if (mr)
2677 {
2678 if (automatic_hardware_breakpoints)
2679 {
765dc015
VP
2680 enum bp_loc_type new_type;
2681
2682 if (mr->attrib.mode != MEM_RW)
2683 new_type = bp_loc_hardware_breakpoint;
2684 else
2685 new_type = bp_loc_software_breakpoint;
2686
35df4500 2687 if (new_type != bl->loc_type)
765dc015
VP
2688 {
2689 static int said = 0;
cc59ec59 2690
35df4500 2691 bl->loc_type = new_type;
765dc015
VP
2692 if (!said)
2693 {
3e43a32a
MS
2694 fprintf_filtered (gdb_stdout,
2695 _("Note: automatically using "
2696 "hardware breakpoints for "
2697 "read-only addresses.\n"));
765dc015
VP
2698 said = 1;
2699 }
2700 }
2701 }
35df4500 2702 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2703 && mr->attrib.mode != MEM_RW)
2704 {
2705 fprintf_unfiltered (tmp_error_stream,
2706 _("Cannot insert breakpoint %d.\n"
2707 "Cannot set software breakpoint "
2708 "at read-only address %s\n"),
2709 bl->owner->number,
2710 paddress (bl->gdbarch, bl->address));
2711 return 1;
2712 }
765dc015
VP
2713 }
2714 }
2715
879bfdc2
DJ
2716 /* First check to see if we have to handle an overlay. */
2717 if (overlay_debugging == ovly_off
35df4500
TJB
2718 || bl->section == NULL
2719 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2720 {
2721 /* No overlay handling: just set the breakpoint. */
492d29ea 2722 TRY
dd61ec5c 2723 {
0000e5cc
PA
2724 int val;
2725
dd61ec5c 2726 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2727 if (val)
2728 bp_err = GENERIC_ERROR;
dd61ec5c 2729 }
492d29ea 2730 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2731 {
0000e5cc
PA
2732 bp_err = e.error;
2733 bp_err_message = e.message;
dd61ec5c 2734 }
492d29ea 2735 END_CATCH
879bfdc2
DJ
2736 }
2737 else
2738 {
4a64f543 2739 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2740 Shall we set a breakpoint at the LMA? */
2741 if (!overlay_events_enabled)
2742 {
2743 /* Yes -- overlay event support is not active,
2744 so we must try to set a breakpoint at the LMA.
2745 This will not work for a hardware breakpoint. */
35df4500 2746 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2747 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2748 bl->owner->number);
879bfdc2
DJ
2749 else
2750 {
35df4500
TJB
2751 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2752 bl->section);
879bfdc2 2753 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2754 bl->overlay_target_info = bl->target_info;
0d5ed153 2755 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2756
2757 /* No overlay handling: just set the breakpoint. */
492d29ea 2758 TRY
0000e5cc
PA
2759 {
2760 int val;
2761
579c6ad9 2762 bl->overlay_target_info.kind
cd6c3b4f
YQ
2763 = breakpoint_kind (bl, &addr);
2764 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2765 val = target_insert_breakpoint (bl->gdbarch,
2766 &bl->overlay_target_info);
2767 if (val)
2768 bp_err = GENERIC_ERROR;
2769 }
492d29ea 2770 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2771 {
2772 bp_err = e.error;
2773 bp_err_message = e.message;
2774 }
492d29ea 2775 END_CATCH
0000e5cc
PA
2776
2777 if (bp_err != GDB_NO_ERROR)
99361f52 2778 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2779 "Overlay breakpoint %d "
2780 "failed: in ROM?\n",
35df4500 2781 bl->owner->number);
879bfdc2
DJ
2782 }
2783 }
2784 /* Shall we set a breakpoint at the VMA? */
35df4500 2785 if (section_is_mapped (bl->section))
879bfdc2
DJ
2786 {
2787 /* Yes. This overlay section is mapped into memory. */
492d29ea 2788 TRY
dd61ec5c 2789 {
0000e5cc
PA
2790 int val;
2791
dd61ec5c 2792 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2793 if (val)
2794 bp_err = GENERIC_ERROR;
dd61ec5c 2795 }
492d29ea 2796 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2797 {
0000e5cc
PA
2798 bp_err = e.error;
2799 bp_err_message = e.message;
dd61ec5c 2800 }
492d29ea 2801 END_CATCH
879bfdc2
DJ
2802 }
2803 else
2804 {
2805 /* No. This breakpoint will not be inserted.
2806 No error, but do not mark the bp as 'inserted'. */
2807 return 0;
2808 }
2809 }
2810
0000e5cc 2811 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2812 {
2813 /* Can't set the breakpoint. */
0000e5cc
PA
2814
2815 /* In some cases, we might not be able to insert a
2816 breakpoint in a shared library that has already been
2817 removed, but we have not yet processed the shlib unload
2818 event. Unfortunately, some targets that implement
076855f9
PA
2819 breakpoint insertion themselves can't tell why the
2820 breakpoint insertion failed (e.g., the remote target
2821 doesn't define error codes), so we must treat generic
2822 errors as memory errors. */
0000e5cc 2823 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2824 && bl->loc_type == bp_loc_software_breakpoint
08351840 2825 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2826 || shared_objfile_contains_address_p (bl->pspace,
2827 bl->address)))
879bfdc2 2828 {
4a64f543 2829 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2830 bl->shlib_disabled = 1;
8d3788bd 2831 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2832 if (!*disabled_breaks)
2833 {
2834 fprintf_unfiltered (tmp_error_stream,
2835 "Cannot insert breakpoint %d.\n",
2836 bl->owner->number);
2837 fprintf_unfiltered (tmp_error_stream,
2838 "Temporarily disabling shared "
2839 "library breakpoints:\n");
2840 }
2841 *disabled_breaks = 1;
879bfdc2 2842 fprintf_unfiltered (tmp_error_stream,
35df4500 2843 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2844 return 0;
879bfdc2
DJ
2845 }
2846 else
879bfdc2 2847 {
35df4500 2848 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2849 {
0000e5cc
PA
2850 *hw_breakpoint_error = 1;
2851 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2854 bl->owner->number, bp_err_message ? ":" : ".\n");
2855 if (bp_err_message != NULL)
2856 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2857 }
2858 else
2859 {
0000e5cc
PA
2860 if (bp_err_message == NULL)
2861 {
2862 char *message
2863 = memory_error_message (TARGET_XFER_E_IO,
2864 bl->gdbarch, bl->address);
2865 struct cleanup *old_chain = make_cleanup (xfree, message);
2866
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Cannot insert breakpoint %d.\n"
2869 "%s\n",
2870 bl->owner->number, message);
2871 do_cleanups (old_chain);
2872 }
2873 else
2874 {
2875 fprintf_unfiltered (tmp_error_stream,
2876 "Cannot insert breakpoint %d: %s\n",
2877 bl->owner->number,
2878 bp_err_message);
2879 }
879bfdc2 2880 }
0000e5cc 2881 return 1;
879bfdc2
DJ
2882
2883 }
2884 }
2885 else
35df4500 2886 bl->inserted = 1;
879bfdc2 2887
0000e5cc 2888 return 0;
879bfdc2
DJ
2889 }
2890
35df4500 2891 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2892 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2893 watchpoints. It's not clear that it's necessary... */
35df4500 2894 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2895 {
0000e5cc
PA
2896 int val;
2897
77b06cd7
TJB
2898 gdb_assert (bl->owner->ops != NULL
2899 && bl->owner->ops->insert_location != NULL);
2900
2901 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2902
2903 /* If trying to set a read-watchpoint, and it turns out it's not
2904 supported, try emulating one with an access watchpoint. */
35df4500 2905 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2906 {
2907 struct bp_location *loc, **loc_temp;
2908
2909 /* But don't try to insert it, if there's already another
2910 hw_access location that would be considered a duplicate
2911 of this one. */
2912 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2913 if (loc != bl
85d721b8 2914 && loc->watchpoint_type == hw_access
35df4500 2915 && watchpoint_locations_match (bl, loc))
85d721b8 2916 {
35df4500
TJB
2917 bl->duplicate = 1;
2918 bl->inserted = 1;
2919 bl->target_info = loc->target_info;
2920 bl->watchpoint_type = hw_access;
85d721b8
PA
2921 val = 0;
2922 break;
2923 }
2924
2925 if (val == 1)
2926 {
77b06cd7
TJB
2927 bl->watchpoint_type = hw_access;
2928 val = bl->owner->ops->insert_location (bl);
2929
2930 if (val)
2931 /* Back to the original value. */
2932 bl->watchpoint_type = hw_read;
85d721b8
PA
2933 }
2934 }
2935
35df4500 2936 bl->inserted = (val == 0);
879bfdc2
DJ
2937 }
2938
35df4500 2939 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2940 {
0000e5cc
PA
2941 int val;
2942
77b06cd7
TJB
2943 gdb_assert (bl->owner->ops != NULL
2944 && bl->owner->ops->insert_location != NULL);
2945
2946 val = bl->owner->ops->insert_location (bl);
2947 if (val)
2948 {
2949 bl->owner->enable_state = bp_disabled;
2950
2951 if (val == 1)
2952 warning (_("\
2953Error inserting catchpoint %d: Your system does not support this type\n\
2954of catchpoint."), bl->owner->number);
2955 else
2956 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2957 }
2958
2959 bl->inserted = (val == 0);
1640b821
DJ
2960
2961 /* We've already printed an error message if there was a problem
2962 inserting this catchpoint, and we've disabled the catchpoint,
2963 so just return success. */
2964 return 0;
879bfdc2
DJ
2965 }
2966
2967 return 0;
2968}
2969
6c95b8df
PA
2970/* This function is called when program space PSPACE is about to be
2971 deleted. It takes care of updating breakpoints to not reference
2972 PSPACE anymore. */
2973
2974void
2975breakpoint_program_space_exit (struct program_space *pspace)
2976{
2977 struct breakpoint *b, *b_temp;
876fa593 2978 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2979
2980 /* Remove any breakpoint that was set through this program space. */
2981 ALL_BREAKPOINTS_SAFE (b, b_temp)
2982 {
2983 if (b->pspace == pspace)
2984 delete_breakpoint (b);
2985 }
2986
2987 /* Breakpoints set through other program spaces could have locations
2988 bound to PSPACE as well. Remove those. */
876fa593 2989 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2990 {
2991 struct bp_location *tmp;
2992
2993 if (loc->pspace == pspace)
2994 {
2bdf28a0 2995 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2996 if (loc->owner->loc == loc)
2997 loc->owner->loc = loc->next;
2998 else
2999 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3000 if (tmp->next == loc)
3001 {
3002 tmp->next = loc->next;
3003 break;
3004 }
3005 }
3006 }
3007
3008 /* Now update the global location list to permanently delete the
3009 removed locations above. */
44702360 3010 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3011}
3012
74960c60
VP
3013/* Make sure all breakpoints are inserted in inferior.
3014 Throws exception on any error.
3015 A breakpoint that is already inserted won't be inserted
3016 again, so calling this function twice is safe. */
3017void
3018insert_breakpoints (void)
3019{
3020 struct breakpoint *bpt;
3021
3022 ALL_BREAKPOINTS (bpt)
3023 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3024 {
3025 struct watchpoint *w = (struct watchpoint *) bpt;
3026
3027 update_watchpoint (w, 0 /* don't reparse. */);
3028 }
74960c60 3029
04086b45
PA
3030 /* Updating watchpoints creates new locations, so update the global
3031 location list. Explicitly tell ugll to insert locations and
3032 ignore breakpoints_always_inserted_mode. */
3033 update_global_location_list (UGLL_INSERT);
74960c60
VP
3034}
3035
20388dd6
YQ
3036/* Invoke CALLBACK for each of bp_location. */
3037
3038void
3039iterate_over_bp_locations (walk_bp_location_callback callback)
3040{
3041 struct bp_location *loc, **loc_tmp;
3042
3043 ALL_BP_LOCATIONS (loc, loc_tmp)
3044 {
3045 callback (loc, NULL);
3046 }
3047}
3048
b775012e
LM
3049/* This is used when we need to synch breakpoint conditions between GDB and the
3050 target. It is the case with deleting and disabling of breakpoints when using
3051 always-inserted mode. */
3052
3053static void
3054update_inserted_breakpoint_locations (void)
3055{
3056 struct bp_location *bl, **blp_tmp;
3057 int error_flag = 0;
3058 int val = 0;
3059 int disabled_breaks = 0;
3060 int hw_breakpoint_error = 0;
dd61ec5c 3061 int hw_bp_details_reported = 0;
b775012e 3062
d7e74731 3063 string_file tmp_error_stream;
b775012e
LM
3064
3065 /* Explicitly mark the warning -- this will only be printed if
3066 there was an error. */
d7e74731 3067 tmp_error_stream.puts ("Warning:\n");
b775012e 3068
5ed8105e 3069 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
3070
3071 ALL_BP_LOCATIONS (bl, blp_tmp)
3072 {
3073 /* We only want to update software breakpoints and hardware
3074 breakpoints. */
3075 if (!is_breakpoint (bl->owner))
3076 continue;
3077
3078 /* We only want to update locations that are already inserted
3079 and need updating. This is to avoid unwanted insertion during
3080 deletion of breakpoints. */
3081 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3082 continue;
3083
3084 switch_to_program_space_and_thread (bl->pspace);
3085
3086 /* For targets that support global breakpoints, there's no need
3087 to select an inferior to insert breakpoint to. In fact, even
3088 if we aren't attached to any process yet, we should still
3089 insert breakpoints. */
f5656ead 3090 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3091 && ptid_equal (inferior_ptid, null_ptid))
3092 continue;
3093
d7e74731 3094 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3095 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3096 if (val)
3097 error_flag = val;
3098 }
3099
3100 if (error_flag)
3101 {
3102 target_terminal_ours_for_output ();
3103 error_stream (tmp_error_stream);
3104 }
b775012e
LM
3105}
3106
c30eee59 3107/* Used when starting or continuing the program. */
c906108c 3108
74960c60
VP
3109static void
3110insert_breakpoint_locations (void)
c906108c 3111{
a5606eee 3112 struct breakpoint *bpt;
35df4500 3113 struct bp_location *bl, **blp_tmp;
eacd795a 3114 int error_flag = 0;
c906108c 3115 int val = 0;
3fbb6ffa 3116 int disabled_breaks = 0;
81d0cc19 3117 int hw_breakpoint_error = 0;
dd61ec5c 3118 int hw_bp_error_explained_already = 0;
c906108c 3119
d7e74731
PA
3120 string_file tmp_error_stream;
3121
81d0cc19
GS
3122 /* Explicitly mark the warning -- this will only be printed if
3123 there was an error. */
d7e74731 3124 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3125
5ed8105e 3126 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3127
35df4500 3128 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3129 {
b775012e 3130 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3131 continue;
3132
4a64f543
MS
3133 /* There is no point inserting thread-specific breakpoints if
3134 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3135 has BL->OWNER always non-NULL. */
35df4500 3136 if (bl->owner->thread != -1
5d5658a1 3137 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3138 continue;
3139
35df4500 3140 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3141
3142 /* For targets that support global breakpoints, there's no need
3143 to select an inferior to insert breakpoint to. In fact, even
3144 if we aren't attached to any process yet, we should still
3145 insert breakpoints. */
f5656ead 3146 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3147 && ptid_equal (inferior_ptid, null_ptid))
3148 continue;
3149
d7e74731 3150 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3151 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3152 if (val)
eacd795a 3153 error_flag = val;
879bfdc2 3154 }
c906108c 3155
4a64f543
MS
3156 /* If we failed to insert all locations of a watchpoint, remove
3157 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3158 ALL_BREAKPOINTS (bpt)
3159 {
3160 int some_failed = 0;
3161 struct bp_location *loc;
3162
3163 if (!is_hardware_watchpoint (bpt))
3164 continue;
3165
d6b74ac4 3166 if (!breakpoint_enabled (bpt))
a5606eee 3167 continue;
74960c60
VP
3168
3169 if (bpt->disposition == disp_del_at_next_stop)
3170 continue;
a5606eee
VP
3171
3172 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3173 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3174 {
3175 some_failed = 1;
3176 break;
3177 }
3178 if (some_failed)
3179 {
3180 for (loc = bpt->loc; loc; loc = loc->next)
3181 if (loc->inserted)
834c0d03 3182 remove_breakpoint (loc);
a5606eee
VP
3183
3184 hw_breakpoint_error = 1;
d7e74731
PA
3185 tmp_error_stream.printf ("Could not insert "
3186 "hardware watchpoint %d.\n",
3187 bpt->number);
eacd795a 3188 error_flag = -1;
a5606eee
VP
3189 }
3190 }
3191
eacd795a 3192 if (error_flag)
81d0cc19
GS
3193 {
3194 /* If a hardware breakpoint or watchpoint was inserted, add a
3195 message about possibly exhausted resources. */
dd61ec5c 3196 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3197 {
d7e74731 3198 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3199You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3200 }
81d0cc19
GS
3201 target_terminal_ours_for_output ();
3202 error_stream (tmp_error_stream);
3203 }
c906108c
SS
3204}
3205
c30eee59
TJB
3206/* Used when the program stops.
3207 Returns zero if successful, or non-zero if there was a problem
3208 removing a breakpoint location. */
3209
c906108c 3210int
fba45db2 3211remove_breakpoints (void)
c906108c 3212{
35df4500 3213 struct bp_location *bl, **blp_tmp;
3a1bae8e 3214 int val = 0;
c906108c 3215
35df4500 3216 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3217 {
1e4d1764 3218 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3219 val |= remove_breakpoint (bl);
c5aa993b 3220 }
3a1bae8e 3221 return val;
c906108c
SS
3222}
3223
49fa26b0
PA
3224/* When a thread exits, remove breakpoints that are related to
3225 that thread. */
3226
3227static void
3228remove_threaded_breakpoints (struct thread_info *tp, int silent)
3229{
3230 struct breakpoint *b, *b_tmp;
3231
3232 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3233 {
5d5658a1 3234 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3235 {
3236 b->disposition = disp_del_at_next_stop;
3237
3238 printf_filtered (_("\
43792cf0
PA
3239Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3240 b->number, print_thread_id (tp));
49fa26b0
PA
3241
3242 /* Hide it from the user. */
3243 b->number = 0;
3244 }
3245 }
3246}
3247
6c95b8df
PA
3248/* Remove breakpoints of process PID. */
3249
3250int
3251remove_breakpoints_pid (int pid)
3252{
35df4500 3253 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3254 int val;
3255 struct inferior *inf = find_inferior_pid (pid);
3256
35df4500 3257 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3258 {
35df4500 3259 if (bl->pspace != inf->pspace)
6c95b8df
PA
3260 continue;
3261
fc126975 3262 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3263 {
834c0d03 3264 val = remove_breakpoint (bl);
6c95b8df
PA
3265 if (val != 0)
3266 return val;
3267 }
3268 }
3269 return 0;
3270}
3271
c906108c 3272int
fba45db2 3273reattach_breakpoints (int pid)
c906108c 3274{
6c95b8df 3275 struct cleanup *old_chain;
35df4500 3276 struct bp_location *bl, **blp_tmp;
c906108c 3277 int val;
dd61ec5c 3278 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3279 struct inferior *inf;
3280 struct thread_info *tp;
3281
3282 tp = any_live_thread_of_process (pid);
3283 if (tp == NULL)
3284 return 1;
3285
3286 inf = find_inferior_pid (pid);
3287 old_chain = save_inferior_ptid ();
3288
3289 inferior_ptid = tp->ptid;
a4954f26 3290
d7e74731 3291 string_file tmp_error_stream;
c906108c 3292
35df4500 3293 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3294 {
35df4500 3295 if (bl->pspace != inf->pspace)
6c95b8df
PA
3296 continue;
3297
35df4500 3298 if (bl->inserted)
c5aa993b 3299 {
35df4500 3300 bl->inserted = 0;
d7e74731 3301 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3302 if (val != 0)
3303 {
ce696e05 3304 do_cleanups (old_chain);
c5aa993b
JM
3305 return val;
3306 }
3307 }
3308 }
ce696e05 3309 do_cleanups (old_chain);
c906108c
SS
3310 return 0;
3311}
3312
e58b0e63
PA
3313static int internal_breakpoint_number = -1;
3314
84f4c1fe
PM
3315/* Set the breakpoint number of B, depending on the value of INTERNAL.
3316 If INTERNAL is non-zero, the breakpoint number will be populated
3317 from internal_breakpoint_number and that variable decremented.
e5dd4106 3318 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3319 breakpoint_count and that value incremented. Internal breakpoints
3320 do not set the internal var bpnum. */
3321static void
3322set_breakpoint_number (int internal, struct breakpoint *b)
3323{
3324 if (internal)
3325 b->number = internal_breakpoint_number--;
3326 else
3327 {
3328 set_breakpoint_count (breakpoint_count + 1);
3329 b->number = breakpoint_count;
3330 }
3331}
3332
e62c965a 3333static struct breakpoint *
a6d9a66e 3334create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3335 CORE_ADDR address, enum bptype type,
c0a91b2b 3336 const struct breakpoint_ops *ops)
e62c965a 3337{
e62c965a
PP
3338 struct symtab_and_line sal;
3339 struct breakpoint *b;
3340
4a64f543 3341 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3342
3343 sal.pc = address;
3344 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3345 sal.pspace = current_program_space;
e62c965a 3346
06edf0c0 3347 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3348 b->number = internal_breakpoint_number--;
3349 b->disposition = disp_donttouch;
3350
3351 return b;
3352}
3353
17450429
PP
3354static const char *const longjmp_names[] =
3355 {
3356 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3357 };
3358#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3359
3360/* Per-objfile data private to breakpoint.c. */
3361struct breakpoint_objfile_data
3362{
3363 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3364 struct bound_minimal_symbol overlay_msym;
17450429
PP
3365
3366 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3367 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3368
28106bc2
SDJ
3369 /* True if we have looked for longjmp probes. */
3370 int longjmp_searched;
3371
3372 /* SystemTap probe points for longjmp (if any). */
3373 VEC (probe_p) *longjmp_probes;
3374
17450429 3375 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3376 struct bound_minimal_symbol terminate_msym;
17450429
PP
3377
3378 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3379 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3380
3381 /* True if we have looked for exception probes. */
3382 int exception_searched;
3383
3384 /* SystemTap probe points for unwinding (if any). */
3385 VEC (probe_p) *exception_probes;
17450429
PP
3386};
3387
3388static const struct objfile_data *breakpoint_objfile_key;
3389
3390/* Minimal symbol not found sentinel. */
3391static struct minimal_symbol msym_not_found;
3392
3393/* Returns TRUE if MSYM point to the "not found" sentinel. */
3394
3395static int
3396msym_not_found_p (const struct minimal_symbol *msym)
3397{
3398 return msym == &msym_not_found;
3399}
3400
3401/* Return per-objfile data needed by breakpoint.c.
3402 Allocate the data if necessary. */
3403
3404static struct breakpoint_objfile_data *
3405get_breakpoint_objfile_data (struct objfile *objfile)
3406{
3407 struct breakpoint_objfile_data *bp_objfile_data;
3408
9a3c8263
SM
3409 bp_objfile_data = ((struct breakpoint_objfile_data *)
3410 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3411 if (bp_objfile_data == NULL)
3412 {
8d749320
SM
3413 bp_objfile_data =
3414 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3415
3416 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3417 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3418 }
3419 return bp_objfile_data;
3420}
3421
28106bc2
SDJ
3422static void
3423free_breakpoint_probes (struct objfile *obj, void *data)
3424{
9a3c8263
SM
3425 struct breakpoint_objfile_data *bp_objfile_data
3426 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3427
3428 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3429 VEC_free (probe_p, bp_objfile_data->exception_probes);
3430}
3431
e62c965a 3432static void
af02033e 3433create_overlay_event_breakpoint (void)
e62c965a 3434{
69de3c6a 3435 struct objfile *objfile;
af02033e 3436 const char *const func_name = "_ovly_debug_event";
e62c965a 3437
69de3c6a
PP
3438 ALL_OBJFILES (objfile)
3439 {
3440 struct breakpoint *b;
17450429
PP
3441 struct breakpoint_objfile_data *bp_objfile_data;
3442 CORE_ADDR addr;
67994074 3443 struct explicit_location explicit_loc;
69de3c6a 3444
17450429
PP
3445 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3446
3b7344d5 3447 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3448 continue;
3449
3b7344d5 3450 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3451 {
3b7344d5 3452 struct bound_minimal_symbol m;
17450429
PP
3453
3454 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3455 if (m.minsym == NULL)
17450429
PP
3456 {
3457 /* Avoid future lookups in this objfile. */
3b7344d5 3458 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3459 continue;
3460 }
3461 bp_objfile_data->overlay_msym = m;
3462 }
e62c965a 3463
77e371c0 3464 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3465 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3466 bp_overlay_event,
3467 &internal_breakpoint_ops);
67994074
KS
3468 initialize_explicit_location (&explicit_loc);
3469 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3470 b->location = new_explicit_location (&explicit_loc);
e62c965a 3471
69de3c6a
PP
3472 if (overlay_debugging == ovly_auto)
3473 {
3474 b->enable_state = bp_enabled;
3475 overlay_events_enabled = 1;
3476 }
3477 else
3478 {
3479 b->enable_state = bp_disabled;
3480 overlay_events_enabled = 0;
3481 }
e62c965a 3482 }
e62c965a
PP
3483}
3484
0fd8e87f 3485static void
af02033e 3486create_longjmp_master_breakpoint (void)
0fd8e87f 3487{
6c95b8df 3488 struct program_space *pspace;
6c95b8df 3489
5ed8105e 3490 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3491
6c95b8df 3492 ALL_PSPACES (pspace)
af02033e
PP
3493 {
3494 struct objfile *objfile;
3495
3496 set_current_program_space (pspace);
3497
3498 ALL_OBJFILES (objfile)
0fd8e87f 3499 {
af02033e
PP
3500 int i;
3501 struct gdbarch *gdbarch;
17450429 3502 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3503
af02033e 3504 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3505
17450429
PP
3506 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3507
28106bc2
SDJ
3508 if (!bp_objfile_data->longjmp_searched)
3509 {
25f9533e
SDJ
3510 VEC (probe_p) *ret;
3511
3512 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3513 if (ret != NULL)
3514 {
3515 /* We are only interested in checking one element. */
3516 struct probe *p = VEC_index (probe_p, ret, 0);
3517
3518 if (!can_evaluate_probe_arguments (p))
3519 {
3520 /* We cannot use the probe interface here, because it does
3521 not know how to evaluate arguments. */
3522 VEC_free (probe_p, ret);
3523 ret = NULL;
3524 }
3525 }
3526 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3527 bp_objfile_data->longjmp_searched = 1;
3528 }
3529
3530 if (bp_objfile_data->longjmp_probes != NULL)
3531 {
3532 int i;
3533 struct probe *probe;
3534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3535
3536 for (i = 0;
3537 VEC_iterate (probe_p,
3538 bp_objfile_data->longjmp_probes,
3539 i, probe);
3540 ++i)
3541 {
3542 struct breakpoint *b;
3543
729662a5
TT
3544 b = create_internal_breakpoint (gdbarch,
3545 get_probe_address (probe,
3546 objfile),
28106bc2
SDJ
3547 bp_longjmp_master,
3548 &internal_breakpoint_ops);
d28cd78a 3549 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3550 b->enable_state = bp_disabled;
3551 }
3552
3553 continue;
3554 }
3555
0569175e
TSD
3556 if (!gdbarch_get_longjmp_target_p (gdbarch))
3557 continue;
3558
17450429 3559 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3560 {
3561 struct breakpoint *b;
af02033e 3562 const char *func_name;
17450429 3563 CORE_ADDR addr;
67994074 3564 struct explicit_location explicit_loc;
6c95b8df 3565
3b7344d5 3566 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3567 continue;
0fd8e87f 3568
17450429 3569 func_name = longjmp_names[i];
3b7344d5 3570 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3571 {
3b7344d5 3572 struct bound_minimal_symbol m;
17450429
PP
3573
3574 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3575 if (m.minsym == NULL)
17450429
PP
3576 {
3577 /* Prevent future lookups in this objfile. */
3b7344d5 3578 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3579 continue;
3580 }
3581 bp_objfile_data->longjmp_msym[i] = m;
3582 }
3583
77e371c0 3584 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3585 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3586 &internal_breakpoint_ops);
67994074
KS
3587 initialize_explicit_location (&explicit_loc);
3588 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3589 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3590 b->enable_state = bp_disabled;
3591 }
0fd8e87f 3592 }
af02033e 3593 }
0fd8e87f
UW
3594}
3595
af02033e 3596/* Create a master std::terminate breakpoint. */
aa7d318d 3597static void
af02033e 3598create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3599{
3600 struct program_space *pspace;
af02033e 3601 const char *const func_name = "std::terminate()";
aa7d318d 3602
5ed8105e 3603 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3604
3605 ALL_PSPACES (pspace)
17450429
PP
3606 {
3607 struct objfile *objfile;
3608 CORE_ADDR addr;
3609
3610 set_current_program_space (pspace);
3611
aa7d318d
TT
3612 ALL_OBJFILES (objfile)
3613 {
3614 struct breakpoint *b;
17450429 3615 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3616 struct explicit_location explicit_loc;
aa7d318d 3617
17450429 3618 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3619
3b7344d5 3620 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3621 continue;
3622
3b7344d5 3623 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3624 {
3b7344d5 3625 struct bound_minimal_symbol m;
17450429
PP
3626
3627 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3628 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3629 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3630 {
3631 /* Prevent future lookups in this objfile. */
3b7344d5 3632 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3633 continue;
3634 }
3635 bp_objfile_data->terminate_msym = m;
3636 }
aa7d318d 3637
77e371c0 3638 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3639 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3640 bp_std_terminate_master,
3641 &internal_breakpoint_ops);
67994074
KS
3642 initialize_explicit_location (&explicit_loc);
3643 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3644 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3645 b->enable_state = bp_disabled;
3646 }
17450429 3647 }
aa7d318d
TT
3648}
3649
186c406b
TT
3650/* Install a master breakpoint on the unwinder's debug hook. */
3651
70221824 3652static void
186c406b
TT
3653create_exception_master_breakpoint (void)
3654{
3655 struct objfile *objfile;
17450429 3656 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3657
3658 ALL_OBJFILES (objfile)
3659 {
17450429
PP
3660 struct breakpoint *b;
3661 struct gdbarch *gdbarch;
3662 struct breakpoint_objfile_data *bp_objfile_data;
3663 CORE_ADDR addr;
67994074 3664 struct explicit_location explicit_loc;
17450429
PP
3665
3666 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3667
28106bc2
SDJ
3668 /* We prefer the SystemTap probe point if it exists. */
3669 if (!bp_objfile_data->exception_searched)
3670 {
25f9533e
SDJ
3671 VEC (probe_p) *ret;
3672
3673 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3674
3675 if (ret != NULL)
3676 {
3677 /* We are only interested in checking one element. */
3678 struct probe *p = VEC_index (probe_p, ret, 0);
3679
3680 if (!can_evaluate_probe_arguments (p))
3681 {
3682 /* We cannot use the probe interface here, because it does
3683 not know how to evaluate arguments. */
3684 VEC_free (probe_p, ret);
3685 ret = NULL;
3686 }
3687 }
3688 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3689 bp_objfile_data->exception_searched = 1;
3690 }
3691
3692 if (bp_objfile_data->exception_probes != NULL)
3693 {
3694 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3695 int i;
3696 struct probe *probe;
3697
3698 for (i = 0;
3699 VEC_iterate (probe_p,
3700 bp_objfile_data->exception_probes,
3701 i, probe);
3702 ++i)
3703 {
3704 struct breakpoint *b;
3705
729662a5
TT
3706 b = create_internal_breakpoint (gdbarch,
3707 get_probe_address (probe,
3708 objfile),
28106bc2
SDJ
3709 bp_exception_master,
3710 &internal_breakpoint_ops);
d28cd78a 3711 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3712 b->enable_state = bp_disabled;
3713 }
3714
3715 continue;
3716 }
3717
3718 /* Otherwise, try the hook function. */
3719
3b7344d5 3720 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3721 continue;
3722
3723 gdbarch = get_objfile_arch (objfile);
186c406b 3724
3b7344d5 3725 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3726 {
3b7344d5 3727 struct bound_minimal_symbol debug_hook;
186c406b 3728
17450429 3729 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3730 if (debug_hook.minsym == NULL)
17450429 3731 {
3b7344d5 3732 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3733 continue;
3734 }
3735
3736 bp_objfile_data->exception_msym = debug_hook;
186c406b 3737 }
17450429 3738
77e371c0 3739 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3740 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3741 &current_target);
06edf0c0
PA
3742 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3743 &internal_breakpoint_ops);
67994074
KS
3744 initialize_explicit_location (&explicit_loc);
3745 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3746 b->location = new_explicit_location (&explicit_loc);
17450429 3747 b->enable_state = bp_disabled;
186c406b 3748 }
186c406b
TT
3749}
3750
9ef9e6a6
KS
3751/* Does B have a location spec? */
3752
3753static int
3754breakpoint_event_location_empty_p (const struct breakpoint *b)
3755{
d28cd78a 3756 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3757}
3758
c906108c 3759void
fba45db2 3760update_breakpoints_after_exec (void)
c906108c 3761{
35df4500 3762 struct breakpoint *b, *b_tmp;
876fa593 3763 struct bp_location *bploc, **bplocp_tmp;
c906108c 3764
25b22b0a
PA
3765 /* We're about to delete breakpoints from GDB's lists. If the
3766 INSERTED flag is true, GDB will try to lift the breakpoints by
3767 writing the breakpoints' "shadow contents" back into memory. The
3768 "shadow contents" are NOT valid after an exec, so GDB should not
3769 do that. Instead, the target is responsible from marking
3770 breakpoints out as soon as it detects an exec. We don't do that
3771 here instead, because there may be other attempts to delete
3772 breakpoints after detecting an exec and before reaching here. */
876fa593 3773 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3774 if (bploc->pspace == current_program_space)
3775 gdb_assert (!bploc->inserted);
c906108c 3776
35df4500 3777 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3778 {
6c95b8df
PA
3779 if (b->pspace != current_program_space)
3780 continue;
3781
4a64f543 3782 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3783 if (b->type == bp_shlib_event)
3784 {
3785 delete_breakpoint (b);
3786 continue;
3787 }
c906108c 3788
4a64f543 3789 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3790 if (b->type == bp_jit_event)
3791 {
3792 delete_breakpoint (b);
3793 continue;
3794 }
3795
1900040c 3796 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3797 as must overlay event and longjmp master breakpoints. */
3798 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3799 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3800 || b->type == bp_exception_master)
c4093a6a
JM
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805
4a64f543 3806 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3807 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3808 {
3809 delete_breakpoint (b);
3810 continue;
3811 }
3812
7c16b83e
PA
3813 /* Just like single-step breakpoints. */
3814 if (b->type == bp_single_step)
3815 {
3816 delete_breakpoint (b);
3817 continue;
3818 }
3819
611c83ae
PA
3820 /* Longjmp and longjmp-resume breakpoints are also meaningless
3821 after an exec. */
186c406b 3822 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3823 || b->type == bp_longjmp_call_dummy
186c406b 3824 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3825 {
3826 delete_breakpoint (b);
3827 continue;
3828 }
3829
ce78b96d
JB
3830 if (b->type == bp_catchpoint)
3831 {
3832 /* For now, none of the bp_catchpoint breakpoints need to
3833 do anything at this point. In the future, if some of
3834 the catchpoints need to something, we will need to add
3835 a new method, and call this method from here. */
3836 continue;
3837 }
3838
c5aa993b
JM
3839 /* bp_finish is a special case. The only way we ought to be able
3840 to see one of these when an exec() has happened, is if the user
3841 caught a vfork, and then said "finish". Ordinarily a finish just
3842 carries them to the call-site of the current callee, by setting
3843 a temporary bp there and resuming. But in this case, the finish
3844 will carry them entirely through the vfork & exec.
3845
3846 We don't want to allow a bp_finish to remain inserted now. But
3847 we can't safely delete it, 'cause finish_command has a handle to
3848 the bp on a bpstat, and will later want to delete it. There's a
3849 chance (and I've seen it happen) that if we delete the bp_finish
3850 here, that its storage will get reused by the time finish_command
3851 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3852 We really must allow finish_command to delete a bp_finish.
3853
e5dd4106 3854 In the absence of a general solution for the "how do we know
53a5351d
JM
3855 it's safe to delete something others may have handles to?"
3856 problem, what we'll do here is just uninsert the bp_finish, and
3857 let finish_command delete it.
3858
3859 (We know the bp_finish is "doomed" in the sense that it's
3860 momentary, and will be deleted as soon as finish_command sees
3861 the inferior stopped. So it doesn't matter that the bp's
3862 address is probably bogus in the new a.out, unlike e.g., the
3863 solib breakpoints.) */
c5aa993b 3864
c5aa993b
JM
3865 if (b->type == bp_finish)
3866 {
3867 continue;
3868 }
3869
3870 /* Without a symbolic address, we have little hope of the
3871 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3872 a.out. */
9ef9e6a6 3873 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3874 {
3875 delete_breakpoint (b);
3876 continue;
3877 }
c5aa993b 3878 }
c906108c
SS
3879}
3880
3881int
d80ee84f 3882detach_breakpoints (ptid_t ptid)
c906108c 3883{
35df4500 3884 struct bp_location *bl, **blp_tmp;
3a1bae8e 3885 int val = 0;
ce696e05 3886 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3887 struct inferior *inf = current_inferior ();
c5aa993b 3888
dfd4cc63 3889 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3890 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3891
6c95b8df 3892 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3893 inferior_ptid = ptid;
35df4500 3894 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3895 {
35df4500 3896 if (bl->pspace != inf->pspace)
6c95b8df
PA
3897 continue;
3898
bd9673a4
PW
3899 /* This function must physically remove breakpoints locations
3900 from the specified ptid, without modifying the breakpoint
3901 package's state. Locations of type bp_loc_other are only
3902 maintained at GDB side. So, there is no need to remove
3903 these bp_loc_other locations. Moreover, removing these
3904 would modify the breakpoint package's state. */
3905 if (bl->loc_type == bp_loc_other)
3906 continue;
3907
35df4500 3908 if (bl->inserted)
b2b6a7da 3909 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3910 }
d03285ec 3911
ce696e05 3912 do_cleanups (old_chain);
3a1bae8e 3913 return val;
c906108c
SS
3914}
3915
35df4500 3916/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3917 Note that this is used to detach breakpoints from a child fork.
3918 When we get here, the child isn't in the inferior list, and neither
3919 do we have objects to represent its address space --- we should
35df4500 3920 *not* look at bl->pspace->aspace here. */
6c95b8df 3921
c906108c 3922static int
b2b6a7da 3923remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3924{
3925 int val;
c5aa993b 3926
35df4500
TJB
3927 /* BL is never in moribund_locations by our callers. */
3928 gdb_assert (bl->owner != NULL);
2bdf28a0 3929
74960c60
VP
3930 /* The type of none suggests that owner is actually deleted.
3931 This should not ever happen. */
35df4500 3932 gdb_assert (bl->owner->type != bp_none);
0bde7532 3933
35df4500
TJB
3934 if (bl->loc_type == bp_loc_software_breakpoint
3935 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3936 {
c02f5703
MS
3937 /* "Normal" instruction breakpoint: either the standard
3938 trap-instruction bp (bp_breakpoint), or a
3939 bp_hardware_breakpoint. */
3940
3941 /* First check to see if we have to handle an overlay. */
3942 if (overlay_debugging == ovly_off
35df4500
TJB
3943 || bl->section == NULL
3944 || !(section_is_overlay (bl->section)))
c02f5703
MS
3945 {
3946 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3947
3948 /* If we're trying to uninsert a memory breakpoint that we
3949 know is set in a dynamic object that is marked
3950 shlib_disabled, then either the dynamic object was
3951 removed with "remove-symbol-file" or with
3952 "nosharedlibrary". In the former case, we don't know
3953 whether another dynamic object might have loaded over the
3954 breakpoint's address -- the user might well let us know
3955 about it next with add-symbol-file (the whole point of
d03de421 3956 add-symbol-file is letting the user manually maintain a
08351840
PA
3957 list of dynamically loaded objects). If we have the
3958 breakpoint's shadow memory, that is, this is a software
3959 breakpoint managed by GDB, check whether the breakpoint
3960 is still inserted in memory, to avoid overwriting wrong
3961 code with stale saved shadow contents. Note that HW
3962 breakpoints don't have shadow memory, as they're
3963 implemented using a mechanism that is not dependent on
3964 being able to modify the target's memory, and as such
3965 they should always be removed. */
3966 if (bl->shlib_disabled
3967 && bl->target_info.shadow_len != 0
3968 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3969 val = 0;
3970 else
73971819 3971 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3972 }
c906108c
SS
3973 else
3974 {
4a64f543 3975 /* This breakpoint is in an overlay section.
c02f5703
MS
3976 Did we set a breakpoint at the LMA? */
3977 if (!overlay_events_enabled)
3978 {
3979 /* Yes -- overlay event support is not active, so we
3980 should have set a breakpoint at the LMA. Remove it.
3981 */
c02f5703
MS
3982 /* Ignore any failures: if the LMA is in ROM, we will
3983 have already warned when we failed to insert it. */
35df4500
TJB
3984 if (bl->loc_type == bp_loc_hardware_breakpoint)
3985 target_remove_hw_breakpoint (bl->gdbarch,
3986 &bl->overlay_target_info);
c02f5703 3987 else
35df4500 3988 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3989 &bl->overlay_target_info,
3990 reason);
c02f5703
MS
3991 }
3992 /* Did we set a breakpoint at the VMA?
3993 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3994 if (bl->inserted)
c906108c 3995 {
c02f5703
MS
3996 /* Yes -- remove it. Previously we did not bother to
3997 remove the breakpoint if the section had been
3998 unmapped, but let's not rely on that being safe. We
3999 don't know what the overlay manager might do. */
aa67235e
UW
4000
4001 /* However, we should remove *software* breakpoints only
4002 if the section is still mapped, or else we overwrite
4003 wrong code with the saved shadow contents. */
348d480f
PA
4004 if (bl->loc_type == bp_loc_hardware_breakpoint
4005 || section_is_mapped (bl->section))
73971819 4006 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4007 else
4008 val = 0;
c906108c 4009 }
c02f5703
MS
4010 else
4011 {
4012 /* No -- not inserted, so no need to remove. No error. */
4013 val = 0;
4014 }
c906108c 4015 }
879d1e6b 4016
08351840
PA
4017 /* In some cases, we might not be able to remove a breakpoint in
4018 a shared library that has already been removed, but we have
4019 not yet processed the shlib unload event. Similarly for an
4020 unloaded add-symbol-file object - the user might not yet have
4021 had the chance to remove-symbol-file it. shlib_disabled will
4022 be set if the library/object has already been removed, but
4023 the breakpoint hasn't been uninserted yet, e.g., after
4024 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4025 always-inserted mode. */
076855f9 4026 if (val
08351840
PA
4027 && (bl->loc_type == bp_loc_software_breakpoint
4028 && (bl->shlib_disabled
4029 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4030 || shared_objfile_contains_address_p (bl->pspace,
4031 bl->address))))
879d1e6b
UW
4032 val = 0;
4033
c906108c
SS
4034 if (val)
4035 return val;
b2b6a7da 4036 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4037 }
35df4500 4038 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4039 {
77b06cd7
TJB
4040 gdb_assert (bl->owner->ops != NULL
4041 && bl->owner->ops->remove_location != NULL);
4042
b2b6a7da 4043 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4044 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4045
c906108c 4046 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4047 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4048 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4049 bl->owner->number);
c906108c 4050 }
35df4500
TJB
4051 else if (bl->owner->type == bp_catchpoint
4052 && breakpoint_enabled (bl->owner)
4053 && !bl->duplicate)
ce78b96d 4054 {
77b06cd7
TJB
4055 gdb_assert (bl->owner->ops != NULL
4056 && bl->owner->ops->remove_location != NULL);
ce78b96d 4057
73971819 4058 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4059 if (val)
4060 return val;
77b06cd7 4061
b2b6a7da 4062 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4063 }
c906108c
SS
4064
4065 return 0;
4066}
4067
6c95b8df 4068static int
834c0d03 4069remove_breakpoint (struct bp_location *bl)
6c95b8df 4070{
35df4500
TJB
4071 /* BL is never in moribund_locations by our callers. */
4072 gdb_assert (bl->owner != NULL);
2bdf28a0 4073
6c95b8df
PA
4074 /* The type of none suggests that owner is actually deleted.
4075 This should not ever happen. */
35df4500 4076 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4077
5ed8105e 4078 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4079
35df4500 4080 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4081
5ed8105e 4082 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4083}
4084
c906108c
SS
4085/* Clear the "inserted" flag in all breakpoints. */
4086
25b22b0a 4087void
fba45db2 4088mark_breakpoints_out (void)
c906108c 4089{
35df4500 4090 struct bp_location *bl, **blp_tmp;
c906108c 4091
35df4500 4092 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4093 if (bl->pspace == current_program_space)
35df4500 4094 bl->inserted = 0;
c906108c
SS
4095}
4096
53a5351d
JM
4097/* Clear the "inserted" flag in all breakpoints and delete any
4098 breakpoints which should go away between runs of the program.
c906108c
SS
4099
4100 Plus other such housekeeping that has to be done for breakpoints
4101 between runs.
4102
53a5351d
JM
4103 Note: this function gets called at the end of a run (by
4104 generic_mourn_inferior) and when a run begins (by
4a64f543 4105 init_wait_for_inferior). */
c906108c
SS
4106
4107
4108
4109void
fba45db2 4110breakpoint_init_inferior (enum inf_context context)
c906108c 4111{
35df4500 4112 struct breakpoint *b, *b_tmp;
870f88f7 4113 struct bp_location *bl;
1c5cfe86 4114 int ix;
6c95b8df 4115 struct program_space *pspace = current_program_space;
c906108c 4116
50c71eaf
PA
4117 /* If breakpoint locations are shared across processes, then there's
4118 nothing to do. */
f5656ead 4119 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4120 return;
4121
1a853c52 4122 mark_breakpoints_out ();
075f6582 4123
35df4500 4124 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4125 {
6c95b8df
PA
4126 if (b->loc && b->loc->pspace != pspace)
4127 continue;
4128
c5aa993b
JM
4129 switch (b->type)
4130 {
4131 case bp_call_dummy:
e2e4d78b 4132 case bp_longjmp_call_dummy:
c906108c 4133
c5aa993b 4134 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4135 cause problems when the inferior is rerun, so we better get
4136 rid of it. */
4137
4138 case bp_watchpoint_scope:
4139
4140 /* Also get rid of scope breakpoints. */
4141
4142 case bp_shlib_event:
4143
4144 /* Also remove solib event breakpoints. Their addresses may
4145 have changed since the last time we ran the program.
4146 Actually we may now be debugging against different target;
4147 and so the solib backend that installed this breakpoint may
4148 not be used in by the target. E.g.,
4149
4150 (gdb) file prog-linux
4151 (gdb) run # native linux target
4152 ...
4153 (gdb) kill
4154 (gdb) file prog-win.exe
4155 (gdb) tar rem :9999 # remote Windows gdbserver.
4156 */
c906108c 4157
f59f708a
PA
4158 case bp_step_resume:
4159
4160 /* Also remove step-resume breakpoints. */
4161
7c16b83e
PA
4162 case bp_single_step:
4163
4164 /* Also remove single-step breakpoints. */
4165
c5aa993b
JM
4166 delete_breakpoint (b);
4167 break;
c906108c 4168
c5aa993b
JM
4169 case bp_watchpoint:
4170 case bp_hardware_watchpoint:
4171 case bp_read_watchpoint:
4172 case bp_access_watchpoint:
3a5c3e22
PA
4173 {
4174 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4175
3a5c3e22
PA
4176 /* Likewise for watchpoints on local expressions. */
4177 if (w->exp_valid_block != NULL)
4178 delete_breakpoint (b);
63000888 4179 else
3a5c3e22 4180 {
63000888
PA
4181 /* Get rid of existing locations, which are no longer
4182 valid. New ones will be created in
4183 update_watchpoint, when the inferior is restarted.
4184 The next update_global_location_list call will
4185 garbage collect them. */
4186 b->loc = NULL;
4187
4188 if (context == inf_starting)
4189 {
4190 /* Reset val field to force reread of starting value in
4191 insert_breakpoints. */
4192 if (w->val)
4193 value_free (w->val);
4194 w->val = NULL;
4195 w->val_valid = 0;
4196 }
4197 }
3a5c3e22 4198 }
c5aa993b
JM
4199 break;
4200 default:
c5aa993b
JM
4201 break;
4202 }
4203 }
1c5cfe86
PA
4204
4205 /* Get rid of the moribund locations. */
35df4500
TJB
4206 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4207 decref_bp_location (&bl);
1c5cfe86 4208 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4209}
4210
6c95b8df
PA
4211/* These functions concern about actual breakpoints inserted in the
4212 target --- to e.g. check if we need to do decr_pc adjustment or if
4213 we need to hop over the bkpt --- so we check for address space
4214 match, not program space. */
4215
c2c6d25f
JM
4216/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4217 exists at PC. It returns ordinary_breakpoint_here if it's an
4218 ordinary breakpoint, or permanent_breakpoint_here if it's a
4219 permanent breakpoint.
4220 - When continuing from a location with an ordinary breakpoint, we
4221 actually single step once before calling insert_breakpoints.
e5dd4106 4222 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4223 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4224 the target, to advance the PC past the breakpoint. */
c906108c 4225
c2c6d25f 4226enum breakpoint_here
6c95b8df 4227breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4228{
35df4500 4229 struct bp_location *bl, **blp_tmp;
c2c6d25f 4230 int any_breakpoint_here = 0;
c906108c 4231
35df4500 4232 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4233 {
35df4500
TJB
4234 if (bl->loc_type != bp_loc_software_breakpoint
4235 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4236 continue;
4237
f1310107 4238 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4239 if ((breakpoint_enabled (bl->owner)
1a853c52 4240 || bl->permanent)
f1310107 4241 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4242 {
4243 if (overlay_debugging
35df4500
TJB
4244 && section_is_overlay (bl->section)
4245 && !section_is_mapped (bl->section))
075f6582 4246 continue; /* unmapped overlay -- can't be a match */
1a853c52 4247 else if (bl->permanent)
075f6582
DJ
4248 return permanent_breakpoint_here;
4249 else
4250 any_breakpoint_here = 1;
4251 }
4252 }
c906108c 4253
f486487f 4254 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4255}
4256
d35ae833
PA
4257/* See breakpoint.h. */
4258
4259int
4260breakpoint_in_range_p (struct address_space *aspace,
4261 CORE_ADDR addr, ULONGEST len)
4262{
4263 struct bp_location *bl, **blp_tmp;
4264
4265 ALL_BP_LOCATIONS (bl, blp_tmp)
4266 {
4267 if (bl->loc_type != bp_loc_software_breakpoint
4268 && bl->loc_type != bp_loc_hardware_breakpoint)
4269 continue;
4270
4271 if ((breakpoint_enabled (bl->owner)
4272 || bl->permanent)
4273 && breakpoint_location_address_range_overlap (bl, aspace,
4274 addr, len))
4275 {
4276 if (overlay_debugging
4277 && section_is_overlay (bl->section)
4278 && !section_is_mapped (bl->section))
4279 {
4280 /* Unmapped overlay -- can't be a match. */
4281 continue;
4282 }
4283
4284 return 1;
4285 }
4286 }
4287
4288 return 0;
4289}
4290
1c5cfe86
PA
4291/* Return true if there's a moribund breakpoint at PC. */
4292
4293int
6c95b8df 4294moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4295{
4296 struct bp_location *loc;
4297 int ix;
4298
4299 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4300 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4301 return 1;
4302
4303 return 0;
4304}
c2c6d25f 4305
f7ce857f
PA
4306/* Returns non-zero iff BL is inserted at PC, in address space
4307 ASPACE. */
4308
4309static int
4310bp_location_inserted_here_p (struct bp_location *bl,
4311 struct address_space *aspace, CORE_ADDR pc)
4312{
4313 if (bl->inserted
4314 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4315 aspace, pc))
4316 {
4317 if (overlay_debugging
4318 && section_is_overlay (bl->section)
4319 && !section_is_mapped (bl->section))
4320 return 0; /* unmapped overlay -- can't be a match */
4321 else
4322 return 1;
4323 }
4324 return 0;
4325}
4326
a1fd2fa5 4327/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4328
4329int
a1fd2fa5 4330breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4331{
f7ce857f 4332 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4333
f7ce857f 4334 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4335 {
f7ce857f
PA
4336 struct bp_location *bl = *blp;
4337
35df4500
TJB
4338 if (bl->loc_type != bp_loc_software_breakpoint
4339 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4340 continue;
4341
f7ce857f
PA
4342 if (bp_location_inserted_here_p (bl, aspace, pc))
4343 return 1;
c5aa993b 4344 }
c36b740a
VP
4345 return 0;
4346}
4347
a1fd2fa5
PA
4348/* This function returns non-zero iff there is a software breakpoint
4349 inserted at PC. */
c36b740a
VP
4350
4351int
a1fd2fa5
PA
4352software_breakpoint_inserted_here_p (struct address_space *aspace,
4353 CORE_ADDR pc)
4fa8626c 4354{
f7ce857f 4355 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4356
f7ce857f 4357 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4358 {
f7ce857f
PA
4359 struct bp_location *bl = *blp;
4360
35df4500 4361 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4362 continue;
4363
f7ce857f
PA
4364 if (bp_location_inserted_here_p (bl, aspace, pc))
4365 return 1;
4fa8626c
DJ
4366 }
4367
4368 return 0;
9c02b525
PA
4369}
4370
4371/* See breakpoint.h. */
4372
4373int
4374hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4375 CORE_ADDR pc)
4376{
4377 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4378
4379 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4380 {
4381 struct bp_location *bl = *blp;
4382
4383 if (bl->loc_type != bp_loc_hardware_breakpoint)
4384 continue;
4385
4386 if (bp_location_inserted_here_p (bl, aspace, pc))
4387 return 1;
4388 }
4389
4390 return 0;
4fa8626c
DJ
4391}
4392
9093389c
PA
4393int
4394hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4395 CORE_ADDR addr, ULONGEST len)
4396{
4397 struct breakpoint *bpt;
4398
4399 ALL_BREAKPOINTS (bpt)
4400 {
4401 struct bp_location *loc;
4402
4403 if (bpt->type != bp_hardware_watchpoint
4404 && bpt->type != bp_access_watchpoint)
4405 continue;
4406
4407 if (!breakpoint_enabled (bpt))
4408 continue;
4409
4410 for (loc = bpt->loc; loc; loc = loc->next)
4411 if (loc->pspace->aspace == aspace && loc->inserted)
4412 {
4413 CORE_ADDR l, h;
4414
4415 /* Check for intersection. */
768adc05
PA
4416 l = std::max<CORE_ADDR> (loc->address, addr);
4417 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4418 if (l < h)
4419 return 1;
4420 }
4421 }
4422 return 0;
4423}
c906108c 4424\f
c5aa993b 4425
c906108c
SS
4426/* bpstat stuff. External routines' interfaces are documented
4427 in breakpoint.h. */
4428
4429int
c326b90e 4430is_catchpoint (struct breakpoint *ep)
c906108c 4431{
533be4dd 4432 return (ep->type == bp_catchpoint);
c906108c
SS
4433}
4434
f431efe5
PA
4435/* Frees any storage that is part of a bpstat. Does not walk the
4436 'next' chain. */
4437
4438static void
198757a8
VP
4439bpstat_free (bpstat bs)
4440{
4441 if (bs->old_val != NULL)
4442 value_free (bs->old_val);
9add0f1b 4443 decref_counted_command_line (&bs->commands);
f431efe5 4444 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4445 xfree (bs);
4446}
4447
c906108c
SS
4448/* Clear a bpstat so that it says we are not at any breakpoint.
4449 Also free any storage that is part of a bpstat. */
4450
4451void
fba45db2 4452bpstat_clear (bpstat *bsp)
c906108c
SS
4453{
4454 bpstat p;
4455 bpstat q;
4456
4457 if (bsp == 0)
4458 return;
4459 p = *bsp;
4460 while (p != NULL)
4461 {
4462 q = p->next;
198757a8 4463 bpstat_free (p);
c906108c
SS
4464 p = q;
4465 }
4466 *bsp = NULL;
4467}
4468
4469/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4470 is part of the bpstat is copied as well. */
4471
4472bpstat
fba45db2 4473bpstat_copy (bpstat bs)
c906108c
SS
4474{
4475 bpstat p = NULL;
4476 bpstat tmp;
4477 bpstat retval = NULL;
4478
4479 if (bs == NULL)
4480 return bs;
4481
4482 for (; bs != NULL; bs = bs->next)
4483 {
4484 tmp = (bpstat) xmalloc (sizeof (*tmp));
4485 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4486 incref_counted_command_line (tmp->commands);
f431efe5 4487 incref_bp_location (tmp->bp_location_at);
31cc81e9 4488 if (bs->old_val != NULL)
3c3185ac
JK
4489 {
4490 tmp->old_val = value_copy (bs->old_val);
4491 release_value (tmp->old_val);
4492 }
31cc81e9 4493
c906108c
SS
4494 if (p == NULL)
4495 /* This is the first thing in the chain. */
4496 retval = tmp;
4497 else
4498 p->next = tmp;
4499 p = tmp;
4500 }
4501 p->next = NULL;
4502 return retval;
4503}
4504
4a64f543 4505/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4506
4507bpstat
fba45db2 4508bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4509{
c5aa993b
JM
4510 if (bsp == NULL)
4511 return NULL;
c906108c 4512
c5aa993b
JM
4513 for (; bsp != NULL; bsp = bsp->next)
4514 {
f431efe5 4515 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4516 return bsp;
4517 }
c906108c
SS
4518 return NULL;
4519}
4520
ab04a2af
TT
4521/* See breakpoint.h. */
4522
47591c29 4523int
427cd150 4524bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4525{
ab04a2af
TT
4526 for (; bsp != NULL; bsp = bsp->next)
4527 {
427cd150
TT
4528 if (bsp->breakpoint_at == NULL)
4529 {
4530 /* A moribund location can never explain a signal other than
4531 GDB_SIGNAL_TRAP. */
4532 if (sig == GDB_SIGNAL_TRAP)
47591c29 4533 return 1;
427cd150
TT
4534 }
4535 else
47591c29
PA
4536 {
4537 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4538 sig))
4539 return 1;
4540 }
ab04a2af
TT
4541 }
4542
47591c29 4543 return 0;
ab04a2af
TT
4544}
4545
4a64f543
MS
4546/* Put in *NUM the breakpoint number of the first breakpoint we are
4547 stopped at. *BSP upon return is a bpstat which points to the
4548 remaining breakpoints stopped at (but which is not guaranteed to be
4549 good for anything but further calls to bpstat_num).
4550
8671a17b
PA
4551 Return 0 if passed a bpstat which does not indicate any breakpoints.
4552 Return -1 if stopped at a breakpoint that has been deleted since
4553 we set it.
4554 Return 1 otherwise. */
c906108c
SS
4555
4556int
8671a17b 4557bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4558{
4559 struct breakpoint *b;
4560
4561 if ((*bsp) == NULL)
4562 return 0; /* No more breakpoint values */
8671a17b 4563
4a64f543
MS
4564 /* We assume we'll never have several bpstats that correspond to a
4565 single breakpoint -- otherwise, this function might return the
4566 same number more than once and this will look ugly. */
f431efe5 4567 b = (*bsp)->breakpoint_at;
8671a17b
PA
4568 *bsp = (*bsp)->next;
4569 if (b == NULL)
4570 return -1; /* breakpoint that's been deleted since */
4571
4572 *num = b->number; /* We have its number */
4573 return 1;
c906108c
SS
4574}
4575
e93ca019 4576/* See breakpoint.h. */
c906108c
SS
4577
4578void
e93ca019 4579bpstat_clear_actions (void)
c906108c 4580{
e93ca019
JK
4581 struct thread_info *tp;
4582 bpstat bs;
4583
4584 if (ptid_equal (inferior_ptid, null_ptid))
4585 return;
4586
4587 tp = find_thread_ptid (inferior_ptid);
4588 if (tp == NULL)
4589 return;
4590
4591 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4592 {
9add0f1b 4593 decref_counted_command_line (&bs->commands);
abf85f46 4594
c906108c
SS
4595 if (bs->old_val != NULL)
4596 {
4597 value_free (bs->old_val);
4598 bs->old_val = NULL;
4599 }
4600 }
4601}
4602
f3b1572e
PA
4603/* Called when a command is about to proceed the inferior. */
4604
4605static void
4606breakpoint_about_to_proceed (void)
4607{
4608 if (!ptid_equal (inferior_ptid, null_ptid))
4609 {
4610 struct thread_info *tp = inferior_thread ();
4611
4612 /* Allow inferior function calls in breakpoint commands to not
4613 interrupt the command list. When the call finishes
4614 successfully, the inferior will be standing at the same
4615 breakpoint as if nothing happened. */
16c381f0 4616 if (tp->control.in_infcall)
f3b1572e
PA
4617 return;
4618 }
4619
4620 breakpoint_proceeded = 1;
4621}
4622
4a64f543
MS
4623/* Stub for cleaning up our state if we error-out of a breakpoint
4624 command. */
c906108c 4625static void
4efb68b1 4626cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4627{
4628 executing_breakpoint_commands = 0;
4629}
4630
abf85f46
JK
4631/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4632 or its equivalent. */
4633
4634static int
4635command_line_is_silent (struct command_line *cmd)
4636{
4f45d445 4637 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4638}
4639
4a64f543
MS
4640/* Execute all the commands associated with all the breakpoints at
4641 this location. Any of these commands could cause the process to
4642 proceed beyond this point, etc. We look out for such changes by
4643 checking the global "breakpoint_proceeded" after each command.
c906108c 4644
347bddb7
PA
4645 Returns true if a breakpoint command resumed the inferior. In that
4646 case, it is the caller's responsibility to recall it again with the
4647 bpstat of the current thread. */
4648
4649static int
4650bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4651{
4652 bpstat bs;
4653 struct cleanup *old_chain;
347bddb7 4654 int again = 0;
c906108c
SS
4655
4656 /* Avoid endless recursion if a `source' command is contained
4657 in bs->commands. */
4658 if (executing_breakpoint_commands)
347bddb7 4659 return 0;
c906108c
SS
4660
4661 executing_breakpoint_commands = 1;
4662 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4663
1ac32117 4664 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4665
4a64f543 4666 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4667 bs = *bsp;
4668
4669 breakpoint_proceeded = 0;
4670 for (; bs != NULL; bs = bs->next)
4671 {
9add0f1b 4672 struct counted_command_line *ccmd;
6c50ab1c
JB
4673 struct command_line *cmd;
4674 struct cleanup *this_cmd_tree_chain;
4675
4676 /* Take ownership of the BSP's command tree, if it has one.
4677
4678 The command tree could legitimately contain commands like
4679 'step' and 'next', which call clear_proceed_status, which
4680 frees stop_bpstat's command tree. To make sure this doesn't
4681 free the tree we're executing out from under us, we need to
4682 take ownership of the tree ourselves. Since a given bpstat's
4683 commands are only executed once, we don't need to copy it; we
4684 can clear the pointer in the bpstat, and make sure we free
4685 the tree when we're done. */
9add0f1b
TT
4686 ccmd = bs->commands;
4687 bs->commands = NULL;
abf85f46
JK
4688 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4689 cmd = ccmd ? ccmd->commands : NULL;
4690 if (command_line_is_silent (cmd))
4691 {
4692 /* The action has been already done by bpstat_stop_status. */
4693 cmd = cmd->next;
4694 }
6c50ab1c 4695
c906108c
SS
4696 while (cmd != NULL)
4697 {
4698 execute_control_command (cmd);
4699
4700 if (breakpoint_proceeded)
4701 break;
4702 else
4703 cmd = cmd->next;
4704 }
6c50ab1c
JB
4705
4706 /* We can free this command tree now. */
4707 do_cleanups (this_cmd_tree_chain);
4708
c906108c 4709 if (breakpoint_proceeded)
32c1e744 4710 {
cb814510 4711 if (current_ui->async)
347bddb7
PA
4712 /* If we are in async mode, then the target might be still
4713 running, not stopped at any breakpoint, so nothing for
4714 us to do here -- just return to the event loop. */
4715 ;
32c1e744
VP
4716 else
4717 /* In sync mode, when execute_control_command returns
4718 we're already standing on the next breakpoint.
347bddb7
PA
4719 Breakpoint commands for that stop were not run, since
4720 execute_command does not run breakpoint commands --
4721 only command_line_handler does, but that one is not
4722 involved in execution of breakpoint commands. So, we
4723 can now execute breakpoint commands. It should be
4724 noted that making execute_command do bpstat actions is
4725 not an option -- in this case we'll have recursive
4726 invocation of bpstat for each breakpoint with a
4727 command, and can easily blow up GDB stack. Instead, we
4728 return true, which will trigger the caller to recall us
4729 with the new stop_bpstat. */
4730 again = 1;
4731 break;
32c1e744 4732 }
c906108c 4733 }
c2b8ed2c 4734 do_cleanups (old_chain);
347bddb7
PA
4735 return again;
4736}
4737
4738void
4739bpstat_do_actions (void)
4740{
353d1d73
JK
4741 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4742
347bddb7
PA
4743 /* Do any commands attached to breakpoint we are stopped at. */
4744 while (!ptid_equal (inferior_ptid, null_ptid)
4745 && target_has_execution
4746 && !is_exited (inferior_ptid)
4747 && !is_executing (inferior_ptid))
4748 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4749 and only return when it is stopped at the next breakpoint, we
4750 keep doing breakpoint actions until it returns false to
4751 indicate the inferior was not resumed. */
16c381f0 4752 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4753 break;
353d1d73
JK
4754
4755 discard_cleanups (cleanup_if_error);
c906108c
SS
4756}
4757
fa4727a6
DJ
4758/* Print out the (old or new) value associated with a watchpoint. */
4759
4760static void
4761watchpoint_value_print (struct value *val, struct ui_file *stream)
4762{
4763 if (val == NULL)
4764 fprintf_unfiltered (stream, _("<unreadable>"));
4765 else
79a45b7d
TT
4766 {
4767 struct value_print_options opts;
4768 get_user_print_options (&opts);
4769 value_print (val, stream, &opts);
4770 }
fa4727a6
DJ
4771}
4772
f303dbd6
PA
4773/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4774 debugging multiple threads. */
4775
4776void
4777maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4778{
112e8700 4779 if (uiout->is_mi_like_p ())
f303dbd6
PA
4780 return;
4781
112e8700 4782 uiout->text ("\n");
f303dbd6
PA
4783
4784 if (show_thread_that_caused_stop ())
4785 {
4786 const char *name;
4787 struct thread_info *thr = inferior_thread ();
4788
112e8700
SM
4789 uiout->text ("Thread ");
4790 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4791
4792 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4793 if (name != NULL)
4794 {
112e8700
SM
4795 uiout->text (" \"");
4796 uiout->field_fmt ("name", "%s", name);
4797 uiout->text ("\"");
f303dbd6
PA
4798 }
4799
112e8700 4800 uiout->text (" hit ");
f303dbd6
PA
4801 }
4802}
4803
e514a9d6 4804/* Generic routine for printing messages indicating why we
4a64f543 4805 stopped. The behavior of this function depends on the value
e514a9d6
JM
4806 'print_it' in the bpstat structure. Under some circumstances we
4807 may decide not to print anything here and delegate the task to
4a64f543 4808 normal_stop(). */
e514a9d6
JM
4809
4810static enum print_stop_action
4811print_bp_stop_message (bpstat bs)
4812{
4813 switch (bs->print_it)
4814 {
4815 case print_it_noop:
4a64f543 4816 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4817 return PRINT_UNKNOWN;
4818 break;
4819
4820 case print_it_done:
4821 /* We still want to print the frame, but we already printed the
4a64f543 4822 relevant messages. */
e514a9d6
JM
4823 return PRINT_SRC_AND_LOC;
4824 break;
4825
4826 case print_it_normal:
4f8d1dc6 4827 {
f431efe5
PA
4828 struct breakpoint *b = bs->breakpoint_at;
4829
1a6a67de
TJB
4830 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4831 which has since been deleted. */
4832 if (b == NULL)
4833 return PRINT_UNKNOWN;
4834
348d480f
PA
4835 /* Normal case. Call the breakpoint's print_it method. */
4836 return b->ops->print_it (bs);
4f8d1dc6 4837 }
348d480f 4838 break;
3086aeae 4839
e514a9d6 4840 default:
8e65ff28 4841 internal_error (__FILE__, __LINE__,
e2e0b3e5 4842 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4843 break;
c906108c 4844 }
c906108c
SS
4845}
4846
edcc5120
TT
4847/* A helper function that prints a shared library stopped event. */
4848
4849static void
4850print_solib_event (int is_catchpoint)
4851{
4852 int any_deleted
4853 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4854 int any_added
4855 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4856
4857 if (!is_catchpoint)
4858 {
4859 if (any_added || any_deleted)
112e8700 4860 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4861 else
112e8700
SM
4862 current_uiout->text (_("Stopped due to shared library event (no "
4863 "libraries added or removed)\n"));
edcc5120
TT
4864 }
4865
112e8700
SM
4866 if (current_uiout->is_mi_like_p ())
4867 current_uiout->field_string ("reason",
4868 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4869
4870 if (any_deleted)
4871 {
edcc5120
TT
4872 char *name;
4873 int ix;
4874
112e8700 4875 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4876 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4877 for (ix = 0;
4878 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4879 ix, name);
4880 ++ix)
4881 {
4882 if (ix > 0)
112e8700
SM
4883 current_uiout->text (" ");
4884 current_uiout->field_string ("library", name);
4885 current_uiout->text ("\n");
edcc5120 4886 }
edcc5120
TT
4887 }
4888
4889 if (any_added)
4890 {
4891 struct so_list *iter;
4892 int ix;
edcc5120 4893
112e8700 4894 current_uiout->text (_(" Inferior loaded "));
10f489e5 4895 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4896 for (ix = 0;
4897 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4898 ix, iter);
4899 ++ix)
4900 {
4901 if (ix > 0)
112e8700
SM
4902 current_uiout->text (" ");
4903 current_uiout->field_string ("library", iter->so_name);
4904 current_uiout->text ("\n");
edcc5120 4905 }
edcc5120
TT
4906 }
4907}
4908
e514a9d6
JM
4909/* Print a message indicating what happened. This is called from
4910 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4911 list - a list of the eventpoints that caused this stop. KIND is
4912 the target_waitkind for the stopping event. This
e514a9d6
JM
4913 routine calls the generic print routine for printing a message
4914 about reasons for stopping. This will print (for example) the
4915 "Breakpoint n," part of the output. The return value of this
4916 routine is one of:
c906108c 4917
4a64f543 4918 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4919 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4920 code to print the location. An example is
c5aa993b
JM
4921 "Breakpoint 1, " which should be followed by
4922 the location.
917317f4 4923 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4924 to also print the location part of the message.
4925 An example is the catch/throw messages, which
4a64f543 4926 don't require a location appended to the end.
917317f4 4927 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4928 further info to be printed. */
c906108c 4929
917317f4 4930enum print_stop_action
36dfb11c 4931bpstat_print (bpstat bs, int kind)
c906108c 4932{
f486487f 4933 enum print_stop_action val;
c5aa993b 4934
c906108c 4935 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4936 (Currently all watchpoints go on the bpstat whether hit or not.
4937 That probably could (should) be changed, provided care is taken
c906108c 4938 with respect to bpstat_explains_signal). */
e514a9d6
JM
4939 for (; bs; bs = bs->next)
4940 {
4941 val = print_bp_stop_message (bs);
4942 if (val == PRINT_SRC_ONLY
4943 || val == PRINT_SRC_AND_LOC
4944 || val == PRINT_NOTHING)
4945 return val;
4946 }
c906108c 4947
36dfb11c
TT
4948 /* If we had hit a shared library event breakpoint,
4949 print_bp_stop_message would print out this message. If we hit an
4950 OS-level shared library event, do the same thing. */
4951 if (kind == TARGET_WAITKIND_LOADED)
4952 {
edcc5120 4953 print_solib_event (0);
36dfb11c
TT
4954 return PRINT_NOTHING;
4955 }
4956
e514a9d6 4957 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4958 with and nothing was printed. */
917317f4 4959 return PRINT_UNKNOWN;
c906108c
SS
4960}
4961
c42bd95a
DE
4962/* Evaluate the expression EXP and return 1 if value is zero.
4963 This returns the inverse of the condition because it is called
4964 from catch_errors which returns 0 if an exception happened, and if an
4965 exception happens we want execution to stop.
4a64f543 4966 The argument is a "struct expression *" that has been cast to a
c42bd95a 4967 "void *" to make it pass through catch_errors. */
c906108c
SS
4968
4969static int
4efb68b1 4970breakpoint_cond_eval (void *exp)
c906108c 4971{
278cd55f 4972 struct value *mark = value_mark ();
c5aa993b 4973 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4974
c906108c
SS
4975 value_free_to_mark (mark);
4976 return i;
4977}
4978
5760d0ab 4979/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4980
4981static bpstat
5760d0ab 4982bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4983{
4984 bpstat bs;
4985
4986 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4987 bs->next = NULL;
4988 **bs_link_pointer = bs;
4989 *bs_link_pointer = &bs->next;
f431efe5
PA
4990 bs->breakpoint_at = bl->owner;
4991 bs->bp_location_at = bl;
4992 incref_bp_location (bl);
c906108c
SS
4993 /* If the condition is false, etc., don't do the commands. */
4994 bs->commands = NULL;
4995 bs->old_val = NULL;
4996 bs->print_it = print_it_normal;
4997 return bs;
4998}
4999\f
d983da9c
DJ
5000/* The target has stopped with waitstatus WS. Check if any hardware
5001 watchpoints have triggered, according to the target. */
5002
5003int
5004watchpoints_triggered (struct target_waitstatus *ws)
5005{
d92524f1 5006 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5007 CORE_ADDR addr;
5008 struct breakpoint *b;
5009
5010 if (!stopped_by_watchpoint)
5011 {
5012 /* We were not stopped by a watchpoint. Mark all watchpoints
5013 as not triggered. */
5014 ALL_BREAKPOINTS (b)
cc60f2e3 5015 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5016 {
5017 struct watchpoint *w = (struct watchpoint *) b;
5018
5019 w->watchpoint_triggered = watch_triggered_no;
5020 }
d983da9c
DJ
5021
5022 return 0;
5023 }
5024
5025 if (!target_stopped_data_address (&current_target, &addr))
5026 {
5027 /* We were stopped by a watchpoint, but we don't know where.
5028 Mark all watchpoints as unknown. */
5029 ALL_BREAKPOINTS (b)
cc60f2e3 5030 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5031 {
5032 struct watchpoint *w = (struct watchpoint *) b;
5033
5034 w->watchpoint_triggered = watch_triggered_unknown;
5035 }
d983da9c 5036
3c4797ba 5037 return 1;
d983da9c
DJ
5038 }
5039
5040 /* The target could report the data address. Mark watchpoints
5041 affected by this data address as triggered, and all others as not
5042 triggered. */
5043
5044 ALL_BREAKPOINTS (b)
cc60f2e3 5045 if (is_hardware_watchpoint (b))
d983da9c 5046 {
3a5c3e22 5047 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5048 struct bp_location *loc;
d983da9c 5049
3a5c3e22 5050 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5051 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5052 {
3a5c3e22 5053 if (is_masked_watchpoint (b))
9c06b0b4 5054 {
3a5c3e22
PA
5055 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5056 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5057
5058 if (newaddr == start)
5059 {
3a5c3e22 5060 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5061 break;
5062 }
5063 }
5064 /* Exact match not required. Within range is sufficient. */
5065 else if (target_watchpoint_addr_within_range (&current_target,
5066 addr, loc->address,
5067 loc->length))
5068 {
3a5c3e22 5069 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5070 break;
5071 }
5072 }
d983da9c
DJ
5073 }
5074
5075 return 1;
5076}
5077
c906108c
SS
5078/* Possible return values for watchpoint_check (this can't be an enum
5079 because of check_errors). */
5080/* The watchpoint has been deleted. */
5081#define WP_DELETED 1
5082/* The value has changed. */
5083#define WP_VALUE_CHANGED 2
5084/* The value has not changed. */
5085#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5086/* Ignore this watchpoint, no matter if the value changed or not. */
5087#define WP_IGNORE 4
c906108c
SS
5088
5089#define BP_TEMPFLAG 1
5090#define BP_HARDWAREFLAG 2
5091
4a64f543
MS
5092/* Evaluate watchpoint condition expression and check if its value
5093 changed.
553e4c11
JB
5094
5095 P should be a pointer to struct bpstat, but is defined as a void *
5096 in order for this function to be usable with catch_errors. */
c906108c
SS
5097
5098static int
4efb68b1 5099watchpoint_check (void *p)
c906108c
SS
5100{
5101 bpstat bs = (bpstat) p;
3a5c3e22 5102 struct watchpoint *b;
c906108c
SS
5103 struct frame_info *fr;
5104 int within_current_scope;
5105
f431efe5 5106 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5107 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5108 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5109
f6bc2008
PA
5110 /* If this is a local watchpoint, we only want to check if the
5111 watchpoint frame is in scope if the current thread is the thread
5112 that was used to create the watchpoint. */
5113 if (!watchpoint_in_thread_scope (b))
60e1c644 5114 return WP_IGNORE;
f6bc2008 5115
c906108c
SS
5116 if (b->exp_valid_block == NULL)
5117 within_current_scope = 1;
5118 else
5119 {
edb3359d
DJ
5120 struct frame_info *frame = get_current_frame ();
5121 struct gdbarch *frame_arch = get_frame_arch (frame);
5122 CORE_ADDR frame_pc = get_frame_pc (frame);
5123
c9cf6e20 5124 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5125 still in the function but the stack frame has already been
5126 invalidated. Since we can't rely on the values of local
5127 variables after the stack has been destroyed, we are treating
5128 the watchpoint in that state as `not changed' without further
5129 checking. Don't mark watchpoints as changed if the current
5130 frame is in an epilogue - even if they are in some other
5131 frame, our view of the stack is likely to be wrong and
5132 frame_find_by_id could error out. */
c9cf6e20 5133 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5134 return WP_IGNORE;
a0f49112 5135
101dcfbe 5136 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5137 within_current_scope = (fr != NULL);
69fbadd5
DJ
5138
5139 /* If we've gotten confused in the unwinder, we might have
5140 returned a frame that can't describe this variable. */
edb3359d
DJ
5141 if (within_current_scope)
5142 {
5143 struct symbol *function;
5144
5145 function = get_frame_function (fr);
5146 if (function == NULL
5147 || !contained_in (b->exp_valid_block,
5148 SYMBOL_BLOCK_VALUE (function)))
5149 within_current_scope = 0;
5150 }
69fbadd5 5151
edb3359d 5152 if (within_current_scope)
c906108c
SS
5153 /* If we end up stopping, the current frame will get selected
5154 in normal_stop. So this call to select_frame won't affect
5155 the user. */
0f7d239c 5156 select_frame (fr);
c906108c 5157 }
c5aa993b 5158
c906108c
SS
5159 if (within_current_scope)
5160 {
4a64f543
MS
5161 /* We use value_{,free_to_}mark because it could be a *long*
5162 time before we return to the command level and call
5163 free_all_values. We can't call free_all_values because we
5164 might be in the middle of evaluating a function call. */
c906108c 5165
0cf6dd15 5166 int pc = 0;
9c06b0b4 5167 struct value *mark;
fa4727a6
DJ
5168 struct value *new_val;
5169
3a5c3e22 5170 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5171 /* Since we don't know the exact trigger address (from
5172 stopped_data_address), just tell the user we've triggered
5173 a mask watchpoint. */
5174 return WP_VALUE_CHANGED;
5175
5176 mark = value_mark ();
4d01a485 5177 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5178
bb9d5f81
PP
5179 if (b->val_bitsize != 0)
5180 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5181
4a64f543
MS
5182 /* We use value_equal_contents instead of value_equal because
5183 the latter coerces an array to a pointer, thus comparing just
5184 the address of the array instead of its contents. This is
5185 not what we want. */
fa4727a6 5186 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5187 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5188 {
fa4727a6
DJ
5189 if (new_val != NULL)
5190 {
5191 release_value (new_val);
5192 value_free_to_mark (mark);
5193 }
c906108c
SS
5194 bs->old_val = b->val;
5195 b->val = new_val;
fa4727a6 5196 b->val_valid = 1;
c906108c
SS
5197 return WP_VALUE_CHANGED;
5198 }
5199 else
5200 {
60e1c644 5201 /* Nothing changed. */
c906108c 5202 value_free_to_mark (mark);
c906108c
SS
5203 return WP_VALUE_NOT_CHANGED;
5204 }
5205 }
5206 else
5207 {
5208 /* This seems like the only logical thing to do because
c5aa993b
JM
5209 if we temporarily ignored the watchpoint, then when
5210 we reenter the block in which it is valid it contains
5211 garbage (in the case of a function, it may have two
5212 garbage values, one before and one after the prologue).
5213 So we can't even detect the first assignment to it and
5214 watch after that (since the garbage may or may not equal
5215 the first value assigned). */
348d480f
PA
5216 /* We print all the stop information in
5217 breakpoint_ops->print_it, but in this case, by the time we
5218 call breakpoint_ops->print_it this bp will be deleted
5219 already. So we have no choice but print the information
5220 here. */
468afe6c 5221
0e454242 5222 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5223 {
5224 struct ui_out *uiout = current_uiout;
5225
112e8700
SM
5226 if (uiout->is_mi_like_p ())
5227 uiout->field_string
5228 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5229 uiout->text ("\nWatchpoint ");
5230 uiout->field_int ("wpnum", b->base.number);
5231 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5232 "which its expression is valid.\n");
5233 }
4ce44c66 5234
cdac0397 5235 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5236 decref_counted_command_line (&b->base.commands);
d0fb5eae 5237 watchpoint_del_at_next_stop (b);
c906108c
SS
5238
5239 return WP_DELETED;
5240 }
5241}
5242
18a18393 5243/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5244 breakpoint location BL. This function does not check if we should
5245 stop, only if BL explains the stop. */
5246
18a18393 5247static int
6c95b8df 5248bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5249 struct address_space *aspace, CORE_ADDR bp_addr,
5250 const struct target_waitstatus *ws)
18a18393
VP
5251{
5252 struct breakpoint *b = bl->owner;
5253
348d480f 5254 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5255 gdb_assert (b != NULL);
5256
09ac7c10 5257 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5258}
5259
3a5c3e22
PA
5260/* Determine if the watched values have actually changed, and we
5261 should stop. If not, set BS->stop to 0. */
5262
18a18393
VP
5263static void
5264bpstat_check_watchpoint (bpstat bs)
5265{
2bdf28a0 5266 const struct bp_location *bl;
3a5c3e22 5267 struct watchpoint *b;
2bdf28a0
JK
5268
5269 /* BS is built for existing struct breakpoint. */
f431efe5 5270 bl = bs->bp_location_at;
2bdf28a0 5271 gdb_assert (bl != NULL);
3a5c3e22 5272 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5273 gdb_assert (b != NULL);
18a18393 5274
18a18393 5275 {
18a18393
VP
5276 int must_check_value = 0;
5277
3a5c3e22 5278 if (b->base.type == bp_watchpoint)
18a18393
VP
5279 /* For a software watchpoint, we must always check the
5280 watched value. */
5281 must_check_value = 1;
5282 else if (b->watchpoint_triggered == watch_triggered_yes)
5283 /* We have a hardware watchpoint (read, write, or access)
5284 and the target earlier reported an address watched by
5285 this watchpoint. */
5286 must_check_value = 1;
5287 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5288 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5289 /* We were stopped by a hardware watchpoint, but the target could
5290 not report the data address. We must check the watchpoint's
5291 value. Access and read watchpoints are out of luck; without
5292 a data address, we can't figure it out. */
5293 must_check_value = 1;
3a5c3e22 5294
18a18393
VP
5295 if (must_check_value)
5296 {
3e43a32a
MS
5297 char *message
5298 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5299 b->base.number);
18a18393
VP
5300 struct cleanup *cleanups = make_cleanup (xfree, message);
5301 int e = catch_errors (watchpoint_check, bs, message,
5302 RETURN_MASK_ALL);
5303 do_cleanups (cleanups);
5304 switch (e)
5305 {
5306 case WP_DELETED:
5307 /* We've already printed what needs to be printed. */
5308 bs->print_it = print_it_done;
5309 /* Stop. */
5310 break;
60e1c644
PA
5311 case WP_IGNORE:
5312 bs->print_it = print_it_noop;
5313 bs->stop = 0;
5314 break;
18a18393 5315 case WP_VALUE_CHANGED:
3a5c3e22 5316 if (b->base.type == bp_read_watchpoint)
18a18393 5317 {
85d721b8
PA
5318 /* There are two cases to consider here:
5319
4a64f543 5320 1. We're watching the triggered memory for reads.
85d721b8
PA
5321 In that case, trust the target, and always report
5322 the watchpoint hit to the user. Even though
5323 reads don't cause value changes, the value may
5324 have changed since the last time it was read, and
5325 since we're not trapping writes, we will not see
5326 those, and as such we should ignore our notion of
5327 old value.
5328
4a64f543 5329 2. We're watching the triggered memory for both
85d721b8
PA
5330 reads and writes. There are two ways this may
5331 happen:
5332
4a64f543 5333 2.1. This is a target that can't break on data
85d721b8
PA
5334 reads only, but can break on accesses (reads or
5335 writes), such as e.g., x86. We detect this case
5336 at the time we try to insert read watchpoints.
5337
4a64f543 5338 2.2. Otherwise, the target supports read
85d721b8
PA
5339 watchpoints, but, the user set an access or write
5340 watchpoint watching the same memory as this read
5341 watchpoint.
5342
5343 If we're watching memory writes as well as reads,
5344 ignore watchpoint hits when we find that the
5345 value hasn't changed, as reads don't cause
5346 changes. This still gives false positives when
5347 the program writes the same value to memory as
5348 what there was already in memory (we will confuse
5349 it for a read), but it's much better than
5350 nothing. */
5351
5352 int other_write_watchpoint = 0;
5353
5354 if (bl->watchpoint_type == hw_read)
5355 {
5356 struct breakpoint *other_b;
5357
5358 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5359 if (other_b->type == bp_hardware_watchpoint
5360 || other_b->type == bp_access_watchpoint)
85d721b8 5361 {
3a5c3e22
PA
5362 struct watchpoint *other_w =
5363 (struct watchpoint *) other_b;
5364
5365 if (other_w->watchpoint_triggered
5366 == watch_triggered_yes)
5367 {
5368 other_write_watchpoint = 1;
5369 break;
5370 }
85d721b8
PA
5371 }
5372 }
5373
5374 if (other_write_watchpoint
5375 || bl->watchpoint_type == hw_access)
5376 {
5377 /* We're watching the same memory for writes,
5378 and the value changed since the last time we
5379 updated it, so this trap must be for a write.
5380 Ignore it. */
5381 bs->print_it = print_it_noop;
5382 bs->stop = 0;
5383 }
18a18393
VP
5384 }
5385 break;
5386 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5387 if (b->base.type == bp_hardware_watchpoint
5388 || b->base.type == bp_watchpoint)
18a18393
VP
5389 {
5390 /* Don't stop: write watchpoints shouldn't fire if
5391 the value hasn't changed. */
5392 bs->print_it = print_it_noop;
5393 bs->stop = 0;
5394 }
5395 /* Stop. */
5396 break;
5397 default:
5398 /* Can't happen. */
5399 case 0:
5400 /* Error from catch_errors. */
468afe6c 5401 {
0e454242 5402 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5403 {
5404 printf_filtered (_("Watchpoint %d deleted.\n"),
5405 b->base.number);
5406 }
5407 watchpoint_del_at_next_stop (b);
5408 /* We've already printed what needs to be printed. */
5409 bs->print_it = print_it_done;
5410 }
18a18393
VP
5411 break;
5412 }
5413 }
5414 else /* must_check_value == 0 */
5415 {
5416 /* This is a case where some watchpoint(s) triggered, but
5417 not at the address of this watchpoint, or else no
5418 watchpoint triggered after all. So don't print
5419 anything for this watchpoint. */
5420 bs->print_it = print_it_noop;
5421 bs->stop = 0;
5422 }
5423 }
5424}
5425
7d4df6a4
DE
5426/* For breakpoints that are currently marked as telling gdb to stop,
5427 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5428 of breakpoint referred to by BS. If we should not stop for this
5429 breakpoint, set BS->stop to 0. */
f431efe5 5430
18a18393
VP
5431static void
5432bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5433{
2bdf28a0
JK
5434 const struct bp_location *bl;
5435 struct breakpoint *b;
7d4df6a4
DE
5436 int value_is_zero = 0;
5437 struct expression *cond;
5438
5439 gdb_assert (bs->stop);
2bdf28a0
JK
5440
5441 /* BS is built for existing struct breakpoint. */
f431efe5 5442 bl = bs->bp_location_at;
2bdf28a0 5443 gdb_assert (bl != NULL);
f431efe5 5444 b = bs->breakpoint_at;
2bdf28a0 5445 gdb_assert (b != NULL);
18a18393 5446
b775012e
LM
5447 /* Even if the target evaluated the condition on its end and notified GDB, we
5448 need to do so again since GDB does not know if we stopped due to a
5449 breakpoint or a single step breakpoint. */
5450
18a18393 5451 if (frame_id_p (b->frame_id)
edb3359d 5452 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5453 {
7d4df6a4
DE
5454 bs->stop = 0;
5455 return;
5456 }
60e1c644 5457
12ab52e9
PA
5458 /* If this is a thread/task-specific breakpoint, don't waste cpu
5459 evaluating the condition if this isn't the specified
5460 thread/task. */
5d5658a1 5461 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5462 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5463
6c1b0f7b
DE
5464 {
5465 bs->stop = 0;
5466 return;
5467 }
5468
6dddc817
DE
5469 /* Evaluate extension language breakpoints that have a "stop" method
5470 implemented. */
5471 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5472
7d4df6a4
DE
5473 if (is_watchpoint (b))
5474 {
5475 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5476
4d01a485 5477 cond = w->cond_exp.get ();
7d4df6a4
DE
5478 }
5479 else
4d01a485 5480 cond = bl->cond.get ();
60e1c644 5481
7d4df6a4
DE
5482 if (cond && b->disposition != disp_del_at_next_stop)
5483 {
5484 int within_current_scope = 1;
5485 struct watchpoint * w;
60e1c644 5486
7d4df6a4
DE
5487 /* We use value_mark and value_free_to_mark because it could
5488 be a long time before we return to the command level and
5489 call free_all_values. We can't call free_all_values
5490 because we might be in the middle of evaluating a
5491 function call. */
5492 struct value *mark = value_mark ();
5493
5494 if (is_watchpoint (b))
5495 w = (struct watchpoint *) b;
5496 else
5497 w = NULL;
5498
5499 /* Need to select the frame, with all that implies so that
5500 the conditions will have the right context. Because we
5501 use the frame, we will not see an inlined function's
5502 variables when we arrive at a breakpoint at the start
5503 of the inlined function; the current frame will be the
5504 call site. */
5505 if (w == NULL || w->cond_exp_valid_block == NULL)
5506 select_frame (get_current_frame ());
5507 else
18a18393 5508 {
7d4df6a4
DE
5509 struct frame_info *frame;
5510
5511 /* For local watchpoint expressions, which particular
5512 instance of a local is being watched matters, so we
5513 keep track of the frame to evaluate the expression
5514 in. To evaluate the condition however, it doesn't
5515 really matter which instantiation of the function
5516 where the condition makes sense triggers the
5517 watchpoint. This allows an expression like "watch
5518 global if q > 10" set in `func', catch writes to
5519 global on all threads that call `func', or catch
5520 writes on all recursive calls of `func' by a single
5521 thread. We simply always evaluate the condition in
5522 the innermost frame that's executing where it makes
5523 sense to evaluate the condition. It seems
5524 intuitive. */
5525 frame = block_innermost_frame (w->cond_exp_valid_block);
5526 if (frame != NULL)
5527 select_frame (frame);
5528 else
5529 within_current_scope = 0;
18a18393 5530 }
7d4df6a4
DE
5531 if (within_current_scope)
5532 value_is_zero
5533 = catch_errors (breakpoint_cond_eval, cond,
5534 "Error in testing breakpoint condition:\n",
5535 RETURN_MASK_ALL);
5536 else
18a18393 5537 {
7d4df6a4
DE
5538 warning (_("Watchpoint condition cannot be tested "
5539 "in the current scope"));
5540 /* If we failed to set the right context for this
5541 watchpoint, unconditionally report it. */
5542 value_is_zero = 0;
18a18393 5543 }
7d4df6a4
DE
5544 /* FIXME-someday, should give breakpoint #. */
5545 value_free_to_mark (mark);
18a18393 5546 }
7d4df6a4
DE
5547
5548 if (cond && value_is_zero)
5549 {
5550 bs->stop = 0;
5551 }
7d4df6a4
DE
5552 else if (b->ignore_count > 0)
5553 {
5554 b->ignore_count--;
5555 bs->stop = 0;
5556 /* Increase the hit count even though we don't stop. */
5557 ++(b->hit_count);
5558 observer_notify_breakpoint_modified (b);
5559 }
18a18393
VP
5560}
5561
1cf4d951
PA
5562/* Returns true if we need to track moribund locations of LOC's type
5563 on the current target. */
5564
5565static int
5566need_moribund_for_location_type (struct bp_location *loc)
5567{
5568 return ((loc->loc_type == bp_loc_software_breakpoint
5569 && !target_supports_stopped_by_sw_breakpoint ())
5570 || (loc->loc_type == bp_loc_hardware_breakpoint
5571 && !target_supports_stopped_by_hw_breakpoint ()));
5572}
5573
18a18393 5574
9709f61c 5575/* Get a bpstat associated with having just stopped at address
d983da9c 5576 BP_ADDR in thread PTID.
c906108c 5577
d983da9c 5578 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5579 don't understand this stop. Result is a chain of bpstat's such
5580 that:
c906108c 5581
c5aa993b 5582 if we don't understand the stop, the result is a null pointer.
c906108c 5583
c5aa993b 5584 if we understand why we stopped, the result is not null.
c906108c 5585
c5aa993b
JM
5586 Each element of the chain refers to a particular breakpoint or
5587 watchpoint at which we have stopped. (We may have stopped for
5588 several reasons concurrently.)
c906108c 5589
c5aa993b
JM
5590 Each element of the chain has valid next, breakpoint_at,
5591 commands, FIXME??? fields. */
c906108c
SS
5592
5593bpstat
6c95b8df 5594bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5595 CORE_ADDR bp_addr, ptid_t ptid,
5596 const struct target_waitstatus *ws)
c906108c 5597{
0d381245 5598 struct breakpoint *b = NULL;
afe38095 5599 struct bp_location *bl;
20874c92 5600 struct bp_location *loc;
5760d0ab
JK
5601 /* First item of allocated bpstat's. */
5602 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5603 /* Pointer to the last thing in the chain currently. */
5760d0ab 5604 bpstat bs;
20874c92 5605 int ix;
429374b8 5606 int need_remove_insert;
f431efe5 5607 int removed_any;
c906108c 5608
f431efe5
PA
5609 /* First, build the bpstat chain with locations that explain a
5610 target stop, while being careful to not set the target running,
5611 as that may invalidate locations (in particular watchpoint
5612 locations are recreated). Resuming will happen here with
5613 breakpoint conditions or watchpoint expressions that include
5614 inferior function calls. */
c5aa993b 5615
429374b8
JK
5616 ALL_BREAKPOINTS (b)
5617 {
1a853c52 5618 if (!breakpoint_enabled (b))
429374b8 5619 continue;
a5606eee 5620
429374b8
JK
5621 for (bl = b->loc; bl != NULL; bl = bl->next)
5622 {
4a64f543
MS
5623 /* For hardware watchpoints, we look only at the first
5624 location. The watchpoint_check function will work on the
5625 entire expression, not the individual locations. For
5626 read watchpoints, the watchpoints_triggered function has
5627 checked all locations already. */
429374b8
JK
5628 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5629 break;
18a18393 5630
f6592439 5631 if (!bl->enabled || bl->shlib_disabled)
429374b8 5632 continue;
c5aa993b 5633
09ac7c10 5634 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5635 continue;
c5aa993b 5636
4a64f543
MS
5637 /* Come here if it's a watchpoint, or if the break address
5638 matches. */
c5aa993b 5639
4a64f543
MS
5640 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5641 explain stop. */
c5aa993b 5642
f431efe5
PA
5643 /* Assume we stop. Should we find a watchpoint that is not
5644 actually triggered, or if the condition of the breakpoint
5645 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5646 bs->stop = 1;
5647 bs->print = 1;
d983da9c 5648
f431efe5
PA
5649 /* If this is a scope breakpoint, mark the associated
5650 watchpoint as triggered so that we will handle the
5651 out-of-scope event. We'll get to the watchpoint next
5652 iteration. */
d0fb5eae 5653 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5654 {
5655 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5656
5657 w->watchpoint_triggered = watch_triggered_yes;
5658 }
f431efe5
PA
5659 }
5660 }
5661
7c16b83e 5662 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5663 if (!target_supports_stopped_by_sw_breakpoint ()
5664 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5665 {
1cf4d951 5666 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5667 {
1cf4d951
PA
5668 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5669 && need_moribund_for_location_type (loc))
5670 {
5671 bs = bpstat_alloc (loc, &bs_link);
5672 /* For hits of moribund locations, we should just proceed. */
5673 bs->stop = 0;
5674 bs->print = 0;
5675 bs->print_it = print_it_noop;
5676 }
f431efe5
PA
5677 }
5678 }
5679
edcc5120
TT
5680 /* A bit of special processing for shlib breakpoints. We need to
5681 process solib loading here, so that the lists of loaded and
5682 unloaded libraries are correct before we handle "catch load" and
5683 "catch unload". */
5684 for (bs = bs_head; bs != NULL; bs = bs->next)
5685 {
5d268276 5686 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5687 {
5688 handle_solib_event ();
5689 break;
5690 }
5691 }
5692
f431efe5
PA
5693 /* Now go through the locations that caused the target to stop, and
5694 check whether we're interested in reporting this stop to higher
5695 layers, or whether we should resume the target transparently. */
5696
5697 removed_any = 0;
5698
5760d0ab 5699 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5700 {
5701 if (!bs->stop)
5702 continue;
5703
f431efe5 5704 b = bs->breakpoint_at;
348d480f
PA
5705 b->ops->check_status (bs);
5706 if (bs->stop)
28010a5d 5707 {
348d480f 5708 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5709
429374b8
JK
5710 if (bs->stop)
5711 {
5712 ++(b->hit_count);
8d3788bd 5713 observer_notify_breakpoint_modified (b);
c906108c 5714
4a64f543 5715 /* We will stop here. */
429374b8
JK
5716 if (b->disposition == disp_disable)
5717 {
816338b5 5718 --(b->enable_count);
1a853c52 5719 if (b->enable_count <= 0)
429374b8 5720 b->enable_state = bp_disabled;
f431efe5 5721 removed_any = 1;
429374b8
JK
5722 }
5723 if (b->silent)
5724 bs->print = 0;
5725 bs->commands = b->commands;
9add0f1b 5726 incref_counted_command_line (bs->commands);
abf85f46
JK
5727 if (command_line_is_silent (bs->commands
5728 ? bs->commands->commands : NULL))
5729 bs->print = 0;
9d6e6e84
HZ
5730
5731 b->ops->after_condition_true (bs);
429374b8
JK
5732 }
5733
348d480f 5734 }
a9b3a50f
PA
5735
5736 /* Print nothing for this entry if we don't stop or don't
5737 print. */
5738 if (!bs->stop || !bs->print)
5739 bs->print_it = print_it_noop;
429374b8 5740 }
876fa593 5741
d983da9c
DJ
5742 /* If we aren't stopping, the value of some hardware watchpoint may
5743 not have changed, but the intermediate memory locations we are
5744 watching may have. Don't bother if we're stopping; this will get
5745 done later. */
d832cb68 5746 need_remove_insert = 0;
5760d0ab
JK
5747 if (! bpstat_causes_stop (bs_head))
5748 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5749 if (!bs->stop
f431efe5
PA
5750 && bs->breakpoint_at
5751 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5752 {
3a5c3e22
PA
5753 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5754
5755 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5756 need_remove_insert = 1;
d983da9c
DJ
5757 }
5758
d832cb68 5759 if (need_remove_insert)
44702360 5760 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5761 else if (removed_any)
44702360 5762 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5763
5760d0ab 5764 return bs_head;
c906108c 5765}
628fe4e4
JK
5766
5767static void
5768handle_jit_event (void)
5769{
5770 struct frame_info *frame;
5771 struct gdbarch *gdbarch;
5772
243a9253
PA
5773 if (debug_infrun)
5774 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5775
628fe4e4
JK
5776 /* Switch terminal for any messages produced by
5777 breakpoint_re_set. */
5778 target_terminal_ours_for_output ();
5779
5780 frame = get_current_frame ();
5781 gdbarch = get_frame_arch (frame);
5782
5783 jit_event_handler (gdbarch);
5784
5785 target_terminal_inferior ();
5786}
5787
5788/* Prepare WHAT final decision for infrun. */
5789
5790/* Decide what infrun needs to do with this bpstat. */
5791
c906108c 5792struct bpstat_what
0e30163f 5793bpstat_what (bpstat bs_head)
c906108c 5794{
c906108c 5795 struct bpstat_what retval;
0e30163f 5796 bpstat bs;
c906108c 5797
628fe4e4 5798 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5799 retval.call_dummy = STOP_NONE;
186c406b 5800 retval.is_longjmp = 0;
628fe4e4 5801
0e30163f 5802 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5803 {
628fe4e4
JK
5804 /* Extract this BS's action. After processing each BS, we check
5805 if its action overrides all we've seem so far. */
5806 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5807 enum bptype bptype;
5808
c906108c 5809 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5810 {
5811 /* I suspect this can happen if it was a momentary
5812 breakpoint which has since been deleted. */
5813 bptype = bp_none;
5814 }
20874c92 5815 else
f431efe5 5816 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5817
5818 switch (bptype)
c906108c
SS
5819 {
5820 case bp_none:
628fe4e4 5821 break;
c906108c
SS
5822 case bp_breakpoint:
5823 case bp_hardware_breakpoint:
7c16b83e 5824 case bp_single_step:
c906108c
SS
5825 case bp_until:
5826 case bp_finish:
a9b3a50f 5827 case bp_shlib_event:
c906108c
SS
5828 if (bs->stop)
5829 {
5830 if (bs->print)
628fe4e4 5831 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5832 else
628fe4e4 5833 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5834 }
5835 else
628fe4e4 5836 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5837 break;
5838 case bp_watchpoint:
5839 case bp_hardware_watchpoint:
5840 case bp_read_watchpoint:
5841 case bp_access_watchpoint:
5842 if (bs->stop)
5843 {
5844 if (bs->print)
628fe4e4 5845 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5846 else
628fe4e4 5847 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5848 }
5849 else
628fe4e4
JK
5850 {
5851 /* There was a watchpoint, but we're not stopping.
5852 This requires no further action. */
5853 }
c906108c
SS
5854 break;
5855 case bp_longjmp:
e2e4d78b 5856 case bp_longjmp_call_dummy:
186c406b 5857 case bp_exception:
0a39bb32
PA
5858 if (bs->stop)
5859 {
5860 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5861 retval.is_longjmp = bptype != bp_exception;
5862 }
5863 else
5864 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5865 break;
5866 case bp_longjmp_resume:
186c406b 5867 case bp_exception_resume:
0a39bb32
PA
5868 if (bs->stop)
5869 {
5870 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5871 retval.is_longjmp = bptype == bp_longjmp_resume;
5872 }
5873 else
5874 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5875 break;
5876 case bp_step_resume:
5877 if (bs->stop)
628fe4e4
JK
5878 this_action = BPSTAT_WHAT_STEP_RESUME;
5879 else
c906108c 5880 {
628fe4e4
JK
5881 /* It is for the wrong frame. */
5882 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5883 }
c906108c 5884 break;
2c03e5be
PA
5885 case bp_hp_step_resume:
5886 if (bs->stop)
5887 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5888 else
5889 {
5890 /* It is for the wrong frame. */
5891 this_action = BPSTAT_WHAT_SINGLE;
5892 }
5893 break;
c906108c 5894 case bp_watchpoint_scope:
c4093a6a 5895 case bp_thread_event:
1900040c 5896 case bp_overlay_event:
0fd8e87f 5897 case bp_longjmp_master:
aa7d318d 5898 case bp_std_terminate_master:
186c406b 5899 case bp_exception_master:
628fe4e4 5900 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5901 break;
ce78b96d 5902 case bp_catchpoint:
c5aa993b
JM
5903 if (bs->stop)
5904 {
5905 if (bs->print)
628fe4e4 5906 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5907 else
628fe4e4 5908 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5909 }
5910 else
628fe4e4
JK
5911 {
5912 /* There was a catchpoint, but we're not stopping.
5913 This requires no further action. */
5914 }
5915 break;
628fe4e4 5916 case bp_jit_event:
628fe4e4 5917 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5918 break;
c906108c 5919 case bp_call_dummy:
53a5351d
JM
5920 /* Make sure the action is stop (silent or noisy),
5921 so infrun.c pops the dummy frame. */
aa7d318d 5922 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5923 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5924 break;
5925 case bp_std_terminate:
5926 /* Make sure the action is stop (silent or noisy),
5927 so infrun.c pops the dummy frame. */
aa7d318d 5928 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5929 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5930 break;
1042e4c0 5931 case bp_tracepoint:
7a697b8d 5932 case bp_fast_tracepoint:
0fb4aa4b 5933 case bp_static_tracepoint:
1042e4c0
SS
5934 /* Tracepoint hits should not be reported back to GDB, and
5935 if one got through somehow, it should have been filtered
5936 out already. */
5937 internal_error (__FILE__, __LINE__,
7a697b8d 5938 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5939 break;
5940 case bp_gnu_ifunc_resolver:
5941 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5942 this_action = BPSTAT_WHAT_SINGLE;
5943 break;
5944 case bp_gnu_ifunc_resolver_return:
5945 /* The breakpoint will be removed, execution will restart from the
5946 PC of the former breakpoint. */
5947 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5948 break;
e7e0cddf
SS
5949
5950 case bp_dprintf:
a11cfd87
HZ
5951 if (bs->stop)
5952 this_action = BPSTAT_WHAT_STOP_SILENT;
5953 else
5954 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5955 break;
5956
628fe4e4
JK
5957 default:
5958 internal_error (__FILE__, __LINE__,
5959 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5960 }
628fe4e4 5961
325fac50 5962 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5963 }
628fe4e4 5964
243a9253
PA
5965 return retval;
5966}
628fe4e4 5967
243a9253
PA
5968void
5969bpstat_run_callbacks (bpstat bs_head)
5970{
5971 bpstat bs;
628fe4e4 5972
0e30163f
JK
5973 for (bs = bs_head; bs != NULL; bs = bs->next)
5974 {
5975 struct breakpoint *b = bs->breakpoint_at;
5976
5977 if (b == NULL)
5978 continue;
5979 switch (b->type)
5980 {
243a9253
PA
5981 case bp_jit_event:
5982 handle_jit_event ();
5983 break;
0e30163f
JK
5984 case bp_gnu_ifunc_resolver:
5985 gnu_ifunc_resolver_stop (b);
5986 break;
5987 case bp_gnu_ifunc_resolver_return:
5988 gnu_ifunc_resolver_return_stop (b);
5989 break;
5990 }
5991 }
c906108c
SS
5992}
5993
5994/* Nonzero if we should step constantly (e.g. watchpoints on machines
5995 without hardware support). This isn't related to a specific bpstat,
5996 just to things like whether watchpoints are set. */
5997
c5aa993b 5998int
fba45db2 5999bpstat_should_step (void)
c906108c
SS
6000{
6001 struct breakpoint *b;
cc59ec59 6002
c906108c 6003 ALL_BREAKPOINTS (b)
717a8278 6004 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6005 return 1;
c906108c
SS
6006 return 0;
6007}
6008
67822962
PA
6009int
6010bpstat_causes_stop (bpstat bs)
6011{
6012 for (; bs != NULL; bs = bs->next)
6013 if (bs->stop)
6014 return 1;
6015
6016 return 0;
6017}
6018
c906108c 6019\f
c5aa993b 6020
170b53b2
UW
6021/* Compute a string of spaces suitable to indent the next line
6022 so it starts at the position corresponding to the table column
6023 named COL_NAME in the currently active table of UIOUT. */
6024
6025static char *
6026wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6027{
6028 static char wrap_indent[80];
6029 int i, total_width, width, align;
c5209615 6030 const char *text;
170b53b2
UW
6031
6032 total_width = 0;
112e8700 6033 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6034 {
6035 if (strcmp (text, col_name) == 0)
6036 {
6037 gdb_assert (total_width < sizeof wrap_indent);
6038 memset (wrap_indent, ' ', total_width);
6039 wrap_indent[total_width] = 0;
6040
6041 return wrap_indent;
6042 }
6043
6044 total_width += width + 1;
6045 }
6046
6047 return NULL;
6048}
6049
b775012e
LM
6050/* Determine if the locations of this breakpoint will have their conditions
6051 evaluated by the target, host or a mix of both. Returns the following:
6052
6053 "host": Host evals condition.
6054 "host or target": Host or Target evals condition.
6055 "target": Target evals condition.
6056*/
6057
6058static const char *
6059bp_condition_evaluator (struct breakpoint *b)
6060{
6061 struct bp_location *bl;
6062 char host_evals = 0;
6063 char target_evals = 0;
6064
6065 if (!b)
6066 return NULL;
6067
6068 if (!is_breakpoint (b))
6069 return NULL;
6070
6071 if (gdb_evaluates_breakpoint_condition_p ()
6072 || !target_supports_evaluation_of_breakpoint_conditions ())
6073 return condition_evaluation_host;
6074
6075 for (bl = b->loc; bl; bl = bl->next)
6076 {
6077 if (bl->cond_bytecode)
6078 target_evals++;
6079 else
6080 host_evals++;
6081 }
6082
6083 if (host_evals && target_evals)
6084 return condition_evaluation_both;
6085 else if (target_evals)
6086 return condition_evaluation_target;
6087 else
6088 return condition_evaluation_host;
6089}
6090
6091/* Determine the breakpoint location's condition evaluator. This is
6092 similar to bp_condition_evaluator, but for locations. */
6093
6094static const char *
6095bp_location_condition_evaluator (struct bp_location *bl)
6096{
6097 if (bl && !is_breakpoint (bl->owner))
6098 return NULL;
6099
6100 if (gdb_evaluates_breakpoint_condition_p ()
6101 || !target_supports_evaluation_of_breakpoint_conditions ())
6102 return condition_evaluation_host;
6103
6104 if (bl && bl->cond_bytecode)
6105 return condition_evaluation_target;
6106 else
6107 return condition_evaluation_host;
6108}
6109
859825b8
JK
6110/* Print the LOC location out of the list of B->LOC locations. */
6111
170b53b2
UW
6112static void
6113print_breakpoint_location (struct breakpoint *b,
6114 struct bp_location *loc)
0d381245 6115{
79a45e25 6116 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6117
6118 scoped_restore_current_program_space restore_pspace;
6c95b8df 6119
859825b8
JK
6120 if (loc != NULL && loc->shlib_disabled)
6121 loc = NULL;
6122
6c95b8df
PA
6123 if (loc != NULL)
6124 set_current_program_space (loc->pspace);
6125
56435ebe 6126 if (b->display_canonical)
d28cd78a 6127 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6128 else if (loc && loc->symtab)
0d381245
VP
6129 {
6130 struct symbol *sym
6131 = find_pc_sect_function (loc->address, loc->section);
6132 if (sym)
6133 {
112e8700
SM
6134 uiout->text ("in ");
6135 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6136 uiout->text (" ");
6137 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6138 uiout->text ("at ");
0d381245 6139 }
112e8700 6140 uiout->field_string ("file",
05cba821 6141 symtab_to_filename_for_display (loc->symtab));
112e8700 6142 uiout->text (":");
05cba821 6143
112e8700
SM
6144 if (uiout->is_mi_like_p ())
6145 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6146
112e8700 6147 uiout->field_int ("line", loc->line_number);
0d381245 6148 }
859825b8 6149 else if (loc)
0d381245 6150 {
d7e74731 6151 string_file stb;
170b53b2 6152
d7e74731 6153 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6154 demangle, "");
112e8700 6155 uiout->field_stream ("at", stb);
0d381245 6156 }
859825b8 6157 else
f00aae0f 6158 {
d28cd78a
TT
6159 uiout->field_string ("pending",
6160 event_location_to_string (b->location.get ()));
f00aae0f
KS
6161 /* If extra_string is available, it could be holding a condition
6162 or dprintf arguments. In either case, make sure it is printed,
6163 too, but only for non-MI streams. */
112e8700 6164 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6165 {
6166 if (b->type == bp_dprintf)
112e8700 6167 uiout->text (",");
f00aae0f 6168 else
112e8700
SM
6169 uiout->text (" ");
6170 uiout->text (b->extra_string);
f00aae0f
KS
6171 }
6172 }
6c95b8df 6173
b775012e
LM
6174 if (loc && is_breakpoint (b)
6175 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6176 && bp_condition_evaluator (b) == condition_evaluation_both)
6177 {
112e8700
SM
6178 uiout->text (" (");
6179 uiout->field_string ("evaluated-by",
b775012e 6180 bp_location_condition_evaluator (loc));
112e8700 6181 uiout->text (")");
b775012e 6182 }
0d381245
VP
6183}
6184
269b11a2
PA
6185static const char *
6186bptype_string (enum bptype type)
c906108c 6187{
c4093a6a
JM
6188 struct ep_type_description
6189 {
6190 enum bptype type;
a121b7c1 6191 const char *description;
c4093a6a
JM
6192 };
6193 static struct ep_type_description bptypes[] =
c906108c 6194 {
c5aa993b
JM
6195 {bp_none, "?deleted?"},
6196 {bp_breakpoint, "breakpoint"},
c906108c 6197 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6198 {bp_single_step, "sw single-step"},
c5aa993b
JM
6199 {bp_until, "until"},
6200 {bp_finish, "finish"},
6201 {bp_watchpoint, "watchpoint"},
c906108c 6202 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6203 {bp_read_watchpoint, "read watchpoint"},
6204 {bp_access_watchpoint, "acc watchpoint"},
6205 {bp_longjmp, "longjmp"},
6206 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6207 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6208 {bp_exception, "exception"},
6209 {bp_exception_resume, "exception resume"},
c5aa993b 6210 {bp_step_resume, "step resume"},
2c03e5be 6211 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6212 {bp_watchpoint_scope, "watchpoint scope"},
6213 {bp_call_dummy, "call dummy"},
aa7d318d 6214 {bp_std_terminate, "std::terminate"},
c5aa993b 6215 {bp_shlib_event, "shlib events"},
c4093a6a 6216 {bp_thread_event, "thread events"},
1900040c 6217 {bp_overlay_event, "overlay events"},
0fd8e87f 6218 {bp_longjmp_master, "longjmp master"},
aa7d318d 6219 {bp_std_terminate_master, "std::terminate master"},
186c406b 6220 {bp_exception_master, "exception master"},
ce78b96d 6221 {bp_catchpoint, "catchpoint"},
1042e4c0 6222 {bp_tracepoint, "tracepoint"},
7a697b8d 6223 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6224 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6225 {bp_dprintf, "dprintf"},
4efc6507 6226 {bp_jit_event, "jit events"},
0e30163f
JK
6227 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6228 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6229 };
269b11a2
PA
6230
6231 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6232 || ((int) type != bptypes[(int) type].type))
6233 internal_error (__FILE__, __LINE__,
6234 _("bptypes table does not describe type #%d."),
6235 (int) type);
6236
6237 return bptypes[(int) type].description;
6238}
6239
998580f1
MK
6240/* For MI, output a field named 'thread-groups' with a list as the value.
6241 For CLI, prefix the list with the string 'inf'. */
6242
6243static void
6244output_thread_groups (struct ui_out *uiout,
6245 const char *field_name,
6246 VEC(int) *inf_num,
6247 int mi_only)
6248{
112e8700 6249 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6250 int inf;
6251 int i;
6252
6253 /* For backward compatibility, don't display inferiors in CLI unless
6254 there are several. Always display them for MI. */
6255 if (!is_mi && mi_only)
6256 return;
6257
10f489e5 6258 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6259
998580f1
MK
6260 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6261 {
6262 if (is_mi)
6263 {
6264 char mi_group[10];
6265
6266 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6267 uiout->field_string (NULL, mi_group);
998580f1
MK
6268 }
6269 else
6270 {
6271 if (i == 0)
112e8700 6272 uiout->text (" inf ");
998580f1 6273 else
112e8700 6274 uiout->text (", ");
998580f1 6275
112e8700 6276 uiout->text (plongest (inf));
998580f1
MK
6277 }
6278 }
998580f1
MK
6279}
6280
269b11a2
PA
6281/* Print B to gdb_stdout. */
6282
6283static void
6284print_one_breakpoint_location (struct breakpoint *b,
6285 struct bp_location *loc,
6286 int loc_number,
6287 struct bp_location **last_loc,
269b11a2
PA
6288 int allflag)
6289{
6290 struct command_line *l;
c2c6d25f 6291 static char bpenables[] = "nynny";
c906108c 6292
79a45e25 6293 struct ui_out *uiout = current_uiout;
0d381245
VP
6294 int header_of_multiple = 0;
6295 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6296 struct value_print_options opts;
6297
6298 get_user_print_options (&opts);
0d381245
VP
6299
6300 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6301 /* See comment in print_one_breakpoint concerning treatment of
6302 breakpoints with single disabled location. */
0d381245
VP
6303 if (loc == NULL
6304 && (b->loc != NULL
6305 && (b->loc->next != NULL || !b->loc->enabled)))
6306 header_of_multiple = 1;
6307 if (loc == NULL)
6308 loc = b->loc;
6309
c4093a6a
JM
6310 annotate_record ();
6311
6312 /* 1 */
6313 annotate_field (0);
0d381245
VP
6314 if (part_of_multiple)
6315 {
6316 char *formatted;
0c6773c1 6317 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6318 uiout->field_string ("number", formatted);
0d381245
VP
6319 xfree (formatted);
6320 }
6321 else
6322 {
112e8700 6323 uiout->field_int ("number", b->number);
0d381245 6324 }
c4093a6a
JM
6325
6326 /* 2 */
6327 annotate_field (1);
0d381245 6328 if (part_of_multiple)
112e8700 6329 uiout->field_skip ("type");
269b11a2 6330 else
112e8700 6331 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6332
6333 /* 3 */
6334 annotate_field (2);
0d381245 6335 if (part_of_multiple)
112e8700 6336 uiout->field_skip ("disp");
0d381245 6337 else
112e8700 6338 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6339
c4093a6a
JM
6340
6341 /* 4 */
6342 annotate_field (3);
0d381245 6343 if (part_of_multiple)
112e8700 6344 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6345 else
112e8700
SM
6346 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6347 uiout->spaces (2);
0d381245 6348
c4093a6a
JM
6349
6350 /* 5 and 6 */
3086aeae 6351 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6352 {
4a64f543
MS
6353 /* Although the print_one can possibly print all locations,
6354 calling it here is not likely to get any nice result. So,
6355 make sure there's just one location. */
0d381245 6356 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6357 b->ops->print_one (b, last_loc);
0d381245 6358 }
3086aeae
DJ
6359 else
6360 switch (b->type)
6361 {
6362 case bp_none:
6363 internal_error (__FILE__, __LINE__,
e2e0b3e5 6364 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6365 break;
c906108c 6366
3086aeae
DJ
6367 case bp_watchpoint:
6368 case bp_hardware_watchpoint:
6369 case bp_read_watchpoint:
6370 case bp_access_watchpoint:
3a5c3e22
PA
6371 {
6372 struct watchpoint *w = (struct watchpoint *) b;
6373
6374 /* Field 4, the address, is omitted (which makes the columns
6375 not line up too nicely with the headers, but the effect
6376 is relatively readable). */
6377 if (opts.addressprint)
112e8700 6378 uiout->field_skip ("addr");
3a5c3e22 6379 annotate_field (5);
112e8700 6380 uiout->field_string ("what", w->exp_string);
3a5c3e22 6381 }
3086aeae
DJ
6382 break;
6383
3086aeae
DJ
6384 case bp_breakpoint:
6385 case bp_hardware_breakpoint:
7c16b83e 6386 case bp_single_step:
3086aeae
DJ
6387 case bp_until:
6388 case bp_finish:
6389 case bp_longjmp:
6390 case bp_longjmp_resume:
e2e4d78b 6391 case bp_longjmp_call_dummy:
186c406b
TT
6392 case bp_exception:
6393 case bp_exception_resume:
3086aeae 6394 case bp_step_resume:
2c03e5be 6395 case bp_hp_step_resume:
3086aeae
DJ
6396 case bp_watchpoint_scope:
6397 case bp_call_dummy:
aa7d318d 6398 case bp_std_terminate:
3086aeae
DJ
6399 case bp_shlib_event:
6400 case bp_thread_event:
6401 case bp_overlay_event:
0fd8e87f 6402 case bp_longjmp_master:
aa7d318d 6403 case bp_std_terminate_master:
186c406b 6404 case bp_exception_master:
1042e4c0 6405 case bp_tracepoint:
7a697b8d 6406 case bp_fast_tracepoint:
0fb4aa4b 6407 case bp_static_tracepoint:
e7e0cddf 6408 case bp_dprintf:
4efc6507 6409 case bp_jit_event:
0e30163f
JK
6410 case bp_gnu_ifunc_resolver:
6411 case bp_gnu_ifunc_resolver_return:
79a45b7d 6412 if (opts.addressprint)
3086aeae
DJ
6413 {
6414 annotate_field (4);
54e52265 6415 if (header_of_multiple)
112e8700 6416 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6417 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6418 uiout->field_string ("addr", "<PENDING>");
0101ce28 6419 else
112e8700 6420 uiout->field_core_addr ("addr",
5af949e3 6421 loc->gdbarch, loc->address);
3086aeae
DJ
6422 }
6423 annotate_field (5);
0d381245 6424 if (!header_of_multiple)
170b53b2 6425 print_breakpoint_location (b, loc);
0d381245 6426 if (b->loc)
a6d9a66e 6427 *last_loc = b->loc;
3086aeae
DJ
6428 break;
6429 }
c906108c 6430
6c95b8df 6431
998580f1 6432 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6433 {
6434 struct inferior *inf;
998580f1
MK
6435 VEC(int) *inf_num = NULL;
6436 int mi_only = 1;
6c95b8df 6437
998580f1 6438 ALL_INFERIORS (inf)
6c95b8df
PA
6439 {
6440 if (inf->pspace == loc->pspace)
998580f1 6441 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6442 }
998580f1
MK
6443
6444 /* For backward compatibility, don't display inferiors in CLI unless
6445 there are several. Always display for MI. */
6446 if (allflag
6447 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6448 && (number_of_program_spaces () > 1
6449 || number_of_inferiors () > 1)
6450 /* LOC is for existing B, it cannot be in
6451 moribund_locations and thus having NULL OWNER. */
6452 && loc->owner->type != bp_catchpoint))
6453 mi_only = 0;
6454 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6455 VEC_free (int, inf_num);
6c95b8df
PA
6456 }
6457
4a306c9a 6458 if (!part_of_multiple)
c4093a6a 6459 {
4a306c9a
JB
6460 if (b->thread != -1)
6461 {
6462 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6463 "stop only in" line a little further down. */
112e8700
SM
6464 uiout->text (" thread ");
6465 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6466 }
6467 else if (b->task != 0)
6468 {
112e8700
SM
6469 uiout->text (" task ");
6470 uiout->field_int ("task", b->task);
4a306c9a 6471 }
c4093a6a 6472 }
f1310107 6473
112e8700 6474 uiout->text ("\n");
f1310107 6475
348d480f 6476 if (!part_of_multiple)
f1310107
TJB
6477 b->ops->print_one_detail (b, uiout);
6478
0d381245 6479 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6480 {
6481 annotate_field (6);
112e8700 6482 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6483 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6484 the frame ID. */
112e8700 6485 uiout->field_core_addr ("frame",
5af949e3 6486 b->gdbarch, b->frame_id.stack_addr);
112e8700 6487 uiout->text ("\n");
c4093a6a
JM
6488 }
6489
28010a5d 6490 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6491 {
6492 annotate_field (7);
d77f58be 6493 if (is_tracepoint (b))
112e8700 6494 uiout->text ("\ttrace only if ");
1042e4c0 6495 else
112e8700
SM
6496 uiout->text ("\tstop only if ");
6497 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6498
6499 /* Print whether the target is doing the breakpoint's condition
6500 evaluation. If GDB is doing the evaluation, don't print anything. */
6501 if (is_breakpoint (b)
6502 && breakpoint_condition_evaluation_mode ()
6503 == condition_evaluation_target)
6504 {
112e8700
SM
6505 uiout->text (" (");
6506 uiout->field_string ("evaluated-by",
b775012e 6507 bp_condition_evaluator (b));
112e8700 6508 uiout->text (" evals)");
b775012e 6509 }
112e8700 6510 uiout->text ("\n");
0101ce28
JJ
6511 }
6512
0d381245 6513 if (!part_of_multiple && b->thread != -1)
c4093a6a 6514 {
4a64f543 6515 /* FIXME should make an annotation for this. */
112e8700
SM
6516 uiout->text ("\tstop only in thread ");
6517 if (uiout->is_mi_like_p ())
6518 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6519 else
6520 {
6521 struct thread_info *thr = find_thread_global_id (b->thread);
6522
112e8700 6523 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6524 }
112e8700 6525 uiout->text ("\n");
c4093a6a
JM
6526 }
6527
556ec64d
YQ
6528 if (!part_of_multiple)
6529 {
6530 if (b->hit_count)
31f56a27
YQ
6531 {
6532 /* FIXME should make an annotation for this. */
6533 if (is_catchpoint (b))
112e8700 6534 uiout->text ("\tcatchpoint");
31f56a27 6535 else if (is_tracepoint (b))
112e8700 6536 uiout->text ("\ttracepoint");
31f56a27 6537 else
112e8700
SM
6538 uiout->text ("\tbreakpoint");
6539 uiout->text (" already hit ");
6540 uiout->field_int ("times", b->hit_count);
31f56a27 6541 if (b->hit_count == 1)
112e8700 6542 uiout->text (" time\n");
31f56a27 6543 else
112e8700 6544 uiout->text (" times\n");
31f56a27 6545 }
556ec64d
YQ
6546 else
6547 {
31f56a27 6548 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6549 if (uiout->is_mi_like_p ())
6550 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6551 }
6552 }
8b93c638 6553
0d381245 6554 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6555 {
6556 annotate_field (8);
112e8700
SM
6557 uiout->text ("\tignore next ");
6558 uiout->field_int ("ignore", b->ignore_count);
6559 uiout->text (" hits\n");
c4093a6a 6560 }
059fb39f 6561
816338b5
SS
6562 /* Note that an enable count of 1 corresponds to "enable once"
6563 behavior, which is reported by the combination of enablement and
6564 disposition, so we don't need to mention it here. */
6565 if (!part_of_multiple && b->enable_count > 1)
6566 {
6567 annotate_field (8);
112e8700 6568 uiout->text ("\tdisable after ");
816338b5
SS
6569 /* Tweak the wording to clarify that ignore and enable counts
6570 are distinct, and have additive effect. */
6571 if (b->ignore_count)
112e8700 6572 uiout->text ("additional ");
816338b5 6573 else
112e8700
SM
6574 uiout->text ("next ");
6575 uiout->field_int ("enable", b->enable_count);
6576 uiout->text (" hits\n");
816338b5
SS
6577 }
6578
f196051f
SS
6579 if (!part_of_multiple && is_tracepoint (b))
6580 {
6581 struct tracepoint *tp = (struct tracepoint *) b;
6582
6583 if (tp->traceframe_usage)
6584 {
112e8700
SM
6585 uiout->text ("\ttrace buffer usage ");
6586 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6587 uiout->text (" bytes\n");
f196051f
SS
6588 }
6589 }
d3ce09f5 6590
9add0f1b 6591 l = b->commands ? b->commands->commands : NULL;
059fb39f 6592 if (!part_of_multiple && l)
c4093a6a
JM
6593 {
6594 annotate_field (9);
2e783024 6595 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6596 print_command_lines (uiout, l, 4);
c4093a6a 6597 }
d24317b4 6598
d9b3f62e 6599 if (is_tracepoint (b))
1042e4c0 6600 {
d9b3f62e
PA
6601 struct tracepoint *t = (struct tracepoint *) b;
6602
6603 if (!part_of_multiple && t->pass_count)
6604 {
6605 annotate_field (10);
112e8700
SM
6606 uiout->text ("\tpass count ");
6607 uiout->field_int ("pass", t->pass_count);
6608 uiout->text (" \n");
d9b3f62e 6609 }
f2a8bc8a
YQ
6610
6611 /* Don't display it when tracepoint or tracepoint location is
6612 pending. */
6613 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6614 {
6615 annotate_field (11);
6616
112e8700
SM
6617 if (uiout->is_mi_like_p ())
6618 uiout->field_string ("installed",
f2a8bc8a
YQ
6619 loc->inserted ? "y" : "n");
6620 else
6621 {
6622 if (loc->inserted)
112e8700 6623 uiout->text ("\t");
f2a8bc8a 6624 else
112e8700
SM
6625 uiout->text ("\tnot ");
6626 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6627 }
6628 }
1042e4c0
SS
6629 }
6630
112e8700 6631 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6632 {
3a5c3e22
PA
6633 if (is_watchpoint (b))
6634 {
6635 struct watchpoint *w = (struct watchpoint *) b;
6636
112e8700 6637 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6638 }
f00aae0f 6639 else if (b->location != NULL
d28cd78a 6640 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6641 uiout->field_string ("original-location",
d28cd78a 6642 event_location_to_string (b->location.get ()));
d24317b4 6643 }
c4093a6a 6644}
c5aa993b 6645
0d381245
VP
6646static void
6647print_one_breakpoint (struct breakpoint *b,
4a64f543 6648 struct bp_location **last_loc,
6c95b8df 6649 int allflag)
0d381245 6650{
79a45e25 6651 struct ui_out *uiout = current_uiout;
8d3788bd 6652
2e783024
TT
6653 {
6654 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6655
2e783024
TT
6656 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6657 }
0d381245
VP
6658
6659 /* If this breakpoint has custom print function,
6660 it's already printed. Otherwise, print individual
6661 locations, if any. */
6662 if (b->ops == NULL || b->ops->print_one == NULL)
6663 {
4a64f543
MS
6664 /* If breakpoint has a single location that is disabled, we
6665 print it as if it had several locations, since otherwise it's
6666 hard to represent "breakpoint enabled, location disabled"
6667 situation.
6668
6669 Note that while hardware watchpoints have several locations
a3be7890 6670 internally, that's not a property exposed to user. */
0d381245 6671 if (b->loc
a5606eee 6672 && !is_hardware_watchpoint (b)
8d3788bd 6673 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6674 {
6675 struct bp_location *loc;
6676 int n = 1;
8d3788bd 6677
0d381245 6678 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6679 {
2e783024 6680 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6681 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6682 }
0d381245
VP
6683 }
6684 }
6685}
6686
a6d9a66e
UW
6687static int
6688breakpoint_address_bits (struct breakpoint *b)
6689{
6690 int print_address_bits = 0;
6691 struct bp_location *loc;
6692
c6d81124
PA
6693 /* Software watchpoints that aren't watching memory don't have an
6694 address to print. */
6695 if (is_no_memory_software_watchpoint (b))
6696 return 0;
6697
a6d9a66e
UW
6698 for (loc = b->loc; loc; loc = loc->next)
6699 {
c7437ca6
PA
6700 int addr_bit;
6701
c7437ca6 6702 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6703 if (addr_bit > print_address_bits)
6704 print_address_bits = addr_bit;
6705 }
6706
6707 return print_address_bits;
6708}
0d381245 6709
c4093a6a
JM
6710struct captured_breakpoint_query_args
6711 {
6712 int bnum;
6713 };
c5aa993b 6714
c4093a6a 6715static int
2b65245e 6716do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6717{
9a3c8263
SM
6718 struct captured_breakpoint_query_args *args
6719 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6720 struct breakpoint *b;
a6d9a66e 6721 struct bp_location *dummy_loc = NULL;
cc59ec59 6722
c4093a6a
JM
6723 ALL_BREAKPOINTS (b)
6724 {
6725 if (args->bnum == b->number)
c5aa993b 6726 {
12c5a436 6727 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6728 return GDB_RC_OK;
c5aa993b 6729 }
c4093a6a
JM
6730 }
6731 return GDB_RC_NONE;
6732}
c5aa993b 6733
c4093a6a 6734enum gdb_rc
4a64f543
MS
6735gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6736 char **error_message)
c4093a6a
JM
6737{
6738 struct captured_breakpoint_query_args args;
cc59ec59 6739
c4093a6a
JM
6740 args.bnum = bnum;
6741 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6742 an error. */
b0b13bb4
DJ
6743 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6744 error_message, RETURN_MASK_ALL) < 0)
6745 return GDB_RC_FAIL;
6746 else
6747 return GDB_RC_OK;
c4093a6a 6748}
c5aa993b 6749
09d682a4
TT
6750/* Return true if this breakpoint was set by the user, false if it is
6751 internal or momentary. */
6752
6753int
6754user_breakpoint_p (struct breakpoint *b)
6755{
46c6471b 6756 return b->number > 0;
09d682a4
TT
6757}
6758
93daf339
TT
6759/* See breakpoint.h. */
6760
6761int
6762pending_breakpoint_p (struct breakpoint *b)
6763{
6764 return b->loc == NULL;
6765}
6766
7f3b0473 6767/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6768 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6769 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6770 FILTER is non-NULL, call it on each breakpoint and only include the
6771 ones for which it returns non-zero. Return the total number of
6772 breakpoints listed. */
c906108c 6773
d77f58be 6774static int
e5a67952 6775breakpoint_1 (char *args, int allflag,
4a64f543 6776 int (*filter) (const struct breakpoint *))
c4093a6a 6777{
52f0bd74 6778 struct breakpoint *b;
a6d9a66e 6779 struct bp_location *last_loc = NULL;
7f3b0473 6780 int nr_printable_breakpoints;
3b31d625 6781 struct cleanup *bkpttbl_chain;
79a45b7d 6782 struct value_print_options opts;
a6d9a66e 6783 int print_address_bits = 0;
269b11a2 6784 int print_type_col_width = 14;
79a45e25 6785 struct ui_out *uiout = current_uiout;
269b11a2 6786
79a45b7d
TT
6787 get_user_print_options (&opts);
6788
4a64f543
MS
6789 /* Compute the number of rows in the table, as well as the size
6790 required for address fields. */
7f3b0473
AC
6791 nr_printable_breakpoints = 0;
6792 ALL_BREAKPOINTS (b)
e5a67952
MS
6793 {
6794 /* If we have a filter, only list the breakpoints it accepts. */
6795 if (filter && !filter (b))
6796 continue;
6797
6798 /* If we have an "args" string, it is a list of breakpoints to
6799 accept. Skip the others. */
6800 if (args != NULL && *args != '\0')
6801 {
6802 if (allflag && parse_and_eval_long (args) != b->number)
6803 continue;
6804 if (!allflag && !number_is_in_list (args, b->number))
6805 continue;
6806 }
269b11a2 6807
e5a67952
MS
6808 if (allflag || user_breakpoint_p (b))
6809 {
6810 int addr_bit, type_len;
a6d9a66e 6811
e5a67952
MS
6812 addr_bit = breakpoint_address_bits (b);
6813 if (addr_bit > print_address_bits)
6814 print_address_bits = addr_bit;
269b11a2 6815
e5a67952
MS
6816 type_len = strlen (bptype_string (b->type));
6817 if (type_len > print_type_col_width)
6818 print_type_col_width = type_len;
6819
6820 nr_printable_breakpoints++;
6821 }
6822 }
7f3b0473 6823
79a45b7d 6824 if (opts.addressprint)
3b31d625 6825 bkpttbl_chain
3e43a32a
MS
6826 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6827 nr_printable_breakpoints,
3b31d625 6828 "BreakpointTable");
8b93c638 6829 else
3b31d625 6830 bkpttbl_chain
3e43a32a
MS
6831 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6832 nr_printable_breakpoints,
3b31d625 6833 "BreakpointTable");
8b93c638 6834
7f3b0473 6835 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6836 annotate_breakpoints_headers ();
6837 if (nr_printable_breakpoints > 0)
6838 annotate_field (0);
112e8700 6839 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6840 if (nr_printable_breakpoints > 0)
6841 annotate_field (1);
112e8700 6842 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
d7faa9e7
AC
6843 if (nr_printable_breakpoints > 0)
6844 annotate_field (2);
112e8700 6845 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6846 if (nr_printable_breakpoints > 0)
6847 annotate_field (3);
112e8700 6848 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6849 if (opts.addressprint)
e5a67952
MS
6850 {
6851 if (nr_printable_breakpoints > 0)
6852 annotate_field (4);
6853 if (print_address_bits <= 32)
112e8700 6854 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
e5a67952 6855 else
112e8700 6856 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
e5a67952 6857 }
d7faa9e7
AC
6858 if (nr_printable_breakpoints > 0)
6859 annotate_field (5);
112e8700
SM
6860 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6861 uiout->table_body ();
d7faa9e7
AC
6862 if (nr_printable_breakpoints > 0)
6863 annotate_breakpoints_table ();
7f3b0473 6864
c4093a6a 6865 ALL_BREAKPOINTS (b)
e5a67952
MS
6866 {
6867 QUIT;
6868 /* If we have a filter, only list the breakpoints it accepts. */
6869 if (filter && !filter (b))
6870 continue;
6871
6872 /* If we have an "args" string, it is a list of breakpoints to
6873 accept. Skip the others. */
6874
6875 if (args != NULL && *args != '\0')
6876 {
6877 if (allflag) /* maintenance info breakpoint */
6878 {
6879 if (parse_and_eval_long (args) != b->number)
6880 continue;
6881 }
6882 else /* all others */
6883 {
6884 if (!number_is_in_list (args, b->number))
6885 continue;
6886 }
6887 }
6888 /* We only print out user settable breakpoints unless the
6889 allflag is set. */
6890 if (allflag || user_breakpoint_p (b))
12c5a436 6891 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6892 }
6893
3b31d625 6894 do_cleanups (bkpttbl_chain);
698384cd 6895
7f3b0473 6896 if (nr_printable_breakpoints == 0)
c906108c 6897 {
4a64f543
MS
6898 /* If there's a filter, let the caller decide how to report
6899 empty list. */
d77f58be
SS
6900 if (!filter)
6901 {
e5a67952 6902 if (args == NULL || *args == '\0')
112e8700 6903 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6904 else
112e8700 6905 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6906 args);
d77f58be 6907 }
c906108c
SS
6908 }
6909 else
c4093a6a 6910 {
a6d9a66e
UW
6911 if (last_loc && !server_command)
6912 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6913 }
c906108c 6914
4a64f543 6915 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6916 there have been breakpoints? */
c906108c 6917 annotate_breakpoints_table_end ();
d77f58be
SS
6918
6919 return nr_printable_breakpoints;
c906108c
SS
6920}
6921
ad443146
SS
6922/* Display the value of default-collect in a way that is generally
6923 compatible with the breakpoint list. */
6924
6925static void
6926default_collect_info (void)
6927{
79a45e25
PA
6928 struct ui_out *uiout = current_uiout;
6929
ad443146
SS
6930 /* If it has no value (which is frequently the case), say nothing; a
6931 message like "No default-collect." gets in user's face when it's
6932 not wanted. */
6933 if (!*default_collect)
6934 return;
6935
6936 /* The following phrase lines up nicely with per-tracepoint collect
6937 actions. */
112e8700
SM
6938 uiout->text ("default collect ");
6939 uiout->field_string ("default-collect", default_collect);
6940 uiout->text (" \n");
ad443146
SS
6941}
6942
c906108c 6943static void
e5a67952 6944breakpoints_info (char *args, int from_tty)
c906108c 6945{
e5a67952 6946 breakpoint_1 (args, 0, NULL);
ad443146
SS
6947
6948 default_collect_info ();
d77f58be
SS
6949}
6950
6951static void
e5a67952 6952watchpoints_info (char *args, int from_tty)
d77f58be 6953{
e5a67952 6954 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6955 struct ui_out *uiout = current_uiout;
d77f58be
SS
6956
6957 if (num_printed == 0)
6958 {
e5a67952 6959 if (args == NULL || *args == '\0')
112e8700 6960 uiout->message ("No watchpoints.\n");
d77f58be 6961 else
112e8700 6962 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6963 }
c906108c
SS
6964}
6965
7a292a7a 6966static void
e5a67952 6967maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6968{
e5a67952 6969 breakpoint_1 (args, 1, NULL);
ad443146
SS
6970
6971 default_collect_info ();
c906108c
SS
6972}
6973
0d381245 6974static int
714835d5 6975breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6976 struct program_space *pspace,
714835d5 6977 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6978{
6979 struct bp_location *bl = b->loc;
cc59ec59 6980
0d381245
VP
6981 for (; bl; bl = bl->next)
6982 {
6c95b8df
PA
6983 if (bl->pspace == pspace
6984 && bl->address == pc
0d381245
VP
6985 && (!overlay_debugging || bl->section == section))
6986 return 1;
6987 }
6988 return 0;
6989}
6990
672f9b60 6991/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6992 concerns with logical breakpoints, so we match program spaces, not
6993 address spaces. */
c906108c
SS
6994
6995static void
6c95b8df
PA
6996describe_other_breakpoints (struct gdbarch *gdbarch,
6997 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6998 struct obj_section *section, int thread)
c906108c 6999{
52f0bd74
AC
7000 int others = 0;
7001 struct breakpoint *b;
c906108c
SS
7002
7003 ALL_BREAKPOINTS (b)
672f9b60
KP
7004 others += (user_breakpoint_p (b)
7005 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7006 if (others > 0)
7007 {
a3f17187
AC
7008 if (others == 1)
7009 printf_filtered (_("Note: breakpoint "));
7010 else /* if (others == ???) */
7011 printf_filtered (_("Note: breakpoints "));
c906108c 7012 ALL_BREAKPOINTS (b)
672f9b60 7013 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7014 {
7015 others--;
7016 printf_filtered ("%d", b->number);
7017 if (b->thread == -1 && thread != -1)
7018 printf_filtered (" (all threads)");
7019 else if (b->thread != -1)
7020 printf_filtered (" (thread %d)", b->thread);
7021 printf_filtered ("%s%s ",
059fb39f 7022 ((b->enable_state == bp_disabled
f8eba3c6 7023 || b->enable_state == bp_call_disabled)
0d381245 7024 ? " (disabled)"
0d381245
VP
7025 : ""),
7026 (others > 1) ? ","
7027 : ((others == 1) ? " and" : ""));
7028 }
a3f17187 7029 printf_filtered (_("also set at pc "));
5af949e3 7030 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7031 printf_filtered (".\n");
7032 }
7033}
7034\f
c906108c 7035
e4f237da 7036/* Return true iff it is meaningful to use the address member of
244558af
LM
7037 BPT locations. For some breakpoint types, the locations' address members
7038 are irrelevant and it makes no sense to attempt to compare them to other
7039 addresses (or use them for any other purpose either).
e4f237da 7040
4a64f543 7041 More specifically, each of the following breakpoint types will
244558af 7042 always have a zero valued location address and we don't want to mark
4a64f543 7043 breakpoints of any of these types to be a duplicate of an actual
244558af 7044 breakpoint location at address zero:
e4f237da
KB
7045
7046 bp_watchpoint
2d134ed3
PA
7047 bp_catchpoint
7048
7049*/
e4f237da
KB
7050
7051static int
7052breakpoint_address_is_meaningful (struct breakpoint *bpt)
7053{
7054 enum bptype type = bpt->type;
7055
2d134ed3
PA
7056 return (type != bp_watchpoint && type != bp_catchpoint);
7057}
7058
7059/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7060 true if LOC1 and LOC2 represent the same watchpoint location. */
7061
7062static int
4a64f543
MS
7063watchpoint_locations_match (struct bp_location *loc1,
7064 struct bp_location *loc2)
2d134ed3 7065{
3a5c3e22
PA
7066 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7067 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7068
7069 /* Both of them must exist. */
7070 gdb_assert (w1 != NULL);
7071 gdb_assert (w2 != NULL);
2bdf28a0 7072
4a64f543
MS
7073 /* If the target can evaluate the condition expression in hardware,
7074 then we we need to insert both watchpoints even if they are at
7075 the same place. Otherwise the watchpoint will only trigger when
7076 the condition of whichever watchpoint was inserted evaluates to
7077 true, not giving a chance for GDB to check the condition of the
7078 other watchpoint. */
3a5c3e22 7079 if ((w1->cond_exp
4a64f543
MS
7080 && target_can_accel_watchpoint_condition (loc1->address,
7081 loc1->length,
0cf6dd15 7082 loc1->watchpoint_type,
4d01a485 7083 w1->cond_exp.get ()))
3a5c3e22 7084 || (w2->cond_exp
4a64f543
MS
7085 && target_can_accel_watchpoint_condition (loc2->address,
7086 loc2->length,
0cf6dd15 7087 loc2->watchpoint_type,
4d01a485 7088 w2->cond_exp.get ())))
0cf6dd15
TJB
7089 return 0;
7090
85d721b8
PA
7091 /* Note that this checks the owner's type, not the location's. In
7092 case the target does not support read watchpoints, but does
7093 support access watchpoints, we'll have bp_read_watchpoint
7094 watchpoints with hw_access locations. Those should be considered
7095 duplicates of hw_read locations. The hw_read locations will
7096 become hw_access locations later. */
2d134ed3
PA
7097 return (loc1->owner->type == loc2->owner->type
7098 && loc1->pspace->aspace == loc2->pspace->aspace
7099 && loc1->address == loc2->address
7100 && loc1->length == loc2->length);
e4f237da
KB
7101}
7102
31e77af2 7103/* See breakpoint.h. */
6c95b8df 7104
31e77af2 7105int
6c95b8df
PA
7106breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7107 struct address_space *aspace2, CORE_ADDR addr2)
7108{
f5656ead 7109 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7110 || aspace1 == aspace2)
7111 && addr1 == addr2);
7112}
7113
f1310107
TJB
7114/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7115 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7116 matches ASPACE2. On targets that have global breakpoints, the address
7117 space doesn't really matter. */
7118
7119static int
7120breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7121 int len1, struct address_space *aspace2,
7122 CORE_ADDR addr2)
7123{
f5656ead 7124 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7125 || aspace1 == aspace2)
7126 && addr2 >= addr1 && addr2 < addr1 + len1);
7127}
7128
7129/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7130 a ranged breakpoint. In most targets, a match happens only if ASPACE
7131 matches the breakpoint's address space. On targets that have global
7132 breakpoints, the address space doesn't really matter. */
7133
7134static int
7135breakpoint_location_address_match (struct bp_location *bl,
7136 struct address_space *aspace,
7137 CORE_ADDR addr)
7138{
7139 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7140 aspace, addr)
7141 || (bl->length
7142 && breakpoint_address_match_range (bl->pspace->aspace,
7143 bl->address, bl->length,
7144 aspace, addr)));
7145}
7146
d35ae833
PA
7147/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7148 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7149 match happens only if ASPACE matches the breakpoint's address
7150 space. On targets that have global breakpoints, the address space
7151 doesn't really matter. */
7152
7153static int
7154breakpoint_location_address_range_overlap (struct bp_location *bl,
7155 struct address_space *aspace,
7156 CORE_ADDR addr, int len)
7157{
7158 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7159 || bl->pspace->aspace == aspace)
7160 {
7161 int bl_len = bl->length != 0 ? bl->length : 1;
7162
7163 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7164 return 1;
7165 }
7166 return 0;
7167}
7168
1e4d1764
YQ
7169/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7170 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7171 true, otherwise returns false. */
7172
7173static int
7174tracepoint_locations_match (struct bp_location *loc1,
7175 struct bp_location *loc2)
7176{
7177 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7178 /* Since tracepoint locations are never duplicated with others', tracepoint
7179 locations at the same address of different tracepoints are regarded as
7180 different locations. */
7181 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7182 else
7183 return 0;
7184}
7185
2d134ed3
PA
7186/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7187 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7188 represent the same location. */
7189
7190static int
4a64f543
MS
7191breakpoint_locations_match (struct bp_location *loc1,
7192 struct bp_location *loc2)
2d134ed3 7193{
2bdf28a0
JK
7194 int hw_point1, hw_point2;
7195
7196 /* Both of them must not be in moribund_locations. */
7197 gdb_assert (loc1->owner != NULL);
7198 gdb_assert (loc2->owner != NULL);
7199
7200 hw_point1 = is_hardware_watchpoint (loc1->owner);
7201 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7202
7203 if (hw_point1 != hw_point2)
7204 return 0;
7205 else if (hw_point1)
7206 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7207 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7208 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7209 else
f1310107
TJB
7210 /* We compare bp_location.length in order to cover ranged breakpoints. */
7211 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7212 loc2->pspace->aspace, loc2->address)
7213 && loc1->length == loc2->length);
2d134ed3
PA
7214}
7215
76897487
KB
7216static void
7217breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7218 int bnum, int have_bnum)
7219{
f63fbe86
MS
7220 /* The longest string possibly returned by hex_string_custom
7221 is 50 chars. These must be at least that big for safety. */
7222 char astr1[64];
7223 char astr2[64];
76897487 7224
bb599908
PH
7225 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7226 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7227 if (have_bnum)
8a3fe4f8 7228 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7229 bnum, astr1, astr2);
7230 else
8a3fe4f8 7231 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7232}
7233
4a64f543
MS
7234/* Adjust a breakpoint's address to account for architectural
7235 constraints on breakpoint placement. Return the adjusted address.
7236 Note: Very few targets require this kind of adjustment. For most
7237 targets, this function is simply the identity function. */
76897487
KB
7238
7239static CORE_ADDR
a6d9a66e
UW
7240adjust_breakpoint_address (struct gdbarch *gdbarch,
7241 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7242{
a6d9a66e 7243 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7244 {
7245 /* Very few targets need any kind of breakpoint adjustment. */
7246 return bpaddr;
7247 }
88f7da05
KB
7248 else if (bptype == bp_watchpoint
7249 || bptype == bp_hardware_watchpoint
7250 || bptype == bp_read_watchpoint
7251 || bptype == bp_access_watchpoint
fe798b75 7252 || bptype == bp_catchpoint)
88f7da05
KB
7253 {
7254 /* Watchpoints and the various bp_catch_* eventpoints should not
7255 have their addresses modified. */
7256 return bpaddr;
7257 }
7c16b83e
PA
7258 else if (bptype == bp_single_step)
7259 {
7260 /* Single-step breakpoints should not have their addresses
7261 modified. If there's any architectural constrain that
7262 applies to this address, then it should have already been
7263 taken into account when the breakpoint was created in the
7264 first place. If we didn't do this, stepping through e.g.,
7265 Thumb-2 IT blocks would break. */
7266 return bpaddr;
7267 }
76897487
KB
7268 else
7269 {
7270 CORE_ADDR adjusted_bpaddr;
7271
7272 /* Some targets have architectural constraints on the placement
7273 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7274 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7275
7276 /* An adjusted breakpoint address can significantly alter
7277 a user's expectations. Print a warning if an adjustment
7278 is required. */
7279 if (adjusted_bpaddr != bpaddr)
7280 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7281
7282 return adjusted_bpaddr;
7283 }
7284}
7285
5625a286 7286bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7287{
5625a286 7288 bp_location *loc = this;
7cc221ef 7289
348d480f
PA
7290 gdb_assert (ops != NULL);
7291
28010a5d
PA
7292 loc->ops = ops;
7293 loc->owner = owner;
b775012e 7294 loc->cond_bytecode = NULL;
0d381245
VP
7295 loc->shlib_disabled = 0;
7296 loc->enabled = 1;
e049a4b5 7297
28010a5d 7298 switch (owner->type)
e049a4b5
DJ
7299 {
7300 case bp_breakpoint:
7c16b83e 7301 case bp_single_step:
e049a4b5
DJ
7302 case bp_until:
7303 case bp_finish:
7304 case bp_longjmp:
7305 case bp_longjmp_resume:
e2e4d78b 7306 case bp_longjmp_call_dummy:
186c406b
TT
7307 case bp_exception:
7308 case bp_exception_resume:
e049a4b5 7309 case bp_step_resume:
2c03e5be 7310 case bp_hp_step_resume:
e049a4b5
DJ
7311 case bp_watchpoint_scope:
7312 case bp_call_dummy:
aa7d318d 7313 case bp_std_terminate:
e049a4b5
DJ
7314 case bp_shlib_event:
7315 case bp_thread_event:
7316 case bp_overlay_event:
4efc6507 7317 case bp_jit_event:
0fd8e87f 7318 case bp_longjmp_master:
aa7d318d 7319 case bp_std_terminate_master:
186c406b 7320 case bp_exception_master:
0e30163f
JK
7321 case bp_gnu_ifunc_resolver:
7322 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7323 case bp_dprintf:
e049a4b5 7324 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7325 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7326 break;
7327 case bp_hardware_breakpoint:
7328 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7329 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7330 break;
7331 case bp_hardware_watchpoint:
7332 case bp_read_watchpoint:
7333 case bp_access_watchpoint:
7334 loc->loc_type = bp_loc_hardware_watchpoint;
7335 break;
7336 case bp_watchpoint:
ce78b96d 7337 case bp_catchpoint:
15c3d785
PA
7338 case bp_tracepoint:
7339 case bp_fast_tracepoint:
0fb4aa4b 7340 case bp_static_tracepoint:
e049a4b5
DJ
7341 loc->loc_type = bp_loc_other;
7342 break;
7343 default:
e2e0b3e5 7344 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7345 }
7346
f431efe5 7347 loc->refc = 1;
28010a5d
PA
7348}
7349
7350/* Allocate a struct bp_location. */
7351
7352static struct bp_location *
7353allocate_bp_location (struct breakpoint *bpt)
7354{
348d480f
PA
7355 return bpt->ops->allocate_location (bpt);
7356}
7cc221ef 7357
f431efe5
PA
7358static void
7359free_bp_location (struct bp_location *loc)
fe3f5fa8 7360{
348d480f 7361 loc->ops->dtor (loc);
4d01a485 7362 delete loc;
fe3f5fa8
VP
7363}
7364
f431efe5
PA
7365/* Increment reference count. */
7366
7367static void
7368incref_bp_location (struct bp_location *bl)
7369{
7370 ++bl->refc;
7371}
7372
7373/* Decrement reference count. If the reference count reaches 0,
7374 destroy the bp_location. Sets *BLP to NULL. */
7375
7376static void
7377decref_bp_location (struct bp_location **blp)
7378{
0807b50c
PA
7379 gdb_assert ((*blp)->refc > 0);
7380
f431efe5
PA
7381 if (--(*blp)->refc == 0)
7382 free_bp_location (*blp);
7383 *blp = NULL;
7384}
7385
346774a9 7386/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7387
346774a9
PA
7388static void
7389add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7390{
346774a9 7391 struct breakpoint *b1;
c906108c 7392
346774a9
PA
7393 /* Add this breakpoint to the end of the chain so that a list of
7394 breakpoints will come out in order of increasing numbers. */
7395
7396 b1 = breakpoint_chain;
7397 if (b1 == 0)
7398 breakpoint_chain = b;
7399 else
7400 {
7401 while (b1->next)
7402 b1 = b1->next;
7403 b1->next = b;
7404 }
7405}
7406
7407/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7408
7409static void
7410init_raw_breakpoint_without_location (struct breakpoint *b,
7411 struct gdbarch *gdbarch,
28010a5d 7412 enum bptype bptype,
c0a91b2b 7413 const struct breakpoint_ops *ops)
346774a9 7414{
348d480f
PA
7415 gdb_assert (ops != NULL);
7416
28010a5d 7417 b->ops = ops;
4d28f7a8 7418 b->type = bptype;
a6d9a66e 7419 b->gdbarch = gdbarch;
c906108c
SS
7420 b->language = current_language->la_language;
7421 b->input_radix = input_radix;
d0fb5eae 7422 b->related_breakpoint = b;
346774a9
PA
7423}
7424
7425/* Helper to set_raw_breakpoint below. Creates a breakpoint
7426 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7427
7428static struct breakpoint *
7429set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7430 enum bptype bptype,
c0a91b2b 7431 const struct breakpoint_ops *ops)
346774a9 7432{
4d01a485 7433 struct breakpoint *b = new breakpoint ();
346774a9 7434
348d480f 7435 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7436 add_to_breakpoint_chain (b);
0d381245
VP
7437 return b;
7438}
7439
0e30163f
JK
7440/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7441 resolutions should be made as the user specified the location explicitly
7442 enough. */
7443
0d381245 7444static void
0e30163f 7445set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7446{
2bdf28a0
JK
7447 gdb_assert (loc->owner != NULL);
7448
0d381245 7449 if (loc->owner->type == bp_breakpoint
1042e4c0 7450 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7451 || is_tracepoint (loc->owner))
0d381245 7452 {
0e30163f 7453 int is_gnu_ifunc;
2c02bd72 7454 const char *function_name;
6a3a010b 7455 CORE_ADDR func_addr;
0e30163f 7456
2c02bd72 7457 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7458 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7459
7460 if (is_gnu_ifunc && !explicit_loc)
7461 {
7462 struct breakpoint *b = loc->owner;
7463
7464 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7465 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7466 &loc->requested_address))
7467 {
7468 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7469 loc->address = adjust_breakpoint_address (loc->gdbarch,
7470 loc->requested_address,
7471 b->type);
7472 }
7473 else if (b->type == bp_breakpoint && b->loc == loc
7474 && loc->next == NULL && b->related_breakpoint == b)
7475 {
7476 /* Create only the whole new breakpoint of this type but do not
7477 mess more complicated breakpoints with multiple locations. */
7478 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7479 /* Remember the resolver's address for use by the return
7480 breakpoint. */
7481 loc->related_address = func_addr;
0e30163f
JK
7482 }
7483 }
7484
2c02bd72
DE
7485 if (function_name)
7486 loc->function_name = xstrdup (function_name);
0d381245
VP
7487 }
7488}
7489
a6d9a66e 7490/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7491struct gdbarch *
a6d9a66e
UW
7492get_sal_arch (struct symtab_and_line sal)
7493{
7494 if (sal.section)
7495 return get_objfile_arch (sal.section->objfile);
7496 if (sal.symtab)
eb822aa6 7497 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7498
7499 return NULL;
7500}
7501
346774a9
PA
7502/* Low level routine for partially initializing a breakpoint of type
7503 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7504 file name, and line number are provided by SAL.
0d381245
VP
7505
7506 It is expected that the caller will complete the initialization of
7507 the newly created breakpoint struct as well as output any status
c56053d2 7508 information regarding the creation of a new breakpoint. */
0d381245 7509
346774a9
PA
7510static void
7511init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7512 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7513 const struct breakpoint_ops *ops)
0d381245 7514{
28010a5d 7515 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7516
3742cc8b 7517 add_location_to_breakpoint (b, &sal);
0d381245 7518
6c95b8df
PA
7519 if (bptype != bp_catchpoint)
7520 gdb_assert (sal.pspace != NULL);
7521
f8eba3c6
TT
7522 /* Store the program space that was used to set the breakpoint,
7523 except for ordinary breakpoints, which are independent of the
7524 program space. */
7525 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7526 b->pspace = sal.pspace;
346774a9 7527}
c906108c 7528
346774a9
PA
7529/* set_raw_breakpoint is a low level routine for allocating and
7530 partially initializing a breakpoint of type BPTYPE. The newly
7531 created breakpoint's address, section, source file name, and line
7532 number are provided by SAL. The newly created and partially
7533 initialized breakpoint is added to the breakpoint chain and
7534 is also returned as the value of this function.
7535
7536 It is expected that the caller will complete the initialization of
7537 the newly created breakpoint struct as well as output any status
7538 information regarding the creation of a new breakpoint. In
7539 particular, set_raw_breakpoint does NOT set the breakpoint
7540 number! Care should be taken to not allow an error to occur
7541 prior to completing the initialization of the breakpoint. If this
7542 should happen, a bogus breakpoint will be left on the chain. */
7543
7544struct breakpoint *
7545set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7546 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7547 const struct breakpoint_ops *ops)
346774a9 7548{
4d01a485 7549 struct breakpoint *b = new breakpoint ();
346774a9 7550
348d480f 7551 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7552 add_to_breakpoint_chain (b);
c906108c
SS
7553 return b;
7554}
7555
53a5351d 7556/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7557 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7558 initiated the operation. */
c906108c
SS
7559
7560void
186c406b 7561set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7562{
35df4500 7563 struct breakpoint *b, *b_tmp;
5d5658a1 7564 int thread = tp->global_num;
0fd8e87f
UW
7565
7566 /* To avoid having to rescan all objfile symbols at every step,
7567 we maintain a list of continually-inserted but always disabled
7568 longjmp "master" breakpoints. Here, we simply create momentary
7569 clones of those and enable them for the requested thread. */
35df4500 7570 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7571 if (b->pspace == current_program_space
186c406b
TT
7572 && (b->type == bp_longjmp_master
7573 || b->type == bp_exception_master))
0fd8e87f 7574 {
06edf0c0
PA
7575 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7576 struct breakpoint *clone;
cc59ec59 7577
e2e4d78b
JK
7578 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7579 after their removal. */
06edf0c0 7580 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7581 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7582 clone->thread = thread;
7583 }
186c406b
TT
7584
7585 tp->initiating_frame = frame;
c906108c
SS
7586}
7587
611c83ae 7588/* Delete all longjmp breakpoints from THREAD. */
c906108c 7589void
611c83ae 7590delete_longjmp_breakpoint (int thread)
c906108c 7591{
35df4500 7592 struct breakpoint *b, *b_tmp;
c906108c 7593
35df4500 7594 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7595 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7596 {
7597 if (b->thread == thread)
7598 delete_breakpoint (b);
7599 }
c906108c
SS
7600}
7601
f59f708a
PA
7602void
7603delete_longjmp_breakpoint_at_next_stop (int thread)
7604{
7605 struct breakpoint *b, *b_tmp;
7606
7607 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7608 if (b->type == bp_longjmp || b->type == bp_exception)
7609 {
7610 if (b->thread == thread)
7611 b->disposition = disp_del_at_next_stop;
7612 }
7613}
7614
e2e4d78b
JK
7615/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7616 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7617 pointer to any of them. Return NULL if this system cannot place longjmp
7618 breakpoints. */
7619
7620struct breakpoint *
7621set_longjmp_breakpoint_for_call_dummy (void)
7622{
7623 struct breakpoint *b, *retval = NULL;
7624
7625 ALL_BREAKPOINTS (b)
7626 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7627 {
7628 struct breakpoint *new_b;
7629
7630 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7631 &momentary_breakpoint_ops,
7632 1);
5d5658a1 7633 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7634
7635 /* Link NEW_B into the chain of RETVAL breakpoints. */
7636
7637 gdb_assert (new_b->related_breakpoint == new_b);
7638 if (retval == NULL)
7639 retval = new_b;
7640 new_b->related_breakpoint = retval;
7641 while (retval->related_breakpoint != new_b->related_breakpoint)
7642 retval = retval->related_breakpoint;
7643 retval->related_breakpoint = new_b;
7644 }
7645
7646 return retval;
7647}
7648
7649/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7650 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7651 stack.
7652
7653 You should call this function only at places where it is safe to currently
7654 unwind the whole stack. Failed stack unwind would discard live dummy
7655 frames. */
7656
7657void
b67a2c6f 7658check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7659{
7660 struct breakpoint *b, *b_tmp;
7661
7662 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7663 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7664 {
7665 struct breakpoint *dummy_b = b->related_breakpoint;
7666
7667 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7668 dummy_b = dummy_b->related_breakpoint;
7669 if (dummy_b->type != bp_call_dummy
7670 || frame_find_by_id (dummy_b->frame_id) != NULL)
7671 continue;
7672
b67a2c6f 7673 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7674
7675 while (b->related_breakpoint != b)
7676 {
7677 if (b_tmp == b->related_breakpoint)
7678 b_tmp = b->related_breakpoint->next;
7679 delete_breakpoint (b->related_breakpoint);
7680 }
7681 delete_breakpoint (b);
7682 }
7683}
7684
1900040c
MS
7685void
7686enable_overlay_breakpoints (void)
7687{
52f0bd74 7688 struct breakpoint *b;
1900040c
MS
7689
7690 ALL_BREAKPOINTS (b)
7691 if (b->type == bp_overlay_event)
7692 {
7693 b->enable_state = bp_enabled;
44702360 7694 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7695 overlay_events_enabled = 1;
1900040c
MS
7696 }
7697}
7698
7699void
7700disable_overlay_breakpoints (void)
7701{
52f0bd74 7702 struct breakpoint *b;
1900040c
MS
7703
7704 ALL_BREAKPOINTS (b)
7705 if (b->type == bp_overlay_event)
7706 {
7707 b->enable_state = bp_disabled;
44702360 7708 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7709 overlay_events_enabled = 0;
1900040c
MS
7710 }
7711}
7712
aa7d318d
TT
7713/* Set an active std::terminate breakpoint for each std::terminate
7714 master breakpoint. */
7715void
7716set_std_terminate_breakpoint (void)
7717{
35df4500 7718 struct breakpoint *b, *b_tmp;
aa7d318d 7719
35df4500 7720 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7721 if (b->pspace == current_program_space
7722 && b->type == bp_std_terminate_master)
7723 {
06edf0c0 7724 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7725 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7726 }
7727}
7728
7729/* Delete all the std::terminate breakpoints. */
7730void
7731delete_std_terminate_breakpoint (void)
7732{
35df4500 7733 struct breakpoint *b, *b_tmp;
aa7d318d 7734
35df4500 7735 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7736 if (b->type == bp_std_terminate)
7737 delete_breakpoint (b);
7738}
7739
c4093a6a 7740struct breakpoint *
a6d9a66e 7741create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7742{
7743 struct breakpoint *b;
c4093a6a 7744
06edf0c0
PA
7745 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7746 &internal_breakpoint_ops);
7747
b5de0fa7 7748 b->enable_state = bp_enabled;
f00aae0f 7749 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7750 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7751
44702360 7752 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7753
c4093a6a
JM
7754 return b;
7755}
7756
0101ce28
JJ
7757struct lang_and_radix
7758 {
7759 enum language lang;
7760 int radix;
7761 };
7762
4efc6507
DE
7763/* Create a breakpoint for JIT code registration and unregistration. */
7764
7765struct breakpoint *
7766create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7767{
2a7f3dff
PA
7768 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7769 &internal_breakpoint_ops);
4efc6507 7770}
0101ce28 7771
03673fc7
PP
7772/* Remove JIT code registration and unregistration breakpoint(s). */
7773
7774void
7775remove_jit_event_breakpoints (void)
7776{
7777 struct breakpoint *b, *b_tmp;
7778
7779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7780 if (b->type == bp_jit_event
7781 && b->loc->pspace == current_program_space)
7782 delete_breakpoint (b);
7783}
7784
cae688ec
JJ
7785void
7786remove_solib_event_breakpoints (void)
7787{
35df4500 7788 struct breakpoint *b, *b_tmp;
cae688ec 7789
35df4500 7790 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7791 if (b->type == bp_shlib_event
7792 && b->loc->pspace == current_program_space)
cae688ec
JJ
7793 delete_breakpoint (b);
7794}
7795
f37f681c
PA
7796/* See breakpoint.h. */
7797
7798void
7799remove_solib_event_breakpoints_at_next_stop (void)
7800{
7801 struct breakpoint *b, *b_tmp;
7802
7803 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7804 if (b->type == bp_shlib_event
7805 && b->loc->pspace == current_program_space)
7806 b->disposition = disp_del_at_next_stop;
7807}
7808
04086b45
PA
7809/* Helper for create_solib_event_breakpoint /
7810 create_and_insert_solib_event_breakpoint. Allows specifying which
7811 INSERT_MODE to pass through to update_global_location_list. */
7812
7813static struct breakpoint *
7814create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7815 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7816{
7817 struct breakpoint *b;
7818
06edf0c0
PA
7819 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7820 &internal_breakpoint_ops);
04086b45 7821 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7822 return b;
7823}
7824
04086b45
PA
7825struct breakpoint *
7826create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7827{
7828 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7829}
7830
f37f681c
PA
7831/* See breakpoint.h. */
7832
7833struct breakpoint *
7834create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7835{
7836 struct breakpoint *b;
7837
04086b45
PA
7838 /* Explicitly tell update_global_location_list to insert
7839 locations. */
7840 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7841 if (!b->loc->inserted)
7842 {
7843 delete_breakpoint (b);
7844 return NULL;
7845 }
7846 return b;
7847}
7848
cae688ec
JJ
7849/* Disable any breakpoints that are on code in shared libraries. Only
7850 apply to enabled breakpoints, disabled ones can just stay disabled. */
7851
7852void
cb851954 7853disable_breakpoints_in_shlibs (void)
cae688ec 7854{
876fa593 7855 struct bp_location *loc, **locp_tmp;
cae688ec 7856
876fa593 7857 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7858 {
2bdf28a0 7859 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7860 struct breakpoint *b = loc->owner;
2bdf28a0 7861
4a64f543
MS
7862 /* We apply the check to all breakpoints, including disabled for
7863 those with loc->duplicate set. This is so that when breakpoint
7864 becomes enabled, or the duplicate is removed, gdb will try to
7865 insert all breakpoints. If we don't set shlib_disabled here,
7866 we'll try to insert those breakpoints and fail. */
1042e4c0 7867 if (((b->type == bp_breakpoint)
508ccb1f 7868 || (b->type == bp_jit_event)
1042e4c0 7869 || (b->type == bp_hardware_breakpoint)
d77f58be 7870 || (is_tracepoint (b)))
6c95b8df 7871 && loc->pspace == current_program_space
0d381245 7872 && !loc->shlib_disabled
6c95b8df 7873 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7874 )
0d381245
VP
7875 {
7876 loc->shlib_disabled = 1;
7877 }
cae688ec
JJ
7878 }
7879}
7880
63644780
NB
7881/* Disable any breakpoints and tracepoints that are in SOLIB upon
7882 notification of unloaded_shlib. Only apply to enabled breakpoints,
7883 disabled ones can just stay disabled. */
84acb35a 7884
75149521 7885static void
84acb35a
JJ
7886disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7887{
876fa593 7888 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7889 int disabled_shlib_breaks = 0;
7890
876fa593 7891 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7892 {
2bdf28a0 7893 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7894 struct breakpoint *b = loc->owner;
cc59ec59 7895
1e4d1764 7896 if (solib->pspace == loc->pspace
e2dd7057 7897 && !loc->shlib_disabled
1e4d1764
YQ
7898 && (((b->type == bp_breakpoint
7899 || b->type == bp_jit_event
7900 || b->type == bp_hardware_breakpoint)
7901 && (loc->loc_type == bp_loc_hardware_breakpoint
7902 || loc->loc_type == bp_loc_software_breakpoint))
7903 || is_tracepoint (b))
e2dd7057 7904 && solib_contains_address_p (solib, loc->address))
84acb35a 7905 {
e2dd7057
PP
7906 loc->shlib_disabled = 1;
7907 /* At this point, we cannot rely on remove_breakpoint
7908 succeeding so we must mark the breakpoint as not inserted
7909 to prevent future errors occurring in remove_breakpoints. */
7910 loc->inserted = 0;
8d3788bd
VP
7911
7912 /* This may cause duplicate notifications for the same breakpoint. */
7913 observer_notify_breakpoint_modified (b);
7914
e2dd7057
PP
7915 if (!disabled_shlib_breaks)
7916 {
7917 target_terminal_ours_for_output ();
3e43a32a
MS
7918 warning (_("Temporarily disabling breakpoints "
7919 "for unloaded shared library \"%s\""),
e2dd7057 7920 solib->so_name);
84acb35a 7921 }
e2dd7057 7922 disabled_shlib_breaks = 1;
84acb35a
JJ
7923 }
7924 }
84acb35a
JJ
7925}
7926
63644780
NB
7927/* Disable any breakpoints and tracepoints in OBJFILE upon
7928 notification of free_objfile. Only apply to enabled breakpoints,
7929 disabled ones can just stay disabled. */
7930
7931static void
7932disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7933{
7934 struct breakpoint *b;
7935
7936 if (objfile == NULL)
7937 return;
7938
d03de421
PA
7939 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7940 managed by the user with add-symbol-file/remove-symbol-file.
7941 Similarly to how breakpoints in shared libraries are handled in
7942 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7943 shlib_disabled so they end up uninserted on the next global
7944 location list update. Shared libraries not loaded by the user
7945 aren't handled here -- they're already handled in
7946 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7947 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7948 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7949 main objfile). */
7950 if ((objfile->flags & OBJF_SHARED) == 0
7951 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7952 return;
7953
7954 ALL_BREAKPOINTS (b)
7955 {
7956 struct bp_location *loc;
7957 int bp_modified = 0;
7958
7959 if (!is_breakpoint (b) && !is_tracepoint (b))
7960 continue;
7961
7962 for (loc = b->loc; loc != NULL; loc = loc->next)
7963 {
7964 CORE_ADDR loc_addr = loc->address;
7965
7966 if (loc->loc_type != bp_loc_hardware_breakpoint
7967 && loc->loc_type != bp_loc_software_breakpoint)
7968 continue;
7969
7970 if (loc->shlib_disabled != 0)
7971 continue;
7972
7973 if (objfile->pspace != loc->pspace)
7974 continue;
7975
7976 if (loc->loc_type != bp_loc_hardware_breakpoint
7977 && loc->loc_type != bp_loc_software_breakpoint)
7978 continue;
7979
7980 if (is_addr_in_objfile (loc_addr, objfile))
7981 {
7982 loc->shlib_disabled = 1;
08351840
PA
7983 /* At this point, we don't know whether the object was
7984 unmapped from the inferior or not, so leave the
7985 inserted flag alone. We'll handle failure to
7986 uninsert quietly, in case the object was indeed
7987 unmapped. */
63644780
NB
7988
7989 mark_breakpoint_location_modified (loc);
7990
7991 bp_modified = 1;
7992 }
7993 }
7994
7995 if (bp_modified)
7996 observer_notify_breakpoint_modified (b);
7997 }
7998}
7999
ce78b96d
JB
8000/* FORK & VFORK catchpoints. */
8001
e29a4733
PA
8002/* An instance of this type is used to represent a fork or vfork
8003 catchpoint. It includes a "struct breakpoint" as a kind of base
8004 class; users downcast to "struct breakpoint *" when needed. A
8005 breakpoint is really of this type iff its ops pointer points to
8006 CATCH_FORK_BREAKPOINT_OPS. */
8007
8008struct fork_catchpoint
8009{
8010 /* The base class. */
8011 struct breakpoint base;
8012
8013 /* Process id of a child process whose forking triggered this
8014 catchpoint. This field is only valid immediately after this
8015 catchpoint has triggered. */
8016 ptid_t forked_inferior_pid;
8017};
8018
4a64f543
MS
8019/* Implement the "insert" breakpoint_ops method for fork
8020 catchpoints. */
ce78b96d 8021
77b06cd7
TJB
8022static int
8023insert_catch_fork (struct bp_location *bl)
ce78b96d 8024{
dfd4cc63 8025 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8026}
8027
4a64f543
MS
8028/* Implement the "remove" breakpoint_ops method for fork
8029 catchpoints. */
ce78b96d
JB
8030
8031static int
73971819 8032remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8033{
dfd4cc63 8034 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8035}
8036
8037/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8038 catchpoints. */
8039
8040static int
f1310107 8041breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8042 struct address_space *aspace, CORE_ADDR bp_addr,
8043 const struct target_waitstatus *ws)
ce78b96d 8044{
e29a4733
PA
8045 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8046
f90263c1
TT
8047 if (ws->kind != TARGET_WAITKIND_FORKED)
8048 return 0;
8049
8050 c->forked_inferior_pid = ws->value.related_pid;
8051 return 1;
ce78b96d
JB
8052}
8053
4a64f543
MS
8054/* Implement the "print_it" breakpoint_ops method for fork
8055 catchpoints. */
ce78b96d
JB
8056
8057static enum print_stop_action
348d480f 8058print_it_catch_fork (bpstat bs)
ce78b96d 8059{
36dfb11c 8060 struct ui_out *uiout = current_uiout;
348d480f
PA
8061 struct breakpoint *b = bs->breakpoint_at;
8062 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8063
ce78b96d 8064 annotate_catchpoint (b->number);
f303dbd6 8065 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8066 if (b->disposition == disp_del)
112e8700 8067 uiout->text ("Temporary catchpoint ");
36dfb11c 8068 else
112e8700
SM
8069 uiout->text ("Catchpoint ");
8070 if (uiout->is_mi_like_p ())
36dfb11c 8071 {
112e8700
SM
8072 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8073 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8074 }
112e8700
SM
8075 uiout->field_int ("bkptno", b->number);
8076 uiout->text (" (forked process ");
8077 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8078 uiout->text ("), ");
ce78b96d
JB
8079 return PRINT_SRC_AND_LOC;
8080}
8081
4a64f543
MS
8082/* Implement the "print_one" breakpoint_ops method for fork
8083 catchpoints. */
ce78b96d
JB
8084
8085static void
a6d9a66e 8086print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8087{
e29a4733 8088 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8089 struct value_print_options opts;
79a45e25 8090 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8091
8092 get_user_print_options (&opts);
8093
4a64f543
MS
8094 /* Field 4, the address, is omitted (which makes the columns not
8095 line up too nicely with the headers, but the effect is relatively
8096 readable). */
79a45b7d 8097 if (opts.addressprint)
112e8700 8098 uiout->field_skip ("addr");
ce78b96d 8099 annotate_field (5);
112e8700 8100 uiout->text ("fork");
e29a4733 8101 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8102 {
112e8700
SM
8103 uiout->text (", process ");
8104 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8105 uiout->spaces (1);
ce78b96d 8106 }
8ac3646f 8107
112e8700
SM
8108 if (uiout->is_mi_like_p ())
8109 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8110}
8111
8112/* Implement the "print_mention" breakpoint_ops method for fork
8113 catchpoints. */
8114
8115static void
8116print_mention_catch_fork (struct breakpoint *b)
8117{
8118 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8119}
8120
6149aea9
PA
8121/* Implement the "print_recreate" breakpoint_ops method for fork
8122 catchpoints. */
8123
8124static void
8125print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8126{
8127 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8128 print_recreate_thread (b, fp);
6149aea9
PA
8129}
8130
ce78b96d
JB
8131/* The breakpoint_ops structure to be used in fork catchpoints. */
8132
2060206e 8133static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8134
4a64f543
MS
8135/* Implement the "insert" breakpoint_ops method for vfork
8136 catchpoints. */
ce78b96d 8137
77b06cd7
TJB
8138static int
8139insert_catch_vfork (struct bp_location *bl)
ce78b96d 8140{
dfd4cc63 8141 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8142}
8143
4a64f543
MS
8144/* Implement the "remove" breakpoint_ops method for vfork
8145 catchpoints. */
ce78b96d
JB
8146
8147static int
73971819 8148remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8149{
dfd4cc63 8150 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8151}
8152
8153/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8154 catchpoints. */
8155
8156static int
f1310107 8157breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8158 struct address_space *aspace, CORE_ADDR bp_addr,
8159 const struct target_waitstatus *ws)
ce78b96d 8160{
e29a4733
PA
8161 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8162
f90263c1
TT
8163 if (ws->kind != TARGET_WAITKIND_VFORKED)
8164 return 0;
8165
8166 c->forked_inferior_pid = ws->value.related_pid;
8167 return 1;
ce78b96d
JB
8168}
8169
4a64f543
MS
8170/* Implement the "print_it" breakpoint_ops method for vfork
8171 catchpoints. */
ce78b96d
JB
8172
8173static enum print_stop_action
348d480f 8174print_it_catch_vfork (bpstat bs)
ce78b96d 8175{
36dfb11c 8176 struct ui_out *uiout = current_uiout;
348d480f 8177 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8178 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8179
ce78b96d 8180 annotate_catchpoint (b->number);
f303dbd6 8181 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8182 if (b->disposition == disp_del)
112e8700 8183 uiout->text ("Temporary catchpoint ");
36dfb11c 8184 else
112e8700
SM
8185 uiout->text ("Catchpoint ");
8186 if (uiout->is_mi_like_p ())
36dfb11c 8187 {
112e8700
SM
8188 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8189 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8190 }
112e8700
SM
8191 uiout->field_int ("bkptno", b->number);
8192 uiout->text (" (vforked process ");
8193 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8194 uiout->text ("), ");
ce78b96d
JB
8195 return PRINT_SRC_AND_LOC;
8196}
8197
4a64f543
MS
8198/* Implement the "print_one" breakpoint_ops method for vfork
8199 catchpoints. */
ce78b96d
JB
8200
8201static void
a6d9a66e 8202print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8203{
e29a4733 8204 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8205 struct value_print_options opts;
79a45e25 8206 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8207
8208 get_user_print_options (&opts);
4a64f543
MS
8209 /* Field 4, the address, is omitted (which makes the columns not
8210 line up too nicely with the headers, but the effect is relatively
8211 readable). */
79a45b7d 8212 if (opts.addressprint)
112e8700 8213 uiout->field_skip ("addr");
ce78b96d 8214 annotate_field (5);
112e8700 8215 uiout->text ("vfork");
e29a4733 8216 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8217 {
112e8700
SM
8218 uiout->text (", process ");
8219 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8220 uiout->spaces (1);
ce78b96d 8221 }
8ac3646f 8222
112e8700
SM
8223 if (uiout->is_mi_like_p ())
8224 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8225}
8226
8227/* Implement the "print_mention" breakpoint_ops method for vfork
8228 catchpoints. */
8229
8230static void
8231print_mention_catch_vfork (struct breakpoint *b)
8232{
8233 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8234}
8235
6149aea9
PA
8236/* Implement the "print_recreate" breakpoint_ops method for vfork
8237 catchpoints. */
8238
8239static void
8240print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8241{
8242 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8243 print_recreate_thread (b, fp);
6149aea9
PA
8244}
8245
ce78b96d
JB
8246/* The breakpoint_ops structure to be used in vfork catchpoints. */
8247
2060206e 8248static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8249
edcc5120
TT
8250/* An instance of this type is used to represent an solib catchpoint.
8251 It includes a "struct breakpoint" as a kind of base class; users
8252 downcast to "struct breakpoint *" when needed. A breakpoint is
8253 really of this type iff its ops pointer points to
8254 CATCH_SOLIB_BREAKPOINT_OPS. */
8255
8256struct solib_catchpoint
8257{
8258 /* The base class. */
8259 struct breakpoint base;
8260
8261 /* True for "catch load", false for "catch unload". */
8262 unsigned char is_load;
8263
8264 /* Regular expression to match, if any. COMPILED is only valid when
8265 REGEX is non-NULL. */
8266 char *regex;
8267 regex_t compiled;
8268};
8269
8270static void
8271dtor_catch_solib (struct breakpoint *b)
8272{
8273 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8274
8275 if (self->regex)
8276 regfree (&self->compiled);
8277 xfree (self->regex);
8278
8279 base_breakpoint_ops.dtor (b);
8280}
8281
8282static int
8283insert_catch_solib (struct bp_location *ignore)
8284{
8285 return 0;
8286}
8287
8288static int
73971819 8289remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8290{
8291 return 0;
8292}
8293
8294static int
8295breakpoint_hit_catch_solib (const struct bp_location *bl,
8296 struct address_space *aspace,
8297 CORE_ADDR bp_addr,
8298 const struct target_waitstatus *ws)
8299{
8300 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8301 struct breakpoint *other;
8302
8303 if (ws->kind == TARGET_WAITKIND_LOADED)
8304 return 1;
8305
8306 ALL_BREAKPOINTS (other)
8307 {
8308 struct bp_location *other_bl;
8309
8310 if (other == bl->owner)
8311 continue;
8312
8313 if (other->type != bp_shlib_event)
8314 continue;
8315
8316 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8317 continue;
8318
8319 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8320 {
8321 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8322 return 1;
8323 }
8324 }
8325
8326 return 0;
8327}
8328
8329static void
8330check_status_catch_solib (struct bpstats *bs)
8331{
8332 struct solib_catchpoint *self
8333 = (struct solib_catchpoint *) bs->breakpoint_at;
8334 int ix;
8335
8336 if (self->is_load)
8337 {
8338 struct so_list *iter;
8339
8340 for (ix = 0;
8341 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8342 ix, iter);
8343 ++ix)
8344 {
8345 if (!self->regex
8346 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8347 return;
8348 }
8349 }
8350 else
8351 {
8352 char *iter;
8353
8354 for (ix = 0;
8355 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8356 ix, iter);
8357 ++ix)
8358 {
8359 if (!self->regex
8360 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8361 return;
8362 }
8363 }
8364
8365 bs->stop = 0;
8366 bs->print_it = print_it_noop;
8367}
8368
8369static enum print_stop_action
8370print_it_catch_solib (bpstat bs)
8371{
8372 struct breakpoint *b = bs->breakpoint_at;
8373 struct ui_out *uiout = current_uiout;
8374
8375 annotate_catchpoint (b->number);
f303dbd6 8376 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8377 if (b->disposition == disp_del)
112e8700 8378 uiout->text ("Temporary catchpoint ");
edcc5120 8379 else
112e8700
SM
8380 uiout->text ("Catchpoint ");
8381 uiout->field_int ("bkptno", b->number);
8382 uiout->text ("\n");
8383 if (uiout->is_mi_like_p ())
8384 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8385 print_solib_event (1);
8386 return PRINT_SRC_AND_LOC;
8387}
8388
8389static void
8390print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8391{
8392 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8393 struct value_print_options opts;
8394 struct ui_out *uiout = current_uiout;
8395 char *msg;
8396
8397 get_user_print_options (&opts);
8398 /* Field 4, the address, is omitted (which makes the columns not
8399 line up too nicely with the headers, but the effect is relatively
8400 readable). */
8401 if (opts.addressprint)
8402 {
8403 annotate_field (4);
112e8700 8404 uiout->field_skip ("addr");
edcc5120
TT
8405 }
8406
8407 annotate_field (5);
8408 if (self->is_load)
8409 {
8410 if (self->regex)
8411 msg = xstrprintf (_("load of library matching %s"), self->regex);
8412 else
8413 msg = xstrdup (_("load of library"));
8414 }
8415 else
8416 {
8417 if (self->regex)
8418 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8419 else
8420 msg = xstrdup (_("unload of library"));
8421 }
112e8700 8422 uiout->field_string ("what", msg);
edcc5120 8423 xfree (msg);
8ac3646f 8424
112e8700
SM
8425 if (uiout->is_mi_like_p ())
8426 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8427}
8428
8429static void
8430print_mention_catch_solib (struct breakpoint *b)
8431{
8432 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8433
8434 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8435 self->is_load ? "load" : "unload");
8436}
8437
8438static void
8439print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8440{
8441 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8442
8443 fprintf_unfiltered (fp, "%s %s",
8444 b->disposition == disp_del ? "tcatch" : "catch",
8445 self->is_load ? "load" : "unload");
8446 if (self->regex)
8447 fprintf_unfiltered (fp, " %s", self->regex);
8448 fprintf_unfiltered (fp, "\n");
8449}
8450
8451static struct breakpoint_ops catch_solib_breakpoint_ops;
8452
91985142
MG
8453/* Shared helper function (MI and CLI) for creating and installing
8454 a shared object event catchpoint. If IS_LOAD is non-zero then
8455 the events to be caught are load events, otherwise they are
8456 unload events. If IS_TEMP is non-zero the catchpoint is a
8457 temporary one. If ENABLED is non-zero the catchpoint is
8458 created in an enabled state. */
edcc5120 8459
91985142 8460void
a121b7c1 8461add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8462{
8463 struct solib_catchpoint *c;
8464 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8465 struct cleanup *cleanup;
8466
edcc5120
TT
8467 if (!arg)
8468 arg = "";
a121b7c1 8469 arg = skip_spaces_const (arg);
edcc5120 8470
4d01a485 8471 c = new solib_catchpoint ();
edcc5120
TT
8472 cleanup = make_cleanup (xfree, c);
8473
8474 if (*arg != '\0')
8475 {
8476 int errcode;
8477
8478 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8479 if (errcode != 0)
8480 {
8481 char *err = get_regcomp_error (errcode, &c->compiled);
8482
8483 make_cleanup (xfree, err);
8484 error (_("Invalid regexp (%s): %s"), err, arg);
8485 }
8486 c->regex = xstrdup (arg);
8487 }
8488
8489 c->is_load = is_load;
91985142 8490 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8491 &catch_solib_breakpoint_ops);
8492
91985142
MG
8493 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8494
edcc5120
TT
8495 discard_cleanups (cleanup);
8496 install_breakpoint (0, &c->base, 1);
8497}
8498
91985142
MG
8499/* A helper function that does all the work for "catch load" and
8500 "catch unload". */
8501
8502static void
8503catch_load_or_unload (char *arg, int from_tty, int is_load,
8504 struct cmd_list_element *command)
8505{
8506 int tempflag;
8507 const int enabled = 1;
8508
8509 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8510
8511 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8512}
8513
edcc5120
TT
8514static void
8515catch_load_command_1 (char *arg, int from_tty,
8516 struct cmd_list_element *command)
8517{
8518 catch_load_or_unload (arg, from_tty, 1, command);
8519}
8520
8521static void
8522catch_unload_command_1 (char *arg, int from_tty,
8523 struct cmd_list_element *command)
8524{
8525 catch_load_or_unload (arg, from_tty, 0, command);
8526}
8527
346774a9
PA
8528/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8529 is non-zero, then make the breakpoint temporary. If COND_STRING is
8530 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8531 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8532
ab04a2af 8533void
346774a9
PA
8534init_catchpoint (struct breakpoint *b,
8535 struct gdbarch *gdbarch, int tempflag,
63160a43 8536 const char *cond_string,
c0a91b2b 8537 const struct breakpoint_ops *ops)
c906108c 8538{
c5aa993b 8539 struct symtab_and_line sal;
346774a9 8540
fe39c653 8541 init_sal (&sal);
6c95b8df 8542 sal.pspace = current_program_space;
c5aa993b 8543
28010a5d 8544 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8545
1b36a34b 8546 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8547 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8548}
8549
28010a5d 8550void
3ea46bff 8551install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8552{
8553 add_to_breakpoint_chain (b);
3a5c3e22 8554 set_breakpoint_number (internal, b);
558a9d82
YQ
8555 if (is_tracepoint (b))
8556 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8557 if (!internal)
8558 mention (b);
c56053d2 8559 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8560
8561 if (update_gll)
44702360 8562 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8563}
8564
9b70b993 8565static void
a6d9a66e 8566create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8567 int tempflag, const char *cond_string,
c0a91b2b 8568 const struct breakpoint_ops *ops)
c906108c 8569{
4d01a485 8570 struct fork_catchpoint *c = new fork_catchpoint ();
ce78b96d 8571
e29a4733
PA
8572 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8573
8574 c->forked_inferior_pid = null_ptid;
8575
3ea46bff 8576 install_breakpoint (0, &c->base, 1);
c906108c
SS
8577}
8578
fe798b75
JB
8579/* Exec catchpoints. */
8580
b4d90040
PA
8581/* An instance of this type is used to represent an exec catchpoint.
8582 It includes a "struct breakpoint" as a kind of base class; users
8583 downcast to "struct breakpoint *" when needed. A breakpoint is
8584 really of this type iff its ops pointer points to
8585 CATCH_EXEC_BREAKPOINT_OPS. */
8586
8587struct exec_catchpoint
8588{
8589 /* The base class. */
8590 struct breakpoint base;
8591
8592 /* Filename of a program whose exec triggered this catchpoint.
8593 This field is only valid immediately after this catchpoint has
8594 triggered. */
8595 char *exec_pathname;
8596};
8597
8598/* Implement the "dtor" breakpoint_ops method for exec
8599 catchpoints. */
8600
8601static void
8602dtor_catch_exec (struct breakpoint *b)
8603{
8604 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8605
8606 xfree (c->exec_pathname);
348d480f 8607
2060206e 8608 base_breakpoint_ops.dtor (b);
b4d90040
PA
8609}
8610
77b06cd7
TJB
8611static int
8612insert_catch_exec (struct bp_location *bl)
c906108c 8613{
dfd4cc63 8614 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8615}
c906108c 8616
fe798b75 8617static int
73971819 8618remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8619{
dfd4cc63 8620 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8621}
c906108c 8622
fe798b75 8623static int
f1310107 8624breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8625 struct address_space *aspace, CORE_ADDR bp_addr,
8626 const struct target_waitstatus *ws)
fe798b75 8627{
b4d90040
PA
8628 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8629
f90263c1
TT
8630 if (ws->kind != TARGET_WAITKIND_EXECD)
8631 return 0;
8632
8633 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8634 return 1;
fe798b75 8635}
c906108c 8636
fe798b75 8637static enum print_stop_action
348d480f 8638print_it_catch_exec (bpstat bs)
fe798b75 8639{
36dfb11c 8640 struct ui_out *uiout = current_uiout;
348d480f 8641 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8642 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8643
fe798b75 8644 annotate_catchpoint (b->number);
f303dbd6 8645 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8646 if (b->disposition == disp_del)
112e8700 8647 uiout->text ("Temporary catchpoint ");
36dfb11c 8648 else
112e8700
SM
8649 uiout->text ("Catchpoint ");
8650 if (uiout->is_mi_like_p ())
36dfb11c 8651 {
112e8700
SM
8652 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8653 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8654 }
112e8700
SM
8655 uiout->field_int ("bkptno", b->number);
8656 uiout->text (" (exec'd ");
8657 uiout->field_string ("new-exec", c->exec_pathname);
8658 uiout->text ("), ");
36dfb11c 8659
fe798b75 8660 return PRINT_SRC_AND_LOC;
c906108c
SS
8661}
8662
fe798b75 8663static void
a6d9a66e 8664print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8665{
b4d90040 8666 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8667 struct value_print_options opts;
79a45e25 8668 struct ui_out *uiout = current_uiout;
fe798b75
JB
8669
8670 get_user_print_options (&opts);
8671
8672 /* Field 4, the address, is omitted (which makes the columns
8673 not line up too nicely with the headers, but the effect
8674 is relatively readable). */
8675 if (opts.addressprint)
112e8700 8676 uiout->field_skip ("addr");
fe798b75 8677 annotate_field (5);
112e8700 8678 uiout->text ("exec");
b4d90040 8679 if (c->exec_pathname != NULL)
fe798b75 8680 {
112e8700
SM
8681 uiout->text (", program \"");
8682 uiout->field_string ("what", c->exec_pathname);
8683 uiout->text ("\" ");
fe798b75 8684 }
8ac3646f 8685
112e8700
SM
8686 if (uiout->is_mi_like_p ())
8687 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8688}
8689
8690static void
8691print_mention_catch_exec (struct breakpoint *b)
8692{
8693 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8694}
8695
6149aea9
PA
8696/* Implement the "print_recreate" breakpoint_ops method for exec
8697 catchpoints. */
8698
8699static void
8700print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8701{
8702 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8703 print_recreate_thread (b, fp);
6149aea9
PA
8704}
8705
2060206e 8706static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8707
c906108c 8708static int
fba45db2 8709hw_breakpoint_used_count (void)
c906108c 8710{
c906108c 8711 int i = 0;
f1310107
TJB
8712 struct breakpoint *b;
8713 struct bp_location *bl;
c906108c
SS
8714
8715 ALL_BREAKPOINTS (b)
c5aa993b 8716 {
d6b74ac4 8717 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8718 for (bl = b->loc; bl; bl = bl->next)
8719 {
8720 /* Special types of hardware breakpoints may use more than
8721 one register. */
348d480f 8722 i += b->ops->resources_needed (bl);
f1310107 8723 }
c5aa993b 8724 }
c906108c
SS
8725
8726 return i;
8727}
8728
a1398e0c
PA
8729/* Returns the resources B would use if it were a hardware
8730 watchpoint. */
8731
c906108c 8732static int
a1398e0c 8733hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8734{
c906108c 8735 int i = 0;
e09342b5 8736 struct bp_location *bl;
c906108c 8737
a1398e0c
PA
8738 if (!breakpoint_enabled (b))
8739 return 0;
8740
8741 for (bl = b->loc; bl; bl = bl->next)
8742 {
8743 /* Special types of hardware watchpoints may use more than
8744 one register. */
8745 i += b->ops->resources_needed (bl);
8746 }
8747
8748 return i;
8749}
8750
8751/* Returns the sum the used resources of all hardware watchpoints of
8752 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8753 the sum of the used resources of all hardware watchpoints of other
8754 types _not_ TYPE. */
8755
8756static int
8757hw_watchpoint_used_count_others (struct breakpoint *except,
8758 enum bptype type, int *other_type_used)
8759{
8760 int i = 0;
8761 struct breakpoint *b;
8762
c906108c
SS
8763 *other_type_used = 0;
8764 ALL_BREAKPOINTS (b)
e09342b5 8765 {
a1398e0c
PA
8766 if (b == except)
8767 continue;
e09342b5
TJB
8768 if (!breakpoint_enabled (b))
8769 continue;
8770
a1398e0c
PA
8771 if (b->type == type)
8772 i += hw_watchpoint_use_count (b);
8773 else if (is_hardware_watchpoint (b))
8774 *other_type_used = 1;
e09342b5
TJB
8775 }
8776
c906108c
SS
8777 return i;
8778}
8779
c906108c 8780void
fba45db2 8781disable_watchpoints_before_interactive_call_start (void)
c906108c 8782{
c5aa993b 8783 struct breakpoint *b;
c906108c
SS
8784
8785 ALL_BREAKPOINTS (b)
c5aa993b 8786 {
cc60f2e3 8787 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8788 {
b5de0fa7 8789 b->enable_state = bp_call_disabled;
44702360 8790 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8791 }
8792 }
c906108c
SS
8793}
8794
8795void
fba45db2 8796enable_watchpoints_after_interactive_call_stop (void)
c906108c 8797{
c5aa993b 8798 struct breakpoint *b;
c906108c
SS
8799
8800 ALL_BREAKPOINTS (b)
c5aa993b 8801 {
cc60f2e3 8802 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8803 {
b5de0fa7 8804 b->enable_state = bp_enabled;
44702360 8805 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8806 }
8807 }
c906108c
SS
8808}
8809
8bea4e01
UW
8810void
8811disable_breakpoints_before_startup (void)
8812{
6c95b8df 8813 current_program_space->executing_startup = 1;
44702360 8814 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8815}
8816
8817void
8818enable_breakpoints_after_startup (void)
8819{
6c95b8df 8820 current_program_space->executing_startup = 0;
f8eba3c6 8821 breakpoint_re_set ();
8bea4e01
UW
8822}
8823
7c16b83e
PA
8824/* Create a new single-step breakpoint for thread THREAD, with no
8825 locations. */
c906108c 8826
7c16b83e
PA
8827static struct breakpoint *
8828new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8829{
4d01a485 8830 struct breakpoint *b = new breakpoint ();
7c16b83e
PA
8831
8832 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8833 &momentary_breakpoint_ops);
8834
8835 b->disposition = disp_donttouch;
8836 b->frame_id = null_frame_id;
8837
8838 b->thread = thread;
8839 gdb_assert (b->thread != 0);
8840
8841 add_to_breakpoint_chain (b);
8842
8843 return b;
8844}
8845
8846/* Set a momentary breakpoint of type TYPE at address specified by
8847 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8848 frame. */
c906108c
SS
8849
8850struct breakpoint *
a6d9a66e
UW
8851set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8852 struct frame_id frame_id, enum bptype type)
c906108c 8853{
52f0bd74 8854 struct breakpoint *b;
edb3359d 8855
193facb3
JK
8856 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8857 tail-called one. */
8858 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8859
06edf0c0 8860 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8861 b->enable_state = bp_enabled;
8862 b->disposition = disp_donttouch;
818dd999 8863 b->frame_id = frame_id;
c906108c 8864
4a64f543
MS
8865 /* If we're debugging a multi-threaded program, then we want
8866 momentary breakpoints to be active in only a single thread of
8867 control. */
39f77062 8868 if (in_thread_list (inferior_ptid))
5d5658a1 8869 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8870
44702360 8871 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8872
c906108c
SS
8873 return b;
8874}
611c83ae 8875
06edf0c0 8876/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8877 The new breakpoint will have type TYPE, use OPS as its
8878 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8879
06edf0c0
PA
8880static struct breakpoint *
8881momentary_breakpoint_from_master (struct breakpoint *orig,
8882 enum bptype type,
a1aa2221
LM
8883 const struct breakpoint_ops *ops,
8884 int loc_enabled)
e58b0e63
PA
8885{
8886 struct breakpoint *copy;
8887
06edf0c0 8888 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8889 copy->loc = allocate_bp_location (copy);
0e30163f 8890 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8891
a6d9a66e 8892 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8893 copy->loc->requested_address = orig->loc->requested_address;
8894 copy->loc->address = orig->loc->address;
8895 copy->loc->section = orig->loc->section;
6c95b8df 8896 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8897 copy->loc->probe = orig->loc->probe;
f8eba3c6 8898 copy->loc->line_number = orig->loc->line_number;
2f202fde 8899 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8900 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8901 copy->frame_id = orig->frame_id;
8902 copy->thread = orig->thread;
6c95b8df 8903 copy->pspace = orig->pspace;
e58b0e63
PA
8904
8905 copy->enable_state = bp_enabled;
8906 copy->disposition = disp_donttouch;
8907 copy->number = internal_breakpoint_number--;
8908
44702360 8909 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8910 return copy;
8911}
8912
06edf0c0
PA
8913/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8914 ORIG is NULL. */
8915
8916struct breakpoint *
8917clone_momentary_breakpoint (struct breakpoint *orig)
8918{
8919 /* If there's nothing to clone, then return nothing. */
8920 if (orig == NULL)
8921 return NULL;
8922
a1aa2221 8923 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8924}
8925
611c83ae 8926struct breakpoint *
a6d9a66e
UW
8927set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8928 enum bptype type)
611c83ae
PA
8929{
8930 struct symtab_and_line sal;
8931
8932 sal = find_pc_line (pc, 0);
8933 sal.pc = pc;
8934 sal.section = find_pc_overlay (pc);
8935 sal.explicit_pc = 1;
8936
a6d9a66e 8937 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8938}
c906108c 8939\f
c5aa993b 8940
c906108c
SS
8941/* Tell the user we have just set a breakpoint B. */
8942
8943static void
fba45db2 8944mention (struct breakpoint *b)
c906108c 8945{
348d480f 8946 b->ops->print_mention (b);
112e8700 8947 if (current_uiout->is_mi_like_p ())
fb40c209 8948 return;
c906108c
SS
8949 printf_filtered ("\n");
8950}
c906108c 8951\f
c5aa993b 8952
1a853c52
PA
8953static int bp_loc_is_permanent (struct bp_location *loc);
8954
0d381245 8955static struct bp_location *
39d61571 8956add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8957 const struct symtab_and_line *sal)
8958{
8959 struct bp_location *loc, **tmp;
3742cc8b
YQ
8960 CORE_ADDR adjusted_address;
8961 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8962
8963 if (loc_gdbarch == NULL)
8964 loc_gdbarch = b->gdbarch;
8965
8966 /* Adjust the breakpoint's address prior to allocating a location.
8967 Once we call allocate_bp_location(), that mostly uninitialized
8968 location will be placed on the location chain. Adjustment of the
8969 breakpoint may cause target_read_memory() to be called and we do
8970 not want its scan of the location chain to find a breakpoint and
8971 location that's only been partially initialized. */
8972 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8973 sal->pc, b->type);
0d381245 8974
d30113d4 8975 /* Sort the locations by their ADDRESS. */
39d61571 8976 loc = allocate_bp_location (b);
d30113d4
JK
8977 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8978 tmp = &((*tmp)->next))
0d381245 8979 ;
d30113d4 8980 loc->next = *tmp;
0d381245 8981 *tmp = loc;
3742cc8b 8982
0d381245 8983 loc->requested_address = sal->pc;
3742cc8b 8984 loc->address = adjusted_address;
6c95b8df 8985 loc->pspace = sal->pspace;
729662a5
TT
8986 loc->probe.probe = sal->probe;
8987 loc->probe.objfile = sal->objfile;
6c95b8df 8988 gdb_assert (loc->pspace != NULL);
0d381245 8989 loc->section = sal->section;
3742cc8b 8990 loc->gdbarch = loc_gdbarch;
f8eba3c6 8991 loc->line_number = sal->line;
2f202fde 8992 loc->symtab = sal->symtab;
f8eba3c6 8993
0e30163f
JK
8994 set_breakpoint_location_function (loc,
8995 sal->explicit_pc || sal->explicit_line);
1a853c52 8996
6ae88661
LM
8997 /* While by definition, permanent breakpoints are already present in the
8998 code, we don't mark the location as inserted. Normally one would expect
8999 that GDB could rely on that breakpoint instruction to stop the program,
9000 thus removing the need to insert its own breakpoint, except that executing
9001 the breakpoint instruction can kill the target instead of reporting a
9002 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9003 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9004 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9005 breakpoint be inserted normally results in QEMU knowing about the GDB
9006 breakpoint, and thus trap before the breakpoint instruction is executed.
9007 (If GDB later needs to continue execution past the permanent breakpoint,
9008 it manually increments the PC, thus avoiding executing the breakpoint
9009 instruction.) */
1a853c52 9010 if (bp_loc_is_permanent (loc))
6ae88661 9011 loc->permanent = 1;
1a853c52 9012
0d381245
VP
9013 return loc;
9014}
514f746b
AR
9015\f
9016
1cf4d951 9017/* See breakpoint.h. */
514f746b 9018
1cf4d951
PA
9019int
9020program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9021{
9022 int len;
9023 CORE_ADDR addr;
1afeeb75 9024 const gdb_byte *bpoint;
514f746b 9025 gdb_byte *target_mem;
939c61fa
JK
9026 struct cleanup *cleanup;
9027 int retval = 0;
514f746b 9028
1cf4d951
PA
9029 addr = address;
9030 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9031
9032 /* Software breakpoints unsupported? */
9033 if (bpoint == NULL)
9034 return 0;
9035
224c3ddb 9036 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9037
9038 /* Enable the automatic memory restoration from breakpoints while
9039 we read the memory. Otherwise we could say about our temporary
9040 breakpoints they are permanent. */
9041 cleanup = make_show_memory_breakpoints_cleanup (0);
9042
9043 if (target_read_memory (address, target_mem, len) == 0
9044 && memcmp (target_mem, bpoint, len) == 0)
9045 retval = 1;
9046
9047 do_cleanups (cleanup);
9048
9049 return retval;
9050}
9051
9052/* Return 1 if LOC is pointing to a permanent breakpoint,
9053 return 0 otherwise. */
9054
9055static int
9056bp_loc_is_permanent (struct bp_location *loc)
9057{
514f746b
AR
9058 gdb_assert (loc != NULL);
9059
244558af
LM
9060 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9061 attempt to read from the addresses the locations of these breakpoint types
9062 point to. program_breakpoint_here_p, below, will attempt to read
9063 memory. */
9064 if (!breakpoint_address_is_meaningful (loc->owner))
9065 return 0;
9066
5ed8105e 9067 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9068 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 9069 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
9070}
9071
e7e0cddf
SS
9072/* Build a command list for the dprintf corresponding to the current
9073 settings of the dprintf style options. */
9074
9075static void
9076update_dprintf_command_list (struct breakpoint *b)
9077{
9078 char *dprintf_args = b->extra_string;
9079 char *printf_line = NULL;
9080
9081 if (!dprintf_args)
9082 return;
9083
9084 dprintf_args = skip_spaces (dprintf_args);
9085
9086 /* Allow a comma, as it may have terminated a location, but don't
9087 insist on it. */
9088 if (*dprintf_args == ',')
9089 ++dprintf_args;
9090 dprintf_args = skip_spaces (dprintf_args);
9091
9092 if (*dprintf_args != '"')
9093 error (_("Bad format string, missing '\"'."));
9094
d3ce09f5 9095 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9096 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9097 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9098 {
9099 if (!dprintf_function)
9100 error (_("No function supplied for dprintf call"));
9101
9102 if (dprintf_channel && strlen (dprintf_channel) > 0)
9103 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9104 dprintf_function,
9105 dprintf_channel,
9106 dprintf_args);
9107 else
9108 printf_line = xstrprintf ("call (void) %s (%s)",
9109 dprintf_function,
9110 dprintf_args);
9111 }
d3ce09f5
SS
9112 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9113 {
9114 if (target_can_run_breakpoint_commands ())
9115 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9116 else
9117 {
9118 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9119 printf_line = xstrprintf ("printf %s", dprintf_args);
9120 }
9121 }
e7e0cddf
SS
9122 else
9123 internal_error (__FILE__, __LINE__,
9124 _("Invalid dprintf style."));
9125
f28045c2 9126 gdb_assert (printf_line != NULL);
9d6e6e84 9127 /* Manufacture a printf sequence. */
f28045c2 9128 {
8d749320 9129 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9130
f28045c2
YQ
9131 printf_cmd_line->control_type = simple_control;
9132 printf_cmd_line->body_count = 0;
9133 printf_cmd_line->body_list = NULL;
9d6e6e84 9134 printf_cmd_line->next = NULL;
f28045c2 9135 printf_cmd_line->line = printf_line;
e7e0cddf 9136
93921405 9137 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9138 }
e7e0cddf
SS
9139}
9140
9141/* Update all dprintf commands, making their command lists reflect
9142 current style settings. */
9143
9144static void
9145update_dprintf_commands (char *args, int from_tty,
9146 struct cmd_list_element *c)
9147{
9148 struct breakpoint *b;
9149
9150 ALL_BREAKPOINTS (b)
9151 {
9152 if (b->type == bp_dprintf)
9153 update_dprintf_command_list (b);
9154 }
9155}
c3f6f71d 9156
f00aae0f
KS
9157/* Create a breakpoint with SAL as location. Use LOCATION
9158 as a description of the location, and COND_STRING
b35a8b2f
DE
9159 as condition expression. If LOCATION is NULL then create an
9160 "address location" from the address in the SAL. */
018d34a4
VP
9161
9162static void
d9b3f62e 9163init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9164 struct symtabs_and_lines sals,
ffc2605c 9165 event_location_up &&location,
f8eba3c6 9166 char *filter, char *cond_string,
e7e0cddf 9167 char *extra_string,
d9b3f62e
PA
9168 enum bptype type, enum bpdisp disposition,
9169 int thread, int task, int ignore_count,
c0a91b2b 9170 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9171 int enabled, int internal, unsigned flags,
9172 int display_canonical)
018d34a4 9173{
0d381245 9174 int i;
018d34a4
VP
9175
9176 if (type == bp_hardware_breakpoint)
9177 {
fbbd034e
AS
9178 int target_resources_ok;
9179
9180 i = hw_breakpoint_used_count ();
9181 target_resources_ok =
9182 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9183 i + 1, 0);
9184 if (target_resources_ok == 0)
9185 error (_("No hardware breakpoint support in the target."));
9186 else if (target_resources_ok < 0)
9187 error (_("Hardware breakpoints used exceeds limit."));
9188 }
9189
6c95b8df
PA
9190 gdb_assert (sals.nelts > 0);
9191
0d381245
VP
9192 for (i = 0; i < sals.nelts; ++i)
9193 {
9194 struct symtab_and_line sal = sals.sals[i];
9195 struct bp_location *loc;
9196
9197 if (from_tty)
5af949e3
UW
9198 {
9199 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9200 if (!loc_gdbarch)
9201 loc_gdbarch = gdbarch;
9202
9203 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9204 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9205 }
0d381245
VP
9206
9207 if (i == 0)
9208 {
d9b3f62e 9209 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9210 b->thread = thread;
4a306c9a 9211 b->task = task;
855a6e68 9212
0d381245 9213 b->cond_string = cond_string;
e7e0cddf 9214 b->extra_string = extra_string;
0d381245 9215 b->ignore_count = ignore_count;
41447f92 9216 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9217 b->disposition = disposition;
6c95b8df 9218
44f238bb
PA
9219 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9220 b->loc->inserted = 1;
9221
0fb4aa4b
PA
9222 if (type == bp_static_tracepoint)
9223 {
d9b3f62e 9224 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9225 struct static_tracepoint_marker marker;
9226
983af33b 9227 if (strace_marker_p (b))
0fb4aa4b
PA
9228 {
9229 /* We already know the marker exists, otherwise, we
9230 wouldn't see a sal for it. */
d28cd78a
TT
9231 const char *p
9232 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9233 const char *endp;
0fb4aa4b 9234 char *marker_str;
0fb4aa4b 9235
f00aae0f 9236 p = skip_spaces_const (p);
0fb4aa4b 9237
f00aae0f 9238 endp = skip_to_space_const (p);
0fb4aa4b
PA
9239
9240 marker_str = savestring (p, endp - p);
d9b3f62e 9241 t->static_trace_marker_id = marker_str;
0fb4aa4b 9242
3e43a32a
MS
9243 printf_filtered (_("Probed static tracepoint "
9244 "marker \"%s\"\n"),
d9b3f62e 9245 t->static_trace_marker_id);
0fb4aa4b
PA
9246 }
9247 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9248 {
d9b3f62e 9249 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9250 release_static_tracepoint_marker (&marker);
9251
3e43a32a
MS
9252 printf_filtered (_("Probed static tracepoint "
9253 "marker \"%s\"\n"),
d9b3f62e 9254 t->static_trace_marker_id);
0fb4aa4b
PA
9255 }
9256 else
3e43a32a
MS
9257 warning (_("Couldn't determine the static "
9258 "tracepoint marker to probe"));
0fb4aa4b
PA
9259 }
9260
0d381245
VP
9261 loc = b->loc;
9262 }
9263 else
018d34a4 9264 {
39d61571 9265 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9266 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9267 loc->inserted = 1;
0d381245
VP
9268 }
9269
9270 if (b->cond_string)
9271 {
bbc13ae3
KS
9272 const char *arg = b->cond_string;
9273
1bb9788d
TT
9274 loc->cond = parse_exp_1 (&arg, loc->address,
9275 block_for_pc (loc->address), 0);
0d381245 9276 if (*arg)
588ae58c 9277 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9278 }
e7e0cddf
SS
9279
9280 /* Dynamic printf requires and uses additional arguments on the
9281 command line, otherwise it's an error. */
9282 if (type == bp_dprintf)
9283 {
9284 if (b->extra_string)
9285 update_dprintf_command_list (b);
9286 else
9287 error (_("Format string required"));
9288 }
9289 else if (b->extra_string)
588ae58c 9290 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9291 }
018d34a4 9292
56435ebe 9293 b->display_canonical = display_canonical;
f00aae0f 9294 if (location != NULL)
d28cd78a 9295 b->location = std::move (location);
018d34a4 9296 else
d28cd78a 9297 b->location = new_address_location (b->loc->address, NULL, 0);
f8eba3c6 9298 b->filter = filter;
d9b3f62e 9299}
018d34a4 9300
d9b3f62e
PA
9301static void
9302create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9303 struct symtabs_and_lines sals,
ffc2605c 9304 event_location_up &&location,
f8eba3c6 9305 char *filter, char *cond_string,
e7e0cddf 9306 char *extra_string,
d9b3f62e
PA
9307 enum bptype type, enum bpdisp disposition,
9308 int thread, int task, int ignore_count,
c0a91b2b 9309 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9310 int enabled, int internal, unsigned flags,
9311 int display_canonical)
d9b3f62e
PA
9312{
9313 struct breakpoint *b;
9314 struct cleanup *old_chain;
9315
9316 if (is_tracepoint_type (type))
9317 {
9318 struct tracepoint *t;
9319
4d01a485 9320 t = new tracepoint ();
d9b3f62e
PA
9321 b = &t->base;
9322 }
9323 else
4d01a485 9324 b = new breakpoint ();
d9b3f62e
PA
9325
9326 old_chain = make_cleanup (xfree, b);
9327
9328 init_breakpoint_sal (b, gdbarch,
ffc2605c 9329 sals, std::move (location),
e7e0cddf 9330 filter, cond_string, extra_string,
d9b3f62e
PA
9331 type, disposition,
9332 thread, task, ignore_count,
9333 ops, from_tty,
44f238bb
PA
9334 enabled, internal, flags,
9335 display_canonical);
d9b3f62e
PA
9336 discard_cleanups (old_chain);
9337
3ea46bff 9338 install_breakpoint (internal, b, 0);
018d34a4
VP
9339}
9340
9341/* Add SALS.nelts breakpoints to the breakpoint table. For each
9342 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9343 value. COND_STRING, if not NULL, specified the condition to be
9344 used for all breakpoints. Essentially the only case where
9345 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9346 function. In that case, it's still not possible to specify
9347 separate conditions for different overloaded functions, so
9348 we take just a single condition string.
9349
c3f6f71d 9350 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9351 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9352 array contents). If the function fails (error() is called), the
9353 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9354 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9355
9356static void
8cdf0e15 9357create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9358 struct linespec_result *canonical,
e7e0cddf 9359 char *cond_string, char *extra_string,
8cdf0e15
VP
9360 enum bptype type, enum bpdisp disposition,
9361 int thread, int task, int ignore_count,
c0a91b2b 9362 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9363 int enabled, int internal, unsigned flags)
c906108c 9364{
018d34a4 9365 int i;
f8eba3c6 9366 struct linespec_sals *lsal;
cc59ec59 9367
f8eba3c6
TT
9368 if (canonical->pre_expanded)
9369 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9370
9371 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9372 {
f00aae0f 9373 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9374 'break', without arguments. */
ffc2605c 9375 event_location_up location
f00aae0f 9376 = (canonical->location != NULL
8e9e35b1 9377 ? copy_event_location (canonical->location.get ()) : NULL);
f8eba3c6 9378 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
0d381245 9379
f8eba3c6
TT
9380 make_cleanup (xfree, filter_string);
9381 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9382 std::move (location),
f8eba3c6 9383 filter_string,
e7e0cddf
SS
9384 cond_string, extra_string,
9385 type, disposition,
84f4c1fe 9386 thread, task, ignore_count, ops,
44f238bb 9387 from_tty, enabled, internal, flags,
56435ebe 9388 canonical->special_display);
c3f6f71d 9389 }
c3f6f71d 9390}
c906108c 9391
f00aae0f 9392/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9393 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9394 addresses found. LOCATION points to the end of the SAL (for
9395 linespec locations).
9998af43
TJB
9396
9397 The array and the line spec strings are allocated on the heap, it is
9398 the caller's responsibility to free them. */
c906108c 9399
b9362cc7 9400static void
f00aae0f 9401parse_breakpoint_sals (const struct event_location *location,
58438ac1 9402 struct linespec_result *canonical)
c3f6f71d 9403{
f00aae0f
KS
9404 struct symtab_and_line cursal;
9405
9406 if (event_location_type (location) == LINESPEC_LOCATION)
9407 {
9408 const char *address = get_linespec_location (location);
9409
9410 if (address == NULL)
9411 {
9412 /* The last displayed codepoint, if it's valid, is our default
9413 breakpoint address. */
9414 if (last_displayed_sal_is_valid ())
9415 {
9416 struct linespec_sals lsal;
9417 struct symtab_and_line sal;
9418 CORE_ADDR pc;
9419
9420 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9421 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9422
9423 /* Set sal's pspace, pc, symtab, and line to the values
9424 corresponding to the last call to print_frame_info.
9425 Be sure to reinitialize LINE with NOTCURRENT == 0
9426 as the breakpoint line number is inappropriate otherwise.
9427 find_pc_line would adjust PC, re-set it back. */
9428 get_last_displayed_sal (&sal);
9429 pc = sal.pc;
9430 sal = find_pc_line (pc, 0);
9431
9432 /* "break" without arguments is equivalent to "break *PC"
9433 where PC is the last displayed codepoint's address. So
9434 make sure to set sal.explicit_pc to prevent GDB from
9435 trying to expand the list of sals to include all other
9436 instances with the same symtab and line. */
9437 sal.pc = pc;
9438 sal.explicit_pc = 1;
9439
9440 lsal.sals.sals[0] = sal;
9441 lsal.sals.nelts = 1;
9442 lsal.canonical = NULL;
9443
9444 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9445 return;
9446 }
9447 else
9448 error (_("No default breakpoint address now."));
c906108c 9449 }
c906108c 9450 }
f00aae0f
KS
9451
9452 /* Force almost all breakpoints to be in terms of the
9453 current_source_symtab (which is decode_line_1's default).
9454 This should produce the results we want almost all of the
9455 time while leaving default_breakpoint_* alone.
9456
9457 ObjC: However, don't match an Objective-C method name which
9458 may have a '+' or '-' succeeded by a '['. */
9459 cursal = get_current_source_symtab_and_line ();
9460 if (last_displayed_sal_is_valid ())
c906108c 9461 {
f00aae0f 9462 const char *address = NULL;
cc80f267 9463
f00aae0f
KS
9464 if (event_location_type (location) == LINESPEC_LOCATION)
9465 address = get_linespec_location (location);
cc80f267 9466
f00aae0f
KS
9467 if (!cursal.symtab
9468 || (address != NULL
9469 && strchr ("+-", address[0]) != NULL
9470 && address[1] != '['))
9471 {
c2f4122d 9472 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9473 get_last_displayed_symtab (),
9474 get_last_displayed_line (),
9475 canonical, NULL, NULL);
9476 return;
9477 }
c906108c 9478 }
f00aae0f 9479
c2f4122d 9480 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9481 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9482}
c906108c 9483
c906108c 9484
c3f6f71d 9485/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9486 inserted as a breakpoint. If it can't throw an error. */
c906108c 9487
b9362cc7 9488static void
23e7acfb 9489breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9490{
9491 int i;
cc59ec59 9492
c3f6f71d 9493 for (i = 0; i < sals->nelts; i++)
ee53e872 9494 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9495}
9496
7a697b8d
SS
9497/* Fast tracepoints may have restrictions on valid locations. For
9498 instance, a fast tracepoint using a jump instead of a trap will
9499 likely have to overwrite more bytes than a trap would, and so can
9500 only be placed where the instruction is longer than the jump, or a
9501 multi-instruction sequence does not have a jump into the middle of
9502 it, etc. */
9503
9504static void
9505check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9506 struct symtabs_and_lines *sals)
9507{
9508 int i, rslt;
9509 struct symtab_and_line *sal;
9510 char *msg;
9511 struct cleanup *old_chain;
9512
9513 for (i = 0; i < sals->nelts; i++)
9514 {
f8eba3c6
TT
9515 struct gdbarch *sarch;
9516
7a697b8d
SS
9517 sal = &sals->sals[i];
9518
f8eba3c6
TT
9519 sarch = get_sal_arch (*sal);
9520 /* We fall back to GDBARCH if there is no architecture
9521 associated with SAL. */
9522 if (sarch == NULL)
9523 sarch = gdbarch;
6b940e6a 9524 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9525 old_chain = make_cleanup (xfree, msg);
9526
9527 if (!rslt)
53c3572a 9528 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9529 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9530
9531 do_cleanups (old_chain);
9532 }
9533}
9534
018d34a4
VP
9535/* Given TOK, a string specification of condition and thread, as
9536 accepted by the 'break' command, extract the condition
9537 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9538 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9539 If no condition is found, *COND_STRING is set to NULL.
9540 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9541
9542static void
bbc13ae3 9543find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9544 char **cond_string, int *thread, int *task,
9545 char **rest)
018d34a4
VP
9546{
9547 *cond_string = NULL;
9548 *thread = -1;
ed1d1739
KS
9549 *task = 0;
9550 *rest = NULL;
9551
018d34a4
VP
9552 while (tok && *tok)
9553 {
bbc13ae3 9554 const char *end_tok;
018d34a4 9555 int toklen;
bbc13ae3
KS
9556 const char *cond_start = NULL;
9557 const char *cond_end = NULL;
cc59ec59 9558
bbc13ae3 9559 tok = skip_spaces_const (tok);
e7e0cddf
SS
9560
9561 if ((*tok == '"' || *tok == ',') && rest)
9562 {
9563 *rest = savestring (tok, strlen (tok));
9564 return;
9565 }
9566
bbc13ae3 9567 end_tok = skip_to_space_const (tok);
d634f2de 9568
018d34a4 9569 toklen = end_tok - tok;
d634f2de 9570
018d34a4
VP
9571 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9572 {
9573 tok = cond_start = end_tok + 1;
4d01a485 9574 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9575 cond_end = tok;
d634f2de 9576 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9577 }
9578 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9579 {
5d5658a1
PA
9580 const char *tmptok;
9581 struct thread_info *thr;
d634f2de 9582
018d34a4 9583 tok = end_tok + 1;
5d5658a1 9584 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9585 if (tok == tmptok)
9586 error (_("Junk after thread keyword."));
5d5658a1 9587 *thread = thr->global_num;
bbc13ae3 9588 tok = tmptok;
018d34a4 9589 }
4a306c9a
JB
9590 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9591 {
9592 char *tmptok;
9593
9594 tok = end_tok + 1;
bbc13ae3 9595 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9596 if (tok == tmptok)
9597 error (_("Junk after task keyword."));
9598 if (!valid_task_id (*task))
b6199126 9599 error (_("Unknown task %d."), *task);
bbc13ae3 9600 tok = tmptok;
4a306c9a 9601 }
e7e0cddf
SS
9602 else if (rest)
9603 {
9604 *rest = savestring (tok, strlen (tok));
ccab2054 9605 return;
e7e0cddf 9606 }
018d34a4
VP
9607 else
9608 error (_("Junk at end of arguments."));
9609 }
9610}
9611
0fb4aa4b
PA
9612/* Decode a static tracepoint marker spec. */
9613
9614static struct symtabs_and_lines
f00aae0f 9615decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9616{
9617 VEC(static_tracepoint_marker_p) *markers = NULL;
9618 struct symtabs_and_lines sals;
0fb4aa4b 9619 struct cleanup *old_chain;
f00aae0f
KS
9620 const char *p = &(*arg_p)[3];
9621 const char *endp;
0fb4aa4b
PA
9622 char *marker_str;
9623 int i;
9624
f00aae0f 9625 p = skip_spaces_const (p);
0fb4aa4b 9626
f00aae0f 9627 endp = skip_to_space_const (p);
0fb4aa4b
PA
9628
9629 marker_str = savestring (p, endp - p);
9630 old_chain = make_cleanup (xfree, marker_str);
9631
9632 markers = target_static_tracepoint_markers_by_strid (marker_str);
9633 if (VEC_empty(static_tracepoint_marker_p, markers))
9634 error (_("No known static tracepoint marker named %s"), marker_str);
9635
9636 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9637 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9638
9639 for (i = 0; i < sals.nelts; i++)
9640 {
9641 struct static_tracepoint_marker *marker;
9642
9643 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9644
9645 init_sal (&sals.sals[i]);
9646
9647 sals.sals[i] = find_pc_line (marker->address, 0);
9648 sals.sals[i].pc = marker->address;
9649
9650 release_static_tracepoint_marker (marker);
9651 }
9652
9653 do_cleanups (old_chain);
9654
9655 *arg_p = endp;
9656 return sals;
9657}
9658
f00aae0f 9659/* See breakpoint.h. */
0101ce28 9660
8cdf0e15
VP
9661int
9662create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9663 const struct event_location *location, char *cond_string,
e7e0cddf 9664 int thread, char *extra_string,
f00aae0f 9665 int parse_extra,
0fb4aa4b 9666 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9667 int ignore_count,
9668 enum auto_boolean pending_break_support,
c0a91b2b 9669 const struct breakpoint_ops *ops,
44f238bb
PA
9670 int from_tty, int enabled, int internal,
9671 unsigned flags)
c3f6f71d 9672{
7efd8fc2 9673 struct linespec_result canonical;
80c99de1 9674 struct cleanup *bkpt_chain = NULL;
0101ce28 9675 int pending = 0;
4a306c9a 9676 int task = 0;
86b17b60 9677 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9678
348d480f
PA
9679 gdb_assert (ops != NULL);
9680
f00aae0f
KS
9681 /* If extra_string isn't useful, set it to NULL. */
9682 if (extra_string != NULL && *extra_string == '\0')
9683 extra_string = NULL;
9684
492d29ea 9685 TRY
b78a6381 9686 {
f00aae0f 9687 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9688 }
492d29ea 9689 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9690 {
492d29ea
PA
9691 /* If caller is interested in rc value from parse, set
9692 value. */
9693 if (e.error == NOT_FOUND_ERROR)
0101ce28 9694 {
05ff989b
AC
9695 /* If pending breakpoint support is turned off, throw
9696 error. */
fa8d40ab
JJ
9697
9698 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9699 throw_exception (e);
9700
9701 exception_print (gdb_stderr, e);
fa8d40ab 9702
05ff989b
AC
9703 /* If pending breakpoint support is auto query and the user
9704 selects no, then simply return the error code. */
059fb39f 9705 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9706 && !nquery (_("Make %s pending on future shared library load? "),
9707 bptype_string (type_wanted)))
fd9b8c24 9708 return 0;
fa8d40ab 9709
05ff989b
AC
9710 /* At this point, either the user was queried about setting
9711 a pending breakpoint and selected yes, or pending
9712 breakpoint behavior is on and thus a pending breakpoint
9713 is defaulted on behalf of the user. */
f00aae0f 9714 pending = 1;
0101ce28 9715 }
492d29ea
PA
9716 else
9717 throw_exception (e);
0101ce28 9718 }
492d29ea
PA
9719 END_CATCH
9720
f00aae0f 9721 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9722 return 0;
c3f6f71d 9723
c3f6f71d
JM
9724 /* ----------------------------- SNIP -----------------------------
9725 Anything added to the cleanup chain beyond this point is assumed
9726 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9727 then the memory is not reclaimed. */
9728 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9729
c3f6f71d
JM
9730 /* Resolve all line numbers to PC's and verify that the addresses
9731 are ok for the target. */
0101ce28 9732 if (!pending)
f8eba3c6
TT
9733 {
9734 int ix;
9735 struct linespec_sals *iter;
9736
9737 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9738 breakpoint_sals_to_pc (&iter->sals);
9739 }
c3f6f71d 9740
7a697b8d 9741 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9742 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9743 {
9744 int ix;
9745 struct linespec_sals *iter;
9746
9747 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9748 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9749 }
7a697b8d 9750
c3f6f71d
JM
9751 /* Verify that condition can be parsed, before setting any
9752 breakpoints. Allocate a separate condition expression for each
4a64f543 9753 breakpoint. */
0101ce28 9754 if (!pending)
c3f6f71d 9755 {
f00aae0f 9756 if (parse_extra)
72b2ff0e 9757 {
0878d0fa 9758 char *rest;
52d361e1
YQ
9759 struct linespec_sals *lsal;
9760
9761 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9762
0878d0fa
YQ
9763 /* Here we only parse 'arg' to separate condition
9764 from thread number, so parsing in context of first
9765 sal is OK. When setting the breakpoint we'll
9766 re-parse it in context of each sal. */
9767
f00aae0f
KS
9768 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9769 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9770 if (cond_string)
9771 make_cleanup (xfree, cond_string);
9772 if (rest)
9773 make_cleanup (xfree, rest);
9774 if (rest)
9775 extra_string = rest;
f00aae0f
KS
9776 else
9777 extra_string = NULL;
72b2ff0e 9778 }
2f069f6f 9779 else
72b2ff0e 9780 {
f00aae0f
KS
9781 if (type_wanted != bp_dprintf
9782 && extra_string != NULL && *extra_string != '\0')
9783 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9784
9785 /* Create a private copy of condition string. */
9786 if (cond_string)
9787 {
9788 cond_string = xstrdup (cond_string);
9789 make_cleanup (xfree, cond_string);
9790 }
9791 /* Create a private copy of any extra string. */
9792 if (extra_string)
9793 {
9794 extra_string = xstrdup (extra_string);
9795 make_cleanup (xfree, extra_string);
9796 }
72b2ff0e 9797 }
0fb4aa4b 9798
52d361e1 9799 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9800 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9801 tempflag ? disp_del : disp_donttouch,
9802 thread, task, ignore_count, ops,
44f238bb 9803 from_tty, enabled, internal, flags);
c906108c 9804 }
0101ce28
JJ
9805 else
9806 {
0101ce28
JJ
9807 struct breakpoint *b;
9808
bfccc43c
YQ
9809 if (is_tracepoint_type (type_wanted))
9810 {
9811 struct tracepoint *t;
9812
4d01a485 9813 t = new tracepoint ();
bfccc43c
YQ
9814 b = &t->base;
9815 }
9816 else
4d01a485 9817 b = new breakpoint ();
bfccc43c
YQ
9818
9819 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
d28cd78a 9820 b->location = copy_event_location (location);
bfccc43c 9821
f00aae0f
KS
9822 if (parse_extra)
9823 b->cond_string = NULL;
e12c7713
MK
9824 else
9825 {
9826 /* Create a private copy of condition string. */
9827 if (cond_string)
9828 {
9829 cond_string = xstrdup (cond_string);
9830 make_cleanup (xfree, cond_string);
9831 }
9832 b->cond_string = cond_string;
15630549 9833 b->thread = thread;
e12c7713 9834 }
f00aae0f
KS
9835
9836 /* Create a private copy of any extra string. */
9837 if (extra_string != NULL)
9838 {
9839 extra_string = xstrdup (extra_string);
9840 make_cleanup (xfree, extra_string);
9841 }
9842 b->extra_string = extra_string;
0101ce28 9843 b->ignore_count = ignore_count;
0101ce28 9844 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9845 b->condition_not_parsed = 1;
41447f92 9846 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9847 if ((type_wanted != bp_breakpoint
9848 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9849 b->pspace = current_program_space;
8bea4e01 9850
bfccc43c 9851 install_breakpoint (internal, b, 0);
0101ce28
JJ
9852 }
9853
f8eba3c6 9854 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9855 {
3e43a32a
MS
9856 warning (_("Multiple breakpoints were set.\nUse the "
9857 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9858 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9859 }
9860
80c99de1
PA
9861 /* That's it. Discard the cleanups for data inserted into the
9862 breakpoint. */
9863 discard_cleanups (bkpt_chain);
217dc9e2 9864
80c99de1 9865 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9866 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9867
9868 return 1;
c3f6f71d 9869}
c906108c 9870
348d480f 9871/* Set a breakpoint.
72b2ff0e
VP
9872 ARG is a string describing breakpoint address,
9873 condition, and thread.
9874 FLAG specifies if a breakpoint is hardware on,
9875 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9876 and BP_TEMPFLAG. */
348d480f 9877
98deb0da 9878static void
72b2ff0e 9879break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9880{
72b2ff0e 9881 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9882 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9883 ? bp_hardware_breakpoint
9884 : bp_breakpoint);
55aa24fb 9885 struct breakpoint_ops *ops;
f00aae0f 9886
ffc2605c 9887 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9888
9889 /* Matching breakpoints on probes. */
5b56227b 9890 if (location != NULL
ffc2605c 9891 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9892 ops = &bkpt_probe_breakpoint_ops;
9893 else
9894 ops = &bkpt_breakpoint_ops;
c3f6f71d 9895
8cdf0e15 9896 create_breakpoint (get_current_arch (),
ffc2605c 9897 location.get (),
f00aae0f 9898 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9899 tempflag, type_wanted,
8cdf0e15
VP
9900 0 /* Ignore count */,
9901 pending_break_support,
55aa24fb 9902 ops,
8cdf0e15 9903 from_tty,
84f4c1fe 9904 1 /* enabled */,
44f238bb
PA
9905 0 /* internal */,
9906 0);
c906108c
SS
9907}
9908
c906108c
SS
9909/* Helper function for break_command_1 and disassemble_command. */
9910
9911void
fba45db2 9912resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9913{
9914 CORE_ADDR pc;
9915
9916 if (sal->pc == 0 && sal->symtab != NULL)
9917 {
9918 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9919 error (_("No line %d in file \"%s\"."),
05cba821 9920 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9921 sal->pc = pc;
6a048695 9922
4a64f543
MS
9923 /* If this SAL corresponds to a breakpoint inserted using a line
9924 number, then skip the function prologue if necessary. */
6a048695 9925 if (sal->explicit_line)
059acae7 9926 skip_prologue_sal (sal);
c906108c
SS
9927 }
9928
9929 if (sal->section == 0 && sal->symtab != NULL)
9930 {
346d1dfe 9931 const struct blockvector *bv;
3977b71f 9932 const struct block *b;
c5aa993b 9933 struct symbol *sym;
c906108c 9934
43f3e411
DE
9935 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9936 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9937 if (bv != NULL)
9938 {
7f0df278 9939 sym = block_linkage_function (b);
c906108c
SS
9940 if (sym != NULL)
9941 {
eb822aa6
DE
9942 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9943 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9944 sym);
c906108c
SS
9945 }
9946 else
9947 {
4a64f543
MS
9948 /* It really is worthwhile to have the section, so we'll
9949 just have to look harder. This case can be executed
9950 if we have line numbers but no functions (as can
9951 happen in assembly source). */
c906108c 9952
5ed8105e 9953 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9954 switch_to_program_space_and_thread (sal->pspace);
c906108c 9955
5ed8105e 9956 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9957 if (msym.minsym)
efd66ac6 9958 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9959 }
9960 }
9961 }
9962}
9963
9964void
fba45db2 9965break_command (char *arg, int from_tty)
c906108c 9966{
db107f19 9967 break_command_1 (arg, 0, from_tty);
c906108c
SS
9968}
9969
c906108c 9970void
fba45db2 9971tbreak_command (char *arg, int from_tty)
c906108c 9972{
db107f19 9973 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9974}
9975
c906108c 9976static void
fba45db2 9977hbreak_command (char *arg, int from_tty)
c906108c 9978{
db107f19 9979 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9980}
9981
9982static void
fba45db2 9983thbreak_command (char *arg, int from_tty)
c906108c 9984{
db107f19 9985 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9986}
9987
9988static void
fba45db2 9989stop_command (char *arg, int from_tty)
c906108c 9990{
a3f17187 9991 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9992Usage: stop in <function | address>\n\
a3f17187 9993 stop at <line>\n"));
c906108c
SS
9994}
9995
9996static void
fba45db2 9997stopin_command (char *arg, int from_tty)
c906108c
SS
9998{
9999 int badInput = 0;
10000
c5aa993b 10001 if (arg == (char *) NULL)
c906108c
SS
10002 badInput = 1;
10003 else if (*arg != '*')
10004 {
10005 char *argptr = arg;
10006 int hasColon = 0;
10007
4a64f543 10008 /* Look for a ':'. If this is a line number specification, then
53a5351d 10009 say it is bad, otherwise, it should be an address or
4a64f543 10010 function/method name. */
c906108c 10011 while (*argptr && !hasColon)
c5aa993b
JM
10012 {
10013 hasColon = (*argptr == ':');
10014 argptr++;
10015 }
c906108c
SS
10016
10017 if (hasColon)
c5aa993b 10018 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10019 else
c5aa993b 10020 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10021 }
10022
10023 if (badInput)
a3f17187 10024 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10025 else
db107f19 10026 break_command_1 (arg, 0, from_tty);
c906108c
SS
10027}
10028
10029static void
fba45db2 10030stopat_command (char *arg, int from_tty)
c906108c
SS
10031{
10032 int badInput = 0;
10033
c5aa993b 10034 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10035 badInput = 1;
10036 else
10037 {
10038 char *argptr = arg;
10039 int hasColon = 0;
10040
4a64f543
MS
10041 /* Look for a ':'. If there is a '::' then get out, otherwise
10042 it is probably a line number. */
c906108c 10043 while (*argptr && !hasColon)
c5aa993b
JM
10044 {
10045 hasColon = (*argptr == ':');
10046 argptr++;
10047 }
c906108c
SS
10048
10049 if (hasColon)
c5aa993b 10050 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10051 else
c5aa993b 10052 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10053 }
10054
10055 if (badInput)
a3f17187 10056 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10057 else
db107f19 10058 break_command_1 (arg, 0, from_tty);
c906108c
SS
10059}
10060
e7e0cddf
SS
10061/* The dynamic printf command is mostly like a regular breakpoint, but
10062 with a prewired command list consisting of a single output command,
10063 built from extra arguments supplied on the dprintf command
10064 line. */
10065
da821c7b 10066static void
e7e0cddf
SS
10067dprintf_command (char *arg, int from_tty)
10068{
ffc2605c 10069 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10070
10071 /* If non-NULL, ARG should have been advanced past the location;
10072 the next character must be ','. */
10073 if (arg != NULL)
10074 {
10075 if (arg[0] != ',' || arg[1] == '\0')
10076 error (_("Format string required"));
10077 else
10078 {
10079 /* Skip the comma. */
10080 ++arg;
10081 }
10082 }
10083
e7e0cddf 10084 create_breakpoint (get_current_arch (),
ffc2605c 10085 location.get (),
f00aae0f 10086 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10087 0, bp_dprintf,
10088 0 /* Ignore count */,
10089 pending_break_support,
10090 &dprintf_breakpoint_ops,
10091 from_tty,
10092 1 /* enabled */,
10093 0 /* internal */,
10094 0);
10095}
10096
d3ce09f5
SS
10097static void
10098agent_printf_command (char *arg, int from_tty)
10099{
10100 error (_("May only run agent-printf on the target"));
10101}
10102
f1310107
TJB
10103/* Implement the "breakpoint_hit" breakpoint_ops method for
10104 ranged breakpoints. */
10105
10106static int
10107breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10108 struct address_space *aspace,
09ac7c10
TT
10109 CORE_ADDR bp_addr,
10110 const struct target_waitstatus *ws)
f1310107 10111{
09ac7c10 10112 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10113 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10114 return 0;
10115
f1310107
TJB
10116 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10117 bl->length, aspace, bp_addr);
10118}
10119
10120/* Implement the "resources_needed" breakpoint_ops method for
10121 ranged breakpoints. */
10122
10123static int
10124resources_needed_ranged_breakpoint (const struct bp_location *bl)
10125{
10126 return target_ranged_break_num_registers ();
10127}
10128
10129/* Implement the "print_it" breakpoint_ops method for
10130 ranged breakpoints. */
10131
10132static enum print_stop_action
348d480f 10133print_it_ranged_breakpoint (bpstat bs)
f1310107 10134{
348d480f 10135 struct breakpoint *b = bs->breakpoint_at;
f1310107 10136 struct bp_location *bl = b->loc;
79a45e25 10137 struct ui_out *uiout = current_uiout;
f1310107
TJB
10138
10139 gdb_assert (b->type == bp_hardware_breakpoint);
10140
10141 /* Ranged breakpoints have only one location. */
10142 gdb_assert (bl && bl->next == NULL);
10143
10144 annotate_breakpoint (b->number);
f303dbd6
PA
10145
10146 maybe_print_thread_hit_breakpoint (uiout);
10147
f1310107 10148 if (b->disposition == disp_del)
112e8700 10149 uiout->text ("Temporary ranged breakpoint ");
f1310107 10150 else
112e8700
SM
10151 uiout->text ("Ranged breakpoint ");
10152 if (uiout->is_mi_like_p ())
f1310107 10153 {
112e8700 10154 uiout->field_string ("reason",
f1310107 10155 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10156 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10157 }
112e8700
SM
10158 uiout->field_int ("bkptno", b->number);
10159 uiout->text (", ");
f1310107
TJB
10160
10161 return PRINT_SRC_AND_LOC;
10162}
10163
10164/* Implement the "print_one" breakpoint_ops method for
10165 ranged breakpoints. */
10166
10167static void
10168print_one_ranged_breakpoint (struct breakpoint *b,
10169 struct bp_location **last_loc)
10170{
10171 struct bp_location *bl = b->loc;
10172 struct value_print_options opts;
79a45e25 10173 struct ui_out *uiout = current_uiout;
f1310107
TJB
10174
10175 /* Ranged breakpoints have only one location. */
10176 gdb_assert (bl && bl->next == NULL);
10177
10178 get_user_print_options (&opts);
10179
10180 if (opts.addressprint)
10181 /* We don't print the address range here, it will be printed later
10182 by print_one_detail_ranged_breakpoint. */
112e8700 10183 uiout->field_skip ("addr");
f1310107
TJB
10184 annotate_field (5);
10185 print_breakpoint_location (b, bl);
10186 *last_loc = bl;
10187}
10188
10189/* Implement the "print_one_detail" breakpoint_ops method for
10190 ranged breakpoints. */
10191
10192static void
10193print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10194 struct ui_out *uiout)
10195{
10196 CORE_ADDR address_start, address_end;
10197 struct bp_location *bl = b->loc;
d7e74731 10198 string_file stb;
f1310107
TJB
10199
10200 gdb_assert (bl);
10201
10202 address_start = bl->address;
10203 address_end = address_start + bl->length - 1;
10204
112e8700 10205 uiout->text ("\taddress range: ");
d7e74731
PA
10206 stb.printf ("[%s, %s]",
10207 print_core_address (bl->gdbarch, address_start),
10208 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10209 uiout->field_stream ("addr", stb);
10210 uiout->text ("\n");
f1310107
TJB
10211}
10212
10213/* Implement the "print_mention" breakpoint_ops method for
10214 ranged breakpoints. */
10215
10216static void
10217print_mention_ranged_breakpoint (struct breakpoint *b)
10218{
10219 struct bp_location *bl = b->loc;
79a45e25 10220 struct ui_out *uiout = current_uiout;
f1310107
TJB
10221
10222 gdb_assert (bl);
10223 gdb_assert (b->type == bp_hardware_breakpoint);
10224
112e8700 10225 if (uiout->is_mi_like_p ())
f1310107
TJB
10226 return;
10227
10228 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10229 b->number, paddress (bl->gdbarch, bl->address),
10230 paddress (bl->gdbarch, bl->address + bl->length - 1));
10231}
10232
10233/* Implement the "print_recreate" breakpoint_ops method for
10234 ranged breakpoints. */
10235
10236static void
10237print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10238{
f00aae0f 10239 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10240 event_location_to_string (b->location.get ()),
10241 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10242 print_recreate_thread (b, fp);
f1310107
TJB
10243}
10244
10245/* The breakpoint_ops structure to be used in ranged breakpoints. */
10246
2060206e 10247static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10248
10249/* Find the address where the end of the breakpoint range should be
10250 placed, given the SAL of the end of the range. This is so that if
10251 the user provides a line number, the end of the range is set to the
10252 last instruction of the given line. */
10253
10254static CORE_ADDR
10255find_breakpoint_range_end (struct symtab_and_line sal)
10256{
10257 CORE_ADDR end;
10258
10259 /* If the user provided a PC value, use it. Otherwise,
10260 find the address of the end of the given location. */
10261 if (sal.explicit_pc)
10262 end = sal.pc;
10263 else
10264 {
10265 int ret;
10266 CORE_ADDR start;
10267
10268 ret = find_line_pc_range (sal, &start, &end);
10269 if (!ret)
10270 error (_("Could not find location of the end of the range."));
10271
10272 /* find_line_pc_range returns the start of the next line. */
10273 end--;
10274 }
10275
10276 return end;
10277}
10278
10279/* Implement the "break-range" CLI command. */
10280
10281static void
10282break_range_command (char *arg, int from_tty)
10283{
870f88f7 10284 char *arg_start, *addr_string_start;
f1310107
TJB
10285 struct linespec_result canonical_start, canonical_end;
10286 int bp_count, can_use_bp, length;
10287 CORE_ADDR end;
10288 struct breakpoint *b;
10289 struct symtab_and_line sal_start, sal_end;
f1310107 10290 struct cleanup *cleanup_bkpt;
f8eba3c6 10291 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10292
10293 /* We don't support software ranged breakpoints. */
10294 if (target_ranged_break_num_registers () < 0)
10295 error (_("This target does not support hardware ranged breakpoints."));
10296
10297 bp_count = hw_breakpoint_used_count ();
10298 bp_count += target_ranged_break_num_registers ();
10299 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10300 bp_count, 0);
10301 if (can_use_bp < 0)
10302 error (_("Hardware breakpoints used exceeds limit."));
10303
f8eba3c6 10304 arg = skip_spaces (arg);
f1310107
TJB
10305 if (arg == NULL || arg[0] == '\0')
10306 error(_("No address range specified."));
10307
f8eba3c6 10308 arg_start = arg;
ffc2605c
TT
10309 event_location_up start_location = string_to_event_location (&arg,
10310 current_language);
10311 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10312
10313 if (arg[0] != ',')
10314 error (_("Too few arguments."));
f8eba3c6 10315 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10316 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10317
10318 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10319
10320 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10321 || lsal_start->sals.nelts != 1)
f1310107
TJB
10322 error (_("Cannot create a ranged breakpoint with multiple locations."));
10323
f8eba3c6
TT
10324 sal_start = lsal_start->sals.sals[0];
10325 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10326 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10327
10328 arg++; /* Skip the comma. */
f8eba3c6 10329 arg = skip_spaces (arg);
f1310107
TJB
10330
10331 /* Parse the end location. */
10332
f1310107
TJB
10333 arg_start = arg;
10334
f8eba3c6 10335 /* We call decode_line_full directly here instead of using
f1310107
TJB
10336 parse_breakpoint_sals because we need to specify the start location's
10337 symtab and line as the default symtab and line for the end of the
10338 range. This makes it possible to have ranges like "foo.c:27, +14",
10339 where +14 means 14 lines from the start location. */
ffc2605c
TT
10340 event_location_up end_location = string_to_event_location (&arg,
10341 current_language);
10342 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10343 sal_start.symtab, sal_start.line,
10344 &canonical_end, NULL, NULL);
10345
f8eba3c6 10346 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10347 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10348
10349 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10350 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10351 || lsal_end->sals.nelts != 1)
f1310107
TJB
10352 error (_("Cannot create a ranged breakpoint with multiple locations."));
10353
f8eba3c6 10354 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10355
10356 end = find_breakpoint_range_end (sal_end);
10357 if (sal_start.pc > end)
177b42fe 10358 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10359
10360 length = end - sal_start.pc + 1;
10361 if (length < 0)
10362 /* Length overflowed. */
10363 error (_("Address range too large."));
10364 else if (length == 1)
10365 {
10366 /* This range is simple enough to be handled by
10367 the `hbreak' command. */
10368 hbreak_command (addr_string_start, 1);
10369
10370 do_cleanups (cleanup_bkpt);
10371
10372 return;
10373 }
10374
10375 /* Now set up the breakpoint. */
10376 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10377 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10378 set_breakpoint_count (breakpoint_count + 1);
10379 b->number = breakpoint_count;
10380 b->disposition = disp_donttouch;
d28cd78a
TT
10381 b->location = std::move (start_location);
10382 b->location_range_end = std::move (end_location);
f1310107
TJB
10383 b->loc->length = length;
10384
f8eba3c6 10385 do_cleanups (cleanup_bkpt);
f1310107
TJB
10386
10387 mention (b);
8d3788bd 10388 observer_notify_breakpoint_created (b);
44702360 10389 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10390}
10391
4a64f543
MS
10392/* Return non-zero if EXP is verified as constant. Returned zero
10393 means EXP is variable. Also the constant detection may fail for
10394 some constant expressions and in such case still falsely return
10395 zero. */
2e6e3d9c 10396
65d79d4b
SDJ
10397static int
10398watchpoint_exp_is_const (const struct expression *exp)
10399{
10400 int i = exp->nelts;
10401
10402 while (i > 0)
10403 {
10404 int oplenp, argsp;
10405
10406 /* We are only interested in the descriptor of each element. */
10407 operator_length (exp, i, &oplenp, &argsp);
10408 i -= oplenp;
10409
10410 switch (exp->elts[i].opcode)
10411 {
10412 case BINOP_ADD:
10413 case BINOP_SUB:
10414 case BINOP_MUL:
10415 case BINOP_DIV:
10416 case BINOP_REM:
10417 case BINOP_MOD:
10418 case BINOP_LSH:
10419 case BINOP_RSH:
10420 case BINOP_LOGICAL_AND:
10421 case BINOP_LOGICAL_OR:
10422 case BINOP_BITWISE_AND:
10423 case BINOP_BITWISE_IOR:
10424 case BINOP_BITWISE_XOR:
10425 case BINOP_EQUAL:
10426 case BINOP_NOTEQUAL:
10427 case BINOP_LESS:
10428 case BINOP_GTR:
10429 case BINOP_LEQ:
10430 case BINOP_GEQ:
10431 case BINOP_REPEAT:
10432 case BINOP_COMMA:
10433 case BINOP_EXP:
10434 case BINOP_MIN:
10435 case BINOP_MAX:
10436 case BINOP_INTDIV:
10437 case BINOP_CONCAT:
65d79d4b
SDJ
10438 case TERNOP_COND:
10439 case TERNOP_SLICE:
65d79d4b
SDJ
10440
10441 case OP_LONG:
10442 case OP_DOUBLE:
10443 case OP_DECFLOAT:
10444 case OP_LAST:
10445 case OP_COMPLEX:
10446 case OP_STRING:
65d79d4b
SDJ
10447 case OP_ARRAY:
10448 case OP_TYPE:
608b4967
TT
10449 case OP_TYPEOF:
10450 case OP_DECLTYPE:
6e72ca20 10451 case OP_TYPEID:
65d79d4b
SDJ
10452 case OP_NAME:
10453 case OP_OBJC_NSSTRING:
10454
10455 case UNOP_NEG:
10456 case UNOP_LOGICAL_NOT:
10457 case UNOP_COMPLEMENT:
10458 case UNOP_ADDR:
10459 case UNOP_HIGH:
aeaa2474 10460 case UNOP_CAST:
9eaf6705
TT
10461
10462 case UNOP_CAST_TYPE:
10463 case UNOP_REINTERPRET_CAST:
10464 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10465 /* Unary, binary and ternary operators: We have to check
10466 their operands. If they are constant, then so is the
10467 result of that operation. For instance, if A and B are
10468 determined to be constants, then so is "A + B".
10469
10470 UNOP_IND is one exception to the rule above, because the
10471 value of *ADDR is not necessarily a constant, even when
10472 ADDR is. */
65d79d4b
SDJ
10473 break;
10474
10475 case OP_VAR_VALUE:
10476 /* Check whether the associated symbol is a constant.
4a64f543 10477
65d79d4b 10478 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10479 possible that a buggy compiler could mark a variable as
10480 constant even when it is not, and TYPE_CONST would return
10481 true in this case, while SYMBOL_CLASS wouldn't.
10482
10483 We also have to check for function symbols because they
10484 are always constant. */
65d79d4b
SDJ
10485 {
10486 struct symbol *s = exp->elts[i + 2].symbol;
10487
10488 if (SYMBOL_CLASS (s) != LOC_BLOCK
10489 && SYMBOL_CLASS (s) != LOC_CONST
10490 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10491 return 0;
10492 break;
10493 }
10494
10495 /* The default action is to return 0 because we are using
10496 the optimistic approach here: If we don't know something,
10497 then it is not a constant. */
10498 default:
10499 return 0;
10500 }
10501 }
10502
10503 return 1;
10504}
10505
3a5c3e22
PA
10506/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10507
10508static void
10509dtor_watchpoint (struct breakpoint *self)
10510{
10511 struct watchpoint *w = (struct watchpoint *) self;
10512
3a5c3e22
PA
10513 xfree (w->exp_string);
10514 xfree (w->exp_string_reparse);
10515 value_free (w->val);
10516
10517 base_breakpoint_ops.dtor (self);
10518}
10519
348d480f
PA
10520/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10521
10522static void
10523re_set_watchpoint (struct breakpoint *b)
10524{
3a5c3e22
PA
10525 struct watchpoint *w = (struct watchpoint *) b;
10526
348d480f
PA
10527 /* Watchpoint can be either on expression using entirely global
10528 variables, or it can be on local variables.
10529
10530 Watchpoints of the first kind are never auto-deleted, and even
10531 persist across program restarts. Since they can use variables
10532 from shared libraries, we need to reparse expression as libraries
10533 are loaded and unloaded.
10534
10535 Watchpoints on local variables can also change meaning as result
10536 of solib event. For example, if a watchpoint uses both a local
10537 and a global variables in expression, it's a local watchpoint,
10538 but unloading of a shared library will make the expression
10539 invalid. This is not a very common use case, but we still
10540 re-evaluate expression, to avoid surprises to the user.
10541
10542 Note that for local watchpoints, we re-evaluate it only if
10543 watchpoints frame id is still valid. If it's not, it means the
10544 watchpoint is out of scope and will be deleted soon. In fact,
10545 I'm not sure we'll ever be called in this case.
10546
10547 If a local watchpoint's frame id is still valid, then
3a5c3e22 10548 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10549
3a5c3e22
PA
10550 Don't do anything about disabled watchpoints, since they will be
10551 reevaluated again when enabled. */
10552 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10553}
10554
77b06cd7
TJB
10555/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10556
10557static int
10558insert_watchpoint (struct bp_location *bl)
10559{
3a5c3e22
PA
10560 struct watchpoint *w = (struct watchpoint *) bl->owner;
10561 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10562
10563 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10564 w->cond_exp.get ());
77b06cd7
TJB
10565}
10566
10567/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10568
10569static int
73971819 10570remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10571{
3a5c3e22
PA
10572 struct watchpoint *w = (struct watchpoint *) bl->owner;
10573 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10574
10575 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10576 w->cond_exp.get ());
e09342b5
TJB
10577}
10578
e09342b5 10579static int
348d480f 10580breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10581 struct address_space *aspace, CORE_ADDR bp_addr,
10582 const struct target_waitstatus *ws)
e09342b5 10583{
348d480f 10584 struct breakpoint *b = bl->owner;
3a5c3e22 10585 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10586
348d480f
PA
10587 /* Continuable hardware watchpoints are treated as non-existent if the
10588 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10589 some data address). Otherwise gdb won't stop on a break instruction
10590 in the code (not from a breakpoint) when a hardware watchpoint has
10591 been defined. Also skip watchpoints which we know did not trigger
10592 (did not match the data address). */
10593 if (is_hardware_watchpoint (b)
3a5c3e22 10594 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10595 return 0;
9c06b0b4 10596
348d480f 10597 return 1;
9c06b0b4
TJB
10598}
10599
348d480f
PA
10600static void
10601check_status_watchpoint (bpstat bs)
9c06b0b4 10602{
348d480f 10603 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10604
348d480f 10605 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10606}
10607
10608/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10609 hardware watchpoints. */
9c06b0b4
TJB
10610
10611static int
348d480f 10612resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10613{
3a5c3e22
PA
10614 struct watchpoint *w = (struct watchpoint *) bl->owner;
10615 int length = w->exact? 1 : bl->length;
348d480f
PA
10616
10617 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10618}
10619
10620/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10621 hardware watchpoints. */
9c06b0b4
TJB
10622
10623static int
348d480f 10624works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10625{
efa80663
PA
10626 /* Read and access watchpoints only work with hardware support. */
10627 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10628}
10629
9c06b0b4 10630static enum print_stop_action
348d480f 10631print_it_watchpoint (bpstat bs)
9c06b0b4 10632{
348d480f
PA
10633 struct cleanup *old_chain;
10634 struct breakpoint *b;
348d480f 10635 enum print_stop_action result;
3a5c3e22 10636 struct watchpoint *w;
79a45e25 10637 struct ui_out *uiout = current_uiout;
348d480f
PA
10638
10639 gdb_assert (bs->bp_location_at != NULL);
10640
348d480f 10641 b = bs->breakpoint_at;
3a5c3e22 10642 w = (struct watchpoint *) b;
348d480f 10643
d7e74731 10644 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10645
f303dbd6
PA
10646 annotate_watchpoint (b->number);
10647 maybe_print_thread_hit_breakpoint (uiout);
10648
d7e74731
PA
10649 string_file stb;
10650
9c06b0b4
TJB
10651 switch (b->type)
10652 {
348d480f 10653 case bp_watchpoint:
9c06b0b4 10654 case bp_hardware_watchpoint:
112e8700
SM
10655 if (uiout->is_mi_like_p ())
10656 uiout->field_string
10657 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10658 mention (b);
10659 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10660 uiout->text ("\nOld value = ");
d7e74731 10661 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10662 uiout->field_stream ("old", stb);
10663 uiout->text ("\nNew value = ");
d7e74731 10664 watchpoint_value_print (w->val, &stb);
112e8700
SM
10665 uiout->field_stream ("new", stb);
10666 uiout->text ("\n");
348d480f
PA
10667 /* More than one watchpoint may have been triggered. */
10668 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10669 break;
10670
10671 case bp_read_watchpoint:
112e8700
SM
10672 if (uiout->is_mi_like_p ())
10673 uiout->field_string
10674 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10675 mention (b);
10676 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10677 uiout->text ("\nValue = ");
d7e74731 10678 watchpoint_value_print (w->val, &stb);
112e8700
SM
10679 uiout->field_stream ("value", stb);
10680 uiout->text ("\n");
348d480f 10681 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10682 break;
10683
10684 case bp_access_watchpoint:
348d480f
PA
10685 if (bs->old_val != NULL)
10686 {
112e8700
SM
10687 if (uiout->is_mi_like_p ())
10688 uiout->field_string
10689 ("reason",
348d480f
PA
10690 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10691 mention (b);
10692 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10693 uiout->text ("\nOld value = ");
d7e74731 10694 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10695 uiout->field_stream ("old", stb);
10696 uiout->text ("\nNew value = ");
348d480f
PA
10697 }
10698 else
10699 {
10700 mention (b);
112e8700
SM
10701 if (uiout->is_mi_like_p ())
10702 uiout->field_string
10703 ("reason",
348d480f
PA
10704 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10705 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10706 uiout->text ("\nValue = ");
348d480f 10707 }
d7e74731 10708 watchpoint_value_print (w->val, &stb);
112e8700
SM
10709 uiout->field_stream ("new", stb);
10710 uiout->text ("\n");
348d480f 10711 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10712 break;
10713 default:
348d480f 10714 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10715 }
10716
348d480f
PA
10717 do_cleanups (old_chain);
10718 return result;
10719}
10720
10721/* Implement the "print_mention" breakpoint_ops method for hardware
10722 watchpoints. */
10723
10724static void
10725print_mention_watchpoint (struct breakpoint *b)
10726{
3a5c3e22 10727 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10728 struct ui_out *uiout = current_uiout;
46b9c129 10729 const char *tuple_name;
348d480f
PA
10730
10731 switch (b->type)
10732 {
10733 case bp_watchpoint:
112e8700 10734 uiout->text ("Watchpoint ");
46b9c129 10735 tuple_name = "wpt";
348d480f
PA
10736 break;
10737 case bp_hardware_watchpoint:
112e8700 10738 uiout->text ("Hardware watchpoint ");
46b9c129 10739 tuple_name = "wpt";
348d480f
PA
10740 break;
10741 case bp_read_watchpoint:
112e8700 10742 uiout->text ("Hardware read watchpoint ");
46b9c129 10743 tuple_name = "hw-rwpt";
348d480f
PA
10744 break;
10745 case bp_access_watchpoint:
112e8700 10746 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10747 tuple_name = "hw-awpt";
348d480f
PA
10748 break;
10749 default:
10750 internal_error (__FILE__, __LINE__,
10751 _("Invalid hardware watchpoint type."));
10752 }
10753
46b9c129 10754 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10755 uiout->field_int ("number", b->number);
10756 uiout->text (": ");
10757 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10758}
10759
10760/* Implement the "print_recreate" breakpoint_ops method for
10761 watchpoints. */
10762
10763static void
10764print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10765{
3a5c3e22
PA
10766 struct watchpoint *w = (struct watchpoint *) b;
10767
348d480f
PA
10768 switch (b->type)
10769 {
10770 case bp_watchpoint:
10771 case bp_hardware_watchpoint:
10772 fprintf_unfiltered (fp, "watch");
10773 break;
10774 case bp_read_watchpoint:
10775 fprintf_unfiltered (fp, "rwatch");
10776 break;
10777 case bp_access_watchpoint:
10778 fprintf_unfiltered (fp, "awatch");
10779 break;
10780 default:
10781 internal_error (__FILE__, __LINE__,
10782 _("Invalid watchpoint type."));
10783 }
10784
3a5c3e22 10785 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10786 print_recreate_thread (b, fp);
348d480f
PA
10787}
10788
427cd150
TT
10789/* Implement the "explains_signal" breakpoint_ops method for
10790 watchpoints. */
10791
47591c29 10792static int
427cd150
TT
10793explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10794{
10795 /* A software watchpoint cannot cause a signal other than
10796 GDB_SIGNAL_TRAP. */
10797 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10798 return 0;
427cd150 10799
47591c29 10800 return 1;
427cd150
TT
10801}
10802
348d480f
PA
10803/* The breakpoint_ops structure to be used in hardware watchpoints. */
10804
2060206e 10805static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10806
10807/* Implement the "insert" breakpoint_ops method for
10808 masked hardware watchpoints. */
10809
10810static int
10811insert_masked_watchpoint (struct bp_location *bl)
10812{
3a5c3e22
PA
10813 struct watchpoint *w = (struct watchpoint *) bl->owner;
10814
10815 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10816 bl->watchpoint_type);
10817}
10818
10819/* Implement the "remove" breakpoint_ops method for
10820 masked hardware watchpoints. */
10821
10822static int
73971819 10823remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10824{
3a5c3e22
PA
10825 struct watchpoint *w = (struct watchpoint *) bl->owner;
10826
10827 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10828 bl->watchpoint_type);
10829}
10830
10831/* Implement the "resources_needed" breakpoint_ops method for
10832 masked hardware watchpoints. */
10833
10834static int
10835resources_needed_masked_watchpoint (const struct bp_location *bl)
10836{
3a5c3e22
PA
10837 struct watchpoint *w = (struct watchpoint *) bl->owner;
10838
10839 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10840}
10841
10842/* Implement the "works_in_software_mode" breakpoint_ops method for
10843 masked hardware watchpoints. */
10844
10845static int
10846works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10847{
10848 return 0;
10849}
10850
10851/* Implement the "print_it" breakpoint_ops method for
10852 masked hardware watchpoints. */
10853
10854static enum print_stop_action
10855print_it_masked_watchpoint (bpstat bs)
10856{
10857 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10858 struct ui_out *uiout = current_uiout;
348d480f
PA
10859
10860 /* Masked watchpoints have only one location. */
10861 gdb_assert (b->loc && b->loc->next == NULL);
10862
f303dbd6
PA
10863 annotate_watchpoint (b->number);
10864 maybe_print_thread_hit_breakpoint (uiout);
10865
348d480f
PA
10866 switch (b->type)
10867 {
10868 case bp_hardware_watchpoint:
112e8700
SM
10869 if (uiout->is_mi_like_p ())
10870 uiout->field_string
10871 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10872 break;
10873
10874 case bp_read_watchpoint:
112e8700
SM
10875 if (uiout->is_mi_like_p ())
10876 uiout->field_string
10877 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10878 break;
10879
10880 case bp_access_watchpoint:
112e8700
SM
10881 if (uiout->is_mi_like_p ())
10882 uiout->field_string
10883 ("reason",
348d480f
PA
10884 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10885 break;
10886 default:
10887 internal_error (__FILE__, __LINE__,
10888 _("Invalid hardware watchpoint type."));
10889 }
10890
10891 mention (b);
112e8700 10892 uiout->text (_("\n\
9c06b0b4
TJB
10893Check the underlying instruction at PC for the memory\n\
10894address and value which triggered this watchpoint.\n"));
112e8700 10895 uiout->text ("\n");
9c06b0b4
TJB
10896
10897 /* More than one watchpoint may have been triggered. */
10898 return PRINT_UNKNOWN;
10899}
10900
10901/* Implement the "print_one_detail" breakpoint_ops method for
10902 masked hardware watchpoints. */
10903
10904static void
10905print_one_detail_masked_watchpoint (const struct breakpoint *b,
10906 struct ui_out *uiout)
10907{
3a5c3e22
PA
10908 struct watchpoint *w = (struct watchpoint *) b;
10909
9c06b0b4
TJB
10910 /* Masked watchpoints have only one location. */
10911 gdb_assert (b->loc && b->loc->next == NULL);
10912
112e8700
SM
10913 uiout->text ("\tmask ");
10914 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10915 uiout->text ("\n");
9c06b0b4
TJB
10916}
10917
10918/* Implement the "print_mention" breakpoint_ops method for
10919 masked hardware watchpoints. */
10920
10921static void
10922print_mention_masked_watchpoint (struct breakpoint *b)
10923{
3a5c3e22 10924 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10925 struct ui_out *uiout = current_uiout;
46b9c129 10926 const char *tuple_name;
9c06b0b4
TJB
10927
10928 switch (b->type)
10929 {
10930 case bp_hardware_watchpoint:
112e8700 10931 uiout->text ("Masked hardware watchpoint ");
46b9c129 10932 tuple_name = "wpt";
9c06b0b4
TJB
10933 break;
10934 case bp_read_watchpoint:
112e8700 10935 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10936 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10937 break;
10938 case bp_access_watchpoint:
112e8700 10939 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10940 tuple_name = "hw-awpt";
9c06b0b4
TJB
10941 break;
10942 default:
10943 internal_error (__FILE__, __LINE__,
10944 _("Invalid hardware watchpoint type."));
10945 }
10946
46b9c129 10947 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10948 uiout->field_int ("number", b->number);
10949 uiout->text (": ");
10950 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10951}
10952
10953/* Implement the "print_recreate" breakpoint_ops method for
10954 masked hardware watchpoints. */
10955
10956static void
10957print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10958{
3a5c3e22 10959 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10960 char tmp[40];
10961
10962 switch (b->type)
10963 {
10964 case bp_hardware_watchpoint:
10965 fprintf_unfiltered (fp, "watch");
10966 break;
10967 case bp_read_watchpoint:
10968 fprintf_unfiltered (fp, "rwatch");
10969 break;
10970 case bp_access_watchpoint:
10971 fprintf_unfiltered (fp, "awatch");
10972 break;
10973 default:
10974 internal_error (__FILE__, __LINE__,
10975 _("Invalid hardware watchpoint type."));
10976 }
10977
3a5c3e22
PA
10978 sprintf_vma (tmp, w->hw_wp_mask);
10979 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10980 print_recreate_thread (b, fp);
9c06b0b4
TJB
10981}
10982
10983/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10984
2060206e 10985static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10986
10987/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10988
10989static int
10990is_masked_watchpoint (const struct breakpoint *b)
10991{
10992 return b->ops == &masked_watchpoint_breakpoint_ops;
10993}
10994
53a5351d
JM
10995/* accessflag: hw_write: watch write,
10996 hw_read: watch read,
10997 hw_access: watch access (read or write) */
c906108c 10998static void
bbc13ae3 10999watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11000 int just_location, int internal)
c906108c 11001{
d983da9c 11002 struct breakpoint *b, *scope_breakpoint = NULL;
270140bd 11003 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11004 struct value *val, *mark, *result;
bb9d5f81 11005 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
11006 const char *exp_start = NULL;
11007 const char *exp_end = NULL;
11008 const char *tok, *end_tok;
9c06b0b4 11009 int toklen = -1;
bbc13ae3
KS
11010 const char *cond_start = NULL;
11011 const char *cond_end = NULL;
c906108c 11012 enum bptype bp_type;
37e4754d 11013 int thread = -1;
0cf6dd15 11014 int pc = 0;
9c06b0b4
TJB
11015 /* Flag to indicate whether we are going to use masks for
11016 the hardware watchpoint. */
11017 int use_mask = 0;
11018 CORE_ADDR mask = 0;
3a5c3e22 11019 struct watchpoint *w;
bbc13ae3
KS
11020 char *expression;
11021 struct cleanup *back_to;
c906108c 11022
37e4754d
LM
11023 /* Make sure that we actually have parameters to parse. */
11024 if (arg != NULL && arg[0] != '\0')
11025 {
bbc13ae3
KS
11026 const char *value_start;
11027
11028 exp_end = arg + strlen (arg);
37e4754d 11029
9c06b0b4
TJB
11030 /* Look for "parameter value" pairs at the end
11031 of the arguments string. */
bbc13ae3 11032 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11033 {
11034 /* Skip whitespace at the end of the argument list. */
11035 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11036 tok--;
11037
11038 /* Find the beginning of the last token.
11039 This is the value of the parameter. */
11040 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11041 tok--;
11042 value_start = tok + 1;
11043
11044 /* Skip whitespace. */
11045 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11046 tok--;
11047
11048 end_tok = tok;
11049
11050 /* Find the beginning of the second to last token.
11051 This is the parameter itself. */
11052 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11053 tok--;
11054 tok++;
11055 toklen = end_tok - tok + 1;
11056
61012eef 11057 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11058 {
5d5658a1 11059 struct thread_info *thr;
9c06b0b4
TJB
11060 /* At this point we've found a "thread" token, which means
11061 the user is trying to set a watchpoint that triggers
11062 only in a specific thread. */
5d5658a1 11063 const char *endp;
37e4754d 11064
9c06b0b4
TJB
11065 if (thread != -1)
11066 error(_("You can specify only one thread."));
37e4754d 11067
9c06b0b4 11068 /* Extract the thread ID from the next token. */
5d5658a1 11069 thr = parse_thread_id (value_start, &endp);
37e4754d 11070
5d5658a1 11071 /* Check if the user provided a valid thread ID. */
9c06b0b4 11072 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11073 invalid_thread_id_error (value_start);
9c06b0b4 11074
5d5658a1 11075 thread = thr->global_num;
9c06b0b4 11076 }
61012eef 11077 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11078 {
11079 /* We've found a "mask" token, which means the user wants to
11080 create a hardware watchpoint that is going to have the mask
11081 facility. */
11082 struct value *mask_value, *mark;
37e4754d 11083
9c06b0b4
TJB
11084 if (use_mask)
11085 error(_("You can specify only one mask."));
37e4754d 11086
9c06b0b4 11087 use_mask = just_location = 1;
37e4754d 11088
9c06b0b4
TJB
11089 mark = value_mark ();
11090 mask_value = parse_to_comma_and_eval (&value_start);
11091 mask = value_as_address (mask_value);
11092 value_free_to_mark (mark);
11093 }
11094 else
11095 /* We didn't recognize what we found. We should stop here. */
11096 break;
37e4754d 11097
9c06b0b4
TJB
11098 /* Truncate the string and get rid of the "parameter value" pair before
11099 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11100 exp_end = tok;
9c06b0b4 11101 }
37e4754d 11102 }
bbc13ae3
KS
11103 else
11104 exp_end = arg;
37e4754d 11105
bbc13ae3
KS
11106 /* Parse the rest of the arguments. From here on out, everything
11107 is in terms of a newly allocated string instead of the original
11108 ARG. */
c906108c 11109 innermost_block = NULL;
bbc13ae3
KS
11110 expression = savestring (arg, exp_end - arg);
11111 back_to = make_cleanup (xfree, expression);
11112 exp_start = arg = expression;
4d01a485 11113 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11114 exp_end = arg;
fa8a61dc
TT
11115 /* Remove trailing whitespace from the expression before saving it.
11116 This makes the eventual display of the expression string a bit
11117 prettier. */
11118 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11119 --exp_end;
11120
65d79d4b 11121 /* Checking if the expression is not constant. */
4d01a485 11122 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11123 {
11124 int len;
11125
11126 len = exp_end - exp_start;
11127 while (len > 0 && isspace (exp_start[len - 1]))
11128 len--;
11129 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11130 }
11131
c906108c
SS
11132 exp_valid_block = innermost_block;
11133 mark = value_mark ();
4d01a485 11134 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11135
bb9d5f81
PP
11136 if (val != NULL && just_location)
11137 {
11138 saved_bitpos = value_bitpos (val);
11139 saved_bitsize = value_bitsize (val);
11140 }
11141
06a64a0b
TT
11142 if (just_location)
11143 {
9c06b0b4
TJB
11144 int ret;
11145
06a64a0b 11146 exp_valid_block = NULL;
a1442452 11147 val = value_addr (result);
06a64a0b
TT
11148 release_value (val);
11149 value_free_to_mark (mark);
9c06b0b4
TJB
11150
11151 if (use_mask)
11152 {
11153 ret = target_masked_watch_num_registers (value_as_address (val),
11154 mask);
11155 if (ret == -1)
11156 error (_("This target does not support masked watchpoints."));
11157 else if (ret == -2)
11158 error (_("Invalid mask or memory region."));
11159 }
06a64a0b
TT
11160 }
11161 else if (val != NULL)
fa4727a6 11162 release_value (val);
c906108c 11163
bbc13ae3
KS
11164 tok = skip_spaces_const (arg);
11165 end_tok = skip_to_space_const (tok);
c906108c
SS
11166
11167 toklen = end_tok - tok;
11168 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11169 {
60e1c644 11170 innermost_block = NULL;
c906108c 11171 tok = cond_start = end_tok + 1;
4d01a485 11172 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11173
11174 /* The watchpoint expression may not be local, but the condition
11175 may still be. E.g.: `watch global if local > 0'. */
11176 cond_exp_valid_block = innermost_block;
11177
c906108c
SS
11178 cond_end = tok;
11179 }
11180 if (*tok)
8a3fe4f8 11181 error (_("Junk at end of command."));
c906108c 11182
441d7c93
PA
11183 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11184
11185 /* Save this because create_internal_breakpoint below invalidates
11186 'wp_frame'. */
11187 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11188
11189 /* If the expression is "local", then set up a "watchpoint scope"
11190 breakpoint at the point where we've left the scope of the watchpoint
11191 expression. Create the scope breakpoint before the watchpoint, so
11192 that we will encounter it first in bpstat_stop_status. */
441d7c93 11193 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11194 {
441d7c93
PA
11195 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11196
11197 if (frame_id_p (caller_frame_id))
edb3359d 11198 {
441d7c93
PA
11199 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11200 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11201
edb3359d 11202 scope_breakpoint
441d7c93 11203 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11204 bp_watchpoint_scope,
11205 &momentary_breakpoint_ops);
d983da9c 11206
441d7c93
PA
11207 /* create_internal_breakpoint could invalidate WP_FRAME. */
11208 wp_frame = NULL;
11209
edb3359d 11210 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11211
edb3359d
DJ
11212 /* Automatically delete the breakpoint when it hits. */
11213 scope_breakpoint->disposition = disp_del;
d983da9c 11214
edb3359d 11215 /* Only break in the proper frame (help with recursion). */
441d7c93 11216 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11217
edb3359d 11218 /* Set the address at which we will stop. */
441d7c93
PA
11219 scope_breakpoint->loc->gdbarch = caller_arch;
11220 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11221 scope_breakpoint->loc->address
a6d9a66e
UW
11222 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11223 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11224 scope_breakpoint->type);
11225 }
d983da9c
DJ
11226 }
11227
e8369a73
AB
11228 /* Now set up the breakpoint. We create all watchpoints as hardware
11229 watchpoints here even if hardware watchpoints are turned off, a call
11230 to update_watchpoint later in this function will cause the type to
11231 drop back to bp_watchpoint (software watchpoint) if required. */
11232
11233 if (accessflag == hw_read)
11234 bp_type = bp_read_watchpoint;
11235 else if (accessflag == hw_access)
11236 bp_type = bp_access_watchpoint;
11237 else
11238 bp_type = bp_hardware_watchpoint;
3a5c3e22 11239
4d01a485 11240 w = new watchpoint ();
3a5c3e22 11241 b = &w->base;
348d480f 11242 if (use_mask)
3a5c3e22
PA
11243 init_raw_breakpoint_without_location (b, NULL, bp_type,
11244 &masked_watchpoint_breakpoint_ops);
348d480f 11245 else
3a5c3e22
PA
11246 init_raw_breakpoint_without_location (b, NULL, bp_type,
11247 &watchpoint_breakpoint_ops);
37e4754d 11248 b->thread = thread;
b5de0fa7 11249 b->disposition = disp_donttouch;
348d480f 11250 b->pspace = current_program_space;
b22e99fd 11251 w->exp = std::move (exp);
3a5c3e22
PA
11252 w->exp_valid_block = exp_valid_block;
11253 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11254 if (just_location)
11255 {
11256 struct type *t = value_type (val);
11257 CORE_ADDR addr = value_as_address (val);
06a64a0b
TT
11258
11259 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
06a64a0b 11260
2f408ecb
PA
11261 std::string name = type_to_string (t);
11262
11263 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
d63d0675 11264 core_addr_to_string (addr));
06a64a0b 11265
3a5c3e22 11266 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11267 (int) (exp_end - exp_start), exp_start);
11268
06a64a0b
TT
11269 /* The above expression is in C. */
11270 b->language = language_c;
11271 }
11272 else
3a5c3e22 11273 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11274
11275 if (use_mask)
11276 {
3a5c3e22 11277 w->hw_wp_mask = mask;
9c06b0b4
TJB
11278 }
11279 else
11280 {
3a5c3e22 11281 w->val = val;
bb9d5f81
PP
11282 w->val_bitpos = saved_bitpos;
11283 w->val_bitsize = saved_bitsize;
3a5c3e22 11284 w->val_valid = 1;
9c06b0b4 11285 }
77b06cd7 11286
c906108c
SS
11287 if (cond_start)
11288 b->cond_string = savestring (cond_start, cond_end - cond_start);
11289 else
11290 b->cond_string = 0;
c5aa993b 11291
441d7c93 11292 if (frame_id_p (watchpoint_frame))
f6bc2008 11293 {
441d7c93 11294 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11295 w->watchpoint_thread = inferior_ptid;
f6bc2008 11296 }
c906108c 11297 else
f6bc2008 11298 {
3a5c3e22
PA
11299 w->watchpoint_frame = null_frame_id;
11300 w->watchpoint_thread = null_ptid;
f6bc2008 11301 }
c906108c 11302
d983da9c 11303 if (scope_breakpoint != NULL)
c906108c 11304 {
d983da9c
DJ
11305 /* The scope breakpoint is related to the watchpoint. We will
11306 need to act on them together. */
11307 b->related_breakpoint = scope_breakpoint;
11308 scope_breakpoint->related_breakpoint = b;
c906108c 11309 }
d983da9c 11310
06a64a0b
TT
11311 if (!just_location)
11312 value_free_to_mark (mark);
2d134ed3 11313
492d29ea 11314 TRY
a9634178
TJB
11315 {
11316 /* Finally update the new watchpoint. This creates the locations
11317 that should be inserted. */
3a5c3e22 11318 update_watchpoint (w, 1);
a9634178 11319 }
492d29ea 11320 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11321 {
11322 delete_breakpoint (b);
11323 throw_exception (e);
11324 }
492d29ea 11325 END_CATCH
a9634178 11326
3ea46bff 11327 install_breakpoint (internal, b, 1);
bbc13ae3 11328 do_cleanups (back_to);
c906108c
SS
11329}
11330
e09342b5 11331/* Return count of debug registers needed to watch the given expression.
e09342b5 11332 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11333
c906108c 11334static int
a9634178 11335can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11336{
11337 int found_memory_cnt = 0;
2e70b7b9 11338 struct value *head = v;
c906108c
SS
11339
11340 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11341 if (!can_use_hw_watchpoints)
c906108c 11342 return 0;
c5aa993b 11343
5c44784c
JM
11344 /* Make sure that the value of the expression depends only upon
11345 memory contents, and values computed from them within GDB. If we
11346 find any register references or function calls, we can't use a
11347 hardware watchpoint.
11348
11349 The idea here is that evaluating an expression generates a series
11350 of values, one holding the value of every subexpression. (The
11351 expression a*b+c has five subexpressions: a, b, a*b, c, and
11352 a*b+c.) GDB's values hold almost enough information to establish
11353 the criteria given above --- they identify memory lvalues,
11354 register lvalues, computed values, etcetera. So we can evaluate
11355 the expression, and then scan the chain of values that leaves
11356 behind to decide whether we can detect any possible change to the
11357 expression's final value using only hardware watchpoints.
11358
11359 However, I don't think that the values returned by inferior
11360 function calls are special in any way. So this function may not
11361 notice that an expression involving an inferior function call
11362 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11363 for (; v; v = value_next (v))
c906108c 11364 {
5c44784c 11365 if (VALUE_LVAL (v) == lval_memory)
c906108c 11366 {
8464be76
DJ
11367 if (v != head && value_lazy (v))
11368 /* A lazy memory lvalue in the chain is one that GDB never
11369 needed to fetch; we either just used its address (e.g.,
11370 `a' in `a.b') or we never needed it at all (e.g., `a'
11371 in `a,b'). This doesn't apply to HEAD; if that is
11372 lazy then it was not readable, but watch it anyway. */
5c44784c 11373 ;
53a5351d 11374 else
5c44784c
JM
11375 {
11376 /* Ahh, memory we actually used! Check if we can cover
11377 it with hardware watchpoints. */
df407dfe 11378 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11379
11380 /* We only watch structs and arrays if user asked for it
11381 explicitly, never if they just happen to appear in a
11382 middle of some value chain. */
11383 if (v == head
11384 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11385 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11386 {
42ae5230 11387 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11388 int len;
11389 int num_regs;
11390
a9634178 11391 len = (target_exact_watchpoints
e09342b5
TJB
11392 && is_scalar_type_recursive (vtype))?
11393 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11394
e09342b5
TJB
11395 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11396 if (!num_regs)
2e70b7b9
MS
11397 return 0;
11398 else
e09342b5 11399 found_memory_cnt += num_regs;
2e70b7b9 11400 }
5c44784c 11401 }
c5aa993b 11402 }
5086187c
AC
11403 else if (VALUE_LVAL (v) != not_lval
11404 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11405 return 0; /* These are values from the history (e.g., $1). */
5086187c 11406 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11407 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11408 }
11409
11410 /* The expression itself looks suitable for using a hardware
11411 watchpoint, but give the target machine a chance to reject it. */
11412 return found_memory_cnt;
11413}
11414
8b93c638 11415void
84f4c1fe 11416watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11417{
84f4c1fe 11418 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11419}
11420
06a64a0b
TT
11421/* A helper function that looks for the "-location" argument and then
11422 calls watch_command_1. */
11423
11424static void
11425watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11426{
11427 int just_location = 0;
11428
11429 if (arg
11430 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11431 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11432 {
e9cafbcc 11433 arg = skip_spaces (arg);
06a64a0b
TT
11434 just_location = 1;
11435 }
11436
84f4c1fe 11437 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11438}
8926118c 11439
c5aa993b 11440static void
fba45db2 11441watch_command (char *arg, int from_tty)
c906108c 11442{
06a64a0b 11443 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11444}
11445
8b93c638 11446void
84f4c1fe 11447rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11448{
84f4c1fe 11449 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11450}
8926118c 11451
c5aa993b 11452static void
fba45db2 11453rwatch_command (char *arg, int from_tty)
c906108c 11454{
06a64a0b 11455 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11456}
11457
8b93c638 11458void
84f4c1fe 11459awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11460{
84f4c1fe 11461 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11462}
8926118c 11463
c5aa993b 11464static void
fba45db2 11465awatch_command (char *arg, int from_tty)
c906108c 11466{
06a64a0b 11467 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11468}
c906108c 11469\f
c5aa993b 11470
cfc31633
PA
11471/* Data for the FSM that manages the until(location)/advance commands
11472 in infcmd.c. Here because it uses the mechanisms of
11473 breakpoints. */
c906108c 11474
cfc31633 11475struct until_break_fsm
bfec99b2 11476{
cfc31633
PA
11477 /* The base class. */
11478 struct thread_fsm thread_fsm;
11479
11480 /* The thread that as current when the command was executed. */
11481 int thread;
11482
11483 /* The breakpoint set at the destination location. */
11484 struct breakpoint *location_breakpoint;
11485
11486 /* Breakpoint set at the return address in the caller frame. May be
11487 NULL. */
11488 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11489};
11490
8980e177
PA
11491static void until_break_fsm_clean_up (struct thread_fsm *self,
11492 struct thread_info *thread);
11493static int until_break_fsm_should_stop (struct thread_fsm *self,
11494 struct thread_info *thread);
cfc31633
PA
11495static enum async_reply_reason
11496 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11497
11498/* until_break_fsm's vtable. */
11499
11500static struct thread_fsm_ops until_break_fsm_ops =
11501{
11502 NULL, /* dtor */
11503 until_break_fsm_clean_up,
11504 until_break_fsm_should_stop,
11505 NULL, /* return_value */
11506 until_break_fsm_async_reply_reason,
11507};
11508
11509/* Allocate a new until_break_command_fsm. */
11510
11511static struct until_break_fsm *
8980e177 11512new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11513 struct breakpoint *location_breakpoint,
11514 struct breakpoint *caller_breakpoint)
11515{
11516 struct until_break_fsm *sm;
11517
11518 sm = XCNEW (struct until_break_fsm);
8980e177 11519 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11520
11521 sm->thread = thread;
11522 sm->location_breakpoint = location_breakpoint;
11523 sm->caller_breakpoint = caller_breakpoint;
11524
11525 return sm;
11526}
11527
11528/* Implementation of the 'should_stop' FSM method for the
11529 until(location)/advance commands. */
11530
11531static int
8980e177
PA
11532until_break_fsm_should_stop (struct thread_fsm *self,
11533 struct thread_info *tp)
cfc31633
PA
11534{
11535 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11536
11537 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11538 sm->location_breakpoint) != NULL
11539 || (sm->caller_breakpoint != NULL
11540 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11541 sm->caller_breakpoint) != NULL))
11542 thread_fsm_set_finished (self);
11543
11544 return 1;
11545}
11546
11547/* Implementation of the 'clean_up' FSM method for the
11548 until(location)/advance commands. */
11549
c2c6d25f 11550static void
8980e177
PA
11551until_break_fsm_clean_up (struct thread_fsm *self,
11552 struct thread_info *thread)
43ff13b4 11553{
cfc31633 11554 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11555
cfc31633
PA
11556 /* Clean up our temporary breakpoints. */
11557 if (sm->location_breakpoint != NULL)
11558 {
11559 delete_breakpoint (sm->location_breakpoint);
11560 sm->location_breakpoint = NULL;
11561 }
11562 if (sm->caller_breakpoint != NULL)
11563 {
11564 delete_breakpoint (sm->caller_breakpoint);
11565 sm->caller_breakpoint = NULL;
11566 }
11567 delete_longjmp_breakpoint (sm->thread);
11568}
11569
11570/* Implementation of the 'async_reply_reason' FSM method for the
11571 until(location)/advance commands. */
11572
11573static enum async_reply_reason
11574until_break_fsm_async_reply_reason (struct thread_fsm *self)
11575{
11576 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11577}
11578
c906108c 11579void
ae66c1fc 11580until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11581{
11582 struct symtabs_and_lines sals;
11583 struct symtab_and_line sal;
8556afb4
PA
11584 struct frame_info *frame;
11585 struct gdbarch *frame_gdbarch;
11586 struct frame_id stack_frame_id;
11587 struct frame_id caller_frame_id;
cfc31633
PA
11588 struct breakpoint *location_breakpoint;
11589 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11590 struct cleanup *old_chain;
186c406b
TT
11591 int thread;
11592 struct thread_info *tp;
cfc31633 11593 struct until_break_fsm *sm;
c906108c 11594
70509625 11595 clear_proceed_status (0);
c906108c
SS
11596
11597 /* Set a breakpoint where the user wants it and at return from
4a64f543 11598 this function. */
c5aa993b 11599
ffc2605c 11600 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11601
1bfeeb0f 11602 if (last_displayed_sal_is_valid ())
ffc2605c 11603 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11604 get_last_displayed_symtab (),
f8eba3c6 11605 get_last_displayed_line ());
c906108c 11606 else
ffc2605c 11607 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11608 NULL, (struct symtab *) NULL, 0);
c5aa993b 11609
c906108c 11610 if (sals.nelts != 1)
8a3fe4f8 11611 error (_("Couldn't get information on specified line."));
c5aa993b 11612
c906108c 11613 sal = sals.sals[0];
4a64f543 11614 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11615
c906108c 11616 if (*arg)
8a3fe4f8 11617 error (_("Junk at end of arguments."));
c5aa993b 11618
c906108c 11619 resolve_sal_pc (&sal);
c5aa993b 11620
186c406b 11621 tp = inferior_thread ();
5d5658a1 11622 thread = tp->global_num;
186c406b 11623
883bc8d1
PA
11624 old_chain = make_cleanup (null_cleanup, NULL);
11625
8556afb4
PA
11626 /* Note linespec handling above invalidates the frame chain.
11627 Installing a breakpoint also invalidates the frame chain (as it
11628 may need to switch threads), so do any frame handling before
11629 that. */
11630
11631 frame = get_selected_frame (NULL);
11632 frame_gdbarch = get_frame_arch (frame);
11633 stack_frame_id = get_stack_frame_id (frame);
11634 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11635
ae66c1fc
EZ
11636 /* Keep within the current frame, or in frames called by the current
11637 one. */
edb3359d 11638
883bc8d1 11639 if (frame_id_p (caller_frame_id))
c906108c 11640 {
883bc8d1 11641 struct symtab_and_line sal2;
cfc31633 11642 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11643
11644 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11645 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11646 caller_gdbarch = frame_unwind_caller_arch (frame);
11647 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11648 sal2,
11649 caller_frame_id,
11650 bp_until);
11651 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11652
883bc8d1 11653 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11654 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11655 }
c5aa993b 11656
c70a6932
JK
11657 /* set_momentary_breakpoint could invalidate FRAME. */
11658 frame = NULL;
11659
883bc8d1
PA
11660 if (anywhere)
11661 /* If the user told us to continue until a specified location,
11662 we don't specify a frame at which we need to stop. */
cfc31633
PA
11663 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11664 null_frame_id, bp_until);
883bc8d1
PA
11665 else
11666 /* Otherwise, specify the selected frame, because we want to stop
11667 only at the very same frame. */
cfc31633
PA
11668 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11669 stack_frame_id, bp_until);
11670 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11671
8980e177 11672 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11673 location_breakpoint, caller_breakpoint);
cfc31633 11674 tp->thread_fsm = &sm->thread_fsm;
f107f563 11675
cfc31633 11676 discard_cleanups (old_chain);
f107f563 11677
cfc31633 11678 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11679}
ae66c1fc 11680
c906108c
SS
11681/* This function attempts to parse an optional "if <cond>" clause
11682 from the arg string. If one is not found, it returns NULL.
c5aa993b 11683
c906108c
SS
11684 Else, it returns a pointer to the condition string. (It does not
11685 attempt to evaluate the string against a particular block.) And,
11686 it updates arg to point to the first character following the parsed
4a64f543 11687 if clause in the arg string. */
53a5351d 11688
63160a43
PA
11689const char *
11690ep_parse_optional_if_clause (const char **arg)
c906108c 11691{
63160a43 11692 const char *cond_string;
c5aa993b
JM
11693
11694 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11695 return NULL;
c5aa993b 11696
4a64f543 11697 /* Skip the "if" keyword. */
c906108c 11698 (*arg) += 2;
c5aa993b 11699
c906108c 11700 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11701 condition string. */
63160a43 11702 *arg = skip_spaces_const (*arg);
c906108c 11703 cond_string = *arg;
c5aa993b 11704
4a64f543
MS
11705 /* Assume that the condition occupies the remainder of the arg
11706 string. */
c906108c 11707 (*arg) += strlen (cond_string);
c5aa993b 11708
c906108c
SS
11709 return cond_string;
11710}
c5aa993b 11711
c906108c
SS
11712/* Commands to deal with catching events, such as signals, exceptions,
11713 process start/exit, etc. */
c5aa993b
JM
11714
11715typedef enum
11716{
44feb3ce
TT
11717 catch_fork_temporary, catch_vfork_temporary,
11718 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11719}
11720catch_fork_kind;
11721
c906108c 11722static void
63160a43 11723catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11724 struct cmd_list_element *command)
c906108c 11725{
63160a43 11726 const char *arg = arg_entry;
a6d9a66e 11727 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11728 const char *cond_string = NULL;
44feb3ce
TT
11729 catch_fork_kind fork_kind;
11730 int tempflag;
11731
11732 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11733 tempflag = (fork_kind == catch_fork_temporary
11734 || fork_kind == catch_vfork_temporary);
c5aa993b 11735
44feb3ce
TT
11736 if (!arg)
11737 arg = "";
63160a43 11738 arg = skip_spaces_const (arg);
c5aa993b 11739
c906108c 11740 /* The allowed syntax is:
c5aa993b
JM
11741 catch [v]fork
11742 catch [v]fork if <cond>
11743
4a64f543 11744 First, check if there's an if clause. */
c906108c 11745 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11746
c906108c 11747 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11748 error (_("Junk at end of arguments."));
c5aa993b 11749
c906108c 11750 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11751 and enable reporting of such events. */
c5aa993b
JM
11752 switch (fork_kind)
11753 {
44feb3ce
TT
11754 case catch_fork_temporary:
11755 case catch_fork_permanent:
a6d9a66e 11756 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11757 &catch_fork_breakpoint_ops);
c906108c 11758 break;
44feb3ce
TT
11759 case catch_vfork_temporary:
11760 case catch_vfork_permanent:
a6d9a66e 11761 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11762 &catch_vfork_breakpoint_ops);
c906108c 11763 break;
c5aa993b 11764 default:
8a3fe4f8 11765 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11766 break;
c5aa993b 11767 }
c906108c
SS
11768}
11769
11770static void
63160a43 11771catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11772 struct cmd_list_element *command)
c906108c 11773{
63160a43 11774 const char *arg = arg_entry;
b4d90040 11775 struct exec_catchpoint *c;
a6d9a66e 11776 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11777 int tempflag;
63160a43 11778 const char *cond_string = NULL;
c906108c 11779
44feb3ce
TT
11780 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11781
11782 if (!arg)
11783 arg = "";
63160a43 11784 arg = skip_spaces_const (arg);
c906108c
SS
11785
11786 /* The allowed syntax is:
c5aa993b
JM
11787 catch exec
11788 catch exec if <cond>
c906108c 11789
4a64f543 11790 First, check if there's an if clause. */
c906108c
SS
11791 cond_string = ep_parse_optional_if_clause (&arg);
11792
11793 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11794 error (_("Junk at end of arguments."));
c906108c 11795
4d01a485 11796 c = new exec_catchpoint ();
b4d90040
PA
11797 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11798 &catch_exec_breakpoint_ops);
11799 c->exec_pathname = NULL;
11800
3ea46bff 11801 install_breakpoint (0, &c->base, 1);
c906108c 11802}
c5aa993b 11803
9ac4176b 11804void
28010a5d
PA
11805init_ada_exception_breakpoint (struct breakpoint *b,
11806 struct gdbarch *gdbarch,
11807 struct symtab_and_line sal,
11808 char *addr_string,
c0a91b2b 11809 const struct breakpoint_ops *ops,
28010a5d 11810 int tempflag,
349774ef 11811 int enabled,
28010a5d 11812 int from_tty)
f7f9143b 11813{
f7f9143b
JB
11814 if (from_tty)
11815 {
5af949e3
UW
11816 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11817 if (!loc_gdbarch)
11818 loc_gdbarch = gdbarch;
11819
6c95b8df
PA
11820 describe_other_breakpoints (loc_gdbarch,
11821 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11822 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11823 version for exception catchpoints, because two catchpoints
11824 used for different exception names will use the same address.
11825 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11826 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11827 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11828 the user what type of catchpoint it is. The above is good
11829 enough for now, though. */
11830 }
11831
28010a5d 11832 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11833
349774ef 11834 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11835 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11836 b->location = string_to_event_location (&addr_string,
11837 language_def (language_ada));
f7f9143b 11838 b->language = language_ada;
f7f9143b
JB
11839}
11840
c906108c 11841static void
fba45db2 11842catch_command (char *arg, int from_tty)
c906108c 11843{
44feb3ce 11844 error (_("Catch requires an event name."));
c906108c
SS
11845}
11846\f
11847
11848static void
fba45db2 11849tcatch_command (char *arg, int from_tty)
c906108c 11850{
44feb3ce 11851 error (_("Catch requires an event name."));
c906108c
SS
11852}
11853
8a2c437b
TT
11854/* A qsort comparison function that sorts breakpoints in order. */
11855
11856static int
11857compare_breakpoints (const void *a, const void *b)
11858{
9a3c8263 11859 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11860 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11861 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11862 uintptr_t ub = (uintptr_t) *bb;
11863
11864 if ((*ba)->number < (*bb)->number)
11865 return -1;
11866 else if ((*ba)->number > (*bb)->number)
11867 return 1;
11868
11869 /* Now sort by address, in case we see, e..g, two breakpoints with
11870 the number 0. */
11871 if (ua < ub)
11872 return -1;
94b0e70d 11873 return ua > ub ? 1 : 0;
8a2c437b
TT
11874}
11875
80f8a6eb 11876/* Delete breakpoints by address or line. */
c906108c
SS
11877
11878static void
fba45db2 11879clear_command (char *arg, int from_tty)
c906108c 11880{
8a2c437b 11881 struct breakpoint *b, *prev;
d6e956e5
VP
11882 VEC(breakpoint_p) *found = 0;
11883 int ix;
c906108c
SS
11884 int default_match;
11885 struct symtabs_and_lines sals;
11886 struct symtab_and_line sal;
c906108c 11887 int i;
8a2c437b 11888 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11889
11890 if (arg)
11891 {
39cf75f7
DE
11892 sals = decode_line_with_current_source (arg,
11893 (DECODE_LINE_FUNFIRSTLINE
11894 | DECODE_LINE_LIST_MODE));
cf4ded82 11895 make_cleanup (xfree, sals.sals);
c906108c
SS
11896 default_match = 0;
11897 }
11898 else
11899 {
8d749320 11900 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11901 make_cleanup (xfree, sals.sals);
4a64f543 11902 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11903
11904 /* Set sal's line, symtab, pc, and pspace to the values
11905 corresponding to the last call to print_frame_info. If the
11906 codepoint is not valid, this will set all the fields to 0. */
11907 get_last_displayed_sal (&sal);
c906108c 11908 if (sal.symtab == 0)
8a3fe4f8 11909 error (_("No source file specified."));
c906108c
SS
11910
11911 sals.sals[0] = sal;
11912 sals.nelts = 1;
11913
11914 default_match = 1;
11915 }
11916
4a64f543
MS
11917 /* We don't call resolve_sal_pc here. That's not as bad as it
11918 seems, because all existing breakpoints typically have both
11919 file/line and pc set. So, if clear is given file/line, we can
11920 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11921
11922 We only support clearing given the address explicitly
11923 present in breakpoint table. Say, we've set breakpoint
4a64f543 11924 at file:line. There were several PC values for that file:line,
ed0616c6 11925 due to optimization, all in one block.
4a64f543
MS
11926
11927 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11928 PC corresponding to the same file:line, the breakpoint won't
11929 be cleared. We probably can still clear the breakpoint, but
11930 since the other PC value is never presented to user, user
11931 can only find it by guessing, and it does not seem important
11932 to support that. */
11933
4a64f543
MS
11934 /* For each line spec given, delete bps which correspond to it. Do
11935 it in two passes, solely to preserve the current behavior that
11936 from_tty is forced true if we delete more than one
11937 breakpoint. */
c906108c 11938
80f8a6eb 11939 found = NULL;
8a2c437b 11940 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11941 for (i = 0; i < sals.nelts; i++)
11942 {
05cba821
JK
11943 const char *sal_fullname;
11944
c906108c 11945 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11946 If line given (pc == 0), clear all bpts on specified line.
11947 If defaulting, clear all bpts on default line
c906108c 11948 or at default pc.
c5aa993b
JM
11949
11950 defaulting sal.pc != 0 tests to do
11951
11952 0 1 pc
11953 1 1 pc _and_ line
11954 0 0 line
11955 1 0 <can't happen> */
c906108c
SS
11956
11957 sal = sals.sals[i];
05cba821
JK
11958 sal_fullname = (sal.symtab == NULL
11959 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11960
4a64f543 11961 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11962 ALL_BREAKPOINTS (b)
c5aa993b 11963 {
0d381245 11964 int match = 0;
4a64f543 11965 /* Are we going to delete b? */
cc60f2e3 11966 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11967 {
11968 struct bp_location *loc = b->loc;
11969 for (; loc; loc = loc->next)
11970 {
f8eba3c6
TT
11971 /* If the user specified file:line, don't allow a PC
11972 match. This matches historical gdb behavior. */
11973 int pc_match = (!sal.explicit_line
11974 && sal.pc
11975 && (loc->pspace == sal.pspace)
11976 && (loc->address == sal.pc)
11977 && (!section_is_overlay (loc->section)
11978 || loc->section == sal.section));
4aac40c8
TT
11979 int line_match = 0;
11980
11981 if ((default_match || sal.explicit_line)
2f202fde 11982 && loc->symtab != NULL
05cba821 11983 && sal_fullname != NULL
4aac40c8 11984 && sal.pspace == loc->pspace
05cba821
JK
11985 && loc->line_number == sal.line
11986 && filename_cmp (symtab_to_fullname (loc->symtab),
11987 sal_fullname) == 0)
11988 line_match = 1;
4aac40c8 11989
0d381245
VP
11990 if (pc_match || line_match)
11991 {
11992 match = 1;
11993 break;
11994 }
11995 }
11996 }
11997
11998 if (match)
d6e956e5 11999 VEC_safe_push(breakpoint_p, found, b);
c906108c 12000 }
80f8a6eb 12001 }
8a2c437b 12002
80f8a6eb 12003 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12004 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12005 {
12006 if (arg)
8a3fe4f8 12007 error (_("No breakpoint at %s."), arg);
80f8a6eb 12008 else
8a3fe4f8 12009 error (_("No breakpoint at this line."));
80f8a6eb 12010 }
c906108c 12011
8a2c437b
TT
12012 /* Remove duplicates from the vec. */
12013 qsort (VEC_address (breakpoint_p, found),
12014 VEC_length (breakpoint_p, found),
12015 sizeof (breakpoint_p),
12016 compare_breakpoints);
12017 prev = VEC_index (breakpoint_p, found, 0);
12018 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12019 {
12020 if (b == prev)
12021 {
12022 VEC_ordered_remove (breakpoint_p, found, ix);
12023 --ix;
12024 }
12025 }
12026
d6e956e5 12027 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12028 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12029 if (from_tty)
a3f17187 12030 {
d6e956e5 12031 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12032 printf_unfiltered (_("Deleted breakpoint "));
12033 else
12034 printf_unfiltered (_("Deleted breakpoints "));
12035 }
d6e956e5
VP
12036
12037 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12038 {
c5aa993b 12039 if (from_tty)
d6e956e5
VP
12040 printf_unfiltered ("%d ", b->number);
12041 delete_breakpoint (b);
c906108c 12042 }
80f8a6eb
MS
12043 if (from_tty)
12044 putchar_unfiltered ('\n');
8a2c437b
TT
12045
12046 do_cleanups (cleanups);
c906108c
SS
12047}
12048\f
12049/* Delete breakpoint in BS if they are `delete' breakpoints and
12050 all breakpoints that are marked for deletion, whether hit or not.
12051 This is called after any breakpoint is hit, or after errors. */
12052
12053void
fba45db2 12054breakpoint_auto_delete (bpstat bs)
c906108c 12055{
35df4500 12056 struct breakpoint *b, *b_tmp;
c906108c
SS
12057
12058 for (; bs; bs = bs->next)
f431efe5
PA
12059 if (bs->breakpoint_at
12060 && bs->breakpoint_at->disposition == disp_del
c906108c 12061 && bs->stop)
f431efe5 12062 delete_breakpoint (bs->breakpoint_at);
c906108c 12063
35df4500 12064 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12065 {
b5de0fa7 12066 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12067 delete_breakpoint (b);
12068 }
c906108c
SS
12069}
12070
4a64f543
MS
12071/* A comparison function for bp_location AP and BP being interfaced to
12072 qsort. Sort elements primarily by their ADDRESS (no matter what
12073 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12074 secondarily by ordering first permanent elements and
4a64f543 12075 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12076 qsort being an unstable algorithm. */
876fa593
JK
12077
12078static int
f5336ca5 12079bp_locations_compare (const void *ap, const void *bp)
876fa593 12080{
9a3c8263
SM
12081 const struct bp_location *a = *(const struct bp_location **) ap;
12082 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12083
12084 if (a->address != b->address)
12085 return (a->address > b->address) - (a->address < b->address);
12086
dea2aa5f
LM
12087 /* Sort locations at the same address by their pspace number, keeping
12088 locations of the same inferior (in a multi-inferior environment)
12089 grouped. */
12090
12091 if (a->pspace->num != b->pspace->num)
12092 return ((a->pspace->num > b->pspace->num)
12093 - (a->pspace->num < b->pspace->num));
12094
876fa593 12095 /* Sort permanent breakpoints first. */
1a853c52
PA
12096 if (a->permanent != b->permanent)
12097 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12098
c56a97f9
JK
12099 /* Make the internal GDB representation stable across GDB runs
12100 where A and B memory inside GDB can differ. Breakpoint locations of
12101 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12102
12103 if (a->owner->number != b->owner->number)
c56a97f9
JK
12104 return ((a->owner->number > b->owner->number)
12105 - (a->owner->number < b->owner->number));
876fa593
JK
12106
12107 return (a > b) - (a < b);
12108}
12109
f5336ca5
PA
12110/* Set bp_locations_placed_address_before_address_max and
12111 bp_locations_shadow_len_after_address_max according to the current
12112 content of the bp_locations array. */
f7545552
TT
12113
12114static void
f5336ca5 12115bp_locations_target_extensions_update (void)
f7545552 12116{
876fa593
JK
12117 struct bp_location *bl, **blp_tmp;
12118
f5336ca5
PA
12119 bp_locations_placed_address_before_address_max = 0;
12120 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12121
12122 ALL_BP_LOCATIONS (bl, blp_tmp)
12123 {
12124 CORE_ADDR start, end, addr;
12125
12126 if (!bp_location_has_shadow (bl))
12127 continue;
12128
12129 start = bl->target_info.placed_address;
12130 end = start + bl->target_info.shadow_len;
12131
12132 gdb_assert (bl->address >= start);
12133 addr = bl->address - start;
f5336ca5
PA
12134 if (addr > bp_locations_placed_address_before_address_max)
12135 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12136
12137 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12138
12139 gdb_assert (bl->address < end);
12140 addr = end - bl->address;
f5336ca5
PA
12141 if (addr > bp_locations_shadow_len_after_address_max)
12142 bp_locations_shadow_len_after_address_max = addr;
876fa593 12143 }
f7545552
TT
12144}
12145
1e4d1764
YQ
12146/* Download tracepoint locations if they haven't been. */
12147
12148static void
12149download_tracepoint_locations (void)
12150{
7ed2c994 12151 struct breakpoint *b;
dd2e65cc 12152 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 12153
5ed8105e 12154 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 12155
7ed2c994 12156 ALL_TRACEPOINTS (b)
1e4d1764 12157 {
7ed2c994 12158 struct bp_location *bl;
1e4d1764 12159 struct tracepoint *t;
f2a8bc8a 12160 int bp_location_downloaded = 0;
1e4d1764 12161
7ed2c994 12162 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12163 ? !may_insert_fast_tracepoints
12164 : !may_insert_tracepoints))
12165 continue;
12166
dd2e65cc
YQ
12167 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12168 {
12169 if (target_can_download_tracepoint ())
12170 can_download_tracepoint = TRIBOOL_TRUE;
12171 else
12172 can_download_tracepoint = TRIBOOL_FALSE;
12173 }
12174
12175 if (can_download_tracepoint == TRIBOOL_FALSE)
12176 break;
12177
7ed2c994
YQ
12178 for (bl = b->loc; bl; bl = bl->next)
12179 {
12180 /* In tracepoint, locations are _never_ duplicated, so
12181 should_be_inserted is equivalent to
12182 unduplicated_should_be_inserted. */
12183 if (!should_be_inserted (bl) || bl->inserted)
12184 continue;
1e4d1764 12185
7ed2c994 12186 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12187
7ed2c994 12188 target_download_tracepoint (bl);
1e4d1764 12189
7ed2c994 12190 bl->inserted = 1;
f2a8bc8a 12191 bp_location_downloaded = 1;
7ed2c994
YQ
12192 }
12193 t = (struct tracepoint *) b;
12194 t->number_on_target = b->number;
f2a8bc8a
YQ
12195 if (bp_location_downloaded)
12196 observer_notify_breakpoint_modified (b);
1e4d1764 12197 }
1e4d1764
YQ
12198}
12199
934709f0
PW
12200/* Swap the insertion/duplication state between two locations. */
12201
12202static void
12203swap_insertion (struct bp_location *left, struct bp_location *right)
12204{
12205 const int left_inserted = left->inserted;
12206 const int left_duplicate = left->duplicate;
b775012e 12207 const int left_needs_update = left->needs_update;
934709f0
PW
12208 const struct bp_target_info left_target_info = left->target_info;
12209
1e4d1764
YQ
12210 /* Locations of tracepoints can never be duplicated. */
12211 if (is_tracepoint (left->owner))
12212 gdb_assert (!left->duplicate);
12213 if (is_tracepoint (right->owner))
12214 gdb_assert (!right->duplicate);
12215
934709f0
PW
12216 left->inserted = right->inserted;
12217 left->duplicate = right->duplicate;
b775012e 12218 left->needs_update = right->needs_update;
934709f0
PW
12219 left->target_info = right->target_info;
12220 right->inserted = left_inserted;
12221 right->duplicate = left_duplicate;
b775012e 12222 right->needs_update = left_needs_update;
934709f0
PW
12223 right->target_info = left_target_info;
12224}
12225
b775012e
LM
12226/* Force the re-insertion of the locations at ADDRESS. This is called
12227 once a new/deleted/modified duplicate location is found and we are evaluating
12228 conditions on the target's side. Such conditions need to be updated on
12229 the target. */
12230
12231static void
12232force_breakpoint_reinsertion (struct bp_location *bl)
12233{
12234 struct bp_location **locp = NULL, **loc2p;
12235 struct bp_location *loc;
12236 CORE_ADDR address = 0;
12237 int pspace_num;
12238
12239 address = bl->address;
12240 pspace_num = bl->pspace->num;
12241
12242 /* This is only meaningful if the target is
12243 evaluating conditions and if the user has
12244 opted for condition evaluation on the target's
12245 side. */
12246 if (gdb_evaluates_breakpoint_condition_p ()
12247 || !target_supports_evaluation_of_breakpoint_conditions ())
12248 return;
12249
12250 /* Flag all breakpoint locations with this address and
12251 the same program space as the location
12252 as "its condition has changed". We need to
12253 update the conditions on the target's side. */
12254 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12255 {
12256 loc = *loc2p;
12257
12258 if (!is_breakpoint (loc->owner)
12259 || pspace_num != loc->pspace->num)
12260 continue;
12261
12262 /* Flag the location appropriately. We use a different state to
12263 let everyone know that we already updated the set of locations
12264 with addr bl->address and program space bl->pspace. This is so
12265 we don't have to keep calling these functions just to mark locations
12266 that have already been marked. */
12267 loc->condition_changed = condition_updated;
12268
12269 /* Free the agent expression bytecode as well. We will compute
12270 it later on. */
833177a4 12271 loc->cond_bytecode.reset ();
b775012e
LM
12272 }
12273}
44702360
PA
12274/* Called whether new breakpoints are created, or existing breakpoints
12275 deleted, to update the global location list and recompute which
12276 locations are duplicate of which.
b775012e 12277
04086b45
PA
12278 The INSERT_MODE flag determines whether locations may not, may, or
12279 shall be inserted now. See 'enum ugll_insert_mode' for more
12280 info. */
b60e7edf 12281
0d381245 12282static void
44702360 12283update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12284{
74960c60 12285 struct breakpoint *b;
876fa593 12286 struct bp_location **locp, *loc;
f7545552 12287 struct cleanup *cleanups;
b775012e
LM
12288 /* Last breakpoint location address that was marked for update. */
12289 CORE_ADDR last_addr = 0;
12290 /* Last breakpoint location program space that was marked for update. */
12291 int last_pspace_num = -1;
f7545552 12292
2d134ed3
PA
12293 /* Used in the duplicates detection below. When iterating over all
12294 bp_locations, points to the first bp_location of a given address.
12295 Breakpoints and watchpoints of different types are never
12296 duplicates of each other. Keep one pointer for each type of
12297 breakpoint/watchpoint, so we only need to loop over all locations
12298 once. */
12299 struct bp_location *bp_loc_first; /* breakpoint */
12300 struct bp_location *wp_loc_first; /* hardware watchpoint */
12301 struct bp_location *awp_loc_first; /* access watchpoint */
12302 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12303
f5336ca5
PA
12304 /* Saved former bp_locations array which we compare against the newly
12305 built bp_locations from the current state of ALL_BREAKPOINTS. */
12306 struct bp_location **old_locations, **old_locp;
12307 unsigned old_locations_count;
876fa593 12308
f5336ca5
PA
12309 old_locations = bp_locations;
12310 old_locations_count = bp_locations_count;
12311 bp_locations = NULL;
12312 bp_locations_count = 0;
12313 cleanups = make_cleanup (xfree, old_locations);
0d381245 12314
74960c60 12315 ALL_BREAKPOINTS (b)
876fa593 12316 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12317 bp_locations_count++;
876fa593 12318
f5336ca5
PA
12319 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12320 locp = bp_locations;
876fa593
JK
12321 ALL_BREAKPOINTS (b)
12322 for (loc = b->loc; loc; loc = loc->next)
12323 *locp++ = loc;
f5336ca5
PA
12324 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12325 bp_locations_compare);
876fa593 12326
f5336ca5 12327 bp_locations_target_extensions_update ();
74960c60 12328
4a64f543
MS
12329 /* Identify bp_location instances that are no longer present in the
12330 new list, and therefore should be freed. Note that it's not
12331 necessary that those locations should be removed from inferior --
12332 if there's another location at the same address (previously
12333 marked as duplicate), we don't need to remove/insert the
12334 location.
876fa593 12335
4a64f543
MS
12336 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12337 and former bp_location array state respectively. */
876fa593 12338
f5336ca5
PA
12339 locp = bp_locations;
12340 for (old_locp = old_locations;
12341 old_locp < old_locations + old_locations_count;
876fa593 12342 old_locp++)
74960c60 12343 {
876fa593 12344 struct bp_location *old_loc = *old_locp;
c7d46a38 12345 struct bp_location **loc2p;
876fa593 12346
e5dd4106 12347 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12348 not, we have to free it. */
c7d46a38 12349 int found_object = 0;
20874c92
VP
12350 /* Tells if the location should remain inserted in the target. */
12351 int keep_in_target = 0;
12352 int removed = 0;
876fa593 12353
4a64f543
MS
12354 /* Skip LOCP entries which will definitely never be needed.
12355 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12356 while (locp < bp_locations + bp_locations_count
c7d46a38 12357 && (*locp)->address < old_loc->address)
876fa593 12358 locp++;
c7d46a38
PA
12359
12360 for (loc2p = locp;
f5336ca5 12361 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12362 && (*loc2p)->address == old_loc->address);
12363 loc2p++)
12364 {
b775012e
LM
12365 /* Check if this is a new/duplicated location or a duplicated
12366 location that had its condition modified. If so, we want to send
12367 its condition to the target if evaluation of conditions is taking
12368 place there. */
12369 if ((*loc2p)->condition_changed == condition_modified
12370 && (last_addr != old_loc->address
12371 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12372 {
b775012e
LM
12373 force_breakpoint_reinsertion (*loc2p);
12374 last_pspace_num = old_loc->pspace->num;
c7d46a38 12375 }
b775012e
LM
12376
12377 if (*loc2p == old_loc)
12378 found_object = 1;
c7d46a38 12379 }
74960c60 12380
b775012e
LM
12381 /* We have already handled this address, update it so that we don't
12382 have to go through updates again. */
12383 last_addr = old_loc->address;
12384
12385 /* Target-side condition evaluation: Handle deleted locations. */
12386 if (!found_object)
12387 force_breakpoint_reinsertion (old_loc);
12388
4a64f543
MS
12389 /* If this location is no longer present, and inserted, look if
12390 there's maybe a new location at the same address. If so,
12391 mark that one inserted, and don't remove this one. This is
12392 needed so that we don't have a time window where a breakpoint
12393 at certain location is not inserted. */
74960c60 12394
876fa593 12395 if (old_loc->inserted)
0d381245 12396 {
4a64f543
MS
12397 /* If the location is inserted now, we might have to remove
12398 it. */
74960c60 12399
876fa593 12400 if (found_object && should_be_inserted (old_loc))
74960c60 12401 {
4a64f543
MS
12402 /* The location is still present in the location list,
12403 and still should be inserted. Don't do anything. */
20874c92 12404 keep_in_target = 1;
74960c60
VP
12405 }
12406 else
12407 {
b775012e
LM
12408 /* This location still exists, but it won't be kept in the
12409 target since it may have been disabled. We proceed to
12410 remove its target-side condition. */
12411
4a64f543
MS
12412 /* The location is either no longer present, or got
12413 disabled. See if there's another location at the
12414 same address, in which case we don't need to remove
12415 this one from the target. */
876fa593 12416
2bdf28a0 12417 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12418 if (breakpoint_address_is_meaningful (old_loc->owner))
12419 {
876fa593 12420 for (loc2p = locp;
f5336ca5 12421 (loc2p < bp_locations + bp_locations_count
c7d46a38 12422 && (*loc2p)->address == old_loc->address);
876fa593
JK
12423 loc2p++)
12424 {
12425 struct bp_location *loc2 = *loc2p;
12426
2d134ed3 12427 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12428 {
85d721b8
PA
12429 /* Read watchpoint locations are switched to
12430 access watchpoints, if the former are not
12431 supported, but the latter are. */
12432 if (is_hardware_watchpoint (old_loc->owner))
12433 {
12434 gdb_assert (is_hardware_watchpoint (loc2->owner));
12435 loc2->watchpoint_type = old_loc->watchpoint_type;
12436 }
12437
934709f0
PW
12438 /* loc2 is a duplicated location. We need to check
12439 if it should be inserted in case it will be
12440 unduplicated. */
12441 if (loc2 != old_loc
12442 && unduplicated_should_be_inserted (loc2))
c7d46a38 12443 {
934709f0 12444 swap_insertion (old_loc, loc2);
c7d46a38
PA
12445 keep_in_target = 1;
12446 break;
12447 }
876fa593
JK
12448 }
12449 }
12450 }
74960c60
VP
12451 }
12452
20874c92
VP
12453 if (!keep_in_target)
12454 {
834c0d03 12455 if (remove_breakpoint (old_loc))
20874c92 12456 {
4a64f543
MS
12457 /* This is just about all we can do. We could keep
12458 this location on the global list, and try to
12459 remove it next time, but there's no particular
12460 reason why we will succeed next time.
20874c92 12461
4a64f543
MS
12462 Note that at this point, old_loc->owner is still
12463 valid, as delete_breakpoint frees the breakpoint
12464 only after calling us. */
3e43a32a
MS
12465 printf_filtered (_("warning: Error removing "
12466 "breakpoint %d\n"),
876fa593 12467 old_loc->owner->number);
20874c92
VP
12468 }
12469 removed = 1;
12470 }
0d381245 12471 }
74960c60
VP
12472
12473 if (!found_object)
1c5cfe86 12474 {
fbea99ea 12475 if (removed && target_is_non_stop_p ()
1cf4d951 12476 && need_moribund_for_location_type (old_loc))
20874c92 12477 {
db82e815
PA
12478 /* This location was removed from the target. In
12479 non-stop mode, a race condition is possible where
12480 we've removed a breakpoint, but stop events for that
12481 breakpoint are already queued and will arrive later.
12482 We apply an heuristic to be able to distinguish such
12483 SIGTRAPs from other random SIGTRAPs: we keep this
12484 breakpoint location for a bit, and will retire it
12485 after we see some number of events. The theory here
12486 is that reporting of events should, "on the average",
12487 be fair, so after a while we'll see events from all
12488 threads that have anything of interest, and no longer
12489 need to keep this breakpoint location around. We
12490 don't hold locations forever so to reduce chances of
12491 mistaking a non-breakpoint SIGTRAP for a breakpoint
12492 SIGTRAP.
12493
12494 The heuristic failing can be disastrous on
12495 decr_pc_after_break targets.
12496
12497 On decr_pc_after_break targets, like e.g., x86-linux,
12498 if we fail to recognize a late breakpoint SIGTRAP,
12499 because events_till_retirement has reached 0 too
12500 soon, we'll fail to do the PC adjustment, and report
12501 a random SIGTRAP to the user. When the user resumes
12502 the inferior, it will most likely immediately crash
2dec564e 12503 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12504 corrupted, because of being resumed e.g., in the
12505 middle of a multi-byte instruction, or skipped a
12506 one-byte instruction. This was actually seen happen
12507 on native x86-linux, and should be less rare on
12508 targets that do not support new thread events, like
12509 remote, due to the heuristic depending on
12510 thread_count.
12511
12512 Mistaking a random SIGTRAP for a breakpoint trap
12513 causes similar symptoms (PC adjustment applied when
12514 it shouldn't), but then again, playing with SIGTRAPs
12515 behind the debugger's back is asking for trouble.
12516
12517 Since hardware watchpoint traps are always
12518 distinguishable from other traps, so we don't need to
12519 apply keep hardware watchpoint moribund locations
12520 around. We simply always ignore hardware watchpoint
12521 traps we can no longer explain. */
12522
876fa593
JK
12523 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12524 old_loc->owner = NULL;
20874c92 12525
876fa593 12526 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12527 }
12528 else
f431efe5
PA
12529 {
12530 old_loc->owner = NULL;
12531 decref_bp_location (&old_loc);
12532 }
20874c92 12533 }
74960c60 12534 }
1c5cfe86 12535
348d480f
PA
12536 /* Rescan breakpoints at the same address and section, marking the
12537 first one as "first" and any others as "duplicates". This is so
12538 that the bpt instruction is only inserted once. If we have a
12539 permanent breakpoint at the same place as BPT, make that one the
12540 official one, and the rest as duplicates. Permanent breakpoints
12541 are sorted first for the same address.
12542
12543 Do the same for hardware watchpoints, but also considering the
12544 watchpoint's type (regular/access/read) and length. */
12545
12546 bp_loc_first = NULL;
12547 wp_loc_first = NULL;
12548 awp_loc_first = NULL;
12549 rwp_loc_first = NULL;
12550 ALL_BP_LOCATIONS (loc, locp)
12551 {
12552 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12553 non-NULL. */
348d480f 12554 struct bp_location **loc_first_p;
d3fbdd86 12555 b = loc->owner;
348d480f 12556
6f380991 12557 if (!unduplicated_should_be_inserted (loc)
348d480f 12558 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12559 /* Don't detect duplicate for tracepoint locations because they are
12560 never duplicated. See the comments in field `duplicate' of
12561 `struct bp_location'. */
348d480f 12562 || is_tracepoint (b))
b775012e
LM
12563 {
12564 /* Clear the condition modification flag. */
12565 loc->condition_changed = condition_unchanged;
12566 continue;
12567 }
348d480f 12568
348d480f
PA
12569 if (b->type == bp_hardware_watchpoint)
12570 loc_first_p = &wp_loc_first;
12571 else if (b->type == bp_read_watchpoint)
12572 loc_first_p = &rwp_loc_first;
12573 else if (b->type == bp_access_watchpoint)
12574 loc_first_p = &awp_loc_first;
12575 else
12576 loc_first_p = &bp_loc_first;
12577
12578 if (*loc_first_p == NULL
12579 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12580 || !breakpoint_locations_match (loc, *loc_first_p))
12581 {
12582 *loc_first_p = loc;
12583 loc->duplicate = 0;
b775012e
LM
12584
12585 if (is_breakpoint (loc->owner) && loc->condition_changed)
12586 {
12587 loc->needs_update = 1;
12588 /* Clear the condition modification flag. */
12589 loc->condition_changed = condition_unchanged;
12590 }
348d480f
PA
12591 continue;
12592 }
12593
934709f0
PW
12594
12595 /* This and the above ensure the invariant that the first location
12596 is not duplicated, and is the inserted one.
12597 All following are marked as duplicated, and are not inserted. */
12598 if (loc->inserted)
12599 swap_insertion (loc, *loc_first_p);
348d480f
PA
12600 loc->duplicate = 1;
12601
b775012e
LM
12602 /* Clear the condition modification flag. */
12603 loc->condition_changed = condition_unchanged;
348d480f
PA
12604 }
12605
a25a5a45 12606 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12607 {
04086b45 12608 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12609 insert_breakpoint_locations ();
12610 else
12611 {
44702360
PA
12612 /* Even though the caller told us to not insert new
12613 locations, we may still need to update conditions on the
12614 target's side of breakpoints that were already inserted
12615 if the target is evaluating breakpoint conditions. We
b775012e
LM
12616 only update conditions for locations that are marked
12617 "needs_update". */
12618 update_inserted_breakpoint_locations ();
12619 }
12620 }
348d480f 12621
04086b45 12622 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12623 download_tracepoint_locations ();
12624
348d480f
PA
12625 do_cleanups (cleanups);
12626}
12627
12628void
12629breakpoint_retire_moribund (void)
12630{
12631 struct bp_location *loc;
12632 int ix;
12633
12634 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12635 if (--(loc->events_till_retirement) == 0)
12636 {
12637 decref_bp_location (&loc);
12638 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12639 --ix;
12640 }
12641}
12642
12643static void
44702360 12644update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12645{
348d480f 12646
492d29ea
PA
12647 TRY
12648 {
12649 update_global_location_list (insert_mode);
12650 }
12651 CATCH (e, RETURN_MASK_ERROR)
12652 {
12653 }
12654 END_CATCH
348d480f
PA
12655}
12656
12657/* Clear BKP from a BPS. */
12658
12659static void
12660bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12661{
12662 bpstat bs;
12663
12664 for (bs = bps; bs; bs = bs->next)
12665 if (bs->breakpoint_at == bpt)
12666 {
12667 bs->breakpoint_at = NULL;
12668 bs->old_val = NULL;
12669 /* bs->commands will be freed later. */
12670 }
12671}
12672
12673/* Callback for iterate_over_threads. */
12674static int
12675bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12676{
9a3c8263 12677 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12678
12679 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12680 return 0;
12681}
12682
12683/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12684 callbacks. */
12685
12686static void
12687say_where (struct breakpoint *b)
12688{
12689 struct value_print_options opts;
12690
12691 get_user_print_options (&opts);
12692
12693 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12694 single string. */
12695 if (b->loc == NULL)
12696 {
f00aae0f
KS
12697 /* For pending locations, the output differs slightly based
12698 on b->extra_string. If this is non-NULL, it contains either
12699 a condition or dprintf arguments. */
12700 if (b->extra_string == NULL)
12701 {
12702 printf_filtered (_(" (%s) pending."),
d28cd78a 12703 event_location_to_string (b->location.get ()));
f00aae0f
KS
12704 }
12705 else if (b->type == bp_dprintf)
12706 {
12707 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12708 event_location_to_string (b->location.get ()),
f00aae0f
KS
12709 b->extra_string);
12710 }
12711 else
12712 {
12713 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12714 event_location_to_string (b->location.get ()),
f00aae0f
KS
12715 b->extra_string);
12716 }
348d480f
PA
12717 }
12718 else
12719 {
2f202fde 12720 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12721 {
12722 printf_filtered (" at ");
12723 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12724 gdb_stdout);
12725 }
2f202fde 12726 if (b->loc->symtab != NULL)
f8eba3c6
TT
12727 {
12728 /* If there is a single location, we can print the location
12729 more nicely. */
12730 if (b->loc->next == NULL)
12731 printf_filtered (": file %s, line %d.",
05cba821
JK
12732 symtab_to_filename_for_display (b->loc->symtab),
12733 b->loc->line_number);
f8eba3c6
TT
12734 else
12735 /* This is not ideal, but each location may have a
12736 different file name, and this at least reflects the
12737 real situation somewhat. */
f00aae0f 12738 printf_filtered (": %s.",
d28cd78a 12739 event_location_to_string (b->location.get ()));
f8eba3c6 12740 }
348d480f
PA
12741
12742 if (b->loc->next)
12743 {
12744 struct bp_location *loc = b->loc;
12745 int n = 0;
12746 for (; loc; loc = loc->next)
12747 ++n;
12748 printf_filtered (" (%d locations)", n);
12749 }
12750 }
12751}
12752
348d480f
PA
12753/* Default bp_location_ops methods. */
12754
12755static void
12756bp_location_dtor (struct bp_location *self)
12757{
348d480f
PA
12758 xfree (self->function_name);
12759}
12760
12761static const struct bp_location_ops bp_location_ops =
12762{
12763 bp_location_dtor
12764};
12765
2060206e
PA
12766/* Default breakpoint_ops methods all breakpoint_ops ultimately
12767 inherit from. */
348d480f 12768
2060206e
PA
12769static void
12770base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12771{
12772 decref_counted_command_line (&self->commands);
12773 xfree (self->cond_string);
fb81d016 12774 xfree (self->extra_string);
f8eba3c6 12775 xfree (self->filter);
348d480f
PA
12776}
12777
2060206e
PA
12778static struct bp_location *
12779base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12780{
5625a286 12781 return new bp_location (&bp_location_ops, self);
348d480f
PA
12782}
12783
2060206e
PA
12784static void
12785base_breakpoint_re_set (struct breakpoint *b)
12786{
12787 /* Nothing to re-set. */
12788}
12789
12790#define internal_error_pure_virtual_called() \
12791 gdb_assert_not_reached ("pure virtual function called")
12792
12793static int
12794base_breakpoint_insert_location (struct bp_location *bl)
12795{
12796 internal_error_pure_virtual_called ();
12797}
12798
12799static int
73971819
PA
12800base_breakpoint_remove_location (struct bp_location *bl,
12801 enum remove_bp_reason reason)
2060206e
PA
12802{
12803 internal_error_pure_virtual_called ();
12804}
12805
12806static int
12807base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12808 struct address_space *aspace,
09ac7c10
TT
12809 CORE_ADDR bp_addr,
12810 const struct target_waitstatus *ws)
2060206e
PA
12811{
12812 internal_error_pure_virtual_called ();
12813}
12814
12815static void
12816base_breakpoint_check_status (bpstat bs)
12817{
12818 /* Always stop. */
12819}
12820
12821/* A "works_in_software_mode" breakpoint_ops method that just internal
12822 errors. */
12823
12824static int
12825base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12826{
12827 internal_error_pure_virtual_called ();
12828}
12829
12830/* A "resources_needed" breakpoint_ops method that just internal
12831 errors. */
12832
12833static int
12834base_breakpoint_resources_needed (const struct bp_location *bl)
12835{
12836 internal_error_pure_virtual_called ();
12837}
12838
12839static enum print_stop_action
12840base_breakpoint_print_it (bpstat bs)
12841{
12842 internal_error_pure_virtual_called ();
12843}
12844
12845static void
12846base_breakpoint_print_one_detail (const struct breakpoint *self,
12847 struct ui_out *uiout)
12848{
12849 /* nothing */
12850}
12851
12852static void
12853base_breakpoint_print_mention (struct breakpoint *b)
12854{
12855 internal_error_pure_virtual_called ();
12856}
12857
12858static void
12859base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12860{
12861 internal_error_pure_virtual_called ();
12862}
12863
983af33b 12864static void
f00aae0f
KS
12865base_breakpoint_create_sals_from_location
12866 (const struct event_location *location,
12867 struct linespec_result *canonical,
12868 enum bptype type_wanted)
983af33b
SDJ
12869{
12870 internal_error_pure_virtual_called ();
12871}
12872
12873static void
12874base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12875 struct linespec_result *c,
983af33b 12876 char *cond_string,
e7e0cddf 12877 char *extra_string,
983af33b
SDJ
12878 enum bptype type_wanted,
12879 enum bpdisp disposition,
12880 int thread,
12881 int task, int ignore_count,
12882 const struct breakpoint_ops *o,
12883 int from_tty, int enabled,
44f238bb 12884 int internal, unsigned flags)
983af33b
SDJ
12885{
12886 internal_error_pure_virtual_called ();
12887}
12888
12889static void
f00aae0f
KS
12890base_breakpoint_decode_location (struct breakpoint *b,
12891 const struct event_location *location,
c2f4122d 12892 struct program_space *search_pspace,
983af33b
SDJ
12893 struct symtabs_and_lines *sals)
12894{
12895 internal_error_pure_virtual_called ();
12896}
12897
ab04a2af
TT
12898/* The default 'explains_signal' method. */
12899
47591c29 12900static int
427cd150 12901base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12902{
47591c29 12903 return 1;
ab04a2af
TT
12904}
12905
9d6e6e84
HZ
12906/* The default "after_condition_true" method. */
12907
12908static void
12909base_breakpoint_after_condition_true (struct bpstats *bs)
12910{
12911 /* Nothing to do. */
12912}
12913
ab04a2af 12914struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12915{
12916 base_breakpoint_dtor,
12917 base_breakpoint_allocate_location,
12918 base_breakpoint_re_set,
12919 base_breakpoint_insert_location,
12920 base_breakpoint_remove_location,
12921 base_breakpoint_breakpoint_hit,
12922 base_breakpoint_check_status,
12923 base_breakpoint_resources_needed,
12924 base_breakpoint_works_in_software_mode,
12925 base_breakpoint_print_it,
12926 NULL,
12927 base_breakpoint_print_one_detail,
12928 base_breakpoint_print_mention,
983af33b 12929 base_breakpoint_print_recreate,
5f700d83 12930 base_breakpoint_create_sals_from_location,
983af33b 12931 base_breakpoint_create_breakpoints_sal,
5f700d83 12932 base_breakpoint_decode_location,
9d6e6e84
HZ
12933 base_breakpoint_explains_signal,
12934 base_breakpoint_after_condition_true,
2060206e
PA
12935};
12936
12937/* Default breakpoint_ops methods. */
12938
12939static void
348d480f
PA
12940bkpt_re_set (struct breakpoint *b)
12941{
06edf0c0 12942 /* FIXME: is this still reachable? */
9ef9e6a6 12943 if (breakpoint_event_location_empty_p (b))
06edf0c0 12944 {
f00aae0f 12945 /* Anything without a location can't be re-set. */
348d480f 12946 delete_breakpoint (b);
06edf0c0 12947 return;
348d480f 12948 }
06edf0c0
PA
12949
12950 breakpoint_re_set_default (b);
348d480f
PA
12951}
12952
2060206e 12953static int
348d480f
PA
12954bkpt_insert_location (struct bp_location *bl)
12955{
cd6c3b4f
YQ
12956 CORE_ADDR addr = bl->target_info.reqstd_address;
12957
579c6ad9 12958 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12959 bl->target_info.placed_address = addr;
12960
348d480f 12961 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12962 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12963 else
7c16b83e 12964 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12965}
12966
2060206e 12967static int
73971819 12968bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12969{
12970 if (bl->loc_type == bp_loc_hardware_breakpoint)
12971 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12972 else
73971819 12973 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12974}
12975
2060206e 12976static int
348d480f 12977bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12978 struct address_space *aspace, CORE_ADDR bp_addr,
12979 const struct target_waitstatus *ws)
348d480f 12980{
09ac7c10 12981 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12982 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12983 return 0;
12984
348d480f
PA
12985 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12986 aspace, bp_addr))
12987 return 0;
12988
12989 if (overlay_debugging /* unmapped overlay section */
12990 && section_is_overlay (bl->section)
12991 && !section_is_mapped (bl->section))
12992 return 0;
12993
12994 return 1;
12995}
12996
cd1608cc
PA
12997static int
12998dprintf_breakpoint_hit (const struct bp_location *bl,
12999 struct address_space *aspace, CORE_ADDR bp_addr,
13000 const struct target_waitstatus *ws)
13001{
13002 if (dprintf_style == dprintf_style_agent
13003 && target_can_run_breakpoint_commands ())
13004 {
13005 /* An agent-style dprintf never causes a stop. If we see a trap
13006 for this address it must be for a breakpoint that happens to
13007 be set at the same address. */
13008 return 0;
13009 }
13010
13011 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13012}
13013
2060206e 13014static int
348d480f
PA
13015bkpt_resources_needed (const struct bp_location *bl)
13016{
13017 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13018
13019 return 1;
13020}
13021
2060206e 13022static enum print_stop_action
348d480f
PA
13023bkpt_print_it (bpstat bs)
13024{
348d480f
PA
13025 struct breakpoint *b;
13026 const struct bp_location *bl;
001c8c33 13027 int bp_temp;
79a45e25 13028 struct ui_out *uiout = current_uiout;
348d480f
PA
13029
13030 gdb_assert (bs->bp_location_at != NULL);
13031
13032 bl = bs->bp_location_at;
13033 b = bs->breakpoint_at;
13034
001c8c33
PA
13035 bp_temp = b->disposition == disp_del;
13036 if (bl->address != bl->requested_address)
13037 breakpoint_adjustment_warning (bl->requested_address,
13038 bl->address,
13039 b->number, 1);
13040 annotate_breakpoint (b->number);
f303dbd6
PA
13041 maybe_print_thread_hit_breakpoint (uiout);
13042
001c8c33 13043 if (bp_temp)
112e8700 13044 uiout->text ("Temporary breakpoint ");
001c8c33 13045 else
112e8700
SM
13046 uiout->text ("Breakpoint ");
13047 if (uiout->is_mi_like_p ())
348d480f 13048 {
112e8700 13049 uiout->field_string ("reason",
001c8c33 13050 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 13051 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 13052 }
112e8700
SM
13053 uiout->field_int ("bkptno", b->number);
13054 uiout->text (", ");
06edf0c0 13055
001c8c33 13056 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13057}
13058
2060206e 13059static void
06edf0c0
PA
13060bkpt_print_mention (struct breakpoint *b)
13061{
112e8700 13062 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
13063 return;
13064
13065 switch (b->type)
13066 {
13067 case bp_breakpoint:
13068 case bp_gnu_ifunc_resolver:
13069 if (b->disposition == disp_del)
13070 printf_filtered (_("Temporary breakpoint"));
13071 else
13072 printf_filtered (_("Breakpoint"));
13073 printf_filtered (_(" %d"), b->number);
13074 if (b->type == bp_gnu_ifunc_resolver)
13075 printf_filtered (_(" at gnu-indirect-function resolver"));
13076 break;
13077 case bp_hardware_breakpoint:
13078 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13079 break;
e7e0cddf
SS
13080 case bp_dprintf:
13081 printf_filtered (_("Dprintf %d"), b->number);
13082 break;
06edf0c0
PA
13083 }
13084
13085 say_where (b);
13086}
13087
2060206e 13088static void
06edf0c0
PA
13089bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13090{
13091 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13092 fprintf_unfiltered (fp, "tbreak");
13093 else if (tp->type == bp_breakpoint)
13094 fprintf_unfiltered (fp, "break");
13095 else if (tp->type == bp_hardware_breakpoint
13096 && tp->disposition == disp_del)
13097 fprintf_unfiltered (fp, "thbreak");
13098 else if (tp->type == bp_hardware_breakpoint)
13099 fprintf_unfiltered (fp, "hbreak");
13100 else
13101 internal_error (__FILE__, __LINE__,
13102 _("unhandled breakpoint type %d"), (int) tp->type);
13103
f00aae0f 13104 fprintf_unfiltered (fp, " %s",
d28cd78a 13105 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13106
13107 /* Print out extra_string if this breakpoint is pending. It might
13108 contain, for example, conditions that were set by the user. */
13109 if (tp->loc == NULL && tp->extra_string != NULL)
13110 fprintf_unfiltered (fp, " %s", tp->extra_string);
13111
dd11a36c 13112 print_recreate_thread (tp, fp);
06edf0c0
PA
13113}
13114
983af33b 13115static void
f00aae0f
KS
13116bkpt_create_sals_from_location (const struct event_location *location,
13117 struct linespec_result *canonical,
13118 enum bptype type_wanted)
983af33b 13119{
f00aae0f 13120 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13121}
13122
13123static void
13124bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13125 struct linespec_result *canonical,
983af33b 13126 char *cond_string,
e7e0cddf 13127 char *extra_string,
983af33b
SDJ
13128 enum bptype type_wanted,
13129 enum bpdisp disposition,
13130 int thread,
13131 int task, int ignore_count,
13132 const struct breakpoint_ops *ops,
13133 int from_tty, int enabled,
44f238bb 13134 int internal, unsigned flags)
983af33b 13135{
023fa29b 13136 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13137 cond_string, extra_string,
13138 type_wanted,
983af33b
SDJ
13139 disposition, thread, task,
13140 ignore_count, ops, from_tty,
44f238bb 13141 enabled, internal, flags);
983af33b
SDJ
13142}
13143
13144static void
f00aae0f
KS
13145bkpt_decode_location (struct breakpoint *b,
13146 const struct event_location *location,
c2f4122d 13147 struct program_space *search_pspace,
983af33b
SDJ
13148 struct symtabs_and_lines *sals)
13149{
c2f4122d 13150 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13151}
13152
06edf0c0
PA
13153/* Virtual table for internal breakpoints. */
13154
13155static void
13156internal_bkpt_re_set (struct breakpoint *b)
13157{
13158 switch (b->type)
13159 {
13160 /* Delete overlay event and longjmp master breakpoints; they
13161 will be reset later by breakpoint_re_set. */
13162 case bp_overlay_event:
13163 case bp_longjmp_master:
13164 case bp_std_terminate_master:
13165 case bp_exception_master:
13166 delete_breakpoint (b);
13167 break;
13168
13169 /* This breakpoint is special, it's set up when the inferior
13170 starts and we really don't want to touch it. */
13171 case bp_shlib_event:
13172
13173 /* Like bp_shlib_event, this breakpoint type is special. Once
13174 it is set up, we do not want to touch it. */
13175 case bp_thread_event:
13176 break;
13177 }
13178}
13179
13180static void
13181internal_bkpt_check_status (bpstat bs)
13182{
a9b3a50f
PA
13183 if (bs->breakpoint_at->type == bp_shlib_event)
13184 {
13185 /* If requested, stop when the dynamic linker notifies GDB of
13186 events. This allows the user to get control and place
13187 breakpoints in initializer routines for dynamically loaded
13188 objects (among other things). */
13189 bs->stop = stop_on_solib_events;
13190 bs->print = stop_on_solib_events;
13191 }
13192 else
13193 bs->stop = 0;
06edf0c0
PA
13194}
13195
13196static enum print_stop_action
13197internal_bkpt_print_it (bpstat bs)
13198{
06edf0c0 13199 struct breakpoint *b;
06edf0c0 13200
06edf0c0
PA
13201 b = bs->breakpoint_at;
13202
06edf0c0
PA
13203 switch (b->type)
13204 {
348d480f
PA
13205 case bp_shlib_event:
13206 /* Did we stop because the user set the stop_on_solib_events
13207 variable? (If so, we report this as a generic, "Stopped due
13208 to shlib event" message.) */
edcc5120 13209 print_solib_event (0);
348d480f
PA
13210 break;
13211
13212 case bp_thread_event:
13213 /* Not sure how we will get here.
13214 GDB should not stop for these breakpoints. */
13215 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13216 break;
13217
13218 case bp_overlay_event:
13219 /* By analogy with the thread event, GDB should not stop for these. */
13220 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13221 break;
13222
13223 case bp_longjmp_master:
13224 /* These should never be enabled. */
13225 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13226 break;
13227
13228 case bp_std_terminate_master:
13229 /* These should never be enabled. */
13230 printf_filtered (_("std::terminate Master Breakpoint: "
13231 "gdb should not stop!\n"));
348d480f
PA
13232 break;
13233
13234 case bp_exception_master:
13235 /* These should never be enabled. */
13236 printf_filtered (_("Exception Master Breakpoint: "
13237 "gdb should not stop!\n"));
06edf0c0
PA
13238 break;
13239 }
13240
001c8c33 13241 return PRINT_NOTHING;
06edf0c0
PA
13242}
13243
13244static void
13245internal_bkpt_print_mention (struct breakpoint *b)
13246{
13247 /* Nothing to mention. These breakpoints are internal. */
13248}
13249
06edf0c0
PA
13250/* Virtual table for momentary breakpoints */
13251
13252static void
13253momentary_bkpt_re_set (struct breakpoint *b)
13254{
13255 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13256 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13257 Otherwise these should have been blown away via the cleanup chain
13258 or by breakpoint_init_inferior when we rerun the executable. */
13259}
13260
13261static void
13262momentary_bkpt_check_status (bpstat bs)
13263{
13264 /* Nothing. The point of these breakpoints is causing a stop. */
13265}
13266
13267static enum print_stop_action
13268momentary_bkpt_print_it (bpstat bs)
13269{
001c8c33 13270 return PRINT_UNKNOWN;
348d480f
PA
13271}
13272
06edf0c0
PA
13273static void
13274momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13275{
06edf0c0 13276 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13277}
13278
e2e4d78b
JK
13279/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13280
13281 It gets cleared already on the removal of the first one of such placed
13282 breakpoints. This is OK as they get all removed altogether. */
13283
13284static void
13285longjmp_bkpt_dtor (struct breakpoint *self)
13286{
5d5658a1 13287 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13288
13289 if (tp)
13290 tp->initiating_frame = null_frame_id;
13291
13292 momentary_breakpoint_ops.dtor (self);
13293}
13294
55aa24fb
SDJ
13295/* Specific methods for probe breakpoints. */
13296
13297static int
13298bkpt_probe_insert_location (struct bp_location *bl)
13299{
13300 int v = bkpt_insert_location (bl);
13301
13302 if (v == 0)
13303 {
13304 /* The insertion was successful, now let's set the probe's semaphore
13305 if needed. */
0ea5cda8
SDJ
13306 if (bl->probe.probe->pops->set_semaphore != NULL)
13307 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13308 bl->probe.objfile,
13309 bl->gdbarch);
55aa24fb
SDJ
13310 }
13311
13312 return v;
13313}
13314
13315static int
73971819
PA
13316bkpt_probe_remove_location (struct bp_location *bl,
13317 enum remove_bp_reason reason)
55aa24fb
SDJ
13318{
13319 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13320 if (bl->probe.probe->pops->clear_semaphore != NULL)
13321 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13322 bl->probe.objfile,
13323 bl->gdbarch);
55aa24fb 13324
73971819 13325 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13326}
13327
13328static void
f00aae0f 13329bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13330 struct linespec_result *canonical,
f00aae0f 13331 enum bptype type_wanted)
55aa24fb
SDJ
13332{
13333 struct linespec_sals lsal;
13334
c2f4122d 13335 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13336 lsal.canonical
13337 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13338 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13339}
13340
13341static void
f00aae0f
KS
13342bkpt_probe_decode_location (struct breakpoint *b,
13343 const struct event_location *location,
c2f4122d 13344 struct program_space *search_pspace,
55aa24fb
SDJ
13345 struct symtabs_and_lines *sals)
13346{
c2f4122d 13347 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13348 if (!sals->sals)
13349 error (_("probe not found"));
13350}
13351
348d480f 13352/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13353
348d480f
PA
13354static void
13355tracepoint_re_set (struct breakpoint *b)
13356{
13357 breakpoint_re_set_default (b);
13358}
876fa593 13359
348d480f
PA
13360static int
13361tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13362 struct address_space *aspace, CORE_ADDR bp_addr,
13363 const struct target_waitstatus *ws)
348d480f
PA
13364{
13365 /* By definition, the inferior does not report stops at
13366 tracepoints. */
13367 return 0;
74960c60
VP
13368}
13369
13370static void
348d480f
PA
13371tracepoint_print_one_detail (const struct breakpoint *self,
13372 struct ui_out *uiout)
74960c60 13373{
d9b3f62e
PA
13374 struct tracepoint *tp = (struct tracepoint *) self;
13375 if (tp->static_trace_marker_id)
348d480f
PA
13376 {
13377 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13378
112e8700
SM
13379 uiout->text ("\tmarker id is ");
13380 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13381 tp->static_trace_marker_id);
112e8700 13382 uiout->text ("\n");
348d480f 13383 }
0d381245
VP
13384}
13385
a474d7c2 13386static void
348d480f 13387tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13388{
112e8700 13389 if (current_uiout->is_mi_like_p ())
348d480f 13390 return;
cc59ec59 13391
348d480f
PA
13392 switch (b->type)
13393 {
13394 case bp_tracepoint:
13395 printf_filtered (_("Tracepoint"));
13396 printf_filtered (_(" %d"), b->number);
13397 break;
13398 case bp_fast_tracepoint:
13399 printf_filtered (_("Fast tracepoint"));
13400 printf_filtered (_(" %d"), b->number);
13401 break;
13402 case bp_static_tracepoint:
13403 printf_filtered (_("Static tracepoint"));
13404 printf_filtered (_(" %d"), b->number);
13405 break;
13406 default:
13407 internal_error (__FILE__, __LINE__,
13408 _("unhandled tracepoint type %d"), (int) b->type);
13409 }
13410
13411 say_where (b);
a474d7c2
PA
13412}
13413
348d480f 13414static void
d9b3f62e 13415tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13416{
d9b3f62e
PA
13417 struct tracepoint *tp = (struct tracepoint *) self;
13418
13419 if (self->type == bp_fast_tracepoint)
348d480f 13420 fprintf_unfiltered (fp, "ftrace");
c93e8391 13421 else if (self->type == bp_static_tracepoint)
348d480f 13422 fprintf_unfiltered (fp, "strace");
d9b3f62e 13423 else if (self->type == bp_tracepoint)
348d480f
PA
13424 fprintf_unfiltered (fp, "trace");
13425 else
13426 internal_error (__FILE__, __LINE__,
d9b3f62e 13427 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13428
f00aae0f 13429 fprintf_unfiltered (fp, " %s",
d28cd78a 13430 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13431 print_recreate_thread (self, fp);
13432
13433 if (tp->pass_count)
13434 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13435}
13436
983af33b 13437static void
f00aae0f
KS
13438tracepoint_create_sals_from_location (const struct event_location *location,
13439 struct linespec_result *canonical,
13440 enum bptype type_wanted)
983af33b 13441{
f00aae0f 13442 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13443}
13444
13445static void
13446tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13447 struct linespec_result *canonical,
983af33b 13448 char *cond_string,
e7e0cddf 13449 char *extra_string,
983af33b
SDJ
13450 enum bptype type_wanted,
13451 enum bpdisp disposition,
13452 int thread,
13453 int task, int ignore_count,
13454 const struct breakpoint_ops *ops,
13455 int from_tty, int enabled,
44f238bb 13456 int internal, unsigned flags)
983af33b 13457{
023fa29b 13458 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13459 cond_string, extra_string,
13460 type_wanted,
983af33b
SDJ
13461 disposition, thread, task,
13462 ignore_count, ops, from_tty,
44f238bb 13463 enabled, internal, flags);
983af33b
SDJ
13464}
13465
13466static void
f00aae0f
KS
13467tracepoint_decode_location (struct breakpoint *b,
13468 const struct event_location *location,
c2f4122d 13469 struct program_space *search_pspace,
983af33b
SDJ
13470 struct symtabs_and_lines *sals)
13471{
c2f4122d 13472 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13473}
13474
2060206e 13475struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13476
55aa24fb
SDJ
13477/* The breakpoint_ops structure to be use on tracepoints placed in a
13478 static probe. */
13479
13480static void
f00aae0f
KS
13481tracepoint_probe_create_sals_from_location
13482 (const struct event_location *location,
13483 struct linespec_result *canonical,
13484 enum bptype type_wanted)
55aa24fb
SDJ
13485{
13486 /* We use the same method for breakpoint on probes. */
f00aae0f 13487 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13488}
13489
13490static void
f00aae0f
KS
13491tracepoint_probe_decode_location (struct breakpoint *b,
13492 const struct event_location *location,
c2f4122d 13493 struct program_space *search_pspace,
55aa24fb
SDJ
13494 struct symtabs_and_lines *sals)
13495{
13496 /* We use the same method for breakpoint on probes. */
c2f4122d 13497 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13498}
13499
13500static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13501
5c2b4418
HZ
13502/* Dprintf breakpoint_ops methods. */
13503
13504static void
13505dprintf_re_set (struct breakpoint *b)
13506{
13507 breakpoint_re_set_default (b);
13508
f00aae0f
KS
13509 /* extra_string should never be non-NULL for dprintf. */
13510 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13511
13512 /* 1 - connect to target 1, that can run breakpoint commands.
13513 2 - create a dprintf, which resolves fine.
13514 3 - disconnect from target 1
13515 4 - connect to target 2, that can NOT run breakpoint commands.
13516
13517 After steps #3/#4, you'll want the dprintf command list to
13518 be updated, because target 1 and 2 may well return different
13519 answers for target_can_run_breakpoint_commands().
13520 Given absence of finer grained resetting, we get to do
13521 it all the time. */
13522 if (b->extra_string != NULL)
13523 update_dprintf_command_list (b);
13524}
13525
2d9442cc
HZ
13526/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13527
13528static void
13529dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13530{
f00aae0f 13531 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13532 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13533 tp->extra_string);
13534 print_recreate_thread (tp, fp);
13535}
13536
9d6e6e84
HZ
13537/* Implement the "after_condition_true" breakpoint_ops method for
13538 dprintf.
13539
13540 dprintf's are implemented with regular commands in their command
13541 list, but we run the commands here instead of before presenting the
13542 stop to the user, as dprintf's don't actually cause a stop. This
13543 also makes it so that the commands of multiple dprintfs at the same
13544 address are all handled. */
13545
13546static void
13547dprintf_after_condition_true (struct bpstats *bs)
13548{
13549 struct cleanup *old_chain;
13550 struct bpstats tmp_bs = { NULL };
13551 struct bpstats *tmp_bs_p = &tmp_bs;
13552
13553 /* dprintf's never cause a stop. This wasn't set in the
13554 check_status hook instead because that would make the dprintf's
13555 condition not be evaluated. */
13556 bs->stop = 0;
13557
13558 /* Run the command list here. Take ownership of it instead of
13559 copying. We never want these commands to run later in
13560 bpstat_do_actions, if a breakpoint that causes a stop happens to
13561 be set at same address as this dprintf, or even if running the
13562 commands here throws. */
13563 tmp_bs.commands = bs->commands;
13564 bs->commands = NULL;
13565 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13566
13567 bpstat_do_actions_1 (&tmp_bs_p);
13568
13569 /* 'tmp_bs.commands' will usually be NULL by now, but
13570 bpstat_do_actions_1 may return early without processing the whole
13571 list. */
13572 do_cleanups (old_chain);
13573}
13574
983af33b
SDJ
13575/* The breakpoint_ops structure to be used on static tracepoints with
13576 markers (`-m'). */
13577
13578static void
f00aae0f 13579strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13580 struct linespec_result *canonical,
f00aae0f 13581 enum bptype type_wanted)
983af33b
SDJ
13582{
13583 struct linespec_sals lsal;
f00aae0f
KS
13584 const char *arg_start, *arg;
13585 char *str;
13586 struct cleanup *cleanup;
983af33b 13587
f00aae0f
KS
13588 arg = arg_start = get_linespec_location (location);
13589 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13590
f00aae0f
KS
13591 str = savestring (arg_start, arg - arg_start);
13592 cleanup = make_cleanup (xfree, str);
8e9e35b1 13593 canonical->location = new_linespec_location (&str);
f00aae0f 13594 do_cleanups (cleanup);
983af33b 13595
8e9e35b1
TT
13596 lsal.canonical
13597 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13598 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13599}
13600
13601static void
13602strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13603 struct linespec_result *canonical,
983af33b 13604 char *cond_string,
e7e0cddf 13605 char *extra_string,
983af33b
SDJ
13606 enum bptype type_wanted,
13607 enum bpdisp disposition,
13608 int thread,
13609 int task, int ignore_count,
13610 const struct breakpoint_ops *ops,
13611 int from_tty, int enabled,
44f238bb 13612 int internal, unsigned flags)
983af33b
SDJ
13613{
13614 int i;
52d361e1
YQ
13615 struct linespec_sals *lsal = VEC_index (linespec_sals,
13616 canonical->sals, 0);
983af33b
SDJ
13617
13618 /* If the user is creating a static tracepoint by marker id
13619 (strace -m MARKER_ID), then store the sals index, so that
13620 breakpoint_re_set can try to match up which of the newly
13621 found markers corresponds to this one, and, don't try to
13622 expand multiple locations for each sal, given than SALS
13623 already should contain all sals for MARKER_ID. */
13624
13625 for (i = 0; i < lsal->sals.nelts; ++i)
13626 {
13627 struct symtabs_and_lines expanded;
13628 struct tracepoint *tp;
ffc2605c 13629 event_location_up location;
983af33b
SDJ
13630
13631 expanded.nelts = 1;
13632 expanded.sals = &lsal->sals.sals[i];
13633
8e9e35b1 13634 location = copy_event_location (canonical->location.get ());
983af33b 13635
4d01a485 13636 tp = new tracepoint ();
983af33b 13637 init_breakpoint_sal (&tp->base, gdbarch, expanded,
ffc2605c 13638 std::move (location), NULL,
e7e0cddf
SS
13639 cond_string, extra_string,
13640 type_wanted, disposition,
983af33b 13641 thread, task, ignore_count, ops,
44f238bb 13642 from_tty, enabled, internal, flags,
983af33b
SDJ
13643 canonical->special_display);
13644 /* Given that its possible to have multiple markers with
13645 the same string id, if the user is creating a static
13646 tracepoint by marker id ("strace -m MARKER_ID"), then
13647 store the sals index, so that breakpoint_re_set can
13648 try to match up which of the newly found markers
13649 corresponds to this one */
13650 tp->static_trace_marker_id_idx = i;
13651
13652 install_breakpoint (internal, &tp->base, 0);
983af33b
SDJ
13653 }
13654}
13655
13656static void
f00aae0f
KS
13657strace_marker_decode_location (struct breakpoint *b,
13658 const struct event_location *location,
c2f4122d 13659 struct program_space *search_pspace,
983af33b
SDJ
13660 struct symtabs_and_lines *sals)
13661{
13662 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13663 const char *s = get_linespec_location (location);
983af33b 13664
f00aae0f 13665 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13666 if (sals->nelts > tp->static_trace_marker_id_idx)
13667 {
13668 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13669 sals->nelts = 1;
13670 }
13671 else
13672 error (_("marker %s not found"), tp->static_trace_marker_id);
13673}
13674
13675static struct breakpoint_ops strace_marker_breakpoint_ops;
13676
13677static int
13678strace_marker_p (struct breakpoint *b)
13679{
13680 return b->ops == &strace_marker_breakpoint_ops;
13681}
13682
53a5351d 13683/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13684 structures. */
c906108c
SS
13685
13686void
fba45db2 13687delete_breakpoint (struct breakpoint *bpt)
c906108c 13688{
52f0bd74 13689 struct breakpoint *b;
c906108c 13690
8a3fe4f8 13691 gdb_assert (bpt != NULL);
c906108c 13692
4a64f543
MS
13693 /* Has this bp already been deleted? This can happen because
13694 multiple lists can hold pointers to bp's. bpstat lists are
13695 especial culprits.
13696
13697 One example of this happening is a watchpoint's scope bp. When
13698 the scope bp triggers, we notice that the watchpoint is out of
13699 scope, and delete it. We also delete its scope bp. But the
13700 scope bp is marked "auto-deleting", and is already on a bpstat.
13701 That bpstat is then checked for auto-deleting bp's, which are
13702 deleted.
13703
13704 A real solution to this problem might involve reference counts in
13705 bp's, and/or giving them pointers back to their referencing
13706 bpstat's, and teaching delete_breakpoint to only free a bp's
13707 storage when no more references were extent. A cheaper bandaid
13708 was chosen. */
c906108c
SS
13709 if (bpt->type == bp_none)
13710 return;
13711
4a64f543
MS
13712 /* At least avoid this stale reference until the reference counting
13713 of breakpoints gets resolved. */
d0fb5eae 13714 if (bpt->related_breakpoint != bpt)
e5a0a904 13715 {
d0fb5eae 13716 struct breakpoint *related;
3a5c3e22 13717 struct watchpoint *w;
d0fb5eae
JK
13718
13719 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13720 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13721 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13722 w = (struct watchpoint *) bpt;
13723 else
13724 w = NULL;
13725 if (w != NULL)
13726 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13727
13728 /* Unlink bpt from the bpt->related_breakpoint ring. */
13729 for (related = bpt; related->related_breakpoint != bpt;
13730 related = related->related_breakpoint);
13731 related->related_breakpoint = bpt->related_breakpoint;
13732 bpt->related_breakpoint = bpt;
e5a0a904
JK
13733 }
13734
a9634178
TJB
13735 /* watch_command_1 creates a watchpoint but only sets its number if
13736 update_watchpoint succeeds in creating its bp_locations. If there's
13737 a problem in that process, we'll be asked to delete the half-created
13738 watchpoint. In that case, don't announce the deletion. */
13739 if (bpt->number)
13740 observer_notify_breakpoint_deleted (bpt);
c906108c 13741
c906108c
SS
13742 if (breakpoint_chain == bpt)
13743 breakpoint_chain = bpt->next;
13744
c906108c
SS
13745 ALL_BREAKPOINTS (b)
13746 if (b->next == bpt)
c5aa993b
JM
13747 {
13748 b->next = bpt->next;
13749 break;
13750 }
c906108c 13751
f431efe5
PA
13752 /* Be sure no bpstat's are pointing at the breakpoint after it's
13753 been freed. */
13754 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13755 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13756 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13757 commands are associated with the bpstat; if we remove it here,
13758 then the later call to bpstat_do_actions (&stop_bpstat); in
13759 event-top.c won't do anything, and temporary breakpoints with
13760 commands won't work. */
13761
13762 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13763
4a64f543
MS
13764 /* Now that breakpoint is removed from breakpoint list, update the
13765 global location list. This will remove locations that used to
13766 belong to this breakpoint. Do this before freeing the breakpoint
13767 itself, since remove_breakpoint looks at location's owner. It
13768 might be better design to have location completely
13769 self-contained, but it's not the case now. */
44702360 13770 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13771
348d480f 13772 bpt->ops->dtor (bpt);
4a64f543
MS
13773 /* On the chance that someone will soon try again to delete this
13774 same bp, we mark it as deleted before freeing its storage. */
c906108c 13775 bpt->type = bp_none;
4d01a485 13776 delete bpt;
c906108c
SS
13777}
13778
4d6140d9
AC
13779static void
13780do_delete_breakpoint_cleanup (void *b)
13781{
9a3c8263 13782 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13783}
13784
13785struct cleanup *
13786make_cleanup_delete_breakpoint (struct breakpoint *b)
13787{
13788 return make_cleanup (do_delete_breakpoint_cleanup, b);
13789}
13790
51be5b68
PA
13791/* Iterator function to call a user-provided callback function once
13792 for each of B and its related breakpoints. */
13793
13794static void
13795iterate_over_related_breakpoints (struct breakpoint *b,
13796 void (*function) (struct breakpoint *,
13797 void *),
13798 void *data)
13799{
13800 struct breakpoint *related;
13801
13802 related = b;
13803 do
13804 {
13805 struct breakpoint *next;
13806
13807 /* FUNCTION may delete RELATED. */
13808 next = related->related_breakpoint;
13809
13810 if (next == related)
13811 {
13812 /* RELATED is the last ring entry. */
13813 function (related, data);
13814
13815 /* FUNCTION may have deleted it, so we'd never reach back to
13816 B. There's nothing left to do anyway, so just break
13817 out. */
13818 break;
13819 }
13820 else
13821 function (related, data);
13822
13823 related = next;
13824 }
13825 while (related != b);
13826}
95a42b64
TT
13827
13828static void
13829do_delete_breakpoint (struct breakpoint *b, void *ignore)
13830{
13831 delete_breakpoint (b);
13832}
13833
51be5b68
PA
13834/* A callback for map_breakpoint_numbers that calls
13835 delete_breakpoint. */
13836
13837static void
13838do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13839{
13840 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13841}
13842
c906108c 13843void
fba45db2 13844delete_command (char *arg, int from_tty)
c906108c 13845{
35df4500 13846 struct breakpoint *b, *b_tmp;
c906108c 13847
ea9365bb
TT
13848 dont_repeat ();
13849
c906108c
SS
13850 if (arg == 0)
13851 {
13852 int breaks_to_delete = 0;
13853
46c6471b
PA
13854 /* Delete all breakpoints if no argument. Do not delete
13855 internal breakpoints, these have to be deleted with an
13856 explicit breakpoint number argument. */
c5aa993b 13857 ALL_BREAKPOINTS (b)
46c6471b 13858 if (user_breakpoint_p (b))
973d738b
DJ
13859 {
13860 breaks_to_delete = 1;
13861 break;
13862 }
c906108c
SS
13863
13864 /* Ask user only if there are some breakpoints to delete. */
13865 if (!from_tty
e2e0b3e5 13866 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13867 {
35df4500 13868 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13869 if (user_breakpoint_p (b))
c5aa993b 13870 delete_breakpoint (b);
c906108c
SS
13871 }
13872 }
13873 else
51be5b68 13874 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13875}
13876
c2f4122d
PA
13877/* Return true if all locations of B bound to PSPACE are pending. If
13878 PSPACE is NULL, all locations of all program spaces are
13879 considered. */
13880
0d381245 13881static int
c2f4122d 13882all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13883{
c2f4122d
PA
13884 struct bp_location *loc;
13885
13886 for (loc = b->loc; loc != NULL; loc = loc->next)
13887 if ((pspace == NULL
13888 || loc->pspace == pspace)
13889 && !loc->shlib_disabled
8645ff69 13890 && !loc->pspace->executing_startup)
0d381245
VP
13891 return 0;
13892 return 1;
fe3f5fa8
VP
13893}
13894
776592bf
DE
13895/* Subroutine of update_breakpoint_locations to simplify it.
13896 Return non-zero if multiple fns in list LOC have the same name.
13897 Null names are ignored. */
13898
13899static int
13900ambiguous_names_p (struct bp_location *loc)
13901{
13902 struct bp_location *l;
13903 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13904 (int (*) (const void *,
13905 const void *)) streq,
776592bf
DE
13906 NULL, xcalloc, xfree);
13907
13908 for (l = loc; l != NULL; l = l->next)
13909 {
13910 const char **slot;
13911 const char *name = l->function_name;
13912
13913 /* Allow for some names to be NULL, ignore them. */
13914 if (name == NULL)
13915 continue;
13916
13917 slot = (const char **) htab_find_slot (htab, (const void *) name,
13918 INSERT);
4a64f543
MS
13919 /* NOTE: We can assume slot != NULL here because xcalloc never
13920 returns NULL. */
776592bf
DE
13921 if (*slot != NULL)
13922 {
13923 htab_delete (htab);
13924 return 1;
13925 }
13926 *slot = name;
13927 }
13928
13929 htab_delete (htab);
13930 return 0;
13931}
13932
0fb4aa4b
PA
13933/* When symbols change, it probably means the sources changed as well,
13934 and it might mean the static tracepoint markers are no longer at
13935 the same address or line numbers they used to be at last we
13936 checked. Losing your static tracepoints whenever you rebuild is
13937 undesirable. This function tries to resync/rematch gdb static
13938 tracepoints with the markers on the target, for static tracepoints
13939 that have not been set by marker id. Static tracepoint that have
13940 been set by marker id are reset by marker id in breakpoint_re_set.
13941 The heuristic is:
13942
13943 1) For a tracepoint set at a specific address, look for a marker at
13944 the old PC. If one is found there, assume to be the same marker.
13945 If the name / string id of the marker found is different from the
13946 previous known name, assume that means the user renamed the marker
13947 in the sources, and output a warning.
13948
13949 2) For a tracepoint set at a given line number, look for a marker
13950 at the new address of the old line number. If one is found there,
13951 assume to be the same marker. If the name / string id of the
13952 marker found is different from the previous known name, assume that
13953 means the user renamed the marker in the sources, and output a
13954 warning.
13955
13956 3) If a marker is no longer found at the same address or line, it
13957 may mean the marker no longer exists. But it may also just mean
13958 the code changed a bit. Maybe the user added a few lines of code
13959 that made the marker move up or down (in line number terms). Ask
13960 the target for info about the marker with the string id as we knew
13961 it. If found, update line number and address in the matching
13962 static tracepoint. This will get confused if there's more than one
13963 marker with the same ID (possible in UST, although unadvised
13964 precisely because it confuses tools). */
13965
13966static struct symtab_and_line
13967update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13968{
d9b3f62e 13969 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13970 struct static_tracepoint_marker marker;
13971 CORE_ADDR pc;
0fb4aa4b
PA
13972
13973 pc = sal.pc;
13974 if (sal.line)
13975 find_line_pc (sal.symtab, sal.line, &pc);
13976
13977 if (target_static_tracepoint_marker_at (pc, &marker))
13978 {
d9b3f62e 13979 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13980 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13981 b->number,
d9b3f62e 13982 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13983
d9b3f62e
PA
13984 xfree (tp->static_trace_marker_id);
13985 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13986 release_static_tracepoint_marker (&marker);
13987
13988 return sal;
13989 }
13990
13991 /* Old marker wasn't found on target at lineno. Try looking it up
13992 by string ID. */
13993 if (!sal.explicit_pc
13994 && sal.line != 0
13995 && sal.symtab != NULL
d9b3f62e 13996 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13997 {
13998 VEC(static_tracepoint_marker_p) *markers;
13999
14000 markers
d9b3f62e 14001 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14002
14003 if (!VEC_empty(static_tracepoint_marker_p, markers))
14004 {
80e1d417 14005 struct symtab_and_line sal2;
0fb4aa4b 14006 struct symbol *sym;
80e1d417 14007 struct static_tracepoint_marker *tpmarker;
79a45e25 14008 struct ui_out *uiout = current_uiout;
67994074 14009 struct explicit_location explicit_loc;
0fb4aa4b 14010
80e1d417 14011 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14012
d9b3f62e 14013 xfree (tp->static_trace_marker_id);
80e1d417 14014 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14015
14016 warning (_("marker for static tracepoint %d (%s) not "
14017 "found at previous line number"),
d9b3f62e 14018 b->number, tp->static_trace_marker_id);
0fb4aa4b 14019
80e1d417 14020 init_sal (&sal2);
0fb4aa4b 14021
80e1d417 14022 sal2.pc = tpmarker->address;
0fb4aa4b 14023
80e1d417
AS
14024 sal2 = find_pc_line (tpmarker->address, 0);
14025 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 14026 uiout->text ("Now in ");
0fb4aa4b
PA
14027 if (sym)
14028 {
112e8700
SM
14029 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14030 uiout->text (" at ");
0fb4aa4b 14031 }
112e8700 14032 uiout->field_string ("file",
05cba821 14033 symtab_to_filename_for_display (sal2.symtab));
112e8700 14034 uiout->text (":");
0fb4aa4b 14035
112e8700 14036 if (uiout->is_mi_like_p ())
0fb4aa4b 14037 {
0b0865da 14038 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14039
112e8700 14040 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
14041 }
14042
112e8700
SM
14043 uiout->field_int ("line", sal2.line);
14044 uiout->text ("\n");
0fb4aa4b 14045
80e1d417 14046 b->loc->line_number = sal2.line;
2f202fde 14047 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14048
d28cd78a 14049 b->location.reset (NULL);
67994074
KS
14050 initialize_explicit_location (&explicit_loc);
14051 explicit_loc.source_filename
00e52e53 14052 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14053 explicit_loc.line_offset.offset = b->loc->line_number;
14054 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 14055 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14056
14057 /* Might be nice to check if function changed, and warn if
14058 so. */
14059
80e1d417 14060 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14061 }
14062 }
14063 return sal;
14064}
14065
8d3788bd
VP
14066/* Returns 1 iff locations A and B are sufficiently same that
14067 we don't need to report breakpoint as changed. */
14068
14069static int
14070locations_are_equal (struct bp_location *a, struct bp_location *b)
14071{
14072 while (a && b)
14073 {
14074 if (a->address != b->address)
14075 return 0;
14076
14077 if (a->shlib_disabled != b->shlib_disabled)
14078 return 0;
14079
14080 if (a->enabled != b->enabled)
14081 return 0;
14082
14083 a = a->next;
14084 b = b->next;
14085 }
14086
14087 if ((a == NULL) != (b == NULL))
14088 return 0;
14089
14090 return 1;
14091}
14092
c2f4122d
PA
14093/* Split all locations of B that are bound to PSPACE out of B's
14094 location list to a separate list and return that list's head. If
14095 PSPACE is NULL, hoist out all locations of B. */
14096
14097static struct bp_location *
14098hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14099{
14100 struct bp_location head;
14101 struct bp_location *i = b->loc;
14102 struct bp_location **i_link = &b->loc;
14103 struct bp_location *hoisted = &head;
14104
14105 if (pspace == NULL)
14106 {
14107 i = b->loc;
14108 b->loc = NULL;
14109 return i;
14110 }
14111
14112 head.next = NULL;
14113
14114 while (i != NULL)
14115 {
14116 if (i->pspace == pspace)
14117 {
14118 *i_link = i->next;
14119 i->next = NULL;
14120 hoisted->next = i;
14121 hoisted = i;
14122 }
14123 else
14124 i_link = &i->next;
14125 i = *i_link;
14126 }
14127
14128 return head.next;
14129}
14130
14131/* Create new breakpoint locations for B (a hardware or software
14132 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14133 zero, then B is a ranged breakpoint. Only recreates locations for
14134 FILTER_PSPACE. Locations of other program spaces are left
14135 untouched. */
f1310107 14136
0e30163f 14137void
0d381245 14138update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14139 struct program_space *filter_pspace,
f1310107
TJB
14140 struct symtabs_and_lines sals,
14141 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14142{
14143 int i;
c2f4122d 14144 struct bp_location *existing_locations;
0d381245 14145
f8eba3c6
TT
14146 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14147 {
14148 /* Ranged breakpoints have only one start location and one end
14149 location. */
14150 b->enable_state = bp_disabled;
f8eba3c6
TT
14151 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14152 "multiple locations found\n"),
14153 b->number);
14154 return;
14155 }
f1310107 14156
4a64f543
MS
14157 /* If there's no new locations, and all existing locations are
14158 pending, don't do anything. This optimizes the common case where
14159 all locations are in the same shared library, that was unloaded.
14160 We'd like to retain the location, so that when the library is
14161 loaded again, we don't loose the enabled/disabled status of the
14162 individual locations. */
c2f4122d 14163 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14164 return;
14165
c2f4122d 14166 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14167
0d381245 14168 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14169 {
f8eba3c6
TT
14170 struct bp_location *new_loc;
14171
14172 switch_to_program_space_and_thread (sals.sals[i].pspace);
14173
14174 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14175
0d381245
VP
14176 /* Reparse conditions, they might contain references to the
14177 old symtab. */
14178 if (b->cond_string != NULL)
14179 {
bbc13ae3 14180 const char *s;
fe3f5fa8 14181
0d381245 14182 s = b->cond_string;
492d29ea 14183 TRY
0d381245 14184 {
1bb9788d
TT
14185 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14186 block_for_pc (sals.sals[i].pc),
0d381245
VP
14187 0);
14188 }
492d29ea 14189 CATCH (e, RETURN_MASK_ERROR)
0d381245 14190 {
3e43a32a
MS
14191 warning (_("failed to reevaluate condition "
14192 "for breakpoint %d: %s"),
0d381245
VP
14193 b->number, e.message);
14194 new_loc->enabled = 0;
14195 }
492d29ea 14196 END_CATCH
0d381245 14197 }
fe3f5fa8 14198
f1310107
TJB
14199 if (sals_end.nelts)
14200 {
14201 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14202
14203 new_loc->length = end - sals.sals[0].pc + 1;
14204 }
0d381245 14205 }
fe3f5fa8 14206
4a64f543
MS
14207 /* If possible, carry over 'disable' status from existing
14208 breakpoints. */
0d381245
VP
14209 {
14210 struct bp_location *e = existing_locations;
776592bf
DE
14211 /* If there are multiple breakpoints with the same function name,
14212 e.g. for inline functions, comparing function names won't work.
14213 Instead compare pc addresses; this is just a heuristic as things
14214 may have moved, but in practice it gives the correct answer
14215 often enough until a better solution is found. */
14216 int have_ambiguous_names = ambiguous_names_p (b->loc);
14217
0d381245
VP
14218 for (; e; e = e->next)
14219 {
14220 if (!e->enabled && e->function_name)
14221 {
14222 struct bp_location *l = b->loc;
776592bf
DE
14223 if (have_ambiguous_names)
14224 {
14225 for (; l; l = l->next)
f1310107 14226 if (breakpoint_locations_match (e, l))
776592bf
DE
14227 {
14228 l->enabled = 0;
14229 break;
14230 }
14231 }
14232 else
14233 {
14234 for (; l; l = l->next)
14235 if (l->function_name
14236 && strcmp (e->function_name, l->function_name) == 0)
14237 {
14238 l->enabled = 0;
14239 break;
14240 }
14241 }
0d381245
VP
14242 }
14243 }
14244 }
fe3f5fa8 14245
8d3788bd
VP
14246 if (!locations_are_equal (existing_locations, b->loc))
14247 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14248}
14249
f00aae0f 14250/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14251 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14252
14253static struct symtabs_and_lines
f00aae0f 14254location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14255 struct program_space *search_pspace, int *found)
ef23e705 14256{
02d20e4a 14257 struct symtabs_and_lines sals = {0};
492d29ea 14258 struct gdb_exception exception = exception_none;
ef23e705 14259
983af33b 14260 gdb_assert (b->ops != NULL);
ef23e705 14261
492d29ea 14262 TRY
ef23e705 14263 {
c2f4122d 14264 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14265 }
492d29ea 14266 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14267 {
14268 int not_found_and_ok = 0;
492d29ea
PA
14269
14270 exception = e;
14271
ef23e705
TJB
14272 /* For pending breakpoints, it's expected that parsing will
14273 fail until the right shared library is loaded. User has
14274 already told to create pending breakpoints and don't need
14275 extra messages. If breakpoint is in bp_shlib_disabled
14276 state, then user already saw the message about that
14277 breakpoint being disabled, and don't want to see more
14278 errors. */
58438ac1 14279 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14280 && (b->condition_not_parsed
14281 || (b->loc != NULL
14282 && search_pspace != NULL
14283 && b->loc->pspace != search_pspace)
ef23e705 14284 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14285 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14286 || b->enable_state == bp_disabled))
14287 not_found_and_ok = 1;
14288
14289 if (!not_found_and_ok)
14290 {
14291 /* We surely don't want to warn about the same breakpoint
14292 10 times. One solution, implemented here, is disable
14293 the breakpoint on error. Another solution would be to
14294 have separate 'warning emitted' flag. Since this
14295 happens only when a binary has changed, I don't know
14296 which approach is better. */
14297 b->enable_state = bp_disabled;
14298 throw_exception (e);
14299 }
14300 }
492d29ea 14301 END_CATCH
ef23e705 14302
492d29ea 14303 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14304 {
f8eba3c6 14305 int i;
ef23e705 14306
f8eba3c6
TT
14307 for (i = 0; i < sals.nelts; ++i)
14308 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14309 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14310 {
ed1d1739
KS
14311 char *cond_string, *extra_string;
14312 int thread, task;
ef23e705 14313
f00aae0f 14314 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14315 &cond_string, &thread, &task,
14316 &extra_string);
f00aae0f 14317 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14318 if (cond_string)
14319 b->cond_string = cond_string;
14320 b->thread = thread;
14321 b->task = task;
e7e0cddf 14322 if (extra_string)
f00aae0f
KS
14323 {
14324 xfree (b->extra_string);
14325 b->extra_string = extra_string;
14326 }
ef23e705
TJB
14327 b->condition_not_parsed = 0;
14328 }
14329
983af33b 14330 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14331 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14332
58438ac1
TT
14333 *found = 1;
14334 }
14335 else
14336 *found = 0;
ef23e705
TJB
14337
14338 return sals;
14339}
14340
348d480f
PA
14341/* The default re_set method, for typical hardware or software
14342 breakpoints. Reevaluate the breakpoint and recreate its
14343 locations. */
14344
14345static void
28010a5d 14346breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14347{
14348 int found;
f1310107 14349 struct symtabs_and_lines sals, sals_end;
ef23e705 14350 struct symtabs_and_lines expanded = {0};
f1310107 14351 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14352 struct program_space *filter_pspace = current_program_space;
ef23e705 14353
d28cd78a 14354 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14355 if (found)
14356 {
14357 make_cleanup (xfree, sals.sals);
f8eba3c6 14358 expanded = sals;
ef23e705
TJB
14359 }
14360
f00aae0f 14361 if (b->location_range_end != NULL)
f1310107 14362 {
d28cd78a 14363 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14364 filter_pspace, &found);
f1310107
TJB
14365 if (found)
14366 {
14367 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14368 expanded_end = sals_end;
f1310107
TJB
14369 }
14370 }
14371
c2f4122d 14372 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14373}
14374
983af33b
SDJ
14375/* Default method for creating SALs from an address string. It basically
14376 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14377
14378static void
f00aae0f
KS
14379create_sals_from_location_default (const struct event_location *location,
14380 struct linespec_result *canonical,
14381 enum bptype type_wanted)
983af33b 14382{
f00aae0f 14383 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14384}
14385
14386/* Call create_breakpoints_sal for the given arguments. This is the default
14387 function for the `create_breakpoints_sal' method of
14388 breakpoint_ops. */
14389
14390static void
14391create_breakpoints_sal_default (struct gdbarch *gdbarch,
14392 struct linespec_result *canonical,
983af33b 14393 char *cond_string,
e7e0cddf 14394 char *extra_string,
983af33b
SDJ
14395 enum bptype type_wanted,
14396 enum bpdisp disposition,
14397 int thread,
14398 int task, int ignore_count,
14399 const struct breakpoint_ops *ops,
14400 int from_tty, int enabled,
44f238bb 14401 int internal, unsigned flags)
983af33b
SDJ
14402{
14403 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14404 extra_string,
983af33b
SDJ
14405 type_wanted, disposition,
14406 thread, task, ignore_count, ops, from_tty,
44f238bb 14407 enabled, internal, flags);
983af33b
SDJ
14408}
14409
14410/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14411 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14412
14413static void
f00aae0f
KS
14414decode_location_default (struct breakpoint *b,
14415 const struct event_location *location,
c2f4122d 14416 struct program_space *search_pspace,
983af33b
SDJ
14417 struct symtabs_and_lines *sals)
14418{
14419 struct linespec_result canonical;
14420
c2f4122d 14421 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14422 (struct symtab *) NULL, 0,
14423 &canonical, multiple_symbols_all,
14424 b->filter);
14425
14426 /* We should get 0 or 1 resulting SALs. */
14427 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14428
14429 if (VEC_length (linespec_sals, canonical.sals) > 0)
14430 {
14431 struct linespec_sals *lsal;
14432
14433 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14434 *sals = lsal->sals;
14435 /* Arrange it so the destructor does not free the
14436 contents. */
14437 lsal->sals.sals = NULL;
14438 }
983af33b
SDJ
14439}
14440
28010a5d
PA
14441/* Prepare the global context for a re-set of breakpoint B. */
14442
14443static struct cleanup *
14444prepare_re_set_context (struct breakpoint *b)
14445{
28010a5d 14446 input_radix = b->input_radix;
28010a5d
PA
14447 set_language (b->language);
14448
c2f4122d 14449 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14450}
14451
c906108c
SS
14452/* Reset a breakpoint given it's struct breakpoint * BINT.
14453 The value we return ends up being the return value from catch_errors.
14454 Unused in this case. */
14455
14456static int
4efb68b1 14457breakpoint_re_set_one (void *bint)
c906108c 14458{
4a64f543 14459 /* Get past catch_errs. */
53a5351d 14460 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14461 struct cleanup *cleanups;
c906108c 14462
348d480f
PA
14463 cleanups = prepare_re_set_context (b);
14464 b->ops->re_set (b);
14465 do_cleanups (cleanups);
c906108c
SS
14466 return 0;
14467}
14468
c2f4122d
PA
14469/* Re-set breakpoint locations for the current program space.
14470 Locations bound to other program spaces are left untouched. */
14471
c906108c 14472void
69de3c6a 14473breakpoint_re_set (void)
c906108c 14474{
35df4500 14475 struct breakpoint *b, *b_tmp;
c906108c
SS
14476 enum language save_language;
14477 int save_input_radix;
c5aa993b 14478
c906108c
SS
14479 save_language = current_language->la_language;
14480 save_input_radix = input_radix;
2a7f3dff 14481
c5aa993b 14482 {
5ed8105e 14483 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14484
5ed8105e
PA
14485 /* Note: we must not try to insert locations until after all
14486 breakpoints have been re-set. Otherwise, e.g., when re-setting
14487 breakpoint 1, we'd insert the locations of breakpoint 2, which
14488 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14489
5ed8105e
PA
14490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14491 {
14492 /* Format possible error msg. */
14493 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14494 b->number);
14495 struct cleanup *cleanups = make_cleanup (xfree, message);
14496 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14497 do_cleanups (cleanups);
14498 }
14499 set_language (save_language);
14500 input_radix = save_input_radix;
14501
14502 jit_breakpoint_re_set ();
14503 }
6c95b8df 14504
af02033e
PP
14505 create_overlay_event_breakpoint ();
14506 create_longjmp_master_breakpoint ();
14507 create_std_terminate_master_breakpoint ();
186c406b 14508 create_exception_master_breakpoint ();
2a7f3dff
PA
14509
14510 /* Now we can insert. */
14511 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14512}
14513\f
c906108c
SS
14514/* Reset the thread number of this breakpoint:
14515
14516 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14517 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14518void
fba45db2 14519breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14520{
14521 if (b->thread != -1)
14522 {
39f77062 14523 if (in_thread_list (inferior_ptid))
5d5658a1 14524 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14525
14526 /* We're being called after following a fork. The new fork is
14527 selected as current, and unless this was a vfork will have a
14528 different program space from the original thread. Reset that
14529 as well. */
14530 b->loc->pspace = current_program_space;
c906108c
SS
14531 }
14532}
14533
03ac34d5
MS
14534/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14535 If from_tty is nonzero, it prints a message to that effect,
14536 which ends with a period (no newline). */
14537
c906108c 14538void
fba45db2 14539set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14540{
52f0bd74 14541 struct breakpoint *b;
c906108c
SS
14542
14543 if (count < 0)
14544 count = 0;
14545
14546 ALL_BREAKPOINTS (b)
14547 if (b->number == bptnum)
c5aa993b 14548 {
d77f58be
SS
14549 if (is_tracepoint (b))
14550 {
14551 if (from_tty && count != 0)
14552 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14553 bptnum);
14554 return;
14555 }
14556
c5aa993b 14557 b->ignore_count = count;
221ea385
KS
14558 if (from_tty)
14559 {
14560 if (count == 0)
3e43a32a
MS
14561 printf_filtered (_("Will stop next time "
14562 "breakpoint %d is reached."),
221ea385
KS
14563 bptnum);
14564 else if (count == 1)
a3f17187 14565 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14566 bptnum);
14567 else
3e43a32a
MS
14568 printf_filtered (_("Will ignore next %d "
14569 "crossings of breakpoint %d."),
221ea385
KS
14570 count, bptnum);
14571 }
8d3788bd 14572 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14573 return;
14574 }
c906108c 14575
8a3fe4f8 14576 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14577}
14578
c906108c
SS
14579/* Command to set ignore-count of breakpoint N to COUNT. */
14580
14581static void
fba45db2 14582ignore_command (char *args, int from_tty)
c906108c
SS
14583{
14584 char *p = args;
52f0bd74 14585 int num;
c906108c
SS
14586
14587 if (p == 0)
e2e0b3e5 14588 error_no_arg (_("a breakpoint number"));
c5aa993b 14589
c906108c 14590 num = get_number (&p);
5c44784c 14591 if (num == 0)
8a3fe4f8 14592 error (_("bad breakpoint number: '%s'"), args);
c906108c 14593 if (*p == 0)
8a3fe4f8 14594 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14595
14596 set_ignore_count (num,
14597 longest_to_int (value_as_long (parse_and_eval (p))),
14598 from_tty);
221ea385
KS
14599 if (from_tty)
14600 printf_filtered ("\n");
c906108c
SS
14601}
14602\f
14603/* Call FUNCTION on each of the breakpoints
14604 whose numbers are given in ARGS. */
14605
14606static void
896b6bda
PA
14607map_breakpoint_numbers (const char *args,
14608 void (*function) (struct breakpoint *,
14609 void *),
95a42b64 14610 void *data)
c906108c 14611{
52f0bd74
AC
14612 int num;
14613 struct breakpoint *b, *tmp;
c906108c 14614
b9d61307 14615 if (args == 0 || *args == '\0')
e2e0b3e5 14616 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14617
bfd28288 14618 number_or_range_parser parser (args);
197f0a60 14619
bfd28288 14620 while (!parser.finished ())
c906108c 14621 {
bfd28288
PA
14622 const char *p = parser.cur_tok ();
14623 bool match = false;
197f0a60 14624
bfd28288 14625 num = parser.get_number ();
5c44784c 14626 if (num == 0)
c5aa993b 14627 {
8a3fe4f8 14628 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14629 }
14630 else
14631 {
14632 ALL_BREAKPOINTS_SAFE (b, tmp)
14633 if (b->number == num)
14634 {
bfd28288 14635 match = true;
cdac0397 14636 function (b, data);
11cf8741 14637 break;
5c44784c 14638 }
bfd28288 14639 if (!match)
a3f17187 14640 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14641 }
c906108c
SS
14642 }
14643}
14644
0d381245
VP
14645static struct bp_location *
14646find_location_by_number (char *number)
14647{
14648 char *dot = strchr (number, '.');
14649 char *p1;
14650 int bp_num;
14651 int loc_num;
14652 struct breakpoint *b;
14653 struct bp_location *loc;
14654
14655 *dot = '\0';
14656
14657 p1 = number;
197f0a60 14658 bp_num = get_number (&p1);
0d381245
VP
14659 if (bp_num == 0)
14660 error (_("Bad breakpoint number '%s'"), number);
14661
14662 ALL_BREAKPOINTS (b)
14663 if (b->number == bp_num)
14664 {
14665 break;
14666 }
14667
14668 if (!b || b->number != bp_num)
14669 error (_("Bad breakpoint number '%s'"), number);
14670
14671 p1 = dot+1;
197f0a60 14672 loc_num = get_number (&p1);
0d381245
VP
14673 if (loc_num == 0)
14674 error (_("Bad breakpoint location number '%s'"), number);
14675
14676 --loc_num;
14677 loc = b->loc;
14678 for (;loc_num && loc; --loc_num, loc = loc->next)
14679 ;
14680 if (!loc)
14681 error (_("Bad breakpoint location number '%s'"), dot+1);
14682
14683 return loc;
14684}
14685
14686
1900040c
MS
14687/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14688 If from_tty is nonzero, it prints a message to that effect,
14689 which ends with a period (no newline). */
14690
c906108c 14691void
fba45db2 14692disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14693{
14694 /* Never disable a watchpoint scope breakpoint; we want to
14695 hit them when we leave scope so we can delete both the
14696 watchpoint and its scope breakpoint at that time. */
14697 if (bpt->type == bp_watchpoint_scope)
14698 return;
14699
b5de0fa7 14700 bpt->enable_state = bp_disabled;
c906108c 14701
b775012e
LM
14702 /* Mark breakpoint locations modified. */
14703 mark_breakpoint_modified (bpt);
14704
d248b706
KY
14705 if (target_supports_enable_disable_tracepoint ()
14706 && current_trace_status ()->running && is_tracepoint (bpt))
14707 {
14708 struct bp_location *location;
14709
14710 for (location = bpt->loc; location; location = location->next)
14711 target_disable_tracepoint (location);
14712 }
14713
44702360 14714 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14715
8d3788bd 14716 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14717}
14718
51be5b68
PA
14719/* A callback for iterate_over_related_breakpoints. */
14720
14721static void
14722do_disable_breakpoint (struct breakpoint *b, void *ignore)
14723{
14724 disable_breakpoint (b);
14725}
14726
95a42b64
TT
14727/* A callback for map_breakpoint_numbers that calls
14728 disable_breakpoint. */
14729
14730static void
14731do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14732{
51be5b68 14733 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14734}
14735
c906108c 14736static void
fba45db2 14737disable_command (char *args, int from_tty)
c906108c 14738{
c906108c 14739 if (args == 0)
46c6471b
PA
14740 {
14741 struct breakpoint *bpt;
14742
14743 ALL_BREAKPOINTS (bpt)
14744 if (user_breakpoint_p (bpt))
14745 disable_breakpoint (bpt);
14746 }
9eaabc75 14747 else
0d381245 14748 {
9eaabc75
MW
14749 char *num = extract_arg (&args);
14750
14751 while (num)
d248b706 14752 {
9eaabc75 14753 if (strchr (num, '.'))
b775012e 14754 {
9eaabc75
MW
14755 struct bp_location *loc = find_location_by_number (num);
14756
14757 if (loc)
14758 {
14759 if (loc->enabled)
14760 {
14761 loc->enabled = 0;
14762 mark_breakpoint_location_modified (loc);
14763 }
14764 if (target_supports_enable_disable_tracepoint ()
14765 && current_trace_status ()->running && loc->owner
14766 && is_tracepoint (loc->owner))
14767 target_disable_tracepoint (loc);
14768 }
44702360 14769 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14770 }
9eaabc75
MW
14771 else
14772 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14773 num = extract_arg (&args);
d248b706 14774 }
0d381245 14775 }
c906108c
SS
14776}
14777
14778static void
816338b5
SS
14779enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14780 int count)
c906108c 14781{
afe38095 14782 int target_resources_ok;
c906108c
SS
14783
14784 if (bpt->type == bp_hardware_breakpoint)
14785 {
14786 int i;
c5aa993b 14787 i = hw_breakpoint_used_count ();
53a5351d 14788 target_resources_ok =
d92524f1 14789 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14790 i + 1, 0);
c906108c 14791 if (target_resources_ok == 0)
8a3fe4f8 14792 error (_("No hardware breakpoint support in the target."));
c906108c 14793 else if (target_resources_ok < 0)
8a3fe4f8 14794 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14795 }
14796
cc60f2e3 14797 if (is_watchpoint (bpt))
c906108c 14798 {
d07205c2 14799 /* Initialize it just to avoid a GCC false warning. */
f486487f 14800 enum enable_state orig_enable_state = bp_disabled;
dde02812 14801
492d29ea 14802 TRY
c906108c 14803 {
3a5c3e22
PA
14804 struct watchpoint *w = (struct watchpoint *) bpt;
14805
1e718ff1
TJB
14806 orig_enable_state = bpt->enable_state;
14807 bpt->enable_state = bp_enabled;
3a5c3e22 14808 update_watchpoint (w, 1 /* reparse */);
c906108c 14809 }
492d29ea 14810 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14811 {
1e718ff1 14812 bpt->enable_state = orig_enable_state;
dde02812
ES
14813 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14814 bpt->number);
14815 return;
c5aa993b 14816 }
492d29ea 14817 END_CATCH
c906108c 14818 }
0101ce28 14819
b775012e
LM
14820 bpt->enable_state = bp_enabled;
14821
14822 /* Mark breakpoint locations modified. */
14823 mark_breakpoint_modified (bpt);
14824
d248b706
KY
14825 if (target_supports_enable_disable_tracepoint ()
14826 && current_trace_status ()->running && is_tracepoint (bpt))
14827 {
14828 struct bp_location *location;
14829
14830 for (location = bpt->loc; location; location = location->next)
14831 target_enable_tracepoint (location);
14832 }
14833
b4c291bb 14834 bpt->disposition = disposition;
816338b5 14835 bpt->enable_count = count;
44702360 14836 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14837
8d3788bd 14838 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14839}
14840
fe3f5fa8 14841
c906108c 14842void
fba45db2 14843enable_breakpoint (struct breakpoint *bpt)
c906108c 14844{
816338b5 14845 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14846}
14847
14848static void
14849do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14850{
14851 enable_breakpoint (bpt);
c906108c
SS
14852}
14853
95a42b64
TT
14854/* A callback for map_breakpoint_numbers that calls
14855 enable_breakpoint. */
14856
14857static void
14858do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14859{
51be5b68 14860 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14861}
14862
c906108c
SS
14863/* The enable command enables the specified breakpoints (or all defined
14864 breakpoints) so they once again become (or continue to be) effective
1272ad14 14865 in stopping the inferior. */
c906108c 14866
c906108c 14867static void
fba45db2 14868enable_command (char *args, int from_tty)
c906108c 14869{
c906108c 14870 if (args == 0)
46c6471b
PA
14871 {
14872 struct breakpoint *bpt;
14873
14874 ALL_BREAKPOINTS (bpt)
14875 if (user_breakpoint_p (bpt))
14876 enable_breakpoint (bpt);
14877 }
9eaabc75 14878 else
0d381245 14879 {
9eaabc75
MW
14880 char *num = extract_arg (&args);
14881
14882 while (num)
d248b706 14883 {
9eaabc75 14884 if (strchr (num, '.'))
b775012e 14885 {
9eaabc75
MW
14886 struct bp_location *loc = find_location_by_number (num);
14887
14888 if (loc)
14889 {
14890 if (!loc->enabled)
14891 {
14892 loc->enabled = 1;
14893 mark_breakpoint_location_modified (loc);
14894 }
14895 if (target_supports_enable_disable_tracepoint ()
14896 && current_trace_status ()->running && loc->owner
14897 && is_tracepoint (loc->owner))
14898 target_enable_tracepoint (loc);
14899 }
44702360 14900 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14901 }
9eaabc75
MW
14902 else
14903 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14904 num = extract_arg (&args);
d248b706 14905 }
0d381245 14906 }
c906108c
SS
14907}
14908
816338b5
SS
14909/* This struct packages up disposition data for application to multiple
14910 breakpoints. */
14911
14912struct disp_data
14913{
14914 enum bpdisp disp;
14915 int count;
14916};
14917
c906108c 14918static void
51be5b68
PA
14919do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14920{
816338b5 14921 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14922
816338b5 14923 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14924}
14925
14926static void
14927do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14928{
816338b5 14929 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14930
14931 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14932}
14933
c906108c 14934static void
fba45db2 14935enable_once_command (char *args, int from_tty)
c906108c 14936{
51be5b68 14937 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14938}
14939
816338b5
SS
14940static void
14941do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14942{
14943 struct disp_data disp = { disp_disable, *(int *) countptr };
14944
14945 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14946}
14947
14948static void
14949enable_count_command (char *args, int from_tty)
14950{
b9d61307
SM
14951 int count;
14952
14953 if (args == NULL)
14954 error_no_arg (_("hit count"));
14955
14956 count = get_number (&args);
816338b5
SS
14957
14958 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14959}
14960
c906108c 14961static void
51be5b68 14962do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14963{
816338b5 14964 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14965
14966 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14967}
14968
c906108c 14969static void
fba45db2 14970enable_delete_command (char *args, int from_tty)
c906108c 14971{
51be5b68 14972 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14973}
14974\f
fa8d40ab
JJ
14975static void
14976set_breakpoint_cmd (char *args, int from_tty)
14977{
14978}
14979
14980static void
14981show_breakpoint_cmd (char *args, int from_tty)
14982{
14983}
14984
1f3b5d1b
PP
14985/* Invalidate last known value of any hardware watchpoint if
14986 the memory which that value represents has been written to by
14987 GDB itself. */
14988
14989static void
8de0566d
YQ
14990invalidate_bp_value_on_memory_change (struct inferior *inferior,
14991 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14992 const bfd_byte *data)
14993{
14994 struct breakpoint *bp;
14995
14996 ALL_BREAKPOINTS (bp)
14997 if (bp->enable_state == bp_enabled
3a5c3e22 14998 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14999 {
3a5c3e22 15000 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15001
3a5c3e22
PA
15002 if (wp->val_valid && wp->val)
15003 {
15004 struct bp_location *loc;
15005
15006 for (loc = bp->loc; loc != NULL; loc = loc->next)
15007 if (loc->loc_type == bp_loc_hardware_watchpoint
15008 && loc->address + loc->length > addr
15009 && addr + len > loc->address)
15010 {
15011 value_free (wp->val);
15012 wp->val = NULL;
15013 wp->val_valid = 0;
15014 }
15015 }
1f3b5d1b
PP
15016 }
15017}
15018
8181d85f
DJ
15019/* Create and insert a breakpoint for software single step. */
15020
15021void
6c95b8df 15022insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15023 struct address_space *aspace,
15024 CORE_ADDR next_pc)
8181d85f 15025{
7c16b83e
PA
15026 struct thread_info *tp = inferior_thread ();
15027 struct symtab_and_line sal;
15028 CORE_ADDR pc = next_pc;
8181d85f 15029
34b7e8a6
PA
15030 if (tp->control.single_step_breakpoints == NULL)
15031 {
15032 tp->control.single_step_breakpoints
5d5658a1 15033 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15034 }
8181d85f 15035
7c16b83e
PA
15036 sal = find_pc_line (pc, 0);
15037 sal.pc = pc;
15038 sal.section = find_pc_overlay (pc);
15039 sal.explicit_pc = 1;
34b7e8a6 15040 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15041
7c16b83e 15042 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15043}
15044
93f9a11f
YQ
15045/* Insert single step breakpoints according to the current state. */
15046
15047int
15048insert_single_step_breakpoints (struct gdbarch *gdbarch)
15049{
f5ea389a 15050 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 15051 std::vector<CORE_ADDR> next_pcs;
93f9a11f 15052
f5ea389a 15053 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 15054
a0ff9e1a 15055 if (!next_pcs.empty ())
93f9a11f 15056 {
f5ea389a 15057 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
15058 struct address_space *aspace = get_frame_address_space (frame);
15059
a0ff9e1a 15060 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
15061 insert_single_step_breakpoint (gdbarch, aspace, pc);
15062
93f9a11f
YQ
15063 return 1;
15064 }
15065 else
15066 return 0;
15067}
15068
34b7e8a6 15069/* See breakpoint.h. */
f02253f1
HZ
15070
15071int
7c16b83e
PA
15072breakpoint_has_location_inserted_here (struct breakpoint *bp,
15073 struct address_space *aspace,
15074 CORE_ADDR pc)
1aafd4da 15075{
7c16b83e 15076 struct bp_location *loc;
1aafd4da 15077
7c16b83e
PA
15078 for (loc = bp->loc; loc != NULL; loc = loc->next)
15079 if (loc->inserted
15080 && breakpoint_location_address_match (loc, aspace, pc))
15081 return 1;
1aafd4da 15082
7c16b83e 15083 return 0;
ef370185
JB
15084}
15085
15086/* Check whether a software single-step breakpoint is inserted at
15087 PC. */
15088
15089int
15090single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15091 CORE_ADDR pc)
15092{
34b7e8a6
PA
15093 struct breakpoint *bpt;
15094
15095 ALL_BREAKPOINTS (bpt)
15096 {
15097 if (bpt->type == bp_single_step
15098 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15099 return 1;
15100 }
15101 return 0;
1aafd4da
UW
15102}
15103
1042e4c0
SS
15104/* Tracepoint-specific operations. */
15105
15106/* Set tracepoint count to NUM. */
15107static void
15108set_tracepoint_count (int num)
15109{
15110 tracepoint_count = num;
4fa62494 15111 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15112}
15113
70221824 15114static void
1042e4c0
SS
15115trace_command (char *arg, int from_tty)
15116{
55aa24fb 15117 struct breakpoint_ops *ops;
55aa24fb 15118
ffc2605c
TT
15119 event_location_up location = string_to_event_location (&arg,
15120 current_language);
5b56227b 15121 if (location != NULL
ffc2605c 15122 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15123 ops = &tracepoint_probe_breakpoint_ops;
15124 else
15125 ops = &tracepoint_breakpoint_ops;
15126
558a9d82 15127 create_breakpoint (get_current_arch (),
ffc2605c 15128 location.get (),
f00aae0f 15129 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15130 0 /* tempflag */,
15131 bp_tracepoint /* type_wanted */,
15132 0 /* Ignore count */,
15133 pending_break_support,
15134 ops,
15135 from_tty,
15136 1 /* enabled */,
15137 0 /* internal */, 0);
1042e4c0
SS
15138}
15139
70221824 15140static void
7a697b8d
SS
15141ftrace_command (char *arg, int from_tty)
15142{
ffc2605c
TT
15143 event_location_up location = string_to_event_location (&arg,
15144 current_language);
558a9d82 15145 create_breakpoint (get_current_arch (),
ffc2605c 15146 location.get (),
f00aae0f 15147 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15148 0 /* tempflag */,
15149 bp_fast_tracepoint /* type_wanted */,
15150 0 /* Ignore count */,
15151 pending_break_support,
15152 &tracepoint_breakpoint_ops,
15153 from_tty,
15154 1 /* enabled */,
15155 0 /* internal */, 0);
0fb4aa4b
PA
15156}
15157
15158/* strace command implementation. Creates a static tracepoint. */
15159
70221824 15160static void
0fb4aa4b
PA
15161strace_command (char *arg, int from_tty)
15162{
983af33b 15163 struct breakpoint_ops *ops;
ffc2605c 15164 event_location_up location;
f00aae0f 15165 struct cleanup *back_to;
983af33b
SDJ
15166
15167 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15168 or with a normal static tracepoint. */
61012eef 15169 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15170 {
15171 ops = &strace_marker_breakpoint_ops;
15172 location = new_linespec_location (&arg);
15173 }
983af33b 15174 else
f00aae0f
KS
15175 {
15176 ops = &tracepoint_breakpoint_ops;
15177 location = string_to_event_location (&arg, current_language);
15178 }
983af33b 15179
558a9d82 15180 create_breakpoint (get_current_arch (),
ffc2605c 15181 location.get (),
f00aae0f 15182 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15183 0 /* tempflag */,
15184 bp_static_tracepoint /* type_wanted */,
15185 0 /* Ignore count */,
15186 pending_break_support,
15187 ops,
15188 from_tty,
15189 1 /* enabled */,
15190 0 /* internal */, 0);
7a697b8d
SS
15191}
15192
409873ef
SS
15193/* Set up a fake reader function that gets command lines from a linked
15194 list that was acquired during tracepoint uploading. */
15195
15196static struct uploaded_tp *this_utp;
3149d8c1 15197static int next_cmd;
409873ef
SS
15198
15199static char *
15200read_uploaded_action (void)
15201{
15202 char *rslt;
15203
3149d8c1 15204 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15205
3149d8c1 15206 next_cmd++;
409873ef
SS
15207
15208 return rslt;
15209}
15210
00bf0b85
SS
15211/* Given information about a tracepoint as recorded on a target (which
15212 can be either a live system or a trace file), attempt to create an
15213 equivalent GDB tracepoint. This is not a reliable process, since
15214 the target does not necessarily have all the information used when
15215 the tracepoint was originally defined. */
15216
d9b3f62e 15217struct tracepoint *
00bf0b85 15218create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15219{
409873ef 15220 char *addr_str, small_buf[100];
d9b3f62e 15221 struct tracepoint *tp;
fd9b8c24 15222
409873ef
SS
15223 if (utp->at_string)
15224 addr_str = utp->at_string;
15225 else
15226 {
15227 /* In the absence of a source location, fall back to raw
15228 address. Since there is no way to confirm that the address
15229 means the same thing as when the trace was started, warn the
15230 user. */
3e43a32a
MS
15231 warning (_("Uploaded tracepoint %d has no "
15232 "source location, using raw address"),
409873ef 15233 utp->number);
8c042590 15234 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15235 addr_str = small_buf;
15236 }
15237
15238 /* There's not much we can do with a sequence of bytecodes. */
15239 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15240 warning (_("Uploaded tracepoint %d condition "
15241 "has no source form, ignoring it"),
409873ef 15242 utp->number);
d5551862 15243
ffc2605c
TT
15244 event_location_up location = string_to_event_location (&addr_str,
15245 current_language);
8cdf0e15 15246 if (!create_breakpoint (get_current_arch (),
ffc2605c 15247 location.get (),
f00aae0f 15248 utp->cond_string, -1, addr_str,
e7e0cddf 15249 0 /* parse cond/thread */,
8cdf0e15 15250 0 /* tempflag */,
0fb4aa4b 15251 utp->type /* type_wanted */,
8cdf0e15
VP
15252 0 /* Ignore count */,
15253 pending_break_support,
348d480f 15254 &tracepoint_breakpoint_ops,
8cdf0e15 15255 0 /* from_tty */,
84f4c1fe 15256 utp->enabled /* enabled */,
44f238bb
PA
15257 0 /* internal */,
15258 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15259 return NULL;
fd9b8c24 15260
409873ef 15261 /* Get the tracepoint we just created. */
fd9b8c24
PA
15262 tp = get_tracepoint (tracepoint_count);
15263 gdb_assert (tp != NULL);
d5551862 15264
00bf0b85
SS
15265 if (utp->pass > 0)
15266 {
8c042590
PM
15267 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15268 tp->base.number);
00bf0b85 15269
409873ef 15270 trace_pass_command (small_buf, 0);
00bf0b85
SS
15271 }
15272
409873ef
SS
15273 /* If we have uploaded versions of the original commands, set up a
15274 special-purpose "reader" function and call the usual command line
15275 reader, then pass the result to the breakpoint command-setting
15276 function. */
3149d8c1 15277 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15278 {
93921405 15279 command_line_up cmd_list;
00bf0b85 15280
409873ef 15281 this_utp = utp;
3149d8c1 15282 next_cmd = 0;
d5551862 15283
409873ef
SS
15284 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15285
93921405 15286 breakpoint_set_commands (&tp->base, std::move (cmd_list));
00bf0b85 15287 }
3149d8c1
SS
15288 else if (!VEC_empty (char_ptr, utp->actions)
15289 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15290 warning (_("Uploaded tracepoint %d actions "
15291 "have no source form, ignoring them"),
409873ef 15292 utp->number);
00bf0b85 15293
f196051f
SS
15294 /* Copy any status information that might be available. */
15295 tp->base.hit_count = utp->hit_count;
15296 tp->traceframe_usage = utp->traceframe_usage;
15297
00bf0b85 15298 return tp;
d9b3f62e 15299}
00bf0b85 15300
1042e4c0
SS
15301/* Print information on tracepoint number TPNUM_EXP, or all if
15302 omitted. */
15303
15304static void
e5a67952 15305tracepoints_info (char *args, int from_tty)
1042e4c0 15306{
79a45e25 15307 struct ui_out *uiout = current_uiout;
e5a67952 15308 int num_printed;
1042e4c0 15309
e5a67952 15310 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15311
15312 if (num_printed == 0)
1042e4c0 15313 {
e5a67952 15314 if (args == NULL || *args == '\0')
112e8700 15315 uiout->message ("No tracepoints.\n");
d77f58be 15316 else
112e8700 15317 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15318 }
ad443146
SS
15319
15320 default_collect_info ();
1042e4c0
SS
15321}
15322
4a64f543 15323/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15324 Not supported by all targets. */
15325static void
15326enable_trace_command (char *args, int from_tty)
15327{
15328 enable_command (args, from_tty);
15329}
15330
4a64f543 15331/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15332 Not supported by all targets. */
15333static void
15334disable_trace_command (char *args, int from_tty)
15335{
15336 disable_command (args, from_tty);
15337}
15338
4a64f543 15339/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15340static void
15341delete_trace_command (char *arg, int from_tty)
15342{
35df4500 15343 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15344
15345 dont_repeat ();
15346
15347 if (arg == 0)
15348 {
15349 int breaks_to_delete = 0;
15350
15351 /* Delete all breakpoints if no argument.
15352 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15353 have to be deleted with an explicit breakpoint number
15354 argument. */
1042e4c0 15355 ALL_TRACEPOINTS (b)
46c6471b 15356 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15357 {
15358 breaks_to_delete = 1;
15359 break;
15360 }
1042e4c0
SS
15361
15362 /* Ask user only if there are some breakpoints to delete. */
15363 if (!from_tty
15364 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15365 {
35df4500 15366 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15367 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15368 delete_breakpoint (b);
1042e4c0
SS
15369 }
15370 }
15371 else
51be5b68 15372 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15373}
15374
197f0a60
TT
15375/* Helper function for trace_pass_command. */
15376
15377static void
d9b3f62e 15378trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15379{
d9b3f62e 15380 tp->pass_count = count;
6f6484cd 15381 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15382 if (from_tty)
15383 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15384 tp->base.number, count);
197f0a60
TT
15385}
15386
1042e4c0
SS
15387/* Set passcount for tracepoint.
15388
15389 First command argument is passcount, second is tracepoint number.
15390 If tracepoint number omitted, apply to most recently defined.
15391 Also accepts special argument "all". */
15392
15393static void
15394trace_pass_command (char *args, int from_tty)
15395{
d9b3f62e 15396 struct tracepoint *t1;
1042e4c0 15397 unsigned int count;
1042e4c0
SS
15398
15399 if (args == 0 || *args == 0)
3e43a32a
MS
15400 error (_("passcount command requires an "
15401 "argument (count + optional TP num)"));
1042e4c0 15402
4a64f543 15403 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15404
529480d0 15405 args = skip_spaces (args);
1042e4c0
SS
15406 if (*args && strncasecmp (args, "all", 3) == 0)
15407 {
d9b3f62e
PA
15408 struct breakpoint *b;
15409
1042e4c0 15410 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15411 if (*args)
15412 error (_("Junk at end of arguments."));
1042e4c0 15413
d9b3f62e 15414 ALL_TRACEPOINTS (b)
197f0a60 15415 {
d9b3f62e 15416 t1 = (struct tracepoint *) b;
197f0a60
TT
15417 trace_pass_set_count (t1, count, from_tty);
15418 }
15419 }
15420 else if (*args == '\0')
1042e4c0 15421 {
5fa1d40e 15422 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15423 if (t1)
197f0a60
TT
15424 trace_pass_set_count (t1, count, from_tty);
15425 }
15426 else
15427 {
bfd28288
PA
15428 number_or_range_parser parser (args);
15429 while (!parser.finished ())
1042e4c0 15430 {
bfd28288 15431 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15432 if (t1)
15433 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15434 }
15435 }
1042e4c0
SS
15436}
15437
d9b3f62e 15438struct tracepoint *
1042e4c0
SS
15439get_tracepoint (int num)
15440{
15441 struct breakpoint *t;
15442
15443 ALL_TRACEPOINTS (t)
15444 if (t->number == num)
d9b3f62e 15445 return (struct tracepoint *) t;
1042e4c0
SS
15446
15447 return NULL;
15448}
15449
d5551862
SS
15450/* Find the tracepoint with the given target-side number (which may be
15451 different from the tracepoint number after disconnecting and
15452 reconnecting). */
15453
d9b3f62e 15454struct tracepoint *
d5551862
SS
15455get_tracepoint_by_number_on_target (int num)
15456{
d9b3f62e 15457 struct breakpoint *b;
d5551862 15458
d9b3f62e
PA
15459 ALL_TRACEPOINTS (b)
15460 {
15461 struct tracepoint *t = (struct tracepoint *) b;
15462
15463 if (t->number_on_target == num)
15464 return t;
15465 }
d5551862
SS
15466
15467 return NULL;
15468}
15469
1042e4c0 15470/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15471 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15472 If the argument is missing, the most recent tracepoint
15473 (tracepoint_count) is returned. */
15474
d9b3f62e 15475struct tracepoint *
197f0a60 15476get_tracepoint_by_number (char **arg,
bfd28288 15477 number_or_range_parser *parser)
1042e4c0 15478{
1042e4c0
SS
15479 struct breakpoint *t;
15480 int tpnum;
15481 char *instring = arg == NULL ? NULL : *arg;
15482
bfd28288 15483 if (parser != NULL)
197f0a60 15484 {
bfd28288
PA
15485 gdb_assert (!parser->finished ());
15486 tpnum = parser->get_number ();
197f0a60
TT
15487 }
15488 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15489 tpnum = tracepoint_count;
1042e4c0 15490 else
197f0a60 15491 tpnum = get_number (arg);
1042e4c0
SS
15492
15493 if (tpnum <= 0)
15494 {
15495 if (instring && *instring)
15496 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15497 instring);
15498 else
5fa1d40e 15499 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15500 return NULL;
15501 }
15502
15503 ALL_TRACEPOINTS (t)
15504 if (t->number == tpnum)
15505 {
d9b3f62e 15506 return (struct tracepoint *) t;
1042e4c0
SS
15507 }
15508
1042e4c0
SS
15509 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15510 return NULL;
15511}
15512
d9b3f62e
PA
15513void
15514print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15515{
15516 if (b->thread != -1)
15517 fprintf_unfiltered (fp, " thread %d", b->thread);
15518
15519 if (b->task != 0)
15520 fprintf_unfiltered (fp, " task %d", b->task);
15521
15522 fprintf_unfiltered (fp, "\n");
15523}
15524
6149aea9
PA
15525/* Save information on user settable breakpoints (watchpoints, etc) to
15526 a new script file named FILENAME. If FILTER is non-NULL, call it
15527 on each breakpoint and only include the ones for which it returns
15528 non-zero. */
15529
1042e4c0 15530static void
6149aea9
PA
15531save_breakpoints (char *filename, int from_tty,
15532 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15533{
15534 struct breakpoint *tp;
6149aea9 15535 int any = 0;
1042e4c0 15536 struct cleanup *cleanup;
6149aea9 15537 int extra_trace_bits = 0;
1042e4c0 15538
6149aea9
PA
15539 if (filename == 0 || *filename == 0)
15540 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15541
15542 /* See if we have anything to save. */
6149aea9 15543 ALL_BREAKPOINTS (tp)
1042e4c0 15544 {
6149aea9 15545 /* Skip internal and momentary breakpoints. */
09d682a4 15546 if (!user_breakpoint_p (tp))
6149aea9
PA
15547 continue;
15548
15549 /* If we have a filter, only save the breakpoints it accepts. */
15550 if (filter && !filter (tp))
15551 continue;
15552
15553 any = 1;
15554
15555 if (is_tracepoint (tp))
15556 {
15557 extra_trace_bits = 1;
15558
15559 /* We can stop searching. */
15560 break;
15561 }
1042e4c0 15562 }
6149aea9
PA
15563
15564 if (!any)
1042e4c0 15565 {
6149aea9 15566 warning (_("Nothing to save."));
1042e4c0
SS
15567 return;
15568 }
15569
c718be47
PA
15570 filename = tilde_expand (filename);
15571 cleanup = make_cleanup (xfree, filename);
d7e74731
PA
15572
15573 stdio_file fp;
15574
15575 if (!fp.open (filename, "w"))
6149aea9
PA
15576 error (_("Unable to open file '%s' for saving (%s)"),
15577 filename, safe_strerror (errno));
8bf6485c 15578
6149aea9 15579 if (extra_trace_bits)
d7e74731 15580 save_trace_state_variables (&fp);
8bf6485c 15581
6149aea9 15582 ALL_BREAKPOINTS (tp)
1042e4c0 15583 {
6149aea9 15584 /* Skip internal and momentary breakpoints. */
09d682a4 15585 if (!user_breakpoint_p (tp))
6149aea9 15586 continue;
8bf6485c 15587
6149aea9
PA
15588 /* If we have a filter, only save the breakpoints it accepts. */
15589 if (filter && !filter (tp))
15590 continue;
15591
d7e74731 15592 tp->ops->print_recreate (tp, &fp);
1042e4c0 15593
6149aea9
PA
15594 /* Note, we can't rely on tp->number for anything, as we can't
15595 assume the recreated breakpoint numbers will match. Use $bpnum
15596 instead. */
15597
15598 if (tp->cond_string)
d7e74731 15599 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15600
15601 if (tp->ignore_count)
d7e74731 15602 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15603
2d9442cc 15604 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15605 {
d7e74731 15606 fp.puts (" commands\n");
a7bdde9e 15607
d7e74731 15608 current_uiout->redirect (&fp);
492d29ea 15609 TRY
1042e4c0 15610 {
79a45e25 15611 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15612 }
492d29ea
PA
15613 CATCH (ex, RETURN_MASK_ALL)
15614 {
112e8700 15615 current_uiout->redirect (NULL);
492d29ea
PA
15616 throw_exception (ex);
15617 }
15618 END_CATCH
1042e4c0 15619
112e8700 15620 current_uiout->redirect (NULL);
d7e74731 15621 fp.puts (" end\n");
1042e4c0 15622 }
6149aea9
PA
15623
15624 if (tp->enable_state == bp_disabled)
d7e74731 15625 fp.puts ("disable $bpnum\n");
6149aea9
PA
15626
15627 /* If this is a multi-location breakpoint, check if the locations
15628 should be individually disabled. Watchpoint locations are
15629 special, and not user visible. */
15630 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15631 {
15632 struct bp_location *loc;
15633 int n = 1;
15634
15635 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15636 if (!loc->enabled)
d7e74731 15637 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15638 }
1042e4c0 15639 }
8bf6485c 15640
6149aea9 15641 if (extra_trace_bits && *default_collect)
d7e74731 15642 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15643
1042e4c0 15644 if (from_tty)
6149aea9 15645 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15646 do_cleanups (cleanup);
6149aea9
PA
15647}
15648
15649/* The `save breakpoints' command. */
15650
15651static void
15652save_breakpoints_command (char *args, int from_tty)
15653{
15654 save_breakpoints (args, from_tty, NULL);
15655}
15656
15657/* The `save tracepoints' command. */
15658
15659static void
15660save_tracepoints_command (char *args, int from_tty)
15661{
15662 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15663}
15664
15665/* Create a vector of all tracepoints. */
15666
15667VEC(breakpoint_p) *
eeae04df 15668all_tracepoints (void)
1042e4c0
SS
15669{
15670 VEC(breakpoint_p) *tp_vec = 0;
15671 struct breakpoint *tp;
15672
15673 ALL_TRACEPOINTS (tp)
15674 {
15675 VEC_safe_push (breakpoint_p, tp_vec, tp);
15676 }
15677
15678 return tp_vec;
15679}
15680
c906108c 15681\f
629500fa
KS
15682/* This help string is used to consolidate all the help string for specifying
15683 locations used by several commands. */
15684
15685#define LOCATION_HELP_STRING \
15686"Linespecs are colon-separated lists of location parameters, such as\n\
15687source filename, function name, label name, and line number.\n\
15688Example: To specify the start of a label named \"the_top\" in the\n\
15689function \"fact\" in the file \"factorial.c\", use\n\
15690\"factorial.c:fact:the_top\".\n\
15691\n\
15692Address locations begin with \"*\" and specify an exact address in the\n\
15693program. Example: To specify the fourth byte past the start function\n\
15694\"main\", use \"*main + 4\".\n\
15695\n\
15696Explicit locations are similar to linespecs but use an option/argument\n\
15697syntax to specify location parameters.\n\
15698Example: To specify the start of the label named \"the_top\" in the\n\
15699function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15700-function fact -label the_top\".\n"
15701
4a64f543
MS
15702/* This help string is used for the break, hbreak, tbreak and thbreak
15703 commands. It is defined as a macro to prevent duplication.
15704 COMMAND should be a string constant containing the name of the
15705 command. */
629500fa 15706
31e2b00f 15707#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15708command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15709PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15710probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15711guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15712`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15713LOCATION may be a linespec, address, or explicit location as described\n\
15714below.\n\
15715\n\
dc10affe
PA
15716With no LOCATION, uses current execution address of the selected\n\
15717stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15718\n\
15719THREADNUM is the number from \"info threads\".\n\
15720CONDITION is a boolean expression.\n\
629500fa 15721\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15722Multiple breakpoints at one place are permitted, and useful if their\n\
15723conditions are different.\n\
31e2b00f
AS
15724\n\
15725Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15726
44feb3ce
TT
15727/* List of subcommands for "catch". */
15728static struct cmd_list_element *catch_cmdlist;
15729
15730/* List of subcommands for "tcatch". */
15731static struct cmd_list_element *tcatch_cmdlist;
15732
9ac4176b 15733void
a121b7c1 15734add_catch_command (const char *name, const char *docstring,
82ae6c8d 15735 cmd_sfunc_ftype *sfunc,
625e8578 15736 completer_ftype *completer,
44feb3ce
TT
15737 void *user_data_catch,
15738 void *user_data_tcatch)
15739{
15740 struct cmd_list_element *command;
15741
15742 command = add_cmd (name, class_breakpoint, NULL, docstring,
15743 &catch_cmdlist);
15744 set_cmd_sfunc (command, sfunc);
15745 set_cmd_context (command, user_data_catch);
a96d9b2e 15746 set_cmd_completer (command, completer);
44feb3ce
TT
15747
15748 command = add_cmd (name, class_breakpoint, NULL, docstring,
15749 &tcatch_cmdlist);
15750 set_cmd_sfunc (command, sfunc);
15751 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15752 set_cmd_completer (command, completer);
44feb3ce
TT
15753}
15754
6149aea9
PA
15755static void
15756save_command (char *arg, int from_tty)
15757{
3e43a32a
MS
15758 printf_unfiltered (_("\"save\" must be followed by "
15759 "the name of a save subcommand.\n"));
635c7e8a 15760 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15761}
15762
84f4c1fe
PM
15763struct breakpoint *
15764iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15765 void *data)
15766{
35df4500 15767 struct breakpoint *b, *b_tmp;
84f4c1fe 15768
35df4500 15769 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15770 {
15771 if ((*callback) (b, data))
15772 return b;
15773 }
15774
15775 return NULL;
15776}
15777
0574c78f
GB
15778/* Zero if any of the breakpoint's locations could be a location where
15779 functions have been inlined, nonzero otherwise. */
15780
15781static int
15782is_non_inline_function (struct breakpoint *b)
15783{
15784 /* The shared library event breakpoint is set on the address of a
15785 non-inline function. */
15786 if (b->type == bp_shlib_event)
15787 return 1;
15788
15789 return 0;
15790}
15791
15792/* Nonzero if the specified PC cannot be a location where functions
15793 have been inlined. */
15794
15795int
09ac7c10
TT
15796pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15797 const struct target_waitstatus *ws)
0574c78f
GB
15798{
15799 struct breakpoint *b;
15800 struct bp_location *bl;
15801
15802 ALL_BREAKPOINTS (b)
15803 {
15804 if (!is_non_inline_function (b))
15805 continue;
15806
15807 for (bl = b->loc; bl != NULL; bl = bl->next)
15808 {
15809 if (!bl->shlib_disabled
09ac7c10 15810 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15811 return 1;
15812 }
15813 }
15814
15815 return 0;
15816}
15817
2f202fde
JK
15818/* Remove any references to OBJFILE which is going to be freed. */
15819
15820void
15821breakpoint_free_objfile (struct objfile *objfile)
15822{
15823 struct bp_location **locp, *loc;
15824
15825 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15826 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15827 loc->symtab = NULL;
15828}
15829
2060206e
PA
15830void
15831initialize_breakpoint_ops (void)
15832{
15833 static int initialized = 0;
15834
15835 struct breakpoint_ops *ops;
15836
15837 if (initialized)
15838 return;
15839 initialized = 1;
15840
15841 /* The breakpoint_ops structure to be inherit by all kinds of
15842 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15843 internal and momentary breakpoints, etc.). */
15844 ops = &bkpt_base_breakpoint_ops;
15845 *ops = base_breakpoint_ops;
15846 ops->re_set = bkpt_re_set;
15847 ops->insert_location = bkpt_insert_location;
15848 ops->remove_location = bkpt_remove_location;
15849 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15850 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15851 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15852 ops->decode_location = bkpt_decode_location;
2060206e
PA
15853
15854 /* The breakpoint_ops structure to be used in regular breakpoints. */
15855 ops = &bkpt_breakpoint_ops;
15856 *ops = bkpt_base_breakpoint_ops;
15857 ops->re_set = bkpt_re_set;
15858 ops->resources_needed = bkpt_resources_needed;
15859 ops->print_it = bkpt_print_it;
15860 ops->print_mention = bkpt_print_mention;
15861 ops->print_recreate = bkpt_print_recreate;
15862
15863 /* Ranged breakpoints. */
15864 ops = &ranged_breakpoint_ops;
15865 *ops = bkpt_breakpoint_ops;
15866 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15867 ops->resources_needed = resources_needed_ranged_breakpoint;
15868 ops->print_it = print_it_ranged_breakpoint;
15869 ops->print_one = print_one_ranged_breakpoint;
15870 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15871 ops->print_mention = print_mention_ranged_breakpoint;
15872 ops->print_recreate = print_recreate_ranged_breakpoint;
15873
15874 /* Internal breakpoints. */
15875 ops = &internal_breakpoint_ops;
15876 *ops = bkpt_base_breakpoint_ops;
15877 ops->re_set = internal_bkpt_re_set;
15878 ops->check_status = internal_bkpt_check_status;
15879 ops->print_it = internal_bkpt_print_it;
15880 ops->print_mention = internal_bkpt_print_mention;
15881
15882 /* Momentary breakpoints. */
15883 ops = &momentary_breakpoint_ops;
15884 *ops = bkpt_base_breakpoint_ops;
15885 ops->re_set = momentary_bkpt_re_set;
15886 ops->check_status = momentary_bkpt_check_status;
15887 ops->print_it = momentary_bkpt_print_it;
15888 ops->print_mention = momentary_bkpt_print_mention;
15889
e2e4d78b
JK
15890 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15891 ops = &longjmp_breakpoint_ops;
15892 *ops = momentary_breakpoint_ops;
15893 ops->dtor = longjmp_bkpt_dtor;
15894
55aa24fb
SDJ
15895 /* Probe breakpoints. */
15896 ops = &bkpt_probe_breakpoint_ops;
15897 *ops = bkpt_breakpoint_ops;
15898 ops->insert_location = bkpt_probe_insert_location;
15899 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15900 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15901 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15902
2060206e
PA
15903 /* Watchpoints. */
15904 ops = &watchpoint_breakpoint_ops;
15905 *ops = base_breakpoint_ops;
3a5c3e22 15906 ops->dtor = dtor_watchpoint;
2060206e
PA
15907 ops->re_set = re_set_watchpoint;
15908 ops->insert_location = insert_watchpoint;
15909 ops->remove_location = remove_watchpoint;
15910 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15911 ops->check_status = check_status_watchpoint;
15912 ops->resources_needed = resources_needed_watchpoint;
15913 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15914 ops->print_it = print_it_watchpoint;
15915 ops->print_mention = print_mention_watchpoint;
15916 ops->print_recreate = print_recreate_watchpoint;
427cd150 15917 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15918
15919 /* Masked watchpoints. */
15920 ops = &masked_watchpoint_breakpoint_ops;
15921 *ops = watchpoint_breakpoint_ops;
15922 ops->insert_location = insert_masked_watchpoint;
15923 ops->remove_location = remove_masked_watchpoint;
15924 ops->resources_needed = resources_needed_masked_watchpoint;
15925 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15926 ops->print_it = print_it_masked_watchpoint;
15927 ops->print_one_detail = print_one_detail_masked_watchpoint;
15928 ops->print_mention = print_mention_masked_watchpoint;
15929 ops->print_recreate = print_recreate_masked_watchpoint;
15930
15931 /* Tracepoints. */
15932 ops = &tracepoint_breakpoint_ops;
15933 *ops = base_breakpoint_ops;
15934 ops->re_set = tracepoint_re_set;
15935 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15936 ops->print_one_detail = tracepoint_print_one_detail;
15937 ops->print_mention = tracepoint_print_mention;
15938 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15939 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15940 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15941 ops->decode_location = tracepoint_decode_location;
983af33b 15942
55aa24fb
SDJ
15943 /* Probe tracepoints. */
15944 ops = &tracepoint_probe_breakpoint_ops;
15945 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15946 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15947 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15948
983af33b
SDJ
15949 /* Static tracepoints with marker (`-m'). */
15950 ops = &strace_marker_breakpoint_ops;
15951 *ops = tracepoint_breakpoint_ops;
5f700d83 15952 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15953 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15954 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15955
15956 /* Fork catchpoints. */
15957 ops = &catch_fork_breakpoint_ops;
15958 *ops = base_breakpoint_ops;
15959 ops->insert_location = insert_catch_fork;
15960 ops->remove_location = remove_catch_fork;
15961 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15962 ops->print_it = print_it_catch_fork;
15963 ops->print_one = print_one_catch_fork;
15964 ops->print_mention = print_mention_catch_fork;
15965 ops->print_recreate = print_recreate_catch_fork;
15966
15967 /* Vfork catchpoints. */
15968 ops = &catch_vfork_breakpoint_ops;
15969 *ops = base_breakpoint_ops;
15970 ops->insert_location = insert_catch_vfork;
15971 ops->remove_location = remove_catch_vfork;
15972 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15973 ops->print_it = print_it_catch_vfork;
15974 ops->print_one = print_one_catch_vfork;
15975 ops->print_mention = print_mention_catch_vfork;
15976 ops->print_recreate = print_recreate_catch_vfork;
15977
15978 /* Exec catchpoints. */
15979 ops = &catch_exec_breakpoint_ops;
15980 *ops = base_breakpoint_ops;
15981 ops->dtor = dtor_catch_exec;
15982 ops->insert_location = insert_catch_exec;
15983 ops->remove_location = remove_catch_exec;
15984 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15985 ops->print_it = print_it_catch_exec;
15986 ops->print_one = print_one_catch_exec;
15987 ops->print_mention = print_mention_catch_exec;
15988 ops->print_recreate = print_recreate_catch_exec;
15989
edcc5120
TT
15990 /* Solib-related catchpoints. */
15991 ops = &catch_solib_breakpoint_ops;
15992 *ops = base_breakpoint_ops;
15993 ops->dtor = dtor_catch_solib;
15994 ops->insert_location = insert_catch_solib;
15995 ops->remove_location = remove_catch_solib;
15996 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15997 ops->check_status = check_status_catch_solib;
15998 ops->print_it = print_it_catch_solib;
15999 ops->print_one = print_one_catch_solib;
16000 ops->print_mention = print_mention_catch_solib;
16001 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16002
16003 ops = &dprintf_breakpoint_ops;
16004 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16005 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16006 ops->resources_needed = bkpt_resources_needed;
16007 ops->print_it = bkpt_print_it;
16008 ops->print_mention = bkpt_print_mention;
2d9442cc 16009 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16010 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16011 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16012}
16013
8bfd80db
YQ
16014/* Chain containing all defined "enable breakpoint" subcommands. */
16015
16016static struct cmd_list_element *enablebreaklist = NULL;
16017
c906108c 16018void
fba45db2 16019_initialize_breakpoint (void)
c906108c
SS
16020{
16021 struct cmd_list_element *c;
16022
2060206e
PA
16023 initialize_breakpoint_ops ();
16024
84acb35a 16025 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16026 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16027 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16028
55aa24fb
SDJ
16029 breakpoint_objfile_key
16030 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16031
c906108c
SS
16032 breakpoint_chain = 0;
16033 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16034 before a breakpoint is set. */
16035 breakpoint_count = 0;
16036
1042e4c0
SS
16037 tracepoint_count = 0;
16038
1bedd215
AC
16039 add_com ("ignore", class_breakpoint, ignore_command, _("\
16040Set ignore-count of breakpoint number N to COUNT.\n\
16041Usage is `ignore N COUNT'."));
c906108c 16042
1bedd215 16043 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
16044Set commands to be executed when the given breakpoints are hit.\n\
16045Give a space-separated breakpoint list as argument after \"commands\".\n\
16046A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16047(e.g. `5-7').\n\
c906108c
SS
16048With no argument, the targeted breakpoint is the last one set.\n\
16049The commands themselves follow starting on the next line.\n\
16050Type a line containing \"end\" to indicate the end of them.\n\
16051Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16052then no output is printed when it is hit, except what the commands print."));
c906108c 16053
d55637df 16054 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16055Specify breakpoint number N to break only if COND is true.\n\
c906108c 16056Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16057expression to be evaluated whenever breakpoint N is reached."));
d55637df 16058 set_cmd_completer (c, condition_completer);
c906108c 16059
1bedd215 16060 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16061Set a temporary breakpoint.\n\
c906108c
SS
16062Like \"break\" except the breakpoint is only temporary,\n\
16063so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16064by using \"enable delete\" on the breakpoint number.\n\
16065\n"
16066BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16067 set_cmd_completer (c, location_completer);
c94fdfd0 16068
1bedd215 16069 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16070Set a hardware assisted breakpoint.\n\
c906108c 16071Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16072some target hardware may not have this support.\n\
16073\n"
16074BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16075 set_cmd_completer (c, location_completer);
c906108c 16076
1bedd215 16077 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16078Set a temporary hardware assisted breakpoint.\n\
c906108c 16079Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16080so it will be deleted when hit.\n\
16081\n"
16082BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16083 set_cmd_completer (c, location_completer);
c906108c 16084
1bedd215
AC
16085 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16086Enable some breakpoints.\n\
c906108c
SS
16087Give breakpoint numbers (separated by spaces) as arguments.\n\
16088With no subcommand, breakpoints are enabled until you command otherwise.\n\
16089This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16090With a subcommand you can enable temporarily."),
c906108c 16091 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16092
16093 add_com_alias ("en", "enable", class_breakpoint, 1);
16094
84951ab5 16095 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16096Enable some breakpoints.\n\
c906108c
SS
16097Give breakpoint numbers (separated by spaces) as arguments.\n\
16098This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16099May be abbreviated to simply \"enable\".\n"),
c5aa993b 16100 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16101
1a966eab
AC
16102 add_cmd ("once", no_class, enable_once_command, _("\
16103Enable breakpoints for one hit. Give breakpoint numbers.\n\
16104If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16105 &enablebreaklist);
16106
1a966eab
AC
16107 add_cmd ("delete", no_class, enable_delete_command, _("\
16108Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16109If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16110 &enablebreaklist);
16111
816338b5
SS
16112 add_cmd ("count", no_class, enable_count_command, _("\
16113Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16114If a breakpoint is hit while enabled in this fashion,\n\
16115the count is decremented; when it reaches zero, the breakpoint is disabled."),
16116 &enablebreaklist);
16117
1a966eab
AC
16118 add_cmd ("delete", no_class, enable_delete_command, _("\
16119Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16120If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16121 &enablelist);
16122
1a966eab
AC
16123 add_cmd ("once", no_class, enable_once_command, _("\
16124Enable breakpoints for one hit. Give breakpoint numbers.\n\
16125If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16126 &enablelist);
16127
16128 add_cmd ("count", no_class, enable_count_command, _("\
16129Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16130If a breakpoint is hit while enabled in this fashion,\n\
16131the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16132 &enablelist);
16133
1bedd215
AC
16134 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16135Disable some breakpoints.\n\
c906108c
SS
16136Arguments are breakpoint numbers with spaces in between.\n\
16137To disable all breakpoints, give no argument.\n\
64b9b334 16138A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16139 &disablelist, "disable ", 1, &cmdlist);
16140 add_com_alias ("dis", "disable", class_breakpoint, 1);
16141 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16142
1a966eab
AC
16143 add_cmd ("breakpoints", class_alias, disable_command, _("\
16144Disable some breakpoints.\n\
c906108c
SS
16145Arguments are breakpoint numbers with spaces in between.\n\
16146To disable all breakpoints, give no argument.\n\
64b9b334 16147A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16148This command may be abbreviated \"disable\"."),
c906108c
SS
16149 &disablelist);
16150
1bedd215
AC
16151 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16152Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16153Arguments are breakpoint numbers with spaces in between.\n\
16154To delete all breakpoints, give no argument.\n\
16155\n\
16156Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16157The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16158 &deletelist, "delete ", 1, &cmdlist);
16159 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16160 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16161
1a966eab
AC
16162 add_cmd ("breakpoints", class_alias, delete_command, _("\
16163Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16164Arguments are breakpoint numbers with spaces in between.\n\
16165To delete all breakpoints, give no argument.\n\
1a966eab 16166This command may be abbreviated \"delete\"."),
c906108c
SS
16167 &deletelist);
16168
1bedd215 16169 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16170Clear breakpoint at specified location.\n\
16171Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16172\n\
16173With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16174is executing in.\n"
16175"\n" LOCATION_HELP_STRING "\n\
1bedd215 16176See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16177 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16178
1bedd215 16179 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16180Set breakpoint at specified location.\n"
31e2b00f 16181BREAK_ARGS_HELP ("break")));
5ba2abeb 16182 set_cmd_completer (c, location_completer);
c94fdfd0 16183
c906108c
SS
16184 add_com_alias ("b", "break", class_run, 1);
16185 add_com_alias ("br", "break", class_run, 1);
16186 add_com_alias ("bre", "break", class_run, 1);
16187 add_com_alias ("brea", "break", class_run, 1);
16188
c906108c
SS
16189 if (dbx_commands)
16190 {
1bedd215
AC
16191 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16192Break in function/address or break at a line in the current file."),
c5aa993b
JM
16193 &stoplist, "stop ", 1, &cmdlist);
16194 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16195 _("Break in function or address."), &stoplist);
c5aa993b 16196 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16197 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16198 add_com ("status", class_info, breakpoints_info, _("\
16199Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16200The \"Type\" column indicates one of:\n\
16201\tbreakpoint - normal breakpoint\n\
16202\twatchpoint - watchpoint\n\
16203The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16204the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16205breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16206address and file/line number respectively.\n\
16207\n\
16208Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16209are set to the address of the last breakpoint listed unless the command\n\
16210is prefixed with \"server \".\n\n\
c906108c 16211Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16212breakpoint set."));
c906108c
SS
16213 }
16214
1bedd215 16215 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16216Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16217The \"Type\" column indicates one of:\n\
16218\tbreakpoint - normal breakpoint\n\
16219\twatchpoint - watchpoint\n\
16220The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16221the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16222breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16223address and file/line number respectively.\n\
16224\n\
16225Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16226are set to the address of the last breakpoint listed unless the command\n\
16227is prefixed with \"server \".\n\n\
c906108c 16228Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16229breakpoint set."));
c906108c 16230
6b04bdb7
MS
16231 add_info_alias ("b", "breakpoints", 1);
16232
1a966eab
AC
16233 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16234Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16235The \"Type\" column indicates one of:\n\
16236\tbreakpoint - normal breakpoint\n\
16237\twatchpoint - watchpoint\n\
16238\tlongjmp - internal breakpoint used to step through longjmp()\n\
16239\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16240\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16241\tfinish - internal breakpoint used by the \"finish\" command\n\
16242The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16243the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16244breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16245address and file/line number respectively.\n\
16246\n\
16247Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16248are set to the address of the last breakpoint listed unless the command\n\
16249is prefixed with \"server \".\n\n\
c906108c 16250Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16251breakpoint set."),
c906108c
SS
16252 &maintenanceinfolist);
16253
44feb3ce
TT
16254 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16255Set catchpoints to catch events."),
16256 &catch_cmdlist, "catch ",
16257 0/*allow-unknown*/, &cmdlist);
16258
16259 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16260Set temporary catchpoints to catch events."),
16261 &tcatch_cmdlist, "tcatch ",
16262 0/*allow-unknown*/, &cmdlist);
16263
44feb3ce
TT
16264 add_catch_command ("fork", _("Catch calls to fork."),
16265 catch_fork_command_1,
a96d9b2e 16266 NULL,
44feb3ce
TT
16267 (void *) (uintptr_t) catch_fork_permanent,
16268 (void *) (uintptr_t) catch_fork_temporary);
16269 add_catch_command ("vfork", _("Catch calls to vfork."),
16270 catch_fork_command_1,
a96d9b2e 16271 NULL,
44feb3ce
TT
16272 (void *) (uintptr_t) catch_vfork_permanent,
16273 (void *) (uintptr_t) catch_vfork_temporary);
16274 add_catch_command ("exec", _("Catch calls to exec."),
16275 catch_exec_command_1,
a96d9b2e
SDJ
16276 NULL,
16277 CATCH_PERMANENT,
16278 CATCH_TEMPORARY);
edcc5120
TT
16279 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16280Usage: catch load [REGEX]\n\
16281If REGEX is given, only stop for libraries matching the regular expression."),
16282 catch_load_command_1,
16283 NULL,
16284 CATCH_PERMANENT,
16285 CATCH_TEMPORARY);
16286 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16287Usage: catch unload [REGEX]\n\
16288If REGEX is given, only stop for libraries matching the regular expression."),
16289 catch_unload_command_1,
16290 NULL,
16291 CATCH_PERMANENT,
16292 CATCH_TEMPORARY);
c5aa993b 16293
1bedd215
AC
16294 c = add_com ("watch", class_breakpoint, watch_command, _("\
16295Set a watchpoint for an expression.\n\
06a64a0b 16296Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16297A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16298an expression changes.\n\
16299If -l or -location is given, this evaluates EXPRESSION and watches\n\
16300the memory to which it refers."));
65d12d83 16301 set_cmd_completer (c, expression_completer);
c906108c 16302
1bedd215
AC
16303 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16304Set a read watchpoint for an expression.\n\
06a64a0b 16305Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16306A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16307an expression is read.\n\
16308If -l or -location is given, this evaluates EXPRESSION and watches\n\
16309the memory to which it refers."));
65d12d83 16310 set_cmd_completer (c, expression_completer);
c906108c 16311
1bedd215
AC
16312 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16313Set a watchpoint for an expression.\n\
06a64a0b 16314Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16315A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16316an expression is either read or written.\n\
16317If -l or -location is given, this evaluates EXPRESSION and watches\n\
16318the memory to which it refers."));
65d12d83 16319 set_cmd_completer (c, expression_completer);
c906108c 16320
d77f58be 16321 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16322Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16323
920d2a44
AC
16324 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16325 respond to changes - contrary to the description. */
85c07804
AC
16326 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16327 &can_use_hw_watchpoints, _("\
16328Set debugger's willingness to use watchpoint hardware."), _("\
16329Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16330If zero, gdb will not use hardware for new watchpoints, even if\n\
16331such is available. (However, any hardware watchpoints that were\n\
16332created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16333hardware.)"),
16334 NULL,
920d2a44 16335 show_can_use_hw_watchpoints,
85c07804 16336 &setlist, &showlist);
c906108c
SS
16337
16338 can_use_hw_watchpoints = 1;
fa8d40ab 16339
1042e4c0
SS
16340 /* Tracepoint manipulation commands. */
16341
16342 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16343Set a tracepoint at specified location.\n\
1042e4c0
SS
16344\n"
16345BREAK_ARGS_HELP ("trace") "\n\
16346Do \"help tracepoints\" for info on other tracepoint commands."));
16347 set_cmd_completer (c, location_completer);
16348
16349 add_com_alias ("tp", "trace", class_alias, 0);
16350 add_com_alias ("tr", "trace", class_alias, 1);
16351 add_com_alias ("tra", "trace", class_alias, 1);
16352 add_com_alias ("trac", "trace", class_alias, 1);
16353
7a697b8d 16354 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16355Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16356\n"
16357BREAK_ARGS_HELP ("ftrace") "\n\
16358Do \"help tracepoints\" for info on other tracepoint commands."));
16359 set_cmd_completer (c, location_completer);
16360
0fb4aa4b 16361 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16362Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16363\n\
16364strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16365LOCATION may be a linespec, explicit, or address location (described below) \n\
16366or -m MARKER_ID.\n\n\
16367If a marker id is specified, probe the marker with that name. With\n\
16368no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16369Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16370This collects arbitrary user data passed in the probe point call to the\n\
16371tracing library. You can inspect it when analyzing the trace buffer,\n\
16372by printing the $_sdata variable like any other convenience variable.\n\
16373\n\
16374CONDITION is a boolean expression.\n\
629500fa 16375\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16376Multiple tracepoints at one place are permitted, and useful if their\n\
16377conditions are different.\n\
0fb4aa4b
PA
16378\n\
16379Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16380Do \"help tracepoints\" for info on other tracepoint commands."));
16381 set_cmd_completer (c, location_completer);
16382
1042e4c0 16383 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16384Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16385Convenience variable \"$tpnum\" contains the number of the\n\
16386last tracepoint set."));
16387
16388 add_info_alias ("tp", "tracepoints", 1);
16389
16390 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16391Delete specified tracepoints.\n\
16392Arguments are tracepoint numbers, separated by spaces.\n\
16393No argument means delete all tracepoints."),
16394 &deletelist);
7e20dfcd 16395 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16396
16397 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16398Disable specified tracepoints.\n\
16399Arguments are tracepoint numbers, separated by spaces.\n\
16400No argument means disable all tracepoints."),
16401 &disablelist);
16402 deprecate_cmd (c, "disable");
16403
16404 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16405Enable specified tracepoints.\n\
16406Arguments are tracepoint numbers, separated by spaces.\n\
16407No argument means enable all tracepoints."),
16408 &enablelist);
16409 deprecate_cmd (c, "enable");
16410
16411 add_com ("passcount", class_trace, trace_pass_command, _("\
16412Set the passcount for a tracepoint.\n\
16413The trace will end when the tracepoint has been passed 'count' times.\n\
16414Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16415if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16416
6149aea9
PA
16417 add_prefix_cmd ("save", class_breakpoint, save_command,
16418 _("Save breakpoint definitions as a script."),
16419 &save_cmdlist, "save ",
16420 0/*allow-unknown*/, &cmdlist);
16421
16422 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16423Save current breakpoint definitions as a script.\n\
cce7e648 16424This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16425catchpoints, tracepoints). Use the 'source' command in another debug\n\
16426session to restore them."),
16427 &save_cmdlist);
16428 set_cmd_completer (c, filename_completer);
16429
16430 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16431Save current tracepoint definitions as a script.\n\
6149aea9
PA
16432Use the 'source' command in another debug session to restore them."),
16433 &save_cmdlist);
1042e4c0
SS
16434 set_cmd_completer (c, filename_completer);
16435
6149aea9
PA
16436 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16437 deprecate_cmd (c, "save tracepoints");
16438
1bedd215 16439 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16440Breakpoint specific settings\n\
16441Configure various breakpoint-specific variables such as\n\
1bedd215 16442pending breakpoint behavior"),
fa8d40ab
JJ
16443 &breakpoint_set_cmdlist, "set breakpoint ",
16444 0/*allow-unknown*/, &setlist);
1bedd215 16445 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16446Breakpoint specific settings\n\
16447Configure various breakpoint-specific variables such as\n\
1bedd215 16448pending breakpoint behavior"),
fa8d40ab
JJ
16449 &breakpoint_show_cmdlist, "show breakpoint ",
16450 0/*allow-unknown*/, &showlist);
16451
7915a72c
AC
16452 add_setshow_auto_boolean_cmd ("pending", no_class,
16453 &pending_break_support, _("\
16454Set debugger's behavior regarding pending breakpoints."), _("\
16455Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16456If on, an unrecognized breakpoint location will cause gdb to create a\n\
16457pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16458an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16459user-query to see if a pending breakpoint should be created."),
2c5b56ce 16460 NULL,
920d2a44 16461 show_pending_break_support,
6e1d7d6c
AC
16462 &breakpoint_set_cmdlist,
16463 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16464
16465 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16466
16467 add_setshow_boolean_cmd ("auto-hw", no_class,
16468 &automatic_hardware_breakpoints, _("\
16469Set automatic usage of hardware breakpoints."), _("\
16470Show automatic usage of hardware breakpoints."), _("\
16471If set, the debugger will automatically use hardware breakpoints for\n\
16472breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16473a warning will be emitted for such breakpoints."),
16474 NULL,
16475 show_automatic_hardware_breakpoints,
16476 &breakpoint_set_cmdlist,
16477 &breakpoint_show_cmdlist);
74960c60 16478
a25a5a45
PA
16479 add_setshow_boolean_cmd ("always-inserted", class_support,
16480 &always_inserted_mode, _("\
74960c60
VP
16481Set mode for inserting breakpoints."), _("\
16482Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16483When this mode is on, breakpoints are inserted immediately as soon as\n\
16484they're created, kept inserted even when execution stops, and removed\n\
16485only when the user deletes them. When this mode is off (the default),\n\
16486breakpoints are inserted only when execution continues, and removed\n\
16487when execution stops."),
72d0e2c5
YQ
16488 NULL,
16489 &show_always_inserted_mode,
16490 &breakpoint_set_cmdlist,
16491 &breakpoint_show_cmdlist);
f1310107 16492
b775012e
LM
16493 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16494 condition_evaluation_enums,
16495 &condition_evaluation_mode_1, _("\
16496Set mode of breakpoint condition evaluation."), _("\
16497Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16498When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16499evaluated on the host's side by GDB. When it is set to \"target\",\n\
16500breakpoint conditions will be downloaded to the target (if the target\n\
16501supports such feature) and conditions will be evaluated on the target's side.\n\
16502If this is set to \"auto\" (default), this will be automatically set to\n\
16503\"target\" if it supports condition evaluation, otherwise it will\n\
16504be set to \"gdb\""),
16505 &set_condition_evaluation_mode,
16506 &show_condition_evaluation_mode,
16507 &breakpoint_set_cmdlist,
16508 &breakpoint_show_cmdlist);
16509
f1310107
TJB
16510 add_com ("break-range", class_breakpoint, break_range_command, _("\
16511Set a breakpoint for an address range.\n\
16512break-range START-LOCATION, END-LOCATION\n\
16513where START-LOCATION and END-LOCATION can be one of the following:\n\
16514 LINENUM, for that line in the current file,\n\
16515 FILE:LINENUM, for that line in that file,\n\
16516 +OFFSET, for that number of lines after the current line\n\
16517 or the start of the range\n\
16518 FUNCTION, for the first line in that function,\n\
16519 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16520 *ADDRESS, for the instruction at that address.\n\
16521\n\
16522The breakpoint will stop execution of the inferior whenever it executes\n\
16523an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16524range (including START-LOCATION and END-LOCATION)."));
16525
e7e0cddf 16526 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16527Set a dynamic printf at specified location.\n\
e7e0cddf 16528dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16529location may be a linespec, explicit, or address location.\n"
16530"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16531 set_cmd_completer (c, location_completer);
16532
16533 add_setshow_enum_cmd ("dprintf-style", class_support,
16534 dprintf_style_enums, &dprintf_style, _("\
16535Set the style of usage for dynamic printf."), _("\
16536Show the style of usage for dynamic printf."), _("\
16537This setting chooses how GDB will do a dynamic printf.\n\
16538If the value is \"gdb\", then the printing is done by GDB to its own\n\
16539console, as with the \"printf\" command.\n\
16540If the value is \"call\", the print is done by calling a function in your\n\
16541program; by default printf(), but you can choose a different function or\n\
16542output stream by setting dprintf-function and dprintf-channel."),
16543 update_dprintf_commands, NULL,
16544 &setlist, &showlist);
16545
16546 dprintf_function = xstrdup ("printf");
16547 add_setshow_string_cmd ("dprintf-function", class_support,
16548 &dprintf_function, _("\
16549Set the function to use for dynamic printf"), _("\
16550Show the function to use for dynamic printf"), NULL,
16551 update_dprintf_commands, NULL,
16552 &setlist, &showlist);
16553
16554 dprintf_channel = xstrdup ("");
16555 add_setshow_string_cmd ("dprintf-channel", class_support,
16556 &dprintf_channel, _("\
16557Set the channel to use for dynamic printf"), _("\
16558Show the channel to use for dynamic printf"), NULL,
16559 update_dprintf_commands, NULL,
16560 &setlist, &showlist);
16561
d3ce09f5
SS
16562 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16563 &disconnected_dprintf, _("\
16564Set whether dprintf continues after GDB disconnects."), _("\
16565Show whether dprintf continues after GDB disconnects."), _("\
16566Use this to let dprintf commands continue to hit and produce output\n\
16567even if GDB disconnects or detaches from the target."),
16568 NULL,
16569 NULL,
16570 &setlist, &showlist);
16571
16572 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16573agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16574(target agent only) This is useful for formatted output in user-defined commands."));
16575
765dc015 16576 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16577
16578 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16579 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16580}
This page took 3.147612 seconds and 4 git commands to generate.