S390: Hardware breakpoint support
[deliverable/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"
104c1213 83
e7e8980f
YQ
84/* Enums for exception-handling support. */
85enum exception_event_kind
86{
87 EX_EVENT_THROW,
591f19e8 88 EX_EVENT_RETHROW,
e7e8980f
YQ
89 EX_EVENT_CATCH
90};
91
4a64f543 92/* Prototypes for local functions. */
c906108c 93
a14ed312 94static void enable_delete_command (char *, int);
c906108c 95
a14ed312 96static void enable_once_command (char *, int);
c906108c 97
816338b5
SS
98static void enable_count_command (char *, int);
99
a14ed312 100static void disable_command (char *, int);
c906108c 101
a14ed312 102static void enable_command (char *, int);
c906108c 103
95a42b64
TT
104static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
c906108c 107
a14ed312 108static void ignore_command (char *, int);
c906108c 109
4efb68b1 110static int breakpoint_re_set_one (void *);
c906108c 111
348d480f
PA
112static void breakpoint_re_set_default (struct breakpoint *);
113
f00aae0f
KS
114static void
115 create_sals_from_location_default (const struct event_location *location,
116 struct linespec_result *canonical,
117 enum bptype type_wanted);
983af33b
SDJ
118
119static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
e7e0cddf 121 char *, char *, enum bptype,
983af33b
SDJ
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
44f238bb 125 int, int, int, unsigned);
983af33b 126
f00aae0f
KS
127static void decode_location_default (struct breakpoint *b,
128 const struct event_location *location,
c2f4122d 129 struct program_space *search_pspace,
f00aae0f 130 struct symtabs_and_lines *sals);
983af33b 131
a14ed312 132static void clear_command (char *, int);
c906108c 133
a14ed312 134static void catch_command (char *, int);
c906108c 135
a9634178 136static int can_use_hardware_watchpoint (struct value *);
c906108c 137
98deb0da 138static void break_command_1 (char *, int, int);
c906108c 139
a14ed312 140static void mention (struct breakpoint *);
c906108c 141
348d480f
PA
142static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
143 enum bptype,
c0a91b2b 144 const struct breakpoint_ops *);
3742cc8b
YQ
145static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
146 const struct symtab_and_line *);
147
4a64f543
MS
148/* This function is used in gdbtk sources and thus can not be made
149 static. */
63c252f8 150struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 151 struct symtab_and_line,
c0a91b2b
TT
152 enum bptype,
153 const struct breakpoint_ops *);
c906108c 154
06edf0c0
PA
155static struct breakpoint *
156 momentary_breakpoint_from_master (struct breakpoint *orig,
157 enum bptype type,
a1aa2221
LM
158 const struct breakpoint_ops *ops,
159 int loc_enabled);
06edf0c0 160
76897487
KB
161static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
162
a6d9a66e
UW
163static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
164 CORE_ADDR bpaddr,
88f7da05 165 enum bptype bptype);
76897487 166
6c95b8df
PA
167static void describe_other_breakpoints (struct gdbarch *,
168 struct program_space *, CORE_ADDR,
5af949e3 169 struct obj_section *, int);
c906108c 170
85d721b8
PA
171static int watchpoint_locations_match (struct bp_location *loc1,
172 struct bp_location *loc2);
173
f1310107
TJB
174static int breakpoint_location_address_match (struct bp_location *bl,
175 struct address_space *aspace,
176 CORE_ADDR addr);
177
d35ae833
PA
178static int breakpoint_location_address_range_overlap (struct bp_location *,
179 struct address_space *,
180 CORE_ADDR, int);
181
a14ed312 182static void breakpoints_info (char *, int);
c906108c 183
d77f58be
SS
184static void watchpoints_info (char *, int);
185
e5a67952
MS
186static int breakpoint_1 (char *, int,
187 int (*) (const struct breakpoint *));
c906108c 188
4efb68b1 189static int breakpoint_cond_eval (void *);
c906108c 190
4efb68b1 191static void cleanup_executing_breakpoints (void *);
c906108c 192
a14ed312 193static void commands_command (char *, int);
c906108c 194
a14ed312 195static void condition_command (char *, int);
c906108c 196
834c0d03 197static int remove_breakpoint (struct bp_location *);
b2b6a7da 198static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 199
e514a9d6 200static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 201
4efb68b1 202static int watchpoint_check (void *);
c906108c 203
a14ed312 204static void maintenance_info_breakpoints (char *, int);
c906108c 205
a14ed312 206static int hw_breakpoint_used_count (void);
c906108c 207
a1398e0c
PA
208static int hw_watchpoint_use_count (struct breakpoint *);
209
210static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
c906108c 213
a14ed312 214static void hbreak_command (char *, int);
c906108c 215
a14ed312 216static void thbreak_command (char *, int);
c906108c 217
816338b5
SS
218static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
c906108c 220
a14ed312 221static void stop_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopin_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopat_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void tcatch_command (char *arg, int from_tty);
7a292a7a 228
fe3f5fa8 229static void free_bp_location (struct bp_location *loc);
f431efe5
PA
230static void incref_bp_location (struct bp_location *loc);
231static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 232
39d61571 233static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 234
44702360
PA
235/* update_global_location_list's modes of operation wrt to whether to
236 insert locations now. */
237enum ugll_insert_mode
238{
239 /* Don't insert any breakpoint locations into the inferior, only
240 remove already-inserted locations that no longer should be
241 inserted. Functions that delete a breakpoint or breakpoints
242 should specify this mode, so that deleting a breakpoint doesn't
243 have the side effect of inserting the locations of other
244 breakpoints that are marked not-inserted, but should_be_inserted
245 returns true on them.
246
247 This behavior is useful is situations close to tear-down -- e.g.,
248 after an exec, while the target still has execution, but
249 breakpoint shadows of the previous executable image should *NOT*
250 be restored to the new image; or before detaching, where the
251 target still has execution and wants to delete breakpoints from
252 GDB's lists, and all breakpoints had already been removed from
253 the inferior. */
254 UGLL_DONT_INSERT,
255
a25a5a45
PA
256 /* May insert breakpoints iff breakpoints_should_be_inserted_now
257 claims breakpoints should be inserted now. */
04086b45
PA
258 UGLL_MAY_INSERT,
259
a25a5a45
PA
260 /* Insert locations now, irrespective of
261 breakpoints_should_be_inserted_now. E.g., say all threads are
262 stopped right now, and the user did "continue". We need to
263 insert breakpoints _before_ resuming the target, but
264 UGLL_MAY_INSERT wouldn't insert them, because
265 breakpoints_should_be_inserted_now returns false at that point,
266 as no thread is running yet. */
04086b45 267 UGLL_INSERT
44702360
PA
268};
269
270static void update_global_location_list (enum ugll_insert_mode);
a5606eee 271
44702360 272static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 273
d77f58be 274static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
275
276static void insert_breakpoint_locations (void);
a5606eee 277
1042e4c0
SS
278static void tracepoints_info (char *, int);
279
280static void delete_trace_command (char *, int);
281
282static void enable_trace_command (char *, int);
283
284static void disable_trace_command (char *, int);
285
286static void trace_pass_command (char *, int);
287
558a9d82
YQ
288static void set_tracepoint_count (int num);
289
9c06b0b4
TJB
290static int is_masked_watchpoint (const struct breakpoint *b);
291
b775012e
LM
292static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
293
983af33b
SDJ
294/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
295 otherwise. */
296
297static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 298
2060206e
PA
299/* The breakpoint_ops structure to be inherited by all breakpoint_ops
300 that are implemented on top of software or hardware breakpoints
301 (user breakpoints, internal and momentary breakpoints, etc.). */
302static struct breakpoint_ops bkpt_base_breakpoint_ops;
303
304/* Internal breakpoints class type. */
06edf0c0 305static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
306
307/* Momentary breakpoints class type. */
06edf0c0
PA
308static struct breakpoint_ops momentary_breakpoint_ops;
309
e2e4d78b
JK
310/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
311static struct breakpoint_ops longjmp_breakpoint_ops;
312
2060206e
PA
313/* The breakpoint_ops structure to be used in regular user created
314 breakpoints. */
315struct breakpoint_ops bkpt_breakpoint_ops;
316
55aa24fb
SDJ
317/* Breakpoints set on probes. */
318static struct breakpoint_ops bkpt_probe_breakpoint_ops;
319
e7e0cddf 320/* Dynamic printf class type. */
c5867ab6 321struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 322
d3ce09f5
SS
323/* The style in which to perform a dynamic printf. This is a user
324 option because different output options have different tradeoffs;
325 if GDB does the printing, there is better error handling if there
326 is a problem with any of the arguments, but using an inferior
327 function lets you have special-purpose printers and sending of
328 output to the same place as compiled-in print functions. */
329
330static const char dprintf_style_gdb[] = "gdb";
331static const char dprintf_style_call[] = "call";
332static const char dprintf_style_agent[] = "agent";
333static const char *const dprintf_style_enums[] = {
334 dprintf_style_gdb,
335 dprintf_style_call,
336 dprintf_style_agent,
337 NULL
338};
339static const char *dprintf_style = dprintf_style_gdb;
340
341/* The function to use for dynamic printf if the preferred style is to
342 call into the inferior. The value is simply a string that is
343 copied into the command, so it can be anything that GDB can
344 evaluate to a callable address, not necessarily a function name. */
345
346static char *dprintf_function = "";
347
348/* The channel to use for dynamic printf if the preferred style is to
349 call into the inferior; if a nonempty string, it will be passed to
350 the call as the first argument, with the format string as the
351 second. As with the dprintf function, this can be anything that
352 GDB knows how to evaluate, so in addition to common choices like
353 "stderr", this could be an app-specific expression like
354 "mystreams[curlogger]". */
355
356static char *dprintf_channel = "";
357
358/* True if dprintf commands should continue to operate even if GDB
359 has disconnected. */
360static int disconnected_dprintf = 1;
361
5cea2a26
PA
362/* A reference-counted struct command_line. This lets multiple
363 breakpoints share a single command list. */
364struct counted_command_line
365{
366 /* The reference count. */
367 int refc;
368
369 /* The command list. */
370 struct command_line *commands;
371};
372
373struct command_line *
374breakpoint_commands (struct breakpoint *b)
375{
376 return b->commands ? b->commands->commands : NULL;
377}
3daf8fe5 378
f3b1572e
PA
379/* Flag indicating that a command has proceeded the inferior past the
380 current breakpoint. */
381
382static int breakpoint_proceeded;
383
956a9fb9 384const char *
2cec12e5
AR
385bpdisp_text (enum bpdisp disp)
386{
4a64f543
MS
387 /* NOTE: the following values are a part of MI protocol and
388 represent values of 'disp' field returned when inferior stops at
389 a breakpoint. */
bc043ef3 390 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 391
2cec12e5
AR
392 return bpdisps[(int) disp];
393}
c906108c 394
4a64f543 395/* Prototypes for exported functions. */
c906108c 396/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 397 if such is available. */
c906108c
SS
398static int can_use_hw_watchpoints;
399
920d2a44
AC
400static void
401show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *value)
404{
3e43a32a
MS
405 fprintf_filtered (file,
406 _("Debugger's willingness to use "
407 "watchpoint hardware is %s.\n"),
920d2a44
AC
408 value);
409}
410
fa8d40ab
JJ
411/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
412 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 413 for unrecognized breakpoint locations.
fa8d40ab
JJ
414 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
415static enum auto_boolean pending_break_support;
920d2a44
AC
416static void
417show_pending_break_support (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420{
3e43a32a
MS
421 fprintf_filtered (file,
422 _("Debugger's behavior regarding "
423 "pending breakpoints is %s.\n"),
920d2a44
AC
424 value);
425}
fa8d40ab 426
765dc015 427/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 428 set with "break" but falling in read-only memory.
765dc015
VP
429 If 0, gdb will warn about such breakpoints, but won't automatically
430 use hardware breakpoints. */
431static int automatic_hardware_breakpoints;
432static void
433show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c,
435 const char *value)
436{
3e43a32a
MS
437 fprintf_filtered (file,
438 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
439 value);
440}
441
a25a5a45
PA
442/* If on, GDB keeps breakpoints inserted even if the inferior is
443 stopped, and immediately inserts any new breakpoints as soon as
444 they're created. If off (default), GDB keeps breakpoints off of
445 the target as long as possible. That is, it delays inserting
446 breakpoints until the next resume, and removes them again when the
447 target fully stops. This is a bit safer in case GDB crashes while
448 processing user input. */
449static int always_inserted_mode = 0;
72d0e2c5 450
33e5cbd6 451static void
74960c60 452show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 453 struct cmd_list_element *c, const char *value)
74960c60 454{
a25a5a45
PA
455 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
456 value);
74960c60
VP
457}
458
b57bacec
PA
459/* See breakpoint.h. */
460
33e5cbd6 461int
a25a5a45 462breakpoints_should_be_inserted_now (void)
33e5cbd6 463{
a25a5a45
PA
464 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
465 {
466 /* If breakpoints are global, they should be inserted even if no
467 thread under gdb's control is running, or even if there are
468 no threads under GDB's control yet. */
469 return 1;
470 }
471 else if (target_has_execution)
472 {
372316f1
PA
473 struct thread_info *tp;
474
a25a5a45
PA
475 if (always_inserted_mode)
476 {
477 /* The user wants breakpoints inserted even if all threads
478 are stopped. */
479 return 1;
480 }
481
b57bacec
PA
482 if (threads_are_executing ())
483 return 1;
372316f1
PA
484
485 /* Don't remove breakpoints yet if, even though all threads are
486 stopped, we still have events to process. */
487 ALL_NON_EXITED_THREADS (tp)
488 if (tp->resumed
489 && tp->suspend.waitstatus_pending_p)
490 return 1;
a25a5a45
PA
491 }
492 return 0;
33e5cbd6 493}
765dc015 494
b775012e
LM
495static const char condition_evaluation_both[] = "host or target";
496
497/* Modes for breakpoint condition evaluation. */
498static const char condition_evaluation_auto[] = "auto";
499static const char condition_evaluation_host[] = "host";
500static const char condition_evaluation_target[] = "target";
501static const char *const condition_evaluation_enums[] = {
502 condition_evaluation_auto,
503 condition_evaluation_host,
504 condition_evaluation_target,
505 NULL
506};
507
508/* Global that holds the current mode for breakpoint condition evaluation. */
509static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
510
511/* Global that we use to display information to the user (gets its value from
512 condition_evaluation_mode_1. */
513static const char *condition_evaluation_mode = condition_evaluation_auto;
514
515/* Translate a condition evaluation mode MODE into either "host"
516 or "target". This is used mostly to translate from "auto" to the
517 real setting that is being used. It returns the translated
518 evaluation mode. */
519
520static const char *
521translate_condition_evaluation_mode (const char *mode)
522{
523 if (mode == condition_evaluation_auto)
524 {
525 if (target_supports_evaluation_of_breakpoint_conditions ())
526 return condition_evaluation_target;
527 else
528 return condition_evaluation_host;
529 }
530 else
531 return mode;
532}
533
534/* Discovers what condition_evaluation_auto translates to. */
535
536static const char *
537breakpoint_condition_evaluation_mode (void)
538{
539 return translate_condition_evaluation_mode (condition_evaluation_mode);
540}
541
542/* Return true if GDB should evaluate breakpoint conditions or false
543 otherwise. */
544
545static int
546gdb_evaluates_breakpoint_condition_p (void)
547{
548 const char *mode = breakpoint_condition_evaluation_mode ();
549
550 return (mode == condition_evaluation_host);
551}
552
a14ed312 553void _initialize_breakpoint (void);
c906108c 554
c906108c
SS
555/* Are we executing breakpoint commands? */
556static int executing_breakpoint_commands;
557
c02f5703
MS
558/* Are overlay event breakpoints enabled? */
559static int overlay_events_enabled;
560
e09342b5
TJB
561/* See description in breakpoint.h. */
562int target_exact_watchpoints = 0;
563
c906108c 564/* Walk the following statement or block through all breakpoints.
e5dd4106 565 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 566 current breakpoint. */
c906108c 567
5c44784c 568#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 569
5c44784c
JM
570#define ALL_BREAKPOINTS_SAFE(B,TMP) \
571 for (B = breakpoint_chain; \
572 B ? (TMP=B->next, 1): 0; \
573 B = TMP)
c906108c 574
4a64f543
MS
575/* Similar iterator for the low-level breakpoints. SAFE variant is
576 not provided so update_global_location_list must not be called
577 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 578
876fa593
JK
579#define ALL_BP_LOCATIONS(B,BP_TMP) \
580 for (BP_TMP = bp_location; \
581 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
582 BP_TMP++)
7cc221ef 583
b775012e
LM
584/* Iterates through locations with address ADDRESS for the currently selected
585 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
586 to where the loop should start from.
587 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
588 appropriate location to start with. */
589
590#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
591 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
592 BP_LOCP_TMP = BP_LOCP_START; \
593 BP_LOCP_START \
594 && (BP_LOCP_TMP < bp_location + bp_location_count \
595 && (*BP_LOCP_TMP)->address == ADDRESS); \
596 BP_LOCP_TMP++)
597
1042e4c0
SS
598/* Iterator for tracepoints only. */
599
600#define ALL_TRACEPOINTS(B) \
601 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 602 if (is_tracepoint (B))
1042e4c0 603
7cc221ef 604/* Chains of all breakpoints defined. */
c906108c
SS
605
606struct breakpoint *breakpoint_chain;
607
876fa593
JK
608/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
609
610static struct bp_location **bp_location;
611
612/* Number of elements of BP_LOCATION. */
613
614static unsigned bp_location_count;
615
4a64f543
MS
616/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
617 ADDRESS for the current elements of BP_LOCATION which get a valid
618 result from bp_location_has_shadow. You can use it for roughly
619 limiting the subrange of BP_LOCATION to scan for shadow bytes for
620 an address you need to read. */
876fa593
JK
621
622static CORE_ADDR bp_location_placed_address_before_address_max;
623
4a64f543
MS
624/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
625 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
626 BP_LOCATION which get a valid result from bp_location_has_shadow.
627 You can use it for roughly limiting the subrange of BP_LOCATION to
628 scan for shadow bytes for an address you need to read. */
876fa593
JK
629
630static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 631
4a64f543
MS
632/* The locations that no longer correspond to any breakpoint, unlinked
633 from bp_location array, but for which a hit may still be reported
634 by a target. */
20874c92
VP
635VEC(bp_location_p) *moribund_locations = NULL;
636
c906108c
SS
637/* Number of last breakpoint made. */
638
95a42b64
TT
639static int breakpoint_count;
640
86b17b60
PA
641/* The value of `breakpoint_count' before the last command that
642 created breakpoints. If the last (break-like) command created more
643 than one breakpoint, then the difference between BREAKPOINT_COUNT
644 and PREV_BREAKPOINT_COUNT is more than one. */
645static int prev_breakpoint_count;
c906108c 646
1042e4c0
SS
647/* Number of last tracepoint made. */
648
95a42b64 649static int tracepoint_count;
1042e4c0 650
6149aea9
PA
651static struct cmd_list_element *breakpoint_set_cmdlist;
652static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 653struct cmd_list_element *save_cmdlist;
6149aea9 654
badd37ce
SDJ
655/* See declaration at breakpoint.h. */
656
657struct breakpoint *
658breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
659 void *user_data)
660{
661 struct breakpoint *b = NULL;
662
663 ALL_BREAKPOINTS (b)
664 {
665 if (func (b, user_data) != 0)
666 break;
667 }
668
669 return b;
670}
671
468d015d
JJ
672/* Return whether a breakpoint is an active enabled breakpoint. */
673static int
674breakpoint_enabled (struct breakpoint *b)
675{
0d381245 676 return (b->enable_state == bp_enabled);
468d015d
JJ
677}
678
c906108c
SS
679/* Set breakpoint count to NUM. */
680
95a42b64 681static void
fba45db2 682set_breakpoint_count (int num)
c906108c 683{
86b17b60 684 prev_breakpoint_count = breakpoint_count;
c906108c 685 breakpoint_count = num;
4fa62494 686 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
687}
688
86b17b60
PA
689/* Used by `start_rbreak_breakpoints' below, to record the current
690 breakpoint count before "rbreak" creates any breakpoint. */
691static int rbreak_start_breakpoint_count;
692
95a42b64
TT
693/* Called at the start an "rbreak" command to record the first
694 breakpoint made. */
86b17b60 695
95a42b64
TT
696void
697start_rbreak_breakpoints (void)
698{
86b17b60 699 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
700}
701
702/* Called at the end of an "rbreak" command to record the last
703 breakpoint made. */
86b17b60 704
95a42b64
TT
705void
706end_rbreak_breakpoints (void)
707{
86b17b60 708 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
709}
710
4a64f543 711/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
712
713void
fba45db2 714clear_breakpoint_hit_counts (void)
c906108c
SS
715{
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 b->hit_count = 0;
720}
721
9add0f1b
TT
722/* Allocate a new counted_command_line with reference count of 1.
723 The new structure owns COMMANDS. */
724
725static struct counted_command_line *
726alloc_counted_command_line (struct command_line *commands)
727{
8d749320 728 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 729
9add0f1b
TT
730 result->refc = 1;
731 result->commands = commands;
8d749320 732
9add0f1b
TT
733 return result;
734}
735
736/* Increment reference count. This does nothing if CMD is NULL. */
737
738static void
739incref_counted_command_line (struct counted_command_line *cmd)
740{
741 if (cmd)
742 ++cmd->refc;
743}
744
745/* Decrement reference count. If the reference count reaches 0,
746 destroy the counted_command_line. Sets *CMDP to NULL. This does
747 nothing if *CMDP is NULL. */
748
749static void
750decref_counted_command_line (struct counted_command_line **cmdp)
751{
752 if (*cmdp)
753 {
754 if (--(*cmdp)->refc == 0)
755 {
756 free_command_lines (&(*cmdp)->commands);
757 xfree (*cmdp);
758 }
759 *cmdp = NULL;
760 }
761}
762
763/* A cleanup function that calls decref_counted_command_line. */
764
765static void
766do_cleanup_counted_command_line (void *arg)
767{
9a3c8263 768 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
769}
770
771/* Create a cleanup that calls decref_counted_command_line on the
772 argument. */
773
774static struct cleanup *
775make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
776{
777 return make_cleanup (do_cleanup_counted_command_line, cmdp);
778}
779
c906108c 780\f
48cb2d85
VP
781/* Return the breakpoint with the specified number, or NULL
782 if the number does not refer to an existing breakpoint. */
783
784struct breakpoint *
785get_breakpoint (int num)
786{
787 struct breakpoint *b;
788
789 ALL_BREAKPOINTS (b)
790 if (b->number == num)
791 return b;
792
793 return NULL;
794}
5c44784c 795
c906108c 796\f
adc36818 797
b775012e
LM
798/* Mark locations as "conditions have changed" in case the target supports
799 evaluating conditions on its side. */
800
801static void
802mark_breakpoint_modified (struct breakpoint *b)
803{
804 struct bp_location *loc;
805
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812 return;
813
814 if (!is_breakpoint (b))
815 return;
816
817 for (loc = b->loc; loc; loc = loc->next)
818 loc->condition_changed = condition_modified;
819}
820
821/* Mark location as "conditions have changed" in case the target supports
822 evaluating conditions on its side. */
823
824static void
825mark_breakpoint_location_modified (struct bp_location *loc)
826{
827 /* This is only meaningful if the target is
828 evaluating conditions and if the user has
829 opted for condition evaluation on the target's
830 side. */
831 if (gdb_evaluates_breakpoint_condition_p ()
832 || !target_supports_evaluation_of_breakpoint_conditions ())
833
834 return;
835
836 if (!is_breakpoint (loc->owner))
837 return;
838
839 loc->condition_changed = condition_modified;
840}
841
842/* Sets the condition-evaluation mode using the static global
843 condition_evaluation_mode. */
844
845static void
846set_condition_evaluation_mode (char *args, int from_tty,
847 struct cmd_list_element *c)
848{
b775012e
LM
849 const char *old_mode, *new_mode;
850
851 if ((condition_evaluation_mode_1 == condition_evaluation_target)
852 && !target_supports_evaluation_of_breakpoint_conditions ())
853 {
854 condition_evaluation_mode_1 = condition_evaluation_mode;
855 warning (_("Target does not support breakpoint condition evaluation.\n"
856 "Using host evaluation mode instead."));
857 return;
858 }
859
860 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
861 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
862
abf1152a
JK
863 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
864 settings was "auto". */
865 condition_evaluation_mode = condition_evaluation_mode_1;
866
b775012e
LM
867 /* Only update the mode if the user picked a different one. */
868 if (new_mode != old_mode)
869 {
870 struct bp_location *loc, **loc_tmp;
871 /* If the user switched to a different evaluation mode, we
872 need to synch the changes with the target as follows:
873
874 "host" -> "target": Send all (valid) conditions to the target.
875 "target" -> "host": Remove all the conditions from the target.
876 */
877
b775012e
LM
878 if (new_mode == condition_evaluation_target)
879 {
880 /* Mark everything modified and synch conditions with the
881 target. */
882 ALL_BP_LOCATIONS (loc, loc_tmp)
883 mark_breakpoint_location_modified (loc);
884 }
885 else
886 {
887 /* Manually mark non-duplicate locations to synch conditions
888 with the target. We do this to remove all the conditions the
889 target knows about. */
890 ALL_BP_LOCATIONS (loc, loc_tmp)
891 if (is_breakpoint (loc->owner) && loc->inserted)
892 loc->needs_update = 1;
893 }
894
895 /* Do the update. */
44702360 896 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
897 }
898
899 return;
900}
901
902/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
903 what "auto" is translating to. */
904
905static void
906show_condition_evaluation_mode (struct ui_file *file, int from_tty,
907 struct cmd_list_element *c, const char *value)
908{
909 if (condition_evaluation_mode == condition_evaluation_auto)
910 fprintf_filtered (file,
911 _("Breakpoint condition evaluation "
912 "mode is %s (currently %s).\n"),
913 value,
914 breakpoint_condition_evaluation_mode ());
915 else
916 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
917 value);
918}
919
920/* A comparison function for bp_location AP and BP that is used by
921 bsearch. This comparison function only cares about addresses, unlike
922 the more general bp_location_compare function. */
923
924static int
925bp_location_compare_addrs (const void *ap, const void *bp)
926{
9a3c8263
SM
927 const struct bp_location *a = *(const struct bp_location **) ap;
928 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
929
930 if (a->address == b->address)
931 return 0;
932 else
933 return ((a->address > b->address) - (a->address < b->address));
934}
935
936/* Helper function to skip all bp_locations with addresses
937 less than ADDRESS. It returns the first bp_location that
938 is greater than or equal to ADDRESS. If none is found, just
939 return NULL. */
940
941static struct bp_location **
942get_first_locp_gte_addr (CORE_ADDR address)
943{
944 struct bp_location dummy_loc;
945 struct bp_location *dummy_locp = &dummy_loc;
946 struct bp_location **locp_found = NULL;
947
948 /* Initialize the dummy location's address field. */
949 memset (&dummy_loc, 0, sizeof (struct bp_location));
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
953 locp_found = ((struct bp_location **)
954 bsearch (&dummy_locp, bp_location, bp_location_count,
955 sizeof (struct bp_location **),
956 bp_location_compare_addrs));
b775012e
LM
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
964 while ((locp_found - 1) >= bp_location
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969}
970
adc36818 971void
7a26bd4d 972set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
973 int from_tty)
974{
3a5c3e22
PA
975 xfree (b->cond_string);
976 b->cond_string = NULL;
adc36818 977
3a5c3e22 978 if (is_watchpoint (b))
adc36818 979 {
3a5c3e22
PA
980 struct watchpoint *w = (struct watchpoint *) b;
981
982 xfree (w->cond_exp);
983 w->cond_exp = NULL;
984 }
985 else
986 {
987 struct bp_location *loc;
988
989 for (loc = b->loc; loc; loc = loc->next)
990 {
991 xfree (loc->cond);
992 loc->cond = NULL;
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. */
1340 char *arg;
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
4a64f543
MS
1401commands_command_1 (char *arg, int from_tty,
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
1414 if (arg == NULL || !*arg)
1415 {
86b17b60 1416 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1417 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1418 breakpoint_count);
95a42b64
TT
1419 else if (breakpoint_count > 0)
1420 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1421 else
1422 {
1423 /* So that we don't try to free the incoming non-NULL
1424 argument in the cleanup below. Mapping breakpoint
1425 numbers will fail in this case. */
1426 arg = NULL;
1427 }
95a42b64 1428 }
9766ced4
SS
1429 else
1430 /* The command loop has some static state, so we need to preserve
1431 our argument. */
1432 arg = xstrdup (arg);
86b17b60
PA
1433
1434 if (arg != NULL)
1435 make_cleanup (xfree, arg);
1436
1437 info.arg = arg;
95a42b64
TT
1438
1439 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1440
1441 if (info.cmd == NULL)
1442 error (_("No breakpoints specified."));
1443
1444 do_cleanups (cleanups);
1445}
1446
1447static void
1448commands_command (char *arg, int from_tty)
1449{
1450 commands_command_1 (arg, from_tty, NULL);
c906108c 1451}
40c03ae8
EZ
1452
1453/* Like commands_command, but instead of reading the commands from
1454 input stream, takes them from an already parsed command structure.
1455
1456 This is used by cli-script.c to DTRT with breakpoint commands
1457 that are part of if and while bodies. */
1458enum command_control_type
1459commands_from_control_command (char *arg, struct command_line *cmd)
1460{
95a42b64
TT
1461 commands_command_1 (arg, 0, cmd);
1462 return simple_control;
40c03ae8 1463}
876fa593
JK
1464
1465/* Return non-zero if BL->TARGET_INFO contains valid information. */
1466
1467static int
1468bp_location_has_shadow (struct bp_location *bl)
1469{
1470 if (bl->loc_type != bp_loc_software_breakpoint)
1471 return 0;
1472 if (!bl->inserted)
1473 return 0;
1474 if (bl->target_info.shadow_len == 0)
e5dd4106 1475 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1476 return 0;
1477 return 1;
1478}
1479
9d497a19
PA
1480/* Update BUF, which is LEN bytes read from the target address
1481 MEMADDR, by replacing a memory breakpoint with its shadowed
1482 contents.
1483
1484 If READBUF is not NULL, this buffer must not overlap with the of
1485 the breakpoint location's shadow_contents buffer. Otherwise, a
1486 failed assertion internal error will be raised. */
1487
1488static void
1489one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1490 const gdb_byte *writebuf_org,
1491 ULONGEST memaddr, LONGEST len,
1492 struct bp_target_info *target_info,
1493 struct gdbarch *gdbarch)
1494{
1495 /* Now do full processing of the found relevant range of elements. */
1496 CORE_ADDR bp_addr = 0;
1497 int bp_size = 0;
1498 int bptoffset = 0;
1499
1500 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1501 current_program_space->aspace, 0))
1502 {
1503 /* The breakpoint is inserted in a different address space. */
1504 return;
1505 }
1506
1507 /* Addresses and length of the part of the breakpoint that
1508 we need to copy. */
1509 bp_addr = target_info->placed_address;
1510 bp_size = target_info->shadow_len;
1511
1512 if (bp_addr + bp_size <= memaddr)
1513 {
1514 /* The breakpoint is entirely before the chunk of memory we are
1515 reading. */
1516 return;
1517 }
1518
1519 if (bp_addr >= memaddr + len)
1520 {
1521 /* The breakpoint is entirely after the chunk of memory we are
1522 reading. */
1523 return;
1524 }
1525
1526 /* Offset within shadow_contents. */
1527 if (bp_addr < memaddr)
1528 {
1529 /* Only copy the second part of the breakpoint. */
1530 bp_size -= memaddr - bp_addr;
1531 bptoffset = memaddr - bp_addr;
1532 bp_addr = memaddr;
1533 }
1534
1535 if (bp_addr + bp_size > memaddr + len)
1536 {
1537 /* Only copy the first part of the breakpoint. */
1538 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1539 }
1540
1541 if (readbuf != NULL)
1542 {
1543 /* Verify that the readbuf buffer does not overlap with the
1544 shadow_contents buffer. */
1545 gdb_assert (target_info->shadow_contents >= readbuf + len
1546 || readbuf >= (target_info->shadow_contents
1547 + target_info->shadow_len));
1548
1549 /* Update the read buffer with this inserted breakpoint's
1550 shadow. */
1551 memcpy (readbuf + bp_addr - memaddr,
1552 target_info->shadow_contents + bptoffset, bp_size);
1553 }
1554 else
1555 {
1556 const unsigned char *bp;
0d5ed153
MR
1557 CORE_ADDR addr = target_info->reqstd_address;
1558 int placed_size;
9d497a19
PA
1559
1560 /* Update the shadow with what we want to write to memory. */
1561 memcpy (target_info->shadow_contents + bptoffset,
1562 writebuf_org + bp_addr - memaddr, bp_size);
1563
1564 /* Determine appropriate breakpoint contents and size for this
1565 address. */
0d5ed153 1566 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1567
1568 /* Update the final write buffer with this inserted
1569 breakpoint's INSN. */
1570 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1571 }
1572}
1573
8defab1a 1574/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1575 by replacing any memory breakpoints with their shadowed contents.
1576
35c63cd8
JB
1577 If READBUF is not NULL, this buffer must not overlap with any of
1578 the breakpoint location's shadow_contents buffers. Otherwise,
1579 a failed assertion internal error will be raised.
1580
876fa593 1581 The range of shadowed area by each bp_location is:
35df4500
TJB
1582 bl->address - bp_location_placed_address_before_address_max
1583 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1584 The range we were requested to resolve shadows for is:
1585 memaddr ... memaddr + len
1586 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1587 memaddr + len <= (bl->address
1588 - bp_location_placed_address_before_address_max)
876fa593 1589 and:
35df4500 1590 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1591
8defab1a 1592void
f0ba3972
PA
1593breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1594 const gdb_byte *writebuf_org,
1595 ULONGEST memaddr, LONGEST len)
c906108c 1596{
4a64f543
MS
1597 /* Left boundary, right boundary and median element of our binary
1598 search. */
876fa593
JK
1599 unsigned bc_l, bc_r, bc;
1600
4a64f543
MS
1601 /* Find BC_L which is a leftmost element which may affect BUF
1602 content. It is safe to report lower value but a failure to
1603 report higher one. */
876fa593
JK
1604
1605 bc_l = 0;
1606 bc_r = bp_location_count;
1607 while (bc_l + 1 < bc_r)
1608 {
35df4500 1609 struct bp_location *bl;
876fa593
JK
1610
1611 bc = (bc_l + bc_r) / 2;
35df4500 1612 bl = bp_location[bc];
876fa593 1613
4a64f543
MS
1614 /* Check first BL->ADDRESS will not overflow due to the added
1615 constant. Then advance the left boundary only if we are sure
1616 the BC element can in no way affect the BUF content (MEMADDR
1617 to MEMADDR + LEN range).
876fa593 1618
4a64f543
MS
1619 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1620 offset so that we cannot miss a breakpoint with its shadow
1621 range tail still reaching MEMADDR. */
c5aa993b 1622
35df4500
TJB
1623 if ((bl->address + bp_location_shadow_len_after_address_max
1624 >= bl->address)
1625 && (bl->address + bp_location_shadow_len_after_address_max
1626 <= memaddr))
876fa593
JK
1627 bc_l = bc;
1628 else
1629 bc_r = bc;
1630 }
1631
128070bb
PA
1632 /* Due to the binary search above, we need to make sure we pick the
1633 first location that's at BC_L's address. E.g., if there are
1634 multiple locations at the same address, BC_L may end up pointing
1635 at a duplicate location, and miss the "master"/"inserted"
1636 location. Say, given locations L1, L2 and L3 at addresses A and
1637 B:
1638
1639 L1@A, L2@A, L3@B, ...
1640
1641 BC_L could end up pointing at location L2, while the "master"
1642 location could be L1. Since the `loc->inserted' flag is only set
1643 on "master" locations, we'd forget to restore the shadow of L1
1644 and L2. */
1645 while (bc_l > 0
1646 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1647 bc_l--;
1648
876fa593
JK
1649 /* Now do full processing of the found relevant range of elements. */
1650
1651 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1652 {
35df4500 1653 struct bp_location *bl = bp_location[bc];
876fa593 1654
35df4500
TJB
1655 /* bp_location array has BL->OWNER always non-NULL. */
1656 if (bl->owner->type == bp_none)
8a3fe4f8 1657 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1658 bl->owner->number);
ffce0d52 1659
e5dd4106 1660 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1661 content. */
1662
35df4500
TJB
1663 if (bl->address >= bp_location_placed_address_before_address_max
1664 && memaddr + len <= (bl->address
1665 - bp_location_placed_address_before_address_max))
876fa593
JK
1666 break;
1667
35df4500 1668 if (!bp_location_has_shadow (bl))
c5aa993b 1669 continue;
6c95b8df 1670
9d497a19
PA
1671 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1672 memaddr, len, &bl->target_info, bl->gdbarch);
1673 }
c906108c 1674}
9d497a19 1675
c906108c 1676\f
c5aa993b 1677
b775012e
LM
1678/* Return true if BPT is either a software breakpoint or a hardware
1679 breakpoint. */
1680
1681int
1682is_breakpoint (const struct breakpoint *bpt)
1683{
1684 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1685 || bpt->type == bp_hardware_breakpoint
1686 || bpt->type == bp_dprintf);
b775012e
LM
1687}
1688
60e1c644
PA
1689/* Return true if BPT is of any hardware watchpoint kind. */
1690
a5606eee 1691static int
d77f58be 1692is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1693{
1694 return (bpt->type == bp_hardware_watchpoint
1695 || bpt->type == bp_read_watchpoint
1696 || bpt->type == bp_access_watchpoint);
1697}
7270d8f2 1698
60e1c644
PA
1699/* Return true if BPT is of any watchpoint kind, hardware or
1700 software. */
1701
3a5c3e22 1702int
d77f58be 1703is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1704{
1705 return (is_hardware_watchpoint (bpt)
1706 || bpt->type == bp_watchpoint);
1707}
1708
3a5c3e22
PA
1709/* Returns true if the current thread and its running state are safe
1710 to evaluate or update watchpoint B. Watchpoints on local
1711 expressions need to be evaluated in the context of the thread that
1712 was current when the watchpoint was created, and, that thread needs
1713 to be stopped to be able to select the correct frame context.
1714 Watchpoints on global expressions can be evaluated on any thread,
1715 and in any state. It is presently left to the target allowing
1716 memory accesses when threads are running. */
f6bc2008
PA
1717
1718static int
3a5c3e22 1719watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1720{
d0d8b0c6
JK
1721 return (b->base.pspace == current_program_space
1722 && (ptid_equal (b->watchpoint_thread, null_ptid)
1723 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1724 && !is_executing (inferior_ptid))));
f6bc2008
PA
1725}
1726
d0fb5eae
JK
1727/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1728 associated bp_watchpoint_scope breakpoint. */
1729
1730static void
3a5c3e22 1731watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1732{
3a5c3e22 1733 struct breakpoint *b = &w->base;
d0fb5eae
JK
1734
1735 if (b->related_breakpoint != b)
1736 {
1737 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1738 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1739 b->related_breakpoint->disposition = disp_del_at_next_stop;
1740 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1741 b->related_breakpoint = b;
1742 }
1743 b->disposition = disp_del_at_next_stop;
1744}
1745
bb9d5f81
PP
1746/* Extract a bitfield value from value VAL using the bit parameters contained in
1747 watchpoint W. */
1748
1749static struct value *
1750extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1751{
1752 struct value *bit_val;
1753
1754 if (val == NULL)
1755 return NULL;
1756
1757 bit_val = allocate_value (value_type (val));
1758
1759 unpack_value_bitfield (bit_val,
1760 w->val_bitpos,
1761 w->val_bitsize,
1762 value_contents_for_printing (val),
1763 value_offset (val),
1764 val);
1765
1766 return bit_val;
1767}
1768
c6d81124
PA
1769/* Allocate a dummy location and add it to B, which must be a software
1770 watchpoint. This is required because even if a software watchpoint
1771 is not watching any memory, bpstat_stop_status requires a location
1772 to be able to report stops. */
1773
1774static void
1775software_watchpoint_add_no_memory_location (struct breakpoint *b,
1776 struct program_space *pspace)
1777{
1778 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1779
1780 b->loc = allocate_bp_location (b);
1781 b->loc->pspace = pspace;
1782 b->loc->address = -1;
1783 b->loc->length = -1;
1784}
1785
1786/* Returns true if B is a software watchpoint that is not watching any
1787 memory (e.g., "watch $pc"). */
1788
1789static int
1790is_no_memory_software_watchpoint (struct breakpoint *b)
1791{
1792 return (b->type == bp_watchpoint
1793 && b->loc != NULL
1794 && b->loc->next == NULL
1795 && b->loc->address == -1
1796 && b->loc->length == -1);
1797}
1798
567e1b4e
JB
1799/* Assuming that B is a watchpoint:
1800 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1801 - Evaluate expression and store the result in B->val
567e1b4e
JB
1802 - Evaluate the condition if there is one, and store the result
1803 in b->loc->cond.
a5606eee
VP
1804 - Update the list of values that must be watched in B->loc.
1805
4a64f543
MS
1806 If the watchpoint disposition is disp_del_at_next_stop, then do
1807 nothing. If this is local watchpoint that is out of scope, delete
1808 it.
1809
1810 Even with `set breakpoint always-inserted on' the watchpoints are
1811 removed + inserted on each stop here. Normal breakpoints must
1812 never be removed because they might be missed by a running thread
1813 when debugging in non-stop mode. On the other hand, hardware
1814 watchpoints (is_hardware_watchpoint; processed here) are specific
1815 to each LWP since they are stored in each LWP's hardware debug
1816 registers. Therefore, such LWP must be stopped first in order to
1817 be able to modify its hardware watchpoints.
1818
1819 Hardware watchpoints must be reset exactly once after being
1820 presented to the user. It cannot be done sooner, because it would
1821 reset the data used to present the watchpoint hit to the user. And
1822 it must not be done later because it could display the same single
1823 watchpoint hit during multiple GDB stops. Note that the latter is
1824 relevant only to the hardware watchpoint types bp_read_watchpoint
1825 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1826 not user-visible - its hit is suppressed if the memory content has
1827 not changed.
1828
1829 The following constraints influence the location where we can reset
1830 hardware watchpoints:
1831
1832 * target_stopped_by_watchpoint and target_stopped_data_address are
1833 called several times when GDB stops.
1834
1835 [linux]
1836 * Multiple hardware watchpoints can be hit at the same time,
1837 causing GDB to stop. GDB only presents one hardware watchpoint
1838 hit at a time as the reason for stopping, and all the other hits
1839 are presented later, one after the other, each time the user
1840 requests the execution to be resumed. Execution is not resumed
1841 for the threads still having pending hit event stored in
1842 LWP_INFO->STATUS. While the watchpoint is already removed from
1843 the inferior on the first stop the thread hit event is kept being
1844 reported from its cached value by linux_nat_stopped_data_address
1845 until the real thread resume happens after the watchpoint gets
1846 presented and thus its LWP_INFO->STATUS gets reset.
1847
1848 Therefore the hardware watchpoint hit can get safely reset on the
1849 watchpoint removal from inferior. */
a79d3c27 1850
b40ce68a 1851static void
3a5c3e22 1852update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1853{
a5606eee 1854 int within_current_scope;
a5606eee 1855 struct frame_id saved_frame_id;
66076460 1856 int frame_saved;
a5606eee 1857
f6bc2008
PA
1858 /* If this is a local watchpoint, we only want to check if the
1859 watchpoint frame is in scope if the current thread is the thread
1860 that was used to create the watchpoint. */
1861 if (!watchpoint_in_thread_scope (b))
1862 return;
1863
3a5c3e22 1864 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1865 return;
1866
66076460 1867 frame_saved = 0;
a5606eee
VP
1868
1869 /* Determine if the watchpoint is within scope. */
1870 if (b->exp_valid_block == NULL)
1871 within_current_scope = 1;
1872 else
1873 {
b5db5dfc
UW
1874 struct frame_info *fi = get_current_frame ();
1875 struct gdbarch *frame_arch = get_frame_arch (fi);
1876 CORE_ADDR frame_pc = get_frame_pc (fi);
1877
c9cf6e20
MG
1878 /* If we're at a point where the stack has been destroyed
1879 (e.g. in a function epilogue), unwinding may not work
1880 properly. Do not attempt to recreate locations at this
b5db5dfc 1881 point. See similar comments in watchpoint_check. */
c9cf6e20 1882 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1883 return;
66076460
DJ
1884
1885 /* Save the current frame's ID so we can restore it after
1886 evaluating the watchpoint expression on its own frame. */
1887 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1888 took a frame parameter, so that we didn't have to change the
1889 selected frame. */
1890 frame_saved = 1;
1891 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1892
a5606eee
VP
1893 fi = frame_find_by_id (b->watchpoint_frame);
1894 within_current_scope = (fi != NULL);
1895 if (within_current_scope)
1896 select_frame (fi);
1897 }
1898
b5db5dfc
UW
1899 /* We don't free locations. They are stored in the bp_location array
1900 and update_global_location_list will eventually delete them and
1901 remove breakpoints if needed. */
3a5c3e22 1902 b->base.loc = NULL;
b5db5dfc 1903
a5606eee
VP
1904 if (within_current_scope && reparse)
1905 {
bbc13ae3 1906 const char *s;
d63d0675 1907
a5606eee
VP
1908 if (b->exp)
1909 {
1910 xfree (b->exp);
1911 b->exp = NULL;
1912 }
d63d0675 1913 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1914 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1915 /* If the meaning of expression itself changed, the old value is
1916 no longer relevant. We don't want to report a watchpoint hit
1917 to the user when the old value and the new value may actually
1918 be completely different objects. */
1919 value_free (b->val);
fa4727a6
DJ
1920 b->val = NULL;
1921 b->val_valid = 0;
60e1c644
PA
1922
1923 /* Note that unlike with breakpoints, the watchpoint's condition
1924 expression is stored in the breakpoint object, not in the
1925 locations (re)created below. */
3a5c3e22 1926 if (b->base.cond_string != NULL)
60e1c644
PA
1927 {
1928 if (b->cond_exp != NULL)
1929 {
1930 xfree (b->cond_exp);
1931 b->cond_exp = NULL;
1932 }
1933
3a5c3e22 1934 s = b->base.cond_string;
1bb9788d 1935 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1936 }
a5606eee 1937 }
a5606eee
VP
1938
1939 /* If we failed to parse the expression, for example because
1940 it refers to a global variable in a not-yet-loaded shared library,
1941 don't try to insert watchpoint. We don't automatically delete
1942 such watchpoint, though, since failure to parse expression
1943 is different from out-of-scope watchpoint. */
e8369a73 1944 if (!target_has_execution)
2d134ed3
PA
1945 {
1946 /* Without execution, memory can't change. No use to try and
1947 set watchpoint locations. The watchpoint will be reset when
1948 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1949 if (!can_use_hw_watchpoints)
1950 {
1951 if (b->base.ops->works_in_software_mode (&b->base))
1952 b->base.type = bp_watchpoint;
1953 else
638aa5a1
AB
1954 error (_("Can't set read/access watchpoint when "
1955 "hardware watchpoints are disabled."));
e8369a73 1956 }
2d134ed3
PA
1957 }
1958 else if (within_current_scope && b->exp)
a5606eee 1959 {
0cf6dd15 1960 int pc = 0;
fa4727a6 1961 struct value *val_chain, *v, *result, *next;
2d134ed3 1962 struct program_space *frame_pspace;
a5606eee 1963
3a1115a0 1964 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1965
a5606eee
VP
1966 /* Avoid setting b->val if it's already set. The meaning of
1967 b->val is 'the last value' user saw, and we should update
1968 it only if we reported that last value to user. As it
9c06b0b4
TJB
1969 happens, the code that reports it updates b->val directly.
1970 We don't keep track of the memory value for masked
1971 watchpoints. */
3a5c3e22 1972 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1973 {
bb9d5f81
PP
1974 if (b->val_bitsize != 0)
1975 {
1976 v = extract_bitfield_from_watchpoint_value (b, v);
1977 if (v != NULL)
1978 release_value (v);
1979 }
fa4727a6
DJ
1980 b->val = v;
1981 b->val_valid = 1;
1982 }
a5606eee 1983
2d134ed3
PA
1984 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1985
a5606eee 1986 /* Look at each value on the value chain. */
9fa40276 1987 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1988 {
1989 /* If it's a memory location, and GDB actually needed
1990 its contents to evaluate the expression, then we
fa4727a6
DJ
1991 must watch it. If the first value returned is
1992 still lazy, that means an error occurred reading it;
1993 watch it anyway in case it becomes readable. */
a5606eee 1994 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1995 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1996 {
1997 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1998
a5606eee
VP
1999 /* We only watch structs and arrays if user asked
2000 for it explicitly, never if they just happen to
2001 appear in the middle of some value chain. */
fa4727a6 2002 if (v == result
a5606eee
VP
2003 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2004 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2005 {
2006 CORE_ADDR addr;
f486487f 2007 enum target_hw_bp_type type;
a5606eee 2008 struct bp_location *loc, **tmp;
bb9d5f81
PP
2009 int bitpos = 0, bitsize = 0;
2010
2011 if (value_bitsize (v) != 0)
2012 {
2013 /* Extract the bit parameters out from the bitfield
2014 sub-expression. */
2015 bitpos = value_bitpos (v);
2016 bitsize = value_bitsize (v);
2017 }
2018 else if (v == result && b->val_bitsize != 0)
2019 {
2020 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2021 lvalue whose bit parameters are saved in the fields
2022 VAL_BITPOS and VAL_BITSIZE. */
2023 bitpos = b->val_bitpos;
2024 bitsize = b->val_bitsize;
2025 }
a5606eee 2026
42ae5230 2027 addr = value_address (v);
bb9d5f81
PP
2028 if (bitsize != 0)
2029 {
2030 /* Skip the bytes that don't contain the bitfield. */
2031 addr += bitpos / 8;
2032 }
2033
a5606eee 2034 type = hw_write;
3a5c3e22 2035 if (b->base.type == bp_read_watchpoint)
a5606eee 2036 type = hw_read;
3a5c3e22 2037 else if (b->base.type == bp_access_watchpoint)
a5606eee 2038 type = hw_access;
3a5c3e22
PA
2039
2040 loc = allocate_bp_location (&b->base);
2041 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2042 ;
2043 *tmp = loc;
a6d9a66e 2044 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2045
2046 loc->pspace = frame_pspace;
a5606eee 2047 loc->address = addr;
bb9d5f81
PP
2048
2049 if (bitsize != 0)
2050 {
2051 /* Just cover the bytes that make up the bitfield. */
2052 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2053 }
2054 else
2055 loc->length = TYPE_LENGTH (value_type (v));
2056
a5606eee
VP
2057 loc->watchpoint_type = type;
2058 }
2059 }
9fa40276
TJB
2060 }
2061
2062 /* Change the type of breakpoint between hardware assisted or
2063 an ordinary watchpoint depending on the hardware support
2064 and free hardware slots. REPARSE is set when the inferior
2065 is started. */
a9634178 2066 if (reparse)
9fa40276 2067 {
e09342b5 2068 int reg_cnt;
9fa40276
TJB
2069 enum bp_loc_type loc_type;
2070 struct bp_location *bl;
a5606eee 2071
a9634178 2072 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2073
2074 if (reg_cnt)
9fa40276
TJB
2075 {
2076 int i, target_resources_ok, other_type_used;
a1398e0c 2077 enum bptype type;
9fa40276 2078
a9634178
TJB
2079 /* Use an exact watchpoint when there's only one memory region to be
2080 watched, and only one debug register is needed to watch it. */
2081 b->exact = target_exact_watchpoints && reg_cnt == 1;
2082
9fa40276 2083 /* We need to determine how many resources are already
e09342b5
TJB
2084 used for all other hardware watchpoints plus this one
2085 to see if we still have enough resources to also fit
a1398e0c
PA
2086 this watchpoint in as well. */
2087
2088 /* If this is a software watchpoint, we try to turn it
2089 to a hardware one -- count resources as if B was of
2090 hardware watchpoint type. */
2091 type = b->base.type;
2092 if (type == bp_watchpoint)
2093 type = bp_hardware_watchpoint;
2094
2095 /* This watchpoint may or may not have been placed on
2096 the list yet at this point (it won't be in the list
2097 if we're trying to create it for the first time,
2098 through watch_command), so always account for it
2099 manually. */
2100
2101 /* Count resources used by all watchpoints except B. */
2102 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2103
2104 /* Add in the resources needed for B. */
2105 i += hw_watchpoint_use_count (&b->base);
2106
2107 target_resources_ok
2108 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2109 if (target_resources_ok <= 0)
a9634178 2110 {
3a5c3e22 2111 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2112
2113 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2114 error (_("Target does not support this type of "
2115 "hardware watchpoint."));
9c06b0b4
TJB
2116 else if (target_resources_ok < 0 && !sw_mode)
2117 error (_("There are not enough available hardware "
2118 "resources for this watchpoint."));
a1398e0c
PA
2119
2120 /* Downgrade to software watchpoint. */
2121 b->base.type = bp_watchpoint;
2122 }
2123 else
2124 {
2125 /* If this was a software watchpoint, we've just
2126 found we have enough resources to turn it to a
2127 hardware watchpoint. Otherwise, this is a
2128 nop. */
2129 b->base.type = type;
a9634178 2130 }
9fa40276 2131 }
3a5c3e22 2132 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2133 {
2134 if (!can_use_hw_watchpoints)
2135 error (_("Can't set read/access watchpoint when "
2136 "hardware watchpoints are disabled."));
2137 else
2138 error (_("Expression cannot be implemented with "
2139 "read/access watchpoint."));
2140 }
9fa40276 2141 else
3a5c3e22 2142 b->base.type = bp_watchpoint;
9fa40276 2143
3a5c3e22 2144 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2145 : bp_loc_hardware_watchpoint);
3a5c3e22 2146 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2147 bl->loc_type = loc_type;
2148 }
2149
2150 for (v = val_chain; v; v = next)
2151 {
a5606eee
VP
2152 next = value_next (v);
2153 if (v != b->val)
2154 value_free (v);
2155 }
2156
c7437ca6
PA
2157 /* If a software watchpoint is not watching any memory, then the
2158 above left it without any location set up. But,
2159 bpstat_stop_status requires a location to be able to report
2160 stops, so make sure there's at least a dummy one. */
3a5c3e22 2161 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2162 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2163 }
2164 else if (!within_current_scope)
7270d8f2 2165 {
ac74f770
MS
2166 printf_filtered (_("\
2167Watchpoint %d deleted because the program has left the block\n\
2168in which its expression is valid.\n"),
3a5c3e22 2169 b->base.number);
d0fb5eae 2170 watchpoint_del_at_next_stop (b);
7270d8f2 2171 }
a5606eee
VP
2172
2173 /* Restore the selected frame. */
66076460
DJ
2174 if (frame_saved)
2175 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2176}
2177
a5606eee 2178
74960c60 2179/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2180 inserted in the inferior. We don't differentiate the type of BL's owner
2181 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2182 breakpoint_ops is not defined, because in insert_bp_location,
2183 tracepoint's insert_location will not be called. */
74960c60 2184static int
35df4500 2185should_be_inserted (struct bp_location *bl)
74960c60 2186{
35df4500 2187 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2188 return 0;
2189
35df4500 2190 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2191 return 0;
2192
35df4500 2193 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2194 return 0;
2195
f8eba3c6
TT
2196 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2197 return 0;
2198
56710373
PA
2199 /* This is set for example, when we're attached to the parent of a
2200 vfork, and have detached from the child. The child is running
2201 free, and we expect it to do an exec or exit, at which point the
2202 OS makes the parent schedulable again (and the target reports
2203 that the vfork is done). Until the child is done with the shared
2204 memory region, do not insert breakpoints in the parent, otherwise
2205 the child could still trip on the parent's breakpoints. Since
2206 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2207 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2208 return 0;
2209
31e77af2 2210 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2211 location, except if the breakpoint is a single-step breakpoint,
2212 and the breakpoint's thread is the thread which is stepping past
2213 a breakpoint. */
31e77af2
PA
2214 if ((bl->loc_type == bp_loc_software_breakpoint
2215 || bl->loc_type == bp_loc_hardware_breakpoint)
2216 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2217 bl->address)
2218 /* The single-step breakpoint may be inserted at the location
2219 we're trying to step if the instruction branches to itself.
2220 However, the instruction won't be executed at all and it may
2221 break the semantics of the instruction, for example, the
2222 instruction is a conditional branch or updates some flags.
2223 We can't fix it unless GDB is able to emulate the instruction
2224 or switch to displaced stepping. */
2225 && !(bl->owner->type == bp_single_step
2226 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2227 {
2228 if (debug_infrun)
2229 {
2230 fprintf_unfiltered (gdb_stdlog,
2231 "infrun: skipping breakpoint: "
2232 "stepping past insn at: %s\n",
2233 paddress (bl->gdbarch, bl->address));
2234 }
2235 return 0;
2236 }
31e77af2 2237
963f9c80
PA
2238 /* Don't insert watchpoints if we're trying to step past the
2239 instruction that triggered one. */
2240 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2241 && stepping_past_nonsteppable_watchpoint ())
2242 {
2243 if (debug_infrun)
2244 {
2245 fprintf_unfiltered (gdb_stdlog,
2246 "infrun: stepping past non-steppable watchpoint. "
2247 "skipping watchpoint at %s:%d\n",
2248 paddress (bl->gdbarch, bl->address),
2249 bl->length);
2250 }
2251 return 0;
2252 }
2253
74960c60
VP
2254 return 1;
2255}
2256
934709f0
PW
2257/* Same as should_be_inserted but does the check assuming
2258 that the location is not duplicated. */
2259
2260static int
2261unduplicated_should_be_inserted (struct bp_location *bl)
2262{
2263 int result;
2264 const int save_duplicate = bl->duplicate;
2265
2266 bl->duplicate = 0;
2267 result = should_be_inserted (bl);
2268 bl->duplicate = save_duplicate;
2269 return result;
2270}
2271
b775012e
LM
2272/* Parses a conditional described by an expression COND into an
2273 agent expression bytecode suitable for evaluation
2274 by the bytecode interpreter. Return NULL if there was
2275 any error during parsing. */
2276
2277static struct agent_expr *
2278parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2279{
2280 struct agent_expr *aexpr = NULL;
b775012e
LM
2281
2282 if (!cond)
2283 return NULL;
2284
2285 /* We don't want to stop processing, so catch any errors
2286 that may show up. */
492d29ea 2287 TRY
b775012e
LM
2288 {
2289 aexpr = gen_eval_for_expr (scope, cond);
2290 }
2291
492d29ea 2292 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2293 {
2294 /* If we got here, it means the condition could not be parsed to a valid
2295 bytecode expression and thus can't be evaluated on the target's side.
2296 It's no use iterating through the conditions. */
2297 return NULL;
2298 }
492d29ea 2299 END_CATCH
b775012e
LM
2300
2301 /* We have a valid agent expression. */
2302 return aexpr;
2303}
2304
2305/* Based on location BL, create a list of breakpoint conditions to be
2306 passed on to the target. If we have duplicated locations with different
2307 conditions, we will add such conditions to the list. The idea is that the
2308 target will evaluate the list of conditions and will only notify GDB when
2309 one of them is true. */
2310
2311static void
2312build_target_condition_list (struct bp_location *bl)
2313{
2314 struct bp_location **locp = NULL, **loc2p;
2315 int null_condition_or_parse_error = 0;
2316 int modified = bl->needs_update;
2317 struct bp_location *loc;
2318
8b4f3082
PA
2319 /* Release conditions left over from a previous insert. */
2320 VEC_free (agent_expr_p, bl->target_info.conditions);
2321
b775012e
LM
2322 /* This is only meaningful if the target is
2323 evaluating conditions and if the user has
2324 opted for condition evaluation on the target's
2325 side. */
2326 if (gdb_evaluates_breakpoint_condition_p ()
2327 || !target_supports_evaluation_of_breakpoint_conditions ())
2328 return;
2329
2330 /* Do a first pass to check for locations with no assigned
2331 conditions or conditions that fail to parse to a valid agent expression
2332 bytecode. If any of these happen, then it's no use to send conditions
2333 to the target since this location will always trigger and generate a
2334 response back to GDB. */
2335 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2336 {
2337 loc = (*loc2p);
2338 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2339 {
2340 if (modified)
2341 {
2342 struct agent_expr *aexpr;
2343
2344 /* Re-parse the conditions since something changed. In that
2345 case we already freed the condition bytecodes (see
2346 force_breakpoint_reinsertion). We just
2347 need to parse the condition to bytecodes again. */
2348 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2349 loc->cond_bytecode = aexpr;
b775012e
LM
2350 }
2351
2352 /* If we have a NULL bytecode expression, it means something
2353 went wrong or we have a null condition expression. */
2354 if (!loc->cond_bytecode)
2355 {
2356 null_condition_or_parse_error = 1;
2357 break;
2358 }
2359 }
2360 }
2361
2362 /* If any of these happened, it means we will have to evaluate the conditions
2363 for the location's address on gdb's side. It is no use keeping bytecodes
2364 for all the other duplicate locations, thus we free all of them here.
2365
2366 This is so we have a finer control over which locations' conditions are
2367 being evaluated by GDB or the remote stub. */
2368 if (null_condition_or_parse_error)
2369 {
2370 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2371 {
2372 loc = (*loc2p);
2373 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2374 {
2375 /* Only go as far as the first NULL bytecode is
2376 located. */
2377 if (!loc->cond_bytecode)
2378 return;
2379
2380 free_agent_expr (loc->cond_bytecode);
2381 loc->cond_bytecode = NULL;
2382 }
2383 }
2384 }
2385
2386 /* No NULL conditions or failed bytecode generation. Build a condition list
2387 for this location's address. */
2388 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2389 {
2390 loc = (*loc2p);
2391 if (loc->cond
2392 && is_breakpoint (loc->owner)
2393 && loc->pspace->num == bl->pspace->num
2394 && loc->owner->enable_state == bp_enabled
2395 && loc->enabled)
2396 /* Add the condition to the vector. This will be used later to send the
2397 conditions to the target. */
2398 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2399 loc->cond_bytecode);
2400 }
2401
2402 return;
2403}
2404
d3ce09f5
SS
2405/* Parses a command described by string CMD into an agent expression
2406 bytecode suitable for evaluation by the bytecode interpreter.
2407 Return NULL if there was any error during parsing. */
2408
2409static struct agent_expr *
2410parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2411{
2412 struct cleanup *old_cleanups = 0;
2413 struct expression *expr, **argvec;
2414 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2415 const char *cmdrest;
2416 const char *format_start, *format_end;
d3ce09f5
SS
2417 struct format_piece *fpieces;
2418 int nargs;
2419 struct gdbarch *gdbarch = get_current_arch ();
2420
2421 if (!cmd)
2422 return NULL;
2423
2424 cmdrest = cmd;
2425
2426 if (*cmdrest == ',')
2427 ++cmdrest;
bbc13ae3 2428 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2429
2430 if (*cmdrest++ != '"')
2431 error (_("No format string following the location"));
2432
2433 format_start = cmdrest;
2434
2435 fpieces = parse_format_string (&cmdrest);
2436
2437 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2438
2439 format_end = cmdrest;
2440
2441 if (*cmdrest++ != '"')
2442 error (_("Bad format string, non-terminated '\"'."));
2443
bbc13ae3 2444 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2445
2446 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2447 error (_("Invalid argument syntax"));
2448
2449 if (*cmdrest == ',')
2450 cmdrest++;
bbc13ae3 2451 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2452
2453 /* For each argument, make an expression. */
2454
2455 argvec = (struct expression **) alloca (strlen (cmd)
2456 * sizeof (struct expression *));
2457
2458 nargs = 0;
2459 while (*cmdrest != '\0')
2460 {
bbc13ae3 2461 const char *cmd1;
d3ce09f5
SS
2462
2463 cmd1 = cmdrest;
2464 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2465 argvec[nargs++] = expr;
2466 cmdrest = cmd1;
2467 if (*cmdrest == ',')
2468 ++cmdrest;
2469 }
2470
2471 /* We don't want to stop processing, so catch any errors
2472 that may show up. */
492d29ea 2473 TRY
d3ce09f5
SS
2474 {
2475 aexpr = gen_printf (scope, gdbarch, 0, 0,
2476 format_start, format_end - format_start,
2477 fpieces, nargs, argvec);
2478 }
492d29ea 2479 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2480 {
2481 /* If we got here, it means the command could not be parsed to a valid
2482 bytecode expression and thus can't be evaluated on the target's side.
2483 It's no use iterating through the other commands. */
492d29ea 2484 aexpr = NULL;
d3ce09f5 2485 }
492d29ea
PA
2486 END_CATCH
2487
2488 do_cleanups (old_cleanups);
d3ce09f5 2489
d3ce09f5
SS
2490 /* We have a valid agent expression, return it. */
2491 return aexpr;
2492}
2493
2494/* Based on location BL, create a list of breakpoint commands to be
2495 passed on to the target. If we have duplicated locations with
2496 different commands, we will add any such to the list. */
2497
2498static void
2499build_target_command_list (struct bp_location *bl)
2500{
2501 struct bp_location **locp = NULL, **loc2p;
2502 int null_command_or_parse_error = 0;
2503 int modified = bl->needs_update;
2504 struct bp_location *loc;
2505
8b4f3082
PA
2506 /* Release commands left over from a previous insert. */
2507 VEC_free (agent_expr_p, bl->target_info.tcommands);
2508
41fac0cf 2509 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2510 return;
2511
41fac0cf
PA
2512 /* For now, limit to agent-style dprintf breakpoints. */
2513 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2514 return;
2515
41fac0cf
PA
2516 /* For now, if we have any duplicate location that isn't a dprintf,
2517 don't install the target-side commands, as that would make the
2518 breakpoint not be reported to the core, and we'd lose
2519 control. */
2520 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2521 {
2522 loc = (*loc2p);
2523 if (is_breakpoint (loc->owner)
2524 && loc->pspace->num == bl->pspace->num
2525 && loc->owner->type != bp_dprintf)
2526 return;
2527 }
2528
d3ce09f5
SS
2529 /* Do a first pass to check for locations with no assigned
2530 conditions or conditions that fail to parse to a valid agent expression
2531 bytecode. If any of these happen, then it's no use to send conditions
2532 to the target since this location will always trigger and generate a
2533 response back to GDB. */
2534 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2535 {
2536 loc = (*loc2p);
2537 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2538 {
2539 if (modified)
2540 {
2541 struct agent_expr *aexpr;
2542
2543 /* Re-parse the commands since something changed. In that
2544 case we already freed the command bytecodes (see
2545 force_breakpoint_reinsertion). We just
2546 need to parse the command to bytecodes again. */
2547 aexpr = parse_cmd_to_aexpr (bl->address,
2548 loc->owner->extra_string);
2549 loc->cmd_bytecode = aexpr;
d3ce09f5
SS
2550 }
2551
2552 /* If we have a NULL bytecode expression, it means something
2553 went wrong or we have a null command expression. */
2554 if (!loc->cmd_bytecode)
2555 {
2556 null_command_or_parse_error = 1;
2557 break;
2558 }
2559 }
2560 }
2561
2562 /* If anything failed, then we're not doing target-side commands,
2563 and so clean up. */
2564 if (null_command_or_parse_error)
2565 {
2566 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2567 {
2568 loc = (*loc2p);
2569 if (is_breakpoint (loc->owner)
2570 && loc->pspace->num == bl->pspace->num)
2571 {
2572 /* Only go as far as the first NULL bytecode is
2573 located. */
40fb6c5e 2574 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2575 return;
2576
40fb6c5e
HZ
2577 free_agent_expr (loc->cmd_bytecode);
2578 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2579 }
2580 }
2581 }
2582
2583 /* No NULL commands or failed bytecode generation. Build a command list
2584 for this location's address. */
2585 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2586 {
2587 loc = (*loc2p);
2588 if (loc->owner->extra_string
2589 && is_breakpoint (loc->owner)
2590 && loc->pspace->num == bl->pspace->num
2591 && loc->owner->enable_state == bp_enabled
2592 && loc->enabled)
2593 /* Add the command to the vector. This will be used later
2594 to send the commands to the target. */
2595 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2596 loc->cmd_bytecode);
2597 }
2598
2599 bl->target_info.persist = 0;
2600 /* Maybe flag this location as persistent. */
2601 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2602 bl->target_info.persist = 1;
2603}
2604
35df4500
TJB
2605/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2606 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2607 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2608 Returns 0 for success, 1 if the bp_location type is not supported or
2609 -1 for failure.
879bfdc2 2610
4a64f543
MS
2611 NOTE drow/2003-09-09: This routine could be broken down to an
2612 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2613static int
35df4500 2614insert_bp_location (struct bp_location *bl,
26bb91f3 2615 struct ui_file *tmp_error_stream,
3fbb6ffa 2616 int *disabled_breaks,
dd61ec5c
MW
2617 int *hw_breakpoint_error,
2618 int *hw_bp_error_explained_already)
879bfdc2 2619{
0000e5cc
PA
2620 enum errors bp_err = GDB_NO_ERROR;
2621 const char *bp_err_message = NULL;
879bfdc2 2622
b775012e 2623 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2624 return 0;
2625
35c63cd8
JB
2626 /* Note we don't initialize bl->target_info, as that wipes out
2627 the breakpoint location's shadow_contents if the breakpoint
2628 is still inserted at that location. This in turn breaks
2629 target_read_memory which depends on these buffers when
2630 a memory read is requested at the breakpoint location:
2631 Once the target_info has been wiped, we fail to see that
2632 we have a breakpoint inserted at that address and thus
2633 read the breakpoint instead of returning the data saved in
2634 the breakpoint location's shadow contents. */
0d5ed153 2635 bl->target_info.reqstd_address = bl->address;
35df4500 2636 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2637 bl->target_info.length = bl->length;
8181d85f 2638
b775012e
LM
2639 /* When working with target-side conditions, we must pass all the conditions
2640 for the same breakpoint address down to the target since GDB will not
2641 insert those locations. With a list of breakpoint conditions, the target
2642 can decide when to stop and notify GDB. */
2643
2644 if (is_breakpoint (bl->owner))
2645 {
2646 build_target_condition_list (bl);
d3ce09f5
SS
2647 build_target_command_list (bl);
2648 /* Reset the modification marker. */
b775012e
LM
2649 bl->needs_update = 0;
2650 }
2651
35df4500
TJB
2652 if (bl->loc_type == bp_loc_software_breakpoint
2653 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2654 {
35df4500 2655 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2656 {
2657 /* If the explicitly specified breakpoint type
2658 is not hardware breakpoint, check the memory map to see
2659 if the breakpoint address is in read only memory or not.
4a64f543 2660
765dc015
VP
2661 Two important cases are:
2662 - location type is not hardware breakpoint, memory
2663 is readonly. We change the type of the location to
2664 hardware breakpoint.
4a64f543
MS
2665 - location type is hardware breakpoint, memory is
2666 read-write. This means we've previously made the
2667 location hardware one, but then the memory map changed,
2668 so we undo.
765dc015 2669
4a64f543
MS
2670 When breakpoints are removed, remove_breakpoints will use
2671 location types we've just set here, the only possible
2672 problem is that memory map has changed during running
2673 program, but it's not going to work anyway with current
2674 gdb. */
765dc015 2675 struct mem_region *mr
0d5ed153 2676 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2677
2678 if (mr)
2679 {
2680 if (automatic_hardware_breakpoints)
2681 {
765dc015
VP
2682 enum bp_loc_type new_type;
2683
2684 if (mr->attrib.mode != MEM_RW)
2685 new_type = bp_loc_hardware_breakpoint;
2686 else
2687 new_type = bp_loc_software_breakpoint;
2688
35df4500 2689 if (new_type != bl->loc_type)
765dc015
VP
2690 {
2691 static int said = 0;
cc59ec59 2692
35df4500 2693 bl->loc_type = new_type;
765dc015
VP
2694 if (!said)
2695 {
3e43a32a
MS
2696 fprintf_filtered (gdb_stdout,
2697 _("Note: automatically using "
2698 "hardware breakpoints for "
2699 "read-only addresses.\n"));
765dc015
VP
2700 said = 1;
2701 }
2702 }
2703 }
35df4500 2704 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2705 && mr->attrib.mode != MEM_RW)
2706 {
2707 fprintf_unfiltered (tmp_error_stream,
2708 _("Cannot insert breakpoint %d.\n"
2709 "Cannot set software breakpoint "
2710 "at read-only address %s\n"),
2711 bl->owner->number,
2712 paddress (bl->gdbarch, bl->address));
2713 return 1;
2714 }
765dc015
VP
2715 }
2716 }
2717
879bfdc2
DJ
2718 /* First check to see if we have to handle an overlay. */
2719 if (overlay_debugging == ovly_off
35df4500
TJB
2720 || bl->section == NULL
2721 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2722 {
2723 /* No overlay handling: just set the breakpoint. */
492d29ea 2724 TRY
dd61ec5c 2725 {
0000e5cc
PA
2726 int val;
2727
dd61ec5c 2728 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2729 if (val)
2730 bp_err = GENERIC_ERROR;
dd61ec5c 2731 }
492d29ea 2732 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2733 {
0000e5cc
PA
2734 bp_err = e.error;
2735 bp_err_message = e.message;
dd61ec5c 2736 }
492d29ea 2737 END_CATCH
879bfdc2
DJ
2738 }
2739 else
2740 {
4a64f543 2741 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2742 Shall we set a breakpoint at the LMA? */
2743 if (!overlay_events_enabled)
2744 {
2745 /* Yes -- overlay event support is not active,
2746 so we must try to set a breakpoint at the LMA.
2747 This will not work for a hardware breakpoint. */
35df4500 2748 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2749 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2750 bl->owner->number);
879bfdc2
DJ
2751 else
2752 {
35df4500
TJB
2753 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2754 bl->section);
879bfdc2 2755 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2756 bl->overlay_target_info = bl->target_info;
0d5ed153 2757 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2758
2759 /* No overlay handling: just set the breakpoint. */
492d29ea 2760 TRY
0000e5cc
PA
2761 {
2762 int val;
2763
2764 val = target_insert_breakpoint (bl->gdbarch,
2765 &bl->overlay_target_info);
2766 if (val)
2767 bp_err = GENERIC_ERROR;
2768 }
492d29ea 2769 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2770 {
2771 bp_err = e.error;
2772 bp_err_message = e.message;
2773 }
492d29ea 2774 END_CATCH
0000e5cc
PA
2775
2776 if (bp_err != GDB_NO_ERROR)
99361f52 2777 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2778 "Overlay breakpoint %d "
2779 "failed: in ROM?\n",
35df4500 2780 bl->owner->number);
879bfdc2
DJ
2781 }
2782 }
2783 /* Shall we set a breakpoint at the VMA? */
35df4500 2784 if (section_is_mapped (bl->section))
879bfdc2
DJ
2785 {
2786 /* Yes. This overlay section is mapped into memory. */
492d29ea 2787 TRY
dd61ec5c 2788 {
0000e5cc
PA
2789 int val;
2790
dd61ec5c 2791 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2792 if (val)
2793 bp_err = GENERIC_ERROR;
dd61ec5c 2794 }
492d29ea 2795 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2796 {
0000e5cc
PA
2797 bp_err = e.error;
2798 bp_err_message = e.message;
dd61ec5c 2799 }
492d29ea 2800 END_CATCH
879bfdc2
DJ
2801 }
2802 else
2803 {
2804 /* No. This breakpoint will not be inserted.
2805 No error, but do not mark the bp as 'inserted'. */
2806 return 0;
2807 }
2808 }
2809
0000e5cc 2810 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2811 {
2812 /* Can't set the breakpoint. */
0000e5cc
PA
2813
2814 /* In some cases, we might not be able to insert a
2815 breakpoint in a shared library that has already been
2816 removed, but we have not yet processed the shlib unload
2817 event. Unfortunately, some targets that implement
076855f9
PA
2818 breakpoint insertion themselves can't tell why the
2819 breakpoint insertion failed (e.g., the remote target
2820 doesn't define error codes), so we must treat generic
2821 errors as memory errors. */
0000e5cc 2822 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2823 && bl->loc_type == bp_loc_software_breakpoint
08351840 2824 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2825 || shared_objfile_contains_address_p (bl->pspace,
2826 bl->address)))
879bfdc2 2827 {
4a64f543 2828 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2829 bl->shlib_disabled = 1;
8d3788bd 2830 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2831 if (!*disabled_breaks)
2832 {
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n",
2835 bl->owner->number);
2836 fprintf_unfiltered (tmp_error_stream,
2837 "Temporarily disabling shared "
2838 "library breakpoints:\n");
2839 }
2840 *disabled_breaks = 1;
879bfdc2 2841 fprintf_unfiltered (tmp_error_stream,
35df4500 2842 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2843 return 0;
879bfdc2
DJ
2844 }
2845 else
879bfdc2 2846 {
35df4500 2847 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2848 {
0000e5cc
PA
2849 *hw_breakpoint_error = 1;
2850 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2851 fprintf_unfiltered (tmp_error_stream,
2852 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2853 bl->owner->number, bp_err_message ? ":" : ".\n");
2854 if (bp_err_message != NULL)
2855 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2856 }
2857 else
2858 {
0000e5cc
PA
2859 if (bp_err_message == NULL)
2860 {
2861 char *message
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
2864 struct cleanup *old_chain = make_cleanup (xfree, message);
2865
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Cannot insert breakpoint %d.\n"
2868 "%s\n",
2869 bl->owner->number, message);
2870 do_cleanups (old_chain);
2871 }
2872 else
2873 {
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d: %s\n",
2876 bl->owner->number,
2877 bp_err_message);
2878 }
879bfdc2 2879 }
0000e5cc 2880 return 1;
879bfdc2
DJ
2881
2882 }
2883 }
2884 else
35df4500 2885 bl->inserted = 1;
879bfdc2 2886
0000e5cc 2887 return 0;
879bfdc2
DJ
2888 }
2889
35df4500 2890 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2891 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2892 watchpoints. It's not clear that it's necessary... */
35df4500 2893 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2894 {
0000e5cc
PA
2895 int val;
2896
77b06cd7
TJB
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2901
2902 /* If trying to set a read-watchpoint, and it turns out it's not
2903 supported, try emulating one with an access watchpoint. */
35df4500 2904 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* But don't try to insert it, if there's already another
2909 hw_access location that would be considered a duplicate
2910 of this one. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2912 if (loc != bl
85d721b8 2913 && loc->watchpoint_type == hw_access
35df4500 2914 && watchpoint_locations_match (bl, loc))
85d721b8 2915 {
35df4500
TJB
2916 bl->duplicate = 1;
2917 bl->inserted = 1;
2918 bl->target_info = loc->target_info;
2919 bl->watchpoint_type = hw_access;
85d721b8
PA
2920 val = 0;
2921 break;
2922 }
2923
2924 if (val == 1)
2925 {
77b06cd7
TJB
2926 bl->watchpoint_type = hw_access;
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 if (val)
2930 /* Back to the original value. */
2931 bl->watchpoint_type = hw_read;
85d721b8
PA
2932 }
2933 }
2934
35df4500 2935 bl->inserted = (val == 0);
879bfdc2
DJ
2936 }
2937
35df4500 2938 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2939 {
0000e5cc
PA
2940 int val;
2941
77b06cd7
TJB
2942 gdb_assert (bl->owner->ops != NULL
2943 && bl->owner->ops->insert_location != NULL);
2944
2945 val = bl->owner->ops->insert_location (bl);
2946 if (val)
2947 {
2948 bl->owner->enable_state = bp_disabled;
2949
2950 if (val == 1)
2951 warning (_("\
2952Error inserting catchpoint %d: Your system does not support this type\n\
2953of catchpoint."), bl->owner->number);
2954 else
2955 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2956 }
2957
2958 bl->inserted = (val == 0);
1640b821
DJ
2959
2960 /* We've already printed an error message if there was a problem
2961 inserting this catchpoint, and we've disabled the catchpoint,
2962 so just return success. */
2963 return 0;
879bfdc2
DJ
2964 }
2965
2966 return 0;
2967}
2968
6c95b8df
PA
2969/* This function is called when program space PSPACE is about to be
2970 deleted. It takes care of updating breakpoints to not reference
2971 PSPACE anymore. */
2972
2973void
2974breakpoint_program_space_exit (struct program_space *pspace)
2975{
2976 struct breakpoint *b, *b_temp;
876fa593 2977 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2978
2979 /* Remove any breakpoint that was set through this program space. */
2980 ALL_BREAKPOINTS_SAFE (b, b_temp)
2981 {
2982 if (b->pspace == pspace)
2983 delete_breakpoint (b);
2984 }
2985
2986 /* Breakpoints set through other program spaces could have locations
2987 bound to PSPACE as well. Remove those. */
876fa593 2988 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2989 {
2990 struct bp_location *tmp;
2991
2992 if (loc->pspace == pspace)
2993 {
2bdf28a0 2994 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2995 if (loc->owner->loc == loc)
2996 loc->owner->loc = loc->next;
2997 else
2998 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2999 if (tmp->next == loc)
3000 {
3001 tmp->next = loc->next;
3002 break;
3003 }
3004 }
3005 }
3006
3007 /* Now update the global location list to permanently delete the
3008 removed locations above. */
44702360 3009 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3010}
3011
74960c60
VP
3012/* Make sure all breakpoints are inserted in inferior.
3013 Throws exception on any error.
3014 A breakpoint that is already inserted won't be inserted
3015 again, so calling this function twice is safe. */
3016void
3017insert_breakpoints (void)
3018{
3019 struct breakpoint *bpt;
3020
3021 ALL_BREAKPOINTS (bpt)
3022 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3023 {
3024 struct watchpoint *w = (struct watchpoint *) bpt;
3025
3026 update_watchpoint (w, 0 /* don't reparse. */);
3027 }
74960c60 3028
04086b45
PA
3029 /* Updating watchpoints creates new locations, so update the global
3030 location list. Explicitly tell ugll to insert locations and
3031 ignore breakpoints_always_inserted_mode. */
3032 update_global_location_list (UGLL_INSERT);
74960c60
VP
3033}
3034
20388dd6
YQ
3035/* Invoke CALLBACK for each of bp_location. */
3036
3037void
3038iterate_over_bp_locations (walk_bp_location_callback callback)
3039{
3040 struct bp_location *loc, **loc_tmp;
3041
3042 ALL_BP_LOCATIONS (loc, loc_tmp)
3043 {
3044 callback (loc, NULL);
3045 }
3046}
3047
b775012e
LM
3048/* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3051
3052static void
3053update_inserted_breakpoint_locations (void)
3054{
3055 struct bp_location *bl, **blp_tmp;
3056 int error_flag = 0;
3057 int val = 0;
3058 int disabled_breaks = 0;
3059 int hw_breakpoint_error = 0;
dd61ec5c 3060 int hw_bp_details_reported = 0;
b775012e
LM
3061
3062 struct ui_file *tmp_error_stream = mem_fileopen ();
3063 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3064
3065 /* Explicitly mark the warning -- this will only be printed if
3066 there was an error. */
3067 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3068
3069 save_current_space_and_thread ();
3070
3071 ALL_BP_LOCATIONS (bl, blp_tmp)
3072 {
3073 /* We only want to update software breakpoints and hardware
3074 breakpoints. */
3075 if (!is_breakpoint (bl->owner))
3076 continue;
3077
3078 /* We only want to update locations that are already inserted
3079 and need updating. This is to avoid unwanted insertion during
3080 deletion of breakpoints. */
3081 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3082 continue;
3083
3084 switch_to_program_space_and_thread (bl->pspace);
3085
3086 /* For targets that support global breakpoints, there's no need
3087 to select an inferior to insert breakpoint to. In fact, even
3088 if we aren't attached to any process yet, we should still
3089 insert breakpoints. */
f5656ead 3090 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3091 && ptid_equal (inferior_ptid, null_ptid))
3092 continue;
3093
3094 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3095 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3096 if (val)
3097 error_flag = val;
3098 }
3099
3100 if (error_flag)
3101 {
3102 target_terminal_ours_for_output ();
3103 error_stream (tmp_error_stream);
3104 }
3105
3106 do_cleanups (cleanups);
3107}
3108
c30eee59 3109/* Used when starting or continuing the program. */
c906108c 3110
74960c60
VP
3111static void
3112insert_breakpoint_locations (void)
c906108c 3113{
a5606eee 3114 struct breakpoint *bpt;
35df4500 3115 struct bp_location *bl, **blp_tmp;
eacd795a 3116 int error_flag = 0;
c906108c 3117 int val = 0;
3fbb6ffa 3118 int disabled_breaks = 0;
81d0cc19 3119 int hw_breakpoint_error = 0;
dd61ec5c 3120 int hw_bp_error_explained_already = 0;
c906108c 3121
81d0cc19 3122 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3123 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3124
81d0cc19
GS
3125 /* Explicitly mark the warning -- this will only be printed if
3126 there was an error. */
3127 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3128
3129 save_current_space_and_thread ();
3130
35df4500 3131 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3132 {
b775012e 3133 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3134 continue;
3135
4a64f543
MS
3136 /* There is no point inserting thread-specific breakpoints if
3137 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3138 has BL->OWNER always non-NULL. */
35df4500 3139 if (bl->owner->thread != -1
5d5658a1 3140 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3141 continue;
3142
35df4500 3143 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3144
3145 /* For targets that support global breakpoints, there's no need
3146 to select an inferior to insert breakpoint to. In fact, even
3147 if we aren't attached to any process yet, we should still
3148 insert breakpoints. */
f5656ead 3149 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3150 && ptid_equal (inferior_ptid, null_ptid))
3151 continue;
3152
3fbb6ffa 3153 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3154 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3155 if (val)
eacd795a 3156 error_flag = val;
879bfdc2 3157 }
c906108c 3158
4a64f543
MS
3159 /* If we failed to insert all locations of a watchpoint, remove
3160 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3161 ALL_BREAKPOINTS (bpt)
3162 {
3163 int some_failed = 0;
3164 struct bp_location *loc;
3165
3166 if (!is_hardware_watchpoint (bpt))
3167 continue;
3168
d6b74ac4 3169 if (!breakpoint_enabled (bpt))
a5606eee 3170 continue;
74960c60
VP
3171
3172 if (bpt->disposition == disp_del_at_next_stop)
3173 continue;
a5606eee
VP
3174
3175 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3176 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3177 {
3178 some_failed = 1;
3179 break;
3180 }
3181 if (some_failed)
3182 {
3183 for (loc = bpt->loc; loc; loc = loc->next)
3184 if (loc->inserted)
834c0d03 3185 remove_breakpoint (loc);
a5606eee
VP
3186
3187 hw_breakpoint_error = 1;
3188 fprintf_unfiltered (tmp_error_stream,
3189 "Could not insert hardware watchpoint %d.\n",
3190 bpt->number);
eacd795a 3191 error_flag = -1;
a5606eee
VP
3192 }
3193 }
3194
eacd795a 3195 if (error_flag)
81d0cc19
GS
3196 {
3197 /* If a hardware breakpoint or watchpoint was inserted, add a
3198 message about possibly exhausted resources. */
dd61ec5c 3199 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3200 {
c6510018
MS
3201 fprintf_unfiltered (tmp_error_stream,
3202 "Could not insert hardware breakpoints:\n\
3203You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3204 }
81d0cc19
GS
3205 target_terminal_ours_for_output ();
3206 error_stream (tmp_error_stream);
3207 }
f7545552
TT
3208
3209 do_cleanups (cleanups);
c906108c
SS
3210}
3211
c30eee59
TJB
3212/* Used when the program stops.
3213 Returns zero if successful, or non-zero if there was a problem
3214 removing a breakpoint location. */
3215
c906108c 3216int
fba45db2 3217remove_breakpoints (void)
c906108c 3218{
35df4500 3219 struct bp_location *bl, **blp_tmp;
3a1bae8e 3220 int val = 0;
c906108c 3221
35df4500 3222 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3223 {
1e4d1764 3224 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3225 val |= remove_breakpoint (bl);
c5aa993b 3226 }
3a1bae8e 3227 return val;
c906108c
SS
3228}
3229
49fa26b0
PA
3230/* When a thread exits, remove breakpoints that are related to
3231 that thread. */
3232
3233static void
3234remove_threaded_breakpoints (struct thread_info *tp, int silent)
3235{
3236 struct breakpoint *b, *b_tmp;
3237
3238 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3239 {
5d5658a1 3240 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3241 {
3242 b->disposition = disp_del_at_next_stop;
3243
3244 printf_filtered (_("\
43792cf0
PA
3245Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3246 b->number, print_thread_id (tp));
49fa26b0
PA
3247
3248 /* Hide it from the user. */
3249 b->number = 0;
3250 }
3251 }
3252}
3253
6c95b8df
PA
3254/* Remove breakpoints of process PID. */
3255
3256int
3257remove_breakpoints_pid (int pid)
3258{
35df4500 3259 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3260 int val;
3261 struct inferior *inf = find_inferior_pid (pid);
3262
35df4500 3263 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3264 {
35df4500 3265 if (bl->pspace != inf->pspace)
6c95b8df
PA
3266 continue;
3267
fc126975 3268 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3269 {
834c0d03 3270 val = remove_breakpoint (bl);
6c95b8df
PA
3271 if (val != 0)
3272 return val;
3273 }
3274 }
3275 return 0;
3276}
3277
c906108c 3278int
fba45db2 3279reattach_breakpoints (int pid)
c906108c 3280{
6c95b8df 3281 struct cleanup *old_chain;
35df4500 3282 struct bp_location *bl, **blp_tmp;
c906108c 3283 int val;
86b887df 3284 struct ui_file *tmp_error_stream;
dd61ec5c 3285 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3286 struct inferior *inf;
3287 struct thread_info *tp;
3288
3289 tp = any_live_thread_of_process (pid);
3290 if (tp == NULL)
3291 return 1;
3292
3293 inf = find_inferior_pid (pid);
3294 old_chain = save_inferior_ptid ();
3295
3296 inferior_ptid = tp->ptid;
a4954f26 3297
86b887df 3298 tmp_error_stream = mem_fileopen ();
a4954f26 3299 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3300
35df4500 3301 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3302 {
35df4500 3303 if (bl->pspace != inf->pspace)
6c95b8df
PA
3304 continue;
3305
35df4500 3306 if (bl->inserted)
c5aa993b 3307 {
35df4500 3308 bl->inserted = 0;
dd61ec5c 3309 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3310 if (val != 0)
3311 {
ce696e05 3312 do_cleanups (old_chain);
c5aa993b
JM
3313 return val;
3314 }
3315 }
3316 }
ce696e05 3317 do_cleanups (old_chain);
c906108c
SS
3318 return 0;
3319}
3320
e58b0e63
PA
3321static int internal_breakpoint_number = -1;
3322
84f4c1fe
PM
3323/* Set the breakpoint number of B, depending on the value of INTERNAL.
3324 If INTERNAL is non-zero, the breakpoint number will be populated
3325 from internal_breakpoint_number and that variable decremented.
e5dd4106 3326 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3327 breakpoint_count and that value incremented. Internal breakpoints
3328 do not set the internal var bpnum. */
3329static void
3330set_breakpoint_number (int internal, struct breakpoint *b)
3331{
3332 if (internal)
3333 b->number = internal_breakpoint_number--;
3334 else
3335 {
3336 set_breakpoint_count (breakpoint_count + 1);
3337 b->number = breakpoint_count;
3338 }
3339}
3340
e62c965a 3341static struct breakpoint *
a6d9a66e 3342create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3343 CORE_ADDR address, enum bptype type,
c0a91b2b 3344 const struct breakpoint_ops *ops)
e62c965a 3345{
e62c965a
PP
3346 struct symtab_and_line sal;
3347 struct breakpoint *b;
3348
4a64f543 3349 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3350
3351 sal.pc = address;
3352 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3353 sal.pspace = current_program_space;
e62c965a 3354
06edf0c0 3355 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3356 b->number = internal_breakpoint_number--;
3357 b->disposition = disp_donttouch;
3358
3359 return b;
3360}
3361
17450429
PP
3362static const char *const longjmp_names[] =
3363 {
3364 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3365 };
3366#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3367
3368/* Per-objfile data private to breakpoint.c. */
3369struct breakpoint_objfile_data
3370{
3371 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3372 struct bound_minimal_symbol overlay_msym;
17450429
PP
3373
3374 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3375 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3376
28106bc2
SDJ
3377 /* True if we have looked for longjmp probes. */
3378 int longjmp_searched;
3379
3380 /* SystemTap probe points for longjmp (if any). */
3381 VEC (probe_p) *longjmp_probes;
3382
17450429 3383 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3384 struct bound_minimal_symbol terminate_msym;
17450429
PP
3385
3386 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3387 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3388
3389 /* True if we have looked for exception probes. */
3390 int exception_searched;
3391
3392 /* SystemTap probe points for unwinding (if any). */
3393 VEC (probe_p) *exception_probes;
17450429
PP
3394};
3395
3396static const struct objfile_data *breakpoint_objfile_key;
3397
3398/* Minimal symbol not found sentinel. */
3399static struct minimal_symbol msym_not_found;
3400
3401/* Returns TRUE if MSYM point to the "not found" sentinel. */
3402
3403static int
3404msym_not_found_p (const struct minimal_symbol *msym)
3405{
3406 return msym == &msym_not_found;
3407}
3408
3409/* Return per-objfile data needed by breakpoint.c.
3410 Allocate the data if necessary. */
3411
3412static struct breakpoint_objfile_data *
3413get_breakpoint_objfile_data (struct objfile *objfile)
3414{
3415 struct breakpoint_objfile_data *bp_objfile_data;
3416
9a3c8263
SM
3417 bp_objfile_data = ((struct breakpoint_objfile_data *)
3418 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3419 if (bp_objfile_data == NULL)
3420 {
8d749320
SM
3421 bp_objfile_data =
3422 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3423
3424 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3425 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3426 }
3427 return bp_objfile_data;
3428}
3429
28106bc2
SDJ
3430static void
3431free_breakpoint_probes (struct objfile *obj, void *data)
3432{
9a3c8263
SM
3433 struct breakpoint_objfile_data *bp_objfile_data
3434 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3435
3436 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3437 VEC_free (probe_p, bp_objfile_data->exception_probes);
3438}
3439
e62c965a 3440static void
af02033e 3441create_overlay_event_breakpoint (void)
e62c965a 3442{
69de3c6a 3443 struct objfile *objfile;
af02033e 3444 const char *const func_name = "_ovly_debug_event";
e62c965a 3445
69de3c6a
PP
3446 ALL_OBJFILES (objfile)
3447 {
3448 struct breakpoint *b;
17450429
PP
3449 struct breakpoint_objfile_data *bp_objfile_data;
3450 CORE_ADDR addr;
67994074 3451 struct explicit_location explicit_loc;
69de3c6a 3452
17450429
PP
3453 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3454
3b7344d5 3455 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3456 continue;
3457
3b7344d5 3458 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3459 {
3b7344d5 3460 struct bound_minimal_symbol m;
17450429
PP
3461
3462 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3463 if (m.minsym == NULL)
17450429
PP
3464 {
3465 /* Avoid future lookups in this objfile. */
3b7344d5 3466 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3467 continue;
3468 }
3469 bp_objfile_data->overlay_msym = m;
3470 }
e62c965a 3471
77e371c0 3472 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3473 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3474 bp_overlay_event,
3475 &internal_breakpoint_ops);
67994074
KS
3476 initialize_explicit_location (&explicit_loc);
3477 explicit_loc.function_name = ASTRDUP (func_name);
3478 b->location = new_explicit_location (&explicit_loc);
e62c965a 3479
69de3c6a
PP
3480 if (overlay_debugging == ovly_auto)
3481 {
3482 b->enable_state = bp_enabled;
3483 overlay_events_enabled = 1;
3484 }
3485 else
3486 {
3487 b->enable_state = bp_disabled;
3488 overlay_events_enabled = 0;
3489 }
e62c965a 3490 }
e62c965a
PP
3491}
3492
0fd8e87f 3493static void
af02033e 3494create_longjmp_master_breakpoint (void)
0fd8e87f 3495{
6c95b8df 3496 struct program_space *pspace;
6c95b8df
PA
3497 struct cleanup *old_chain;
3498
3499 old_chain = save_current_program_space ();
0fd8e87f 3500
6c95b8df 3501 ALL_PSPACES (pspace)
af02033e
PP
3502 {
3503 struct objfile *objfile;
3504
3505 set_current_program_space (pspace);
3506
3507 ALL_OBJFILES (objfile)
0fd8e87f 3508 {
af02033e
PP
3509 int i;
3510 struct gdbarch *gdbarch;
17450429 3511 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3512
af02033e 3513 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3514
17450429
PP
3515 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3516
28106bc2
SDJ
3517 if (!bp_objfile_data->longjmp_searched)
3518 {
25f9533e
SDJ
3519 VEC (probe_p) *ret;
3520
3521 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3522 if (ret != NULL)
3523 {
3524 /* We are only interested in checking one element. */
3525 struct probe *p = VEC_index (probe_p, ret, 0);
3526
3527 if (!can_evaluate_probe_arguments (p))
3528 {
3529 /* We cannot use the probe interface here, because it does
3530 not know how to evaluate arguments. */
3531 VEC_free (probe_p, ret);
3532 ret = NULL;
3533 }
3534 }
3535 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3536 bp_objfile_data->longjmp_searched = 1;
3537 }
3538
3539 if (bp_objfile_data->longjmp_probes != NULL)
3540 {
3541 int i;
3542 struct probe *probe;
3543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3544
3545 for (i = 0;
3546 VEC_iterate (probe_p,
3547 bp_objfile_data->longjmp_probes,
3548 i, probe);
3549 ++i)
3550 {
3551 struct breakpoint *b;
3552
729662a5
TT
3553 b = create_internal_breakpoint (gdbarch,
3554 get_probe_address (probe,
3555 objfile),
28106bc2
SDJ
3556 bp_longjmp_master,
3557 &internal_breakpoint_ops);
5b56227b
KS
3558 b->location
3559 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3560 b->enable_state = bp_disabled;
3561 }
3562
3563 continue;
3564 }
3565
0569175e
TSD
3566 if (!gdbarch_get_longjmp_target_p (gdbarch))
3567 continue;
3568
17450429 3569 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3570 {
3571 struct breakpoint *b;
af02033e 3572 const char *func_name;
17450429 3573 CORE_ADDR addr;
67994074 3574 struct explicit_location explicit_loc;
6c95b8df 3575
3b7344d5 3576 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3577 continue;
0fd8e87f 3578
17450429 3579 func_name = longjmp_names[i];
3b7344d5 3580 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3581 {
3b7344d5 3582 struct bound_minimal_symbol m;
17450429
PP
3583
3584 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3585 if (m.minsym == NULL)
17450429
PP
3586 {
3587 /* Prevent future lookups in this objfile. */
3b7344d5 3588 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3589 continue;
3590 }
3591 bp_objfile_data->longjmp_msym[i] = m;
3592 }
3593
77e371c0 3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3595 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3596 &internal_breakpoint_ops);
67994074
KS
3597 initialize_explicit_location (&explicit_loc);
3598 explicit_loc.function_name = ASTRDUP (func_name);
3599 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3600 b->enable_state = bp_disabled;
3601 }
0fd8e87f 3602 }
af02033e 3603 }
6c95b8df
PA
3604
3605 do_cleanups (old_chain);
0fd8e87f
UW
3606}
3607
af02033e 3608/* Create a master std::terminate breakpoint. */
aa7d318d 3609static void
af02033e 3610create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3611{
3612 struct program_space *pspace;
aa7d318d 3613 struct cleanup *old_chain;
af02033e 3614 const char *const func_name = "std::terminate()";
aa7d318d
TT
3615
3616 old_chain = save_current_program_space ();
3617
3618 ALL_PSPACES (pspace)
17450429
PP
3619 {
3620 struct objfile *objfile;
3621 CORE_ADDR addr;
3622
3623 set_current_program_space (pspace);
3624
aa7d318d
TT
3625 ALL_OBJFILES (objfile)
3626 {
3627 struct breakpoint *b;
17450429 3628 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3629 struct explicit_location explicit_loc;
aa7d318d 3630
17450429 3631 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3632
3b7344d5 3633 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3634 continue;
3635
3b7344d5 3636 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3637 {
3b7344d5 3638 struct bound_minimal_symbol m;
17450429
PP
3639
3640 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3641 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3642 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3643 {
3644 /* Prevent future lookups in this objfile. */
3b7344d5 3645 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3646 continue;
3647 }
3648 bp_objfile_data->terminate_msym = m;
3649 }
aa7d318d 3650
77e371c0 3651 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3652 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3653 bp_std_terminate_master,
3654 &internal_breakpoint_ops);
67994074
KS
3655 initialize_explicit_location (&explicit_loc);
3656 explicit_loc.function_name = ASTRDUP (func_name);
3657 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3658 b->enable_state = bp_disabled;
3659 }
17450429
PP
3660 }
3661
aa7d318d
TT
3662 do_cleanups (old_chain);
3663}
3664
186c406b
TT
3665/* Install a master breakpoint on the unwinder's debug hook. */
3666
70221824 3667static void
186c406b
TT
3668create_exception_master_breakpoint (void)
3669{
3670 struct objfile *objfile;
17450429 3671 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3672
3673 ALL_OBJFILES (objfile)
3674 {
17450429
PP
3675 struct breakpoint *b;
3676 struct gdbarch *gdbarch;
3677 struct breakpoint_objfile_data *bp_objfile_data;
3678 CORE_ADDR addr;
67994074 3679 struct explicit_location explicit_loc;
17450429
PP
3680
3681 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3682
28106bc2
SDJ
3683 /* We prefer the SystemTap probe point if it exists. */
3684 if (!bp_objfile_data->exception_searched)
3685 {
25f9533e
SDJ
3686 VEC (probe_p) *ret;
3687
3688 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3689
3690 if (ret != NULL)
3691 {
3692 /* We are only interested in checking one element. */
3693 struct probe *p = VEC_index (probe_p, ret, 0);
3694
3695 if (!can_evaluate_probe_arguments (p))
3696 {
3697 /* We cannot use the probe interface here, because it does
3698 not know how to evaluate arguments. */
3699 VEC_free (probe_p, ret);
3700 ret = NULL;
3701 }
3702 }
3703 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3704 bp_objfile_data->exception_searched = 1;
3705 }
3706
3707 if (bp_objfile_data->exception_probes != NULL)
3708 {
3709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3710 int i;
3711 struct probe *probe;
3712
3713 for (i = 0;
3714 VEC_iterate (probe_p,
3715 bp_objfile_data->exception_probes,
3716 i, probe);
3717 ++i)
3718 {
3719 struct breakpoint *b;
3720
729662a5
TT
3721 b = create_internal_breakpoint (gdbarch,
3722 get_probe_address (probe,
3723 objfile),
28106bc2
SDJ
3724 bp_exception_master,
3725 &internal_breakpoint_ops);
5b56227b
KS
3726 b->location
3727 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3728 b->enable_state = bp_disabled;
3729 }
3730
3731 continue;
3732 }
3733
3734 /* Otherwise, try the hook function. */
3735
3b7344d5 3736 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3737 continue;
3738
3739 gdbarch = get_objfile_arch (objfile);
186c406b 3740
3b7344d5 3741 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3742 {
3b7344d5 3743 struct bound_minimal_symbol debug_hook;
186c406b 3744
17450429 3745 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3746 if (debug_hook.minsym == NULL)
17450429 3747 {
3b7344d5 3748 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3749 continue;
3750 }
3751
3752 bp_objfile_data->exception_msym = debug_hook;
186c406b 3753 }
17450429 3754
77e371c0 3755 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3756 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3757 &current_target);
06edf0c0
PA
3758 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3759 &internal_breakpoint_ops);
67994074
KS
3760 initialize_explicit_location (&explicit_loc);
3761 explicit_loc.function_name = ASTRDUP (func_name);
3762 b->location = new_explicit_location (&explicit_loc);
17450429 3763 b->enable_state = bp_disabled;
186c406b 3764 }
186c406b
TT
3765}
3766
9ef9e6a6
KS
3767/* Does B have a location spec? */
3768
3769static int
3770breakpoint_event_location_empty_p (const struct breakpoint *b)
3771{
3772 return b->location != NULL && event_location_empty_p (b->location);
3773}
3774
c906108c 3775void
fba45db2 3776update_breakpoints_after_exec (void)
c906108c 3777{
35df4500 3778 struct breakpoint *b, *b_tmp;
876fa593 3779 struct bp_location *bploc, **bplocp_tmp;
c906108c 3780
25b22b0a
PA
3781 /* We're about to delete breakpoints from GDB's lists. If the
3782 INSERTED flag is true, GDB will try to lift the breakpoints by
3783 writing the breakpoints' "shadow contents" back into memory. The
3784 "shadow contents" are NOT valid after an exec, so GDB should not
3785 do that. Instead, the target is responsible from marking
3786 breakpoints out as soon as it detects an exec. We don't do that
3787 here instead, because there may be other attempts to delete
3788 breakpoints after detecting an exec and before reaching here. */
876fa593 3789 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3790 if (bploc->pspace == current_program_space)
3791 gdb_assert (!bploc->inserted);
c906108c 3792
35df4500 3793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3794 {
6c95b8df
PA
3795 if (b->pspace != current_program_space)
3796 continue;
3797
4a64f543 3798 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3799 if (b->type == bp_shlib_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
c906108c 3804
4a64f543 3805 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3806 if (b->type == bp_jit_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
3811
1900040c 3812 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3813 as must overlay event and longjmp master breakpoints. */
3814 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3815 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3816 || b->type == bp_exception_master)
c4093a6a
JM
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
4a64f543 3822 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3823 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
7c16b83e
PA
3829 /* Just like single-step breakpoints. */
3830 if (b->type == bp_single_step)
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
611c83ae
PA
3836 /* Longjmp and longjmp-resume breakpoints are also meaningless
3837 after an exec. */
186c406b 3838 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3839 || b->type == bp_longjmp_call_dummy
186c406b 3840 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3841 {
3842 delete_breakpoint (b);
3843 continue;
3844 }
3845
ce78b96d
JB
3846 if (b->type == bp_catchpoint)
3847 {
3848 /* For now, none of the bp_catchpoint breakpoints need to
3849 do anything at this point. In the future, if some of
3850 the catchpoints need to something, we will need to add
3851 a new method, and call this method from here. */
3852 continue;
3853 }
3854
c5aa993b
JM
3855 /* bp_finish is a special case. The only way we ought to be able
3856 to see one of these when an exec() has happened, is if the user
3857 caught a vfork, and then said "finish". Ordinarily a finish just
3858 carries them to the call-site of the current callee, by setting
3859 a temporary bp there and resuming. But in this case, the finish
3860 will carry them entirely through the vfork & exec.
3861
3862 We don't want to allow a bp_finish to remain inserted now. But
3863 we can't safely delete it, 'cause finish_command has a handle to
3864 the bp on a bpstat, and will later want to delete it. There's a
3865 chance (and I've seen it happen) that if we delete the bp_finish
3866 here, that its storage will get reused by the time finish_command
3867 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3868 We really must allow finish_command to delete a bp_finish.
3869
e5dd4106 3870 In the absence of a general solution for the "how do we know
53a5351d
JM
3871 it's safe to delete something others may have handles to?"
3872 problem, what we'll do here is just uninsert the bp_finish, and
3873 let finish_command delete it.
3874
3875 (We know the bp_finish is "doomed" in the sense that it's
3876 momentary, and will be deleted as soon as finish_command sees
3877 the inferior stopped. So it doesn't matter that the bp's
3878 address is probably bogus in the new a.out, unlike e.g., the
3879 solib breakpoints.) */
c5aa993b 3880
c5aa993b
JM
3881 if (b->type == bp_finish)
3882 {
3883 continue;
3884 }
3885
3886 /* Without a symbolic address, we have little hope of the
3887 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3888 a.out. */
9ef9e6a6 3889 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3890 {
3891 delete_breakpoint (b);
3892 continue;
3893 }
c5aa993b 3894 }
c906108c
SS
3895}
3896
3897int
d80ee84f 3898detach_breakpoints (ptid_t ptid)
c906108c 3899{
35df4500 3900 struct bp_location *bl, **blp_tmp;
3a1bae8e 3901 int val = 0;
ce696e05 3902 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3903 struct inferior *inf = current_inferior ();
c5aa993b 3904
dfd4cc63 3905 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3906 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3907
6c95b8df 3908 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3909 inferior_ptid = ptid;
35df4500 3910 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3911 {
35df4500 3912 if (bl->pspace != inf->pspace)
6c95b8df
PA
3913 continue;
3914
bd9673a4
PW
3915 /* This function must physically remove breakpoints locations
3916 from the specified ptid, without modifying the breakpoint
3917 package's state. Locations of type bp_loc_other are only
3918 maintained at GDB side. So, there is no need to remove
3919 these bp_loc_other locations. Moreover, removing these
3920 would modify the breakpoint package's state. */
3921 if (bl->loc_type == bp_loc_other)
3922 continue;
3923
35df4500 3924 if (bl->inserted)
b2b6a7da 3925 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3926 }
d03285ec 3927
ce696e05 3928 do_cleanups (old_chain);
3a1bae8e 3929 return val;
c906108c
SS
3930}
3931
35df4500 3932/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3933 Note that this is used to detach breakpoints from a child fork.
3934 When we get here, the child isn't in the inferior list, and neither
3935 do we have objects to represent its address space --- we should
35df4500 3936 *not* look at bl->pspace->aspace here. */
6c95b8df 3937
c906108c 3938static int
b2b6a7da 3939remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3940{
3941 int val;
c5aa993b 3942
35df4500
TJB
3943 /* BL is never in moribund_locations by our callers. */
3944 gdb_assert (bl->owner != NULL);
2bdf28a0 3945
74960c60
VP
3946 /* The type of none suggests that owner is actually deleted.
3947 This should not ever happen. */
35df4500 3948 gdb_assert (bl->owner->type != bp_none);
0bde7532 3949
35df4500
TJB
3950 if (bl->loc_type == bp_loc_software_breakpoint
3951 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3952 {
c02f5703
MS
3953 /* "Normal" instruction breakpoint: either the standard
3954 trap-instruction bp (bp_breakpoint), or a
3955 bp_hardware_breakpoint. */
3956
3957 /* First check to see if we have to handle an overlay. */
3958 if (overlay_debugging == ovly_off
35df4500
TJB
3959 || bl->section == NULL
3960 || !(section_is_overlay (bl->section)))
c02f5703
MS
3961 {
3962 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3963
3964 /* If we're trying to uninsert a memory breakpoint that we
3965 know is set in a dynamic object that is marked
3966 shlib_disabled, then either the dynamic object was
3967 removed with "remove-symbol-file" or with
3968 "nosharedlibrary". In the former case, we don't know
3969 whether another dynamic object might have loaded over the
3970 breakpoint's address -- the user might well let us know
3971 about it next with add-symbol-file (the whole point of
d03de421 3972 add-symbol-file is letting the user manually maintain a
08351840
PA
3973 list of dynamically loaded objects). If we have the
3974 breakpoint's shadow memory, that is, this is a software
3975 breakpoint managed by GDB, check whether the breakpoint
3976 is still inserted in memory, to avoid overwriting wrong
3977 code with stale saved shadow contents. Note that HW
3978 breakpoints don't have shadow memory, as they're
3979 implemented using a mechanism that is not dependent on
3980 being able to modify the target's memory, and as such
3981 they should always be removed. */
3982 if (bl->shlib_disabled
3983 && bl->target_info.shadow_len != 0
3984 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3985 val = 0;
3986 else
73971819 3987 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3988 }
c906108c
SS
3989 else
3990 {
4a64f543 3991 /* This breakpoint is in an overlay section.
c02f5703
MS
3992 Did we set a breakpoint at the LMA? */
3993 if (!overlay_events_enabled)
3994 {
3995 /* Yes -- overlay event support is not active, so we
3996 should have set a breakpoint at the LMA. Remove it.
3997 */
c02f5703
MS
3998 /* Ignore any failures: if the LMA is in ROM, we will
3999 have already warned when we failed to insert it. */
35df4500
TJB
4000 if (bl->loc_type == bp_loc_hardware_breakpoint)
4001 target_remove_hw_breakpoint (bl->gdbarch,
4002 &bl->overlay_target_info);
c02f5703 4003 else
35df4500 4004 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4005 &bl->overlay_target_info,
4006 reason);
c02f5703
MS
4007 }
4008 /* Did we set a breakpoint at the VMA?
4009 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4010 if (bl->inserted)
c906108c 4011 {
c02f5703
MS
4012 /* Yes -- remove it. Previously we did not bother to
4013 remove the breakpoint if the section had been
4014 unmapped, but let's not rely on that being safe. We
4015 don't know what the overlay manager might do. */
aa67235e
UW
4016
4017 /* However, we should remove *software* breakpoints only
4018 if the section is still mapped, or else we overwrite
4019 wrong code with the saved shadow contents. */
348d480f
PA
4020 if (bl->loc_type == bp_loc_hardware_breakpoint
4021 || section_is_mapped (bl->section))
73971819 4022 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4023 else
4024 val = 0;
c906108c 4025 }
c02f5703
MS
4026 else
4027 {
4028 /* No -- not inserted, so no need to remove. No error. */
4029 val = 0;
4030 }
c906108c 4031 }
879d1e6b 4032
08351840
PA
4033 /* In some cases, we might not be able to remove a breakpoint in
4034 a shared library that has already been removed, but we have
4035 not yet processed the shlib unload event. Similarly for an
4036 unloaded add-symbol-file object - the user might not yet have
4037 had the chance to remove-symbol-file it. shlib_disabled will
4038 be set if the library/object has already been removed, but
4039 the breakpoint hasn't been uninserted yet, e.g., after
4040 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4041 always-inserted mode. */
076855f9 4042 if (val
08351840
PA
4043 && (bl->loc_type == bp_loc_software_breakpoint
4044 && (bl->shlib_disabled
4045 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4046 || shared_objfile_contains_address_p (bl->pspace,
4047 bl->address))))
879d1e6b
UW
4048 val = 0;
4049
c906108c
SS
4050 if (val)
4051 return val;
b2b6a7da 4052 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4053 }
35df4500 4054 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4055 {
77b06cd7
TJB
4056 gdb_assert (bl->owner->ops != NULL
4057 && bl->owner->ops->remove_location != NULL);
4058
b2b6a7da 4059 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4060 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4061
c906108c 4062 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4063 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4064 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4065 bl->owner->number);
c906108c 4066 }
35df4500
TJB
4067 else if (bl->owner->type == bp_catchpoint
4068 && breakpoint_enabled (bl->owner)
4069 && !bl->duplicate)
ce78b96d 4070 {
77b06cd7
TJB
4071 gdb_assert (bl->owner->ops != NULL
4072 && bl->owner->ops->remove_location != NULL);
ce78b96d 4073
73971819 4074 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4075 if (val)
4076 return val;
77b06cd7 4077
b2b6a7da 4078 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4079 }
c906108c
SS
4080
4081 return 0;
4082}
4083
6c95b8df 4084static int
834c0d03 4085remove_breakpoint (struct bp_location *bl)
6c95b8df
PA
4086{
4087 int ret;
4088 struct cleanup *old_chain;
4089
35df4500
TJB
4090 /* BL is never in moribund_locations by our callers. */
4091 gdb_assert (bl->owner != NULL);
2bdf28a0 4092
6c95b8df
PA
4093 /* The type of none suggests that owner is actually deleted.
4094 This should not ever happen. */
35df4500 4095 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4096
4097 old_chain = save_current_space_and_thread ();
4098
35df4500 4099 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4100
b2b6a7da 4101 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4102
4103 do_cleanups (old_chain);
4104 return ret;
4105}
4106
c906108c
SS
4107/* Clear the "inserted" flag in all breakpoints. */
4108
25b22b0a 4109void
fba45db2 4110mark_breakpoints_out (void)
c906108c 4111{
35df4500 4112 struct bp_location *bl, **blp_tmp;
c906108c 4113
35df4500 4114 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4115 if (bl->pspace == current_program_space)
35df4500 4116 bl->inserted = 0;
c906108c
SS
4117}
4118
53a5351d
JM
4119/* Clear the "inserted" flag in all breakpoints and delete any
4120 breakpoints which should go away between runs of the program.
c906108c
SS
4121
4122 Plus other such housekeeping that has to be done for breakpoints
4123 between runs.
4124
53a5351d
JM
4125 Note: this function gets called at the end of a run (by
4126 generic_mourn_inferior) and when a run begins (by
4a64f543 4127 init_wait_for_inferior). */
c906108c
SS
4128
4129
4130
4131void
fba45db2 4132breakpoint_init_inferior (enum inf_context context)
c906108c 4133{
35df4500 4134 struct breakpoint *b, *b_tmp;
870f88f7 4135 struct bp_location *bl;
1c5cfe86 4136 int ix;
6c95b8df 4137 struct program_space *pspace = current_program_space;
c906108c 4138
50c71eaf
PA
4139 /* If breakpoint locations are shared across processes, then there's
4140 nothing to do. */
f5656ead 4141 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4142 return;
4143
1a853c52 4144 mark_breakpoints_out ();
075f6582 4145
35df4500 4146 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4147 {
6c95b8df
PA
4148 if (b->loc && b->loc->pspace != pspace)
4149 continue;
4150
c5aa993b
JM
4151 switch (b->type)
4152 {
4153 case bp_call_dummy:
e2e4d78b 4154 case bp_longjmp_call_dummy:
c906108c 4155
c5aa993b 4156 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4157 cause problems when the inferior is rerun, so we better get
4158 rid of it. */
4159
4160 case bp_watchpoint_scope:
4161
4162 /* Also get rid of scope breakpoints. */
4163
4164 case bp_shlib_event:
4165
4166 /* Also remove solib event breakpoints. Their addresses may
4167 have changed since the last time we ran the program.
4168 Actually we may now be debugging against different target;
4169 and so the solib backend that installed this breakpoint may
4170 not be used in by the target. E.g.,
4171
4172 (gdb) file prog-linux
4173 (gdb) run # native linux target
4174 ...
4175 (gdb) kill
4176 (gdb) file prog-win.exe
4177 (gdb) tar rem :9999 # remote Windows gdbserver.
4178 */
c906108c 4179
f59f708a
PA
4180 case bp_step_resume:
4181
4182 /* Also remove step-resume breakpoints. */
4183
7c16b83e
PA
4184 case bp_single_step:
4185
4186 /* Also remove single-step breakpoints. */
4187
c5aa993b
JM
4188 delete_breakpoint (b);
4189 break;
c906108c 4190
c5aa993b
JM
4191 case bp_watchpoint:
4192 case bp_hardware_watchpoint:
4193 case bp_read_watchpoint:
4194 case bp_access_watchpoint:
3a5c3e22
PA
4195 {
4196 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4197
3a5c3e22
PA
4198 /* Likewise for watchpoints on local expressions. */
4199 if (w->exp_valid_block != NULL)
4200 delete_breakpoint (b);
63000888 4201 else
3a5c3e22 4202 {
63000888
PA
4203 /* Get rid of existing locations, which are no longer
4204 valid. New ones will be created in
4205 update_watchpoint, when the inferior is restarted.
4206 The next update_global_location_list call will
4207 garbage collect them. */
4208 b->loc = NULL;
4209
4210 if (context == inf_starting)
4211 {
4212 /* Reset val field to force reread of starting value in
4213 insert_breakpoints. */
4214 if (w->val)
4215 value_free (w->val);
4216 w->val = NULL;
4217 w->val_valid = 0;
4218 }
4219 }
3a5c3e22 4220 }
c5aa993b
JM
4221 break;
4222 default:
c5aa993b
JM
4223 break;
4224 }
4225 }
1c5cfe86
PA
4226
4227 /* Get rid of the moribund locations. */
35df4500
TJB
4228 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4229 decref_bp_location (&bl);
1c5cfe86 4230 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4231}
4232
6c95b8df
PA
4233/* These functions concern about actual breakpoints inserted in the
4234 target --- to e.g. check if we need to do decr_pc adjustment or if
4235 we need to hop over the bkpt --- so we check for address space
4236 match, not program space. */
4237
c2c6d25f
JM
4238/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4239 exists at PC. It returns ordinary_breakpoint_here if it's an
4240 ordinary breakpoint, or permanent_breakpoint_here if it's a
4241 permanent breakpoint.
4242 - When continuing from a location with an ordinary breakpoint, we
4243 actually single step once before calling insert_breakpoints.
e5dd4106 4244 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4245 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4246 the target, to advance the PC past the breakpoint. */
c906108c 4247
c2c6d25f 4248enum breakpoint_here
6c95b8df 4249breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4250{
35df4500 4251 struct bp_location *bl, **blp_tmp;
c2c6d25f 4252 int any_breakpoint_here = 0;
c906108c 4253
35df4500 4254 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4255 {
35df4500
TJB
4256 if (bl->loc_type != bp_loc_software_breakpoint
4257 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4258 continue;
4259
f1310107 4260 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4261 if ((breakpoint_enabled (bl->owner)
1a853c52 4262 || bl->permanent)
f1310107 4263 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4264 {
4265 if (overlay_debugging
35df4500
TJB
4266 && section_is_overlay (bl->section)
4267 && !section_is_mapped (bl->section))
075f6582 4268 continue; /* unmapped overlay -- can't be a match */
1a853c52 4269 else if (bl->permanent)
075f6582
DJ
4270 return permanent_breakpoint_here;
4271 else
4272 any_breakpoint_here = 1;
4273 }
4274 }
c906108c 4275
f486487f 4276 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4277}
4278
d35ae833
PA
4279/* See breakpoint.h. */
4280
4281int
4282breakpoint_in_range_p (struct address_space *aspace,
4283 CORE_ADDR addr, ULONGEST len)
4284{
4285 struct bp_location *bl, **blp_tmp;
4286
4287 ALL_BP_LOCATIONS (bl, blp_tmp)
4288 {
4289 if (bl->loc_type != bp_loc_software_breakpoint
4290 && bl->loc_type != bp_loc_hardware_breakpoint)
4291 continue;
4292
4293 if ((breakpoint_enabled (bl->owner)
4294 || bl->permanent)
4295 && breakpoint_location_address_range_overlap (bl, aspace,
4296 addr, len))
4297 {
4298 if (overlay_debugging
4299 && section_is_overlay (bl->section)
4300 && !section_is_mapped (bl->section))
4301 {
4302 /* Unmapped overlay -- can't be a match. */
4303 continue;
4304 }
4305
4306 return 1;
4307 }
4308 }
4309
4310 return 0;
4311}
4312
1c5cfe86
PA
4313/* Return true if there's a moribund breakpoint at PC. */
4314
4315int
6c95b8df 4316moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4317{
4318 struct bp_location *loc;
4319 int ix;
4320
4321 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4322 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4323 return 1;
4324
4325 return 0;
4326}
c2c6d25f 4327
f7ce857f
PA
4328/* Returns non-zero iff BL is inserted at PC, in address space
4329 ASPACE. */
4330
4331static int
4332bp_location_inserted_here_p (struct bp_location *bl,
4333 struct address_space *aspace, CORE_ADDR pc)
4334{
4335 if (bl->inserted
4336 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4337 aspace, pc))
4338 {
4339 if (overlay_debugging
4340 && section_is_overlay (bl->section)
4341 && !section_is_mapped (bl->section))
4342 return 0; /* unmapped overlay -- can't be a match */
4343 else
4344 return 1;
4345 }
4346 return 0;
4347}
4348
a1fd2fa5 4349/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4350
4351int
a1fd2fa5 4352breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4353{
f7ce857f 4354 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4355
f7ce857f 4356 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4357 {
f7ce857f
PA
4358 struct bp_location *bl = *blp;
4359
35df4500
TJB
4360 if (bl->loc_type != bp_loc_software_breakpoint
4361 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4362 continue;
4363
f7ce857f
PA
4364 if (bp_location_inserted_here_p (bl, aspace, pc))
4365 return 1;
c5aa993b 4366 }
c36b740a
VP
4367 return 0;
4368}
4369
a1fd2fa5
PA
4370/* This function returns non-zero iff there is a software breakpoint
4371 inserted at PC. */
c36b740a
VP
4372
4373int
a1fd2fa5
PA
4374software_breakpoint_inserted_here_p (struct address_space *aspace,
4375 CORE_ADDR pc)
4fa8626c 4376{
f7ce857f 4377 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4378
f7ce857f 4379 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4380 {
f7ce857f
PA
4381 struct bp_location *bl = *blp;
4382
35df4500 4383 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4384 continue;
4385
f7ce857f
PA
4386 if (bp_location_inserted_here_p (bl, aspace, pc))
4387 return 1;
4fa8626c
DJ
4388 }
4389
4390 return 0;
9c02b525
PA
4391}
4392
4393/* See breakpoint.h. */
4394
4395int
4396hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4397 CORE_ADDR pc)
4398{
4399 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4400
4401 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4402 {
4403 struct bp_location *bl = *blp;
4404
4405 if (bl->loc_type != bp_loc_hardware_breakpoint)
4406 continue;
4407
4408 if (bp_location_inserted_here_p (bl, aspace, pc))
4409 return 1;
4410 }
4411
4412 return 0;
4fa8626c
DJ
4413}
4414
9093389c
PA
4415int
4416hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4417 CORE_ADDR addr, ULONGEST len)
4418{
4419 struct breakpoint *bpt;
4420
4421 ALL_BREAKPOINTS (bpt)
4422 {
4423 struct bp_location *loc;
4424
4425 if (bpt->type != bp_hardware_watchpoint
4426 && bpt->type != bp_access_watchpoint)
4427 continue;
4428
4429 if (!breakpoint_enabled (bpt))
4430 continue;
4431
4432 for (loc = bpt->loc; loc; loc = loc->next)
4433 if (loc->pspace->aspace == aspace && loc->inserted)
4434 {
4435 CORE_ADDR l, h;
4436
4437 /* Check for intersection. */
4438 l = max (loc->address, addr);
4439 h = min (loc->address + loc->length, addr + len);
4440 if (l < h)
4441 return 1;
4442 }
4443 }
4444 return 0;
4445}
c906108c 4446\f
c5aa993b 4447
c906108c
SS
4448/* bpstat stuff. External routines' interfaces are documented
4449 in breakpoint.h. */
4450
4451int
c326b90e 4452is_catchpoint (struct breakpoint *ep)
c906108c 4453{
533be4dd 4454 return (ep->type == bp_catchpoint);
c906108c
SS
4455}
4456
f431efe5
PA
4457/* Frees any storage that is part of a bpstat. Does not walk the
4458 'next' chain. */
4459
4460static void
198757a8
VP
4461bpstat_free (bpstat bs)
4462{
4463 if (bs->old_val != NULL)
4464 value_free (bs->old_val);
9add0f1b 4465 decref_counted_command_line (&bs->commands);
f431efe5 4466 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4467 xfree (bs);
4468}
4469
c906108c
SS
4470/* Clear a bpstat so that it says we are not at any breakpoint.
4471 Also free any storage that is part of a bpstat. */
4472
4473void
fba45db2 4474bpstat_clear (bpstat *bsp)
c906108c
SS
4475{
4476 bpstat p;
4477 bpstat q;
4478
4479 if (bsp == 0)
4480 return;
4481 p = *bsp;
4482 while (p != NULL)
4483 {
4484 q = p->next;
198757a8 4485 bpstat_free (p);
c906108c
SS
4486 p = q;
4487 }
4488 *bsp = NULL;
4489}
4490
4491/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4492 is part of the bpstat is copied as well. */
4493
4494bpstat
fba45db2 4495bpstat_copy (bpstat bs)
c906108c
SS
4496{
4497 bpstat p = NULL;
4498 bpstat tmp;
4499 bpstat retval = NULL;
4500
4501 if (bs == NULL)
4502 return bs;
4503
4504 for (; bs != NULL; bs = bs->next)
4505 {
4506 tmp = (bpstat) xmalloc (sizeof (*tmp));
4507 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4508 incref_counted_command_line (tmp->commands);
f431efe5 4509 incref_bp_location (tmp->bp_location_at);
31cc81e9 4510 if (bs->old_val != NULL)
3c3185ac
JK
4511 {
4512 tmp->old_val = value_copy (bs->old_val);
4513 release_value (tmp->old_val);
4514 }
31cc81e9 4515
c906108c
SS
4516 if (p == NULL)
4517 /* This is the first thing in the chain. */
4518 retval = tmp;
4519 else
4520 p->next = tmp;
4521 p = tmp;
4522 }
4523 p->next = NULL;
4524 return retval;
4525}
4526
4a64f543 4527/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4528
4529bpstat
fba45db2 4530bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4531{
c5aa993b
JM
4532 if (bsp == NULL)
4533 return NULL;
c906108c 4534
c5aa993b
JM
4535 for (; bsp != NULL; bsp = bsp->next)
4536 {
f431efe5 4537 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4538 return bsp;
4539 }
c906108c
SS
4540 return NULL;
4541}
4542
ab04a2af
TT
4543/* See breakpoint.h. */
4544
47591c29 4545int
427cd150 4546bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4547{
ab04a2af
TT
4548 for (; bsp != NULL; bsp = bsp->next)
4549 {
427cd150
TT
4550 if (bsp->breakpoint_at == NULL)
4551 {
4552 /* A moribund location can never explain a signal other than
4553 GDB_SIGNAL_TRAP. */
4554 if (sig == GDB_SIGNAL_TRAP)
47591c29 4555 return 1;
427cd150
TT
4556 }
4557 else
47591c29
PA
4558 {
4559 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4560 sig))
4561 return 1;
4562 }
ab04a2af
TT
4563 }
4564
47591c29 4565 return 0;
ab04a2af
TT
4566}
4567
4a64f543
MS
4568/* Put in *NUM the breakpoint number of the first breakpoint we are
4569 stopped at. *BSP upon return is a bpstat which points to the
4570 remaining breakpoints stopped at (but which is not guaranteed to be
4571 good for anything but further calls to bpstat_num).
4572
8671a17b
PA
4573 Return 0 if passed a bpstat which does not indicate any breakpoints.
4574 Return -1 if stopped at a breakpoint that has been deleted since
4575 we set it.
4576 Return 1 otherwise. */
c906108c
SS
4577
4578int
8671a17b 4579bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4580{
4581 struct breakpoint *b;
4582
4583 if ((*bsp) == NULL)
4584 return 0; /* No more breakpoint values */
8671a17b 4585
4a64f543
MS
4586 /* We assume we'll never have several bpstats that correspond to a
4587 single breakpoint -- otherwise, this function might return the
4588 same number more than once and this will look ugly. */
f431efe5 4589 b = (*bsp)->breakpoint_at;
8671a17b
PA
4590 *bsp = (*bsp)->next;
4591 if (b == NULL)
4592 return -1; /* breakpoint that's been deleted since */
4593
4594 *num = b->number; /* We have its number */
4595 return 1;
c906108c
SS
4596}
4597
e93ca019 4598/* See breakpoint.h. */
c906108c
SS
4599
4600void
e93ca019 4601bpstat_clear_actions (void)
c906108c 4602{
e93ca019
JK
4603 struct thread_info *tp;
4604 bpstat bs;
4605
4606 if (ptid_equal (inferior_ptid, null_ptid))
4607 return;
4608
4609 tp = find_thread_ptid (inferior_ptid);
4610 if (tp == NULL)
4611 return;
4612
4613 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4614 {
9add0f1b 4615 decref_counted_command_line (&bs->commands);
abf85f46 4616
c906108c
SS
4617 if (bs->old_val != NULL)
4618 {
4619 value_free (bs->old_val);
4620 bs->old_val = NULL;
4621 }
4622 }
4623}
4624
f3b1572e
PA
4625/* Called when a command is about to proceed the inferior. */
4626
4627static void
4628breakpoint_about_to_proceed (void)
4629{
4630 if (!ptid_equal (inferior_ptid, null_ptid))
4631 {
4632 struct thread_info *tp = inferior_thread ();
4633
4634 /* Allow inferior function calls in breakpoint commands to not
4635 interrupt the command list. When the call finishes
4636 successfully, the inferior will be standing at the same
4637 breakpoint as if nothing happened. */
16c381f0 4638 if (tp->control.in_infcall)
f3b1572e
PA
4639 return;
4640 }
4641
4642 breakpoint_proceeded = 1;
4643}
4644
4a64f543
MS
4645/* Stub for cleaning up our state if we error-out of a breakpoint
4646 command. */
c906108c 4647static void
4efb68b1 4648cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4649{
4650 executing_breakpoint_commands = 0;
4651}
4652
abf85f46
JK
4653/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4654 or its equivalent. */
4655
4656static int
4657command_line_is_silent (struct command_line *cmd)
4658{
4f45d445 4659 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4660}
4661
4a64f543
MS
4662/* Execute all the commands associated with all the breakpoints at
4663 this location. Any of these commands could cause the process to
4664 proceed beyond this point, etc. We look out for such changes by
4665 checking the global "breakpoint_proceeded" after each command.
c906108c 4666
347bddb7
PA
4667 Returns true if a breakpoint command resumed the inferior. In that
4668 case, it is the caller's responsibility to recall it again with the
4669 bpstat of the current thread. */
4670
4671static int
4672bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4673{
4674 bpstat bs;
4675 struct cleanup *old_chain;
347bddb7 4676 int again = 0;
c906108c
SS
4677
4678 /* Avoid endless recursion if a `source' command is contained
4679 in bs->commands. */
4680 if (executing_breakpoint_commands)
347bddb7 4681 return 0;
c906108c
SS
4682
4683 executing_breakpoint_commands = 1;
4684 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4685
cf6c5ffb
TT
4686 prevent_dont_repeat ();
4687
4a64f543 4688 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4689 bs = *bsp;
4690
4691 breakpoint_proceeded = 0;
4692 for (; bs != NULL; bs = bs->next)
4693 {
9add0f1b 4694 struct counted_command_line *ccmd;
6c50ab1c
JB
4695 struct command_line *cmd;
4696 struct cleanup *this_cmd_tree_chain;
4697
4698 /* Take ownership of the BSP's command tree, if it has one.
4699
4700 The command tree could legitimately contain commands like
4701 'step' and 'next', which call clear_proceed_status, which
4702 frees stop_bpstat's command tree. To make sure this doesn't
4703 free the tree we're executing out from under us, we need to
4704 take ownership of the tree ourselves. Since a given bpstat's
4705 commands are only executed once, we don't need to copy it; we
4706 can clear the pointer in the bpstat, and make sure we free
4707 the tree when we're done. */
9add0f1b
TT
4708 ccmd = bs->commands;
4709 bs->commands = NULL;
abf85f46
JK
4710 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4711 cmd = ccmd ? ccmd->commands : NULL;
4712 if (command_line_is_silent (cmd))
4713 {
4714 /* The action has been already done by bpstat_stop_status. */
4715 cmd = cmd->next;
4716 }
6c50ab1c 4717
c906108c
SS
4718 while (cmd != NULL)
4719 {
4720 execute_control_command (cmd);
4721
4722 if (breakpoint_proceeded)
4723 break;
4724 else
4725 cmd = cmd->next;
4726 }
6c50ab1c
JB
4727
4728 /* We can free this command tree now. */
4729 do_cleanups (this_cmd_tree_chain);
4730
c906108c 4731 if (breakpoint_proceeded)
32c1e744 4732 {
cb814510 4733 if (current_ui->async)
347bddb7
PA
4734 /* If we are in async mode, then the target might be still
4735 running, not stopped at any breakpoint, so nothing for
4736 us to do here -- just return to the event loop. */
4737 ;
32c1e744
VP
4738 else
4739 /* In sync mode, when execute_control_command returns
4740 we're already standing on the next breakpoint.
347bddb7
PA
4741 Breakpoint commands for that stop were not run, since
4742 execute_command does not run breakpoint commands --
4743 only command_line_handler does, but that one is not
4744 involved in execution of breakpoint commands. So, we
4745 can now execute breakpoint commands. It should be
4746 noted that making execute_command do bpstat actions is
4747 not an option -- in this case we'll have recursive
4748 invocation of bpstat for each breakpoint with a
4749 command, and can easily blow up GDB stack. Instead, we
4750 return true, which will trigger the caller to recall us
4751 with the new stop_bpstat. */
4752 again = 1;
4753 break;
32c1e744 4754 }
c906108c 4755 }
c2b8ed2c 4756 do_cleanups (old_chain);
347bddb7
PA
4757 return again;
4758}
4759
4760void
4761bpstat_do_actions (void)
4762{
353d1d73
JK
4763 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4764
347bddb7
PA
4765 /* Do any commands attached to breakpoint we are stopped at. */
4766 while (!ptid_equal (inferior_ptid, null_ptid)
4767 && target_has_execution
4768 && !is_exited (inferior_ptid)
4769 && !is_executing (inferior_ptid))
4770 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4771 and only return when it is stopped at the next breakpoint, we
4772 keep doing breakpoint actions until it returns false to
4773 indicate the inferior was not resumed. */
16c381f0 4774 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4775 break;
353d1d73
JK
4776
4777 discard_cleanups (cleanup_if_error);
c906108c
SS
4778}
4779
fa4727a6
DJ
4780/* Print out the (old or new) value associated with a watchpoint. */
4781
4782static void
4783watchpoint_value_print (struct value *val, struct ui_file *stream)
4784{
4785 if (val == NULL)
4786 fprintf_unfiltered (stream, _("<unreadable>"));
4787 else
79a45b7d
TT
4788 {
4789 struct value_print_options opts;
4790 get_user_print_options (&opts);
4791 value_print (val, stream, &opts);
4792 }
fa4727a6
DJ
4793}
4794
f303dbd6
PA
4795/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4796 debugging multiple threads. */
4797
4798void
4799maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4800{
4801 if (ui_out_is_mi_like_p (uiout))
4802 return;
4803
4804 ui_out_text (uiout, "\n");
4805
4806 if (show_thread_that_caused_stop ())
4807 {
4808 const char *name;
4809 struct thread_info *thr = inferior_thread ();
4810
4811 ui_out_text (uiout, "Thread ");
4812 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4813
4814 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4815 if (name != NULL)
4816 {
4817 ui_out_text (uiout, " \"");
4818 ui_out_field_fmt (uiout, "name", "%s", name);
4819 ui_out_text (uiout, "\"");
4820 }
4821
4822 ui_out_text (uiout, " hit ");
4823 }
4824}
4825
e514a9d6 4826/* Generic routine for printing messages indicating why we
4a64f543 4827 stopped. The behavior of this function depends on the value
e514a9d6
JM
4828 'print_it' in the bpstat structure. Under some circumstances we
4829 may decide not to print anything here and delegate the task to
4a64f543 4830 normal_stop(). */
e514a9d6
JM
4831
4832static enum print_stop_action
4833print_bp_stop_message (bpstat bs)
4834{
4835 switch (bs->print_it)
4836 {
4837 case print_it_noop:
4a64f543 4838 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4839 return PRINT_UNKNOWN;
4840 break;
4841
4842 case print_it_done:
4843 /* We still want to print the frame, but we already printed the
4a64f543 4844 relevant messages. */
e514a9d6
JM
4845 return PRINT_SRC_AND_LOC;
4846 break;
4847
4848 case print_it_normal:
4f8d1dc6 4849 {
f431efe5
PA
4850 struct breakpoint *b = bs->breakpoint_at;
4851
1a6a67de
TJB
4852 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4853 which has since been deleted. */
4854 if (b == NULL)
4855 return PRINT_UNKNOWN;
4856
348d480f
PA
4857 /* Normal case. Call the breakpoint's print_it method. */
4858 return b->ops->print_it (bs);
4f8d1dc6 4859 }
348d480f 4860 break;
3086aeae 4861
e514a9d6 4862 default:
8e65ff28 4863 internal_error (__FILE__, __LINE__,
e2e0b3e5 4864 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4865 break;
c906108c 4866 }
c906108c
SS
4867}
4868
edcc5120
TT
4869/* A helper function that prints a shared library stopped event. */
4870
4871static void
4872print_solib_event (int is_catchpoint)
4873{
4874 int any_deleted
4875 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4876 int any_added
4877 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4878
4879 if (!is_catchpoint)
4880 {
4881 if (any_added || any_deleted)
4882 ui_out_text (current_uiout,
4883 _("Stopped due to shared library event:\n"));
4884 else
4885 ui_out_text (current_uiout,
4886 _("Stopped due to shared library event (no "
4887 "libraries added or removed)\n"));
4888 }
4889
4890 if (ui_out_is_mi_like_p (current_uiout))
4891 ui_out_field_string (current_uiout, "reason",
4892 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4893
4894 if (any_deleted)
4895 {
4896 struct cleanup *cleanup;
4897 char *name;
4898 int ix;
4899
4900 ui_out_text (current_uiout, _(" Inferior unloaded "));
4901 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4902 "removed");
4903 for (ix = 0;
4904 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4905 ix, name);
4906 ++ix)
4907 {
4908 if (ix > 0)
4909 ui_out_text (current_uiout, " ");
4910 ui_out_field_string (current_uiout, "library", name);
4911 ui_out_text (current_uiout, "\n");
4912 }
4913
4914 do_cleanups (cleanup);
4915 }
4916
4917 if (any_added)
4918 {
4919 struct so_list *iter;
4920 int ix;
4921 struct cleanup *cleanup;
4922
4923 ui_out_text (current_uiout, _(" Inferior loaded "));
4924 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4925 "added");
4926 for (ix = 0;
4927 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4928 ix, iter);
4929 ++ix)
4930 {
4931 if (ix > 0)
4932 ui_out_text (current_uiout, " ");
4933 ui_out_field_string (current_uiout, "library", iter->so_name);
4934 ui_out_text (current_uiout, "\n");
4935 }
4936
4937 do_cleanups (cleanup);
4938 }
4939}
4940
e514a9d6
JM
4941/* Print a message indicating what happened. This is called from
4942 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4943 list - a list of the eventpoints that caused this stop. KIND is
4944 the target_waitkind for the stopping event. This
e514a9d6
JM
4945 routine calls the generic print routine for printing a message
4946 about reasons for stopping. This will print (for example) the
4947 "Breakpoint n," part of the output. The return value of this
4948 routine is one of:
c906108c 4949
4a64f543 4950 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4951 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4952 code to print the location. An example is
c5aa993b
JM
4953 "Breakpoint 1, " which should be followed by
4954 the location.
917317f4 4955 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4956 to also print the location part of the message.
4957 An example is the catch/throw messages, which
4a64f543 4958 don't require a location appended to the end.
917317f4 4959 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4960 further info to be printed. */
c906108c 4961
917317f4 4962enum print_stop_action
36dfb11c 4963bpstat_print (bpstat bs, int kind)
c906108c 4964{
f486487f 4965 enum print_stop_action val;
c5aa993b 4966
c906108c 4967 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4968 (Currently all watchpoints go on the bpstat whether hit or not.
4969 That probably could (should) be changed, provided care is taken
c906108c 4970 with respect to bpstat_explains_signal). */
e514a9d6
JM
4971 for (; bs; bs = bs->next)
4972 {
4973 val = print_bp_stop_message (bs);
4974 if (val == PRINT_SRC_ONLY
4975 || val == PRINT_SRC_AND_LOC
4976 || val == PRINT_NOTHING)
4977 return val;
4978 }
c906108c 4979
36dfb11c
TT
4980 /* If we had hit a shared library event breakpoint,
4981 print_bp_stop_message would print out this message. If we hit an
4982 OS-level shared library event, do the same thing. */
4983 if (kind == TARGET_WAITKIND_LOADED)
4984 {
edcc5120 4985 print_solib_event (0);
36dfb11c
TT
4986 return PRINT_NOTHING;
4987 }
4988
e514a9d6 4989 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4990 with and nothing was printed. */
917317f4 4991 return PRINT_UNKNOWN;
c906108c
SS
4992}
4993
c42bd95a
DE
4994/* Evaluate the expression EXP and return 1 if value is zero.
4995 This returns the inverse of the condition because it is called
4996 from catch_errors which returns 0 if an exception happened, and if an
4997 exception happens we want execution to stop.
4a64f543 4998 The argument is a "struct expression *" that has been cast to a
c42bd95a 4999 "void *" to make it pass through catch_errors. */
c906108c
SS
5000
5001static int
4efb68b1 5002breakpoint_cond_eval (void *exp)
c906108c 5003{
278cd55f 5004 struct value *mark = value_mark ();
c5aa993b 5005 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 5006
c906108c
SS
5007 value_free_to_mark (mark);
5008 return i;
5009}
5010
5760d0ab 5011/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5012
5013static bpstat
5760d0ab 5014bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5015{
5016 bpstat bs;
5017
5018 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5019 bs->next = NULL;
5020 **bs_link_pointer = bs;
5021 *bs_link_pointer = &bs->next;
f431efe5
PA
5022 bs->breakpoint_at = bl->owner;
5023 bs->bp_location_at = bl;
5024 incref_bp_location (bl);
c906108c
SS
5025 /* If the condition is false, etc., don't do the commands. */
5026 bs->commands = NULL;
5027 bs->old_val = NULL;
5028 bs->print_it = print_it_normal;
5029 return bs;
5030}
5031\f
d983da9c
DJ
5032/* The target has stopped with waitstatus WS. Check if any hardware
5033 watchpoints have triggered, according to the target. */
5034
5035int
5036watchpoints_triggered (struct target_waitstatus *ws)
5037{
d92524f1 5038 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5039 CORE_ADDR addr;
5040 struct breakpoint *b;
5041
5042 if (!stopped_by_watchpoint)
5043 {
5044 /* We were not stopped by a watchpoint. Mark all watchpoints
5045 as not triggered. */
5046 ALL_BREAKPOINTS (b)
cc60f2e3 5047 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5048 {
5049 struct watchpoint *w = (struct watchpoint *) b;
5050
5051 w->watchpoint_triggered = watch_triggered_no;
5052 }
d983da9c
DJ
5053
5054 return 0;
5055 }
5056
5057 if (!target_stopped_data_address (&current_target, &addr))
5058 {
5059 /* We were stopped by a watchpoint, but we don't know where.
5060 Mark all watchpoints as unknown. */
5061 ALL_BREAKPOINTS (b)
cc60f2e3 5062 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5063 {
5064 struct watchpoint *w = (struct watchpoint *) b;
5065
5066 w->watchpoint_triggered = watch_triggered_unknown;
5067 }
d983da9c 5068
3c4797ba 5069 return 1;
d983da9c
DJ
5070 }
5071
5072 /* The target could report the data address. Mark watchpoints
5073 affected by this data address as triggered, and all others as not
5074 triggered. */
5075
5076 ALL_BREAKPOINTS (b)
cc60f2e3 5077 if (is_hardware_watchpoint (b))
d983da9c 5078 {
3a5c3e22 5079 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5080 struct bp_location *loc;
d983da9c 5081
3a5c3e22 5082 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5083 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5084 {
3a5c3e22 5085 if (is_masked_watchpoint (b))
9c06b0b4 5086 {
3a5c3e22
PA
5087 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5088 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5089
5090 if (newaddr == start)
5091 {
3a5c3e22 5092 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5093 break;
5094 }
5095 }
5096 /* Exact match not required. Within range is sufficient. */
5097 else if (target_watchpoint_addr_within_range (&current_target,
5098 addr, loc->address,
5099 loc->length))
5100 {
3a5c3e22 5101 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5102 break;
5103 }
5104 }
d983da9c
DJ
5105 }
5106
5107 return 1;
5108}
5109
c906108c
SS
5110/* Possible return values for watchpoint_check (this can't be an enum
5111 because of check_errors). */
5112/* The watchpoint has been deleted. */
5113#define WP_DELETED 1
5114/* The value has changed. */
5115#define WP_VALUE_CHANGED 2
5116/* The value has not changed. */
5117#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5118/* Ignore this watchpoint, no matter if the value changed or not. */
5119#define WP_IGNORE 4
c906108c
SS
5120
5121#define BP_TEMPFLAG 1
5122#define BP_HARDWAREFLAG 2
5123
4a64f543
MS
5124/* Evaluate watchpoint condition expression and check if its value
5125 changed.
553e4c11
JB
5126
5127 P should be a pointer to struct bpstat, but is defined as a void *
5128 in order for this function to be usable with catch_errors. */
c906108c
SS
5129
5130static int
4efb68b1 5131watchpoint_check (void *p)
c906108c
SS
5132{
5133 bpstat bs = (bpstat) p;
3a5c3e22 5134 struct watchpoint *b;
c906108c
SS
5135 struct frame_info *fr;
5136 int within_current_scope;
5137
f431efe5 5138 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5139 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5140 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5141
f6bc2008
PA
5142 /* If this is a local watchpoint, we only want to check if the
5143 watchpoint frame is in scope if the current thread is the thread
5144 that was used to create the watchpoint. */
5145 if (!watchpoint_in_thread_scope (b))
60e1c644 5146 return WP_IGNORE;
f6bc2008 5147
c906108c
SS
5148 if (b->exp_valid_block == NULL)
5149 within_current_scope = 1;
5150 else
5151 {
edb3359d
DJ
5152 struct frame_info *frame = get_current_frame ();
5153 struct gdbarch *frame_arch = get_frame_arch (frame);
5154 CORE_ADDR frame_pc = get_frame_pc (frame);
5155
c9cf6e20 5156 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5157 still in the function but the stack frame has already been
5158 invalidated. Since we can't rely on the values of local
5159 variables after the stack has been destroyed, we are treating
5160 the watchpoint in that state as `not changed' without further
5161 checking. Don't mark watchpoints as changed if the current
5162 frame is in an epilogue - even if they are in some other
5163 frame, our view of the stack is likely to be wrong and
5164 frame_find_by_id could error out. */
c9cf6e20 5165 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5166 return WP_IGNORE;
a0f49112 5167
101dcfbe 5168 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5169 within_current_scope = (fr != NULL);
69fbadd5
DJ
5170
5171 /* If we've gotten confused in the unwinder, we might have
5172 returned a frame that can't describe this variable. */
edb3359d
DJ
5173 if (within_current_scope)
5174 {
5175 struct symbol *function;
5176
5177 function = get_frame_function (fr);
5178 if (function == NULL
5179 || !contained_in (b->exp_valid_block,
5180 SYMBOL_BLOCK_VALUE (function)))
5181 within_current_scope = 0;
5182 }
69fbadd5 5183
edb3359d 5184 if (within_current_scope)
c906108c
SS
5185 /* If we end up stopping, the current frame will get selected
5186 in normal_stop. So this call to select_frame won't affect
5187 the user. */
0f7d239c 5188 select_frame (fr);
c906108c 5189 }
c5aa993b 5190
c906108c
SS
5191 if (within_current_scope)
5192 {
4a64f543
MS
5193 /* We use value_{,free_to_}mark because it could be a *long*
5194 time before we return to the command level and call
5195 free_all_values. We can't call free_all_values because we
5196 might be in the middle of evaluating a function call. */
c906108c 5197
0cf6dd15 5198 int pc = 0;
9c06b0b4 5199 struct value *mark;
fa4727a6
DJ
5200 struct value *new_val;
5201
3a5c3e22 5202 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5203 /* Since we don't know the exact trigger address (from
5204 stopped_data_address), just tell the user we've triggered
5205 a mask watchpoint. */
5206 return WP_VALUE_CHANGED;
5207
5208 mark = value_mark ();
3a1115a0 5209 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5210
bb9d5f81
PP
5211 if (b->val_bitsize != 0)
5212 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5213
4a64f543
MS
5214 /* We use value_equal_contents instead of value_equal because
5215 the latter coerces an array to a pointer, thus comparing just
5216 the address of the array instead of its contents. This is
5217 not what we want. */
fa4727a6 5218 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5219 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5220 {
fa4727a6
DJ
5221 if (new_val != NULL)
5222 {
5223 release_value (new_val);
5224 value_free_to_mark (mark);
5225 }
c906108c
SS
5226 bs->old_val = b->val;
5227 b->val = new_val;
fa4727a6 5228 b->val_valid = 1;
c906108c
SS
5229 return WP_VALUE_CHANGED;
5230 }
5231 else
5232 {
60e1c644 5233 /* Nothing changed. */
c906108c 5234 value_free_to_mark (mark);
c906108c
SS
5235 return WP_VALUE_NOT_CHANGED;
5236 }
5237 }
5238 else
5239 {
468afe6c 5240 struct switch_thru_all_uis state;
79a45e25 5241
c906108c 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
PA
5255
5256 SWITCH_THRU_ALL_UIS (state)
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
PA
5436 {
5437 struct switch_thru_all_uis state;
5438
5439 SWITCH_THRU_ALL_UIS (state)
5440 {
5441 printf_filtered (_("Watchpoint %d deleted.\n"),
5442 b->base.number);
5443 }
5444 watchpoint_del_at_next_stop (b);
5445 /* We've already printed what needs to be printed. */
5446 bs->print_it = print_it_done;
5447 }
18a18393
VP
5448 break;
5449 }
5450 }
5451 else /* must_check_value == 0 */
5452 {
5453 /* This is a case where some watchpoint(s) triggered, but
5454 not at the address of this watchpoint, or else no
5455 watchpoint triggered after all. So don't print
5456 anything for this watchpoint. */
5457 bs->print_it = print_it_noop;
5458 bs->stop = 0;
5459 }
5460 }
5461}
5462
7d4df6a4
DE
5463/* For breakpoints that are currently marked as telling gdb to stop,
5464 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5465 of breakpoint referred to by BS. If we should not stop for this
5466 breakpoint, set BS->stop to 0. */
f431efe5 5467
18a18393
VP
5468static void
5469bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5470{
2bdf28a0
JK
5471 const struct bp_location *bl;
5472 struct breakpoint *b;
7d4df6a4
DE
5473 int value_is_zero = 0;
5474 struct expression *cond;
5475
5476 gdb_assert (bs->stop);
2bdf28a0
JK
5477
5478 /* BS is built for existing struct breakpoint. */
f431efe5 5479 bl = bs->bp_location_at;
2bdf28a0 5480 gdb_assert (bl != NULL);
f431efe5 5481 b = bs->breakpoint_at;
2bdf28a0 5482 gdb_assert (b != NULL);
18a18393 5483
b775012e
LM
5484 /* Even if the target evaluated the condition on its end and notified GDB, we
5485 need to do so again since GDB does not know if we stopped due to a
5486 breakpoint or a single step breakpoint. */
5487
18a18393 5488 if (frame_id_p (b->frame_id)
edb3359d 5489 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5490 {
7d4df6a4
DE
5491 bs->stop = 0;
5492 return;
5493 }
60e1c644 5494
12ab52e9
PA
5495 /* If this is a thread/task-specific breakpoint, don't waste cpu
5496 evaluating the condition if this isn't the specified
5497 thread/task. */
5d5658a1 5498 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5499 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5500
6c1b0f7b
DE
5501 {
5502 bs->stop = 0;
5503 return;
5504 }
5505
6dddc817
DE
5506 /* Evaluate extension language breakpoints that have a "stop" method
5507 implemented. */
5508 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5509
7d4df6a4
DE
5510 if (is_watchpoint (b))
5511 {
5512 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5513
7d4df6a4
DE
5514 cond = w->cond_exp;
5515 }
5516 else
5517 cond = bl->cond;
60e1c644 5518
7d4df6a4
DE
5519 if (cond && b->disposition != disp_del_at_next_stop)
5520 {
5521 int within_current_scope = 1;
5522 struct watchpoint * w;
60e1c644 5523
7d4df6a4
DE
5524 /* We use value_mark and value_free_to_mark because it could
5525 be a long time before we return to the command level and
5526 call free_all_values. We can't call free_all_values
5527 because we might be in the middle of evaluating a
5528 function call. */
5529 struct value *mark = value_mark ();
5530
5531 if (is_watchpoint (b))
5532 w = (struct watchpoint *) b;
5533 else
5534 w = NULL;
5535
5536 /* Need to select the frame, with all that implies so that
5537 the conditions will have the right context. Because we
5538 use the frame, we will not see an inlined function's
5539 variables when we arrive at a breakpoint at the start
5540 of the inlined function; the current frame will be the
5541 call site. */
5542 if (w == NULL || w->cond_exp_valid_block == NULL)
5543 select_frame (get_current_frame ());
5544 else
18a18393 5545 {
7d4df6a4
DE
5546 struct frame_info *frame;
5547
5548 /* For local watchpoint expressions, which particular
5549 instance of a local is being watched matters, so we
5550 keep track of the frame to evaluate the expression
5551 in. To evaluate the condition however, it doesn't
5552 really matter which instantiation of the function
5553 where the condition makes sense triggers the
5554 watchpoint. This allows an expression like "watch
5555 global if q > 10" set in `func', catch writes to
5556 global on all threads that call `func', or catch
5557 writes on all recursive calls of `func' by a single
5558 thread. We simply always evaluate the condition in
5559 the innermost frame that's executing where it makes
5560 sense to evaluate the condition. It seems
5561 intuitive. */
5562 frame = block_innermost_frame (w->cond_exp_valid_block);
5563 if (frame != NULL)
5564 select_frame (frame);
5565 else
5566 within_current_scope = 0;
18a18393 5567 }
7d4df6a4
DE
5568 if (within_current_scope)
5569 value_is_zero
5570 = catch_errors (breakpoint_cond_eval, cond,
5571 "Error in testing breakpoint condition:\n",
5572 RETURN_MASK_ALL);
5573 else
18a18393 5574 {
7d4df6a4
DE
5575 warning (_("Watchpoint condition cannot be tested "
5576 "in the current scope"));
5577 /* If we failed to set the right context for this
5578 watchpoint, unconditionally report it. */
5579 value_is_zero = 0;
18a18393 5580 }
7d4df6a4
DE
5581 /* FIXME-someday, should give breakpoint #. */
5582 value_free_to_mark (mark);
18a18393 5583 }
7d4df6a4
DE
5584
5585 if (cond && value_is_zero)
5586 {
5587 bs->stop = 0;
5588 }
7d4df6a4
DE
5589 else if (b->ignore_count > 0)
5590 {
5591 b->ignore_count--;
5592 bs->stop = 0;
5593 /* Increase the hit count even though we don't stop. */
5594 ++(b->hit_count);
5595 observer_notify_breakpoint_modified (b);
5596 }
18a18393
VP
5597}
5598
1cf4d951
PA
5599/* Returns true if we need to track moribund locations of LOC's type
5600 on the current target. */
5601
5602static int
5603need_moribund_for_location_type (struct bp_location *loc)
5604{
5605 return ((loc->loc_type == bp_loc_software_breakpoint
5606 && !target_supports_stopped_by_sw_breakpoint ())
5607 || (loc->loc_type == bp_loc_hardware_breakpoint
5608 && !target_supports_stopped_by_hw_breakpoint ()));
5609}
5610
18a18393 5611
9709f61c 5612/* Get a bpstat associated with having just stopped at address
d983da9c 5613 BP_ADDR in thread PTID.
c906108c 5614
d983da9c 5615 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5616 don't understand this stop. Result is a chain of bpstat's such
5617 that:
c906108c 5618
c5aa993b 5619 if we don't understand the stop, the result is a null pointer.
c906108c 5620
c5aa993b 5621 if we understand why we stopped, the result is not null.
c906108c 5622
c5aa993b
JM
5623 Each element of the chain refers to a particular breakpoint or
5624 watchpoint at which we have stopped. (We may have stopped for
5625 several reasons concurrently.)
c906108c 5626
c5aa993b
JM
5627 Each element of the chain has valid next, breakpoint_at,
5628 commands, FIXME??? fields. */
c906108c
SS
5629
5630bpstat
6c95b8df 5631bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5632 CORE_ADDR bp_addr, ptid_t ptid,
5633 const struct target_waitstatus *ws)
c906108c 5634{
0d381245 5635 struct breakpoint *b = NULL;
afe38095 5636 struct bp_location *bl;
20874c92 5637 struct bp_location *loc;
5760d0ab
JK
5638 /* First item of allocated bpstat's. */
5639 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5640 /* Pointer to the last thing in the chain currently. */
5760d0ab 5641 bpstat bs;
20874c92 5642 int ix;
429374b8 5643 int need_remove_insert;
f431efe5 5644 int removed_any;
c906108c 5645
f431efe5
PA
5646 /* First, build the bpstat chain with locations that explain a
5647 target stop, while being careful to not set the target running,
5648 as that may invalidate locations (in particular watchpoint
5649 locations are recreated). Resuming will happen here with
5650 breakpoint conditions or watchpoint expressions that include
5651 inferior function calls. */
c5aa993b 5652
429374b8
JK
5653 ALL_BREAKPOINTS (b)
5654 {
1a853c52 5655 if (!breakpoint_enabled (b))
429374b8 5656 continue;
a5606eee 5657
429374b8
JK
5658 for (bl = b->loc; bl != NULL; bl = bl->next)
5659 {
4a64f543
MS
5660 /* For hardware watchpoints, we look only at the first
5661 location. The watchpoint_check function will work on the
5662 entire expression, not the individual locations. For
5663 read watchpoints, the watchpoints_triggered function has
5664 checked all locations already. */
429374b8
JK
5665 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5666 break;
18a18393 5667
f6592439 5668 if (!bl->enabled || bl->shlib_disabled)
429374b8 5669 continue;
c5aa993b 5670
09ac7c10 5671 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5672 continue;
c5aa993b 5673
4a64f543
MS
5674 /* Come here if it's a watchpoint, or if the break address
5675 matches. */
c5aa993b 5676
4a64f543
MS
5677 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5678 explain stop. */
c5aa993b 5679
f431efe5
PA
5680 /* Assume we stop. Should we find a watchpoint that is not
5681 actually triggered, or if the condition of the breakpoint
5682 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5683 bs->stop = 1;
5684 bs->print = 1;
d983da9c 5685
f431efe5
PA
5686 /* If this is a scope breakpoint, mark the associated
5687 watchpoint as triggered so that we will handle the
5688 out-of-scope event. We'll get to the watchpoint next
5689 iteration. */
d0fb5eae 5690 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5691 {
5692 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5693
5694 w->watchpoint_triggered = watch_triggered_yes;
5695 }
f431efe5
PA
5696 }
5697 }
5698
7c16b83e 5699 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5700 if (!target_supports_stopped_by_sw_breakpoint ()
5701 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5702 {
1cf4d951 5703 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5704 {
1cf4d951
PA
5705 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5706 && need_moribund_for_location_type (loc))
5707 {
5708 bs = bpstat_alloc (loc, &bs_link);
5709 /* For hits of moribund locations, we should just proceed. */
5710 bs->stop = 0;
5711 bs->print = 0;
5712 bs->print_it = print_it_noop;
5713 }
f431efe5
PA
5714 }
5715 }
5716
edcc5120
TT
5717 /* A bit of special processing for shlib breakpoints. We need to
5718 process solib loading here, so that the lists of loaded and
5719 unloaded libraries are correct before we handle "catch load" and
5720 "catch unload". */
5721 for (bs = bs_head; bs != NULL; bs = bs->next)
5722 {
5d268276 5723 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5724 {
5725 handle_solib_event ();
5726 break;
5727 }
5728 }
5729
f431efe5
PA
5730 /* Now go through the locations that caused the target to stop, and
5731 check whether we're interested in reporting this stop to higher
5732 layers, or whether we should resume the target transparently. */
5733
5734 removed_any = 0;
5735
5760d0ab 5736 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5737 {
5738 if (!bs->stop)
5739 continue;
5740
f431efe5 5741 b = bs->breakpoint_at;
348d480f
PA
5742 b->ops->check_status (bs);
5743 if (bs->stop)
28010a5d 5744 {
348d480f 5745 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5746
429374b8
JK
5747 if (bs->stop)
5748 {
5749 ++(b->hit_count);
8d3788bd 5750 observer_notify_breakpoint_modified (b);
c906108c 5751
4a64f543 5752 /* We will stop here. */
429374b8
JK
5753 if (b->disposition == disp_disable)
5754 {
816338b5 5755 --(b->enable_count);
1a853c52 5756 if (b->enable_count <= 0)
429374b8 5757 b->enable_state = bp_disabled;
f431efe5 5758 removed_any = 1;
429374b8
JK
5759 }
5760 if (b->silent)
5761 bs->print = 0;
5762 bs->commands = b->commands;
9add0f1b 5763 incref_counted_command_line (bs->commands);
abf85f46
JK
5764 if (command_line_is_silent (bs->commands
5765 ? bs->commands->commands : NULL))
5766 bs->print = 0;
9d6e6e84
HZ
5767
5768 b->ops->after_condition_true (bs);
429374b8
JK
5769 }
5770
348d480f 5771 }
a9b3a50f
PA
5772
5773 /* Print nothing for this entry if we don't stop or don't
5774 print. */
5775 if (!bs->stop || !bs->print)
5776 bs->print_it = print_it_noop;
429374b8 5777 }
876fa593 5778
d983da9c
DJ
5779 /* If we aren't stopping, the value of some hardware watchpoint may
5780 not have changed, but the intermediate memory locations we are
5781 watching may have. Don't bother if we're stopping; this will get
5782 done later. */
d832cb68 5783 need_remove_insert = 0;
5760d0ab
JK
5784 if (! bpstat_causes_stop (bs_head))
5785 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5786 if (!bs->stop
f431efe5
PA
5787 && bs->breakpoint_at
5788 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5789 {
3a5c3e22
PA
5790 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5791
5792 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5793 need_remove_insert = 1;
d983da9c
DJ
5794 }
5795
d832cb68 5796 if (need_remove_insert)
44702360 5797 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5798 else if (removed_any)
44702360 5799 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5800
5760d0ab 5801 return bs_head;
c906108c 5802}
628fe4e4
JK
5803
5804static void
5805handle_jit_event (void)
5806{
5807 struct frame_info *frame;
5808 struct gdbarch *gdbarch;
5809
243a9253
PA
5810 if (debug_infrun)
5811 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5812
628fe4e4
JK
5813 /* Switch terminal for any messages produced by
5814 breakpoint_re_set. */
5815 target_terminal_ours_for_output ();
5816
5817 frame = get_current_frame ();
5818 gdbarch = get_frame_arch (frame);
5819
5820 jit_event_handler (gdbarch);
5821
5822 target_terminal_inferior ();
5823}
5824
5825/* Prepare WHAT final decision for infrun. */
5826
5827/* Decide what infrun needs to do with this bpstat. */
5828
c906108c 5829struct bpstat_what
0e30163f 5830bpstat_what (bpstat bs_head)
c906108c 5831{
c906108c 5832 struct bpstat_what retval;
0e30163f 5833 bpstat bs;
c906108c 5834
628fe4e4 5835 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5836 retval.call_dummy = STOP_NONE;
186c406b 5837 retval.is_longjmp = 0;
628fe4e4 5838
0e30163f 5839 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5840 {
628fe4e4
JK
5841 /* Extract this BS's action. After processing each BS, we check
5842 if its action overrides all we've seem so far. */
5843 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5844 enum bptype bptype;
5845
c906108c 5846 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5847 {
5848 /* I suspect this can happen if it was a momentary
5849 breakpoint which has since been deleted. */
5850 bptype = bp_none;
5851 }
20874c92 5852 else
f431efe5 5853 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5854
5855 switch (bptype)
c906108c
SS
5856 {
5857 case bp_none:
628fe4e4 5858 break;
c906108c
SS
5859 case bp_breakpoint:
5860 case bp_hardware_breakpoint:
7c16b83e 5861 case bp_single_step:
c906108c
SS
5862 case bp_until:
5863 case bp_finish:
a9b3a50f 5864 case bp_shlib_event:
c906108c
SS
5865 if (bs->stop)
5866 {
5867 if (bs->print)
628fe4e4 5868 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5869 else
628fe4e4 5870 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5871 }
5872 else
628fe4e4 5873 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5874 break;
5875 case bp_watchpoint:
5876 case bp_hardware_watchpoint:
5877 case bp_read_watchpoint:
5878 case bp_access_watchpoint:
5879 if (bs->stop)
5880 {
5881 if (bs->print)
628fe4e4 5882 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5883 else
628fe4e4 5884 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5885 }
5886 else
628fe4e4
JK
5887 {
5888 /* There was a watchpoint, but we're not stopping.
5889 This requires no further action. */
5890 }
c906108c
SS
5891 break;
5892 case bp_longjmp:
e2e4d78b 5893 case bp_longjmp_call_dummy:
186c406b 5894 case bp_exception:
0a39bb32
PA
5895 if (bs->stop)
5896 {
5897 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5898 retval.is_longjmp = bptype != bp_exception;
5899 }
5900 else
5901 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5902 break;
5903 case bp_longjmp_resume:
186c406b 5904 case bp_exception_resume:
0a39bb32
PA
5905 if (bs->stop)
5906 {
5907 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5908 retval.is_longjmp = bptype == bp_longjmp_resume;
5909 }
5910 else
5911 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5912 break;
5913 case bp_step_resume:
5914 if (bs->stop)
628fe4e4
JK
5915 this_action = BPSTAT_WHAT_STEP_RESUME;
5916 else
c906108c 5917 {
628fe4e4
JK
5918 /* It is for the wrong frame. */
5919 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5920 }
c906108c 5921 break;
2c03e5be
PA
5922 case bp_hp_step_resume:
5923 if (bs->stop)
5924 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5925 else
5926 {
5927 /* It is for the wrong frame. */
5928 this_action = BPSTAT_WHAT_SINGLE;
5929 }
5930 break;
c906108c 5931 case bp_watchpoint_scope:
c4093a6a 5932 case bp_thread_event:
1900040c 5933 case bp_overlay_event:
0fd8e87f 5934 case bp_longjmp_master:
aa7d318d 5935 case bp_std_terminate_master:
186c406b 5936 case bp_exception_master:
628fe4e4 5937 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5938 break;
ce78b96d 5939 case bp_catchpoint:
c5aa993b
JM
5940 if (bs->stop)
5941 {
5942 if (bs->print)
628fe4e4 5943 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5944 else
628fe4e4 5945 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5946 }
5947 else
628fe4e4
JK
5948 {
5949 /* There was a catchpoint, but we're not stopping.
5950 This requires no further action. */
5951 }
5952 break;
628fe4e4 5953 case bp_jit_event:
628fe4e4 5954 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5955 break;
c906108c 5956 case bp_call_dummy:
53a5351d
JM
5957 /* Make sure the action is stop (silent or noisy),
5958 so infrun.c pops the dummy frame. */
aa7d318d 5959 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5960 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5961 break;
5962 case bp_std_terminate:
5963 /* Make sure the action is stop (silent or noisy),
5964 so infrun.c pops the dummy frame. */
aa7d318d 5965 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5966 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5967 break;
1042e4c0 5968 case bp_tracepoint:
7a697b8d 5969 case bp_fast_tracepoint:
0fb4aa4b 5970 case bp_static_tracepoint:
1042e4c0
SS
5971 /* Tracepoint hits should not be reported back to GDB, and
5972 if one got through somehow, it should have been filtered
5973 out already. */
5974 internal_error (__FILE__, __LINE__,
7a697b8d 5975 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5976 break;
5977 case bp_gnu_ifunc_resolver:
5978 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5979 this_action = BPSTAT_WHAT_SINGLE;
5980 break;
5981 case bp_gnu_ifunc_resolver_return:
5982 /* The breakpoint will be removed, execution will restart from the
5983 PC of the former breakpoint. */
5984 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5985 break;
e7e0cddf
SS
5986
5987 case bp_dprintf:
a11cfd87
HZ
5988 if (bs->stop)
5989 this_action = BPSTAT_WHAT_STOP_SILENT;
5990 else
5991 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5992 break;
5993
628fe4e4
JK
5994 default:
5995 internal_error (__FILE__, __LINE__,
5996 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5997 }
628fe4e4
JK
5998
5999 retval.main_action = max (retval.main_action, this_action);
c906108c 6000 }
628fe4e4 6001
243a9253
PA
6002 return retval;
6003}
628fe4e4 6004
243a9253
PA
6005void
6006bpstat_run_callbacks (bpstat bs_head)
6007{
6008 bpstat bs;
628fe4e4 6009
0e30163f
JK
6010 for (bs = bs_head; bs != NULL; bs = bs->next)
6011 {
6012 struct breakpoint *b = bs->breakpoint_at;
6013
6014 if (b == NULL)
6015 continue;
6016 switch (b->type)
6017 {
243a9253
PA
6018 case bp_jit_event:
6019 handle_jit_event ();
6020 break;
0e30163f
JK
6021 case bp_gnu_ifunc_resolver:
6022 gnu_ifunc_resolver_stop (b);
6023 break;
6024 case bp_gnu_ifunc_resolver_return:
6025 gnu_ifunc_resolver_return_stop (b);
6026 break;
6027 }
6028 }
c906108c
SS
6029}
6030
6031/* Nonzero if we should step constantly (e.g. watchpoints on machines
6032 without hardware support). This isn't related to a specific bpstat,
6033 just to things like whether watchpoints are set. */
6034
c5aa993b 6035int
fba45db2 6036bpstat_should_step (void)
c906108c
SS
6037{
6038 struct breakpoint *b;
cc59ec59 6039
c906108c 6040 ALL_BREAKPOINTS (b)
717a8278 6041 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6042 return 1;
c906108c
SS
6043 return 0;
6044}
6045
67822962
PA
6046int
6047bpstat_causes_stop (bpstat bs)
6048{
6049 for (; bs != NULL; bs = bs->next)
6050 if (bs->stop)
6051 return 1;
6052
6053 return 0;
6054}
6055
c906108c 6056\f
c5aa993b 6057
170b53b2
UW
6058/* Compute a string of spaces suitable to indent the next line
6059 so it starts at the position corresponding to the table column
6060 named COL_NAME in the currently active table of UIOUT. */
6061
6062static char *
6063wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6064{
6065 static char wrap_indent[80];
6066 int i, total_width, width, align;
6067 char *text;
6068
6069 total_width = 0;
6070 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6071 {
6072 if (strcmp (text, col_name) == 0)
6073 {
6074 gdb_assert (total_width < sizeof wrap_indent);
6075 memset (wrap_indent, ' ', total_width);
6076 wrap_indent[total_width] = 0;
6077
6078 return wrap_indent;
6079 }
6080
6081 total_width += width + 1;
6082 }
6083
6084 return NULL;
6085}
6086
b775012e
LM
6087/* Determine if the locations of this breakpoint will have their conditions
6088 evaluated by the target, host or a mix of both. Returns the following:
6089
6090 "host": Host evals condition.
6091 "host or target": Host or Target evals condition.
6092 "target": Target evals condition.
6093*/
6094
6095static const char *
6096bp_condition_evaluator (struct breakpoint *b)
6097{
6098 struct bp_location *bl;
6099 char host_evals = 0;
6100 char target_evals = 0;
6101
6102 if (!b)
6103 return NULL;
6104
6105 if (!is_breakpoint (b))
6106 return NULL;
6107
6108 if (gdb_evaluates_breakpoint_condition_p ()
6109 || !target_supports_evaluation_of_breakpoint_conditions ())
6110 return condition_evaluation_host;
6111
6112 for (bl = b->loc; bl; bl = bl->next)
6113 {
6114 if (bl->cond_bytecode)
6115 target_evals++;
6116 else
6117 host_evals++;
6118 }
6119
6120 if (host_evals && target_evals)
6121 return condition_evaluation_both;
6122 else if (target_evals)
6123 return condition_evaluation_target;
6124 else
6125 return condition_evaluation_host;
6126}
6127
6128/* Determine the breakpoint location's condition evaluator. This is
6129 similar to bp_condition_evaluator, but for locations. */
6130
6131static const char *
6132bp_location_condition_evaluator (struct bp_location *bl)
6133{
6134 if (bl && !is_breakpoint (bl->owner))
6135 return NULL;
6136
6137 if (gdb_evaluates_breakpoint_condition_p ()
6138 || !target_supports_evaluation_of_breakpoint_conditions ())
6139 return condition_evaluation_host;
6140
6141 if (bl && bl->cond_bytecode)
6142 return condition_evaluation_target;
6143 else
6144 return condition_evaluation_host;
6145}
6146
859825b8
JK
6147/* Print the LOC location out of the list of B->LOC locations. */
6148
170b53b2
UW
6149static void
6150print_breakpoint_location (struct breakpoint *b,
6151 struct bp_location *loc)
0d381245 6152{
79a45e25 6153 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6154 struct cleanup *old_chain = save_current_program_space ();
6155
859825b8
JK
6156 if (loc != NULL && loc->shlib_disabled)
6157 loc = NULL;
6158
6c95b8df
PA
6159 if (loc != NULL)
6160 set_current_program_space (loc->pspace);
6161
56435ebe 6162 if (b->display_canonical)
f00aae0f
KS
6163 ui_out_field_string (uiout, "what",
6164 event_location_to_string (b->location));
2f202fde 6165 else if (loc && loc->symtab)
0d381245
VP
6166 {
6167 struct symbol *sym
6168 = find_pc_sect_function (loc->address, loc->section);
6169 if (sym)
6170 {
6171 ui_out_text (uiout, "in ");
6172 ui_out_field_string (uiout, "func",
6173 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6174 ui_out_text (uiout, " ");
6175 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6176 ui_out_text (uiout, "at ");
0d381245 6177 }
05cba821
JK
6178 ui_out_field_string (uiout, "file",
6179 symtab_to_filename_for_display (loc->symtab));
0d381245 6180 ui_out_text (uiout, ":");
05cba821 6181
0d381245 6182 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6183 ui_out_field_string (uiout, "fullname",
6184 symtab_to_fullname (loc->symtab));
0d381245 6185
f8eba3c6 6186 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6187 }
859825b8 6188 else if (loc)
0d381245 6189 {
f99d8bf4
PA
6190 struct ui_file *stb = mem_fileopen ();
6191 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6192
f99d8bf4 6193 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6194 demangle, "");
0d381245 6195 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6196
6197 do_cleanups (stb_chain);
0d381245 6198 }
859825b8 6199 else
f00aae0f
KS
6200 {
6201 ui_out_field_string (uiout, "pending",
6202 event_location_to_string (b->location));
6203 /* If extra_string is available, it could be holding a condition
6204 or dprintf arguments. In either case, make sure it is printed,
6205 too, but only for non-MI streams. */
6206 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6207 {
6208 if (b->type == bp_dprintf)
6209 ui_out_text (uiout, ",");
6210 else
6211 ui_out_text (uiout, " ");
6212 ui_out_text (uiout, b->extra_string);
6213 }
6214 }
6c95b8df 6215
b775012e
LM
6216 if (loc && is_breakpoint (b)
6217 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6218 && bp_condition_evaluator (b) == condition_evaluation_both)
6219 {
6220 ui_out_text (uiout, " (");
6221 ui_out_field_string (uiout, "evaluated-by",
6222 bp_location_condition_evaluator (loc));
6223 ui_out_text (uiout, ")");
6224 }
6225
6c95b8df 6226 do_cleanups (old_chain);
0d381245
VP
6227}
6228
269b11a2
PA
6229static const char *
6230bptype_string (enum bptype type)
c906108c 6231{
c4093a6a
JM
6232 struct ep_type_description
6233 {
6234 enum bptype type;
6235 char *description;
6236 };
6237 static struct ep_type_description bptypes[] =
c906108c 6238 {
c5aa993b
JM
6239 {bp_none, "?deleted?"},
6240 {bp_breakpoint, "breakpoint"},
c906108c 6241 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6242 {bp_single_step, "sw single-step"},
c5aa993b
JM
6243 {bp_until, "until"},
6244 {bp_finish, "finish"},
6245 {bp_watchpoint, "watchpoint"},
c906108c 6246 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6247 {bp_read_watchpoint, "read watchpoint"},
6248 {bp_access_watchpoint, "acc watchpoint"},
6249 {bp_longjmp, "longjmp"},
6250 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6251 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6252 {bp_exception, "exception"},
6253 {bp_exception_resume, "exception resume"},
c5aa993b 6254 {bp_step_resume, "step resume"},
2c03e5be 6255 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6256 {bp_watchpoint_scope, "watchpoint scope"},
6257 {bp_call_dummy, "call dummy"},
aa7d318d 6258 {bp_std_terminate, "std::terminate"},
c5aa993b 6259 {bp_shlib_event, "shlib events"},
c4093a6a 6260 {bp_thread_event, "thread events"},
1900040c 6261 {bp_overlay_event, "overlay events"},
0fd8e87f 6262 {bp_longjmp_master, "longjmp master"},
aa7d318d 6263 {bp_std_terminate_master, "std::terminate master"},
186c406b 6264 {bp_exception_master, "exception master"},
ce78b96d 6265 {bp_catchpoint, "catchpoint"},
1042e4c0 6266 {bp_tracepoint, "tracepoint"},
7a697b8d 6267 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6268 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6269 {bp_dprintf, "dprintf"},
4efc6507 6270 {bp_jit_event, "jit events"},
0e30163f
JK
6271 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6272 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6273 };
269b11a2
PA
6274
6275 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6276 || ((int) type != bptypes[(int) type].type))
6277 internal_error (__FILE__, __LINE__,
6278 _("bptypes table does not describe type #%d."),
6279 (int) type);
6280
6281 return bptypes[(int) type].description;
6282}
6283
998580f1
MK
6284/* For MI, output a field named 'thread-groups' with a list as the value.
6285 For CLI, prefix the list with the string 'inf'. */
6286
6287static void
6288output_thread_groups (struct ui_out *uiout,
6289 const char *field_name,
6290 VEC(int) *inf_num,
6291 int mi_only)
6292{
752eb8b4 6293 struct cleanup *back_to;
998580f1
MK
6294 int is_mi = ui_out_is_mi_like_p (uiout);
6295 int inf;
6296 int i;
6297
6298 /* For backward compatibility, don't display inferiors in CLI unless
6299 there are several. Always display them for MI. */
6300 if (!is_mi && mi_only)
6301 return;
6302
752eb8b4
TT
6303 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6304
998580f1
MK
6305 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6306 {
6307 if (is_mi)
6308 {
6309 char mi_group[10];
6310
6311 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6312 ui_out_field_string (uiout, NULL, mi_group);
6313 }
6314 else
6315 {
6316 if (i == 0)
6317 ui_out_text (uiout, " inf ");
6318 else
6319 ui_out_text (uiout, ", ");
6320
6321 ui_out_text (uiout, plongest (inf));
6322 }
6323 }
6324
6325 do_cleanups (back_to);
6326}
6327
269b11a2
PA
6328/* Print B to gdb_stdout. */
6329
6330static void
6331print_one_breakpoint_location (struct breakpoint *b,
6332 struct bp_location *loc,
6333 int loc_number,
6334 struct bp_location **last_loc,
269b11a2
PA
6335 int allflag)
6336{
6337 struct command_line *l;
c2c6d25f 6338 static char bpenables[] = "nynny";
c906108c 6339
79a45e25 6340 struct ui_out *uiout = current_uiout;
0d381245
VP
6341 int header_of_multiple = 0;
6342 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6343 struct value_print_options opts;
6344
6345 get_user_print_options (&opts);
0d381245
VP
6346
6347 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6348 /* See comment in print_one_breakpoint concerning treatment of
6349 breakpoints with single disabled location. */
0d381245
VP
6350 if (loc == NULL
6351 && (b->loc != NULL
6352 && (b->loc->next != NULL || !b->loc->enabled)))
6353 header_of_multiple = 1;
6354 if (loc == NULL)
6355 loc = b->loc;
6356
c4093a6a
JM
6357 annotate_record ();
6358
6359 /* 1 */
6360 annotate_field (0);
0d381245
VP
6361 if (part_of_multiple)
6362 {
6363 char *formatted;
0c6773c1 6364 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6365 ui_out_field_string (uiout, "number", formatted);
6366 xfree (formatted);
6367 }
6368 else
6369 {
6370 ui_out_field_int (uiout, "number", b->number);
6371 }
c4093a6a
JM
6372
6373 /* 2 */
6374 annotate_field (1);
0d381245
VP
6375 if (part_of_multiple)
6376 ui_out_field_skip (uiout, "type");
269b11a2
PA
6377 else
6378 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6379
6380 /* 3 */
6381 annotate_field (2);
0d381245
VP
6382 if (part_of_multiple)
6383 ui_out_field_skip (uiout, "disp");
6384 else
2cec12e5 6385 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6386
c4093a6a
JM
6387
6388 /* 4 */
6389 annotate_field (3);
0d381245 6390 if (part_of_multiple)
54e52265 6391 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6392 else
4a64f543
MS
6393 ui_out_field_fmt (uiout, "enabled", "%c",
6394 bpenables[(int) b->enable_state]);
54e52265 6395 ui_out_spaces (uiout, 2);
0d381245 6396
c4093a6a
JM
6397
6398 /* 5 and 6 */
3086aeae 6399 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6400 {
4a64f543
MS
6401 /* Although the print_one can possibly print all locations,
6402 calling it here is not likely to get any nice result. So,
6403 make sure there's just one location. */
0d381245 6404 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6405 b->ops->print_one (b, last_loc);
0d381245 6406 }
3086aeae
DJ
6407 else
6408 switch (b->type)
6409 {
6410 case bp_none:
6411 internal_error (__FILE__, __LINE__,
e2e0b3e5 6412 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6413 break;
c906108c 6414
3086aeae
DJ
6415 case bp_watchpoint:
6416 case bp_hardware_watchpoint:
6417 case bp_read_watchpoint:
6418 case bp_access_watchpoint:
3a5c3e22
PA
6419 {
6420 struct watchpoint *w = (struct watchpoint *) b;
6421
6422 /* Field 4, the address, is omitted (which makes the columns
6423 not line up too nicely with the headers, but the effect
6424 is relatively readable). */
6425 if (opts.addressprint)
6426 ui_out_field_skip (uiout, "addr");
6427 annotate_field (5);
6428 ui_out_field_string (uiout, "what", w->exp_string);
6429 }
3086aeae
DJ
6430 break;
6431
3086aeae
DJ
6432 case bp_breakpoint:
6433 case bp_hardware_breakpoint:
7c16b83e 6434 case bp_single_step:
3086aeae
DJ
6435 case bp_until:
6436 case bp_finish:
6437 case bp_longjmp:
6438 case bp_longjmp_resume:
e2e4d78b 6439 case bp_longjmp_call_dummy:
186c406b
TT
6440 case bp_exception:
6441 case bp_exception_resume:
3086aeae 6442 case bp_step_resume:
2c03e5be 6443 case bp_hp_step_resume:
3086aeae
DJ
6444 case bp_watchpoint_scope:
6445 case bp_call_dummy:
aa7d318d 6446 case bp_std_terminate:
3086aeae
DJ
6447 case bp_shlib_event:
6448 case bp_thread_event:
6449 case bp_overlay_event:
0fd8e87f 6450 case bp_longjmp_master:
aa7d318d 6451 case bp_std_terminate_master:
186c406b 6452 case bp_exception_master:
1042e4c0 6453 case bp_tracepoint:
7a697b8d 6454 case bp_fast_tracepoint:
0fb4aa4b 6455 case bp_static_tracepoint:
e7e0cddf 6456 case bp_dprintf:
4efc6507 6457 case bp_jit_event:
0e30163f
JK
6458 case bp_gnu_ifunc_resolver:
6459 case bp_gnu_ifunc_resolver_return:
79a45b7d 6460 if (opts.addressprint)
3086aeae
DJ
6461 {
6462 annotate_field (4);
54e52265 6463 if (header_of_multiple)
0d381245 6464 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6465 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6466 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6467 else
5af949e3
UW
6468 ui_out_field_core_addr (uiout, "addr",
6469 loc->gdbarch, loc->address);
3086aeae
DJ
6470 }
6471 annotate_field (5);
0d381245 6472 if (!header_of_multiple)
170b53b2 6473 print_breakpoint_location (b, loc);
0d381245 6474 if (b->loc)
a6d9a66e 6475 *last_loc = b->loc;
3086aeae
DJ
6476 break;
6477 }
c906108c 6478
6c95b8df 6479
998580f1 6480 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6481 {
6482 struct inferior *inf;
998580f1
MK
6483 VEC(int) *inf_num = NULL;
6484 int mi_only = 1;
6c95b8df 6485
998580f1 6486 ALL_INFERIORS (inf)
6c95b8df
PA
6487 {
6488 if (inf->pspace == loc->pspace)
998580f1 6489 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6490 }
998580f1
MK
6491
6492 /* For backward compatibility, don't display inferiors in CLI unless
6493 there are several. Always display for MI. */
6494 if (allflag
6495 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6496 && (number_of_program_spaces () > 1
6497 || number_of_inferiors () > 1)
6498 /* LOC is for existing B, it cannot be in
6499 moribund_locations and thus having NULL OWNER. */
6500 && loc->owner->type != bp_catchpoint))
6501 mi_only = 0;
6502 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6503 VEC_free (int, inf_num);
6c95b8df
PA
6504 }
6505
4a306c9a 6506 if (!part_of_multiple)
c4093a6a 6507 {
4a306c9a
JB
6508 if (b->thread != -1)
6509 {
6510 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6511 "stop only in" line a little further down. */
4a306c9a
JB
6512 ui_out_text (uiout, " thread ");
6513 ui_out_field_int (uiout, "thread", b->thread);
6514 }
6515 else if (b->task != 0)
6516 {
6517 ui_out_text (uiout, " task ");
6518 ui_out_field_int (uiout, "task", b->task);
6519 }
c4093a6a 6520 }
f1310107 6521
8b93c638 6522 ui_out_text (uiout, "\n");
f1310107 6523
348d480f 6524 if (!part_of_multiple)
f1310107
TJB
6525 b->ops->print_one_detail (b, uiout);
6526
0d381245 6527 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6528 {
6529 annotate_field (6);
8b93c638 6530 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6531 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6532 the frame ID. */
5af949e3
UW
6533 ui_out_field_core_addr (uiout, "frame",
6534 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6535 ui_out_text (uiout, "\n");
c4093a6a
JM
6536 }
6537
28010a5d 6538 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6539 {
6540 annotate_field (7);
d77f58be 6541 if (is_tracepoint (b))
1042e4c0
SS
6542 ui_out_text (uiout, "\ttrace only if ");
6543 else
6544 ui_out_text (uiout, "\tstop only if ");
0101ce28 6545 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6546
6547 /* Print whether the target is doing the breakpoint's condition
6548 evaluation. If GDB is doing the evaluation, don't print anything. */
6549 if (is_breakpoint (b)
6550 && breakpoint_condition_evaluation_mode ()
6551 == condition_evaluation_target)
6552 {
6553 ui_out_text (uiout, " (");
6554 ui_out_field_string (uiout, "evaluated-by",
6555 bp_condition_evaluator (b));
6556 ui_out_text (uiout, " evals)");
6557 }
0101ce28
JJ
6558 ui_out_text (uiout, "\n");
6559 }
6560
0d381245 6561 if (!part_of_multiple && b->thread != -1)
c4093a6a 6562 {
4a64f543 6563 /* FIXME should make an annotation for this. */
8b93c638 6564 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6565 if (ui_out_is_mi_like_p (uiout))
6566 ui_out_field_int (uiout, "thread", b->thread);
6567 else
6568 {
6569 struct thread_info *thr = find_thread_global_id (b->thread);
6570
6571 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6572 }
8b93c638 6573 ui_out_text (uiout, "\n");
c4093a6a
JM
6574 }
6575
556ec64d
YQ
6576 if (!part_of_multiple)
6577 {
6578 if (b->hit_count)
31f56a27
YQ
6579 {
6580 /* FIXME should make an annotation for this. */
6581 if (is_catchpoint (b))
6582 ui_out_text (uiout, "\tcatchpoint");
6583 else if (is_tracepoint (b))
6584 ui_out_text (uiout, "\ttracepoint");
6585 else
6586 ui_out_text (uiout, "\tbreakpoint");
6587 ui_out_text (uiout, " already hit ");
6588 ui_out_field_int (uiout, "times", b->hit_count);
6589 if (b->hit_count == 1)
6590 ui_out_text (uiout, " time\n");
6591 else
6592 ui_out_text (uiout, " times\n");
6593 }
556ec64d
YQ
6594 else
6595 {
31f56a27
YQ
6596 /* Output the count also if it is zero, but only if this is mi. */
6597 if (ui_out_is_mi_like_p (uiout))
6598 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6599 }
6600 }
8b93c638 6601
0d381245 6602 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6603 {
6604 annotate_field (8);
8b93c638
JM
6605 ui_out_text (uiout, "\tignore next ");
6606 ui_out_field_int (uiout, "ignore", b->ignore_count);
6607 ui_out_text (uiout, " hits\n");
c4093a6a 6608 }
059fb39f 6609
816338b5
SS
6610 /* Note that an enable count of 1 corresponds to "enable once"
6611 behavior, which is reported by the combination of enablement and
6612 disposition, so we don't need to mention it here. */
6613 if (!part_of_multiple && b->enable_count > 1)
6614 {
6615 annotate_field (8);
6616 ui_out_text (uiout, "\tdisable after ");
6617 /* Tweak the wording to clarify that ignore and enable counts
6618 are distinct, and have additive effect. */
6619 if (b->ignore_count)
6620 ui_out_text (uiout, "additional ");
6621 else
6622 ui_out_text (uiout, "next ");
6623 ui_out_field_int (uiout, "enable", b->enable_count);
6624 ui_out_text (uiout, " hits\n");
6625 }
6626
f196051f
SS
6627 if (!part_of_multiple && is_tracepoint (b))
6628 {
6629 struct tracepoint *tp = (struct tracepoint *) b;
6630
6631 if (tp->traceframe_usage)
6632 {
6633 ui_out_text (uiout, "\ttrace buffer usage ");
6634 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6635 ui_out_text (uiout, " bytes\n");
6636 }
6637 }
d3ce09f5 6638
9add0f1b 6639 l = b->commands ? b->commands->commands : NULL;
059fb39f 6640 if (!part_of_multiple && l)
c4093a6a 6641 {
3b31d625
EZ
6642 struct cleanup *script_chain;
6643
c4093a6a 6644 annotate_field (9);
3b31d625 6645 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6646 print_command_lines (uiout, l, 4);
3b31d625 6647 do_cleanups (script_chain);
c4093a6a 6648 }
d24317b4 6649
d9b3f62e 6650 if (is_tracepoint (b))
1042e4c0 6651 {
d9b3f62e
PA
6652 struct tracepoint *t = (struct tracepoint *) b;
6653
6654 if (!part_of_multiple && t->pass_count)
6655 {
6656 annotate_field (10);
6657 ui_out_text (uiout, "\tpass count ");
6658 ui_out_field_int (uiout, "pass", t->pass_count);
6659 ui_out_text (uiout, " \n");
6660 }
f2a8bc8a
YQ
6661
6662 /* Don't display it when tracepoint or tracepoint location is
6663 pending. */
6664 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6665 {
6666 annotate_field (11);
6667
6668 if (ui_out_is_mi_like_p (uiout))
6669 ui_out_field_string (uiout, "installed",
6670 loc->inserted ? "y" : "n");
6671 else
6672 {
6673 if (loc->inserted)
6674 ui_out_text (uiout, "\t");
6675 else
6676 ui_out_text (uiout, "\tnot ");
6677 ui_out_text (uiout, "installed on target\n");
6678 }
6679 }
1042e4c0
SS
6680 }
6681
d24317b4
VP
6682 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6683 {
3a5c3e22
PA
6684 if (is_watchpoint (b))
6685 {
6686 struct watchpoint *w = (struct watchpoint *) b;
6687
6688 ui_out_field_string (uiout, "original-location", w->exp_string);
6689 }
f00aae0f
KS
6690 else if (b->location != NULL
6691 && event_location_to_string (b->location) != NULL)
6692 ui_out_field_string (uiout, "original-location",
6693 event_location_to_string (b->location));
d24317b4 6694 }
c4093a6a 6695}
c5aa993b 6696
0d381245
VP
6697static void
6698print_one_breakpoint (struct breakpoint *b,
4a64f543 6699 struct bp_location **last_loc,
6c95b8df 6700 int allflag)
0d381245 6701{
8d3788bd 6702 struct cleanup *bkpt_chain;
79a45e25 6703 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6704
6705 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6706
12c5a436 6707 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6708 do_cleanups (bkpt_chain);
0d381245
VP
6709
6710 /* If this breakpoint has custom print function,
6711 it's already printed. Otherwise, print individual
6712 locations, if any. */
6713 if (b->ops == NULL || b->ops->print_one == NULL)
6714 {
4a64f543
MS
6715 /* If breakpoint has a single location that is disabled, we
6716 print it as if it had several locations, since otherwise it's
6717 hard to represent "breakpoint enabled, location disabled"
6718 situation.
6719
6720 Note that while hardware watchpoints have several locations
a3be7890 6721 internally, that's not a property exposed to user. */
0d381245 6722 if (b->loc
a5606eee 6723 && !is_hardware_watchpoint (b)
8d3788bd 6724 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6725 {
6726 struct bp_location *loc;
6727 int n = 1;
8d3788bd 6728
0d381245 6729 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6730 {
6731 struct cleanup *inner2 =
6732 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6733 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6734 do_cleanups (inner2);
6735 }
0d381245
VP
6736 }
6737 }
6738}
6739
a6d9a66e
UW
6740static int
6741breakpoint_address_bits (struct breakpoint *b)
6742{
6743 int print_address_bits = 0;
6744 struct bp_location *loc;
6745
c6d81124
PA
6746 /* Software watchpoints that aren't watching memory don't have an
6747 address to print. */
6748 if (is_no_memory_software_watchpoint (b))
6749 return 0;
6750
a6d9a66e
UW
6751 for (loc = b->loc; loc; loc = loc->next)
6752 {
c7437ca6
PA
6753 int addr_bit;
6754
c7437ca6 6755 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6756 if (addr_bit > print_address_bits)
6757 print_address_bits = addr_bit;
6758 }
6759
6760 return print_address_bits;
6761}
0d381245 6762
c4093a6a
JM
6763struct captured_breakpoint_query_args
6764 {
6765 int bnum;
6766 };
c5aa993b 6767
c4093a6a 6768static int
2b65245e 6769do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6770{
9a3c8263
SM
6771 struct captured_breakpoint_query_args *args
6772 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6773 struct breakpoint *b;
a6d9a66e 6774 struct bp_location *dummy_loc = NULL;
cc59ec59 6775
c4093a6a
JM
6776 ALL_BREAKPOINTS (b)
6777 {
6778 if (args->bnum == b->number)
c5aa993b 6779 {
12c5a436 6780 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6781 return GDB_RC_OK;
c5aa993b 6782 }
c4093a6a
JM
6783 }
6784 return GDB_RC_NONE;
6785}
c5aa993b 6786
c4093a6a 6787enum gdb_rc
4a64f543
MS
6788gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6789 char **error_message)
c4093a6a
JM
6790{
6791 struct captured_breakpoint_query_args args;
cc59ec59 6792
c4093a6a
JM
6793 args.bnum = bnum;
6794 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6795 an error. */
b0b13bb4
DJ
6796 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6797 error_message, RETURN_MASK_ALL) < 0)
6798 return GDB_RC_FAIL;
6799 else
6800 return GDB_RC_OK;
c4093a6a 6801}
c5aa993b 6802
09d682a4
TT
6803/* Return true if this breakpoint was set by the user, false if it is
6804 internal or momentary. */
6805
6806int
6807user_breakpoint_p (struct breakpoint *b)
6808{
46c6471b 6809 return b->number > 0;
09d682a4
TT
6810}
6811
93daf339
TT
6812/* See breakpoint.h. */
6813
6814int
6815pending_breakpoint_p (struct breakpoint *b)
6816{
6817 return b->loc == NULL;
6818}
6819
7f3b0473 6820/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6821 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6822 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6823 FILTER is non-NULL, call it on each breakpoint and only include the
6824 ones for which it returns non-zero. Return the total number of
6825 breakpoints listed. */
c906108c 6826
d77f58be 6827static int
e5a67952 6828breakpoint_1 (char *args, int allflag,
4a64f543 6829 int (*filter) (const struct breakpoint *))
c4093a6a 6830{
52f0bd74 6831 struct breakpoint *b;
a6d9a66e 6832 struct bp_location *last_loc = NULL;
7f3b0473 6833 int nr_printable_breakpoints;
3b31d625 6834 struct cleanup *bkpttbl_chain;
79a45b7d 6835 struct value_print_options opts;
a6d9a66e 6836 int print_address_bits = 0;
269b11a2 6837 int print_type_col_width = 14;
79a45e25 6838 struct ui_out *uiout = current_uiout;
269b11a2 6839
79a45b7d
TT
6840 get_user_print_options (&opts);
6841
4a64f543
MS
6842 /* Compute the number of rows in the table, as well as the size
6843 required for address fields. */
7f3b0473
AC
6844 nr_printable_breakpoints = 0;
6845 ALL_BREAKPOINTS (b)
e5a67952
MS
6846 {
6847 /* If we have a filter, only list the breakpoints it accepts. */
6848 if (filter && !filter (b))
6849 continue;
6850
6851 /* If we have an "args" string, it is a list of breakpoints to
6852 accept. Skip the others. */
6853 if (args != NULL && *args != '\0')
6854 {
6855 if (allflag && parse_and_eval_long (args) != b->number)
6856 continue;
6857 if (!allflag && !number_is_in_list (args, b->number))
6858 continue;
6859 }
269b11a2 6860
e5a67952
MS
6861 if (allflag || user_breakpoint_p (b))
6862 {
6863 int addr_bit, type_len;
a6d9a66e 6864
e5a67952
MS
6865 addr_bit = breakpoint_address_bits (b);
6866 if (addr_bit > print_address_bits)
6867 print_address_bits = addr_bit;
269b11a2 6868
e5a67952
MS
6869 type_len = strlen (bptype_string (b->type));
6870 if (type_len > print_type_col_width)
6871 print_type_col_width = type_len;
6872
6873 nr_printable_breakpoints++;
6874 }
6875 }
7f3b0473 6876
79a45b7d 6877 if (opts.addressprint)
3b31d625 6878 bkpttbl_chain
3e43a32a
MS
6879 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6880 nr_printable_breakpoints,
3b31d625 6881 "BreakpointTable");
8b93c638 6882 else
3b31d625 6883 bkpttbl_chain
3e43a32a
MS
6884 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6885 nr_printable_breakpoints,
3b31d625 6886 "BreakpointTable");
8b93c638 6887
7f3b0473 6888 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6889 annotate_breakpoints_headers ();
6890 if (nr_printable_breakpoints > 0)
6891 annotate_field (0);
4a64f543 6892 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6893 if (nr_printable_breakpoints > 0)
6894 annotate_field (1);
269b11a2 6895 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6896 "type", "Type"); /* 2 */
d7faa9e7
AC
6897 if (nr_printable_breakpoints > 0)
6898 annotate_field (2);
4a64f543 6899 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6900 if (nr_printable_breakpoints > 0)
6901 annotate_field (3);
54e52265 6902 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6903 if (opts.addressprint)
e5a67952
MS
6904 {
6905 if (nr_printable_breakpoints > 0)
6906 annotate_field (4);
6907 if (print_address_bits <= 32)
6908 ui_out_table_header (uiout, 10, ui_left,
6909 "addr", "Address"); /* 5 */
6910 else
6911 ui_out_table_header (uiout, 18, ui_left,
6912 "addr", "Address"); /* 5 */
6913 }
d7faa9e7
AC
6914 if (nr_printable_breakpoints > 0)
6915 annotate_field (5);
6916 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6917 ui_out_table_body (uiout);
6918 if (nr_printable_breakpoints > 0)
6919 annotate_breakpoints_table ();
7f3b0473 6920
c4093a6a 6921 ALL_BREAKPOINTS (b)
e5a67952
MS
6922 {
6923 QUIT;
6924 /* If we have a filter, only list the breakpoints it accepts. */
6925 if (filter && !filter (b))
6926 continue;
6927
6928 /* If we have an "args" string, it is a list of breakpoints to
6929 accept. Skip the others. */
6930
6931 if (args != NULL && *args != '\0')
6932 {
6933 if (allflag) /* maintenance info breakpoint */
6934 {
6935 if (parse_and_eval_long (args) != b->number)
6936 continue;
6937 }
6938 else /* all others */
6939 {
6940 if (!number_is_in_list (args, b->number))
6941 continue;
6942 }
6943 }
6944 /* We only print out user settable breakpoints unless the
6945 allflag is set. */
6946 if (allflag || user_breakpoint_p (b))
12c5a436 6947 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6948 }
6949
3b31d625 6950 do_cleanups (bkpttbl_chain);
698384cd 6951
7f3b0473 6952 if (nr_printable_breakpoints == 0)
c906108c 6953 {
4a64f543
MS
6954 /* If there's a filter, let the caller decide how to report
6955 empty list. */
d77f58be
SS
6956 if (!filter)
6957 {
e5a67952 6958 if (args == NULL || *args == '\0')
d77f58be
SS
6959 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6960 else
4a64f543 6961 ui_out_message (uiout, 0,
e5a67952
MS
6962 "No breakpoint or watchpoint matching '%s'.\n",
6963 args);
d77f58be 6964 }
c906108c
SS
6965 }
6966 else
c4093a6a 6967 {
a6d9a66e
UW
6968 if (last_loc && !server_command)
6969 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6970 }
c906108c 6971
4a64f543 6972 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6973 there have been breakpoints? */
c906108c 6974 annotate_breakpoints_table_end ();
d77f58be
SS
6975
6976 return nr_printable_breakpoints;
c906108c
SS
6977}
6978
ad443146
SS
6979/* Display the value of default-collect in a way that is generally
6980 compatible with the breakpoint list. */
6981
6982static void
6983default_collect_info (void)
6984{
79a45e25
PA
6985 struct ui_out *uiout = current_uiout;
6986
ad443146
SS
6987 /* If it has no value (which is frequently the case), say nothing; a
6988 message like "No default-collect." gets in user's face when it's
6989 not wanted. */
6990 if (!*default_collect)
6991 return;
6992
6993 /* The following phrase lines up nicely with per-tracepoint collect
6994 actions. */
6995 ui_out_text (uiout, "default collect ");
6996 ui_out_field_string (uiout, "default-collect", default_collect);
6997 ui_out_text (uiout, " \n");
6998}
6999
c906108c 7000static void
e5a67952 7001breakpoints_info (char *args, int from_tty)
c906108c 7002{
e5a67952 7003 breakpoint_1 (args, 0, NULL);
ad443146
SS
7004
7005 default_collect_info ();
d77f58be
SS
7006}
7007
7008static void
e5a67952 7009watchpoints_info (char *args, int from_tty)
d77f58be 7010{
e5a67952 7011 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 7012 struct ui_out *uiout = current_uiout;
d77f58be
SS
7013
7014 if (num_printed == 0)
7015 {
e5a67952 7016 if (args == NULL || *args == '\0')
d77f58be
SS
7017 ui_out_message (uiout, 0, "No watchpoints.\n");
7018 else
e5a67952 7019 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 7020 }
c906108c
SS
7021}
7022
7a292a7a 7023static void
e5a67952 7024maintenance_info_breakpoints (char *args, int from_tty)
c906108c 7025{
e5a67952 7026 breakpoint_1 (args, 1, NULL);
ad443146
SS
7027
7028 default_collect_info ();
c906108c
SS
7029}
7030
0d381245 7031static int
714835d5 7032breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7033 struct program_space *pspace,
714835d5 7034 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7035{
7036 struct bp_location *bl = b->loc;
cc59ec59 7037
0d381245
VP
7038 for (; bl; bl = bl->next)
7039 {
6c95b8df
PA
7040 if (bl->pspace == pspace
7041 && bl->address == pc
0d381245
VP
7042 && (!overlay_debugging || bl->section == section))
7043 return 1;
7044 }
7045 return 0;
7046}
7047
672f9b60 7048/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7049 concerns with logical breakpoints, so we match program spaces, not
7050 address spaces. */
c906108c
SS
7051
7052static void
6c95b8df
PA
7053describe_other_breakpoints (struct gdbarch *gdbarch,
7054 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7055 struct obj_section *section, int thread)
c906108c 7056{
52f0bd74
AC
7057 int others = 0;
7058 struct breakpoint *b;
c906108c
SS
7059
7060 ALL_BREAKPOINTS (b)
672f9b60
KP
7061 others += (user_breakpoint_p (b)
7062 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7063 if (others > 0)
7064 {
a3f17187
AC
7065 if (others == 1)
7066 printf_filtered (_("Note: breakpoint "));
7067 else /* if (others == ???) */
7068 printf_filtered (_("Note: breakpoints "));
c906108c 7069 ALL_BREAKPOINTS (b)
672f9b60 7070 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7071 {
7072 others--;
7073 printf_filtered ("%d", b->number);
7074 if (b->thread == -1 && thread != -1)
7075 printf_filtered (" (all threads)");
7076 else if (b->thread != -1)
7077 printf_filtered (" (thread %d)", b->thread);
7078 printf_filtered ("%s%s ",
059fb39f 7079 ((b->enable_state == bp_disabled
f8eba3c6 7080 || b->enable_state == bp_call_disabled)
0d381245 7081 ? " (disabled)"
0d381245
VP
7082 : ""),
7083 (others > 1) ? ","
7084 : ((others == 1) ? " and" : ""));
7085 }
a3f17187 7086 printf_filtered (_("also set at pc "));
5af949e3 7087 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7088 printf_filtered (".\n");
7089 }
7090}
7091\f
c906108c 7092
e4f237da 7093/* Return true iff it is meaningful to use the address member of
244558af
LM
7094 BPT locations. For some breakpoint types, the locations' address members
7095 are irrelevant and it makes no sense to attempt to compare them to other
7096 addresses (or use them for any other purpose either).
e4f237da 7097
4a64f543 7098 More specifically, each of the following breakpoint types will
244558af 7099 always have a zero valued location address and we don't want to mark
4a64f543 7100 breakpoints of any of these types to be a duplicate of an actual
244558af 7101 breakpoint location at address zero:
e4f237da
KB
7102
7103 bp_watchpoint
2d134ed3
PA
7104 bp_catchpoint
7105
7106*/
e4f237da
KB
7107
7108static int
7109breakpoint_address_is_meaningful (struct breakpoint *bpt)
7110{
7111 enum bptype type = bpt->type;
7112
2d134ed3
PA
7113 return (type != bp_watchpoint && type != bp_catchpoint);
7114}
7115
7116/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7117 true if LOC1 and LOC2 represent the same watchpoint location. */
7118
7119static int
4a64f543
MS
7120watchpoint_locations_match (struct bp_location *loc1,
7121 struct bp_location *loc2)
2d134ed3 7122{
3a5c3e22
PA
7123 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7124 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7125
7126 /* Both of them must exist. */
7127 gdb_assert (w1 != NULL);
7128 gdb_assert (w2 != NULL);
2bdf28a0 7129
4a64f543
MS
7130 /* If the target can evaluate the condition expression in hardware,
7131 then we we need to insert both watchpoints even if they are at
7132 the same place. Otherwise the watchpoint will only trigger when
7133 the condition of whichever watchpoint was inserted evaluates to
7134 true, not giving a chance for GDB to check the condition of the
7135 other watchpoint. */
3a5c3e22 7136 if ((w1->cond_exp
4a64f543
MS
7137 && target_can_accel_watchpoint_condition (loc1->address,
7138 loc1->length,
0cf6dd15 7139 loc1->watchpoint_type,
3a5c3e22
PA
7140 w1->cond_exp))
7141 || (w2->cond_exp
4a64f543
MS
7142 && target_can_accel_watchpoint_condition (loc2->address,
7143 loc2->length,
0cf6dd15 7144 loc2->watchpoint_type,
3a5c3e22 7145 w2->cond_exp)))
0cf6dd15
TJB
7146 return 0;
7147
85d721b8
PA
7148 /* Note that this checks the owner's type, not the location's. In
7149 case the target does not support read watchpoints, but does
7150 support access watchpoints, we'll have bp_read_watchpoint
7151 watchpoints with hw_access locations. Those should be considered
7152 duplicates of hw_read locations. The hw_read locations will
7153 become hw_access locations later. */
2d134ed3
PA
7154 return (loc1->owner->type == loc2->owner->type
7155 && loc1->pspace->aspace == loc2->pspace->aspace
7156 && loc1->address == loc2->address
7157 && loc1->length == loc2->length);
e4f237da
KB
7158}
7159
31e77af2 7160/* See breakpoint.h. */
6c95b8df 7161
31e77af2 7162int
6c95b8df
PA
7163breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7164 struct address_space *aspace2, CORE_ADDR addr2)
7165{
f5656ead 7166 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7167 || aspace1 == aspace2)
7168 && addr1 == addr2);
7169}
7170
f1310107
TJB
7171/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7172 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7173 matches ASPACE2. On targets that have global breakpoints, the address
7174 space doesn't really matter. */
7175
7176static int
7177breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7178 int len1, struct address_space *aspace2,
7179 CORE_ADDR addr2)
7180{
f5656ead 7181 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7182 || aspace1 == aspace2)
7183 && addr2 >= addr1 && addr2 < addr1 + len1);
7184}
7185
7186/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7187 a ranged breakpoint. In most targets, a match happens only if ASPACE
7188 matches the breakpoint's address space. On targets that have global
7189 breakpoints, the address space doesn't really matter. */
7190
7191static int
7192breakpoint_location_address_match (struct bp_location *bl,
7193 struct address_space *aspace,
7194 CORE_ADDR addr)
7195{
7196 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7197 aspace, addr)
7198 || (bl->length
7199 && breakpoint_address_match_range (bl->pspace->aspace,
7200 bl->address, bl->length,
7201 aspace, addr)));
7202}
7203
d35ae833
PA
7204/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7205 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7206 match happens only if ASPACE matches the breakpoint's address
7207 space. On targets that have global breakpoints, the address space
7208 doesn't really matter. */
7209
7210static int
7211breakpoint_location_address_range_overlap (struct bp_location *bl,
7212 struct address_space *aspace,
7213 CORE_ADDR addr, int len)
7214{
7215 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7216 || bl->pspace->aspace == aspace)
7217 {
7218 int bl_len = bl->length != 0 ? bl->length : 1;
7219
7220 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7221 return 1;
7222 }
7223 return 0;
7224}
7225
1e4d1764
YQ
7226/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7227 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7228 true, otherwise returns false. */
7229
7230static int
7231tracepoint_locations_match (struct bp_location *loc1,
7232 struct bp_location *loc2)
7233{
7234 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7235 /* Since tracepoint locations are never duplicated with others', tracepoint
7236 locations at the same address of different tracepoints are regarded as
7237 different locations. */
7238 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7239 else
7240 return 0;
7241}
7242
2d134ed3
PA
7243/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7244 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7245 represent the same location. */
7246
7247static int
4a64f543
MS
7248breakpoint_locations_match (struct bp_location *loc1,
7249 struct bp_location *loc2)
2d134ed3 7250{
2bdf28a0
JK
7251 int hw_point1, hw_point2;
7252
7253 /* Both of them must not be in moribund_locations. */
7254 gdb_assert (loc1->owner != NULL);
7255 gdb_assert (loc2->owner != NULL);
7256
7257 hw_point1 = is_hardware_watchpoint (loc1->owner);
7258 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7259
7260 if (hw_point1 != hw_point2)
7261 return 0;
7262 else if (hw_point1)
7263 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7264 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7265 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7266 else
f1310107
TJB
7267 /* We compare bp_location.length in order to cover ranged breakpoints. */
7268 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7269 loc2->pspace->aspace, loc2->address)
7270 && loc1->length == loc2->length);
2d134ed3
PA
7271}
7272
76897487
KB
7273static void
7274breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7275 int bnum, int have_bnum)
7276{
f63fbe86
MS
7277 /* The longest string possibly returned by hex_string_custom
7278 is 50 chars. These must be at least that big for safety. */
7279 char astr1[64];
7280 char astr2[64];
76897487 7281
bb599908
PH
7282 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7283 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7284 if (have_bnum)
8a3fe4f8 7285 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7286 bnum, astr1, astr2);
7287 else
8a3fe4f8 7288 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7289}
7290
4a64f543
MS
7291/* Adjust a breakpoint's address to account for architectural
7292 constraints on breakpoint placement. Return the adjusted address.
7293 Note: Very few targets require this kind of adjustment. For most
7294 targets, this function is simply the identity function. */
76897487
KB
7295
7296static CORE_ADDR
a6d9a66e
UW
7297adjust_breakpoint_address (struct gdbarch *gdbarch,
7298 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7299{
a6d9a66e 7300 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7301 {
7302 /* Very few targets need any kind of breakpoint adjustment. */
7303 return bpaddr;
7304 }
88f7da05
KB
7305 else if (bptype == bp_watchpoint
7306 || bptype == bp_hardware_watchpoint
7307 || bptype == bp_read_watchpoint
7308 || bptype == bp_access_watchpoint
fe798b75 7309 || bptype == bp_catchpoint)
88f7da05
KB
7310 {
7311 /* Watchpoints and the various bp_catch_* eventpoints should not
7312 have their addresses modified. */
7313 return bpaddr;
7314 }
7c16b83e
PA
7315 else if (bptype == bp_single_step)
7316 {
7317 /* Single-step breakpoints should not have their addresses
7318 modified. If there's any architectural constrain that
7319 applies to this address, then it should have already been
7320 taken into account when the breakpoint was created in the
7321 first place. If we didn't do this, stepping through e.g.,
7322 Thumb-2 IT blocks would break. */
7323 return bpaddr;
7324 }
76897487
KB
7325 else
7326 {
7327 CORE_ADDR adjusted_bpaddr;
7328
7329 /* Some targets have architectural constraints on the placement
7330 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7331 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7332
7333 /* An adjusted breakpoint address can significantly alter
7334 a user's expectations. Print a warning if an adjustment
7335 is required. */
7336 if (adjusted_bpaddr != bpaddr)
7337 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7338
7339 return adjusted_bpaddr;
7340 }
7341}
7342
28010a5d
PA
7343void
7344init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7345 struct breakpoint *owner)
7cc221ef 7346{
7cc221ef
DJ
7347 memset (loc, 0, sizeof (*loc));
7348
348d480f
PA
7349 gdb_assert (ops != NULL);
7350
28010a5d
PA
7351 loc->ops = ops;
7352 loc->owner = owner;
511a6cd4 7353 loc->cond = NULL;
b775012e 7354 loc->cond_bytecode = NULL;
0d381245
VP
7355 loc->shlib_disabled = 0;
7356 loc->enabled = 1;
e049a4b5 7357
28010a5d 7358 switch (owner->type)
e049a4b5
DJ
7359 {
7360 case bp_breakpoint:
7c16b83e 7361 case bp_single_step:
e049a4b5
DJ
7362 case bp_until:
7363 case bp_finish:
7364 case bp_longjmp:
7365 case bp_longjmp_resume:
e2e4d78b 7366 case bp_longjmp_call_dummy:
186c406b
TT
7367 case bp_exception:
7368 case bp_exception_resume:
e049a4b5 7369 case bp_step_resume:
2c03e5be 7370 case bp_hp_step_resume:
e049a4b5
DJ
7371 case bp_watchpoint_scope:
7372 case bp_call_dummy:
aa7d318d 7373 case bp_std_terminate:
e049a4b5
DJ
7374 case bp_shlib_event:
7375 case bp_thread_event:
7376 case bp_overlay_event:
4efc6507 7377 case bp_jit_event:
0fd8e87f 7378 case bp_longjmp_master:
aa7d318d 7379 case bp_std_terminate_master:
186c406b 7380 case bp_exception_master:
0e30163f
JK
7381 case bp_gnu_ifunc_resolver:
7382 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7383 case bp_dprintf:
e049a4b5 7384 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7385 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7386 break;
7387 case bp_hardware_breakpoint:
7388 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7389 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7390 break;
7391 case bp_hardware_watchpoint:
7392 case bp_read_watchpoint:
7393 case bp_access_watchpoint:
7394 loc->loc_type = bp_loc_hardware_watchpoint;
7395 break;
7396 case bp_watchpoint:
ce78b96d 7397 case bp_catchpoint:
15c3d785
PA
7398 case bp_tracepoint:
7399 case bp_fast_tracepoint:
0fb4aa4b 7400 case bp_static_tracepoint:
e049a4b5
DJ
7401 loc->loc_type = bp_loc_other;
7402 break;
7403 default:
e2e0b3e5 7404 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7405 }
7406
f431efe5 7407 loc->refc = 1;
28010a5d
PA
7408}
7409
7410/* Allocate a struct bp_location. */
7411
7412static struct bp_location *
7413allocate_bp_location (struct breakpoint *bpt)
7414{
348d480f
PA
7415 return bpt->ops->allocate_location (bpt);
7416}
7cc221ef 7417
f431efe5
PA
7418static void
7419free_bp_location (struct bp_location *loc)
fe3f5fa8 7420{
348d480f 7421 loc->ops->dtor (loc);
fe3f5fa8
VP
7422 xfree (loc);
7423}
7424
f431efe5
PA
7425/* Increment reference count. */
7426
7427static void
7428incref_bp_location (struct bp_location *bl)
7429{
7430 ++bl->refc;
7431}
7432
7433/* Decrement reference count. If the reference count reaches 0,
7434 destroy the bp_location. Sets *BLP to NULL. */
7435
7436static void
7437decref_bp_location (struct bp_location **blp)
7438{
0807b50c
PA
7439 gdb_assert ((*blp)->refc > 0);
7440
f431efe5
PA
7441 if (--(*blp)->refc == 0)
7442 free_bp_location (*blp);
7443 *blp = NULL;
7444}
7445
346774a9 7446/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7447
346774a9
PA
7448static void
7449add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7450{
346774a9 7451 struct breakpoint *b1;
c906108c 7452
346774a9
PA
7453 /* Add this breakpoint to the end of the chain so that a list of
7454 breakpoints will come out in order of increasing numbers. */
7455
7456 b1 = breakpoint_chain;
7457 if (b1 == 0)
7458 breakpoint_chain = b;
7459 else
7460 {
7461 while (b1->next)
7462 b1 = b1->next;
7463 b1->next = b;
7464 }
7465}
7466
7467/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7468
7469static void
7470init_raw_breakpoint_without_location (struct breakpoint *b,
7471 struct gdbarch *gdbarch,
28010a5d 7472 enum bptype bptype,
c0a91b2b 7473 const struct breakpoint_ops *ops)
346774a9 7474{
c906108c 7475 memset (b, 0, sizeof (*b));
2219d63c 7476
348d480f
PA
7477 gdb_assert (ops != NULL);
7478
28010a5d 7479 b->ops = ops;
4d28f7a8 7480 b->type = bptype;
a6d9a66e 7481 b->gdbarch = gdbarch;
c906108c
SS
7482 b->language = current_language->la_language;
7483 b->input_radix = input_radix;
7484 b->thread = -1;
b5de0fa7 7485 b->enable_state = bp_enabled;
c906108c
SS
7486 b->next = 0;
7487 b->silent = 0;
7488 b->ignore_count = 0;
7489 b->commands = NULL;
818dd999 7490 b->frame_id = null_frame_id;
0d381245 7491 b->condition_not_parsed = 0;
84f4c1fe 7492 b->py_bp_object = NULL;
d0fb5eae 7493 b->related_breakpoint = b;
f00aae0f 7494 b->location = NULL;
346774a9
PA
7495}
7496
7497/* Helper to set_raw_breakpoint below. Creates a breakpoint
7498 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7499
7500static struct breakpoint *
7501set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7502 enum bptype bptype,
c0a91b2b 7503 const struct breakpoint_ops *ops)
346774a9
PA
7504{
7505 struct breakpoint *b = XNEW (struct breakpoint);
7506
348d480f 7507 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7508 add_to_breakpoint_chain (b);
0d381245
VP
7509 return b;
7510}
7511
0e30163f
JK
7512/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7513 resolutions should be made as the user specified the location explicitly
7514 enough. */
7515
0d381245 7516static void
0e30163f 7517set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7518{
2bdf28a0
JK
7519 gdb_assert (loc->owner != NULL);
7520
0d381245 7521 if (loc->owner->type == bp_breakpoint
1042e4c0 7522 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7523 || is_tracepoint (loc->owner))
0d381245 7524 {
0e30163f 7525 int is_gnu_ifunc;
2c02bd72 7526 const char *function_name;
6a3a010b 7527 CORE_ADDR func_addr;
0e30163f 7528
2c02bd72 7529 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7530 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7531
7532 if (is_gnu_ifunc && !explicit_loc)
7533 {
7534 struct breakpoint *b = loc->owner;
7535
7536 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7537 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7538 &loc->requested_address))
7539 {
7540 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7541 loc->address = adjust_breakpoint_address (loc->gdbarch,
7542 loc->requested_address,
7543 b->type);
7544 }
7545 else if (b->type == bp_breakpoint && b->loc == loc
7546 && loc->next == NULL && b->related_breakpoint == b)
7547 {
7548 /* Create only the whole new breakpoint of this type but do not
7549 mess more complicated breakpoints with multiple locations. */
7550 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7551 /* Remember the resolver's address for use by the return
7552 breakpoint. */
7553 loc->related_address = func_addr;
0e30163f
JK
7554 }
7555 }
7556
2c02bd72
DE
7557 if (function_name)
7558 loc->function_name = xstrdup (function_name);
0d381245
VP
7559 }
7560}
7561
a6d9a66e 7562/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7563struct gdbarch *
a6d9a66e
UW
7564get_sal_arch (struct symtab_and_line sal)
7565{
7566 if (sal.section)
7567 return get_objfile_arch (sal.section->objfile);
7568 if (sal.symtab)
eb822aa6 7569 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7570
7571 return NULL;
7572}
7573
346774a9
PA
7574/* Low level routine for partially initializing a breakpoint of type
7575 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7576 file name, and line number are provided by SAL.
0d381245
VP
7577
7578 It is expected that the caller will complete the initialization of
7579 the newly created breakpoint struct as well as output any status
c56053d2 7580 information regarding the creation of a new breakpoint. */
0d381245 7581
346774a9
PA
7582static void
7583init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7584 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7585 const struct breakpoint_ops *ops)
0d381245 7586{
28010a5d 7587 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7588
3742cc8b 7589 add_location_to_breakpoint (b, &sal);
0d381245 7590
6c95b8df
PA
7591 if (bptype != bp_catchpoint)
7592 gdb_assert (sal.pspace != NULL);
7593
f8eba3c6
TT
7594 /* Store the program space that was used to set the breakpoint,
7595 except for ordinary breakpoints, which are independent of the
7596 program space. */
7597 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7598 b->pspace = sal.pspace;
346774a9 7599}
c906108c 7600
346774a9
PA
7601/* set_raw_breakpoint is a low level routine for allocating and
7602 partially initializing a breakpoint of type BPTYPE. The newly
7603 created breakpoint's address, section, source file name, and line
7604 number are provided by SAL. The newly created and partially
7605 initialized breakpoint is added to the breakpoint chain and
7606 is also returned as the value of this function.
7607
7608 It is expected that the caller will complete the initialization of
7609 the newly created breakpoint struct as well as output any status
7610 information regarding the creation of a new breakpoint. In
7611 particular, set_raw_breakpoint does NOT set the breakpoint
7612 number! Care should be taken to not allow an error to occur
7613 prior to completing the initialization of the breakpoint. If this
7614 should happen, a bogus breakpoint will be left on the chain. */
7615
7616struct breakpoint *
7617set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7618 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7619 const struct breakpoint_ops *ops)
346774a9
PA
7620{
7621 struct breakpoint *b = XNEW (struct breakpoint);
7622
348d480f 7623 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7624 add_to_breakpoint_chain (b);
c906108c
SS
7625 return b;
7626}
7627
53a5351d 7628/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7629 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7630 initiated the operation. */
c906108c
SS
7631
7632void
186c406b 7633set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7634{
35df4500 7635 struct breakpoint *b, *b_tmp;
5d5658a1 7636 int thread = tp->global_num;
0fd8e87f
UW
7637
7638 /* To avoid having to rescan all objfile symbols at every step,
7639 we maintain a list of continually-inserted but always disabled
7640 longjmp "master" breakpoints. Here, we simply create momentary
7641 clones of those and enable them for the requested thread. */
35df4500 7642 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7643 if (b->pspace == current_program_space
186c406b
TT
7644 && (b->type == bp_longjmp_master
7645 || b->type == bp_exception_master))
0fd8e87f 7646 {
06edf0c0
PA
7647 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7648 struct breakpoint *clone;
cc59ec59 7649
e2e4d78b
JK
7650 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7651 after their removal. */
06edf0c0 7652 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7653 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7654 clone->thread = thread;
7655 }
186c406b
TT
7656
7657 tp->initiating_frame = frame;
c906108c
SS
7658}
7659
611c83ae 7660/* Delete all longjmp breakpoints from THREAD. */
c906108c 7661void
611c83ae 7662delete_longjmp_breakpoint (int thread)
c906108c 7663{
35df4500 7664 struct breakpoint *b, *b_tmp;
c906108c 7665
35df4500 7666 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7667 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7668 {
7669 if (b->thread == thread)
7670 delete_breakpoint (b);
7671 }
c906108c
SS
7672}
7673
f59f708a
PA
7674void
7675delete_longjmp_breakpoint_at_next_stop (int thread)
7676{
7677 struct breakpoint *b, *b_tmp;
7678
7679 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7680 if (b->type == bp_longjmp || b->type == bp_exception)
7681 {
7682 if (b->thread == thread)
7683 b->disposition = disp_del_at_next_stop;
7684 }
7685}
7686
e2e4d78b
JK
7687/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7688 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7689 pointer to any of them. Return NULL if this system cannot place longjmp
7690 breakpoints. */
7691
7692struct breakpoint *
7693set_longjmp_breakpoint_for_call_dummy (void)
7694{
7695 struct breakpoint *b, *retval = NULL;
7696
7697 ALL_BREAKPOINTS (b)
7698 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7699 {
7700 struct breakpoint *new_b;
7701
7702 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7703 &momentary_breakpoint_ops,
7704 1);
5d5658a1 7705 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7706
7707 /* Link NEW_B into the chain of RETVAL breakpoints. */
7708
7709 gdb_assert (new_b->related_breakpoint == new_b);
7710 if (retval == NULL)
7711 retval = new_b;
7712 new_b->related_breakpoint = retval;
7713 while (retval->related_breakpoint != new_b->related_breakpoint)
7714 retval = retval->related_breakpoint;
7715 retval->related_breakpoint = new_b;
7716 }
7717
7718 return retval;
7719}
7720
7721/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7722 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7723 stack.
7724
7725 You should call this function only at places where it is safe to currently
7726 unwind the whole stack. Failed stack unwind would discard live dummy
7727 frames. */
7728
7729void
b67a2c6f 7730check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7731{
7732 struct breakpoint *b, *b_tmp;
7733
7734 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7735 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7736 {
7737 struct breakpoint *dummy_b = b->related_breakpoint;
7738
7739 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7740 dummy_b = dummy_b->related_breakpoint;
7741 if (dummy_b->type != bp_call_dummy
7742 || frame_find_by_id (dummy_b->frame_id) != NULL)
7743 continue;
7744
b67a2c6f 7745 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7746
7747 while (b->related_breakpoint != b)
7748 {
7749 if (b_tmp == b->related_breakpoint)
7750 b_tmp = b->related_breakpoint->next;
7751 delete_breakpoint (b->related_breakpoint);
7752 }
7753 delete_breakpoint (b);
7754 }
7755}
7756
1900040c
MS
7757void
7758enable_overlay_breakpoints (void)
7759{
52f0bd74 7760 struct breakpoint *b;
1900040c
MS
7761
7762 ALL_BREAKPOINTS (b)
7763 if (b->type == bp_overlay_event)
7764 {
7765 b->enable_state = bp_enabled;
44702360 7766 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7767 overlay_events_enabled = 1;
1900040c
MS
7768 }
7769}
7770
7771void
7772disable_overlay_breakpoints (void)
7773{
52f0bd74 7774 struct breakpoint *b;
1900040c
MS
7775
7776 ALL_BREAKPOINTS (b)
7777 if (b->type == bp_overlay_event)
7778 {
7779 b->enable_state = bp_disabled;
44702360 7780 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7781 overlay_events_enabled = 0;
1900040c
MS
7782 }
7783}
7784
aa7d318d
TT
7785/* Set an active std::terminate breakpoint for each std::terminate
7786 master breakpoint. */
7787void
7788set_std_terminate_breakpoint (void)
7789{
35df4500 7790 struct breakpoint *b, *b_tmp;
aa7d318d 7791
35df4500 7792 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7793 if (b->pspace == current_program_space
7794 && b->type == bp_std_terminate_master)
7795 {
06edf0c0 7796 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7797 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7798 }
7799}
7800
7801/* Delete all the std::terminate breakpoints. */
7802void
7803delete_std_terminate_breakpoint (void)
7804{
35df4500 7805 struct breakpoint *b, *b_tmp;
aa7d318d 7806
35df4500 7807 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7808 if (b->type == bp_std_terminate)
7809 delete_breakpoint (b);
7810}
7811
c4093a6a 7812struct breakpoint *
a6d9a66e 7813create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7814{
7815 struct breakpoint *b;
c4093a6a 7816
06edf0c0
PA
7817 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7818 &internal_breakpoint_ops);
7819
b5de0fa7 7820 b->enable_state = bp_enabled;
f00aae0f 7821 /* location has to be used or breakpoint_re_set will delete me. */
305e13e6 7822 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7823
44702360 7824 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7825
c4093a6a
JM
7826 return b;
7827}
7828
0101ce28
JJ
7829struct lang_and_radix
7830 {
7831 enum language lang;
7832 int radix;
7833 };
7834
4efc6507
DE
7835/* Create a breakpoint for JIT code registration and unregistration. */
7836
7837struct breakpoint *
7838create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7839{
2a7f3dff
PA
7840 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7841 &internal_breakpoint_ops);
4efc6507 7842}
0101ce28 7843
03673fc7
PP
7844/* Remove JIT code registration and unregistration breakpoint(s). */
7845
7846void
7847remove_jit_event_breakpoints (void)
7848{
7849 struct breakpoint *b, *b_tmp;
7850
7851 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7852 if (b->type == bp_jit_event
7853 && b->loc->pspace == current_program_space)
7854 delete_breakpoint (b);
7855}
7856
cae688ec
JJ
7857void
7858remove_solib_event_breakpoints (void)
7859{
35df4500 7860 struct breakpoint *b, *b_tmp;
cae688ec 7861
35df4500 7862 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7863 if (b->type == bp_shlib_event
7864 && b->loc->pspace == current_program_space)
cae688ec
JJ
7865 delete_breakpoint (b);
7866}
7867
f37f681c
PA
7868/* See breakpoint.h. */
7869
7870void
7871remove_solib_event_breakpoints_at_next_stop (void)
7872{
7873 struct breakpoint *b, *b_tmp;
7874
7875 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7876 if (b->type == bp_shlib_event
7877 && b->loc->pspace == current_program_space)
7878 b->disposition = disp_del_at_next_stop;
7879}
7880
04086b45
PA
7881/* Helper for create_solib_event_breakpoint /
7882 create_and_insert_solib_event_breakpoint. Allows specifying which
7883 INSERT_MODE to pass through to update_global_location_list. */
7884
7885static struct breakpoint *
7886create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7887 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7888{
7889 struct breakpoint *b;
7890
06edf0c0
PA
7891 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7892 &internal_breakpoint_ops);
04086b45 7893 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7894 return b;
7895}
7896
04086b45
PA
7897struct breakpoint *
7898create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7899{
7900 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7901}
7902
f37f681c
PA
7903/* See breakpoint.h. */
7904
7905struct breakpoint *
7906create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7907{
7908 struct breakpoint *b;
7909
04086b45
PA
7910 /* Explicitly tell update_global_location_list to insert
7911 locations. */
7912 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7913 if (!b->loc->inserted)
7914 {
7915 delete_breakpoint (b);
7916 return NULL;
7917 }
7918 return b;
7919}
7920
cae688ec
JJ
7921/* Disable any breakpoints that are on code in shared libraries. Only
7922 apply to enabled breakpoints, disabled ones can just stay disabled. */
7923
7924void
cb851954 7925disable_breakpoints_in_shlibs (void)
cae688ec 7926{
876fa593 7927 struct bp_location *loc, **locp_tmp;
cae688ec 7928
876fa593 7929 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7930 {
2bdf28a0 7931 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7932 struct breakpoint *b = loc->owner;
2bdf28a0 7933
4a64f543
MS
7934 /* We apply the check to all breakpoints, including disabled for
7935 those with loc->duplicate set. This is so that when breakpoint
7936 becomes enabled, or the duplicate is removed, gdb will try to
7937 insert all breakpoints. If we don't set shlib_disabled here,
7938 we'll try to insert those breakpoints and fail. */
1042e4c0 7939 if (((b->type == bp_breakpoint)
508ccb1f 7940 || (b->type == bp_jit_event)
1042e4c0 7941 || (b->type == bp_hardware_breakpoint)
d77f58be 7942 || (is_tracepoint (b)))
6c95b8df 7943 && loc->pspace == current_program_space
0d381245 7944 && !loc->shlib_disabled
6c95b8df 7945 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7946 )
0d381245
VP
7947 {
7948 loc->shlib_disabled = 1;
7949 }
cae688ec
JJ
7950 }
7951}
7952
63644780
NB
7953/* Disable any breakpoints and tracepoints that are in SOLIB upon
7954 notification of unloaded_shlib. Only apply to enabled breakpoints,
7955 disabled ones can just stay disabled. */
84acb35a 7956
75149521 7957static void
84acb35a
JJ
7958disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7959{
876fa593 7960 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7961 int disabled_shlib_breaks = 0;
7962
c86cf029
VP
7963 /* SunOS a.out shared libraries are always mapped, so do not
7964 disable breakpoints; they will only be reported as unloaded
7965 through clear_solib when GDB discards its shared library
7966 list. See clear_solib for more information. */
7967 if (exec_bfd != NULL
7968 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7969 return;
7970
876fa593 7971 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7972 {
2bdf28a0 7973 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7974 struct breakpoint *b = loc->owner;
cc59ec59 7975
1e4d1764 7976 if (solib->pspace == loc->pspace
e2dd7057 7977 && !loc->shlib_disabled
1e4d1764
YQ
7978 && (((b->type == bp_breakpoint
7979 || b->type == bp_jit_event
7980 || b->type == bp_hardware_breakpoint)
7981 && (loc->loc_type == bp_loc_hardware_breakpoint
7982 || loc->loc_type == bp_loc_software_breakpoint))
7983 || is_tracepoint (b))
e2dd7057 7984 && solib_contains_address_p (solib, loc->address))
84acb35a 7985 {
e2dd7057
PP
7986 loc->shlib_disabled = 1;
7987 /* At this point, we cannot rely on remove_breakpoint
7988 succeeding so we must mark the breakpoint as not inserted
7989 to prevent future errors occurring in remove_breakpoints. */
7990 loc->inserted = 0;
8d3788bd
VP
7991
7992 /* This may cause duplicate notifications for the same breakpoint. */
7993 observer_notify_breakpoint_modified (b);
7994
e2dd7057
PP
7995 if (!disabled_shlib_breaks)
7996 {
7997 target_terminal_ours_for_output ();
3e43a32a
MS
7998 warning (_("Temporarily disabling breakpoints "
7999 "for unloaded shared library \"%s\""),
e2dd7057 8000 solib->so_name);
84acb35a 8001 }
e2dd7057 8002 disabled_shlib_breaks = 1;
84acb35a
JJ
8003 }
8004 }
84acb35a
JJ
8005}
8006
63644780
NB
8007/* Disable any breakpoints and tracepoints in OBJFILE upon
8008 notification of free_objfile. Only apply to enabled breakpoints,
8009 disabled ones can just stay disabled. */
8010
8011static void
8012disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8013{
8014 struct breakpoint *b;
8015
8016 if (objfile == NULL)
8017 return;
8018
d03de421
PA
8019 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8020 managed by the user with add-symbol-file/remove-symbol-file.
8021 Similarly to how breakpoints in shared libraries are handled in
8022 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
8023 shlib_disabled so they end up uninserted on the next global
8024 location list update. Shared libraries not loaded by the user
8025 aren't handled here -- they're already handled in
8026 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8027 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8028 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8029 main objfile). */
8030 if ((objfile->flags & OBJF_SHARED) == 0
8031 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8032 return;
8033
8034 ALL_BREAKPOINTS (b)
8035 {
8036 struct bp_location *loc;
8037 int bp_modified = 0;
8038
8039 if (!is_breakpoint (b) && !is_tracepoint (b))
8040 continue;
8041
8042 for (loc = b->loc; loc != NULL; loc = loc->next)
8043 {
8044 CORE_ADDR loc_addr = loc->address;
8045
8046 if (loc->loc_type != bp_loc_hardware_breakpoint
8047 && loc->loc_type != bp_loc_software_breakpoint)
8048 continue;
8049
8050 if (loc->shlib_disabled != 0)
8051 continue;
8052
8053 if (objfile->pspace != loc->pspace)
8054 continue;
8055
8056 if (loc->loc_type != bp_loc_hardware_breakpoint
8057 && loc->loc_type != bp_loc_software_breakpoint)
8058 continue;
8059
8060 if (is_addr_in_objfile (loc_addr, objfile))
8061 {
8062 loc->shlib_disabled = 1;
08351840
PA
8063 /* At this point, we don't know whether the object was
8064 unmapped from the inferior or not, so leave the
8065 inserted flag alone. We'll handle failure to
8066 uninsert quietly, in case the object was indeed
8067 unmapped. */
63644780
NB
8068
8069 mark_breakpoint_location_modified (loc);
8070
8071 bp_modified = 1;
8072 }
8073 }
8074
8075 if (bp_modified)
8076 observer_notify_breakpoint_modified (b);
8077 }
8078}
8079
ce78b96d
JB
8080/* FORK & VFORK catchpoints. */
8081
e29a4733
PA
8082/* An instance of this type is used to represent a fork or vfork
8083 catchpoint. It includes a "struct breakpoint" as a kind of base
8084 class; users downcast to "struct breakpoint *" when needed. A
8085 breakpoint is really of this type iff its ops pointer points to
8086 CATCH_FORK_BREAKPOINT_OPS. */
8087
8088struct fork_catchpoint
8089{
8090 /* The base class. */
8091 struct breakpoint base;
8092
8093 /* Process id of a child process whose forking triggered this
8094 catchpoint. This field is only valid immediately after this
8095 catchpoint has triggered. */
8096 ptid_t forked_inferior_pid;
8097};
8098
4a64f543
MS
8099/* Implement the "insert" breakpoint_ops method for fork
8100 catchpoints. */
ce78b96d 8101
77b06cd7
TJB
8102static int
8103insert_catch_fork (struct bp_location *bl)
ce78b96d 8104{
dfd4cc63 8105 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8106}
8107
4a64f543
MS
8108/* Implement the "remove" breakpoint_ops method for fork
8109 catchpoints. */
ce78b96d
JB
8110
8111static int
73971819 8112remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8113{
dfd4cc63 8114 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8115}
8116
8117/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8118 catchpoints. */
8119
8120static int
f1310107 8121breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8122 struct address_space *aspace, CORE_ADDR bp_addr,
8123 const struct target_waitstatus *ws)
ce78b96d 8124{
e29a4733
PA
8125 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8126
f90263c1
TT
8127 if (ws->kind != TARGET_WAITKIND_FORKED)
8128 return 0;
8129
8130 c->forked_inferior_pid = ws->value.related_pid;
8131 return 1;
ce78b96d
JB
8132}
8133
4a64f543
MS
8134/* Implement the "print_it" breakpoint_ops method for fork
8135 catchpoints. */
ce78b96d
JB
8136
8137static enum print_stop_action
348d480f 8138print_it_catch_fork (bpstat bs)
ce78b96d 8139{
36dfb11c 8140 struct ui_out *uiout = current_uiout;
348d480f
PA
8141 struct breakpoint *b = bs->breakpoint_at;
8142 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8143
ce78b96d 8144 annotate_catchpoint (b->number);
f303dbd6 8145 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8146 if (b->disposition == disp_del)
f303dbd6 8147 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8148 else
f303dbd6 8149 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8150 if (ui_out_is_mi_like_p (uiout))
8151 {
8152 ui_out_field_string (uiout, "reason",
8153 async_reason_lookup (EXEC_ASYNC_FORK));
8154 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8155 }
8156 ui_out_field_int (uiout, "bkptno", b->number);
8157 ui_out_text (uiout, " (forked process ");
8158 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8159 ui_out_text (uiout, "), ");
ce78b96d
JB
8160 return PRINT_SRC_AND_LOC;
8161}
8162
4a64f543
MS
8163/* Implement the "print_one" breakpoint_ops method for fork
8164 catchpoints. */
ce78b96d
JB
8165
8166static void
a6d9a66e 8167print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8168{
e29a4733 8169 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8170 struct value_print_options opts;
79a45e25 8171 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8172
8173 get_user_print_options (&opts);
8174
4a64f543
MS
8175 /* Field 4, the address, is omitted (which makes the columns not
8176 line up too nicely with the headers, but the effect is relatively
8177 readable). */
79a45b7d 8178 if (opts.addressprint)
ce78b96d
JB
8179 ui_out_field_skip (uiout, "addr");
8180 annotate_field (5);
8181 ui_out_text (uiout, "fork");
e29a4733 8182 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8183 {
8184 ui_out_text (uiout, ", process ");
8185 ui_out_field_int (uiout, "what",
e29a4733 8186 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8187 ui_out_spaces (uiout, 1);
8188 }
8ac3646f
TT
8189
8190 if (ui_out_is_mi_like_p (uiout))
8191 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8192}
8193
8194/* Implement the "print_mention" breakpoint_ops method for fork
8195 catchpoints. */
8196
8197static void
8198print_mention_catch_fork (struct breakpoint *b)
8199{
8200 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8201}
8202
6149aea9
PA
8203/* Implement the "print_recreate" breakpoint_ops method for fork
8204 catchpoints. */
8205
8206static void
8207print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8208{
8209 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8210 print_recreate_thread (b, fp);
6149aea9
PA
8211}
8212
ce78b96d
JB
8213/* The breakpoint_ops structure to be used in fork catchpoints. */
8214
2060206e 8215static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8216
4a64f543
MS
8217/* Implement the "insert" breakpoint_ops method for vfork
8218 catchpoints. */
ce78b96d 8219
77b06cd7
TJB
8220static int
8221insert_catch_vfork (struct bp_location *bl)
ce78b96d 8222{
dfd4cc63 8223 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8224}
8225
4a64f543
MS
8226/* Implement the "remove" breakpoint_ops method for vfork
8227 catchpoints. */
ce78b96d
JB
8228
8229static int
73971819 8230remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8231{
dfd4cc63 8232 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8233}
8234
8235/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8236 catchpoints. */
8237
8238static int
f1310107 8239breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8240 struct address_space *aspace, CORE_ADDR bp_addr,
8241 const struct target_waitstatus *ws)
ce78b96d 8242{
e29a4733
PA
8243 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8244
f90263c1
TT
8245 if (ws->kind != TARGET_WAITKIND_VFORKED)
8246 return 0;
8247
8248 c->forked_inferior_pid = ws->value.related_pid;
8249 return 1;
ce78b96d
JB
8250}
8251
4a64f543
MS
8252/* Implement the "print_it" breakpoint_ops method for vfork
8253 catchpoints. */
ce78b96d
JB
8254
8255static enum print_stop_action
348d480f 8256print_it_catch_vfork (bpstat bs)
ce78b96d 8257{
36dfb11c 8258 struct ui_out *uiout = current_uiout;
348d480f 8259 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8260 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8261
ce78b96d 8262 annotate_catchpoint (b->number);
f303dbd6 8263 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8264 if (b->disposition == disp_del)
f303dbd6 8265 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8266 else
f303dbd6 8267 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8268 if (ui_out_is_mi_like_p (uiout))
8269 {
8270 ui_out_field_string (uiout, "reason",
8271 async_reason_lookup (EXEC_ASYNC_VFORK));
8272 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8273 }
8274 ui_out_field_int (uiout, "bkptno", b->number);
8275 ui_out_text (uiout, " (vforked process ");
8276 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8277 ui_out_text (uiout, "), ");
ce78b96d
JB
8278 return PRINT_SRC_AND_LOC;
8279}
8280
4a64f543
MS
8281/* Implement the "print_one" breakpoint_ops method for vfork
8282 catchpoints. */
ce78b96d
JB
8283
8284static void
a6d9a66e 8285print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8286{
e29a4733 8287 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8288 struct value_print_options opts;
79a45e25 8289 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8290
8291 get_user_print_options (&opts);
4a64f543
MS
8292 /* Field 4, the address, is omitted (which makes the columns not
8293 line up too nicely with the headers, but the effect is relatively
8294 readable). */
79a45b7d 8295 if (opts.addressprint)
ce78b96d
JB
8296 ui_out_field_skip (uiout, "addr");
8297 annotate_field (5);
8298 ui_out_text (uiout, "vfork");
e29a4733 8299 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8300 {
8301 ui_out_text (uiout, ", process ");
8302 ui_out_field_int (uiout, "what",
e29a4733 8303 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8304 ui_out_spaces (uiout, 1);
8305 }
8ac3646f
TT
8306
8307 if (ui_out_is_mi_like_p (uiout))
8308 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8309}
8310
8311/* Implement the "print_mention" breakpoint_ops method for vfork
8312 catchpoints. */
8313
8314static void
8315print_mention_catch_vfork (struct breakpoint *b)
8316{
8317 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8318}
8319
6149aea9
PA
8320/* Implement the "print_recreate" breakpoint_ops method for vfork
8321 catchpoints. */
8322
8323static void
8324print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8325{
8326 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8327 print_recreate_thread (b, fp);
6149aea9
PA
8328}
8329
ce78b96d
JB
8330/* The breakpoint_ops structure to be used in vfork catchpoints. */
8331
2060206e 8332static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8333
edcc5120
TT
8334/* An instance of this type is used to represent an solib catchpoint.
8335 It includes a "struct breakpoint" as a kind of base class; users
8336 downcast to "struct breakpoint *" when needed. A breakpoint is
8337 really of this type iff its ops pointer points to
8338 CATCH_SOLIB_BREAKPOINT_OPS. */
8339
8340struct solib_catchpoint
8341{
8342 /* The base class. */
8343 struct breakpoint base;
8344
8345 /* True for "catch load", false for "catch unload". */
8346 unsigned char is_load;
8347
8348 /* Regular expression to match, if any. COMPILED is only valid when
8349 REGEX is non-NULL. */
8350 char *regex;
8351 regex_t compiled;
8352};
8353
8354static void
8355dtor_catch_solib (struct breakpoint *b)
8356{
8357 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8358
8359 if (self->regex)
8360 regfree (&self->compiled);
8361 xfree (self->regex);
8362
8363 base_breakpoint_ops.dtor (b);
8364}
8365
8366static int
8367insert_catch_solib (struct bp_location *ignore)
8368{
8369 return 0;
8370}
8371
8372static int
73971819 8373remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8374{
8375 return 0;
8376}
8377
8378static int
8379breakpoint_hit_catch_solib (const struct bp_location *bl,
8380 struct address_space *aspace,
8381 CORE_ADDR bp_addr,
8382 const struct target_waitstatus *ws)
8383{
8384 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8385 struct breakpoint *other;
8386
8387 if (ws->kind == TARGET_WAITKIND_LOADED)
8388 return 1;
8389
8390 ALL_BREAKPOINTS (other)
8391 {
8392 struct bp_location *other_bl;
8393
8394 if (other == bl->owner)
8395 continue;
8396
8397 if (other->type != bp_shlib_event)
8398 continue;
8399
8400 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8401 continue;
8402
8403 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8404 {
8405 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8406 return 1;
8407 }
8408 }
8409
8410 return 0;
8411}
8412
8413static void
8414check_status_catch_solib (struct bpstats *bs)
8415{
8416 struct solib_catchpoint *self
8417 = (struct solib_catchpoint *) bs->breakpoint_at;
8418 int ix;
8419
8420 if (self->is_load)
8421 {
8422 struct so_list *iter;
8423
8424 for (ix = 0;
8425 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8426 ix, iter);
8427 ++ix)
8428 {
8429 if (!self->regex
8430 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8431 return;
8432 }
8433 }
8434 else
8435 {
8436 char *iter;
8437
8438 for (ix = 0;
8439 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8440 ix, iter);
8441 ++ix)
8442 {
8443 if (!self->regex
8444 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8445 return;
8446 }
8447 }
8448
8449 bs->stop = 0;
8450 bs->print_it = print_it_noop;
8451}
8452
8453static enum print_stop_action
8454print_it_catch_solib (bpstat bs)
8455{
8456 struct breakpoint *b = bs->breakpoint_at;
8457 struct ui_out *uiout = current_uiout;
8458
8459 annotate_catchpoint (b->number);
f303dbd6 8460 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8461 if (b->disposition == disp_del)
f303dbd6 8462 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8463 else
f303dbd6 8464 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8465 ui_out_field_int (uiout, "bkptno", b->number);
8466 ui_out_text (uiout, "\n");
8467 if (ui_out_is_mi_like_p (uiout))
8468 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8469 print_solib_event (1);
8470 return PRINT_SRC_AND_LOC;
8471}
8472
8473static void
8474print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8475{
8476 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8477 struct value_print_options opts;
8478 struct ui_out *uiout = current_uiout;
8479 char *msg;
8480
8481 get_user_print_options (&opts);
8482 /* Field 4, the address, is omitted (which makes the columns not
8483 line up too nicely with the headers, but the effect is relatively
8484 readable). */
8485 if (opts.addressprint)
8486 {
8487 annotate_field (4);
8488 ui_out_field_skip (uiout, "addr");
8489 }
8490
8491 annotate_field (5);
8492 if (self->is_load)
8493 {
8494 if (self->regex)
8495 msg = xstrprintf (_("load of library matching %s"), self->regex);
8496 else
8497 msg = xstrdup (_("load of library"));
8498 }
8499 else
8500 {
8501 if (self->regex)
8502 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8503 else
8504 msg = xstrdup (_("unload of library"));
8505 }
8506 ui_out_field_string (uiout, "what", msg);
8507 xfree (msg);
8ac3646f
TT
8508
8509 if (ui_out_is_mi_like_p (uiout))
8510 ui_out_field_string (uiout, "catch-type",
8511 self->is_load ? "load" : "unload");
edcc5120
TT
8512}
8513
8514static void
8515print_mention_catch_solib (struct breakpoint *b)
8516{
8517 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8518
8519 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8520 self->is_load ? "load" : "unload");
8521}
8522
8523static void
8524print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8525{
8526 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8527
8528 fprintf_unfiltered (fp, "%s %s",
8529 b->disposition == disp_del ? "tcatch" : "catch",
8530 self->is_load ? "load" : "unload");
8531 if (self->regex)
8532 fprintf_unfiltered (fp, " %s", self->regex);
8533 fprintf_unfiltered (fp, "\n");
8534}
8535
8536static struct breakpoint_ops catch_solib_breakpoint_ops;
8537
91985142
MG
8538/* Shared helper function (MI and CLI) for creating and installing
8539 a shared object event catchpoint. If IS_LOAD is non-zero then
8540 the events to be caught are load events, otherwise they are
8541 unload events. If IS_TEMP is non-zero the catchpoint is a
8542 temporary one. If ENABLED is non-zero the catchpoint is
8543 created in an enabled state. */
edcc5120 8544
91985142
MG
8545void
8546add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8547{
8548 struct solib_catchpoint *c;
8549 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8550 struct cleanup *cleanup;
8551
edcc5120
TT
8552 if (!arg)
8553 arg = "";
8554 arg = skip_spaces (arg);
8555
8556 c = XCNEW (struct solib_catchpoint);
8557 cleanup = make_cleanup (xfree, c);
8558
8559 if (*arg != '\0')
8560 {
8561 int errcode;
8562
8563 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8564 if (errcode != 0)
8565 {
8566 char *err = get_regcomp_error (errcode, &c->compiled);
8567
8568 make_cleanup (xfree, err);
8569 error (_("Invalid regexp (%s): %s"), err, arg);
8570 }
8571 c->regex = xstrdup (arg);
8572 }
8573
8574 c->is_load = is_load;
91985142 8575 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8576 &catch_solib_breakpoint_ops);
8577
91985142
MG
8578 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8579
edcc5120
TT
8580 discard_cleanups (cleanup);
8581 install_breakpoint (0, &c->base, 1);
8582}
8583
91985142
MG
8584/* A helper function that does all the work for "catch load" and
8585 "catch unload". */
8586
8587static void
8588catch_load_or_unload (char *arg, int from_tty, int is_load,
8589 struct cmd_list_element *command)
8590{
8591 int tempflag;
8592 const int enabled = 1;
8593
8594 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8595
8596 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8597}
8598
edcc5120
TT
8599static void
8600catch_load_command_1 (char *arg, int from_tty,
8601 struct cmd_list_element *command)
8602{
8603 catch_load_or_unload (arg, from_tty, 1, command);
8604}
8605
8606static void
8607catch_unload_command_1 (char *arg, int from_tty,
8608 struct cmd_list_element *command)
8609{
8610 catch_load_or_unload (arg, from_tty, 0, command);
8611}
8612
346774a9
PA
8613/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8614 is non-zero, then make the breakpoint temporary. If COND_STRING is
8615 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8616 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8617
ab04a2af 8618void
346774a9
PA
8619init_catchpoint (struct breakpoint *b,
8620 struct gdbarch *gdbarch, int tempflag,
8621 char *cond_string,
c0a91b2b 8622 const struct breakpoint_ops *ops)
c906108c 8623{
c5aa993b 8624 struct symtab_and_line sal;
346774a9 8625
fe39c653 8626 init_sal (&sal);
6c95b8df 8627 sal.pspace = current_program_space;
c5aa993b 8628
28010a5d 8629 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8630
1b36a34b 8631 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8632 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8633}
8634
28010a5d 8635void
3ea46bff 8636install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8637{
8638 add_to_breakpoint_chain (b);
3a5c3e22 8639 set_breakpoint_number (internal, b);
558a9d82
YQ
8640 if (is_tracepoint (b))
8641 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8642 if (!internal)
8643 mention (b);
c56053d2 8644 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8645
8646 if (update_gll)
44702360 8647 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8648}
8649
9b70b993 8650static void
a6d9a66e
UW
8651create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8652 int tempflag, char *cond_string,
c0a91b2b 8653 const struct breakpoint_ops *ops)
c906108c 8654{
e29a4733 8655 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8656
e29a4733
PA
8657 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8658
8659 c->forked_inferior_pid = null_ptid;
8660
3ea46bff 8661 install_breakpoint (0, &c->base, 1);
c906108c
SS
8662}
8663
fe798b75
JB
8664/* Exec catchpoints. */
8665
b4d90040
PA
8666/* An instance of this type is used to represent an exec catchpoint.
8667 It includes a "struct breakpoint" as a kind of base class; users
8668 downcast to "struct breakpoint *" when needed. A breakpoint is
8669 really of this type iff its ops pointer points to
8670 CATCH_EXEC_BREAKPOINT_OPS. */
8671
8672struct exec_catchpoint
8673{
8674 /* The base class. */
8675 struct breakpoint base;
8676
8677 /* Filename of a program whose exec triggered this catchpoint.
8678 This field is only valid immediately after this catchpoint has
8679 triggered. */
8680 char *exec_pathname;
8681};
8682
8683/* Implement the "dtor" breakpoint_ops method for exec
8684 catchpoints. */
8685
8686static void
8687dtor_catch_exec (struct breakpoint *b)
8688{
8689 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8690
8691 xfree (c->exec_pathname);
348d480f 8692
2060206e 8693 base_breakpoint_ops.dtor (b);
b4d90040
PA
8694}
8695
77b06cd7
TJB
8696static int
8697insert_catch_exec (struct bp_location *bl)
c906108c 8698{
dfd4cc63 8699 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8700}
c906108c 8701
fe798b75 8702static int
73971819 8703remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8704{
dfd4cc63 8705 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8706}
c906108c 8707
fe798b75 8708static int
f1310107 8709breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8710 struct address_space *aspace, CORE_ADDR bp_addr,
8711 const struct target_waitstatus *ws)
fe798b75 8712{
b4d90040
PA
8713 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8714
f90263c1
TT
8715 if (ws->kind != TARGET_WAITKIND_EXECD)
8716 return 0;
8717
8718 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8719 return 1;
fe798b75 8720}
c906108c 8721
fe798b75 8722static enum print_stop_action
348d480f 8723print_it_catch_exec (bpstat bs)
fe798b75 8724{
36dfb11c 8725 struct ui_out *uiout = current_uiout;
348d480f 8726 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8727 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8728
fe798b75 8729 annotate_catchpoint (b->number);
f303dbd6 8730 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8731 if (b->disposition == disp_del)
f303dbd6 8732 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8733 else
f303dbd6 8734 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8735 if (ui_out_is_mi_like_p (uiout))
8736 {
8737 ui_out_field_string (uiout, "reason",
8738 async_reason_lookup (EXEC_ASYNC_EXEC));
8739 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8740 }
8741 ui_out_field_int (uiout, "bkptno", b->number);
8742 ui_out_text (uiout, " (exec'd ");
8743 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8744 ui_out_text (uiout, "), ");
8745
fe798b75 8746 return PRINT_SRC_AND_LOC;
c906108c
SS
8747}
8748
fe798b75 8749static void
a6d9a66e 8750print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8751{
b4d90040 8752 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8753 struct value_print_options opts;
79a45e25 8754 struct ui_out *uiout = current_uiout;
fe798b75
JB
8755
8756 get_user_print_options (&opts);
8757
8758 /* Field 4, the address, is omitted (which makes the columns
8759 not line up too nicely with the headers, but the effect
8760 is relatively readable). */
8761 if (opts.addressprint)
8762 ui_out_field_skip (uiout, "addr");
8763 annotate_field (5);
8764 ui_out_text (uiout, "exec");
b4d90040 8765 if (c->exec_pathname != NULL)
fe798b75
JB
8766 {
8767 ui_out_text (uiout, ", program \"");
b4d90040 8768 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8769 ui_out_text (uiout, "\" ");
8770 }
8ac3646f
TT
8771
8772 if (ui_out_is_mi_like_p (uiout))
8773 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8774}
8775
8776static void
8777print_mention_catch_exec (struct breakpoint *b)
8778{
8779 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8780}
8781
6149aea9
PA
8782/* Implement the "print_recreate" breakpoint_ops method for exec
8783 catchpoints. */
8784
8785static void
8786print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8787{
8788 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8789 print_recreate_thread (b, fp);
6149aea9
PA
8790}
8791
2060206e 8792static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8793
c906108c 8794static int
fba45db2 8795hw_breakpoint_used_count (void)
c906108c 8796{
c906108c 8797 int i = 0;
f1310107
TJB
8798 struct breakpoint *b;
8799 struct bp_location *bl;
c906108c
SS
8800
8801 ALL_BREAKPOINTS (b)
c5aa993b 8802 {
d6b74ac4 8803 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8804 for (bl = b->loc; bl; bl = bl->next)
8805 {
8806 /* Special types of hardware breakpoints may use more than
8807 one register. */
348d480f 8808 i += b->ops->resources_needed (bl);
f1310107 8809 }
c5aa993b 8810 }
c906108c
SS
8811
8812 return i;
8813}
8814
a1398e0c
PA
8815/* Returns the resources B would use if it were a hardware
8816 watchpoint. */
8817
c906108c 8818static int
a1398e0c 8819hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8820{
c906108c 8821 int i = 0;
e09342b5 8822 struct bp_location *bl;
c906108c 8823
a1398e0c
PA
8824 if (!breakpoint_enabled (b))
8825 return 0;
8826
8827 for (bl = b->loc; bl; bl = bl->next)
8828 {
8829 /* Special types of hardware watchpoints may use more than
8830 one register. */
8831 i += b->ops->resources_needed (bl);
8832 }
8833
8834 return i;
8835}
8836
8837/* Returns the sum the used resources of all hardware watchpoints of
8838 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8839 the sum of the used resources of all hardware watchpoints of other
8840 types _not_ TYPE. */
8841
8842static int
8843hw_watchpoint_used_count_others (struct breakpoint *except,
8844 enum bptype type, int *other_type_used)
8845{
8846 int i = 0;
8847 struct breakpoint *b;
8848
c906108c
SS
8849 *other_type_used = 0;
8850 ALL_BREAKPOINTS (b)
e09342b5 8851 {
a1398e0c
PA
8852 if (b == except)
8853 continue;
e09342b5
TJB
8854 if (!breakpoint_enabled (b))
8855 continue;
8856
a1398e0c
PA
8857 if (b->type == type)
8858 i += hw_watchpoint_use_count (b);
8859 else if (is_hardware_watchpoint (b))
8860 *other_type_used = 1;
e09342b5
TJB
8861 }
8862
c906108c
SS
8863 return i;
8864}
8865
c906108c 8866void
fba45db2 8867disable_watchpoints_before_interactive_call_start (void)
c906108c 8868{
c5aa993b 8869 struct breakpoint *b;
c906108c
SS
8870
8871 ALL_BREAKPOINTS (b)
c5aa993b 8872 {
cc60f2e3 8873 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8874 {
b5de0fa7 8875 b->enable_state = bp_call_disabled;
44702360 8876 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8877 }
8878 }
c906108c
SS
8879}
8880
8881void
fba45db2 8882enable_watchpoints_after_interactive_call_stop (void)
c906108c 8883{
c5aa993b 8884 struct breakpoint *b;
c906108c
SS
8885
8886 ALL_BREAKPOINTS (b)
c5aa993b 8887 {
cc60f2e3 8888 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8889 {
b5de0fa7 8890 b->enable_state = bp_enabled;
44702360 8891 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8892 }
8893 }
c906108c
SS
8894}
8895
8bea4e01
UW
8896void
8897disable_breakpoints_before_startup (void)
8898{
6c95b8df 8899 current_program_space->executing_startup = 1;
44702360 8900 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8901}
8902
8903void
8904enable_breakpoints_after_startup (void)
8905{
6c95b8df 8906 current_program_space->executing_startup = 0;
f8eba3c6 8907 breakpoint_re_set ();
8bea4e01
UW
8908}
8909
7c16b83e
PA
8910/* Create a new single-step breakpoint for thread THREAD, with no
8911 locations. */
c906108c 8912
7c16b83e
PA
8913static struct breakpoint *
8914new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8915{
8916 struct breakpoint *b = XNEW (struct breakpoint);
8917
8918 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8919 &momentary_breakpoint_ops);
8920
8921 b->disposition = disp_donttouch;
8922 b->frame_id = null_frame_id;
8923
8924 b->thread = thread;
8925 gdb_assert (b->thread != 0);
8926
8927 add_to_breakpoint_chain (b);
8928
8929 return b;
8930}
8931
8932/* Set a momentary breakpoint of type TYPE at address specified by
8933 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8934 frame. */
c906108c
SS
8935
8936struct breakpoint *
a6d9a66e
UW
8937set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8938 struct frame_id frame_id, enum bptype type)
c906108c 8939{
52f0bd74 8940 struct breakpoint *b;
edb3359d 8941
193facb3
JK
8942 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8943 tail-called one. */
8944 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8945
06edf0c0 8946 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8947 b->enable_state = bp_enabled;
8948 b->disposition = disp_donttouch;
818dd999 8949 b->frame_id = frame_id;
c906108c 8950
4a64f543
MS
8951 /* If we're debugging a multi-threaded program, then we want
8952 momentary breakpoints to be active in only a single thread of
8953 control. */
39f77062 8954 if (in_thread_list (inferior_ptid))
5d5658a1 8955 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8956
44702360 8957 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8958
c906108c
SS
8959 return b;
8960}
611c83ae 8961
06edf0c0 8962/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8963 The new breakpoint will have type TYPE, use OPS as its
8964 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8965
06edf0c0
PA
8966static struct breakpoint *
8967momentary_breakpoint_from_master (struct breakpoint *orig,
8968 enum bptype type,
a1aa2221
LM
8969 const struct breakpoint_ops *ops,
8970 int loc_enabled)
e58b0e63
PA
8971{
8972 struct breakpoint *copy;
8973
06edf0c0 8974 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8975 copy->loc = allocate_bp_location (copy);
0e30163f 8976 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8977
a6d9a66e 8978 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8979 copy->loc->requested_address = orig->loc->requested_address;
8980 copy->loc->address = orig->loc->address;
8981 copy->loc->section = orig->loc->section;
6c95b8df 8982 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8983 copy->loc->probe = orig->loc->probe;
f8eba3c6 8984 copy->loc->line_number = orig->loc->line_number;
2f202fde 8985 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8986 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8987 copy->frame_id = orig->frame_id;
8988 copy->thread = orig->thread;
6c95b8df 8989 copy->pspace = orig->pspace;
e58b0e63
PA
8990
8991 copy->enable_state = bp_enabled;
8992 copy->disposition = disp_donttouch;
8993 copy->number = internal_breakpoint_number--;
8994
44702360 8995 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8996 return copy;
8997}
8998
06edf0c0
PA
8999/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9000 ORIG is NULL. */
9001
9002struct breakpoint *
9003clone_momentary_breakpoint (struct breakpoint *orig)
9004{
9005 /* If there's nothing to clone, then return nothing. */
9006 if (orig == NULL)
9007 return NULL;
9008
a1aa2221 9009 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9010}
9011
611c83ae 9012struct breakpoint *
a6d9a66e
UW
9013set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9014 enum bptype type)
611c83ae
PA
9015{
9016 struct symtab_and_line sal;
9017
9018 sal = find_pc_line (pc, 0);
9019 sal.pc = pc;
9020 sal.section = find_pc_overlay (pc);
9021 sal.explicit_pc = 1;
9022
a6d9a66e 9023 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9024}
c906108c 9025\f
c5aa993b 9026
c906108c
SS
9027/* Tell the user we have just set a breakpoint B. */
9028
9029static void
fba45db2 9030mention (struct breakpoint *b)
c906108c 9031{
348d480f 9032 b->ops->print_mention (b);
79a45e25 9033 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9034 return;
c906108c
SS
9035 printf_filtered ("\n");
9036}
c906108c 9037\f
c5aa993b 9038
1a853c52
PA
9039static int bp_loc_is_permanent (struct bp_location *loc);
9040
0d381245 9041static struct bp_location *
39d61571 9042add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9043 const struct symtab_and_line *sal)
9044{
9045 struct bp_location *loc, **tmp;
3742cc8b
YQ
9046 CORE_ADDR adjusted_address;
9047 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9048
9049 if (loc_gdbarch == NULL)
9050 loc_gdbarch = b->gdbarch;
9051
9052 /* Adjust the breakpoint's address prior to allocating a location.
9053 Once we call allocate_bp_location(), that mostly uninitialized
9054 location will be placed on the location chain. Adjustment of the
9055 breakpoint may cause target_read_memory() to be called and we do
9056 not want its scan of the location chain to find a breakpoint and
9057 location that's only been partially initialized. */
9058 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9059 sal->pc, b->type);
0d381245 9060
d30113d4 9061 /* Sort the locations by their ADDRESS. */
39d61571 9062 loc = allocate_bp_location (b);
d30113d4
JK
9063 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9064 tmp = &((*tmp)->next))
0d381245 9065 ;
d30113d4 9066 loc->next = *tmp;
0d381245 9067 *tmp = loc;
3742cc8b 9068
0d381245 9069 loc->requested_address = sal->pc;
3742cc8b 9070 loc->address = adjusted_address;
6c95b8df 9071 loc->pspace = sal->pspace;
729662a5
TT
9072 loc->probe.probe = sal->probe;
9073 loc->probe.objfile = sal->objfile;
6c95b8df 9074 gdb_assert (loc->pspace != NULL);
0d381245 9075 loc->section = sal->section;
3742cc8b 9076 loc->gdbarch = loc_gdbarch;
f8eba3c6 9077 loc->line_number = sal->line;
2f202fde 9078 loc->symtab = sal->symtab;
f8eba3c6 9079
0e30163f
JK
9080 set_breakpoint_location_function (loc,
9081 sal->explicit_pc || sal->explicit_line);
1a853c52 9082
6ae88661
LM
9083 /* While by definition, permanent breakpoints are already present in the
9084 code, we don't mark the location as inserted. Normally one would expect
9085 that GDB could rely on that breakpoint instruction to stop the program,
9086 thus removing the need to insert its own breakpoint, except that executing
9087 the breakpoint instruction can kill the target instead of reporting a
9088 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9089 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9090 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9091 breakpoint be inserted normally results in QEMU knowing about the GDB
9092 breakpoint, and thus trap before the breakpoint instruction is executed.
9093 (If GDB later needs to continue execution past the permanent breakpoint,
9094 it manually increments the PC, thus avoiding executing the breakpoint
9095 instruction.) */
1a853c52 9096 if (bp_loc_is_permanent (loc))
6ae88661 9097 loc->permanent = 1;
1a853c52 9098
0d381245
VP
9099 return loc;
9100}
514f746b
AR
9101\f
9102
1cf4d951 9103/* See breakpoint.h. */
514f746b 9104
1cf4d951
PA
9105int
9106program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9107{
9108 int len;
9109 CORE_ADDR addr;
1afeeb75 9110 const gdb_byte *bpoint;
514f746b 9111 gdb_byte *target_mem;
939c61fa
JK
9112 struct cleanup *cleanup;
9113 int retval = 0;
514f746b 9114
1cf4d951
PA
9115 addr = address;
9116 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9117
9118 /* Software breakpoints unsupported? */
9119 if (bpoint == NULL)
9120 return 0;
9121
224c3ddb 9122 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9123
9124 /* Enable the automatic memory restoration from breakpoints while
9125 we read the memory. Otherwise we could say about our temporary
9126 breakpoints they are permanent. */
9127 cleanup = make_show_memory_breakpoints_cleanup (0);
9128
9129 if (target_read_memory (address, target_mem, len) == 0
9130 && memcmp (target_mem, bpoint, len) == 0)
9131 retval = 1;
9132
9133 do_cleanups (cleanup);
9134
9135 return retval;
9136}
9137
9138/* Return 1 if LOC is pointing to a permanent breakpoint,
9139 return 0 otherwise. */
9140
9141static int
9142bp_loc_is_permanent (struct bp_location *loc)
9143{
9144 struct cleanup *cleanup;
9145 int retval;
9146
514f746b
AR
9147 gdb_assert (loc != NULL);
9148
244558af
LM
9149 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9150 attempt to read from the addresses the locations of these breakpoint types
9151 point to. program_breakpoint_here_p, below, will attempt to read
9152 memory. */
9153 if (!breakpoint_address_is_meaningful (loc->owner))
9154 return 0;
9155
6c95b8df 9156 cleanup = save_current_space_and_thread ();
6c95b8df 9157 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9158
1cf4d951 9159 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9160
939c61fa
JK
9161 do_cleanups (cleanup);
9162
9163 return retval;
514f746b
AR
9164}
9165
e7e0cddf
SS
9166/* Build a command list for the dprintf corresponding to the current
9167 settings of the dprintf style options. */
9168
9169static void
9170update_dprintf_command_list (struct breakpoint *b)
9171{
9172 char *dprintf_args = b->extra_string;
9173 char *printf_line = NULL;
9174
9175 if (!dprintf_args)
9176 return;
9177
9178 dprintf_args = skip_spaces (dprintf_args);
9179
9180 /* Allow a comma, as it may have terminated a location, but don't
9181 insist on it. */
9182 if (*dprintf_args == ',')
9183 ++dprintf_args;
9184 dprintf_args = skip_spaces (dprintf_args);
9185
9186 if (*dprintf_args != '"')
9187 error (_("Bad format string, missing '\"'."));
9188
d3ce09f5 9189 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9190 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9191 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9192 {
9193 if (!dprintf_function)
9194 error (_("No function supplied for dprintf call"));
9195
9196 if (dprintf_channel && strlen (dprintf_channel) > 0)
9197 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9198 dprintf_function,
9199 dprintf_channel,
9200 dprintf_args);
9201 else
9202 printf_line = xstrprintf ("call (void) %s (%s)",
9203 dprintf_function,
9204 dprintf_args);
9205 }
d3ce09f5
SS
9206 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9207 {
9208 if (target_can_run_breakpoint_commands ())
9209 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9210 else
9211 {
9212 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9213 printf_line = xstrprintf ("printf %s", dprintf_args);
9214 }
9215 }
e7e0cddf
SS
9216 else
9217 internal_error (__FILE__, __LINE__,
9218 _("Invalid dprintf style."));
9219
f28045c2 9220 gdb_assert (printf_line != NULL);
9d6e6e84 9221 /* Manufacture a printf sequence. */
f28045c2 9222 {
8d749320 9223 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9224
f28045c2
YQ
9225 printf_cmd_line->control_type = simple_control;
9226 printf_cmd_line->body_count = 0;
9227 printf_cmd_line->body_list = NULL;
9d6e6e84 9228 printf_cmd_line->next = NULL;
f28045c2 9229 printf_cmd_line->line = printf_line;
e7e0cddf 9230
f28045c2
YQ
9231 breakpoint_set_commands (b, printf_cmd_line);
9232 }
e7e0cddf
SS
9233}
9234
9235/* Update all dprintf commands, making their command lists reflect
9236 current style settings. */
9237
9238static void
9239update_dprintf_commands (char *args, int from_tty,
9240 struct cmd_list_element *c)
9241{
9242 struct breakpoint *b;
9243
9244 ALL_BREAKPOINTS (b)
9245 {
9246 if (b->type == bp_dprintf)
9247 update_dprintf_command_list (b);
9248 }
9249}
c3f6f71d 9250
f00aae0f
KS
9251/* Create a breakpoint with SAL as location. Use LOCATION
9252 as a description of the location, and COND_STRING
b35a8b2f
DE
9253 as condition expression. If LOCATION is NULL then create an
9254 "address location" from the address in the SAL. */
018d34a4
VP
9255
9256static void
d9b3f62e 9257init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9258 struct symtabs_and_lines sals,
9259 struct event_location *location,
f8eba3c6 9260 char *filter, char *cond_string,
e7e0cddf 9261 char *extra_string,
d9b3f62e
PA
9262 enum bptype type, enum bpdisp disposition,
9263 int thread, int task, int ignore_count,
c0a91b2b 9264 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9265 int enabled, int internal, unsigned flags,
9266 int display_canonical)
018d34a4 9267{
0d381245 9268 int i;
018d34a4
VP
9269
9270 if (type == bp_hardware_breakpoint)
9271 {
fbbd034e
AS
9272 int target_resources_ok;
9273
9274 i = hw_breakpoint_used_count ();
9275 target_resources_ok =
9276 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9277 i + 1, 0);
9278 if (target_resources_ok == 0)
9279 error (_("No hardware breakpoint support in the target."));
9280 else if (target_resources_ok < 0)
9281 error (_("Hardware breakpoints used exceeds limit."));
9282 }
9283
6c95b8df
PA
9284 gdb_assert (sals.nelts > 0);
9285
0d381245
VP
9286 for (i = 0; i < sals.nelts; ++i)
9287 {
9288 struct symtab_and_line sal = sals.sals[i];
9289 struct bp_location *loc;
9290
9291 if (from_tty)
5af949e3
UW
9292 {
9293 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9294 if (!loc_gdbarch)
9295 loc_gdbarch = gdbarch;
9296
9297 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9298 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9299 }
0d381245
VP
9300
9301 if (i == 0)
9302 {
d9b3f62e 9303 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9304 b->thread = thread;
4a306c9a 9305 b->task = task;
855a6e68 9306
0d381245 9307 b->cond_string = cond_string;
e7e0cddf 9308 b->extra_string = extra_string;
0d381245 9309 b->ignore_count = ignore_count;
41447f92 9310 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9311 b->disposition = disposition;
6c95b8df 9312
44f238bb
PA
9313 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9314 b->loc->inserted = 1;
9315
0fb4aa4b
PA
9316 if (type == bp_static_tracepoint)
9317 {
d9b3f62e 9318 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9319 struct static_tracepoint_marker marker;
9320
983af33b 9321 if (strace_marker_p (b))
0fb4aa4b
PA
9322 {
9323 /* We already know the marker exists, otherwise, we
9324 wouldn't see a sal for it. */
f00aae0f
KS
9325 const char *p = &event_location_to_string (b->location)[3];
9326 const char *endp;
0fb4aa4b 9327 char *marker_str;
0fb4aa4b 9328
f00aae0f 9329 p = skip_spaces_const (p);
0fb4aa4b 9330
f00aae0f 9331 endp = skip_to_space_const (p);
0fb4aa4b
PA
9332
9333 marker_str = savestring (p, endp - p);
d9b3f62e 9334 t->static_trace_marker_id = marker_str;
0fb4aa4b 9335
3e43a32a
MS
9336 printf_filtered (_("Probed static tracepoint "
9337 "marker \"%s\"\n"),
d9b3f62e 9338 t->static_trace_marker_id);
0fb4aa4b
PA
9339 }
9340 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9341 {
d9b3f62e 9342 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9343 release_static_tracepoint_marker (&marker);
9344
3e43a32a
MS
9345 printf_filtered (_("Probed static tracepoint "
9346 "marker \"%s\"\n"),
d9b3f62e 9347 t->static_trace_marker_id);
0fb4aa4b
PA
9348 }
9349 else
3e43a32a
MS
9350 warning (_("Couldn't determine the static "
9351 "tracepoint marker to probe"));
0fb4aa4b
PA
9352 }
9353
0d381245
VP
9354 loc = b->loc;
9355 }
9356 else
018d34a4 9357 {
39d61571 9358 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9359 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9360 loc->inserted = 1;
0d381245
VP
9361 }
9362
9363 if (b->cond_string)
9364 {
bbc13ae3
KS
9365 const char *arg = b->cond_string;
9366
1bb9788d
TT
9367 loc->cond = parse_exp_1 (&arg, loc->address,
9368 block_for_pc (loc->address), 0);
0d381245 9369 if (*arg)
588ae58c 9370 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9371 }
e7e0cddf
SS
9372
9373 /* Dynamic printf requires and uses additional arguments on the
9374 command line, otherwise it's an error. */
9375 if (type == bp_dprintf)
9376 {
9377 if (b->extra_string)
9378 update_dprintf_command_list (b);
9379 else
9380 error (_("Format string required"));
9381 }
9382 else if (b->extra_string)
588ae58c 9383 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9384 }
018d34a4 9385
56435ebe 9386 b->display_canonical = display_canonical;
f00aae0f
KS
9387 if (location != NULL)
9388 b->location = location;
018d34a4 9389 else
305e13e6
JB
9390 {
9391 const char *addr_string = NULL;
9392 int addr_string_len = 0;
9393
9394 if (location != NULL)
9395 addr_string = event_location_to_string (location);
9396 if (addr_string != NULL)
9397 addr_string_len = strlen (addr_string);
9398
9399 b->location = new_address_location (b->loc->address,
9400 addr_string, addr_string_len);
9401 }
f8eba3c6 9402 b->filter = filter;
d9b3f62e 9403}
018d34a4 9404
d9b3f62e
PA
9405static void
9406create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9407 struct symtabs_and_lines sals,
9408 struct event_location *location,
f8eba3c6 9409 char *filter, char *cond_string,
e7e0cddf 9410 char *extra_string,
d9b3f62e
PA
9411 enum bptype type, enum bpdisp disposition,
9412 int thread, int task, int ignore_count,
c0a91b2b 9413 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9414 int enabled, int internal, unsigned flags,
9415 int display_canonical)
d9b3f62e
PA
9416{
9417 struct breakpoint *b;
9418 struct cleanup *old_chain;
9419
9420 if (is_tracepoint_type (type))
9421 {
9422 struct tracepoint *t;
9423
9424 t = XCNEW (struct tracepoint);
9425 b = &t->base;
9426 }
9427 else
9428 b = XNEW (struct breakpoint);
9429
9430 old_chain = make_cleanup (xfree, b);
9431
9432 init_breakpoint_sal (b, gdbarch,
f00aae0f 9433 sals, location,
e7e0cddf 9434 filter, cond_string, extra_string,
d9b3f62e
PA
9435 type, disposition,
9436 thread, task, ignore_count,
9437 ops, from_tty,
44f238bb
PA
9438 enabled, internal, flags,
9439 display_canonical);
d9b3f62e
PA
9440 discard_cleanups (old_chain);
9441
3ea46bff 9442 install_breakpoint (internal, b, 0);
018d34a4
VP
9443}
9444
9445/* Add SALS.nelts breakpoints to the breakpoint table. For each
9446 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9447 value. COND_STRING, if not NULL, specified the condition to be
9448 used for all breakpoints. Essentially the only case where
9449 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9450 function. In that case, it's still not possible to specify
9451 separate conditions for different overloaded functions, so
9452 we take just a single condition string.
9453
c3f6f71d 9454 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9455 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9456 array contents). If the function fails (error() is called), the
9457 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9458 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9459
9460static void
8cdf0e15 9461create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9462 struct linespec_result *canonical,
e7e0cddf 9463 char *cond_string, char *extra_string,
8cdf0e15
VP
9464 enum bptype type, enum bpdisp disposition,
9465 int thread, int task, int ignore_count,
c0a91b2b 9466 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9467 int enabled, int internal, unsigned flags)
c906108c 9468{
018d34a4 9469 int i;
f8eba3c6 9470 struct linespec_sals *lsal;
cc59ec59 9471
f8eba3c6
TT
9472 if (canonical->pre_expanded)
9473 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9474
9475 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9476 {
f00aae0f 9477 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9478 'break', without arguments. */
f00aae0f
KS
9479 struct event_location *location
9480 = (canonical->location != NULL
9481 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9482 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9483 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9484
f8eba3c6
TT
9485 make_cleanup (xfree, filter_string);
9486 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9487 location,
f8eba3c6 9488 filter_string,
e7e0cddf
SS
9489 cond_string, extra_string,
9490 type, disposition,
84f4c1fe 9491 thread, task, ignore_count, ops,
44f238bb 9492 from_tty, enabled, internal, flags,
56435ebe 9493 canonical->special_display);
f8eba3c6 9494 discard_cleanups (inner);
c3f6f71d 9495 }
c3f6f71d 9496}
c906108c 9497
f00aae0f 9498/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9499 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9500 addresses found. LOCATION points to the end of the SAL (for
9501 linespec locations).
9998af43
TJB
9502
9503 The array and the line spec strings are allocated on the heap, it is
9504 the caller's responsibility to free them. */
c906108c 9505
b9362cc7 9506static void
f00aae0f 9507parse_breakpoint_sals (const struct event_location *location,
58438ac1 9508 struct linespec_result *canonical)
c3f6f71d 9509{
f00aae0f
KS
9510 struct symtab_and_line cursal;
9511
9512 if (event_location_type (location) == LINESPEC_LOCATION)
9513 {
9514 const char *address = get_linespec_location (location);
9515
9516 if (address == NULL)
9517 {
9518 /* The last displayed codepoint, if it's valid, is our default
9519 breakpoint address. */
9520 if (last_displayed_sal_is_valid ())
9521 {
9522 struct linespec_sals lsal;
9523 struct symtab_and_line sal;
9524 CORE_ADDR pc;
9525
9526 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9527 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9528
9529 /* Set sal's pspace, pc, symtab, and line to the values
9530 corresponding to the last call to print_frame_info.
9531 Be sure to reinitialize LINE with NOTCURRENT == 0
9532 as the breakpoint line number is inappropriate otherwise.
9533 find_pc_line would adjust PC, re-set it back. */
9534 get_last_displayed_sal (&sal);
9535 pc = sal.pc;
9536 sal = find_pc_line (pc, 0);
9537
9538 /* "break" without arguments is equivalent to "break *PC"
9539 where PC is the last displayed codepoint's address. So
9540 make sure to set sal.explicit_pc to prevent GDB from
9541 trying to expand the list of sals to include all other
9542 instances with the same symtab and line. */
9543 sal.pc = pc;
9544 sal.explicit_pc = 1;
9545
9546 lsal.sals.sals[0] = sal;
9547 lsal.sals.nelts = 1;
9548 lsal.canonical = NULL;
9549
9550 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9551 return;
9552 }
9553 else
9554 error (_("No default breakpoint address now."));
c906108c 9555 }
c906108c 9556 }
f00aae0f
KS
9557
9558 /* Force almost all breakpoints to be in terms of the
9559 current_source_symtab (which is decode_line_1's default).
9560 This should produce the results we want almost all of the
9561 time while leaving default_breakpoint_* alone.
9562
9563 ObjC: However, don't match an Objective-C method name which
9564 may have a '+' or '-' succeeded by a '['. */
9565 cursal = get_current_source_symtab_and_line ();
9566 if (last_displayed_sal_is_valid ())
c906108c 9567 {
f00aae0f 9568 const char *address = NULL;
cc80f267 9569
f00aae0f
KS
9570 if (event_location_type (location) == LINESPEC_LOCATION)
9571 address = get_linespec_location (location);
cc80f267 9572
f00aae0f
KS
9573 if (!cursal.symtab
9574 || (address != NULL
9575 && strchr ("+-", address[0]) != NULL
9576 && address[1] != '['))
9577 {
c2f4122d 9578 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9579 get_last_displayed_symtab (),
9580 get_last_displayed_line (),
9581 canonical, NULL, NULL);
9582 return;
9583 }
c906108c 9584 }
f00aae0f 9585
c2f4122d 9586 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9587 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9588}
c906108c 9589
c906108c 9590
c3f6f71d 9591/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9592 inserted as a breakpoint. If it can't throw an error. */
c906108c 9593
b9362cc7 9594static void
23e7acfb 9595breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9596{
9597 int i;
cc59ec59 9598
c3f6f71d 9599 for (i = 0; i < sals->nelts; i++)
ee53e872 9600 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9601}
9602
7a697b8d
SS
9603/* Fast tracepoints may have restrictions on valid locations. For
9604 instance, a fast tracepoint using a jump instead of a trap will
9605 likely have to overwrite more bytes than a trap would, and so can
9606 only be placed where the instruction is longer than the jump, or a
9607 multi-instruction sequence does not have a jump into the middle of
9608 it, etc. */
9609
9610static void
9611check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9612 struct symtabs_and_lines *sals)
9613{
9614 int i, rslt;
9615 struct symtab_and_line *sal;
9616 char *msg;
9617 struct cleanup *old_chain;
9618
9619 for (i = 0; i < sals->nelts; i++)
9620 {
f8eba3c6
TT
9621 struct gdbarch *sarch;
9622
7a697b8d
SS
9623 sal = &sals->sals[i];
9624
f8eba3c6
TT
9625 sarch = get_sal_arch (*sal);
9626 /* We fall back to GDBARCH if there is no architecture
9627 associated with SAL. */
9628 if (sarch == NULL)
9629 sarch = gdbarch;
6b940e6a 9630 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9631 old_chain = make_cleanup (xfree, msg);
9632
9633 if (!rslt)
9634 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9635 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9636
9637 do_cleanups (old_chain);
9638 }
9639}
9640
018d34a4
VP
9641/* Given TOK, a string specification of condition and thread, as
9642 accepted by the 'break' command, extract the condition
9643 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9644 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9645 If no condition is found, *COND_STRING is set to NULL.
9646 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9647
9648static void
bbc13ae3 9649find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9650 char **cond_string, int *thread, int *task,
9651 char **rest)
018d34a4
VP
9652{
9653 *cond_string = NULL;
9654 *thread = -1;
ed1d1739
KS
9655 *task = 0;
9656 *rest = NULL;
9657
018d34a4
VP
9658 while (tok && *tok)
9659 {
bbc13ae3 9660 const char *end_tok;
018d34a4 9661 int toklen;
bbc13ae3
KS
9662 const char *cond_start = NULL;
9663 const char *cond_end = NULL;
cc59ec59 9664
bbc13ae3 9665 tok = skip_spaces_const (tok);
e7e0cddf
SS
9666
9667 if ((*tok == '"' || *tok == ',') && rest)
9668 {
9669 *rest = savestring (tok, strlen (tok));
9670 return;
9671 }
9672
bbc13ae3 9673 end_tok = skip_to_space_const (tok);
d634f2de 9674
018d34a4 9675 toklen = end_tok - tok;
d634f2de 9676
018d34a4
VP
9677 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9678 {
f7545552
TT
9679 struct expression *expr;
9680
018d34a4 9681 tok = cond_start = end_tok + 1;
1bb9788d 9682 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9683 xfree (expr);
018d34a4 9684 cond_end = tok;
d634f2de 9685 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9686 }
9687 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9688 {
5d5658a1
PA
9689 const char *tmptok;
9690 struct thread_info *thr;
d634f2de 9691
018d34a4 9692 tok = end_tok + 1;
5d5658a1 9693 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9694 if (tok == tmptok)
9695 error (_("Junk after thread keyword."));
5d5658a1 9696 *thread = thr->global_num;
bbc13ae3 9697 tok = tmptok;
018d34a4 9698 }
4a306c9a
JB
9699 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9700 {
9701 char *tmptok;
9702
9703 tok = end_tok + 1;
bbc13ae3 9704 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9705 if (tok == tmptok)
9706 error (_("Junk after task keyword."));
9707 if (!valid_task_id (*task))
b6199126 9708 error (_("Unknown task %d."), *task);
bbc13ae3 9709 tok = tmptok;
4a306c9a 9710 }
e7e0cddf
SS
9711 else if (rest)
9712 {
9713 *rest = savestring (tok, strlen (tok));
ccab2054 9714 return;
e7e0cddf 9715 }
018d34a4
VP
9716 else
9717 error (_("Junk at end of arguments."));
9718 }
9719}
9720
0fb4aa4b
PA
9721/* Decode a static tracepoint marker spec. */
9722
9723static struct symtabs_and_lines
f00aae0f 9724decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9725{
9726 VEC(static_tracepoint_marker_p) *markers = NULL;
9727 struct symtabs_and_lines sals;
0fb4aa4b 9728 struct cleanup *old_chain;
f00aae0f
KS
9729 const char *p = &(*arg_p)[3];
9730 const char *endp;
0fb4aa4b
PA
9731 char *marker_str;
9732 int i;
9733
f00aae0f 9734 p = skip_spaces_const (p);
0fb4aa4b 9735
f00aae0f 9736 endp = skip_to_space_const (p);
0fb4aa4b
PA
9737
9738 marker_str = savestring (p, endp - p);
9739 old_chain = make_cleanup (xfree, marker_str);
9740
9741 markers = target_static_tracepoint_markers_by_strid (marker_str);
9742 if (VEC_empty(static_tracepoint_marker_p, markers))
9743 error (_("No known static tracepoint marker named %s"), marker_str);
9744
9745 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9746 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9747
9748 for (i = 0; i < sals.nelts; i++)
9749 {
9750 struct static_tracepoint_marker *marker;
9751
9752 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9753
9754 init_sal (&sals.sals[i]);
9755
9756 sals.sals[i] = find_pc_line (marker->address, 0);
9757 sals.sals[i].pc = marker->address;
9758
9759 release_static_tracepoint_marker (marker);
9760 }
9761
9762 do_cleanups (old_chain);
9763
9764 *arg_p = endp;
9765 return sals;
9766}
9767
f00aae0f 9768/* See breakpoint.h. */
0101ce28 9769
8cdf0e15
VP
9770int
9771create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9772 const struct event_location *location, char *cond_string,
e7e0cddf 9773 int thread, char *extra_string,
f00aae0f 9774 int parse_extra,
0fb4aa4b 9775 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9776 int ignore_count,
9777 enum auto_boolean pending_break_support,
c0a91b2b 9778 const struct breakpoint_ops *ops,
44f238bb
PA
9779 int from_tty, int enabled, int internal,
9780 unsigned flags)
c3f6f71d 9781{
7efd8fc2 9782 struct linespec_result canonical;
c3f6f71d 9783 struct cleanup *old_chain;
80c99de1 9784 struct cleanup *bkpt_chain = NULL;
0101ce28 9785 int pending = 0;
4a306c9a 9786 int task = 0;
86b17b60 9787 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9788
348d480f
PA
9789 gdb_assert (ops != NULL);
9790
f00aae0f
KS
9791 /* If extra_string isn't useful, set it to NULL. */
9792 if (extra_string != NULL && *extra_string == '\0')
9793 extra_string = NULL;
9794
7efd8fc2 9795 init_linespec_result (&canonical);
c3f6f71d 9796
492d29ea 9797 TRY
b78a6381 9798 {
f00aae0f 9799 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9800 }
492d29ea 9801 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9802 {
492d29ea
PA
9803 /* If caller is interested in rc value from parse, set
9804 value. */
9805 if (e.error == NOT_FOUND_ERROR)
0101ce28 9806 {
05ff989b
AC
9807 /* If pending breakpoint support is turned off, throw
9808 error. */
fa8d40ab
JJ
9809
9810 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9811 throw_exception (e);
9812
9813 exception_print (gdb_stderr, e);
fa8d40ab 9814
05ff989b
AC
9815 /* If pending breakpoint support is auto query and the user
9816 selects no, then simply return the error code. */
059fb39f 9817 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9818 && !nquery (_("Make %s pending on future shared library load? "),
9819 bptype_string (type_wanted)))
fd9b8c24 9820 return 0;
fa8d40ab 9821
05ff989b
AC
9822 /* At this point, either the user was queried about setting
9823 a pending breakpoint and selected yes, or pending
9824 breakpoint behavior is on and thus a pending breakpoint
9825 is defaulted on behalf of the user. */
f00aae0f 9826 pending = 1;
0101ce28 9827 }
492d29ea
PA
9828 else
9829 throw_exception (e);
0101ce28 9830 }
492d29ea
PA
9831 END_CATCH
9832
f00aae0f 9833 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9834 return 0;
c3f6f71d 9835
4a64f543 9836 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9837 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9838
c3f6f71d
JM
9839 /* ----------------------------- SNIP -----------------------------
9840 Anything added to the cleanup chain beyond this point is assumed
9841 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9842 then the memory is not reclaimed. */
9843 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9844
c3f6f71d
JM
9845 /* Resolve all line numbers to PC's and verify that the addresses
9846 are ok for the target. */
0101ce28 9847 if (!pending)
f8eba3c6
TT
9848 {
9849 int ix;
9850 struct linespec_sals *iter;
9851
9852 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9853 breakpoint_sals_to_pc (&iter->sals);
9854 }
c3f6f71d 9855
7a697b8d 9856 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9857 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9858 {
9859 int ix;
9860 struct linespec_sals *iter;
9861
9862 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9863 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9864 }
7a697b8d 9865
c3f6f71d
JM
9866 /* Verify that condition can be parsed, before setting any
9867 breakpoints. Allocate a separate condition expression for each
4a64f543 9868 breakpoint. */
0101ce28 9869 if (!pending)
c3f6f71d 9870 {
f00aae0f 9871 if (parse_extra)
72b2ff0e 9872 {
0878d0fa 9873 char *rest;
52d361e1
YQ
9874 struct linespec_sals *lsal;
9875
9876 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9877
0878d0fa
YQ
9878 /* Here we only parse 'arg' to separate condition
9879 from thread number, so parsing in context of first
9880 sal is OK. When setting the breakpoint we'll
9881 re-parse it in context of each sal. */
9882
f00aae0f
KS
9883 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9884 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9885 if (cond_string)
9886 make_cleanup (xfree, cond_string);
9887 if (rest)
9888 make_cleanup (xfree, rest);
9889 if (rest)
9890 extra_string = rest;
f00aae0f
KS
9891 else
9892 extra_string = NULL;
72b2ff0e 9893 }
2f069f6f 9894 else
72b2ff0e 9895 {
f00aae0f
KS
9896 if (type_wanted != bp_dprintf
9897 && extra_string != NULL && *extra_string != '\0')
9898 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9899
9900 /* Create a private copy of condition string. */
9901 if (cond_string)
9902 {
9903 cond_string = xstrdup (cond_string);
9904 make_cleanup (xfree, cond_string);
9905 }
9906 /* Create a private copy of any extra string. */
9907 if (extra_string)
9908 {
9909 extra_string = xstrdup (extra_string);
9910 make_cleanup (xfree, extra_string);
9911 }
72b2ff0e 9912 }
0fb4aa4b 9913
52d361e1 9914 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9915 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9916 tempflag ? disp_del : disp_donttouch,
9917 thread, task, ignore_count, ops,
44f238bb 9918 from_tty, enabled, internal, flags);
c906108c 9919 }
0101ce28
JJ
9920 else
9921 {
0101ce28
JJ
9922 struct breakpoint *b;
9923
bfccc43c
YQ
9924 if (is_tracepoint_type (type_wanted))
9925 {
9926 struct tracepoint *t;
9927
9928 t = XCNEW (struct tracepoint);
9929 b = &t->base;
9930 }
9931 else
9932 b = XNEW (struct breakpoint);
9933
9934 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9935 b->location = copy_event_location (location);
bfccc43c 9936
f00aae0f
KS
9937 if (parse_extra)
9938 b->cond_string = NULL;
e12c7713
MK
9939 else
9940 {
9941 /* Create a private copy of condition string. */
9942 if (cond_string)
9943 {
9944 cond_string = xstrdup (cond_string);
9945 make_cleanup (xfree, cond_string);
9946 }
9947 b->cond_string = cond_string;
15630549 9948 b->thread = thread;
e12c7713 9949 }
f00aae0f
KS
9950
9951 /* Create a private copy of any extra string. */
9952 if (extra_string != NULL)
9953 {
9954 extra_string = xstrdup (extra_string);
9955 make_cleanup (xfree, extra_string);
9956 }
9957 b->extra_string = extra_string;
0101ce28 9958 b->ignore_count = ignore_count;
0101ce28 9959 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9960 b->condition_not_parsed = 1;
41447f92 9961 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9962 if ((type_wanted != bp_breakpoint
9963 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9964 b->pspace = current_program_space;
8bea4e01 9965
bfccc43c 9966 install_breakpoint (internal, b, 0);
0101ce28
JJ
9967 }
9968
f8eba3c6 9969 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9970 {
3e43a32a
MS
9971 warning (_("Multiple breakpoints were set.\nUse the "
9972 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9973 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9974 }
9975
80c99de1
PA
9976 /* That's it. Discard the cleanups for data inserted into the
9977 breakpoint. */
9978 discard_cleanups (bkpt_chain);
9979 /* But cleanup everything else. */
c3f6f71d 9980 do_cleanups (old_chain);
217dc9e2 9981
80c99de1 9982 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9983 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9984
9985 return 1;
c3f6f71d 9986}
c906108c 9987
348d480f 9988/* Set a breakpoint.
72b2ff0e
VP
9989 ARG is a string describing breakpoint address,
9990 condition, and thread.
9991 FLAG specifies if a breakpoint is hardware on,
9992 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9993 and BP_TEMPFLAG. */
348d480f 9994
98deb0da 9995static void
72b2ff0e 9996break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9997{
72b2ff0e 9998 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9999 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10000 ? bp_hardware_breakpoint
10001 : bp_breakpoint);
55aa24fb 10002 struct breakpoint_ops *ops;
f00aae0f
KS
10003 struct event_location *location;
10004 struct cleanup *cleanup;
10005
10006 location = string_to_event_location (&arg, current_language);
10007 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
10008
10009 /* Matching breakpoints on probes. */
5b56227b
KS
10010 if (location != NULL
10011 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
10012 ops = &bkpt_probe_breakpoint_ops;
10013 else
10014 ops = &bkpt_breakpoint_ops;
c3f6f71d 10015
8cdf0e15 10016 create_breakpoint (get_current_arch (),
f00aae0f
KS
10017 location,
10018 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 10019 tempflag, type_wanted,
8cdf0e15
VP
10020 0 /* Ignore count */,
10021 pending_break_support,
55aa24fb 10022 ops,
8cdf0e15 10023 from_tty,
84f4c1fe 10024 1 /* enabled */,
44f238bb
PA
10025 0 /* internal */,
10026 0);
f00aae0f 10027 do_cleanups (cleanup);
c906108c
SS
10028}
10029
c906108c
SS
10030/* Helper function for break_command_1 and disassemble_command. */
10031
10032void
fba45db2 10033resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10034{
10035 CORE_ADDR pc;
10036
10037 if (sal->pc == 0 && sal->symtab != NULL)
10038 {
10039 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10040 error (_("No line %d in file \"%s\"."),
05cba821 10041 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10042 sal->pc = pc;
6a048695 10043
4a64f543
MS
10044 /* If this SAL corresponds to a breakpoint inserted using a line
10045 number, then skip the function prologue if necessary. */
6a048695 10046 if (sal->explicit_line)
059acae7 10047 skip_prologue_sal (sal);
c906108c
SS
10048 }
10049
10050 if (sal->section == 0 && sal->symtab != NULL)
10051 {
346d1dfe 10052 const struct blockvector *bv;
3977b71f 10053 const struct block *b;
c5aa993b 10054 struct symbol *sym;
c906108c 10055
43f3e411
DE
10056 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10057 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10058 if (bv != NULL)
10059 {
7f0df278 10060 sym = block_linkage_function (b);
c906108c
SS
10061 if (sym != NULL)
10062 {
eb822aa6
DE
10063 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10064 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10065 sym);
c906108c
SS
10066 }
10067 else
10068 {
4a64f543
MS
10069 /* It really is worthwhile to have the section, so we'll
10070 just have to look harder. This case can be executed
10071 if we have line numbers but no functions (as can
10072 happen in assembly source). */
c906108c 10073
7cbd4a93 10074 struct bound_minimal_symbol msym;
6c95b8df
PA
10075 struct cleanup *old_chain = save_current_space_and_thread ();
10076
10077 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10078
10079 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10080 if (msym.minsym)
efd66ac6 10081 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10082
10083 do_cleanups (old_chain);
c906108c
SS
10084 }
10085 }
10086 }
10087}
10088
10089void
fba45db2 10090break_command (char *arg, int from_tty)
c906108c 10091{
db107f19 10092 break_command_1 (arg, 0, from_tty);
c906108c
SS
10093}
10094
c906108c 10095void
fba45db2 10096tbreak_command (char *arg, int from_tty)
c906108c 10097{
db107f19 10098 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10099}
10100
c906108c 10101static void
fba45db2 10102hbreak_command (char *arg, int from_tty)
c906108c 10103{
db107f19 10104 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10105}
10106
10107static void
fba45db2 10108thbreak_command (char *arg, int from_tty)
c906108c 10109{
db107f19 10110 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10111}
10112
10113static void
fba45db2 10114stop_command (char *arg, int from_tty)
c906108c 10115{
a3f17187 10116 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10117Usage: stop in <function | address>\n\
a3f17187 10118 stop at <line>\n"));
c906108c
SS
10119}
10120
10121static void
fba45db2 10122stopin_command (char *arg, int from_tty)
c906108c
SS
10123{
10124 int badInput = 0;
10125
c5aa993b 10126 if (arg == (char *) NULL)
c906108c
SS
10127 badInput = 1;
10128 else if (*arg != '*')
10129 {
10130 char *argptr = arg;
10131 int hasColon = 0;
10132
4a64f543 10133 /* Look for a ':'. If this is a line number specification, then
53a5351d 10134 say it is bad, otherwise, it should be an address or
4a64f543 10135 function/method name. */
c906108c 10136 while (*argptr && !hasColon)
c5aa993b
JM
10137 {
10138 hasColon = (*argptr == ':');
10139 argptr++;
10140 }
c906108c
SS
10141
10142 if (hasColon)
c5aa993b 10143 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10144 else
c5aa993b 10145 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10146 }
10147
10148 if (badInput)
a3f17187 10149 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10150 else
db107f19 10151 break_command_1 (arg, 0, from_tty);
c906108c
SS
10152}
10153
10154static void
fba45db2 10155stopat_command (char *arg, int from_tty)
c906108c
SS
10156{
10157 int badInput = 0;
10158
c5aa993b 10159 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10160 badInput = 1;
10161 else
10162 {
10163 char *argptr = arg;
10164 int hasColon = 0;
10165
4a64f543
MS
10166 /* Look for a ':'. If there is a '::' then get out, otherwise
10167 it is probably a line number. */
c906108c 10168 while (*argptr && !hasColon)
c5aa993b
JM
10169 {
10170 hasColon = (*argptr == ':');
10171 argptr++;
10172 }
c906108c
SS
10173
10174 if (hasColon)
c5aa993b 10175 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10176 else
c5aa993b 10177 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10178 }
10179
10180 if (badInput)
a3f17187 10181 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10182 else
db107f19 10183 break_command_1 (arg, 0, from_tty);
c906108c
SS
10184}
10185
e7e0cddf
SS
10186/* The dynamic printf command is mostly like a regular breakpoint, but
10187 with a prewired command list consisting of a single output command,
10188 built from extra arguments supplied on the dprintf command
10189 line. */
10190
da821c7b 10191static void
e7e0cddf
SS
10192dprintf_command (char *arg, int from_tty)
10193{
f00aae0f
KS
10194 struct event_location *location;
10195 struct cleanup *cleanup;
10196
10197 location = string_to_event_location (&arg, current_language);
10198 cleanup = make_cleanup_delete_event_location (location);
10199
10200 /* If non-NULL, ARG should have been advanced past the location;
10201 the next character must be ','. */
10202 if (arg != NULL)
10203 {
10204 if (arg[0] != ',' || arg[1] == '\0')
10205 error (_("Format string required"));
10206 else
10207 {
10208 /* Skip the comma. */
10209 ++arg;
10210 }
10211 }
10212
e7e0cddf 10213 create_breakpoint (get_current_arch (),
f00aae0f
KS
10214 location,
10215 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10216 0, bp_dprintf,
10217 0 /* Ignore count */,
10218 pending_break_support,
10219 &dprintf_breakpoint_ops,
10220 from_tty,
10221 1 /* enabled */,
10222 0 /* internal */,
10223 0);
f00aae0f 10224 do_cleanups (cleanup);
e7e0cddf
SS
10225}
10226
d3ce09f5
SS
10227static void
10228agent_printf_command (char *arg, int from_tty)
10229{
10230 error (_("May only run agent-printf on the target"));
10231}
10232
f1310107
TJB
10233/* Implement the "breakpoint_hit" breakpoint_ops method for
10234 ranged breakpoints. */
10235
10236static int
10237breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10238 struct address_space *aspace,
09ac7c10
TT
10239 CORE_ADDR bp_addr,
10240 const struct target_waitstatus *ws)
f1310107 10241{
09ac7c10 10242 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10243 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10244 return 0;
10245
f1310107
TJB
10246 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10247 bl->length, aspace, bp_addr);
10248}
10249
10250/* Implement the "resources_needed" breakpoint_ops method for
10251 ranged breakpoints. */
10252
10253static int
10254resources_needed_ranged_breakpoint (const struct bp_location *bl)
10255{
10256 return target_ranged_break_num_registers ();
10257}
10258
10259/* Implement the "print_it" breakpoint_ops method for
10260 ranged breakpoints. */
10261
10262static enum print_stop_action
348d480f 10263print_it_ranged_breakpoint (bpstat bs)
f1310107 10264{
348d480f 10265 struct breakpoint *b = bs->breakpoint_at;
f1310107 10266 struct bp_location *bl = b->loc;
79a45e25 10267 struct ui_out *uiout = current_uiout;
f1310107
TJB
10268
10269 gdb_assert (b->type == bp_hardware_breakpoint);
10270
10271 /* Ranged breakpoints have only one location. */
10272 gdb_assert (bl && bl->next == NULL);
10273
10274 annotate_breakpoint (b->number);
f303dbd6
PA
10275
10276 maybe_print_thread_hit_breakpoint (uiout);
10277
f1310107 10278 if (b->disposition == disp_del)
f303dbd6 10279 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10280 else
f303dbd6 10281 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10282 if (ui_out_is_mi_like_p (uiout))
10283 {
10284 ui_out_field_string (uiout, "reason",
10285 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10286 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10287 }
10288 ui_out_field_int (uiout, "bkptno", b->number);
10289 ui_out_text (uiout, ", ");
10290
10291 return PRINT_SRC_AND_LOC;
10292}
10293
10294/* Implement the "print_one" breakpoint_ops method for
10295 ranged breakpoints. */
10296
10297static void
10298print_one_ranged_breakpoint (struct breakpoint *b,
10299 struct bp_location **last_loc)
10300{
10301 struct bp_location *bl = b->loc;
10302 struct value_print_options opts;
79a45e25 10303 struct ui_out *uiout = current_uiout;
f1310107
TJB
10304
10305 /* Ranged breakpoints have only one location. */
10306 gdb_assert (bl && bl->next == NULL);
10307
10308 get_user_print_options (&opts);
10309
10310 if (opts.addressprint)
10311 /* We don't print the address range here, it will be printed later
10312 by print_one_detail_ranged_breakpoint. */
10313 ui_out_field_skip (uiout, "addr");
10314 annotate_field (5);
10315 print_breakpoint_location (b, bl);
10316 *last_loc = bl;
10317}
10318
10319/* Implement the "print_one_detail" breakpoint_ops method for
10320 ranged breakpoints. */
10321
10322static void
10323print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10324 struct ui_out *uiout)
10325{
10326 CORE_ADDR address_start, address_end;
10327 struct bp_location *bl = b->loc;
f99d8bf4
PA
10328 struct ui_file *stb = mem_fileopen ();
10329 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10330
10331 gdb_assert (bl);
10332
10333 address_start = bl->address;
10334 address_end = address_start + bl->length - 1;
10335
10336 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10337 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10338 print_core_address (bl->gdbarch, address_start),
10339 print_core_address (bl->gdbarch, address_end));
10340 ui_out_field_stream (uiout, "addr", stb);
10341 ui_out_text (uiout, "\n");
10342
10343 do_cleanups (cleanup);
10344}
10345
10346/* Implement the "print_mention" breakpoint_ops method for
10347 ranged breakpoints. */
10348
10349static void
10350print_mention_ranged_breakpoint (struct breakpoint *b)
10351{
10352 struct bp_location *bl = b->loc;
79a45e25 10353 struct ui_out *uiout = current_uiout;
f1310107
TJB
10354
10355 gdb_assert (bl);
10356 gdb_assert (b->type == bp_hardware_breakpoint);
10357
10358 if (ui_out_is_mi_like_p (uiout))
10359 return;
10360
10361 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10362 b->number, paddress (bl->gdbarch, bl->address),
10363 paddress (bl->gdbarch, bl->address + bl->length - 1));
10364}
10365
10366/* Implement the "print_recreate" breakpoint_ops method for
10367 ranged breakpoints. */
10368
10369static void
10370print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10371{
f00aae0f
KS
10372 fprintf_unfiltered (fp, "break-range %s, %s",
10373 event_location_to_string (b->location),
10374 event_location_to_string (b->location_range_end));
d9b3f62e 10375 print_recreate_thread (b, fp);
f1310107
TJB
10376}
10377
10378/* The breakpoint_ops structure to be used in ranged breakpoints. */
10379
2060206e 10380static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10381
10382/* Find the address where the end of the breakpoint range should be
10383 placed, given the SAL of the end of the range. This is so that if
10384 the user provides a line number, the end of the range is set to the
10385 last instruction of the given line. */
10386
10387static CORE_ADDR
10388find_breakpoint_range_end (struct symtab_and_line sal)
10389{
10390 CORE_ADDR end;
10391
10392 /* If the user provided a PC value, use it. Otherwise,
10393 find the address of the end of the given location. */
10394 if (sal.explicit_pc)
10395 end = sal.pc;
10396 else
10397 {
10398 int ret;
10399 CORE_ADDR start;
10400
10401 ret = find_line_pc_range (sal, &start, &end);
10402 if (!ret)
10403 error (_("Could not find location of the end of the range."));
10404
10405 /* find_line_pc_range returns the start of the next line. */
10406 end--;
10407 }
10408
10409 return end;
10410}
10411
10412/* Implement the "break-range" CLI command. */
10413
10414static void
10415break_range_command (char *arg, int from_tty)
10416{
870f88f7 10417 char *arg_start, *addr_string_start;
f1310107
TJB
10418 struct linespec_result canonical_start, canonical_end;
10419 int bp_count, can_use_bp, length;
10420 CORE_ADDR end;
10421 struct breakpoint *b;
10422 struct symtab_and_line sal_start, sal_end;
f1310107 10423 struct cleanup *cleanup_bkpt;
f8eba3c6 10424 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10425 struct event_location *start_location, *end_location;
f1310107
TJB
10426
10427 /* We don't support software ranged breakpoints. */
10428 if (target_ranged_break_num_registers () < 0)
10429 error (_("This target does not support hardware ranged breakpoints."));
10430
10431 bp_count = hw_breakpoint_used_count ();
10432 bp_count += target_ranged_break_num_registers ();
10433 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10434 bp_count, 0);
10435 if (can_use_bp < 0)
10436 error (_("Hardware breakpoints used exceeds limit."));
10437
f8eba3c6 10438 arg = skip_spaces (arg);
f1310107
TJB
10439 if (arg == NULL || arg[0] == '\0')
10440 error(_("No address range specified."));
10441
f1310107
TJB
10442 init_linespec_result (&canonical_start);
10443
f8eba3c6 10444 arg_start = arg;
f00aae0f
KS
10445 start_location = string_to_event_location (&arg, current_language);
10446 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10447 parse_breakpoint_sals (start_location, &canonical_start);
10448 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10449
10450 if (arg[0] != ',')
10451 error (_("Too few arguments."));
f8eba3c6 10452 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10453 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10454
10455 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10456
10457 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10458 || lsal_start->sals.nelts != 1)
f1310107
TJB
10459 error (_("Cannot create a ranged breakpoint with multiple locations."));
10460
f8eba3c6
TT
10461 sal_start = lsal_start->sals.sals[0];
10462 addr_string_start = savestring (arg_start, arg - arg_start);
10463 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10464
10465 arg++; /* Skip the comma. */
f8eba3c6 10466 arg = skip_spaces (arg);
f1310107
TJB
10467
10468 /* Parse the end location. */
10469
f1310107
TJB
10470 init_linespec_result (&canonical_end);
10471 arg_start = arg;
10472
f8eba3c6 10473 /* We call decode_line_full directly here instead of using
f1310107
TJB
10474 parse_breakpoint_sals because we need to specify the start location's
10475 symtab and line as the default symtab and line for the end of the
10476 range. This makes it possible to have ranges like "foo.c:27, +14",
10477 where +14 means 14 lines from the start location. */
f00aae0f
KS
10478 end_location = string_to_event_location (&arg, current_language);
10479 make_cleanup_delete_event_location (end_location);
c2f4122d 10480 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10481 sal_start.symtab, sal_start.line,
10482 &canonical_end, NULL, NULL);
10483
10484 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10485
f8eba3c6 10486 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10487 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10488
10489 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10490 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10491 || lsal_end->sals.nelts != 1)
f1310107
TJB
10492 error (_("Cannot create a ranged breakpoint with multiple locations."));
10493
f8eba3c6 10494 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10495
10496 end = find_breakpoint_range_end (sal_end);
10497 if (sal_start.pc > end)
177b42fe 10498 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10499
10500 length = end - sal_start.pc + 1;
10501 if (length < 0)
10502 /* Length overflowed. */
10503 error (_("Address range too large."));
10504 else if (length == 1)
10505 {
10506 /* This range is simple enough to be handled by
10507 the `hbreak' command. */
10508 hbreak_command (addr_string_start, 1);
10509
10510 do_cleanups (cleanup_bkpt);
10511
10512 return;
10513 }
10514
10515 /* Now set up the breakpoint. */
10516 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10517 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10518 set_breakpoint_count (breakpoint_count + 1);
10519 b->number = breakpoint_count;
10520 b->disposition = disp_donttouch;
f00aae0f
KS
10521 b->location = copy_event_location (start_location);
10522 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10523 b->loc->length = length;
10524
f8eba3c6 10525 do_cleanups (cleanup_bkpt);
f1310107
TJB
10526
10527 mention (b);
8d3788bd 10528 observer_notify_breakpoint_created (b);
44702360 10529 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10530}
10531
4a64f543
MS
10532/* Return non-zero if EXP is verified as constant. Returned zero
10533 means EXP is variable. Also the constant detection may fail for
10534 some constant expressions and in such case still falsely return
10535 zero. */
2e6e3d9c 10536
65d79d4b
SDJ
10537static int
10538watchpoint_exp_is_const (const struct expression *exp)
10539{
10540 int i = exp->nelts;
10541
10542 while (i > 0)
10543 {
10544 int oplenp, argsp;
10545
10546 /* We are only interested in the descriptor of each element. */
10547 operator_length (exp, i, &oplenp, &argsp);
10548 i -= oplenp;
10549
10550 switch (exp->elts[i].opcode)
10551 {
10552 case BINOP_ADD:
10553 case BINOP_SUB:
10554 case BINOP_MUL:
10555 case BINOP_DIV:
10556 case BINOP_REM:
10557 case BINOP_MOD:
10558 case BINOP_LSH:
10559 case BINOP_RSH:
10560 case BINOP_LOGICAL_AND:
10561 case BINOP_LOGICAL_OR:
10562 case BINOP_BITWISE_AND:
10563 case BINOP_BITWISE_IOR:
10564 case BINOP_BITWISE_XOR:
10565 case BINOP_EQUAL:
10566 case BINOP_NOTEQUAL:
10567 case BINOP_LESS:
10568 case BINOP_GTR:
10569 case BINOP_LEQ:
10570 case BINOP_GEQ:
10571 case BINOP_REPEAT:
10572 case BINOP_COMMA:
10573 case BINOP_EXP:
10574 case BINOP_MIN:
10575 case BINOP_MAX:
10576 case BINOP_INTDIV:
10577 case BINOP_CONCAT:
65d79d4b
SDJ
10578 case TERNOP_COND:
10579 case TERNOP_SLICE:
65d79d4b
SDJ
10580
10581 case OP_LONG:
10582 case OP_DOUBLE:
10583 case OP_DECFLOAT:
10584 case OP_LAST:
10585 case OP_COMPLEX:
10586 case OP_STRING:
65d79d4b
SDJ
10587 case OP_ARRAY:
10588 case OP_TYPE:
608b4967
TT
10589 case OP_TYPEOF:
10590 case OP_DECLTYPE:
6e72ca20 10591 case OP_TYPEID:
65d79d4b
SDJ
10592 case OP_NAME:
10593 case OP_OBJC_NSSTRING:
10594
10595 case UNOP_NEG:
10596 case UNOP_LOGICAL_NOT:
10597 case UNOP_COMPLEMENT:
10598 case UNOP_ADDR:
10599 case UNOP_HIGH:
aeaa2474 10600 case UNOP_CAST:
9eaf6705
TT
10601
10602 case UNOP_CAST_TYPE:
10603 case UNOP_REINTERPRET_CAST:
10604 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10605 /* Unary, binary and ternary operators: We have to check
10606 their operands. If they are constant, then so is the
10607 result of that operation. For instance, if A and B are
10608 determined to be constants, then so is "A + B".
10609
10610 UNOP_IND is one exception to the rule above, because the
10611 value of *ADDR is not necessarily a constant, even when
10612 ADDR is. */
65d79d4b
SDJ
10613 break;
10614
10615 case OP_VAR_VALUE:
10616 /* Check whether the associated symbol is a constant.
4a64f543 10617
65d79d4b 10618 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10619 possible that a buggy compiler could mark a variable as
10620 constant even when it is not, and TYPE_CONST would return
10621 true in this case, while SYMBOL_CLASS wouldn't.
10622
10623 We also have to check for function symbols because they
10624 are always constant. */
65d79d4b
SDJ
10625 {
10626 struct symbol *s = exp->elts[i + 2].symbol;
10627
10628 if (SYMBOL_CLASS (s) != LOC_BLOCK
10629 && SYMBOL_CLASS (s) != LOC_CONST
10630 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10631 return 0;
10632 break;
10633 }
10634
10635 /* The default action is to return 0 because we are using
10636 the optimistic approach here: If we don't know something,
10637 then it is not a constant. */
10638 default:
10639 return 0;
10640 }
10641 }
10642
10643 return 1;
10644}
10645
3a5c3e22
PA
10646/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10647
10648static void
10649dtor_watchpoint (struct breakpoint *self)
10650{
10651 struct watchpoint *w = (struct watchpoint *) self;
10652
10653 xfree (w->cond_exp);
10654 xfree (w->exp);
10655 xfree (w->exp_string);
10656 xfree (w->exp_string_reparse);
10657 value_free (w->val);
10658
10659 base_breakpoint_ops.dtor (self);
10660}
10661
348d480f
PA
10662/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10663
10664static void
10665re_set_watchpoint (struct breakpoint *b)
10666{
3a5c3e22
PA
10667 struct watchpoint *w = (struct watchpoint *) b;
10668
348d480f
PA
10669 /* Watchpoint can be either on expression using entirely global
10670 variables, or it can be on local variables.
10671
10672 Watchpoints of the first kind are never auto-deleted, and even
10673 persist across program restarts. Since they can use variables
10674 from shared libraries, we need to reparse expression as libraries
10675 are loaded and unloaded.
10676
10677 Watchpoints on local variables can also change meaning as result
10678 of solib event. For example, if a watchpoint uses both a local
10679 and a global variables in expression, it's a local watchpoint,
10680 but unloading of a shared library will make the expression
10681 invalid. This is not a very common use case, but we still
10682 re-evaluate expression, to avoid surprises to the user.
10683
10684 Note that for local watchpoints, we re-evaluate it only if
10685 watchpoints frame id is still valid. If it's not, it means the
10686 watchpoint is out of scope and will be deleted soon. In fact,
10687 I'm not sure we'll ever be called in this case.
10688
10689 If a local watchpoint's frame id is still valid, then
3a5c3e22 10690 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10691
3a5c3e22
PA
10692 Don't do anything about disabled watchpoints, since they will be
10693 reevaluated again when enabled. */
10694 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10695}
10696
77b06cd7
TJB
10697/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10698
10699static int
10700insert_watchpoint (struct bp_location *bl)
10701{
3a5c3e22
PA
10702 struct watchpoint *w = (struct watchpoint *) bl->owner;
10703 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10704
10705 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10706 w->cond_exp);
77b06cd7
TJB
10707}
10708
10709/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10710
10711static int
73971819 10712remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10713{
3a5c3e22
PA
10714 struct watchpoint *w = (struct watchpoint *) bl->owner;
10715 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10716
10717 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10718 w->cond_exp);
e09342b5
TJB
10719}
10720
e09342b5 10721static int
348d480f 10722breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10723 struct address_space *aspace, CORE_ADDR bp_addr,
10724 const struct target_waitstatus *ws)
e09342b5 10725{
348d480f 10726 struct breakpoint *b = bl->owner;
3a5c3e22 10727 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10728
348d480f
PA
10729 /* Continuable hardware watchpoints are treated as non-existent if the
10730 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10731 some data address). Otherwise gdb won't stop on a break instruction
10732 in the code (not from a breakpoint) when a hardware watchpoint has
10733 been defined. Also skip watchpoints which we know did not trigger
10734 (did not match the data address). */
10735 if (is_hardware_watchpoint (b)
3a5c3e22 10736 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10737 return 0;
9c06b0b4 10738
348d480f 10739 return 1;
9c06b0b4
TJB
10740}
10741
348d480f
PA
10742static void
10743check_status_watchpoint (bpstat bs)
9c06b0b4 10744{
348d480f 10745 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10746
348d480f 10747 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10748}
10749
10750/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10751 hardware watchpoints. */
9c06b0b4
TJB
10752
10753static int
348d480f 10754resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10755{
3a5c3e22
PA
10756 struct watchpoint *w = (struct watchpoint *) bl->owner;
10757 int length = w->exact? 1 : bl->length;
348d480f
PA
10758
10759 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10760}
10761
10762/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10763 hardware watchpoints. */
9c06b0b4
TJB
10764
10765static int
348d480f 10766works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10767{
efa80663
PA
10768 /* Read and access watchpoints only work with hardware support. */
10769 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10770}
10771
9c06b0b4 10772static enum print_stop_action
348d480f 10773print_it_watchpoint (bpstat bs)
9c06b0b4 10774{
348d480f
PA
10775 struct cleanup *old_chain;
10776 struct breakpoint *b;
f99d8bf4 10777 struct ui_file *stb;
348d480f 10778 enum print_stop_action result;
3a5c3e22 10779 struct watchpoint *w;
79a45e25 10780 struct ui_out *uiout = current_uiout;
348d480f
PA
10781
10782 gdb_assert (bs->bp_location_at != NULL);
10783
348d480f 10784 b = bs->breakpoint_at;
3a5c3e22 10785 w = (struct watchpoint *) b;
348d480f 10786
f99d8bf4
PA
10787 stb = mem_fileopen ();
10788 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10789
f303dbd6
PA
10790 annotate_watchpoint (b->number);
10791 maybe_print_thread_hit_breakpoint (uiout);
10792
9c06b0b4
TJB
10793 switch (b->type)
10794 {
348d480f 10795 case bp_watchpoint:
9c06b0b4 10796 case bp_hardware_watchpoint:
9c06b0b4
TJB
10797 if (ui_out_is_mi_like_p (uiout))
10798 ui_out_field_string
10799 (uiout, "reason",
10800 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10801 mention (b);
10802 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10803 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10804 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10805 ui_out_field_stream (uiout, "old", stb);
10806 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10807 watchpoint_value_print (w->val, stb);
348d480f
PA
10808 ui_out_field_stream (uiout, "new", stb);
10809 ui_out_text (uiout, "\n");
10810 /* More than one watchpoint may have been triggered. */
10811 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10812 break;
10813
10814 case bp_read_watchpoint:
10815 if (ui_out_is_mi_like_p (uiout))
10816 ui_out_field_string
10817 (uiout, "reason",
10818 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10819 mention (b);
10820 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10821 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10822 watchpoint_value_print (w->val, stb);
348d480f
PA
10823 ui_out_field_stream (uiout, "value", stb);
10824 ui_out_text (uiout, "\n");
10825 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10826 break;
10827
10828 case bp_access_watchpoint:
348d480f
PA
10829 if (bs->old_val != NULL)
10830 {
348d480f
PA
10831 if (ui_out_is_mi_like_p (uiout))
10832 ui_out_field_string
10833 (uiout, "reason",
10834 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10835 mention (b);
10836 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10837 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10838 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10839 ui_out_field_stream (uiout, "old", stb);
10840 ui_out_text (uiout, "\nNew value = ");
10841 }
10842 else
10843 {
10844 mention (b);
10845 if (ui_out_is_mi_like_p (uiout))
10846 ui_out_field_string
10847 (uiout, "reason",
10848 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10849 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10850 ui_out_text (uiout, "\nValue = ");
10851 }
f99d8bf4 10852 watchpoint_value_print (w->val, stb);
348d480f
PA
10853 ui_out_field_stream (uiout, "new", stb);
10854 ui_out_text (uiout, "\n");
10855 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10856 break;
10857 default:
348d480f 10858 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10859 }
10860
348d480f
PA
10861 do_cleanups (old_chain);
10862 return result;
10863}
10864
10865/* Implement the "print_mention" breakpoint_ops method for hardware
10866 watchpoints. */
10867
10868static void
10869print_mention_watchpoint (struct breakpoint *b)
10870{
10871 struct cleanup *ui_out_chain;
3a5c3e22 10872 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10873 struct ui_out *uiout = current_uiout;
348d480f
PA
10874
10875 switch (b->type)
10876 {
10877 case bp_watchpoint:
10878 ui_out_text (uiout, "Watchpoint ");
10879 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10880 break;
10881 case bp_hardware_watchpoint:
10882 ui_out_text (uiout, "Hardware watchpoint ");
10883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10884 break;
10885 case bp_read_watchpoint:
10886 ui_out_text (uiout, "Hardware read watchpoint ");
10887 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10888 break;
10889 case bp_access_watchpoint:
10890 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10891 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10892 break;
10893 default:
10894 internal_error (__FILE__, __LINE__,
10895 _("Invalid hardware watchpoint type."));
10896 }
10897
10898 ui_out_field_int (uiout, "number", b->number);
10899 ui_out_text (uiout, ": ");
3a5c3e22 10900 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10901 do_cleanups (ui_out_chain);
10902}
10903
10904/* Implement the "print_recreate" breakpoint_ops method for
10905 watchpoints. */
10906
10907static void
10908print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10909{
3a5c3e22
PA
10910 struct watchpoint *w = (struct watchpoint *) b;
10911
348d480f
PA
10912 switch (b->type)
10913 {
10914 case bp_watchpoint:
10915 case bp_hardware_watchpoint:
10916 fprintf_unfiltered (fp, "watch");
10917 break;
10918 case bp_read_watchpoint:
10919 fprintf_unfiltered (fp, "rwatch");
10920 break;
10921 case bp_access_watchpoint:
10922 fprintf_unfiltered (fp, "awatch");
10923 break;
10924 default:
10925 internal_error (__FILE__, __LINE__,
10926 _("Invalid watchpoint type."));
10927 }
10928
3a5c3e22 10929 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10930 print_recreate_thread (b, fp);
348d480f
PA
10931}
10932
427cd150
TT
10933/* Implement the "explains_signal" breakpoint_ops method for
10934 watchpoints. */
10935
47591c29 10936static int
427cd150
TT
10937explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10938{
10939 /* A software watchpoint cannot cause a signal other than
10940 GDB_SIGNAL_TRAP. */
10941 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10942 return 0;
427cd150 10943
47591c29 10944 return 1;
427cd150
TT
10945}
10946
348d480f
PA
10947/* The breakpoint_ops structure to be used in hardware watchpoints. */
10948
2060206e 10949static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10950
10951/* Implement the "insert" breakpoint_ops method for
10952 masked hardware watchpoints. */
10953
10954static int
10955insert_masked_watchpoint (struct bp_location *bl)
10956{
3a5c3e22
PA
10957 struct watchpoint *w = (struct watchpoint *) bl->owner;
10958
10959 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10960 bl->watchpoint_type);
10961}
10962
10963/* Implement the "remove" breakpoint_ops method for
10964 masked hardware watchpoints. */
10965
10966static int
73971819 10967remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10968{
3a5c3e22
PA
10969 struct watchpoint *w = (struct watchpoint *) bl->owner;
10970
10971 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10972 bl->watchpoint_type);
10973}
10974
10975/* Implement the "resources_needed" breakpoint_ops method for
10976 masked hardware watchpoints. */
10977
10978static int
10979resources_needed_masked_watchpoint (const struct bp_location *bl)
10980{
3a5c3e22
PA
10981 struct watchpoint *w = (struct watchpoint *) bl->owner;
10982
10983 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10984}
10985
10986/* Implement the "works_in_software_mode" breakpoint_ops method for
10987 masked hardware watchpoints. */
10988
10989static int
10990works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10991{
10992 return 0;
10993}
10994
10995/* Implement the "print_it" breakpoint_ops method for
10996 masked hardware watchpoints. */
10997
10998static enum print_stop_action
10999print_it_masked_watchpoint (bpstat bs)
11000{
11001 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11002 struct ui_out *uiout = current_uiout;
348d480f
PA
11003
11004 /* Masked watchpoints have only one location. */
11005 gdb_assert (b->loc && b->loc->next == NULL);
11006
f303dbd6
PA
11007 annotate_watchpoint (b->number);
11008 maybe_print_thread_hit_breakpoint (uiout);
11009
348d480f
PA
11010 switch (b->type)
11011 {
11012 case bp_hardware_watchpoint:
348d480f
PA
11013 if (ui_out_is_mi_like_p (uiout))
11014 ui_out_field_string
11015 (uiout, "reason",
11016 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11017 break;
11018
11019 case bp_read_watchpoint:
11020 if (ui_out_is_mi_like_p (uiout))
11021 ui_out_field_string
11022 (uiout, "reason",
11023 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11024 break;
11025
11026 case bp_access_watchpoint:
11027 if (ui_out_is_mi_like_p (uiout))
11028 ui_out_field_string
11029 (uiout, "reason",
11030 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11031 break;
11032 default:
11033 internal_error (__FILE__, __LINE__,
11034 _("Invalid hardware watchpoint type."));
11035 }
11036
11037 mention (b);
9c06b0b4
TJB
11038 ui_out_text (uiout, _("\n\
11039Check the underlying instruction at PC for the memory\n\
11040address and value which triggered this watchpoint.\n"));
11041 ui_out_text (uiout, "\n");
11042
11043 /* More than one watchpoint may have been triggered. */
11044 return PRINT_UNKNOWN;
11045}
11046
11047/* Implement the "print_one_detail" breakpoint_ops method for
11048 masked hardware watchpoints. */
11049
11050static void
11051print_one_detail_masked_watchpoint (const struct breakpoint *b,
11052 struct ui_out *uiout)
11053{
3a5c3e22
PA
11054 struct watchpoint *w = (struct watchpoint *) b;
11055
9c06b0b4
TJB
11056 /* Masked watchpoints have only one location. */
11057 gdb_assert (b->loc && b->loc->next == NULL);
11058
11059 ui_out_text (uiout, "\tmask ");
3a5c3e22 11060 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11061 ui_out_text (uiout, "\n");
11062}
11063
11064/* Implement the "print_mention" breakpoint_ops method for
11065 masked hardware watchpoints. */
11066
11067static void
11068print_mention_masked_watchpoint (struct breakpoint *b)
11069{
3a5c3e22 11070 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11071 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11072 struct cleanup *ui_out_chain;
11073
11074 switch (b->type)
11075 {
11076 case bp_hardware_watchpoint:
11077 ui_out_text (uiout, "Masked hardware watchpoint ");
11078 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11079 break;
11080 case bp_read_watchpoint:
11081 ui_out_text (uiout, "Masked hardware read watchpoint ");
11082 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11083 break;
11084 case bp_access_watchpoint:
11085 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11086 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11087 break;
11088 default:
11089 internal_error (__FILE__, __LINE__,
11090 _("Invalid hardware watchpoint type."));
11091 }
11092
11093 ui_out_field_int (uiout, "number", b->number);
11094 ui_out_text (uiout, ": ");
3a5c3e22 11095 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11096 do_cleanups (ui_out_chain);
11097}
11098
11099/* Implement the "print_recreate" breakpoint_ops method for
11100 masked hardware watchpoints. */
11101
11102static void
11103print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11104{
3a5c3e22 11105 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11106 char tmp[40];
11107
11108 switch (b->type)
11109 {
11110 case bp_hardware_watchpoint:
11111 fprintf_unfiltered (fp, "watch");
11112 break;
11113 case bp_read_watchpoint:
11114 fprintf_unfiltered (fp, "rwatch");
11115 break;
11116 case bp_access_watchpoint:
11117 fprintf_unfiltered (fp, "awatch");
11118 break;
11119 default:
11120 internal_error (__FILE__, __LINE__,
11121 _("Invalid hardware watchpoint type."));
11122 }
11123
3a5c3e22
PA
11124 sprintf_vma (tmp, w->hw_wp_mask);
11125 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11126 print_recreate_thread (b, fp);
9c06b0b4
TJB
11127}
11128
11129/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11130
2060206e 11131static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11132
11133/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11134
11135static int
11136is_masked_watchpoint (const struct breakpoint *b)
11137{
11138 return b->ops == &masked_watchpoint_breakpoint_ops;
11139}
11140
53a5351d
JM
11141/* accessflag: hw_write: watch write,
11142 hw_read: watch read,
11143 hw_access: watch access (read or write) */
c906108c 11144static void
bbc13ae3 11145watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11146 int just_location, int internal)
c906108c 11147{
d983da9c 11148 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11149 struct expression *exp;
270140bd 11150 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11151 struct value *val, *mark, *result;
bb9d5f81 11152 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11153 struct frame_info *frame;
bbc13ae3
KS
11154 const char *exp_start = NULL;
11155 const char *exp_end = NULL;
11156 const char *tok, *end_tok;
9c06b0b4 11157 int toklen = -1;
bbc13ae3
KS
11158 const char *cond_start = NULL;
11159 const char *cond_end = NULL;
c906108c 11160 enum bptype bp_type;
37e4754d 11161 int thread = -1;
0cf6dd15 11162 int pc = 0;
9c06b0b4
TJB
11163 /* Flag to indicate whether we are going to use masks for
11164 the hardware watchpoint. */
11165 int use_mask = 0;
11166 CORE_ADDR mask = 0;
3a5c3e22 11167 struct watchpoint *w;
bbc13ae3
KS
11168 char *expression;
11169 struct cleanup *back_to;
c906108c 11170
37e4754d
LM
11171 /* Make sure that we actually have parameters to parse. */
11172 if (arg != NULL && arg[0] != '\0')
11173 {
bbc13ae3
KS
11174 const char *value_start;
11175
11176 exp_end = arg + strlen (arg);
37e4754d 11177
9c06b0b4
TJB
11178 /* Look for "parameter value" pairs at the end
11179 of the arguments string. */
bbc13ae3 11180 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11181 {
11182 /* Skip whitespace at the end of the argument list. */
11183 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11184 tok--;
11185
11186 /* Find the beginning of the last token.
11187 This is the value of the parameter. */
11188 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11189 tok--;
11190 value_start = tok + 1;
11191
11192 /* Skip whitespace. */
11193 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11194 tok--;
11195
11196 end_tok = tok;
11197
11198 /* Find the beginning of the second to last token.
11199 This is the parameter itself. */
11200 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11201 tok--;
11202 tok++;
11203 toklen = end_tok - tok + 1;
11204
61012eef 11205 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11206 {
5d5658a1 11207 struct thread_info *thr;
9c06b0b4
TJB
11208 /* At this point we've found a "thread" token, which means
11209 the user is trying to set a watchpoint that triggers
11210 only in a specific thread. */
5d5658a1 11211 const char *endp;
37e4754d 11212
9c06b0b4
TJB
11213 if (thread != -1)
11214 error(_("You can specify only one thread."));
37e4754d 11215
9c06b0b4 11216 /* Extract the thread ID from the next token. */
5d5658a1 11217 thr = parse_thread_id (value_start, &endp);
37e4754d 11218
5d5658a1 11219 /* Check if the user provided a valid thread ID. */
9c06b0b4 11220 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11221 invalid_thread_id_error (value_start);
9c06b0b4 11222
5d5658a1 11223 thread = thr->global_num;
9c06b0b4 11224 }
61012eef 11225 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11226 {
11227 /* We've found a "mask" token, which means the user wants to
11228 create a hardware watchpoint that is going to have the mask
11229 facility. */
11230 struct value *mask_value, *mark;
37e4754d 11231
9c06b0b4
TJB
11232 if (use_mask)
11233 error(_("You can specify only one mask."));
37e4754d 11234
9c06b0b4 11235 use_mask = just_location = 1;
37e4754d 11236
9c06b0b4
TJB
11237 mark = value_mark ();
11238 mask_value = parse_to_comma_and_eval (&value_start);
11239 mask = value_as_address (mask_value);
11240 value_free_to_mark (mark);
11241 }
11242 else
11243 /* We didn't recognize what we found. We should stop here. */
11244 break;
37e4754d 11245
9c06b0b4
TJB
11246 /* Truncate the string and get rid of the "parameter value" pair before
11247 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11248 exp_end = tok;
9c06b0b4 11249 }
37e4754d 11250 }
bbc13ae3
KS
11251 else
11252 exp_end = arg;
37e4754d 11253
bbc13ae3
KS
11254 /* Parse the rest of the arguments. From here on out, everything
11255 is in terms of a newly allocated string instead of the original
11256 ARG. */
c906108c 11257 innermost_block = NULL;
bbc13ae3
KS
11258 expression = savestring (arg, exp_end - arg);
11259 back_to = make_cleanup (xfree, expression);
11260 exp_start = arg = expression;
1bb9788d 11261 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11262 exp_end = arg;
fa8a61dc
TT
11263 /* Remove trailing whitespace from the expression before saving it.
11264 This makes the eventual display of the expression string a bit
11265 prettier. */
11266 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11267 --exp_end;
11268
65d79d4b
SDJ
11269 /* Checking if the expression is not constant. */
11270 if (watchpoint_exp_is_const (exp))
11271 {
11272 int len;
11273
11274 len = exp_end - exp_start;
11275 while (len > 0 && isspace (exp_start[len - 1]))
11276 len--;
11277 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11278 }
11279
c906108c
SS
11280 exp_valid_block = innermost_block;
11281 mark = value_mark ();
3a1115a0 11282 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11283
bb9d5f81
PP
11284 if (val != NULL && just_location)
11285 {
11286 saved_bitpos = value_bitpos (val);
11287 saved_bitsize = value_bitsize (val);
11288 }
11289
06a64a0b
TT
11290 if (just_location)
11291 {
9c06b0b4
TJB
11292 int ret;
11293
06a64a0b 11294 exp_valid_block = NULL;
a1442452 11295 val = value_addr (result);
06a64a0b
TT
11296 release_value (val);
11297 value_free_to_mark (mark);
9c06b0b4
TJB
11298
11299 if (use_mask)
11300 {
11301 ret = target_masked_watch_num_registers (value_as_address (val),
11302 mask);
11303 if (ret == -1)
11304 error (_("This target does not support masked watchpoints."));
11305 else if (ret == -2)
11306 error (_("Invalid mask or memory region."));
11307 }
06a64a0b
TT
11308 }
11309 else if (val != NULL)
fa4727a6 11310 release_value (val);
c906108c 11311
bbc13ae3
KS
11312 tok = skip_spaces_const (arg);
11313 end_tok = skip_to_space_const (tok);
c906108c
SS
11314
11315 toklen = end_tok - tok;
11316 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11317 {
2d134ed3
PA
11318 struct expression *cond;
11319
60e1c644 11320 innermost_block = NULL;
c906108c 11321 tok = cond_start = end_tok + 1;
1bb9788d 11322 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11323
11324 /* The watchpoint expression may not be local, but the condition
11325 may still be. E.g.: `watch global if local > 0'. */
11326 cond_exp_valid_block = innermost_block;
11327
2d134ed3 11328 xfree (cond);
c906108c
SS
11329 cond_end = tok;
11330 }
11331 if (*tok)
8a3fe4f8 11332 error (_("Junk at end of command."));
c906108c 11333
d983da9c 11334 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11335
11336 /* If the expression is "local", then set up a "watchpoint scope"
11337 breakpoint at the point where we've left the scope of the watchpoint
11338 expression. Create the scope breakpoint before the watchpoint, so
11339 that we will encounter it first in bpstat_stop_status. */
60e1c644 11340 if (exp_valid_block && frame)
d983da9c 11341 {
edb3359d
DJ
11342 if (frame_id_p (frame_unwind_caller_id (frame)))
11343 {
11344 scope_breakpoint
a6d9a66e
UW
11345 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11346 frame_unwind_caller_pc (frame),
06edf0c0
PA
11347 bp_watchpoint_scope,
11348 &momentary_breakpoint_ops);
d983da9c 11349
edb3359d 11350 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11351
edb3359d
DJ
11352 /* Automatically delete the breakpoint when it hits. */
11353 scope_breakpoint->disposition = disp_del;
d983da9c 11354
edb3359d
DJ
11355 /* Only break in the proper frame (help with recursion). */
11356 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11357
edb3359d 11358 /* Set the address at which we will stop. */
a6d9a66e
UW
11359 scope_breakpoint->loc->gdbarch
11360 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11361 scope_breakpoint->loc->requested_address
11362 = frame_unwind_caller_pc (frame);
11363 scope_breakpoint->loc->address
a6d9a66e
UW
11364 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11365 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11366 scope_breakpoint->type);
11367 }
d983da9c
DJ
11368 }
11369
e8369a73
AB
11370 /* Now set up the breakpoint. We create all watchpoints as hardware
11371 watchpoints here even if hardware watchpoints are turned off, a call
11372 to update_watchpoint later in this function will cause the type to
11373 drop back to bp_watchpoint (software watchpoint) if required. */
11374
11375 if (accessflag == hw_read)
11376 bp_type = bp_read_watchpoint;
11377 else if (accessflag == hw_access)
11378 bp_type = bp_access_watchpoint;
11379 else
11380 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11381
11382 w = XCNEW (struct watchpoint);
11383 b = &w->base;
348d480f 11384 if (use_mask)
3a5c3e22
PA
11385 init_raw_breakpoint_without_location (b, NULL, bp_type,
11386 &masked_watchpoint_breakpoint_ops);
348d480f 11387 else
3a5c3e22
PA
11388 init_raw_breakpoint_without_location (b, NULL, bp_type,
11389 &watchpoint_breakpoint_ops);
37e4754d 11390 b->thread = thread;
b5de0fa7 11391 b->disposition = disp_donttouch;
348d480f 11392 b->pspace = current_program_space;
3a5c3e22
PA
11393 w->exp = exp;
11394 w->exp_valid_block = exp_valid_block;
11395 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11396 if (just_location)
11397 {
11398 struct type *t = value_type (val);
11399 CORE_ADDR addr = value_as_address (val);
11400 char *name;
11401
11402 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11403 name = type_to_string (t);
11404
3a5c3e22 11405 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11406 core_addr_to_string (addr));
06a64a0b
TT
11407 xfree (name);
11408
3a5c3e22 11409 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11410 (int) (exp_end - exp_start), exp_start);
11411
06a64a0b
TT
11412 /* The above expression is in C. */
11413 b->language = language_c;
11414 }
11415 else
3a5c3e22 11416 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11417
11418 if (use_mask)
11419 {
3a5c3e22 11420 w->hw_wp_mask = mask;
9c06b0b4
TJB
11421 }
11422 else
11423 {
3a5c3e22 11424 w->val = val;
bb9d5f81
PP
11425 w->val_bitpos = saved_bitpos;
11426 w->val_bitsize = saved_bitsize;
3a5c3e22 11427 w->val_valid = 1;
9c06b0b4 11428 }
77b06cd7 11429
c906108c
SS
11430 if (cond_start)
11431 b->cond_string = savestring (cond_start, cond_end - cond_start);
11432 else
11433 b->cond_string = 0;
c5aa993b 11434
c906108c 11435 if (frame)
f6bc2008 11436 {
3a5c3e22
PA
11437 w->watchpoint_frame = get_frame_id (frame);
11438 w->watchpoint_thread = inferior_ptid;
f6bc2008 11439 }
c906108c 11440 else
f6bc2008 11441 {
3a5c3e22
PA
11442 w->watchpoint_frame = null_frame_id;
11443 w->watchpoint_thread = null_ptid;
f6bc2008 11444 }
c906108c 11445
d983da9c 11446 if (scope_breakpoint != NULL)
c906108c 11447 {
d983da9c
DJ
11448 /* The scope breakpoint is related to the watchpoint. We will
11449 need to act on them together. */
11450 b->related_breakpoint = scope_breakpoint;
11451 scope_breakpoint->related_breakpoint = b;
c906108c 11452 }
d983da9c 11453
06a64a0b
TT
11454 if (!just_location)
11455 value_free_to_mark (mark);
2d134ed3 11456
492d29ea 11457 TRY
a9634178
TJB
11458 {
11459 /* Finally update the new watchpoint. This creates the locations
11460 that should be inserted. */
3a5c3e22 11461 update_watchpoint (w, 1);
a9634178 11462 }
492d29ea 11463 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11464 {
11465 delete_breakpoint (b);
11466 throw_exception (e);
11467 }
492d29ea 11468 END_CATCH
a9634178 11469
3ea46bff 11470 install_breakpoint (internal, b, 1);
bbc13ae3 11471 do_cleanups (back_to);
c906108c
SS
11472}
11473
e09342b5 11474/* Return count of debug registers needed to watch the given expression.
e09342b5 11475 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11476
c906108c 11477static int
a9634178 11478can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11479{
11480 int found_memory_cnt = 0;
2e70b7b9 11481 struct value *head = v;
c906108c
SS
11482
11483 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11484 if (!can_use_hw_watchpoints)
c906108c 11485 return 0;
c5aa993b 11486
5c44784c
JM
11487 /* Make sure that the value of the expression depends only upon
11488 memory contents, and values computed from them within GDB. If we
11489 find any register references or function calls, we can't use a
11490 hardware watchpoint.
11491
11492 The idea here is that evaluating an expression generates a series
11493 of values, one holding the value of every subexpression. (The
11494 expression a*b+c has five subexpressions: a, b, a*b, c, and
11495 a*b+c.) GDB's values hold almost enough information to establish
11496 the criteria given above --- they identify memory lvalues,
11497 register lvalues, computed values, etcetera. So we can evaluate
11498 the expression, and then scan the chain of values that leaves
11499 behind to decide whether we can detect any possible change to the
11500 expression's final value using only hardware watchpoints.
11501
11502 However, I don't think that the values returned by inferior
11503 function calls are special in any way. So this function may not
11504 notice that an expression involving an inferior function call
11505 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11506 for (; v; v = value_next (v))
c906108c 11507 {
5c44784c 11508 if (VALUE_LVAL (v) == lval_memory)
c906108c 11509 {
8464be76
DJ
11510 if (v != head && value_lazy (v))
11511 /* A lazy memory lvalue in the chain is one that GDB never
11512 needed to fetch; we either just used its address (e.g.,
11513 `a' in `a.b') or we never needed it at all (e.g., `a'
11514 in `a,b'). This doesn't apply to HEAD; if that is
11515 lazy then it was not readable, but watch it anyway. */
5c44784c 11516 ;
53a5351d 11517 else
5c44784c
JM
11518 {
11519 /* Ahh, memory we actually used! Check if we can cover
11520 it with hardware watchpoints. */
df407dfe 11521 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11522
11523 /* We only watch structs and arrays if user asked for it
11524 explicitly, never if they just happen to appear in a
11525 middle of some value chain. */
11526 if (v == head
11527 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11528 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11529 {
42ae5230 11530 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11531 int len;
11532 int num_regs;
11533
a9634178 11534 len = (target_exact_watchpoints
e09342b5
TJB
11535 && is_scalar_type_recursive (vtype))?
11536 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11537
e09342b5
TJB
11538 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11539 if (!num_regs)
2e70b7b9
MS
11540 return 0;
11541 else
e09342b5 11542 found_memory_cnt += num_regs;
2e70b7b9 11543 }
5c44784c 11544 }
c5aa993b 11545 }
5086187c
AC
11546 else if (VALUE_LVAL (v) != not_lval
11547 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11548 return 0; /* These are values from the history (e.g., $1). */
5086187c 11549 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11550 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11551 }
11552
11553 /* The expression itself looks suitable for using a hardware
11554 watchpoint, but give the target machine a chance to reject it. */
11555 return found_memory_cnt;
11556}
11557
8b93c638 11558void
84f4c1fe 11559watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11560{
84f4c1fe 11561 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11562}
11563
06a64a0b
TT
11564/* A helper function that looks for the "-location" argument and then
11565 calls watch_command_1. */
11566
11567static void
11568watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11569{
11570 int just_location = 0;
11571
11572 if (arg
11573 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11574 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11575 {
e9cafbcc 11576 arg = skip_spaces (arg);
06a64a0b
TT
11577 just_location = 1;
11578 }
11579
84f4c1fe 11580 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11581}
8926118c 11582
c5aa993b 11583static void
fba45db2 11584watch_command (char *arg, int from_tty)
c906108c 11585{
06a64a0b 11586 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11587}
11588
8b93c638 11589void
84f4c1fe 11590rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11591{
84f4c1fe 11592 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11593}
8926118c 11594
c5aa993b 11595static void
fba45db2 11596rwatch_command (char *arg, int from_tty)
c906108c 11597{
06a64a0b 11598 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11599}
11600
8b93c638 11601void
84f4c1fe 11602awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11603{
84f4c1fe 11604 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11605}
8926118c 11606
c5aa993b 11607static void
fba45db2 11608awatch_command (char *arg, int from_tty)
c906108c 11609{
06a64a0b 11610 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11611}
c906108c 11612\f
c5aa993b 11613
cfc31633
PA
11614/* Data for the FSM that manages the until(location)/advance commands
11615 in infcmd.c. Here because it uses the mechanisms of
11616 breakpoints. */
c906108c 11617
cfc31633 11618struct until_break_fsm
bfec99b2 11619{
cfc31633
PA
11620 /* The base class. */
11621 struct thread_fsm thread_fsm;
11622
11623 /* The thread that as current when the command was executed. */
11624 int thread;
11625
11626 /* The breakpoint set at the destination location. */
11627 struct breakpoint *location_breakpoint;
11628
11629 /* Breakpoint set at the return address in the caller frame. May be
11630 NULL. */
11631 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11632};
11633
8980e177
PA
11634static void until_break_fsm_clean_up (struct thread_fsm *self,
11635 struct thread_info *thread);
11636static int until_break_fsm_should_stop (struct thread_fsm *self,
11637 struct thread_info *thread);
cfc31633
PA
11638static enum async_reply_reason
11639 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11640
11641/* until_break_fsm's vtable. */
11642
11643static struct thread_fsm_ops until_break_fsm_ops =
11644{
11645 NULL, /* dtor */
11646 until_break_fsm_clean_up,
11647 until_break_fsm_should_stop,
11648 NULL, /* return_value */
11649 until_break_fsm_async_reply_reason,
11650};
11651
11652/* Allocate a new until_break_command_fsm. */
11653
11654static struct until_break_fsm *
8980e177 11655new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11656 struct breakpoint *location_breakpoint,
11657 struct breakpoint *caller_breakpoint)
11658{
11659 struct until_break_fsm *sm;
11660
11661 sm = XCNEW (struct until_break_fsm);
8980e177 11662 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11663
11664 sm->thread = thread;
11665 sm->location_breakpoint = location_breakpoint;
11666 sm->caller_breakpoint = caller_breakpoint;
11667
11668 return sm;
11669}
11670
11671/* Implementation of the 'should_stop' FSM method for the
11672 until(location)/advance commands. */
11673
11674static int
8980e177
PA
11675until_break_fsm_should_stop (struct thread_fsm *self,
11676 struct thread_info *tp)
cfc31633
PA
11677{
11678 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11679
11680 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11681 sm->location_breakpoint) != NULL
11682 || (sm->caller_breakpoint != NULL
11683 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11684 sm->caller_breakpoint) != NULL))
11685 thread_fsm_set_finished (self);
11686
11687 return 1;
11688}
11689
11690/* Implementation of the 'clean_up' FSM method for the
11691 until(location)/advance commands. */
11692
c2c6d25f 11693static void
8980e177
PA
11694until_break_fsm_clean_up (struct thread_fsm *self,
11695 struct thread_info *thread)
43ff13b4 11696{
cfc31633 11697 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11698
cfc31633
PA
11699 /* Clean up our temporary breakpoints. */
11700 if (sm->location_breakpoint != NULL)
11701 {
11702 delete_breakpoint (sm->location_breakpoint);
11703 sm->location_breakpoint = NULL;
11704 }
11705 if (sm->caller_breakpoint != NULL)
11706 {
11707 delete_breakpoint (sm->caller_breakpoint);
11708 sm->caller_breakpoint = NULL;
11709 }
11710 delete_longjmp_breakpoint (sm->thread);
11711}
11712
11713/* Implementation of the 'async_reply_reason' FSM method for the
11714 until(location)/advance commands. */
11715
11716static enum async_reply_reason
11717until_break_fsm_async_reply_reason (struct thread_fsm *self)
11718{
11719 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11720}
11721
c906108c 11722void
ae66c1fc 11723until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11724{
11725 struct symtabs_and_lines sals;
11726 struct symtab_and_line sal;
8556afb4
PA
11727 struct frame_info *frame;
11728 struct gdbarch *frame_gdbarch;
11729 struct frame_id stack_frame_id;
11730 struct frame_id caller_frame_id;
cfc31633
PA
11731 struct breakpoint *location_breakpoint;
11732 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11733 struct cleanup *old_chain, *cleanup;
186c406b
TT
11734 int thread;
11735 struct thread_info *tp;
f00aae0f 11736 struct event_location *location;
cfc31633 11737 struct until_break_fsm *sm;
c906108c 11738
70509625 11739 clear_proceed_status (0);
c906108c
SS
11740
11741 /* Set a breakpoint where the user wants it and at return from
4a64f543 11742 this function. */
c5aa993b 11743
f00aae0f
KS
11744 location = string_to_event_location (&arg, current_language);
11745 cleanup = make_cleanup_delete_event_location (location);
11746
1bfeeb0f 11747 if (last_displayed_sal_is_valid ())
c2f4122d 11748 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11749 get_last_displayed_symtab (),
f8eba3c6 11750 get_last_displayed_line ());
c906108c 11751 else
f00aae0f 11752 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11753 NULL, (struct symtab *) NULL, 0);
c5aa993b 11754
c906108c 11755 if (sals.nelts != 1)
8a3fe4f8 11756 error (_("Couldn't get information on specified line."));
c5aa993b 11757
c906108c 11758 sal = sals.sals[0];
4a64f543 11759 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11760
c906108c 11761 if (*arg)
8a3fe4f8 11762 error (_("Junk at end of arguments."));
c5aa993b 11763
c906108c 11764 resolve_sal_pc (&sal);
c5aa993b 11765
186c406b 11766 tp = inferior_thread ();
5d5658a1 11767 thread = tp->global_num;
186c406b 11768
883bc8d1
PA
11769 old_chain = make_cleanup (null_cleanup, NULL);
11770
8556afb4
PA
11771 /* Note linespec handling above invalidates the frame chain.
11772 Installing a breakpoint also invalidates the frame chain (as it
11773 may need to switch threads), so do any frame handling before
11774 that. */
11775
11776 frame = get_selected_frame (NULL);
11777 frame_gdbarch = get_frame_arch (frame);
11778 stack_frame_id = get_stack_frame_id (frame);
11779 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11780
ae66c1fc
EZ
11781 /* Keep within the current frame, or in frames called by the current
11782 one. */
edb3359d 11783
883bc8d1 11784 if (frame_id_p (caller_frame_id))
c906108c 11785 {
883bc8d1 11786 struct symtab_and_line sal2;
cfc31633 11787 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11788
11789 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11790 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11791 caller_gdbarch = frame_unwind_caller_arch (frame);
11792 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11793 sal2,
11794 caller_frame_id,
11795 bp_until);
11796 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11797
883bc8d1 11798 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11799 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11800 }
c5aa993b 11801
c70a6932
JK
11802 /* set_momentary_breakpoint could invalidate FRAME. */
11803 frame = NULL;
11804
883bc8d1
PA
11805 if (anywhere)
11806 /* If the user told us to continue until a specified location,
11807 we don't specify a frame at which we need to stop. */
cfc31633
PA
11808 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11809 null_frame_id, bp_until);
883bc8d1
PA
11810 else
11811 /* Otherwise, specify the selected frame, because we want to stop
11812 only at the very same frame. */
cfc31633
PA
11813 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11814 stack_frame_id, bp_until);
11815 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11816
8980e177 11817 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11818 location_breakpoint, caller_breakpoint);
cfc31633 11819 tp->thread_fsm = &sm->thread_fsm;
f107f563 11820
cfc31633 11821 discard_cleanups (old_chain);
f107f563 11822
cfc31633 11823 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11824
11825 do_cleanups (cleanup);
c906108c 11826}
ae66c1fc 11827
c906108c
SS
11828/* This function attempts to parse an optional "if <cond>" clause
11829 from the arg string. If one is not found, it returns NULL.
c5aa993b 11830
c906108c
SS
11831 Else, it returns a pointer to the condition string. (It does not
11832 attempt to evaluate the string against a particular block.) And,
11833 it updates arg to point to the first character following the parsed
4a64f543 11834 if clause in the arg string. */
53a5351d 11835
916703c0 11836char *
fba45db2 11837ep_parse_optional_if_clause (char **arg)
c906108c 11838{
c5aa993b
JM
11839 char *cond_string;
11840
11841 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11842 return NULL;
c5aa993b 11843
4a64f543 11844 /* Skip the "if" keyword. */
c906108c 11845 (*arg) += 2;
c5aa993b 11846
c906108c 11847 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11848 condition string. */
e9cafbcc 11849 *arg = skip_spaces (*arg);
c906108c 11850 cond_string = *arg;
c5aa993b 11851
4a64f543
MS
11852 /* Assume that the condition occupies the remainder of the arg
11853 string. */
c906108c 11854 (*arg) += strlen (cond_string);
c5aa993b 11855
c906108c
SS
11856 return cond_string;
11857}
c5aa993b 11858
c906108c
SS
11859/* Commands to deal with catching events, such as signals, exceptions,
11860 process start/exit, etc. */
c5aa993b
JM
11861
11862typedef enum
11863{
44feb3ce
TT
11864 catch_fork_temporary, catch_vfork_temporary,
11865 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11866}
11867catch_fork_kind;
11868
c906108c 11869static void
cc59ec59
MS
11870catch_fork_command_1 (char *arg, int from_tty,
11871 struct cmd_list_element *command)
c906108c 11872{
a6d9a66e 11873 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11874 char *cond_string = NULL;
44feb3ce
TT
11875 catch_fork_kind fork_kind;
11876 int tempflag;
11877
11878 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11879 tempflag = (fork_kind == catch_fork_temporary
11880 || fork_kind == catch_vfork_temporary);
c5aa993b 11881
44feb3ce
TT
11882 if (!arg)
11883 arg = "";
e9cafbcc 11884 arg = skip_spaces (arg);
c5aa993b 11885
c906108c 11886 /* The allowed syntax is:
c5aa993b
JM
11887 catch [v]fork
11888 catch [v]fork if <cond>
11889
4a64f543 11890 First, check if there's an if clause. */
c906108c 11891 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11892
c906108c 11893 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11894 error (_("Junk at end of arguments."));
c5aa993b 11895
c906108c 11896 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11897 and enable reporting of such events. */
c5aa993b
JM
11898 switch (fork_kind)
11899 {
44feb3ce
TT
11900 case catch_fork_temporary:
11901 case catch_fork_permanent:
a6d9a66e 11902 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11903 &catch_fork_breakpoint_ops);
c906108c 11904 break;
44feb3ce
TT
11905 case catch_vfork_temporary:
11906 case catch_vfork_permanent:
a6d9a66e 11907 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11908 &catch_vfork_breakpoint_ops);
c906108c 11909 break;
c5aa993b 11910 default:
8a3fe4f8 11911 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11912 break;
c5aa993b 11913 }
c906108c
SS
11914}
11915
11916static void
cc59ec59
MS
11917catch_exec_command_1 (char *arg, int from_tty,
11918 struct cmd_list_element *command)
c906108c 11919{
b4d90040 11920 struct exec_catchpoint *c;
a6d9a66e 11921 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11922 int tempflag;
c5aa993b 11923 char *cond_string = NULL;
c906108c 11924
44feb3ce
TT
11925 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11926
11927 if (!arg)
11928 arg = "";
e9cafbcc 11929 arg = skip_spaces (arg);
c906108c
SS
11930
11931 /* The allowed syntax is:
c5aa993b
JM
11932 catch exec
11933 catch exec if <cond>
c906108c 11934
4a64f543 11935 First, check if there's an if clause. */
c906108c
SS
11936 cond_string = ep_parse_optional_if_clause (&arg);
11937
11938 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11939 error (_("Junk at end of arguments."));
c906108c 11940
b4d90040
PA
11941 c = XNEW (struct exec_catchpoint);
11942 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11943 &catch_exec_breakpoint_ops);
11944 c->exec_pathname = NULL;
11945
3ea46bff 11946 install_breakpoint (0, &c->base, 1);
c906108c 11947}
c5aa993b 11948
9ac4176b 11949void
28010a5d
PA
11950init_ada_exception_breakpoint (struct breakpoint *b,
11951 struct gdbarch *gdbarch,
11952 struct symtab_and_line sal,
11953 char *addr_string,
c0a91b2b 11954 const struct breakpoint_ops *ops,
28010a5d 11955 int tempflag,
349774ef 11956 int enabled,
28010a5d 11957 int from_tty)
f7f9143b 11958{
f7f9143b
JB
11959 if (from_tty)
11960 {
5af949e3
UW
11961 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11962 if (!loc_gdbarch)
11963 loc_gdbarch = gdbarch;
11964
6c95b8df
PA
11965 describe_other_breakpoints (loc_gdbarch,
11966 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11967 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11968 version for exception catchpoints, because two catchpoints
11969 used for different exception names will use the same address.
11970 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11971 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11972 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11973 the user what type of catchpoint it is. The above is good
11974 enough for now, though. */
11975 }
11976
28010a5d 11977 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11978
349774ef 11979 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11980 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11981 b->location = string_to_event_location (&addr_string,
11982 language_def (language_ada));
f7f9143b 11983 b->language = language_ada;
f7f9143b
JB
11984}
11985
c906108c 11986static void
fba45db2 11987catch_command (char *arg, int from_tty)
c906108c 11988{
44feb3ce 11989 error (_("Catch requires an event name."));
c906108c
SS
11990}
11991\f
11992
11993static void
fba45db2 11994tcatch_command (char *arg, int from_tty)
c906108c 11995{
44feb3ce 11996 error (_("Catch requires an event name."));
c906108c
SS
11997}
11998
8a2c437b
TT
11999/* A qsort comparison function that sorts breakpoints in order. */
12000
12001static int
12002compare_breakpoints (const void *a, const void *b)
12003{
9a3c8263 12004 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 12005 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 12006 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
12007 uintptr_t ub = (uintptr_t) *bb;
12008
12009 if ((*ba)->number < (*bb)->number)
12010 return -1;
12011 else if ((*ba)->number > (*bb)->number)
12012 return 1;
12013
12014 /* Now sort by address, in case we see, e..g, two breakpoints with
12015 the number 0. */
12016 if (ua < ub)
12017 return -1;
94b0e70d 12018 return ua > ub ? 1 : 0;
8a2c437b
TT
12019}
12020
80f8a6eb 12021/* Delete breakpoints by address or line. */
c906108c
SS
12022
12023static void
fba45db2 12024clear_command (char *arg, int from_tty)
c906108c 12025{
8a2c437b 12026 struct breakpoint *b, *prev;
d6e956e5
VP
12027 VEC(breakpoint_p) *found = 0;
12028 int ix;
c906108c
SS
12029 int default_match;
12030 struct symtabs_and_lines sals;
12031 struct symtab_and_line sal;
c906108c 12032 int i;
8a2c437b 12033 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12034
12035 if (arg)
12036 {
39cf75f7
DE
12037 sals = decode_line_with_current_source (arg,
12038 (DECODE_LINE_FUNFIRSTLINE
12039 | DECODE_LINE_LIST_MODE));
cf4ded82 12040 make_cleanup (xfree, sals.sals);
c906108c
SS
12041 default_match = 0;
12042 }
12043 else
12044 {
8d749320 12045 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12046 make_cleanup (xfree, sals.sals);
4a64f543 12047 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12048
12049 /* Set sal's line, symtab, pc, and pspace to the values
12050 corresponding to the last call to print_frame_info. If the
12051 codepoint is not valid, this will set all the fields to 0. */
12052 get_last_displayed_sal (&sal);
c906108c 12053 if (sal.symtab == 0)
8a3fe4f8 12054 error (_("No source file specified."));
c906108c
SS
12055
12056 sals.sals[0] = sal;
12057 sals.nelts = 1;
12058
12059 default_match = 1;
12060 }
12061
4a64f543
MS
12062 /* We don't call resolve_sal_pc here. That's not as bad as it
12063 seems, because all existing breakpoints typically have both
12064 file/line and pc set. So, if clear is given file/line, we can
12065 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12066
12067 We only support clearing given the address explicitly
12068 present in breakpoint table. Say, we've set breakpoint
4a64f543 12069 at file:line. There were several PC values for that file:line,
ed0616c6 12070 due to optimization, all in one block.
4a64f543
MS
12071
12072 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12073 PC corresponding to the same file:line, the breakpoint won't
12074 be cleared. We probably can still clear the breakpoint, but
12075 since the other PC value is never presented to user, user
12076 can only find it by guessing, and it does not seem important
12077 to support that. */
12078
4a64f543
MS
12079 /* For each line spec given, delete bps which correspond to it. Do
12080 it in two passes, solely to preserve the current behavior that
12081 from_tty is forced true if we delete more than one
12082 breakpoint. */
c906108c 12083
80f8a6eb 12084 found = NULL;
8a2c437b 12085 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12086 for (i = 0; i < sals.nelts; i++)
12087 {
05cba821
JK
12088 const char *sal_fullname;
12089
c906108c 12090 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12091 If line given (pc == 0), clear all bpts on specified line.
12092 If defaulting, clear all bpts on default line
c906108c 12093 or at default pc.
c5aa993b
JM
12094
12095 defaulting sal.pc != 0 tests to do
12096
12097 0 1 pc
12098 1 1 pc _and_ line
12099 0 0 line
12100 1 0 <can't happen> */
c906108c
SS
12101
12102 sal = sals.sals[i];
05cba821
JK
12103 sal_fullname = (sal.symtab == NULL
12104 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12105
4a64f543 12106 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12107 ALL_BREAKPOINTS (b)
c5aa993b 12108 {
0d381245 12109 int match = 0;
4a64f543 12110 /* Are we going to delete b? */
cc60f2e3 12111 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12112 {
12113 struct bp_location *loc = b->loc;
12114 for (; loc; loc = loc->next)
12115 {
f8eba3c6
TT
12116 /* If the user specified file:line, don't allow a PC
12117 match. This matches historical gdb behavior. */
12118 int pc_match = (!sal.explicit_line
12119 && sal.pc
12120 && (loc->pspace == sal.pspace)
12121 && (loc->address == sal.pc)
12122 && (!section_is_overlay (loc->section)
12123 || loc->section == sal.section));
4aac40c8
TT
12124 int line_match = 0;
12125
12126 if ((default_match || sal.explicit_line)
2f202fde 12127 && loc->symtab != NULL
05cba821 12128 && sal_fullname != NULL
4aac40c8 12129 && sal.pspace == loc->pspace
05cba821
JK
12130 && loc->line_number == sal.line
12131 && filename_cmp (symtab_to_fullname (loc->symtab),
12132 sal_fullname) == 0)
12133 line_match = 1;
4aac40c8 12134
0d381245
VP
12135 if (pc_match || line_match)
12136 {
12137 match = 1;
12138 break;
12139 }
12140 }
12141 }
12142
12143 if (match)
d6e956e5 12144 VEC_safe_push(breakpoint_p, found, b);
c906108c 12145 }
80f8a6eb 12146 }
8a2c437b 12147
80f8a6eb 12148 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12149 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12150 {
12151 if (arg)
8a3fe4f8 12152 error (_("No breakpoint at %s."), arg);
80f8a6eb 12153 else
8a3fe4f8 12154 error (_("No breakpoint at this line."));
80f8a6eb 12155 }
c906108c 12156
8a2c437b
TT
12157 /* Remove duplicates from the vec. */
12158 qsort (VEC_address (breakpoint_p, found),
12159 VEC_length (breakpoint_p, found),
12160 sizeof (breakpoint_p),
12161 compare_breakpoints);
12162 prev = VEC_index (breakpoint_p, found, 0);
12163 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12164 {
12165 if (b == prev)
12166 {
12167 VEC_ordered_remove (breakpoint_p, found, ix);
12168 --ix;
12169 }
12170 }
12171
d6e956e5 12172 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12173 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12174 if (from_tty)
a3f17187 12175 {
d6e956e5 12176 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12177 printf_unfiltered (_("Deleted breakpoint "));
12178 else
12179 printf_unfiltered (_("Deleted breakpoints "));
12180 }
d6e956e5
VP
12181
12182 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12183 {
c5aa993b 12184 if (from_tty)
d6e956e5
VP
12185 printf_unfiltered ("%d ", b->number);
12186 delete_breakpoint (b);
c906108c 12187 }
80f8a6eb
MS
12188 if (from_tty)
12189 putchar_unfiltered ('\n');
8a2c437b
TT
12190
12191 do_cleanups (cleanups);
c906108c
SS
12192}
12193\f
12194/* Delete breakpoint in BS if they are `delete' breakpoints and
12195 all breakpoints that are marked for deletion, whether hit or not.
12196 This is called after any breakpoint is hit, or after errors. */
12197
12198void
fba45db2 12199breakpoint_auto_delete (bpstat bs)
c906108c 12200{
35df4500 12201 struct breakpoint *b, *b_tmp;
c906108c
SS
12202
12203 for (; bs; bs = bs->next)
f431efe5
PA
12204 if (bs->breakpoint_at
12205 && bs->breakpoint_at->disposition == disp_del
c906108c 12206 && bs->stop)
f431efe5 12207 delete_breakpoint (bs->breakpoint_at);
c906108c 12208
35df4500 12209 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12210 {
b5de0fa7 12211 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12212 delete_breakpoint (b);
12213 }
c906108c
SS
12214}
12215
4a64f543
MS
12216/* A comparison function for bp_location AP and BP being interfaced to
12217 qsort. Sort elements primarily by their ADDRESS (no matter what
12218 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12219 secondarily by ordering first permanent elements and
4a64f543 12220 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12221 qsort being an unstable algorithm. */
876fa593
JK
12222
12223static int
494cfb0f 12224bp_location_compare (const void *ap, const void *bp)
876fa593 12225{
9a3c8263
SM
12226 const struct bp_location *a = *(const struct bp_location **) ap;
12227 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12228
12229 if (a->address != b->address)
12230 return (a->address > b->address) - (a->address < b->address);
12231
dea2aa5f
LM
12232 /* Sort locations at the same address by their pspace number, keeping
12233 locations of the same inferior (in a multi-inferior environment)
12234 grouped. */
12235
12236 if (a->pspace->num != b->pspace->num)
12237 return ((a->pspace->num > b->pspace->num)
12238 - (a->pspace->num < b->pspace->num));
12239
876fa593 12240 /* Sort permanent breakpoints first. */
1a853c52
PA
12241 if (a->permanent != b->permanent)
12242 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12243
c56a97f9
JK
12244 /* Make the internal GDB representation stable across GDB runs
12245 where A and B memory inside GDB can differ. Breakpoint locations of
12246 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12247
12248 if (a->owner->number != b->owner->number)
c56a97f9
JK
12249 return ((a->owner->number > b->owner->number)
12250 - (a->owner->number < b->owner->number));
876fa593
JK
12251
12252 return (a > b) - (a < b);
12253}
12254
876fa593 12255/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12256 bp_location_shadow_len_after_address_max according to the current
12257 content of the bp_location array. */
f7545552
TT
12258
12259static void
876fa593 12260bp_location_target_extensions_update (void)
f7545552 12261{
876fa593
JK
12262 struct bp_location *bl, **blp_tmp;
12263
12264 bp_location_placed_address_before_address_max = 0;
12265 bp_location_shadow_len_after_address_max = 0;
12266
12267 ALL_BP_LOCATIONS (bl, blp_tmp)
12268 {
12269 CORE_ADDR start, end, addr;
12270
12271 if (!bp_location_has_shadow (bl))
12272 continue;
12273
12274 start = bl->target_info.placed_address;
12275 end = start + bl->target_info.shadow_len;
12276
12277 gdb_assert (bl->address >= start);
12278 addr = bl->address - start;
12279 if (addr > bp_location_placed_address_before_address_max)
12280 bp_location_placed_address_before_address_max = addr;
12281
12282 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12283
12284 gdb_assert (bl->address < end);
12285 addr = end - bl->address;
12286 if (addr > bp_location_shadow_len_after_address_max)
12287 bp_location_shadow_len_after_address_max = addr;
12288 }
f7545552
TT
12289}
12290
1e4d1764
YQ
12291/* Download tracepoint locations if they haven't been. */
12292
12293static void
12294download_tracepoint_locations (void)
12295{
7ed2c994 12296 struct breakpoint *b;
1e4d1764 12297 struct cleanup *old_chain;
dd2e65cc 12298 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12299
12300 old_chain = save_current_space_and_thread ();
12301
7ed2c994 12302 ALL_TRACEPOINTS (b)
1e4d1764 12303 {
7ed2c994 12304 struct bp_location *bl;
1e4d1764 12305 struct tracepoint *t;
f2a8bc8a 12306 int bp_location_downloaded = 0;
1e4d1764 12307
7ed2c994 12308 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12309 ? !may_insert_fast_tracepoints
12310 : !may_insert_tracepoints))
12311 continue;
12312
dd2e65cc
YQ
12313 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12314 {
12315 if (target_can_download_tracepoint ())
12316 can_download_tracepoint = TRIBOOL_TRUE;
12317 else
12318 can_download_tracepoint = TRIBOOL_FALSE;
12319 }
12320
12321 if (can_download_tracepoint == TRIBOOL_FALSE)
12322 break;
12323
7ed2c994
YQ
12324 for (bl = b->loc; bl; bl = bl->next)
12325 {
12326 /* In tracepoint, locations are _never_ duplicated, so
12327 should_be_inserted is equivalent to
12328 unduplicated_should_be_inserted. */
12329 if (!should_be_inserted (bl) || bl->inserted)
12330 continue;
1e4d1764 12331
7ed2c994 12332 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12333
7ed2c994 12334 target_download_tracepoint (bl);
1e4d1764 12335
7ed2c994 12336 bl->inserted = 1;
f2a8bc8a 12337 bp_location_downloaded = 1;
7ed2c994
YQ
12338 }
12339 t = (struct tracepoint *) b;
12340 t->number_on_target = b->number;
f2a8bc8a
YQ
12341 if (bp_location_downloaded)
12342 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12343 }
12344
12345 do_cleanups (old_chain);
12346}
12347
934709f0
PW
12348/* Swap the insertion/duplication state between two locations. */
12349
12350static void
12351swap_insertion (struct bp_location *left, struct bp_location *right)
12352{
12353 const int left_inserted = left->inserted;
12354 const int left_duplicate = left->duplicate;
b775012e 12355 const int left_needs_update = left->needs_update;
934709f0
PW
12356 const struct bp_target_info left_target_info = left->target_info;
12357
1e4d1764
YQ
12358 /* Locations of tracepoints can never be duplicated. */
12359 if (is_tracepoint (left->owner))
12360 gdb_assert (!left->duplicate);
12361 if (is_tracepoint (right->owner))
12362 gdb_assert (!right->duplicate);
12363
934709f0
PW
12364 left->inserted = right->inserted;
12365 left->duplicate = right->duplicate;
b775012e 12366 left->needs_update = right->needs_update;
934709f0
PW
12367 left->target_info = right->target_info;
12368 right->inserted = left_inserted;
12369 right->duplicate = left_duplicate;
b775012e 12370 right->needs_update = left_needs_update;
934709f0
PW
12371 right->target_info = left_target_info;
12372}
12373
b775012e
LM
12374/* Force the re-insertion of the locations at ADDRESS. This is called
12375 once a new/deleted/modified duplicate location is found and we are evaluating
12376 conditions on the target's side. Such conditions need to be updated on
12377 the target. */
12378
12379static void
12380force_breakpoint_reinsertion (struct bp_location *bl)
12381{
12382 struct bp_location **locp = NULL, **loc2p;
12383 struct bp_location *loc;
12384 CORE_ADDR address = 0;
12385 int pspace_num;
12386
12387 address = bl->address;
12388 pspace_num = bl->pspace->num;
12389
12390 /* This is only meaningful if the target is
12391 evaluating conditions and if the user has
12392 opted for condition evaluation on the target's
12393 side. */
12394 if (gdb_evaluates_breakpoint_condition_p ()
12395 || !target_supports_evaluation_of_breakpoint_conditions ())
12396 return;
12397
12398 /* Flag all breakpoint locations with this address and
12399 the same program space as the location
12400 as "its condition has changed". We need to
12401 update the conditions on the target's side. */
12402 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12403 {
12404 loc = *loc2p;
12405
12406 if (!is_breakpoint (loc->owner)
12407 || pspace_num != loc->pspace->num)
12408 continue;
12409
12410 /* Flag the location appropriately. We use a different state to
12411 let everyone know that we already updated the set of locations
12412 with addr bl->address and program space bl->pspace. This is so
12413 we don't have to keep calling these functions just to mark locations
12414 that have already been marked. */
12415 loc->condition_changed = condition_updated;
12416
12417 /* Free the agent expression bytecode as well. We will compute
12418 it later on. */
12419 if (loc->cond_bytecode)
12420 {
12421 free_agent_expr (loc->cond_bytecode);
12422 loc->cond_bytecode = NULL;
12423 }
12424 }
12425}
44702360
PA
12426/* Called whether new breakpoints are created, or existing breakpoints
12427 deleted, to update the global location list and recompute which
12428 locations are duplicate of which.
b775012e 12429
04086b45
PA
12430 The INSERT_MODE flag determines whether locations may not, may, or
12431 shall be inserted now. See 'enum ugll_insert_mode' for more
12432 info. */
b60e7edf 12433
0d381245 12434static void
44702360 12435update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12436{
74960c60 12437 struct breakpoint *b;
876fa593 12438 struct bp_location **locp, *loc;
f7545552 12439 struct cleanup *cleanups;
b775012e
LM
12440 /* Last breakpoint location address that was marked for update. */
12441 CORE_ADDR last_addr = 0;
12442 /* Last breakpoint location program space that was marked for update. */
12443 int last_pspace_num = -1;
f7545552 12444
2d134ed3
PA
12445 /* Used in the duplicates detection below. When iterating over all
12446 bp_locations, points to the first bp_location of a given address.
12447 Breakpoints and watchpoints of different types are never
12448 duplicates of each other. Keep one pointer for each type of
12449 breakpoint/watchpoint, so we only need to loop over all locations
12450 once. */
12451 struct bp_location *bp_loc_first; /* breakpoint */
12452 struct bp_location *wp_loc_first; /* hardware watchpoint */
12453 struct bp_location *awp_loc_first; /* access watchpoint */
12454 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12455
4a64f543
MS
12456 /* Saved former bp_location array which we compare against the newly
12457 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12458 struct bp_location **old_location, **old_locp;
12459 unsigned old_location_count;
12460
12461 old_location = bp_location;
12462 old_location_count = bp_location_count;
12463 bp_location = NULL;
12464 bp_location_count = 0;
12465 cleanups = make_cleanup (xfree, old_location);
0d381245 12466
74960c60 12467 ALL_BREAKPOINTS (b)
876fa593
JK
12468 for (loc = b->loc; loc; loc = loc->next)
12469 bp_location_count++;
12470
8d749320 12471 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12472 locp = bp_location;
12473 ALL_BREAKPOINTS (b)
12474 for (loc = b->loc; loc; loc = loc->next)
12475 *locp++ = loc;
12476 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12477 bp_location_compare);
876fa593
JK
12478
12479 bp_location_target_extensions_update ();
74960c60 12480
4a64f543
MS
12481 /* Identify bp_location instances that are no longer present in the
12482 new list, and therefore should be freed. Note that it's not
12483 necessary that those locations should be removed from inferior --
12484 if there's another location at the same address (previously
12485 marked as duplicate), we don't need to remove/insert the
12486 location.
876fa593 12487
4a64f543
MS
12488 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12489 and former bp_location array state respectively. */
876fa593
JK
12490
12491 locp = bp_location;
12492 for (old_locp = old_location; old_locp < old_location + old_location_count;
12493 old_locp++)
74960c60 12494 {
876fa593 12495 struct bp_location *old_loc = *old_locp;
c7d46a38 12496 struct bp_location **loc2p;
876fa593 12497
e5dd4106 12498 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12499 not, we have to free it. */
c7d46a38 12500 int found_object = 0;
20874c92
VP
12501 /* Tells if the location should remain inserted in the target. */
12502 int keep_in_target = 0;
12503 int removed = 0;
876fa593 12504
4a64f543
MS
12505 /* Skip LOCP entries which will definitely never be needed.
12506 Stop either at or being the one matching OLD_LOC. */
876fa593 12507 while (locp < bp_location + bp_location_count
c7d46a38 12508 && (*locp)->address < old_loc->address)
876fa593 12509 locp++;
c7d46a38
PA
12510
12511 for (loc2p = locp;
12512 (loc2p < bp_location + bp_location_count
12513 && (*loc2p)->address == old_loc->address);
12514 loc2p++)
12515 {
b775012e
LM
12516 /* Check if this is a new/duplicated location or a duplicated
12517 location that had its condition modified. If so, we want to send
12518 its condition to the target if evaluation of conditions is taking
12519 place there. */
12520 if ((*loc2p)->condition_changed == condition_modified
12521 && (last_addr != old_loc->address
12522 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12523 {
b775012e
LM
12524 force_breakpoint_reinsertion (*loc2p);
12525 last_pspace_num = old_loc->pspace->num;
c7d46a38 12526 }
b775012e
LM
12527
12528 if (*loc2p == old_loc)
12529 found_object = 1;
c7d46a38 12530 }
74960c60 12531
b775012e
LM
12532 /* We have already handled this address, update it so that we don't
12533 have to go through updates again. */
12534 last_addr = old_loc->address;
12535
12536 /* Target-side condition evaluation: Handle deleted locations. */
12537 if (!found_object)
12538 force_breakpoint_reinsertion (old_loc);
12539
4a64f543
MS
12540 /* If this location is no longer present, and inserted, look if
12541 there's maybe a new location at the same address. If so,
12542 mark that one inserted, and don't remove this one. This is
12543 needed so that we don't have a time window where a breakpoint
12544 at certain location is not inserted. */
74960c60 12545
876fa593 12546 if (old_loc->inserted)
0d381245 12547 {
4a64f543
MS
12548 /* If the location is inserted now, we might have to remove
12549 it. */
74960c60 12550
876fa593 12551 if (found_object && should_be_inserted (old_loc))
74960c60 12552 {
4a64f543
MS
12553 /* The location is still present in the location list,
12554 and still should be inserted. Don't do anything. */
20874c92 12555 keep_in_target = 1;
74960c60
VP
12556 }
12557 else
12558 {
b775012e
LM
12559 /* This location still exists, but it won't be kept in the
12560 target since it may have been disabled. We proceed to
12561 remove its target-side condition. */
12562
4a64f543
MS
12563 /* The location is either no longer present, or got
12564 disabled. See if there's another location at the
12565 same address, in which case we don't need to remove
12566 this one from the target. */
876fa593 12567
2bdf28a0 12568 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12569 if (breakpoint_address_is_meaningful (old_loc->owner))
12570 {
876fa593 12571 for (loc2p = locp;
c7d46a38
PA
12572 (loc2p < bp_location + bp_location_count
12573 && (*loc2p)->address == old_loc->address);
876fa593
JK
12574 loc2p++)
12575 {
12576 struct bp_location *loc2 = *loc2p;
12577
2d134ed3 12578 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12579 {
85d721b8
PA
12580 /* Read watchpoint locations are switched to
12581 access watchpoints, if the former are not
12582 supported, but the latter are. */
12583 if (is_hardware_watchpoint (old_loc->owner))
12584 {
12585 gdb_assert (is_hardware_watchpoint (loc2->owner));
12586 loc2->watchpoint_type = old_loc->watchpoint_type;
12587 }
12588
934709f0
PW
12589 /* loc2 is a duplicated location. We need to check
12590 if it should be inserted in case it will be
12591 unduplicated. */
12592 if (loc2 != old_loc
12593 && unduplicated_should_be_inserted (loc2))
c7d46a38 12594 {
934709f0 12595 swap_insertion (old_loc, loc2);
c7d46a38
PA
12596 keep_in_target = 1;
12597 break;
12598 }
876fa593
JK
12599 }
12600 }
12601 }
74960c60
VP
12602 }
12603
20874c92
VP
12604 if (!keep_in_target)
12605 {
834c0d03 12606 if (remove_breakpoint (old_loc))
20874c92 12607 {
4a64f543
MS
12608 /* This is just about all we can do. We could keep
12609 this location on the global list, and try to
12610 remove it next time, but there's no particular
12611 reason why we will succeed next time.
20874c92 12612
4a64f543
MS
12613 Note that at this point, old_loc->owner is still
12614 valid, as delete_breakpoint frees the breakpoint
12615 only after calling us. */
3e43a32a
MS
12616 printf_filtered (_("warning: Error removing "
12617 "breakpoint %d\n"),
876fa593 12618 old_loc->owner->number);
20874c92
VP
12619 }
12620 removed = 1;
12621 }
0d381245 12622 }
74960c60
VP
12623
12624 if (!found_object)
1c5cfe86 12625 {
fbea99ea 12626 if (removed && target_is_non_stop_p ()
1cf4d951 12627 && need_moribund_for_location_type (old_loc))
20874c92 12628 {
db82e815
PA
12629 /* This location was removed from the target. In
12630 non-stop mode, a race condition is possible where
12631 we've removed a breakpoint, but stop events for that
12632 breakpoint are already queued and will arrive later.
12633 We apply an heuristic to be able to distinguish such
12634 SIGTRAPs from other random SIGTRAPs: we keep this
12635 breakpoint location for a bit, and will retire it
12636 after we see some number of events. The theory here
12637 is that reporting of events should, "on the average",
12638 be fair, so after a while we'll see events from all
12639 threads that have anything of interest, and no longer
12640 need to keep this breakpoint location around. We
12641 don't hold locations forever so to reduce chances of
12642 mistaking a non-breakpoint SIGTRAP for a breakpoint
12643 SIGTRAP.
12644
12645 The heuristic failing can be disastrous on
12646 decr_pc_after_break targets.
12647
12648 On decr_pc_after_break targets, like e.g., x86-linux,
12649 if we fail to recognize a late breakpoint SIGTRAP,
12650 because events_till_retirement has reached 0 too
12651 soon, we'll fail to do the PC adjustment, and report
12652 a random SIGTRAP to the user. When the user resumes
12653 the inferior, it will most likely immediately crash
2dec564e 12654 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12655 corrupted, because of being resumed e.g., in the
12656 middle of a multi-byte instruction, or skipped a
12657 one-byte instruction. This was actually seen happen
12658 on native x86-linux, and should be less rare on
12659 targets that do not support new thread events, like
12660 remote, due to the heuristic depending on
12661 thread_count.
12662
12663 Mistaking a random SIGTRAP for a breakpoint trap
12664 causes similar symptoms (PC adjustment applied when
12665 it shouldn't), but then again, playing with SIGTRAPs
12666 behind the debugger's back is asking for trouble.
12667
12668 Since hardware watchpoint traps are always
12669 distinguishable from other traps, so we don't need to
12670 apply keep hardware watchpoint moribund locations
12671 around. We simply always ignore hardware watchpoint
12672 traps we can no longer explain. */
12673
876fa593
JK
12674 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12675 old_loc->owner = NULL;
20874c92 12676
876fa593 12677 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12678 }
12679 else
f431efe5
PA
12680 {
12681 old_loc->owner = NULL;
12682 decref_bp_location (&old_loc);
12683 }
20874c92 12684 }
74960c60 12685 }
1c5cfe86 12686
348d480f
PA
12687 /* Rescan breakpoints at the same address and section, marking the
12688 first one as "first" and any others as "duplicates". This is so
12689 that the bpt instruction is only inserted once. If we have a
12690 permanent breakpoint at the same place as BPT, make that one the
12691 official one, and the rest as duplicates. Permanent breakpoints
12692 are sorted first for the same address.
12693
12694 Do the same for hardware watchpoints, but also considering the
12695 watchpoint's type (regular/access/read) and length. */
12696
12697 bp_loc_first = NULL;
12698 wp_loc_first = NULL;
12699 awp_loc_first = NULL;
12700 rwp_loc_first = NULL;
12701 ALL_BP_LOCATIONS (loc, locp)
12702 {
12703 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12704 non-NULL. */
348d480f 12705 struct bp_location **loc_first_p;
d3fbdd86 12706 b = loc->owner;
348d480f 12707
6f380991 12708 if (!unduplicated_should_be_inserted (loc)
348d480f 12709 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12710 /* Don't detect duplicate for tracepoint locations because they are
12711 never duplicated. See the comments in field `duplicate' of
12712 `struct bp_location'. */
348d480f 12713 || is_tracepoint (b))
b775012e
LM
12714 {
12715 /* Clear the condition modification flag. */
12716 loc->condition_changed = condition_unchanged;
12717 continue;
12718 }
348d480f 12719
348d480f
PA
12720 if (b->type == bp_hardware_watchpoint)
12721 loc_first_p = &wp_loc_first;
12722 else if (b->type == bp_read_watchpoint)
12723 loc_first_p = &rwp_loc_first;
12724 else if (b->type == bp_access_watchpoint)
12725 loc_first_p = &awp_loc_first;
12726 else
12727 loc_first_p = &bp_loc_first;
12728
12729 if (*loc_first_p == NULL
12730 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12731 || !breakpoint_locations_match (loc, *loc_first_p))
12732 {
12733 *loc_first_p = loc;
12734 loc->duplicate = 0;
b775012e
LM
12735
12736 if (is_breakpoint (loc->owner) && loc->condition_changed)
12737 {
12738 loc->needs_update = 1;
12739 /* Clear the condition modification flag. */
12740 loc->condition_changed = condition_unchanged;
12741 }
348d480f
PA
12742 continue;
12743 }
12744
934709f0
PW
12745
12746 /* This and the above ensure the invariant that the first location
12747 is not duplicated, and is the inserted one.
12748 All following are marked as duplicated, and are not inserted. */
12749 if (loc->inserted)
12750 swap_insertion (loc, *loc_first_p);
348d480f
PA
12751 loc->duplicate = 1;
12752
b775012e
LM
12753 /* Clear the condition modification flag. */
12754 loc->condition_changed = condition_unchanged;
348d480f
PA
12755 }
12756
a25a5a45 12757 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12758 {
04086b45 12759 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12760 insert_breakpoint_locations ();
12761 else
12762 {
44702360
PA
12763 /* Even though the caller told us to not insert new
12764 locations, we may still need to update conditions on the
12765 target's side of breakpoints that were already inserted
12766 if the target is evaluating breakpoint conditions. We
b775012e
LM
12767 only update conditions for locations that are marked
12768 "needs_update". */
12769 update_inserted_breakpoint_locations ();
12770 }
12771 }
348d480f 12772
04086b45 12773 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12774 download_tracepoint_locations ();
12775
348d480f
PA
12776 do_cleanups (cleanups);
12777}
12778
12779void
12780breakpoint_retire_moribund (void)
12781{
12782 struct bp_location *loc;
12783 int ix;
12784
12785 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12786 if (--(loc->events_till_retirement) == 0)
12787 {
12788 decref_bp_location (&loc);
12789 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12790 --ix;
12791 }
12792}
12793
12794static void
44702360 12795update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12796{
348d480f 12797
492d29ea
PA
12798 TRY
12799 {
12800 update_global_location_list (insert_mode);
12801 }
12802 CATCH (e, RETURN_MASK_ERROR)
12803 {
12804 }
12805 END_CATCH
348d480f
PA
12806}
12807
12808/* Clear BKP from a BPS. */
12809
12810static void
12811bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12812{
12813 bpstat bs;
12814
12815 for (bs = bps; bs; bs = bs->next)
12816 if (bs->breakpoint_at == bpt)
12817 {
12818 bs->breakpoint_at = NULL;
12819 bs->old_val = NULL;
12820 /* bs->commands will be freed later. */
12821 }
12822}
12823
12824/* Callback for iterate_over_threads. */
12825static int
12826bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12827{
9a3c8263 12828 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12829
12830 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12831 return 0;
12832}
12833
12834/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12835 callbacks. */
12836
12837static void
12838say_where (struct breakpoint *b)
12839{
12840 struct value_print_options opts;
12841
12842 get_user_print_options (&opts);
12843
12844 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12845 single string. */
12846 if (b->loc == NULL)
12847 {
f00aae0f
KS
12848 /* For pending locations, the output differs slightly based
12849 on b->extra_string. If this is non-NULL, it contains either
12850 a condition or dprintf arguments. */
12851 if (b->extra_string == NULL)
12852 {
12853 printf_filtered (_(" (%s) pending."),
12854 event_location_to_string (b->location));
12855 }
12856 else if (b->type == bp_dprintf)
12857 {
12858 printf_filtered (_(" (%s,%s) pending."),
12859 event_location_to_string (b->location),
12860 b->extra_string);
12861 }
12862 else
12863 {
12864 printf_filtered (_(" (%s %s) pending."),
12865 event_location_to_string (b->location),
12866 b->extra_string);
12867 }
348d480f
PA
12868 }
12869 else
12870 {
2f202fde 12871 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12872 {
12873 printf_filtered (" at ");
12874 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12875 gdb_stdout);
12876 }
2f202fde 12877 if (b->loc->symtab != NULL)
f8eba3c6
TT
12878 {
12879 /* If there is a single location, we can print the location
12880 more nicely. */
12881 if (b->loc->next == NULL)
12882 printf_filtered (": file %s, line %d.",
05cba821
JK
12883 symtab_to_filename_for_display (b->loc->symtab),
12884 b->loc->line_number);
f8eba3c6
TT
12885 else
12886 /* This is not ideal, but each location may have a
12887 different file name, and this at least reflects the
12888 real situation somewhat. */
f00aae0f
KS
12889 printf_filtered (": %s.",
12890 event_location_to_string (b->location));
f8eba3c6 12891 }
348d480f
PA
12892
12893 if (b->loc->next)
12894 {
12895 struct bp_location *loc = b->loc;
12896 int n = 0;
12897 for (; loc; loc = loc->next)
12898 ++n;
12899 printf_filtered (" (%d locations)", n);
12900 }
12901 }
12902}
12903
348d480f
PA
12904/* Default bp_location_ops methods. */
12905
12906static void
12907bp_location_dtor (struct bp_location *self)
12908{
12909 xfree (self->cond);
b775012e
LM
12910 if (self->cond_bytecode)
12911 free_agent_expr (self->cond_bytecode);
348d480f 12912 xfree (self->function_name);
8b4f3082
PA
12913
12914 VEC_free (agent_expr_p, self->target_info.conditions);
12915 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12916}
12917
12918static const struct bp_location_ops bp_location_ops =
12919{
12920 bp_location_dtor
12921};
12922
2060206e
PA
12923/* Default breakpoint_ops methods all breakpoint_ops ultimately
12924 inherit from. */
348d480f 12925
2060206e
PA
12926static void
12927base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12928{
12929 decref_counted_command_line (&self->commands);
12930 xfree (self->cond_string);
fb81d016 12931 xfree (self->extra_string);
f8eba3c6 12932 xfree (self->filter);
f00aae0f
KS
12933 delete_event_location (self->location);
12934 delete_event_location (self->location_range_end);
348d480f
PA
12935}
12936
2060206e
PA
12937static struct bp_location *
12938base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12939{
12940 struct bp_location *loc;
12941
12942 loc = XNEW (struct bp_location);
12943 init_bp_location (loc, &bp_location_ops, self);
12944 return loc;
12945}
12946
2060206e
PA
12947static void
12948base_breakpoint_re_set (struct breakpoint *b)
12949{
12950 /* Nothing to re-set. */
12951}
12952
12953#define internal_error_pure_virtual_called() \
12954 gdb_assert_not_reached ("pure virtual function called")
12955
12956static int
12957base_breakpoint_insert_location (struct bp_location *bl)
12958{
12959 internal_error_pure_virtual_called ();
12960}
12961
12962static int
73971819
PA
12963base_breakpoint_remove_location (struct bp_location *bl,
12964 enum remove_bp_reason reason)
2060206e
PA
12965{
12966 internal_error_pure_virtual_called ();
12967}
12968
12969static int
12970base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12971 struct address_space *aspace,
09ac7c10
TT
12972 CORE_ADDR bp_addr,
12973 const struct target_waitstatus *ws)
2060206e
PA
12974{
12975 internal_error_pure_virtual_called ();
12976}
12977
12978static void
12979base_breakpoint_check_status (bpstat bs)
12980{
12981 /* Always stop. */
12982}
12983
12984/* A "works_in_software_mode" breakpoint_ops method that just internal
12985 errors. */
12986
12987static int
12988base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12989{
12990 internal_error_pure_virtual_called ();
12991}
12992
12993/* A "resources_needed" breakpoint_ops method that just internal
12994 errors. */
12995
12996static int
12997base_breakpoint_resources_needed (const struct bp_location *bl)
12998{
12999 internal_error_pure_virtual_called ();
13000}
13001
13002static enum print_stop_action
13003base_breakpoint_print_it (bpstat bs)
13004{
13005 internal_error_pure_virtual_called ();
13006}
13007
13008static void
13009base_breakpoint_print_one_detail (const struct breakpoint *self,
13010 struct ui_out *uiout)
13011{
13012 /* nothing */
13013}
13014
13015static void
13016base_breakpoint_print_mention (struct breakpoint *b)
13017{
13018 internal_error_pure_virtual_called ();
13019}
13020
13021static void
13022base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13023{
13024 internal_error_pure_virtual_called ();
13025}
13026
983af33b 13027static void
f00aae0f
KS
13028base_breakpoint_create_sals_from_location
13029 (const struct event_location *location,
13030 struct linespec_result *canonical,
13031 enum bptype type_wanted)
983af33b
SDJ
13032{
13033 internal_error_pure_virtual_called ();
13034}
13035
13036static void
13037base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13038 struct linespec_result *c,
983af33b 13039 char *cond_string,
e7e0cddf 13040 char *extra_string,
983af33b
SDJ
13041 enum bptype type_wanted,
13042 enum bpdisp disposition,
13043 int thread,
13044 int task, int ignore_count,
13045 const struct breakpoint_ops *o,
13046 int from_tty, int enabled,
44f238bb 13047 int internal, unsigned flags)
983af33b
SDJ
13048{
13049 internal_error_pure_virtual_called ();
13050}
13051
13052static void
f00aae0f
KS
13053base_breakpoint_decode_location (struct breakpoint *b,
13054 const struct event_location *location,
c2f4122d 13055 struct program_space *search_pspace,
983af33b
SDJ
13056 struct symtabs_and_lines *sals)
13057{
13058 internal_error_pure_virtual_called ();
13059}
13060
ab04a2af
TT
13061/* The default 'explains_signal' method. */
13062
47591c29 13063static int
427cd150 13064base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13065{
47591c29 13066 return 1;
ab04a2af
TT
13067}
13068
9d6e6e84
HZ
13069/* The default "after_condition_true" method. */
13070
13071static void
13072base_breakpoint_after_condition_true (struct bpstats *bs)
13073{
13074 /* Nothing to do. */
13075}
13076
ab04a2af 13077struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13078{
13079 base_breakpoint_dtor,
13080 base_breakpoint_allocate_location,
13081 base_breakpoint_re_set,
13082 base_breakpoint_insert_location,
13083 base_breakpoint_remove_location,
13084 base_breakpoint_breakpoint_hit,
13085 base_breakpoint_check_status,
13086 base_breakpoint_resources_needed,
13087 base_breakpoint_works_in_software_mode,
13088 base_breakpoint_print_it,
13089 NULL,
13090 base_breakpoint_print_one_detail,
13091 base_breakpoint_print_mention,
983af33b 13092 base_breakpoint_print_recreate,
5f700d83 13093 base_breakpoint_create_sals_from_location,
983af33b 13094 base_breakpoint_create_breakpoints_sal,
5f700d83 13095 base_breakpoint_decode_location,
9d6e6e84
HZ
13096 base_breakpoint_explains_signal,
13097 base_breakpoint_after_condition_true,
2060206e
PA
13098};
13099
13100/* Default breakpoint_ops methods. */
13101
13102static void
348d480f
PA
13103bkpt_re_set (struct breakpoint *b)
13104{
06edf0c0 13105 /* FIXME: is this still reachable? */
9ef9e6a6 13106 if (breakpoint_event_location_empty_p (b))
06edf0c0 13107 {
f00aae0f 13108 /* Anything without a location can't be re-set. */
348d480f 13109 delete_breakpoint (b);
06edf0c0 13110 return;
348d480f 13111 }
06edf0c0
PA
13112
13113 breakpoint_re_set_default (b);
348d480f
PA
13114}
13115
2060206e 13116static int
348d480f
PA
13117bkpt_insert_location (struct bp_location *bl)
13118{
13119 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13120 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13121 else
7c16b83e 13122 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13123}
13124
2060206e 13125static int
73971819 13126bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
13127{
13128 if (bl->loc_type == bp_loc_hardware_breakpoint)
13129 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13130 else
73971819 13131 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
13132}
13133
2060206e 13134static int
348d480f 13135bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13136 struct address_space *aspace, CORE_ADDR bp_addr,
13137 const struct target_waitstatus *ws)
348d480f 13138{
09ac7c10 13139 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13140 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13141 return 0;
13142
348d480f
PA
13143 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13144 aspace, bp_addr))
13145 return 0;
13146
13147 if (overlay_debugging /* unmapped overlay section */
13148 && section_is_overlay (bl->section)
13149 && !section_is_mapped (bl->section))
13150 return 0;
13151
13152 return 1;
13153}
13154
cd1608cc
PA
13155static int
13156dprintf_breakpoint_hit (const struct bp_location *bl,
13157 struct address_space *aspace, CORE_ADDR bp_addr,
13158 const struct target_waitstatus *ws)
13159{
13160 if (dprintf_style == dprintf_style_agent
13161 && target_can_run_breakpoint_commands ())
13162 {
13163 /* An agent-style dprintf never causes a stop. If we see a trap
13164 for this address it must be for a breakpoint that happens to
13165 be set at the same address. */
13166 return 0;
13167 }
13168
13169 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13170}
13171
2060206e 13172static int
348d480f
PA
13173bkpt_resources_needed (const struct bp_location *bl)
13174{
13175 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13176
13177 return 1;
13178}
13179
2060206e 13180static enum print_stop_action
348d480f
PA
13181bkpt_print_it (bpstat bs)
13182{
348d480f
PA
13183 struct breakpoint *b;
13184 const struct bp_location *bl;
001c8c33 13185 int bp_temp;
79a45e25 13186 struct ui_out *uiout = current_uiout;
348d480f
PA
13187
13188 gdb_assert (bs->bp_location_at != NULL);
13189
13190 bl = bs->bp_location_at;
13191 b = bs->breakpoint_at;
13192
001c8c33
PA
13193 bp_temp = b->disposition == disp_del;
13194 if (bl->address != bl->requested_address)
13195 breakpoint_adjustment_warning (bl->requested_address,
13196 bl->address,
13197 b->number, 1);
13198 annotate_breakpoint (b->number);
f303dbd6
PA
13199 maybe_print_thread_hit_breakpoint (uiout);
13200
001c8c33 13201 if (bp_temp)
f303dbd6 13202 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13203 else
f303dbd6 13204 ui_out_text (uiout, "Breakpoint ");
001c8c33 13205 if (ui_out_is_mi_like_p (uiout))
348d480f 13206 {
001c8c33
PA
13207 ui_out_field_string (uiout, "reason",
13208 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13209 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13210 }
001c8c33
PA
13211 ui_out_field_int (uiout, "bkptno", b->number);
13212 ui_out_text (uiout, ", ");
06edf0c0 13213
001c8c33 13214 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13215}
13216
2060206e 13217static void
06edf0c0
PA
13218bkpt_print_mention (struct breakpoint *b)
13219{
79a45e25 13220 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13221 return;
13222
13223 switch (b->type)
13224 {
13225 case bp_breakpoint:
13226 case bp_gnu_ifunc_resolver:
13227 if (b->disposition == disp_del)
13228 printf_filtered (_("Temporary breakpoint"));
13229 else
13230 printf_filtered (_("Breakpoint"));
13231 printf_filtered (_(" %d"), b->number);
13232 if (b->type == bp_gnu_ifunc_resolver)
13233 printf_filtered (_(" at gnu-indirect-function resolver"));
13234 break;
13235 case bp_hardware_breakpoint:
13236 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13237 break;
e7e0cddf
SS
13238 case bp_dprintf:
13239 printf_filtered (_("Dprintf %d"), b->number);
13240 break;
06edf0c0
PA
13241 }
13242
13243 say_where (b);
13244}
13245
2060206e 13246static void
06edf0c0
PA
13247bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13248{
13249 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13250 fprintf_unfiltered (fp, "tbreak");
13251 else if (tp->type == bp_breakpoint)
13252 fprintf_unfiltered (fp, "break");
13253 else if (tp->type == bp_hardware_breakpoint
13254 && tp->disposition == disp_del)
13255 fprintf_unfiltered (fp, "thbreak");
13256 else if (tp->type == bp_hardware_breakpoint)
13257 fprintf_unfiltered (fp, "hbreak");
13258 else
13259 internal_error (__FILE__, __LINE__,
13260 _("unhandled breakpoint type %d"), (int) tp->type);
13261
f00aae0f
KS
13262 fprintf_unfiltered (fp, " %s",
13263 event_location_to_string (tp->location));
13264
13265 /* Print out extra_string if this breakpoint is pending. It might
13266 contain, for example, conditions that were set by the user. */
13267 if (tp->loc == NULL && tp->extra_string != NULL)
13268 fprintf_unfiltered (fp, " %s", tp->extra_string);
13269
dd11a36c 13270 print_recreate_thread (tp, fp);
06edf0c0
PA
13271}
13272
983af33b 13273static void
f00aae0f
KS
13274bkpt_create_sals_from_location (const struct event_location *location,
13275 struct linespec_result *canonical,
13276 enum bptype type_wanted)
983af33b 13277{
f00aae0f 13278 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13279}
13280
13281static void
13282bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13283 struct linespec_result *canonical,
983af33b 13284 char *cond_string,
e7e0cddf 13285 char *extra_string,
983af33b
SDJ
13286 enum bptype type_wanted,
13287 enum bpdisp disposition,
13288 int thread,
13289 int task, int ignore_count,
13290 const struct breakpoint_ops *ops,
13291 int from_tty, int enabled,
44f238bb 13292 int internal, unsigned flags)
983af33b 13293{
023fa29b 13294 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13295 cond_string, extra_string,
13296 type_wanted,
983af33b
SDJ
13297 disposition, thread, task,
13298 ignore_count, ops, from_tty,
44f238bb 13299 enabled, internal, flags);
983af33b
SDJ
13300}
13301
13302static void
f00aae0f
KS
13303bkpt_decode_location (struct breakpoint *b,
13304 const struct event_location *location,
c2f4122d 13305 struct program_space *search_pspace,
983af33b
SDJ
13306 struct symtabs_and_lines *sals)
13307{
c2f4122d 13308 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13309}
13310
06edf0c0
PA
13311/* Virtual table for internal breakpoints. */
13312
13313static void
13314internal_bkpt_re_set (struct breakpoint *b)
13315{
13316 switch (b->type)
13317 {
13318 /* Delete overlay event and longjmp master breakpoints; they
13319 will be reset later by breakpoint_re_set. */
13320 case bp_overlay_event:
13321 case bp_longjmp_master:
13322 case bp_std_terminate_master:
13323 case bp_exception_master:
13324 delete_breakpoint (b);
13325 break;
13326
13327 /* This breakpoint is special, it's set up when the inferior
13328 starts and we really don't want to touch it. */
13329 case bp_shlib_event:
13330
13331 /* Like bp_shlib_event, this breakpoint type is special. Once
13332 it is set up, we do not want to touch it. */
13333 case bp_thread_event:
13334 break;
13335 }
13336}
13337
13338static void
13339internal_bkpt_check_status (bpstat bs)
13340{
a9b3a50f
PA
13341 if (bs->breakpoint_at->type == bp_shlib_event)
13342 {
13343 /* If requested, stop when the dynamic linker notifies GDB of
13344 events. This allows the user to get control and place
13345 breakpoints in initializer routines for dynamically loaded
13346 objects (among other things). */
13347 bs->stop = stop_on_solib_events;
13348 bs->print = stop_on_solib_events;
13349 }
13350 else
13351 bs->stop = 0;
06edf0c0
PA
13352}
13353
13354static enum print_stop_action
13355internal_bkpt_print_it (bpstat bs)
13356{
06edf0c0 13357 struct breakpoint *b;
06edf0c0 13358
06edf0c0
PA
13359 b = bs->breakpoint_at;
13360
06edf0c0
PA
13361 switch (b->type)
13362 {
348d480f
PA
13363 case bp_shlib_event:
13364 /* Did we stop because the user set the stop_on_solib_events
13365 variable? (If so, we report this as a generic, "Stopped due
13366 to shlib event" message.) */
edcc5120 13367 print_solib_event (0);
348d480f
PA
13368 break;
13369
13370 case bp_thread_event:
13371 /* Not sure how we will get here.
13372 GDB should not stop for these breakpoints. */
13373 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13374 break;
13375
13376 case bp_overlay_event:
13377 /* By analogy with the thread event, GDB should not stop for these. */
13378 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13379 break;
13380
13381 case bp_longjmp_master:
13382 /* These should never be enabled. */
13383 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13384 break;
13385
13386 case bp_std_terminate_master:
13387 /* These should never be enabled. */
13388 printf_filtered (_("std::terminate Master Breakpoint: "
13389 "gdb should not stop!\n"));
348d480f
PA
13390 break;
13391
13392 case bp_exception_master:
13393 /* These should never be enabled. */
13394 printf_filtered (_("Exception Master Breakpoint: "
13395 "gdb should not stop!\n"));
06edf0c0
PA
13396 break;
13397 }
13398
001c8c33 13399 return PRINT_NOTHING;
06edf0c0
PA
13400}
13401
13402static void
13403internal_bkpt_print_mention (struct breakpoint *b)
13404{
13405 /* Nothing to mention. These breakpoints are internal. */
13406}
13407
06edf0c0
PA
13408/* Virtual table for momentary breakpoints */
13409
13410static void
13411momentary_bkpt_re_set (struct breakpoint *b)
13412{
13413 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13414 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13415 Otherwise these should have been blown away via the cleanup chain
13416 or by breakpoint_init_inferior when we rerun the executable. */
13417}
13418
13419static void
13420momentary_bkpt_check_status (bpstat bs)
13421{
13422 /* Nothing. The point of these breakpoints is causing a stop. */
13423}
13424
13425static enum print_stop_action
13426momentary_bkpt_print_it (bpstat bs)
13427{
001c8c33 13428 return PRINT_UNKNOWN;
348d480f
PA
13429}
13430
06edf0c0
PA
13431static void
13432momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13433{
06edf0c0 13434 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13435}
13436
e2e4d78b
JK
13437/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13438
13439 It gets cleared already on the removal of the first one of such placed
13440 breakpoints. This is OK as they get all removed altogether. */
13441
13442static void
13443longjmp_bkpt_dtor (struct breakpoint *self)
13444{
5d5658a1 13445 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13446
13447 if (tp)
13448 tp->initiating_frame = null_frame_id;
13449
13450 momentary_breakpoint_ops.dtor (self);
13451}
13452
55aa24fb
SDJ
13453/* Specific methods for probe breakpoints. */
13454
13455static int
13456bkpt_probe_insert_location (struct bp_location *bl)
13457{
13458 int v = bkpt_insert_location (bl);
13459
13460 if (v == 0)
13461 {
13462 /* The insertion was successful, now let's set the probe's semaphore
13463 if needed. */
0ea5cda8
SDJ
13464 if (bl->probe.probe->pops->set_semaphore != NULL)
13465 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13466 bl->probe.objfile,
13467 bl->gdbarch);
55aa24fb
SDJ
13468 }
13469
13470 return v;
13471}
13472
13473static int
73971819
PA
13474bkpt_probe_remove_location (struct bp_location *bl,
13475 enum remove_bp_reason reason)
55aa24fb
SDJ
13476{
13477 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13478 if (bl->probe.probe->pops->clear_semaphore != NULL)
13479 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13480 bl->probe.objfile,
13481 bl->gdbarch);
55aa24fb 13482
73971819 13483 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13484}
13485
13486static void
f00aae0f 13487bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13488 struct linespec_result *canonical,
f00aae0f 13489 enum bptype type_wanted)
55aa24fb
SDJ
13490{
13491 struct linespec_sals lsal;
13492
c2f4122d 13493 lsal.sals = parse_probes (location, NULL, canonical);
f00aae0f 13494 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13495 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13496}
13497
13498static void
f00aae0f
KS
13499bkpt_probe_decode_location (struct breakpoint *b,
13500 const struct event_location *location,
c2f4122d 13501 struct program_space *search_pspace,
55aa24fb
SDJ
13502 struct symtabs_and_lines *sals)
13503{
c2f4122d 13504 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13505 if (!sals->sals)
13506 error (_("probe not found"));
13507}
13508
348d480f 13509/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13510
348d480f
PA
13511static void
13512tracepoint_re_set (struct breakpoint *b)
13513{
13514 breakpoint_re_set_default (b);
13515}
876fa593 13516
348d480f
PA
13517static int
13518tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13519 struct address_space *aspace, CORE_ADDR bp_addr,
13520 const struct target_waitstatus *ws)
348d480f
PA
13521{
13522 /* By definition, the inferior does not report stops at
13523 tracepoints. */
13524 return 0;
74960c60
VP
13525}
13526
13527static void
348d480f
PA
13528tracepoint_print_one_detail (const struct breakpoint *self,
13529 struct ui_out *uiout)
74960c60 13530{
d9b3f62e
PA
13531 struct tracepoint *tp = (struct tracepoint *) self;
13532 if (tp->static_trace_marker_id)
348d480f
PA
13533 {
13534 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13535
348d480f
PA
13536 ui_out_text (uiout, "\tmarker id is ");
13537 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13538 tp->static_trace_marker_id);
348d480f
PA
13539 ui_out_text (uiout, "\n");
13540 }
0d381245
VP
13541}
13542
a474d7c2 13543static void
348d480f 13544tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13545{
79a45e25 13546 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13547 return;
cc59ec59 13548
348d480f
PA
13549 switch (b->type)
13550 {
13551 case bp_tracepoint:
13552 printf_filtered (_("Tracepoint"));
13553 printf_filtered (_(" %d"), b->number);
13554 break;
13555 case bp_fast_tracepoint:
13556 printf_filtered (_("Fast tracepoint"));
13557 printf_filtered (_(" %d"), b->number);
13558 break;
13559 case bp_static_tracepoint:
13560 printf_filtered (_("Static tracepoint"));
13561 printf_filtered (_(" %d"), b->number);
13562 break;
13563 default:
13564 internal_error (__FILE__, __LINE__,
13565 _("unhandled tracepoint type %d"), (int) b->type);
13566 }
13567
13568 say_where (b);
a474d7c2
PA
13569}
13570
348d480f 13571static void
d9b3f62e 13572tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13573{
d9b3f62e
PA
13574 struct tracepoint *tp = (struct tracepoint *) self;
13575
13576 if (self->type == bp_fast_tracepoint)
348d480f 13577 fprintf_unfiltered (fp, "ftrace");
c93e8391 13578 else if (self->type == bp_static_tracepoint)
348d480f 13579 fprintf_unfiltered (fp, "strace");
d9b3f62e 13580 else if (self->type == bp_tracepoint)
348d480f
PA
13581 fprintf_unfiltered (fp, "trace");
13582 else
13583 internal_error (__FILE__, __LINE__,
d9b3f62e 13584 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13585
f00aae0f
KS
13586 fprintf_unfiltered (fp, " %s",
13587 event_location_to_string (self->location));
d9b3f62e
PA
13588 print_recreate_thread (self, fp);
13589
13590 if (tp->pass_count)
13591 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13592}
13593
983af33b 13594static void
f00aae0f
KS
13595tracepoint_create_sals_from_location (const struct event_location *location,
13596 struct linespec_result *canonical,
13597 enum bptype type_wanted)
983af33b 13598{
f00aae0f 13599 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13600}
13601
13602static void
13603tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13604 struct linespec_result *canonical,
983af33b 13605 char *cond_string,
e7e0cddf 13606 char *extra_string,
983af33b
SDJ
13607 enum bptype type_wanted,
13608 enum bpdisp disposition,
13609 int thread,
13610 int task, int ignore_count,
13611 const struct breakpoint_ops *ops,
13612 int from_tty, int enabled,
44f238bb 13613 int internal, unsigned flags)
983af33b 13614{
023fa29b 13615 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13616 cond_string, extra_string,
13617 type_wanted,
983af33b
SDJ
13618 disposition, thread, task,
13619 ignore_count, ops, from_tty,
44f238bb 13620 enabled, internal, flags);
983af33b
SDJ
13621}
13622
13623static void
f00aae0f
KS
13624tracepoint_decode_location (struct breakpoint *b,
13625 const struct event_location *location,
c2f4122d 13626 struct program_space *search_pspace,
983af33b
SDJ
13627 struct symtabs_and_lines *sals)
13628{
c2f4122d 13629 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13630}
13631
2060206e 13632struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13633
55aa24fb
SDJ
13634/* The breakpoint_ops structure to be use on tracepoints placed in a
13635 static probe. */
13636
13637static void
f00aae0f
KS
13638tracepoint_probe_create_sals_from_location
13639 (const struct event_location *location,
13640 struct linespec_result *canonical,
13641 enum bptype type_wanted)
55aa24fb
SDJ
13642{
13643 /* We use the same method for breakpoint on probes. */
f00aae0f 13644 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13645}
13646
13647static void
f00aae0f
KS
13648tracepoint_probe_decode_location (struct breakpoint *b,
13649 const struct event_location *location,
c2f4122d 13650 struct program_space *search_pspace,
55aa24fb
SDJ
13651 struct symtabs_and_lines *sals)
13652{
13653 /* We use the same method for breakpoint on probes. */
c2f4122d 13654 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13655}
13656
13657static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13658
5c2b4418
HZ
13659/* Dprintf breakpoint_ops methods. */
13660
13661static void
13662dprintf_re_set (struct breakpoint *b)
13663{
13664 breakpoint_re_set_default (b);
13665
f00aae0f
KS
13666 /* extra_string should never be non-NULL for dprintf. */
13667 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13668
13669 /* 1 - connect to target 1, that can run breakpoint commands.
13670 2 - create a dprintf, which resolves fine.
13671 3 - disconnect from target 1
13672 4 - connect to target 2, that can NOT run breakpoint commands.
13673
13674 After steps #3/#4, you'll want the dprintf command list to
13675 be updated, because target 1 and 2 may well return different
13676 answers for target_can_run_breakpoint_commands().
13677 Given absence of finer grained resetting, we get to do
13678 it all the time. */
13679 if (b->extra_string != NULL)
13680 update_dprintf_command_list (b);
13681}
13682
2d9442cc
HZ
13683/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13684
13685static void
13686dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13687{
f00aae0f
KS
13688 fprintf_unfiltered (fp, "dprintf %s,%s",
13689 event_location_to_string (tp->location),
2d9442cc
HZ
13690 tp->extra_string);
13691 print_recreate_thread (tp, fp);
13692}
13693
9d6e6e84
HZ
13694/* Implement the "after_condition_true" breakpoint_ops method for
13695 dprintf.
13696
13697 dprintf's are implemented with regular commands in their command
13698 list, but we run the commands here instead of before presenting the
13699 stop to the user, as dprintf's don't actually cause a stop. This
13700 also makes it so that the commands of multiple dprintfs at the same
13701 address are all handled. */
13702
13703static void
13704dprintf_after_condition_true (struct bpstats *bs)
13705{
13706 struct cleanup *old_chain;
13707 struct bpstats tmp_bs = { NULL };
13708 struct bpstats *tmp_bs_p = &tmp_bs;
13709
13710 /* dprintf's never cause a stop. This wasn't set in the
13711 check_status hook instead because that would make the dprintf's
13712 condition not be evaluated. */
13713 bs->stop = 0;
13714
13715 /* Run the command list here. Take ownership of it instead of
13716 copying. We never want these commands to run later in
13717 bpstat_do_actions, if a breakpoint that causes a stop happens to
13718 be set at same address as this dprintf, or even if running the
13719 commands here throws. */
13720 tmp_bs.commands = bs->commands;
13721 bs->commands = NULL;
13722 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13723
13724 bpstat_do_actions_1 (&tmp_bs_p);
13725
13726 /* 'tmp_bs.commands' will usually be NULL by now, but
13727 bpstat_do_actions_1 may return early without processing the whole
13728 list. */
13729 do_cleanups (old_chain);
13730}
13731
983af33b
SDJ
13732/* The breakpoint_ops structure to be used on static tracepoints with
13733 markers (`-m'). */
13734
13735static void
f00aae0f 13736strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13737 struct linespec_result *canonical,
f00aae0f 13738 enum bptype type_wanted)
983af33b
SDJ
13739{
13740 struct linespec_sals lsal;
f00aae0f
KS
13741 const char *arg_start, *arg;
13742 char *str;
13743 struct cleanup *cleanup;
983af33b 13744
f00aae0f
KS
13745 arg = arg_start = get_linespec_location (location);
13746 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13747
f00aae0f
KS
13748 str = savestring (arg_start, arg - arg_start);
13749 cleanup = make_cleanup (xfree, str);
13750 canonical->location = new_linespec_location (&str);
13751 do_cleanups (cleanup);
983af33b 13752
f00aae0f 13753 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13754 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13755}
13756
13757static void
13758strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13759 struct linespec_result *canonical,
983af33b 13760 char *cond_string,
e7e0cddf 13761 char *extra_string,
983af33b
SDJ
13762 enum bptype type_wanted,
13763 enum bpdisp disposition,
13764 int thread,
13765 int task, int ignore_count,
13766 const struct breakpoint_ops *ops,
13767 int from_tty, int enabled,
44f238bb 13768 int internal, unsigned flags)
983af33b
SDJ
13769{
13770 int i;
52d361e1
YQ
13771 struct linespec_sals *lsal = VEC_index (linespec_sals,
13772 canonical->sals, 0);
983af33b
SDJ
13773
13774 /* If the user is creating a static tracepoint by marker id
13775 (strace -m MARKER_ID), then store the sals index, so that
13776 breakpoint_re_set can try to match up which of the newly
13777 found markers corresponds to this one, and, don't try to
13778 expand multiple locations for each sal, given than SALS
13779 already should contain all sals for MARKER_ID. */
13780
13781 for (i = 0; i < lsal->sals.nelts; ++i)
13782 {
13783 struct symtabs_and_lines expanded;
13784 struct tracepoint *tp;
13785 struct cleanup *old_chain;
f00aae0f 13786 struct event_location *location;
983af33b
SDJ
13787
13788 expanded.nelts = 1;
13789 expanded.sals = &lsal->sals.sals[i];
13790
f00aae0f
KS
13791 location = copy_event_location (canonical->location);
13792 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13793
13794 tp = XCNEW (struct tracepoint);
13795 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13796 location, NULL,
e7e0cddf
SS
13797 cond_string, extra_string,
13798 type_wanted, disposition,
983af33b 13799 thread, task, ignore_count, ops,
44f238bb 13800 from_tty, enabled, internal, flags,
983af33b
SDJ
13801 canonical->special_display);
13802 /* Given that its possible to have multiple markers with
13803 the same string id, if the user is creating a static
13804 tracepoint by marker id ("strace -m MARKER_ID"), then
13805 store the sals index, so that breakpoint_re_set can
13806 try to match up which of the newly found markers
13807 corresponds to this one */
13808 tp->static_trace_marker_id_idx = i;
13809
13810 install_breakpoint (internal, &tp->base, 0);
13811
13812 discard_cleanups (old_chain);
13813 }
13814}
13815
13816static void
f00aae0f
KS
13817strace_marker_decode_location (struct breakpoint *b,
13818 const struct event_location *location,
c2f4122d 13819 struct program_space *search_pspace,
983af33b
SDJ
13820 struct symtabs_and_lines *sals)
13821{
13822 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13823 const char *s = get_linespec_location (location);
983af33b 13824
f00aae0f 13825 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13826 if (sals->nelts > tp->static_trace_marker_id_idx)
13827 {
13828 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13829 sals->nelts = 1;
13830 }
13831 else
13832 error (_("marker %s not found"), tp->static_trace_marker_id);
13833}
13834
13835static struct breakpoint_ops strace_marker_breakpoint_ops;
13836
13837static int
13838strace_marker_p (struct breakpoint *b)
13839{
13840 return b->ops == &strace_marker_breakpoint_ops;
13841}
13842
53a5351d 13843/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13844 structures. */
c906108c
SS
13845
13846void
fba45db2 13847delete_breakpoint (struct breakpoint *bpt)
c906108c 13848{
52f0bd74 13849 struct breakpoint *b;
c906108c 13850
8a3fe4f8 13851 gdb_assert (bpt != NULL);
c906108c 13852
4a64f543
MS
13853 /* Has this bp already been deleted? This can happen because
13854 multiple lists can hold pointers to bp's. bpstat lists are
13855 especial culprits.
13856
13857 One example of this happening is a watchpoint's scope bp. When
13858 the scope bp triggers, we notice that the watchpoint is out of
13859 scope, and delete it. We also delete its scope bp. But the
13860 scope bp is marked "auto-deleting", and is already on a bpstat.
13861 That bpstat is then checked for auto-deleting bp's, which are
13862 deleted.
13863
13864 A real solution to this problem might involve reference counts in
13865 bp's, and/or giving them pointers back to their referencing
13866 bpstat's, and teaching delete_breakpoint to only free a bp's
13867 storage when no more references were extent. A cheaper bandaid
13868 was chosen. */
c906108c
SS
13869 if (bpt->type == bp_none)
13870 return;
13871
4a64f543
MS
13872 /* At least avoid this stale reference until the reference counting
13873 of breakpoints gets resolved. */
d0fb5eae 13874 if (bpt->related_breakpoint != bpt)
e5a0a904 13875 {
d0fb5eae 13876 struct breakpoint *related;
3a5c3e22 13877 struct watchpoint *w;
d0fb5eae
JK
13878
13879 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13880 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13881 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13882 w = (struct watchpoint *) bpt;
13883 else
13884 w = NULL;
13885 if (w != NULL)
13886 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13887
13888 /* Unlink bpt from the bpt->related_breakpoint ring. */
13889 for (related = bpt; related->related_breakpoint != bpt;
13890 related = related->related_breakpoint);
13891 related->related_breakpoint = bpt->related_breakpoint;
13892 bpt->related_breakpoint = bpt;
e5a0a904
JK
13893 }
13894
a9634178
TJB
13895 /* watch_command_1 creates a watchpoint but only sets its number if
13896 update_watchpoint succeeds in creating its bp_locations. If there's
13897 a problem in that process, we'll be asked to delete the half-created
13898 watchpoint. In that case, don't announce the deletion. */
13899 if (bpt->number)
13900 observer_notify_breakpoint_deleted (bpt);
c906108c 13901
c906108c
SS
13902 if (breakpoint_chain == bpt)
13903 breakpoint_chain = bpt->next;
13904
c906108c
SS
13905 ALL_BREAKPOINTS (b)
13906 if (b->next == bpt)
c5aa993b
JM
13907 {
13908 b->next = bpt->next;
13909 break;
13910 }
c906108c 13911
f431efe5
PA
13912 /* Be sure no bpstat's are pointing at the breakpoint after it's
13913 been freed. */
13914 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13915 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13916 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13917 commands are associated with the bpstat; if we remove it here,
13918 then the later call to bpstat_do_actions (&stop_bpstat); in
13919 event-top.c won't do anything, and temporary breakpoints with
13920 commands won't work. */
13921
13922 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13923
4a64f543
MS
13924 /* Now that breakpoint is removed from breakpoint list, update the
13925 global location list. This will remove locations that used to
13926 belong to this breakpoint. Do this before freeing the breakpoint
13927 itself, since remove_breakpoint looks at location's owner. It
13928 might be better design to have location completely
13929 self-contained, but it's not the case now. */
44702360 13930 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13931
348d480f 13932 bpt->ops->dtor (bpt);
4a64f543
MS
13933 /* On the chance that someone will soon try again to delete this
13934 same bp, we mark it as deleted before freeing its storage. */
c906108c 13935 bpt->type = bp_none;
b8c9b27d 13936 xfree (bpt);
c906108c
SS
13937}
13938
4d6140d9
AC
13939static void
13940do_delete_breakpoint_cleanup (void *b)
13941{
9a3c8263 13942 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13943}
13944
13945struct cleanup *
13946make_cleanup_delete_breakpoint (struct breakpoint *b)
13947{
13948 return make_cleanup (do_delete_breakpoint_cleanup, b);
13949}
13950
51be5b68
PA
13951/* Iterator function to call a user-provided callback function once
13952 for each of B and its related breakpoints. */
13953
13954static void
13955iterate_over_related_breakpoints (struct breakpoint *b,
13956 void (*function) (struct breakpoint *,
13957 void *),
13958 void *data)
13959{
13960 struct breakpoint *related;
13961
13962 related = b;
13963 do
13964 {
13965 struct breakpoint *next;
13966
13967 /* FUNCTION may delete RELATED. */
13968 next = related->related_breakpoint;
13969
13970 if (next == related)
13971 {
13972 /* RELATED is the last ring entry. */
13973 function (related, data);
13974
13975 /* FUNCTION may have deleted it, so we'd never reach back to
13976 B. There's nothing left to do anyway, so just break
13977 out. */
13978 break;
13979 }
13980 else
13981 function (related, data);
13982
13983 related = next;
13984 }
13985 while (related != b);
13986}
95a42b64
TT
13987
13988static void
13989do_delete_breakpoint (struct breakpoint *b, void *ignore)
13990{
13991 delete_breakpoint (b);
13992}
13993
51be5b68
PA
13994/* A callback for map_breakpoint_numbers that calls
13995 delete_breakpoint. */
13996
13997static void
13998do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13999{
14000 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14001}
14002
c906108c 14003void
fba45db2 14004delete_command (char *arg, int from_tty)
c906108c 14005{
35df4500 14006 struct breakpoint *b, *b_tmp;
c906108c 14007
ea9365bb
TT
14008 dont_repeat ();
14009
c906108c
SS
14010 if (arg == 0)
14011 {
14012 int breaks_to_delete = 0;
14013
46c6471b
PA
14014 /* Delete all breakpoints if no argument. Do not delete
14015 internal breakpoints, these have to be deleted with an
14016 explicit breakpoint number argument. */
c5aa993b 14017 ALL_BREAKPOINTS (b)
46c6471b 14018 if (user_breakpoint_p (b))
973d738b
DJ
14019 {
14020 breaks_to_delete = 1;
14021 break;
14022 }
c906108c
SS
14023
14024 /* Ask user only if there are some breakpoints to delete. */
14025 if (!from_tty
e2e0b3e5 14026 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14027 {
35df4500 14028 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14029 if (user_breakpoint_p (b))
c5aa993b 14030 delete_breakpoint (b);
c906108c
SS
14031 }
14032 }
14033 else
51be5b68 14034 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14035}
14036
c2f4122d
PA
14037/* Return true if all locations of B bound to PSPACE are pending. If
14038 PSPACE is NULL, all locations of all program spaces are
14039 considered. */
14040
0d381245 14041static int
c2f4122d 14042all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 14043{
c2f4122d
PA
14044 struct bp_location *loc;
14045
14046 for (loc = b->loc; loc != NULL; loc = loc->next)
14047 if ((pspace == NULL
14048 || loc->pspace == pspace)
14049 && !loc->shlib_disabled
8645ff69 14050 && !loc->pspace->executing_startup)
0d381245
VP
14051 return 0;
14052 return 1;
fe3f5fa8
VP
14053}
14054
776592bf
DE
14055/* Subroutine of update_breakpoint_locations to simplify it.
14056 Return non-zero if multiple fns in list LOC have the same name.
14057 Null names are ignored. */
14058
14059static int
14060ambiguous_names_p (struct bp_location *loc)
14061{
14062 struct bp_location *l;
14063 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14064 (int (*) (const void *,
14065 const void *)) streq,
776592bf
DE
14066 NULL, xcalloc, xfree);
14067
14068 for (l = loc; l != NULL; l = l->next)
14069 {
14070 const char **slot;
14071 const char *name = l->function_name;
14072
14073 /* Allow for some names to be NULL, ignore them. */
14074 if (name == NULL)
14075 continue;
14076
14077 slot = (const char **) htab_find_slot (htab, (const void *) name,
14078 INSERT);
4a64f543
MS
14079 /* NOTE: We can assume slot != NULL here because xcalloc never
14080 returns NULL. */
776592bf
DE
14081 if (*slot != NULL)
14082 {
14083 htab_delete (htab);
14084 return 1;
14085 }
14086 *slot = name;
14087 }
14088
14089 htab_delete (htab);
14090 return 0;
14091}
14092
0fb4aa4b
PA
14093/* When symbols change, it probably means the sources changed as well,
14094 and it might mean the static tracepoint markers are no longer at
14095 the same address or line numbers they used to be at last we
14096 checked. Losing your static tracepoints whenever you rebuild is
14097 undesirable. This function tries to resync/rematch gdb static
14098 tracepoints with the markers on the target, for static tracepoints
14099 that have not been set by marker id. Static tracepoint that have
14100 been set by marker id are reset by marker id in breakpoint_re_set.
14101 The heuristic is:
14102
14103 1) For a tracepoint set at a specific address, look for a marker at
14104 the old PC. If one is found there, assume to be the same marker.
14105 If the name / string id of the marker found is different from the
14106 previous known name, assume that means the user renamed the marker
14107 in the sources, and output a warning.
14108
14109 2) For a tracepoint set at a given line number, look for a marker
14110 at the new address of the old line number. If one is found there,
14111 assume to be the same marker. If the name / string id of the
14112 marker found is different from the previous known name, assume that
14113 means the user renamed the marker in the sources, and output a
14114 warning.
14115
14116 3) If a marker is no longer found at the same address or line, it
14117 may mean the marker no longer exists. But it may also just mean
14118 the code changed a bit. Maybe the user added a few lines of code
14119 that made the marker move up or down (in line number terms). Ask
14120 the target for info about the marker with the string id as we knew
14121 it. If found, update line number and address in the matching
14122 static tracepoint. This will get confused if there's more than one
14123 marker with the same ID (possible in UST, although unadvised
14124 precisely because it confuses tools). */
14125
14126static struct symtab_and_line
14127update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14128{
d9b3f62e 14129 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14130 struct static_tracepoint_marker marker;
14131 CORE_ADDR pc;
0fb4aa4b
PA
14132
14133 pc = sal.pc;
14134 if (sal.line)
14135 find_line_pc (sal.symtab, sal.line, &pc);
14136
14137 if (target_static_tracepoint_marker_at (pc, &marker))
14138 {
d9b3f62e 14139 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14140 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14141 b->number,
d9b3f62e 14142 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14143
d9b3f62e
PA
14144 xfree (tp->static_trace_marker_id);
14145 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14146 release_static_tracepoint_marker (&marker);
14147
14148 return sal;
14149 }
14150
14151 /* Old marker wasn't found on target at lineno. Try looking it up
14152 by string ID. */
14153 if (!sal.explicit_pc
14154 && sal.line != 0
14155 && sal.symtab != NULL
d9b3f62e 14156 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14157 {
14158 VEC(static_tracepoint_marker_p) *markers;
14159
14160 markers
d9b3f62e 14161 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14162
14163 if (!VEC_empty(static_tracepoint_marker_p, markers))
14164 {
80e1d417 14165 struct symtab_and_line sal2;
0fb4aa4b 14166 struct symbol *sym;
80e1d417 14167 struct static_tracepoint_marker *tpmarker;
79a45e25 14168 struct ui_out *uiout = current_uiout;
67994074 14169 struct explicit_location explicit_loc;
0fb4aa4b 14170
80e1d417 14171 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14172
d9b3f62e 14173 xfree (tp->static_trace_marker_id);
80e1d417 14174 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14175
14176 warning (_("marker for static tracepoint %d (%s) not "
14177 "found at previous line number"),
d9b3f62e 14178 b->number, tp->static_trace_marker_id);
0fb4aa4b 14179
80e1d417 14180 init_sal (&sal2);
0fb4aa4b 14181
80e1d417 14182 sal2.pc = tpmarker->address;
0fb4aa4b 14183
80e1d417
AS
14184 sal2 = find_pc_line (tpmarker->address, 0);
14185 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14186 ui_out_text (uiout, "Now in ");
14187 if (sym)
14188 {
14189 ui_out_field_string (uiout, "func",
14190 SYMBOL_PRINT_NAME (sym));
14191 ui_out_text (uiout, " at ");
14192 }
05cba821
JK
14193 ui_out_field_string (uiout, "file",
14194 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14195 ui_out_text (uiout, ":");
14196
14197 if (ui_out_is_mi_like_p (uiout))
14198 {
0b0865da 14199 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14200
f35a17b5 14201 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14202 }
14203
80e1d417 14204 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14205 ui_out_text (uiout, "\n");
14206
80e1d417 14207 b->loc->line_number = sal2.line;
2f202fde 14208 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14209
f00aae0f 14210 delete_event_location (b->location);
67994074
KS
14211 initialize_explicit_location (&explicit_loc);
14212 explicit_loc.source_filename
00e52e53 14213 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14214 explicit_loc.line_offset.offset = b->loc->line_number;
14215 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14216 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14217
14218 /* Might be nice to check if function changed, and warn if
14219 so. */
14220
80e1d417 14221 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14222 }
14223 }
14224 return sal;
14225}
14226
8d3788bd
VP
14227/* Returns 1 iff locations A and B are sufficiently same that
14228 we don't need to report breakpoint as changed. */
14229
14230static int
14231locations_are_equal (struct bp_location *a, struct bp_location *b)
14232{
14233 while (a && b)
14234 {
14235 if (a->address != b->address)
14236 return 0;
14237
14238 if (a->shlib_disabled != b->shlib_disabled)
14239 return 0;
14240
14241 if (a->enabled != b->enabled)
14242 return 0;
14243
14244 a = a->next;
14245 b = b->next;
14246 }
14247
14248 if ((a == NULL) != (b == NULL))
14249 return 0;
14250
14251 return 1;
14252}
14253
c2f4122d
PA
14254/* Split all locations of B that are bound to PSPACE out of B's
14255 location list to a separate list and return that list's head. If
14256 PSPACE is NULL, hoist out all locations of B. */
14257
14258static struct bp_location *
14259hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14260{
14261 struct bp_location head;
14262 struct bp_location *i = b->loc;
14263 struct bp_location **i_link = &b->loc;
14264 struct bp_location *hoisted = &head;
14265
14266 if (pspace == NULL)
14267 {
14268 i = b->loc;
14269 b->loc = NULL;
14270 return i;
14271 }
14272
14273 head.next = NULL;
14274
14275 while (i != NULL)
14276 {
14277 if (i->pspace == pspace)
14278 {
14279 *i_link = i->next;
14280 i->next = NULL;
14281 hoisted->next = i;
14282 hoisted = i;
14283 }
14284 else
14285 i_link = &i->next;
14286 i = *i_link;
14287 }
14288
14289 return head.next;
14290}
14291
14292/* Create new breakpoint locations for B (a hardware or software
14293 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14294 zero, then B is a ranged breakpoint. Only recreates locations for
14295 FILTER_PSPACE. Locations of other program spaces are left
14296 untouched. */
f1310107 14297
0e30163f 14298void
0d381245 14299update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14300 struct program_space *filter_pspace,
f1310107
TJB
14301 struct symtabs_and_lines sals,
14302 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14303{
14304 int i;
c2f4122d 14305 struct bp_location *existing_locations;
0d381245 14306
f8eba3c6
TT
14307 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14308 {
14309 /* Ranged breakpoints have only one start location and one end
14310 location. */
14311 b->enable_state = bp_disabled;
f8eba3c6
TT
14312 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14313 "multiple locations found\n"),
14314 b->number);
14315 return;
14316 }
f1310107 14317
4a64f543
MS
14318 /* If there's no new locations, and all existing locations are
14319 pending, don't do anything. This optimizes the common case where
14320 all locations are in the same shared library, that was unloaded.
14321 We'd like to retain the location, so that when the library is
14322 loaded again, we don't loose the enabled/disabled status of the
14323 individual locations. */
c2f4122d 14324 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14325 return;
14326
c2f4122d 14327 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14328
0d381245 14329 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14330 {
f8eba3c6
TT
14331 struct bp_location *new_loc;
14332
14333 switch_to_program_space_and_thread (sals.sals[i].pspace);
14334
14335 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14336
0d381245
VP
14337 /* Reparse conditions, they might contain references to the
14338 old symtab. */
14339 if (b->cond_string != NULL)
14340 {
bbc13ae3 14341 const char *s;
fe3f5fa8 14342
0d381245 14343 s = b->cond_string;
492d29ea 14344 TRY
0d381245 14345 {
1bb9788d
TT
14346 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14347 block_for_pc (sals.sals[i].pc),
0d381245
VP
14348 0);
14349 }
492d29ea 14350 CATCH (e, RETURN_MASK_ERROR)
0d381245 14351 {
3e43a32a
MS
14352 warning (_("failed to reevaluate condition "
14353 "for breakpoint %d: %s"),
0d381245
VP
14354 b->number, e.message);
14355 new_loc->enabled = 0;
14356 }
492d29ea 14357 END_CATCH
0d381245 14358 }
fe3f5fa8 14359
f1310107
TJB
14360 if (sals_end.nelts)
14361 {
14362 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14363
14364 new_loc->length = end - sals.sals[0].pc + 1;
14365 }
0d381245 14366 }
fe3f5fa8 14367
4a64f543
MS
14368 /* If possible, carry over 'disable' status from existing
14369 breakpoints. */
0d381245
VP
14370 {
14371 struct bp_location *e = existing_locations;
776592bf
DE
14372 /* If there are multiple breakpoints with the same function name,
14373 e.g. for inline functions, comparing function names won't work.
14374 Instead compare pc addresses; this is just a heuristic as things
14375 may have moved, but in practice it gives the correct answer
14376 often enough until a better solution is found. */
14377 int have_ambiguous_names = ambiguous_names_p (b->loc);
14378
0d381245
VP
14379 for (; e; e = e->next)
14380 {
14381 if (!e->enabled && e->function_name)
14382 {
14383 struct bp_location *l = b->loc;
776592bf
DE
14384 if (have_ambiguous_names)
14385 {
14386 for (; l; l = l->next)
f1310107 14387 if (breakpoint_locations_match (e, l))
776592bf
DE
14388 {
14389 l->enabled = 0;
14390 break;
14391 }
14392 }
14393 else
14394 {
14395 for (; l; l = l->next)
14396 if (l->function_name
14397 && strcmp (e->function_name, l->function_name) == 0)
14398 {
14399 l->enabled = 0;
14400 break;
14401 }
14402 }
0d381245
VP
14403 }
14404 }
14405 }
fe3f5fa8 14406
8d3788bd
VP
14407 if (!locations_are_equal (existing_locations, b->loc))
14408 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14409}
14410
f00aae0f 14411/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14412 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14413
14414static struct symtabs_and_lines
f00aae0f 14415location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14416 struct program_space *search_pspace, int *found)
ef23e705 14417{
02d20e4a 14418 struct symtabs_and_lines sals = {0};
492d29ea 14419 struct gdb_exception exception = exception_none;
ef23e705 14420
983af33b 14421 gdb_assert (b->ops != NULL);
ef23e705 14422
492d29ea 14423 TRY
ef23e705 14424 {
c2f4122d 14425 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14426 }
492d29ea 14427 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14428 {
14429 int not_found_and_ok = 0;
492d29ea
PA
14430
14431 exception = e;
14432
ef23e705
TJB
14433 /* For pending breakpoints, it's expected that parsing will
14434 fail until the right shared library is loaded. User has
14435 already told to create pending breakpoints and don't need
14436 extra messages. If breakpoint is in bp_shlib_disabled
14437 state, then user already saw the message about that
14438 breakpoint being disabled, and don't want to see more
14439 errors. */
58438ac1 14440 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14441 && (b->condition_not_parsed
14442 || (b->loc != NULL
14443 && search_pspace != NULL
14444 && b->loc->pspace != search_pspace)
ef23e705 14445 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14446 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14447 || b->enable_state == bp_disabled))
14448 not_found_and_ok = 1;
14449
14450 if (!not_found_and_ok)
14451 {
14452 /* We surely don't want to warn about the same breakpoint
14453 10 times. One solution, implemented here, is disable
14454 the breakpoint on error. Another solution would be to
14455 have separate 'warning emitted' flag. Since this
14456 happens only when a binary has changed, I don't know
14457 which approach is better. */
14458 b->enable_state = bp_disabled;
14459 throw_exception (e);
14460 }
14461 }
492d29ea 14462 END_CATCH
ef23e705 14463
492d29ea 14464 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14465 {
f8eba3c6 14466 int i;
ef23e705 14467
f8eba3c6
TT
14468 for (i = 0; i < sals.nelts; ++i)
14469 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14470 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14471 {
ed1d1739
KS
14472 char *cond_string, *extra_string;
14473 int thread, task;
ef23e705 14474
f00aae0f 14475 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14476 &cond_string, &thread, &task,
14477 &extra_string);
f00aae0f 14478 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14479 if (cond_string)
14480 b->cond_string = cond_string;
14481 b->thread = thread;
14482 b->task = task;
e7e0cddf 14483 if (extra_string)
f00aae0f
KS
14484 {
14485 xfree (b->extra_string);
14486 b->extra_string = extra_string;
14487 }
ef23e705
TJB
14488 b->condition_not_parsed = 0;
14489 }
14490
983af33b 14491 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14492 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14493
58438ac1
TT
14494 *found = 1;
14495 }
14496 else
14497 *found = 0;
ef23e705
TJB
14498
14499 return sals;
14500}
14501
348d480f
PA
14502/* The default re_set method, for typical hardware or software
14503 breakpoints. Reevaluate the breakpoint and recreate its
14504 locations. */
14505
14506static void
28010a5d 14507breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14508{
14509 int found;
f1310107 14510 struct symtabs_and_lines sals, sals_end;
ef23e705 14511 struct symtabs_and_lines expanded = {0};
f1310107 14512 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14513 struct program_space *filter_pspace = current_program_space;
ef23e705 14514
c2f4122d 14515 sals = location_to_sals (b, b->location, filter_pspace, &found);
ef23e705
TJB
14516 if (found)
14517 {
14518 make_cleanup (xfree, sals.sals);
f8eba3c6 14519 expanded = sals;
ef23e705
TJB
14520 }
14521
f00aae0f 14522 if (b->location_range_end != NULL)
f1310107 14523 {
c2f4122d
PA
14524 sals_end = location_to_sals (b, b->location_range_end,
14525 filter_pspace, &found);
f1310107
TJB
14526 if (found)
14527 {
14528 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14529 expanded_end = sals_end;
f1310107
TJB
14530 }
14531 }
14532
c2f4122d 14533 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14534}
14535
983af33b
SDJ
14536/* Default method for creating SALs from an address string. It basically
14537 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14538
14539static void
f00aae0f
KS
14540create_sals_from_location_default (const struct event_location *location,
14541 struct linespec_result *canonical,
14542 enum bptype type_wanted)
983af33b 14543{
f00aae0f 14544 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14545}
14546
14547/* Call create_breakpoints_sal for the given arguments. This is the default
14548 function for the `create_breakpoints_sal' method of
14549 breakpoint_ops. */
14550
14551static void
14552create_breakpoints_sal_default (struct gdbarch *gdbarch,
14553 struct linespec_result *canonical,
983af33b 14554 char *cond_string,
e7e0cddf 14555 char *extra_string,
983af33b
SDJ
14556 enum bptype type_wanted,
14557 enum bpdisp disposition,
14558 int thread,
14559 int task, int ignore_count,
14560 const struct breakpoint_ops *ops,
14561 int from_tty, int enabled,
44f238bb 14562 int internal, unsigned flags)
983af33b
SDJ
14563{
14564 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14565 extra_string,
983af33b
SDJ
14566 type_wanted, disposition,
14567 thread, task, ignore_count, ops, from_tty,
44f238bb 14568 enabled, internal, flags);
983af33b
SDJ
14569}
14570
14571/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14572 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14573
14574static void
f00aae0f
KS
14575decode_location_default (struct breakpoint *b,
14576 const struct event_location *location,
c2f4122d 14577 struct program_space *search_pspace,
983af33b
SDJ
14578 struct symtabs_and_lines *sals)
14579{
14580 struct linespec_result canonical;
14581
14582 init_linespec_result (&canonical);
c2f4122d 14583 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14584 (struct symtab *) NULL, 0,
14585 &canonical, multiple_symbols_all,
14586 b->filter);
14587
14588 /* We should get 0 or 1 resulting SALs. */
14589 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14590
14591 if (VEC_length (linespec_sals, canonical.sals) > 0)
14592 {
14593 struct linespec_sals *lsal;
14594
14595 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14596 *sals = lsal->sals;
14597 /* Arrange it so the destructor does not free the
14598 contents. */
14599 lsal->sals.sals = NULL;
14600 }
14601
14602 destroy_linespec_result (&canonical);
14603}
14604
28010a5d
PA
14605/* Prepare the global context for a re-set of breakpoint B. */
14606
14607static struct cleanup *
14608prepare_re_set_context (struct breakpoint *b)
14609{
28010a5d 14610 input_radix = b->input_radix;
28010a5d
PA
14611 set_language (b->language);
14612
c2f4122d 14613 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14614}
14615
c906108c
SS
14616/* Reset a breakpoint given it's struct breakpoint * BINT.
14617 The value we return ends up being the return value from catch_errors.
14618 Unused in this case. */
14619
14620static int
4efb68b1 14621breakpoint_re_set_one (void *bint)
c906108c 14622{
4a64f543 14623 /* Get past catch_errs. */
53a5351d 14624 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14625 struct cleanup *cleanups;
c906108c 14626
348d480f
PA
14627 cleanups = prepare_re_set_context (b);
14628 b->ops->re_set (b);
14629 do_cleanups (cleanups);
c906108c
SS
14630 return 0;
14631}
14632
c2f4122d
PA
14633/* Re-set breakpoint locations for the current program space.
14634 Locations bound to other program spaces are left untouched. */
14635
c906108c 14636void
69de3c6a 14637breakpoint_re_set (void)
c906108c 14638{
35df4500 14639 struct breakpoint *b, *b_tmp;
c906108c
SS
14640 enum language save_language;
14641 int save_input_radix;
6c95b8df 14642 struct cleanup *old_chain;
c5aa993b 14643
c906108c
SS
14644 save_language = current_language->la_language;
14645 save_input_radix = input_radix;
c2f4122d 14646 old_chain = save_current_space_and_thread ();
6c95b8df 14647
2a7f3dff
PA
14648 /* Note: we must not try to insert locations until after all
14649 breakpoints have been re-set. Otherwise, e.g., when re-setting
14650 breakpoint 1, we'd insert the locations of breakpoint 2, which
14651 hadn't been re-set yet, and thus may have stale locations. */
14652
35df4500 14653 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14654 {
4a64f543 14655 /* Format possible error msg. */
fe3f5fa8 14656 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14657 b->number);
14658 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14659 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14660 do_cleanups (cleanups);
c5aa993b 14661 }
c906108c
SS
14662 set_language (save_language);
14663 input_radix = save_input_radix;
e62c965a 14664
0756c555 14665 jit_breakpoint_re_set ();
4efc6507 14666
6c95b8df
PA
14667 do_cleanups (old_chain);
14668
af02033e
PP
14669 create_overlay_event_breakpoint ();
14670 create_longjmp_master_breakpoint ();
14671 create_std_terminate_master_breakpoint ();
186c406b 14672 create_exception_master_breakpoint ();
2a7f3dff
PA
14673
14674 /* Now we can insert. */
14675 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14676}
14677\f
c906108c
SS
14678/* Reset the thread number of this breakpoint:
14679
14680 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14681 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14682void
fba45db2 14683breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14684{
14685 if (b->thread != -1)
14686 {
39f77062 14687 if (in_thread_list (inferior_ptid))
5d5658a1 14688 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14689
14690 /* We're being called after following a fork. The new fork is
14691 selected as current, and unless this was a vfork will have a
14692 different program space from the original thread. Reset that
14693 as well. */
14694 b->loc->pspace = current_program_space;
c906108c
SS
14695 }
14696}
14697
03ac34d5
MS
14698/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14699 If from_tty is nonzero, it prints a message to that effect,
14700 which ends with a period (no newline). */
14701
c906108c 14702void
fba45db2 14703set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14704{
52f0bd74 14705 struct breakpoint *b;
c906108c
SS
14706
14707 if (count < 0)
14708 count = 0;
14709
14710 ALL_BREAKPOINTS (b)
14711 if (b->number == bptnum)
c5aa993b 14712 {
d77f58be
SS
14713 if (is_tracepoint (b))
14714 {
14715 if (from_tty && count != 0)
14716 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14717 bptnum);
14718 return;
14719 }
14720
c5aa993b 14721 b->ignore_count = count;
221ea385
KS
14722 if (from_tty)
14723 {
14724 if (count == 0)
3e43a32a
MS
14725 printf_filtered (_("Will stop next time "
14726 "breakpoint %d is reached."),
221ea385
KS
14727 bptnum);
14728 else if (count == 1)
a3f17187 14729 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14730 bptnum);
14731 else
3e43a32a
MS
14732 printf_filtered (_("Will ignore next %d "
14733 "crossings of breakpoint %d."),
221ea385
KS
14734 count, bptnum);
14735 }
8d3788bd 14736 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14737 return;
14738 }
c906108c 14739
8a3fe4f8 14740 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14741}
14742
c906108c
SS
14743/* Command to set ignore-count of breakpoint N to COUNT. */
14744
14745static void
fba45db2 14746ignore_command (char *args, int from_tty)
c906108c
SS
14747{
14748 char *p = args;
52f0bd74 14749 int num;
c906108c
SS
14750
14751 if (p == 0)
e2e0b3e5 14752 error_no_arg (_("a breakpoint number"));
c5aa993b 14753
c906108c 14754 num = get_number (&p);
5c44784c 14755 if (num == 0)
8a3fe4f8 14756 error (_("bad breakpoint number: '%s'"), args);
c906108c 14757 if (*p == 0)
8a3fe4f8 14758 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14759
14760 set_ignore_count (num,
14761 longest_to_int (value_as_long (parse_and_eval (p))),
14762 from_tty);
221ea385
KS
14763 if (from_tty)
14764 printf_filtered ("\n");
c906108c
SS
14765}
14766\f
14767/* Call FUNCTION on each of the breakpoints
14768 whose numbers are given in ARGS. */
14769
14770static void
95a42b64
TT
14771map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14772 void *),
14773 void *data)
c906108c 14774{
52f0bd74
AC
14775 int num;
14776 struct breakpoint *b, *tmp;
11cf8741 14777 int match;
197f0a60 14778 struct get_number_or_range_state state;
c906108c 14779
b9d61307 14780 if (args == 0 || *args == '\0')
e2e0b3e5 14781 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14782
197f0a60
TT
14783 init_number_or_range (&state, args);
14784
14785 while (!state.finished)
c906108c 14786 {
e799154c 14787 const char *p = state.string;
197f0a60 14788
11cf8741 14789 match = 0;
c5aa993b 14790
197f0a60 14791 num = get_number_or_range (&state);
5c44784c 14792 if (num == 0)
c5aa993b 14793 {
8a3fe4f8 14794 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14795 }
14796 else
14797 {
14798 ALL_BREAKPOINTS_SAFE (b, tmp)
14799 if (b->number == num)
14800 {
11cf8741 14801 match = 1;
cdac0397 14802 function (b, data);
11cf8741 14803 break;
5c44784c 14804 }
11cf8741 14805 if (match == 0)
a3f17187 14806 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14807 }
c906108c
SS
14808 }
14809}
14810
0d381245
VP
14811static struct bp_location *
14812find_location_by_number (char *number)
14813{
14814 char *dot = strchr (number, '.');
14815 char *p1;
14816 int bp_num;
14817 int loc_num;
14818 struct breakpoint *b;
14819 struct bp_location *loc;
14820
14821 *dot = '\0';
14822
14823 p1 = number;
197f0a60 14824 bp_num = get_number (&p1);
0d381245
VP
14825 if (bp_num == 0)
14826 error (_("Bad breakpoint number '%s'"), number);
14827
14828 ALL_BREAKPOINTS (b)
14829 if (b->number == bp_num)
14830 {
14831 break;
14832 }
14833
14834 if (!b || b->number != bp_num)
14835 error (_("Bad breakpoint number '%s'"), number);
14836
14837 p1 = dot+1;
197f0a60 14838 loc_num = get_number (&p1);
0d381245
VP
14839 if (loc_num == 0)
14840 error (_("Bad breakpoint location number '%s'"), number);
14841
14842 --loc_num;
14843 loc = b->loc;
14844 for (;loc_num && loc; --loc_num, loc = loc->next)
14845 ;
14846 if (!loc)
14847 error (_("Bad breakpoint location number '%s'"), dot+1);
14848
14849 return loc;
14850}
14851
14852
1900040c
MS
14853/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14854 If from_tty is nonzero, it prints a message to that effect,
14855 which ends with a period (no newline). */
14856
c906108c 14857void
fba45db2 14858disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14859{
14860 /* Never disable a watchpoint scope breakpoint; we want to
14861 hit them when we leave scope so we can delete both the
14862 watchpoint and its scope breakpoint at that time. */
14863 if (bpt->type == bp_watchpoint_scope)
14864 return;
14865
b5de0fa7 14866 bpt->enable_state = bp_disabled;
c906108c 14867
b775012e
LM
14868 /* Mark breakpoint locations modified. */
14869 mark_breakpoint_modified (bpt);
14870
d248b706
KY
14871 if (target_supports_enable_disable_tracepoint ()
14872 && current_trace_status ()->running && is_tracepoint (bpt))
14873 {
14874 struct bp_location *location;
14875
14876 for (location = bpt->loc; location; location = location->next)
14877 target_disable_tracepoint (location);
14878 }
14879
44702360 14880 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14881
8d3788bd 14882 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14883}
14884
51be5b68
PA
14885/* A callback for iterate_over_related_breakpoints. */
14886
14887static void
14888do_disable_breakpoint (struct breakpoint *b, void *ignore)
14889{
14890 disable_breakpoint (b);
14891}
14892
95a42b64
TT
14893/* A callback for map_breakpoint_numbers that calls
14894 disable_breakpoint. */
14895
14896static void
14897do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14898{
51be5b68 14899 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14900}
14901
c906108c 14902static void
fba45db2 14903disable_command (char *args, int from_tty)
c906108c 14904{
c906108c 14905 if (args == 0)
46c6471b
PA
14906 {
14907 struct breakpoint *bpt;
14908
14909 ALL_BREAKPOINTS (bpt)
14910 if (user_breakpoint_p (bpt))
14911 disable_breakpoint (bpt);
14912 }
9eaabc75 14913 else
0d381245 14914 {
9eaabc75
MW
14915 char *num = extract_arg (&args);
14916
14917 while (num)
d248b706 14918 {
9eaabc75 14919 if (strchr (num, '.'))
b775012e 14920 {
9eaabc75
MW
14921 struct bp_location *loc = find_location_by_number (num);
14922
14923 if (loc)
14924 {
14925 if (loc->enabled)
14926 {
14927 loc->enabled = 0;
14928 mark_breakpoint_location_modified (loc);
14929 }
14930 if (target_supports_enable_disable_tracepoint ()
14931 && current_trace_status ()->running && loc->owner
14932 && is_tracepoint (loc->owner))
14933 target_disable_tracepoint (loc);
14934 }
44702360 14935 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14936 }
9eaabc75
MW
14937 else
14938 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14939 num = extract_arg (&args);
d248b706 14940 }
0d381245 14941 }
c906108c
SS
14942}
14943
14944static void
816338b5
SS
14945enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14946 int count)
c906108c 14947{
afe38095 14948 int target_resources_ok;
c906108c
SS
14949
14950 if (bpt->type == bp_hardware_breakpoint)
14951 {
14952 int i;
c5aa993b 14953 i = hw_breakpoint_used_count ();
53a5351d 14954 target_resources_ok =
d92524f1 14955 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14956 i + 1, 0);
c906108c 14957 if (target_resources_ok == 0)
8a3fe4f8 14958 error (_("No hardware breakpoint support in the target."));
c906108c 14959 else if (target_resources_ok < 0)
8a3fe4f8 14960 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14961 }
14962
cc60f2e3 14963 if (is_watchpoint (bpt))
c906108c 14964 {
d07205c2 14965 /* Initialize it just to avoid a GCC false warning. */
f486487f 14966 enum enable_state orig_enable_state = bp_disabled;
dde02812 14967
492d29ea 14968 TRY
c906108c 14969 {
3a5c3e22
PA
14970 struct watchpoint *w = (struct watchpoint *) bpt;
14971
1e718ff1
TJB
14972 orig_enable_state = bpt->enable_state;
14973 bpt->enable_state = bp_enabled;
3a5c3e22 14974 update_watchpoint (w, 1 /* reparse */);
c906108c 14975 }
492d29ea 14976 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14977 {
1e718ff1 14978 bpt->enable_state = orig_enable_state;
dde02812
ES
14979 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14980 bpt->number);
14981 return;
c5aa993b 14982 }
492d29ea 14983 END_CATCH
c906108c 14984 }
0101ce28 14985
b775012e
LM
14986 bpt->enable_state = bp_enabled;
14987
14988 /* Mark breakpoint locations modified. */
14989 mark_breakpoint_modified (bpt);
14990
d248b706
KY
14991 if (target_supports_enable_disable_tracepoint ()
14992 && current_trace_status ()->running && is_tracepoint (bpt))
14993 {
14994 struct bp_location *location;
14995
14996 for (location = bpt->loc; location; location = location->next)
14997 target_enable_tracepoint (location);
14998 }
14999
b4c291bb 15000 bpt->disposition = disposition;
816338b5 15001 bpt->enable_count = count;
44702360 15002 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15003
8d3788bd 15004 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15005}
15006
fe3f5fa8 15007
c906108c 15008void
fba45db2 15009enable_breakpoint (struct breakpoint *bpt)
c906108c 15010{
816338b5 15011 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15012}
15013
15014static void
15015do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15016{
15017 enable_breakpoint (bpt);
c906108c
SS
15018}
15019
95a42b64
TT
15020/* A callback for map_breakpoint_numbers that calls
15021 enable_breakpoint. */
15022
15023static void
15024do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15025{
51be5b68 15026 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15027}
15028
c906108c
SS
15029/* The enable command enables the specified breakpoints (or all defined
15030 breakpoints) so they once again become (or continue to be) effective
1272ad14 15031 in stopping the inferior. */
c906108c 15032
c906108c 15033static void
fba45db2 15034enable_command (char *args, int from_tty)
c906108c 15035{
c906108c 15036 if (args == 0)
46c6471b
PA
15037 {
15038 struct breakpoint *bpt;
15039
15040 ALL_BREAKPOINTS (bpt)
15041 if (user_breakpoint_p (bpt))
15042 enable_breakpoint (bpt);
15043 }
9eaabc75 15044 else
0d381245 15045 {
9eaabc75
MW
15046 char *num = extract_arg (&args);
15047
15048 while (num)
d248b706 15049 {
9eaabc75 15050 if (strchr (num, '.'))
b775012e 15051 {
9eaabc75
MW
15052 struct bp_location *loc = find_location_by_number (num);
15053
15054 if (loc)
15055 {
15056 if (!loc->enabled)
15057 {
15058 loc->enabled = 1;
15059 mark_breakpoint_location_modified (loc);
15060 }
15061 if (target_supports_enable_disable_tracepoint ()
15062 && current_trace_status ()->running && loc->owner
15063 && is_tracepoint (loc->owner))
15064 target_enable_tracepoint (loc);
15065 }
44702360 15066 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15067 }
9eaabc75
MW
15068 else
15069 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15070 num = extract_arg (&args);
d248b706 15071 }
0d381245 15072 }
c906108c
SS
15073}
15074
816338b5
SS
15075/* This struct packages up disposition data for application to multiple
15076 breakpoints. */
15077
15078struct disp_data
15079{
15080 enum bpdisp disp;
15081 int count;
15082};
15083
c906108c 15084static void
51be5b68
PA
15085do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15086{
816338b5 15087 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15088
816338b5 15089 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15090}
15091
15092static void
15093do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15094{
816338b5 15095 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15096
15097 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15098}
15099
c906108c 15100static void
fba45db2 15101enable_once_command (char *args, int from_tty)
c906108c 15102{
51be5b68 15103 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15104}
15105
816338b5
SS
15106static void
15107do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15108{
15109 struct disp_data disp = { disp_disable, *(int *) countptr };
15110
15111 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15112}
15113
15114static void
15115enable_count_command (char *args, int from_tty)
15116{
b9d61307
SM
15117 int count;
15118
15119 if (args == NULL)
15120 error_no_arg (_("hit count"));
15121
15122 count = get_number (&args);
816338b5
SS
15123
15124 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15125}
15126
c906108c 15127static void
51be5b68 15128do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15129{
816338b5 15130 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15131
15132 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15133}
15134
c906108c 15135static void
fba45db2 15136enable_delete_command (char *args, int from_tty)
c906108c 15137{
51be5b68 15138 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15139}
15140\f
fa8d40ab
JJ
15141static void
15142set_breakpoint_cmd (char *args, int from_tty)
15143{
15144}
15145
15146static void
15147show_breakpoint_cmd (char *args, int from_tty)
15148{
15149}
15150
1f3b5d1b
PP
15151/* Invalidate last known value of any hardware watchpoint if
15152 the memory which that value represents has been written to by
15153 GDB itself. */
15154
15155static void
8de0566d
YQ
15156invalidate_bp_value_on_memory_change (struct inferior *inferior,
15157 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15158 const bfd_byte *data)
15159{
15160 struct breakpoint *bp;
15161
15162 ALL_BREAKPOINTS (bp)
15163 if (bp->enable_state == bp_enabled
3a5c3e22 15164 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15165 {
3a5c3e22 15166 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15167
3a5c3e22
PA
15168 if (wp->val_valid && wp->val)
15169 {
15170 struct bp_location *loc;
15171
15172 for (loc = bp->loc; loc != NULL; loc = loc->next)
15173 if (loc->loc_type == bp_loc_hardware_watchpoint
15174 && loc->address + loc->length > addr
15175 && addr + len > loc->address)
15176 {
15177 value_free (wp->val);
15178 wp->val = NULL;
15179 wp->val_valid = 0;
15180 }
15181 }
1f3b5d1b
PP
15182 }
15183}
15184
8181d85f
DJ
15185/* Create and insert a breakpoint for software single step. */
15186
15187void
6c95b8df 15188insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15189 struct address_space *aspace,
15190 CORE_ADDR next_pc)
8181d85f 15191{
7c16b83e
PA
15192 struct thread_info *tp = inferior_thread ();
15193 struct symtab_and_line sal;
15194 CORE_ADDR pc = next_pc;
8181d85f 15195
34b7e8a6
PA
15196 if (tp->control.single_step_breakpoints == NULL)
15197 {
15198 tp->control.single_step_breakpoints
5d5658a1 15199 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15200 }
8181d85f 15201
7c16b83e
PA
15202 sal = find_pc_line (pc, 0);
15203 sal.pc = pc;
15204 sal.section = find_pc_overlay (pc);
15205 sal.explicit_pc = 1;
34b7e8a6 15206 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15207
7c16b83e 15208 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15209}
15210
34b7e8a6 15211/* See breakpoint.h. */
f02253f1
HZ
15212
15213int
7c16b83e
PA
15214breakpoint_has_location_inserted_here (struct breakpoint *bp,
15215 struct address_space *aspace,
15216 CORE_ADDR pc)
1aafd4da 15217{
7c16b83e 15218 struct bp_location *loc;
1aafd4da 15219
7c16b83e
PA
15220 for (loc = bp->loc; loc != NULL; loc = loc->next)
15221 if (loc->inserted
15222 && breakpoint_location_address_match (loc, aspace, pc))
15223 return 1;
1aafd4da 15224
7c16b83e 15225 return 0;
ef370185
JB
15226}
15227
15228/* Check whether a software single-step breakpoint is inserted at
15229 PC. */
15230
15231int
15232single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15233 CORE_ADDR pc)
15234{
34b7e8a6
PA
15235 struct breakpoint *bpt;
15236
15237 ALL_BREAKPOINTS (bpt)
15238 {
15239 if (bpt->type == bp_single_step
15240 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15241 return 1;
15242 }
15243 return 0;
1aafd4da
UW
15244}
15245
1042e4c0
SS
15246/* Tracepoint-specific operations. */
15247
15248/* Set tracepoint count to NUM. */
15249static void
15250set_tracepoint_count (int num)
15251{
15252 tracepoint_count = num;
4fa62494 15253 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15254}
15255
70221824 15256static void
1042e4c0
SS
15257trace_command (char *arg, int from_tty)
15258{
55aa24fb 15259 struct breakpoint_ops *ops;
f00aae0f
KS
15260 struct event_location *location;
15261 struct cleanup *back_to;
55aa24fb 15262
f00aae0f
KS
15263 location = string_to_event_location (&arg, current_language);
15264 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15265 if (location != NULL
15266 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15267 ops = &tracepoint_probe_breakpoint_ops;
15268 else
15269 ops = &tracepoint_breakpoint_ops;
15270
558a9d82 15271 create_breakpoint (get_current_arch (),
f00aae0f
KS
15272 location,
15273 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15274 0 /* tempflag */,
15275 bp_tracepoint /* type_wanted */,
15276 0 /* Ignore count */,
15277 pending_break_support,
15278 ops,
15279 from_tty,
15280 1 /* enabled */,
15281 0 /* internal */, 0);
f00aae0f 15282 do_cleanups (back_to);
1042e4c0
SS
15283}
15284
70221824 15285static void
7a697b8d
SS
15286ftrace_command (char *arg, int from_tty)
15287{
f00aae0f
KS
15288 struct event_location *location;
15289 struct cleanup *back_to;
15290
15291 location = string_to_event_location (&arg, current_language);
15292 back_to = make_cleanup_delete_event_location (location);
558a9d82 15293 create_breakpoint (get_current_arch (),
f00aae0f
KS
15294 location,
15295 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15296 0 /* tempflag */,
15297 bp_fast_tracepoint /* type_wanted */,
15298 0 /* Ignore count */,
15299 pending_break_support,
15300 &tracepoint_breakpoint_ops,
15301 from_tty,
15302 1 /* enabled */,
15303 0 /* internal */, 0);
f00aae0f 15304 do_cleanups (back_to);
0fb4aa4b
PA
15305}
15306
15307/* strace command implementation. Creates a static tracepoint. */
15308
70221824 15309static void
0fb4aa4b
PA
15310strace_command (char *arg, int from_tty)
15311{
983af33b 15312 struct breakpoint_ops *ops;
f00aae0f
KS
15313 struct event_location *location;
15314 struct cleanup *back_to;
983af33b
SDJ
15315
15316 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15317 or with a normal static tracepoint. */
61012eef 15318 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15319 {
15320 ops = &strace_marker_breakpoint_ops;
15321 location = new_linespec_location (&arg);
15322 }
983af33b 15323 else
f00aae0f
KS
15324 {
15325 ops = &tracepoint_breakpoint_ops;
15326 location = string_to_event_location (&arg, current_language);
15327 }
983af33b 15328
f00aae0f 15329 back_to = make_cleanup_delete_event_location (location);
558a9d82 15330 create_breakpoint (get_current_arch (),
f00aae0f
KS
15331 location,
15332 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15333 0 /* tempflag */,
15334 bp_static_tracepoint /* type_wanted */,
15335 0 /* Ignore count */,
15336 pending_break_support,
15337 ops,
15338 from_tty,
15339 1 /* enabled */,
15340 0 /* internal */, 0);
f00aae0f 15341 do_cleanups (back_to);
7a697b8d
SS
15342}
15343
409873ef
SS
15344/* Set up a fake reader function that gets command lines from a linked
15345 list that was acquired during tracepoint uploading. */
15346
15347static struct uploaded_tp *this_utp;
3149d8c1 15348static int next_cmd;
409873ef
SS
15349
15350static char *
15351read_uploaded_action (void)
15352{
15353 char *rslt;
15354
3149d8c1 15355 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15356
3149d8c1 15357 next_cmd++;
409873ef
SS
15358
15359 return rslt;
15360}
15361
00bf0b85
SS
15362/* Given information about a tracepoint as recorded on a target (which
15363 can be either a live system or a trace file), attempt to create an
15364 equivalent GDB tracepoint. This is not a reliable process, since
15365 the target does not necessarily have all the information used when
15366 the tracepoint was originally defined. */
15367
d9b3f62e 15368struct tracepoint *
00bf0b85 15369create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15370{
409873ef 15371 char *addr_str, small_buf[100];
d9b3f62e 15372 struct tracepoint *tp;
f00aae0f
KS
15373 struct event_location *location;
15374 struct cleanup *cleanup;
fd9b8c24 15375
409873ef
SS
15376 if (utp->at_string)
15377 addr_str = utp->at_string;
15378 else
15379 {
15380 /* In the absence of a source location, fall back to raw
15381 address. Since there is no way to confirm that the address
15382 means the same thing as when the trace was started, warn the
15383 user. */
3e43a32a
MS
15384 warning (_("Uploaded tracepoint %d has no "
15385 "source location, using raw address"),
409873ef 15386 utp->number);
8c042590 15387 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15388 addr_str = small_buf;
15389 }
15390
15391 /* There's not much we can do with a sequence of bytecodes. */
15392 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15393 warning (_("Uploaded tracepoint %d condition "
15394 "has no source form, ignoring it"),
409873ef 15395 utp->number);
d5551862 15396
f00aae0f
KS
15397 location = string_to_event_location (&addr_str, current_language);
15398 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15399 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15400 location,
15401 utp->cond_string, -1, addr_str,
e7e0cddf 15402 0 /* parse cond/thread */,
8cdf0e15 15403 0 /* tempflag */,
0fb4aa4b 15404 utp->type /* type_wanted */,
8cdf0e15
VP
15405 0 /* Ignore count */,
15406 pending_break_support,
348d480f 15407 &tracepoint_breakpoint_ops,
8cdf0e15 15408 0 /* from_tty */,
84f4c1fe 15409 utp->enabled /* enabled */,
44f238bb
PA
15410 0 /* internal */,
15411 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15412 {
15413 do_cleanups (cleanup);
15414 return NULL;
15415 }
15416
15417 do_cleanups (cleanup);
fd9b8c24 15418
409873ef 15419 /* Get the tracepoint we just created. */
fd9b8c24
PA
15420 tp = get_tracepoint (tracepoint_count);
15421 gdb_assert (tp != NULL);
d5551862 15422
00bf0b85
SS
15423 if (utp->pass > 0)
15424 {
8c042590
PM
15425 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15426 tp->base.number);
00bf0b85 15427
409873ef 15428 trace_pass_command (small_buf, 0);
00bf0b85
SS
15429 }
15430
409873ef
SS
15431 /* If we have uploaded versions of the original commands, set up a
15432 special-purpose "reader" function and call the usual command line
15433 reader, then pass the result to the breakpoint command-setting
15434 function. */
3149d8c1 15435 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15436 {
409873ef 15437 struct command_line *cmd_list;
00bf0b85 15438
409873ef 15439 this_utp = utp;
3149d8c1 15440 next_cmd = 0;
d5551862 15441
409873ef
SS
15442 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15443
d9b3f62e 15444 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15445 }
3149d8c1
SS
15446 else if (!VEC_empty (char_ptr, utp->actions)
15447 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15448 warning (_("Uploaded tracepoint %d actions "
15449 "have no source form, ignoring them"),
409873ef 15450 utp->number);
00bf0b85 15451
f196051f
SS
15452 /* Copy any status information that might be available. */
15453 tp->base.hit_count = utp->hit_count;
15454 tp->traceframe_usage = utp->traceframe_usage;
15455
00bf0b85 15456 return tp;
d9b3f62e 15457}
00bf0b85 15458
1042e4c0
SS
15459/* Print information on tracepoint number TPNUM_EXP, or all if
15460 omitted. */
15461
15462static void
e5a67952 15463tracepoints_info (char *args, int from_tty)
1042e4c0 15464{
79a45e25 15465 struct ui_out *uiout = current_uiout;
e5a67952 15466 int num_printed;
1042e4c0 15467
e5a67952 15468 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15469
15470 if (num_printed == 0)
1042e4c0 15471 {
e5a67952 15472 if (args == NULL || *args == '\0')
d77f58be
SS
15473 ui_out_message (uiout, 0, "No tracepoints.\n");
15474 else
e5a67952 15475 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15476 }
ad443146
SS
15477
15478 default_collect_info ();
1042e4c0
SS
15479}
15480
4a64f543 15481/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15482 Not supported by all targets. */
15483static void
15484enable_trace_command (char *args, int from_tty)
15485{
15486 enable_command (args, from_tty);
15487}
15488
4a64f543 15489/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15490 Not supported by all targets. */
15491static void
15492disable_trace_command (char *args, int from_tty)
15493{
15494 disable_command (args, from_tty);
15495}
15496
4a64f543 15497/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15498static void
15499delete_trace_command (char *arg, int from_tty)
15500{
35df4500 15501 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15502
15503 dont_repeat ();
15504
15505 if (arg == 0)
15506 {
15507 int breaks_to_delete = 0;
15508
15509 /* Delete all breakpoints if no argument.
15510 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15511 have to be deleted with an explicit breakpoint number
15512 argument. */
1042e4c0 15513 ALL_TRACEPOINTS (b)
46c6471b 15514 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15515 {
15516 breaks_to_delete = 1;
15517 break;
15518 }
1042e4c0
SS
15519
15520 /* Ask user only if there are some breakpoints to delete. */
15521 if (!from_tty
15522 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15523 {
35df4500 15524 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15525 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15526 delete_breakpoint (b);
1042e4c0
SS
15527 }
15528 }
15529 else
51be5b68 15530 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15531}
15532
197f0a60
TT
15533/* Helper function for trace_pass_command. */
15534
15535static void
d9b3f62e 15536trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15537{
d9b3f62e 15538 tp->pass_count = count;
6f6484cd 15539 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15540 if (from_tty)
15541 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15542 tp->base.number, count);
197f0a60
TT
15543}
15544
1042e4c0
SS
15545/* Set passcount for tracepoint.
15546
15547 First command argument is passcount, second is tracepoint number.
15548 If tracepoint number omitted, apply to most recently defined.
15549 Also accepts special argument "all". */
15550
15551static void
15552trace_pass_command (char *args, int from_tty)
15553{
d9b3f62e 15554 struct tracepoint *t1;
1042e4c0 15555 unsigned int count;
1042e4c0
SS
15556
15557 if (args == 0 || *args == 0)
3e43a32a
MS
15558 error (_("passcount command requires an "
15559 "argument (count + optional TP num)"));
1042e4c0 15560
4a64f543 15561 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15562
529480d0 15563 args = skip_spaces (args);
1042e4c0
SS
15564 if (*args && strncasecmp (args, "all", 3) == 0)
15565 {
d9b3f62e
PA
15566 struct breakpoint *b;
15567
1042e4c0 15568 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15569 if (*args)
15570 error (_("Junk at end of arguments."));
1042e4c0 15571
d9b3f62e 15572 ALL_TRACEPOINTS (b)
197f0a60 15573 {
d9b3f62e 15574 t1 = (struct tracepoint *) b;
197f0a60
TT
15575 trace_pass_set_count (t1, count, from_tty);
15576 }
15577 }
15578 else if (*args == '\0')
1042e4c0 15579 {
5fa1d40e 15580 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15581 if (t1)
197f0a60
TT
15582 trace_pass_set_count (t1, count, from_tty);
15583 }
15584 else
15585 {
15586 struct get_number_or_range_state state;
15587
15588 init_number_or_range (&state, args);
15589 while (!state.finished)
1042e4c0 15590 {
5fa1d40e 15591 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15592 if (t1)
15593 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15594 }
15595 }
1042e4c0
SS
15596}
15597
d9b3f62e 15598struct tracepoint *
1042e4c0
SS
15599get_tracepoint (int num)
15600{
15601 struct breakpoint *t;
15602
15603 ALL_TRACEPOINTS (t)
15604 if (t->number == num)
d9b3f62e 15605 return (struct tracepoint *) t;
1042e4c0
SS
15606
15607 return NULL;
15608}
15609
d5551862
SS
15610/* Find the tracepoint with the given target-side number (which may be
15611 different from the tracepoint number after disconnecting and
15612 reconnecting). */
15613
d9b3f62e 15614struct tracepoint *
d5551862
SS
15615get_tracepoint_by_number_on_target (int num)
15616{
d9b3f62e 15617 struct breakpoint *b;
d5551862 15618
d9b3f62e
PA
15619 ALL_TRACEPOINTS (b)
15620 {
15621 struct tracepoint *t = (struct tracepoint *) b;
15622
15623 if (t->number_on_target == num)
15624 return t;
15625 }
d5551862
SS
15626
15627 return NULL;
15628}
15629
1042e4c0 15630/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15631 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15632 If the argument is missing, the most recent tracepoint
15633 (tracepoint_count) is returned. */
15634
d9b3f62e 15635struct tracepoint *
197f0a60 15636get_tracepoint_by_number (char **arg,
5fa1d40e 15637 struct get_number_or_range_state *state)
1042e4c0 15638{
1042e4c0
SS
15639 struct breakpoint *t;
15640 int tpnum;
15641 char *instring = arg == NULL ? NULL : *arg;
15642
197f0a60
TT
15643 if (state)
15644 {
15645 gdb_assert (!state->finished);
15646 tpnum = get_number_or_range (state);
15647 }
15648 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15649 tpnum = tracepoint_count;
1042e4c0 15650 else
197f0a60 15651 tpnum = get_number (arg);
1042e4c0
SS
15652
15653 if (tpnum <= 0)
15654 {
15655 if (instring && *instring)
15656 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15657 instring);
15658 else
5fa1d40e 15659 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15660 return NULL;
15661 }
15662
15663 ALL_TRACEPOINTS (t)
15664 if (t->number == tpnum)
15665 {
d9b3f62e 15666 return (struct tracepoint *) t;
1042e4c0
SS
15667 }
15668
1042e4c0
SS
15669 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15670 return NULL;
15671}
15672
d9b3f62e
PA
15673void
15674print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15675{
15676 if (b->thread != -1)
15677 fprintf_unfiltered (fp, " thread %d", b->thread);
15678
15679 if (b->task != 0)
15680 fprintf_unfiltered (fp, " task %d", b->task);
15681
15682 fprintf_unfiltered (fp, "\n");
15683}
15684
6149aea9
PA
15685/* Save information on user settable breakpoints (watchpoints, etc) to
15686 a new script file named FILENAME. If FILTER is non-NULL, call it
15687 on each breakpoint and only include the ones for which it returns
15688 non-zero. */
15689
1042e4c0 15690static void
6149aea9
PA
15691save_breakpoints (char *filename, int from_tty,
15692 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15693{
15694 struct breakpoint *tp;
6149aea9 15695 int any = 0;
1042e4c0 15696 struct cleanup *cleanup;
a7bdde9e 15697 struct ui_file *fp;
6149aea9 15698 int extra_trace_bits = 0;
1042e4c0 15699
6149aea9
PA
15700 if (filename == 0 || *filename == 0)
15701 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15702
15703 /* See if we have anything to save. */
6149aea9 15704 ALL_BREAKPOINTS (tp)
1042e4c0 15705 {
6149aea9 15706 /* Skip internal and momentary breakpoints. */
09d682a4 15707 if (!user_breakpoint_p (tp))
6149aea9
PA
15708 continue;
15709
15710 /* If we have a filter, only save the breakpoints it accepts. */
15711 if (filter && !filter (tp))
15712 continue;
15713
15714 any = 1;
15715
15716 if (is_tracepoint (tp))
15717 {
15718 extra_trace_bits = 1;
15719
15720 /* We can stop searching. */
15721 break;
15722 }
1042e4c0 15723 }
6149aea9
PA
15724
15725 if (!any)
1042e4c0 15726 {
6149aea9 15727 warning (_("Nothing to save."));
1042e4c0
SS
15728 return;
15729 }
15730
c718be47
PA
15731 filename = tilde_expand (filename);
15732 cleanup = make_cleanup (xfree, filename);
15733 fp = gdb_fopen (filename, "w");
059fb39f 15734 if (!fp)
6149aea9
PA
15735 error (_("Unable to open file '%s' for saving (%s)"),
15736 filename, safe_strerror (errno));
a7bdde9e 15737 make_cleanup_ui_file_delete (fp);
8bf6485c 15738
6149aea9
PA
15739 if (extra_trace_bits)
15740 save_trace_state_variables (fp);
8bf6485c 15741
6149aea9 15742 ALL_BREAKPOINTS (tp)
1042e4c0 15743 {
6149aea9 15744 /* Skip internal and momentary breakpoints. */
09d682a4 15745 if (!user_breakpoint_p (tp))
6149aea9 15746 continue;
8bf6485c 15747
6149aea9
PA
15748 /* If we have a filter, only save the breakpoints it accepts. */
15749 if (filter && !filter (tp))
15750 continue;
15751
348d480f 15752 tp->ops->print_recreate (tp, fp);
1042e4c0 15753
6149aea9
PA
15754 /* Note, we can't rely on tp->number for anything, as we can't
15755 assume the recreated breakpoint numbers will match. Use $bpnum
15756 instead. */
15757
15758 if (tp->cond_string)
15759 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15760
15761 if (tp->ignore_count)
15762 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15763
2d9442cc 15764 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15765 {
6149aea9 15766 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15767
79a45e25 15768 ui_out_redirect (current_uiout, fp);
492d29ea 15769 TRY
1042e4c0 15770 {
79a45e25 15771 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15772 }
492d29ea
PA
15773 CATCH (ex, RETURN_MASK_ALL)
15774 {
6c63c96a 15775 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15776 throw_exception (ex);
15777 }
15778 END_CATCH
1042e4c0 15779
6c63c96a 15780 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15781 fprintf_unfiltered (fp, " end\n");
1042e4c0 15782 }
6149aea9
PA
15783
15784 if (tp->enable_state == bp_disabled)
99894e11 15785 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15786
15787 /* If this is a multi-location breakpoint, check if the locations
15788 should be individually disabled. Watchpoint locations are
15789 special, and not user visible. */
15790 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15791 {
15792 struct bp_location *loc;
15793 int n = 1;
15794
15795 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15796 if (!loc->enabled)
15797 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15798 }
1042e4c0 15799 }
8bf6485c 15800
6149aea9 15801 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15802 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15803
1042e4c0 15804 if (from_tty)
6149aea9 15805 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15806 do_cleanups (cleanup);
6149aea9
PA
15807}
15808
15809/* The `save breakpoints' command. */
15810
15811static void
15812save_breakpoints_command (char *args, int from_tty)
15813{
15814 save_breakpoints (args, from_tty, NULL);
15815}
15816
15817/* The `save tracepoints' command. */
15818
15819static void
15820save_tracepoints_command (char *args, int from_tty)
15821{
15822 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15823}
15824
15825/* Create a vector of all tracepoints. */
15826
15827VEC(breakpoint_p) *
eeae04df 15828all_tracepoints (void)
1042e4c0
SS
15829{
15830 VEC(breakpoint_p) *tp_vec = 0;
15831 struct breakpoint *tp;
15832
15833 ALL_TRACEPOINTS (tp)
15834 {
15835 VEC_safe_push (breakpoint_p, tp_vec, tp);
15836 }
15837
15838 return tp_vec;
15839}
15840
c906108c 15841\f
629500fa
KS
15842/* This help string is used to consolidate all the help string for specifying
15843 locations used by several commands. */
15844
15845#define LOCATION_HELP_STRING \
15846"Linespecs are colon-separated lists of location parameters, such as\n\
15847source filename, function name, label name, and line number.\n\
15848Example: To specify the start of a label named \"the_top\" in the\n\
15849function \"fact\" in the file \"factorial.c\", use\n\
15850\"factorial.c:fact:the_top\".\n\
15851\n\
15852Address locations begin with \"*\" and specify an exact address in the\n\
15853program. Example: To specify the fourth byte past the start function\n\
15854\"main\", use \"*main + 4\".\n\
15855\n\
15856Explicit locations are similar to linespecs but use an option/argument\n\
15857syntax to specify location parameters.\n\
15858Example: To specify the start of the label named \"the_top\" in the\n\
15859function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15860-function fact -label the_top\".\n"
15861
4a64f543
MS
15862/* This help string is used for the break, hbreak, tbreak and thbreak
15863 commands. It is defined as a macro to prevent duplication.
15864 COMMAND should be a string constant containing the name of the
15865 command. */
629500fa 15866
31e2b00f 15867#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15868command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15869PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15870probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15871guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15872`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15873LOCATION may be a linespec, address, or explicit location as described\n\
15874below.\n\
15875\n\
dc10affe
PA
15876With no LOCATION, uses current execution address of the selected\n\
15877stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15878\n\
15879THREADNUM is the number from \"info threads\".\n\
15880CONDITION is a boolean expression.\n\
629500fa 15881\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15882Multiple breakpoints at one place are permitted, and useful if their\n\
15883conditions are different.\n\
31e2b00f
AS
15884\n\
15885Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15886
44feb3ce
TT
15887/* List of subcommands for "catch". */
15888static struct cmd_list_element *catch_cmdlist;
15889
15890/* List of subcommands for "tcatch". */
15891static struct cmd_list_element *tcatch_cmdlist;
15892
9ac4176b 15893void
44feb3ce 15894add_catch_command (char *name, char *docstring,
82ae6c8d 15895 cmd_sfunc_ftype *sfunc,
625e8578 15896 completer_ftype *completer,
44feb3ce
TT
15897 void *user_data_catch,
15898 void *user_data_tcatch)
15899{
15900 struct cmd_list_element *command;
15901
15902 command = add_cmd (name, class_breakpoint, NULL, docstring,
15903 &catch_cmdlist);
15904 set_cmd_sfunc (command, sfunc);
15905 set_cmd_context (command, user_data_catch);
a96d9b2e 15906 set_cmd_completer (command, completer);
44feb3ce
TT
15907
15908 command = add_cmd (name, class_breakpoint, NULL, docstring,
15909 &tcatch_cmdlist);
15910 set_cmd_sfunc (command, sfunc);
15911 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15912 set_cmd_completer (command, completer);
44feb3ce
TT
15913}
15914
6149aea9
PA
15915static void
15916save_command (char *arg, int from_tty)
15917{
3e43a32a
MS
15918 printf_unfiltered (_("\"save\" must be followed by "
15919 "the name of a save subcommand.\n"));
635c7e8a 15920 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15921}
15922
84f4c1fe
PM
15923struct breakpoint *
15924iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15925 void *data)
15926{
35df4500 15927 struct breakpoint *b, *b_tmp;
84f4c1fe 15928
35df4500 15929 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15930 {
15931 if ((*callback) (b, data))
15932 return b;
15933 }
15934
15935 return NULL;
15936}
15937
0574c78f
GB
15938/* Zero if any of the breakpoint's locations could be a location where
15939 functions have been inlined, nonzero otherwise. */
15940
15941static int
15942is_non_inline_function (struct breakpoint *b)
15943{
15944 /* The shared library event breakpoint is set on the address of a
15945 non-inline function. */
15946 if (b->type == bp_shlib_event)
15947 return 1;
15948
15949 return 0;
15950}
15951
15952/* Nonzero if the specified PC cannot be a location where functions
15953 have been inlined. */
15954
15955int
09ac7c10
TT
15956pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15957 const struct target_waitstatus *ws)
0574c78f
GB
15958{
15959 struct breakpoint *b;
15960 struct bp_location *bl;
15961
15962 ALL_BREAKPOINTS (b)
15963 {
15964 if (!is_non_inline_function (b))
15965 continue;
15966
15967 for (bl = b->loc; bl != NULL; bl = bl->next)
15968 {
15969 if (!bl->shlib_disabled
09ac7c10 15970 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15971 return 1;
15972 }
15973 }
15974
15975 return 0;
15976}
15977
2f202fde
JK
15978/* Remove any references to OBJFILE which is going to be freed. */
15979
15980void
15981breakpoint_free_objfile (struct objfile *objfile)
15982{
15983 struct bp_location **locp, *loc;
15984
15985 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15986 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15987 loc->symtab = NULL;
15988}
15989
2060206e
PA
15990void
15991initialize_breakpoint_ops (void)
15992{
15993 static int initialized = 0;
15994
15995 struct breakpoint_ops *ops;
15996
15997 if (initialized)
15998 return;
15999 initialized = 1;
16000
16001 /* The breakpoint_ops structure to be inherit by all kinds of
16002 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16003 internal and momentary breakpoints, etc.). */
16004 ops = &bkpt_base_breakpoint_ops;
16005 *ops = base_breakpoint_ops;
16006 ops->re_set = bkpt_re_set;
16007 ops->insert_location = bkpt_insert_location;
16008 ops->remove_location = bkpt_remove_location;
16009 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 16010 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 16011 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 16012 ops->decode_location = bkpt_decode_location;
2060206e
PA
16013
16014 /* The breakpoint_ops structure to be used in regular breakpoints. */
16015 ops = &bkpt_breakpoint_ops;
16016 *ops = bkpt_base_breakpoint_ops;
16017 ops->re_set = bkpt_re_set;
16018 ops->resources_needed = bkpt_resources_needed;
16019 ops->print_it = bkpt_print_it;
16020 ops->print_mention = bkpt_print_mention;
16021 ops->print_recreate = bkpt_print_recreate;
16022
16023 /* Ranged breakpoints. */
16024 ops = &ranged_breakpoint_ops;
16025 *ops = bkpt_breakpoint_ops;
16026 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16027 ops->resources_needed = resources_needed_ranged_breakpoint;
16028 ops->print_it = print_it_ranged_breakpoint;
16029 ops->print_one = print_one_ranged_breakpoint;
16030 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16031 ops->print_mention = print_mention_ranged_breakpoint;
16032 ops->print_recreate = print_recreate_ranged_breakpoint;
16033
16034 /* Internal breakpoints. */
16035 ops = &internal_breakpoint_ops;
16036 *ops = bkpt_base_breakpoint_ops;
16037 ops->re_set = internal_bkpt_re_set;
16038 ops->check_status = internal_bkpt_check_status;
16039 ops->print_it = internal_bkpt_print_it;
16040 ops->print_mention = internal_bkpt_print_mention;
16041
16042 /* Momentary breakpoints. */
16043 ops = &momentary_breakpoint_ops;
16044 *ops = bkpt_base_breakpoint_ops;
16045 ops->re_set = momentary_bkpt_re_set;
16046 ops->check_status = momentary_bkpt_check_status;
16047 ops->print_it = momentary_bkpt_print_it;
16048 ops->print_mention = momentary_bkpt_print_mention;
16049
e2e4d78b
JK
16050 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16051 ops = &longjmp_breakpoint_ops;
16052 *ops = momentary_breakpoint_ops;
16053 ops->dtor = longjmp_bkpt_dtor;
16054
55aa24fb
SDJ
16055 /* Probe breakpoints. */
16056 ops = &bkpt_probe_breakpoint_ops;
16057 *ops = bkpt_breakpoint_ops;
16058 ops->insert_location = bkpt_probe_insert_location;
16059 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
16060 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16061 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 16062
2060206e
PA
16063 /* Watchpoints. */
16064 ops = &watchpoint_breakpoint_ops;
16065 *ops = base_breakpoint_ops;
3a5c3e22 16066 ops->dtor = dtor_watchpoint;
2060206e
PA
16067 ops->re_set = re_set_watchpoint;
16068 ops->insert_location = insert_watchpoint;
16069 ops->remove_location = remove_watchpoint;
16070 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16071 ops->check_status = check_status_watchpoint;
16072 ops->resources_needed = resources_needed_watchpoint;
16073 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16074 ops->print_it = print_it_watchpoint;
16075 ops->print_mention = print_mention_watchpoint;
16076 ops->print_recreate = print_recreate_watchpoint;
427cd150 16077 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16078
16079 /* Masked watchpoints. */
16080 ops = &masked_watchpoint_breakpoint_ops;
16081 *ops = watchpoint_breakpoint_ops;
16082 ops->insert_location = insert_masked_watchpoint;
16083 ops->remove_location = remove_masked_watchpoint;
16084 ops->resources_needed = resources_needed_masked_watchpoint;
16085 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16086 ops->print_it = print_it_masked_watchpoint;
16087 ops->print_one_detail = print_one_detail_masked_watchpoint;
16088 ops->print_mention = print_mention_masked_watchpoint;
16089 ops->print_recreate = print_recreate_masked_watchpoint;
16090
16091 /* Tracepoints. */
16092 ops = &tracepoint_breakpoint_ops;
16093 *ops = base_breakpoint_ops;
16094 ops->re_set = tracepoint_re_set;
16095 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16096 ops->print_one_detail = tracepoint_print_one_detail;
16097 ops->print_mention = tracepoint_print_mention;
16098 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16099 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16100 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16101 ops->decode_location = tracepoint_decode_location;
983af33b 16102
55aa24fb
SDJ
16103 /* Probe tracepoints. */
16104 ops = &tracepoint_probe_breakpoint_ops;
16105 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16106 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16107 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16108
983af33b
SDJ
16109 /* Static tracepoints with marker (`-m'). */
16110 ops = &strace_marker_breakpoint_ops;
16111 *ops = tracepoint_breakpoint_ops;
5f700d83 16112 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16113 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16114 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16115
16116 /* Fork catchpoints. */
16117 ops = &catch_fork_breakpoint_ops;
16118 *ops = base_breakpoint_ops;
16119 ops->insert_location = insert_catch_fork;
16120 ops->remove_location = remove_catch_fork;
16121 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16122 ops->print_it = print_it_catch_fork;
16123 ops->print_one = print_one_catch_fork;
16124 ops->print_mention = print_mention_catch_fork;
16125 ops->print_recreate = print_recreate_catch_fork;
16126
16127 /* Vfork catchpoints. */
16128 ops = &catch_vfork_breakpoint_ops;
16129 *ops = base_breakpoint_ops;
16130 ops->insert_location = insert_catch_vfork;
16131 ops->remove_location = remove_catch_vfork;
16132 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16133 ops->print_it = print_it_catch_vfork;
16134 ops->print_one = print_one_catch_vfork;
16135 ops->print_mention = print_mention_catch_vfork;
16136 ops->print_recreate = print_recreate_catch_vfork;
16137
16138 /* Exec catchpoints. */
16139 ops = &catch_exec_breakpoint_ops;
16140 *ops = base_breakpoint_ops;
16141 ops->dtor = dtor_catch_exec;
16142 ops->insert_location = insert_catch_exec;
16143 ops->remove_location = remove_catch_exec;
16144 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16145 ops->print_it = print_it_catch_exec;
16146 ops->print_one = print_one_catch_exec;
16147 ops->print_mention = print_mention_catch_exec;
16148 ops->print_recreate = print_recreate_catch_exec;
16149
edcc5120
TT
16150 /* Solib-related catchpoints. */
16151 ops = &catch_solib_breakpoint_ops;
16152 *ops = base_breakpoint_ops;
16153 ops->dtor = dtor_catch_solib;
16154 ops->insert_location = insert_catch_solib;
16155 ops->remove_location = remove_catch_solib;
16156 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16157 ops->check_status = check_status_catch_solib;
16158 ops->print_it = print_it_catch_solib;
16159 ops->print_one = print_one_catch_solib;
16160 ops->print_mention = print_mention_catch_solib;
16161 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16162
16163 ops = &dprintf_breakpoint_ops;
16164 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16165 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16166 ops->resources_needed = bkpt_resources_needed;
16167 ops->print_it = bkpt_print_it;
16168 ops->print_mention = bkpt_print_mention;
2d9442cc 16169 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16170 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16171 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16172}
16173
8bfd80db
YQ
16174/* Chain containing all defined "enable breakpoint" subcommands. */
16175
16176static struct cmd_list_element *enablebreaklist = NULL;
16177
c906108c 16178void
fba45db2 16179_initialize_breakpoint (void)
c906108c
SS
16180{
16181 struct cmd_list_element *c;
16182
2060206e
PA
16183 initialize_breakpoint_ops ();
16184
84acb35a 16185 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16186 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16187 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16188
55aa24fb
SDJ
16189 breakpoint_objfile_key
16190 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16191
c906108c
SS
16192 breakpoint_chain = 0;
16193 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16194 before a breakpoint is set. */
16195 breakpoint_count = 0;
16196
1042e4c0
SS
16197 tracepoint_count = 0;
16198
1bedd215
AC
16199 add_com ("ignore", class_breakpoint, ignore_command, _("\
16200Set ignore-count of breakpoint number N to COUNT.\n\
16201Usage is `ignore N COUNT'."));
c906108c 16202
1bedd215
AC
16203 add_com ("commands", class_breakpoint, commands_command, _("\
16204Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16205Give breakpoint number as argument after \"commands\".\n\
16206With no argument, the targeted breakpoint is the last one set.\n\
16207The commands themselves follow starting on the next line.\n\
16208Type a line containing \"end\" to indicate the end of them.\n\
16209Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16210then no output is printed when it is hit, except what the commands print."));
c906108c 16211
d55637df 16212 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16213Specify breakpoint number N to break only if COND is true.\n\
c906108c 16214Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16215expression to be evaluated whenever breakpoint N is reached."));
d55637df 16216 set_cmd_completer (c, condition_completer);
c906108c 16217
1bedd215 16218 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16219Set a temporary breakpoint.\n\
c906108c
SS
16220Like \"break\" except the breakpoint is only temporary,\n\
16221so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16222by using \"enable delete\" on the breakpoint number.\n\
16223\n"
16224BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16225 set_cmd_completer (c, location_completer);
c94fdfd0 16226
1bedd215 16227 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16228Set a hardware assisted breakpoint.\n\
c906108c 16229Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16230some target hardware may not have this support.\n\
16231\n"
16232BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16233 set_cmd_completer (c, location_completer);
c906108c 16234
1bedd215 16235 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16236Set a temporary hardware assisted breakpoint.\n\
c906108c 16237Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16238so it will be deleted when hit.\n\
16239\n"
16240BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16241 set_cmd_completer (c, location_completer);
c906108c 16242
1bedd215
AC
16243 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16244Enable some breakpoints.\n\
c906108c
SS
16245Give breakpoint numbers (separated by spaces) as arguments.\n\
16246With no subcommand, breakpoints are enabled until you command otherwise.\n\
16247This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16248With a subcommand you can enable temporarily."),
c906108c 16249 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16250
16251 add_com_alias ("en", "enable", class_breakpoint, 1);
16252
84951ab5 16253 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16254Enable some breakpoints.\n\
c906108c
SS
16255Give breakpoint numbers (separated by spaces) as arguments.\n\
16256This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16257May be abbreviated to simply \"enable\".\n"),
c5aa993b 16258 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16259
1a966eab
AC
16260 add_cmd ("once", no_class, enable_once_command, _("\
16261Enable breakpoints for one hit. Give breakpoint numbers.\n\
16262If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16263 &enablebreaklist);
16264
1a966eab
AC
16265 add_cmd ("delete", no_class, enable_delete_command, _("\
16266Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16267If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16268 &enablebreaklist);
16269
816338b5
SS
16270 add_cmd ("count", no_class, enable_count_command, _("\
16271Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16272If a breakpoint is hit while enabled in this fashion,\n\
16273the count is decremented; when it reaches zero, the breakpoint is disabled."),
16274 &enablebreaklist);
16275
1a966eab
AC
16276 add_cmd ("delete", no_class, enable_delete_command, _("\
16277Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16278If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16279 &enablelist);
16280
1a966eab
AC
16281 add_cmd ("once", no_class, enable_once_command, _("\
16282Enable breakpoints for one hit. Give breakpoint numbers.\n\
16283If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16284 &enablelist);
16285
16286 add_cmd ("count", no_class, enable_count_command, _("\
16287Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16288If a breakpoint is hit while enabled in this fashion,\n\
16289the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16290 &enablelist);
16291
1bedd215
AC
16292 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16293Disable some breakpoints.\n\
c906108c
SS
16294Arguments are breakpoint numbers with spaces in between.\n\
16295To disable all breakpoints, give no argument.\n\
64b9b334 16296A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16297 &disablelist, "disable ", 1, &cmdlist);
16298 add_com_alias ("dis", "disable", class_breakpoint, 1);
16299 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16300
1a966eab
AC
16301 add_cmd ("breakpoints", class_alias, disable_command, _("\
16302Disable some breakpoints.\n\
c906108c
SS
16303Arguments are breakpoint numbers with spaces in between.\n\
16304To disable all breakpoints, give no argument.\n\
64b9b334 16305A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16306This command may be abbreviated \"disable\"."),
c906108c
SS
16307 &disablelist);
16308
1bedd215
AC
16309 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16310Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16311Arguments are breakpoint numbers with spaces in between.\n\
16312To delete all breakpoints, give no argument.\n\
16313\n\
16314Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16315The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16316 &deletelist, "delete ", 1, &cmdlist);
16317 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16318 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16319
1a966eab
AC
16320 add_cmd ("breakpoints", class_alias, delete_command, _("\
16321Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16322Arguments are breakpoint numbers with spaces in between.\n\
16323To delete all breakpoints, give no argument.\n\
1a966eab 16324This command may be abbreviated \"delete\"."),
c906108c
SS
16325 &deletelist);
16326
1bedd215 16327 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16328Clear breakpoint at specified location.\n\
16329Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16330\n\
16331With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16332is executing in.\n"
16333"\n" LOCATION_HELP_STRING "\n\
1bedd215 16334See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16335 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16336
1bedd215 16337 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16338Set breakpoint at specified location.\n"
31e2b00f 16339BREAK_ARGS_HELP ("break")));
5ba2abeb 16340 set_cmd_completer (c, location_completer);
c94fdfd0 16341
c906108c
SS
16342 add_com_alias ("b", "break", class_run, 1);
16343 add_com_alias ("br", "break", class_run, 1);
16344 add_com_alias ("bre", "break", class_run, 1);
16345 add_com_alias ("brea", "break", class_run, 1);
16346
c906108c
SS
16347 if (dbx_commands)
16348 {
1bedd215
AC
16349 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16350Break in function/address or break at a line in the current file."),
c5aa993b
JM
16351 &stoplist, "stop ", 1, &cmdlist);
16352 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16353 _("Break in function or address."), &stoplist);
c5aa993b 16354 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16355 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16356 add_com ("status", class_info, breakpoints_info, _("\
16357Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16358The \"Type\" column indicates one of:\n\
16359\tbreakpoint - normal breakpoint\n\
16360\twatchpoint - watchpoint\n\
16361The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16362the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16363breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16364address and file/line number respectively.\n\
16365\n\
16366Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16367are set to the address of the last breakpoint listed unless the command\n\
16368is prefixed with \"server \".\n\n\
c906108c 16369Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16370breakpoint set."));
c906108c
SS
16371 }
16372
1bedd215 16373 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16374Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16375The \"Type\" column indicates one of:\n\
16376\tbreakpoint - normal breakpoint\n\
16377\twatchpoint - watchpoint\n\
16378The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16379the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16380breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16381address and file/line number respectively.\n\
16382\n\
16383Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16384are set to the address of the last breakpoint listed unless the command\n\
16385is prefixed with \"server \".\n\n\
c906108c 16386Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16387breakpoint set."));
c906108c 16388
6b04bdb7
MS
16389 add_info_alias ("b", "breakpoints", 1);
16390
1a966eab
AC
16391 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16392Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16393The \"Type\" column indicates one of:\n\
16394\tbreakpoint - normal breakpoint\n\
16395\twatchpoint - watchpoint\n\
16396\tlongjmp - internal breakpoint used to step through longjmp()\n\
16397\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16398\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16399\tfinish - internal breakpoint used by the \"finish\" command\n\
16400The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16401the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16402breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16403address and file/line number respectively.\n\
16404\n\
16405Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16406are set to the address of the last breakpoint listed unless the command\n\
16407is prefixed with \"server \".\n\n\
c906108c 16408Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16409breakpoint set."),
c906108c
SS
16410 &maintenanceinfolist);
16411
44feb3ce
TT
16412 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16413Set catchpoints to catch events."),
16414 &catch_cmdlist, "catch ",
16415 0/*allow-unknown*/, &cmdlist);
16416
16417 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16418Set temporary catchpoints to catch events."),
16419 &tcatch_cmdlist, "tcatch ",
16420 0/*allow-unknown*/, &cmdlist);
16421
44feb3ce
TT
16422 add_catch_command ("fork", _("Catch calls to fork."),
16423 catch_fork_command_1,
a96d9b2e 16424 NULL,
44feb3ce
TT
16425 (void *) (uintptr_t) catch_fork_permanent,
16426 (void *) (uintptr_t) catch_fork_temporary);
16427 add_catch_command ("vfork", _("Catch calls to vfork."),
16428 catch_fork_command_1,
a96d9b2e 16429 NULL,
44feb3ce
TT
16430 (void *) (uintptr_t) catch_vfork_permanent,
16431 (void *) (uintptr_t) catch_vfork_temporary);
16432 add_catch_command ("exec", _("Catch calls to exec."),
16433 catch_exec_command_1,
a96d9b2e
SDJ
16434 NULL,
16435 CATCH_PERMANENT,
16436 CATCH_TEMPORARY);
edcc5120
TT
16437 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16438Usage: catch load [REGEX]\n\
16439If REGEX is given, only stop for libraries matching the regular expression."),
16440 catch_load_command_1,
16441 NULL,
16442 CATCH_PERMANENT,
16443 CATCH_TEMPORARY);
16444 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16445Usage: catch unload [REGEX]\n\
16446If REGEX is given, only stop for libraries matching the regular expression."),
16447 catch_unload_command_1,
16448 NULL,
16449 CATCH_PERMANENT,
16450 CATCH_TEMPORARY);
c5aa993b 16451
1bedd215
AC
16452 c = add_com ("watch", class_breakpoint, watch_command, _("\
16453Set a watchpoint for an expression.\n\
06a64a0b 16454Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16455A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16456an expression changes.\n\
16457If -l or -location is given, this evaluates EXPRESSION and watches\n\
16458the memory to which it refers."));
65d12d83 16459 set_cmd_completer (c, expression_completer);
c906108c 16460
1bedd215
AC
16461 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16462Set a read watchpoint for an expression.\n\
06a64a0b 16463Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16464A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16465an expression is read.\n\
16466If -l or -location is given, this evaluates EXPRESSION and watches\n\
16467the memory to which it refers."));
65d12d83 16468 set_cmd_completer (c, expression_completer);
c906108c 16469
1bedd215
AC
16470 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16471Set a watchpoint for an expression.\n\
06a64a0b 16472Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16473A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16474an expression is either read or written.\n\
16475If -l or -location is given, this evaluates EXPRESSION and watches\n\
16476the memory to which it refers."));
65d12d83 16477 set_cmd_completer (c, expression_completer);
c906108c 16478
d77f58be 16479 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16480Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16481
920d2a44
AC
16482 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16483 respond to changes - contrary to the description. */
85c07804
AC
16484 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16485 &can_use_hw_watchpoints, _("\
16486Set debugger's willingness to use watchpoint hardware."), _("\
16487Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16488If zero, gdb will not use hardware for new watchpoints, even if\n\
16489such is available. (However, any hardware watchpoints that were\n\
16490created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16491hardware.)"),
16492 NULL,
920d2a44 16493 show_can_use_hw_watchpoints,
85c07804 16494 &setlist, &showlist);
c906108c
SS
16495
16496 can_use_hw_watchpoints = 1;
fa8d40ab 16497
1042e4c0
SS
16498 /* Tracepoint manipulation commands. */
16499
16500 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16501Set a tracepoint at specified location.\n\
1042e4c0
SS
16502\n"
16503BREAK_ARGS_HELP ("trace") "\n\
16504Do \"help tracepoints\" for info on other tracepoint commands."));
16505 set_cmd_completer (c, location_completer);
16506
16507 add_com_alias ("tp", "trace", class_alias, 0);
16508 add_com_alias ("tr", "trace", class_alias, 1);
16509 add_com_alias ("tra", "trace", class_alias, 1);
16510 add_com_alias ("trac", "trace", class_alias, 1);
16511
7a697b8d 16512 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16513Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16514\n"
16515BREAK_ARGS_HELP ("ftrace") "\n\
16516Do \"help tracepoints\" for info on other tracepoint commands."));
16517 set_cmd_completer (c, location_completer);
16518
0fb4aa4b 16519 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16520Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16521\n\
16522strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16523LOCATION may be a linespec, explicit, or address location (described below) \n\
16524or -m MARKER_ID.\n\n\
16525If a marker id is specified, probe the marker with that name. With\n\
16526no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16527Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16528This collects arbitrary user data passed in the probe point call to the\n\
16529tracing library. You can inspect it when analyzing the trace buffer,\n\
16530by printing the $_sdata variable like any other convenience variable.\n\
16531\n\
16532CONDITION is a boolean expression.\n\
629500fa 16533\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16534Multiple tracepoints at one place are permitted, and useful if their\n\
16535conditions are different.\n\
0fb4aa4b
PA
16536\n\
16537Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16538Do \"help tracepoints\" for info on other tracepoint commands."));
16539 set_cmd_completer (c, location_completer);
16540
1042e4c0 16541 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16542Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16543Convenience variable \"$tpnum\" contains the number of the\n\
16544last tracepoint set."));
16545
16546 add_info_alias ("tp", "tracepoints", 1);
16547
16548 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16549Delete specified tracepoints.\n\
16550Arguments are tracepoint numbers, separated by spaces.\n\
16551No argument means delete all tracepoints."),
16552 &deletelist);
7e20dfcd 16553 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16554
16555 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16556Disable specified tracepoints.\n\
16557Arguments are tracepoint numbers, separated by spaces.\n\
16558No argument means disable all tracepoints."),
16559 &disablelist);
16560 deprecate_cmd (c, "disable");
16561
16562 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16563Enable specified tracepoints.\n\
16564Arguments are tracepoint numbers, separated by spaces.\n\
16565No argument means enable all tracepoints."),
16566 &enablelist);
16567 deprecate_cmd (c, "enable");
16568
16569 add_com ("passcount", class_trace, trace_pass_command, _("\
16570Set the passcount for a tracepoint.\n\
16571The trace will end when the tracepoint has been passed 'count' times.\n\
16572Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16573if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16574
6149aea9
PA
16575 add_prefix_cmd ("save", class_breakpoint, save_command,
16576 _("Save breakpoint definitions as a script."),
16577 &save_cmdlist, "save ",
16578 0/*allow-unknown*/, &cmdlist);
16579
16580 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16581Save current breakpoint definitions as a script.\n\
cce7e648 16582This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16583catchpoints, tracepoints). Use the 'source' command in another debug\n\
16584session to restore them."),
16585 &save_cmdlist);
16586 set_cmd_completer (c, filename_completer);
16587
16588 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16589Save current tracepoint definitions as a script.\n\
6149aea9
PA
16590Use the 'source' command in another debug session to restore them."),
16591 &save_cmdlist);
1042e4c0
SS
16592 set_cmd_completer (c, filename_completer);
16593
6149aea9
PA
16594 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16595 deprecate_cmd (c, "save tracepoints");
16596
1bedd215 16597 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16598Breakpoint specific settings\n\
16599Configure various breakpoint-specific variables such as\n\
1bedd215 16600pending breakpoint behavior"),
fa8d40ab
JJ
16601 &breakpoint_set_cmdlist, "set breakpoint ",
16602 0/*allow-unknown*/, &setlist);
1bedd215 16603 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16604Breakpoint specific settings\n\
16605Configure various breakpoint-specific variables such as\n\
1bedd215 16606pending breakpoint behavior"),
fa8d40ab
JJ
16607 &breakpoint_show_cmdlist, "show breakpoint ",
16608 0/*allow-unknown*/, &showlist);
16609
7915a72c
AC
16610 add_setshow_auto_boolean_cmd ("pending", no_class,
16611 &pending_break_support, _("\
16612Set debugger's behavior regarding pending breakpoints."), _("\
16613Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16614If on, an unrecognized breakpoint location will cause gdb to create a\n\
16615pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16616an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16617user-query to see if a pending breakpoint should be created."),
2c5b56ce 16618 NULL,
920d2a44 16619 show_pending_break_support,
6e1d7d6c
AC
16620 &breakpoint_set_cmdlist,
16621 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16622
16623 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16624
16625 add_setshow_boolean_cmd ("auto-hw", no_class,
16626 &automatic_hardware_breakpoints, _("\
16627Set automatic usage of hardware breakpoints."), _("\
16628Show automatic usage of hardware breakpoints."), _("\
16629If set, the debugger will automatically use hardware breakpoints for\n\
16630breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16631a warning will be emitted for such breakpoints."),
16632 NULL,
16633 show_automatic_hardware_breakpoints,
16634 &breakpoint_set_cmdlist,
16635 &breakpoint_show_cmdlist);
74960c60 16636
a25a5a45
PA
16637 add_setshow_boolean_cmd ("always-inserted", class_support,
16638 &always_inserted_mode, _("\
74960c60
VP
16639Set mode for inserting breakpoints."), _("\
16640Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16641When this mode is on, breakpoints are inserted immediately as soon as\n\
16642they're created, kept inserted even when execution stops, and removed\n\
16643only when the user deletes them. When this mode is off (the default),\n\
16644breakpoints are inserted only when execution continues, and removed\n\
16645when execution stops."),
72d0e2c5
YQ
16646 NULL,
16647 &show_always_inserted_mode,
16648 &breakpoint_set_cmdlist,
16649 &breakpoint_show_cmdlist);
f1310107 16650
b775012e
LM
16651 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16652 condition_evaluation_enums,
16653 &condition_evaluation_mode_1, _("\
16654Set mode of breakpoint condition evaluation."), _("\
16655Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16656When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16657evaluated on the host's side by GDB. When it is set to \"target\",\n\
16658breakpoint conditions will be downloaded to the target (if the target\n\
16659supports such feature) and conditions will be evaluated on the target's side.\n\
16660If this is set to \"auto\" (default), this will be automatically set to\n\
16661\"target\" if it supports condition evaluation, otherwise it will\n\
16662be set to \"gdb\""),
16663 &set_condition_evaluation_mode,
16664 &show_condition_evaluation_mode,
16665 &breakpoint_set_cmdlist,
16666 &breakpoint_show_cmdlist);
16667
f1310107
TJB
16668 add_com ("break-range", class_breakpoint, break_range_command, _("\
16669Set a breakpoint for an address range.\n\
16670break-range START-LOCATION, END-LOCATION\n\
16671where START-LOCATION and END-LOCATION can be one of the following:\n\
16672 LINENUM, for that line in the current file,\n\
16673 FILE:LINENUM, for that line in that file,\n\
16674 +OFFSET, for that number of lines after the current line\n\
16675 or the start of the range\n\
16676 FUNCTION, for the first line in that function,\n\
16677 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16678 *ADDRESS, for the instruction at that address.\n\
16679\n\
16680The breakpoint will stop execution of the inferior whenever it executes\n\
16681an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16682range (including START-LOCATION and END-LOCATION)."));
16683
e7e0cddf 16684 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16685Set a dynamic printf at specified location.\n\
e7e0cddf 16686dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16687location may be a linespec, explicit, or address location.\n"
16688"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16689 set_cmd_completer (c, location_completer);
16690
16691 add_setshow_enum_cmd ("dprintf-style", class_support,
16692 dprintf_style_enums, &dprintf_style, _("\
16693Set the style of usage for dynamic printf."), _("\
16694Show the style of usage for dynamic printf."), _("\
16695This setting chooses how GDB will do a dynamic printf.\n\
16696If the value is \"gdb\", then the printing is done by GDB to its own\n\
16697console, as with the \"printf\" command.\n\
16698If the value is \"call\", the print is done by calling a function in your\n\
16699program; by default printf(), but you can choose a different function or\n\
16700output stream by setting dprintf-function and dprintf-channel."),
16701 update_dprintf_commands, NULL,
16702 &setlist, &showlist);
16703
16704 dprintf_function = xstrdup ("printf");
16705 add_setshow_string_cmd ("dprintf-function", class_support,
16706 &dprintf_function, _("\
16707Set the function to use for dynamic printf"), _("\
16708Show the function to use for dynamic printf"), NULL,
16709 update_dprintf_commands, NULL,
16710 &setlist, &showlist);
16711
16712 dprintf_channel = xstrdup ("");
16713 add_setshow_string_cmd ("dprintf-channel", class_support,
16714 &dprintf_channel, _("\
16715Set the channel to use for dynamic printf"), _("\
16716Show the channel to use for dynamic printf"), NULL,
16717 update_dprintf_commands, NULL,
16718 &setlist, &showlist);
16719
d3ce09f5
SS
16720 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16721 &disconnected_dprintf, _("\
16722Set whether dprintf continues after GDB disconnects."), _("\
16723Show whether dprintf continues after GDB disconnects."), _("\
16724Use this to let dprintf commands continue to hit and produce output\n\
16725even if GDB disconnects or detaches from the target."),
16726 NULL,
16727 NULL,
16728 &setlist, &showlist);
16729
16730 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16731agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16732(target agent only) This is useful for formatted output in user-defined commands."));
16733
765dc015 16734 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16735
16736 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16737 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16738}
This page took 5.879684 seconds and 4 git commands to generate.