Change return type of gdbarch_software_single_step to vector<CORE_ADDR>
[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>
104c1213 83
e7e8980f
YQ
84/* Enums for exception-handling support. */
85enum exception_event_kind
86{
87 EX_EVENT_THROW,
591f19e8 88 EX_EVENT_RETHROW,
e7e8980f
YQ
89 EX_EVENT_CATCH
90};
91
4a64f543 92/* Prototypes for local functions. */
c906108c 93
a14ed312 94static void enable_delete_command (char *, int);
c906108c 95
a14ed312 96static void enable_once_command (char *, int);
c906108c 97
816338b5
SS
98static void enable_count_command (char *, int);
99
a14ed312 100static void disable_command (char *, int);
c906108c 101
a14ed312 102static void enable_command (char *, int);
c906108c 103
896b6bda
PA
104static void map_breakpoint_numbers (const char *,
105 void (*) (struct breakpoint *,
106 void *),
95a42b64 107 void *);
c906108c 108
a14ed312 109static void ignore_command (char *, int);
c906108c 110
4efb68b1 111static int breakpoint_re_set_one (void *);
c906108c 112
348d480f
PA
113static void breakpoint_re_set_default (struct breakpoint *);
114
f00aae0f
KS
115static void
116 create_sals_from_location_default (const struct event_location *location,
117 struct linespec_result *canonical,
118 enum bptype type_wanted);
983af33b
SDJ
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
e7e0cddf 122 char *, char *, enum bptype,
983af33b
SDJ
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
44f238bb 126 int, int, int, unsigned);
983af33b 127
f00aae0f
KS
128static void decode_location_default (struct breakpoint *b,
129 const struct event_location *location,
c2f4122d 130 struct program_space *search_pspace,
f00aae0f 131 struct symtabs_and_lines *sals);
983af33b 132
a14ed312 133static void clear_command (char *, int);
c906108c 134
a14ed312 135static void catch_command (char *, int);
c906108c 136
a9634178 137static int can_use_hardware_watchpoint (struct value *);
c906108c 138
98deb0da 139static void break_command_1 (char *, int, int);
c906108c 140
a14ed312 141static void mention (struct breakpoint *);
c906108c 142
348d480f
PA
143static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
c0a91b2b 145 const struct breakpoint_ops *);
3742cc8b
YQ
146static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
4a64f543
MS
149/* This function is used in gdbtk sources and thus can not be made
150 static. */
63c252f8 151struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 152 struct symtab_and_line,
c0a91b2b
TT
153 enum bptype,
154 const struct breakpoint_ops *);
c906108c 155
06edf0c0
PA
156static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
a1aa2221
LM
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
06edf0c0 161
76897487
KB
162static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
a6d9a66e
UW
164static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
88f7da05 166 enum bptype bptype);
76897487 167
6c95b8df
PA
168static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
5af949e3 170 struct obj_section *, int);
c906108c 171
85d721b8
PA
172static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
f1310107
TJB
175static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
d35ae833
PA
179static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
a14ed312 183static void breakpoints_info (char *, int);
c906108c 184
d77f58be
SS
185static void watchpoints_info (char *, int);
186
e5a67952
MS
187static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
c906108c 189
4efb68b1 190static int breakpoint_cond_eval (void *);
c906108c 191
4efb68b1 192static void cleanup_executing_breakpoints (void *);
c906108c 193
a14ed312 194static void commands_command (char *, int);
c906108c 195
a14ed312 196static void condition_command (char *, int);
c906108c 197
834c0d03 198static int remove_breakpoint (struct bp_location *);
b2b6a7da 199static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 200
e514a9d6 201static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 202
4efb68b1 203static int watchpoint_check (void *);
c906108c 204
a14ed312 205static void maintenance_info_breakpoints (char *, int);
c906108c 206
a14ed312 207static int hw_breakpoint_used_count (void);
c906108c 208
a1398e0c
PA
209static int hw_watchpoint_use_count (struct breakpoint *);
210
211static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
c906108c 214
a14ed312 215static void hbreak_command (char *, int);
c906108c 216
a14ed312 217static void thbreak_command (char *, int);
c906108c 218
816338b5
SS
219static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
c906108c 221
a14ed312 222static void stop_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopin_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void stopat_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
fe3f5fa8 230static void free_bp_location (struct bp_location *loc);
f431efe5
PA
231static void incref_bp_location (struct bp_location *loc);
232static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 233
39d61571 234static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 235
44702360
PA
236/* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238enum ugll_insert_mode
239{
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
a25a5a45
PA
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
04086b45
PA
259 UGLL_MAY_INSERT,
260
a25a5a45
PA
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
04086b45 268 UGLL_INSERT
44702360
PA
269};
270
271static void update_global_location_list (enum ugll_insert_mode);
a5606eee 272
44702360 273static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 274
d77f58be 275static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
276
277static void insert_breakpoint_locations (void);
a5606eee 278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305/* Internal breakpoints class type. */
06edf0c0 306static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
307
308/* Momentary breakpoints class type. */
06edf0c0
PA
309static struct breakpoint_ops momentary_breakpoint_ops;
310
e2e4d78b
JK
311/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312static struct breakpoint_ops longjmp_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
bde6261a 347static char *dprintf_function;
d3ce09f5
SS
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
bde6261a 357static char *dprintf_channel;
d3ce09f5
SS
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
372316f1
PA
474 struct thread_info *tp;
475
a25a5a45
PA
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
b57bacec
PA
483 if (threads_are_executing ())
484 return 1;
372316f1
PA
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593 580#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
581 for (BP_TMP = bp_locations; \
582 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
f5336ca5 595 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
f5336ca5 609/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 610
f5336ca5 611static struct bp_location **bp_locations;
876fa593 612
f5336ca5 613/* Number of elements of BP_LOCATIONS. */
876fa593 614
f5336ca5 615static unsigned bp_locations_count;
876fa593 616
4a64f543 617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 618 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 619 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 620 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 621 an address you need to read. */
876fa593 622
f5336ca5 623static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
627 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 629 scan for shadow bytes for an address you need to read. */
876fa593 630
f5336ca5 631static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 632
4a64f543 633/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
634 from the bp_locations array, but for which a hit may still be
635 reported by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
badd37ce
SDJ
656/* See declaration at breakpoint.h. */
657
658struct breakpoint *
659breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661{
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671}
672
468d015d
JJ
673/* Return whether a breakpoint is an active enabled breakpoint. */
674static int
675breakpoint_enabled (struct breakpoint *b)
676{
0d381245 677 return (b->enable_state == bp_enabled);
468d015d
JJ
678}
679
c906108c
SS
680/* Set breakpoint count to NUM. */
681
95a42b64 682static void
fba45db2 683set_breakpoint_count (int num)
c906108c 684{
86b17b60 685 prev_breakpoint_count = breakpoint_count;
c906108c 686 breakpoint_count = num;
4fa62494 687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
688}
689
86b17b60
PA
690/* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692static int rbreak_start_breakpoint_count;
693
95a42b64
TT
694/* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
86b17b60 696
95a42b64
TT
697void
698start_rbreak_breakpoints (void)
699{
86b17b60 700 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
701}
702
703/* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
86b17b60 705
95a42b64
TT
706void
707end_rbreak_breakpoints (void)
708{
86b17b60 709 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
710}
711
4a64f543 712/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
713
714void
fba45db2 715clear_breakpoint_hit_counts (void)
c906108c
SS
716{
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721}
722
9add0f1b
TT
723/* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726static struct counted_command_line *
727alloc_counted_command_line (struct command_line *commands)
728{
8d749320 729 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 730
9add0f1b
TT
731 result->refc = 1;
732 result->commands = commands;
8d749320 733
9add0f1b
TT
734 return result;
735}
736
737/* Increment reference count. This does nothing if CMD is NULL. */
738
739static void
740incref_counted_command_line (struct counted_command_line *cmd)
741{
742 if (cmd)
743 ++cmd->refc;
744}
745
746/* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750static void
751decref_counted_command_line (struct counted_command_line **cmdp)
752{
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762}
763
764/* A cleanup function that calls decref_counted_command_line. */
765
766static void
767do_cleanup_counted_command_line (void *arg)
768{
9a3c8263 769 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
770}
771
772/* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775static struct cleanup *
776make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777{
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779}
780
c906108c 781\f
48cb2d85
VP
782/* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785struct breakpoint *
786get_breakpoint (int num)
787{
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795}
5c44784c 796
c906108c 797\f
adc36818 798
b775012e
LM
799/* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802static void
803mark_breakpoint_modified (struct breakpoint *b)
804{
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820}
821
822/* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825static void
826mark_breakpoint_location_modified (struct bp_location *loc)
827{
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841}
842
843/* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846static void
847set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849{
b775012e
LM
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
abf1152a
JK
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
b775012e
LM
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
b775012e
LM
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
44702360 897 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
898 }
899
900 return;
901}
902
903/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906static void
907show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909{
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919}
920
921/* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 923 the more general bp_locations_compare function. */
b775012e
LM
924
925static int
f5336ca5 926bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 927{
9a3c8263
SM
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935}
936
937/* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942static struct bp_location **
943get_first_locp_gte_addr (CORE_ADDR address)
944{
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
b775012e
LM
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
9a3c8263 953 locp_found = ((struct bp_location **)
f5336ca5 954 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 955 sizeof (struct bp_location **),
f5336ca5 956 bp_locations_compare_addrs));
b775012e
LM
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 964 while ((locp_found - 1) >= bp_locations
b775012e
LM
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969}
970
adc36818 971void
7a26bd4d 972set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
973 int from_tty)
974{
3a5c3e22
PA
975 xfree (b->cond_string);
976 b->cond_string = NULL;
adc36818 977
3a5c3e22 978 if (is_watchpoint (b))
adc36818 979 {
3a5c3e22
PA
980 struct watchpoint *w = (struct watchpoint *) b;
981
4d01a485 982 w->cond_exp.reset ();
3a5c3e22
PA
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
4d01a485 990 loc->cond.reset ();
b775012e
LM
991
992 /* No need to free the condition agent expression
993 bytecode (if we have one). We will handle this
994 when we go through update_global_location_list. */
3a5c3e22 995 }
adc36818 996 }
adc36818
PM
997
998 if (*exp == 0)
999 {
1000 if (from_tty)
1001 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1002 }
1003 else
1004 {
bbc13ae3 1005 const char *arg = exp;
cc59ec59 1006
adc36818
PM
1007 /* I don't know if it matters whether this is the string the user
1008 typed in or the decompiled expression. */
1009 b->cond_string = xstrdup (arg);
1010 b->condition_not_parsed = 0;
1011
1012 if (is_watchpoint (b))
1013 {
3a5c3e22
PA
1014 struct watchpoint *w = (struct watchpoint *) b;
1015
adc36818
PM
1016 innermost_block = NULL;
1017 arg = exp;
1bb9788d 1018 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1019 if (*arg)
1020 error (_("Junk at end of expression"));
3a5c3e22 1021 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1022 }
1023 else
1024 {
3a5c3e22
PA
1025 struct bp_location *loc;
1026
adc36818
PM
1027 for (loc = b->loc; loc; loc = loc->next)
1028 {
1029 arg = exp;
1030 loc->cond =
1bb9788d
TT
1031 parse_exp_1 (&arg, loc->address,
1032 block_for_pc (loc->address), 0);
adc36818
PM
1033 if (*arg)
1034 error (_("Junk at end of expression"));
1035 }
1036 }
1037 }
b775012e
LM
1038 mark_breakpoint_modified (b);
1039
8d3788bd 1040 observer_notify_breakpoint_modified (b);
adc36818
PM
1041}
1042
d55637df
TT
1043/* Completion for the "condition" command. */
1044
1045static VEC (char_ptr) *
6f937416
PA
1046condition_completer (struct cmd_list_element *cmd,
1047 const char *text, const char *word)
d55637df 1048{
6f937416 1049 const char *space;
d55637df 1050
6f937416
PA
1051 text = skip_spaces_const (text);
1052 space = skip_to_space_const (text);
d55637df
TT
1053 if (*space == '\0')
1054 {
1055 int len;
1056 struct breakpoint *b;
1057 VEC (char_ptr) *result = NULL;
1058
1059 if (text[0] == '$')
1060 {
1061 /* We don't support completion of history indices. */
1062 if (isdigit (text[1]))
1063 return NULL;
1064 return complete_internalvar (&text[1]);
1065 }
1066
1067 /* We're completing the breakpoint number. */
1068 len = strlen (text);
1069
1070 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1071 {
1072 char number[50];
1073
1074 xsnprintf (number, sizeof (number), "%d", b->number);
1075
1076 if (strncmp (number, text, len) == 0)
1077 VEC_safe_push (char_ptr, result, xstrdup (number));
1078 }
d55637df
TT
1079
1080 return result;
1081 }
1082
1083 /* We're completing the expression part. */
6f937416 1084 text = skip_spaces_const (space);
d55637df
TT
1085 return expression_completer (cmd, text, word);
1086}
1087
c906108c
SS
1088/* condition N EXP -- set break condition of breakpoint N to EXP. */
1089
1090static void
fba45db2 1091condition_command (char *arg, int from_tty)
c906108c 1092{
52f0bd74 1093 struct breakpoint *b;
c906108c 1094 char *p;
52f0bd74 1095 int bnum;
c906108c
SS
1096
1097 if (arg == 0)
e2e0b3e5 1098 error_no_arg (_("breakpoint number"));
c906108c
SS
1099
1100 p = arg;
1101 bnum = get_number (&p);
5c44784c 1102 if (bnum == 0)
8a3fe4f8 1103 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1104
1105 ALL_BREAKPOINTS (b)
1106 if (b->number == bnum)
2f069f6f 1107 {
6dddc817
DE
1108 /* Check if this breakpoint has a "stop" method implemented in an
1109 extension language. This method and conditions entered into GDB
1110 from the CLI are mutually exclusive. */
1111 const struct extension_language_defn *extlang
1112 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1113
1114 if (extlang != NULL)
1115 {
1116 error (_("Only one stop condition allowed. There is currently"
1117 " a %s stop condition defined for this breakpoint."),
1118 ext_lang_capitalized_name (extlang));
1119 }
2566ad2d 1120 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1121
1122 if (is_breakpoint (b))
44702360 1123 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1124
2f069f6f
JB
1125 return;
1126 }
c906108c 1127
8a3fe4f8 1128 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1129}
1130
a7bdde9e
VP
1131/* Check that COMMAND do not contain commands that are suitable
1132 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1133 Throw if any such commands is found. */
1134
a7bdde9e
VP
1135static void
1136check_no_tracepoint_commands (struct command_line *commands)
1137{
1138 struct command_line *c;
cc59ec59 1139
a7bdde9e
VP
1140 for (c = commands; c; c = c->next)
1141 {
1142 int i;
1143
1144 if (c->control_type == while_stepping_control)
3e43a32a
MS
1145 error (_("The 'while-stepping' command can "
1146 "only be used for tracepoints"));
a7bdde9e
VP
1147
1148 for (i = 0; i < c->body_count; ++i)
1149 check_no_tracepoint_commands ((c->body_list)[i]);
1150
1151 /* Not that command parsing removes leading whitespace and comment
4a64f543 1152 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1153 command directly. */
1154 if (strstr (c->line, "collect ") == c->line)
1155 error (_("The 'collect' command can only be used for tracepoints"));
1156
51661e93
VP
1157 if (strstr (c->line, "teval ") == c->line)
1158 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1159 }
1160}
1161
d77f58be
SS
1162/* Encapsulate tests for different types of tracepoints. */
1163
d9b3f62e
PA
1164static int
1165is_tracepoint_type (enum bptype type)
1166{
1167 return (type == bp_tracepoint
1168 || type == bp_fast_tracepoint
1169 || type == bp_static_tracepoint);
1170}
1171
a7bdde9e 1172int
d77f58be 1173is_tracepoint (const struct breakpoint *b)
a7bdde9e 1174{
d9b3f62e 1175 return is_tracepoint_type (b->type);
a7bdde9e 1176}
d9b3f62e 1177
e5dd4106 1178/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1179 breakpoint. This function will throw an exception if a problem is
1180 found. */
48cb2d85 1181
95a42b64
TT
1182static void
1183validate_commands_for_breakpoint (struct breakpoint *b,
1184 struct command_line *commands)
48cb2d85 1185{
d77f58be 1186 if (is_tracepoint (b))
a7bdde9e 1187 {
c9a6ce02 1188 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1189 struct command_line *c;
1190 struct command_line *while_stepping = 0;
c9a6ce02
PA
1191
1192 /* Reset the while-stepping step count. The previous commands
1193 might have included a while-stepping action, while the new
1194 ones might not. */
1195 t->step_count = 0;
1196
1197 /* We need to verify that each top-level element of commands is
1198 valid for tracepoints, that there's at most one
1199 while-stepping element, and that the while-stepping's body
1200 has valid tracing commands excluding nested while-stepping.
1201 We also need to validate the tracepoint action line in the
1202 context of the tracepoint --- validate_actionline actually
1203 has side effects, like setting the tracepoint's
1204 while-stepping STEP_COUNT, in addition to checking if the
1205 collect/teval actions parse and make sense in the
1206 tracepoint's context. */
a7bdde9e
VP
1207 for (c = commands; c; c = c->next)
1208 {
a7bdde9e
VP
1209 if (c->control_type == while_stepping_control)
1210 {
1211 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1212 error (_("The 'while-stepping' command "
1213 "cannot be used for fast tracepoint"));
0fb4aa4b 1214 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1215 error (_("The 'while-stepping' command "
1216 "cannot be used for static tracepoint"));
a7bdde9e
VP
1217
1218 if (while_stepping)
3e43a32a
MS
1219 error (_("The 'while-stepping' command "
1220 "can be used only once"));
a7bdde9e
VP
1221 else
1222 while_stepping = c;
1223 }
c9a6ce02
PA
1224
1225 validate_actionline (c->line, b);
a7bdde9e
VP
1226 }
1227 if (while_stepping)
1228 {
1229 struct command_line *c2;
1230
1231 gdb_assert (while_stepping->body_count == 1);
1232 c2 = while_stepping->body_list[0];
1233 for (; c2; c2 = c2->next)
1234 {
a7bdde9e
VP
1235 if (c2->control_type == while_stepping_control)
1236 error (_("The 'while-stepping' command cannot be nested"));
1237 }
1238 }
1239 }
1240 else
1241 {
1242 check_no_tracepoint_commands (commands);
1243 }
95a42b64
TT
1244}
1245
0fb4aa4b
PA
1246/* Return a vector of all the static tracepoints set at ADDR. The
1247 caller is responsible for releasing the vector. */
1248
1249VEC(breakpoint_p) *
1250static_tracepoints_here (CORE_ADDR addr)
1251{
1252 struct breakpoint *b;
1253 VEC(breakpoint_p) *found = 0;
1254 struct bp_location *loc;
1255
1256 ALL_BREAKPOINTS (b)
1257 if (b->type == bp_static_tracepoint)
1258 {
1259 for (loc = b->loc; loc; loc = loc->next)
1260 if (loc->address == addr)
1261 VEC_safe_push(breakpoint_p, found, b);
1262 }
1263
1264 return found;
1265}
1266
95a42b64 1267/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1268 validate that only allowed commands are included. */
95a42b64
TT
1269
1270void
4a64f543 1271breakpoint_set_commands (struct breakpoint *b,
93921405 1272 command_line_up &&commands)
95a42b64 1273{
93921405 1274 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1275
9add0f1b 1276 decref_counted_command_line (&b->commands);
93921405 1277 b->commands = alloc_counted_command_line (commands.release ());
8d3788bd 1278 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1279}
1280
45a43567
TT
1281/* Set the internal `silent' flag on the breakpoint. Note that this
1282 is not the same as the "silent" that may appear in the breakpoint's
1283 commands. */
1284
1285void
1286breakpoint_set_silent (struct breakpoint *b, int silent)
1287{
1288 int old_silent = b->silent;
1289
1290 b->silent = silent;
1291 if (old_silent != silent)
8d3788bd 1292 observer_notify_breakpoint_modified (b);
45a43567
TT
1293}
1294
1295/* Set the thread for this breakpoint. If THREAD is -1, make the
1296 breakpoint work for any thread. */
1297
1298void
1299breakpoint_set_thread (struct breakpoint *b, int thread)
1300{
1301 int old_thread = b->thread;
1302
1303 b->thread = thread;
1304 if (old_thread != thread)
8d3788bd 1305 observer_notify_breakpoint_modified (b);
45a43567
TT
1306}
1307
1308/* Set the task for this breakpoint. If TASK is 0, make the
1309 breakpoint work for any task. */
1310
1311void
1312breakpoint_set_task (struct breakpoint *b, int task)
1313{
1314 int old_task = b->task;
1315
1316 b->task = task;
1317 if (old_task != task)
8d3788bd 1318 observer_notify_breakpoint_modified (b);
45a43567
TT
1319}
1320
95a42b64
TT
1321void
1322check_tracepoint_command (char *line, void *closure)
a7bdde9e 1323{
9a3c8263 1324 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1325
6f937416 1326 validate_actionline (line, b);
a7bdde9e
VP
1327}
1328
95a42b64
TT
1329/* A structure used to pass information through
1330 map_breakpoint_numbers. */
1331
1332struct commands_info
1333{
1334 /* True if the command was typed at a tty. */
1335 int from_tty;
86b17b60
PA
1336
1337 /* The breakpoint range spec. */
896b6bda 1338 const char *arg;
86b17b60 1339
95a42b64
TT
1340 /* Non-NULL if the body of the commands are being read from this
1341 already-parsed command. */
1342 struct command_line *control;
86b17b60 1343
95a42b64
TT
1344 /* The command lines read from the user, or NULL if they have not
1345 yet been read. */
1346 struct counted_command_line *cmd;
1347};
1348
1349/* A callback for map_breakpoint_numbers that sets the commands for
1350 commands_command. */
1351
c906108c 1352static void
95a42b64 1353do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1354{
9a3c8263 1355 struct commands_info *info = (struct commands_info *) data;
c906108c 1356
95a42b64
TT
1357 if (info->cmd == NULL)
1358 {
93921405 1359 command_line_up l;
5c44784c 1360
95a42b64
TT
1361 if (info->control != NULL)
1362 l = copy_command_lines (info->control->body_list[0]);
1363 else
86b17b60
PA
1364 {
1365 struct cleanup *old_chain;
1366 char *str;
c5aa993b 1367
3e43a32a
MS
1368 str = xstrprintf (_("Type commands for breakpoint(s) "
1369 "%s, one per line."),
86b17b60
PA
1370 info->arg);
1371
1372 old_chain = make_cleanup (xfree, str);
1373
1374 l = read_command_lines (str,
1375 info->from_tty, 1,
d77f58be 1376 (is_tracepoint (b)
86b17b60
PA
1377 ? check_tracepoint_command : 0),
1378 b);
1379
1380 do_cleanups (old_chain);
1381 }
a7bdde9e 1382
93921405 1383 info->cmd = alloc_counted_command_line (l.release ());
95a42b64
TT
1384 }
1385
1386 /* If a breakpoint was on the list more than once, we don't need to
1387 do anything. */
1388 if (b->commands != info->cmd)
1389 {
1390 validate_commands_for_breakpoint (b, info->cmd->commands);
1391 incref_counted_command_line (info->cmd);
1392 decref_counted_command_line (&b->commands);
1393 b->commands = info->cmd;
8d3788bd 1394 observer_notify_breakpoint_modified (b);
c5aa993b 1395 }
95a42b64
TT
1396}
1397
1398static void
896b6bda 1399commands_command_1 (const char *arg, int from_tty,
4a64f543 1400 struct command_line *control)
95a42b64
TT
1401{
1402 struct cleanup *cleanups;
1403 struct commands_info info;
1404
1405 info.from_tty = from_tty;
1406 info.control = control;
1407 info.cmd = NULL;
1408 /* If we read command lines from the user, then `info' will hold an
1409 extra reference to the commands that we must clean up. */
1410 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1411
896b6bda
PA
1412 std::string new_arg;
1413
95a42b64
TT
1414 if (arg == NULL || !*arg)
1415 {
86b17b60 1416 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1417 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1418 breakpoint_count);
95a42b64 1419 else if (breakpoint_count > 0)
896b6bda 1420 new_arg = string_printf ("%d", breakpoint_count);
95a42b64 1421 }
9766ced4 1422 else
896b6bda 1423 new_arg = arg;
86b17b60 1424
896b6bda 1425 info.arg = new_arg.c_str ();
95a42b64 1426
896b6bda 1427 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
95a42b64
TT
1428
1429 if (info.cmd == NULL)
1430 error (_("No breakpoints specified."));
1431
1432 do_cleanups (cleanups);
1433}
1434
1435static void
1436commands_command (char *arg, int from_tty)
1437{
1438 commands_command_1 (arg, from_tty, NULL);
c906108c 1439}
40c03ae8
EZ
1440
1441/* Like commands_command, but instead of reading the commands from
1442 input stream, takes them from an already parsed command structure.
1443
1444 This is used by cli-script.c to DTRT with breakpoint commands
1445 that are part of if and while bodies. */
1446enum command_control_type
896b6bda 1447commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1448{
95a42b64
TT
1449 commands_command_1 (arg, 0, cmd);
1450 return simple_control;
40c03ae8 1451}
876fa593
JK
1452
1453/* Return non-zero if BL->TARGET_INFO contains valid information. */
1454
1455static int
1456bp_location_has_shadow (struct bp_location *bl)
1457{
1458 if (bl->loc_type != bp_loc_software_breakpoint)
1459 return 0;
1460 if (!bl->inserted)
1461 return 0;
1462 if (bl->target_info.shadow_len == 0)
e5dd4106 1463 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1464 return 0;
1465 return 1;
1466}
1467
9d497a19
PA
1468/* Update BUF, which is LEN bytes read from the target address
1469 MEMADDR, by replacing a memory breakpoint with its shadowed
1470 contents.
1471
1472 If READBUF is not NULL, this buffer must not overlap with the of
1473 the breakpoint location's shadow_contents buffer. Otherwise, a
1474 failed assertion internal error will be raised. */
1475
1476static void
1477one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1478 const gdb_byte *writebuf_org,
1479 ULONGEST memaddr, LONGEST len,
1480 struct bp_target_info *target_info,
1481 struct gdbarch *gdbarch)
1482{
1483 /* Now do full processing of the found relevant range of elements. */
1484 CORE_ADDR bp_addr = 0;
1485 int bp_size = 0;
1486 int bptoffset = 0;
1487
1488 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1489 current_program_space->aspace, 0))
1490 {
1491 /* The breakpoint is inserted in a different address space. */
1492 return;
1493 }
1494
1495 /* Addresses and length of the part of the breakpoint that
1496 we need to copy. */
1497 bp_addr = target_info->placed_address;
1498 bp_size = target_info->shadow_len;
1499
1500 if (bp_addr + bp_size <= memaddr)
1501 {
1502 /* The breakpoint is entirely before the chunk of memory we are
1503 reading. */
1504 return;
1505 }
1506
1507 if (bp_addr >= memaddr + len)
1508 {
1509 /* The breakpoint is entirely after the chunk of memory we are
1510 reading. */
1511 return;
1512 }
1513
1514 /* Offset within shadow_contents. */
1515 if (bp_addr < memaddr)
1516 {
1517 /* Only copy the second part of the breakpoint. */
1518 bp_size -= memaddr - bp_addr;
1519 bptoffset = memaddr - bp_addr;
1520 bp_addr = memaddr;
1521 }
1522
1523 if (bp_addr + bp_size > memaddr + len)
1524 {
1525 /* Only copy the first part of the breakpoint. */
1526 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1527 }
1528
1529 if (readbuf != NULL)
1530 {
1531 /* Verify that the readbuf buffer does not overlap with the
1532 shadow_contents buffer. */
1533 gdb_assert (target_info->shadow_contents >= readbuf + len
1534 || readbuf >= (target_info->shadow_contents
1535 + target_info->shadow_len));
1536
1537 /* Update the read buffer with this inserted breakpoint's
1538 shadow. */
1539 memcpy (readbuf + bp_addr - memaddr,
1540 target_info->shadow_contents + bptoffset, bp_size);
1541 }
1542 else
1543 {
1544 const unsigned char *bp;
0d5ed153
MR
1545 CORE_ADDR addr = target_info->reqstd_address;
1546 int placed_size;
9d497a19
PA
1547
1548 /* Update the shadow with what we want to write to memory. */
1549 memcpy (target_info->shadow_contents + bptoffset,
1550 writebuf_org + bp_addr - memaddr, bp_size);
1551
1552 /* Determine appropriate breakpoint contents and size for this
1553 address. */
0d5ed153 1554 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1555
1556 /* Update the final write buffer with this inserted
1557 breakpoint's INSN. */
1558 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1559 }
1560}
1561
8defab1a 1562/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1563 by replacing any memory breakpoints with their shadowed contents.
1564
35c63cd8
JB
1565 If READBUF is not NULL, this buffer must not overlap with any of
1566 the breakpoint location's shadow_contents buffers. Otherwise,
1567 a failed assertion internal error will be raised.
1568
876fa593 1569 The range of shadowed area by each bp_location is:
f5336ca5
PA
1570 bl->address - bp_locations_placed_address_before_address_max
1571 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1572 The range we were requested to resolve shadows for is:
1573 memaddr ... memaddr + len
1574 Thus the safe cutoff boundaries for performance optimization are
35df4500 1575 memaddr + len <= (bl->address
f5336ca5 1576 - bp_locations_placed_address_before_address_max)
876fa593 1577 and:
f5336ca5 1578 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1579
8defab1a 1580void
f0ba3972
PA
1581breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1582 const gdb_byte *writebuf_org,
1583 ULONGEST memaddr, LONGEST len)
c906108c 1584{
4a64f543
MS
1585 /* Left boundary, right boundary and median element of our binary
1586 search. */
876fa593
JK
1587 unsigned bc_l, bc_r, bc;
1588
4a64f543
MS
1589 /* Find BC_L which is a leftmost element which may affect BUF
1590 content. It is safe to report lower value but a failure to
1591 report higher one. */
876fa593
JK
1592
1593 bc_l = 0;
f5336ca5 1594 bc_r = bp_locations_count;
876fa593
JK
1595 while (bc_l + 1 < bc_r)
1596 {
35df4500 1597 struct bp_location *bl;
876fa593
JK
1598
1599 bc = (bc_l + bc_r) / 2;
f5336ca5 1600 bl = bp_locations[bc];
876fa593 1601
4a64f543
MS
1602 /* Check first BL->ADDRESS will not overflow due to the added
1603 constant. Then advance the left boundary only if we are sure
1604 the BC element can in no way affect the BUF content (MEMADDR
1605 to MEMADDR + LEN range).
876fa593 1606
f5336ca5 1607 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1608 offset so that we cannot miss a breakpoint with its shadow
1609 range tail still reaching MEMADDR. */
c5aa993b 1610
f5336ca5 1611 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1612 >= bl->address)
f5336ca5 1613 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1614 <= memaddr))
876fa593
JK
1615 bc_l = bc;
1616 else
1617 bc_r = bc;
1618 }
1619
128070bb
PA
1620 /* Due to the binary search above, we need to make sure we pick the
1621 first location that's at BC_L's address. E.g., if there are
1622 multiple locations at the same address, BC_L may end up pointing
1623 at a duplicate location, and miss the "master"/"inserted"
1624 location. Say, given locations L1, L2 and L3 at addresses A and
1625 B:
1626
1627 L1@A, L2@A, L3@B, ...
1628
1629 BC_L could end up pointing at location L2, while the "master"
1630 location could be L1. Since the `loc->inserted' flag is only set
1631 on "master" locations, we'd forget to restore the shadow of L1
1632 and L2. */
1633 while (bc_l > 0
f5336ca5 1634 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1635 bc_l--;
1636
876fa593
JK
1637 /* Now do full processing of the found relevant range of elements. */
1638
f5336ca5 1639 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1640 {
f5336ca5 1641 struct bp_location *bl = bp_locations[bc];
876fa593 1642
35df4500
TJB
1643 /* bp_location array has BL->OWNER always non-NULL. */
1644 if (bl->owner->type == bp_none)
8a3fe4f8 1645 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1646 bl->owner->number);
ffce0d52 1647
e5dd4106 1648 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1649 content. */
1650
f5336ca5
PA
1651 if (bl->address >= bp_locations_placed_address_before_address_max
1652 && memaddr + len <= (bl->address
1653 - bp_locations_placed_address_before_address_max))
876fa593
JK
1654 break;
1655
35df4500 1656 if (!bp_location_has_shadow (bl))
c5aa993b 1657 continue;
6c95b8df 1658
9d497a19
PA
1659 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1660 memaddr, len, &bl->target_info, bl->gdbarch);
1661 }
c906108c 1662}
9d497a19 1663
c906108c 1664\f
c5aa993b 1665
b775012e
LM
1666/* Return true if BPT is either a software breakpoint or a hardware
1667 breakpoint. */
1668
1669int
1670is_breakpoint (const struct breakpoint *bpt)
1671{
1672 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1673 || bpt->type == bp_hardware_breakpoint
1674 || bpt->type == bp_dprintf);
b775012e
LM
1675}
1676
60e1c644
PA
1677/* Return true if BPT is of any hardware watchpoint kind. */
1678
a5606eee 1679static int
d77f58be 1680is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1681{
1682 return (bpt->type == bp_hardware_watchpoint
1683 || bpt->type == bp_read_watchpoint
1684 || bpt->type == bp_access_watchpoint);
1685}
7270d8f2 1686
60e1c644
PA
1687/* Return true if BPT is of any watchpoint kind, hardware or
1688 software. */
1689
3a5c3e22 1690int
d77f58be 1691is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1692{
1693 return (is_hardware_watchpoint (bpt)
1694 || bpt->type == bp_watchpoint);
1695}
1696
3a5c3e22
PA
1697/* Returns true if the current thread and its running state are safe
1698 to evaluate or update watchpoint B. Watchpoints on local
1699 expressions need to be evaluated in the context of the thread that
1700 was current when the watchpoint was created, and, that thread needs
1701 to be stopped to be able to select the correct frame context.
1702 Watchpoints on global expressions can be evaluated on any thread,
1703 and in any state. It is presently left to the target allowing
1704 memory accesses when threads are running. */
f6bc2008
PA
1705
1706static int
3a5c3e22 1707watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1708{
d0d8b0c6
JK
1709 return (b->base.pspace == current_program_space
1710 && (ptid_equal (b->watchpoint_thread, null_ptid)
1711 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1712 && !is_executing (inferior_ptid))));
f6bc2008
PA
1713}
1714
d0fb5eae
JK
1715/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1716 associated bp_watchpoint_scope breakpoint. */
1717
1718static void
3a5c3e22 1719watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1720{
3a5c3e22 1721 struct breakpoint *b = &w->base;
d0fb5eae
JK
1722
1723 if (b->related_breakpoint != b)
1724 {
1725 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1726 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1727 b->related_breakpoint->disposition = disp_del_at_next_stop;
1728 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1729 b->related_breakpoint = b;
1730 }
1731 b->disposition = disp_del_at_next_stop;
1732}
1733
bb9d5f81
PP
1734/* Extract a bitfield value from value VAL using the bit parameters contained in
1735 watchpoint W. */
1736
1737static struct value *
1738extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1739{
1740 struct value *bit_val;
1741
1742 if (val == NULL)
1743 return NULL;
1744
1745 bit_val = allocate_value (value_type (val));
1746
1747 unpack_value_bitfield (bit_val,
1748 w->val_bitpos,
1749 w->val_bitsize,
1750 value_contents_for_printing (val),
1751 value_offset (val),
1752 val);
1753
1754 return bit_val;
1755}
1756
c6d81124
PA
1757/* Allocate a dummy location and add it to B, which must be a software
1758 watchpoint. This is required because even if a software watchpoint
1759 is not watching any memory, bpstat_stop_status requires a location
1760 to be able to report stops. */
1761
1762static void
1763software_watchpoint_add_no_memory_location (struct breakpoint *b,
1764 struct program_space *pspace)
1765{
1766 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1767
1768 b->loc = allocate_bp_location (b);
1769 b->loc->pspace = pspace;
1770 b->loc->address = -1;
1771 b->loc->length = -1;
1772}
1773
1774/* Returns true if B is a software watchpoint that is not watching any
1775 memory (e.g., "watch $pc"). */
1776
1777static int
1778is_no_memory_software_watchpoint (struct breakpoint *b)
1779{
1780 return (b->type == bp_watchpoint
1781 && b->loc != NULL
1782 && b->loc->next == NULL
1783 && b->loc->address == -1
1784 && b->loc->length == -1);
1785}
1786
567e1b4e
JB
1787/* Assuming that B is a watchpoint:
1788 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1789 - Evaluate expression and store the result in B->val
567e1b4e
JB
1790 - Evaluate the condition if there is one, and store the result
1791 in b->loc->cond.
a5606eee
VP
1792 - Update the list of values that must be watched in B->loc.
1793
4a64f543
MS
1794 If the watchpoint disposition is disp_del_at_next_stop, then do
1795 nothing. If this is local watchpoint that is out of scope, delete
1796 it.
1797
1798 Even with `set breakpoint always-inserted on' the watchpoints are
1799 removed + inserted on each stop here. Normal breakpoints must
1800 never be removed because they might be missed by a running thread
1801 when debugging in non-stop mode. On the other hand, hardware
1802 watchpoints (is_hardware_watchpoint; processed here) are specific
1803 to each LWP since they are stored in each LWP's hardware debug
1804 registers. Therefore, such LWP must be stopped first in order to
1805 be able to modify its hardware watchpoints.
1806
1807 Hardware watchpoints must be reset exactly once after being
1808 presented to the user. It cannot be done sooner, because it would
1809 reset the data used to present the watchpoint hit to the user. And
1810 it must not be done later because it could display the same single
1811 watchpoint hit during multiple GDB stops. Note that the latter is
1812 relevant only to the hardware watchpoint types bp_read_watchpoint
1813 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1814 not user-visible - its hit is suppressed if the memory content has
1815 not changed.
1816
1817 The following constraints influence the location where we can reset
1818 hardware watchpoints:
1819
1820 * target_stopped_by_watchpoint and target_stopped_data_address are
1821 called several times when GDB stops.
1822
1823 [linux]
1824 * Multiple hardware watchpoints can be hit at the same time,
1825 causing GDB to stop. GDB only presents one hardware watchpoint
1826 hit at a time as the reason for stopping, and all the other hits
1827 are presented later, one after the other, each time the user
1828 requests the execution to be resumed. Execution is not resumed
1829 for the threads still having pending hit event stored in
1830 LWP_INFO->STATUS. While the watchpoint is already removed from
1831 the inferior on the first stop the thread hit event is kept being
1832 reported from its cached value by linux_nat_stopped_data_address
1833 until the real thread resume happens after the watchpoint gets
1834 presented and thus its LWP_INFO->STATUS gets reset.
1835
1836 Therefore the hardware watchpoint hit can get safely reset on the
1837 watchpoint removal from inferior. */
a79d3c27 1838
b40ce68a 1839static void
3a5c3e22 1840update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1841{
a5606eee 1842 int within_current_scope;
a5606eee 1843 struct frame_id saved_frame_id;
66076460 1844 int frame_saved;
a5606eee 1845
f6bc2008
PA
1846 /* If this is a local watchpoint, we only want to check if the
1847 watchpoint frame is in scope if the current thread is the thread
1848 that was used to create the watchpoint. */
1849 if (!watchpoint_in_thread_scope (b))
1850 return;
1851
3a5c3e22 1852 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1853 return;
1854
66076460 1855 frame_saved = 0;
a5606eee
VP
1856
1857 /* Determine if the watchpoint is within scope. */
1858 if (b->exp_valid_block == NULL)
1859 within_current_scope = 1;
1860 else
1861 {
b5db5dfc
UW
1862 struct frame_info *fi = get_current_frame ();
1863 struct gdbarch *frame_arch = get_frame_arch (fi);
1864 CORE_ADDR frame_pc = get_frame_pc (fi);
1865
c9cf6e20
MG
1866 /* If we're at a point where the stack has been destroyed
1867 (e.g. in a function epilogue), unwinding may not work
1868 properly. Do not attempt to recreate locations at this
b5db5dfc 1869 point. See similar comments in watchpoint_check. */
c9cf6e20 1870 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1871 return;
66076460
DJ
1872
1873 /* Save the current frame's ID so we can restore it after
1874 evaluating the watchpoint expression on its own frame. */
1875 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1876 took a frame parameter, so that we didn't have to change the
1877 selected frame. */
1878 frame_saved = 1;
1879 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1880
a5606eee
VP
1881 fi = frame_find_by_id (b->watchpoint_frame);
1882 within_current_scope = (fi != NULL);
1883 if (within_current_scope)
1884 select_frame (fi);
1885 }
1886
b5db5dfc
UW
1887 /* We don't free locations. They are stored in the bp_location array
1888 and update_global_location_list will eventually delete them and
1889 remove breakpoints if needed. */
3a5c3e22 1890 b->base.loc = NULL;
b5db5dfc 1891
a5606eee
VP
1892 if (within_current_scope && reparse)
1893 {
bbc13ae3 1894 const char *s;
d63d0675 1895
4d01a485 1896 b->exp.reset ();
d63d0675 1897 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1898 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1899 /* If the meaning of expression itself changed, the old value is
1900 no longer relevant. We don't want to report a watchpoint hit
1901 to the user when the old value and the new value may actually
1902 be completely different objects. */
1903 value_free (b->val);
fa4727a6
DJ
1904 b->val = NULL;
1905 b->val_valid = 0;
60e1c644
PA
1906
1907 /* Note that unlike with breakpoints, the watchpoint's condition
1908 expression is stored in the breakpoint object, not in the
1909 locations (re)created below. */
3a5c3e22 1910 if (b->base.cond_string != NULL)
60e1c644 1911 {
4d01a485 1912 b->cond_exp.reset ();
60e1c644 1913
3a5c3e22 1914 s = b->base.cond_string;
1bb9788d 1915 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1916 }
a5606eee 1917 }
a5606eee
VP
1918
1919 /* If we failed to parse the expression, for example because
1920 it refers to a global variable in a not-yet-loaded shared library,
1921 don't try to insert watchpoint. We don't automatically delete
1922 such watchpoint, though, since failure to parse expression
1923 is different from out-of-scope watchpoint. */
e8369a73 1924 if (!target_has_execution)
2d134ed3
PA
1925 {
1926 /* Without execution, memory can't change. No use to try and
1927 set watchpoint locations. The watchpoint will be reset when
1928 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1929 if (!can_use_hw_watchpoints)
1930 {
1931 if (b->base.ops->works_in_software_mode (&b->base))
1932 b->base.type = bp_watchpoint;
1933 else
638aa5a1
AB
1934 error (_("Can't set read/access watchpoint when "
1935 "hardware watchpoints are disabled."));
e8369a73 1936 }
2d134ed3
PA
1937 }
1938 else if (within_current_scope && b->exp)
a5606eee 1939 {
0cf6dd15 1940 int pc = 0;
fa4727a6 1941 struct value *val_chain, *v, *result, *next;
2d134ed3 1942 struct program_space *frame_pspace;
a5606eee 1943
4d01a485 1944 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1945
a5606eee
VP
1946 /* Avoid setting b->val if it's already set. The meaning of
1947 b->val is 'the last value' user saw, and we should update
1948 it only if we reported that last value to user. As it
9c06b0b4
TJB
1949 happens, the code that reports it updates b->val directly.
1950 We don't keep track of the memory value for masked
1951 watchpoints. */
3a5c3e22 1952 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1953 {
bb9d5f81
PP
1954 if (b->val_bitsize != 0)
1955 {
1956 v = extract_bitfield_from_watchpoint_value (b, v);
1957 if (v != NULL)
1958 release_value (v);
1959 }
fa4727a6
DJ
1960 b->val = v;
1961 b->val_valid = 1;
1962 }
a5606eee 1963
2d134ed3
PA
1964 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1965
a5606eee 1966 /* Look at each value on the value chain. */
9fa40276 1967 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1968 {
1969 /* If it's a memory location, and GDB actually needed
1970 its contents to evaluate the expression, then we
fa4727a6
DJ
1971 must watch it. If the first value returned is
1972 still lazy, that means an error occurred reading it;
1973 watch it anyway in case it becomes readable. */
a5606eee 1974 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1975 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1976 {
1977 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1978
a5606eee
VP
1979 /* We only watch structs and arrays if user asked
1980 for it explicitly, never if they just happen to
1981 appear in the middle of some value chain. */
fa4727a6 1982 if (v == result
a5606eee
VP
1983 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1984 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1985 {
1986 CORE_ADDR addr;
f486487f 1987 enum target_hw_bp_type type;
a5606eee 1988 struct bp_location *loc, **tmp;
bb9d5f81
PP
1989 int bitpos = 0, bitsize = 0;
1990
1991 if (value_bitsize (v) != 0)
1992 {
1993 /* Extract the bit parameters out from the bitfield
1994 sub-expression. */
1995 bitpos = value_bitpos (v);
1996 bitsize = value_bitsize (v);
1997 }
1998 else if (v == result && b->val_bitsize != 0)
1999 {
2000 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2001 lvalue whose bit parameters are saved in the fields
2002 VAL_BITPOS and VAL_BITSIZE. */
2003 bitpos = b->val_bitpos;
2004 bitsize = b->val_bitsize;
2005 }
a5606eee 2006
42ae5230 2007 addr = value_address (v);
bb9d5f81
PP
2008 if (bitsize != 0)
2009 {
2010 /* Skip the bytes that don't contain the bitfield. */
2011 addr += bitpos / 8;
2012 }
2013
a5606eee 2014 type = hw_write;
3a5c3e22 2015 if (b->base.type == bp_read_watchpoint)
a5606eee 2016 type = hw_read;
3a5c3e22 2017 else if (b->base.type == bp_access_watchpoint)
a5606eee 2018 type = hw_access;
3a5c3e22
PA
2019
2020 loc = allocate_bp_location (&b->base);
2021 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2022 ;
2023 *tmp = loc;
a6d9a66e 2024 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2025
2026 loc->pspace = frame_pspace;
a5606eee 2027 loc->address = addr;
bb9d5f81
PP
2028
2029 if (bitsize != 0)
2030 {
2031 /* Just cover the bytes that make up the bitfield. */
2032 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2033 }
2034 else
2035 loc->length = TYPE_LENGTH (value_type (v));
2036
a5606eee
VP
2037 loc->watchpoint_type = type;
2038 }
2039 }
9fa40276
TJB
2040 }
2041
2042 /* Change the type of breakpoint between hardware assisted or
2043 an ordinary watchpoint depending on the hardware support
2044 and free hardware slots. REPARSE is set when the inferior
2045 is started. */
a9634178 2046 if (reparse)
9fa40276 2047 {
e09342b5 2048 int reg_cnt;
9fa40276
TJB
2049 enum bp_loc_type loc_type;
2050 struct bp_location *bl;
a5606eee 2051
a9634178 2052 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2053
2054 if (reg_cnt)
9fa40276
TJB
2055 {
2056 int i, target_resources_ok, other_type_used;
a1398e0c 2057 enum bptype type;
9fa40276 2058
a9634178
TJB
2059 /* Use an exact watchpoint when there's only one memory region to be
2060 watched, and only one debug register is needed to watch it. */
2061 b->exact = target_exact_watchpoints && reg_cnt == 1;
2062
9fa40276 2063 /* We need to determine how many resources are already
e09342b5
TJB
2064 used for all other hardware watchpoints plus this one
2065 to see if we still have enough resources to also fit
a1398e0c
PA
2066 this watchpoint in as well. */
2067
2068 /* If this is a software watchpoint, we try to turn it
2069 to a hardware one -- count resources as if B was of
2070 hardware watchpoint type. */
2071 type = b->base.type;
2072 if (type == bp_watchpoint)
2073 type = bp_hardware_watchpoint;
2074
2075 /* This watchpoint may or may not have been placed on
2076 the list yet at this point (it won't be in the list
2077 if we're trying to create it for the first time,
2078 through watch_command), so always account for it
2079 manually. */
2080
2081 /* Count resources used by all watchpoints except B. */
2082 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2083
2084 /* Add in the resources needed for B. */
2085 i += hw_watchpoint_use_count (&b->base);
2086
2087 target_resources_ok
2088 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2089 if (target_resources_ok <= 0)
a9634178 2090 {
3a5c3e22 2091 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2092
2093 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2094 error (_("Target does not support this type of "
2095 "hardware watchpoint."));
9c06b0b4
TJB
2096 else if (target_resources_ok < 0 && !sw_mode)
2097 error (_("There are not enough available hardware "
2098 "resources for this watchpoint."));
a1398e0c
PA
2099
2100 /* Downgrade to software watchpoint. */
2101 b->base.type = bp_watchpoint;
2102 }
2103 else
2104 {
2105 /* If this was a software watchpoint, we've just
2106 found we have enough resources to turn it to a
2107 hardware watchpoint. Otherwise, this is a
2108 nop. */
2109 b->base.type = type;
a9634178 2110 }
9fa40276 2111 }
3a5c3e22 2112 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2113 {
2114 if (!can_use_hw_watchpoints)
2115 error (_("Can't set read/access watchpoint when "
2116 "hardware watchpoints are disabled."));
2117 else
2118 error (_("Expression cannot be implemented with "
2119 "read/access watchpoint."));
2120 }
9fa40276 2121 else
3a5c3e22 2122 b->base.type = bp_watchpoint;
9fa40276 2123
3a5c3e22 2124 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2125 : bp_loc_hardware_watchpoint);
3a5c3e22 2126 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2127 bl->loc_type = loc_type;
2128 }
2129
2130 for (v = val_chain; v; v = next)
2131 {
a5606eee
VP
2132 next = value_next (v);
2133 if (v != b->val)
2134 value_free (v);
2135 }
2136
c7437ca6
PA
2137 /* If a software watchpoint is not watching any memory, then the
2138 above left it without any location set up. But,
2139 bpstat_stop_status requires a location to be able to report
2140 stops, so make sure there's at least a dummy one. */
3a5c3e22 2141 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2142 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2143 }
2144 else if (!within_current_scope)
7270d8f2 2145 {
ac74f770
MS
2146 printf_filtered (_("\
2147Watchpoint %d deleted because the program has left the block\n\
2148in which its expression is valid.\n"),
3a5c3e22 2149 b->base.number);
d0fb5eae 2150 watchpoint_del_at_next_stop (b);
7270d8f2 2151 }
a5606eee
VP
2152
2153 /* Restore the selected frame. */
66076460
DJ
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2156}
2157
a5606eee 2158
74960c60 2159/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
74960c60 2164static int
35df4500 2165should_be_inserted (struct bp_location *bl)
74960c60 2166{
35df4500 2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2168 return 0;
2169
35df4500 2170 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2171 return 0;
2172
35df4500 2173 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2174 return 0;
2175
f8eba3c6
TT
2176 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2177 return 0;
2178
56710373
PA
2179 /* This is set for example, when we're attached to the parent of a
2180 vfork, and have detached from the child. The child is running
2181 free, and we expect it to do an exec or exit, at which point the
2182 OS makes the parent schedulable again (and the target reports
2183 that the vfork is done). Until the child is done with the shared
2184 memory region, do not insert breakpoints in the parent, otherwise
2185 the child could still trip on the parent's breakpoints. Since
2186 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2187 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2188 return 0;
2189
31e77af2 2190 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2191 location, except if the breakpoint is a single-step breakpoint,
2192 and the breakpoint's thread is the thread which is stepping past
2193 a breakpoint. */
31e77af2
PA
2194 if ((bl->loc_type == bp_loc_software_breakpoint
2195 || bl->loc_type == bp_loc_hardware_breakpoint)
2196 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2197 bl->address)
2198 /* The single-step breakpoint may be inserted at the location
2199 we're trying to step if the instruction branches to itself.
2200 However, the instruction won't be executed at all and it may
2201 break the semantics of the instruction, for example, the
2202 instruction is a conditional branch or updates some flags.
2203 We can't fix it unless GDB is able to emulate the instruction
2204 or switch to displaced stepping. */
2205 && !(bl->owner->type == bp_single_step
2206 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2207 {
2208 if (debug_infrun)
2209 {
2210 fprintf_unfiltered (gdb_stdlog,
2211 "infrun: skipping breakpoint: "
2212 "stepping past insn at: %s\n",
2213 paddress (bl->gdbarch, bl->address));
2214 }
2215 return 0;
2216 }
31e77af2 2217
963f9c80
PA
2218 /* Don't insert watchpoints if we're trying to step past the
2219 instruction that triggered one. */
2220 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2221 && stepping_past_nonsteppable_watchpoint ())
2222 {
2223 if (debug_infrun)
2224 {
2225 fprintf_unfiltered (gdb_stdlog,
2226 "infrun: stepping past non-steppable watchpoint. "
2227 "skipping watchpoint at %s:%d\n",
2228 paddress (bl->gdbarch, bl->address),
2229 bl->length);
2230 }
2231 return 0;
2232 }
2233
74960c60
VP
2234 return 1;
2235}
2236
934709f0
PW
2237/* Same as should_be_inserted but does the check assuming
2238 that the location is not duplicated. */
2239
2240static int
2241unduplicated_should_be_inserted (struct bp_location *bl)
2242{
2243 int result;
2244 const int save_duplicate = bl->duplicate;
2245
2246 bl->duplicate = 0;
2247 result = should_be_inserted (bl);
2248 bl->duplicate = save_duplicate;
2249 return result;
2250}
2251
b775012e
LM
2252/* Parses a conditional described by an expression COND into an
2253 agent expression bytecode suitable for evaluation
2254 by the bytecode interpreter. Return NULL if there was
2255 any error during parsing. */
2256
833177a4 2257static agent_expr_up
b775012e
LM
2258parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2259{
833177a4 2260 if (cond == NULL)
b775012e
LM
2261 return NULL;
2262
833177a4
PA
2263 agent_expr_up aexpr;
2264
b775012e
LM
2265 /* We don't want to stop processing, so catch any errors
2266 that may show up. */
492d29ea 2267 TRY
b775012e 2268 {
036e657b 2269 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2270 }
2271
492d29ea 2272 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2273 {
2274 /* If we got here, it means the condition could not be parsed to a valid
2275 bytecode expression and thus can't be evaluated on the target's side.
2276 It's no use iterating through the conditions. */
b775012e 2277 }
492d29ea 2278 END_CATCH
b775012e
LM
2279
2280 /* We have a valid agent expression. */
2281 return aexpr;
2282}
2283
2284/* Based on location BL, create a list of breakpoint conditions to be
2285 passed on to the target. If we have duplicated locations with different
2286 conditions, we will add such conditions to the list. The idea is that the
2287 target will evaluate the list of conditions and will only notify GDB when
2288 one of them is true. */
2289
2290static void
2291build_target_condition_list (struct bp_location *bl)
2292{
2293 struct bp_location **locp = NULL, **loc2p;
2294 int null_condition_or_parse_error = 0;
2295 int modified = bl->needs_update;
2296 struct bp_location *loc;
2297
8b4f3082 2298 /* Release conditions left over from a previous insert. */
3cde5c42 2299 bl->target_info.conditions.clear ();
8b4f3082 2300
b775012e
LM
2301 /* This is only meaningful if the target is
2302 evaluating conditions and if the user has
2303 opted for condition evaluation on the target's
2304 side. */
2305 if (gdb_evaluates_breakpoint_condition_p ()
2306 || !target_supports_evaluation_of_breakpoint_conditions ())
2307 return;
2308
2309 /* Do a first pass to check for locations with no assigned
2310 conditions or conditions that fail to parse to a valid agent expression
2311 bytecode. If any of these happen, then it's no use to send conditions
2312 to the target since this location will always trigger and generate a
2313 response back to GDB. */
2314 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2315 {
2316 loc = (*loc2p);
2317 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2318 {
2319 if (modified)
2320 {
b775012e
LM
2321 /* Re-parse the conditions since something changed. In that
2322 case we already freed the condition bytecodes (see
2323 force_breakpoint_reinsertion). We just
2324 need to parse the condition to bytecodes again. */
833177a4
PA
2325 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2326 loc->cond.get ());
b775012e
LM
2327 }
2328
2329 /* If we have a NULL bytecode expression, it means something
2330 went wrong or we have a null condition expression. */
2331 if (!loc->cond_bytecode)
2332 {
2333 null_condition_or_parse_error = 1;
2334 break;
2335 }
2336 }
2337 }
2338
2339 /* If any of these happened, it means we will have to evaluate the conditions
2340 for the location's address on gdb's side. It is no use keeping bytecodes
2341 for all the other duplicate locations, thus we free all of them here.
2342
2343 This is so we have a finer control over which locations' conditions are
2344 being evaluated by GDB or the remote stub. */
2345 if (null_condition_or_parse_error)
2346 {
2347 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2348 {
2349 loc = (*loc2p);
2350 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2351 {
2352 /* Only go as far as the first NULL bytecode is
2353 located. */
2354 if (!loc->cond_bytecode)
2355 return;
2356
833177a4 2357 loc->cond_bytecode.reset ();
b775012e
LM
2358 }
2359 }
2360 }
2361
2362 /* No NULL conditions or failed bytecode generation. Build a condition list
2363 for this location's address. */
2364 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2365 {
2366 loc = (*loc2p);
2367 if (loc->cond
2368 && is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->enable_state == bp_enabled
2371 && loc->enabled)
3cde5c42
PA
2372 {
2373 /* Add the condition to the vector. This will be used later
2374 to send the conditions to the target. */
2375 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2376 }
b775012e
LM
2377 }
2378
2379 return;
2380}
2381
d3ce09f5
SS
2382/* Parses a command described by string CMD into an agent expression
2383 bytecode suitable for evaluation by the bytecode interpreter.
2384 Return NULL if there was any error during parsing. */
2385
833177a4 2386static agent_expr_up
d3ce09f5
SS
2387parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2388{
2389 struct cleanup *old_cleanups = 0;
4d01a485 2390 struct expression **argvec;
bbc13ae3
KS
2391 const char *cmdrest;
2392 const char *format_start, *format_end;
d3ce09f5
SS
2393 struct format_piece *fpieces;
2394 int nargs;
2395 struct gdbarch *gdbarch = get_current_arch ();
2396
833177a4 2397 if (cmd == NULL)
d3ce09f5
SS
2398 return NULL;
2399
2400 cmdrest = cmd;
2401
2402 if (*cmdrest == ',')
2403 ++cmdrest;
bbc13ae3 2404 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2405
2406 if (*cmdrest++ != '"')
2407 error (_("No format string following the location"));
2408
2409 format_start = cmdrest;
2410
2411 fpieces = parse_format_string (&cmdrest);
2412
2413 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2414
2415 format_end = cmdrest;
2416
2417 if (*cmdrest++ != '"')
2418 error (_("Bad format string, non-terminated '\"'."));
2419
bbc13ae3 2420 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2421
2422 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2423 error (_("Invalid argument syntax"));
2424
2425 if (*cmdrest == ',')
2426 cmdrest++;
bbc13ae3 2427 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2428
2429 /* For each argument, make an expression. */
2430
2431 argvec = (struct expression **) alloca (strlen (cmd)
2432 * sizeof (struct expression *));
2433
2434 nargs = 0;
2435 while (*cmdrest != '\0')
2436 {
bbc13ae3 2437 const char *cmd1;
d3ce09f5
SS
2438
2439 cmd1 = cmdrest;
4d01a485
PA
2440 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2441 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2442 cmdrest = cmd1;
2443 if (*cmdrest == ',')
2444 ++cmdrest;
2445 }
2446
833177a4
PA
2447 agent_expr_up aexpr;
2448
d3ce09f5
SS
2449 /* We don't want to stop processing, so catch any errors
2450 that may show up. */
492d29ea 2451 TRY
d3ce09f5 2452 {
036e657b
JB
2453 aexpr = gen_printf (scope, gdbarch, 0, 0,
2454 format_start, format_end - format_start,
2455 fpieces, nargs, argvec);
d3ce09f5 2456 }
492d29ea 2457 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2458 {
2459 /* If we got here, it means the command could not be parsed to a valid
2460 bytecode expression and thus can't be evaluated on the target's side.
2461 It's no use iterating through the other commands. */
d3ce09f5 2462 }
492d29ea
PA
2463 END_CATCH
2464
2465 do_cleanups (old_cleanups);
d3ce09f5 2466
d3ce09f5
SS
2467 /* We have a valid agent expression, return it. */
2468 return aexpr;
2469}
2470
2471/* Based on location BL, create a list of breakpoint commands to be
2472 passed on to the target. If we have duplicated locations with
2473 different commands, we will add any such to the list. */
2474
2475static void
2476build_target_command_list (struct bp_location *bl)
2477{
2478 struct bp_location **locp = NULL, **loc2p;
2479 int null_command_or_parse_error = 0;
2480 int modified = bl->needs_update;
2481 struct bp_location *loc;
2482
3cde5c42
PA
2483 /* Clear commands left over from a previous insert. */
2484 bl->target_info.tcommands.clear ();
8b4f3082 2485
41fac0cf 2486 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2487 return;
2488
41fac0cf
PA
2489 /* For now, limit to agent-style dprintf breakpoints. */
2490 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2491 return;
2492
41fac0cf
PA
2493 /* For now, if we have any duplicate location that isn't a dprintf,
2494 don't install the target-side commands, as that would make the
2495 breakpoint not be reported to the core, and we'd lose
2496 control. */
2497 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2498 {
2499 loc = (*loc2p);
2500 if (is_breakpoint (loc->owner)
2501 && loc->pspace->num == bl->pspace->num
2502 && loc->owner->type != bp_dprintf)
2503 return;
2504 }
2505
d3ce09f5
SS
2506 /* Do a first pass to check for locations with no assigned
2507 conditions or conditions that fail to parse to a valid agent expression
2508 bytecode. If any of these happen, then it's no use to send conditions
2509 to the target since this location will always trigger and generate a
2510 response back to GDB. */
2511 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2512 {
2513 loc = (*loc2p);
2514 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2515 {
2516 if (modified)
2517 {
d3ce09f5
SS
2518 /* Re-parse the commands since something changed. In that
2519 case we already freed the command bytecodes (see
2520 force_breakpoint_reinsertion). We just
2521 need to parse the command to bytecodes again. */
833177a4
PA
2522 loc->cmd_bytecode
2523 = parse_cmd_to_aexpr (bl->address,
2524 loc->owner->extra_string);
d3ce09f5
SS
2525 }
2526
2527 /* If we have a NULL bytecode expression, it means something
2528 went wrong or we have a null command expression. */
2529 if (!loc->cmd_bytecode)
2530 {
2531 null_command_or_parse_error = 1;
2532 break;
2533 }
2534 }
2535 }
2536
2537 /* If anything failed, then we're not doing target-side commands,
2538 and so clean up. */
2539 if (null_command_or_parse_error)
2540 {
2541 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2542 {
2543 loc = (*loc2p);
2544 if (is_breakpoint (loc->owner)
2545 && loc->pspace->num == bl->pspace->num)
2546 {
2547 /* Only go as far as the first NULL bytecode is
2548 located. */
40fb6c5e 2549 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2550 return;
2551
833177a4 2552 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2553 }
2554 }
2555 }
2556
2557 /* No NULL commands or failed bytecode generation. Build a command list
2558 for this location's address. */
2559 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2560 {
2561 loc = (*loc2p);
2562 if (loc->owner->extra_string
2563 && is_breakpoint (loc->owner)
2564 && loc->pspace->num == bl->pspace->num
2565 && loc->owner->enable_state == bp_enabled
2566 && loc->enabled)
3cde5c42
PA
2567 {
2568 /* Add the command to the vector. This will be used later
2569 to send the commands to the target. */
2570 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2571 }
d3ce09f5
SS
2572 }
2573
2574 bl->target_info.persist = 0;
2575 /* Maybe flag this location as persistent. */
2576 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2577 bl->target_info.persist = 1;
2578}
2579
833b7ab5
YQ
2580/* Return the kind of breakpoint on address *ADDR. Get the kind
2581 of breakpoint according to ADDR except single-step breakpoint.
2582 Get the kind of single-step breakpoint according to the current
2583 registers state. */
cd6c3b4f
YQ
2584
2585static int
2586breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2587{
833b7ab5
YQ
2588 if (bl->owner->type == bp_single_step)
2589 {
2590 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2591 struct regcache *regcache;
2592
2593 regcache = get_thread_regcache (thr->ptid);
2594
2595 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2596 regcache, addr);
2597 }
2598 else
2599 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2600}
2601
35df4500
TJB
2602/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2603 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2604 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2605 Returns 0 for success, 1 if the bp_location type is not supported or
2606 -1 for failure.
879bfdc2 2607
4a64f543
MS
2608 NOTE drow/2003-09-09: This routine could be broken down to an
2609 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2610static int
35df4500 2611insert_bp_location (struct bp_location *bl,
26bb91f3 2612 struct ui_file *tmp_error_stream,
3fbb6ffa 2613 int *disabled_breaks,
dd61ec5c
MW
2614 int *hw_breakpoint_error,
2615 int *hw_bp_error_explained_already)
879bfdc2 2616{
0000e5cc
PA
2617 enum errors bp_err = GDB_NO_ERROR;
2618 const char *bp_err_message = NULL;
879bfdc2 2619
b775012e 2620 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2621 return 0;
2622
35c63cd8
JB
2623 /* Note we don't initialize bl->target_info, as that wipes out
2624 the breakpoint location's shadow_contents if the breakpoint
2625 is still inserted at that location. This in turn breaks
2626 target_read_memory which depends on these buffers when
2627 a memory read is requested at the breakpoint location:
2628 Once the target_info has been wiped, we fail to see that
2629 we have a breakpoint inserted at that address and thus
2630 read the breakpoint instead of returning the data saved in
2631 the breakpoint location's shadow contents. */
0d5ed153 2632 bl->target_info.reqstd_address = bl->address;
35df4500 2633 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2634 bl->target_info.length = bl->length;
8181d85f 2635
b775012e
LM
2636 /* When working with target-side conditions, we must pass all the conditions
2637 for the same breakpoint address down to the target since GDB will not
2638 insert those locations. With a list of breakpoint conditions, the target
2639 can decide when to stop and notify GDB. */
2640
2641 if (is_breakpoint (bl->owner))
2642 {
2643 build_target_condition_list (bl);
d3ce09f5
SS
2644 build_target_command_list (bl);
2645 /* Reset the modification marker. */
b775012e
LM
2646 bl->needs_update = 0;
2647 }
2648
35df4500
TJB
2649 if (bl->loc_type == bp_loc_software_breakpoint
2650 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2651 {
35df4500 2652 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2653 {
2654 /* If the explicitly specified breakpoint type
2655 is not hardware breakpoint, check the memory map to see
2656 if the breakpoint address is in read only memory or not.
4a64f543 2657
765dc015
VP
2658 Two important cases are:
2659 - location type is not hardware breakpoint, memory
2660 is readonly. We change the type of the location to
2661 hardware breakpoint.
4a64f543
MS
2662 - location type is hardware breakpoint, memory is
2663 read-write. This means we've previously made the
2664 location hardware one, but then the memory map changed,
2665 so we undo.
765dc015 2666
4a64f543
MS
2667 When breakpoints are removed, remove_breakpoints will use
2668 location types we've just set here, the only possible
2669 problem is that memory map has changed during running
2670 program, but it's not going to work anyway with current
2671 gdb. */
765dc015 2672 struct mem_region *mr
0d5ed153 2673 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2674
2675 if (mr)
2676 {
2677 if (automatic_hardware_breakpoints)
2678 {
765dc015
VP
2679 enum bp_loc_type new_type;
2680
2681 if (mr->attrib.mode != MEM_RW)
2682 new_type = bp_loc_hardware_breakpoint;
2683 else
2684 new_type = bp_loc_software_breakpoint;
2685
35df4500 2686 if (new_type != bl->loc_type)
765dc015
VP
2687 {
2688 static int said = 0;
cc59ec59 2689
35df4500 2690 bl->loc_type = new_type;
765dc015
VP
2691 if (!said)
2692 {
3e43a32a
MS
2693 fprintf_filtered (gdb_stdout,
2694 _("Note: automatically using "
2695 "hardware breakpoints for "
2696 "read-only addresses.\n"));
765dc015
VP
2697 said = 1;
2698 }
2699 }
2700 }
35df4500 2701 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2702 && mr->attrib.mode != MEM_RW)
2703 {
2704 fprintf_unfiltered (tmp_error_stream,
2705 _("Cannot insert breakpoint %d.\n"
2706 "Cannot set software breakpoint "
2707 "at read-only address %s\n"),
2708 bl->owner->number,
2709 paddress (bl->gdbarch, bl->address));
2710 return 1;
2711 }
765dc015
VP
2712 }
2713 }
2714
879bfdc2
DJ
2715 /* First check to see if we have to handle an overlay. */
2716 if (overlay_debugging == ovly_off
35df4500
TJB
2717 || bl->section == NULL
2718 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2719 {
2720 /* No overlay handling: just set the breakpoint. */
492d29ea 2721 TRY
dd61ec5c 2722 {
0000e5cc
PA
2723 int val;
2724
dd61ec5c 2725 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2726 if (val)
2727 bp_err = GENERIC_ERROR;
dd61ec5c 2728 }
492d29ea 2729 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2730 {
0000e5cc
PA
2731 bp_err = e.error;
2732 bp_err_message = e.message;
dd61ec5c 2733 }
492d29ea 2734 END_CATCH
879bfdc2
DJ
2735 }
2736 else
2737 {
4a64f543 2738 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2739 Shall we set a breakpoint at the LMA? */
2740 if (!overlay_events_enabled)
2741 {
2742 /* Yes -- overlay event support is not active,
2743 so we must try to set a breakpoint at the LMA.
2744 This will not work for a hardware breakpoint. */
35df4500 2745 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2746 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2747 bl->owner->number);
879bfdc2
DJ
2748 else
2749 {
35df4500
TJB
2750 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2751 bl->section);
879bfdc2 2752 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2753 bl->overlay_target_info = bl->target_info;
0d5ed153 2754 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2755
2756 /* No overlay handling: just set the breakpoint. */
492d29ea 2757 TRY
0000e5cc
PA
2758 {
2759 int val;
2760
579c6ad9 2761 bl->overlay_target_info.kind
cd6c3b4f
YQ
2762 = breakpoint_kind (bl, &addr);
2763 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2764 val = target_insert_breakpoint (bl->gdbarch,
2765 &bl->overlay_target_info);
2766 if (val)
2767 bp_err = GENERIC_ERROR;
2768 }
492d29ea 2769 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2770 {
2771 bp_err = e.error;
2772 bp_err_message = e.message;
2773 }
492d29ea 2774 END_CATCH
0000e5cc
PA
2775
2776 if (bp_err != GDB_NO_ERROR)
99361f52 2777 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2778 "Overlay breakpoint %d "
2779 "failed: in ROM?\n",
35df4500 2780 bl->owner->number);
879bfdc2
DJ
2781 }
2782 }
2783 /* Shall we set a breakpoint at the VMA? */
35df4500 2784 if (section_is_mapped (bl->section))
879bfdc2
DJ
2785 {
2786 /* Yes. This overlay section is mapped into memory. */
492d29ea 2787 TRY
dd61ec5c 2788 {
0000e5cc
PA
2789 int val;
2790
dd61ec5c 2791 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2792 if (val)
2793 bp_err = GENERIC_ERROR;
dd61ec5c 2794 }
492d29ea 2795 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2796 {
0000e5cc
PA
2797 bp_err = e.error;
2798 bp_err_message = e.message;
dd61ec5c 2799 }
492d29ea 2800 END_CATCH
879bfdc2
DJ
2801 }
2802 else
2803 {
2804 /* No. This breakpoint will not be inserted.
2805 No error, but do not mark the bp as 'inserted'. */
2806 return 0;
2807 }
2808 }
2809
0000e5cc 2810 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2811 {
2812 /* Can't set the breakpoint. */
0000e5cc
PA
2813
2814 /* In some cases, we might not be able to insert a
2815 breakpoint in a shared library that has already been
2816 removed, but we have not yet processed the shlib unload
2817 event. Unfortunately, some targets that implement
076855f9
PA
2818 breakpoint insertion themselves can't tell why the
2819 breakpoint insertion failed (e.g., the remote target
2820 doesn't define error codes), so we must treat generic
2821 errors as memory errors. */
0000e5cc 2822 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2823 && bl->loc_type == bp_loc_software_breakpoint
08351840 2824 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2825 || shared_objfile_contains_address_p (bl->pspace,
2826 bl->address)))
879bfdc2 2827 {
4a64f543 2828 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2829 bl->shlib_disabled = 1;
8d3788bd 2830 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2831 if (!*disabled_breaks)
2832 {
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n",
2835 bl->owner->number);
2836 fprintf_unfiltered (tmp_error_stream,
2837 "Temporarily disabling shared "
2838 "library breakpoints:\n");
2839 }
2840 *disabled_breaks = 1;
879bfdc2 2841 fprintf_unfiltered (tmp_error_stream,
35df4500 2842 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2843 return 0;
879bfdc2
DJ
2844 }
2845 else
879bfdc2 2846 {
35df4500 2847 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2848 {
0000e5cc
PA
2849 *hw_breakpoint_error = 1;
2850 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2851 fprintf_unfiltered (tmp_error_stream,
2852 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2853 bl->owner->number, bp_err_message ? ":" : ".\n");
2854 if (bp_err_message != NULL)
2855 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2856 }
2857 else
2858 {
0000e5cc
PA
2859 if (bp_err_message == NULL)
2860 {
2861 char *message
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
2864 struct cleanup *old_chain = make_cleanup (xfree, message);
2865
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Cannot insert breakpoint %d.\n"
2868 "%s\n",
2869 bl->owner->number, message);
2870 do_cleanups (old_chain);
2871 }
2872 else
2873 {
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d: %s\n",
2876 bl->owner->number,
2877 bp_err_message);
2878 }
879bfdc2 2879 }
0000e5cc 2880 return 1;
879bfdc2
DJ
2881
2882 }
2883 }
2884 else
35df4500 2885 bl->inserted = 1;
879bfdc2 2886
0000e5cc 2887 return 0;
879bfdc2
DJ
2888 }
2889
35df4500 2890 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2891 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2892 watchpoints. It's not clear that it's necessary... */
35df4500 2893 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2894 {
0000e5cc
PA
2895 int val;
2896
77b06cd7
TJB
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2901
2902 /* If trying to set a read-watchpoint, and it turns out it's not
2903 supported, try emulating one with an access watchpoint. */
35df4500 2904 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* But don't try to insert it, if there's already another
2909 hw_access location that would be considered a duplicate
2910 of this one. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2912 if (loc != bl
85d721b8 2913 && loc->watchpoint_type == hw_access
35df4500 2914 && watchpoint_locations_match (bl, loc))
85d721b8 2915 {
35df4500
TJB
2916 bl->duplicate = 1;
2917 bl->inserted = 1;
2918 bl->target_info = loc->target_info;
2919 bl->watchpoint_type = hw_access;
85d721b8
PA
2920 val = 0;
2921 break;
2922 }
2923
2924 if (val == 1)
2925 {
77b06cd7
TJB
2926 bl->watchpoint_type = hw_access;
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 if (val)
2930 /* Back to the original value. */
2931 bl->watchpoint_type = hw_read;
85d721b8
PA
2932 }
2933 }
2934
35df4500 2935 bl->inserted = (val == 0);
879bfdc2
DJ
2936 }
2937
35df4500 2938 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2939 {
0000e5cc
PA
2940 int val;
2941
77b06cd7
TJB
2942 gdb_assert (bl->owner->ops != NULL
2943 && bl->owner->ops->insert_location != NULL);
2944
2945 val = bl->owner->ops->insert_location (bl);
2946 if (val)
2947 {
2948 bl->owner->enable_state = bp_disabled;
2949
2950 if (val == 1)
2951 warning (_("\
2952Error inserting catchpoint %d: Your system does not support this type\n\
2953of catchpoint."), bl->owner->number);
2954 else
2955 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2956 }
2957
2958 bl->inserted = (val == 0);
1640b821
DJ
2959
2960 /* We've already printed an error message if there was a problem
2961 inserting this catchpoint, and we've disabled the catchpoint,
2962 so just return success. */
2963 return 0;
879bfdc2
DJ
2964 }
2965
2966 return 0;
2967}
2968
6c95b8df
PA
2969/* This function is called when program space PSPACE is about to be
2970 deleted. It takes care of updating breakpoints to not reference
2971 PSPACE anymore. */
2972
2973void
2974breakpoint_program_space_exit (struct program_space *pspace)
2975{
2976 struct breakpoint *b, *b_temp;
876fa593 2977 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2978
2979 /* Remove any breakpoint that was set through this program space. */
2980 ALL_BREAKPOINTS_SAFE (b, b_temp)
2981 {
2982 if (b->pspace == pspace)
2983 delete_breakpoint (b);
2984 }
2985
2986 /* Breakpoints set through other program spaces could have locations
2987 bound to PSPACE as well. Remove those. */
876fa593 2988 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2989 {
2990 struct bp_location *tmp;
2991
2992 if (loc->pspace == pspace)
2993 {
2bdf28a0 2994 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2995 if (loc->owner->loc == loc)
2996 loc->owner->loc = loc->next;
2997 else
2998 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2999 if (tmp->next == loc)
3000 {
3001 tmp->next = loc->next;
3002 break;
3003 }
3004 }
3005 }
3006
3007 /* Now update the global location list to permanently delete the
3008 removed locations above. */
44702360 3009 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3010}
3011
74960c60
VP
3012/* Make sure all breakpoints are inserted in inferior.
3013 Throws exception on any error.
3014 A breakpoint that is already inserted won't be inserted
3015 again, so calling this function twice is safe. */
3016void
3017insert_breakpoints (void)
3018{
3019 struct breakpoint *bpt;
3020
3021 ALL_BREAKPOINTS (bpt)
3022 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3023 {
3024 struct watchpoint *w = (struct watchpoint *) bpt;
3025
3026 update_watchpoint (w, 0 /* don't reparse. */);
3027 }
74960c60 3028
04086b45
PA
3029 /* Updating watchpoints creates new locations, so update the global
3030 location list. Explicitly tell ugll to insert locations and
3031 ignore breakpoints_always_inserted_mode. */
3032 update_global_location_list (UGLL_INSERT);
74960c60
VP
3033}
3034
20388dd6
YQ
3035/* Invoke CALLBACK for each of bp_location. */
3036
3037void
3038iterate_over_bp_locations (walk_bp_location_callback callback)
3039{
3040 struct bp_location *loc, **loc_tmp;
3041
3042 ALL_BP_LOCATIONS (loc, loc_tmp)
3043 {
3044 callback (loc, NULL);
3045 }
3046}
3047
b775012e
LM
3048/* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3051
3052static void
3053update_inserted_breakpoint_locations (void)
3054{
3055 struct bp_location *bl, **blp_tmp;
3056 int error_flag = 0;
3057 int val = 0;
3058 int disabled_breaks = 0;
3059 int hw_breakpoint_error = 0;
dd61ec5c 3060 int hw_bp_details_reported = 0;
b775012e 3061
d7e74731 3062 string_file tmp_error_stream;
b775012e
LM
3063
3064 /* Explicitly mark the warning -- this will only be printed if
3065 there was an error. */
d7e74731 3066 tmp_error_stream.puts ("Warning:\n");
b775012e 3067
d7e74731 3068 struct cleanup *cleanups = save_current_space_and_thread ();
b775012e
LM
3069
3070 ALL_BP_LOCATIONS (bl, blp_tmp)
3071 {
3072 /* We only want to update software breakpoints and hardware
3073 breakpoints. */
3074 if (!is_breakpoint (bl->owner))
3075 continue;
3076
3077 /* We only want to update locations that are already inserted
3078 and need updating. This is to avoid unwanted insertion during
3079 deletion of breakpoints. */
3080 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3081 continue;
3082
3083 switch_to_program_space_and_thread (bl->pspace);
3084
3085 /* For targets that support global breakpoints, there's no need
3086 to select an inferior to insert breakpoint to. In fact, even
3087 if we aren't attached to any process yet, we should still
3088 insert breakpoints. */
f5656ead 3089 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3090 && ptid_equal (inferior_ptid, null_ptid))
3091 continue;
3092
d7e74731 3093 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3094 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3095 if (val)
3096 error_flag = val;
3097 }
3098
3099 if (error_flag)
3100 {
3101 target_terminal_ours_for_output ();
3102 error_stream (tmp_error_stream);
3103 }
3104
3105 do_cleanups (cleanups);
3106}
3107
c30eee59 3108/* Used when starting or continuing the program. */
c906108c 3109
74960c60
VP
3110static void
3111insert_breakpoint_locations (void)
c906108c 3112{
a5606eee 3113 struct breakpoint *bpt;
35df4500 3114 struct bp_location *bl, **blp_tmp;
eacd795a 3115 int error_flag = 0;
c906108c 3116 int val = 0;
3fbb6ffa 3117 int disabled_breaks = 0;
81d0cc19 3118 int hw_breakpoint_error = 0;
dd61ec5c 3119 int hw_bp_error_explained_already = 0;
c906108c 3120
d7e74731
PA
3121 string_file tmp_error_stream;
3122
81d0cc19
GS
3123 /* Explicitly mark the warning -- this will only be printed if
3124 there was an error. */
d7e74731 3125 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3126
d7e74731 3127 struct cleanup *cleanups = save_current_space_and_thread ();
6c95b8df 3128
35df4500 3129 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3130 {
b775012e 3131 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3132 continue;
3133
4a64f543
MS
3134 /* There is no point inserting thread-specific breakpoints if
3135 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3136 has BL->OWNER always non-NULL. */
35df4500 3137 if (bl->owner->thread != -1
5d5658a1 3138 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3139 continue;
3140
35df4500 3141 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3142
3143 /* For targets that support global breakpoints, there's no need
3144 to select an inferior to insert breakpoint to. In fact, even
3145 if we aren't attached to any process yet, we should still
3146 insert breakpoints. */
f5656ead 3147 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3148 && ptid_equal (inferior_ptid, null_ptid))
3149 continue;
3150
d7e74731 3151 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3152 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3153 if (val)
eacd795a 3154 error_flag = val;
879bfdc2 3155 }
c906108c 3156
4a64f543
MS
3157 /* If we failed to insert all locations of a watchpoint, remove
3158 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3159 ALL_BREAKPOINTS (bpt)
3160 {
3161 int some_failed = 0;
3162 struct bp_location *loc;
3163
3164 if (!is_hardware_watchpoint (bpt))
3165 continue;
3166
d6b74ac4 3167 if (!breakpoint_enabled (bpt))
a5606eee 3168 continue;
74960c60
VP
3169
3170 if (bpt->disposition == disp_del_at_next_stop)
3171 continue;
a5606eee
VP
3172
3173 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3174 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3175 {
3176 some_failed = 1;
3177 break;
3178 }
3179 if (some_failed)
3180 {
3181 for (loc = bpt->loc; loc; loc = loc->next)
3182 if (loc->inserted)
834c0d03 3183 remove_breakpoint (loc);
a5606eee
VP
3184
3185 hw_breakpoint_error = 1;
d7e74731
PA
3186 tmp_error_stream.printf ("Could not insert "
3187 "hardware watchpoint %d.\n",
3188 bpt->number);
eacd795a 3189 error_flag = -1;
a5606eee
VP
3190 }
3191 }
3192
eacd795a 3193 if (error_flag)
81d0cc19
GS
3194 {
3195 /* If a hardware breakpoint or watchpoint was inserted, add a
3196 message about possibly exhausted resources. */
dd61ec5c 3197 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3198 {
d7e74731 3199 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3200You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3201 }
81d0cc19
GS
3202 target_terminal_ours_for_output ();
3203 error_stream (tmp_error_stream);
3204 }
f7545552
TT
3205
3206 do_cleanups (cleanups);
c906108c
SS
3207}
3208
c30eee59
TJB
3209/* Used when the program stops.
3210 Returns zero if successful, or non-zero if there was a problem
3211 removing a breakpoint location. */
3212
c906108c 3213int
fba45db2 3214remove_breakpoints (void)
c906108c 3215{
35df4500 3216 struct bp_location *bl, **blp_tmp;
3a1bae8e 3217 int val = 0;
c906108c 3218
35df4500 3219 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3220 {
1e4d1764 3221 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3222 val |= remove_breakpoint (bl);
c5aa993b 3223 }
3a1bae8e 3224 return val;
c906108c
SS
3225}
3226
49fa26b0
PA
3227/* When a thread exits, remove breakpoints that are related to
3228 that thread. */
3229
3230static void
3231remove_threaded_breakpoints (struct thread_info *tp, int silent)
3232{
3233 struct breakpoint *b, *b_tmp;
3234
3235 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3236 {
5d5658a1 3237 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3238 {
3239 b->disposition = disp_del_at_next_stop;
3240
3241 printf_filtered (_("\
43792cf0
PA
3242Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3243 b->number, print_thread_id (tp));
49fa26b0
PA
3244
3245 /* Hide it from the user. */
3246 b->number = 0;
3247 }
3248 }
3249}
3250
6c95b8df
PA
3251/* Remove breakpoints of process PID. */
3252
3253int
3254remove_breakpoints_pid (int pid)
3255{
35df4500 3256 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3257 int val;
3258 struct inferior *inf = find_inferior_pid (pid);
3259
35df4500 3260 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3261 {
35df4500 3262 if (bl->pspace != inf->pspace)
6c95b8df
PA
3263 continue;
3264
fc126975 3265 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3266 {
834c0d03 3267 val = remove_breakpoint (bl);
6c95b8df
PA
3268 if (val != 0)
3269 return val;
3270 }
3271 }
3272 return 0;
3273}
3274
c906108c 3275int
fba45db2 3276reattach_breakpoints (int pid)
c906108c 3277{
6c95b8df 3278 struct cleanup *old_chain;
35df4500 3279 struct bp_location *bl, **blp_tmp;
c906108c 3280 int val;
dd61ec5c 3281 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3282 struct inferior *inf;
3283 struct thread_info *tp;
3284
3285 tp = any_live_thread_of_process (pid);
3286 if (tp == NULL)
3287 return 1;
3288
3289 inf = find_inferior_pid (pid);
3290 old_chain = save_inferior_ptid ();
3291
3292 inferior_ptid = tp->ptid;
a4954f26 3293
d7e74731 3294 string_file tmp_error_stream;
c906108c 3295
35df4500 3296 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3297 {
35df4500 3298 if (bl->pspace != inf->pspace)
6c95b8df
PA
3299 continue;
3300
35df4500 3301 if (bl->inserted)
c5aa993b 3302 {
35df4500 3303 bl->inserted = 0;
d7e74731 3304 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3305 if (val != 0)
3306 {
ce696e05 3307 do_cleanups (old_chain);
c5aa993b
JM
3308 return val;
3309 }
3310 }
3311 }
ce696e05 3312 do_cleanups (old_chain);
c906108c
SS
3313 return 0;
3314}
3315
e58b0e63
PA
3316static int internal_breakpoint_number = -1;
3317
84f4c1fe
PM
3318/* Set the breakpoint number of B, depending on the value of INTERNAL.
3319 If INTERNAL is non-zero, the breakpoint number will be populated
3320 from internal_breakpoint_number and that variable decremented.
e5dd4106 3321 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3322 breakpoint_count and that value incremented. Internal breakpoints
3323 do not set the internal var bpnum. */
3324static void
3325set_breakpoint_number (int internal, struct breakpoint *b)
3326{
3327 if (internal)
3328 b->number = internal_breakpoint_number--;
3329 else
3330 {
3331 set_breakpoint_count (breakpoint_count + 1);
3332 b->number = breakpoint_count;
3333 }
3334}
3335
e62c965a 3336static struct breakpoint *
a6d9a66e 3337create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3338 CORE_ADDR address, enum bptype type,
c0a91b2b 3339 const struct breakpoint_ops *ops)
e62c965a 3340{
e62c965a
PP
3341 struct symtab_and_line sal;
3342 struct breakpoint *b;
3343
4a64f543 3344 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3345
3346 sal.pc = address;
3347 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3348 sal.pspace = current_program_space;
e62c965a 3349
06edf0c0 3350 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3351 b->number = internal_breakpoint_number--;
3352 b->disposition = disp_donttouch;
3353
3354 return b;
3355}
3356
17450429
PP
3357static const char *const longjmp_names[] =
3358 {
3359 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3360 };
3361#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3362
3363/* Per-objfile data private to breakpoint.c. */
3364struct breakpoint_objfile_data
3365{
3366 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3367 struct bound_minimal_symbol overlay_msym;
17450429
PP
3368
3369 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3370 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3371
28106bc2
SDJ
3372 /* True if we have looked for longjmp probes. */
3373 int longjmp_searched;
3374
3375 /* SystemTap probe points for longjmp (if any). */
3376 VEC (probe_p) *longjmp_probes;
3377
17450429 3378 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3379 struct bound_minimal_symbol terminate_msym;
17450429
PP
3380
3381 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3382 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3383
3384 /* True if we have looked for exception probes. */
3385 int exception_searched;
3386
3387 /* SystemTap probe points for unwinding (if any). */
3388 VEC (probe_p) *exception_probes;
17450429
PP
3389};
3390
3391static const struct objfile_data *breakpoint_objfile_key;
3392
3393/* Minimal symbol not found sentinel. */
3394static struct minimal_symbol msym_not_found;
3395
3396/* Returns TRUE if MSYM point to the "not found" sentinel. */
3397
3398static int
3399msym_not_found_p (const struct minimal_symbol *msym)
3400{
3401 return msym == &msym_not_found;
3402}
3403
3404/* Return per-objfile data needed by breakpoint.c.
3405 Allocate the data if necessary. */
3406
3407static struct breakpoint_objfile_data *
3408get_breakpoint_objfile_data (struct objfile *objfile)
3409{
3410 struct breakpoint_objfile_data *bp_objfile_data;
3411
9a3c8263
SM
3412 bp_objfile_data = ((struct breakpoint_objfile_data *)
3413 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3414 if (bp_objfile_data == NULL)
3415 {
8d749320
SM
3416 bp_objfile_data =
3417 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3418
3419 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3420 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3421 }
3422 return bp_objfile_data;
3423}
3424
28106bc2
SDJ
3425static void
3426free_breakpoint_probes (struct objfile *obj, void *data)
3427{
9a3c8263
SM
3428 struct breakpoint_objfile_data *bp_objfile_data
3429 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3430
3431 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3432 VEC_free (probe_p, bp_objfile_data->exception_probes);
3433}
3434
e62c965a 3435static void
af02033e 3436create_overlay_event_breakpoint (void)
e62c965a 3437{
69de3c6a 3438 struct objfile *objfile;
af02033e 3439 const char *const func_name = "_ovly_debug_event";
e62c965a 3440
69de3c6a
PP
3441 ALL_OBJFILES (objfile)
3442 {
3443 struct breakpoint *b;
17450429
PP
3444 struct breakpoint_objfile_data *bp_objfile_data;
3445 CORE_ADDR addr;
67994074 3446 struct explicit_location explicit_loc;
69de3c6a 3447
17450429
PP
3448 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3449
3b7344d5 3450 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3451 continue;
3452
3b7344d5 3453 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3454 {
3b7344d5 3455 struct bound_minimal_symbol m;
17450429
PP
3456
3457 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3458 if (m.minsym == NULL)
17450429
PP
3459 {
3460 /* Avoid future lookups in this objfile. */
3b7344d5 3461 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3462 continue;
3463 }
3464 bp_objfile_data->overlay_msym = m;
3465 }
e62c965a 3466
77e371c0 3467 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3468 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3469 bp_overlay_event,
3470 &internal_breakpoint_ops);
67994074
KS
3471 initialize_explicit_location (&explicit_loc);
3472 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3473 b->location = new_explicit_location (&explicit_loc);
e62c965a 3474
69de3c6a
PP
3475 if (overlay_debugging == ovly_auto)
3476 {
3477 b->enable_state = bp_enabled;
3478 overlay_events_enabled = 1;
3479 }
3480 else
3481 {
3482 b->enable_state = bp_disabled;
3483 overlay_events_enabled = 0;
3484 }
e62c965a 3485 }
e62c965a
PP
3486}
3487
0fd8e87f 3488static void
af02033e 3489create_longjmp_master_breakpoint (void)
0fd8e87f 3490{
6c95b8df 3491 struct program_space *pspace;
6c95b8df
PA
3492 struct cleanup *old_chain;
3493
3494 old_chain = save_current_program_space ();
0fd8e87f 3495
6c95b8df 3496 ALL_PSPACES (pspace)
af02033e
PP
3497 {
3498 struct objfile *objfile;
3499
3500 set_current_program_space (pspace);
3501
3502 ALL_OBJFILES (objfile)
0fd8e87f 3503 {
af02033e
PP
3504 int i;
3505 struct gdbarch *gdbarch;
17450429 3506 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3507
af02033e 3508 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3509
17450429
PP
3510 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3511
28106bc2
SDJ
3512 if (!bp_objfile_data->longjmp_searched)
3513 {
25f9533e
SDJ
3514 VEC (probe_p) *ret;
3515
3516 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3517 if (ret != NULL)
3518 {
3519 /* We are only interested in checking one element. */
3520 struct probe *p = VEC_index (probe_p, ret, 0);
3521
3522 if (!can_evaluate_probe_arguments (p))
3523 {
3524 /* We cannot use the probe interface here, because it does
3525 not know how to evaluate arguments. */
3526 VEC_free (probe_p, ret);
3527 ret = NULL;
3528 }
3529 }
3530 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3531 bp_objfile_data->longjmp_searched = 1;
3532 }
3533
3534 if (bp_objfile_data->longjmp_probes != NULL)
3535 {
3536 int i;
3537 struct probe *probe;
3538 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3539
3540 for (i = 0;
3541 VEC_iterate (probe_p,
3542 bp_objfile_data->longjmp_probes,
3543 i, probe);
3544 ++i)
3545 {
3546 struct breakpoint *b;
3547
729662a5
TT
3548 b = create_internal_breakpoint (gdbarch,
3549 get_probe_address (probe,
3550 objfile),
28106bc2
SDJ
3551 bp_longjmp_master,
3552 &internal_breakpoint_ops);
d28cd78a 3553 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3554 b->enable_state = bp_disabled;
3555 }
3556
3557 continue;
3558 }
3559
0569175e
TSD
3560 if (!gdbarch_get_longjmp_target_p (gdbarch))
3561 continue;
3562
17450429 3563 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3564 {
3565 struct breakpoint *b;
af02033e 3566 const char *func_name;
17450429 3567 CORE_ADDR addr;
67994074 3568 struct explicit_location explicit_loc;
6c95b8df 3569
3b7344d5 3570 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3571 continue;
0fd8e87f 3572
17450429 3573 func_name = longjmp_names[i];
3b7344d5 3574 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3575 {
3b7344d5 3576 struct bound_minimal_symbol m;
17450429
PP
3577
3578 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3579 if (m.minsym == NULL)
17450429
PP
3580 {
3581 /* Prevent future lookups in this objfile. */
3b7344d5 3582 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3583 continue;
3584 }
3585 bp_objfile_data->longjmp_msym[i] = m;
3586 }
3587
77e371c0 3588 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3589 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3590 &internal_breakpoint_ops);
67994074
KS
3591 initialize_explicit_location (&explicit_loc);
3592 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3593 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3594 b->enable_state = bp_disabled;
3595 }
0fd8e87f 3596 }
af02033e 3597 }
6c95b8df
PA
3598
3599 do_cleanups (old_chain);
0fd8e87f
UW
3600}
3601
af02033e 3602/* Create a master std::terminate breakpoint. */
aa7d318d 3603static void
af02033e 3604create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3605{
3606 struct program_space *pspace;
aa7d318d 3607 struct cleanup *old_chain;
af02033e 3608 const char *const func_name = "std::terminate()";
aa7d318d
TT
3609
3610 old_chain = save_current_program_space ();
3611
3612 ALL_PSPACES (pspace)
17450429
PP
3613 {
3614 struct objfile *objfile;
3615 CORE_ADDR addr;
3616
3617 set_current_program_space (pspace);
3618
aa7d318d
TT
3619 ALL_OBJFILES (objfile)
3620 {
3621 struct breakpoint *b;
17450429 3622 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3623 struct explicit_location explicit_loc;
aa7d318d 3624
17450429 3625 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3626
3b7344d5 3627 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3628 continue;
3629
3b7344d5 3630 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3631 {
3b7344d5 3632 struct bound_minimal_symbol m;
17450429
PP
3633
3634 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3635 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3636 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3637 {
3638 /* Prevent future lookups in this objfile. */
3b7344d5 3639 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3640 continue;
3641 }
3642 bp_objfile_data->terminate_msym = m;
3643 }
aa7d318d 3644
77e371c0 3645 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3646 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3647 bp_std_terminate_master,
3648 &internal_breakpoint_ops);
67994074
KS
3649 initialize_explicit_location (&explicit_loc);
3650 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3651 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3652 b->enable_state = bp_disabled;
3653 }
17450429
PP
3654 }
3655
aa7d318d
TT
3656 do_cleanups (old_chain);
3657}
3658
186c406b
TT
3659/* Install a master breakpoint on the unwinder's debug hook. */
3660
70221824 3661static void
186c406b
TT
3662create_exception_master_breakpoint (void)
3663{
3664 struct objfile *objfile;
17450429 3665 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3666
3667 ALL_OBJFILES (objfile)
3668 {
17450429
PP
3669 struct breakpoint *b;
3670 struct gdbarch *gdbarch;
3671 struct breakpoint_objfile_data *bp_objfile_data;
3672 CORE_ADDR addr;
67994074 3673 struct explicit_location explicit_loc;
17450429
PP
3674
3675 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3676
28106bc2
SDJ
3677 /* We prefer the SystemTap probe point if it exists. */
3678 if (!bp_objfile_data->exception_searched)
3679 {
25f9533e
SDJ
3680 VEC (probe_p) *ret;
3681
3682 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3683
3684 if (ret != NULL)
3685 {
3686 /* We are only interested in checking one element. */
3687 struct probe *p = VEC_index (probe_p, ret, 0);
3688
3689 if (!can_evaluate_probe_arguments (p))
3690 {
3691 /* We cannot use the probe interface here, because it does
3692 not know how to evaluate arguments. */
3693 VEC_free (probe_p, ret);
3694 ret = NULL;
3695 }
3696 }
3697 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3698 bp_objfile_data->exception_searched = 1;
3699 }
3700
3701 if (bp_objfile_data->exception_probes != NULL)
3702 {
3703 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3704 int i;
3705 struct probe *probe;
3706
3707 for (i = 0;
3708 VEC_iterate (probe_p,
3709 bp_objfile_data->exception_probes,
3710 i, probe);
3711 ++i)
3712 {
3713 struct breakpoint *b;
3714
729662a5
TT
3715 b = create_internal_breakpoint (gdbarch,
3716 get_probe_address (probe,
3717 objfile),
28106bc2
SDJ
3718 bp_exception_master,
3719 &internal_breakpoint_ops);
d28cd78a 3720 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3721 b->enable_state = bp_disabled;
3722 }
3723
3724 continue;
3725 }
3726
3727 /* Otherwise, try the hook function. */
3728
3b7344d5 3729 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3730 continue;
3731
3732 gdbarch = get_objfile_arch (objfile);
186c406b 3733
3b7344d5 3734 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3735 {
3b7344d5 3736 struct bound_minimal_symbol debug_hook;
186c406b 3737
17450429 3738 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3739 if (debug_hook.minsym == NULL)
17450429 3740 {
3b7344d5 3741 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3742 continue;
3743 }
3744
3745 bp_objfile_data->exception_msym = debug_hook;
186c406b 3746 }
17450429 3747
77e371c0 3748 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3749 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3750 &current_target);
06edf0c0
PA
3751 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3752 &internal_breakpoint_ops);
67994074
KS
3753 initialize_explicit_location (&explicit_loc);
3754 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3755 b->location = new_explicit_location (&explicit_loc);
17450429 3756 b->enable_state = bp_disabled;
186c406b 3757 }
186c406b
TT
3758}
3759
9ef9e6a6
KS
3760/* Does B have a location spec? */
3761
3762static int
3763breakpoint_event_location_empty_p (const struct breakpoint *b)
3764{
d28cd78a 3765 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3766}
3767
c906108c 3768void
fba45db2 3769update_breakpoints_after_exec (void)
c906108c 3770{
35df4500 3771 struct breakpoint *b, *b_tmp;
876fa593 3772 struct bp_location *bploc, **bplocp_tmp;
c906108c 3773
25b22b0a
PA
3774 /* We're about to delete breakpoints from GDB's lists. If the
3775 INSERTED flag is true, GDB will try to lift the breakpoints by
3776 writing the breakpoints' "shadow contents" back into memory. The
3777 "shadow contents" are NOT valid after an exec, so GDB should not
3778 do that. Instead, the target is responsible from marking
3779 breakpoints out as soon as it detects an exec. We don't do that
3780 here instead, because there may be other attempts to delete
3781 breakpoints after detecting an exec and before reaching here. */
876fa593 3782 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3783 if (bploc->pspace == current_program_space)
3784 gdb_assert (!bploc->inserted);
c906108c 3785
35df4500 3786 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3787 {
6c95b8df
PA
3788 if (b->pspace != current_program_space)
3789 continue;
3790
4a64f543 3791 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3792 if (b->type == bp_shlib_event)
3793 {
3794 delete_breakpoint (b);
3795 continue;
3796 }
c906108c 3797
4a64f543 3798 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3799 if (b->type == bp_jit_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
3804
1900040c 3805 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3806 as must overlay event and longjmp master breakpoints. */
3807 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3808 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3809 || b->type == bp_exception_master)
c4093a6a
JM
3810 {
3811 delete_breakpoint (b);
3812 continue;
3813 }
3814
4a64f543 3815 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3816 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
7c16b83e
PA
3822 /* Just like single-step breakpoints. */
3823 if (b->type == bp_single_step)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
611c83ae
PA
3829 /* Longjmp and longjmp-resume breakpoints are also meaningless
3830 after an exec. */
186c406b 3831 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3832 || b->type == bp_longjmp_call_dummy
186c406b 3833 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3834 {
3835 delete_breakpoint (b);
3836 continue;
3837 }
3838
ce78b96d
JB
3839 if (b->type == bp_catchpoint)
3840 {
3841 /* For now, none of the bp_catchpoint breakpoints need to
3842 do anything at this point. In the future, if some of
3843 the catchpoints need to something, we will need to add
3844 a new method, and call this method from here. */
3845 continue;
3846 }
3847
c5aa993b
JM
3848 /* bp_finish is a special case. The only way we ought to be able
3849 to see one of these when an exec() has happened, is if the user
3850 caught a vfork, and then said "finish". Ordinarily a finish just
3851 carries them to the call-site of the current callee, by setting
3852 a temporary bp there and resuming. But in this case, the finish
3853 will carry them entirely through the vfork & exec.
3854
3855 We don't want to allow a bp_finish to remain inserted now. But
3856 we can't safely delete it, 'cause finish_command has a handle to
3857 the bp on a bpstat, and will later want to delete it. There's a
3858 chance (and I've seen it happen) that if we delete the bp_finish
3859 here, that its storage will get reused by the time finish_command
3860 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3861 We really must allow finish_command to delete a bp_finish.
3862
e5dd4106 3863 In the absence of a general solution for the "how do we know
53a5351d
JM
3864 it's safe to delete something others may have handles to?"
3865 problem, what we'll do here is just uninsert the bp_finish, and
3866 let finish_command delete it.
3867
3868 (We know the bp_finish is "doomed" in the sense that it's
3869 momentary, and will be deleted as soon as finish_command sees
3870 the inferior stopped. So it doesn't matter that the bp's
3871 address is probably bogus in the new a.out, unlike e.g., the
3872 solib breakpoints.) */
c5aa993b 3873
c5aa993b
JM
3874 if (b->type == bp_finish)
3875 {
3876 continue;
3877 }
3878
3879 /* Without a symbolic address, we have little hope of the
3880 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3881 a.out. */
9ef9e6a6 3882 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3883 {
3884 delete_breakpoint (b);
3885 continue;
3886 }
c5aa993b 3887 }
c906108c
SS
3888}
3889
3890int
d80ee84f 3891detach_breakpoints (ptid_t ptid)
c906108c 3892{
35df4500 3893 struct bp_location *bl, **blp_tmp;
3a1bae8e 3894 int val = 0;
ce696e05 3895 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3896 struct inferior *inf = current_inferior ();
c5aa993b 3897
dfd4cc63 3898 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3899 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3900
6c95b8df 3901 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3902 inferior_ptid = ptid;
35df4500 3903 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3904 {
35df4500 3905 if (bl->pspace != inf->pspace)
6c95b8df
PA
3906 continue;
3907
bd9673a4
PW
3908 /* This function must physically remove breakpoints locations
3909 from the specified ptid, without modifying the breakpoint
3910 package's state. Locations of type bp_loc_other are only
3911 maintained at GDB side. So, there is no need to remove
3912 these bp_loc_other locations. Moreover, removing these
3913 would modify the breakpoint package's state. */
3914 if (bl->loc_type == bp_loc_other)
3915 continue;
3916
35df4500 3917 if (bl->inserted)
b2b6a7da 3918 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3919 }
d03285ec 3920
ce696e05 3921 do_cleanups (old_chain);
3a1bae8e 3922 return val;
c906108c
SS
3923}
3924
35df4500 3925/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3926 Note that this is used to detach breakpoints from a child fork.
3927 When we get here, the child isn't in the inferior list, and neither
3928 do we have objects to represent its address space --- we should
35df4500 3929 *not* look at bl->pspace->aspace here. */
6c95b8df 3930
c906108c 3931static int
b2b6a7da 3932remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3933{
3934 int val;
c5aa993b 3935
35df4500
TJB
3936 /* BL is never in moribund_locations by our callers. */
3937 gdb_assert (bl->owner != NULL);
2bdf28a0 3938
74960c60
VP
3939 /* The type of none suggests that owner is actually deleted.
3940 This should not ever happen. */
35df4500 3941 gdb_assert (bl->owner->type != bp_none);
0bde7532 3942
35df4500
TJB
3943 if (bl->loc_type == bp_loc_software_breakpoint
3944 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3945 {
c02f5703
MS
3946 /* "Normal" instruction breakpoint: either the standard
3947 trap-instruction bp (bp_breakpoint), or a
3948 bp_hardware_breakpoint. */
3949
3950 /* First check to see if we have to handle an overlay. */
3951 if (overlay_debugging == ovly_off
35df4500
TJB
3952 || bl->section == NULL
3953 || !(section_is_overlay (bl->section)))
c02f5703
MS
3954 {
3955 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3956
3957 /* If we're trying to uninsert a memory breakpoint that we
3958 know is set in a dynamic object that is marked
3959 shlib_disabled, then either the dynamic object was
3960 removed with "remove-symbol-file" or with
3961 "nosharedlibrary". In the former case, we don't know
3962 whether another dynamic object might have loaded over the
3963 breakpoint's address -- the user might well let us know
3964 about it next with add-symbol-file (the whole point of
d03de421 3965 add-symbol-file is letting the user manually maintain a
08351840
PA
3966 list of dynamically loaded objects). If we have the
3967 breakpoint's shadow memory, that is, this is a software
3968 breakpoint managed by GDB, check whether the breakpoint
3969 is still inserted in memory, to avoid overwriting wrong
3970 code with stale saved shadow contents. Note that HW
3971 breakpoints don't have shadow memory, as they're
3972 implemented using a mechanism that is not dependent on
3973 being able to modify the target's memory, and as such
3974 they should always be removed. */
3975 if (bl->shlib_disabled
3976 && bl->target_info.shadow_len != 0
3977 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3978 val = 0;
3979 else
73971819 3980 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3981 }
c906108c
SS
3982 else
3983 {
4a64f543 3984 /* This breakpoint is in an overlay section.
c02f5703
MS
3985 Did we set a breakpoint at the LMA? */
3986 if (!overlay_events_enabled)
3987 {
3988 /* Yes -- overlay event support is not active, so we
3989 should have set a breakpoint at the LMA. Remove it.
3990 */
c02f5703
MS
3991 /* Ignore any failures: if the LMA is in ROM, we will
3992 have already warned when we failed to insert it. */
35df4500
TJB
3993 if (bl->loc_type == bp_loc_hardware_breakpoint)
3994 target_remove_hw_breakpoint (bl->gdbarch,
3995 &bl->overlay_target_info);
c02f5703 3996 else
35df4500 3997 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3998 &bl->overlay_target_info,
3999 reason);
c02f5703
MS
4000 }
4001 /* Did we set a breakpoint at the VMA?
4002 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4003 if (bl->inserted)
c906108c 4004 {
c02f5703
MS
4005 /* Yes -- remove it. Previously we did not bother to
4006 remove the breakpoint if the section had been
4007 unmapped, but let's not rely on that being safe. We
4008 don't know what the overlay manager might do. */
aa67235e
UW
4009
4010 /* However, we should remove *software* breakpoints only
4011 if the section is still mapped, or else we overwrite
4012 wrong code with the saved shadow contents. */
348d480f
PA
4013 if (bl->loc_type == bp_loc_hardware_breakpoint
4014 || section_is_mapped (bl->section))
73971819 4015 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4016 else
4017 val = 0;
c906108c 4018 }
c02f5703
MS
4019 else
4020 {
4021 /* No -- not inserted, so no need to remove. No error. */
4022 val = 0;
4023 }
c906108c 4024 }
879d1e6b 4025
08351840
PA
4026 /* In some cases, we might not be able to remove a breakpoint in
4027 a shared library that has already been removed, but we have
4028 not yet processed the shlib unload event. Similarly for an
4029 unloaded add-symbol-file object - the user might not yet have
4030 had the chance to remove-symbol-file it. shlib_disabled will
4031 be set if the library/object has already been removed, but
4032 the breakpoint hasn't been uninserted yet, e.g., after
4033 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4034 always-inserted mode. */
076855f9 4035 if (val
08351840
PA
4036 && (bl->loc_type == bp_loc_software_breakpoint
4037 && (bl->shlib_disabled
4038 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4039 || shared_objfile_contains_address_p (bl->pspace,
4040 bl->address))))
879d1e6b
UW
4041 val = 0;
4042
c906108c
SS
4043 if (val)
4044 return val;
b2b6a7da 4045 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4046 }
35df4500 4047 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4048 {
77b06cd7
TJB
4049 gdb_assert (bl->owner->ops != NULL
4050 && bl->owner->ops->remove_location != NULL);
4051
b2b6a7da 4052 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4053 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4054
c906108c 4055 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4056 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4057 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4058 bl->owner->number);
c906108c 4059 }
35df4500
TJB
4060 else if (bl->owner->type == bp_catchpoint
4061 && breakpoint_enabled (bl->owner)
4062 && !bl->duplicate)
ce78b96d 4063 {
77b06cd7
TJB
4064 gdb_assert (bl->owner->ops != NULL
4065 && bl->owner->ops->remove_location != NULL);
ce78b96d 4066
73971819 4067 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4068 if (val)
4069 return val;
77b06cd7 4070
b2b6a7da 4071 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4072 }
c906108c
SS
4073
4074 return 0;
4075}
4076
6c95b8df 4077static int
834c0d03 4078remove_breakpoint (struct bp_location *bl)
6c95b8df
PA
4079{
4080 int ret;
4081 struct cleanup *old_chain;
4082
35df4500
TJB
4083 /* BL is never in moribund_locations by our callers. */
4084 gdb_assert (bl->owner != NULL);
2bdf28a0 4085
6c95b8df
PA
4086 /* The type of none suggests that owner is actually deleted.
4087 This should not ever happen. */
35df4500 4088 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4089
4090 old_chain = save_current_space_and_thread ();
4091
35df4500 4092 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4093
b2b6a7da 4094 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4095
4096 do_cleanups (old_chain);
4097 return ret;
4098}
4099
c906108c
SS
4100/* Clear the "inserted" flag in all breakpoints. */
4101
25b22b0a 4102void
fba45db2 4103mark_breakpoints_out (void)
c906108c 4104{
35df4500 4105 struct bp_location *bl, **blp_tmp;
c906108c 4106
35df4500 4107 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4108 if (bl->pspace == current_program_space)
35df4500 4109 bl->inserted = 0;
c906108c
SS
4110}
4111
53a5351d
JM
4112/* Clear the "inserted" flag in all breakpoints and delete any
4113 breakpoints which should go away between runs of the program.
c906108c
SS
4114
4115 Plus other such housekeeping that has to be done for breakpoints
4116 between runs.
4117
53a5351d
JM
4118 Note: this function gets called at the end of a run (by
4119 generic_mourn_inferior) and when a run begins (by
4a64f543 4120 init_wait_for_inferior). */
c906108c
SS
4121
4122
4123
4124void
fba45db2 4125breakpoint_init_inferior (enum inf_context context)
c906108c 4126{
35df4500 4127 struct breakpoint *b, *b_tmp;
870f88f7 4128 struct bp_location *bl;
1c5cfe86 4129 int ix;
6c95b8df 4130 struct program_space *pspace = current_program_space;
c906108c 4131
50c71eaf
PA
4132 /* If breakpoint locations are shared across processes, then there's
4133 nothing to do. */
f5656ead 4134 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4135 return;
4136
1a853c52 4137 mark_breakpoints_out ();
075f6582 4138
35df4500 4139 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4140 {
6c95b8df
PA
4141 if (b->loc && b->loc->pspace != pspace)
4142 continue;
4143
c5aa993b
JM
4144 switch (b->type)
4145 {
4146 case bp_call_dummy:
e2e4d78b 4147 case bp_longjmp_call_dummy:
c906108c 4148
c5aa993b 4149 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4150 cause problems when the inferior is rerun, so we better get
4151 rid of it. */
4152
4153 case bp_watchpoint_scope:
4154
4155 /* Also get rid of scope breakpoints. */
4156
4157 case bp_shlib_event:
4158
4159 /* Also remove solib event breakpoints. Their addresses may
4160 have changed since the last time we ran the program.
4161 Actually we may now be debugging against different target;
4162 and so the solib backend that installed this breakpoint may
4163 not be used in by the target. E.g.,
4164
4165 (gdb) file prog-linux
4166 (gdb) run # native linux target
4167 ...
4168 (gdb) kill
4169 (gdb) file prog-win.exe
4170 (gdb) tar rem :9999 # remote Windows gdbserver.
4171 */
c906108c 4172
f59f708a
PA
4173 case bp_step_resume:
4174
4175 /* Also remove step-resume breakpoints. */
4176
7c16b83e
PA
4177 case bp_single_step:
4178
4179 /* Also remove single-step breakpoints. */
4180
c5aa993b
JM
4181 delete_breakpoint (b);
4182 break;
c906108c 4183
c5aa993b
JM
4184 case bp_watchpoint:
4185 case bp_hardware_watchpoint:
4186 case bp_read_watchpoint:
4187 case bp_access_watchpoint:
3a5c3e22
PA
4188 {
4189 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4190
3a5c3e22
PA
4191 /* Likewise for watchpoints on local expressions. */
4192 if (w->exp_valid_block != NULL)
4193 delete_breakpoint (b);
63000888 4194 else
3a5c3e22 4195 {
63000888
PA
4196 /* Get rid of existing locations, which are no longer
4197 valid. New ones will be created in
4198 update_watchpoint, when the inferior is restarted.
4199 The next update_global_location_list call will
4200 garbage collect them. */
4201 b->loc = NULL;
4202
4203 if (context == inf_starting)
4204 {
4205 /* Reset val field to force reread of starting value in
4206 insert_breakpoints. */
4207 if (w->val)
4208 value_free (w->val);
4209 w->val = NULL;
4210 w->val_valid = 0;
4211 }
4212 }
3a5c3e22 4213 }
c5aa993b
JM
4214 break;
4215 default:
c5aa993b
JM
4216 break;
4217 }
4218 }
1c5cfe86
PA
4219
4220 /* Get rid of the moribund locations. */
35df4500
TJB
4221 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4222 decref_bp_location (&bl);
1c5cfe86 4223 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4224}
4225
6c95b8df
PA
4226/* These functions concern about actual breakpoints inserted in the
4227 target --- to e.g. check if we need to do decr_pc adjustment or if
4228 we need to hop over the bkpt --- so we check for address space
4229 match, not program space. */
4230
c2c6d25f
JM
4231/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4232 exists at PC. It returns ordinary_breakpoint_here if it's an
4233 ordinary breakpoint, or permanent_breakpoint_here if it's a
4234 permanent breakpoint.
4235 - When continuing from a location with an ordinary breakpoint, we
4236 actually single step once before calling insert_breakpoints.
e5dd4106 4237 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4238 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4239 the target, to advance the PC past the breakpoint. */
c906108c 4240
c2c6d25f 4241enum breakpoint_here
6c95b8df 4242breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4243{
35df4500 4244 struct bp_location *bl, **blp_tmp;
c2c6d25f 4245 int any_breakpoint_here = 0;
c906108c 4246
35df4500 4247 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4248 {
35df4500
TJB
4249 if (bl->loc_type != bp_loc_software_breakpoint
4250 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4251 continue;
4252
f1310107 4253 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4254 if ((breakpoint_enabled (bl->owner)
1a853c52 4255 || bl->permanent)
f1310107 4256 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4257 {
4258 if (overlay_debugging
35df4500
TJB
4259 && section_is_overlay (bl->section)
4260 && !section_is_mapped (bl->section))
075f6582 4261 continue; /* unmapped overlay -- can't be a match */
1a853c52 4262 else if (bl->permanent)
075f6582
DJ
4263 return permanent_breakpoint_here;
4264 else
4265 any_breakpoint_here = 1;
4266 }
4267 }
c906108c 4268
f486487f 4269 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4270}
4271
d35ae833
PA
4272/* See breakpoint.h. */
4273
4274int
4275breakpoint_in_range_p (struct address_space *aspace,
4276 CORE_ADDR addr, ULONGEST len)
4277{
4278 struct bp_location *bl, **blp_tmp;
4279
4280 ALL_BP_LOCATIONS (bl, blp_tmp)
4281 {
4282 if (bl->loc_type != bp_loc_software_breakpoint
4283 && bl->loc_type != bp_loc_hardware_breakpoint)
4284 continue;
4285
4286 if ((breakpoint_enabled (bl->owner)
4287 || bl->permanent)
4288 && breakpoint_location_address_range_overlap (bl, aspace,
4289 addr, len))
4290 {
4291 if (overlay_debugging
4292 && section_is_overlay (bl->section)
4293 && !section_is_mapped (bl->section))
4294 {
4295 /* Unmapped overlay -- can't be a match. */
4296 continue;
4297 }
4298
4299 return 1;
4300 }
4301 }
4302
4303 return 0;
4304}
4305
1c5cfe86
PA
4306/* Return true if there's a moribund breakpoint at PC. */
4307
4308int
6c95b8df 4309moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4310{
4311 struct bp_location *loc;
4312 int ix;
4313
4314 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4315 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4316 return 1;
4317
4318 return 0;
4319}
c2c6d25f 4320
f7ce857f
PA
4321/* Returns non-zero iff BL is inserted at PC, in address space
4322 ASPACE. */
4323
4324static int
4325bp_location_inserted_here_p (struct bp_location *bl,
4326 struct address_space *aspace, CORE_ADDR pc)
4327{
4328 if (bl->inserted
4329 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4330 aspace, pc))
4331 {
4332 if (overlay_debugging
4333 && section_is_overlay (bl->section)
4334 && !section_is_mapped (bl->section))
4335 return 0; /* unmapped overlay -- can't be a match */
4336 else
4337 return 1;
4338 }
4339 return 0;
4340}
4341
a1fd2fa5 4342/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4343
4344int
a1fd2fa5 4345breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4346{
f7ce857f 4347 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4348
f7ce857f 4349 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4350 {
f7ce857f
PA
4351 struct bp_location *bl = *blp;
4352
35df4500
TJB
4353 if (bl->loc_type != bp_loc_software_breakpoint
4354 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4355 continue;
4356
f7ce857f
PA
4357 if (bp_location_inserted_here_p (bl, aspace, pc))
4358 return 1;
c5aa993b 4359 }
c36b740a
VP
4360 return 0;
4361}
4362
a1fd2fa5
PA
4363/* This function returns non-zero iff there is a software breakpoint
4364 inserted at PC. */
c36b740a
VP
4365
4366int
a1fd2fa5
PA
4367software_breakpoint_inserted_here_p (struct address_space *aspace,
4368 CORE_ADDR pc)
4fa8626c 4369{
f7ce857f 4370 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4371
f7ce857f 4372 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4373 {
f7ce857f
PA
4374 struct bp_location *bl = *blp;
4375
35df4500 4376 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4377 continue;
4378
f7ce857f
PA
4379 if (bp_location_inserted_here_p (bl, aspace, pc))
4380 return 1;
4fa8626c
DJ
4381 }
4382
4383 return 0;
9c02b525
PA
4384}
4385
4386/* See breakpoint.h. */
4387
4388int
4389hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4390 CORE_ADDR pc)
4391{
4392 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4393
4394 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4395 {
4396 struct bp_location *bl = *blp;
4397
4398 if (bl->loc_type != bp_loc_hardware_breakpoint)
4399 continue;
4400
4401 if (bp_location_inserted_here_p (bl, aspace, pc))
4402 return 1;
4403 }
4404
4405 return 0;
4fa8626c
DJ
4406}
4407
9093389c
PA
4408int
4409hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4410 CORE_ADDR addr, ULONGEST len)
4411{
4412 struct breakpoint *bpt;
4413
4414 ALL_BREAKPOINTS (bpt)
4415 {
4416 struct bp_location *loc;
4417
4418 if (bpt->type != bp_hardware_watchpoint
4419 && bpt->type != bp_access_watchpoint)
4420 continue;
4421
4422 if (!breakpoint_enabled (bpt))
4423 continue;
4424
4425 for (loc = bpt->loc; loc; loc = loc->next)
4426 if (loc->pspace->aspace == aspace && loc->inserted)
4427 {
4428 CORE_ADDR l, h;
4429
4430 /* Check for intersection. */
768adc05
PA
4431 l = std::max<CORE_ADDR> (loc->address, addr);
4432 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4433 if (l < h)
4434 return 1;
4435 }
4436 }
4437 return 0;
4438}
c906108c 4439\f
c5aa993b 4440
c906108c
SS
4441/* bpstat stuff. External routines' interfaces are documented
4442 in breakpoint.h. */
4443
4444int
c326b90e 4445is_catchpoint (struct breakpoint *ep)
c906108c 4446{
533be4dd 4447 return (ep->type == bp_catchpoint);
c906108c
SS
4448}
4449
f431efe5
PA
4450/* Frees any storage that is part of a bpstat. Does not walk the
4451 'next' chain. */
4452
4453static void
198757a8
VP
4454bpstat_free (bpstat bs)
4455{
4456 if (bs->old_val != NULL)
4457 value_free (bs->old_val);
9add0f1b 4458 decref_counted_command_line (&bs->commands);
f431efe5 4459 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4460 xfree (bs);
4461}
4462
c906108c
SS
4463/* Clear a bpstat so that it says we are not at any breakpoint.
4464 Also free any storage that is part of a bpstat. */
4465
4466void
fba45db2 4467bpstat_clear (bpstat *bsp)
c906108c
SS
4468{
4469 bpstat p;
4470 bpstat q;
4471
4472 if (bsp == 0)
4473 return;
4474 p = *bsp;
4475 while (p != NULL)
4476 {
4477 q = p->next;
198757a8 4478 bpstat_free (p);
c906108c
SS
4479 p = q;
4480 }
4481 *bsp = NULL;
4482}
4483
4484/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4485 is part of the bpstat is copied as well. */
4486
4487bpstat
fba45db2 4488bpstat_copy (bpstat bs)
c906108c
SS
4489{
4490 bpstat p = NULL;
4491 bpstat tmp;
4492 bpstat retval = NULL;
4493
4494 if (bs == NULL)
4495 return bs;
4496
4497 for (; bs != NULL; bs = bs->next)
4498 {
4499 tmp = (bpstat) xmalloc (sizeof (*tmp));
4500 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4501 incref_counted_command_line (tmp->commands);
f431efe5 4502 incref_bp_location (tmp->bp_location_at);
31cc81e9 4503 if (bs->old_val != NULL)
3c3185ac
JK
4504 {
4505 tmp->old_val = value_copy (bs->old_val);
4506 release_value (tmp->old_val);
4507 }
31cc81e9 4508
c906108c
SS
4509 if (p == NULL)
4510 /* This is the first thing in the chain. */
4511 retval = tmp;
4512 else
4513 p->next = tmp;
4514 p = tmp;
4515 }
4516 p->next = NULL;
4517 return retval;
4518}
4519
4a64f543 4520/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4521
4522bpstat
fba45db2 4523bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4524{
c5aa993b
JM
4525 if (bsp == NULL)
4526 return NULL;
c906108c 4527
c5aa993b
JM
4528 for (; bsp != NULL; bsp = bsp->next)
4529 {
f431efe5 4530 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4531 return bsp;
4532 }
c906108c
SS
4533 return NULL;
4534}
4535
ab04a2af
TT
4536/* See breakpoint.h. */
4537
47591c29 4538int
427cd150 4539bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4540{
ab04a2af
TT
4541 for (; bsp != NULL; bsp = bsp->next)
4542 {
427cd150
TT
4543 if (bsp->breakpoint_at == NULL)
4544 {
4545 /* A moribund location can never explain a signal other than
4546 GDB_SIGNAL_TRAP. */
4547 if (sig == GDB_SIGNAL_TRAP)
47591c29 4548 return 1;
427cd150
TT
4549 }
4550 else
47591c29
PA
4551 {
4552 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4553 sig))
4554 return 1;
4555 }
ab04a2af
TT
4556 }
4557
47591c29 4558 return 0;
ab04a2af
TT
4559}
4560
4a64f543
MS
4561/* Put in *NUM the breakpoint number of the first breakpoint we are
4562 stopped at. *BSP upon return is a bpstat which points to the
4563 remaining breakpoints stopped at (but which is not guaranteed to be
4564 good for anything but further calls to bpstat_num).
4565
8671a17b
PA
4566 Return 0 if passed a bpstat which does not indicate any breakpoints.
4567 Return -1 if stopped at a breakpoint that has been deleted since
4568 we set it.
4569 Return 1 otherwise. */
c906108c
SS
4570
4571int
8671a17b 4572bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4573{
4574 struct breakpoint *b;
4575
4576 if ((*bsp) == NULL)
4577 return 0; /* No more breakpoint values */
8671a17b 4578
4a64f543
MS
4579 /* We assume we'll never have several bpstats that correspond to a
4580 single breakpoint -- otherwise, this function might return the
4581 same number more than once and this will look ugly. */
f431efe5 4582 b = (*bsp)->breakpoint_at;
8671a17b
PA
4583 *bsp = (*bsp)->next;
4584 if (b == NULL)
4585 return -1; /* breakpoint that's been deleted since */
4586
4587 *num = b->number; /* We have its number */
4588 return 1;
c906108c
SS
4589}
4590
e93ca019 4591/* See breakpoint.h. */
c906108c
SS
4592
4593void
e93ca019 4594bpstat_clear_actions (void)
c906108c 4595{
e93ca019
JK
4596 struct thread_info *tp;
4597 bpstat bs;
4598
4599 if (ptid_equal (inferior_ptid, null_ptid))
4600 return;
4601
4602 tp = find_thread_ptid (inferior_ptid);
4603 if (tp == NULL)
4604 return;
4605
4606 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4607 {
9add0f1b 4608 decref_counted_command_line (&bs->commands);
abf85f46 4609
c906108c
SS
4610 if (bs->old_val != NULL)
4611 {
4612 value_free (bs->old_val);
4613 bs->old_val = NULL;
4614 }
4615 }
4616}
4617
f3b1572e
PA
4618/* Called when a command is about to proceed the inferior. */
4619
4620static void
4621breakpoint_about_to_proceed (void)
4622{
4623 if (!ptid_equal (inferior_ptid, null_ptid))
4624 {
4625 struct thread_info *tp = inferior_thread ();
4626
4627 /* Allow inferior function calls in breakpoint commands to not
4628 interrupt the command list. When the call finishes
4629 successfully, the inferior will be standing at the same
4630 breakpoint as if nothing happened. */
16c381f0 4631 if (tp->control.in_infcall)
f3b1572e
PA
4632 return;
4633 }
4634
4635 breakpoint_proceeded = 1;
4636}
4637
4a64f543
MS
4638/* Stub for cleaning up our state if we error-out of a breakpoint
4639 command. */
c906108c 4640static void
4efb68b1 4641cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4642{
4643 executing_breakpoint_commands = 0;
4644}
4645
abf85f46
JK
4646/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4647 or its equivalent. */
4648
4649static int
4650command_line_is_silent (struct command_line *cmd)
4651{
4f45d445 4652 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4653}
4654
4a64f543
MS
4655/* Execute all the commands associated with all the breakpoints at
4656 this location. Any of these commands could cause the process to
4657 proceed beyond this point, etc. We look out for such changes by
4658 checking the global "breakpoint_proceeded" after each command.
c906108c 4659
347bddb7
PA
4660 Returns true if a breakpoint command resumed the inferior. In that
4661 case, it is the caller's responsibility to recall it again with the
4662 bpstat of the current thread. */
4663
4664static int
4665bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4666{
4667 bpstat bs;
4668 struct cleanup *old_chain;
347bddb7 4669 int again = 0;
c906108c
SS
4670
4671 /* Avoid endless recursion if a `source' command is contained
4672 in bs->commands. */
4673 if (executing_breakpoint_commands)
347bddb7 4674 return 0;
c906108c
SS
4675
4676 executing_breakpoint_commands = 1;
4677 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4678
1ac32117 4679 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4680
4a64f543 4681 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4682 bs = *bsp;
4683
4684 breakpoint_proceeded = 0;
4685 for (; bs != NULL; bs = bs->next)
4686 {
9add0f1b 4687 struct counted_command_line *ccmd;
6c50ab1c
JB
4688 struct command_line *cmd;
4689 struct cleanup *this_cmd_tree_chain;
4690
4691 /* Take ownership of the BSP's command tree, if it has one.
4692
4693 The command tree could legitimately contain commands like
4694 'step' and 'next', which call clear_proceed_status, which
4695 frees stop_bpstat's command tree. To make sure this doesn't
4696 free the tree we're executing out from under us, we need to
4697 take ownership of the tree ourselves. Since a given bpstat's
4698 commands are only executed once, we don't need to copy it; we
4699 can clear the pointer in the bpstat, and make sure we free
4700 the tree when we're done. */
9add0f1b
TT
4701 ccmd = bs->commands;
4702 bs->commands = NULL;
abf85f46
JK
4703 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4704 cmd = ccmd ? ccmd->commands : NULL;
4705 if (command_line_is_silent (cmd))
4706 {
4707 /* The action has been already done by bpstat_stop_status. */
4708 cmd = cmd->next;
4709 }
6c50ab1c 4710
c906108c
SS
4711 while (cmd != NULL)
4712 {
4713 execute_control_command (cmd);
4714
4715 if (breakpoint_proceeded)
4716 break;
4717 else
4718 cmd = cmd->next;
4719 }
6c50ab1c
JB
4720
4721 /* We can free this command tree now. */
4722 do_cleanups (this_cmd_tree_chain);
4723
c906108c 4724 if (breakpoint_proceeded)
32c1e744 4725 {
cb814510 4726 if (current_ui->async)
347bddb7
PA
4727 /* If we are in async mode, then the target might be still
4728 running, not stopped at any breakpoint, so nothing for
4729 us to do here -- just return to the event loop. */
4730 ;
32c1e744
VP
4731 else
4732 /* In sync mode, when execute_control_command returns
4733 we're already standing on the next breakpoint.
347bddb7
PA
4734 Breakpoint commands for that stop were not run, since
4735 execute_command does not run breakpoint commands --
4736 only command_line_handler does, but that one is not
4737 involved in execution of breakpoint commands. So, we
4738 can now execute breakpoint commands. It should be
4739 noted that making execute_command do bpstat actions is
4740 not an option -- in this case we'll have recursive
4741 invocation of bpstat for each breakpoint with a
4742 command, and can easily blow up GDB stack. Instead, we
4743 return true, which will trigger the caller to recall us
4744 with the new stop_bpstat. */
4745 again = 1;
4746 break;
32c1e744 4747 }
c906108c 4748 }
c2b8ed2c 4749 do_cleanups (old_chain);
347bddb7
PA
4750 return again;
4751}
4752
4753void
4754bpstat_do_actions (void)
4755{
353d1d73
JK
4756 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4757
347bddb7
PA
4758 /* Do any commands attached to breakpoint we are stopped at. */
4759 while (!ptid_equal (inferior_ptid, null_ptid)
4760 && target_has_execution
4761 && !is_exited (inferior_ptid)
4762 && !is_executing (inferior_ptid))
4763 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4764 and only return when it is stopped at the next breakpoint, we
4765 keep doing breakpoint actions until it returns false to
4766 indicate the inferior was not resumed. */
16c381f0 4767 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4768 break;
353d1d73
JK
4769
4770 discard_cleanups (cleanup_if_error);
c906108c
SS
4771}
4772
fa4727a6
DJ
4773/* Print out the (old or new) value associated with a watchpoint. */
4774
4775static void
4776watchpoint_value_print (struct value *val, struct ui_file *stream)
4777{
4778 if (val == NULL)
4779 fprintf_unfiltered (stream, _("<unreadable>"));
4780 else
79a45b7d
TT
4781 {
4782 struct value_print_options opts;
4783 get_user_print_options (&opts);
4784 value_print (val, stream, &opts);
4785 }
fa4727a6
DJ
4786}
4787
f303dbd6
PA
4788/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4789 debugging multiple threads. */
4790
4791void
4792maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4793{
112e8700 4794 if (uiout->is_mi_like_p ())
f303dbd6
PA
4795 return;
4796
112e8700 4797 uiout->text ("\n");
f303dbd6
PA
4798
4799 if (show_thread_that_caused_stop ())
4800 {
4801 const char *name;
4802 struct thread_info *thr = inferior_thread ();
4803
112e8700
SM
4804 uiout->text ("Thread ");
4805 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4806
4807 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4808 if (name != NULL)
4809 {
112e8700
SM
4810 uiout->text (" \"");
4811 uiout->field_fmt ("name", "%s", name);
4812 uiout->text ("\"");
f303dbd6
PA
4813 }
4814
112e8700 4815 uiout->text (" hit ");
f303dbd6
PA
4816 }
4817}
4818
e514a9d6 4819/* Generic routine for printing messages indicating why we
4a64f543 4820 stopped. The behavior of this function depends on the value
e514a9d6
JM
4821 'print_it' in the bpstat structure. Under some circumstances we
4822 may decide not to print anything here and delegate the task to
4a64f543 4823 normal_stop(). */
e514a9d6
JM
4824
4825static enum print_stop_action
4826print_bp_stop_message (bpstat bs)
4827{
4828 switch (bs->print_it)
4829 {
4830 case print_it_noop:
4a64f543 4831 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4832 return PRINT_UNKNOWN;
4833 break;
4834
4835 case print_it_done:
4836 /* We still want to print the frame, but we already printed the
4a64f543 4837 relevant messages. */
e514a9d6
JM
4838 return PRINT_SRC_AND_LOC;
4839 break;
4840
4841 case print_it_normal:
4f8d1dc6 4842 {
f431efe5
PA
4843 struct breakpoint *b = bs->breakpoint_at;
4844
1a6a67de
TJB
4845 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4846 which has since been deleted. */
4847 if (b == NULL)
4848 return PRINT_UNKNOWN;
4849
348d480f
PA
4850 /* Normal case. Call the breakpoint's print_it method. */
4851 return b->ops->print_it (bs);
4f8d1dc6 4852 }
348d480f 4853 break;
3086aeae 4854
e514a9d6 4855 default:
8e65ff28 4856 internal_error (__FILE__, __LINE__,
e2e0b3e5 4857 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4858 break;
c906108c 4859 }
c906108c
SS
4860}
4861
edcc5120
TT
4862/* A helper function that prints a shared library stopped event. */
4863
4864static void
4865print_solib_event (int is_catchpoint)
4866{
4867 int any_deleted
4868 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4869 int any_added
4870 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4871
4872 if (!is_catchpoint)
4873 {
4874 if (any_added || any_deleted)
112e8700 4875 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4876 else
112e8700
SM
4877 current_uiout->text (_("Stopped due to shared library event (no "
4878 "libraries added or removed)\n"));
edcc5120
TT
4879 }
4880
112e8700
SM
4881 if (current_uiout->is_mi_like_p ())
4882 current_uiout->field_string ("reason",
4883 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4884
4885 if (any_deleted)
4886 {
edcc5120
TT
4887 char *name;
4888 int ix;
4889
112e8700 4890 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4891 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4892 for (ix = 0;
4893 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4894 ix, name);
4895 ++ix)
4896 {
4897 if (ix > 0)
112e8700
SM
4898 current_uiout->text (" ");
4899 current_uiout->field_string ("library", name);
4900 current_uiout->text ("\n");
edcc5120 4901 }
edcc5120
TT
4902 }
4903
4904 if (any_added)
4905 {
4906 struct so_list *iter;
4907 int ix;
edcc5120 4908
112e8700 4909 current_uiout->text (_(" Inferior loaded "));
10f489e5 4910 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4911 for (ix = 0;
4912 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4913 ix, iter);
4914 ++ix)
4915 {
4916 if (ix > 0)
112e8700
SM
4917 current_uiout->text (" ");
4918 current_uiout->field_string ("library", iter->so_name);
4919 current_uiout->text ("\n");
edcc5120 4920 }
edcc5120
TT
4921 }
4922}
4923
e514a9d6
JM
4924/* Print a message indicating what happened. This is called from
4925 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4926 list - a list of the eventpoints that caused this stop. KIND is
4927 the target_waitkind for the stopping event. This
e514a9d6
JM
4928 routine calls the generic print routine for printing a message
4929 about reasons for stopping. This will print (for example) the
4930 "Breakpoint n," part of the output. The return value of this
4931 routine is one of:
c906108c 4932
4a64f543 4933 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4934 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4935 code to print the location. An example is
c5aa993b
JM
4936 "Breakpoint 1, " which should be followed by
4937 the location.
917317f4 4938 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4939 to also print the location part of the message.
4940 An example is the catch/throw messages, which
4a64f543 4941 don't require a location appended to the end.
917317f4 4942 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4943 further info to be printed. */
c906108c 4944
917317f4 4945enum print_stop_action
36dfb11c 4946bpstat_print (bpstat bs, int kind)
c906108c 4947{
f486487f 4948 enum print_stop_action val;
c5aa993b 4949
c906108c 4950 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4951 (Currently all watchpoints go on the bpstat whether hit or not.
4952 That probably could (should) be changed, provided care is taken
c906108c 4953 with respect to bpstat_explains_signal). */
e514a9d6
JM
4954 for (; bs; bs = bs->next)
4955 {
4956 val = print_bp_stop_message (bs);
4957 if (val == PRINT_SRC_ONLY
4958 || val == PRINT_SRC_AND_LOC
4959 || val == PRINT_NOTHING)
4960 return val;
4961 }
c906108c 4962
36dfb11c
TT
4963 /* If we had hit a shared library event breakpoint,
4964 print_bp_stop_message would print out this message. If we hit an
4965 OS-level shared library event, do the same thing. */
4966 if (kind == TARGET_WAITKIND_LOADED)
4967 {
edcc5120 4968 print_solib_event (0);
36dfb11c
TT
4969 return PRINT_NOTHING;
4970 }
4971
e514a9d6 4972 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4973 with and nothing was printed. */
917317f4 4974 return PRINT_UNKNOWN;
c906108c
SS
4975}
4976
c42bd95a
DE
4977/* Evaluate the expression EXP and return 1 if value is zero.
4978 This returns the inverse of the condition because it is called
4979 from catch_errors which returns 0 if an exception happened, and if an
4980 exception happens we want execution to stop.
4a64f543 4981 The argument is a "struct expression *" that has been cast to a
c42bd95a 4982 "void *" to make it pass through catch_errors. */
c906108c
SS
4983
4984static int
4efb68b1 4985breakpoint_cond_eval (void *exp)
c906108c 4986{
278cd55f 4987 struct value *mark = value_mark ();
c5aa993b 4988 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4989
c906108c
SS
4990 value_free_to_mark (mark);
4991 return i;
4992}
4993
5760d0ab 4994/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4995
4996static bpstat
5760d0ab 4997bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4998{
4999 bpstat bs;
5000
5001 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5002 bs->next = NULL;
5003 **bs_link_pointer = bs;
5004 *bs_link_pointer = &bs->next;
f431efe5
PA
5005 bs->breakpoint_at = bl->owner;
5006 bs->bp_location_at = bl;
5007 incref_bp_location (bl);
c906108c
SS
5008 /* If the condition is false, etc., don't do the commands. */
5009 bs->commands = NULL;
5010 bs->old_val = NULL;
5011 bs->print_it = print_it_normal;
5012 return bs;
5013}
5014\f
d983da9c
DJ
5015/* The target has stopped with waitstatus WS. Check if any hardware
5016 watchpoints have triggered, according to the target. */
5017
5018int
5019watchpoints_triggered (struct target_waitstatus *ws)
5020{
d92524f1 5021 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5022 CORE_ADDR addr;
5023 struct breakpoint *b;
5024
5025 if (!stopped_by_watchpoint)
5026 {
5027 /* We were not stopped by a watchpoint. Mark all watchpoints
5028 as not triggered. */
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_no;
5035 }
d983da9c
DJ
5036
5037 return 0;
5038 }
5039
5040 if (!target_stopped_data_address (&current_target, &addr))
5041 {
5042 /* We were stopped by a watchpoint, but we don't know where.
5043 Mark all watchpoints as unknown. */
5044 ALL_BREAKPOINTS (b)
cc60f2e3 5045 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5046 {
5047 struct watchpoint *w = (struct watchpoint *) b;
5048
5049 w->watchpoint_triggered = watch_triggered_unknown;
5050 }
d983da9c 5051
3c4797ba 5052 return 1;
d983da9c
DJ
5053 }
5054
5055 /* The target could report the data address. Mark watchpoints
5056 affected by this data address as triggered, and all others as not
5057 triggered. */
5058
5059 ALL_BREAKPOINTS (b)
cc60f2e3 5060 if (is_hardware_watchpoint (b))
d983da9c 5061 {
3a5c3e22 5062 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5063 struct bp_location *loc;
d983da9c 5064
3a5c3e22 5065 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5066 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5067 {
3a5c3e22 5068 if (is_masked_watchpoint (b))
9c06b0b4 5069 {
3a5c3e22
PA
5070 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5071 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5072
5073 if (newaddr == start)
5074 {
3a5c3e22 5075 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5076 break;
5077 }
5078 }
5079 /* Exact match not required. Within range is sufficient. */
5080 else if (target_watchpoint_addr_within_range (&current_target,
5081 addr, loc->address,
5082 loc->length))
5083 {
3a5c3e22 5084 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5085 break;
5086 }
5087 }
d983da9c
DJ
5088 }
5089
5090 return 1;
5091}
5092
c906108c
SS
5093/* Possible return values for watchpoint_check (this can't be an enum
5094 because of check_errors). */
5095/* The watchpoint has been deleted. */
5096#define WP_DELETED 1
5097/* The value has changed. */
5098#define WP_VALUE_CHANGED 2
5099/* The value has not changed. */
5100#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5101/* Ignore this watchpoint, no matter if the value changed or not. */
5102#define WP_IGNORE 4
c906108c
SS
5103
5104#define BP_TEMPFLAG 1
5105#define BP_HARDWAREFLAG 2
5106
4a64f543
MS
5107/* Evaluate watchpoint condition expression and check if its value
5108 changed.
553e4c11
JB
5109
5110 P should be a pointer to struct bpstat, but is defined as a void *
5111 in order for this function to be usable with catch_errors. */
c906108c
SS
5112
5113static int
4efb68b1 5114watchpoint_check (void *p)
c906108c
SS
5115{
5116 bpstat bs = (bpstat) p;
3a5c3e22 5117 struct watchpoint *b;
c906108c
SS
5118 struct frame_info *fr;
5119 int within_current_scope;
5120
f431efe5 5121 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5122 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5123 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5124
f6bc2008
PA
5125 /* If this is a local watchpoint, we only want to check if the
5126 watchpoint frame is in scope if the current thread is the thread
5127 that was used to create the watchpoint. */
5128 if (!watchpoint_in_thread_scope (b))
60e1c644 5129 return WP_IGNORE;
f6bc2008 5130
c906108c
SS
5131 if (b->exp_valid_block == NULL)
5132 within_current_scope = 1;
5133 else
5134 {
edb3359d
DJ
5135 struct frame_info *frame = get_current_frame ();
5136 struct gdbarch *frame_arch = get_frame_arch (frame);
5137 CORE_ADDR frame_pc = get_frame_pc (frame);
5138
c9cf6e20 5139 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5140 still in the function but the stack frame has already been
5141 invalidated. Since we can't rely on the values of local
5142 variables after the stack has been destroyed, we are treating
5143 the watchpoint in that state as `not changed' without further
5144 checking. Don't mark watchpoints as changed if the current
5145 frame is in an epilogue - even if they are in some other
5146 frame, our view of the stack is likely to be wrong and
5147 frame_find_by_id could error out. */
c9cf6e20 5148 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5149 return WP_IGNORE;
a0f49112 5150
101dcfbe 5151 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5152 within_current_scope = (fr != NULL);
69fbadd5
DJ
5153
5154 /* If we've gotten confused in the unwinder, we might have
5155 returned a frame that can't describe this variable. */
edb3359d
DJ
5156 if (within_current_scope)
5157 {
5158 struct symbol *function;
5159
5160 function = get_frame_function (fr);
5161 if (function == NULL
5162 || !contained_in (b->exp_valid_block,
5163 SYMBOL_BLOCK_VALUE (function)))
5164 within_current_scope = 0;
5165 }
69fbadd5 5166
edb3359d 5167 if (within_current_scope)
c906108c
SS
5168 /* If we end up stopping, the current frame will get selected
5169 in normal_stop. So this call to select_frame won't affect
5170 the user. */
0f7d239c 5171 select_frame (fr);
c906108c 5172 }
c5aa993b 5173
c906108c
SS
5174 if (within_current_scope)
5175 {
4a64f543
MS
5176 /* We use value_{,free_to_}mark because it could be a *long*
5177 time before we return to the command level and call
5178 free_all_values. We can't call free_all_values because we
5179 might be in the middle of evaluating a function call. */
c906108c 5180
0cf6dd15 5181 int pc = 0;
9c06b0b4 5182 struct value *mark;
fa4727a6
DJ
5183 struct value *new_val;
5184
3a5c3e22 5185 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5186 /* Since we don't know the exact trigger address (from
5187 stopped_data_address), just tell the user we've triggered
5188 a mask watchpoint. */
5189 return WP_VALUE_CHANGED;
5190
5191 mark = value_mark ();
4d01a485 5192 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5193
bb9d5f81
PP
5194 if (b->val_bitsize != 0)
5195 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5196
4a64f543
MS
5197 /* We use value_equal_contents instead of value_equal because
5198 the latter coerces an array to a pointer, thus comparing just
5199 the address of the array instead of its contents. This is
5200 not what we want. */
fa4727a6 5201 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5202 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5203 {
fa4727a6
DJ
5204 if (new_val != NULL)
5205 {
5206 release_value (new_val);
5207 value_free_to_mark (mark);
5208 }
c906108c
SS
5209 bs->old_val = b->val;
5210 b->val = new_val;
fa4727a6 5211 b->val_valid = 1;
c906108c
SS
5212 return WP_VALUE_CHANGED;
5213 }
5214 else
5215 {
60e1c644 5216 /* Nothing changed. */
c906108c 5217 value_free_to_mark (mark);
c906108c
SS
5218 return WP_VALUE_NOT_CHANGED;
5219 }
5220 }
5221 else
5222 {
5223 /* This seems like the only logical thing to do because
c5aa993b
JM
5224 if we temporarily ignored the watchpoint, then when
5225 we reenter the block in which it is valid it contains
5226 garbage (in the case of a function, it may have two
5227 garbage values, one before and one after the prologue).
5228 So we can't even detect the first assignment to it and
5229 watch after that (since the garbage may or may not equal
5230 the first value assigned). */
348d480f
PA
5231 /* We print all the stop information in
5232 breakpoint_ops->print_it, but in this case, by the time we
5233 call breakpoint_ops->print_it this bp will be deleted
5234 already. So we have no choice but print the information
5235 here. */
468afe6c 5236
0e454242 5237 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5238 {
5239 struct ui_out *uiout = current_uiout;
5240
112e8700
SM
5241 if (uiout->is_mi_like_p ())
5242 uiout->field_string
5243 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5244 uiout->text ("\nWatchpoint ");
5245 uiout->field_int ("wpnum", b->base.number);
5246 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5247 "which its expression is valid.\n");
5248 }
4ce44c66 5249
cdac0397 5250 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5251 decref_counted_command_line (&b->base.commands);
d0fb5eae 5252 watchpoint_del_at_next_stop (b);
c906108c
SS
5253
5254 return WP_DELETED;
5255 }
5256}
5257
18a18393 5258/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5259 breakpoint location BL. This function does not check if we should
5260 stop, only if BL explains the stop. */
5261
18a18393 5262static int
6c95b8df 5263bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5264 struct address_space *aspace, CORE_ADDR bp_addr,
5265 const struct target_waitstatus *ws)
18a18393
VP
5266{
5267 struct breakpoint *b = bl->owner;
5268
348d480f 5269 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5270 gdb_assert (b != NULL);
5271
09ac7c10 5272 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5273}
5274
3a5c3e22
PA
5275/* Determine if the watched values have actually changed, and we
5276 should stop. If not, set BS->stop to 0. */
5277
18a18393
VP
5278static void
5279bpstat_check_watchpoint (bpstat bs)
5280{
2bdf28a0 5281 const struct bp_location *bl;
3a5c3e22 5282 struct watchpoint *b;
2bdf28a0
JK
5283
5284 /* BS is built for existing struct breakpoint. */
f431efe5 5285 bl = bs->bp_location_at;
2bdf28a0 5286 gdb_assert (bl != NULL);
3a5c3e22 5287 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5288 gdb_assert (b != NULL);
18a18393 5289
18a18393 5290 {
18a18393
VP
5291 int must_check_value = 0;
5292
3a5c3e22 5293 if (b->base.type == bp_watchpoint)
18a18393
VP
5294 /* For a software watchpoint, we must always check the
5295 watched value. */
5296 must_check_value = 1;
5297 else if (b->watchpoint_triggered == watch_triggered_yes)
5298 /* We have a hardware watchpoint (read, write, or access)
5299 and the target earlier reported an address watched by
5300 this watchpoint. */
5301 must_check_value = 1;
5302 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5303 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5304 /* We were stopped by a hardware watchpoint, but the target could
5305 not report the data address. We must check the watchpoint's
5306 value. Access and read watchpoints are out of luck; without
5307 a data address, we can't figure it out. */
5308 must_check_value = 1;
3a5c3e22 5309
18a18393
VP
5310 if (must_check_value)
5311 {
3e43a32a
MS
5312 char *message
5313 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5314 b->base.number);
18a18393
VP
5315 struct cleanup *cleanups = make_cleanup (xfree, message);
5316 int e = catch_errors (watchpoint_check, bs, message,
5317 RETURN_MASK_ALL);
5318 do_cleanups (cleanups);
5319 switch (e)
5320 {
5321 case WP_DELETED:
5322 /* We've already printed what needs to be printed. */
5323 bs->print_it = print_it_done;
5324 /* Stop. */
5325 break;
60e1c644
PA
5326 case WP_IGNORE:
5327 bs->print_it = print_it_noop;
5328 bs->stop = 0;
5329 break;
18a18393 5330 case WP_VALUE_CHANGED:
3a5c3e22 5331 if (b->base.type == bp_read_watchpoint)
18a18393 5332 {
85d721b8
PA
5333 /* There are two cases to consider here:
5334
4a64f543 5335 1. We're watching the triggered memory for reads.
85d721b8
PA
5336 In that case, trust the target, and always report
5337 the watchpoint hit to the user. Even though
5338 reads don't cause value changes, the value may
5339 have changed since the last time it was read, and
5340 since we're not trapping writes, we will not see
5341 those, and as such we should ignore our notion of
5342 old value.
5343
4a64f543 5344 2. We're watching the triggered memory for both
85d721b8
PA
5345 reads and writes. There are two ways this may
5346 happen:
5347
4a64f543 5348 2.1. This is a target that can't break on data
85d721b8
PA
5349 reads only, but can break on accesses (reads or
5350 writes), such as e.g., x86. We detect this case
5351 at the time we try to insert read watchpoints.
5352
4a64f543 5353 2.2. Otherwise, the target supports read
85d721b8
PA
5354 watchpoints, but, the user set an access or write
5355 watchpoint watching the same memory as this read
5356 watchpoint.
5357
5358 If we're watching memory writes as well as reads,
5359 ignore watchpoint hits when we find that the
5360 value hasn't changed, as reads don't cause
5361 changes. This still gives false positives when
5362 the program writes the same value to memory as
5363 what there was already in memory (we will confuse
5364 it for a read), but it's much better than
5365 nothing. */
5366
5367 int other_write_watchpoint = 0;
5368
5369 if (bl->watchpoint_type == hw_read)
5370 {
5371 struct breakpoint *other_b;
5372
5373 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5374 if (other_b->type == bp_hardware_watchpoint
5375 || other_b->type == bp_access_watchpoint)
85d721b8 5376 {
3a5c3e22
PA
5377 struct watchpoint *other_w =
5378 (struct watchpoint *) other_b;
5379
5380 if (other_w->watchpoint_triggered
5381 == watch_triggered_yes)
5382 {
5383 other_write_watchpoint = 1;
5384 break;
5385 }
85d721b8
PA
5386 }
5387 }
5388
5389 if (other_write_watchpoint
5390 || bl->watchpoint_type == hw_access)
5391 {
5392 /* We're watching the same memory for writes,
5393 and the value changed since the last time we
5394 updated it, so this trap must be for a write.
5395 Ignore it. */
5396 bs->print_it = print_it_noop;
5397 bs->stop = 0;
5398 }
18a18393
VP
5399 }
5400 break;
5401 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5402 if (b->base.type == bp_hardware_watchpoint
5403 || b->base.type == bp_watchpoint)
18a18393
VP
5404 {
5405 /* Don't stop: write watchpoints shouldn't fire if
5406 the value hasn't changed. */
5407 bs->print_it = print_it_noop;
5408 bs->stop = 0;
5409 }
5410 /* Stop. */
5411 break;
5412 default:
5413 /* Can't happen. */
5414 case 0:
5415 /* Error from catch_errors. */
468afe6c 5416 {
0e454242 5417 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5418 {
5419 printf_filtered (_("Watchpoint %d deleted.\n"),
5420 b->base.number);
5421 }
5422 watchpoint_del_at_next_stop (b);
5423 /* We've already printed what needs to be printed. */
5424 bs->print_it = print_it_done;
5425 }
18a18393
VP
5426 break;
5427 }
5428 }
5429 else /* must_check_value == 0 */
5430 {
5431 /* This is a case where some watchpoint(s) triggered, but
5432 not at the address of this watchpoint, or else no
5433 watchpoint triggered after all. So don't print
5434 anything for this watchpoint. */
5435 bs->print_it = print_it_noop;
5436 bs->stop = 0;
5437 }
5438 }
5439}
5440
7d4df6a4
DE
5441/* For breakpoints that are currently marked as telling gdb to stop,
5442 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5443 of breakpoint referred to by BS. If we should not stop for this
5444 breakpoint, set BS->stop to 0. */
f431efe5 5445
18a18393
VP
5446static void
5447bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5448{
2bdf28a0
JK
5449 const struct bp_location *bl;
5450 struct breakpoint *b;
7d4df6a4
DE
5451 int value_is_zero = 0;
5452 struct expression *cond;
5453
5454 gdb_assert (bs->stop);
2bdf28a0
JK
5455
5456 /* BS is built for existing struct breakpoint. */
f431efe5 5457 bl = bs->bp_location_at;
2bdf28a0 5458 gdb_assert (bl != NULL);
f431efe5 5459 b = bs->breakpoint_at;
2bdf28a0 5460 gdb_assert (b != NULL);
18a18393 5461
b775012e
LM
5462 /* Even if the target evaluated the condition on its end and notified GDB, we
5463 need to do so again since GDB does not know if we stopped due to a
5464 breakpoint or a single step breakpoint. */
5465
18a18393 5466 if (frame_id_p (b->frame_id)
edb3359d 5467 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5468 {
7d4df6a4
DE
5469 bs->stop = 0;
5470 return;
5471 }
60e1c644 5472
12ab52e9
PA
5473 /* If this is a thread/task-specific breakpoint, don't waste cpu
5474 evaluating the condition if this isn't the specified
5475 thread/task. */
5d5658a1 5476 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5477 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5478
6c1b0f7b
DE
5479 {
5480 bs->stop = 0;
5481 return;
5482 }
5483
6dddc817
DE
5484 /* Evaluate extension language breakpoints that have a "stop" method
5485 implemented. */
5486 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5487
7d4df6a4
DE
5488 if (is_watchpoint (b))
5489 {
5490 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5491
4d01a485 5492 cond = w->cond_exp.get ();
7d4df6a4
DE
5493 }
5494 else
4d01a485 5495 cond = bl->cond.get ();
60e1c644 5496
7d4df6a4
DE
5497 if (cond && b->disposition != disp_del_at_next_stop)
5498 {
5499 int within_current_scope = 1;
5500 struct watchpoint * w;
60e1c644 5501
7d4df6a4
DE
5502 /* We use value_mark and value_free_to_mark because it could
5503 be a long time before we return to the command level and
5504 call free_all_values. We can't call free_all_values
5505 because we might be in the middle of evaluating a
5506 function call. */
5507 struct value *mark = value_mark ();
5508
5509 if (is_watchpoint (b))
5510 w = (struct watchpoint *) b;
5511 else
5512 w = NULL;
5513
5514 /* Need to select the frame, with all that implies so that
5515 the conditions will have the right context. Because we
5516 use the frame, we will not see an inlined function's
5517 variables when we arrive at a breakpoint at the start
5518 of the inlined function; the current frame will be the
5519 call site. */
5520 if (w == NULL || w->cond_exp_valid_block == NULL)
5521 select_frame (get_current_frame ());
5522 else
18a18393 5523 {
7d4df6a4
DE
5524 struct frame_info *frame;
5525
5526 /* For local watchpoint expressions, which particular
5527 instance of a local is being watched matters, so we
5528 keep track of the frame to evaluate the expression
5529 in. To evaluate the condition however, it doesn't
5530 really matter which instantiation of the function
5531 where the condition makes sense triggers the
5532 watchpoint. This allows an expression like "watch
5533 global if q > 10" set in `func', catch writes to
5534 global on all threads that call `func', or catch
5535 writes on all recursive calls of `func' by a single
5536 thread. We simply always evaluate the condition in
5537 the innermost frame that's executing where it makes
5538 sense to evaluate the condition. It seems
5539 intuitive. */
5540 frame = block_innermost_frame (w->cond_exp_valid_block);
5541 if (frame != NULL)
5542 select_frame (frame);
5543 else
5544 within_current_scope = 0;
18a18393 5545 }
7d4df6a4
DE
5546 if (within_current_scope)
5547 value_is_zero
5548 = catch_errors (breakpoint_cond_eval, cond,
5549 "Error in testing breakpoint condition:\n",
5550 RETURN_MASK_ALL);
5551 else
18a18393 5552 {
7d4df6a4
DE
5553 warning (_("Watchpoint condition cannot be tested "
5554 "in the current scope"));
5555 /* If we failed to set the right context for this
5556 watchpoint, unconditionally report it. */
5557 value_is_zero = 0;
18a18393 5558 }
7d4df6a4
DE
5559 /* FIXME-someday, should give breakpoint #. */
5560 value_free_to_mark (mark);
18a18393 5561 }
7d4df6a4
DE
5562
5563 if (cond && value_is_zero)
5564 {
5565 bs->stop = 0;
5566 }
7d4df6a4
DE
5567 else if (b->ignore_count > 0)
5568 {
5569 b->ignore_count--;
5570 bs->stop = 0;
5571 /* Increase the hit count even though we don't stop. */
5572 ++(b->hit_count);
5573 observer_notify_breakpoint_modified (b);
5574 }
18a18393
VP
5575}
5576
1cf4d951
PA
5577/* Returns true if we need to track moribund locations of LOC's type
5578 on the current target. */
5579
5580static int
5581need_moribund_for_location_type (struct bp_location *loc)
5582{
5583 return ((loc->loc_type == bp_loc_software_breakpoint
5584 && !target_supports_stopped_by_sw_breakpoint ())
5585 || (loc->loc_type == bp_loc_hardware_breakpoint
5586 && !target_supports_stopped_by_hw_breakpoint ()));
5587}
5588
18a18393 5589
9709f61c 5590/* Get a bpstat associated with having just stopped at address
d983da9c 5591 BP_ADDR in thread PTID.
c906108c 5592
d983da9c 5593 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5594 don't understand this stop. Result is a chain of bpstat's such
5595 that:
c906108c 5596
c5aa993b 5597 if we don't understand the stop, the result is a null pointer.
c906108c 5598
c5aa993b 5599 if we understand why we stopped, the result is not null.
c906108c 5600
c5aa993b
JM
5601 Each element of the chain refers to a particular breakpoint or
5602 watchpoint at which we have stopped. (We may have stopped for
5603 several reasons concurrently.)
c906108c 5604
c5aa993b
JM
5605 Each element of the chain has valid next, breakpoint_at,
5606 commands, FIXME??? fields. */
c906108c
SS
5607
5608bpstat
6c95b8df 5609bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5610 CORE_ADDR bp_addr, ptid_t ptid,
5611 const struct target_waitstatus *ws)
c906108c 5612{
0d381245 5613 struct breakpoint *b = NULL;
afe38095 5614 struct bp_location *bl;
20874c92 5615 struct bp_location *loc;
5760d0ab
JK
5616 /* First item of allocated bpstat's. */
5617 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5618 /* Pointer to the last thing in the chain currently. */
5760d0ab 5619 bpstat bs;
20874c92 5620 int ix;
429374b8 5621 int need_remove_insert;
f431efe5 5622 int removed_any;
c906108c 5623
f431efe5
PA
5624 /* First, build the bpstat chain with locations that explain a
5625 target stop, while being careful to not set the target running,
5626 as that may invalidate locations (in particular watchpoint
5627 locations are recreated). Resuming will happen here with
5628 breakpoint conditions or watchpoint expressions that include
5629 inferior function calls. */
c5aa993b 5630
429374b8
JK
5631 ALL_BREAKPOINTS (b)
5632 {
1a853c52 5633 if (!breakpoint_enabled (b))
429374b8 5634 continue;
a5606eee 5635
429374b8
JK
5636 for (bl = b->loc; bl != NULL; bl = bl->next)
5637 {
4a64f543
MS
5638 /* For hardware watchpoints, we look only at the first
5639 location. The watchpoint_check function will work on the
5640 entire expression, not the individual locations. For
5641 read watchpoints, the watchpoints_triggered function has
5642 checked all locations already. */
429374b8
JK
5643 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5644 break;
18a18393 5645
f6592439 5646 if (!bl->enabled || bl->shlib_disabled)
429374b8 5647 continue;
c5aa993b 5648
09ac7c10 5649 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5650 continue;
c5aa993b 5651
4a64f543
MS
5652 /* Come here if it's a watchpoint, or if the break address
5653 matches. */
c5aa993b 5654
4a64f543
MS
5655 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5656 explain stop. */
c5aa993b 5657
f431efe5
PA
5658 /* Assume we stop. Should we find a watchpoint that is not
5659 actually triggered, or if the condition of the breakpoint
5660 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5661 bs->stop = 1;
5662 bs->print = 1;
d983da9c 5663
f431efe5
PA
5664 /* If this is a scope breakpoint, mark the associated
5665 watchpoint as triggered so that we will handle the
5666 out-of-scope event. We'll get to the watchpoint next
5667 iteration. */
d0fb5eae 5668 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5669 {
5670 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5671
5672 w->watchpoint_triggered = watch_triggered_yes;
5673 }
f431efe5
PA
5674 }
5675 }
5676
7c16b83e 5677 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5678 if (!target_supports_stopped_by_sw_breakpoint ()
5679 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5680 {
1cf4d951 5681 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5682 {
1cf4d951
PA
5683 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5684 && need_moribund_for_location_type (loc))
5685 {
5686 bs = bpstat_alloc (loc, &bs_link);
5687 /* For hits of moribund locations, we should just proceed. */
5688 bs->stop = 0;
5689 bs->print = 0;
5690 bs->print_it = print_it_noop;
5691 }
f431efe5
PA
5692 }
5693 }
5694
edcc5120
TT
5695 /* A bit of special processing for shlib breakpoints. We need to
5696 process solib loading here, so that the lists of loaded and
5697 unloaded libraries are correct before we handle "catch load" and
5698 "catch unload". */
5699 for (bs = bs_head; bs != NULL; bs = bs->next)
5700 {
5d268276 5701 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5702 {
5703 handle_solib_event ();
5704 break;
5705 }
5706 }
5707
f431efe5
PA
5708 /* Now go through the locations that caused the target to stop, and
5709 check whether we're interested in reporting this stop to higher
5710 layers, or whether we should resume the target transparently. */
5711
5712 removed_any = 0;
5713
5760d0ab 5714 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5715 {
5716 if (!bs->stop)
5717 continue;
5718
f431efe5 5719 b = bs->breakpoint_at;
348d480f
PA
5720 b->ops->check_status (bs);
5721 if (bs->stop)
28010a5d 5722 {
348d480f 5723 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5724
429374b8
JK
5725 if (bs->stop)
5726 {
5727 ++(b->hit_count);
8d3788bd 5728 observer_notify_breakpoint_modified (b);
c906108c 5729
4a64f543 5730 /* We will stop here. */
429374b8
JK
5731 if (b->disposition == disp_disable)
5732 {
816338b5 5733 --(b->enable_count);
1a853c52 5734 if (b->enable_count <= 0)
429374b8 5735 b->enable_state = bp_disabled;
f431efe5 5736 removed_any = 1;
429374b8
JK
5737 }
5738 if (b->silent)
5739 bs->print = 0;
5740 bs->commands = b->commands;
9add0f1b 5741 incref_counted_command_line (bs->commands);
abf85f46
JK
5742 if (command_line_is_silent (bs->commands
5743 ? bs->commands->commands : NULL))
5744 bs->print = 0;
9d6e6e84
HZ
5745
5746 b->ops->after_condition_true (bs);
429374b8
JK
5747 }
5748
348d480f 5749 }
a9b3a50f
PA
5750
5751 /* Print nothing for this entry if we don't stop or don't
5752 print. */
5753 if (!bs->stop || !bs->print)
5754 bs->print_it = print_it_noop;
429374b8 5755 }
876fa593 5756
d983da9c
DJ
5757 /* If we aren't stopping, the value of some hardware watchpoint may
5758 not have changed, but the intermediate memory locations we are
5759 watching may have. Don't bother if we're stopping; this will get
5760 done later. */
d832cb68 5761 need_remove_insert = 0;
5760d0ab
JK
5762 if (! bpstat_causes_stop (bs_head))
5763 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5764 if (!bs->stop
f431efe5
PA
5765 && bs->breakpoint_at
5766 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5767 {
3a5c3e22
PA
5768 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5769
5770 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5771 need_remove_insert = 1;
d983da9c
DJ
5772 }
5773
d832cb68 5774 if (need_remove_insert)
44702360 5775 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5776 else if (removed_any)
44702360 5777 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5778
5760d0ab 5779 return bs_head;
c906108c 5780}
628fe4e4
JK
5781
5782static void
5783handle_jit_event (void)
5784{
5785 struct frame_info *frame;
5786 struct gdbarch *gdbarch;
5787
243a9253
PA
5788 if (debug_infrun)
5789 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5790
628fe4e4
JK
5791 /* Switch terminal for any messages produced by
5792 breakpoint_re_set. */
5793 target_terminal_ours_for_output ();
5794
5795 frame = get_current_frame ();
5796 gdbarch = get_frame_arch (frame);
5797
5798 jit_event_handler (gdbarch);
5799
5800 target_terminal_inferior ();
5801}
5802
5803/* Prepare WHAT final decision for infrun. */
5804
5805/* Decide what infrun needs to do with this bpstat. */
5806
c906108c 5807struct bpstat_what
0e30163f 5808bpstat_what (bpstat bs_head)
c906108c 5809{
c906108c 5810 struct bpstat_what retval;
0e30163f 5811 bpstat bs;
c906108c 5812
628fe4e4 5813 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5814 retval.call_dummy = STOP_NONE;
186c406b 5815 retval.is_longjmp = 0;
628fe4e4 5816
0e30163f 5817 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5818 {
628fe4e4
JK
5819 /* Extract this BS's action. After processing each BS, we check
5820 if its action overrides all we've seem so far. */
5821 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5822 enum bptype bptype;
5823
c906108c 5824 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5825 {
5826 /* I suspect this can happen if it was a momentary
5827 breakpoint which has since been deleted. */
5828 bptype = bp_none;
5829 }
20874c92 5830 else
f431efe5 5831 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5832
5833 switch (bptype)
c906108c
SS
5834 {
5835 case bp_none:
628fe4e4 5836 break;
c906108c
SS
5837 case bp_breakpoint:
5838 case bp_hardware_breakpoint:
7c16b83e 5839 case bp_single_step:
c906108c
SS
5840 case bp_until:
5841 case bp_finish:
a9b3a50f 5842 case bp_shlib_event:
c906108c
SS
5843 if (bs->stop)
5844 {
5845 if (bs->print)
628fe4e4 5846 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5847 else
628fe4e4 5848 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5849 }
5850 else
628fe4e4 5851 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5852 break;
5853 case bp_watchpoint:
5854 case bp_hardware_watchpoint:
5855 case bp_read_watchpoint:
5856 case bp_access_watchpoint:
5857 if (bs->stop)
5858 {
5859 if (bs->print)
628fe4e4 5860 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5861 else
628fe4e4 5862 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5863 }
5864 else
628fe4e4
JK
5865 {
5866 /* There was a watchpoint, but we're not stopping.
5867 This requires no further action. */
5868 }
c906108c
SS
5869 break;
5870 case bp_longjmp:
e2e4d78b 5871 case bp_longjmp_call_dummy:
186c406b 5872 case bp_exception:
0a39bb32
PA
5873 if (bs->stop)
5874 {
5875 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5876 retval.is_longjmp = bptype != bp_exception;
5877 }
5878 else
5879 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5880 break;
5881 case bp_longjmp_resume:
186c406b 5882 case bp_exception_resume:
0a39bb32
PA
5883 if (bs->stop)
5884 {
5885 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5886 retval.is_longjmp = bptype == bp_longjmp_resume;
5887 }
5888 else
5889 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5890 break;
5891 case bp_step_resume:
5892 if (bs->stop)
628fe4e4
JK
5893 this_action = BPSTAT_WHAT_STEP_RESUME;
5894 else
c906108c 5895 {
628fe4e4
JK
5896 /* It is for the wrong frame. */
5897 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5898 }
c906108c 5899 break;
2c03e5be
PA
5900 case bp_hp_step_resume:
5901 if (bs->stop)
5902 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5903 else
5904 {
5905 /* It is for the wrong frame. */
5906 this_action = BPSTAT_WHAT_SINGLE;
5907 }
5908 break;
c906108c 5909 case bp_watchpoint_scope:
c4093a6a 5910 case bp_thread_event:
1900040c 5911 case bp_overlay_event:
0fd8e87f 5912 case bp_longjmp_master:
aa7d318d 5913 case bp_std_terminate_master:
186c406b 5914 case bp_exception_master:
628fe4e4 5915 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5916 break;
ce78b96d 5917 case bp_catchpoint:
c5aa993b
JM
5918 if (bs->stop)
5919 {
5920 if (bs->print)
628fe4e4 5921 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5922 else
628fe4e4 5923 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5924 }
5925 else
628fe4e4
JK
5926 {
5927 /* There was a catchpoint, but we're not stopping.
5928 This requires no further action. */
5929 }
5930 break;
628fe4e4 5931 case bp_jit_event:
628fe4e4 5932 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5933 break;
c906108c 5934 case bp_call_dummy:
53a5351d
JM
5935 /* Make sure the action is stop (silent or noisy),
5936 so infrun.c pops the dummy frame. */
aa7d318d 5937 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5938 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5939 break;
5940 case bp_std_terminate:
5941 /* Make sure the action is stop (silent or noisy),
5942 so infrun.c pops the dummy frame. */
aa7d318d 5943 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5944 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5945 break;
1042e4c0 5946 case bp_tracepoint:
7a697b8d 5947 case bp_fast_tracepoint:
0fb4aa4b 5948 case bp_static_tracepoint:
1042e4c0
SS
5949 /* Tracepoint hits should not be reported back to GDB, and
5950 if one got through somehow, it should have been filtered
5951 out already. */
5952 internal_error (__FILE__, __LINE__,
7a697b8d 5953 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5954 break;
5955 case bp_gnu_ifunc_resolver:
5956 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5957 this_action = BPSTAT_WHAT_SINGLE;
5958 break;
5959 case bp_gnu_ifunc_resolver_return:
5960 /* The breakpoint will be removed, execution will restart from the
5961 PC of the former breakpoint. */
5962 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5963 break;
e7e0cddf
SS
5964
5965 case bp_dprintf:
a11cfd87
HZ
5966 if (bs->stop)
5967 this_action = BPSTAT_WHAT_STOP_SILENT;
5968 else
5969 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5970 break;
5971
628fe4e4
JK
5972 default:
5973 internal_error (__FILE__, __LINE__,
5974 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5975 }
628fe4e4 5976
325fac50 5977 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5978 }
628fe4e4 5979
243a9253
PA
5980 return retval;
5981}
628fe4e4 5982
243a9253
PA
5983void
5984bpstat_run_callbacks (bpstat bs_head)
5985{
5986 bpstat bs;
628fe4e4 5987
0e30163f
JK
5988 for (bs = bs_head; bs != NULL; bs = bs->next)
5989 {
5990 struct breakpoint *b = bs->breakpoint_at;
5991
5992 if (b == NULL)
5993 continue;
5994 switch (b->type)
5995 {
243a9253
PA
5996 case bp_jit_event:
5997 handle_jit_event ();
5998 break;
0e30163f
JK
5999 case bp_gnu_ifunc_resolver:
6000 gnu_ifunc_resolver_stop (b);
6001 break;
6002 case bp_gnu_ifunc_resolver_return:
6003 gnu_ifunc_resolver_return_stop (b);
6004 break;
6005 }
6006 }
c906108c
SS
6007}
6008
6009/* Nonzero if we should step constantly (e.g. watchpoints on machines
6010 without hardware support). This isn't related to a specific bpstat,
6011 just to things like whether watchpoints are set. */
6012
c5aa993b 6013int
fba45db2 6014bpstat_should_step (void)
c906108c
SS
6015{
6016 struct breakpoint *b;
cc59ec59 6017
c906108c 6018 ALL_BREAKPOINTS (b)
717a8278 6019 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6020 return 1;
c906108c
SS
6021 return 0;
6022}
6023
67822962
PA
6024int
6025bpstat_causes_stop (bpstat bs)
6026{
6027 for (; bs != NULL; bs = bs->next)
6028 if (bs->stop)
6029 return 1;
6030
6031 return 0;
6032}
6033
c906108c 6034\f
c5aa993b 6035
170b53b2
UW
6036/* Compute a string of spaces suitable to indent the next line
6037 so it starts at the position corresponding to the table column
6038 named COL_NAME in the currently active table of UIOUT. */
6039
6040static char *
6041wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6042{
6043 static char wrap_indent[80];
6044 int i, total_width, width, align;
c5209615 6045 const char *text;
170b53b2
UW
6046
6047 total_width = 0;
112e8700 6048 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6049 {
6050 if (strcmp (text, col_name) == 0)
6051 {
6052 gdb_assert (total_width < sizeof wrap_indent);
6053 memset (wrap_indent, ' ', total_width);
6054 wrap_indent[total_width] = 0;
6055
6056 return wrap_indent;
6057 }
6058
6059 total_width += width + 1;
6060 }
6061
6062 return NULL;
6063}
6064
b775012e
LM
6065/* Determine if the locations of this breakpoint will have their conditions
6066 evaluated by the target, host or a mix of both. Returns the following:
6067
6068 "host": Host evals condition.
6069 "host or target": Host or Target evals condition.
6070 "target": Target evals condition.
6071*/
6072
6073static const char *
6074bp_condition_evaluator (struct breakpoint *b)
6075{
6076 struct bp_location *bl;
6077 char host_evals = 0;
6078 char target_evals = 0;
6079
6080 if (!b)
6081 return NULL;
6082
6083 if (!is_breakpoint (b))
6084 return NULL;
6085
6086 if (gdb_evaluates_breakpoint_condition_p ()
6087 || !target_supports_evaluation_of_breakpoint_conditions ())
6088 return condition_evaluation_host;
6089
6090 for (bl = b->loc; bl; bl = bl->next)
6091 {
6092 if (bl->cond_bytecode)
6093 target_evals++;
6094 else
6095 host_evals++;
6096 }
6097
6098 if (host_evals && target_evals)
6099 return condition_evaluation_both;
6100 else if (target_evals)
6101 return condition_evaluation_target;
6102 else
6103 return condition_evaluation_host;
6104}
6105
6106/* Determine the breakpoint location's condition evaluator. This is
6107 similar to bp_condition_evaluator, but for locations. */
6108
6109static const char *
6110bp_location_condition_evaluator (struct bp_location *bl)
6111{
6112 if (bl && !is_breakpoint (bl->owner))
6113 return NULL;
6114
6115 if (gdb_evaluates_breakpoint_condition_p ()
6116 || !target_supports_evaluation_of_breakpoint_conditions ())
6117 return condition_evaluation_host;
6118
6119 if (bl && bl->cond_bytecode)
6120 return condition_evaluation_target;
6121 else
6122 return condition_evaluation_host;
6123}
6124
859825b8
JK
6125/* Print the LOC location out of the list of B->LOC locations. */
6126
170b53b2
UW
6127static void
6128print_breakpoint_location (struct breakpoint *b,
6129 struct bp_location *loc)
0d381245 6130{
79a45e25 6131 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6132 struct cleanup *old_chain = save_current_program_space ();
6133
859825b8
JK
6134 if (loc != NULL && loc->shlib_disabled)
6135 loc = NULL;
6136
6c95b8df
PA
6137 if (loc != NULL)
6138 set_current_program_space (loc->pspace);
6139
56435ebe 6140 if (b->display_canonical)
d28cd78a 6141 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6142 else if (loc && loc->symtab)
0d381245
VP
6143 {
6144 struct symbol *sym
6145 = find_pc_sect_function (loc->address, loc->section);
6146 if (sym)
6147 {
112e8700
SM
6148 uiout->text ("in ");
6149 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6150 uiout->text (" ");
6151 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6152 uiout->text ("at ");
0d381245 6153 }
112e8700 6154 uiout->field_string ("file",
05cba821 6155 symtab_to_filename_for_display (loc->symtab));
112e8700 6156 uiout->text (":");
05cba821 6157
112e8700
SM
6158 if (uiout->is_mi_like_p ())
6159 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6160
112e8700 6161 uiout->field_int ("line", loc->line_number);
0d381245 6162 }
859825b8 6163 else if (loc)
0d381245 6164 {
d7e74731 6165 string_file stb;
170b53b2 6166
d7e74731 6167 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6168 demangle, "");
112e8700 6169 uiout->field_stream ("at", stb);
0d381245 6170 }
859825b8 6171 else
f00aae0f 6172 {
d28cd78a
TT
6173 uiout->field_string ("pending",
6174 event_location_to_string (b->location.get ()));
f00aae0f
KS
6175 /* If extra_string is available, it could be holding a condition
6176 or dprintf arguments. In either case, make sure it is printed,
6177 too, but only for non-MI streams. */
112e8700 6178 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6179 {
6180 if (b->type == bp_dprintf)
112e8700 6181 uiout->text (",");
f00aae0f 6182 else
112e8700
SM
6183 uiout->text (" ");
6184 uiout->text (b->extra_string);
f00aae0f
KS
6185 }
6186 }
6c95b8df 6187
b775012e
LM
6188 if (loc && is_breakpoint (b)
6189 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6190 && bp_condition_evaluator (b) == condition_evaluation_both)
6191 {
112e8700
SM
6192 uiout->text (" (");
6193 uiout->field_string ("evaluated-by",
b775012e 6194 bp_location_condition_evaluator (loc));
112e8700 6195 uiout->text (")");
b775012e
LM
6196 }
6197
6c95b8df 6198 do_cleanups (old_chain);
0d381245
VP
6199}
6200
269b11a2
PA
6201static const char *
6202bptype_string (enum bptype type)
c906108c 6203{
c4093a6a
JM
6204 struct ep_type_description
6205 {
6206 enum bptype type;
a121b7c1 6207 const char *description;
c4093a6a
JM
6208 };
6209 static struct ep_type_description bptypes[] =
c906108c 6210 {
c5aa993b
JM
6211 {bp_none, "?deleted?"},
6212 {bp_breakpoint, "breakpoint"},
c906108c 6213 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6214 {bp_single_step, "sw single-step"},
c5aa993b
JM
6215 {bp_until, "until"},
6216 {bp_finish, "finish"},
6217 {bp_watchpoint, "watchpoint"},
c906108c 6218 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6219 {bp_read_watchpoint, "read watchpoint"},
6220 {bp_access_watchpoint, "acc watchpoint"},
6221 {bp_longjmp, "longjmp"},
6222 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6223 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6224 {bp_exception, "exception"},
6225 {bp_exception_resume, "exception resume"},
c5aa993b 6226 {bp_step_resume, "step resume"},
2c03e5be 6227 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6228 {bp_watchpoint_scope, "watchpoint scope"},
6229 {bp_call_dummy, "call dummy"},
aa7d318d 6230 {bp_std_terminate, "std::terminate"},
c5aa993b 6231 {bp_shlib_event, "shlib events"},
c4093a6a 6232 {bp_thread_event, "thread events"},
1900040c 6233 {bp_overlay_event, "overlay events"},
0fd8e87f 6234 {bp_longjmp_master, "longjmp master"},
aa7d318d 6235 {bp_std_terminate_master, "std::terminate master"},
186c406b 6236 {bp_exception_master, "exception master"},
ce78b96d 6237 {bp_catchpoint, "catchpoint"},
1042e4c0 6238 {bp_tracepoint, "tracepoint"},
7a697b8d 6239 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6240 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6241 {bp_dprintf, "dprintf"},
4efc6507 6242 {bp_jit_event, "jit events"},
0e30163f
JK
6243 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6244 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6245 };
269b11a2
PA
6246
6247 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6248 || ((int) type != bptypes[(int) type].type))
6249 internal_error (__FILE__, __LINE__,
6250 _("bptypes table does not describe type #%d."),
6251 (int) type);
6252
6253 return bptypes[(int) type].description;
6254}
6255
998580f1
MK
6256/* For MI, output a field named 'thread-groups' with a list as the value.
6257 For CLI, prefix the list with the string 'inf'. */
6258
6259static void
6260output_thread_groups (struct ui_out *uiout,
6261 const char *field_name,
6262 VEC(int) *inf_num,
6263 int mi_only)
6264{
112e8700 6265 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6266 int inf;
6267 int i;
6268
6269 /* For backward compatibility, don't display inferiors in CLI unless
6270 there are several. Always display them for MI. */
6271 if (!is_mi && mi_only)
6272 return;
6273
10f489e5 6274 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6275
998580f1
MK
6276 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6277 {
6278 if (is_mi)
6279 {
6280 char mi_group[10];
6281
6282 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6283 uiout->field_string (NULL, mi_group);
998580f1
MK
6284 }
6285 else
6286 {
6287 if (i == 0)
112e8700 6288 uiout->text (" inf ");
998580f1 6289 else
112e8700 6290 uiout->text (", ");
998580f1 6291
112e8700 6292 uiout->text (plongest (inf));
998580f1
MK
6293 }
6294 }
998580f1
MK
6295}
6296
269b11a2
PA
6297/* Print B to gdb_stdout. */
6298
6299static void
6300print_one_breakpoint_location (struct breakpoint *b,
6301 struct bp_location *loc,
6302 int loc_number,
6303 struct bp_location **last_loc,
269b11a2
PA
6304 int allflag)
6305{
6306 struct command_line *l;
c2c6d25f 6307 static char bpenables[] = "nynny";
c906108c 6308
79a45e25 6309 struct ui_out *uiout = current_uiout;
0d381245
VP
6310 int header_of_multiple = 0;
6311 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6312 struct value_print_options opts;
6313
6314 get_user_print_options (&opts);
0d381245
VP
6315
6316 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6317 /* See comment in print_one_breakpoint concerning treatment of
6318 breakpoints with single disabled location. */
0d381245
VP
6319 if (loc == NULL
6320 && (b->loc != NULL
6321 && (b->loc->next != NULL || !b->loc->enabled)))
6322 header_of_multiple = 1;
6323 if (loc == NULL)
6324 loc = b->loc;
6325
c4093a6a
JM
6326 annotate_record ();
6327
6328 /* 1 */
6329 annotate_field (0);
0d381245
VP
6330 if (part_of_multiple)
6331 {
6332 char *formatted;
0c6773c1 6333 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6334 uiout->field_string ("number", formatted);
0d381245
VP
6335 xfree (formatted);
6336 }
6337 else
6338 {
112e8700 6339 uiout->field_int ("number", b->number);
0d381245 6340 }
c4093a6a
JM
6341
6342 /* 2 */
6343 annotate_field (1);
0d381245 6344 if (part_of_multiple)
112e8700 6345 uiout->field_skip ("type");
269b11a2 6346 else
112e8700 6347 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6348
6349 /* 3 */
6350 annotate_field (2);
0d381245 6351 if (part_of_multiple)
112e8700 6352 uiout->field_skip ("disp");
0d381245 6353 else
112e8700 6354 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6355
c4093a6a
JM
6356
6357 /* 4 */
6358 annotate_field (3);
0d381245 6359 if (part_of_multiple)
112e8700 6360 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6361 else
112e8700
SM
6362 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6363 uiout->spaces (2);
0d381245 6364
c4093a6a
JM
6365
6366 /* 5 and 6 */
3086aeae 6367 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6368 {
4a64f543
MS
6369 /* Although the print_one can possibly print all locations,
6370 calling it here is not likely to get any nice result. So,
6371 make sure there's just one location. */
0d381245 6372 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6373 b->ops->print_one (b, last_loc);
0d381245 6374 }
3086aeae
DJ
6375 else
6376 switch (b->type)
6377 {
6378 case bp_none:
6379 internal_error (__FILE__, __LINE__,
e2e0b3e5 6380 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6381 break;
c906108c 6382
3086aeae
DJ
6383 case bp_watchpoint:
6384 case bp_hardware_watchpoint:
6385 case bp_read_watchpoint:
6386 case bp_access_watchpoint:
3a5c3e22
PA
6387 {
6388 struct watchpoint *w = (struct watchpoint *) b;
6389
6390 /* Field 4, the address, is omitted (which makes the columns
6391 not line up too nicely with the headers, but the effect
6392 is relatively readable). */
6393 if (opts.addressprint)
112e8700 6394 uiout->field_skip ("addr");
3a5c3e22 6395 annotate_field (5);
112e8700 6396 uiout->field_string ("what", w->exp_string);
3a5c3e22 6397 }
3086aeae
DJ
6398 break;
6399
3086aeae
DJ
6400 case bp_breakpoint:
6401 case bp_hardware_breakpoint:
7c16b83e 6402 case bp_single_step:
3086aeae
DJ
6403 case bp_until:
6404 case bp_finish:
6405 case bp_longjmp:
6406 case bp_longjmp_resume:
e2e4d78b 6407 case bp_longjmp_call_dummy:
186c406b
TT
6408 case bp_exception:
6409 case bp_exception_resume:
3086aeae 6410 case bp_step_resume:
2c03e5be 6411 case bp_hp_step_resume:
3086aeae
DJ
6412 case bp_watchpoint_scope:
6413 case bp_call_dummy:
aa7d318d 6414 case bp_std_terminate:
3086aeae
DJ
6415 case bp_shlib_event:
6416 case bp_thread_event:
6417 case bp_overlay_event:
0fd8e87f 6418 case bp_longjmp_master:
aa7d318d 6419 case bp_std_terminate_master:
186c406b 6420 case bp_exception_master:
1042e4c0 6421 case bp_tracepoint:
7a697b8d 6422 case bp_fast_tracepoint:
0fb4aa4b 6423 case bp_static_tracepoint:
e7e0cddf 6424 case bp_dprintf:
4efc6507 6425 case bp_jit_event:
0e30163f
JK
6426 case bp_gnu_ifunc_resolver:
6427 case bp_gnu_ifunc_resolver_return:
79a45b7d 6428 if (opts.addressprint)
3086aeae
DJ
6429 {
6430 annotate_field (4);
54e52265 6431 if (header_of_multiple)
112e8700 6432 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6433 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6434 uiout->field_string ("addr", "<PENDING>");
0101ce28 6435 else
112e8700 6436 uiout->field_core_addr ("addr",
5af949e3 6437 loc->gdbarch, loc->address);
3086aeae
DJ
6438 }
6439 annotate_field (5);
0d381245 6440 if (!header_of_multiple)
170b53b2 6441 print_breakpoint_location (b, loc);
0d381245 6442 if (b->loc)
a6d9a66e 6443 *last_loc = b->loc;
3086aeae
DJ
6444 break;
6445 }
c906108c 6446
6c95b8df 6447
998580f1 6448 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6449 {
6450 struct inferior *inf;
998580f1
MK
6451 VEC(int) *inf_num = NULL;
6452 int mi_only = 1;
6c95b8df 6453
998580f1 6454 ALL_INFERIORS (inf)
6c95b8df
PA
6455 {
6456 if (inf->pspace == loc->pspace)
998580f1 6457 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6458 }
998580f1
MK
6459
6460 /* For backward compatibility, don't display inferiors in CLI unless
6461 there are several. Always display for MI. */
6462 if (allflag
6463 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6464 && (number_of_program_spaces () > 1
6465 || number_of_inferiors () > 1)
6466 /* LOC is for existing B, it cannot be in
6467 moribund_locations and thus having NULL OWNER. */
6468 && loc->owner->type != bp_catchpoint))
6469 mi_only = 0;
6470 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6471 VEC_free (int, inf_num);
6c95b8df
PA
6472 }
6473
4a306c9a 6474 if (!part_of_multiple)
c4093a6a 6475 {
4a306c9a
JB
6476 if (b->thread != -1)
6477 {
6478 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6479 "stop only in" line a little further down. */
112e8700
SM
6480 uiout->text (" thread ");
6481 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6482 }
6483 else if (b->task != 0)
6484 {
112e8700
SM
6485 uiout->text (" task ");
6486 uiout->field_int ("task", b->task);
4a306c9a 6487 }
c4093a6a 6488 }
f1310107 6489
112e8700 6490 uiout->text ("\n");
f1310107 6491
348d480f 6492 if (!part_of_multiple)
f1310107
TJB
6493 b->ops->print_one_detail (b, uiout);
6494
0d381245 6495 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6496 {
6497 annotate_field (6);
112e8700 6498 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6499 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6500 the frame ID. */
112e8700 6501 uiout->field_core_addr ("frame",
5af949e3 6502 b->gdbarch, b->frame_id.stack_addr);
112e8700 6503 uiout->text ("\n");
c4093a6a
JM
6504 }
6505
28010a5d 6506 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6507 {
6508 annotate_field (7);
d77f58be 6509 if (is_tracepoint (b))
112e8700 6510 uiout->text ("\ttrace only if ");
1042e4c0 6511 else
112e8700
SM
6512 uiout->text ("\tstop only if ");
6513 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6514
6515 /* Print whether the target is doing the breakpoint's condition
6516 evaluation. If GDB is doing the evaluation, don't print anything. */
6517 if (is_breakpoint (b)
6518 && breakpoint_condition_evaluation_mode ()
6519 == condition_evaluation_target)
6520 {
112e8700
SM
6521 uiout->text (" (");
6522 uiout->field_string ("evaluated-by",
b775012e 6523 bp_condition_evaluator (b));
112e8700 6524 uiout->text (" evals)");
b775012e 6525 }
112e8700 6526 uiout->text ("\n");
0101ce28
JJ
6527 }
6528
0d381245 6529 if (!part_of_multiple && b->thread != -1)
c4093a6a 6530 {
4a64f543 6531 /* FIXME should make an annotation for this. */
112e8700
SM
6532 uiout->text ("\tstop only in thread ");
6533 if (uiout->is_mi_like_p ())
6534 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6535 else
6536 {
6537 struct thread_info *thr = find_thread_global_id (b->thread);
6538
112e8700 6539 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6540 }
112e8700 6541 uiout->text ("\n");
c4093a6a
JM
6542 }
6543
556ec64d
YQ
6544 if (!part_of_multiple)
6545 {
6546 if (b->hit_count)
31f56a27
YQ
6547 {
6548 /* FIXME should make an annotation for this. */
6549 if (is_catchpoint (b))
112e8700 6550 uiout->text ("\tcatchpoint");
31f56a27 6551 else if (is_tracepoint (b))
112e8700 6552 uiout->text ("\ttracepoint");
31f56a27 6553 else
112e8700
SM
6554 uiout->text ("\tbreakpoint");
6555 uiout->text (" already hit ");
6556 uiout->field_int ("times", b->hit_count);
31f56a27 6557 if (b->hit_count == 1)
112e8700 6558 uiout->text (" time\n");
31f56a27 6559 else
112e8700 6560 uiout->text (" times\n");
31f56a27 6561 }
556ec64d
YQ
6562 else
6563 {
31f56a27 6564 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6565 if (uiout->is_mi_like_p ())
6566 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6567 }
6568 }
8b93c638 6569
0d381245 6570 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6571 {
6572 annotate_field (8);
112e8700
SM
6573 uiout->text ("\tignore next ");
6574 uiout->field_int ("ignore", b->ignore_count);
6575 uiout->text (" hits\n");
c4093a6a 6576 }
059fb39f 6577
816338b5
SS
6578 /* Note that an enable count of 1 corresponds to "enable once"
6579 behavior, which is reported by the combination of enablement and
6580 disposition, so we don't need to mention it here. */
6581 if (!part_of_multiple && b->enable_count > 1)
6582 {
6583 annotate_field (8);
112e8700 6584 uiout->text ("\tdisable after ");
816338b5
SS
6585 /* Tweak the wording to clarify that ignore and enable counts
6586 are distinct, and have additive effect. */
6587 if (b->ignore_count)
112e8700 6588 uiout->text ("additional ");
816338b5 6589 else
112e8700
SM
6590 uiout->text ("next ");
6591 uiout->field_int ("enable", b->enable_count);
6592 uiout->text (" hits\n");
816338b5
SS
6593 }
6594
f196051f
SS
6595 if (!part_of_multiple && is_tracepoint (b))
6596 {
6597 struct tracepoint *tp = (struct tracepoint *) b;
6598
6599 if (tp->traceframe_usage)
6600 {
112e8700
SM
6601 uiout->text ("\ttrace buffer usage ");
6602 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6603 uiout->text (" bytes\n");
f196051f
SS
6604 }
6605 }
d3ce09f5 6606
9add0f1b 6607 l = b->commands ? b->commands->commands : NULL;
059fb39f 6608 if (!part_of_multiple && l)
c4093a6a
JM
6609 {
6610 annotate_field (9);
2e783024 6611 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6612 print_command_lines (uiout, l, 4);
c4093a6a 6613 }
d24317b4 6614
d9b3f62e 6615 if (is_tracepoint (b))
1042e4c0 6616 {
d9b3f62e
PA
6617 struct tracepoint *t = (struct tracepoint *) b;
6618
6619 if (!part_of_multiple && t->pass_count)
6620 {
6621 annotate_field (10);
112e8700
SM
6622 uiout->text ("\tpass count ");
6623 uiout->field_int ("pass", t->pass_count);
6624 uiout->text (" \n");
d9b3f62e 6625 }
f2a8bc8a
YQ
6626
6627 /* Don't display it when tracepoint or tracepoint location is
6628 pending. */
6629 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6630 {
6631 annotate_field (11);
6632
112e8700
SM
6633 if (uiout->is_mi_like_p ())
6634 uiout->field_string ("installed",
f2a8bc8a
YQ
6635 loc->inserted ? "y" : "n");
6636 else
6637 {
6638 if (loc->inserted)
112e8700 6639 uiout->text ("\t");
f2a8bc8a 6640 else
112e8700
SM
6641 uiout->text ("\tnot ");
6642 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6643 }
6644 }
1042e4c0
SS
6645 }
6646
112e8700 6647 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6648 {
3a5c3e22
PA
6649 if (is_watchpoint (b))
6650 {
6651 struct watchpoint *w = (struct watchpoint *) b;
6652
112e8700 6653 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6654 }
f00aae0f 6655 else if (b->location != NULL
d28cd78a 6656 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6657 uiout->field_string ("original-location",
d28cd78a 6658 event_location_to_string (b->location.get ()));
d24317b4 6659 }
c4093a6a 6660}
c5aa993b 6661
0d381245
VP
6662static void
6663print_one_breakpoint (struct breakpoint *b,
4a64f543 6664 struct bp_location **last_loc,
6c95b8df 6665 int allflag)
0d381245 6666{
79a45e25 6667 struct ui_out *uiout = current_uiout;
8d3788bd 6668
2e783024
TT
6669 {
6670 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6671
2e783024
TT
6672 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6673 }
0d381245
VP
6674
6675 /* If this breakpoint has custom print function,
6676 it's already printed. Otherwise, print individual
6677 locations, if any. */
6678 if (b->ops == NULL || b->ops->print_one == NULL)
6679 {
4a64f543
MS
6680 /* If breakpoint has a single location that is disabled, we
6681 print it as if it had several locations, since otherwise it's
6682 hard to represent "breakpoint enabled, location disabled"
6683 situation.
6684
6685 Note that while hardware watchpoints have several locations
a3be7890 6686 internally, that's not a property exposed to user. */
0d381245 6687 if (b->loc
a5606eee 6688 && !is_hardware_watchpoint (b)
8d3788bd 6689 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6690 {
6691 struct bp_location *loc;
6692 int n = 1;
8d3788bd 6693
0d381245 6694 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6695 {
2e783024 6696 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6697 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6698 }
0d381245
VP
6699 }
6700 }
6701}
6702
a6d9a66e
UW
6703static int
6704breakpoint_address_bits (struct breakpoint *b)
6705{
6706 int print_address_bits = 0;
6707 struct bp_location *loc;
6708
c6d81124
PA
6709 /* Software watchpoints that aren't watching memory don't have an
6710 address to print. */
6711 if (is_no_memory_software_watchpoint (b))
6712 return 0;
6713
a6d9a66e
UW
6714 for (loc = b->loc; loc; loc = loc->next)
6715 {
c7437ca6
PA
6716 int addr_bit;
6717
c7437ca6 6718 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6719 if (addr_bit > print_address_bits)
6720 print_address_bits = addr_bit;
6721 }
6722
6723 return print_address_bits;
6724}
0d381245 6725
c4093a6a
JM
6726struct captured_breakpoint_query_args
6727 {
6728 int bnum;
6729 };
c5aa993b 6730
c4093a6a 6731static int
2b65245e 6732do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6733{
9a3c8263
SM
6734 struct captured_breakpoint_query_args *args
6735 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6736 struct breakpoint *b;
a6d9a66e 6737 struct bp_location *dummy_loc = NULL;
cc59ec59 6738
c4093a6a
JM
6739 ALL_BREAKPOINTS (b)
6740 {
6741 if (args->bnum == b->number)
c5aa993b 6742 {
12c5a436 6743 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6744 return GDB_RC_OK;
c5aa993b 6745 }
c4093a6a
JM
6746 }
6747 return GDB_RC_NONE;
6748}
c5aa993b 6749
c4093a6a 6750enum gdb_rc
4a64f543
MS
6751gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6752 char **error_message)
c4093a6a
JM
6753{
6754 struct captured_breakpoint_query_args args;
cc59ec59 6755
c4093a6a
JM
6756 args.bnum = bnum;
6757 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6758 an error. */
b0b13bb4
DJ
6759 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6760 error_message, RETURN_MASK_ALL) < 0)
6761 return GDB_RC_FAIL;
6762 else
6763 return GDB_RC_OK;
c4093a6a 6764}
c5aa993b 6765
09d682a4
TT
6766/* Return true if this breakpoint was set by the user, false if it is
6767 internal or momentary. */
6768
6769int
6770user_breakpoint_p (struct breakpoint *b)
6771{
46c6471b 6772 return b->number > 0;
09d682a4
TT
6773}
6774
93daf339
TT
6775/* See breakpoint.h. */
6776
6777int
6778pending_breakpoint_p (struct breakpoint *b)
6779{
6780 return b->loc == NULL;
6781}
6782
7f3b0473 6783/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6784 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6785 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6786 FILTER is non-NULL, call it on each breakpoint and only include the
6787 ones for which it returns non-zero. Return the total number of
6788 breakpoints listed. */
c906108c 6789
d77f58be 6790static int
e5a67952 6791breakpoint_1 (char *args, int allflag,
4a64f543 6792 int (*filter) (const struct breakpoint *))
c4093a6a 6793{
52f0bd74 6794 struct breakpoint *b;
a6d9a66e 6795 struct bp_location *last_loc = NULL;
7f3b0473 6796 int nr_printable_breakpoints;
3b31d625 6797 struct cleanup *bkpttbl_chain;
79a45b7d 6798 struct value_print_options opts;
a6d9a66e 6799 int print_address_bits = 0;
269b11a2 6800 int print_type_col_width = 14;
79a45e25 6801 struct ui_out *uiout = current_uiout;
269b11a2 6802
79a45b7d
TT
6803 get_user_print_options (&opts);
6804
4a64f543
MS
6805 /* Compute the number of rows in the table, as well as the size
6806 required for address fields. */
7f3b0473
AC
6807 nr_printable_breakpoints = 0;
6808 ALL_BREAKPOINTS (b)
e5a67952
MS
6809 {
6810 /* If we have a filter, only list the breakpoints it accepts. */
6811 if (filter && !filter (b))
6812 continue;
6813
6814 /* If we have an "args" string, it is a list of breakpoints to
6815 accept. Skip the others. */
6816 if (args != NULL && *args != '\0')
6817 {
6818 if (allflag && parse_and_eval_long (args) != b->number)
6819 continue;
6820 if (!allflag && !number_is_in_list (args, b->number))
6821 continue;
6822 }
269b11a2 6823
e5a67952
MS
6824 if (allflag || user_breakpoint_p (b))
6825 {
6826 int addr_bit, type_len;
a6d9a66e 6827
e5a67952
MS
6828 addr_bit = breakpoint_address_bits (b);
6829 if (addr_bit > print_address_bits)
6830 print_address_bits = addr_bit;
269b11a2 6831
e5a67952
MS
6832 type_len = strlen (bptype_string (b->type));
6833 if (type_len > print_type_col_width)
6834 print_type_col_width = type_len;
6835
6836 nr_printable_breakpoints++;
6837 }
6838 }
7f3b0473 6839
79a45b7d 6840 if (opts.addressprint)
3b31d625 6841 bkpttbl_chain
3e43a32a
MS
6842 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6843 nr_printable_breakpoints,
3b31d625 6844 "BreakpointTable");
8b93c638 6845 else
3b31d625 6846 bkpttbl_chain
3e43a32a
MS
6847 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6848 nr_printable_breakpoints,
3b31d625 6849 "BreakpointTable");
8b93c638 6850
7f3b0473 6851 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6852 annotate_breakpoints_headers ();
6853 if (nr_printable_breakpoints > 0)
6854 annotate_field (0);
112e8700 6855 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6856 if (nr_printable_breakpoints > 0)
6857 annotate_field (1);
112e8700 6858 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
d7faa9e7
AC
6859 if (nr_printable_breakpoints > 0)
6860 annotate_field (2);
112e8700 6861 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (3);
112e8700 6864 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6865 if (opts.addressprint)
e5a67952
MS
6866 {
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (4);
6869 if (print_address_bits <= 32)
112e8700 6870 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
e5a67952 6871 else
112e8700 6872 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
e5a67952 6873 }
d7faa9e7
AC
6874 if (nr_printable_breakpoints > 0)
6875 annotate_field (5);
112e8700
SM
6876 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6877 uiout->table_body ();
d7faa9e7
AC
6878 if (nr_printable_breakpoints > 0)
6879 annotate_breakpoints_table ();
7f3b0473 6880
c4093a6a 6881 ALL_BREAKPOINTS (b)
e5a67952
MS
6882 {
6883 QUIT;
6884 /* If we have a filter, only list the breakpoints it accepts. */
6885 if (filter && !filter (b))
6886 continue;
6887
6888 /* If we have an "args" string, it is a list of breakpoints to
6889 accept. Skip the others. */
6890
6891 if (args != NULL && *args != '\0')
6892 {
6893 if (allflag) /* maintenance info breakpoint */
6894 {
6895 if (parse_and_eval_long (args) != b->number)
6896 continue;
6897 }
6898 else /* all others */
6899 {
6900 if (!number_is_in_list (args, b->number))
6901 continue;
6902 }
6903 }
6904 /* We only print out user settable breakpoints unless the
6905 allflag is set. */
6906 if (allflag || user_breakpoint_p (b))
12c5a436 6907 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6908 }
6909
3b31d625 6910 do_cleanups (bkpttbl_chain);
698384cd 6911
7f3b0473 6912 if (nr_printable_breakpoints == 0)
c906108c 6913 {
4a64f543
MS
6914 /* If there's a filter, let the caller decide how to report
6915 empty list. */
d77f58be
SS
6916 if (!filter)
6917 {
e5a67952 6918 if (args == NULL || *args == '\0')
112e8700 6919 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6920 else
112e8700 6921 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6922 args);
d77f58be 6923 }
c906108c
SS
6924 }
6925 else
c4093a6a 6926 {
a6d9a66e
UW
6927 if (last_loc && !server_command)
6928 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6929 }
c906108c 6930
4a64f543 6931 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6932 there have been breakpoints? */
c906108c 6933 annotate_breakpoints_table_end ();
d77f58be
SS
6934
6935 return nr_printable_breakpoints;
c906108c
SS
6936}
6937
ad443146
SS
6938/* Display the value of default-collect in a way that is generally
6939 compatible with the breakpoint list. */
6940
6941static void
6942default_collect_info (void)
6943{
79a45e25
PA
6944 struct ui_out *uiout = current_uiout;
6945
ad443146
SS
6946 /* If it has no value (which is frequently the case), say nothing; a
6947 message like "No default-collect." gets in user's face when it's
6948 not wanted. */
6949 if (!*default_collect)
6950 return;
6951
6952 /* The following phrase lines up nicely with per-tracepoint collect
6953 actions. */
112e8700
SM
6954 uiout->text ("default collect ");
6955 uiout->field_string ("default-collect", default_collect);
6956 uiout->text (" \n");
ad443146
SS
6957}
6958
c906108c 6959static void
e5a67952 6960breakpoints_info (char *args, int from_tty)
c906108c 6961{
e5a67952 6962 breakpoint_1 (args, 0, NULL);
ad443146
SS
6963
6964 default_collect_info ();
d77f58be
SS
6965}
6966
6967static void
e5a67952 6968watchpoints_info (char *args, int from_tty)
d77f58be 6969{
e5a67952 6970 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6971 struct ui_out *uiout = current_uiout;
d77f58be
SS
6972
6973 if (num_printed == 0)
6974 {
e5a67952 6975 if (args == NULL || *args == '\0')
112e8700 6976 uiout->message ("No watchpoints.\n");
d77f58be 6977 else
112e8700 6978 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6979 }
c906108c
SS
6980}
6981
7a292a7a 6982static void
e5a67952 6983maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6984{
e5a67952 6985 breakpoint_1 (args, 1, NULL);
ad443146
SS
6986
6987 default_collect_info ();
c906108c
SS
6988}
6989
0d381245 6990static int
714835d5 6991breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6992 struct program_space *pspace,
714835d5 6993 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6994{
6995 struct bp_location *bl = b->loc;
cc59ec59 6996
0d381245
VP
6997 for (; bl; bl = bl->next)
6998 {
6c95b8df
PA
6999 if (bl->pspace == pspace
7000 && bl->address == pc
0d381245
VP
7001 && (!overlay_debugging || bl->section == section))
7002 return 1;
7003 }
7004 return 0;
7005}
7006
672f9b60 7007/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7008 concerns with logical breakpoints, so we match program spaces, not
7009 address spaces. */
c906108c
SS
7010
7011static void
6c95b8df
PA
7012describe_other_breakpoints (struct gdbarch *gdbarch,
7013 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7014 struct obj_section *section, int thread)
c906108c 7015{
52f0bd74
AC
7016 int others = 0;
7017 struct breakpoint *b;
c906108c
SS
7018
7019 ALL_BREAKPOINTS (b)
672f9b60
KP
7020 others += (user_breakpoint_p (b)
7021 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7022 if (others > 0)
7023 {
a3f17187
AC
7024 if (others == 1)
7025 printf_filtered (_("Note: breakpoint "));
7026 else /* if (others == ???) */
7027 printf_filtered (_("Note: breakpoints "));
c906108c 7028 ALL_BREAKPOINTS (b)
672f9b60 7029 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7030 {
7031 others--;
7032 printf_filtered ("%d", b->number);
7033 if (b->thread == -1 && thread != -1)
7034 printf_filtered (" (all threads)");
7035 else if (b->thread != -1)
7036 printf_filtered (" (thread %d)", b->thread);
7037 printf_filtered ("%s%s ",
059fb39f 7038 ((b->enable_state == bp_disabled
f8eba3c6 7039 || b->enable_state == bp_call_disabled)
0d381245 7040 ? " (disabled)"
0d381245
VP
7041 : ""),
7042 (others > 1) ? ","
7043 : ((others == 1) ? " and" : ""));
7044 }
a3f17187 7045 printf_filtered (_("also set at pc "));
5af949e3 7046 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7047 printf_filtered (".\n");
7048 }
7049}
7050\f
c906108c 7051
e4f237da 7052/* Return true iff it is meaningful to use the address member of
244558af
LM
7053 BPT locations. For some breakpoint types, the locations' address members
7054 are irrelevant and it makes no sense to attempt to compare them to other
7055 addresses (or use them for any other purpose either).
e4f237da 7056
4a64f543 7057 More specifically, each of the following breakpoint types will
244558af 7058 always have a zero valued location address and we don't want to mark
4a64f543 7059 breakpoints of any of these types to be a duplicate of an actual
244558af 7060 breakpoint location at address zero:
e4f237da
KB
7061
7062 bp_watchpoint
2d134ed3
PA
7063 bp_catchpoint
7064
7065*/
e4f237da
KB
7066
7067static int
7068breakpoint_address_is_meaningful (struct breakpoint *bpt)
7069{
7070 enum bptype type = bpt->type;
7071
2d134ed3
PA
7072 return (type != bp_watchpoint && type != bp_catchpoint);
7073}
7074
7075/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7076 true if LOC1 and LOC2 represent the same watchpoint location. */
7077
7078static int
4a64f543
MS
7079watchpoint_locations_match (struct bp_location *loc1,
7080 struct bp_location *loc2)
2d134ed3 7081{
3a5c3e22
PA
7082 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7083 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7084
7085 /* Both of them must exist. */
7086 gdb_assert (w1 != NULL);
7087 gdb_assert (w2 != NULL);
2bdf28a0 7088
4a64f543
MS
7089 /* If the target can evaluate the condition expression in hardware,
7090 then we we need to insert both watchpoints even if they are at
7091 the same place. Otherwise the watchpoint will only trigger when
7092 the condition of whichever watchpoint was inserted evaluates to
7093 true, not giving a chance for GDB to check the condition of the
7094 other watchpoint. */
3a5c3e22 7095 if ((w1->cond_exp
4a64f543
MS
7096 && target_can_accel_watchpoint_condition (loc1->address,
7097 loc1->length,
0cf6dd15 7098 loc1->watchpoint_type,
4d01a485 7099 w1->cond_exp.get ()))
3a5c3e22 7100 || (w2->cond_exp
4a64f543
MS
7101 && target_can_accel_watchpoint_condition (loc2->address,
7102 loc2->length,
0cf6dd15 7103 loc2->watchpoint_type,
4d01a485 7104 w2->cond_exp.get ())))
0cf6dd15
TJB
7105 return 0;
7106
85d721b8
PA
7107 /* Note that this checks the owner's type, not the location's. In
7108 case the target does not support read watchpoints, but does
7109 support access watchpoints, we'll have bp_read_watchpoint
7110 watchpoints with hw_access locations. Those should be considered
7111 duplicates of hw_read locations. The hw_read locations will
7112 become hw_access locations later. */
2d134ed3
PA
7113 return (loc1->owner->type == loc2->owner->type
7114 && loc1->pspace->aspace == loc2->pspace->aspace
7115 && loc1->address == loc2->address
7116 && loc1->length == loc2->length);
e4f237da
KB
7117}
7118
31e77af2 7119/* See breakpoint.h. */
6c95b8df 7120
31e77af2 7121int
6c95b8df
PA
7122breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7123 struct address_space *aspace2, CORE_ADDR addr2)
7124{
f5656ead 7125 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7126 || aspace1 == aspace2)
7127 && addr1 == addr2);
7128}
7129
f1310107
TJB
7130/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7131 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7132 matches ASPACE2. On targets that have global breakpoints, the address
7133 space doesn't really matter. */
7134
7135static int
7136breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7137 int len1, struct address_space *aspace2,
7138 CORE_ADDR addr2)
7139{
f5656ead 7140 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7141 || aspace1 == aspace2)
7142 && addr2 >= addr1 && addr2 < addr1 + len1);
7143}
7144
7145/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7146 a ranged breakpoint. In most targets, a match happens only if ASPACE
7147 matches the breakpoint's address space. On targets that have global
7148 breakpoints, the address space doesn't really matter. */
7149
7150static int
7151breakpoint_location_address_match (struct bp_location *bl,
7152 struct address_space *aspace,
7153 CORE_ADDR addr)
7154{
7155 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7156 aspace, addr)
7157 || (bl->length
7158 && breakpoint_address_match_range (bl->pspace->aspace,
7159 bl->address, bl->length,
7160 aspace, addr)));
7161}
7162
d35ae833
PA
7163/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7164 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7165 match happens only if ASPACE matches the breakpoint's address
7166 space. On targets that have global breakpoints, the address space
7167 doesn't really matter. */
7168
7169static int
7170breakpoint_location_address_range_overlap (struct bp_location *bl,
7171 struct address_space *aspace,
7172 CORE_ADDR addr, int len)
7173{
7174 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7175 || bl->pspace->aspace == aspace)
7176 {
7177 int bl_len = bl->length != 0 ? bl->length : 1;
7178
7179 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7180 return 1;
7181 }
7182 return 0;
7183}
7184
1e4d1764
YQ
7185/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7186 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7187 true, otherwise returns false. */
7188
7189static int
7190tracepoint_locations_match (struct bp_location *loc1,
7191 struct bp_location *loc2)
7192{
7193 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7194 /* Since tracepoint locations are never duplicated with others', tracepoint
7195 locations at the same address of different tracepoints are regarded as
7196 different locations. */
7197 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7198 else
7199 return 0;
7200}
7201
2d134ed3
PA
7202/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7203 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7204 represent the same location. */
7205
7206static int
4a64f543
MS
7207breakpoint_locations_match (struct bp_location *loc1,
7208 struct bp_location *loc2)
2d134ed3 7209{
2bdf28a0
JK
7210 int hw_point1, hw_point2;
7211
7212 /* Both of them must not be in moribund_locations. */
7213 gdb_assert (loc1->owner != NULL);
7214 gdb_assert (loc2->owner != NULL);
7215
7216 hw_point1 = is_hardware_watchpoint (loc1->owner);
7217 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7218
7219 if (hw_point1 != hw_point2)
7220 return 0;
7221 else if (hw_point1)
7222 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7223 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7224 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7225 else
f1310107
TJB
7226 /* We compare bp_location.length in order to cover ranged breakpoints. */
7227 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7228 loc2->pspace->aspace, loc2->address)
7229 && loc1->length == loc2->length);
2d134ed3
PA
7230}
7231
76897487
KB
7232static void
7233breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7234 int bnum, int have_bnum)
7235{
f63fbe86
MS
7236 /* The longest string possibly returned by hex_string_custom
7237 is 50 chars. These must be at least that big for safety. */
7238 char astr1[64];
7239 char astr2[64];
76897487 7240
bb599908
PH
7241 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7242 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7243 if (have_bnum)
8a3fe4f8 7244 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7245 bnum, astr1, astr2);
7246 else
8a3fe4f8 7247 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7248}
7249
4a64f543
MS
7250/* Adjust a breakpoint's address to account for architectural
7251 constraints on breakpoint placement. Return the adjusted address.
7252 Note: Very few targets require this kind of adjustment. For most
7253 targets, this function is simply the identity function. */
76897487
KB
7254
7255static CORE_ADDR
a6d9a66e
UW
7256adjust_breakpoint_address (struct gdbarch *gdbarch,
7257 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7258{
a6d9a66e 7259 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7260 {
7261 /* Very few targets need any kind of breakpoint adjustment. */
7262 return bpaddr;
7263 }
88f7da05
KB
7264 else if (bptype == bp_watchpoint
7265 || bptype == bp_hardware_watchpoint
7266 || bptype == bp_read_watchpoint
7267 || bptype == bp_access_watchpoint
fe798b75 7268 || bptype == bp_catchpoint)
88f7da05
KB
7269 {
7270 /* Watchpoints and the various bp_catch_* eventpoints should not
7271 have their addresses modified. */
7272 return bpaddr;
7273 }
7c16b83e
PA
7274 else if (bptype == bp_single_step)
7275 {
7276 /* Single-step breakpoints should not have their addresses
7277 modified. If there's any architectural constrain that
7278 applies to this address, then it should have already been
7279 taken into account when the breakpoint was created in the
7280 first place. If we didn't do this, stepping through e.g.,
7281 Thumb-2 IT blocks would break. */
7282 return bpaddr;
7283 }
76897487
KB
7284 else
7285 {
7286 CORE_ADDR adjusted_bpaddr;
7287
7288 /* Some targets have architectural constraints on the placement
7289 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7290 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7291
7292 /* An adjusted breakpoint address can significantly alter
7293 a user's expectations. Print a warning if an adjustment
7294 is required. */
7295 if (adjusted_bpaddr != bpaddr)
7296 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7297
7298 return adjusted_bpaddr;
7299 }
7300}
7301
5625a286 7302bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7303{
5625a286 7304 bp_location *loc = this;
7cc221ef 7305
348d480f
PA
7306 gdb_assert (ops != NULL);
7307
28010a5d
PA
7308 loc->ops = ops;
7309 loc->owner = owner;
b775012e 7310 loc->cond_bytecode = NULL;
0d381245
VP
7311 loc->shlib_disabled = 0;
7312 loc->enabled = 1;
e049a4b5 7313
28010a5d 7314 switch (owner->type)
e049a4b5
DJ
7315 {
7316 case bp_breakpoint:
7c16b83e 7317 case bp_single_step:
e049a4b5
DJ
7318 case bp_until:
7319 case bp_finish:
7320 case bp_longjmp:
7321 case bp_longjmp_resume:
e2e4d78b 7322 case bp_longjmp_call_dummy:
186c406b
TT
7323 case bp_exception:
7324 case bp_exception_resume:
e049a4b5 7325 case bp_step_resume:
2c03e5be 7326 case bp_hp_step_resume:
e049a4b5
DJ
7327 case bp_watchpoint_scope:
7328 case bp_call_dummy:
aa7d318d 7329 case bp_std_terminate:
e049a4b5
DJ
7330 case bp_shlib_event:
7331 case bp_thread_event:
7332 case bp_overlay_event:
4efc6507 7333 case bp_jit_event:
0fd8e87f 7334 case bp_longjmp_master:
aa7d318d 7335 case bp_std_terminate_master:
186c406b 7336 case bp_exception_master:
0e30163f
JK
7337 case bp_gnu_ifunc_resolver:
7338 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7339 case bp_dprintf:
e049a4b5 7340 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7341 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7342 break;
7343 case bp_hardware_breakpoint:
7344 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7345 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7346 break;
7347 case bp_hardware_watchpoint:
7348 case bp_read_watchpoint:
7349 case bp_access_watchpoint:
7350 loc->loc_type = bp_loc_hardware_watchpoint;
7351 break;
7352 case bp_watchpoint:
ce78b96d 7353 case bp_catchpoint:
15c3d785
PA
7354 case bp_tracepoint:
7355 case bp_fast_tracepoint:
0fb4aa4b 7356 case bp_static_tracepoint:
e049a4b5
DJ
7357 loc->loc_type = bp_loc_other;
7358 break;
7359 default:
e2e0b3e5 7360 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7361 }
7362
f431efe5 7363 loc->refc = 1;
28010a5d
PA
7364}
7365
7366/* Allocate a struct bp_location. */
7367
7368static struct bp_location *
7369allocate_bp_location (struct breakpoint *bpt)
7370{
348d480f
PA
7371 return bpt->ops->allocate_location (bpt);
7372}
7cc221ef 7373
f431efe5
PA
7374static void
7375free_bp_location (struct bp_location *loc)
fe3f5fa8 7376{
348d480f 7377 loc->ops->dtor (loc);
4d01a485 7378 delete loc;
fe3f5fa8
VP
7379}
7380
f431efe5
PA
7381/* Increment reference count. */
7382
7383static void
7384incref_bp_location (struct bp_location *bl)
7385{
7386 ++bl->refc;
7387}
7388
7389/* Decrement reference count. If the reference count reaches 0,
7390 destroy the bp_location. Sets *BLP to NULL. */
7391
7392static void
7393decref_bp_location (struct bp_location **blp)
7394{
0807b50c
PA
7395 gdb_assert ((*blp)->refc > 0);
7396
f431efe5
PA
7397 if (--(*blp)->refc == 0)
7398 free_bp_location (*blp);
7399 *blp = NULL;
7400}
7401
346774a9 7402/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7403
346774a9
PA
7404static void
7405add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7406{
346774a9 7407 struct breakpoint *b1;
c906108c 7408
346774a9
PA
7409 /* Add this breakpoint to the end of the chain so that a list of
7410 breakpoints will come out in order of increasing numbers. */
7411
7412 b1 = breakpoint_chain;
7413 if (b1 == 0)
7414 breakpoint_chain = b;
7415 else
7416 {
7417 while (b1->next)
7418 b1 = b1->next;
7419 b1->next = b;
7420 }
7421}
7422
7423/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7424
7425static void
7426init_raw_breakpoint_without_location (struct breakpoint *b,
7427 struct gdbarch *gdbarch,
28010a5d 7428 enum bptype bptype,
c0a91b2b 7429 const struct breakpoint_ops *ops)
346774a9 7430{
348d480f
PA
7431 gdb_assert (ops != NULL);
7432
28010a5d 7433 b->ops = ops;
4d28f7a8 7434 b->type = bptype;
a6d9a66e 7435 b->gdbarch = gdbarch;
c906108c
SS
7436 b->language = current_language->la_language;
7437 b->input_radix = input_radix;
d0fb5eae 7438 b->related_breakpoint = b;
346774a9
PA
7439}
7440
7441/* Helper to set_raw_breakpoint below. Creates a breakpoint
7442 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7443
7444static struct breakpoint *
7445set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7446 enum bptype bptype,
c0a91b2b 7447 const struct breakpoint_ops *ops)
346774a9 7448{
4d01a485 7449 struct breakpoint *b = new breakpoint ();
346774a9 7450
348d480f 7451 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7452 add_to_breakpoint_chain (b);
0d381245
VP
7453 return b;
7454}
7455
0e30163f
JK
7456/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7457 resolutions should be made as the user specified the location explicitly
7458 enough. */
7459
0d381245 7460static void
0e30163f 7461set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7462{
2bdf28a0
JK
7463 gdb_assert (loc->owner != NULL);
7464
0d381245 7465 if (loc->owner->type == bp_breakpoint
1042e4c0 7466 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7467 || is_tracepoint (loc->owner))
0d381245 7468 {
0e30163f 7469 int is_gnu_ifunc;
2c02bd72 7470 const char *function_name;
6a3a010b 7471 CORE_ADDR func_addr;
0e30163f 7472
2c02bd72 7473 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7474 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7475
7476 if (is_gnu_ifunc && !explicit_loc)
7477 {
7478 struct breakpoint *b = loc->owner;
7479
7480 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7481 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7482 &loc->requested_address))
7483 {
7484 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7485 loc->address = adjust_breakpoint_address (loc->gdbarch,
7486 loc->requested_address,
7487 b->type);
7488 }
7489 else if (b->type == bp_breakpoint && b->loc == loc
7490 && loc->next == NULL && b->related_breakpoint == b)
7491 {
7492 /* Create only the whole new breakpoint of this type but do not
7493 mess more complicated breakpoints with multiple locations. */
7494 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7495 /* Remember the resolver's address for use by the return
7496 breakpoint. */
7497 loc->related_address = func_addr;
0e30163f
JK
7498 }
7499 }
7500
2c02bd72
DE
7501 if (function_name)
7502 loc->function_name = xstrdup (function_name);
0d381245
VP
7503 }
7504}
7505
a6d9a66e 7506/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7507struct gdbarch *
a6d9a66e
UW
7508get_sal_arch (struct symtab_and_line sal)
7509{
7510 if (sal.section)
7511 return get_objfile_arch (sal.section->objfile);
7512 if (sal.symtab)
eb822aa6 7513 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7514
7515 return NULL;
7516}
7517
346774a9
PA
7518/* Low level routine for partially initializing a breakpoint of type
7519 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7520 file name, and line number are provided by SAL.
0d381245
VP
7521
7522 It is expected that the caller will complete the initialization of
7523 the newly created breakpoint struct as well as output any status
c56053d2 7524 information regarding the creation of a new breakpoint. */
0d381245 7525
346774a9
PA
7526static void
7527init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7528 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7529 const struct breakpoint_ops *ops)
0d381245 7530{
28010a5d 7531 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7532
3742cc8b 7533 add_location_to_breakpoint (b, &sal);
0d381245 7534
6c95b8df
PA
7535 if (bptype != bp_catchpoint)
7536 gdb_assert (sal.pspace != NULL);
7537
f8eba3c6
TT
7538 /* Store the program space that was used to set the breakpoint,
7539 except for ordinary breakpoints, which are independent of the
7540 program space. */
7541 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7542 b->pspace = sal.pspace;
346774a9 7543}
c906108c 7544
346774a9
PA
7545/* set_raw_breakpoint is a low level routine for allocating and
7546 partially initializing a breakpoint of type BPTYPE. The newly
7547 created breakpoint's address, section, source file name, and line
7548 number are provided by SAL. The newly created and partially
7549 initialized breakpoint is added to the breakpoint chain and
7550 is also returned as the value of this function.
7551
7552 It is expected that the caller will complete the initialization of
7553 the newly created breakpoint struct as well as output any status
7554 information regarding the creation of a new breakpoint. In
7555 particular, set_raw_breakpoint does NOT set the breakpoint
7556 number! Care should be taken to not allow an error to occur
7557 prior to completing the initialization of the breakpoint. If this
7558 should happen, a bogus breakpoint will be left on the chain. */
7559
7560struct breakpoint *
7561set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7562 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7563 const struct breakpoint_ops *ops)
346774a9 7564{
4d01a485 7565 struct breakpoint *b = new breakpoint ();
346774a9 7566
348d480f 7567 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7568 add_to_breakpoint_chain (b);
c906108c
SS
7569 return b;
7570}
7571
53a5351d 7572/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7573 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7574 initiated the operation. */
c906108c
SS
7575
7576void
186c406b 7577set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7578{
35df4500 7579 struct breakpoint *b, *b_tmp;
5d5658a1 7580 int thread = tp->global_num;
0fd8e87f
UW
7581
7582 /* To avoid having to rescan all objfile symbols at every step,
7583 we maintain a list of continually-inserted but always disabled
7584 longjmp "master" breakpoints. Here, we simply create momentary
7585 clones of those and enable them for the requested thread. */
35df4500 7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7587 if (b->pspace == current_program_space
186c406b
TT
7588 && (b->type == bp_longjmp_master
7589 || b->type == bp_exception_master))
0fd8e87f 7590 {
06edf0c0
PA
7591 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7592 struct breakpoint *clone;
cc59ec59 7593
e2e4d78b
JK
7594 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7595 after their removal. */
06edf0c0 7596 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7597 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7598 clone->thread = thread;
7599 }
186c406b
TT
7600
7601 tp->initiating_frame = frame;
c906108c
SS
7602}
7603
611c83ae 7604/* Delete all longjmp breakpoints from THREAD. */
c906108c 7605void
611c83ae 7606delete_longjmp_breakpoint (int thread)
c906108c 7607{
35df4500 7608 struct breakpoint *b, *b_tmp;
c906108c 7609
35df4500 7610 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7611 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7612 {
7613 if (b->thread == thread)
7614 delete_breakpoint (b);
7615 }
c906108c
SS
7616}
7617
f59f708a
PA
7618void
7619delete_longjmp_breakpoint_at_next_stop (int thread)
7620{
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_longjmp || b->type == bp_exception)
7625 {
7626 if (b->thread == thread)
7627 b->disposition = disp_del_at_next_stop;
7628 }
7629}
7630
e2e4d78b
JK
7631/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7632 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7633 pointer to any of them. Return NULL if this system cannot place longjmp
7634 breakpoints. */
7635
7636struct breakpoint *
7637set_longjmp_breakpoint_for_call_dummy (void)
7638{
7639 struct breakpoint *b, *retval = NULL;
7640
7641 ALL_BREAKPOINTS (b)
7642 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7643 {
7644 struct breakpoint *new_b;
7645
7646 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7647 &momentary_breakpoint_ops,
7648 1);
5d5658a1 7649 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7650
7651 /* Link NEW_B into the chain of RETVAL breakpoints. */
7652
7653 gdb_assert (new_b->related_breakpoint == new_b);
7654 if (retval == NULL)
7655 retval = new_b;
7656 new_b->related_breakpoint = retval;
7657 while (retval->related_breakpoint != new_b->related_breakpoint)
7658 retval = retval->related_breakpoint;
7659 retval->related_breakpoint = new_b;
7660 }
7661
7662 return retval;
7663}
7664
7665/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7666 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7667 stack.
7668
7669 You should call this function only at places where it is safe to currently
7670 unwind the whole stack. Failed stack unwind would discard live dummy
7671 frames. */
7672
7673void
b67a2c6f 7674check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7675{
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7679 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7680 {
7681 struct breakpoint *dummy_b = b->related_breakpoint;
7682
7683 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7684 dummy_b = dummy_b->related_breakpoint;
7685 if (dummy_b->type != bp_call_dummy
7686 || frame_find_by_id (dummy_b->frame_id) != NULL)
7687 continue;
7688
b67a2c6f 7689 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7690
7691 while (b->related_breakpoint != b)
7692 {
7693 if (b_tmp == b->related_breakpoint)
7694 b_tmp = b->related_breakpoint->next;
7695 delete_breakpoint (b->related_breakpoint);
7696 }
7697 delete_breakpoint (b);
7698 }
7699}
7700
1900040c
MS
7701void
7702enable_overlay_breakpoints (void)
7703{
52f0bd74 7704 struct breakpoint *b;
1900040c
MS
7705
7706 ALL_BREAKPOINTS (b)
7707 if (b->type == bp_overlay_event)
7708 {
7709 b->enable_state = bp_enabled;
44702360 7710 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7711 overlay_events_enabled = 1;
1900040c
MS
7712 }
7713}
7714
7715void
7716disable_overlay_breakpoints (void)
7717{
52f0bd74 7718 struct breakpoint *b;
1900040c
MS
7719
7720 ALL_BREAKPOINTS (b)
7721 if (b->type == bp_overlay_event)
7722 {
7723 b->enable_state = bp_disabled;
44702360 7724 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7725 overlay_events_enabled = 0;
1900040c
MS
7726 }
7727}
7728
aa7d318d
TT
7729/* Set an active std::terminate breakpoint for each std::terminate
7730 master breakpoint. */
7731void
7732set_std_terminate_breakpoint (void)
7733{
35df4500 7734 struct breakpoint *b, *b_tmp;
aa7d318d 7735
35df4500 7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7737 if (b->pspace == current_program_space
7738 && b->type == bp_std_terminate_master)
7739 {
06edf0c0 7740 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7741 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7742 }
7743}
7744
7745/* Delete all the std::terminate breakpoints. */
7746void
7747delete_std_terminate_breakpoint (void)
7748{
35df4500 7749 struct breakpoint *b, *b_tmp;
aa7d318d 7750
35df4500 7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7752 if (b->type == bp_std_terminate)
7753 delete_breakpoint (b);
7754}
7755
c4093a6a 7756struct breakpoint *
a6d9a66e 7757create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7758{
7759 struct breakpoint *b;
c4093a6a 7760
06edf0c0
PA
7761 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7762 &internal_breakpoint_ops);
7763
b5de0fa7 7764 b->enable_state = bp_enabled;
f00aae0f 7765 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7766 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7767
44702360 7768 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7769
c4093a6a
JM
7770 return b;
7771}
7772
0101ce28
JJ
7773struct lang_and_radix
7774 {
7775 enum language lang;
7776 int radix;
7777 };
7778
4efc6507
DE
7779/* Create a breakpoint for JIT code registration and unregistration. */
7780
7781struct breakpoint *
7782create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7783{
2a7f3dff
PA
7784 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7785 &internal_breakpoint_ops);
4efc6507 7786}
0101ce28 7787
03673fc7
PP
7788/* Remove JIT code registration and unregistration breakpoint(s). */
7789
7790void
7791remove_jit_event_breakpoints (void)
7792{
7793 struct breakpoint *b, *b_tmp;
7794
7795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7796 if (b->type == bp_jit_event
7797 && b->loc->pspace == current_program_space)
7798 delete_breakpoint (b);
7799}
7800
cae688ec
JJ
7801void
7802remove_solib_event_breakpoints (void)
7803{
35df4500 7804 struct breakpoint *b, *b_tmp;
cae688ec 7805
35df4500 7806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7807 if (b->type == bp_shlib_event
7808 && b->loc->pspace == current_program_space)
cae688ec
JJ
7809 delete_breakpoint (b);
7810}
7811
f37f681c
PA
7812/* See breakpoint.h. */
7813
7814void
7815remove_solib_event_breakpoints_at_next_stop (void)
7816{
7817 struct breakpoint *b, *b_tmp;
7818
7819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7820 if (b->type == bp_shlib_event
7821 && b->loc->pspace == current_program_space)
7822 b->disposition = disp_del_at_next_stop;
7823}
7824
04086b45
PA
7825/* Helper for create_solib_event_breakpoint /
7826 create_and_insert_solib_event_breakpoint. Allows specifying which
7827 INSERT_MODE to pass through to update_global_location_list. */
7828
7829static struct breakpoint *
7830create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7831 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7832{
7833 struct breakpoint *b;
7834
06edf0c0
PA
7835 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7836 &internal_breakpoint_ops);
04086b45 7837 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7838 return b;
7839}
7840
04086b45
PA
7841struct breakpoint *
7842create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7843{
7844 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7845}
7846
f37f681c
PA
7847/* See breakpoint.h. */
7848
7849struct breakpoint *
7850create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7851{
7852 struct breakpoint *b;
7853
04086b45
PA
7854 /* Explicitly tell update_global_location_list to insert
7855 locations. */
7856 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7857 if (!b->loc->inserted)
7858 {
7859 delete_breakpoint (b);
7860 return NULL;
7861 }
7862 return b;
7863}
7864
cae688ec
JJ
7865/* Disable any breakpoints that are on code in shared libraries. Only
7866 apply to enabled breakpoints, disabled ones can just stay disabled. */
7867
7868void
cb851954 7869disable_breakpoints_in_shlibs (void)
cae688ec 7870{
876fa593 7871 struct bp_location *loc, **locp_tmp;
cae688ec 7872
876fa593 7873 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7874 {
2bdf28a0 7875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7876 struct breakpoint *b = loc->owner;
2bdf28a0 7877
4a64f543
MS
7878 /* We apply the check to all breakpoints, including disabled for
7879 those with loc->duplicate set. This is so that when breakpoint
7880 becomes enabled, or the duplicate is removed, gdb will try to
7881 insert all breakpoints. If we don't set shlib_disabled here,
7882 we'll try to insert those breakpoints and fail. */
1042e4c0 7883 if (((b->type == bp_breakpoint)
508ccb1f 7884 || (b->type == bp_jit_event)
1042e4c0 7885 || (b->type == bp_hardware_breakpoint)
d77f58be 7886 || (is_tracepoint (b)))
6c95b8df 7887 && loc->pspace == current_program_space
0d381245 7888 && !loc->shlib_disabled
6c95b8df 7889 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7890 )
0d381245
VP
7891 {
7892 loc->shlib_disabled = 1;
7893 }
cae688ec
JJ
7894 }
7895}
7896
63644780
NB
7897/* Disable any breakpoints and tracepoints that are in SOLIB upon
7898 notification of unloaded_shlib. Only apply to enabled breakpoints,
7899 disabled ones can just stay disabled. */
84acb35a 7900
75149521 7901static void
84acb35a
JJ
7902disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7903{
876fa593 7904 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7905 int disabled_shlib_breaks = 0;
7906
876fa593 7907 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7908 {
2bdf28a0 7909 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7910 struct breakpoint *b = loc->owner;
cc59ec59 7911
1e4d1764 7912 if (solib->pspace == loc->pspace
e2dd7057 7913 && !loc->shlib_disabled
1e4d1764
YQ
7914 && (((b->type == bp_breakpoint
7915 || b->type == bp_jit_event
7916 || b->type == bp_hardware_breakpoint)
7917 && (loc->loc_type == bp_loc_hardware_breakpoint
7918 || loc->loc_type == bp_loc_software_breakpoint))
7919 || is_tracepoint (b))
e2dd7057 7920 && solib_contains_address_p (solib, loc->address))
84acb35a 7921 {
e2dd7057
PP
7922 loc->shlib_disabled = 1;
7923 /* At this point, we cannot rely on remove_breakpoint
7924 succeeding so we must mark the breakpoint as not inserted
7925 to prevent future errors occurring in remove_breakpoints. */
7926 loc->inserted = 0;
8d3788bd
VP
7927
7928 /* This may cause duplicate notifications for the same breakpoint. */
7929 observer_notify_breakpoint_modified (b);
7930
e2dd7057
PP
7931 if (!disabled_shlib_breaks)
7932 {
7933 target_terminal_ours_for_output ();
3e43a32a
MS
7934 warning (_("Temporarily disabling breakpoints "
7935 "for unloaded shared library \"%s\""),
e2dd7057 7936 solib->so_name);
84acb35a 7937 }
e2dd7057 7938 disabled_shlib_breaks = 1;
84acb35a
JJ
7939 }
7940 }
84acb35a
JJ
7941}
7942
63644780
NB
7943/* Disable any breakpoints and tracepoints in OBJFILE upon
7944 notification of free_objfile. Only apply to enabled breakpoints,
7945 disabled ones can just stay disabled. */
7946
7947static void
7948disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7949{
7950 struct breakpoint *b;
7951
7952 if (objfile == NULL)
7953 return;
7954
d03de421
PA
7955 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7956 managed by the user with add-symbol-file/remove-symbol-file.
7957 Similarly to how breakpoints in shared libraries are handled in
7958 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7959 shlib_disabled so they end up uninserted on the next global
7960 location list update. Shared libraries not loaded by the user
7961 aren't handled here -- they're already handled in
7962 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7963 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7964 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7965 main objfile). */
7966 if ((objfile->flags & OBJF_SHARED) == 0
7967 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7968 return;
7969
7970 ALL_BREAKPOINTS (b)
7971 {
7972 struct bp_location *loc;
7973 int bp_modified = 0;
7974
7975 if (!is_breakpoint (b) && !is_tracepoint (b))
7976 continue;
7977
7978 for (loc = b->loc; loc != NULL; loc = loc->next)
7979 {
7980 CORE_ADDR loc_addr = loc->address;
7981
7982 if (loc->loc_type != bp_loc_hardware_breakpoint
7983 && loc->loc_type != bp_loc_software_breakpoint)
7984 continue;
7985
7986 if (loc->shlib_disabled != 0)
7987 continue;
7988
7989 if (objfile->pspace != loc->pspace)
7990 continue;
7991
7992 if (loc->loc_type != bp_loc_hardware_breakpoint
7993 && loc->loc_type != bp_loc_software_breakpoint)
7994 continue;
7995
7996 if (is_addr_in_objfile (loc_addr, objfile))
7997 {
7998 loc->shlib_disabled = 1;
08351840
PA
7999 /* At this point, we don't know whether the object was
8000 unmapped from the inferior or not, so leave the
8001 inserted flag alone. We'll handle failure to
8002 uninsert quietly, in case the object was indeed
8003 unmapped. */
63644780
NB
8004
8005 mark_breakpoint_location_modified (loc);
8006
8007 bp_modified = 1;
8008 }
8009 }
8010
8011 if (bp_modified)
8012 observer_notify_breakpoint_modified (b);
8013 }
8014}
8015
ce78b96d
JB
8016/* FORK & VFORK catchpoints. */
8017
e29a4733
PA
8018/* An instance of this type is used to represent a fork or vfork
8019 catchpoint. It includes a "struct breakpoint" as a kind of base
8020 class; users downcast to "struct breakpoint *" when needed. A
8021 breakpoint is really of this type iff its ops pointer points to
8022 CATCH_FORK_BREAKPOINT_OPS. */
8023
8024struct fork_catchpoint
8025{
8026 /* The base class. */
8027 struct breakpoint base;
8028
8029 /* Process id of a child process whose forking triggered this
8030 catchpoint. This field is only valid immediately after this
8031 catchpoint has triggered. */
8032 ptid_t forked_inferior_pid;
8033};
8034
4a64f543
MS
8035/* Implement the "insert" breakpoint_ops method for fork
8036 catchpoints. */
ce78b96d 8037
77b06cd7
TJB
8038static int
8039insert_catch_fork (struct bp_location *bl)
ce78b96d 8040{
dfd4cc63 8041 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8042}
8043
4a64f543
MS
8044/* Implement the "remove" breakpoint_ops method for fork
8045 catchpoints. */
ce78b96d
JB
8046
8047static int
73971819 8048remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8049{
dfd4cc63 8050 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8051}
8052
8053/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8054 catchpoints. */
8055
8056static int
f1310107 8057breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8058 struct address_space *aspace, CORE_ADDR bp_addr,
8059 const struct target_waitstatus *ws)
ce78b96d 8060{
e29a4733
PA
8061 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8062
f90263c1
TT
8063 if (ws->kind != TARGET_WAITKIND_FORKED)
8064 return 0;
8065
8066 c->forked_inferior_pid = ws->value.related_pid;
8067 return 1;
ce78b96d
JB
8068}
8069
4a64f543
MS
8070/* Implement the "print_it" breakpoint_ops method for fork
8071 catchpoints. */
ce78b96d
JB
8072
8073static enum print_stop_action
348d480f 8074print_it_catch_fork (bpstat bs)
ce78b96d 8075{
36dfb11c 8076 struct ui_out *uiout = current_uiout;
348d480f
PA
8077 struct breakpoint *b = bs->breakpoint_at;
8078 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8079
ce78b96d 8080 annotate_catchpoint (b->number);
f303dbd6 8081 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8082 if (b->disposition == disp_del)
112e8700 8083 uiout->text ("Temporary catchpoint ");
36dfb11c 8084 else
112e8700
SM
8085 uiout->text ("Catchpoint ");
8086 if (uiout->is_mi_like_p ())
36dfb11c 8087 {
112e8700
SM
8088 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8089 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8090 }
112e8700
SM
8091 uiout->field_int ("bkptno", b->number);
8092 uiout->text (" (forked process ");
8093 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8094 uiout->text ("), ");
ce78b96d
JB
8095 return PRINT_SRC_AND_LOC;
8096}
8097
4a64f543
MS
8098/* Implement the "print_one" breakpoint_ops method for fork
8099 catchpoints. */
ce78b96d
JB
8100
8101static void
a6d9a66e 8102print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8103{
e29a4733 8104 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8105 struct value_print_options opts;
79a45e25 8106 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8107
8108 get_user_print_options (&opts);
8109
4a64f543
MS
8110 /* Field 4, the address, is omitted (which makes the columns not
8111 line up too nicely with the headers, but the effect is relatively
8112 readable). */
79a45b7d 8113 if (opts.addressprint)
112e8700 8114 uiout->field_skip ("addr");
ce78b96d 8115 annotate_field (5);
112e8700 8116 uiout->text ("fork");
e29a4733 8117 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8118 {
112e8700
SM
8119 uiout->text (", process ");
8120 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8121 uiout->spaces (1);
ce78b96d 8122 }
8ac3646f 8123
112e8700
SM
8124 if (uiout->is_mi_like_p ())
8125 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8126}
8127
8128/* Implement the "print_mention" breakpoint_ops method for fork
8129 catchpoints. */
8130
8131static void
8132print_mention_catch_fork (struct breakpoint *b)
8133{
8134 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8135}
8136
6149aea9
PA
8137/* Implement the "print_recreate" breakpoint_ops method for fork
8138 catchpoints. */
8139
8140static void
8141print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8142{
8143 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8144 print_recreate_thread (b, fp);
6149aea9
PA
8145}
8146
ce78b96d
JB
8147/* The breakpoint_ops structure to be used in fork catchpoints. */
8148
2060206e 8149static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8150
4a64f543
MS
8151/* Implement the "insert" breakpoint_ops method for vfork
8152 catchpoints. */
ce78b96d 8153
77b06cd7
TJB
8154static int
8155insert_catch_vfork (struct bp_location *bl)
ce78b96d 8156{
dfd4cc63 8157 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8158}
8159
4a64f543
MS
8160/* Implement the "remove" breakpoint_ops method for vfork
8161 catchpoints. */
ce78b96d
JB
8162
8163static int
73971819 8164remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8165{
dfd4cc63 8166 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8167}
8168
8169/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8170 catchpoints. */
8171
8172static int
f1310107 8173breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8174 struct address_space *aspace, CORE_ADDR bp_addr,
8175 const struct target_waitstatus *ws)
ce78b96d 8176{
e29a4733
PA
8177 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8178
f90263c1
TT
8179 if (ws->kind != TARGET_WAITKIND_VFORKED)
8180 return 0;
8181
8182 c->forked_inferior_pid = ws->value.related_pid;
8183 return 1;
ce78b96d
JB
8184}
8185
4a64f543
MS
8186/* Implement the "print_it" breakpoint_ops method for vfork
8187 catchpoints. */
ce78b96d
JB
8188
8189static enum print_stop_action
348d480f 8190print_it_catch_vfork (bpstat bs)
ce78b96d 8191{
36dfb11c 8192 struct ui_out *uiout = current_uiout;
348d480f 8193 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8194 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8195
ce78b96d 8196 annotate_catchpoint (b->number);
f303dbd6 8197 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8198 if (b->disposition == disp_del)
112e8700 8199 uiout->text ("Temporary catchpoint ");
36dfb11c 8200 else
112e8700
SM
8201 uiout->text ("Catchpoint ");
8202 if (uiout->is_mi_like_p ())
36dfb11c 8203 {
112e8700
SM
8204 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8205 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8206 }
112e8700
SM
8207 uiout->field_int ("bkptno", b->number);
8208 uiout->text (" (vforked process ");
8209 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8210 uiout->text ("), ");
ce78b96d
JB
8211 return PRINT_SRC_AND_LOC;
8212}
8213
4a64f543
MS
8214/* Implement the "print_one" breakpoint_ops method for vfork
8215 catchpoints. */
ce78b96d
JB
8216
8217static void
a6d9a66e 8218print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8219{
e29a4733 8220 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8221 struct value_print_options opts;
79a45e25 8222 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8223
8224 get_user_print_options (&opts);
4a64f543
MS
8225 /* Field 4, the address, is omitted (which makes the columns not
8226 line up too nicely with the headers, but the effect is relatively
8227 readable). */
79a45b7d 8228 if (opts.addressprint)
112e8700 8229 uiout->field_skip ("addr");
ce78b96d 8230 annotate_field (5);
112e8700 8231 uiout->text ("vfork");
e29a4733 8232 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8233 {
112e8700
SM
8234 uiout->text (", process ");
8235 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8236 uiout->spaces (1);
ce78b96d 8237 }
8ac3646f 8238
112e8700
SM
8239 if (uiout->is_mi_like_p ())
8240 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8241}
8242
8243/* Implement the "print_mention" breakpoint_ops method for vfork
8244 catchpoints. */
8245
8246static void
8247print_mention_catch_vfork (struct breakpoint *b)
8248{
8249 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8250}
8251
6149aea9
PA
8252/* Implement the "print_recreate" breakpoint_ops method for vfork
8253 catchpoints. */
8254
8255static void
8256print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8257{
8258 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8259 print_recreate_thread (b, fp);
6149aea9
PA
8260}
8261
ce78b96d
JB
8262/* The breakpoint_ops structure to be used in vfork catchpoints. */
8263
2060206e 8264static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8265
edcc5120
TT
8266/* An instance of this type is used to represent an solib catchpoint.
8267 It includes a "struct breakpoint" as a kind of base class; users
8268 downcast to "struct breakpoint *" when needed. A breakpoint is
8269 really of this type iff its ops pointer points to
8270 CATCH_SOLIB_BREAKPOINT_OPS. */
8271
8272struct solib_catchpoint
8273{
8274 /* The base class. */
8275 struct breakpoint base;
8276
8277 /* True for "catch load", false for "catch unload". */
8278 unsigned char is_load;
8279
8280 /* Regular expression to match, if any. COMPILED is only valid when
8281 REGEX is non-NULL. */
8282 char *regex;
8283 regex_t compiled;
8284};
8285
8286static void
8287dtor_catch_solib (struct breakpoint *b)
8288{
8289 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8290
8291 if (self->regex)
8292 regfree (&self->compiled);
8293 xfree (self->regex);
8294
8295 base_breakpoint_ops.dtor (b);
8296}
8297
8298static int
8299insert_catch_solib (struct bp_location *ignore)
8300{
8301 return 0;
8302}
8303
8304static int
73971819 8305remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8306{
8307 return 0;
8308}
8309
8310static int
8311breakpoint_hit_catch_solib (const struct bp_location *bl,
8312 struct address_space *aspace,
8313 CORE_ADDR bp_addr,
8314 const struct target_waitstatus *ws)
8315{
8316 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8317 struct breakpoint *other;
8318
8319 if (ws->kind == TARGET_WAITKIND_LOADED)
8320 return 1;
8321
8322 ALL_BREAKPOINTS (other)
8323 {
8324 struct bp_location *other_bl;
8325
8326 if (other == bl->owner)
8327 continue;
8328
8329 if (other->type != bp_shlib_event)
8330 continue;
8331
8332 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8333 continue;
8334
8335 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8336 {
8337 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8338 return 1;
8339 }
8340 }
8341
8342 return 0;
8343}
8344
8345static void
8346check_status_catch_solib (struct bpstats *bs)
8347{
8348 struct solib_catchpoint *self
8349 = (struct solib_catchpoint *) bs->breakpoint_at;
8350 int ix;
8351
8352 if (self->is_load)
8353 {
8354 struct so_list *iter;
8355
8356 for (ix = 0;
8357 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8358 ix, iter);
8359 ++ix)
8360 {
8361 if (!self->regex
8362 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8363 return;
8364 }
8365 }
8366 else
8367 {
8368 char *iter;
8369
8370 for (ix = 0;
8371 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8372 ix, iter);
8373 ++ix)
8374 {
8375 if (!self->regex
8376 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8377 return;
8378 }
8379 }
8380
8381 bs->stop = 0;
8382 bs->print_it = print_it_noop;
8383}
8384
8385static enum print_stop_action
8386print_it_catch_solib (bpstat bs)
8387{
8388 struct breakpoint *b = bs->breakpoint_at;
8389 struct ui_out *uiout = current_uiout;
8390
8391 annotate_catchpoint (b->number);
f303dbd6 8392 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8393 if (b->disposition == disp_del)
112e8700 8394 uiout->text ("Temporary catchpoint ");
edcc5120 8395 else
112e8700
SM
8396 uiout->text ("Catchpoint ");
8397 uiout->field_int ("bkptno", b->number);
8398 uiout->text ("\n");
8399 if (uiout->is_mi_like_p ())
8400 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8401 print_solib_event (1);
8402 return PRINT_SRC_AND_LOC;
8403}
8404
8405static void
8406print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8407{
8408 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8409 struct value_print_options opts;
8410 struct ui_out *uiout = current_uiout;
8411 char *msg;
8412
8413 get_user_print_options (&opts);
8414 /* Field 4, the address, is omitted (which makes the columns not
8415 line up too nicely with the headers, but the effect is relatively
8416 readable). */
8417 if (opts.addressprint)
8418 {
8419 annotate_field (4);
112e8700 8420 uiout->field_skip ("addr");
edcc5120
TT
8421 }
8422
8423 annotate_field (5);
8424 if (self->is_load)
8425 {
8426 if (self->regex)
8427 msg = xstrprintf (_("load of library matching %s"), self->regex);
8428 else
8429 msg = xstrdup (_("load of library"));
8430 }
8431 else
8432 {
8433 if (self->regex)
8434 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8435 else
8436 msg = xstrdup (_("unload of library"));
8437 }
112e8700 8438 uiout->field_string ("what", msg);
edcc5120 8439 xfree (msg);
8ac3646f 8440
112e8700
SM
8441 if (uiout->is_mi_like_p ())
8442 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8443}
8444
8445static void
8446print_mention_catch_solib (struct breakpoint *b)
8447{
8448 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8449
8450 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8451 self->is_load ? "load" : "unload");
8452}
8453
8454static void
8455print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8456{
8457 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8458
8459 fprintf_unfiltered (fp, "%s %s",
8460 b->disposition == disp_del ? "tcatch" : "catch",
8461 self->is_load ? "load" : "unload");
8462 if (self->regex)
8463 fprintf_unfiltered (fp, " %s", self->regex);
8464 fprintf_unfiltered (fp, "\n");
8465}
8466
8467static struct breakpoint_ops catch_solib_breakpoint_ops;
8468
91985142
MG
8469/* Shared helper function (MI and CLI) for creating and installing
8470 a shared object event catchpoint. If IS_LOAD is non-zero then
8471 the events to be caught are load events, otherwise they are
8472 unload events. If IS_TEMP is non-zero the catchpoint is a
8473 temporary one. If ENABLED is non-zero the catchpoint is
8474 created in an enabled state. */
edcc5120 8475
91985142 8476void
a121b7c1 8477add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8478{
8479 struct solib_catchpoint *c;
8480 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8481 struct cleanup *cleanup;
8482
edcc5120
TT
8483 if (!arg)
8484 arg = "";
a121b7c1 8485 arg = skip_spaces_const (arg);
edcc5120 8486
4d01a485 8487 c = new solib_catchpoint ();
edcc5120
TT
8488 cleanup = make_cleanup (xfree, c);
8489
8490 if (*arg != '\0')
8491 {
8492 int errcode;
8493
8494 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8495 if (errcode != 0)
8496 {
8497 char *err = get_regcomp_error (errcode, &c->compiled);
8498
8499 make_cleanup (xfree, err);
8500 error (_("Invalid regexp (%s): %s"), err, arg);
8501 }
8502 c->regex = xstrdup (arg);
8503 }
8504
8505 c->is_load = is_load;
91985142 8506 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8507 &catch_solib_breakpoint_ops);
8508
91985142
MG
8509 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8510
edcc5120
TT
8511 discard_cleanups (cleanup);
8512 install_breakpoint (0, &c->base, 1);
8513}
8514
91985142
MG
8515/* A helper function that does all the work for "catch load" and
8516 "catch unload". */
8517
8518static void
8519catch_load_or_unload (char *arg, int from_tty, int is_load,
8520 struct cmd_list_element *command)
8521{
8522 int tempflag;
8523 const int enabled = 1;
8524
8525 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8526
8527 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8528}
8529
edcc5120
TT
8530static void
8531catch_load_command_1 (char *arg, int from_tty,
8532 struct cmd_list_element *command)
8533{
8534 catch_load_or_unload (arg, from_tty, 1, command);
8535}
8536
8537static void
8538catch_unload_command_1 (char *arg, int from_tty,
8539 struct cmd_list_element *command)
8540{
8541 catch_load_or_unload (arg, from_tty, 0, command);
8542}
8543
346774a9
PA
8544/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8545 is non-zero, then make the breakpoint temporary. If COND_STRING is
8546 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8547 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8548
ab04a2af 8549void
346774a9
PA
8550init_catchpoint (struct breakpoint *b,
8551 struct gdbarch *gdbarch, int tempflag,
63160a43 8552 const char *cond_string,
c0a91b2b 8553 const struct breakpoint_ops *ops)
c906108c 8554{
c5aa993b 8555 struct symtab_and_line sal;
346774a9 8556
fe39c653 8557 init_sal (&sal);
6c95b8df 8558 sal.pspace = current_program_space;
c5aa993b 8559
28010a5d 8560 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8561
1b36a34b 8562 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8563 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8564}
8565
28010a5d 8566void
3ea46bff 8567install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8568{
8569 add_to_breakpoint_chain (b);
3a5c3e22 8570 set_breakpoint_number (internal, b);
558a9d82
YQ
8571 if (is_tracepoint (b))
8572 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8573 if (!internal)
8574 mention (b);
c56053d2 8575 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8576
8577 if (update_gll)
44702360 8578 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8579}
8580
9b70b993 8581static void
a6d9a66e 8582create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8583 int tempflag, const char *cond_string,
c0a91b2b 8584 const struct breakpoint_ops *ops)
c906108c 8585{
4d01a485 8586 struct fork_catchpoint *c = new fork_catchpoint ();
ce78b96d 8587
e29a4733
PA
8588 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8589
8590 c->forked_inferior_pid = null_ptid;
8591
3ea46bff 8592 install_breakpoint (0, &c->base, 1);
c906108c
SS
8593}
8594
fe798b75
JB
8595/* Exec catchpoints. */
8596
b4d90040
PA
8597/* An instance of this type is used to represent an exec catchpoint.
8598 It includes a "struct breakpoint" as a kind of base class; users
8599 downcast to "struct breakpoint *" when needed. A breakpoint is
8600 really of this type iff its ops pointer points to
8601 CATCH_EXEC_BREAKPOINT_OPS. */
8602
8603struct exec_catchpoint
8604{
8605 /* The base class. */
8606 struct breakpoint base;
8607
8608 /* Filename of a program whose exec triggered this catchpoint.
8609 This field is only valid immediately after this catchpoint has
8610 triggered. */
8611 char *exec_pathname;
8612};
8613
8614/* Implement the "dtor" breakpoint_ops method for exec
8615 catchpoints. */
8616
8617static void
8618dtor_catch_exec (struct breakpoint *b)
8619{
8620 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8621
8622 xfree (c->exec_pathname);
348d480f 8623
2060206e 8624 base_breakpoint_ops.dtor (b);
b4d90040
PA
8625}
8626
77b06cd7
TJB
8627static int
8628insert_catch_exec (struct bp_location *bl)
c906108c 8629{
dfd4cc63 8630 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8631}
c906108c 8632
fe798b75 8633static int
73971819 8634remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8635{
dfd4cc63 8636 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8637}
c906108c 8638
fe798b75 8639static int
f1310107 8640breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8641 struct address_space *aspace, CORE_ADDR bp_addr,
8642 const struct target_waitstatus *ws)
fe798b75 8643{
b4d90040
PA
8644 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8645
f90263c1
TT
8646 if (ws->kind != TARGET_WAITKIND_EXECD)
8647 return 0;
8648
8649 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8650 return 1;
fe798b75 8651}
c906108c 8652
fe798b75 8653static enum print_stop_action
348d480f 8654print_it_catch_exec (bpstat bs)
fe798b75 8655{
36dfb11c 8656 struct ui_out *uiout = current_uiout;
348d480f 8657 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8658 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8659
fe798b75 8660 annotate_catchpoint (b->number);
f303dbd6 8661 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8662 if (b->disposition == disp_del)
112e8700 8663 uiout->text ("Temporary catchpoint ");
36dfb11c 8664 else
112e8700
SM
8665 uiout->text ("Catchpoint ");
8666 if (uiout->is_mi_like_p ())
36dfb11c 8667 {
112e8700
SM
8668 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8669 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8670 }
112e8700
SM
8671 uiout->field_int ("bkptno", b->number);
8672 uiout->text (" (exec'd ");
8673 uiout->field_string ("new-exec", c->exec_pathname);
8674 uiout->text ("), ");
36dfb11c 8675
fe798b75 8676 return PRINT_SRC_AND_LOC;
c906108c
SS
8677}
8678
fe798b75 8679static void
a6d9a66e 8680print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8681{
b4d90040 8682 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8683 struct value_print_options opts;
79a45e25 8684 struct ui_out *uiout = current_uiout;
fe798b75
JB
8685
8686 get_user_print_options (&opts);
8687
8688 /* Field 4, the address, is omitted (which makes the columns
8689 not line up too nicely with the headers, but the effect
8690 is relatively readable). */
8691 if (opts.addressprint)
112e8700 8692 uiout->field_skip ("addr");
fe798b75 8693 annotate_field (5);
112e8700 8694 uiout->text ("exec");
b4d90040 8695 if (c->exec_pathname != NULL)
fe798b75 8696 {
112e8700
SM
8697 uiout->text (", program \"");
8698 uiout->field_string ("what", c->exec_pathname);
8699 uiout->text ("\" ");
fe798b75 8700 }
8ac3646f 8701
112e8700
SM
8702 if (uiout->is_mi_like_p ())
8703 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8704}
8705
8706static void
8707print_mention_catch_exec (struct breakpoint *b)
8708{
8709 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8710}
8711
6149aea9
PA
8712/* Implement the "print_recreate" breakpoint_ops method for exec
8713 catchpoints. */
8714
8715static void
8716print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8717{
8718 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8719 print_recreate_thread (b, fp);
6149aea9
PA
8720}
8721
2060206e 8722static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8723
c906108c 8724static int
fba45db2 8725hw_breakpoint_used_count (void)
c906108c 8726{
c906108c 8727 int i = 0;
f1310107
TJB
8728 struct breakpoint *b;
8729 struct bp_location *bl;
c906108c
SS
8730
8731 ALL_BREAKPOINTS (b)
c5aa993b 8732 {
d6b74ac4 8733 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8734 for (bl = b->loc; bl; bl = bl->next)
8735 {
8736 /* Special types of hardware breakpoints may use more than
8737 one register. */
348d480f 8738 i += b->ops->resources_needed (bl);
f1310107 8739 }
c5aa993b 8740 }
c906108c
SS
8741
8742 return i;
8743}
8744
a1398e0c
PA
8745/* Returns the resources B would use if it were a hardware
8746 watchpoint. */
8747
c906108c 8748static int
a1398e0c 8749hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8750{
c906108c 8751 int i = 0;
e09342b5 8752 struct bp_location *bl;
c906108c 8753
a1398e0c
PA
8754 if (!breakpoint_enabled (b))
8755 return 0;
8756
8757 for (bl = b->loc; bl; bl = bl->next)
8758 {
8759 /* Special types of hardware watchpoints may use more than
8760 one register. */
8761 i += b->ops->resources_needed (bl);
8762 }
8763
8764 return i;
8765}
8766
8767/* Returns the sum the used resources of all hardware watchpoints of
8768 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8769 the sum of the used resources of all hardware watchpoints of other
8770 types _not_ TYPE. */
8771
8772static int
8773hw_watchpoint_used_count_others (struct breakpoint *except,
8774 enum bptype type, int *other_type_used)
8775{
8776 int i = 0;
8777 struct breakpoint *b;
8778
c906108c
SS
8779 *other_type_used = 0;
8780 ALL_BREAKPOINTS (b)
e09342b5 8781 {
a1398e0c
PA
8782 if (b == except)
8783 continue;
e09342b5
TJB
8784 if (!breakpoint_enabled (b))
8785 continue;
8786
a1398e0c
PA
8787 if (b->type == type)
8788 i += hw_watchpoint_use_count (b);
8789 else if (is_hardware_watchpoint (b))
8790 *other_type_used = 1;
e09342b5
TJB
8791 }
8792
c906108c
SS
8793 return i;
8794}
8795
c906108c 8796void
fba45db2 8797disable_watchpoints_before_interactive_call_start (void)
c906108c 8798{
c5aa993b 8799 struct breakpoint *b;
c906108c
SS
8800
8801 ALL_BREAKPOINTS (b)
c5aa993b 8802 {
cc60f2e3 8803 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8804 {
b5de0fa7 8805 b->enable_state = bp_call_disabled;
44702360 8806 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8807 }
8808 }
c906108c
SS
8809}
8810
8811void
fba45db2 8812enable_watchpoints_after_interactive_call_stop (void)
c906108c 8813{
c5aa993b 8814 struct breakpoint *b;
c906108c
SS
8815
8816 ALL_BREAKPOINTS (b)
c5aa993b 8817 {
cc60f2e3 8818 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8819 {
b5de0fa7 8820 b->enable_state = bp_enabled;
44702360 8821 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8822 }
8823 }
c906108c
SS
8824}
8825
8bea4e01
UW
8826void
8827disable_breakpoints_before_startup (void)
8828{
6c95b8df 8829 current_program_space->executing_startup = 1;
44702360 8830 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8831}
8832
8833void
8834enable_breakpoints_after_startup (void)
8835{
6c95b8df 8836 current_program_space->executing_startup = 0;
f8eba3c6 8837 breakpoint_re_set ();
8bea4e01
UW
8838}
8839
7c16b83e
PA
8840/* Create a new single-step breakpoint for thread THREAD, with no
8841 locations. */
c906108c 8842
7c16b83e
PA
8843static struct breakpoint *
8844new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8845{
4d01a485 8846 struct breakpoint *b = new breakpoint ();
7c16b83e
PA
8847
8848 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8849 &momentary_breakpoint_ops);
8850
8851 b->disposition = disp_donttouch;
8852 b->frame_id = null_frame_id;
8853
8854 b->thread = thread;
8855 gdb_assert (b->thread != 0);
8856
8857 add_to_breakpoint_chain (b);
8858
8859 return b;
8860}
8861
8862/* Set a momentary breakpoint of type TYPE at address specified by
8863 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8864 frame. */
c906108c
SS
8865
8866struct breakpoint *
a6d9a66e
UW
8867set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8868 struct frame_id frame_id, enum bptype type)
c906108c 8869{
52f0bd74 8870 struct breakpoint *b;
edb3359d 8871
193facb3
JK
8872 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8873 tail-called one. */
8874 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8875
06edf0c0 8876 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8877 b->enable_state = bp_enabled;
8878 b->disposition = disp_donttouch;
818dd999 8879 b->frame_id = frame_id;
c906108c 8880
4a64f543
MS
8881 /* If we're debugging a multi-threaded program, then we want
8882 momentary breakpoints to be active in only a single thread of
8883 control. */
39f77062 8884 if (in_thread_list (inferior_ptid))
5d5658a1 8885 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8886
44702360 8887 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8888
c906108c
SS
8889 return b;
8890}
611c83ae 8891
06edf0c0 8892/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8893 The new breakpoint will have type TYPE, use OPS as its
8894 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8895
06edf0c0
PA
8896static struct breakpoint *
8897momentary_breakpoint_from_master (struct breakpoint *orig,
8898 enum bptype type,
a1aa2221
LM
8899 const struct breakpoint_ops *ops,
8900 int loc_enabled)
e58b0e63
PA
8901{
8902 struct breakpoint *copy;
8903
06edf0c0 8904 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8905 copy->loc = allocate_bp_location (copy);
0e30163f 8906 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8907
a6d9a66e 8908 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8909 copy->loc->requested_address = orig->loc->requested_address;
8910 copy->loc->address = orig->loc->address;
8911 copy->loc->section = orig->loc->section;
6c95b8df 8912 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8913 copy->loc->probe = orig->loc->probe;
f8eba3c6 8914 copy->loc->line_number = orig->loc->line_number;
2f202fde 8915 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8916 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8917 copy->frame_id = orig->frame_id;
8918 copy->thread = orig->thread;
6c95b8df 8919 copy->pspace = orig->pspace;
e58b0e63
PA
8920
8921 copy->enable_state = bp_enabled;
8922 copy->disposition = disp_donttouch;
8923 copy->number = internal_breakpoint_number--;
8924
44702360 8925 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8926 return copy;
8927}
8928
06edf0c0
PA
8929/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8930 ORIG is NULL. */
8931
8932struct breakpoint *
8933clone_momentary_breakpoint (struct breakpoint *orig)
8934{
8935 /* If there's nothing to clone, then return nothing. */
8936 if (orig == NULL)
8937 return NULL;
8938
a1aa2221 8939 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8940}
8941
611c83ae 8942struct breakpoint *
a6d9a66e
UW
8943set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8944 enum bptype type)
611c83ae
PA
8945{
8946 struct symtab_and_line sal;
8947
8948 sal = find_pc_line (pc, 0);
8949 sal.pc = pc;
8950 sal.section = find_pc_overlay (pc);
8951 sal.explicit_pc = 1;
8952
a6d9a66e 8953 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8954}
c906108c 8955\f
c5aa993b 8956
c906108c
SS
8957/* Tell the user we have just set a breakpoint B. */
8958
8959static void
fba45db2 8960mention (struct breakpoint *b)
c906108c 8961{
348d480f 8962 b->ops->print_mention (b);
112e8700 8963 if (current_uiout->is_mi_like_p ())
fb40c209 8964 return;
c906108c
SS
8965 printf_filtered ("\n");
8966}
c906108c 8967\f
c5aa993b 8968
1a853c52
PA
8969static int bp_loc_is_permanent (struct bp_location *loc);
8970
0d381245 8971static struct bp_location *
39d61571 8972add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8973 const struct symtab_and_line *sal)
8974{
8975 struct bp_location *loc, **tmp;
3742cc8b
YQ
8976 CORE_ADDR adjusted_address;
8977 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8978
8979 if (loc_gdbarch == NULL)
8980 loc_gdbarch = b->gdbarch;
8981
8982 /* Adjust the breakpoint's address prior to allocating a location.
8983 Once we call allocate_bp_location(), that mostly uninitialized
8984 location will be placed on the location chain. Adjustment of the
8985 breakpoint may cause target_read_memory() to be called and we do
8986 not want its scan of the location chain to find a breakpoint and
8987 location that's only been partially initialized. */
8988 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8989 sal->pc, b->type);
0d381245 8990
d30113d4 8991 /* Sort the locations by their ADDRESS. */
39d61571 8992 loc = allocate_bp_location (b);
d30113d4
JK
8993 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8994 tmp = &((*tmp)->next))
0d381245 8995 ;
d30113d4 8996 loc->next = *tmp;
0d381245 8997 *tmp = loc;
3742cc8b 8998
0d381245 8999 loc->requested_address = sal->pc;
3742cc8b 9000 loc->address = adjusted_address;
6c95b8df 9001 loc->pspace = sal->pspace;
729662a5
TT
9002 loc->probe.probe = sal->probe;
9003 loc->probe.objfile = sal->objfile;
6c95b8df 9004 gdb_assert (loc->pspace != NULL);
0d381245 9005 loc->section = sal->section;
3742cc8b 9006 loc->gdbarch = loc_gdbarch;
f8eba3c6 9007 loc->line_number = sal->line;
2f202fde 9008 loc->symtab = sal->symtab;
f8eba3c6 9009
0e30163f
JK
9010 set_breakpoint_location_function (loc,
9011 sal->explicit_pc || sal->explicit_line);
1a853c52 9012
6ae88661
LM
9013 /* While by definition, permanent breakpoints are already present in the
9014 code, we don't mark the location as inserted. Normally one would expect
9015 that GDB could rely on that breakpoint instruction to stop the program,
9016 thus removing the need to insert its own breakpoint, except that executing
9017 the breakpoint instruction can kill the target instead of reporting a
9018 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9019 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9020 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9021 breakpoint be inserted normally results in QEMU knowing about the GDB
9022 breakpoint, and thus trap before the breakpoint instruction is executed.
9023 (If GDB later needs to continue execution past the permanent breakpoint,
9024 it manually increments the PC, thus avoiding executing the breakpoint
9025 instruction.) */
1a853c52 9026 if (bp_loc_is_permanent (loc))
6ae88661 9027 loc->permanent = 1;
1a853c52 9028
0d381245
VP
9029 return loc;
9030}
514f746b
AR
9031\f
9032
1cf4d951 9033/* See breakpoint.h. */
514f746b 9034
1cf4d951
PA
9035int
9036program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9037{
9038 int len;
9039 CORE_ADDR addr;
1afeeb75 9040 const gdb_byte *bpoint;
514f746b 9041 gdb_byte *target_mem;
939c61fa
JK
9042 struct cleanup *cleanup;
9043 int retval = 0;
514f746b 9044
1cf4d951
PA
9045 addr = address;
9046 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9047
9048 /* Software breakpoints unsupported? */
9049 if (bpoint == NULL)
9050 return 0;
9051
224c3ddb 9052 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9053
9054 /* Enable the automatic memory restoration from breakpoints while
9055 we read the memory. Otherwise we could say about our temporary
9056 breakpoints they are permanent. */
9057 cleanup = make_show_memory_breakpoints_cleanup (0);
9058
9059 if (target_read_memory (address, target_mem, len) == 0
9060 && memcmp (target_mem, bpoint, len) == 0)
9061 retval = 1;
9062
9063 do_cleanups (cleanup);
9064
9065 return retval;
9066}
9067
9068/* Return 1 if LOC is pointing to a permanent breakpoint,
9069 return 0 otherwise. */
9070
9071static int
9072bp_loc_is_permanent (struct bp_location *loc)
9073{
9074 struct cleanup *cleanup;
9075 int retval;
9076
514f746b
AR
9077 gdb_assert (loc != NULL);
9078
244558af
LM
9079 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9080 attempt to read from the addresses the locations of these breakpoint types
9081 point to. program_breakpoint_here_p, below, will attempt to read
9082 memory. */
9083 if (!breakpoint_address_is_meaningful (loc->owner))
9084 return 0;
9085
6c95b8df 9086 cleanup = save_current_space_and_thread ();
6c95b8df 9087 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9088
1cf4d951 9089 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9090
939c61fa
JK
9091 do_cleanups (cleanup);
9092
9093 return retval;
514f746b
AR
9094}
9095
e7e0cddf
SS
9096/* Build a command list for the dprintf corresponding to the current
9097 settings of the dprintf style options. */
9098
9099static void
9100update_dprintf_command_list (struct breakpoint *b)
9101{
9102 char *dprintf_args = b->extra_string;
9103 char *printf_line = NULL;
9104
9105 if (!dprintf_args)
9106 return;
9107
9108 dprintf_args = skip_spaces (dprintf_args);
9109
9110 /* Allow a comma, as it may have terminated a location, but don't
9111 insist on it. */
9112 if (*dprintf_args == ',')
9113 ++dprintf_args;
9114 dprintf_args = skip_spaces (dprintf_args);
9115
9116 if (*dprintf_args != '"')
9117 error (_("Bad format string, missing '\"'."));
9118
d3ce09f5 9119 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9120 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9121 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9122 {
9123 if (!dprintf_function)
9124 error (_("No function supplied for dprintf call"));
9125
9126 if (dprintf_channel && strlen (dprintf_channel) > 0)
9127 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9128 dprintf_function,
9129 dprintf_channel,
9130 dprintf_args);
9131 else
9132 printf_line = xstrprintf ("call (void) %s (%s)",
9133 dprintf_function,
9134 dprintf_args);
9135 }
d3ce09f5
SS
9136 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9137 {
9138 if (target_can_run_breakpoint_commands ())
9139 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9140 else
9141 {
9142 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9143 printf_line = xstrprintf ("printf %s", dprintf_args);
9144 }
9145 }
e7e0cddf
SS
9146 else
9147 internal_error (__FILE__, __LINE__,
9148 _("Invalid dprintf style."));
9149
f28045c2 9150 gdb_assert (printf_line != NULL);
9d6e6e84 9151 /* Manufacture a printf sequence. */
f28045c2 9152 {
8d749320 9153 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9154
f28045c2
YQ
9155 printf_cmd_line->control_type = simple_control;
9156 printf_cmd_line->body_count = 0;
9157 printf_cmd_line->body_list = NULL;
9d6e6e84 9158 printf_cmd_line->next = NULL;
f28045c2 9159 printf_cmd_line->line = printf_line;
e7e0cddf 9160
93921405 9161 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9162 }
e7e0cddf
SS
9163}
9164
9165/* Update all dprintf commands, making their command lists reflect
9166 current style settings. */
9167
9168static void
9169update_dprintf_commands (char *args, int from_tty,
9170 struct cmd_list_element *c)
9171{
9172 struct breakpoint *b;
9173
9174 ALL_BREAKPOINTS (b)
9175 {
9176 if (b->type == bp_dprintf)
9177 update_dprintf_command_list (b);
9178 }
9179}
c3f6f71d 9180
f00aae0f
KS
9181/* Create a breakpoint with SAL as location. Use LOCATION
9182 as a description of the location, and COND_STRING
b35a8b2f
DE
9183 as condition expression. If LOCATION is NULL then create an
9184 "address location" from the address in the SAL. */
018d34a4
VP
9185
9186static void
d9b3f62e 9187init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9188 struct symtabs_and_lines sals,
ffc2605c 9189 event_location_up &&location,
f8eba3c6 9190 char *filter, char *cond_string,
e7e0cddf 9191 char *extra_string,
d9b3f62e
PA
9192 enum bptype type, enum bpdisp disposition,
9193 int thread, int task, int ignore_count,
c0a91b2b 9194 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9195 int enabled, int internal, unsigned flags,
9196 int display_canonical)
018d34a4 9197{
0d381245 9198 int i;
018d34a4
VP
9199
9200 if (type == bp_hardware_breakpoint)
9201 {
fbbd034e
AS
9202 int target_resources_ok;
9203
9204 i = hw_breakpoint_used_count ();
9205 target_resources_ok =
9206 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9207 i + 1, 0);
9208 if (target_resources_ok == 0)
9209 error (_("No hardware breakpoint support in the target."));
9210 else if (target_resources_ok < 0)
9211 error (_("Hardware breakpoints used exceeds limit."));
9212 }
9213
6c95b8df
PA
9214 gdb_assert (sals.nelts > 0);
9215
0d381245
VP
9216 for (i = 0; i < sals.nelts; ++i)
9217 {
9218 struct symtab_and_line sal = sals.sals[i];
9219 struct bp_location *loc;
9220
9221 if (from_tty)
5af949e3
UW
9222 {
9223 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9224 if (!loc_gdbarch)
9225 loc_gdbarch = gdbarch;
9226
9227 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9228 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9229 }
0d381245
VP
9230
9231 if (i == 0)
9232 {
d9b3f62e 9233 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9234 b->thread = thread;
4a306c9a 9235 b->task = task;
855a6e68 9236
0d381245 9237 b->cond_string = cond_string;
e7e0cddf 9238 b->extra_string = extra_string;
0d381245 9239 b->ignore_count = ignore_count;
41447f92 9240 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9241 b->disposition = disposition;
6c95b8df 9242
44f238bb
PA
9243 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9244 b->loc->inserted = 1;
9245
0fb4aa4b
PA
9246 if (type == bp_static_tracepoint)
9247 {
d9b3f62e 9248 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9249 struct static_tracepoint_marker marker;
9250
983af33b 9251 if (strace_marker_p (b))
0fb4aa4b
PA
9252 {
9253 /* We already know the marker exists, otherwise, we
9254 wouldn't see a sal for it. */
d28cd78a
TT
9255 const char *p
9256 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9257 const char *endp;
0fb4aa4b 9258 char *marker_str;
0fb4aa4b 9259
f00aae0f 9260 p = skip_spaces_const (p);
0fb4aa4b 9261
f00aae0f 9262 endp = skip_to_space_const (p);
0fb4aa4b
PA
9263
9264 marker_str = savestring (p, endp - p);
d9b3f62e 9265 t->static_trace_marker_id = marker_str;
0fb4aa4b 9266
3e43a32a
MS
9267 printf_filtered (_("Probed static tracepoint "
9268 "marker \"%s\"\n"),
d9b3f62e 9269 t->static_trace_marker_id);
0fb4aa4b
PA
9270 }
9271 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9272 {
d9b3f62e 9273 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9274 release_static_tracepoint_marker (&marker);
9275
3e43a32a
MS
9276 printf_filtered (_("Probed static tracepoint "
9277 "marker \"%s\"\n"),
d9b3f62e 9278 t->static_trace_marker_id);
0fb4aa4b
PA
9279 }
9280 else
3e43a32a
MS
9281 warning (_("Couldn't determine the static "
9282 "tracepoint marker to probe"));
0fb4aa4b
PA
9283 }
9284
0d381245
VP
9285 loc = b->loc;
9286 }
9287 else
018d34a4 9288 {
39d61571 9289 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9290 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9291 loc->inserted = 1;
0d381245
VP
9292 }
9293
9294 if (b->cond_string)
9295 {
bbc13ae3
KS
9296 const char *arg = b->cond_string;
9297
1bb9788d
TT
9298 loc->cond = parse_exp_1 (&arg, loc->address,
9299 block_for_pc (loc->address), 0);
0d381245 9300 if (*arg)
588ae58c 9301 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9302 }
e7e0cddf
SS
9303
9304 /* Dynamic printf requires and uses additional arguments on the
9305 command line, otherwise it's an error. */
9306 if (type == bp_dprintf)
9307 {
9308 if (b->extra_string)
9309 update_dprintf_command_list (b);
9310 else
9311 error (_("Format string required"));
9312 }
9313 else if (b->extra_string)
588ae58c 9314 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9315 }
018d34a4 9316
56435ebe 9317 b->display_canonical = display_canonical;
f00aae0f 9318 if (location != NULL)
d28cd78a 9319 b->location = std::move (location);
018d34a4 9320 else
d28cd78a 9321 b->location = new_address_location (b->loc->address, NULL, 0);
f8eba3c6 9322 b->filter = filter;
d9b3f62e 9323}
018d34a4 9324
d9b3f62e
PA
9325static void
9326create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9327 struct symtabs_and_lines sals,
ffc2605c 9328 event_location_up &&location,
f8eba3c6 9329 char *filter, char *cond_string,
e7e0cddf 9330 char *extra_string,
d9b3f62e
PA
9331 enum bptype type, enum bpdisp disposition,
9332 int thread, int task, int ignore_count,
c0a91b2b 9333 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9334 int enabled, int internal, unsigned flags,
9335 int display_canonical)
d9b3f62e
PA
9336{
9337 struct breakpoint *b;
9338 struct cleanup *old_chain;
9339
9340 if (is_tracepoint_type (type))
9341 {
9342 struct tracepoint *t;
9343
4d01a485 9344 t = new tracepoint ();
d9b3f62e
PA
9345 b = &t->base;
9346 }
9347 else
4d01a485 9348 b = new breakpoint ();
d9b3f62e
PA
9349
9350 old_chain = make_cleanup (xfree, b);
9351
9352 init_breakpoint_sal (b, gdbarch,
ffc2605c 9353 sals, std::move (location),
e7e0cddf 9354 filter, cond_string, extra_string,
d9b3f62e
PA
9355 type, disposition,
9356 thread, task, ignore_count,
9357 ops, from_tty,
44f238bb
PA
9358 enabled, internal, flags,
9359 display_canonical);
d9b3f62e
PA
9360 discard_cleanups (old_chain);
9361
3ea46bff 9362 install_breakpoint (internal, b, 0);
018d34a4
VP
9363}
9364
9365/* Add SALS.nelts breakpoints to the breakpoint table. For each
9366 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9367 value. COND_STRING, if not NULL, specified the condition to be
9368 used for all breakpoints. Essentially the only case where
9369 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9370 function. In that case, it's still not possible to specify
9371 separate conditions for different overloaded functions, so
9372 we take just a single condition string.
9373
c3f6f71d 9374 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9375 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9376 array contents). If the function fails (error() is called), the
9377 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9378 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9379
9380static void
8cdf0e15 9381create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9382 struct linespec_result *canonical,
e7e0cddf 9383 char *cond_string, char *extra_string,
8cdf0e15
VP
9384 enum bptype type, enum bpdisp disposition,
9385 int thread, int task, int ignore_count,
c0a91b2b 9386 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9387 int enabled, int internal, unsigned flags)
c906108c 9388{
018d34a4 9389 int i;
f8eba3c6 9390 struct linespec_sals *lsal;
cc59ec59 9391
f8eba3c6
TT
9392 if (canonical->pre_expanded)
9393 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9394
9395 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9396 {
f00aae0f 9397 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9398 'break', without arguments. */
ffc2605c 9399 event_location_up location
f00aae0f 9400 = (canonical->location != NULL
8e9e35b1 9401 ? copy_event_location (canonical->location.get ()) : NULL);
f8eba3c6 9402 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
0d381245 9403
f8eba3c6
TT
9404 make_cleanup (xfree, filter_string);
9405 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9406 std::move (location),
f8eba3c6 9407 filter_string,
e7e0cddf
SS
9408 cond_string, extra_string,
9409 type, disposition,
84f4c1fe 9410 thread, task, ignore_count, ops,
44f238bb 9411 from_tty, enabled, internal, flags,
56435ebe 9412 canonical->special_display);
c3f6f71d 9413 }
c3f6f71d 9414}
c906108c 9415
f00aae0f 9416/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9417 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9418 addresses found. LOCATION points to the end of the SAL (for
9419 linespec locations).
9998af43
TJB
9420
9421 The array and the line spec strings are allocated on the heap, it is
9422 the caller's responsibility to free them. */
c906108c 9423
b9362cc7 9424static void
f00aae0f 9425parse_breakpoint_sals (const struct event_location *location,
58438ac1 9426 struct linespec_result *canonical)
c3f6f71d 9427{
f00aae0f
KS
9428 struct symtab_and_line cursal;
9429
9430 if (event_location_type (location) == LINESPEC_LOCATION)
9431 {
9432 const char *address = get_linespec_location (location);
9433
9434 if (address == NULL)
9435 {
9436 /* The last displayed codepoint, if it's valid, is our default
9437 breakpoint address. */
9438 if (last_displayed_sal_is_valid ())
9439 {
9440 struct linespec_sals lsal;
9441 struct symtab_and_line sal;
9442 CORE_ADDR pc;
9443
9444 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9445 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9446
9447 /* Set sal's pspace, pc, symtab, and line to the values
9448 corresponding to the last call to print_frame_info.
9449 Be sure to reinitialize LINE with NOTCURRENT == 0
9450 as the breakpoint line number is inappropriate otherwise.
9451 find_pc_line would adjust PC, re-set it back. */
9452 get_last_displayed_sal (&sal);
9453 pc = sal.pc;
9454 sal = find_pc_line (pc, 0);
9455
9456 /* "break" without arguments is equivalent to "break *PC"
9457 where PC is the last displayed codepoint's address. So
9458 make sure to set sal.explicit_pc to prevent GDB from
9459 trying to expand the list of sals to include all other
9460 instances with the same symtab and line. */
9461 sal.pc = pc;
9462 sal.explicit_pc = 1;
9463
9464 lsal.sals.sals[0] = sal;
9465 lsal.sals.nelts = 1;
9466 lsal.canonical = NULL;
9467
9468 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9469 return;
9470 }
9471 else
9472 error (_("No default breakpoint address now."));
c906108c 9473 }
c906108c 9474 }
f00aae0f
KS
9475
9476 /* Force almost all breakpoints to be in terms of the
9477 current_source_symtab (which is decode_line_1's default).
9478 This should produce the results we want almost all of the
9479 time while leaving default_breakpoint_* alone.
9480
9481 ObjC: However, don't match an Objective-C method name which
9482 may have a '+' or '-' succeeded by a '['. */
9483 cursal = get_current_source_symtab_and_line ();
9484 if (last_displayed_sal_is_valid ())
c906108c 9485 {
f00aae0f 9486 const char *address = NULL;
cc80f267 9487
f00aae0f
KS
9488 if (event_location_type (location) == LINESPEC_LOCATION)
9489 address = get_linespec_location (location);
cc80f267 9490
f00aae0f
KS
9491 if (!cursal.symtab
9492 || (address != NULL
9493 && strchr ("+-", address[0]) != NULL
9494 && address[1] != '['))
9495 {
c2f4122d 9496 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9497 get_last_displayed_symtab (),
9498 get_last_displayed_line (),
9499 canonical, NULL, NULL);
9500 return;
9501 }
c906108c 9502 }
f00aae0f 9503
c2f4122d 9504 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9505 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9506}
c906108c 9507
c906108c 9508
c3f6f71d 9509/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9510 inserted as a breakpoint. If it can't throw an error. */
c906108c 9511
b9362cc7 9512static void
23e7acfb 9513breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9514{
9515 int i;
cc59ec59 9516
c3f6f71d 9517 for (i = 0; i < sals->nelts; i++)
ee53e872 9518 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9519}
9520
7a697b8d
SS
9521/* Fast tracepoints may have restrictions on valid locations. For
9522 instance, a fast tracepoint using a jump instead of a trap will
9523 likely have to overwrite more bytes than a trap would, and so can
9524 only be placed where the instruction is longer than the jump, or a
9525 multi-instruction sequence does not have a jump into the middle of
9526 it, etc. */
9527
9528static void
9529check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9530 struct symtabs_and_lines *sals)
9531{
9532 int i, rslt;
9533 struct symtab_and_line *sal;
9534 char *msg;
9535 struct cleanup *old_chain;
9536
9537 for (i = 0; i < sals->nelts; i++)
9538 {
f8eba3c6
TT
9539 struct gdbarch *sarch;
9540
7a697b8d
SS
9541 sal = &sals->sals[i];
9542
f8eba3c6
TT
9543 sarch = get_sal_arch (*sal);
9544 /* We fall back to GDBARCH if there is no architecture
9545 associated with SAL. */
9546 if (sarch == NULL)
9547 sarch = gdbarch;
6b940e6a 9548 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9549 old_chain = make_cleanup (xfree, msg);
9550
9551 if (!rslt)
53c3572a 9552 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9553 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9554
9555 do_cleanups (old_chain);
9556 }
9557}
9558
018d34a4
VP
9559/* Given TOK, a string specification of condition and thread, as
9560 accepted by the 'break' command, extract the condition
9561 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9562 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9563 If no condition is found, *COND_STRING is set to NULL.
9564 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9565
9566static void
bbc13ae3 9567find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9568 char **cond_string, int *thread, int *task,
9569 char **rest)
018d34a4
VP
9570{
9571 *cond_string = NULL;
9572 *thread = -1;
ed1d1739
KS
9573 *task = 0;
9574 *rest = NULL;
9575
018d34a4
VP
9576 while (tok && *tok)
9577 {
bbc13ae3 9578 const char *end_tok;
018d34a4 9579 int toklen;
bbc13ae3
KS
9580 const char *cond_start = NULL;
9581 const char *cond_end = NULL;
cc59ec59 9582
bbc13ae3 9583 tok = skip_spaces_const (tok);
e7e0cddf
SS
9584
9585 if ((*tok == '"' || *tok == ',') && rest)
9586 {
9587 *rest = savestring (tok, strlen (tok));
9588 return;
9589 }
9590
bbc13ae3 9591 end_tok = skip_to_space_const (tok);
d634f2de 9592
018d34a4 9593 toklen = end_tok - tok;
d634f2de 9594
018d34a4
VP
9595 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9596 {
9597 tok = cond_start = end_tok + 1;
4d01a485 9598 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9599 cond_end = tok;
d634f2de 9600 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9601 }
9602 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9603 {
5d5658a1
PA
9604 const char *tmptok;
9605 struct thread_info *thr;
d634f2de 9606
018d34a4 9607 tok = end_tok + 1;
5d5658a1 9608 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9609 if (tok == tmptok)
9610 error (_("Junk after thread keyword."));
5d5658a1 9611 *thread = thr->global_num;
bbc13ae3 9612 tok = tmptok;
018d34a4 9613 }
4a306c9a
JB
9614 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9615 {
9616 char *tmptok;
9617
9618 tok = end_tok + 1;
bbc13ae3 9619 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9620 if (tok == tmptok)
9621 error (_("Junk after task keyword."));
9622 if (!valid_task_id (*task))
b6199126 9623 error (_("Unknown task %d."), *task);
bbc13ae3 9624 tok = tmptok;
4a306c9a 9625 }
e7e0cddf
SS
9626 else if (rest)
9627 {
9628 *rest = savestring (tok, strlen (tok));
ccab2054 9629 return;
e7e0cddf 9630 }
018d34a4
VP
9631 else
9632 error (_("Junk at end of arguments."));
9633 }
9634}
9635
0fb4aa4b
PA
9636/* Decode a static tracepoint marker spec. */
9637
9638static struct symtabs_and_lines
f00aae0f 9639decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9640{
9641 VEC(static_tracepoint_marker_p) *markers = NULL;
9642 struct symtabs_and_lines sals;
0fb4aa4b 9643 struct cleanup *old_chain;
f00aae0f
KS
9644 const char *p = &(*arg_p)[3];
9645 const char *endp;
0fb4aa4b
PA
9646 char *marker_str;
9647 int i;
9648
f00aae0f 9649 p = skip_spaces_const (p);
0fb4aa4b 9650
f00aae0f 9651 endp = skip_to_space_const (p);
0fb4aa4b
PA
9652
9653 marker_str = savestring (p, endp - p);
9654 old_chain = make_cleanup (xfree, marker_str);
9655
9656 markers = target_static_tracepoint_markers_by_strid (marker_str);
9657 if (VEC_empty(static_tracepoint_marker_p, markers))
9658 error (_("No known static tracepoint marker named %s"), marker_str);
9659
9660 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9661 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9662
9663 for (i = 0; i < sals.nelts; i++)
9664 {
9665 struct static_tracepoint_marker *marker;
9666
9667 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9668
9669 init_sal (&sals.sals[i]);
9670
9671 sals.sals[i] = find_pc_line (marker->address, 0);
9672 sals.sals[i].pc = marker->address;
9673
9674 release_static_tracepoint_marker (marker);
9675 }
9676
9677 do_cleanups (old_chain);
9678
9679 *arg_p = endp;
9680 return sals;
9681}
9682
f00aae0f 9683/* See breakpoint.h. */
0101ce28 9684
8cdf0e15
VP
9685int
9686create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9687 const struct event_location *location, char *cond_string,
e7e0cddf 9688 int thread, char *extra_string,
f00aae0f 9689 int parse_extra,
0fb4aa4b 9690 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9691 int ignore_count,
9692 enum auto_boolean pending_break_support,
c0a91b2b 9693 const struct breakpoint_ops *ops,
44f238bb
PA
9694 int from_tty, int enabled, int internal,
9695 unsigned flags)
c3f6f71d 9696{
7efd8fc2 9697 struct linespec_result canonical;
80c99de1 9698 struct cleanup *bkpt_chain = NULL;
0101ce28 9699 int pending = 0;
4a306c9a 9700 int task = 0;
86b17b60 9701 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9702
348d480f
PA
9703 gdb_assert (ops != NULL);
9704
f00aae0f
KS
9705 /* If extra_string isn't useful, set it to NULL. */
9706 if (extra_string != NULL && *extra_string == '\0')
9707 extra_string = NULL;
9708
492d29ea 9709 TRY
b78a6381 9710 {
f00aae0f 9711 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9712 }
492d29ea 9713 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9714 {
492d29ea
PA
9715 /* If caller is interested in rc value from parse, set
9716 value. */
9717 if (e.error == NOT_FOUND_ERROR)
0101ce28 9718 {
05ff989b
AC
9719 /* If pending breakpoint support is turned off, throw
9720 error. */
fa8d40ab
JJ
9721
9722 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9723 throw_exception (e);
9724
9725 exception_print (gdb_stderr, e);
fa8d40ab 9726
05ff989b
AC
9727 /* If pending breakpoint support is auto query and the user
9728 selects no, then simply return the error code. */
059fb39f 9729 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9730 && !nquery (_("Make %s pending on future shared library load? "),
9731 bptype_string (type_wanted)))
fd9b8c24 9732 return 0;
fa8d40ab 9733
05ff989b
AC
9734 /* At this point, either the user was queried about setting
9735 a pending breakpoint and selected yes, or pending
9736 breakpoint behavior is on and thus a pending breakpoint
9737 is defaulted on behalf of the user. */
f00aae0f 9738 pending = 1;
0101ce28 9739 }
492d29ea
PA
9740 else
9741 throw_exception (e);
0101ce28 9742 }
492d29ea
PA
9743 END_CATCH
9744
f00aae0f 9745 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9746 return 0;
c3f6f71d 9747
c3f6f71d
JM
9748 /* ----------------------------- SNIP -----------------------------
9749 Anything added to the cleanup chain beyond this point is assumed
9750 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9751 then the memory is not reclaimed. */
9752 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9753
c3f6f71d
JM
9754 /* Resolve all line numbers to PC's and verify that the addresses
9755 are ok for the target. */
0101ce28 9756 if (!pending)
f8eba3c6
TT
9757 {
9758 int ix;
9759 struct linespec_sals *iter;
9760
9761 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9762 breakpoint_sals_to_pc (&iter->sals);
9763 }
c3f6f71d 9764
7a697b8d 9765 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9766 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9767 {
9768 int ix;
9769 struct linespec_sals *iter;
9770
9771 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9772 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9773 }
7a697b8d 9774
c3f6f71d
JM
9775 /* Verify that condition can be parsed, before setting any
9776 breakpoints. Allocate a separate condition expression for each
4a64f543 9777 breakpoint. */
0101ce28 9778 if (!pending)
c3f6f71d 9779 {
f00aae0f 9780 if (parse_extra)
72b2ff0e 9781 {
0878d0fa 9782 char *rest;
52d361e1
YQ
9783 struct linespec_sals *lsal;
9784
9785 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9786
0878d0fa
YQ
9787 /* Here we only parse 'arg' to separate condition
9788 from thread number, so parsing in context of first
9789 sal is OK. When setting the breakpoint we'll
9790 re-parse it in context of each sal. */
9791
f00aae0f
KS
9792 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9793 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9794 if (cond_string)
9795 make_cleanup (xfree, cond_string);
9796 if (rest)
9797 make_cleanup (xfree, rest);
9798 if (rest)
9799 extra_string = rest;
f00aae0f
KS
9800 else
9801 extra_string = NULL;
72b2ff0e 9802 }
2f069f6f 9803 else
72b2ff0e 9804 {
f00aae0f
KS
9805 if (type_wanted != bp_dprintf
9806 && extra_string != NULL && *extra_string != '\0')
9807 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9808
9809 /* Create a private copy of condition string. */
9810 if (cond_string)
9811 {
9812 cond_string = xstrdup (cond_string);
9813 make_cleanup (xfree, cond_string);
9814 }
9815 /* Create a private copy of any extra string. */
9816 if (extra_string)
9817 {
9818 extra_string = xstrdup (extra_string);
9819 make_cleanup (xfree, extra_string);
9820 }
72b2ff0e 9821 }
0fb4aa4b 9822
52d361e1 9823 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9824 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9825 tempflag ? disp_del : disp_donttouch,
9826 thread, task, ignore_count, ops,
44f238bb 9827 from_tty, enabled, internal, flags);
c906108c 9828 }
0101ce28
JJ
9829 else
9830 {
0101ce28
JJ
9831 struct breakpoint *b;
9832
bfccc43c
YQ
9833 if (is_tracepoint_type (type_wanted))
9834 {
9835 struct tracepoint *t;
9836
4d01a485 9837 t = new tracepoint ();
bfccc43c
YQ
9838 b = &t->base;
9839 }
9840 else
4d01a485 9841 b = new breakpoint ();
bfccc43c
YQ
9842
9843 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
d28cd78a 9844 b->location = copy_event_location (location);
bfccc43c 9845
f00aae0f
KS
9846 if (parse_extra)
9847 b->cond_string = NULL;
e12c7713
MK
9848 else
9849 {
9850 /* Create a private copy of condition string. */
9851 if (cond_string)
9852 {
9853 cond_string = xstrdup (cond_string);
9854 make_cleanup (xfree, cond_string);
9855 }
9856 b->cond_string = cond_string;
15630549 9857 b->thread = thread;
e12c7713 9858 }
f00aae0f
KS
9859
9860 /* Create a private copy of any extra string. */
9861 if (extra_string != NULL)
9862 {
9863 extra_string = xstrdup (extra_string);
9864 make_cleanup (xfree, extra_string);
9865 }
9866 b->extra_string = extra_string;
0101ce28 9867 b->ignore_count = ignore_count;
0101ce28 9868 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9869 b->condition_not_parsed = 1;
41447f92 9870 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9871 if ((type_wanted != bp_breakpoint
9872 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9873 b->pspace = current_program_space;
8bea4e01 9874
bfccc43c 9875 install_breakpoint (internal, b, 0);
0101ce28
JJ
9876 }
9877
f8eba3c6 9878 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9879 {
3e43a32a
MS
9880 warning (_("Multiple breakpoints were set.\nUse the "
9881 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9882 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9883 }
9884
80c99de1
PA
9885 /* That's it. Discard the cleanups for data inserted into the
9886 breakpoint. */
9887 discard_cleanups (bkpt_chain);
217dc9e2 9888
80c99de1 9889 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9890 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9891
9892 return 1;
c3f6f71d 9893}
c906108c 9894
348d480f 9895/* Set a breakpoint.
72b2ff0e
VP
9896 ARG is a string describing breakpoint address,
9897 condition, and thread.
9898 FLAG specifies if a breakpoint is hardware on,
9899 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9900 and BP_TEMPFLAG. */
348d480f 9901
98deb0da 9902static void
72b2ff0e 9903break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9904{
72b2ff0e 9905 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9906 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9907 ? bp_hardware_breakpoint
9908 : bp_breakpoint);
55aa24fb 9909 struct breakpoint_ops *ops;
f00aae0f 9910
ffc2605c 9911 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9912
9913 /* Matching breakpoints on probes. */
5b56227b 9914 if (location != NULL
ffc2605c 9915 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9916 ops = &bkpt_probe_breakpoint_ops;
9917 else
9918 ops = &bkpt_breakpoint_ops;
c3f6f71d 9919
8cdf0e15 9920 create_breakpoint (get_current_arch (),
ffc2605c 9921 location.get (),
f00aae0f 9922 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9923 tempflag, type_wanted,
8cdf0e15
VP
9924 0 /* Ignore count */,
9925 pending_break_support,
55aa24fb 9926 ops,
8cdf0e15 9927 from_tty,
84f4c1fe 9928 1 /* enabled */,
44f238bb
PA
9929 0 /* internal */,
9930 0);
c906108c
SS
9931}
9932
c906108c
SS
9933/* Helper function for break_command_1 and disassemble_command. */
9934
9935void
fba45db2 9936resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9937{
9938 CORE_ADDR pc;
9939
9940 if (sal->pc == 0 && sal->symtab != NULL)
9941 {
9942 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9943 error (_("No line %d in file \"%s\"."),
05cba821 9944 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9945 sal->pc = pc;
6a048695 9946
4a64f543
MS
9947 /* If this SAL corresponds to a breakpoint inserted using a line
9948 number, then skip the function prologue if necessary. */
6a048695 9949 if (sal->explicit_line)
059acae7 9950 skip_prologue_sal (sal);
c906108c
SS
9951 }
9952
9953 if (sal->section == 0 && sal->symtab != NULL)
9954 {
346d1dfe 9955 const struct blockvector *bv;
3977b71f 9956 const struct block *b;
c5aa993b 9957 struct symbol *sym;
c906108c 9958
43f3e411
DE
9959 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9960 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9961 if (bv != NULL)
9962 {
7f0df278 9963 sym = block_linkage_function (b);
c906108c
SS
9964 if (sym != NULL)
9965 {
eb822aa6
DE
9966 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9967 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9968 sym);
c906108c
SS
9969 }
9970 else
9971 {
4a64f543
MS
9972 /* It really is worthwhile to have the section, so we'll
9973 just have to look harder. This case can be executed
9974 if we have line numbers but no functions (as can
9975 happen in assembly source). */
c906108c 9976
7cbd4a93 9977 struct bound_minimal_symbol msym;
6c95b8df
PA
9978 struct cleanup *old_chain = save_current_space_and_thread ();
9979
9980 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9981
9982 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9983 if (msym.minsym)
efd66ac6 9984 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9985
9986 do_cleanups (old_chain);
c906108c
SS
9987 }
9988 }
9989 }
9990}
9991
9992void
fba45db2 9993break_command (char *arg, int from_tty)
c906108c 9994{
db107f19 9995 break_command_1 (arg, 0, from_tty);
c906108c
SS
9996}
9997
c906108c 9998void
fba45db2 9999tbreak_command (char *arg, int from_tty)
c906108c 10000{
db107f19 10001 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10002}
10003
c906108c 10004static void
fba45db2 10005hbreak_command (char *arg, int from_tty)
c906108c 10006{
db107f19 10007 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10008}
10009
10010static void
fba45db2 10011thbreak_command (char *arg, int from_tty)
c906108c 10012{
db107f19 10013 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10014}
10015
10016static void
fba45db2 10017stop_command (char *arg, int from_tty)
c906108c 10018{
a3f17187 10019 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10020Usage: stop in <function | address>\n\
a3f17187 10021 stop at <line>\n"));
c906108c
SS
10022}
10023
10024static void
fba45db2 10025stopin_command (char *arg, int from_tty)
c906108c
SS
10026{
10027 int badInput = 0;
10028
c5aa993b 10029 if (arg == (char *) NULL)
c906108c
SS
10030 badInput = 1;
10031 else if (*arg != '*')
10032 {
10033 char *argptr = arg;
10034 int hasColon = 0;
10035
4a64f543 10036 /* Look for a ':'. If this is a line number specification, then
53a5351d 10037 say it is bad, otherwise, it should be an address or
4a64f543 10038 function/method name. */
c906108c 10039 while (*argptr && !hasColon)
c5aa993b
JM
10040 {
10041 hasColon = (*argptr == ':');
10042 argptr++;
10043 }
c906108c
SS
10044
10045 if (hasColon)
c5aa993b 10046 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10047 else
c5aa993b 10048 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10049 }
10050
10051 if (badInput)
a3f17187 10052 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10053 else
db107f19 10054 break_command_1 (arg, 0, from_tty);
c906108c
SS
10055}
10056
10057static void
fba45db2 10058stopat_command (char *arg, int from_tty)
c906108c
SS
10059{
10060 int badInput = 0;
10061
c5aa993b 10062 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10063 badInput = 1;
10064 else
10065 {
10066 char *argptr = arg;
10067 int hasColon = 0;
10068
4a64f543
MS
10069 /* Look for a ':'. If there is a '::' then get out, otherwise
10070 it is probably a line number. */
c906108c 10071 while (*argptr && !hasColon)
c5aa993b
JM
10072 {
10073 hasColon = (*argptr == ':');
10074 argptr++;
10075 }
c906108c
SS
10076
10077 if (hasColon)
c5aa993b 10078 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10079 else
c5aa993b 10080 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10081 }
10082
10083 if (badInput)
a3f17187 10084 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10085 else
db107f19 10086 break_command_1 (arg, 0, from_tty);
c906108c
SS
10087}
10088
e7e0cddf
SS
10089/* The dynamic printf command is mostly like a regular breakpoint, but
10090 with a prewired command list consisting of a single output command,
10091 built from extra arguments supplied on the dprintf command
10092 line. */
10093
da821c7b 10094static void
e7e0cddf
SS
10095dprintf_command (char *arg, int from_tty)
10096{
ffc2605c 10097 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10098
10099 /* If non-NULL, ARG should have been advanced past the location;
10100 the next character must be ','. */
10101 if (arg != NULL)
10102 {
10103 if (arg[0] != ',' || arg[1] == '\0')
10104 error (_("Format string required"));
10105 else
10106 {
10107 /* Skip the comma. */
10108 ++arg;
10109 }
10110 }
10111
e7e0cddf 10112 create_breakpoint (get_current_arch (),
ffc2605c 10113 location.get (),
f00aae0f 10114 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10115 0, bp_dprintf,
10116 0 /* Ignore count */,
10117 pending_break_support,
10118 &dprintf_breakpoint_ops,
10119 from_tty,
10120 1 /* enabled */,
10121 0 /* internal */,
10122 0);
10123}
10124
d3ce09f5
SS
10125static void
10126agent_printf_command (char *arg, int from_tty)
10127{
10128 error (_("May only run agent-printf on the target"));
10129}
10130
f1310107
TJB
10131/* Implement the "breakpoint_hit" breakpoint_ops method for
10132 ranged breakpoints. */
10133
10134static int
10135breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10136 struct address_space *aspace,
09ac7c10
TT
10137 CORE_ADDR bp_addr,
10138 const struct target_waitstatus *ws)
f1310107 10139{
09ac7c10 10140 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10141 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10142 return 0;
10143
f1310107
TJB
10144 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10145 bl->length, aspace, bp_addr);
10146}
10147
10148/* Implement the "resources_needed" breakpoint_ops method for
10149 ranged breakpoints. */
10150
10151static int
10152resources_needed_ranged_breakpoint (const struct bp_location *bl)
10153{
10154 return target_ranged_break_num_registers ();
10155}
10156
10157/* Implement the "print_it" breakpoint_ops method for
10158 ranged breakpoints. */
10159
10160static enum print_stop_action
348d480f 10161print_it_ranged_breakpoint (bpstat bs)
f1310107 10162{
348d480f 10163 struct breakpoint *b = bs->breakpoint_at;
f1310107 10164 struct bp_location *bl = b->loc;
79a45e25 10165 struct ui_out *uiout = current_uiout;
f1310107
TJB
10166
10167 gdb_assert (b->type == bp_hardware_breakpoint);
10168
10169 /* Ranged breakpoints have only one location. */
10170 gdb_assert (bl && bl->next == NULL);
10171
10172 annotate_breakpoint (b->number);
f303dbd6
PA
10173
10174 maybe_print_thread_hit_breakpoint (uiout);
10175
f1310107 10176 if (b->disposition == disp_del)
112e8700 10177 uiout->text ("Temporary ranged breakpoint ");
f1310107 10178 else
112e8700
SM
10179 uiout->text ("Ranged breakpoint ");
10180 if (uiout->is_mi_like_p ())
f1310107 10181 {
112e8700 10182 uiout->field_string ("reason",
f1310107 10183 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10184 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10185 }
112e8700
SM
10186 uiout->field_int ("bkptno", b->number);
10187 uiout->text (", ");
f1310107
TJB
10188
10189 return PRINT_SRC_AND_LOC;
10190}
10191
10192/* Implement the "print_one" breakpoint_ops method for
10193 ranged breakpoints. */
10194
10195static void
10196print_one_ranged_breakpoint (struct breakpoint *b,
10197 struct bp_location **last_loc)
10198{
10199 struct bp_location *bl = b->loc;
10200 struct value_print_options opts;
79a45e25 10201 struct ui_out *uiout = current_uiout;
f1310107
TJB
10202
10203 /* Ranged breakpoints have only one location. */
10204 gdb_assert (bl && bl->next == NULL);
10205
10206 get_user_print_options (&opts);
10207
10208 if (opts.addressprint)
10209 /* We don't print the address range here, it will be printed later
10210 by print_one_detail_ranged_breakpoint. */
112e8700 10211 uiout->field_skip ("addr");
f1310107
TJB
10212 annotate_field (5);
10213 print_breakpoint_location (b, bl);
10214 *last_loc = bl;
10215}
10216
10217/* Implement the "print_one_detail" breakpoint_ops method for
10218 ranged breakpoints. */
10219
10220static void
10221print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10222 struct ui_out *uiout)
10223{
10224 CORE_ADDR address_start, address_end;
10225 struct bp_location *bl = b->loc;
d7e74731 10226 string_file stb;
f1310107
TJB
10227
10228 gdb_assert (bl);
10229
10230 address_start = bl->address;
10231 address_end = address_start + bl->length - 1;
10232
112e8700 10233 uiout->text ("\taddress range: ");
d7e74731
PA
10234 stb.printf ("[%s, %s]",
10235 print_core_address (bl->gdbarch, address_start),
10236 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10237 uiout->field_stream ("addr", stb);
10238 uiout->text ("\n");
f1310107
TJB
10239}
10240
10241/* Implement the "print_mention" breakpoint_ops method for
10242 ranged breakpoints. */
10243
10244static void
10245print_mention_ranged_breakpoint (struct breakpoint *b)
10246{
10247 struct bp_location *bl = b->loc;
79a45e25 10248 struct ui_out *uiout = current_uiout;
f1310107
TJB
10249
10250 gdb_assert (bl);
10251 gdb_assert (b->type == bp_hardware_breakpoint);
10252
112e8700 10253 if (uiout->is_mi_like_p ())
f1310107
TJB
10254 return;
10255
10256 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10257 b->number, paddress (bl->gdbarch, bl->address),
10258 paddress (bl->gdbarch, bl->address + bl->length - 1));
10259}
10260
10261/* Implement the "print_recreate" breakpoint_ops method for
10262 ranged breakpoints. */
10263
10264static void
10265print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10266{
f00aae0f 10267 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10268 event_location_to_string (b->location.get ()),
10269 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10270 print_recreate_thread (b, fp);
f1310107
TJB
10271}
10272
10273/* The breakpoint_ops structure to be used in ranged breakpoints. */
10274
2060206e 10275static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10276
10277/* Find the address where the end of the breakpoint range should be
10278 placed, given the SAL of the end of the range. This is so that if
10279 the user provides a line number, the end of the range is set to the
10280 last instruction of the given line. */
10281
10282static CORE_ADDR
10283find_breakpoint_range_end (struct symtab_and_line sal)
10284{
10285 CORE_ADDR end;
10286
10287 /* If the user provided a PC value, use it. Otherwise,
10288 find the address of the end of the given location. */
10289 if (sal.explicit_pc)
10290 end = sal.pc;
10291 else
10292 {
10293 int ret;
10294 CORE_ADDR start;
10295
10296 ret = find_line_pc_range (sal, &start, &end);
10297 if (!ret)
10298 error (_("Could not find location of the end of the range."));
10299
10300 /* find_line_pc_range returns the start of the next line. */
10301 end--;
10302 }
10303
10304 return end;
10305}
10306
10307/* Implement the "break-range" CLI command. */
10308
10309static void
10310break_range_command (char *arg, int from_tty)
10311{
870f88f7 10312 char *arg_start, *addr_string_start;
f1310107
TJB
10313 struct linespec_result canonical_start, canonical_end;
10314 int bp_count, can_use_bp, length;
10315 CORE_ADDR end;
10316 struct breakpoint *b;
10317 struct symtab_and_line sal_start, sal_end;
f1310107 10318 struct cleanup *cleanup_bkpt;
f8eba3c6 10319 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10320
10321 /* We don't support software ranged breakpoints. */
10322 if (target_ranged_break_num_registers () < 0)
10323 error (_("This target does not support hardware ranged breakpoints."));
10324
10325 bp_count = hw_breakpoint_used_count ();
10326 bp_count += target_ranged_break_num_registers ();
10327 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10328 bp_count, 0);
10329 if (can_use_bp < 0)
10330 error (_("Hardware breakpoints used exceeds limit."));
10331
f8eba3c6 10332 arg = skip_spaces (arg);
f1310107
TJB
10333 if (arg == NULL || arg[0] == '\0')
10334 error(_("No address range specified."));
10335
f8eba3c6 10336 arg_start = arg;
ffc2605c
TT
10337 event_location_up start_location = string_to_event_location (&arg,
10338 current_language);
10339 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10340
10341 if (arg[0] != ',')
10342 error (_("Too few arguments."));
f8eba3c6 10343 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10344 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10345
10346 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10347
10348 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10349 || lsal_start->sals.nelts != 1)
f1310107
TJB
10350 error (_("Cannot create a ranged breakpoint with multiple locations."));
10351
f8eba3c6
TT
10352 sal_start = lsal_start->sals.sals[0];
10353 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10354 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10355
10356 arg++; /* Skip the comma. */
f8eba3c6 10357 arg = skip_spaces (arg);
f1310107
TJB
10358
10359 /* Parse the end location. */
10360
f1310107
TJB
10361 arg_start = arg;
10362
f8eba3c6 10363 /* We call decode_line_full directly here instead of using
f1310107
TJB
10364 parse_breakpoint_sals because we need to specify the start location's
10365 symtab and line as the default symtab and line for the end of the
10366 range. This makes it possible to have ranges like "foo.c:27, +14",
10367 where +14 means 14 lines from the start location. */
ffc2605c
TT
10368 event_location_up end_location = string_to_event_location (&arg,
10369 current_language);
10370 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10371 sal_start.symtab, sal_start.line,
10372 &canonical_end, NULL, NULL);
10373
f8eba3c6 10374 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10375 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10376
10377 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10378 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10379 || lsal_end->sals.nelts != 1)
f1310107
TJB
10380 error (_("Cannot create a ranged breakpoint with multiple locations."));
10381
f8eba3c6 10382 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10383
10384 end = find_breakpoint_range_end (sal_end);
10385 if (sal_start.pc > end)
177b42fe 10386 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10387
10388 length = end - sal_start.pc + 1;
10389 if (length < 0)
10390 /* Length overflowed. */
10391 error (_("Address range too large."));
10392 else if (length == 1)
10393 {
10394 /* This range is simple enough to be handled by
10395 the `hbreak' command. */
10396 hbreak_command (addr_string_start, 1);
10397
10398 do_cleanups (cleanup_bkpt);
10399
10400 return;
10401 }
10402
10403 /* Now set up the breakpoint. */
10404 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10405 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10406 set_breakpoint_count (breakpoint_count + 1);
10407 b->number = breakpoint_count;
10408 b->disposition = disp_donttouch;
d28cd78a
TT
10409 b->location = std::move (start_location);
10410 b->location_range_end = std::move (end_location);
f1310107
TJB
10411 b->loc->length = length;
10412
f8eba3c6 10413 do_cleanups (cleanup_bkpt);
f1310107
TJB
10414
10415 mention (b);
8d3788bd 10416 observer_notify_breakpoint_created (b);
44702360 10417 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10418}
10419
4a64f543
MS
10420/* Return non-zero if EXP is verified as constant. Returned zero
10421 means EXP is variable. Also the constant detection may fail for
10422 some constant expressions and in such case still falsely return
10423 zero. */
2e6e3d9c 10424
65d79d4b
SDJ
10425static int
10426watchpoint_exp_is_const (const struct expression *exp)
10427{
10428 int i = exp->nelts;
10429
10430 while (i > 0)
10431 {
10432 int oplenp, argsp;
10433
10434 /* We are only interested in the descriptor of each element. */
10435 operator_length (exp, i, &oplenp, &argsp);
10436 i -= oplenp;
10437
10438 switch (exp->elts[i].opcode)
10439 {
10440 case BINOP_ADD:
10441 case BINOP_SUB:
10442 case BINOP_MUL:
10443 case BINOP_DIV:
10444 case BINOP_REM:
10445 case BINOP_MOD:
10446 case BINOP_LSH:
10447 case BINOP_RSH:
10448 case BINOP_LOGICAL_AND:
10449 case BINOP_LOGICAL_OR:
10450 case BINOP_BITWISE_AND:
10451 case BINOP_BITWISE_IOR:
10452 case BINOP_BITWISE_XOR:
10453 case BINOP_EQUAL:
10454 case BINOP_NOTEQUAL:
10455 case BINOP_LESS:
10456 case BINOP_GTR:
10457 case BINOP_LEQ:
10458 case BINOP_GEQ:
10459 case BINOP_REPEAT:
10460 case BINOP_COMMA:
10461 case BINOP_EXP:
10462 case BINOP_MIN:
10463 case BINOP_MAX:
10464 case BINOP_INTDIV:
10465 case BINOP_CONCAT:
65d79d4b
SDJ
10466 case TERNOP_COND:
10467 case TERNOP_SLICE:
65d79d4b
SDJ
10468
10469 case OP_LONG:
10470 case OP_DOUBLE:
10471 case OP_DECFLOAT:
10472 case OP_LAST:
10473 case OP_COMPLEX:
10474 case OP_STRING:
65d79d4b
SDJ
10475 case OP_ARRAY:
10476 case OP_TYPE:
608b4967
TT
10477 case OP_TYPEOF:
10478 case OP_DECLTYPE:
6e72ca20 10479 case OP_TYPEID:
65d79d4b
SDJ
10480 case OP_NAME:
10481 case OP_OBJC_NSSTRING:
10482
10483 case UNOP_NEG:
10484 case UNOP_LOGICAL_NOT:
10485 case UNOP_COMPLEMENT:
10486 case UNOP_ADDR:
10487 case UNOP_HIGH:
aeaa2474 10488 case UNOP_CAST:
9eaf6705
TT
10489
10490 case UNOP_CAST_TYPE:
10491 case UNOP_REINTERPRET_CAST:
10492 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10493 /* Unary, binary and ternary operators: We have to check
10494 their operands. If they are constant, then so is the
10495 result of that operation. For instance, if A and B are
10496 determined to be constants, then so is "A + B".
10497
10498 UNOP_IND is one exception to the rule above, because the
10499 value of *ADDR is not necessarily a constant, even when
10500 ADDR is. */
65d79d4b
SDJ
10501 break;
10502
10503 case OP_VAR_VALUE:
10504 /* Check whether the associated symbol is a constant.
4a64f543 10505
65d79d4b 10506 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10507 possible that a buggy compiler could mark a variable as
10508 constant even when it is not, and TYPE_CONST would return
10509 true in this case, while SYMBOL_CLASS wouldn't.
10510
10511 We also have to check for function symbols because they
10512 are always constant. */
65d79d4b
SDJ
10513 {
10514 struct symbol *s = exp->elts[i + 2].symbol;
10515
10516 if (SYMBOL_CLASS (s) != LOC_BLOCK
10517 && SYMBOL_CLASS (s) != LOC_CONST
10518 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10519 return 0;
10520 break;
10521 }
10522
10523 /* The default action is to return 0 because we are using
10524 the optimistic approach here: If we don't know something,
10525 then it is not a constant. */
10526 default:
10527 return 0;
10528 }
10529 }
10530
10531 return 1;
10532}
10533
3a5c3e22
PA
10534/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10535
10536static void
10537dtor_watchpoint (struct breakpoint *self)
10538{
10539 struct watchpoint *w = (struct watchpoint *) self;
10540
3a5c3e22
PA
10541 xfree (w->exp_string);
10542 xfree (w->exp_string_reparse);
10543 value_free (w->val);
10544
10545 base_breakpoint_ops.dtor (self);
10546}
10547
348d480f
PA
10548/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10549
10550static void
10551re_set_watchpoint (struct breakpoint *b)
10552{
3a5c3e22
PA
10553 struct watchpoint *w = (struct watchpoint *) b;
10554
348d480f
PA
10555 /* Watchpoint can be either on expression using entirely global
10556 variables, or it can be on local variables.
10557
10558 Watchpoints of the first kind are never auto-deleted, and even
10559 persist across program restarts. Since they can use variables
10560 from shared libraries, we need to reparse expression as libraries
10561 are loaded and unloaded.
10562
10563 Watchpoints on local variables can also change meaning as result
10564 of solib event. For example, if a watchpoint uses both a local
10565 and a global variables in expression, it's a local watchpoint,
10566 but unloading of a shared library will make the expression
10567 invalid. This is not a very common use case, but we still
10568 re-evaluate expression, to avoid surprises to the user.
10569
10570 Note that for local watchpoints, we re-evaluate it only if
10571 watchpoints frame id is still valid. If it's not, it means the
10572 watchpoint is out of scope and will be deleted soon. In fact,
10573 I'm not sure we'll ever be called in this case.
10574
10575 If a local watchpoint's frame id is still valid, then
3a5c3e22 10576 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10577
3a5c3e22
PA
10578 Don't do anything about disabled watchpoints, since they will be
10579 reevaluated again when enabled. */
10580 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10581}
10582
77b06cd7
TJB
10583/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10584
10585static int
10586insert_watchpoint (struct bp_location *bl)
10587{
3a5c3e22
PA
10588 struct watchpoint *w = (struct watchpoint *) bl->owner;
10589 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10590
10591 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10592 w->cond_exp.get ());
77b06cd7
TJB
10593}
10594
10595/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10596
10597static int
73971819 10598remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10599{
3a5c3e22
PA
10600 struct watchpoint *w = (struct watchpoint *) bl->owner;
10601 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10602
10603 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10604 w->cond_exp.get ());
e09342b5
TJB
10605}
10606
e09342b5 10607static int
348d480f 10608breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10609 struct address_space *aspace, CORE_ADDR bp_addr,
10610 const struct target_waitstatus *ws)
e09342b5 10611{
348d480f 10612 struct breakpoint *b = bl->owner;
3a5c3e22 10613 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10614
348d480f
PA
10615 /* Continuable hardware watchpoints are treated as non-existent if the
10616 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10617 some data address). Otherwise gdb won't stop on a break instruction
10618 in the code (not from a breakpoint) when a hardware watchpoint has
10619 been defined. Also skip watchpoints which we know did not trigger
10620 (did not match the data address). */
10621 if (is_hardware_watchpoint (b)
3a5c3e22 10622 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10623 return 0;
9c06b0b4 10624
348d480f 10625 return 1;
9c06b0b4
TJB
10626}
10627
348d480f
PA
10628static void
10629check_status_watchpoint (bpstat bs)
9c06b0b4 10630{
348d480f 10631 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10632
348d480f 10633 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10634}
10635
10636/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10637 hardware watchpoints. */
9c06b0b4
TJB
10638
10639static int
348d480f 10640resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10641{
3a5c3e22
PA
10642 struct watchpoint *w = (struct watchpoint *) bl->owner;
10643 int length = w->exact? 1 : bl->length;
348d480f
PA
10644
10645 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10646}
10647
10648/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10649 hardware watchpoints. */
9c06b0b4
TJB
10650
10651static int
348d480f 10652works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10653{
efa80663
PA
10654 /* Read and access watchpoints only work with hardware support. */
10655 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10656}
10657
9c06b0b4 10658static enum print_stop_action
348d480f 10659print_it_watchpoint (bpstat bs)
9c06b0b4 10660{
348d480f
PA
10661 struct cleanup *old_chain;
10662 struct breakpoint *b;
348d480f 10663 enum print_stop_action result;
3a5c3e22 10664 struct watchpoint *w;
79a45e25 10665 struct ui_out *uiout = current_uiout;
348d480f
PA
10666
10667 gdb_assert (bs->bp_location_at != NULL);
10668
348d480f 10669 b = bs->breakpoint_at;
3a5c3e22 10670 w = (struct watchpoint *) b;
348d480f 10671
d7e74731 10672 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10673
f303dbd6
PA
10674 annotate_watchpoint (b->number);
10675 maybe_print_thread_hit_breakpoint (uiout);
10676
d7e74731
PA
10677 string_file stb;
10678
9c06b0b4
TJB
10679 switch (b->type)
10680 {
348d480f 10681 case bp_watchpoint:
9c06b0b4 10682 case bp_hardware_watchpoint:
112e8700
SM
10683 if (uiout->is_mi_like_p ())
10684 uiout->field_string
10685 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10686 mention (b);
10687 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10688 uiout->text ("\nOld value = ");
d7e74731 10689 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10690 uiout->field_stream ("old", stb);
10691 uiout->text ("\nNew value = ");
d7e74731 10692 watchpoint_value_print (w->val, &stb);
112e8700
SM
10693 uiout->field_stream ("new", stb);
10694 uiout->text ("\n");
348d480f
PA
10695 /* More than one watchpoint may have been triggered. */
10696 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10697 break;
10698
10699 case bp_read_watchpoint:
112e8700
SM
10700 if (uiout->is_mi_like_p ())
10701 uiout->field_string
10702 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10703 mention (b);
10704 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10705 uiout->text ("\nValue = ");
d7e74731 10706 watchpoint_value_print (w->val, &stb);
112e8700
SM
10707 uiout->field_stream ("value", stb);
10708 uiout->text ("\n");
348d480f 10709 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10710 break;
10711
10712 case bp_access_watchpoint:
348d480f
PA
10713 if (bs->old_val != NULL)
10714 {
112e8700
SM
10715 if (uiout->is_mi_like_p ())
10716 uiout->field_string
10717 ("reason",
348d480f
PA
10718 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10719 mention (b);
10720 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10721 uiout->text ("\nOld value = ");
d7e74731 10722 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10723 uiout->field_stream ("old", stb);
10724 uiout->text ("\nNew value = ");
348d480f
PA
10725 }
10726 else
10727 {
10728 mention (b);
112e8700
SM
10729 if (uiout->is_mi_like_p ())
10730 uiout->field_string
10731 ("reason",
348d480f
PA
10732 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10733 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10734 uiout->text ("\nValue = ");
348d480f 10735 }
d7e74731 10736 watchpoint_value_print (w->val, &stb);
112e8700
SM
10737 uiout->field_stream ("new", stb);
10738 uiout->text ("\n");
348d480f 10739 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10740 break;
10741 default:
348d480f 10742 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10743 }
10744
348d480f
PA
10745 do_cleanups (old_chain);
10746 return result;
10747}
10748
10749/* Implement the "print_mention" breakpoint_ops method for hardware
10750 watchpoints. */
10751
10752static void
10753print_mention_watchpoint (struct breakpoint *b)
10754{
3a5c3e22 10755 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10756 struct ui_out *uiout = current_uiout;
46b9c129 10757 const char *tuple_name;
348d480f
PA
10758
10759 switch (b->type)
10760 {
10761 case bp_watchpoint:
112e8700 10762 uiout->text ("Watchpoint ");
46b9c129 10763 tuple_name = "wpt";
348d480f
PA
10764 break;
10765 case bp_hardware_watchpoint:
112e8700 10766 uiout->text ("Hardware watchpoint ");
46b9c129 10767 tuple_name = "wpt";
348d480f
PA
10768 break;
10769 case bp_read_watchpoint:
112e8700 10770 uiout->text ("Hardware read watchpoint ");
46b9c129 10771 tuple_name = "hw-rwpt";
348d480f
PA
10772 break;
10773 case bp_access_watchpoint:
112e8700 10774 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10775 tuple_name = "hw-awpt";
348d480f
PA
10776 break;
10777 default:
10778 internal_error (__FILE__, __LINE__,
10779 _("Invalid hardware watchpoint type."));
10780 }
10781
46b9c129 10782 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10783 uiout->field_int ("number", b->number);
10784 uiout->text (": ");
10785 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10786}
10787
10788/* Implement the "print_recreate" breakpoint_ops method for
10789 watchpoints. */
10790
10791static void
10792print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10793{
3a5c3e22
PA
10794 struct watchpoint *w = (struct watchpoint *) b;
10795
348d480f
PA
10796 switch (b->type)
10797 {
10798 case bp_watchpoint:
10799 case bp_hardware_watchpoint:
10800 fprintf_unfiltered (fp, "watch");
10801 break;
10802 case bp_read_watchpoint:
10803 fprintf_unfiltered (fp, "rwatch");
10804 break;
10805 case bp_access_watchpoint:
10806 fprintf_unfiltered (fp, "awatch");
10807 break;
10808 default:
10809 internal_error (__FILE__, __LINE__,
10810 _("Invalid watchpoint type."));
10811 }
10812
3a5c3e22 10813 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10814 print_recreate_thread (b, fp);
348d480f
PA
10815}
10816
427cd150
TT
10817/* Implement the "explains_signal" breakpoint_ops method for
10818 watchpoints. */
10819
47591c29 10820static int
427cd150
TT
10821explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10822{
10823 /* A software watchpoint cannot cause a signal other than
10824 GDB_SIGNAL_TRAP. */
10825 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10826 return 0;
427cd150 10827
47591c29 10828 return 1;
427cd150
TT
10829}
10830
348d480f
PA
10831/* The breakpoint_ops structure to be used in hardware watchpoints. */
10832
2060206e 10833static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10834
10835/* Implement the "insert" breakpoint_ops method for
10836 masked hardware watchpoints. */
10837
10838static int
10839insert_masked_watchpoint (struct bp_location *bl)
10840{
3a5c3e22
PA
10841 struct watchpoint *w = (struct watchpoint *) bl->owner;
10842
10843 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10844 bl->watchpoint_type);
10845}
10846
10847/* Implement the "remove" breakpoint_ops method for
10848 masked hardware watchpoints. */
10849
10850static int
73971819 10851remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10852{
3a5c3e22
PA
10853 struct watchpoint *w = (struct watchpoint *) bl->owner;
10854
10855 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10856 bl->watchpoint_type);
10857}
10858
10859/* Implement the "resources_needed" breakpoint_ops method for
10860 masked hardware watchpoints. */
10861
10862static int
10863resources_needed_masked_watchpoint (const struct bp_location *bl)
10864{
3a5c3e22
PA
10865 struct watchpoint *w = (struct watchpoint *) bl->owner;
10866
10867 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10868}
10869
10870/* Implement the "works_in_software_mode" breakpoint_ops method for
10871 masked hardware watchpoints. */
10872
10873static int
10874works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10875{
10876 return 0;
10877}
10878
10879/* Implement the "print_it" breakpoint_ops method for
10880 masked hardware watchpoints. */
10881
10882static enum print_stop_action
10883print_it_masked_watchpoint (bpstat bs)
10884{
10885 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10886 struct ui_out *uiout = current_uiout;
348d480f
PA
10887
10888 /* Masked watchpoints have only one location. */
10889 gdb_assert (b->loc && b->loc->next == NULL);
10890
f303dbd6
PA
10891 annotate_watchpoint (b->number);
10892 maybe_print_thread_hit_breakpoint (uiout);
10893
348d480f
PA
10894 switch (b->type)
10895 {
10896 case bp_hardware_watchpoint:
112e8700
SM
10897 if (uiout->is_mi_like_p ())
10898 uiout->field_string
10899 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10900 break;
10901
10902 case bp_read_watchpoint:
112e8700
SM
10903 if (uiout->is_mi_like_p ())
10904 uiout->field_string
10905 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10906 break;
10907
10908 case bp_access_watchpoint:
112e8700
SM
10909 if (uiout->is_mi_like_p ())
10910 uiout->field_string
10911 ("reason",
348d480f
PA
10912 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10913 break;
10914 default:
10915 internal_error (__FILE__, __LINE__,
10916 _("Invalid hardware watchpoint type."));
10917 }
10918
10919 mention (b);
112e8700 10920 uiout->text (_("\n\
9c06b0b4
TJB
10921Check the underlying instruction at PC for the memory\n\
10922address and value which triggered this watchpoint.\n"));
112e8700 10923 uiout->text ("\n");
9c06b0b4
TJB
10924
10925 /* More than one watchpoint may have been triggered. */
10926 return PRINT_UNKNOWN;
10927}
10928
10929/* Implement the "print_one_detail" breakpoint_ops method for
10930 masked hardware watchpoints. */
10931
10932static void
10933print_one_detail_masked_watchpoint (const struct breakpoint *b,
10934 struct ui_out *uiout)
10935{
3a5c3e22
PA
10936 struct watchpoint *w = (struct watchpoint *) b;
10937
9c06b0b4
TJB
10938 /* Masked watchpoints have only one location. */
10939 gdb_assert (b->loc && b->loc->next == NULL);
10940
112e8700
SM
10941 uiout->text ("\tmask ");
10942 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10943 uiout->text ("\n");
9c06b0b4
TJB
10944}
10945
10946/* Implement the "print_mention" breakpoint_ops method for
10947 masked hardware watchpoints. */
10948
10949static void
10950print_mention_masked_watchpoint (struct breakpoint *b)
10951{
3a5c3e22 10952 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10953 struct ui_out *uiout = current_uiout;
46b9c129 10954 const char *tuple_name;
9c06b0b4
TJB
10955
10956 switch (b->type)
10957 {
10958 case bp_hardware_watchpoint:
112e8700 10959 uiout->text ("Masked hardware watchpoint ");
46b9c129 10960 tuple_name = "wpt";
9c06b0b4
TJB
10961 break;
10962 case bp_read_watchpoint:
112e8700 10963 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10964 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10965 break;
10966 case bp_access_watchpoint:
112e8700 10967 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10968 tuple_name = "hw-awpt";
9c06b0b4
TJB
10969 break;
10970 default:
10971 internal_error (__FILE__, __LINE__,
10972 _("Invalid hardware watchpoint type."));
10973 }
10974
46b9c129 10975 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10976 uiout->field_int ("number", b->number);
10977 uiout->text (": ");
10978 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10979}
10980
10981/* Implement the "print_recreate" breakpoint_ops method for
10982 masked hardware watchpoints. */
10983
10984static void
10985print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10986{
3a5c3e22 10987 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10988 char tmp[40];
10989
10990 switch (b->type)
10991 {
10992 case bp_hardware_watchpoint:
10993 fprintf_unfiltered (fp, "watch");
10994 break;
10995 case bp_read_watchpoint:
10996 fprintf_unfiltered (fp, "rwatch");
10997 break;
10998 case bp_access_watchpoint:
10999 fprintf_unfiltered (fp, "awatch");
11000 break;
11001 default:
11002 internal_error (__FILE__, __LINE__,
11003 _("Invalid hardware watchpoint type."));
11004 }
11005
3a5c3e22
PA
11006 sprintf_vma (tmp, w->hw_wp_mask);
11007 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11008 print_recreate_thread (b, fp);
9c06b0b4
TJB
11009}
11010
11011/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11012
2060206e 11013static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11014
11015/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11016
11017static int
11018is_masked_watchpoint (const struct breakpoint *b)
11019{
11020 return b->ops == &masked_watchpoint_breakpoint_ops;
11021}
11022
53a5351d
JM
11023/* accessflag: hw_write: watch write,
11024 hw_read: watch read,
11025 hw_access: watch access (read or write) */
c906108c 11026static void
bbc13ae3 11027watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11028 int just_location, int internal)
c906108c 11029{
d983da9c 11030 struct breakpoint *b, *scope_breakpoint = NULL;
270140bd 11031 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11032 struct value *val, *mark, *result;
bb9d5f81 11033 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
11034 const char *exp_start = NULL;
11035 const char *exp_end = NULL;
11036 const char *tok, *end_tok;
9c06b0b4 11037 int toklen = -1;
bbc13ae3
KS
11038 const char *cond_start = NULL;
11039 const char *cond_end = NULL;
c906108c 11040 enum bptype bp_type;
37e4754d 11041 int thread = -1;
0cf6dd15 11042 int pc = 0;
9c06b0b4
TJB
11043 /* Flag to indicate whether we are going to use masks for
11044 the hardware watchpoint. */
11045 int use_mask = 0;
11046 CORE_ADDR mask = 0;
3a5c3e22 11047 struct watchpoint *w;
bbc13ae3
KS
11048 char *expression;
11049 struct cleanup *back_to;
c906108c 11050
37e4754d
LM
11051 /* Make sure that we actually have parameters to parse. */
11052 if (arg != NULL && arg[0] != '\0')
11053 {
bbc13ae3
KS
11054 const char *value_start;
11055
11056 exp_end = arg + strlen (arg);
37e4754d 11057
9c06b0b4
TJB
11058 /* Look for "parameter value" pairs at the end
11059 of the arguments string. */
bbc13ae3 11060 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11061 {
11062 /* Skip whitespace at the end of the argument list. */
11063 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11064 tok--;
11065
11066 /* Find the beginning of the last token.
11067 This is the value of the parameter. */
11068 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11069 tok--;
11070 value_start = tok + 1;
11071
11072 /* Skip whitespace. */
11073 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11074 tok--;
11075
11076 end_tok = tok;
11077
11078 /* Find the beginning of the second to last token.
11079 This is the parameter itself. */
11080 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11081 tok--;
11082 tok++;
11083 toklen = end_tok - tok + 1;
11084
61012eef 11085 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11086 {
5d5658a1 11087 struct thread_info *thr;
9c06b0b4
TJB
11088 /* At this point we've found a "thread" token, which means
11089 the user is trying to set a watchpoint that triggers
11090 only in a specific thread. */
5d5658a1 11091 const char *endp;
37e4754d 11092
9c06b0b4
TJB
11093 if (thread != -1)
11094 error(_("You can specify only one thread."));
37e4754d 11095
9c06b0b4 11096 /* Extract the thread ID from the next token. */
5d5658a1 11097 thr = parse_thread_id (value_start, &endp);
37e4754d 11098
5d5658a1 11099 /* Check if the user provided a valid thread ID. */
9c06b0b4 11100 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11101 invalid_thread_id_error (value_start);
9c06b0b4 11102
5d5658a1 11103 thread = thr->global_num;
9c06b0b4 11104 }
61012eef 11105 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11106 {
11107 /* We've found a "mask" token, which means the user wants to
11108 create a hardware watchpoint that is going to have the mask
11109 facility. */
11110 struct value *mask_value, *mark;
37e4754d 11111
9c06b0b4
TJB
11112 if (use_mask)
11113 error(_("You can specify only one mask."));
37e4754d 11114
9c06b0b4 11115 use_mask = just_location = 1;
37e4754d 11116
9c06b0b4
TJB
11117 mark = value_mark ();
11118 mask_value = parse_to_comma_and_eval (&value_start);
11119 mask = value_as_address (mask_value);
11120 value_free_to_mark (mark);
11121 }
11122 else
11123 /* We didn't recognize what we found. We should stop here. */
11124 break;
37e4754d 11125
9c06b0b4
TJB
11126 /* Truncate the string and get rid of the "parameter value" pair before
11127 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11128 exp_end = tok;
9c06b0b4 11129 }
37e4754d 11130 }
bbc13ae3
KS
11131 else
11132 exp_end = arg;
37e4754d 11133
bbc13ae3
KS
11134 /* Parse the rest of the arguments. From here on out, everything
11135 is in terms of a newly allocated string instead of the original
11136 ARG. */
c906108c 11137 innermost_block = NULL;
bbc13ae3
KS
11138 expression = savestring (arg, exp_end - arg);
11139 back_to = make_cleanup (xfree, expression);
11140 exp_start = arg = expression;
4d01a485 11141 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11142 exp_end = arg;
fa8a61dc
TT
11143 /* Remove trailing whitespace from the expression before saving it.
11144 This makes the eventual display of the expression string a bit
11145 prettier. */
11146 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11147 --exp_end;
11148
65d79d4b 11149 /* Checking if the expression is not constant. */
4d01a485 11150 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11151 {
11152 int len;
11153
11154 len = exp_end - exp_start;
11155 while (len > 0 && isspace (exp_start[len - 1]))
11156 len--;
11157 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11158 }
11159
c906108c
SS
11160 exp_valid_block = innermost_block;
11161 mark = value_mark ();
4d01a485 11162 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11163
bb9d5f81
PP
11164 if (val != NULL && just_location)
11165 {
11166 saved_bitpos = value_bitpos (val);
11167 saved_bitsize = value_bitsize (val);
11168 }
11169
06a64a0b
TT
11170 if (just_location)
11171 {
9c06b0b4
TJB
11172 int ret;
11173
06a64a0b 11174 exp_valid_block = NULL;
a1442452 11175 val = value_addr (result);
06a64a0b
TT
11176 release_value (val);
11177 value_free_to_mark (mark);
9c06b0b4
TJB
11178
11179 if (use_mask)
11180 {
11181 ret = target_masked_watch_num_registers (value_as_address (val),
11182 mask);
11183 if (ret == -1)
11184 error (_("This target does not support masked watchpoints."));
11185 else if (ret == -2)
11186 error (_("Invalid mask or memory region."));
11187 }
06a64a0b
TT
11188 }
11189 else if (val != NULL)
fa4727a6 11190 release_value (val);
c906108c 11191
bbc13ae3
KS
11192 tok = skip_spaces_const (arg);
11193 end_tok = skip_to_space_const (tok);
c906108c
SS
11194
11195 toklen = end_tok - tok;
11196 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11197 {
60e1c644 11198 innermost_block = NULL;
c906108c 11199 tok = cond_start = end_tok + 1;
4d01a485 11200 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11201
11202 /* The watchpoint expression may not be local, but the condition
11203 may still be. E.g.: `watch global if local > 0'. */
11204 cond_exp_valid_block = innermost_block;
11205
c906108c
SS
11206 cond_end = tok;
11207 }
11208 if (*tok)
8a3fe4f8 11209 error (_("Junk at end of command."));
c906108c 11210
441d7c93
PA
11211 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11212
11213 /* Save this because create_internal_breakpoint below invalidates
11214 'wp_frame'. */
11215 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11216
11217 /* If the expression is "local", then set up a "watchpoint scope"
11218 breakpoint at the point where we've left the scope of the watchpoint
11219 expression. Create the scope breakpoint before the watchpoint, so
11220 that we will encounter it first in bpstat_stop_status. */
441d7c93 11221 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11222 {
441d7c93
PA
11223 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11224
11225 if (frame_id_p (caller_frame_id))
edb3359d 11226 {
441d7c93
PA
11227 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11228 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11229
edb3359d 11230 scope_breakpoint
441d7c93 11231 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11232 bp_watchpoint_scope,
11233 &momentary_breakpoint_ops);
d983da9c 11234
441d7c93
PA
11235 /* create_internal_breakpoint could invalidate WP_FRAME. */
11236 wp_frame = NULL;
11237
edb3359d 11238 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11239
edb3359d
DJ
11240 /* Automatically delete the breakpoint when it hits. */
11241 scope_breakpoint->disposition = disp_del;
d983da9c 11242
edb3359d 11243 /* Only break in the proper frame (help with recursion). */
441d7c93 11244 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11245
edb3359d 11246 /* Set the address at which we will stop. */
441d7c93
PA
11247 scope_breakpoint->loc->gdbarch = caller_arch;
11248 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11249 scope_breakpoint->loc->address
a6d9a66e
UW
11250 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11251 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11252 scope_breakpoint->type);
11253 }
d983da9c
DJ
11254 }
11255
e8369a73
AB
11256 /* Now set up the breakpoint. We create all watchpoints as hardware
11257 watchpoints here even if hardware watchpoints are turned off, a call
11258 to update_watchpoint later in this function will cause the type to
11259 drop back to bp_watchpoint (software watchpoint) if required. */
11260
11261 if (accessflag == hw_read)
11262 bp_type = bp_read_watchpoint;
11263 else if (accessflag == hw_access)
11264 bp_type = bp_access_watchpoint;
11265 else
11266 bp_type = bp_hardware_watchpoint;
3a5c3e22 11267
4d01a485 11268 w = new watchpoint ();
3a5c3e22 11269 b = &w->base;
348d480f 11270 if (use_mask)
3a5c3e22
PA
11271 init_raw_breakpoint_without_location (b, NULL, bp_type,
11272 &masked_watchpoint_breakpoint_ops);
348d480f 11273 else
3a5c3e22
PA
11274 init_raw_breakpoint_without_location (b, NULL, bp_type,
11275 &watchpoint_breakpoint_ops);
37e4754d 11276 b->thread = thread;
b5de0fa7 11277 b->disposition = disp_donttouch;
348d480f 11278 b->pspace = current_program_space;
b22e99fd 11279 w->exp = std::move (exp);
3a5c3e22
PA
11280 w->exp_valid_block = exp_valid_block;
11281 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11282 if (just_location)
11283 {
11284 struct type *t = value_type (val);
11285 CORE_ADDR addr = value_as_address (val);
06a64a0b
TT
11286
11287 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
06a64a0b 11288
2f408ecb
PA
11289 std::string name = type_to_string (t);
11290
11291 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
d63d0675 11292 core_addr_to_string (addr));
06a64a0b 11293
3a5c3e22 11294 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11295 (int) (exp_end - exp_start), exp_start);
11296
06a64a0b
TT
11297 /* The above expression is in C. */
11298 b->language = language_c;
11299 }
11300 else
3a5c3e22 11301 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11302
11303 if (use_mask)
11304 {
3a5c3e22 11305 w->hw_wp_mask = mask;
9c06b0b4
TJB
11306 }
11307 else
11308 {
3a5c3e22 11309 w->val = val;
bb9d5f81
PP
11310 w->val_bitpos = saved_bitpos;
11311 w->val_bitsize = saved_bitsize;
3a5c3e22 11312 w->val_valid = 1;
9c06b0b4 11313 }
77b06cd7 11314
c906108c
SS
11315 if (cond_start)
11316 b->cond_string = savestring (cond_start, cond_end - cond_start);
11317 else
11318 b->cond_string = 0;
c5aa993b 11319
441d7c93 11320 if (frame_id_p (watchpoint_frame))
f6bc2008 11321 {
441d7c93 11322 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11323 w->watchpoint_thread = inferior_ptid;
f6bc2008 11324 }
c906108c 11325 else
f6bc2008 11326 {
3a5c3e22
PA
11327 w->watchpoint_frame = null_frame_id;
11328 w->watchpoint_thread = null_ptid;
f6bc2008 11329 }
c906108c 11330
d983da9c 11331 if (scope_breakpoint != NULL)
c906108c 11332 {
d983da9c
DJ
11333 /* The scope breakpoint is related to the watchpoint. We will
11334 need to act on them together. */
11335 b->related_breakpoint = scope_breakpoint;
11336 scope_breakpoint->related_breakpoint = b;
c906108c 11337 }
d983da9c 11338
06a64a0b
TT
11339 if (!just_location)
11340 value_free_to_mark (mark);
2d134ed3 11341
492d29ea 11342 TRY
a9634178
TJB
11343 {
11344 /* Finally update the new watchpoint. This creates the locations
11345 that should be inserted. */
3a5c3e22 11346 update_watchpoint (w, 1);
a9634178 11347 }
492d29ea 11348 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11349 {
11350 delete_breakpoint (b);
11351 throw_exception (e);
11352 }
492d29ea 11353 END_CATCH
a9634178 11354
3ea46bff 11355 install_breakpoint (internal, b, 1);
bbc13ae3 11356 do_cleanups (back_to);
c906108c
SS
11357}
11358
e09342b5 11359/* Return count of debug registers needed to watch the given expression.
e09342b5 11360 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11361
c906108c 11362static int
a9634178 11363can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11364{
11365 int found_memory_cnt = 0;
2e70b7b9 11366 struct value *head = v;
c906108c
SS
11367
11368 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11369 if (!can_use_hw_watchpoints)
c906108c 11370 return 0;
c5aa993b 11371
5c44784c
JM
11372 /* Make sure that the value of the expression depends only upon
11373 memory contents, and values computed from them within GDB. If we
11374 find any register references or function calls, we can't use a
11375 hardware watchpoint.
11376
11377 The idea here is that evaluating an expression generates a series
11378 of values, one holding the value of every subexpression. (The
11379 expression a*b+c has five subexpressions: a, b, a*b, c, and
11380 a*b+c.) GDB's values hold almost enough information to establish
11381 the criteria given above --- they identify memory lvalues,
11382 register lvalues, computed values, etcetera. So we can evaluate
11383 the expression, and then scan the chain of values that leaves
11384 behind to decide whether we can detect any possible change to the
11385 expression's final value using only hardware watchpoints.
11386
11387 However, I don't think that the values returned by inferior
11388 function calls are special in any way. So this function may not
11389 notice that an expression involving an inferior function call
11390 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11391 for (; v; v = value_next (v))
c906108c 11392 {
5c44784c 11393 if (VALUE_LVAL (v) == lval_memory)
c906108c 11394 {
8464be76
DJ
11395 if (v != head && value_lazy (v))
11396 /* A lazy memory lvalue in the chain is one that GDB never
11397 needed to fetch; we either just used its address (e.g.,
11398 `a' in `a.b') or we never needed it at all (e.g., `a'
11399 in `a,b'). This doesn't apply to HEAD; if that is
11400 lazy then it was not readable, but watch it anyway. */
5c44784c 11401 ;
53a5351d 11402 else
5c44784c
JM
11403 {
11404 /* Ahh, memory we actually used! Check if we can cover
11405 it with hardware watchpoints. */
df407dfe 11406 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11407
11408 /* We only watch structs and arrays if user asked for it
11409 explicitly, never if they just happen to appear in a
11410 middle of some value chain. */
11411 if (v == head
11412 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11413 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11414 {
42ae5230 11415 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11416 int len;
11417 int num_regs;
11418
a9634178 11419 len = (target_exact_watchpoints
e09342b5
TJB
11420 && is_scalar_type_recursive (vtype))?
11421 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11422
e09342b5
TJB
11423 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11424 if (!num_regs)
2e70b7b9
MS
11425 return 0;
11426 else
e09342b5 11427 found_memory_cnt += num_regs;
2e70b7b9 11428 }
5c44784c 11429 }
c5aa993b 11430 }
5086187c
AC
11431 else if (VALUE_LVAL (v) != not_lval
11432 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11433 return 0; /* These are values from the history (e.g., $1). */
5086187c 11434 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11435 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11436 }
11437
11438 /* The expression itself looks suitable for using a hardware
11439 watchpoint, but give the target machine a chance to reject it. */
11440 return found_memory_cnt;
11441}
11442
8b93c638 11443void
84f4c1fe 11444watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11445{
84f4c1fe 11446 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11447}
11448
06a64a0b
TT
11449/* A helper function that looks for the "-location" argument and then
11450 calls watch_command_1. */
11451
11452static void
11453watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11454{
11455 int just_location = 0;
11456
11457 if (arg
11458 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11459 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11460 {
e9cafbcc 11461 arg = skip_spaces (arg);
06a64a0b
TT
11462 just_location = 1;
11463 }
11464
84f4c1fe 11465 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11466}
8926118c 11467
c5aa993b 11468static void
fba45db2 11469watch_command (char *arg, int from_tty)
c906108c 11470{
06a64a0b 11471 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11472}
11473
8b93c638 11474void
84f4c1fe 11475rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11476{
84f4c1fe 11477 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11478}
8926118c 11479
c5aa993b 11480static void
fba45db2 11481rwatch_command (char *arg, int from_tty)
c906108c 11482{
06a64a0b 11483 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11484}
11485
8b93c638 11486void
84f4c1fe 11487awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11488{
84f4c1fe 11489 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11490}
8926118c 11491
c5aa993b 11492static void
fba45db2 11493awatch_command (char *arg, int from_tty)
c906108c 11494{
06a64a0b 11495 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11496}
c906108c 11497\f
c5aa993b 11498
cfc31633
PA
11499/* Data for the FSM that manages the until(location)/advance commands
11500 in infcmd.c. Here because it uses the mechanisms of
11501 breakpoints. */
c906108c 11502
cfc31633 11503struct until_break_fsm
bfec99b2 11504{
cfc31633
PA
11505 /* The base class. */
11506 struct thread_fsm thread_fsm;
11507
11508 /* The thread that as current when the command was executed. */
11509 int thread;
11510
11511 /* The breakpoint set at the destination location. */
11512 struct breakpoint *location_breakpoint;
11513
11514 /* Breakpoint set at the return address in the caller frame. May be
11515 NULL. */
11516 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11517};
11518
8980e177
PA
11519static void until_break_fsm_clean_up (struct thread_fsm *self,
11520 struct thread_info *thread);
11521static int until_break_fsm_should_stop (struct thread_fsm *self,
11522 struct thread_info *thread);
cfc31633
PA
11523static enum async_reply_reason
11524 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11525
11526/* until_break_fsm's vtable. */
11527
11528static struct thread_fsm_ops until_break_fsm_ops =
11529{
11530 NULL, /* dtor */
11531 until_break_fsm_clean_up,
11532 until_break_fsm_should_stop,
11533 NULL, /* return_value */
11534 until_break_fsm_async_reply_reason,
11535};
11536
11537/* Allocate a new until_break_command_fsm. */
11538
11539static struct until_break_fsm *
8980e177 11540new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11541 struct breakpoint *location_breakpoint,
11542 struct breakpoint *caller_breakpoint)
11543{
11544 struct until_break_fsm *sm;
11545
11546 sm = XCNEW (struct until_break_fsm);
8980e177 11547 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11548
11549 sm->thread = thread;
11550 sm->location_breakpoint = location_breakpoint;
11551 sm->caller_breakpoint = caller_breakpoint;
11552
11553 return sm;
11554}
11555
11556/* Implementation of the 'should_stop' FSM method for the
11557 until(location)/advance commands. */
11558
11559static int
8980e177
PA
11560until_break_fsm_should_stop (struct thread_fsm *self,
11561 struct thread_info *tp)
cfc31633
PA
11562{
11563 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11564
11565 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11566 sm->location_breakpoint) != NULL
11567 || (sm->caller_breakpoint != NULL
11568 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11569 sm->caller_breakpoint) != NULL))
11570 thread_fsm_set_finished (self);
11571
11572 return 1;
11573}
11574
11575/* Implementation of the 'clean_up' FSM method for the
11576 until(location)/advance commands. */
11577
c2c6d25f 11578static void
8980e177
PA
11579until_break_fsm_clean_up (struct thread_fsm *self,
11580 struct thread_info *thread)
43ff13b4 11581{
cfc31633 11582 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11583
cfc31633
PA
11584 /* Clean up our temporary breakpoints. */
11585 if (sm->location_breakpoint != NULL)
11586 {
11587 delete_breakpoint (sm->location_breakpoint);
11588 sm->location_breakpoint = NULL;
11589 }
11590 if (sm->caller_breakpoint != NULL)
11591 {
11592 delete_breakpoint (sm->caller_breakpoint);
11593 sm->caller_breakpoint = NULL;
11594 }
11595 delete_longjmp_breakpoint (sm->thread);
11596}
11597
11598/* Implementation of the 'async_reply_reason' FSM method for the
11599 until(location)/advance commands. */
11600
11601static enum async_reply_reason
11602until_break_fsm_async_reply_reason (struct thread_fsm *self)
11603{
11604 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11605}
11606
c906108c 11607void
ae66c1fc 11608until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11609{
11610 struct symtabs_and_lines sals;
11611 struct symtab_and_line sal;
8556afb4
PA
11612 struct frame_info *frame;
11613 struct gdbarch *frame_gdbarch;
11614 struct frame_id stack_frame_id;
11615 struct frame_id caller_frame_id;
cfc31633
PA
11616 struct breakpoint *location_breakpoint;
11617 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11618 struct cleanup *old_chain;
186c406b
TT
11619 int thread;
11620 struct thread_info *tp;
cfc31633 11621 struct until_break_fsm *sm;
c906108c 11622
70509625 11623 clear_proceed_status (0);
c906108c
SS
11624
11625 /* Set a breakpoint where the user wants it and at return from
4a64f543 11626 this function. */
c5aa993b 11627
ffc2605c 11628 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11629
1bfeeb0f 11630 if (last_displayed_sal_is_valid ())
ffc2605c 11631 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11632 get_last_displayed_symtab (),
f8eba3c6 11633 get_last_displayed_line ());
c906108c 11634 else
ffc2605c 11635 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11636 NULL, (struct symtab *) NULL, 0);
c5aa993b 11637
c906108c 11638 if (sals.nelts != 1)
8a3fe4f8 11639 error (_("Couldn't get information on specified line."));
c5aa993b 11640
c906108c 11641 sal = sals.sals[0];
4a64f543 11642 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11643
c906108c 11644 if (*arg)
8a3fe4f8 11645 error (_("Junk at end of arguments."));
c5aa993b 11646
c906108c 11647 resolve_sal_pc (&sal);
c5aa993b 11648
186c406b 11649 tp = inferior_thread ();
5d5658a1 11650 thread = tp->global_num;
186c406b 11651
883bc8d1
PA
11652 old_chain = make_cleanup (null_cleanup, NULL);
11653
8556afb4
PA
11654 /* Note linespec handling above invalidates the frame chain.
11655 Installing a breakpoint also invalidates the frame chain (as it
11656 may need to switch threads), so do any frame handling before
11657 that. */
11658
11659 frame = get_selected_frame (NULL);
11660 frame_gdbarch = get_frame_arch (frame);
11661 stack_frame_id = get_stack_frame_id (frame);
11662 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11663
ae66c1fc
EZ
11664 /* Keep within the current frame, or in frames called by the current
11665 one. */
edb3359d 11666
883bc8d1 11667 if (frame_id_p (caller_frame_id))
c906108c 11668 {
883bc8d1 11669 struct symtab_and_line sal2;
cfc31633 11670 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11671
11672 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11673 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11674 caller_gdbarch = frame_unwind_caller_arch (frame);
11675 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11676 sal2,
11677 caller_frame_id,
11678 bp_until);
11679 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11680
883bc8d1 11681 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11682 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11683 }
c5aa993b 11684
c70a6932
JK
11685 /* set_momentary_breakpoint could invalidate FRAME. */
11686 frame = NULL;
11687
883bc8d1
PA
11688 if (anywhere)
11689 /* If the user told us to continue until a specified location,
11690 we don't specify a frame at which we need to stop. */
cfc31633
PA
11691 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11692 null_frame_id, bp_until);
883bc8d1
PA
11693 else
11694 /* Otherwise, specify the selected frame, because we want to stop
11695 only at the very same frame. */
cfc31633
PA
11696 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11697 stack_frame_id, bp_until);
11698 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11699
8980e177 11700 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11701 location_breakpoint, caller_breakpoint);
cfc31633 11702 tp->thread_fsm = &sm->thread_fsm;
f107f563 11703
cfc31633 11704 discard_cleanups (old_chain);
f107f563 11705
cfc31633 11706 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11707}
ae66c1fc 11708
c906108c
SS
11709/* This function attempts to parse an optional "if <cond>" clause
11710 from the arg string. If one is not found, it returns NULL.
c5aa993b 11711
c906108c
SS
11712 Else, it returns a pointer to the condition string. (It does not
11713 attempt to evaluate the string against a particular block.) And,
11714 it updates arg to point to the first character following the parsed
4a64f543 11715 if clause in the arg string. */
53a5351d 11716
63160a43
PA
11717const char *
11718ep_parse_optional_if_clause (const char **arg)
c906108c 11719{
63160a43 11720 const char *cond_string;
c5aa993b
JM
11721
11722 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11723 return NULL;
c5aa993b 11724
4a64f543 11725 /* Skip the "if" keyword. */
c906108c 11726 (*arg) += 2;
c5aa993b 11727
c906108c 11728 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11729 condition string. */
63160a43 11730 *arg = skip_spaces_const (*arg);
c906108c 11731 cond_string = *arg;
c5aa993b 11732
4a64f543
MS
11733 /* Assume that the condition occupies the remainder of the arg
11734 string. */
c906108c 11735 (*arg) += strlen (cond_string);
c5aa993b 11736
c906108c
SS
11737 return cond_string;
11738}
c5aa993b 11739
c906108c
SS
11740/* Commands to deal with catching events, such as signals, exceptions,
11741 process start/exit, etc. */
c5aa993b
JM
11742
11743typedef enum
11744{
44feb3ce
TT
11745 catch_fork_temporary, catch_vfork_temporary,
11746 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11747}
11748catch_fork_kind;
11749
c906108c 11750static void
63160a43 11751catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11752 struct cmd_list_element *command)
c906108c 11753{
63160a43 11754 const char *arg = arg_entry;
a6d9a66e 11755 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11756 const char *cond_string = NULL;
44feb3ce
TT
11757 catch_fork_kind fork_kind;
11758 int tempflag;
11759
11760 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11761 tempflag = (fork_kind == catch_fork_temporary
11762 || fork_kind == catch_vfork_temporary);
c5aa993b 11763
44feb3ce
TT
11764 if (!arg)
11765 arg = "";
63160a43 11766 arg = skip_spaces_const (arg);
c5aa993b 11767
c906108c 11768 /* The allowed syntax is:
c5aa993b
JM
11769 catch [v]fork
11770 catch [v]fork if <cond>
11771
4a64f543 11772 First, check if there's an if clause. */
c906108c 11773 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11774
c906108c 11775 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11776 error (_("Junk at end of arguments."));
c5aa993b 11777
c906108c 11778 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11779 and enable reporting of such events. */
c5aa993b
JM
11780 switch (fork_kind)
11781 {
44feb3ce
TT
11782 case catch_fork_temporary:
11783 case catch_fork_permanent:
a6d9a66e 11784 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11785 &catch_fork_breakpoint_ops);
c906108c 11786 break;
44feb3ce
TT
11787 case catch_vfork_temporary:
11788 case catch_vfork_permanent:
a6d9a66e 11789 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11790 &catch_vfork_breakpoint_ops);
c906108c 11791 break;
c5aa993b 11792 default:
8a3fe4f8 11793 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11794 break;
c5aa993b 11795 }
c906108c
SS
11796}
11797
11798static void
63160a43 11799catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11800 struct cmd_list_element *command)
c906108c 11801{
63160a43 11802 const char *arg = arg_entry;
b4d90040 11803 struct exec_catchpoint *c;
a6d9a66e 11804 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11805 int tempflag;
63160a43 11806 const char *cond_string = NULL;
c906108c 11807
44feb3ce
TT
11808 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11809
11810 if (!arg)
11811 arg = "";
63160a43 11812 arg = skip_spaces_const (arg);
c906108c
SS
11813
11814 /* The allowed syntax is:
c5aa993b
JM
11815 catch exec
11816 catch exec if <cond>
c906108c 11817
4a64f543 11818 First, check if there's an if clause. */
c906108c
SS
11819 cond_string = ep_parse_optional_if_clause (&arg);
11820
11821 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11822 error (_("Junk at end of arguments."));
c906108c 11823
4d01a485 11824 c = new exec_catchpoint ();
b4d90040
PA
11825 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11826 &catch_exec_breakpoint_ops);
11827 c->exec_pathname = NULL;
11828
3ea46bff 11829 install_breakpoint (0, &c->base, 1);
c906108c 11830}
c5aa993b 11831
9ac4176b 11832void
28010a5d
PA
11833init_ada_exception_breakpoint (struct breakpoint *b,
11834 struct gdbarch *gdbarch,
11835 struct symtab_and_line sal,
11836 char *addr_string,
c0a91b2b 11837 const struct breakpoint_ops *ops,
28010a5d 11838 int tempflag,
349774ef 11839 int enabled,
28010a5d 11840 int from_tty)
f7f9143b 11841{
f7f9143b
JB
11842 if (from_tty)
11843 {
5af949e3
UW
11844 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11845 if (!loc_gdbarch)
11846 loc_gdbarch = gdbarch;
11847
6c95b8df
PA
11848 describe_other_breakpoints (loc_gdbarch,
11849 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11850 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11851 version for exception catchpoints, because two catchpoints
11852 used for different exception names will use the same address.
11853 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11854 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11855 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11856 the user what type of catchpoint it is. The above is good
11857 enough for now, though. */
11858 }
11859
28010a5d 11860 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11861
349774ef 11862 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11863 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11864 b->location = string_to_event_location (&addr_string,
11865 language_def (language_ada));
f7f9143b 11866 b->language = language_ada;
f7f9143b
JB
11867}
11868
c906108c 11869static void
fba45db2 11870catch_command (char *arg, int from_tty)
c906108c 11871{
44feb3ce 11872 error (_("Catch requires an event name."));
c906108c
SS
11873}
11874\f
11875
11876static void
fba45db2 11877tcatch_command (char *arg, int from_tty)
c906108c 11878{
44feb3ce 11879 error (_("Catch requires an event name."));
c906108c
SS
11880}
11881
8a2c437b
TT
11882/* A qsort comparison function that sorts breakpoints in order. */
11883
11884static int
11885compare_breakpoints (const void *a, const void *b)
11886{
9a3c8263 11887 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11888 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11889 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11890 uintptr_t ub = (uintptr_t) *bb;
11891
11892 if ((*ba)->number < (*bb)->number)
11893 return -1;
11894 else if ((*ba)->number > (*bb)->number)
11895 return 1;
11896
11897 /* Now sort by address, in case we see, e..g, two breakpoints with
11898 the number 0. */
11899 if (ua < ub)
11900 return -1;
94b0e70d 11901 return ua > ub ? 1 : 0;
8a2c437b
TT
11902}
11903
80f8a6eb 11904/* Delete breakpoints by address or line. */
c906108c
SS
11905
11906static void
fba45db2 11907clear_command (char *arg, int from_tty)
c906108c 11908{
8a2c437b 11909 struct breakpoint *b, *prev;
d6e956e5
VP
11910 VEC(breakpoint_p) *found = 0;
11911 int ix;
c906108c
SS
11912 int default_match;
11913 struct symtabs_and_lines sals;
11914 struct symtab_and_line sal;
c906108c 11915 int i;
8a2c437b 11916 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11917
11918 if (arg)
11919 {
39cf75f7
DE
11920 sals = decode_line_with_current_source (arg,
11921 (DECODE_LINE_FUNFIRSTLINE
11922 | DECODE_LINE_LIST_MODE));
cf4ded82 11923 make_cleanup (xfree, sals.sals);
c906108c
SS
11924 default_match = 0;
11925 }
11926 else
11927 {
8d749320 11928 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11929 make_cleanup (xfree, sals.sals);
4a64f543 11930 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11931
11932 /* Set sal's line, symtab, pc, and pspace to the values
11933 corresponding to the last call to print_frame_info. If the
11934 codepoint is not valid, this will set all the fields to 0. */
11935 get_last_displayed_sal (&sal);
c906108c 11936 if (sal.symtab == 0)
8a3fe4f8 11937 error (_("No source file specified."));
c906108c
SS
11938
11939 sals.sals[0] = sal;
11940 sals.nelts = 1;
11941
11942 default_match = 1;
11943 }
11944
4a64f543
MS
11945 /* We don't call resolve_sal_pc here. That's not as bad as it
11946 seems, because all existing breakpoints typically have both
11947 file/line and pc set. So, if clear is given file/line, we can
11948 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11949
11950 We only support clearing given the address explicitly
11951 present in breakpoint table. Say, we've set breakpoint
4a64f543 11952 at file:line. There were several PC values for that file:line,
ed0616c6 11953 due to optimization, all in one block.
4a64f543
MS
11954
11955 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11956 PC corresponding to the same file:line, the breakpoint won't
11957 be cleared. We probably can still clear the breakpoint, but
11958 since the other PC value is never presented to user, user
11959 can only find it by guessing, and it does not seem important
11960 to support that. */
11961
4a64f543
MS
11962 /* For each line spec given, delete bps which correspond to it. Do
11963 it in two passes, solely to preserve the current behavior that
11964 from_tty is forced true if we delete more than one
11965 breakpoint. */
c906108c 11966
80f8a6eb 11967 found = NULL;
8a2c437b 11968 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11969 for (i = 0; i < sals.nelts; i++)
11970 {
05cba821
JK
11971 const char *sal_fullname;
11972
c906108c 11973 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11974 If line given (pc == 0), clear all bpts on specified line.
11975 If defaulting, clear all bpts on default line
c906108c 11976 or at default pc.
c5aa993b
JM
11977
11978 defaulting sal.pc != 0 tests to do
11979
11980 0 1 pc
11981 1 1 pc _and_ line
11982 0 0 line
11983 1 0 <can't happen> */
c906108c
SS
11984
11985 sal = sals.sals[i];
05cba821
JK
11986 sal_fullname = (sal.symtab == NULL
11987 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11988
4a64f543 11989 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11990 ALL_BREAKPOINTS (b)
c5aa993b 11991 {
0d381245 11992 int match = 0;
4a64f543 11993 /* Are we going to delete b? */
cc60f2e3 11994 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11995 {
11996 struct bp_location *loc = b->loc;
11997 for (; loc; loc = loc->next)
11998 {
f8eba3c6
TT
11999 /* If the user specified file:line, don't allow a PC
12000 match. This matches historical gdb behavior. */
12001 int pc_match = (!sal.explicit_line
12002 && sal.pc
12003 && (loc->pspace == sal.pspace)
12004 && (loc->address == sal.pc)
12005 && (!section_is_overlay (loc->section)
12006 || loc->section == sal.section));
4aac40c8
TT
12007 int line_match = 0;
12008
12009 if ((default_match || sal.explicit_line)
2f202fde 12010 && loc->symtab != NULL
05cba821 12011 && sal_fullname != NULL
4aac40c8 12012 && sal.pspace == loc->pspace
05cba821
JK
12013 && loc->line_number == sal.line
12014 && filename_cmp (symtab_to_fullname (loc->symtab),
12015 sal_fullname) == 0)
12016 line_match = 1;
4aac40c8 12017
0d381245
VP
12018 if (pc_match || line_match)
12019 {
12020 match = 1;
12021 break;
12022 }
12023 }
12024 }
12025
12026 if (match)
d6e956e5 12027 VEC_safe_push(breakpoint_p, found, b);
c906108c 12028 }
80f8a6eb 12029 }
8a2c437b 12030
80f8a6eb 12031 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12032 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12033 {
12034 if (arg)
8a3fe4f8 12035 error (_("No breakpoint at %s."), arg);
80f8a6eb 12036 else
8a3fe4f8 12037 error (_("No breakpoint at this line."));
80f8a6eb 12038 }
c906108c 12039
8a2c437b
TT
12040 /* Remove duplicates from the vec. */
12041 qsort (VEC_address (breakpoint_p, found),
12042 VEC_length (breakpoint_p, found),
12043 sizeof (breakpoint_p),
12044 compare_breakpoints);
12045 prev = VEC_index (breakpoint_p, found, 0);
12046 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12047 {
12048 if (b == prev)
12049 {
12050 VEC_ordered_remove (breakpoint_p, found, ix);
12051 --ix;
12052 }
12053 }
12054
d6e956e5 12055 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12056 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12057 if (from_tty)
a3f17187 12058 {
d6e956e5 12059 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12060 printf_unfiltered (_("Deleted breakpoint "));
12061 else
12062 printf_unfiltered (_("Deleted breakpoints "));
12063 }
d6e956e5
VP
12064
12065 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12066 {
c5aa993b 12067 if (from_tty)
d6e956e5
VP
12068 printf_unfiltered ("%d ", b->number);
12069 delete_breakpoint (b);
c906108c 12070 }
80f8a6eb
MS
12071 if (from_tty)
12072 putchar_unfiltered ('\n');
8a2c437b
TT
12073
12074 do_cleanups (cleanups);
c906108c
SS
12075}
12076\f
12077/* Delete breakpoint in BS if they are `delete' breakpoints and
12078 all breakpoints that are marked for deletion, whether hit or not.
12079 This is called after any breakpoint is hit, or after errors. */
12080
12081void
fba45db2 12082breakpoint_auto_delete (bpstat bs)
c906108c 12083{
35df4500 12084 struct breakpoint *b, *b_tmp;
c906108c
SS
12085
12086 for (; bs; bs = bs->next)
f431efe5
PA
12087 if (bs->breakpoint_at
12088 && bs->breakpoint_at->disposition == disp_del
c906108c 12089 && bs->stop)
f431efe5 12090 delete_breakpoint (bs->breakpoint_at);
c906108c 12091
35df4500 12092 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12093 {
b5de0fa7 12094 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12095 delete_breakpoint (b);
12096 }
c906108c
SS
12097}
12098
4a64f543
MS
12099/* A comparison function for bp_location AP and BP being interfaced to
12100 qsort. Sort elements primarily by their ADDRESS (no matter what
12101 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12102 secondarily by ordering first permanent elements and
4a64f543 12103 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12104 qsort being an unstable algorithm. */
876fa593
JK
12105
12106static int
f5336ca5 12107bp_locations_compare (const void *ap, const void *bp)
876fa593 12108{
9a3c8263
SM
12109 const struct bp_location *a = *(const struct bp_location **) ap;
12110 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12111
12112 if (a->address != b->address)
12113 return (a->address > b->address) - (a->address < b->address);
12114
dea2aa5f
LM
12115 /* Sort locations at the same address by their pspace number, keeping
12116 locations of the same inferior (in a multi-inferior environment)
12117 grouped. */
12118
12119 if (a->pspace->num != b->pspace->num)
12120 return ((a->pspace->num > b->pspace->num)
12121 - (a->pspace->num < b->pspace->num));
12122
876fa593 12123 /* Sort permanent breakpoints first. */
1a853c52
PA
12124 if (a->permanent != b->permanent)
12125 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12126
c56a97f9
JK
12127 /* Make the internal GDB representation stable across GDB runs
12128 where A and B memory inside GDB can differ. Breakpoint locations of
12129 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12130
12131 if (a->owner->number != b->owner->number)
c56a97f9
JK
12132 return ((a->owner->number > b->owner->number)
12133 - (a->owner->number < b->owner->number));
876fa593
JK
12134
12135 return (a > b) - (a < b);
12136}
12137
f5336ca5
PA
12138/* Set bp_locations_placed_address_before_address_max and
12139 bp_locations_shadow_len_after_address_max according to the current
12140 content of the bp_locations array. */
f7545552
TT
12141
12142static void
f5336ca5 12143bp_locations_target_extensions_update (void)
f7545552 12144{
876fa593
JK
12145 struct bp_location *bl, **blp_tmp;
12146
f5336ca5
PA
12147 bp_locations_placed_address_before_address_max = 0;
12148 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12149
12150 ALL_BP_LOCATIONS (bl, blp_tmp)
12151 {
12152 CORE_ADDR start, end, addr;
12153
12154 if (!bp_location_has_shadow (bl))
12155 continue;
12156
12157 start = bl->target_info.placed_address;
12158 end = start + bl->target_info.shadow_len;
12159
12160 gdb_assert (bl->address >= start);
12161 addr = bl->address - start;
f5336ca5
PA
12162 if (addr > bp_locations_placed_address_before_address_max)
12163 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12164
12165 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12166
12167 gdb_assert (bl->address < end);
12168 addr = end - bl->address;
f5336ca5
PA
12169 if (addr > bp_locations_shadow_len_after_address_max)
12170 bp_locations_shadow_len_after_address_max = addr;
876fa593 12171 }
f7545552
TT
12172}
12173
1e4d1764
YQ
12174/* Download tracepoint locations if they haven't been. */
12175
12176static void
12177download_tracepoint_locations (void)
12178{
7ed2c994 12179 struct breakpoint *b;
1e4d1764 12180 struct cleanup *old_chain;
dd2e65cc 12181 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12182
12183 old_chain = save_current_space_and_thread ();
12184
7ed2c994 12185 ALL_TRACEPOINTS (b)
1e4d1764 12186 {
7ed2c994 12187 struct bp_location *bl;
1e4d1764 12188 struct tracepoint *t;
f2a8bc8a 12189 int bp_location_downloaded = 0;
1e4d1764 12190
7ed2c994 12191 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12192 ? !may_insert_fast_tracepoints
12193 : !may_insert_tracepoints))
12194 continue;
12195
dd2e65cc
YQ
12196 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12197 {
12198 if (target_can_download_tracepoint ())
12199 can_download_tracepoint = TRIBOOL_TRUE;
12200 else
12201 can_download_tracepoint = TRIBOOL_FALSE;
12202 }
12203
12204 if (can_download_tracepoint == TRIBOOL_FALSE)
12205 break;
12206
7ed2c994
YQ
12207 for (bl = b->loc; bl; bl = bl->next)
12208 {
12209 /* In tracepoint, locations are _never_ duplicated, so
12210 should_be_inserted is equivalent to
12211 unduplicated_should_be_inserted. */
12212 if (!should_be_inserted (bl) || bl->inserted)
12213 continue;
1e4d1764 12214
7ed2c994 12215 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12216
7ed2c994 12217 target_download_tracepoint (bl);
1e4d1764 12218
7ed2c994 12219 bl->inserted = 1;
f2a8bc8a 12220 bp_location_downloaded = 1;
7ed2c994
YQ
12221 }
12222 t = (struct tracepoint *) b;
12223 t->number_on_target = b->number;
f2a8bc8a
YQ
12224 if (bp_location_downloaded)
12225 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12226 }
12227
12228 do_cleanups (old_chain);
12229}
12230
934709f0
PW
12231/* Swap the insertion/duplication state between two locations. */
12232
12233static void
12234swap_insertion (struct bp_location *left, struct bp_location *right)
12235{
12236 const int left_inserted = left->inserted;
12237 const int left_duplicate = left->duplicate;
b775012e 12238 const int left_needs_update = left->needs_update;
934709f0
PW
12239 const struct bp_target_info left_target_info = left->target_info;
12240
1e4d1764
YQ
12241 /* Locations of tracepoints can never be duplicated. */
12242 if (is_tracepoint (left->owner))
12243 gdb_assert (!left->duplicate);
12244 if (is_tracepoint (right->owner))
12245 gdb_assert (!right->duplicate);
12246
934709f0
PW
12247 left->inserted = right->inserted;
12248 left->duplicate = right->duplicate;
b775012e 12249 left->needs_update = right->needs_update;
934709f0
PW
12250 left->target_info = right->target_info;
12251 right->inserted = left_inserted;
12252 right->duplicate = left_duplicate;
b775012e 12253 right->needs_update = left_needs_update;
934709f0
PW
12254 right->target_info = left_target_info;
12255}
12256
b775012e
LM
12257/* Force the re-insertion of the locations at ADDRESS. This is called
12258 once a new/deleted/modified duplicate location is found and we are evaluating
12259 conditions on the target's side. Such conditions need to be updated on
12260 the target. */
12261
12262static void
12263force_breakpoint_reinsertion (struct bp_location *bl)
12264{
12265 struct bp_location **locp = NULL, **loc2p;
12266 struct bp_location *loc;
12267 CORE_ADDR address = 0;
12268 int pspace_num;
12269
12270 address = bl->address;
12271 pspace_num = bl->pspace->num;
12272
12273 /* This is only meaningful if the target is
12274 evaluating conditions and if the user has
12275 opted for condition evaluation on the target's
12276 side. */
12277 if (gdb_evaluates_breakpoint_condition_p ()
12278 || !target_supports_evaluation_of_breakpoint_conditions ())
12279 return;
12280
12281 /* Flag all breakpoint locations with this address and
12282 the same program space as the location
12283 as "its condition has changed". We need to
12284 update the conditions on the target's side. */
12285 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12286 {
12287 loc = *loc2p;
12288
12289 if (!is_breakpoint (loc->owner)
12290 || pspace_num != loc->pspace->num)
12291 continue;
12292
12293 /* Flag the location appropriately. We use a different state to
12294 let everyone know that we already updated the set of locations
12295 with addr bl->address and program space bl->pspace. This is so
12296 we don't have to keep calling these functions just to mark locations
12297 that have already been marked. */
12298 loc->condition_changed = condition_updated;
12299
12300 /* Free the agent expression bytecode as well. We will compute
12301 it later on. */
833177a4 12302 loc->cond_bytecode.reset ();
b775012e
LM
12303 }
12304}
44702360
PA
12305/* Called whether new breakpoints are created, or existing breakpoints
12306 deleted, to update the global location list and recompute which
12307 locations are duplicate of which.
b775012e 12308
04086b45
PA
12309 The INSERT_MODE flag determines whether locations may not, may, or
12310 shall be inserted now. See 'enum ugll_insert_mode' for more
12311 info. */
b60e7edf 12312
0d381245 12313static void
44702360 12314update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12315{
74960c60 12316 struct breakpoint *b;
876fa593 12317 struct bp_location **locp, *loc;
f7545552 12318 struct cleanup *cleanups;
b775012e
LM
12319 /* Last breakpoint location address that was marked for update. */
12320 CORE_ADDR last_addr = 0;
12321 /* Last breakpoint location program space that was marked for update. */
12322 int last_pspace_num = -1;
f7545552 12323
2d134ed3
PA
12324 /* Used in the duplicates detection below. When iterating over all
12325 bp_locations, points to the first bp_location of a given address.
12326 Breakpoints and watchpoints of different types are never
12327 duplicates of each other. Keep one pointer for each type of
12328 breakpoint/watchpoint, so we only need to loop over all locations
12329 once. */
12330 struct bp_location *bp_loc_first; /* breakpoint */
12331 struct bp_location *wp_loc_first; /* hardware watchpoint */
12332 struct bp_location *awp_loc_first; /* access watchpoint */
12333 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12334
f5336ca5
PA
12335 /* Saved former bp_locations array which we compare against the newly
12336 built bp_locations from the current state of ALL_BREAKPOINTS. */
12337 struct bp_location **old_locations, **old_locp;
12338 unsigned old_locations_count;
876fa593 12339
f5336ca5
PA
12340 old_locations = bp_locations;
12341 old_locations_count = bp_locations_count;
12342 bp_locations = NULL;
12343 bp_locations_count = 0;
12344 cleanups = make_cleanup (xfree, old_locations);
0d381245 12345
74960c60 12346 ALL_BREAKPOINTS (b)
876fa593 12347 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12348 bp_locations_count++;
876fa593 12349
f5336ca5
PA
12350 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12351 locp = bp_locations;
876fa593
JK
12352 ALL_BREAKPOINTS (b)
12353 for (loc = b->loc; loc; loc = loc->next)
12354 *locp++ = loc;
f5336ca5
PA
12355 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12356 bp_locations_compare);
876fa593 12357
f5336ca5 12358 bp_locations_target_extensions_update ();
74960c60 12359
4a64f543
MS
12360 /* Identify bp_location instances that are no longer present in the
12361 new list, and therefore should be freed. Note that it's not
12362 necessary that those locations should be removed from inferior --
12363 if there's another location at the same address (previously
12364 marked as duplicate), we don't need to remove/insert the
12365 location.
876fa593 12366
4a64f543
MS
12367 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12368 and former bp_location array state respectively. */
876fa593 12369
f5336ca5
PA
12370 locp = bp_locations;
12371 for (old_locp = old_locations;
12372 old_locp < old_locations + old_locations_count;
876fa593 12373 old_locp++)
74960c60 12374 {
876fa593 12375 struct bp_location *old_loc = *old_locp;
c7d46a38 12376 struct bp_location **loc2p;
876fa593 12377
e5dd4106 12378 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12379 not, we have to free it. */
c7d46a38 12380 int found_object = 0;
20874c92
VP
12381 /* Tells if the location should remain inserted in the target. */
12382 int keep_in_target = 0;
12383 int removed = 0;
876fa593 12384
4a64f543
MS
12385 /* Skip LOCP entries which will definitely never be needed.
12386 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12387 while (locp < bp_locations + bp_locations_count
c7d46a38 12388 && (*locp)->address < old_loc->address)
876fa593 12389 locp++;
c7d46a38
PA
12390
12391 for (loc2p = locp;
f5336ca5 12392 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12393 && (*loc2p)->address == old_loc->address);
12394 loc2p++)
12395 {
b775012e
LM
12396 /* Check if this is a new/duplicated location or a duplicated
12397 location that had its condition modified. If so, we want to send
12398 its condition to the target if evaluation of conditions is taking
12399 place there. */
12400 if ((*loc2p)->condition_changed == condition_modified
12401 && (last_addr != old_loc->address
12402 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12403 {
b775012e
LM
12404 force_breakpoint_reinsertion (*loc2p);
12405 last_pspace_num = old_loc->pspace->num;
c7d46a38 12406 }
b775012e
LM
12407
12408 if (*loc2p == old_loc)
12409 found_object = 1;
c7d46a38 12410 }
74960c60 12411
b775012e
LM
12412 /* We have already handled this address, update it so that we don't
12413 have to go through updates again. */
12414 last_addr = old_loc->address;
12415
12416 /* Target-side condition evaluation: Handle deleted locations. */
12417 if (!found_object)
12418 force_breakpoint_reinsertion (old_loc);
12419
4a64f543
MS
12420 /* If this location is no longer present, and inserted, look if
12421 there's maybe a new location at the same address. If so,
12422 mark that one inserted, and don't remove this one. This is
12423 needed so that we don't have a time window where a breakpoint
12424 at certain location is not inserted. */
74960c60 12425
876fa593 12426 if (old_loc->inserted)
0d381245 12427 {
4a64f543
MS
12428 /* If the location is inserted now, we might have to remove
12429 it. */
74960c60 12430
876fa593 12431 if (found_object && should_be_inserted (old_loc))
74960c60 12432 {
4a64f543
MS
12433 /* The location is still present in the location list,
12434 and still should be inserted. Don't do anything. */
20874c92 12435 keep_in_target = 1;
74960c60
VP
12436 }
12437 else
12438 {
b775012e
LM
12439 /* This location still exists, but it won't be kept in the
12440 target since it may have been disabled. We proceed to
12441 remove its target-side condition. */
12442
4a64f543
MS
12443 /* The location is either no longer present, or got
12444 disabled. See if there's another location at the
12445 same address, in which case we don't need to remove
12446 this one from the target. */
876fa593 12447
2bdf28a0 12448 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12449 if (breakpoint_address_is_meaningful (old_loc->owner))
12450 {
876fa593 12451 for (loc2p = locp;
f5336ca5 12452 (loc2p < bp_locations + bp_locations_count
c7d46a38 12453 && (*loc2p)->address == old_loc->address);
876fa593
JK
12454 loc2p++)
12455 {
12456 struct bp_location *loc2 = *loc2p;
12457
2d134ed3 12458 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12459 {
85d721b8
PA
12460 /* Read watchpoint locations are switched to
12461 access watchpoints, if the former are not
12462 supported, but the latter are. */
12463 if (is_hardware_watchpoint (old_loc->owner))
12464 {
12465 gdb_assert (is_hardware_watchpoint (loc2->owner));
12466 loc2->watchpoint_type = old_loc->watchpoint_type;
12467 }
12468
934709f0
PW
12469 /* loc2 is a duplicated location. We need to check
12470 if it should be inserted in case it will be
12471 unduplicated. */
12472 if (loc2 != old_loc
12473 && unduplicated_should_be_inserted (loc2))
c7d46a38 12474 {
934709f0 12475 swap_insertion (old_loc, loc2);
c7d46a38
PA
12476 keep_in_target = 1;
12477 break;
12478 }
876fa593
JK
12479 }
12480 }
12481 }
74960c60
VP
12482 }
12483
20874c92
VP
12484 if (!keep_in_target)
12485 {
834c0d03 12486 if (remove_breakpoint (old_loc))
20874c92 12487 {
4a64f543
MS
12488 /* This is just about all we can do. We could keep
12489 this location on the global list, and try to
12490 remove it next time, but there's no particular
12491 reason why we will succeed next time.
20874c92 12492
4a64f543
MS
12493 Note that at this point, old_loc->owner is still
12494 valid, as delete_breakpoint frees the breakpoint
12495 only after calling us. */
3e43a32a
MS
12496 printf_filtered (_("warning: Error removing "
12497 "breakpoint %d\n"),
876fa593 12498 old_loc->owner->number);
20874c92
VP
12499 }
12500 removed = 1;
12501 }
0d381245 12502 }
74960c60
VP
12503
12504 if (!found_object)
1c5cfe86 12505 {
fbea99ea 12506 if (removed && target_is_non_stop_p ()
1cf4d951 12507 && need_moribund_for_location_type (old_loc))
20874c92 12508 {
db82e815
PA
12509 /* This location was removed from the target. In
12510 non-stop mode, a race condition is possible where
12511 we've removed a breakpoint, but stop events for that
12512 breakpoint are already queued and will arrive later.
12513 We apply an heuristic to be able to distinguish such
12514 SIGTRAPs from other random SIGTRAPs: we keep this
12515 breakpoint location for a bit, and will retire it
12516 after we see some number of events. The theory here
12517 is that reporting of events should, "on the average",
12518 be fair, so after a while we'll see events from all
12519 threads that have anything of interest, and no longer
12520 need to keep this breakpoint location around. We
12521 don't hold locations forever so to reduce chances of
12522 mistaking a non-breakpoint SIGTRAP for a breakpoint
12523 SIGTRAP.
12524
12525 The heuristic failing can be disastrous on
12526 decr_pc_after_break targets.
12527
12528 On decr_pc_after_break targets, like e.g., x86-linux,
12529 if we fail to recognize a late breakpoint SIGTRAP,
12530 because events_till_retirement has reached 0 too
12531 soon, we'll fail to do the PC adjustment, and report
12532 a random SIGTRAP to the user. When the user resumes
12533 the inferior, it will most likely immediately crash
2dec564e 12534 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12535 corrupted, because of being resumed e.g., in the
12536 middle of a multi-byte instruction, or skipped a
12537 one-byte instruction. This was actually seen happen
12538 on native x86-linux, and should be less rare on
12539 targets that do not support new thread events, like
12540 remote, due to the heuristic depending on
12541 thread_count.
12542
12543 Mistaking a random SIGTRAP for a breakpoint trap
12544 causes similar symptoms (PC adjustment applied when
12545 it shouldn't), but then again, playing with SIGTRAPs
12546 behind the debugger's back is asking for trouble.
12547
12548 Since hardware watchpoint traps are always
12549 distinguishable from other traps, so we don't need to
12550 apply keep hardware watchpoint moribund locations
12551 around. We simply always ignore hardware watchpoint
12552 traps we can no longer explain. */
12553
876fa593
JK
12554 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12555 old_loc->owner = NULL;
20874c92 12556
876fa593 12557 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12558 }
12559 else
f431efe5
PA
12560 {
12561 old_loc->owner = NULL;
12562 decref_bp_location (&old_loc);
12563 }
20874c92 12564 }
74960c60 12565 }
1c5cfe86 12566
348d480f
PA
12567 /* Rescan breakpoints at the same address and section, marking the
12568 first one as "first" and any others as "duplicates". This is so
12569 that the bpt instruction is only inserted once. If we have a
12570 permanent breakpoint at the same place as BPT, make that one the
12571 official one, and the rest as duplicates. Permanent breakpoints
12572 are sorted first for the same address.
12573
12574 Do the same for hardware watchpoints, but also considering the
12575 watchpoint's type (regular/access/read) and length. */
12576
12577 bp_loc_first = NULL;
12578 wp_loc_first = NULL;
12579 awp_loc_first = NULL;
12580 rwp_loc_first = NULL;
12581 ALL_BP_LOCATIONS (loc, locp)
12582 {
12583 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12584 non-NULL. */
348d480f 12585 struct bp_location **loc_first_p;
d3fbdd86 12586 b = loc->owner;
348d480f 12587
6f380991 12588 if (!unduplicated_should_be_inserted (loc)
348d480f 12589 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12590 /* Don't detect duplicate for tracepoint locations because they are
12591 never duplicated. See the comments in field `duplicate' of
12592 `struct bp_location'. */
348d480f 12593 || is_tracepoint (b))
b775012e
LM
12594 {
12595 /* Clear the condition modification flag. */
12596 loc->condition_changed = condition_unchanged;
12597 continue;
12598 }
348d480f 12599
348d480f
PA
12600 if (b->type == bp_hardware_watchpoint)
12601 loc_first_p = &wp_loc_first;
12602 else if (b->type == bp_read_watchpoint)
12603 loc_first_p = &rwp_loc_first;
12604 else if (b->type == bp_access_watchpoint)
12605 loc_first_p = &awp_loc_first;
12606 else
12607 loc_first_p = &bp_loc_first;
12608
12609 if (*loc_first_p == NULL
12610 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12611 || !breakpoint_locations_match (loc, *loc_first_p))
12612 {
12613 *loc_first_p = loc;
12614 loc->duplicate = 0;
b775012e
LM
12615
12616 if (is_breakpoint (loc->owner) && loc->condition_changed)
12617 {
12618 loc->needs_update = 1;
12619 /* Clear the condition modification flag. */
12620 loc->condition_changed = condition_unchanged;
12621 }
348d480f
PA
12622 continue;
12623 }
12624
934709f0
PW
12625
12626 /* This and the above ensure the invariant that the first location
12627 is not duplicated, and is the inserted one.
12628 All following are marked as duplicated, and are not inserted. */
12629 if (loc->inserted)
12630 swap_insertion (loc, *loc_first_p);
348d480f
PA
12631 loc->duplicate = 1;
12632
b775012e
LM
12633 /* Clear the condition modification flag. */
12634 loc->condition_changed = condition_unchanged;
348d480f
PA
12635 }
12636
a25a5a45 12637 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12638 {
04086b45 12639 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12640 insert_breakpoint_locations ();
12641 else
12642 {
44702360
PA
12643 /* Even though the caller told us to not insert new
12644 locations, we may still need to update conditions on the
12645 target's side of breakpoints that were already inserted
12646 if the target is evaluating breakpoint conditions. We
b775012e
LM
12647 only update conditions for locations that are marked
12648 "needs_update". */
12649 update_inserted_breakpoint_locations ();
12650 }
12651 }
348d480f 12652
04086b45 12653 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12654 download_tracepoint_locations ();
12655
348d480f
PA
12656 do_cleanups (cleanups);
12657}
12658
12659void
12660breakpoint_retire_moribund (void)
12661{
12662 struct bp_location *loc;
12663 int ix;
12664
12665 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12666 if (--(loc->events_till_retirement) == 0)
12667 {
12668 decref_bp_location (&loc);
12669 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12670 --ix;
12671 }
12672}
12673
12674static void
44702360 12675update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12676{
348d480f 12677
492d29ea
PA
12678 TRY
12679 {
12680 update_global_location_list (insert_mode);
12681 }
12682 CATCH (e, RETURN_MASK_ERROR)
12683 {
12684 }
12685 END_CATCH
348d480f
PA
12686}
12687
12688/* Clear BKP from a BPS. */
12689
12690static void
12691bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12692{
12693 bpstat bs;
12694
12695 for (bs = bps; bs; bs = bs->next)
12696 if (bs->breakpoint_at == bpt)
12697 {
12698 bs->breakpoint_at = NULL;
12699 bs->old_val = NULL;
12700 /* bs->commands will be freed later. */
12701 }
12702}
12703
12704/* Callback for iterate_over_threads. */
12705static int
12706bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12707{
9a3c8263 12708 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12709
12710 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12711 return 0;
12712}
12713
12714/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12715 callbacks. */
12716
12717static void
12718say_where (struct breakpoint *b)
12719{
12720 struct value_print_options opts;
12721
12722 get_user_print_options (&opts);
12723
12724 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12725 single string. */
12726 if (b->loc == NULL)
12727 {
f00aae0f
KS
12728 /* For pending locations, the output differs slightly based
12729 on b->extra_string. If this is non-NULL, it contains either
12730 a condition or dprintf arguments. */
12731 if (b->extra_string == NULL)
12732 {
12733 printf_filtered (_(" (%s) pending."),
d28cd78a 12734 event_location_to_string (b->location.get ()));
f00aae0f
KS
12735 }
12736 else if (b->type == bp_dprintf)
12737 {
12738 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12739 event_location_to_string (b->location.get ()),
f00aae0f
KS
12740 b->extra_string);
12741 }
12742 else
12743 {
12744 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12745 event_location_to_string (b->location.get ()),
f00aae0f
KS
12746 b->extra_string);
12747 }
348d480f
PA
12748 }
12749 else
12750 {
2f202fde 12751 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12752 {
12753 printf_filtered (" at ");
12754 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12755 gdb_stdout);
12756 }
2f202fde 12757 if (b->loc->symtab != NULL)
f8eba3c6
TT
12758 {
12759 /* If there is a single location, we can print the location
12760 more nicely. */
12761 if (b->loc->next == NULL)
12762 printf_filtered (": file %s, line %d.",
05cba821
JK
12763 symtab_to_filename_for_display (b->loc->symtab),
12764 b->loc->line_number);
f8eba3c6
TT
12765 else
12766 /* This is not ideal, but each location may have a
12767 different file name, and this at least reflects the
12768 real situation somewhat. */
f00aae0f 12769 printf_filtered (": %s.",
d28cd78a 12770 event_location_to_string (b->location.get ()));
f8eba3c6 12771 }
348d480f
PA
12772
12773 if (b->loc->next)
12774 {
12775 struct bp_location *loc = b->loc;
12776 int n = 0;
12777 for (; loc; loc = loc->next)
12778 ++n;
12779 printf_filtered (" (%d locations)", n);
12780 }
12781 }
12782}
12783
348d480f
PA
12784/* Default bp_location_ops methods. */
12785
12786static void
12787bp_location_dtor (struct bp_location *self)
12788{
348d480f
PA
12789 xfree (self->function_name);
12790}
12791
12792static const struct bp_location_ops bp_location_ops =
12793{
12794 bp_location_dtor
12795};
12796
2060206e
PA
12797/* Default breakpoint_ops methods all breakpoint_ops ultimately
12798 inherit from. */
348d480f 12799
2060206e
PA
12800static void
12801base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12802{
12803 decref_counted_command_line (&self->commands);
12804 xfree (self->cond_string);
fb81d016 12805 xfree (self->extra_string);
f8eba3c6 12806 xfree (self->filter);
348d480f
PA
12807}
12808
2060206e
PA
12809static struct bp_location *
12810base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12811{
5625a286 12812 return new bp_location (&bp_location_ops, self);
348d480f
PA
12813}
12814
2060206e
PA
12815static void
12816base_breakpoint_re_set (struct breakpoint *b)
12817{
12818 /* Nothing to re-set. */
12819}
12820
12821#define internal_error_pure_virtual_called() \
12822 gdb_assert_not_reached ("pure virtual function called")
12823
12824static int
12825base_breakpoint_insert_location (struct bp_location *bl)
12826{
12827 internal_error_pure_virtual_called ();
12828}
12829
12830static int
73971819
PA
12831base_breakpoint_remove_location (struct bp_location *bl,
12832 enum remove_bp_reason reason)
2060206e
PA
12833{
12834 internal_error_pure_virtual_called ();
12835}
12836
12837static int
12838base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12839 struct address_space *aspace,
09ac7c10
TT
12840 CORE_ADDR bp_addr,
12841 const struct target_waitstatus *ws)
2060206e
PA
12842{
12843 internal_error_pure_virtual_called ();
12844}
12845
12846static void
12847base_breakpoint_check_status (bpstat bs)
12848{
12849 /* Always stop. */
12850}
12851
12852/* A "works_in_software_mode" breakpoint_ops method that just internal
12853 errors. */
12854
12855static int
12856base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12857{
12858 internal_error_pure_virtual_called ();
12859}
12860
12861/* A "resources_needed" breakpoint_ops method that just internal
12862 errors. */
12863
12864static int
12865base_breakpoint_resources_needed (const struct bp_location *bl)
12866{
12867 internal_error_pure_virtual_called ();
12868}
12869
12870static enum print_stop_action
12871base_breakpoint_print_it (bpstat bs)
12872{
12873 internal_error_pure_virtual_called ();
12874}
12875
12876static void
12877base_breakpoint_print_one_detail (const struct breakpoint *self,
12878 struct ui_out *uiout)
12879{
12880 /* nothing */
12881}
12882
12883static void
12884base_breakpoint_print_mention (struct breakpoint *b)
12885{
12886 internal_error_pure_virtual_called ();
12887}
12888
12889static void
12890base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12891{
12892 internal_error_pure_virtual_called ();
12893}
12894
983af33b 12895static void
f00aae0f
KS
12896base_breakpoint_create_sals_from_location
12897 (const struct event_location *location,
12898 struct linespec_result *canonical,
12899 enum bptype type_wanted)
983af33b
SDJ
12900{
12901 internal_error_pure_virtual_called ();
12902}
12903
12904static void
12905base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12906 struct linespec_result *c,
983af33b 12907 char *cond_string,
e7e0cddf 12908 char *extra_string,
983af33b
SDJ
12909 enum bptype type_wanted,
12910 enum bpdisp disposition,
12911 int thread,
12912 int task, int ignore_count,
12913 const struct breakpoint_ops *o,
12914 int from_tty, int enabled,
44f238bb 12915 int internal, unsigned flags)
983af33b
SDJ
12916{
12917 internal_error_pure_virtual_called ();
12918}
12919
12920static void
f00aae0f
KS
12921base_breakpoint_decode_location (struct breakpoint *b,
12922 const struct event_location *location,
c2f4122d 12923 struct program_space *search_pspace,
983af33b
SDJ
12924 struct symtabs_and_lines *sals)
12925{
12926 internal_error_pure_virtual_called ();
12927}
12928
ab04a2af
TT
12929/* The default 'explains_signal' method. */
12930
47591c29 12931static int
427cd150 12932base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12933{
47591c29 12934 return 1;
ab04a2af
TT
12935}
12936
9d6e6e84
HZ
12937/* The default "after_condition_true" method. */
12938
12939static void
12940base_breakpoint_after_condition_true (struct bpstats *bs)
12941{
12942 /* Nothing to do. */
12943}
12944
ab04a2af 12945struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12946{
12947 base_breakpoint_dtor,
12948 base_breakpoint_allocate_location,
12949 base_breakpoint_re_set,
12950 base_breakpoint_insert_location,
12951 base_breakpoint_remove_location,
12952 base_breakpoint_breakpoint_hit,
12953 base_breakpoint_check_status,
12954 base_breakpoint_resources_needed,
12955 base_breakpoint_works_in_software_mode,
12956 base_breakpoint_print_it,
12957 NULL,
12958 base_breakpoint_print_one_detail,
12959 base_breakpoint_print_mention,
983af33b 12960 base_breakpoint_print_recreate,
5f700d83 12961 base_breakpoint_create_sals_from_location,
983af33b 12962 base_breakpoint_create_breakpoints_sal,
5f700d83 12963 base_breakpoint_decode_location,
9d6e6e84
HZ
12964 base_breakpoint_explains_signal,
12965 base_breakpoint_after_condition_true,
2060206e
PA
12966};
12967
12968/* Default breakpoint_ops methods. */
12969
12970static void
348d480f
PA
12971bkpt_re_set (struct breakpoint *b)
12972{
06edf0c0 12973 /* FIXME: is this still reachable? */
9ef9e6a6 12974 if (breakpoint_event_location_empty_p (b))
06edf0c0 12975 {
f00aae0f 12976 /* Anything without a location can't be re-set. */
348d480f 12977 delete_breakpoint (b);
06edf0c0 12978 return;
348d480f 12979 }
06edf0c0
PA
12980
12981 breakpoint_re_set_default (b);
348d480f
PA
12982}
12983
2060206e 12984static int
348d480f
PA
12985bkpt_insert_location (struct bp_location *bl)
12986{
cd6c3b4f
YQ
12987 CORE_ADDR addr = bl->target_info.reqstd_address;
12988
579c6ad9 12989 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12990 bl->target_info.placed_address = addr;
12991
348d480f 12992 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12993 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12994 else
7c16b83e 12995 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12996}
12997
2060206e 12998static int
73971819 12999bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
13000{
13001 if (bl->loc_type == bp_loc_hardware_breakpoint)
13002 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13003 else
73971819 13004 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
13005}
13006
2060206e 13007static int
348d480f 13008bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13009 struct address_space *aspace, CORE_ADDR bp_addr,
13010 const struct target_waitstatus *ws)
348d480f 13011{
09ac7c10 13012 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13013 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13014 return 0;
13015
348d480f
PA
13016 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13017 aspace, bp_addr))
13018 return 0;
13019
13020 if (overlay_debugging /* unmapped overlay section */
13021 && section_is_overlay (bl->section)
13022 && !section_is_mapped (bl->section))
13023 return 0;
13024
13025 return 1;
13026}
13027
cd1608cc
PA
13028static int
13029dprintf_breakpoint_hit (const struct bp_location *bl,
13030 struct address_space *aspace, CORE_ADDR bp_addr,
13031 const struct target_waitstatus *ws)
13032{
13033 if (dprintf_style == dprintf_style_agent
13034 && target_can_run_breakpoint_commands ())
13035 {
13036 /* An agent-style dprintf never causes a stop. If we see a trap
13037 for this address it must be for a breakpoint that happens to
13038 be set at the same address. */
13039 return 0;
13040 }
13041
13042 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13043}
13044
2060206e 13045static int
348d480f
PA
13046bkpt_resources_needed (const struct bp_location *bl)
13047{
13048 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13049
13050 return 1;
13051}
13052
2060206e 13053static enum print_stop_action
348d480f
PA
13054bkpt_print_it (bpstat bs)
13055{
348d480f
PA
13056 struct breakpoint *b;
13057 const struct bp_location *bl;
001c8c33 13058 int bp_temp;
79a45e25 13059 struct ui_out *uiout = current_uiout;
348d480f
PA
13060
13061 gdb_assert (bs->bp_location_at != NULL);
13062
13063 bl = bs->bp_location_at;
13064 b = bs->breakpoint_at;
13065
001c8c33
PA
13066 bp_temp = b->disposition == disp_del;
13067 if (bl->address != bl->requested_address)
13068 breakpoint_adjustment_warning (bl->requested_address,
13069 bl->address,
13070 b->number, 1);
13071 annotate_breakpoint (b->number);
f303dbd6
PA
13072 maybe_print_thread_hit_breakpoint (uiout);
13073
001c8c33 13074 if (bp_temp)
112e8700 13075 uiout->text ("Temporary breakpoint ");
001c8c33 13076 else
112e8700
SM
13077 uiout->text ("Breakpoint ");
13078 if (uiout->is_mi_like_p ())
348d480f 13079 {
112e8700 13080 uiout->field_string ("reason",
001c8c33 13081 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 13082 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 13083 }
112e8700
SM
13084 uiout->field_int ("bkptno", b->number);
13085 uiout->text (", ");
06edf0c0 13086
001c8c33 13087 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13088}
13089
2060206e 13090static void
06edf0c0
PA
13091bkpt_print_mention (struct breakpoint *b)
13092{
112e8700 13093 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
13094 return;
13095
13096 switch (b->type)
13097 {
13098 case bp_breakpoint:
13099 case bp_gnu_ifunc_resolver:
13100 if (b->disposition == disp_del)
13101 printf_filtered (_("Temporary breakpoint"));
13102 else
13103 printf_filtered (_("Breakpoint"));
13104 printf_filtered (_(" %d"), b->number);
13105 if (b->type == bp_gnu_ifunc_resolver)
13106 printf_filtered (_(" at gnu-indirect-function resolver"));
13107 break;
13108 case bp_hardware_breakpoint:
13109 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13110 break;
e7e0cddf
SS
13111 case bp_dprintf:
13112 printf_filtered (_("Dprintf %d"), b->number);
13113 break;
06edf0c0
PA
13114 }
13115
13116 say_where (b);
13117}
13118
2060206e 13119static void
06edf0c0
PA
13120bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13121{
13122 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13123 fprintf_unfiltered (fp, "tbreak");
13124 else if (tp->type == bp_breakpoint)
13125 fprintf_unfiltered (fp, "break");
13126 else if (tp->type == bp_hardware_breakpoint
13127 && tp->disposition == disp_del)
13128 fprintf_unfiltered (fp, "thbreak");
13129 else if (tp->type == bp_hardware_breakpoint)
13130 fprintf_unfiltered (fp, "hbreak");
13131 else
13132 internal_error (__FILE__, __LINE__,
13133 _("unhandled breakpoint type %d"), (int) tp->type);
13134
f00aae0f 13135 fprintf_unfiltered (fp, " %s",
d28cd78a 13136 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13137
13138 /* Print out extra_string if this breakpoint is pending. It might
13139 contain, for example, conditions that were set by the user. */
13140 if (tp->loc == NULL && tp->extra_string != NULL)
13141 fprintf_unfiltered (fp, " %s", tp->extra_string);
13142
dd11a36c 13143 print_recreate_thread (tp, fp);
06edf0c0
PA
13144}
13145
983af33b 13146static void
f00aae0f
KS
13147bkpt_create_sals_from_location (const struct event_location *location,
13148 struct linespec_result *canonical,
13149 enum bptype type_wanted)
983af33b 13150{
f00aae0f 13151 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13152}
13153
13154static void
13155bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13156 struct linespec_result *canonical,
983af33b 13157 char *cond_string,
e7e0cddf 13158 char *extra_string,
983af33b
SDJ
13159 enum bptype type_wanted,
13160 enum bpdisp disposition,
13161 int thread,
13162 int task, int ignore_count,
13163 const struct breakpoint_ops *ops,
13164 int from_tty, int enabled,
44f238bb 13165 int internal, unsigned flags)
983af33b 13166{
023fa29b 13167 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13168 cond_string, extra_string,
13169 type_wanted,
983af33b
SDJ
13170 disposition, thread, task,
13171 ignore_count, ops, from_tty,
44f238bb 13172 enabled, internal, flags);
983af33b
SDJ
13173}
13174
13175static void
f00aae0f
KS
13176bkpt_decode_location (struct breakpoint *b,
13177 const struct event_location *location,
c2f4122d 13178 struct program_space *search_pspace,
983af33b
SDJ
13179 struct symtabs_and_lines *sals)
13180{
c2f4122d 13181 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13182}
13183
06edf0c0
PA
13184/* Virtual table for internal breakpoints. */
13185
13186static void
13187internal_bkpt_re_set (struct breakpoint *b)
13188{
13189 switch (b->type)
13190 {
13191 /* Delete overlay event and longjmp master breakpoints; they
13192 will be reset later by breakpoint_re_set. */
13193 case bp_overlay_event:
13194 case bp_longjmp_master:
13195 case bp_std_terminate_master:
13196 case bp_exception_master:
13197 delete_breakpoint (b);
13198 break;
13199
13200 /* This breakpoint is special, it's set up when the inferior
13201 starts and we really don't want to touch it. */
13202 case bp_shlib_event:
13203
13204 /* Like bp_shlib_event, this breakpoint type is special. Once
13205 it is set up, we do not want to touch it. */
13206 case bp_thread_event:
13207 break;
13208 }
13209}
13210
13211static void
13212internal_bkpt_check_status (bpstat bs)
13213{
a9b3a50f
PA
13214 if (bs->breakpoint_at->type == bp_shlib_event)
13215 {
13216 /* If requested, stop when the dynamic linker notifies GDB of
13217 events. This allows the user to get control and place
13218 breakpoints in initializer routines for dynamically loaded
13219 objects (among other things). */
13220 bs->stop = stop_on_solib_events;
13221 bs->print = stop_on_solib_events;
13222 }
13223 else
13224 bs->stop = 0;
06edf0c0
PA
13225}
13226
13227static enum print_stop_action
13228internal_bkpt_print_it (bpstat bs)
13229{
06edf0c0 13230 struct breakpoint *b;
06edf0c0 13231
06edf0c0
PA
13232 b = bs->breakpoint_at;
13233
06edf0c0
PA
13234 switch (b->type)
13235 {
348d480f
PA
13236 case bp_shlib_event:
13237 /* Did we stop because the user set the stop_on_solib_events
13238 variable? (If so, we report this as a generic, "Stopped due
13239 to shlib event" message.) */
edcc5120 13240 print_solib_event (0);
348d480f
PA
13241 break;
13242
13243 case bp_thread_event:
13244 /* Not sure how we will get here.
13245 GDB should not stop for these breakpoints. */
13246 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13247 break;
13248
13249 case bp_overlay_event:
13250 /* By analogy with the thread event, GDB should not stop for these. */
13251 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13252 break;
13253
13254 case bp_longjmp_master:
13255 /* These should never be enabled. */
13256 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13257 break;
13258
13259 case bp_std_terminate_master:
13260 /* These should never be enabled. */
13261 printf_filtered (_("std::terminate Master Breakpoint: "
13262 "gdb should not stop!\n"));
348d480f
PA
13263 break;
13264
13265 case bp_exception_master:
13266 /* These should never be enabled. */
13267 printf_filtered (_("Exception Master Breakpoint: "
13268 "gdb should not stop!\n"));
06edf0c0
PA
13269 break;
13270 }
13271
001c8c33 13272 return PRINT_NOTHING;
06edf0c0
PA
13273}
13274
13275static void
13276internal_bkpt_print_mention (struct breakpoint *b)
13277{
13278 /* Nothing to mention. These breakpoints are internal. */
13279}
13280
06edf0c0
PA
13281/* Virtual table for momentary breakpoints */
13282
13283static void
13284momentary_bkpt_re_set (struct breakpoint *b)
13285{
13286 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13287 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13288 Otherwise these should have been blown away via the cleanup chain
13289 or by breakpoint_init_inferior when we rerun the executable. */
13290}
13291
13292static void
13293momentary_bkpt_check_status (bpstat bs)
13294{
13295 /* Nothing. The point of these breakpoints is causing a stop. */
13296}
13297
13298static enum print_stop_action
13299momentary_bkpt_print_it (bpstat bs)
13300{
001c8c33 13301 return PRINT_UNKNOWN;
348d480f
PA
13302}
13303
06edf0c0
PA
13304static void
13305momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13306{
06edf0c0 13307 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13308}
13309
e2e4d78b
JK
13310/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13311
13312 It gets cleared already on the removal of the first one of such placed
13313 breakpoints. This is OK as they get all removed altogether. */
13314
13315static void
13316longjmp_bkpt_dtor (struct breakpoint *self)
13317{
5d5658a1 13318 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13319
13320 if (tp)
13321 tp->initiating_frame = null_frame_id;
13322
13323 momentary_breakpoint_ops.dtor (self);
13324}
13325
55aa24fb
SDJ
13326/* Specific methods for probe breakpoints. */
13327
13328static int
13329bkpt_probe_insert_location (struct bp_location *bl)
13330{
13331 int v = bkpt_insert_location (bl);
13332
13333 if (v == 0)
13334 {
13335 /* The insertion was successful, now let's set the probe's semaphore
13336 if needed. */
0ea5cda8
SDJ
13337 if (bl->probe.probe->pops->set_semaphore != NULL)
13338 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13339 bl->probe.objfile,
13340 bl->gdbarch);
55aa24fb
SDJ
13341 }
13342
13343 return v;
13344}
13345
13346static int
73971819
PA
13347bkpt_probe_remove_location (struct bp_location *bl,
13348 enum remove_bp_reason reason)
55aa24fb
SDJ
13349{
13350 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13351 if (bl->probe.probe->pops->clear_semaphore != NULL)
13352 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13353 bl->probe.objfile,
13354 bl->gdbarch);
55aa24fb 13355
73971819 13356 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13357}
13358
13359static void
f00aae0f 13360bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13361 struct linespec_result *canonical,
f00aae0f 13362 enum bptype type_wanted)
55aa24fb
SDJ
13363{
13364 struct linespec_sals lsal;
13365
c2f4122d 13366 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13367 lsal.canonical
13368 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13369 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13370}
13371
13372static void
f00aae0f
KS
13373bkpt_probe_decode_location (struct breakpoint *b,
13374 const struct event_location *location,
c2f4122d 13375 struct program_space *search_pspace,
55aa24fb
SDJ
13376 struct symtabs_and_lines *sals)
13377{
c2f4122d 13378 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13379 if (!sals->sals)
13380 error (_("probe not found"));
13381}
13382
348d480f 13383/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13384
348d480f
PA
13385static void
13386tracepoint_re_set (struct breakpoint *b)
13387{
13388 breakpoint_re_set_default (b);
13389}
876fa593 13390
348d480f
PA
13391static int
13392tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13393 struct address_space *aspace, CORE_ADDR bp_addr,
13394 const struct target_waitstatus *ws)
348d480f
PA
13395{
13396 /* By definition, the inferior does not report stops at
13397 tracepoints. */
13398 return 0;
74960c60
VP
13399}
13400
13401static void
348d480f
PA
13402tracepoint_print_one_detail (const struct breakpoint *self,
13403 struct ui_out *uiout)
74960c60 13404{
d9b3f62e
PA
13405 struct tracepoint *tp = (struct tracepoint *) self;
13406 if (tp->static_trace_marker_id)
348d480f
PA
13407 {
13408 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13409
112e8700
SM
13410 uiout->text ("\tmarker id is ");
13411 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13412 tp->static_trace_marker_id);
112e8700 13413 uiout->text ("\n");
348d480f 13414 }
0d381245
VP
13415}
13416
a474d7c2 13417static void
348d480f 13418tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13419{
112e8700 13420 if (current_uiout->is_mi_like_p ())
348d480f 13421 return;
cc59ec59 13422
348d480f
PA
13423 switch (b->type)
13424 {
13425 case bp_tracepoint:
13426 printf_filtered (_("Tracepoint"));
13427 printf_filtered (_(" %d"), b->number);
13428 break;
13429 case bp_fast_tracepoint:
13430 printf_filtered (_("Fast tracepoint"));
13431 printf_filtered (_(" %d"), b->number);
13432 break;
13433 case bp_static_tracepoint:
13434 printf_filtered (_("Static tracepoint"));
13435 printf_filtered (_(" %d"), b->number);
13436 break;
13437 default:
13438 internal_error (__FILE__, __LINE__,
13439 _("unhandled tracepoint type %d"), (int) b->type);
13440 }
13441
13442 say_where (b);
a474d7c2
PA
13443}
13444
348d480f 13445static void
d9b3f62e 13446tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13447{
d9b3f62e
PA
13448 struct tracepoint *tp = (struct tracepoint *) self;
13449
13450 if (self->type == bp_fast_tracepoint)
348d480f 13451 fprintf_unfiltered (fp, "ftrace");
c93e8391 13452 else if (self->type == bp_static_tracepoint)
348d480f 13453 fprintf_unfiltered (fp, "strace");
d9b3f62e 13454 else if (self->type == bp_tracepoint)
348d480f
PA
13455 fprintf_unfiltered (fp, "trace");
13456 else
13457 internal_error (__FILE__, __LINE__,
d9b3f62e 13458 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13459
f00aae0f 13460 fprintf_unfiltered (fp, " %s",
d28cd78a 13461 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13462 print_recreate_thread (self, fp);
13463
13464 if (tp->pass_count)
13465 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13466}
13467
983af33b 13468static void
f00aae0f
KS
13469tracepoint_create_sals_from_location (const struct event_location *location,
13470 struct linespec_result *canonical,
13471 enum bptype type_wanted)
983af33b 13472{
f00aae0f 13473 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13474}
13475
13476static void
13477tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13478 struct linespec_result *canonical,
983af33b 13479 char *cond_string,
e7e0cddf 13480 char *extra_string,
983af33b
SDJ
13481 enum bptype type_wanted,
13482 enum bpdisp disposition,
13483 int thread,
13484 int task, int ignore_count,
13485 const struct breakpoint_ops *ops,
13486 int from_tty, int enabled,
44f238bb 13487 int internal, unsigned flags)
983af33b 13488{
023fa29b 13489 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13490 cond_string, extra_string,
13491 type_wanted,
983af33b
SDJ
13492 disposition, thread, task,
13493 ignore_count, ops, from_tty,
44f238bb 13494 enabled, internal, flags);
983af33b
SDJ
13495}
13496
13497static void
f00aae0f
KS
13498tracepoint_decode_location (struct breakpoint *b,
13499 const struct event_location *location,
c2f4122d 13500 struct program_space *search_pspace,
983af33b
SDJ
13501 struct symtabs_and_lines *sals)
13502{
c2f4122d 13503 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13504}
13505
2060206e 13506struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13507
55aa24fb
SDJ
13508/* The breakpoint_ops structure to be use on tracepoints placed in a
13509 static probe. */
13510
13511static void
f00aae0f
KS
13512tracepoint_probe_create_sals_from_location
13513 (const struct event_location *location,
13514 struct linespec_result *canonical,
13515 enum bptype type_wanted)
55aa24fb
SDJ
13516{
13517 /* We use the same method for breakpoint on probes. */
f00aae0f 13518 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13519}
13520
13521static void
f00aae0f
KS
13522tracepoint_probe_decode_location (struct breakpoint *b,
13523 const struct event_location *location,
c2f4122d 13524 struct program_space *search_pspace,
55aa24fb
SDJ
13525 struct symtabs_and_lines *sals)
13526{
13527 /* We use the same method for breakpoint on probes. */
c2f4122d 13528 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13529}
13530
13531static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13532
5c2b4418
HZ
13533/* Dprintf breakpoint_ops methods. */
13534
13535static void
13536dprintf_re_set (struct breakpoint *b)
13537{
13538 breakpoint_re_set_default (b);
13539
f00aae0f
KS
13540 /* extra_string should never be non-NULL for dprintf. */
13541 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13542
13543 /* 1 - connect to target 1, that can run breakpoint commands.
13544 2 - create a dprintf, which resolves fine.
13545 3 - disconnect from target 1
13546 4 - connect to target 2, that can NOT run breakpoint commands.
13547
13548 After steps #3/#4, you'll want the dprintf command list to
13549 be updated, because target 1 and 2 may well return different
13550 answers for target_can_run_breakpoint_commands().
13551 Given absence of finer grained resetting, we get to do
13552 it all the time. */
13553 if (b->extra_string != NULL)
13554 update_dprintf_command_list (b);
13555}
13556
2d9442cc
HZ
13557/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13558
13559static void
13560dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13561{
f00aae0f 13562 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13563 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13564 tp->extra_string);
13565 print_recreate_thread (tp, fp);
13566}
13567
9d6e6e84
HZ
13568/* Implement the "after_condition_true" breakpoint_ops method for
13569 dprintf.
13570
13571 dprintf's are implemented with regular commands in their command
13572 list, but we run the commands here instead of before presenting the
13573 stop to the user, as dprintf's don't actually cause a stop. This
13574 also makes it so that the commands of multiple dprintfs at the same
13575 address are all handled. */
13576
13577static void
13578dprintf_after_condition_true (struct bpstats *bs)
13579{
13580 struct cleanup *old_chain;
13581 struct bpstats tmp_bs = { NULL };
13582 struct bpstats *tmp_bs_p = &tmp_bs;
13583
13584 /* dprintf's never cause a stop. This wasn't set in the
13585 check_status hook instead because that would make the dprintf's
13586 condition not be evaluated. */
13587 bs->stop = 0;
13588
13589 /* Run the command list here. Take ownership of it instead of
13590 copying. We never want these commands to run later in
13591 bpstat_do_actions, if a breakpoint that causes a stop happens to
13592 be set at same address as this dprintf, or even if running the
13593 commands here throws. */
13594 tmp_bs.commands = bs->commands;
13595 bs->commands = NULL;
13596 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13597
13598 bpstat_do_actions_1 (&tmp_bs_p);
13599
13600 /* 'tmp_bs.commands' will usually be NULL by now, but
13601 bpstat_do_actions_1 may return early without processing the whole
13602 list. */
13603 do_cleanups (old_chain);
13604}
13605
983af33b
SDJ
13606/* The breakpoint_ops structure to be used on static tracepoints with
13607 markers (`-m'). */
13608
13609static void
f00aae0f 13610strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13611 struct linespec_result *canonical,
f00aae0f 13612 enum bptype type_wanted)
983af33b
SDJ
13613{
13614 struct linespec_sals lsal;
f00aae0f
KS
13615 const char *arg_start, *arg;
13616 char *str;
13617 struct cleanup *cleanup;
983af33b 13618
f00aae0f
KS
13619 arg = arg_start = get_linespec_location (location);
13620 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13621
f00aae0f
KS
13622 str = savestring (arg_start, arg - arg_start);
13623 cleanup = make_cleanup (xfree, str);
8e9e35b1 13624 canonical->location = new_linespec_location (&str);
f00aae0f 13625 do_cleanups (cleanup);
983af33b 13626
8e9e35b1
TT
13627 lsal.canonical
13628 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13629 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13630}
13631
13632static void
13633strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13634 struct linespec_result *canonical,
983af33b 13635 char *cond_string,
e7e0cddf 13636 char *extra_string,
983af33b
SDJ
13637 enum bptype type_wanted,
13638 enum bpdisp disposition,
13639 int thread,
13640 int task, int ignore_count,
13641 const struct breakpoint_ops *ops,
13642 int from_tty, int enabled,
44f238bb 13643 int internal, unsigned flags)
983af33b
SDJ
13644{
13645 int i;
52d361e1
YQ
13646 struct linespec_sals *lsal = VEC_index (linespec_sals,
13647 canonical->sals, 0);
983af33b
SDJ
13648
13649 /* If the user is creating a static tracepoint by marker id
13650 (strace -m MARKER_ID), then store the sals index, so that
13651 breakpoint_re_set can try to match up which of the newly
13652 found markers corresponds to this one, and, don't try to
13653 expand multiple locations for each sal, given than SALS
13654 already should contain all sals for MARKER_ID. */
13655
13656 for (i = 0; i < lsal->sals.nelts; ++i)
13657 {
13658 struct symtabs_and_lines expanded;
13659 struct tracepoint *tp;
ffc2605c 13660 event_location_up location;
983af33b
SDJ
13661
13662 expanded.nelts = 1;
13663 expanded.sals = &lsal->sals.sals[i];
13664
8e9e35b1 13665 location = copy_event_location (canonical->location.get ());
983af33b 13666
4d01a485 13667 tp = new tracepoint ();
983af33b 13668 init_breakpoint_sal (&tp->base, gdbarch, expanded,
ffc2605c 13669 std::move (location), NULL,
e7e0cddf
SS
13670 cond_string, extra_string,
13671 type_wanted, disposition,
983af33b 13672 thread, task, ignore_count, ops,
44f238bb 13673 from_tty, enabled, internal, flags,
983af33b
SDJ
13674 canonical->special_display);
13675 /* Given that its possible to have multiple markers with
13676 the same string id, if the user is creating a static
13677 tracepoint by marker id ("strace -m MARKER_ID"), then
13678 store the sals index, so that breakpoint_re_set can
13679 try to match up which of the newly found markers
13680 corresponds to this one */
13681 tp->static_trace_marker_id_idx = i;
13682
13683 install_breakpoint (internal, &tp->base, 0);
983af33b
SDJ
13684 }
13685}
13686
13687static void
f00aae0f
KS
13688strace_marker_decode_location (struct breakpoint *b,
13689 const struct event_location *location,
c2f4122d 13690 struct program_space *search_pspace,
983af33b
SDJ
13691 struct symtabs_and_lines *sals)
13692{
13693 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13694 const char *s = get_linespec_location (location);
983af33b 13695
f00aae0f 13696 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13697 if (sals->nelts > tp->static_trace_marker_id_idx)
13698 {
13699 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13700 sals->nelts = 1;
13701 }
13702 else
13703 error (_("marker %s not found"), tp->static_trace_marker_id);
13704}
13705
13706static struct breakpoint_ops strace_marker_breakpoint_ops;
13707
13708static int
13709strace_marker_p (struct breakpoint *b)
13710{
13711 return b->ops == &strace_marker_breakpoint_ops;
13712}
13713
53a5351d 13714/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13715 structures. */
c906108c
SS
13716
13717void
fba45db2 13718delete_breakpoint (struct breakpoint *bpt)
c906108c 13719{
52f0bd74 13720 struct breakpoint *b;
c906108c 13721
8a3fe4f8 13722 gdb_assert (bpt != NULL);
c906108c 13723
4a64f543
MS
13724 /* Has this bp already been deleted? This can happen because
13725 multiple lists can hold pointers to bp's. bpstat lists are
13726 especial culprits.
13727
13728 One example of this happening is a watchpoint's scope bp. When
13729 the scope bp triggers, we notice that the watchpoint is out of
13730 scope, and delete it. We also delete its scope bp. But the
13731 scope bp is marked "auto-deleting", and is already on a bpstat.
13732 That bpstat is then checked for auto-deleting bp's, which are
13733 deleted.
13734
13735 A real solution to this problem might involve reference counts in
13736 bp's, and/or giving them pointers back to their referencing
13737 bpstat's, and teaching delete_breakpoint to only free a bp's
13738 storage when no more references were extent. A cheaper bandaid
13739 was chosen. */
c906108c
SS
13740 if (bpt->type == bp_none)
13741 return;
13742
4a64f543
MS
13743 /* At least avoid this stale reference until the reference counting
13744 of breakpoints gets resolved. */
d0fb5eae 13745 if (bpt->related_breakpoint != bpt)
e5a0a904 13746 {
d0fb5eae 13747 struct breakpoint *related;
3a5c3e22 13748 struct watchpoint *w;
d0fb5eae
JK
13749
13750 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13751 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13752 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13753 w = (struct watchpoint *) bpt;
13754 else
13755 w = NULL;
13756 if (w != NULL)
13757 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13758
13759 /* Unlink bpt from the bpt->related_breakpoint ring. */
13760 for (related = bpt; related->related_breakpoint != bpt;
13761 related = related->related_breakpoint);
13762 related->related_breakpoint = bpt->related_breakpoint;
13763 bpt->related_breakpoint = bpt;
e5a0a904
JK
13764 }
13765
a9634178
TJB
13766 /* watch_command_1 creates a watchpoint but only sets its number if
13767 update_watchpoint succeeds in creating its bp_locations. If there's
13768 a problem in that process, we'll be asked to delete the half-created
13769 watchpoint. In that case, don't announce the deletion. */
13770 if (bpt->number)
13771 observer_notify_breakpoint_deleted (bpt);
c906108c 13772
c906108c
SS
13773 if (breakpoint_chain == bpt)
13774 breakpoint_chain = bpt->next;
13775
c906108c
SS
13776 ALL_BREAKPOINTS (b)
13777 if (b->next == bpt)
c5aa993b
JM
13778 {
13779 b->next = bpt->next;
13780 break;
13781 }
c906108c 13782
f431efe5
PA
13783 /* Be sure no bpstat's are pointing at the breakpoint after it's
13784 been freed. */
13785 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13786 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13787 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13788 commands are associated with the bpstat; if we remove it here,
13789 then the later call to bpstat_do_actions (&stop_bpstat); in
13790 event-top.c won't do anything, and temporary breakpoints with
13791 commands won't work. */
13792
13793 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13794
4a64f543
MS
13795 /* Now that breakpoint is removed from breakpoint list, update the
13796 global location list. This will remove locations that used to
13797 belong to this breakpoint. Do this before freeing the breakpoint
13798 itself, since remove_breakpoint looks at location's owner. It
13799 might be better design to have location completely
13800 self-contained, but it's not the case now. */
44702360 13801 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13802
348d480f 13803 bpt->ops->dtor (bpt);
4a64f543
MS
13804 /* On the chance that someone will soon try again to delete this
13805 same bp, we mark it as deleted before freeing its storage. */
c906108c 13806 bpt->type = bp_none;
4d01a485 13807 delete bpt;
c906108c
SS
13808}
13809
4d6140d9
AC
13810static void
13811do_delete_breakpoint_cleanup (void *b)
13812{
9a3c8263 13813 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13814}
13815
13816struct cleanup *
13817make_cleanup_delete_breakpoint (struct breakpoint *b)
13818{
13819 return make_cleanup (do_delete_breakpoint_cleanup, b);
13820}
13821
51be5b68
PA
13822/* Iterator function to call a user-provided callback function once
13823 for each of B and its related breakpoints. */
13824
13825static void
13826iterate_over_related_breakpoints (struct breakpoint *b,
13827 void (*function) (struct breakpoint *,
13828 void *),
13829 void *data)
13830{
13831 struct breakpoint *related;
13832
13833 related = b;
13834 do
13835 {
13836 struct breakpoint *next;
13837
13838 /* FUNCTION may delete RELATED. */
13839 next = related->related_breakpoint;
13840
13841 if (next == related)
13842 {
13843 /* RELATED is the last ring entry. */
13844 function (related, data);
13845
13846 /* FUNCTION may have deleted it, so we'd never reach back to
13847 B. There's nothing left to do anyway, so just break
13848 out. */
13849 break;
13850 }
13851 else
13852 function (related, data);
13853
13854 related = next;
13855 }
13856 while (related != b);
13857}
95a42b64
TT
13858
13859static void
13860do_delete_breakpoint (struct breakpoint *b, void *ignore)
13861{
13862 delete_breakpoint (b);
13863}
13864
51be5b68
PA
13865/* A callback for map_breakpoint_numbers that calls
13866 delete_breakpoint. */
13867
13868static void
13869do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13870{
13871 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13872}
13873
c906108c 13874void
fba45db2 13875delete_command (char *arg, int from_tty)
c906108c 13876{
35df4500 13877 struct breakpoint *b, *b_tmp;
c906108c 13878
ea9365bb
TT
13879 dont_repeat ();
13880
c906108c
SS
13881 if (arg == 0)
13882 {
13883 int breaks_to_delete = 0;
13884
46c6471b
PA
13885 /* Delete all breakpoints if no argument. Do not delete
13886 internal breakpoints, these have to be deleted with an
13887 explicit breakpoint number argument. */
c5aa993b 13888 ALL_BREAKPOINTS (b)
46c6471b 13889 if (user_breakpoint_p (b))
973d738b
DJ
13890 {
13891 breaks_to_delete = 1;
13892 break;
13893 }
c906108c
SS
13894
13895 /* Ask user only if there are some breakpoints to delete. */
13896 if (!from_tty
e2e0b3e5 13897 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13898 {
35df4500 13899 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13900 if (user_breakpoint_p (b))
c5aa993b 13901 delete_breakpoint (b);
c906108c
SS
13902 }
13903 }
13904 else
51be5b68 13905 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13906}
13907
c2f4122d
PA
13908/* Return true if all locations of B bound to PSPACE are pending. If
13909 PSPACE is NULL, all locations of all program spaces are
13910 considered. */
13911
0d381245 13912static int
c2f4122d 13913all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13914{
c2f4122d
PA
13915 struct bp_location *loc;
13916
13917 for (loc = b->loc; loc != NULL; loc = loc->next)
13918 if ((pspace == NULL
13919 || loc->pspace == pspace)
13920 && !loc->shlib_disabled
8645ff69 13921 && !loc->pspace->executing_startup)
0d381245
VP
13922 return 0;
13923 return 1;
fe3f5fa8
VP
13924}
13925
776592bf
DE
13926/* Subroutine of update_breakpoint_locations to simplify it.
13927 Return non-zero if multiple fns in list LOC have the same name.
13928 Null names are ignored. */
13929
13930static int
13931ambiguous_names_p (struct bp_location *loc)
13932{
13933 struct bp_location *l;
13934 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13935 (int (*) (const void *,
13936 const void *)) streq,
776592bf
DE
13937 NULL, xcalloc, xfree);
13938
13939 for (l = loc; l != NULL; l = l->next)
13940 {
13941 const char **slot;
13942 const char *name = l->function_name;
13943
13944 /* Allow for some names to be NULL, ignore them. */
13945 if (name == NULL)
13946 continue;
13947
13948 slot = (const char **) htab_find_slot (htab, (const void *) name,
13949 INSERT);
4a64f543
MS
13950 /* NOTE: We can assume slot != NULL here because xcalloc never
13951 returns NULL. */
776592bf
DE
13952 if (*slot != NULL)
13953 {
13954 htab_delete (htab);
13955 return 1;
13956 }
13957 *slot = name;
13958 }
13959
13960 htab_delete (htab);
13961 return 0;
13962}
13963
0fb4aa4b
PA
13964/* When symbols change, it probably means the sources changed as well,
13965 and it might mean the static tracepoint markers are no longer at
13966 the same address or line numbers they used to be at last we
13967 checked. Losing your static tracepoints whenever you rebuild is
13968 undesirable. This function tries to resync/rematch gdb static
13969 tracepoints with the markers on the target, for static tracepoints
13970 that have not been set by marker id. Static tracepoint that have
13971 been set by marker id are reset by marker id in breakpoint_re_set.
13972 The heuristic is:
13973
13974 1) For a tracepoint set at a specific address, look for a marker at
13975 the old PC. If one is found there, assume to be the same marker.
13976 If the name / string id of the marker found is different from the
13977 previous known name, assume that means the user renamed the marker
13978 in the sources, and output a warning.
13979
13980 2) For a tracepoint set at a given line number, look for a marker
13981 at the new address of the old line number. If one is found there,
13982 assume to be the same marker. If the name / string id of the
13983 marker found is different from the previous known name, assume that
13984 means the user renamed the marker in the sources, and output a
13985 warning.
13986
13987 3) If a marker is no longer found at the same address or line, it
13988 may mean the marker no longer exists. But it may also just mean
13989 the code changed a bit. Maybe the user added a few lines of code
13990 that made the marker move up or down (in line number terms). Ask
13991 the target for info about the marker with the string id as we knew
13992 it. If found, update line number and address in the matching
13993 static tracepoint. This will get confused if there's more than one
13994 marker with the same ID (possible in UST, although unadvised
13995 precisely because it confuses tools). */
13996
13997static struct symtab_and_line
13998update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13999{
d9b3f62e 14000 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14001 struct static_tracepoint_marker marker;
14002 CORE_ADDR pc;
0fb4aa4b
PA
14003
14004 pc = sal.pc;
14005 if (sal.line)
14006 find_line_pc (sal.symtab, sal.line, &pc);
14007
14008 if (target_static_tracepoint_marker_at (pc, &marker))
14009 {
d9b3f62e 14010 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14011 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14012 b->number,
d9b3f62e 14013 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14014
d9b3f62e
PA
14015 xfree (tp->static_trace_marker_id);
14016 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14017 release_static_tracepoint_marker (&marker);
14018
14019 return sal;
14020 }
14021
14022 /* Old marker wasn't found on target at lineno. Try looking it up
14023 by string ID. */
14024 if (!sal.explicit_pc
14025 && sal.line != 0
14026 && sal.symtab != NULL
d9b3f62e 14027 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14028 {
14029 VEC(static_tracepoint_marker_p) *markers;
14030
14031 markers
d9b3f62e 14032 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14033
14034 if (!VEC_empty(static_tracepoint_marker_p, markers))
14035 {
80e1d417 14036 struct symtab_and_line sal2;
0fb4aa4b 14037 struct symbol *sym;
80e1d417 14038 struct static_tracepoint_marker *tpmarker;
79a45e25 14039 struct ui_out *uiout = current_uiout;
67994074 14040 struct explicit_location explicit_loc;
0fb4aa4b 14041
80e1d417 14042 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14043
d9b3f62e 14044 xfree (tp->static_trace_marker_id);
80e1d417 14045 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14046
14047 warning (_("marker for static tracepoint %d (%s) not "
14048 "found at previous line number"),
d9b3f62e 14049 b->number, tp->static_trace_marker_id);
0fb4aa4b 14050
80e1d417 14051 init_sal (&sal2);
0fb4aa4b 14052
80e1d417 14053 sal2.pc = tpmarker->address;
0fb4aa4b 14054
80e1d417
AS
14055 sal2 = find_pc_line (tpmarker->address, 0);
14056 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 14057 uiout->text ("Now in ");
0fb4aa4b
PA
14058 if (sym)
14059 {
112e8700
SM
14060 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14061 uiout->text (" at ");
0fb4aa4b 14062 }
112e8700 14063 uiout->field_string ("file",
05cba821 14064 symtab_to_filename_for_display (sal2.symtab));
112e8700 14065 uiout->text (":");
0fb4aa4b 14066
112e8700 14067 if (uiout->is_mi_like_p ())
0fb4aa4b 14068 {
0b0865da 14069 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14070
112e8700 14071 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
14072 }
14073
112e8700
SM
14074 uiout->field_int ("line", sal2.line);
14075 uiout->text ("\n");
0fb4aa4b 14076
80e1d417 14077 b->loc->line_number = sal2.line;
2f202fde 14078 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14079
d28cd78a 14080 b->location.reset (NULL);
67994074
KS
14081 initialize_explicit_location (&explicit_loc);
14082 explicit_loc.source_filename
00e52e53 14083 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14084 explicit_loc.line_offset.offset = b->loc->line_number;
14085 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 14086 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14087
14088 /* Might be nice to check if function changed, and warn if
14089 so. */
14090
80e1d417 14091 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14092 }
14093 }
14094 return sal;
14095}
14096
8d3788bd
VP
14097/* Returns 1 iff locations A and B are sufficiently same that
14098 we don't need to report breakpoint as changed. */
14099
14100static int
14101locations_are_equal (struct bp_location *a, struct bp_location *b)
14102{
14103 while (a && b)
14104 {
14105 if (a->address != b->address)
14106 return 0;
14107
14108 if (a->shlib_disabled != b->shlib_disabled)
14109 return 0;
14110
14111 if (a->enabled != b->enabled)
14112 return 0;
14113
14114 a = a->next;
14115 b = b->next;
14116 }
14117
14118 if ((a == NULL) != (b == NULL))
14119 return 0;
14120
14121 return 1;
14122}
14123
c2f4122d
PA
14124/* Split all locations of B that are bound to PSPACE out of B's
14125 location list to a separate list and return that list's head. If
14126 PSPACE is NULL, hoist out all locations of B. */
14127
14128static struct bp_location *
14129hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14130{
14131 struct bp_location head;
14132 struct bp_location *i = b->loc;
14133 struct bp_location **i_link = &b->loc;
14134 struct bp_location *hoisted = &head;
14135
14136 if (pspace == NULL)
14137 {
14138 i = b->loc;
14139 b->loc = NULL;
14140 return i;
14141 }
14142
14143 head.next = NULL;
14144
14145 while (i != NULL)
14146 {
14147 if (i->pspace == pspace)
14148 {
14149 *i_link = i->next;
14150 i->next = NULL;
14151 hoisted->next = i;
14152 hoisted = i;
14153 }
14154 else
14155 i_link = &i->next;
14156 i = *i_link;
14157 }
14158
14159 return head.next;
14160}
14161
14162/* Create new breakpoint locations for B (a hardware or software
14163 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14164 zero, then B is a ranged breakpoint. Only recreates locations for
14165 FILTER_PSPACE. Locations of other program spaces are left
14166 untouched. */
f1310107 14167
0e30163f 14168void
0d381245 14169update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14170 struct program_space *filter_pspace,
f1310107
TJB
14171 struct symtabs_and_lines sals,
14172 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14173{
14174 int i;
c2f4122d 14175 struct bp_location *existing_locations;
0d381245 14176
f8eba3c6
TT
14177 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14178 {
14179 /* Ranged breakpoints have only one start location and one end
14180 location. */
14181 b->enable_state = bp_disabled;
f8eba3c6
TT
14182 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14183 "multiple locations found\n"),
14184 b->number);
14185 return;
14186 }
f1310107 14187
4a64f543
MS
14188 /* If there's no new locations, and all existing locations are
14189 pending, don't do anything. This optimizes the common case where
14190 all locations are in the same shared library, that was unloaded.
14191 We'd like to retain the location, so that when the library is
14192 loaded again, we don't loose the enabled/disabled status of the
14193 individual locations. */
c2f4122d 14194 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14195 return;
14196
c2f4122d 14197 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14198
0d381245 14199 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14200 {
f8eba3c6
TT
14201 struct bp_location *new_loc;
14202
14203 switch_to_program_space_and_thread (sals.sals[i].pspace);
14204
14205 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14206
0d381245
VP
14207 /* Reparse conditions, they might contain references to the
14208 old symtab. */
14209 if (b->cond_string != NULL)
14210 {
bbc13ae3 14211 const char *s;
fe3f5fa8 14212
0d381245 14213 s = b->cond_string;
492d29ea 14214 TRY
0d381245 14215 {
1bb9788d
TT
14216 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14217 block_for_pc (sals.sals[i].pc),
0d381245
VP
14218 0);
14219 }
492d29ea 14220 CATCH (e, RETURN_MASK_ERROR)
0d381245 14221 {
3e43a32a
MS
14222 warning (_("failed to reevaluate condition "
14223 "for breakpoint %d: %s"),
0d381245
VP
14224 b->number, e.message);
14225 new_loc->enabled = 0;
14226 }
492d29ea 14227 END_CATCH
0d381245 14228 }
fe3f5fa8 14229
f1310107
TJB
14230 if (sals_end.nelts)
14231 {
14232 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14233
14234 new_loc->length = end - sals.sals[0].pc + 1;
14235 }
0d381245 14236 }
fe3f5fa8 14237
4a64f543
MS
14238 /* If possible, carry over 'disable' status from existing
14239 breakpoints. */
0d381245
VP
14240 {
14241 struct bp_location *e = existing_locations;
776592bf
DE
14242 /* If there are multiple breakpoints with the same function name,
14243 e.g. for inline functions, comparing function names won't work.
14244 Instead compare pc addresses; this is just a heuristic as things
14245 may have moved, but in practice it gives the correct answer
14246 often enough until a better solution is found. */
14247 int have_ambiguous_names = ambiguous_names_p (b->loc);
14248
0d381245
VP
14249 for (; e; e = e->next)
14250 {
14251 if (!e->enabled && e->function_name)
14252 {
14253 struct bp_location *l = b->loc;
776592bf
DE
14254 if (have_ambiguous_names)
14255 {
14256 for (; l; l = l->next)
f1310107 14257 if (breakpoint_locations_match (e, l))
776592bf
DE
14258 {
14259 l->enabled = 0;
14260 break;
14261 }
14262 }
14263 else
14264 {
14265 for (; l; l = l->next)
14266 if (l->function_name
14267 && strcmp (e->function_name, l->function_name) == 0)
14268 {
14269 l->enabled = 0;
14270 break;
14271 }
14272 }
0d381245
VP
14273 }
14274 }
14275 }
fe3f5fa8 14276
8d3788bd
VP
14277 if (!locations_are_equal (existing_locations, b->loc))
14278 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14279}
14280
f00aae0f 14281/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14282 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14283
14284static struct symtabs_and_lines
f00aae0f 14285location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14286 struct program_space *search_pspace, int *found)
ef23e705 14287{
02d20e4a 14288 struct symtabs_and_lines sals = {0};
492d29ea 14289 struct gdb_exception exception = exception_none;
ef23e705 14290
983af33b 14291 gdb_assert (b->ops != NULL);
ef23e705 14292
492d29ea 14293 TRY
ef23e705 14294 {
c2f4122d 14295 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14296 }
492d29ea 14297 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14298 {
14299 int not_found_and_ok = 0;
492d29ea
PA
14300
14301 exception = e;
14302
ef23e705
TJB
14303 /* For pending breakpoints, it's expected that parsing will
14304 fail until the right shared library is loaded. User has
14305 already told to create pending breakpoints and don't need
14306 extra messages. If breakpoint is in bp_shlib_disabled
14307 state, then user already saw the message about that
14308 breakpoint being disabled, and don't want to see more
14309 errors. */
58438ac1 14310 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14311 && (b->condition_not_parsed
14312 || (b->loc != NULL
14313 && search_pspace != NULL
14314 && b->loc->pspace != search_pspace)
ef23e705 14315 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14316 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14317 || b->enable_state == bp_disabled))
14318 not_found_and_ok = 1;
14319
14320 if (!not_found_and_ok)
14321 {
14322 /* We surely don't want to warn about the same breakpoint
14323 10 times. One solution, implemented here, is disable
14324 the breakpoint on error. Another solution would be to
14325 have separate 'warning emitted' flag. Since this
14326 happens only when a binary has changed, I don't know
14327 which approach is better. */
14328 b->enable_state = bp_disabled;
14329 throw_exception (e);
14330 }
14331 }
492d29ea 14332 END_CATCH
ef23e705 14333
492d29ea 14334 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14335 {
f8eba3c6 14336 int i;
ef23e705 14337
f8eba3c6
TT
14338 for (i = 0; i < sals.nelts; ++i)
14339 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14340 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14341 {
ed1d1739
KS
14342 char *cond_string, *extra_string;
14343 int thread, task;
ef23e705 14344
f00aae0f 14345 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14346 &cond_string, &thread, &task,
14347 &extra_string);
f00aae0f 14348 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14349 if (cond_string)
14350 b->cond_string = cond_string;
14351 b->thread = thread;
14352 b->task = task;
e7e0cddf 14353 if (extra_string)
f00aae0f
KS
14354 {
14355 xfree (b->extra_string);
14356 b->extra_string = extra_string;
14357 }
ef23e705
TJB
14358 b->condition_not_parsed = 0;
14359 }
14360
983af33b 14361 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14362 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14363
58438ac1
TT
14364 *found = 1;
14365 }
14366 else
14367 *found = 0;
ef23e705
TJB
14368
14369 return sals;
14370}
14371
348d480f
PA
14372/* The default re_set method, for typical hardware or software
14373 breakpoints. Reevaluate the breakpoint and recreate its
14374 locations. */
14375
14376static void
28010a5d 14377breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14378{
14379 int found;
f1310107 14380 struct symtabs_and_lines sals, sals_end;
ef23e705 14381 struct symtabs_and_lines expanded = {0};
f1310107 14382 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14383 struct program_space *filter_pspace = current_program_space;
ef23e705 14384
d28cd78a 14385 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14386 if (found)
14387 {
14388 make_cleanup (xfree, sals.sals);
f8eba3c6 14389 expanded = sals;
ef23e705
TJB
14390 }
14391
f00aae0f 14392 if (b->location_range_end != NULL)
f1310107 14393 {
d28cd78a 14394 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14395 filter_pspace, &found);
f1310107
TJB
14396 if (found)
14397 {
14398 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14399 expanded_end = sals_end;
f1310107
TJB
14400 }
14401 }
14402
c2f4122d 14403 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14404}
14405
983af33b
SDJ
14406/* Default method for creating SALs from an address string. It basically
14407 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14408
14409static void
f00aae0f
KS
14410create_sals_from_location_default (const struct event_location *location,
14411 struct linespec_result *canonical,
14412 enum bptype type_wanted)
983af33b 14413{
f00aae0f 14414 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14415}
14416
14417/* Call create_breakpoints_sal for the given arguments. This is the default
14418 function for the `create_breakpoints_sal' method of
14419 breakpoint_ops. */
14420
14421static void
14422create_breakpoints_sal_default (struct gdbarch *gdbarch,
14423 struct linespec_result *canonical,
983af33b 14424 char *cond_string,
e7e0cddf 14425 char *extra_string,
983af33b
SDJ
14426 enum bptype type_wanted,
14427 enum bpdisp disposition,
14428 int thread,
14429 int task, int ignore_count,
14430 const struct breakpoint_ops *ops,
14431 int from_tty, int enabled,
44f238bb 14432 int internal, unsigned flags)
983af33b
SDJ
14433{
14434 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14435 extra_string,
983af33b
SDJ
14436 type_wanted, disposition,
14437 thread, task, ignore_count, ops, from_tty,
44f238bb 14438 enabled, internal, flags);
983af33b
SDJ
14439}
14440
14441/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14442 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14443
14444static void
f00aae0f
KS
14445decode_location_default (struct breakpoint *b,
14446 const struct event_location *location,
c2f4122d 14447 struct program_space *search_pspace,
983af33b
SDJ
14448 struct symtabs_and_lines *sals)
14449{
14450 struct linespec_result canonical;
14451
c2f4122d 14452 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14453 (struct symtab *) NULL, 0,
14454 &canonical, multiple_symbols_all,
14455 b->filter);
14456
14457 /* We should get 0 or 1 resulting SALs. */
14458 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14459
14460 if (VEC_length (linespec_sals, canonical.sals) > 0)
14461 {
14462 struct linespec_sals *lsal;
14463
14464 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14465 *sals = lsal->sals;
14466 /* Arrange it so the destructor does not free the
14467 contents. */
14468 lsal->sals.sals = NULL;
14469 }
983af33b
SDJ
14470}
14471
28010a5d
PA
14472/* Prepare the global context for a re-set of breakpoint B. */
14473
14474static struct cleanup *
14475prepare_re_set_context (struct breakpoint *b)
14476{
28010a5d 14477 input_radix = b->input_radix;
28010a5d
PA
14478 set_language (b->language);
14479
c2f4122d 14480 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14481}
14482
c906108c
SS
14483/* Reset a breakpoint given it's struct breakpoint * BINT.
14484 The value we return ends up being the return value from catch_errors.
14485 Unused in this case. */
14486
14487static int
4efb68b1 14488breakpoint_re_set_one (void *bint)
c906108c 14489{
4a64f543 14490 /* Get past catch_errs. */
53a5351d 14491 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14492 struct cleanup *cleanups;
c906108c 14493
348d480f
PA
14494 cleanups = prepare_re_set_context (b);
14495 b->ops->re_set (b);
14496 do_cleanups (cleanups);
c906108c
SS
14497 return 0;
14498}
14499
c2f4122d
PA
14500/* Re-set breakpoint locations for the current program space.
14501 Locations bound to other program spaces are left untouched. */
14502
c906108c 14503void
69de3c6a 14504breakpoint_re_set (void)
c906108c 14505{
35df4500 14506 struct breakpoint *b, *b_tmp;
c906108c
SS
14507 enum language save_language;
14508 int save_input_radix;
6c95b8df 14509 struct cleanup *old_chain;
c5aa993b 14510
c906108c
SS
14511 save_language = current_language->la_language;
14512 save_input_radix = input_radix;
c2f4122d 14513 old_chain = save_current_space_and_thread ();
6c95b8df 14514
2a7f3dff
PA
14515 /* Note: we must not try to insert locations until after all
14516 breakpoints have been re-set. Otherwise, e.g., when re-setting
14517 breakpoint 1, we'd insert the locations of breakpoint 2, which
14518 hadn't been re-set yet, and thus may have stale locations. */
14519
35df4500 14520 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14521 {
4a64f543 14522 /* Format possible error msg. */
fe3f5fa8 14523 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14524 b->number);
14525 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14526 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14527 do_cleanups (cleanups);
c5aa993b 14528 }
c906108c
SS
14529 set_language (save_language);
14530 input_radix = save_input_radix;
e62c965a 14531
0756c555 14532 jit_breakpoint_re_set ();
4efc6507 14533
6c95b8df
PA
14534 do_cleanups (old_chain);
14535
af02033e
PP
14536 create_overlay_event_breakpoint ();
14537 create_longjmp_master_breakpoint ();
14538 create_std_terminate_master_breakpoint ();
186c406b 14539 create_exception_master_breakpoint ();
2a7f3dff
PA
14540
14541 /* Now we can insert. */
14542 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14543}
14544\f
c906108c
SS
14545/* Reset the thread number of this breakpoint:
14546
14547 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14548 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14549void
fba45db2 14550breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14551{
14552 if (b->thread != -1)
14553 {
39f77062 14554 if (in_thread_list (inferior_ptid))
5d5658a1 14555 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14556
14557 /* We're being called after following a fork. The new fork is
14558 selected as current, and unless this was a vfork will have a
14559 different program space from the original thread. Reset that
14560 as well. */
14561 b->loc->pspace = current_program_space;
c906108c
SS
14562 }
14563}
14564
03ac34d5
MS
14565/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14566 If from_tty is nonzero, it prints a message to that effect,
14567 which ends with a period (no newline). */
14568
c906108c 14569void
fba45db2 14570set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14571{
52f0bd74 14572 struct breakpoint *b;
c906108c
SS
14573
14574 if (count < 0)
14575 count = 0;
14576
14577 ALL_BREAKPOINTS (b)
14578 if (b->number == bptnum)
c5aa993b 14579 {
d77f58be
SS
14580 if (is_tracepoint (b))
14581 {
14582 if (from_tty && count != 0)
14583 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14584 bptnum);
14585 return;
14586 }
14587
c5aa993b 14588 b->ignore_count = count;
221ea385
KS
14589 if (from_tty)
14590 {
14591 if (count == 0)
3e43a32a
MS
14592 printf_filtered (_("Will stop next time "
14593 "breakpoint %d is reached."),
221ea385
KS
14594 bptnum);
14595 else if (count == 1)
a3f17187 14596 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14597 bptnum);
14598 else
3e43a32a
MS
14599 printf_filtered (_("Will ignore next %d "
14600 "crossings of breakpoint %d."),
221ea385
KS
14601 count, bptnum);
14602 }
8d3788bd 14603 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14604 return;
14605 }
c906108c 14606
8a3fe4f8 14607 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14608}
14609
c906108c
SS
14610/* Command to set ignore-count of breakpoint N to COUNT. */
14611
14612static void
fba45db2 14613ignore_command (char *args, int from_tty)
c906108c
SS
14614{
14615 char *p = args;
52f0bd74 14616 int num;
c906108c
SS
14617
14618 if (p == 0)
e2e0b3e5 14619 error_no_arg (_("a breakpoint number"));
c5aa993b 14620
c906108c 14621 num = get_number (&p);
5c44784c 14622 if (num == 0)
8a3fe4f8 14623 error (_("bad breakpoint number: '%s'"), args);
c906108c 14624 if (*p == 0)
8a3fe4f8 14625 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14626
14627 set_ignore_count (num,
14628 longest_to_int (value_as_long (parse_and_eval (p))),
14629 from_tty);
221ea385
KS
14630 if (from_tty)
14631 printf_filtered ("\n");
c906108c
SS
14632}
14633\f
14634/* Call FUNCTION on each of the breakpoints
14635 whose numbers are given in ARGS. */
14636
14637static void
896b6bda
PA
14638map_breakpoint_numbers (const char *args,
14639 void (*function) (struct breakpoint *,
14640 void *),
95a42b64 14641 void *data)
c906108c 14642{
52f0bd74
AC
14643 int num;
14644 struct breakpoint *b, *tmp;
c906108c 14645
b9d61307 14646 if (args == 0 || *args == '\0')
e2e0b3e5 14647 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14648
bfd28288 14649 number_or_range_parser parser (args);
197f0a60 14650
bfd28288 14651 while (!parser.finished ())
c906108c 14652 {
bfd28288
PA
14653 const char *p = parser.cur_tok ();
14654 bool match = false;
197f0a60 14655
bfd28288 14656 num = parser.get_number ();
5c44784c 14657 if (num == 0)
c5aa993b 14658 {
8a3fe4f8 14659 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14660 }
14661 else
14662 {
14663 ALL_BREAKPOINTS_SAFE (b, tmp)
14664 if (b->number == num)
14665 {
bfd28288 14666 match = true;
cdac0397 14667 function (b, data);
11cf8741 14668 break;
5c44784c 14669 }
bfd28288 14670 if (!match)
a3f17187 14671 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14672 }
c906108c
SS
14673 }
14674}
14675
0d381245
VP
14676static struct bp_location *
14677find_location_by_number (char *number)
14678{
14679 char *dot = strchr (number, '.');
14680 char *p1;
14681 int bp_num;
14682 int loc_num;
14683 struct breakpoint *b;
14684 struct bp_location *loc;
14685
14686 *dot = '\0';
14687
14688 p1 = number;
197f0a60 14689 bp_num = get_number (&p1);
0d381245
VP
14690 if (bp_num == 0)
14691 error (_("Bad breakpoint number '%s'"), number);
14692
14693 ALL_BREAKPOINTS (b)
14694 if (b->number == bp_num)
14695 {
14696 break;
14697 }
14698
14699 if (!b || b->number != bp_num)
14700 error (_("Bad breakpoint number '%s'"), number);
14701
14702 p1 = dot+1;
197f0a60 14703 loc_num = get_number (&p1);
0d381245
VP
14704 if (loc_num == 0)
14705 error (_("Bad breakpoint location number '%s'"), number);
14706
14707 --loc_num;
14708 loc = b->loc;
14709 for (;loc_num && loc; --loc_num, loc = loc->next)
14710 ;
14711 if (!loc)
14712 error (_("Bad breakpoint location number '%s'"), dot+1);
14713
14714 return loc;
14715}
14716
14717
1900040c
MS
14718/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14719 If from_tty is nonzero, it prints a message to that effect,
14720 which ends with a period (no newline). */
14721
c906108c 14722void
fba45db2 14723disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14724{
14725 /* Never disable a watchpoint scope breakpoint; we want to
14726 hit them when we leave scope so we can delete both the
14727 watchpoint and its scope breakpoint at that time. */
14728 if (bpt->type == bp_watchpoint_scope)
14729 return;
14730
b5de0fa7 14731 bpt->enable_state = bp_disabled;
c906108c 14732
b775012e
LM
14733 /* Mark breakpoint locations modified. */
14734 mark_breakpoint_modified (bpt);
14735
d248b706
KY
14736 if (target_supports_enable_disable_tracepoint ()
14737 && current_trace_status ()->running && is_tracepoint (bpt))
14738 {
14739 struct bp_location *location;
14740
14741 for (location = bpt->loc; location; location = location->next)
14742 target_disable_tracepoint (location);
14743 }
14744
44702360 14745 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14746
8d3788bd 14747 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14748}
14749
51be5b68
PA
14750/* A callback for iterate_over_related_breakpoints. */
14751
14752static void
14753do_disable_breakpoint (struct breakpoint *b, void *ignore)
14754{
14755 disable_breakpoint (b);
14756}
14757
95a42b64
TT
14758/* A callback for map_breakpoint_numbers that calls
14759 disable_breakpoint. */
14760
14761static void
14762do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14763{
51be5b68 14764 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14765}
14766
c906108c 14767static void
fba45db2 14768disable_command (char *args, int from_tty)
c906108c 14769{
c906108c 14770 if (args == 0)
46c6471b
PA
14771 {
14772 struct breakpoint *bpt;
14773
14774 ALL_BREAKPOINTS (bpt)
14775 if (user_breakpoint_p (bpt))
14776 disable_breakpoint (bpt);
14777 }
9eaabc75 14778 else
0d381245 14779 {
9eaabc75
MW
14780 char *num = extract_arg (&args);
14781
14782 while (num)
d248b706 14783 {
9eaabc75 14784 if (strchr (num, '.'))
b775012e 14785 {
9eaabc75
MW
14786 struct bp_location *loc = find_location_by_number (num);
14787
14788 if (loc)
14789 {
14790 if (loc->enabled)
14791 {
14792 loc->enabled = 0;
14793 mark_breakpoint_location_modified (loc);
14794 }
14795 if (target_supports_enable_disable_tracepoint ()
14796 && current_trace_status ()->running && loc->owner
14797 && is_tracepoint (loc->owner))
14798 target_disable_tracepoint (loc);
14799 }
44702360 14800 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14801 }
9eaabc75
MW
14802 else
14803 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14804 num = extract_arg (&args);
d248b706 14805 }
0d381245 14806 }
c906108c
SS
14807}
14808
14809static void
816338b5
SS
14810enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14811 int count)
c906108c 14812{
afe38095 14813 int target_resources_ok;
c906108c
SS
14814
14815 if (bpt->type == bp_hardware_breakpoint)
14816 {
14817 int i;
c5aa993b 14818 i = hw_breakpoint_used_count ();
53a5351d 14819 target_resources_ok =
d92524f1 14820 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14821 i + 1, 0);
c906108c 14822 if (target_resources_ok == 0)
8a3fe4f8 14823 error (_("No hardware breakpoint support in the target."));
c906108c 14824 else if (target_resources_ok < 0)
8a3fe4f8 14825 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14826 }
14827
cc60f2e3 14828 if (is_watchpoint (bpt))
c906108c 14829 {
d07205c2 14830 /* Initialize it just to avoid a GCC false warning. */
f486487f 14831 enum enable_state orig_enable_state = bp_disabled;
dde02812 14832
492d29ea 14833 TRY
c906108c 14834 {
3a5c3e22
PA
14835 struct watchpoint *w = (struct watchpoint *) bpt;
14836
1e718ff1
TJB
14837 orig_enable_state = bpt->enable_state;
14838 bpt->enable_state = bp_enabled;
3a5c3e22 14839 update_watchpoint (w, 1 /* reparse */);
c906108c 14840 }
492d29ea 14841 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14842 {
1e718ff1 14843 bpt->enable_state = orig_enable_state;
dde02812
ES
14844 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14845 bpt->number);
14846 return;
c5aa993b 14847 }
492d29ea 14848 END_CATCH
c906108c 14849 }
0101ce28 14850
b775012e
LM
14851 bpt->enable_state = bp_enabled;
14852
14853 /* Mark breakpoint locations modified. */
14854 mark_breakpoint_modified (bpt);
14855
d248b706
KY
14856 if (target_supports_enable_disable_tracepoint ()
14857 && current_trace_status ()->running && is_tracepoint (bpt))
14858 {
14859 struct bp_location *location;
14860
14861 for (location = bpt->loc; location; location = location->next)
14862 target_enable_tracepoint (location);
14863 }
14864
b4c291bb 14865 bpt->disposition = disposition;
816338b5 14866 bpt->enable_count = count;
44702360 14867 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14868
8d3788bd 14869 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14870}
14871
fe3f5fa8 14872
c906108c 14873void
fba45db2 14874enable_breakpoint (struct breakpoint *bpt)
c906108c 14875{
816338b5 14876 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14877}
14878
14879static void
14880do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14881{
14882 enable_breakpoint (bpt);
c906108c
SS
14883}
14884
95a42b64
TT
14885/* A callback for map_breakpoint_numbers that calls
14886 enable_breakpoint. */
14887
14888static void
14889do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14890{
51be5b68 14891 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14892}
14893
c906108c
SS
14894/* The enable command enables the specified breakpoints (or all defined
14895 breakpoints) so they once again become (or continue to be) effective
1272ad14 14896 in stopping the inferior. */
c906108c 14897
c906108c 14898static void
fba45db2 14899enable_command (char *args, int from_tty)
c906108c 14900{
c906108c 14901 if (args == 0)
46c6471b
PA
14902 {
14903 struct breakpoint *bpt;
14904
14905 ALL_BREAKPOINTS (bpt)
14906 if (user_breakpoint_p (bpt))
14907 enable_breakpoint (bpt);
14908 }
9eaabc75 14909 else
0d381245 14910 {
9eaabc75
MW
14911 char *num = extract_arg (&args);
14912
14913 while (num)
d248b706 14914 {
9eaabc75 14915 if (strchr (num, '.'))
b775012e 14916 {
9eaabc75
MW
14917 struct bp_location *loc = find_location_by_number (num);
14918
14919 if (loc)
14920 {
14921 if (!loc->enabled)
14922 {
14923 loc->enabled = 1;
14924 mark_breakpoint_location_modified (loc);
14925 }
14926 if (target_supports_enable_disable_tracepoint ()
14927 && current_trace_status ()->running && loc->owner
14928 && is_tracepoint (loc->owner))
14929 target_enable_tracepoint (loc);
14930 }
44702360 14931 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14932 }
9eaabc75
MW
14933 else
14934 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14935 num = extract_arg (&args);
d248b706 14936 }
0d381245 14937 }
c906108c
SS
14938}
14939
816338b5
SS
14940/* This struct packages up disposition data for application to multiple
14941 breakpoints. */
14942
14943struct disp_data
14944{
14945 enum bpdisp disp;
14946 int count;
14947};
14948
c906108c 14949static void
51be5b68
PA
14950do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14951{
816338b5 14952 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14953
816338b5 14954 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14955}
14956
14957static void
14958do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14959{
816338b5 14960 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14961
14962 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14963}
14964
c906108c 14965static void
fba45db2 14966enable_once_command (char *args, int from_tty)
c906108c 14967{
51be5b68 14968 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14969}
14970
816338b5
SS
14971static void
14972do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14973{
14974 struct disp_data disp = { disp_disable, *(int *) countptr };
14975
14976 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14977}
14978
14979static void
14980enable_count_command (char *args, int from_tty)
14981{
b9d61307
SM
14982 int count;
14983
14984 if (args == NULL)
14985 error_no_arg (_("hit count"));
14986
14987 count = get_number (&args);
816338b5
SS
14988
14989 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14990}
14991
c906108c 14992static void
51be5b68 14993do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14994{
816338b5 14995 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14996
14997 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14998}
14999
c906108c 15000static void
fba45db2 15001enable_delete_command (char *args, int from_tty)
c906108c 15002{
51be5b68 15003 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15004}
15005\f
fa8d40ab
JJ
15006static void
15007set_breakpoint_cmd (char *args, int from_tty)
15008{
15009}
15010
15011static void
15012show_breakpoint_cmd (char *args, int from_tty)
15013{
15014}
15015
1f3b5d1b
PP
15016/* Invalidate last known value of any hardware watchpoint if
15017 the memory which that value represents has been written to by
15018 GDB itself. */
15019
15020static void
8de0566d
YQ
15021invalidate_bp_value_on_memory_change (struct inferior *inferior,
15022 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15023 const bfd_byte *data)
15024{
15025 struct breakpoint *bp;
15026
15027 ALL_BREAKPOINTS (bp)
15028 if (bp->enable_state == bp_enabled
3a5c3e22 15029 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15030 {
3a5c3e22 15031 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15032
3a5c3e22
PA
15033 if (wp->val_valid && wp->val)
15034 {
15035 struct bp_location *loc;
15036
15037 for (loc = bp->loc; loc != NULL; loc = loc->next)
15038 if (loc->loc_type == bp_loc_hardware_watchpoint
15039 && loc->address + loc->length > addr
15040 && addr + len > loc->address)
15041 {
15042 value_free (wp->val);
15043 wp->val = NULL;
15044 wp->val_valid = 0;
15045 }
15046 }
1f3b5d1b
PP
15047 }
15048}
15049
8181d85f
DJ
15050/* Create and insert a breakpoint for software single step. */
15051
15052void
6c95b8df 15053insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15054 struct address_space *aspace,
15055 CORE_ADDR next_pc)
8181d85f 15056{
7c16b83e
PA
15057 struct thread_info *tp = inferior_thread ();
15058 struct symtab_and_line sal;
15059 CORE_ADDR pc = next_pc;
8181d85f 15060
34b7e8a6
PA
15061 if (tp->control.single_step_breakpoints == NULL)
15062 {
15063 tp->control.single_step_breakpoints
5d5658a1 15064 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15065 }
8181d85f 15066
7c16b83e
PA
15067 sal = find_pc_line (pc, 0);
15068 sal.pc = pc;
15069 sal.section = find_pc_overlay (pc);
15070 sal.explicit_pc = 1;
34b7e8a6 15071 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15072
7c16b83e 15073 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15074}
15075
93f9a11f
YQ
15076/* Insert single step breakpoints according to the current state. */
15077
15078int
15079insert_single_step_breakpoints (struct gdbarch *gdbarch)
15080{
f5ea389a 15081 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 15082 std::vector<CORE_ADDR> next_pcs;
93f9a11f 15083
f5ea389a 15084 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 15085
a0ff9e1a 15086 if (!next_pcs.empty ())
93f9a11f 15087 {
f5ea389a 15088 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
15089 struct address_space *aspace = get_frame_address_space (frame);
15090
a0ff9e1a 15091 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
15092 insert_single_step_breakpoint (gdbarch, aspace, pc);
15093
93f9a11f
YQ
15094 return 1;
15095 }
15096 else
15097 return 0;
15098}
15099
34b7e8a6 15100/* See breakpoint.h. */
f02253f1
HZ
15101
15102int
7c16b83e
PA
15103breakpoint_has_location_inserted_here (struct breakpoint *bp,
15104 struct address_space *aspace,
15105 CORE_ADDR pc)
1aafd4da 15106{
7c16b83e 15107 struct bp_location *loc;
1aafd4da 15108
7c16b83e
PA
15109 for (loc = bp->loc; loc != NULL; loc = loc->next)
15110 if (loc->inserted
15111 && breakpoint_location_address_match (loc, aspace, pc))
15112 return 1;
1aafd4da 15113
7c16b83e 15114 return 0;
ef370185
JB
15115}
15116
15117/* Check whether a software single-step breakpoint is inserted at
15118 PC. */
15119
15120int
15121single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15122 CORE_ADDR pc)
15123{
34b7e8a6
PA
15124 struct breakpoint *bpt;
15125
15126 ALL_BREAKPOINTS (bpt)
15127 {
15128 if (bpt->type == bp_single_step
15129 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15130 return 1;
15131 }
15132 return 0;
1aafd4da
UW
15133}
15134
1042e4c0
SS
15135/* Tracepoint-specific operations. */
15136
15137/* Set tracepoint count to NUM. */
15138static void
15139set_tracepoint_count (int num)
15140{
15141 tracepoint_count = num;
4fa62494 15142 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15143}
15144
70221824 15145static void
1042e4c0
SS
15146trace_command (char *arg, int from_tty)
15147{
55aa24fb 15148 struct breakpoint_ops *ops;
55aa24fb 15149
ffc2605c
TT
15150 event_location_up location = string_to_event_location (&arg,
15151 current_language);
5b56227b 15152 if (location != NULL
ffc2605c 15153 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15154 ops = &tracepoint_probe_breakpoint_ops;
15155 else
15156 ops = &tracepoint_breakpoint_ops;
15157
558a9d82 15158 create_breakpoint (get_current_arch (),
ffc2605c 15159 location.get (),
f00aae0f 15160 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15161 0 /* tempflag */,
15162 bp_tracepoint /* type_wanted */,
15163 0 /* Ignore count */,
15164 pending_break_support,
15165 ops,
15166 from_tty,
15167 1 /* enabled */,
15168 0 /* internal */, 0);
1042e4c0
SS
15169}
15170
70221824 15171static void
7a697b8d
SS
15172ftrace_command (char *arg, int from_tty)
15173{
ffc2605c
TT
15174 event_location_up location = string_to_event_location (&arg,
15175 current_language);
558a9d82 15176 create_breakpoint (get_current_arch (),
ffc2605c 15177 location.get (),
f00aae0f 15178 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15179 0 /* tempflag */,
15180 bp_fast_tracepoint /* type_wanted */,
15181 0 /* Ignore count */,
15182 pending_break_support,
15183 &tracepoint_breakpoint_ops,
15184 from_tty,
15185 1 /* enabled */,
15186 0 /* internal */, 0);
0fb4aa4b
PA
15187}
15188
15189/* strace command implementation. Creates a static tracepoint. */
15190
70221824 15191static void
0fb4aa4b
PA
15192strace_command (char *arg, int from_tty)
15193{
983af33b 15194 struct breakpoint_ops *ops;
ffc2605c 15195 event_location_up location;
f00aae0f 15196 struct cleanup *back_to;
983af33b
SDJ
15197
15198 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15199 or with a normal static tracepoint. */
61012eef 15200 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15201 {
15202 ops = &strace_marker_breakpoint_ops;
15203 location = new_linespec_location (&arg);
15204 }
983af33b 15205 else
f00aae0f
KS
15206 {
15207 ops = &tracepoint_breakpoint_ops;
15208 location = string_to_event_location (&arg, current_language);
15209 }
983af33b 15210
558a9d82 15211 create_breakpoint (get_current_arch (),
ffc2605c 15212 location.get (),
f00aae0f 15213 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15214 0 /* tempflag */,
15215 bp_static_tracepoint /* type_wanted */,
15216 0 /* Ignore count */,
15217 pending_break_support,
15218 ops,
15219 from_tty,
15220 1 /* enabled */,
15221 0 /* internal */, 0);
7a697b8d
SS
15222}
15223
409873ef
SS
15224/* Set up a fake reader function that gets command lines from a linked
15225 list that was acquired during tracepoint uploading. */
15226
15227static struct uploaded_tp *this_utp;
3149d8c1 15228static int next_cmd;
409873ef
SS
15229
15230static char *
15231read_uploaded_action (void)
15232{
15233 char *rslt;
15234
3149d8c1 15235 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15236
3149d8c1 15237 next_cmd++;
409873ef
SS
15238
15239 return rslt;
15240}
15241
00bf0b85
SS
15242/* Given information about a tracepoint as recorded on a target (which
15243 can be either a live system or a trace file), attempt to create an
15244 equivalent GDB tracepoint. This is not a reliable process, since
15245 the target does not necessarily have all the information used when
15246 the tracepoint was originally defined. */
15247
d9b3f62e 15248struct tracepoint *
00bf0b85 15249create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15250{
409873ef 15251 char *addr_str, small_buf[100];
d9b3f62e 15252 struct tracepoint *tp;
fd9b8c24 15253
409873ef
SS
15254 if (utp->at_string)
15255 addr_str = utp->at_string;
15256 else
15257 {
15258 /* In the absence of a source location, fall back to raw
15259 address. Since there is no way to confirm that the address
15260 means the same thing as when the trace was started, warn the
15261 user. */
3e43a32a
MS
15262 warning (_("Uploaded tracepoint %d has no "
15263 "source location, using raw address"),
409873ef 15264 utp->number);
8c042590 15265 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15266 addr_str = small_buf;
15267 }
15268
15269 /* There's not much we can do with a sequence of bytecodes. */
15270 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15271 warning (_("Uploaded tracepoint %d condition "
15272 "has no source form, ignoring it"),
409873ef 15273 utp->number);
d5551862 15274
ffc2605c
TT
15275 event_location_up location = string_to_event_location (&addr_str,
15276 current_language);
8cdf0e15 15277 if (!create_breakpoint (get_current_arch (),
ffc2605c 15278 location.get (),
f00aae0f 15279 utp->cond_string, -1, addr_str,
e7e0cddf 15280 0 /* parse cond/thread */,
8cdf0e15 15281 0 /* tempflag */,
0fb4aa4b 15282 utp->type /* type_wanted */,
8cdf0e15
VP
15283 0 /* Ignore count */,
15284 pending_break_support,
348d480f 15285 &tracepoint_breakpoint_ops,
8cdf0e15 15286 0 /* from_tty */,
84f4c1fe 15287 utp->enabled /* enabled */,
44f238bb
PA
15288 0 /* internal */,
15289 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15290 return NULL;
fd9b8c24 15291
409873ef 15292 /* Get the tracepoint we just created. */
fd9b8c24
PA
15293 tp = get_tracepoint (tracepoint_count);
15294 gdb_assert (tp != NULL);
d5551862 15295
00bf0b85
SS
15296 if (utp->pass > 0)
15297 {
8c042590
PM
15298 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15299 tp->base.number);
00bf0b85 15300
409873ef 15301 trace_pass_command (small_buf, 0);
00bf0b85
SS
15302 }
15303
409873ef
SS
15304 /* If we have uploaded versions of the original commands, set up a
15305 special-purpose "reader" function and call the usual command line
15306 reader, then pass the result to the breakpoint command-setting
15307 function. */
3149d8c1 15308 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15309 {
93921405 15310 command_line_up cmd_list;
00bf0b85 15311
409873ef 15312 this_utp = utp;
3149d8c1 15313 next_cmd = 0;
d5551862 15314
409873ef
SS
15315 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15316
93921405 15317 breakpoint_set_commands (&tp->base, std::move (cmd_list));
00bf0b85 15318 }
3149d8c1
SS
15319 else if (!VEC_empty (char_ptr, utp->actions)
15320 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15321 warning (_("Uploaded tracepoint %d actions "
15322 "have no source form, ignoring them"),
409873ef 15323 utp->number);
00bf0b85 15324
f196051f
SS
15325 /* Copy any status information that might be available. */
15326 tp->base.hit_count = utp->hit_count;
15327 tp->traceframe_usage = utp->traceframe_usage;
15328
00bf0b85 15329 return tp;
d9b3f62e 15330}
00bf0b85 15331
1042e4c0
SS
15332/* Print information on tracepoint number TPNUM_EXP, or all if
15333 omitted. */
15334
15335static void
e5a67952 15336tracepoints_info (char *args, int from_tty)
1042e4c0 15337{
79a45e25 15338 struct ui_out *uiout = current_uiout;
e5a67952 15339 int num_printed;
1042e4c0 15340
e5a67952 15341 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15342
15343 if (num_printed == 0)
1042e4c0 15344 {
e5a67952 15345 if (args == NULL || *args == '\0')
112e8700 15346 uiout->message ("No tracepoints.\n");
d77f58be 15347 else
112e8700 15348 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15349 }
ad443146
SS
15350
15351 default_collect_info ();
1042e4c0
SS
15352}
15353
4a64f543 15354/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15355 Not supported by all targets. */
15356static void
15357enable_trace_command (char *args, int from_tty)
15358{
15359 enable_command (args, from_tty);
15360}
15361
4a64f543 15362/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15363 Not supported by all targets. */
15364static void
15365disable_trace_command (char *args, int from_tty)
15366{
15367 disable_command (args, from_tty);
15368}
15369
4a64f543 15370/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15371static void
15372delete_trace_command (char *arg, int from_tty)
15373{
35df4500 15374 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15375
15376 dont_repeat ();
15377
15378 if (arg == 0)
15379 {
15380 int breaks_to_delete = 0;
15381
15382 /* Delete all breakpoints if no argument.
15383 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15384 have to be deleted with an explicit breakpoint number
15385 argument. */
1042e4c0 15386 ALL_TRACEPOINTS (b)
46c6471b 15387 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15388 {
15389 breaks_to_delete = 1;
15390 break;
15391 }
1042e4c0
SS
15392
15393 /* Ask user only if there are some breakpoints to delete. */
15394 if (!from_tty
15395 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15396 {
35df4500 15397 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15398 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15399 delete_breakpoint (b);
1042e4c0
SS
15400 }
15401 }
15402 else
51be5b68 15403 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15404}
15405
197f0a60
TT
15406/* Helper function for trace_pass_command. */
15407
15408static void
d9b3f62e 15409trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15410{
d9b3f62e 15411 tp->pass_count = count;
6f6484cd 15412 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15413 if (from_tty)
15414 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15415 tp->base.number, count);
197f0a60
TT
15416}
15417
1042e4c0
SS
15418/* Set passcount for tracepoint.
15419
15420 First command argument is passcount, second is tracepoint number.
15421 If tracepoint number omitted, apply to most recently defined.
15422 Also accepts special argument "all". */
15423
15424static void
15425trace_pass_command (char *args, int from_tty)
15426{
d9b3f62e 15427 struct tracepoint *t1;
1042e4c0 15428 unsigned int count;
1042e4c0
SS
15429
15430 if (args == 0 || *args == 0)
3e43a32a
MS
15431 error (_("passcount command requires an "
15432 "argument (count + optional TP num)"));
1042e4c0 15433
4a64f543 15434 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15435
529480d0 15436 args = skip_spaces (args);
1042e4c0
SS
15437 if (*args && strncasecmp (args, "all", 3) == 0)
15438 {
d9b3f62e
PA
15439 struct breakpoint *b;
15440
1042e4c0 15441 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15442 if (*args)
15443 error (_("Junk at end of arguments."));
1042e4c0 15444
d9b3f62e 15445 ALL_TRACEPOINTS (b)
197f0a60 15446 {
d9b3f62e 15447 t1 = (struct tracepoint *) b;
197f0a60
TT
15448 trace_pass_set_count (t1, count, from_tty);
15449 }
15450 }
15451 else if (*args == '\0')
1042e4c0 15452 {
5fa1d40e 15453 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15454 if (t1)
197f0a60
TT
15455 trace_pass_set_count (t1, count, from_tty);
15456 }
15457 else
15458 {
bfd28288
PA
15459 number_or_range_parser parser (args);
15460 while (!parser.finished ())
1042e4c0 15461 {
bfd28288 15462 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15463 if (t1)
15464 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15465 }
15466 }
1042e4c0
SS
15467}
15468
d9b3f62e 15469struct tracepoint *
1042e4c0
SS
15470get_tracepoint (int num)
15471{
15472 struct breakpoint *t;
15473
15474 ALL_TRACEPOINTS (t)
15475 if (t->number == num)
d9b3f62e 15476 return (struct tracepoint *) t;
1042e4c0
SS
15477
15478 return NULL;
15479}
15480
d5551862
SS
15481/* Find the tracepoint with the given target-side number (which may be
15482 different from the tracepoint number after disconnecting and
15483 reconnecting). */
15484
d9b3f62e 15485struct tracepoint *
d5551862
SS
15486get_tracepoint_by_number_on_target (int num)
15487{
d9b3f62e 15488 struct breakpoint *b;
d5551862 15489
d9b3f62e
PA
15490 ALL_TRACEPOINTS (b)
15491 {
15492 struct tracepoint *t = (struct tracepoint *) b;
15493
15494 if (t->number_on_target == num)
15495 return t;
15496 }
d5551862
SS
15497
15498 return NULL;
15499}
15500
1042e4c0 15501/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15502 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15503 If the argument is missing, the most recent tracepoint
15504 (tracepoint_count) is returned. */
15505
d9b3f62e 15506struct tracepoint *
197f0a60 15507get_tracepoint_by_number (char **arg,
bfd28288 15508 number_or_range_parser *parser)
1042e4c0 15509{
1042e4c0
SS
15510 struct breakpoint *t;
15511 int tpnum;
15512 char *instring = arg == NULL ? NULL : *arg;
15513
bfd28288 15514 if (parser != NULL)
197f0a60 15515 {
bfd28288
PA
15516 gdb_assert (!parser->finished ());
15517 tpnum = parser->get_number ();
197f0a60
TT
15518 }
15519 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15520 tpnum = tracepoint_count;
1042e4c0 15521 else
197f0a60 15522 tpnum = get_number (arg);
1042e4c0
SS
15523
15524 if (tpnum <= 0)
15525 {
15526 if (instring && *instring)
15527 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15528 instring);
15529 else
5fa1d40e 15530 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15531 return NULL;
15532 }
15533
15534 ALL_TRACEPOINTS (t)
15535 if (t->number == tpnum)
15536 {
d9b3f62e 15537 return (struct tracepoint *) t;
1042e4c0
SS
15538 }
15539
1042e4c0
SS
15540 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15541 return NULL;
15542}
15543
d9b3f62e
PA
15544void
15545print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15546{
15547 if (b->thread != -1)
15548 fprintf_unfiltered (fp, " thread %d", b->thread);
15549
15550 if (b->task != 0)
15551 fprintf_unfiltered (fp, " task %d", b->task);
15552
15553 fprintf_unfiltered (fp, "\n");
15554}
15555
6149aea9
PA
15556/* Save information on user settable breakpoints (watchpoints, etc) to
15557 a new script file named FILENAME. If FILTER is non-NULL, call it
15558 on each breakpoint and only include the ones for which it returns
15559 non-zero. */
15560
1042e4c0 15561static void
6149aea9
PA
15562save_breakpoints (char *filename, int from_tty,
15563 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15564{
15565 struct breakpoint *tp;
6149aea9 15566 int any = 0;
1042e4c0 15567 struct cleanup *cleanup;
6149aea9 15568 int extra_trace_bits = 0;
1042e4c0 15569
6149aea9
PA
15570 if (filename == 0 || *filename == 0)
15571 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15572
15573 /* See if we have anything to save. */
6149aea9 15574 ALL_BREAKPOINTS (tp)
1042e4c0 15575 {
6149aea9 15576 /* Skip internal and momentary breakpoints. */
09d682a4 15577 if (!user_breakpoint_p (tp))
6149aea9
PA
15578 continue;
15579
15580 /* If we have a filter, only save the breakpoints it accepts. */
15581 if (filter && !filter (tp))
15582 continue;
15583
15584 any = 1;
15585
15586 if (is_tracepoint (tp))
15587 {
15588 extra_trace_bits = 1;
15589
15590 /* We can stop searching. */
15591 break;
15592 }
1042e4c0 15593 }
6149aea9
PA
15594
15595 if (!any)
1042e4c0 15596 {
6149aea9 15597 warning (_("Nothing to save."));
1042e4c0
SS
15598 return;
15599 }
15600
c718be47
PA
15601 filename = tilde_expand (filename);
15602 cleanup = make_cleanup (xfree, filename);
d7e74731
PA
15603
15604 stdio_file fp;
15605
15606 if (!fp.open (filename, "w"))
6149aea9
PA
15607 error (_("Unable to open file '%s' for saving (%s)"),
15608 filename, safe_strerror (errno));
8bf6485c 15609
6149aea9 15610 if (extra_trace_bits)
d7e74731 15611 save_trace_state_variables (&fp);
8bf6485c 15612
6149aea9 15613 ALL_BREAKPOINTS (tp)
1042e4c0 15614 {
6149aea9 15615 /* Skip internal and momentary breakpoints. */
09d682a4 15616 if (!user_breakpoint_p (tp))
6149aea9 15617 continue;
8bf6485c 15618
6149aea9
PA
15619 /* If we have a filter, only save the breakpoints it accepts. */
15620 if (filter && !filter (tp))
15621 continue;
15622
d7e74731 15623 tp->ops->print_recreate (tp, &fp);
1042e4c0 15624
6149aea9
PA
15625 /* Note, we can't rely on tp->number for anything, as we can't
15626 assume the recreated breakpoint numbers will match. Use $bpnum
15627 instead. */
15628
15629 if (tp->cond_string)
d7e74731 15630 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15631
15632 if (tp->ignore_count)
d7e74731 15633 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15634
2d9442cc 15635 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15636 {
d7e74731 15637 fp.puts (" commands\n");
a7bdde9e 15638
d7e74731 15639 current_uiout->redirect (&fp);
492d29ea 15640 TRY
1042e4c0 15641 {
79a45e25 15642 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15643 }
492d29ea
PA
15644 CATCH (ex, RETURN_MASK_ALL)
15645 {
112e8700 15646 current_uiout->redirect (NULL);
492d29ea
PA
15647 throw_exception (ex);
15648 }
15649 END_CATCH
1042e4c0 15650
112e8700 15651 current_uiout->redirect (NULL);
d7e74731 15652 fp.puts (" end\n");
1042e4c0 15653 }
6149aea9
PA
15654
15655 if (tp->enable_state == bp_disabled)
d7e74731 15656 fp.puts ("disable $bpnum\n");
6149aea9
PA
15657
15658 /* If this is a multi-location breakpoint, check if the locations
15659 should be individually disabled. Watchpoint locations are
15660 special, and not user visible. */
15661 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15662 {
15663 struct bp_location *loc;
15664 int n = 1;
15665
15666 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15667 if (!loc->enabled)
d7e74731 15668 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15669 }
1042e4c0 15670 }
8bf6485c 15671
6149aea9 15672 if (extra_trace_bits && *default_collect)
d7e74731 15673 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15674
1042e4c0 15675 if (from_tty)
6149aea9 15676 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15677 do_cleanups (cleanup);
6149aea9
PA
15678}
15679
15680/* The `save breakpoints' command. */
15681
15682static void
15683save_breakpoints_command (char *args, int from_tty)
15684{
15685 save_breakpoints (args, from_tty, NULL);
15686}
15687
15688/* The `save tracepoints' command. */
15689
15690static void
15691save_tracepoints_command (char *args, int from_tty)
15692{
15693 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15694}
15695
15696/* Create a vector of all tracepoints. */
15697
15698VEC(breakpoint_p) *
eeae04df 15699all_tracepoints (void)
1042e4c0
SS
15700{
15701 VEC(breakpoint_p) *tp_vec = 0;
15702 struct breakpoint *tp;
15703
15704 ALL_TRACEPOINTS (tp)
15705 {
15706 VEC_safe_push (breakpoint_p, tp_vec, tp);
15707 }
15708
15709 return tp_vec;
15710}
15711
c906108c 15712\f
629500fa
KS
15713/* This help string is used to consolidate all the help string for specifying
15714 locations used by several commands. */
15715
15716#define LOCATION_HELP_STRING \
15717"Linespecs are colon-separated lists of location parameters, such as\n\
15718source filename, function name, label name, and line number.\n\
15719Example: To specify the start of a label named \"the_top\" in the\n\
15720function \"fact\" in the file \"factorial.c\", use\n\
15721\"factorial.c:fact:the_top\".\n\
15722\n\
15723Address locations begin with \"*\" and specify an exact address in the\n\
15724program. Example: To specify the fourth byte past the start function\n\
15725\"main\", use \"*main + 4\".\n\
15726\n\
15727Explicit locations are similar to linespecs but use an option/argument\n\
15728syntax to specify location parameters.\n\
15729Example: To specify the start of the label named \"the_top\" in the\n\
15730function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15731-function fact -label the_top\".\n"
15732
4a64f543
MS
15733/* This help string is used for the break, hbreak, tbreak and thbreak
15734 commands. It is defined as a macro to prevent duplication.
15735 COMMAND should be a string constant containing the name of the
15736 command. */
629500fa 15737
31e2b00f 15738#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15739command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15740PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15741probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15742guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15743`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15744LOCATION may be a linespec, address, or explicit location as described\n\
15745below.\n\
15746\n\
dc10affe
PA
15747With no LOCATION, uses current execution address of the selected\n\
15748stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15749\n\
15750THREADNUM is the number from \"info threads\".\n\
15751CONDITION is a boolean expression.\n\
629500fa 15752\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15753Multiple breakpoints at one place are permitted, and useful if their\n\
15754conditions are different.\n\
31e2b00f
AS
15755\n\
15756Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15757
44feb3ce
TT
15758/* List of subcommands for "catch". */
15759static struct cmd_list_element *catch_cmdlist;
15760
15761/* List of subcommands for "tcatch". */
15762static struct cmd_list_element *tcatch_cmdlist;
15763
9ac4176b 15764void
a121b7c1 15765add_catch_command (const char *name, const char *docstring,
82ae6c8d 15766 cmd_sfunc_ftype *sfunc,
625e8578 15767 completer_ftype *completer,
44feb3ce
TT
15768 void *user_data_catch,
15769 void *user_data_tcatch)
15770{
15771 struct cmd_list_element *command;
15772
15773 command = add_cmd (name, class_breakpoint, NULL, docstring,
15774 &catch_cmdlist);
15775 set_cmd_sfunc (command, sfunc);
15776 set_cmd_context (command, user_data_catch);
a96d9b2e 15777 set_cmd_completer (command, completer);
44feb3ce
TT
15778
15779 command = add_cmd (name, class_breakpoint, NULL, docstring,
15780 &tcatch_cmdlist);
15781 set_cmd_sfunc (command, sfunc);
15782 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15783 set_cmd_completer (command, completer);
44feb3ce
TT
15784}
15785
6149aea9
PA
15786static void
15787save_command (char *arg, int from_tty)
15788{
3e43a32a
MS
15789 printf_unfiltered (_("\"save\" must be followed by "
15790 "the name of a save subcommand.\n"));
635c7e8a 15791 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15792}
15793
84f4c1fe
PM
15794struct breakpoint *
15795iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15796 void *data)
15797{
35df4500 15798 struct breakpoint *b, *b_tmp;
84f4c1fe 15799
35df4500 15800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15801 {
15802 if ((*callback) (b, data))
15803 return b;
15804 }
15805
15806 return NULL;
15807}
15808
0574c78f
GB
15809/* Zero if any of the breakpoint's locations could be a location where
15810 functions have been inlined, nonzero otherwise. */
15811
15812static int
15813is_non_inline_function (struct breakpoint *b)
15814{
15815 /* The shared library event breakpoint is set on the address of a
15816 non-inline function. */
15817 if (b->type == bp_shlib_event)
15818 return 1;
15819
15820 return 0;
15821}
15822
15823/* Nonzero if the specified PC cannot be a location where functions
15824 have been inlined. */
15825
15826int
09ac7c10
TT
15827pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15828 const struct target_waitstatus *ws)
0574c78f
GB
15829{
15830 struct breakpoint *b;
15831 struct bp_location *bl;
15832
15833 ALL_BREAKPOINTS (b)
15834 {
15835 if (!is_non_inline_function (b))
15836 continue;
15837
15838 for (bl = b->loc; bl != NULL; bl = bl->next)
15839 {
15840 if (!bl->shlib_disabled
09ac7c10 15841 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15842 return 1;
15843 }
15844 }
15845
15846 return 0;
15847}
15848
2f202fde
JK
15849/* Remove any references to OBJFILE which is going to be freed. */
15850
15851void
15852breakpoint_free_objfile (struct objfile *objfile)
15853{
15854 struct bp_location **locp, *loc;
15855
15856 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15857 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15858 loc->symtab = NULL;
15859}
15860
2060206e
PA
15861void
15862initialize_breakpoint_ops (void)
15863{
15864 static int initialized = 0;
15865
15866 struct breakpoint_ops *ops;
15867
15868 if (initialized)
15869 return;
15870 initialized = 1;
15871
15872 /* The breakpoint_ops structure to be inherit by all kinds of
15873 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15874 internal and momentary breakpoints, etc.). */
15875 ops = &bkpt_base_breakpoint_ops;
15876 *ops = base_breakpoint_ops;
15877 ops->re_set = bkpt_re_set;
15878 ops->insert_location = bkpt_insert_location;
15879 ops->remove_location = bkpt_remove_location;
15880 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15881 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15882 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15883 ops->decode_location = bkpt_decode_location;
2060206e
PA
15884
15885 /* The breakpoint_ops structure to be used in regular breakpoints. */
15886 ops = &bkpt_breakpoint_ops;
15887 *ops = bkpt_base_breakpoint_ops;
15888 ops->re_set = bkpt_re_set;
15889 ops->resources_needed = bkpt_resources_needed;
15890 ops->print_it = bkpt_print_it;
15891 ops->print_mention = bkpt_print_mention;
15892 ops->print_recreate = bkpt_print_recreate;
15893
15894 /* Ranged breakpoints. */
15895 ops = &ranged_breakpoint_ops;
15896 *ops = bkpt_breakpoint_ops;
15897 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15898 ops->resources_needed = resources_needed_ranged_breakpoint;
15899 ops->print_it = print_it_ranged_breakpoint;
15900 ops->print_one = print_one_ranged_breakpoint;
15901 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15902 ops->print_mention = print_mention_ranged_breakpoint;
15903 ops->print_recreate = print_recreate_ranged_breakpoint;
15904
15905 /* Internal breakpoints. */
15906 ops = &internal_breakpoint_ops;
15907 *ops = bkpt_base_breakpoint_ops;
15908 ops->re_set = internal_bkpt_re_set;
15909 ops->check_status = internal_bkpt_check_status;
15910 ops->print_it = internal_bkpt_print_it;
15911 ops->print_mention = internal_bkpt_print_mention;
15912
15913 /* Momentary breakpoints. */
15914 ops = &momentary_breakpoint_ops;
15915 *ops = bkpt_base_breakpoint_ops;
15916 ops->re_set = momentary_bkpt_re_set;
15917 ops->check_status = momentary_bkpt_check_status;
15918 ops->print_it = momentary_bkpt_print_it;
15919 ops->print_mention = momentary_bkpt_print_mention;
15920
e2e4d78b
JK
15921 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15922 ops = &longjmp_breakpoint_ops;
15923 *ops = momentary_breakpoint_ops;
15924 ops->dtor = longjmp_bkpt_dtor;
15925
55aa24fb
SDJ
15926 /* Probe breakpoints. */
15927 ops = &bkpt_probe_breakpoint_ops;
15928 *ops = bkpt_breakpoint_ops;
15929 ops->insert_location = bkpt_probe_insert_location;
15930 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15931 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15932 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15933
2060206e
PA
15934 /* Watchpoints. */
15935 ops = &watchpoint_breakpoint_ops;
15936 *ops = base_breakpoint_ops;
3a5c3e22 15937 ops->dtor = dtor_watchpoint;
2060206e
PA
15938 ops->re_set = re_set_watchpoint;
15939 ops->insert_location = insert_watchpoint;
15940 ops->remove_location = remove_watchpoint;
15941 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15942 ops->check_status = check_status_watchpoint;
15943 ops->resources_needed = resources_needed_watchpoint;
15944 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15945 ops->print_it = print_it_watchpoint;
15946 ops->print_mention = print_mention_watchpoint;
15947 ops->print_recreate = print_recreate_watchpoint;
427cd150 15948 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15949
15950 /* Masked watchpoints. */
15951 ops = &masked_watchpoint_breakpoint_ops;
15952 *ops = watchpoint_breakpoint_ops;
15953 ops->insert_location = insert_masked_watchpoint;
15954 ops->remove_location = remove_masked_watchpoint;
15955 ops->resources_needed = resources_needed_masked_watchpoint;
15956 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15957 ops->print_it = print_it_masked_watchpoint;
15958 ops->print_one_detail = print_one_detail_masked_watchpoint;
15959 ops->print_mention = print_mention_masked_watchpoint;
15960 ops->print_recreate = print_recreate_masked_watchpoint;
15961
15962 /* Tracepoints. */
15963 ops = &tracepoint_breakpoint_ops;
15964 *ops = base_breakpoint_ops;
15965 ops->re_set = tracepoint_re_set;
15966 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15967 ops->print_one_detail = tracepoint_print_one_detail;
15968 ops->print_mention = tracepoint_print_mention;
15969 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15970 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15971 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15972 ops->decode_location = tracepoint_decode_location;
983af33b 15973
55aa24fb
SDJ
15974 /* Probe tracepoints. */
15975 ops = &tracepoint_probe_breakpoint_ops;
15976 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15977 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15978 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15979
983af33b
SDJ
15980 /* Static tracepoints with marker (`-m'). */
15981 ops = &strace_marker_breakpoint_ops;
15982 *ops = tracepoint_breakpoint_ops;
5f700d83 15983 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15984 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15985 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15986
15987 /* Fork catchpoints. */
15988 ops = &catch_fork_breakpoint_ops;
15989 *ops = base_breakpoint_ops;
15990 ops->insert_location = insert_catch_fork;
15991 ops->remove_location = remove_catch_fork;
15992 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15993 ops->print_it = print_it_catch_fork;
15994 ops->print_one = print_one_catch_fork;
15995 ops->print_mention = print_mention_catch_fork;
15996 ops->print_recreate = print_recreate_catch_fork;
15997
15998 /* Vfork catchpoints. */
15999 ops = &catch_vfork_breakpoint_ops;
16000 *ops = base_breakpoint_ops;
16001 ops->insert_location = insert_catch_vfork;
16002 ops->remove_location = remove_catch_vfork;
16003 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16004 ops->print_it = print_it_catch_vfork;
16005 ops->print_one = print_one_catch_vfork;
16006 ops->print_mention = print_mention_catch_vfork;
16007 ops->print_recreate = print_recreate_catch_vfork;
16008
16009 /* Exec catchpoints. */
16010 ops = &catch_exec_breakpoint_ops;
16011 *ops = base_breakpoint_ops;
16012 ops->dtor = dtor_catch_exec;
16013 ops->insert_location = insert_catch_exec;
16014 ops->remove_location = remove_catch_exec;
16015 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16016 ops->print_it = print_it_catch_exec;
16017 ops->print_one = print_one_catch_exec;
16018 ops->print_mention = print_mention_catch_exec;
16019 ops->print_recreate = print_recreate_catch_exec;
16020
edcc5120
TT
16021 /* Solib-related catchpoints. */
16022 ops = &catch_solib_breakpoint_ops;
16023 *ops = base_breakpoint_ops;
16024 ops->dtor = dtor_catch_solib;
16025 ops->insert_location = insert_catch_solib;
16026 ops->remove_location = remove_catch_solib;
16027 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16028 ops->check_status = check_status_catch_solib;
16029 ops->print_it = print_it_catch_solib;
16030 ops->print_one = print_one_catch_solib;
16031 ops->print_mention = print_mention_catch_solib;
16032 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16033
16034 ops = &dprintf_breakpoint_ops;
16035 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16036 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16037 ops->resources_needed = bkpt_resources_needed;
16038 ops->print_it = bkpt_print_it;
16039 ops->print_mention = bkpt_print_mention;
2d9442cc 16040 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16041 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16042 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16043}
16044
8bfd80db
YQ
16045/* Chain containing all defined "enable breakpoint" subcommands. */
16046
16047static struct cmd_list_element *enablebreaklist = NULL;
16048
c906108c 16049void
fba45db2 16050_initialize_breakpoint (void)
c906108c
SS
16051{
16052 struct cmd_list_element *c;
16053
2060206e
PA
16054 initialize_breakpoint_ops ();
16055
84acb35a 16056 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16057 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16058 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16059
55aa24fb
SDJ
16060 breakpoint_objfile_key
16061 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16062
c906108c
SS
16063 breakpoint_chain = 0;
16064 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16065 before a breakpoint is set. */
16066 breakpoint_count = 0;
16067
1042e4c0
SS
16068 tracepoint_count = 0;
16069
1bedd215
AC
16070 add_com ("ignore", class_breakpoint, ignore_command, _("\
16071Set ignore-count of breakpoint number N to COUNT.\n\
16072Usage is `ignore N COUNT'."));
c906108c 16073
1bedd215 16074 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
16075Set commands to be executed when the given breakpoints are hit.\n\
16076Give a space-separated breakpoint list as argument after \"commands\".\n\
16077A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16078(e.g. `5-7').\n\
c906108c
SS
16079With no argument, the targeted breakpoint is the last one set.\n\
16080The commands themselves follow starting on the next line.\n\
16081Type a line containing \"end\" to indicate the end of them.\n\
16082Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16083then no output is printed when it is hit, except what the commands print."));
c906108c 16084
d55637df 16085 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16086Specify breakpoint number N to break only if COND is true.\n\
c906108c 16087Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16088expression to be evaluated whenever breakpoint N is reached."));
d55637df 16089 set_cmd_completer (c, condition_completer);
c906108c 16090
1bedd215 16091 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16092Set a temporary breakpoint.\n\
c906108c
SS
16093Like \"break\" except the breakpoint is only temporary,\n\
16094so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16095by using \"enable delete\" on the breakpoint number.\n\
16096\n"
16097BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16098 set_cmd_completer (c, location_completer);
c94fdfd0 16099
1bedd215 16100 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16101Set a hardware assisted breakpoint.\n\
c906108c 16102Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16103some target hardware may not have this support.\n\
16104\n"
16105BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16106 set_cmd_completer (c, location_completer);
c906108c 16107
1bedd215 16108 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16109Set a temporary hardware assisted breakpoint.\n\
c906108c 16110Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16111so it will be deleted when hit.\n\
16112\n"
16113BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16114 set_cmd_completer (c, location_completer);
c906108c 16115
1bedd215
AC
16116 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16117Enable some breakpoints.\n\
c906108c
SS
16118Give breakpoint numbers (separated by spaces) as arguments.\n\
16119With no subcommand, breakpoints are enabled until you command otherwise.\n\
16120This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16121With a subcommand you can enable temporarily."),
c906108c 16122 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16123
16124 add_com_alias ("en", "enable", class_breakpoint, 1);
16125
84951ab5 16126 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16127Enable some breakpoints.\n\
c906108c
SS
16128Give breakpoint numbers (separated by spaces) as arguments.\n\
16129This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16130May be abbreviated to simply \"enable\".\n"),
c5aa993b 16131 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16132
1a966eab
AC
16133 add_cmd ("once", no_class, enable_once_command, _("\
16134Enable breakpoints for one hit. Give breakpoint numbers.\n\
16135If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16136 &enablebreaklist);
16137
1a966eab
AC
16138 add_cmd ("delete", no_class, enable_delete_command, _("\
16139Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16140If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16141 &enablebreaklist);
16142
816338b5
SS
16143 add_cmd ("count", no_class, enable_count_command, _("\
16144Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16145If a breakpoint is hit while enabled in this fashion,\n\
16146the count is decremented; when it reaches zero, the breakpoint is disabled."),
16147 &enablebreaklist);
16148
1a966eab
AC
16149 add_cmd ("delete", no_class, enable_delete_command, _("\
16150Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16151If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16152 &enablelist);
16153
1a966eab
AC
16154 add_cmd ("once", no_class, enable_once_command, _("\
16155Enable breakpoints for one hit. Give breakpoint numbers.\n\
16156If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16157 &enablelist);
16158
16159 add_cmd ("count", no_class, enable_count_command, _("\
16160Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16161If a breakpoint is hit while enabled in this fashion,\n\
16162the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16163 &enablelist);
16164
1bedd215
AC
16165 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16166Disable some breakpoints.\n\
c906108c
SS
16167Arguments are breakpoint numbers with spaces in between.\n\
16168To disable all breakpoints, give no argument.\n\
64b9b334 16169A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16170 &disablelist, "disable ", 1, &cmdlist);
16171 add_com_alias ("dis", "disable", class_breakpoint, 1);
16172 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16173
1a966eab
AC
16174 add_cmd ("breakpoints", class_alias, disable_command, _("\
16175Disable some breakpoints.\n\
c906108c
SS
16176Arguments are breakpoint numbers with spaces in between.\n\
16177To disable all breakpoints, give no argument.\n\
64b9b334 16178A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16179This command may be abbreviated \"disable\"."),
c906108c
SS
16180 &disablelist);
16181
1bedd215
AC
16182 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16183Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16184Arguments are breakpoint numbers with spaces in between.\n\
16185To delete all breakpoints, give no argument.\n\
16186\n\
16187Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16188The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16189 &deletelist, "delete ", 1, &cmdlist);
16190 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16191 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16192
1a966eab
AC
16193 add_cmd ("breakpoints", class_alias, delete_command, _("\
16194Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16195Arguments are breakpoint numbers with spaces in between.\n\
16196To delete all breakpoints, give no argument.\n\
1a966eab 16197This command may be abbreviated \"delete\"."),
c906108c
SS
16198 &deletelist);
16199
1bedd215 16200 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16201Clear breakpoint at specified location.\n\
16202Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16203\n\
16204With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16205is executing in.\n"
16206"\n" LOCATION_HELP_STRING "\n\
1bedd215 16207See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16208 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16209
1bedd215 16210 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16211Set breakpoint at specified location.\n"
31e2b00f 16212BREAK_ARGS_HELP ("break")));
5ba2abeb 16213 set_cmd_completer (c, location_completer);
c94fdfd0 16214
c906108c
SS
16215 add_com_alias ("b", "break", class_run, 1);
16216 add_com_alias ("br", "break", class_run, 1);
16217 add_com_alias ("bre", "break", class_run, 1);
16218 add_com_alias ("brea", "break", class_run, 1);
16219
c906108c
SS
16220 if (dbx_commands)
16221 {
1bedd215
AC
16222 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16223Break in function/address or break at a line in the current file."),
c5aa993b
JM
16224 &stoplist, "stop ", 1, &cmdlist);
16225 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16226 _("Break in function or address."), &stoplist);
c5aa993b 16227 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16228 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16229 add_com ("status", class_info, breakpoints_info, _("\
16230Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16231The \"Type\" column indicates one of:\n\
16232\tbreakpoint - normal breakpoint\n\
16233\twatchpoint - watchpoint\n\
16234The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16235the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16236breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16237address and file/line number respectively.\n\
16238\n\
16239Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16240are set to the address of the last breakpoint listed unless the command\n\
16241is prefixed with \"server \".\n\n\
c906108c 16242Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16243breakpoint set."));
c906108c
SS
16244 }
16245
1bedd215 16246 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16247Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16248The \"Type\" column indicates one of:\n\
16249\tbreakpoint - normal breakpoint\n\
16250\twatchpoint - watchpoint\n\
16251The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16252the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16253breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16254address and file/line number respectively.\n\
16255\n\
16256Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16257are set to the address of the last breakpoint listed unless the command\n\
16258is prefixed with \"server \".\n\n\
c906108c 16259Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16260breakpoint set."));
c906108c 16261
6b04bdb7
MS
16262 add_info_alias ("b", "breakpoints", 1);
16263
1a966eab
AC
16264 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16265Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16266The \"Type\" column indicates one of:\n\
16267\tbreakpoint - normal breakpoint\n\
16268\twatchpoint - watchpoint\n\
16269\tlongjmp - internal breakpoint used to step through longjmp()\n\
16270\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16271\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16272\tfinish - internal breakpoint used by the \"finish\" command\n\
16273The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16274the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16275breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16276address and file/line number respectively.\n\
16277\n\
16278Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16279are set to the address of the last breakpoint listed unless the command\n\
16280is prefixed with \"server \".\n\n\
c906108c 16281Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16282breakpoint set."),
c906108c
SS
16283 &maintenanceinfolist);
16284
44feb3ce
TT
16285 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16286Set catchpoints to catch events."),
16287 &catch_cmdlist, "catch ",
16288 0/*allow-unknown*/, &cmdlist);
16289
16290 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16291Set temporary catchpoints to catch events."),
16292 &tcatch_cmdlist, "tcatch ",
16293 0/*allow-unknown*/, &cmdlist);
16294
44feb3ce
TT
16295 add_catch_command ("fork", _("Catch calls to fork."),
16296 catch_fork_command_1,
a96d9b2e 16297 NULL,
44feb3ce
TT
16298 (void *) (uintptr_t) catch_fork_permanent,
16299 (void *) (uintptr_t) catch_fork_temporary);
16300 add_catch_command ("vfork", _("Catch calls to vfork."),
16301 catch_fork_command_1,
a96d9b2e 16302 NULL,
44feb3ce
TT
16303 (void *) (uintptr_t) catch_vfork_permanent,
16304 (void *) (uintptr_t) catch_vfork_temporary);
16305 add_catch_command ("exec", _("Catch calls to exec."),
16306 catch_exec_command_1,
a96d9b2e
SDJ
16307 NULL,
16308 CATCH_PERMANENT,
16309 CATCH_TEMPORARY);
edcc5120
TT
16310 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16311Usage: catch load [REGEX]\n\
16312If REGEX is given, only stop for libraries matching the regular expression."),
16313 catch_load_command_1,
16314 NULL,
16315 CATCH_PERMANENT,
16316 CATCH_TEMPORARY);
16317 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16318Usage: catch unload [REGEX]\n\
16319If REGEX is given, only stop for libraries matching the regular expression."),
16320 catch_unload_command_1,
16321 NULL,
16322 CATCH_PERMANENT,
16323 CATCH_TEMPORARY);
c5aa993b 16324
1bedd215
AC
16325 c = add_com ("watch", class_breakpoint, watch_command, _("\
16326Set a watchpoint for an expression.\n\
06a64a0b 16327Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16328A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16329an expression changes.\n\
16330If -l or -location is given, this evaluates EXPRESSION and watches\n\
16331the memory to which it refers."));
65d12d83 16332 set_cmd_completer (c, expression_completer);
c906108c 16333
1bedd215
AC
16334 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16335Set a read watchpoint for an expression.\n\
06a64a0b 16336Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16337A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16338an expression is read.\n\
16339If -l or -location is given, this evaluates EXPRESSION and watches\n\
16340the memory to which it refers."));
65d12d83 16341 set_cmd_completer (c, expression_completer);
c906108c 16342
1bedd215
AC
16343 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16344Set a watchpoint for an expression.\n\
06a64a0b 16345Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16346A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16347an expression is either read or written.\n\
16348If -l or -location is given, this evaluates EXPRESSION and watches\n\
16349the memory to which it refers."));
65d12d83 16350 set_cmd_completer (c, expression_completer);
c906108c 16351
d77f58be 16352 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16353Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16354
920d2a44
AC
16355 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16356 respond to changes - contrary to the description. */
85c07804
AC
16357 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16358 &can_use_hw_watchpoints, _("\
16359Set debugger's willingness to use watchpoint hardware."), _("\
16360Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16361If zero, gdb will not use hardware for new watchpoints, even if\n\
16362such is available. (However, any hardware watchpoints that were\n\
16363created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16364hardware.)"),
16365 NULL,
920d2a44 16366 show_can_use_hw_watchpoints,
85c07804 16367 &setlist, &showlist);
c906108c
SS
16368
16369 can_use_hw_watchpoints = 1;
fa8d40ab 16370
1042e4c0
SS
16371 /* Tracepoint manipulation commands. */
16372
16373 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16374Set a tracepoint at specified location.\n\
1042e4c0
SS
16375\n"
16376BREAK_ARGS_HELP ("trace") "\n\
16377Do \"help tracepoints\" for info on other tracepoint commands."));
16378 set_cmd_completer (c, location_completer);
16379
16380 add_com_alias ("tp", "trace", class_alias, 0);
16381 add_com_alias ("tr", "trace", class_alias, 1);
16382 add_com_alias ("tra", "trace", class_alias, 1);
16383 add_com_alias ("trac", "trace", class_alias, 1);
16384
7a697b8d 16385 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16386Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16387\n"
16388BREAK_ARGS_HELP ("ftrace") "\n\
16389Do \"help tracepoints\" for info on other tracepoint commands."));
16390 set_cmd_completer (c, location_completer);
16391
0fb4aa4b 16392 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16393Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16394\n\
16395strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16396LOCATION may be a linespec, explicit, or address location (described below) \n\
16397or -m MARKER_ID.\n\n\
16398If a marker id is specified, probe the marker with that name. With\n\
16399no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16400Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16401This collects arbitrary user data passed in the probe point call to the\n\
16402tracing library. You can inspect it when analyzing the trace buffer,\n\
16403by printing the $_sdata variable like any other convenience variable.\n\
16404\n\
16405CONDITION is a boolean expression.\n\
629500fa 16406\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16407Multiple tracepoints at one place are permitted, and useful if their\n\
16408conditions are different.\n\
0fb4aa4b
PA
16409\n\
16410Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16411Do \"help tracepoints\" for info on other tracepoint commands."));
16412 set_cmd_completer (c, location_completer);
16413
1042e4c0 16414 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16415Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16416Convenience variable \"$tpnum\" contains the number of the\n\
16417last tracepoint set."));
16418
16419 add_info_alias ("tp", "tracepoints", 1);
16420
16421 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16422Delete specified tracepoints.\n\
16423Arguments are tracepoint numbers, separated by spaces.\n\
16424No argument means delete all tracepoints."),
16425 &deletelist);
7e20dfcd 16426 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16427
16428 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16429Disable specified tracepoints.\n\
16430Arguments are tracepoint numbers, separated by spaces.\n\
16431No argument means disable all tracepoints."),
16432 &disablelist);
16433 deprecate_cmd (c, "disable");
16434
16435 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16436Enable specified tracepoints.\n\
16437Arguments are tracepoint numbers, separated by spaces.\n\
16438No argument means enable all tracepoints."),
16439 &enablelist);
16440 deprecate_cmd (c, "enable");
16441
16442 add_com ("passcount", class_trace, trace_pass_command, _("\
16443Set the passcount for a tracepoint.\n\
16444The trace will end when the tracepoint has been passed 'count' times.\n\
16445Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16446if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16447
6149aea9
PA
16448 add_prefix_cmd ("save", class_breakpoint, save_command,
16449 _("Save breakpoint definitions as a script."),
16450 &save_cmdlist, "save ",
16451 0/*allow-unknown*/, &cmdlist);
16452
16453 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16454Save current breakpoint definitions as a script.\n\
cce7e648 16455This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16456catchpoints, tracepoints). Use the 'source' command in another debug\n\
16457session to restore them."),
16458 &save_cmdlist);
16459 set_cmd_completer (c, filename_completer);
16460
16461 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16462Save current tracepoint definitions as a script.\n\
6149aea9
PA
16463Use the 'source' command in another debug session to restore them."),
16464 &save_cmdlist);
1042e4c0
SS
16465 set_cmd_completer (c, filename_completer);
16466
6149aea9
PA
16467 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16468 deprecate_cmd (c, "save tracepoints");
16469
1bedd215 16470 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16471Breakpoint specific settings\n\
16472Configure various breakpoint-specific variables such as\n\
1bedd215 16473pending breakpoint behavior"),
fa8d40ab
JJ
16474 &breakpoint_set_cmdlist, "set breakpoint ",
16475 0/*allow-unknown*/, &setlist);
1bedd215 16476 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16477Breakpoint specific settings\n\
16478Configure various breakpoint-specific variables such as\n\
1bedd215 16479pending breakpoint behavior"),
fa8d40ab
JJ
16480 &breakpoint_show_cmdlist, "show breakpoint ",
16481 0/*allow-unknown*/, &showlist);
16482
7915a72c
AC
16483 add_setshow_auto_boolean_cmd ("pending", no_class,
16484 &pending_break_support, _("\
16485Set debugger's behavior regarding pending breakpoints."), _("\
16486Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16487If on, an unrecognized breakpoint location will cause gdb to create a\n\
16488pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16489an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16490user-query to see if a pending breakpoint should be created."),
2c5b56ce 16491 NULL,
920d2a44 16492 show_pending_break_support,
6e1d7d6c
AC
16493 &breakpoint_set_cmdlist,
16494 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16495
16496 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16497
16498 add_setshow_boolean_cmd ("auto-hw", no_class,
16499 &automatic_hardware_breakpoints, _("\
16500Set automatic usage of hardware breakpoints."), _("\
16501Show automatic usage of hardware breakpoints."), _("\
16502If set, the debugger will automatically use hardware breakpoints for\n\
16503breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16504a warning will be emitted for such breakpoints."),
16505 NULL,
16506 show_automatic_hardware_breakpoints,
16507 &breakpoint_set_cmdlist,
16508 &breakpoint_show_cmdlist);
74960c60 16509
a25a5a45
PA
16510 add_setshow_boolean_cmd ("always-inserted", class_support,
16511 &always_inserted_mode, _("\
74960c60
VP
16512Set mode for inserting breakpoints."), _("\
16513Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16514When this mode is on, breakpoints are inserted immediately as soon as\n\
16515they're created, kept inserted even when execution stops, and removed\n\
16516only when the user deletes them. When this mode is off (the default),\n\
16517breakpoints are inserted only when execution continues, and removed\n\
16518when execution stops."),
72d0e2c5
YQ
16519 NULL,
16520 &show_always_inserted_mode,
16521 &breakpoint_set_cmdlist,
16522 &breakpoint_show_cmdlist);
f1310107 16523
b775012e
LM
16524 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16525 condition_evaluation_enums,
16526 &condition_evaluation_mode_1, _("\
16527Set mode of breakpoint condition evaluation."), _("\
16528Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16529When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16530evaluated on the host's side by GDB. When it is set to \"target\",\n\
16531breakpoint conditions will be downloaded to the target (if the target\n\
16532supports such feature) and conditions will be evaluated on the target's side.\n\
16533If this is set to \"auto\" (default), this will be automatically set to\n\
16534\"target\" if it supports condition evaluation, otherwise it will\n\
16535be set to \"gdb\""),
16536 &set_condition_evaluation_mode,
16537 &show_condition_evaluation_mode,
16538 &breakpoint_set_cmdlist,
16539 &breakpoint_show_cmdlist);
16540
f1310107
TJB
16541 add_com ("break-range", class_breakpoint, break_range_command, _("\
16542Set a breakpoint for an address range.\n\
16543break-range START-LOCATION, END-LOCATION\n\
16544where START-LOCATION and END-LOCATION can be one of the following:\n\
16545 LINENUM, for that line in the current file,\n\
16546 FILE:LINENUM, for that line in that file,\n\
16547 +OFFSET, for that number of lines after the current line\n\
16548 or the start of the range\n\
16549 FUNCTION, for the first line in that function,\n\
16550 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16551 *ADDRESS, for the instruction at that address.\n\
16552\n\
16553The breakpoint will stop execution of the inferior whenever it executes\n\
16554an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16555range (including START-LOCATION and END-LOCATION)."));
16556
e7e0cddf 16557 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16558Set a dynamic printf at specified location.\n\
e7e0cddf 16559dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16560location may be a linespec, explicit, or address location.\n"
16561"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16562 set_cmd_completer (c, location_completer);
16563
16564 add_setshow_enum_cmd ("dprintf-style", class_support,
16565 dprintf_style_enums, &dprintf_style, _("\
16566Set the style of usage for dynamic printf."), _("\
16567Show the style of usage for dynamic printf."), _("\
16568This setting chooses how GDB will do a dynamic printf.\n\
16569If the value is \"gdb\", then the printing is done by GDB to its own\n\
16570console, as with the \"printf\" command.\n\
16571If the value is \"call\", the print is done by calling a function in your\n\
16572program; by default printf(), but you can choose a different function or\n\
16573output stream by setting dprintf-function and dprintf-channel."),
16574 update_dprintf_commands, NULL,
16575 &setlist, &showlist);
16576
16577 dprintf_function = xstrdup ("printf");
16578 add_setshow_string_cmd ("dprintf-function", class_support,
16579 &dprintf_function, _("\
16580Set the function to use for dynamic printf"), _("\
16581Show the function to use for dynamic printf"), NULL,
16582 update_dprintf_commands, NULL,
16583 &setlist, &showlist);
16584
16585 dprintf_channel = xstrdup ("");
16586 add_setshow_string_cmd ("dprintf-channel", class_support,
16587 &dprintf_channel, _("\
16588Set the channel to use for dynamic printf"), _("\
16589Show the channel to use for dynamic printf"), NULL,
16590 update_dprintf_commands, NULL,
16591 &setlist, &showlist);
16592
d3ce09f5
SS
16593 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16594 &disconnected_dprintf, _("\
16595Set whether dprintf continues after GDB disconnects."), _("\
16596Show whether dprintf continues after GDB disconnects."), _("\
16597Use this to let dprintf commands continue to hit and produce output\n\
16598even if GDB disconnects or detaches from the target."),
16599 NULL,
16600 NULL,
16601 &setlist, &showlist);
16602
16603 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16604agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16605(target agent only) This is useful for formatted output in user-defined commands."));
16606
765dc015 16607 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16608
16609 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16610 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16611}
This page took 3.092209 seconds and 4 git commands to generate.