]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Add noexcept to custom non-throwing new operators.
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
618f726f 3 Copyright (C) 1986-2016 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"
f00aae0f 70#include "location.h"
cfc31633 71#include "thread-fsm.h"
5d5658a1 72#include "tid-parse.h"
d3ce09f5 73
1042e4c0
SS
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
034dad6f 81#include "mi/mi-common.h"
6dddc817 82#include "extension.h"
325fac50 83#include <algorithm>
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
896b6bda
PA
105static void map_breakpoint_numbers (const char *,
106 void (*) (struct breakpoint *,
107 void *),
95a42b64 108 void *);
c906108c 109
a14ed312 110static void ignore_command (char *, int);
c906108c 111
4efb68b1 112static int breakpoint_re_set_one (void *);
c906108c 113
348d480f
PA
114static void breakpoint_re_set_default (struct breakpoint *);
115
f00aae0f
KS
116static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
983af33b
SDJ
120
121static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
e7e0cddf 123 char *, char *, enum bptype,
983af33b
SDJ
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
44f238bb 127 int, int, int, unsigned);
983af33b 128
f00aae0f
KS
129static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
c2f4122d 131 struct program_space *search_pspace,
f00aae0f 132 struct symtabs_and_lines *sals);
983af33b 133
a14ed312 134static void clear_command (char *, int);
c906108c 135
a14ed312 136static void catch_command (char *, int);
c906108c 137
a9634178 138static int can_use_hardware_watchpoint (struct value *);
c906108c 139
98deb0da 140static void break_command_1 (char *, int, int);
c906108c 141
a14ed312 142static void mention (struct breakpoint *);
c906108c 143
348d480f
PA
144static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
c0a91b2b 146 const struct breakpoint_ops *);
3742cc8b
YQ
147static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
4a64f543
MS
150/* This function is used in gdbtk sources and thus can not be made
151 static. */
63c252f8 152struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 153 struct symtab_and_line,
c0a91b2b
TT
154 enum bptype,
155 const struct breakpoint_ops *);
c906108c 156
06edf0c0
PA
157static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
a1aa2221
LM
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
06edf0c0 162
76897487
KB
163static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
a6d9a66e
UW
165static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
88f7da05 167 enum bptype bptype);
76897487 168
6c95b8df
PA
169static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
5af949e3 171 struct obj_section *, int);
c906108c 172
85d721b8
PA
173static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
f1310107
TJB
176static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
d35ae833
PA
180static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
a14ed312 184static void breakpoints_info (char *, int);
c906108c 185
d77f58be
SS
186static void watchpoints_info (char *, int);
187
e5a67952
MS
188static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
c906108c 190
4efb68b1 191static int breakpoint_cond_eval (void *);
c906108c 192
4efb68b1 193static void cleanup_executing_breakpoints (void *);
c906108c 194
a14ed312 195static void commands_command (char *, int);
c906108c 196
a14ed312 197static void condition_command (char *, int);
c906108c 198
834c0d03 199static int remove_breakpoint (struct bp_location *);
b2b6a7da 200static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 201
e514a9d6 202static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 203
4efb68b1 204static int watchpoint_check (void *);
c906108c 205
a14ed312 206static void maintenance_info_breakpoints (char *, int);
c906108c 207
a14ed312 208static int hw_breakpoint_used_count (void);
c906108c 209
a1398e0c
PA
210static int hw_watchpoint_use_count (struct breakpoint *);
211
212static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
c906108c 215
a14ed312 216static void hbreak_command (char *, int);
c906108c 217
a14ed312 218static void thbreak_command (char *, int);
c906108c 219
816338b5
SS
220static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
c906108c 222
a14ed312 223static void stop_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopin_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void stopat_command (char *arg, int from_tty);
7a292a7a 228
a14ed312 229static void tcatch_command (char *arg, int from_tty);
7a292a7a 230
fe3f5fa8 231static void free_bp_location (struct bp_location *loc);
f431efe5
PA
232static void incref_bp_location (struct bp_location *loc);
233static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 234
39d61571 235static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 236
44702360
PA
237/* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239enum ugll_insert_mode
240{
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
a25a5a45
PA
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
04086b45
PA
260 UGLL_MAY_INSERT,
261
a25a5a45
PA
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
04086b45 269 UGLL_INSERT
44702360
PA
270};
271
272static void update_global_location_list (enum ugll_insert_mode);
a5606eee 273
44702360 274static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 275
d77f58be 276static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
277
278static void insert_breakpoint_locations (void);
a5606eee 279
1042e4c0
SS
280static void tracepoints_info (char *, int);
281
282static void delete_trace_command (char *, int);
283
284static void enable_trace_command (char *, int);
285
286static void disable_trace_command (char *, int);
287
288static void trace_pass_command (char *, int);
289
558a9d82
YQ
290static void set_tracepoint_count (int num);
291
9c06b0b4
TJB
292static int is_masked_watchpoint (const struct breakpoint *b);
293
b775012e
LM
294static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
983af33b
SDJ
296/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 300
2060206e
PA
301/* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306/* Internal breakpoints class type. */
06edf0c0 307static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
308
309/* Momentary breakpoints class type. */
06edf0c0
PA
310static struct breakpoint_ops momentary_breakpoint_ops;
311
e2e4d78b
JK
312/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313static struct breakpoint_ops longjmp_breakpoint_ops;
314
2060206e
PA
315/* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317struct breakpoint_ops bkpt_breakpoint_ops;
318
55aa24fb
SDJ
319/* Breakpoints set on probes. */
320static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
e7e0cddf 322/* Dynamic printf class type. */
c5867ab6 323struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 324
d3ce09f5
SS
325/* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332static const char dprintf_style_gdb[] = "gdb";
333static const char dprintf_style_call[] = "call";
334static const char dprintf_style_agent[] = "agent";
335static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340};
341static const char *dprintf_style = dprintf_style_gdb;
342
343/* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348static char *dprintf_function = "";
349
350/* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358static char *dprintf_channel = "";
359
360/* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362static int disconnected_dprintf = 1;
363
5cea2a26
PA
364/* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366struct counted_command_line
367{
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373};
374
375struct command_line *
376breakpoint_commands (struct breakpoint *b)
377{
378 return b->commands ? b->commands->commands : NULL;
379}
3daf8fe5 380
f3b1572e
PA
381/* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384static int breakpoint_proceeded;
385
956a9fb9 386const char *
2cec12e5
AR
387bpdisp_text (enum bpdisp disp)
388{
4a64f543
MS
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
bc043ef3 392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 393
2cec12e5
AR
394 return bpdisps[(int) disp];
395}
c906108c 396
4a64f543 397/* Prototypes for exported functions. */
c906108c 398/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 399 if such is available. */
c906108c
SS
400static int can_use_hw_watchpoints;
401
920d2a44
AC
402static void
403show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406{
3e43a32a
MS
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
920d2a44
AC
410 value);
411}
412
fa8d40ab
JJ
413/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 415 for unrecognized breakpoint locations.
fa8d40ab
JJ
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417static enum auto_boolean pending_break_support;
920d2a44
AC
418static void
419show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422{
3e43a32a
MS
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
920d2a44
AC
426 value);
427}
fa8d40ab 428
765dc015 429/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 430 set with "break" but falling in read-only memory.
765dc015
VP
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433static int automatic_hardware_breakpoints;
434static void
435show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438{
3e43a32a
MS
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
441 value);
442}
443
a25a5a45
PA
444/* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451static int always_inserted_mode = 0;
72d0e2c5 452
33e5cbd6 453static void
74960c60 454show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 455 struct cmd_list_element *c, const char *value)
74960c60 456{
a25a5a45
PA
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
74960c60
VP
459}
460
b57bacec
PA
461/* See breakpoint.h. */
462
33e5cbd6 463int
a25a5a45 464breakpoints_should_be_inserted_now (void)
33e5cbd6 465{
a25a5a45
PA
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
372316f1
PA
475 struct thread_info *tp;
476
a25a5a45
PA
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
b57bacec
PA
484 if (threads_are_executing ())
485 return 1;
372316f1
PA
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
a25a5a45
PA
493 }
494 return 0;
33e5cbd6 495}
765dc015 496
b775012e
LM
497static const char condition_evaluation_both[] = "host or target";
498
499/* Modes for breakpoint condition evaluation. */
500static const char condition_evaluation_auto[] = "auto";
501static const char condition_evaluation_host[] = "host";
502static const char condition_evaluation_target[] = "target";
503static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508};
509
510/* Global that holds the current mode for breakpoint condition evaluation. */
511static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513/* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517/* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522static const char *
523translate_condition_evaluation_mode (const char *mode)
524{
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534}
535
536/* Discovers what condition_evaluation_auto translates to. */
537
538static const char *
539breakpoint_condition_evaluation_mode (void)
540{
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542}
543
544/* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547static int
548gdb_evaluates_breakpoint_condition_p (void)
549{
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553}
554
a14ed312 555void _initialize_breakpoint (void);
c906108c 556
c906108c
SS
557/* Are we executing breakpoint commands? */
558static int executing_breakpoint_commands;
559
c02f5703
MS
560/* Are overlay event breakpoints enabled? */
561static int overlay_events_enabled;
562
e09342b5
TJB
563/* See description in breakpoint.h. */
564int target_exact_watchpoints = 0;
565
c906108c 566/* Walk the following statement or block through all breakpoints.
e5dd4106 567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 568 current breakpoint. */
c906108c 569
5c44784c 570#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 571
5c44784c
JM
572#define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
c906108c 576
4a64f543
MS
577/* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 580
876fa593
JK
581#define ALL_BP_LOCATIONS(B,BP_TMP) \
582 for (BP_TMP = bp_location; \
583 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
584 BP_TMP++)
7cc221ef 585
b775012e
LM
586/* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
596 && (BP_LOCP_TMP < bp_location + bp_location_count \
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
1042e4c0
SS
600/* Iterator for tracepoints only. */
601
602#define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 604 if (is_tracepoint (B))
1042e4c0 605
7cc221ef 606/* Chains of all breakpoints defined. */
c906108c
SS
607
608struct breakpoint *breakpoint_chain;
609
876fa593
JK
610/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
611
612static struct bp_location **bp_location;
613
614/* Number of elements of BP_LOCATION. */
615
616static unsigned bp_location_count;
617
4a64f543
MS
618/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
619 ADDRESS for the current elements of BP_LOCATION which get a valid
620 result from bp_location_has_shadow. You can use it for roughly
621 limiting the subrange of BP_LOCATION to scan for shadow bytes for
622 an address you need to read. */
876fa593
JK
623
624static CORE_ADDR bp_location_placed_address_before_address_max;
625
4a64f543
MS
626/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
628 BP_LOCATION which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATION to
630 scan for shadow bytes for an address you need to read. */
876fa593
JK
631
632static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 633
4a64f543
MS
634/* The locations that no longer correspond to any breakpoint, unlinked
635 from bp_location array, but for which a hit may still be reported
636 by a target. */
20874c92
VP
637VEC(bp_location_p) *moribund_locations = NULL;
638
c906108c
SS
639/* Number of last breakpoint made. */
640
95a42b64
TT
641static int breakpoint_count;
642
86b17b60
PA
643/* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647static int prev_breakpoint_count;
c906108c 648
1042e4c0
SS
649/* Number of last tracepoint made. */
650
95a42b64 651static int tracepoint_count;
1042e4c0 652
6149aea9
PA
653static struct cmd_list_element *breakpoint_set_cmdlist;
654static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 655struct cmd_list_element *save_cmdlist;
6149aea9 656
badd37ce
SDJ
657/* See declaration at breakpoint.h. */
658
659struct breakpoint *
660breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662{
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672}
673
468d015d
JJ
674/* Return whether a breakpoint is an active enabled breakpoint. */
675static int
676breakpoint_enabled (struct breakpoint *b)
677{
0d381245 678 return (b->enable_state == bp_enabled);
468d015d
JJ
679}
680
c906108c
SS
681/* Set breakpoint count to NUM. */
682
95a42b64 683static void
fba45db2 684set_breakpoint_count (int num)
c906108c 685{
86b17b60 686 prev_breakpoint_count = breakpoint_count;
c906108c 687 breakpoint_count = num;
4fa62494 688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
689}
690
86b17b60
PA
691/* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693static int rbreak_start_breakpoint_count;
694
95a42b64
TT
695/* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
86b17b60 697
95a42b64
TT
698void
699start_rbreak_breakpoints (void)
700{
86b17b60 701 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
702}
703
704/* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
86b17b60 706
95a42b64
TT
707void
708end_rbreak_breakpoints (void)
709{
86b17b60 710 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
711}
712
4a64f543 713/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
714
715void
fba45db2 716clear_breakpoint_hit_counts (void)
c906108c
SS
717{
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722}
723
9add0f1b
TT
724/* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727static struct counted_command_line *
728alloc_counted_command_line (struct command_line *commands)
729{
8d749320 730 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 731
9add0f1b
TT
732 result->refc = 1;
733 result->commands = commands;
8d749320 734
9add0f1b
TT
735 return result;
736}
737
738/* Increment reference count. This does nothing if CMD is NULL. */
739
740static void
741incref_counted_command_line (struct counted_command_line *cmd)
742{
743 if (cmd)
744 ++cmd->refc;
745}
746
747/* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751static void
752decref_counted_command_line (struct counted_command_line **cmdp)
753{
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763}
764
765/* A cleanup function that calls decref_counted_command_line. */
766
767static void
768do_cleanup_counted_command_line (void *arg)
769{
9a3c8263 770 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
771}
772
773/* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776static struct cleanup *
777make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778{
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780}
781
c906108c 782\f
48cb2d85
VP
783/* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786struct breakpoint *
787get_breakpoint (int num)
788{
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796}
5c44784c 797
c906108c 798\f
adc36818 799
b775012e
LM
800/* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803static void
804mark_breakpoint_modified (struct breakpoint *b)
805{
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821}
822
823/* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826static void
827mark_breakpoint_location_modified (struct bp_location *loc)
828{
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842}
843
844/* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847static void
848set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850{
b775012e
LM
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
abf1152a
JK
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
b775012e
LM
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
b775012e
LM
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
44702360 898 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
899 }
900
901 return;
902}
903
904/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907static void
908show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910{
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920}
921
922/* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
924 the more general bp_location_compare function. */
925
926static int
927bp_location_compare_addrs (const void *ap, const void *bp)
928{
9a3c8263
SM
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936}
937
938/* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943static struct bp_location **
944get_first_locp_gte_addr (CORE_ADDR address)
945{
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
951 memset (&dummy_loc, 0, sizeof (struct bp_location));
952 dummy_loc.address = address;
953
954 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
955 locp_found = ((struct bp_location **)
956 bsearch (&dummy_locp, bp_location, bp_location_count,
957 sizeof (struct bp_location **),
958 bp_location_compare_addrs));
b775012e
LM
959
960 /* Nothing was found, nothing left to do. */
961 if (locp_found == NULL)
962 return NULL;
963
964 /* We may have found a location that is at ADDRESS but is not the first in the
965 location's list. Go backwards (if possible) and locate the first one. */
966 while ((locp_found - 1) >= bp_location
967 && (*(locp_found - 1))->address == address)
968 locp_found--;
969
970 return locp_found;
971}
972
adc36818 973void
7a26bd4d 974set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
975 int from_tty)
976{
3a5c3e22
PA
977 xfree (b->cond_string);
978 b->cond_string = NULL;
adc36818 979
3a5c3e22 980 if (is_watchpoint (b))
adc36818 981 {
3a5c3e22
PA
982 struct watchpoint *w = (struct watchpoint *) b;
983
4d01a485 984 w->cond_exp.reset ();
3a5c3e22
PA
985 }
986 else
987 {
988 struct bp_location *loc;
989
990 for (loc = b->loc; loc; loc = loc->next)
991 {
4d01a485 992 loc->cond.reset ();
b775012e
LM
993
994 /* No need to free the condition agent expression
995 bytecode (if we have one). We will handle this
996 when we go through update_global_location_list. */
3a5c3e22 997 }
adc36818 998 }
adc36818
PM
999
1000 if (*exp == 0)
1001 {
1002 if (from_tty)
1003 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1004 }
1005 else
1006 {
bbc13ae3 1007 const char *arg = exp;
cc59ec59 1008
adc36818
PM
1009 /* I don't know if it matters whether this is the string the user
1010 typed in or the decompiled expression. */
1011 b->cond_string = xstrdup (arg);
1012 b->condition_not_parsed = 0;
1013
1014 if (is_watchpoint (b))
1015 {
3a5c3e22
PA
1016 struct watchpoint *w = (struct watchpoint *) b;
1017
adc36818
PM
1018 innermost_block = NULL;
1019 arg = exp;
1bb9788d 1020 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1021 if (*arg)
1022 error (_("Junk at end of expression"));
3a5c3e22 1023 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1024 }
1025 else
1026 {
3a5c3e22
PA
1027 struct bp_location *loc;
1028
adc36818
PM
1029 for (loc = b->loc; loc; loc = loc->next)
1030 {
1031 arg = exp;
1032 loc->cond =
1bb9788d
TT
1033 parse_exp_1 (&arg, loc->address,
1034 block_for_pc (loc->address), 0);
adc36818
PM
1035 if (*arg)
1036 error (_("Junk at end of expression"));
1037 }
1038 }
1039 }
b775012e
LM
1040 mark_breakpoint_modified (b);
1041
8d3788bd 1042 observer_notify_breakpoint_modified (b);
adc36818
PM
1043}
1044
d55637df
TT
1045/* Completion for the "condition" command. */
1046
1047static VEC (char_ptr) *
6f937416
PA
1048condition_completer (struct cmd_list_element *cmd,
1049 const char *text, const char *word)
d55637df 1050{
6f937416 1051 const char *space;
d55637df 1052
6f937416
PA
1053 text = skip_spaces_const (text);
1054 space = skip_to_space_const (text);
d55637df
TT
1055 if (*space == '\0')
1056 {
1057 int len;
1058 struct breakpoint *b;
1059 VEC (char_ptr) *result = NULL;
1060
1061 if (text[0] == '$')
1062 {
1063 /* We don't support completion of history indices. */
1064 if (isdigit (text[1]))
1065 return NULL;
1066 return complete_internalvar (&text[1]);
1067 }
1068
1069 /* We're completing the breakpoint number. */
1070 len = strlen (text);
1071
1072 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1073 {
1074 char number[50];
1075
1076 xsnprintf (number, sizeof (number), "%d", b->number);
1077
1078 if (strncmp (number, text, len) == 0)
1079 VEC_safe_push (char_ptr, result, xstrdup (number));
1080 }
d55637df
TT
1081
1082 return result;
1083 }
1084
1085 /* We're completing the expression part. */
6f937416 1086 text = skip_spaces_const (space);
d55637df
TT
1087 return expression_completer (cmd, text, word);
1088}
1089
c906108c
SS
1090/* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092static void
fba45db2 1093condition_command (char *arg, int from_tty)
c906108c 1094{
52f0bd74 1095 struct breakpoint *b;
c906108c 1096 char *p;
52f0bd74 1097 int bnum;
c906108c
SS
1098
1099 if (arg == 0)
e2e0b3e5 1100 error_no_arg (_("breakpoint number"));
c906108c
SS
1101
1102 p = arg;
1103 bnum = get_number (&p);
5c44784c 1104 if (bnum == 0)
8a3fe4f8 1105 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1106
1107 ALL_BREAKPOINTS (b)
1108 if (b->number == bnum)
2f069f6f 1109 {
6dddc817
DE
1110 /* Check if this breakpoint has a "stop" method implemented in an
1111 extension language. This method and conditions entered into GDB
1112 from the CLI are mutually exclusive. */
1113 const struct extension_language_defn *extlang
1114 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1115
1116 if (extlang != NULL)
1117 {
1118 error (_("Only one stop condition allowed. There is currently"
1119 " a %s stop condition defined for this breakpoint."),
1120 ext_lang_capitalized_name (extlang));
1121 }
2566ad2d 1122 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1123
1124 if (is_breakpoint (b))
44702360 1125 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1126
2f069f6f
JB
1127 return;
1128 }
c906108c 1129
8a3fe4f8 1130 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1131}
1132
a7bdde9e
VP
1133/* Check that COMMAND do not contain commands that are suitable
1134 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1135 Throw if any such commands is found. */
1136
a7bdde9e
VP
1137static void
1138check_no_tracepoint_commands (struct command_line *commands)
1139{
1140 struct command_line *c;
cc59ec59 1141
a7bdde9e
VP
1142 for (c = commands; c; c = c->next)
1143 {
1144 int i;
1145
1146 if (c->control_type == while_stepping_control)
3e43a32a
MS
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
a7bdde9e
VP
1149
1150 for (i = 0; i < c->body_count; ++i)
1151 check_no_tracepoint_commands ((c->body_list)[i]);
1152
1153 /* Not that command parsing removes leading whitespace and comment
4a64f543 1154 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
51661e93
VP
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1161 }
1162}
1163
d77f58be
SS
1164/* Encapsulate tests for different types of tracepoints. */
1165
d9b3f62e
PA
1166static int
1167is_tracepoint_type (enum bptype type)
1168{
1169 return (type == bp_tracepoint
1170 || type == bp_fast_tracepoint
1171 || type == bp_static_tracepoint);
1172}
1173
a7bdde9e 1174int
d77f58be 1175is_tracepoint (const struct breakpoint *b)
a7bdde9e 1176{
d9b3f62e 1177 return is_tracepoint_type (b->type);
a7bdde9e 1178}
d9b3f62e 1179
e5dd4106 1180/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1181 breakpoint. This function will throw an exception if a problem is
1182 found. */
48cb2d85 1183
95a42b64
TT
1184static void
1185validate_commands_for_breakpoint (struct breakpoint *b,
1186 struct command_line *commands)
48cb2d85 1187{
d77f58be 1188 if (is_tracepoint (b))
a7bdde9e 1189 {
c9a6ce02 1190 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1191 struct command_line *c;
1192 struct command_line *while_stepping = 0;
c9a6ce02
PA
1193
1194 /* Reset the while-stepping step count. The previous commands
1195 might have included a while-stepping action, while the new
1196 ones might not. */
1197 t->step_count = 0;
1198
1199 /* We need to verify that each top-level element of commands is
1200 valid for tracepoints, that there's at most one
1201 while-stepping element, and that the while-stepping's body
1202 has valid tracing commands excluding nested while-stepping.
1203 We also need to validate the tracepoint action line in the
1204 context of the tracepoint --- validate_actionline actually
1205 has side effects, like setting the tracepoint's
1206 while-stepping STEP_COUNT, in addition to checking if the
1207 collect/teval actions parse and make sense in the
1208 tracepoint's context. */
a7bdde9e
VP
1209 for (c = commands; c; c = c->next)
1210 {
a7bdde9e
VP
1211 if (c->control_type == while_stepping_control)
1212 {
1213 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for fast tracepoint"));
0fb4aa4b 1216 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1217 error (_("The 'while-stepping' command "
1218 "cannot be used for static tracepoint"));
a7bdde9e
VP
1219
1220 if (while_stepping)
3e43a32a
MS
1221 error (_("The 'while-stepping' command "
1222 "can be used only once"));
a7bdde9e
VP
1223 else
1224 while_stepping = c;
1225 }
c9a6ce02
PA
1226
1227 validate_actionline (c->line, b);
a7bdde9e
VP
1228 }
1229 if (while_stepping)
1230 {
1231 struct command_line *c2;
1232
1233 gdb_assert (while_stepping->body_count == 1);
1234 c2 = while_stepping->body_list[0];
1235 for (; c2; c2 = c2->next)
1236 {
a7bdde9e
VP
1237 if (c2->control_type == while_stepping_control)
1238 error (_("The 'while-stepping' command cannot be nested"));
1239 }
1240 }
1241 }
1242 else
1243 {
1244 check_no_tracepoint_commands (commands);
1245 }
95a42b64
TT
1246}
1247
0fb4aa4b
PA
1248/* Return a vector of all the static tracepoints set at ADDR. The
1249 caller is responsible for releasing the vector. */
1250
1251VEC(breakpoint_p) *
1252static_tracepoints_here (CORE_ADDR addr)
1253{
1254 struct breakpoint *b;
1255 VEC(breakpoint_p) *found = 0;
1256 struct bp_location *loc;
1257
1258 ALL_BREAKPOINTS (b)
1259 if (b->type == bp_static_tracepoint)
1260 {
1261 for (loc = b->loc; loc; loc = loc->next)
1262 if (loc->address == addr)
1263 VEC_safe_push(breakpoint_p, found, b);
1264 }
1265
1266 return found;
1267}
1268
95a42b64 1269/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1270 validate that only allowed commands are included. */
95a42b64
TT
1271
1272void
4a64f543
MS
1273breakpoint_set_commands (struct breakpoint *b,
1274 struct command_line *commands)
95a42b64
TT
1275{
1276 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1277
9add0f1b
TT
1278 decref_counted_command_line (&b->commands);
1279 b->commands = alloc_counted_command_line (commands);
8d3788bd 1280 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1281}
1282
45a43567
TT
1283/* Set the internal `silent' flag on the breakpoint. Note that this
1284 is not the same as the "silent" that may appear in the breakpoint's
1285 commands. */
1286
1287void
1288breakpoint_set_silent (struct breakpoint *b, int silent)
1289{
1290 int old_silent = b->silent;
1291
1292 b->silent = silent;
1293 if (old_silent != silent)
8d3788bd 1294 observer_notify_breakpoint_modified (b);
45a43567
TT
1295}
1296
1297/* Set the thread for this breakpoint. If THREAD is -1, make the
1298 breakpoint work for any thread. */
1299
1300void
1301breakpoint_set_thread (struct breakpoint *b, int thread)
1302{
1303 int old_thread = b->thread;
1304
1305 b->thread = thread;
1306 if (old_thread != thread)
8d3788bd 1307 observer_notify_breakpoint_modified (b);
45a43567
TT
1308}
1309
1310/* Set the task for this breakpoint. If TASK is 0, make the
1311 breakpoint work for any task. */
1312
1313void
1314breakpoint_set_task (struct breakpoint *b, int task)
1315{
1316 int old_task = b->task;
1317
1318 b->task = task;
1319 if (old_task != task)
8d3788bd 1320 observer_notify_breakpoint_modified (b);
45a43567
TT
1321}
1322
95a42b64
TT
1323void
1324check_tracepoint_command (char *line, void *closure)
a7bdde9e 1325{
9a3c8263 1326 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1327
6f937416 1328 validate_actionline (line, b);
a7bdde9e
VP
1329}
1330
95a42b64
TT
1331/* A structure used to pass information through
1332 map_breakpoint_numbers. */
1333
1334struct commands_info
1335{
1336 /* True if the command was typed at a tty. */
1337 int from_tty;
86b17b60
PA
1338
1339 /* The breakpoint range spec. */
896b6bda 1340 const char *arg;
86b17b60 1341
95a42b64
TT
1342 /* Non-NULL if the body of the commands are being read from this
1343 already-parsed command. */
1344 struct command_line *control;
86b17b60 1345
95a42b64
TT
1346 /* The command lines read from the user, or NULL if they have not
1347 yet been read. */
1348 struct counted_command_line *cmd;
1349};
1350
1351/* A callback for map_breakpoint_numbers that sets the commands for
1352 commands_command. */
1353
c906108c 1354static void
95a42b64 1355do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1356{
9a3c8263 1357 struct commands_info *info = (struct commands_info *) data;
c906108c 1358
95a42b64
TT
1359 if (info->cmd == NULL)
1360 {
1361 struct command_line *l;
5c44784c 1362
95a42b64
TT
1363 if (info->control != NULL)
1364 l = copy_command_lines (info->control->body_list[0]);
1365 else
86b17b60
PA
1366 {
1367 struct cleanup *old_chain;
1368 char *str;
c5aa993b 1369
3e43a32a
MS
1370 str = xstrprintf (_("Type commands for breakpoint(s) "
1371 "%s, one per line."),
86b17b60
PA
1372 info->arg);
1373
1374 old_chain = make_cleanup (xfree, str);
1375
1376 l = read_command_lines (str,
1377 info->from_tty, 1,
d77f58be 1378 (is_tracepoint (b)
86b17b60
PA
1379 ? check_tracepoint_command : 0),
1380 b);
1381
1382 do_cleanups (old_chain);
1383 }
a7bdde9e 1384
95a42b64
TT
1385 info->cmd = alloc_counted_command_line (l);
1386 }
1387
1388 /* If a breakpoint was on the list more than once, we don't need to
1389 do anything. */
1390 if (b->commands != info->cmd)
1391 {
1392 validate_commands_for_breakpoint (b, info->cmd->commands);
1393 incref_counted_command_line (info->cmd);
1394 decref_counted_command_line (&b->commands);
1395 b->commands = info->cmd;
8d3788bd 1396 observer_notify_breakpoint_modified (b);
c5aa993b 1397 }
95a42b64
TT
1398}
1399
1400static void
896b6bda 1401commands_command_1 (const char *arg, int from_tty,
4a64f543 1402 struct command_line *control)
95a42b64
TT
1403{
1404 struct cleanup *cleanups;
1405 struct commands_info info;
1406
1407 info.from_tty = from_tty;
1408 info.control = control;
1409 info.cmd = NULL;
1410 /* If we read command lines from the user, then `info' will hold an
1411 extra reference to the commands that we must clean up. */
1412 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1413
896b6bda
PA
1414 std::string new_arg;
1415
95a42b64
TT
1416 if (arg == NULL || !*arg)
1417 {
86b17b60 1418 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1419 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1420 breakpoint_count);
95a42b64 1421 else if (breakpoint_count > 0)
896b6bda 1422 new_arg = string_printf ("%d", breakpoint_count);
95a42b64 1423 }
9766ced4 1424 else
896b6bda 1425 new_arg = arg;
86b17b60 1426
896b6bda 1427 info.arg = new_arg.c_str ();
95a42b64 1428
896b6bda 1429 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
95a42b64
TT
1430
1431 if (info.cmd == NULL)
1432 error (_("No breakpoints specified."));
1433
1434 do_cleanups (cleanups);
1435}
1436
1437static void
1438commands_command (char *arg, int from_tty)
1439{
1440 commands_command_1 (arg, from_tty, NULL);
c906108c 1441}
40c03ae8
EZ
1442
1443/* Like commands_command, but instead of reading the commands from
1444 input stream, takes them from an already parsed command structure.
1445
1446 This is used by cli-script.c to DTRT with breakpoint commands
1447 that are part of if and while bodies. */
1448enum command_control_type
896b6bda 1449commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1450{
95a42b64
TT
1451 commands_command_1 (arg, 0, cmd);
1452 return simple_control;
40c03ae8 1453}
876fa593
JK
1454
1455/* Return non-zero if BL->TARGET_INFO contains valid information. */
1456
1457static int
1458bp_location_has_shadow (struct bp_location *bl)
1459{
1460 if (bl->loc_type != bp_loc_software_breakpoint)
1461 return 0;
1462 if (!bl->inserted)
1463 return 0;
1464 if (bl->target_info.shadow_len == 0)
e5dd4106 1465 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1466 return 0;
1467 return 1;
1468}
1469
9d497a19
PA
1470/* Update BUF, which is LEN bytes read from the target address
1471 MEMADDR, by replacing a memory breakpoint with its shadowed
1472 contents.
1473
1474 If READBUF is not NULL, this buffer must not overlap with the of
1475 the breakpoint location's shadow_contents buffer. Otherwise, a
1476 failed assertion internal error will be raised. */
1477
1478static void
1479one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1480 const gdb_byte *writebuf_org,
1481 ULONGEST memaddr, LONGEST len,
1482 struct bp_target_info *target_info,
1483 struct gdbarch *gdbarch)
1484{
1485 /* Now do full processing of the found relevant range of elements. */
1486 CORE_ADDR bp_addr = 0;
1487 int bp_size = 0;
1488 int bptoffset = 0;
1489
1490 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1491 current_program_space->aspace, 0))
1492 {
1493 /* The breakpoint is inserted in a different address space. */
1494 return;
1495 }
1496
1497 /* Addresses and length of the part of the breakpoint that
1498 we need to copy. */
1499 bp_addr = target_info->placed_address;
1500 bp_size = target_info->shadow_len;
1501
1502 if (bp_addr + bp_size <= memaddr)
1503 {
1504 /* The breakpoint is entirely before the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 if (bp_addr >= memaddr + len)
1510 {
1511 /* The breakpoint is entirely after the chunk of memory we are
1512 reading. */
1513 return;
1514 }
1515
1516 /* Offset within shadow_contents. */
1517 if (bp_addr < memaddr)
1518 {
1519 /* Only copy the second part of the breakpoint. */
1520 bp_size -= memaddr - bp_addr;
1521 bptoffset = memaddr - bp_addr;
1522 bp_addr = memaddr;
1523 }
1524
1525 if (bp_addr + bp_size > memaddr + len)
1526 {
1527 /* Only copy the first part of the breakpoint. */
1528 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1529 }
1530
1531 if (readbuf != NULL)
1532 {
1533 /* Verify that the readbuf buffer does not overlap with the
1534 shadow_contents buffer. */
1535 gdb_assert (target_info->shadow_contents >= readbuf + len
1536 || readbuf >= (target_info->shadow_contents
1537 + target_info->shadow_len));
1538
1539 /* Update the read buffer with this inserted breakpoint's
1540 shadow. */
1541 memcpy (readbuf + bp_addr - memaddr,
1542 target_info->shadow_contents + bptoffset, bp_size);
1543 }
1544 else
1545 {
1546 const unsigned char *bp;
0d5ed153
MR
1547 CORE_ADDR addr = target_info->reqstd_address;
1548 int placed_size;
9d497a19
PA
1549
1550 /* Update the shadow with what we want to write to memory. */
1551 memcpy (target_info->shadow_contents + bptoffset,
1552 writebuf_org + bp_addr - memaddr, bp_size);
1553
1554 /* Determine appropriate breakpoint contents and size for this
1555 address. */
0d5ed153 1556 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1557
1558 /* Update the final write buffer with this inserted
1559 breakpoint's INSN. */
1560 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1561 }
1562}
1563
8defab1a 1564/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1565 by replacing any memory breakpoints with their shadowed contents.
1566
35c63cd8
JB
1567 If READBUF is not NULL, this buffer must not overlap with any of
1568 the breakpoint location's shadow_contents buffers. Otherwise,
1569 a failed assertion internal error will be raised.
1570
876fa593 1571 The range of shadowed area by each bp_location is:
35df4500
TJB
1572 bl->address - bp_location_placed_address_before_address_max
1573 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1574 The range we were requested to resolve shadows for is:
1575 memaddr ... memaddr + len
1576 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1577 memaddr + len <= (bl->address
1578 - bp_location_placed_address_before_address_max)
876fa593 1579 and:
35df4500 1580 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1581
8defab1a 1582void
f0ba3972
PA
1583breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1584 const gdb_byte *writebuf_org,
1585 ULONGEST memaddr, LONGEST len)
c906108c 1586{
4a64f543
MS
1587 /* Left boundary, right boundary and median element of our binary
1588 search. */
876fa593
JK
1589 unsigned bc_l, bc_r, bc;
1590
4a64f543
MS
1591 /* Find BC_L which is a leftmost element which may affect BUF
1592 content. It is safe to report lower value but a failure to
1593 report higher one. */
876fa593
JK
1594
1595 bc_l = 0;
1596 bc_r = bp_location_count;
1597 while (bc_l + 1 < bc_r)
1598 {
35df4500 1599 struct bp_location *bl;
876fa593
JK
1600
1601 bc = (bc_l + bc_r) / 2;
35df4500 1602 bl = bp_location[bc];
876fa593 1603
4a64f543
MS
1604 /* Check first BL->ADDRESS will not overflow due to the added
1605 constant. Then advance the left boundary only if we are sure
1606 the BC element can in no way affect the BUF content (MEMADDR
1607 to MEMADDR + LEN range).
876fa593 1608
4a64f543
MS
1609 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1610 offset so that we cannot miss a breakpoint with its shadow
1611 range tail still reaching MEMADDR. */
c5aa993b 1612
35df4500
TJB
1613 if ((bl->address + bp_location_shadow_len_after_address_max
1614 >= bl->address)
1615 && (bl->address + bp_location_shadow_len_after_address_max
1616 <= memaddr))
876fa593
JK
1617 bc_l = bc;
1618 else
1619 bc_r = bc;
1620 }
1621
128070bb
PA
1622 /* Due to the binary search above, we need to make sure we pick the
1623 first location that's at BC_L's address. E.g., if there are
1624 multiple locations at the same address, BC_L may end up pointing
1625 at a duplicate location, and miss the "master"/"inserted"
1626 location. Say, given locations L1, L2 and L3 at addresses A and
1627 B:
1628
1629 L1@A, L2@A, L3@B, ...
1630
1631 BC_L could end up pointing at location L2, while the "master"
1632 location could be L1. Since the `loc->inserted' flag is only set
1633 on "master" locations, we'd forget to restore the shadow of L1
1634 and L2. */
1635 while (bc_l > 0
1636 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1637 bc_l--;
1638
876fa593
JK
1639 /* Now do full processing of the found relevant range of elements. */
1640
1641 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1642 {
35df4500 1643 struct bp_location *bl = bp_location[bc];
876fa593 1644
35df4500
TJB
1645 /* bp_location array has BL->OWNER always non-NULL. */
1646 if (bl->owner->type == bp_none)
8a3fe4f8 1647 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1648 bl->owner->number);
ffce0d52 1649
e5dd4106 1650 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1651 content. */
1652
35df4500
TJB
1653 if (bl->address >= bp_location_placed_address_before_address_max
1654 && memaddr + len <= (bl->address
1655 - bp_location_placed_address_before_address_max))
876fa593
JK
1656 break;
1657
35df4500 1658 if (!bp_location_has_shadow (bl))
c5aa993b 1659 continue;
6c95b8df 1660
9d497a19
PA
1661 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1662 memaddr, len, &bl->target_info, bl->gdbarch);
1663 }
c906108c 1664}
9d497a19 1665
c906108c 1666\f
c5aa993b 1667
b775012e
LM
1668/* Return true if BPT is either a software breakpoint or a hardware
1669 breakpoint. */
1670
1671int
1672is_breakpoint (const struct breakpoint *bpt)
1673{
1674 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1675 || bpt->type == bp_hardware_breakpoint
1676 || bpt->type == bp_dprintf);
b775012e
LM
1677}
1678
60e1c644
PA
1679/* Return true if BPT is of any hardware watchpoint kind. */
1680
a5606eee 1681static int
d77f58be 1682is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1683{
1684 return (bpt->type == bp_hardware_watchpoint
1685 || bpt->type == bp_read_watchpoint
1686 || bpt->type == bp_access_watchpoint);
1687}
7270d8f2 1688
60e1c644
PA
1689/* Return true if BPT is of any watchpoint kind, hardware or
1690 software. */
1691
3a5c3e22 1692int
d77f58be 1693is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1694{
1695 return (is_hardware_watchpoint (bpt)
1696 || bpt->type == bp_watchpoint);
1697}
1698
3a5c3e22
PA
1699/* Returns true if the current thread and its running state are safe
1700 to evaluate or update watchpoint B. Watchpoints on local
1701 expressions need to be evaluated in the context of the thread that
1702 was current when the watchpoint was created, and, that thread needs
1703 to be stopped to be able to select the correct frame context.
1704 Watchpoints on global expressions can be evaluated on any thread,
1705 and in any state. It is presently left to the target allowing
1706 memory accesses when threads are running. */
f6bc2008
PA
1707
1708static int
3a5c3e22 1709watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1710{
d0d8b0c6
JK
1711 return (b->base.pspace == current_program_space
1712 && (ptid_equal (b->watchpoint_thread, null_ptid)
1713 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1714 && !is_executing (inferior_ptid))));
f6bc2008
PA
1715}
1716
d0fb5eae
JK
1717/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1718 associated bp_watchpoint_scope breakpoint. */
1719
1720static void
3a5c3e22 1721watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1722{
3a5c3e22 1723 struct breakpoint *b = &w->base;
d0fb5eae
JK
1724
1725 if (b->related_breakpoint != b)
1726 {
1727 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1728 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1729 b->related_breakpoint->disposition = disp_del_at_next_stop;
1730 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1731 b->related_breakpoint = b;
1732 }
1733 b->disposition = disp_del_at_next_stop;
1734}
1735
bb9d5f81
PP
1736/* Extract a bitfield value from value VAL using the bit parameters contained in
1737 watchpoint W. */
1738
1739static struct value *
1740extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1741{
1742 struct value *bit_val;
1743
1744 if (val == NULL)
1745 return NULL;
1746
1747 bit_val = allocate_value (value_type (val));
1748
1749 unpack_value_bitfield (bit_val,
1750 w->val_bitpos,
1751 w->val_bitsize,
1752 value_contents_for_printing (val),
1753 value_offset (val),
1754 val);
1755
1756 return bit_val;
1757}
1758
c6d81124
PA
1759/* Allocate a dummy location and add it to B, which must be a software
1760 watchpoint. This is required because even if a software watchpoint
1761 is not watching any memory, bpstat_stop_status requires a location
1762 to be able to report stops. */
1763
1764static void
1765software_watchpoint_add_no_memory_location (struct breakpoint *b,
1766 struct program_space *pspace)
1767{
1768 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1769
1770 b->loc = allocate_bp_location (b);
1771 b->loc->pspace = pspace;
1772 b->loc->address = -1;
1773 b->loc->length = -1;
1774}
1775
1776/* Returns true if B is a software watchpoint that is not watching any
1777 memory (e.g., "watch $pc"). */
1778
1779static int
1780is_no_memory_software_watchpoint (struct breakpoint *b)
1781{
1782 return (b->type == bp_watchpoint
1783 && b->loc != NULL
1784 && b->loc->next == NULL
1785 && b->loc->address == -1
1786 && b->loc->length == -1);
1787}
1788
567e1b4e
JB
1789/* Assuming that B is a watchpoint:
1790 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1791 - Evaluate expression and store the result in B->val
567e1b4e
JB
1792 - Evaluate the condition if there is one, and store the result
1793 in b->loc->cond.
a5606eee
VP
1794 - Update the list of values that must be watched in B->loc.
1795
4a64f543
MS
1796 If the watchpoint disposition is disp_del_at_next_stop, then do
1797 nothing. If this is local watchpoint that is out of scope, delete
1798 it.
1799
1800 Even with `set breakpoint always-inserted on' the watchpoints are
1801 removed + inserted on each stop here. Normal breakpoints must
1802 never be removed because they might be missed by a running thread
1803 when debugging in non-stop mode. On the other hand, hardware
1804 watchpoints (is_hardware_watchpoint; processed here) are specific
1805 to each LWP since they are stored in each LWP's hardware debug
1806 registers. Therefore, such LWP must be stopped first in order to
1807 be able to modify its hardware watchpoints.
1808
1809 Hardware watchpoints must be reset exactly once after being
1810 presented to the user. It cannot be done sooner, because it would
1811 reset the data used to present the watchpoint hit to the user. And
1812 it must not be done later because it could display the same single
1813 watchpoint hit during multiple GDB stops. Note that the latter is
1814 relevant only to the hardware watchpoint types bp_read_watchpoint
1815 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1816 not user-visible - its hit is suppressed if the memory content has
1817 not changed.
1818
1819 The following constraints influence the location where we can reset
1820 hardware watchpoints:
1821
1822 * target_stopped_by_watchpoint and target_stopped_data_address are
1823 called several times when GDB stops.
1824
1825 [linux]
1826 * Multiple hardware watchpoints can be hit at the same time,
1827 causing GDB to stop. GDB only presents one hardware watchpoint
1828 hit at a time as the reason for stopping, and all the other hits
1829 are presented later, one after the other, each time the user
1830 requests the execution to be resumed. Execution is not resumed
1831 for the threads still having pending hit event stored in
1832 LWP_INFO->STATUS. While the watchpoint is already removed from
1833 the inferior on the first stop the thread hit event is kept being
1834 reported from its cached value by linux_nat_stopped_data_address
1835 until the real thread resume happens after the watchpoint gets
1836 presented and thus its LWP_INFO->STATUS gets reset.
1837
1838 Therefore the hardware watchpoint hit can get safely reset on the
1839 watchpoint removal from inferior. */
a79d3c27 1840
b40ce68a 1841static void
3a5c3e22 1842update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1843{
a5606eee 1844 int within_current_scope;
a5606eee 1845 struct frame_id saved_frame_id;
66076460 1846 int frame_saved;
a5606eee 1847
f6bc2008
PA
1848 /* If this is a local watchpoint, we only want to check if the
1849 watchpoint frame is in scope if the current thread is the thread
1850 that was used to create the watchpoint. */
1851 if (!watchpoint_in_thread_scope (b))
1852 return;
1853
3a5c3e22 1854 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1855 return;
1856
66076460 1857 frame_saved = 0;
a5606eee
VP
1858
1859 /* Determine if the watchpoint is within scope. */
1860 if (b->exp_valid_block == NULL)
1861 within_current_scope = 1;
1862 else
1863 {
b5db5dfc
UW
1864 struct frame_info *fi = get_current_frame ();
1865 struct gdbarch *frame_arch = get_frame_arch (fi);
1866 CORE_ADDR frame_pc = get_frame_pc (fi);
1867
c9cf6e20
MG
1868 /* If we're at a point where the stack has been destroyed
1869 (e.g. in a function epilogue), unwinding may not work
1870 properly. Do not attempt to recreate locations at this
b5db5dfc 1871 point. See similar comments in watchpoint_check. */
c9cf6e20 1872 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1873 return;
66076460
DJ
1874
1875 /* Save the current frame's ID so we can restore it after
1876 evaluating the watchpoint expression on its own frame. */
1877 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1878 took a frame parameter, so that we didn't have to change the
1879 selected frame. */
1880 frame_saved = 1;
1881 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1882
a5606eee
VP
1883 fi = frame_find_by_id (b->watchpoint_frame);
1884 within_current_scope = (fi != NULL);
1885 if (within_current_scope)
1886 select_frame (fi);
1887 }
1888
b5db5dfc
UW
1889 /* We don't free locations. They are stored in the bp_location array
1890 and update_global_location_list will eventually delete them and
1891 remove breakpoints if needed. */
3a5c3e22 1892 b->base.loc = NULL;
b5db5dfc 1893
a5606eee
VP
1894 if (within_current_scope && reparse)
1895 {
bbc13ae3 1896 const char *s;
d63d0675 1897
4d01a485 1898 b->exp.reset ();
d63d0675 1899 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1900 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1901 /* If the meaning of expression itself changed, the old value is
1902 no longer relevant. We don't want to report a watchpoint hit
1903 to the user when the old value and the new value may actually
1904 be completely different objects. */
1905 value_free (b->val);
fa4727a6
DJ
1906 b->val = NULL;
1907 b->val_valid = 0;
60e1c644
PA
1908
1909 /* Note that unlike with breakpoints, the watchpoint's condition
1910 expression is stored in the breakpoint object, not in the
1911 locations (re)created below. */
3a5c3e22 1912 if (b->base.cond_string != NULL)
60e1c644 1913 {
4d01a485 1914 b->cond_exp.reset ();
60e1c644 1915
3a5c3e22 1916 s = b->base.cond_string;
1bb9788d 1917 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1918 }
a5606eee 1919 }
a5606eee
VP
1920
1921 /* If we failed to parse the expression, for example because
1922 it refers to a global variable in a not-yet-loaded shared library,
1923 don't try to insert watchpoint. We don't automatically delete
1924 such watchpoint, though, since failure to parse expression
1925 is different from out-of-scope watchpoint. */
e8369a73 1926 if (!target_has_execution)
2d134ed3
PA
1927 {
1928 /* Without execution, memory can't change. No use to try and
1929 set watchpoint locations. The watchpoint will be reset when
1930 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1931 if (!can_use_hw_watchpoints)
1932 {
1933 if (b->base.ops->works_in_software_mode (&b->base))
1934 b->base.type = bp_watchpoint;
1935 else
638aa5a1
AB
1936 error (_("Can't set read/access watchpoint when "
1937 "hardware watchpoints are disabled."));
e8369a73 1938 }
2d134ed3
PA
1939 }
1940 else if (within_current_scope && b->exp)
a5606eee 1941 {
0cf6dd15 1942 int pc = 0;
fa4727a6 1943 struct value *val_chain, *v, *result, *next;
2d134ed3 1944 struct program_space *frame_pspace;
a5606eee 1945
4d01a485 1946 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1947
a5606eee
VP
1948 /* Avoid setting b->val if it's already set. The meaning of
1949 b->val is 'the last value' user saw, and we should update
1950 it only if we reported that last value to user. As it
9c06b0b4
TJB
1951 happens, the code that reports it updates b->val directly.
1952 We don't keep track of the memory value for masked
1953 watchpoints. */
3a5c3e22 1954 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1955 {
bb9d5f81
PP
1956 if (b->val_bitsize != 0)
1957 {
1958 v = extract_bitfield_from_watchpoint_value (b, v);
1959 if (v != NULL)
1960 release_value (v);
1961 }
fa4727a6
DJ
1962 b->val = v;
1963 b->val_valid = 1;
1964 }
a5606eee 1965
2d134ed3
PA
1966 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1967
a5606eee 1968 /* Look at each value on the value chain. */
9fa40276 1969 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1970 {
1971 /* If it's a memory location, and GDB actually needed
1972 its contents to evaluate the expression, then we
fa4727a6
DJ
1973 must watch it. If the first value returned is
1974 still lazy, that means an error occurred reading it;
1975 watch it anyway in case it becomes readable. */
a5606eee 1976 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1977 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1978 {
1979 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1980
a5606eee
VP
1981 /* We only watch structs and arrays if user asked
1982 for it explicitly, never if they just happen to
1983 appear in the middle of some value chain. */
fa4727a6 1984 if (v == result
a5606eee
VP
1985 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1986 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1987 {
1988 CORE_ADDR addr;
f486487f 1989 enum target_hw_bp_type type;
a5606eee 1990 struct bp_location *loc, **tmp;
bb9d5f81
PP
1991 int bitpos = 0, bitsize = 0;
1992
1993 if (value_bitsize (v) != 0)
1994 {
1995 /* Extract the bit parameters out from the bitfield
1996 sub-expression. */
1997 bitpos = value_bitpos (v);
1998 bitsize = value_bitsize (v);
1999 }
2000 else if (v == result && b->val_bitsize != 0)
2001 {
2002 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2003 lvalue whose bit parameters are saved in the fields
2004 VAL_BITPOS and VAL_BITSIZE. */
2005 bitpos = b->val_bitpos;
2006 bitsize = b->val_bitsize;
2007 }
a5606eee 2008
42ae5230 2009 addr = value_address (v);
bb9d5f81
PP
2010 if (bitsize != 0)
2011 {
2012 /* Skip the bytes that don't contain the bitfield. */
2013 addr += bitpos / 8;
2014 }
2015
a5606eee 2016 type = hw_write;
3a5c3e22 2017 if (b->base.type == bp_read_watchpoint)
a5606eee 2018 type = hw_read;
3a5c3e22 2019 else if (b->base.type == bp_access_watchpoint)
a5606eee 2020 type = hw_access;
3a5c3e22
PA
2021
2022 loc = allocate_bp_location (&b->base);
2023 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2024 ;
2025 *tmp = loc;
a6d9a66e 2026 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2027
2028 loc->pspace = frame_pspace;
a5606eee 2029 loc->address = addr;
bb9d5f81
PP
2030
2031 if (bitsize != 0)
2032 {
2033 /* Just cover the bytes that make up the bitfield. */
2034 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2035 }
2036 else
2037 loc->length = TYPE_LENGTH (value_type (v));
2038
a5606eee
VP
2039 loc->watchpoint_type = type;
2040 }
2041 }
9fa40276
TJB
2042 }
2043
2044 /* Change the type of breakpoint between hardware assisted or
2045 an ordinary watchpoint depending on the hardware support
2046 and free hardware slots. REPARSE is set when the inferior
2047 is started. */
a9634178 2048 if (reparse)
9fa40276 2049 {
e09342b5 2050 int reg_cnt;
9fa40276
TJB
2051 enum bp_loc_type loc_type;
2052 struct bp_location *bl;
a5606eee 2053
a9634178 2054 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2055
2056 if (reg_cnt)
9fa40276
TJB
2057 {
2058 int i, target_resources_ok, other_type_used;
a1398e0c 2059 enum bptype type;
9fa40276 2060
a9634178
TJB
2061 /* Use an exact watchpoint when there's only one memory region to be
2062 watched, and only one debug register is needed to watch it. */
2063 b->exact = target_exact_watchpoints && reg_cnt == 1;
2064
9fa40276 2065 /* We need to determine how many resources are already
e09342b5
TJB
2066 used for all other hardware watchpoints plus this one
2067 to see if we still have enough resources to also fit
a1398e0c
PA
2068 this watchpoint in as well. */
2069
2070 /* If this is a software watchpoint, we try to turn it
2071 to a hardware one -- count resources as if B was of
2072 hardware watchpoint type. */
2073 type = b->base.type;
2074 if (type == bp_watchpoint)
2075 type = bp_hardware_watchpoint;
2076
2077 /* This watchpoint may or may not have been placed on
2078 the list yet at this point (it won't be in the list
2079 if we're trying to create it for the first time,
2080 through watch_command), so always account for it
2081 manually. */
2082
2083 /* Count resources used by all watchpoints except B. */
2084 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2085
2086 /* Add in the resources needed for B. */
2087 i += hw_watchpoint_use_count (&b->base);
2088
2089 target_resources_ok
2090 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2091 if (target_resources_ok <= 0)
a9634178 2092 {
3a5c3e22 2093 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2094
2095 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2096 error (_("Target does not support this type of "
2097 "hardware watchpoint."));
9c06b0b4
TJB
2098 else if (target_resources_ok < 0 && !sw_mode)
2099 error (_("There are not enough available hardware "
2100 "resources for this watchpoint."));
a1398e0c
PA
2101
2102 /* Downgrade to software watchpoint. */
2103 b->base.type = bp_watchpoint;
2104 }
2105 else
2106 {
2107 /* If this was a software watchpoint, we've just
2108 found we have enough resources to turn it to a
2109 hardware watchpoint. Otherwise, this is a
2110 nop. */
2111 b->base.type = type;
a9634178 2112 }
9fa40276 2113 }
3a5c3e22 2114 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2115 {
2116 if (!can_use_hw_watchpoints)
2117 error (_("Can't set read/access watchpoint when "
2118 "hardware watchpoints are disabled."));
2119 else
2120 error (_("Expression cannot be implemented with "
2121 "read/access watchpoint."));
2122 }
9fa40276 2123 else
3a5c3e22 2124 b->base.type = bp_watchpoint;
9fa40276 2125
3a5c3e22 2126 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2127 : bp_loc_hardware_watchpoint);
3a5c3e22 2128 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2129 bl->loc_type = loc_type;
2130 }
2131
2132 for (v = val_chain; v; v = next)
2133 {
a5606eee
VP
2134 next = value_next (v);
2135 if (v != b->val)
2136 value_free (v);
2137 }
2138
c7437ca6
PA
2139 /* If a software watchpoint is not watching any memory, then the
2140 above left it without any location set up. But,
2141 bpstat_stop_status requires a location to be able to report
2142 stops, so make sure there's at least a dummy one. */
3a5c3e22 2143 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2144 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2145 }
2146 else if (!within_current_scope)
7270d8f2 2147 {
ac74f770
MS
2148 printf_filtered (_("\
2149Watchpoint %d deleted because the program has left the block\n\
2150in which its expression is valid.\n"),
3a5c3e22 2151 b->base.number);
d0fb5eae 2152 watchpoint_del_at_next_stop (b);
7270d8f2 2153 }
a5606eee
VP
2154
2155 /* Restore the selected frame. */
66076460
DJ
2156 if (frame_saved)
2157 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2158}
2159
a5606eee 2160
74960c60 2161/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2162 inserted in the inferior. We don't differentiate the type of BL's owner
2163 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2164 breakpoint_ops is not defined, because in insert_bp_location,
2165 tracepoint's insert_location will not be called. */
74960c60 2166static int
35df4500 2167should_be_inserted (struct bp_location *bl)
74960c60 2168{
35df4500 2169 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2170 return 0;
2171
35df4500 2172 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2173 return 0;
2174
35df4500 2175 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2176 return 0;
2177
f8eba3c6
TT
2178 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2179 return 0;
2180
56710373
PA
2181 /* This is set for example, when we're attached to the parent of a
2182 vfork, and have detached from the child. The child is running
2183 free, and we expect it to do an exec or exit, at which point the
2184 OS makes the parent schedulable again (and the target reports
2185 that the vfork is done). Until the child is done with the shared
2186 memory region, do not insert breakpoints in the parent, otherwise
2187 the child could still trip on the parent's breakpoints. Since
2188 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2189 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2190 return 0;
2191
31e77af2 2192 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2193 location, except if the breakpoint is a single-step breakpoint,
2194 and the breakpoint's thread is the thread which is stepping past
2195 a breakpoint. */
31e77af2
PA
2196 if ((bl->loc_type == bp_loc_software_breakpoint
2197 || bl->loc_type == bp_loc_hardware_breakpoint)
2198 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2199 bl->address)
2200 /* The single-step breakpoint may be inserted at the location
2201 we're trying to step if the instruction branches to itself.
2202 However, the instruction won't be executed at all and it may
2203 break the semantics of the instruction, for example, the
2204 instruction is a conditional branch or updates some flags.
2205 We can't fix it unless GDB is able to emulate the instruction
2206 or switch to displaced stepping. */
2207 && !(bl->owner->type == bp_single_step
2208 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2209 {
2210 if (debug_infrun)
2211 {
2212 fprintf_unfiltered (gdb_stdlog,
2213 "infrun: skipping breakpoint: "
2214 "stepping past insn at: %s\n",
2215 paddress (bl->gdbarch, bl->address));
2216 }
2217 return 0;
2218 }
31e77af2 2219
963f9c80
PA
2220 /* Don't insert watchpoints if we're trying to step past the
2221 instruction that triggered one. */
2222 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2223 && stepping_past_nonsteppable_watchpoint ())
2224 {
2225 if (debug_infrun)
2226 {
2227 fprintf_unfiltered (gdb_stdlog,
2228 "infrun: stepping past non-steppable watchpoint. "
2229 "skipping watchpoint at %s:%d\n",
2230 paddress (bl->gdbarch, bl->address),
2231 bl->length);
2232 }
2233 return 0;
2234 }
2235
74960c60
VP
2236 return 1;
2237}
2238
934709f0
PW
2239/* Same as should_be_inserted but does the check assuming
2240 that the location is not duplicated. */
2241
2242static int
2243unduplicated_should_be_inserted (struct bp_location *bl)
2244{
2245 int result;
2246 const int save_duplicate = bl->duplicate;
2247
2248 bl->duplicate = 0;
2249 result = should_be_inserted (bl);
2250 bl->duplicate = save_duplicate;
2251 return result;
2252}
2253
b775012e
LM
2254/* Parses a conditional described by an expression COND into an
2255 agent expression bytecode suitable for evaluation
2256 by the bytecode interpreter. Return NULL if there was
2257 any error during parsing. */
2258
833177a4 2259static agent_expr_up
b775012e
LM
2260parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2261{
833177a4 2262 if (cond == NULL)
b775012e
LM
2263 return NULL;
2264
833177a4
PA
2265 agent_expr_up aexpr;
2266
b775012e
LM
2267 /* We don't want to stop processing, so catch any errors
2268 that may show up. */
492d29ea 2269 TRY
b775012e 2270 {
6c73cd95 2271 aexpr = std::move (gen_eval_for_expr (scope, cond));
b775012e
LM
2272 }
2273
492d29ea 2274 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2275 {
2276 /* If we got here, it means the condition could not be parsed to a valid
2277 bytecode expression and thus can't be evaluated on the target's side.
2278 It's no use iterating through the conditions. */
b775012e 2279 }
492d29ea 2280 END_CATCH
b775012e
LM
2281
2282 /* We have a valid agent expression. */
2283 return aexpr;
2284}
2285
2286/* Based on location BL, create a list of breakpoint conditions to be
2287 passed on to the target. If we have duplicated locations with different
2288 conditions, we will add such conditions to the list. The idea is that the
2289 target will evaluate the list of conditions and will only notify GDB when
2290 one of them is true. */
2291
2292static void
2293build_target_condition_list (struct bp_location *bl)
2294{
2295 struct bp_location **locp = NULL, **loc2p;
2296 int null_condition_or_parse_error = 0;
2297 int modified = bl->needs_update;
2298 struct bp_location *loc;
2299
8b4f3082 2300 /* Release conditions left over from a previous insert. */
3cde5c42 2301 bl->target_info.conditions.clear ();
8b4f3082 2302
b775012e
LM
2303 /* This is only meaningful if the target is
2304 evaluating conditions and if the user has
2305 opted for condition evaluation on the target's
2306 side. */
2307 if (gdb_evaluates_breakpoint_condition_p ()
2308 || !target_supports_evaluation_of_breakpoint_conditions ())
2309 return;
2310
2311 /* Do a first pass to check for locations with no assigned
2312 conditions or conditions that fail to parse to a valid agent expression
2313 bytecode. If any of these happen, then it's no use to send conditions
2314 to the target since this location will always trigger and generate a
2315 response back to GDB. */
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2320 {
2321 if (modified)
2322 {
b775012e
LM
2323 /* Re-parse the conditions since something changed. In that
2324 case we already freed the condition bytecodes (see
2325 force_breakpoint_reinsertion). We just
2326 need to parse the condition to bytecodes again. */
833177a4
PA
2327 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2328 loc->cond.get ());
b775012e
LM
2329 }
2330
2331 /* If we have a NULL bytecode expression, it means something
2332 went wrong or we have a null condition expression. */
2333 if (!loc->cond_bytecode)
2334 {
2335 null_condition_or_parse_error = 1;
2336 break;
2337 }
2338 }
2339 }
2340
2341 /* If any of these happened, it means we will have to evaluate the conditions
2342 for the location's address on gdb's side. It is no use keeping bytecodes
2343 for all the other duplicate locations, thus we free all of them here.
2344
2345 This is so we have a finer control over which locations' conditions are
2346 being evaluated by GDB or the remote stub. */
2347 if (null_condition_or_parse_error)
2348 {
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2353 {
2354 /* Only go as far as the first NULL bytecode is
2355 located. */
2356 if (!loc->cond_bytecode)
2357 return;
2358
833177a4 2359 loc->cond_bytecode.reset ();
b775012e
LM
2360 }
2361 }
2362 }
2363
2364 /* No NULL conditions or failed bytecode generation. Build a condition list
2365 for this location's address. */
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (loc->cond
2370 && is_breakpoint (loc->owner)
2371 && loc->pspace->num == bl->pspace->num
2372 && loc->owner->enable_state == bp_enabled
2373 && loc->enabled)
3cde5c42
PA
2374 {
2375 /* Add the condition to the vector. This will be used later
2376 to send the conditions to the target. */
2377 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2378 }
b775012e
LM
2379 }
2380
2381 return;
2382}
2383
d3ce09f5
SS
2384/* Parses a command described by string CMD into an agent expression
2385 bytecode suitable for evaluation by the bytecode interpreter.
2386 Return NULL if there was any error during parsing. */
2387
833177a4 2388static agent_expr_up
d3ce09f5
SS
2389parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2390{
2391 struct cleanup *old_cleanups = 0;
4d01a485 2392 struct expression **argvec;
bbc13ae3
KS
2393 const char *cmdrest;
2394 const char *format_start, *format_end;
d3ce09f5
SS
2395 struct format_piece *fpieces;
2396 int nargs;
2397 struct gdbarch *gdbarch = get_current_arch ();
2398
833177a4 2399 if (cmd == NULL)
d3ce09f5
SS
2400 return NULL;
2401
2402 cmdrest = cmd;
2403
2404 if (*cmdrest == ',')
2405 ++cmdrest;
bbc13ae3 2406 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2407
2408 if (*cmdrest++ != '"')
2409 error (_("No format string following the location"));
2410
2411 format_start = cmdrest;
2412
2413 fpieces = parse_format_string (&cmdrest);
2414
2415 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2416
2417 format_end = cmdrest;
2418
2419 if (*cmdrest++ != '"')
2420 error (_("Bad format string, non-terminated '\"'."));
2421
bbc13ae3 2422 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2423
2424 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2425 error (_("Invalid argument syntax"));
2426
2427 if (*cmdrest == ',')
2428 cmdrest++;
bbc13ae3 2429 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2430
2431 /* For each argument, make an expression. */
2432
2433 argvec = (struct expression **) alloca (strlen (cmd)
2434 * sizeof (struct expression *));
2435
2436 nargs = 0;
2437 while (*cmdrest != '\0')
2438 {
bbc13ae3 2439 const char *cmd1;
d3ce09f5
SS
2440
2441 cmd1 = cmdrest;
4d01a485
PA
2442 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2443 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2444 cmdrest = cmd1;
2445 if (*cmdrest == ',')
2446 ++cmdrest;
2447 }
2448
833177a4
PA
2449 agent_expr_up aexpr;
2450
d3ce09f5
SS
2451 /* We don't want to stop processing, so catch any errors
2452 that may show up. */
492d29ea 2453 TRY
d3ce09f5 2454 {
6c73cd95 2455 aexpr = std::move (gen_printf (scope, gdbarch, 0, 0,
833177a4
PA
2456 format_start, format_end - format_start,
2457 fpieces, nargs, argvec));
d3ce09f5 2458 }
492d29ea 2459 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2460 {
2461 /* If we got here, it means the command could not be parsed to a valid
2462 bytecode expression and thus can't be evaluated on the target's side.
2463 It's no use iterating through the other commands. */
d3ce09f5 2464 }
492d29ea
PA
2465 END_CATCH
2466
2467 do_cleanups (old_cleanups);
d3ce09f5 2468
d3ce09f5
SS
2469 /* We have a valid agent expression, return it. */
2470 return aexpr;
2471}
2472
2473/* Based on location BL, create a list of breakpoint commands to be
2474 passed on to the target. If we have duplicated locations with
2475 different commands, we will add any such to the list. */
2476
2477static void
2478build_target_command_list (struct bp_location *bl)
2479{
2480 struct bp_location **locp = NULL, **loc2p;
2481 int null_command_or_parse_error = 0;
2482 int modified = bl->needs_update;
2483 struct bp_location *loc;
2484
3cde5c42
PA
2485 /* Clear commands left over from a previous insert. */
2486 bl->target_info.tcommands.clear ();
8b4f3082 2487
41fac0cf 2488 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2489 return;
2490
41fac0cf
PA
2491 /* For now, limit to agent-style dprintf breakpoints. */
2492 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2493 return;
2494
41fac0cf
PA
2495 /* For now, if we have any duplicate location that isn't a dprintf,
2496 don't install the target-side commands, as that would make the
2497 breakpoint not be reported to the core, and we'd lose
2498 control. */
2499 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2500 {
2501 loc = (*loc2p);
2502 if (is_breakpoint (loc->owner)
2503 && loc->pspace->num == bl->pspace->num
2504 && loc->owner->type != bp_dprintf)
2505 return;
2506 }
2507
d3ce09f5
SS
2508 /* Do a first pass to check for locations with no assigned
2509 conditions or conditions that fail to parse to a valid agent expression
2510 bytecode. If any of these happen, then it's no use to send conditions
2511 to the target since this location will always trigger and generate a
2512 response back to GDB. */
2513 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2514 {
2515 loc = (*loc2p);
2516 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2517 {
2518 if (modified)
2519 {
d3ce09f5
SS
2520 /* Re-parse the commands since something changed. In that
2521 case we already freed the command bytecodes (see
2522 force_breakpoint_reinsertion). We just
2523 need to parse the command to bytecodes again. */
833177a4
PA
2524 loc->cmd_bytecode
2525 = parse_cmd_to_aexpr (bl->address,
2526 loc->owner->extra_string);
d3ce09f5
SS
2527 }
2528
2529 /* If we have a NULL bytecode expression, it means something
2530 went wrong or we have a null command expression. */
2531 if (!loc->cmd_bytecode)
2532 {
2533 null_command_or_parse_error = 1;
2534 break;
2535 }
2536 }
2537 }
2538
2539 /* If anything failed, then we're not doing target-side commands,
2540 and so clean up. */
2541 if (null_command_or_parse_error)
2542 {
2543 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2544 {
2545 loc = (*loc2p);
2546 if (is_breakpoint (loc->owner)
2547 && loc->pspace->num == bl->pspace->num)
2548 {
2549 /* Only go as far as the first NULL bytecode is
2550 located. */
40fb6c5e 2551 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2552 return;
2553
833177a4 2554 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2555 }
2556 }
2557 }
2558
2559 /* No NULL commands or failed bytecode generation. Build a command list
2560 for this location's address. */
2561 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2562 {
2563 loc = (*loc2p);
2564 if (loc->owner->extra_string
2565 && is_breakpoint (loc->owner)
2566 && loc->pspace->num == bl->pspace->num
2567 && loc->owner->enable_state == bp_enabled
2568 && loc->enabled)
3cde5c42
PA
2569 {
2570 /* Add the command to the vector. This will be used later
2571 to send the commands to the target. */
2572 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2573 }
d3ce09f5
SS
2574 }
2575
2576 bl->target_info.persist = 0;
2577 /* Maybe flag this location as persistent. */
2578 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2579 bl->target_info.persist = 1;
2580}
2581
833b7ab5
YQ
2582/* Return the kind of breakpoint on address *ADDR. Get the kind
2583 of breakpoint according to ADDR except single-step breakpoint.
2584 Get the kind of single-step breakpoint according to the current
2585 registers state. */
cd6c3b4f
YQ
2586
2587static int
2588breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2589{
833b7ab5
YQ
2590 if (bl->owner->type == bp_single_step)
2591 {
2592 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2593 struct regcache *regcache;
2594
2595 regcache = get_thread_regcache (thr->ptid);
2596
2597 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2598 regcache, addr);
2599 }
2600 else
2601 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2602}
2603
35df4500
TJB
2604/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2605 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2606 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2607 Returns 0 for success, 1 if the bp_location type is not supported or
2608 -1 for failure.
879bfdc2 2609
4a64f543
MS
2610 NOTE drow/2003-09-09: This routine could be broken down to an
2611 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2612static int
35df4500 2613insert_bp_location (struct bp_location *bl,
26bb91f3 2614 struct ui_file *tmp_error_stream,
3fbb6ffa 2615 int *disabled_breaks,
dd61ec5c
MW
2616 int *hw_breakpoint_error,
2617 int *hw_bp_error_explained_already)
879bfdc2 2618{
0000e5cc
PA
2619 enum errors bp_err = GDB_NO_ERROR;
2620 const char *bp_err_message = NULL;
879bfdc2 2621
b775012e 2622 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2623 return 0;
2624
35c63cd8
JB
2625 /* Note we don't initialize bl->target_info, as that wipes out
2626 the breakpoint location's shadow_contents if the breakpoint
2627 is still inserted at that location. This in turn breaks
2628 target_read_memory which depends on these buffers when
2629 a memory read is requested at the breakpoint location:
2630 Once the target_info has been wiped, we fail to see that
2631 we have a breakpoint inserted at that address and thus
2632 read the breakpoint instead of returning the data saved in
2633 the breakpoint location's shadow contents. */
0d5ed153 2634 bl->target_info.reqstd_address = bl->address;
35df4500 2635 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2636 bl->target_info.length = bl->length;
8181d85f 2637
b775012e
LM
2638 /* When working with target-side conditions, we must pass all the conditions
2639 for the same breakpoint address down to the target since GDB will not
2640 insert those locations. With a list of breakpoint conditions, the target
2641 can decide when to stop and notify GDB. */
2642
2643 if (is_breakpoint (bl->owner))
2644 {
2645 build_target_condition_list (bl);
d3ce09f5
SS
2646 build_target_command_list (bl);
2647 /* Reset the modification marker. */
b775012e
LM
2648 bl->needs_update = 0;
2649 }
2650
35df4500
TJB
2651 if (bl->loc_type == bp_loc_software_breakpoint
2652 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2653 {
35df4500 2654 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2655 {
2656 /* If the explicitly specified breakpoint type
2657 is not hardware breakpoint, check the memory map to see
2658 if the breakpoint address is in read only memory or not.
4a64f543 2659
765dc015
VP
2660 Two important cases are:
2661 - location type is not hardware breakpoint, memory
2662 is readonly. We change the type of the location to
2663 hardware breakpoint.
4a64f543
MS
2664 - location type is hardware breakpoint, memory is
2665 read-write. This means we've previously made the
2666 location hardware one, but then the memory map changed,
2667 so we undo.
765dc015 2668
4a64f543
MS
2669 When breakpoints are removed, remove_breakpoints will use
2670 location types we've just set here, the only possible
2671 problem is that memory map has changed during running
2672 program, but it's not going to work anyway with current
2673 gdb. */
765dc015 2674 struct mem_region *mr
0d5ed153 2675 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2676
2677 if (mr)
2678 {
2679 if (automatic_hardware_breakpoints)
2680 {
765dc015
VP
2681 enum bp_loc_type new_type;
2682
2683 if (mr->attrib.mode != MEM_RW)
2684 new_type = bp_loc_hardware_breakpoint;
2685 else
2686 new_type = bp_loc_software_breakpoint;
2687
35df4500 2688 if (new_type != bl->loc_type)
765dc015
VP
2689 {
2690 static int said = 0;
cc59ec59 2691
35df4500 2692 bl->loc_type = new_type;
765dc015
VP
2693 if (!said)
2694 {
3e43a32a
MS
2695 fprintf_filtered (gdb_stdout,
2696 _("Note: automatically using "
2697 "hardware breakpoints for "
2698 "read-only addresses.\n"));
765dc015
VP
2699 said = 1;
2700 }
2701 }
2702 }
35df4500 2703 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2704 && mr->attrib.mode != MEM_RW)
2705 {
2706 fprintf_unfiltered (tmp_error_stream,
2707 _("Cannot insert breakpoint %d.\n"
2708 "Cannot set software breakpoint "
2709 "at read-only address %s\n"),
2710 bl->owner->number,
2711 paddress (bl->gdbarch, bl->address));
2712 return 1;
2713 }
765dc015
VP
2714 }
2715 }
2716
879bfdc2
DJ
2717 /* First check to see if we have to handle an overlay. */
2718 if (overlay_debugging == ovly_off
35df4500
TJB
2719 || bl->section == NULL
2720 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2721 {
2722 /* No overlay handling: just set the breakpoint. */
492d29ea 2723 TRY
dd61ec5c 2724 {
0000e5cc
PA
2725 int val;
2726
dd61ec5c 2727 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2728 if (val)
2729 bp_err = GENERIC_ERROR;
dd61ec5c 2730 }
492d29ea 2731 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2732 {
0000e5cc
PA
2733 bp_err = e.error;
2734 bp_err_message = e.message;
dd61ec5c 2735 }
492d29ea 2736 END_CATCH
879bfdc2
DJ
2737 }
2738 else
2739 {
4a64f543 2740 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2741 Shall we set a breakpoint at the LMA? */
2742 if (!overlay_events_enabled)
2743 {
2744 /* Yes -- overlay event support is not active,
2745 so we must try to set a breakpoint at the LMA.
2746 This will not work for a hardware breakpoint. */
35df4500 2747 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2748 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2749 bl->owner->number);
879bfdc2
DJ
2750 else
2751 {
35df4500
TJB
2752 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2753 bl->section);
879bfdc2 2754 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2755 bl->overlay_target_info = bl->target_info;
0d5ed153 2756 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2757
2758 /* No overlay handling: just set the breakpoint. */
492d29ea 2759 TRY
0000e5cc
PA
2760 {
2761 int val;
2762
579c6ad9 2763 bl->overlay_target_info.kind
cd6c3b4f
YQ
2764 = breakpoint_kind (bl, &addr);
2765 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2766 val = target_insert_breakpoint (bl->gdbarch,
2767 &bl->overlay_target_info);
2768 if (val)
2769 bp_err = GENERIC_ERROR;
2770 }
492d29ea 2771 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2772 {
2773 bp_err = e.error;
2774 bp_err_message = e.message;
2775 }
492d29ea 2776 END_CATCH
0000e5cc
PA
2777
2778 if (bp_err != GDB_NO_ERROR)
99361f52 2779 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2780 "Overlay breakpoint %d "
2781 "failed: in ROM?\n",
35df4500 2782 bl->owner->number);
879bfdc2
DJ
2783 }
2784 }
2785 /* Shall we set a breakpoint at the VMA? */
35df4500 2786 if (section_is_mapped (bl->section))
879bfdc2
DJ
2787 {
2788 /* Yes. This overlay section is mapped into memory. */
492d29ea 2789 TRY
dd61ec5c 2790 {
0000e5cc
PA
2791 int val;
2792
dd61ec5c 2793 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2794 if (val)
2795 bp_err = GENERIC_ERROR;
dd61ec5c 2796 }
492d29ea 2797 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2798 {
0000e5cc
PA
2799 bp_err = e.error;
2800 bp_err_message = e.message;
dd61ec5c 2801 }
492d29ea 2802 END_CATCH
879bfdc2
DJ
2803 }
2804 else
2805 {
2806 /* No. This breakpoint will not be inserted.
2807 No error, but do not mark the bp as 'inserted'. */
2808 return 0;
2809 }
2810 }
2811
0000e5cc 2812 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2813 {
2814 /* Can't set the breakpoint. */
0000e5cc
PA
2815
2816 /* In some cases, we might not be able to insert a
2817 breakpoint in a shared library that has already been
2818 removed, but we have not yet processed the shlib unload
2819 event. Unfortunately, some targets that implement
076855f9
PA
2820 breakpoint insertion themselves can't tell why the
2821 breakpoint insertion failed (e.g., the remote target
2822 doesn't define error codes), so we must treat generic
2823 errors as memory errors. */
0000e5cc 2824 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2825 && bl->loc_type == bp_loc_software_breakpoint
08351840 2826 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2827 || shared_objfile_contains_address_p (bl->pspace,
2828 bl->address)))
879bfdc2 2829 {
4a64f543 2830 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2831 bl->shlib_disabled = 1;
8d3788bd 2832 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2833 if (!*disabled_breaks)
2834 {
2835 fprintf_unfiltered (tmp_error_stream,
2836 "Cannot insert breakpoint %d.\n",
2837 bl->owner->number);
2838 fprintf_unfiltered (tmp_error_stream,
2839 "Temporarily disabling shared "
2840 "library breakpoints:\n");
2841 }
2842 *disabled_breaks = 1;
879bfdc2 2843 fprintf_unfiltered (tmp_error_stream,
35df4500 2844 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2845 return 0;
879bfdc2
DJ
2846 }
2847 else
879bfdc2 2848 {
35df4500 2849 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2850 {
0000e5cc
PA
2851 *hw_breakpoint_error = 1;
2852 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2853 fprintf_unfiltered (tmp_error_stream,
2854 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2855 bl->owner->number, bp_err_message ? ":" : ".\n");
2856 if (bp_err_message != NULL)
2857 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2858 }
2859 else
2860 {
0000e5cc
PA
2861 if (bp_err_message == NULL)
2862 {
2863 char *message
2864 = memory_error_message (TARGET_XFER_E_IO,
2865 bl->gdbarch, bl->address);
2866 struct cleanup *old_chain = make_cleanup (xfree, message);
2867
2868 fprintf_unfiltered (tmp_error_stream,
2869 "Cannot insert breakpoint %d.\n"
2870 "%s\n",
2871 bl->owner->number, message);
2872 do_cleanups (old_chain);
2873 }
2874 else
2875 {
2876 fprintf_unfiltered (tmp_error_stream,
2877 "Cannot insert breakpoint %d: %s\n",
2878 bl->owner->number,
2879 bp_err_message);
2880 }
879bfdc2 2881 }
0000e5cc 2882 return 1;
879bfdc2
DJ
2883
2884 }
2885 }
2886 else
35df4500 2887 bl->inserted = 1;
879bfdc2 2888
0000e5cc 2889 return 0;
879bfdc2
DJ
2890 }
2891
35df4500 2892 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2893 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2894 watchpoints. It's not clear that it's necessary... */
35df4500 2895 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2896 {
0000e5cc
PA
2897 int val;
2898
77b06cd7
TJB
2899 gdb_assert (bl->owner->ops != NULL
2900 && bl->owner->ops->insert_location != NULL);
2901
2902 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2903
2904 /* If trying to set a read-watchpoint, and it turns out it's not
2905 supported, try emulating one with an access watchpoint. */
35df4500 2906 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2907 {
2908 struct bp_location *loc, **loc_temp;
2909
2910 /* But don't try to insert it, if there's already another
2911 hw_access location that would be considered a duplicate
2912 of this one. */
2913 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2914 if (loc != bl
85d721b8 2915 && loc->watchpoint_type == hw_access
35df4500 2916 && watchpoint_locations_match (bl, loc))
85d721b8 2917 {
35df4500
TJB
2918 bl->duplicate = 1;
2919 bl->inserted = 1;
2920 bl->target_info = loc->target_info;
2921 bl->watchpoint_type = hw_access;
85d721b8
PA
2922 val = 0;
2923 break;
2924 }
2925
2926 if (val == 1)
2927 {
77b06cd7
TJB
2928 bl->watchpoint_type = hw_access;
2929 val = bl->owner->ops->insert_location (bl);
2930
2931 if (val)
2932 /* Back to the original value. */
2933 bl->watchpoint_type = hw_read;
85d721b8
PA
2934 }
2935 }
2936
35df4500 2937 bl->inserted = (val == 0);
879bfdc2
DJ
2938 }
2939
35df4500 2940 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2941 {
0000e5cc
PA
2942 int val;
2943
77b06cd7
TJB
2944 gdb_assert (bl->owner->ops != NULL
2945 && bl->owner->ops->insert_location != NULL);
2946
2947 val = bl->owner->ops->insert_location (bl);
2948 if (val)
2949 {
2950 bl->owner->enable_state = bp_disabled;
2951
2952 if (val == 1)
2953 warning (_("\
2954Error inserting catchpoint %d: Your system does not support this type\n\
2955of catchpoint."), bl->owner->number);
2956 else
2957 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2958 }
2959
2960 bl->inserted = (val == 0);
1640b821
DJ
2961
2962 /* We've already printed an error message if there was a problem
2963 inserting this catchpoint, and we've disabled the catchpoint,
2964 so just return success. */
2965 return 0;
879bfdc2
DJ
2966 }
2967
2968 return 0;
2969}
2970
6c95b8df
PA
2971/* This function is called when program space PSPACE is about to be
2972 deleted. It takes care of updating breakpoints to not reference
2973 PSPACE anymore. */
2974
2975void
2976breakpoint_program_space_exit (struct program_space *pspace)
2977{
2978 struct breakpoint *b, *b_temp;
876fa593 2979 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2980
2981 /* Remove any breakpoint that was set through this program space. */
2982 ALL_BREAKPOINTS_SAFE (b, b_temp)
2983 {
2984 if (b->pspace == pspace)
2985 delete_breakpoint (b);
2986 }
2987
2988 /* Breakpoints set through other program spaces could have locations
2989 bound to PSPACE as well. Remove those. */
876fa593 2990 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2991 {
2992 struct bp_location *tmp;
2993
2994 if (loc->pspace == pspace)
2995 {
2bdf28a0 2996 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2997 if (loc->owner->loc == loc)
2998 loc->owner->loc = loc->next;
2999 else
3000 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3001 if (tmp->next == loc)
3002 {
3003 tmp->next = loc->next;
3004 break;
3005 }
3006 }
3007 }
3008
3009 /* Now update the global location list to permanently delete the
3010 removed locations above. */
44702360 3011 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3012}
3013
74960c60
VP
3014/* Make sure all breakpoints are inserted in inferior.
3015 Throws exception on any error.
3016 A breakpoint that is already inserted won't be inserted
3017 again, so calling this function twice is safe. */
3018void
3019insert_breakpoints (void)
3020{
3021 struct breakpoint *bpt;
3022
3023 ALL_BREAKPOINTS (bpt)
3024 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3025 {
3026 struct watchpoint *w = (struct watchpoint *) bpt;
3027
3028 update_watchpoint (w, 0 /* don't reparse. */);
3029 }
74960c60 3030
04086b45
PA
3031 /* Updating watchpoints creates new locations, so update the global
3032 location list. Explicitly tell ugll to insert locations and
3033 ignore breakpoints_always_inserted_mode. */
3034 update_global_location_list (UGLL_INSERT);
74960c60
VP
3035}
3036
20388dd6
YQ
3037/* Invoke CALLBACK for each of bp_location. */
3038
3039void
3040iterate_over_bp_locations (walk_bp_location_callback callback)
3041{
3042 struct bp_location *loc, **loc_tmp;
3043
3044 ALL_BP_LOCATIONS (loc, loc_tmp)
3045 {
3046 callback (loc, NULL);
3047 }
3048}
3049
b775012e
LM
3050/* This is used when we need to synch breakpoint conditions between GDB and the
3051 target. It is the case with deleting and disabling of breakpoints when using
3052 always-inserted mode. */
3053
3054static void
3055update_inserted_breakpoint_locations (void)
3056{
3057 struct bp_location *bl, **blp_tmp;
3058 int error_flag = 0;
3059 int val = 0;
3060 int disabled_breaks = 0;
3061 int hw_breakpoint_error = 0;
dd61ec5c 3062 int hw_bp_details_reported = 0;
b775012e
LM
3063
3064 struct ui_file *tmp_error_stream = mem_fileopen ();
3065 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3066
3067 /* Explicitly mark the warning -- this will only be printed if
3068 there was an error. */
3069 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3070
3071 save_current_space_and_thread ();
3072
3073 ALL_BP_LOCATIONS (bl, blp_tmp)
3074 {
3075 /* We only want to update software breakpoints and hardware
3076 breakpoints. */
3077 if (!is_breakpoint (bl->owner))
3078 continue;
3079
3080 /* We only want to update locations that are already inserted
3081 and need updating. This is to avoid unwanted insertion during
3082 deletion of breakpoints. */
3083 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3084 continue;
3085
3086 switch_to_program_space_and_thread (bl->pspace);
3087
3088 /* For targets that support global breakpoints, there's no need
3089 to select an inferior to insert breakpoint to. In fact, even
3090 if we aren't attached to any process yet, we should still
3091 insert breakpoints. */
f5656ead 3092 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3093 && ptid_equal (inferior_ptid, null_ptid))
3094 continue;
3095
3096 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3097 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3098 if (val)
3099 error_flag = val;
3100 }
3101
3102 if (error_flag)
3103 {
3104 target_terminal_ours_for_output ();
3105 error_stream (tmp_error_stream);
3106 }
3107
3108 do_cleanups (cleanups);
3109}
3110
c30eee59 3111/* Used when starting or continuing the program. */
c906108c 3112
74960c60
VP
3113static void
3114insert_breakpoint_locations (void)
c906108c 3115{
a5606eee 3116 struct breakpoint *bpt;
35df4500 3117 struct bp_location *bl, **blp_tmp;
eacd795a 3118 int error_flag = 0;
c906108c 3119 int val = 0;
3fbb6ffa 3120 int disabled_breaks = 0;
81d0cc19 3121 int hw_breakpoint_error = 0;
dd61ec5c 3122 int hw_bp_error_explained_already = 0;
c906108c 3123
81d0cc19 3124 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3125 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3126
81d0cc19
GS
3127 /* Explicitly mark the warning -- this will only be printed if
3128 there was an error. */
3129 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3130
3131 save_current_space_and_thread ();
3132
35df4500 3133 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3134 {
b775012e 3135 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3136 continue;
3137
4a64f543
MS
3138 /* There is no point inserting thread-specific breakpoints if
3139 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3140 has BL->OWNER always non-NULL. */
35df4500 3141 if (bl->owner->thread != -1
5d5658a1 3142 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3143 continue;
3144
35df4500 3145 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3146
3147 /* For targets that support global breakpoints, there's no need
3148 to select an inferior to insert breakpoint to. In fact, even
3149 if we aren't attached to any process yet, we should still
3150 insert breakpoints. */
f5656ead 3151 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3152 && ptid_equal (inferior_ptid, null_ptid))
3153 continue;
3154
3fbb6ffa 3155 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3156 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3157 if (val)
eacd795a 3158 error_flag = val;
879bfdc2 3159 }
c906108c 3160
4a64f543
MS
3161 /* If we failed to insert all locations of a watchpoint, remove
3162 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3163 ALL_BREAKPOINTS (bpt)
3164 {
3165 int some_failed = 0;
3166 struct bp_location *loc;
3167
3168 if (!is_hardware_watchpoint (bpt))
3169 continue;
3170
d6b74ac4 3171 if (!breakpoint_enabled (bpt))
a5606eee 3172 continue;
74960c60
VP
3173
3174 if (bpt->disposition == disp_del_at_next_stop)
3175 continue;
a5606eee
VP
3176
3177 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3178 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3179 {
3180 some_failed = 1;
3181 break;
3182 }
3183 if (some_failed)
3184 {
3185 for (loc = bpt->loc; loc; loc = loc->next)
3186 if (loc->inserted)
834c0d03 3187 remove_breakpoint (loc);
a5606eee
VP
3188
3189 hw_breakpoint_error = 1;
3190 fprintf_unfiltered (tmp_error_stream,
3191 "Could not insert hardware watchpoint %d.\n",
3192 bpt->number);
eacd795a 3193 error_flag = -1;
a5606eee
VP
3194 }
3195 }
3196
eacd795a 3197 if (error_flag)
81d0cc19
GS
3198 {
3199 /* If a hardware breakpoint or watchpoint was inserted, add a
3200 message about possibly exhausted resources. */
dd61ec5c 3201 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3202 {
c6510018
MS
3203 fprintf_unfiltered (tmp_error_stream,
3204 "Could not insert hardware breakpoints:\n\
3205You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3206 }
81d0cc19
GS
3207 target_terminal_ours_for_output ();
3208 error_stream (tmp_error_stream);
3209 }
f7545552
TT
3210
3211 do_cleanups (cleanups);
c906108c
SS
3212}
3213
c30eee59
TJB
3214/* Used when the program stops.
3215 Returns zero if successful, or non-zero if there was a problem
3216 removing a breakpoint location. */
3217
c906108c 3218int
fba45db2 3219remove_breakpoints (void)
c906108c 3220{
35df4500 3221 struct bp_location *bl, **blp_tmp;
3a1bae8e 3222 int val = 0;
c906108c 3223
35df4500 3224 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3225 {
1e4d1764 3226 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3227 val |= remove_breakpoint (bl);
c5aa993b 3228 }
3a1bae8e 3229 return val;
c906108c
SS
3230}
3231
49fa26b0
PA
3232/* When a thread exits, remove breakpoints that are related to
3233 that thread. */
3234
3235static void
3236remove_threaded_breakpoints (struct thread_info *tp, int silent)
3237{
3238 struct breakpoint *b, *b_tmp;
3239
3240 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3241 {
5d5658a1 3242 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3243 {
3244 b->disposition = disp_del_at_next_stop;
3245
3246 printf_filtered (_("\
43792cf0
PA
3247Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3248 b->number, print_thread_id (tp));
49fa26b0
PA
3249
3250 /* Hide it from the user. */
3251 b->number = 0;
3252 }
3253 }
3254}
3255
6c95b8df
PA
3256/* Remove breakpoints of process PID. */
3257
3258int
3259remove_breakpoints_pid (int pid)
3260{
35df4500 3261 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3262 int val;
3263 struct inferior *inf = find_inferior_pid (pid);
3264
35df4500 3265 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3266 {
35df4500 3267 if (bl->pspace != inf->pspace)
6c95b8df
PA
3268 continue;
3269
fc126975 3270 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3271 {
834c0d03 3272 val = remove_breakpoint (bl);
6c95b8df
PA
3273 if (val != 0)
3274 return val;
3275 }
3276 }
3277 return 0;
3278}
3279
c906108c 3280int
fba45db2 3281reattach_breakpoints (int pid)
c906108c 3282{
6c95b8df 3283 struct cleanup *old_chain;
35df4500 3284 struct bp_location *bl, **blp_tmp;
c906108c 3285 int val;
86b887df 3286 struct ui_file *tmp_error_stream;
dd61ec5c 3287 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3288 struct inferior *inf;
3289 struct thread_info *tp;
3290
3291 tp = any_live_thread_of_process (pid);
3292 if (tp == NULL)
3293 return 1;
3294
3295 inf = find_inferior_pid (pid);
3296 old_chain = save_inferior_ptid ();
3297
3298 inferior_ptid = tp->ptid;
a4954f26 3299
86b887df 3300 tmp_error_stream = mem_fileopen ();
a4954f26 3301 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3302
35df4500 3303 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3304 {
35df4500 3305 if (bl->pspace != inf->pspace)
6c95b8df
PA
3306 continue;
3307
35df4500 3308 if (bl->inserted)
c5aa993b 3309 {
35df4500 3310 bl->inserted = 0;
dd61ec5c 3311 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3312 if (val != 0)
3313 {
ce696e05 3314 do_cleanups (old_chain);
c5aa993b
JM
3315 return val;
3316 }
3317 }
3318 }
ce696e05 3319 do_cleanups (old_chain);
c906108c
SS
3320 return 0;
3321}
3322
e58b0e63
PA
3323static int internal_breakpoint_number = -1;
3324
84f4c1fe
PM
3325/* Set the breakpoint number of B, depending on the value of INTERNAL.
3326 If INTERNAL is non-zero, the breakpoint number will be populated
3327 from internal_breakpoint_number and that variable decremented.
e5dd4106 3328 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3329 breakpoint_count and that value incremented. Internal breakpoints
3330 do not set the internal var bpnum. */
3331static void
3332set_breakpoint_number (int internal, struct breakpoint *b)
3333{
3334 if (internal)
3335 b->number = internal_breakpoint_number--;
3336 else
3337 {
3338 set_breakpoint_count (breakpoint_count + 1);
3339 b->number = breakpoint_count;
3340 }
3341}
3342
e62c965a 3343static struct breakpoint *
a6d9a66e 3344create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3345 CORE_ADDR address, enum bptype type,
c0a91b2b 3346 const struct breakpoint_ops *ops)
e62c965a 3347{
e62c965a
PP
3348 struct symtab_and_line sal;
3349 struct breakpoint *b;
3350
4a64f543 3351 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3352
3353 sal.pc = address;
3354 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3355 sal.pspace = current_program_space;
e62c965a 3356
06edf0c0 3357 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3358 b->number = internal_breakpoint_number--;
3359 b->disposition = disp_donttouch;
3360
3361 return b;
3362}
3363
17450429
PP
3364static const char *const longjmp_names[] =
3365 {
3366 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3367 };
3368#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3369
3370/* Per-objfile data private to breakpoint.c. */
3371struct breakpoint_objfile_data
3372{
3373 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3374 struct bound_minimal_symbol overlay_msym;
17450429
PP
3375
3376 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3377 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3378
28106bc2
SDJ
3379 /* True if we have looked for longjmp probes. */
3380 int longjmp_searched;
3381
3382 /* SystemTap probe points for longjmp (if any). */
3383 VEC (probe_p) *longjmp_probes;
3384
17450429 3385 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3386 struct bound_minimal_symbol terminate_msym;
17450429
PP
3387
3388 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3389 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3390
3391 /* True if we have looked for exception probes. */
3392 int exception_searched;
3393
3394 /* SystemTap probe points for unwinding (if any). */
3395 VEC (probe_p) *exception_probes;
17450429
PP
3396};
3397
3398static const struct objfile_data *breakpoint_objfile_key;
3399
3400/* Minimal symbol not found sentinel. */
3401static struct minimal_symbol msym_not_found;
3402
3403/* Returns TRUE if MSYM point to the "not found" sentinel. */
3404
3405static int
3406msym_not_found_p (const struct minimal_symbol *msym)
3407{
3408 return msym == &msym_not_found;
3409}
3410
3411/* Return per-objfile data needed by breakpoint.c.
3412 Allocate the data if necessary. */
3413
3414static struct breakpoint_objfile_data *
3415get_breakpoint_objfile_data (struct objfile *objfile)
3416{
3417 struct breakpoint_objfile_data *bp_objfile_data;
3418
9a3c8263
SM
3419 bp_objfile_data = ((struct breakpoint_objfile_data *)
3420 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3421 if (bp_objfile_data == NULL)
3422 {
8d749320
SM
3423 bp_objfile_data =
3424 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3425
3426 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3427 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3428 }
3429 return bp_objfile_data;
3430}
3431
28106bc2
SDJ
3432static void
3433free_breakpoint_probes (struct objfile *obj, void *data)
3434{
9a3c8263
SM
3435 struct breakpoint_objfile_data *bp_objfile_data
3436 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3437
3438 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3439 VEC_free (probe_p, bp_objfile_data->exception_probes);
3440}
3441
e62c965a 3442static void
af02033e 3443create_overlay_event_breakpoint (void)
e62c965a 3444{
69de3c6a 3445 struct objfile *objfile;
af02033e 3446 const char *const func_name = "_ovly_debug_event";
e62c965a 3447
69de3c6a
PP
3448 ALL_OBJFILES (objfile)
3449 {
3450 struct breakpoint *b;
17450429
PP
3451 struct breakpoint_objfile_data *bp_objfile_data;
3452 CORE_ADDR addr;
67994074 3453 struct explicit_location explicit_loc;
69de3c6a 3454
17450429
PP
3455 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3456
3b7344d5 3457 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3458 continue;
3459
3b7344d5 3460 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3461 {
3b7344d5 3462 struct bound_minimal_symbol m;
17450429
PP
3463
3464 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3465 if (m.minsym == NULL)
17450429
PP
3466 {
3467 /* Avoid future lookups in this objfile. */
3b7344d5 3468 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3469 continue;
3470 }
3471 bp_objfile_data->overlay_msym = m;
3472 }
e62c965a 3473
77e371c0 3474 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3475 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3476 bp_overlay_event,
3477 &internal_breakpoint_ops);
67994074
KS
3478 initialize_explicit_location (&explicit_loc);
3479 explicit_loc.function_name = ASTRDUP (func_name);
3480 b->location = new_explicit_location (&explicit_loc);
e62c965a 3481
69de3c6a
PP
3482 if (overlay_debugging == ovly_auto)
3483 {
3484 b->enable_state = bp_enabled;
3485 overlay_events_enabled = 1;
3486 }
3487 else
3488 {
3489 b->enable_state = bp_disabled;
3490 overlay_events_enabled = 0;
3491 }
e62c965a 3492 }
e62c965a
PP
3493}
3494
0fd8e87f 3495static void
af02033e 3496create_longjmp_master_breakpoint (void)
0fd8e87f 3497{
6c95b8df 3498 struct program_space *pspace;
6c95b8df
PA
3499 struct cleanup *old_chain;
3500
3501 old_chain = save_current_program_space ();
0fd8e87f 3502
6c95b8df 3503 ALL_PSPACES (pspace)
af02033e
PP
3504 {
3505 struct objfile *objfile;
3506
3507 set_current_program_space (pspace);
3508
3509 ALL_OBJFILES (objfile)
0fd8e87f 3510 {
af02033e
PP
3511 int i;
3512 struct gdbarch *gdbarch;
17450429 3513 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3514
af02033e 3515 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3516
17450429
PP
3517 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3518
28106bc2
SDJ
3519 if (!bp_objfile_data->longjmp_searched)
3520 {
25f9533e
SDJ
3521 VEC (probe_p) *ret;
3522
3523 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3524 if (ret != NULL)
3525 {
3526 /* We are only interested in checking one element. */
3527 struct probe *p = VEC_index (probe_p, ret, 0);
3528
3529 if (!can_evaluate_probe_arguments (p))
3530 {
3531 /* We cannot use the probe interface here, because it does
3532 not know how to evaluate arguments. */
3533 VEC_free (probe_p, ret);
3534 ret = NULL;
3535 }
3536 }
3537 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3538 bp_objfile_data->longjmp_searched = 1;
3539 }
3540
3541 if (bp_objfile_data->longjmp_probes != NULL)
3542 {
3543 int i;
3544 struct probe *probe;
3545 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3546
3547 for (i = 0;
3548 VEC_iterate (probe_p,
3549 bp_objfile_data->longjmp_probes,
3550 i, probe);
3551 ++i)
3552 {
3553 struct breakpoint *b;
3554
729662a5
TT
3555 b = create_internal_breakpoint (gdbarch,
3556 get_probe_address (probe,
3557 objfile),
28106bc2
SDJ
3558 bp_longjmp_master,
3559 &internal_breakpoint_ops);
5b56227b
KS
3560 b->location
3561 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3562 b->enable_state = bp_disabled;
3563 }
3564
3565 continue;
3566 }
3567
0569175e
TSD
3568 if (!gdbarch_get_longjmp_target_p (gdbarch))
3569 continue;
3570
17450429 3571 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3572 {
3573 struct breakpoint *b;
af02033e 3574 const char *func_name;
17450429 3575 CORE_ADDR addr;
67994074 3576 struct explicit_location explicit_loc;
6c95b8df 3577
3b7344d5 3578 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3579 continue;
0fd8e87f 3580
17450429 3581 func_name = longjmp_names[i];
3b7344d5 3582 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3583 {
3b7344d5 3584 struct bound_minimal_symbol m;
17450429
PP
3585
3586 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3587 if (m.minsym == NULL)
17450429
PP
3588 {
3589 /* Prevent future lookups in this objfile. */
3b7344d5 3590 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3591 continue;
3592 }
3593 bp_objfile_data->longjmp_msym[i] = m;
3594 }
3595
77e371c0 3596 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3597 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3598 &internal_breakpoint_ops);
67994074
KS
3599 initialize_explicit_location (&explicit_loc);
3600 explicit_loc.function_name = ASTRDUP (func_name);
3601 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3602 b->enable_state = bp_disabled;
3603 }
0fd8e87f 3604 }
af02033e 3605 }
6c95b8df
PA
3606
3607 do_cleanups (old_chain);
0fd8e87f
UW
3608}
3609
af02033e 3610/* Create a master std::terminate breakpoint. */
aa7d318d 3611static void
af02033e 3612create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3613{
3614 struct program_space *pspace;
aa7d318d 3615 struct cleanup *old_chain;
af02033e 3616 const char *const func_name = "std::terminate()";
aa7d318d
TT
3617
3618 old_chain = save_current_program_space ();
3619
3620 ALL_PSPACES (pspace)
17450429
PP
3621 {
3622 struct objfile *objfile;
3623 CORE_ADDR addr;
3624
3625 set_current_program_space (pspace);
3626
aa7d318d
TT
3627 ALL_OBJFILES (objfile)
3628 {
3629 struct breakpoint *b;
17450429 3630 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3631 struct explicit_location explicit_loc;
aa7d318d 3632
17450429 3633 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3634
3b7344d5 3635 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3636 continue;
3637
3b7344d5 3638 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3639 {
3b7344d5 3640 struct bound_minimal_symbol m;
17450429
PP
3641
3642 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3643 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3644 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3645 {
3646 /* Prevent future lookups in this objfile. */
3b7344d5 3647 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3648 continue;
3649 }
3650 bp_objfile_data->terminate_msym = m;
3651 }
aa7d318d 3652
77e371c0 3653 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3654 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3655 bp_std_terminate_master,
3656 &internal_breakpoint_ops);
67994074
KS
3657 initialize_explicit_location (&explicit_loc);
3658 explicit_loc.function_name = ASTRDUP (func_name);
3659 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3660 b->enable_state = bp_disabled;
3661 }
17450429
PP
3662 }
3663
aa7d318d
TT
3664 do_cleanups (old_chain);
3665}
3666
186c406b
TT
3667/* Install a master breakpoint on the unwinder's debug hook. */
3668
70221824 3669static void
186c406b
TT
3670create_exception_master_breakpoint (void)
3671{
3672 struct objfile *objfile;
17450429 3673 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3674
3675 ALL_OBJFILES (objfile)
3676 {
17450429
PP
3677 struct breakpoint *b;
3678 struct gdbarch *gdbarch;
3679 struct breakpoint_objfile_data *bp_objfile_data;
3680 CORE_ADDR addr;
67994074 3681 struct explicit_location explicit_loc;
17450429
PP
3682
3683 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3684
28106bc2
SDJ
3685 /* We prefer the SystemTap probe point if it exists. */
3686 if (!bp_objfile_data->exception_searched)
3687 {
25f9533e
SDJ
3688 VEC (probe_p) *ret;
3689
3690 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3691
3692 if (ret != NULL)
3693 {
3694 /* We are only interested in checking one element. */
3695 struct probe *p = VEC_index (probe_p, ret, 0);
3696
3697 if (!can_evaluate_probe_arguments (p))
3698 {
3699 /* We cannot use the probe interface here, because it does
3700 not know how to evaluate arguments. */
3701 VEC_free (probe_p, ret);
3702 ret = NULL;
3703 }
3704 }
3705 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3706 bp_objfile_data->exception_searched = 1;
3707 }
3708
3709 if (bp_objfile_data->exception_probes != NULL)
3710 {
3711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3712 int i;
3713 struct probe *probe;
3714
3715 for (i = 0;
3716 VEC_iterate (probe_p,
3717 bp_objfile_data->exception_probes,
3718 i, probe);
3719 ++i)
3720 {
3721 struct breakpoint *b;
3722
729662a5
TT
3723 b = create_internal_breakpoint (gdbarch,
3724 get_probe_address (probe,
3725 objfile),
28106bc2
SDJ
3726 bp_exception_master,
3727 &internal_breakpoint_ops);
5b56227b
KS
3728 b->location
3729 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3730 b->enable_state = bp_disabled;
3731 }
3732
3733 continue;
3734 }
3735
3736 /* Otherwise, try the hook function. */
3737
3b7344d5 3738 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3739 continue;
3740
3741 gdbarch = get_objfile_arch (objfile);
186c406b 3742
3b7344d5 3743 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3744 {
3b7344d5 3745 struct bound_minimal_symbol debug_hook;
186c406b 3746
17450429 3747 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3748 if (debug_hook.minsym == NULL)
17450429 3749 {
3b7344d5 3750 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3751 continue;
3752 }
3753
3754 bp_objfile_data->exception_msym = debug_hook;
186c406b 3755 }
17450429 3756
77e371c0 3757 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3758 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3759 &current_target);
06edf0c0
PA
3760 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3761 &internal_breakpoint_ops);
67994074
KS
3762 initialize_explicit_location (&explicit_loc);
3763 explicit_loc.function_name = ASTRDUP (func_name);
3764 b->location = new_explicit_location (&explicit_loc);
17450429 3765 b->enable_state = bp_disabled;
186c406b 3766 }
186c406b
TT
3767}
3768
9ef9e6a6
KS
3769/* Does B have a location spec? */
3770
3771static int
3772breakpoint_event_location_empty_p (const struct breakpoint *b)
3773{
3774 return b->location != NULL && event_location_empty_p (b->location);
3775}
3776
c906108c 3777void
fba45db2 3778update_breakpoints_after_exec (void)
c906108c 3779{
35df4500 3780 struct breakpoint *b, *b_tmp;
876fa593 3781 struct bp_location *bploc, **bplocp_tmp;
c906108c 3782
25b22b0a
PA
3783 /* We're about to delete breakpoints from GDB's lists. If the
3784 INSERTED flag is true, GDB will try to lift the breakpoints by
3785 writing the breakpoints' "shadow contents" back into memory. The
3786 "shadow contents" are NOT valid after an exec, so GDB should not
3787 do that. Instead, the target is responsible from marking
3788 breakpoints out as soon as it detects an exec. We don't do that
3789 here instead, because there may be other attempts to delete
3790 breakpoints after detecting an exec and before reaching here. */
876fa593 3791 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3792 if (bploc->pspace == current_program_space)
3793 gdb_assert (!bploc->inserted);
c906108c 3794
35df4500 3795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3796 {
6c95b8df
PA
3797 if (b->pspace != current_program_space)
3798 continue;
3799
4a64f543 3800 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3801 if (b->type == bp_shlib_event)
3802 {
3803 delete_breakpoint (b);
3804 continue;
3805 }
c906108c 3806
4a64f543 3807 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3808 if (b->type == bp_jit_event)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
3813
1900040c 3814 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3815 as must overlay event and longjmp master breakpoints. */
3816 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3817 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3818 || b->type == bp_exception_master)
c4093a6a
JM
3819 {
3820 delete_breakpoint (b);
3821 continue;
3822 }
3823
4a64f543 3824 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3825 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3826 {
3827 delete_breakpoint (b);
3828 continue;
3829 }
3830
7c16b83e
PA
3831 /* Just like single-step breakpoints. */
3832 if (b->type == bp_single_step)
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
611c83ae
PA
3838 /* Longjmp and longjmp-resume breakpoints are also meaningless
3839 after an exec. */
186c406b 3840 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3841 || b->type == bp_longjmp_call_dummy
186c406b 3842 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3843 {
3844 delete_breakpoint (b);
3845 continue;
3846 }
3847
ce78b96d
JB
3848 if (b->type == bp_catchpoint)
3849 {
3850 /* For now, none of the bp_catchpoint breakpoints need to
3851 do anything at this point. In the future, if some of
3852 the catchpoints need to something, we will need to add
3853 a new method, and call this method from here. */
3854 continue;
3855 }
3856
c5aa993b
JM
3857 /* bp_finish is a special case. The only way we ought to be able
3858 to see one of these when an exec() has happened, is if the user
3859 caught a vfork, and then said "finish". Ordinarily a finish just
3860 carries them to the call-site of the current callee, by setting
3861 a temporary bp there and resuming. But in this case, the finish
3862 will carry them entirely through the vfork & exec.
3863
3864 We don't want to allow a bp_finish to remain inserted now. But
3865 we can't safely delete it, 'cause finish_command has a handle to
3866 the bp on a bpstat, and will later want to delete it. There's a
3867 chance (and I've seen it happen) that if we delete the bp_finish
3868 here, that its storage will get reused by the time finish_command
3869 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3870 We really must allow finish_command to delete a bp_finish.
3871
e5dd4106 3872 In the absence of a general solution for the "how do we know
53a5351d
JM
3873 it's safe to delete something others may have handles to?"
3874 problem, what we'll do here is just uninsert the bp_finish, and
3875 let finish_command delete it.
3876
3877 (We know the bp_finish is "doomed" in the sense that it's
3878 momentary, and will be deleted as soon as finish_command sees
3879 the inferior stopped. So it doesn't matter that the bp's
3880 address is probably bogus in the new a.out, unlike e.g., the
3881 solib breakpoints.) */
c5aa993b 3882
c5aa993b
JM
3883 if (b->type == bp_finish)
3884 {
3885 continue;
3886 }
3887
3888 /* Without a symbolic address, we have little hope of the
3889 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3890 a.out. */
9ef9e6a6 3891 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3892 {
3893 delete_breakpoint (b);
3894 continue;
3895 }
c5aa993b 3896 }
c906108c
SS
3897}
3898
3899int
d80ee84f 3900detach_breakpoints (ptid_t ptid)
c906108c 3901{
35df4500 3902 struct bp_location *bl, **blp_tmp;
3a1bae8e 3903 int val = 0;
ce696e05 3904 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3905 struct inferior *inf = current_inferior ();
c5aa993b 3906
dfd4cc63 3907 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3908 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3909
6c95b8df 3910 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3911 inferior_ptid = ptid;
35df4500 3912 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3913 {
35df4500 3914 if (bl->pspace != inf->pspace)
6c95b8df
PA
3915 continue;
3916
bd9673a4
PW
3917 /* This function must physically remove breakpoints locations
3918 from the specified ptid, without modifying the breakpoint
3919 package's state. Locations of type bp_loc_other are only
3920 maintained at GDB side. So, there is no need to remove
3921 these bp_loc_other locations. Moreover, removing these
3922 would modify the breakpoint package's state. */
3923 if (bl->loc_type == bp_loc_other)
3924 continue;
3925
35df4500 3926 if (bl->inserted)
b2b6a7da 3927 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3928 }
d03285ec 3929
ce696e05 3930 do_cleanups (old_chain);
3a1bae8e 3931 return val;
c906108c
SS
3932}
3933
35df4500 3934/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3935 Note that this is used to detach breakpoints from a child fork.
3936 When we get here, the child isn't in the inferior list, and neither
3937 do we have objects to represent its address space --- we should
35df4500 3938 *not* look at bl->pspace->aspace here. */
6c95b8df 3939
c906108c 3940static int
b2b6a7da 3941remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3942{
3943 int val;
c5aa993b 3944
35df4500
TJB
3945 /* BL is never in moribund_locations by our callers. */
3946 gdb_assert (bl->owner != NULL);
2bdf28a0 3947
74960c60
VP
3948 /* The type of none suggests that owner is actually deleted.
3949 This should not ever happen. */
35df4500 3950 gdb_assert (bl->owner->type != bp_none);
0bde7532 3951
35df4500
TJB
3952 if (bl->loc_type == bp_loc_software_breakpoint
3953 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3954 {
c02f5703
MS
3955 /* "Normal" instruction breakpoint: either the standard
3956 trap-instruction bp (bp_breakpoint), or a
3957 bp_hardware_breakpoint. */
3958
3959 /* First check to see if we have to handle an overlay. */
3960 if (overlay_debugging == ovly_off
35df4500
TJB
3961 || bl->section == NULL
3962 || !(section_is_overlay (bl->section)))
c02f5703
MS
3963 {
3964 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3965
3966 /* If we're trying to uninsert a memory breakpoint that we
3967 know is set in a dynamic object that is marked
3968 shlib_disabled, then either the dynamic object was
3969 removed with "remove-symbol-file" or with
3970 "nosharedlibrary". In the former case, we don't know
3971 whether another dynamic object might have loaded over the
3972 breakpoint's address -- the user might well let us know
3973 about it next with add-symbol-file (the whole point of
d03de421 3974 add-symbol-file is letting the user manually maintain a
08351840
PA
3975 list of dynamically loaded objects). If we have the
3976 breakpoint's shadow memory, that is, this is a software
3977 breakpoint managed by GDB, check whether the breakpoint
3978 is still inserted in memory, to avoid overwriting wrong
3979 code with stale saved shadow contents. Note that HW
3980 breakpoints don't have shadow memory, as they're
3981 implemented using a mechanism that is not dependent on
3982 being able to modify the target's memory, and as such
3983 they should always be removed. */
3984 if (bl->shlib_disabled
3985 && bl->target_info.shadow_len != 0
3986 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3987 val = 0;
3988 else
73971819 3989 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3990 }
c906108c
SS
3991 else
3992 {
4a64f543 3993 /* This breakpoint is in an overlay section.
c02f5703
MS
3994 Did we set a breakpoint at the LMA? */
3995 if (!overlay_events_enabled)
3996 {
3997 /* Yes -- overlay event support is not active, so we
3998 should have set a breakpoint at the LMA. Remove it.
3999 */
c02f5703
MS
4000 /* Ignore any failures: if the LMA is in ROM, we will
4001 have already warned when we failed to insert it. */
35df4500
TJB
4002 if (bl->loc_type == bp_loc_hardware_breakpoint)
4003 target_remove_hw_breakpoint (bl->gdbarch,
4004 &bl->overlay_target_info);
c02f5703 4005 else
35df4500 4006 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4007 &bl->overlay_target_info,
4008 reason);
c02f5703
MS
4009 }
4010 /* Did we set a breakpoint at the VMA?
4011 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4012 if (bl->inserted)
c906108c 4013 {
c02f5703
MS
4014 /* Yes -- remove it. Previously we did not bother to
4015 remove the breakpoint if the section had been
4016 unmapped, but let's not rely on that being safe. We
4017 don't know what the overlay manager might do. */
aa67235e
UW
4018
4019 /* However, we should remove *software* breakpoints only
4020 if the section is still mapped, or else we overwrite
4021 wrong code with the saved shadow contents. */
348d480f
PA
4022 if (bl->loc_type == bp_loc_hardware_breakpoint
4023 || section_is_mapped (bl->section))
73971819 4024 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4025 else
4026 val = 0;
c906108c 4027 }
c02f5703
MS
4028 else
4029 {
4030 /* No -- not inserted, so no need to remove. No error. */
4031 val = 0;
4032 }
c906108c 4033 }
879d1e6b 4034
08351840
PA
4035 /* In some cases, we might not be able to remove a breakpoint in
4036 a shared library that has already been removed, but we have
4037 not yet processed the shlib unload event. Similarly for an
4038 unloaded add-symbol-file object - the user might not yet have
4039 had the chance to remove-symbol-file it. shlib_disabled will
4040 be set if the library/object has already been removed, but
4041 the breakpoint hasn't been uninserted yet, e.g., after
4042 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4043 always-inserted mode. */
076855f9 4044 if (val
08351840
PA
4045 && (bl->loc_type == bp_loc_software_breakpoint
4046 && (bl->shlib_disabled
4047 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4048 || shared_objfile_contains_address_p (bl->pspace,
4049 bl->address))))
879d1e6b
UW
4050 val = 0;
4051
c906108c
SS
4052 if (val)
4053 return val;
b2b6a7da 4054 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4055 }
35df4500 4056 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4057 {
77b06cd7
TJB
4058 gdb_assert (bl->owner->ops != NULL
4059 && bl->owner->ops->remove_location != NULL);
4060
b2b6a7da 4061 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4062 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4063
c906108c 4064 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4065 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4066 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4067 bl->owner->number);
c906108c 4068 }
35df4500
TJB
4069 else if (bl->owner->type == bp_catchpoint
4070 && breakpoint_enabled (bl->owner)
4071 && !bl->duplicate)
ce78b96d 4072 {
77b06cd7
TJB
4073 gdb_assert (bl->owner->ops != NULL
4074 && bl->owner->ops->remove_location != NULL);
ce78b96d 4075
73971819 4076 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4077 if (val)
4078 return val;
77b06cd7 4079
b2b6a7da 4080 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4081 }
c906108c
SS
4082
4083 return 0;
4084}
4085
6c95b8df 4086static int
834c0d03 4087remove_breakpoint (struct bp_location *bl)
6c95b8df
PA
4088{
4089 int ret;
4090 struct cleanup *old_chain;
4091
35df4500
TJB
4092 /* BL is never in moribund_locations by our callers. */
4093 gdb_assert (bl->owner != NULL);
2bdf28a0 4094
6c95b8df
PA
4095 /* The type of none suggests that owner is actually deleted.
4096 This should not ever happen. */
35df4500 4097 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4098
4099 old_chain = save_current_space_and_thread ();
4100
35df4500 4101 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4102
b2b6a7da 4103 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4104
4105 do_cleanups (old_chain);
4106 return ret;
4107}
4108
c906108c
SS
4109/* Clear the "inserted" flag in all breakpoints. */
4110
25b22b0a 4111void
fba45db2 4112mark_breakpoints_out (void)
c906108c 4113{
35df4500 4114 struct bp_location *bl, **blp_tmp;
c906108c 4115
35df4500 4116 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4117 if (bl->pspace == current_program_space)
35df4500 4118 bl->inserted = 0;
c906108c
SS
4119}
4120
53a5351d
JM
4121/* Clear the "inserted" flag in all breakpoints and delete any
4122 breakpoints which should go away between runs of the program.
c906108c
SS
4123
4124 Plus other such housekeeping that has to be done for breakpoints
4125 between runs.
4126
53a5351d
JM
4127 Note: this function gets called at the end of a run (by
4128 generic_mourn_inferior) and when a run begins (by
4a64f543 4129 init_wait_for_inferior). */
c906108c
SS
4130
4131
4132
4133void
fba45db2 4134breakpoint_init_inferior (enum inf_context context)
c906108c 4135{
35df4500 4136 struct breakpoint *b, *b_tmp;
870f88f7 4137 struct bp_location *bl;
1c5cfe86 4138 int ix;
6c95b8df 4139 struct program_space *pspace = current_program_space;
c906108c 4140
50c71eaf
PA
4141 /* If breakpoint locations are shared across processes, then there's
4142 nothing to do. */
f5656ead 4143 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4144 return;
4145
1a853c52 4146 mark_breakpoints_out ();
075f6582 4147
35df4500 4148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4149 {
6c95b8df
PA
4150 if (b->loc && b->loc->pspace != pspace)
4151 continue;
4152
c5aa993b
JM
4153 switch (b->type)
4154 {
4155 case bp_call_dummy:
e2e4d78b 4156 case bp_longjmp_call_dummy:
c906108c 4157
c5aa993b 4158 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4159 cause problems when the inferior is rerun, so we better get
4160 rid of it. */
4161
4162 case bp_watchpoint_scope:
4163
4164 /* Also get rid of scope breakpoints. */
4165
4166 case bp_shlib_event:
4167
4168 /* Also remove solib event breakpoints. Their addresses may
4169 have changed since the last time we ran the program.
4170 Actually we may now be debugging against different target;
4171 and so the solib backend that installed this breakpoint may
4172 not be used in by the target. E.g.,
4173
4174 (gdb) file prog-linux
4175 (gdb) run # native linux target
4176 ...
4177 (gdb) kill
4178 (gdb) file prog-win.exe
4179 (gdb) tar rem :9999 # remote Windows gdbserver.
4180 */
c906108c 4181
f59f708a
PA
4182 case bp_step_resume:
4183
4184 /* Also remove step-resume breakpoints. */
4185
7c16b83e
PA
4186 case bp_single_step:
4187
4188 /* Also remove single-step breakpoints. */
4189
c5aa993b
JM
4190 delete_breakpoint (b);
4191 break;
c906108c 4192
c5aa993b
JM
4193 case bp_watchpoint:
4194 case bp_hardware_watchpoint:
4195 case bp_read_watchpoint:
4196 case bp_access_watchpoint:
3a5c3e22
PA
4197 {
4198 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4199
3a5c3e22
PA
4200 /* Likewise for watchpoints on local expressions. */
4201 if (w->exp_valid_block != NULL)
4202 delete_breakpoint (b);
63000888 4203 else
3a5c3e22 4204 {
63000888
PA
4205 /* Get rid of existing locations, which are no longer
4206 valid. New ones will be created in
4207 update_watchpoint, when the inferior is restarted.
4208 The next update_global_location_list call will
4209 garbage collect them. */
4210 b->loc = NULL;
4211
4212 if (context == inf_starting)
4213 {
4214 /* Reset val field to force reread of starting value in
4215 insert_breakpoints. */
4216 if (w->val)
4217 value_free (w->val);
4218 w->val = NULL;
4219 w->val_valid = 0;
4220 }
4221 }
3a5c3e22 4222 }
c5aa993b
JM
4223 break;
4224 default:
c5aa993b
JM
4225 break;
4226 }
4227 }
1c5cfe86
PA
4228
4229 /* Get rid of the moribund locations. */
35df4500
TJB
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4231 decref_bp_location (&bl);
1c5cfe86 4232 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4233}
4234
6c95b8df
PA
4235/* These functions concern about actual breakpoints inserted in the
4236 target --- to e.g. check if we need to do decr_pc adjustment or if
4237 we need to hop over the bkpt --- so we check for address space
4238 match, not program space. */
4239
c2c6d25f
JM
4240/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4241 exists at PC. It returns ordinary_breakpoint_here if it's an
4242 ordinary breakpoint, or permanent_breakpoint_here if it's a
4243 permanent breakpoint.
4244 - When continuing from a location with an ordinary breakpoint, we
4245 actually single step once before calling insert_breakpoints.
e5dd4106 4246 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4247 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4248 the target, to advance the PC past the breakpoint. */
c906108c 4249
c2c6d25f 4250enum breakpoint_here
6c95b8df 4251breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4252{
35df4500 4253 struct bp_location *bl, **blp_tmp;
c2c6d25f 4254 int any_breakpoint_here = 0;
c906108c 4255
35df4500 4256 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4257 {
35df4500
TJB
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4260 continue;
4261
f1310107 4262 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4263 if ((breakpoint_enabled (bl->owner)
1a853c52 4264 || bl->permanent)
f1310107 4265 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4266 {
4267 if (overlay_debugging
35df4500
TJB
4268 && section_is_overlay (bl->section)
4269 && !section_is_mapped (bl->section))
075f6582 4270 continue; /* unmapped overlay -- can't be a match */
1a853c52 4271 else if (bl->permanent)
075f6582
DJ
4272 return permanent_breakpoint_here;
4273 else
4274 any_breakpoint_here = 1;
4275 }
4276 }
c906108c 4277
f486487f 4278 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4279}
4280
d35ae833
PA
4281/* See breakpoint.h. */
4282
4283int
4284breakpoint_in_range_p (struct address_space *aspace,
4285 CORE_ADDR addr, ULONGEST len)
4286{
4287 struct bp_location *bl, **blp_tmp;
4288
4289 ALL_BP_LOCATIONS (bl, blp_tmp)
4290 {
4291 if (bl->loc_type != bp_loc_software_breakpoint
4292 && bl->loc_type != bp_loc_hardware_breakpoint)
4293 continue;
4294
4295 if ((breakpoint_enabled (bl->owner)
4296 || bl->permanent)
4297 && breakpoint_location_address_range_overlap (bl, aspace,
4298 addr, len))
4299 {
4300 if (overlay_debugging
4301 && section_is_overlay (bl->section)
4302 && !section_is_mapped (bl->section))
4303 {
4304 /* Unmapped overlay -- can't be a match. */
4305 continue;
4306 }
4307
4308 return 1;
4309 }
4310 }
4311
4312 return 0;
4313}
4314
1c5cfe86
PA
4315/* Return true if there's a moribund breakpoint at PC. */
4316
4317int
6c95b8df 4318moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4319{
4320 struct bp_location *loc;
4321 int ix;
4322
4323 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4324 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4325 return 1;
4326
4327 return 0;
4328}
c2c6d25f 4329
f7ce857f
PA
4330/* Returns non-zero iff BL is inserted at PC, in address space
4331 ASPACE. */
4332
4333static int
4334bp_location_inserted_here_p (struct bp_location *bl,
4335 struct address_space *aspace, CORE_ADDR pc)
4336{
4337 if (bl->inserted
4338 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4339 aspace, pc))
4340 {
4341 if (overlay_debugging
4342 && section_is_overlay (bl->section)
4343 && !section_is_mapped (bl->section))
4344 return 0; /* unmapped overlay -- can't be a match */
4345 else
4346 return 1;
4347 }
4348 return 0;
4349}
4350
a1fd2fa5 4351/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4352
4353int
a1fd2fa5 4354breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4355{
f7ce857f 4356 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4357
f7ce857f 4358 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4359 {
f7ce857f
PA
4360 struct bp_location *bl = *blp;
4361
35df4500
TJB
4362 if (bl->loc_type != bp_loc_software_breakpoint
4363 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4364 continue;
4365
f7ce857f
PA
4366 if (bp_location_inserted_here_p (bl, aspace, pc))
4367 return 1;
c5aa993b 4368 }
c36b740a
VP
4369 return 0;
4370}
4371
a1fd2fa5
PA
4372/* This function returns non-zero iff there is a software breakpoint
4373 inserted at PC. */
c36b740a
VP
4374
4375int
a1fd2fa5
PA
4376software_breakpoint_inserted_here_p (struct address_space *aspace,
4377 CORE_ADDR pc)
4fa8626c 4378{
f7ce857f 4379 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4380
f7ce857f 4381 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4382 {
f7ce857f
PA
4383 struct bp_location *bl = *blp;
4384
35df4500 4385 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4386 continue;
4387
f7ce857f
PA
4388 if (bp_location_inserted_here_p (bl, aspace, pc))
4389 return 1;
4fa8626c
DJ
4390 }
4391
4392 return 0;
9c02b525
PA
4393}
4394
4395/* See breakpoint.h. */
4396
4397int
4398hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4399 CORE_ADDR pc)
4400{
4401 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4402
4403 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4404 {
4405 struct bp_location *bl = *blp;
4406
4407 if (bl->loc_type != bp_loc_hardware_breakpoint)
4408 continue;
4409
4410 if (bp_location_inserted_here_p (bl, aspace, pc))
4411 return 1;
4412 }
4413
4414 return 0;
4fa8626c
DJ
4415}
4416
9093389c
PA
4417int
4418hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4419 CORE_ADDR addr, ULONGEST len)
4420{
4421 struct breakpoint *bpt;
4422
4423 ALL_BREAKPOINTS (bpt)
4424 {
4425 struct bp_location *loc;
4426
4427 if (bpt->type != bp_hardware_watchpoint
4428 && bpt->type != bp_access_watchpoint)
4429 continue;
4430
4431 if (!breakpoint_enabled (bpt))
4432 continue;
4433
4434 for (loc = bpt->loc; loc; loc = loc->next)
4435 if (loc->pspace->aspace == aspace && loc->inserted)
4436 {
4437 CORE_ADDR l, h;
4438
4439 /* Check for intersection. */
768adc05
PA
4440 l = std::max<CORE_ADDR> (loc->address, addr);
4441 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4442 if (l < h)
4443 return 1;
4444 }
4445 }
4446 return 0;
4447}
c906108c 4448\f
c5aa993b 4449
c906108c
SS
4450/* bpstat stuff. External routines' interfaces are documented
4451 in breakpoint.h. */
4452
4453int
c326b90e 4454is_catchpoint (struct breakpoint *ep)
c906108c 4455{
533be4dd 4456 return (ep->type == bp_catchpoint);
c906108c
SS
4457}
4458
f431efe5
PA
4459/* Frees any storage that is part of a bpstat. Does not walk the
4460 'next' chain. */
4461
4462static void
198757a8
VP
4463bpstat_free (bpstat bs)
4464{
4465 if (bs->old_val != NULL)
4466 value_free (bs->old_val);
9add0f1b 4467 decref_counted_command_line (&bs->commands);
f431efe5 4468 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4469 xfree (bs);
4470}
4471
c906108c
SS
4472/* Clear a bpstat so that it says we are not at any breakpoint.
4473 Also free any storage that is part of a bpstat. */
4474
4475void
fba45db2 4476bpstat_clear (bpstat *bsp)
c906108c
SS
4477{
4478 bpstat p;
4479 bpstat q;
4480
4481 if (bsp == 0)
4482 return;
4483 p = *bsp;
4484 while (p != NULL)
4485 {
4486 q = p->next;
198757a8 4487 bpstat_free (p);
c906108c
SS
4488 p = q;
4489 }
4490 *bsp = NULL;
4491}
4492
4493/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4494 is part of the bpstat is copied as well. */
4495
4496bpstat
fba45db2 4497bpstat_copy (bpstat bs)
c906108c
SS
4498{
4499 bpstat p = NULL;
4500 bpstat tmp;
4501 bpstat retval = NULL;
4502
4503 if (bs == NULL)
4504 return bs;
4505
4506 for (; bs != NULL; bs = bs->next)
4507 {
4508 tmp = (bpstat) xmalloc (sizeof (*tmp));
4509 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4510 incref_counted_command_line (tmp->commands);
f431efe5 4511 incref_bp_location (tmp->bp_location_at);
31cc81e9 4512 if (bs->old_val != NULL)
3c3185ac
JK
4513 {
4514 tmp->old_val = value_copy (bs->old_val);
4515 release_value (tmp->old_val);
4516 }
31cc81e9 4517
c906108c
SS
4518 if (p == NULL)
4519 /* This is the first thing in the chain. */
4520 retval = tmp;
4521 else
4522 p->next = tmp;
4523 p = tmp;
4524 }
4525 p->next = NULL;
4526 return retval;
4527}
4528
4a64f543 4529/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4530
4531bpstat
fba45db2 4532bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4533{
c5aa993b
JM
4534 if (bsp == NULL)
4535 return NULL;
c906108c 4536
c5aa993b
JM
4537 for (; bsp != NULL; bsp = bsp->next)
4538 {
f431efe5 4539 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4540 return bsp;
4541 }
c906108c
SS
4542 return NULL;
4543}
4544
ab04a2af
TT
4545/* See breakpoint.h. */
4546
47591c29 4547int
427cd150 4548bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4549{
ab04a2af
TT
4550 for (; bsp != NULL; bsp = bsp->next)
4551 {
427cd150
TT
4552 if (bsp->breakpoint_at == NULL)
4553 {
4554 /* A moribund location can never explain a signal other than
4555 GDB_SIGNAL_TRAP. */
4556 if (sig == GDB_SIGNAL_TRAP)
47591c29 4557 return 1;
427cd150
TT
4558 }
4559 else
47591c29
PA
4560 {
4561 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4562 sig))
4563 return 1;
4564 }
ab04a2af
TT
4565 }
4566
47591c29 4567 return 0;
ab04a2af
TT
4568}
4569
4a64f543
MS
4570/* Put in *NUM the breakpoint number of the first breakpoint we are
4571 stopped at. *BSP upon return is a bpstat which points to the
4572 remaining breakpoints stopped at (but which is not guaranteed to be
4573 good for anything but further calls to bpstat_num).
4574
8671a17b
PA
4575 Return 0 if passed a bpstat which does not indicate any breakpoints.
4576 Return -1 if stopped at a breakpoint that has been deleted since
4577 we set it.
4578 Return 1 otherwise. */
c906108c
SS
4579
4580int
8671a17b 4581bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4582{
4583 struct breakpoint *b;
4584
4585 if ((*bsp) == NULL)
4586 return 0; /* No more breakpoint values */
8671a17b 4587
4a64f543
MS
4588 /* We assume we'll never have several bpstats that correspond to a
4589 single breakpoint -- otherwise, this function might return the
4590 same number more than once and this will look ugly. */
f431efe5 4591 b = (*bsp)->breakpoint_at;
8671a17b
PA
4592 *bsp = (*bsp)->next;
4593 if (b == NULL)
4594 return -1; /* breakpoint that's been deleted since */
4595
4596 *num = b->number; /* We have its number */
4597 return 1;
c906108c
SS
4598}
4599
e93ca019 4600/* See breakpoint.h. */
c906108c
SS
4601
4602void
e93ca019 4603bpstat_clear_actions (void)
c906108c 4604{
e93ca019
JK
4605 struct thread_info *tp;
4606 bpstat bs;
4607
4608 if (ptid_equal (inferior_ptid, null_ptid))
4609 return;
4610
4611 tp = find_thread_ptid (inferior_ptid);
4612 if (tp == NULL)
4613 return;
4614
4615 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4616 {
9add0f1b 4617 decref_counted_command_line (&bs->commands);
abf85f46 4618
c906108c
SS
4619 if (bs->old_val != NULL)
4620 {
4621 value_free (bs->old_val);
4622 bs->old_val = NULL;
4623 }
4624 }
4625}
4626
f3b1572e
PA
4627/* Called when a command is about to proceed the inferior. */
4628
4629static void
4630breakpoint_about_to_proceed (void)
4631{
4632 if (!ptid_equal (inferior_ptid, null_ptid))
4633 {
4634 struct thread_info *tp = inferior_thread ();
4635
4636 /* Allow inferior function calls in breakpoint commands to not
4637 interrupt the command list. When the call finishes
4638 successfully, the inferior will be standing at the same
4639 breakpoint as if nothing happened. */
16c381f0 4640 if (tp->control.in_infcall)
f3b1572e
PA
4641 return;
4642 }
4643
4644 breakpoint_proceeded = 1;
4645}
4646
4a64f543
MS
4647/* Stub for cleaning up our state if we error-out of a breakpoint
4648 command. */
c906108c 4649static void
4efb68b1 4650cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4651{
4652 executing_breakpoint_commands = 0;
4653}
4654
abf85f46
JK
4655/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4656 or its equivalent. */
4657
4658static int
4659command_line_is_silent (struct command_line *cmd)
4660{
4f45d445 4661 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4662}
4663
4a64f543
MS
4664/* Execute all the commands associated with all the breakpoints at
4665 this location. Any of these commands could cause the process to
4666 proceed beyond this point, etc. We look out for such changes by
4667 checking the global "breakpoint_proceeded" after each command.
c906108c 4668
347bddb7
PA
4669 Returns true if a breakpoint command resumed the inferior. In that
4670 case, it is the caller's responsibility to recall it again with the
4671 bpstat of the current thread. */
4672
4673static int
4674bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4675{
4676 bpstat bs;
4677 struct cleanup *old_chain;
347bddb7 4678 int again = 0;
c906108c
SS
4679
4680 /* Avoid endless recursion if a `source' command is contained
4681 in bs->commands. */
4682 if (executing_breakpoint_commands)
347bddb7 4683 return 0;
c906108c
SS
4684
4685 executing_breakpoint_commands = 1;
4686 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4687
cf6c5ffb
TT
4688 prevent_dont_repeat ();
4689
4a64f543 4690 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4691 bs = *bsp;
4692
4693 breakpoint_proceeded = 0;
4694 for (; bs != NULL; bs = bs->next)
4695 {
9add0f1b 4696 struct counted_command_line *ccmd;
6c50ab1c
JB
4697 struct command_line *cmd;
4698 struct cleanup *this_cmd_tree_chain;
4699
4700 /* Take ownership of the BSP's command tree, if it has one.
4701
4702 The command tree could legitimately contain commands like
4703 'step' and 'next', which call clear_proceed_status, which
4704 frees stop_bpstat's command tree. To make sure this doesn't
4705 free the tree we're executing out from under us, we need to
4706 take ownership of the tree ourselves. Since a given bpstat's
4707 commands are only executed once, we don't need to copy it; we
4708 can clear the pointer in the bpstat, and make sure we free
4709 the tree when we're done. */
9add0f1b
TT
4710 ccmd = bs->commands;
4711 bs->commands = NULL;
abf85f46
JK
4712 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4713 cmd = ccmd ? ccmd->commands : NULL;
4714 if (command_line_is_silent (cmd))
4715 {
4716 /* The action has been already done by bpstat_stop_status. */
4717 cmd = cmd->next;
4718 }
6c50ab1c 4719
c906108c
SS
4720 while (cmd != NULL)
4721 {
4722 execute_control_command (cmd);
4723
4724 if (breakpoint_proceeded)
4725 break;
4726 else
4727 cmd = cmd->next;
4728 }
6c50ab1c
JB
4729
4730 /* We can free this command tree now. */
4731 do_cleanups (this_cmd_tree_chain);
4732
c906108c 4733 if (breakpoint_proceeded)
32c1e744 4734 {
cb814510 4735 if (current_ui->async)
347bddb7
PA
4736 /* If we are in async mode, then the target might be still
4737 running, not stopped at any breakpoint, so nothing for
4738 us to do here -- just return to the event loop. */
4739 ;
32c1e744
VP
4740 else
4741 /* In sync mode, when execute_control_command returns
4742 we're already standing on the next breakpoint.
347bddb7
PA
4743 Breakpoint commands for that stop were not run, since
4744 execute_command does not run breakpoint commands --
4745 only command_line_handler does, but that one is not
4746 involved in execution of breakpoint commands. So, we
4747 can now execute breakpoint commands. It should be
4748 noted that making execute_command do bpstat actions is
4749 not an option -- in this case we'll have recursive
4750 invocation of bpstat for each breakpoint with a
4751 command, and can easily blow up GDB stack. Instead, we
4752 return true, which will trigger the caller to recall us
4753 with the new stop_bpstat. */
4754 again = 1;
4755 break;
32c1e744 4756 }
c906108c 4757 }
c2b8ed2c 4758 do_cleanups (old_chain);
347bddb7
PA
4759 return again;
4760}
4761
4762void
4763bpstat_do_actions (void)
4764{
353d1d73
JK
4765 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4766
347bddb7
PA
4767 /* Do any commands attached to breakpoint we are stopped at. */
4768 while (!ptid_equal (inferior_ptid, null_ptid)
4769 && target_has_execution
4770 && !is_exited (inferior_ptid)
4771 && !is_executing (inferior_ptid))
4772 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4773 and only return when it is stopped at the next breakpoint, we
4774 keep doing breakpoint actions until it returns false to
4775 indicate the inferior was not resumed. */
16c381f0 4776 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4777 break;
353d1d73
JK
4778
4779 discard_cleanups (cleanup_if_error);
c906108c
SS
4780}
4781
fa4727a6
DJ
4782/* Print out the (old or new) value associated with a watchpoint. */
4783
4784static void
4785watchpoint_value_print (struct value *val, struct ui_file *stream)
4786{
4787 if (val == NULL)
4788 fprintf_unfiltered (stream, _("<unreadable>"));
4789 else
79a45b7d
TT
4790 {
4791 struct value_print_options opts;
4792 get_user_print_options (&opts);
4793 value_print (val, stream, &opts);
4794 }
fa4727a6
DJ
4795}
4796
f303dbd6
PA
4797/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4798 debugging multiple threads. */
4799
4800void
4801maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4802{
4803 if (ui_out_is_mi_like_p (uiout))
4804 return;
4805
4806 ui_out_text (uiout, "\n");
4807
4808 if (show_thread_that_caused_stop ())
4809 {
4810 const char *name;
4811 struct thread_info *thr = inferior_thread ();
4812
4813 ui_out_text (uiout, "Thread ");
4814 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4815
4816 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4817 if (name != NULL)
4818 {
4819 ui_out_text (uiout, " \"");
4820 ui_out_field_fmt (uiout, "name", "%s", name);
4821 ui_out_text (uiout, "\"");
4822 }
4823
4824 ui_out_text (uiout, " hit ");
4825 }
4826}
4827
e514a9d6 4828/* Generic routine for printing messages indicating why we
4a64f543 4829 stopped. The behavior of this function depends on the value
e514a9d6
JM
4830 'print_it' in the bpstat structure. Under some circumstances we
4831 may decide not to print anything here and delegate the task to
4a64f543 4832 normal_stop(). */
e514a9d6
JM
4833
4834static enum print_stop_action
4835print_bp_stop_message (bpstat bs)
4836{
4837 switch (bs->print_it)
4838 {
4839 case print_it_noop:
4a64f543 4840 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4841 return PRINT_UNKNOWN;
4842 break;
4843
4844 case print_it_done:
4845 /* We still want to print the frame, but we already printed the
4a64f543 4846 relevant messages. */
e514a9d6
JM
4847 return PRINT_SRC_AND_LOC;
4848 break;
4849
4850 case print_it_normal:
4f8d1dc6 4851 {
f431efe5
PA
4852 struct breakpoint *b = bs->breakpoint_at;
4853
1a6a67de
TJB
4854 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4855 which has since been deleted. */
4856 if (b == NULL)
4857 return PRINT_UNKNOWN;
4858
348d480f
PA
4859 /* Normal case. Call the breakpoint's print_it method. */
4860 return b->ops->print_it (bs);
4f8d1dc6 4861 }
348d480f 4862 break;
3086aeae 4863
e514a9d6 4864 default:
8e65ff28 4865 internal_error (__FILE__, __LINE__,
e2e0b3e5 4866 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4867 break;
c906108c 4868 }
c906108c
SS
4869}
4870
edcc5120
TT
4871/* A helper function that prints a shared library stopped event. */
4872
4873static void
4874print_solib_event (int is_catchpoint)
4875{
4876 int any_deleted
4877 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4878 int any_added
4879 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4880
4881 if (!is_catchpoint)
4882 {
4883 if (any_added || any_deleted)
4884 ui_out_text (current_uiout,
4885 _("Stopped due to shared library event:\n"));
4886 else
4887 ui_out_text (current_uiout,
4888 _("Stopped due to shared library event (no "
4889 "libraries added or removed)\n"));
4890 }
4891
4892 if (ui_out_is_mi_like_p (current_uiout))
4893 ui_out_field_string (current_uiout, "reason",
4894 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4895
4896 if (any_deleted)
4897 {
4898 struct cleanup *cleanup;
4899 char *name;
4900 int ix;
4901
4902 ui_out_text (current_uiout, _(" Inferior unloaded "));
4903 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4904 "removed");
4905 for (ix = 0;
4906 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4907 ix, name);
4908 ++ix)
4909 {
4910 if (ix > 0)
4911 ui_out_text (current_uiout, " ");
4912 ui_out_field_string (current_uiout, "library", name);
4913 ui_out_text (current_uiout, "\n");
4914 }
4915
4916 do_cleanups (cleanup);
4917 }
4918
4919 if (any_added)
4920 {
4921 struct so_list *iter;
4922 int ix;
4923 struct cleanup *cleanup;
4924
4925 ui_out_text (current_uiout, _(" Inferior loaded "));
4926 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4927 "added");
4928 for (ix = 0;
4929 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4930 ix, iter);
4931 ++ix)
4932 {
4933 if (ix > 0)
4934 ui_out_text (current_uiout, " ");
4935 ui_out_field_string (current_uiout, "library", iter->so_name);
4936 ui_out_text (current_uiout, "\n");
4937 }
4938
4939 do_cleanups (cleanup);
4940 }
4941}
4942
e514a9d6
JM
4943/* Print a message indicating what happened. This is called from
4944 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4945 list - a list of the eventpoints that caused this stop. KIND is
4946 the target_waitkind for the stopping event. This
e514a9d6
JM
4947 routine calls the generic print routine for printing a message
4948 about reasons for stopping. This will print (for example) the
4949 "Breakpoint n," part of the output. The return value of this
4950 routine is one of:
c906108c 4951
4a64f543 4952 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4953 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4954 code to print the location. An example is
c5aa993b
JM
4955 "Breakpoint 1, " which should be followed by
4956 the location.
917317f4 4957 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4958 to also print the location part of the message.
4959 An example is the catch/throw messages, which
4a64f543 4960 don't require a location appended to the end.
917317f4 4961 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4962 further info to be printed. */
c906108c 4963
917317f4 4964enum print_stop_action
36dfb11c 4965bpstat_print (bpstat bs, int kind)
c906108c 4966{
f486487f 4967 enum print_stop_action val;
c5aa993b 4968
c906108c 4969 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4970 (Currently all watchpoints go on the bpstat whether hit or not.
4971 That probably could (should) be changed, provided care is taken
c906108c 4972 with respect to bpstat_explains_signal). */
e514a9d6
JM
4973 for (; bs; bs = bs->next)
4974 {
4975 val = print_bp_stop_message (bs);
4976 if (val == PRINT_SRC_ONLY
4977 || val == PRINT_SRC_AND_LOC
4978 || val == PRINT_NOTHING)
4979 return val;
4980 }
c906108c 4981
36dfb11c
TT
4982 /* If we had hit a shared library event breakpoint,
4983 print_bp_stop_message would print out this message. If we hit an
4984 OS-level shared library event, do the same thing. */
4985 if (kind == TARGET_WAITKIND_LOADED)
4986 {
edcc5120 4987 print_solib_event (0);
36dfb11c
TT
4988 return PRINT_NOTHING;
4989 }
4990
e514a9d6 4991 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4992 with and nothing was printed. */
917317f4 4993 return PRINT_UNKNOWN;
c906108c
SS
4994}
4995
c42bd95a
DE
4996/* Evaluate the expression EXP and return 1 if value is zero.
4997 This returns the inverse of the condition because it is called
4998 from catch_errors which returns 0 if an exception happened, and if an
4999 exception happens we want execution to stop.
4a64f543 5000 The argument is a "struct expression *" that has been cast to a
c42bd95a 5001 "void *" to make it pass through catch_errors. */
c906108c
SS
5002
5003static int
4efb68b1 5004breakpoint_cond_eval (void *exp)
c906108c 5005{
278cd55f 5006 struct value *mark = value_mark ();
c5aa993b 5007 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 5008
c906108c
SS
5009 value_free_to_mark (mark);
5010 return i;
5011}
5012
5760d0ab 5013/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5014
5015static bpstat
5760d0ab 5016bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5017{
5018 bpstat bs;
5019
5020 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5021 bs->next = NULL;
5022 **bs_link_pointer = bs;
5023 *bs_link_pointer = &bs->next;
f431efe5
PA
5024 bs->breakpoint_at = bl->owner;
5025 bs->bp_location_at = bl;
5026 incref_bp_location (bl);
c906108c
SS
5027 /* If the condition is false, etc., don't do the commands. */
5028 bs->commands = NULL;
5029 bs->old_val = NULL;
5030 bs->print_it = print_it_normal;
5031 return bs;
5032}
5033\f
d983da9c
DJ
5034/* The target has stopped with waitstatus WS. Check if any hardware
5035 watchpoints have triggered, according to the target. */
5036
5037int
5038watchpoints_triggered (struct target_waitstatus *ws)
5039{
d92524f1 5040 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5041 CORE_ADDR addr;
5042 struct breakpoint *b;
5043
5044 if (!stopped_by_watchpoint)
5045 {
5046 /* We were not stopped by a watchpoint. Mark all watchpoints
5047 as not triggered. */
5048 ALL_BREAKPOINTS (b)
cc60f2e3 5049 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5050 {
5051 struct watchpoint *w = (struct watchpoint *) b;
5052
5053 w->watchpoint_triggered = watch_triggered_no;
5054 }
d983da9c
DJ
5055
5056 return 0;
5057 }
5058
5059 if (!target_stopped_data_address (&current_target, &addr))
5060 {
5061 /* We were stopped by a watchpoint, but we don't know where.
5062 Mark all watchpoints as unknown. */
5063 ALL_BREAKPOINTS (b)
cc60f2e3 5064 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5065 {
5066 struct watchpoint *w = (struct watchpoint *) b;
5067
5068 w->watchpoint_triggered = watch_triggered_unknown;
5069 }
d983da9c 5070
3c4797ba 5071 return 1;
d983da9c
DJ
5072 }
5073
5074 /* The target could report the data address. Mark watchpoints
5075 affected by this data address as triggered, and all others as not
5076 triggered. */
5077
5078 ALL_BREAKPOINTS (b)
cc60f2e3 5079 if (is_hardware_watchpoint (b))
d983da9c 5080 {
3a5c3e22 5081 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5082 struct bp_location *loc;
d983da9c 5083
3a5c3e22 5084 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5085 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5086 {
3a5c3e22 5087 if (is_masked_watchpoint (b))
9c06b0b4 5088 {
3a5c3e22
PA
5089 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5090 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5091
5092 if (newaddr == start)
5093 {
3a5c3e22 5094 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5095 break;
5096 }
5097 }
5098 /* Exact match not required. Within range is sufficient. */
5099 else if (target_watchpoint_addr_within_range (&current_target,
5100 addr, loc->address,
5101 loc->length))
5102 {
3a5c3e22 5103 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5104 break;
5105 }
5106 }
d983da9c
DJ
5107 }
5108
5109 return 1;
5110}
5111
c906108c
SS
5112/* Possible return values for watchpoint_check (this can't be an enum
5113 because of check_errors). */
5114/* The watchpoint has been deleted. */
5115#define WP_DELETED 1
5116/* The value has changed. */
5117#define WP_VALUE_CHANGED 2
5118/* The value has not changed. */
5119#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5120/* Ignore this watchpoint, no matter if the value changed or not. */
5121#define WP_IGNORE 4
c906108c
SS
5122
5123#define BP_TEMPFLAG 1
5124#define BP_HARDWAREFLAG 2
5125
4a64f543
MS
5126/* Evaluate watchpoint condition expression and check if its value
5127 changed.
553e4c11
JB
5128
5129 P should be a pointer to struct bpstat, but is defined as a void *
5130 in order for this function to be usable with catch_errors. */
c906108c
SS
5131
5132static int
4efb68b1 5133watchpoint_check (void *p)
c906108c
SS
5134{
5135 bpstat bs = (bpstat) p;
3a5c3e22 5136 struct watchpoint *b;
c906108c
SS
5137 struct frame_info *fr;
5138 int within_current_scope;
5139
f431efe5 5140 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5141 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5142 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5143
f6bc2008
PA
5144 /* If this is a local watchpoint, we only want to check if the
5145 watchpoint frame is in scope if the current thread is the thread
5146 that was used to create the watchpoint. */
5147 if (!watchpoint_in_thread_scope (b))
60e1c644 5148 return WP_IGNORE;
f6bc2008 5149
c906108c
SS
5150 if (b->exp_valid_block == NULL)
5151 within_current_scope = 1;
5152 else
5153 {
edb3359d
DJ
5154 struct frame_info *frame = get_current_frame ();
5155 struct gdbarch *frame_arch = get_frame_arch (frame);
5156 CORE_ADDR frame_pc = get_frame_pc (frame);
5157
c9cf6e20 5158 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5159 still in the function but the stack frame has already been
5160 invalidated. Since we can't rely on the values of local
5161 variables after the stack has been destroyed, we are treating
5162 the watchpoint in that state as `not changed' without further
5163 checking. Don't mark watchpoints as changed if the current
5164 frame is in an epilogue - even if they are in some other
5165 frame, our view of the stack is likely to be wrong and
5166 frame_find_by_id could error out. */
c9cf6e20 5167 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5168 return WP_IGNORE;
a0f49112 5169
101dcfbe 5170 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5171 within_current_scope = (fr != NULL);
69fbadd5
DJ
5172
5173 /* If we've gotten confused in the unwinder, we might have
5174 returned a frame that can't describe this variable. */
edb3359d
DJ
5175 if (within_current_scope)
5176 {
5177 struct symbol *function;
5178
5179 function = get_frame_function (fr);
5180 if (function == NULL
5181 || !contained_in (b->exp_valid_block,
5182 SYMBOL_BLOCK_VALUE (function)))
5183 within_current_scope = 0;
5184 }
69fbadd5 5185
edb3359d 5186 if (within_current_scope)
c906108c
SS
5187 /* If we end up stopping, the current frame will get selected
5188 in normal_stop. So this call to select_frame won't affect
5189 the user. */
0f7d239c 5190 select_frame (fr);
c906108c 5191 }
c5aa993b 5192
c906108c
SS
5193 if (within_current_scope)
5194 {
4a64f543
MS
5195 /* We use value_{,free_to_}mark because it could be a *long*
5196 time before we return to the command level and call
5197 free_all_values. We can't call free_all_values because we
5198 might be in the middle of evaluating a function call. */
c906108c 5199
0cf6dd15 5200 int pc = 0;
9c06b0b4 5201 struct value *mark;
fa4727a6
DJ
5202 struct value *new_val;
5203
3a5c3e22 5204 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5205 /* Since we don't know the exact trigger address (from
5206 stopped_data_address), just tell the user we've triggered
5207 a mask watchpoint. */
5208 return WP_VALUE_CHANGED;
5209
5210 mark = value_mark ();
4d01a485 5211 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5212
bb9d5f81
PP
5213 if (b->val_bitsize != 0)
5214 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5215
4a64f543
MS
5216 /* We use value_equal_contents instead of value_equal because
5217 the latter coerces an array to a pointer, thus comparing just
5218 the address of the array instead of its contents. This is
5219 not what we want. */
fa4727a6 5220 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5221 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5222 {
fa4727a6
DJ
5223 if (new_val != NULL)
5224 {
5225 release_value (new_val);
5226 value_free_to_mark (mark);
5227 }
c906108c
SS
5228 bs->old_val = b->val;
5229 b->val = new_val;
fa4727a6 5230 b->val_valid = 1;
c906108c
SS
5231 return WP_VALUE_CHANGED;
5232 }
5233 else
5234 {
60e1c644 5235 /* Nothing changed. */
c906108c 5236 value_free_to_mark (mark);
c906108c
SS
5237 return WP_VALUE_NOT_CHANGED;
5238 }
5239 }
5240 else
5241 {
5242 /* This seems like the only logical thing to do because
c5aa993b
JM
5243 if we temporarily ignored the watchpoint, then when
5244 we reenter the block in which it is valid it contains
5245 garbage (in the case of a function, it may have two
5246 garbage values, one before and one after the prologue).
5247 So we can't even detect the first assignment to it and
5248 watch after that (since the garbage may or may not equal
5249 the first value assigned). */
348d480f
PA
5250 /* We print all the stop information in
5251 breakpoint_ops->print_it, but in this case, by the time we
5252 call breakpoint_ops->print_it this bp will be deleted
5253 already. So we have no choice but print the information
5254 here. */
468afe6c 5255
0e454242 5256 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5257 {
5258 struct ui_out *uiout = current_uiout;
5259
5260 if (ui_out_is_mi_like_p (uiout))
5261 ui_out_field_string
5262 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5263 ui_out_text (uiout, "\nWatchpoint ");
5264 ui_out_field_int (uiout, "wpnum", b->base.number);
5265 ui_out_text (uiout,
5266 " deleted because the program has left the block in\n"
5267 "which its expression is valid.\n");
5268 }
4ce44c66 5269
cdac0397 5270 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5271 decref_counted_command_line (&b->base.commands);
d0fb5eae 5272 watchpoint_del_at_next_stop (b);
c906108c
SS
5273
5274 return WP_DELETED;
5275 }
5276}
5277
18a18393 5278/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5279 breakpoint location BL. This function does not check if we should
5280 stop, only if BL explains the stop. */
5281
18a18393 5282static int
6c95b8df 5283bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5284 struct address_space *aspace, CORE_ADDR bp_addr,
5285 const struct target_waitstatus *ws)
18a18393
VP
5286{
5287 struct breakpoint *b = bl->owner;
5288
348d480f 5289 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5290 gdb_assert (b != NULL);
5291
09ac7c10 5292 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5293}
5294
3a5c3e22
PA
5295/* Determine if the watched values have actually changed, and we
5296 should stop. If not, set BS->stop to 0. */
5297
18a18393
VP
5298static void
5299bpstat_check_watchpoint (bpstat bs)
5300{
2bdf28a0 5301 const struct bp_location *bl;
3a5c3e22 5302 struct watchpoint *b;
2bdf28a0
JK
5303
5304 /* BS is built for existing struct breakpoint. */
f431efe5 5305 bl = bs->bp_location_at;
2bdf28a0 5306 gdb_assert (bl != NULL);
3a5c3e22 5307 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5308 gdb_assert (b != NULL);
18a18393 5309
18a18393 5310 {
18a18393
VP
5311 int must_check_value = 0;
5312
3a5c3e22 5313 if (b->base.type == bp_watchpoint)
18a18393
VP
5314 /* For a software watchpoint, we must always check the
5315 watched value. */
5316 must_check_value = 1;
5317 else if (b->watchpoint_triggered == watch_triggered_yes)
5318 /* We have a hardware watchpoint (read, write, or access)
5319 and the target earlier reported an address watched by
5320 this watchpoint. */
5321 must_check_value = 1;
5322 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5323 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5324 /* We were stopped by a hardware watchpoint, but the target could
5325 not report the data address. We must check the watchpoint's
5326 value. Access and read watchpoints are out of luck; without
5327 a data address, we can't figure it out. */
5328 must_check_value = 1;
3a5c3e22 5329
18a18393
VP
5330 if (must_check_value)
5331 {
3e43a32a
MS
5332 char *message
5333 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5334 b->base.number);
18a18393
VP
5335 struct cleanup *cleanups = make_cleanup (xfree, message);
5336 int e = catch_errors (watchpoint_check, bs, message,
5337 RETURN_MASK_ALL);
5338 do_cleanups (cleanups);
5339 switch (e)
5340 {
5341 case WP_DELETED:
5342 /* We've already printed what needs to be printed. */
5343 bs->print_it = print_it_done;
5344 /* Stop. */
5345 break;
60e1c644
PA
5346 case WP_IGNORE:
5347 bs->print_it = print_it_noop;
5348 bs->stop = 0;
5349 break;
18a18393 5350 case WP_VALUE_CHANGED:
3a5c3e22 5351 if (b->base.type == bp_read_watchpoint)
18a18393 5352 {
85d721b8
PA
5353 /* There are two cases to consider here:
5354
4a64f543 5355 1. We're watching the triggered memory for reads.
85d721b8
PA
5356 In that case, trust the target, and always report
5357 the watchpoint hit to the user. Even though
5358 reads don't cause value changes, the value may
5359 have changed since the last time it was read, and
5360 since we're not trapping writes, we will not see
5361 those, and as such we should ignore our notion of
5362 old value.
5363
4a64f543 5364 2. We're watching the triggered memory for both
85d721b8
PA
5365 reads and writes. There are two ways this may
5366 happen:
5367
4a64f543 5368 2.1. This is a target that can't break on data
85d721b8
PA
5369 reads only, but can break on accesses (reads or
5370 writes), such as e.g., x86. We detect this case
5371 at the time we try to insert read watchpoints.
5372
4a64f543 5373 2.2. Otherwise, the target supports read
85d721b8
PA
5374 watchpoints, but, the user set an access or write
5375 watchpoint watching the same memory as this read
5376 watchpoint.
5377
5378 If we're watching memory writes as well as reads,
5379 ignore watchpoint hits when we find that the
5380 value hasn't changed, as reads don't cause
5381 changes. This still gives false positives when
5382 the program writes the same value to memory as
5383 what there was already in memory (we will confuse
5384 it for a read), but it's much better than
5385 nothing. */
5386
5387 int other_write_watchpoint = 0;
5388
5389 if (bl->watchpoint_type == hw_read)
5390 {
5391 struct breakpoint *other_b;
5392
5393 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5394 if (other_b->type == bp_hardware_watchpoint
5395 || other_b->type == bp_access_watchpoint)
85d721b8 5396 {
3a5c3e22
PA
5397 struct watchpoint *other_w =
5398 (struct watchpoint *) other_b;
5399
5400 if (other_w->watchpoint_triggered
5401 == watch_triggered_yes)
5402 {
5403 other_write_watchpoint = 1;
5404 break;
5405 }
85d721b8
PA
5406 }
5407 }
5408
5409 if (other_write_watchpoint
5410 || bl->watchpoint_type == hw_access)
5411 {
5412 /* We're watching the same memory for writes,
5413 and the value changed since the last time we
5414 updated it, so this trap must be for a write.
5415 Ignore it. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
18a18393
VP
5419 }
5420 break;
5421 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5422 if (b->base.type == bp_hardware_watchpoint
5423 || b->base.type == bp_watchpoint)
18a18393
VP
5424 {
5425 /* Don't stop: write watchpoints shouldn't fire if
5426 the value hasn't changed. */
5427 bs->print_it = print_it_noop;
5428 bs->stop = 0;
5429 }
5430 /* Stop. */
5431 break;
5432 default:
5433 /* Can't happen. */
5434 case 0:
5435 /* Error from catch_errors. */
468afe6c 5436 {
0e454242 5437 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5438 {
5439 printf_filtered (_("Watchpoint %d deleted.\n"),
5440 b->base.number);
5441 }
5442 watchpoint_del_at_next_stop (b);
5443 /* We've already printed what needs to be printed. */
5444 bs->print_it = print_it_done;
5445 }
18a18393
VP
5446 break;
5447 }
5448 }
5449 else /* must_check_value == 0 */
5450 {
5451 /* This is a case where some watchpoint(s) triggered, but
5452 not at the address of this watchpoint, or else no
5453 watchpoint triggered after all. So don't print
5454 anything for this watchpoint. */
5455 bs->print_it = print_it_noop;
5456 bs->stop = 0;
5457 }
5458 }
5459}
5460
7d4df6a4
DE
5461/* For breakpoints that are currently marked as telling gdb to stop,
5462 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5463 of breakpoint referred to by BS. If we should not stop for this
5464 breakpoint, set BS->stop to 0. */
f431efe5 5465
18a18393
VP
5466static void
5467bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5468{
2bdf28a0
JK
5469 const struct bp_location *bl;
5470 struct breakpoint *b;
7d4df6a4
DE
5471 int value_is_zero = 0;
5472 struct expression *cond;
5473
5474 gdb_assert (bs->stop);
2bdf28a0
JK
5475
5476 /* BS is built for existing struct breakpoint. */
f431efe5 5477 bl = bs->bp_location_at;
2bdf28a0 5478 gdb_assert (bl != NULL);
f431efe5 5479 b = bs->breakpoint_at;
2bdf28a0 5480 gdb_assert (b != NULL);
18a18393 5481
b775012e
LM
5482 /* Even if the target evaluated the condition on its end and notified GDB, we
5483 need to do so again since GDB does not know if we stopped due to a
5484 breakpoint or a single step breakpoint. */
5485
18a18393 5486 if (frame_id_p (b->frame_id)
edb3359d 5487 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5488 {
7d4df6a4
DE
5489 bs->stop = 0;
5490 return;
5491 }
60e1c644 5492
12ab52e9
PA
5493 /* If this is a thread/task-specific breakpoint, don't waste cpu
5494 evaluating the condition if this isn't the specified
5495 thread/task. */
5d5658a1 5496 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5497 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5498
6c1b0f7b
DE
5499 {
5500 bs->stop = 0;
5501 return;
5502 }
5503
6dddc817
DE
5504 /* Evaluate extension language breakpoints that have a "stop" method
5505 implemented. */
5506 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5507
7d4df6a4
DE
5508 if (is_watchpoint (b))
5509 {
5510 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5511
4d01a485 5512 cond = w->cond_exp.get ();
7d4df6a4
DE
5513 }
5514 else
4d01a485 5515 cond = bl->cond.get ();
60e1c644 5516
7d4df6a4
DE
5517 if (cond && b->disposition != disp_del_at_next_stop)
5518 {
5519 int within_current_scope = 1;
5520 struct watchpoint * w;
60e1c644 5521
7d4df6a4
DE
5522 /* We use value_mark and value_free_to_mark because it could
5523 be a long time before we return to the command level and
5524 call free_all_values. We can't call free_all_values
5525 because we might be in the middle of evaluating a
5526 function call. */
5527 struct value *mark = value_mark ();
5528
5529 if (is_watchpoint (b))
5530 w = (struct watchpoint *) b;
5531 else
5532 w = NULL;
5533
5534 /* Need to select the frame, with all that implies so that
5535 the conditions will have the right context. Because we
5536 use the frame, we will not see an inlined function's
5537 variables when we arrive at a breakpoint at the start
5538 of the inlined function; the current frame will be the
5539 call site. */
5540 if (w == NULL || w->cond_exp_valid_block == NULL)
5541 select_frame (get_current_frame ());
5542 else
18a18393 5543 {
7d4df6a4
DE
5544 struct frame_info *frame;
5545
5546 /* For local watchpoint expressions, which particular
5547 instance of a local is being watched matters, so we
5548 keep track of the frame to evaluate the expression
5549 in. To evaluate the condition however, it doesn't
5550 really matter which instantiation of the function
5551 where the condition makes sense triggers the
5552 watchpoint. This allows an expression like "watch
5553 global if q > 10" set in `func', catch writes to
5554 global on all threads that call `func', or catch
5555 writes on all recursive calls of `func' by a single
5556 thread. We simply always evaluate the condition in
5557 the innermost frame that's executing where it makes
5558 sense to evaluate the condition. It seems
5559 intuitive. */
5560 frame = block_innermost_frame (w->cond_exp_valid_block);
5561 if (frame != NULL)
5562 select_frame (frame);
5563 else
5564 within_current_scope = 0;
18a18393 5565 }
7d4df6a4
DE
5566 if (within_current_scope)
5567 value_is_zero
5568 = catch_errors (breakpoint_cond_eval, cond,
5569 "Error in testing breakpoint condition:\n",
5570 RETURN_MASK_ALL);
5571 else
18a18393 5572 {
7d4df6a4
DE
5573 warning (_("Watchpoint condition cannot be tested "
5574 "in the current scope"));
5575 /* If we failed to set the right context for this
5576 watchpoint, unconditionally report it. */
5577 value_is_zero = 0;
18a18393 5578 }
7d4df6a4
DE
5579 /* FIXME-someday, should give breakpoint #. */
5580 value_free_to_mark (mark);
18a18393 5581 }
7d4df6a4
DE
5582
5583 if (cond && value_is_zero)
5584 {
5585 bs->stop = 0;
5586 }
7d4df6a4
DE
5587 else if (b->ignore_count > 0)
5588 {
5589 b->ignore_count--;
5590 bs->stop = 0;
5591 /* Increase the hit count even though we don't stop. */
5592 ++(b->hit_count);
5593 observer_notify_breakpoint_modified (b);
5594 }
18a18393
VP
5595}
5596
1cf4d951
PA
5597/* Returns true if we need to track moribund locations of LOC's type
5598 on the current target. */
5599
5600static int
5601need_moribund_for_location_type (struct bp_location *loc)
5602{
5603 return ((loc->loc_type == bp_loc_software_breakpoint
5604 && !target_supports_stopped_by_sw_breakpoint ())
5605 || (loc->loc_type == bp_loc_hardware_breakpoint
5606 && !target_supports_stopped_by_hw_breakpoint ()));
5607}
5608
18a18393 5609
9709f61c 5610/* Get a bpstat associated with having just stopped at address
d983da9c 5611 BP_ADDR in thread PTID.
c906108c 5612
d983da9c 5613 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5614 don't understand this stop. Result is a chain of bpstat's such
5615 that:
c906108c 5616
c5aa993b 5617 if we don't understand the stop, the result is a null pointer.
c906108c 5618
c5aa993b 5619 if we understand why we stopped, the result is not null.
c906108c 5620
c5aa993b
JM
5621 Each element of the chain refers to a particular breakpoint or
5622 watchpoint at which we have stopped. (We may have stopped for
5623 several reasons concurrently.)
c906108c 5624
c5aa993b
JM
5625 Each element of the chain has valid next, breakpoint_at,
5626 commands, FIXME??? fields. */
c906108c
SS
5627
5628bpstat
6c95b8df 5629bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5630 CORE_ADDR bp_addr, ptid_t ptid,
5631 const struct target_waitstatus *ws)
c906108c 5632{
0d381245 5633 struct breakpoint *b = NULL;
afe38095 5634 struct bp_location *bl;
20874c92 5635 struct bp_location *loc;
5760d0ab
JK
5636 /* First item of allocated bpstat's. */
5637 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5638 /* Pointer to the last thing in the chain currently. */
5760d0ab 5639 bpstat bs;
20874c92 5640 int ix;
429374b8 5641 int need_remove_insert;
f431efe5 5642 int removed_any;
c906108c 5643
f431efe5
PA
5644 /* First, build the bpstat chain with locations that explain a
5645 target stop, while being careful to not set the target running,
5646 as that may invalidate locations (in particular watchpoint
5647 locations are recreated). Resuming will happen here with
5648 breakpoint conditions or watchpoint expressions that include
5649 inferior function calls. */
c5aa993b 5650
429374b8
JK
5651 ALL_BREAKPOINTS (b)
5652 {
1a853c52 5653 if (!breakpoint_enabled (b))
429374b8 5654 continue;
a5606eee 5655
429374b8
JK
5656 for (bl = b->loc; bl != NULL; bl = bl->next)
5657 {
4a64f543
MS
5658 /* For hardware watchpoints, we look only at the first
5659 location. The watchpoint_check function will work on the
5660 entire expression, not the individual locations. For
5661 read watchpoints, the watchpoints_triggered function has
5662 checked all locations already. */
429374b8
JK
5663 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5664 break;
18a18393 5665
f6592439 5666 if (!bl->enabled || bl->shlib_disabled)
429374b8 5667 continue;
c5aa993b 5668
09ac7c10 5669 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5670 continue;
c5aa993b 5671
4a64f543
MS
5672 /* Come here if it's a watchpoint, or if the break address
5673 matches. */
c5aa993b 5674
4a64f543
MS
5675 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5676 explain stop. */
c5aa993b 5677
f431efe5
PA
5678 /* Assume we stop. Should we find a watchpoint that is not
5679 actually triggered, or if the condition of the breakpoint
5680 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5681 bs->stop = 1;
5682 bs->print = 1;
d983da9c 5683
f431efe5
PA
5684 /* If this is a scope breakpoint, mark the associated
5685 watchpoint as triggered so that we will handle the
5686 out-of-scope event. We'll get to the watchpoint next
5687 iteration. */
d0fb5eae 5688 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5689 {
5690 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5691
5692 w->watchpoint_triggered = watch_triggered_yes;
5693 }
f431efe5
PA
5694 }
5695 }
5696
7c16b83e 5697 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5698 if (!target_supports_stopped_by_sw_breakpoint ()
5699 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5700 {
1cf4d951 5701 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5702 {
1cf4d951
PA
5703 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5704 && need_moribund_for_location_type (loc))
5705 {
5706 bs = bpstat_alloc (loc, &bs_link);
5707 /* For hits of moribund locations, we should just proceed. */
5708 bs->stop = 0;
5709 bs->print = 0;
5710 bs->print_it = print_it_noop;
5711 }
f431efe5
PA
5712 }
5713 }
5714
edcc5120
TT
5715 /* A bit of special processing for shlib breakpoints. We need to
5716 process solib loading here, so that the lists of loaded and
5717 unloaded libraries are correct before we handle "catch load" and
5718 "catch unload". */
5719 for (bs = bs_head; bs != NULL; bs = bs->next)
5720 {
5d268276 5721 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5722 {
5723 handle_solib_event ();
5724 break;
5725 }
5726 }
5727
f431efe5
PA
5728 /* Now go through the locations that caused the target to stop, and
5729 check whether we're interested in reporting this stop to higher
5730 layers, or whether we should resume the target transparently. */
5731
5732 removed_any = 0;
5733
5760d0ab 5734 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5735 {
5736 if (!bs->stop)
5737 continue;
5738
f431efe5 5739 b = bs->breakpoint_at;
348d480f
PA
5740 b->ops->check_status (bs);
5741 if (bs->stop)
28010a5d 5742 {
348d480f 5743 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5744
429374b8
JK
5745 if (bs->stop)
5746 {
5747 ++(b->hit_count);
8d3788bd 5748 observer_notify_breakpoint_modified (b);
c906108c 5749
4a64f543 5750 /* We will stop here. */
429374b8
JK
5751 if (b->disposition == disp_disable)
5752 {
816338b5 5753 --(b->enable_count);
1a853c52 5754 if (b->enable_count <= 0)
429374b8 5755 b->enable_state = bp_disabled;
f431efe5 5756 removed_any = 1;
429374b8
JK
5757 }
5758 if (b->silent)
5759 bs->print = 0;
5760 bs->commands = b->commands;
9add0f1b 5761 incref_counted_command_line (bs->commands);
abf85f46
JK
5762 if (command_line_is_silent (bs->commands
5763 ? bs->commands->commands : NULL))
5764 bs->print = 0;
9d6e6e84
HZ
5765
5766 b->ops->after_condition_true (bs);
429374b8
JK
5767 }
5768
348d480f 5769 }
a9b3a50f
PA
5770
5771 /* Print nothing for this entry if we don't stop or don't
5772 print. */
5773 if (!bs->stop || !bs->print)
5774 bs->print_it = print_it_noop;
429374b8 5775 }
876fa593 5776
d983da9c
DJ
5777 /* If we aren't stopping, the value of some hardware watchpoint may
5778 not have changed, but the intermediate memory locations we are
5779 watching may have. Don't bother if we're stopping; this will get
5780 done later. */
d832cb68 5781 need_remove_insert = 0;
5760d0ab
JK
5782 if (! bpstat_causes_stop (bs_head))
5783 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5784 if (!bs->stop
f431efe5
PA
5785 && bs->breakpoint_at
5786 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5787 {
3a5c3e22
PA
5788 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5789
5790 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5791 need_remove_insert = 1;
d983da9c
DJ
5792 }
5793
d832cb68 5794 if (need_remove_insert)
44702360 5795 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5796 else if (removed_any)
44702360 5797 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5798
5760d0ab 5799 return bs_head;
c906108c 5800}
628fe4e4
JK
5801
5802static void
5803handle_jit_event (void)
5804{
5805 struct frame_info *frame;
5806 struct gdbarch *gdbarch;
5807
243a9253
PA
5808 if (debug_infrun)
5809 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5810
628fe4e4
JK
5811 /* Switch terminal for any messages produced by
5812 breakpoint_re_set. */
5813 target_terminal_ours_for_output ();
5814
5815 frame = get_current_frame ();
5816 gdbarch = get_frame_arch (frame);
5817
5818 jit_event_handler (gdbarch);
5819
5820 target_terminal_inferior ();
5821}
5822
5823/* Prepare WHAT final decision for infrun. */
5824
5825/* Decide what infrun needs to do with this bpstat. */
5826
c906108c 5827struct bpstat_what
0e30163f 5828bpstat_what (bpstat bs_head)
c906108c 5829{
c906108c 5830 struct bpstat_what retval;
0e30163f 5831 bpstat bs;
c906108c 5832
628fe4e4 5833 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5834 retval.call_dummy = STOP_NONE;
186c406b 5835 retval.is_longjmp = 0;
628fe4e4 5836
0e30163f 5837 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5838 {
628fe4e4
JK
5839 /* Extract this BS's action. After processing each BS, we check
5840 if its action overrides all we've seem so far. */
5841 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5842 enum bptype bptype;
5843
c906108c 5844 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5845 {
5846 /* I suspect this can happen if it was a momentary
5847 breakpoint which has since been deleted. */
5848 bptype = bp_none;
5849 }
20874c92 5850 else
f431efe5 5851 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5852
5853 switch (bptype)
c906108c
SS
5854 {
5855 case bp_none:
628fe4e4 5856 break;
c906108c
SS
5857 case bp_breakpoint:
5858 case bp_hardware_breakpoint:
7c16b83e 5859 case bp_single_step:
c906108c
SS
5860 case bp_until:
5861 case bp_finish:
a9b3a50f 5862 case bp_shlib_event:
c906108c
SS
5863 if (bs->stop)
5864 {
5865 if (bs->print)
628fe4e4 5866 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5867 else
628fe4e4 5868 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5869 }
5870 else
628fe4e4 5871 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5872 break;
5873 case bp_watchpoint:
5874 case bp_hardware_watchpoint:
5875 case bp_read_watchpoint:
5876 case bp_access_watchpoint:
5877 if (bs->stop)
5878 {
5879 if (bs->print)
628fe4e4 5880 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5881 else
628fe4e4 5882 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5883 }
5884 else
628fe4e4
JK
5885 {
5886 /* There was a watchpoint, but we're not stopping.
5887 This requires no further action. */
5888 }
c906108c
SS
5889 break;
5890 case bp_longjmp:
e2e4d78b 5891 case bp_longjmp_call_dummy:
186c406b 5892 case bp_exception:
0a39bb32
PA
5893 if (bs->stop)
5894 {
5895 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5896 retval.is_longjmp = bptype != bp_exception;
5897 }
5898 else
5899 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5900 break;
5901 case bp_longjmp_resume:
186c406b 5902 case bp_exception_resume:
0a39bb32
PA
5903 if (bs->stop)
5904 {
5905 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5906 retval.is_longjmp = bptype == bp_longjmp_resume;
5907 }
5908 else
5909 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5910 break;
5911 case bp_step_resume:
5912 if (bs->stop)
628fe4e4
JK
5913 this_action = BPSTAT_WHAT_STEP_RESUME;
5914 else
c906108c 5915 {
628fe4e4
JK
5916 /* It is for the wrong frame. */
5917 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5918 }
c906108c 5919 break;
2c03e5be
PA
5920 case bp_hp_step_resume:
5921 if (bs->stop)
5922 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5923 else
5924 {
5925 /* It is for the wrong frame. */
5926 this_action = BPSTAT_WHAT_SINGLE;
5927 }
5928 break;
c906108c 5929 case bp_watchpoint_scope:
c4093a6a 5930 case bp_thread_event:
1900040c 5931 case bp_overlay_event:
0fd8e87f 5932 case bp_longjmp_master:
aa7d318d 5933 case bp_std_terminate_master:
186c406b 5934 case bp_exception_master:
628fe4e4 5935 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5936 break;
ce78b96d 5937 case bp_catchpoint:
c5aa993b
JM
5938 if (bs->stop)
5939 {
5940 if (bs->print)
628fe4e4 5941 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5942 else
628fe4e4 5943 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5944 }
5945 else
628fe4e4
JK
5946 {
5947 /* There was a catchpoint, but we're not stopping.
5948 This requires no further action. */
5949 }
5950 break;
628fe4e4 5951 case bp_jit_event:
628fe4e4 5952 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5953 break;
c906108c 5954 case bp_call_dummy:
53a5351d
JM
5955 /* Make sure the action is stop (silent or noisy),
5956 so infrun.c pops the dummy frame. */
aa7d318d 5957 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5958 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5959 break;
5960 case bp_std_terminate:
5961 /* Make sure the action is stop (silent or noisy),
5962 so infrun.c pops the dummy frame. */
aa7d318d 5963 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5964 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5965 break;
1042e4c0 5966 case bp_tracepoint:
7a697b8d 5967 case bp_fast_tracepoint:
0fb4aa4b 5968 case bp_static_tracepoint:
1042e4c0
SS
5969 /* Tracepoint hits should not be reported back to GDB, and
5970 if one got through somehow, it should have been filtered
5971 out already. */
5972 internal_error (__FILE__, __LINE__,
7a697b8d 5973 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5974 break;
5975 case bp_gnu_ifunc_resolver:
5976 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5977 this_action = BPSTAT_WHAT_SINGLE;
5978 break;
5979 case bp_gnu_ifunc_resolver_return:
5980 /* The breakpoint will be removed, execution will restart from the
5981 PC of the former breakpoint. */
5982 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5983 break;
e7e0cddf
SS
5984
5985 case bp_dprintf:
a11cfd87
HZ
5986 if (bs->stop)
5987 this_action = BPSTAT_WHAT_STOP_SILENT;
5988 else
5989 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5990 break;
5991
628fe4e4
JK
5992 default:
5993 internal_error (__FILE__, __LINE__,
5994 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5995 }
628fe4e4 5996
325fac50 5997 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5998 }
628fe4e4 5999
243a9253
PA
6000 return retval;
6001}
628fe4e4 6002
243a9253
PA
6003void
6004bpstat_run_callbacks (bpstat bs_head)
6005{
6006 bpstat bs;
628fe4e4 6007
0e30163f
JK
6008 for (bs = bs_head; bs != NULL; bs = bs->next)
6009 {
6010 struct breakpoint *b = bs->breakpoint_at;
6011
6012 if (b == NULL)
6013 continue;
6014 switch (b->type)
6015 {
243a9253
PA
6016 case bp_jit_event:
6017 handle_jit_event ();
6018 break;
0e30163f
JK
6019 case bp_gnu_ifunc_resolver:
6020 gnu_ifunc_resolver_stop (b);
6021 break;
6022 case bp_gnu_ifunc_resolver_return:
6023 gnu_ifunc_resolver_return_stop (b);
6024 break;
6025 }
6026 }
c906108c
SS
6027}
6028
6029/* Nonzero if we should step constantly (e.g. watchpoints on machines
6030 without hardware support). This isn't related to a specific bpstat,
6031 just to things like whether watchpoints are set. */
6032
c5aa993b 6033int
fba45db2 6034bpstat_should_step (void)
c906108c
SS
6035{
6036 struct breakpoint *b;
cc59ec59 6037
c906108c 6038 ALL_BREAKPOINTS (b)
717a8278 6039 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6040 return 1;
c906108c
SS
6041 return 0;
6042}
6043
67822962
PA
6044int
6045bpstat_causes_stop (bpstat bs)
6046{
6047 for (; bs != NULL; bs = bs->next)
6048 if (bs->stop)
6049 return 1;
6050
6051 return 0;
6052}
6053
c906108c 6054\f
c5aa993b 6055
170b53b2
UW
6056/* Compute a string of spaces suitable to indent the next line
6057 so it starts at the position corresponding to the table column
6058 named COL_NAME in the currently active table of UIOUT. */
6059
6060static char *
6061wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6062{
6063 static char wrap_indent[80];
6064 int i, total_width, width, align;
6065 char *text;
6066
6067 total_width = 0;
6068 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6069 {
6070 if (strcmp (text, col_name) == 0)
6071 {
6072 gdb_assert (total_width < sizeof wrap_indent);
6073 memset (wrap_indent, ' ', total_width);
6074 wrap_indent[total_width] = 0;
6075
6076 return wrap_indent;
6077 }
6078
6079 total_width += width + 1;
6080 }
6081
6082 return NULL;
6083}
6084
b775012e
LM
6085/* Determine if the locations of this breakpoint will have their conditions
6086 evaluated by the target, host or a mix of both. Returns the following:
6087
6088 "host": Host evals condition.
6089 "host or target": Host or Target evals condition.
6090 "target": Target evals condition.
6091*/
6092
6093static const char *
6094bp_condition_evaluator (struct breakpoint *b)
6095{
6096 struct bp_location *bl;
6097 char host_evals = 0;
6098 char target_evals = 0;
6099
6100 if (!b)
6101 return NULL;
6102
6103 if (!is_breakpoint (b))
6104 return NULL;
6105
6106 if (gdb_evaluates_breakpoint_condition_p ()
6107 || !target_supports_evaluation_of_breakpoint_conditions ())
6108 return condition_evaluation_host;
6109
6110 for (bl = b->loc; bl; bl = bl->next)
6111 {
6112 if (bl->cond_bytecode)
6113 target_evals++;
6114 else
6115 host_evals++;
6116 }
6117
6118 if (host_evals && target_evals)
6119 return condition_evaluation_both;
6120 else if (target_evals)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124}
6125
6126/* Determine the breakpoint location's condition evaluator. This is
6127 similar to bp_condition_evaluator, but for locations. */
6128
6129static const char *
6130bp_location_condition_evaluator (struct bp_location *bl)
6131{
6132 if (bl && !is_breakpoint (bl->owner))
6133 return NULL;
6134
6135 if (gdb_evaluates_breakpoint_condition_p ()
6136 || !target_supports_evaluation_of_breakpoint_conditions ())
6137 return condition_evaluation_host;
6138
6139 if (bl && bl->cond_bytecode)
6140 return condition_evaluation_target;
6141 else
6142 return condition_evaluation_host;
6143}
6144
859825b8
JK
6145/* Print the LOC location out of the list of B->LOC locations. */
6146
170b53b2
UW
6147static void
6148print_breakpoint_location (struct breakpoint *b,
6149 struct bp_location *loc)
0d381245 6150{
79a45e25 6151 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6152 struct cleanup *old_chain = save_current_program_space ();
6153
859825b8
JK
6154 if (loc != NULL && loc->shlib_disabled)
6155 loc = NULL;
6156
6c95b8df
PA
6157 if (loc != NULL)
6158 set_current_program_space (loc->pspace);
6159
56435ebe 6160 if (b->display_canonical)
f00aae0f
KS
6161 ui_out_field_string (uiout, "what",
6162 event_location_to_string (b->location));
2f202fde 6163 else if (loc && loc->symtab)
0d381245
VP
6164 {
6165 struct symbol *sym
6166 = find_pc_sect_function (loc->address, loc->section);
6167 if (sym)
6168 {
6169 ui_out_text (uiout, "in ");
6170 ui_out_field_string (uiout, "func",
6171 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6172 ui_out_text (uiout, " ");
6173 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6174 ui_out_text (uiout, "at ");
0d381245 6175 }
05cba821
JK
6176 ui_out_field_string (uiout, "file",
6177 symtab_to_filename_for_display (loc->symtab));
0d381245 6178 ui_out_text (uiout, ":");
05cba821 6179
0d381245 6180 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6181 ui_out_field_string (uiout, "fullname",
6182 symtab_to_fullname (loc->symtab));
0d381245 6183
f8eba3c6 6184 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6185 }
859825b8 6186 else if (loc)
0d381245 6187 {
f99d8bf4
PA
6188 struct ui_file *stb = mem_fileopen ();
6189 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6190
f99d8bf4 6191 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6192 demangle, "");
0d381245 6193 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6194
6195 do_cleanups (stb_chain);
0d381245 6196 }
859825b8 6197 else
f00aae0f
KS
6198 {
6199 ui_out_field_string (uiout, "pending",
6200 event_location_to_string (b->location));
6201 /* If extra_string is available, it could be holding a condition
6202 or dprintf arguments. In either case, make sure it is printed,
6203 too, but only for non-MI streams. */
6204 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6205 {
6206 if (b->type == bp_dprintf)
6207 ui_out_text (uiout, ",");
6208 else
6209 ui_out_text (uiout, " ");
6210 ui_out_text (uiout, b->extra_string);
6211 }
6212 }
6c95b8df 6213
b775012e
LM
6214 if (loc && is_breakpoint (b)
6215 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6216 && bp_condition_evaluator (b) == condition_evaluation_both)
6217 {
6218 ui_out_text (uiout, " (");
6219 ui_out_field_string (uiout, "evaluated-by",
6220 bp_location_condition_evaluator (loc));
6221 ui_out_text (uiout, ")");
6222 }
6223
6c95b8df 6224 do_cleanups (old_chain);
0d381245
VP
6225}
6226
269b11a2
PA
6227static const char *
6228bptype_string (enum bptype type)
c906108c 6229{
c4093a6a
JM
6230 struct ep_type_description
6231 {
6232 enum bptype type;
6233 char *description;
6234 };
6235 static struct ep_type_description bptypes[] =
c906108c 6236 {
c5aa993b
JM
6237 {bp_none, "?deleted?"},
6238 {bp_breakpoint, "breakpoint"},
c906108c 6239 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6240 {bp_single_step, "sw single-step"},
c5aa993b
JM
6241 {bp_until, "until"},
6242 {bp_finish, "finish"},
6243 {bp_watchpoint, "watchpoint"},
c906108c 6244 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6245 {bp_read_watchpoint, "read watchpoint"},
6246 {bp_access_watchpoint, "acc watchpoint"},
6247 {bp_longjmp, "longjmp"},
6248 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6249 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6250 {bp_exception, "exception"},
6251 {bp_exception_resume, "exception resume"},
c5aa993b 6252 {bp_step_resume, "step resume"},
2c03e5be 6253 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6254 {bp_watchpoint_scope, "watchpoint scope"},
6255 {bp_call_dummy, "call dummy"},
aa7d318d 6256 {bp_std_terminate, "std::terminate"},
c5aa993b 6257 {bp_shlib_event, "shlib events"},
c4093a6a 6258 {bp_thread_event, "thread events"},
1900040c 6259 {bp_overlay_event, "overlay events"},
0fd8e87f 6260 {bp_longjmp_master, "longjmp master"},
aa7d318d 6261 {bp_std_terminate_master, "std::terminate master"},
186c406b 6262 {bp_exception_master, "exception master"},
ce78b96d 6263 {bp_catchpoint, "catchpoint"},
1042e4c0 6264 {bp_tracepoint, "tracepoint"},
7a697b8d 6265 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6266 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6267 {bp_dprintf, "dprintf"},
4efc6507 6268 {bp_jit_event, "jit events"},
0e30163f
JK
6269 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6270 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6271 };
269b11a2
PA
6272
6273 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6274 || ((int) type != bptypes[(int) type].type))
6275 internal_error (__FILE__, __LINE__,
6276 _("bptypes table does not describe type #%d."),
6277 (int) type);
6278
6279 return bptypes[(int) type].description;
6280}
6281
998580f1
MK
6282/* For MI, output a field named 'thread-groups' with a list as the value.
6283 For CLI, prefix the list with the string 'inf'. */
6284
6285static void
6286output_thread_groups (struct ui_out *uiout,
6287 const char *field_name,
6288 VEC(int) *inf_num,
6289 int mi_only)
6290{
752eb8b4 6291 struct cleanup *back_to;
998580f1
MK
6292 int is_mi = ui_out_is_mi_like_p (uiout);
6293 int inf;
6294 int i;
6295
6296 /* For backward compatibility, don't display inferiors in CLI unless
6297 there are several. Always display them for MI. */
6298 if (!is_mi && mi_only)
6299 return;
6300
752eb8b4
TT
6301 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6302
998580f1
MK
6303 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6304 {
6305 if (is_mi)
6306 {
6307 char mi_group[10];
6308
6309 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6310 ui_out_field_string (uiout, NULL, mi_group);
6311 }
6312 else
6313 {
6314 if (i == 0)
6315 ui_out_text (uiout, " inf ");
6316 else
6317 ui_out_text (uiout, ", ");
6318
6319 ui_out_text (uiout, plongest (inf));
6320 }
6321 }
6322
6323 do_cleanups (back_to);
6324}
6325
269b11a2
PA
6326/* Print B to gdb_stdout. */
6327
6328static void
6329print_one_breakpoint_location (struct breakpoint *b,
6330 struct bp_location *loc,
6331 int loc_number,
6332 struct bp_location **last_loc,
269b11a2
PA
6333 int allflag)
6334{
6335 struct command_line *l;
c2c6d25f 6336 static char bpenables[] = "nynny";
c906108c 6337
79a45e25 6338 struct ui_out *uiout = current_uiout;
0d381245
VP
6339 int header_of_multiple = 0;
6340 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6341 struct value_print_options opts;
6342
6343 get_user_print_options (&opts);
0d381245
VP
6344
6345 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6346 /* See comment in print_one_breakpoint concerning treatment of
6347 breakpoints with single disabled location. */
0d381245
VP
6348 if (loc == NULL
6349 && (b->loc != NULL
6350 && (b->loc->next != NULL || !b->loc->enabled)))
6351 header_of_multiple = 1;
6352 if (loc == NULL)
6353 loc = b->loc;
6354
c4093a6a
JM
6355 annotate_record ();
6356
6357 /* 1 */
6358 annotate_field (0);
0d381245
VP
6359 if (part_of_multiple)
6360 {
6361 char *formatted;
0c6773c1 6362 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6363 ui_out_field_string (uiout, "number", formatted);
6364 xfree (formatted);
6365 }
6366 else
6367 {
6368 ui_out_field_int (uiout, "number", b->number);
6369 }
c4093a6a
JM
6370
6371 /* 2 */
6372 annotate_field (1);
0d381245
VP
6373 if (part_of_multiple)
6374 ui_out_field_skip (uiout, "type");
269b11a2
PA
6375 else
6376 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6377
6378 /* 3 */
6379 annotate_field (2);
0d381245
VP
6380 if (part_of_multiple)
6381 ui_out_field_skip (uiout, "disp");
6382 else
2cec12e5 6383 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6384
c4093a6a
JM
6385
6386 /* 4 */
6387 annotate_field (3);
0d381245 6388 if (part_of_multiple)
54e52265 6389 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6390 else
4a64f543
MS
6391 ui_out_field_fmt (uiout, "enabled", "%c",
6392 bpenables[(int) b->enable_state]);
54e52265 6393 ui_out_spaces (uiout, 2);
0d381245 6394
c4093a6a
JM
6395
6396 /* 5 and 6 */
3086aeae 6397 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6398 {
4a64f543
MS
6399 /* Although the print_one can possibly print all locations,
6400 calling it here is not likely to get any nice result. So,
6401 make sure there's just one location. */
0d381245 6402 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6403 b->ops->print_one (b, last_loc);
0d381245 6404 }
3086aeae
DJ
6405 else
6406 switch (b->type)
6407 {
6408 case bp_none:
6409 internal_error (__FILE__, __LINE__,
e2e0b3e5 6410 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6411 break;
c906108c 6412
3086aeae
DJ
6413 case bp_watchpoint:
6414 case bp_hardware_watchpoint:
6415 case bp_read_watchpoint:
6416 case bp_access_watchpoint:
3a5c3e22
PA
6417 {
6418 struct watchpoint *w = (struct watchpoint *) b;
6419
6420 /* Field 4, the address, is omitted (which makes the columns
6421 not line up too nicely with the headers, but the effect
6422 is relatively readable). */
6423 if (opts.addressprint)
6424 ui_out_field_skip (uiout, "addr");
6425 annotate_field (5);
6426 ui_out_field_string (uiout, "what", w->exp_string);
6427 }
3086aeae
DJ
6428 break;
6429
3086aeae
DJ
6430 case bp_breakpoint:
6431 case bp_hardware_breakpoint:
7c16b83e 6432 case bp_single_step:
3086aeae
DJ
6433 case bp_until:
6434 case bp_finish:
6435 case bp_longjmp:
6436 case bp_longjmp_resume:
e2e4d78b 6437 case bp_longjmp_call_dummy:
186c406b
TT
6438 case bp_exception:
6439 case bp_exception_resume:
3086aeae 6440 case bp_step_resume:
2c03e5be 6441 case bp_hp_step_resume:
3086aeae
DJ
6442 case bp_watchpoint_scope:
6443 case bp_call_dummy:
aa7d318d 6444 case bp_std_terminate:
3086aeae
DJ
6445 case bp_shlib_event:
6446 case bp_thread_event:
6447 case bp_overlay_event:
0fd8e87f 6448 case bp_longjmp_master:
aa7d318d 6449 case bp_std_terminate_master:
186c406b 6450 case bp_exception_master:
1042e4c0 6451 case bp_tracepoint:
7a697b8d 6452 case bp_fast_tracepoint:
0fb4aa4b 6453 case bp_static_tracepoint:
e7e0cddf 6454 case bp_dprintf:
4efc6507 6455 case bp_jit_event:
0e30163f
JK
6456 case bp_gnu_ifunc_resolver:
6457 case bp_gnu_ifunc_resolver_return:
79a45b7d 6458 if (opts.addressprint)
3086aeae
DJ
6459 {
6460 annotate_field (4);
54e52265 6461 if (header_of_multiple)
0d381245 6462 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6463 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6464 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6465 else
5af949e3
UW
6466 ui_out_field_core_addr (uiout, "addr",
6467 loc->gdbarch, loc->address);
3086aeae
DJ
6468 }
6469 annotate_field (5);
0d381245 6470 if (!header_of_multiple)
170b53b2 6471 print_breakpoint_location (b, loc);
0d381245 6472 if (b->loc)
a6d9a66e 6473 *last_loc = b->loc;
3086aeae
DJ
6474 break;
6475 }
c906108c 6476
6c95b8df 6477
998580f1 6478 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6479 {
6480 struct inferior *inf;
998580f1
MK
6481 VEC(int) *inf_num = NULL;
6482 int mi_only = 1;
6c95b8df 6483
998580f1 6484 ALL_INFERIORS (inf)
6c95b8df
PA
6485 {
6486 if (inf->pspace == loc->pspace)
998580f1 6487 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6488 }
998580f1
MK
6489
6490 /* For backward compatibility, don't display inferiors in CLI unless
6491 there are several. Always display for MI. */
6492 if (allflag
6493 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6494 && (number_of_program_spaces () > 1
6495 || number_of_inferiors () > 1)
6496 /* LOC is for existing B, it cannot be in
6497 moribund_locations and thus having NULL OWNER. */
6498 && loc->owner->type != bp_catchpoint))
6499 mi_only = 0;
6500 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6501 VEC_free (int, inf_num);
6c95b8df
PA
6502 }
6503
4a306c9a 6504 if (!part_of_multiple)
c4093a6a 6505 {
4a306c9a
JB
6506 if (b->thread != -1)
6507 {
6508 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6509 "stop only in" line a little further down. */
4a306c9a
JB
6510 ui_out_text (uiout, " thread ");
6511 ui_out_field_int (uiout, "thread", b->thread);
6512 }
6513 else if (b->task != 0)
6514 {
6515 ui_out_text (uiout, " task ");
6516 ui_out_field_int (uiout, "task", b->task);
6517 }
c4093a6a 6518 }
f1310107 6519
8b93c638 6520 ui_out_text (uiout, "\n");
f1310107 6521
348d480f 6522 if (!part_of_multiple)
f1310107
TJB
6523 b->ops->print_one_detail (b, uiout);
6524
0d381245 6525 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6526 {
6527 annotate_field (6);
8b93c638 6528 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6529 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6530 the frame ID. */
5af949e3
UW
6531 ui_out_field_core_addr (uiout, "frame",
6532 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6533 ui_out_text (uiout, "\n");
c4093a6a
JM
6534 }
6535
28010a5d 6536 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6537 {
6538 annotate_field (7);
d77f58be 6539 if (is_tracepoint (b))
1042e4c0
SS
6540 ui_out_text (uiout, "\ttrace only if ");
6541 else
6542 ui_out_text (uiout, "\tstop only if ");
0101ce28 6543 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6544
6545 /* Print whether the target is doing the breakpoint's condition
6546 evaluation. If GDB is doing the evaluation, don't print anything. */
6547 if (is_breakpoint (b)
6548 && breakpoint_condition_evaluation_mode ()
6549 == condition_evaluation_target)
6550 {
6551 ui_out_text (uiout, " (");
6552 ui_out_field_string (uiout, "evaluated-by",
6553 bp_condition_evaluator (b));
6554 ui_out_text (uiout, " evals)");
6555 }
0101ce28
JJ
6556 ui_out_text (uiout, "\n");
6557 }
6558
0d381245 6559 if (!part_of_multiple && b->thread != -1)
c4093a6a 6560 {
4a64f543 6561 /* FIXME should make an annotation for this. */
8b93c638 6562 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6563 if (ui_out_is_mi_like_p (uiout))
6564 ui_out_field_int (uiout, "thread", b->thread);
6565 else
6566 {
6567 struct thread_info *thr = find_thread_global_id (b->thread);
6568
6569 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6570 }
8b93c638 6571 ui_out_text (uiout, "\n");
c4093a6a
JM
6572 }
6573
556ec64d
YQ
6574 if (!part_of_multiple)
6575 {
6576 if (b->hit_count)
31f56a27
YQ
6577 {
6578 /* FIXME should make an annotation for this. */
6579 if (is_catchpoint (b))
6580 ui_out_text (uiout, "\tcatchpoint");
6581 else if (is_tracepoint (b))
6582 ui_out_text (uiout, "\ttracepoint");
6583 else
6584 ui_out_text (uiout, "\tbreakpoint");
6585 ui_out_text (uiout, " already hit ");
6586 ui_out_field_int (uiout, "times", b->hit_count);
6587 if (b->hit_count == 1)
6588 ui_out_text (uiout, " time\n");
6589 else
6590 ui_out_text (uiout, " times\n");
6591 }
556ec64d
YQ
6592 else
6593 {
31f56a27
YQ
6594 /* Output the count also if it is zero, but only if this is mi. */
6595 if (ui_out_is_mi_like_p (uiout))
6596 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6597 }
6598 }
8b93c638 6599
0d381245 6600 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6601 {
6602 annotate_field (8);
8b93c638
JM
6603 ui_out_text (uiout, "\tignore next ");
6604 ui_out_field_int (uiout, "ignore", b->ignore_count);
6605 ui_out_text (uiout, " hits\n");
c4093a6a 6606 }
059fb39f 6607
816338b5
SS
6608 /* Note that an enable count of 1 corresponds to "enable once"
6609 behavior, which is reported by the combination of enablement and
6610 disposition, so we don't need to mention it here. */
6611 if (!part_of_multiple && b->enable_count > 1)
6612 {
6613 annotate_field (8);
6614 ui_out_text (uiout, "\tdisable after ");
6615 /* Tweak the wording to clarify that ignore and enable counts
6616 are distinct, and have additive effect. */
6617 if (b->ignore_count)
6618 ui_out_text (uiout, "additional ");
6619 else
6620 ui_out_text (uiout, "next ");
6621 ui_out_field_int (uiout, "enable", b->enable_count);
6622 ui_out_text (uiout, " hits\n");
6623 }
6624
f196051f
SS
6625 if (!part_of_multiple && is_tracepoint (b))
6626 {
6627 struct tracepoint *tp = (struct tracepoint *) b;
6628
6629 if (tp->traceframe_usage)
6630 {
6631 ui_out_text (uiout, "\ttrace buffer usage ");
6632 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6633 ui_out_text (uiout, " bytes\n");
6634 }
6635 }
d3ce09f5 6636
9add0f1b 6637 l = b->commands ? b->commands->commands : NULL;
059fb39f 6638 if (!part_of_multiple && l)
c4093a6a 6639 {
3b31d625
EZ
6640 struct cleanup *script_chain;
6641
c4093a6a 6642 annotate_field (9);
3b31d625 6643 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6644 print_command_lines (uiout, l, 4);
3b31d625 6645 do_cleanups (script_chain);
c4093a6a 6646 }
d24317b4 6647
d9b3f62e 6648 if (is_tracepoint (b))
1042e4c0 6649 {
d9b3f62e
PA
6650 struct tracepoint *t = (struct tracepoint *) b;
6651
6652 if (!part_of_multiple && t->pass_count)
6653 {
6654 annotate_field (10);
6655 ui_out_text (uiout, "\tpass count ");
6656 ui_out_field_int (uiout, "pass", t->pass_count);
6657 ui_out_text (uiout, " \n");
6658 }
f2a8bc8a
YQ
6659
6660 /* Don't display it when tracepoint or tracepoint location is
6661 pending. */
6662 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6663 {
6664 annotate_field (11);
6665
6666 if (ui_out_is_mi_like_p (uiout))
6667 ui_out_field_string (uiout, "installed",
6668 loc->inserted ? "y" : "n");
6669 else
6670 {
6671 if (loc->inserted)
6672 ui_out_text (uiout, "\t");
6673 else
6674 ui_out_text (uiout, "\tnot ");
6675 ui_out_text (uiout, "installed on target\n");
6676 }
6677 }
1042e4c0
SS
6678 }
6679
d24317b4
VP
6680 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6681 {
3a5c3e22
PA
6682 if (is_watchpoint (b))
6683 {
6684 struct watchpoint *w = (struct watchpoint *) b;
6685
6686 ui_out_field_string (uiout, "original-location", w->exp_string);
6687 }
f00aae0f
KS
6688 else if (b->location != NULL
6689 && event_location_to_string (b->location) != NULL)
6690 ui_out_field_string (uiout, "original-location",
6691 event_location_to_string (b->location));
d24317b4 6692 }
c4093a6a 6693}
c5aa993b 6694
0d381245
VP
6695static void
6696print_one_breakpoint (struct breakpoint *b,
4a64f543 6697 struct bp_location **last_loc,
6c95b8df 6698 int allflag)
0d381245 6699{
8d3788bd 6700 struct cleanup *bkpt_chain;
79a45e25 6701 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6702
6703 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6704
12c5a436 6705 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6706 do_cleanups (bkpt_chain);
0d381245
VP
6707
6708 /* If this breakpoint has custom print function,
6709 it's already printed. Otherwise, print individual
6710 locations, if any. */
6711 if (b->ops == NULL || b->ops->print_one == NULL)
6712 {
4a64f543
MS
6713 /* If breakpoint has a single location that is disabled, we
6714 print it as if it had several locations, since otherwise it's
6715 hard to represent "breakpoint enabled, location disabled"
6716 situation.
6717
6718 Note that while hardware watchpoints have several locations
a3be7890 6719 internally, that's not a property exposed to user. */
0d381245 6720 if (b->loc
a5606eee 6721 && !is_hardware_watchpoint (b)
8d3788bd 6722 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6723 {
6724 struct bp_location *loc;
6725 int n = 1;
8d3788bd 6726
0d381245 6727 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6728 {
6729 struct cleanup *inner2 =
6730 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6731 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6732 do_cleanups (inner2);
6733 }
0d381245
VP
6734 }
6735 }
6736}
6737
a6d9a66e
UW
6738static int
6739breakpoint_address_bits (struct breakpoint *b)
6740{
6741 int print_address_bits = 0;
6742 struct bp_location *loc;
6743
c6d81124
PA
6744 /* Software watchpoints that aren't watching memory don't have an
6745 address to print. */
6746 if (is_no_memory_software_watchpoint (b))
6747 return 0;
6748
a6d9a66e
UW
6749 for (loc = b->loc; loc; loc = loc->next)
6750 {
c7437ca6
PA
6751 int addr_bit;
6752
c7437ca6 6753 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6754 if (addr_bit > print_address_bits)
6755 print_address_bits = addr_bit;
6756 }
6757
6758 return print_address_bits;
6759}
0d381245 6760
c4093a6a
JM
6761struct captured_breakpoint_query_args
6762 {
6763 int bnum;
6764 };
c5aa993b 6765
c4093a6a 6766static int
2b65245e 6767do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6768{
9a3c8263
SM
6769 struct captured_breakpoint_query_args *args
6770 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6771 struct breakpoint *b;
a6d9a66e 6772 struct bp_location *dummy_loc = NULL;
cc59ec59 6773
c4093a6a
JM
6774 ALL_BREAKPOINTS (b)
6775 {
6776 if (args->bnum == b->number)
c5aa993b 6777 {
12c5a436 6778 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6779 return GDB_RC_OK;
c5aa993b 6780 }
c4093a6a
JM
6781 }
6782 return GDB_RC_NONE;
6783}
c5aa993b 6784
c4093a6a 6785enum gdb_rc
4a64f543
MS
6786gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6787 char **error_message)
c4093a6a
JM
6788{
6789 struct captured_breakpoint_query_args args;
cc59ec59 6790
c4093a6a
JM
6791 args.bnum = bnum;
6792 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6793 an error. */
b0b13bb4
DJ
6794 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6795 error_message, RETURN_MASK_ALL) < 0)
6796 return GDB_RC_FAIL;
6797 else
6798 return GDB_RC_OK;
c4093a6a 6799}
c5aa993b 6800
09d682a4
TT
6801/* Return true if this breakpoint was set by the user, false if it is
6802 internal or momentary. */
6803
6804int
6805user_breakpoint_p (struct breakpoint *b)
6806{
46c6471b 6807 return b->number > 0;
09d682a4
TT
6808}
6809
93daf339
TT
6810/* See breakpoint.h. */
6811
6812int
6813pending_breakpoint_p (struct breakpoint *b)
6814{
6815 return b->loc == NULL;
6816}
6817
7f3b0473 6818/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6819 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6820 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6821 FILTER is non-NULL, call it on each breakpoint and only include the
6822 ones for which it returns non-zero. Return the total number of
6823 breakpoints listed. */
c906108c 6824
d77f58be 6825static int
e5a67952 6826breakpoint_1 (char *args, int allflag,
4a64f543 6827 int (*filter) (const struct breakpoint *))
c4093a6a 6828{
52f0bd74 6829 struct breakpoint *b;
a6d9a66e 6830 struct bp_location *last_loc = NULL;
7f3b0473 6831 int nr_printable_breakpoints;
3b31d625 6832 struct cleanup *bkpttbl_chain;
79a45b7d 6833 struct value_print_options opts;
a6d9a66e 6834 int print_address_bits = 0;
269b11a2 6835 int print_type_col_width = 14;
79a45e25 6836 struct ui_out *uiout = current_uiout;
269b11a2 6837
79a45b7d
TT
6838 get_user_print_options (&opts);
6839
4a64f543
MS
6840 /* Compute the number of rows in the table, as well as the size
6841 required for address fields. */
7f3b0473
AC
6842 nr_printable_breakpoints = 0;
6843 ALL_BREAKPOINTS (b)
e5a67952
MS
6844 {
6845 /* If we have a filter, only list the breakpoints it accepts. */
6846 if (filter && !filter (b))
6847 continue;
6848
6849 /* If we have an "args" string, it is a list of breakpoints to
6850 accept. Skip the others. */
6851 if (args != NULL && *args != '\0')
6852 {
6853 if (allflag && parse_and_eval_long (args) != b->number)
6854 continue;
6855 if (!allflag && !number_is_in_list (args, b->number))
6856 continue;
6857 }
269b11a2 6858
e5a67952
MS
6859 if (allflag || user_breakpoint_p (b))
6860 {
6861 int addr_bit, type_len;
a6d9a66e 6862
e5a67952
MS
6863 addr_bit = breakpoint_address_bits (b);
6864 if (addr_bit > print_address_bits)
6865 print_address_bits = addr_bit;
269b11a2 6866
e5a67952
MS
6867 type_len = strlen (bptype_string (b->type));
6868 if (type_len > print_type_col_width)
6869 print_type_col_width = type_len;
6870
6871 nr_printable_breakpoints++;
6872 }
6873 }
7f3b0473 6874
79a45b7d 6875 if (opts.addressprint)
3b31d625 6876 bkpttbl_chain
3e43a32a
MS
6877 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6878 nr_printable_breakpoints,
3b31d625 6879 "BreakpointTable");
8b93c638 6880 else
3b31d625 6881 bkpttbl_chain
3e43a32a
MS
6882 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6883 nr_printable_breakpoints,
3b31d625 6884 "BreakpointTable");
8b93c638 6885
7f3b0473 6886 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6887 annotate_breakpoints_headers ();
6888 if (nr_printable_breakpoints > 0)
6889 annotate_field (0);
4a64f543 6890 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (1);
269b11a2 6893 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6894 "type", "Type"); /* 2 */
d7faa9e7
AC
6895 if (nr_printable_breakpoints > 0)
6896 annotate_field (2);
4a64f543 6897 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6898 if (nr_printable_breakpoints > 0)
6899 annotate_field (3);
54e52265 6900 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6901 if (opts.addressprint)
e5a67952
MS
6902 {
6903 if (nr_printable_breakpoints > 0)
6904 annotate_field (4);
6905 if (print_address_bits <= 32)
6906 ui_out_table_header (uiout, 10, ui_left,
6907 "addr", "Address"); /* 5 */
6908 else
6909 ui_out_table_header (uiout, 18, ui_left,
6910 "addr", "Address"); /* 5 */
6911 }
d7faa9e7
AC
6912 if (nr_printable_breakpoints > 0)
6913 annotate_field (5);
6914 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6915 ui_out_table_body (uiout);
6916 if (nr_printable_breakpoints > 0)
6917 annotate_breakpoints_table ();
7f3b0473 6918
c4093a6a 6919 ALL_BREAKPOINTS (b)
e5a67952
MS
6920 {
6921 QUIT;
6922 /* If we have a filter, only list the breakpoints it accepts. */
6923 if (filter && !filter (b))
6924 continue;
6925
6926 /* If we have an "args" string, it is a list of breakpoints to
6927 accept. Skip the others. */
6928
6929 if (args != NULL && *args != '\0')
6930 {
6931 if (allflag) /* maintenance info breakpoint */
6932 {
6933 if (parse_and_eval_long (args) != b->number)
6934 continue;
6935 }
6936 else /* all others */
6937 {
6938 if (!number_is_in_list (args, b->number))
6939 continue;
6940 }
6941 }
6942 /* We only print out user settable breakpoints unless the
6943 allflag is set. */
6944 if (allflag || user_breakpoint_p (b))
12c5a436 6945 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6946 }
6947
3b31d625 6948 do_cleanups (bkpttbl_chain);
698384cd 6949
7f3b0473 6950 if (nr_printable_breakpoints == 0)
c906108c 6951 {
4a64f543
MS
6952 /* If there's a filter, let the caller decide how to report
6953 empty list. */
d77f58be
SS
6954 if (!filter)
6955 {
e5a67952 6956 if (args == NULL || *args == '\0')
d77f58be
SS
6957 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6958 else
4a64f543 6959 ui_out_message (uiout, 0,
e5a67952
MS
6960 "No breakpoint or watchpoint matching '%s'.\n",
6961 args);
d77f58be 6962 }
c906108c
SS
6963 }
6964 else
c4093a6a 6965 {
a6d9a66e
UW
6966 if (last_loc && !server_command)
6967 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6968 }
c906108c 6969
4a64f543 6970 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6971 there have been breakpoints? */
c906108c 6972 annotate_breakpoints_table_end ();
d77f58be
SS
6973
6974 return nr_printable_breakpoints;
c906108c
SS
6975}
6976
ad443146
SS
6977/* Display the value of default-collect in a way that is generally
6978 compatible with the breakpoint list. */
6979
6980static void
6981default_collect_info (void)
6982{
79a45e25
PA
6983 struct ui_out *uiout = current_uiout;
6984
ad443146
SS
6985 /* If it has no value (which is frequently the case), say nothing; a
6986 message like "No default-collect." gets in user's face when it's
6987 not wanted. */
6988 if (!*default_collect)
6989 return;
6990
6991 /* The following phrase lines up nicely with per-tracepoint collect
6992 actions. */
6993 ui_out_text (uiout, "default collect ");
6994 ui_out_field_string (uiout, "default-collect", default_collect);
6995 ui_out_text (uiout, " \n");
6996}
6997
c906108c 6998static void
e5a67952 6999breakpoints_info (char *args, int from_tty)
c906108c 7000{
e5a67952 7001 breakpoint_1 (args, 0, NULL);
ad443146
SS
7002
7003 default_collect_info ();
d77f58be
SS
7004}
7005
7006static void
e5a67952 7007watchpoints_info (char *args, int from_tty)
d77f58be 7008{
e5a67952 7009 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 7010 struct ui_out *uiout = current_uiout;
d77f58be
SS
7011
7012 if (num_printed == 0)
7013 {
e5a67952 7014 if (args == NULL || *args == '\0')
d77f58be
SS
7015 ui_out_message (uiout, 0, "No watchpoints.\n");
7016 else
e5a67952 7017 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 7018 }
c906108c
SS
7019}
7020
7a292a7a 7021static void
e5a67952 7022maintenance_info_breakpoints (char *args, int from_tty)
c906108c 7023{
e5a67952 7024 breakpoint_1 (args, 1, NULL);
ad443146
SS
7025
7026 default_collect_info ();
c906108c
SS
7027}
7028
0d381245 7029static int
714835d5 7030breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7031 struct program_space *pspace,
714835d5 7032 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7033{
7034 struct bp_location *bl = b->loc;
cc59ec59 7035
0d381245
VP
7036 for (; bl; bl = bl->next)
7037 {
6c95b8df
PA
7038 if (bl->pspace == pspace
7039 && bl->address == pc
0d381245
VP
7040 && (!overlay_debugging || bl->section == section))
7041 return 1;
7042 }
7043 return 0;
7044}
7045
672f9b60 7046/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7047 concerns with logical breakpoints, so we match program spaces, not
7048 address spaces. */
c906108c
SS
7049
7050static void
6c95b8df
PA
7051describe_other_breakpoints (struct gdbarch *gdbarch,
7052 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7053 struct obj_section *section, int thread)
c906108c 7054{
52f0bd74
AC
7055 int others = 0;
7056 struct breakpoint *b;
c906108c
SS
7057
7058 ALL_BREAKPOINTS (b)
672f9b60
KP
7059 others += (user_breakpoint_p (b)
7060 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7061 if (others > 0)
7062 {
a3f17187
AC
7063 if (others == 1)
7064 printf_filtered (_("Note: breakpoint "));
7065 else /* if (others == ???) */
7066 printf_filtered (_("Note: breakpoints "));
c906108c 7067 ALL_BREAKPOINTS (b)
672f9b60 7068 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7069 {
7070 others--;
7071 printf_filtered ("%d", b->number);
7072 if (b->thread == -1 && thread != -1)
7073 printf_filtered (" (all threads)");
7074 else if (b->thread != -1)
7075 printf_filtered (" (thread %d)", b->thread);
7076 printf_filtered ("%s%s ",
059fb39f 7077 ((b->enable_state == bp_disabled
f8eba3c6 7078 || b->enable_state == bp_call_disabled)
0d381245 7079 ? " (disabled)"
0d381245
VP
7080 : ""),
7081 (others > 1) ? ","
7082 : ((others == 1) ? " and" : ""));
7083 }
a3f17187 7084 printf_filtered (_("also set at pc "));
5af949e3 7085 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7086 printf_filtered (".\n");
7087 }
7088}
7089\f
c906108c 7090
e4f237da 7091/* Return true iff it is meaningful to use the address member of
244558af
LM
7092 BPT locations. For some breakpoint types, the locations' address members
7093 are irrelevant and it makes no sense to attempt to compare them to other
7094 addresses (or use them for any other purpose either).
e4f237da 7095
4a64f543 7096 More specifically, each of the following breakpoint types will
244558af 7097 always have a zero valued location address and we don't want to mark
4a64f543 7098 breakpoints of any of these types to be a duplicate of an actual
244558af 7099 breakpoint location at address zero:
e4f237da
KB
7100
7101 bp_watchpoint
2d134ed3
PA
7102 bp_catchpoint
7103
7104*/
e4f237da
KB
7105
7106static int
7107breakpoint_address_is_meaningful (struct breakpoint *bpt)
7108{
7109 enum bptype type = bpt->type;
7110
2d134ed3
PA
7111 return (type != bp_watchpoint && type != bp_catchpoint);
7112}
7113
7114/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7115 true if LOC1 and LOC2 represent the same watchpoint location. */
7116
7117static int
4a64f543
MS
7118watchpoint_locations_match (struct bp_location *loc1,
7119 struct bp_location *loc2)
2d134ed3 7120{
3a5c3e22
PA
7121 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7122 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7123
7124 /* Both of them must exist. */
7125 gdb_assert (w1 != NULL);
7126 gdb_assert (w2 != NULL);
2bdf28a0 7127
4a64f543
MS
7128 /* If the target can evaluate the condition expression in hardware,
7129 then we we need to insert both watchpoints even if they are at
7130 the same place. Otherwise the watchpoint will only trigger when
7131 the condition of whichever watchpoint was inserted evaluates to
7132 true, not giving a chance for GDB to check the condition of the
7133 other watchpoint. */
3a5c3e22 7134 if ((w1->cond_exp
4a64f543
MS
7135 && target_can_accel_watchpoint_condition (loc1->address,
7136 loc1->length,
0cf6dd15 7137 loc1->watchpoint_type,
4d01a485 7138 w1->cond_exp.get ()))
3a5c3e22 7139 || (w2->cond_exp
4a64f543
MS
7140 && target_can_accel_watchpoint_condition (loc2->address,
7141 loc2->length,
0cf6dd15 7142 loc2->watchpoint_type,
4d01a485 7143 w2->cond_exp.get ())))
0cf6dd15
TJB
7144 return 0;
7145
85d721b8
PA
7146 /* Note that this checks the owner's type, not the location's. In
7147 case the target does not support read watchpoints, but does
7148 support access watchpoints, we'll have bp_read_watchpoint
7149 watchpoints with hw_access locations. Those should be considered
7150 duplicates of hw_read locations. The hw_read locations will
7151 become hw_access locations later. */
2d134ed3
PA
7152 return (loc1->owner->type == loc2->owner->type
7153 && loc1->pspace->aspace == loc2->pspace->aspace
7154 && loc1->address == loc2->address
7155 && loc1->length == loc2->length);
e4f237da
KB
7156}
7157
31e77af2 7158/* See breakpoint.h. */
6c95b8df 7159
31e77af2 7160int
6c95b8df
PA
7161breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7162 struct address_space *aspace2, CORE_ADDR addr2)
7163{
f5656ead 7164 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7165 || aspace1 == aspace2)
7166 && addr1 == addr2);
7167}
7168
f1310107
TJB
7169/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7170 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7171 matches ASPACE2. On targets that have global breakpoints, the address
7172 space doesn't really matter. */
7173
7174static int
7175breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7176 int len1, struct address_space *aspace2,
7177 CORE_ADDR addr2)
7178{
f5656ead 7179 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7180 || aspace1 == aspace2)
7181 && addr2 >= addr1 && addr2 < addr1 + len1);
7182}
7183
7184/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7185 a ranged breakpoint. In most targets, a match happens only if ASPACE
7186 matches the breakpoint's address space. On targets that have global
7187 breakpoints, the address space doesn't really matter. */
7188
7189static int
7190breakpoint_location_address_match (struct bp_location *bl,
7191 struct address_space *aspace,
7192 CORE_ADDR addr)
7193{
7194 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7195 aspace, addr)
7196 || (bl->length
7197 && breakpoint_address_match_range (bl->pspace->aspace,
7198 bl->address, bl->length,
7199 aspace, addr)));
7200}
7201
d35ae833
PA
7202/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7203 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7204 match happens only if ASPACE matches the breakpoint's address
7205 space. On targets that have global breakpoints, the address space
7206 doesn't really matter. */
7207
7208static int
7209breakpoint_location_address_range_overlap (struct bp_location *bl,
7210 struct address_space *aspace,
7211 CORE_ADDR addr, int len)
7212{
7213 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7214 || bl->pspace->aspace == aspace)
7215 {
7216 int bl_len = bl->length != 0 ? bl->length : 1;
7217
7218 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7219 return 1;
7220 }
7221 return 0;
7222}
7223
1e4d1764
YQ
7224/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7225 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7226 true, otherwise returns false. */
7227
7228static int
7229tracepoint_locations_match (struct bp_location *loc1,
7230 struct bp_location *loc2)
7231{
7232 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7233 /* Since tracepoint locations are never duplicated with others', tracepoint
7234 locations at the same address of different tracepoints are regarded as
7235 different locations. */
7236 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7237 else
7238 return 0;
7239}
7240
2d134ed3
PA
7241/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7242 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7243 represent the same location. */
7244
7245static int
4a64f543
MS
7246breakpoint_locations_match (struct bp_location *loc1,
7247 struct bp_location *loc2)
2d134ed3 7248{
2bdf28a0
JK
7249 int hw_point1, hw_point2;
7250
7251 /* Both of them must not be in moribund_locations. */
7252 gdb_assert (loc1->owner != NULL);
7253 gdb_assert (loc2->owner != NULL);
7254
7255 hw_point1 = is_hardware_watchpoint (loc1->owner);
7256 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7257
7258 if (hw_point1 != hw_point2)
7259 return 0;
7260 else if (hw_point1)
7261 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7262 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7263 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7264 else
f1310107
TJB
7265 /* We compare bp_location.length in order to cover ranged breakpoints. */
7266 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7267 loc2->pspace->aspace, loc2->address)
7268 && loc1->length == loc2->length);
2d134ed3
PA
7269}
7270
76897487
KB
7271static void
7272breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7273 int bnum, int have_bnum)
7274{
f63fbe86
MS
7275 /* The longest string possibly returned by hex_string_custom
7276 is 50 chars. These must be at least that big for safety. */
7277 char astr1[64];
7278 char astr2[64];
76897487 7279
bb599908
PH
7280 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7281 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7282 if (have_bnum)
8a3fe4f8 7283 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7284 bnum, astr1, astr2);
7285 else
8a3fe4f8 7286 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7287}
7288
4a64f543
MS
7289/* Adjust a breakpoint's address to account for architectural
7290 constraints on breakpoint placement. Return the adjusted address.
7291 Note: Very few targets require this kind of adjustment. For most
7292 targets, this function is simply the identity function. */
76897487
KB
7293
7294static CORE_ADDR
a6d9a66e
UW
7295adjust_breakpoint_address (struct gdbarch *gdbarch,
7296 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7297{
a6d9a66e 7298 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7299 {
7300 /* Very few targets need any kind of breakpoint adjustment. */
7301 return bpaddr;
7302 }
88f7da05
KB
7303 else if (bptype == bp_watchpoint
7304 || bptype == bp_hardware_watchpoint
7305 || bptype == bp_read_watchpoint
7306 || bptype == bp_access_watchpoint
fe798b75 7307 || bptype == bp_catchpoint)
88f7da05
KB
7308 {
7309 /* Watchpoints and the various bp_catch_* eventpoints should not
7310 have their addresses modified. */
7311 return bpaddr;
7312 }
7c16b83e
PA
7313 else if (bptype == bp_single_step)
7314 {
7315 /* Single-step breakpoints should not have their addresses
7316 modified. If there's any architectural constrain that
7317 applies to this address, then it should have already been
7318 taken into account when the breakpoint was created in the
7319 first place. If we didn't do this, stepping through e.g.,
7320 Thumb-2 IT blocks would break. */
7321 return bpaddr;
7322 }
76897487
KB
7323 else
7324 {
7325 CORE_ADDR adjusted_bpaddr;
7326
7327 /* Some targets have architectural constraints on the placement
7328 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7329 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7330
7331 /* An adjusted breakpoint address can significantly alter
7332 a user's expectations. Print a warning if an adjustment
7333 is required. */
7334 if (adjusted_bpaddr != bpaddr)
7335 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7336
7337 return adjusted_bpaddr;
7338 }
7339}
7340
28010a5d
PA
7341void
7342init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7343 struct breakpoint *owner)
7cc221ef 7344{
7cc221ef
DJ
7345 memset (loc, 0, sizeof (*loc));
7346
348d480f
PA
7347 gdb_assert (ops != NULL);
7348
28010a5d
PA
7349 loc->ops = ops;
7350 loc->owner = owner;
b775012e 7351 loc->cond_bytecode = NULL;
0d381245
VP
7352 loc->shlib_disabled = 0;
7353 loc->enabled = 1;
e049a4b5 7354
28010a5d 7355 switch (owner->type)
e049a4b5
DJ
7356 {
7357 case bp_breakpoint:
7c16b83e 7358 case bp_single_step:
e049a4b5
DJ
7359 case bp_until:
7360 case bp_finish:
7361 case bp_longjmp:
7362 case bp_longjmp_resume:
e2e4d78b 7363 case bp_longjmp_call_dummy:
186c406b
TT
7364 case bp_exception:
7365 case bp_exception_resume:
e049a4b5 7366 case bp_step_resume:
2c03e5be 7367 case bp_hp_step_resume:
e049a4b5
DJ
7368 case bp_watchpoint_scope:
7369 case bp_call_dummy:
aa7d318d 7370 case bp_std_terminate:
e049a4b5
DJ
7371 case bp_shlib_event:
7372 case bp_thread_event:
7373 case bp_overlay_event:
4efc6507 7374 case bp_jit_event:
0fd8e87f 7375 case bp_longjmp_master:
aa7d318d 7376 case bp_std_terminate_master:
186c406b 7377 case bp_exception_master:
0e30163f
JK
7378 case bp_gnu_ifunc_resolver:
7379 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7380 case bp_dprintf:
e049a4b5 7381 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7382 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7383 break;
7384 case bp_hardware_breakpoint:
7385 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7386 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7387 break;
7388 case bp_hardware_watchpoint:
7389 case bp_read_watchpoint:
7390 case bp_access_watchpoint:
7391 loc->loc_type = bp_loc_hardware_watchpoint;
7392 break;
7393 case bp_watchpoint:
ce78b96d 7394 case bp_catchpoint:
15c3d785
PA
7395 case bp_tracepoint:
7396 case bp_fast_tracepoint:
0fb4aa4b 7397 case bp_static_tracepoint:
e049a4b5
DJ
7398 loc->loc_type = bp_loc_other;
7399 break;
7400 default:
e2e0b3e5 7401 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7402 }
7403
f431efe5 7404 loc->refc = 1;
28010a5d
PA
7405}
7406
7407/* Allocate a struct bp_location. */
7408
7409static struct bp_location *
7410allocate_bp_location (struct breakpoint *bpt)
7411{
348d480f
PA
7412 return bpt->ops->allocate_location (bpt);
7413}
7cc221ef 7414
f431efe5
PA
7415static void
7416free_bp_location (struct bp_location *loc)
fe3f5fa8 7417{
348d480f 7418 loc->ops->dtor (loc);
4d01a485 7419 delete loc;
fe3f5fa8
VP
7420}
7421
f431efe5
PA
7422/* Increment reference count. */
7423
7424static void
7425incref_bp_location (struct bp_location *bl)
7426{
7427 ++bl->refc;
7428}
7429
7430/* Decrement reference count. If the reference count reaches 0,
7431 destroy the bp_location. Sets *BLP to NULL. */
7432
7433static void
7434decref_bp_location (struct bp_location **blp)
7435{
0807b50c
PA
7436 gdb_assert ((*blp)->refc > 0);
7437
f431efe5
PA
7438 if (--(*blp)->refc == 0)
7439 free_bp_location (*blp);
7440 *blp = NULL;
7441}
7442
346774a9 7443/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7444
346774a9
PA
7445static void
7446add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7447{
346774a9 7448 struct breakpoint *b1;
c906108c 7449
346774a9
PA
7450 /* Add this breakpoint to the end of the chain so that a list of
7451 breakpoints will come out in order of increasing numbers. */
7452
7453 b1 = breakpoint_chain;
7454 if (b1 == 0)
7455 breakpoint_chain = b;
7456 else
7457 {
7458 while (b1->next)
7459 b1 = b1->next;
7460 b1->next = b;
7461 }
7462}
7463
7464/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7465
7466static void
7467init_raw_breakpoint_without_location (struct breakpoint *b,
7468 struct gdbarch *gdbarch,
28010a5d 7469 enum bptype bptype,
c0a91b2b 7470 const struct breakpoint_ops *ops)
346774a9 7471{
c906108c 7472 memset (b, 0, sizeof (*b));
2219d63c 7473
348d480f
PA
7474 gdb_assert (ops != NULL);
7475
28010a5d 7476 b->ops = ops;
4d28f7a8 7477 b->type = bptype;
a6d9a66e 7478 b->gdbarch = gdbarch;
c906108c
SS
7479 b->language = current_language->la_language;
7480 b->input_radix = input_radix;
7481 b->thread = -1;
b5de0fa7 7482 b->enable_state = bp_enabled;
c906108c
SS
7483 b->next = 0;
7484 b->silent = 0;
7485 b->ignore_count = 0;
7486 b->commands = NULL;
818dd999 7487 b->frame_id = null_frame_id;
0d381245 7488 b->condition_not_parsed = 0;
84f4c1fe 7489 b->py_bp_object = NULL;
d0fb5eae 7490 b->related_breakpoint = b;
f00aae0f 7491 b->location = NULL;
346774a9
PA
7492}
7493
7494/* Helper to set_raw_breakpoint below. Creates a breakpoint
7495 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7496
7497static struct breakpoint *
7498set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7499 enum bptype bptype,
c0a91b2b 7500 const struct breakpoint_ops *ops)
346774a9 7501{
4d01a485 7502 struct breakpoint *b = new breakpoint ();
346774a9 7503
348d480f 7504 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7505 add_to_breakpoint_chain (b);
0d381245
VP
7506 return b;
7507}
7508
0e30163f
JK
7509/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7510 resolutions should be made as the user specified the location explicitly
7511 enough. */
7512
0d381245 7513static void
0e30163f 7514set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7515{
2bdf28a0
JK
7516 gdb_assert (loc->owner != NULL);
7517
0d381245 7518 if (loc->owner->type == bp_breakpoint
1042e4c0 7519 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7520 || is_tracepoint (loc->owner))
0d381245 7521 {
0e30163f 7522 int is_gnu_ifunc;
2c02bd72 7523 const char *function_name;
6a3a010b 7524 CORE_ADDR func_addr;
0e30163f 7525
2c02bd72 7526 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7527 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7528
7529 if (is_gnu_ifunc && !explicit_loc)
7530 {
7531 struct breakpoint *b = loc->owner;
7532
7533 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7534 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7535 &loc->requested_address))
7536 {
7537 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7538 loc->address = adjust_breakpoint_address (loc->gdbarch,
7539 loc->requested_address,
7540 b->type);
7541 }
7542 else if (b->type == bp_breakpoint && b->loc == loc
7543 && loc->next == NULL && b->related_breakpoint == b)
7544 {
7545 /* Create only the whole new breakpoint of this type but do not
7546 mess more complicated breakpoints with multiple locations. */
7547 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7548 /* Remember the resolver's address for use by the return
7549 breakpoint. */
7550 loc->related_address = func_addr;
0e30163f
JK
7551 }
7552 }
7553
2c02bd72
DE
7554 if (function_name)
7555 loc->function_name = xstrdup (function_name);
0d381245
VP
7556 }
7557}
7558
a6d9a66e 7559/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7560struct gdbarch *
a6d9a66e
UW
7561get_sal_arch (struct symtab_and_line sal)
7562{
7563 if (sal.section)
7564 return get_objfile_arch (sal.section->objfile);
7565 if (sal.symtab)
eb822aa6 7566 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7567
7568 return NULL;
7569}
7570
346774a9
PA
7571/* Low level routine for partially initializing a breakpoint of type
7572 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7573 file name, and line number are provided by SAL.
0d381245
VP
7574
7575 It is expected that the caller will complete the initialization of
7576 the newly created breakpoint struct as well as output any status
c56053d2 7577 information regarding the creation of a new breakpoint. */
0d381245 7578
346774a9
PA
7579static void
7580init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7581 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7582 const struct breakpoint_ops *ops)
0d381245 7583{
28010a5d 7584 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7585
3742cc8b 7586 add_location_to_breakpoint (b, &sal);
0d381245 7587
6c95b8df
PA
7588 if (bptype != bp_catchpoint)
7589 gdb_assert (sal.pspace != NULL);
7590
f8eba3c6
TT
7591 /* Store the program space that was used to set the breakpoint,
7592 except for ordinary breakpoints, which are independent of the
7593 program space. */
7594 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7595 b->pspace = sal.pspace;
346774a9 7596}
c906108c 7597
346774a9
PA
7598/* set_raw_breakpoint is a low level routine for allocating and
7599 partially initializing a breakpoint of type BPTYPE. The newly
7600 created breakpoint's address, section, source file name, and line
7601 number are provided by SAL. The newly created and partially
7602 initialized breakpoint is added to the breakpoint chain and
7603 is also returned as the value of this function.
7604
7605 It is expected that the caller will complete the initialization of
7606 the newly created breakpoint struct as well as output any status
7607 information regarding the creation of a new breakpoint. In
7608 particular, set_raw_breakpoint does NOT set the breakpoint
7609 number! Care should be taken to not allow an error to occur
7610 prior to completing the initialization of the breakpoint. If this
7611 should happen, a bogus breakpoint will be left on the chain. */
7612
7613struct breakpoint *
7614set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7615 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7616 const struct breakpoint_ops *ops)
346774a9 7617{
4d01a485 7618 struct breakpoint *b = new breakpoint ();
346774a9 7619
348d480f 7620 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7621 add_to_breakpoint_chain (b);
c906108c
SS
7622 return b;
7623}
7624
53a5351d 7625/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7626 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7627 initiated the operation. */
c906108c
SS
7628
7629void
186c406b 7630set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7631{
35df4500 7632 struct breakpoint *b, *b_tmp;
5d5658a1 7633 int thread = tp->global_num;
0fd8e87f
UW
7634
7635 /* To avoid having to rescan all objfile symbols at every step,
7636 we maintain a list of continually-inserted but always disabled
7637 longjmp "master" breakpoints. Here, we simply create momentary
7638 clones of those and enable them for the requested thread. */
35df4500 7639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7640 if (b->pspace == current_program_space
186c406b
TT
7641 && (b->type == bp_longjmp_master
7642 || b->type == bp_exception_master))
0fd8e87f 7643 {
06edf0c0
PA
7644 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7645 struct breakpoint *clone;
cc59ec59 7646
e2e4d78b
JK
7647 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7648 after their removal. */
06edf0c0 7649 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7650 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7651 clone->thread = thread;
7652 }
186c406b
TT
7653
7654 tp->initiating_frame = frame;
c906108c
SS
7655}
7656
611c83ae 7657/* Delete all longjmp breakpoints from THREAD. */
c906108c 7658void
611c83ae 7659delete_longjmp_breakpoint (int thread)
c906108c 7660{
35df4500 7661 struct breakpoint *b, *b_tmp;
c906108c 7662
35df4500 7663 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7664 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7665 {
7666 if (b->thread == thread)
7667 delete_breakpoint (b);
7668 }
c906108c
SS
7669}
7670
f59f708a
PA
7671void
7672delete_longjmp_breakpoint_at_next_stop (int thread)
7673{
7674 struct breakpoint *b, *b_tmp;
7675
7676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7677 if (b->type == bp_longjmp || b->type == bp_exception)
7678 {
7679 if (b->thread == thread)
7680 b->disposition = disp_del_at_next_stop;
7681 }
7682}
7683
e2e4d78b
JK
7684/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7685 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7686 pointer to any of them. Return NULL if this system cannot place longjmp
7687 breakpoints. */
7688
7689struct breakpoint *
7690set_longjmp_breakpoint_for_call_dummy (void)
7691{
7692 struct breakpoint *b, *retval = NULL;
7693
7694 ALL_BREAKPOINTS (b)
7695 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7696 {
7697 struct breakpoint *new_b;
7698
7699 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7700 &momentary_breakpoint_ops,
7701 1);
5d5658a1 7702 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7703
7704 /* Link NEW_B into the chain of RETVAL breakpoints. */
7705
7706 gdb_assert (new_b->related_breakpoint == new_b);
7707 if (retval == NULL)
7708 retval = new_b;
7709 new_b->related_breakpoint = retval;
7710 while (retval->related_breakpoint != new_b->related_breakpoint)
7711 retval = retval->related_breakpoint;
7712 retval->related_breakpoint = new_b;
7713 }
7714
7715 return retval;
7716}
7717
7718/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7719 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7720 stack.
7721
7722 You should call this function only at places where it is safe to currently
7723 unwind the whole stack. Failed stack unwind would discard live dummy
7724 frames. */
7725
7726void
b67a2c6f 7727check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7728{
7729 struct breakpoint *b, *b_tmp;
7730
7731 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7732 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7733 {
7734 struct breakpoint *dummy_b = b->related_breakpoint;
7735
7736 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7737 dummy_b = dummy_b->related_breakpoint;
7738 if (dummy_b->type != bp_call_dummy
7739 || frame_find_by_id (dummy_b->frame_id) != NULL)
7740 continue;
7741
b67a2c6f 7742 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7743
7744 while (b->related_breakpoint != b)
7745 {
7746 if (b_tmp == b->related_breakpoint)
7747 b_tmp = b->related_breakpoint->next;
7748 delete_breakpoint (b->related_breakpoint);
7749 }
7750 delete_breakpoint (b);
7751 }
7752}
7753
1900040c
MS
7754void
7755enable_overlay_breakpoints (void)
7756{
52f0bd74 7757 struct breakpoint *b;
1900040c
MS
7758
7759 ALL_BREAKPOINTS (b)
7760 if (b->type == bp_overlay_event)
7761 {
7762 b->enable_state = bp_enabled;
44702360 7763 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7764 overlay_events_enabled = 1;
1900040c
MS
7765 }
7766}
7767
7768void
7769disable_overlay_breakpoints (void)
7770{
52f0bd74 7771 struct breakpoint *b;
1900040c
MS
7772
7773 ALL_BREAKPOINTS (b)
7774 if (b->type == bp_overlay_event)
7775 {
7776 b->enable_state = bp_disabled;
44702360 7777 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7778 overlay_events_enabled = 0;
1900040c
MS
7779 }
7780}
7781
aa7d318d
TT
7782/* Set an active std::terminate breakpoint for each std::terminate
7783 master breakpoint. */
7784void
7785set_std_terminate_breakpoint (void)
7786{
35df4500 7787 struct breakpoint *b, *b_tmp;
aa7d318d 7788
35df4500 7789 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7790 if (b->pspace == current_program_space
7791 && b->type == bp_std_terminate_master)
7792 {
06edf0c0 7793 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7794 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7795 }
7796}
7797
7798/* Delete all the std::terminate breakpoints. */
7799void
7800delete_std_terminate_breakpoint (void)
7801{
35df4500 7802 struct breakpoint *b, *b_tmp;
aa7d318d 7803
35df4500 7804 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7805 if (b->type == bp_std_terminate)
7806 delete_breakpoint (b);
7807}
7808
c4093a6a 7809struct breakpoint *
a6d9a66e 7810create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7811{
7812 struct breakpoint *b;
c4093a6a 7813
06edf0c0
PA
7814 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7815 &internal_breakpoint_ops);
7816
b5de0fa7 7817 b->enable_state = bp_enabled;
f00aae0f 7818 /* location has to be used or breakpoint_re_set will delete me. */
305e13e6 7819 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7820
44702360 7821 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7822
c4093a6a
JM
7823 return b;
7824}
7825
0101ce28
JJ
7826struct lang_and_radix
7827 {
7828 enum language lang;
7829 int radix;
7830 };
7831
4efc6507
DE
7832/* Create a breakpoint for JIT code registration and unregistration. */
7833
7834struct breakpoint *
7835create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7836{
2a7f3dff
PA
7837 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7838 &internal_breakpoint_ops);
4efc6507 7839}
0101ce28 7840
03673fc7
PP
7841/* Remove JIT code registration and unregistration breakpoint(s). */
7842
7843void
7844remove_jit_event_breakpoints (void)
7845{
7846 struct breakpoint *b, *b_tmp;
7847
7848 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7849 if (b->type == bp_jit_event
7850 && b->loc->pspace == current_program_space)
7851 delete_breakpoint (b);
7852}
7853
cae688ec
JJ
7854void
7855remove_solib_event_breakpoints (void)
7856{
35df4500 7857 struct breakpoint *b, *b_tmp;
cae688ec 7858
35df4500 7859 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7860 if (b->type == bp_shlib_event
7861 && b->loc->pspace == current_program_space)
cae688ec
JJ
7862 delete_breakpoint (b);
7863}
7864
f37f681c
PA
7865/* See breakpoint.h. */
7866
7867void
7868remove_solib_event_breakpoints_at_next_stop (void)
7869{
7870 struct breakpoint *b, *b_tmp;
7871
7872 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7873 if (b->type == bp_shlib_event
7874 && b->loc->pspace == current_program_space)
7875 b->disposition = disp_del_at_next_stop;
7876}
7877
04086b45
PA
7878/* Helper for create_solib_event_breakpoint /
7879 create_and_insert_solib_event_breakpoint. Allows specifying which
7880 INSERT_MODE to pass through to update_global_location_list. */
7881
7882static struct breakpoint *
7883create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7884 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7885{
7886 struct breakpoint *b;
7887
06edf0c0
PA
7888 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7889 &internal_breakpoint_ops);
04086b45 7890 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7891 return b;
7892}
7893
04086b45
PA
7894struct breakpoint *
7895create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7896{
7897 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7898}
7899
f37f681c
PA
7900/* See breakpoint.h. */
7901
7902struct breakpoint *
7903create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7904{
7905 struct breakpoint *b;
7906
04086b45
PA
7907 /* Explicitly tell update_global_location_list to insert
7908 locations. */
7909 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7910 if (!b->loc->inserted)
7911 {
7912 delete_breakpoint (b);
7913 return NULL;
7914 }
7915 return b;
7916}
7917
cae688ec
JJ
7918/* Disable any breakpoints that are on code in shared libraries. Only
7919 apply to enabled breakpoints, disabled ones can just stay disabled. */
7920
7921void
cb851954 7922disable_breakpoints_in_shlibs (void)
cae688ec 7923{
876fa593 7924 struct bp_location *loc, **locp_tmp;
cae688ec 7925
876fa593 7926 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7927 {
2bdf28a0 7928 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7929 struct breakpoint *b = loc->owner;
2bdf28a0 7930
4a64f543
MS
7931 /* We apply the check to all breakpoints, including disabled for
7932 those with loc->duplicate set. This is so that when breakpoint
7933 becomes enabled, or the duplicate is removed, gdb will try to
7934 insert all breakpoints. If we don't set shlib_disabled here,
7935 we'll try to insert those breakpoints and fail. */
1042e4c0 7936 if (((b->type == bp_breakpoint)
508ccb1f 7937 || (b->type == bp_jit_event)
1042e4c0 7938 || (b->type == bp_hardware_breakpoint)
d77f58be 7939 || (is_tracepoint (b)))
6c95b8df 7940 && loc->pspace == current_program_space
0d381245 7941 && !loc->shlib_disabled
6c95b8df 7942 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7943 )
0d381245
VP
7944 {
7945 loc->shlib_disabled = 1;
7946 }
cae688ec
JJ
7947 }
7948}
7949
63644780
NB
7950/* Disable any breakpoints and tracepoints that are in SOLIB upon
7951 notification of unloaded_shlib. Only apply to enabled breakpoints,
7952 disabled ones can just stay disabled. */
84acb35a 7953
75149521 7954static void
84acb35a
JJ
7955disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7956{
876fa593 7957 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7958 int disabled_shlib_breaks = 0;
7959
c86cf029
VP
7960 /* SunOS a.out shared libraries are always mapped, so do not
7961 disable breakpoints; they will only be reported as unloaded
7962 through clear_solib when GDB discards its shared library
7963 list. See clear_solib for more information. */
7964 if (exec_bfd != NULL
7965 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7966 return;
7967
876fa593 7968 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7969 {
2bdf28a0 7970 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7971 struct breakpoint *b = loc->owner;
cc59ec59 7972
1e4d1764 7973 if (solib->pspace == loc->pspace
e2dd7057 7974 && !loc->shlib_disabled
1e4d1764
YQ
7975 && (((b->type == bp_breakpoint
7976 || b->type == bp_jit_event
7977 || b->type == bp_hardware_breakpoint)
7978 && (loc->loc_type == bp_loc_hardware_breakpoint
7979 || loc->loc_type == bp_loc_software_breakpoint))
7980 || is_tracepoint (b))
e2dd7057 7981 && solib_contains_address_p (solib, loc->address))
84acb35a 7982 {
e2dd7057
PP
7983 loc->shlib_disabled = 1;
7984 /* At this point, we cannot rely on remove_breakpoint
7985 succeeding so we must mark the breakpoint as not inserted
7986 to prevent future errors occurring in remove_breakpoints. */
7987 loc->inserted = 0;
8d3788bd
VP
7988
7989 /* This may cause duplicate notifications for the same breakpoint. */
7990 observer_notify_breakpoint_modified (b);
7991
e2dd7057
PP
7992 if (!disabled_shlib_breaks)
7993 {
7994 target_terminal_ours_for_output ();
3e43a32a
MS
7995 warning (_("Temporarily disabling breakpoints "
7996 "for unloaded shared library \"%s\""),
e2dd7057 7997 solib->so_name);
84acb35a 7998 }
e2dd7057 7999 disabled_shlib_breaks = 1;
84acb35a
JJ
8000 }
8001 }
84acb35a
JJ
8002}
8003
63644780
NB
8004/* Disable any breakpoints and tracepoints in OBJFILE upon
8005 notification of free_objfile. Only apply to enabled breakpoints,
8006 disabled ones can just stay disabled. */
8007
8008static void
8009disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8010{
8011 struct breakpoint *b;
8012
8013 if (objfile == NULL)
8014 return;
8015
d03de421
PA
8016 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8017 managed by the user with add-symbol-file/remove-symbol-file.
8018 Similarly to how breakpoints in shared libraries are handled in
8019 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
8020 shlib_disabled so they end up uninserted on the next global
8021 location list update. Shared libraries not loaded by the user
8022 aren't handled here -- they're already handled in
8023 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8024 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8025 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8026 main objfile). */
8027 if ((objfile->flags & OBJF_SHARED) == 0
8028 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8029 return;
8030
8031 ALL_BREAKPOINTS (b)
8032 {
8033 struct bp_location *loc;
8034 int bp_modified = 0;
8035
8036 if (!is_breakpoint (b) && !is_tracepoint (b))
8037 continue;
8038
8039 for (loc = b->loc; loc != NULL; loc = loc->next)
8040 {
8041 CORE_ADDR loc_addr = loc->address;
8042
8043 if (loc->loc_type != bp_loc_hardware_breakpoint
8044 && loc->loc_type != bp_loc_software_breakpoint)
8045 continue;
8046
8047 if (loc->shlib_disabled != 0)
8048 continue;
8049
8050 if (objfile->pspace != loc->pspace)
8051 continue;
8052
8053 if (loc->loc_type != bp_loc_hardware_breakpoint
8054 && loc->loc_type != bp_loc_software_breakpoint)
8055 continue;
8056
8057 if (is_addr_in_objfile (loc_addr, objfile))
8058 {
8059 loc->shlib_disabled = 1;
08351840
PA
8060 /* At this point, we don't know whether the object was
8061 unmapped from the inferior or not, so leave the
8062 inserted flag alone. We'll handle failure to
8063 uninsert quietly, in case the object was indeed
8064 unmapped. */
63644780
NB
8065
8066 mark_breakpoint_location_modified (loc);
8067
8068 bp_modified = 1;
8069 }
8070 }
8071
8072 if (bp_modified)
8073 observer_notify_breakpoint_modified (b);
8074 }
8075}
8076
ce78b96d
JB
8077/* FORK & VFORK catchpoints. */
8078
e29a4733
PA
8079/* An instance of this type is used to represent a fork or vfork
8080 catchpoint. It includes a "struct breakpoint" as a kind of base
8081 class; users downcast to "struct breakpoint *" when needed. A
8082 breakpoint is really of this type iff its ops pointer points to
8083 CATCH_FORK_BREAKPOINT_OPS. */
8084
8085struct fork_catchpoint
8086{
8087 /* The base class. */
8088 struct breakpoint base;
8089
8090 /* Process id of a child process whose forking triggered this
8091 catchpoint. This field is only valid immediately after this
8092 catchpoint has triggered. */
8093 ptid_t forked_inferior_pid;
8094};
8095
4a64f543
MS
8096/* Implement the "insert" breakpoint_ops method for fork
8097 catchpoints. */
ce78b96d 8098
77b06cd7
TJB
8099static int
8100insert_catch_fork (struct bp_location *bl)
ce78b96d 8101{
dfd4cc63 8102 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8103}
8104
4a64f543
MS
8105/* Implement the "remove" breakpoint_ops method for fork
8106 catchpoints. */
ce78b96d
JB
8107
8108static int
73971819 8109remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8110{
dfd4cc63 8111 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8112}
8113
8114/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8115 catchpoints. */
8116
8117static int
f1310107 8118breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8119 struct address_space *aspace, CORE_ADDR bp_addr,
8120 const struct target_waitstatus *ws)
ce78b96d 8121{
e29a4733
PA
8122 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8123
f90263c1
TT
8124 if (ws->kind != TARGET_WAITKIND_FORKED)
8125 return 0;
8126
8127 c->forked_inferior_pid = ws->value.related_pid;
8128 return 1;
ce78b96d
JB
8129}
8130
4a64f543
MS
8131/* Implement the "print_it" breakpoint_ops method for fork
8132 catchpoints. */
ce78b96d
JB
8133
8134static enum print_stop_action
348d480f 8135print_it_catch_fork (bpstat bs)
ce78b96d 8136{
36dfb11c 8137 struct ui_out *uiout = current_uiout;
348d480f
PA
8138 struct breakpoint *b = bs->breakpoint_at;
8139 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8140
ce78b96d 8141 annotate_catchpoint (b->number);
f303dbd6 8142 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8143 if (b->disposition == disp_del)
f303dbd6 8144 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8145 else
f303dbd6 8146 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8147 if (ui_out_is_mi_like_p (uiout))
8148 {
8149 ui_out_field_string (uiout, "reason",
8150 async_reason_lookup (EXEC_ASYNC_FORK));
8151 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8152 }
8153 ui_out_field_int (uiout, "bkptno", b->number);
8154 ui_out_text (uiout, " (forked process ");
8155 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8156 ui_out_text (uiout, "), ");
ce78b96d
JB
8157 return PRINT_SRC_AND_LOC;
8158}
8159
4a64f543
MS
8160/* Implement the "print_one" breakpoint_ops method for fork
8161 catchpoints. */
ce78b96d
JB
8162
8163static void
a6d9a66e 8164print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8165{
e29a4733 8166 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8167 struct value_print_options opts;
79a45e25 8168 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8169
8170 get_user_print_options (&opts);
8171
4a64f543
MS
8172 /* Field 4, the address, is omitted (which makes the columns not
8173 line up too nicely with the headers, but the effect is relatively
8174 readable). */
79a45b7d 8175 if (opts.addressprint)
ce78b96d
JB
8176 ui_out_field_skip (uiout, "addr");
8177 annotate_field (5);
8178 ui_out_text (uiout, "fork");
e29a4733 8179 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8180 {
8181 ui_out_text (uiout, ", process ");
8182 ui_out_field_int (uiout, "what",
e29a4733 8183 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8184 ui_out_spaces (uiout, 1);
8185 }
8ac3646f
TT
8186
8187 if (ui_out_is_mi_like_p (uiout))
8188 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8189}
8190
8191/* Implement the "print_mention" breakpoint_ops method for fork
8192 catchpoints. */
8193
8194static void
8195print_mention_catch_fork (struct breakpoint *b)
8196{
8197 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8198}
8199
6149aea9
PA
8200/* Implement the "print_recreate" breakpoint_ops method for fork
8201 catchpoints. */
8202
8203static void
8204print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8205{
8206 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8207 print_recreate_thread (b, fp);
6149aea9
PA
8208}
8209
ce78b96d
JB
8210/* The breakpoint_ops structure to be used in fork catchpoints. */
8211
2060206e 8212static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8213
4a64f543
MS
8214/* Implement the "insert" breakpoint_ops method for vfork
8215 catchpoints. */
ce78b96d 8216
77b06cd7
TJB
8217static int
8218insert_catch_vfork (struct bp_location *bl)
ce78b96d 8219{
dfd4cc63 8220 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8221}
8222
4a64f543
MS
8223/* Implement the "remove" breakpoint_ops method for vfork
8224 catchpoints. */
ce78b96d
JB
8225
8226static int
73971819 8227remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8228{
dfd4cc63 8229 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8230}
8231
8232/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8233 catchpoints. */
8234
8235static int
f1310107 8236breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8237 struct address_space *aspace, CORE_ADDR bp_addr,
8238 const struct target_waitstatus *ws)
ce78b96d 8239{
e29a4733
PA
8240 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8241
f90263c1
TT
8242 if (ws->kind != TARGET_WAITKIND_VFORKED)
8243 return 0;
8244
8245 c->forked_inferior_pid = ws->value.related_pid;
8246 return 1;
ce78b96d
JB
8247}
8248
4a64f543
MS
8249/* Implement the "print_it" breakpoint_ops method for vfork
8250 catchpoints. */
ce78b96d
JB
8251
8252static enum print_stop_action
348d480f 8253print_it_catch_vfork (bpstat bs)
ce78b96d 8254{
36dfb11c 8255 struct ui_out *uiout = current_uiout;
348d480f 8256 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8257 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8258
ce78b96d 8259 annotate_catchpoint (b->number);
f303dbd6 8260 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8261 if (b->disposition == disp_del)
f303dbd6 8262 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8263 else
f303dbd6 8264 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8265 if (ui_out_is_mi_like_p (uiout))
8266 {
8267 ui_out_field_string (uiout, "reason",
8268 async_reason_lookup (EXEC_ASYNC_VFORK));
8269 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8270 }
8271 ui_out_field_int (uiout, "bkptno", b->number);
8272 ui_out_text (uiout, " (vforked process ");
8273 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8274 ui_out_text (uiout, "), ");
ce78b96d
JB
8275 return PRINT_SRC_AND_LOC;
8276}
8277
4a64f543
MS
8278/* Implement the "print_one" breakpoint_ops method for vfork
8279 catchpoints. */
ce78b96d
JB
8280
8281static void
a6d9a66e 8282print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8283{
e29a4733 8284 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8285 struct value_print_options opts;
79a45e25 8286 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8287
8288 get_user_print_options (&opts);
4a64f543
MS
8289 /* Field 4, the address, is omitted (which makes the columns not
8290 line up too nicely with the headers, but the effect is relatively
8291 readable). */
79a45b7d 8292 if (opts.addressprint)
ce78b96d
JB
8293 ui_out_field_skip (uiout, "addr");
8294 annotate_field (5);
8295 ui_out_text (uiout, "vfork");
e29a4733 8296 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8297 {
8298 ui_out_text (uiout, ", process ");
8299 ui_out_field_int (uiout, "what",
e29a4733 8300 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8301 ui_out_spaces (uiout, 1);
8302 }
8ac3646f
TT
8303
8304 if (ui_out_is_mi_like_p (uiout))
8305 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8306}
8307
8308/* Implement the "print_mention" breakpoint_ops method for vfork
8309 catchpoints. */
8310
8311static void
8312print_mention_catch_vfork (struct breakpoint *b)
8313{
8314 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8315}
8316
6149aea9
PA
8317/* Implement the "print_recreate" breakpoint_ops method for vfork
8318 catchpoints. */
8319
8320static void
8321print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8322{
8323 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8324 print_recreate_thread (b, fp);
6149aea9
PA
8325}
8326
ce78b96d
JB
8327/* The breakpoint_ops structure to be used in vfork catchpoints. */
8328
2060206e 8329static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8330
edcc5120
TT
8331/* An instance of this type is used to represent an solib catchpoint.
8332 It includes a "struct breakpoint" as a kind of base class; users
8333 downcast to "struct breakpoint *" when needed. A breakpoint is
8334 really of this type iff its ops pointer points to
8335 CATCH_SOLIB_BREAKPOINT_OPS. */
8336
8337struct solib_catchpoint
8338{
8339 /* The base class. */
8340 struct breakpoint base;
8341
8342 /* True for "catch load", false for "catch unload". */
8343 unsigned char is_load;
8344
8345 /* Regular expression to match, if any. COMPILED is only valid when
8346 REGEX is non-NULL. */
8347 char *regex;
8348 regex_t compiled;
8349};
8350
8351static void
8352dtor_catch_solib (struct breakpoint *b)
8353{
8354 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8355
8356 if (self->regex)
8357 regfree (&self->compiled);
8358 xfree (self->regex);
8359
8360 base_breakpoint_ops.dtor (b);
8361}
8362
8363static int
8364insert_catch_solib (struct bp_location *ignore)
8365{
8366 return 0;
8367}
8368
8369static int
73971819 8370remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8371{
8372 return 0;
8373}
8374
8375static int
8376breakpoint_hit_catch_solib (const struct bp_location *bl,
8377 struct address_space *aspace,
8378 CORE_ADDR bp_addr,
8379 const struct target_waitstatus *ws)
8380{
8381 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8382 struct breakpoint *other;
8383
8384 if (ws->kind == TARGET_WAITKIND_LOADED)
8385 return 1;
8386
8387 ALL_BREAKPOINTS (other)
8388 {
8389 struct bp_location *other_bl;
8390
8391 if (other == bl->owner)
8392 continue;
8393
8394 if (other->type != bp_shlib_event)
8395 continue;
8396
8397 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8398 continue;
8399
8400 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8401 {
8402 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8403 return 1;
8404 }
8405 }
8406
8407 return 0;
8408}
8409
8410static void
8411check_status_catch_solib (struct bpstats *bs)
8412{
8413 struct solib_catchpoint *self
8414 = (struct solib_catchpoint *) bs->breakpoint_at;
8415 int ix;
8416
8417 if (self->is_load)
8418 {
8419 struct so_list *iter;
8420
8421 for (ix = 0;
8422 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8423 ix, iter);
8424 ++ix)
8425 {
8426 if (!self->regex
8427 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8428 return;
8429 }
8430 }
8431 else
8432 {
8433 char *iter;
8434
8435 for (ix = 0;
8436 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8437 ix, iter);
8438 ++ix)
8439 {
8440 if (!self->regex
8441 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8442 return;
8443 }
8444 }
8445
8446 bs->stop = 0;
8447 bs->print_it = print_it_noop;
8448}
8449
8450static enum print_stop_action
8451print_it_catch_solib (bpstat bs)
8452{
8453 struct breakpoint *b = bs->breakpoint_at;
8454 struct ui_out *uiout = current_uiout;
8455
8456 annotate_catchpoint (b->number);
f303dbd6 8457 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8458 if (b->disposition == disp_del)
f303dbd6 8459 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8460 else
f303dbd6 8461 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8462 ui_out_field_int (uiout, "bkptno", b->number);
8463 ui_out_text (uiout, "\n");
8464 if (ui_out_is_mi_like_p (uiout))
8465 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8466 print_solib_event (1);
8467 return PRINT_SRC_AND_LOC;
8468}
8469
8470static void
8471print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8472{
8473 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8474 struct value_print_options opts;
8475 struct ui_out *uiout = current_uiout;
8476 char *msg;
8477
8478 get_user_print_options (&opts);
8479 /* Field 4, the address, is omitted (which makes the columns not
8480 line up too nicely with the headers, but the effect is relatively
8481 readable). */
8482 if (opts.addressprint)
8483 {
8484 annotate_field (4);
8485 ui_out_field_skip (uiout, "addr");
8486 }
8487
8488 annotate_field (5);
8489 if (self->is_load)
8490 {
8491 if (self->regex)
8492 msg = xstrprintf (_("load of library matching %s"), self->regex);
8493 else
8494 msg = xstrdup (_("load of library"));
8495 }
8496 else
8497 {
8498 if (self->regex)
8499 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8500 else
8501 msg = xstrdup (_("unload of library"));
8502 }
8503 ui_out_field_string (uiout, "what", msg);
8504 xfree (msg);
8ac3646f
TT
8505
8506 if (ui_out_is_mi_like_p (uiout))
8507 ui_out_field_string (uiout, "catch-type",
8508 self->is_load ? "load" : "unload");
edcc5120
TT
8509}
8510
8511static void
8512print_mention_catch_solib (struct breakpoint *b)
8513{
8514 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8515
8516 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8517 self->is_load ? "load" : "unload");
8518}
8519
8520static void
8521print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8522{
8523 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8524
8525 fprintf_unfiltered (fp, "%s %s",
8526 b->disposition == disp_del ? "tcatch" : "catch",
8527 self->is_load ? "load" : "unload");
8528 if (self->regex)
8529 fprintf_unfiltered (fp, " %s", self->regex);
8530 fprintf_unfiltered (fp, "\n");
8531}
8532
8533static struct breakpoint_ops catch_solib_breakpoint_ops;
8534
91985142
MG
8535/* Shared helper function (MI and CLI) for creating and installing
8536 a shared object event catchpoint. If IS_LOAD is non-zero then
8537 the events to be caught are load events, otherwise they are
8538 unload events. If IS_TEMP is non-zero the catchpoint is a
8539 temporary one. If ENABLED is non-zero the catchpoint is
8540 created in an enabled state. */
edcc5120 8541
91985142
MG
8542void
8543add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8544{
8545 struct solib_catchpoint *c;
8546 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8547 struct cleanup *cleanup;
8548
edcc5120
TT
8549 if (!arg)
8550 arg = "";
8551 arg = skip_spaces (arg);
8552
4d01a485 8553 c = new solib_catchpoint ();
edcc5120
TT
8554 cleanup = make_cleanup (xfree, c);
8555
8556 if (*arg != '\0')
8557 {
8558 int errcode;
8559
8560 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8561 if (errcode != 0)
8562 {
8563 char *err = get_regcomp_error (errcode, &c->compiled);
8564
8565 make_cleanup (xfree, err);
8566 error (_("Invalid regexp (%s): %s"), err, arg);
8567 }
8568 c->regex = xstrdup (arg);
8569 }
8570
8571 c->is_load = is_load;
91985142 8572 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8573 &catch_solib_breakpoint_ops);
8574
91985142
MG
8575 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8576
edcc5120
TT
8577 discard_cleanups (cleanup);
8578 install_breakpoint (0, &c->base, 1);
8579}
8580
91985142
MG
8581/* A helper function that does all the work for "catch load" and
8582 "catch unload". */
8583
8584static void
8585catch_load_or_unload (char *arg, int from_tty, int is_load,
8586 struct cmd_list_element *command)
8587{
8588 int tempflag;
8589 const int enabled = 1;
8590
8591 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8592
8593 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8594}
8595
edcc5120
TT
8596static void
8597catch_load_command_1 (char *arg, int from_tty,
8598 struct cmd_list_element *command)
8599{
8600 catch_load_or_unload (arg, from_tty, 1, command);
8601}
8602
8603static void
8604catch_unload_command_1 (char *arg, int from_tty,
8605 struct cmd_list_element *command)
8606{
8607 catch_load_or_unload (arg, from_tty, 0, command);
8608}
8609
346774a9
PA
8610/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8611 is non-zero, then make the breakpoint temporary. If COND_STRING is
8612 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8613 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8614
ab04a2af 8615void
346774a9
PA
8616init_catchpoint (struct breakpoint *b,
8617 struct gdbarch *gdbarch, int tempflag,
8618 char *cond_string,
c0a91b2b 8619 const struct breakpoint_ops *ops)
c906108c 8620{
c5aa993b 8621 struct symtab_and_line sal;
346774a9 8622
fe39c653 8623 init_sal (&sal);
6c95b8df 8624 sal.pspace = current_program_space;
c5aa993b 8625
28010a5d 8626 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8627
1b36a34b 8628 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8629 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8630}
8631
28010a5d 8632void
3ea46bff 8633install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8634{
8635 add_to_breakpoint_chain (b);
3a5c3e22 8636 set_breakpoint_number (internal, b);
558a9d82
YQ
8637 if (is_tracepoint (b))
8638 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8639 if (!internal)
8640 mention (b);
c56053d2 8641 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8642
8643 if (update_gll)
44702360 8644 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8645}
8646
9b70b993 8647static void
a6d9a66e
UW
8648create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8649 int tempflag, char *cond_string,
c0a91b2b 8650 const struct breakpoint_ops *ops)
c906108c 8651{
4d01a485 8652 struct fork_catchpoint *c = new fork_catchpoint ();
ce78b96d 8653
e29a4733
PA
8654 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8655
8656 c->forked_inferior_pid = null_ptid;
8657
3ea46bff 8658 install_breakpoint (0, &c->base, 1);
c906108c
SS
8659}
8660
fe798b75
JB
8661/* Exec catchpoints. */
8662
b4d90040
PA
8663/* An instance of this type is used to represent an exec catchpoint.
8664 It includes a "struct breakpoint" as a kind of base class; users
8665 downcast to "struct breakpoint *" when needed. A breakpoint is
8666 really of this type iff its ops pointer points to
8667 CATCH_EXEC_BREAKPOINT_OPS. */
8668
8669struct exec_catchpoint
8670{
8671 /* The base class. */
8672 struct breakpoint base;
8673
8674 /* Filename of a program whose exec triggered this catchpoint.
8675 This field is only valid immediately after this catchpoint has
8676 triggered. */
8677 char *exec_pathname;
8678};
8679
8680/* Implement the "dtor" breakpoint_ops method for exec
8681 catchpoints. */
8682
8683static void
8684dtor_catch_exec (struct breakpoint *b)
8685{
8686 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8687
8688 xfree (c->exec_pathname);
348d480f 8689
2060206e 8690 base_breakpoint_ops.dtor (b);
b4d90040
PA
8691}
8692
77b06cd7
TJB
8693static int
8694insert_catch_exec (struct bp_location *bl)
c906108c 8695{
dfd4cc63 8696 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8697}
c906108c 8698
fe798b75 8699static int
73971819 8700remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8701{
dfd4cc63 8702 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8703}
c906108c 8704
fe798b75 8705static int
f1310107 8706breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8707 struct address_space *aspace, CORE_ADDR bp_addr,
8708 const struct target_waitstatus *ws)
fe798b75 8709{
b4d90040
PA
8710 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8711
f90263c1
TT
8712 if (ws->kind != TARGET_WAITKIND_EXECD)
8713 return 0;
8714
8715 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8716 return 1;
fe798b75 8717}
c906108c 8718
fe798b75 8719static enum print_stop_action
348d480f 8720print_it_catch_exec (bpstat bs)
fe798b75 8721{
36dfb11c 8722 struct ui_out *uiout = current_uiout;
348d480f 8723 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8724 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8725
fe798b75 8726 annotate_catchpoint (b->number);
f303dbd6 8727 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8728 if (b->disposition == disp_del)
f303dbd6 8729 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8730 else
f303dbd6 8731 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8732 if (ui_out_is_mi_like_p (uiout))
8733 {
8734 ui_out_field_string (uiout, "reason",
8735 async_reason_lookup (EXEC_ASYNC_EXEC));
8736 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8737 }
8738 ui_out_field_int (uiout, "bkptno", b->number);
8739 ui_out_text (uiout, " (exec'd ");
8740 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8741 ui_out_text (uiout, "), ");
8742
fe798b75 8743 return PRINT_SRC_AND_LOC;
c906108c
SS
8744}
8745
fe798b75 8746static void
a6d9a66e 8747print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8748{
b4d90040 8749 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8750 struct value_print_options opts;
79a45e25 8751 struct ui_out *uiout = current_uiout;
fe798b75
JB
8752
8753 get_user_print_options (&opts);
8754
8755 /* Field 4, the address, is omitted (which makes the columns
8756 not line up too nicely with the headers, but the effect
8757 is relatively readable). */
8758 if (opts.addressprint)
8759 ui_out_field_skip (uiout, "addr");
8760 annotate_field (5);
8761 ui_out_text (uiout, "exec");
b4d90040 8762 if (c->exec_pathname != NULL)
fe798b75
JB
8763 {
8764 ui_out_text (uiout, ", program \"");
b4d90040 8765 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8766 ui_out_text (uiout, "\" ");
8767 }
8ac3646f
TT
8768
8769 if (ui_out_is_mi_like_p (uiout))
8770 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8771}
8772
8773static void
8774print_mention_catch_exec (struct breakpoint *b)
8775{
8776 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8777}
8778
6149aea9
PA
8779/* Implement the "print_recreate" breakpoint_ops method for exec
8780 catchpoints. */
8781
8782static void
8783print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8784{
8785 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8786 print_recreate_thread (b, fp);
6149aea9
PA
8787}
8788
2060206e 8789static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8790
c906108c 8791static int
fba45db2 8792hw_breakpoint_used_count (void)
c906108c 8793{
c906108c 8794 int i = 0;
f1310107
TJB
8795 struct breakpoint *b;
8796 struct bp_location *bl;
c906108c
SS
8797
8798 ALL_BREAKPOINTS (b)
c5aa993b 8799 {
d6b74ac4 8800 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8801 for (bl = b->loc; bl; bl = bl->next)
8802 {
8803 /* Special types of hardware breakpoints may use more than
8804 one register. */
348d480f 8805 i += b->ops->resources_needed (bl);
f1310107 8806 }
c5aa993b 8807 }
c906108c
SS
8808
8809 return i;
8810}
8811
a1398e0c
PA
8812/* Returns the resources B would use if it were a hardware
8813 watchpoint. */
8814
c906108c 8815static int
a1398e0c 8816hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8817{
c906108c 8818 int i = 0;
e09342b5 8819 struct bp_location *bl;
c906108c 8820
a1398e0c
PA
8821 if (!breakpoint_enabled (b))
8822 return 0;
8823
8824 for (bl = b->loc; bl; bl = bl->next)
8825 {
8826 /* Special types of hardware watchpoints may use more than
8827 one register. */
8828 i += b->ops->resources_needed (bl);
8829 }
8830
8831 return i;
8832}
8833
8834/* Returns the sum the used resources of all hardware watchpoints of
8835 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8836 the sum of the used resources of all hardware watchpoints of other
8837 types _not_ TYPE. */
8838
8839static int
8840hw_watchpoint_used_count_others (struct breakpoint *except,
8841 enum bptype type, int *other_type_used)
8842{
8843 int i = 0;
8844 struct breakpoint *b;
8845
c906108c
SS
8846 *other_type_used = 0;
8847 ALL_BREAKPOINTS (b)
e09342b5 8848 {
a1398e0c
PA
8849 if (b == except)
8850 continue;
e09342b5
TJB
8851 if (!breakpoint_enabled (b))
8852 continue;
8853
a1398e0c
PA
8854 if (b->type == type)
8855 i += hw_watchpoint_use_count (b);
8856 else if (is_hardware_watchpoint (b))
8857 *other_type_used = 1;
e09342b5
TJB
8858 }
8859
c906108c
SS
8860 return i;
8861}
8862
c906108c 8863void
fba45db2 8864disable_watchpoints_before_interactive_call_start (void)
c906108c 8865{
c5aa993b 8866 struct breakpoint *b;
c906108c
SS
8867
8868 ALL_BREAKPOINTS (b)
c5aa993b 8869 {
cc60f2e3 8870 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8871 {
b5de0fa7 8872 b->enable_state = bp_call_disabled;
44702360 8873 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8874 }
8875 }
c906108c
SS
8876}
8877
8878void
fba45db2 8879enable_watchpoints_after_interactive_call_stop (void)
c906108c 8880{
c5aa993b 8881 struct breakpoint *b;
c906108c
SS
8882
8883 ALL_BREAKPOINTS (b)
c5aa993b 8884 {
cc60f2e3 8885 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8886 {
b5de0fa7 8887 b->enable_state = bp_enabled;
44702360 8888 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8889 }
8890 }
c906108c
SS
8891}
8892
8bea4e01
UW
8893void
8894disable_breakpoints_before_startup (void)
8895{
6c95b8df 8896 current_program_space->executing_startup = 1;
44702360 8897 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8898}
8899
8900void
8901enable_breakpoints_after_startup (void)
8902{
6c95b8df 8903 current_program_space->executing_startup = 0;
f8eba3c6 8904 breakpoint_re_set ();
8bea4e01
UW
8905}
8906
7c16b83e
PA
8907/* Create a new single-step breakpoint for thread THREAD, with no
8908 locations. */
c906108c 8909
7c16b83e
PA
8910static struct breakpoint *
8911new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8912{
4d01a485 8913 struct breakpoint *b = new breakpoint ();
7c16b83e
PA
8914
8915 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8916 &momentary_breakpoint_ops);
8917
8918 b->disposition = disp_donttouch;
8919 b->frame_id = null_frame_id;
8920
8921 b->thread = thread;
8922 gdb_assert (b->thread != 0);
8923
8924 add_to_breakpoint_chain (b);
8925
8926 return b;
8927}
8928
8929/* Set a momentary breakpoint of type TYPE at address specified by
8930 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8931 frame. */
c906108c
SS
8932
8933struct breakpoint *
a6d9a66e
UW
8934set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8935 struct frame_id frame_id, enum bptype type)
c906108c 8936{
52f0bd74 8937 struct breakpoint *b;
edb3359d 8938
193facb3
JK
8939 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8940 tail-called one. */
8941 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8942
06edf0c0 8943 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8944 b->enable_state = bp_enabled;
8945 b->disposition = disp_donttouch;
818dd999 8946 b->frame_id = frame_id;
c906108c 8947
4a64f543
MS
8948 /* If we're debugging a multi-threaded program, then we want
8949 momentary breakpoints to be active in only a single thread of
8950 control. */
39f77062 8951 if (in_thread_list (inferior_ptid))
5d5658a1 8952 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8953
44702360 8954 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8955
c906108c
SS
8956 return b;
8957}
611c83ae 8958
06edf0c0 8959/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8960 The new breakpoint will have type TYPE, use OPS as its
8961 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8962
06edf0c0
PA
8963static struct breakpoint *
8964momentary_breakpoint_from_master (struct breakpoint *orig,
8965 enum bptype type,
a1aa2221
LM
8966 const struct breakpoint_ops *ops,
8967 int loc_enabled)
e58b0e63
PA
8968{
8969 struct breakpoint *copy;
8970
06edf0c0 8971 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8972 copy->loc = allocate_bp_location (copy);
0e30163f 8973 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8974
a6d9a66e 8975 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8976 copy->loc->requested_address = orig->loc->requested_address;
8977 copy->loc->address = orig->loc->address;
8978 copy->loc->section = orig->loc->section;
6c95b8df 8979 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8980 copy->loc->probe = orig->loc->probe;
f8eba3c6 8981 copy->loc->line_number = orig->loc->line_number;
2f202fde 8982 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8983 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8984 copy->frame_id = orig->frame_id;
8985 copy->thread = orig->thread;
6c95b8df 8986 copy->pspace = orig->pspace;
e58b0e63
PA
8987
8988 copy->enable_state = bp_enabled;
8989 copy->disposition = disp_donttouch;
8990 copy->number = internal_breakpoint_number--;
8991
44702360 8992 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8993 return copy;
8994}
8995
06edf0c0
PA
8996/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8997 ORIG is NULL. */
8998
8999struct breakpoint *
9000clone_momentary_breakpoint (struct breakpoint *orig)
9001{
9002 /* If there's nothing to clone, then return nothing. */
9003 if (orig == NULL)
9004 return NULL;
9005
a1aa2221 9006 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9007}
9008
611c83ae 9009struct breakpoint *
a6d9a66e
UW
9010set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9011 enum bptype type)
611c83ae
PA
9012{
9013 struct symtab_and_line sal;
9014
9015 sal = find_pc_line (pc, 0);
9016 sal.pc = pc;
9017 sal.section = find_pc_overlay (pc);
9018 sal.explicit_pc = 1;
9019
a6d9a66e 9020 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9021}
c906108c 9022\f
c5aa993b 9023
c906108c
SS
9024/* Tell the user we have just set a breakpoint B. */
9025
9026static void
fba45db2 9027mention (struct breakpoint *b)
c906108c 9028{
348d480f 9029 b->ops->print_mention (b);
79a45e25 9030 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9031 return;
c906108c
SS
9032 printf_filtered ("\n");
9033}
c906108c 9034\f
c5aa993b 9035
1a853c52
PA
9036static int bp_loc_is_permanent (struct bp_location *loc);
9037
0d381245 9038static struct bp_location *
39d61571 9039add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9040 const struct symtab_and_line *sal)
9041{
9042 struct bp_location *loc, **tmp;
3742cc8b
YQ
9043 CORE_ADDR adjusted_address;
9044 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9045
9046 if (loc_gdbarch == NULL)
9047 loc_gdbarch = b->gdbarch;
9048
9049 /* Adjust the breakpoint's address prior to allocating a location.
9050 Once we call allocate_bp_location(), that mostly uninitialized
9051 location will be placed on the location chain. Adjustment of the
9052 breakpoint may cause target_read_memory() to be called and we do
9053 not want its scan of the location chain to find a breakpoint and
9054 location that's only been partially initialized. */
9055 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9056 sal->pc, b->type);
0d381245 9057
d30113d4 9058 /* Sort the locations by their ADDRESS. */
39d61571 9059 loc = allocate_bp_location (b);
d30113d4
JK
9060 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9061 tmp = &((*tmp)->next))
0d381245 9062 ;
d30113d4 9063 loc->next = *tmp;
0d381245 9064 *tmp = loc;
3742cc8b 9065
0d381245 9066 loc->requested_address = sal->pc;
3742cc8b 9067 loc->address = adjusted_address;
6c95b8df 9068 loc->pspace = sal->pspace;
729662a5
TT
9069 loc->probe.probe = sal->probe;
9070 loc->probe.objfile = sal->objfile;
6c95b8df 9071 gdb_assert (loc->pspace != NULL);
0d381245 9072 loc->section = sal->section;
3742cc8b 9073 loc->gdbarch = loc_gdbarch;
f8eba3c6 9074 loc->line_number = sal->line;
2f202fde 9075 loc->symtab = sal->symtab;
f8eba3c6 9076
0e30163f
JK
9077 set_breakpoint_location_function (loc,
9078 sal->explicit_pc || sal->explicit_line);
1a853c52 9079
6ae88661
LM
9080 /* While by definition, permanent breakpoints are already present in the
9081 code, we don't mark the location as inserted. Normally one would expect
9082 that GDB could rely on that breakpoint instruction to stop the program,
9083 thus removing the need to insert its own breakpoint, except that executing
9084 the breakpoint instruction can kill the target instead of reporting a
9085 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9086 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9087 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9088 breakpoint be inserted normally results in QEMU knowing about the GDB
9089 breakpoint, and thus trap before the breakpoint instruction is executed.
9090 (If GDB later needs to continue execution past the permanent breakpoint,
9091 it manually increments the PC, thus avoiding executing the breakpoint
9092 instruction.) */
1a853c52 9093 if (bp_loc_is_permanent (loc))
6ae88661 9094 loc->permanent = 1;
1a853c52 9095
0d381245
VP
9096 return loc;
9097}
514f746b
AR
9098\f
9099
1cf4d951 9100/* See breakpoint.h. */
514f746b 9101
1cf4d951
PA
9102int
9103program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9104{
9105 int len;
9106 CORE_ADDR addr;
1afeeb75 9107 const gdb_byte *bpoint;
514f746b 9108 gdb_byte *target_mem;
939c61fa
JK
9109 struct cleanup *cleanup;
9110 int retval = 0;
514f746b 9111
1cf4d951
PA
9112 addr = address;
9113 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9114
9115 /* Software breakpoints unsupported? */
9116 if (bpoint == NULL)
9117 return 0;
9118
224c3ddb 9119 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9120
9121 /* Enable the automatic memory restoration from breakpoints while
9122 we read the memory. Otherwise we could say about our temporary
9123 breakpoints they are permanent. */
9124 cleanup = make_show_memory_breakpoints_cleanup (0);
9125
9126 if (target_read_memory (address, target_mem, len) == 0
9127 && memcmp (target_mem, bpoint, len) == 0)
9128 retval = 1;
9129
9130 do_cleanups (cleanup);
9131
9132 return retval;
9133}
9134
9135/* Return 1 if LOC is pointing to a permanent breakpoint,
9136 return 0 otherwise. */
9137
9138static int
9139bp_loc_is_permanent (struct bp_location *loc)
9140{
9141 struct cleanup *cleanup;
9142 int retval;
9143
514f746b
AR
9144 gdb_assert (loc != NULL);
9145
244558af
LM
9146 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9147 attempt to read from the addresses the locations of these breakpoint types
9148 point to. program_breakpoint_here_p, below, will attempt to read
9149 memory. */
9150 if (!breakpoint_address_is_meaningful (loc->owner))
9151 return 0;
9152
6c95b8df 9153 cleanup = save_current_space_and_thread ();
6c95b8df 9154 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9155
1cf4d951 9156 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9157
939c61fa
JK
9158 do_cleanups (cleanup);
9159
9160 return retval;
514f746b
AR
9161}
9162
e7e0cddf
SS
9163/* Build a command list for the dprintf corresponding to the current
9164 settings of the dprintf style options. */
9165
9166static void
9167update_dprintf_command_list (struct breakpoint *b)
9168{
9169 char *dprintf_args = b->extra_string;
9170 char *printf_line = NULL;
9171
9172 if (!dprintf_args)
9173 return;
9174
9175 dprintf_args = skip_spaces (dprintf_args);
9176
9177 /* Allow a comma, as it may have terminated a location, but don't
9178 insist on it. */
9179 if (*dprintf_args == ',')
9180 ++dprintf_args;
9181 dprintf_args = skip_spaces (dprintf_args);
9182
9183 if (*dprintf_args != '"')
9184 error (_("Bad format string, missing '\"'."));
9185
d3ce09f5 9186 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9187 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9188 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9189 {
9190 if (!dprintf_function)
9191 error (_("No function supplied for dprintf call"));
9192
9193 if (dprintf_channel && strlen (dprintf_channel) > 0)
9194 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9195 dprintf_function,
9196 dprintf_channel,
9197 dprintf_args);
9198 else
9199 printf_line = xstrprintf ("call (void) %s (%s)",
9200 dprintf_function,
9201 dprintf_args);
9202 }
d3ce09f5
SS
9203 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9204 {
9205 if (target_can_run_breakpoint_commands ())
9206 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9207 else
9208 {
9209 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9210 printf_line = xstrprintf ("printf %s", dprintf_args);
9211 }
9212 }
e7e0cddf
SS
9213 else
9214 internal_error (__FILE__, __LINE__,
9215 _("Invalid dprintf style."));
9216
f28045c2 9217 gdb_assert (printf_line != NULL);
9d6e6e84 9218 /* Manufacture a printf sequence. */
f28045c2 9219 {
8d749320 9220 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9221
f28045c2
YQ
9222 printf_cmd_line->control_type = simple_control;
9223 printf_cmd_line->body_count = 0;
9224 printf_cmd_line->body_list = NULL;
9d6e6e84 9225 printf_cmd_line->next = NULL;
f28045c2 9226 printf_cmd_line->line = printf_line;
e7e0cddf 9227
f28045c2
YQ
9228 breakpoint_set_commands (b, printf_cmd_line);
9229 }
e7e0cddf
SS
9230}
9231
9232/* Update all dprintf commands, making their command lists reflect
9233 current style settings. */
9234
9235static void
9236update_dprintf_commands (char *args, int from_tty,
9237 struct cmd_list_element *c)
9238{
9239 struct breakpoint *b;
9240
9241 ALL_BREAKPOINTS (b)
9242 {
9243 if (b->type == bp_dprintf)
9244 update_dprintf_command_list (b);
9245 }
9246}
c3f6f71d 9247
f00aae0f
KS
9248/* Create a breakpoint with SAL as location. Use LOCATION
9249 as a description of the location, and COND_STRING
b35a8b2f
DE
9250 as condition expression. If LOCATION is NULL then create an
9251 "address location" from the address in the SAL. */
018d34a4
VP
9252
9253static void
d9b3f62e 9254init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9255 struct symtabs_and_lines sals,
9256 struct event_location *location,
f8eba3c6 9257 char *filter, char *cond_string,
e7e0cddf 9258 char *extra_string,
d9b3f62e
PA
9259 enum bptype type, enum bpdisp disposition,
9260 int thread, int task, int ignore_count,
c0a91b2b 9261 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9262 int enabled, int internal, unsigned flags,
9263 int display_canonical)
018d34a4 9264{
0d381245 9265 int i;
018d34a4
VP
9266
9267 if (type == bp_hardware_breakpoint)
9268 {
fbbd034e
AS
9269 int target_resources_ok;
9270
9271 i = hw_breakpoint_used_count ();
9272 target_resources_ok =
9273 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9274 i + 1, 0);
9275 if (target_resources_ok == 0)
9276 error (_("No hardware breakpoint support in the target."));
9277 else if (target_resources_ok < 0)
9278 error (_("Hardware breakpoints used exceeds limit."));
9279 }
9280
6c95b8df
PA
9281 gdb_assert (sals.nelts > 0);
9282
0d381245
VP
9283 for (i = 0; i < sals.nelts; ++i)
9284 {
9285 struct symtab_and_line sal = sals.sals[i];
9286 struct bp_location *loc;
9287
9288 if (from_tty)
5af949e3
UW
9289 {
9290 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9291 if (!loc_gdbarch)
9292 loc_gdbarch = gdbarch;
9293
9294 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9295 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9296 }
0d381245
VP
9297
9298 if (i == 0)
9299 {
d9b3f62e 9300 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9301 b->thread = thread;
4a306c9a 9302 b->task = task;
855a6e68 9303
0d381245 9304 b->cond_string = cond_string;
e7e0cddf 9305 b->extra_string = extra_string;
0d381245 9306 b->ignore_count = ignore_count;
41447f92 9307 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9308 b->disposition = disposition;
6c95b8df 9309
44f238bb
PA
9310 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9311 b->loc->inserted = 1;
9312
0fb4aa4b
PA
9313 if (type == bp_static_tracepoint)
9314 {
d9b3f62e 9315 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9316 struct static_tracepoint_marker marker;
9317
983af33b 9318 if (strace_marker_p (b))
0fb4aa4b
PA
9319 {
9320 /* We already know the marker exists, otherwise, we
9321 wouldn't see a sal for it. */
f00aae0f
KS
9322 const char *p = &event_location_to_string (b->location)[3];
9323 const char *endp;
0fb4aa4b 9324 char *marker_str;
0fb4aa4b 9325
f00aae0f 9326 p = skip_spaces_const (p);
0fb4aa4b 9327
f00aae0f 9328 endp = skip_to_space_const (p);
0fb4aa4b
PA
9329
9330 marker_str = savestring (p, endp - p);
d9b3f62e 9331 t->static_trace_marker_id = marker_str;
0fb4aa4b 9332
3e43a32a
MS
9333 printf_filtered (_("Probed static tracepoint "
9334 "marker \"%s\"\n"),
d9b3f62e 9335 t->static_trace_marker_id);
0fb4aa4b
PA
9336 }
9337 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9338 {
d9b3f62e 9339 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9340 release_static_tracepoint_marker (&marker);
9341
3e43a32a
MS
9342 printf_filtered (_("Probed static tracepoint "
9343 "marker \"%s\"\n"),
d9b3f62e 9344 t->static_trace_marker_id);
0fb4aa4b
PA
9345 }
9346 else
3e43a32a
MS
9347 warning (_("Couldn't determine the static "
9348 "tracepoint marker to probe"));
0fb4aa4b
PA
9349 }
9350
0d381245
VP
9351 loc = b->loc;
9352 }
9353 else
018d34a4 9354 {
39d61571 9355 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9356 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9357 loc->inserted = 1;
0d381245
VP
9358 }
9359
9360 if (b->cond_string)
9361 {
bbc13ae3
KS
9362 const char *arg = b->cond_string;
9363
1bb9788d
TT
9364 loc->cond = parse_exp_1 (&arg, loc->address,
9365 block_for_pc (loc->address), 0);
0d381245 9366 if (*arg)
588ae58c 9367 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9368 }
e7e0cddf
SS
9369
9370 /* Dynamic printf requires and uses additional arguments on the
9371 command line, otherwise it's an error. */
9372 if (type == bp_dprintf)
9373 {
9374 if (b->extra_string)
9375 update_dprintf_command_list (b);
9376 else
9377 error (_("Format string required"));
9378 }
9379 else if (b->extra_string)
588ae58c 9380 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9381 }
018d34a4 9382
56435ebe 9383 b->display_canonical = display_canonical;
f00aae0f
KS
9384 if (location != NULL)
9385 b->location = location;
018d34a4 9386 else
305e13e6
JB
9387 {
9388 const char *addr_string = NULL;
9389 int addr_string_len = 0;
9390
9391 if (location != NULL)
9392 addr_string = event_location_to_string (location);
9393 if (addr_string != NULL)
9394 addr_string_len = strlen (addr_string);
9395
9396 b->location = new_address_location (b->loc->address,
9397 addr_string, addr_string_len);
9398 }
f8eba3c6 9399 b->filter = filter;
d9b3f62e 9400}
018d34a4 9401
d9b3f62e
PA
9402static void
9403create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9404 struct symtabs_and_lines sals,
9405 struct event_location *location,
f8eba3c6 9406 char *filter, char *cond_string,
e7e0cddf 9407 char *extra_string,
d9b3f62e
PA
9408 enum bptype type, enum bpdisp disposition,
9409 int thread, int task, int ignore_count,
c0a91b2b 9410 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9411 int enabled, int internal, unsigned flags,
9412 int display_canonical)
d9b3f62e
PA
9413{
9414 struct breakpoint *b;
9415 struct cleanup *old_chain;
9416
9417 if (is_tracepoint_type (type))
9418 {
9419 struct tracepoint *t;
9420
4d01a485 9421 t = new tracepoint ();
d9b3f62e
PA
9422 b = &t->base;
9423 }
9424 else
4d01a485 9425 b = new breakpoint ();
d9b3f62e
PA
9426
9427 old_chain = make_cleanup (xfree, b);
9428
9429 init_breakpoint_sal (b, gdbarch,
f00aae0f 9430 sals, location,
e7e0cddf 9431 filter, cond_string, extra_string,
d9b3f62e
PA
9432 type, disposition,
9433 thread, task, ignore_count,
9434 ops, from_tty,
44f238bb
PA
9435 enabled, internal, flags,
9436 display_canonical);
d9b3f62e
PA
9437 discard_cleanups (old_chain);
9438
3ea46bff 9439 install_breakpoint (internal, b, 0);
018d34a4
VP
9440}
9441
9442/* Add SALS.nelts breakpoints to the breakpoint table. For each
9443 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9444 value. COND_STRING, if not NULL, specified the condition to be
9445 used for all breakpoints. Essentially the only case where
9446 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9447 function. In that case, it's still not possible to specify
9448 separate conditions for different overloaded functions, so
9449 we take just a single condition string.
9450
c3f6f71d 9451 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9452 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9453 array contents). If the function fails (error() is called), the
9454 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9455 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9456
9457static void
8cdf0e15 9458create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9459 struct linespec_result *canonical,
e7e0cddf 9460 char *cond_string, char *extra_string,
8cdf0e15
VP
9461 enum bptype type, enum bpdisp disposition,
9462 int thread, int task, int ignore_count,
c0a91b2b 9463 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9464 int enabled, int internal, unsigned flags)
c906108c 9465{
018d34a4 9466 int i;
f8eba3c6 9467 struct linespec_sals *lsal;
cc59ec59 9468
f8eba3c6
TT
9469 if (canonical->pre_expanded)
9470 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9471
9472 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9473 {
f00aae0f 9474 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9475 'break', without arguments. */
f00aae0f
KS
9476 struct event_location *location
9477 = (canonical->location != NULL
9478 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9479 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9480 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9481
f8eba3c6
TT
9482 make_cleanup (xfree, filter_string);
9483 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9484 location,
f8eba3c6 9485 filter_string,
e7e0cddf
SS
9486 cond_string, extra_string,
9487 type, disposition,
84f4c1fe 9488 thread, task, ignore_count, ops,
44f238bb 9489 from_tty, enabled, internal, flags,
56435ebe 9490 canonical->special_display);
f8eba3c6 9491 discard_cleanups (inner);
c3f6f71d 9492 }
c3f6f71d 9493}
c906108c 9494
f00aae0f 9495/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9496 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9497 addresses found. LOCATION points to the end of the SAL (for
9498 linespec locations).
9998af43
TJB
9499
9500 The array and the line spec strings are allocated on the heap, it is
9501 the caller's responsibility to free them. */
c906108c 9502
b9362cc7 9503static void
f00aae0f 9504parse_breakpoint_sals (const struct event_location *location,
58438ac1 9505 struct linespec_result *canonical)
c3f6f71d 9506{
f00aae0f
KS
9507 struct symtab_and_line cursal;
9508
9509 if (event_location_type (location) == LINESPEC_LOCATION)
9510 {
9511 const char *address = get_linespec_location (location);
9512
9513 if (address == NULL)
9514 {
9515 /* The last displayed codepoint, if it's valid, is our default
9516 breakpoint address. */
9517 if (last_displayed_sal_is_valid ())
9518 {
9519 struct linespec_sals lsal;
9520 struct symtab_and_line sal;
9521 CORE_ADDR pc;
9522
9523 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9524 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9525
9526 /* Set sal's pspace, pc, symtab, and line to the values
9527 corresponding to the last call to print_frame_info.
9528 Be sure to reinitialize LINE with NOTCURRENT == 0
9529 as the breakpoint line number is inappropriate otherwise.
9530 find_pc_line would adjust PC, re-set it back. */
9531 get_last_displayed_sal (&sal);
9532 pc = sal.pc;
9533 sal = find_pc_line (pc, 0);
9534
9535 /* "break" without arguments is equivalent to "break *PC"
9536 where PC is the last displayed codepoint's address. So
9537 make sure to set sal.explicit_pc to prevent GDB from
9538 trying to expand the list of sals to include all other
9539 instances with the same symtab and line. */
9540 sal.pc = pc;
9541 sal.explicit_pc = 1;
9542
9543 lsal.sals.sals[0] = sal;
9544 lsal.sals.nelts = 1;
9545 lsal.canonical = NULL;
9546
9547 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9548 return;
9549 }
9550 else
9551 error (_("No default breakpoint address now."));
c906108c 9552 }
c906108c 9553 }
f00aae0f
KS
9554
9555 /* Force almost all breakpoints to be in terms of the
9556 current_source_symtab (which is decode_line_1's default).
9557 This should produce the results we want almost all of the
9558 time while leaving default_breakpoint_* alone.
9559
9560 ObjC: However, don't match an Objective-C method name which
9561 may have a '+' or '-' succeeded by a '['. */
9562 cursal = get_current_source_symtab_and_line ();
9563 if (last_displayed_sal_is_valid ())
c906108c 9564 {
f00aae0f 9565 const char *address = NULL;
cc80f267 9566
f00aae0f
KS
9567 if (event_location_type (location) == LINESPEC_LOCATION)
9568 address = get_linespec_location (location);
cc80f267 9569
f00aae0f
KS
9570 if (!cursal.symtab
9571 || (address != NULL
9572 && strchr ("+-", address[0]) != NULL
9573 && address[1] != '['))
9574 {
c2f4122d 9575 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9576 get_last_displayed_symtab (),
9577 get_last_displayed_line (),
9578 canonical, NULL, NULL);
9579 return;
9580 }
c906108c 9581 }
f00aae0f 9582
c2f4122d 9583 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9584 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9585}
c906108c 9586
c906108c 9587
c3f6f71d 9588/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9589 inserted as a breakpoint. If it can't throw an error. */
c906108c 9590
b9362cc7 9591static void
23e7acfb 9592breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9593{
9594 int i;
cc59ec59 9595
c3f6f71d 9596 for (i = 0; i < sals->nelts; i++)
ee53e872 9597 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9598}
9599
7a697b8d
SS
9600/* Fast tracepoints may have restrictions on valid locations. For
9601 instance, a fast tracepoint using a jump instead of a trap will
9602 likely have to overwrite more bytes than a trap would, and so can
9603 only be placed where the instruction is longer than the jump, or a
9604 multi-instruction sequence does not have a jump into the middle of
9605 it, etc. */
9606
9607static void
9608check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9609 struct symtabs_and_lines *sals)
9610{
9611 int i, rslt;
9612 struct symtab_and_line *sal;
9613 char *msg;
9614 struct cleanup *old_chain;
9615
9616 for (i = 0; i < sals->nelts; i++)
9617 {
f8eba3c6
TT
9618 struct gdbarch *sarch;
9619
7a697b8d
SS
9620 sal = &sals->sals[i];
9621
f8eba3c6
TT
9622 sarch = get_sal_arch (*sal);
9623 /* We fall back to GDBARCH if there is no architecture
9624 associated with SAL. */
9625 if (sarch == NULL)
9626 sarch = gdbarch;
6b940e6a 9627 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9628 old_chain = make_cleanup (xfree, msg);
9629
9630 if (!rslt)
53c3572a 9631 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9632 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9633
9634 do_cleanups (old_chain);
9635 }
9636}
9637
018d34a4
VP
9638/* Given TOK, a string specification of condition and thread, as
9639 accepted by the 'break' command, extract the condition
9640 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9641 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9642 If no condition is found, *COND_STRING is set to NULL.
9643 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9644
9645static void
bbc13ae3 9646find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9647 char **cond_string, int *thread, int *task,
9648 char **rest)
018d34a4
VP
9649{
9650 *cond_string = NULL;
9651 *thread = -1;
ed1d1739
KS
9652 *task = 0;
9653 *rest = NULL;
9654
018d34a4
VP
9655 while (tok && *tok)
9656 {
bbc13ae3 9657 const char *end_tok;
018d34a4 9658 int toklen;
bbc13ae3
KS
9659 const char *cond_start = NULL;
9660 const char *cond_end = NULL;
cc59ec59 9661
bbc13ae3 9662 tok = skip_spaces_const (tok);
e7e0cddf
SS
9663
9664 if ((*tok == '"' || *tok == ',') && rest)
9665 {
9666 *rest = savestring (tok, strlen (tok));
9667 return;
9668 }
9669
bbc13ae3 9670 end_tok = skip_to_space_const (tok);
d634f2de 9671
018d34a4 9672 toklen = end_tok - tok;
d634f2de 9673
018d34a4
VP
9674 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9675 {
9676 tok = cond_start = end_tok + 1;
4d01a485 9677 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9678 cond_end = tok;
d634f2de 9679 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9680 }
9681 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9682 {
5d5658a1
PA
9683 const char *tmptok;
9684 struct thread_info *thr;
d634f2de 9685
018d34a4 9686 tok = end_tok + 1;
5d5658a1 9687 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9688 if (tok == tmptok)
9689 error (_("Junk after thread keyword."));
5d5658a1 9690 *thread = thr->global_num;
bbc13ae3 9691 tok = tmptok;
018d34a4 9692 }
4a306c9a
JB
9693 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9694 {
9695 char *tmptok;
9696
9697 tok = end_tok + 1;
bbc13ae3 9698 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9699 if (tok == tmptok)
9700 error (_("Junk after task keyword."));
9701 if (!valid_task_id (*task))
b6199126 9702 error (_("Unknown task %d."), *task);
bbc13ae3 9703 tok = tmptok;
4a306c9a 9704 }
e7e0cddf
SS
9705 else if (rest)
9706 {
9707 *rest = savestring (tok, strlen (tok));
ccab2054 9708 return;
e7e0cddf 9709 }
018d34a4
VP
9710 else
9711 error (_("Junk at end of arguments."));
9712 }
9713}
9714
0fb4aa4b
PA
9715/* Decode a static tracepoint marker spec. */
9716
9717static struct symtabs_and_lines
f00aae0f 9718decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9719{
9720 VEC(static_tracepoint_marker_p) *markers = NULL;
9721 struct symtabs_and_lines sals;
0fb4aa4b 9722 struct cleanup *old_chain;
f00aae0f
KS
9723 const char *p = &(*arg_p)[3];
9724 const char *endp;
0fb4aa4b
PA
9725 char *marker_str;
9726 int i;
9727
f00aae0f 9728 p = skip_spaces_const (p);
0fb4aa4b 9729
f00aae0f 9730 endp = skip_to_space_const (p);
0fb4aa4b
PA
9731
9732 marker_str = savestring (p, endp - p);
9733 old_chain = make_cleanup (xfree, marker_str);
9734
9735 markers = target_static_tracepoint_markers_by_strid (marker_str);
9736 if (VEC_empty(static_tracepoint_marker_p, markers))
9737 error (_("No known static tracepoint marker named %s"), marker_str);
9738
9739 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9740 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9741
9742 for (i = 0; i < sals.nelts; i++)
9743 {
9744 struct static_tracepoint_marker *marker;
9745
9746 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9747
9748 init_sal (&sals.sals[i]);
9749
9750 sals.sals[i] = find_pc_line (marker->address, 0);
9751 sals.sals[i].pc = marker->address;
9752
9753 release_static_tracepoint_marker (marker);
9754 }
9755
9756 do_cleanups (old_chain);
9757
9758 *arg_p = endp;
9759 return sals;
9760}
9761
f00aae0f 9762/* See breakpoint.h. */
0101ce28 9763
8cdf0e15
VP
9764int
9765create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9766 const struct event_location *location, char *cond_string,
e7e0cddf 9767 int thread, char *extra_string,
f00aae0f 9768 int parse_extra,
0fb4aa4b 9769 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9770 int ignore_count,
9771 enum auto_boolean pending_break_support,
c0a91b2b 9772 const struct breakpoint_ops *ops,
44f238bb
PA
9773 int from_tty, int enabled, int internal,
9774 unsigned flags)
c3f6f71d 9775{
7efd8fc2 9776 struct linespec_result canonical;
c3f6f71d 9777 struct cleanup *old_chain;
80c99de1 9778 struct cleanup *bkpt_chain = NULL;
0101ce28 9779 int pending = 0;
4a306c9a 9780 int task = 0;
86b17b60 9781 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9782
348d480f
PA
9783 gdb_assert (ops != NULL);
9784
f00aae0f
KS
9785 /* If extra_string isn't useful, set it to NULL. */
9786 if (extra_string != NULL && *extra_string == '\0')
9787 extra_string = NULL;
9788
7efd8fc2 9789 init_linespec_result (&canonical);
c3f6f71d 9790
492d29ea 9791 TRY
b78a6381 9792 {
f00aae0f 9793 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9794 }
492d29ea 9795 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9796 {
492d29ea
PA
9797 /* If caller is interested in rc value from parse, set
9798 value. */
9799 if (e.error == NOT_FOUND_ERROR)
0101ce28 9800 {
05ff989b
AC
9801 /* If pending breakpoint support is turned off, throw
9802 error. */
fa8d40ab
JJ
9803
9804 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9805 throw_exception (e);
9806
9807 exception_print (gdb_stderr, e);
fa8d40ab 9808
05ff989b
AC
9809 /* If pending breakpoint support is auto query and the user
9810 selects no, then simply return the error code. */
059fb39f 9811 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9812 && !nquery (_("Make %s pending on future shared library load? "),
9813 bptype_string (type_wanted)))
fd9b8c24 9814 return 0;
fa8d40ab 9815
05ff989b
AC
9816 /* At this point, either the user was queried about setting
9817 a pending breakpoint and selected yes, or pending
9818 breakpoint behavior is on and thus a pending breakpoint
9819 is defaulted on behalf of the user. */
f00aae0f 9820 pending = 1;
0101ce28 9821 }
492d29ea
PA
9822 else
9823 throw_exception (e);
0101ce28 9824 }
492d29ea
PA
9825 END_CATCH
9826
f00aae0f 9827 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9828 return 0;
c3f6f71d 9829
4a64f543 9830 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9831 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9832
c3f6f71d
JM
9833 /* ----------------------------- SNIP -----------------------------
9834 Anything added to the cleanup chain beyond this point is assumed
9835 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9836 then the memory is not reclaimed. */
9837 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9838
c3f6f71d
JM
9839 /* Resolve all line numbers to PC's and verify that the addresses
9840 are ok for the target. */
0101ce28 9841 if (!pending)
f8eba3c6
TT
9842 {
9843 int ix;
9844 struct linespec_sals *iter;
9845
9846 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9847 breakpoint_sals_to_pc (&iter->sals);
9848 }
c3f6f71d 9849
7a697b8d 9850 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9851 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9852 {
9853 int ix;
9854 struct linespec_sals *iter;
9855
9856 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9857 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9858 }
7a697b8d 9859
c3f6f71d
JM
9860 /* Verify that condition can be parsed, before setting any
9861 breakpoints. Allocate a separate condition expression for each
4a64f543 9862 breakpoint. */
0101ce28 9863 if (!pending)
c3f6f71d 9864 {
f00aae0f 9865 if (parse_extra)
72b2ff0e 9866 {
0878d0fa 9867 char *rest;
52d361e1
YQ
9868 struct linespec_sals *lsal;
9869
9870 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9871
0878d0fa
YQ
9872 /* Here we only parse 'arg' to separate condition
9873 from thread number, so parsing in context of first
9874 sal is OK. When setting the breakpoint we'll
9875 re-parse it in context of each sal. */
9876
f00aae0f
KS
9877 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9878 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9879 if (cond_string)
9880 make_cleanup (xfree, cond_string);
9881 if (rest)
9882 make_cleanup (xfree, rest);
9883 if (rest)
9884 extra_string = rest;
f00aae0f
KS
9885 else
9886 extra_string = NULL;
72b2ff0e 9887 }
2f069f6f 9888 else
72b2ff0e 9889 {
f00aae0f
KS
9890 if (type_wanted != bp_dprintf
9891 && extra_string != NULL && *extra_string != '\0')
9892 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9893
9894 /* Create a private copy of condition string. */
9895 if (cond_string)
9896 {
9897 cond_string = xstrdup (cond_string);
9898 make_cleanup (xfree, cond_string);
9899 }
9900 /* Create a private copy of any extra string. */
9901 if (extra_string)
9902 {
9903 extra_string = xstrdup (extra_string);
9904 make_cleanup (xfree, extra_string);
9905 }
72b2ff0e 9906 }
0fb4aa4b 9907
52d361e1 9908 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9909 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9910 tempflag ? disp_del : disp_donttouch,
9911 thread, task, ignore_count, ops,
44f238bb 9912 from_tty, enabled, internal, flags);
c906108c 9913 }
0101ce28
JJ
9914 else
9915 {
0101ce28
JJ
9916 struct breakpoint *b;
9917
bfccc43c
YQ
9918 if (is_tracepoint_type (type_wanted))
9919 {
9920 struct tracepoint *t;
9921
4d01a485 9922 t = new tracepoint ();
bfccc43c
YQ
9923 b = &t->base;
9924 }
9925 else
4d01a485 9926 b = new breakpoint ();
bfccc43c
YQ
9927
9928 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9929 b->location = copy_event_location (location);
bfccc43c 9930
f00aae0f
KS
9931 if (parse_extra)
9932 b->cond_string = NULL;
e12c7713
MK
9933 else
9934 {
9935 /* Create a private copy of condition string. */
9936 if (cond_string)
9937 {
9938 cond_string = xstrdup (cond_string);
9939 make_cleanup (xfree, cond_string);
9940 }
9941 b->cond_string = cond_string;
15630549 9942 b->thread = thread;
e12c7713 9943 }
f00aae0f
KS
9944
9945 /* Create a private copy of any extra string. */
9946 if (extra_string != NULL)
9947 {
9948 extra_string = xstrdup (extra_string);
9949 make_cleanup (xfree, extra_string);
9950 }
9951 b->extra_string = extra_string;
0101ce28 9952 b->ignore_count = ignore_count;
0101ce28 9953 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9954 b->condition_not_parsed = 1;
41447f92 9955 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9956 if ((type_wanted != bp_breakpoint
9957 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9958 b->pspace = current_program_space;
8bea4e01 9959
bfccc43c 9960 install_breakpoint (internal, b, 0);
0101ce28
JJ
9961 }
9962
f8eba3c6 9963 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9964 {
3e43a32a
MS
9965 warning (_("Multiple breakpoints were set.\nUse the "
9966 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9967 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9968 }
9969
80c99de1
PA
9970 /* That's it. Discard the cleanups for data inserted into the
9971 breakpoint. */
9972 discard_cleanups (bkpt_chain);
9973 /* But cleanup everything else. */
c3f6f71d 9974 do_cleanups (old_chain);
217dc9e2 9975
80c99de1 9976 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9977 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9978
9979 return 1;
c3f6f71d 9980}
c906108c 9981
348d480f 9982/* Set a breakpoint.
72b2ff0e
VP
9983 ARG is a string describing breakpoint address,
9984 condition, and thread.
9985 FLAG specifies if a breakpoint is hardware on,
9986 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9987 and BP_TEMPFLAG. */
348d480f 9988
98deb0da 9989static void
72b2ff0e 9990break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9991{
72b2ff0e 9992 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9993 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9994 ? bp_hardware_breakpoint
9995 : bp_breakpoint);
55aa24fb 9996 struct breakpoint_ops *ops;
f00aae0f
KS
9997 struct event_location *location;
9998 struct cleanup *cleanup;
9999
10000 location = string_to_event_location (&arg, current_language);
10001 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
10002
10003 /* Matching breakpoints on probes. */
5b56227b
KS
10004 if (location != NULL
10005 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
10006 ops = &bkpt_probe_breakpoint_ops;
10007 else
10008 ops = &bkpt_breakpoint_ops;
c3f6f71d 10009
8cdf0e15 10010 create_breakpoint (get_current_arch (),
f00aae0f
KS
10011 location,
10012 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 10013 tempflag, type_wanted,
8cdf0e15
VP
10014 0 /* Ignore count */,
10015 pending_break_support,
55aa24fb 10016 ops,
8cdf0e15 10017 from_tty,
84f4c1fe 10018 1 /* enabled */,
44f238bb
PA
10019 0 /* internal */,
10020 0);
f00aae0f 10021 do_cleanups (cleanup);
c906108c
SS
10022}
10023
c906108c
SS
10024/* Helper function for break_command_1 and disassemble_command. */
10025
10026void
fba45db2 10027resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10028{
10029 CORE_ADDR pc;
10030
10031 if (sal->pc == 0 && sal->symtab != NULL)
10032 {
10033 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10034 error (_("No line %d in file \"%s\"."),
05cba821 10035 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10036 sal->pc = pc;
6a048695 10037
4a64f543
MS
10038 /* If this SAL corresponds to a breakpoint inserted using a line
10039 number, then skip the function prologue if necessary. */
6a048695 10040 if (sal->explicit_line)
059acae7 10041 skip_prologue_sal (sal);
c906108c
SS
10042 }
10043
10044 if (sal->section == 0 && sal->symtab != NULL)
10045 {
346d1dfe 10046 const struct blockvector *bv;
3977b71f 10047 const struct block *b;
c5aa993b 10048 struct symbol *sym;
c906108c 10049
43f3e411
DE
10050 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10051 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10052 if (bv != NULL)
10053 {
7f0df278 10054 sym = block_linkage_function (b);
c906108c
SS
10055 if (sym != NULL)
10056 {
eb822aa6
DE
10057 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10058 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10059 sym);
c906108c
SS
10060 }
10061 else
10062 {
4a64f543
MS
10063 /* It really is worthwhile to have the section, so we'll
10064 just have to look harder. This case can be executed
10065 if we have line numbers but no functions (as can
10066 happen in assembly source). */
c906108c 10067
7cbd4a93 10068 struct bound_minimal_symbol msym;
6c95b8df
PA
10069 struct cleanup *old_chain = save_current_space_and_thread ();
10070
10071 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10072
10073 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10074 if (msym.minsym)
efd66ac6 10075 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10076
10077 do_cleanups (old_chain);
c906108c
SS
10078 }
10079 }
10080 }
10081}
10082
10083void
fba45db2 10084break_command (char *arg, int from_tty)
c906108c 10085{
db107f19 10086 break_command_1 (arg, 0, from_tty);
c906108c
SS
10087}
10088
c906108c 10089void
fba45db2 10090tbreak_command (char *arg, int from_tty)
c906108c 10091{
db107f19 10092 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10093}
10094
c906108c 10095static void
fba45db2 10096hbreak_command (char *arg, int from_tty)
c906108c 10097{
db107f19 10098 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10099}
10100
10101static void
fba45db2 10102thbreak_command (char *arg, int from_tty)
c906108c 10103{
db107f19 10104 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10105}
10106
10107static void
fba45db2 10108stop_command (char *arg, int from_tty)
c906108c 10109{
a3f17187 10110 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10111Usage: stop in <function | address>\n\
a3f17187 10112 stop at <line>\n"));
c906108c
SS
10113}
10114
10115static void
fba45db2 10116stopin_command (char *arg, int from_tty)
c906108c
SS
10117{
10118 int badInput = 0;
10119
c5aa993b 10120 if (arg == (char *) NULL)
c906108c
SS
10121 badInput = 1;
10122 else if (*arg != '*')
10123 {
10124 char *argptr = arg;
10125 int hasColon = 0;
10126
4a64f543 10127 /* Look for a ':'. If this is a line number specification, then
53a5351d 10128 say it is bad, otherwise, it should be an address or
4a64f543 10129 function/method name. */
c906108c 10130 while (*argptr && !hasColon)
c5aa993b
JM
10131 {
10132 hasColon = (*argptr == ':');
10133 argptr++;
10134 }
c906108c
SS
10135
10136 if (hasColon)
c5aa993b 10137 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10138 else
c5aa993b 10139 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10140 }
10141
10142 if (badInput)
a3f17187 10143 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10144 else
db107f19 10145 break_command_1 (arg, 0, from_tty);
c906108c
SS
10146}
10147
10148static void
fba45db2 10149stopat_command (char *arg, int from_tty)
c906108c
SS
10150{
10151 int badInput = 0;
10152
c5aa993b 10153 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10154 badInput = 1;
10155 else
10156 {
10157 char *argptr = arg;
10158 int hasColon = 0;
10159
4a64f543
MS
10160 /* Look for a ':'. If there is a '::' then get out, otherwise
10161 it is probably a line number. */
c906108c 10162 while (*argptr && !hasColon)
c5aa993b
JM
10163 {
10164 hasColon = (*argptr == ':');
10165 argptr++;
10166 }
c906108c
SS
10167
10168 if (hasColon)
c5aa993b 10169 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10170 else
c5aa993b 10171 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10172 }
10173
10174 if (badInput)
a3f17187 10175 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10176 else
db107f19 10177 break_command_1 (arg, 0, from_tty);
c906108c
SS
10178}
10179
e7e0cddf
SS
10180/* The dynamic printf command is mostly like a regular breakpoint, but
10181 with a prewired command list consisting of a single output command,
10182 built from extra arguments supplied on the dprintf command
10183 line. */
10184
da821c7b 10185static void
e7e0cddf
SS
10186dprintf_command (char *arg, int from_tty)
10187{
f00aae0f
KS
10188 struct event_location *location;
10189 struct cleanup *cleanup;
10190
10191 location = string_to_event_location (&arg, current_language);
10192 cleanup = make_cleanup_delete_event_location (location);
10193
10194 /* If non-NULL, ARG should have been advanced past the location;
10195 the next character must be ','. */
10196 if (arg != NULL)
10197 {
10198 if (arg[0] != ',' || arg[1] == '\0')
10199 error (_("Format string required"));
10200 else
10201 {
10202 /* Skip the comma. */
10203 ++arg;
10204 }
10205 }
10206
e7e0cddf 10207 create_breakpoint (get_current_arch (),
f00aae0f
KS
10208 location,
10209 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10210 0, bp_dprintf,
10211 0 /* Ignore count */,
10212 pending_break_support,
10213 &dprintf_breakpoint_ops,
10214 from_tty,
10215 1 /* enabled */,
10216 0 /* internal */,
10217 0);
f00aae0f 10218 do_cleanups (cleanup);
e7e0cddf
SS
10219}
10220
d3ce09f5
SS
10221static void
10222agent_printf_command (char *arg, int from_tty)
10223{
10224 error (_("May only run agent-printf on the target"));
10225}
10226
f1310107
TJB
10227/* Implement the "breakpoint_hit" breakpoint_ops method for
10228 ranged breakpoints. */
10229
10230static int
10231breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10232 struct address_space *aspace,
09ac7c10
TT
10233 CORE_ADDR bp_addr,
10234 const struct target_waitstatus *ws)
f1310107 10235{
09ac7c10 10236 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10237 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10238 return 0;
10239
f1310107
TJB
10240 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10241 bl->length, aspace, bp_addr);
10242}
10243
10244/* Implement the "resources_needed" breakpoint_ops method for
10245 ranged breakpoints. */
10246
10247static int
10248resources_needed_ranged_breakpoint (const struct bp_location *bl)
10249{
10250 return target_ranged_break_num_registers ();
10251}
10252
10253/* Implement the "print_it" breakpoint_ops method for
10254 ranged breakpoints. */
10255
10256static enum print_stop_action
348d480f 10257print_it_ranged_breakpoint (bpstat bs)
f1310107 10258{
348d480f 10259 struct breakpoint *b = bs->breakpoint_at;
f1310107 10260 struct bp_location *bl = b->loc;
79a45e25 10261 struct ui_out *uiout = current_uiout;
f1310107
TJB
10262
10263 gdb_assert (b->type == bp_hardware_breakpoint);
10264
10265 /* Ranged breakpoints have only one location. */
10266 gdb_assert (bl && bl->next == NULL);
10267
10268 annotate_breakpoint (b->number);
f303dbd6
PA
10269
10270 maybe_print_thread_hit_breakpoint (uiout);
10271
f1310107 10272 if (b->disposition == disp_del)
f303dbd6 10273 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10274 else
f303dbd6 10275 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10276 if (ui_out_is_mi_like_p (uiout))
10277 {
10278 ui_out_field_string (uiout, "reason",
10279 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10280 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10281 }
10282 ui_out_field_int (uiout, "bkptno", b->number);
10283 ui_out_text (uiout, ", ");
10284
10285 return PRINT_SRC_AND_LOC;
10286}
10287
10288/* Implement the "print_one" breakpoint_ops method for
10289 ranged breakpoints. */
10290
10291static void
10292print_one_ranged_breakpoint (struct breakpoint *b,
10293 struct bp_location **last_loc)
10294{
10295 struct bp_location *bl = b->loc;
10296 struct value_print_options opts;
79a45e25 10297 struct ui_out *uiout = current_uiout;
f1310107
TJB
10298
10299 /* Ranged breakpoints have only one location. */
10300 gdb_assert (bl && bl->next == NULL);
10301
10302 get_user_print_options (&opts);
10303
10304 if (opts.addressprint)
10305 /* We don't print the address range here, it will be printed later
10306 by print_one_detail_ranged_breakpoint. */
10307 ui_out_field_skip (uiout, "addr");
10308 annotate_field (5);
10309 print_breakpoint_location (b, bl);
10310 *last_loc = bl;
10311}
10312
10313/* Implement the "print_one_detail" breakpoint_ops method for
10314 ranged breakpoints. */
10315
10316static void
10317print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10318 struct ui_out *uiout)
10319{
10320 CORE_ADDR address_start, address_end;
10321 struct bp_location *bl = b->loc;
f99d8bf4
PA
10322 struct ui_file *stb = mem_fileopen ();
10323 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10324
10325 gdb_assert (bl);
10326
10327 address_start = bl->address;
10328 address_end = address_start + bl->length - 1;
10329
10330 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10331 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10332 print_core_address (bl->gdbarch, address_start),
10333 print_core_address (bl->gdbarch, address_end));
10334 ui_out_field_stream (uiout, "addr", stb);
10335 ui_out_text (uiout, "\n");
10336
10337 do_cleanups (cleanup);
10338}
10339
10340/* Implement the "print_mention" breakpoint_ops method for
10341 ranged breakpoints. */
10342
10343static void
10344print_mention_ranged_breakpoint (struct breakpoint *b)
10345{
10346 struct bp_location *bl = b->loc;
79a45e25 10347 struct ui_out *uiout = current_uiout;
f1310107
TJB
10348
10349 gdb_assert (bl);
10350 gdb_assert (b->type == bp_hardware_breakpoint);
10351
10352 if (ui_out_is_mi_like_p (uiout))
10353 return;
10354
10355 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10356 b->number, paddress (bl->gdbarch, bl->address),
10357 paddress (bl->gdbarch, bl->address + bl->length - 1));
10358}
10359
10360/* Implement the "print_recreate" breakpoint_ops method for
10361 ranged breakpoints. */
10362
10363static void
10364print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10365{
f00aae0f
KS
10366 fprintf_unfiltered (fp, "break-range %s, %s",
10367 event_location_to_string (b->location),
10368 event_location_to_string (b->location_range_end));
d9b3f62e 10369 print_recreate_thread (b, fp);
f1310107
TJB
10370}
10371
10372/* The breakpoint_ops structure to be used in ranged breakpoints. */
10373
2060206e 10374static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10375
10376/* Find the address where the end of the breakpoint range should be
10377 placed, given the SAL of the end of the range. This is so that if
10378 the user provides a line number, the end of the range is set to the
10379 last instruction of the given line. */
10380
10381static CORE_ADDR
10382find_breakpoint_range_end (struct symtab_and_line sal)
10383{
10384 CORE_ADDR end;
10385
10386 /* If the user provided a PC value, use it. Otherwise,
10387 find the address of the end of the given location. */
10388 if (sal.explicit_pc)
10389 end = sal.pc;
10390 else
10391 {
10392 int ret;
10393 CORE_ADDR start;
10394
10395 ret = find_line_pc_range (sal, &start, &end);
10396 if (!ret)
10397 error (_("Could not find location of the end of the range."));
10398
10399 /* find_line_pc_range returns the start of the next line. */
10400 end--;
10401 }
10402
10403 return end;
10404}
10405
10406/* Implement the "break-range" CLI command. */
10407
10408static void
10409break_range_command (char *arg, int from_tty)
10410{
870f88f7 10411 char *arg_start, *addr_string_start;
f1310107
TJB
10412 struct linespec_result canonical_start, canonical_end;
10413 int bp_count, can_use_bp, length;
10414 CORE_ADDR end;
10415 struct breakpoint *b;
10416 struct symtab_and_line sal_start, sal_end;
f1310107 10417 struct cleanup *cleanup_bkpt;
f8eba3c6 10418 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10419 struct event_location *start_location, *end_location;
f1310107
TJB
10420
10421 /* We don't support software ranged breakpoints. */
10422 if (target_ranged_break_num_registers () < 0)
10423 error (_("This target does not support hardware ranged breakpoints."));
10424
10425 bp_count = hw_breakpoint_used_count ();
10426 bp_count += target_ranged_break_num_registers ();
10427 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10428 bp_count, 0);
10429 if (can_use_bp < 0)
10430 error (_("Hardware breakpoints used exceeds limit."));
10431
f8eba3c6 10432 arg = skip_spaces (arg);
f1310107
TJB
10433 if (arg == NULL || arg[0] == '\0')
10434 error(_("No address range specified."));
10435
f1310107
TJB
10436 init_linespec_result (&canonical_start);
10437
f8eba3c6 10438 arg_start = arg;
f00aae0f
KS
10439 start_location = string_to_event_location (&arg, current_language);
10440 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10441 parse_breakpoint_sals (start_location, &canonical_start);
10442 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10443
10444 if (arg[0] != ',')
10445 error (_("Too few arguments."));
f8eba3c6 10446 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10447 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10448
10449 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10450
10451 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10452 || lsal_start->sals.nelts != 1)
f1310107
TJB
10453 error (_("Cannot create a ranged breakpoint with multiple locations."));
10454
f8eba3c6
TT
10455 sal_start = lsal_start->sals.sals[0];
10456 addr_string_start = savestring (arg_start, arg - arg_start);
10457 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10458
10459 arg++; /* Skip the comma. */
f8eba3c6 10460 arg = skip_spaces (arg);
f1310107
TJB
10461
10462 /* Parse the end location. */
10463
f1310107
TJB
10464 init_linespec_result (&canonical_end);
10465 arg_start = arg;
10466
f8eba3c6 10467 /* We call decode_line_full directly here instead of using
f1310107
TJB
10468 parse_breakpoint_sals because we need to specify the start location's
10469 symtab and line as the default symtab and line for the end of the
10470 range. This makes it possible to have ranges like "foo.c:27, +14",
10471 where +14 means 14 lines from the start location. */
f00aae0f
KS
10472 end_location = string_to_event_location (&arg, current_language);
10473 make_cleanup_delete_event_location (end_location);
c2f4122d 10474 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10475 sal_start.symtab, sal_start.line,
10476 &canonical_end, NULL, NULL);
10477
10478 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10479
f8eba3c6 10480 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10481 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10482
10483 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10484 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10485 || lsal_end->sals.nelts != 1)
f1310107
TJB
10486 error (_("Cannot create a ranged breakpoint with multiple locations."));
10487
f8eba3c6 10488 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10489
10490 end = find_breakpoint_range_end (sal_end);
10491 if (sal_start.pc > end)
177b42fe 10492 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10493
10494 length = end - sal_start.pc + 1;
10495 if (length < 0)
10496 /* Length overflowed. */
10497 error (_("Address range too large."));
10498 else if (length == 1)
10499 {
10500 /* This range is simple enough to be handled by
10501 the `hbreak' command. */
10502 hbreak_command (addr_string_start, 1);
10503
10504 do_cleanups (cleanup_bkpt);
10505
10506 return;
10507 }
10508
10509 /* Now set up the breakpoint. */
10510 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10511 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10512 set_breakpoint_count (breakpoint_count + 1);
10513 b->number = breakpoint_count;
10514 b->disposition = disp_donttouch;
f00aae0f
KS
10515 b->location = copy_event_location (start_location);
10516 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10517 b->loc->length = length;
10518
f8eba3c6 10519 do_cleanups (cleanup_bkpt);
f1310107
TJB
10520
10521 mention (b);
8d3788bd 10522 observer_notify_breakpoint_created (b);
44702360 10523 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10524}
10525
4a64f543
MS
10526/* Return non-zero if EXP is verified as constant. Returned zero
10527 means EXP is variable. Also the constant detection may fail for
10528 some constant expressions and in such case still falsely return
10529 zero. */
2e6e3d9c 10530
65d79d4b
SDJ
10531static int
10532watchpoint_exp_is_const (const struct expression *exp)
10533{
10534 int i = exp->nelts;
10535
10536 while (i > 0)
10537 {
10538 int oplenp, argsp;
10539
10540 /* We are only interested in the descriptor of each element. */
10541 operator_length (exp, i, &oplenp, &argsp);
10542 i -= oplenp;
10543
10544 switch (exp->elts[i].opcode)
10545 {
10546 case BINOP_ADD:
10547 case BINOP_SUB:
10548 case BINOP_MUL:
10549 case BINOP_DIV:
10550 case BINOP_REM:
10551 case BINOP_MOD:
10552 case BINOP_LSH:
10553 case BINOP_RSH:
10554 case BINOP_LOGICAL_AND:
10555 case BINOP_LOGICAL_OR:
10556 case BINOP_BITWISE_AND:
10557 case BINOP_BITWISE_IOR:
10558 case BINOP_BITWISE_XOR:
10559 case BINOP_EQUAL:
10560 case BINOP_NOTEQUAL:
10561 case BINOP_LESS:
10562 case BINOP_GTR:
10563 case BINOP_LEQ:
10564 case BINOP_GEQ:
10565 case BINOP_REPEAT:
10566 case BINOP_COMMA:
10567 case BINOP_EXP:
10568 case BINOP_MIN:
10569 case BINOP_MAX:
10570 case BINOP_INTDIV:
10571 case BINOP_CONCAT:
65d79d4b
SDJ
10572 case TERNOP_COND:
10573 case TERNOP_SLICE:
65d79d4b
SDJ
10574
10575 case OP_LONG:
10576 case OP_DOUBLE:
10577 case OP_DECFLOAT:
10578 case OP_LAST:
10579 case OP_COMPLEX:
10580 case OP_STRING:
65d79d4b
SDJ
10581 case OP_ARRAY:
10582 case OP_TYPE:
608b4967
TT
10583 case OP_TYPEOF:
10584 case OP_DECLTYPE:
6e72ca20 10585 case OP_TYPEID:
65d79d4b
SDJ
10586 case OP_NAME:
10587 case OP_OBJC_NSSTRING:
10588
10589 case UNOP_NEG:
10590 case UNOP_LOGICAL_NOT:
10591 case UNOP_COMPLEMENT:
10592 case UNOP_ADDR:
10593 case UNOP_HIGH:
aeaa2474 10594 case UNOP_CAST:
9eaf6705
TT
10595
10596 case UNOP_CAST_TYPE:
10597 case UNOP_REINTERPRET_CAST:
10598 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10599 /* Unary, binary and ternary operators: We have to check
10600 their operands. If they are constant, then so is the
10601 result of that operation. For instance, if A and B are
10602 determined to be constants, then so is "A + B".
10603
10604 UNOP_IND is one exception to the rule above, because the
10605 value of *ADDR is not necessarily a constant, even when
10606 ADDR is. */
65d79d4b
SDJ
10607 break;
10608
10609 case OP_VAR_VALUE:
10610 /* Check whether the associated symbol is a constant.
4a64f543 10611
65d79d4b 10612 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10613 possible that a buggy compiler could mark a variable as
10614 constant even when it is not, and TYPE_CONST would return
10615 true in this case, while SYMBOL_CLASS wouldn't.
10616
10617 We also have to check for function symbols because they
10618 are always constant. */
65d79d4b
SDJ
10619 {
10620 struct symbol *s = exp->elts[i + 2].symbol;
10621
10622 if (SYMBOL_CLASS (s) != LOC_BLOCK
10623 && SYMBOL_CLASS (s) != LOC_CONST
10624 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10625 return 0;
10626 break;
10627 }
10628
10629 /* The default action is to return 0 because we are using
10630 the optimistic approach here: If we don't know something,
10631 then it is not a constant. */
10632 default:
10633 return 0;
10634 }
10635 }
10636
10637 return 1;
10638}
10639
3a5c3e22
PA
10640/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10641
10642static void
10643dtor_watchpoint (struct breakpoint *self)
10644{
10645 struct watchpoint *w = (struct watchpoint *) self;
10646
3a5c3e22
PA
10647 xfree (w->exp_string);
10648 xfree (w->exp_string_reparse);
10649 value_free (w->val);
10650
10651 base_breakpoint_ops.dtor (self);
10652}
10653
348d480f
PA
10654/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10655
10656static void
10657re_set_watchpoint (struct breakpoint *b)
10658{
3a5c3e22
PA
10659 struct watchpoint *w = (struct watchpoint *) b;
10660
348d480f
PA
10661 /* Watchpoint can be either on expression using entirely global
10662 variables, or it can be on local variables.
10663
10664 Watchpoints of the first kind are never auto-deleted, and even
10665 persist across program restarts. Since they can use variables
10666 from shared libraries, we need to reparse expression as libraries
10667 are loaded and unloaded.
10668
10669 Watchpoints on local variables can also change meaning as result
10670 of solib event. For example, if a watchpoint uses both a local
10671 and a global variables in expression, it's a local watchpoint,
10672 but unloading of a shared library will make the expression
10673 invalid. This is not a very common use case, but we still
10674 re-evaluate expression, to avoid surprises to the user.
10675
10676 Note that for local watchpoints, we re-evaluate it only if
10677 watchpoints frame id is still valid. If it's not, it means the
10678 watchpoint is out of scope and will be deleted soon. In fact,
10679 I'm not sure we'll ever be called in this case.
10680
10681 If a local watchpoint's frame id is still valid, then
3a5c3e22 10682 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10683
3a5c3e22
PA
10684 Don't do anything about disabled watchpoints, since they will be
10685 reevaluated again when enabled. */
10686 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10687}
10688
77b06cd7
TJB
10689/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10690
10691static int
10692insert_watchpoint (struct bp_location *bl)
10693{
3a5c3e22
PA
10694 struct watchpoint *w = (struct watchpoint *) bl->owner;
10695 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10696
10697 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10698 w->cond_exp.get ());
77b06cd7
TJB
10699}
10700
10701/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10702
10703static int
73971819 10704remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10705{
3a5c3e22
PA
10706 struct watchpoint *w = (struct watchpoint *) bl->owner;
10707 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10708
10709 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10710 w->cond_exp.get ());
e09342b5
TJB
10711}
10712
e09342b5 10713static int
348d480f 10714breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10715 struct address_space *aspace, CORE_ADDR bp_addr,
10716 const struct target_waitstatus *ws)
e09342b5 10717{
348d480f 10718 struct breakpoint *b = bl->owner;
3a5c3e22 10719 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10720
348d480f
PA
10721 /* Continuable hardware watchpoints are treated as non-existent if the
10722 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10723 some data address). Otherwise gdb won't stop on a break instruction
10724 in the code (not from a breakpoint) when a hardware watchpoint has
10725 been defined. Also skip watchpoints which we know did not trigger
10726 (did not match the data address). */
10727 if (is_hardware_watchpoint (b)
3a5c3e22 10728 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10729 return 0;
9c06b0b4 10730
348d480f 10731 return 1;
9c06b0b4
TJB
10732}
10733
348d480f
PA
10734static void
10735check_status_watchpoint (bpstat bs)
9c06b0b4 10736{
348d480f 10737 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10738
348d480f 10739 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10740}
10741
10742/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10743 hardware watchpoints. */
9c06b0b4
TJB
10744
10745static int
348d480f 10746resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10747{
3a5c3e22
PA
10748 struct watchpoint *w = (struct watchpoint *) bl->owner;
10749 int length = w->exact? 1 : bl->length;
348d480f
PA
10750
10751 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10752}
10753
10754/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10755 hardware watchpoints. */
9c06b0b4
TJB
10756
10757static int
348d480f 10758works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10759{
efa80663
PA
10760 /* Read and access watchpoints only work with hardware support. */
10761 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10762}
10763
9c06b0b4 10764static enum print_stop_action
348d480f 10765print_it_watchpoint (bpstat bs)
9c06b0b4 10766{
348d480f
PA
10767 struct cleanup *old_chain;
10768 struct breakpoint *b;
f99d8bf4 10769 struct ui_file *stb;
348d480f 10770 enum print_stop_action result;
3a5c3e22 10771 struct watchpoint *w;
79a45e25 10772 struct ui_out *uiout = current_uiout;
348d480f
PA
10773
10774 gdb_assert (bs->bp_location_at != NULL);
10775
348d480f 10776 b = bs->breakpoint_at;
3a5c3e22 10777 w = (struct watchpoint *) b;
348d480f 10778
f99d8bf4
PA
10779 stb = mem_fileopen ();
10780 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10781
f303dbd6
PA
10782 annotate_watchpoint (b->number);
10783 maybe_print_thread_hit_breakpoint (uiout);
10784
9c06b0b4
TJB
10785 switch (b->type)
10786 {
348d480f 10787 case bp_watchpoint:
9c06b0b4 10788 case bp_hardware_watchpoint:
9c06b0b4
TJB
10789 if (ui_out_is_mi_like_p (uiout))
10790 ui_out_field_string
10791 (uiout, "reason",
10792 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10793 mention (b);
10794 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10795 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10796 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10797 ui_out_field_stream (uiout, "old", stb);
10798 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10799 watchpoint_value_print (w->val, stb);
348d480f
PA
10800 ui_out_field_stream (uiout, "new", stb);
10801 ui_out_text (uiout, "\n");
10802 /* More than one watchpoint may have been triggered. */
10803 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10804 break;
10805
10806 case bp_read_watchpoint:
10807 if (ui_out_is_mi_like_p (uiout))
10808 ui_out_field_string
10809 (uiout, "reason",
10810 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10811 mention (b);
10812 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10813 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10814 watchpoint_value_print (w->val, stb);
348d480f
PA
10815 ui_out_field_stream (uiout, "value", stb);
10816 ui_out_text (uiout, "\n");
10817 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10818 break;
10819
10820 case bp_access_watchpoint:
348d480f
PA
10821 if (bs->old_val != NULL)
10822 {
348d480f
PA
10823 if (ui_out_is_mi_like_p (uiout))
10824 ui_out_field_string
10825 (uiout, "reason",
10826 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10827 mention (b);
10828 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10829 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10830 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10831 ui_out_field_stream (uiout, "old", stb);
10832 ui_out_text (uiout, "\nNew value = ");
10833 }
10834 else
10835 {
10836 mention (b);
10837 if (ui_out_is_mi_like_p (uiout))
10838 ui_out_field_string
10839 (uiout, "reason",
10840 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10841 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10842 ui_out_text (uiout, "\nValue = ");
10843 }
f99d8bf4 10844 watchpoint_value_print (w->val, stb);
348d480f
PA
10845 ui_out_field_stream (uiout, "new", stb);
10846 ui_out_text (uiout, "\n");
10847 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10848 break;
10849 default:
348d480f 10850 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10851 }
10852
348d480f
PA
10853 do_cleanups (old_chain);
10854 return result;
10855}
10856
10857/* Implement the "print_mention" breakpoint_ops method for hardware
10858 watchpoints. */
10859
10860static void
10861print_mention_watchpoint (struct breakpoint *b)
10862{
10863 struct cleanup *ui_out_chain;
3a5c3e22 10864 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10865 struct ui_out *uiout = current_uiout;
348d480f
PA
10866
10867 switch (b->type)
10868 {
10869 case bp_watchpoint:
10870 ui_out_text (uiout, "Watchpoint ");
10871 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10872 break;
10873 case bp_hardware_watchpoint:
10874 ui_out_text (uiout, "Hardware watchpoint ");
10875 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10876 break;
10877 case bp_read_watchpoint:
10878 ui_out_text (uiout, "Hardware read watchpoint ");
10879 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10880 break;
10881 case bp_access_watchpoint:
10882 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10884 break;
10885 default:
10886 internal_error (__FILE__, __LINE__,
10887 _("Invalid hardware watchpoint type."));
10888 }
10889
10890 ui_out_field_int (uiout, "number", b->number);
10891 ui_out_text (uiout, ": ");
3a5c3e22 10892 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10893 do_cleanups (ui_out_chain);
10894}
10895
10896/* Implement the "print_recreate" breakpoint_ops method for
10897 watchpoints. */
10898
10899static void
10900print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10901{
3a5c3e22
PA
10902 struct watchpoint *w = (struct watchpoint *) b;
10903
348d480f
PA
10904 switch (b->type)
10905 {
10906 case bp_watchpoint:
10907 case bp_hardware_watchpoint:
10908 fprintf_unfiltered (fp, "watch");
10909 break;
10910 case bp_read_watchpoint:
10911 fprintf_unfiltered (fp, "rwatch");
10912 break;
10913 case bp_access_watchpoint:
10914 fprintf_unfiltered (fp, "awatch");
10915 break;
10916 default:
10917 internal_error (__FILE__, __LINE__,
10918 _("Invalid watchpoint type."));
10919 }
10920
3a5c3e22 10921 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10922 print_recreate_thread (b, fp);
348d480f
PA
10923}
10924
427cd150
TT
10925/* Implement the "explains_signal" breakpoint_ops method for
10926 watchpoints. */
10927
47591c29 10928static int
427cd150
TT
10929explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10930{
10931 /* A software watchpoint cannot cause a signal other than
10932 GDB_SIGNAL_TRAP. */
10933 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10934 return 0;
427cd150 10935
47591c29 10936 return 1;
427cd150
TT
10937}
10938
348d480f
PA
10939/* The breakpoint_ops structure to be used in hardware watchpoints. */
10940
2060206e 10941static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10942
10943/* Implement the "insert" breakpoint_ops method for
10944 masked hardware watchpoints. */
10945
10946static int
10947insert_masked_watchpoint (struct bp_location *bl)
10948{
3a5c3e22
PA
10949 struct watchpoint *w = (struct watchpoint *) bl->owner;
10950
10951 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10952 bl->watchpoint_type);
10953}
10954
10955/* Implement the "remove" breakpoint_ops method for
10956 masked hardware watchpoints. */
10957
10958static int
73971819 10959remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10960{
3a5c3e22
PA
10961 struct watchpoint *w = (struct watchpoint *) bl->owner;
10962
10963 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10964 bl->watchpoint_type);
10965}
10966
10967/* Implement the "resources_needed" breakpoint_ops method for
10968 masked hardware watchpoints. */
10969
10970static int
10971resources_needed_masked_watchpoint (const struct bp_location *bl)
10972{
3a5c3e22
PA
10973 struct watchpoint *w = (struct watchpoint *) bl->owner;
10974
10975 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10976}
10977
10978/* Implement the "works_in_software_mode" breakpoint_ops method for
10979 masked hardware watchpoints. */
10980
10981static int
10982works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10983{
10984 return 0;
10985}
10986
10987/* Implement the "print_it" breakpoint_ops method for
10988 masked hardware watchpoints. */
10989
10990static enum print_stop_action
10991print_it_masked_watchpoint (bpstat bs)
10992{
10993 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10994 struct ui_out *uiout = current_uiout;
348d480f
PA
10995
10996 /* Masked watchpoints have only one location. */
10997 gdb_assert (b->loc && b->loc->next == NULL);
10998
f303dbd6
PA
10999 annotate_watchpoint (b->number);
11000 maybe_print_thread_hit_breakpoint (uiout);
11001
348d480f
PA
11002 switch (b->type)
11003 {
11004 case bp_hardware_watchpoint:
348d480f
PA
11005 if (ui_out_is_mi_like_p (uiout))
11006 ui_out_field_string
11007 (uiout, "reason",
11008 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11009 break;
11010
11011 case bp_read_watchpoint:
11012 if (ui_out_is_mi_like_p (uiout))
11013 ui_out_field_string
11014 (uiout, "reason",
11015 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11016 break;
11017
11018 case bp_access_watchpoint:
11019 if (ui_out_is_mi_like_p (uiout))
11020 ui_out_field_string
11021 (uiout, "reason",
11022 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11023 break;
11024 default:
11025 internal_error (__FILE__, __LINE__,
11026 _("Invalid hardware watchpoint type."));
11027 }
11028
11029 mention (b);
9c06b0b4
TJB
11030 ui_out_text (uiout, _("\n\
11031Check the underlying instruction at PC for the memory\n\
11032address and value which triggered this watchpoint.\n"));
11033 ui_out_text (uiout, "\n");
11034
11035 /* More than one watchpoint may have been triggered. */
11036 return PRINT_UNKNOWN;
11037}
11038
11039/* Implement the "print_one_detail" breakpoint_ops method for
11040 masked hardware watchpoints. */
11041
11042static void
11043print_one_detail_masked_watchpoint (const struct breakpoint *b,
11044 struct ui_out *uiout)
11045{
3a5c3e22
PA
11046 struct watchpoint *w = (struct watchpoint *) b;
11047
9c06b0b4
TJB
11048 /* Masked watchpoints have only one location. */
11049 gdb_assert (b->loc && b->loc->next == NULL);
11050
11051 ui_out_text (uiout, "\tmask ");
3a5c3e22 11052 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11053 ui_out_text (uiout, "\n");
11054}
11055
11056/* Implement the "print_mention" breakpoint_ops method for
11057 masked hardware watchpoints. */
11058
11059static void
11060print_mention_masked_watchpoint (struct breakpoint *b)
11061{
3a5c3e22 11062 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11063 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11064 struct cleanup *ui_out_chain;
11065
11066 switch (b->type)
11067 {
11068 case bp_hardware_watchpoint:
11069 ui_out_text (uiout, "Masked hardware watchpoint ");
11070 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11071 break;
11072 case bp_read_watchpoint:
11073 ui_out_text (uiout, "Masked hardware read watchpoint ");
11074 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11075 break;
11076 case bp_access_watchpoint:
11077 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11078 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11079 break;
11080 default:
11081 internal_error (__FILE__, __LINE__,
11082 _("Invalid hardware watchpoint type."));
11083 }
11084
11085 ui_out_field_int (uiout, "number", b->number);
11086 ui_out_text (uiout, ": ");
3a5c3e22 11087 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11088 do_cleanups (ui_out_chain);
11089}
11090
11091/* Implement the "print_recreate" breakpoint_ops method for
11092 masked hardware watchpoints. */
11093
11094static void
11095print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11096{
3a5c3e22 11097 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11098 char tmp[40];
11099
11100 switch (b->type)
11101 {
11102 case bp_hardware_watchpoint:
11103 fprintf_unfiltered (fp, "watch");
11104 break;
11105 case bp_read_watchpoint:
11106 fprintf_unfiltered (fp, "rwatch");
11107 break;
11108 case bp_access_watchpoint:
11109 fprintf_unfiltered (fp, "awatch");
11110 break;
11111 default:
11112 internal_error (__FILE__, __LINE__,
11113 _("Invalid hardware watchpoint type."));
11114 }
11115
3a5c3e22
PA
11116 sprintf_vma (tmp, w->hw_wp_mask);
11117 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11118 print_recreate_thread (b, fp);
9c06b0b4
TJB
11119}
11120
11121/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11122
2060206e 11123static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11124
11125/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11126
11127static int
11128is_masked_watchpoint (const struct breakpoint *b)
11129{
11130 return b->ops == &masked_watchpoint_breakpoint_ops;
11131}
11132
53a5351d
JM
11133/* accessflag: hw_write: watch write,
11134 hw_read: watch read,
11135 hw_access: watch access (read or write) */
c906108c 11136static void
bbc13ae3 11137watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11138 int just_location, int internal)
c906108c 11139{
d983da9c 11140 struct breakpoint *b, *scope_breakpoint = NULL;
270140bd 11141 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11142 struct value *val, *mark, *result;
bb9d5f81 11143 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11144 struct frame_info *frame;
bbc13ae3
KS
11145 const char *exp_start = NULL;
11146 const char *exp_end = NULL;
11147 const char *tok, *end_tok;
9c06b0b4 11148 int toklen = -1;
bbc13ae3
KS
11149 const char *cond_start = NULL;
11150 const char *cond_end = NULL;
c906108c 11151 enum bptype bp_type;
37e4754d 11152 int thread = -1;
0cf6dd15 11153 int pc = 0;
9c06b0b4
TJB
11154 /* Flag to indicate whether we are going to use masks for
11155 the hardware watchpoint. */
11156 int use_mask = 0;
11157 CORE_ADDR mask = 0;
3a5c3e22 11158 struct watchpoint *w;
bbc13ae3
KS
11159 char *expression;
11160 struct cleanup *back_to;
c906108c 11161
37e4754d
LM
11162 /* Make sure that we actually have parameters to parse. */
11163 if (arg != NULL && arg[0] != '\0')
11164 {
bbc13ae3
KS
11165 const char *value_start;
11166
11167 exp_end = arg + strlen (arg);
37e4754d 11168
9c06b0b4
TJB
11169 /* Look for "parameter value" pairs at the end
11170 of the arguments string. */
bbc13ae3 11171 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11172 {
11173 /* Skip whitespace at the end of the argument list. */
11174 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11175 tok--;
11176
11177 /* Find the beginning of the last token.
11178 This is the value of the parameter. */
11179 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11180 tok--;
11181 value_start = tok + 1;
11182
11183 /* Skip whitespace. */
11184 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11185 tok--;
11186
11187 end_tok = tok;
11188
11189 /* Find the beginning of the second to last token.
11190 This is the parameter itself. */
11191 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11192 tok--;
11193 tok++;
11194 toklen = end_tok - tok + 1;
11195
61012eef 11196 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11197 {
5d5658a1 11198 struct thread_info *thr;
9c06b0b4
TJB
11199 /* At this point we've found a "thread" token, which means
11200 the user is trying to set a watchpoint that triggers
11201 only in a specific thread. */
5d5658a1 11202 const char *endp;
37e4754d 11203
9c06b0b4
TJB
11204 if (thread != -1)
11205 error(_("You can specify only one thread."));
37e4754d 11206
9c06b0b4 11207 /* Extract the thread ID from the next token. */
5d5658a1 11208 thr = parse_thread_id (value_start, &endp);
37e4754d 11209
5d5658a1 11210 /* Check if the user provided a valid thread ID. */
9c06b0b4 11211 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11212 invalid_thread_id_error (value_start);
9c06b0b4 11213
5d5658a1 11214 thread = thr->global_num;
9c06b0b4 11215 }
61012eef 11216 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11217 {
11218 /* We've found a "mask" token, which means the user wants to
11219 create a hardware watchpoint that is going to have the mask
11220 facility. */
11221 struct value *mask_value, *mark;
37e4754d 11222
9c06b0b4
TJB
11223 if (use_mask)
11224 error(_("You can specify only one mask."));
37e4754d 11225
9c06b0b4 11226 use_mask = just_location = 1;
37e4754d 11227
9c06b0b4
TJB
11228 mark = value_mark ();
11229 mask_value = parse_to_comma_and_eval (&value_start);
11230 mask = value_as_address (mask_value);
11231 value_free_to_mark (mark);
11232 }
11233 else
11234 /* We didn't recognize what we found. We should stop here. */
11235 break;
37e4754d 11236
9c06b0b4
TJB
11237 /* Truncate the string and get rid of the "parameter value" pair before
11238 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11239 exp_end = tok;
9c06b0b4 11240 }
37e4754d 11241 }
bbc13ae3
KS
11242 else
11243 exp_end = arg;
37e4754d 11244
bbc13ae3
KS
11245 /* Parse the rest of the arguments. From here on out, everything
11246 is in terms of a newly allocated string instead of the original
11247 ARG. */
c906108c 11248 innermost_block = NULL;
bbc13ae3
KS
11249 expression = savestring (arg, exp_end - arg);
11250 back_to = make_cleanup (xfree, expression);
11251 exp_start = arg = expression;
4d01a485 11252 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11253 exp_end = arg;
fa8a61dc
TT
11254 /* Remove trailing whitespace from the expression before saving it.
11255 This makes the eventual display of the expression string a bit
11256 prettier. */
11257 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11258 --exp_end;
11259
65d79d4b 11260 /* Checking if the expression is not constant. */
4d01a485 11261 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11262 {
11263 int len;
11264
11265 len = exp_end - exp_start;
11266 while (len > 0 && isspace (exp_start[len - 1]))
11267 len--;
11268 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11269 }
11270
c906108c
SS
11271 exp_valid_block = innermost_block;
11272 mark = value_mark ();
4d01a485 11273 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11274
bb9d5f81
PP
11275 if (val != NULL && just_location)
11276 {
11277 saved_bitpos = value_bitpos (val);
11278 saved_bitsize = value_bitsize (val);
11279 }
11280
06a64a0b
TT
11281 if (just_location)
11282 {
9c06b0b4
TJB
11283 int ret;
11284
06a64a0b 11285 exp_valid_block = NULL;
a1442452 11286 val = value_addr (result);
06a64a0b
TT
11287 release_value (val);
11288 value_free_to_mark (mark);
9c06b0b4
TJB
11289
11290 if (use_mask)
11291 {
11292 ret = target_masked_watch_num_registers (value_as_address (val),
11293 mask);
11294 if (ret == -1)
11295 error (_("This target does not support masked watchpoints."));
11296 else if (ret == -2)
11297 error (_("Invalid mask or memory region."));
11298 }
06a64a0b
TT
11299 }
11300 else if (val != NULL)
fa4727a6 11301 release_value (val);
c906108c 11302
bbc13ae3
KS
11303 tok = skip_spaces_const (arg);
11304 end_tok = skip_to_space_const (tok);
c906108c
SS
11305
11306 toklen = end_tok - tok;
11307 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11308 {
60e1c644 11309 innermost_block = NULL;
c906108c 11310 tok = cond_start = end_tok + 1;
4d01a485 11311 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11312
11313 /* The watchpoint expression may not be local, but the condition
11314 may still be. E.g.: `watch global if local > 0'. */
11315 cond_exp_valid_block = innermost_block;
11316
c906108c
SS
11317 cond_end = tok;
11318 }
11319 if (*tok)
8a3fe4f8 11320 error (_("Junk at end of command."));
c906108c 11321
d983da9c 11322 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11323
11324 /* If the expression is "local", then set up a "watchpoint scope"
11325 breakpoint at the point where we've left the scope of the watchpoint
11326 expression. Create the scope breakpoint before the watchpoint, so
11327 that we will encounter it first in bpstat_stop_status. */
60e1c644 11328 if (exp_valid_block && frame)
d983da9c 11329 {
edb3359d
DJ
11330 if (frame_id_p (frame_unwind_caller_id (frame)))
11331 {
11332 scope_breakpoint
a6d9a66e
UW
11333 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11334 frame_unwind_caller_pc (frame),
06edf0c0
PA
11335 bp_watchpoint_scope,
11336 &momentary_breakpoint_ops);
d983da9c 11337
edb3359d 11338 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11339
edb3359d
DJ
11340 /* Automatically delete the breakpoint when it hits. */
11341 scope_breakpoint->disposition = disp_del;
d983da9c 11342
edb3359d
DJ
11343 /* Only break in the proper frame (help with recursion). */
11344 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11345
edb3359d 11346 /* Set the address at which we will stop. */
a6d9a66e
UW
11347 scope_breakpoint->loc->gdbarch
11348 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11349 scope_breakpoint->loc->requested_address
11350 = frame_unwind_caller_pc (frame);
11351 scope_breakpoint->loc->address
a6d9a66e
UW
11352 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11353 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11354 scope_breakpoint->type);
11355 }
d983da9c
DJ
11356 }
11357
e8369a73
AB
11358 /* Now set up the breakpoint. We create all watchpoints as hardware
11359 watchpoints here even if hardware watchpoints are turned off, a call
11360 to update_watchpoint later in this function will cause the type to
11361 drop back to bp_watchpoint (software watchpoint) if required. */
11362
11363 if (accessflag == hw_read)
11364 bp_type = bp_read_watchpoint;
11365 else if (accessflag == hw_access)
11366 bp_type = bp_access_watchpoint;
11367 else
11368 bp_type = bp_hardware_watchpoint;
3a5c3e22 11369
4d01a485 11370 w = new watchpoint ();
3a5c3e22 11371 b = &w->base;
348d480f 11372 if (use_mask)
3a5c3e22
PA
11373 init_raw_breakpoint_without_location (b, NULL, bp_type,
11374 &masked_watchpoint_breakpoint_ops);
348d480f 11375 else
3a5c3e22
PA
11376 init_raw_breakpoint_without_location (b, NULL, bp_type,
11377 &watchpoint_breakpoint_ops);
37e4754d 11378 b->thread = thread;
b5de0fa7 11379 b->disposition = disp_donttouch;
348d480f 11380 b->pspace = current_program_space;
b22e99fd 11381 w->exp = std::move (exp);
3a5c3e22
PA
11382 w->exp_valid_block = exp_valid_block;
11383 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11384 if (just_location)
11385 {
11386 struct type *t = value_type (val);
11387 CORE_ADDR addr = value_as_address (val);
06a64a0b
TT
11388
11389 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
06a64a0b 11390
2f408ecb
PA
11391 std::string name = type_to_string (t);
11392
11393 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
d63d0675 11394 core_addr_to_string (addr));
06a64a0b 11395
3a5c3e22 11396 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11397 (int) (exp_end - exp_start), exp_start);
11398
06a64a0b
TT
11399 /* The above expression is in C. */
11400 b->language = language_c;
11401 }
11402 else
3a5c3e22 11403 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11404
11405 if (use_mask)
11406 {
3a5c3e22 11407 w->hw_wp_mask = mask;
9c06b0b4
TJB
11408 }
11409 else
11410 {
3a5c3e22 11411 w->val = val;
bb9d5f81
PP
11412 w->val_bitpos = saved_bitpos;
11413 w->val_bitsize = saved_bitsize;
3a5c3e22 11414 w->val_valid = 1;
9c06b0b4 11415 }
77b06cd7 11416
c906108c
SS
11417 if (cond_start)
11418 b->cond_string = savestring (cond_start, cond_end - cond_start);
11419 else
11420 b->cond_string = 0;
c5aa993b 11421
c906108c 11422 if (frame)
f6bc2008 11423 {
3a5c3e22
PA
11424 w->watchpoint_frame = get_frame_id (frame);
11425 w->watchpoint_thread = inferior_ptid;
f6bc2008 11426 }
c906108c 11427 else
f6bc2008 11428 {
3a5c3e22
PA
11429 w->watchpoint_frame = null_frame_id;
11430 w->watchpoint_thread = null_ptid;
f6bc2008 11431 }
c906108c 11432
d983da9c 11433 if (scope_breakpoint != NULL)
c906108c 11434 {
d983da9c
DJ
11435 /* The scope breakpoint is related to the watchpoint. We will
11436 need to act on them together. */
11437 b->related_breakpoint = scope_breakpoint;
11438 scope_breakpoint->related_breakpoint = b;
c906108c 11439 }
d983da9c 11440
06a64a0b
TT
11441 if (!just_location)
11442 value_free_to_mark (mark);
2d134ed3 11443
492d29ea 11444 TRY
a9634178
TJB
11445 {
11446 /* Finally update the new watchpoint. This creates the locations
11447 that should be inserted. */
3a5c3e22 11448 update_watchpoint (w, 1);
a9634178 11449 }
492d29ea 11450 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11451 {
11452 delete_breakpoint (b);
11453 throw_exception (e);
11454 }
492d29ea 11455 END_CATCH
a9634178 11456
3ea46bff 11457 install_breakpoint (internal, b, 1);
bbc13ae3 11458 do_cleanups (back_to);
c906108c
SS
11459}
11460
e09342b5 11461/* Return count of debug registers needed to watch the given expression.
e09342b5 11462 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11463
c906108c 11464static int
a9634178 11465can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11466{
11467 int found_memory_cnt = 0;
2e70b7b9 11468 struct value *head = v;
c906108c
SS
11469
11470 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11471 if (!can_use_hw_watchpoints)
c906108c 11472 return 0;
c5aa993b 11473
5c44784c
JM
11474 /* Make sure that the value of the expression depends only upon
11475 memory contents, and values computed from them within GDB. If we
11476 find any register references or function calls, we can't use a
11477 hardware watchpoint.
11478
11479 The idea here is that evaluating an expression generates a series
11480 of values, one holding the value of every subexpression. (The
11481 expression a*b+c has five subexpressions: a, b, a*b, c, and
11482 a*b+c.) GDB's values hold almost enough information to establish
11483 the criteria given above --- they identify memory lvalues,
11484 register lvalues, computed values, etcetera. So we can evaluate
11485 the expression, and then scan the chain of values that leaves
11486 behind to decide whether we can detect any possible change to the
11487 expression's final value using only hardware watchpoints.
11488
11489 However, I don't think that the values returned by inferior
11490 function calls are special in any way. So this function may not
11491 notice that an expression involving an inferior function call
11492 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11493 for (; v; v = value_next (v))
c906108c 11494 {
5c44784c 11495 if (VALUE_LVAL (v) == lval_memory)
c906108c 11496 {
8464be76
DJ
11497 if (v != head && value_lazy (v))
11498 /* A lazy memory lvalue in the chain is one that GDB never
11499 needed to fetch; we either just used its address (e.g.,
11500 `a' in `a.b') or we never needed it at all (e.g., `a'
11501 in `a,b'). This doesn't apply to HEAD; if that is
11502 lazy then it was not readable, but watch it anyway. */
5c44784c 11503 ;
53a5351d 11504 else
5c44784c
JM
11505 {
11506 /* Ahh, memory we actually used! Check if we can cover
11507 it with hardware watchpoints. */
df407dfe 11508 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11509
11510 /* We only watch structs and arrays if user asked for it
11511 explicitly, never if they just happen to appear in a
11512 middle of some value chain. */
11513 if (v == head
11514 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11515 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11516 {
42ae5230 11517 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11518 int len;
11519 int num_regs;
11520
a9634178 11521 len = (target_exact_watchpoints
e09342b5
TJB
11522 && is_scalar_type_recursive (vtype))?
11523 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11524
e09342b5
TJB
11525 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11526 if (!num_regs)
2e70b7b9
MS
11527 return 0;
11528 else
e09342b5 11529 found_memory_cnt += num_regs;
2e70b7b9 11530 }
5c44784c 11531 }
c5aa993b 11532 }
5086187c
AC
11533 else if (VALUE_LVAL (v) != not_lval
11534 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11535 return 0; /* These are values from the history (e.g., $1). */
5086187c 11536 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11537 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11538 }
11539
11540 /* The expression itself looks suitable for using a hardware
11541 watchpoint, but give the target machine a chance to reject it. */
11542 return found_memory_cnt;
11543}
11544
8b93c638 11545void
84f4c1fe 11546watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11547{
84f4c1fe 11548 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11549}
11550
06a64a0b
TT
11551/* A helper function that looks for the "-location" argument and then
11552 calls watch_command_1. */
11553
11554static void
11555watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11556{
11557 int just_location = 0;
11558
11559 if (arg
11560 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11561 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11562 {
e9cafbcc 11563 arg = skip_spaces (arg);
06a64a0b
TT
11564 just_location = 1;
11565 }
11566
84f4c1fe 11567 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11568}
8926118c 11569
c5aa993b 11570static void
fba45db2 11571watch_command (char *arg, int from_tty)
c906108c 11572{
06a64a0b 11573 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11574}
11575
8b93c638 11576void
84f4c1fe 11577rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11578{
84f4c1fe 11579 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11580}
8926118c 11581
c5aa993b 11582static void
fba45db2 11583rwatch_command (char *arg, int from_tty)
c906108c 11584{
06a64a0b 11585 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11586}
11587
8b93c638 11588void
84f4c1fe 11589awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11590{
84f4c1fe 11591 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11592}
8926118c 11593
c5aa993b 11594static void
fba45db2 11595awatch_command (char *arg, int from_tty)
c906108c 11596{
06a64a0b 11597 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11598}
c906108c 11599\f
c5aa993b 11600
cfc31633
PA
11601/* Data for the FSM that manages the until(location)/advance commands
11602 in infcmd.c. Here because it uses the mechanisms of
11603 breakpoints. */
c906108c 11604
cfc31633 11605struct until_break_fsm
bfec99b2 11606{
cfc31633
PA
11607 /* The base class. */
11608 struct thread_fsm thread_fsm;
11609
11610 /* The thread that as current when the command was executed. */
11611 int thread;
11612
11613 /* The breakpoint set at the destination location. */
11614 struct breakpoint *location_breakpoint;
11615
11616 /* Breakpoint set at the return address in the caller frame. May be
11617 NULL. */
11618 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11619};
11620
8980e177
PA
11621static void until_break_fsm_clean_up (struct thread_fsm *self,
11622 struct thread_info *thread);
11623static int until_break_fsm_should_stop (struct thread_fsm *self,
11624 struct thread_info *thread);
cfc31633
PA
11625static enum async_reply_reason
11626 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11627
11628/* until_break_fsm's vtable. */
11629
11630static struct thread_fsm_ops until_break_fsm_ops =
11631{
11632 NULL, /* dtor */
11633 until_break_fsm_clean_up,
11634 until_break_fsm_should_stop,
11635 NULL, /* return_value */
11636 until_break_fsm_async_reply_reason,
11637};
11638
11639/* Allocate a new until_break_command_fsm. */
11640
11641static struct until_break_fsm *
8980e177 11642new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11643 struct breakpoint *location_breakpoint,
11644 struct breakpoint *caller_breakpoint)
11645{
11646 struct until_break_fsm *sm;
11647
11648 sm = XCNEW (struct until_break_fsm);
8980e177 11649 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11650
11651 sm->thread = thread;
11652 sm->location_breakpoint = location_breakpoint;
11653 sm->caller_breakpoint = caller_breakpoint;
11654
11655 return sm;
11656}
11657
11658/* Implementation of the 'should_stop' FSM method for the
11659 until(location)/advance commands. */
11660
11661static int
8980e177
PA
11662until_break_fsm_should_stop (struct thread_fsm *self,
11663 struct thread_info *tp)
cfc31633
PA
11664{
11665 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11666
11667 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11668 sm->location_breakpoint) != NULL
11669 || (sm->caller_breakpoint != NULL
11670 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11671 sm->caller_breakpoint) != NULL))
11672 thread_fsm_set_finished (self);
11673
11674 return 1;
11675}
11676
11677/* Implementation of the 'clean_up' FSM method for the
11678 until(location)/advance commands. */
11679
c2c6d25f 11680static void
8980e177
PA
11681until_break_fsm_clean_up (struct thread_fsm *self,
11682 struct thread_info *thread)
43ff13b4 11683{
cfc31633 11684 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11685
cfc31633
PA
11686 /* Clean up our temporary breakpoints. */
11687 if (sm->location_breakpoint != NULL)
11688 {
11689 delete_breakpoint (sm->location_breakpoint);
11690 sm->location_breakpoint = NULL;
11691 }
11692 if (sm->caller_breakpoint != NULL)
11693 {
11694 delete_breakpoint (sm->caller_breakpoint);
11695 sm->caller_breakpoint = NULL;
11696 }
11697 delete_longjmp_breakpoint (sm->thread);
11698}
11699
11700/* Implementation of the 'async_reply_reason' FSM method for the
11701 until(location)/advance commands. */
11702
11703static enum async_reply_reason
11704until_break_fsm_async_reply_reason (struct thread_fsm *self)
11705{
11706 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11707}
11708
c906108c 11709void
ae66c1fc 11710until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11711{
11712 struct symtabs_and_lines sals;
11713 struct symtab_and_line sal;
8556afb4
PA
11714 struct frame_info *frame;
11715 struct gdbarch *frame_gdbarch;
11716 struct frame_id stack_frame_id;
11717 struct frame_id caller_frame_id;
cfc31633
PA
11718 struct breakpoint *location_breakpoint;
11719 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11720 struct cleanup *old_chain, *cleanup;
186c406b
TT
11721 int thread;
11722 struct thread_info *tp;
f00aae0f 11723 struct event_location *location;
cfc31633 11724 struct until_break_fsm *sm;
c906108c 11725
70509625 11726 clear_proceed_status (0);
c906108c
SS
11727
11728 /* Set a breakpoint where the user wants it and at return from
4a64f543 11729 this function. */
c5aa993b 11730
f00aae0f
KS
11731 location = string_to_event_location (&arg, current_language);
11732 cleanup = make_cleanup_delete_event_location (location);
11733
1bfeeb0f 11734 if (last_displayed_sal_is_valid ())
c2f4122d 11735 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11736 get_last_displayed_symtab (),
f8eba3c6 11737 get_last_displayed_line ());
c906108c 11738 else
f00aae0f 11739 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11740 NULL, (struct symtab *) NULL, 0);
c5aa993b 11741
c906108c 11742 if (sals.nelts != 1)
8a3fe4f8 11743 error (_("Couldn't get information on specified line."));
c5aa993b 11744
c906108c 11745 sal = sals.sals[0];
4a64f543 11746 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11747
c906108c 11748 if (*arg)
8a3fe4f8 11749 error (_("Junk at end of arguments."));
c5aa993b 11750
c906108c 11751 resolve_sal_pc (&sal);
c5aa993b 11752
186c406b 11753 tp = inferior_thread ();
5d5658a1 11754 thread = tp->global_num;
186c406b 11755
883bc8d1
PA
11756 old_chain = make_cleanup (null_cleanup, NULL);
11757
8556afb4
PA
11758 /* Note linespec handling above invalidates the frame chain.
11759 Installing a breakpoint also invalidates the frame chain (as it
11760 may need to switch threads), so do any frame handling before
11761 that. */
11762
11763 frame = get_selected_frame (NULL);
11764 frame_gdbarch = get_frame_arch (frame);
11765 stack_frame_id = get_stack_frame_id (frame);
11766 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11767
ae66c1fc
EZ
11768 /* Keep within the current frame, or in frames called by the current
11769 one. */
edb3359d 11770
883bc8d1 11771 if (frame_id_p (caller_frame_id))
c906108c 11772 {
883bc8d1 11773 struct symtab_and_line sal2;
cfc31633 11774 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11775
11776 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11777 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11778 caller_gdbarch = frame_unwind_caller_arch (frame);
11779 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11780 sal2,
11781 caller_frame_id,
11782 bp_until);
11783 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11784
883bc8d1 11785 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11786 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11787 }
c5aa993b 11788
c70a6932
JK
11789 /* set_momentary_breakpoint could invalidate FRAME. */
11790 frame = NULL;
11791
883bc8d1
PA
11792 if (anywhere)
11793 /* If the user told us to continue until a specified location,
11794 we don't specify a frame at which we need to stop. */
cfc31633
PA
11795 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11796 null_frame_id, bp_until);
883bc8d1
PA
11797 else
11798 /* Otherwise, specify the selected frame, because we want to stop
11799 only at the very same frame. */
cfc31633
PA
11800 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11801 stack_frame_id, bp_until);
11802 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11803
8980e177 11804 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11805 location_breakpoint, caller_breakpoint);
cfc31633 11806 tp->thread_fsm = &sm->thread_fsm;
f107f563 11807
cfc31633 11808 discard_cleanups (old_chain);
f107f563 11809
cfc31633 11810 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11811
11812 do_cleanups (cleanup);
c906108c 11813}
ae66c1fc 11814
c906108c
SS
11815/* This function attempts to parse an optional "if <cond>" clause
11816 from the arg string. If one is not found, it returns NULL.
c5aa993b 11817
c906108c
SS
11818 Else, it returns a pointer to the condition string. (It does not
11819 attempt to evaluate the string against a particular block.) And,
11820 it updates arg to point to the first character following the parsed
4a64f543 11821 if clause in the arg string. */
53a5351d 11822
916703c0 11823char *
fba45db2 11824ep_parse_optional_if_clause (char **arg)
c906108c 11825{
c5aa993b
JM
11826 char *cond_string;
11827
11828 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11829 return NULL;
c5aa993b 11830
4a64f543 11831 /* Skip the "if" keyword. */
c906108c 11832 (*arg) += 2;
c5aa993b 11833
c906108c 11834 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11835 condition string. */
e9cafbcc 11836 *arg = skip_spaces (*arg);
c906108c 11837 cond_string = *arg;
c5aa993b 11838
4a64f543
MS
11839 /* Assume that the condition occupies the remainder of the arg
11840 string. */
c906108c 11841 (*arg) += strlen (cond_string);
c5aa993b 11842
c906108c
SS
11843 return cond_string;
11844}
c5aa993b 11845
c906108c
SS
11846/* Commands to deal with catching events, such as signals, exceptions,
11847 process start/exit, etc. */
c5aa993b
JM
11848
11849typedef enum
11850{
44feb3ce
TT
11851 catch_fork_temporary, catch_vfork_temporary,
11852 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11853}
11854catch_fork_kind;
11855
c906108c 11856static void
cc59ec59
MS
11857catch_fork_command_1 (char *arg, int from_tty,
11858 struct cmd_list_element *command)
c906108c 11859{
a6d9a66e 11860 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11861 char *cond_string = NULL;
44feb3ce
TT
11862 catch_fork_kind fork_kind;
11863 int tempflag;
11864
11865 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11866 tempflag = (fork_kind == catch_fork_temporary
11867 || fork_kind == catch_vfork_temporary);
c5aa993b 11868
44feb3ce
TT
11869 if (!arg)
11870 arg = "";
e9cafbcc 11871 arg = skip_spaces (arg);
c5aa993b 11872
c906108c 11873 /* The allowed syntax is:
c5aa993b
JM
11874 catch [v]fork
11875 catch [v]fork if <cond>
11876
4a64f543 11877 First, check if there's an if clause. */
c906108c 11878 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11879
c906108c 11880 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11881 error (_("Junk at end of arguments."));
c5aa993b 11882
c906108c 11883 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11884 and enable reporting of such events. */
c5aa993b
JM
11885 switch (fork_kind)
11886 {
44feb3ce
TT
11887 case catch_fork_temporary:
11888 case catch_fork_permanent:
a6d9a66e 11889 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11890 &catch_fork_breakpoint_ops);
c906108c 11891 break;
44feb3ce
TT
11892 case catch_vfork_temporary:
11893 case catch_vfork_permanent:
a6d9a66e 11894 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11895 &catch_vfork_breakpoint_ops);
c906108c 11896 break;
c5aa993b 11897 default:
8a3fe4f8 11898 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11899 break;
c5aa993b 11900 }
c906108c
SS
11901}
11902
11903static void
cc59ec59
MS
11904catch_exec_command_1 (char *arg, int from_tty,
11905 struct cmd_list_element *command)
c906108c 11906{
b4d90040 11907 struct exec_catchpoint *c;
a6d9a66e 11908 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11909 int tempflag;
c5aa993b 11910 char *cond_string = NULL;
c906108c 11911
44feb3ce
TT
11912 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11913
11914 if (!arg)
11915 arg = "";
e9cafbcc 11916 arg = skip_spaces (arg);
c906108c
SS
11917
11918 /* The allowed syntax is:
c5aa993b
JM
11919 catch exec
11920 catch exec if <cond>
c906108c 11921
4a64f543 11922 First, check if there's an if clause. */
c906108c
SS
11923 cond_string = ep_parse_optional_if_clause (&arg);
11924
11925 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11926 error (_("Junk at end of arguments."));
c906108c 11927
4d01a485 11928 c = new exec_catchpoint ();
b4d90040
PA
11929 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11930 &catch_exec_breakpoint_ops);
11931 c->exec_pathname = NULL;
11932
3ea46bff 11933 install_breakpoint (0, &c->base, 1);
c906108c 11934}
c5aa993b 11935
9ac4176b 11936void
28010a5d
PA
11937init_ada_exception_breakpoint (struct breakpoint *b,
11938 struct gdbarch *gdbarch,
11939 struct symtab_and_line sal,
11940 char *addr_string,
c0a91b2b 11941 const struct breakpoint_ops *ops,
28010a5d 11942 int tempflag,
349774ef 11943 int enabled,
28010a5d 11944 int from_tty)
f7f9143b 11945{
f7f9143b
JB
11946 if (from_tty)
11947 {
5af949e3
UW
11948 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11949 if (!loc_gdbarch)
11950 loc_gdbarch = gdbarch;
11951
6c95b8df
PA
11952 describe_other_breakpoints (loc_gdbarch,
11953 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11954 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11955 version for exception catchpoints, because two catchpoints
11956 used for different exception names will use the same address.
11957 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11958 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11959 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11960 the user what type of catchpoint it is. The above is good
11961 enough for now, though. */
11962 }
11963
28010a5d 11964 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11965
349774ef 11966 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11967 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11968 b->location = string_to_event_location (&addr_string,
11969 language_def (language_ada));
f7f9143b 11970 b->language = language_ada;
f7f9143b
JB
11971}
11972
c906108c 11973static void
fba45db2 11974catch_command (char *arg, int from_tty)
c906108c 11975{
44feb3ce 11976 error (_("Catch requires an event name."));
c906108c
SS
11977}
11978\f
11979
11980static void
fba45db2 11981tcatch_command (char *arg, int from_tty)
c906108c 11982{
44feb3ce 11983 error (_("Catch requires an event name."));
c906108c
SS
11984}
11985
8a2c437b
TT
11986/* A qsort comparison function that sorts breakpoints in order. */
11987
11988static int
11989compare_breakpoints (const void *a, const void *b)
11990{
9a3c8263 11991 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11992 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11993 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11994 uintptr_t ub = (uintptr_t) *bb;
11995
11996 if ((*ba)->number < (*bb)->number)
11997 return -1;
11998 else if ((*ba)->number > (*bb)->number)
11999 return 1;
12000
12001 /* Now sort by address, in case we see, e..g, two breakpoints with
12002 the number 0. */
12003 if (ua < ub)
12004 return -1;
94b0e70d 12005 return ua > ub ? 1 : 0;
8a2c437b
TT
12006}
12007
80f8a6eb 12008/* Delete breakpoints by address or line. */
c906108c
SS
12009
12010static void
fba45db2 12011clear_command (char *arg, int from_tty)
c906108c 12012{
8a2c437b 12013 struct breakpoint *b, *prev;
d6e956e5
VP
12014 VEC(breakpoint_p) *found = 0;
12015 int ix;
c906108c
SS
12016 int default_match;
12017 struct symtabs_and_lines sals;
12018 struct symtab_and_line sal;
c906108c 12019 int i;
8a2c437b 12020 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12021
12022 if (arg)
12023 {
39cf75f7
DE
12024 sals = decode_line_with_current_source (arg,
12025 (DECODE_LINE_FUNFIRSTLINE
12026 | DECODE_LINE_LIST_MODE));
cf4ded82 12027 make_cleanup (xfree, sals.sals);
c906108c
SS
12028 default_match = 0;
12029 }
12030 else
12031 {
8d749320 12032 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12033 make_cleanup (xfree, sals.sals);
4a64f543 12034 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12035
12036 /* Set sal's line, symtab, pc, and pspace to the values
12037 corresponding to the last call to print_frame_info. If the
12038 codepoint is not valid, this will set all the fields to 0. */
12039 get_last_displayed_sal (&sal);
c906108c 12040 if (sal.symtab == 0)
8a3fe4f8 12041 error (_("No source file specified."));
c906108c
SS
12042
12043 sals.sals[0] = sal;
12044 sals.nelts = 1;
12045
12046 default_match = 1;
12047 }
12048
4a64f543
MS
12049 /* We don't call resolve_sal_pc here. That's not as bad as it
12050 seems, because all existing breakpoints typically have both
12051 file/line and pc set. So, if clear is given file/line, we can
12052 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12053
12054 We only support clearing given the address explicitly
12055 present in breakpoint table. Say, we've set breakpoint
4a64f543 12056 at file:line. There were several PC values for that file:line,
ed0616c6 12057 due to optimization, all in one block.
4a64f543
MS
12058
12059 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12060 PC corresponding to the same file:line, the breakpoint won't
12061 be cleared. We probably can still clear the breakpoint, but
12062 since the other PC value is never presented to user, user
12063 can only find it by guessing, and it does not seem important
12064 to support that. */
12065
4a64f543
MS
12066 /* For each line spec given, delete bps which correspond to it. Do
12067 it in two passes, solely to preserve the current behavior that
12068 from_tty is forced true if we delete more than one
12069 breakpoint. */
c906108c 12070
80f8a6eb 12071 found = NULL;
8a2c437b 12072 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12073 for (i = 0; i < sals.nelts; i++)
12074 {
05cba821
JK
12075 const char *sal_fullname;
12076
c906108c 12077 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12078 If line given (pc == 0), clear all bpts on specified line.
12079 If defaulting, clear all bpts on default line
c906108c 12080 or at default pc.
c5aa993b
JM
12081
12082 defaulting sal.pc != 0 tests to do
12083
12084 0 1 pc
12085 1 1 pc _and_ line
12086 0 0 line
12087 1 0 <can't happen> */
c906108c
SS
12088
12089 sal = sals.sals[i];
05cba821
JK
12090 sal_fullname = (sal.symtab == NULL
12091 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12092
4a64f543 12093 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12094 ALL_BREAKPOINTS (b)
c5aa993b 12095 {
0d381245 12096 int match = 0;
4a64f543 12097 /* Are we going to delete b? */
cc60f2e3 12098 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12099 {
12100 struct bp_location *loc = b->loc;
12101 for (; loc; loc = loc->next)
12102 {
f8eba3c6
TT
12103 /* If the user specified file:line, don't allow a PC
12104 match. This matches historical gdb behavior. */
12105 int pc_match = (!sal.explicit_line
12106 && sal.pc
12107 && (loc->pspace == sal.pspace)
12108 && (loc->address == sal.pc)
12109 && (!section_is_overlay (loc->section)
12110 || loc->section == sal.section));
4aac40c8
TT
12111 int line_match = 0;
12112
12113 if ((default_match || sal.explicit_line)
2f202fde 12114 && loc->symtab != NULL
05cba821 12115 && sal_fullname != NULL
4aac40c8 12116 && sal.pspace == loc->pspace
05cba821
JK
12117 && loc->line_number == sal.line
12118 && filename_cmp (symtab_to_fullname (loc->symtab),
12119 sal_fullname) == 0)
12120 line_match = 1;
4aac40c8 12121
0d381245
VP
12122 if (pc_match || line_match)
12123 {
12124 match = 1;
12125 break;
12126 }
12127 }
12128 }
12129
12130 if (match)
d6e956e5 12131 VEC_safe_push(breakpoint_p, found, b);
c906108c 12132 }
80f8a6eb 12133 }
8a2c437b 12134
80f8a6eb 12135 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12136 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12137 {
12138 if (arg)
8a3fe4f8 12139 error (_("No breakpoint at %s."), arg);
80f8a6eb 12140 else
8a3fe4f8 12141 error (_("No breakpoint at this line."));
80f8a6eb 12142 }
c906108c 12143
8a2c437b
TT
12144 /* Remove duplicates from the vec. */
12145 qsort (VEC_address (breakpoint_p, found),
12146 VEC_length (breakpoint_p, found),
12147 sizeof (breakpoint_p),
12148 compare_breakpoints);
12149 prev = VEC_index (breakpoint_p, found, 0);
12150 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12151 {
12152 if (b == prev)
12153 {
12154 VEC_ordered_remove (breakpoint_p, found, ix);
12155 --ix;
12156 }
12157 }
12158
d6e956e5 12159 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12160 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12161 if (from_tty)
a3f17187 12162 {
d6e956e5 12163 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12164 printf_unfiltered (_("Deleted breakpoint "));
12165 else
12166 printf_unfiltered (_("Deleted breakpoints "));
12167 }
d6e956e5
VP
12168
12169 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12170 {
c5aa993b 12171 if (from_tty)
d6e956e5
VP
12172 printf_unfiltered ("%d ", b->number);
12173 delete_breakpoint (b);
c906108c 12174 }
80f8a6eb
MS
12175 if (from_tty)
12176 putchar_unfiltered ('\n');
8a2c437b
TT
12177
12178 do_cleanups (cleanups);
c906108c
SS
12179}
12180\f
12181/* Delete breakpoint in BS if they are `delete' breakpoints and
12182 all breakpoints that are marked for deletion, whether hit or not.
12183 This is called after any breakpoint is hit, or after errors. */
12184
12185void
fba45db2 12186breakpoint_auto_delete (bpstat bs)
c906108c 12187{
35df4500 12188 struct breakpoint *b, *b_tmp;
c906108c
SS
12189
12190 for (; bs; bs = bs->next)
f431efe5
PA
12191 if (bs->breakpoint_at
12192 && bs->breakpoint_at->disposition == disp_del
c906108c 12193 && bs->stop)
f431efe5 12194 delete_breakpoint (bs->breakpoint_at);
c906108c 12195
35df4500 12196 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12197 {
b5de0fa7 12198 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12199 delete_breakpoint (b);
12200 }
c906108c
SS
12201}
12202
4a64f543
MS
12203/* A comparison function for bp_location AP and BP being interfaced to
12204 qsort. Sort elements primarily by their ADDRESS (no matter what
12205 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12206 secondarily by ordering first permanent elements and
4a64f543 12207 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12208 qsort being an unstable algorithm. */
876fa593
JK
12209
12210static int
494cfb0f 12211bp_location_compare (const void *ap, const void *bp)
876fa593 12212{
9a3c8263
SM
12213 const struct bp_location *a = *(const struct bp_location **) ap;
12214 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12215
12216 if (a->address != b->address)
12217 return (a->address > b->address) - (a->address < b->address);
12218
dea2aa5f
LM
12219 /* Sort locations at the same address by their pspace number, keeping
12220 locations of the same inferior (in a multi-inferior environment)
12221 grouped. */
12222
12223 if (a->pspace->num != b->pspace->num)
12224 return ((a->pspace->num > b->pspace->num)
12225 - (a->pspace->num < b->pspace->num));
12226
876fa593 12227 /* Sort permanent breakpoints first. */
1a853c52
PA
12228 if (a->permanent != b->permanent)
12229 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12230
c56a97f9
JK
12231 /* Make the internal GDB representation stable across GDB runs
12232 where A and B memory inside GDB can differ. Breakpoint locations of
12233 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12234
12235 if (a->owner->number != b->owner->number)
c56a97f9
JK
12236 return ((a->owner->number > b->owner->number)
12237 - (a->owner->number < b->owner->number));
876fa593
JK
12238
12239 return (a > b) - (a < b);
12240}
12241
876fa593 12242/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12243 bp_location_shadow_len_after_address_max according to the current
12244 content of the bp_location array. */
f7545552
TT
12245
12246static void
876fa593 12247bp_location_target_extensions_update (void)
f7545552 12248{
876fa593
JK
12249 struct bp_location *bl, **blp_tmp;
12250
12251 bp_location_placed_address_before_address_max = 0;
12252 bp_location_shadow_len_after_address_max = 0;
12253
12254 ALL_BP_LOCATIONS (bl, blp_tmp)
12255 {
12256 CORE_ADDR start, end, addr;
12257
12258 if (!bp_location_has_shadow (bl))
12259 continue;
12260
12261 start = bl->target_info.placed_address;
12262 end = start + bl->target_info.shadow_len;
12263
12264 gdb_assert (bl->address >= start);
12265 addr = bl->address - start;
12266 if (addr > bp_location_placed_address_before_address_max)
12267 bp_location_placed_address_before_address_max = addr;
12268
12269 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12270
12271 gdb_assert (bl->address < end);
12272 addr = end - bl->address;
12273 if (addr > bp_location_shadow_len_after_address_max)
12274 bp_location_shadow_len_after_address_max = addr;
12275 }
f7545552
TT
12276}
12277
1e4d1764
YQ
12278/* Download tracepoint locations if they haven't been. */
12279
12280static void
12281download_tracepoint_locations (void)
12282{
7ed2c994 12283 struct breakpoint *b;
1e4d1764 12284 struct cleanup *old_chain;
dd2e65cc 12285 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12286
12287 old_chain = save_current_space_and_thread ();
12288
7ed2c994 12289 ALL_TRACEPOINTS (b)
1e4d1764 12290 {
7ed2c994 12291 struct bp_location *bl;
1e4d1764 12292 struct tracepoint *t;
f2a8bc8a 12293 int bp_location_downloaded = 0;
1e4d1764 12294
7ed2c994 12295 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12296 ? !may_insert_fast_tracepoints
12297 : !may_insert_tracepoints))
12298 continue;
12299
dd2e65cc
YQ
12300 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12301 {
12302 if (target_can_download_tracepoint ())
12303 can_download_tracepoint = TRIBOOL_TRUE;
12304 else
12305 can_download_tracepoint = TRIBOOL_FALSE;
12306 }
12307
12308 if (can_download_tracepoint == TRIBOOL_FALSE)
12309 break;
12310
7ed2c994
YQ
12311 for (bl = b->loc; bl; bl = bl->next)
12312 {
12313 /* In tracepoint, locations are _never_ duplicated, so
12314 should_be_inserted is equivalent to
12315 unduplicated_should_be_inserted. */
12316 if (!should_be_inserted (bl) || bl->inserted)
12317 continue;
1e4d1764 12318
7ed2c994 12319 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12320
7ed2c994 12321 target_download_tracepoint (bl);
1e4d1764 12322
7ed2c994 12323 bl->inserted = 1;
f2a8bc8a 12324 bp_location_downloaded = 1;
7ed2c994
YQ
12325 }
12326 t = (struct tracepoint *) b;
12327 t->number_on_target = b->number;
f2a8bc8a
YQ
12328 if (bp_location_downloaded)
12329 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12330 }
12331
12332 do_cleanups (old_chain);
12333}
12334
934709f0
PW
12335/* Swap the insertion/duplication state between two locations. */
12336
12337static void
12338swap_insertion (struct bp_location *left, struct bp_location *right)
12339{
12340 const int left_inserted = left->inserted;
12341 const int left_duplicate = left->duplicate;
b775012e 12342 const int left_needs_update = left->needs_update;
934709f0
PW
12343 const struct bp_target_info left_target_info = left->target_info;
12344
1e4d1764
YQ
12345 /* Locations of tracepoints can never be duplicated. */
12346 if (is_tracepoint (left->owner))
12347 gdb_assert (!left->duplicate);
12348 if (is_tracepoint (right->owner))
12349 gdb_assert (!right->duplicate);
12350
934709f0
PW
12351 left->inserted = right->inserted;
12352 left->duplicate = right->duplicate;
b775012e 12353 left->needs_update = right->needs_update;
934709f0
PW
12354 left->target_info = right->target_info;
12355 right->inserted = left_inserted;
12356 right->duplicate = left_duplicate;
b775012e 12357 right->needs_update = left_needs_update;
934709f0
PW
12358 right->target_info = left_target_info;
12359}
12360
b775012e
LM
12361/* Force the re-insertion of the locations at ADDRESS. This is called
12362 once a new/deleted/modified duplicate location is found and we are evaluating
12363 conditions on the target's side. Such conditions need to be updated on
12364 the target. */
12365
12366static void
12367force_breakpoint_reinsertion (struct bp_location *bl)
12368{
12369 struct bp_location **locp = NULL, **loc2p;
12370 struct bp_location *loc;
12371 CORE_ADDR address = 0;
12372 int pspace_num;
12373
12374 address = bl->address;
12375 pspace_num = bl->pspace->num;
12376
12377 /* This is only meaningful if the target is
12378 evaluating conditions and if the user has
12379 opted for condition evaluation on the target's
12380 side. */
12381 if (gdb_evaluates_breakpoint_condition_p ()
12382 || !target_supports_evaluation_of_breakpoint_conditions ())
12383 return;
12384
12385 /* Flag all breakpoint locations with this address and
12386 the same program space as the location
12387 as "its condition has changed". We need to
12388 update the conditions on the target's side. */
12389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12390 {
12391 loc = *loc2p;
12392
12393 if (!is_breakpoint (loc->owner)
12394 || pspace_num != loc->pspace->num)
12395 continue;
12396
12397 /* Flag the location appropriately. We use a different state to
12398 let everyone know that we already updated the set of locations
12399 with addr bl->address and program space bl->pspace. This is so
12400 we don't have to keep calling these functions just to mark locations
12401 that have already been marked. */
12402 loc->condition_changed = condition_updated;
12403
12404 /* Free the agent expression bytecode as well. We will compute
12405 it later on. */
833177a4 12406 loc->cond_bytecode.reset ();
b775012e
LM
12407 }
12408}
44702360
PA
12409/* Called whether new breakpoints are created, or existing breakpoints
12410 deleted, to update the global location list and recompute which
12411 locations are duplicate of which.
b775012e 12412
04086b45
PA
12413 The INSERT_MODE flag determines whether locations may not, may, or
12414 shall be inserted now. See 'enum ugll_insert_mode' for more
12415 info. */
b60e7edf 12416
0d381245 12417static void
44702360 12418update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12419{
74960c60 12420 struct breakpoint *b;
876fa593 12421 struct bp_location **locp, *loc;
f7545552 12422 struct cleanup *cleanups;
b775012e
LM
12423 /* Last breakpoint location address that was marked for update. */
12424 CORE_ADDR last_addr = 0;
12425 /* Last breakpoint location program space that was marked for update. */
12426 int last_pspace_num = -1;
f7545552 12427
2d134ed3
PA
12428 /* Used in the duplicates detection below. When iterating over all
12429 bp_locations, points to the first bp_location of a given address.
12430 Breakpoints and watchpoints of different types are never
12431 duplicates of each other. Keep one pointer for each type of
12432 breakpoint/watchpoint, so we only need to loop over all locations
12433 once. */
12434 struct bp_location *bp_loc_first; /* breakpoint */
12435 struct bp_location *wp_loc_first; /* hardware watchpoint */
12436 struct bp_location *awp_loc_first; /* access watchpoint */
12437 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12438
4a64f543
MS
12439 /* Saved former bp_location array which we compare against the newly
12440 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12441 struct bp_location **old_location, **old_locp;
12442 unsigned old_location_count;
12443
12444 old_location = bp_location;
12445 old_location_count = bp_location_count;
12446 bp_location = NULL;
12447 bp_location_count = 0;
12448 cleanups = make_cleanup (xfree, old_location);
0d381245 12449
74960c60 12450 ALL_BREAKPOINTS (b)
876fa593
JK
12451 for (loc = b->loc; loc; loc = loc->next)
12452 bp_location_count++;
12453
8d749320 12454 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12455 locp = bp_location;
12456 ALL_BREAKPOINTS (b)
12457 for (loc = b->loc; loc; loc = loc->next)
12458 *locp++ = loc;
12459 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12460 bp_location_compare);
876fa593
JK
12461
12462 bp_location_target_extensions_update ();
74960c60 12463
4a64f543
MS
12464 /* Identify bp_location instances that are no longer present in the
12465 new list, and therefore should be freed. Note that it's not
12466 necessary that those locations should be removed from inferior --
12467 if there's another location at the same address (previously
12468 marked as duplicate), we don't need to remove/insert the
12469 location.
876fa593 12470
4a64f543
MS
12471 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12472 and former bp_location array state respectively. */
876fa593
JK
12473
12474 locp = bp_location;
12475 for (old_locp = old_location; old_locp < old_location + old_location_count;
12476 old_locp++)
74960c60 12477 {
876fa593 12478 struct bp_location *old_loc = *old_locp;
c7d46a38 12479 struct bp_location **loc2p;
876fa593 12480
e5dd4106 12481 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12482 not, we have to free it. */
c7d46a38 12483 int found_object = 0;
20874c92
VP
12484 /* Tells if the location should remain inserted in the target. */
12485 int keep_in_target = 0;
12486 int removed = 0;
876fa593 12487
4a64f543
MS
12488 /* Skip LOCP entries which will definitely never be needed.
12489 Stop either at or being the one matching OLD_LOC. */
876fa593 12490 while (locp < bp_location + bp_location_count
c7d46a38 12491 && (*locp)->address < old_loc->address)
876fa593 12492 locp++;
c7d46a38
PA
12493
12494 for (loc2p = locp;
12495 (loc2p < bp_location + bp_location_count
12496 && (*loc2p)->address == old_loc->address);
12497 loc2p++)
12498 {
b775012e
LM
12499 /* Check if this is a new/duplicated location or a duplicated
12500 location that had its condition modified. If so, we want to send
12501 its condition to the target if evaluation of conditions is taking
12502 place there. */
12503 if ((*loc2p)->condition_changed == condition_modified
12504 && (last_addr != old_loc->address
12505 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12506 {
b775012e
LM
12507 force_breakpoint_reinsertion (*loc2p);
12508 last_pspace_num = old_loc->pspace->num;
c7d46a38 12509 }
b775012e
LM
12510
12511 if (*loc2p == old_loc)
12512 found_object = 1;
c7d46a38 12513 }
74960c60 12514
b775012e
LM
12515 /* We have already handled this address, update it so that we don't
12516 have to go through updates again. */
12517 last_addr = old_loc->address;
12518
12519 /* Target-side condition evaluation: Handle deleted locations. */
12520 if (!found_object)
12521 force_breakpoint_reinsertion (old_loc);
12522
4a64f543
MS
12523 /* If this location is no longer present, and inserted, look if
12524 there's maybe a new location at the same address. If so,
12525 mark that one inserted, and don't remove this one. This is
12526 needed so that we don't have a time window where a breakpoint
12527 at certain location is not inserted. */
74960c60 12528
876fa593 12529 if (old_loc->inserted)
0d381245 12530 {
4a64f543
MS
12531 /* If the location is inserted now, we might have to remove
12532 it. */
74960c60 12533
876fa593 12534 if (found_object && should_be_inserted (old_loc))
74960c60 12535 {
4a64f543
MS
12536 /* The location is still present in the location list,
12537 and still should be inserted. Don't do anything. */
20874c92 12538 keep_in_target = 1;
74960c60
VP
12539 }
12540 else
12541 {
b775012e
LM
12542 /* This location still exists, but it won't be kept in the
12543 target since it may have been disabled. We proceed to
12544 remove its target-side condition. */
12545
4a64f543
MS
12546 /* The location is either no longer present, or got
12547 disabled. See if there's another location at the
12548 same address, in which case we don't need to remove
12549 this one from the target. */
876fa593 12550
2bdf28a0 12551 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12552 if (breakpoint_address_is_meaningful (old_loc->owner))
12553 {
876fa593 12554 for (loc2p = locp;
c7d46a38
PA
12555 (loc2p < bp_location + bp_location_count
12556 && (*loc2p)->address == old_loc->address);
876fa593
JK
12557 loc2p++)
12558 {
12559 struct bp_location *loc2 = *loc2p;
12560
2d134ed3 12561 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12562 {
85d721b8
PA
12563 /* Read watchpoint locations are switched to
12564 access watchpoints, if the former are not
12565 supported, but the latter are. */
12566 if (is_hardware_watchpoint (old_loc->owner))
12567 {
12568 gdb_assert (is_hardware_watchpoint (loc2->owner));
12569 loc2->watchpoint_type = old_loc->watchpoint_type;
12570 }
12571
934709f0
PW
12572 /* loc2 is a duplicated location. We need to check
12573 if it should be inserted in case it will be
12574 unduplicated. */
12575 if (loc2 != old_loc
12576 && unduplicated_should_be_inserted (loc2))
c7d46a38 12577 {
934709f0 12578 swap_insertion (old_loc, loc2);
c7d46a38
PA
12579 keep_in_target = 1;
12580 break;
12581 }
876fa593
JK
12582 }
12583 }
12584 }
74960c60
VP
12585 }
12586
20874c92
VP
12587 if (!keep_in_target)
12588 {
834c0d03 12589 if (remove_breakpoint (old_loc))
20874c92 12590 {
4a64f543
MS
12591 /* This is just about all we can do. We could keep
12592 this location on the global list, and try to
12593 remove it next time, but there's no particular
12594 reason why we will succeed next time.
20874c92 12595
4a64f543
MS
12596 Note that at this point, old_loc->owner is still
12597 valid, as delete_breakpoint frees the breakpoint
12598 only after calling us. */
3e43a32a
MS
12599 printf_filtered (_("warning: Error removing "
12600 "breakpoint %d\n"),
876fa593 12601 old_loc->owner->number);
20874c92
VP
12602 }
12603 removed = 1;
12604 }
0d381245 12605 }
74960c60
VP
12606
12607 if (!found_object)
1c5cfe86 12608 {
fbea99ea 12609 if (removed && target_is_non_stop_p ()
1cf4d951 12610 && need_moribund_for_location_type (old_loc))
20874c92 12611 {
db82e815
PA
12612 /* This location was removed from the target. In
12613 non-stop mode, a race condition is possible where
12614 we've removed a breakpoint, but stop events for that
12615 breakpoint are already queued and will arrive later.
12616 We apply an heuristic to be able to distinguish such
12617 SIGTRAPs from other random SIGTRAPs: we keep this
12618 breakpoint location for a bit, and will retire it
12619 after we see some number of events. The theory here
12620 is that reporting of events should, "on the average",
12621 be fair, so after a while we'll see events from all
12622 threads that have anything of interest, and no longer
12623 need to keep this breakpoint location around. We
12624 don't hold locations forever so to reduce chances of
12625 mistaking a non-breakpoint SIGTRAP for a breakpoint
12626 SIGTRAP.
12627
12628 The heuristic failing can be disastrous on
12629 decr_pc_after_break targets.
12630
12631 On decr_pc_after_break targets, like e.g., x86-linux,
12632 if we fail to recognize a late breakpoint SIGTRAP,
12633 because events_till_retirement has reached 0 too
12634 soon, we'll fail to do the PC adjustment, and report
12635 a random SIGTRAP to the user. When the user resumes
12636 the inferior, it will most likely immediately crash
2dec564e 12637 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12638 corrupted, because of being resumed e.g., in the
12639 middle of a multi-byte instruction, or skipped a
12640 one-byte instruction. This was actually seen happen
12641 on native x86-linux, and should be less rare on
12642 targets that do not support new thread events, like
12643 remote, due to the heuristic depending on
12644 thread_count.
12645
12646 Mistaking a random SIGTRAP for a breakpoint trap
12647 causes similar symptoms (PC adjustment applied when
12648 it shouldn't), but then again, playing with SIGTRAPs
12649 behind the debugger's back is asking for trouble.
12650
12651 Since hardware watchpoint traps are always
12652 distinguishable from other traps, so we don't need to
12653 apply keep hardware watchpoint moribund locations
12654 around. We simply always ignore hardware watchpoint
12655 traps we can no longer explain. */
12656
876fa593
JK
12657 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12658 old_loc->owner = NULL;
20874c92 12659
876fa593 12660 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12661 }
12662 else
f431efe5
PA
12663 {
12664 old_loc->owner = NULL;
12665 decref_bp_location (&old_loc);
12666 }
20874c92 12667 }
74960c60 12668 }
1c5cfe86 12669
348d480f
PA
12670 /* Rescan breakpoints at the same address and section, marking the
12671 first one as "first" and any others as "duplicates". This is so
12672 that the bpt instruction is only inserted once. If we have a
12673 permanent breakpoint at the same place as BPT, make that one the
12674 official one, and the rest as duplicates. Permanent breakpoints
12675 are sorted first for the same address.
12676
12677 Do the same for hardware watchpoints, but also considering the
12678 watchpoint's type (regular/access/read) and length. */
12679
12680 bp_loc_first = NULL;
12681 wp_loc_first = NULL;
12682 awp_loc_first = NULL;
12683 rwp_loc_first = NULL;
12684 ALL_BP_LOCATIONS (loc, locp)
12685 {
12686 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12687 non-NULL. */
348d480f 12688 struct bp_location **loc_first_p;
d3fbdd86 12689 b = loc->owner;
348d480f 12690
6f380991 12691 if (!unduplicated_should_be_inserted (loc)
348d480f 12692 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12693 /* Don't detect duplicate for tracepoint locations because they are
12694 never duplicated. See the comments in field `duplicate' of
12695 `struct bp_location'. */
348d480f 12696 || is_tracepoint (b))
b775012e
LM
12697 {
12698 /* Clear the condition modification flag. */
12699 loc->condition_changed = condition_unchanged;
12700 continue;
12701 }
348d480f 12702
348d480f
PA
12703 if (b->type == bp_hardware_watchpoint)
12704 loc_first_p = &wp_loc_first;
12705 else if (b->type == bp_read_watchpoint)
12706 loc_first_p = &rwp_loc_first;
12707 else if (b->type == bp_access_watchpoint)
12708 loc_first_p = &awp_loc_first;
12709 else
12710 loc_first_p = &bp_loc_first;
12711
12712 if (*loc_first_p == NULL
12713 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12714 || !breakpoint_locations_match (loc, *loc_first_p))
12715 {
12716 *loc_first_p = loc;
12717 loc->duplicate = 0;
b775012e
LM
12718
12719 if (is_breakpoint (loc->owner) && loc->condition_changed)
12720 {
12721 loc->needs_update = 1;
12722 /* Clear the condition modification flag. */
12723 loc->condition_changed = condition_unchanged;
12724 }
348d480f
PA
12725 continue;
12726 }
12727
934709f0
PW
12728
12729 /* This and the above ensure the invariant that the first location
12730 is not duplicated, and is the inserted one.
12731 All following are marked as duplicated, and are not inserted. */
12732 if (loc->inserted)
12733 swap_insertion (loc, *loc_first_p);
348d480f
PA
12734 loc->duplicate = 1;
12735
b775012e
LM
12736 /* Clear the condition modification flag. */
12737 loc->condition_changed = condition_unchanged;
348d480f
PA
12738 }
12739
a25a5a45 12740 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12741 {
04086b45 12742 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12743 insert_breakpoint_locations ();
12744 else
12745 {
44702360
PA
12746 /* Even though the caller told us to not insert new
12747 locations, we may still need to update conditions on the
12748 target's side of breakpoints that were already inserted
12749 if the target is evaluating breakpoint conditions. We
b775012e
LM
12750 only update conditions for locations that are marked
12751 "needs_update". */
12752 update_inserted_breakpoint_locations ();
12753 }
12754 }
348d480f 12755
04086b45 12756 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12757 download_tracepoint_locations ();
12758
348d480f
PA
12759 do_cleanups (cleanups);
12760}
12761
12762void
12763breakpoint_retire_moribund (void)
12764{
12765 struct bp_location *loc;
12766 int ix;
12767
12768 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12769 if (--(loc->events_till_retirement) == 0)
12770 {
12771 decref_bp_location (&loc);
12772 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12773 --ix;
12774 }
12775}
12776
12777static void
44702360 12778update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12779{
348d480f 12780
492d29ea
PA
12781 TRY
12782 {
12783 update_global_location_list (insert_mode);
12784 }
12785 CATCH (e, RETURN_MASK_ERROR)
12786 {
12787 }
12788 END_CATCH
348d480f
PA
12789}
12790
12791/* Clear BKP from a BPS. */
12792
12793static void
12794bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12795{
12796 bpstat bs;
12797
12798 for (bs = bps; bs; bs = bs->next)
12799 if (bs->breakpoint_at == bpt)
12800 {
12801 bs->breakpoint_at = NULL;
12802 bs->old_val = NULL;
12803 /* bs->commands will be freed later. */
12804 }
12805}
12806
12807/* Callback for iterate_over_threads. */
12808static int
12809bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12810{
9a3c8263 12811 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12812
12813 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12814 return 0;
12815}
12816
12817/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12818 callbacks. */
12819
12820static void
12821say_where (struct breakpoint *b)
12822{
12823 struct value_print_options opts;
12824
12825 get_user_print_options (&opts);
12826
12827 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12828 single string. */
12829 if (b->loc == NULL)
12830 {
f00aae0f
KS
12831 /* For pending locations, the output differs slightly based
12832 on b->extra_string. If this is non-NULL, it contains either
12833 a condition or dprintf arguments. */
12834 if (b->extra_string == NULL)
12835 {
12836 printf_filtered (_(" (%s) pending."),
12837 event_location_to_string (b->location));
12838 }
12839 else if (b->type == bp_dprintf)
12840 {
12841 printf_filtered (_(" (%s,%s) pending."),
12842 event_location_to_string (b->location),
12843 b->extra_string);
12844 }
12845 else
12846 {
12847 printf_filtered (_(" (%s %s) pending."),
12848 event_location_to_string (b->location),
12849 b->extra_string);
12850 }
348d480f
PA
12851 }
12852 else
12853 {
2f202fde 12854 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12855 {
12856 printf_filtered (" at ");
12857 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12858 gdb_stdout);
12859 }
2f202fde 12860 if (b->loc->symtab != NULL)
f8eba3c6
TT
12861 {
12862 /* If there is a single location, we can print the location
12863 more nicely. */
12864 if (b->loc->next == NULL)
12865 printf_filtered (": file %s, line %d.",
05cba821
JK
12866 symtab_to_filename_for_display (b->loc->symtab),
12867 b->loc->line_number);
f8eba3c6
TT
12868 else
12869 /* This is not ideal, but each location may have a
12870 different file name, and this at least reflects the
12871 real situation somewhat. */
f00aae0f
KS
12872 printf_filtered (": %s.",
12873 event_location_to_string (b->location));
f8eba3c6 12874 }
348d480f
PA
12875
12876 if (b->loc->next)
12877 {
12878 struct bp_location *loc = b->loc;
12879 int n = 0;
12880 for (; loc; loc = loc->next)
12881 ++n;
12882 printf_filtered (" (%d locations)", n);
12883 }
12884 }
12885}
12886
348d480f
PA
12887/* Default bp_location_ops methods. */
12888
12889static void
12890bp_location_dtor (struct bp_location *self)
12891{
348d480f
PA
12892 xfree (self->function_name);
12893}
12894
12895static const struct bp_location_ops bp_location_ops =
12896{
12897 bp_location_dtor
12898};
12899
2060206e
PA
12900/* Default breakpoint_ops methods all breakpoint_ops ultimately
12901 inherit from. */
348d480f 12902
2060206e
PA
12903static void
12904base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12905{
12906 decref_counted_command_line (&self->commands);
12907 xfree (self->cond_string);
fb81d016 12908 xfree (self->extra_string);
f8eba3c6 12909 xfree (self->filter);
f00aae0f
KS
12910 delete_event_location (self->location);
12911 delete_event_location (self->location_range_end);
348d480f
PA
12912}
12913
2060206e
PA
12914static struct bp_location *
12915base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12916{
12917 struct bp_location *loc;
12918
4d01a485 12919 loc = new struct bp_location ();
348d480f
PA
12920 init_bp_location (loc, &bp_location_ops, self);
12921 return loc;
12922}
12923
2060206e
PA
12924static void
12925base_breakpoint_re_set (struct breakpoint *b)
12926{
12927 /* Nothing to re-set. */
12928}
12929
12930#define internal_error_pure_virtual_called() \
12931 gdb_assert_not_reached ("pure virtual function called")
12932
12933static int
12934base_breakpoint_insert_location (struct bp_location *bl)
12935{
12936 internal_error_pure_virtual_called ();
12937}
12938
12939static int
73971819
PA
12940base_breakpoint_remove_location (struct bp_location *bl,
12941 enum remove_bp_reason reason)
2060206e
PA
12942{
12943 internal_error_pure_virtual_called ();
12944}
12945
12946static int
12947base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12948 struct address_space *aspace,
09ac7c10
TT
12949 CORE_ADDR bp_addr,
12950 const struct target_waitstatus *ws)
2060206e
PA
12951{
12952 internal_error_pure_virtual_called ();
12953}
12954
12955static void
12956base_breakpoint_check_status (bpstat bs)
12957{
12958 /* Always stop. */
12959}
12960
12961/* A "works_in_software_mode" breakpoint_ops method that just internal
12962 errors. */
12963
12964static int
12965base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12966{
12967 internal_error_pure_virtual_called ();
12968}
12969
12970/* A "resources_needed" breakpoint_ops method that just internal
12971 errors. */
12972
12973static int
12974base_breakpoint_resources_needed (const struct bp_location *bl)
12975{
12976 internal_error_pure_virtual_called ();
12977}
12978
12979static enum print_stop_action
12980base_breakpoint_print_it (bpstat bs)
12981{
12982 internal_error_pure_virtual_called ();
12983}
12984
12985static void
12986base_breakpoint_print_one_detail (const struct breakpoint *self,
12987 struct ui_out *uiout)
12988{
12989 /* nothing */
12990}
12991
12992static void
12993base_breakpoint_print_mention (struct breakpoint *b)
12994{
12995 internal_error_pure_virtual_called ();
12996}
12997
12998static void
12999base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13000{
13001 internal_error_pure_virtual_called ();
13002}
13003
983af33b 13004static void
f00aae0f
KS
13005base_breakpoint_create_sals_from_location
13006 (const struct event_location *location,
13007 struct linespec_result *canonical,
13008 enum bptype type_wanted)
983af33b
SDJ
13009{
13010 internal_error_pure_virtual_called ();
13011}
13012
13013static void
13014base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13015 struct linespec_result *c,
983af33b 13016 char *cond_string,
e7e0cddf 13017 char *extra_string,
983af33b
SDJ
13018 enum bptype type_wanted,
13019 enum bpdisp disposition,
13020 int thread,
13021 int task, int ignore_count,
13022 const struct breakpoint_ops *o,
13023 int from_tty, int enabled,
44f238bb 13024 int internal, unsigned flags)
983af33b
SDJ
13025{
13026 internal_error_pure_virtual_called ();
13027}
13028
13029static void
f00aae0f
KS
13030base_breakpoint_decode_location (struct breakpoint *b,
13031 const struct event_location *location,
c2f4122d 13032 struct program_space *search_pspace,
983af33b
SDJ
13033 struct symtabs_and_lines *sals)
13034{
13035 internal_error_pure_virtual_called ();
13036}
13037
ab04a2af
TT
13038/* The default 'explains_signal' method. */
13039
47591c29 13040static int
427cd150 13041base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13042{
47591c29 13043 return 1;
ab04a2af
TT
13044}
13045
9d6e6e84
HZ
13046/* The default "after_condition_true" method. */
13047
13048static void
13049base_breakpoint_after_condition_true (struct bpstats *bs)
13050{
13051 /* Nothing to do. */
13052}
13053
ab04a2af 13054struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13055{
13056 base_breakpoint_dtor,
13057 base_breakpoint_allocate_location,
13058 base_breakpoint_re_set,
13059 base_breakpoint_insert_location,
13060 base_breakpoint_remove_location,
13061 base_breakpoint_breakpoint_hit,
13062 base_breakpoint_check_status,
13063 base_breakpoint_resources_needed,
13064 base_breakpoint_works_in_software_mode,
13065 base_breakpoint_print_it,
13066 NULL,
13067 base_breakpoint_print_one_detail,
13068 base_breakpoint_print_mention,
983af33b 13069 base_breakpoint_print_recreate,
5f700d83 13070 base_breakpoint_create_sals_from_location,
983af33b 13071 base_breakpoint_create_breakpoints_sal,
5f700d83 13072 base_breakpoint_decode_location,
9d6e6e84
HZ
13073 base_breakpoint_explains_signal,
13074 base_breakpoint_after_condition_true,
2060206e
PA
13075};
13076
13077/* Default breakpoint_ops methods. */
13078
13079static void
348d480f
PA
13080bkpt_re_set (struct breakpoint *b)
13081{
06edf0c0 13082 /* FIXME: is this still reachable? */
9ef9e6a6 13083 if (breakpoint_event_location_empty_p (b))
06edf0c0 13084 {
f00aae0f 13085 /* Anything without a location can't be re-set. */
348d480f 13086 delete_breakpoint (b);
06edf0c0 13087 return;
348d480f 13088 }
06edf0c0
PA
13089
13090 breakpoint_re_set_default (b);
348d480f
PA
13091}
13092
2060206e 13093static int
348d480f
PA
13094bkpt_insert_location (struct bp_location *bl)
13095{
cd6c3b4f
YQ
13096 CORE_ADDR addr = bl->target_info.reqstd_address;
13097
579c6ad9 13098 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
13099 bl->target_info.placed_address = addr;
13100
348d480f 13101 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13102 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13103 else
7c16b83e 13104 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13105}
13106
2060206e 13107static int
73971819 13108bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
13109{
13110 if (bl->loc_type == bp_loc_hardware_breakpoint)
13111 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13112 else
73971819 13113 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
13114}
13115
2060206e 13116static int
348d480f 13117bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13118 struct address_space *aspace, CORE_ADDR bp_addr,
13119 const struct target_waitstatus *ws)
348d480f 13120{
09ac7c10 13121 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13122 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13123 return 0;
13124
348d480f
PA
13125 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13126 aspace, bp_addr))
13127 return 0;
13128
13129 if (overlay_debugging /* unmapped overlay section */
13130 && section_is_overlay (bl->section)
13131 && !section_is_mapped (bl->section))
13132 return 0;
13133
13134 return 1;
13135}
13136
cd1608cc
PA
13137static int
13138dprintf_breakpoint_hit (const struct bp_location *bl,
13139 struct address_space *aspace, CORE_ADDR bp_addr,
13140 const struct target_waitstatus *ws)
13141{
13142 if (dprintf_style == dprintf_style_agent
13143 && target_can_run_breakpoint_commands ())
13144 {
13145 /* An agent-style dprintf never causes a stop. If we see a trap
13146 for this address it must be for a breakpoint that happens to
13147 be set at the same address. */
13148 return 0;
13149 }
13150
13151 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13152}
13153
2060206e 13154static int
348d480f
PA
13155bkpt_resources_needed (const struct bp_location *bl)
13156{
13157 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13158
13159 return 1;
13160}
13161
2060206e 13162static enum print_stop_action
348d480f
PA
13163bkpt_print_it (bpstat bs)
13164{
348d480f
PA
13165 struct breakpoint *b;
13166 const struct bp_location *bl;
001c8c33 13167 int bp_temp;
79a45e25 13168 struct ui_out *uiout = current_uiout;
348d480f
PA
13169
13170 gdb_assert (bs->bp_location_at != NULL);
13171
13172 bl = bs->bp_location_at;
13173 b = bs->breakpoint_at;
13174
001c8c33
PA
13175 bp_temp = b->disposition == disp_del;
13176 if (bl->address != bl->requested_address)
13177 breakpoint_adjustment_warning (bl->requested_address,
13178 bl->address,
13179 b->number, 1);
13180 annotate_breakpoint (b->number);
f303dbd6
PA
13181 maybe_print_thread_hit_breakpoint (uiout);
13182
001c8c33 13183 if (bp_temp)
f303dbd6 13184 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13185 else
f303dbd6 13186 ui_out_text (uiout, "Breakpoint ");
001c8c33 13187 if (ui_out_is_mi_like_p (uiout))
348d480f 13188 {
001c8c33
PA
13189 ui_out_field_string (uiout, "reason",
13190 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13191 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13192 }
001c8c33
PA
13193 ui_out_field_int (uiout, "bkptno", b->number);
13194 ui_out_text (uiout, ", ");
06edf0c0 13195
001c8c33 13196 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13197}
13198
2060206e 13199static void
06edf0c0
PA
13200bkpt_print_mention (struct breakpoint *b)
13201{
79a45e25 13202 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13203 return;
13204
13205 switch (b->type)
13206 {
13207 case bp_breakpoint:
13208 case bp_gnu_ifunc_resolver:
13209 if (b->disposition == disp_del)
13210 printf_filtered (_("Temporary breakpoint"));
13211 else
13212 printf_filtered (_("Breakpoint"));
13213 printf_filtered (_(" %d"), b->number);
13214 if (b->type == bp_gnu_ifunc_resolver)
13215 printf_filtered (_(" at gnu-indirect-function resolver"));
13216 break;
13217 case bp_hardware_breakpoint:
13218 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13219 break;
e7e0cddf
SS
13220 case bp_dprintf:
13221 printf_filtered (_("Dprintf %d"), b->number);
13222 break;
06edf0c0
PA
13223 }
13224
13225 say_where (b);
13226}
13227
2060206e 13228static void
06edf0c0
PA
13229bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13230{
13231 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13232 fprintf_unfiltered (fp, "tbreak");
13233 else if (tp->type == bp_breakpoint)
13234 fprintf_unfiltered (fp, "break");
13235 else if (tp->type == bp_hardware_breakpoint
13236 && tp->disposition == disp_del)
13237 fprintf_unfiltered (fp, "thbreak");
13238 else if (tp->type == bp_hardware_breakpoint)
13239 fprintf_unfiltered (fp, "hbreak");
13240 else
13241 internal_error (__FILE__, __LINE__,
13242 _("unhandled breakpoint type %d"), (int) tp->type);
13243
f00aae0f
KS
13244 fprintf_unfiltered (fp, " %s",
13245 event_location_to_string (tp->location));
13246
13247 /* Print out extra_string if this breakpoint is pending. It might
13248 contain, for example, conditions that were set by the user. */
13249 if (tp->loc == NULL && tp->extra_string != NULL)
13250 fprintf_unfiltered (fp, " %s", tp->extra_string);
13251
dd11a36c 13252 print_recreate_thread (tp, fp);
06edf0c0
PA
13253}
13254
983af33b 13255static void
f00aae0f
KS
13256bkpt_create_sals_from_location (const struct event_location *location,
13257 struct linespec_result *canonical,
13258 enum bptype type_wanted)
983af33b 13259{
f00aae0f 13260 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13261}
13262
13263static void
13264bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13265 struct linespec_result *canonical,
983af33b 13266 char *cond_string,
e7e0cddf 13267 char *extra_string,
983af33b
SDJ
13268 enum bptype type_wanted,
13269 enum bpdisp disposition,
13270 int thread,
13271 int task, int ignore_count,
13272 const struct breakpoint_ops *ops,
13273 int from_tty, int enabled,
44f238bb 13274 int internal, unsigned flags)
983af33b 13275{
023fa29b 13276 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13277 cond_string, extra_string,
13278 type_wanted,
983af33b
SDJ
13279 disposition, thread, task,
13280 ignore_count, ops, from_tty,
44f238bb 13281 enabled, internal, flags);
983af33b
SDJ
13282}
13283
13284static void
f00aae0f
KS
13285bkpt_decode_location (struct breakpoint *b,
13286 const struct event_location *location,
c2f4122d 13287 struct program_space *search_pspace,
983af33b
SDJ
13288 struct symtabs_and_lines *sals)
13289{
c2f4122d 13290 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13291}
13292
06edf0c0
PA
13293/* Virtual table for internal breakpoints. */
13294
13295static void
13296internal_bkpt_re_set (struct breakpoint *b)
13297{
13298 switch (b->type)
13299 {
13300 /* Delete overlay event and longjmp master breakpoints; they
13301 will be reset later by breakpoint_re_set. */
13302 case bp_overlay_event:
13303 case bp_longjmp_master:
13304 case bp_std_terminate_master:
13305 case bp_exception_master:
13306 delete_breakpoint (b);
13307 break;
13308
13309 /* This breakpoint is special, it's set up when the inferior
13310 starts and we really don't want to touch it. */
13311 case bp_shlib_event:
13312
13313 /* Like bp_shlib_event, this breakpoint type is special. Once
13314 it is set up, we do not want to touch it. */
13315 case bp_thread_event:
13316 break;
13317 }
13318}
13319
13320static void
13321internal_bkpt_check_status (bpstat bs)
13322{
a9b3a50f
PA
13323 if (bs->breakpoint_at->type == bp_shlib_event)
13324 {
13325 /* If requested, stop when the dynamic linker notifies GDB of
13326 events. This allows the user to get control and place
13327 breakpoints in initializer routines for dynamically loaded
13328 objects (among other things). */
13329 bs->stop = stop_on_solib_events;
13330 bs->print = stop_on_solib_events;
13331 }
13332 else
13333 bs->stop = 0;
06edf0c0
PA
13334}
13335
13336static enum print_stop_action
13337internal_bkpt_print_it (bpstat bs)
13338{
06edf0c0 13339 struct breakpoint *b;
06edf0c0 13340
06edf0c0
PA
13341 b = bs->breakpoint_at;
13342
06edf0c0
PA
13343 switch (b->type)
13344 {
348d480f
PA
13345 case bp_shlib_event:
13346 /* Did we stop because the user set the stop_on_solib_events
13347 variable? (If so, we report this as a generic, "Stopped due
13348 to shlib event" message.) */
edcc5120 13349 print_solib_event (0);
348d480f
PA
13350 break;
13351
13352 case bp_thread_event:
13353 /* Not sure how we will get here.
13354 GDB should not stop for these breakpoints. */
13355 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13356 break;
13357
13358 case bp_overlay_event:
13359 /* By analogy with the thread event, GDB should not stop for these. */
13360 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13361 break;
13362
13363 case bp_longjmp_master:
13364 /* These should never be enabled. */
13365 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13366 break;
13367
13368 case bp_std_terminate_master:
13369 /* These should never be enabled. */
13370 printf_filtered (_("std::terminate Master Breakpoint: "
13371 "gdb should not stop!\n"));
348d480f
PA
13372 break;
13373
13374 case bp_exception_master:
13375 /* These should never be enabled. */
13376 printf_filtered (_("Exception Master Breakpoint: "
13377 "gdb should not stop!\n"));
06edf0c0
PA
13378 break;
13379 }
13380
001c8c33 13381 return PRINT_NOTHING;
06edf0c0
PA
13382}
13383
13384static void
13385internal_bkpt_print_mention (struct breakpoint *b)
13386{
13387 /* Nothing to mention. These breakpoints are internal. */
13388}
13389
06edf0c0
PA
13390/* Virtual table for momentary breakpoints */
13391
13392static void
13393momentary_bkpt_re_set (struct breakpoint *b)
13394{
13395 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13396 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13397 Otherwise these should have been blown away via the cleanup chain
13398 or by breakpoint_init_inferior when we rerun the executable. */
13399}
13400
13401static void
13402momentary_bkpt_check_status (bpstat bs)
13403{
13404 /* Nothing. The point of these breakpoints is causing a stop. */
13405}
13406
13407static enum print_stop_action
13408momentary_bkpt_print_it (bpstat bs)
13409{
001c8c33 13410 return PRINT_UNKNOWN;
348d480f
PA
13411}
13412
06edf0c0
PA
13413static void
13414momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13415{
06edf0c0 13416 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13417}
13418
e2e4d78b
JK
13419/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13420
13421 It gets cleared already on the removal of the first one of such placed
13422 breakpoints. This is OK as they get all removed altogether. */
13423
13424static void
13425longjmp_bkpt_dtor (struct breakpoint *self)
13426{
5d5658a1 13427 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13428
13429 if (tp)
13430 tp->initiating_frame = null_frame_id;
13431
13432 momentary_breakpoint_ops.dtor (self);
13433}
13434
55aa24fb
SDJ
13435/* Specific methods for probe breakpoints. */
13436
13437static int
13438bkpt_probe_insert_location (struct bp_location *bl)
13439{
13440 int v = bkpt_insert_location (bl);
13441
13442 if (v == 0)
13443 {
13444 /* The insertion was successful, now let's set the probe's semaphore
13445 if needed. */
0ea5cda8
SDJ
13446 if (bl->probe.probe->pops->set_semaphore != NULL)
13447 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13448 bl->probe.objfile,
13449 bl->gdbarch);
55aa24fb
SDJ
13450 }
13451
13452 return v;
13453}
13454
13455static int
73971819
PA
13456bkpt_probe_remove_location (struct bp_location *bl,
13457 enum remove_bp_reason reason)
55aa24fb
SDJ
13458{
13459 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13460 if (bl->probe.probe->pops->clear_semaphore != NULL)
13461 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13462 bl->probe.objfile,
13463 bl->gdbarch);
55aa24fb 13464
73971819 13465 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13466}
13467
13468static void
f00aae0f 13469bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13470 struct linespec_result *canonical,
f00aae0f 13471 enum bptype type_wanted)
55aa24fb
SDJ
13472{
13473 struct linespec_sals lsal;
13474
c2f4122d 13475 lsal.sals = parse_probes (location, NULL, canonical);
f00aae0f 13476 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13477 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13478}
13479
13480static void
f00aae0f
KS
13481bkpt_probe_decode_location (struct breakpoint *b,
13482 const struct event_location *location,
c2f4122d 13483 struct program_space *search_pspace,
55aa24fb
SDJ
13484 struct symtabs_and_lines *sals)
13485{
c2f4122d 13486 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13487 if (!sals->sals)
13488 error (_("probe not found"));
13489}
13490
348d480f 13491/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13492
348d480f
PA
13493static void
13494tracepoint_re_set (struct breakpoint *b)
13495{
13496 breakpoint_re_set_default (b);
13497}
876fa593 13498
348d480f
PA
13499static int
13500tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13501 struct address_space *aspace, CORE_ADDR bp_addr,
13502 const struct target_waitstatus *ws)
348d480f
PA
13503{
13504 /* By definition, the inferior does not report stops at
13505 tracepoints. */
13506 return 0;
74960c60
VP
13507}
13508
13509static void
348d480f
PA
13510tracepoint_print_one_detail (const struct breakpoint *self,
13511 struct ui_out *uiout)
74960c60 13512{
d9b3f62e
PA
13513 struct tracepoint *tp = (struct tracepoint *) self;
13514 if (tp->static_trace_marker_id)
348d480f
PA
13515 {
13516 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13517
348d480f
PA
13518 ui_out_text (uiout, "\tmarker id is ");
13519 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13520 tp->static_trace_marker_id);
348d480f
PA
13521 ui_out_text (uiout, "\n");
13522 }
0d381245
VP
13523}
13524
a474d7c2 13525static void
348d480f 13526tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13527{
79a45e25 13528 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13529 return;
cc59ec59 13530
348d480f
PA
13531 switch (b->type)
13532 {
13533 case bp_tracepoint:
13534 printf_filtered (_("Tracepoint"));
13535 printf_filtered (_(" %d"), b->number);
13536 break;
13537 case bp_fast_tracepoint:
13538 printf_filtered (_("Fast tracepoint"));
13539 printf_filtered (_(" %d"), b->number);
13540 break;
13541 case bp_static_tracepoint:
13542 printf_filtered (_("Static tracepoint"));
13543 printf_filtered (_(" %d"), b->number);
13544 break;
13545 default:
13546 internal_error (__FILE__, __LINE__,
13547 _("unhandled tracepoint type %d"), (int) b->type);
13548 }
13549
13550 say_where (b);
a474d7c2
PA
13551}
13552
348d480f 13553static void
d9b3f62e 13554tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13555{
d9b3f62e
PA
13556 struct tracepoint *tp = (struct tracepoint *) self;
13557
13558 if (self->type == bp_fast_tracepoint)
348d480f 13559 fprintf_unfiltered (fp, "ftrace");
c93e8391 13560 else if (self->type == bp_static_tracepoint)
348d480f 13561 fprintf_unfiltered (fp, "strace");
d9b3f62e 13562 else if (self->type == bp_tracepoint)
348d480f
PA
13563 fprintf_unfiltered (fp, "trace");
13564 else
13565 internal_error (__FILE__, __LINE__,
d9b3f62e 13566 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13567
f00aae0f
KS
13568 fprintf_unfiltered (fp, " %s",
13569 event_location_to_string (self->location));
d9b3f62e
PA
13570 print_recreate_thread (self, fp);
13571
13572 if (tp->pass_count)
13573 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13574}
13575
983af33b 13576static void
f00aae0f
KS
13577tracepoint_create_sals_from_location (const struct event_location *location,
13578 struct linespec_result *canonical,
13579 enum bptype type_wanted)
983af33b 13580{
f00aae0f 13581 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13582}
13583
13584static void
13585tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13586 struct linespec_result *canonical,
983af33b 13587 char *cond_string,
e7e0cddf 13588 char *extra_string,
983af33b
SDJ
13589 enum bptype type_wanted,
13590 enum bpdisp disposition,
13591 int thread,
13592 int task, int ignore_count,
13593 const struct breakpoint_ops *ops,
13594 int from_tty, int enabled,
44f238bb 13595 int internal, unsigned flags)
983af33b 13596{
023fa29b 13597 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13598 cond_string, extra_string,
13599 type_wanted,
983af33b
SDJ
13600 disposition, thread, task,
13601 ignore_count, ops, from_tty,
44f238bb 13602 enabled, internal, flags);
983af33b
SDJ
13603}
13604
13605static void
f00aae0f
KS
13606tracepoint_decode_location (struct breakpoint *b,
13607 const struct event_location *location,
c2f4122d 13608 struct program_space *search_pspace,
983af33b
SDJ
13609 struct symtabs_and_lines *sals)
13610{
c2f4122d 13611 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13612}
13613
2060206e 13614struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13615
55aa24fb
SDJ
13616/* The breakpoint_ops structure to be use on tracepoints placed in a
13617 static probe. */
13618
13619static void
f00aae0f
KS
13620tracepoint_probe_create_sals_from_location
13621 (const struct event_location *location,
13622 struct linespec_result *canonical,
13623 enum bptype type_wanted)
55aa24fb
SDJ
13624{
13625 /* We use the same method for breakpoint on probes. */
f00aae0f 13626 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13627}
13628
13629static void
f00aae0f
KS
13630tracepoint_probe_decode_location (struct breakpoint *b,
13631 const struct event_location *location,
c2f4122d 13632 struct program_space *search_pspace,
55aa24fb
SDJ
13633 struct symtabs_and_lines *sals)
13634{
13635 /* We use the same method for breakpoint on probes. */
c2f4122d 13636 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13637}
13638
13639static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13640
5c2b4418
HZ
13641/* Dprintf breakpoint_ops methods. */
13642
13643static void
13644dprintf_re_set (struct breakpoint *b)
13645{
13646 breakpoint_re_set_default (b);
13647
f00aae0f
KS
13648 /* extra_string should never be non-NULL for dprintf. */
13649 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13650
13651 /* 1 - connect to target 1, that can run breakpoint commands.
13652 2 - create a dprintf, which resolves fine.
13653 3 - disconnect from target 1
13654 4 - connect to target 2, that can NOT run breakpoint commands.
13655
13656 After steps #3/#4, you'll want the dprintf command list to
13657 be updated, because target 1 and 2 may well return different
13658 answers for target_can_run_breakpoint_commands().
13659 Given absence of finer grained resetting, we get to do
13660 it all the time. */
13661 if (b->extra_string != NULL)
13662 update_dprintf_command_list (b);
13663}
13664
2d9442cc
HZ
13665/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13666
13667static void
13668dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13669{
f00aae0f
KS
13670 fprintf_unfiltered (fp, "dprintf %s,%s",
13671 event_location_to_string (tp->location),
2d9442cc
HZ
13672 tp->extra_string);
13673 print_recreate_thread (tp, fp);
13674}
13675
9d6e6e84
HZ
13676/* Implement the "after_condition_true" breakpoint_ops method for
13677 dprintf.
13678
13679 dprintf's are implemented with regular commands in their command
13680 list, but we run the commands here instead of before presenting the
13681 stop to the user, as dprintf's don't actually cause a stop. This
13682 also makes it so that the commands of multiple dprintfs at the same
13683 address are all handled. */
13684
13685static void
13686dprintf_after_condition_true (struct bpstats *bs)
13687{
13688 struct cleanup *old_chain;
13689 struct bpstats tmp_bs = { NULL };
13690 struct bpstats *tmp_bs_p = &tmp_bs;
13691
13692 /* dprintf's never cause a stop. This wasn't set in the
13693 check_status hook instead because that would make the dprintf's
13694 condition not be evaluated. */
13695 bs->stop = 0;
13696
13697 /* Run the command list here. Take ownership of it instead of
13698 copying. We never want these commands to run later in
13699 bpstat_do_actions, if a breakpoint that causes a stop happens to
13700 be set at same address as this dprintf, or even if running the
13701 commands here throws. */
13702 tmp_bs.commands = bs->commands;
13703 bs->commands = NULL;
13704 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13705
13706 bpstat_do_actions_1 (&tmp_bs_p);
13707
13708 /* 'tmp_bs.commands' will usually be NULL by now, but
13709 bpstat_do_actions_1 may return early without processing the whole
13710 list. */
13711 do_cleanups (old_chain);
13712}
13713
983af33b
SDJ
13714/* The breakpoint_ops structure to be used on static tracepoints with
13715 markers (`-m'). */
13716
13717static void
f00aae0f 13718strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13719 struct linespec_result *canonical,
f00aae0f 13720 enum bptype type_wanted)
983af33b
SDJ
13721{
13722 struct linespec_sals lsal;
f00aae0f
KS
13723 const char *arg_start, *arg;
13724 char *str;
13725 struct cleanup *cleanup;
983af33b 13726
f00aae0f
KS
13727 arg = arg_start = get_linespec_location (location);
13728 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13729
f00aae0f
KS
13730 str = savestring (arg_start, arg - arg_start);
13731 cleanup = make_cleanup (xfree, str);
13732 canonical->location = new_linespec_location (&str);
13733 do_cleanups (cleanup);
983af33b 13734
f00aae0f 13735 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13736 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13737}
13738
13739static void
13740strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13741 struct linespec_result *canonical,
983af33b 13742 char *cond_string,
e7e0cddf 13743 char *extra_string,
983af33b
SDJ
13744 enum bptype type_wanted,
13745 enum bpdisp disposition,
13746 int thread,
13747 int task, int ignore_count,
13748 const struct breakpoint_ops *ops,
13749 int from_tty, int enabled,
44f238bb 13750 int internal, unsigned flags)
983af33b
SDJ
13751{
13752 int i;
52d361e1
YQ
13753 struct linespec_sals *lsal = VEC_index (linespec_sals,
13754 canonical->sals, 0);
983af33b
SDJ
13755
13756 /* If the user is creating a static tracepoint by marker id
13757 (strace -m MARKER_ID), then store the sals index, so that
13758 breakpoint_re_set can try to match up which of the newly
13759 found markers corresponds to this one, and, don't try to
13760 expand multiple locations for each sal, given than SALS
13761 already should contain all sals for MARKER_ID. */
13762
13763 for (i = 0; i < lsal->sals.nelts; ++i)
13764 {
13765 struct symtabs_and_lines expanded;
13766 struct tracepoint *tp;
13767 struct cleanup *old_chain;
f00aae0f 13768 struct event_location *location;
983af33b
SDJ
13769
13770 expanded.nelts = 1;
13771 expanded.sals = &lsal->sals.sals[i];
13772
f00aae0f
KS
13773 location = copy_event_location (canonical->location);
13774 old_chain = make_cleanup_delete_event_location (location);
983af33b 13775
4d01a485 13776 tp = new tracepoint ();
983af33b 13777 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13778 location, NULL,
e7e0cddf
SS
13779 cond_string, extra_string,
13780 type_wanted, disposition,
983af33b 13781 thread, task, ignore_count, ops,
44f238bb 13782 from_tty, enabled, internal, flags,
983af33b
SDJ
13783 canonical->special_display);
13784 /* Given that its possible to have multiple markers with
13785 the same string id, if the user is creating a static
13786 tracepoint by marker id ("strace -m MARKER_ID"), then
13787 store the sals index, so that breakpoint_re_set can
13788 try to match up which of the newly found markers
13789 corresponds to this one */
13790 tp->static_trace_marker_id_idx = i;
13791
13792 install_breakpoint (internal, &tp->base, 0);
13793
13794 discard_cleanups (old_chain);
13795 }
13796}
13797
13798static void
f00aae0f
KS
13799strace_marker_decode_location (struct breakpoint *b,
13800 const struct event_location *location,
c2f4122d 13801 struct program_space *search_pspace,
983af33b
SDJ
13802 struct symtabs_and_lines *sals)
13803{
13804 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13805 const char *s = get_linespec_location (location);
983af33b 13806
f00aae0f 13807 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13808 if (sals->nelts > tp->static_trace_marker_id_idx)
13809 {
13810 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13811 sals->nelts = 1;
13812 }
13813 else
13814 error (_("marker %s not found"), tp->static_trace_marker_id);
13815}
13816
13817static struct breakpoint_ops strace_marker_breakpoint_ops;
13818
13819static int
13820strace_marker_p (struct breakpoint *b)
13821{
13822 return b->ops == &strace_marker_breakpoint_ops;
13823}
13824
53a5351d 13825/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13826 structures. */
c906108c
SS
13827
13828void
fba45db2 13829delete_breakpoint (struct breakpoint *bpt)
c906108c 13830{
52f0bd74 13831 struct breakpoint *b;
c906108c 13832
8a3fe4f8 13833 gdb_assert (bpt != NULL);
c906108c 13834
4a64f543
MS
13835 /* Has this bp already been deleted? This can happen because
13836 multiple lists can hold pointers to bp's. bpstat lists are
13837 especial culprits.
13838
13839 One example of this happening is a watchpoint's scope bp. When
13840 the scope bp triggers, we notice that the watchpoint is out of
13841 scope, and delete it. We also delete its scope bp. But the
13842 scope bp is marked "auto-deleting", and is already on a bpstat.
13843 That bpstat is then checked for auto-deleting bp's, which are
13844 deleted.
13845
13846 A real solution to this problem might involve reference counts in
13847 bp's, and/or giving them pointers back to their referencing
13848 bpstat's, and teaching delete_breakpoint to only free a bp's
13849 storage when no more references were extent. A cheaper bandaid
13850 was chosen. */
c906108c
SS
13851 if (bpt->type == bp_none)
13852 return;
13853
4a64f543
MS
13854 /* At least avoid this stale reference until the reference counting
13855 of breakpoints gets resolved. */
d0fb5eae 13856 if (bpt->related_breakpoint != bpt)
e5a0a904 13857 {
d0fb5eae 13858 struct breakpoint *related;
3a5c3e22 13859 struct watchpoint *w;
d0fb5eae
JK
13860
13861 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13862 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13863 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13864 w = (struct watchpoint *) bpt;
13865 else
13866 w = NULL;
13867 if (w != NULL)
13868 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13869
13870 /* Unlink bpt from the bpt->related_breakpoint ring. */
13871 for (related = bpt; related->related_breakpoint != bpt;
13872 related = related->related_breakpoint);
13873 related->related_breakpoint = bpt->related_breakpoint;
13874 bpt->related_breakpoint = bpt;
e5a0a904
JK
13875 }
13876
a9634178
TJB
13877 /* watch_command_1 creates a watchpoint but only sets its number if
13878 update_watchpoint succeeds in creating its bp_locations. If there's
13879 a problem in that process, we'll be asked to delete the half-created
13880 watchpoint. In that case, don't announce the deletion. */
13881 if (bpt->number)
13882 observer_notify_breakpoint_deleted (bpt);
c906108c 13883
c906108c
SS
13884 if (breakpoint_chain == bpt)
13885 breakpoint_chain = bpt->next;
13886
c906108c
SS
13887 ALL_BREAKPOINTS (b)
13888 if (b->next == bpt)
c5aa993b
JM
13889 {
13890 b->next = bpt->next;
13891 break;
13892 }
c906108c 13893
f431efe5
PA
13894 /* Be sure no bpstat's are pointing at the breakpoint after it's
13895 been freed. */
13896 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13897 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13898 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13899 commands are associated with the bpstat; if we remove it here,
13900 then the later call to bpstat_do_actions (&stop_bpstat); in
13901 event-top.c won't do anything, and temporary breakpoints with
13902 commands won't work. */
13903
13904 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13905
4a64f543
MS
13906 /* Now that breakpoint is removed from breakpoint list, update the
13907 global location list. This will remove locations that used to
13908 belong to this breakpoint. Do this before freeing the breakpoint
13909 itself, since remove_breakpoint looks at location's owner. It
13910 might be better design to have location completely
13911 self-contained, but it's not the case now. */
44702360 13912 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13913
348d480f 13914 bpt->ops->dtor (bpt);
4a64f543
MS
13915 /* On the chance that someone will soon try again to delete this
13916 same bp, we mark it as deleted before freeing its storage. */
c906108c 13917 bpt->type = bp_none;
4d01a485 13918 delete bpt;
c906108c
SS
13919}
13920
4d6140d9
AC
13921static void
13922do_delete_breakpoint_cleanup (void *b)
13923{
9a3c8263 13924 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13925}
13926
13927struct cleanup *
13928make_cleanup_delete_breakpoint (struct breakpoint *b)
13929{
13930 return make_cleanup (do_delete_breakpoint_cleanup, b);
13931}
13932
51be5b68
PA
13933/* Iterator function to call a user-provided callback function once
13934 for each of B and its related breakpoints. */
13935
13936static void
13937iterate_over_related_breakpoints (struct breakpoint *b,
13938 void (*function) (struct breakpoint *,
13939 void *),
13940 void *data)
13941{
13942 struct breakpoint *related;
13943
13944 related = b;
13945 do
13946 {
13947 struct breakpoint *next;
13948
13949 /* FUNCTION may delete RELATED. */
13950 next = related->related_breakpoint;
13951
13952 if (next == related)
13953 {
13954 /* RELATED is the last ring entry. */
13955 function (related, data);
13956
13957 /* FUNCTION may have deleted it, so we'd never reach back to
13958 B. There's nothing left to do anyway, so just break
13959 out. */
13960 break;
13961 }
13962 else
13963 function (related, data);
13964
13965 related = next;
13966 }
13967 while (related != b);
13968}
95a42b64
TT
13969
13970static void
13971do_delete_breakpoint (struct breakpoint *b, void *ignore)
13972{
13973 delete_breakpoint (b);
13974}
13975
51be5b68
PA
13976/* A callback for map_breakpoint_numbers that calls
13977 delete_breakpoint. */
13978
13979static void
13980do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13981{
13982 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13983}
13984
c906108c 13985void
fba45db2 13986delete_command (char *arg, int from_tty)
c906108c 13987{
35df4500 13988 struct breakpoint *b, *b_tmp;
c906108c 13989
ea9365bb
TT
13990 dont_repeat ();
13991
c906108c
SS
13992 if (arg == 0)
13993 {
13994 int breaks_to_delete = 0;
13995
46c6471b
PA
13996 /* Delete all breakpoints if no argument. Do not delete
13997 internal breakpoints, these have to be deleted with an
13998 explicit breakpoint number argument. */
c5aa993b 13999 ALL_BREAKPOINTS (b)
46c6471b 14000 if (user_breakpoint_p (b))
973d738b
DJ
14001 {
14002 breaks_to_delete = 1;
14003 break;
14004 }
c906108c
SS
14005
14006 /* Ask user only if there are some breakpoints to delete. */
14007 if (!from_tty
e2e0b3e5 14008 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14009 {
35df4500 14010 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14011 if (user_breakpoint_p (b))
c5aa993b 14012 delete_breakpoint (b);
c906108c
SS
14013 }
14014 }
14015 else
51be5b68 14016 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14017}
14018
c2f4122d
PA
14019/* Return true if all locations of B bound to PSPACE are pending. If
14020 PSPACE is NULL, all locations of all program spaces are
14021 considered. */
14022
0d381245 14023static int
c2f4122d 14024all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 14025{
c2f4122d
PA
14026 struct bp_location *loc;
14027
14028 for (loc = b->loc; loc != NULL; loc = loc->next)
14029 if ((pspace == NULL
14030 || loc->pspace == pspace)
14031 && !loc->shlib_disabled
8645ff69 14032 && !loc->pspace->executing_startup)
0d381245
VP
14033 return 0;
14034 return 1;
fe3f5fa8
VP
14035}
14036
776592bf
DE
14037/* Subroutine of update_breakpoint_locations to simplify it.
14038 Return non-zero if multiple fns in list LOC have the same name.
14039 Null names are ignored. */
14040
14041static int
14042ambiguous_names_p (struct bp_location *loc)
14043{
14044 struct bp_location *l;
14045 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14046 (int (*) (const void *,
14047 const void *)) streq,
776592bf
DE
14048 NULL, xcalloc, xfree);
14049
14050 for (l = loc; l != NULL; l = l->next)
14051 {
14052 const char **slot;
14053 const char *name = l->function_name;
14054
14055 /* Allow for some names to be NULL, ignore them. */
14056 if (name == NULL)
14057 continue;
14058
14059 slot = (const char **) htab_find_slot (htab, (const void *) name,
14060 INSERT);
4a64f543
MS
14061 /* NOTE: We can assume slot != NULL here because xcalloc never
14062 returns NULL. */
776592bf
DE
14063 if (*slot != NULL)
14064 {
14065 htab_delete (htab);
14066 return 1;
14067 }
14068 *slot = name;
14069 }
14070
14071 htab_delete (htab);
14072 return 0;
14073}
14074
0fb4aa4b
PA
14075/* When symbols change, it probably means the sources changed as well,
14076 and it might mean the static tracepoint markers are no longer at
14077 the same address or line numbers they used to be at last we
14078 checked. Losing your static tracepoints whenever you rebuild is
14079 undesirable. This function tries to resync/rematch gdb static
14080 tracepoints with the markers on the target, for static tracepoints
14081 that have not been set by marker id. Static tracepoint that have
14082 been set by marker id are reset by marker id in breakpoint_re_set.
14083 The heuristic is:
14084
14085 1) For a tracepoint set at a specific address, look for a marker at
14086 the old PC. If one is found there, assume to be the same marker.
14087 If the name / string id of the marker found is different from the
14088 previous known name, assume that means the user renamed the marker
14089 in the sources, and output a warning.
14090
14091 2) For a tracepoint set at a given line number, look for a marker
14092 at the new address of the old line number. If one is found there,
14093 assume to be the same marker. If the name / string id of the
14094 marker found is different from the previous known name, assume that
14095 means the user renamed the marker in the sources, and output a
14096 warning.
14097
14098 3) If a marker is no longer found at the same address or line, it
14099 may mean the marker no longer exists. But it may also just mean
14100 the code changed a bit. Maybe the user added a few lines of code
14101 that made the marker move up or down (in line number terms). Ask
14102 the target for info about the marker with the string id as we knew
14103 it. If found, update line number and address in the matching
14104 static tracepoint. This will get confused if there's more than one
14105 marker with the same ID (possible in UST, although unadvised
14106 precisely because it confuses tools). */
14107
14108static struct symtab_and_line
14109update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14110{
d9b3f62e 14111 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14112 struct static_tracepoint_marker marker;
14113 CORE_ADDR pc;
0fb4aa4b
PA
14114
14115 pc = sal.pc;
14116 if (sal.line)
14117 find_line_pc (sal.symtab, sal.line, &pc);
14118
14119 if (target_static_tracepoint_marker_at (pc, &marker))
14120 {
d9b3f62e 14121 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14122 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14123 b->number,
d9b3f62e 14124 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14125
d9b3f62e
PA
14126 xfree (tp->static_trace_marker_id);
14127 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14128 release_static_tracepoint_marker (&marker);
14129
14130 return sal;
14131 }
14132
14133 /* Old marker wasn't found on target at lineno. Try looking it up
14134 by string ID. */
14135 if (!sal.explicit_pc
14136 && sal.line != 0
14137 && sal.symtab != NULL
d9b3f62e 14138 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14139 {
14140 VEC(static_tracepoint_marker_p) *markers;
14141
14142 markers
d9b3f62e 14143 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14144
14145 if (!VEC_empty(static_tracepoint_marker_p, markers))
14146 {
80e1d417 14147 struct symtab_and_line sal2;
0fb4aa4b 14148 struct symbol *sym;
80e1d417 14149 struct static_tracepoint_marker *tpmarker;
79a45e25 14150 struct ui_out *uiout = current_uiout;
67994074 14151 struct explicit_location explicit_loc;
0fb4aa4b 14152
80e1d417 14153 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14154
d9b3f62e 14155 xfree (tp->static_trace_marker_id);
80e1d417 14156 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14157
14158 warning (_("marker for static tracepoint %d (%s) not "
14159 "found at previous line number"),
d9b3f62e 14160 b->number, tp->static_trace_marker_id);
0fb4aa4b 14161
80e1d417 14162 init_sal (&sal2);
0fb4aa4b 14163
80e1d417 14164 sal2.pc = tpmarker->address;
0fb4aa4b 14165
80e1d417
AS
14166 sal2 = find_pc_line (tpmarker->address, 0);
14167 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14168 ui_out_text (uiout, "Now in ");
14169 if (sym)
14170 {
14171 ui_out_field_string (uiout, "func",
14172 SYMBOL_PRINT_NAME (sym));
14173 ui_out_text (uiout, " at ");
14174 }
05cba821
JK
14175 ui_out_field_string (uiout, "file",
14176 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14177 ui_out_text (uiout, ":");
14178
14179 if (ui_out_is_mi_like_p (uiout))
14180 {
0b0865da 14181 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14182
f35a17b5 14183 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14184 }
14185
80e1d417 14186 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14187 ui_out_text (uiout, "\n");
14188
80e1d417 14189 b->loc->line_number = sal2.line;
2f202fde 14190 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14191
f00aae0f 14192 delete_event_location (b->location);
67994074
KS
14193 initialize_explicit_location (&explicit_loc);
14194 explicit_loc.source_filename
00e52e53 14195 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14196 explicit_loc.line_offset.offset = b->loc->line_number;
14197 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14198 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14199
14200 /* Might be nice to check if function changed, and warn if
14201 so. */
14202
80e1d417 14203 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14204 }
14205 }
14206 return sal;
14207}
14208
8d3788bd
VP
14209/* Returns 1 iff locations A and B are sufficiently same that
14210 we don't need to report breakpoint as changed. */
14211
14212static int
14213locations_are_equal (struct bp_location *a, struct bp_location *b)
14214{
14215 while (a && b)
14216 {
14217 if (a->address != b->address)
14218 return 0;
14219
14220 if (a->shlib_disabled != b->shlib_disabled)
14221 return 0;
14222
14223 if (a->enabled != b->enabled)
14224 return 0;
14225
14226 a = a->next;
14227 b = b->next;
14228 }
14229
14230 if ((a == NULL) != (b == NULL))
14231 return 0;
14232
14233 return 1;
14234}
14235
c2f4122d
PA
14236/* Split all locations of B that are bound to PSPACE out of B's
14237 location list to a separate list and return that list's head. If
14238 PSPACE is NULL, hoist out all locations of B. */
14239
14240static struct bp_location *
14241hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14242{
14243 struct bp_location head;
14244 struct bp_location *i = b->loc;
14245 struct bp_location **i_link = &b->loc;
14246 struct bp_location *hoisted = &head;
14247
14248 if (pspace == NULL)
14249 {
14250 i = b->loc;
14251 b->loc = NULL;
14252 return i;
14253 }
14254
14255 head.next = NULL;
14256
14257 while (i != NULL)
14258 {
14259 if (i->pspace == pspace)
14260 {
14261 *i_link = i->next;
14262 i->next = NULL;
14263 hoisted->next = i;
14264 hoisted = i;
14265 }
14266 else
14267 i_link = &i->next;
14268 i = *i_link;
14269 }
14270
14271 return head.next;
14272}
14273
14274/* Create new breakpoint locations for B (a hardware or software
14275 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14276 zero, then B is a ranged breakpoint. Only recreates locations for
14277 FILTER_PSPACE. Locations of other program spaces are left
14278 untouched. */
f1310107 14279
0e30163f 14280void
0d381245 14281update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14282 struct program_space *filter_pspace,
f1310107
TJB
14283 struct symtabs_and_lines sals,
14284 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14285{
14286 int i;
c2f4122d 14287 struct bp_location *existing_locations;
0d381245 14288
f8eba3c6
TT
14289 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14290 {
14291 /* Ranged breakpoints have only one start location and one end
14292 location. */
14293 b->enable_state = bp_disabled;
f8eba3c6
TT
14294 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14295 "multiple locations found\n"),
14296 b->number);
14297 return;
14298 }
f1310107 14299
4a64f543
MS
14300 /* If there's no new locations, and all existing locations are
14301 pending, don't do anything. This optimizes the common case where
14302 all locations are in the same shared library, that was unloaded.
14303 We'd like to retain the location, so that when the library is
14304 loaded again, we don't loose the enabled/disabled status of the
14305 individual locations. */
c2f4122d 14306 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14307 return;
14308
c2f4122d 14309 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14310
0d381245 14311 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14312 {
f8eba3c6
TT
14313 struct bp_location *new_loc;
14314
14315 switch_to_program_space_and_thread (sals.sals[i].pspace);
14316
14317 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14318
0d381245
VP
14319 /* Reparse conditions, they might contain references to the
14320 old symtab. */
14321 if (b->cond_string != NULL)
14322 {
bbc13ae3 14323 const char *s;
fe3f5fa8 14324
0d381245 14325 s = b->cond_string;
492d29ea 14326 TRY
0d381245 14327 {
1bb9788d
TT
14328 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14329 block_for_pc (sals.sals[i].pc),
0d381245
VP
14330 0);
14331 }
492d29ea 14332 CATCH (e, RETURN_MASK_ERROR)
0d381245 14333 {
3e43a32a
MS
14334 warning (_("failed to reevaluate condition "
14335 "for breakpoint %d: %s"),
0d381245
VP
14336 b->number, e.message);
14337 new_loc->enabled = 0;
14338 }
492d29ea 14339 END_CATCH
0d381245 14340 }
fe3f5fa8 14341
f1310107
TJB
14342 if (sals_end.nelts)
14343 {
14344 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14345
14346 new_loc->length = end - sals.sals[0].pc + 1;
14347 }
0d381245 14348 }
fe3f5fa8 14349
4a64f543
MS
14350 /* If possible, carry over 'disable' status from existing
14351 breakpoints. */
0d381245
VP
14352 {
14353 struct bp_location *e = existing_locations;
776592bf
DE
14354 /* If there are multiple breakpoints with the same function name,
14355 e.g. for inline functions, comparing function names won't work.
14356 Instead compare pc addresses; this is just a heuristic as things
14357 may have moved, but in practice it gives the correct answer
14358 often enough until a better solution is found. */
14359 int have_ambiguous_names = ambiguous_names_p (b->loc);
14360
0d381245
VP
14361 for (; e; e = e->next)
14362 {
14363 if (!e->enabled && e->function_name)
14364 {
14365 struct bp_location *l = b->loc;
776592bf
DE
14366 if (have_ambiguous_names)
14367 {
14368 for (; l; l = l->next)
f1310107 14369 if (breakpoint_locations_match (e, l))
776592bf
DE
14370 {
14371 l->enabled = 0;
14372 break;
14373 }
14374 }
14375 else
14376 {
14377 for (; l; l = l->next)
14378 if (l->function_name
14379 && strcmp (e->function_name, l->function_name) == 0)
14380 {
14381 l->enabled = 0;
14382 break;
14383 }
14384 }
0d381245
VP
14385 }
14386 }
14387 }
fe3f5fa8 14388
8d3788bd
VP
14389 if (!locations_are_equal (existing_locations, b->loc))
14390 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14391}
14392
f00aae0f 14393/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14394 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14395
14396static struct symtabs_and_lines
f00aae0f 14397location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14398 struct program_space *search_pspace, int *found)
ef23e705 14399{
02d20e4a 14400 struct symtabs_and_lines sals = {0};
492d29ea 14401 struct gdb_exception exception = exception_none;
ef23e705 14402
983af33b 14403 gdb_assert (b->ops != NULL);
ef23e705 14404
492d29ea 14405 TRY
ef23e705 14406 {
c2f4122d 14407 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14408 }
492d29ea 14409 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14410 {
14411 int not_found_and_ok = 0;
492d29ea
PA
14412
14413 exception = e;
14414
ef23e705
TJB
14415 /* For pending breakpoints, it's expected that parsing will
14416 fail until the right shared library is loaded. User has
14417 already told to create pending breakpoints and don't need
14418 extra messages. If breakpoint is in bp_shlib_disabled
14419 state, then user already saw the message about that
14420 breakpoint being disabled, and don't want to see more
14421 errors. */
58438ac1 14422 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14423 && (b->condition_not_parsed
14424 || (b->loc != NULL
14425 && search_pspace != NULL
14426 && b->loc->pspace != search_pspace)
ef23e705 14427 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14428 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14429 || b->enable_state == bp_disabled))
14430 not_found_and_ok = 1;
14431
14432 if (!not_found_and_ok)
14433 {
14434 /* We surely don't want to warn about the same breakpoint
14435 10 times. One solution, implemented here, is disable
14436 the breakpoint on error. Another solution would be to
14437 have separate 'warning emitted' flag. Since this
14438 happens only when a binary has changed, I don't know
14439 which approach is better. */
14440 b->enable_state = bp_disabled;
14441 throw_exception (e);
14442 }
14443 }
492d29ea 14444 END_CATCH
ef23e705 14445
492d29ea 14446 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14447 {
f8eba3c6 14448 int i;
ef23e705 14449
f8eba3c6
TT
14450 for (i = 0; i < sals.nelts; ++i)
14451 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14452 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14453 {
ed1d1739
KS
14454 char *cond_string, *extra_string;
14455 int thread, task;
ef23e705 14456
f00aae0f 14457 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14458 &cond_string, &thread, &task,
14459 &extra_string);
f00aae0f 14460 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14461 if (cond_string)
14462 b->cond_string = cond_string;
14463 b->thread = thread;
14464 b->task = task;
e7e0cddf 14465 if (extra_string)
f00aae0f
KS
14466 {
14467 xfree (b->extra_string);
14468 b->extra_string = extra_string;
14469 }
ef23e705
TJB
14470 b->condition_not_parsed = 0;
14471 }
14472
983af33b 14473 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14474 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14475
58438ac1
TT
14476 *found = 1;
14477 }
14478 else
14479 *found = 0;
ef23e705
TJB
14480
14481 return sals;
14482}
14483
348d480f
PA
14484/* The default re_set method, for typical hardware or software
14485 breakpoints. Reevaluate the breakpoint and recreate its
14486 locations. */
14487
14488static void
28010a5d 14489breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14490{
14491 int found;
f1310107 14492 struct symtabs_and_lines sals, sals_end;
ef23e705 14493 struct symtabs_and_lines expanded = {0};
f1310107 14494 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14495 struct program_space *filter_pspace = current_program_space;
ef23e705 14496
c2f4122d 14497 sals = location_to_sals (b, b->location, filter_pspace, &found);
ef23e705
TJB
14498 if (found)
14499 {
14500 make_cleanup (xfree, sals.sals);
f8eba3c6 14501 expanded = sals;
ef23e705
TJB
14502 }
14503
f00aae0f 14504 if (b->location_range_end != NULL)
f1310107 14505 {
c2f4122d
PA
14506 sals_end = location_to_sals (b, b->location_range_end,
14507 filter_pspace, &found);
f1310107
TJB
14508 if (found)
14509 {
14510 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14511 expanded_end = sals_end;
f1310107
TJB
14512 }
14513 }
14514
c2f4122d 14515 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14516}
14517
983af33b
SDJ
14518/* Default method for creating SALs from an address string. It basically
14519 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14520
14521static void
f00aae0f
KS
14522create_sals_from_location_default (const struct event_location *location,
14523 struct linespec_result *canonical,
14524 enum bptype type_wanted)
983af33b 14525{
f00aae0f 14526 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14527}
14528
14529/* Call create_breakpoints_sal for the given arguments. This is the default
14530 function for the `create_breakpoints_sal' method of
14531 breakpoint_ops. */
14532
14533static void
14534create_breakpoints_sal_default (struct gdbarch *gdbarch,
14535 struct linespec_result *canonical,
983af33b 14536 char *cond_string,
e7e0cddf 14537 char *extra_string,
983af33b
SDJ
14538 enum bptype type_wanted,
14539 enum bpdisp disposition,
14540 int thread,
14541 int task, int ignore_count,
14542 const struct breakpoint_ops *ops,
14543 int from_tty, int enabled,
44f238bb 14544 int internal, unsigned flags)
983af33b
SDJ
14545{
14546 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14547 extra_string,
983af33b
SDJ
14548 type_wanted, disposition,
14549 thread, task, ignore_count, ops, from_tty,
44f238bb 14550 enabled, internal, flags);
983af33b
SDJ
14551}
14552
14553/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14554 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14555
14556static void
f00aae0f
KS
14557decode_location_default (struct breakpoint *b,
14558 const struct event_location *location,
c2f4122d 14559 struct program_space *search_pspace,
983af33b
SDJ
14560 struct symtabs_and_lines *sals)
14561{
14562 struct linespec_result canonical;
14563
14564 init_linespec_result (&canonical);
c2f4122d 14565 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14566 (struct symtab *) NULL, 0,
14567 &canonical, multiple_symbols_all,
14568 b->filter);
14569
14570 /* We should get 0 or 1 resulting SALs. */
14571 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14572
14573 if (VEC_length (linespec_sals, canonical.sals) > 0)
14574 {
14575 struct linespec_sals *lsal;
14576
14577 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14578 *sals = lsal->sals;
14579 /* Arrange it so the destructor does not free the
14580 contents. */
14581 lsal->sals.sals = NULL;
14582 }
14583
14584 destroy_linespec_result (&canonical);
14585}
14586
28010a5d
PA
14587/* Prepare the global context for a re-set of breakpoint B. */
14588
14589static struct cleanup *
14590prepare_re_set_context (struct breakpoint *b)
14591{
28010a5d 14592 input_radix = b->input_radix;
28010a5d
PA
14593 set_language (b->language);
14594
c2f4122d 14595 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14596}
14597
c906108c
SS
14598/* Reset a breakpoint given it's struct breakpoint * BINT.
14599 The value we return ends up being the return value from catch_errors.
14600 Unused in this case. */
14601
14602static int
4efb68b1 14603breakpoint_re_set_one (void *bint)
c906108c 14604{
4a64f543 14605 /* Get past catch_errs. */
53a5351d 14606 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14607 struct cleanup *cleanups;
c906108c 14608
348d480f
PA
14609 cleanups = prepare_re_set_context (b);
14610 b->ops->re_set (b);
14611 do_cleanups (cleanups);
c906108c
SS
14612 return 0;
14613}
14614
c2f4122d
PA
14615/* Re-set breakpoint locations for the current program space.
14616 Locations bound to other program spaces are left untouched. */
14617
c906108c 14618void
69de3c6a 14619breakpoint_re_set (void)
c906108c 14620{
35df4500 14621 struct breakpoint *b, *b_tmp;
c906108c
SS
14622 enum language save_language;
14623 int save_input_radix;
6c95b8df 14624 struct cleanup *old_chain;
c5aa993b 14625
c906108c
SS
14626 save_language = current_language->la_language;
14627 save_input_radix = input_radix;
c2f4122d 14628 old_chain = save_current_space_and_thread ();
6c95b8df 14629
2a7f3dff
PA
14630 /* Note: we must not try to insert locations until after all
14631 breakpoints have been re-set. Otherwise, e.g., when re-setting
14632 breakpoint 1, we'd insert the locations of breakpoint 2, which
14633 hadn't been re-set yet, and thus may have stale locations. */
14634
35df4500 14635 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14636 {
4a64f543 14637 /* Format possible error msg. */
fe3f5fa8 14638 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14639 b->number);
14640 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14641 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14642 do_cleanups (cleanups);
c5aa993b 14643 }
c906108c
SS
14644 set_language (save_language);
14645 input_radix = save_input_radix;
e62c965a 14646
0756c555 14647 jit_breakpoint_re_set ();
4efc6507 14648
6c95b8df
PA
14649 do_cleanups (old_chain);
14650
af02033e
PP
14651 create_overlay_event_breakpoint ();
14652 create_longjmp_master_breakpoint ();
14653 create_std_terminate_master_breakpoint ();
186c406b 14654 create_exception_master_breakpoint ();
2a7f3dff
PA
14655
14656 /* Now we can insert. */
14657 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14658}
14659\f
c906108c
SS
14660/* Reset the thread number of this breakpoint:
14661
14662 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14663 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14664void
fba45db2 14665breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14666{
14667 if (b->thread != -1)
14668 {
39f77062 14669 if (in_thread_list (inferior_ptid))
5d5658a1 14670 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14671
14672 /* We're being called after following a fork. The new fork is
14673 selected as current, and unless this was a vfork will have a
14674 different program space from the original thread. Reset that
14675 as well. */
14676 b->loc->pspace = current_program_space;
c906108c
SS
14677 }
14678}
14679
03ac34d5
MS
14680/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14681 If from_tty is nonzero, it prints a message to that effect,
14682 which ends with a period (no newline). */
14683
c906108c 14684void
fba45db2 14685set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14686{
52f0bd74 14687 struct breakpoint *b;
c906108c
SS
14688
14689 if (count < 0)
14690 count = 0;
14691
14692 ALL_BREAKPOINTS (b)
14693 if (b->number == bptnum)
c5aa993b 14694 {
d77f58be
SS
14695 if (is_tracepoint (b))
14696 {
14697 if (from_tty && count != 0)
14698 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14699 bptnum);
14700 return;
14701 }
14702
c5aa993b 14703 b->ignore_count = count;
221ea385
KS
14704 if (from_tty)
14705 {
14706 if (count == 0)
3e43a32a
MS
14707 printf_filtered (_("Will stop next time "
14708 "breakpoint %d is reached."),
221ea385
KS
14709 bptnum);
14710 else if (count == 1)
a3f17187 14711 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14712 bptnum);
14713 else
3e43a32a
MS
14714 printf_filtered (_("Will ignore next %d "
14715 "crossings of breakpoint %d."),
221ea385
KS
14716 count, bptnum);
14717 }
8d3788bd 14718 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14719 return;
14720 }
c906108c 14721
8a3fe4f8 14722 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14723}
14724
c906108c
SS
14725/* Command to set ignore-count of breakpoint N to COUNT. */
14726
14727static void
fba45db2 14728ignore_command (char *args, int from_tty)
c906108c
SS
14729{
14730 char *p = args;
52f0bd74 14731 int num;
c906108c
SS
14732
14733 if (p == 0)
e2e0b3e5 14734 error_no_arg (_("a breakpoint number"));
c5aa993b 14735
c906108c 14736 num = get_number (&p);
5c44784c 14737 if (num == 0)
8a3fe4f8 14738 error (_("bad breakpoint number: '%s'"), args);
c906108c 14739 if (*p == 0)
8a3fe4f8 14740 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14741
14742 set_ignore_count (num,
14743 longest_to_int (value_as_long (parse_and_eval (p))),
14744 from_tty);
221ea385
KS
14745 if (from_tty)
14746 printf_filtered ("\n");
c906108c
SS
14747}
14748\f
14749/* Call FUNCTION on each of the breakpoints
14750 whose numbers are given in ARGS. */
14751
14752static void
896b6bda
PA
14753map_breakpoint_numbers (const char *args,
14754 void (*function) (struct breakpoint *,
14755 void *),
95a42b64 14756 void *data)
c906108c 14757{
52f0bd74
AC
14758 int num;
14759 struct breakpoint *b, *tmp;
c906108c 14760
b9d61307 14761 if (args == 0 || *args == '\0')
e2e0b3e5 14762 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14763
bfd28288 14764 number_or_range_parser parser (args);
197f0a60 14765
bfd28288 14766 while (!parser.finished ())
c906108c 14767 {
bfd28288
PA
14768 const char *p = parser.cur_tok ();
14769 bool match = false;
197f0a60 14770
bfd28288 14771 num = parser.get_number ();
5c44784c 14772 if (num == 0)
c5aa993b 14773 {
8a3fe4f8 14774 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14775 }
14776 else
14777 {
14778 ALL_BREAKPOINTS_SAFE (b, tmp)
14779 if (b->number == num)
14780 {
bfd28288 14781 match = true;
cdac0397 14782 function (b, data);
11cf8741 14783 break;
5c44784c 14784 }
bfd28288 14785 if (!match)
a3f17187 14786 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14787 }
c906108c
SS
14788 }
14789}
14790
0d381245
VP
14791static struct bp_location *
14792find_location_by_number (char *number)
14793{
14794 char *dot = strchr (number, '.');
14795 char *p1;
14796 int bp_num;
14797 int loc_num;
14798 struct breakpoint *b;
14799 struct bp_location *loc;
14800
14801 *dot = '\0';
14802
14803 p1 = number;
197f0a60 14804 bp_num = get_number (&p1);
0d381245
VP
14805 if (bp_num == 0)
14806 error (_("Bad breakpoint number '%s'"), number);
14807
14808 ALL_BREAKPOINTS (b)
14809 if (b->number == bp_num)
14810 {
14811 break;
14812 }
14813
14814 if (!b || b->number != bp_num)
14815 error (_("Bad breakpoint number '%s'"), number);
14816
14817 p1 = dot+1;
197f0a60 14818 loc_num = get_number (&p1);
0d381245
VP
14819 if (loc_num == 0)
14820 error (_("Bad breakpoint location number '%s'"), number);
14821
14822 --loc_num;
14823 loc = b->loc;
14824 for (;loc_num && loc; --loc_num, loc = loc->next)
14825 ;
14826 if (!loc)
14827 error (_("Bad breakpoint location number '%s'"), dot+1);
14828
14829 return loc;
14830}
14831
14832
1900040c
MS
14833/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14834 If from_tty is nonzero, it prints a message to that effect,
14835 which ends with a period (no newline). */
14836
c906108c 14837void
fba45db2 14838disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14839{
14840 /* Never disable a watchpoint scope breakpoint; we want to
14841 hit them when we leave scope so we can delete both the
14842 watchpoint and its scope breakpoint at that time. */
14843 if (bpt->type == bp_watchpoint_scope)
14844 return;
14845
b5de0fa7 14846 bpt->enable_state = bp_disabled;
c906108c 14847
b775012e
LM
14848 /* Mark breakpoint locations modified. */
14849 mark_breakpoint_modified (bpt);
14850
d248b706
KY
14851 if (target_supports_enable_disable_tracepoint ()
14852 && current_trace_status ()->running && is_tracepoint (bpt))
14853 {
14854 struct bp_location *location;
14855
14856 for (location = bpt->loc; location; location = location->next)
14857 target_disable_tracepoint (location);
14858 }
14859
44702360 14860 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14861
8d3788bd 14862 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14863}
14864
51be5b68
PA
14865/* A callback for iterate_over_related_breakpoints. */
14866
14867static void
14868do_disable_breakpoint (struct breakpoint *b, void *ignore)
14869{
14870 disable_breakpoint (b);
14871}
14872
95a42b64
TT
14873/* A callback for map_breakpoint_numbers that calls
14874 disable_breakpoint. */
14875
14876static void
14877do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14878{
51be5b68 14879 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14880}
14881
c906108c 14882static void
fba45db2 14883disable_command (char *args, int from_tty)
c906108c 14884{
c906108c 14885 if (args == 0)
46c6471b
PA
14886 {
14887 struct breakpoint *bpt;
14888
14889 ALL_BREAKPOINTS (bpt)
14890 if (user_breakpoint_p (bpt))
14891 disable_breakpoint (bpt);
14892 }
9eaabc75 14893 else
0d381245 14894 {
9eaabc75
MW
14895 char *num = extract_arg (&args);
14896
14897 while (num)
d248b706 14898 {
9eaabc75 14899 if (strchr (num, '.'))
b775012e 14900 {
9eaabc75
MW
14901 struct bp_location *loc = find_location_by_number (num);
14902
14903 if (loc)
14904 {
14905 if (loc->enabled)
14906 {
14907 loc->enabled = 0;
14908 mark_breakpoint_location_modified (loc);
14909 }
14910 if (target_supports_enable_disable_tracepoint ()
14911 && current_trace_status ()->running && loc->owner
14912 && is_tracepoint (loc->owner))
14913 target_disable_tracepoint (loc);
14914 }
44702360 14915 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14916 }
9eaabc75
MW
14917 else
14918 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14919 num = extract_arg (&args);
d248b706 14920 }
0d381245 14921 }
c906108c
SS
14922}
14923
14924static void
816338b5
SS
14925enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14926 int count)
c906108c 14927{
afe38095 14928 int target_resources_ok;
c906108c
SS
14929
14930 if (bpt->type == bp_hardware_breakpoint)
14931 {
14932 int i;
c5aa993b 14933 i = hw_breakpoint_used_count ();
53a5351d 14934 target_resources_ok =
d92524f1 14935 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14936 i + 1, 0);
c906108c 14937 if (target_resources_ok == 0)
8a3fe4f8 14938 error (_("No hardware breakpoint support in the target."));
c906108c 14939 else if (target_resources_ok < 0)
8a3fe4f8 14940 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14941 }
14942
cc60f2e3 14943 if (is_watchpoint (bpt))
c906108c 14944 {
d07205c2 14945 /* Initialize it just to avoid a GCC false warning. */
f486487f 14946 enum enable_state orig_enable_state = bp_disabled;
dde02812 14947
492d29ea 14948 TRY
c906108c 14949 {
3a5c3e22
PA
14950 struct watchpoint *w = (struct watchpoint *) bpt;
14951
1e718ff1
TJB
14952 orig_enable_state = bpt->enable_state;
14953 bpt->enable_state = bp_enabled;
3a5c3e22 14954 update_watchpoint (w, 1 /* reparse */);
c906108c 14955 }
492d29ea 14956 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14957 {
1e718ff1 14958 bpt->enable_state = orig_enable_state;
dde02812
ES
14959 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14960 bpt->number);
14961 return;
c5aa993b 14962 }
492d29ea 14963 END_CATCH
c906108c 14964 }
0101ce28 14965
b775012e
LM
14966 bpt->enable_state = bp_enabled;
14967
14968 /* Mark breakpoint locations modified. */
14969 mark_breakpoint_modified (bpt);
14970
d248b706
KY
14971 if (target_supports_enable_disable_tracepoint ()
14972 && current_trace_status ()->running && is_tracepoint (bpt))
14973 {
14974 struct bp_location *location;
14975
14976 for (location = bpt->loc; location; location = location->next)
14977 target_enable_tracepoint (location);
14978 }
14979
b4c291bb 14980 bpt->disposition = disposition;
816338b5 14981 bpt->enable_count = count;
44702360 14982 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14983
8d3788bd 14984 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14985}
14986
fe3f5fa8 14987
c906108c 14988void
fba45db2 14989enable_breakpoint (struct breakpoint *bpt)
c906108c 14990{
816338b5 14991 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14992}
14993
14994static void
14995do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14996{
14997 enable_breakpoint (bpt);
c906108c
SS
14998}
14999
95a42b64
TT
15000/* A callback for map_breakpoint_numbers that calls
15001 enable_breakpoint. */
15002
15003static void
15004do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15005{
51be5b68 15006 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15007}
15008
c906108c
SS
15009/* The enable command enables the specified breakpoints (or all defined
15010 breakpoints) so they once again become (or continue to be) effective
1272ad14 15011 in stopping the inferior. */
c906108c 15012
c906108c 15013static void
fba45db2 15014enable_command (char *args, int from_tty)
c906108c 15015{
c906108c 15016 if (args == 0)
46c6471b
PA
15017 {
15018 struct breakpoint *bpt;
15019
15020 ALL_BREAKPOINTS (bpt)
15021 if (user_breakpoint_p (bpt))
15022 enable_breakpoint (bpt);
15023 }
9eaabc75 15024 else
0d381245 15025 {
9eaabc75
MW
15026 char *num = extract_arg (&args);
15027
15028 while (num)
d248b706 15029 {
9eaabc75 15030 if (strchr (num, '.'))
b775012e 15031 {
9eaabc75
MW
15032 struct bp_location *loc = find_location_by_number (num);
15033
15034 if (loc)
15035 {
15036 if (!loc->enabled)
15037 {
15038 loc->enabled = 1;
15039 mark_breakpoint_location_modified (loc);
15040 }
15041 if (target_supports_enable_disable_tracepoint ()
15042 && current_trace_status ()->running && loc->owner
15043 && is_tracepoint (loc->owner))
15044 target_enable_tracepoint (loc);
15045 }
44702360 15046 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15047 }
9eaabc75
MW
15048 else
15049 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15050 num = extract_arg (&args);
d248b706 15051 }
0d381245 15052 }
c906108c
SS
15053}
15054
816338b5
SS
15055/* This struct packages up disposition data for application to multiple
15056 breakpoints. */
15057
15058struct disp_data
15059{
15060 enum bpdisp disp;
15061 int count;
15062};
15063
c906108c 15064static void
51be5b68
PA
15065do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15066{
816338b5 15067 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15068
816338b5 15069 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15070}
15071
15072static void
15073do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15074{
816338b5 15075 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15076
15077 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15078}
15079
c906108c 15080static void
fba45db2 15081enable_once_command (char *args, int from_tty)
c906108c 15082{
51be5b68 15083 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15084}
15085
816338b5
SS
15086static void
15087do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15088{
15089 struct disp_data disp = { disp_disable, *(int *) countptr };
15090
15091 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15092}
15093
15094static void
15095enable_count_command (char *args, int from_tty)
15096{
b9d61307
SM
15097 int count;
15098
15099 if (args == NULL)
15100 error_no_arg (_("hit count"));
15101
15102 count = get_number (&args);
816338b5
SS
15103
15104 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15105}
15106
c906108c 15107static void
51be5b68 15108do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15109{
816338b5 15110 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15111
15112 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15113}
15114
c906108c 15115static void
fba45db2 15116enable_delete_command (char *args, int from_tty)
c906108c 15117{
51be5b68 15118 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15119}
15120\f
fa8d40ab
JJ
15121static void
15122set_breakpoint_cmd (char *args, int from_tty)
15123{
15124}
15125
15126static void
15127show_breakpoint_cmd (char *args, int from_tty)
15128{
15129}
15130
1f3b5d1b
PP
15131/* Invalidate last known value of any hardware watchpoint if
15132 the memory which that value represents has been written to by
15133 GDB itself. */
15134
15135static void
8de0566d
YQ
15136invalidate_bp_value_on_memory_change (struct inferior *inferior,
15137 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15138 const bfd_byte *data)
15139{
15140 struct breakpoint *bp;
15141
15142 ALL_BREAKPOINTS (bp)
15143 if (bp->enable_state == bp_enabled
3a5c3e22 15144 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15145 {
3a5c3e22 15146 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15147
3a5c3e22
PA
15148 if (wp->val_valid && wp->val)
15149 {
15150 struct bp_location *loc;
15151
15152 for (loc = bp->loc; loc != NULL; loc = loc->next)
15153 if (loc->loc_type == bp_loc_hardware_watchpoint
15154 && loc->address + loc->length > addr
15155 && addr + len > loc->address)
15156 {
15157 value_free (wp->val);
15158 wp->val = NULL;
15159 wp->val_valid = 0;
15160 }
15161 }
1f3b5d1b
PP
15162 }
15163}
15164
8181d85f
DJ
15165/* Create and insert a breakpoint for software single step. */
15166
15167void
6c95b8df 15168insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15169 struct address_space *aspace,
15170 CORE_ADDR next_pc)
8181d85f 15171{
7c16b83e
PA
15172 struct thread_info *tp = inferior_thread ();
15173 struct symtab_and_line sal;
15174 CORE_ADDR pc = next_pc;
8181d85f 15175
34b7e8a6
PA
15176 if (tp->control.single_step_breakpoints == NULL)
15177 {
15178 tp->control.single_step_breakpoints
5d5658a1 15179 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15180 }
8181d85f 15181
7c16b83e
PA
15182 sal = find_pc_line (pc, 0);
15183 sal.pc = pc;
15184 sal.section = find_pc_overlay (pc);
15185 sal.explicit_pc = 1;
34b7e8a6 15186 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15187
7c16b83e 15188 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15189}
15190
93f9a11f
YQ
15191/* Insert single step breakpoints according to the current state. */
15192
15193int
15194insert_single_step_breakpoints (struct gdbarch *gdbarch)
15195{
f5ea389a 15196 struct regcache *regcache = get_current_regcache ();
93f9a11f
YQ
15197 VEC (CORE_ADDR) * next_pcs;
15198
f5ea389a 15199 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f
YQ
15200
15201 if (next_pcs != NULL)
15202 {
15203 int i;
15204 CORE_ADDR pc;
f5ea389a 15205 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
15206 struct address_space *aspace = get_frame_address_space (frame);
15207
15208 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15209 insert_single_step_breakpoint (gdbarch, aspace, pc);
15210
15211 VEC_free (CORE_ADDR, next_pcs);
15212
15213 return 1;
15214 }
15215 else
15216 return 0;
15217}
15218
34b7e8a6 15219/* See breakpoint.h. */
f02253f1
HZ
15220
15221int
7c16b83e
PA
15222breakpoint_has_location_inserted_here (struct breakpoint *bp,
15223 struct address_space *aspace,
15224 CORE_ADDR pc)
1aafd4da 15225{
7c16b83e 15226 struct bp_location *loc;
1aafd4da 15227
7c16b83e
PA
15228 for (loc = bp->loc; loc != NULL; loc = loc->next)
15229 if (loc->inserted
15230 && breakpoint_location_address_match (loc, aspace, pc))
15231 return 1;
1aafd4da 15232
7c16b83e 15233 return 0;
ef370185
JB
15234}
15235
15236/* Check whether a software single-step breakpoint is inserted at
15237 PC. */
15238
15239int
15240single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15241 CORE_ADDR pc)
15242{
34b7e8a6
PA
15243 struct breakpoint *bpt;
15244
15245 ALL_BREAKPOINTS (bpt)
15246 {
15247 if (bpt->type == bp_single_step
15248 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15249 return 1;
15250 }
15251 return 0;
1aafd4da
UW
15252}
15253
1042e4c0
SS
15254/* Tracepoint-specific operations. */
15255
15256/* Set tracepoint count to NUM. */
15257static void
15258set_tracepoint_count (int num)
15259{
15260 tracepoint_count = num;
4fa62494 15261 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15262}
15263
70221824 15264static void
1042e4c0
SS
15265trace_command (char *arg, int from_tty)
15266{
55aa24fb 15267 struct breakpoint_ops *ops;
f00aae0f
KS
15268 struct event_location *location;
15269 struct cleanup *back_to;
55aa24fb 15270
f00aae0f
KS
15271 location = string_to_event_location (&arg, current_language);
15272 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15273 if (location != NULL
15274 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15275 ops = &tracepoint_probe_breakpoint_ops;
15276 else
15277 ops = &tracepoint_breakpoint_ops;
15278
558a9d82 15279 create_breakpoint (get_current_arch (),
f00aae0f
KS
15280 location,
15281 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15282 0 /* tempflag */,
15283 bp_tracepoint /* type_wanted */,
15284 0 /* Ignore count */,
15285 pending_break_support,
15286 ops,
15287 from_tty,
15288 1 /* enabled */,
15289 0 /* internal */, 0);
f00aae0f 15290 do_cleanups (back_to);
1042e4c0
SS
15291}
15292
70221824 15293static void
7a697b8d
SS
15294ftrace_command (char *arg, int from_tty)
15295{
f00aae0f
KS
15296 struct event_location *location;
15297 struct cleanup *back_to;
15298
15299 location = string_to_event_location (&arg, current_language);
15300 back_to = make_cleanup_delete_event_location (location);
558a9d82 15301 create_breakpoint (get_current_arch (),
f00aae0f
KS
15302 location,
15303 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15304 0 /* tempflag */,
15305 bp_fast_tracepoint /* type_wanted */,
15306 0 /* Ignore count */,
15307 pending_break_support,
15308 &tracepoint_breakpoint_ops,
15309 from_tty,
15310 1 /* enabled */,
15311 0 /* internal */, 0);
f00aae0f 15312 do_cleanups (back_to);
0fb4aa4b
PA
15313}
15314
15315/* strace command implementation. Creates a static tracepoint. */
15316
70221824 15317static void
0fb4aa4b
PA
15318strace_command (char *arg, int from_tty)
15319{
983af33b 15320 struct breakpoint_ops *ops;
f00aae0f
KS
15321 struct event_location *location;
15322 struct cleanup *back_to;
983af33b
SDJ
15323
15324 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15325 or with a normal static tracepoint. */
61012eef 15326 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15327 {
15328 ops = &strace_marker_breakpoint_ops;
15329 location = new_linespec_location (&arg);
15330 }
983af33b 15331 else
f00aae0f
KS
15332 {
15333 ops = &tracepoint_breakpoint_ops;
15334 location = string_to_event_location (&arg, current_language);
15335 }
983af33b 15336
f00aae0f 15337 back_to = make_cleanup_delete_event_location (location);
558a9d82 15338 create_breakpoint (get_current_arch (),
f00aae0f
KS
15339 location,
15340 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15341 0 /* tempflag */,
15342 bp_static_tracepoint /* type_wanted */,
15343 0 /* Ignore count */,
15344 pending_break_support,
15345 ops,
15346 from_tty,
15347 1 /* enabled */,
15348 0 /* internal */, 0);
f00aae0f 15349 do_cleanups (back_to);
7a697b8d
SS
15350}
15351
409873ef
SS
15352/* Set up a fake reader function that gets command lines from a linked
15353 list that was acquired during tracepoint uploading. */
15354
15355static struct uploaded_tp *this_utp;
3149d8c1 15356static int next_cmd;
409873ef
SS
15357
15358static char *
15359read_uploaded_action (void)
15360{
15361 char *rslt;
15362
3149d8c1 15363 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15364
3149d8c1 15365 next_cmd++;
409873ef
SS
15366
15367 return rslt;
15368}
15369
00bf0b85
SS
15370/* Given information about a tracepoint as recorded on a target (which
15371 can be either a live system or a trace file), attempt to create an
15372 equivalent GDB tracepoint. This is not a reliable process, since
15373 the target does not necessarily have all the information used when
15374 the tracepoint was originally defined. */
15375
d9b3f62e 15376struct tracepoint *
00bf0b85 15377create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15378{
409873ef 15379 char *addr_str, small_buf[100];
d9b3f62e 15380 struct tracepoint *tp;
f00aae0f
KS
15381 struct event_location *location;
15382 struct cleanup *cleanup;
fd9b8c24 15383
409873ef
SS
15384 if (utp->at_string)
15385 addr_str = utp->at_string;
15386 else
15387 {
15388 /* In the absence of a source location, fall back to raw
15389 address. Since there is no way to confirm that the address
15390 means the same thing as when the trace was started, warn the
15391 user. */
3e43a32a
MS
15392 warning (_("Uploaded tracepoint %d has no "
15393 "source location, using raw address"),
409873ef 15394 utp->number);
8c042590 15395 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15396 addr_str = small_buf;
15397 }
15398
15399 /* There's not much we can do with a sequence of bytecodes. */
15400 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15401 warning (_("Uploaded tracepoint %d condition "
15402 "has no source form, ignoring it"),
409873ef 15403 utp->number);
d5551862 15404
f00aae0f
KS
15405 location = string_to_event_location (&addr_str, current_language);
15406 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15407 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15408 location,
15409 utp->cond_string, -1, addr_str,
e7e0cddf 15410 0 /* parse cond/thread */,
8cdf0e15 15411 0 /* tempflag */,
0fb4aa4b 15412 utp->type /* type_wanted */,
8cdf0e15
VP
15413 0 /* Ignore count */,
15414 pending_break_support,
348d480f 15415 &tracepoint_breakpoint_ops,
8cdf0e15 15416 0 /* from_tty */,
84f4c1fe 15417 utp->enabled /* enabled */,
44f238bb
PA
15418 0 /* internal */,
15419 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15420 {
15421 do_cleanups (cleanup);
15422 return NULL;
15423 }
15424
15425 do_cleanups (cleanup);
fd9b8c24 15426
409873ef 15427 /* Get the tracepoint we just created. */
fd9b8c24
PA
15428 tp = get_tracepoint (tracepoint_count);
15429 gdb_assert (tp != NULL);
d5551862 15430
00bf0b85
SS
15431 if (utp->pass > 0)
15432 {
8c042590
PM
15433 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15434 tp->base.number);
00bf0b85 15435
409873ef 15436 trace_pass_command (small_buf, 0);
00bf0b85
SS
15437 }
15438
409873ef
SS
15439 /* If we have uploaded versions of the original commands, set up a
15440 special-purpose "reader" function and call the usual command line
15441 reader, then pass the result to the breakpoint command-setting
15442 function. */
3149d8c1 15443 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15444 {
409873ef 15445 struct command_line *cmd_list;
00bf0b85 15446
409873ef 15447 this_utp = utp;
3149d8c1 15448 next_cmd = 0;
d5551862 15449
409873ef
SS
15450 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15451
d9b3f62e 15452 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15453 }
3149d8c1
SS
15454 else if (!VEC_empty (char_ptr, utp->actions)
15455 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15456 warning (_("Uploaded tracepoint %d actions "
15457 "have no source form, ignoring them"),
409873ef 15458 utp->number);
00bf0b85 15459
f196051f
SS
15460 /* Copy any status information that might be available. */
15461 tp->base.hit_count = utp->hit_count;
15462 tp->traceframe_usage = utp->traceframe_usage;
15463
00bf0b85 15464 return tp;
d9b3f62e 15465}
00bf0b85 15466
1042e4c0
SS
15467/* Print information on tracepoint number TPNUM_EXP, or all if
15468 omitted. */
15469
15470static void
e5a67952 15471tracepoints_info (char *args, int from_tty)
1042e4c0 15472{
79a45e25 15473 struct ui_out *uiout = current_uiout;
e5a67952 15474 int num_printed;
1042e4c0 15475
e5a67952 15476 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15477
15478 if (num_printed == 0)
1042e4c0 15479 {
e5a67952 15480 if (args == NULL || *args == '\0')
d77f58be
SS
15481 ui_out_message (uiout, 0, "No tracepoints.\n");
15482 else
e5a67952 15483 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15484 }
ad443146
SS
15485
15486 default_collect_info ();
1042e4c0
SS
15487}
15488
4a64f543 15489/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15490 Not supported by all targets. */
15491static void
15492enable_trace_command (char *args, int from_tty)
15493{
15494 enable_command (args, from_tty);
15495}
15496
4a64f543 15497/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15498 Not supported by all targets. */
15499static void
15500disable_trace_command (char *args, int from_tty)
15501{
15502 disable_command (args, from_tty);
15503}
15504
4a64f543 15505/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15506static void
15507delete_trace_command (char *arg, int from_tty)
15508{
35df4500 15509 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15510
15511 dont_repeat ();
15512
15513 if (arg == 0)
15514 {
15515 int breaks_to_delete = 0;
15516
15517 /* Delete all breakpoints if no argument.
15518 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15519 have to be deleted with an explicit breakpoint number
15520 argument. */
1042e4c0 15521 ALL_TRACEPOINTS (b)
46c6471b 15522 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15523 {
15524 breaks_to_delete = 1;
15525 break;
15526 }
1042e4c0
SS
15527
15528 /* Ask user only if there are some breakpoints to delete. */
15529 if (!from_tty
15530 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15531 {
35df4500 15532 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15533 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15534 delete_breakpoint (b);
1042e4c0
SS
15535 }
15536 }
15537 else
51be5b68 15538 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15539}
15540
197f0a60
TT
15541/* Helper function for trace_pass_command. */
15542
15543static void
d9b3f62e 15544trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15545{
d9b3f62e 15546 tp->pass_count = count;
6f6484cd 15547 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15548 if (from_tty)
15549 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15550 tp->base.number, count);
197f0a60
TT
15551}
15552
1042e4c0
SS
15553/* Set passcount for tracepoint.
15554
15555 First command argument is passcount, second is tracepoint number.
15556 If tracepoint number omitted, apply to most recently defined.
15557 Also accepts special argument "all". */
15558
15559static void
15560trace_pass_command (char *args, int from_tty)
15561{
d9b3f62e 15562 struct tracepoint *t1;
1042e4c0 15563 unsigned int count;
1042e4c0
SS
15564
15565 if (args == 0 || *args == 0)
3e43a32a
MS
15566 error (_("passcount command requires an "
15567 "argument (count + optional TP num)"));
1042e4c0 15568
4a64f543 15569 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15570
529480d0 15571 args = skip_spaces (args);
1042e4c0
SS
15572 if (*args && strncasecmp (args, "all", 3) == 0)
15573 {
d9b3f62e
PA
15574 struct breakpoint *b;
15575
1042e4c0 15576 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15577 if (*args)
15578 error (_("Junk at end of arguments."));
1042e4c0 15579
d9b3f62e 15580 ALL_TRACEPOINTS (b)
197f0a60 15581 {
d9b3f62e 15582 t1 = (struct tracepoint *) b;
197f0a60
TT
15583 trace_pass_set_count (t1, count, from_tty);
15584 }
15585 }
15586 else if (*args == '\0')
1042e4c0 15587 {
5fa1d40e 15588 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15589 if (t1)
197f0a60
TT
15590 trace_pass_set_count (t1, count, from_tty);
15591 }
15592 else
15593 {
bfd28288
PA
15594 number_or_range_parser parser (args);
15595 while (!parser.finished ())
1042e4c0 15596 {
bfd28288 15597 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15598 if (t1)
15599 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15600 }
15601 }
1042e4c0
SS
15602}
15603
d9b3f62e 15604struct tracepoint *
1042e4c0
SS
15605get_tracepoint (int num)
15606{
15607 struct breakpoint *t;
15608
15609 ALL_TRACEPOINTS (t)
15610 if (t->number == num)
d9b3f62e 15611 return (struct tracepoint *) t;
1042e4c0
SS
15612
15613 return NULL;
15614}
15615
d5551862
SS
15616/* Find the tracepoint with the given target-side number (which may be
15617 different from the tracepoint number after disconnecting and
15618 reconnecting). */
15619
d9b3f62e 15620struct tracepoint *
d5551862
SS
15621get_tracepoint_by_number_on_target (int num)
15622{
d9b3f62e 15623 struct breakpoint *b;
d5551862 15624
d9b3f62e
PA
15625 ALL_TRACEPOINTS (b)
15626 {
15627 struct tracepoint *t = (struct tracepoint *) b;
15628
15629 if (t->number_on_target == num)
15630 return t;
15631 }
d5551862
SS
15632
15633 return NULL;
15634}
15635
1042e4c0 15636/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15637 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15638 If the argument is missing, the most recent tracepoint
15639 (tracepoint_count) is returned. */
15640
d9b3f62e 15641struct tracepoint *
197f0a60 15642get_tracepoint_by_number (char **arg,
bfd28288 15643 number_or_range_parser *parser)
1042e4c0 15644{
1042e4c0
SS
15645 struct breakpoint *t;
15646 int tpnum;
15647 char *instring = arg == NULL ? NULL : *arg;
15648
bfd28288 15649 if (parser != NULL)
197f0a60 15650 {
bfd28288
PA
15651 gdb_assert (!parser->finished ());
15652 tpnum = parser->get_number ();
197f0a60
TT
15653 }
15654 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15655 tpnum = tracepoint_count;
1042e4c0 15656 else
197f0a60 15657 tpnum = get_number (arg);
1042e4c0
SS
15658
15659 if (tpnum <= 0)
15660 {
15661 if (instring && *instring)
15662 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15663 instring);
15664 else
5fa1d40e 15665 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15666 return NULL;
15667 }
15668
15669 ALL_TRACEPOINTS (t)
15670 if (t->number == tpnum)
15671 {
d9b3f62e 15672 return (struct tracepoint *) t;
1042e4c0
SS
15673 }
15674
1042e4c0
SS
15675 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15676 return NULL;
15677}
15678
d9b3f62e
PA
15679void
15680print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15681{
15682 if (b->thread != -1)
15683 fprintf_unfiltered (fp, " thread %d", b->thread);
15684
15685 if (b->task != 0)
15686 fprintf_unfiltered (fp, " task %d", b->task);
15687
15688 fprintf_unfiltered (fp, "\n");
15689}
15690
6149aea9
PA
15691/* Save information on user settable breakpoints (watchpoints, etc) to
15692 a new script file named FILENAME. If FILTER is non-NULL, call it
15693 on each breakpoint and only include the ones for which it returns
15694 non-zero. */
15695
1042e4c0 15696static void
6149aea9
PA
15697save_breakpoints (char *filename, int from_tty,
15698 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15699{
15700 struct breakpoint *tp;
6149aea9 15701 int any = 0;
1042e4c0 15702 struct cleanup *cleanup;
a7bdde9e 15703 struct ui_file *fp;
6149aea9 15704 int extra_trace_bits = 0;
1042e4c0 15705
6149aea9
PA
15706 if (filename == 0 || *filename == 0)
15707 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15708
15709 /* See if we have anything to save. */
6149aea9 15710 ALL_BREAKPOINTS (tp)
1042e4c0 15711 {
6149aea9 15712 /* Skip internal and momentary breakpoints. */
09d682a4 15713 if (!user_breakpoint_p (tp))
6149aea9
PA
15714 continue;
15715
15716 /* If we have a filter, only save the breakpoints it accepts. */
15717 if (filter && !filter (tp))
15718 continue;
15719
15720 any = 1;
15721
15722 if (is_tracepoint (tp))
15723 {
15724 extra_trace_bits = 1;
15725
15726 /* We can stop searching. */
15727 break;
15728 }
1042e4c0 15729 }
6149aea9
PA
15730
15731 if (!any)
1042e4c0 15732 {
6149aea9 15733 warning (_("Nothing to save."));
1042e4c0
SS
15734 return;
15735 }
15736
c718be47
PA
15737 filename = tilde_expand (filename);
15738 cleanup = make_cleanup (xfree, filename);
15739 fp = gdb_fopen (filename, "w");
059fb39f 15740 if (!fp)
6149aea9
PA
15741 error (_("Unable to open file '%s' for saving (%s)"),
15742 filename, safe_strerror (errno));
a7bdde9e 15743 make_cleanup_ui_file_delete (fp);
8bf6485c 15744
6149aea9
PA
15745 if (extra_trace_bits)
15746 save_trace_state_variables (fp);
8bf6485c 15747
6149aea9 15748 ALL_BREAKPOINTS (tp)
1042e4c0 15749 {
6149aea9 15750 /* Skip internal and momentary breakpoints. */
09d682a4 15751 if (!user_breakpoint_p (tp))
6149aea9 15752 continue;
8bf6485c 15753
6149aea9
PA
15754 /* If we have a filter, only save the breakpoints it accepts. */
15755 if (filter && !filter (tp))
15756 continue;
15757
348d480f 15758 tp->ops->print_recreate (tp, fp);
1042e4c0 15759
6149aea9
PA
15760 /* Note, we can't rely on tp->number for anything, as we can't
15761 assume the recreated breakpoint numbers will match. Use $bpnum
15762 instead. */
15763
15764 if (tp->cond_string)
15765 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15766
15767 if (tp->ignore_count)
15768 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15769
2d9442cc 15770 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15771 {
6149aea9 15772 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15773
79a45e25 15774 ui_out_redirect (current_uiout, fp);
492d29ea 15775 TRY
1042e4c0 15776 {
79a45e25 15777 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15778 }
492d29ea
PA
15779 CATCH (ex, RETURN_MASK_ALL)
15780 {
6c63c96a 15781 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15782 throw_exception (ex);
15783 }
15784 END_CATCH
1042e4c0 15785
6c63c96a 15786 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15787 fprintf_unfiltered (fp, " end\n");
1042e4c0 15788 }
6149aea9
PA
15789
15790 if (tp->enable_state == bp_disabled)
99894e11 15791 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15792
15793 /* If this is a multi-location breakpoint, check if the locations
15794 should be individually disabled. Watchpoint locations are
15795 special, and not user visible. */
15796 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15797 {
15798 struct bp_location *loc;
15799 int n = 1;
15800
15801 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15802 if (!loc->enabled)
15803 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15804 }
1042e4c0 15805 }
8bf6485c 15806
6149aea9 15807 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15808 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15809
1042e4c0 15810 if (from_tty)
6149aea9 15811 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15812 do_cleanups (cleanup);
6149aea9
PA
15813}
15814
15815/* The `save breakpoints' command. */
15816
15817static void
15818save_breakpoints_command (char *args, int from_tty)
15819{
15820 save_breakpoints (args, from_tty, NULL);
15821}
15822
15823/* The `save tracepoints' command. */
15824
15825static void
15826save_tracepoints_command (char *args, int from_tty)
15827{
15828 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15829}
15830
15831/* Create a vector of all tracepoints. */
15832
15833VEC(breakpoint_p) *
eeae04df 15834all_tracepoints (void)
1042e4c0
SS
15835{
15836 VEC(breakpoint_p) *tp_vec = 0;
15837 struct breakpoint *tp;
15838
15839 ALL_TRACEPOINTS (tp)
15840 {
15841 VEC_safe_push (breakpoint_p, tp_vec, tp);
15842 }
15843
15844 return tp_vec;
15845}
15846
c906108c 15847\f
629500fa
KS
15848/* This help string is used to consolidate all the help string for specifying
15849 locations used by several commands. */
15850
15851#define LOCATION_HELP_STRING \
15852"Linespecs are colon-separated lists of location parameters, such as\n\
15853source filename, function name, label name, and line number.\n\
15854Example: To specify the start of a label named \"the_top\" in the\n\
15855function \"fact\" in the file \"factorial.c\", use\n\
15856\"factorial.c:fact:the_top\".\n\
15857\n\
15858Address locations begin with \"*\" and specify an exact address in the\n\
15859program. Example: To specify the fourth byte past the start function\n\
15860\"main\", use \"*main + 4\".\n\
15861\n\
15862Explicit locations are similar to linespecs but use an option/argument\n\
15863syntax to specify location parameters.\n\
15864Example: To specify the start of the label named \"the_top\" in the\n\
15865function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15866-function fact -label the_top\".\n"
15867
4a64f543
MS
15868/* This help string is used for the break, hbreak, tbreak and thbreak
15869 commands. It is defined as a macro to prevent duplication.
15870 COMMAND should be a string constant containing the name of the
15871 command. */
629500fa 15872
31e2b00f 15873#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15874command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15875PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15876probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15877guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15878`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15879LOCATION may be a linespec, address, or explicit location as described\n\
15880below.\n\
15881\n\
dc10affe
PA
15882With no LOCATION, uses current execution address of the selected\n\
15883stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15884\n\
15885THREADNUM is the number from \"info threads\".\n\
15886CONDITION is a boolean expression.\n\
629500fa 15887\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15888Multiple breakpoints at one place are permitted, and useful if their\n\
15889conditions are different.\n\
31e2b00f
AS
15890\n\
15891Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15892
44feb3ce
TT
15893/* List of subcommands for "catch". */
15894static struct cmd_list_element *catch_cmdlist;
15895
15896/* List of subcommands for "tcatch". */
15897static struct cmd_list_element *tcatch_cmdlist;
15898
9ac4176b 15899void
44feb3ce 15900add_catch_command (char *name, char *docstring,
82ae6c8d 15901 cmd_sfunc_ftype *sfunc,
625e8578 15902 completer_ftype *completer,
44feb3ce
TT
15903 void *user_data_catch,
15904 void *user_data_tcatch)
15905{
15906 struct cmd_list_element *command;
15907
15908 command = add_cmd (name, class_breakpoint, NULL, docstring,
15909 &catch_cmdlist);
15910 set_cmd_sfunc (command, sfunc);
15911 set_cmd_context (command, user_data_catch);
a96d9b2e 15912 set_cmd_completer (command, completer);
44feb3ce
TT
15913
15914 command = add_cmd (name, class_breakpoint, NULL, docstring,
15915 &tcatch_cmdlist);
15916 set_cmd_sfunc (command, sfunc);
15917 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15918 set_cmd_completer (command, completer);
44feb3ce
TT
15919}
15920
6149aea9
PA
15921static void
15922save_command (char *arg, int from_tty)
15923{
3e43a32a
MS
15924 printf_unfiltered (_("\"save\" must be followed by "
15925 "the name of a save subcommand.\n"));
635c7e8a 15926 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15927}
15928
84f4c1fe
PM
15929struct breakpoint *
15930iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15931 void *data)
15932{
35df4500 15933 struct breakpoint *b, *b_tmp;
84f4c1fe 15934
35df4500 15935 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15936 {
15937 if ((*callback) (b, data))
15938 return b;
15939 }
15940
15941 return NULL;
15942}
15943
0574c78f
GB
15944/* Zero if any of the breakpoint's locations could be a location where
15945 functions have been inlined, nonzero otherwise. */
15946
15947static int
15948is_non_inline_function (struct breakpoint *b)
15949{
15950 /* The shared library event breakpoint is set on the address of a
15951 non-inline function. */
15952 if (b->type == bp_shlib_event)
15953 return 1;
15954
15955 return 0;
15956}
15957
15958/* Nonzero if the specified PC cannot be a location where functions
15959 have been inlined. */
15960
15961int
09ac7c10
TT
15962pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15963 const struct target_waitstatus *ws)
0574c78f
GB
15964{
15965 struct breakpoint *b;
15966 struct bp_location *bl;
15967
15968 ALL_BREAKPOINTS (b)
15969 {
15970 if (!is_non_inline_function (b))
15971 continue;
15972
15973 for (bl = b->loc; bl != NULL; bl = bl->next)
15974 {
15975 if (!bl->shlib_disabled
09ac7c10 15976 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15977 return 1;
15978 }
15979 }
15980
15981 return 0;
15982}
15983
2f202fde
JK
15984/* Remove any references to OBJFILE which is going to be freed. */
15985
15986void
15987breakpoint_free_objfile (struct objfile *objfile)
15988{
15989 struct bp_location **locp, *loc;
15990
15991 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15992 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15993 loc->symtab = NULL;
15994}
15995
2060206e
PA
15996void
15997initialize_breakpoint_ops (void)
15998{
15999 static int initialized = 0;
16000
16001 struct breakpoint_ops *ops;
16002
16003 if (initialized)
16004 return;
16005 initialized = 1;
16006
16007 /* The breakpoint_ops structure to be inherit by all kinds of
16008 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16009 internal and momentary breakpoints, etc.). */
16010 ops = &bkpt_base_breakpoint_ops;
16011 *ops = base_breakpoint_ops;
16012 ops->re_set = bkpt_re_set;
16013 ops->insert_location = bkpt_insert_location;
16014 ops->remove_location = bkpt_remove_location;
16015 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 16016 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 16017 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 16018 ops->decode_location = bkpt_decode_location;
2060206e
PA
16019
16020 /* The breakpoint_ops structure to be used in regular breakpoints. */
16021 ops = &bkpt_breakpoint_ops;
16022 *ops = bkpt_base_breakpoint_ops;
16023 ops->re_set = bkpt_re_set;
16024 ops->resources_needed = bkpt_resources_needed;
16025 ops->print_it = bkpt_print_it;
16026 ops->print_mention = bkpt_print_mention;
16027 ops->print_recreate = bkpt_print_recreate;
16028
16029 /* Ranged breakpoints. */
16030 ops = &ranged_breakpoint_ops;
16031 *ops = bkpt_breakpoint_ops;
16032 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16033 ops->resources_needed = resources_needed_ranged_breakpoint;
16034 ops->print_it = print_it_ranged_breakpoint;
16035 ops->print_one = print_one_ranged_breakpoint;
16036 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16037 ops->print_mention = print_mention_ranged_breakpoint;
16038 ops->print_recreate = print_recreate_ranged_breakpoint;
16039
16040 /* Internal breakpoints. */
16041 ops = &internal_breakpoint_ops;
16042 *ops = bkpt_base_breakpoint_ops;
16043 ops->re_set = internal_bkpt_re_set;
16044 ops->check_status = internal_bkpt_check_status;
16045 ops->print_it = internal_bkpt_print_it;
16046 ops->print_mention = internal_bkpt_print_mention;
16047
16048 /* Momentary breakpoints. */
16049 ops = &momentary_breakpoint_ops;
16050 *ops = bkpt_base_breakpoint_ops;
16051 ops->re_set = momentary_bkpt_re_set;
16052 ops->check_status = momentary_bkpt_check_status;
16053 ops->print_it = momentary_bkpt_print_it;
16054 ops->print_mention = momentary_bkpt_print_mention;
16055
e2e4d78b
JK
16056 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16057 ops = &longjmp_breakpoint_ops;
16058 *ops = momentary_breakpoint_ops;
16059 ops->dtor = longjmp_bkpt_dtor;
16060
55aa24fb
SDJ
16061 /* Probe breakpoints. */
16062 ops = &bkpt_probe_breakpoint_ops;
16063 *ops = bkpt_breakpoint_ops;
16064 ops->insert_location = bkpt_probe_insert_location;
16065 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
16066 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16067 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 16068
2060206e
PA
16069 /* Watchpoints. */
16070 ops = &watchpoint_breakpoint_ops;
16071 *ops = base_breakpoint_ops;
3a5c3e22 16072 ops->dtor = dtor_watchpoint;
2060206e
PA
16073 ops->re_set = re_set_watchpoint;
16074 ops->insert_location = insert_watchpoint;
16075 ops->remove_location = remove_watchpoint;
16076 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16077 ops->check_status = check_status_watchpoint;
16078 ops->resources_needed = resources_needed_watchpoint;
16079 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16080 ops->print_it = print_it_watchpoint;
16081 ops->print_mention = print_mention_watchpoint;
16082 ops->print_recreate = print_recreate_watchpoint;
427cd150 16083 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16084
16085 /* Masked watchpoints. */
16086 ops = &masked_watchpoint_breakpoint_ops;
16087 *ops = watchpoint_breakpoint_ops;
16088 ops->insert_location = insert_masked_watchpoint;
16089 ops->remove_location = remove_masked_watchpoint;
16090 ops->resources_needed = resources_needed_masked_watchpoint;
16091 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16092 ops->print_it = print_it_masked_watchpoint;
16093 ops->print_one_detail = print_one_detail_masked_watchpoint;
16094 ops->print_mention = print_mention_masked_watchpoint;
16095 ops->print_recreate = print_recreate_masked_watchpoint;
16096
16097 /* Tracepoints. */
16098 ops = &tracepoint_breakpoint_ops;
16099 *ops = base_breakpoint_ops;
16100 ops->re_set = tracepoint_re_set;
16101 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16102 ops->print_one_detail = tracepoint_print_one_detail;
16103 ops->print_mention = tracepoint_print_mention;
16104 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16105 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16106 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16107 ops->decode_location = tracepoint_decode_location;
983af33b 16108
55aa24fb
SDJ
16109 /* Probe tracepoints. */
16110 ops = &tracepoint_probe_breakpoint_ops;
16111 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16112 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16113 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16114
983af33b
SDJ
16115 /* Static tracepoints with marker (`-m'). */
16116 ops = &strace_marker_breakpoint_ops;
16117 *ops = tracepoint_breakpoint_ops;
5f700d83 16118 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16119 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16120 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16121
16122 /* Fork catchpoints. */
16123 ops = &catch_fork_breakpoint_ops;
16124 *ops = base_breakpoint_ops;
16125 ops->insert_location = insert_catch_fork;
16126 ops->remove_location = remove_catch_fork;
16127 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16128 ops->print_it = print_it_catch_fork;
16129 ops->print_one = print_one_catch_fork;
16130 ops->print_mention = print_mention_catch_fork;
16131 ops->print_recreate = print_recreate_catch_fork;
16132
16133 /* Vfork catchpoints. */
16134 ops = &catch_vfork_breakpoint_ops;
16135 *ops = base_breakpoint_ops;
16136 ops->insert_location = insert_catch_vfork;
16137 ops->remove_location = remove_catch_vfork;
16138 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16139 ops->print_it = print_it_catch_vfork;
16140 ops->print_one = print_one_catch_vfork;
16141 ops->print_mention = print_mention_catch_vfork;
16142 ops->print_recreate = print_recreate_catch_vfork;
16143
16144 /* Exec catchpoints. */
16145 ops = &catch_exec_breakpoint_ops;
16146 *ops = base_breakpoint_ops;
16147 ops->dtor = dtor_catch_exec;
16148 ops->insert_location = insert_catch_exec;
16149 ops->remove_location = remove_catch_exec;
16150 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16151 ops->print_it = print_it_catch_exec;
16152 ops->print_one = print_one_catch_exec;
16153 ops->print_mention = print_mention_catch_exec;
16154 ops->print_recreate = print_recreate_catch_exec;
16155
edcc5120
TT
16156 /* Solib-related catchpoints. */
16157 ops = &catch_solib_breakpoint_ops;
16158 *ops = base_breakpoint_ops;
16159 ops->dtor = dtor_catch_solib;
16160 ops->insert_location = insert_catch_solib;
16161 ops->remove_location = remove_catch_solib;
16162 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16163 ops->check_status = check_status_catch_solib;
16164 ops->print_it = print_it_catch_solib;
16165 ops->print_one = print_one_catch_solib;
16166 ops->print_mention = print_mention_catch_solib;
16167 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16168
16169 ops = &dprintf_breakpoint_ops;
16170 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16171 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16172 ops->resources_needed = bkpt_resources_needed;
16173 ops->print_it = bkpt_print_it;
16174 ops->print_mention = bkpt_print_mention;
2d9442cc 16175 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16176 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16177 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16178}
16179
8bfd80db
YQ
16180/* Chain containing all defined "enable breakpoint" subcommands. */
16181
16182static struct cmd_list_element *enablebreaklist = NULL;
16183
c906108c 16184void
fba45db2 16185_initialize_breakpoint (void)
c906108c
SS
16186{
16187 struct cmd_list_element *c;
16188
2060206e
PA
16189 initialize_breakpoint_ops ();
16190
84acb35a 16191 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16192 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16193 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16194
55aa24fb
SDJ
16195 breakpoint_objfile_key
16196 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16197
c906108c
SS
16198 breakpoint_chain = 0;
16199 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16200 before a breakpoint is set. */
16201 breakpoint_count = 0;
16202
1042e4c0
SS
16203 tracepoint_count = 0;
16204
1bedd215
AC
16205 add_com ("ignore", class_breakpoint, ignore_command, _("\
16206Set ignore-count of breakpoint number N to COUNT.\n\
16207Usage is `ignore N COUNT'."));
c906108c 16208
1bedd215
AC
16209 add_com ("commands", class_breakpoint, commands_command, _("\
16210Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16211Give breakpoint number as argument after \"commands\".\n\
16212With no argument, the targeted breakpoint is the last one set.\n\
16213The commands themselves follow starting on the next line.\n\
16214Type a line containing \"end\" to indicate the end of them.\n\
16215Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16216then no output is printed when it is hit, except what the commands print."));
c906108c 16217
d55637df 16218 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16219Specify breakpoint number N to break only if COND is true.\n\
c906108c 16220Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16221expression to be evaluated whenever breakpoint N is reached."));
d55637df 16222 set_cmd_completer (c, condition_completer);
c906108c 16223
1bedd215 16224 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16225Set a temporary breakpoint.\n\
c906108c
SS
16226Like \"break\" except the breakpoint is only temporary,\n\
16227so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16228by using \"enable delete\" on the breakpoint number.\n\
16229\n"
16230BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16231 set_cmd_completer (c, location_completer);
c94fdfd0 16232
1bedd215 16233 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16234Set a hardware assisted breakpoint.\n\
c906108c 16235Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16236some target hardware may not have this support.\n\
16237\n"
16238BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16239 set_cmd_completer (c, location_completer);
c906108c 16240
1bedd215 16241 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16242Set a temporary hardware assisted breakpoint.\n\
c906108c 16243Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16244so it will be deleted when hit.\n\
16245\n"
16246BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16247 set_cmd_completer (c, location_completer);
c906108c 16248
1bedd215
AC
16249 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16250Enable some breakpoints.\n\
c906108c
SS
16251Give breakpoint numbers (separated by spaces) as arguments.\n\
16252With no subcommand, breakpoints are enabled until you command otherwise.\n\
16253This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16254With a subcommand you can enable temporarily."),
c906108c 16255 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16256
16257 add_com_alias ("en", "enable", class_breakpoint, 1);
16258
84951ab5 16259 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16260Enable some breakpoints.\n\
c906108c
SS
16261Give breakpoint numbers (separated by spaces) as arguments.\n\
16262This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16263May be abbreviated to simply \"enable\".\n"),
c5aa993b 16264 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16265
1a966eab
AC
16266 add_cmd ("once", no_class, enable_once_command, _("\
16267Enable breakpoints for one hit. Give breakpoint numbers.\n\
16268If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16269 &enablebreaklist);
16270
1a966eab
AC
16271 add_cmd ("delete", no_class, enable_delete_command, _("\
16272Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16273If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16274 &enablebreaklist);
16275
816338b5
SS
16276 add_cmd ("count", no_class, enable_count_command, _("\
16277Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16278If a breakpoint is hit while enabled in this fashion,\n\
16279the count is decremented; when it reaches zero, the breakpoint is disabled."),
16280 &enablebreaklist);
16281
1a966eab
AC
16282 add_cmd ("delete", no_class, enable_delete_command, _("\
16283Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16284If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16285 &enablelist);
16286
1a966eab
AC
16287 add_cmd ("once", no_class, enable_once_command, _("\
16288Enable breakpoints for one hit. Give breakpoint numbers.\n\
16289If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16290 &enablelist);
16291
16292 add_cmd ("count", no_class, enable_count_command, _("\
16293Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16294If a breakpoint is hit while enabled in this fashion,\n\
16295the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16296 &enablelist);
16297
1bedd215
AC
16298 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16299Disable some breakpoints.\n\
c906108c
SS
16300Arguments are breakpoint numbers with spaces in between.\n\
16301To disable all breakpoints, give no argument.\n\
64b9b334 16302A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16303 &disablelist, "disable ", 1, &cmdlist);
16304 add_com_alias ("dis", "disable", class_breakpoint, 1);
16305 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16306
1a966eab
AC
16307 add_cmd ("breakpoints", class_alias, disable_command, _("\
16308Disable some breakpoints.\n\
c906108c
SS
16309Arguments are breakpoint numbers with spaces in between.\n\
16310To disable all breakpoints, give no argument.\n\
64b9b334 16311A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16312This command may be abbreviated \"disable\"."),
c906108c
SS
16313 &disablelist);
16314
1bedd215
AC
16315 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16316Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16317Arguments are breakpoint numbers with spaces in between.\n\
16318To delete all breakpoints, give no argument.\n\
16319\n\
16320Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16321The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16322 &deletelist, "delete ", 1, &cmdlist);
16323 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16324 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16325
1a966eab
AC
16326 add_cmd ("breakpoints", class_alias, delete_command, _("\
16327Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16328Arguments are breakpoint numbers with spaces in between.\n\
16329To delete all breakpoints, give no argument.\n\
1a966eab 16330This command may be abbreviated \"delete\"."),
c906108c
SS
16331 &deletelist);
16332
1bedd215 16333 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16334Clear breakpoint at specified location.\n\
16335Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16336\n\
16337With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16338is executing in.\n"
16339"\n" LOCATION_HELP_STRING "\n\
1bedd215 16340See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16341 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16342
1bedd215 16343 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16344Set breakpoint at specified location.\n"
31e2b00f 16345BREAK_ARGS_HELP ("break")));
5ba2abeb 16346 set_cmd_completer (c, location_completer);
c94fdfd0 16347
c906108c
SS
16348 add_com_alias ("b", "break", class_run, 1);
16349 add_com_alias ("br", "break", class_run, 1);
16350 add_com_alias ("bre", "break", class_run, 1);
16351 add_com_alias ("brea", "break", class_run, 1);
16352
c906108c
SS
16353 if (dbx_commands)
16354 {
1bedd215
AC
16355 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16356Break in function/address or break at a line in the current file."),
c5aa993b
JM
16357 &stoplist, "stop ", 1, &cmdlist);
16358 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16359 _("Break in function or address."), &stoplist);
c5aa993b 16360 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16361 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16362 add_com ("status", class_info, breakpoints_info, _("\
16363Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16364The \"Type\" column indicates one of:\n\
16365\tbreakpoint - normal breakpoint\n\
16366\twatchpoint - watchpoint\n\
16367The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16368the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16369breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16370address and file/line number respectively.\n\
16371\n\
16372Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16373are set to the address of the last breakpoint listed unless the command\n\
16374is prefixed with \"server \".\n\n\
c906108c 16375Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16376breakpoint set."));
c906108c
SS
16377 }
16378
1bedd215 16379 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16380Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16381The \"Type\" column indicates one of:\n\
16382\tbreakpoint - normal breakpoint\n\
16383\twatchpoint - watchpoint\n\
16384The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16385the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16386breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16387address and file/line number respectively.\n\
16388\n\
16389Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16390are set to the address of the last breakpoint listed unless the command\n\
16391is prefixed with \"server \".\n\n\
c906108c 16392Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16393breakpoint set."));
c906108c 16394
6b04bdb7
MS
16395 add_info_alias ("b", "breakpoints", 1);
16396
1a966eab
AC
16397 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16398Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16399The \"Type\" column indicates one of:\n\
16400\tbreakpoint - normal breakpoint\n\
16401\twatchpoint - watchpoint\n\
16402\tlongjmp - internal breakpoint used to step through longjmp()\n\
16403\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16404\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16405\tfinish - internal breakpoint used by the \"finish\" command\n\
16406The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16407the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16408breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16409address and file/line number respectively.\n\
16410\n\
16411Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16412are set to the address of the last breakpoint listed unless the command\n\
16413is prefixed with \"server \".\n\n\
c906108c 16414Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16415breakpoint set."),
c906108c
SS
16416 &maintenanceinfolist);
16417
44feb3ce
TT
16418 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16419Set catchpoints to catch events."),
16420 &catch_cmdlist, "catch ",
16421 0/*allow-unknown*/, &cmdlist);
16422
16423 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16424Set temporary catchpoints to catch events."),
16425 &tcatch_cmdlist, "tcatch ",
16426 0/*allow-unknown*/, &cmdlist);
16427
44feb3ce
TT
16428 add_catch_command ("fork", _("Catch calls to fork."),
16429 catch_fork_command_1,
a96d9b2e 16430 NULL,
44feb3ce
TT
16431 (void *) (uintptr_t) catch_fork_permanent,
16432 (void *) (uintptr_t) catch_fork_temporary);
16433 add_catch_command ("vfork", _("Catch calls to vfork."),
16434 catch_fork_command_1,
a96d9b2e 16435 NULL,
44feb3ce
TT
16436 (void *) (uintptr_t) catch_vfork_permanent,
16437 (void *) (uintptr_t) catch_vfork_temporary);
16438 add_catch_command ("exec", _("Catch calls to exec."),
16439 catch_exec_command_1,
a96d9b2e
SDJ
16440 NULL,
16441 CATCH_PERMANENT,
16442 CATCH_TEMPORARY);
edcc5120
TT
16443 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16444Usage: catch load [REGEX]\n\
16445If REGEX is given, only stop for libraries matching the regular expression."),
16446 catch_load_command_1,
16447 NULL,
16448 CATCH_PERMANENT,
16449 CATCH_TEMPORARY);
16450 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16451Usage: catch unload [REGEX]\n\
16452If REGEX is given, only stop for libraries matching the regular expression."),
16453 catch_unload_command_1,
16454 NULL,
16455 CATCH_PERMANENT,
16456 CATCH_TEMPORARY);
c5aa993b 16457
1bedd215
AC
16458 c = add_com ("watch", class_breakpoint, watch_command, _("\
16459Set a watchpoint for an expression.\n\
06a64a0b 16460Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16461A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16462an expression changes.\n\
16463If -l or -location is given, this evaluates EXPRESSION and watches\n\
16464the memory to which it refers."));
65d12d83 16465 set_cmd_completer (c, expression_completer);
c906108c 16466
1bedd215
AC
16467 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16468Set a read watchpoint for an expression.\n\
06a64a0b 16469Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16470A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16471an expression is read.\n\
16472If -l or -location is given, this evaluates EXPRESSION and watches\n\
16473the memory to which it refers."));
65d12d83 16474 set_cmd_completer (c, expression_completer);
c906108c 16475
1bedd215
AC
16476 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16477Set a watchpoint for an expression.\n\
06a64a0b 16478Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16479A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16480an expression is either read or written.\n\
16481If -l or -location is given, this evaluates EXPRESSION and watches\n\
16482the memory to which it refers."));
65d12d83 16483 set_cmd_completer (c, expression_completer);
c906108c 16484
d77f58be 16485 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16486Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16487
920d2a44
AC
16488 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16489 respond to changes - contrary to the description. */
85c07804
AC
16490 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16491 &can_use_hw_watchpoints, _("\
16492Set debugger's willingness to use watchpoint hardware."), _("\
16493Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16494If zero, gdb will not use hardware for new watchpoints, even if\n\
16495such is available. (However, any hardware watchpoints that were\n\
16496created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16497hardware.)"),
16498 NULL,
920d2a44 16499 show_can_use_hw_watchpoints,
85c07804 16500 &setlist, &showlist);
c906108c
SS
16501
16502 can_use_hw_watchpoints = 1;
fa8d40ab 16503
1042e4c0
SS
16504 /* Tracepoint manipulation commands. */
16505
16506 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16507Set a tracepoint at specified location.\n\
1042e4c0
SS
16508\n"
16509BREAK_ARGS_HELP ("trace") "\n\
16510Do \"help tracepoints\" for info on other tracepoint commands."));
16511 set_cmd_completer (c, location_completer);
16512
16513 add_com_alias ("tp", "trace", class_alias, 0);
16514 add_com_alias ("tr", "trace", class_alias, 1);
16515 add_com_alias ("tra", "trace", class_alias, 1);
16516 add_com_alias ("trac", "trace", class_alias, 1);
16517
7a697b8d 16518 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16519Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16520\n"
16521BREAK_ARGS_HELP ("ftrace") "\n\
16522Do \"help tracepoints\" for info on other tracepoint commands."));
16523 set_cmd_completer (c, location_completer);
16524
0fb4aa4b 16525 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16526Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16527\n\
16528strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16529LOCATION may be a linespec, explicit, or address location (described below) \n\
16530or -m MARKER_ID.\n\n\
16531If a marker id is specified, probe the marker with that name. With\n\
16532no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16533Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16534This collects arbitrary user data passed in the probe point call to the\n\
16535tracing library. You can inspect it when analyzing the trace buffer,\n\
16536by printing the $_sdata variable like any other convenience variable.\n\
16537\n\
16538CONDITION is a boolean expression.\n\
629500fa 16539\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16540Multiple tracepoints at one place are permitted, and useful if their\n\
16541conditions are different.\n\
0fb4aa4b
PA
16542\n\
16543Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16544Do \"help tracepoints\" for info on other tracepoint commands."));
16545 set_cmd_completer (c, location_completer);
16546
1042e4c0 16547 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16548Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16549Convenience variable \"$tpnum\" contains the number of the\n\
16550last tracepoint set."));
16551
16552 add_info_alias ("tp", "tracepoints", 1);
16553
16554 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16555Delete specified tracepoints.\n\
16556Arguments are tracepoint numbers, separated by spaces.\n\
16557No argument means delete all tracepoints."),
16558 &deletelist);
7e20dfcd 16559 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16560
16561 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16562Disable specified tracepoints.\n\
16563Arguments are tracepoint numbers, separated by spaces.\n\
16564No argument means disable all tracepoints."),
16565 &disablelist);
16566 deprecate_cmd (c, "disable");
16567
16568 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16569Enable specified tracepoints.\n\
16570Arguments are tracepoint numbers, separated by spaces.\n\
16571No argument means enable all tracepoints."),
16572 &enablelist);
16573 deprecate_cmd (c, "enable");
16574
16575 add_com ("passcount", class_trace, trace_pass_command, _("\
16576Set the passcount for a tracepoint.\n\
16577The trace will end when the tracepoint has been passed 'count' times.\n\
16578Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16579if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16580
6149aea9
PA
16581 add_prefix_cmd ("save", class_breakpoint, save_command,
16582 _("Save breakpoint definitions as a script."),
16583 &save_cmdlist, "save ",
16584 0/*allow-unknown*/, &cmdlist);
16585
16586 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16587Save current breakpoint definitions as a script.\n\
cce7e648 16588This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16589catchpoints, tracepoints). Use the 'source' command in another debug\n\
16590session to restore them."),
16591 &save_cmdlist);
16592 set_cmd_completer (c, filename_completer);
16593
16594 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16595Save current tracepoint definitions as a script.\n\
6149aea9
PA
16596Use the 'source' command in another debug session to restore them."),
16597 &save_cmdlist);
1042e4c0
SS
16598 set_cmd_completer (c, filename_completer);
16599
6149aea9
PA
16600 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16601 deprecate_cmd (c, "save tracepoints");
16602
1bedd215 16603 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16604Breakpoint specific settings\n\
16605Configure various breakpoint-specific variables such as\n\
1bedd215 16606pending breakpoint behavior"),
fa8d40ab
JJ
16607 &breakpoint_set_cmdlist, "set breakpoint ",
16608 0/*allow-unknown*/, &setlist);
1bedd215 16609 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16610Breakpoint specific settings\n\
16611Configure various breakpoint-specific variables such as\n\
1bedd215 16612pending breakpoint behavior"),
fa8d40ab
JJ
16613 &breakpoint_show_cmdlist, "show breakpoint ",
16614 0/*allow-unknown*/, &showlist);
16615
7915a72c
AC
16616 add_setshow_auto_boolean_cmd ("pending", no_class,
16617 &pending_break_support, _("\
16618Set debugger's behavior regarding pending breakpoints."), _("\
16619Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16620If on, an unrecognized breakpoint location will cause gdb to create a\n\
16621pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16622an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16623user-query to see if a pending breakpoint should be created."),
2c5b56ce 16624 NULL,
920d2a44 16625 show_pending_break_support,
6e1d7d6c
AC
16626 &breakpoint_set_cmdlist,
16627 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16628
16629 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16630
16631 add_setshow_boolean_cmd ("auto-hw", no_class,
16632 &automatic_hardware_breakpoints, _("\
16633Set automatic usage of hardware breakpoints."), _("\
16634Show automatic usage of hardware breakpoints."), _("\
16635If set, the debugger will automatically use hardware breakpoints for\n\
16636breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16637a warning will be emitted for such breakpoints."),
16638 NULL,
16639 show_automatic_hardware_breakpoints,
16640 &breakpoint_set_cmdlist,
16641 &breakpoint_show_cmdlist);
74960c60 16642
a25a5a45
PA
16643 add_setshow_boolean_cmd ("always-inserted", class_support,
16644 &always_inserted_mode, _("\
74960c60
VP
16645Set mode for inserting breakpoints."), _("\
16646Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16647When this mode is on, breakpoints are inserted immediately as soon as\n\
16648they're created, kept inserted even when execution stops, and removed\n\
16649only when the user deletes them. When this mode is off (the default),\n\
16650breakpoints are inserted only when execution continues, and removed\n\
16651when execution stops."),
72d0e2c5
YQ
16652 NULL,
16653 &show_always_inserted_mode,
16654 &breakpoint_set_cmdlist,
16655 &breakpoint_show_cmdlist);
f1310107 16656
b775012e
LM
16657 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16658 condition_evaluation_enums,
16659 &condition_evaluation_mode_1, _("\
16660Set mode of breakpoint condition evaluation."), _("\
16661Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16662When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16663evaluated on the host's side by GDB. When it is set to \"target\",\n\
16664breakpoint conditions will be downloaded to the target (if the target\n\
16665supports such feature) and conditions will be evaluated on the target's side.\n\
16666If this is set to \"auto\" (default), this will be automatically set to\n\
16667\"target\" if it supports condition evaluation, otherwise it will\n\
16668be set to \"gdb\""),
16669 &set_condition_evaluation_mode,
16670 &show_condition_evaluation_mode,
16671 &breakpoint_set_cmdlist,
16672 &breakpoint_show_cmdlist);
16673
f1310107
TJB
16674 add_com ("break-range", class_breakpoint, break_range_command, _("\
16675Set a breakpoint for an address range.\n\
16676break-range START-LOCATION, END-LOCATION\n\
16677where START-LOCATION and END-LOCATION can be one of the following:\n\
16678 LINENUM, for that line in the current file,\n\
16679 FILE:LINENUM, for that line in that file,\n\
16680 +OFFSET, for that number of lines after the current line\n\
16681 or the start of the range\n\
16682 FUNCTION, for the first line in that function,\n\
16683 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16684 *ADDRESS, for the instruction at that address.\n\
16685\n\
16686The breakpoint will stop execution of the inferior whenever it executes\n\
16687an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16688range (including START-LOCATION and END-LOCATION)."));
16689
e7e0cddf 16690 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16691Set a dynamic printf at specified location.\n\
e7e0cddf 16692dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16693location may be a linespec, explicit, or address location.\n"
16694"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16695 set_cmd_completer (c, location_completer);
16696
16697 add_setshow_enum_cmd ("dprintf-style", class_support,
16698 dprintf_style_enums, &dprintf_style, _("\
16699Set the style of usage for dynamic printf."), _("\
16700Show the style of usage for dynamic printf."), _("\
16701This setting chooses how GDB will do a dynamic printf.\n\
16702If the value is \"gdb\", then the printing is done by GDB to its own\n\
16703console, as with the \"printf\" command.\n\
16704If the value is \"call\", the print is done by calling a function in your\n\
16705program; by default printf(), but you can choose a different function or\n\
16706output stream by setting dprintf-function and dprintf-channel."),
16707 update_dprintf_commands, NULL,
16708 &setlist, &showlist);
16709
16710 dprintf_function = xstrdup ("printf");
16711 add_setshow_string_cmd ("dprintf-function", class_support,
16712 &dprintf_function, _("\
16713Set the function to use for dynamic printf"), _("\
16714Show the function to use for dynamic printf"), NULL,
16715 update_dprintf_commands, NULL,
16716 &setlist, &showlist);
16717
16718 dprintf_channel = xstrdup ("");
16719 add_setshow_string_cmd ("dprintf-channel", class_support,
16720 &dprintf_channel, _("\
16721Set the channel to use for dynamic printf"), _("\
16722Show the channel to use for dynamic printf"), NULL,
16723 update_dprintf_commands, NULL,
16724 &setlist, &showlist);
16725
d3ce09f5
SS
16726 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16727 &disconnected_dprintf, _("\
16728Set whether dprintf continues after GDB disconnects."), _("\
16729Show whether dprintf continues after GDB disconnects."), _("\
16730Use this to let dprintf commands continue to hit and produce output\n\
16731even if GDB disconnects or detaches from the target."),
16732 NULL,
16733 NULL,
16734 &setlist, &showlist);
16735
16736 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16737agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16738(target agent only) This is useful for formatted output in user-defined commands."));
16739
765dc015 16740 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16741
16742 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16743 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16744}