Automatic date update in version.in
[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
c5aa993b
JM
197typedef enum
198 {
199 mark_inserted,
200 mark_uninserted
201 }
202insertion_state_t;
c906108c 203
0bde7532 204static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 205static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 206
e514a9d6 207static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 208
4efb68b1 209static int watchpoint_check (void *);
c906108c 210
a14ed312 211static void maintenance_info_breakpoints (char *, int);
c906108c 212
a14ed312 213static int hw_breakpoint_used_count (void);
c906108c 214
a1398e0c
PA
215static int hw_watchpoint_use_count (struct breakpoint *);
216
217static int hw_watchpoint_used_count_others (struct breakpoint *except,
218 enum bptype type,
219 int *other_type_used);
c906108c 220
a14ed312 221static void hbreak_command (char *, int);
c906108c 222
a14ed312 223static void thbreak_command (char *, int);
c906108c 224
816338b5
SS
225static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
226 int count);
c906108c 227
a14ed312 228static void stop_command (char *arg, int from_tty);
7a292a7a 229
a14ed312 230static void stopin_command (char *arg, int from_tty);
7a292a7a 231
a14ed312 232static void stopat_command (char *arg, int from_tty);
7a292a7a 233
a14ed312 234static void tcatch_command (char *arg, int from_tty);
7a292a7a 235
fe3f5fa8 236static void free_bp_location (struct bp_location *loc);
f431efe5
PA
237static void incref_bp_location (struct bp_location *loc);
238static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 239
39d61571 240static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 241
44702360
PA
242/* update_global_location_list's modes of operation wrt to whether to
243 insert locations now. */
244enum ugll_insert_mode
245{
246 /* Don't insert any breakpoint locations into the inferior, only
247 remove already-inserted locations that no longer should be
248 inserted. Functions that delete a breakpoint or breakpoints
249 should specify this mode, so that deleting a breakpoint doesn't
250 have the side effect of inserting the locations of other
251 breakpoints that are marked not-inserted, but should_be_inserted
252 returns true on them.
253
254 This behavior is useful is situations close to tear-down -- e.g.,
255 after an exec, while the target still has execution, but
256 breakpoint shadows of the previous executable image should *NOT*
257 be restored to the new image; or before detaching, where the
258 target still has execution and wants to delete breakpoints from
259 GDB's lists, and all breakpoints had already been removed from
260 the inferior. */
261 UGLL_DONT_INSERT,
262
a25a5a45
PA
263 /* May insert breakpoints iff breakpoints_should_be_inserted_now
264 claims breakpoints should be inserted now. */
04086b45
PA
265 UGLL_MAY_INSERT,
266
a25a5a45
PA
267 /* Insert locations now, irrespective of
268 breakpoints_should_be_inserted_now. E.g., say all threads are
269 stopped right now, and the user did "continue". We need to
270 insert breakpoints _before_ resuming the target, but
271 UGLL_MAY_INSERT wouldn't insert them, because
272 breakpoints_should_be_inserted_now returns false at that point,
273 as no thread is running yet. */
04086b45 274 UGLL_INSERT
44702360
PA
275};
276
277static void update_global_location_list (enum ugll_insert_mode);
a5606eee 278
44702360 279static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 280
d77f58be 281static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
282
283static void insert_breakpoint_locations (void);
a5606eee 284
1042e4c0
SS
285static void tracepoints_info (char *, int);
286
287static void delete_trace_command (char *, int);
288
289static void enable_trace_command (char *, int);
290
291static void disable_trace_command (char *, int);
292
293static void trace_pass_command (char *, int);
294
558a9d82
YQ
295static void set_tracepoint_count (int num);
296
9c06b0b4
TJB
297static int is_masked_watchpoint (const struct breakpoint *b);
298
b775012e
LM
299static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
300
983af33b
SDJ
301/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
302 otherwise. */
303
304static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 305
2060206e
PA
306/* The breakpoint_ops structure to be inherited by all breakpoint_ops
307 that are implemented on top of software or hardware breakpoints
308 (user breakpoints, internal and momentary breakpoints, etc.). */
309static struct breakpoint_ops bkpt_base_breakpoint_ops;
310
311/* Internal breakpoints class type. */
06edf0c0 312static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
313
314/* Momentary breakpoints class type. */
06edf0c0
PA
315static struct breakpoint_ops momentary_breakpoint_ops;
316
e2e4d78b
JK
317/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
318static struct breakpoint_ops longjmp_breakpoint_ops;
319
2060206e
PA
320/* The breakpoint_ops structure to be used in regular user created
321 breakpoints. */
322struct breakpoint_ops bkpt_breakpoint_ops;
323
55aa24fb
SDJ
324/* Breakpoints set on probes. */
325static struct breakpoint_ops bkpt_probe_breakpoint_ops;
326
e7e0cddf 327/* Dynamic printf class type. */
c5867ab6 328struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 329
d3ce09f5
SS
330/* The style in which to perform a dynamic printf. This is a user
331 option because different output options have different tradeoffs;
332 if GDB does the printing, there is better error handling if there
333 is a problem with any of the arguments, but using an inferior
334 function lets you have special-purpose printers and sending of
335 output to the same place as compiled-in print functions. */
336
337static const char dprintf_style_gdb[] = "gdb";
338static const char dprintf_style_call[] = "call";
339static const char dprintf_style_agent[] = "agent";
340static const char *const dprintf_style_enums[] = {
341 dprintf_style_gdb,
342 dprintf_style_call,
343 dprintf_style_agent,
344 NULL
345};
346static const char *dprintf_style = dprintf_style_gdb;
347
348/* The function to use for dynamic printf if the preferred style is to
349 call into the inferior. The value is simply a string that is
350 copied into the command, so it can be anything that GDB can
351 evaluate to a callable address, not necessarily a function name. */
352
353static char *dprintf_function = "";
354
355/* The channel to use for dynamic printf if the preferred style is to
356 call into the inferior; if a nonempty string, it will be passed to
357 the call as the first argument, with the format string as the
358 second. As with the dprintf function, this can be anything that
359 GDB knows how to evaluate, so in addition to common choices like
360 "stderr", this could be an app-specific expression like
361 "mystreams[curlogger]". */
362
363static char *dprintf_channel = "";
364
365/* True if dprintf commands should continue to operate even if GDB
366 has disconnected. */
367static int disconnected_dprintf = 1;
368
5cea2a26
PA
369/* A reference-counted struct command_line. This lets multiple
370 breakpoints share a single command list. */
371struct counted_command_line
372{
373 /* The reference count. */
374 int refc;
375
376 /* The command list. */
377 struct command_line *commands;
378};
379
380struct command_line *
381breakpoint_commands (struct breakpoint *b)
382{
383 return b->commands ? b->commands->commands : NULL;
384}
3daf8fe5 385
f3b1572e
PA
386/* Flag indicating that a command has proceeded the inferior past the
387 current breakpoint. */
388
389static int breakpoint_proceeded;
390
956a9fb9 391const char *
2cec12e5
AR
392bpdisp_text (enum bpdisp disp)
393{
4a64f543
MS
394 /* NOTE: the following values are a part of MI protocol and
395 represent values of 'disp' field returned when inferior stops at
396 a breakpoint. */
bc043ef3 397 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 398
2cec12e5
AR
399 return bpdisps[(int) disp];
400}
c906108c 401
4a64f543 402/* Prototypes for exported functions. */
c906108c 403/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 404 if such is available. */
c906108c
SS
405static int can_use_hw_watchpoints;
406
920d2a44
AC
407static void
408show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
409 struct cmd_list_element *c,
410 const char *value)
411{
3e43a32a
MS
412 fprintf_filtered (file,
413 _("Debugger's willingness to use "
414 "watchpoint hardware is %s.\n"),
920d2a44
AC
415 value);
416}
417
fa8d40ab
JJ
418/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
419 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 420 for unrecognized breakpoint locations.
fa8d40ab
JJ
421 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
422static enum auto_boolean pending_break_support;
920d2a44
AC
423static void
424show_pending_break_support (struct ui_file *file, int from_tty,
425 struct cmd_list_element *c,
426 const char *value)
427{
3e43a32a
MS
428 fprintf_filtered (file,
429 _("Debugger's behavior regarding "
430 "pending breakpoints is %s.\n"),
920d2a44
AC
431 value);
432}
fa8d40ab 433
765dc015 434/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 435 set with "break" but falling in read-only memory.
765dc015
VP
436 If 0, gdb will warn about such breakpoints, but won't automatically
437 use hardware breakpoints. */
438static int automatic_hardware_breakpoints;
439static void
440show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
441 struct cmd_list_element *c,
442 const char *value)
443{
3e43a32a
MS
444 fprintf_filtered (file,
445 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
446 value);
447}
448
a25a5a45
PA
449/* If on, GDB keeps breakpoints inserted even if the inferior is
450 stopped, and immediately inserts any new breakpoints as soon as
451 they're created. If off (default), GDB keeps breakpoints off of
452 the target as long as possible. That is, it delays inserting
453 breakpoints until the next resume, and removes them again when the
454 target fully stops. This is a bit safer in case GDB crashes while
455 processing user input. */
456static int always_inserted_mode = 0;
72d0e2c5 457
33e5cbd6 458static void
74960c60 459show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 460 struct cmd_list_element *c, const char *value)
74960c60 461{
a25a5a45
PA
462 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
463 value);
74960c60
VP
464}
465
b57bacec
PA
466/* See breakpoint.h. */
467
33e5cbd6 468int
a25a5a45 469breakpoints_should_be_inserted_now (void)
33e5cbd6 470{
a25a5a45
PA
471 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
472 {
473 /* If breakpoints are global, they should be inserted even if no
474 thread under gdb's control is running, or even if there are
475 no threads under GDB's control yet. */
476 return 1;
477 }
478 else if (target_has_execution)
479 {
372316f1
PA
480 struct thread_info *tp;
481
a25a5a45
PA
482 if (always_inserted_mode)
483 {
484 /* The user wants breakpoints inserted even if all threads
485 are stopped. */
486 return 1;
487 }
488
b57bacec
PA
489 if (threads_are_executing ())
490 return 1;
372316f1
PA
491
492 /* Don't remove breakpoints yet if, even though all threads are
493 stopped, we still have events to process. */
494 ALL_NON_EXITED_THREADS (tp)
495 if (tp->resumed
496 && tp->suspend.waitstatus_pending_p)
497 return 1;
a25a5a45
PA
498 }
499 return 0;
33e5cbd6 500}
765dc015 501
b775012e
LM
502static const char condition_evaluation_both[] = "host or target";
503
504/* Modes for breakpoint condition evaluation. */
505static const char condition_evaluation_auto[] = "auto";
506static const char condition_evaluation_host[] = "host";
507static const char condition_evaluation_target[] = "target";
508static const char *const condition_evaluation_enums[] = {
509 condition_evaluation_auto,
510 condition_evaluation_host,
511 condition_evaluation_target,
512 NULL
513};
514
515/* Global that holds the current mode for breakpoint condition evaluation. */
516static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
517
518/* Global that we use to display information to the user (gets its value from
519 condition_evaluation_mode_1. */
520static const char *condition_evaluation_mode = condition_evaluation_auto;
521
522/* Translate a condition evaluation mode MODE into either "host"
523 or "target". This is used mostly to translate from "auto" to the
524 real setting that is being used. It returns the translated
525 evaluation mode. */
526
527static const char *
528translate_condition_evaluation_mode (const char *mode)
529{
530 if (mode == condition_evaluation_auto)
531 {
532 if (target_supports_evaluation_of_breakpoint_conditions ())
533 return condition_evaluation_target;
534 else
535 return condition_evaluation_host;
536 }
537 else
538 return mode;
539}
540
541/* Discovers what condition_evaluation_auto translates to. */
542
543static const char *
544breakpoint_condition_evaluation_mode (void)
545{
546 return translate_condition_evaluation_mode (condition_evaluation_mode);
547}
548
549/* Return true if GDB should evaluate breakpoint conditions or false
550 otherwise. */
551
552static int
553gdb_evaluates_breakpoint_condition_p (void)
554{
555 const char *mode = breakpoint_condition_evaluation_mode ();
556
557 return (mode == condition_evaluation_host);
558}
559
a14ed312 560void _initialize_breakpoint (void);
c906108c 561
c906108c
SS
562/* Are we executing breakpoint commands? */
563static int executing_breakpoint_commands;
564
c02f5703
MS
565/* Are overlay event breakpoints enabled? */
566static int overlay_events_enabled;
567
e09342b5
TJB
568/* See description in breakpoint.h. */
569int target_exact_watchpoints = 0;
570
c906108c 571/* Walk the following statement or block through all breakpoints.
e5dd4106 572 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 573 current breakpoint. */
c906108c 574
5c44784c 575#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 576
5c44784c
JM
577#define ALL_BREAKPOINTS_SAFE(B,TMP) \
578 for (B = breakpoint_chain; \
579 B ? (TMP=B->next, 1): 0; \
580 B = TMP)
c906108c 581
4a64f543
MS
582/* Similar iterator for the low-level breakpoints. SAFE variant is
583 not provided so update_global_location_list must not be called
584 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 585
876fa593
JK
586#define ALL_BP_LOCATIONS(B,BP_TMP) \
587 for (BP_TMP = bp_location; \
588 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
589 BP_TMP++)
7cc221ef 590
b775012e
LM
591/* Iterates through locations with address ADDRESS for the currently selected
592 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
593 to where the loop should start from.
594 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
595 appropriate location to start with. */
596
597#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
598 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
599 BP_LOCP_TMP = BP_LOCP_START; \
600 BP_LOCP_START \
601 && (BP_LOCP_TMP < bp_location + bp_location_count \
602 && (*BP_LOCP_TMP)->address == ADDRESS); \
603 BP_LOCP_TMP++)
604
1042e4c0
SS
605/* Iterator for tracepoints only. */
606
607#define ALL_TRACEPOINTS(B) \
608 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 609 if (is_tracepoint (B))
1042e4c0 610
7cc221ef 611/* Chains of all breakpoints defined. */
c906108c
SS
612
613struct breakpoint *breakpoint_chain;
614
876fa593
JK
615/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
616
617static struct bp_location **bp_location;
618
619/* Number of elements of BP_LOCATION. */
620
621static unsigned bp_location_count;
622
4a64f543
MS
623/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
624 ADDRESS for the current elements of BP_LOCATION which get a valid
625 result from bp_location_has_shadow. You can use it for roughly
626 limiting the subrange of BP_LOCATION to scan for shadow bytes for
627 an address you need to read. */
876fa593
JK
628
629static CORE_ADDR bp_location_placed_address_before_address_max;
630
4a64f543
MS
631/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
632 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
633 BP_LOCATION which get a valid result from bp_location_has_shadow.
634 You can use it for roughly limiting the subrange of BP_LOCATION to
635 scan for shadow bytes for an address you need to read. */
876fa593
JK
636
637static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 638
4a64f543
MS
639/* The locations that no longer correspond to any breakpoint, unlinked
640 from bp_location array, but for which a hit may still be reported
641 by a target. */
20874c92
VP
642VEC(bp_location_p) *moribund_locations = NULL;
643
c906108c
SS
644/* Number of last breakpoint made. */
645
95a42b64
TT
646static int breakpoint_count;
647
86b17b60
PA
648/* The value of `breakpoint_count' before the last command that
649 created breakpoints. If the last (break-like) command created more
650 than one breakpoint, then the difference between BREAKPOINT_COUNT
651 and PREV_BREAKPOINT_COUNT is more than one. */
652static int prev_breakpoint_count;
c906108c 653
1042e4c0
SS
654/* Number of last tracepoint made. */
655
95a42b64 656static int tracepoint_count;
1042e4c0 657
6149aea9
PA
658static struct cmd_list_element *breakpoint_set_cmdlist;
659static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 660struct cmd_list_element *save_cmdlist;
6149aea9 661
badd37ce
SDJ
662/* See declaration at breakpoint.h. */
663
664struct breakpoint *
665breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
666 void *user_data)
667{
668 struct breakpoint *b = NULL;
669
670 ALL_BREAKPOINTS (b)
671 {
672 if (func (b, user_data) != 0)
673 break;
674 }
675
676 return b;
677}
678
468d015d
JJ
679/* Return whether a breakpoint is an active enabled breakpoint. */
680static int
681breakpoint_enabled (struct breakpoint *b)
682{
0d381245 683 return (b->enable_state == bp_enabled);
468d015d
JJ
684}
685
c906108c
SS
686/* Set breakpoint count to NUM. */
687
95a42b64 688static void
fba45db2 689set_breakpoint_count (int num)
c906108c 690{
86b17b60 691 prev_breakpoint_count = breakpoint_count;
c906108c 692 breakpoint_count = num;
4fa62494 693 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
694}
695
86b17b60
PA
696/* Used by `start_rbreak_breakpoints' below, to record the current
697 breakpoint count before "rbreak" creates any breakpoint. */
698static int rbreak_start_breakpoint_count;
699
95a42b64
TT
700/* Called at the start an "rbreak" command to record the first
701 breakpoint made. */
86b17b60 702
95a42b64
TT
703void
704start_rbreak_breakpoints (void)
705{
86b17b60 706 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
707}
708
709/* Called at the end of an "rbreak" command to record the last
710 breakpoint made. */
86b17b60 711
95a42b64
TT
712void
713end_rbreak_breakpoints (void)
714{
86b17b60 715 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
716}
717
4a64f543 718/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
719
720void
fba45db2 721clear_breakpoint_hit_counts (void)
c906108c
SS
722{
723 struct breakpoint *b;
724
725 ALL_BREAKPOINTS (b)
726 b->hit_count = 0;
727}
728
9add0f1b
TT
729/* Allocate a new counted_command_line with reference count of 1.
730 The new structure owns COMMANDS. */
731
732static struct counted_command_line *
733alloc_counted_command_line (struct command_line *commands)
734{
8d749320 735 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 736
9add0f1b
TT
737 result->refc = 1;
738 result->commands = commands;
8d749320 739
9add0f1b
TT
740 return result;
741}
742
743/* Increment reference count. This does nothing if CMD is NULL. */
744
745static void
746incref_counted_command_line (struct counted_command_line *cmd)
747{
748 if (cmd)
749 ++cmd->refc;
750}
751
752/* Decrement reference count. If the reference count reaches 0,
753 destroy the counted_command_line. Sets *CMDP to NULL. This does
754 nothing if *CMDP is NULL. */
755
756static void
757decref_counted_command_line (struct counted_command_line **cmdp)
758{
759 if (*cmdp)
760 {
761 if (--(*cmdp)->refc == 0)
762 {
763 free_command_lines (&(*cmdp)->commands);
764 xfree (*cmdp);
765 }
766 *cmdp = NULL;
767 }
768}
769
770/* A cleanup function that calls decref_counted_command_line. */
771
772static void
773do_cleanup_counted_command_line (void *arg)
774{
9a3c8263 775 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
776}
777
778/* Create a cleanup that calls decref_counted_command_line on the
779 argument. */
780
781static struct cleanup *
782make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
783{
784 return make_cleanup (do_cleanup_counted_command_line, cmdp);
785}
786
c906108c 787\f
48cb2d85
VP
788/* Return the breakpoint with the specified number, or NULL
789 if the number does not refer to an existing breakpoint. */
790
791struct breakpoint *
792get_breakpoint (int num)
793{
794 struct breakpoint *b;
795
796 ALL_BREAKPOINTS (b)
797 if (b->number == num)
798 return b;
799
800 return NULL;
801}
5c44784c 802
c906108c 803\f
adc36818 804
b775012e
LM
805/* Mark locations as "conditions have changed" in case the target supports
806 evaluating conditions on its side. */
807
808static void
809mark_breakpoint_modified (struct breakpoint *b)
810{
811 struct bp_location *loc;
812
813 /* This is only meaningful if the target is
814 evaluating conditions and if the user has
815 opted for condition evaluation on the target's
816 side. */
817 if (gdb_evaluates_breakpoint_condition_p ()
818 || !target_supports_evaluation_of_breakpoint_conditions ())
819 return;
820
821 if (!is_breakpoint (b))
822 return;
823
824 for (loc = b->loc; loc; loc = loc->next)
825 loc->condition_changed = condition_modified;
826}
827
828/* Mark location as "conditions have changed" in case the target supports
829 evaluating conditions on its side. */
830
831static void
832mark_breakpoint_location_modified (struct bp_location *loc)
833{
834 /* This is only meaningful if the target is
835 evaluating conditions and if the user has
836 opted for condition evaluation on the target's
837 side. */
838 if (gdb_evaluates_breakpoint_condition_p ()
839 || !target_supports_evaluation_of_breakpoint_conditions ())
840
841 return;
842
843 if (!is_breakpoint (loc->owner))
844 return;
845
846 loc->condition_changed = condition_modified;
847}
848
849/* Sets the condition-evaluation mode using the static global
850 condition_evaluation_mode. */
851
852static void
853set_condition_evaluation_mode (char *args, int from_tty,
854 struct cmd_list_element *c)
855{
b775012e
LM
856 const char *old_mode, *new_mode;
857
858 if ((condition_evaluation_mode_1 == condition_evaluation_target)
859 && !target_supports_evaluation_of_breakpoint_conditions ())
860 {
861 condition_evaluation_mode_1 = condition_evaluation_mode;
862 warning (_("Target does not support breakpoint condition evaluation.\n"
863 "Using host evaluation mode instead."));
864 return;
865 }
866
867 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
868 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
869
abf1152a
JK
870 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
871 settings was "auto". */
872 condition_evaluation_mode = condition_evaluation_mode_1;
873
b775012e
LM
874 /* Only update the mode if the user picked a different one. */
875 if (new_mode != old_mode)
876 {
877 struct bp_location *loc, **loc_tmp;
878 /* If the user switched to a different evaluation mode, we
879 need to synch the changes with the target as follows:
880
881 "host" -> "target": Send all (valid) conditions to the target.
882 "target" -> "host": Remove all the conditions from the target.
883 */
884
b775012e
LM
885 if (new_mode == condition_evaluation_target)
886 {
887 /* Mark everything modified and synch conditions with the
888 target. */
889 ALL_BP_LOCATIONS (loc, loc_tmp)
890 mark_breakpoint_location_modified (loc);
891 }
892 else
893 {
894 /* Manually mark non-duplicate locations to synch conditions
895 with the target. We do this to remove all the conditions the
896 target knows about. */
897 ALL_BP_LOCATIONS (loc, loc_tmp)
898 if (is_breakpoint (loc->owner) && loc->inserted)
899 loc->needs_update = 1;
900 }
901
902 /* Do the update. */
44702360 903 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
904 }
905
906 return;
907}
908
909/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
910 what "auto" is translating to. */
911
912static void
913show_condition_evaluation_mode (struct ui_file *file, int from_tty,
914 struct cmd_list_element *c, const char *value)
915{
916 if (condition_evaluation_mode == condition_evaluation_auto)
917 fprintf_filtered (file,
918 _("Breakpoint condition evaluation "
919 "mode is %s (currently %s).\n"),
920 value,
921 breakpoint_condition_evaluation_mode ());
922 else
923 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
924 value);
925}
926
927/* A comparison function for bp_location AP and BP that is used by
928 bsearch. This comparison function only cares about addresses, unlike
929 the more general bp_location_compare function. */
930
931static int
932bp_location_compare_addrs (const void *ap, const void *bp)
933{
9a3c8263
SM
934 const struct bp_location *a = *(const struct bp_location **) ap;
935 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
936
937 if (a->address == b->address)
938 return 0;
939 else
940 return ((a->address > b->address) - (a->address < b->address));
941}
942
943/* Helper function to skip all bp_locations with addresses
944 less than ADDRESS. It returns the first bp_location that
945 is greater than or equal to ADDRESS. If none is found, just
946 return NULL. */
947
948static struct bp_location **
949get_first_locp_gte_addr (CORE_ADDR address)
950{
951 struct bp_location dummy_loc;
952 struct bp_location *dummy_locp = &dummy_loc;
953 struct bp_location **locp_found = NULL;
954
955 /* Initialize the dummy location's address field. */
956 memset (&dummy_loc, 0, sizeof (struct bp_location));
957 dummy_loc.address = address;
958
959 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
960 locp_found = ((struct bp_location **)
961 bsearch (&dummy_locp, bp_location, bp_location_count,
962 sizeof (struct bp_location **),
963 bp_location_compare_addrs));
b775012e
LM
964
965 /* Nothing was found, nothing left to do. */
966 if (locp_found == NULL)
967 return NULL;
968
969 /* We may have found a location that is at ADDRESS but is not the first in the
970 location's list. Go backwards (if possible) and locate the first one. */
971 while ((locp_found - 1) >= bp_location
972 && (*(locp_found - 1))->address == address)
973 locp_found--;
974
975 return locp_found;
976}
977
adc36818 978void
7a26bd4d 979set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
980 int from_tty)
981{
3a5c3e22
PA
982 xfree (b->cond_string);
983 b->cond_string = NULL;
adc36818 984
3a5c3e22 985 if (is_watchpoint (b))
adc36818 986 {
3a5c3e22
PA
987 struct watchpoint *w = (struct watchpoint *) b;
988
989 xfree (w->cond_exp);
990 w->cond_exp = NULL;
991 }
992 else
993 {
994 struct bp_location *loc;
995
996 for (loc = b->loc; loc; loc = loc->next)
997 {
998 xfree (loc->cond);
999 loc->cond = NULL;
b775012e
LM
1000
1001 /* No need to free the condition agent expression
1002 bytecode (if we have one). We will handle this
1003 when we go through update_global_location_list. */
3a5c3e22 1004 }
adc36818 1005 }
adc36818
PM
1006
1007 if (*exp == 0)
1008 {
1009 if (from_tty)
1010 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1011 }
1012 else
1013 {
bbc13ae3 1014 const char *arg = exp;
cc59ec59 1015
adc36818
PM
1016 /* I don't know if it matters whether this is the string the user
1017 typed in or the decompiled expression. */
1018 b->cond_string = xstrdup (arg);
1019 b->condition_not_parsed = 0;
1020
1021 if (is_watchpoint (b))
1022 {
3a5c3e22
PA
1023 struct watchpoint *w = (struct watchpoint *) b;
1024
adc36818
PM
1025 innermost_block = NULL;
1026 arg = exp;
1bb9788d 1027 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1028 if (*arg)
1029 error (_("Junk at end of expression"));
3a5c3e22 1030 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1031 }
1032 else
1033 {
3a5c3e22
PA
1034 struct bp_location *loc;
1035
adc36818
PM
1036 for (loc = b->loc; loc; loc = loc->next)
1037 {
1038 arg = exp;
1039 loc->cond =
1bb9788d
TT
1040 parse_exp_1 (&arg, loc->address,
1041 block_for_pc (loc->address), 0);
adc36818
PM
1042 if (*arg)
1043 error (_("Junk at end of expression"));
1044 }
1045 }
1046 }
b775012e
LM
1047 mark_breakpoint_modified (b);
1048
8d3788bd 1049 observer_notify_breakpoint_modified (b);
adc36818
PM
1050}
1051
d55637df
TT
1052/* Completion for the "condition" command. */
1053
1054static VEC (char_ptr) *
6f937416
PA
1055condition_completer (struct cmd_list_element *cmd,
1056 const char *text, const char *word)
d55637df 1057{
6f937416 1058 const char *space;
d55637df 1059
6f937416
PA
1060 text = skip_spaces_const (text);
1061 space = skip_to_space_const (text);
d55637df
TT
1062 if (*space == '\0')
1063 {
1064 int len;
1065 struct breakpoint *b;
1066 VEC (char_ptr) *result = NULL;
1067
1068 if (text[0] == '$')
1069 {
1070 /* We don't support completion of history indices. */
1071 if (isdigit (text[1]))
1072 return NULL;
1073 return complete_internalvar (&text[1]);
1074 }
1075
1076 /* We're completing the breakpoint number. */
1077 len = strlen (text);
1078
1079 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1080 {
1081 char number[50];
1082
1083 xsnprintf (number, sizeof (number), "%d", b->number);
1084
1085 if (strncmp (number, text, len) == 0)
1086 VEC_safe_push (char_ptr, result, xstrdup (number));
1087 }
d55637df
TT
1088
1089 return result;
1090 }
1091
1092 /* We're completing the expression part. */
6f937416 1093 text = skip_spaces_const (space);
d55637df
TT
1094 return expression_completer (cmd, text, word);
1095}
1096
c906108c
SS
1097/* condition N EXP -- set break condition of breakpoint N to EXP. */
1098
1099static void
fba45db2 1100condition_command (char *arg, int from_tty)
c906108c 1101{
52f0bd74 1102 struct breakpoint *b;
c906108c 1103 char *p;
52f0bd74 1104 int bnum;
c906108c
SS
1105
1106 if (arg == 0)
e2e0b3e5 1107 error_no_arg (_("breakpoint number"));
c906108c
SS
1108
1109 p = arg;
1110 bnum = get_number (&p);
5c44784c 1111 if (bnum == 0)
8a3fe4f8 1112 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1113
1114 ALL_BREAKPOINTS (b)
1115 if (b->number == bnum)
2f069f6f 1116 {
6dddc817
DE
1117 /* Check if this breakpoint has a "stop" method implemented in an
1118 extension language. This method and conditions entered into GDB
1119 from the CLI are mutually exclusive. */
1120 const struct extension_language_defn *extlang
1121 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1122
1123 if (extlang != NULL)
1124 {
1125 error (_("Only one stop condition allowed. There is currently"
1126 " a %s stop condition defined for this breakpoint."),
1127 ext_lang_capitalized_name (extlang));
1128 }
2566ad2d 1129 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1130
1131 if (is_breakpoint (b))
44702360 1132 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1133
2f069f6f
JB
1134 return;
1135 }
c906108c 1136
8a3fe4f8 1137 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1138}
1139
a7bdde9e
VP
1140/* Check that COMMAND do not contain commands that are suitable
1141 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1142 Throw if any such commands is found. */
1143
a7bdde9e
VP
1144static void
1145check_no_tracepoint_commands (struct command_line *commands)
1146{
1147 struct command_line *c;
cc59ec59 1148
a7bdde9e
VP
1149 for (c = commands; c; c = c->next)
1150 {
1151 int i;
1152
1153 if (c->control_type == while_stepping_control)
3e43a32a
MS
1154 error (_("The 'while-stepping' command can "
1155 "only be used for tracepoints"));
a7bdde9e
VP
1156
1157 for (i = 0; i < c->body_count; ++i)
1158 check_no_tracepoint_commands ((c->body_list)[i]);
1159
1160 /* Not that command parsing removes leading whitespace and comment
4a64f543 1161 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1162 command directly. */
1163 if (strstr (c->line, "collect ") == c->line)
1164 error (_("The 'collect' command can only be used for tracepoints"));
1165
51661e93
VP
1166 if (strstr (c->line, "teval ") == c->line)
1167 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1168 }
1169}
1170
d77f58be
SS
1171/* Encapsulate tests for different types of tracepoints. */
1172
d9b3f62e
PA
1173static int
1174is_tracepoint_type (enum bptype type)
1175{
1176 return (type == bp_tracepoint
1177 || type == bp_fast_tracepoint
1178 || type == bp_static_tracepoint);
1179}
1180
a7bdde9e 1181int
d77f58be 1182is_tracepoint (const struct breakpoint *b)
a7bdde9e 1183{
d9b3f62e 1184 return is_tracepoint_type (b->type);
a7bdde9e 1185}
d9b3f62e 1186
e5dd4106 1187/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1188 breakpoint. This function will throw an exception if a problem is
1189 found. */
48cb2d85 1190
95a42b64
TT
1191static void
1192validate_commands_for_breakpoint (struct breakpoint *b,
1193 struct command_line *commands)
48cb2d85 1194{
d77f58be 1195 if (is_tracepoint (b))
a7bdde9e 1196 {
c9a6ce02 1197 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1198 struct command_line *c;
1199 struct command_line *while_stepping = 0;
c9a6ce02
PA
1200
1201 /* Reset the while-stepping step count. The previous commands
1202 might have included a while-stepping action, while the new
1203 ones might not. */
1204 t->step_count = 0;
1205
1206 /* We need to verify that each top-level element of commands is
1207 valid for tracepoints, that there's at most one
1208 while-stepping element, and that the while-stepping's body
1209 has valid tracing commands excluding nested while-stepping.
1210 We also need to validate the tracepoint action line in the
1211 context of the tracepoint --- validate_actionline actually
1212 has side effects, like setting the tracepoint's
1213 while-stepping STEP_COUNT, in addition to checking if the
1214 collect/teval actions parse and make sense in the
1215 tracepoint's context. */
a7bdde9e
VP
1216 for (c = commands; c; c = c->next)
1217 {
a7bdde9e
VP
1218 if (c->control_type == while_stepping_control)
1219 {
1220 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1221 error (_("The 'while-stepping' command "
1222 "cannot be used for fast tracepoint"));
0fb4aa4b 1223 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for static tracepoint"));
a7bdde9e
VP
1226
1227 if (while_stepping)
3e43a32a
MS
1228 error (_("The 'while-stepping' command "
1229 "can be used only once"));
a7bdde9e
VP
1230 else
1231 while_stepping = c;
1232 }
c9a6ce02
PA
1233
1234 validate_actionline (c->line, b);
a7bdde9e
VP
1235 }
1236 if (while_stepping)
1237 {
1238 struct command_line *c2;
1239
1240 gdb_assert (while_stepping->body_count == 1);
1241 c2 = while_stepping->body_list[0];
1242 for (; c2; c2 = c2->next)
1243 {
a7bdde9e
VP
1244 if (c2->control_type == while_stepping_control)
1245 error (_("The 'while-stepping' command cannot be nested"));
1246 }
1247 }
1248 }
1249 else
1250 {
1251 check_no_tracepoint_commands (commands);
1252 }
95a42b64
TT
1253}
1254
0fb4aa4b
PA
1255/* Return a vector of all the static tracepoints set at ADDR. The
1256 caller is responsible for releasing the vector. */
1257
1258VEC(breakpoint_p) *
1259static_tracepoints_here (CORE_ADDR addr)
1260{
1261 struct breakpoint *b;
1262 VEC(breakpoint_p) *found = 0;
1263 struct bp_location *loc;
1264
1265 ALL_BREAKPOINTS (b)
1266 if (b->type == bp_static_tracepoint)
1267 {
1268 for (loc = b->loc; loc; loc = loc->next)
1269 if (loc->address == addr)
1270 VEC_safe_push(breakpoint_p, found, b);
1271 }
1272
1273 return found;
1274}
1275
95a42b64 1276/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1277 validate that only allowed commands are included. */
95a42b64
TT
1278
1279void
4a64f543
MS
1280breakpoint_set_commands (struct breakpoint *b,
1281 struct command_line *commands)
95a42b64
TT
1282{
1283 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1284
9add0f1b
TT
1285 decref_counted_command_line (&b->commands);
1286 b->commands = alloc_counted_command_line (commands);
8d3788bd 1287 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1288}
1289
45a43567
TT
1290/* Set the internal `silent' flag on the breakpoint. Note that this
1291 is not the same as the "silent" that may appear in the breakpoint's
1292 commands. */
1293
1294void
1295breakpoint_set_silent (struct breakpoint *b, int silent)
1296{
1297 int old_silent = b->silent;
1298
1299 b->silent = silent;
1300 if (old_silent != silent)
8d3788bd 1301 observer_notify_breakpoint_modified (b);
45a43567
TT
1302}
1303
1304/* Set the thread for this breakpoint. If THREAD is -1, make the
1305 breakpoint work for any thread. */
1306
1307void
1308breakpoint_set_thread (struct breakpoint *b, int thread)
1309{
1310 int old_thread = b->thread;
1311
1312 b->thread = thread;
1313 if (old_thread != thread)
8d3788bd 1314 observer_notify_breakpoint_modified (b);
45a43567
TT
1315}
1316
1317/* Set the task for this breakpoint. If TASK is 0, make the
1318 breakpoint work for any task. */
1319
1320void
1321breakpoint_set_task (struct breakpoint *b, int task)
1322{
1323 int old_task = b->task;
1324
1325 b->task = task;
1326 if (old_task != task)
8d3788bd 1327 observer_notify_breakpoint_modified (b);
45a43567
TT
1328}
1329
95a42b64
TT
1330void
1331check_tracepoint_command (char *line, void *closure)
a7bdde9e 1332{
9a3c8263 1333 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1334
6f937416 1335 validate_actionline (line, b);
a7bdde9e
VP
1336}
1337
95a42b64
TT
1338/* A structure used to pass information through
1339 map_breakpoint_numbers. */
1340
1341struct commands_info
1342{
1343 /* True if the command was typed at a tty. */
1344 int from_tty;
86b17b60
PA
1345
1346 /* The breakpoint range spec. */
1347 char *arg;
1348
95a42b64
TT
1349 /* Non-NULL if the body of the commands are being read from this
1350 already-parsed command. */
1351 struct command_line *control;
86b17b60 1352
95a42b64
TT
1353 /* The command lines read from the user, or NULL if they have not
1354 yet been read. */
1355 struct counted_command_line *cmd;
1356};
1357
1358/* A callback for map_breakpoint_numbers that sets the commands for
1359 commands_command. */
1360
c906108c 1361static void
95a42b64 1362do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1363{
9a3c8263 1364 struct commands_info *info = (struct commands_info *) data;
c906108c 1365
95a42b64
TT
1366 if (info->cmd == NULL)
1367 {
1368 struct command_line *l;
5c44784c 1369
95a42b64
TT
1370 if (info->control != NULL)
1371 l = copy_command_lines (info->control->body_list[0]);
1372 else
86b17b60
PA
1373 {
1374 struct cleanup *old_chain;
1375 char *str;
c5aa993b 1376
3e43a32a
MS
1377 str = xstrprintf (_("Type commands for breakpoint(s) "
1378 "%s, one per line."),
86b17b60
PA
1379 info->arg);
1380
1381 old_chain = make_cleanup (xfree, str);
1382
1383 l = read_command_lines (str,
1384 info->from_tty, 1,
d77f58be 1385 (is_tracepoint (b)
86b17b60
PA
1386 ? check_tracepoint_command : 0),
1387 b);
1388
1389 do_cleanups (old_chain);
1390 }
a7bdde9e 1391
95a42b64
TT
1392 info->cmd = alloc_counted_command_line (l);
1393 }
1394
1395 /* If a breakpoint was on the list more than once, we don't need to
1396 do anything. */
1397 if (b->commands != info->cmd)
1398 {
1399 validate_commands_for_breakpoint (b, info->cmd->commands);
1400 incref_counted_command_line (info->cmd);
1401 decref_counted_command_line (&b->commands);
1402 b->commands = info->cmd;
8d3788bd 1403 observer_notify_breakpoint_modified (b);
c5aa993b 1404 }
95a42b64
TT
1405}
1406
1407static void
4a64f543
MS
1408commands_command_1 (char *arg, int from_tty,
1409 struct command_line *control)
95a42b64
TT
1410{
1411 struct cleanup *cleanups;
1412 struct commands_info info;
1413
1414 info.from_tty = from_tty;
1415 info.control = control;
1416 info.cmd = NULL;
1417 /* If we read command lines from the user, then `info' will hold an
1418 extra reference to the commands that we must clean up. */
1419 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1420
1421 if (arg == NULL || !*arg)
1422 {
86b17b60 1423 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1424 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1425 breakpoint_count);
95a42b64
TT
1426 else if (breakpoint_count > 0)
1427 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1428 else
1429 {
1430 /* So that we don't try to free the incoming non-NULL
1431 argument in the cleanup below. Mapping breakpoint
1432 numbers will fail in this case. */
1433 arg = NULL;
1434 }
95a42b64 1435 }
9766ced4
SS
1436 else
1437 /* The command loop has some static state, so we need to preserve
1438 our argument. */
1439 arg = xstrdup (arg);
86b17b60
PA
1440
1441 if (arg != NULL)
1442 make_cleanup (xfree, arg);
1443
1444 info.arg = arg;
95a42b64
TT
1445
1446 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1447
1448 if (info.cmd == NULL)
1449 error (_("No breakpoints specified."));
1450
1451 do_cleanups (cleanups);
1452}
1453
1454static void
1455commands_command (char *arg, int from_tty)
1456{
1457 commands_command_1 (arg, from_tty, NULL);
c906108c 1458}
40c03ae8
EZ
1459
1460/* Like commands_command, but instead of reading the commands from
1461 input stream, takes them from an already parsed command structure.
1462
1463 This is used by cli-script.c to DTRT with breakpoint commands
1464 that are part of if and while bodies. */
1465enum command_control_type
1466commands_from_control_command (char *arg, struct command_line *cmd)
1467{
95a42b64
TT
1468 commands_command_1 (arg, 0, cmd);
1469 return simple_control;
40c03ae8 1470}
876fa593
JK
1471
1472/* Return non-zero if BL->TARGET_INFO contains valid information. */
1473
1474static int
1475bp_location_has_shadow (struct bp_location *bl)
1476{
1477 if (bl->loc_type != bp_loc_software_breakpoint)
1478 return 0;
1479 if (!bl->inserted)
1480 return 0;
1481 if (bl->target_info.shadow_len == 0)
e5dd4106 1482 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1483 return 0;
1484 return 1;
1485}
1486
9d497a19
PA
1487/* Update BUF, which is LEN bytes read from the target address
1488 MEMADDR, by replacing a memory breakpoint with its shadowed
1489 contents.
1490
1491 If READBUF is not NULL, this buffer must not overlap with the of
1492 the breakpoint location's shadow_contents buffer. Otherwise, a
1493 failed assertion internal error will be raised. */
1494
1495static void
1496one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1497 const gdb_byte *writebuf_org,
1498 ULONGEST memaddr, LONGEST len,
1499 struct bp_target_info *target_info,
1500 struct gdbarch *gdbarch)
1501{
1502 /* Now do full processing of the found relevant range of elements. */
1503 CORE_ADDR bp_addr = 0;
1504 int bp_size = 0;
1505 int bptoffset = 0;
1506
1507 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1508 current_program_space->aspace, 0))
1509 {
1510 /* The breakpoint is inserted in a different address space. */
1511 return;
1512 }
1513
1514 /* Addresses and length of the part of the breakpoint that
1515 we need to copy. */
1516 bp_addr = target_info->placed_address;
1517 bp_size = target_info->shadow_len;
1518
1519 if (bp_addr + bp_size <= memaddr)
1520 {
1521 /* The breakpoint is entirely before the chunk of memory we are
1522 reading. */
1523 return;
1524 }
1525
1526 if (bp_addr >= memaddr + len)
1527 {
1528 /* The breakpoint is entirely after the chunk of memory we are
1529 reading. */
1530 return;
1531 }
1532
1533 /* Offset within shadow_contents. */
1534 if (bp_addr < memaddr)
1535 {
1536 /* Only copy the second part of the breakpoint. */
1537 bp_size -= memaddr - bp_addr;
1538 bptoffset = memaddr - bp_addr;
1539 bp_addr = memaddr;
1540 }
1541
1542 if (bp_addr + bp_size > memaddr + len)
1543 {
1544 /* Only copy the first part of the breakpoint. */
1545 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1546 }
1547
1548 if (readbuf != NULL)
1549 {
1550 /* Verify that the readbuf buffer does not overlap with the
1551 shadow_contents buffer. */
1552 gdb_assert (target_info->shadow_contents >= readbuf + len
1553 || readbuf >= (target_info->shadow_contents
1554 + target_info->shadow_len));
1555
1556 /* Update the read buffer with this inserted breakpoint's
1557 shadow. */
1558 memcpy (readbuf + bp_addr - memaddr,
1559 target_info->shadow_contents + bptoffset, bp_size);
1560 }
1561 else
1562 {
1563 const unsigned char *bp;
0d5ed153
MR
1564 CORE_ADDR addr = target_info->reqstd_address;
1565 int placed_size;
9d497a19
PA
1566
1567 /* Update the shadow with what we want to write to memory. */
1568 memcpy (target_info->shadow_contents + bptoffset,
1569 writebuf_org + bp_addr - memaddr, bp_size);
1570
1571 /* Determine appropriate breakpoint contents and size for this
1572 address. */
0d5ed153 1573 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1574
1575 /* Update the final write buffer with this inserted
1576 breakpoint's INSN. */
1577 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1578 }
1579}
1580
8defab1a 1581/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1582 by replacing any memory breakpoints with their shadowed contents.
1583
35c63cd8
JB
1584 If READBUF is not NULL, this buffer must not overlap with any of
1585 the breakpoint location's shadow_contents buffers. Otherwise,
1586 a failed assertion internal error will be raised.
1587
876fa593 1588 The range of shadowed area by each bp_location is:
35df4500
TJB
1589 bl->address - bp_location_placed_address_before_address_max
1590 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1591 The range we were requested to resolve shadows for is:
1592 memaddr ... memaddr + len
1593 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1594 memaddr + len <= (bl->address
1595 - bp_location_placed_address_before_address_max)
876fa593 1596 and:
35df4500 1597 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1598
8defab1a 1599void
f0ba3972
PA
1600breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1601 const gdb_byte *writebuf_org,
1602 ULONGEST memaddr, LONGEST len)
c906108c 1603{
4a64f543
MS
1604 /* Left boundary, right boundary and median element of our binary
1605 search. */
876fa593 1606 unsigned bc_l, bc_r, bc;
9d497a19 1607 size_t i;
876fa593 1608
4a64f543
MS
1609 /* Find BC_L which is a leftmost element which may affect BUF
1610 content. It is safe to report lower value but a failure to
1611 report higher one. */
876fa593
JK
1612
1613 bc_l = 0;
1614 bc_r = bp_location_count;
1615 while (bc_l + 1 < bc_r)
1616 {
35df4500 1617 struct bp_location *bl;
876fa593
JK
1618
1619 bc = (bc_l + bc_r) / 2;
35df4500 1620 bl = bp_location[bc];
876fa593 1621
4a64f543
MS
1622 /* Check first BL->ADDRESS will not overflow due to the added
1623 constant. Then advance the left boundary only if we are sure
1624 the BC element can in no way affect the BUF content (MEMADDR
1625 to MEMADDR + LEN range).
876fa593 1626
4a64f543
MS
1627 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1628 offset so that we cannot miss a breakpoint with its shadow
1629 range tail still reaching MEMADDR. */
c5aa993b 1630
35df4500
TJB
1631 if ((bl->address + bp_location_shadow_len_after_address_max
1632 >= bl->address)
1633 && (bl->address + bp_location_shadow_len_after_address_max
1634 <= memaddr))
876fa593
JK
1635 bc_l = bc;
1636 else
1637 bc_r = bc;
1638 }
1639
128070bb
PA
1640 /* Due to the binary search above, we need to make sure we pick the
1641 first location that's at BC_L's address. E.g., if there are
1642 multiple locations at the same address, BC_L may end up pointing
1643 at a duplicate location, and miss the "master"/"inserted"
1644 location. Say, given locations L1, L2 and L3 at addresses A and
1645 B:
1646
1647 L1@A, L2@A, L3@B, ...
1648
1649 BC_L could end up pointing at location L2, while the "master"
1650 location could be L1. Since the `loc->inserted' flag is only set
1651 on "master" locations, we'd forget to restore the shadow of L1
1652 and L2. */
1653 while (bc_l > 0
1654 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1655 bc_l--;
1656
876fa593
JK
1657 /* Now do full processing of the found relevant range of elements. */
1658
1659 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1660 {
35df4500 1661 struct bp_location *bl = bp_location[bc];
876fa593
JK
1662 CORE_ADDR bp_addr = 0;
1663 int bp_size = 0;
1664 int bptoffset = 0;
1665
35df4500
TJB
1666 /* bp_location array has BL->OWNER always non-NULL. */
1667 if (bl->owner->type == bp_none)
8a3fe4f8 1668 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1669 bl->owner->number);
ffce0d52 1670
e5dd4106 1671 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1672 content. */
1673
35df4500
TJB
1674 if (bl->address >= bp_location_placed_address_before_address_max
1675 && memaddr + len <= (bl->address
1676 - bp_location_placed_address_before_address_max))
876fa593
JK
1677 break;
1678
35df4500 1679 if (!bp_location_has_shadow (bl))
c5aa993b 1680 continue;
6c95b8df 1681
9d497a19
PA
1682 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1683 memaddr, len, &bl->target_info, bl->gdbarch);
1684 }
c906108c 1685}
9d497a19 1686
c906108c 1687\f
c5aa993b 1688
b775012e
LM
1689/* Return true if BPT is either a software breakpoint or a hardware
1690 breakpoint. */
1691
1692int
1693is_breakpoint (const struct breakpoint *bpt)
1694{
1695 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1696 || bpt->type == bp_hardware_breakpoint
1697 || bpt->type == bp_dprintf);
b775012e
LM
1698}
1699
60e1c644
PA
1700/* Return true if BPT is of any hardware watchpoint kind. */
1701
a5606eee 1702static int
d77f58be 1703is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1704{
1705 return (bpt->type == bp_hardware_watchpoint
1706 || bpt->type == bp_read_watchpoint
1707 || bpt->type == bp_access_watchpoint);
1708}
7270d8f2 1709
60e1c644
PA
1710/* Return true if BPT is of any watchpoint kind, hardware or
1711 software. */
1712
3a5c3e22 1713int
d77f58be 1714is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1715{
1716 return (is_hardware_watchpoint (bpt)
1717 || bpt->type == bp_watchpoint);
1718}
1719
3a5c3e22
PA
1720/* Returns true if the current thread and its running state are safe
1721 to evaluate or update watchpoint B. Watchpoints on local
1722 expressions need to be evaluated in the context of the thread that
1723 was current when the watchpoint was created, and, that thread needs
1724 to be stopped to be able to select the correct frame context.
1725 Watchpoints on global expressions can be evaluated on any thread,
1726 and in any state. It is presently left to the target allowing
1727 memory accesses when threads are running. */
f6bc2008
PA
1728
1729static int
3a5c3e22 1730watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1731{
d0d8b0c6
JK
1732 return (b->base.pspace == current_program_space
1733 && (ptid_equal (b->watchpoint_thread, null_ptid)
1734 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1735 && !is_executing (inferior_ptid))));
f6bc2008
PA
1736}
1737
d0fb5eae
JK
1738/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1739 associated bp_watchpoint_scope breakpoint. */
1740
1741static void
3a5c3e22 1742watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1743{
3a5c3e22 1744 struct breakpoint *b = &w->base;
d0fb5eae
JK
1745
1746 if (b->related_breakpoint != b)
1747 {
1748 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1749 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1750 b->related_breakpoint->disposition = disp_del_at_next_stop;
1751 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1752 b->related_breakpoint = b;
1753 }
1754 b->disposition = disp_del_at_next_stop;
1755}
1756
bb9d5f81
PP
1757/* Extract a bitfield value from value VAL using the bit parameters contained in
1758 watchpoint W. */
1759
1760static struct value *
1761extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1762{
1763 struct value *bit_val;
1764
1765 if (val == NULL)
1766 return NULL;
1767
1768 bit_val = allocate_value (value_type (val));
1769
1770 unpack_value_bitfield (bit_val,
1771 w->val_bitpos,
1772 w->val_bitsize,
1773 value_contents_for_printing (val),
1774 value_offset (val),
1775 val);
1776
1777 return bit_val;
1778}
1779
c6d81124
PA
1780/* Allocate a dummy location and add it to B, which must be a software
1781 watchpoint. This is required because even if a software watchpoint
1782 is not watching any memory, bpstat_stop_status requires a location
1783 to be able to report stops. */
1784
1785static void
1786software_watchpoint_add_no_memory_location (struct breakpoint *b,
1787 struct program_space *pspace)
1788{
1789 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1790
1791 b->loc = allocate_bp_location (b);
1792 b->loc->pspace = pspace;
1793 b->loc->address = -1;
1794 b->loc->length = -1;
1795}
1796
1797/* Returns true if B is a software watchpoint that is not watching any
1798 memory (e.g., "watch $pc"). */
1799
1800static int
1801is_no_memory_software_watchpoint (struct breakpoint *b)
1802{
1803 return (b->type == bp_watchpoint
1804 && b->loc != NULL
1805 && b->loc->next == NULL
1806 && b->loc->address == -1
1807 && b->loc->length == -1);
1808}
1809
567e1b4e
JB
1810/* Assuming that B is a watchpoint:
1811 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1812 - Evaluate expression and store the result in B->val
567e1b4e
JB
1813 - Evaluate the condition if there is one, and store the result
1814 in b->loc->cond.
a5606eee
VP
1815 - Update the list of values that must be watched in B->loc.
1816
4a64f543
MS
1817 If the watchpoint disposition is disp_del_at_next_stop, then do
1818 nothing. If this is local watchpoint that is out of scope, delete
1819 it.
1820
1821 Even with `set breakpoint always-inserted on' the watchpoints are
1822 removed + inserted on each stop here. Normal breakpoints must
1823 never be removed because they might be missed by a running thread
1824 when debugging in non-stop mode. On the other hand, hardware
1825 watchpoints (is_hardware_watchpoint; processed here) are specific
1826 to each LWP since they are stored in each LWP's hardware debug
1827 registers. Therefore, such LWP must be stopped first in order to
1828 be able to modify its hardware watchpoints.
1829
1830 Hardware watchpoints must be reset exactly once after being
1831 presented to the user. It cannot be done sooner, because it would
1832 reset the data used to present the watchpoint hit to the user. And
1833 it must not be done later because it could display the same single
1834 watchpoint hit during multiple GDB stops. Note that the latter is
1835 relevant only to the hardware watchpoint types bp_read_watchpoint
1836 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1837 not user-visible - its hit is suppressed if the memory content has
1838 not changed.
1839
1840 The following constraints influence the location where we can reset
1841 hardware watchpoints:
1842
1843 * target_stopped_by_watchpoint and target_stopped_data_address are
1844 called several times when GDB stops.
1845
1846 [linux]
1847 * Multiple hardware watchpoints can be hit at the same time,
1848 causing GDB to stop. GDB only presents one hardware watchpoint
1849 hit at a time as the reason for stopping, and all the other hits
1850 are presented later, one after the other, each time the user
1851 requests the execution to be resumed. Execution is not resumed
1852 for the threads still having pending hit event stored in
1853 LWP_INFO->STATUS. While the watchpoint is already removed from
1854 the inferior on the first stop the thread hit event is kept being
1855 reported from its cached value by linux_nat_stopped_data_address
1856 until the real thread resume happens after the watchpoint gets
1857 presented and thus its LWP_INFO->STATUS gets reset.
1858
1859 Therefore the hardware watchpoint hit can get safely reset on the
1860 watchpoint removal from inferior. */
a79d3c27 1861
b40ce68a 1862static void
3a5c3e22 1863update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1864{
a5606eee 1865 int within_current_scope;
a5606eee 1866 struct frame_id saved_frame_id;
66076460 1867 int frame_saved;
a5606eee 1868
f6bc2008
PA
1869 /* If this is a local watchpoint, we only want to check if the
1870 watchpoint frame is in scope if the current thread is the thread
1871 that was used to create the watchpoint. */
1872 if (!watchpoint_in_thread_scope (b))
1873 return;
1874
3a5c3e22 1875 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1876 return;
1877
66076460 1878 frame_saved = 0;
a5606eee
VP
1879
1880 /* Determine if the watchpoint is within scope. */
1881 if (b->exp_valid_block == NULL)
1882 within_current_scope = 1;
1883 else
1884 {
b5db5dfc
UW
1885 struct frame_info *fi = get_current_frame ();
1886 struct gdbarch *frame_arch = get_frame_arch (fi);
1887 CORE_ADDR frame_pc = get_frame_pc (fi);
1888
c9cf6e20
MG
1889 /* If we're at a point where the stack has been destroyed
1890 (e.g. in a function epilogue), unwinding may not work
1891 properly. Do not attempt to recreate locations at this
b5db5dfc 1892 point. See similar comments in watchpoint_check. */
c9cf6e20 1893 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1894 return;
66076460
DJ
1895
1896 /* Save the current frame's ID so we can restore it after
1897 evaluating the watchpoint expression on its own frame. */
1898 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1899 took a frame parameter, so that we didn't have to change the
1900 selected frame. */
1901 frame_saved = 1;
1902 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1903
a5606eee
VP
1904 fi = frame_find_by_id (b->watchpoint_frame);
1905 within_current_scope = (fi != NULL);
1906 if (within_current_scope)
1907 select_frame (fi);
1908 }
1909
b5db5dfc
UW
1910 /* We don't free locations. They are stored in the bp_location array
1911 and update_global_location_list will eventually delete them and
1912 remove breakpoints if needed. */
3a5c3e22 1913 b->base.loc = NULL;
b5db5dfc 1914
a5606eee
VP
1915 if (within_current_scope && reparse)
1916 {
bbc13ae3 1917 const char *s;
d63d0675 1918
a5606eee
VP
1919 if (b->exp)
1920 {
1921 xfree (b->exp);
1922 b->exp = NULL;
1923 }
d63d0675 1924 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1925 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1926 /* If the meaning of expression itself changed, the old value is
1927 no longer relevant. We don't want to report a watchpoint hit
1928 to the user when the old value and the new value may actually
1929 be completely different objects. */
1930 value_free (b->val);
fa4727a6
DJ
1931 b->val = NULL;
1932 b->val_valid = 0;
60e1c644
PA
1933
1934 /* Note that unlike with breakpoints, the watchpoint's condition
1935 expression is stored in the breakpoint object, not in the
1936 locations (re)created below. */
3a5c3e22 1937 if (b->base.cond_string != NULL)
60e1c644
PA
1938 {
1939 if (b->cond_exp != NULL)
1940 {
1941 xfree (b->cond_exp);
1942 b->cond_exp = NULL;
1943 }
1944
3a5c3e22 1945 s = b->base.cond_string;
1bb9788d 1946 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1947 }
a5606eee 1948 }
a5606eee
VP
1949
1950 /* If we failed to parse the expression, for example because
1951 it refers to a global variable in a not-yet-loaded shared library,
1952 don't try to insert watchpoint. We don't automatically delete
1953 such watchpoint, though, since failure to parse expression
1954 is different from out-of-scope watchpoint. */
e8369a73 1955 if (!target_has_execution)
2d134ed3
PA
1956 {
1957 /* Without execution, memory can't change. No use to try and
1958 set watchpoint locations. The watchpoint will be reset when
1959 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1960 if (!can_use_hw_watchpoints)
1961 {
1962 if (b->base.ops->works_in_software_mode (&b->base))
1963 b->base.type = bp_watchpoint;
1964 else
638aa5a1
AB
1965 error (_("Can't set read/access watchpoint when "
1966 "hardware watchpoints are disabled."));
e8369a73 1967 }
2d134ed3
PA
1968 }
1969 else if (within_current_scope && b->exp)
a5606eee 1970 {
0cf6dd15 1971 int pc = 0;
fa4727a6 1972 struct value *val_chain, *v, *result, *next;
2d134ed3 1973 struct program_space *frame_pspace;
a5606eee 1974
3a1115a0 1975 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1976
a5606eee
VP
1977 /* Avoid setting b->val if it's already set. The meaning of
1978 b->val is 'the last value' user saw, and we should update
1979 it only if we reported that last value to user. As it
9c06b0b4
TJB
1980 happens, the code that reports it updates b->val directly.
1981 We don't keep track of the memory value for masked
1982 watchpoints. */
3a5c3e22 1983 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1984 {
bb9d5f81
PP
1985 if (b->val_bitsize != 0)
1986 {
1987 v = extract_bitfield_from_watchpoint_value (b, v);
1988 if (v != NULL)
1989 release_value (v);
1990 }
fa4727a6
DJ
1991 b->val = v;
1992 b->val_valid = 1;
1993 }
a5606eee 1994
2d134ed3
PA
1995 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1996
a5606eee 1997 /* Look at each value on the value chain. */
9fa40276 1998 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1999 {
2000 /* If it's a memory location, and GDB actually needed
2001 its contents to evaluate the expression, then we
fa4727a6
DJ
2002 must watch it. If the first value returned is
2003 still lazy, that means an error occurred reading it;
2004 watch it anyway in case it becomes readable. */
a5606eee 2005 if (VALUE_LVAL (v) == lval_memory
fa4727a6 2006 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
2007 {
2008 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2009
a5606eee
VP
2010 /* We only watch structs and arrays if user asked
2011 for it explicitly, never if they just happen to
2012 appear in the middle of some value chain. */
fa4727a6 2013 if (v == result
a5606eee
VP
2014 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2015 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2016 {
2017 CORE_ADDR addr;
f486487f 2018 enum target_hw_bp_type type;
a5606eee 2019 struct bp_location *loc, **tmp;
bb9d5f81
PP
2020 int bitpos = 0, bitsize = 0;
2021
2022 if (value_bitsize (v) != 0)
2023 {
2024 /* Extract the bit parameters out from the bitfield
2025 sub-expression. */
2026 bitpos = value_bitpos (v);
2027 bitsize = value_bitsize (v);
2028 }
2029 else if (v == result && b->val_bitsize != 0)
2030 {
2031 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2032 lvalue whose bit parameters are saved in the fields
2033 VAL_BITPOS and VAL_BITSIZE. */
2034 bitpos = b->val_bitpos;
2035 bitsize = b->val_bitsize;
2036 }
a5606eee 2037
42ae5230 2038 addr = value_address (v);
bb9d5f81
PP
2039 if (bitsize != 0)
2040 {
2041 /* Skip the bytes that don't contain the bitfield. */
2042 addr += bitpos / 8;
2043 }
2044
a5606eee 2045 type = hw_write;
3a5c3e22 2046 if (b->base.type == bp_read_watchpoint)
a5606eee 2047 type = hw_read;
3a5c3e22 2048 else if (b->base.type == bp_access_watchpoint)
a5606eee 2049 type = hw_access;
3a5c3e22
PA
2050
2051 loc = allocate_bp_location (&b->base);
2052 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2053 ;
2054 *tmp = loc;
a6d9a66e 2055 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2056
2057 loc->pspace = frame_pspace;
a5606eee 2058 loc->address = addr;
bb9d5f81
PP
2059
2060 if (bitsize != 0)
2061 {
2062 /* Just cover the bytes that make up the bitfield. */
2063 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2064 }
2065 else
2066 loc->length = TYPE_LENGTH (value_type (v));
2067
a5606eee
VP
2068 loc->watchpoint_type = type;
2069 }
2070 }
9fa40276
TJB
2071 }
2072
2073 /* Change the type of breakpoint between hardware assisted or
2074 an ordinary watchpoint depending on the hardware support
2075 and free hardware slots. REPARSE is set when the inferior
2076 is started. */
a9634178 2077 if (reparse)
9fa40276 2078 {
e09342b5 2079 int reg_cnt;
9fa40276
TJB
2080 enum bp_loc_type loc_type;
2081 struct bp_location *bl;
a5606eee 2082
a9634178 2083 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2084
2085 if (reg_cnt)
9fa40276
TJB
2086 {
2087 int i, target_resources_ok, other_type_used;
a1398e0c 2088 enum bptype type;
9fa40276 2089
a9634178
TJB
2090 /* Use an exact watchpoint when there's only one memory region to be
2091 watched, and only one debug register is needed to watch it. */
2092 b->exact = target_exact_watchpoints && reg_cnt == 1;
2093
9fa40276 2094 /* We need to determine how many resources are already
e09342b5
TJB
2095 used for all other hardware watchpoints plus this one
2096 to see if we still have enough resources to also fit
a1398e0c
PA
2097 this watchpoint in as well. */
2098
2099 /* If this is a software watchpoint, we try to turn it
2100 to a hardware one -- count resources as if B was of
2101 hardware watchpoint type. */
2102 type = b->base.type;
2103 if (type == bp_watchpoint)
2104 type = bp_hardware_watchpoint;
2105
2106 /* This watchpoint may or may not have been placed on
2107 the list yet at this point (it won't be in the list
2108 if we're trying to create it for the first time,
2109 through watch_command), so always account for it
2110 manually. */
2111
2112 /* Count resources used by all watchpoints except B. */
2113 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2114
2115 /* Add in the resources needed for B. */
2116 i += hw_watchpoint_use_count (&b->base);
2117
2118 target_resources_ok
2119 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2120 if (target_resources_ok <= 0)
a9634178 2121 {
3a5c3e22 2122 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2123
2124 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2125 error (_("Target does not support this type of "
2126 "hardware watchpoint."));
9c06b0b4
TJB
2127 else if (target_resources_ok < 0 && !sw_mode)
2128 error (_("There are not enough available hardware "
2129 "resources for this watchpoint."));
a1398e0c
PA
2130
2131 /* Downgrade to software watchpoint. */
2132 b->base.type = bp_watchpoint;
2133 }
2134 else
2135 {
2136 /* If this was a software watchpoint, we've just
2137 found we have enough resources to turn it to a
2138 hardware watchpoint. Otherwise, this is a
2139 nop. */
2140 b->base.type = type;
a9634178 2141 }
9fa40276 2142 }
3a5c3e22 2143 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2144 {
2145 if (!can_use_hw_watchpoints)
2146 error (_("Can't set read/access watchpoint when "
2147 "hardware watchpoints are disabled."));
2148 else
2149 error (_("Expression cannot be implemented with "
2150 "read/access watchpoint."));
2151 }
9fa40276 2152 else
3a5c3e22 2153 b->base.type = bp_watchpoint;
9fa40276 2154
3a5c3e22 2155 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2156 : bp_loc_hardware_watchpoint);
3a5c3e22 2157 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2158 bl->loc_type = loc_type;
2159 }
2160
2161 for (v = val_chain; v; v = next)
2162 {
a5606eee
VP
2163 next = value_next (v);
2164 if (v != b->val)
2165 value_free (v);
2166 }
2167
c7437ca6
PA
2168 /* If a software watchpoint is not watching any memory, then the
2169 above left it without any location set up. But,
2170 bpstat_stop_status requires a location to be able to report
2171 stops, so make sure there's at least a dummy one. */
3a5c3e22 2172 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2173 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2174 }
2175 else if (!within_current_scope)
7270d8f2 2176 {
ac74f770
MS
2177 printf_filtered (_("\
2178Watchpoint %d deleted because the program has left the block\n\
2179in which its expression is valid.\n"),
3a5c3e22 2180 b->base.number);
d0fb5eae 2181 watchpoint_del_at_next_stop (b);
7270d8f2 2182 }
a5606eee
VP
2183
2184 /* Restore the selected frame. */
66076460
DJ
2185 if (frame_saved)
2186 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2187}
2188
a5606eee 2189
74960c60 2190/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2191 inserted in the inferior. We don't differentiate the type of BL's owner
2192 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2193 breakpoint_ops is not defined, because in insert_bp_location,
2194 tracepoint's insert_location will not be called. */
74960c60 2195static int
35df4500 2196should_be_inserted (struct bp_location *bl)
74960c60 2197{
35df4500 2198 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2199 return 0;
2200
35df4500 2201 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2202 return 0;
2203
35df4500 2204 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2205 return 0;
2206
f8eba3c6
TT
2207 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2208 return 0;
2209
56710373
PA
2210 /* This is set for example, when we're attached to the parent of a
2211 vfork, and have detached from the child. The child is running
2212 free, and we expect it to do an exec or exit, at which point the
2213 OS makes the parent schedulable again (and the target reports
2214 that the vfork is done). Until the child is done with the shared
2215 memory region, do not insert breakpoints in the parent, otherwise
2216 the child could still trip on the parent's breakpoints. Since
2217 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2218 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2219 return 0;
2220
31e77af2
PA
2221 /* Don't insert a breakpoint if we're trying to step past its
2222 location. */
2223 if ((bl->loc_type == bp_loc_software_breakpoint
2224 || bl->loc_type == bp_loc_hardware_breakpoint)
2225 && stepping_past_instruction_at (bl->pspace->aspace,
2226 bl->address))
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)
3185 remove_breakpoint (loc, mark_uninserted);
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))
35df4500 3225 val |= remove_breakpoint (bl, mark_uninserted);
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 {
35df4500 3270 val = remove_breakpoint (bl, mark_uninserted);
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
TJB
3924 if (bl->inserted)
3925 val |= remove_breakpoint_1 (bl, mark_inserted);
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
35df4500 3939remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
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
3987 val = bl->owner->ops->remove_location (bl);
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
TJB
4004 target_remove_breakpoint (bl->gdbarch,
4005 &bl->overlay_target_info);
c02f5703
MS
4006 }
4007 /* Did we set a breakpoint at the VMA?
4008 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4009 if (bl->inserted)
c906108c 4010 {
c02f5703
MS
4011 /* Yes -- remove it. Previously we did not bother to
4012 remove the breakpoint if the section had been
4013 unmapped, but let's not rely on that being safe. We
4014 don't know what the overlay manager might do. */
aa67235e
UW
4015
4016 /* However, we should remove *software* breakpoints only
4017 if the section is still mapped, or else we overwrite
4018 wrong code with the saved shadow contents. */
348d480f
PA
4019 if (bl->loc_type == bp_loc_hardware_breakpoint
4020 || section_is_mapped (bl->section))
4021 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
4022 else
4023 val = 0;
c906108c 4024 }
c02f5703
MS
4025 else
4026 {
4027 /* No -- not inserted, so no need to remove. No error. */
4028 val = 0;
4029 }
c906108c 4030 }
879d1e6b 4031
08351840
PA
4032 /* In some cases, we might not be able to remove a breakpoint in
4033 a shared library that has already been removed, but we have
4034 not yet processed the shlib unload event. Similarly for an
4035 unloaded add-symbol-file object - the user might not yet have
4036 had the chance to remove-symbol-file it. shlib_disabled will
4037 be set if the library/object has already been removed, but
4038 the breakpoint hasn't been uninserted yet, e.g., after
4039 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4040 always-inserted mode. */
076855f9 4041 if (val
08351840
PA
4042 && (bl->loc_type == bp_loc_software_breakpoint
4043 && (bl->shlib_disabled
4044 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4045 || shared_objfile_contains_address_p (bl->pspace,
4046 bl->address))))
879d1e6b
UW
4047 val = 0;
4048
c906108c
SS
4049 if (val)
4050 return val;
35df4500 4051 bl->inserted = (is == mark_inserted);
c906108c 4052 }
35df4500 4053 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4054 {
77b06cd7
TJB
4055 gdb_assert (bl->owner->ops != NULL
4056 && bl->owner->ops->remove_location != NULL);
4057
35df4500 4058 bl->inserted = (is == mark_inserted);
77b06cd7 4059 bl->owner->ops->remove_location (bl);
2e70b7b9 4060
c906108c 4061 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4062 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4063 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4064 bl->owner->number);
c906108c 4065 }
35df4500
TJB
4066 else if (bl->owner->type == bp_catchpoint
4067 && breakpoint_enabled (bl->owner)
4068 && !bl->duplicate)
ce78b96d 4069 {
77b06cd7
TJB
4070 gdb_assert (bl->owner->ops != NULL
4071 && bl->owner->ops->remove_location != NULL);
ce78b96d 4072
77b06cd7 4073 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4074 if (val)
4075 return val;
77b06cd7 4076
35df4500 4077 bl->inserted = (is == mark_inserted);
ce78b96d 4078 }
c906108c
SS
4079
4080 return 0;
4081}
4082
6c95b8df 4083static int
35df4500 4084remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4085{
4086 int ret;
4087 struct cleanup *old_chain;
4088
35df4500
TJB
4089 /* BL is never in moribund_locations by our callers. */
4090 gdb_assert (bl->owner != NULL);
2bdf28a0 4091
6c95b8df
PA
4092 /* The type of none suggests that owner is actually deleted.
4093 This should not ever happen. */
35df4500 4094 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4095
4096 old_chain = save_current_space_and_thread ();
4097
35df4500 4098 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4099
35df4500 4100 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4101
4102 do_cleanups (old_chain);
4103 return ret;
4104}
4105
c906108c
SS
4106/* Clear the "inserted" flag in all breakpoints. */
4107
25b22b0a 4108void
fba45db2 4109mark_breakpoints_out (void)
c906108c 4110{
35df4500 4111 struct bp_location *bl, **blp_tmp;
c906108c 4112
35df4500 4113 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4114 if (bl->pspace == current_program_space)
35df4500 4115 bl->inserted = 0;
c906108c
SS
4116}
4117
53a5351d
JM
4118/* Clear the "inserted" flag in all breakpoints and delete any
4119 breakpoints which should go away between runs of the program.
c906108c
SS
4120
4121 Plus other such housekeeping that has to be done for breakpoints
4122 between runs.
4123
53a5351d
JM
4124 Note: this function gets called at the end of a run (by
4125 generic_mourn_inferior) and when a run begins (by
4a64f543 4126 init_wait_for_inferior). */
c906108c
SS
4127
4128
4129
4130void
fba45db2 4131breakpoint_init_inferior (enum inf_context context)
c906108c 4132{
35df4500
TJB
4133 struct breakpoint *b, *b_tmp;
4134 struct bp_location *bl, **blp_tmp;
1c5cfe86 4135 int ix;
6c95b8df 4136 struct program_space *pspace = current_program_space;
c906108c 4137
50c71eaf
PA
4138 /* If breakpoint locations are shared across processes, then there's
4139 nothing to do. */
f5656ead 4140 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4141 return;
4142
1a853c52 4143 mark_breakpoints_out ();
075f6582 4144
35df4500 4145 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4146 {
6c95b8df
PA
4147 if (b->loc && b->loc->pspace != pspace)
4148 continue;
4149
c5aa993b
JM
4150 switch (b->type)
4151 {
4152 case bp_call_dummy:
e2e4d78b 4153 case bp_longjmp_call_dummy:
c906108c 4154
c5aa993b 4155 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4156 cause problems when the inferior is rerun, so we better get
4157 rid of it. */
4158
4159 case bp_watchpoint_scope:
4160
4161 /* Also get rid of scope breakpoints. */
4162
4163 case bp_shlib_event:
4164
4165 /* Also remove solib event breakpoints. Their addresses may
4166 have changed since the last time we ran the program.
4167 Actually we may now be debugging against different target;
4168 and so the solib backend that installed this breakpoint may
4169 not be used in by the target. E.g.,
4170
4171 (gdb) file prog-linux
4172 (gdb) run # native linux target
4173 ...
4174 (gdb) kill
4175 (gdb) file prog-win.exe
4176 (gdb) tar rem :9999 # remote Windows gdbserver.
4177 */
c906108c 4178
f59f708a
PA
4179 case bp_step_resume:
4180
4181 /* Also remove step-resume breakpoints. */
4182
7c16b83e
PA
4183 case bp_single_step:
4184
4185 /* Also remove single-step breakpoints. */
4186
c5aa993b
JM
4187 delete_breakpoint (b);
4188 break;
c906108c 4189
c5aa993b
JM
4190 case bp_watchpoint:
4191 case bp_hardware_watchpoint:
4192 case bp_read_watchpoint:
4193 case bp_access_watchpoint:
3a5c3e22
PA
4194 {
4195 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4196
3a5c3e22
PA
4197 /* Likewise for watchpoints on local expressions. */
4198 if (w->exp_valid_block != NULL)
4199 delete_breakpoint (b);
4200 else if (context == inf_starting)
4201 {
4202 /* Reset val field to force reread of starting value in
4203 insert_breakpoints. */
4204 if (w->val)
4205 value_free (w->val);
4206 w->val = NULL;
4207 w->val_valid = 0;
c860120c 4208 }
3a5c3e22 4209 }
c5aa993b
JM
4210 break;
4211 default:
c5aa993b
JM
4212 break;
4213 }
4214 }
1c5cfe86
PA
4215
4216 /* Get rid of the moribund locations. */
35df4500
TJB
4217 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4218 decref_bp_location (&bl);
1c5cfe86 4219 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4220}
4221
6c95b8df
PA
4222/* These functions concern about actual breakpoints inserted in the
4223 target --- to e.g. check if we need to do decr_pc adjustment or if
4224 we need to hop over the bkpt --- so we check for address space
4225 match, not program space. */
4226
c2c6d25f
JM
4227/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4228 exists at PC. It returns ordinary_breakpoint_here if it's an
4229 ordinary breakpoint, or permanent_breakpoint_here if it's a
4230 permanent breakpoint.
4231 - When continuing from a location with an ordinary breakpoint, we
4232 actually single step once before calling insert_breakpoints.
e5dd4106 4233 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4234 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4235 the target, to advance the PC past the breakpoint. */
c906108c 4236
c2c6d25f 4237enum breakpoint_here
6c95b8df 4238breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4239{
35df4500 4240 struct bp_location *bl, **blp_tmp;
c2c6d25f 4241 int any_breakpoint_here = 0;
c906108c 4242
35df4500 4243 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4244 {
35df4500
TJB
4245 if (bl->loc_type != bp_loc_software_breakpoint
4246 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4247 continue;
4248
f1310107 4249 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4250 if ((breakpoint_enabled (bl->owner)
1a853c52 4251 || bl->permanent)
f1310107 4252 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4253 {
4254 if (overlay_debugging
35df4500
TJB
4255 && section_is_overlay (bl->section)
4256 && !section_is_mapped (bl->section))
075f6582 4257 continue; /* unmapped overlay -- can't be a match */
1a853c52 4258 else if (bl->permanent)
075f6582
DJ
4259 return permanent_breakpoint_here;
4260 else
4261 any_breakpoint_here = 1;
4262 }
4263 }
c906108c 4264
f486487f 4265 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4266}
4267
d35ae833
PA
4268/* See breakpoint.h. */
4269
4270int
4271breakpoint_in_range_p (struct address_space *aspace,
4272 CORE_ADDR addr, ULONGEST len)
4273{
4274 struct bp_location *bl, **blp_tmp;
4275
4276 ALL_BP_LOCATIONS (bl, blp_tmp)
4277 {
4278 if (bl->loc_type != bp_loc_software_breakpoint
4279 && bl->loc_type != bp_loc_hardware_breakpoint)
4280 continue;
4281
4282 if ((breakpoint_enabled (bl->owner)
4283 || bl->permanent)
4284 && breakpoint_location_address_range_overlap (bl, aspace,
4285 addr, len))
4286 {
4287 if (overlay_debugging
4288 && section_is_overlay (bl->section)
4289 && !section_is_mapped (bl->section))
4290 {
4291 /* Unmapped overlay -- can't be a match. */
4292 continue;
4293 }
4294
4295 return 1;
4296 }
4297 }
4298
4299 return 0;
4300}
4301
1c5cfe86
PA
4302/* Return true if there's a moribund breakpoint at PC. */
4303
4304int
6c95b8df 4305moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4306{
4307 struct bp_location *loc;
4308 int ix;
4309
4310 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4311 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4312 return 1;
4313
4314 return 0;
4315}
c2c6d25f 4316
f7ce857f
PA
4317/* Returns non-zero iff BL is inserted at PC, in address space
4318 ASPACE. */
4319
4320static int
4321bp_location_inserted_here_p (struct bp_location *bl,
4322 struct address_space *aspace, CORE_ADDR pc)
4323{
4324 if (bl->inserted
4325 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4326 aspace, pc))
4327 {
4328 if (overlay_debugging
4329 && section_is_overlay (bl->section)
4330 && !section_is_mapped (bl->section))
4331 return 0; /* unmapped overlay -- can't be a match */
4332 else
4333 return 1;
4334 }
4335 return 0;
4336}
4337
a1fd2fa5 4338/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4339
4340int
a1fd2fa5 4341breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4342{
f7ce857f
PA
4343 struct bp_location **blp, **blp_tmp = NULL;
4344 struct bp_location *bl;
c906108c 4345
f7ce857f 4346 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4347 {
f7ce857f
PA
4348 struct bp_location *bl = *blp;
4349
35df4500
TJB
4350 if (bl->loc_type != bp_loc_software_breakpoint
4351 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4352 continue;
4353
f7ce857f
PA
4354 if (bp_location_inserted_here_p (bl, aspace, pc))
4355 return 1;
c5aa993b 4356 }
c36b740a
VP
4357 return 0;
4358}
4359
a1fd2fa5
PA
4360/* This function returns non-zero iff there is a software breakpoint
4361 inserted at PC. */
c36b740a
VP
4362
4363int
a1fd2fa5
PA
4364software_breakpoint_inserted_here_p (struct address_space *aspace,
4365 CORE_ADDR pc)
4fa8626c 4366{
f7ce857f
PA
4367 struct bp_location **blp, **blp_tmp = NULL;
4368 struct bp_location *bl;
4fa8626c 4369
f7ce857f 4370 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4371 {
f7ce857f
PA
4372 struct bp_location *bl = *blp;
4373
35df4500 4374 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4375 continue;
4376
f7ce857f
PA
4377 if (bp_location_inserted_here_p (bl, aspace, pc))
4378 return 1;
4fa8626c
DJ
4379 }
4380
4381 return 0;
9c02b525
PA
4382}
4383
4384/* See breakpoint.h. */
4385
4386int
4387hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4388 CORE_ADDR pc)
4389{
4390 struct bp_location **blp, **blp_tmp = NULL;
4391 struct bp_location *bl;
4392
4393 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4394 {
4395 struct bp_location *bl = *blp;
4396
4397 if (bl->loc_type != bp_loc_hardware_breakpoint)
4398 continue;
4399
4400 if (bp_location_inserted_here_p (bl, aspace, pc))
4401 return 1;
4402 }
4403
4404 return 0;
4fa8626c
DJ
4405}
4406
9093389c
PA
4407int
4408hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4409 CORE_ADDR addr, ULONGEST len)
4410{
4411 struct breakpoint *bpt;
4412
4413 ALL_BREAKPOINTS (bpt)
4414 {
4415 struct bp_location *loc;
4416
4417 if (bpt->type != bp_hardware_watchpoint
4418 && bpt->type != bp_access_watchpoint)
4419 continue;
4420
4421 if (!breakpoint_enabled (bpt))
4422 continue;
4423
4424 for (loc = bpt->loc; loc; loc = loc->next)
4425 if (loc->pspace->aspace == aspace && loc->inserted)
4426 {
4427 CORE_ADDR l, h;
4428
4429 /* Check for intersection. */
4430 l = max (loc->address, addr);
4431 h = min (loc->address + loc->length, addr + len);
4432 if (l < h)
4433 return 1;
4434 }
4435 }
4436 return 0;
4437}
c906108c 4438\f
c5aa993b 4439
c906108c
SS
4440/* bpstat stuff. External routines' interfaces are documented
4441 in breakpoint.h. */
4442
4443int
c326b90e 4444is_catchpoint (struct breakpoint *ep)
c906108c 4445{
533be4dd 4446 return (ep->type == bp_catchpoint);
c906108c
SS
4447}
4448
f431efe5
PA
4449/* Frees any storage that is part of a bpstat. Does not walk the
4450 'next' chain. */
4451
4452static void
198757a8
VP
4453bpstat_free (bpstat bs)
4454{
4455 if (bs->old_val != NULL)
4456 value_free (bs->old_val);
9add0f1b 4457 decref_counted_command_line (&bs->commands);
f431efe5 4458 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4459 xfree (bs);
4460}
4461
c906108c
SS
4462/* Clear a bpstat so that it says we are not at any breakpoint.
4463 Also free any storage that is part of a bpstat. */
4464
4465void
fba45db2 4466bpstat_clear (bpstat *bsp)
c906108c
SS
4467{
4468 bpstat p;
4469 bpstat q;
4470
4471 if (bsp == 0)
4472 return;
4473 p = *bsp;
4474 while (p != NULL)
4475 {
4476 q = p->next;
198757a8 4477 bpstat_free (p);
c906108c
SS
4478 p = q;
4479 }
4480 *bsp = NULL;
4481}
4482
4483/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4484 is part of the bpstat is copied as well. */
4485
4486bpstat
fba45db2 4487bpstat_copy (bpstat bs)
c906108c
SS
4488{
4489 bpstat p = NULL;
4490 bpstat tmp;
4491 bpstat retval = NULL;
4492
4493 if (bs == NULL)
4494 return bs;
4495
4496 for (; bs != NULL; bs = bs->next)
4497 {
4498 tmp = (bpstat) xmalloc (sizeof (*tmp));
4499 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4500 incref_counted_command_line (tmp->commands);
f431efe5 4501 incref_bp_location (tmp->bp_location_at);
31cc81e9 4502 if (bs->old_val != NULL)
3c3185ac
JK
4503 {
4504 tmp->old_val = value_copy (bs->old_val);
4505 release_value (tmp->old_val);
4506 }
31cc81e9 4507
c906108c
SS
4508 if (p == NULL)
4509 /* This is the first thing in the chain. */
4510 retval = tmp;
4511 else
4512 p->next = tmp;
4513 p = tmp;
4514 }
4515 p->next = NULL;
4516 return retval;
4517}
4518
4a64f543 4519/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4520
4521bpstat
fba45db2 4522bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4523{
c5aa993b
JM
4524 if (bsp == NULL)
4525 return NULL;
c906108c 4526
c5aa993b
JM
4527 for (; bsp != NULL; bsp = bsp->next)
4528 {
f431efe5 4529 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4530 return bsp;
4531 }
c906108c
SS
4532 return NULL;
4533}
4534
ab04a2af
TT
4535/* See breakpoint.h. */
4536
47591c29 4537int
427cd150 4538bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4539{
ab04a2af
TT
4540 for (; bsp != NULL; bsp = bsp->next)
4541 {
427cd150
TT
4542 if (bsp->breakpoint_at == NULL)
4543 {
4544 /* A moribund location can never explain a signal other than
4545 GDB_SIGNAL_TRAP. */
4546 if (sig == GDB_SIGNAL_TRAP)
47591c29 4547 return 1;
427cd150
TT
4548 }
4549 else
47591c29
PA
4550 {
4551 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4552 sig))
4553 return 1;
4554 }
ab04a2af
TT
4555 }
4556
47591c29 4557 return 0;
ab04a2af
TT
4558}
4559
4a64f543
MS
4560/* Put in *NUM the breakpoint number of the first breakpoint we are
4561 stopped at. *BSP upon return is a bpstat which points to the
4562 remaining breakpoints stopped at (but which is not guaranteed to be
4563 good for anything but further calls to bpstat_num).
4564
8671a17b
PA
4565 Return 0 if passed a bpstat which does not indicate any breakpoints.
4566 Return -1 if stopped at a breakpoint that has been deleted since
4567 we set it.
4568 Return 1 otherwise. */
c906108c
SS
4569
4570int
8671a17b 4571bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4572{
4573 struct breakpoint *b;
4574
4575 if ((*bsp) == NULL)
4576 return 0; /* No more breakpoint values */
8671a17b 4577
4a64f543
MS
4578 /* We assume we'll never have several bpstats that correspond to a
4579 single breakpoint -- otherwise, this function might return the
4580 same number more than once and this will look ugly. */
f431efe5 4581 b = (*bsp)->breakpoint_at;
8671a17b
PA
4582 *bsp = (*bsp)->next;
4583 if (b == NULL)
4584 return -1; /* breakpoint that's been deleted since */
4585
4586 *num = b->number; /* We have its number */
4587 return 1;
c906108c
SS
4588}
4589
e93ca019 4590/* See breakpoint.h. */
c906108c
SS
4591
4592void
e93ca019 4593bpstat_clear_actions (void)
c906108c 4594{
e93ca019
JK
4595 struct thread_info *tp;
4596 bpstat bs;
4597
4598 if (ptid_equal (inferior_ptid, null_ptid))
4599 return;
4600
4601 tp = find_thread_ptid (inferior_ptid);
4602 if (tp == NULL)
4603 return;
4604
4605 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4606 {
9add0f1b 4607 decref_counted_command_line (&bs->commands);
abf85f46 4608
c906108c
SS
4609 if (bs->old_val != NULL)
4610 {
4611 value_free (bs->old_val);
4612 bs->old_val = NULL;
4613 }
4614 }
4615}
4616
f3b1572e
PA
4617/* Called when a command is about to proceed the inferior. */
4618
4619static void
4620breakpoint_about_to_proceed (void)
4621{
4622 if (!ptid_equal (inferior_ptid, null_ptid))
4623 {
4624 struct thread_info *tp = inferior_thread ();
4625
4626 /* Allow inferior function calls in breakpoint commands to not
4627 interrupt the command list. When the call finishes
4628 successfully, the inferior will be standing at the same
4629 breakpoint as if nothing happened. */
16c381f0 4630 if (tp->control.in_infcall)
f3b1572e
PA
4631 return;
4632 }
4633
4634 breakpoint_proceeded = 1;
4635}
4636
4a64f543
MS
4637/* Stub for cleaning up our state if we error-out of a breakpoint
4638 command. */
c906108c 4639static void
4efb68b1 4640cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4641{
4642 executing_breakpoint_commands = 0;
4643}
4644
abf85f46
JK
4645/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4646 or its equivalent. */
4647
4648static int
4649command_line_is_silent (struct command_line *cmd)
4650{
4f45d445 4651 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4652}
4653
4a64f543
MS
4654/* Execute all the commands associated with all the breakpoints at
4655 this location. Any of these commands could cause the process to
4656 proceed beyond this point, etc. We look out for such changes by
4657 checking the global "breakpoint_proceeded" after each command.
c906108c 4658
347bddb7
PA
4659 Returns true if a breakpoint command resumed the inferior. In that
4660 case, it is the caller's responsibility to recall it again with the
4661 bpstat of the current thread. */
4662
4663static int
4664bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4665{
4666 bpstat bs;
4667 struct cleanup *old_chain;
347bddb7 4668 int again = 0;
c906108c
SS
4669
4670 /* Avoid endless recursion if a `source' command is contained
4671 in bs->commands. */
4672 if (executing_breakpoint_commands)
347bddb7 4673 return 0;
c906108c
SS
4674
4675 executing_breakpoint_commands = 1;
4676 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4677
cf6c5ffb
TT
4678 prevent_dont_repeat ();
4679
4a64f543 4680 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4681 bs = *bsp;
4682
4683 breakpoint_proceeded = 0;
4684 for (; bs != NULL; bs = bs->next)
4685 {
9add0f1b 4686 struct counted_command_line *ccmd;
6c50ab1c
JB
4687 struct command_line *cmd;
4688 struct cleanup *this_cmd_tree_chain;
4689
4690 /* Take ownership of the BSP's command tree, if it has one.
4691
4692 The command tree could legitimately contain commands like
4693 'step' and 'next', which call clear_proceed_status, which
4694 frees stop_bpstat's command tree. To make sure this doesn't
4695 free the tree we're executing out from under us, we need to
4696 take ownership of the tree ourselves. Since a given bpstat's
4697 commands are only executed once, we don't need to copy it; we
4698 can clear the pointer in the bpstat, and make sure we free
4699 the tree when we're done. */
9add0f1b
TT
4700 ccmd = bs->commands;
4701 bs->commands = NULL;
abf85f46
JK
4702 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4703 cmd = ccmd ? ccmd->commands : NULL;
4704 if (command_line_is_silent (cmd))
4705 {
4706 /* The action has been already done by bpstat_stop_status. */
4707 cmd = cmd->next;
4708 }
6c50ab1c 4709
c906108c
SS
4710 while (cmd != NULL)
4711 {
4712 execute_control_command (cmd);
4713
4714 if (breakpoint_proceeded)
4715 break;
4716 else
4717 cmd = cmd->next;
4718 }
6c50ab1c
JB
4719
4720 /* We can free this command tree now. */
4721 do_cleanups (this_cmd_tree_chain);
4722
c906108c 4723 if (breakpoint_proceeded)
32c1e744 4724 {
0b333c5e 4725 if (interpreter_async)
347bddb7
PA
4726 /* If we are in async mode, then the target might be still
4727 running, not stopped at any breakpoint, so nothing for
4728 us to do here -- just return to the event loop. */
4729 ;
32c1e744
VP
4730 else
4731 /* In sync mode, when execute_control_command returns
4732 we're already standing on the next breakpoint.
347bddb7
PA
4733 Breakpoint commands for that stop were not run, since
4734 execute_command does not run breakpoint commands --
4735 only command_line_handler does, but that one is not
4736 involved in execution of breakpoint commands. So, we
4737 can now execute breakpoint commands. It should be
4738 noted that making execute_command do bpstat actions is
4739 not an option -- in this case we'll have recursive
4740 invocation of bpstat for each breakpoint with a
4741 command, and can easily blow up GDB stack. Instead, we
4742 return true, which will trigger the caller to recall us
4743 with the new stop_bpstat. */
4744 again = 1;
4745 break;
32c1e744 4746 }
c906108c 4747 }
c2b8ed2c 4748 do_cleanups (old_chain);
347bddb7
PA
4749 return again;
4750}
4751
4752void
4753bpstat_do_actions (void)
4754{
353d1d73
JK
4755 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4756
347bddb7
PA
4757 /* Do any commands attached to breakpoint we are stopped at. */
4758 while (!ptid_equal (inferior_ptid, null_ptid)
4759 && target_has_execution
4760 && !is_exited (inferior_ptid)
4761 && !is_executing (inferior_ptid))
4762 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4763 and only return when it is stopped at the next breakpoint, we
4764 keep doing breakpoint actions until it returns false to
4765 indicate the inferior was not resumed. */
16c381f0 4766 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4767 break;
353d1d73
JK
4768
4769 discard_cleanups (cleanup_if_error);
c906108c
SS
4770}
4771
fa4727a6
DJ
4772/* Print out the (old or new) value associated with a watchpoint. */
4773
4774static void
4775watchpoint_value_print (struct value *val, struct ui_file *stream)
4776{
4777 if (val == NULL)
4778 fprintf_unfiltered (stream, _("<unreadable>"));
4779 else
79a45b7d
TT
4780 {
4781 struct value_print_options opts;
4782 get_user_print_options (&opts);
4783 value_print (val, stream, &opts);
4784 }
fa4727a6
DJ
4785}
4786
f303dbd6
PA
4787/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4788 debugging multiple threads. */
4789
4790void
4791maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4792{
4793 if (ui_out_is_mi_like_p (uiout))
4794 return;
4795
4796 ui_out_text (uiout, "\n");
4797
4798 if (show_thread_that_caused_stop ())
4799 {
4800 const char *name;
4801 struct thread_info *thr = inferior_thread ();
4802
4803 ui_out_text (uiout, "Thread ");
4804 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4805
4806 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4807 if (name != NULL)
4808 {
4809 ui_out_text (uiout, " \"");
4810 ui_out_field_fmt (uiout, "name", "%s", name);
4811 ui_out_text (uiout, "\"");
4812 }
4813
4814 ui_out_text (uiout, " hit ");
4815 }
4816}
4817
e514a9d6 4818/* Generic routine for printing messages indicating why we
4a64f543 4819 stopped. The behavior of this function depends on the value
e514a9d6
JM
4820 'print_it' in the bpstat structure. Under some circumstances we
4821 may decide not to print anything here and delegate the task to
4a64f543 4822 normal_stop(). */
e514a9d6
JM
4823
4824static enum print_stop_action
4825print_bp_stop_message (bpstat bs)
4826{
4827 switch (bs->print_it)
4828 {
4829 case print_it_noop:
4a64f543 4830 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4831 return PRINT_UNKNOWN;
4832 break;
4833
4834 case print_it_done:
4835 /* We still want to print the frame, but we already printed the
4a64f543 4836 relevant messages. */
e514a9d6
JM
4837 return PRINT_SRC_AND_LOC;
4838 break;
4839
4840 case print_it_normal:
4f8d1dc6 4841 {
f431efe5
PA
4842 struct breakpoint *b = bs->breakpoint_at;
4843
1a6a67de
TJB
4844 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4845 which has since been deleted. */
4846 if (b == NULL)
4847 return PRINT_UNKNOWN;
4848
348d480f
PA
4849 /* Normal case. Call the breakpoint's print_it method. */
4850 return b->ops->print_it (bs);
4f8d1dc6 4851 }
348d480f 4852 break;
3086aeae 4853
e514a9d6 4854 default:
8e65ff28 4855 internal_error (__FILE__, __LINE__,
e2e0b3e5 4856 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4857 break;
c906108c 4858 }
c906108c
SS
4859}
4860
edcc5120
TT
4861/* A helper function that prints a shared library stopped event. */
4862
4863static void
4864print_solib_event (int is_catchpoint)
4865{
4866 int any_deleted
4867 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4868 int any_added
4869 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4870
4871 if (!is_catchpoint)
4872 {
4873 if (any_added || any_deleted)
4874 ui_out_text (current_uiout,
4875 _("Stopped due to shared library event:\n"));
4876 else
4877 ui_out_text (current_uiout,
4878 _("Stopped due to shared library event (no "
4879 "libraries added or removed)\n"));
4880 }
4881
4882 if (ui_out_is_mi_like_p (current_uiout))
4883 ui_out_field_string (current_uiout, "reason",
4884 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4885
4886 if (any_deleted)
4887 {
4888 struct cleanup *cleanup;
4889 char *name;
4890 int ix;
4891
4892 ui_out_text (current_uiout, _(" Inferior unloaded "));
4893 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4894 "removed");
4895 for (ix = 0;
4896 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4897 ix, name);
4898 ++ix)
4899 {
4900 if (ix > 0)
4901 ui_out_text (current_uiout, " ");
4902 ui_out_field_string (current_uiout, "library", name);
4903 ui_out_text (current_uiout, "\n");
4904 }
4905
4906 do_cleanups (cleanup);
4907 }
4908
4909 if (any_added)
4910 {
4911 struct so_list *iter;
4912 int ix;
4913 struct cleanup *cleanup;
4914
4915 ui_out_text (current_uiout, _(" Inferior loaded "));
4916 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4917 "added");
4918 for (ix = 0;
4919 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4920 ix, iter);
4921 ++ix)
4922 {
4923 if (ix > 0)
4924 ui_out_text (current_uiout, " ");
4925 ui_out_field_string (current_uiout, "library", iter->so_name);
4926 ui_out_text (current_uiout, "\n");
4927 }
4928
4929 do_cleanups (cleanup);
4930 }
4931}
4932
e514a9d6
JM
4933/* Print a message indicating what happened. This is called from
4934 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4935 list - a list of the eventpoints that caused this stop. KIND is
4936 the target_waitkind for the stopping event. This
e514a9d6
JM
4937 routine calls the generic print routine for printing a message
4938 about reasons for stopping. This will print (for example) the
4939 "Breakpoint n," part of the output. The return value of this
4940 routine is one of:
c906108c 4941
4a64f543 4942 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4943 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4944 code to print the location. An example is
c5aa993b
JM
4945 "Breakpoint 1, " which should be followed by
4946 the location.
917317f4 4947 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4948 to also print the location part of the message.
4949 An example is the catch/throw messages, which
4a64f543 4950 don't require a location appended to the end.
917317f4 4951 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4952 further info to be printed. */
c906108c 4953
917317f4 4954enum print_stop_action
36dfb11c 4955bpstat_print (bpstat bs, int kind)
c906108c 4956{
f486487f 4957 enum print_stop_action val;
c5aa993b 4958
c906108c 4959 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4960 (Currently all watchpoints go on the bpstat whether hit or not.
4961 That probably could (should) be changed, provided care is taken
c906108c 4962 with respect to bpstat_explains_signal). */
e514a9d6
JM
4963 for (; bs; bs = bs->next)
4964 {
4965 val = print_bp_stop_message (bs);
4966 if (val == PRINT_SRC_ONLY
4967 || val == PRINT_SRC_AND_LOC
4968 || val == PRINT_NOTHING)
4969 return val;
4970 }
c906108c 4971
36dfb11c
TT
4972 /* If we had hit a shared library event breakpoint,
4973 print_bp_stop_message would print out this message. If we hit an
4974 OS-level shared library event, do the same thing. */
4975 if (kind == TARGET_WAITKIND_LOADED)
4976 {
edcc5120 4977 print_solib_event (0);
36dfb11c
TT
4978 return PRINT_NOTHING;
4979 }
4980
e514a9d6 4981 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4982 with and nothing was printed. */
917317f4 4983 return PRINT_UNKNOWN;
c906108c
SS
4984}
4985
c42bd95a
DE
4986/* Evaluate the expression EXP and return 1 if value is zero.
4987 This returns the inverse of the condition because it is called
4988 from catch_errors which returns 0 if an exception happened, and if an
4989 exception happens we want execution to stop.
4a64f543 4990 The argument is a "struct expression *" that has been cast to a
c42bd95a 4991 "void *" to make it pass through catch_errors. */
c906108c
SS
4992
4993static int
4efb68b1 4994breakpoint_cond_eval (void *exp)
c906108c 4995{
278cd55f 4996 struct value *mark = value_mark ();
c5aa993b 4997 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4998
c906108c
SS
4999 value_free_to_mark (mark);
5000 return i;
5001}
5002
5760d0ab 5003/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5004
5005static bpstat
5760d0ab 5006bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5007{
5008 bpstat bs;
5009
5010 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5011 bs->next = NULL;
5012 **bs_link_pointer = bs;
5013 *bs_link_pointer = &bs->next;
f431efe5
PA
5014 bs->breakpoint_at = bl->owner;
5015 bs->bp_location_at = bl;
5016 incref_bp_location (bl);
c906108c
SS
5017 /* If the condition is false, etc., don't do the commands. */
5018 bs->commands = NULL;
5019 bs->old_val = NULL;
5020 bs->print_it = print_it_normal;
5021 return bs;
5022}
5023\f
d983da9c
DJ
5024/* The target has stopped with waitstatus WS. Check if any hardware
5025 watchpoints have triggered, according to the target. */
5026
5027int
5028watchpoints_triggered (struct target_waitstatus *ws)
5029{
d92524f1 5030 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5031 CORE_ADDR addr;
5032 struct breakpoint *b;
5033
5034 if (!stopped_by_watchpoint)
5035 {
5036 /* We were not stopped by a watchpoint. Mark all watchpoints
5037 as not triggered. */
5038 ALL_BREAKPOINTS (b)
cc60f2e3 5039 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042
5043 w->watchpoint_triggered = watch_triggered_no;
5044 }
d983da9c
DJ
5045
5046 return 0;
5047 }
5048
5049 if (!target_stopped_data_address (&current_target, &addr))
5050 {
5051 /* We were stopped by a watchpoint, but we don't know where.
5052 Mark all watchpoints as unknown. */
5053 ALL_BREAKPOINTS (b)
cc60f2e3 5054 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_unknown;
5059 }
d983da9c 5060
3c4797ba 5061 return 1;
d983da9c
DJ
5062 }
5063
5064 /* The target could report the data address. Mark watchpoints
5065 affected by this data address as triggered, and all others as not
5066 triggered. */
5067
5068 ALL_BREAKPOINTS (b)
cc60f2e3 5069 if (is_hardware_watchpoint (b))
d983da9c 5070 {
3a5c3e22 5071 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5072 struct bp_location *loc;
d983da9c 5073
3a5c3e22 5074 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5075 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5076 {
3a5c3e22 5077 if (is_masked_watchpoint (b))
9c06b0b4 5078 {
3a5c3e22
PA
5079 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5080 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5081
5082 if (newaddr == start)
5083 {
3a5c3e22 5084 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5085 break;
5086 }
5087 }
5088 /* Exact match not required. Within range is sufficient. */
5089 else if (target_watchpoint_addr_within_range (&current_target,
5090 addr, loc->address,
5091 loc->length))
5092 {
3a5c3e22 5093 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5094 break;
5095 }
5096 }
d983da9c
DJ
5097 }
5098
5099 return 1;
5100}
5101
c906108c
SS
5102/* Possible return values for watchpoint_check (this can't be an enum
5103 because of check_errors). */
5104/* The watchpoint has been deleted. */
5105#define WP_DELETED 1
5106/* The value has changed. */
5107#define WP_VALUE_CHANGED 2
5108/* The value has not changed. */
5109#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5110/* Ignore this watchpoint, no matter if the value changed or not. */
5111#define WP_IGNORE 4
c906108c
SS
5112
5113#define BP_TEMPFLAG 1
5114#define BP_HARDWAREFLAG 2
5115
4a64f543
MS
5116/* Evaluate watchpoint condition expression and check if its value
5117 changed.
553e4c11
JB
5118
5119 P should be a pointer to struct bpstat, but is defined as a void *
5120 in order for this function to be usable with catch_errors. */
c906108c
SS
5121
5122static int
4efb68b1 5123watchpoint_check (void *p)
c906108c
SS
5124{
5125 bpstat bs = (bpstat) p;
3a5c3e22 5126 struct watchpoint *b;
c906108c
SS
5127 struct frame_info *fr;
5128 int within_current_scope;
5129
f431efe5 5130 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5131 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5132 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5133
f6bc2008
PA
5134 /* If this is a local watchpoint, we only want to check if the
5135 watchpoint frame is in scope if the current thread is the thread
5136 that was used to create the watchpoint. */
5137 if (!watchpoint_in_thread_scope (b))
60e1c644 5138 return WP_IGNORE;
f6bc2008 5139
c906108c
SS
5140 if (b->exp_valid_block == NULL)
5141 within_current_scope = 1;
5142 else
5143 {
edb3359d
DJ
5144 struct frame_info *frame = get_current_frame ();
5145 struct gdbarch *frame_arch = get_frame_arch (frame);
5146 CORE_ADDR frame_pc = get_frame_pc (frame);
5147
c9cf6e20 5148 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5149 still in the function but the stack frame has already been
5150 invalidated. Since we can't rely on the values of local
5151 variables after the stack has been destroyed, we are treating
5152 the watchpoint in that state as `not changed' without further
5153 checking. Don't mark watchpoints as changed if the current
5154 frame is in an epilogue - even if they are in some other
5155 frame, our view of the stack is likely to be wrong and
5156 frame_find_by_id could error out. */
c9cf6e20 5157 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5158 return WP_IGNORE;
a0f49112 5159
101dcfbe 5160 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5161 within_current_scope = (fr != NULL);
69fbadd5
DJ
5162
5163 /* If we've gotten confused in the unwinder, we might have
5164 returned a frame that can't describe this variable. */
edb3359d
DJ
5165 if (within_current_scope)
5166 {
5167 struct symbol *function;
5168
5169 function = get_frame_function (fr);
5170 if (function == NULL
5171 || !contained_in (b->exp_valid_block,
5172 SYMBOL_BLOCK_VALUE (function)))
5173 within_current_scope = 0;
5174 }
69fbadd5 5175
edb3359d 5176 if (within_current_scope)
c906108c
SS
5177 /* If we end up stopping, the current frame will get selected
5178 in normal_stop. So this call to select_frame won't affect
5179 the user. */
0f7d239c 5180 select_frame (fr);
c906108c 5181 }
c5aa993b 5182
c906108c
SS
5183 if (within_current_scope)
5184 {
4a64f543
MS
5185 /* We use value_{,free_to_}mark because it could be a *long*
5186 time before we return to the command level and call
5187 free_all_values. We can't call free_all_values because we
5188 might be in the middle of evaluating a function call. */
c906108c 5189
0cf6dd15 5190 int pc = 0;
9c06b0b4 5191 struct value *mark;
fa4727a6
DJ
5192 struct value *new_val;
5193
3a5c3e22 5194 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5195 /* Since we don't know the exact trigger address (from
5196 stopped_data_address), just tell the user we've triggered
5197 a mask watchpoint. */
5198 return WP_VALUE_CHANGED;
5199
5200 mark = value_mark ();
3a1115a0 5201 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5202
bb9d5f81
PP
5203 if (b->val_bitsize != 0)
5204 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5205
4a64f543
MS
5206 /* We use value_equal_contents instead of value_equal because
5207 the latter coerces an array to a pointer, thus comparing just
5208 the address of the array instead of its contents. This is
5209 not what we want. */
fa4727a6 5210 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5211 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5212 {
fa4727a6
DJ
5213 if (new_val != NULL)
5214 {
5215 release_value (new_val);
5216 value_free_to_mark (mark);
5217 }
c906108c
SS
5218 bs->old_val = b->val;
5219 b->val = new_val;
fa4727a6 5220 b->val_valid = 1;
c906108c
SS
5221 return WP_VALUE_CHANGED;
5222 }
5223 else
5224 {
60e1c644 5225 /* Nothing changed. */
c906108c 5226 value_free_to_mark (mark);
c906108c
SS
5227 return WP_VALUE_NOT_CHANGED;
5228 }
5229 }
5230 else
5231 {
79a45e25
PA
5232 struct ui_out *uiout = current_uiout;
5233
c906108c 5234 /* This seems like the only logical thing to do because
c5aa993b
JM
5235 if we temporarily ignored the watchpoint, then when
5236 we reenter the block in which it is valid it contains
5237 garbage (in the case of a function, it may have two
5238 garbage values, one before and one after the prologue).
5239 So we can't even detect the first assignment to it and
5240 watch after that (since the garbage may or may not equal
5241 the first value assigned). */
348d480f
PA
5242 /* We print all the stop information in
5243 breakpoint_ops->print_it, but in this case, by the time we
5244 call breakpoint_ops->print_it this bp will be deleted
5245 already. So we have no choice but print the information
5246 here. */
9dc5e2a9 5247 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5248 ui_out_field_string
5249 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5250 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5251 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5252 ui_out_text (uiout,
5253 " deleted because the program has left the block in\n\
8b93c638 5254which its expression is valid.\n");
4ce44c66 5255
cdac0397 5256 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5257 decref_counted_command_line (&b->base.commands);
d0fb5eae 5258 watchpoint_del_at_next_stop (b);
c906108c
SS
5259
5260 return WP_DELETED;
5261 }
5262}
5263
18a18393 5264/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5265 breakpoint location BL. This function does not check if we should
5266 stop, only if BL explains the stop. */
5267
18a18393 5268static int
6c95b8df 5269bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5270 struct address_space *aspace, CORE_ADDR bp_addr,
5271 const struct target_waitstatus *ws)
18a18393
VP
5272{
5273 struct breakpoint *b = bl->owner;
5274
348d480f 5275 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5276 gdb_assert (b != NULL);
5277
09ac7c10 5278 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5279}
5280
3a5c3e22
PA
5281/* Determine if the watched values have actually changed, and we
5282 should stop. If not, set BS->stop to 0. */
5283
18a18393
VP
5284static void
5285bpstat_check_watchpoint (bpstat bs)
5286{
2bdf28a0 5287 const struct bp_location *bl;
3a5c3e22 5288 struct watchpoint *b;
2bdf28a0
JK
5289
5290 /* BS is built for existing struct breakpoint. */
f431efe5 5291 bl = bs->bp_location_at;
2bdf28a0 5292 gdb_assert (bl != NULL);
3a5c3e22 5293 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5294 gdb_assert (b != NULL);
18a18393 5295
18a18393 5296 {
18a18393
VP
5297 int must_check_value = 0;
5298
3a5c3e22 5299 if (b->base.type == bp_watchpoint)
18a18393
VP
5300 /* For a software watchpoint, we must always check the
5301 watched value. */
5302 must_check_value = 1;
5303 else if (b->watchpoint_triggered == watch_triggered_yes)
5304 /* We have a hardware watchpoint (read, write, or access)
5305 and the target earlier reported an address watched by
5306 this watchpoint. */
5307 must_check_value = 1;
5308 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5309 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5310 /* We were stopped by a hardware watchpoint, but the target could
5311 not report the data address. We must check the watchpoint's
5312 value. Access and read watchpoints are out of luck; without
5313 a data address, we can't figure it out. */
5314 must_check_value = 1;
3a5c3e22 5315
18a18393
VP
5316 if (must_check_value)
5317 {
3e43a32a
MS
5318 char *message
5319 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5320 b->base.number);
18a18393
VP
5321 struct cleanup *cleanups = make_cleanup (xfree, message);
5322 int e = catch_errors (watchpoint_check, bs, message,
5323 RETURN_MASK_ALL);
5324 do_cleanups (cleanups);
5325 switch (e)
5326 {
5327 case WP_DELETED:
5328 /* We've already printed what needs to be printed. */
5329 bs->print_it = print_it_done;
5330 /* Stop. */
5331 break;
60e1c644
PA
5332 case WP_IGNORE:
5333 bs->print_it = print_it_noop;
5334 bs->stop = 0;
5335 break;
18a18393 5336 case WP_VALUE_CHANGED:
3a5c3e22 5337 if (b->base.type == bp_read_watchpoint)
18a18393 5338 {
85d721b8
PA
5339 /* There are two cases to consider here:
5340
4a64f543 5341 1. We're watching the triggered memory for reads.
85d721b8
PA
5342 In that case, trust the target, and always report
5343 the watchpoint hit to the user. Even though
5344 reads don't cause value changes, the value may
5345 have changed since the last time it was read, and
5346 since we're not trapping writes, we will not see
5347 those, and as such we should ignore our notion of
5348 old value.
5349
4a64f543 5350 2. We're watching the triggered memory for both
85d721b8
PA
5351 reads and writes. There are two ways this may
5352 happen:
5353
4a64f543 5354 2.1. This is a target that can't break on data
85d721b8
PA
5355 reads only, but can break on accesses (reads or
5356 writes), such as e.g., x86. We detect this case
5357 at the time we try to insert read watchpoints.
5358
4a64f543 5359 2.2. Otherwise, the target supports read
85d721b8
PA
5360 watchpoints, but, the user set an access or write
5361 watchpoint watching the same memory as this read
5362 watchpoint.
5363
5364 If we're watching memory writes as well as reads,
5365 ignore watchpoint hits when we find that the
5366 value hasn't changed, as reads don't cause
5367 changes. This still gives false positives when
5368 the program writes the same value to memory as
5369 what there was already in memory (we will confuse
5370 it for a read), but it's much better than
5371 nothing. */
5372
5373 int other_write_watchpoint = 0;
5374
5375 if (bl->watchpoint_type == hw_read)
5376 {
5377 struct breakpoint *other_b;
5378
5379 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5380 if (other_b->type == bp_hardware_watchpoint
5381 || other_b->type == bp_access_watchpoint)
85d721b8 5382 {
3a5c3e22
PA
5383 struct watchpoint *other_w =
5384 (struct watchpoint *) other_b;
5385
5386 if (other_w->watchpoint_triggered
5387 == watch_triggered_yes)
5388 {
5389 other_write_watchpoint = 1;
5390 break;
5391 }
85d721b8
PA
5392 }
5393 }
5394
5395 if (other_write_watchpoint
5396 || bl->watchpoint_type == hw_access)
5397 {
5398 /* We're watching the same memory for writes,
5399 and the value changed since the last time we
5400 updated it, so this trap must be for a write.
5401 Ignore it. */
5402 bs->print_it = print_it_noop;
5403 bs->stop = 0;
5404 }
18a18393
VP
5405 }
5406 break;
5407 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5408 if (b->base.type == bp_hardware_watchpoint
5409 || b->base.type == bp_watchpoint)
18a18393
VP
5410 {
5411 /* Don't stop: write watchpoints shouldn't fire if
5412 the value hasn't changed. */
5413 bs->print_it = print_it_noop;
5414 bs->stop = 0;
5415 }
5416 /* Stop. */
5417 break;
5418 default:
5419 /* Can't happen. */
5420 case 0:
5421 /* Error from catch_errors. */
3a5c3e22 5422 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5423 watchpoint_del_at_next_stop (b);
18a18393
VP
5424 /* We've already printed what needs to be printed. */
5425 bs->print_it = print_it_done;
5426 break;
5427 }
5428 }
5429 else /* must_check_value == 0 */
5430 {
5431 /* This is a case where some watchpoint(s) triggered, but
5432 not at the address of this watchpoint, or else no
5433 watchpoint triggered after all. So don't print
5434 anything for this watchpoint. */
5435 bs->print_it = print_it_noop;
5436 bs->stop = 0;
5437 }
5438 }
5439}
5440
7d4df6a4
DE
5441/* For breakpoints that are currently marked as telling gdb to stop,
5442 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5443 of breakpoint referred to by BS. If we should not stop for this
5444 breakpoint, set BS->stop to 0. */
f431efe5 5445
18a18393
VP
5446static void
5447bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5448{
2bdf28a0
JK
5449 const struct bp_location *bl;
5450 struct breakpoint *b;
7d4df6a4
DE
5451 int value_is_zero = 0;
5452 struct expression *cond;
5453
5454 gdb_assert (bs->stop);
2bdf28a0
JK
5455
5456 /* BS is built for existing struct breakpoint. */
f431efe5 5457 bl = bs->bp_location_at;
2bdf28a0 5458 gdb_assert (bl != NULL);
f431efe5 5459 b = bs->breakpoint_at;
2bdf28a0 5460 gdb_assert (b != NULL);
18a18393 5461
b775012e
LM
5462 /* Even if the target evaluated the condition on its end and notified GDB, we
5463 need to do so again since GDB does not know if we stopped due to a
5464 breakpoint or a single step breakpoint. */
5465
18a18393 5466 if (frame_id_p (b->frame_id)
edb3359d 5467 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5468 {
7d4df6a4
DE
5469 bs->stop = 0;
5470 return;
5471 }
60e1c644 5472
12ab52e9
PA
5473 /* If this is a thread/task-specific breakpoint, don't waste cpu
5474 evaluating the condition if this isn't the specified
5475 thread/task. */
5d5658a1 5476 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5477 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5478
6c1b0f7b
DE
5479 {
5480 bs->stop = 0;
5481 return;
5482 }
5483
6dddc817
DE
5484 /* Evaluate extension language breakpoints that have a "stop" method
5485 implemented. */
5486 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5487
7d4df6a4
DE
5488 if (is_watchpoint (b))
5489 {
5490 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5491
7d4df6a4
DE
5492 cond = w->cond_exp;
5493 }
5494 else
5495 cond = bl->cond;
60e1c644 5496
7d4df6a4
DE
5497 if (cond && b->disposition != disp_del_at_next_stop)
5498 {
5499 int within_current_scope = 1;
5500 struct watchpoint * w;
60e1c644 5501
7d4df6a4
DE
5502 /* We use value_mark and value_free_to_mark because it could
5503 be a long time before we return to the command level and
5504 call free_all_values. We can't call free_all_values
5505 because we might be in the middle of evaluating a
5506 function call. */
5507 struct value *mark = value_mark ();
5508
5509 if (is_watchpoint (b))
5510 w = (struct watchpoint *) b;
5511 else
5512 w = NULL;
5513
5514 /* Need to select the frame, with all that implies so that
5515 the conditions will have the right context. Because we
5516 use the frame, we will not see an inlined function's
5517 variables when we arrive at a breakpoint at the start
5518 of the inlined function; the current frame will be the
5519 call site. */
5520 if (w == NULL || w->cond_exp_valid_block == NULL)
5521 select_frame (get_current_frame ());
5522 else
18a18393 5523 {
7d4df6a4
DE
5524 struct frame_info *frame;
5525
5526 /* For local watchpoint expressions, which particular
5527 instance of a local is being watched matters, so we
5528 keep track of the frame to evaluate the expression
5529 in. To evaluate the condition however, it doesn't
5530 really matter which instantiation of the function
5531 where the condition makes sense triggers the
5532 watchpoint. This allows an expression like "watch
5533 global if q > 10" set in `func', catch writes to
5534 global on all threads that call `func', or catch
5535 writes on all recursive calls of `func' by a single
5536 thread. We simply always evaluate the condition in
5537 the innermost frame that's executing where it makes
5538 sense to evaluate the condition. It seems
5539 intuitive. */
5540 frame = block_innermost_frame (w->cond_exp_valid_block);
5541 if (frame != NULL)
5542 select_frame (frame);
5543 else
5544 within_current_scope = 0;
18a18393 5545 }
7d4df6a4
DE
5546 if (within_current_scope)
5547 value_is_zero
5548 = catch_errors (breakpoint_cond_eval, cond,
5549 "Error in testing breakpoint condition:\n",
5550 RETURN_MASK_ALL);
5551 else
18a18393 5552 {
7d4df6a4
DE
5553 warning (_("Watchpoint condition cannot be tested "
5554 "in the current scope"));
5555 /* If we failed to set the right context for this
5556 watchpoint, unconditionally report it. */
5557 value_is_zero = 0;
18a18393 5558 }
7d4df6a4
DE
5559 /* FIXME-someday, should give breakpoint #. */
5560 value_free_to_mark (mark);
18a18393 5561 }
7d4df6a4
DE
5562
5563 if (cond && value_is_zero)
5564 {
5565 bs->stop = 0;
5566 }
7d4df6a4
DE
5567 else if (b->ignore_count > 0)
5568 {
5569 b->ignore_count--;
5570 bs->stop = 0;
5571 /* Increase the hit count even though we don't stop. */
5572 ++(b->hit_count);
5573 observer_notify_breakpoint_modified (b);
5574 }
18a18393
VP
5575}
5576
1cf4d951
PA
5577/* Returns true if we need to track moribund locations of LOC's type
5578 on the current target. */
5579
5580static int
5581need_moribund_for_location_type (struct bp_location *loc)
5582{
5583 return ((loc->loc_type == bp_loc_software_breakpoint
5584 && !target_supports_stopped_by_sw_breakpoint ())
5585 || (loc->loc_type == bp_loc_hardware_breakpoint
5586 && !target_supports_stopped_by_hw_breakpoint ()));
5587}
5588
18a18393 5589
9709f61c 5590/* Get a bpstat associated with having just stopped at address
d983da9c 5591 BP_ADDR in thread PTID.
c906108c 5592
d983da9c 5593 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5594 don't understand this stop. Result is a chain of bpstat's such
5595 that:
c906108c 5596
c5aa993b 5597 if we don't understand the stop, the result is a null pointer.
c906108c 5598
c5aa993b 5599 if we understand why we stopped, the result is not null.
c906108c 5600
c5aa993b
JM
5601 Each element of the chain refers to a particular breakpoint or
5602 watchpoint at which we have stopped. (We may have stopped for
5603 several reasons concurrently.)
c906108c 5604
c5aa993b
JM
5605 Each element of the chain has valid next, breakpoint_at,
5606 commands, FIXME??? fields. */
c906108c
SS
5607
5608bpstat
6c95b8df 5609bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5610 CORE_ADDR bp_addr, ptid_t ptid,
5611 const struct target_waitstatus *ws)
c906108c 5612{
0d381245 5613 struct breakpoint *b = NULL;
afe38095 5614 struct bp_location *bl;
20874c92 5615 struct bp_location *loc;
5760d0ab
JK
5616 /* First item of allocated bpstat's. */
5617 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5618 /* Pointer to the last thing in the chain currently. */
5760d0ab 5619 bpstat bs;
20874c92 5620 int ix;
429374b8 5621 int need_remove_insert;
f431efe5 5622 int removed_any;
c906108c 5623
f431efe5
PA
5624 /* First, build the bpstat chain with locations that explain a
5625 target stop, while being careful to not set the target running,
5626 as that may invalidate locations (in particular watchpoint
5627 locations are recreated). Resuming will happen here with
5628 breakpoint conditions or watchpoint expressions that include
5629 inferior function calls. */
c5aa993b 5630
429374b8
JK
5631 ALL_BREAKPOINTS (b)
5632 {
1a853c52 5633 if (!breakpoint_enabled (b))
429374b8 5634 continue;
a5606eee 5635
429374b8
JK
5636 for (bl = b->loc; bl != NULL; bl = bl->next)
5637 {
4a64f543
MS
5638 /* For hardware watchpoints, we look only at the first
5639 location. The watchpoint_check function will work on the
5640 entire expression, not the individual locations. For
5641 read watchpoints, the watchpoints_triggered function has
5642 checked all locations already. */
429374b8
JK
5643 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5644 break;
18a18393 5645
f6592439 5646 if (!bl->enabled || bl->shlib_disabled)
429374b8 5647 continue;
c5aa993b 5648
09ac7c10 5649 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5650 continue;
c5aa993b 5651
4a64f543
MS
5652 /* Come here if it's a watchpoint, or if the break address
5653 matches. */
c5aa993b 5654
4a64f543
MS
5655 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5656 explain stop. */
c5aa993b 5657
f431efe5
PA
5658 /* Assume we stop. Should we find a watchpoint that is not
5659 actually triggered, or if the condition of the breakpoint
5660 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5661 bs->stop = 1;
5662 bs->print = 1;
d983da9c 5663
f431efe5
PA
5664 /* If this is a scope breakpoint, mark the associated
5665 watchpoint as triggered so that we will handle the
5666 out-of-scope event. We'll get to the watchpoint next
5667 iteration. */
d0fb5eae 5668 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5669 {
5670 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5671
5672 w->watchpoint_triggered = watch_triggered_yes;
5673 }
f431efe5
PA
5674 }
5675 }
5676
7c16b83e 5677 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5678 if (!target_supports_stopped_by_sw_breakpoint ()
5679 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5680 {
1cf4d951 5681 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5682 {
1cf4d951
PA
5683 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5684 && need_moribund_for_location_type (loc))
5685 {
5686 bs = bpstat_alloc (loc, &bs_link);
5687 /* For hits of moribund locations, we should just proceed. */
5688 bs->stop = 0;
5689 bs->print = 0;
5690 bs->print_it = print_it_noop;
5691 }
f431efe5
PA
5692 }
5693 }
5694
edcc5120
TT
5695 /* A bit of special processing for shlib breakpoints. We need to
5696 process solib loading here, so that the lists of loaded and
5697 unloaded libraries are correct before we handle "catch load" and
5698 "catch unload". */
5699 for (bs = bs_head; bs != NULL; bs = bs->next)
5700 {
5d268276 5701 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5702 {
5703 handle_solib_event ();
5704 break;
5705 }
5706 }
5707
f431efe5
PA
5708 /* Now go through the locations that caused the target to stop, and
5709 check whether we're interested in reporting this stop to higher
5710 layers, or whether we should resume the target transparently. */
5711
5712 removed_any = 0;
5713
5760d0ab 5714 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5715 {
5716 if (!bs->stop)
5717 continue;
5718
f431efe5 5719 b = bs->breakpoint_at;
348d480f
PA
5720 b->ops->check_status (bs);
5721 if (bs->stop)
28010a5d 5722 {
348d480f 5723 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5724
429374b8
JK
5725 if (bs->stop)
5726 {
5727 ++(b->hit_count);
8d3788bd 5728 observer_notify_breakpoint_modified (b);
c906108c 5729
4a64f543 5730 /* We will stop here. */
429374b8
JK
5731 if (b->disposition == disp_disable)
5732 {
816338b5 5733 --(b->enable_count);
1a853c52 5734 if (b->enable_count <= 0)
429374b8 5735 b->enable_state = bp_disabled;
f431efe5 5736 removed_any = 1;
429374b8
JK
5737 }
5738 if (b->silent)
5739 bs->print = 0;
5740 bs->commands = b->commands;
9add0f1b 5741 incref_counted_command_line (bs->commands);
abf85f46
JK
5742 if (command_line_is_silent (bs->commands
5743 ? bs->commands->commands : NULL))
5744 bs->print = 0;
9d6e6e84
HZ
5745
5746 b->ops->after_condition_true (bs);
429374b8
JK
5747 }
5748
348d480f 5749 }
a9b3a50f
PA
5750
5751 /* Print nothing for this entry if we don't stop or don't
5752 print. */
5753 if (!bs->stop || !bs->print)
5754 bs->print_it = print_it_noop;
429374b8 5755 }
876fa593 5756
d983da9c
DJ
5757 /* If we aren't stopping, the value of some hardware watchpoint may
5758 not have changed, but the intermediate memory locations we are
5759 watching may have. Don't bother if we're stopping; this will get
5760 done later. */
d832cb68 5761 need_remove_insert = 0;
5760d0ab
JK
5762 if (! bpstat_causes_stop (bs_head))
5763 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5764 if (!bs->stop
f431efe5
PA
5765 && bs->breakpoint_at
5766 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5767 {
3a5c3e22
PA
5768 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5769
5770 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5771 need_remove_insert = 1;
d983da9c
DJ
5772 }
5773
d832cb68 5774 if (need_remove_insert)
44702360 5775 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5776 else if (removed_any)
44702360 5777 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5778
5760d0ab 5779 return bs_head;
c906108c 5780}
628fe4e4
JK
5781
5782static void
5783handle_jit_event (void)
5784{
5785 struct frame_info *frame;
5786 struct gdbarch *gdbarch;
5787
243a9253
PA
5788 if (debug_infrun)
5789 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5790
628fe4e4
JK
5791 /* Switch terminal for any messages produced by
5792 breakpoint_re_set. */
5793 target_terminal_ours_for_output ();
5794
5795 frame = get_current_frame ();
5796 gdbarch = get_frame_arch (frame);
5797
5798 jit_event_handler (gdbarch);
5799
5800 target_terminal_inferior ();
5801}
5802
5803/* Prepare WHAT final decision for infrun. */
5804
5805/* Decide what infrun needs to do with this bpstat. */
5806
c906108c 5807struct bpstat_what
0e30163f 5808bpstat_what (bpstat bs_head)
c906108c 5809{
c906108c 5810 struct bpstat_what retval;
628fe4e4 5811 int jit_event = 0;
0e30163f 5812 bpstat bs;
c906108c 5813
628fe4e4 5814 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5815 retval.call_dummy = STOP_NONE;
186c406b 5816 retval.is_longjmp = 0;
628fe4e4 5817
0e30163f 5818 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5819 {
628fe4e4
JK
5820 /* Extract this BS's action. After processing each BS, we check
5821 if its action overrides all we've seem so far. */
5822 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 enum bptype bptype;
5824
c906108c 5825 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5826 {
5827 /* I suspect this can happen if it was a momentary
5828 breakpoint which has since been deleted. */
5829 bptype = bp_none;
5830 }
20874c92 5831 else
f431efe5 5832 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5833
5834 switch (bptype)
c906108c
SS
5835 {
5836 case bp_none:
628fe4e4 5837 break;
c906108c
SS
5838 case bp_breakpoint:
5839 case bp_hardware_breakpoint:
7c16b83e 5840 case bp_single_step:
c906108c
SS
5841 case bp_until:
5842 case bp_finish:
a9b3a50f 5843 case bp_shlib_event:
c906108c
SS
5844 if (bs->stop)
5845 {
5846 if (bs->print)
628fe4e4 5847 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5848 else
628fe4e4 5849 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5850 }
5851 else
628fe4e4 5852 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5853 break;
5854 case bp_watchpoint:
5855 case bp_hardware_watchpoint:
5856 case bp_read_watchpoint:
5857 case bp_access_watchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
628fe4e4 5861 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5862 else
628fe4e4 5863 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5864 }
5865 else
628fe4e4
JK
5866 {
5867 /* There was a watchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
c906108c
SS
5870 break;
5871 case bp_longjmp:
e2e4d78b 5872 case bp_longjmp_call_dummy:
186c406b 5873 case bp_exception:
0a39bb32
PA
5874 if (bs->stop)
5875 {
5876 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5877 retval.is_longjmp = bptype != bp_exception;
5878 }
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5881 break;
5882 case bp_longjmp_resume:
186c406b 5883 case bp_exception_resume:
0a39bb32
PA
5884 if (bs->stop)
5885 {
5886 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5887 retval.is_longjmp = bptype == bp_longjmp_resume;
5888 }
5889 else
5890 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5891 break;
5892 case bp_step_resume:
5893 if (bs->stop)
628fe4e4
JK
5894 this_action = BPSTAT_WHAT_STEP_RESUME;
5895 else
c906108c 5896 {
628fe4e4
JK
5897 /* It is for the wrong frame. */
5898 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5899 }
c906108c 5900 break;
2c03e5be
PA
5901 case bp_hp_step_resume:
5902 if (bs->stop)
5903 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5904 else
5905 {
5906 /* It is for the wrong frame. */
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 }
5909 break;
c906108c 5910 case bp_watchpoint_scope:
c4093a6a 5911 case bp_thread_event:
1900040c 5912 case bp_overlay_event:
0fd8e87f 5913 case bp_longjmp_master:
aa7d318d 5914 case bp_std_terminate_master:
186c406b 5915 case bp_exception_master:
628fe4e4 5916 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5917 break;
ce78b96d 5918 case bp_catchpoint:
c5aa993b
JM
5919 if (bs->stop)
5920 {
5921 if (bs->print)
628fe4e4 5922 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5923 else
628fe4e4 5924 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5925 }
5926 else
628fe4e4
JK
5927 {
5928 /* There was a catchpoint, but we're not stopping.
5929 This requires no further action. */
5930 }
5931 break;
628fe4e4
JK
5932 case bp_jit_event:
5933 jit_event = 1;
5934 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5935 break;
c906108c 5936 case bp_call_dummy:
53a5351d
JM
5937 /* Make sure the action is stop (silent or noisy),
5938 so infrun.c pops the dummy frame. */
aa7d318d 5939 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5940 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5941 break;
5942 case bp_std_terminate:
5943 /* Make sure the action is stop (silent or noisy),
5944 so infrun.c pops the dummy frame. */
aa7d318d 5945 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5946 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5947 break;
1042e4c0 5948 case bp_tracepoint:
7a697b8d 5949 case bp_fast_tracepoint:
0fb4aa4b 5950 case bp_static_tracepoint:
1042e4c0
SS
5951 /* Tracepoint hits should not be reported back to GDB, and
5952 if one got through somehow, it should have been filtered
5953 out already. */
5954 internal_error (__FILE__, __LINE__,
7a697b8d 5955 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5956 break;
5957 case bp_gnu_ifunc_resolver:
5958 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5959 this_action = BPSTAT_WHAT_SINGLE;
5960 break;
5961 case bp_gnu_ifunc_resolver_return:
5962 /* The breakpoint will be removed, execution will restart from the
5963 PC of the former breakpoint. */
5964 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5965 break;
e7e0cddf
SS
5966
5967 case bp_dprintf:
a11cfd87
HZ
5968 if (bs->stop)
5969 this_action = BPSTAT_WHAT_STOP_SILENT;
5970 else
5971 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5972 break;
5973
628fe4e4
JK
5974 default:
5975 internal_error (__FILE__, __LINE__,
5976 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5977 }
628fe4e4
JK
5978
5979 retval.main_action = max (retval.main_action, this_action);
c906108c 5980 }
628fe4e4 5981
243a9253
PA
5982 return retval;
5983}
628fe4e4 5984
243a9253
PA
5985void
5986bpstat_run_callbacks (bpstat bs_head)
5987{
5988 bpstat bs;
628fe4e4 5989
0e30163f
JK
5990 for (bs = bs_head; bs != NULL; bs = bs->next)
5991 {
5992 struct breakpoint *b = bs->breakpoint_at;
5993
5994 if (b == NULL)
5995 continue;
5996 switch (b->type)
5997 {
243a9253
PA
5998 case bp_jit_event:
5999 handle_jit_event ();
6000 break;
0e30163f
JK
6001 case bp_gnu_ifunc_resolver:
6002 gnu_ifunc_resolver_stop (b);
6003 break;
6004 case bp_gnu_ifunc_resolver_return:
6005 gnu_ifunc_resolver_return_stop (b);
6006 break;
6007 }
6008 }
c906108c
SS
6009}
6010
6011/* Nonzero if we should step constantly (e.g. watchpoints on machines
6012 without hardware support). This isn't related to a specific bpstat,
6013 just to things like whether watchpoints are set. */
6014
c5aa993b 6015int
fba45db2 6016bpstat_should_step (void)
c906108c
SS
6017{
6018 struct breakpoint *b;
cc59ec59 6019
c906108c 6020 ALL_BREAKPOINTS (b)
717a8278 6021 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6022 return 1;
c906108c
SS
6023 return 0;
6024}
6025
67822962
PA
6026int
6027bpstat_causes_stop (bpstat bs)
6028{
6029 for (; bs != NULL; bs = bs->next)
6030 if (bs->stop)
6031 return 1;
6032
6033 return 0;
6034}
6035
c906108c 6036\f
c5aa993b 6037
170b53b2
UW
6038/* Compute a string of spaces suitable to indent the next line
6039 so it starts at the position corresponding to the table column
6040 named COL_NAME in the currently active table of UIOUT. */
6041
6042static char *
6043wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6044{
6045 static char wrap_indent[80];
6046 int i, total_width, width, align;
6047 char *text;
6048
6049 total_width = 0;
6050 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6051 {
6052 if (strcmp (text, col_name) == 0)
6053 {
6054 gdb_assert (total_width < sizeof wrap_indent);
6055 memset (wrap_indent, ' ', total_width);
6056 wrap_indent[total_width] = 0;
6057
6058 return wrap_indent;
6059 }
6060
6061 total_width += width + 1;
6062 }
6063
6064 return NULL;
6065}
6066
b775012e
LM
6067/* Determine if the locations of this breakpoint will have their conditions
6068 evaluated by the target, host or a mix of both. Returns the following:
6069
6070 "host": Host evals condition.
6071 "host or target": Host or Target evals condition.
6072 "target": Target evals condition.
6073*/
6074
6075static const char *
6076bp_condition_evaluator (struct breakpoint *b)
6077{
6078 struct bp_location *bl;
6079 char host_evals = 0;
6080 char target_evals = 0;
6081
6082 if (!b)
6083 return NULL;
6084
6085 if (!is_breakpoint (b))
6086 return NULL;
6087
6088 if (gdb_evaluates_breakpoint_condition_p ()
6089 || !target_supports_evaluation_of_breakpoint_conditions ())
6090 return condition_evaluation_host;
6091
6092 for (bl = b->loc; bl; bl = bl->next)
6093 {
6094 if (bl->cond_bytecode)
6095 target_evals++;
6096 else
6097 host_evals++;
6098 }
6099
6100 if (host_evals && target_evals)
6101 return condition_evaluation_both;
6102 else if (target_evals)
6103 return condition_evaluation_target;
6104 else
6105 return condition_evaluation_host;
6106}
6107
6108/* Determine the breakpoint location's condition evaluator. This is
6109 similar to bp_condition_evaluator, but for locations. */
6110
6111static const char *
6112bp_location_condition_evaluator (struct bp_location *bl)
6113{
6114 if (bl && !is_breakpoint (bl->owner))
6115 return NULL;
6116
6117 if (gdb_evaluates_breakpoint_condition_p ()
6118 || !target_supports_evaluation_of_breakpoint_conditions ())
6119 return condition_evaluation_host;
6120
6121 if (bl && bl->cond_bytecode)
6122 return condition_evaluation_target;
6123 else
6124 return condition_evaluation_host;
6125}
6126
859825b8
JK
6127/* Print the LOC location out of the list of B->LOC locations. */
6128
170b53b2
UW
6129static void
6130print_breakpoint_location (struct breakpoint *b,
6131 struct bp_location *loc)
0d381245 6132{
79a45e25 6133 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6134 struct cleanup *old_chain = save_current_program_space ();
6135
859825b8
JK
6136 if (loc != NULL && loc->shlib_disabled)
6137 loc = NULL;
6138
6c95b8df
PA
6139 if (loc != NULL)
6140 set_current_program_space (loc->pspace);
6141
56435ebe 6142 if (b->display_canonical)
f00aae0f
KS
6143 ui_out_field_string (uiout, "what",
6144 event_location_to_string (b->location));
2f202fde 6145 else if (loc && loc->symtab)
0d381245
VP
6146 {
6147 struct symbol *sym
6148 = find_pc_sect_function (loc->address, loc->section);
6149 if (sym)
6150 {
6151 ui_out_text (uiout, "in ");
6152 ui_out_field_string (uiout, "func",
6153 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6154 ui_out_text (uiout, " ");
6155 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6156 ui_out_text (uiout, "at ");
0d381245 6157 }
05cba821
JK
6158 ui_out_field_string (uiout, "file",
6159 symtab_to_filename_for_display (loc->symtab));
0d381245 6160 ui_out_text (uiout, ":");
05cba821 6161
0d381245 6162 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6163 ui_out_field_string (uiout, "fullname",
6164 symtab_to_fullname (loc->symtab));
0d381245 6165
f8eba3c6 6166 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6167 }
859825b8 6168 else if (loc)
0d381245 6169 {
f99d8bf4
PA
6170 struct ui_file *stb = mem_fileopen ();
6171 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6172
f99d8bf4 6173 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6174 demangle, "");
0d381245 6175 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6176
6177 do_cleanups (stb_chain);
0d381245 6178 }
859825b8 6179 else
f00aae0f
KS
6180 {
6181 ui_out_field_string (uiout, "pending",
6182 event_location_to_string (b->location));
6183 /* If extra_string is available, it could be holding a condition
6184 or dprintf arguments. In either case, make sure it is printed,
6185 too, but only for non-MI streams. */
6186 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6187 {
6188 if (b->type == bp_dprintf)
6189 ui_out_text (uiout, ",");
6190 else
6191 ui_out_text (uiout, " ");
6192 ui_out_text (uiout, b->extra_string);
6193 }
6194 }
6c95b8df 6195
b775012e
LM
6196 if (loc && is_breakpoint (b)
6197 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6198 && bp_condition_evaluator (b) == condition_evaluation_both)
6199 {
6200 ui_out_text (uiout, " (");
6201 ui_out_field_string (uiout, "evaluated-by",
6202 bp_location_condition_evaluator (loc));
6203 ui_out_text (uiout, ")");
6204 }
6205
6c95b8df 6206 do_cleanups (old_chain);
0d381245
VP
6207}
6208
269b11a2
PA
6209static const char *
6210bptype_string (enum bptype type)
c906108c 6211{
c4093a6a
JM
6212 struct ep_type_description
6213 {
6214 enum bptype type;
6215 char *description;
6216 };
6217 static struct ep_type_description bptypes[] =
c906108c 6218 {
c5aa993b
JM
6219 {bp_none, "?deleted?"},
6220 {bp_breakpoint, "breakpoint"},
c906108c 6221 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6222 {bp_single_step, "sw single-step"},
c5aa993b
JM
6223 {bp_until, "until"},
6224 {bp_finish, "finish"},
6225 {bp_watchpoint, "watchpoint"},
c906108c 6226 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6227 {bp_read_watchpoint, "read watchpoint"},
6228 {bp_access_watchpoint, "acc watchpoint"},
6229 {bp_longjmp, "longjmp"},
6230 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6231 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6232 {bp_exception, "exception"},
6233 {bp_exception_resume, "exception resume"},
c5aa993b 6234 {bp_step_resume, "step resume"},
2c03e5be 6235 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6236 {bp_watchpoint_scope, "watchpoint scope"},
6237 {bp_call_dummy, "call dummy"},
aa7d318d 6238 {bp_std_terminate, "std::terminate"},
c5aa993b 6239 {bp_shlib_event, "shlib events"},
c4093a6a 6240 {bp_thread_event, "thread events"},
1900040c 6241 {bp_overlay_event, "overlay events"},
0fd8e87f 6242 {bp_longjmp_master, "longjmp master"},
aa7d318d 6243 {bp_std_terminate_master, "std::terminate master"},
186c406b 6244 {bp_exception_master, "exception master"},
ce78b96d 6245 {bp_catchpoint, "catchpoint"},
1042e4c0 6246 {bp_tracepoint, "tracepoint"},
7a697b8d 6247 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6248 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6249 {bp_dprintf, "dprintf"},
4efc6507 6250 {bp_jit_event, "jit events"},
0e30163f
JK
6251 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6252 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6253 };
269b11a2
PA
6254
6255 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6256 || ((int) type != bptypes[(int) type].type))
6257 internal_error (__FILE__, __LINE__,
6258 _("bptypes table does not describe type #%d."),
6259 (int) type);
6260
6261 return bptypes[(int) type].description;
6262}
6263
998580f1
MK
6264/* For MI, output a field named 'thread-groups' with a list as the value.
6265 For CLI, prefix the list with the string 'inf'. */
6266
6267static void
6268output_thread_groups (struct ui_out *uiout,
6269 const char *field_name,
6270 VEC(int) *inf_num,
6271 int mi_only)
6272{
752eb8b4 6273 struct cleanup *back_to;
998580f1
MK
6274 int is_mi = ui_out_is_mi_like_p (uiout);
6275 int inf;
6276 int i;
6277
6278 /* For backward compatibility, don't display inferiors in CLI unless
6279 there are several. Always display them for MI. */
6280 if (!is_mi && mi_only)
6281 return;
6282
752eb8b4
TT
6283 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6284
998580f1
MK
6285 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6286 {
6287 if (is_mi)
6288 {
6289 char mi_group[10];
6290
6291 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6292 ui_out_field_string (uiout, NULL, mi_group);
6293 }
6294 else
6295 {
6296 if (i == 0)
6297 ui_out_text (uiout, " inf ");
6298 else
6299 ui_out_text (uiout, ", ");
6300
6301 ui_out_text (uiout, plongest (inf));
6302 }
6303 }
6304
6305 do_cleanups (back_to);
6306}
6307
269b11a2
PA
6308/* Print B to gdb_stdout. */
6309
6310static void
6311print_one_breakpoint_location (struct breakpoint *b,
6312 struct bp_location *loc,
6313 int loc_number,
6314 struct bp_location **last_loc,
269b11a2
PA
6315 int allflag)
6316{
6317 struct command_line *l;
c2c6d25f 6318 static char bpenables[] = "nynny";
c906108c 6319
79a45e25 6320 struct ui_out *uiout = current_uiout;
0d381245
VP
6321 int header_of_multiple = 0;
6322 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6323 struct value_print_options opts;
6324
6325 get_user_print_options (&opts);
0d381245
VP
6326
6327 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6328 /* See comment in print_one_breakpoint concerning treatment of
6329 breakpoints with single disabled location. */
0d381245
VP
6330 if (loc == NULL
6331 && (b->loc != NULL
6332 && (b->loc->next != NULL || !b->loc->enabled)))
6333 header_of_multiple = 1;
6334 if (loc == NULL)
6335 loc = b->loc;
6336
c4093a6a
JM
6337 annotate_record ();
6338
6339 /* 1 */
6340 annotate_field (0);
0d381245
VP
6341 if (part_of_multiple)
6342 {
6343 char *formatted;
0c6773c1 6344 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6345 ui_out_field_string (uiout, "number", formatted);
6346 xfree (formatted);
6347 }
6348 else
6349 {
6350 ui_out_field_int (uiout, "number", b->number);
6351 }
c4093a6a
JM
6352
6353 /* 2 */
6354 annotate_field (1);
0d381245
VP
6355 if (part_of_multiple)
6356 ui_out_field_skip (uiout, "type");
269b11a2
PA
6357 else
6358 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6359
6360 /* 3 */
6361 annotate_field (2);
0d381245
VP
6362 if (part_of_multiple)
6363 ui_out_field_skip (uiout, "disp");
6364 else
2cec12e5 6365 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6366
c4093a6a
JM
6367
6368 /* 4 */
6369 annotate_field (3);
0d381245 6370 if (part_of_multiple)
54e52265 6371 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6372 else
4a64f543
MS
6373 ui_out_field_fmt (uiout, "enabled", "%c",
6374 bpenables[(int) b->enable_state]);
54e52265 6375 ui_out_spaces (uiout, 2);
0d381245 6376
c4093a6a
JM
6377
6378 /* 5 and 6 */
3086aeae 6379 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6380 {
4a64f543
MS
6381 /* Although the print_one can possibly print all locations,
6382 calling it here is not likely to get any nice result. So,
6383 make sure there's just one location. */
0d381245 6384 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6385 b->ops->print_one (b, last_loc);
0d381245 6386 }
3086aeae
DJ
6387 else
6388 switch (b->type)
6389 {
6390 case bp_none:
6391 internal_error (__FILE__, __LINE__,
e2e0b3e5 6392 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6393 break;
c906108c 6394
3086aeae
DJ
6395 case bp_watchpoint:
6396 case bp_hardware_watchpoint:
6397 case bp_read_watchpoint:
6398 case bp_access_watchpoint:
3a5c3e22
PA
6399 {
6400 struct watchpoint *w = (struct watchpoint *) b;
6401
6402 /* Field 4, the address, is omitted (which makes the columns
6403 not line up too nicely with the headers, but the effect
6404 is relatively readable). */
6405 if (opts.addressprint)
6406 ui_out_field_skip (uiout, "addr");
6407 annotate_field (5);
6408 ui_out_field_string (uiout, "what", w->exp_string);
6409 }
3086aeae
DJ
6410 break;
6411
3086aeae
DJ
6412 case bp_breakpoint:
6413 case bp_hardware_breakpoint:
7c16b83e 6414 case bp_single_step:
3086aeae
DJ
6415 case bp_until:
6416 case bp_finish:
6417 case bp_longjmp:
6418 case bp_longjmp_resume:
e2e4d78b 6419 case bp_longjmp_call_dummy:
186c406b
TT
6420 case bp_exception:
6421 case bp_exception_resume:
3086aeae 6422 case bp_step_resume:
2c03e5be 6423 case bp_hp_step_resume:
3086aeae
DJ
6424 case bp_watchpoint_scope:
6425 case bp_call_dummy:
aa7d318d 6426 case bp_std_terminate:
3086aeae
DJ
6427 case bp_shlib_event:
6428 case bp_thread_event:
6429 case bp_overlay_event:
0fd8e87f 6430 case bp_longjmp_master:
aa7d318d 6431 case bp_std_terminate_master:
186c406b 6432 case bp_exception_master:
1042e4c0 6433 case bp_tracepoint:
7a697b8d 6434 case bp_fast_tracepoint:
0fb4aa4b 6435 case bp_static_tracepoint:
e7e0cddf 6436 case bp_dprintf:
4efc6507 6437 case bp_jit_event:
0e30163f
JK
6438 case bp_gnu_ifunc_resolver:
6439 case bp_gnu_ifunc_resolver_return:
79a45b7d 6440 if (opts.addressprint)
3086aeae
DJ
6441 {
6442 annotate_field (4);
54e52265 6443 if (header_of_multiple)
0d381245 6444 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6445 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6446 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6447 else
5af949e3
UW
6448 ui_out_field_core_addr (uiout, "addr",
6449 loc->gdbarch, loc->address);
3086aeae
DJ
6450 }
6451 annotate_field (5);
0d381245 6452 if (!header_of_multiple)
170b53b2 6453 print_breakpoint_location (b, loc);
0d381245 6454 if (b->loc)
a6d9a66e 6455 *last_loc = b->loc;
3086aeae
DJ
6456 break;
6457 }
c906108c 6458
6c95b8df 6459
998580f1 6460 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6461 {
6462 struct inferior *inf;
998580f1
MK
6463 VEC(int) *inf_num = NULL;
6464 int mi_only = 1;
6c95b8df 6465
998580f1 6466 ALL_INFERIORS (inf)
6c95b8df
PA
6467 {
6468 if (inf->pspace == loc->pspace)
998580f1 6469 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6470 }
998580f1
MK
6471
6472 /* For backward compatibility, don't display inferiors in CLI unless
6473 there are several. Always display for MI. */
6474 if (allflag
6475 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6476 && (number_of_program_spaces () > 1
6477 || number_of_inferiors () > 1)
6478 /* LOC is for existing B, it cannot be in
6479 moribund_locations and thus having NULL OWNER. */
6480 && loc->owner->type != bp_catchpoint))
6481 mi_only = 0;
6482 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6483 VEC_free (int, inf_num);
6c95b8df
PA
6484 }
6485
4a306c9a 6486 if (!part_of_multiple)
c4093a6a 6487 {
4a306c9a
JB
6488 if (b->thread != -1)
6489 {
6490 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6491 "stop only in" line a little further down. */
4a306c9a
JB
6492 ui_out_text (uiout, " thread ");
6493 ui_out_field_int (uiout, "thread", b->thread);
6494 }
6495 else if (b->task != 0)
6496 {
6497 ui_out_text (uiout, " task ");
6498 ui_out_field_int (uiout, "task", b->task);
6499 }
c4093a6a 6500 }
f1310107 6501
8b93c638 6502 ui_out_text (uiout, "\n");
f1310107 6503
348d480f 6504 if (!part_of_multiple)
f1310107
TJB
6505 b->ops->print_one_detail (b, uiout);
6506
0d381245 6507 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6508 {
6509 annotate_field (6);
8b93c638 6510 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6511 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6512 the frame ID. */
5af949e3
UW
6513 ui_out_field_core_addr (uiout, "frame",
6514 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6515 ui_out_text (uiout, "\n");
c4093a6a
JM
6516 }
6517
28010a5d 6518 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6519 {
6520 annotate_field (7);
d77f58be 6521 if (is_tracepoint (b))
1042e4c0
SS
6522 ui_out_text (uiout, "\ttrace only if ");
6523 else
6524 ui_out_text (uiout, "\tstop only if ");
0101ce28 6525 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6526
6527 /* Print whether the target is doing the breakpoint's condition
6528 evaluation. If GDB is doing the evaluation, don't print anything. */
6529 if (is_breakpoint (b)
6530 && breakpoint_condition_evaluation_mode ()
6531 == condition_evaluation_target)
6532 {
6533 ui_out_text (uiout, " (");
6534 ui_out_field_string (uiout, "evaluated-by",
6535 bp_condition_evaluator (b));
6536 ui_out_text (uiout, " evals)");
6537 }
0101ce28
JJ
6538 ui_out_text (uiout, "\n");
6539 }
6540
0d381245 6541 if (!part_of_multiple && b->thread != -1)
c4093a6a 6542 {
4a64f543 6543 /* FIXME should make an annotation for this. */
8b93c638 6544 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6545 if (ui_out_is_mi_like_p (uiout))
6546 ui_out_field_int (uiout, "thread", b->thread);
6547 else
6548 {
6549 struct thread_info *thr = find_thread_global_id (b->thread);
6550
6551 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6552 }
8b93c638 6553 ui_out_text (uiout, "\n");
c4093a6a
JM
6554 }
6555
556ec64d
YQ
6556 if (!part_of_multiple)
6557 {
6558 if (b->hit_count)
31f56a27
YQ
6559 {
6560 /* FIXME should make an annotation for this. */
6561 if (is_catchpoint (b))
6562 ui_out_text (uiout, "\tcatchpoint");
6563 else if (is_tracepoint (b))
6564 ui_out_text (uiout, "\ttracepoint");
6565 else
6566 ui_out_text (uiout, "\tbreakpoint");
6567 ui_out_text (uiout, " already hit ");
6568 ui_out_field_int (uiout, "times", b->hit_count);
6569 if (b->hit_count == 1)
6570 ui_out_text (uiout, " time\n");
6571 else
6572 ui_out_text (uiout, " times\n");
6573 }
556ec64d
YQ
6574 else
6575 {
31f56a27
YQ
6576 /* Output the count also if it is zero, but only if this is mi. */
6577 if (ui_out_is_mi_like_p (uiout))
6578 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6579 }
6580 }
8b93c638 6581
0d381245 6582 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6583 {
6584 annotate_field (8);
8b93c638
JM
6585 ui_out_text (uiout, "\tignore next ");
6586 ui_out_field_int (uiout, "ignore", b->ignore_count);
6587 ui_out_text (uiout, " hits\n");
c4093a6a 6588 }
059fb39f 6589
816338b5
SS
6590 /* Note that an enable count of 1 corresponds to "enable once"
6591 behavior, which is reported by the combination of enablement and
6592 disposition, so we don't need to mention it here. */
6593 if (!part_of_multiple && b->enable_count > 1)
6594 {
6595 annotate_field (8);
6596 ui_out_text (uiout, "\tdisable after ");
6597 /* Tweak the wording to clarify that ignore and enable counts
6598 are distinct, and have additive effect. */
6599 if (b->ignore_count)
6600 ui_out_text (uiout, "additional ");
6601 else
6602 ui_out_text (uiout, "next ");
6603 ui_out_field_int (uiout, "enable", b->enable_count);
6604 ui_out_text (uiout, " hits\n");
6605 }
6606
f196051f
SS
6607 if (!part_of_multiple && is_tracepoint (b))
6608 {
6609 struct tracepoint *tp = (struct tracepoint *) b;
6610
6611 if (tp->traceframe_usage)
6612 {
6613 ui_out_text (uiout, "\ttrace buffer usage ");
6614 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6615 ui_out_text (uiout, " bytes\n");
6616 }
6617 }
d3ce09f5 6618
9add0f1b 6619 l = b->commands ? b->commands->commands : NULL;
059fb39f 6620 if (!part_of_multiple && l)
c4093a6a 6621 {
3b31d625
EZ
6622 struct cleanup *script_chain;
6623
c4093a6a 6624 annotate_field (9);
3b31d625 6625 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6626 print_command_lines (uiout, l, 4);
3b31d625 6627 do_cleanups (script_chain);
c4093a6a 6628 }
d24317b4 6629
d9b3f62e 6630 if (is_tracepoint (b))
1042e4c0 6631 {
d9b3f62e
PA
6632 struct tracepoint *t = (struct tracepoint *) b;
6633
6634 if (!part_of_multiple && t->pass_count)
6635 {
6636 annotate_field (10);
6637 ui_out_text (uiout, "\tpass count ");
6638 ui_out_field_int (uiout, "pass", t->pass_count);
6639 ui_out_text (uiout, " \n");
6640 }
f2a8bc8a
YQ
6641
6642 /* Don't display it when tracepoint or tracepoint location is
6643 pending. */
6644 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6645 {
6646 annotate_field (11);
6647
6648 if (ui_out_is_mi_like_p (uiout))
6649 ui_out_field_string (uiout, "installed",
6650 loc->inserted ? "y" : "n");
6651 else
6652 {
6653 if (loc->inserted)
6654 ui_out_text (uiout, "\t");
6655 else
6656 ui_out_text (uiout, "\tnot ");
6657 ui_out_text (uiout, "installed on target\n");
6658 }
6659 }
1042e4c0
SS
6660 }
6661
d24317b4
VP
6662 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6663 {
3a5c3e22
PA
6664 if (is_watchpoint (b))
6665 {
6666 struct watchpoint *w = (struct watchpoint *) b;
6667
6668 ui_out_field_string (uiout, "original-location", w->exp_string);
6669 }
f00aae0f
KS
6670 else if (b->location != NULL
6671 && event_location_to_string (b->location) != NULL)
6672 ui_out_field_string (uiout, "original-location",
6673 event_location_to_string (b->location));
d24317b4 6674 }
c4093a6a 6675}
c5aa993b 6676
0d381245
VP
6677static void
6678print_one_breakpoint (struct breakpoint *b,
4a64f543 6679 struct bp_location **last_loc,
6c95b8df 6680 int allflag)
0d381245 6681{
8d3788bd 6682 struct cleanup *bkpt_chain;
79a45e25 6683 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6684
6685 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6686
12c5a436 6687 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6688 do_cleanups (bkpt_chain);
0d381245
VP
6689
6690 /* If this breakpoint has custom print function,
6691 it's already printed. Otherwise, print individual
6692 locations, if any. */
6693 if (b->ops == NULL || b->ops->print_one == NULL)
6694 {
4a64f543
MS
6695 /* If breakpoint has a single location that is disabled, we
6696 print it as if it had several locations, since otherwise it's
6697 hard to represent "breakpoint enabled, location disabled"
6698 situation.
6699
6700 Note that while hardware watchpoints have several locations
a3be7890 6701 internally, that's not a property exposed to user. */
0d381245 6702 if (b->loc
a5606eee 6703 && !is_hardware_watchpoint (b)
8d3788bd 6704 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6705 {
6706 struct bp_location *loc;
6707 int n = 1;
8d3788bd 6708
0d381245 6709 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6710 {
6711 struct cleanup *inner2 =
6712 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6713 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6714 do_cleanups (inner2);
6715 }
0d381245
VP
6716 }
6717 }
6718}
6719
a6d9a66e
UW
6720static int
6721breakpoint_address_bits (struct breakpoint *b)
6722{
6723 int print_address_bits = 0;
6724 struct bp_location *loc;
6725
c6d81124
PA
6726 /* Software watchpoints that aren't watching memory don't have an
6727 address to print. */
6728 if (is_no_memory_software_watchpoint (b))
6729 return 0;
6730
a6d9a66e
UW
6731 for (loc = b->loc; loc; loc = loc->next)
6732 {
c7437ca6
PA
6733 int addr_bit;
6734
c7437ca6 6735 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6736 if (addr_bit > print_address_bits)
6737 print_address_bits = addr_bit;
6738 }
6739
6740 return print_address_bits;
6741}
0d381245 6742
c4093a6a
JM
6743struct captured_breakpoint_query_args
6744 {
6745 int bnum;
6746 };
c5aa993b 6747
c4093a6a 6748static int
2b65245e 6749do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6750{
9a3c8263
SM
6751 struct captured_breakpoint_query_args *args
6752 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6753 struct breakpoint *b;
a6d9a66e 6754 struct bp_location *dummy_loc = NULL;
cc59ec59 6755
c4093a6a
JM
6756 ALL_BREAKPOINTS (b)
6757 {
6758 if (args->bnum == b->number)
c5aa993b 6759 {
12c5a436 6760 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6761 return GDB_RC_OK;
c5aa993b 6762 }
c4093a6a
JM
6763 }
6764 return GDB_RC_NONE;
6765}
c5aa993b 6766
c4093a6a 6767enum gdb_rc
4a64f543
MS
6768gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6769 char **error_message)
c4093a6a
JM
6770{
6771 struct captured_breakpoint_query_args args;
cc59ec59 6772
c4093a6a
JM
6773 args.bnum = bnum;
6774 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6775 an error. */
b0b13bb4
DJ
6776 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6777 error_message, RETURN_MASK_ALL) < 0)
6778 return GDB_RC_FAIL;
6779 else
6780 return GDB_RC_OK;
c4093a6a 6781}
c5aa993b 6782
09d682a4
TT
6783/* Return true if this breakpoint was set by the user, false if it is
6784 internal or momentary. */
6785
6786int
6787user_breakpoint_p (struct breakpoint *b)
6788{
46c6471b 6789 return b->number > 0;
09d682a4
TT
6790}
6791
7f3b0473 6792/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6793 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6794 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6795 FILTER is non-NULL, call it on each breakpoint and only include the
6796 ones for which it returns non-zero. Return the total number of
6797 breakpoints listed. */
c906108c 6798
d77f58be 6799static int
e5a67952 6800breakpoint_1 (char *args, int allflag,
4a64f543 6801 int (*filter) (const struct breakpoint *))
c4093a6a 6802{
52f0bd74 6803 struct breakpoint *b;
a6d9a66e 6804 struct bp_location *last_loc = NULL;
7f3b0473 6805 int nr_printable_breakpoints;
3b31d625 6806 struct cleanup *bkpttbl_chain;
79a45b7d 6807 struct value_print_options opts;
a6d9a66e 6808 int print_address_bits = 0;
269b11a2 6809 int print_type_col_width = 14;
79a45e25 6810 struct ui_out *uiout = current_uiout;
269b11a2 6811
79a45b7d
TT
6812 get_user_print_options (&opts);
6813
4a64f543
MS
6814 /* Compute the number of rows in the table, as well as the size
6815 required for address fields. */
7f3b0473
AC
6816 nr_printable_breakpoints = 0;
6817 ALL_BREAKPOINTS (b)
e5a67952
MS
6818 {
6819 /* If we have a filter, only list the breakpoints it accepts. */
6820 if (filter && !filter (b))
6821 continue;
6822
6823 /* If we have an "args" string, it is a list of breakpoints to
6824 accept. Skip the others. */
6825 if (args != NULL && *args != '\0')
6826 {
6827 if (allflag && parse_and_eval_long (args) != b->number)
6828 continue;
6829 if (!allflag && !number_is_in_list (args, b->number))
6830 continue;
6831 }
269b11a2 6832
e5a67952
MS
6833 if (allflag || user_breakpoint_p (b))
6834 {
6835 int addr_bit, type_len;
a6d9a66e 6836
e5a67952
MS
6837 addr_bit = breakpoint_address_bits (b);
6838 if (addr_bit > print_address_bits)
6839 print_address_bits = addr_bit;
269b11a2 6840
e5a67952
MS
6841 type_len = strlen (bptype_string (b->type));
6842 if (type_len > print_type_col_width)
6843 print_type_col_width = type_len;
6844
6845 nr_printable_breakpoints++;
6846 }
6847 }
7f3b0473 6848
79a45b7d 6849 if (opts.addressprint)
3b31d625 6850 bkpttbl_chain
3e43a32a
MS
6851 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6852 nr_printable_breakpoints,
3b31d625 6853 "BreakpointTable");
8b93c638 6854 else
3b31d625 6855 bkpttbl_chain
3e43a32a
MS
6856 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6857 nr_printable_breakpoints,
3b31d625 6858 "BreakpointTable");
8b93c638 6859
7f3b0473 6860 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6861 annotate_breakpoints_headers ();
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (0);
4a64f543 6864 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6865 if (nr_printable_breakpoints > 0)
6866 annotate_field (1);
269b11a2 6867 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6868 "type", "Type"); /* 2 */
d7faa9e7
AC
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (2);
4a64f543 6871 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6872 if (nr_printable_breakpoints > 0)
6873 annotate_field (3);
54e52265 6874 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6875 if (opts.addressprint)
e5a67952
MS
6876 {
6877 if (nr_printable_breakpoints > 0)
6878 annotate_field (4);
6879 if (print_address_bits <= 32)
6880 ui_out_table_header (uiout, 10, ui_left,
6881 "addr", "Address"); /* 5 */
6882 else
6883 ui_out_table_header (uiout, 18, ui_left,
6884 "addr", "Address"); /* 5 */
6885 }
d7faa9e7
AC
6886 if (nr_printable_breakpoints > 0)
6887 annotate_field (5);
6888 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6889 ui_out_table_body (uiout);
6890 if (nr_printable_breakpoints > 0)
6891 annotate_breakpoints_table ();
7f3b0473 6892
c4093a6a 6893 ALL_BREAKPOINTS (b)
e5a67952
MS
6894 {
6895 QUIT;
6896 /* If we have a filter, only list the breakpoints it accepts. */
6897 if (filter && !filter (b))
6898 continue;
6899
6900 /* If we have an "args" string, it is a list of breakpoints to
6901 accept. Skip the others. */
6902
6903 if (args != NULL && *args != '\0')
6904 {
6905 if (allflag) /* maintenance info breakpoint */
6906 {
6907 if (parse_and_eval_long (args) != b->number)
6908 continue;
6909 }
6910 else /* all others */
6911 {
6912 if (!number_is_in_list (args, b->number))
6913 continue;
6914 }
6915 }
6916 /* We only print out user settable breakpoints unless the
6917 allflag is set. */
6918 if (allflag || user_breakpoint_p (b))
12c5a436 6919 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6920 }
6921
3b31d625 6922 do_cleanups (bkpttbl_chain);
698384cd 6923
7f3b0473 6924 if (nr_printable_breakpoints == 0)
c906108c 6925 {
4a64f543
MS
6926 /* If there's a filter, let the caller decide how to report
6927 empty list. */
d77f58be
SS
6928 if (!filter)
6929 {
e5a67952 6930 if (args == NULL || *args == '\0')
d77f58be
SS
6931 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6932 else
4a64f543 6933 ui_out_message (uiout, 0,
e5a67952
MS
6934 "No breakpoint or watchpoint matching '%s'.\n",
6935 args);
d77f58be 6936 }
c906108c
SS
6937 }
6938 else
c4093a6a 6939 {
a6d9a66e
UW
6940 if (last_loc && !server_command)
6941 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6942 }
c906108c 6943
4a64f543 6944 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6945 there have been breakpoints? */
c906108c 6946 annotate_breakpoints_table_end ();
d77f58be
SS
6947
6948 return nr_printable_breakpoints;
c906108c
SS
6949}
6950
ad443146
SS
6951/* Display the value of default-collect in a way that is generally
6952 compatible with the breakpoint list. */
6953
6954static void
6955default_collect_info (void)
6956{
79a45e25
PA
6957 struct ui_out *uiout = current_uiout;
6958
ad443146
SS
6959 /* If it has no value (which is frequently the case), say nothing; a
6960 message like "No default-collect." gets in user's face when it's
6961 not wanted. */
6962 if (!*default_collect)
6963 return;
6964
6965 /* The following phrase lines up nicely with per-tracepoint collect
6966 actions. */
6967 ui_out_text (uiout, "default collect ");
6968 ui_out_field_string (uiout, "default-collect", default_collect);
6969 ui_out_text (uiout, " \n");
6970}
6971
c906108c 6972static void
e5a67952 6973breakpoints_info (char *args, int from_tty)
c906108c 6974{
e5a67952 6975 breakpoint_1 (args, 0, NULL);
ad443146
SS
6976
6977 default_collect_info ();
d77f58be
SS
6978}
6979
6980static void
e5a67952 6981watchpoints_info (char *args, int from_tty)
d77f58be 6982{
e5a67952 6983 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6984 struct ui_out *uiout = current_uiout;
d77f58be
SS
6985
6986 if (num_printed == 0)
6987 {
e5a67952 6988 if (args == NULL || *args == '\0')
d77f58be
SS
6989 ui_out_message (uiout, 0, "No watchpoints.\n");
6990 else
e5a67952 6991 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6992 }
c906108c
SS
6993}
6994
7a292a7a 6995static void
e5a67952 6996maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6997{
e5a67952 6998 breakpoint_1 (args, 1, NULL);
ad443146
SS
6999
7000 default_collect_info ();
c906108c
SS
7001}
7002
0d381245 7003static int
714835d5 7004breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7005 struct program_space *pspace,
714835d5 7006 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7007{
7008 struct bp_location *bl = b->loc;
cc59ec59 7009
0d381245
VP
7010 for (; bl; bl = bl->next)
7011 {
6c95b8df
PA
7012 if (bl->pspace == pspace
7013 && bl->address == pc
0d381245
VP
7014 && (!overlay_debugging || bl->section == section))
7015 return 1;
7016 }
7017 return 0;
7018}
7019
672f9b60 7020/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7021 concerns with logical breakpoints, so we match program spaces, not
7022 address spaces. */
c906108c
SS
7023
7024static void
6c95b8df
PA
7025describe_other_breakpoints (struct gdbarch *gdbarch,
7026 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7027 struct obj_section *section, int thread)
c906108c 7028{
52f0bd74
AC
7029 int others = 0;
7030 struct breakpoint *b;
c906108c
SS
7031
7032 ALL_BREAKPOINTS (b)
672f9b60
KP
7033 others += (user_breakpoint_p (b)
7034 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7035 if (others > 0)
7036 {
a3f17187
AC
7037 if (others == 1)
7038 printf_filtered (_("Note: breakpoint "));
7039 else /* if (others == ???) */
7040 printf_filtered (_("Note: breakpoints "));
c906108c 7041 ALL_BREAKPOINTS (b)
672f9b60 7042 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7043 {
7044 others--;
7045 printf_filtered ("%d", b->number);
7046 if (b->thread == -1 && thread != -1)
7047 printf_filtered (" (all threads)");
7048 else if (b->thread != -1)
7049 printf_filtered (" (thread %d)", b->thread);
7050 printf_filtered ("%s%s ",
059fb39f 7051 ((b->enable_state == bp_disabled
f8eba3c6 7052 || b->enable_state == bp_call_disabled)
0d381245 7053 ? " (disabled)"
0d381245
VP
7054 : ""),
7055 (others > 1) ? ","
7056 : ((others == 1) ? " and" : ""));
7057 }
a3f17187 7058 printf_filtered (_("also set at pc "));
5af949e3 7059 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7060 printf_filtered (".\n");
7061 }
7062}
7063\f
c906108c 7064
e4f237da 7065/* Return true iff it is meaningful to use the address member of
244558af
LM
7066 BPT locations. For some breakpoint types, the locations' address members
7067 are irrelevant and it makes no sense to attempt to compare them to other
7068 addresses (or use them for any other purpose either).
e4f237da 7069
4a64f543 7070 More specifically, each of the following breakpoint types will
244558af 7071 always have a zero valued location address and we don't want to mark
4a64f543 7072 breakpoints of any of these types to be a duplicate of an actual
244558af 7073 breakpoint location at address zero:
e4f237da
KB
7074
7075 bp_watchpoint
2d134ed3
PA
7076 bp_catchpoint
7077
7078*/
e4f237da
KB
7079
7080static int
7081breakpoint_address_is_meaningful (struct breakpoint *bpt)
7082{
7083 enum bptype type = bpt->type;
7084
2d134ed3
PA
7085 return (type != bp_watchpoint && type != bp_catchpoint);
7086}
7087
7088/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7089 true if LOC1 and LOC2 represent the same watchpoint location. */
7090
7091static int
4a64f543
MS
7092watchpoint_locations_match (struct bp_location *loc1,
7093 struct bp_location *loc2)
2d134ed3 7094{
3a5c3e22
PA
7095 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7096 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7097
7098 /* Both of them must exist. */
7099 gdb_assert (w1 != NULL);
7100 gdb_assert (w2 != NULL);
2bdf28a0 7101
4a64f543
MS
7102 /* If the target can evaluate the condition expression in hardware,
7103 then we we need to insert both watchpoints even if they are at
7104 the same place. Otherwise the watchpoint will only trigger when
7105 the condition of whichever watchpoint was inserted evaluates to
7106 true, not giving a chance for GDB to check the condition of the
7107 other watchpoint. */
3a5c3e22 7108 if ((w1->cond_exp
4a64f543
MS
7109 && target_can_accel_watchpoint_condition (loc1->address,
7110 loc1->length,
0cf6dd15 7111 loc1->watchpoint_type,
3a5c3e22
PA
7112 w1->cond_exp))
7113 || (w2->cond_exp
4a64f543
MS
7114 && target_can_accel_watchpoint_condition (loc2->address,
7115 loc2->length,
0cf6dd15 7116 loc2->watchpoint_type,
3a5c3e22 7117 w2->cond_exp)))
0cf6dd15
TJB
7118 return 0;
7119
85d721b8
PA
7120 /* Note that this checks the owner's type, not the location's. In
7121 case the target does not support read watchpoints, but does
7122 support access watchpoints, we'll have bp_read_watchpoint
7123 watchpoints with hw_access locations. Those should be considered
7124 duplicates of hw_read locations. The hw_read locations will
7125 become hw_access locations later. */
2d134ed3
PA
7126 return (loc1->owner->type == loc2->owner->type
7127 && loc1->pspace->aspace == loc2->pspace->aspace
7128 && loc1->address == loc2->address
7129 && loc1->length == loc2->length);
e4f237da
KB
7130}
7131
31e77af2 7132/* See breakpoint.h. */
6c95b8df 7133
31e77af2 7134int
6c95b8df
PA
7135breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7136 struct address_space *aspace2, CORE_ADDR addr2)
7137{
f5656ead 7138 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7139 || aspace1 == aspace2)
7140 && addr1 == addr2);
7141}
7142
f1310107
TJB
7143/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7144 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7145 matches ASPACE2. On targets that have global breakpoints, the address
7146 space doesn't really matter. */
7147
7148static int
7149breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7150 int len1, struct address_space *aspace2,
7151 CORE_ADDR addr2)
7152{
f5656ead 7153 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7154 || aspace1 == aspace2)
7155 && addr2 >= addr1 && addr2 < addr1 + len1);
7156}
7157
7158/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7159 a ranged breakpoint. In most targets, a match happens only if ASPACE
7160 matches the breakpoint's address space. On targets that have global
7161 breakpoints, the address space doesn't really matter. */
7162
7163static int
7164breakpoint_location_address_match (struct bp_location *bl,
7165 struct address_space *aspace,
7166 CORE_ADDR addr)
7167{
7168 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7169 aspace, addr)
7170 || (bl->length
7171 && breakpoint_address_match_range (bl->pspace->aspace,
7172 bl->address, bl->length,
7173 aspace, addr)));
7174}
7175
d35ae833
PA
7176/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7177 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7178 match happens only if ASPACE matches the breakpoint's address
7179 space. On targets that have global breakpoints, the address space
7180 doesn't really matter. */
7181
7182static int
7183breakpoint_location_address_range_overlap (struct bp_location *bl,
7184 struct address_space *aspace,
7185 CORE_ADDR addr, int len)
7186{
7187 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7188 || bl->pspace->aspace == aspace)
7189 {
7190 int bl_len = bl->length != 0 ? bl->length : 1;
7191
7192 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7193 return 1;
7194 }
7195 return 0;
7196}
7197
1e4d1764
YQ
7198/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7199 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7200 true, otherwise returns false. */
7201
7202static int
7203tracepoint_locations_match (struct bp_location *loc1,
7204 struct bp_location *loc2)
7205{
7206 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7207 /* Since tracepoint locations are never duplicated with others', tracepoint
7208 locations at the same address of different tracepoints are regarded as
7209 different locations. */
7210 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7211 else
7212 return 0;
7213}
7214
2d134ed3
PA
7215/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7216 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7217 represent the same location. */
7218
7219static int
4a64f543
MS
7220breakpoint_locations_match (struct bp_location *loc1,
7221 struct bp_location *loc2)
2d134ed3 7222{
2bdf28a0
JK
7223 int hw_point1, hw_point2;
7224
7225 /* Both of them must not be in moribund_locations. */
7226 gdb_assert (loc1->owner != NULL);
7227 gdb_assert (loc2->owner != NULL);
7228
7229 hw_point1 = is_hardware_watchpoint (loc1->owner);
7230 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7231
7232 if (hw_point1 != hw_point2)
7233 return 0;
7234 else if (hw_point1)
7235 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7236 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7237 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7238 else
f1310107
TJB
7239 /* We compare bp_location.length in order to cover ranged breakpoints. */
7240 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7241 loc2->pspace->aspace, loc2->address)
7242 && loc1->length == loc2->length);
2d134ed3
PA
7243}
7244
76897487
KB
7245static void
7246breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7247 int bnum, int have_bnum)
7248{
f63fbe86
MS
7249 /* The longest string possibly returned by hex_string_custom
7250 is 50 chars. These must be at least that big for safety. */
7251 char astr1[64];
7252 char astr2[64];
76897487 7253
bb599908
PH
7254 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7255 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7256 if (have_bnum)
8a3fe4f8 7257 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7258 bnum, astr1, astr2);
7259 else
8a3fe4f8 7260 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7261}
7262
4a64f543
MS
7263/* Adjust a breakpoint's address to account for architectural
7264 constraints on breakpoint placement. Return the adjusted address.
7265 Note: Very few targets require this kind of adjustment. For most
7266 targets, this function is simply the identity function. */
76897487
KB
7267
7268static CORE_ADDR
a6d9a66e
UW
7269adjust_breakpoint_address (struct gdbarch *gdbarch,
7270 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7271{
a6d9a66e 7272 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7273 {
7274 /* Very few targets need any kind of breakpoint adjustment. */
7275 return bpaddr;
7276 }
88f7da05
KB
7277 else if (bptype == bp_watchpoint
7278 || bptype == bp_hardware_watchpoint
7279 || bptype == bp_read_watchpoint
7280 || bptype == bp_access_watchpoint
fe798b75 7281 || bptype == bp_catchpoint)
88f7da05
KB
7282 {
7283 /* Watchpoints and the various bp_catch_* eventpoints should not
7284 have their addresses modified. */
7285 return bpaddr;
7286 }
7c16b83e
PA
7287 else if (bptype == bp_single_step)
7288 {
7289 /* Single-step breakpoints should not have their addresses
7290 modified. If there's any architectural constrain that
7291 applies to this address, then it should have already been
7292 taken into account when the breakpoint was created in the
7293 first place. If we didn't do this, stepping through e.g.,
7294 Thumb-2 IT blocks would break. */
7295 return bpaddr;
7296 }
76897487
KB
7297 else
7298 {
7299 CORE_ADDR adjusted_bpaddr;
7300
7301 /* Some targets have architectural constraints on the placement
7302 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7303 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7304
7305 /* An adjusted breakpoint address can significantly alter
7306 a user's expectations. Print a warning if an adjustment
7307 is required. */
7308 if (adjusted_bpaddr != bpaddr)
7309 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7310
7311 return adjusted_bpaddr;
7312 }
7313}
7314
28010a5d
PA
7315void
7316init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7317 struct breakpoint *owner)
7cc221ef 7318{
7cc221ef
DJ
7319 memset (loc, 0, sizeof (*loc));
7320
348d480f
PA
7321 gdb_assert (ops != NULL);
7322
28010a5d
PA
7323 loc->ops = ops;
7324 loc->owner = owner;
511a6cd4 7325 loc->cond = NULL;
b775012e 7326 loc->cond_bytecode = NULL;
0d381245
VP
7327 loc->shlib_disabled = 0;
7328 loc->enabled = 1;
e049a4b5 7329
28010a5d 7330 switch (owner->type)
e049a4b5
DJ
7331 {
7332 case bp_breakpoint:
7c16b83e 7333 case bp_single_step:
e049a4b5
DJ
7334 case bp_until:
7335 case bp_finish:
7336 case bp_longjmp:
7337 case bp_longjmp_resume:
e2e4d78b 7338 case bp_longjmp_call_dummy:
186c406b
TT
7339 case bp_exception:
7340 case bp_exception_resume:
e049a4b5 7341 case bp_step_resume:
2c03e5be 7342 case bp_hp_step_resume:
e049a4b5
DJ
7343 case bp_watchpoint_scope:
7344 case bp_call_dummy:
aa7d318d 7345 case bp_std_terminate:
e049a4b5
DJ
7346 case bp_shlib_event:
7347 case bp_thread_event:
7348 case bp_overlay_event:
4efc6507 7349 case bp_jit_event:
0fd8e87f 7350 case bp_longjmp_master:
aa7d318d 7351 case bp_std_terminate_master:
186c406b 7352 case bp_exception_master:
0e30163f
JK
7353 case bp_gnu_ifunc_resolver:
7354 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7355 case bp_dprintf:
e049a4b5 7356 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7357 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7358 break;
7359 case bp_hardware_breakpoint:
7360 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7361 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7362 break;
7363 case bp_hardware_watchpoint:
7364 case bp_read_watchpoint:
7365 case bp_access_watchpoint:
7366 loc->loc_type = bp_loc_hardware_watchpoint;
7367 break;
7368 case bp_watchpoint:
ce78b96d 7369 case bp_catchpoint:
15c3d785
PA
7370 case bp_tracepoint:
7371 case bp_fast_tracepoint:
0fb4aa4b 7372 case bp_static_tracepoint:
e049a4b5
DJ
7373 loc->loc_type = bp_loc_other;
7374 break;
7375 default:
e2e0b3e5 7376 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7377 }
7378
f431efe5 7379 loc->refc = 1;
28010a5d
PA
7380}
7381
7382/* Allocate a struct bp_location. */
7383
7384static struct bp_location *
7385allocate_bp_location (struct breakpoint *bpt)
7386{
348d480f
PA
7387 return bpt->ops->allocate_location (bpt);
7388}
7cc221ef 7389
f431efe5
PA
7390static void
7391free_bp_location (struct bp_location *loc)
fe3f5fa8 7392{
348d480f 7393 loc->ops->dtor (loc);
fe3f5fa8
VP
7394 xfree (loc);
7395}
7396
f431efe5
PA
7397/* Increment reference count. */
7398
7399static void
7400incref_bp_location (struct bp_location *bl)
7401{
7402 ++bl->refc;
7403}
7404
7405/* Decrement reference count. If the reference count reaches 0,
7406 destroy the bp_location. Sets *BLP to NULL. */
7407
7408static void
7409decref_bp_location (struct bp_location **blp)
7410{
0807b50c
PA
7411 gdb_assert ((*blp)->refc > 0);
7412
f431efe5
PA
7413 if (--(*blp)->refc == 0)
7414 free_bp_location (*blp);
7415 *blp = NULL;
7416}
7417
346774a9 7418/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7419
346774a9
PA
7420static void
7421add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7422{
346774a9 7423 struct breakpoint *b1;
c906108c 7424
346774a9
PA
7425 /* Add this breakpoint to the end of the chain so that a list of
7426 breakpoints will come out in order of increasing numbers. */
7427
7428 b1 = breakpoint_chain;
7429 if (b1 == 0)
7430 breakpoint_chain = b;
7431 else
7432 {
7433 while (b1->next)
7434 b1 = b1->next;
7435 b1->next = b;
7436 }
7437}
7438
7439/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7440
7441static void
7442init_raw_breakpoint_without_location (struct breakpoint *b,
7443 struct gdbarch *gdbarch,
28010a5d 7444 enum bptype bptype,
c0a91b2b 7445 const struct breakpoint_ops *ops)
346774a9 7446{
c906108c 7447 memset (b, 0, sizeof (*b));
2219d63c 7448
348d480f
PA
7449 gdb_assert (ops != NULL);
7450
28010a5d 7451 b->ops = ops;
4d28f7a8 7452 b->type = bptype;
a6d9a66e 7453 b->gdbarch = gdbarch;
c906108c
SS
7454 b->language = current_language->la_language;
7455 b->input_radix = input_radix;
7456 b->thread = -1;
b5de0fa7 7457 b->enable_state = bp_enabled;
c906108c
SS
7458 b->next = 0;
7459 b->silent = 0;
7460 b->ignore_count = 0;
7461 b->commands = NULL;
818dd999 7462 b->frame_id = null_frame_id;
0d381245 7463 b->condition_not_parsed = 0;
84f4c1fe 7464 b->py_bp_object = NULL;
d0fb5eae 7465 b->related_breakpoint = b;
f00aae0f 7466 b->location = NULL;
346774a9
PA
7467}
7468
7469/* Helper to set_raw_breakpoint below. Creates a breakpoint
7470 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7471
7472static struct breakpoint *
7473set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7474 enum bptype bptype,
c0a91b2b 7475 const struct breakpoint_ops *ops)
346774a9
PA
7476{
7477 struct breakpoint *b = XNEW (struct breakpoint);
7478
348d480f 7479 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7480 add_to_breakpoint_chain (b);
0d381245
VP
7481 return b;
7482}
7483
0e30163f
JK
7484/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7485 resolutions should be made as the user specified the location explicitly
7486 enough. */
7487
0d381245 7488static void
0e30163f 7489set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7490{
2bdf28a0
JK
7491 gdb_assert (loc->owner != NULL);
7492
0d381245 7493 if (loc->owner->type == bp_breakpoint
1042e4c0 7494 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7495 || is_tracepoint (loc->owner))
0d381245 7496 {
0e30163f 7497 int is_gnu_ifunc;
2c02bd72 7498 const char *function_name;
6a3a010b 7499 CORE_ADDR func_addr;
0e30163f 7500
2c02bd72 7501 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7502 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7503
7504 if (is_gnu_ifunc && !explicit_loc)
7505 {
7506 struct breakpoint *b = loc->owner;
7507
7508 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7509 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7510 &loc->requested_address))
7511 {
7512 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7513 loc->address = adjust_breakpoint_address (loc->gdbarch,
7514 loc->requested_address,
7515 b->type);
7516 }
7517 else if (b->type == bp_breakpoint && b->loc == loc
7518 && loc->next == NULL && b->related_breakpoint == b)
7519 {
7520 /* Create only the whole new breakpoint of this type but do not
7521 mess more complicated breakpoints with multiple locations. */
7522 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7523 /* Remember the resolver's address for use by the return
7524 breakpoint. */
7525 loc->related_address = func_addr;
0e30163f
JK
7526 }
7527 }
7528
2c02bd72
DE
7529 if (function_name)
7530 loc->function_name = xstrdup (function_name);
0d381245
VP
7531 }
7532}
7533
a6d9a66e 7534/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7535struct gdbarch *
a6d9a66e
UW
7536get_sal_arch (struct symtab_and_line sal)
7537{
7538 if (sal.section)
7539 return get_objfile_arch (sal.section->objfile);
7540 if (sal.symtab)
eb822aa6 7541 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7542
7543 return NULL;
7544}
7545
346774a9
PA
7546/* Low level routine for partially initializing a breakpoint of type
7547 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7548 file name, and line number are provided by SAL.
0d381245
VP
7549
7550 It is expected that the caller will complete the initialization of
7551 the newly created breakpoint struct as well as output any status
c56053d2 7552 information regarding the creation of a new breakpoint. */
0d381245 7553
346774a9
PA
7554static void
7555init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7556 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7557 const struct breakpoint_ops *ops)
0d381245 7558{
28010a5d 7559 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7560
3742cc8b 7561 add_location_to_breakpoint (b, &sal);
0d381245 7562
6c95b8df
PA
7563 if (bptype != bp_catchpoint)
7564 gdb_assert (sal.pspace != NULL);
7565
f8eba3c6
TT
7566 /* Store the program space that was used to set the breakpoint,
7567 except for ordinary breakpoints, which are independent of the
7568 program space. */
7569 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7570 b->pspace = sal.pspace;
346774a9 7571}
c906108c 7572
346774a9
PA
7573/* set_raw_breakpoint is a low level routine for allocating and
7574 partially initializing a breakpoint of type BPTYPE. The newly
7575 created breakpoint's address, section, source file name, and line
7576 number are provided by SAL. The newly created and partially
7577 initialized breakpoint is added to the breakpoint chain and
7578 is also returned as the value of this function.
7579
7580 It is expected that the caller will complete the initialization of
7581 the newly created breakpoint struct as well as output any status
7582 information regarding the creation of a new breakpoint. In
7583 particular, set_raw_breakpoint does NOT set the breakpoint
7584 number! Care should be taken to not allow an error to occur
7585 prior to completing the initialization of the breakpoint. If this
7586 should happen, a bogus breakpoint will be left on the chain. */
7587
7588struct breakpoint *
7589set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7590 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7591 const struct breakpoint_ops *ops)
346774a9
PA
7592{
7593 struct breakpoint *b = XNEW (struct breakpoint);
7594
348d480f 7595 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7596 add_to_breakpoint_chain (b);
c906108c
SS
7597 return b;
7598}
7599
53a5351d 7600/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7601 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7602 initiated the operation. */
c906108c
SS
7603
7604void
186c406b 7605set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7606{
35df4500 7607 struct breakpoint *b, *b_tmp;
5d5658a1 7608 int thread = tp->global_num;
0fd8e87f
UW
7609
7610 /* To avoid having to rescan all objfile symbols at every step,
7611 we maintain a list of continually-inserted but always disabled
7612 longjmp "master" breakpoints. Here, we simply create momentary
7613 clones of those and enable them for the requested thread. */
35df4500 7614 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7615 if (b->pspace == current_program_space
186c406b
TT
7616 && (b->type == bp_longjmp_master
7617 || b->type == bp_exception_master))
0fd8e87f 7618 {
06edf0c0
PA
7619 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7620 struct breakpoint *clone;
cc59ec59 7621
e2e4d78b
JK
7622 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7623 after their removal. */
06edf0c0 7624 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7625 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7626 clone->thread = thread;
7627 }
186c406b
TT
7628
7629 tp->initiating_frame = frame;
c906108c
SS
7630}
7631
611c83ae 7632/* Delete all longjmp breakpoints from THREAD. */
c906108c 7633void
611c83ae 7634delete_longjmp_breakpoint (int thread)
c906108c 7635{
35df4500 7636 struct breakpoint *b, *b_tmp;
c906108c 7637
35df4500 7638 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7639 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7640 {
7641 if (b->thread == thread)
7642 delete_breakpoint (b);
7643 }
c906108c
SS
7644}
7645
f59f708a
PA
7646void
7647delete_longjmp_breakpoint_at_next_stop (int thread)
7648{
7649 struct breakpoint *b, *b_tmp;
7650
7651 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7652 if (b->type == bp_longjmp || b->type == bp_exception)
7653 {
7654 if (b->thread == thread)
7655 b->disposition = disp_del_at_next_stop;
7656 }
7657}
7658
e2e4d78b
JK
7659/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7660 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7661 pointer to any of them. Return NULL if this system cannot place longjmp
7662 breakpoints. */
7663
7664struct breakpoint *
7665set_longjmp_breakpoint_for_call_dummy (void)
7666{
7667 struct breakpoint *b, *retval = NULL;
7668
7669 ALL_BREAKPOINTS (b)
7670 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7671 {
7672 struct breakpoint *new_b;
7673
7674 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7675 &momentary_breakpoint_ops,
7676 1);
5d5658a1 7677 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7678
7679 /* Link NEW_B into the chain of RETVAL breakpoints. */
7680
7681 gdb_assert (new_b->related_breakpoint == new_b);
7682 if (retval == NULL)
7683 retval = new_b;
7684 new_b->related_breakpoint = retval;
7685 while (retval->related_breakpoint != new_b->related_breakpoint)
7686 retval = retval->related_breakpoint;
7687 retval->related_breakpoint = new_b;
7688 }
7689
7690 return retval;
7691}
7692
7693/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7694 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7695 stack.
7696
7697 You should call this function only at places where it is safe to currently
7698 unwind the whole stack. Failed stack unwind would discard live dummy
7699 frames. */
7700
7701void
b67a2c6f 7702check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7703{
7704 struct breakpoint *b, *b_tmp;
7705
7706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7707 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7708 {
7709 struct breakpoint *dummy_b = b->related_breakpoint;
7710
7711 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7712 dummy_b = dummy_b->related_breakpoint;
7713 if (dummy_b->type != bp_call_dummy
7714 || frame_find_by_id (dummy_b->frame_id) != NULL)
7715 continue;
7716
b67a2c6f 7717 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7718
7719 while (b->related_breakpoint != b)
7720 {
7721 if (b_tmp == b->related_breakpoint)
7722 b_tmp = b->related_breakpoint->next;
7723 delete_breakpoint (b->related_breakpoint);
7724 }
7725 delete_breakpoint (b);
7726 }
7727}
7728
1900040c
MS
7729void
7730enable_overlay_breakpoints (void)
7731{
52f0bd74 7732 struct breakpoint *b;
1900040c
MS
7733
7734 ALL_BREAKPOINTS (b)
7735 if (b->type == bp_overlay_event)
7736 {
7737 b->enable_state = bp_enabled;
44702360 7738 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7739 overlay_events_enabled = 1;
1900040c
MS
7740 }
7741}
7742
7743void
7744disable_overlay_breakpoints (void)
7745{
52f0bd74 7746 struct breakpoint *b;
1900040c
MS
7747
7748 ALL_BREAKPOINTS (b)
7749 if (b->type == bp_overlay_event)
7750 {
7751 b->enable_state = bp_disabled;
44702360 7752 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7753 overlay_events_enabled = 0;
1900040c
MS
7754 }
7755}
7756
aa7d318d
TT
7757/* Set an active std::terminate breakpoint for each std::terminate
7758 master breakpoint. */
7759void
7760set_std_terminate_breakpoint (void)
7761{
35df4500 7762 struct breakpoint *b, *b_tmp;
aa7d318d 7763
35df4500 7764 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7765 if (b->pspace == current_program_space
7766 && b->type == bp_std_terminate_master)
7767 {
06edf0c0 7768 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7769 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7770 }
7771}
7772
7773/* Delete all the std::terminate breakpoints. */
7774void
7775delete_std_terminate_breakpoint (void)
7776{
35df4500 7777 struct breakpoint *b, *b_tmp;
aa7d318d 7778
35df4500 7779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7780 if (b->type == bp_std_terminate)
7781 delete_breakpoint (b);
7782}
7783
c4093a6a 7784struct breakpoint *
a6d9a66e 7785create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7786{
7787 struct breakpoint *b;
c4093a6a 7788
06edf0c0
PA
7789 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7790 &internal_breakpoint_ops);
7791
b5de0fa7 7792 b->enable_state = bp_enabled;
f00aae0f 7793 /* location has to be used or breakpoint_re_set will delete me. */
305e13e6 7794 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7795
44702360 7796 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7797
c4093a6a
JM
7798 return b;
7799}
7800
0101ce28
JJ
7801struct lang_and_radix
7802 {
7803 enum language lang;
7804 int radix;
7805 };
7806
4efc6507
DE
7807/* Create a breakpoint for JIT code registration and unregistration. */
7808
7809struct breakpoint *
7810create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7811{
2a7f3dff
PA
7812 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7813 &internal_breakpoint_ops);
4efc6507 7814}
0101ce28 7815
03673fc7
PP
7816/* Remove JIT code registration and unregistration breakpoint(s). */
7817
7818void
7819remove_jit_event_breakpoints (void)
7820{
7821 struct breakpoint *b, *b_tmp;
7822
7823 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7824 if (b->type == bp_jit_event
7825 && b->loc->pspace == current_program_space)
7826 delete_breakpoint (b);
7827}
7828
cae688ec
JJ
7829void
7830remove_solib_event_breakpoints (void)
7831{
35df4500 7832 struct breakpoint *b, *b_tmp;
cae688ec 7833
35df4500 7834 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7835 if (b->type == bp_shlib_event
7836 && b->loc->pspace == current_program_space)
cae688ec
JJ
7837 delete_breakpoint (b);
7838}
7839
f37f681c
PA
7840/* See breakpoint.h. */
7841
7842void
7843remove_solib_event_breakpoints_at_next_stop (void)
7844{
7845 struct breakpoint *b, *b_tmp;
7846
7847 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7848 if (b->type == bp_shlib_event
7849 && b->loc->pspace == current_program_space)
7850 b->disposition = disp_del_at_next_stop;
7851}
7852
04086b45
PA
7853/* Helper for create_solib_event_breakpoint /
7854 create_and_insert_solib_event_breakpoint. Allows specifying which
7855 INSERT_MODE to pass through to update_global_location_list. */
7856
7857static struct breakpoint *
7858create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7859 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7860{
7861 struct breakpoint *b;
7862
06edf0c0
PA
7863 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7864 &internal_breakpoint_ops);
04086b45 7865 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7866 return b;
7867}
7868
04086b45
PA
7869struct breakpoint *
7870create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7871{
7872 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7873}
7874
f37f681c
PA
7875/* See breakpoint.h. */
7876
7877struct breakpoint *
7878create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7879{
7880 struct breakpoint *b;
7881
04086b45
PA
7882 /* Explicitly tell update_global_location_list to insert
7883 locations. */
7884 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7885 if (!b->loc->inserted)
7886 {
7887 delete_breakpoint (b);
7888 return NULL;
7889 }
7890 return b;
7891}
7892
cae688ec
JJ
7893/* Disable any breakpoints that are on code in shared libraries. Only
7894 apply to enabled breakpoints, disabled ones can just stay disabled. */
7895
7896void
cb851954 7897disable_breakpoints_in_shlibs (void)
cae688ec 7898{
876fa593 7899 struct bp_location *loc, **locp_tmp;
cae688ec 7900
876fa593 7901 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7902 {
2bdf28a0 7903 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7904 struct breakpoint *b = loc->owner;
2bdf28a0 7905
4a64f543
MS
7906 /* We apply the check to all breakpoints, including disabled for
7907 those with loc->duplicate set. This is so that when breakpoint
7908 becomes enabled, or the duplicate is removed, gdb will try to
7909 insert all breakpoints. If we don't set shlib_disabled here,
7910 we'll try to insert those breakpoints and fail. */
1042e4c0 7911 if (((b->type == bp_breakpoint)
508ccb1f 7912 || (b->type == bp_jit_event)
1042e4c0 7913 || (b->type == bp_hardware_breakpoint)
d77f58be 7914 || (is_tracepoint (b)))
6c95b8df 7915 && loc->pspace == current_program_space
0d381245 7916 && !loc->shlib_disabled
6c95b8df 7917 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7918 )
0d381245
VP
7919 {
7920 loc->shlib_disabled = 1;
7921 }
cae688ec
JJ
7922 }
7923}
7924
63644780
NB
7925/* Disable any breakpoints and tracepoints that are in SOLIB upon
7926 notification of unloaded_shlib. Only apply to enabled breakpoints,
7927 disabled ones can just stay disabled. */
84acb35a 7928
75149521 7929static void
84acb35a
JJ
7930disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7931{
876fa593 7932 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7933 int disabled_shlib_breaks = 0;
7934
c86cf029
VP
7935 /* SunOS a.out shared libraries are always mapped, so do not
7936 disable breakpoints; they will only be reported as unloaded
7937 through clear_solib when GDB discards its shared library
7938 list. See clear_solib for more information. */
7939 if (exec_bfd != NULL
7940 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7941 return;
7942
876fa593 7943 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7944 {
2bdf28a0 7945 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7946 struct breakpoint *b = loc->owner;
cc59ec59 7947
1e4d1764 7948 if (solib->pspace == loc->pspace
e2dd7057 7949 && !loc->shlib_disabled
1e4d1764
YQ
7950 && (((b->type == bp_breakpoint
7951 || b->type == bp_jit_event
7952 || b->type == bp_hardware_breakpoint)
7953 && (loc->loc_type == bp_loc_hardware_breakpoint
7954 || loc->loc_type == bp_loc_software_breakpoint))
7955 || is_tracepoint (b))
e2dd7057 7956 && solib_contains_address_p (solib, loc->address))
84acb35a 7957 {
e2dd7057
PP
7958 loc->shlib_disabled = 1;
7959 /* At this point, we cannot rely on remove_breakpoint
7960 succeeding so we must mark the breakpoint as not inserted
7961 to prevent future errors occurring in remove_breakpoints. */
7962 loc->inserted = 0;
8d3788bd
VP
7963
7964 /* This may cause duplicate notifications for the same breakpoint. */
7965 observer_notify_breakpoint_modified (b);
7966
e2dd7057
PP
7967 if (!disabled_shlib_breaks)
7968 {
7969 target_terminal_ours_for_output ();
3e43a32a
MS
7970 warning (_("Temporarily disabling breakpoints "
7971 "for unloaded shared library \"%s\""),
e2dd7057 7972 solib->so_name);
84acb35a 7973 }
e2dd7057 7974 disabled_shlib_breaks = 1;
84acb35a
JJ
7975 }
7976 }
84acb35a
JJ
7977}
7978
63644780
NB
7979/* Disable any breakpoints and tracepoints in OBJFILE upon
7980 notification of free_objfile. Only apply to enabled breakpoints,
7981 disabled ones can just stay disabled. */
7982
7983static void
7984disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7985{
7986 struct breakpoint *b;
7987
7988 if (objfile == NULL)
7989 return;
7990
d03de421
PA
7991 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7992 managed by the user with add-symbol-file/remove-symbol-file.
7993 Similarly to how breakpoints in shared libraries are handled in
7994 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7995 shlib_disabled so they end up uninserted on the next global
7996 location list update. Shared libraries not loaded by the user
7997 aren't handled here -- they're already handled in
7998 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7999 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8000 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8001 main objfile). */
8002 if ((objfile->flags & OBJF_SHARED) == 0
8003 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8004 return;
8005
8006 ALL_BREAKPOINTS (b)
8007 {
8008 struct bp_location *loc;
8009 int bp_modified = 0;
8010
8011 if (!is_breakpoint (b) && !is_tracepoint (b))
8012 continue;
8013
8014 for (loc = b->loc; loc != NULL; loc = loc->next)
8015 {
8016 CORE_ADDR loc_addr = loc->address;
8017
8018 if (loc->loc_type != bp_loc_hardware_breakpoint
8019 && loc->loc_type != bp_loc_software_breakpoint)
8020 continue;
8021
8022 if (loc->shlib_disabled != 0)
8023 continue;
8024
8025 if (objfile->pspace != loc->pspace)
8026 continue;
8027
8028 if (loc->loc_type != bp_loc_hardware_breakpoint
8029 && loc->loc_type != bp_loc_software_breakpoint)
8030 continue;
8031
8032 if (is_addr_in_objfile (loc_addr, objfile))
8033 {
8034 loc->shlib_disabled = 1;
08351840
PA
8035 /* At this point, we don't know whether the object was
8036 unmapped from the inferior or not, so leave the
8037 inserted flag alone. We'll handle failure to
8038 uninsert quietly, in case the object was indeed
8039 unmapped. */
63644780
NB
8040
8041 mark_breakpoint_location_modified (loc);
8042
8043 bp_modified = 1;
8044 }
8045 }
8046
8047 if (bp_modified)
8048 observer_notify_breakpoint_modified (b);
8049 }
8050}
8051
ce78b96d
JB
8052/* FORK & VFORK catchpoints. */
8053
e29a4733
PA
8054/* An instance of this type is used to represent a fork or vfork
8055 catchpoint. It includes a "struct breakpoint" as a kind of base
8056 class; users downcast to "struct breakpoint *" when needed. A
8057 breakpoint is really of this type iff its ops pointer points to
8058 CATCH_FORK_BREAKPOINT_OPS. */
8059
8060struct fork_catchpoint
8061{
8062 /* The base class. */
8063 struct breakpoint base;
8064
8065 /* Process id of a child process whose forking triggered this
8066 catchpoint. This field is only valid immediately after this
8067 catchpoint has triggered. */
8068 ptid_t forked_inferior_pid;
8069};
8070
4a64f543
MS
8071/* Implement the "insert" breakpoint_ops method for fork
8072 catchpoints. */
ce78b96d 8073
77b06cd7
TJB
8074static int
8075insert_catch_fork (struct bp_location *bl)
ce78b96d 8076{
dfd4cc63 8077 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8078}
8079
4a64f543
MS
8080/* Implement the "remove" breakpoint_ops method for fork
8081 catchpoints. */
ce78b96d
JB
8082
8083static int
77b06cd7 8084remove_catch_fork (struct bp_location *bl)
ce78b96d 8085{
dfd4cc63 8086 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8087}
8088
8089/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8090 catchpoints. */
8091
8092static int
f1310107 8093breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8094 struct address_space *aspace, CORE_ADDR bp_addr,
8095 const struct target_waitstatus *ws)
ce78b96d 8096{
e29a4733
PA
8097 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8098
f90263c1
TT
8099 if (ws->kind != TARGET_WAITKIND_FORKED)
8100 return 0;
8101
8102 c->forked_inferior_pid = ws->value.related_pid;
8103 return 1;
ce78b96d
JB
8104}
8105
4a64f543
MS
8106/* Implement the "print_it" breakpoint_ops method for fork
8107 catchpoints. */
ce78b96d
JB
8108
8109static enum print_stop_action
348d480f 8110print_it_catch_fork (bpstat bs)
ce78b96d 8111{
36dfb11c 8112 struct ui_out *uiout = current_uiout;
348d480f
PA
8113 struct breakpoint *b = bs->breakpoint_at;
8114 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8115
ce78b96d 8116 annotate_catchpoint (b->number);
f303dbd6 8117 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8118 if (b->disposition == disp_del)
f303dbd6 8119 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8120 else
f303dbd6 8121 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8122 if (ui_out_is_mi_like_p (uiout))
8123 {
8124 ui_out_field_string (uiout, "reason",
8125 async_reason_lookup (EXEC_ASYNC_FORK));
8126 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8127 }
8128 ui_out_field_int (uiout, "bkptno", b->number);
8129 ui_out_text (uiout, " (forked process ");
8130 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8131 ui_out_text (uiout, "), ");
ce78b96d
JB
8132 return PRINT_SRC_AND_LOC;
8133}
8134
4a64f543
MS
8135/* Implement the "print_one" breakpoint_ops method for fork
8136 catchpoints. */
ce78b96d
JB
8137
8138static void
a6d9a66e 8139print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8140{
e29a4733 8141 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8142 struct value_print_options opts;
79a45e25 8143 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8144
8145 get_user_print_options (&opts);
8146
4a64f543
MS
8147 /* Field 4, the address, is omitted (which makes the columns not
8148 line up too nicely with the headers, but the effect is relatively
8149 readable). */
79a45b7d 8150 if (opts.addressprint)
ce78b96d
JB
8151 ui_out_field_skip (uiout, "addr");
8152 annotate_field (5);
8153 ui_out_text (uiout, "fork");
e29a4733 8154 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8155 {
8156 ui_out_text (uiout, ", process ");
8157 ui_out_field_int (uiout, "what",
e29a4733 8158 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8159 ui_out_spaces (uiout, 1);
8160 }
8ac3646f
TT
8161
8162 if (ui_out_is_mi_like_p (uiout))
8163 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8164}
8165
8166/* Implement the "print_mention" breakpoint_ops method for fork
8167 catchpoints. */
8168
8169static void
8170print_mention_catch_fork (struct breakpoint *b)
8171{
8172 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8173}
8174
6149aea9
PA
8175/* Implement the "print_recreate" breakpoint_ops method for fork
8176 catchpoints. */
8177
8178static void
8179print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8180{
8181 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8182 print_recreate_thread (b, fp);
6149aea9
PA
8183}
8184
ce78b96d
JB
8185/* The breakpoint_ops structure to be used in fork catchpoints. */
8186
2060206e 8187static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8188
4a64f543
MS
8189/* Implement the "insert" breakpoint_ops method for vfork
8190 catchpoints. */
ce78b96d 8191
77b06cd7
TJB
8192static int
8193insert_catch_vfork (struct bp_location *bl)
ce78b96d 8194{
dfd4cc63 8195 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8196}
8197
4a64f543
MS
8198/* Implement the "remove" breakpoint_ops method for vfork
8199 catchpoints. */
ce78b96d
JB
8200
8201static int
77b06cd7 8202remove_catch_vfork (struct bp_location *bl)
ce78b96d 8203{
dfd4cc63 8204 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8205}
8206
8207/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8208 catchpoints. */
8209
8210static int
f1310107 8211breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8212 struct address_space *aspace, CORE_ADDR bp_addr,
8213 const struct target_waitstatus *ws)
ce78b96d 8214{
e29a4733
PA
8215 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8216
f90263c1
TT
8217 if (ws->kind != TARGET_WAITKIND_VFORKED)
8218 return 0;
8219
8220 c->forked_inferior_pid = ws->value.related_pid;
8221 return 1;
ce78b96d
JB
8222}
8223
4a64f543
MS
8224/* Implement the "print_it" breakpoint_ops method for vfork
8225 catchpoints. */
ce78b96d
JB
8226
8227static enum print_stop_action
348d480f 8228print_it_catch_vfork (bpstat bs)
ce78b96d 8229{
36dfb11c 8230 struct ui_out *uiout = current_uiout;
348d480f 8231 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8232 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8233
ce78b96d 8234 annotate_catchpoint (b->number);
f303dbd6 8235 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8236 if (b->disposition == disp_del)
f303dbd6 8237 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8238 else
f303dbd6 8239 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8240 if (ui_out_is_mi_like_p (uiout))
8241 {
8242 ui_out_field_string (uiout, "reason",
8243 async_reason_lookup (EXEC_ASYNC_VFORK));
8244 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8245 }
8246 ui_out_field_int (uiout, "bkptno", b->number);
8247 ui_out_text (uiout, " (vforked process ");
8248 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8249 ui_out_text (uiout, "), ");
ce78b96d
JB
8250 return PRINT_SRC_AND_LOC;
8251}
8252
4a64f543
MS
8253/* Implement the "print_one" breakpoint_ops method for vfork
8254 catchpoints. */
ce78b96d
JB
8255
8256static void
a6d9a66e 8257print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8258{
e29a4733 8259 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8260 struct value_print_options opts;
79a45e25 8261 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8262
8263 get_user_print_options (&opts);
4a64f543
MS
8264 /* Field 4, the address, is omitted (which makes the columns not
8265 line up too nicely with the headers, but the effect is relatively
8266 readable). */
79a45b7d 8267 if (opts.addressprint)
ce78b96d
JB
8268 ui_out_field_skip (uiout, "addr");
8269 annotate_field (5);
8270 ui_out_text (uiout, "vfork");
e29a4733 8271 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8272 {
8273 ui_out_text (uiout, ", process ");
8274 ui_out_field_int (uiout, "what",
e29a4733 8275 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8276 ui_out_spaces (uiout, 1);
8277 }
8ac3646f
TT
8278
8279 if (ui_out_is_mi_like_p (uiout))
8280 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8281}
8282
8283/* Implement the "print_mention" breakpoint_ops method for vfork
8284 catchpoints. */
8285
8286static void
8287print_mention_catch_vfork (struct breakpoint *b)
8288{
8289 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8290}
8291
6149aea9
PA
8292/* Implement the "print_recreate" breakpoint_ops method for vfork
8293 catchpoints. */
8294
8295static void
8296print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8297{
8298 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8299 print_recreate_thread (b, fp);
6149aea9
PA
8300}
8301
ce78b96d
JB
8302/* The breakpoint_ops structure to be used in vfork catchpoints. */
8303
2060206e 8304static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8305
edcc5120
TT
8306/* An instance of this type is used to represent an solib catchpoint.
8307 It includes a "struct breakpoint" as a kind of base class; users
8308 downcast to "struct breakpoint *" when needed. A breakpoint is
8309 really of this type iff its ops pointer points to
8310 CATCH_SOLIB_BREAKPOINT_OPS. */
8311
8312struct solib_catchpoint
8313{
8314 /* The base class. */
8315 struct breakpoint base;
8316
8317 /* True for "catch load", false for "catch unload". */
8318 unsigned char is_load;
8319
8320 /* Regular expression to match, if any. COMPILED is only valid when
8321 REGEX is non-NULL. */
8322 char *regex;
8323 regex_t compiled;
8324};
8325
8326static void
8327dtor_catch_solib (struct breakpoint *b)
8328{
8329 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8330
8331 if (self->regex)
8332 regfree (&self->compiled);
8333 xfree (self->regex);
8334
8335 base_breakpoint_ops.dtor (b);
8336}
8337
8338static int
8339insert_catch_solib (struct bp_location *ignore)
8340{
8341 return 0;
8342}
8343
8344static int
8345remove_catch_solib (struct bp_location *ignore)
8346{
8347 return 0;
8348}
8349
8350static int
8351breakpoint_hit_catch_solib (const struct bp_location *bl,
8352 struct address_space *aspace,
8353 CORE_ADDR bp_addr,
8354 const struct target_waitstatus *ws)
8355{
8356 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8357 struct breakpoint *other;
8358
8359 if (ws->kind == TARGET_WAITKIND_LOADED)
8360 return 1;
8361
8362 ALL_BREAKPOINTS (other)
8363 {
8364 struct bp_location *other_bl;
8365
8366 if (other == bl->owner)
8367 continue;
8368
8369 if (other->type != bp_shlib_event)
8370 continue;
8371
8372 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8373 continue;
8374
8375 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8376 {
8377 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8378 return 1;
8379 }
8380 }
8381
8382 return 0;
8383}
8384
8385static void
8386check_status_catch_solib (struct bpstats *bs)
8387{
8388 struct solib_catchpoint *self
8389 = (struct solib_catchpoint *) bs->breakpoint_at;
8390 int ix;
8391
8392 if (self->is_load)
8393 {
8394 struct so_list *iter;
8395
8396 for (ix = 0;
8397 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8398 ix, iter);
8399 ++ix)
8400 {
8401 if (!self->regex
8402 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8403 return;
8404 }
8405 }
8406 else
8407 {
8408 char *iter;
8409
8410 for (ix = 0;
8411 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8412 ix, iter);
8413 ++ix)
8414 {
8415 if (!self->regex
8416 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8417 return;
8418 }
8419 }
8420
8421 bs->stop = 0;
8422 bs->print_it = print_it_noop;
8423}
8424
8425static enum print_stop_action
8426print_it_catch_solib (bpstat bs)
8427{
8428 struct breakpoint *b = bs->breakpoint_at;
8429 struct ui_out *uiout = current_uiout;
8430
8431 annotate_catchpoint (b->number);
f303dbd6 8432 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8433 if (b->disposition == disp_del)
f303dbd6 8434 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8435 else
f303dbd6 8436 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8437 ui_out_field_int (uiout, "bkptno", b->number);
8438 ui_out_text (uiout, "\n");
8439 if (ui_out_is_mi_like_p (uiout))
8440 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8441 print_solib_event (1);
8442 return PRINT_SRC_AND_LOC;
8443}
8444
8445static void
8446print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8447{
8448 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8449 struct value_print_options opts;
8450 struct ui_out *uiout = current_uiout;
8451 char *msg;
8452
8453 get_user_print_options (&opts);
8454 /* Field 4, the address, is omitted (which makes the columns not
8455 line up too nicely with the headers, but the effect is relatively
8456 readable). */
8457 if (opts.addressprint)
8458 {
8459 annotate_field (4);
8460 ui_out_field_skip (uiout, "addr");
8461 }
8462
8463 annotate_field (5);
8464 if (self->is_load)
8465 {
8466 if (self->regex)
8467 msg = xstrprintf (_("load of library matching %s"), self->regex);
8468 else
8469 msg = xstrdup (_("load of library"));
8470 }
8471 else
8472 {
8473 if (self->regex)
8474 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8475 else
8476 msg = xstrdup (_("unload of library"));
8477 }
8478 ui_out_field_string (uiout, "what", msg);
8479 xfree (msg);
8ac3646f
TT
8480
8481 if (ui_out_is_mi_like_p (uiout))
8482 ui_out_field_string (uiout, "catch-type",
8483 self->is_load ? "load" : "unload");
edcc5120
TT
8484}
8485
8486static void
8487print_mention_catch_solib (struct breakpoint *b)
8488{
8489 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8490
8491 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8492 self->is_load ? "load" : "unload");
8493}
8494
8495static void
8496print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8497{
8498 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8499
8500 fprintf_unfiltered (fp, "%s %s",
8501 b->disposition == disp_del ? "tcatch" : "catch",
8502 self->is_load ? "load" : "unload");
8503 if (self->regex)
8504 fprintf_unfiltered (fp, " %s", self->regex);
8505 fprintf_unfiltered (fp, "\n");
8506}
8507
8508static struct breakpoint_ops catch_solib_breakpoint_ops;
8509
91985142
MG
8510/* Shared helper function (MI and CLI) for creating and installing
8511 a shared object event catchpoint. If IS_LOAD is non-zero then
8512 the events to be caught are load events, otherwise they are
8513 unload events. If IS_TEMP is non-zero the catchpoint is a
8514 temporary one. If ENABLED is non-zero the catchpoint is
8515 created in an enabled state. */
edcc5120 8516
91985142
MG
8517void
8518add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8519{
8520 struct solib_catchpoint *c;
8521 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8522 struct cleanup *cleanup;
8523
edcc5120
TT
8524 if (!arg)
8525 arg = "";
8526 arg = skip_spaces (arg);
8527
8528 c = XCNEW (struct solib_catchpoint);
8529 cleanup = make_cleanup (xfree, c);
8530
8531 if (*arg != '\0')
8532 {
8533 int errcode;
8534
8535 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8536 if (errcode != 0)
8537 {
8538 char *err = get_regcomp_error (errcode, &c->compiled);
8539
8540 make_cleanup (xfree, err);
8541 error (_("Invalid regexp (%s): %s"), err, arg);
8542 }
8543 c->regex = xstrdup (arg);
8544 }
8545
8546 c->is_load = is_load;
91985142 8547 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8548 &catch_solib_breakpoint_ops);
8549
91985142
MG
8550 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8551
edcc5120
TT
8552 discard_cleanups (cleanup);
8553 install_breakpoint (0, &c->base, 1);
8554}
8555
91985142
MG
8556/* A helper function that does all the work for "catch load" and
8557 "catch unload". */
8558
8559static void
8560catch_load_or_unload (char *arg, int from_tty, int is_load,
8561 struct cmd_list_element *command)
8562{
8563 int tempflag;
8564 const int enabled = 1;
8565
8566 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8567
8568 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8569}
8570
edcc5120
TT
8571static void
8572catch_load_command_1 (char *arg, int from_tty,
8573 struct cmd_list_element *command)
8574{
8575 catch_load_or_unload (arg, from_tty, 1, command);
8576}
8577
8578static void
8579catch_unload_command_1 (char *arg, int from_tty,
8580 struct cmd_list_element *command)
8581{
8582 catch_load_or_unload (arg, from_tty, 0, command);
8583}
8584
346774a9
PA
8585/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8586 is non-zero, then make the breakpoint temporary. If COND_STRING is
8587 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8588 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8589
ab04a2af 8590void
346774a9
PA
8591init_catchpoint (struct breakpoint *b,
8592 struct gdbarch *gdbarch, int tempflag,
8593 char *cond_string,
c0a91b2b 8594 const struct breakpoint_ops *ops)
c906108c 8595{
c5aa993b 8596 struct symtab_and_line sal;
346774a9 8597
fe39c653 8598 init_sal (&sal);
6c95b8df 8599 sal.pspace = current_program_space;
c5aa993b 8600
28010a5d 8601 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8602
1b36a34b 8603 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8604 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8605}
8606
28010a5d 8607void
3ea46bff 8608install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8609{
8610 add_to_breakpoint_chain (b);
3a5c3e22 8611 set_breakpoint_number (internal, b);
558a9d82
YQ
8612 if (is_tracepoint (b))
8613 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8614 if (!internal)
8615 mention (b);
c56053d2 8616 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8617
8618 if (update_gll)
44702360 8619 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8620}
8621
9b70b993 8622static void
a6d9a66e
UW
8623create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8624 int tempflag, char *cond_string,
c0a91b2b 8625 const struct breakpoint_ops *ops)
c906108c 8626{
e29a4733 8627 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8628
e29a4733
PA
8629 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8630
8631 c->forked_inferior_pid = null_ptid;
8632
3ea46bff 8633 install_breakpoint (0, &c->base, 1);
c906108c
SS
8634}
8635
fe798b75
JB
8636/* Exec catchpoints. */
8637
b4d90040
PA
8638/* An instance of this type is used to represent an exec catchpoint.
8639 It includes a "struct breakpoint" as a kind of base class; users
8640 downcast to "struct breakpoint *" when needed. A breakpoint is
8641 really of this type iff its ops pointer points to
8642 CATCH_EXEC_BREAKPOINT_OPS. */
8643
8644struct exec_catchpoint
8645{
8646 /* The base class. */
8647 struct breakpoint base;
8648
8649 /* Filename of a program whose exec triggered this catchpoint.
8650 This field is only valid immediately after this catchpoint has
8651 triggered. */
8652 char *exec_pathname;
8653};
8654
8655/* Implement the "dtor" breakpoint_ops method for exec
8656 catchpoints. */
8657
8658static void
8659dtor_catch_exec (struct breakpoint *b)
8660{
8661 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8662
8663 xfree (c->exec_pathname);
348d480f 8664
2060206e 8665 base_breakpoint_ops.dtor (b);
b4d90040
PA
8666}
8667
77b06cd7
TJB
8668static int
8669insert_catch_exec (struct bp_location *bl)
c906108c 8670{
dfd4cc63 8671 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8672}
c906108c 8673
fe798b75 8674static int
77b06cd7 8675remove_catch_exec (struct bp_location *bl)
fe798b75 8676{
dfd4cc63 8677 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8678}
c906108c 8679
fe798b75 8680static int
f1310107 8681breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8682 struct address_space *aspace, CORE_ADDR bp_addr,
8683 const struct target_waitstatus *ws)
fe798b75 8684{
b4d90040
PA
8685 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8686
f90263c1
TT
8687 if (ws->kind != TARGET_WAITKIND_EXECD)
8688 return 0;
8689
8690 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8691 return 1;
fe798b75 8692}
c906108c 8693
fe798b75 8694static enum print_stop_action
348d480f 8695print_it_catch_exec (bpstat bs)
fe798b75 8696{
36dfb11c 8697 struct ui_out *uiout = current_uiout;
348d480f 8698 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8699 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8700
fe798b75 8701 annotate_catchpoint (b->number);
f303dbd6 8702 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8703 if (b->disposition == disp_del)
f303dbd6 8704 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8705 else
f303dbd6 8706 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8707 if (ui_out_is_mi_like_p (uiout))
8708 {
8709 ui_out_field_string (uiout, "reason",
8710 async_reason_lookup (EXEC_ASYNC_EXEC));
8711 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8712 }
8713 ui_out_field_int (uiout, "bkptno", b->number);
8714 ui_out_text (uiout, " (exec'd ");
8715 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8716 ui_out_text (uiout, "), ");
8717
fe798b75 8718 return PRINT_SRC_AND_LOC;
c906108c
SS
8719}
8720
fe798b75 8721static void
a6d9a66e 8722print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8723{
b4d90040 8724 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8725 struct value_print_options opts;
79a45e25 8726 struct ui_out *uiout = current_uiout;
fe798b75
JB
8727
8728 get_user_print_options (&opts);
8729
8730 /* Field 4, the address, is omitted (which makes the columns
8731 not line up too nicely with the headers, but the effect
8732 is relatively readable). */
8733 if (opts.addressprint)
8734 ui_out_field_skip (uiout, "addr");
8735 annotate_field (5);
8736 ui_out_text (uiout, "exec");
b4d90040 8737 if (c->exec_pathname != NULL)
fe798b75
JB
8738 {
8739 ui_out_text (uiout, ", program \"");
b4d90040 8740 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8741 ui_out_text (uiout, "\" ");
8742 }
8ac3646f
TT
8743
8744 if (ui_out_is_mi_like_p (uiout))
8745 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8746}
8747
8748static void
8749print_mention_catch_exec (struct breakpoint *b)
8750{
8751 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8752}
8753
6149aea9
PA
8754/* Implement the "print_recreate" breakpoint_ops method for exec
8755 catchpoints. */
8756
8757static void
8758print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8759{
8760 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8761 print_recreate_thread (b, fp);
6149aea9
PA
8762}
8763
2060206e 8764static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8765
c906108c 8766static int
fba45db2 8767hw_breakpoint_used_count (void)
c906108c 8768{
c906108c 8769 int i = 0;
f1310107
TJB
8770 struct breakpoint *b;
8771 struct bp_location *bl;
c906108c
SS
8772
8773 ALL_BREAKPOINTS (b)
c5aa993b 8774 {
d6b74ac4 8775 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8776 for (bl = b->loc; bl; bl = bl->next)
8777 {
8778 /* Special types of hardware breakpoints may use more than
8779 one register. */
348d480f 8780 i += b->ops->resources_needed (bl);
f1310107 8781 }
c5aa993b 8782 }
c906108c
SS
8783
8784 return i;
8785}
8786
a1398e0c
PA
8787/* Returns the resources B would use if it were a hardware
8788 watchpoint. */
8789
c906108c 8790static int
a1398e0c 8791hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8792{
c906108c 8793 int i = 0;
e09342b5 8794 struct bp_location *bl;
c906108c 8795
a1398e0c
PA
8796 if (!breakpoint_enabled (b))
8797 return 0;
8798
8799 for (bl = b->loc; bl; bl = bl->next)
8800 {
8801 /* Special types of hardware watchpoints may use more than
8802 one register. */
8803 i += b->ops->resources_needed (bl);
8804 }
8805
8806 return i;
8807}
8808
8809/* Returns the sum the used resources of all hardware watchpoints of
8810 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8811 the sum of the used resources of all hardware watchpoints of other
8812 types _not_ TYPE. */
8813
8814static int
8815hw_watchpoint_used_count_others (struct breakpoint *except,
8816 enum bptype type, int *other_type_used)
8817{
8818 int i = 0;
8819 struct breakpoint *b;
8820
c906108c
SS
8821 *other_type_used = 0;
8822 ALL_BREAKPOINTS (b)
e09342b5 8823 {
a1398e0c
PA
8824 if (b == except)
8825 continue;
e09342b5
TJB
8826 if (!breakpoint_enabled (b))
8827 continue;
8828
a1398e0c
PA
8829 if (b->type == type)
8830 i += hw_watchpoint_use_count (b);
8831 else if (is_hardware_watchpoint (b))
8832 *other_type_used = 1;
e09342b5
TJB
8833 }
8834
c906108c
SS
8835 return i;
8836}
8837
c906108c 8838void
fba45db2 8839disable_watchpoints_before_interactive_call_start (void)
c906108c 8840{
c5aa993b 8841 struct breakpoint *b;
c906108c
SS
8842
8843 ALL_BREAKPOINTS (b)
c5aa993b 8844 {
cc60f2e3 8845 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8846 {
b5de0fa7 8847 b->enable_state = bp_call_disabled;
44702360 8848 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8849 }
8850 }
c906108c
SS
8851}
8852
8853void
fba45db2 8854enable_watchpoints_after_interactive_call_stop (void)
c906108c 8855{
c5aa993b 8856 struct breakpoint *b;
c906108c
SS
8857
8858 ALL_BREAKPOINTS (b)
c5aa993b 8859 {
cc60f2e3 8860 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8861 {
b5de0fa7 8862 b->enable_state = bp_enabled;
44702360 8863 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8864 }
8865 }
c906108c
SS
8866}
8867
8bea4e01
UW
8868void
8869disable_breakpoints_before_startup (void)
8870{
6c95b8df 8871 current_program_space->executing_startup = 1;
44702360 8872 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8873}
8874
8875void
8876enable_breakpoints_after_startup (void)
8877{
6c95b8df 8878 current_program_space->executing_startup = 0;
f8eba3c6 8879 breakpoint_re_set ();
8bea4e01
UW
8880}
8881
7c16b83e
PA
8882/* Create a new single-step breakpoint for thread THREAD, with no
8883 locations. */
c906108c 8884
7c16b83e
PA
8885static struct breakpoint *
8886new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8887{
8888 struct breakpoint *b = XNEW (struct breakpoint);
8889
8890 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8891 &momentary_breakpoint_ops);
8892
8893 b->disposition = disp_donttouch;
8894 b->frame_id = null_frame_id;
8895
8896 b->thread = thread;
8897 gdb_assert (b->thread != 0);
8898
8899 add_to_breakpoint_chain (b);
8900
8901 return b;
8902}
8903
8904/* Set a momentary breakpoint of type TYPE at address specified by
8905 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8906 frame. */
c906108c
SS
8907
8908struct breakpoint *
a6d9a66e
UW
8909set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8910 struct frame_id frame_id, enum bptype type)
c906108c 8911{
52f0bd74 8912 struct breakpoint *b;
edb3359d 8913
193facb3
JK
8914 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8915 tail-called one. */
8916 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8917
06edf0c0 8918 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8919 b->enable_state = bp_enabled;
8920 b->disposition = disp_donttouch;
818dd999 8921 b->frame_id = frame_id;
c906108c 8922
4a64f543
MS
8923 /* If we're debugging a multi-threaded program, then we want
8924 momentary breakpoints to be active in only a single thread of
8925 control. */
39f77062 8926 if (in_thread_list (inferior_ptid))
5d5658a1 8927 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8928
44702360 8929 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8930
c906108c
SS
8931 return b;
8932}
611c83ae 8933
06edf0c0 8934/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8935 The new breakpoint will have type TYPE, use OPS as its
8936 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8937
06edf0c0
PA
8938static struct breakpoint *
8939momentary_breakpoint_from_master (struct breakpoint *orig,
8940 enum bptype type,
a1aa2221
LM
8941 const struct breakpoint_ops *ops,
8942 int loc_enabled)
e58b0e63
PA
8943{
8944 struct breakpoint *copy;
8945
06edf0c0 8946 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8947 copy->loc = allocate_bp_location (copy);
0e30163f 8948 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8949
a6d9a66e 8950 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8951 copy->loc->requested_address = orig->loc->requested_address;
8952 copy->loc->address = orig->loc->address;
8953 copy->loc->section = orig->loc->section;
6c95b8df 8954 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8955 copy->loc->probe = orig->loc->probe;
f8eba3c6 8956 copy->loc->line_number = orig->loc->line_number;
2f202fde 8957 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8958 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8959 copy->frame_id = orig->frame_id;
8960 copy->thread = orig->thread;
6c95b8df 8961 copy->pspace = orig->pspace;
e58b0e63
PA
8962
8963 copy->enable_state = bp_enabled;
8964 copy->disposition = disp_donttouch;
8965 copy->number = internal_breakpoint_number--;
8966
44702360 8967 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8968 return copy;
8969}
8970
06edf0c0
PA
8971/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8972 ORIG is NULL. */
8973
8974struct breakpoint *
8975clone_momentary_breakpoint (struct breakpoint *orig)
8976{
8977 /* If there's nothing to clone, then return nothing. */
8978 if (orig == NULL)
8979 return NULL;
8980
a1aa2221 8981 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8982}
8983
611c83ae 8984struct breakpoint *
a6d9a66e
UW
8985set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8986 enum bptype type)
611c83ae
PA
8987{
8988 struct symtab_and_line sal;
8989
8990 sal = find_pc_line (pc, 0);
8991 sal.pc = pc;
8992 sal.section = find_pc_overlay (pc);
8993 sal.explicit_pc = 1;
8994
a6d9a66e 8995 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8996}
c906108c 8997\f
c5aa993b 8998
c906108c
SS
8999/* Tell the user we have just set a breakpoint B. */
9000
9001static void
fba45db2 9002mention (struct breakpoint *b)
c906108c 9003{
348d480f 9004 b->ops->print_mention (b);
79a45e25 9005 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9006 return;
c906108c
SS
9007 printf_filtered ("\n");
9008}
c906108c 9009\f
c5aa993b 9010
1a853c52
PA
9011static int bp_loc_is_permanent (struct bp_location *loc);
9012
0d381245 9013static struct bp_location *
39d61571 9014add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9015 const struct symtab_and_line *sal)
9016{
9017 struct bp_location *loc, **tmp;
3742cc8b
YQ
9018 CORE_ADDR adjusted_address;
9019 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9020
9021 if (loc_gdbarch == NULL)
9022 loc_gdbarch = b->gdbarch;
9023
9024 /* Adjust the breakpoint's address prior to allocating a location.
9025 Once we call allocate_bp_location(), that mostly uninitialized
9026 location will be placed on the location chain. Adjustment of the
9027 breakpoint may cause target_read_memory() to be called and we do
9028 not want its scan of the location chain to find a breakpoint and
9029 location that's only been partially initialized. */
9030 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9031 sal->pc, b->type);
0d381245 9032
d30113d4 9033 /* Sort the locations by their ADDRESS. */
39d61571 9034 loc = allocate_bp_location (b);
d30113d4
JK
9035 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9036 tmp = &((*tmp)->next))
0d381245 9037 ;
d30113d4 9038 loc->next = *tmp;
0d381245 9039 *tmp = loc;
3742cc8b 9040
0d381245 9041 loc->requested_address = sal->pc;
3742cc8b 9042 loc->address = adjusted_address;
6c95b8df 9043 loc->pspace = sal->pspace;
729662a5
TT
9044 loc->probe.probe = sal->probe;
9045 loc->probe.objfile = sal->objfile;
6c95b8df 9046 gdb_assert (loc->pspace != NULL);
0d381245 9047 loc->section = sal->section;
3742cc8b 9048 loc->gdbarch = loc_gdbarch;
f8eba3c6 9049 loc->line_number = sal->line;
2f202fde 9050 loc->symtab = sal->symtab;
f8eba3c6 9051
0e30163f
JK
9052 set_breakpoint_location_function (loc,
9053 sal->explicit_pc || sal->explicit_line);
1a853c52 9054
6ae88661
LM
9055 /* While by definition, permanent breakpoints are already present in the
9056 code, we don't mark the location as inserted. Normally one would expect
9057 that GDB could rely on that breakpoint instruction to stop the program,
9058 thus removing the need to insert its own breakpoint, except that executing
9059 the breakpoint instruction can kill the target instead of reporting a
9060 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9061 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9062 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9063 breakpoint be inserted normally results in QEMU knowing about the GDB
9064 breakpoint, and thus trap before the breakpoint instruction is executed.
9065 (If GDB later needs to continue execution past the permanent breakpoint,
9066 it manually increments the PC, thus avoiding executing the breakpoint
9067 instruction.) */
1a853c52 9068 if (bp_loc_is_permanent (loc))
6ae88661 9069 loc->permanent = 1;
1a853c52 9070
0d381245
VP
9071 return loc;
9072}
514f746b
AR
9073\f
9074
1cf4d951 9075/* See breakpoint.h. */
514f746b 9076
1cf4d951
PA
9077int
9078program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9079{
9080 int len;
9081 CORE_ADDR addr;
1afeeb75 9082 const gdb_byte *bpoint;
514f746b 9083 gdb_byte *target_mem;
939c61fa
JK
9084 struct cleanup *cleanup;
9085 int retval = 0;
514f746b 9086
1cf4d951
PA
9087 addr = address;
9088 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9089
9090 /* Software breakpoints unsupported? */
9091 if (bpoint == NULL)
9092 return 0;
9093
224c3ddb 9094 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9095
9096 /* Enable the automatic memory restoration from breakpoints while
9097 we read the memory. Otherwise we could say about our temporary
9098 breakpoints they are permanent. */
9099 cleanup = make_show_memory_breakpoints_cleanup (0);
9100
9101 if (target_read_memory (address, target_mem, len) == 0
9102 && memcmp (target_mem, bpoint, len) == 0)
9103 retval = 1;
9104
9105 do_cleanups (cleanup);
9106
9107 return retval;
9108}
9109
9110/* Return 1 if LOC is pointing to a permanent breakpoint,
9111 return 0 otherwise. */
9112
9113static int
9114bp_loc_is_permanent (struct bp_location *loc)
9115{
9116 struct cleanup *cleanup;
9117 int retval;
9118
514f746b
AR
9119 gdb_assert (loc != NULL);
9120
244558af
LM
9121 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9122 attempt to read from the addresses the locations of these breakpoint types
9123 point to. program_breakpoint_here_p, below, will attempt to read
9124 memory. */
9125 if (!breakpoint_address_is_meaningful (loc->owner))
9126 return 0;
9127
6c95b8df 9128 cleanup = save_current_space_and_thread ();
6c95b8df 9129 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9130
1cf4d951 9131 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9132
939c61fa
JK
9133 do_cleanups (cleanup);
9134
9135 return retval;
514f746b
AR
9136}
9137
e7e0cddf
SS
9138/* Build a command list for the dprintf corresponding to the current
9139 settings of the dprintf style options. */
9140
9141static void
9142update_dprintf_command_list (struct breakpoint *b)
9143{
9144 char *dprintf_args = b->extra_string;
9145 char *printf_line = NULL;
9146
9147 if (!dprintf_args)
9148 return;
9149
9150 dprintf_args = skip_spaces (dprintf_args);
9151
9152 /* Allow a comma, as it may have terminated a location, but don't
9153 insist on it. */
9154 if (*dprintf_args == ',')
9155 ++dprintf_args;
9156 dprintf_args = skip_spaces (dprintf_args);
9157
9158 if (*dprintf_args != '"')
9159 error (_("Bad format string, missing '\"'."));
9160
d3ce09f5 9161 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9162 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9163 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9164 {
9165 if (!dprintf_function)
9166 error (_("No function supplied for dprintf call"));
9167
9168 if (dprintf_channel && strlen (dprintf_channel) > 0)
9169 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9170 dprintf_function,
9171 dprintf_channel,
9172 dprintf_args);
9173 else
9174 printf_line = xstrprintf ("call (void) %s (%s)",
9175 dprintf_function,
9176 dprintf_args);
9177 }
d3ce09f5
SS
9178 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9179 {
9180 if (target_can_run_breakpoint_commands ())
9181 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9182 else
9183 {
9184 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9185 printf_line = xstrprintf ("printf %s", dprintf_args);
9186 }
9187 }
e7e0cddf
SS
9188 else
9189 internal_error (__FILE__, __LINE__,
9190 _("Invalid dprintf style."));
9191
f28045c2 9192 gdb_assert (printf_line != NULL);
9d6e6e84 9193 /* Manufacture a printf sequence. */
f28045c2 9194 {
8d749320 9195 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9196
f28045c2
YQ
9197 printf_cmd_line->control_type = simple_control;
9198 printf_cmd_line->body_count = 0;
9199 printf_cmd_line->body_list = NULL;
9d6e6e84 9200 printf_cmd_line->next = NULL;
f28045c2 9201 printf_cmd_line->line = printf_line;
e7e0cddf 9202
f28045c2
YQ
9203 breakpoint_set_commands (b, printf_cmd_line);
9204 }
e7e0cddf
SS
9205}
9206
9207/* Update all dprintf commands, making their command lists reflect
9208 current style settings. */
9209
9210static void
9211update_dprintf_commands (char *args, int from_tty,
9212 struct cmd_list_element *c)
9213{
9214 struct breakpoint *b;
9215
9216 ALL_BREAKPOINTS (b)
9217 {
9218 if (b->type == bp_dprintf)
9219 update_dprintf_command_list (b);
9220 }
9221}
c3f6f71d 9222
f00aae0f
KS
9223/* Create a breakpoint with SAL as location. Use LOCATION
9224 as a description of the location, and COND_STRING
b35a8b2f
DE
9225 as condition expression. If LOCATION is NULL then create an
9226 "address location" from the address in the SAL. */
018d34a4
VP
9227
9228static void
d9b3f62e 9229init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9230 struct symtabs_and_lines sals,
9231 struct event_location *location,
f8eba3c6 9232 char *filter, char *cond_string,
e7e0cddf 9233 char *extra_string,
d9b3f62e
PA
9234 enum bptype type, enum bpdisp disposition,
9235 int thread, int task, int ignore_count,
c0a91b2b 9236 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9237 int enabled, int internal, unsigned flags,
9238 int display_canonical)
018d34a4 9239{
0d381245 9240 int i;
018d34a4
VP
9241
9242 if (type == bp_hardware_breakpoint)
9243 {
fbbd034e
AS
9244 int target_resources_ok;
9245
9246 i = hw_breakpoint_used_count ();
9247 target_resources_ok =
9248 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9249 i + 1, 0);
9250 if (target_resources_ok == 0)
9251 error (_("No hardware breakpoint support in the target."));
9252 else if (target_resources_ok < 0)
9253 error (_("Hardware breakpoints used exceeds limit."));
9254 }
9255
6c95b8df
PA
9256 gdb_assert (sals.nelts > 0);
9257
0d381245
VP
9258 for (i = 0; i < sals.nelts; ++i)
9259 {
9260 struct symtab_and_line sal = sals.sals[i];
9261 struct bp_location *loc;
9262
9263 if (from_tty)
5af949e3
UW
9264 {
9265 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9266 if (!loc_gdbarch)
9267 loc_gdbarch = gdbarch;
9268
9269 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9270 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9271 }
0d381245
VP
9272
9273 if (i == 0)
9274 {
d9b3f62e 9275 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9276 b->thread = thread;
4a306c9a 9277 b->task = task;
855a6e68 9278
0d381245 9279 b->cond_string = cond_string;
e7e0cddf 9280 b->extra_string = extra_string;
0d381245 9281 b->ignore_count = ignore_count;
41447f92 9282 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9283 b->disposition = disposition;
6c95b8df 9284
44f238bb
PA
9285 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9286 b->loc->inserted = 1;
9287
0fb4aa4b
PA
9288 if (type == bp_static_tracepoint)
9289 {
d9b3f62e 9290 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9291 struct static_tracepoint_marker marker;
9292
983af33b 9293 if (strace_marker_p (b))
0fb4aa4b
PA
9294 {
9295 /* We already know the marker exists, otherwise, we
9296 wouldn't see a sal for it. */
f00aae0f
KS
9297 const char *p = &event_location_to_string (b->location)[3];
9298 const char *endp;
0fb4aa4b 9299 char *marker_str;
0fb4aa4b 9300
f00aae0f 9301 p = skip_spaces_const (p);
0fb4aa4b 9302
f00aae0f 9303 endp = skip_to_space_const (p);
0fb4aa4b
PA
9304
9305 marker_str = savestring (p, endp - p);
d9b3f62e 9306 t->static_trace_marker_id = marker_str;
0fb4aa4b 9307
3e43a32a
MS
9308 printf_filtered (_("Probed static tracepoint "
9309 "marker \"%s\"\n"),
d9b3f62e 9310 t->static_trace_marker_id);
0fb4aa4b
PA
9311 }
9312 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9313 {
d9b3f62e 9314 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9315 release_static_tracepoint_marker (&marker);
9316
3e43a32a
MS
9317 printf_filtered (_("Probed static tracepoint "
9318 "marker \"%s\"\n"),
d9b3f62e 9319 t->static_trace_marker_id);
0fb4aa4b
PA
9320 }
9321 else
3e43a32a
MS
9322 warning (_("Couldn't determine the static "
9323 "tracepoint marker to probe"));
0fb4aa4b
PA
9324 }
9325
0d381245
VP
9326 loc = b->loc;
9327 }
9328 else
018d34a4 9329 {
39d61571 9330 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9331 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9332 loc->inserted = 1;
0d381245
VP
9333 }
9334
9335 if (b->cond_string)
9336 {
bbc13ae3
KS
9337 const char *arg = b->cond_string;
9338
1bb9788d
TT
9339 loc->cond = parse_exp_1 (&arg, loc->address,
9340 block_for_pc (loc->address), 0);
0d381245 9341 if (*arg)
588ae58c 9342 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9343 }
e7e0cddf
SS
9344
9345 /* Dynamic printf requires and uses additional arguments on the
9346 command line, otherwise it's an error. */
9347 if (type == bp_dprintf)
9348 {
9349 if (b->extra_string)
9350 update_dprintf_command_list (b);
9351 else
9352 error (_("Format string required"));
9353 }
9354 else if (b->extra_string)
588ae58c 9355 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9356 }
018d34a4 9357
56435ebe 9358 b->display_canonical = display_canonical;
f00aae0f
KS
9359 if (location != NULL)
9360 b->location = location;
018d34a4 9361 else
305e13e6
JB
9362 {
9363 const char *addr_string = NULL;
9364 int addr_string_len = 0;
9365
9366 if (location != NULL)
9367 addr_string = event_location_to_string (location);
9368 if (addr_string != NULL)
9369 addr_string_len = strlen (addr_string);
9370
9371 b->location = new_address_location (b->loc->address,
9372 addr_string, addr_string_len);
9373 }
f8eba3c6 9374 b->filter = filter;
d9b3f62e 9375}
018d34a4 9376
d9b3f62e
PA
9377static void
9378create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9379 struct symtabs_and_lines sals,
9380 struct event_location *location,
f8eba3c6 9381 char *filter, char *cond_string,
e7e0cddf 9382 char *extra_string,
d9b3f62e
PA
9383 enum bptype type, enum bpdisp disposition,
9384 int thread, int task, int ignore_count,
c0a91b2b 9385 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9386 int enabled, int internal, unsigned flags,
9387 int display_canonical)
d9b3f62e
PA
9388{
9389 struct breakpoint *b;
9390 struct cleanup *old_chain;
9391
9392 if (is_tracepoint_type (type))
9393 {
9394 struct tracepoint *t;
9395
9396 t = XCNEW (struct tracepoint);
9397 b = &t->base;
9398 }
9399 else
9400 b = XNEW (struct breakpoint);
9401
9402 old_chain = make_cleanup (xfree, b);
9403
9404 init_breakpoint_sal (b, gdbarch,
f00aae0f 9405 sals, location,
e7e0cddf 9406 filter, cond_string, extra_string,
d9b3f62e
PA
9407 type, disposition,
9408 thread, task, ignore_count,
9409 ops, from_tty,
44f238bb
PA
9410 enabled, internal, flags,
9411 display_canonical);
d9b3f62e
PA
9412 discard_cleanups (old_chain);
9413
3ea46bff 9414 install_breakpoint (internal, b, 0);
018d34a4
VP
9415}
9416
9417/* Add SALS.nelts breakpoints to the breakpoint table. For each
9418 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9419 value. COND_STRING, if not NULL, specified the condition to be
9420 used for all breakpoints. Essentially the only case where
9421 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9422 function. In that case, it's still not possible to specify
9423 separate conditions for different overloaded functions, so
9424 we take just a single condition string.
9425
c3f6f71d 9426 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9427 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9428 array contents). If the function fails (error() is called), the
9429 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9430 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9431
9432static void
8cdf0e15 9433create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9434 struct linespec_result *canonical,
e7e0cddf 9435 char *cond_string, char *extra_string,
8cdf0e15
VP
9436 enum bptype type, enum bpdisp disposition,
9437 int thread, int task, int ignore_count,
c0a91b2b 9438 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9439 int enabled, int internal, unsigned flags)
c906108c 9440{
018d34a4 9441 int i;
f8eba3c6 9442 struct linespec_sals *lsal;
cc59ec59 9443
f8eba3c6
TT
9444 if (canonical->pre_expanded)
9445 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9446
9447 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9448 {
f00aae0f 9449 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9450 'break', without arguments. */
f00aae0f
KS
9451 struct event_location *location
9452 = (canonical->location != NULL
9453 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9454 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9455 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9456
f8eba3c6
TT
9457 make_cleanup (xfree, filter_string);
9458 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9459 location,
f8eba3c6 9460 filter_string,
e7e0cddf
SS
9461 cond_string, extra_string,
9462 type, disposition,
84f4c1fe 9463 thread, task, ignore_count, ops,
44f238bb 9464 from_tty, enabled, internal, flags,
56435ebe 9465 canonical->special_display);
f8eba3c6 9466 discard_cleanups (inner);
c3f6f71d 9467 }
c3f6f71d 9468}
c906108c 9469
f00aae0f 9470/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9471 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9472 addresses found. LOCATION points to the end of the SAL (for
9473 linespec locations).
9998af43
TJB
9474
9475 The array and the line spec strings are allocated on the heap, it is
9476 the caller's responsibility to free them. */
c906108c 9477
b9362cc7 9478static void
f00aae0f 9479parse_breakpoint_sals (const struct event_location *location,
58438ac1 9480 struct linespec_result *canonical)
c3f6f71d 9481{
f00aae0f
KS
9482 struct symtab_and_line cursal;
9483
9484 if (event_location_type (location) == LINESPEC_LOCATION)
9485 {
9486 const char *address = get_linespec_location (location);
9487
9488 if (address == NULL)
9489 {
9490 /* The last displayed codepoint, if it's valid, is our default
9491 breakpoint address. */
9492 if (last_displayed_sal_is_valid ())
9493 {
9494 struct linespec_sals lsal;
9495 struct symtab_and_line sal;
9496 CORE_ADDR pc;
9497
9498 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9499 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9500
9501 /* Set sal's pspace, pc, symtab, and line to the values
9502 corresponding to the last call to print_frame_info.
9503 Be sure to reinitialize LINE with NOTCURRENT == 0
9504 as the breakpoint line number is inappropriate otherwise.
9505 find_pc_line would adjust PC, re-set it back. */
9506 get_last_displayed_sal (&sal);
9507 pc = sal.pc;
9508 sal = find_pc_line (pc, 0);
9509
9510 /* "break" without arguments is equivalent to "break *PC"
9511 where PC is the last displayed codepoint's address. So
9512 make sure to set sal.explicit_pc to prevent GDB from
9513 trying to expand the list of sals to include all other
9514 instances with the same symtab and line. */
9515 sal.pc = pc;
9516 sal.explicit_pc = 1;
9517
9518 lsal.sals.sals[0] = sal;
9519 lsal.sals.nelts = 1;
9520 lsal.canonical = NULL;
9521
9522 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9523 return;
9524 }
9525 else
9526 error (_("No default breakpoint address now."));
c906108c 9527 }
c906108c 9528 }
f00aae0f
KS
9529
9530 /* Force almost all breakpoints to be in terms of the
9531 current_source_symtab (which is decode_line_1's default).
9532 This should produce the results we want almost all of the
9533 time while leaving default_breakpoint_* alone.
9534
9535 ObjC: However, don't match an Objective-C method name which
9536 may have a '+' or '-' succeeded by a '['. */
9537 cursal = get_current_source_symtab_and_line ();
9538 if (last_displayed_sal_is_valid ())
c906108c 9539 {
f00aae0f 9540 const char *address = NULL;
cc80f267 9541
f00aae0f
KS
9542 if (event_location_type (location) == LINESPEC_LOCATION)
9543 address = get_linespec_location (location);
cc80f267 9544
f00aae0f
KS
9545 if (!cursal.symtab
9546 || (address != NULL
9547 && strchr ("+-", address[0]) != NULL
9548 && address[1] != '['))
9549 {
c2f4122d 9550 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9551 get_last_displayed_symtab (),
9552 get_last_displayed_line (),
9553 canonical, NULL, NULL);
9554 return;
9555 }
c906108c 9556 }
f00aae0f 9557
c2f4122d 9558 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9559 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9560}
c906108c 9561
c906108c 9562
c3f6f71d 9563/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9564 inserted as a breakpoint. If it can't throw an error. */
c906108c 9565
b9362cc7 9566static void
23e7acfb 9567breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9568{
9569 int i;
cc59ec59 9570
c3f6f71d 9571 for (i = 0; i < sals->nelts; i++)
ee53e872 9572 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9573}
9574
7a697b8d
SS
9575/* Fast tracepoints may have restrictions on valid locations. For
9576 instance, a fast tracepoint using a jump instead of a trap will
9577 likely have to overwrite more bytes than a trap would, and so can
9578 only be placed where the instruction is longer than the jump, or a
9579 multi-instruction sequence does not have a jump into the middle of
9580 it, etc. */
9581
9582static void
9583check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9584 struct symtabs_and_lines *sals)
9585{
9586 int i, rslt;
9587 struct symtab_and_line *sal;
9588 char *msg;
9589 struct cleanup *old_chain;
9590
9591 for (i = 0; i < sals->nelts; i++)
9592 {
f8eba3c6
TT
9593 struct gdbarch *sarch;
9594
7a697b8d
SS
9595 sal = &sals->sals[i];
9596
f8eba3c6
TT
9597 sarch = get_sal_arch (*sal);
9598 /* We fall back to GDBARCH if there is no architecture
9599 associated with SAL. */
9600 if (sarch == NULL)
9601 sarch = gdbarch;
6b940e6a 9602 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9603 old_chain = make_cleanup (xfree, msg);
9604
9605 if (!rslt)
9606 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9607 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9608
9609 do_cleanups (old_chain);
9610 }
9611}
9612
018d34a4
VP
9613/* Given TOK, a string specification of condition and thread, as
9614 accepted by the 'break' command, extract the condition
9615 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9616 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9617 If no condition is found, *COND_STRING is set to NULL.
9618 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9619
9620static void
bbc13ae3 9621find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9622 char **cond_string, int *thread, int *task,
9623 char **rest)
018d34a4
VP
9624{
9625 *cond_string = NULL;
9626 *thread = -1;
ed1d1739
KS
9627 *task = 0;
9628 *rest = NULL;
9629
018d34a4
VP
9630 while (tok && *tok)
9631 {
bbc13ae3 9632 const char *end_tok;
018d34a4 9633 int toklen;
bbc13ae3
KS
9634 const char *cond_start = NULL;
9635 const char *cond_end = NULL;
cc59ec59 9636
bbc13ae3 9637 tok = skip_spaces_const (tok);
e7e0cddf
SS
9638
9639 if ((*tok == '"' || *tok == ',') && rest)
9640 {
9641 *rest = savestring (tok, strlen (tok));
9642 return;
9643 }
9644
bbc13ae3 9645 end_tok = skip_to_space_const (tok);
d634f2de 9646
018d34a4 9647 toklen = end_tok - tok;
d634f2de 9648
018d34a4
VP
9649 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9650 {
f7545552
TT
9651 struct expression *expr;
9652
018d34a4 9653 tok = cond_start = end_tok + 1;
1bb9788d 9654 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9655 xfree (expr);
018d34a4 9656 cond_end = tok;
d634f2de 9657 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9658 }
9659 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9660 {
5d5658a1
PA
9661 const char *tmptok;
9662 struct thread_info *thr;
d634f2de 9663
018d34a4 9664 tok = end_tok + 1;
5d5658a1 9665 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9666 if (tok == tmptok)
9667 error (_("Junk after thread keyword."));
5d5658a1 9668 *thread = thr->global_num;
bbc13ae3 9669 tok = tmptok;
018d34a4 9670 }
4a306c9a
JB
9671 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9672 {
9673 char *tmptok;
9674
9675 tok = end_tok + 1;
bbc13ae3 9676 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9677 if (tok == tmptok)
9678 error (_("Junk after task keyword."));
9679 if (!valid_task_id (*task))
b6199126 9680 error (_("Unknown task %d."), *task);
bbc13ae3 9681 tok = tmptok;
4a306c9a 9682 }
e7e0cddf
SS
9683 else if (rest)
9684 {
9685 *rest = savestring (tok, strlen (tok));
ccab2054 9686 return;
e7e0cddf 9687 }
018d34a4
VP
9688 else
9689 error (_("Junk at end of arguments."));
9690 }
9691}
9692
0fb4aa4b
PA
9693/* Decode a static tracepoint marker spec. */
9694
9695static struct symtabs_and_lines
f00aae0f 9696decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9697{
9698 VEC(static_tracepoint_marker_p) *markers = NULL;
9699 struct symtabs_and_lines sals;
0fb4aa4b 9700 struct cleanup *old_chain;
f00aae0f
KS
9701 const char *p = &(*arg_p)[3];
9702 const char *endp;
0fb4aa4b
PA
9703 char *marker_str;
9704 int i;
9705
f00aae0f 9706 p = skip_spaces_const (p);
0fb4aa4b 9707
f00aae0f 9708 endp = skip_to_space_const (p);
0fb4aa4b
PA
9709
9710 marker_str = savestring (p, endp - p);
9711 old_chain = make_cleanup (xfree, marker_str);
9712
9713 markers = target_static_tracepoint_markers_by_strid (marker_str);
9714 if (VEC_empty(static_tracepoint_marker_p, markers))
9715 error (_("No known static tracepoint marker named %s"), marker_str);
9716
9717 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9718 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9719
9720 for (i = 0; i < sals.nelts; i++)
9721 {
9722 struct static_tracepoint_marker *marker;
9723
9724 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9725
9726 init_sal (&sals.sals[i]);
9727
9728 sals.sals[i] = find_pc_line (marker->address, 0);
9729 sals.sals[i].pc = marker->address;
9730
9731 release_static_tracepoint_marker (marker);
9732 }
9733
9734 do_cleanups (old_chain);
9735
9736 *arg_p = endp;
9737 return sals;
9738}
9739
f00aae0f 9740/* See breakpoint.h. */
0101ce28 9741
8cdf0e15
VP
9742int
9743create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9744 const struct event_location *location, char *cond_string,
e7e0cddf 9745 int thread, char *extra_string,
f00aae0f 9746 int parse_extra,
0fb4aa4b 9747 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9748 int ignore_count,
9749 enum auto_boolean pending_break_support,
c0a91b2b 9750 const struct breakpoint_ops *ops,
44f238bb
PA
9751 int from_tty, int enabled, int internal,
9752 unsigned flags)
c3f6f71d 9753{
7efd8fc2 9754 struct linespec_result canonical;
c3f6f71d 9755 struct cleanup *old_chain;
80c99de1 9756 struct cleanup *bkpt_chain = NULL;
0101ce28 9757 int pending = 0;
4a306c9a 9758 int task = 0;
86b17b60 9759 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9760
348d480f
PA
9761 gdb_assert (ops != NULL);
9762
f00aae0f
KS
9763 /* If extra_string isn't useful, set it to NULL. */
9764 if (extra_string != NULL && *extra_string == '\0')
9765 extra_string = NULL;
9766
7efd8fc2 9767 init_linespec_result (&canonical);
c3f6f71d 9768
492d29ea 9769 TRY
b78a6381 9770 {
f00aae0f 9771 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9772 }
492d29ea 9773 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9774 {
492d29ea
PA
9775 /* If caller is interested in rc value from parse, set
9776 value. */
9777 if (e.error == NOT_FOUND_ERROR)
0101ce28 9778 {
05ff989b
AC
9779 /* If pending breakpoint support is turned off, throw
9780 error. */
fa8d40ab
JJ
9781
9782 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9783 throw_exception (e);
9784
9785 exception_print (gdb_stderr, e);
fa8d40ab 9786
05ff989b
AC
9787 /* If pending breakpoint support is auto query and the user
9788 selects no, then simply return the error code. */
059fb39f 9789 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9790 && !nquery (_("Make %s pending on future shared library load? "),
9791 bptype_string (type_wanted)))
fd9b8c24 9792 return 0;
fa8d40ab 9793
05ff989b
AC
9794 /* At this point, either the user was queried about setting
9795 a pending breakpoint and selected yes, or pending
9796 breakpoint behavior is on and thus a pending breakpoint
9797 is defaulted on behalf of the user. */
f00aae0f 9798 pending = 1;
0101ce28 9799 }
492d29ea
PA
9800 else
9801 throw_exception (e);
0101ce28 9802 }
492d29ea
PA
9803 END_CATCH
9804
f00aae0f 9805 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9806 return 0;
c3f6f71d 9807
4a64f543 9808 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9809 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9810
c3f6f71d
JM
9811 /* ----------------------------- SNIP -----------------------------
9812 Anything added to the cleanup chain beyond this point is assumed
9813 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9814 then the memory is not reclaimed. */
9815 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9816
c3f6f71d
JM
9817 /* Resolve all line numbers to PC's and verify that the addresses
9818 are ok for the target. */
0101ce28 9819 if (!pending)
f8eba3c6
TT
9820 {
9821 int ix;
9822 struct linespec_sals *iter;
9823
9824 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9825 breakpoint_sals_to_pc (&iter->sals);
9826 }
c3f6f71d 9827
7a697b8d 9828 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9829 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9830 {
9831 int ix;
9832 struct linespec_sals *iter;
9833
9834 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9835 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9836 }
7a697b8d 9837
c3f6f71d
JM
9838 /* Verify that condition can be parsed, before setting any
9839 breakpoints. Allocate a separate condition expression for each
4a64f543 9840 breakpoint. */
0101ce28 9841 if (!pending)
c3f6f71d 9842 {
f00aae0f 9843 if (parse_extra)
72b2ff0e 9844 {
0878d0fa 9845 char *rest;
52d361e1
YQ
9846 struct linespec_sals *lsal;
9847
9848 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9849
0878d0fa
YQ
9850 /* Here we only parse 'arg' to separate condition
9851 from thread number, so parsing in context of first
9852 sal is OK. When setting the breakpoint we'll
9853 re-parse it in context of each sal. */
9854
f00aae0f
KS
9855 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9856 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9857 if (cond_string)
9858 make_cleanup (xfree, cond_string);
9859 if (rest)
9860 make_cleanup (xfree, rest);
9861 if (rest)
9862 extra_string = rest;
f00aae0f
KS
9863 else
9864 extra_string = NULL;
72b2ff0e 9865 }
2f069f6f 9866 else
72b2ff0e 9867 {
f00aae0f
KS
9868 if (type_wanted != bp_dprintf
9869 && extra_string != NULL && *extra_string != '\0')
9870 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9871
9872 /* Create a private copy of condition string. */
9873 if (cond_string)
9874 {
9875 cond_string = xstrdup (cond_string);
9876 make_cleanup (xfree, cond_string);
9877 }
9878 /* Create a private copy of any extra string. */
9879 if (extra_string)
9880 {
9881 extra_string = xstrdup (extra_string);
9882 make_cleanup (xfree, extra_string);
9883 }
72b2ff0e 9884 }
0fb4aa4b 9885
52d361e1 9886 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9887 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9888 tempflag ? disp_del : disp_donttouch,
9889 thread, task, ignore_count, ops,
44f238bb 9890 from_tty, enabled, internal, flags);
c906108c 9891 }
0101ce28
JJ
9892 else
9893 {
0101ce28
JJ
9894 struct breakpoint *b;
9895
bfccc43c
YQ
9896 if (is_tracepoint_type (type_wanted))
9897 {
9898 struct tracepoint *t;
9899
9900 t = XCNEW (struct tracepoint);
9901 b = &t->base;
9902 }
9903 else
9904 b = XNEW (struct breakpoint);
9905
9906 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9907 b->location = copy_event_location (location);
bfccc43c 9908
f00aae0f
KS
9909 if (parse_extra)
9910 b->cond_string = NULL;
e12c7713
MK
9911 else
9912 {
9913 /* Create a private copy of condition string. */
9914 if (cond_string)
9915 {
9916 cond_string = xstrdup (cond_string);
9917 make_cleanup (xfree, cond_string);
9918 }
9919 b->cond_string = cond_string;
15630549 9920 b->thread = thread;
e12c7713 9921 }
f00aae0f
KS
9922
9923 /* Create a private copy of any extra string. */
9924 if (extra_string != NULL)
9925 {
9926 extra_string = xstrdup (extra_string);
9927 make_cleanup (xfree, extra_string);
9928 }
9929 b->extra_string = extra_string;
0101ce28 9930 b->ignore_count = ignore_count;
0101ce28 9931 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9932 b->condition_not_parsed = 1;
41447f92 9933 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9934 if ((type_wanted != bp_breakpoint
9935 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9936 b->pspace = current_program_space;
8bea4e01 9937
bfccc43c 9938 install_breakpoint (internal, b, 0);
0101ce28
JJ
9939 }
9940
f8eba3c6 9941 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9942 {
3e43a32a
MS
9943 warning (_("Multiple breakpoints were set.\nUse the "
9944 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9945 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9946 }
9947
80c99de1
PA
9948 /* That's it. Discard the cleanups for data inserted into the
9949 breakpoint. */
9950 discard_cleanups (bkpt_chain);
9951 /* But cleanup everything else. */
c3f6f71d 9952 do_cleanups (old_chain);
217dc9e2 9953
80c99de1 9954 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9955 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9956
9957 return 1;
c3f6f71d 9958}
c906108c 9959
348d480f 9960/* Set a breakpoint.
72b2ff0e
VP
9961 ARG is a string describing breakpoint address,
9962 condition, and thread.
9963 FLAG specifies if a breakpoint is hardware on,
9964 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9965 and BP_TEMPFLAG. */
348d480f 9966
98deb0da 9967static void
72b2ff0e 9968break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9969{
72b2ff0e 9970 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9971 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9972 ? bp_hardware_breakpoint
9973 : bp_breakpoint);
55aa24fb 9974 struct breakpoint_ops *ops;
f00aae0f
KS
9975 struct event_location *location;
9976 struct cleanup *cleanup;
9977
9978 location = string_to_event_location (&arg, current_language);
9979 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9980
9981 /* Matching breakpoints on probes. */
5b56227b
KS
9982 if (location != NULL
9983 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9984 ops = &bkpt_probe_breakpoint_ops;
9985 else
9986 ops = &bkpt_breakpoint_ops;
c3f6f71d 9987
8cdf0e15 9988 create_breakpoint (get_current_arch (),
f00aae0f
KS
9989 location,
9990 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9991 tempflag, type_wanted,
8cdf0e15
VP
9992 0 /* Ignore count */,
9993 pending_break_support,
55aa24fb 9994 ops,
8cdf0e15 9995 from_tty,
84f4c1fe 9996 1 /* enabled */,
44f238bb
PA
9997 0 /* internal */,
9998 0);
f00aae0f 9999 do_cleanups (cleanup);
c906108c
SS
10000}
10001
c906108c
SS
10002/* Helper function for break_command_1 and disassemble_command. */
10003
10004void
fba45db2 10005resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10006{
10007 CORE_ADDR pc;
10008
10009 if (sal->pc == 0 && sal->symtab != NULL)
10010 {
10011 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10012 error (_("No line %d in file \"%s\"."),
05cba821 10013 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10014 sal->pc = pc;
6a048695 10015
4a64f543
MS
10016 /* If this SAL corresponds to a breakpoint inserted using a line
10017 number, then skip the function prologue if necessary. */
6a048695 10018 if (sal->explicit_line)
059acae7 10019 skip_prologue_sal (sal);
c906108c
SS
10020 }
10021
10022 if (sal->section == 0 && sal->symtab != NULL)
10023 {
346d1dfe 10024 const struct blockvector *bv;
3977b71f 10025 const struct block *b;
c5aa993b 10026 struct symbol *sym;
c906108c 10027
43f3e411
DE
10028 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10029 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10030 if (bv != NULL)
10031 {
7f0df278 10032 sym = block_linkage_function (b);
c906108c
SS
10033 if (sym != NULL)
10034 {
eb822aa6
DE
10035 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10036 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10037 sym);
c906108c
SS
10038 }
10039 else
10040 {
4a64f543
MS
10041 /* It really is worthwhile to have the section, so we'll
10042 just have to look harder. This case can be executed
10043 if we have line numbers but no functions (as can
10044 happen in assembly source). */
c906108c 10045
7cbd4a93 10046 struct bound_minimal_symbol msym;
6c95b8df
PA
10047 struct cleanup *old_chain = save_current_space_and_thread ();
10048
10049 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10050
10051 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10052 if (msym.minsym)
efd66ac6 10053 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10054
10055 do_cleanups (old_chain);
c906108c
SS
10056 }
10057 }
10058 }
10059}
10060
10061void
fba45db2 10062break_command (char *arg, int from_tty)
c906108c 10063{
db107f19 10064 break_command_1 (arg, 0, from_tty);
c906108c
SS
10065}
10066
c906108c 10067void
fba45db2 10068tbreak_command (char *arg, int from_tty)
c906108c 10069{
db107f19 10070 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10071}
10072
c906108c 10073static void
fba45db2 10074hbreak_command (char *arg, int from_tty)
c906108c 10075{
db107f19 10076 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10077}
10078
10079static void
fba45db2 10080thbreak_command (char *arg, int from_tty)
c906108c 10081{
db107f19 10082 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10083}
10084
10085static void
fba45db2 10086stop_command (char *arg, int from_tty)
c906108c 10087{
a3f17187 10088 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10089Usage: stop in <function | address>\n\
a3f17187 10090 stop at <line>\n"));
c906108c
SS
10091}
10092
10093static void
fba45db2 10094stopin_command (char *arg, int from_tty)
c906108c
SS
10095{
10096 int badInput = 0;
10097
c5aa993b 10098 if (arg == (char *) NULL)
c906108c
SS
10099 badInput = 1;
10100 else if (*arg != '*')
10101 {
10102 char *argptr = arg;
10103 int hasColon = 0;
10104
4a64f543 10105 /* Look for a ':'. If this is a line number specification, then
53a5351d 10106 say it is bad, otherwise, it should be an address or
4a64f543 10107 function/method name. */
c906108c 10108 while (*argptr && !hasColon)
c5aa993b
JM
10109 {
10110 hasColon = (*argptr == ':');
10111 argptr++;
10112 }
c906108c
SS
10113
10114 if (hasColon)
c5aa993b 10115 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10116 else
c5aa993b 10117 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10118 }
10119
10120 if (badInput)
a3f17187 10121 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10122 else
db107f19 10123 break_command_1 (arg, 0, from_tty);
c906108c
SS
10124}
10125
10126static void
fba45db2 10127stopat_command (char *arg, int from_tty)
c906108c
SS
10128{
10129 int badInput = 0;
10130
c5aa993b 10131 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10132 badInput = 1;
10133 else
10134 {
10135 char *argptr = arg;
10136 int hasColon = 0;
10137
4a64f543
MS
10138 /* Look for a ':'. If there is a '::' then get out, otherwise
10139 it is probably a line number. */
c906108c 10140 while (*argptr && !hasColon)
c5aa993b
JM
10141 {
10142 hasColon = (*argptr == ':');
10143 argptr++;
10144 }
c906108c
SS
10145
10146 if (hasColon)
c5aa993b 10147 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10148 else
c5aa993b 10149 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10150 }
10151
10152 if (badInput)
a3f17187 10153 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10154 else
db107f19 10155 break_command_1 (arg, 0, from_tty);
c906108c
SS
10156}
10157
e7e0cddf
SS
10158/* The dynamic printf command is mostly like a regular breakpoint, but
10159 with a prewired command list consisting of a single output command,
10160 built from extra arguments supplied on the dprintf command
10161 line. */
10162
da821c7b 10163static void
e7e0cddf
SS
10164dprintf_command (char *arg, int from_tty)
10165{
f00aae0f
KS
10166 struct event_location *location;
10167 struct cleanup *cleanup;
10168
10169 location = string_to_event_location (&arg, current_language);
10170 cleanup = make_cleanup_delete_event_location (location);
10171
10172 /* If non-NULL, ARG should have been advanced past the location;
10173 the next character must be ','. */
10174 if (arg != NULL)
10175 {
10176 if (arg[0] != ',' || arg[1] == '\0')
10177 error (_("Format string required"));
10178 else
10179 {
10180 /* Skip the comma. */
10181 ++arg;
10182 }
10183 }
10184
e7e0cddf 10185 create_breakpoint (get_current_arch (),
f00aae0f
KS
10186 location,
10187 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10188 0, bp_dprintf,
10189 0 /* Ignore count */,
10190 pending_break_support,
10191 &dprintf_breakpoint_ops,
10192 from_tty,
10193 1 /* enabled */,
10194 0 /* internal */,
10195 0);
f00aae0f 10196 do_cleanups (cleanup);
e7e0cddf
SS
10197}
10198
d3ce09f5
SS
10199static void
10200agent_printf_command (char *arg, int from_tty)
10201{
10202 error (_("May only run agent-printf on the target"));
10203}
10204
f1310107
TJB
10205/* Implement the "breakpoint_hit" breakpoint_ops method for
10206 ranged breakpoints. */
10207
10208static int
10209breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10210 struct address_space *aspace,
09ac7c10
TT
10211 CORE_ADDR bp_addr,
10212 const struct target_waitstatus *ws)
f1310107 10213{
09ac7c10 10214 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10215 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10216 return 0;
10217
f1310107
TJB
10218 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10219 bl->length, aspace, bp_addr);
10220}
10221
10222/* Implement the "resources_needed" breakpoint_ops method for
10223 ranged breakpoints. */
10224
10225static int
10226resources_needed_ranged_breakpoint (const struct bp_location *bl)
10227{
10228 return target_ranged_break_num_registers ();
10229}
10230
10231/* Implement the "print_it" breakpoint_ops method for
10232 ranged breakpoints. */
10233
10234static enum print_stop_action
348d480f 10235print_it_ranged_breakpoint (bpstat bs)
f1310107 10236{
348d480f 10237 struct breakpoint *b = bs->breakpoint_at;
f1310107 10238 struct bp_location *bl = b->loc;
79a45e25 10239 struct ui_out *uiout = current_uiout;
f1310107
TJB
10240
10241 gdb_assert (b->type == bp_hardware_breakpoint);
10242
10243 /* Ranged breakpoints have only one location. */
10244 gdb_assert (bl && bl->next == NULL);
10245
10246 annotate_breakpoint (b->number);
f303dbd6
PA
10247
10248 maybe_print_thread_hit_breakpoint (uiout);
10249
f1310107 10250 if (b->disposition == disp_del)
f303dbd6 10251 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10252 else
f303dbd6 10253 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10254 if (ui_out_is_mi_like_p (uiout))
10255 {
10256 ui_out_field_string (uiout, "reason",
10257 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10258 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10259 }
10260 ui_out_field_int (uiout, "bkptno", b->number);
10261 ui_out_text (uiout, ", ");
10262
10263 return PRINT_SRC_AND_LOC;
10264}
10265
10266/* Implement the "print_one" breakpoint_ops method for
10267 ranged breakpoints. */
10268
10269static void
10270print_one_ranged_breakpoint (struct breakpoint *b,
10271 struct bp_location **last_loc)
10272{
10273 struct bp_location *bl = b->loc;
10274 struct value_print_options opts;
79a45e25 10275 struct ui_out *uiout = current_uiout;
f1310107
TJB
10276
10277 /* Ranged breakpoints have only one location. */
10278 gdb_assert (bl && bl->next == NULL);
10279
10280 get_user_print_options (&opts);
10281
10282 if (opts.addressprint)
10283 /* We don't print the address range here, it will be printed later
10284 by print_one_detail_ranged_breakpoint. */
10285 ui_out_field_skip (uiout, "addr");
10286 annotate_field (5);
10287 print_breakpoint_location (b, bl);
10288 *last_loc = bl;
10289}
10290
10291/* Implement the "print_one_detail" breakpoint_ops method for
10292 ranged breakpoints. */
10293
10294static void
10295print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10296 struct ui_out *uiout)
10297{
10298 CORE_ADDR address_start, address_end;
10299 struct bp_location *bl = b->loc;
f99d8bf4
PA
10300 struct ui_file *stb = mem_fileopen ();
10301 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10302
10303 gdb_assert (bl);
10304
10305 address_start = bl->address;
10306 address_end = address_start + bl->length - 1;
10307
10308 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10309 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10310 print_core_address (bl->gdbarch, address_start),
10311 print_core_address (bl->gdbarch, address_end));
10312 ui_out_field_stream (uiout, "addr", stb);
10313 ui_out_text (uiout, "\n");
10314
10315 do_cleanups (cleanup);
10316}
10317
10318/* Implement the "print_mention" breakpoint_ops method for
10319 ranged breakpoints. */
10320
10321static void
10322print_mention_ranged_breakpoint (struct breakpoint *b)
10323{
10324 struct bp_location *bl = b->loc;
79a45e25 10325 struct ui_out *uiout = current_uiout;
f1310107
TJB
10326
10327 gdb_assert (bl);
10328 gdb_assert (b->type == bp_hardware_breakpoint);
10329
10330 if (ui_out_is_mi_like_p (uiout))
10331 return;
10332
10333 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10334 b->number, paddress (bl->gdbarch, bl->address),
10335 paddress (bl->gdbarch, bl->address + bl->length - 1));
10336}
10337
10338/* Implement the "print_recreate" breakpoint_ops method for
10339 ranged breakpoints. */
10340
10341static void
10342print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10343{
f00aae0f
KS
10344 fprintf_unfiltered (fp, "break-range %s, %s",
10345 event_location_to_string (b->location),
10346 event_location_to_string (b->location_range_end));
d9b3f62e 10347 print_recreate_thread (b, fp);
f1310107
TJB
10348}
10349
10350/* The breakpoint_ops structure to be used in ranged breakpoints. */
10351
2060206e 10352static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10353
10354/* Find the address where the end of the breakpoint range should be
10355 placed, given the SAL of the end of the range. This is so that if
10356 the user provides a line number, the end of the range is set to the
10357 last instruction of the given line. */
10358
10359static CORE_ADDR
10360find_breakpoint_range_end (struct symtab_and_line sal)
10361{
10362 CORE_ADDR end;
10363
10364 /* If the user provided a PC value, use it. Otherwise,
10365 find the address of the end of the given location. */
10366 if (sal.explicit_pc)
10367 end = sal.pc;
10368 else
10369 {
10370 int ret;
10371 CORE_ADDR start;
10372
10373 ret = find_line_pc_range (sal, &start, &end);
10374 if (!ret)
10375 error (_("Could not find location of the end of the range."));
10376
10377 /* find_line_pc_range returns the start of the next line. */
10378 end--;
10379 }
10380
10381 return end;
10382}
10383
10384/* Implement the "break-range" CLI command. */
10385
10386static void
10387break_range_command (char *arg, int from_tty)
10388{
10389 char *arg_start, *addr_string_start, *addr_string_end;
10390 struct linespec_result canonical_start, canonical_end;
10391 int bp_count, can_use_bp, length;
10392 CORE_ADDR end;
10393 struct breakpoint *b;
10394 struct symtab_and_line sal_start, sal_end;
f1310107 10395 struct cleanup *cleanup_bkpt;
f8eba3c6 10396 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10397 struct event_location *start_location, *end_location;
f1310107
TJB
10398
10399 /* We don't support software ranged breakpoints. */
10400 if (target_ranged_break_num_registers () < 0)
10401 error (_("This target does not support hardware ranged breakpoints."));
10402
10403 bp_count = hw_breakpoint_used_count ();
10404 bp_count += target_ranged_break_num_registers ();
10405 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10406 bp_count, 0);
10407 if (can_use_bp < 0)
10408 error (_("Hardware breakpoints used exceeds limit."));
10409
f8eba3c6 10410 arg = skip_spaces (arg);
f1310107
TJB
10411 if (arg == NULL || arg[0] == '\0')
10412 error(_("No address range specified."));
10413
f1310107
TJB
10414 init_linespec_result (&canonical_start);
10415
f8eba3c6 10416 arg_start = arg;
f00aae0f
KS
10417 start_location = string_to_event_location (&arg, current_language);
10418 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10419 parse_breakpoint_sals (start_location, &canonical_start);
10420 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10421
10422 if (arg[0] != ',')
10423 error (_("Too few arguments."));
f8eba3c6 10424 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10425 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10426
10427 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10428
10429 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10430 || lsal_start->sals.nelts != 1)
f1310107
TJB
10431 error (_("Cannot create a ranged breakpoint with multiple locations."));
10432
f8eba3c6
TT
10433 sal_start = lsal_start->sals.sals[0];
10434 addr_string_start = savestring (arg_start, arg - arg_start);
10435 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10436
10437 arg++; /* Skip the comma. */
f8eba3c6 10438 arg = skip_spaces (arg);
f1310107
TJB
10439
10440 /* Parse the end location. */
10441
f1310107
TJB
10442 init_linespec_result (&canonical_end);
10443 arg_start = arg;
10444
f8eba3c6 10445 /* We call decode_line_full directly here instead of using
f1310107
TJB
10446 parse_breakpoint_sals because we need to specify the start location's
10447 symtab and line as the default symtab and line for the end of the
10448 range. This makes it possible to have ranges like "foo.c:27, +14",
10449 where +14 means 14 lines from the start location. */
f00aae0f
KS
10450 end_location = string_to_event_location (&arg, current_language);
10451 make_cleanup_delete_event_location (end_location);
c2f4122d 10452 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10453 sal_start.symtab, sal_start.line,
10454 &canonical_end, NULL, NULL);
10455
10456 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10457
f8eba3c6 10458 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10459 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10460
10461 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10462 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10463 || lsal_end->sals.nelts != 1)
f1310107
TJB
10464 error (_("Cannot create a ranged breakpoint with multiple locations."));
10465
f8eba3c6 10466 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10467
10468 end = find_breakpoint_range_end (sal_end);
10469 if (sal_start.pc > end)
177b42fe 10470 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10471
10472 length = end - sal_start.pc + 1;
10473 if (length < 0)
10474 /* Length overflowed. */
10475 error (_("Address range too large."));
10476 else if (length == 1)
10477 {
10478 /* This range is simple enough to be handled by
10479 the `hbreak' command. */
10480 hbreak_command (addr_string_start, 1);
10481
10482 do_cleanups (cleanup_bkpt);
10483
10484 return;
10485 }
10486
10487 /* Now set up the breakpoint. */
10488 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10489 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10490 set_breakpoint_count (breakpoint_count + 1);
10491 b->number = breakpoint_count;
10492 b->disposition = disp_donttouch;
f00aae0f
KS
10493 b->location = copy_event_location (start_location);
10494 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10495 b->loc->length = length;
10496
f8eba3c6 10497 do_cleanups (cleanup_bkpt);
f1310107
TJB
10498
10499 mention (b);
8d3788bd 10500 observer_notify_breakpoint_created (b);
44702360 10501 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10502}
10503
4a64f543
MS
10504/* Return non-zero if EXP is verified as constant. Returned zero
10505 means EXP is variable. Also the constant detection may fail for
10506 some constant expressions and in such case still falsely return
10507 zero. */
2e6e3d9c 10508
65d79d4b
SDJ
10509static int
10510watchpoint_exp_is_const (const struct expression *exp)
10511{
10512 int i = exp->nelts;
10513
10514 while (i > 0)
10515 {
10516 int oplenp, argsp;
10517
10518 /* We are only interested in the descriptor of each element. */
10519 operator_length (exp, i, &oplenp, &argsp);
10520 i -= oplenp;
10521
10522 switch (exp->elts[i].opcode)
10523 {
10524 case BINOP_ADD:
10525 case BINOP_SUB:
10526 case BINOP_MUL:
10527 case BINOP_DIV:
10528 case BINOP_REM:
10529 case BINOP_MOD:
10530 case BINOP_LSH:
10531 case BINOP_RSH:
10532 case BINOP_LOGICAL_AND:
10533 case BINOP_LOGICAL_OR:
10534 case BINOP_BITWISE_AND:
10535 case BINOP_BITWISE_IOR:
10536 case BINOP_BITWISE_XOR:
10537 case BINOP_EQUAL:
10538 case BINOP_NOTEQUAL:
10539 case BINOP_LESS:
10540 case BINOP_GTR:
10541 case BINOP_LEQ:
10542 case BINOP_GEQ:
10543 case BINOP_REPEAT:
10544 case BINOP_COMMA:
10545 case BINOP_EXP:
10546 case BINOP_MIN:
10547 case BINOP_MAX:
10548 case BINOP_INTDIV:
10549 case BINOP_CONCAT:
65d79d4b
SDJ
10550 case TERNOP_COND:
10551 case TERNOP_SLICE:
65d79d4b
SDJ
10552
10553 case OP_LONG:
10554 case OP_DOUBLE:
10555 case OP_DECFLOAT:
10556 case OP_LAST:
10557 case OP_COMPLEX:
10558 case OP_STRING:
65d79d4b
SDJ
10559 case OP_ARRAY:
10560 case OP_TYPE:
608b4967
TT
10561 case OP_TYPEOF:
10562 case OP_DECLTYPE:
6e72ca20 10563 case OP_TYPEID:
65d79d4b
SDJ
10564 case OP_NAME:
10565 case OP_OBJC_NSSTRING:
10566
10567 case UNOP_NEG:
10568 case UNOP_LOGICAL_NOT:
10569 case UNOP_COMPLEMENT:
10570 case UNOP_ADDR:
10571 case UNOP_HIGH:
aeaa2474 10572 case UNOP_CAST:
9eaf6705
TT
10573
10574 case UNOP_CAST_TYPE:
10575 case UNOP_REINTERPRET_CAST:
10576 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10577 /* Unary, binary and ternary operators: We have to check
10578 their operands. If they are constant, then so is the
10579 result of that operation. For instance, if A and B are
10580 determined to be constants, then so is "A + B".
10581
10582 UNOP_IND is one exception to the rule above, because the
10583 value of *ADDR is not necessarily a constant, even when
10584 ADDR is. */
65d79d4b
SDJ
10585 break;
10586
10587 case OP_VAR_VALUE:
10588 /* Check whether the associated symbol is a constant.
4a64f543 10589
65d79d4b 10590 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10591 possible that a buggy compiler could mark a variable as
10592 constant even when it is not, and TYPE_CONST would return
10593 true in this case, while SYMBOL_CLASS wouldn't.
10594
10595 We also have to check for function symbols because they
10596 are always constant. */
65d79d4b
SDJ
10597 {
10598 struct symbol *s = exp->elts[i + 2].symbol;
10599
10600 if (SYMBOL_CLASS (s) != LOC_BLOCK
10601 && SYMBOL_CLASS (s) != LOC_CONST
10602 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10603 return 0;
10604 break;
10605 }
10606
10607 /* The default action is to return 0 because we are using
10608 the optimistic approach here: If we don't know something,
10609 then it is not a constant. */
10610 default:
10611 return 0;
10612 }
10613 }
10614
10615 return 1;
10616}
10617
3a5c3e22
PA
10618/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10619
10620static void
10621dtor_watchpoint (struct breakpoint *self)
10622{
10623 struct watchpoint *w = (struct watchpoint *) self;
10624
10625 xfree (w->cond_exp);
10626 xfree (w->exp);
10627 xfree (w->exp_string);
10628 xfree (w->exp_string_reparse);
10629 value_free (w->val);
10630
10631 base_breakpoint_ops.dtor (self);
10632}
10633
348d480f
PA
10634/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10635
10636static void
10637re_set_watchpoint (struct breakpoint *b)
10638{
3a5c3e22
PA
10639 struct watchpoint *w = (struct watchpoint *) b;
10640
348d480f
PA
10641 /* Watchpoint can be either on expression using entirely global
10642 variables, or it can be on local variables.
10643
10644 Watchpoints of the first kind are never auto-deleted, and even
10645 persist across program restarts. Since they can use variables
10646 from shared libraries, we need to reparse expression as libraries
10647 are loaded and unloaded.
10648
10649 Watchpoints on local variables can also change meaning as result
10650 of solib event. For example, if a watchpoint uses both a local
10651 and a global variables in expression, it's a local watchpoint,
10652 but unloading of a shared library will make the expression
10653 invalid. This is not a very common use case, but we still
10654 re-evaluate expression, to avoid surprises to the user.
10655
10656 Note that for local watchpoints, we re-evaluate it only if
10657 watchpoints frame id is still valid. If it's not, it means the
10658 watchpoint is out of scope and will be deleted soon. In fact,
10659 I'm not sure we'll ever be called in this case.
10660
10661 If a local watchpoint's frame id is still valid, then
3a5c3e22 10662 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10663
3a5c3e22
PA
10664 Don't do anything about disabled watchpoints, since they will be
10665 reevaluated again when enabled. */
10666 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10667}
10668
77b06cd7
TJB
10669/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10670
10671static int
10672insert_watchpoint (struct bp_location *bl)
10673{
3a5c3e22
PA
10674 struct watchpoint *w = (struct watchpoint *) bl->owner;
10675 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10676
10677 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10678 w->cond_exp);
77b06cd7
TJB
10679}
10680
10681/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10682
10683static int
10684remove_watchpoint (struct bp_location *bl)
10685{
3a5c3e22
PA
10686 struct watchpoint *w = (struct watchpoint *) bl->owner;
10687 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10688
10689 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10690 w->cond_exp);
e09342b5
TJB
10691}
10692
e09342b5 10693static int
348d480f 10694breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10695 struct address_space *aspace, CORE_ADDR bp_addr,
10696 const struct target_waitstatus *ws)
e09342b5 10697{
348d480f 10698 struct breakpoint *b = bl->owner;
3a5c3e22 10699 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10700
348d480f
PA
10701 /* Continuable hardware watchpoints are treated as non-existent if the
10702 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10703 some data address). Otherwise gdb won't stop on a break instruction
10704 in the code (not from a breakpoint) when a hardware watchpoint has
10705 been defined. Also skip watchpoints which we know did not trigger
10706 (did not match the data address). */
10707 if (is_hardware_watchpoint (b)
3a5c3e22 10708 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10709 return 0;
9c06b0b4 10710
348d480f 10711 return 1;
9c06b0b4
TJB
10712}
10713
348d480f
PA
10714static void
10715check_status_watchpoint (bpstat bs)
9c06b0b4 10716{
348d480f 10717 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10718
348d480f 10719 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10720}
10721
10722/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10723 hardware watchpoints. */
9c06b0b4
TJB
10724
10725static int
348d480f 10726resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10727{
3a5c3e22
PA
10728 struct watchpoint *w = (struct watchpoint *) bl->owner;
10729 int length = w->exact? 1 : bl->length;
348d480f
PA
10730
10731 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10732}
10733
10734/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10735 hardware watchpoints. */
9c06b0b4
TJB
10736
10737static int
348d480f 10738works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10739{
efa80663
PA
10740 /* Read and access watchpoints only work with hardware support. */
10741 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10742}
10743
9c06b0b4 10744static enum print_stop_action
348d480f 10745print_it_watchpoint (bpstat bs)
9c06b0b4 10746{
348d480f
PA
10747 struct cleanup *old_chain;
10748 struct breakpoint *b;
f99d8bf4 10749 struct ui_file *stb;
348d480f 10750 enum print_stop_action result;
3a5c3e22 10751 struct watchpoint *w;
79a45e25 10752 struct ui_out *uiout = current_uiout;
348d480f
PA
10753
10754 gdb_assert (bs->bp_location_at != NULL);
10755
348d480f 10756 b = bs->breakpoint_at;
3a5c3e22 10757 w = (struct watchpoint *) b;
348d480f 10758
f99d8bf4
PA
10759 stb = mem_fileopen ();
10760 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10761
f303dbd6
PA
10762 annotate_watchpoint (b->number);
10763 maybe_print_thread_hit_breakpoint (uiout);
10764
9c06b0b4
TJB
10765 switch (b->type)
10766 {
348d480f 10767 case bp_watchpoint:
9c06b0b4 10768 case bp_hardware_watchpoint:
9c06b0b4
TJB
10769 if (ui_out_is_mi_like_p (uiout))
10770 ui_out_field_string
10771 (uiout, "reason",
10772 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10773 mention (b);
10774 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10775 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10776 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10777 ui_out_field_stream (uiout, "old", stb);
10778 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10779 watchpoint_value_print (w->val, stb);
348d480f
PA
10780 ui_out_field_stream (uiout, "new", stb);
10781 ui_out_text (uiout, "\n");
10782 /* More than one watchpoint may have been triggered. */
10783 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10784 break;
10785
10786 case bp_read_watchpoint:
10787 if (ui_out_is_mi_like_p (uiout))
10788 ui_out_field_string
10789 (uiout, "reason",
10790 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10791 mention (b);
10792 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10793 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10794 watchpoint_value_print (w->val, stb);
348d480f
PA
10795 ui_out_field_stream (uiout, "value", stb);
10796 ui_out_text (uiout, "\n");
10797 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10798 break;
10799
10800 case bp_access_watchpoint:
348d480f
PA
10801 if (bs->old_val != NULL)
10802 {
348d480f
PA
10803 if (ui_out_is_mi_like_p (uiout))
10804 ui_out_field_string
10805 (uiout, "reason",
10806 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10807 mention (b);
10808 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10809 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10810 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10811 ui_out_field_stream (uiout, "old", stb);
10812 ui_out_text (uiout, "\nNew value = ");
10813 }
10814 else
10815 {
10816 mention (b);
10817 if (ui_out_is_mi_like_p (uiout))
10818 ui_out_field_string
10819 (uiout, "reason",
10820 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10821 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10822 ui_out_text (uiout, "\nValue = ");
10823 }
f99d8bf4 10824 watchpoint_value_print (w->val, stb);
348d480f
PA
10825 ui_out_field_stream (uiout, "new", stb);
10826 ui_out_text (uiout, "\n");
10827 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10828 break;
10829 default:
348d480f 10830 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10831 }
10832
348d480f
PA
10833 do_cleanups (old_chain);
10834 return result;
10835}
10836
10837/* Implement the "print_mention" breakpoint_ops method for hardware
10838 watchpoints. */
10839
10840static void
10841print_mention_watchpoint (struct breakpoint *b)
10842{
10843 struct cleanup *ui_out_chain;
3a5c3e22 10844 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10845 struct ui_out *uiout = current_uiout;
348d480f
PA
10846
10847 switch (b->type)
10848 {
10849 case bp_watchpoint:
10850 ui_out_text (uiout, "Watchpoint ");
10851 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10852 break;
10853 case bp_hardware_watchpoint:
10854 ui_out_text (uiout, "Hardware watchpoint ");
10855 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10856 break;
10857 case bp_read_watchpoint:
10858 ui_out_text (uiout, "Hardware read watchpoint ");
10859 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10860 break;
10861 case bp_access_watchpoint:
10862 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10863 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10864 break;
10865 default:
10866 internal_error (__FILE__, __LINE__,
10867 _("Invalid hardware watchpoint type."));
10868 }
10869
10870 ui_out_field_int (uiout, "number", b->number);
10871 ui_out_text (uiout, ": ");
3a5c3e22 10872 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10873 do_cleanups (ui_out_chain);
10874}
10875
10876/* Implement the "print_recreate" breakpoint_ops method for
10877 watchpoints. */
10878
10879static void
10880print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10881{
3a5c3e22
PA
10882 struct watchpoint *w = (struct watchpoint *) b;
10883
348d480f
PA
10884 switch (b->type)
10885 {
10886 case bp_watchpoint:
10887 case bp_hardware_watchpoint:
10888 fprintf_unfiltered (fp, "watch");
10889 break;
10890 case bp_read_watchpoint:
10891 fprintf_unfiltered (fp, "rwatch");
10892 break;
10893 case bp_access_watchpoint:
10894 fprintf_unfiltered (fp, "awatch");
10895 break;
10896 default:
10897 internal_error (__FILE__, __LINE__,
10898 _("Invalid watchpoint type."));
10899 }
10900
3a5c3e22 10901 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10902 print_recreate_thread (b, fp);
348d480f
PA
10903}
10904
427cd150
TT
10905/* Implement the "explains_signal" breakpoint_ops method for
10906 watchpoints. */
10907
47591c29 10908static int
427cd150
TT
10909explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10910{
10911 /* A software watchpoint cannot cause a signal other than
10912 GDB_SIGNAL_TRAP. */
10913 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10914 return 0;
427cd150 10915
47591c29 10916 return 1;
427cd150
TT
10917}
10918
348d480f
PA
10919/* The breakpoint_ops structure to be used in hardware watchpoints. */
10920
2060206e 10921static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10922
10923/* Implement the "insert" breakpoint_ops method for
10924 masked hardware watchpoints. */
10925
10926static int
10927insert_masked_watchpoint (struct bp_location *bl)
10928{
3a5c3e22
PA
10929 struct watchpoint *w = (struct watchpoint *) bl->owner;
10930
10931 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10932 bl->watchpoint_type);
10933}
10934
10935/* Implement the "remove" breakpoint_ops method for
10936 masked hardware watchpoints. */
10937
10938static int
10939remove_masked_watchpoint (struct bp_location *bl)
10940{
3a5c3e22
PA
10941 struct watchpoint *w = (struct watchpoint *) bl->owner;
10942
10943 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10944 bl->watchpoint_type);
10945}
10946
10947/* Implement the "resources_needed" breakpoint_ops method for
10948 masked hardware watchpoints. */
10949
10950static int
10951resources_needed_masked_watchpoint (const struct bp_location *bl)
10952{
3a5c3e22
PA
10953 struct watchpoint *w = (struct watchpoint *) bl->owner;
10954
10955 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10956}
10957
10958/* Implement the "works_in_software_mode" breakpoint_ops method for
10959 masked hardware watchpoints. */
10960
10961static int
10962works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10963{
10964 return 0;
10965}
10966
10967/* Implement the "print_it" breakpoint_ops method for
10968 masked hardware watchpoints. */
10969
10970static enum print_stop_action
10971print_it_masked_watchpoint (bpstat bs)
10972{
10973 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10974 struct ui_out *uiout = current_uiout;
348d480f
PA
10975
10976 /* Masked watchpoints have only one location. */
10977 gdb_assert (b->loc && b->loc->next == NULL);
10978
f303dbd6
PA
10979 annotate_watchpoint (b->number);
10980 maybe_print_thread_hit_breakpoint (uiout);
10981
348d480f
PA
10982 switch (b->type)
10983 {
10984 case bp_hardware_watchpoint:
348d480f
PA
10985 if (ui_out_is_mi_like_p (uiout))
10986 ui_out_field_string
10987 (uiout, "reason",
10988 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10989 break;
10990
10991 case bp_read_watchpoint:
10992 if (ui_out_is_mi_like_p (uiout))
10993 ui_out_field_string
10994 (uiout, "reason",
10995 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10996 break;
10997
10998 case bp_access_watchpoint:
10999 if (ui_out_is_mi_like_p (uiout))
11000 ui_out_field_string
11001 (uiout, "reason",
11002 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11003 break;
11004 default:
11005 internal_error (__FILE__, __LINE__,
11006 _("Invalid hardware watchpoint type."));
11007 }
11008
11009 mention (b);
9c06b0b4
TJB
11010 ui_out_text (uiout, _("\n\
11011Check the underlying instruction at PC for the memory\n\
11012address and value which triggered this watchpoint.\n"));
11013 ui_out_text (uiout, "\n");
11014
11015 /* More than one watchpoint may have been triggered. */
11016 return PRINT_UNKNOWN;
11017}
11018
11019/* Implement the "print_one_detail" breakpoint_ops method for
11020 masked hardware watchpoints. */
11021
11022static void
11023print_one_detail_masked_watchpoint (const struct breakpoint *b,
11024 struct ui_out *uiout)
11025{
3a5c3e22
PA
11026 struct watchpoint *w = (struct watchpoint *) b;
11027
9c06b0b4
TJB
11028 /* Masked watchpoints have only one location. */
11029 gdb_assert (b->loc && b->loc->next == NULL);
11030
11031 ui_out_text (uiout, "\tmask ");
3a5c3e22 11032 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11033 ui_out_text (uiout, "\n");
11034}
11035
11036/* Implement the "print_mention" breakpoint_ops method for
11037 masked hardware watchpoints. */
11038
11039static void
11040print_mention_masked_watchpoint (struct breakpoint *b)
11041{
3a5c3e22 11042 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11043 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11044 struct cleanup *ui_out_chain;
11045
11046 switch (b->type)
11047 {
11048 case bp_hardware_watchpoint:
11049 ui_out_text (uiout, "Masked hardware watchpoint ");
11050 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11051 break;
11052 case bp_read_watchpoint:
11053 ui_out_text (uiout, "Masked hardware read watchpoint ");
11054 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11055 break;
11056 case bp_access_watchpoint:
11057 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11058 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11059 break;
11060 default:
11061 internal_error (__FILE__, __LINE__,
11062 _("Invalid hardware watchpoint type."));
11063 }
11064
11065 ui_out_field_int (uiout, "number", b->number);
11066 ui_out_text (uiout, ": ");
3a5c3e22 11067 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11068 do_cleanups (ui_out_chain);
11069}
11070
11071/* Implement the "print_recreate" breakpoint_ops method for
11072 masked hardware watchpoints. */
11073
11074static void
11075print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11076{
3a5c3e22 11077 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11078 char tmp[40];
11079
11080 switch (b->type)
11081 {
11082 case bp_hardware_watchpoint:
11083 fprintf_unfiltered (fp, "watch");
11084 break;
11085 case bp_read_watchpoint:
11086 fprintf_unfiltered (fp, "rwatch");
11087 break;
11088 case bp_access_watchpoint:
11089 fprintf_unfiltered (fp, "awatch");
11090 break;
11091 default:
11092 internal_error (__FILE__, __LINE__,
11093 _("Invalid hardware watchpoint type."));
11094 }
11095
3a5c3e22
PA
11096 sprintf_vma (tmp, w->hw_wp_mask);
11097 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11098 print_recreate_thread (b, fp);
9c06b0b4
TJB
11099}
11100
11101/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11102
2060206e 11103static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11104
11105/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11106
11107static int
11108is_masked_watchpoint (const struct breakpoint *b)
11109{
11110 return b->ops == &masked_watchpoint_breakpoint_ops;
11111}
11112
53a5351d
JM
11113/* accessflag: hw_write: watch write,
11114 hw_read: watch read,
11115 hw_access: watch access (read or write) */
c906108c 11116static void
bbc13ae3 11117watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11118 int just_location, int internal)
c906108c 11119{
d983da9c 11120 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11121 struct expression *exp;
270140bd 11122 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11123 struct value *val, *mark, *result;
bb9d5f81 11124 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11125 struct frame_info *frame;
bbc13ae3
KS
11126 const char *exp_start = NULL;
11127 const char *exp_end = NULL;
11128 const char *tok, *end_tok;
9c06b0b4 11129 int toklen = -1;
bbc13ae3
KS
11130 const char *cond_start = NULL;
11131 const char *cond_end = NULL;
c906108c 11132 enum bptype bp_type;
37e4754d 11133 int thread = -1;
0cf6dd15 11134 int pc = 0;
9c06b0b4
TJB
11135 /* Flag to indicate whether we are going to use masks for
11136 the hardware watchpoint. */
11137 int use_mask = 0;
11138 CORE_ADDR mask = 0;
3a5c3e22 11139 struct watchpoint *w;
bbc13ae3
KS
11140 char *expression;
11141 struct cleanup *back_to;
c906108c 11142
37e4754d
LM
11143 /* Make sure that we actually have parameters to parse. */
11144 if (arg != NULL && arg[0] != '\0')
11145 {
bbc13ae3
KS
11146 const char *value_start;
11147
11148 exp_end = arg + strlen (arg);
37e4754d 11149
9c06b0b4
TJB
11150 /* Look for "parameter value" pairs at the end
11151 of the arguments string. */
bbc13ae3 11152 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11153 {
11154 /* Skip whitespace at the end of the argument list. */
11155 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11156 tok--;
11157
11158 /* Find the beginning of the last token.
11159 This is the value of the parameter. */
11160 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11161 tok--;
11162 value_start = tok + 1;
11163
11164 /* Skip whitespace. */
11165 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11166 tok--;
11167
11168 end_tok = tok;
11169
11170 /* Find the beginning of the second to last token.
11171 This is the parameter itself. */
11172 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11173 tok--;
11174 tok++;
11175 toklen = end_tok - tok + 1;
11176
61012eef 11177 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11178 {
5d5658a1 11179 struct thread_info *thr;
9c06b0b4
TJB
11180 /* At this point we've found a "thread" token, which means
11181 the user is trying to set a watchpoint that triggers
11182 only in a specific thread. */
5d5658a1 11183 const char *endp;
37e4754d 11184
9c06b0b4
TJB
11185 if (thread != -1)
11186 error(_("You can specify only one thread."));
37e4754d 11187
9c06b0b4 11188 /* Extract the thread ID from the next token. */
5d5658a1 11189 thr = parse_thread_id (value_start, &endp);
37e4754d 11190
5d5658a1 11191 /* Check if the user provided a valid thread ID. */
9c06b0b4 11192 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11193 invalid_thread_id_error (value_start);
9c06b0b4 11194
5d5658a1 11195 thread = thr->global_num;
9c06b0b4 11196 }
61012eef 11197 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11198 {
11199 /* We've found a "mask" token, which means the user wants to
11200 create a hardware watchpoint that is going to have the mask
11201 facility. */
11202 struct value *mask_value, *mark;
37e4754d 11203
9c06b0b4
TJB
11204 if (use_mask)
11205 error(_("You can specify only one mask."));
37e4754d 11206
9c06b0b4 11207 use_mask = just_location = 1;
37e4754d 11208
9c06b0b4
TJB
11209 mark = value_mark ();
11210 mask_value = parse_to_comma_and_eval (&value_start);
11211 mask = value_as_address (mask_value);
11212 value_free_to_mark (mark);
11213 }
11214 else
11215 /* We didn't recognize what we found. We should stop here. */
11216 break;
37e4754d 11217
9c06b0b4
TJB
11218 /* Truncate the string and get rid of the "parameter value" pair before
11219 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11220 exp_end = tok;
9c06b0b4 11221 }
37e4754d 11222 }
bbc13ae3
KS
11223 else
11224 exp_end = arg;
37e4754d 11225
bbc13ae3
KS
11226 /* Parse the rest of the arguments. From here on out, everything
11227 is in terms of a newly allocated string instead of the original
11228 ARG. */
c906108c 11229 innermost_block = NULL;
bbc13ae3
KS
11230 expression = savestring (arg, exp_end - arg);
11231 back_to = make_cleanup (xfree, expression);
11232 exp_start = arg = expression;
1bb9788d 11233 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11234 exp_end = arg;
fa8a61dc
TT
11235 /* Remove trailing whitespace from the expression before saving it.
11236 This makes the eventual display of the expression string a bit
11237 prettier. */
11238 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11239 --exp_end;
11240
65d79d4b
SDJ
11241 /* Checking if the expression is not constant. */
11242 if (watchpoint_exp_is_const (exp))
11243 {
11244 int len;
11245
11246 len = exp_end - exp_start;
11247 while (len > 0 && isspace (exp_start[len - 1]))
11248 len--;
11249 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11250 }
11251
c906108c
SS
11252 exp_valid_block = innermost_block;
11253 mark = value_mark ();
3a1115a0 11254 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11255
bb9d5f81
PP
11256 if (val != NULL && just_location)
11257 {
11258 saved_bitpos = value_bitpos (val);
11259 saved_bitsize = value_bitsize (val);
11260 }
11261
06a64a0b
TT
11262 if (just_location)
11263 {
9c06b0b4
TJB
11264 int ret;
11265
06a64a0b 11266 exp_valid_block = NULL;
a1442452 11267 val = value_addr (result);
06a64a0b
TT
11268 release_value (val);
11269 value_free_to_mark (mark);
9c06b0b4
TJB
11270
11271 if (use_mask)
11272 {
11273 ret = target_masked_watch_num_registers (value_as_address (val),
11274 mask);
11275 if (ret == -1)
11276 error (_("This target does not support masked watchpoints."));
11277 else if (ret == -2)
11278 error (_("Invalid mask or memory region."));
11279 }
06a64a0b
TT
11280 }
11281 else if (val != NULL)
fa4727a6 11282 release_value (val);
c906108c 11283
bbc13ae3
KS
11284 tok = skip_spaces_const (arg);
11285 end_tok = skip_to_space_const (tok);
c906108c
SS
11286
11287 toklen = end_tok - tok;
11288 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11289 {
2d134ed3
PA
11290 struct expression *cond;
11291
60e1c644 11292 innermost_block = NULL;
c906108c 11293 tok = cond_start = end_tok + 1;
1bb9788d 11294 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11295
11296 /* The watchpoint expression may not be local, but the condition
11297 may still be. E.g.: `watch global if local > 0'. */
11298 cond_exp_valid_block = innermost_block;
11299
2d134ed3 11300 xfree (cond);
c906108c
SS
11301 cond_end = tok;
11302 }
11303 if (*tok)
8a3fe4f8 11304 error (_("Junk at end of command."));
c906108c 11305
d983da9c 11306 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11307
11308 /* If the expression is "local", then set up a "watchpoint scope"
11309 breakpoint at the point where we've left the scope of the watchpoint
11310 expression. Create the scope breakpoint before the watchpoint, so
11311 that we will encounter it first in bpstat_stop_status. */
60e1c644 11312 if (exp_valid_block && frame)
d983da9c 11313 {
edb3359d
DJ
11314 if (frame_id_p (frame_unwind_caller_id (frame)))
11315 {
11316 scope_breakpoint
a6d9a66e
UW
11317 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11318 frame_unwind_caller_pc (frame),
06edf0c0
PA
11319 bp_watchpoint_scope,
11320 &momentary_breakpoint_ops);
d983da9c 11321
edb3359d 11322 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11323
edb3359d
DJ
11324 /* Automatically delete the breakpoint when it hits. */
11325 scope_breakpoint->disposition = disp_del;
d983da9c 11326
edb3359d
DJ
11327 /* Only break in the proper frame (help with recursion). */
11328 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11329
edb3359d 11330 /* Set the address at which we will stop. */
a6d9a66e
UW
11331 scope_breakpoint->loc->gdbarch
11332 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11333 scope_breakpoint->loc->requested_address
11334 = frame_unwind_caller_pc (frame);
11335 scope_breakpoint->loc->address
a6d9a66e
UW
11336 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11337 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11338 scope_breakpoint->type);
11339 }
d983da9c
DJ
11340 }
11341
e8369a73
AB
11342 /* Now set up the breakpoint. We create all watchpoints as hardware
11343 watchpoints here even if hardware watchpoints are turned off, a call
11344 to update_watchpoint later in this function will cause the type to
11345 drop back to bp_watchpoint (software watchpoint) if required. */
11346
11347 if (accessflag == hw_read)
11348 bp_type = bp_read_watchpoint;
11349 else if (accessflag == hw_access)
11350 bp_type = bp_access_watchpoint;
11351 else
11352 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11353
11354 w = XCNEW (struct watchpoint);
11355 b = &w->base;
348d480f 11356 if (use_mask)
3a5c3e22
PA
11357 init_raw_breakpoint_without_location (b, NULL, bp_type,
11358 &masked_watchpoint_breakpoint_ops);
348d480f 11359 else
3a5c3e22
PA
11360 init_raw_breakpoint_without_location (b, NULL, bp_type,
11361 &watchpoint_breakpoint_ops);
37e4754d 11362 b->thread = thread;
b5de0fa7 11363 b->disposition = disp_donttouch;
348d480f 11364 b->pspace = current_program_space;
3a5c3e22
PA
11365 w->exp = exp;
11366 w->exp_valid_block = exp_valid_block;
11367 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11368 if (just_location)
11369 {
11370 struct type *t = value_type (val);
11371 CORE_ADDR addr = value_as_address (val);
11372 char *name;
11373
11374 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11375 name = type_to_string (t);
11376
3a5c3e22 11377 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11378 core_addr_to_string (addr));
06a64a0b
TT
11379 xfree (name);
11380
3a5c3e22 11381 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11382 (int) (exp_end - exp_start), exp_start);
11383
06a64a0b
TT
11384 /* The above expression is in C. */
11385 b->language = language_c;
11386 }
11387 else
3a5c3e22 11388 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11389
11390 if (use_mask)
11391 {
3a5c3e22 11392 w->hw_wp_mask = mask;
9c06b0b4
TJB
11393 }
11394 else
11395 {
3a5c3e22 11396 w->val = val;
bb9d5f81
PP
11397 w->val_bitpos = saved_bitpos;
11398 w->val_bitsize = saved_bitsize;
3a5c3e22 11399 w->val_valid = 1;
9c06b0b4 11400 }
77b06cd7 11401
c906108c
SS
11402 if (cond_start)
11403 b->cond_string = savestring (cond_start, cond_end - cond_start);
11404 else
11405 b->cond_string = 0;
c5aa993b 11406
c906108c 11407 if (frame)
f6bc2008 11408 {
3a5c3e22
PA
11409 w->watchpoint_frame = get_frame_id (frame);
11410 w->watchpoint_thread = inferior_ptid;
f6bc2008 11411 }
c906108c 11412 else
f6bc2008 11413 {
3a5c3e22
PA
11414 w->watchpoint_frame = null_frame_id;
11415 w->watchpoint_thread = null_ptid;
f6bc2008 11416 }
c906108c 11417
d983da9c 11418 if (scope_breakpoint != NULL)
c906108c 11419 {
d983da9c
DJ
11420 /* The scope breakpoint is related to the watchpoint. We will
11421 need to act on them together. */
11422 b->related_breakpoint = scope_breakpoint;
11423 scope_breakpoint->related_breakpoint = b;
c906108c 11424 }
d983da9c 11425
06a64a0b
TT
11426 if (!just_location)
11427 value_free_to_mark (mark);
2d134ed3 11428
492d29ea 11429 TRY
a9634178
TJB
11430 {
11431 /* Finally update the new watchpoint. This creates the locations
11432 that should be inserted. */
3a5c3e22 11433 update_watchpoint (w, 1);
a9634178 11434 }
492d29ea 11435 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11436 {
11437 delete_breakpoint (b);
11438 throw_exception (e);
11439 }
492d29ea 11440 END_CATCH
a9634178 11441
3ea46bff 11442 install_breakpoint (internal, b, 1);
bbc13ae3 11443 do_cleanups (back_to);
c906108c
SS
11444}
11445
e09342b5 11446/* Return count of debug registers needed to watch the given expression.
e09342b5 11447 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11448
c906108c 11449static int
a9634178 11450can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11451{
11452 int found_memory_cnt = 0;
2e70b7b9 11453 struct value *head = v;
c906108c
SS
11454
11455 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11456 if (!can_use_hw_watchpoints)
c906108c 11457 return 0;
c5aa993b 11458
5c44784c
JM
11459 /* Make sure that the value of the expression depends only upon
11460 memory contents, and values computed from them within GDB. If we
11461 find any register references or function calls, we can't use a
11462 hardware watchpoint.
11463
11464 The idea here is that evaluating an expression generates a series
11465 of values, one holding the value of every subexpression. (The
11466 expression a*b+c has five subexpressions: a, b, a*b, c, and
11467 a*b+c.) GDB's values hold almost enough information to establish
11468 the criteria given above --- they identify memory lvalues,
11469 register lvalues, computed values, etcetera. So we can evaluate
11470 the expression, and then scan the chain of values that leaves
11471 behind to decide whether we can detect any possible change to the
11472 expression's final value using only hardware watchpoints.
11473
11474 However, I don't think that the values returned by inferior
11475 function calls are special in any way. So this function may not
11476 notice that an expression involving an inferior function call
11477 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11478 for (; v; v = value_next (v))
c906108c 11479 {
5c44784c 11480 if (VALUE_LVAL (v) == lval_memory)
c906108c 11481 {
8464be76
DJ
11482 if (v != head && value_lazy (v))
11483 /* A lazy memory lvalue in the chain is one that GDB never
11484 needed to fetch; we either just used its address (e.g.,
11485 `a' in `a.b') or we never needed it at all (e.g., `a'
11486 in `a,b'). This doesn't apply to HEAD; if that is
11487 lazy then it was not readable, but watch it anyway. */
5c44784c 11488 ;
53a5351d 11489 else
5c44784c
JM
11490 {
11491 /* Ahh, memory we actually used! Check if we can cover
11492 it with hardware watchpoints. */
df407dfe 11493 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11494
11495 /* We only watch structs and arrays if user asked for it
11496 explicitly, never if they just happen to appear in a
11497 middle of some value chain. */
11498 if (v == head
11499 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11500 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11501 {
42ae5230 11502 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11503 int len;
11504 int num_regs;
11505
a9634178 11506 len = (target_exact_watchpoints
e09342b5
TJB
11507 && is_scalar_type_recursive (vtype))?
11508 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11509
e09342b5
TJB
11510 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11511 if (!num_regs)
2e70b7b9
MS
11512 return 0;
11513 else
e09342b5 11514 found_memory_cnt += num_regs;
2e70b7b9 11515 }
5c44784c 11516 }
c5aa993b 11517 }
5086187c
AC
11518 else if (VALUE_LVAL (v) != not_lval
11519 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11520 return 0; /* These are values from the history (e.g., $1). */
5086187c 11521 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11522 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11523 }
11524
11525 /* The expression itself looks suitable for using a hardware
11526 watchpoint, but give the target machine a chance to reject it. */
11527 return found_memory_cnt;
11528}
11529
8b93c638 11530void
84f4c1fe 11531watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11532{
84f4c1fe 11533 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11534}
11535
06a64a0b
TT
11536/* A helper function that looks for the "-location" argument and then
11537 calls watch_command_1. */
11538
11539static void
11540watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11541{
11542 int just_location = 0;
11543
11544 if (arg
11545 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11546 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11547 {
e9cafbcc 11548 arg = skip_spaces (arg);
06a64a0b
TT
11549 just_location = 1;
11550 }
11551
84f4c1fe 11552 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11553}
8926118c 11554
c5aa993b 11555static void
fba45db2 11556watch_command (char *arg, int from_tty)
c906108c 11557{
06a64a0b 11558 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11559}
11560
8b93c638 11561void
84f4c1fe 11562rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11563{
84f4c1fe 11564 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11565}
8926118c 11566
c5aa993b 11567static void
fba45db2 11568rwatch_command (char *arg, int from_tty)
c906108c 11569{
06a64a0b 11570 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11571}
11572
8b93c638 11573void
84f4c1fe 11574awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11575{
84f4c1fe 11576 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11577}
8926118c 11578
c5aa993b 11579static void
fba45db2 11580awatch_command (char *arg, int from_tty)
c906108c 11581{
06a64a0b 11582 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11583}
c906108c 11584\f
c5aa993b 11585
cfc31633
PA
11586/* Data for the FSM that manages the until(location)/advance commands
11587 in infcmd.c. Here because it uses the mechanisms of
11588 breakpoints. */
c906108c 11589
cfc31633 11590struct until_break_fsm
bfec99b2 11591{
cfc31633
PA
11592 /* The base class. */
11593 struct thread_fsm thread_fsm;
11594
11595 /* The thread that as current when the command was executed. */
11596 int thread;
11597
11598 /* The breakpoint set at the destination location. */
11599 struct breakpoint *location_breakpoint;
11600
11601 /* Breakpoint set at the return address in the caller frame. May be
11602 NULL. */
11603 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11604};
11605
cfc31633
PA
11606static void until_break_fsm_clean_up (struct thread_fsm *self);
11607static int until_break_fsm_should_stop (struct thread_fsm *self);
11608static enum async_reply_reason
11609 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11610
11611/* until_break_fsm's vtable. */
11612
11613static struct thread_fsm_ops until_break_fsm_ops =
11614{
11615 NULL, /* dtor */
11616 until_break_fsm_clean_up,
11617 until_break_fsm_should_stop,
11618 NULL, /* return_value */
11619 until_break_fsm_async_reply_reason,
11620};
11621
11622/* Allocate a new until_break_command_fsm. */
11623
11624static struct until_break_fsm *
11625new_until_break_fsm (int thread,
11626 struct breakpoint *location_breakpoint,
11627 struct breakpoint *caller_breakpoint)
11628{
11629 struct until_break_fsm *sm;
11630
11631 sm = XCNEW (struct until_break_fsm);
11632 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11633
11634 sm->thread = thread;
11635 sm->location_breakpoint = location_breakpoint;
11636 sm->caller_breakpoint = caller_breakpoint;
11637
11638 return sm;
11639}
11640
11641/* Implementation of the 'should_stop' FSM method for the
11642 until(location)/advance commands. */
11643
11644static int
11645until_break_fsm_should_stop (struct thread_fsm *self)
11646{
11647 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11648 struct thread_info *tp = inferior_thread ();
11649
11650 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11651 sm->location_breakpoint) != NULL
11652 || (sm->caller_breakpoint != NULL
11653 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11654 sm->caller_breakpoint) != NULL))
11655 thread_fsm_set_finished (self);
11656
11657 return 1;
11658}
11659
11660/* Implementation of the 'clean_up' FSM method for the
11661 until(location)/advance commands. */
11662
c2c6d25f 11663static void
cfc31633 11664until_break_fsm_clean_up (struct thread_fsm *self)
43ff13b4 11665{
cfc31633 11666 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11667
cfc31633
PA
11668 /* Clean up our temporary breakpoints. */
11669 if (sm->location_breakpoint != NULL)
11670 {
11671 delete_breakpoint (sm->location_breakpoint);
11672 sm->location_breakpoint = NULL;
11673 }
11674 if (sm->caller_breakpoint != NULL)
11675 {
11676 delete_breakpoint (sm->caller_breakpoint);
11677 sm->caller_breakpoint = NULL;
11678 }
11679 delete_longjmp_breakpoint (sm->thread);
11680}
11681
11682/* Implementation of the 'async_reply_reason' FSM method for the
11683 until(location)/advance commands. */
11684
11685static enum async_reply_reason
11686until_break_fsm_async_reply_reason (struct thread_fsm *self)
11687{
11688 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11689}
11690
c906108c 11691void
ae66c1fc 11692until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11693{
11694 struct symtabs_and_lines sals;
11695 struct symtab_and_line sal;
8556afb4
PA
11696 struct frame_info *frame;
11697 struct gdbarch *frame_gdbarch;
11698 struct frame_id stack_frame_id;
11699 struct frame_id caller_frame_id;
cfc31633
PA
11700 struct breakpoint *location_breakpoint;
11701 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11702 struct cleanup *old_chain, *cleanup;
186c406b
TT
11703 int thread;
11704 struct thread_info *tp;
f00aae0f 11705 struct event_location *location;
cfc31633 11706 struct until_break_fsm *sm;
c906108c 11707
70509625 11708 clear_proceed_status (0);
c906108c
SS
11709
11710 /* Set a breakpoint where the user wants it and at return from
4a64f543 11711 this function. */
c5aa993b 11712
f00aae0f
KS
11713 location = string_to_event_location (&arg, current_language);
11714 cleanup = make_cleanup_delete_event_location (location);
11715
1bfeeb0f 11716 if (last_displayed_sal_is_valid ())
c2f4122d 11717 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11718 get_last_displayed_symtab (),
f8eba3c6 11719 get_last_displayed_line ());
c906108c 11720 else
f00aae0f 11721 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11722 NULL, (struct symtab *) NULL, 0);
c5aa993b 11723
c906108c 11724 if (sals.nelts != 1)
8a3fe4f8 11725 error (_("Couldn't get information on specified line."));
c5aa993b 11726
c906108c 11727 sal = sals.sals[0];
4a64f543 11728 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11729
c906108c 11730 if (*arg)
8a3fe4f8 11731 error (_("Junk at end of arguments."));
c5aa993b 11732
c906108c 11733 resolve_sal_pc (&sal);
c5aa993b 11734
186c406b 11735 tp = inferior_thread ();
5d5658a1 11736 thread = tp->global_num;
186c406b 11737
883bc8d1
PA
11738 old_chain = make_cleanup (null_cleanup, NULL);
11739
8556afb4
PA
11740 /* Note linespec handling above invalidates the frame chain.
11741 Installing a breakpoint also invalidates the frame chain (as it
11742 may need to switch threads), so do any frame handling before
11743 that. */
11744
11745 frame = get_selected_frame (NULL);
11746 frame_gdbarch = get_frame_arch (frame);
11747 stack_frame_id = get_stack_frame_id (frame);
11748 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11749
ae66c1fc
EZ
11750 /* Keep within the current frame, or in frames called by the current
11751 one. */
edb3359d 11752
883bc8d1 11753 if (frame_id_p (caller_frame_id))
c906108c 11754 {
883bc8d1 11755 struct symtab_and_line sal2;
cfc31633 11756 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11757
11758 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11759 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11760 caller_gdbarch = frame_unwind_caller_arch (frame);
11761 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11762 sal2,
11763 caller_frame_id,
11764 bp_until);
11765 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11766
883bc8d1 11767 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11768 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11769 }
c5aa993b 11770
c70a6932
JK
11771 /* set_momentary_breakpoint could invalidate FRAME. */
11772 frame = NULL;
11773
883bc8d1
PA
11774 if (anywhere)
11775 /* If the user told us to continue until a specified location,
11776 we don't specify a frame at which we need to stop. */
cfc31633
PA
11777 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11778 null_frame_id, bp_until);
883bc8d1
PA
11779 else
11780 /* Otherwise, specify the selected frame, because we want to stop
11781 only at the very same frame. */
cfc31633
PA
11782 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11783 stack_frame_id, bp_until);
11784 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11785
5d5658a1
PA
11786 sm = new_until_break_fsm (tp->global_num,
11787 location_breakpoint, caller_breakpoint);
cfc31633 11788 tp->thread_fsm = &sm->thread_fsm;
f107f563 11789
cfc31633 11790 discard_cleanups (old_chain);
f107f563 11791
cfc31633 11792 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11793
11794 do_cleanups (cleanup);
c906108c 11795}
ae66c1fc 11796
c906108c
SS
11797/* This function attempts to parse an optional "if <cond>" clause
11798 from the arg string. If one is not found, it returns NULL.
c5aa993b 11799
c906108c
SS
11800 Else, it returns a pointer to the condition string. (It does not
11801 attempt to evaluate the string against a particular block.) And,
11802 it updates arg to point to the first character following the parsed
4a64f543 11803 if clause in the arg string. */
53a5351d 11804
916703c0 11805char *
fba45db2 11806ep_parse_optional_if_clause (char **arg)
c906108c 11807{
c5aa993b
JM
11808 char *cond_string;
11809
11810 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11811 return NULL;
c5aa993b 11812
4a64f543 11813 /* Skip the "if" keyword. */
c906108c 11814 (*arg) += 2;
c5aa993b 11815
c906108c 11816 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11817 condition string. */
e9cafbcc 11818 *arg = skip_spaces (*arg);
c906108c 11819 cond_string = *arg;
c5aa993b 11820
4a64f543
MS
11821 /* Assume that the condition occupies the remainder of the arg
11822 string. */
c906108c 11823 (*arg) += strlen (cond_string);
c5aa993b 11824
c906108c
SS
11825 return cond_string;
11826}
c5aa993b 11827
c906108c
SS
11828/* Commands to deal with catching events, such as signals, exceptions,
11829 process start/exit, etc. */
c5aa993b
JM
11830
11831typedef enum
11832{
44feb3ce
TT
11833 catch_fork_temporary, catch_vfork_temporary,
11834 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11835}
11836catch_fork_kind;
11837
c906108c 11838static void
cc59ec59
MS
11839catch_fork_command_1 (char *arg, int from_tty,
11840 struct cmd_list_element *command)
c906108c 11841{
a6d9a66e 11842 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11843 char *cond_string = NULL;
44feb3ce
TT
11844 catch_fork_kind fork_kind;
11845 int tempflag;
11846
11847 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11848 tempflag = (fork_kind == catch_fork_temporary
11849 || fork_kind == catch_vfork_temporary);
c5aa993b 11850
44feb3ce
TT
11851 if (!arg)
11852 arg = "";
e9cafbcc 11853 arg = skip_spaces (arg);
c5aa993b 11854
c906108c 11855 /* The allowed syntax is:
c5aa993b
JM
11856 catch [v]fork
11857 catch [v]fork if <cond>
11858
4a64f543 11859 First, check if there's an if clause. */
c906108c 11860 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11861
c906108c 11862 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11863 error (_("Junk at end of arguments."));
c5aa993b 11864
c906108c 11865 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11866 and enable reporting of such events. */
c5aa993b
JM
11867 switch (fork_kind)
11868 {
44feb3ce
TT
11869 case catch_fork_temporary:
11870 case catch_fork_permanent:
a6d9a66e 11871 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11872 &catch_fork_breakpoint_ops);
c906108c 11873 break;
44feb3ce
TT
11874 case catch_vfork_temporary:
11875 case catch_vfork_permanent:
a6d9a66e 11876 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11877 &catch_vfork_breakpoint_ops);
c906108c 11878 break;
c5aa993b 11879 default:
8a3fe4f8 11880 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11881 break;
c5aa993b 11882 }
c906108c
SS
11883}
11884
11885static void
cc59ec59
MS
11886catch_exec_command_1 (char *arg, int from_tty,
11887 struct cmd_list_element *command)
c906108c 11888{
b4d90040 11889 struct exec_catchpoint *c;
a6d9a66e 11890 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11891 int tempflag;
c5aa993b 11892 char *cond_string = NULL;
c906108c 11893
44feb3ce
TT
11894 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11895
11896 if (!arg)
11897 arg = "";
e9cafbcc 11898 arg = skip_spaces (arg);
c906108c
SS
11899
11900 /* The allowed syntax is:
c5aa993b
JM
11901 catch exec
11902 catch exec if <cond>
c906108c 11903
4a64f543 11904 First, check if there's an if clause. */
c906108c
SS
11905 cond_string = ep_parse_optional_if_clause (&arg);
11906
11907 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11908 error (_("Junk at end of arguments."));
c906108c 11909
b4d90040
PA
11910 c = XNEW (struct exec_catchpoint);
11911 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11912 &catch_exec_breakpoint_ops);
11913 c->exec_pathname = NULL;
11914
3ea46bff 11915 install_breakpoint (0, &c->base, 1);
c906108c 11916}
c5aa993b 11917
9ac4176b 11918void
28010a5d
PA
11919init_ada_exception_breakpoint (struct breakpoint *b,
11920 struct gdbarch *gdbarch,
11921 struct symtab_and_line sal,
11922 char *addr_string,
c0a91b2b 11923 const struct breakpoint_ops *ops,
28010a5d 11924 int tempflag,
349774ef 11925 int enabled,
28010a5d 11926 int from_tty)
f7f9143b 11927{
f7f9143b
JB
11928 if (from_tty)
11929 {
5af949e3
UW
11930 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11931 if (!loc_gdbarch)
11932 loc_gdbarch = gdbarch;
11933
6c95b8df
PA
11934 describe_other_breakpoints (loc_gdbarch,
11935 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11936 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11937 version for exception catchpoints, because two catchpoints
11938 used for different exception names will use the same address.
11939 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11940 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11941 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11942 the user what type of catchpoint it is. The above is good
11943 enough for now, though. */
11944 }
11945
28010a5d 11946 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11947
349774ef 11948 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11949 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11950 b->location = string_to_event_location (&addr_string,
11951 language_def (language_ada));
f7f9143b 11952 b->language = language_ada;
f7f9143b
JB
11953}
11954
c906108c 11955static void
fba45db2 11956catch_command (char *arg, int from_tty)
c906108c 11957{
44feb3ce 11958 error (_("Catch requires an event name."));
c906108c
SS
11959}
11960\f
11961
11962static void
fba45db2 11963tcatch_command (char *arg, int from_tty)
c906108c 11964{
44feb3ce 11965 error (_("Catch requires an event name."));
c906108c
SS
11966}
11967
8a2c437b
TT
11968/* A qsort comparison function that sorts breakpoints in order. */
11969
11970static int
11971compare_breakpoints (const void *a, const void *b)
11972{
9a3c8263 11973 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11974 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11975 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11976 uintptr_t ub = (uintptr_t) *bb;
11977
11978 if ((*ba)->number < (*bb)->number)
11979 return -1;
11980 else if ((*ba)->number > (*bb)->number)
11981 return 1;
11982
11983 /* Now sort by address, in case we see, e..g, two breakpoints with
11984 the number 0. */
11985 if (ua < ub)
11986 return -1;
94b0e70d 11987 return ua > ub ? 1 : 0;
8a2c437b
TT
11988}
11989
80f8a6eb 11990/* Delete breakpoints by address or line. */
c906108c
SS
11991
11992static void
fba45db2 11993clear_command (char *arg, int from_tty)
c906108c 11994{
8a2c437b 11995 struct breakpoint *b, *prev;
d6e956e5
VP
11996 VEC(breakpoint_p) *found = 0;
11997 int ix;
c906108c
SS
11998 int default_match;
11999 struct symtabs_and_lines sals;
12000 struct symtab_and_line sal;
c906108c 12001 int i;
8a2c437b 12002 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12003
12004 if (arg)
12005 {
39cf75f7
DE
12006 sals = decode_line_with_current_source (arg,
12007 (DECODE_LINE_FUNFIRSTLINE
12008 | DECODE_LINE_LIST_MODE));
cf4ded82 12009 make_cleanup (xfree, sals.sals);
c906108c
SS
12010 default_match = 0;
12011 }
12012 else
12013 {
8d749320 12014 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12015 make_cleanup (xfree, sals.sals);
4a64f543 12016 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12017
12018 /* Set sal's line, symtab, pc, and pspace to the values
12019 corresponding to the last call to print_frame_info. If the
12020 codepoint is not valid, this will set all the fields to 0. */
12021 get_last_displayed_sal (&sal);
c906108c 12022 if (sal.symtab == 0)
8a3fe4f8 12023 error (_("No source file specified."));
c906108c
SS
12024
12025 sals.sals[0] = sal;
12026 sals.nelts = 1;
12027
12028 default_match = 1;
12029 }
12030
4a64f543
MS
12031 /* We don't call resolve_sal_pc here. That's not as bad as it
12032 seems, because all existing breakpoints typically have both
12033 file/line and pc set. So, if clear is given file/line, we can
12034 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12035
12036 We only support clearing given the address explicitly
12037 present in breakpoint table. Say, we've set breakpoint
4a64f543 12038 at file:line. There were several PC values for that file:line,
ed0616c6 12039 due to optimization, all in one block.
4a64f543
MS
12040
12041 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12042 PC corresponding to the same file:line, the breakpoint won't
12043 be cleared. We probably can still clear the breakpoint, but
12044 since the other PC value is never presented to user, user
12045 can only find it by guessing, and it does not seem important
12046 to support that. */
12047
4a64f543
MS
12048 /* For each line spec given, delete bps which correspond to it. Do
12049 it in two passes, solely to preserve the current behavior that
12050 from_tty is forced true if we delete more than one
12051 breakpoint. */
c906108c 12052
80f8a6eb 12053 found = NULL;
8a2c437b 12054 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12055 for (i = 0; i < sals.nelts; i++)
12056 {
05cba821
JK
12057 const char *sal_fullname;
12058
c906108c 12059 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12060 If line given (pc == 0), clear all bpts on specified line.
12061 If defaulting, clear all bpts on default line
c906108c 12062 or at default pc.
c5aa993b
JM
12063
12064 defaulting sal.pc != 0 tests to do
12065
12066 0 1 pc
12067 1 1 pc _and_ line
12068 0 0 line
12069 1 0 <can't happen> */
c906108c
SS
12070
12071 sal = sals.sals[i];
05cba821
JK
12072 sal_fullname = (sal.symtab == NULL
12073 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12074
4a64f543 12075 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12076 ALL_BREAKPOINTS (b)
c5aa993b 12077 {
0d381245 12078 int match = 0;
4a64f543 12079 /* Are we going to delete b? */
cc60f2e3 12080 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12081 {
12082 struct bp_location *loc = b->loc;
12083 for (; loc; loc = loc->next)
12084 {
f8eba3c6
TT
12085 /* If the user specified file:line, don't allow a PC
12086 match. This matches historical gdb behavior. */
12087 int pc_match = (!sal.explicit_line
12088 && sal.pc
12089 && (loc->pspace == sal.pspace)
12090 && (loc->address == sal.pc)
12091 && (!section_is_overlay (loc->section)
12092 || loc->section == sal.section));
4aac40c8
TT
12093 int line_match = 0;
12094
12095 if ((default_match || sal.explicit_line)
2f202fde 12096 && loc->symtab != NULL
05cba821 12097 && sal_fullname != NULL
4aac40c8 12098 && sal.pspace == loc->pspace
05cba821
JK
12099 && loc->line_number == sal.line
12100 && filename_cmp (symtab_to_fullname (loc->symtab),
12101 sal_fullname) == 0)
12102 line_match = 1;
4aac40c8 12103
0d381245
VP
12104 if (pc_match || line_match)
12105 {
12106 match = 1;
12107 break;
12108 }
12109 }
12110 }
12111
12112 if (match)
d6e956e5 12113 VEC_safe_push(breakpoint_p, found, b);
c906108c 12114 }
80f8a6eb 12115 }
8a2c437b 12116
80f8a6eb 12117 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12118 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12119 {
12120 if (arg)
8a3fe4f8 12121 error (_("No breakpoint at %s."), arg);
80f8a6eb 12122 else
8a3fe4f8 12123 error (_("No breakpoint at this line."));
80f8a6eb 12124 }
c906108c 12125
8a2c437b
TT
12126 /* Remove duplicates from the vec. */
12127 qsort (VEC_address (breakpoint_p, found),
12128 VEC_length (breakpoint_p, found),
12129 sizeof (breakpoint_p),
12130 compare_breakpoints);
12131 prev = VEC_index (breakpoint_p, found, 0);
12132 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12133 {
12134 if (b == prev)
12135 {
12136 VEC_ordered_remove (breakpoint_p, found, ix);
12137 --ix;
12138 }
12139 }
12140
d6e956e5 12141 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12142 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12143 if (from_tty)
a3f17187 12144 {
d6e956e5 12145 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12146 printf_unfiltered (_("Deleted breakpoint "));
12147 else
12148 printf_unfiltered (_("Deleted breakpoints "));
12149 }
d6e956e5
VP
12150
12151 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12152 {
c5aa993b 12153 if (from_tty)
d6e956e5
VP
12154 printf_unfiltered ("%d ", b->number);
12155 delete_breakpoint (b);
c906108c 12156 }
80f8a6eb
MS
12157 if (from_tty)
12158 putchar_unfiltered ('\n');
8a2c437b
TT
12159
12160 do_cleanups (cleanups);
c906108c
SS
12161}
12162\f
12163/* Delete breakpoint in BS if they are `delete' breakpoints and
12164 all breakpoints that are marked for deletion, whether hit or not.
12165 This is called after any breakpoint is hit, or after errors. */
12166
12167void
fba45db2 12168breakpoint_auto_delete (bpstat bs)
c906108c 12169{
35df4500 12170 struct breakpoint *b, *b_tmp;
c906108c
SS
12171
12172 for (; bs; bs = bs->next)
f431efe5
PA
12173 if (bs->breakpoint_at
12174 && bs->breakpoint_at->disposition == disp_del
c906108c 12175 && bs->stop)
f431efe5 12176 delete_breakpoint (bs->breakpoint_at);
c906108c 12177
35df4500 12178 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12179 {
b5de0fa7 12180 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12181 delete_breakpoint (b);
12182 }
c906108c
SS
12183}
12184
4a64f543
MS
12185/* A comparison function for bp_location AP and BP being interfaced to
12186 qsort. Sort elements primarily by their ADDRESS (no matter what
12187 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12188 secondarily by ordering first permanent elements and
4a64f543 12189 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12190 qsort being an unstable algorithm. */
876fa593
JK
12191
12192static int
494cfb0f 12193bp_location_compare (const void *ap, const void *bp)
876fa593 12194{
9a3c8263
SM
12195 const struct bp_location *a = *(const struct bp_location **) ap;
12196 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12197
12198 if (a->address != b->address)
12199 return (a->address > b->address) - (a->address < b->address);
12200
dea2aa5f
LM
12201 /* Sort locations at the same address by their pspace number, keeping
12202 locations of the same inferior (in a multi-inferior environment)
12203 grouped. */
12204
12205 if (a->pspace->num != b->pspace->num)
12206 return ((a->pspace->num > b->pspace->num)
12207 - (a->pspace->num < b->pspace->num));
12208
876fa593 12209 /* Sort permanent breakpoints first. */
1a853c52
PA
12210 if (a->permanent != b->permanent)
12211 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12212
c56a97f9
JK
12213 /* Make the internal GDB representation stable across GDB runs
12214 where A and B memory inside GDB can differ. Breakpoint locations of
12215 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12216
12217 if (a->owner->number != b->owner->number)
c56a97f9
JK
12218 return ((a->owner->number > b->owner->number)
12219 - (a->owner->number < b->owner->number));
876fa593
JK
12220
12221 return (a > b) - (a < b);
12222}
12223
876fa593 12224/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12225 bp_location_shadow_len_after_address_max according to the current
12226 content of the bp_location array. */
f7545552
TT
12227
12228static void
876fa593 12229bp_location_target_extensions_update (void)
f7545552 12230{
876fa593
JK
12231 struct bp_location *bl, **blp_tmp;
12232
12233 bp_location_placed_address_before_address_max = 0;
12234 bp_location_shadow_len_after_address_max = 0;
12235
12236 ALL_BP_LOCATIONS (bl, blp_tmp)
12237 {
12238 CORE_ADDR start, end, addr;
12239
12240 if (!bp_location_has_shadow (bl))
12241 continue;
12242
12243 start = bl->target_info.placed_address;
12244 end = start + bl->target_info.shadow_len;
12245
12246 gdb_assert (bl->address >= start);
12247 addr = bl->address - start;
12248 if (addr > bp_location_placed_address_before_address_max)
12249 bp_location_placed_address_before_address_max = addr;
12250
12251 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12252
12253 gdb_assert (bl->address < end);
12254 addr = end - bl->address;
12255 if (addr > bp_location_shadow_len_after_address_max)
12256 bp_location_shadow_len_after_address_max = addr;
12257 }
f7545552
TT
12258}
12259
1e4d1764
YQ
12260/* Download tracepoint locations if they haven't been. */
12261
12262static void
12263download_tracepoint_locations (void)
12264{
7ed2c994 12265 struct breakpoint *b;
1e4d1764 12266 struct cleanup *old_chain;
dd2e65cc 12267 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12268
12269 old_chain = save_current_space_and_thread ();
12270
7ed2c994 12271 ALL_TRACEPOINTS (b)
1e4d1764 12272 {
7ed2c994 12273 struct bp_location *bl;
1e4d1764 12274 struct tracepoint *t;
f2a8bc8a 12275 int bp_location_downloaded = 0;
1e4d1764 12276
7ed2c994 12277 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12278 ? !may_insert_fast_tracepoints
12279 : !may_insert_tracepoints))
12280 continue;
12281
dd2e65cc
YQ
12282 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12283 {
12284 if (target_can_download_tracepoint ())
12285 can_download_tracepoint = TRIBOOL_TRUE;
12286 else
12287 can_download_tracepoint = TRIBOOL_FALSE;
12288 }
12289
12290 if (can_download_tracepoint == TRIBOOL_FALSE)
12291 break;
12292
7ed2c994
YQ
12293 for (bl = b->loc; bl; bl = bl->next)
12294 {
12295 /* In tracepoint, locations are _never_ duplicated, so
12296 should_be_inserted is equivalent to
12297 unduplicated_should_be_inserted. */
12298 if (!should_be_inserted (bl) || bl->inserted)
12299 continue;
1e4d1764 12300
7ed2c994 12301 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12302
7ed2c994 12303 target_download_tracepoint (bl);
1e4d1764 12304
7ed2c994 12305 bl->inserted = 1;
f2a8bc8a 12306 bp_location_downloaded = 1;
7ed2c994
YQ
12307 }
12308 t = (struct tracepoint *) b;
12309 t->number_on_target = b->number;
f2a8bc8a
YQ
12310 if (bp_location_downloaded)
12311 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12312 }
12313
12314 do_cleanups (old_chain);
12315}
12316
934709f0
PW
12317/* Swap the insertion/duplication state between two locations. */
12318
12319static void
12320swap_insertion (struct bp_location *left, struct bp_location *right)
12321{
12322 const int left_inserted = left->inserted;
12323 const int left_duplicate = left->duplicate;
b775012e 12324 const int left_needs_update = left->needs_update;
934709f0
PW
12325 const struct bp_target_info left_target_info = left->target_info;
12326
1e4d1764
YQ
12327 /* Locations of tracepoints can never be duplicated. */
12328 if (is_tracepoint (left->owner))
12329 gdb_assert (!left->duplicate);
12330 if (is_tracepoint (right->owner))
12331 gdb_assert (!right->duplicate);
12332
934709f0
PW
12333 left->inserted = right->inserted;
12334 left->duplicate = right->duplicate;
b775012e 12335 left->needs_update = right->needs_update;
934709f0
PW
12336 left->target_info = right->target_info;
12337 right->inserted = left_inserted;
12338 right->duplicate = left_duplicate;
b775012e 12339 right->needs_update = left_needs_update;
934709f0
PW
12340 right->target_info = left_target_info;
12341}
12342
b775012e
LM
12343/* Force the re-insertion of the locations at ADDRESS. This is called
12344 once a new/deleted/modified duplicate location is found and we are evaluating
12345 conditions on the target's side. Such conditions need to be updated on
12346 the target. */
12347
12348static void
12349force_breakpoint_reinsertion (struct bp_location *bl)
12350{
12351 struct bp_location **locp = NULL, **loc2p;
12352 struct bp_location *loc;
12353 CORE_ADDR address = 0;
12354 int pspace_num;
12355
12356 address = bl->address;
12357 pspace_num = bl->pspace->num;
12358
12359 /* This is only meaningful if the target is
12360 evaluating conditions and if the user has
12361 opted for condition evaluation on the target's
12362 side. */
12363 if (gdb_evaluates_breakpoint_condition_p ()
12364 || !target_supports_evaluation_of_breakpoint_conditions ())
12365 return;
12366
12367 /* Flag all breakpoint locations with this address and
12368 the same program space as the location
12369 as "its condition has changed". We need to
12370 update the conditions on the target's side. */
12371 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12372 {
12373 loc = *loc2p;
12374
12375 if (!is_breakpoint (loc->owner)
12376 || pspace_num != loc->pspace->num)
12377 continue;
12378
12379 /* Flag the location appropriately. We use a different state to
12380 let everyone know that we already updated the set of locations
12381 with addr bl->address and program space bl->pspace. This is so
12382 we don't have to keep calling these functions just to mark locations
12383 that have already been marked. */
12384 loc->condition_changed = condition_updated;
12385
12386 /* Free the agent expression bytecode as well. We will compute
12387 it later on. */
12388 if (loc->cond_bytecode)
12389 {
12390 free_agent_expr (loc->cond_bytecode);
12391 loc->cond_bytecode = NULL;
12392 }
12393 }
12394}
44702360
PA
12395/* Called whether new breakpoints are created, or existing breakpoints
12396 deleted, to update the global location list and recompute which
12397 locations are duplicate of which.
b775012e 12398
04086b45
PA
12399 The INSERT_MODE flag determines whether locations may not, may, or
12400 shall be inserted now. See 'enum ugll_insert_mode' for more
12401 info. */
b60e7edf 12402
0d381245 12403static void
44702360 12404update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12405{
74960c60 12406 struct breakpoint *b;
876fa593 12407 struct bp_location **locp, *loc;
f7545552 12408 struct cleanup *cleanups;
b775012e
LM
12409 /* Last breakpoint location address that was marked for update. */
12410 CORE_ADDR last_addr = 0;
12411 /* Last breakpoint location program space that was marked for update. */
12412 int last_pspace_num = -1;
f7545552 12413
2d134ed3
PA
12414 /* Used in the duplicates detection below. When iterating over all
12415 bp_locations, points to the first bp_location of a given address.
12416 Breakpoints and watchpoints of different types are never
12417 duplicates of each other. Keep one pointer for each type of
12418 breakpoint/watchpoint, so we only need to loop over all locations
12419 once. */
12420 struct bp_location *bp_loc_first; /* breakpoint */
12421 struct bp_location *wp_loc_first; /* hardware watchpoint */
12422 struct bp_location *awp_loc_first; /* access watchpoint */
12423 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12424
4a64f543
MS
12425 /* Saved former bp_location array which we compare against the newly
12426 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12427 struct bp_location **old_location, **old_locp;
12428 unsigned old_location_count;
12429
12430 old_location = bp_location;
12431 old_location_count = bp_location_count;
12432 bp_location = NULL;
12433 bp_location_count = 0;
12434 cleanups = make_cleanup (xfree, old_location);
0d381245 12435
74960c60 12436 ALL_BREAKPOINTS (b)
876fa593
JK
12437 for (loc = b->loc; loc; loc = loc->next)
12438 bp_location_count++;
12439
8d749320 12440 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12441 locp = bp_location;
12442 ALL_BREAKPOINTS (b)
12443 for (loc = b->loc; loc; loc = loc->next)
12444 *locp++ = loc;
12445 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12446 bp_location_compare);
876fa593
JK
12447
12448 bp_location_target_extensions_update ();
74960c60 12449
4a64f543
MS
12450 /* Identify bp_location instances that are no longer present in the
12451 new list, and therefore should be freed. Note that it's not
12452 necessary that those locations should be removed from inferior --
12453 if there's another location at the same address (previously
12454 marked as duplicate), we don't need to remove/insert the
12455 location.
876fa593 12456
4a64f543
MS
12457 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12458 and former bp_location array state respectively. */
876fa593
JK
12459
12460 locp = bp_location;
12461 for (old_locp = old_location; old_locp < old_location + old_location_count;
12462 old_locp++)
74960c60 12463 {
876fa593 12464 struct bp_location *old_loc = *old_locp;
c7d46a38 12465 struct bp_location **loc2p;
876fa593 12466
e5dd4106 12467 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12468 not, we have to free it. */
c7d46a38 12469 int found_object = 0;
20874c92
VP
12470 /* Tells if the location should remain inserted in the target. */
12471 int keep_in_target = 0;
12472 int removed = 0;
876fa593 12473
4a64f543
MS
12474 /* Skip LOCP entries which will definitely never be needed.
12475 Stop either at or being the one matching OLD_LOC. */
876fa593 12476 while (locp < bp_location + bp_location_count
c7d46a38 12477 && (*locp)->address < old_loc->address)
876fa593 12478 locp++;
c7d46a38
PA
12479
12480 for (loc2p = locp;
12481 (loc2p < bp_location + bp_location_count
12482 && (*loc2p)->address == old_loc->address);
12483 loc2p++)
12484 {
b775012e
LM
12485 /* Check if this is a new/duplicated location or a duplicated
12486 location that had its condition modified. If so, we want to send
12487 its condition to the target if evaluation of conditions is taking
12488 place there. */
12489 if ((*loc2p)->condition_changed == condition_modified
12490 && (last_addr != old_loc->address
12491 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12492 {
b775012e
LM
12493 force_breakpoint_reinsertion (*loc2p);
12494 last_pspace_num = old_loc->pspace->num;
c7d46a38 12495 }
b775012e
LM
12496
12497 if (*loc2p == old_loc)
12498 found_object = 1;
c7d46a38 12499 }
74960c60 12500
b775012e
LM
12501 /* We have already handled this address, update it so that we don't
12502 have to go through updates again. */
12503 last_addr = old_loc->address;
12504
12505 /* Target-side condition evaluation: Handle deleted locations. */
12506 if (!found_object)
12507 force_breakpoint_reinsertion (old_loc);
12508
4a64f543
MS
12509 /* If this location is no longer present, and inserted, look if
12510 there's maybe a new location at the same address. If so,
12511 mark that one inserted, and don't remove this one. This is
12512 needed so that we don't have a time window where a breakpoint
12513 at certain location is not inserted. */
74960c60 12514
876fa593 12515 if (old_loc->inserted)
0d381245 12516 {
4a64f543
MS
12517 /* If the location is inserted now, we might have to remove
12518 it. */
74960c60 12519
876fa593 12520 if (found_object && should_be_inserted (old_loc))
74960c60 12521 {
4a64f543
MS
12522 /* The location is still present in the location list,
12523 and still should be inserted. Don't do anything. */
20874c92 12524 keep_in_target = 1;
74960c60
VP
12525 }
12526 else
12527 {
b775012e
LM
12528 /* This location still exists, but it won't be kept in the
12529 target since it may have been disabled. We proceed to
12530 remove its target-side condition. */
12531
4a64f543
MS
12532 /* The location is either no longer present, or got
12533 disabled. See if there's another location at the
12534 same address, in which case we don't need to remove
12535 this one from the target. */
876fa593 12536
2bdf28a0 12537 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12538 if (breakpoint_address_is_meaningful (old_loc->owner))
12539 {
876fa593 12540 for (loc2p = locp;
c7d46a38
PA
12541 (loc2p < bp_location + bp_location_count
12542 && (*loc2p)->address == old_loc->address);
876fa593
JK
12543 loc2p++)
12544 {
12545 struct bp_location *loc2 = *loc2p;
12546
2d134ed3 12547 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12548 {
85d721b8
PA
12549 /* Read watchpoint locations are switched to
12550 access watchpoints, if the former are not
12551 supported, but the latter are. */
12552 if (is_hardware_watchpoint (old_loc->owner))
12553 {
12554 gdb_assert (is_hardware_watchpoint (loc2->owner));
12555 loc2->watchpoint_type = old_loc->watchpoint_type;
12556 }
12557
934709f0
PW
12558 /* loc2 is a duplicated location. We need to check
12559 if it should be inserted in case it will be
12560 unduplicated. */
12561 if (loc2 != old_loc
12562 && unduplicated_should_be_inserted (loc2))
c7d46a38 12563 {
934709f0 12564 swap_insertion (old_loc, loc2);
c7d46a38
PA
12565 keep_in_target = 1;
12566 break;
12567 }
876fa593
JK
12568 }
12569 }
12570 }
74960c60
VP
12571 }
12572
20874c92
VP
12573 if (!keep_in_target)
12574 {
876fa593 12575 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12576 {
4a64f543
MS
12577 /* This is just about all we can do. We could keep
12578 this location on the global list, and try to
12579 remove it next time, but there's no particular
12580 reason why we will succeed next time.
20874c92 12581
4a64f543
MS
12582 Note that at this point, old_loc->owner is still
12583 valid, as delete_breakpoint frees the breakpoint
12584 only after calling us. */
3e43a32a
MS
12585 printf_filtered (_("warning: Error removing "
12586 "breakpoint %d\n"),
876fa593 12587 old_loc->owner->number);
20874c92
VP
12588 }
12589 removed = 1;
12590 }
0d381245 12591 }
74960c60
VP
12592
12593 if (!found_object)
1c5cfe86 12594 {
fbea99ea 12595 if (removed && target_is_non_stop_p ()
1cf4d951 12596 && need_moribund_for_location_type (old_loc))
20874c92 12597 {
db82e815
PA
12598 /* This location was removed from the target. In
12599 non-stop mode, a race condition is possible where
12600 we've removed a breakpoint, but stop events for that
12601 breakpoint are already queued and will arrive later.
12602 We apply an heuristic to be able to distinguish such
12603 SIGTRAPs from other random SIGTRAPs: we keep this
12604 breakpoint location for a bit, and will retire it
12605 after we see some number of events. The theory here
12606 is that reporting of events should, "on the average",
12607 be fair, so after a while we'll see events from all
12608 threads that have anything of interest, and no longer
12609 need to keep this breakpoint location around. We
12610 don't hold locations forever so to reduce chances of
12611 mistaking a non-breakpoint SIGTRAP for a breakpoint
12612 SIGTRAP.
12613
12614 The heuristic failing can be disastrous on
12615 decr_pc_after_break targets.
12616
12617 On decr_pc_after_break targets, like e.g., x86-linux,
12618 if we fail to recognize a late breakpoint SIGTRAP,
12619 because events_till_retirement has reached 0 too
12620 soon, we'll fail to do the PC adjustment, and report
12621 a random SIGTRAP to the user. When the user resumes
12622 the inferior, it will most likely immediately crash
2dec564e 12623 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12624 corrupted, because of being resumed e.g., in the
12625 middle of a multi-byte instruction, or skipped a
12626 one-byte instruction. This was actually seen happen
12627 on native x86-linux, and should be less rare on
12628 targets that do not support new thread events, like
12629 remote, due to the heuristic depending on
12630 thread_count.
12631
12632 Mistaking a random SIGTRAP for a breakpoint trap
12633 causes similar symptoms (PC adjustment applied when
12634 it shouldn't), but then again, playing with SIGTRAPs
12635 behind the debugger's back is asking for trouble.
12636
12637 Since hardware watchpoint traps are always
12638 distinguishable from other traps, so we don't need to
12639 apply keep hardware watchpoint moribund locations
12640 around. We simply always ignore hardware watchpoint
12641 traps we can no longer explain. */
12642
876fa593
JK
12643 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12644 old_loc->owner = NULL;
20874c92 12645
876fa593 12646 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12647 }
12648 else
f431efe5
PA
12649 {
12650 old_loc->owner = NULL;
12651 decref_bp_location (&old_loc);
12652 }
20874c92 12653 }
74960c60 12654 }
1c5cfe86 12655
348d480f
PA
12656 /* Rescan breakpoints at the same address and section, marking the
12657 first one as "first" and any others as "duplicates". This is so
12658 that the bpt instruction is only inserted once. If we have a
12659 permanent breakpoint at the same place as BPT, make that one the
12660 official one, and the rest as duplicates. Permanent breakpoints
12661 are sorted first for the same address.
12662
12663 Do the same for hardware watchpoints, but also considering the
12664 watchpoint's type (regular/access/read) and length. */
12665
12666 bp_loc_first = NULL;
12667 wp_loc_first = NULL;
12668 awp_loc_first = NULL;
12669 rwp_loc_first = NULL;
12670 ALL_BP_LOCATIONS (loc, locp)
12671 {
12672 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12673 non-NULL. */
348d480f 12674 struct bp_location **loc_first_p;
d3fbdd86 12675 b = loc->owner;
348d480f 12676
6f380991 12677 if (!unduplicated_should_be_inserted (loc)
348d480f 12678 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12679 /* Don't detect duplicate for tracepoint locations because they are
12680 never duplicated. See the comments in field `duplicate' of
12681 `struct bp_location'. */
348d480f 12682 || is_tracepoint (b))
b775012e
LM
12683 {
12684 /* Clear the condition modification flag. */
12685 loc->condition_changed = condition_unchanged;
12686 continue;
12687 }
348d480f 12688
348d480f
PA
12689 if (b->type == bp_hardware_watchpoint)
12690 loc_first_p = &wp_loc_first;
12691 else if (b->type == bp_read_watchpoint)
12692 loc_first_p = &rwp_loc_first;
12693 else if (b->type == bp_access_watchpoint)
12694 loc_first_p = &awp_loc_first;
12695 else
12696 loc_first_p = &bp_loc_first;
12697
12698 if (*loc_first_p == NULL
12699 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12700 || !breakpoint_locations_match (loc, *loc_first_p))
12701 {
12702 *loc_first_p = loc;
12703 loc->duplicate = 0;
b775012e
LM
12704
12705 if (is_breakpoint (loc->owner) && loc->condition_changed)
12706 {
12707 loc->needs_update = 1;
12708 /* Clear the condition modification flag. */
12709 loc->condition_changed = condition_unchanged;
12710 }
348d480f
PA
12711 continue;
12712 }
12713
934709f0
PW
12714
12715 /* This and the above ensure the invariant that the first location
12716 is not duplicated, and is the inserted one.
12717 All following are marked as duplicated, and are not inserted. */
12718 if (loc->inserted)
12719 swap_insertion (loc, *loc_first_p);
348d480f
PA
12720 loc->duplicate = 1;
12721
b775012e
LM
12722 /* Clear the condition modification flag. */
12723 loc->condition_changed = condition_unchanged;
348d480f
PA
12724 }
12725
a25a5a45 12726 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12727 {
04086b45 12728 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12729 insert_breakpoint_locations ();
12730 else
12731 {
44702360
PA
12732 /* Even though the caller told us to not insert new
12733 locations, we may still need to update conditions on the
12734 target's side of breakpoints that were already inserted
12735 if the target is evaluating breakpoint conditions. We
b775012e
LM
12736 only update conditions for locations that are marked
12737 "needs_update". */
12738 update_inserted_breakpoint_locations ();
12739 }
12740 }
348d480f 12741
04086b45 12742 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12743 download_tracepoint_locations ();
12744
348d480f
PA
12745 do_cleanups (cleanups);
12746}
12747
12748void
12749breakpoint_retire_moribund (void)
12750{
12751 struct bp_location *loc;
12752 int ix;
12753
12754 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12755 if (--(loc->events_till_retirement) == 0)
12756 {
12757 decref_bp_location (&loc);
12758 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12759 --ix;
12760 }
12761}
12762
12763static void
44702360 12764update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12765{
348d480f 12766
492d29ea
PA
12767 TRY
12768 {
12769 update_global_location_list (insert_mode);
12770 }
12771 CATCH (e, RETURN_MASK_ERROR)
12772 {
12773 }
12774 END_CATCH
348d480f
PA
12775}
12776
12777/* Clear BKP from a BPS. */
12778
12779static void
12780bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12781{
12782 bpstat bs;
12783
12784 for (bs = bps; bs; bs = bs->next)
12785 if (bs->breakpoint_at == bpt)
12786 {
12787 bs->breakpoint_at = NULL;
12788 bs->old_val = NULL;
12789 /* bs->commands will be freed later. */
12790 }
12791}
12792
12793/* Callback for iterate_over_threads. */
12794static int
12795bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12796{
9a3c8263 12797 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12798
12799 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12800 return 0;
12801}
12802
12803/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12804 callbacks. */
12805
12806static void
12807say_where (struct breakpoint *b)
12808{
12809 struct value_print_options opts;
12810
12811 get_user_print_options (&opts);
12812
12813 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12814 single string. */
12815 if (b->loc == NULL)
12816 {
f00aae0f
KS
12817 /* For pending locations, the output differs slightly based
12818 on b->extra_string. If this is non-NULL, it contains either
12819 a condition or dprintf arguments. */
12820 if (b->extra_string == NULL)
12821 {
12822 printf_filtered (_(" (%s) pending."),
12823 event_location_to_string (b->location));
12824 }
12825 else if (b->type == bp_dprintf)
12826 {
12827 printf_filtered (_(" (%s,%s) pending."),
12828 event_location_to_string (b->location),
12829 b->extra_string);
12830 }
12831 else
12832 {
12833 printf_filtered (_(" (%s %s) pending."),
12834 event_location_to_string (b->location),
12835 b->extra_string);
12836 }
348d480f
PA
12837 }
12838 else
12839 {
2f202fde 12840 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12841 {
12842 printf_filtered (" at ");
12843 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12844 gdb_stdout);
12845 }
2f202fde 12846 if (b->loc->symtab != NULL)
f8eba3c6
TT
12847 {
12848 /* If there is a single location, we can print the location
12849 more nicely. */
12850 if (b->loc->next == NULL)
12851 printf_filtered (": file %s, line %d.",
05cba821
JK
12852 symtab_to_filename_for_display (b->loc->symtab),
12853 b->loc->line_number);
f8eba3c6
TT
12854 else
12855 /* This is not ideal, but each location may have a
12856 different file name, and this at least reflects the
12857 real situation somewhat. */
f00aae0f
KS
12858 printf_filtered (": %s.",
12859 event_location_to_string (b->location));
f8eba3c6 12860 }
348d480f
PA
12861
12862 if (b->loc->next)
12863 {
12864 struct bp_location *loc = b->loc;
12865 int n = 0;
12866 for (; loc; loc = loc->next)
12867 ++n;
12868 printf_filtered (" (%d locations)", n);
12869 }
12870 }
12871}
12872
348d480f
PA
12873/* Default bp_location_ops methods. */
12874
12875static void
12876bp_location_dtor (struct bp_location *self)
12877{
12878 xfree (self->cond);
b775012e
LM
12879 if (self->cond_bytecode)
12880 free_agent_expr (self->cond_bytecode);
348d480f 12881 xfree (self->function_name);
8b4f3082
PA
12882
12883 VEC_free (agent_expr_p, self->target_info.conditions);
12884 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12885}
12886
12887static const struct bp_location_ops bp_location_ops =
12888{
12889 bp_location_dtor
12890};
12891
2060206e
PA
12892/* Default breakpoint_ops methods all breakpoint_ops ultimately
12893 inherit from. */
348d480f 12894
2060206e
PA
12895static void
12896base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12897{
12898 decref_counted_command_line (&self->commands);
12899 xfree (self->cond_string);
fb81d016 12900 xfree (self->extra_string);
f8eba3c6 12901 xfree (self->filter);
f00aae0f
KS
12902 delete_event_location (self->location);
12903 delete_event_location (self->location_range_end);
348d480f
PA
12904}
12905
2060206e
PA
12906static struct bp_location *
12907base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12908{
12909 struct bp_location *loc;
12910
12911 loc = XNEW (struct bp_location);
12912 init_bp_location (loc, &bp_location_ops, self);
12913 return loc;
12914}
12915
2060206e
PA
12916static void
12917base_breakpoint_re_set (struct breakpoint *b)
12918{
12919 /* Nothing to re-set. */
12920}
12921
12922#define internal_error_pure_virtual_called() \
12923 gdb_assert_not_reached ("pure virtual function called")
12924
12925static int
12926base_breakpoint_insert_location (struct bp_location *bl)
12927{
12928 internal_error_pure_virtual_called ();
12929}
12930
12931static int
12932base_breakpoint_remove_location (struct bp_location *bl)
12933{
12934 internal_error_pure_virtual_called ();
12935}
12936
12937static int
12938base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12939 struct address_space *aspace,
09ac7c10
TT
12940 CORE_ADDR bp_addr,
12941 const struct target_waitstatus *ws)
2060206e
PA
12942{
12943 internal_error_pure_virtual_called ();
12944}
12945
12946static void
12947base_breakpoint_check_status (bpstat bs)
12948{
12949 /* Always stop. */
12950}
12951
12952/* A "works_in_software_mode" breakpoint_ops method that just internal
12953 errors. */
12954
12955static int
12956base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12957{
12958 internal_error_pure_virtual_called ();
12959}
12960
12961/* A "resources_needed" breakpoint_ops method that just internal
12962 errors. */
12963
12964static int
12965base_breakpoint_resources_needed (const struct bp_location *bl)
12966{
12967 internal_error_pure_virtual_called ();
12968}
12969
12970static enum print_stop_action
12971base_breakpoint_print_it (bpstat bs)
12972{
12973 internal_error_pure_virtual_called ();
12974}
12975
12976static void
12977base_breakpoint_print_one_detail (const struct breakpoint *self,
12978 struct ui_out *uiout)
12979{
12980 /* nothing */
12981}
12982
12983static void
12984base_breakpoint_print_mention (struct breakpoint *b)
12985{
12986 internal_error_pure_virtual_called ();
12987}
12988
12989static void
12990base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12991{
12992 internal_error_pure_virtual_called ();
12993}
12994
983af33b 12995static void
f00aae0f
KS
12996base_breakpoint_create_sals_from_location
12997 (const struct event_location *location,
12998 struct linespec_result *canonical,
12999 enum bptype type_wanted)
983af33b
SDJ
13000{
13001 internal_error_pure_virtual_called ();
13002}
13003
13004static void
13005base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13006 struct linespec_result *c,
983af33b 13007 char *cond_string,
e7e0cddf 13008 char *extra_string,
983af33b
SDJ
13009 enum bptype type_wanted,
13010 enum bpdisp disposition,
13011 int thread,
13012 int task, int ignore_count,
13013 const struct breakpoint_ops *o,
13014 int from_tty, int enabled,
44f238bb 13015 int internal, unsigned flags)
983af33b
SDJ
13016{
13017 internal_error_pure_virtual_called ();
13018}
13019
13020static void
f00aae0f
KS
13021base_breakpoint_decode_location (struct breakpoint *b,
13022 const struct event_location *location,
c2f4122d 13023 struct program_space *search_pspace,
983af33b
SDJ
13024 struct symtabs_and_lines *sals)
13025{
13026 internal_error_pure_virtual_called ();
13027}
13028
ab04a2af
TT
13029/* The default 'explains_signal' method. */
13030
47591c29 13031static int
427cd150 13032base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13033{
47591c29 13034 return 1;
ab04a2af
TT
13035}
13036
9d6e6e84
HZ
13037/* The default "after_condition_true" method. */
13038
13039static void
13040base_breakpoint_after_condition_true (struct bpstats *bs)
13041{
13042 /* Nothing to do. */
13043}
13044
ab04a2af 13045struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13046{
13047 base_breakpoint_dtor,
13048 base_breakpoint_allocate_location,
13049 base_breakpoint_re_set,
13050 base_breakpoint_insert_location,
13051 base_breakpoint_remove_location,
13052 base_breakpoint_breakpoint_hit,
13053 base_breakpoint_check_status,
13054 base_breakpoint_resources_needed,
13055 base_breakpoint_works_in_software_mode,
13056 base_breakpoint_print_it,
13057 NULL,
13058 base_breakpoint_print_one_detail,
13059 base_breakpoint_print_mention,
983af33b 13060 base_breakpoint_print_recreate,
5f700d83 13061 base_breakpoint_create_sals_from_location,
983af33b 13062 base_breakpoint_create_breakpoints_sal,
5f700d83 13063 base_breakpoint_decode_location,
9d6e6e84
HZ
13064 base_breakpoint_explains_signal,
13065 base_breakpoint_after_condition_true,
2060206e
PA
13066};
13067
13068/* Default breakpoint_ops methods. */
13069
13070static void
348d480f
PA
13071bkpt_re_set (struct breakpoint *b)
13072{
06edf0c0 13073 /* FIXME: is this still reachable? */
9ef9e6a6 13074 if (breakpoint_event_location_empty_p (b))
06edf0c0 13075 {
f00aae0f 13076 /* Anything without a location can't be re-set. */
348d480f 13077 delete_breakpoint (b);
06edf0c0 13078 return;
348d480f 13079 }
06edf0c0
PA
13080
13081 breakpoint_re_set_default (b);
348d480f
PA
13082}
13083
2060206e 13084static int
348d480f
PA
13085bkpt_insert_location (struct bp_location *bl)
13086{
13087 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13088 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13089 else
7c16b83e 13090 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13091}
13092
2060206e 13093static int
348d480f
PA
13094bkpt_remove_location (struct bp_location *bl)
13095{
13096 if (bl->loc_type == bp_loc_hardware_breakpoint)
13097 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13098 else
7c16b83e 13099 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13100}
13101
2060206e 13102static int
348d480f 13103bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13104 struct address_space *aspace, CORE_ADDR bp_addr,
13105 const struct target_waitstatus *ws)
348d480f 13106{
09ac7c10 13107 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13108 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13109 return 0;
13110
348d480f
PA
13111 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13112 aspace, bp_addr))
13113 return 0;
13114
13115 if (overlay_debugging /* unmapped overlay section */
13116 && section_is_overlay (bl->section)
13117 && !section_is_mapped (bl->section))
13118 return 0;
13119
13120 return 1;
13121}
13122
cd1608cc
PA
13123static int
13124dprintf_breakpoint_hit (const struct bp_location *bl,
13125 struct address_space *aspace, CORE_ADDR bp_addr,
13126 const struct target_waitstatus *ws)
13127{
13128 if (dprintf_style == dprintf_style_agent
13129 && target_can_run_breakpoint_commands ())
13130 {
13131 /* An agent-style dprintf never causes a stop. If we see a trap
13132 for this address it must be for a breakpoint that happens to
13133 be set at the same address. */
13134 return 0;
13135 }
13136
13137 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13138}
13139
2060206e 13140static int
348d480f
PA
13141bkpt_resources_needed (const struct bp_location *bl)
13142{
13143 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13144
13145 return 1;
13146}
13147
2060206e 13148static enum print_stop_action
348d480f
PA
13149bkpt_print_it (bpstat bs)
13150{
348d480f
PA
13151 struct breakpoint *b;
13152 const struct bp_location *bl;
001c8c33 13153 int bp_temp;
79a45e25 13154 struct ui_out *uiout = current_uiout;
348d480f
PA
13155
13156 gdb_assert (bs->bp_location_at != NULL);
13157
13158 bl = bs->bp_location_at;
13159 b = bs->breakpoint_at;
13160
001c8c33
PA
13161 bp_temp = b->disposition == disp_del;
13162 if (bl->address != bl->requested_address)
13163 breakpoint_adjustment_warning (bl->requested_address,
13164 bl->address,
13165 b->number, 1);
13166 annotate_breakpoint (b->number);
f303dbd6
PA
13167 maybe_print_thread_hit_breakpoint (uiout);
13168
001c8c33 13169 if (bp_temp)
f303dbd6 13170 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13171 else
f303dbd6 13172 ui_out_text (uiout, "Breakpoint ");
001c8c33 13173 if (ui_out_is_mi_like_p (uiout))
348d480f 13174 {
001c8c33
PA
13175 ui_out_field_string (uiout, "reason",
13176 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13177 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13178 }
001c8c33
PA
13179 ui_out_field_int (uiout, "bkptno", b->number);
13180 ui_out_text (uiout, ", ");
06edf0c0 13181
001c8c33 13182 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13183}
13184
2060206e 13185static void
06edf0c0
PA
13186bkpt_print_mention (struct breakpoint *b)
13187{
79a45e25 13188 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13189 return;
13190
13191 switch (b->type)
13192 {
13193 case bp_breakpoint:
13194 case bp_gnu_ifunc_resolver:
13195 if (b->disposition == disp_del)
13196 printf_filtered (_("Temporary breakpoint"));
13197 else
13198 printf_filtered (_("Breakpoint"));
13199 printf_filtered (_(" %d"), b->number);
13200 if (b->type == bp_gnu_ifunc_resolver)
13201 printf_filtered (_(" at gnu-indirect-function resolver"));
13202 break;
13203 case bp_hardware_breakpoint:
13204 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13205 break;
e7e0cddf
SS
13206 case bp_dprintf:
13207 printf_filtered (_("Dprintf %d"), b->number);
13208 break;
06edf0c0
PA
13209 }
13210
13211 say_where (b);
13212}
13213
2060206e 13214static void
06edf0c0
PA
13215bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13216{
13217 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13218 fprintf_unfiltered (fp, "tbreak");
13219 else if (tp->type == bp_breakpoint)
13220 fprintf_unfiltered (fp, "break");
13221 else if (tp->type == bp_hardware_breakpoint
13222 && tp->disposition == disp_del)
13223 fprintf_unfiltered (fp, "thbreak");
13224 else if (tp->type == bp_hardware_breakpoint)
13225 fprintf_unfiltered (fp, "hbreak");
13226 else
13227 internal_error (__FILE__, __LINE__,
13228 _("unhandled breakpoint type %d"), (int) tp->type);
13229
f00aae0f
KS
13230 fprintf_unfiltered (fp, " %s",
13231 event_location_to_string (tp->location));
13232
13233 /* Print out extra_string if this breakpoint is pending. It might
13234 contain, for example, conditions that were set by the user. */
13235 if (tp->loc == NULL && tp->extra_string != NULL)
13236 fprintf_unfiltered (fp, " %s", tp->extra_string);
13237
dd11a36c 13238 print_recreate_thread (tp, fp);
06edf0c0
PA
13239}
13240
983af33b 13241static void
f00aae0f
KS
13242bkpt_create_sals_from_location (const struct event_location *location,
13243 struct linespec_result *canonical,
13244 enum bptype type_wanted)
983af33b 13245{
f00aae0f 13246 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13247}
13248
13249static void
13250bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13251 struct linespec_result *canonical,
983af33b 13252 char *cond_string,
e7e0cddf 13253 char *extra_string,
983af33b
SDJ
13254 enum bptype type_wanted,
13255 enum bpdisp disposition,
13256 int thread,
13257 int task, int ignore_count,
13258 const struct breakpoint_ops *ops,
13259 int from_tty, int enabled,
44f238bb 13260 int internal, unsigned flags)
983af33b 13261{
023fa29b 13262 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13263 cond_string, extra_string,
13264 type_wanted,
983af33b
SDJ
13265 disposition, thread, task,
13266 ignore_count, ops, from_tty,
44f238bb 13267 enabled, internal, flags);
983af33b
SDJ
13268}
13269
13270static void
f00aae0f
KS
13271bkpt_decode_location (struct breakpoint *b,
13272 const struct event_location *location,
c2f4122d 13273 struct program_space *search_pspace,
983af33b
SDJ
13274 struct symtabs_and_lines *sals)
13275{
c2f4122d 13276 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13277}
13278
06edf0c0
PA
13279/* Virtual table for internal breakpoints. */
13280
13281static void
13282internal_bkpt_re_set (struct breakpoint *b)
13283{
13284 switch (b->type)
13285 {
13286 /* Delete overlay event and longjmp master breakpoints; they
13287 will be reset later by breakpoint_re_set. */
13288 case bp_overlay_event:
13289 case bp_longjmp_master:
13290 case bp_std_terminate_master:
13291 case bp_exception_master:
13292 delete_breakpoint (b);
13293 break;
13294
13295 /* This breakpoint is special, it's set up when the inferior
13296 starts and we really don't want to touch it. */
13297 case bp_shlib_event:
13298
13299 /* Like bp_shlib_event, this breakpoint type is special. Once
13300 it is set up, we do not want to touch it. */
13301 case bp_thread_event:
13302 break;
13303 }
13304}
13305
13306static void
13307internal_bkpt_check_status (bpstat bs)
13308{
a9b3a50f
PA
13309 if (bs->breakpoint_at->type == bp_shlib_event)
13310 {
13311 /* If requested, stop when the dynamic linker notifies GDB of
13312 events. This allows the user to get control and place
13313 breakpoints in initializer routines for dynamically loaded
13314 objects (among other things). */
13315 bs->stop = stop_on_solib_events;
13316 bs->print = stop_on_solib_events;
13317 }
13318 else
13319 bs->stop = 0;
06edf0c0
PA
13320}
13321
13322static enum print_stop_action
13323internal_bkpt_print_it (bpstat bs)
13324{
06edf0c0 13325 struct breakpoint *b;
06edf0c0 13326
06edf0c0
PA
13327 b = bs->breakpoint_at;
13328
06edf0c0
PA
13329 switch (b->type)
13330 {
348d480f
PA
13331 case bp_shlib_event:
13332 /* Did we stop because the user set the stop_on_solib_events
13333 variable? (If so, we report this as a generic, "Stopped due
13334 to shlib event" message.) */
edcc5120 13335 print_solib_event (0);
348d480f
PA
13336 break;
13337
13338 case bp_thread_event:
13339 /* Not sure how we will get here.
13340 GDB should not stop for these breakpoints. */
13341 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13342 break;
13343
13344 case bp_overlay_event:
13345 /* By analogy with the thread event, GDB should not stop for these. */
13346 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13347 break;
13348
13349 case bp_longjmp_master:
13350 /* These should never be enabled. */
13351 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13352 break;
13353
13354 case bp_std_terminate_master:
13355 /* These should never be enabled. */
13356 printf_filtered (_("std::terminate Master Breakpoint: "
13357 "gdb should not stop!\n"));
348d480f
PA
13358 break;
13359
13360 case bp_exception_master:
13361 /* These should never be enabled. */
13362 printf_filtered (_("Exception Master Breakpoint: "
13363 "gdb should not stop!\n"));
06edf0c0
PA
13364 break;
13365 }
13366
001c8c33 13367 return PRINT_NOTHING;
06edf0c0
PA
13368}
13369
13370static void
13371internal_bkpt_print_mention (struct breakpoint *b)
13372{
13373 /* Nothing to mention. These breakpoints are internal. */
13374}
13375
06edf0c0
PA
13376/* Virtual table for momentary breakpoints */
13377
13378static void
13379momentary_bkpt_re_set (struct breakpoint *b)
13380{
13381 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13382 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13383 Otherwise these should have been blown away via the cleanup chain
13384 or by breakpoint_init_inferior when we rerun the executable. */
13385}
13386
13387static void
13388momentary_bkpt_check_status (bpstat bs)
13389{
13390 /* Nothing. The point of these breakpoints is causing a stop. */
13391}
13392
13393static enum print_stop_action
13394momentary_bkpt_print_it (bpstat bs)
13395{
001c8c33 13396 return PRINT_UNKNOWN;
348d480f
PA
13397}
13398
06edf0c0
PA
13399static void
13400momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13401{
06edf0c0 13402 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13403}
13404
e2e4d78b
JK
13405/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13406
13407 It gets cleared already on the removal of the first one of such placed
13408 breakpoints. This is OK as they get all removed altogether. */
13409
13410static void
13411longjmp_bkpt_dtor (struct breakpoint *self)
13412{
5d5658a1 13413 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13414
13415 if (tp)
13416 tp->initiating_frame = null_frame_id;
13417
13418 momentary_breakpoint_ops.dtor (self);
13419}
13420
55aa24fb
SDJ
13421/* Specific methods for probe breakpoints. */
13422
13423static int
13424bkpt_probe_insert_location (struct bp_location *bl)
13425{
13426 int v = bkpt_insert_location (bl);
13427
13428 if (v == 0)
13429 {
13430 /* The insertion was successful, now let's set the probe's semaphore
13431 if needed. */
0ea5cda8
SDJ
13432 if (bl->probe.probe->pops->set_semaphore != NULL)
13433 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13434 bl->probe.objfile,
13435 bl->gdbarch);
55aa24fb
SDJ
13436 }
13437
13438 return v;
13439}
13440
13441static int
13442bkpt_probe_remove_location (struct bp_location *bl)
13443{
13444 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13445 if (bl->probe.probe->pops->clear_semaphore != NULL)
13446 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13447 bl->probe.objfile,
13448 bl->gdbarch);
55aa24fb
SDJ
13449
13450 return bkpt_remove_location (bl);
13451}
13452
13453static void
f00aae0f 13454bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13455 struct linespec_result *canonical,
f00aae0f 13456 enum bptype type_wanted)
55aa24fb
SDJ
13457{
13458 struct linespec_sals lsal;
13459
c2f4122d 13460 lsal.sals = parse_probes (location, NULL, canonical);
f00aae0f 13461 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13462 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13463}
13464
13465static void
f00aae0f
KS
13466bkpt_probe_decode_location (struct breakpoint *b,
13467 const struct event_location *location,
c2f4122d 13468 struct program_space *search_pspace,
55aa24fb
SDJ
13469 struct symtabs_and_lines *sals)
13470{
c2f4122d 13471 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13472 if (!sals->sals)
13473 error (_("probe not found"));
13474}
13475
348d480f 13476/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13477
348d480f
PA
13478static void
13479tracepoint_re_set (struct breakpoint *b)
13480{
13481 breakpoint_re_set_default (b);
13482}
876fa593 13483
348d480f
PA
13484static int
13485tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13486 struct address_space *aspace, CORE_ADDR bp_addr,
13487 const struct target_waitstatus *ws)
348d480f
PA
13488{
13489 /* By definition, the inferior does not report stops at
13490 tracepoints. */
13491 return 0;
74960c60
VP
13492}
13493
13494static void
348d480f
PA
13495tracepoint_print_one_detail (const struct breakpoint *self,
13496 struct ui_out *uiout)
74960c60 13497{
d9b3f62e
PA
13498 struct tracepoint *tp = (struct tracepoint *) self;
13499 if (tp->static_trace_marker_id)
348d480f
PA
13500 {
13501 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13502
348d480f
PA
13503 ui_out_text (uiout, "\tmarker id is ");
13504 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13505 tp->static_trace_marker_id);
348d480f
PA
13506 ui_out_text (uiout, "\n");
13507 }
0d381245
VP
13508}
13509
a474d7c2 13510static void
348d480f 13511tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13512{
79a45e25 13513 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13514 return;
cc59ec59 13515
348d480f
PA
13516 switch (b->type)
13517 {
13518 case bp_tracepoint:
13519 printf_filtered (_("Tracepoint"));
13520 printf_filtered (_(" %d"), b->number);
13521 break;
13522 case bp_fast_tracepoint:
13523 printf_filtered (_("Fast tracepoint"));
13524 printf_filtered (_(" %d"), b->number);
13525 break;
13526 case bp_static_tracepoint:
13527 printf_filtered (_("Static tracepoint"));
13528 printf_filtered (_(" %d"), b->number);
13529 break;
13530 default:
13531 internal_error (__FILE__, __LINE__,
13532 _("unhandled tracepoint type %d"), (int) b->type);
13533 }
13534
13535 say_where (b);
a474d7c2
PA
13536}
13537
348d480f 13538static void
d9b3f62e 13539tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13540{
d9b3f62e
PA
13541 struct tracepoint *tp = (struct tracepoint *) self;
13542
13543 if (self->type == bp_fast_tracepoint)
348d480f 13544 fprintf_unfiltered (fp, "ftrace");
c93e8391 13545 else if (self->type == bp_static_tracepoint)
348d480f 13546 fprintf_unfiltered (fp, "strace");
d9b3f62e 13547 else if (self->type == bp_tracepoint)
348d480f
PA
13548 fprintf_unfiltered (fp, "trace");
13549 else
13550 internal_error (__FILE__, __LINE__,
d9b3f62e 13551 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13552
f00aae0f
KS
13553 fprintf_unfiltered (fp, " %s",
13554 event_location_to_string (self->location));
d9b3f62e
PA
13555 print_recreate_thread (self, fp);
13556
13557 if (tp->pass_count)
13558 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13559}
13560
983af33b 13561static void
f00aae0f
KS
13562tracepoint_create_sals_from_location (const struct event_location *location,
13563 struct linespec_result *canonical,
13564 enum bptype type_wanted)
983af33b 13565{
f00aae0f 13566 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13567}
13568
13569static void
13570tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13571 struct linespec_result *canonical,
983af33b 13572 char *cond_string,
e7e0cddf 13573 char *extra_string,
983af33b
SDJ
13574 enum bptype type_wanted,
13575 enum bpdisp disposition,
13576 int thread,
13577 int task, int ignore_count,
13578 const struct breakpoint_ops *ops,
13579 int from_tty, int enabled,
44f238bb 13580 int internal, unsigned flags)
983af33b 13581{
023fa29b 13582 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13583 cond_string, extra_string,
13584 type_wanted,
983af33b
SDJ
13585 disposition, thread, task,
13586 ignore_count, ops, from_tty,
44f238bb 13587 enabled, internal, flags);
983af33b
SDJ
13588}
13589
13590static void
f00aae0f
KS
13591tracepoint_decode_location (struct breakpoint *b,
13592 const struct event_location *location,
c2f4122d 13593 struct program_space *search_pspace,
983af33b
SDJ
13594 struct symtabs_and_lines *sals)
13595{
c2f4122d 13596 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13597}
13598
2060206e 13599struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13600
55aa24fb
SDJ
13601/* The breakpoint_ops structure to be use on tracepoints placed in a
13602 static probe. */
13603
13604static void
f00aae0f
KS
13605tracepoint_probe_create_sals_from_location
13606 (const struct event_location *location,
13607 struct linespec_result *canonical,
13608 enum bptype type_wanted)
55aa24fb
SDJ
13609{
13610 /* We use the same method for breakpoint on probes. */
f00aae0f 13611 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13612}
13613
13614static void
f00aae0f
KS
13615tracepoint_probe_decode_location (struct breakpoint *b,
13616 const struct event_location *location,
c2f4122d 13617 struct program_space *search_pspace,
55aa24fb
SDJ
13618 struct symtabs_and_lines *sals)
13619{
13620 /* We use the same method for breakpoint on probes. */
c2f4122d 13621 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13622}
13623
13624static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13625
5c2b4418
HZ
13626/* Dprintf breakpoint_ops methods. */
13627
13628static void
13629dprintf_re_set (struct breakpoint *b)
13630{
13631 breakpoint_re_set_default (b);
13632
f00aae0f
KS
13633 /* extra_string should never be non-NULL for dprintf. */
13634 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13635
13636 /* 1 - connect to target 1, that can run breakpoint commands.
13637 2 - create a dprintf, which resolves fine.
13638 3 - disconnect from target 1
13639 4 - connect to target 2, that can NOT run breakpoint commands.
13640
13641 After steps #3/#4, you'll want the dprintf command list to
13642 be updated, because target 1 and 2 may well return different
13643 answers for target_can_run_breakpoint_commands().
13644 Given absence of finer grained resetting, we get to do
13645 it all the time. */
13646 if (b->extra_string != NULL)
13647 update_dprintf_command_list (b);
13648}
13649
2d9442cc
HZ
13650/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13651
13652static void
13653dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13654{
f00aae0f
KS
13655 fprintf_unfiltered (fp, "dprintf %s,%s",
13656 event_location_to_string (tp->location),
2d9442cc
HZ
13657 tp->extra_string);
13658 print_recreate_thread (tp, fp);
13659}
13660
9d6e6e84
HZ
13661/* Implement the "after_condition_true" breakpoint_ops method for
13662 dprintf.
13663
13664 dprintf's are implemented with regular commands in their command
13665 list, but we run the commands here instead of before presenting the
13666 stop to the user, as dprintf's don't actually cause a stop. This
13667 also makes it so that the commands of multiple dprintfs at the same
13668 address are all handled. */
13669
13670static void
13671dprintf_after_condition_true (struct bpstats *bs)
13672{
13673 struct cleanup *old_chain;
13674 struct bpstats tmp_bs = { NULL };
13675 struct bpstats *tmp_bs_p = &tmp_bs;
13676
13677 /* dprintf's never cause a stop. This wasn't set in the
13678 check_status hook instead because that would make the dprintf's
13679 condition not be evaluated. */
13680 bs->stop = 0;
13681
13682 /* Run the command list here. Take ownership of it instead of
13683 copying. We never want these commands to run later in
13684 bpstat_do_actions, if a breakpoint that causes a stop happens to
13685 be set at same address as this dprintf, or even if running the
13686 commands here throws. */
13687 tmp_bs.commands = bs->commands;
13688 bs->commands = NULL;
13689 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13690
13691 bpstat_do_actions_1 (&tmp_bs_p);
13692
13693 /* 'tmp_bs.commands' will usually be NULL by now, but
13694 bpstat_do_actions_1 may return early without processing the whole
13695 list. */
13696 do_cleanups (old_chain);
13697}
13698
983af33b
SDJ
13699/* The breakpoint_ops structure to be used on static tracepoints with
13700 markers (`-m'). */
13701
13702static void
f00aae0f 13703strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13704 struct linespec_result *canonical,
f00aae0f 13705 enum bptype type_wanted)
983af33b
SDJ
13706{
13707 struct linespec_sals lsal;
f00aae0f
KS
13708 const char *arg_start, *arg;
13709 char *str;
13710 struct cleanup *cleanup;
983af33b 13711
f00aae0f
KS
13712 arg = arg_start = get_linespec_location (location);
13713 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13714
f00aae0f
KS
13715 str = savestring (arg_start, arg - arg_start);
13716 cleanup = make_cleanup (xfree, str);
13717 canonical->location = new_linespec_location (&str);
13718 do_cleanups (cleanup);
983af33b 13719
f00aae0f 13720 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13721 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13722}
13723
13724static void
13725strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13726 struct linespec_result *canonical,
983af33b 13727 char *cond_string,
e7e0cddf 13728 char *extra_string,
983af33b
SDJ
13729 enum bptype type_wanted,
13730 enum bpdisp disposition,
13731 int thread,
13732 int task, int ignore_count,
13733 const struct breakpoint_ops *ops,
13734 int from_tty, int enabled,
44f238bb 13735 int internal, unsigned flags)
983af33b
SDJ
13736{
13737 int i;
52d361e1
YQ
13738 struct linespec_sals *lsal = VEC_index (linespec_sals,
13739 canonical->sals, 0);
983af33b
SDJ
13740
13741 /* If the user is creating a static tracepoint by marker id
13742 (strace -m MARKER_ID), then store the sals index, so that
13743 breakpoint_re_set can try to match up which of the newly
13744 found markers corresponds to this one, and, don't try to
13745 expand multiple locations for each sal, given than SALS
13746 already should contain all sals for MARKER_ID. */
13747
13748 for (i = 0; i < lsal->sals.nelts; ++i)
13749 {
13750 struct symtabs_and_lines expanded;
13751 struct tracepoint *tp;
13752 struct cleanup *old_chain;
f00aae0f 13753 struct event_location *location;
983af33b
SDJ
13754
13755 expanded.nelts = 1;
13756 expanded.sals = &lsal->sals.sals[i];
13757
f00aae0f
KS
13758 location = copy_event_location (canonical->location);
13759 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13760
13761 tp = XCNEW (struct tracepoint);
13762 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13763 location, NULL,
e7e0cddf
SS
13764 cond_string, extra_string,
13765 type_wanted, disposition,
983af33b 13766 thread, task, ignore_count, ops,
44f238bb 13767 from_tty, enabled, internal, flags,
983af33b
SDJ
13768 canonical->special_display);
13769 /* Given that its possible to have multiple markers with
13770 the same string id, if the user is creating a static
13771 tracepoint by marker id ("strace -m MARKER_ID"), then
13772 store the sals index, so that breakpoint_re_set can
13773 try to match up which of the newly found markers
13774 corresponds to this one */
13775 tp->static_trace_marker_id_idx = i;
13776
13777 install_breakpoint (internal, &tp->base, 0);
13778
13779 discard_cleanups (old_chain);
13780 }
13781}
13782
13783static void
f00aae0f
KS
13784strace_marker_decode_location (struct breakpoint *b,
13785 const struct event_location *location,
c2f4122d 13786 struct program_space *search_pspace,
983af33b
SDJ
13787 struct symtabs_and_lines *sals)
13788{
13789 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13790 const char *s = get_linespec_location (location);
983af33b 13791
f00aae0f 13792 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13793 if (sals->nelts > tp->static_trace_marker_id_idx)
13794 {
13795 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13796 sals->nelts = 1;
13797 }
13798 else
13799 error (_("marker %s not found"), tp->static_trace_marker_id);
13800}
13801
13802static struct breakpoint_ops strace_marker_breakpoint_ops;
13803
13804static int
13805strace_marker_p (struct breakpoint *b)
13806{
13807 return b->ops == &strace_marker_breakpoint_ops;
13808}
13809
53a5351d 13810/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13811 structures. */
c906108c
SS
13812
13813void
fba45db2 13814delete_breakpoint (struct breakpoint *bpt)
c906108c 13815{
52f0bd74 13816 struct breakpoint *b;
c906108c 13817
8a3fe4f8 13818 gdb_assert (bpt != NULL);
c906108c 13819
4a64f543
MS
13820 /* Has this bp already been deleted? This can happen because
13821 multiple lists can hold pointers to bp's. bpstat lists are
13822 especial culprits.
13823
13824 One example of this happening is a watchpoint's scope bp. When
13825 the scope bp triggers, we notice that the watchpoint is out of
13826 scope, and delete it. We also delete its scope bp. But the
13827 scope bp is marked "auto-deleting", and is already on a bpstat.
13828 That bpstat is then checked for auto-deleting bp's, which are
13829 deleted.
13830
13831 A real solution to this problem might involve reference counts in
13832 bp's, and/or giving them pointers back to their referencing
13833 bpstat's, and teaching delete_breakpoint to only free a bp's
13834 storage when no more references were extent. A cheaper bandaid
13835 was chosen. */
c906108c
SS
13836 if (bpt->type == bp_none)
13837 return;
13838
4a64f543
MS
13839 /* At least avoid this stale reference until the reference counting
13840 of breakpoints gets resolved. */
d0fb5eae 13841 if (bpt->related_breakpoint != bpt)
e5a0a904 13842 {
d0fb5eae 13843 struct breakpoint *related;
3a5c3e22 13844 struct watchpoint *w;
d0fb5eae
JK
13845
13846 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13847 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13848 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13849 w = (struct watchpoint *) bpt;
13850 else
13851 w = NULL;
13852 if (w != NULL)
13853 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13854
13855 /* Unlink bpt from the bpt->related_breakpoint ring. */
13856 for (related = bpt; related->related_breakpoint != bpt;
13857 related = related->related_breakpoint);
13858 related->related_breakpoint = bpt->related_breakpoint;
13859 bpt->related_breakpoint = bpt;
e5a0a904
JK
13860 }
13861
a9634178
TJB
13862 /* watch_command_1 creates a watchpoint but only sets its number if
13863 update_watchpoint succeeds in creating its bp_locations. If there's
13864 a problem in that process, we'll be asked to delete the half-created
13865 watchpoint. In that case, don't announce the deletion. */
13866 if (bpt->number)
13867 observer_notify_breakpoint_deleted (bpt);
c906108c 13868
c906108c
SS
13869 if (breakpoint_chain == bpt)
13870 breakpoint_chain = bpt->next;
13871
c906108c
SS
13872 ALL_BREAKPOINTS (b)
13873 if (b->next == bpt)
c5aa993b
JM
13874 {
13875 b->next = bpt->next;
13876 break;
13877 }
c906108c 13878
f431efe5
PA
13879 /* Be sure no bpstat's are pointing at the breakpoint after it's
13880 been freed. */
13881 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13882 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13883 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13884 commands are associated with the bpstat; if we remove it here,
13885 then the later call to bpstat_do_actions (&stop_bpstat); in
13886 event-top.c won't do anything, and temporary breakpoints with
13887 commands won't work. */
13888
13889 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13890
4a64f543
MS
13891 /* Now that breakpoint is removed from breakpoint list, update the
13892 global location list. This will remove locations that used to
13893 belong to this breakpoint. Do this before freeing the breakpoint
13894 itself, since remove_breakpoint looks at location's owner. It
13895 might be better design to have location completely
13896 self-contained, but it's not the case now. */
44702360 13897 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13898
348d480f 13899 bpt->ops->dtor (bpt);
4a64f543
MS
13900 /* On the chance that someone will soon try again to delete this
13901 same bp, we mark it as deleted before freeing its storage. */
c906108c 13902 bpt->type = bp_none;
b8c9b27d 13903 xfree (bpt);
c906108c
SS
13904}
13905
4d6140d9
AC
13906static void
13907do_delete_breakpoint_cleanup (void *b)
13908{
9a3c8263 13909 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13910}
13911
13912struct cleanup *
13913make_cleanup_delete_breakpoint (struct breakpoint *b)
13914{
13915 return make_cleanup (do_delete_breakpoint_cleanup, b);
13916}
13917
51be5b68
PA
13918/* Iterator function to call a user-provided callback function once
13919 for each of B and its related breakpoints. */
13920
13921static void
13922iterate_over_related_breakpoints (struct breakpoint *b,
13923 void (*function) (struct breakpoint *,
13924 void *),
13925 void *data)
13926{
13927 struct breakpoint *related;
13928
13929 related = b;
13930 do
13931 {
13932 struct breakpoint *next;
13933
13934 /* FUNCTION may delete RELATED. */
13935 next = related->related_breakpoint;
13936
13937 if (next == related)
13938 {
13939 /* RELATED is the last ring entry. */
13940 function (related, data);
13941
13942 /* FUNCTION may have deleted it, so we'd never reach back to
13943 B. There's nothing left to do anyway, so just break
13944 out. */
13945 break;
13946 }
13947 else
13948 function (related, data);
13949
13950 related = next;
13951 }
13952 while (related != b);
13953}
95a42b64
TT
13954
13955static void
13956do_delete_breakpoint (struct breakpoint *b, void *ignore)
13957{
13958 delete_breakpoint (b);
13959}
13960
51be5b68
PA
13961/* A callback for map_breakpoint_numbers that calls
13962 delete_breakpoint. */
13963
13964static void
13965do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13966{
13967 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13968}
13969
c906108c 13970void
fba45db2 13971delete_command (char *arg, int from_tty)
c906108c 13972{
35df4500 13973 struct breakpoint *b, *b_tmp;
c906108c 13974
ea9365bb
TT
13975 dont_repeat ();
13976
c906108c
SS
13977 if (arg == 0)
13978 {
13979 int breaks_to_delete = 0;
13980
46c6471b
PA
13981 /* Delete all breakpoints if no argument. Do not delete
13982 internal breakpoints, these have to be deleted with an
13983 explicit breakpoint number argument. */
c5aa993b 13984 ALL_BREAKPOINTS (b)
46c6471b 13985 if (user_breakpoint_p (b))
973d738b
DJ
13986 {
13987 breaks_to_delete = 1;
13988 break;
13989 }
c906108c
SS
13990
13991 /* Ask user only if there are some breakpoints to delete. */
13992 if (!from_tty
e2e0b3e5 13993 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13994 {
35df4500 13995 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13996 if (user_breakpoint_p (b))
c5aa993b 13997 delete_breakpoint (b);
c906108c
SS
13998 }
13999 }
14000 else
51be5b68 14001 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14002}
14003
c2f4122d
PA
14004/* Return true if all locations of B bound to PSPACE are pending. If
14005 PSPACE is NULL, all locations of all program spaces are
14006 considered. */
14007
0d381245 14008static int
c2f4122d 14009all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 14010{
c2f4122d
PA
14011 struct bp_location *loc;
14012
14013 for (loc = b->loc; loc != NULL; loc = loc->next)
14014 if ((pspace == NULL
14015 || loc->pspace == pspace)
14016 && !loc->shlib_disabled
8645ff69 14017 && !loc->pspace->executing_startup)
0d381245
VP
14018 return 0;
14019 return 1;
fe3f5fa8
VP
14020}
14021
776592bf
DE
14022/* Subroutine of update_breakpoint_locations to simplify it.
14023 Return non-zero if multiple fns in list LOC have the same name.
14024 Null names are ignored. */
14025
14026static int
14027ambiguous_names_p (struct bp_location *loc)
14028{
14029 struct bp_location *l;
14030 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14031 (int (*) (const void *,
14032 const void *)) streq,
776592bf
DE
14033 NULL, xcalloc, xfree);
14034
14035 for (l = loc; l != NULL; l = l->next)
14036 {
14037 const char **slot;
14038 const char *name = l->function_name;
14039
14040 /* Allow for some names to be NULL, ignore them. */
14041 if (name == NULL)
14042 continue;
14043
14044 slot = (const char **) htab_find_slot (htab, (const void *) name,
14045 INSERT);
4a64f543
MS
14046 /* NOTE: We can assume slot != NULL here because xcalloc never
14047 returns NULL. */
776592bf
DE
14048 if (*slot != NULL)
14049 {
14050 htab_delete (htab);
14051 return 1;
14052 }
14053 *slot = name;
14054 }
14055
14056 htab_delete (htab);
14057 return 0;
14058}
14059
0fb4aa4b
PA
14060/* When symbols change, it probably means the sources changed as well,
14061 and it might mean the static tracepoint markers are no longer at
14062 the same address or line numbers they used to be at last we
14063 checked. Losing your static tracepoints whenever you rebuild is
14064 undesirable. This function tries to resync/rematch gdb static
14065 tracepoints with the markers on the target, for static tracepoints
14066 that have not been set by marker id. Static tracepoint that have
14067 been set by marker id are reset by marker id in breakpoint_re_set.
14068 The heuristic is:
14069
14070 1) For a tracepoint set at a specific address, look for a marker at
14071 the old PC. If one is found there, assume to be the same marker.
14072 If the name / string id of the marker found is different from the
14073 previous known name, assume that means the user renamed the marker
14074 in the sources, and output a warning.
14075
14076 2) For a tracepoint set at a given line number, look for a marker
14077 at the new address of the old line number. If one is found there,
14078 assume to be the same marker. If the name / string id of the
14079 marker found is different from the previous known name, assume that
14080 means the user renamed the marker in the sources, and output a
14081 warning.
14082
14083 3) If a marker is no longer found at the same address or line, it
14084 may mean the marker no longer exists. But it may also just mean
14085 the code changed a bit. Maybe the user added a few lines of code
14086 that made the marker move up or down (in line number terms). Ask
14087 the target for info about the marker with the string id as we knew
14088 it. If found, update line number and address in the matching
14089 static tracepoint. This will get confused if there's more than one
14090 marker with the same ID (possible in UST, although unadvised
14091 precisely because it confuses tools). */
14092
14093static struct symtab_and_line
14094update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14095{
d9b3f62e 14096 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14097 struct static_tracepoint_marker marker;
14098 CORE_ADDR pc;
0fb4aa4b
PA
14099
14100 pc = sal.pc;
14101 if (sal.line)
14102 find_line_pc (sal.symtab, sal.line, &pc);
14103
14104 if (target_static_tracepoint_marker_at (pc, &marker))
14105 {
d9b3f62e 14106 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14107 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14108 b->number,
d9b3f62e 14109 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14110
d9b3f62e
PA
14111 xfree (tp->static_trace_marker_id);
14112 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14113 release_static_tracepoint_marker (&marker);
14114
14115 return sal;
14116 }
14117
14118 /* Old marker wasn't found on target at lineno. Try looking it up
14119 by string ID. */
14120 if (!sal.explicit_pc
14121 && sal.line != 0
14122 && sal.symtab != NULL
d9b3f62e 14123 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14124 {
14125 VEC(static_tracepoint_marker_p) *markers;
14126
14127 markers
d9b3f62e 14128 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14129
14130 if (!VEC_empty(static_tracepoint_marker_p, markers))
14131 {
80e1d417 14132 struct symtab_and_line sal2;
0fb4aa4b 14133 struct symbol *sym;
80e1d417 14134 struct static_tracepoint_marker *tpmarker;
79a45e25 14135 struct ui_out *uiout = current_uiout;
67994074 14136 struct explicit_location explicit_loc;
0fb4aa4b 14137
80e1d417 14138 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14139
d9b3f62e 14140 xfree (tp->static_trace_marker_id);
80e1d417 14141 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14142
14143 warning (_("marker for static tracepoint %d (%s) not "
14144 "found at previous line number"),
d9b3f62e 14145 b->number, tp->static_trace_marker_id);
0fb4aa4b 14146
80e1d417 14147 init_sal (&sal2);
0fb4aa4b 14148
80e1d417 14149 sal2.pc = tpmarker->address;
0fb4aa4b 14150
80e1d417
AS
14151 sal2 = find_pc_line (tpmarker->address, 0);
14152 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14153 ui_out_text (uiout, "Now in ");
14154 if (sym)
14155 {
14156 ui_out_field_string (uiout, "func",
14157 SYMBOL_PRINT_NAME (sym));
14158 ui_out_text (uiout, " at ");
14159 }
05cba821
JK
14160 ui_out_field_string (uiout, "file",
14161 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14162 ui_out_text (uiout, ":");
14163
14164 if (ui_out_is_mi_like_p (uiout))
14165 {
0b0865da 14166 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14167
f35a17b5 14168 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14169 }
14170
80e1d417 14171 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14172 ui_out_text (uiout, "\n");
14173
80e1d417 14174 b->loc->line_number = sal2.line;
2f202fde 14175 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14176
f00aae0f 14177 delete_event_location (b->location);
67994074
KS
14178 initialize_explicit_location (&explicit_loc);
14179 explicit_loc.source_filename
00e52e53 14180 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14181 explicit_loc.line_offset.offset = b->loc->line_number;
14182 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14183 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14184
14185 /* Might be nice to check if function changed, and warn if
14186 so. */
14187
80e1d417 14188 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14189 }
14190 }
14191 return sal;
14192}
14193
8d3788bd
VP
14194/* Returns 1 iff locations A and B are sufficiently same that
14195 we don't need to report breakpoint as changed. */
14196
14197static int
14198locations_are_equal (struct bp_location *a, struct bp_location *b)
14199{
14200 while (a && b)
14201 {
14202 if (a->address != b->address)
14203 return 0;
14204
14205 if (a->shlib_disabled != b->shlib_disabled)
14206 return 0;
14207
14208 if (a->enabled != b->enabled)
14209 return 0;
14210
14211 a = a->next;
14212 b = b->next;
14213 }
14214
14215 if ((a == NULL) != (b == NULL))
14216 return 0;
14217
14218 return 1;
14219}
14220
c2f4122d
PA
14221/* Split all locations of B that are bound to PSPACE out of B's
14222 location list to a separate list and return that list's head. If
14223 PSPACE is NULL, hoist out all locations of B. */
14224
14225static struct bp_location *
14226hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14227{
14228 struct bp_location head;
14229 struct bp_location *i = b->loc;
14230 struct bp_location **i_link = &b->loc;
14231 struct bp_location *hoisted = &head;
14232
14233 if (pspace == NULL)
14234 {
14235 i = b->loc;
14236 b->loc = NULL;
14237 return i;
14238 }
14239
14240 head.next = NULL;
14241
14242 while (i != NULL)
14243 {
14244 if (i->pspace == pspace)
14245 {
14246 *i_link = i->next;
14247 i->next = NULL;
14248 hoisted->next = i;
14249 hoisted = i;
14250 }
14251 else
14252 i_link = &i->next;
14253 i = *i_link;
14254 }
14255
14256 return head.next;
14257}
14258
14259/* Create new breakpoint locations for B (a hardware or software
14260 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14261 zero, then B is a ranged breakpoint. Only recreates locations for
14262 FILTER_PSPACE. Locations of other program spaces are left
14263 untouched. */
f1310107 14264
0e30163f 14265void
0d381245 14266update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14267 struct program_space *filter_pspace,
f1310107
TJB
14268 struct symtabs_and_lines sals,
14269 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14270{
14271 int i;
c2f4122d 14272 struct bp_location *existing_locations;
0d381245 14273
f8eba3c6
TT
14274 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14275 {
14276 /* Ranged breakpoints have only one start location and one end
14277 location. */
14278 b->enable_state = bp_disabled;
f8eba3c6
TT
14279 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14280 "multiple locations found\n"),
14281 b->number);
14282 return;
14283 }
f1310107 14284
4a64f543
MS
14285 /* If there's no new locations, and all existing locations are
14286 pending, don't do anything. This optimizes the common case where
14287 all locations are in the same shared library, that was unloaded.
14288 We'd like to retain the location, so that when the library is
14289 loaded again, we don't loose the enabled/disabled status of the
14290 individual locations. */
c2f4122d 14291 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14292 return;
14293
c2f4122d 14294 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14295
0d381245 14296 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14297 {
f8eba3c6
TT
14298 struct bp_location *new_loc;
14299
14300 switch_to_program_space_and_thread (sals.sals[i].pspace);
14301
14302 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14303
0d381245
VP
14304 /* Reparse conditions, they might contain references to the
14305 old symtab. */
14306 if (b->cond_string != NULL)
14307 {
bbc13ae3 14308 const char *s;
fe3f5fa8 14309
0d381245 14310 s = b->cond_string;
492d29ea 14311 TRY
0d381245 14312 {
1bb9788d
TT
14313 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14314 block_for_pc (sals.sals[i].pc),
0d381245
VP
14315 0);
14316 }
492d29ea 14317 CATCH (e, RETURN_MASK_ERROR)
0d381245 14318 {
3e43a32a
MS
14319 warning (_("failed to reevaluate condition "
14320 "for breakpoint %d: %s"),
0d381245
VP
14321 b->number, e.message);
14322 new_loc->enabled = 0;
14323 }
492d29ea 14324 END_CATCH
0d381245 14325 }
fe3f5fa8 14326
f1310107
TJB
14327 if (sals_end.nelts)
14328 {
14329 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14330
14331 new_loc->length = end - sals.sals[0].pc + 1;
14332 }
0d381245 14333 }
fe3f5fa8 14334
4a64f543
MS
14335 /* If possible, carry over 'disable' status from existing
14336 breakpoints. */
0d381245
VP
14337 {
14338 struct bp_location *e = existing_locations;
776592bf
DE
14339 /* If there are multiple breakpoints with the same function name,
14340 e.g. for inline functions, comparing function names won't work.
14341 Instead compare pc addresses; this is just a heuristic as things
14342 may have moved, but in practice it gives the correct answer
14343 often enough until a better solution is found. */
14344 int have_ambiguous_names = ambiguous_names_p (b->loc);
14345
0d381245
VP
14346 for (; e; e = e->next)
14347 {
14348 if (!e->enabled && e->function_name)
14349 {
14350 struct bp_location *l = b->loc;
776592bf
DE
14351 if (have_ambiguous_names)
14352 {
14353 for (; l; l = l->next)
f1310107 14354 if (breakpoint_locations_match (e, l))
776592bf
DE
14355 {
14356 l->enabled = 0;
14357 break;
14358 }
14359 }
14360 else
14361 {
14362 for (; l; l = l->next)
14363 if (l->function_name
14364 && strcmp (e->function_name, l->function_name) == 0)
14365 {
14366 l->enabled = 0;
14367 break;
14368 }
14369 }
0d381245
VP
14370 }
14371 }
14372 }
fe3f5fa8 14373
8d3788bd
VP
14374 if (!locations_are_equal (existing_locations, b->loc))
14375 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14376}
14377
f00aae0f 14378/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14379 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14380
14381static struct symtabs_and_lines
f00aae0f 14382location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14383 struct program_space *search_pspace, int *found)
ef23e705 14384{
02d20e4a 14385 struct symtabs_and_lines sals = {0};
492d29ea 14386 struct gdb_exception exception = exception_none;
ef23e705 14387
983af33b 14388 gdb_assert (b->ops != NULL);
ef23e705 14389
492d29ea 14390 TRY
ef23e705 14391 {
c2f4122d 14392 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14393 }
492d29ea 14394 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14395 {
14396 int not_found_and_ok = 0;
492d29ea
PA
14397
14398 exception = e;
14399
ef23e705
TJB
14400 /* For pending breakpoints, it's expected that parsing will
14401 fail until the right shared library is loaded. User has
14402 already told to create pending breakpoints and don't need
14403 extra messages. If breakpoint is in bp_shlib_disabled
14404 state, then user already saw the message about that
14405 breakpoint being disabled, and don't want to see more
14406 errors. */
58438ac1 14407 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14408 && (b->condition_not_parsed
14409 || (b->loc != NULL
14410 && search_pspace != NULL
14411 && b->loc->pspace != search_pspace)
ef23e705 14412 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14413 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14414 || b->enable_state == bp_disabled))
14415 not_found_and_ok = 1;
14416
14417 if (!not_found_and_ok)
14418 {
14419 /* We surely don't want to warn about the same breakpoint
14420 10 times. One solution, implemented here, is disable
14421 the breakpoint on error. Another solution would be to
14422 have separate 'warning emitted' flag. Since this
14423 happens only when a binary has changed, I don't know
14424 which approach is better. */
14425 b->enable_state = bp_disabled;
14426 throw_exception (e);
14427 }
14428 }
492d29ea 14429 END_CATCH
ef23e705 14430
492d29ea 14431 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14432 {
f8eba3c6 14433 int i;
ef23e705 14434
f8eba3c6
TT
14435 for (i = 0; i < sals.nelts; ++i)
14436 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14437 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14438 {
ed1d1739
KS
14439 char *cond_string, *extra_string;
14440 int thread, task;
ef23e705 14441
f00aae0f 14442 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14443 &cond_string, &thread, &task,
14444 &extra_string);
f00aae0f 14445 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14446 if (cond_string)
14447 b->cond_string = cond_string;
14448 b->thread = thread;
14449 b->task = task;
e7e0cddf 14450 if (extra_string)
f00aae0f
KS
14451 {
14452 xfree (b->extra_string);
14453 b->extra_string = extra_string;
14454 }
ef23e705
TJB
14455 b->condition_not_parsed = 0;
14456 }
14457
983af33b 14458 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14459 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14460
58438ac1
TT
14461 *found = 1;
14462 }
14463 else
14464 *found = 0;
ef23e705
TJB
14465
14466 return sals;
14467}
14468
348d480f
PA
14469/* The default re_set method, for typical hardware or software
14470 breakpoints. Reevaluate the breakpoint and recreate its
14471 locations. */
14472
14473static void
28010a5d 14474breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14475{
14476 int found;
f1310107 14477 struct symtabs_and_lines sals, sals_end;
ef23e705 14478 struct symtabs_and_lines expanded = {0};
f1310107 14479 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14480 struct program_space *filter_pspace = current_program_space;
ef23e705 14481
c2f4122d 14482 sals = location_to_sals (b, b->location, filter_pspace, &found);
ef23e705
TJB
14483 if (found)
14484 {
14485 make_cleanup (xfree, sals.sals);
f8eba3c6 14486 expanded = sals;
ef23e705
TJB
14487 }
14488
f00aae0f 14489 if (b->location_range_end != NULL)
f1310107 14490 {
c2f4122d
PA
14491 sals_end = location_to_sals (b, b->location_range_end,
14492 filter_pspace, &found);
f1310107
TJB
14493 if (found)
14494 {
14495 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14496 expanded_end = sals_end;
f1310107
TJB
14497 }
14498 }
14499
c2f4122d 14500 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14501}
14502
983af33b
SDJ
14503/* Default method for creating SALs from an address string. It basically
14504 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14505
14506static void
f00aae0f
KS
14507create_sals_from_location_default (const struct event_location *location,
14508 struct linespec_result *canonical,
14509 enum bptype type_wanted)
983af33b 14510{
f00aae0f 14511 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14512}
14513
14514/* Call create_breakpoints_sal for the given arguments. This is the default
14515 function for the `create_breakpoints_sal' method of
14516 breakpoint_ops. */
14517
14518static void
14519create_breakpoints_sal_default (struct gdbarch *gdbarch,
14520 struct linespec_result *canonical,
983af33b 14521 char *cond_string,
e7e0cddf 14522 char *extra_string,
983af33b
SDJ
14523 enum bptype type_wanted,
14524 enum bpdisp disposition,
14525 int thread,
14526 int task, int ignore_count,
14527 const struct breakpoint_ops *ops,
14528 int from_tty, int enabled,
44f238bb 14529 int internal, unsigned flags)
983af33b
SDJ
14530{
14531 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14532 extra_string,
983af33b
SDJ
14533 type_wanted, disposition,
14534 thread, task, ignore_count, ops, from_tty,
44f238bb 14535 enabled, internal, flags);
983af33b
SDJ
14536}
14537
14538/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14539 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14540
14541static void
f00aae0f
KS
14542decode_location_default (struct breakpoint *b,
14543 const struct event_location *location,
c2f4122d 14544 struct program_space *search_pspace,
983af33b
SDJ
14545 struct symtabs_and_lines *sals)
14546{
14547 struct linespec_result canonical;
14548
14549 init_linespec_result (&canonical);
c2f4122d 14550 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14551 (struct symtab *) NULL, 0,
14552 &canonical, multiple_symbols_all,
14553 b->filter);
14554
14555 /* We should get 0 or 1 resulting SALs. */
14556 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14557
14558 if (VEC_length (linespec_sals, canonical.sals) > 0)
14559 {
14560 struct linespec_sals *lsal;
14561
14562 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14563 *sals = lsal->sals;
14564 /* Arrange it so the destructor does not free the
14565 contents. */
14566 lsal->sals.sals = NULL;
14567 }
14568
14569 destroy_linespec_result (&canonical);
14570}
14571
28010a5d
PA
14572/* Prepare the global context for a re-set of breakpoint B. */
14573
14574static struct cleanup *
14575prepare_re_set_context (struct breakpoint *b)
14576{
28010a5d 14577 input_radix = b->input_radix;
28010a5d
PA
14578 set_language (b->language);
14579
c2f4122d 14580 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14581}
14582
c906108c
SS
14583/* Reset a breakpoint given it's struct breakpoint * BINT.
14584 The value we return ends up being the return value from catch_errors.
14585 Unused in this case. */
14586
14587static int
4efb68b1 14588breakpoint_re_set_one (void *bint)
c906108c 14589{
4a64f543 14590 /* Get past catch_errs. */
53a5351d 14591 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14592 struct cleanup *cleanups;
c906108c 14593
348d480f
PA
14594 cleanups = prepare_re_set_context (b);
14595 b->ops->re_set (b);
14596 do_cleanups (cleanups);
c906108c
SS
14597 return 0;
14598}
14599
c2f4122d
PA
14600/* Re-set breakpoint locations for the current program space.
14601 Locations bound to other program spaces are left untouched. */
14602
c906108c 14603void
69de3c6a 14604breakpoint_re_set (void)
c906108c 14605{
35df4500 14606 struct breakpoint *b, *b_tmp;
c906108c
SS
14607 enum language save_language;
14608 int save_input_radix;
6c95b8df 14609 struct cleanup *old_chain;
c5aa993b 14610
c906108c
SS
14611 save_language = current_language->la_language;
14612 save_input_radix = input_radix;
c2f4122d 14613 old_chain = save_current_space_and_thread ();
6c95b8df 14614
2a7f3dff
PA
14615 /* Note: we must not try to insert locations until after all
14616 breakpoints have been re-set. Otherwise, e.g., when re-setting
14617 breakpoint 1, we'd insert the locations of breakpoint 2, which
14618 hadn't been re-set yet, and thus may have stale locations. */
14619
35df4500 14620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14621 {
4a64f543 14622 /* Format possible error msg. */
fe3f5fa8 14623 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14624 b->number);
14625 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14626 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14627 do_cleanups (cleanups);
c5aa993b 14628 }
c906108c
SS
14629 set_language (save_language);
14630 input_radix = save_input_radix;
e62c965a 14631
0756c555 14632 jit_breakpoint_re_set ();
4efc6507 14633
6c95b8df
PA
14634 do_cleanups (old_chain);
14635
af02033e
PP
14636 create_overlay_event_breakpoint ();
14637 create_longjmp_master_breakpoint ();
14638 create_std_terminate_master_breakpoint ();
186c406b 14639 create_exception_master_breakpoint ();
2a7f3dff
PA
14640
14641 /* Now we can insert. */
14642 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14643}
14644\f
c906108c
SS
14645/* Reset the thread number of this breakpoint:
14646
14647 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14648 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14649void
fba45db2 14650breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14651{
14652 if (b->thread != -1)
14653 {
39f77062 14654 if (in_thread_list (inferior_ptid))
5d5658a1 14655 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14656
14657 /* We're being called after following a fork. The new fork is
14658 selected as current, and unless this was a vfork will have a
14659 different program space from the original thread. Reset that
14660 as well. */
14661 b->loc->pspace = current_program_space;
c906108c
SS
14662 }
14663}
14664
03ac34d5
MS
14665/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14666 If from_tty is nonzero, it prints a message to that effect,
14667 which ends with a period (no newline). */
14668
c906108c 14669void
fba45db2 14670set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14671{
52f0bd74 14672 struct breakpoint *b;
c906108c
SS
14673
14674 if (count < 0)
14675 count = 0;
14676
14677 ALL_BREAKPOINTS (b)
14678 if (b->number == bptnum)
c5aa993b 14679 {
d77f58be
SS
14680 if (is_tracepoint (b))
14681 {
14682 if (from_tty && count != 0)
14683 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14684 bptnum);
14685 return;
14686 }
14687
c5aa993b 14688 b->ignore_count = count;
221ea385
KS
14689 if (from_tty)
14690 {
14691 if (count == 0)
3e43a32a
MS
14692 printf_filtered (_("Will stop next time "
14693 "breakpoint %d is reached."),
221ea385
KS
14694 bptnum);
14695 else if (count == 1)
a3f17187 14696 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14697 bptnum);
14698 else
3e43a32a
MS
14699 printf_filtered (_("Will ignore next %d "
14700 "crossings of breakpoint %d."),
221ea385
KS
14701 count, bptnum);
14702 }
8d3788bd 14703 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14704 return;
14705 }
c906108c 14706
8a3fe4f8 14707 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14708}
14709
c906108c
SS
14710/* Command to set ignore-count of breakpoint N to COUNT. */
14711
14712static void
fba45db2 14713ignore_command (char *args, int from_tty)
c906108c
SS
14714{
14715 char *p = args;
52f0bd74 14716 int num;
c906108c
SS
14717
14718 if (p == 0)
e2e0b3e5 14719 error_no_arg (_("a breakpoint number"));
c5aa993b 14720
c906108c 14721 num = get_number (&p);
5c44784c 14722 if (num == 0)
8a3fe4f8 14723 error (_("bad breakpoint number: '%s'"), args);
c906108c 14724 if (*p == 0)
8a3fe4f8 14725 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14726
14727 set_ignore_count (num,
14728 longest_to_int (value_as_long (parse_and_eval (p))),
14729 from_tty);
221ea385
KS
14730 if (from_tty)
14731 printf_filtered ("\n");
c906108c
SS
14732}
14733\f
14734/* Call FUNCTION on each of the breakpoints
14735 whose numbers are given in ARGS. */
14736
14737static void
95a42b64
TT
14738map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14739 void *),
14740 void *data)
c906108c 14741{
52f0bd74
AC
14742 int num;
14743 struct breakpoint *b, *tmp;
11cf8741 14744 int match;
197f0a60 14745 struct get_number_or_range_state state;
c906108c 14746
b9d61307 14747 if (args == 0 || *args == '\0')
e2e0b3e5 14748 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14749
197f0a60
TT
14750 init_number_or_range (&state, args);
14751
14752 while (!state.finished)
c906108c 14753 {
e799154c 14754 const char *p = state.string;
197f0a60 14755
11cf8741 14756 match = 0;
c5aa993b 14757
197f0a60 14758 num = get_number_or_range (&state);
5c44784c 14759 if (num == 0)
c5aa993b 14760 {
8a3fe4f8 14761 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14762 }
14763 else
14764 {
14765 ALL_BREAKPOINTS_SAFE (b, tmp)
14766 if (b->number == num)
14767 {
11cf8741 14768 match = 1;
cdac0397 14769 function (b, data);
11cf8741 14770 break;
5c44784c 14771 }
11cf8741 14772 if (match == 0)
a3f17187 14773 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14774 }
c906108c
SS
14775 }
14776}
14777
0d381245
VP
14778static struct bp_location *
14779find_location_by_number (char *number)
14780{
14781 char *dot = strchr (number, '.');
14782 char *p1;
14783 int bp_num;
14784 int loc_num;
14785 struct breakpoint *b;
14786 struct bp_location *loc;
14787
14788 *dot = '\0';
14789
14790 p1 = number;
197f0a60 14791 bp_num = get_number (&p1);
0d381245
VP
14792 if (bp_num == 0)
14793 error (_("Bad breakpoint number '%s'"), number);
14794
14795 ALL_BREAKPOINTS (b)
14796 if (b->number == bp_num)
14797 {
14798 break;
14799 }
14800
14801 if (!b || b->number != bp_num)
14802 error (_("Bad breakpoint number '%s'"), number);
14803
14804 p1 = dot+1;
197f0a60 14805 loc_num = get_number (&p1);
0d381245
VP
14806 if (loc_num == 0)
14807 error (_("Bad breakpoint location number '%s'"), number);
14808
14809 --loc_num;
14810 loc = b->loc;
14811 for (;loc_num && loc; --loc_num, loc = loc->next)
14812 ;
14813 if (!loc)
14814 error (_("Bad breakpoint location number '%s'"), dot+1);
14815
14816 return loc;
14817}
14818
14819
1900040c
MS
14820/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14821 If from_tty is nonzero, it prints a message to that effect,
14822 which ends with a period (no newline). */
14823
c906108c 14824void
fba45db2 14825disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14826{
14827 /* Never disable a watchpoint scope breakpoint; we want to
14828 hit them when we leave scope so we can delete both the
14829 watchpoint and its scope breakpoint at that time. */
14830 if (bpt->type == bp_watchpoint_scope)
14831 return;
14832
b5de0fa7 14833 bpt->enable_state = bp_disabled;
c906108c 14834
b775012e
LM
14835 /* Mark breakpoint locations modified. */
14836 mark_breakpoint_modified (bpt);
14837
d248b706
KY
14838 if (target_supports_enable_disable_tracepoint ()
14839 && current_trace_status ()->running && is_tracepoint (bpt))
14840 {
14841 struct bp_location *location;
14842
14843 for (location = bpt->loc; location; location = location->next)
14844 target_disable_tracepoint (location);
14845 }
14846
44702360 14847 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14848
8d3788bd 14849 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14850}
14851
51be5b68
PA
14852/* A callback for iterate_over_related_breakpoints. */
14853
14854static void
14855do_disable_breakpoint (struct breakpoint *b, void *ignore)
14856{
14857 disable_breakpoint (b);
14858}
14859
95a42b64
TT
14860/* A callback for map_breakpoint_numbers that calls
14861 disable_breakpoint. */
14862
14863static void
14864do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14865{
51be5b68 14866 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14867}
14868
c906108c 14869static void
fba45db2 14870disable_command (char *args, int from_tty)
c906108c 14871{
c906108c 14872 if (args == 0)
46c6471b
PA
14873 {
14874 struct breakpoint *bpt;
14875
14876 ALL_BREAKPOINTS (bpt)
14877 if (user_breakpoint_p (bpt))
14878 disable_breakpoint (bpt);
14879 }
9eaabc75 14880 else
0d381245 14881 {
9eaabc75
MW
14882 char *num = extract_arg (&args);
14883
14884 while (num)
d248b706 14885 {
9eaabc75 14886 if (strchr (num, '.'))
b775012e 14887 {
9eaabc75
MW
14888 struct bp_location *loc = find_location_by_number (num);
14889
14890 if (loc)
14891 {
14892 if (loc->enabled)
14893 {
14894 loc->enabled = 0;
14895 mark_breakpoint_location_modified (loc);
14896 }
14897 if (target_supports_enable_disable_tracepoint ()
14898 && current_trace_status ()->running && loc->owner
14899 && is_tracepoint (loc->owner))
14900 target_disable_tracepoint (loc);
14901 }
44702360 14902 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14903 }
9eaabc75
MW
14904 else
14905 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14906 num = extract_arg (&args);
d248b706 14907 }
0d381245 14908 }
c906108c
SS
14909}
14910
14911static void
816338b5
SS
14912enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14913 int count)
c906108c 14914{
afe38095 14915 int target_resources_ok;
c906108c
SS
14916
14917 if (bpt->type == bp_hardware_breakpoint)
14918 {
14919 int i;
c5aa993b 14920 i = hw_breakpoint_used_count ();
53a5351d 14921 target_resources_ok =
d92524f1 14922 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14923 i + 1, 0);
c906108c 14924 if (target_resources_ok == 0)
8a3fe4f8 14925 error (_("No hardware breakpoint support in the target."));
c906108c 14926 else if (target_resources_ok < 0)
8a3fe4f8 14927 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14928 }
14929
cc60f2e3 14930 if (is_watchpoint (bpt))
c906108c 14931 {
d07205c2 14932 /* Initialize it just to avoid a GCC false warning. */
f486487f 14933 enum enable_state orig_enable_state = bp_disabled;
dde02812 14934
492d29ea 14935 TRY
c906108c 14936 {
3a5c3e22
PA
14937 struct watchpoint *w = (struct watchpoint *) bpt;
14938
1e718ff1
TJB
14939 orig_enable_state = bpt->enable_state;
14940 bpt->enable_state = bp_enabled;
3a5c3e22 14941 update_watchpoint (w, 1 /* reparse */);
c906108c 14942 }
492d29ea 14943 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14944 {
1e718ff1 14945 bpt->enable_state = orig_enable_state;
dde02812
ES
14946 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14947 bpt->number);
14948 return;
c5aa993b 14949 }
492d29ea 14950 END_CATCH
c906108c 14951 }
0101ce28 14952
b775012e
LM
14953 bpt->enable_state = bp_enabled;
14954
14955 /* Mark breakpoint locations modified. */
14956 mark_breakpoint_modified (bpt);
14957
d248b706
KY
14958 if (target_supports_enable_disable_tracepoint ()
14959 && current_trace_status ()->running && is_tracepoint (bpt))
14960 {
14961 struct bp_location *location;
14962
14963 for (location = bpt->loc; location; location = location->next)
14964 target_enable_tracepoint (location);
14965 }
14966
b4c291bb 14967 bpt->disposition = disposition;
816338b5 14968 bpt->enable_count = count;
44702360 14969 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14970
8d3788bd 14971 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14972}
14973
fe3f5fa8 14974
c906108c 14975void
fba45db2 14976enable_breakpoint (struct breakpoint *bpt)
c906108c 14977{
816338b5 14978 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14979}
14980
14981static void
14982do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14983{
14984 enable_breakpoint (bpt);
c906108c
SS
14985}
14986
95a42b64
TT
14987/* A callback for map_breakpoint_numbers that calls
14988 enable_breakpoint. */
14989
14990static void
14991do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14992{
51be5b68 14993 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14994}
14995
c906108c
SS
14996/* The enable command enables the specified breakpoints (or all defined
14997 breakpoints) so they once again become (or continue to be) effective
1272ad14 14998 in stopping the inferior. */
c906108c 14999
c906108c 15000static void
fba45db2 15001enable_command (char *args, int from_tty)
c906108c 15002{
c906108c 15003 if (args == 0)
46c6471b
PA
15004 {
15005 struct breakpoint *bpt;
15006
15007 ALL_BREAKPOINTS (bpt)
15008 if (user_breakpoint_p (bpt))
15009 enable_breakpoint (bpt);
15010 }
9eaabc75 15011 else
0d381245 15012 {
9eaabc75
MW
15013 char *num = extract_arg (&args);
15014
15015 while (num)
d248b706 15016 {
9eaabc75 15017 if (strchr (num, '.'))
b775012e 15018 {
9eaabc75
MW
15019 struct bp_location *loc = find_location_by_number (num);
15020
15021 if (loc)
15022 {
15023 if (!loc->enabled)
15024 {
15025 loc->enabled = 1;
15026 mark_breakpoint_location_modified (loc);
15027 }
15028 if (target_supports_enable_disable_tracepoint ()
15029 && current_trace_status ()->running && loc->owner
15030 && is_tracepoint (loc->owner))
15031 target_enable_tracepoint (loc);
15032 }
44702360 15033 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15034 }
9eaabc75
MW
15035 else
15036 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15037 num = extract_arg (&args);
d248b706 15038 }
0d381245 15039 }
c906108c
SS
15040}
15041
816338b5
SS
15042/* This struct packages up disposition data for application to multiple
15043 breakpoints. */
15044
15045struct disp_data
15046{
15047 enum bpdisp disp;
15048 int count;
15049};
15050
c906108c 15051static void
51be5b68
PA
15052do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15053{
816338b5 15054 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15055
816338b5 15056 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15057}
15058
15059static void
15060do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15061{
816338b5 15062 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15063
15064 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15065}
15066
c906108c 15067static void
fba45db2 15068enable_once_command (char *args, int from_tty)
c906108c 15069{
51be5b68 15070 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15071}
15072
816338b5
SS
15073static void
15074do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15075{
15076 struct disp_data disp = { disp_disable, *(int *) countptr };
15077
15078 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15079}
15080
15081static void
15082enable_count_command (char *args, int from_tty)
15083{
b9d61307
SM
15084 int count;
15085
15086 if (args == NULL)
15087 error_no_arg (_("hit count"));
15088
15089 count = get_number (&args);
816338b5
SS
15090
15091 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15092}
15093
c906108c 15094static void
51be5b68 15095do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15096{
816338b5 15097 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15098
15099 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15100}
15101
c906108c 15102static void
fba45db2 15103enable_delete_command (char *args, int from_tty)
c906108c 15104{
51be5b68 15105 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15106}
15107\f
fa8d40ab
JJ
15108static void
15109set_breakpoint_cmd (char *args, int from_tty)
15110{
15111}
15112
15113static void
15114show_breakpoint_cmd (char *args, int from_tty)
15115{
15116}
15117
1f3b5d1b
PP
15118/* Invalidate last known value of any hardware watchpoint if
15119 the memory which that value represents has been written to by
15120 GDB itself. */
15121
15122static void
8de0566d
YQ
15123invalidate_bp_value_on_memory_change (struct inferior *inferior,
15124 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15125 const bfd_byte *data)
15126{
15127 struct breakpoint *bp;
15128
15129 ALL_BREAKPOINTS (bp)
15130 if (bp->enable_state == bp_enabled
3a5c3e22 15131 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15132 {
3a5c3e22 15133 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15134
3a5c3e22
PA
15135 if (wp->val_valid && wp->val)
15136 {
15137 struct bp_location *loc;
15138
15139 for (loc = bp->loc; loc != NULL; loc = loc->next)
15140 if (loc->loc_type == bp_loc_hardware_watchpoint
15141 && loc->address + loc->length > addr
15142 && addr + len > loc->address)
15143 {
15144 value_free (wp->val);
15145 wp->val = NULL;
15146 wp->val_valid = 0;
15147 }
15148 }
1f3b5d1b
PP
15149 }
15150}
15151
8181d85f
DJ
15152/* Create and insert a breakpoint for software single step. */
15153
15154void
6c95b8df 15155insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15156 struct address_space *aspace,
15157 CORE_ADDR next_pc)
8181d85f 15158{
7c16b83e
PA
15159 struct thread_info *tp = inferior_thread ();
15160 struct symtab_and_line sal;
15161 CORE_ADDR pc = next_pc;
8181d85f 15162
34b7e8a6
PA
15163 if (tp->control.single_step_breakpoints == NULL)
15164 {
15165 tp->control.single_step_breakpoints
5d5658a1 15166 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15167 }
8181d85f 15168
7c16b83e
PA
15169 sal = find_pc_line (pc, 0);
15170 sal.pc = pc;
15171 sal.section = find_pc_overlay (pc);
15172 sal.explicit_pc = 1;
34b7e8a6 15173 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15174
7c16b83e 15175 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15176}
15177
34b7e8a6 15178/* See breakpoint.h. */
f02253f1
HZ
15179
15180int
7c16b83e
PA
15181breakpoint_has_location_inserted_here (struct breakpoint *bp,
15182 struct address_space *aspace,
15183 CORE_ADDR pc)
1aafd4da 15184{
7c16b83e 15185 struct bp_location *loc;
1aafd4da 15186
7c16b83e
PA
15187 for (loc = bp->loc; loc != NULL; loc = loc->next)
15188 if (loc->inserted
15189 && breakpoint_location_address_match (loc, aspace, pc))
15190 return 1;
1aafd4da 15191
7c16b83e 15192 return 0;
ef370185
JB
15193}
15194
15195/* Check whether a software single-step breakpoint is inserted at
15196 PC. */
15197
15198int
15199single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15200 CORE_ADDR pc)
15201{
34b7e8a6
PA
15202 struct breakpoint *bpt;
15203
15204 ALL_BREAKPOINTS (bpt)
15205 {
15206 if (bpt->type == bp_single_step
15207 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15208 return 1;
15209 }
15210 return 0;
1aafd4da
UW
15211}
15212
1042e4c0
SS
15213/* Tracepoint-specific operations. */
15214
15215/* Set tracepoint count to NUM. */
15216static void
15217set_tracepoint_count (int num)
15218{
15219 tracepoint_count = num;
4fa62494 15220 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15221}
15222
70221824 15223static void
1042e4c0
SS
15224trace_command (char *arg, int from_tty)
15225{
55aa24fb 15226 struct breakpoint_ops *ops;
f00aae0f
KS
15227 struct event_location *location;
15228 struct cleanup *back_to;
55aa24fb 15229
f00aae0f
KS
15230 location = string_to_event_location (&arg, current_language);
15231 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15232 if (location != NULL
15233 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15234 ops = &tracepoint_probe_breakpoint_ops;
15235 else
15236 ops = &tracepoint_breakpoint_ops;
15237
558a9d82 15238 create_breakpoint (get_current_arch (),
f00aae0f
KS
15239 location,
15240 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15241 0 /* tempflag */,
15242 bp_tracepoint /* type_wanted */,
15243 0 /* Ignore count */,
15244 pending_break_support,
15245 ops,
15246 from_tty,
15247 1 /* enabled */,
15248 0 /* internal */, 0);
f00aae0f 15249 do_cleanups (back_to);
1042e4c0
SS
15250}
15251
70221824 15252static void
7a697b8d
SS
15253ftrace_command (char *arg, int from_tty)
15254{
f00aae0f
KS
15255 struct event_location *location;
15256 struct cleanup *back_to;
15257
15258 location = string_to_event_location (&arg, current_language);
15259 back_to = make_cleanup_delete_event_location (location);
558a9d82 15260 create_breakpoint (get_current_arch (),
f00aae0f
KS
15261 location,
15262 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15263 0 /* tempflag */,
15264 bp_fast_tracepoint /* type_wanted */,
15265 0 /* Ignore count */,
15266 pending_break_support,
15267 &tracepoint_breakpoint_ops,
15268 from_tty,
15269 1 /* enabled */,
15270 0 /* internal */, 0);
f00aae0f 15271 do_cleanups (back_to);
0fb4aa4b
PA
15272}
15273
15274/* strace command implementation. Creates a static tracepoint. */
15275
70221824 15276static void
0fb4aa4b
PA
15277strace_command (char *arg, int from_tty)
15278{
983af33b 15279 struct breakpoint_ops *ops;
f00aae0f
KS
15280 struct event_location *location;
15281 struct cleanup *back_to;
983af33b
SDJ
15282
15283 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15284 or with a normal static tracepoint. */
61012eef 15285 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15286 {
15287 ops = &strace_marker_breakpoint_ops;
15288 location = new_linespec_location (&arg);
15289 }
983af33b 15290 else
f00aae0f
KS
15291 {
15292 ops = &tracepoint_breakpoint_ops;
15293 location = string_to_event_location (&arg, current_language);
15294 }
983af33b 15295
f00aae0f 15296 back_to = make_cleanup_delete_event_location (location);
558a9d82 15297 create_breakpoint (get_current_arch (),
f00aae0f
KS
15298 location,
15299 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15300 0 /* tempflag */,
15301 bp_static_tracepoint /* type_wanted */,
15302 0 /* Ignore count */,
15303 pending_break_support,
15304 ops,
15305 from_tty,
15306 1 /* enabled */,
15307 0 /* internal */, 0);
f00aae0f 15308 do_cleanups (back_to);
7a697b8d
SS
15309}
15310
409873ef
SS
15311/* Set up a fake reader function that gets command lines from a linked
15312 list that was acquired during tracepoint uploading. */
15313
15314static struct uploaded_tp *this_utp;
3149d8c1 15315static int next_cmd;
409873ef
SS
15316
15317static char *
15318read_uploaded_action (void)
15319{
15320 char *rslt;
15321
3149d8c1 15322 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15323
3149d8c1 15324 next_cmd++;
409873ef
SS
15325
15326 return rslt;
15327}
15328
00bf0b85
SS
15329/* Given information about a tracepoint as recorded on a target (which
15330 can be either a live system or a trace file), attempt to create an
15331 equivalent GDB tracepoint. This is not a reliable process, since
15332 the target does not necessarily have all the information used when
15333 the tracepoint was originally defined. */
15334
d9b3f62e 15335struct tracepoint *
00bf0b85 15336create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15337{
409873ef 15338 char *addr_str, small_buf[100];
d9b3f62e 15339 struct tracepoint *tp;
f00aae0f
KS
15340 struct event_location *location;
15341 struct cleanup *cleanup;
fd9b8c24 15342
409873ef
SS
15343 if (utp->at_string)
15344 addr_str = utp->at_string;
15345 else
15346 {
15347 /* In the absence of a source location, fall back to raw
15348 address. Since there is no way to confirm that the address
15349 means the same thing as when the trace was started, warn the
15350 user. */
3e43a32a
MS
15351 warning (_("Uploaded tracepoint %d has no "
15352 "source location, using raw address"),
409873ef 15353 utp->number);
8c042590 15354 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15355 addr_str = small_buf;
15356 }
15357
15358 /* There's not much we can do with a sequence of bytecodes. */
15359 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15360 warning (_("Uploaded tracepoint %d condition "
15361 "has no source form, ignoring it"),
409873ef 15362 utp->number);
d5551862 15363
f00aae0f
KS
15364 location = string_to_event_location (&addr_str, current_language);
15365 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15366 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15367 location,
15368 utp->cond_string, -1, addr_str,
e7e0cddf 15369 0 /* parse cond/thread */,
8cdf0e15 15370 0 /* tempflag */,
0fb4aa4b 15371 utp->type /* type_wanted */,
8cdf0e15
VP
15372 0 /* Ignore count */,
15373 pending_break_support,
348d480f 15374 &tracepoint_breakpoint_ops,
8cdf0e15 15375 0 /* from_tty */,
84f4c1fe 15376 utp->enabled /* enabled */,
44f238bb
PA
15377 0 /* internal */,
15378 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15379 {
15380 do_cleanups (cleanup);
15381 return NULL;
15382 }
15383
15384 do_cleanups (cleanup);
fd9b8c24 15385
409873ef 15386 /* Get the tracepoint we just created. */
fd9b8c24
PA
15387 tp = get_tracepoint (tracepoint_count);
15388 gdb_assert (tp != NULL);
d5551862 15389
00bf0b85
SS
15390 if (utp->pass > 0)
15391 {
8c042590
PM
15392 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15393 tp->base.number);
00bf0b85 15394
409873ef 15395 trace_pass_command (small_buf, 0);
00bf0b85
SS
15396 }
15397
409873ef
SS
15398 /* If we have uploaded versions of the original commands, set up a
15399 special-purpose "reader" function and call the usual command line
15400 reader, then pass the result to the breakpoint command-setting
15401 function. */
3149d8c1 15402 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15403 {
409873ef 15404 struct command_line *cmd_list;
00bf0b85 15405
409873ef 15406 this_utp = utp;
3149d8c1 15407 next_cmd = 0;
d5551862 15408
409873ef
SS
15409 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15410
d9b3f62e 15411 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15412 }
3149d8c1
SS
15413 else if (!VEC_empty (char_ptr, utp->actions)
15414 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15415 warning (_("Uploaded tracepoint %d actions "
15416 "have no source form, ignoring them"),
409873ef 15417 utp->number);
00bf0b85 15418
f196051f
SS
15419 /* Copy any status information that might be available. */
15420 tp->base.hit_count = utp->hit_count;
15421 tp->traceframe_usage = utp->traceframe_usage;
15422
00bf0b85 15423 return tp;
d9b3f62e 15424}
00bf0b85 15425
1042e4c0
SS
15426/* Print information on tracepoint number TPNUM_EXP, or all if
15427 omitted. */
15428
15429static void
e5a67952 15430tracepoints_info (char *args, int from_tty)
1042e4c0 15431{
79a45e25 15432 struct ui_out *uiout = current_uiout;
e5a67952 15433 int num_printed;
1042e4c0 15434
e5a67952 15435 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15436
15437 if (num_printed == 0)
1042e4c0 15438 {
e5a67952 15439 if (args == NULL || *args == '\0')
d77f58be
SS
15440 ui_out_message (uiout, 0, "No tracepoints.\n");
15441 else
e5a67952 15442 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15443 }
ad443146
SS
15444
15445 default_collect_info ();
1042e4c0
SS
15446}
15447
4a64f543 15448/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15449 Not supported by all targets. */
15450static void
15451enable_trace_command (char *args, int from_tty)
15452{
15453 enable_command (args, from_tty);
15454}
15455
4a64f543 15456/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15457 Not supported by all targets. */
15458static void
15459disable_trace_command (char *args, int from_tty)
15460{
15461 disable_command (args, from_tty);
15462}
15463
4a64f543 15464/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15465static void
15466delete_trace_command (char *arg, int from_tty)
15467{
35df4500 15468 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15469
15470 dont_repeat ();
15471
15472 if (arg == 0)
15473 {
15474 int breaks_to_delete = 0;
15475
15476 /* Delete all breakpoints if no argument.
15477 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15478 have to be deleted with an explicit breakpoint number
15479 argument. */
1042e4c0 15480 ALL_TRACEPOINTS (b)
46c6471b 15481 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15482 {
15483 breaks_to_delete = 1;
15484 break;
15485 }
1042e4c0
SS
15486
15487 /* Ask user only if there are some breakpoints to delete. */
15488 if (!from_tty
15489 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15490 {
35df4500 15491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15492 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15493 delete_breakpoint (b);
1042e4c0
SS
15494 }
15495 }
15496 else
51be5b68 15497 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15498}
15499
197f0a60
TT
15500/* Helper function for trace_pass_command. */
15501
15502static void
d9b3f62e 15503trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15504{
d9b3f62e 15505 tp->pass_count = count;
6f6484cd 15506 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15507 if (from_tty)
15508 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15509 tp->base.number, count);
197f0a60
TT
15510}
15511
1042e4c0
SS
15512/* Set passcount for tracepoint.
15513
15514 First command argument is passcount, second is tracepoint number.
15515 If tracepoint number omitted, apply to most recently defined.
15516 Also accepts special argument "all". */
15517
15518static void
15519trace_pass_command (char *args, int from_tty)
15520{
d9b3f62e 15521 struct tracepoint *t1;
1042e4c0 15522 unsigned int count;
1042e4c0
SS
15523
15524 if (args == 0 || *args == 0)
3e43a32a
MS
15525 error (_("passcount command requires an "
15526 "argument (count + optional TP num)"));
1042e4c0 15527
4a64f543 15528 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15529
529480d0 15530 args = skip_spaces (args);
1042e4c0
SS
15531 if (*args && strncasecmp (args, "all", 3) == 0)
15532 {
d9b3f62e
PA
15533 struct breakpoint *b;
15534
1042e4c0 15535 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15536 if (*args)
15537 error (_("Junk at end of arguments."));
1042e4c0 15538
d9b3f62e 15539 ALL_TRACEPOINTS (b)
197f0a60 15540 {
d9b3f62e 15541 t1 = (struct tracepoint *) b;
197f0a60
TT
15542 trace_pass_set_count (t1, count, from_tty);
15543 }
15544 }
15545 else if (*args == '\0')
1042e4c0 15546 {
5fa1d40e 15547 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15548 if (t1)
197f0a60
TT
15549 trace_pass_set_count (t1, count, from_tty);
15550 }
15551 else
15552 {
15553 struct get_number_or_range_state state;
15554
15555 init_number_or_range (&state, args);
15556 while (!state.finished)
1042e4c0 15557 {
5fa1d40e 15558 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15559 if (t1)
15560 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15561 }
15562 }
1042e4c0
SS
15563}
15564
d9b3f62e 15565struct tracepoint *
1042e4c0
SS
15566get_tracepoint (int num)
15567{
15568 struct breakpoint *t;
15569
15570 ALL_TRACEPOINTS (t)
15571 if (t->number == num)
d9b3f62e 15572 return (struct tracepoint *) t;
1042e4c0
SS
15573
15574 return NULL;
15575}
15576
d5551862
SS
15577/* Find the tracepoint with the given target-side number (which may be
15578 different from the tracepoint number after disconnecting and
15579 reconnecting). */
15580
d9b3f62e 15581struct tracepoint *
d5551862
SS
15582get_tracepoint_by_number_on_target (int num)
15583{
d9b3f62e 15584 struct breakpoint *b;
d5551862 15585
d9b3f62e
PA
15586 ALL_TRACEPOINTS (b)
15587 {
15588 struct tracepoint *t = (struct tracepoint *) b;
15589
15590 if (t->number_on_target == num)
15591 return t;
15592 }
d5551862
SS
15593
15594 return NULL;
15595}
15596
1042e4c0 15597/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15598 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15599 If the argument is missing, the most recent tracepoint
15600 (tracepoint_count) is returned. */
15601
d9b3f62e 15602struct tracepoint *
197f0a60 15603get_tracepoint_by_number (char **arg,
5fa1d40e 15604 struct get_number_or_range_state *state)
1042e4c0 15605{
1042e4c0
SS
15606 struct breakpoint *t;
15607 int tpnum;
15608 char *instring = arg == NULL ? NULL : *arg;
15609
197f0a60
TT
15610 if (state)
15611 {
15612 gdb_assert (!state->finished);
15613 tpnum = get_number_or_range (state);
15614 }
15615 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15616 tpnum = tracepoint_count;
1042e4c0 15617 else
197f0a60 15618 tpnum = get_number (arg);
1042e4c0
SS
15619
15620 if (tpnum <= 0)
15621 {
15622 if (instring && *instring)
15623 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15624 instring);
15625 else
5fa1d40e 15626 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15627 return NULL;
15628 }
15629
15630 ALL_TRACEPOINTS (t)
15631 if (t->number == tpnum)
15632 {
d9b3f62e 15633 return (struct tracepoint *) t;
1042e4c0
SS
15634 }
15635
1042e4c0
SS
15636 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15637 return NULL;
15638}
15639
d9b3f62e
PA
15640void
15641print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15642{
15643 if (b->thread != -1)
15644 fprintf_unfiltered (fp, " thread %d", b->thread);
15645
15646 if (b->task != 0)
15647 fprintf_unfiltered (fp, " task %d", b->task);
15648
15649 fprintf_unfiltered (fp, "\n");
15650}
15651
6149aea9
PA
15652/* Save information on user settable breakpoints (watchpoints, etc) to
15653 a new script file named FILENAME. If FILTER is non-NULL, call it
15654 on each breakpoint and only include the ones for which it returns
15655 non-zero. */
15656
1042e4c0 15657static void
6149aea9
PA
15658save_breakpoints (char *filename, int from_tty,
15659 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15660{
15661 struct breakpoint *tp;
6149aea9 15662 int any = 0;
1042e4c0 15663 struct cleanup *cleanup;
a7bdde9e 15664 struct ui_file *fp;
6149aea9 15665 int extra_trace_bits = 0;
1042e4c0 15666
6149aea9
PA
15667 if (filename == 0 || *filename == 0)
15668 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15669
15670 /* See if we have anything to save. */
6149aea9 15671 ALL_BREAKPOINTS (tp)
1042e4c0 15672 {
6149aea9 15673 /* Skip internal and momentary breakpoints. */
09d682a4 15674 if (!user_breakpoint_p (tp))
6149aea9
PA
15675 continue;
15676
15677 /* If we have a filter, only save the breakpoints it accepts. */
15678 if (filter && !filter (tp))
15679 continue;
15680
15681 any = 1;
15682
15683 if (is_tracepoint (tp))
15684 {
15685 extra_trace_bits = 1;
15686
15687 /* We can stop searching. */
15688 break;
15689 }
1042e4c0 15690 }
6149aea9
PA
15691
15692 if (!any)
1042e4c0 15693 {
6149aea9 15694 warning (_("Nothing to save."));
1042e4c0
SS
15695 return;
15696 }
15697
c718be47
PA
15698 filename = tilde_expand (filename);
15699 cleanup = make_cleanup (xfree, filename);
15700 fp = gdb_fopen (filename, "w");
059fb39f 15701 if (!fp)
6149aea9
PA
15702 error (_("Unable to open file '%s' for saving (%s)"),
15703 filename, safe_strerror (errno));
a7bdde9e 15704 make_cleanup_ui_file_delete (fp);
8bf6485c 15705
6149aea9
PA
15706 if (extra_trace_bits)
15707 save_trace_state_variables (fp);
8bf6485c 15708
6149aea9 15709 ALL_BREAKPOINTS (tp)
1042e4c0 15710 {
6149aea9 15711 /* Skip internal and momentary breakpoints. */
09d682a4 15712 if (!user_breakpoint_p (tp))
6149aea9 15713 continue;
8bf6485c 15714
6149aea9
PA
15715 /* If we have a filter, only save the breakpoints it accepts. */
15716 if (filter && !filter (tp))
15717 continue;
15718
348d480f 15719 tp->ops->print_recreate (tp, fp);
1042e4c0 15720
6149aea9
PA
15721 /* Note, we can't rely on tp->number for anything, as we can't
15722 assume the recreated breakpoint numbers will match. Use $bpnum
15723 instead. */
15724
15725 if (tp->cond_string)
15726 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15727
15728 if (tp->ignore_count)
15729 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15730
2d9442cc 15731 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15732 {
6c63c96a 15733 struct gdb_exception exception;
a7bdde9e 15734
6149aea9 15735 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15736
79a45e25 15737 ui_out_redirect (current_uiout, fp);
492d29ea 15738 TRY
1042e4c0 15739 {
79a45e25 15740 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15741 }
492d29ea
PA
15742 CATCH (ex, RETURN_MASK_ALL)
15743 {
6c63c96a 15744 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15745 throw_exception (ex);
15746 }
15747 END_CATCH
1042e4c0 15748
6c63c96a 15749 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15750 fprintf_unfiltered (fp, " end\n");
1042e4c0 15751 }
6149aea9
PA
15752
15753 if (tp->enable_state == bp_disabled)
99894e11 15754 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15755
15756 /* If this is a multi-location breakpoint, check if the locations
15757 should be individually disabled. Watchpoint locations are
15758 special, and not user visible. */
15759 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15760 {
15761 struct bp_location *loc;
15762 int n = 1;
15763
15764 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15765 if (!loc->enabled)
15766 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15767 }
1042e4c0 15768 }
8bf6485c 15769
6149aea9 15770 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15771 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15772
1042e4c0 15773 if (from_tty)
6149aea9 15774 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15775 do_cleanups (cleanup);
6149aea9
PA
15776}
15777
15778/* The `save breakpoints' command. */
15779
15780static void
15781save_breakpoints_command (char *args, int from_tty)
15782{
15783 save_breakpoints (args, from_tty, NULL);
15784}
15785
15786/* The `save tracepoints' command. */
15787
15788static void
15789save_tracepoints_command (char *args, int from_tty)
15790{
15791 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15792}
15793
15794/* Create a vector of all tracepoints. */
15795
15796VEC(breakpoint_p) *
eeae04df 15797all_tracepoints (void)
1042e4c0
SS
15798{
15799 VEC(breakpoint_p) *tp_vec = 0;
15800 struct breakpoint *tp;
15801
15802 ALL_TRACEPOINTS (tp)
15803 {
15804 VEC_safe_push (breakpoint_p, tp_vec, tp);
15805 }
15806
15807 return tp_vec;
15808}
15809
c906108c 15810\f
629500fa
KS
15811/* This help string is used to consolidate all the help string for specifying
15812 locations used by several commands. */
15813
15814#define LOCATION_HELP_STRING \
15815"Linespecs are colon-separated lists of location parameters, such as\n\
15816source filename, function name, label name, and line number.\n\
15817Example: To specify the start of a label named \"the_top\" in the\n\
15818function \"fact\" in the file \"factorial.c\", use\n\
15819\"factorial.c:fact:the_top\".\n\
15820\n\
15821Address locations begin with \"*\" and specify an exact address in the\n\
15822program. Example: To specify the fourth byte past the start function\n\
15823\"main\", use \"*main + 4\".\n\
15824\n\
15825Explicit locations are similar to linespecs but use an option/argument\n\
15826syntax to specify location parameters.\n\
15827Example: To specify the start of the label named \"the_top\" in the\n\
15828function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15829-function fact -label the_top\".\n"
15830
4a64f543
MS
15831/* This help string is used for the break, hbreak, tbreak and thbreak
15832 commands. It is defined as a macro to prevent duplication.
15833 COMMAND should be a string constant containing the name of the
15834 command. */
629500fa 15835
31e2b00f 15836#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15837command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15838PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15839probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15840guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15841`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15842LOCATION may be a linespec, address, or explicit location as described\n\
15843below.\n\
15844\n\
dc10affe
PA
15845With no LOCATION, uses current execution address of the selected\n\
15846stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15847\n\
15848THREADNUM is the number from \"info threads\".\n\
15849CONDITION is a boolean expression.\n\
629500fa 15850\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15851Multiple breakpoints at one place are permitted, and useful if their\n\
15852conditions are different.\n\
31e2b00f
AS
15853\n\
15854Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15855
44feb3ce
TT
15856/* List of subcommands for "catch". */
15857static struct cmd_list_element *catch_cmdlist;
15858
15859/* List of subcommands for "tcatch". */
15860static struct cmd_list_element *tcatch_cmdlist;
15861
9ac4176b 15862void
44feb3ce 15863add_catch_command (char *name, char *docstring,
82ae6c8d 15864 cmd_sfunc_ftype *sfunc,
625e8578 15865 completer_ftype *completer,
44feb3ce
TT
15866 void *user_data_catch,
15867 void *user_data_tcatch)
15868{
15869 struct cmd_list_element *command;
15870
15871 command = add_cmd (name, class_breakpoint, NULL, docstring,
15872 &catch_cmdlist);
15873 set_cmd_sfunc (command, sfunc);
15874 set_cmd_context (command, user_data_catch);
a96d9b2e 15875 set_cmd_completer (command, completer);
44feb3ce
TT
15876
15877 command = add_cmd (name, class_breakpoint, NULL, docstring,
15878 &tcatch_cmdlist);
15879 set_cmd_sfunc (command, sfunc);
15880 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15881 set_cmd_completer (command, completer);
44feb3ce
TT
15882}
15883
6149aea9
PA
15884static void
15885save_command (char *arg, int from_tty)
15886{
3e43a32a
MS
15887 printf_unfiltered (_("\"save\" must be followed by "
15888 "the name of a save subcommand.\n"));
635c7e8a 15889 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15890}
15891
84f4c1fe
PM
15892struct breakpoint *
15893iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15894 void *data)
15895{
35df4500 15896 struct breakpoint *b, *b_tmp;
84f4c1fe 15897
35df4500 15898 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15899 {
15900 if ((*callback) (b, data))
15901 return b;
15902 }
15903
15904 return NULL;
15905}
15906
0574c78f
GB
15907/* Zero if any of the breakpoint's locations could be a location where
15908 functions have been inlined, nonzero otherwise. */
15909
15910static int
15911is_non_inline_function (struct breakpoint *b)
15912{
15913 /* The shared library event breakpoint is set on the address of a
15914 non-inline function. */
15915 if (b->type == bp_shlib_event)
15916 return 1;
15917
15918 return 0;
15919}
15920
15921/* Nonzero if the specified PC cannot be a location where functions
15922 have been inlined. */
15923
15924int
09ac7c10
TT
15925pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15926 const struct target_waitstatus *ws)
0574c78f
GB
15927{
15928 struct breakpoint *b;
15929 struct bp_location *bl;
15930
15931 ALL_BREAKPOINTS (b)
15932 {
15933 if (!is_non_inline_function (b))
15934 continue;
15935
15936 for (bl = b->loc; bl != NULL; bl = bl->next)
15937 {
15938 if (!bl->shlib_disabled
09ac7c10 15939 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15940 return 1;
15941 }
15942 }
15943
15944 return 0;
15945}
15946
2f202fde
JK
15947/* Remove any references to OBJFILE which is going to be freed. */
15948
15949void
15950breakpoint_free_objfile (struct objfile *objfile)
15951{
15952 struct bp_location **locp, *loc;
15953
15954 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15955 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15956 loc->symtab = NULL;
15957}
15958
2060206e
PA
15959void
15960initialize_breakpoint_ops (void)
15961{
15962 static int initialized = 0;
15963
15964 struct breakpoint_ops *ops;
15965
15966 if (initialized)
15967 return;
15968 initialized = 1;
15969
15970 /* The breakpoint_ops structure to be inherit by all kinds of
15971 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15972 internal and momentary breakpoints, etc.). */
15973 ops = &bkpt_base_breakpoint_ops;
15974 *ops = base_breakpoint_ops;
15975 ops->re_set = bkpt_re_set;
15976 ops->insert_location = bkpt_insert_location;
15977 ops->remove_location = bkpt_remove_location;
15978 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15979 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15980 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15981 ops->decode_location = bkpt_decode_location;
2060206e
PA
15982
15983 /* The breakpoint_ops structure to be used in regular breakpoints. */
15984 ops = &bkpt_breakpoint_ops;
15985 *ops = bkpt_base_breakpoint_ops;
15986 ops->re_set = bkpt_re_set;
15987 ops->resources_needed = bkpt_resources_needed;
15988 ops->print_it = bkpt_print_it;
15989 ops->print_mention = bkpt_print_mention;
15990 ops->print_recreate = bkpt_print_recreate;
15991
15992 /* Ranged breakpoints. */
15993 ops = &ranged_breakpoint_ops;
15994 *ops = bkpt_breakpoint_ops;
15995 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15996 ops->resources_needed = resources_needed_ranged_breakpoint;
15997 ops->print_it = print_it_ranged_breakpoint;
15998 ops->print_one = print_one_ranged_breakpoint;
15999 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16000 ops->print_mention = print_mention_ranged_breakpoint;
16001 ops->print_recreate = print_recreate_ranged_breakpoint;
16002
16003 /* Internal breakpoints. */
16004 ops = &internal_breakpoint_ops;
16005 *ops = bkpt_base_breakpoint_ops;
16006 ops->re_set = internal_bkpt_re_set;
16007 ops->check_status = internal_bkpt_check_status;
16008 ops->print_it = internal_bkpt_print_it;
16009 ops->print_mention = internal_bkpt_print_mention;
16010
16011 /* Momentary breakpoints. */
16012 ops = &momentary_breakpoint_ops;
16013 *ops = bkpt_base_breakpoint_ops;
16014 ops->re_set = momentary_bkpt_re_set;
16015 ops->check_status = momentary_bkpt_check_status;
16016 ops->print_it = momentary_bkpt_print_it;
16017 ops->print_mention = momentary_bkpt_print_mention;
16018
e2e4d78b
JK
16019 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16020 ops = &longjmp_breakpoint_ops;
16021 *ops = momentary_breakpoint_ops;
16022 ops->dtor = longjmp_bkpt_dtor;
16023
55aa24fb
SDJ
16024 /* Probe breakpoints. */
16025 ops = &bkpt_probe_breakpoint_ops;
16026 *ops = bkpt_breakpoint_ops;
16027 ops->insert_location = bkpt_probe_insert_location;
16028 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
16029 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16030 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 16031
2060206e
PA
16032 /* Watchpoints. */
16033 ops = &watchpoint_breakpoint_ops;
16034 *ops = base_breakpoint_ops;
3a5c3e22 16035 ops->dtor = dtor_watchpoint;
2060206e
PA
16036 ops->re_set = re_set_watchpoint;
16037 ops->insert_location = insert_watchpoint;
16038 ops->remove_location = remove_watchpoint;
16039 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16040 ops->check_status = check_status_watchpoint;
16041 ops->resources_needed = resources_needed_watchpoint;
16042 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16043 ops->print_it = print_it_watchpoint;
16044 ops->print_mention = print_mention_watchpoint;
16045 ops->print_recreate = print_recreate_watchpoint;
427cd150 16046 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16047
16048 /* Masked watchpoints. */
16049 ops = &masked_watchpoint_breakpoint_ops;
16050 *ops = watchpoint_breakpoint_ops;
16051 ops->insert_location = insert_masked_watchpoint;
16052 ops->remove_location = remove_masked_watchpoint;
16053 ops->resources_needed = resources_needed_masked_watchpoint;
16054 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16055 ops->print_it = print_it_masked_watchpoint;
16056 ops->print_one_detail = print_one_detail_masked_watchpoint;
16057 ops->print_mention = print_mention_masked_watchpoint;
16058 ops->print_recreate = print_recreate_masked_watchpoint;
16059
16060 /* Tracepoints. */
16061 ops = &tracepoint_breakpoint_ops;
16062 *ops = base_breakpoint_ops;
16063 ops->re_set = tracepoint_re_set;
16064 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16065 ops->print_one_detail = tracepoint_print_one_detail;
16066 ops->print_mention = tracepoint_print_mention;
16067 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16068 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16069 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16070 ops->decode_location = tracepoint_decode_location;
983af33b 16071
55aa24fb
SDJ
16072 /* Probe tracepoints. */
16073 ops = &tracepoint_probe_breakpoint_ops;
16074 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16075 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16076 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16077
983af33b
SDJ
16078 /* Static tracepoints with marker (`-m'). */
16079 ops = &strace_marker_breakpoint_ops;
16080 *ops = tracepoint_breakpoint_ops;
5f700d83 16081 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16082 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16083 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16084
16085 /* Fork catchpoints. */
16086 ops = &catch_fork_breakpoint_ops;
16087 *ops = base_breakpoint_ops;
16088 ops->insert_location = insert_catch_fork;
16089 ops->remove_location = remove_catch_fork;
16090 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16091 ops->print_it = print_it_catch_fork;
16092 ops->print_one = print_one_catch_fork;
16093 ops->print_mention = print_mention_catch_fork;
16094 ops->print_recreate = print_recreate_catch_fork;
16095
16096 /* Vfork catchpoints. */
16097 ops = &catch_vfork_breakpoint_ops;
16098 *ops = base_breakpoint_ops;
16099 ops->insert_location = insert_catch_vfork;
16100 ops->remove_location = remove_catch_vfork;
16101 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16102 ops->print_it = print_it_catch_vfork;
16103 ops->print_one = print_one_catch_vfork;
16104 ops->print_mention = print_mention_catch_vfork;
16105 ops->print_recreate = print_recreate_catch_vfork;
16106
16107 /* Exec catchpoints. */
16108 ops = &catch_exec_breakpoint_ops;
16109 *ops = base_breakpoint_ops;
16110 ops->dtor = dtor_catch_exec;
16111 ops->insert_location = insert_catch_exec;
16112 ops->remove_location = remove_catch_exec;
16113 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16114 ops->print_it = print_it_catch_exec;
16115 ops->print_one = print_one_catch_exec;
16116 ops->print_mention = print_mention_catch_exec;
16117 ops->print_recreate = print_recreate_catch_exec;
16118
edcc5120
TT
16119 /* Solib-related catchpoints. */
16120 ops = &catch_solib_breakpoint_ops;
16121 *ops = base_breakpoint_ops;
16122 ops->dtor = dtor_catch_solib;
16123 ops->insert_location = insert_catch_solib;
16124 ops->remove_location = remove_catch_solib;
16125 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16126 ops->check_status = check_status_catch_solib;
16127 ops->print_it = print_it_catch_solib;
16128 ops->print_one = print_one_catch_solib;
16129 ops->print_mention = print_mention_catch_solib;
16130 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16131
16132 ops = &dprintf_breakpoint_ops;
16133 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16134 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16135 ops->resources_needed = bkpt_resources_needed;
16136 ops->print_it = bkpt_print_it;
16137 ops->print_mention = bkpt_print_mention;
2d9442cc 16138 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16139 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16140 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16141}
16142
8bfd80db
YQ
16143/* Chain containing all defined "enable breakpoint" subcommands. */
16144
16145static struct cmd_list_element *enablebreaklist = NULL;
16146
c906108c 16147void
fba45db2 16148_initialize_breakpoint (void)
c906108c
SS
16149{
16150 struct cmd_list_element *c;
16151
2060206e
PA
16152 initialize_breakpoint_ops ();
16153
84acb35a 16154 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16155 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16156 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16157
55aa24fb
SDJ
16158 breakpoint_objfile_key
16159 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16160
c906108c
SS
16161 breakpoint_chain = 0;
16162 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16163 before a breakpoint is set. */
16164 breakpoint_count = 0;
16165
1042e4c0
SS
16166 tracepoint_count = 0;
16167
1bedd215
AC
16168 add_com ("ignore", class_breakpoint, ignore_command, _("\
16169Set ignore-count of breakpoint number N to COUNT.\n\
16170Usage is `ignore N COUNT'."));
c906108c 16171
1bedd215
AC
16172 add_com ("commands", class_breakpoint, commands_command, _("\
16173Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16174Give breakpoint number as argument after \"commands\".\n\
16175With no argument, the targeted breakpoint is the last one set.\n\
16176The commands themselves follow starting on the next line.\n\
16177Type a line containing \"end\" to indicate the end of them.\n\
16178Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16179then no output is printed when it is hit, except what the commands print."));
c906108c 16180
d55637df 16181 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16182Specify breakpoint number N to break only if COND is true.\n\
c906108c 16183Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16184expression to be evaluated whenever breakpoint N is reached."));
d55637df 16185 set_cmd_completer (c, condition_completer);
c906108c 16186
1bedd215 16187 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16188Set a temporary breakpoint.\n\
c906108c
SS
16189Like \"break\" except the breakpoint is only temporary,\n\
16190so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16191by using \"enable delete\" on the breakpoint number.\n\
16192\n"
16193BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16194 set_cmd_completer (c, location_completer);
c94fdfd0 16195
1bedd215 16196 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16197Set a hardware assisted breakpoint.\n\
c906108c 16198Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16199some target hardware may not have this support.\n\
16200\n"
16201BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16202 set_cmd_completer (c, location_completer);
c906108c 16203
1bedd215 16204 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16205Set a temporary hardware assisted breakpoint.\n\
c906108c 16206Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16207so it will be deleted when hit.\n\
16208\n"
16209BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16210 set_cmd_completer (c, location_completer);
c906108c 16211
1bedd215
AC
16212 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16213Enable some breakpoints.\n\
c906108c
SS
16214Give breakpoint numbers (separated by spaces) as arguments.\n\
16215With no subcommand, breakpoints are enabled until you command otherwise.\n\
16216This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16217With a subcommand you can enable temporarily."),
c906108c 16218 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16219
16220 add_com_alias ("en", "enable", class_breakpoint, 1);
16221
84951ab5 16222 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16223Enable some breakpoints.\n\
c906108c
SS
16224Give breakpoint numbers (separated by spaces) as arguments.\n\
16225This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16226May be abbreviated to simply \"enable\".\n"),
c5aa993b 16227 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16228
1a966eab
AC
16229 add_cmd ("once", no_class, enable_once_command, _("\
16230Enable breakpoints for one hit. Give breakpoint numbers.\n\
16231If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16232 &enablebreaklist);
16233
1a966eab
AC
16234 add_cmd ("delete", no_class, enable_delete_command, _("\
16235Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16236If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16237 &enablebreaklist);
16238
816338b5
SS
16239 add_cmd ("count", no_class, enable_count_command, _("\
16240Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16241If a breakpoint is hit while enabled in this fashion,\n\
16242the count is decremented; when it reaches zero, the breakpoint is disabled."),
16243 &enablebreaklist);
16244
1a966eab
AC
16245 add_cmd ("delete", no_class, enable_delete_command, _("\
16246Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16247If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16248 &enablelist);
16249
1a966eab
AC
16250 add_cmd ("once", no_class, enable_once_command, _("\
16251Enable breakpoints for one hit. Give breakpoint numbers.\n\
16252If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16253 &enablelist);
16254
16255 add_cmd ("count", no_class, enable_count_command, _("\
16256Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16257If a breakpoint is hit while enabled in this fashion,\n\
16258the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16259 &enablelist);
16260
1bedd215
AC
16261 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16262Disable some breakpoints.\n\
c906108c
SS
16263Arguments are breakpoint numbers with spaces in between.\n\
16264To disable all breakpoints, give no argument.\n\
64b9b334 16265A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16266 &disablelist, "disable ", 1, &cmdlist);
16267 add_com_alias ("dis", "disable", class_breakpoint, 1);
16268 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16269
1a966eab
AC
16270 add_cmd ("breakpoints", class_alias, disable_command, _("\
16271Disable some breakpoints.\n\
c906108c
SS
16272Arguments are breakpoint numbers with spaces in between.\n\
16273To disable all breakpoints, give no argument.\n\
64b9b334 16274A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16275This command may be abbreviated \"disable\"."),
c906108c
SS
16276 &disablelist);
16277
1bedd215
AC
16278 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16279Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16280Arguments are breakpoint numbers with spaces in between.\n\
16281To delete all breakpoints, give no argument.\n\
16282\n\
16283Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16284The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16285 &deletelist, "delete ", 1, &cmdlist);
16286 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16287 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16288
1a966eab
AC
16289 add_cmd ("breakpoints", class_alias, delete_command, _("\
16290Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16291Arguments are breakpoint numbers with spaces in between.\n\
16292To delete all breakpoints, give no argument.\n\
1a966eab 16293This command may be abbreviated \"delete\"."),
c906108c
SS
16294 &deletelist);
16295
1bedd215 16296 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16297Clear breakpoint at specified location.\n\
16298Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16299\n\
16300With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16301is executing in.\n"
16302"\n" LOCATION_HELP_STRING "\n\
1bedd215 16303See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16304 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16305
1bedd215 16306 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16307Set breakpoint at specified location.\n"
31e2b00f 16308BREAK_ARGS_HELP ("break")));
5ba2abeb 16309 set_cmd_completer (c, location_completer);
c94fdfd0 16310
c906108c
SS
16311 add_com_alias ("b", "break", class_run, 1);
16312 add_com_alias ("br", "break", class_run, 1);
16313 add_com_alias ("bre", "break", class_run, 1);
16314 add_com_alias ("brea", "break", class_run, 1);
16315
c906108c
SS
16316 if (dbx_commands)
16317 {
1bedd215
AC
16318 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16319Break in function/address or break at a line in the current file."),
c5aa993b
JM
16320 &stoplist, "stop ", 1, &cmdlist);
16321 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16322 _("Break in function or address."), &stoplist);
c5aa993b 16323 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16324 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16325 add_com ("status", class_info, breakpoints_info, _("\
16326Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16327The \"Type\" column indicates one of:\n\
16328\tbreakpoint - normal breakpoint\n\
16329\twatchpoint - watchpoint\n\
16330The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16331the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16332breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16333address and file/line number respectively.\n\
16334\n\
16335Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16336are set to the address of the last breakpoint listed unless the command\n\
16337is prefixed with \"server \".\n\n\
c906108c 16338Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16339breakpoint set."));
c906108c
SS
16340 }
16341
1bedd215 16342 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16343Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16344The \"Type\" column indicates one of:\n\
16345\tbreakpoint - normal breakpoint\n\
16346\twatchpoint - watchpoint\n\
16347The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16348the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16349breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16350address and file/line number respectively.\n\
16351\n\
16352Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16353are set to the address of the last breakpoint listed unless the command\n\
16354is prefixed with \"server \".\n\n\
c906108c 16355Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16356breakpoint set."));
c906108c 16357
6b04bdb7
MS
16358 add_info_alias ("b", "breakpoints", 1);
16359
1a966eab
AC
16360 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16361Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16362The \"Type\" column indicates one of:\n\
16363\tbreakpoint - normal breakpoint\n\
16364\twatchpoint - watchpoint\n\
16365\tlongjmp - internal breakpoint used to step through longjmp()\n\
16366\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16367\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16368\tfinish - internal breakpoint used by the \"finish\" command\n\
16369The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16370the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16371breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16372address and file/line number respectively.\n\
16373\n\
16374Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16375are set to the address of the last breakpoint listed unless the command\n\
16376is prefixed with \"server \".\n\n\
c906108c 16377Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16378breakpoint set."),
c906108c
SS
16379 &maintenanceinfolist);
16380
44feb3ce
TT
16381 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16382Set catchpoints to catch events."),
16383 &catch_cmdlist, "catch ",
16384 0/*allow-unknown*/, &cmdlist);
16385
16386 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16387Set temporary catchpoints to catch events."),
16388 &tcatch_cmdlist, "tcatch ",
16389 0/*allow-unknown*/, &cmdlist);
16390
44feb3ce
TT
16391 add_catch_command ("fork", _("Catch calls to fork."),
16392 catch_fork_command_1,
a96d9b2e 16393 NULL,
44feb3ce
TT
16394 (void *) (uintptr_t) catch_fork_permanent,
16395 (void *) (uintptr_t) catch_fork_temporary);
16396 add_catch_command ("vfork", _("Catch calls to vfork."),
16397 catch_fork_command_1,
a96d9b2e 16398 NULL,
44feb3ce
TT
16399 (void *) (uintptr_t) catch_vfork_permanent,
16400 (void *) (uintptr_t) catch_vfork_temporary);
16401 add_catch_command ("exec", _("Catch calls to exec."),
16402 catch_exec_command_1,
a96d9b2e
SDJ
16403 NULL,
16404 CATCH_PERMANENT,
16405 CATCH_TEMPORARY);
edcc5120
TT
16406 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16407Usage: catch load [REGEX]\n\
16408If REGEX is given, only stop for libraries matching the regular expression."),
16409 catch_load_command_1,
16410 NULL,
16411 CATCH_PERMANENT,
16412 CATCH_TEMPORARY);
16413 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16414Usage: catch unload [REGEX]\n\
16415If REGEX is given, only stop for libraries matching the regular expression."),
16416 catch_unload_command_1,
16417 NULL,
16418 CATCH_PERMANENT,
16419 CATCH_TEMPORARY);
c5aa993b 16420
1bedd215
AC
16421 c = add_com ("watch", class_breakpoint, watch_command, _("\
16422Set a watchpoint for an expression.\n\
06a64a0b 16423Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16424A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16425an expression changes.\n\
16426If -l or -location is given, this evaluates EXPRESSION and watches\n\
16427the memory to which it refers."));
65d12d83 16428 set_cmd_completer (c, expression_completer);
c906108c 16429
1bedd215
AC
16430 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16431Set a read watchpoint for an expression.\n\
06a64a0b 16432Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16433A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16434an expression is read.\n\
16435If -l or -location is given, this evaluates EXPRESSION and watches\n\
16436the memory to which it refers."));
65d12d83 16437 set_cmd_completer (c, expression_completer);
c906108c 16438
1bedd215
AC
16439 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16440Set a watchpoint for an expression.\n\
06a64a0b 16441Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16442A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16443an expression is either read or written.\n\
16444If -l or -location is given, this evaluates EXPRESSION and watches\n\
16445the memory to which it refers."));
65d12d83 16446 set_cmd_completer (c, expression_completer);
c906108c 16447
d77f58be 16448 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16449Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16450
920d2a44
AC
16451 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16452 respond to changes - contrary to the description. */
85c07804
AC
16453 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16454 &can_use_hw_watchpoints, _("\
16455Set debugger's willingness to use watchpoint hardware."), _("\
16456Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16457If zero, gdb will not use hardware for new watchpoints, even if\n\
16458such is available. (However, any hardware watchpoints that were\n\
16459created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16460hardware.)"),
16461 NULL,
920d2a44 16462 show_can_use_hw_watchpoints,
85c07804 16463 &setlist, &showlist);
c906108c
SS
16464
16465 can_use_hw_watchpoints = 1;
fa8d40ab 16466
1042e4c0
SS
16467 /* Tracepoint manipulation commands. */
16468
16469 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16470Set a tracepoint at specified location.\n\
1042e4c0
SS
16471\n"
16472BREAK_ARGS_HELP ("trace") "\n\
16473Do \"help tracepoints\" for info on other tracepoint commands."));
16474 set_cmd_completer (c, location_completer);
16475
16476 add_com_alias ("tp", "trace", class_alias, 0);
16477 add_com_alias ("tr", "trace", class_alias, 1);
16478 add_com_alias ("tra", "trace", class_alias, 1);
16479 add_com_alias ("trac", "trace", class_alias, 1);
16480
7a697b8d 16481 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16482Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16483\n"
16484BREAK_ARGS_HELP ("ftrace") "\n\
16485Do \"help tracepoints\" for info on other tracepoint commands."));
16486 set_cmd_completer (c, location_completer);
16487
0fb4aa4b 16488 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16489Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16490\n\
16491strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16492LOCATION may be a linespec, explicit, or address location (described below) \n\
16493or -m MARKER_ID.\n\n\
16494If a marker id is specified, probe the marker with that name. With\n\
16495no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16496Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16497This collects arbitrary user data passed in the probe point call to the\n\
16498tracing library. You can inspect it when analyzing the trace buffer,\n\
16499by printing the $_sdata variable like any other convenience variable.\n\
16500\n\
16501CONDITION is a boolean expression.\n\
629500fa 16502\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16503Multiple tracepoints at one place are permitted, and useful if their\n\
16504conditions are different.\n\
0fb4aa4b
PA
16505\n\
16506Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16507Do \"help tracepoints\" for info on other tracepoint commands."));
16508 set_cmd_completer (c, location_completer);
16509
1042e4c0 16510 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16511Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16512Convenience variable \"$tpnum\" contains the number of the\n\
16513last tracepoint set."));
16514
16515 add_info_alias ("tp", "tracepoints", 1);
16516
16517 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16518Delete specified tracepoints.\n\
16519Arguments are tracepoint numbers, separated by spaces.\n\
16520No argument means delete all tracepoints."),
16521 &deletelist);
7e20dfcd 16522 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16523
16524 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16525Disable specified tracepoints.\n\
16526Arguments are tracepoint numbers, separated by spaces.\n\
16527No argument means disable all tracepoints."),
16528 &disablelist);
16529 deprecate_cmd (c, "disable");
16530
16531 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16532Enable specified tracepoints.\n\
16533Arguments are tracepoint numbers, separated by spaces.\n\
16534No argument means enable all tracepoints."),
16535 &enablelist);
16536 deprecate_cmd (c, "enable");
16537
16538 add_com ("passcount", class_trace, trace_pass_command, _("\
16539Set the passcount for a tracepoint.\n\
16540The trace will end when the tracepoint has been passed 'count' times.\n\
16541Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16542if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16543
6149aea9
PA
16544 add_prefix_cmd ("save", class_breakpoint, save_command,
16545 _("Save breakpoint definitions as a script."),
16546 &save_cmdlist, "save ",
16547 0/*allow-unknown*/, &cmdlist);
16548
16549 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16550Save current breakpoint definitions as a script.\n\
cce7e648 16551This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16552catchpoints, tracepoints). Use the 'source' command in another debug\n\
16553session to restore them."),
16554 &save_cmdlist);
16555 set_cmd_completer (c, filename_completer);
16556
16557 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16558Save current tracepoint definitions as a script.\n\
6149aea9
PA
16559Use the 'source' command in another debug session to restore them."),
16560 &save_cmdlist);
1042e4c0
SS
16561 set_cmd_completer (c, filename_completer);
16562
6149aea9
PA
16563 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16564 deprecate_cmd (c, "save tracepoints");
16565
1bedd215 16566 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16567Breakpoint specific settings\n\
16568Configure various breakpoint-specific variables such as\n\
1bedd215 16569pending breakpoint behavior"),
fa8d40ab
JJ
16570 &breakpoint_set_cmdlist, "set breakpoint ",
16571 0/*allow-unknown*/, &setlist);
1bedd215 16572 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16573Breakpoint specific settings\n\
16574Configure various breakpoint-specific variables such as\n\
1bedd215 16575pending breakpoint behavior"),
fa8d40ab
JJ
16576 &breakpoint_show_cmdlist, "show breakpoint ",
16577 0/*allow-unknown*/, &showlist);
16578
7915a72c
AC
16579 add_setshow_auto_boolean_cmd ("pending", no_class,
16580 &pending_break_support, _("\
16581Set debugger's behavior regarding pending breakpoints."), _("\
16582Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16583If on, an unrecognized breakpoint location will cause gdb to create a\n\
16584pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16585an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16586user-query to see if a pending breakpoint should be created."),
2c5b56ce 16587 NULL,
920d2a44 16588 show_pending_break_support,
6e1d7d6c
AC
16589 &breakpoint_set_cmdlist,
16590 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16591
16592 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16593
16594 add_setshow_boolean_cmd ("auto-hw", no_class,
16595 &automatic_hardware_breakpoints, _("\
16596Set automatic usage of hardware breakpoints."), _("\
16597Show automatic usage of hardware breakpoints."), _("\
16598If set, the debugger will automatically use hardware breakpoints for\n\
16599breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16600a warning will be emitted for such breakpoints."),
16601 NULL,
16602 show_automatic_hardware_breakpoints,
16603 &breakpoint_set_cmdlist,
16604 &breakpoint_show_cmdlist);
74960c60 16605
a25a5a45
PA
16606 add_setshow_boolean_cmd ("always-inserted", class_support,
16607 &always_inserted_mode, _("\
74960c60
VP
16608Set mode for inserting breakpoints."), _("\
16609Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16610When this mode is on, breakpoints are inserted immediately as soon as\n\
16611they're created, kept inserted even when execution stops, and removed\n\
16612only when the user deletes them. When this mode is off (the default),\n\
16613breakpoints are inserted only when execution continues, and removed\n\
16614when execution stops."),
72d0e2c5
YQ
16615 NULL,
16616 &show_always_inserted_mode,
16617 &breakpoint_set_cmdlist,
16618 &breakpoint_show_cmdlist);
f1310107 16619
b775012e
LM
16620 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16621 condition_evaluation_enums,
16622 &condition_evaluation_mode_1, _("\
16623Set mode of breakpoint condition evaluation."), _("\
16624Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16625When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16626evaluated on the host's side by GDB. When it is set to \"target\",\n\
16627breakpoint conditions will be downloaded to the target (if the target\n\
16628supports such feature) and conditions will be evaluated on the target's side.\n\
16629If this is set to \"auto\" (default), this will be automatically set to\n\
16630\"target\" if it supports condition evaluation, otherwise it will\n\
16631be set to \"gdb\""),
16632 &set_condition_evaluation_mode,
16633 &show_condition_evaluation_mode,
16634 &breakpoint_set_cmdlist,
16635 &breakpoint_show_cmdlist);
16636
f1310107
TJB
16637 add_com ("break-range", class_breakpoint, break_range_command, _("\
16638Set a breakpoint for an address range.\n\
16639break-range START-LOCATION, END-LOCATION\n\
16640where START-LOCATION and END-LOCATION can be one of the following:\n\
16641 LINENUM, for that line in the current file,\n\
16642 FILE:LINENUM, for that line in that file,\n\
16643 +OFFSET, for that number of lines after the current line\n\
16644 or the start of the range\n\
16645 FUNCTION, for the first line in that function,\n\
16646 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16647 *ADDRESS, for the instruction at that address.\n\
16648\n\
16649The breakpoint will stop execution of the inferior whenever it executes\n\
16650an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16651range (including START-LOCATION and END-LOCATION)."));
16652
e7e0cddf 16653 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16654Set a dynamic printf at specified location.\n\
e7e0cddf 16655dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16656location may be a linespec, explicit, or address location.\n"
16657"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16658 set_cmd_completer (c, location_completer);
16659
16660 add_setshow_enum_cmd ("dprintf-style", class_support,
16661 dprintf_style_enums, &dprintf_style, _("\
16662Set the style of usage for dynamic printf."), _("\
16663Show the style of usage for dynamic printf."), _("\
16664This setting chooses how GDB will do a dynamic printf.\n\
16665If the value is \"gdb\", then the printing is done by GDB to its own\n\
16666console, as with the \"printf\" command.\n\
16667If the value is \"call\", the print is done by calling a function in your\n\
16668program; by default printf(), but you can choose a different function or\n\
16669output stream by setting dprintf-function and dprintf-channel."),
16670 update_dprintf_commands, NULL,
16671 &setlist, &showlist);
16672
16673 dprintf_function = xstrdup ("printf");
16674 add_setshow_string_cmd ("dprintf-function", class_support,
16675 &dprintf_function, _("\
16676Set the function to use for dynamic printf"), _("\
16677Show the function to use for dynamic printf"), NULL,
16678 update_dprintf_commands, NULL,
16679 &setlist, &showlist);
16680
16681 dprintf_channel = xstrdup ("");
16682 add_setshow_string_cmd ("dprintf-channel", class_support,
16683 &dprintf_channel, _("\
16684Set the channel to use for dynamic printf"), _("\
16685Show the channel to use for dynamic printf"), NULL,
16686 update_dprintf_commands, NULL,
16687 &setlist, &showlist);
16688
d3ce09f5
SS
16689 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16690 &disconnected_dprintf, _("\
16691Set whether dprintf continues after GDB disconnects."), _("\
16692Show whether dprintf continues after GDB disconnects."), _("\
16693Use this to let dprintf commands continue to hit and produce output\n\
16694even if GDB disconnects or detaches from the target."),
16695 NULL,
16696 NULL,
16697 &setlist, &showlist);
16698
16699 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16700agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16701(target agent only) This is useful for formatted output in user-defined commands."));
16702
765dc015 16703 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16704
16705 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16706 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16707}
This page took 3.258094 seconds and 4 git commands to generate.