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
c906108c 3767void
fba45db2 3768update_breakpoints_after_exec (void)
c906108c 3769{
35df4500 3770 struct breakpoint *b, *b_tmp;
876fa593 3771 struct bp_location *bploc, **bplocp_tmp;
c906108c 3772
25b22b0a
PA
3773 /* We're about to delete breakpoints from GDB's lists. If the
3774 INSERTED flag is true, GDB will try to lift the breakpoints by
3775 writing the breakpoints' "shadow contents" back into memory. The
3776 "shadow contents" are NOT valid after an exec, so GDB should not
3777 do that. Instead, the target is responsible from marking
3778 breakpoints out as soon as it detects an exec. We don't do that
3779 here instead, because there may be other attempts to delete
3780 breakpoints after detecting an exec and before reaching here. */
876fa593 3781 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3782 if (bploc->pspace == current_program_space)
3783 gdb_assert (!bploc->inserted);
c906108c 3784
35df4500 3785 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3786 {
6c95b8df
PA
3787 if (b->pspace != current_program_space)
3788 continue;
3789
4a64f543 3790 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3791 if (b->type == bp_shlib_event)
3792 {
3793 delete_breakpoint (b);
3794 continue;
3795 }
c906108c 3796
4a64f543 3797 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3798 if (b->type == bp_jit_event)
3799 {
3800 delete_breakpoint (b);
3801 continue;
3802 }
3803
1900040c 3804 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3805 as must overlay event and longjmp master breakpoints. */
3806 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3807 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3808 || b->type == bp_exception_master)
c4093a6a
JM
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
3813
4a64f543 3814 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3815 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820
7c16b83e
PA
3821 /* Just like single-step breakpoints. */
3822 if (b->type == bp_single_step)
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
3827
611c83ae
PA
3828 /* Longjmp and longjmp-resume breakpoints are also meaningless
3829 after an exec. */
186c406b 3830 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3831 || b->type == bp_longjmp_call_dummy
186c406b 3832 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
ce78b96d
JB
3838 if (b->type == bp_catchpoint)
3839 {
3840 /* For now, none of the bp_catchpoint breakpoints need to
3841 do anything at this point. In the future, if some of
3842 the catchpoints need to something, we will need to add
3843 a new method, and call this method from here. */
3844 continue;
3845 }
3846
c5aa993b
JM
3847 /* bp_finish is a special case. The only way we ought to be able
3848 to see one of these when an exec() has happened, is if the user
3849 caught a vfork, and then said "finish". Ordinarily a finish just
3850 carries them to the call-site of the current callee, by setting
3851 a temporary bp there and resuming. But in this case, the finish
3852 will carry them entirely through the vfork & exec.
3853
3854 We don't want to allow a bp_finish to remain inserted now. But
3855 we can't safely delete it, 'cause finish_command has a handle to
3856 the bp on a bpstat, and will later want to delete it. There's a
3857 chance (and I've seen it happen) that if we delete the bp_finish
3858 here, that its storage will get reused by the time finish_command
3859 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3860 We really must allow finish_command to delete a bp_finish.
3861
e5dd4106 3862 In the absence of a general solution for the "how do we know
53a5351d
JM
3863 it's safe to delete something others may have handles to?"
3864 problem, what we'll do here is just uninsert the bp_finish, and
3865 let finish_command delete it.
3866
3867 (We know the bp_finish is "doomed" in the sense that it's
3868 momentary, and will be deleted as soon as finish_command sees
3869 the inferior stopped. So it doesn't matter that the bp's
3870 address is probably bogus in the new a.out, unlike e.g., the
3871 solib breakpoints.) */
c5aa993b 3872
c5aa993b
JM
3873 if (b->type == bp_finish)
3874 {
3875 continue;
3876 }
3877
3878 /* Without a symbolic address, we have little hope of the
3879 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3880 a.out. */
f00aae0f 3881 if (event_location_empty_p (b->location))
c5aa993b
JM
3882 {
3883 delete_breakpoint (b);
3884 continue;
3885 }
c5aa993b 3886 }
c906108c
SS
3887}
3888
3889int
d80ee84f 3890detach_breakpoints (ptid_t ptid)
c906108c 3891{
35df4500 3892 struct bp_location *bl, **blp_tmp;
3a1bae8e 3893 int val = 0;
ce696e05 3894 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3895 struct inferior *inf = current_inferior ();
c5aa993b 3896
dfd4cc63 3897 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3898 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3899
6c95b8df 3900 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3901 inferior_ptid = ptid;
35df4500 3902 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3903 {
35df4500 3904 if (bl->pspace != inf->pspace)
6c95b8df
PA
3905 continue;
3906
bd9673a4
PW
3907 /* This function must physically remove breakpoints locations
3908 from the specified ptid, without modifying the breakpoint
3909 package's state. Locations of type bp_loc_other are only
3910 maintained at GDB side. So, there is no need to remove
3911 these bp_loc_other locations. Moreover, removing these
3912 would modify the breakpoint package's state. */
3913 if (bl->loc_type == bp_loc_other)
3914 continue;
3915
35df4500
TJB
3916 if (bl->inserted)
3917 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3918 }
d03285ec 3919
ce696e05 3920 do_cleanups (old_chain);
3a1bae8e 3921 return val;
c906108c
SS
3922}
3923
35df4500 3924/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3925 Note that this is used to detach breakpoints from a child fork.
3926 When we get here, the child isn't in the inferior list, and neither
3927 do we have objects to represent its address space --- we should
35df4500 3928 *not* look at bl->pspace->aspace here. */
6c95b8df 3929
c906108c 3930static int
35df4500 3931remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3932{
3933 int val;
c5aa993b 3934
35df4500
TJB
3935 /* BL is never in moribund_locations by our callers. */
3936 gdb_assert (bl->owner != NULL);
2bdf28a0 3937
74960c60
VP
3938 /* The type of none suggests that owner is actually deleted.
3939 This should not ever happen. */
35df4500 3940 gdb_assert (bl->owner->type != bp_none);
0bde7532 3941
35df4500
TJB
3942 if (bl->loc_type == bp_loc_software_breakpoint
3943 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3944 {
c02f5703
MS
3945 /* "Normal" instruction breakpoint: either the standard
3946 trap-instruction bp (bp_breakpoint), or a
3947 bp_hardware_breakpoint. */
3948
3949 /* First check to see if we have to handle an overlay. */
3950 if (overlay_debugging == ovly_off
35df4500
TJB
3951 || bl->section == NULL
3952 || !(section_is_overlay (bl->section)))
c02f5703
MS
3953 {
3954 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3955
3956 /* If we're trying to uninsert a memory breakpoint that we
3957 know is set in a dynamic object that is marked
3958 shlib_disabled, then either the dynamic object was
3959 removed with "remove-symbol-file" or with
3960 "nosharedlibrary". In the former case, we don't know
3961 whether another dynamic object might have loaded over the
3962 breakpoint's address -- the user might well let us know
3963 about it next with add-symbol-file (the whole point of
d03de421 3964 add-symbol-file is letting the user manually maintain a
08351840
PA
3965 list of dynamically loaded objects). If we have the
3966 breakpoint's shadow memory, that is, this is a software
3967 breakpoint managed by GDB, check whether the breakpoint
3968 is still inserted in memory, to avoid overwriting wrong
3969 code with stale saved shadow contents. Note that HW
3970 breakpoints don't have shadow memory, as they're
3971 implemented using a mechanism that is not dependent on
3972 being able to modify the target's memory, and as such
3973 they should always be removed. */
3974 if (bl->shlib_disabled
3975 && bl->target_info.shadow_len != 0
3976 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3977 val = 0;
3978 else
3979 val = bl->owner->ops->remove_location (bl);
c02f5703 3980 }
c906108c
SS
3981 else
3982 {
4a64f543 3983 /* This breakpoint is in an overlay section.
c02f5703
MS
3984 Did we set a breakpoint at the LMA? */
3985 if (!overlay_events_enabled)
3986 {
3987 /* Yes -- overlay event support is not active, so we
3988 should have set a breakpoint at the LMA. Remove it.
3989 */
c02f5703
MS
3990 /* Ignore any failures: if the LMA is in ROM, we will
3991 have already warned when we failed to insert it. */
35df4500
TJB
3992 if (bl->loc_type == bp_loc_hardware_breakpoint)
3993 target_remove_hw_breakpoint (bl->gdbarch,
3994 &bl->overlay_target_info);
c02f5703 3995 else
35df4500
TJB
3996 target_remove_breakpoint (bl->gdbarch,
3997 &bl->overlay_target_info);
c02f5703
MS
3998 }
3999 /* Did we set a breakpoint at the VMA?
4000 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4001 if (bl->inserted)
c906108c 4002 {
c02f5703
MS
4003 /* Yes -- remove it. Previously we did not bother to
4004 remove the breakpoint if the section had been
4005 unmapped, but let's not rely on that being safe. We
4006 don't know what the overlay manager might do. */
aa67235e
UW
4007
4008 /* However, we should remove *software* breakpoints only
4009 if the section is still mapped, or else we overwrite
4010 wrong code with the saved shadow contents. */
348d480f
PA
4011 if (bl->loc_type == bp_loc_hardware_breakpoint
4012 || section_is_mapped (bl->section))
4013 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
4014 else
4015 val = 0;
c906108c 4016 }
c02f5703
MS
4017 else
4018 {
4019 /* No -- not inserted, so no need to remove. No error. */
4020 val = 0;
4021 }
c906108c 4022 }
879d1e6b 4023
08351840
PA
4024 /* In some cases, we might not be able to remove a breakpoint in
4025 a shared library that has already been removed, but we have
4026 not yet processed the shlib unload event. Similarly for an
4027 unloaded add-symbol-file object - the user might not yet have
4028 had the chance to remove-symbol-file it. shlib_disabled will
4029 be set if the library/object has already been removed, but
4030 the breakpoint hasn't been uninserted yet, e.g., after
4031 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4032 always-inserted mode. */
076855f9 4033 if (val
08351840
PA
4034 && (bl->loc_type == bp_loc_software_breakpoint
4035 && (bl->shlib_disabled
4036 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4037 || shared_objfile_contains_address_p (bl->pspace,
4038 bl->address))))
879d1e6b
UW
4039 val = 0;
4040
c906108c
SS
4041 if (val)
4042 return val;
35df4500 4043 bl->inserted = (is == mark_inserted);
c906108c 4044 }
35df4500 4045 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4046 {
77b06cd7
TJB
4047 gdb_assert (bl->owner->ops != NULL
4048 && bl->owner->ops->remove_location != NULL);
4049
35df4500 4050 bl->inserted = (is == mark_inserted);
77b06cd7 4051 bl->owner->ops->remove_location (bl);
2e70b7b9 4052
c906108c 4053 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4054 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4055 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4056 bl->owner->number);
c906108c 4057 }
35df4500
TJB
4058 else if (bl->owner->type == bp_catchpoint
4059 && breakpoint_enabled (bl->owner)
4060 && !bl->duplicate)
ce78b96d 4061 {
77b06cd7
TJB
4062 gdb_assert (bl->owner->ops != NULL
4063 && bl->owner->ops->remove_location != NULL);
ce78b96d 4064
77b06cd7 4065 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4066 if (val)
4067 return val;
77b06cd7 4068
35df4500 4069 bl->inserted = (is == mark_inserted);
ce78b96d 4070 }
c906108c
SS
4071
4072 return 0;
4073}
4074
6c95b8df 4075static int
35df4500 4076remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4077{
4078 int ret;
4079 struct cleanup *old_chain;
4080
35df4500
TJB
4081 /* BL is never in moribund_locations by our callers. */
4082 gdb_assert (bl->owner != NULL);
2bdf28a0 4083
6c95b8df
PA
4084 /* The type of none suggests that owner is actually deleted.
4085 This should not ever happen. */
35df4500 4086 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4087
4088 old_chain = save_current_space_and_thread ();
4089
35df4500 4090 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4091
35df4500 4092 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4093
4094 do_cleanups (old_chain);
4095 return ret;
4096}
4097
c906108c
SS
4098/* Clear the "inserted" flag in all breakpoints. */
4099
25b22b0a 4100void
fba45db2 4101mark_breakpoints_out (void)
c906108c 4102{
35df4500 4103 struct bp_location *bl, **blp_tmp;
c906108c 4104
35df4500 4105 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4106 if (bl->pspace == current_program_space)
35df4500 4107 bl->inserted = 0;
c906108c
SS
4108}
4109
53a5351d
JM
4110/* Clear the "inserted" flag in all breakpoints and delete any
4111 breakpoints which should go away between runs of the program.
c906108c
SS
4112
4113 Plus other such housekeeping that has to be done for breakpoints
4114 between runs.
4115
53a5351d
JM
4116 Note: this function gets called at the end of a run (by
4117 generic_mourn_inferior) and when a run begins (by
4a64f543 4118 init_wait_for_inferior). */
c906108c
SS
4119
4120
4121
4122void
fba45db2 4123breakpoint_init_inferior (enum inf_context context)
c906108c 4124{
35df4500
TJB
4125 struct breakpoint *b, *b_tmp;
4126 struct bp_location *bl, **blp_tmp;
1c5cfe86 4127 int ix;
6c95b8df 4128 struct program_space *pspace = current_program_space;
c906108c 4129
50c71eaf
PA
4130 /* If breakpoint locations are shared across processes, then there's
4131 nothing to do. */
f5656ead 4132 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4133 return;
4134
1a853c52 4135 mark_breakpoints_out ();
075f6582 4136
35df4500 4137 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4138 {
6c95b8df
PA
4139 if (b->loc && b->loc->pspace != pspace)
4140 continue;
4141
c5aa993b
JM
4142 switch (b->type)
4143 {
4144 case bp_call_dummy:
e2e4d78b 4145 case bp_longjmp_call_dummy:
c906108c 4146
c5aa993b 4147 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4148 cause problems when the inferior is rerun, so we better get
4149 rid of it. */
4150
4151 case bp_watchpoint_scope:
4152
4153 /* Also get rid of scope breakpoints. */
4154
4155 case bp_shlib_event:
4156
4157 /* Also remove solib event breakpoints. Their addresses may
4158 have changed since the last time we ran the program.
4159 Actually we may now be debugging against different target;
4160 and so the solib backend that installed this breakpoint may
4161 not be used in by the target. E.g.,
4162
4163 (gdb) file prog-linux
4164 (gdb) run # native linux target
4165 ...
4166 (gdb) kill
4167 (gdb) file prog-win.exe
4168 (gdb) tar rem :9999 # remote Windows gdbserver.
4169 */
c906108c 4170
f59f708a
PA
4171 case bp_step_resume:
4172
4173 /* Also remove step-resume breakpoints. */
4174
7c16b83e
PA
4175 case bp_single_step:
4176
4177 /* Also remove single-step breakpoints. */
4178
c5aa993b
JM
4179 delete_breakpoint (b);
4180 break;
c906108c 4181
c5aa993b
JM
4182 case bp_watchpoint:
4183 case bp_hardware_watchpoint:
4184 case bp_read_watchpoint:
4185 case bp_access_watchpoint:
3a5c3e22
PA
4186 {
4187 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4188
3a5c3e22
PA
4189 /* Likewise for watchpoints on local expressions. */
4190 if (w->exp_valid_block != NULL)
4191 delete_breakpoint (b);
4192 else if (context == inf_starting)
4193 {
4194 /* Reset val field to force reread of starting value in
4195 insert_breakpoints. */
4196 if (w->val)
4197 value_free (w->val);
4198 w->val = NULL;
4199 w->val_valid = 0;
c860120c 4200 }
3a5c3e22 4201 }
c5aa993b
JM
4202 break;
4203 default:
c5aa993b
JM
4204 break;
4205 }
4206 }
1c5cfe86
PA
4207
4208 /* Get rid of the moribund locations. */
35df4500
TJB
4209 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4210 decref_bp_location (&bl);
1c5cfe86 4211 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4212}
4213
6c95b8df
PA
4214/* These functions concern about actual breakpoints inserted in the
4215 target --- to e.g. check if we need to do decr_pc adjustment or if
4216 we need to hop over the bkpt --- so we check for address space
4217 match, not program space. */
4218
c2c6d25f
JM
4219/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4220 exists at PC. It returns ordinary_breakpoint_here if it's an
4221 ordinary breakpoint, or permanent_breakpoint_here if it's a
4222 permanent breakpoint.
4223 - When continuing from a location with an ordinary breakpoint, we
4224 actually single step once before calling insert_breakpoints.
e5dd4106 4225 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4226 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4227 the target, to advance the PC past the breakpoint. */
c906108c 4228
c2c6d25f 4229enum breakpoint_here
6c95b8df 4230breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4231{
35df4500 4232 struct bp_location *bl, **blp_tmp;
c2c6d25f 4233 int any_breakpoint_here = 0;
c906108c 4234
35df4500 4235 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4236 {
35df4500
TJB
4237 if (bl->loc_type != bp_loc_software_breakpoint
4238 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4239 continue;
4240
f1310107 4241 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4242 if ((breakpoint_enabled (bl->owner)
1a853c52 4243 || bl->permanent)
f1310107 4244 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4245 {
4246 if (overlay_debugging
35df4500
TJB
4247 && section_is_overlay (bl->section)
4248 && !section_is_mapped (bl->section))
075f6582 4249 continue; /* unmapped overlay -- can't be a match */
1a853c52 4250 else if (bl->permanent)
075f6582
DJ
4251 return permanent_breakpoint_here;
4252 else
4253 any_breakpoint_here = 1;
4254 }
4255 }
c906108c 4256
f486487f 4257 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4258}
4259
d35ae833
PA
4260/* See breakpoint.h. */
4261
4262int
4263breakpoint_in_range_p (struct address_space *aspace,
4264 CORE_ADDR addr, ULONGEST len)
4265{
4266 struct bp_location *bl, **blp_tmp;
4267
4268 ALL_BP_LOCATIONS (bl, blp_tmp)
4269 {
4270 if (bl->loc_type != bp_loc_software_breakpoint
4271 && bl->loc_type != bp_loc_hardware_breakpoint)
4272 continue;
4273
4274 if ((breakpoint_enabled (bl->owner)
4275 || bl->permanent)
4276 && breakpoint_location_address_range_overlap (bl, aspace,
4277 addr, len))
4278 {
4279 if (overlay_debugging
4280 && section_is_overlay (bl->section)
4281 && !section_is_mapped (bl->section))
4282 {
4283 /* Unmapped overlay -- can't be a match. */
4284 continue;
4285 }
4286
4287 return 1;
4288 }
4289 }
4290
4291 return 0;
4292}
4293
1c5cfe86
PA
4294/* Return true if there's a moribund breakpoint at PC. */
4295
4296int
6c95b8df 4297moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4298{
4299 struct bp_location *loc;
4300 int ix;
4301
4302 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4303 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4304 return 1;
4305
4306 return 0;
4307}
c2c6d25f 4308
f7ce857f
PA
4309/* Returns non-zero iff BL is inserted at PC, in address space
4310 ASPACE. */
4311
4312static int
4313bp_location_inserted_here_p (struct bp_location *bl,
4314 struct address_space *aspace, CORE_ADDR pc)
4315{
4316 if (bl->inserted
4317 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4318 aspace, pc))
4319 {
4320 if (overlay_debugging
4321 && section_is_overlay (bl->section)
4322 && !section_is_mapped (bl->section))
4323 return 0; /* unmapped overlay -- can't be a match */
4324 else
4325 return 1;
4326 }
4327 return 0;
4328}
4329
a1fd2fa5 4330/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4331
4332int
a1fd2fa5 4333breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4334{
f7ce857f
PA
4335 struct bp_location **blp, **blp_tmp = NULL;
4336 struct bp_location *bl;
c906108c 4337
f7ce857f 4338 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4339 {
f7ce857f
PA
4340 struct bp_location *bl = *blp;
4341
35df4500
TJB
4342 if (bl->loc_type != bp_loc_software_breakpoint
4343 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4344 continue;
4345
f7ce857f
PA
4346 if (bp_location_inserted_here_p (bl, aspace, pc))
4347 return 1;
c5aa993b 4348 }
c36b740a
VP
4349 return 0;
4350}
4351
a1fd2fa5
PA
4352/* This function returns non-zero iff there is a software breakpoint
4353 inserted at PC. */
c36b740a
VP
4354
4355int
a1fd2fa5
PA
4356software_breakpoint_inserted_here_p (struct address_space *aspace,
4357 CORE_ADDR pc)
4fa8626c 4358{
f7ce857f
PA
4359 struct bp_location **blp, **blp_tmp = NULL;
4360 struct bp_location *bl;
4fa8626c 4361
f7ce857f 4362 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4363 {
f7ce857f
PA
4364 struct bp_location *bl = *blp;
4365
35df4500 4366 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4367 continue;
4368
f7ce857f
PA
4369 if (bp_location_inserted_here_p (bl, aspace, pc))
4370 return 1;
4fa8626c
DJ
4371 }
4372
4373 return 0;
9c02b525
PA
4374}
4375
4376/* See breakpoint.h. */
4377
4378int
4379hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4380 CORE_ADDR pc)
4381{
4382 struct bp_location **blp, **blp_tmp = NULL;
4383 struct bp_location *bl;
4384
4385 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4386 {
4387 struct bp_location *bl = *blp;
4388
4389 if (bl->loc_type != bp_loc_hardware_breakpoint)
4390 continue;
4391
4392 if (bp_location_inserted_here_p (bl, aspace, pc))
4393 return 1;
4394 }
4395
4396 return 0;
4fa8626c
DJ
4397}
4398
9093389c
PA
4399int
4400hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4401 CORE_ADDR addr, ULONGEST len)
4402{
4403 struct breakpoint *bpt;
4404
4405 ALL_BREAKPOINTS (bpt)
4406 {
4407 struct bp_location *loc;
4408
4409 if (bpt->type != bp_hardware_watchpoint
4410 && bpt->type != bp_access_watchpoint)
4411 continue;
4412
4413 if (!breakpoint_enabled (bpt))
4414 continue;
4415
4416 for (loc = bpt->loc; loc; loc = loc->next)
4417 if (loc->pspace->aspace == aspace && loc->inserted)
4418 {
4419 CORE_ADDR l, h;
4420
4421 /* Check for intersection. */
4422 l = max (loc->address, addr);
4423 h = min (loc->address + loc->length, addr + len);
4424 if (l < h)
4425 return 1;
4426 }
4427 }
4428 return 0;
4429}
c906108c 4430\f
c5aa993b 4431
c906108c
SS
4432/* bpstat stuff. External routines' interfaces are documented
4433 in breakpoint.h. */
4434
4435int
c326b90e 4436is_catchpoint (struct breakpoint *ep)
c906108c 4437{
533be4dd 4438 return (ep->type == bp_catchpoint);
c906108c
SS
4439}
4440
f431efe5
PA
4441/* Frees any storage that is part of a bpstat. Does not walk the
4442 'next' chain. */
4443
4444static void
198757a8
VP
4445bpstat_free (bpstat bs)
4446{
4447 if (bs->old_val != NULL)
4448 value_free (bs->old_val);
9add0f1b 4449 decref_counted_command_line (&bs->commands);
f431efe5 4450 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4451 xfree (bs);
4452}
4453
c906108c
SS
4454/* Clear a bpstat so that it says we are not at any breakpoint.
4455 Also free any storage that is part of a bpstat. */
4456
4457void
fba45db2 4458bpstat_clear (bpstat *bsp)
c906108c
SS
4459{
4460 bpstat p;
4461 bpstat q;
4462
4463 if (bsp == 0)
4464 return;
4465 p = *bsp;
4466 while (p != NULL)
4467 {
4468 q = p->next;
198757a8 4469 bpstat_free (p);
c906108c
SS
4470 p = q;
4471 }
4472 *bsp = NULL;
4473}
4474
4475/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4476 is part of the bpstat is copied as well. */
4477
4478bpstat
fba45db2 4479bpstat_copy (bpstat bs)
c906108c
SS
4480{
4481 bpstat p = NULL;
4482 bpstat tmp;
4483 bpstat retval = NULL;
4484
4485 if (bs == NULL)
4486 return bs;
4487
4488 for (; bs != NULL; bs = bs->next)
4489 {
4490 tmp = (bpstat) xmalloc (sizeof (*tmp));
4491 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4492 incref_counted_command_line (tmp->commands);
f431efe5 4493 incref_bp_location (tmp->bp_location_at);
31cc81e9 4494 if (bs->old_val != NULL)
3c3185ac
JK
4495 {
4496 tmp->old_val = value_copy (bs->old_val);
4497 release_value (tmp->old_val);
4498 }
31cc81e9 4499
c906108c
SS
4500 if (p == NULL)
4501 /* This is the first thing in the chain. */
4502 retval = tmp;
4503 else
4504 p->next = tmp;
4505 p = tmp;
4506 }
4507 p->next = NULL;
4508 return retval;
4509}
4510
4a64f543 4511/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4512
4513bpstat
fba45db2 4514bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4515{
c5aa993b
JM
4516 if (bsp == NULL)
4517 return NULL;
c906108c 4518
c5aa993b
JM
4519 for (; bsp != NULL; bsp = bsp->next)
4520 {
f431efe5 4521 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4522 return bsp;
4523 }
c906108c
SS
4524 return NULL;
4525}
4526
ab04a2af
TT
4527/* See breakpoint.h. */
4528
47591c29 4529int
427cd150 4530bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4531{
ab04a2af
TT
4532 for (; bsp != NULL; bsp = bsp->next)
4533 {
427cd150
TT
4534 if (bsp->breakpoint_at == NULL)
4535 {
4536 /* A moribund location can never explain a signal other than
4537 GDB_SIGNAL_TRAP. */
4538 if (sig == GDB_SIGNAL_TRAP)
47591c29 4539 return 1;
427cd150
TT
4540 }
4541 else
47591c29
PA
4542 {
4543 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4544 sig))
4545 return 1;
4546 }
ab04a2af
TT
4547 }
4548
47591c29 4549 return 0;
ab04a2af
TT
4550}
4551
4a64f543
MS
4552/* Put in *NUM the breakpoint number of the first breakpoint we are
4553 stopped at. *BSP upon return is a bpstat which points to the
4554 remaining breakpoints stopped at (but which is not guaranteed to be
4555 good for anything but further calls to bpstat_num).
4556
8671a17b
PA
4557 Return 0 if passed a bpstat which does not indicate any breakpoints.
4558 Return -1 if stopped at a breakpoint that has been deleted since
4559 we set it.
4560 Return 1 otherwise. */
c906108c
SS
4561
4562int
8671a17b 4563bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4564{
4565 struct breakpoint *b;
4566
4567 if ((*bsp) == NULL)
4568 return 0; /* No more breakpoint values */
8671a17b 4569
4a64f543
MS
4570 /* We assume we'll never have several bpstats that correspond to a
4571 single breakpoint -- otherwise, this function might return the
4572 same number more than once and this will look ugly. */
f431efe5 4573 b = (*bsp)->breakpoint_at;
8671a17b
PA
4574 *bsp = (*bsp)->next;
4575 if (b == NULL)
4576 return -1; /* breakpoint that's been deleted since */
4577
4578 *num = b->number; /* We have its number */
4579 return 1;
c906108c
SS
4580}
4581
e93ca019 4582/* See breakpoint.h. */
c906108c
SS
4583
4584void
e93ca019 4585bpstat_clear_actions (void)
c906108c 4586{
e93ca019
JK
4587 struct thread_info *tp;
4588 bpstat bs;
4589
4590 if (ptid_equal (inferior_ptid, null_ptid))
4591 return;
4592
4593 tp = find_thread_ptid (inferior_ptid);
4594 if (tp == NULL)
4595 return;
4596
4597 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4598 {
9add0f1b 4599 decref_counted_command_line (&bs->commands);
abf85f46 4600
c906108c
SS
4601 if (bs->old_val != NULL)
4602 {
4603 value_free (bs->old_val);
4604 bs->old_val = NULL;
4605 }
4606 }
4607}
4608
f3b1572e
PA
4609/* Called when a command is about to proceed the inferior. */
4610
4611static void
4612breakpoint_about_to_proceed (void)
4613{
4614 if (!ptid_equal (inferior_ptid, null_ptid))
4615 {
4616 struct thread_info *tp = inferior_thread ();
4617
4618 /* Allow inferior function calls in breakpoint commands to not
4619 interrupt the command list. When the call finishes
4620 successfully, the inferior will be standing at the same
4621 breakpoint as if nothing happened. */
16c381f0 4622 if (tp->control.in_infcall)
f3b1572e
PA
4623 return;
4624 }
4625
4626 breakpoint_proceeded = 1;
4627}
4628
4a64f543
MS
4629/* Stub for cleaning up our state if we error-out of a breakpoint
4630 command. */
c906108c 4631static void
4efb68b1 4632cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4633{
4634 executing_breakpoint_commands = 0;
4635}
4636
abf85f46
JK
4637/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4638 or its equivalent. */
4639
4640static int
4641command_line_is_silent (struct command_line *cmd)
4642{
4f45d445 4643 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4644}
4645
4a64f543
MS
4646/* Execute all the commands associated with all the breakpoints at
4647 this location. Any of these commands could cause the process to
4648 proceed beyond this point, etc. We look out for such changes by
4649 checking the global "breakpoint_proceeded" after each command.
c906108c 4650
347bddb7
PA
4651 Returns true if a breakpoint command resumed the inferior. In that
4652 case, it is the caller's responsibility to recall it again with the
4653 bpstat of the current thread. */
4654
4655static int
4656bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4657{
4658 bpstat bs;
4659 struct cleanup *old_chain;
347bddb7 4660 int again = 0;
c906108c
SS
4661
4662 /* Avoid endless recursion if a `source' command is contained
4663 in bs->commands. */
4664 if (executing_breakpoint_commands)
347bddb7 4665 return 0;
c906108c
SS
4666
4667 executing_breakpoint_commands = 1;
4668 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4669
cf6c5ffb
TT
4670 prevent_dont_repeat ();
4671
4a64f543 4672 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4673 bs = *bsp;
4674
4675 breakpoint_proceeded = 0;
4676 for (; bs != NULL; bs = bs->next)
4677 {
9add0f1b 4678 struct counted_command_line *ccmd;
6c50ab1c
JB
4679 struct command_line *cmd;
4680 struct cleanup *this_cmd_tree_chain;
4681
4682 /* Take ownership of the BSP's command tree, if it has one.
4683
4684 The command tree could legitimately contain commands like
4685 'step' and 'next', which call clear_proceed_status, which
4686 frees stop_bpstat's command tree. To make sure this doesn't
4687 free the tree we're executing out from under us, we need to
4688 take ownership of the tree ourselves. Since a given bpstat's
4689 commands are only executed once, we don't need to copy it; we
4690 can clear the pointer in the bpstat, and make sure we free
4691 the tree when we're done. */
9add0f1b
TT
4692 ccmd = bs->commands;
4693 bs->commands = NULL;
abf85f46
JK
4694 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4695 cmd = ccmd ? ccmd->commands : NULL;
4696 if (command_line_is_silent (cmd))
4697 {
4698 /* The action has been already done by bpstat_stop_status. */
4699 cmd = cmd->next;
4700 }
6c50ab1c 4701
c906108c
SS
4702 while (cmd != NULL)
4703 {
4704 execute_control_command (cmd);
4705
4706 if (breakpoint_proceeded)
4707 break;
4708 else
4709 cmd = cmd->next;
4710 }
6c50ab1c
JB
4711
4712 /* We can free this command tree now. */
4713 do_cleanups (this_cmd_tree_chain);
4714
c906108c 4715 if (breakpoint_proceeded)
32c1e744 4716 {
0b333c5e 4717 if (interpreter_async)
347bddb7
PA
4718 /* If we are in async mode, then the target might be still
4719 running, not stopped at any breakpoint, so nothing for
4720 us to do here -- just return to the event loop. */
4721 ;
32c1e744
VP
4722 else
4723 /* In sync mode, when execute_control_command returns
4724 we're already standing on the next breakpoint.
347bddb7
PA
4725 Breakpoint commands for that stop were not run, since
4726 execute_command does not run breakpoint commands --
4727 only command_line_handler does, but that one is not
4728 involved in execution of breakpoint commands. So, we
4729 can now execute breakpoint commands. It should be
4730 noted that making execute_command do bpstat actions is
4731 not an option -- in this case we'll have recursive
4732 invocation of bpstat for each breakpoint with a
4733 command, and can easily blow up GDB stack. Instead, we
4734 return true, which will trigger the caller to recall us
4735 with the new stop_bpstat. */
4736 again = 1;
4737 break;
32c1e744 4738 }
c906108c 4739 }
c2b8ed2c 4740 do_cleanups (old_chain);
347bddb7
PA
4741 return again;
4742}
4743
4744void
4745bpstat_do_actions (void)
4746{
353d1d73
JK
4747 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4748
347bddb7
PA
4749 /* Do any commands attached to breakpoint we are stopped at. */
4750 while (!ptid_equal (inferior_ptid, null_ptid)
4751 && target_has_execution
4752 && !is_exited (inferior_ptid)
4753 && !is_executing (inferior_ptid))
4754 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4755 and only return when it is stopped at the next breakpoint, we
4756 keep doing breakpoint actions until it returns false to
4757 indicate the inferior was not resumed. */
16c381f0 4758 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4759 break;
353d1d73
JK
4760
4761 discard_cleanups (cleanup_if_error);
c906108c
SS
4762}
4763
fa4727a6
DJ
4764/* Print out the (old or new) value associated with a watchpoint. */
4765
4766static void
4767watchpoint_value_print (struct value *val, struct ui_file *stream)
4768{
4769 if (val == NULL)
4770 fprintf_unfiltered (stream, _("<unreadable>"));
4771 else
79a45b7d
TT
4772 {
4773 struct value_print_options opts;
4774 get_user_print_options (&opts);
4775 value_print (val, stream, &opts);
4776 }
fa4727a6
DJ
4777}
4778
f303dbd6
PA
4779/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4780 debugging multiple threads. */
4781
4782void
4783maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4784{
4785 if (ui_out_is_mi_like_p (uiout))
4786 return;
4787
4788 ui_out_text (uiout, "\n");
4789
4790 if (show_thread_that_caused_stop ())
4791 {
4792 const char *name;
4793 struct thread_info *thr = inferior_thread ();
4794
4795 ui_out_text (uiout, "Thread ");
4796 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4797
4798 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4799 if (name != NULL)
4800 {
4801 ui_out_text (uiout, " \"");
4802 ui_out_field_fmt (uiout, "name", "%s", name);
4803 ui_out_text (uiout, "\"");
4804 }
4805
4806 ui_out_text (uiout, " hit ");
4807 }
4808}
4809
e514a9d6 4810/* Generic routine for printing messages indicating why we
4a64f543 4811 stopped. The behavior of this function depends on the value
e514a9d6
JM
4812 'print_it' in the bpstat structure. Under some circumstances we
4813 may decide not to print anything here and delegate the task to
4a64f543 4814 normal_stop(). */
e514a9d6
JM
4815
4816static enum print_stop_action
4817print_bp_stop_message (bpstat bs)
4818{
4819 switch (bs->print_it)
4820 {
4821 case print_it_noop:
4a64f543 4822 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4823 return PRINT_UNKNOWN;
4824 break;
4825
4826 case print_it_done:
4827 /* We still want to print the frame, but we already printed the
4a64f543 4828 relevant messages. */
e514a9d6
JM
4829 return PRINT_SRC_AND_LOC;
4830 break;
4831
4832 case print_it_normal:
4f8d1dc6 4833 {
f431efe5
PA
4834 struct breakpoint *b = bs->breakpoint_at;
4835
1a6a67de
TJB
4836 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4837 which has since been deleted. */
4838 if (b == NULL)
4839 return PRINT_UNKNOWN;
4840
348d480f
PA
4841 /* Normal case. Call the breakpoint's print_it method. */
4842 return b->ops->print_it (bs);
4f8d1dc6 4843 }
348d480f 4844 break;
3086aeae 4845
e514a9d6 4846 default:
8e65ff28 4847 internal_error (__FILE__, __LINE__,
e2e0b3e5 4848 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4849 break;
c906108c 4850 }
c906108c
SS
4851}
4852
edcc5120
TT
4853/* A helper function that prints a shared library stopped event. */
4854
4855static void
4856print_solib_event (int is_catchpoint)
4857{
4858 int any_deleted
4859 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4860 int any_added
4861 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4862
4863 if (!is_catchpoint)
4864 {
4865 if (any_added || any_deleted)
4866 ui_out_text (current_uiout,
4867 _("Stopped due to shared library event:\n"));
4868 else
4869 ui_out_text (current_uiout,
4870 _("Stopped due to shared library event (no "
4871 "libraries added or removed)\n"));
4872 }
4873
4874 if (ui_out_is_mi_like_p (current_uiout))
4875 ui_out_field_string (current_uiout, "reason",
4876 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4877
4878 if (any_deleted)
4879 {
4880 struct cleanup *cleanup;
4881 char *name;
4882 int ix;
4883
4884 ui_out_text (current_uiout, _(" Inferior unloaded "));
4885 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4886 "removed");
4887 for (ix = 0;
4888 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4889 ix, name);
4890 ++ix)
4891 {
4892 if (ix > 0)
4893 ui_out_text (current_uiout, " ");
4894 ui_out_field_string (current_uiout, "library", name);
4895 ui_out_text (current_uiout, "\n");
4896 }
4897
4898 do_cleanups (cleanup);
4899 }
4900
4901 if (any_added)
4902 {
4903 struct so_list *iter;
4904 int ix;
4905 struct cleanup *cleanup;
4906
4907 ui_out_text (current_uiout, _(" Inferior loaded "));
4908 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4909 "added");
4910 for (ix = 0;
4911 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4912 ix, iter);
4913 ++ix)
4914 {
4915 if (ix > 0)
4916 ui_out_text (current_uiout, " ");
4917 ui_out_field_string (current_uiout, "library", iter->so_name);
4918 ui_out_text (current_uiout, "\n");
4919 }
4920
4921 do_cleanups (cleanup);
4922 }
4923}
4924
e514a9d6
JM
4925/* Print a message indicating what happened. This is called from
4926 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4927 list - a list of the eventpoints that caused this stop. KIND is
4928 the target_waitkind for the stopping event. This
e514a9d6
JM
4929 routine calls the generic print routine for printing a message
4930 about reasons for stopping. This will print (for example) the
4931 "Breakpoint n," part of the output. The return value of this
4932 routine is one of:
c906108c 4933
4a64f543 4934 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4935 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4936 code to print the location. An example is
c5aa993b
JM
4937 "Breakpoint 1, " which should be followed by
4938 the location.
917317f4 4939 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4940 to also print the location part of the message.
4941 An example is the catch/throw messages, which
4a64f543 4942 don't require a location appended to the end.
917317f4 4943 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4944 further info to be printed. */
c906108c 4945
917317f4 4946enum print_stop_action
36dfb11c 4947bpstat_print (bpstat bs, int kind)
c906108c 4948{
f486487f 4949 enum print_stop_action val;
c5aa993b 4950
c906108c 4951 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4952 (Currently all watchpoints go on the bpstat whether hit or not.
4953 That probably could (should) be changed, provided care is taken
c906108c 4954 with respect to bpstat_explains_signal). */
e514a9d6
JM
4955 for (; bs; bs = bs->next)
4956 {
4957 val = print_bp_stop_message (bs);
4958 if (val == PRINT_SRC_ONLY
4959 || val == PRINT_SRC_AND_LOC
4960 || val == PRINT_NOTHING)
4961 return val;
4962 }
c906108c 4963
36dfb11c
TT
4964 /* If we had hit a shared library event breakpoint,
4965 print_bp_stop_message would print out this message. If we hit an
4966 OS-level shared library event, do the same thing. */
4967 if (kind == TARGET_WAITKIND_LOADED)
4968 {
edcc5120 4969 print_solib_event (0);
36dfb11c
TT
4970 return PRINT_NOTHING;
4971 }
4972
e514a9d6 4973 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4974 with and nothing was printed. */
917317f4 4975 return PRINT_UNKNOWN;
c906108c
SS
4976}
4977
c42bd95a
DE
4978/* Evaluate the expression EXP and return 1 if value is zero.
4979 This returns the inverse of the condition because it is called
4980 from catch_errors which returns 0 if an exception happened, and if an
4981 exception happens we want execution to stop.
4a64f543 4982 The argument is a "struct expression *" that has been cast to a
c42bd95a 4983 "void *" to make it pass through catch_errors. */
c906108c
SS
4984
4985static int
4efb68b1 4986breakpoint_cond_eval (void *exp)
c906108c 4987{
278cd55f 4988 struct value *mark = value_mark ();
c5aa993b 4989 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4990
c906108c
SS
4991 value_free_to_mark (mark);
4992 return i;
4993}
4994
5760d0ab 4995/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4996
4997static bpstat
5760d0ab 4998bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4999{
5000 bpstat bs;
5001
5002 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5003 bs->next = NULL;
5004 **bs_link_pointer = bs;
5005 *bs_link_pointer = &bs->next;
f431efe5
PA
5006 bs->breakpoint_at = bl->owner;
5007 bs->bp_location_at = bl;
5008 incref_bp_location (bl);
c906108c
SS
5009 /* If the condition is false, etc., don't do the commands. */
5010 bs->commands = NULL;
5011 bs->old_val = NULL;
5012 bs->print_it = print_it_normal;
5013 return bs;
5014}
5015\f
d983da9c
DJ
5016/* The target has stopped with waitstatus WS. Check if any hardware
5017 watchpoints have triggered, according to the target. */
5018
5019int
5020watchpoints_triggered (struct target_waitstatus *ws)
5021{
d92524f1 5022 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5023 CORE_ADDR addr;
5024 struct breakpoint *b;
5025
5026 if (!stopped_by_watchpoint)
5027 {
5028 /* We were not stopped by a watchpoint. Mark all watchpoints
5029 as not triggered. */
5030 ALL_BREAKPOINTS (b)
cc60f2e3 5031 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5032 {
5033 struct watchpoint *w = (struct watchpoint *) b;
5034
5035 w->watchpoint_triggered = watch_triggered_no;
5036 }
d983da9c
DJ
5037
5038 return 0;
5039 }
5040
5041 if (!target_stopped_data_address (&current_target, &addr))
5042 {
5043 /* We were stopped by a watchpoint, but we don't know where.
5044 Mark all watchpoints as unknown. */
5045 ALL_BREAKPOINTS (b)
cc60f2e3 5046 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5047 {
5048 struct watchpoint *w = (struct watchpoint *) b;
5049
5050 w->watchpoint_triggered = watch_triggered_unknown;
5051 }
d983da9c 5052
3c4797ba 5053 return 1;
d983da9c
DJ
5054 }
5055
5056 /* The target could report the data address. Mark watchpoints
5057 affected by this data address as triggered, and all others as not
5058 triggered. */
5059
5060 ALL_BREAKPOINTS (b)
cc60f2e3 5061 if (is_hardware_watchpoint (b))
d983da9c 5062 {
3a5c3e22 5063 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5064 struct bp_location *loc;
d983da9c 5065
3a5c3e22 5066 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5067 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5068 {
3a5c3e22 5069 if (is_masked_watchpoint (b))
9c06b0b4 5070 {
3a5c3e22
PA
5071 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5072 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5073
5074 if (newaddr == start)
5075 {
3a5c3e22 5076 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5077 break;
5078 }
5079 }
5080 /* Exact match not required. Within range is sufficient. */
5081 else if (target_watchpoint_addr_within_range (&current_target,
5082 addr, loc->address,
5083 loc->length))
5084 {
3a5c3e22 5085 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5086 break;
5087 }
5088 }
d983da9c
DJ
5089 }
5090
5091 return 1;
5092}
5093
c906108c
SS
5094/* Possible return values for watchpoint_check (this can't be an enum
5095 because of check_errors). */
5096/* The watchpoint has been deleted. */
5097#define WP_DELETED 1
5098/* The value has changed. */
5099#define WP_VALUE_CHANGED 2
5100/* The value has not changed. */
5101#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5102/* Ignore this watchpoint, no matter if the value changed or not. */
5103#define WP_IGNORE 4
c906108c
SS
5104
5105#define BP_TEMPFLAG 1
5106#define BP_HARDWAREFLAG 2
5107
4a64f543
MS
5108/* Evaluate watchpoint condition expression and check if its value
5109 changed.
553e4c11
JB
5110
5111 P should be a pointer to struct bpstat, but is defined as a void *
5112 in order for this function to be usable with catch_errors. */
c906108c
SS
5113
5114static int
4efb68b1 5115watchpoint_check (void *p)
c906108c
SS
5116{
5117 bpstat bs = (bpstat) p;
3a5c3e22 5118 struct watchpoint *b;
c906108c
SS
5119 struct frame_info *fr;
5120 int within_current_scope;
5121
f431efe5 5122 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5123 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5124 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5125
f6bc2008
PA
5126 /* If this is a local watchpoint, we only want to check if the
5127 watchpoint frame is in scope if the current thread is the thread
5128 that was used to create the watchpoint. */
5129 if (!watchpoint_in_thread_scope (b))
60e1c644 5130 return WP_IGNORE;
f6bc2008 5131
c906108c
SS
5132 if (b->exp_valid_block == NULL)
5133 within_current_scope = 1;
5134 else
5135 {
edb3359d
DJ
5136 struct frame_info *frame = get_current_frame ();
5137 struct gdbarch *frame_arch = get_frame_arch (frame);
5138 CORE_ADDR frame_pc = get_frame_pc (frame);
5139
c9cf6e20 5140 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5141 still in the function but the stack frame has already been
5142 invalidated. Since we can't rely on the values of local
5143 variables after the stack has been destroyed, we are treating
5144 the watchpoint in that state as `not changed' without further
5145 checking. Don't mark watchpoints as changed if the current
5146 frame is in an epilogue - even if they are in some other
5147 frame, our view of the stack is likely to be wrong and
5148 frame_find_by_id could error out. */
c9cf6e20 5149 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5150 return WP_IGNORE;
a0f49112 5151
101dcfbe 5152 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5153 within_current_scope = (fr != NULL);
69fbadd5
DJ
5154
5155 /* If we've gotten confused in the unwinder, we might have
5156 returned a frame that can't describe this variable. */
edb3359d
DJ
5157 if (within_current_scope)
5158 {
5159 struct symbol *function;
5160
5161 function = get_frame_function (fr);
5162 if (function == NULL
5163 || !contained_in (b->exp_valid_block,
5164 SYMBOL_BLOCK_VALUE (function)))
5165 within_current_scope = 0;
5166 }
69fbadd5 5167
edb3359d 5168 if (within_current_scope)
c906108c
SS
5169 /* If we end up stopping, the current frame will get selected
5170 in normal_stop. So this call to select_frame won't affect
5171 the user. */
0f7d239c 5172 select_frame (fr);
c906108c 5173 }
c5aa993b 5174
c906108c
SS
5175 if (within_current_scope)
5176 {
4a64f543
MS
5177 /* We use value_{,free_to_}mark because it could be a *long*
5178 time before we return to the command level and call
5179 free_all_values. We can't call free_all_values because we
5180 might be in the middle of evaluating a function call. */
c906108c 5181
0cf6dd15 5182 int pc = 0;
9c06b0b4 5183 struct value *mark;
fa4727a6
DJ
5184 struct value *new_val;
5185
3a5c3e22 5186 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5187 /* Since we don't know the exact trigger address (from
5188 stopped_data_address), just tell the user we've triggered
5189 a mask watchpoint. */
5190 return WP_VALUE_CHANGED;
5191
5192 mark = value_mark ();
3a1115a0 5193 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5194
bb9d5f81
PP
5195 if (b->val_bitsize != 0)
5196 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5197
4a64f543
MS
5198 /* We use value_equal_contents instead of value_equal because
5199 the latter coerces an array to a pointer, thus comparing just
5200 the address of the array instead of its contents. This is
5201 not what we want. */
fa4727a6 5202 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5203 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5204 {
fa4727a6
DJ
5205 if (new_val != NULL)
5206 {
5207 release_value (new_val);
5208 value_free_to_mark (mark);
5209 }
c906108c
SS
5210 bs->old_val = b->val;
5211 b->val = new_val;
fa4727a6 5212 b->val_valid = 1;
c906108c
SS
5213 return WP_VALUE_CHANGED;
5214 }
5215 else
5216 {
60e1c644 5217 /* Nothing changed. */
c906108c 5218 value_free_to_mark (mark);
c906108c
SS
5219 return WP_VALUE_NOT_CHANGED;
5220 }
5221 }
5222 else
5223 {
79a45e25
PA
5224 struct ui_out *uiout = current_uiout;
5225
c906108c 5226 /* This seems like the only logical thing to do because
c5aa993b
JM
5227 if we temporarily ignored the watchpoint, then when
5228 we reenter the block in which it is valid it contains
5229 garbage (in the case of a function, it may have two
5230 garbage values, one before and one after the prologue).
5231 So we can't even detect the first assignment to it and
5232 watch after that (since the garbage may or may not equal
5233 the first value assigned). */
348d480f
PA
5234 /* We print all the stop information in
5235 breakpoint_ops->print_it, but in this case, by the time we
5236 call breakpoint_ops->print_it this bp will be deleted
5237 already. So we have no choice but print the information
5238 here. */
9dc5e2a9 5239 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5240 ui_out_field_string
5241 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5242 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5243 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5244 ui_out_text (uiout,
5245 " deleted because the program has left the block in\n\
8b93c638 5246which its expression is valid.\n");
4ce44c66 5247
cdac0397 5248 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5249 decref_counted_command_line (&b->base.commands);
d0fb5eae 5250 watchpoint_del_at_next_stop (b);
c906108c
SS
5251
5252 return WP_DELETED;
5253 }
5254}
5255
18a18393 5256/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5257 breakpoint location BL. This function does not check if we should
5258 stop, only if BL explains the stop. */
5259
18a18393 5260static int
6c95b8df 5261bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5262 struct address_space *aspace, CORE_ADDR bp_addr,
5263 const struct target_waitstatus *ws)
18a18393
VP
5264{
5265 struct breakpoint *b = bl->owner;
5266
348d480f 5267 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5268 gdb_assert (b != NULL);
5269
09ac7c10 5270 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5271}
5272
3a5c3e22
PA
5273/* Determine if the watched values have actually changed, and we
5274 should stop. If not, set BS->stop to 0. */
5275
18a18393
VP
5276static void
5277bpstat_check_watchpoint (bpstat bs)
5278{
2bdf28a0 5279 const struct bp_location *bl;
3a5c3e22 5280 struct watchpoint *b;
2bdf28a0
JK
5281
5282 /* BS is built for existing struct breakpoint. */
f431efe5 5283 bl = bs->bp_location_at;
2bdf28a0 5284 gdb_assert (bl != NULL);
3a5c3e22 5285 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5286 gdb_assert (b != NULL);
18a18393 5287
18a18393 5288 {
18a18393
VP
5289 int must_check_value = 0;
5290
3a5c3e22 5291 if (b->base.type == bp_watchpoint)
18a18393
VP
5292 /* For a software watchpoint, we must always check the
5293 watched value. */
5294 must_check_value = 1;
5295 else if (b->watchpoint_triggered == watch_triggered_yes)
5296 /* We have a hardware watchpoint (read, write, or access)
5297 and the target earlier reported an address watched by
5298 this watchpoint. */
5299 must_check_value = 1;
5300 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5301 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5302 /* We were stopped by a hardware watchpoint, but the target could
5303 not report the data address. We must check the watchpoint's
5304 value. Access and read watchpoints are out of luck; without
5305 a data address, we can't figure it out. */
5306 must_check_value = 1;
3a5c3e22 5307
18a18393
VP
5308 if (must_check_value)
5309 {
3e43a32a
MS
5310 char *message
5311 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5312 b->base.number);
18a18393
VP
5313 struct cleanup *cleanups = make_cleanup (xfree, message);
5314 int e = catch_errors (watchpoint_check, bs, message,
5315 RETURN_MASK_ALL);
5316 do_cleanups (cleanups);
5317 switch (e)
5318 {
5319 case WP_DELETED:
5320 /* We've already printed what needs to be printed. */
5321 bs->print_it = print_it_done;
5322 /* Stop. */
5323 break;
60e1c644
PA
5324 case WP_IGNORE:
5325 bs->print_it = print_it_noop;
5326 bs->stop = 0;
5327 break;
18a18393 5328 case WP_VALUE_CHANGED:
3a5c3e22 5329 if (b->base.type == bp_read_watchpoint)
18a18393 5330 {
85d721b8
PA
5331 /* There are two cases to consider here:
5332
4a64f543 5333 1. We're watching the triggered memory for reads.
85d721b8
PA
5334 In that case, trust the target, and always report
5335 the watchpoint hit to the user. Even though
5336 reads don't cause value changes, the value may
5337 have changed since the last time it was read, and
5338 since we're not trapping writes, we will not see
5339 those, and as such we should ignore our notion of
5340 old value.
5341
4a64f543 5342 2. We're watching the triggered memory for both
85d721b8
PA
5343 reads and writes. There are two ways this may
5344 happen:
5345
4a64f543 5346 2.1. This is a target that can't break on data
85d721b8
PA
5347 reads only, but can break on accesses (reads or
5348 writes), such as e.g., x86. We detect this case
5349 at the time we try to insert read watchpoints.
5350
4a64f543 5351 2.2. Otherwise, the target supports read
85d721b8
PA
5352 watchpoints, but, the user set an access or write
5353 watchpoint watching the same memory as this read
5354 watchpoint.
5355
5356 If we're watching memory writes as well as reads,
5357 ignore watchpoint hits when we find that the
5358 value hasn't changed, as reads don't cause
5359 changes. This still gives false positives when
5360 the program writes the same value to memory as
5361 what there was already in memory (we will confuse
5362 it for a read), but it's much better than
5363 nothing. */
5364
5365 int other_write_watchpoint = 0;
5366
5367 if (bl->watchpoint_type == hw_read)
5368 {
5369 struct breakpoint *other_b;
5370
5371 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5372 if (other_b->type == bp_hardware_watchpoint
5373 || other_b->type == bp_access_watchpoint)
85d721b8 5374 {
3a5c3e22
PA
5375 struct watchpoint *other_w =
5376 (struct watchpoint *) other_b;
5377
5378 if (other_w->watchpoint_triggered
5379 == watch_triggered_yes)
5380 {
5381 other_write_watchpoint = 1;
5382 break;
5383 }
85d721b8
PA
5384 }
5385 }
5386
5387 if (other_write_watchpoint
5388 || bl->watchpoint_type == hw_access)
5389 {
5390 /* We're watching the same memory for writes,
5391 and the value changed since the last time we
5392 updated it, so this trap must be for a write.
5393 Ignore it. */
5394 bs->print_it = print_it_noop;
5395 bs->stop = 0;
5396 }
18a18393
VP
5397 }
5398 break;
5399 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5400 if (b->base.type == bp_hardware_watchpoint
5401 || b->base.type == bp_watchpoint)
18a18393
VP
5402 {
5403 /* Don't stop: write watchpoints shouldn't fire if
5404 the value hasn't changed. */
5405 bs->print_it = print_it_noop;
5406 bs->stop = 0;
5407 }
5408 /* Stop. */
5409 break;
5410 default:
5411 /* Can't happen. */
5412 case 0:
5413 /* Error from catch_errors. */
3a5c3e22 5414 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5415 watchpoint_del_at_next_stop (b);
18a18393
VP
5416 /* We've already printed what needs to be printed. */
5417 bs->print_it = print_it_done;
5418 break;
5419 }
5420 }
5421 else /* must_check_value == 0 */
5422 {
5423 /* This is a case where some watchpoint(s) triggered, but
5424 not at the address of this watchpoint, or else no
5425 watchpoint triggered after all. So don't print
5426 anything for this watchpoint. */
5427 bs->print_it = print_it_noop;
5428 bs->stop = 0;
5429 }
5430 }
5431}
5432
7d4df6a4
DE
5433/* For breakpoints that are currently marked as telling gdb to stop,
5434 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5435 of breakpoint referred to by BS. If we should not stop for this
5436 breakpoint, set BS->stop to 0. */
f431efe5 5437
18a18393
VP
5438static void
5439bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5440{
2bdf28a0
JK
5441 const struct bp_location *bl;
5442 struct breakpoint *b;
7d4df6a4
DE
5443 int value_is_zero = 0;
5444 struct expression *cond;
5445
5446 gdb_assert (bs->stop);
2bdf28a0
JK
5447
5448 /* BS is built for existing struct breakpoint. */
f431efe5 5449 bl = bs->bp_location_at;
2bdf28a0 5450 gdb_assert (bl != NULL);
f431efe5 5451 b = bs->breakpoint_at;
2bdf28a0 5452 gdb_assert (b != NULL);
18a18393 5453
b775012e
LM
5454 /* Even if the target evaluated the condition on its end and notified GDB, we
5455 need to do so again since GDB does not know if we stopped due to a
5456 breakpoint or a single step breakpoint. */
5457
18a18393 5458 if (frame_id_p (b->frame_id)
edb3359d 5459 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5460 {
7d4df6a4
DE
5461 bs->stop = 0;
5462 return;
5463 }
60e1c644 5464
12ab52e9
PA
5465 /* If this is a thread/task-specific breakpoint, don't waste cpu
5466 evaluating the condition if this isn't the specified
5467 thread/task. */
5d5658a1 5468 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5469 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5470
6c1b0f7b
DE
5471 {
5472 bs->stop = 0;
5473 return;
5474 }
5475
6dddc817
DE
5476 /* Evaluate extension language breakpoints that have a "stop" method
5477 implemented. */
5478 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5479
7d4df6a4
DE
5480 if (is_watchpoint (b))
5481 {
5482 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5483
7d4df6a4
DE
5484 cond = w->cond_exp;
5485 }
5486 else
5487 cond = bl->cond;
60e1c644 5488
7d4df6a4
DE
5489 if (cond && b->disposition != disp_del_at_next_stop)
5490 {
5491 int within_current_scope = 1;
5492 struct watchpoint * w;
60e1c644 5493
7d4df6a4
DE
5494 /* We use value_mark and value_free_to_mark because it could
5495 be a long time before we return to the command level and
5496 call free_all_values. We can't call free_all_values
5497 because we might be in the middle of evaluating a
5498 function call. */
5499 struct value *mark = value_mark ();
5500
5501 if (is_watchpoint (b))
5502 w = (struct watchpoint *) b;
5503 else
5504 w = NULL;
5505
5506 /* Need to select the frame, with all that implies so that
5507 the conditions will have the right context. Because we
5508 use the frame, we will not see an inlined function's
5509 variables when we arrive at a breakpoint at the start
5510 of the inlined function; the current frame will be the
5511 call site. */
5512 if (w == NULL || w->cond_exp_valid_block == NULL)
5513 select_frame (get_current_frame ());
5514 else
18a18393 5515 {
7d4df6a4
DE
5516 struct frame_info *frame;
5517
5518 /* For local watchpoint expressions, which particular
5519 instance of a local is being watched matters, so we
5520 keep track of the frame to evaluate the expression
5521 in. To evaluate the condition however, it doesn't
5522 really matter which instantiation of the function
5523 where the condition makes sense triggers the
5524 watchpoint. This allows an expression like "watch
5525 global if q > 10" set in `func', catch writes to
5526 global on all threads that call `func', or catch
5527 writes on all recursive calls of `func' by a single
5528 thread. We simply always evaluate the condition in
5529 the innermost frame that's executing where it makes
5530 sense to evaluate the condition. It seems
5531 intuitive. */
5532 frame = block_innermost_frame (w->cond_exp_valid_block);
5533 if (frame != NULL)
5534 select_frame (frame);
5535 else
5536 within_current_scope = 0;
18a18393 5537 }
7d4df6a4
DE
5538 if (within_current_scope)
5539 value_is_zero
5540 = catch_errors (breakpoint_cond_eval, cond,
5541 "Error in testing breakpoint condition:\n",
5542 RETURN_MASK_ALL);
5543 else
18a18393 5544 {
7d4df6a4
DE
5545 warning (_("Watchpoint condition cannot be tested "
5546 "in the current scope"));
5547 /* If we failed to set the right context for this
5548 watchpoint, unconditionally report it. */
5549 value_is_zero = 0;
18a18393 5550 }
7d4df6a4
DE
5551 /* FIXME-someday, should give breakpoint #. */
5552 value_free_to_mark (mark);
18a18393 5553 }
7d4df6a4
DE
5554
5555 if (cond && value_is_zero)
5556 {
5557 bs->stop = 0;
5558 }
7d4df6a4
DE
5559 else if (b->ignore_count > 0)
5560 {
5561 b->ignore_count--;
5562 bs->stop = 0;
5563 /* Increase the hit count even though we don't stop. */
5564 ++(b->hit_count);
5565 observer_notify_breakpoint_modified (b);
5566 }
18a18393
VP
5567}
5568
1cf4d951
PA
5569/* Returns true if we need to track moribund locations of LOC's type
5570 on the current target. */
5571
5572static int
5573need_moribund_for_location_type (struct bp_location *loc)
5574{
5575 return ((loc->loc_type == bp_loc_software_breakpoint
5576 && !target_supports_stopped_by_sw_breakpoint ())
5577 || (loc->loc_type == bp_loc_hardware_breakpoint
5578 && !target_supports_stopped_by_hw_breakpoint ()));
5579}
5580
18a18393 5581
9709f61c 5582/* Get a bpstat associated with having just stopped at address
d983da9c 5583 BP_ADDR in thread PTID.
c906108c 5584
d983da9c 5585 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5586 don't understand this stop. Result is a chain of bpstat's such
5587 that:
c906108c 5588
c5aa993b 5589 if we don't understand the stop, the result is a null pointer.
c906108c 5590
c5aa993b 5591 if we understand why we stopped, the result is not null.
c906108c 5592
c5aa993b
JM
5593 Each element of the chain refers to a particular breakpoint or
5594 watchpoint at which we have stopped. (We may have stopped for
5595 several reasons concurrently.)
c906108c 5596
c5aa993b
JM
5597 Each element of the chain has valid next, breakpoint_at,
5598 commands, FIXME??? fields. */
c906108c
SS
5599
5600bpstat
6c95b8df 5601bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5602 CORE_ADDR bp_addr, ptid_t ptid,
5603 const struct target_waitstatus *ws)
c906108c 5604{
0d381245 5605 struct breakpoint *b = NULL;
afe38095 5606 struct bp_location *bl;
20874c92 5607 struct bp_location *loc;
5760d0ab
JK
5608 /* First item of allocated bpstat's. */
5609 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5610 /* Pointer to the last thing in the chain currently. */
5760d0ab 5611 bpstat bs;
20874c92 5612 int ix;
429374b8 5613 int need_remove_insert;
f431efe5 5614 int removed_any;
c906108c 5615
f431efe5
PA
5616 /* First, build the bpstat chain with locations that explain a
5617 target stop, while being careful to not set the target running,
5618 as that may invalidate locations (in particular watchpoint
5619 locations are recreated). Resuming will happen here with
5620 breakpoint conditions or watchpoint expressions that include
5621 inferior function calls. */
c5aa993b 5622
429374b8
JK
5623 ALL_BREAKPOINTS (b)
5624 {
1a853c52 5625 if (!breakpoint_enabled (b))
429374b8 5626 continue;
a5606eee 5627
429374b8
JK
5628 for (bl = b->loc; bl != NULL; bl = bl->next)
5629 {
4a64f543
MS
5630 /* For hardware watchpoints, we look only at the first
5631 location. The watchpoint_check function will work on the
5632 entire expression, not the individual locations. For
5633 read watchpoints, the watchpoints_triggered function has
5634 checked all locations already. */
429374b8
JK
5635 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5636 break;
18a18393 5637
f6592439 5638 if (!bl->enabled || bl->shlib_disabled)
429374b8 5639 continue;
c5aa993b 5640
09ac7c10 5641 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5642 continue;
c5aa993b 5643
4a64f543
MS
5644 /* Come here if it's a watchpoint, or if the break address
5645 matches. */
c5aa993b 5646
4a64f543
MS
5647 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5648 explain stop. */
c5aa993b 5649
f431efe5
PA
5650 /* Assume we stop. Should we find a watchpoint that is not
5651 actually triggered, or if the condition of the breakpoint
5652 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5653 bs->stop = 1;
5654 bs->print = 1;
d983da9c 5655
f431efe5
PA
5656 /* If this is a scope breakpoint, mark the associated
5657 watchpoint as triggered so that we will handle the
5658 out-of-scope event. We'll get to the watchpoint next
5659 iteration. */
d0fb5eae 5660 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5661 {
5662 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5663
5664 w->watchpoint_triggered = watch_triggered_yes;
5665 }
f431efe5
PA
5666 }
5667 }
5668
7c16b83e 5669 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5670 if (!target_supports_stopped_by_sw_breakpoint ()
5671 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5672 {
1cf4d951 5673 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5674 {
1cf4d951
PA
5675 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5676 && need_moribund_for_location_type (loc))
5677 {
5678 bs = bpstat_alloc (loc, &bs_link);
5679 /* For hits of moribund locations, we should just proceed. */
5680 bs->stop = 0;
5681 bs->print = 0;
5682 bs->print_it = print_it_noop;
5683 }
f431efe5
PA
5684 }
5685 }
5686
edcc5120
TT
5687 /* A bit of special processing for shlib breakpoints. We need to
5688 process solib loading here, so that the lists of loaded and
5689 unloaded libraries are correct before we handle "catch load" and
5690 "catch unload". */
5691 for (bs = bs_head; bs != NULL; bs = bs->next)
5692 {
5d268276 5693 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5694 {
5695 handle_solib_event ();
5696 break;
5697 }
5698 }
5699
f431efe5
PA
5700 /* Now go through the locations that caused the target to stop, and
5701 check whether we're interested in reporting this stop to higher
5702 layers, or whether we should resume the target transparently. */
5703
5704 removed_any = 0;
5705
5760d0ab 5706 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5707 {
5708 if (!bs->stop)
5709 continue;
5710
f431efe5 5711 b = bs->breakpoint_at;
348d480f
PA
5712 b->ops->check_status (bs);
5713 if (bs->stop)
28010a5d 5714 {
348d480f 5715 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5716
429374b8
JK
5717 if (bs->stop)
5718 {
5719 ++(b->hit_count);
8d3788bd 5720 observer_notify_breakpoint_modified (b);
c906108c 5721
4a64f543 5722 /* We will stop here. */
429374b8
JK
5723 if (b->disposition == disp_disable)
5724 {
816338b5 5725 --(b->enable_count);
1a853c52 5726 if (b->enable_count <= 0)
429374b8 5727 b->enable_state = bp_disabled;
f431efe5 5728 removed_any = 1;
429374b8
JK
5729 }
5730 if (b->silent)
5731 bs->print = 0;
5732 bs->commands = b->commands;
9add0f1b 5733 incref_counted_command_line (bs->commands);
abf85f46
JK
5734 if (command_line_is_silent (bs->commands
5735 ? bs->commands->commands : NULL))
5736 bs->print = 0;
9d6e6e84
HZ
5737
5738 b->ops->after_condition_true (bs);
429374b8
JK
5739 }
5740
348d480f 5741 }
a9b3a50f
PA
5742
5743 /* Print nothing for this entry if we don't stop or don't
5744 print. */
5745 if (!bs->stop || !bs->print)
5746 bs->print_it = print_it_noop;
429374b8 5747 }
876fa593 5748
d983da9c
DJ
5749 /* If we aren't stopping, the value of some hardware watchpoint may
5750 not have changed, but the intermediate memory locations we are
5751 watching may have. Don't bother if we're stopping; this will get
5752 done later. */
d832cb68 5753 need_remove_insert = 0;
5760d0ab
JK
5754 if (! bpstat_causes_stop (bs_head))
5755 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5756 if (!bs->stop
f431efe5
PA
5757 && bs->breakpoint_at
5758 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5759 {
3a5c3e22
PA
5760 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5761
5762 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5763 need_remove_insert = 1;
d983da9c
DJ
5764 }
5765
d832cb68 5766 if (need_remove_insert)
44702360 5767 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5768 else if (removed_any)
44702360 5769 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5770
5760d0ab 5771 return bs_head;
c906108c 5772}
628fe4e4
JK
5773
5774static void
5775handle_jit_event (void)
5776{
5777 struct frame_info *frame;
5778 struct gdbarch *gdbarch;
5779
243a9253
PA
5780 if (debug_infrun)
5781 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5782
628fe4e4
JK
5783 /* Switch terminal for any messages produced by
5784 breakpoint_re_set. */
5785 target_terminal_ours_for_output ();
5786
5787 frame = get_current_frame ();
5788 gdbarch = get_frame_arch (frame);
5789
5790 jit_event_handler (gdbarch);
5791
5792 target_terminal_inferior ();
5793}
5794
5795/* Prepare WHAT final decision for infrun. */
5796
5797/* Decide what infrun needs to do with this bpstat. */
5798
c906108c 5799struct bpstat_what
0e30163f 5800bpstat_what (bpstat bs_head)
c906108c 5801{
c906108c 5802 struct bpstat_what retval;
628fe4e4 5803 int jit_event = 0;
0e30163f 5804 bpstat bs;
c906108c 5805
628fe4e4 5806 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5807 retval.call_dummy = STOP_NONE;
186c406b 5808 retval.is_longjmp = 0;
628fe4e4 5809
0e30163f 5810 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5811 {
628fe4e4
JK
5812 /* Extract this BS's action. After processing each BS, we check
5813 if its action overrides all we've seem so far. */
5814 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5815 enum bptype bptype;
5816
c906108c 5817 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5818 {
5819 /* I suspect this can happen if it was a momentary
5820 breakpoint which has since been deleted. */
5821 bptype = bp_none;
5822 }
20874c92 5823 else
f431efe5 5824 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5825
5826 switch (bptype)
c906108c
SS
5827 {
5828 case bp_none:
628fe4e4 5829 break;
c906108c
SS
5830 case bp_breakpoint:
5831 case bp_hardware_breakpoint:
7c16b83e 5832 case bp_single_step:
c906108c
SS
5833 case bp_until:
5834 case bp_finish:
a9b3a50f 5835 case bp_shlib_event:
c906108c
SS
5836 if (bs->stop)
5837 {
5838 if (bs->print)
628fe4e4 5839 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5840 else
628fe4e4 5841 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5842 }
5843 else
628fe4e4 5844 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5845 break;
5846 case bp_watchpoint:
5847 case bp_hardware_watchpoint:
5848 case bp_read_watchpoint:
5849 case bp_access_watchpoint:
5850 if (bs->stop)
5851 {
5852 if (bs->print)
628fe4e4 5853 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5854 else
628fe4e4 5855 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5856 }
5857 else
628fe4e4
JK
5858 {
5859 /* There was a watchpoint, but we're not stopping.
5860 This requires no further action. */
5861 }
c906108c
SS
5862 break;
5863 case bp_longjmp:
e2e4d78b 5864 case bp_longjmp_call_dummy:
186c406b 5865 case bp_exception:
0a39bb32
PA
5866 if (bs->stop)
5867 {
5868 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5869 retval.is_longjmp = bptype != bp_exception;
5870 }
5871 else
5872 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5873 break;
5874 case bp_longjmp_resume:
186c406b 5875 case bp_exception_resume:
0a39bb32
PA
5876 if (bs->stop)
5877 {
5878 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5879 retval.is_longjmp = bptype == bp_longjmp_resume;
5880 }
5881 else
5882 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5883 break;
5884 case bp_step_resume:
5885 if (bs->stop)
628fe4e4
JK
5886 this_action = BPSTAT_WHAT_STEP_RESUME;
5887 else
c906108c 5888 {
628fe4e4
JK
5889 /* It is for the wrong frame. */
5890 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5891 }
c906108c 5892 break;
2c03e5be
PA
5893 case bp_hp_step_resume:
5894 if (bs->stop)
5895 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5896 else
5897 {
5898 /* It is for the wrong frame. */
5899 this_action = BPSTAT_WHAT_SINGLE;
5900 }
5901 break;
c906108c 5902 case bp_watchpoint_scope:
c4093a6a 5903 case bp_thread_event:
1900040c 5904 case bp_overlay_event:
0fd8e87f 5905 case bp_longjmp_master:
aa7d318d 5906 case bp_std_terminate_master:
186c406b 5907 case bp_exception_master:
628fe4e4 5908 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5909 break;
ce78b96d 5910 case bp_catchpoint:
c5aa993b
JM
5911 if (bs->stop)
5912 {
5913 if (bs->print)
628fe4e4 5914 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5915 else
628fe4e4 5916 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5917 }
5918 else
628fe4e4
JK
5919 {
5920 /* There was a catchpoint, but we're not stopping.
5921 This requires no further action. */
5922 }
5923 break;
628fe4e4
JK
5924 case bp_jit_event:
5925 jit_event = 1;
5926 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5927 break;
c906108c 5928 case bp_call_dummy:
53a5351d
JM
5929 /* Make sure the action is stop (silent or noisy),
5930 so infrun.c pops the dummy frame. */
aa7d318d 5931 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5932 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5933 break;
5934 case bp_std_terminate:
5935 /* Make sure the action is stop (silent or noisy),
5936 so infrun.c pops the dummy frame. */
aa7d318d 5937 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5938 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5939 break;
1042e4c0 5940 case bp_tracepoint:
7a697b8d 5941 case bp_fast_tracepoint:
0fb4aa4b 5942 case bp_static_tracepoint:
1042e4c0
SS
5943 /* Tracepoint hits should not be reported back to GDB, and
5944 if one got through somehow, it should have been filtered
5945 out already. */
5946 internal_error (__FILE__, __LINE__,
7a697b8d 5947 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5948 break;
5949 case bp_gnu_ifunc_resolver:
5950 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5951 this_action = BPSTAT_WHAT_SINGLE;
5952 break;
5953 case bp_gnu_ifunc_resolver_return:
5954 /* The breakpoint will be removed, execution will restart from the
5955 PC of the former breakpoint. */
5956 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5957 break;
e7e0cddf
SS
5958
5959 case bp_dprintf:
a11cfd87
HZ
5960 if (bs->stop)
5961 this_action = BPSTAT_WHAT_STOP_SILENT;
5962 else
5963 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5964 break;
5965
628fe4e4
JK
5966 default:
5967 internal_error (__FILE__, __LINE__,
5968 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5969 }
628fe4e4
JK
5970
5971 retval.main_action = max (retval.main_action, this_action);
c906108c 5972 }
628fe4e4 5973
243a9253
PA
5974 return retval;
5975}
628fe4e4 5976
243a9253
PA
5977void
5978bpstat_run_callbacks (bpstat bs_head)
5979{
5980 bpstat bs;
628fe4e4 5981
0e30163f
JK
5982 for (bs = bs_head; bs != NULL; bs = bs->next)
5983 {
5984 struct breakpoint *b = bs->breakpoint_at;
5985
5986 if (b == NULL)
5987 continue;
5988 switch (b->type)
5989 {
243a9253
PA
5990 case bp_jit_event:
5991 handle_jit_event ();
5992 break;
0e30163f
JK
5993 case bp_gnu_ifunc_resolver:
5994 gnu_ifunc_resolver_stop (b);
5995 break;
5996 case bp_gnu_ifunc_resolver_return:
5997 gnu_ifunc_resolver_return_stop (b);
5998 break;
5999 }
6000 }
c906108c
SS
6001}
6002
6003/* Nonzero if we should step constantly (e.g. watchpoints on machines
6004 without hardware support). This isn't related to a specific bpstat,
6005 just to things like whether watchpoints are set. */
6006
c5aa993b 6007int
fba45db2 6008bpstat_should_step (void)
c906108c
SS
6009{
6010 struct breakpoint *b;
cc59ec59 6011
c906108c 6012 ALL_BREAKPOINTS (b)
717a8278 6013 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6014 return 1;
c906108c
SS
6015 return 0;
6016}
6017
67822962
PA
6018int
6019bpstat_causes_stop (bpstat bs)
6020{
6021 for (; bs != NULL; bs = bs->next)
6022 if (bs->stop)
6023 return 1;
6024
6025 return 0;
6026}
6027
c906108c 6028\f
c5aa993b 6029
170b53b2
UW
6030/* Compute a string of spaces suitable to indent the next line
6031 so it starts at the position corresponding to the table column
6032 named COL_NAME in the currently active table of UIOUT. */
6033
6034static char *
6035wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6036{
6037 static char wrap_indent[80];
6038 int i, total_width, width, align;
6039 char *text;
6040
6041 total_width = 0;
6042 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6043 {
6044 if (strcmp (text, col_name) == 0)
6045 {
6046 gdb_assert (total_width < sizeof wrap_indent);
6047 memset (wrap_indent, ' ', total_width);
6048 wrap_indent[total_width] = 0;
6049
6050 return wrap_indent;
6051 }
6052
6053 total_width += width + 1;
6054 }
6055
6056 return NULL;
6057}
6058
b775012e
LM
6059/* Determine if the locations of this breakpoint will have their conditions
6060 evaluated by the target, host or a mix of both. Returns the following:
6061
6062 "host": Host evals condition.
6063 "host or target": Host or Target evals condition.
6064 "target": Target evals condition.
6065*/
6066
6067static const char *
6068bp_condition_evaluator (struct breakpoint *b)
6069{
6070 struct bp_location *bl;
6071 char host_evals = 0;
6072 char target_evals = 0;
6073
6074 if (!b)
6075 return NULL;
6076
6077 if (!is_breakpoint (b))
6078 return NULL;
6079
6080 if (gdb_evaluates_breakpoint_condition_p ()
6081 || !target_supports_evaluation_of_breakpoint_conditions ())
6082 return condition_evaluation_host;
6083
6084 for (bl = b->loc; bl; bl = bl->next)
6085 {
6086 if (bl->cond_bytecode)
6087 target_evals++;
6088 else
6089 host_evals++;
6090 }
6091
6092 if (host_evals && target_evals)
6093 return condition_evaluation_both;
6094 else if (target_evals)
6095 return condition_evaluation_target;
6096 else
6097 return condition_evaluation_host;
6098}
6099
6100/* Determine the breakpoint location's condition evaluator. This is
6101 similar to bp_condition_evaluator, but for locations. */
6102
6103static const char *
6104bp_location_condition_evaluator (struct bp_location *bl)
6105{
6106 if (bl && !is_breakpoint (bl->owner))
6107 return NULL;
6108
6109 if (gdb_evaluates_breakpoint_condition_p ()
6110 || !target_supports_evaluation_of_breakpoint_conditions ())
6111 return condition_evaluation_host;
6112
6113 if (bl && bl->cond_bytecode)
6114 return condition_evaluation_target;
6115 else
6116 return condition_evaluation_host;
6117}
6118
859825b8
JK
6119/* Print the LOC location out of the list of B->LOC locations. */
6120
170b53b2
UW
6121static void
6122print_breakpoint_location (struct breakpoint *b,
6123 struct bp_location *loc)
0d381245 6124{
79a45e25 6125 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6126 struct cleanup *old_chain = save_current_program_space ();
6127
859825b8
JK
6128 if (loc != NULL && loc->shlib_disabled)
6129 loc = NULL;
6130
6c95b8df
PA
6131 if (loc != NULL)
6132 set_current_program_space (loc->pspace);
6133
56435ebe 6134 if (b->display_canonical)
f00aae0f
KS
6135 ui_out_field_string (uiout, "what",
6136 event_location_to_string (b->location));
2f202fde 6137 else if (loc && loc->symtab)
0d381245
VP
6138 {
6139 struct symbol *sym
6140 = find_pc_sect_function (loc->address, loc->section);
6141 if (sym)
6142 {
6143 ui_out_text (uiout, "in ");
6144 ui_out_field_string (uiout, "func",
6145 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6146 ui_out_text (uiout, " ");
6147 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6148 ui_out_text (uiout, "at ");
0d381245 6149 }
05cba821
JK
6150 ui_out_field_string (uiout, "file",
6151 symtab_to_filename_for_display (loc->symtab));
0d381245 6152 ui_out_text (uiout, ":");
05cba821 6153
0d381245 6154 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6155 ui_out_field_string (uiout, "fullname",
6156 symtab_to_fullname (loc->symtab));
0d381245 6157
f8eba3c6 6158 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6159 }
859825b8 6160 else if (loc)
0d381245 6161 {
f99d8bf4
PA
6162 struct ui_file *stb = mem_fileopen ();
6163 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6164
f99d8bf4 6165 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6166 demangle, "");
0d381245 6167 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6168
6169 do_cleanups (stb_chain);
0d381245 6170 }
859825b8 6171 else
f00aae0f
KS
6172 {
6173 ui_out_field_string (uiout, "pending",
6174 event_location_to_string (b->location));
6175 /* If extra_string is available, it could be holding a condition
6176 or dprintf arguments. In either case, make sure it is printed,
6177 too, but only for non-MI streams. */
6178 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6179 {
6180 if (b->type == bp_dprintf)
6181 ui_out_text (uiout, ",");
6182 else
6183 ui_out_text (uiout, " ");
6184 ui_out_text (uiout, b->extra_string);
6185 }
6186 }
6c95b8df 6187
b775012e
LM
6188 if (loc && is_breakpoint (b)
6189 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6190 && bp_condition_evaluator (b) == condition_evaluation_both)
6191 {
6192 ui_out_text (uiout, " (");
6193 ui_out_field_string (uiout, "evaluated-by",
6194 bp_location_condition_evaluator (loc));
6195 ui_out_text (uiout, ")");
6196 }
6197
6c95b8df 6198 do_cleanups (old_chain);
0d381245
VP
6199}
6200
269b11a2
PA
6201static const char *
6202bptype_string (enum bptype type)
c906108c 6203{
c4093a6a
JM
6204 struct ep_type_description
6205 {
6206 enum bptype type;
6207 char *description;
6208 };
6209 static struct ep_type_description bptypes[] =
c906108c 6210 {
c5aa993b
JM
6211 {bp_none, "?deleted?"},
6212 {bp_breakpoint, "breakpoint"},
c906108c 6213 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6214 {bp_single_step, "sw single-step"},
c5aa993b
JM
6215 {bp_until, "until"},
6216 {bp_finish, "finish"},
6217 {bp_watchpoint, "watchpoint"},
c906108c 6218 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6219 {bp_read_watchpoint, "read watchpoint"},
6220 {bp_access_watchpoint, "acc watchpoint"},
6221 {bp_longjmp, "longjmp"},
6222 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6223 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6224 {bp_exception, "exception"},
6225 {bp_exception_resume, "exception resume"},
c5aa993b 6226 {bp_step_resume, "step resume"},
2c03e5be 6227 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6228 {bp_watchpoint_scope, "watchpoint scope"},
6229 {bp_call_dummy, "call dummy"},
aa7d318d 6230 {bp_std_terminate, "std::terminate"},
c5aa993b 6231 {bp_shlib_event, "shlib events"},
c4093a6a 6232 {bp_thread_event, "thread events"},
1900040c 6233 {bp_overlay_event, "overlay events"},
0fd8e87f 6234 {bp_longjmp_master, "longjmp master"},
aa7d318d 6235 {bp_std_terminate_master, "std::terminate master"},
186c406b 6236 {bp_exception_master, "exception master"},
ce78b96d 6237 {bp_catchpoint, "catchpoint"},
1042e4c0 6238 {bp_tracepoint, "tracepoint"},
7a697b8d 6239 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6240 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6241 {bp_dprintf, "dprintf"},
4efc6507 6242 {bp_jit_event, "jit events"},
0e30163f
JK
6243 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6244 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6245 };
269b11a2
PA
6246
6247 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6248 || ((int) type != bptypes[(int) type].type))
6249 internal_error (__FILE__, __LINE__,
6250 _("bptypes table does not describe type #%d."),
6251 (int) type);
6252
6253 return bptypes[(int) type].description;
6254}
6255
998580f1
MK
6256/* For MI, output a field named 'thread-groups' with a list as the value.
6257 For CLI, prefix the list with the string 'inf'. */
6258
6259static void
6260output_thread_groups (struct ui_out *uiout,
6261 const char *field_name,
6262 VEC(int) *inf_num,
6263 int mi_only)
6264{
752eb8b4 6265 struct cleanup *back_to;
998580f1
MK
6266 int is_mi = ui_out_is_mi_like_p (uiout);
6267 int inf;
6268 int i;
6269
6270 /* For backward compatibility, don't display inferiors in CLI unless
6271 there are several. Always display them for MI. */
6272 if (!is_mi && mi_only)
6273 return;
6274
752eb8b4
TT
6275 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6276
998580f1
MK
6277 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6278 {
6279 if (is_mi)
6280 {
6281 char mi_group[10];
6282
6283 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6284 ui_out_field_string (uiout, NULL, mi_group);
6285 }
6286 else
6287 {
6288 if (i == 0)
6289 ui_out_text (uiout, " inf ");
6290 else
6291 ui_out_text (uiout, ", ");
6292
6293 ui_out_text (uiout, plongest (inf));
6294 }
6295 }
6296
6297 do_cleanups (back_to);
6298}
6299
269b11a2
PA
6300/* Print B to gdb_stdout. */
6301
6302static void
6303print_one_breakpoint_location (struct breakpoint *b,
6304 struct bp_location *loc,
6305 int loc_number,
6306 struct bp_location **last_loc,
269b11a2
PA
6307 int allflag)
6308{
6309 struct command_line *l;
c2c6d25f 6310 static char bpenables[] = "nynny";
c906108c 6311
79a45e25 6312 struct ui_out *uiout = current_uiout;
0d381245
VP
6313 int header_of_multiple = 0;
6314 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6315 struct value_print_options opts;
6316
6317 get_user_print_options (&opts);
0d381245
VP
6318
6319 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6320 /* See comment in print_one_breakpoint concerning treatment of
6321 breakpoints with single disabled location. */
0d381245
VP
6322 if (loc == NULL
6323 && (b->loc != NULL
6324 && (b->loc->next != NULL || !b->loc->enabled)))
6325 header_of_multiple = 1;
6326 if (loc == NULL)
6327 loc = b->loc;
6328
c4093a6a
JM
6329 annotate_record ();
6330
6331 /* 1 */
6332 annotate_field (0);
0d381245
VP
6333 if (part_of_multiple)
6334 {
6335 char *formatted;
0c6773c1 6336 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6337 ui_out_field_string (uiout, "number", formatted);
6338 xfree (formatted);
6339 }
6340 else
6341 {
6342 ui_out_field_int (uiout, "number", b->number);
6343 }
c4093a6a
JM
6344
6345 /* 2 */
6346 annotate_field (1);
0d381245
VP
6347 if (part_of_multiple)
6348 ui_out_field_skip (uiout, "type");
269b11a2
PA
6349 else
6350 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6351
6352 /* 3 */
6353 annotate_field (2);
0d381245
VP
6354 if (part_of_multiple)
6355 ui_out_field_skip (uiout, "disp");
6356 else
2cec12e5 6357 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6358
c4093a6a
JM
6359
6360 /* 4 */
6361 annotate_field (3);
0d381245 6362 if (part_of_multiple)
54e52265 6363 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6364 else
4a64f543
MS
6365 ui_out_field_fmt (uiout, "enabled", "%c",
6366 bpenables[(int) b->enable_state]);
54e52265 6367 ui_out_spaces (uiout, 2);
0d381245 6368
c4093a6a
JM
6369
6370 /* 5 and 6 */
3086aeae 6371 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6372 {
4a64f543
MS
6373 /* Although the print_one can possibly print all locations,
6374 calling it here is not likely to get any nice result. So,
6375 make sure there's just one location. */
0d381245 6376 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6377 b->ops->print_one (b, last_loc);
0d381245 6378 }
3086aeae
DJ
6379 else
6380 switch (b->type)
6381 {
6382 case bp_none:
6383 internal_error (__FILE__, __LINE__,
e2e0b3e5 6384 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6385 break;
c906108c 6386
3086aeae
DJ
6387 case bp_watchpoint:
6388 case bp_hardware_watchpoint:
6389 case bp_read_watchpoint:
6390 case bp_access_watchpoint:
3a5c3e22
PA
6391 {
6392 struct watchpoint *w = (struct watchpoint *) b;
6393
6394 /* Field 4, the address, is omitted (which makes the columns
6395 not line up too nicely with the headers, but the effect
6396 is relatively readable). */
6397 if (opts.addressprint)
6398 ui_out_field_skip (uiout, "addr");
6399 annotate_field (5);
6400 ui_out_field_string (uiout, "what", w->exp_string);
6401 }
3086aeae
DJ
6402 break;
6403
3086aeae
DJ
6404 case bp_breakpoint:
6405 case bp_hardware_breakpoint:
7c16b83e 6406 case bp_single_step:
3086aeae
DJ
6407 case bp_until:
6408 case bp_finish:
6409 case bp_longjmp:
6410 case bp_longjmp_resume:
e2e4d78b 6411 case bp_longjmp_call_dummy:
186c406b
TT
6412 case bp_exception:
6413 case bp_exception_resume:
3086aeae 6414 case bp_step_resume:
2c03e5be 6415 case bp_hp_step_resume:
3086aeae
DJ
6416 case bp_watchpoint_scope:
6417 case bp_call_dummy:
aa7d318d 6418 case bp_std_terminate:
3086aeae
DJ
6419 case bp_shlib_event:
6420 case bp_thread_event:
6421 case bp_overlay_event:
0fd8e87f 6422 case bp_longjmp_master:
aa7d318d 6423 case bp_std_terminate_master:
186c406b 6424 case bp_exception_master:
1042e4c0 6425 case bp_tracepoint:
7a697b8d 6426 case bp_fast_tracepoint:
0fb4aa4b 6427 case bp_static_tracepoint:
e7e0cddf 6428 case bp_dprintf:
4efc6507 6429 case bp_jit_event:
0e30163f
JK
6430 case bp_gnu_ifunc_resolver:
6431 case bp_gnu_ifunc_resolver_return:
79a45b7d 6432 if (opts.addressprint)
3086aeae
DJ
6433 {
6434 annotate_field (4);
54e52265 6435 if (header_of_multiple)
0d381245 6436 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6437 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6438 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6439 else
5af949e3
UW
6440 ui_out_field_core_addr (uiout, "addr",
6441 loc->gdbarch, loc->address);
3086aeae
DJ
6442 }
6443 annotate_field (5);
0d381245 6444 if (!header_of_multiple)
170b53b2 6445 print_breakpoint_location (b, loc);
0d381245 6446 if (b->loc)
a6d9a66e 6447 *last_loc = b->loc;
3086aeae
DJ
6448 break;
6449 }
c906108c 6450
6c95b8df 6451
998580f1 6452 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6453 {
6454 struct inferior *inf;
998580f1
MK
6455 VEC(int) *inf_num = NULL;
6456 int mi_only = 1;
6c95b8df 6457
998580f1 6458 ALL_INFERIORS (inf)
6c95b8df
PA
6459 {
6460 if (inf->pspace == loc->pspace)
998580f1 6461 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6462 }
998580f1
MK
6463
6464 /* For backward compatibility, don't display inferiors in CLI unless
6465 there are several. Always display for MI. */
6466 if (allflag
6467 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6468 && (number_of_program_spaces () > 1
6469 || number_of_inferiors () > 1)
6470 /* LOC is for existing B, it cannot be in
6471 moribund_locations and thus having NULL OWNER. */
6472 && loc->owner->type != bp_catchpoint))
6473 mi_only = 0;
6474 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6475 VEC_free (int, inf_num);
6c95b8df
PA
6476 }
6477
4a306c9a 6478 if (!part_of_multiple)
c4093a6a 6479 {
4a306c9a
JB
6480 if (b->thread != -1)
6481 {
6482 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6483 "stop only in" line a little further down. */
4a306c9a
JB
6484 ui_out_text (uiout, " thread ");
6485 ui_out_field_int (uiout, "thread", b->thread);
6486 }
6487 else if (b->task != 0)
6488 {
6489 ui_out_text (uiout, " task ");
6490 ui_out_field_int (uiout, "task", b->task);
6491 }
c4093a6a 6492 }
f1310107 6493
8b93c638 6494 ui_out_text (uiout, "\n");
f1310107 6495
348d480f 6496 if (!part_of_multiple)
f1310107
TJB
6497 b->ops->print_one_detail (b, uiout);
6498
0d381245 6499 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6500 {
6501 annotate_field (6);
8b93c638 6502 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6503 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6504 the frame ID. */
5af949e3
UW
6505 ui_out_field_core_addr (uiout, "frame",
6506 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6507 ui_out_text (uiout, "\n");
c4093a6a
JM
6508 }
6509
28010a5d 6510 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6511 {
6512 annotate_field (7);
d77f58be 6513 if (is_tracepoint (b))
1042e4c0
SS
6514 ui_out_text (uiout, "\ttrace only if ");
6515 else
6516 ui_out_text (uiout, "\tstop only if ");
0101ce28 6517 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6518
6519 /* Print whether the target is doing the breakpoint's condition
6520 evaluation. If GDB is doing the evaluation, don't print anything. */
6521 if (is_breakpoint (b)
6522 && breakpoint_condition_evaluation_mode ()
6523 == condition_evaluation_target)
6524 {
6525 ui_out_text (uiout, " (");
6526 ui_out_field_string (uiout, "evaluated-by",
6527 bp_condition_evaluator (b));
6528 ui_out_text (uiout, " evals)");
6529 }
0101ce28
JJ
6530 ui_out_text (uiout, "\n");
6531 }
6532
0d381245 6533 if (!part_of_multiple && b->thread != -1)
c4093a6a 6534 {
4a64f543 6535 /* FIXME should make an annotation for this. */
8b93c638 6536 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6537 if (ui_out_is_mi_like_p (uiout))
6538 ui_out_field_int (uiout, "thread", b->thread);
6539 else
6540 {
6541 struct thread_info *thr = find_thread_global_id (b->thread);
6542
6543 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6544 }
8b93c638 6545 ui_out_text (uiout, "\n");
c4093a6a
JM
6546 }
6547
556ec64d
YQ
6548 if (!part_of_multiple)
6549 {
6550 if (b->hit_count)
31f56a27
YQ
6551 {
6552 /* FIXME should make an annotation for this. */
6553 if (is_catchpoint (b))
6554 ui_out_text (uiout, "\tcatchpoint");
6555 else if (is_tracepoint (b))
6556 ui_out_text (uiout, "\ttracepoint");
6557 else
6558 ui_out_text (uiout, "\tbreakpoint");
6559 ui_out_text (uiout, " already hit ");
6560 ui_out_field_int (uiout, "times", b->hit_count);
6561 if (b->hit_count == 1)
6562 ui_out_text (uiout, " time\n");
6563 else
6564 ui_out_text (uiout, " times\n");
6565 }
556ec64d
YQ
6566 else
6567 {
31f56a27
YQ
6568 /* Output the count also if it is zero, but only if this is mi. */
6569 if (ui_out_is_mi_like_p (uiout))
6570 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6571 }
6572 }
8b93c638 6573
0d381245 6574 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6575 {
6576 annotate_field (8);
8b93c638
JM
6577 ui_out_text (uiout, "\tignore next ");
6578 ui_out_field_int (uiout, "ignore", b->ignore_count);
6579 ui_out_text (uiout, " hits\n");
c4093a6a 6580 }
059fb39f 6581
816338b5
SS
6582 /* Note that an enable count of 1 corresponds to "enable once"
6583 behavior, which is reported by the combination of enablement and
6584 disposition, so we don't need to mention it here. */
6585 if (!part_of_multiple && b->enable_count > 1)
6586 {
6587 annotate_field (8);
6588 ui_out_text (uiout, "\tdisable after ");
6589 /* Tweak the wording to clarify that ignore and enable counts
6590 are distinct, and have additive effect. */
6591 if (b->ignore_count)
6592 ui_out_text (uiout, "additional ");
6593 else
6594 ui_out_text (uiout, "next ");
6595 ui_out_field_int (uiout, "enable", b->enable_count);
6596 ui_out_text (uiout, " hits\n");
6597 }
6598
f196051f
SS
6599 if (!part_of_multiple && is_tracepoint (b))
6600 {
6601 struct tracepoint *tp = (struct tracepoint *) b;
6602
6603 if (tp->traceframe_usage)
6604 {
6605 ui_out_text (uiout, "\ttrace buffer usage ");
6606 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6607 ui_out_text (uiout, " bytes\n");
6608 }
6609 }
d3ce09f5 6610
9add0f1b 6611 l = b->commands ? b->commands->commands : NULL;
059fb39f 6612 if (!part_of_multiple && l)
c4093a6a 6613 {
3b31d625
EZ
6614 struct cleanup *script_chain;
6615
c4093a6a 6616 annotate_field (9);
3b31d625 6617 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6618 print_command_lines (uiout, l, 4);
3b31d625 6619 do_cleanups (script_chain);
c4093a6a 6620 }
d24317b4 6621
d9b3f62e 6622 if (is_tracepoint (b))
1042e4c0 6623 {
d9b3f62e
PA
6624 struct tracepoint *t = (struct tracepoint *) b;
6625
6626 if (!part_of_multiple && t->pass_count)
6627 {
6628 annotate_field (10);
6629 ui_out_text (uiout, "\tpass count ");
6630 ui_out_field_int (uiout, "pass", t->pass_count);
6631 ui_out_text (uiout, " \n");
6632 }
f2a8bc8a
YQ
6633
6634 /* Don't display it when tracepoint or tracepoint location is
6635 pending. */
6636 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6637 {
6638 annotate_field (11);
6639
6640 if (ui_out_is_mi_like_p (uiout))
6641 ui_out_field_string (uiout, "installed",
6642 loc->inserted ? "y" : "n");
6643 else
6644 {
6645 if (loc->inserted)
6646 ui_out_text (uiout, "\t");
6647 else
6648 ui_out_text (uiout, "\tnot ");
6649 ui_out_text (uiout, "installed on target\n");
6650 }
6651 }
1042e4c0
SS
6652 }
6653
d24317b4
VP
6654 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6655 {
3a5c3e22
PA
6656 if (is_watchpoint (b))
6657 {
6658 struct watchpoint *w = (struct watchpoint *) b;
6659
6660 ui_out_field_string (uiout, "original-location", w->exp_string);
6661 }
f00aae0f
KS
6662 else if (b->location != NULL
6663 && event_location_to_string (b->location) != NULL)
6664 ui_out_field_string (uiout, "original-location",
6665 event_location_to_string (b->location));
d24317b4 6666 }
c4093a6a 6667}
c5aa993b 6668
0d381245
VP
6669static void
6670print_one_breakpoint (struct breakpoint *b,
4a64f543 6671 struct bp_location **last_loc,
6c95b8df 6672 int allflag)
0d381245 6673{
8d3788bd 6674 struct cleanup *bkpt_chain;
79a45e25 6675 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6676
6677 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6678
12c5a436 6679 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6680 do_cleanups (bkpt_chain);
0d381245
VP
6681
6682 /* If this breakpoint has custom print function,
6683 it's already printed. Otherwise, print individual
6684 locations, if any. */
6685 if (b->ops == NULL || b->ops->print_one == NULL)
6686 {
4a64f543
MS
6687 /* If breakpoint has a single location that is disabled, we
6688 print it as if it had several locations, since otherwise it's
6689 hard to represent "breakpoint enabled, location disabled"
6690 situation.
6691
6692 Note that while hardware watchpoints have several locations
a3be7890 6693 internally, that's not a property exposed to user. */
0d381245 6694 if (b->loc
a5606eee 6695 && !is_hardware_watchpoint (b)
8d3788bd 6696 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6697 {
6698 struct bp_location *loc;
6699 int n = 1;
8d3788bd 6700
0d381245 6701 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6702 {
6703 struct cleanup *inner2 =
6704 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6705 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6706 do_cleanups (inner2);
6707 }
0d381245
VP
6708 }
6709 }
6710}
6711
a6d9a66e
UW
6712static int
6713breakpoint_address_bits (struct breakpoint *b)
6714{
6715 int print_address_bits = 0;
6716 struct bp_location *loc;
6717
c6d81124
PA
6718 /* Software watchpoints that aren't watching memory don't have an
6719 address to print. */
6720 if (is_no_memory_software_watchpoint (b))
6721 return 0;
6722
a6d9a66e
UW
6723 for (loc = b->loc; loc; loc = loc->next)
6724 {
c7437ca6
PA
6725 int addr_bit;
6726
c7437ca6 6727 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6728 if (addr_bit > print_address_bits)
6729 print_address_bits = addr_bit;
6730 }
6731
6732 return print_address_bits;
6733}
0d381245 6734
c4093a6a
JM
6735struct captured_breakpoint_query_args
6736 {
6737 int bnum;
6738 };
c5aa993b 6739
c4093a6a 6740static int
2b65245e 6741do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6742{
9a3c8263
SM
6743 struct captured_breakpoint_query_args *args
6744 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6745 struct breakpoint *b;
a6d9a66e 6746 struct bp_location *dummy_loc = NULL;
cc59ec59 6747
c4093a6a
JM
6748 ALL_BREAKPOINTS (b)
6749 {
6750 if (args->bnum == b->number)
c5aa993b 6751 {
12c5a436 6752 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6753 return GDB_RC_OK;
c5aa993b 6754 }
c4093a6a
JM
6755 }
6756 return GDB_RC_NONE;
6757}
c5aa993b 6758
c4093a6a 6759enum gdb_rc
4a64f543
MS
6760gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6761 char **error_message)
c4093a6a
JM
6762{
6763 struct captured_breakpoint_query_args args;
cc59ec59 6764
c4093a6a
JM
6765 args.bnum = bnum;
6766 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6767 an error. */
b0b13bb4
DJ
6768 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6769 error_message, RETURN_MASK_ALL) < 0)
6770 return GDB_RC_FAIL;
6771 else
6772 return GDB_RC_OK;
c4093a6a 6773}
c5aa993b 6774
09d682a4
TT
6775/* Return true if this breakpoint was set by the user, false if it is
6776 internal or momentary. */
6777
6778int
6779user_breakpoint_p (struct breakpoint *b)
6780{
46c6471b 6781 return b->number > 0;
09d682a4
TT
6782}
6783
7f3b0473 6784/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6785 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6786 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6787 FILTER is non-NULL, call it on each breakpoint and only include the
6788 ones for which it returns non-zero. Return the total number of
6789 breakpoints listed. */
c906108c 6790
d77f58be 6791static int
e5a67952 6792breakpoint_1 (char *args, int allflag,
4a64f543 6793 int (*filter) (const struct breakpoint *))
c4093a6a 6794{
52f0bd74 6795 struct breakpoint *b;
a6d9a66e 6796 struct bp_location *last_loc = NULL;
7f3b0473 6797 int nr_printable_breakpoints;
3b31d625 6798 struct cleanup *bkpttbl_chain;
79a45b7d 6799 struct value_print_options opts;
a6d9a66e 6800 int print_address_bits = 0;
269b11a2 6801 int print_type_col_width = 14;
79a45e25 6802 struct ui_out *uiout = current_uiout;
269b11a2 6803
79a45b7d
TT
6804 get_user_print_options (&opts);
6805
4a64f543
MS
6806 /* Compute the number of rows in the table, as well as the size
6807 required for address fields. */
7f3b0473
AC
6808 nr_printable_breakpoints = 0;
6809 ALL_BREAKPOINTS (b)
e5a67952
MS
6810 {
6811 /* If we have a filter, only list the breakpoints it accepts. */
6812 if (filter && !filter (b))
6813 continue;
6814
6815 /* If we have an "args" string, it is a list of breakpoints to
6816 accept. Skip the others. */
6817 if (args != NULL && *args != '\0')
6818 {
6819 if (allflag && parse_and_eval_long (args) != b->number)
6820 continue;
6821 if (!allflag && !number_is_in_list (args, b->number))
6822 continue;
6823 }
269b11a2 6824
e5a67952
MS
6825 if (allflag || user_breakpoint_p (b))
6826 {
6827 int addr_bit, type_len;
a6d9a66e 6828
e5a67952
MS
6829 addr_bit = breakpoint_address_bits (b);
6830 if (addr_bit > print_address_bits)
6831 print_address_bits = addr_bit;
269b11a2 6832
e5a67952
MS
6833 type_len = strlen (bptype_string (b->type));
6834 if (type_len > print_type_col_width)
6835 print_type_col_width = type_len;
6836
6837 nr_printable_breakpoints++;
6838 }
6839 }
7f3b0473 6840
79a45b7d 6841 if (opts.addressprint)
3b31d625 6842 bkpttbl_chain
3e43a32a
MS
6843 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6844 nr_printable_breakpoints,
3b31d625 6845 "BreakpointTable");
8b93c638 6846 else
3b31d625 6847 bkpttbl_chain
3e43a32a
MS
6848 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6849 nr_printable_breakpoints,
3b31d625 6850 "BreakpointTable");
8b93c638 6851
7f3b0473 6852 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6853 annotate_breakpoints_headers ();
6854 if (nr_printable_breakpoints > 0)
6855 annotate_field (0);
4a64f543 6856 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6857 if (nr_printable_breakpoints > 0)
6858 annotate_field (1);
269b11a2 6859 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6860 "type", "Type"); /* 2 */
d7faa9e7
AC
6861 if (nr_printable_breakpoints > 0)
6862 annotate_field (2);
4a64f543 6863 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6864 if (nr_printable_breakpoints > 0)
6865 annotate_field (3);
54e52265 6866 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6867 if (opts.addressprint)
e5a67952
MS
6868 {
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (4);
6871 if (print_address_bits <= 32)
6872 ui_out_table_header (uiout, 10, ui_left,
6873 "addr", "Address"); /* 5 */
6874 else
6875 ui_out_table_header (uiout, 18, ui_left,
6876 "addr", "Address"); /* 5 */
6877 }
d7faa9e7
AC
6878 if (nr_printable_breakpoints > 0)
6879 annotate_field (5);
6880 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6881 ui_out_table_body (uiout);
6882 if (nr_printable_breakpoints > 0)
6883 annotate_breakpoints_table ();
7f3b0473 6884
c4093a6a 6885 ALL_BREAKPOINTS (b)
e5a67952
MS
6886 {
6887 QUIT;
6888 /* If we have a filter, only list the breakpoints it accepts. */
6889 if (filter && !filter (b))
6890 continue;
6891
6892 /* If we have an "args" string, it is a list of breakpoints to
6893 accept. Skip the others. */
6894
6895 if (args != NULL && *args != '\0')
6896 {
6897 if (allflag) /* maintenance info breakpoint */
6898 {
6899 if (parse_and_eval_long (args) != b->number)
6900 continue;
6901 }
6902 else /* all others */
6903 {
6904 if (!number_is_in_list (args, b->number))
6905 continue;
6906 }
6907 }
6908 /* We only print out user settable breakpoints unless the
6909 allflag is set. */
6910 if (allflag || user_breakpoint_p (b))
12c5a436 6911 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6912 }
6913
3b31d625 6914 do_cleanups (bkpttbl_chain);
698384cd 6915
7f3b0473 6916 if (nr_printable_breakpoints == 0)
c906108c 6917 {
4a64f543
MS
6918 /* If there's a filter, let the caller decide how to report
6919 empty list. */
d77f58be
SS
6920 if (!filter)
6921 {
e5a67952 6922 if (args == NULL || *args == '\0')
d77f58be
SS
6923 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6924 else
4a64f543 6925 ui_out_message (uiout, 0,
e5a67952
MS
6926 "No breakpoint or watchpoint matching '%s'.\n",
6927 args);
d77f58be 6928 }
c906108c
SS
6929 }
6930 else
c4093a6a 6931 {
a6d9a66e
UW
6932 if (last_loc && !server_command)
6933 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6934 }
c906108c 6935
4a64f543 6936 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6937 there have been breakpoints? */
c906108c 6938 annotate_breakpoints_table_end ();
d77f58be
SS
6939
6940 return nr_printable_breakpoints;
c906108c
SS
6941}
6942
ad443146
SS
6943/* Display the value of default-collect in a way that is generally
6944 compatible with the breakpoint list. */
6945
6946static void
6947default_collect_info (void)
6948{
79a45e25
PA
6949 struct ui_out *uiout = current_uiout;
6950
ad443146
SS
6951 /* If it has no value (which is frequently the case), say nothing; a
6952 message like "No default-collect." gets in user's face when it's
6953 not wanted. */
6954 if (!*default_collect)
6955 return;
6956
6957 /* The following phrase lines up nicely with per-tracepoint collect
6958 actions. */
6959 ui_out_text (uiout, "default collect ");
6960 ui_out_field_string (uiout, "default-collect", default_collect);
6961 ui_out_text (uiout, " \n");
6962}
6963
c906108c 6964static void
e5a67952 6965breakpoints_info (char *args, int from_tty)
c906108c 6966{
e5a67952 6967 breakpoint_1 (args, 0, NULL);
ad443146
SS
6968
6969 default_collect_info ();
d77f58be
SS
6970}
6971
6972static void
e5a67952 6973watchpoints_info (char *args, int from_tty)
d77f58be 6974{
e5a67952 6975 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6976 struct ui_out *uiout = current_uiout;
d77f58be
SS
6977
6978 if (num_printed == 0)
6979 {
e5a67952 6980 if (args == NULL || *args == '\0')
d77f58be
SS
6981 ui_out_message (uiout, 0, "No watchpoints.\n");
6982 else
e5a67952 6983 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6984 }
c906108c
SS
6985}
6986
7a292a7a 6987static void
e5a67952 6988maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6989{
e5a67952 6990 breakpoint_1 (args, 1, NULL);
ad443146
SS
6991
6992 default_collect_info ();
c906108c
SS
6993}
6994
0d381245 6995static int
714835d5 6996breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6997 struct program_space *pspace,
714835d5 6998 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6999{
7000 struct bp_location *bl = b->loc;
cc59ec59 7001
0d381245
VP
7002 for (; bl; bl = bl->next)
7003 {
6c95b8df
PA
7004 if (bl->pspace == pspace
7005 && bl->address == pc
0d381245
VP
7006 && (!overlay_debugging || bl->section == section))
7007 return 1;
7008 }
7009 return 0;
7010}
7011
672f9b60 7012/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7013 concerns with logical breakpoints, so we match program spaces, not
7014 address spaces. */
c906108c
SS
7015
7016static void
6c95b8df
PA
7017describe_other_breakpoints (struct gdbarch *gdbarch,
7018 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7019 struct obj_section *section, int thread)
c906108c 7020{
52f0bd74
AC
7021 int others = 0;
7022 struct breakpoint *b;
c906108c
SS
7023
7024 ALL_BREAKPOINTS (b)
672f9b60
KP
7025 others += (user_breakpoint_p (b)
7026 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7027 if (others > 0)
7028 {
a3f17187
AC
7029 if (others == 1)
7030 printf_filtered (_("Note: breakpoint "));
7031 else /* if (others == ???) */
7032 printf_filtered (_("Note: breakpoints "));
c906108c 7033 ALL_BREAKPOINTS (b)
672f9b60 7034 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7035 {
7036 others--;
7037 printf_filtered ("%d", b->number);
7038 if (b->thread == -1 && thread != -1)
7039 printf_filtered (" (all threads)");
7040 else if (b->thread != -1)
7041 printf_filtered (" (thread %d)", b->thread);
7042 printf_filtered ("%s%s ",
059fb39f 7043 ((b->enable_state == bp_disabled
f8eba3c6 7044 || b->enable_state == bp_call_disabled)
0d381245 7045 ? " (disabled)"
0d381245
VP
7046 : ""),
7047 (others > 1) ? ","
7048 : ((others == 1) ? " and" : ""));
7049 }
a3f17187 7050 printf_filtered (_("also set at pc "));
5af949e3 7051 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7052 printf_filtered (".\n");
7053 }
7054}
7055\f
c906108c 7056
e4f237da 7057/* Return true iff it is meaningful to use the address member of
244558af
LM
7058 BPT locations. For some breakpoint types, the locations' address members
7059 are irrelevant and it makes no sense to attempt to compare them to other
7060 addresses (or use them for any other purpose either).
e4f237da 7061
4a64f543 7062 More specifically, each of the following breakpoint types will
244558af 7063 always have a zero valued location address and we don't want to mark
4a64f543 7064 breakpoints of any of these types to be a duplicate of an actual
244558af 7065 breakpoint location at address zero:
e4f237da
KB
7066
7067 bp_watchpoint
2d134ed3
PA
7068 bp_catchpoint
7069
7070*/
e4f237da
KB
7071
7072static int
7073breakpoint_address_is_meaningful (struct breakpoint *bpt)
7074{
7075 enum bptype type = bpt->type;
7076
2d134ed3
PA
7077 return (type != bp_watchpoint && type != bp_catchpoint);
7078}
7079
7080/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7081 true if LOC1 and LOC2 represent the same watchpoint location. */
7082
7083static int
4a64f543
MS
7084watchpoint_locations_match (struct bp_location *loc1,
7085 struct bp_location *loc2)
2d134ed3 7086{
3a5c3e22
PA
7087 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7088 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7089
7090 /* Both of them must exist. */
7091 gdb_assert (w1 != NULL);
7092 gdb_assert (w2 != NULL);
2bdf28a0 7093
4a64f543
MS
7094 /* If the target can evaluate the condition expression in hardware,
7095 then we we need to insert both watchpoints even if they are at
7096 the same place. Otherwise the watchpoint will only trigger when
7097 the condition of whichever watchpoint was inserted evaluates to
7098 true, not giving a chance for GDB to check the condition of the
7099 other watchpoint. */
3a5c3e22 7100 if ((w1->cond_exp
4a64f543
MS
7101 && target_can_accel_watchpoint_condition (loc1->address,
7102 loc1->length,
0cf6dd15 7103 loc1->watchpoint_type,
3a5c3e22
PA
7104 w1->cond_exp))
7105 || (w2->cond_exp
4a64f543
MS
7106 && target_can_accel_watchpoint_condition (loc2->address,
7107 loc2->length,
0cf6dd15 7108 loc2->watchpoint_type,
3a5c3e22 7109 w2->cond_exp)))
0cf6dd15
TJB
7110 return 0;
7111
85d721b8
PA
7112 /* Note that this checks the owner's type, not the location's. In
7113 case the target does not support read watchpoints, but does
7114 support access watchpoints, we'll have bp_read_watchpoint
7115 watchpoints with hw_access locations. Those should be considered
7116 duplicates of hw_read locations. The hw_read locations will
7117 become hw_access locations later. */
2d134ed3
PA
7118 return (loc1->owner->type == loc2->owner->type
7119 && loc1->pspace->aspace == loc2->pspace->aspace
7120 && loc1->address == loc2->address
7121 && loc1->length == loc2->length);
e4f237da
KB
7122}
7123
31e77af2 7124/* See breakpoint.h. */
6c95b8df 7125
31e77af2 7126int
6c95b8df
PA
7127breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7128 struct address_space *aspace2, CORE_ADDR addr2)
7129{
f5656ead 7130 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7131 || aspace1 == aspace2)
7132 && addr1 == addr2);
7133}
7134
f1310107
TJB
7135/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7136 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7137 matches ASPACE2. On targets that have global breakpoints, the address
7138 space doesn't really matter. */
7139
7140static int
7141breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7142 int len1, struct address_space *aspace2,
7143 CORE_ADDR addr2)
7144{
f5656ead 7145 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7146 || aspace1 == aspace2)
7147 && addr2 >= addr1 && addr2 < addr1 + len1);
7148}
7149
7150/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7151 a ranged breakpoint. In most targets, a match happens only if ASPACE
7152 matches the breakpoint's address space. On targets that have global
7153 breakpoints, the address space doesn't really matter. */
7154
7155static int
7156breakpoint_location_address_match (struct bp_location *bl,
7157 struct address_space *aspace,
7158 CORE_ADDR addr)
7159{
7160 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7161 aspace, addr)
7162 || (bl->length
7163 && breakpoint_address_match_range (bl->pspace->aspace,
7164 bl->address, bl->length,
7165 aspace, addr)));
7166}
7167
d35ae833
PA
7168/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7169 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7170 match happens only if ASPACE matches the breakpoint's address
7171 space. On targets that have global breakpoints, the address space
7172 doesn't really matter. */
7173
7174static int
7175breakpoint_location_address_range_overlap (struct bp_location *bl,
7176 struct address_space *aspace,
7177 CORE_ADDR addr, int len)
7178{
7179 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7180 || bl->pspace->aspace == aspace)
7181 {
7182 int bl_len = bl->length != 0 ? bl->length : 1;
7183
7184 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7185 return 1;
7186 }
7187 return 0;
7188}
7189
1e4d1764
YQ
7190/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7191 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7192 true, otherwise returns false. */
7193
7194static int
7195tracepoint_locations_match (struct bp_location *loc1,
7196 struct bp_location *loc2)
7197{
7198 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7199 /* Since tracepoint locations are never duplicated with others', tracepoint
7200 locations at the same address of different tracepoints are regarded as
7201 different locations. */
7202 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7203 else
7204 return 0;
7205}
7206
2d134ed3
PA
7207/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7208 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7209 represent the same location. */
7210
7211static int
4a64f543
MS
7212breakpoint_locations_match (struct bp_location *loc1,
7213 struct bp_location *loc2)
2d134ed3 7214{
2bdf28a0
JK
7215 int hw_point1, hw_point2;
7216
7217 /* Both of them must not be in moribund_locations. */
7218 gdb_assert (loc1->owner != NULL);
7219 gdb_assert (loc2->owner != NULL);
7220
7221 hw_point1 = is_hardware_watchpoint (loc1->owner);
7222 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7223
7224 if (hw_point1 != hw_point2)
7225 return 0;
7226 else if (hw_point1)
7227 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7228 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7229 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7230 else
f1310107
TJB
7231 /* We compare bp_location.length in order to cover ranged breakpoints. */
7232 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7233 loc2->pspace->aspace, loc2->address)
7234 && loc1->length == loc2->length);
2d134ed3
PA
7235}
7236
76897487
KB
7237static void
7238breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7239 int bnum, int have_bnum)
7240{
f63fbe86
MS
7241 /* The longest string possibly returned by hex_string_custom
7242 is 50 chars. These must be at least that big for safety. */
7243 char astr1[64];
7244 char astr2[64];
76897487 7245
bb599908
PH
7246 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7247 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7248 if (have_bnum)
8a3fe4f8 7249 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7250 bnum, astr1, astr2);
7251 else
8a3fe4f8 7252 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7253}
7254
4a64f543
MS
7255/* Adjust a breakpoint's address to account for architectural
7256 constraints on breakpoint placement. Return the adjusted address.
7257 Note: Very few targets require this kind of adjustment. For most
7258 targets, this function is simply the identity function. */
76897487
KB
7259
7260static CORE_ADDR
a6d9a66e
UW
7261adjust_breakpoint_address (struct gdbarch *gdbarch,
7262 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7263{
a6d9a66e 7264 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7265 {
7266 /* Very few targets need any kind of breakpoint adjustment. */
7267 return bpaddr;
7268 }
88f7da05
KB
7269 else if (bptype == bp_watchpoint
7270 || bptype == bp_hardware_watchpoint
7271 || bptype == bp_read_watchpoint
7272 || bptype == bp_access_watchpoint
fe798b75 7273 || bptype == bp_catchpoint)
88f7da05
KB
7274 {
7275 /* Watchpoints and the various bp_catch_* eventpoints should not
7276 have their addresses modified. */
7277 return bpaddr;
7278 }
7c16b83e
PA
7279 else if (bptype == bp_single_step)
7280 {
7281 /* Single-step breakpoints should not have their addresses
7282 modified. If there's any architectural constrain that
7283 applies to this address, then it should have already been
7284 taken into account when the breakpoint was created in the
7285 first place. If we didn't do this, stepping through e.g.,
7286 Thumb-2 IT blocks would break. */
7287 return bpaddr;
7288 }
76897487
KB
7289 else
7290 {
7291 CORE_ADDR adjusted_bpaddr;
7292
7293 /* Some targets have architectural constraints on the placement
7294 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7295 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7296
7297 /* An adjusted breakpoint address can significantly alter
7298 a user's expectations. Print a warning if an adjustment
7299 is required. */
7300 if (adjusted_bpaddr != bpaddr)
7301 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7302
7303 return adjusted_bpaddr;
7304 }
7305}
7306
28010a5d
PA
7307void
7308init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7309 struct breakpoint *owner)
7cc221ef 7310{
7cc221ef
DJ
7311 memset (loc, 0, sizeof (*loc));
7312
348d480f
PA
7313 gdb_assert (ops != NULL);
7314
28010a5d
PA
7315 loc->ops = ops;
7316 loc->owner = owner;
511a6cd4 7317 loc->cond = NULL;
b775012e 7318 loc->cond_bytecode = NULL;
0d381245
VP
7319 loc->shlib_disabled = 0;
7320 loc->enabled = 1;
e049a4b5 7321
28010a5d 7322 switch (owner->type)
e049a4b5
DJ
7323 {
7324 case bp_breakpoint:
7c16b83e 7325 case bp_single_step:
e049a4b5
DJ
7326 case bp_until:
7327 case bp_finish:
7328 case bp_longjmp:
7329 case bp_longjmp_resume:
e2e4d78b 7330 case bp_longjmp_call_dummy:
186c406b
TT
7331 case bp_exception:
7332 case bp_exception_resume:
e049a4b5 7333 case bp_step_resume:
2c03e5be 7334 case bp_hp_step_resume:
e049a4b5
DJ
7335 case bp_watchpoint_scope:
7336 case bp_call_dummy:
aa7d318d 7337 case bp_std_terminate:
e049a4b5
DJ
7338 case bp_shlib_event:
7339 case bp_thread_event:
7340 case bp_overlay_event:
4efc6507 7341 case bp_jit_event:
0fd8e87f 7342 case bp_longjmp_master:
aa7d318d 7343 case bp_std_terminate_master:
186c406b 7344 case bp_exception_master:
0e30163f
JK
7345 case bp_gnu_ifunc_resolver:
7346 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7347 case bp_dprintf:
e049a4b5 7348 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7349 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7350 break;
7351 case bp_hardware_breakpoint:
7352 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7353 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7354 break;
7355 case bp_hardware_watchpoint:
7356 case bp_read_watchpoint:
7357 case bp_access_watchpoint:
7358 loc->loc_type = bp_loc_hardware_watchpoint;
7359 break;
7360 case bp_watchpoint:
ce78b96d 7361 case bp_catchpoint:
15c3d785
PA
7362 case bp_tracepoint:
7363 case bp_fast_tracepoint:
0fb4aa4b 7364 case bp_static_tracepoint:
e049a4b5
DJ
7365 loc->loc_type = bp_loc_other;
7366 break;
7367 default:
e2e0b3e5 7368 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7369 }
7370
f431efe5 7371 loc->refc = 1;
28010a5d
PA
7372}
7373
7374/* Allocate a struct bp_location. */
7375
7376static struct bp_location *
7377allocate_bp_location (struct breakpoint *bpt)
7378{
348d480f
PA
7379 return bpt->ops->allocate_location (bpt);
7380}
7cc221ef 7381
f431efe5
PA
7382static void
7383free_bp_location (struct bp_location *loc)
fe3f5fa8 7384{
348d480f 7385 loc->ops->dtor (loc);
fe3f5fa8
VP
7386 xfree (loc);
7387}
7388
f431efe5
PA
7389/* Increment reference count. */
7390
7391static void
7392incref_bp_location (struct bp_location *bl)
7393{
7394 ++bl->refc;
7395}
7396
7397/* Decrement reference count. If the reference count reaches 0,
7398 destroy the bp_location. Sets *BLP to NULL. */
7399
7400static void
7401decref_bp_location (struct bp_location **blp)
7402{
0807b50c
PA
7403 gdb_assert ((*blp)->refc > 0);
7404
f431efe5
PA
7405 if (--(*blp)->refc == 0)
7406 free_bp_location (*blp);
7407 *blp = NULL;
7408}
7409
346774a9 7410/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7411
346774a9
PA
7412static void
7413add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7414{
346774a9 7415 struct breakpoint *b1;
c906108c 7416
346774a9
PA
7417 /* Add this breakpoint to the end of the chain so that a list of
7418 breakpoints will come out in order of increasing numbers. */
7419
7420 b1 = breakpoint_chain;
7421 if (b1 == 0)
7422 breakpoint_chain = b;
7423 else
7424 {
7425 while (b1->next)
7426 b1 = b1->next;
7427 b1->next = b;
7428 }
7429}
7430
7431/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7432
7433static void
7434init_raw_breakpoint_without_location (struct breakpoint *b,
7435 struct gdbarch *gdbarch,
28010a5d 7436 enum bptype bptype,
c0a91b2b 7437 const struct breakpoint_ops *ops)
346774a9 7438{
c906108c 7439 memset (b, 0, sizeof (*b));
2219d63c 7440
348d480f
PA
7441 gdb_assert (ops != NULL);
7442
28010a5d 7443 b->ops = ops;
4d28f7a8 7444 b->type = bptype;
a6d9a66e 7445 b->gdbarch = gdbarch;
c906108c
SS
7446 b->language = current_language->la_language;
7447 b->input_radix = input_radix;
7448 b->thread = -1;
b5de0fa7 7449 b->enable_state = bp_enabled;
c906108c
SS
7450 b->next = 0;
7451 b->silent = 0;
7452 b->ignore_count = 0;
7453 b->commands = NULL;
818dd999 7454 b->frame_id = null_frame_id;
0d381245 7455 b->condition_not_parsed = 0;
84f4c1fe 7456 b->py_bp_object = NULL;
d0fb5eae 7457 b->related_breakpoint = b;
f00aae0f 7458 b->location = NULL;
346774a9
PA
7459}
7460
7461/* Helper to set_raw_breakpoint below. Creates a breakpoint
7462 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7463
7464static struct breakpoint *
7465set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7466 enum bptype bptype,
c0a91b2b 7467 const struct breakpoint_ops *ops)
346774a9
PA
7468{
7469 struct breakpoint *b = XNEW (struct breakpoint);
7470
348d480f 7471 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7472 add_to_breakpoint_chain (b);
0d381245
VP
7473 return b;
7474}
7475
0e30163f
JK
7476/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7477 resolutions should be made as the user specified the location explicitly
7478 enough. */
7479
0d381245 7480static void
0e30163f 7481set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7482{
2bdf28a0
JK
7483 gdb_assert (loc->owner != NULL);
7484
0d381245 7485 if (loc->owner->type == bp_breakpoint
1042e4c0 7486 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7487 || is_tracepoint (loc->owner))
0d381245 7488 {
0e30163f 7489 int is_gnu_ifunc;
2c02bd72 7490 const char *function_name;
6a3a010b 7491 CORE_ADDR func_addr;
0e30163f 7492
2c02bd72 7493 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7494 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7495
7496 if (is_gnu_ifunc && !explicit_loc)
7497 {
7498 struct breakpoint *b = loc->owner;
7499
7500 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7501 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7502 &loc->requested_address))
7503 {
7504 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7505 loc->address = adjust_breakpoint_address (loc->gdbarch,
7506 loc->requested_address,
7507 b->type);
7508 }
7509 else if (b->type == bp_breakpoint && b->loc == loc
7510 && loc->next == NULL && b->related_breakpoint == b)
7511 {
7512 /* Create only the whole new breakpoint of this type but do not
7513 mess more complicated breakpoints with multiple locations. */
7514 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7515 /* Remember the resolver's address for use by the return
7516 breakpoint. */
7517 loc->related_address = func_addr;
0e30163f
JK
7518 }
7519 }
7520
2c02bd72
DE
7521 if (function_name)
7522 loc->function_name = xstrdup (function_name);
0d381245
VP
7523 }
7524}
7525
a6d9a66e 7526/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7527struct gdbarch *
a6d9a66e
UW
7528get_sal_arch (struct symtab_and_line sal)
7529{
7530 if (sal.section)
7531 return get_objfile_arch (sal.section->objfile);
7532 if (sal.symtab)
eb822aa6 7533 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7534
7535 return NULL;
7536}
7537
346774a9
PA
7538/* Low level routine for partially initializing a breakpoint of type
7539 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7540 file name, and line number are provided by SAL.
0d381245
VP
7541
7542 It is expected that the caller will complete the initialization of
7543 the newly created breakpoint struct as well as output any status
c56053d2 7544 information regarding the creation of a new breakpoint. */
0d381245 7545
346774a9
PA
7546static void
7547init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7548 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7549 const struct breakpoint_ops *ops)
0d381245 7550{
28010a5d 7551 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7552
3742cc8b 7553 add_location_to_breakpoint (b, &sal);
0d381245 7554
6c95b8df
PA
7555 if (bptype != bp_catchpoint)
7556 gdb_assert (sal.pspace != NULL);
7557
f8eba3c6
TT
7558 /* Store the program space that was used to set the breakpoint,
7559 except for ordinary breakpoints, which are independent of the
7560 program space. */
7561 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7562 b->pspace = sal.pspace;
346774a9 7563}
c906108c 7564
346774a9
PA
7565/* set_raw_breakpoint is a low level routine for allocating and
7566 partially initializing a breakpoint of type BPTYPE. The newly
7567 created breakpoint's address, section, source file name, and line
7568 number are provided by SAL. The newly created and partially
7569 initialized breakpoint is added to the breakpoint chain and
7570 is also returned as the value of this function.
7571
7572 It is expected that the caller will complete the initialization of
7573 the newly created breakpoint struct as well as output any status
7574 information regarding the creation of a new breakpoint. In
7575 particular, set_raw_breakpoint does NOT set the breakpoint
7576 number! Care should be taken to not allow an error to occur
7577 prior to completing the initialization of the breakpoint. If this
7578 should happen, a bogus breakpoint will be left on the chain. */
7579
7580struct breakpoint *
7581set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7582 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7583 const struct breakpoint_ops *ops)
346774a9
PA
7584{
7585 struct breakpoint *b = XNEW (struct breakpoint);
7586
348d480f 7587 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7588 add_to_breakpoint_chain (b);
c906108c
SS
7589 return b;
7590}
7591
53a5351d 7592/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7593 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7594 initiated the operation. */
c906108c
SS
7595
7596void
186c406b 7597set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7598{
35df4500 7599 struct breakpoint *b, *b_tmp;
5d5658a1 7600 int thread = tp->global_num;
0fd8e87f
UW
7601
7602 /* To avoid having to rescan all objfile symbols at every step,
7603 we maintain a list of continually-inserted but always disabled
7604 longjmp "master" breakpoints. Here, we simply create momentary
7605 clones of those and enable them for the requested thread. */
35df4500 7606 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7607 if (b->pspace == current_program_space
186c406b
TT
7608 && (b->type == bp_longjmp_master
7609 || b->type == bp_exception_master))
0fd8e87f 7610 {
06edf0c0
PA
7611 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7612 struct breakpoint *clone;
cc59ec59 7613
e2e4d78b
JK
7614 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7615 after their removal. */
06edf0c0 7616 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7617 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7618 clone->thread = thread;
7619 }
186c406b
TT
7620
7621 tp->initiating_frame = frame;
c906108c
SS
7622}
7623
611c83ae 7624/* Delete all longjmp breakpoints from THREAD. */
c906108c 7625void
611c83ae 7626delete_longjmp_breakpoint (int thread)
c906108c 7627{
35df4500 7628 struct breakpoint *b, *b_tmp;
c906108c 7629
35df4500 7630 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7631 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7632 {
7633 if (b->thread == thread)
7634 delete_breakpoint (b);
7635 }
c906108c
SS
7636}
7637
f59f708a
PA
7638void
7639delete_longjmp_breakpoint_at_next_stop (int thread)
7640{
7641 struct breakpoint *b, *b_tmp;
7642
7643 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7644 if (b->type == bp_longjmp || b->type == bp_exception)
7645 {
7646 if (b->thread == thread)
7647 b->disposition = disp_del_at_next_stop;
7648 }
7649}
7650
e2e4d78b
JK
7651/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7652 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7653 pointer to any of them. Return NULL if this system cannot place longjmp
7654 breakpoints. */
7655
7656struct breakpoint *
7657set_longjmp_breakpoint_for_call_dummy (void)
7658{
7659 struct breakpoint *b, *retval = NULL;
7660
7661 ALL_BREAKPOINTS (b)
7662 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7663 {
7664 struct breakpoint *new_b;
7665
7666 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7667 &momentary_breakpoint_ops,
7668 1);
5d5658a1 7669 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7670
7671 /* Link NEW_B into the chain of RETVAL breakpoints. */
7672
7673 gdb_assert (new_b->related_breakpoint == new_b);
7674 if (retval == NULL)
7675 retval = new_b;
7676 new_b->related_breakpoint = retval;
7677 while (retval->related_breakpoint != new_b->related_breakpoint)
7678 retval = retval->related_breakpoint;
7679 retval->related_breakpoint = new_b;
7680 }
7681
7682 return retval;
7683}
7684
7685/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7686 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7687 stack.
7688
7689 You should call this function only at places where it is safe to currently
7690 unwind the whole stack. Failed stack unwind would discard live dummy
7691 frames. */
7692
7693void
b67a2c6f 7694check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7695{
7696 struct breakpoint *b, *b_tmp;
7697
7698 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7699 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7700 {
7701 struct breakpoint *dummy_b = b->related_breakpoint;
7702
7703 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7704 dummy_b = dummy_b->related_breakpoint;
7705 if (dummy_b->type != bp_call_dummy
7706 || frame_find_by_id (dummy_b->frame_id) != NULL)
7707 continue;
7708
b67a2c6f 7709 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7710
7711 while (b->related_breakpoint != b)
7712 {
7713 if (b_tmp == b->related_breakpoint)
7714 b_tmp = b->related_breakpoint->next;
7715 delete_breakpoint (b->related_breakpoint);
7716 }
7717 delete_breakpoint (b);
7718 }
7719}
7720
1900040c
MS
7721void
7722enable_overlay_breakpoints (void)
7723{
52f0bd74 7724 struct breakpoint *b;
1900040c
MS
7725
7726 ALL_BREAKPOINTS (b)
7727 if (b->type == bp_overlay_event)
7728 {
7729 b->enable_state = bp_enabled;
44702360 7730 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7731 overlay_events_enabled = 1;
1900040c
MS
7732 }
7733}
7734
7735void
7736disable_overlay_breakpoints (void)
7737{
52f0bd74 7738 struct breakpoint *b;
1900040c
MS
7739
7740 ALL_BREAKPOINTS (b)
7741 if (b->type == bp_overlay_event)
7742 {
7743 b->enable_state = bp_disabled;
44702360 7744 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7745 overlay_events_enabled = 0;
1900040c
MS
7746 }
7747}
7748
aa7d318d
TT
7749/* Set an active std::terminate breakpoint for each std::terminate
7750 master breakpoint. */
7751void
7752set_std_terminate_breakpoint (void)
7753{
35df4500 7754 struct breakpoint *b, *b_tmp;
aa7d318d 7755
35df4500 7756 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7757 if (b->pspace == current_program_space
7758 && b->type == bp_std_terminate_master)
7759 {
06edf0c0 7760 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7761 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7762 }
7763}
7764
7765/* Delete all the std::terminate breakpoints. */
7766void
7767delete_std_terminate_breakpoint (void)
7768{
35df4500 7769 struct breakpoint *b, *b_tmp;
aa7d318d 7770
35df4500 7771 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7772 if (b->type == bp_std_terminate)
7773 delete_breakpoint (b);
7774}
7775
c4093a6a 7776struct breakpoint *
a6d9a66e 7777create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7778{
7779 struct breakpoint *b;
c4093a6a 7780
06edf0c0
PA
7781 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7782 &internal_breakpoint_ops);
7783
b5de0fa7 7784 b->enable_state = bp_enabled;
f00aae0f 7785 /* location has to be used or breakpoint_re_set will delete me. */
305e13e6 7786 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7787
44702360 7788 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7789
c4093a6a
JM
7790 return b;
7791}
7792
0101ce28
JJ
7793struct lang_and_radix
7794 {
7795 enum language lang;
7796 int radix;
7797 };
7798
4efc6507
DE
7799/* Create a breakpoint for JIT code registration and unregistration. */
7800
7801struct breakpoint *
7802create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7803{
2a7f3dff
PA
7804 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7805 &internal_breakpoint_ops);
4efc6507 7806}
0101ce28 7807
03673fc7
PP
7808/* Remove JIT code registration and unregistration breakpoint(s). */
7809
7810void
7811remove_jit_event_breakpoints (void)
7812{
7813 struct breakpoint *b, *b_tmp;
7814
7815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7816 if (b->type == bp_jit_event
7817 && b->loc->pspace == current_program_space)
7818 delete_breakpoint (b);
7819}
7820
cae688ec
JJ
7821void
7822remove_solib_event_breakpoints (void)
7823{
35df4500 7824 struct breakpoint *b, *b_tmp;
cae688ec 7825
35df4500 7826 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7827 if (b->type == bp_shlib_event
7828 && b->loc->pspace == current_program_space)
cae688ec
JJ
7829 delete_breakpoint (b);
7830}
7831
f37f681c
PA
7832/* See breakpoint.h. */
7833
7834void
7835remove_solib_event_breakpoints_at_next_stop (void)
7836{
7837 struct breakpoint *b, *b_tmp;
7838
7839 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7840 if (b->type == bp_shlib_event
7841 && b->loc->pspace == current_program_space)
7842 b->disposition = disp_del_at_next_stop;
7843}
7844
04086b45
PA
7845/* Helper for create_solib_event_breakpoint /
7846 create_and_insert_solib_event_breakpoint. Allows specifying which
7847 INSERT_MODE to pass through to update_global_location_list. */
7848
7849static struct breakpoint *
7850create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7851 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7852{
7853 struct breakpoint *b;
7854
06edf0c0
PA
7855 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7856 &internal_breakpoint_ops);
04086b45 7857 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7858 return b;
7859}
7860
04086b45
PA
7861struct breakpoint *
7862create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7863{
7864 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7865}
7866
f37f681c
PA
7867/* See breakpoint.h. */
7868
7869struct breakpoint *
7870create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7871{
7872 struct breakpoint *b;
7873
04086b45
PA
7874 /* Explicitly tell update_global_location_list to insert
7875 locations. */
7876 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7877 if (!b->loc->inserted)
7878 {
7879 delete_breakpoint (b);
7880 return NULL;
7881 }
7882 return b;
7883}
7884
cae688ec
JJ
7885/* Disable any breakpoints that are on code in shared libraries. Only
7886 apply to enabled breakpoints, disabled ones can just stay disabled. */
7887
7888void
cb851954 7889disable_breakpoints_in_shlibs (void)
cae688ec 7890{
876fa593 7891 struct bp_location *loc, **locp_tmp;
cae688ec 7892
876fa593 7893 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7894 {
2bdf28a0 7895 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7896 struct breakpoint *b = loc->owner;
2bdf28a0 7897
4a64f543
MS
7898 /* We apply the check to all breakpoints, including disabled for
7899 those with loc->duplicate set. This is so that when breakpoint
7900 becomes enabled, or the duplicate is removed, gdb will try to
7901 insert all breakpoints. If we don't set shlib_disabled here,
7902 we'll try to insert those breakpoints and fail. */
1042e4c0 7903 if (((b->type == bp_breakpoint)
508ccb1f 7904 || (b->type == bp_jit_event)
1042e4c0 7905 || (b->type == bp_hardware_breakpoint)
d77f58be 7906 || (is_tracepoint (b)))
6c95b8df 7907 && loc->pspace == current_program_space
0d381245 7908 && !loc->shlib_disabled
6c95b8df 7909 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7910 )
0d381245
VP
7911 {
7912 loc->shlib_disabled = 1;
7913 }
cae688ec
JJ
7914 }
7915}
7916
63644780
NB
7917/* Disable any breakpoints and tracepoints that are in SOLIB upon
7918 notification of unloaded_shlib. Only apply to enabled breakpoints,
7919 disabled ones can just stay disabled. */
84acb35a 7920
75149521 7921static void
84acb35a
JJ
7922disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7923{
876fa593 7924 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7925 int disabled_shlib_breaks = 0;
7926
c86cf029
VP
7927 /* SunOS a.out shared libraries are always mapped, so do not
7928 disable breakpoints; they will only be reported as unloaded
7929 through clear_solib when GDB discards its shared library
7930 list. See clear_solib for more information. */
7931 if (exec_bfd != NULL
7932 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7933 return;
7934
876fa593 7935 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7936 {
2bdf28a0 7937 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7938 struct breakpoint *b = loc->owner;
cc59ec59 7939
1e4d1764 7940 if (solib->pspace == loc->pspace
e2dd7057 7941 && !loc->shlib_disabled
1e4d1764
YQ
7942 && (((b->type == bp_breakpoint
7943 || b->type == bp_jit_event
7944 || b->type == bp_hardware_breakpoint)
7945 && (loc->loc_type == bp_loc_hardware_breakpoint
7946 || loc->loc_type == bp_loc_software_breakpoint))
7947 || is_tracepoint (b))
e2dd7057 7948 && solib_contains_address_p (solib, loc->address))
84acb35a 7949 {
e2dd7057
PP
7950 loc->shlib_disabled = 1;
7951 /* At this point, we cannot rely on remove_breakpoint
7952 succeeding so we must mark the breakpoint as not inserted
7953 to prevent future errors occurring in remove_breakpoints. */
7954 loc->inserted = 0;
8d3788bd
VP
7955
7956 /* This may cause duplicate notifications for the same breakpoint. */
7957 observer_notify_breakpoint_modified (b);
7958
e2dd7057
PP
7959 if (!disabled_shlib_breaks)
7960 {
7961 target_terminal_ours_for_output ();
3e43a32a
MS
7962 warning (_("Temporarily disabling breakpoints "
7963 "for unloaded shared library \"%s\""),
e2dd7057 7964 solib->so_name);
84acb35a 7965 }
e2dd7057 7966 disabled_shlib_breaks = 1;
84acb35a
JJ
7967 }
7968 }
84acb35a
JJ
7969}
7970
63644780
NB
7971/* Disable any breakpoints and tracepoints in OBJFILE upon
7972 notification of free_objfile. Only apply to enabled breakpoints,
7973 disabled ones can just stay disabled. */
7974
7975static void
7976disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7977{
7978 struct breakpoint *b;
7979
7980 if (objfile == NULL)
7981 return;
7982
d03de421
PA
7983 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7984 managed by the user with add-symbol-file/remove-symbol-file.
7985 Similarly to how breakpoints in shared libraries are handled in
7986 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7987 shlib_disabled so they end up uninserted on the next global
7988 location list update. Shared libraries not loaded by the user
7989 aren't handled here -- they're already handled in
7990 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7991 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7992 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7993 main objfile). */
7994 if ((objfile->flags & OBJF_SHARED) == 0
7995 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7996 return;
7997
7998 ALL_BREAKPOINTS (b)
7999 {
8000 struct bp_location *loc;
8001 int bp_modified = 0;
8002
8003 if (!is_breakpoint (b) && !is_tracepoint (b))
8004 continue;
8005
8006 for (loc = b->loc; loc != NULL; loc = loc->next)
8007 {
8008 CORE_ADDR loc_addr = loc->address;
8009
8010 if (loc->loc_type != bp_loc_hardware_breakpoint
8011 && loc->loc_type != bp_loc_software_breakpoint)
8012 continue;
8013
8014 if (loc->shlib_disabled != 0)
8015 continue;
8016
8017 if (objfile->pspace != loc->pspace)
8018 continue;
8019
8020 if (loc->loc_type != bp_loc_hardware_breakpoint
8021 && loc->loc_type != bp_loc_software_breakpoint)
8022 continue;
8023
8024 if (is_addr_in_objfile (loc_addr, objfile))
8025 {
8026 loc->shlib_disabled = 1;
08351840
PA
8027 /* At this point, we don't know whether the object was
8028 unmapped from the inferior or not, so leave the
8029 inserted flag alone. We'll handle failure to
8030 uninsert quietly, in case the object was indeed
8031 unmapped. */
63644780
NB
8032
8033 mark_breakpoint_location_modified (loc);
8034
8035 bp_modified = 1;
8036 }
8037 }
8038
8039 if (bp_modified)
8040 observer_notify_breakpoint_modified (b);
8041 }
8042}
8043
ce78b96d
JB
8044/* FORK & VFORK catchpoints. */
8045
e29a4733
PA
8046/* An instance of this type is used to represent a fork or vfork
8047 catchpoint. It includes a "struct breakpoint" as a kind of base
8048 class; users downcast to "struct breakpoint *" when needed. A
8049 breakpoint is really of this type iff its ops pointer points to
8050 CATCH_FORK_BREAKPOINT_OPS. */
8051
8052struct fork_catchpoint
8053{
8054 /* The base class. */
8055 struct breakpoint base;
8056
8057 /* Process id of a child process whose forking triggered this
8058 catchpoint. This field is only valid immediately after this
8059 catchpoint has triggered. */
8060 ptid_t forked_inferior_pid;
8061};
8062
4a64f543
MS
8063/* Implement the "insert" breakpoint_ops method for fork
8064 catchpoints. */
ce78b96d 8065
77b06cd7
TJB
8066static int
8067insert_catch_fork (struct bp_location *bl)
ce78b96d 8068{
dfd4cc63 8069 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8070}
8071
4a64f543
MS
8072/* Implement the "remove" breakpoint_ops method for fork
8073 catchpoints. */
ce78b96d
JB
8074
8075static int
77b06cd7 8076remove_catch_fork (struct bp_location *bl)
ce78b96d 8077{
dfd4cc63 8078 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8079}
8080
8081/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8082 catchpoints. */
8083
8084static int
f1310107 8085breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8086 struct address_space *aspace, CORE_ADDR bp_addr,
8087 const struct target_waitstatus *ws)
ce78b96d 8088{
e29a4733
PA
8089 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8090
f90263c1
TT
8091 if (ws->kind != TARGET_WAITKIND_FORKED)
8092 return 0;
8093
8094 c->forked_inferior_pid = ws->value.related_pid;
8095 return 1;
ce78b96d
JB
8096}
8097
4a64f543
MS
8098/* Implement the "print_it" breakpoint_ops method for fork
8099 catchpoints. */
ce78b96d
JB
8100
8101static enum print_stop_action
348d480f 8102print_it_catch_fork (bpstat bs)
ce78b96d 8103{
36dfb11c 8104 struct ui_out *uiout = current_uiout;
348d480f
PA
8105 struct breakpoint *b = bs->breakpoint_at;
8106 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8107
ce78b96d 8108 annotate_catchpoint (b->number);
f303dbd6 8109 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8110 if (b->disposition == disp_del)
f303dbd6 8111 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8112 else
f303dbd6 8113 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8114 if (ui_out_is_mi_like_p (uiout))
8115 {
8116 ui_out_field_string (uiout, "reason",
8117 async_reason_lookup (EXEC_ASYNC_FORK));
8118 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8119 }
8120 ui_out_field_int (uiout, "bkptno", b->number);
8121 ui_out_text (uiout, " (forked process ");
8122 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8123 ui_out_text (uiout, "), ");
ce78b96d
JB
8124 return PRINT_SRC_AND_LOC;
8125}
8126
4a64f543
MS
8127/* Implement the "print_one" breakpoint_ops method for fork
8128 catchpoints. */
ce78b96d
JB
8129
8130static void
a6d9a66e 8131print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8132{
e29a4733 8133 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8134 struct value_print_options opts;
79a45e25 8135 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8136
8137 get_user_print_options (&opts);
8138
4a64f543
MS
8139 /* Field 4, the address, is omitted (which makes the columns not
8140 line up too nicely with the headers, but the effect is relatively
8141 readable). */
79a45b7d 8142 if (opts.addressprint)
ce78b96d
JB
8143 ui_out_field_skip (uiout, "addr");
8144 annotate_field (5);
8145 ui_out_text (uiout, "fork");
e29a4733 8146 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8147 {
8148 ui_out_text (uiout, ", process ");
8149 ui_out_field_int (uiout, "what",
e29a4733 8150 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8151 ui_out_spaces (uiout, 1);
8152 }
8ac3646f
TT
8153
8154 if (ui_out_is_mi_like_p (uiout))
8155 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8156}
8157
8158/* Implement the "print_mention" breakpoint_ops method for fork
8159 catchpoints. */
8160
8161static void
8162print_mention_catch_fork (struct breakpoint *b)
8163{
8164 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8165}
8166
6149aea9
PA
8167/* Implement the "print_recreate" breakpoint_ops method for fork
8168 catchpoints. */
8169
8170static void
8171print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8172{
8173 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8174 print_recreate_thread (b, fp);
6149aea9
PA
8175}
8176
ce78b96d
JB
8177/* The breakpoint_ops structure to be used in fork catchpoints. */
8178
2060206e 8179static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8180
4a64f543
MS
8181/* Implement the "insert" breakpoint_ops method for vfork
8182 catchpoints. */
ce78b96d 8183
77b06cd7
TJB
8184static int
8185insert_catch_vfork (struct bp_location *bl)
ce78b96d 8186{
dfd4cc63 8187 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8188}
8189
4a64f543
MS
8190/* Implement the "remove" breakpoint_ops method for vfork
8191 catchpoints. */
ce78b96d
JB
8192
8193static int
77b06cd7 8194remove_catch_vfork (struct bp_location *bl)
ce78b96d 8195{
dfd4cc63 8196 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8197}
8198
8199/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8200 catchpoints. */
8201
8202static int
f1310107 8203breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8204 struct address_space *aspace, CORE_ADDR bp_addr,
8205 const struct target_waitstatus *ws)
ce78b96d 8206{
e29a4733
PA
8207 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8208
f90263c1
TT
8209 if (ws->kind != TARGET_WAITKIND_VFORKED)
8210 return 0;
8211
8212 c->forked_inferior_pid = ws->value.related_pid;
8213 return 1;
ce78b96d
JB
8214}
8215
4a64f543
MS
8216/* Implement the "print_it" breakpoint_ops method for vfork
8217 catchpoints. */
ce78b96d
JB
8218
8219static enum print_stop_action
348d480f 8220print_it_catch_vfork (bpstat bs)
ce78b96d 8221{
36dfb11c 8222 struct ui_out *uiout = current_uiout;
348d480f 8223 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8224 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8225
ce78b96d 8226 annotate_catchpoint (b->number);
f303dbd6 8227 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8228 if (b->disposition == disp_del)
f303dbd6 8229 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8230 else
f303dbd6 8231 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8232 if (ui_out_is_mi_like_p (uiout))
8233 {
8234 ui_out_field_string (uiout, "reason",
8235 async_reason_lookup (EXEC_ASYNC_VFORK));
8236 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8237 }
8238 ui_out_field_int (uiout, "bkptno", b->number);
8239 ui_out_text (uiout, " (vforked process ");
8240 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8241 ui_out_text (uiout, "), ");
ce78b96d
JB
8242 return PRINT_SRC_AND_LOC;
8243}
8244
4a64f543
MS
8245/* Implement the "print_one" breakpoint_ops method for vfork
8246 catchpoints. */
ce78b96d
JB
8247
8248static void
a6d9a66e 8249print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8250{
e29a4733 8251 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8252 struct value_print_options opts;
79a45e25 8253 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8254
8255 get_user_print_options (&opts);
4a64f543
MS
8256 /* Field 4, the address, is omitted (which makes the columns not
8257 line up too nicely with the headers, but the effect is relatively
8258 readable). */
79a45b7d 8259 if (opts.addressprint)
ce78b96d
JB
8260 ui_out_field_skip (uiout, "addr");
8261 annotate_field (5);
8262 ui_out_text (uiout, "vfork");
e29a4733 8263 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8264 {
8265 ui_out_text (uiout, ", process ");
8266 ui_out_field_int (uiout, "what",
e29a4733 8267 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8268 ui_out_spaces (uiout, 1);
8269 }
8ac3646f
TT
8270
8271 if (ui_out_is_mi_like_p (uiout))
8272 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8273}
8274
8275/* Implement the "print_mention" breakpoint_ops method for vfork
8276 catchpoints. */
8277
8278static void
8279print_mention_catch_vfork (struct breakpoint *b)
8280{
8281 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8282}
8283
6149aea9
PA
8284/* Implement the "print_recreate" breakpoint_ops method for vfork
8285 catchpoints. */
8286
8287static void
8288print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8289{
8290 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8291 print_recreate_thread (b, fp);
6149aea9
PA
8292}
8293
ce78b96d
JB
8294/* The breakpoint_ops structure to be used in vfork catchpoints. */
8295
2060206e 8296static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8297
edcc5120
TT
8298/* An instance of this type is used to represent an solib catchpoint.
8299 It includes a "struct breakpoint" as a kind of base class; users
8300 downcast to "struct breakpoint *" when needed. A breakpoint is
8301 really of this type iff its ops pointer points to
8302 CATCH_SOLIB_BREAKPOINT_OPS. */
8303
8304struct solib_catchpoint
8305{
8306 /* The base class. */
8307 struct breakpoint base;
8308
8309 /* True for "catch load", false for "catch unload". */
8310 unsigned char is_load;
8311
8312 /* Regular expression to match, if any. COMPILED is only valid when
8313 REGEX is non-NULL. */
8314 char *regex;
8315 regex_t compiled;
8316};
8317
8318static void
8319dtor_catch_solib (struct breakpoint *b)
8320{
8321 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8322
8323 if (self->regex)
8324 regfree (&self->compiled);
8325 xfree (self->regex);
8326
8327 base_breakpoint_ops.dtor (b);
8328}
8329
8330static int
8331insert_catch_solib (struct bp_location *ignore)
8332{
8333 return 0;
8334}
8335
8336static int
8337remove_catch_solib (struct bp_location *ignore)
8338{
8339 return 0;
8340}
8341
8342static int
8343breakpoint_hit_catch_solib (const struct bp_location *bl,
8344 struct address_space *aspace,
8345 CORE_ADDR bp_addr,
8346 const struct target_waitstatus *ws)
8347{
8348 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8349 struct breakpoint *other;
8350
8351 if (ws->kind == TARGET_WAITKIND_LOADED)
8352 return 1;
8353
8354 ALL_BREAKPOINTS (other)
8355 {
8356 struct bp_location *other_bl;
8357
8358 if (other == bl->owner)
8359 continue;
8360
8361 if (other->type != bp_shlib_event)
8362 continue;
8363
8364 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8365 continue;
8366
8367 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8368 {
8369 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8370 return 1;
8371 }
8372 }
8373
8374 return 0;
8375}
8376
8377static void
8378check_status_catch_solib (struct bpstats *bs)
8379{
8380 struct solib_catchpoint *self
8381 = (struct solib_catchpoint *) bs->breakpoint_at;
8382 int ix;
8383
8384 if (self->is_load)
8385 {
8386 struct so_list *iter;
8387
8388 for (ix = 0;
8389 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8390 ix, iter);
8391 ++ix)
8392 {
8393 if (!self->regex
8394 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8395 return;
8396 }
8397 }
8398 else
8399 {
8400 char *iter;
8401
8402 for (ix = 0;
8403 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8404 ix, iter);
8405 ++ix)
8406 {
8407 if (!self->regex
8408 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8409 return;
8410 }
8411 }
8412
8413 bs->stop = 0;
8414 bs->print_it = print_it_noop;
8415}
8416
8417static enum print_stop_action
8418print_it_catch_solib (bpstat bs)
8419{
8420 struct breakpoint *b = bs->breakpoint_at;
8421 struct ui_out *uiout = current_uiout;
8422
8423 annotate_catchpoint (b->number);
f303dbd6 8424 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8425 if (b->disposition == disp_del)
f303dbd6 8426 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8427 else
f303dbd6 8428 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8429 ui_out_field_int (uiout, "bkptno", b->number);
8430 ui_out_text (uiout, "\n");
8431 if (ui_out_is_mi_like_p (uiout))
8432 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8433 print_solib_event (1);
8434 return PRINT_SRC_AND_LOC;
8435}
8436
8437static void
8438print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8439{
8440 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8441 struct value_print_options opts;
8442 struct ui_out *uiout = current_uiout;
8443 char *msg;
8444
8445 get_user_print_options (&opts);
8446 /* Field 4, the address, is omitted (which makes the columns not
8447 line up too nicely with the headers, but the effect is relatively
8448 readable). */
8449 if (opts.addressprint)
8450 {
8451 annotate_field (4);
8452 ui_out_field_skip (uiout, "addr");
8453 }
8454
8455 annotate_field (5);
8456 if (self->is_load)
8457 {
8458 if (self->regex)
8459 msg = xstrprintf (_("load of library matching %s"), self->regex);
8460 else
8461 msg = xstrdup (_("load of library"));
8462 }
8463 else
8464 {
8465 if (self->regex)
8466 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8467 else
8468 msg = xstrdup (_("unload of library"));
8469 }
8470 ui_out_field_string (uiout, "what", msg);
8471 xfree (msg);
8ac3646f
TT
8472
8473 if (ui_out_is_mi_like_p (uiout))
8474 ui_out_field_string (uiout, "catch-type",
8475 self->is_load ? "load" : "unload");
edcc5120
TT
8476}
8477
8478static void
8479print_mention_catch_solib (struct breakpoint *b)
8480{
8481 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8482
8483 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8484 self->is_load ? "load" : "unload");
8485}
8486
8487static void
8488print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8489{
8490 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8491
8492 fprintf_unfiltered (fp, "%s %s",
8493 b->disposition == disp_del ? "tcatch" : "catch",
8494 self->is_load ? "load" : "unload");
8495 if (self->regex)
8496 fprintf_unfiltered (fp, " %s", self->regex);
8497 fprintf_unfiltered (fp, "\n");
8498}
8499
8500static struct breakpoint_ops catch_solib_breakpoint_ops;
8501
91985142
MG
8502/* Shared helper function (MI and CLI) for creating and installing
8503 a shared object event catchpoint. If IS_LOAD is non-zero then
8504 the events to be caught are load events, otherwise they are
8505 unload events. If IS_TEMP is non-zero the catchpoint is a
8506 temporary one. If ENABLED is non-zero the catchpoint is
8507 created in an enabled state. */
edcc5120 8508
91985142
MG
8509void
8510add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8511{
8512 struct solib_catchpoint *c;
8513 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8514 struct cleanup *cleanup;
8515
edcc5120
TT
8516 if (!arg)
8517 arg = "";
8518 arg = skip_spaces (arg);
8519
8520 c = XCNEW (struct solib_catchpoint);
8521 cleanup = make_cleanup (xfree, c);
8522
8523 if (*arg != '\0')
8524 {
8525 int errcode;
8526
8527 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8528 if (errcode != 0)
8529 {
8530 char *err = get_regcomp_error (errcode, &c->compiled);
8531
8532 make_cleanup (xfree, err);
8533 error (_("Invalid regexp (%s): %s"), err, arg);
8534 }
8535 c->regex = xstrdup (arg);
8536 }
8537
8538 c->is_load = is_load;
91985142 8539 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8540 &catch_solib_breakpoint_ops);
8541
91985142
MG
8542 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8543
edcc5120
TT
8544 discard_cleanups (cleanup);
8545 install_breakpoint (0, &c->base, 1);
8546}
8547
91985142
MG
8548/* A helper function that does all the work for "catch load" and
8549 "catch unload". */
8550
8551static void
8552catch_load_or_unload (char *arg, int from_tty, int is_load,
8553 struct cmd_list_element *command)
8554{
8555 int tempflag;
8556 const int enabled = 1;
8557
8558 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8559
8560 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8561}
8562
edcc5120
TT
8563static void
8564catch_load_command_1 (char *arg, int from_tty,
8565 struct cmd_list_element *command)
8566{
8567 catch_load_or_unload (arg, from_tty, 1, command);
8568}
8569
8570static void
8571catch_unload_command_1 (char *arg, int from_tty,
8572 struct cmd_list_element *command)
8573{
8574 catch_load_or_unload (arg, from_tty, 0, command);
8575}
8576
346774a9
PA
8577/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8578 is non-zero, then make the breakpoint temporary. If COND_STRING is
8579 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8580 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8581
ab04a2af 8582void
346774a9
PA
8583init_catchpoint (struct breakpoint *b,
8584 struct gdbarch *gdbarch, int tempflag,
8585 char *cond_string,
c0a91b2b 8586 const struct breakpoint_ops *ops)
c906108c 8587{
c5aa993b 8588 struct symtab_and_line sal;
346774a9 8589
fe39c653 8590 init_sal (&sal);
6c95b8df 8591 sal.pspace = current_program_space;
c5aa993b 8592
28010a5d 8593 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8594
1b36a34b 8595 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8596 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8597}
8598
28010a5d 8599void
3ea46bff 8600install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8601{
8602 add_to_breakpoint_chain (b);
3a5c3e22 8603 set_breakpoint_number (internal, b);
558a9d82
YQ
8604 if (is_tracepoint (b))
8605 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8606 if (!internal)
8607 mention (b);
c56053d2 8608 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8609
8610 if (update_gll)
44702360 8611 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8612}
8613
9b70b993 8614static void
a6d9a66e
UW
8615create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8616 int tempflag, char *cond_string,
c0a91b2b 8617 const struct breakpoint_ops *ops)
c906108c 8618{
e29a4733 8619 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8620
e29a4733
PA
8621 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8622
8623 c->forked_inferior_pid = null_ptid;
8624
3ea46bff 8625 install_breakpoint (0, &c->base, 1);
c906108c
SS
8626}
8627
fe798b75
JB
8628/* Exec catchpoints. */
8629
b4d90040
PA
8630/* An instance of this type is used to represent an exec catchpoint.
8631 It includes a "struct breakpoint" as a kind of base class; users
8632 downcast to "struct breakpoint *" when needed. A breakpoint is
8633 really of this type iff its ops pointer points to
8634 CATCH_EXEC_BREAKPOINT_OPS. */
8635
8636struct exec_catchpoint
8637{
8638 /* The base class. */
8639 struct breakpoint base;
8640
8641 /* Filename of a program whose exec triggered this catchpoint.
8642 This field is only valid immediately after this catchpoint has
8643 triggered. */
8644 char *exec_pathname;
8645};
8646
8647/* Implement the "dtor" breakpoint_ops method for exec
8648 catchpoints. */
8649
8650static void
8651dtor_catch_exec (struct breakpoint *b)
8652{
8653 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8654
8655 xfree (c->exec_pathname);
348d480f 8656
2060206e 8657 base_breakpoint_ops.dtor (b);
b4d90040
PA
8658}
8659
77b06cd7
TJB
8660static int
8661insert_catch_exec (struct bp_location *bl)
c906108c 8662{
dfd4cc63 8663 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8664}
c906108c 8665
fe798b75 8666static int
77b06cd7 8667remove_catch_exec (struct bp_location *bl)
fe798b75 8668{
dfd4cc63 8669 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8670}
c906108c 8671
fe798b75 8672static int
f1310107 8673breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8674 struct address_space *aspace, CORE_ADDR bp_addr,
8675 const struct target_waitstatus *ws)
fe798b75 8676{
b4d90040
PA
8677 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8678
f90263c1
TT
8679 if (ws->kind != TARGET_WAITKIND_EXECD)
8680 return 0;
8681
8682 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8683 return 1;
fe798b75 8684}
c906108c 8685
fe798b75 8686static enum print_stop_action
348d480f 8687print_it_catch_exec (bpstat bs)
fe798b75 8688{
36dfb11c 8689 struct ui_out *uiout = current_uiout;
348d480f 8690 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8691 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8692
fe798b75 8693 annotate_catchpoint (b->number);
f303dbd6 8694 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8695 if (b->disposition == disp_del)
f303dbd6 8696 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8697 else
f303dbd6 8698 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8699 if (ui_out_is_mi_like_p (uiout))
8700 {
8701 ui_out_field_string (uiout, "reason",
8702 async_reason_lookup (EXEC_ASYNC_EXEC));
8703 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8704 }
8705 ui_out_field_int (uiout, "bkptno", b->number);
8706 ui_out_text (uiout, " (exec'd ");
8707 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8708 ui_out_text (uiout, "), ");
8709
fe798b75 8710 return PRINT_SRC_AND_LOC;
c906108c
SS
8711}
8712
fe798b75 8713static void
a6d9a66e 8714print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8715{
b4d90040 8716 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8717 struct value_print_options opts;
79a45e25 8718 struct ui_out *uiout = current_uiout;
fe798b75
JB
8719
8720 get_user_print_options (&opts);
8721
8722 /* Field 4, the address, is omitted (which makes the columns
8723 not line up too nicely with the headers, but the effect
8724 is relatively readable). */
8725 if (opts.addressprint)
8726 ui_out_field_skip (uiout, "addr");
8727 annotate_field (5);
8728 ui_out_text (uiout, "exec");
b4d90040 8729 if (c->exec_pathname != NULL)
fe798b75
JB
8730 {
8731 ui_out_text (uiout, ", program \"");
b4d90040 8732 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8733 ui_out_text (uiout, "\" ");
8734 }
8ac3646f
TT
8735
8736 if (ui_out_is_mi_like_p (uiout))
8737 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8738}
8739
8740static void
8741print_mention_catch_exec (struct breakpoint *b)
8742{
8743 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8744}
8745
6149aea9
PA
8746/* Implement the "print_recreate" breakpoint_ops method for exec
8747 catchpoints. */
8748
8749static void
8750print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8751{
8752 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8753 print_recreate_thread (b, fp);
6149aea9
PA
8754}
8755
2060206e 8756static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8757
c906108c 8758static int
fba45db2 8759hw_breakpoint_used_count (void)
c906108c 8760{
c906108c 8761 int i = 0;
f1310107
TJB
8762 struct breakpoint *b;
8763 struct bp_location *bl;
c906108c
SS
8764
8765 ALL_BREAKPOINTS (b)
c5aa993b 8766 {
d6b74ac4 8767 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8768 for (bl = b->loc; bl; bl = bl->next)
8769 {
8770 /* Special types of hardware breakpoints may use more than
8771 one register. */
348d480f 8772 i += b->ops->resources_needed (bl);
f1310107 8773 }
c5aa993b 8774 }
c906108c
SS
8775
8776 return i;
8777}
8778
a1398e0c
PA
8779/* Returns the resources B would use if it were a hardware
8780 watchpoint. */
8781
c906108c 8782static int
a1398e0c 8783hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8784{
c906108c 8785 int i = 0;
e09342b5 8786 struct bp_location *bl;
c906108c 8787
a1398e0c
PA
8788 if (!breakpoint_enabled (b))
8789 return 0;
8790
8791 for (bl = b->loc; bl; bl = bl->next)
8792 {
8793 /* Special types of hardware watchpoints may use more than
8794 one register. */
8795 i += b->ops->resources_needed (bl);
8796 }
8797
8798 return i;
8799}
8800
8801/* Returns the sum the used resources of all hardware watchpoints of
8802 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8803 the sum of the used resources of all hardware watchpoints of other
8804 types _not_ TYPE. */
8805
8806static int
8807hw_watchpoint_used_count_others (struct breakpoint *except,
8808 enum bptype type, int *other_type_used)
8809{
8810 int i = 0;
8811 struct breakpoint *b;
8812
c906108c
SS
8813 *other_type_used = 0;
8814 ALL_BREAKPOINTS (b)
e09342b5 8815 {
a1398e0c
PA
8816 if (b == except)
8817 continue;
e09342b5
TJB
8818 if (!breakpoint_enabled (b))
8819 continue;
8820
a1398e0c
PA
8821 if (b->type == type)
8822 i += hw_watchpoint_use_count (b);
8823 else if (is_hardware_watchpoint (b))
8824 *other_type_used = 1;
e09342b5
TJB
8825 }
8826
c906108c
SS
8827 return i;
8828}
8829
c906108c 8830void
fba45db2 8831disable_watchpoints_before_interactive_call_start (void)
c906108c 8832{
c5aa993b 8833 struct breakpoint *b;
c906108c
SS
8834
8835 ALL_BREAKPOINTS (b)
c5aa993b 8836 {
cc60f2e3 8837 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8838 {
b5de0fa7 8839 b->enable_state = bp_call_disabled;
44702360 8840 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8841 }
8842 }
c906108c
SS
8843}
8844
8845void
fba45db2 8846enable_watchpoints_after_interactive_call_stop (void)
c906108c 8847{
c5aa993b 8848 struct breakpoint *b;
c906108c
SS
8849
8850 ALL_BREAKPOINTS (b)
c5aa993b 8851 {
cc60f2e3 8852 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8853 {
b5de0fa7 8854 b->enable_state = bp_enabled;
44702360 8855 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8856 }
8857 }
c906108c
SS
8858}
8859
8bea4e01
UW
8860void
8861disable_breakpoints_before_startup (void)
8862{
6c95b8df 8863 current_program_space->executing_startup = 1;
44702360 8864 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8865}
8866
8867void
8868enable_breakpoints_after_startup (void)
8869{
6c95b8df 8870 current_program_space->executing_startup = 0;
f8eba3c6 8871 breakpoint_re_set ();
8bea4e01
UW
8872}
8873
7c16b83e
PA
8874/* Create a new single-step breakpoint for thread THREAD, with no
8875 locations. */
c906108c 8876
7c16b83e
PA
8877static struct breakpoint *
8878new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8879{
8880 struct breakpoint *b = XNEW (struct breakpoint);
8881
8882 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8883 &momentary_breakpoint_ops);
8884
8885 b->disposition = disp_donttouch;
8886 b->frame_id = null_frame_id;
8887
8888 b->thread = thread;
8889 gdb_assert (b->thread != 0);
8890
8891 add_to_breakpoint_chain (b);
8892
8893 return b;
8894}
8895
8896/* Set a momentary breakpoint of type TYPE at address specified by
8897 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8898 frame. */
c906108c
SS
8899
8900struct breakpoint *
a6d9a66e
UW
8901set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8902 struct frame_id frame_id, enum bptype type)
c906108c 8903{
52f0bd74 8904 struct breakpoint *b;
edb3359d 8905
193facb3
JK
8906 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8907 tail-called one. */
8908 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8909
06edf0c0 8910 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8911 b->enable_state = bp_enabled;
8912 b->disposition = disp_donttouch;
818dd999 8913 b->frame_id = frame_id;
c906108c 8914
4a64f543
MS
8915 /* If we're debugging a multi-threaded program, then we want
8916 momentary breakpoints to be active in only a single thread of
8917 control. */
39f77062 8918 if (in_thread_list (inferior_ptid))
5d5658a1 8919 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8920
44702360 8921 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8922
c906108c
SS
8923 return b;
8924}
611c83ae 8925
06edf0c0 8926/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8927 The new breakpoint will have type TYPE, use OPS as its
8928 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8929
06edf0c0
PA
8930static struct breakpoint *
8931momentary_breakpoint_from_master (struct breakpoint *orig,
8932 enum bptype type,
a1aa2221
LM
8933 const struct breakpoint_ops *ops,
8934 int loc_enabled)
e58b0e63
PA
8935{
8936 struct breakpoint *copy;
8937
06edf0c0 8938 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8939 copy->loc = allocate_bp_location (copy);
0e30163f 8940 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8941
a6d9a66e 8942 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8943 copy->loc->requested_address = orig->loc->requested_address;
8944 copy->loc->address = orig->loc->address;
8945 copy->loc->section = orig->loc->section;
6c95b8df 8946 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8947 copy->loc->probe = orig->loc->probe;
f8eba3c6 8948 copy->loc->line_number = orig->loc->line_number;
2f202fde 8949 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8950 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8951 copy->frame_id = orig->frame_id;
8952 copy->thread = orig->thread;
6c95b8df 8953 copy->pspace = orig->pspace;
e58b0e63
PA
8954
8955 copy->enable_state = bp_enabled;
8956 copy->disposition = disp_donttouch;
8957 copy->number = internal_breakpoint_number--;
8958
44702360 8959 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8960 return copy;
8961}
8962
06edf0c0
PA
8963/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8964 ORIG is NULL. */
8965
8966struct breakpoint *
8967clone_momentary_breakpoint (struct breakpoint *orig)
8968{
8969 /* If there's nothing to clone, then return nothing. */
8970 if (orig == NULL)
8971 return NULL;
8972
a1aa2221 8973 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8974}
8975
611c83ae 8976struct breakpoint *
a6d9a66e
UW
8977set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8978 enum bptype type)
611c83ae
PA
8979{
8980 struct symtab_and_line sal;
8981
8982 sal = find_pc_line (pc, 0);
8983 sal.pc = pc;
8984 sal.section = find_pc_overlay (pc);
8985 sal.explicit_pc = 1;
8986
a6d9a66e 8987 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8988}
c906108c 8989\f
c5aa993b 8990
c906108c
SS
8991/* Tell the user we have just set a breakpoint B. */
8992
8993static void
fba45db2 8994mention (struct breakpoint *b)
c906108c 8995{
348d480f 8996 b->ops->print_mention (b);
79a45e25 8997 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8998 return;
c906108c
SS
8999 printf_filtered ("\n");
9000}
c906108c 9001\f
c5aa993b 9002
1a853c52
PA
9003static int bp_loc_is_permanent (struct bp_location *loc);
9004
0d381245 9005static struct bp_location *
39d61571 9006add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9007 const struct symtab_and_line *sal)
9008{
9009 struct bp_location *loc, **tmp;
3742cc8b
YQ
9010 CORE_ADDR adjusted_address;
9011 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9012
9013 if (loc_gdbarch == NULL)
9014 loc_gdbarch = b->gdbarch;
9015
9016 /* Adjust the breakpoint's address prior to allocating a location.
9017 Once we call allocate_bp_location(), that mostly uninitialized
9018 location will be placed on the location chain. Adjustment of the
9019 breakpoint may cause target_read_memory() to be called and we do
9020 not want its scan of the location chain to find a breakpoint and
9021 location that's only been partially initialized. */
9022 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9023 sal->pc, b->type);
0d381245 9024
d30113d4 9025 /* Sort the locations by their ADDRESS. */
39d61571 9026 loc = allocate_bp_location (b);
d30113d4
JK
9027 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9028 tmp = &((*tmp)->next))
0d381245 9029 ;
d30113d4 9030 loc->next = *tmp;
0d381245 9031 *tmp = loc;
3742cc8b 9032
0d381245 9033 loc->requested_address = sal->pc;
3742cc8b 9034 loc->address = adjusted_address;
6c95b8df 9035 loc->pspace = sal->pspace;
729662a5
TT
9036 loc->probe.probe = sal->probe;
9037 loc->probe.objfile = sal->objfile;
6c95b8df 9038 gdb_assert (loc->pspace != NULL);
0d381245 9039 loc->section = sal->section;
3742cc8b 9040 loc->gdbarch = loc_gdbarch;
f8eba3c6 9041 loc->line_number = sal->line;
2f202fde 9042 loc->symtab = sal->symtab;
f8eba3c6 9043
0e30163f
JK
9044 set_breakpoint_location_function (loc,
9045 sal->explicit_pc || sal->explicit_line);
1a853c52 9046
6ae88661
LM
9047 /* While by definition, permanent breakpoints are already present in the
9048 code, we don't mark the location as inserted. Normally one would expect
9049 that GDB could rely on that breakpoint instruction to stop the program,
9050 thus removing the need to insert its own breakpoint, except that executing
9051 the breakpoint instruction can kill the target instead of reporting a
9052 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9053 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9054 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9055 breakpoint be inserted normally results in QEMU knowing about the GDB
9056 breakpoint, and thus trap before the breakpoint instruction is executed.
9057 (If GDB later needs to continue execution past the permanent breakpoint,
9058 it manually increments the PC, thus avoiding executing the breakpoint
9059 instruction.) */
1a853c52 9060 if (bp_loc_is_permanent (loc))
6ae88661 9061 loc->permanent = 1;
1a853c52 9062
0d381245
VP
9063 return loc;
9064}
514f746b
AR
9065\f
9066
1cf4d951 9067/* See breakpoint.h. */
514f746b 9068
1cf4d951
PA
9069int
9070program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9071{
9072 int len;
9073 CORE_ADDR addr;
1afeeb75 9074 const gdb_byte *bpoint;
514f746b 9075 gdb_byte *target_mem;
939c61fa
JK
9076 struct cleanup *cleanup;
9077 int retval = 0;
514f746b 9078
1cf4d951
PA
9079 addr = address;
9080 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9081
9082 /* Software breakpoints unsupported? */
9083 if (bpoint == NULL)
9084 return 0;
9085
224c3ddb 9086 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9087
9088 /* Enable the automatic memory restoration from breakpoints while
9089 we read the memory. Otherwise we could say about our temporary
9090 breakpoints they are permanent. */
9091 cleanup = make_show_memory_breakpoints_cleanup (0);
9092
9093 if (target_read_memory (address, target_mem, len) == 0
9094 && memcmp (target_mem, bpoint, len) == 0)
9095 retval = 1;
9096
9097 do_cleanups (cleanup);
9098
9099 return retval;
9100}
9101
9102/* Return 1 if LOC is pointing to a permanent breakpoint,
9103 return 0 otherwise. */
9104
9105static int
9106bp_loc_is_permanent (struct bp_location *loc)
9107{
9108 struct cleanup *cleanup;
9109 int retval;
9110
514f746b
AR
9111 gdb_assert (loc != NULL);
9112
244558af
LM
9113 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9114 attempt to read from the addresses the locations of these breakpoint types
9115 point to. program_breakpoint_here_p, below, will attempt to read
9116 memory. */
9117 if (!breakpoint_address_is_meaningful (loc->owner))
9118 return 0;
9119
6c95b8df 9120 cleanup = save_current_space_and_thread ();
6c95b8df 9121 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9122
1cf4d951 9123 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9124
939c61fa
JK
9125 do_cleanups (cleanup);
9126
9127 return retval;
514f746b
AR
9128}
9129
e7e0cddf
SS
9130/* Build a command list for the dprintf corresponding to the current
9131 settings of the dprintf style options. */
9132
9133static void
9134update_dprintf_command_list (struct breakpoint *b)
9135{
9136 char *dprintf_args = b->extra_string;
9137 char *printf_line = NULL;
9138
9139 if (!dprintf_args)
9140 return;
9141
9142 dprintf_args = skip_spaces (dprintf_args);
9143
9144 /* Allow a comma, as it may have terminated a location, but don't
9145 insist on it. */
9146 if (*dprintf_args == ',')
9147 ++dprintf_args;
9148 dprintf_args = skip_spaces (dprintf_args);
9149
9150 if (*dprintf_args != '"')
9151 error (_("Bad format string, missing '\"'."));
9152
d3ce09f5 9153 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9154 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9155 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9156 {
9157 if (!dprintf_function)
9158 error (_("No function supplied for dprintf call"));
9159
9160 if (dprintf_channel && strlen (dprintf_channel) > 0)
9161 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9162 dprintf_function,
9163 dprintf_channel,
9164 dprintf_args);
9165 else
9166 printf_line = xstrprintf ("call (void) %s (%s)",
9167 dprintf_function,
9168 dprintf_args);
9169 }
d3ce09f5
SS
9170 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9171 {
9172 if (target_can_run_breakpoint_commands ())
9173 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9174 else
9175 {
9176 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9177 printf_line = xstrprintf ("printf %s", dprintf_args);
9178 }
9179 }
e7e0cddf
SS
9180 else
9181 internal_error (__FILE__, __LINE__,
9182 _("Invalid dprintf style."));
9183
f28045c2 9184 gdb_assert (printf_line != NULL);
9d6e6e84 9185 /* Manufacture a printf sequence. */
f28045c2 9186 {
8d749320 9187 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9188
f28045c2
YQ
9189 printf_cmd_line->control_type = simple_control;
9190 printf_cmd_line->body_count = 0;
9191 printf_cmd_line->body_list = NULL;
9d6e6e84 9192 printf_cmd_line->next = NULL;
f28045c2 9193 printf_cmd_line->line = printf_line;
e7e0cddf 9194
f28045c2
YQ
9195 breakpoint_set_commands (b, printf_cmd_line);
9196 }
e7e0cddf
SS
9197}
9198
9199/* Update all dprintf commands, making their command lists reflect
9200 current style settings. */
9201
9202static void
9203update_dprintf_commands (char *args, int from_tty,
9204 struct cmd_list_element *c)
9205{
9206 struct breakpoint *b;
9207
9208 ALL_BREAKPOINTS (b)
9209 {
9210 if (b->type == bp_dprintf)
9211 update_dprintf_command_list (b);
9212 }
9213}
c3f6f71d 9214
f00aae0f
KS
9215/* Create a breakpoint with SAL as location. Use LOCATION
9216 as a description of the location, and COND_STRING
b35a8b2f
DE
9217 as condition expression. If LOCATION is NULL then create an
9218 "address location" from the address in the SAL. */
018d34a4
VP
9219
9220static void
d9b3f62e 9221init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9222 struct symtabs_and_lines sals,
9223 struct event_location *location,
f8eba3c6 9224 char *filter, char *cond_string,
e7e0cddf 9225 char *extra_string,
d9b3f62e
PA
9226 enum bptype type, enum bpdisp disposition,
9227 int thread, int task, int ignore_count,
c0a91b2b 9228 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9229 int enabled, int internal, unsigned flags,
9230 int display_canonical)
018d34a4 9231{
0d381245 9232 int i;
018d34a4
VP
9233
9234 if (type == bp_hardware_breakpoint)
9235 {
fbbd034e
AS
9236 int target_resources_ok;
9237
9238 i = hw_breakpoint_used_count ();
9239 target_resources_ok =
9240 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9241 i + 1, 0);
9242 if (target_resources_ok == 0)
9243 error (_("No hardware breakpoint support in the target."));
9244 else if (target_resources_ok < 0)
9245 error (_("Hardware breakpoints used exceeds limit."));
9246 }
9247
6c95b8df
PA
9248 gdb_assert (sals.nelts > 0);
9249
0d381245
VP
9250 for (i = 0; i < sals.nelts; ++i)
9251 {
9252 struct symtab_and_line sal = sals.sals[i];
9253 struct bp_location *loc;
9254
9255 if (from_tty)
5af949e3
UW
9256 {
9257 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9258 if (!loc_gdbarch)
9259 loc_gdbarch = gdbarch;
9260
9261 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9262 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9263 }
0d381245
VP
9264
9265 if (i == 0)
9266 {
d9b3f62e 9267 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9268 b->thread = thread;
4a306c9a 9269 b->task = task;
855a6e68 9270
0d381245 9271 b->cond_string = cond_string;
e7e0cddf 9272 b->extra_string = extra_string;
0d381245 9273 b->ignore_count = ignore_count;
41447f92 9274 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9275 b->disposition = disposition;
6c95b8df 9276
44f238bb
PA
9277 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9278 b->loc->inserted = 1;
9279
0fb4aa4b
PA
9280 if (type == bp_static_tracepoint)
9281 {
d9b3f62e 9282 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9283 struct static_tracepoint_marker marker;
9284
983af33b 9285 if (strace_marker_p (b))
0fb4aa4b
PA
9286 {
9287 /* We already know the marker exists, otherwise, we
9288 wouldn't see a sal for it. */
f00aae0f
KS
9289 const char *p = &event_location_to_string (b->location)[3];
9290 const char *endp;
0fb4aa4b 9291 char *marker_str;
0fb4aa4b 9292
f00aae0f 9293 p = skip_spaces_const (p);
0fb4aa4b 9294
f00aae0f 9295 endp = skip_to_space_const (p);
0fb4aa4b
PA
9296
9297 marker_str = savestring (p, endp - p);
d9b3f62e 9298 t->static_trace_marker_id = marker_str;
0fb4aa4b 9299
3e43a32a
MS
9300 printf_filtered (_("Probed static tracepoint "
9301 "marker \"%s\"\n"),
d9b3f62e 9302 t->static_trace_marker_id);
0fb4aa4b
PA
9303 }
9304 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9305 {
d9b3f62e 9306 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9307 release_static_tracepoint_marker (&marker);
9308
3e43a32a
MS
9309 printf_filtered (_("Probed static tracepoint "
9310 "marker \"%s\"\n"),
d9b3f62e 9311 t->static_trace_marker_id);
0fb4aa4b
PA
9312 }
9313 else
3e43a32a
MS
9314 warning (_("Couldn't determine the static "
9315 "tracepoint marker to probe"));
0fb4aa4b
PA
9316 }
9317
0d381245
VP
9318 loc = b->loc;
9319 }
9320 else
018d34a4 9321 {
39d61571 9322 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9323 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9324 loc->inserted = 1;
0d381245
VP
9325 }
9326
9327 if (b->cond_string)
9328 {
bbc13ae3
KS
9329 const char *arg = b->cond_string;
9330
1bb9788d
TT
9331 loc->cond = parse_exp_1 (&arg, loc->address,
9332 block_for_pc (loc->address), 0);
0d381245 9333 if (*arg)
588ae58c 9334 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9335 }
e7e0cddf
SS
9336
9337 /* Dynamic printf requires and uses additional arguments on the
9338 command line, otherwise it's an error. */
9339 if (type == bp_dprintf)
9340 {
9341 if (b->extra_string)
9342 update_dprintf_command_list (b);
9343 else
9344 error (_("Format string required"));
9345 }
9346 else if (b->extra_string)
588ae58c 9347 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9348 }
018d34a4 9349
56435ebe 9350 b->display_canonical = display_canonical;
f00aae0f
KS
9351 if (location != NULL)
9352 b->location = location;
018d34a4 9353 else
305e13e6
JB
9354 {
9355 const char *addr_string = NULL;
9356 int addr_string_len = 0;
9357
9358 if (location != NULL)
9359 addr_string = event_location_to_string (location);
9360 if (addr_string != NULL)
9361 addr_string_len = strlen (addr_string);
9362
9363 b->location = new_address_location (b->loc->address,
9364 addr_string, addr_string_len);
9365 }
f8eba3c6 9366 b->filter = filter;
d9b3f62e 9367}
018d34a4 9368
d9b3f62e
PA
9369static void
9370create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9371 struct symtabs_and_lines sals,
9372 struct event_location *location,
f8eba3c6 9373 char *filter, char *cond_string,
e7e0cddf 9374 char *extra_string,
d9b3f62e
PA
9375 enum bptype type, enum bpdisp disposition,
9376 int thread, int task, int ignore_count,
c0a91b2b 9377 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9378 int enabled, int internal, unsigned flags,
9379 int display_canonical)
d9b3f62e
PA
9380{
9381 struct breakpoint *b;
9382 struct cleanup *old_chain;
9383
9384 if (is_tracepoint_type (type))
9385 {
9386 struct tracepoint *t;
9387
9388 t = XCNEW (struct tracepoint);
9389 b = &t->base;
9390 }
9391 else
9392 b = XNEW (struct breakpoint);
9393
9394 old_chain = make_cleanup (xfree, b);
9395
9396 init_breakpoint_sal (b, gdbarch,
f00aae0f 9397 sals, location,
e7e0cddf 9398 filter, cond_string, extra_string,
d9b3f62e
PA
9399 type, disposition,
9400 thread, task, ignore_count,
9401 ops, from_tty,
44f238bb
PA
9402 enabled, internal, flags,
9403 display_canonical);
d9b3f62e
PA
9404 discard_cleanups (old_chain);
9405
3ea46bff 9406 install_breakpoint (internal, b, 0);
018d34a4
VP
9407}
9408
9409/* Add SALS.nelts breakpoints to the breakpoint table. For each
9410 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9411 value. COND_STRING, if not NULL, specified the condition to be
9412 used for all breakpoints. Essentially the only case where
9413 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9414 function. In that case, it's still not possible to specify
9415 separate conditions for different overloaded functions, so
9416 we take just a single condition string.
9417
c3f6f71d 9418 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9419 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9420 array contents). If the function fails (error() is called), the
9421 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9422 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9423
9424static void
8cdf0e15 9425create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9426 struct linespec_result *canonical,
e7e0cddf 9427 char *cond_string, char *extra_string,
8cdf0e15
VP
9428 enum bptype type, enum bpdisp disposition,
9429 int thread, int task, int ignore_count,
c0a91b2b 9430 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9431 int enabled, int internal, unsigned flags)
c906108c 9432{
018d34a4 9433 int i;
f8eba3c6 9434 struct linespec_sals *lsal;
cc59ec59 9435
f8eba3c6
TT
9436 if (canonical->pre_expanded)
9437 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9438
9439 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9440 {
f00aae0f 9441 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9442 'break', without arguments. */
f00aae0f
KS
9443 struct event_location *location
9444 = (canonical->location != NULL
9445 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9446 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9447 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9448
f8eba3c6
TT
9449 make_cleanup (xfree, filter_string);
9450 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9451 location,
f8eba3c6 9452 filter_string,
e7e0cddf
SS
9453 cond_string, extra_string,
9454 type, disposition,
84f4c1fe 9455 thread, task, ignore_count, ops,
44f238bb 9456 from_tty, enabled, internal, flags,
56435ebe 9457 canonical->special_display);
f8eba3c6 9458 discard_cleanups (inner);
c3f6f71d 9459 }
c3f6f71d 9460}
c906108c 9461
f00aae0f 9462/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9463 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9464 addresses found. LOCATION points to the end of the SAL (for
9465 linespec locations).
9998af43
TJB
9466
9467 The array and the line spec strings are allocated on the heap, it is
9468 the caller's responsibility to free them. */
c906108c 9469
b9362cc7 9470static void
f00aae0f 9471parse_breakpoint_sals (const struct event_location *location,
58438ac1 9472 struct linespec_result *canonical)
c3f6f71d 9473{
f00aae0f
KS
9474 struct symtab_and_line cursal;
9475
9476 if (event_location_type (location) == LINESPEC_LOCATION)
9477 {
9478 const char *address = get_linespec_location (location);
9479
9480 if (address == NULL)
9481 {
9482 /* The last displayed codepoint, if it's valid, is our default
9483 breakpoint address. */
9484 if (last_displayed_sal_is_valid ())
9485 {
9486 struct linespec_sals lsal;
9487 struct symtab_and_line sal;
9488 CORE_ADDR pc;
9489
9490 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9491 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9492
9493 /* Set sal's pspace, pc, symtab, and line to the values
9494 corresponding to the last call to print_frame_info.
9495 Be sure to reinitialize LINE with NOTCURRENT == 0
9496 as the breakpoint line number is inappropriate otherwise.
9497 find_pc_line would adjust PC, re-set it back. */
9498 get_last_displayed_sal (&sal);
9499 pc = sal.pc;
9500 sal = find_pc_line (pc, 0);
9501
9502 /* "break" without arguments is equivalent to "break *PC"
9503 where PC is the last displayed codepoint's address. So
9504 make sure to set sal.explicit_pc to prevent GDB from
9505 trying to expand the list of sals to include all other
9506 instances with the same symtab and line. */
9507 sal.pc = pc;
9508 sal.explicit_pc = 1;
9509
9510 lsal.sals.sals[0] = sal;
9511 lsal.sals.nelts = 1;
9512 lsal.canonical = NULL;
9513
9514 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9515 return;
9516 }
9517 else
9518 error (_("No default breakpoint address now."));
c906108c 9519 }
c906108c 9520 }
f00aae0f
KS
9521
9522 /* Force almost all breakpoints to be in terms of the
9523 current_source_symtab (which is decode_line_1's default).
9524 This should produce the results we want almost all of the
9525 time while leaving default_breakpoint_* alone.
9526
9527 ObjC: However, don't match an Objective-C method name which
9528 may have a '+' or '-' succeeded by a '['. */
9529 cursal = get_current_source_symtab_and_line ();
9530 if (last_displayed_sal_is_valid ())
c906108c 9531 {
f00aae0f 9532 const char *address = NULL;
cc80f267 9533
f00aae0f
KS
9534 if (event_location_type (location) == LINESPEC_LOCATION)
9535 address = get_linespec_location (location);
cc80f267 9536
f00aae0f
KS
9537 if (!cursal.symtab
9538 || (address != NULL
9539 && strchr ("+-", address[0]) != NULL
9540 && address[1] != '['))
9541 {
c2f4122d 9542 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9543 get_last_displayed_symtab (),
9544 get_last_displayed_line (),
9545 canonical, NULL, NULL);
9546 return;
9547 }
c906108c 9548 }
f00aae0f 9549
c2f4122d 9550 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9551 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9552}
c906108c 9553
c906108c 9554
c3f6f71d 9555/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9556 inserted as a breakpoint. If it can't throw an error. */
c906108c 9557
b9362cc7 9558static void
23e7acfb 9559breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9560{
9561 int i;
cc59ec59 9562
c3f6f71d 9563 for (i = 0; i < sals->nelts; i++)
ee53e872 9564 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9565}
9566
7a697b8d
SS
9567/* Fast tracepoints may have restrictions on valid locations. For
9568 instance, a fast tracepoint using a jump instead of a trap will
9569 likely have to overwrite more bytes than a trap would, and so can
9570 only be placed where the instruction is longer than the jump, or a
9571 multi-instruction sequence does not have a jump into the middle of
9572 it, etc. */
9573
9574static void
9575check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9576 struct symtabs_and_lines *sals)
9577{
9578 int i, rslt;
9579 struct symtab_and_line *sal;
9580 char *msg;
9581 struct cleanup *old_chain;
9582
9583 for (i = 0; i < sals->nelts; i++)
9584 {
f8eba3c6
TT
9585 struct gdbarch *sarch;
9586
7a697b8d
SS
9587 sal = &sals->sals[i];
9588
f8eba3c6
TT
9589 sarch = get_sal_arch (*sal);
9590 /* We fall back to GDBARCH if there is no architecture
9591 associated with SAL. */
9592 if (sarch == NULL)
9593 sarch = gdbarch;
6b940e6a 9594 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9595 old_chain = make_cleanup (xfree, msg);
9596
9597 if (!rslt)
9598 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9599 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9600
9601 do_cleanups (old_chain);
9602 }
9603}
9604
018d34a4
VP
9605/* Given TOK, a string specification of condition and thread, as
9606 accepted by the 'break' command, extract the condition
9607 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9608 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9609 If no condition is found, *COND_STRING is set to NULL.
9610 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9611
9612static void
bbc13ae3 9613find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9614 char **cond_string, int *thread, int *task,
9615 char **rest)
018d34a4
VP
9616{
9617 *cond_string = NULL;
9618 *thread = -1;
ed1d1739
KS
9619 *task = 0;
9620 *rest = NULL;
9621
018d34a4
VP
9622 while (tok && *tok)
9623 {
bbc13ae3 9624 const char *end_tok;
018d34a4 9625 int toklen;
bbc13ae3
KS
9626 const char *cond_start = NULL;
9627 const char *cond_end = NULL;
cc59ec59 9628
bbc13ae3 9629 tok = skip_spaces_const (tok);
e7e0cddf
SS
9630
9631 if ((*tok == '"' || *tok == ',') && rest)
9632 {
9633 *rest = savestring (tok, strlen (tok));
9634 return;
9635 }
9636
bbc13ae3 9637 end_tok = skip_to_space_const (tok);
d634f2de 9638
018d34a4 9639 toklen = end_tok - tok;
d634f2de 9640
018d34a4
VP
9641 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9642 {
f7545552
TT
9643 struct expression *expr;
9644
018d34a4 9645 tok = cond_start = end_tok + 1;
1bb9788d 9646 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9647 xfree (expr);
018d34a4 9648 cond_end = tok;
d634f2de 9649 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9650 }
9651 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9652 {
5d5658a1
PA
9653 const char *tmptok;
9654 struct thread_info *thr;
d634f2de 9655
018d34a4 9656 tok = end_tok + 1;
5d5658a1 9657 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9658 if (tok == tmptok)
9659 error (_("Junk after thread keyword."));
5d5658a1 9660 *thread = thr->global_num;
bbc13ae3 9661 tok = tmptok;
018d34a4 9662 }
4a306c9a
JB
9663 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9664 {
9665 char *tmptok;
9666
9667 tok = end_tok + 1;
bbc13ae3 9668 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9669 if (tok == tmptok)
9670 error (_("Junk after task keyword."));
9671 if (!valid_task_id (*task))
b6199126 9672 error (_("Unknown task %d."), *task);
bbc13ae3 9673 tok = tmptok;
4a306c9a 9674 }
e7e0cddf
SS
9675 else if (rest)
9676 {
9677 *rest = savestring (tok, strlen (tok));
ccab2054 9678 return;
e7e0cddf 9679 }
018d34a4
VP
9680 else
9681 error (_("Junk at end of arguments."));
9682 }
9683}
9684
0fb4aa4b
PA
9685/* Decode a static tracepoint marker spec. */
9686
9687static struct symtabs_and_lines
f00aae0f 9688decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9689{
9690 VEC(static_tracepoint_marker_p) *markers = NULL;
9691 struct symtabs_and_lines sals;
0fb4aa4b 9692 struct cleanup *old_chain;
f00aae0f
KS
9693 const char *p = &(*arg_p)[3];
9694 const char *endp;
0fb4aa4b
PA
9695 char *marker_str;
9696 int i;
9697
f00aae0f 9698 p = skip_spaces_const (p);
0fb4aa4b 9699
f00aae0f 9700 endp = skip_to_space_const (p);
0fb4aa4b
PA
9701
9702 marker_str = savestring (p, endp - p);
9703 old_chain = make_cleanup (xfree, marker_str);
9704
9705 markers = target_static_tracepoint_markers_by_strid (marker_str);
9706 if (VEC_empty(static_tracepoint_marker_p, markers))
9707 error (_("No known static tracepoint marker named %s"), marker_str);
9708
9709 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9710 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9711
9712 for (i = 0; i < sals.nelts; i++)
9713 {
9714 struct static_tracepoint_marker *marker;
9715
9716 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9717
9718 init_sal (&sals.sals[i]);
9719
9720 sals.sals[i] = find_pc_line (marker->address, 0);
9721 sals.sals[i].pc = marker->address;
9722
9723 release_static_tracepoint_marker (marker);
9724 }
9725
9726 do_cleanups (old_chain);
9727
9728 *arg_p = endp;
9729 return sals;
9730}
9731
f00aae0f 9732/* See breakpoint.h. */
0101ce28 9733
8cdf0e15
VP
9734int
9735create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9736 const struct event_location *location, char *cond_string,
e7e0cddf 9737 int thread, char *extra_string,
f00aae0f 9738 int parse_extra,
0fb4aa4b 9739 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9740 int ignore_count,
9741 enum auto_boolean pending_break_support,
c0a91b2b 9742 const struct breakpoint_ops *ops,
44f238bb
PA
9743 int from_tty, int enabled, int internal,
9744 unsigned flags)
c3f6f71d 9745{
7efd8fc2 9746 struct linespec_result canonical;
c3f6f71d 9747 struct cleanup *old_chain;
80c99de1 9748 struct cleanup *bkpt_chain = NULL;
0101ce28 9749 int pending = 0;
4a306c9a 9750 int task = 0;
86b17b60 9751 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9752
348d480f
PA
9753 gdb_assert (ops != NULL);
9754
f00aae0f
KS
9755 /* If extra_string isn't useful, set it to NULL. */
9756 if (extra_string != NULL && *extra_string == '\0')
9757 extra_string = NULL;
9758
7efd8fc2 9759 init_linespec_result (&canonical);
c3f6f71d 9760
492d29ea 9761 TRY
b78a6381 9762 {
f00aae0f 9763 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9764 }
492d29ea 9765 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9766 {
492d29ea
PA
9767 /* If caller is interested in rc value from parse, set
9768 value. */
9769 if (e.error == NOT_FOUND_ERROR)
0101ce28 9770 {
05ff989b
AC
9771 /* If pending breakpoint support is turned off, throw
9772 error. */
fa8d40ab
JJ
9773
9774 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9775 throw_exception (e);
9776
9777 exception_print (gdb_stderr, e);
fa8d40ab 9778
05ff989b
AC
9779 /* If pending breakpoint support is auto query and the user
9780 selects no, then simply return the error code. */
059fb39f 9781 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9782 && !nquery (_("Make %s pending on future shared library load? "),
9783 bptype_string (type_wanted)))
fd9b8c24 9784 return 0;
fa8d40ab 9785
05ff989b
AC
9786 /* At this point, either the user was queried about setting
9787 a pending breakpoint and selected yes, or pending
9788 breakpoint behavior is on and thus a pending breakpoint
9789 is defaulted on behalf of the user. */
f00aae0f 9790 pending = 1;
0101ce28 9791 }
492d29ea
PA
9792 else
9793 throw_exception (e);
0101ce28 9794 }
492d29ea
PA
9795 END_CATCH
9796
f00aae0f 9797 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9798 return 0;
c3f6f71d 9799
4a64f543 9800 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9801 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9802
c3f6f71d
JM
9803 /* ----------------------------- SNIP -----------------------------
9804 Anything added to the cleanup chain beyond this point is assumed
9805 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9806 then the memory is not reclaimed. */
9807 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9808
c3f6f71d
JM
9809 /* Resolve all line numbers to PC's and verify that the addresses
9810 are ok for the target. */
0101ce28 9811 if (!pending)
f8eba3c6
TT
9812 {
9813 int ix;
9814 struct linespec_sals *iter;
9815
9816 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9817 breakpoint_sals_to_pc (&iter->sals);
9818 }
c3f6f71d 9819
7a697b8d 9820 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9821 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9822 {
9823 int ix;
9824 struct linespec_sals *iter;
9825
9826 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9827 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9828 }
7a697b8d 9829
c3f6f71d
JM
9830 /* Verify that condition can be parsed, before setting any
9831 breakpoints. Allocate a separate condition expression for each
4a64f543 9832 breakpoint. */
0101ce28 9833 if (!pending)
c3f6f71d 9834 {
f00aae0f 9835 if (parse_extra)
72b2ff0e 9836 {
0878d0fa 9837 char *rest;
52d361e1
YQ
9838 struct linespec_sals *lsal;
9839
9840 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9841
0878d0fa
YQ
9842 /* Here we only parse 'arg' to separate condition
9843 from thread number, so parsing in context of first
9844 sal is OK. When setting the breakpoint we'll
9845 re-parse it in context of each sal. */
9846
f00aae0f
KS
9847 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9848 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9849 if (cond_string)
9850 make_cleanup (xfree, cond_string);
9851 if (rest)
9852 make_cleanup (xfree, rest);
9853 if (rest)
9854 extra_string = rest;
f00aae0f
KS
9855 else
9856 extra_string = NULL;
72b2ff0e 9857 }
2f069f6f 9858 else
72b2ff0e 9859 {
f00aae0f
KS
9860 if (type_wanted != bp_dprintf
9861 && extra_string != NULL && *extra_string != '\0')
9862 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9863
9864 /* Create a private copy of condition string. */
9865 if (cond_string)
9866 {
9867 cond_string = xstrdup (cond_string);
9868 make_cleanup (xfree, cond_string);
9869 }
9870 /* Create a private copy of any extra string. */
9871 if (extra_string)
9872 {
9873 extra_string = xstrdup (extra_string);
9874 make_cleanup (xfree, extra_string);
9875 }
72b2ff0e 9876 }
0fb4aa4b 9877
52d361e1 9878 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9879 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9880 tempflag ? disp_del : disp_donttouch,
9881 thread, task, ignore_count, ops,
44f238bb 9882 from_tty, enabled, internal, flags);
c906108c 9883 }
0101ce28
JJ
9884 else
9885 {
0101ce28
JJ
9886 struct breakpoint *b;
9887
bfccc43c
YQ
9888 if (is_tracepoint_type (type_wanted))
9889 {
9890 struct tracepoint *t;
9891
9892 t = XCNEW (struct tracepoint);
9893 b = &t->base;
9894 }
9895 else
9896 b = XNEW (struct breakpoint);
9897
9898 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9899 b->location = copy_event_location (location);
bfccc43c 9900
f00aae0f
KS
9901 if (parse_extra)
9902 b->cond_string = NULL;
e12c7713
MK
9903 else
9904 {
9905 /* Create a private copy of condition string. */
9906 if (cond_string)
9907 {
9908 cond_string = xstrdup (cond_string);
9909 make_cleanup (xfree, cond_string);
9910 }
9911 b->cond_string = cond_string;
15630549 9912 b->thread = thread;
e12c7713 9913 }
f00aae0f
KS
9914
9915 /* Create a private copy of any extra string. */
9916 if (extra_string != NULL)
9917 {
9918 extra_string = xstrdup (extra_string);
9919 make_cleanup (xfree, extra_string);
9920 }
9921 b->extra_string = extra_string;
0101ce28 9922 b->ignore_count = ignore_count;
0101ce28 9923 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9924 b->condition_not_parsed = 1;
41447f92 9925 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9926 if ((type_wanted != bp_breakpoint
9927 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9928 b->pspace = current_program_space;
8bea4e01 9929
bfccc43c 9930 install_breakpoint (internal, b, 0);
0101ce28
JJ
9931 }
9932
f8eba3c6 9933 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9934 {
3e43a32a
MS
9935 warning (_("Multiple breakpoints were set.\nUse the "
9936 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9937 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9938 }
9939
80c99de1
PA
9940 /* That's it. Discard the cleanups for data inserted into the
9941 breakpoint. */
9942 discard_cleanups (bkpt_chain);
9943 /* But cleanup everything else. */
c3f6f71d 9944 do_cleanups (old_chain);
217dc9e2 9945
80c99de1 9946 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9947 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9948
9949 return 1;
c3f6f71d 9950}
c906108c 9951
348d480f 9952/* Set a breakpoint.
72b2ff0e
VP
9953 ARG is a string describing breakpoint address,
9954 condition, and thread.
9955 FLAG specifies if a breakpoint is hardware on,
9956 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9957 and BP_TEMPFLAG. */
348d480f 9958
98deb0da 9959static void
72b2ff0e 9960break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9961{
72b2ff0e 9962 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9963 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9964 ? bp_hardware_breakpoint
9965 : bp_breakpoint);
55aa24fb 9966 struct breakpoint_ops *ops;
f00aae0f
KS
9967 struct event_location *location;
9968 struct cleanup *cleanup;
9969
9970 location = string_to_event_location (&arg, current_language);
9971 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9972
9973 /* Matching breakpoints on probes. */
5b56227b
KS
9974 if (location != NULL
9975 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9976 ops = &bkpt_probe_breakpoint_ops;
9977 else
9978 ops = &bkpt_breakpoint_ops;
c3f6f71d 9979
8cdf0e15 9980 create_breakpoint (get_current_arch (),
f00aae0f
KS
9981 location,
9982 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9983 tempflag, type_wanted,
8cdf0e15
VP
9984 0 /* Ignore count */,
9985 pending_break_support,
55aa24fb 9986 ops,
8cdf0e15 9987 from_tty,
84f4c1fe 9988 1 /* enabled */,
44f238bb
PA
9989 0 /* internal */,
9990 0);
f00aae0f 9991 do_cleanups (cleanup);
c906108c
SS
9992}
9993
c906108c
SS
9994/* Helper function for break_command_1 and disassemble_command. */
9995
9996void
fba45db2 9997resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9998{
9999 CORE_ADDR pc;
10000
10001 if (sal->pc == 0 && sal->symtab != NULL)
10002 {
10003 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10004 error (_("No line %d in file \"%s\"."),
05cba821 10005 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10006 sal->pc = pc;
6a048695 10007
4a64f543
MS
10008 /* If this SAL corresponds to a breakpoint inserted using a line
10009 number, then skip the function prologue if necessary. */
6a048695 10010 if (sal->explicit_line)
059acae7 10011 skip_prologue_sal (sal);
c906108c
SS
10012 }
10013
10014 if (sal->section == 0 && sal->symtab != NULL)
10015 {
346d1dfe 10016 const struct blockvector *bv;
3977b71f 10017 const struct block *b;
c5aa993b 10018 struct symbol *sym;
c906108c 10019
43f3e411
DE
10020 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10021 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10022 if (bv != NULL)
10023 {
7f0df278 10024 sym = block_linkage_function (b);
c906108c
SS
10025 if (sym != NULL)
10026 {
eb822aa6
DE
10027 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10028 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10029 sym);
c906108c
SS
10030 }
10031 else
10032 {
4a64f543
MS
10033 /* It really is worthwhile to have the section, so we'll
10034 just have to look harder. This case can be executed
10035 if we have line numbers but no functions (as can
10036 happen in assembly source). */
c906108c 10037
7cbd4a93 10038 struct bound_minimal_symbol msym;
6c95b8df
PA
10039 struct cleanup *old_chain = save_current_space_and_thread ();
10040
10041 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10042
10043 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10044 if (msym.minsym)
efd66ac6 10045 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10046
10047 do_cleanups (old_chain);
c906108c
SS
10048 }
10049 }
10050 }
10051}
10052
10053void
fba45db2 10054break_command (char *arg, int from_tty)
c906108c 10055{
db107f19 10056 break_command_1 (arg, 0, from_tty);
c906108c
SS
10057}
10058
c906108c 10059void
fba45db2 10060tbreak_command (char *arg, int from_tty)
c906108c 10061{
db107f19 10062 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10063}
10064
c906108c 10065static void
fba45db2 10066hbreak_command (char *arg, int from_tty)
c906108c 10067{
db107f19 10068 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10069}
10070
10071static void
fba45db2 10072thbreak_command (char *arg, int from_tty)
c906108c 10073{
db107f19 10074 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10075}
10076
10077static void
fba45db2 10078stop_command (char *arg, int from_tty)
c906108c 10079{
a3f17187 10080 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10081Usage: stop in <function | address>\n\
a3f17187 10082 stop at <line>\n"));
c906108c
SS
10083}
10084
10085static void
fba45db2 10086stopin_command (char *arg, int from_tty)
c906108c
SS
10087{
10088 int badInput = 0;
10089
c5aa993b 10090 if (arg == (char *) NULL)
c906108c
SS
10091 badInput = 1;
10092 else if (*arg != '*')
10093 {
10094 char *argptr = arg;
10095 int hasColon = 0;
10096
4a64f543 10097 /* Look for a ':'. If this is a line number specification, then
53a5351d 10098 say it is bad, otherwise, it should be an address or
4a64f543 10099 function/method name. */
c906108c 10100 while (*argptr && !hasColon)
c5aa993b
JM
10101 {
10102 hasColon = (*argptr == ':');
10103 argptr++;
10104 }
c906108c
SS
10105
10106 if (hasColon)
c5aa993b 10107 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10108 else
c5aa993b 10109 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10110 }
10111
10112 if (badInput)
a3f17187 10113 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10114 else
db107f19 10115 break_command_1 (arg, 0, from_tty);
c906108c
SS
10116}
10117
10118static void
fba45db2 10119stopat_command (char *arg, int from_tty)
c906108c
SS
10120{
10121 int badInput = 0;
10122
c5aa993b 10123 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10124 badInput = 1;
10125 else
10126 {
10127 char *argptr = arg;
10128 int hasColon = 0;
10129
4a64f543
MS
10130 /* Look for a ':'. If there is a '::' then get out, otherwise
10131 it is probably a line number. */
c906108c 10132 while (*argptr && !hasColon)
c5aa993b
JM
10133 {
10134 hasColon = (*argptr == ':');
10135 argptr++;
10136 }
c906108c
SS
10137
10138 if (hasColon)
c5aa993b 10139 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10140 else
c5aa993b 10141 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10142 }
10143
10144 if (badInput)
a3f17187 10145 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10146 else
db107f19 10147 break_command_1 (arg, 0, from_tty);
c906108c
SS
10148}
10149
e7e0cddf
SS
10150/* The dynamic printf command is mostly like a regular breakpoint, but
10151 with a prewired command list consisting of a single output command,
10152 built from extra arguments supplied on the dprintf command
10153 line. */
10154
da821c7b 10155static void
e7e0cddf
SS
10156dprintf_command (char *arg, int from_tty)
10157{
f00aae0f
KS
10158 struct event_location *location;
10159 struct cleanup *cleanup;
10160
10161 location = string_to_event_location (&arg, current_language);
10162 cleanup = make_cleanup_delete_event_location (location);
10163
10164 /* If non-NULL, ARG should have been advanced past the location;
10165 the next character must be ','. */
10166 if (arg != NULL)
10167 {
10168 if (arg[0] != ',' || arg[1] == '\0')
10169 error (_("Format string required"));
10170 else
10171 {
10172 /* Skip the comma. */
10173 ++arg;
10174 }
10175 }
10176
e7e0cddf 10177 create_breakpoint (get_current_arch (),
f00aae0f
KS
10178 location,
10179 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10180 0, bp_dprintf,
10181 0 /* Ignore count */,
10182 pending_break_support,
10183 &dprintf_breakpoint_ops,
10184 from_tty,
10185 1 /* enabled */,
10186 0 /* internal */,
10187 0);
f00aae0f 10188 do_cleanups (cleanup);
e7e0cddf
SS
10189}
10190
d3ce09f5
SS
10191static void
10192agent_printf_command (char *arg, int from_tty)
10193{
10194 error (_("May only run agent-printf on the target"));
10195}
10196
f1310107
TJB
10197/* Implement the "breakpoint_hit" breakpoint_ops method for
10198 ranged breakpoints. */
10199
10200static int
10201breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10202 struct address_space *aspace,
09ac7c10
TT
10203 CORE_ADDR bp_addr,
10204 const struct target_waitstatus *ws)
f1310107 10205{
09ac7c10 10206 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10207 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10208 return 0;
10209
f1310107
TJB
10210 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10211 bl->length, aspace, bp_addr);
10212}
10213
10214/* Implement the "resources_needed" breakpoint_ops method for
10215 ranged breakpoints. */
10216
10217static int
10218resources_needed_ranged_breakpoint (const struct bp_location *bl)
10219{
10220 return target_ranged_break_num_registers ();
10221}
10222
10223/* Implement the "print_it" breakpoint_ops method for
10224 ranged breakpoints. */
10225
10226static enum print_stop_action
348d480f 10227print_it_ranged_breakpoint (bpstat bs)
f1310107 10228{
348d480f 10229 struct breakpoint *b = bs->breakpoint_at;
f1310107 10230 struct bp_location *bl = b->loc;
79a45e25 10231 struct ui_out *uiout = current_uiout;
f1310107
TJB
10232
10233 gdb_assert (b->type == bp_hardware_breakpoint);
10234
10235 /* Ranged breakpoints have only one location. */
10236 gdb_assert (bl && bl->next == NULL);
10237
10238 annotate_breakpoint (b->number);
f303dbd6
PA
10239
10240 maybe_print_thread_hit_breakpoint (uiout);
10241
f1310107 10242 if (b->disposition == disp_del)
f303dbd6 10243 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10244 else
f303dbd6 10245 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10246 if (ui_out_is_mi_like_p (uiout))
10247 {
10248 ui_out_field_string (uiout, "reason",
10249 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10250 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10251 }
10252 ui_out_field_int (uiout, "bkptno", b->number);
10253 ui_out_text (uiout, ", ");
10254
10255 return PRINT_SRC_AND_LOC;
10256}
10257
10258/* Implement the "print_one" breakpoint_ops method for
10259 ranged breakpoints. */
10260
10261static void
10262print_one_ranged_breakpoint (struct breakpoint *b,
10263 struct bp_location **last_loc)
10264{
10265 struct bp_location *bl = b->loc;
10266 struct value_print_options opts;
79a45e25 10267 struct ui_out *uiout = current_uiout;
f1310107
TJB
10268
10269 /* Ranged breakpoints have only one location. */
10270 gdb_assert (bl && bl->next == NULL);
10271
10272 get_user_print_options (&opts);
10273
10274 if (opts.addressprint)
10275 /* We don't print the address range here, it will be printed later
10276 by print_one_detail_ranged_breakpoint. */
10277 ui_out_field_skip (uiout, "addr");
10278 annotate_field (5);
10279 print_breakpoint_location (b, bl);
10280 *last_loc = bl;
10281}
10282
10283/* Implement the "print_one_detail" breakpoint_ops method for
10284 ranged breakpoints. */
10285
10286static void
10287print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10288 struct ui_out *uiout)
10289{
10290 CORE_ADDR address_start, address_end;
10291 struct bp_location *bl = b->loc;
f99d8bf4
PA
10292 struct ui_file *stb = mem_fileopen ();
10293 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10294
10295 gdb_assert (bl);
10296
10297 address_start = bl->address;
10298 address_end = address_start + bl->length - 1;
10299
10300 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10301 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10302 print_core_address (bl->gdbarch, address_start),
10303 print_core_address (bl->gdbarch, address_end));
10304 ui_out_field_stream (uiout, "addr", stb);
10305 ui_out_text (uiout, "\n");
10306
10307 do_cleanups (cleanup);
10308}
10309
10310/* Implement the "print_mention" breakpoint_ops method for
10311 ranged breakpoints. */
10312
10313static void
10314print_mention_ranged_breakpoint (struct breakpoint *b)
10315{
10316 struct bp_location *bl = b->loc;
79a45e25 10317 struct ui_out *uiout = current_uiout;
f1310107
TJB
10318
10319 gdb_assert (bl);
10320 gdb_assert (b->type == bp_hardware_breakpoint);
10321
10322 if (ui_out_is_mi_like_p (uiout))
10323 return;
10324
10325 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10326 b->number, paddress (bl->gdbarch, bl->address),
10327 paddress (bl->gdbarch, bl->address + bl->length - 1));
10328}
10329
10330/* Implement the "print_recreate" breakpoint_ops method for
10331 ranged breakpoints. */
10332
10333static void
10334print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10335{
f00aae0f
KS
10336 fprintf_unfiltered (fp, "break-range %s, %s",
10337 event_location_to_string (b->location),
10338 event_location_to_string (b->location_range_end));
d9b3f62e 10339 print_recreate_thread (b, fp);
f1310107
TJB
10340}
10341
10342/* The breakpoint_ops structure to be used in ranged breakpoints. */
10343
2060206e 10344static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10345
10346/* Find the address where the end of the breakpoint range should be
10347 placed, given the SAL of the end of the range. This is so that if
10348 the user provides a line number, the end of the range is set to the
10349 last instruction of the given line. */
10350
10351static CORE_ADDR
10352find_breakpoint_range_end (struct symtab_and_line sal)
10353{
10354 CORE_ADDR end;
10355
10356 /* If the user provided a PC value, use it. Otherwise,
10357 find the address of the end of the given location. */
10358 if (sal.explicit_pc)
10359 end = sal.pc;
10360 else
10361 {
10362 int ret;
10363 CORE_ADDR start;
10364
10365 ret = find_line_pc_range (sal, &start, &end);
10366 if (!ret)
10367 error (_("Could not find location of the end of the range."));
10368
10369 /* find_line_pc_range returns the start of the next line. */
10370 end--;
10371 }
10372
10373 return end;
10374}
10375
10376/* Implement the "break-range" CLI command. */
10377
10378static void
10379break_range_command (char *arg, int from_tty)
10380{
10381 char *arg_start, *addr_string_start, *addr_string_end;
10382 struct linespec_result canonical_start, canonical_end;
10383 int bp_count, can_use_bp, length;
10384 CORE_ADDR end;
10385 struct breakpoint *b;
10386 struct symtab_and_line sal_start, sal_end;
f1310107 10387 struct cleanup *cleanup_bkpt;
f8eba3c6 10388 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10389 struct event_location *start_location, *end_location;
f1310107
TJB
10390
10391 /* We don't support software ranged breakpoints. */
10392 if (target_ranged_break_num_registers () < 0)
10393 error (_("This target does not support hardware ranged breakpoints."));
10394
10395 bp_count = hw_breakpoint_used_count ();
10396 bp_count += target_ranged_break_num_registers ();
10397 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10398 bp_count, 0);
10399 if (can_use_bp < 0)
10400 error (_("Hardware breakpoints used exceeds limit."));
10401
f8eba3c6 10402 arg = skip_spaces (arg);
f1310107
TJB
10403 if (arg == NULL || arg[0] == '\0')
10404 error(_("No address range specified."));
10405
f1310107
TJB
10406 init_linespec_result (&canonical_start);
10407
f8eba3c6 10408 arg_start = arg;
f00aae0f
KS
10409 start_location = string_to_event_location (&arg, current_language);
10410 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10411 parse_breakpoint_sals (start_location, &canonical_start);
10412 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10413
10414 if (arg[0] != ',')
10415 error (_("Too few arguments."));
f8eba3c6 10416 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10417 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10418
10419 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10420
10421 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10422 || lsal_start->sals.nelts != 1)
f1310107
TJB
10423 error (_("Cannot create a ranged breakpoint with multiple locations."));
10424
f8eba3c6
TT
10425 sal_start = lsal_start->sals.sals[0];
10426 addr_string_start = savestring (arg_start, arg - arg_start);
10427 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10428
10429 arg++; /* Skip the comma. */
f8eba3c6 10430 arg = skip_spaces (arg);
f1310107
TJB
10431
10432 /* Parse the end location. */
10433
f1310107
TJB
10434 init_linespec_result (&canonical_end);
10435 arg_start = arg;
10436
f8eba3c6 10437 /* We call decode_line_full directly here instead of using
f1310107
TJB
10438 parse_breakpoint_sals because we need to specify the start location's
10439 symtab and line as the default symtab and line for the end of the
10440 range. This makes it possible to have ranges like "foo.c:27, +14",
10441 where +14 means 14 lines from the start location. */
f00aae0f
KS
10442 end_location = string_to_event_location (&arg, current_language);
10443 make_cleanup_delete_event_location (end_location);
c2f4122d 10444 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10445 sal_start.symtab, sal_start.line,
10446 &canonical_end, NULL, NULL);
10447
10448 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10449
f8eba3c6 10450 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10451 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10452
10453 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10454 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10455 || lsal_end->sals.nelts != 1)
f1310107
TJB
10456 error (_("Cannot create a ranged breakpoint with multiple locations."));
10457
f8eba3c6 10458 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10459
10460 end = find_breakpoint_range_end (sal_end);
10461 if (sal_start.pc > end)
177b42fe 10462 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10463
10464 length = end - sal_start.pc + 1;
10465 if (length < 0)
10466 /* Length overflowed. */
10467 error (_("Address range too large."));
10468 else if (length == 1)
10469 {
10470 /* This range is simple enough to be handled by
10471 the `hbreak' command. */
10472 hbreak_command (addr_string_start, 1);
10473
10474 do_cleanups (cleanup_bkpt);
10475
10476 return;
10477 }
10478
10479 /* Now set up the breakpoint. */
10480 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10481 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10482 set_breakpoint_count (breakpoint_count + 1);
10483 b->number = breakpoint_count;
10484 b->disposition = disp_donttouch;
f00aae0f
KS
10485 b->location = copy_event_location (start_location);
10486 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10487 b->loc->length = length;
10488
f8eba3c6 10489 do_cleanups (cleanup_bkpt);
f1310107
TJB
10490
10491 mention (b);
8d3788bd 10492 observer_notify_breakpoint_created (b);
44702360 10493 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10494}
10495
4a64f543
MS
10496/* Return non-zero if EXP is verified as constant. Returned zero
10497 means EXP is variable. Also the constant detection may fail for
10498 some constant expressions and in such case still falsely return
10499 zero. */
2e6e3d9c 10500
65d79d4b
SDJ
10501static int
10502watchpoint_exp_is_const (const struct expression *exp)
10503{
10504 int i = exp->nelts;
10505
10506 while (i > 0)
10507 {
10508 int oplenp, argsp;
10509
10510 /* We are only interested in the descriptor of each element. */
10511 operator_length (exp, i, &oplenp, &argsp);
10512 i -= oplenp;
10513
10514 switch (exp->elts[i].opcode)
10515 {
10516 case BINOP_ADD:
10517 case BINOP_SUB:
10518 case BINOP_MUL:
10519 case BINOP_DIV:
10520 case BINOP_REM:
10521 case BINOP_MOD:
10522 case BINOP_LSH:
10523 case BINOP_RSH:
10524 case BINOP_LOGICAL_AND:
10525 case BINOP_LOGICAL_OR:
10526 case BINOP_BITWISE_AND:
10527 case BINOP_BITWISE_IOR:
10528 case BINOP_BITWISE_XOR:
10529 case BINOP_EQUAL:
10530 case BINOP_NOTEQUAL:
10531 case BINOP_LESS:
10532 case BINOP_GTR:
10533 case BINOP_LEQ:
10534 case BINOP_GEQ:
10535 case BINOP_REPEAT:
10536 case BINOP_COMMA:
10537 case BINOP_EXP:
10538 case BINOP_MIN:
10539 case BINOP_MAX:
10540 case BINOP_INTDIV:
10541 case BINOP_CONCAT:
65d79d4b
SDJ
10542 case TERNOP_COND:
10543 case TERNOP_SLICE:
65d79d4b
SDJ
10544
10545 case OP_LONG:
10546 case OP_DOUBLE:
10547 case OP_DECFLOAT:
10548 case OP_LAST:
10549 case OP_COMPLEX:
10550 case OP_STRING:
65d79d4b
SDJ
10551 case OP_ARRAY:
10552 case OP_TYPE:
608b4967
TT
10553 case OP_TYPEOF:
10554 case OP_DECLTYPE:
6e72ca20 10555 case OP_TYPEID:
65d79d4b
SDJ
10556 case OP_NAME:
10557 case OP_OBJC_NSSTRING:
10558
10559 case UNOP_NEG:
10560 case UNOP_LOGICAL_NOT:
10561 case UNOP_COMPLEMENT:
10562 case UNOP_ADDR:
10563 case UNOP_HIGH:
aeaa2474 10564 case UNOP_CAST:
9eaf6705
TT
10565
10566 case UNOP_CAST_TYPE:
10567 case UNOP_REINTERPRET_CAST:
10568 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10569 /* Unary, binary and ternary operators: We have to check
10570 their operands. If they are constant, then so is the
10571 result of that operation. For instance, if A and B are
10572 determined to be constants, then so is "A + B".
10573
10574 UNOP_IND is one exception to the rule above, because the
10575 value of *ADDR is not necessarily a constant, even when
10576 ADDR is. */
65d79d4b
SDJ
10577 break;
10578
10579 case OP_VAR_VALUE:
10580 /* Check whether the associated symbol is a constant.
4a64f543 10581
65d79d4b 10582 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10583 possible that a buggy compiler could mark a variable as
10584 constant even when it is not, and TYPE_CONST would return
10585 true in this case, while SYMBOL_CLASS wouldn't.
10586
10587 We also have to check for function symbols because they
10588 are always constant. */
65d79d4b
SDJ
10589 {
10590 struct symbol *s = exp->elts[i + 2].symbol;
10591
10592 if (SYMBOL_CLASS (s) != LOC_BLOCK
10593 && SYMBOL_CLASS (s) != LOC_CONST
10594 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10595 return 0;
10596 break;
10597 }
10598
10599 /* The default action is to return 0 because we are using
10600 the optimistic approach here: If we don't know something,
10601 then it is not a constant. */
10602 default:
10603 return 0;
10604 }
10605 }
10606
10607 return 1;
10608}
10609
3a5c3e22
PA
10610/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10611
10612static void
10613dtor_watchpoint (struct breakpoint *self)
10614{
10615 struct watchpoint *w = (struct watchpoint *) self;
10616
10617 xfree (w->cond_exp);
10618 xfree (w->exp);
10619 xfree (w->exp_string);
10620 xfree (w->exp_string_reparse);
10621 value_free (w->val);
10622
10623 base_breakpoint_ops.dtor (self);
10624}
10625
348d480f
PA
10626/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10627
10628static void
10629re_set_watchpoint (struct breakpoint *b)
10630{
3a5c3e22
PA
10631 struct watchpoint *w = (struct watchpoint *) b;
10632
348d480f
PA
10633 /* Watchpoint can be either on expression using entirely global
10634 variables, or it can be on local variables.
10635
10636 Watchpoints of the first kind are never auto-deleted, and even
10637 persist across program restarts. Since they can use variables
10638 from shared libraries, we need to reparse expression as libraries
10639 are loaded and unloaded.
10640
10641 Watchpoints on local variables can also change meaning as result
10642 of solib event. For example, if a watchpoint uses both a local
10643 and a global variables in expression, it's a local watchpoint,
10644 but unloading of a shared library will make the expression
10645 invalid. This is not a very common use case, but we still
10646 re-evaluate expression, to avoid surprises to the user.
10647
10648 Note that for local watchpoints, we re-evaluate it only if
10649 watchpoints frame id is still valid. If it's not, it means the
10650 watchpoint is out of scope and will be deleted soon. In fact,
10651 I'm not sure we'll ever be called in this case.
10652
10653 If a local watchpoint's frame id is still valid, then
3a5c3e22 10654 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10655
3a5c3e22
PA
10656 Don't do anything about disabled watchpoints, since they will be
10657 reevaluated again when enabled. */
10658 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10659}
10660
77b06cd7
TJB
10661/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10662
10663static int
10664insert_watchpoint (struct bp_location *bl)
10665{
3a5c3e22
PA
10666 struct watchpoint *w = (struct watchpoint *) bl->owner;
10667 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10668
10669 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10670 w->cond_exp);
77b06cd7
TJB
10671}
10672
10673/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10674
10675static int
10676remove_watchpoint (struct bp_location *bl)
10677{
3a5c3e22
PA
10678 struct watchpoint *w = (struct watchpoint *) bl->owner;
10679 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10680
10681 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10682 w->cond_exp);
e09342b5
TJB
10683}
10684
e09342b5 10685static int
348d480f 10686breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10687 struct address_space *aspace, CORE_ADDR bp_addr,
10688 const struct target_waitstatus *ws)
e09342b5 10689{
348d480f 10690 struct breakpoint *b = bl->owner;
3a5c3e22 10691 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10692
348d480f
PA
10693 /* Continuable hardware watchpoints are treated as non-existent if the
10694 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10695 some data address). Otherwise gdb won't stop on a break instruction
10696 in the code (not from a breakpoint) when a hardware watchpoint has
10697 been defined. Also skip watchpoints which we know did not trigger
10698 (did not match the data address). */
10699 if (is_hardware_watchpoint (b)
3a5c3e22 10700 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10701 return 0;
9c06b0b4 10702
348d480f 10703 return 1;
9c06b0b4
TJB
10704}
10705
348d480f
PA
10706static void
10707check_status_watchpoint (bpstat bs)
9c06b0b4 10708{
348d480f 10709 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10710
348d480f 10711 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10712}
10713
10714/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10715 hardware watchpoints. */
9c06b0b4
TJB
10716
10717static int
348d480f 10718resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10719{
3a5c3e22
PA
10720 struct watchpoint *w = (struct watchpoint *) bl->owner;
10721 int length = w->exact? 1 : bl->length;
348d480f
PA
10722
10723 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10724}
10725
10726/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10727 hardware watchpoints. */
9c06b0b4
TJB
10728
10729static int
348d480f 10730works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10731{
efa80663
PA
10732 /* Read and access watchpoints only work with hardware support. */
10733 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10734}
10735
9c06b0b4 10736static enum print_stop_action
348d480f 10737print_it_watchpoint (bpstat bs)
9c06b0b4 10738{
348d480f
PA
10739 struct cleanup *old_chain;
10740 struct breakpoint *b;
f99d8bf4 10741 struct ui_file *stb;
348d480f 10742 enum print_stop_action result;
3a5c3e22 10743 struct watchpoint *w;
79a45e25 10744 struct ui_out *uiout = current_uiout;
348d480f
PA
10745
10746 gdb_assert (bs->bp_location_at != NULL);
10747
348d480f 10748 b = bs->breakpoint_at;
3a5c3e22 10749 w = (struct watchpoint *) b;
348d480f 10750
f99d8bf4
PA
10751 stb = mem_fileopen ();
10752 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10753
f303dbd6
PA
10754 annotate_watchpoint (b->number);
10755 maybe_print_thread_hit_breakpoint (uiout);
10756
9c06b0b4
TJB
10757 switch (b->type)
10758 {
348d480f 10759 case bp_watchpoint:
9c06b0b4 10760 case bp_hardware_watchpoint:
9c06b0b4
TJB
10761 if (ui_out_is_mi_like_p (uiout))
10762 ui_out_field_string
10763 (uiout, "reason",
10764 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10765 mention (b);
10766 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10767 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10768 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10769 ui_out_field_stream (uiout, "old", stb);
10770 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10771 watchpoint_value_print (w->val, stb);
348d480f
PA
10772 ui_out_field_stream (uiout, "new", stb);
10773 ui_out_text (uiout, "\n");
10774 /* More than one watchpoint may have been triggered. */
10775 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10776 break;
10777
10778 case bp_read_watchpoint:
10779 if (ui_out_is_mi_like_p (uiout))
10780 ui_out_field_string
10781 (uiout, "reason",
10782 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10783 mention (b);
10784 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10785 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10786 watchpoint_value_print (w->val, stb);
348d480f
PA
10787 ui_out_field_stream (uiout, "value", stb);
10788 ui_out_text (uiout, "\n");
10789 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10790 break;
10791
10792 case bp_access_watchpoint:
348d480f
PA
10793 if (bs->old_val != NULL)
10794 {
348d480f
PA
10795 if (ui_out_is_mi_like_p (uiout))
10796 ui_out_field_string
10797 (uiout, "reason",
10798 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10799 mention (b);
10800 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10801 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10802 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10803 ui_out_field_stream (uiout, "old", stb);
10804 ui_out_text (uiout, "\nNew value = ");
10805 }
10806 else
10807 {
10808 mention (b);
10809 if (ui_out_is_mi_like_p (uiout))
10810 ui_out_field_string
10811 (uiout, "reason",
10812 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10813 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10814 ui_out_text (uiout, "\nValue = ");
10815 }
f99d8bf4 10816 watchpoint_value_print (w->val, stb);
348d480f
PA
10817 ui_out_field_stream (uiout, "new", stb);
10818 ui_out_text (uiout, "\n");
10819 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10820 break;
10821 default:
348d480f 10822 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10823 }
10824
348d480f
PA
10825 do_cleanups (old_chain);
10826 return result;
10827}
10828
10829/* Implement the "print_mention" breakpoint_ops method for hardware
10830 watchpoints. */
10831
10832static void
10833print_mention_watchpoint (struct breakpoint *b)
10834{
10835 struct cleanup *ui_out_chain;
3a5c3e22 10836 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10837 struct ui_out *uiout = current_uiout;
348d480f
PA
10838
10839 switch (b->type)
10840 {
10841 case bp_watchpoint:
10842 ui_out_text (uiout, "Watchpoint ");
10843 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10844 break;
10845 case bp_hardware_watchpoint:
10846 ui_out_text (uiout, "Hardware watchpoint ");
10847 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10848 break;
10849 case bp_read_watchpoint:
10850 ui_out_text (uiout, "Hardware read watchpoint ");
10851 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10852 break;
10853 case bp_access_watchpoint:
10854 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10855 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10856 break;
10857 default:
10858 internal_error (__FILE__, __LINE__,
10859 _("Invalid hardware watchpoint type."));
10860 }
10861
10862 ui_out_field_int (uiout, "number", b->number);
10863 ui_out_text (uiout, ": ");
3a5c3e22 10864 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10865 do_cleanups (ui_out_chain);
10866}
10867
10868/* Implement the "print_recreate" breakpoint_ops method for
10869 watchpoints. */
10870
10871static void
10872print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10873{
3a5c3e22
PA
10874 struct watchpoint *w = (struct watchpoint *) b;
10875
348d480f
PA
10876 switch (b->type)
10877 {
10878 case bp_watchpoint:
10879 case bp_hardware_watchpoint:
10880 fprintf_unfiltered (fp, "watch");
10881 break;
10882 case bp_read_watchpoint:
10883 fprintf_unfiltered (fp, "rwatch");
10884 break;
10885 case bp_access_watchpoint:
10886 fprintf_unfiltered (fp, "awatch");
10887 break;
10888 default:
10889 internal_error (__FILE__, __LINE__,
10890 _("Invalid watchpoint type."));
10891 }
10892
3a5c3e22 10893 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10894 print_recreate_thread (b, fp);
348d480f
PA
10895}
10896
427cd150
TT
10897/* Implement the "explains_signal" breakpoint_ops method for
10898 watchpoints. */
10899
47591c29 10900static int
427cd150
TT
10901explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10902{
10903 /* A software watchpoint cannot cause a signal other than
10904 GDB_SIGNAL_TRAP. */
10905 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10906 return 0;
427cd150 10907
47591c29 10908 return 1;
427cd150
TT
10909}
10910
348d480f
PA
10911/* The breakpoint_ops structure to be used in hardware watchpoints. */
10912
2060206e 10913static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10914
10915/* Implement the "insert" breakpoint_ops method for
10916 masked hardware watchpoints. */
10917
10918static int
10919insert_masked_watchpoint (struct bp_location *bl)
10920{
3a5c3e22
PA
10921 struct watchpoint *w = (struct watchpoint *) bl->owner;
10922
10923 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10924 bl->watchpoint_type);
10925}
10926
10927/* Implement the "remove" breakpoint_ops method for
10928 masked hardware watchpoints. */
10929
10930static int
10931remove_masked_watchpoint (struct bp_location *bl)
10932{
3a5c3e22
PA
10933 struct watchpoint *w = (struct watchpoint *) bl->owner;
10934
10935 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10936 bl->watchpoint_type);
10937}
10938
10939/* Implement the "resources_needed" breakpoint_ops method for
10940 masked hardware watchpoints. */
10941
10942static int
10943resources_needed_masked_watchpoint (const struct bp_location *bl)
10944{
3a5c3e22
PA
10945 struct watchpoint *w = (struct watchpoint *) bl->owner;
10946
10947 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10948}
10949
10950/* Implement the "works_in_software_mode" breakpoint_ops method for
10951 masked hardware watchpoints. */
10952
10953static int
10954works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10955{
10956 return 0;
10957}
10958
10959/* Implement the "print_it" breakpoint_ops method for
10960 masked hardware watchpoints. */
10961
10962static enum print_stop_action
10963print_it_masked_watchpoint (bpstat bs)
10964{
10965 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10966 struct ui_out *uiout = current_uiout;
348d480f
PA
10967
10968 /* Masked watchpoints have only one location. */
10969 gdb_assert (b->loc && b->loc->next == NULL);
10970
f303dbd6
PA
10971 annotate_watchpoint (b->number);
10972 maybe_print_thread_hit_breakpoint (uiout);
10973
348d480f
PA
10974 switch (b->type)
10975 {
10976 case bp_hardware_watchpoint:
348d480f
PA
10977 if (ui_out_is_mi_like_p (uiout))
10978 ui_out_field_string
10979 (uiout, "reason",
10980 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10981 break;
10982
10983 case bp_read_watchpoint:
10984 if (ui_out_is_mi_like_p (uiout))
10985 ui_out_field_string
10986 (uiout, "reason",
10987 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10988 break;
10989
10990 case bp_access_watchpoint:
10991 if (ui_out_is_mi_like_p (uiout))
10992 ui_out_field_string
10993 (uiout, "reason",
10994 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10995 break;
10996 default:
10997 internal_error (__FILE__, __LINE__,
10998 _("Invalid hardware watchpoint type."));
10999 }
11000
11001 mention (b);
9c06b0b4
TJB
11002 ui_out_text (uiout, _("\n\
11003Check the underlying instruction at PC for the memory\n\
11004address and value which triggered this watchpoint.\n"));
11005 ui_out_text (uiout, "\n");
11006
11007 /* More than one watchpoint may have been triggered. */
11008 return PRINT_UNKNOWN;
11009}
11010
11011/* Implement the "print_one_detail" breakpoint_ops method for
11012 masked hardware watchpoints. */
11013
11014static void
11015print_one_detail_masked_watchpoint (const struct breakpoint *b,
11016 struct ui_out *uiout)
11017{
3a5c3e22
PA
11018 struct watchpoint *w = (struct watchpoint *) b;
11019
9c06b0b4
TJB
11020 /* Masked watchpoints have only one location. */
11021 gdb_assert (b->loc && b->loc->next == NULL);
11022
11023 ui_out_text (uiout, "\tmask ");
3a5c3e22 11024 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11025 ui_out_text (uiout, "\n");
11026}
11027
11028/* Implement the "print_mention" breakpoint_ops method for
11029 masked hardware watchpoints. */
11030
11031static void
11032print_mention_masked_watchpoint (struct breakpoint *b)
11033{
3a5c3e22 11034 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11035 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11036 struct cleanup *ui_out_chain;
11037
11038 switch (b->type)
11039 {
11040 case bp_hardware_watchpoint:
11041 ui_out_text (uiout, "Masked hardware watchpoint ");
11042 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11043 break;
11044 case bp_read_watchpoint:
11045 ui_out_text (uiout, "Masked hardware read watchpoint ");
11046 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11047 break;
11048 case bp_access_watchpoint:
11049 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11050 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11051 break;
11052 default:
11053 internal_error (__FILE__, __LINE__,
11054 _("Invalid hardware watchpoint type."));
11055 }
11056
11057 ui_out_field_int (uiout, "number", b->number);
11058 ui_out_text (uiout, ": ");
3a5c3e22 11059 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11060 do_cleanups (ui_out_chain);
11061}
11062
11063/* Implement the "print_recreate" breakpoint_ops method for
11064 masked hardware watchpoints. */
11065
11066static void
11067print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11068{
3a5c3e22 11069 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11070 char tmp[40];
11071
11072 switch (b->type)
11073 {
11074 case bp_hardware_watchpoint:
11075 fprintf_unfiltered (fp, "watch");
11076 break;
11077 case bp_read_watchpoint:
11078 fprintf_unfiltered (fp, "rwatch");
11079 break;
11080 case bp_access_watchpoint:
11081 fprintf_unfiltered (fp, "awatch");
11082 break;
11083 default:
11084 internal_error (__FILE__, __LINE__,
11085 _("Invalid hardware watchpoint type."));
11086 }
11087
3a5c3e22
PA
11088 sprintf_vma (tmp, w->hw_wp_mask);
11089 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11090 print_recreate_thread (b, fp);
9c06b0b4
TJB
11091}
11092
11093/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11094
2060206e 11095static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11096
11097/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11098
11099static int
11100is_masked_watchpoint (const struct breakpoint *b)
11101{
11102 return b->ops == &masked_watchpoint_breakpoint_ops;
11103}
11104
53a5351d
JM
11105/* accessflag: hw_write: watch write,
11106 hw_read: watch read,
11107 hw_access: watch access (read or write) */
c906108c 11108static void
bbc13ae3 11109watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11110 int just_location, int internal)
c906108c 11111{
d983da9c 11112 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11113 struct expression *exp;
270140bd 11114 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11115 struct value *val, *mark, *result;
bb9d5f81 11116 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11117 struct frame_info *frame;
bbc13ae3
KS
11118 const char *exp_start = NULL;
11119 const char *exp_end = NULL;
11120 const char *tok, *end_tok;
9c06b0b4 11121 int toklen = -1;
bbc13ae3
KS
11122 const char *cond_start = NULL;
11123 const char *cond_end = NULL;
c906108c 11124 enum bptype bp_type;
37e4754d 11125 int thread = -1;
0cf6dd15 11126 int pc = 0;
9c06b0b4
TJB
11127 /* Flag to indicate whether we are going to use masks for
11128 the hardware watchpoint. */
11129 int use_mask = 0;
11130 CORE_ADDR mask = 0;
3a5c3e22 11131 struct watchpoint *w;
bbc13ae3
KS
11132 char *expression;
11133 struct cleanup *back_to;
c906108c 11134
37e4754d
LM
11135 /* Make sure that we actually have parameters to parse. */
11136 if (arg != NULL && arg[0] != '\0')
11137 {
bbc13ae3
KS
11138 const char *value_start;
11139
11140 exp_end = arg + strlen (arg);
37e4754d 11141
9c06b0b4
TJB
11142 /* Look for "parameter value" pairs at the end
11143 of the arguments string. */
bbc13ae3 11144 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11145 {
11146 /* Skip whitespace at the end of the argument list. */
11147 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11148 tok--;
11149
11150 /* Find the beginning of the last token.
11151 This is the value of the parameter. */
11152 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11153 tok--;
11154 value_start = tok + 1;
11155
11156 /* Skip whitespace. */
11157 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11158 tok--;
11159
11160 end_tok = tok;
11161
11162 /* Find the beginning of the second to last token.
11163 This is the parameter itself. */
11164 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11165 tok--;
11166 tok++;
11167 toklen = end_tok - tok + 1;
11168
61012eef 11169 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11170 {
5d5658a1 11171 struct thread_info *thr;
9c06b0b4
TJB
11172 /* At this point we've found a "thread" token, which means
11173 the user is trying to set a watchpoint that triggers
11174 only in a specific thread. */
5d5658a1 11175 const char *endp;
37e4754d 11176
9c06b0b4
TJB
11177 if (thread != -1)
11178 error(_("You can specify only one thread."));
37e4754d 11179
9c06b0b4 11180 /* Extract the thread ID from the next token. */
5d5658a1 11181 thr = parse_thread_id (value_start, &endp);
37e4754d 11182
5d5658a1 11183 /* Check if the user provided a valid thread ID. */
9c06b0b4 11184 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11185 invalid_thread_id_error (value_start);
9c06b0b4 11186
5d5658a1 11187 thread = thr->global_num;
9c06b0b4 11188 }
61012eef 11189 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11190 {
11191 /* We've found a "mask" token, which means the user wants to
11192 create a hardware watchpoint that is going to have the mask
11193 facility. */
11194 struct value *mask_value, *mark;
37e4754d 11195
9c06b0b4
TJB
11196 if (use_mask)
11197 error(_("You can specify only one mask."));
37e4754d 11198
9c06b0b4 11199 use_mask = just_location = 1;
37e4754d 11200
9c06b0b4
TJB
11201 mark = value_mark ();
11202 mask_value = parse_to_comma_and_eval (&value_start);
11203 mask = value_as_address (mask_value);
11204 value_free_to_mark (mark);
11205 }
11206 else
11207 /* We didn't recognize what we found. We should stop here. */
11208 break;
37e4754d 11209
9c06b0b4
TJB
11210 /* Truncate the string and get rid of the "parameter value" pair before
11211 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11212 exp_end = tok;
9c06b0b4 11213 }
37e4754d 11214 }
bbc13ae3
KS
11215 else
11216 exp_end = arg;
37e4754d 11217
bbc13ae3
KS
11218 /* Parse the rest of the arguments. From here on out, everything
11219 is in terms of a newly allocated string instead of the original
11220 ARG. */
c906108c 11221 innermost_block = NULL;
bbc13ae3
KS
11222 expression = savestring (arg, exp_end - arg);
11223 back_to = make_cleanup (xfree, expression);
11224 exp_start = arg = expression;
1bb9788d 11225 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11226 exp_end = arg;
fa8a61dc
TT
11227 /* Remove trailing whitespace from the expression before saving it.
11228 This makes the eventual display of the expression string a bit
11229 prettier. */
11230 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11231 --exp_end;
11232
65d79d4b
SDJ
11233 /* Checking if the expression is not constant. */
11234 if (watchpoint_exp_is_const (exp))
11235 {
11236 int len;
11237
11238 len = exp_end - exp_start;
11239 while (len > 0 && isspace (exp_start[len - 1]))
11240 len--;
11241 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11242 }
11243
c906108c
SS
11244 exp_valid_block = innermost_block;
11245 mark = value_mark ();
3a1115a0 11246 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11247
bb9d5f81
PP
11248 if (val != NULL && just_location)
11249 {
11250 saved_bitpos = value_bitpos (val);
11251 saved_bitsize = value_bitsize (val);
11252 }
11253
06a64a0b
TT
11254 if (just_location)
11255 {
9c06b0b4
TJB
11256 int ret;
11257
06a64a0b 11258 exp_valid_block = NULL;
a1442452 11259 val = value_addr (result);
06a64a0b
TT
11260 release_value (val);
11261 value_free_to_mark (mark);
9c06b0b4
TJB
11262
11263 if (use_mask)
11264 {
11265 ret = target_masked_watch_num_registers (value_as_address (val),
11266 mask);
11267 if (ret == -1)
11268 error (_("This target does not support masked watchpoints."));
11269 else if (ret == -2)
11270 error (_("Invalid mask or memory region."));
11271 }
06a64a0b
TT
11272 }
11273 else if (val != NULL)
fa4727a6 11274 release_value (val);
c906108c 11275
bbc13ae3
KS
11276 tok = skip_spaces_const (arg);
11277 end_tok = skip_to_space_const (tok);
c906108c
SS
11278
11279 toklen = end_tok - tok;
11280 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11281 {
2d134ed3
PA
11282 struct expression *cond;
11283
60e1c644 11284 innermost_block = NULL;
c906108c 11285 tok = cond_start = end_tok + 1;
1bb9788d 11286 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11287
11288 /* The watchpoint expression may not be local, but the condition
11289 may still be. E.g.: `watch global if local > 0'. */
11290 cond_exp_valid_block = innermost_block;
11291
2d134ed3 11292 xfree (cond);
c906108c
SS
11293 cond_end = tok;
11294 }
11295 if (*tok)
8a3fe4f8 11296 error (_("Junk at end of command."));
c906108c 11297
d983da9c 11298 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11299
11300 /* If the expression is "local", then set up a "watchpoint scope"
11301 breakpoint at the point where we've left the scope of the watchpoint
11302 expression. Create the scope breakpoint before the watchpoint, so
11303 that we will encounter it first in bpstat_stop_status. */
60e1c644 11304 if (exp_valid_block && frame)
d983da9c 11305 {
edb3359d
DJ
11306 if (frame_id_p (frame_unwind_caller_id (frame)))
11307 {
11308 scope_breakpoint
a6d9a66e
UW
11309 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11310 frame_unwind_caller_pc (frame),
06edf0c0
PA
11311 bp_watchpoint_scope,
11312 &momentary_breakpoint_ops);
d983da9c 11313
edb3359d 11314 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11315
edb3359d
DJ
11316 /* Automatically delete the breakpoint when it hits. */
11317 scope_breakpoint->disposition = disp_del;
d983da9c 11318
edb3359d
DJ
11319 /* Only break in the proper frame (help with recursion). */
11320 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11321
edb3359d 11322 /* Set the address at which we will stop. */
a6d9a66e
UW
11323 scope_breakpoint->loc->gdbarch
11324 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11325 scope_breakpoint->loc->requested_address
11326 = frame_unwind_caller_pc (frame);
11327 scope_breakpoint->loc->address
a6d9a66e
UW
11328 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11329 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11330 scope_breakpoint->type);
11331 }
d983da9c
DJ
11332 }
11333
e8369a73
AB
11334 /* Now set up the breakpoint. We create all watchpoints as hardware
11335 watchpoints here even if hardware watchpoints are turned off, a call
11336 to update_watchpoint later in this function will cause the type to
11337 drop back to bp_watchpoint (software watchpoint) if required. */
11338
11339 if (accessflag == hw_read)
11340 bp_type = bp_read_watchpoint;
11341 else if (accessflag == hw_access)
11342 bp_type = bp_access_watchpoint;
11343 else
11344 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11345
11346 w = XCNEW (struct watchpoint);
11347 b = &w->base;
348d480f 11348 if (use_mask)
3a5c3e22
PA
11349 init_raw_breakpoint_without_location (b, NULL, bp_type,
11350 &masked_watchpoint_breakpoint_ops);
348d480f 11351 else
3a5c3e22
PA
11352 init_raw_breakpoint_without_location (b, NULL, bp_type,
11353 &watchpoint_breakpoint_ops);
37e4754d 11354 b->thread = thread;
b5de0fa7 11355 b->disposition = disp_donttouch;
348d480f 11356 b->pspace = current_program_space;
3a5c3e22
PA
11357 w->exp = exp;
11358 w->exp_valid_block = exp_valid_block;
11359 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11360 if (just_location)
11361 {
11362 struct type *t = value_type (val);
11363 CORE_ADDR addr = value_as_address (val);
11364 char *name;
11365
11366 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11367 name = type_to_string (t);
11368
3a5c3e22 11369 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11370 core_addr_to_string (addr));
06a64a0b
TT
11371 xfree (name);
11372
3a5c3e22 11373 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11374 (int) (exp_end - exp_start), exp_start);
11375
06a64a0b
TT
11376 /* The above expression is in C. */
11377 b->language = language_c;
11378 }
11379 else
3a5c3e22 11380 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11381
11382 if (use_mask)
11383 {
3a5c3e22 11384 w->hw_wp_mask = mask;
9c06b0b4
TJB
11385 }
11386 else
11387 {
3a5c3e22 11388 w->val = val;
bb9d5f81
PP
11389 w->val_bitpos = saved_bitpos;
11390 w->val_bitsize = saved_bitsize;
3a5c3e22 11391 w->val_valid = 1;
9c06b0b4 11392 }
77b06cd7 11393
c906108c
SS
11394 if (cond_start)
11395 b->cond_string = savestring (cond_start, cond_end - cond_start);
11396 else
11397 b->cond_string = 0;
c5aa993b 11398
c906108c 11399 if (frame)
f6bc2008 11400 {
3a5c3e22
PA
11401 w->watchpoint_frame = get_frame_id (frame);
11402 w->watchpoint_thread = inferior_ptid;
f6bc2008 11403 }
c906108c 11404 else
f6bc2008 11405 {
3a5c3e22
PA
11406 w->watchpoint_frame = null_frame_id;
11407 w->watchpoint_thread = null_ptid;
f6bc2008 11408 }
c906108c 11409
d983da9c 11410 if (scope_breakpoint != NULL)
c906108c 11411 {
d983da9c
DJ
11412 /* The scope breakpoint is related to the watchpoint. We will
11413 need to act on them together. */
11414 b->related_breakpoint = scope_breakpoint;
11415 scope_breakpoint->related_breakpoint = b;
c906108c 11416 }
d983da9c 11417
06a64a0b
TT
11418 if (!just_location)
11419 value_free_to_mark (mark);
2d134ed3 11420
492d29ea 11421 TRY
a9634178
TJB
11422 {
11423 /* Finally update the new watchpoint. This creates the locations
11424 that should be inserted. */
3a5c3e22 11425 update_watchpoint (w, 1);
a9634178 11426 }
492d29ea 11427 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11428 {
11429 delete_breakpoint (b);
11430 throw_exception (e);
11431 }
492d29ea 11432 END_CATCH
a9634178 11433
3ea46bff 11434 install_breakpoint (internal, b, 1);
bbc13ae3 11435 do_cleanups (back_to);
c906108c
SS
11436}
11437
e09342b5 11438/* Return count of debug registers needed to watch the given expression.
e09342b5 11439 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11440
c906108c 11441static int
a9634178 11442can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11443{
11444 int found_memory_cnt = 0;
2e70b7b9 11445 struct value *head = v;
c906108c
SS
11446
11447 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11448 if (!can_use_hw_watchpoints)
c906108c 11449 return 0;
c5aa993b 11450
5c44784c
JM
11451 /* Make sure that the value of the expression depends only upon
11452 memory contents, and values computed from them within GDB. If we
11453 find any register references or function calls, we can't use a
11454 hardware watchpoint.
11455
11456 The idea here is that evaluating an expression generates a series
11457 of values, one holding the value of every subexpression. (The
11458 expression a*b+c has five subexpressions: a, b, a*b, c, and
11459 a*b+c.) GDB's values hold almost enough information to establish
11460 the criteria given above --- they identify memory lvalues,
11461 register lvalues, computed values, etcetera. So we can evaluate
11462 the expression, and then scan the chain of values that leaves
11463 behind to decide whether we can detect any possible change to the
11464 expression's final value using only hardware watchpoints.
11465
11466 However, I don't think that the values returned by inferior
11467 function calls are special in any way. So this function may not
11468 notice that an expression involving an inferior function call
11469 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11470 for (; v; v = value_next (v))
c906108c 11471 {
5c44784c 11472 if (VALUE_LVAL (v) == lval_memory)
c906108c 11473 {
8464be76
DJ
11474 if (v != head && value_lazy (v))
11475 /* A lazy memory lvalue in the chain is one that GDB never
11476 needed to fetch; we either just used its address (e.g.,
11477 `a' in `a.b') or we never needed it at all (e.g., `a'
11478 in `a,b'). This doesn't apply to HEAD; if that is
11479 lazy then it was not readable, but watch it anyway. */
5c44784c 11480 ;
53a5351d 11481 else
5c44784c
JM
11482 {
11483 /* Ahh, memory we actually used! Check if we can cover
11484 it with hardware watchpoints. */
df407dfe 11485 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11486
11487 /* We only watch structs and arrays if user asked for it
11488 explicitly, never if they just happen to appear in a
11489 middle of some value chain. */
11490 if (v == head
11491 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11492 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11493 {
42ae5230 11494 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11495 int len;
11496 int num_regs;
11497
a9634178 11498 len = (target_exact_watchpoints
e09342b5
TJB
11499 && is_scalar_type_recursive (vtype))?
11500 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11501
e09342b5
TJB
11502 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11503 if (!num_regs)
2e70b7b9
MS
11504 return 0;
11505 else
e09342b5 11506 found_memory_cnt += num_regs;
2e70b7b9 11507 }
5c44784c 11508 }
c5aa993b 11509 }
5086187c
AC
11510 else if (VALUE_LVAL (v) != not_lval
11511 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11512 return 0; /* These are values from the history (e.g., $1). */
5086187c 11513 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11514 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11515 }
11516
11517 /* The expression itself looks suitable for using a hardware
11518 watchpoint, but give the target machine a chance to reject it. */
11519 return found_memory_cnt;
11520}
11521
8b93c638 11522void
84f4c1fe 11523watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11524{
84f4c1fe 11525 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11526}
11527
06a64a0b
TT
11528/* A helper function that looks for the "-location" argument and then
11529 calls watch_command_1. */
11530
11531static void
11532watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11533{
11534 int just_location = 0;
11535
11536 if (arg
11537 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11538 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11539 {
e9cafbcc 11540 arg = skip_spaces (arg);
06a64a0b
TT
11541 just_location = 1;
11542 }
11543
84f4c1fe 11544 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11545}
8926118c 11546
c5aa993b 11547static void
fba45db2 11548watch_command (char *arg, int from_tty)
c906108c 11549{
06a64a0b 11550 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11551}
11552
8b93c638 11553void
84f4c1fe 11554rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11555{
84f4c1fe 11556 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11557}
8926118c 11558
c5aa993b 11559static void
fba45db2 11560rwatch_command (char *arg, int from_tty)
c906108c 11561{
06a64a0b 11562 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11563}
11564
8b93c638 11565void
84f4c1fe 11566awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11567{
84f4c1fe 11568 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11569}
8926118c 11570
c5aa993b 11571static void
fba45db2 11572awatch_command (char *arg, int from_tty)
c906108c 11573{
06a64a0b 11574 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11575}
c906108c 11576\f
c5aa993b 11577
cfc31633
PA
11578/* Data for the FSM that manages the until(location)/advance commands
11579 in infcmd.c. Here because it uses the mechanisms of
11580 breakpoints. */
c906108c 11581
cfc31633 11582struct until_break_fsm
bfec99b2 11583{
cfc31633
PA
11584 /* The base class. */
11585 struct thread_fsm thread_fsm;
11586
11587 /* The thread that as current when the command was executed. */
11588 int thread;
11589
11590 /* The breakpoint set at the destination location. */
11591 struct breakpoint *location_breakpoint;
11592
11593 /* Breakpoint set at the return address in the caller frame. May be
11594 NULL. */
11595 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11596};
11597
cfc31633
PA
11598static void until_break_fsm_clean_up (struct thread_fsm *self);
11599static int until_break_fsm_should_stop (struct thread_fsm *self);
11600static enum async_reply_reason
11601 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11602
11603/* until_break_fsm's vtable. */
11604
11605static struct thread_fsm_ops until_break_fsm_ops =
11606{
11607 NULL, /* dtor */
11608 until_break_fsm_clean_up,
11609 until_break_fsm_should_stop,
11610 NULL, /* return_value */
11611 until_break_fsm_async_reply_reason,
11612};
11613
11614/* Allocate a new until_break_command_fsm. */
11615
11616static struct until_break_fsm *
11617new_until_break_fsm (int thread,
11618 struct breakpoint *location_breakpoint,
11619 struct breakpoint *caller_breakpoint)
11620{
11621 struct until_break_fsm *sm;
11622
11623 sm = XCNEW (struct until_break_fsm);
11624 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11625
11626 sm->thread = thread;
11627 sm->location_breakpoint = location_breakpoint;
11628 sm->caller_breakpoint = caller_breakpoint;
11629
11630 return sm;
11631}
11632
11633/* Implementation of the 'should_stop' FSM method for the
11634 until(location)/advance commands. */
11635
11636static int
11637until_break_fsm_should_stop (struct thread_fsm *self)
11638{
11639 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11640 struct thread_info *tp = inferior_thread ();
11641
11642 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11643 sm->location_breakpoint) != NULL
11644 || (sm->caller_breakpoint != NULL
11645 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11646 sm->caller_breakpoint) != NULL))
11647 thread_fsm_set_finished (self);
11648
11649 return 1;
11650}
11651
11652/* Implementation of the 'clean_up' FSM method for the
11653 until(location)/advance commands. */
11654
c2c6d25f 11655static void
cfc31633 11656until_break_fsm_clean_up (struct thread_fsm *self)
43ff13b4 11657{
cfc31633 11658 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11659
cfc31633
PA
11660 /* Clean up our temporary breakpoints. */
11661 if (sm->location_breakpoint != NULL)
11662 {
11663 delete_breakpoint (sm->location_breakpoint);
11664 sm->location_breakpoint = NULL;
11665 }
11666 if (sm->caller_breakpoint != NULL)
11667 {
11668 delete_breakpoint (sm->caller_breakpoint);
11669 sm->caller_breakpoint = NULL;
11670 }
11671 delete_longjmp_breakpoint (sm->thread);
11672}
11673
11674/* Implementation of the 'async_reply_reason' FSM method for the
11675 until(location)/advance commands. */
11676
11677static enum async_reply_reason
11678until_break_fsm_async_reply_reason (struct thread_fsm *self)
11679{
11680 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11681}
11682
c906108c 11683void
ae66c1fc 11684until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11685{
11686 struct symtabs_and_lines sals;
11687 struct symtab_and_line sal;
8556afb4
PA
11688 struct frame_info *frame;
11689 struct gdbarch *frame_gdbarch;
11690 struct frame_id stack_frame_id;
11691 struct frame_id caller_frame_id;
cfc31633
PA
11692 struct breakpoint *location_breakpoint;
11693 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11694 struct cleanup *old_chain, *cleanup;
186c406b
TT
11695 int thread;
11696 struct thread_info *tp;
f00aae0f 11697 struct event_location *location;
cfc31633 11698 struct until_break_fsm *sm;
c906108c 11699
70509625 11700 clear_proceed_status (0);
c906108c
SS
11701
11702 /* Set a breakpoint where the user wants it and at return from
4a64f543 11703 this function. */
c5aa993b 11704
f00aae0f
KS
11705 location = string_to_event_location (&arg, current_language);
11706 cleanup = make_cleanup_delete_event_location (location);
11707
1bfeeb0f 11708 if (last_displayed_sal_is_valid ())
c2f4122d 11709 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11710 get_last_displayed_symtab (),
f8eba3c6 11711 get_last_displayed_line ());
c906108c 11712 else
f00aae0f 11713 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11714 NULL, (struct symtab *) NULL, 0);
c5aa993b 11715
c906108c 11716 if (sals.nelts != 1)
8a3fe4f8 11717 error (_("Couldn't get information on specified line."));
c5aa993b 11718
c906108c 11719 sal = sals.sals[0];
4a64f543 11720 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11721
c906108c 11722 if (*arg)
8a3fe4f8 11723 error (_("Junk at end of arguments."));
c5aa993b 11724
c906108c 11725 resolve_sal_pc (&sal);
c5aa993b 11726
186c406b 11727 tp = inferior_thread ();
5d5658a1 11728 thread = tp->global_num;
186c406b 11729
883bc8d1
PA
11730 old_chain = make_cleanup (null_cleanup, NULL);
11731
8556afb4
PA
11732 /* Note linespec handling above invalidates the frame chain.
11733 Installing a breakpoint also invalidates the frame chain (as it
11734 may need to switch threads), so do any frame handling before
11735 that. */
11736
11737 frame = get_selected_frame (NULL);
11738 frame_gdbarch = get_frame_arch (frame);
11739 stack_frame_id = get_stack_frame_id (frame);
11740 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11741
ae66c1fc
EZ
11742 /* Keep within the current frame, or in frames called by the current
11743 one. */
edb3359d 11744
883bc8d1 11745 if (frame_id_p (caller_frame_id))
c906108c 11746 {
883bc8d1 11747 struct symtab_and_line sal2;
cfc31633 11748 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11749
11750 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11751 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11752 caller_gdbarch = frame_unwind_caller_arch (frame);
11753 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11754 sal2,
11755 caller_frame_id,
11756 bp_until);
11757 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11758
883bc8d1 11759 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11760 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11761 }
c5aa993b 11762
c70a6932
JK
11763 /* set_momentary_breakpoint could invalidate FRAME. */
11764 frame = NULL;
11765
883bc8d1
PA
11766 if (anywhere)
11767 /* If the user told us to continue until a specified location,
11768 we don't specify a frame at which we need to stop. */
cfc31633
PA
11769 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11770 null_frame_id, bp_until);
883bc8d1
PA
11771 else
11772 /* Otherwise, specify the selected frame, because we want to stop
11773 only at the very same frame. */
cfc31633
PA
11774 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11775 stack_frame_id, bp_until);
11776 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11777
5d5658a1
PA
11778 sm = new_until_break_fsm (tp->global_num,
11779 location_breakpoint, caller_breakpoint);
cfc31633 11780 tp->thread_fsm = &sm->thread_fsm;
f107f563 11781
cfc31633 11782 discard_cleanups (old_chain);
f107f563 11783
cfc31633 11784 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11785
11786 do_cleanups (cleanup);
c906108c 11787}
ae66c1fc 11788
c906108c
SS
11789/* This function attempts to parse an optional "if <cond>" clause
11790 from the arg string. If one is not found, it returns NULL.
c5aa993b 11791
c906108c
SS
11792 Else, it returns a pointer to the condition string. (It does not
11793 attempt to evaluate the string against a particular block.) And,
11794 it updates arg to point to the first character following the parsed
4a64f543 11795 if clause in the arg string. */
53a5351d 11796
916703c0 11797char *
fba45db2 11798ep_parse_optional_if_clause (char **arg)
c906108c 11799{
c5aa993b
JM
11800 char *cond_string;
11801
11802 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11803 return NULL;
c5aa993b 11804
4a64f543 11805 /* Skip the "if" keyword. */
c906108c 11806 (*arg) += 2;
c5aa993b 11807
c906108c 11808 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11809 condition string. */
e9cafbcc 11810 *arg = skip_spaces (*arg);
c906108c 11811 cond_string = *arg;
c5aa993b 11812
4a64f543
MS
11813 /* Assume that the condition occupies the remainder of the arg
11814 string. */
c906108c 11815 (*arg) += strlen (cond_string);
c5aa993b 11816
c906108c
SS
11817 return cond_string;
11818}
c5aa993b 11819
c906108c
SS
11820/* Commands to deal with catching events, such as signals, exceptions,
11821 process start/exit, etc. */
c5aa993b
JM
11822
11823typedef enum
11824{
44feb3ce
TT
11825 catch_fork_temporary, catch_vfork_temporary,
11826 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11827}
11828catch_fork_kind;
11829
c906108c 11830static void
cc59ec59
MS
11831catch_fork_command_1 (char *arg, int from_tty,
11832 struct cmd_list_element *command)
c906108c 11833{
a6d9a66e 11834 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11835 char *cond_string = NULL;
44feb3ce
TT
11836 catch_fork_kind fork_kind;
11837 int tempflag;
11838
11839 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11840 tempflag = (fork_kind == catch_fork_temporary
11841 || fork_kind == catch_vfork_temporary);
c5aa993b 11842
44feb3ce
TT
11843 if (!arg)
11844 arg = "";
e9cafbcc 11845 arg = skip_spaces (arg);
c5aa993b 11846
c906108c 11847 /* The allowed syntax is:
c5aa993b
JM
11848 catch [v]fork
11849 catch [v]fork if <cond>
11850
4a64f543 11851 First, check if there's an if clause. */
c906108c 11852 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11853
c906108c 11854 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11855 error (_("Junk at end of arguments."));
c5aa993b 11856
c906108c 11857 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11858 and enable reporting of such events. */
c5aa993b
JM
11859 switch (fork_kind)
11860 {
44feb3ce
TT
11861 case catch_fork_temporary:
11862 case catch_fork_permanent:
a6d9a66e 11863 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11864 &catch_fork_breakpoint_ops);
c906108c 11865 break;
44feb3ce
TT
11866 case catch_vfork_temporary:
11867 case catch_vfork_permanent:
a6d9a66e 11868 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11869 &catch_vfork_breakpoint_ops);
c906108c 11870 break;
c5aa993b 11871 default:
8a3fe4f8 11872 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11873 break;
c5aa993b 11874 }
c906108c
SS
11875}
11876
11877static void
cc59ec59
MS
11878catch_exec_command_1 (char *arg, int from_tty,
11879 struct cmd_list_element *command)
c906108c 11880{
b4d90040 11881 struct exec_catchpoint *c;
a6d9a66e 11882 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11883 int tempflag;
c5aa993b 11884 char *cond_string = NULL;
c906108c 11885
44feb3ce
TT
11886 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11887
11888 if (!arg)
11889 arg = "";
e9cafbcc 11890 arg = skip_spaces (arg);
c906108c
SS
11891
11892 /* The allowed syntax is:
c5aa993b
JM
11893 catch exec
11894 catch exec if <cond>
c906108c 11895
4a64f543 11896 First, check if there's an if clause. */
c906108c
SS
11897 cond_string = ep_parse_optional_if_clause (&arg);
11898
11899 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11900 error (_("Junk at end of arguments."));
c906108c 11901
b4d90040
PA
11902 c = XNEW (struct exec_catchpoint);
11903 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11904 &catch_exec_breakpoint_ops);
11905 c->exec_pathname = NULL;
11906
3ea46bff 11907 install_breakpoint (0, &c->base, 1);
c906108c 11908}
c5aa993b 11909
9ac4176b 11910void
28010a5d
PA
11911init_ada_exception_breakpoint (struct breakpoint *b,
11912 struct gdbarch *gdbarch,
11913 struct symtab_and_line sal,
11914 char *addr_string,
c0a91b2b 11915 const struct breakpoint_ops *ops,
28010a5d 11916 int tempflag,
349774ef 11917 int enabled,
28010a5d 11918 int from_tty)
f7f9143b 11919{
f7f9143b
JB
11920 if (from_tty)
11921 {
5af949e3
UW
11922 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11923 if (!loc_gdbarch)
11924 loc_gdbarch = gdbarch;
11925
6c95b8df
PA
11926 describe_other_breakpoints (loc_gdbarch,
11927 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11928 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11929 version for exception catchpoints, because two catchpoints
11930 used for different exception names will use the same address.
11931 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11932 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11933 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11934 the user what type of catchpoint it is. The above is good
11935 enough for now, though. */
11936 }
11937
28010a5d 11938 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11939
349774ef 11940 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11941 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11942 b->location = string_to_event_location (&addr_string,
11943 language_def (language_ada));
f7f9143b 11944 b->language = language_ada;
f7f9143b
JB
11945}
11946
c906108c 11947static void
fba45db2 11948catch_command (char *arg, int from_tty)
c906108c 11949{
44feb3ce 11950 error (_("Catch requires an event name."));
c906108c
SS
11951}
11952\f
11953
11954static void
fba45db2 11955tcatch_command (char *arg, int from_tty)
c906108c 11956{
44feb3ce 11957 error (_("Catch requires an event name."));
c906108c
SS
11958}
11959
8a2c437b
TT
11960/* A qsort comparison function that sorts breakpoints in order. */
11961
11962static int
11963compare_breakpoints (const void *a, const void *b)
11964{
9a3c8263 11965 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11966 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11967 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11968 uintptr_t ub = (uintptr_t) *bb;
11969
11970 if ((*ba)->number < (*bb)->number)
11971 return -1;
11972 else if ((*ba)->number > (*bb)->number)
11973 return 1;
11974
11975 /* Now sort by address, in case we see, e..g, two breakpoints with
11976 the number 0. */
11977 if (ua < ub)
11978 return -1;
94b0e70d 11979 return ua > ub ? 1 : 0;
8a2c437b
TT
11980}
11981
80f8a6eb 11982/* Delete breakpoints by address or line. */
c906108c
SS
11983
11984static void
fba45db2 11985clear_command (char *arg, int from_tty)
c906108c 11986{
8a2c437b 11987 struct breakpoint *b, *prev;
d6e956e5
VP
11988 VEC(breakpoint_p) *found = 0;
11989 int ix;
c906108c
SS
11990 int default_match;
11991 struct symtabs_and_lines sals;
11992 struct symtab_and_line sal;
c906108c 11993 int i;
8a2c437b 11994 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11995
11996 if (arg)
11997 {
39cf75f7
DE
11998 sals = decode_line_with_current_source (arg,
11999 (DECODE_LINE_FUNFIRSTLINE
12000 | DECODE_LINE_LIST_MODE));
cf4ded82 12001 make_cleanup (xfree, sals.sals);
c906108c
SS
12002 default_match = 0;
12003 }
12004 else
12005 {
8d749320 12006 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12007 make_cleanup (xfree, sals.sals);
4a64f543 12008 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12009
12010 /* Set sal's line, symtab, pc, and pspace to the values
12011 corresponding to the last call to print_frame_info. If the
12012 codepoint is not valid, this will set all the fields to 0. */
12013 get_last_displayed_sal (&sal);
c906108c 12014 if (sal.symtab == 0)
8a3fe4f8 12015 error (_("No source file specified."));
c906108c
SS
12016
12017 sals.sals[0] = sal;
12018 sals.nelts = 1;
12019
12020 default_match = 1;
12021 }
12022
4a64f543
MS
12023 /* We don't call resolve_sal_pc here. That's not as bad as it
12024 seems, because all existing breakpoints typically have both
12025 file/line and pc set. So, if clear is given file/line, we can
12026 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12027
12028 We only support clearing given the address explicitly
12029 present in breakpoint table. Say, we've set breakpoint
4a64f543 12030 at file:line. There were several PC values for that file:line,
ed0616c6 12031 due to optimization, all in one block.
4a64f543
MS
12032
12033 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12034 PC corresponding to the same file:line, the breakpoint won't
12035 be cleared. We probably can still clear the breakpoint, but
12036 since the other PC value is never presented to user, user
12037 can only find it by guessing, and it does not seem important
12038 to support that. */
12039
4a64f543
MS
12040 /* For each line spec given, delete bps which correspond to it. Do
12041 it in two passes, solely to preserve the current behavior that
12042 from_tty is forced true if we delete more than one
12043 breakpoint. */
c906108c 12044
80f8a6eb 12045 found = NULL;
8a2c437b 12046 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12047 for (i = 0; i < sals.nelts; i++)
12048 {
05cba821
JK
12049 const char *sal_fullname;
12050
c906108c 12051 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12052 If line given (pc == 0), clear all bpts on specified line.
12053 If defaulting, clear all bpts on default line
c906108c 12054 or at default pc.
c5aa993b
JM
12055
12056 defaulting sal.pc != 0 tests to do
12057
12058 0 1 pc
12059 1 1 pc _and_ line
12060 0 0 line
12061 1 0 <can't happen> */
c906108c
SS
12062
12063 sal = sals.sals[i];
05cba821
JK
12064 sal_fullname = (sal.symtab == NULL
12065 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12066
4a64f543 12067 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12068 ALL_BREAKPOINTS (b)
c5aa993b 12069 {
0d381245 12070 int match = 0;
4a64f543 12071 /* Are we going to delete b? */
cc60f2e3 12072 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12073 {
12074 struct bp_location *loc = b->loc;
12075 for (; loc; loc = loc->next)
12076 {
f8eba3c6
TT
12077 /* If the user specified file:line, don't allow a PC
12078 match. This matches historical gdb behavior. */
12079 int pc_match = (!sal.explicit_line
12080 && sal.pc
12081 && (loc->pspace == sal.pspace)
12082 && (loc->address == sal.pc)
12083 && (!section_is_overlay (loc->section)
12084 || loc->section == sal.section));
4aac40c8
TT
12085 int line_match = 0;
12086
12087 if ((default_match || sal.explicit_line)
2f202fde 12088 && loc->symtab != NULL
05cba821 12089 && sal_fullname != NULL
4aac40c8 12090 && sal.pspace == loc->pspace
05cba821
JK
12091 && loc->line_number == sal.line
12092 && filename_cmp (symtab_to_fullname (loc->symtab),
12093 sal_fullname) == 0)
12094 line_match = 1;
4aac40c8 12095
0d381245
VP
12096 if (pc_match || line_match)
12097 {
12098 match = 1;
12099 break;
12100 }
12101 }
12102 }
12103
12104 if (match)
d6e956e5 12105 VEC_safe_push(breakpoint_p, found, b);
c906108c 12106 }
80f8a6eb 12107 }
8a2c437b 12108
80f8a6eb 12109 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12110 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12111 {
12112 if (arg)
8a3fe4f8 12113 error (_("No breakpoint at %s."), arg);
80f8a6eb 12114 else
8a3fe4f8 12115 error (_("No breakpoint at this line."));
80f8a6eb 12116 }
c906108c 12117
8a2c437b
TT
12118 /* Remove duplicates from the vec. */
12119 qsort (VEC_address (breakpoint_p, found),
12120 VEC_length (breakpoint_p, found),
12121 sizeof (breakpoint_p),
12122 compare_breakpoints);
12123 prev = VEC_index (breakpoint_p, found, 0);
12124 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12125 {
12126 if (b == prev)
12127 {
12128 VEC_ordered_remove (breakpoint_p, found, ix);
12129 --ix;
12130 }
12131 }
12132
d6e956e5 12133 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12134 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12135 if (from_tty)
a3f17187 12136 {
d6e956e5 12137 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12138 printf_unfiltered (_("Deleted breakpoint "));
12139 else
12140 printf_unfiltered (_("Deleted breakpoints "));
12141 }
d6e956e5
VP
12142
12143 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12144 {
c5aa993b 12145 if (from_tty)
d6e956e5
VP
12146 printf_unfiltered ("%d ", b->number);
12147 delete_breakpoint (b);
c906108c 12148 }
80f8a6eb
MS
12149 if (from_tty)
12150 putchar_unfiltered ('\n');
8a2c437b
TT
12151
12152 do_cleanups (cleanups);
c906108c
SS
12153}
12154\f
12155/* Delete breakpoint in BS if they are `delete' breakpoints and
12156 all breakpoints that are marked for deletion, whether hit or not.
12157 This is called after any breakpoint is hit, or after errors. */
12158
12159void
fba45db2 12160breakpoint_auto_delete (bpstat bs)
c906108c 12161{
35df4500 12162 struct breakpoint *b, *b_tmp;
c906108c
SS
12163
12164 for (; bs; bs = bs->next)
f431efe5
PA
12165 if (bs->breakpoint_at
12166 && bs->breakpoint_at->disposition == disp_del
c906108c 12167 && bs->stop)
f431efe5 12168 delete_breakpoint (bs->breakpoint_at);
c906108c 12169
35df4500 12170 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12171 {
b5de0fa7 12172 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12173 delete_breakpoint (b);
12174 }
c906108c
SS
12175}
12176
4a64f543
MS
12177/* A comparison function for bp_location AP and BP being interfaced to
12178 qsort. Sort elements primarily by their ADDRESS (no matter what
12179 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12180 secondarily by ordering first permanent elements and
4a64f543 12181 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12182 qsort being an unstable algorithm. */
876fa593
JK
12183
12184static int
494cfb0f 12185bp_location_compare (const void *ap, const void *bp)
876fa593 12186{
9a3c8263
SM
12187 const struct bp_location *a = *(const struct bp_location **) ap;
12188 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12189
12190 if (a->address != b->address)
12191 return (a->address > b->address) - (a->address < b->address);
12192
dea2aa5f
LM
12193 /* Sort locations at the same address by their pspace number, keeping
12194 locations of the same inferior (in a multi-inferior environment)
12195 grouped. */
12196
12197 if (a->pspace->num != b->pspace->num)
12198 return ((a->pspace->num > b->pspace->num)
12199 - (a->pspace->num < b->pspace->num));
12200
876fa593 12201 /* Sort permanent breakpoints first. */
1a853c52
PA
12202 if (a->permanent != b->permanent)
12203 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12204
c56a97f9
JK
12205 /* Make the internal GDB representation stable across GDB runs
12206 where A and B memory inside GDB can differ. Breakpoint locations of
12207 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12208
12209 if (a->owner->number != b->owner->number)
c56a97f9
JK
12210 return ((a->owner->number > b->owner->number)
12211 - (a->owner->number < b->owner->number));
876fa593
JK
12212
12213 return (a > b) - (a < b);
12214}
12215
876fa593 12216/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12217 bp_location_shadow_len_after_address_max according to the current
12218 content of the bp_location array. */
f7545552
TT
12219
12220static void
876fa593 12221bp_location_target_extensions_update (void)
f7545552 12222{
876fa593
JK
12223 struct bp_location *bl, **blp_tmp;
12224
12225 bp_location_placed_address_before_address_max = 0;
12226 bp_location_shadow_len_after_address_max = 0;
12227
12228 ALL_BP_LOCATIONS (bl, blp_tmp)
12229 {
12230 CORE_ADDR start, end, addr;
12231
12232 if (!bp_location_has_shadow (bl))
12233 continue;
12234
12235 start = bl->target_info.placed_address;
12236 end = start + bl->target_info.shadow_len;
12237
12238 gdb_assert (bl->address >= start);
12239 addr = bl->address - start;
12240 if (addr > bp_location_placed_address_before_address_max)
12241 bp_location_placed_address_before_address_max = addr;
12242
12243 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12244
12245 gdb_assert (bl->address < end);
12246 addr = end - bl->address;
12247 if (addr > bp_location_shadow_len_after_address_max)
12248 bp_location_shadow_len_after_address_max = addr;
12249 }
f7545552
TT
12250}
12251
1e4d1764
YQ
12252/* Download tracepoint locations if they haven't been. */
12253
12254static void
12255download_tracepoint_locations (void)
12256{
7ed2c994 12257 struct breakpoint *b;
1e4d1764 12258 struct cleanup *old_chain;
dd2e65cc 12259 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12260
12261 old_chain = save_current_space_and_thread ();
12262
7ed2c994 12263 ALL_TRACEPOINTS (b)
1e4d1764 12264 {
7ed2c994 12265 struct bp_location *bl;
1e4d1764 12266 struct tracepoint *t;
f2a8bc8a 12267 int bp_location_downloaded = 0;
1e4d1764 12268
7ed2c994 12269 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12270 ? !may_insert_fast_tracepoints
12271 : !may_insert_tracepoints))
12272 continue;
12273
dd2e65cc
YQ
12274 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12275 {
12276 if (target_can_download_tracepoint ())
12277 can_download_tracepoint = TRIBOOL_TRUE;
12278 else
12279 can_download_tracepoint = TRIBOOL_FALSE;
12280 }
12281
12282 if (can_download_tracepoint == TRIBOOL_FALSE)
12283 break;
12284
7ed2c994
YQ
12285 for (bl = b->loc; bl; bl = bl->next)
12286 {
12287 /* In tracepoint, locations are _never_ duplicated, so
12288 should_be_inserted is equivalent to
12289 unduplicated_should_be_inserted. */
12290 if (!should_be_inserted (bl) || bl->inserted)
12291 continue;
1e4d1764 12292
7ed2c994 12293 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12294
7ed2c994 12295 target_download_tracepoint (bl);
1e4d1764 12296
7ed2c994 12297 bl->inserted = 1;
f2a8bc8a 12298 bp_location_downloaded = 1;
7ed2c994
YQ
12299 }
12300 t = (struct tracepoint *) b;
12301 t->number_on_target = b->number;
f2a8bc8a
YQ
12302 if (bp_location_downloaded)
12303 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12304 }
12305
12306 do_cleanups (old_chain);
12307}
12308
934709f0
PW
12309/* Swap the insertion/duplication state between two locations. */
12310
12311static void
12312swap_insertion (struct bp_location *left, struct bp_location *right)
12313{
12314 const int left_inserted = left->inserted;
12315 const int left_duplicate = left->duplicate;
b775012e 12316 const int left_needs_update = left->needs_update;
934709f0
PW
12317 const struct bp_target_info left_target_info = left->target_info;
12318
1e4d1764
YQ
12319 /* Locations of tracepoints can never be duplicated. */
12320 if (is_tracepoint (left->owner))
12321 gdb_assert (!left->duplicate);
12322 if (is_tracepoint (right->owner))
12323 gdb_assert (!right->duplicate);
12324
934709f0
PW
12325 left->inserted = right->inserted;
12326 left->duplicate = right->duplicate;
b775012e 12327 left->needs_update = right->needs_update;
934709f0
PW
12328 left->target_info = right->target_info;
12329 right->inserted = left_inserted;
12330 right->duplicate = left_duplicate;
b775012e 12331 right->needs_update = left_needs_update;
934709f0
PW
12332 right->target_info = left_target_info;
12333}
12334
b775012e
LM
12335/* Force the re-insertion of the locations at ADDRESS. This is called
12336 once a new/deleted/modified duplicate location is found and we are evaluating
12337 conditions on the target's side. Such conditions need to be updated on
12338 the target. */
12339
12340static void
12341force_breakpoint_reinsertion (struct bp_location *bl)
12342{
12343 struct bp_location **locp = NULL, **loc2p;
12344 struct bp_location *loc;
12345 CORE_ADDR address = 0;
12346 int pspace_num;
12347
12348 address = bl->address;
12349 pspace_num = bl->pspace->num;
12350
12351 /* This is only meaningful if the target is
12352 evaluating conditions and if the user has
12353 opted for condition evaluation on the target's
12354 side. */
12355 if (gdb_evaluates_breakpoint_condition_p ()
12356 || !target_supports_evaluation_of_breakpoint_conditions ())
12357 return;
12358
12359 /* Flag all breakpoint locations with this address and
12360 the same program space as the location
12361 as "its condition has changed". We need to
12362 update the conditions on the target's side. */
12363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12364 {
12365 loc = *loc2p;
12366
12367 if (!is_breakpoint (loc->owner)
12368 || pspace_num != loc->pspace->num)
12369 continue;
12370
12371 /* Flag the location appropriately. We use a different state to
12372 let everyone know that we already updated the set of locations
12373 with addr bl->address and program space bl->pspace. This is so
12374 we don't have to keep calling these functions just to mark locations
12375 that have already been marked. */
12376 loc->condition_changed = condition_updated;
12377
12378 /* Free the agent expression bytecode as well. We will compute
12379 it later on. */
12380 if (loc->cond_bytecode)
12381 {
12382 free_agent_expr (loc->cond_bytecode);
12383 loc->cond_bytecode = NULL;
12384 }
12385 }
12386}
44702360
PA
12387/* Called whether new breakpoints are created, or existing breakpoints
12388 deleted, to update the global location list and recompute which
12389 locations are duplicate of which.
b775012e 12390
04086b45
PA
12391 The INSERT_MODE flag determines whether locations may not, may, or
12392 shall be inserted now. See 'enum ugll_insert_mode' for more
12393 info. */
b60e7edf 12394
0d381245 12395static void
44702360 12396update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12397{
74960c60 12398 struct breakpoint *b;
876fa593 12399 struct bp_location **locp, *loc;
f7545552 12400 struct cleanup *cleanups;
b775012e
LM
12401 /* Last breakpoint location address that was marked for update. */
12402 CORE_ADDR last_addr = 0;
12403 /* Last breakpoint location program space that was marked for update. */
12404 int last_pspace_num = -1;
f7545552 12405
2d134ed3
PA
12406 /* Used in the duplicates detection below. When iterating over all
12407 bp_locations, points to the first bp_location of a given address.
12408 Breakpoints and watchpoints of different types are never
12409 duplicates of each other. Keep one pointer for each type of
12410 breakpoint/watchpoint, so we only need to loop over all locations
12411 once. */
12412 struct bp_location *bp_loc_first; /* breakpoint */
12413 struct bp_location *wp_loc_first; /* hardware watchpoint */
12414 struct bp_location *awp_loc_first; /* access watchpoint */
12415 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12416
4a64f543
MS
12417 /* Saved former bp_location array which we compare against the newly
12418 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12419 struct bp_location **old_location, **old_locp;
12420 unsigned old_location_count;
12421
12422 old_location = bp_location;
12423 old_location_count = bp_location_count;
12424 bp_location = NULL;
12425 bp_location_count = 0;
12426 cleanups = make_cleanup (xfree, old_location);
0d381245 12427
74960c60 12428 ALL_BREAKPOINTS (b)
876fa593
JK
12429 for (loc = b->loc; loc; loc = loc->next)
12430 bp_location_count++;
12431
8d749320 12432 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12433 locp = bp_location;
12434 ALL_BREAKPOINTS (b)
12435 for (loc = b->loc; loc; loc = loc->next)
12436 *locp++ = loc;
12437 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12438 bp_location_compare);
876fa593
JK
12439
12440 bp_location_target_extensions_update ();
74960c60 12441
4a64f543
MS
12442 /* Identify bp_location instances that are no longer present in the
12443 new list, and therefore should be freed. Note that it's not
12444 necessary that those locations should be removed from inferior --
12445 if there's another location at the same address (previously
12446 marked as duplicate), we don't need to remove/insert the
12447 location.
876fa593 12448
4a64f543
MS
12449 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12450 and former bp_location array state respectively. */
876fa593
JK
12451
12452 locp = bp_location;
12453 for (old_locp = old_location; old_locp < old_location + old_location_count;
12454 old_locp++)
74960c60 12455 {
876fa593 12456 struct bp_location *old_loc = *old_locp;
c7d46a38 12457 struct bp_location **loc2p;
876fa593 12458
e5dd4106 12459 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12460 not, we have to free it. */
c7d46a38 12461 int found_object = 0;
20874c92
VP
12462 /* Tells if the location should remain inserted in the target. */
12463 int keep_in_target = 0;
12464 int removed = 0;
876fa593 12465
4a64f543
MS
12466 /* Skip LOCP entries which will definitely never be needed.
12467 Stop either at or being the one matching OLD_LOC. */
876fa593 12468 while (locp < bp_location + bp_location_count
c7d46a38 12469 && (*locp)->address < old_loc->address)
876fa593 12470 locp++;
c7d46a38
PA
12471
12472 for (loc2p = locp;
12473 (loc2p < bp_location + bp_location_count
12474 && (*loc2p)->address == old_loc->address);
12475 loc2p++)
12476 {
b775012e
LM
12477 /* Check if this is a new/duplicated location or a duplicated
12478 location that had its condition modified. If so, we want to send
12479 its condition to the target if evaluation of conditions is taking
12480 place there. */
12481 if ((*loc2p)->condition_changed == condition_modified
12482 && (last_addr != old_loc->address
12483 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12484 {
b775012e
LM
12485 force_breakpoint_reinsertion (*loc2p);
12486 last_pspace_num = old_loc->pspace->num;
c7d46a38 12487 }
b775012e
LM
12488
12489 if (*loc2p == old_loc)
12490 found_object = 1;
c7d46a38 12491 }
74960c60 12492
b775012e
LM
12493 /* We have already handled this address, update it so that we don't
12494 have to go through updates again. */
12495 last_addr = old_loc->address;
12496
12497 /* Target-side condition evaluation: Handle deleted locations. */
12498 if (!found_object)
12499 force_breakpoint_reinsertion (old_loc);
12500
4a64f543
MS
12501 /* If this location is no longer present, and inserted, look if
12502 there's maybe a new location at the same address. If so,
12503 mark that one inserted, and don't remove this one. This is
12504 needed so that we don't have a time window where a breakpoint
12505 at certain location is not inserted. */
74960c60 12506
876fa593 12507 if (old_loc->inserted)
0d381245 12508 {
4a64f543
MS
12509 /* If the location is inserted now, we might have to remove
12510 it. */
74960c60 12511
876fa593 12512 if (found_object && should_be_inserted (old_loc))
74960c60 12513 {
4a64f543
MS
12514 /* The location is still present in the location list,
12515 and still should be inserted. Don't do anything. */
20874c92 12516 keep_in_target = 1;
74960c60
VP
12517 }
12518 else
12519 {
b775012e
LM
12520 /* This location still exists, but it won't be kept in the
12521 target since it may have been disabled. We proceed to
12522 remove its target-side condition. */
12523
4a64f543
MS
12524 /* The location is either no longer present, or got
12525 disabled. See if there's another location at the
12526 same address, in which case we don't need to remove
12527 this one from the target. */
876fa593 12528
2bdf28a0 12529 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12530 if (breakpoint_address_is_meaningful (old_loc->owner))
12531 {
876fa593 12532 for (loc2p = locp;
c7d46a38
PA
12533 (loc2p < bp_location + bp_location_count
12534 && (*loc2p)->address == old_loc->address);
876fa593
JK
12535 loc2p++)
12536 {
12537 struct bp_location *loc2 = *loc2p;
12538
2d134ed3 12539 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12540 {
85d721b8
PA
12541 /* Read watchpoint locations are switched to
12542 access watchpoints, if the former are not
12543 supported, but the latter are. */
12544 if (is_hardware_watchpoint (old_loc->owner))
12545 {
12546 gdb_assert (is_hardware_watchpoint (loc2->owner));
12547 loc2->watchpoint_type = old_loc->watchpoint_type;
12548 }
12549
934709f0
PW
12550 /* loc2 is a duplicated location. We need to check
12551 if it should be inserted in case it will be
12552 unduplicated. */
12553 if (loc2 != old_loc
12554 && unduplicated_should_be_inserted (loc2))
c7d46a38 12555 {
934709f0 12556 swap_insertion (old_loc, loc2);
c7d46a38
PA
12557 keep_in_target = 1;
12558 break;
12559 }
876fa593
JK
12560 }
12561 }
12562 }
74960c60
VP
12563 }
12564
20874c92
VP
12565 if (!keep_in_target)
12566 {
876fa593 12567 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12568 {
4a64f543
MS
12569 /* This is just about all we can do. We could keep
12570 this location on the global list, and try to
12571 remove it next time, but there's no particular
12572 reason why we will succeed next time.
20874c92 12573
4a64f543
MS
12574 Note that at this point, old_loc->owner is still
12575 valid, as delete_breakpoint frees the breakpoint
12576 only after calling us. */
3e43a32a
MS
12577 printf_filtered (_("warning: Error removing "
12578 "breakpoint %d\n"),
876fa593 12579 old_loc->owner->number);
20874c92
VP
12580 }
12581 removed = 1;
12582 }
0d381245 12583 }
74960c60
VP
12584
12585 if (!found_object)
1c5cfe86 12586 {
fbea99ea 12587 if (removed && target_is_non_stop_p ()
1cf4d951 12588 && need_moribund_for_location_type (old_loc))
20874c92 12589 {
db82e815
PA
12590 /* This location was removed from the target. In
12591 non-stop mode, a race condition is possible where
12592 we've removed a breakpoint, but stop events for that
12593 breakpoint are already queued and will arrive later.
12594 We apply an heuristic to be able to distinguish such
12595 SIGTRAPs from other random SIGTRAPs: we keep this
12596 breakpoint location for a bit, and will retire it
12597 after we see some number of events. The theory here
12598 is that reporting of events should, "on the average",
12599 be fair, so after a while we'll see events from all
12600 threads that have anything of interest, and no longer
12601 need to keep this breakpoint location around. We
12602 don't hold locations forever so to reduce chances of
12603 mistaking a non-breakpoint SIGTRAP for a breakpoint
12604 SIGTRAP.
12605
12606 The heuristic failing can be disastrous on
12607 decr_pc_after_break targets.
12608
12609 On decr_pc_after_break targets, like e.g., x86-linux,
12610 if we fail to recognize a late breakpoint SIGTRAP,
12611 because events_till_retirement has reached 0 too
12612 soon, we'll fail to do the PC adjustment, and report
12613 a random SIGTRAP to the user. When the user resumes
12614 the inferior, it will most likely immediately crash
2dec564e 12615 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12616 corrupted, because of being resumed e.g., in the
12617 middle of a multi-byte instruction, or skipped a
12618 one-byte instruction. This was actually seen happen
12619 on native x86-linux, and should be less rare on
12620 targets that do not support new thread events, like
12621 remote, due to the heuristic depending on
12622 thread_count.
12623
12624 Mistaking a random SIGTRAP for a breakpoint trap
12625 causes similar symptoms (PC adjustment applied when
12626 it shouldn't), but then again, playing with SIGTRAPs
12627 behind the debugger's back is asking for trouble.
12628
12629 Since hardware watchpoint traps are always
12630 distinguishable from other traps, so we don't need to
12631 apply keep hardware watchpoint moribund locations
12632 around. We simply always ignore hardware watchpoint
12633 traps we can no longer explain. */
12634
876fa593
JK
12635 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12636 old_loc->owner = NULL;
20874c92 12637
876fa593 12638 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12639 }
12640 else
f431efe5
PA
12641 {
12642 old_loc->owner = NULL;
12643 decref_bp_location (&old_loc);
12644 }
20874c92 12645 }
74960c60 12646 }
1c5cfe86 12647
348d480f
PA
12648 /* Rescan breakpoints at the same address and section, marking the
12649 first one as "first" and any others as "duplicates". This is so
12650 that the bpt instruction is only inserted once. If we have a
12651 permanent breakpoint at the same place as BPT, make that one the
12652 official one, and the rest as duplicates. Permanent breakpoints
12653 are sorted first for the same address.
12654
12655 Do the same for hardware watchpoints, but also considering the
12656 watchpoint's type (regular/access/read) and length. */
12657
12658 bp_loc_first = NULL;
12659 wp_loc_first = NULL;
12660 awp_loc_first = NULL;
12661 rwp_loc_first = NULL;
12662 ALL_BP_LOCATIONS (loc, locp)
12663 {
12664 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12665 non-NULL. */
348d480f 12666 struct bp_location **loc_first_p;
d3fbdd86 12667 b = loc->owner;
348d480f 12668
6f380991 12669 if (!unduplicated_should_be_inserted (loc)
348d480f 12670 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12671 /* Don't detect duplicate for tracepoint locations because they are
12672 never duplicated. See the comments in field `duplicate' of
12673 `struct bp_location'. */
348d480f 12674 || is_tracepoint (b))
b775012e
LM
12675 {
12676 /* Clear the condition modification flag. */
12677 loc->condition_changed = condition_unchanged;
12678 continue;
12679 }
348d480f 12680
348d480f
PA
12681 if (b->type == bp_hardware_watchpoint)
12682 loc_first_p = &wp_loc_first;
12683 else if (b->type == bp_read_watchpoint)
12684 loc_first_p = &rwp_loc_first;
12685 else if (b->type == bp_access_watchpoint)
12686 loc_first_p = &awp_loc_first;
12687 else
12688 loc_first_p = &bp_loc_first;
12689
12690 if (*loc_first_p == NULL
12691 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12692 || !breakpoint_locations_match (loc, *loc_first_p))
12693 {
12694 *loc_first_p = loc;
12695 loc->duplicate = 0;
b775012e
LM
12696
12697 if (is_breakpoint (loc->owner) && loc->condition_changed)
12698 {
12699 loc->needs_update = 1;
12700 /* Clear the condition modification flag. */
12701 loc->condition_changed = condition_unchanged;
12702 }
348d480f
PA
12703 continue;
12704 }
12705
934709f0
PW
12706
12707 /* This and the above ensure the invariant that the first location
12708 is not duplicated, and is the inserted one.
12709 All following are marked as duplicated, and are not inserted. */
12710 if (loc->inserted)
12711 swap_insertion (loc, *loc_first_p);
348d480f
PA
12712 loc->duplicate = 1;
12713
b775012e
LM
12714 /* Clear the condition modification flag. */
12715 loc->condition_changed = condition_unchanged;
348d480f
PA
12716 }
12717
a25a5a45 12718 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12719 {
04086b45 12720 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12721 insert_breakpoint_locations ();
12722 else
12723 {
44702360
PA
12724 /* Even though the caller told us to not insert new
12725 locations, we may still need to update conditions on the
12726 target's side of breakpoints that were already inserted
12727 if the target is evaluating breakpoint conditions. We
b775012e
LM
12728 only update conditions for locations that are marked
12729 "needs_update". */
12730 update_inserted_breakpoint_locations ();
12731 }
12732 }
348d480f 12733
04086b45 12734 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12735 download_tracepoint_locations ();
12736
348d480f
PA
12737 do_cleanups (cleanups);
12738}
12739
12740void
12741breakpoint_retire_moribund (void)
12742{
12743 struct bp_location *loc;
12744 int ix;
12745
12746 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12747 if (--(loc->events_till_retirement) == 0)
12748 {
12749 decref_bp_location (&loc);
12750 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12751 --ix;
12752 }
12753}
12754
12755static void
44702360 12756update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12757{
348d480f 12758
492d29ea
PA
12759 TRY
12760 {
12761 update_global_location_list (insert_mode);
12762 }
12763 CATCH (e, RETURN_MASK_ERROR)
12764 {
12765 }
12766 END_CATCH
348d480f
PA
12767}
12768
12769/* Clear BKP from a BPS. */
12770
12771static void
12772bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12773{
12774 bpstat bs;
12775
12776 for (bs = bps; bs; bs = bs->next)
12777 if (bs->breakpoint_at == bpt)
12778 {
12779 bs->breakpoint_at = NULL;
12780 bs->old_val = NULL;
12781 /* bs->commands will be freed later. */
12782 }
12783}
12784
12785/* Callback for iterate_over_threads. */
12786static int
12787bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12788{
9a3c8263 12789 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12790
12791 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12792 return 0;
12793}
12794
12795/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12796 callbacks. */
12797
12798static void
12799say_where (struct breakpoint *b)
12800{
12801 struct value_print_options opts;
12802
12803 get_user_print_options (&opts);
12804
12805 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12806 single string. */
12807 if (b->loc == NULL)
12808 {
f00aae0f
KS
12809 /* For pending locations, the output differs slightly based
12810 on b->extra_string. If this is non-NULL, it contains either
12811 a condition or dprintf arguments. */
12812 if (b->extra_string == NULL)
12813 {
12814 printf_filtered (_(" (%s) pending."),
12815 event_location_to_string (b->location));
12816 }
12817 else if (b->type == bp_dprintf)
12818 {
12819 printf_filtered (_(" (%s,%s) pending."),
12820 event_location_to_string (b->location),
12821 b->extra_string);
12822 }
12823 else
12824 {
12825 printf_filtered (_(" (%s %s) pending."),
12826 event_location_to_string (b->location),
12827 b->extra_string);
12828 }
348d480f
PA
12829 }
12830 else
12831 {
2f202fde 12832 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12833 {
12834 printf_filtered (" at ");
12835 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12836 gdb_stdout);
12837 }
2f202fde 12838 if (b->loc->symtab != NULL)
f8eba3c6
TT
12839 {
12840 /* If there is a single location, we can print the location
12841 more nicely. */
12842 if (b->loc->next == NULL)
12843 printf_filtered (": file %s, line %d.",
05cba821
JK
12844 symtab_to_filename_for_display (b->loc->symtab),
12845 b->loc->line_number);
f8eba3c6
TT
12846 else
12847 /* This is not ideal, but each location may have a
12848 different file name, and this at least reflects the
12849 real situation somewhat. */
f00aae0f
KS
12850 printf_filtered (": %s.",
12851 event_location_to_string (b->location));
f8eba3c6 12852 }
348d480f
PA
12853
12854 if (b->loc->next)
12855 {
12856 struct bp_location *loc = b->loc;
12857 int n = 0;
12858 for (; loc; loc = loc->next)
12859 ++n;
12860 printf_filtered (" (%d locations)", n);
12861 }
12862 }
12863}
12864
348d480f
PA
12865/* Default bp_location_ops methods. */
12866
12867static void
12868bp_location_dtor (struct bp_location *self)
12869{
12870 xfree (self->cond);
b775012e
LM
12871 if (self->cond_bytecode)
12872 free_agent_expr (self->cond_bytecode);
348d480f 12873 xfree (self->function_name);
8b4f3082
PA
12874
12875 VEC_free (agent_expr_p, self->target_info.conditions);
12876 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12877}
12878
12879static const struct bp_location_ops bp_location_ops =
12880{
12881 bp_location_dtor
12882};
12883
2060206e
PA
12884/* Default breakpoint_ops methods all breakpoint_ops ultimately
12885 inherit from. */
348d480f 12886
2060206e
PA
12887static void
12888base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12889{
12890 decref_counted_command_line (&self->commands);
12891 xfree (self->cond_string);
fb81d016 12892 xfree (self->extra_string);
f8eba3c6 12893 xfree (self->filter);
f00aae0f
KS
12894 delete_event_location (self->location);
12895 delete_event_location (self->location_range_end);
348d480f
PA
12896}
12897
2060206e
PA
12898static struct bp_location *
12899base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12900{
12901 struct bp_location *loc;
12902
12903 loc = XNEW (struct bp_location);
12904 init_bp_location (loc, &bp_location_ops, self);
12905 return loc;
12906}
12907
2060206e
PA
12908static void
12909base_breakpoint_re_set (struct breakpoint *b)
12910{
12911 /* Nothing to re-set. */
12912}
12913
12914#define internal_error_pure_virtual_called() \
12915 gdb_assert_not_reached ("pure virtual function called")
12916
12917static int
12918base_breakpoint_insert_location (struct bp_location *bl)
12919{
12920 internal_error_pure_virtual_called ();
12921}
12922
12923static int
12924base_breakpoint_remove_location (struct bp_location *bl)
12925{
12926 internal_error_pure_virtual_called ();
12927}
12928
12929static int
12930base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12931 struct address_space *aspace,
09ac7c10
TT
12932 CORE_ADDR bp_addr,
12933 const struct target_waitstatus *ws)
2060206e
PA
12934{
12935 internal_error_pure_virtual_called ();
12936}
12937
12938static void
12939base_breakpoint_check_status (bpstat bs)
12940{
12941 /* Always stop. */
12942}
12943
12944/* A "works_in_software_mode" breakpoint_ops method that just internal
12945 errors. */
12946
12947static int
12948base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12949{
12950 internal_error_pure_virtual_called ();
12951}
12952
12953/* A "resources_needed" breakpoint_ops method that just internal
12954 errors. */
12955
12956static int
12957base_breakpoint_resources_needed (const struct bp_location *bl)
12958{
12959 internal_error_pure_virtual_called ();
12960}
12961
12962static enum print_stop_action
12963base_breakpoint_print_it (bpstat bs)
12964{
12965 internal_error_pure_virtual_called ();
12966}
12967
12968static void
12969base_breakpoint_print_one_detail (const struct breakpoint *self,
12970 struct ui_out *uiout)
12971{
12972 /* nothing */
12973}
12974
12975static void
12976base_breakpoint_print_mention (struct breakpoint *b)
12977{
12978 internal_error_pure_virtual_called ();
12979}
12980
12981static void
12982base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12983{
12984 internal_error_pure_virtual_called ();
12985}
12986
983af33b 12987static void
f00aae0f
KS
12988base_breakpoint_create_sals_from_location
12989 (const struct event_location *location,
12990 struct linespec_result *canonical,
12991 enum bptype type_wanted)
983af33b
SDJ
12992{
12993 internal_error_pure_virtual_called ();
12994}
12995
12996static void
12997base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12998 struct linespec_result *c,
983af33b 12999 char *cond_string,
e7e0cddf 13000 char *extra_string,
983af33b
SDJ
13001 enum bptype type_wanted,
13002 enum bpdisp disposition,
13003 int thread,
13004 int task, int ignore_count,
13005 const struct breakpoint_ops *o,
13006 int from_tty, int enabled,
44f238bb 13007 int internal, unsigned flags)
983af33b
SDJ
13008{
13009 internal_error_pure_virtual_called ();
13010}
13011
13012static void
f00aae0f
KS
13013base_breakpoint_decode_location (struct breakpoint *b,
13014 const struct event_location *location,
c2f4122d 13015 struct program_space *search_pspace,
983af33b
SDJ
13016 struct symtabs_and_lines *sals)
13017{
13018 internal_error_pure_virtual_called ();
13019}
13020
ab04a2af
TT
13021/* The default 'explains_signal' method. */
13022
47591c29 13023static int
427cd150 13024base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13025{
47591c29 13026 return 1;
ab04a2af
TT
13027}
13028
9d6e6e84
HZ
13029/* The default "after_condition_true" method. */
13030
13031static void
13032base_breakpoint_after_condition_true (struct bpstats *bs)
13033{
13034 /* Nothing to do. */
13035}
13036
ab04a2af 13037struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13038{
13039 base_breakpoint_dtor,
13040 base_breakpoint_allocate_location,
13041 base_breakpoint_re_set,
13042 base_breakpoint_insert_location,
13043 base_breakpoint_remove_location,
13044 base_breakpoint_breakpoint_hit,
13045 base_breakpoint_check_status,
13046 base_breakpoint_resources_needed,
13047 base_breakpoint_works_in_software_mode,
13048 base_breakpoint_print_it,
13049 NULL,
13050 base_breakpoint_print_one_detail,
13051 base_breakpoint_print_mention,
983af33b 13052 base_breakpoint_print_recreate,
5f700d83 13053 base_breakpoint_create_sals_from_location,
983af33b 13054 base_breakpoint_create_breakpoints_sal,
5f700d83 13055 base_breakpoint_decode_location,
9d6e6e84
HZ
13056 base_breakpoint_explains_signal,
13057 base_breakpoint_after_condition_true,
2060206e
PA
13058};
13059
13060/* Default breakpoint_ops methods. */
13061
13062static void
348d480f
PA
13063bkpt_re_set (struct breakpoint *b)
13064{
06edf0c0 13065 /* FIXME: is this still reachable? */
f00aae0f 13066 if (event_location_empty_p (b->location))
06edf0c0 13067 {
f00aae0f 13068 /* Anything without a location can't be re-set. */
348d480f 13069 delete_breakpoint (b);
06edf0c0 13070 return;
348d480f 13071 }
06edf0c0
PA
13072
13073 breakpoint_re_set_default (b);
348d480f
PA
13074}
13075
2060206e 13076static int
348d480f
PA
13077bkpt_insert_location (struct bp_location *bl)
13078{
13079 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13080 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13081 else
7c16b83e 13082 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13083}
13084
2060206e 13085static int
348d480f
PA
13086bkpt_remove_location (struct bp_location *bl)
13087{
13088 if (bl->loc_type == bp_loc_hardware_breakpoint)
13089 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13090 else
7c16b83e 13091 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13092}
13093
2060206e 13094static int
348d480f 13095bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13096 struct address_space *aspace, CORE_ADDR bp_addr,
13097 const struct target_waitstatus *ws)
348d480f 13098{
09ac7c10 13099 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13100 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13101 return 0;
13102
348d480f
PA
13103 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13104 aspace, bp_addr))
13105 return 0;
13106
13107 if (overlay_debugging /* unmapped overlay section */
13108 && section_is_overlay (bl->section)
13109 && !section_is_mapped (bl->section))
13110 return 0;
13111
13112 return 1;
13113}
13114
cd1608cc
PA
13115static int
13116dprintf_breakpoint_hit (const struct bp_location *bl,
13117 struct address_space *aspace, CORE_ADDR bp_addr,
13118 const struct target_waitstatus *ws)
13119{
13120 if (dprintf_style == dprintf_style_agent
13121 && target_can_run_breakpoint_commands ())
13122 {
13123 /* An agent-style dprintf never causes a stop. If we see a trap
13124 for this address it must be for a breakpoint that happens to
13125 be set at the same address. */
13126 return 0;
13127 }
13128
13129 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13130}
13131
2060206e 13132static int
348d480f
PA
13133bkpt_resources_needed (const struct bp_location *bl)
13134{
13135 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13136
13137 return 1;
13138}
13139
2060206e 13140static enum print_stop_action
348d480f
PA
13141bkpt_print_it (bpstat bs)
13142{
348d480f
PA
13143 struct breakpoint *b;
13144 const struct bp_location *bl;
001c8c33 13145 int bp_temp;
79a45e25 13146 struct ui_out *uiout = current_uiout;
348d480f
PA
13147
13148 gdb_assert (bs->bp_location_at != NULL);
13149
13150 bl = bs->bp_location_at;
13151 b = bs->breakpoint_at;
13152
001c8c33
PA
13153 bp_temp = b->disposition == disp_del;
13154 if (bl->address != bl->requested_address)
13155 breakpoint_adjustment_warning (bl->requested_address,
13156 bl->address,
13157 b->number, 1);
13158 annotate_breakpoint (b->number);
f303dbd6
PA
13159 maybe_print_thread_hit_breakpoint (uiout);
13160
001c8c33 13161 if (bp_temp)
f303dbd6 13162 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13163 else
f303dbd6 13164 ui_out_text (uiout, "Breakpoint ");
001c8c33 13165 if (ui_out_is_mi_like_p (uiout))
348d480f 13166 {
001c8c33
PA
13167 ui_out_field_string (uiout, "reason",
13168 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13169 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13170 }
001c8c33
PA
13171 ui_out_field_int (uiout, "bkptno", b->number);
13172 ui_out_text (uiout, ", ");
06edf0c0 13173
001c8c33 13174 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13175}
13176
2060206e 13177static void
06edf0c0
PA
13178bkpt_print_mention (struct breakpoint *b)
13179{
79a45e25 13180 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13181 return;
13182
13183 switch (b->type)
13184 {
13185 case bp_breakpoint:
13186 case bp_gnu_ifunc_resolver:
13187 if (b->disposition == disp_del)
13188 printf_filtered (_("Temporary breakpoint"));
13189 else
13190 printf_filtered (_("Breakpoint"));
13191 printf_filtered (_(" %d"), b->number);
13192 if (b->type == bp_gnu_ifunc_resolver)
13193 printf_filtered (_(" at gnu-indirect-function resolver"));
13194 break;
13195 case bp_hardware_breakpoint:
13196 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13197 break;
e7e0cddf
SS
13198 case bp_dprintf:
13199 printf_filtered (_("Dprintf %d"), b->number);
13200 break;
06edf0c0
PA
13201 }
13202
13203 say_where (b);
13204}
13205
2060206e 13206static void
06edf0c0
PA
13207bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13208{
13209 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13210 fprintf_unfiltered (fp, "tbreak");
13211 else if (tp->type == bp_breakpoint)
13212 fprintf_unfiltered (fp, "break");
13213 else if (tp->type == bp_hardware_breakpoint
13214 && tp->disposition == disp_del)
13215 fprintf_unfiltered (fp, "thbreak");
13216 else if (tp->type == bp_hardware_breakpoint)
13217 fprintf_unfiltered (fp, "hbreak");
13218 else
13219 internal_error (__FILE__, __LINE__,
13220 _("unhandled breakpoint type %d"), (int) tp->type);
13221
f00aae0f
KS
13222 fprintf_unfiltered (fp, " %s",
13223 event_location_to_string (tp->location));
13224
13225 /* Print out extra_string if this breakpoint is pending. It might
13226 contain, for example, conditions that were set by the user. */
13227 if (tp->loc == NULL && tp->extra_string != NULL)
13228 fprintf_unfiltered (fp, " %s", tp->extra_string);
13229
dd11a36c 13230 print_recreate_thread (tp, fp);
06edf0c0
PA
13231}
13232
983af33b 13233static void
f00aae0f
KS
13234bkpt_create_sals_from_location (const struct event_location *location,
13235 struct linespec_result *canonical,
13236 enum bptype type_wanted)
983af33b 13237{
f00aae0f 13238 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13239}
13240
13241static void
13242bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13243 struct linespec_result *canonical,
983af33b 13244 char *cond_string,
e7e0cddf 13245 char *extra_string,
983af33b
SDJ
13246 enum bptype type_wanted,
13247 enum bpdisp disposition,
13248 int thread,
13249 int task, int ignore_count,
13250 const struct breakpoint_ops *ops,
13251 int from_tty, int enabled,
44f238bb 13252 int internal, unsigned flags)
983af33b 13253{
023fa29b 13254 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13255 cond_string, extra_string,
13256 type_wanted,
983af33b
SDJ
13257 disposition, thread, task,
13258 ignore_count, ops, from_tty,
44f238bb 13259 enabled, internal, flags);
983af33b
SDJ
13260}
13261
13262static void
f00aae0f
KS
13263bkpt_decode_location (struct breakpoint *b,
13264 const struct event_location *location,
c2f4122d 13265 struct program_space *search_pspace,
983af33b
SDJ
13266 struct symtabs_and_lines *sals)
13267{
c2f4122d 13268 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13269}
13270
06edf0c0
PA
13271/* Virtual table for internal breakpoints. */
13272
13273static void
13274internal_bkpt_re_set (struct breakpoint *b)
13275{
13276 switch (b->type)
13277 {
13278 /* Delete overlay event and longjmp master breakpoints; they
13279 will be reset later by breakpoint_re_set. */
13280 case bp_overlay_event:
13281 case bp_longjmp_master:
13282 case bp_std_terminate_master:
13283 case bp_exception_master:
13284 delete_breakpoint (b);
13285 break;
13286
13287 /* This breakpoint is special, it's set up when the inferior
13288 starts and we really don't want to touch it. */
13289 case bp_shlib_event:
13290
13291 /* Like bp_shlib_event, this breakpoint type is special. Once
13292 it is set up, we do not want to touch it. */
13293 case bp_thread_event:
13294 break;
13295 }
13296}
13297
13298static void
13299internal_bkpt_check_status (bpstat bs)
13300{
a9b3a50f
PA
13301 if (bs->breakpoint_at->type == bp_shlib_event)
13302 {
13303 /* If requested, stop when the dynamic linker notifies GDB of
13304 events. This allows the user to get control and place
13305 breakpoints in initializer routines for dynamically loaded
13306 objects (among other things). */
13307 bs->stop = stop_on_solib_events;
13308 bs->print = stop_on_solib_events;
13309 }
13310 else
13311 bs->stop = 0;
06edf0c0
PA
13312}
13313
13314static enum print_stop_action
13315internal_bkpt_print_it (bpstat bs)
13316{
06edf0c0 13317 struct breakpoint *b;
06edf0c0 13318
06edf0c0
PA
13319 b = bs->breakpoint_at;
13320
06edf0c0
PA
13321 switch (b->type)
13322 {
348d480f
PA
13323 case bp_shlib_event:
13324 /* Did we stop because the user set the stop_on_solib_events
13325 variable? (If so, we report this as a generic, "Stopped due
13326 to shlib event" message.) */
edcc5120 13327 print_solib_event (0);
348d480f
PA
13328 break;
13329
13330 case bp_thread_event:
13331 /* Not sure how we will get here.
13332 GDB should not stop for these breakpoints. */
13333 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13334 break;
13335
13336 case bp_overlay_event:
13337 /* By analogy with the thread event, GDB should not stop for these. */
13338 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13339 break;
13340
13341 case bp_longjmp_master:
13342 /* These should never be enabled. */
13343 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13344 break;
13345
13346 case bp_std_terminate_master:
13347 /* These should never be enabled. */
13348 printf_filtered (_("std::terminate Master Breakpoint: "
13349 "gdb should not stop!\n"));
348d480f
PA
13350 break;
13351
13352 case bp_exception_master:
13353 /* These should never be enabled. */
13354 printf_filtered (_("Exception Master Breakpoint: "
13355 "gdb should not stop!\n"));
06edf0c0
PA
13356 break;
13357 }
13358
001c8c33 13359 return PRINT_NOTHING;
06edf0c0
PA
13360}
13361
13362static void
13363internal_bkpt_print_mention (struct breakpoint *b)
13364{
13365 /* Nothing to mention. These breakpoints are internal. */
13366}
13367
06edf0c0
PA
13368/* Virtual table for momentary breakpoints */
13369
13370static void
13371momentary_bkpt_re_set (struct breakpoint *b)
13372{
13373 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13374 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13375 Otherwise these should have been blown away via the cleanup chain
13376 or by breakpoint_init_inferior when we rerun the executable. */
13377}
13378
13379static void
13380momentary_bkpt_check_status (bpstat bs)
13381{
13382 /* Nothing. The point of these breakpoints is causing a stop. */
13383}
13384
13385static enum print_stop_action
13386momentary_bkpt_print_it (bpstat bs)
13387{
001c8c33 13388 return PRINT_UNKNOWN;
348d480f
PA
13389}
13390
06edf0c0
PA
13391static void
13392momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13393{
06edf0c0 13394 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13395}
13396
e2e4d78b
JK
13397/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13398
13399 It gets cleared already on the removal of the first one of such placed
13400 breakpoints. This is OK as they get all removed altogether. */
13401
13402static void
13403longjmp_bkpt_dtor (struct breakpoint *self)
13404{
5d5658a1 13405 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13406
13407 if (tp)
13408 tp->initiating_frame = null_frame_id;
13409
13410 momentary_breakpoint_ops.dtor (self);
13411}
13412
55aa24fb
SDJ
13413/* Specific methods for probe breakpoints. */
13414
13415static int
13416bkpt_probe_insert_location (struct bp_location *bl)
13417{
13418 int v = bkpt_insert_location (bl);
13419
13420 if (v == 0)
13421 {
13422 /* The insertion was successful, now let's set the probe's semaphore
13423 if needed. */
0ea5cda8
SDJ
13424 if (bl->probe.probe->pops->set_semaphore != NULL)
13425 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13426 bl->probe.objfile,
13427 bl->gdbarch);
55aa24fb
SDJ
13428 }
13429
13430 return v;
13431}
13432
13433static int
13434bkpt_probe_remove_location (struct bp_location *bl)
13435{
13436 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13437 if (bl->probe.probe->pops->clear_semaphore != NULL)
13438 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13439 bl->probe.objfile,
13440 bl->gdbarch);
55aa24fb
SDJ
13441
13442 return bkpt_remove_location (bl);
13443}
13444
13445static void
f00aae0f 13446bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13447 struct linespec_result *canonical,
f00aae0f 13448 enum bptype type_wanted)
55aa24fb
SDJ
13449{
13450 struct linespec_sals lsal;
13451
c2f4122d 13452 lsal.sals = parse_probes (location, NULL, canonical);
f00aae0f 13453 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13454 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13455}
13456
13457static void
f00aae0f
KS
13458bkpt_probe_decode_location (struct breakpoint *b,
13459 const struct event_location *location,
c2f4122d 13460 struct program_space *search_pspace,
55aa24fb
SDJ
13461 struct symtabs_and_lines *sals)
13462{
c2f4122d 13463 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13464 if (!sals->sals)
13465 error (_("probe not found"));
13466}
13467
348d480f 13468/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13469
348d480f
PA
13470static void
13471tracepoint_re_set (struct breakpoint *b)
13472{
13473 breakpoint_re_set_default (b);
13474}
876fa593 13475
348d480f
PA
13476static int
13477tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13478 struct address_space *aspace, CORE_ADDR bp_addr,
13479 const struct target_waitstatus *ws)
348d480f
PA
13480{
13481 /* By definition, the inferior does not report stops at
13482 tracepoints. */
13483 return 0;
74960c60
VP
13484}
13485
13486static void
348d480f
PA
13487tracepoint_print_one_detail (const struct breakpoint *self,
13488 struct ui_out *uiout)
74960c60 13489{
d9b3f62e
PA
13490 struct tracepoint *tp = (struct tracepoint *) self;
13491 if (tp->static_trace_marker_id)
348d480f
PA
13492 {
13493 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13494
348d480f
PA
13495 ui_out_text (uiout, "\tmarker id is ");
13496 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13497 tp->static_trace_marker_id);
348d480f
PA
13498 ui_out_text (uiout, "\n");
13499 }
0d381245
VP
13500}
13501
a474d7c2 13502static void
348d480f 13503tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13504{
79a45e25 13505 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13506 return;
cc59ec59 13507
348d480f
PA
13508 switch (b->type)
13509 {
13510 case bp_tracepoint:
13511 printf_filtered (_("Tracepoint"));
13512 printf_filtered (_(" %d"), b->number);
13513 break;
13514 case bp_fast_tracepoint:
13515 printf_filtered (_("Fast tracepoint"));
13516 printf_filtered (_(" %d"), b->number);
13517 break;
13518 case bp_static_tracepoint:
13519 printf_filtered (_("Static tracepoint"));
13520 printf_filtered (_(" %d"), b->number);
13521 break;
13522 default:
13523 internal_error (__FILE__, __LINE__,
13524 _("unhandled tracepoint type %d"), (int) b->type);
13525 }
13526
13527 say_where (b);
a474d7c2
PA
13528}
13529
348d480f 13530static void
d9b3f62e 13531tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13532{
d9b3f62e
PA
13533 struct tracepoint *tp = (struct tracepoint *) self;
13534
13535 if (self->type == bp_fast_tracepoint)
348d480f 13536 fprintf_unfiltered (fp, "ftrace");
c93e8391 13537 else if (self->type == bp_static_tracepoint)
348d480f 13538 fprintf_unfiltered (fp, "strace");
d9b3f62e 13539 else if (self->type == bp_tracepoint)
348d480f
PA
13540 fprintf_unfiltered (fp, "trace");
13541 else
13542 internal_error (__FILE__, __LINE__,
d9b3f62e 13543 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13544
f00aae0f
KS
13545 fprintf_unfiltered (fp, " %s",
13546 event_location_to_string (self->location));
d9b3f62e
PA
13547 print_recreate_thread (self, fp);
13548
13549 if (tp->pass_count)
13550 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13551}
13552
983af33b 13553static void
f00aae0f
KS
13554tracepoint_create_sals_from_location (const struct event_location *location,
13555 struct linespec_result *canonical,
13556 enum bptype type_wanted)
983af33b 13557{
f00aae0f 13558 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13559}
13560
13561static void
13562tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13563 struct linespec_result *canonical,
983af33b 13564 char *cond_string,
e7e0cddf 13565 char *extra_string,
983af33b
SDJ
13566 enum bptype type_wanted,
13567 enum bpdisp disposition,
13568 int thread,
13569 int task, int ignore_count,
13570 const struct breakpoint_ops *ops,
13571 int from_tty, int enabled,
44f238bb 13572 int internal, unsigned flags)
983af33b 13573{
023fa29b 13574 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13575 cond_string, extra_string,
13576 type_wanted,
983af33b
SDJ
13577 disposition, thread, task,
13578 ignore_count, ops, from_tty,
44f238bb 13579 enabled, internal, flags);
983af33b
SDJ
13580}
13581
13582static void
f00aae0f
KS
13583tracepoint_decode_location (struct breakpoint *b,
13584 const struct event_location *location,
c2f4122d 13585 struct program_space *search_pspace,
983af33b
SDJ
13586 struct symtabs_and_lines *sals)
13587{
c2f4122d 13588 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13589}
13590
2060206e 13591struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13592
55aa24fb
SDJ
13593/* The breakpoint_ops structure to be use on tracepoints placed in a
13594 static probe. */
13595
13596static void
f00aae0f
KS
13597tracepoint_probe_create_sals_from_location
13598 (const struct event_location *location,
13599 struct linespec_result *canonical,
13600 enum bptype type_wanted)
55aa24fb
SDJ
13601{
13602 /* We use the same method for breakpoint on probes. */
f00aae0f 13603 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13604}
13605
13606static void
f00aae0f
KS
13607tracepoint_probe_decode_location (struct breakpoint *b,
13608 const struct event_location *location,
c2f4122d 13609 struct program_space *search_pspace,
55aa24fb
SDJ
13610 struct symtabs_and_lines *sals)
13611{
13612 /* We use the same method for breakpoint on probes. */
c2f4122d 13613 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13614}
13615
13616static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13617
5c2b4418
HZ
13618/* Dprintf breakpoint_ops methods. */
13619
13620static void
13621dprintf_re_set (struct breakpoint *b)
13622{
13623 breakpoint_re_set_default (b);
13624
f00aae0f
KS
13625 /* extra_string should never be non-NULL for dprintf. */
13626 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13627
13628 /* 1 - connect to target 1, that can run breakpoint commands.
13629 2 - create a dprintf, which resolves fine.
13630 3 - disconnect from target 1
13631 4 - connect to target 2, that can NOT run breakpoint commands.
13632
13633 After steps #3/#4, you'll want the dprintf command list to
13634 be updated, because target 1 and 2 may well return different
13635 answers for target_can_run_breakpoint_commands().
13636 Given absence of finer grained resetting, we get to do
13637 it all the time. */
13638 if (b->extra_string != NULL)
13639 update_dprintf_command_list (b);
13640}
13641
2d9442cc
HZ
13642/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13643
13644static void
13645dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13646{
f00aae0f
KS
13647 fprintf_unfiltered (fp, "dprintf %s,%s",
13648 event_location_to_string (tp->location),
2d9442cc
HZ
13649 tp->extra_string);
13650 print_recreate_thread (tp, fp);
13651}
13652
9d6e6e84
HZ
13653/* Implement the "after_condition_true" breakpoint_ops method for
13654 dprintf.
13655
13656 dprintf's are implemented with regular commands in their command
13657 list, but we run the commands here instead of before presenting the
13658 stop to the user, as dprintf's don't actually cause a stop. This
13659 also makes it so that the commands of multiple dprintfs at the same
13660 address are all handled. */
13661
13662static void
13663dprintf_after_condition_true (struct bpstats *bs)
13664{
13665 struct cleanup *old_chain;
13666 struct bpstats tmp_bs = { NULL };
13667 struct bpstats *tmp_bs_p = &tmp_bs;
13668
13669 /* dprintf's never cause a stop. This wasn't set in the
13670 check_status hook instead because that would make the dprintf's
13671 condition not be evaluated. */
13672 bs->stop = 0;
13673
13674 /* Run the command list here. Take ownership of it instead of
13675 copying. We never want these commands to run later in
13676 bpstat_do_actions, if a breakpoint that causes a stop happens to
13677 be set at same address as this dprintf, or even if running the
13678 commands here throws. */
13679 tmp_bs.commands = bs->commands;
13680 bs->commands = NULL;
13681 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13682
13683 bpstat_do_actions_1 (&tmp_bs_p);
13684
13685 /* 'tmp_bs.commands' will usually be NULL by now, but
13686 bpstat_do_actions_1 may return early without processing the whole
13687 list. */
13688 do_cleanups (old_chain);
13689}
13690
983af33b
SDJ
13691/* The breakpoint_ops structure to be used on static tracepoints with
13692 markers (`-m'). */
13693
13694static void
f00aae0f 13695strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13696 struct linespec_result *canonical,
f00aae0f 13697 enum bptype type_wanted)
983af33b
SDJ
13698{
13699 struct linespec_sals lsal;
f00aae0f
KS
13700 const char *arg_start, *arg;
13701 char *str;
13702 struct cleanup *cleanup;
983af33b 13703
f00aae0f
KS
13704 arg = arg_start = get_linespec_location (location);
13705 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13706
f00aae0f
KS
13707 str = savestring (arg_start, arg - arg_start);
13708 cleanup = make_cleanup (xfree, str);
13709 canonical->location = new_linespec_location (&str);
13710 do_cleanups (cleanup);
983af33b 13711
f00aae0f 13712 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13713 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13714}
13715
13716static void
13717strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13718 struct linespec_result *canonical,
983af33b 13719 char *cond_string,
e7e0cddf 13720 char *extra_string,
983af33b
SDJ
13721 enum bptype type_wanted,
13722 enum bpdisp disposition,
13723 int thread,
13724 int task, int ignore_count,
13725 const struct breakpoint_ops *ops,
13726 int from_tty, int enabled,
44f238bb 13727 int internal, unsigned flags)
983af33b
SDJ
13728{
13729 int i;
52d361e1
YQ
13730 struct linespec_sals *lsal = VEC_index (linespec_sals,
13731 canonical->sals, 0);
983af33b
SDJ
13732
13733 /* If the user is creating a static tracepoint by marker id
13734 (strace -m MARKER_ID), then store the sals index, so that
13735 breakpoint_re_set can try to match up which of the newly
13736 found markers corresponds to this one, and, don't try to
13737 expand multiple locations for each sal, given than SALS
13738 already should contain all sals for MARKER_ID. */
13739
13740 for (i = 0; i < lsal->sals.nelts; ++i)
13741 {
13742 struct symtabs_and_lines expanded;
13743 struct tracepoint *tp;
13744 struct cleanup *old_chain;
f00aae0f 13745 struct event_location *location;
983af33b
SDJ
13746
13747 expanded.nelts = 1;
13748 expanded.sals = &lsal->sals.sals[i];
13749
f00aae0f
KS
13750 location = copy_event_location (canonical->location);
13751 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13752
13753 tp = XCNEW (struct tracepoint);
13754 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13755 location, NULL,
e7e0cddf
SS
13756 cond_string, extra_string,
13757 type_wanted, disposition,
983af33b 13758 thread, task, ignore_count, ops,
44f238bb 13759 from_tty, enabled, internal, flags,
983af33b
SDJ
13760 canonical->special_display);
13761 /* Given that its possible to have multiple markers with
13762 the same string id, if the user is creating a static
13763 tracepoint by marker id ("strace -m MARKER_ID"), then
13764 store the sals index, so that breakpoint_re_set can
13765 try to match up which of the newly found markers
13766 corresponds to this one */
13767 tp->static_trace_marker_id_idx = i;
13768
13769 install_breakpoint (internal, &tp->base, 0);
13770
13771 discard_cleanups (old_chain);
13772 }
13773}
13774
13775static void
f00aae0f
KS
13776strace_marker_decode_location (struct breakpoint *b,
13777 const struct event_location *location,
c2f4122d 13778 struct program_space *search_pspace,
983af33b
SDJ
13779 struct symtabs_and_lines *sals)
13780{
13781 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13782 const char *s = get_linespec_location (location);
983af33b 13783
f00aae0f 13784 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13785 if (sals->nelts > tp->static_trace_marker_id_idx)
13786 {
13787 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13788 sals->nelts = 1;
13789 }
13790 else
13791 error (_("marker %s not found"), tp->static_trace_marker_id);
13792}
13793
13794static struct breakpoint_ops strace_marker_breakpoint_ops;
13795
13796static int
13797strace_marker_p (struct breakpoint *b)
13798{
13799 return b->ops == &strace_marker_breakpoint_ops;
13800}
13801
53a5351d 13802/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13803 structures. */
c906108c
SS
13804
13805void
fba45db2 13806delete_breakpoint (struct breakpoint *bpt)
c906108c 13807{
52f0bd74 13808 struct breakpoint *b;
c906108c 13809
8a3fe4f8 13810 gdb_assert (bpt != NULL);
c906108c 13811
4a64f543
MS
13812 /* Has this bp already been deleted? This can happen because
13813 multiple lists can hold pointers to bp's. bpstat lists are
13814 especial culprits.
13815
13816 One example of this happening is a watchpoint's scope bp. When
13817 the scope bp triggers, we notice that the watchpoint is out of
13818 scope, and delete it. We also delete its scope bp. But the
13819 scope bp is marked "auto-deleting", and is already on a bpstat.
13820 That bpstat is then checked for auto-deleting bp's, which are
13821 deleted.
13822
13823 A real solution to this problem might involve reference counts in
13824 bp's, and/or giving them pointers back to their referencing
13825 bpstat's, and teaching delete_breakpoint to only free a bp's
13826 storage when no more references were extent. A cheaper bandaid
13827 was chosen. */
c906108c
SS
13828 if (bpt->type == bp_none)
13829 return;
13830
4a64f543
MS
13831 /* At least avoid this stale reference until the reference counting
13832 of breakpoints gets resolved. */
d0fb5eae 13833 if (bpt->related_breakpoint != bpt)
e5a0a904 13834 {
d0fb5eae 13835 struct breakpoint *related;
3a5c3e22 13836 struct watchpoint *w;
d0fb5eae
JK
13837
13838 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13839 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13840 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13841 w = (struct watchpoint *) bpt;
13842 else
13843 w = NULL;
13844 if (w != NULL)
13845 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13846
13847 /* Unlink bpt from the bpt->related_breakpoint ring. */
13848 for (related = bpt; related->related_breakpoint != bpt;
13849 related = related->related_breakpoint);
13850 related->related_breakpoint = bpt->related_breakpoint;
13851 bpt->related_breakpoint = bpt;
e5a0a904
JK
13852 }
13853
a9634178
TJB
13854 /* watch_command_1 creates a watchpoint but only sets its number if
13855 update_watchpoint succeeds in creating its bp_locations. If there's
13856 a problem in that process, we'll be asked to delete the half-created
13857 watchpoint. In that case, don't announce the deletion. */
13858 if (bpt->number)
13859 observer_notify_breakpoint_deleted (bpt);
c906108c 13860
c906108c
SS
13861 if (breakpoint_chain == bpt)
13862 breakpoint_chain = bpt->next;
13863
c906108c
SS
13864 ALL_BREAKPOINTS (b)
13865 if (b->next == bpt)
c5aa993b
JM
13866 {
13867 b->next = bpt->next;
13868 break;
13869 }
c906108c 13870
f431efe5
PA
13871 /* Be sure no bpstat's are pointing at the breakpoint after it's
13872 been freed. */
13873 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13874 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13875 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13876 commands are associated with the bpstat; if we remove it here,
13877 then the later call to bpstat_do_actions (&stop_bpstat); in
13878 event-top.c won't do anything, and temporary breakpoints with
13879 commands won't work. */
13880
13881 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13882
4a64f543
MS
13883 /* Now that breakpoint is removed from breakpoint list, update the
13884 global location list. This will remove locations that used to
13885 belong to this breakpoint. Do this before freeing the breakpoint
13886 itself, since remove_breakpoint looks at location's owner. It
13887 might be better design to have location completely
13888 self-contained, but it's not the case now. */
44702360 13889 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13890
348d480f 13891 bpt->ops->dtor (bpt);
4a64f543
MS
13892 /* On the chance that someone will soon try again to delete this
13893 same bp, we mark it as deleted before freeing its storage. */
c906108c 13894 bpt->type = bp_none;
b8c9b27d 13895 xfree (bpt);
c906108c
SS
13896}
13897
4d6140d9
AC
13898static void
13899do_delete_breakpoint_cleanup (void *b)
13900{
9a3c8263 13901 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13902}
13903
13904struct cleanup *
13905make_cleanup_delete_breakpoint (struct breakpoint *b)
13906{
13907 return make_cleanup (do_delete_breakpoint_cleanup, b);
13908}
13909
51be5b68
PA
13910/* Iterator function to call a user-provided callback function once
13911 for each of B and its related breakpoints. */
13912
13913static void
13914iterate_over_related_breakpoints (struct breakpoint *b,
13915 void (*function) (struct breakpoint *,
13916 void *),
13917 void *data)
13918{
13919 struct breakpoint *related;
13920
13921 related = b;
13922 do
13923 {
13924 struct breakpoint *next;
13925
13926 /* FUNCTION may delete RELATED. */
13927 next = related->related_breakpoint;
13928
13929 if (next == related)
13930 {
13931 /* RELATED is the last ring entry. */
13932 function (related, data);
13933
13934 /* FUNCTION may have deleted it, so we'd never reach back to
13935 B. There's nothing left to do anyway, so just break
13936 out. */
13937 break;
13938 }
13939 else
13940 function (related, data);
13941
13942 related = next;
13943 }
13944 while (related != b);
13945}
95a42b64
TT
13946
13947static void
13948do_delete_breakpoint (struct breakpoint *b, void *ignore)
13949{
13950 delete_breakpoint (b);
13951}
13952
51be5b68
PA
13953/* A callback for map_breakpoint_numbers that calls
13954 delete_breakpoint. */
13955
13956static void
13957do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13958{
13959 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13960}
13961
c906108c 13962void
fba45db2 13963delete_command (char *arg, int from_tty)
c906108c 13964{
35df4500 13965 struct breakpoint *b, *b_tmp;
c906108c 13966
ea9365bb
TT
13967 dont_repeat ();
13968
c906108c
SS
13969 if (arg == 0)
13970 {
13971 int breaks_to_delete = 0;
13972
46c6471b
PA
13973 /* Delete all breakpoints if no argument. Do not delete
13974 internal breakpoints, these have to be deleted with an
13975 explicit breakpoint number argument. */
c5aa993b 13976 ALL_BREAKPOINTS (b)
46c6471b 13977 if (user_breakpoint_p (b))
973d738b
DJ
13978 {
13979 breaks_to_delete = 1;
13980 break;
13981 }
c906108c
SS
13982
13983 /* Ask user only if there are some breakpoints to delete. */
13984 if (!from_tty
e2e0b3e5 13985 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13986 {
35df4500 13987 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13988 if (user_breakpoint_p (b))
c5aa993b 13989 delete_breakpoint (b);
c906108c
SS
13990 }
13991 }
13992 else
51be5b68 13993 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13994}
13995
c2f4122d
PA
13996/* Return true if all locations of B bound to PSPACE are pending. If
13997 PSPACE is NULL, all locations of all program spaces are
13998 considered. */
13999
0d381245 14000static int
c2f4122d 14001all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 14002{
c2f4122d
PA
14003 struct bp_location *loc;
14004
14005 for (loc = b->loc; loc != NULL; loc = loc->next)
14006 if ((pspace == NULL
14007 || loc->pspace == pspace)
14008 && !loc->shlib_disabled
8645ff69 14009 && !loc->pspace->executing_startup)
0d381245
VP
14010 return 0;
14011 return 1;
fe3f5fa8
VP
14012}
14013
776592bf
DE
14014/* Subroutine of update_breakpoint_locations to simplify it.
14015 Return non-zero if multiple fns in list LOC have the same name.
14016 Null names are ignored. */
14017
14018static int
14019ambiguous_names_p (struct bp_location *loc)
14020{
14021 struct bp_location *l;
14022 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14023 (int (*) (const void *,
14024 const void *)) streq,
776592bf
DE
14025 NULL, xcalloc, xfree);
14026
14027 for (l = loc; l != NULL; l = l->next)
14028 {
14029 const char **slot;
14030 const char *name = l->function_name;
14031
14032 /* Allow for some names to be NULL, ignore them. */
14033 if (name == NULL)
14034 continue;
14035
14036 slot = (const char **) htab_find_slot (htab, (const void *) name,
14037 INSERT);
4a64f543
MS
14038 /* NOTE: We can assume slot != NULL here because xcalloc never
14039 returns NULL. */
776592bf
DE
14040 if (*slot != NULL)
14041 {
14042 htab_delete (htab);
14043 return 1;
14044 }
14045 *slot = name;
14046 }
14047
14048 htab_delete (htab);
14049 return 0;
14050}
14051
0fb4aa4b
PA
14052/* When symbols change, it probably means the sources changed as well,
14053 and it might mean the static tracepoint markers are no longer at
14054 the same address or line numbers they used to be at last we
14055 checked. Losing your static tracepoints whenever you rebuild is
14056 undesirable. This function tries to resync/rematch gdb static
14057 tracepoints with the markers on the target, for static tracepoints
14058 that have not been set by marker id. Static tracepoint that have
14059 been set by marker id are reset by marker id in breakpoint_re_set.
14060 The heuristic is:
14061
14062 1) For a tracepoint set at a specific address, look for a marker at
14063 the old PC. If one is found there, assume to be the same marker.
14064 If the name / string id of the marker found is different from the
14065 previous known name, assume that means the user renamed the marker
14066 in the sources, and output a warning.
14067
14068 2) For a tracepoint set at a given line number, look for a marker
14069 at the new address of the old line number. If one is found there,
14070 assume to be the same marker. If the name / string id of the
14071 marker found is different from the previous known name, assume that
14072 means the user renamed the marker in the sources, and output a
14073 warning.
14074
14075 3) If a marker is no longer found at the same address or line, it
14076 may mean the marker no longer exists. But it may also just mean
14077 the code changed a bit. Maybe the user added a few lines of code
14078 that made the marker move up or down (in line number terms). Ask
14079 the target for info about the marker with the string id as we knew
14080 it. If found, update line number and address in the matching
14081 static tracepoint. This will get confused if there's more than one
14082 marker with the same ID (possible in UST, although unadvised
14083 precisely because it confuses tools). */
14084
14085static struct symtab_and_line
14086update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14087{
d9b3f62e 14088 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14089 struct static_tracepoint_marker marker;
14090 CORE_ADDR pc;
0fb4aa4b
PA
14091
14092 pc = sal.pc;
14093 if (sal.line)
14094 find_line_pc (sal.symtab, sal.line, &pc);
14095
14096 if (target_static_tracepoint_marker_at (pc, &marker))
14097 {
d9b3f62e 14098 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14099 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14100 b->number,
d9b3f62e 14101 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14102
d9b3f62e
PA
14103 xfree (tp->static_trace_marker_id);
14104 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14105 release_static_tracepoint_marker (&marker);
14106
14107 return sal;
14108 }
14109
14110 /* Old marker wasn't found on target at lineno. Try looking it up
14111 by string ID. */
14112 if (!sal.explicit_pc
14113 && sal.line != 0
14114 && sal.symtab != NULL
d9b3f62e 14115 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14116 {
14117 VEC(static_tracepoint_marker_p) *markers;
14118
14119 markers
d9b3f62e 14120 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14121
14122 if (!VEC_empty(static_tracepoint_marker_p, markers))
14123 {
80e1d417 14124 struct symtab_and_line sal2;
0fb4aa4b 14125 struct symbol *sym;
80e1d417 14126 struct static_tracepoint_marker *tpmarker;
79a45e25 14127 struct ui_out *uiout = current_uiout;
67994074 14128 struct explicit_location explicit_loc;
0fb4aa4b 14129
80e1d417 14130 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14131
d9b3f62e 14132 xfree (tp->static_trace_marker_id);
80e1d417 14133 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14134
14135 warning (_("marker for static tracepoint %d (%s) not "
14136 "found at previous line number"),
d9b3f62e 14137 b->number, tp->static_trace_marker_id);
0fb4aa4b 14138
80e1d417 14139 init_sal (&sal2);
0fb4aa4b 14140
80e1d417 14141 sal2.pc = tpmarker->address;
0fb4aa4b 14142
80e1d417
AS
14143 sal2 = find_pc_line (tpmarker->address, 0);
14144 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14145 ui_out_text (uiout, "Now in ");
14146 if (sym)
14147 {
14148 ui_out_field_string (uiout, "func",
14149 SYMBOL_PRINT_NAME (sym));
14150 ui_out_text (uiout, " at ");
14151 }
05cba821
JK
14152 ui_out_field_string (uiout, "file",
14153 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14154 ui_out_text (uiout, ":");
14155
14156 if (ui_out_is_mi_like_p (uiout))
14157 {
0b0865da 14158 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14159
f35a17b5 14160 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14161 }
14162
80e1d417 14163 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14164 ui_out_text (uiout, "\n");
14165
80e1d417 14166 b->loc->line_number = sal2.line;
2f202fde 14167 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14168
f00aae0f 14169 delete_event_location (b->location);
67994074
KS
14170 initialize_explicit_location (&explicit_loc);
14171 explicit_loc.source_filename
00e52e53 14172 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14173 explicit_loc.line_offset.offset = b->loc->line_number;
14174 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14175 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14176
14177 /* Might be nice to check if function changed, and warn if
14178 so. */
14179
80e1d417 14180 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14181 }
14182 }
14183 return sal;
14184}
14185
8d3788bd
VP
14186/* Returns 1 iff locations A and B are sufficiently same that
14187 we don't need to report breakpoint as changed. */
14188
14189static int
14190locations_are_equal (struct bp_location *a, struct bp_location *b)
14191{
14192 while (a && b)
14193 {
14194 if (a->address != b->address)
14195 return 0;
14196
14197 if (a->shlib_disabled != b->shlib_disabled)
14198 return 0;
14199
14200 if (a->enabled != b->enabled)
14201 return 0;
14202
14203 a = a->next;
14204 b = b->next;
14205 }
14206
14207 if ((a == NULL) != (b == NULL))
14208 return 0;
14209
14210 return 1;
14211}
14212
c2f4122d
PA
14213/* Split all locations of B that are bound to PSPACE out of B's
14214 location list to a separate list and return that list's head. If
14215 PSPACE is NULL, hoist out all locations of B. */
14216
14217static struct bp_location *
14218hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14219{
14220 struct bp_location head;
14221 struct bp_location *i = b->loc;
14222 struct bp_location **i_link = &b->loc;
14223 struct bp_location *hoisted = &head;
14224
14225 if (pspace == NULL)
14226 {
14227 i = b->loc;
14228 b->loc = NULL;
14229 return i;
14230 }
14231
14232 head.next = NULL;
14233
14234 while (i != NULL)
14235 {
14236 if (i->pspace == pspace)
14237 {
14238 *i_link = i->next;
14239 i->next = NULL;
14240 hoisted->next = i;
14241 hoisted = i;
14242 }
14243 else
14244 i_link = &i->next;
14245 i = *i_link;
14246 }
14247
14248 return head.next;
14249}
14250
14251/* Create new breakpoint locations for B (a hardware or software
14252 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14253 zero, then B is a ranged breakpoint. Only recreates locations for
14254 FILTER_PSPACE. Locations of other program spaces are left
14255 untouched. */
f1310107 14256
0e30163f 14257void
0d381245 14258update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14259 struct program_space *filter_pspace,
f1310107
TJB
14260 struct symtabs_and_lines sals,
14261 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14262{
14263 int i;
c2f4122d 14264 struct bp_location *existing_locations;
0d381245 14265
f8eba3c6
TT
14266 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14267 {
14268 /* Ranged breakpoints have only one start location and one end
14269 location. */
14270 b->enable_state = bp_disabled;
f8eba3c6
TT
14271 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14272 "multiple locations found\n"),
14273 b->number);
14274 return;
14275 }
f1310107 14276
4a64f543
MS
14277 /* If there's no new locations, and all existing locations are
14278 pending, don't do anything. This optimizes the common case where
14279 all locations are in the same shared library, that was unloaded.
14280 We'd like to retain the location, so that when the library is
14281 loaded again, we don't loose the enabled/disabled status of the
14282 individual locations. */
c2f4122d 14283 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14284 return;
14285
c2f4122d 14286 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14287
0d381245 14288 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14289 {
f8eba3c6
TT
14290 struct bp_location *new_loc;
14291
14292 switch_to_program_space_and_thread (sals.sals[i].pspace);
14293
14294 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14295
0d381245
VP
14296 /* Reparse conditions, they might contain references to the
14297 old symtab. */
14298 if (b->cond_string != NULL)
14299 {
bbc13ae3 14300 const char *s;
fe3f5fa8 14301
0d381245 14302 s = b->cond_string;
492d29ea 14303 TRY
0d381245 14304 {
1bb9788d
TT
14305 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14306 block_for_pc (sals.sals[i].pc),
0d381245
VP
14307 0);
14308 }
492d29ea 14309 CATCH (e, RETURN_MASK_ERROR)
0d381245 14310 {
3e43a32a
MS
14311 warning (_("failed to reevaluate condition "
14312 "for breakpoint %d: %s"),
0d381245
VP
14313 b->number, e.message);
14314 new_loc->enabled = 0;
14315 }
492d29ea 14316 END_CATCH
0d381245 14317 }
fe3f5fa8 14318
f1310107
TJB
14319 if (sals_end.nelts)
14320 {
14321 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14322
14323 new_loc->length = end - sals.sals[0].pc + 1;
14324 }
0d381245 14325 }
fe3f5fa8 14326
4a64f543
MS
14327 /* If possible, carry over 'disable' status from existing
14328 breakpoints. */
0d381245
VP
14329 {
14330 struct bp_location *e = existing_locations;
776592bf
DE
14331 /* If there are multiple breakpoints with the same function name,
14332 e.g. for inline functions, comparing function names won't work.
14333 Instead compare pc addresses; this is just a heuristic as things
14334 may have moved, but in practice it gives the correct answer
14335 often enough until a better solution is found. */
14336 int have_ambiguous_names = ambiguous_names_p (b->loc);
14337
0d381245
VP
14338 for (; e; e = e->next)
14339 {
14340 if (!e->enabled && e->function_name)
14341 {
14342 struct bp_location *l = b->loc;
776592bf
DE
14343 if (have_ambiguous_names)
14344 {
14345 for (; l; l = l->next)
f1310107 14346 if (breakpoint_locations_match (e, l))
776592bf
DE
14347 {
14348 l->enabled = 0;
14349 break;
14350 }
14351 }
14352 else
14353 {
14354 for (; l; l = l->next)
14355 if (l->function_name
14356 && strcmp (e->function_name, l->function_name) == 0)
14357 {
14358 l->enabled = 0;
14359 break;
14360 }
14361 }
0d381245
VP
14362 }
14363 }
14364 }
fe3f5fa8 14365
8d3788bd
VP
14366 if (!locations_are_equal (existing_locations, b->loc))
14367 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14368}
14369
f00aae0f 14370/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14371 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14372
14373static struct symtabs_and_lines
f00aae0f 14374location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14375 struct program_space *search_pspace, int *found)
ef23e705 14376{
02d20e4a 14377 struct symtabs_and_lines sals = {0};
492d29ea 14378 struct gdb_exception exception = exception_none;
ef23e705 14379
983af33b 14380 gdb_assert (b->ops != NULL);
ef23e705 14381
492d29ea 14382 TRY
ef23e705 14383 {
c2f4122d 14384 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14385 }
492d29ea 14386 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14387 {
14388 int not_found_and_ok = 0;
492d29ea
PA
14389
14390 exception = e;
14391
ef23e705
TJB
14392 /* For pending breakpoints, it's expected that parsing will
14393 fail until the right shared library is loaded. User has
14394 already told to create pending breakpoints and don't need
14395 extra messages. If breakpoint is in bp_shlib_disabled
14396 state, then user already saw the message about that
14397 breakpoint being disabled, and don't want to see more
14398 errors. */
58438ac1 14399 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14400 && (b->condition_not_parsed
14401 || (b->loc != NULL
14402 && search_pspace != NULL
14403 && b->loc->pspace != search_pspace)
ef23e705 14404 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14405 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14406 || b->enable_state == bp_disabled))
14407 not_found_and_ok = 1;
14408
14409 if (!not_found_and_ok)
14410 {
14411 /* We surely don't want to warn about the same breakpoint
14412 10 times. One solution, implemented here, is disable
14413 the breakpoint on error. Another solution would be to
14414 have separate 'warning emitted' flag. Since this
14415 happens only when a binary has changed, I don't know
14416 which approach is better. */
14417 b->enable_state = bp_disabled;
14418 throw_exception (e);
14419 }
14420 }
492d29ea 14421 END_CATCH
ef23e705 14422
492d29ea 14423 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14424 {
f8eba3c6 14425 int i;
ef23e705 14426
f8eba3c6
TT
14427 for (i = 0; i < sals.nelts; ++i)
14428 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14429 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14430 {
ed1d1739
KS
14431 char *cond_string, *extra_string;
14432 int thread, task;
ef23e705 14433
f00aae0f 14434 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14435 &cond_string, &thread, &task,
14436 &extra_string);
f00aae0f 14437 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14438 if (cond_string)
14439 b->cond_string = cond_string;
14440 b->thread = thread;
14441 b->task = task;
e7e0cddf 14442 if (extra_string)
f00aae0f
KS
14443 {
14444 xfree (b->extra_string);
14445 b->extra_string = extra_string;
14446 }
ef23e705
TJB
14447 b->condition_not_parsed = 0;
14448 }
14449
983af33b 14450 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14451 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14452
58438ac1
TT
14453 *found = 1;
14454 }
14455 else
14456 *found = 0;
ef23e705
TJB
14457
14458 return sals;
14459}
14460
348d480f
PA
14461/* The default re_set method, for typical hardware or software
14462 breakpoints. Reevaluate the breakpoint and recreate its
14463 locations. */
14464
14465static void
28010a5d 14466breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14467{
14468 int found;
f1310107 14469 struct symtabs_and_lines sals, sals_end;
ef23e705 14470 struct symtabs_and_lines expanded = {0};
f1310107 14471 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14472 struct program_space *filter_pspace = current_program_space;
ef23e705 14473
c2f4122d 14474 sals = location_to_sals (b, b->location, filter_pspace, &found);
ef23e705
TJB
14475 if (found)
14476 {
14477 make_cleanup (xfree, sals.sals);
f8eba3c6 14478 expanded = sals;
ef23e705
TJB
14479 }
14480
f00aae0f 14481 if (b->location_range_end != NULL)
f1310107 14482 {
c2f4122d
PA
14483 sals_end = location_to_sals (b, b->location_range_end,
14484 filter_pspace, &found);
f1310107
TJB
14485 if (found)
14486 {
14487 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14488 expanded_end = sals_end;
f1310107
TJB
14489 }
14490 }
14491
c2f4122d 14492 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14493}
14494
983af33b
SDJ
14495/* Default method for creating SALs from an address string. It basically
14496 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14497
14498static void
f00aae0f
KS
14499create_sals_from_location_default (const struct event_location *location,
14500 struct linespec_result *canonical,
14501 enum bptype type_wanted)
983af33b 14502{
f00aae0f 14503 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14504}
14505
14506/* Call create_breakpoints_sal for the given arguments. This is the default
14507 function for the `create_breakpoints_sal' method of
14508 breakpoint_ops. */
14509
14510static void
14511create_breakpoints_sal_default (struct gdbarch *gdbarch,
14512 struct linespec_result *canonical,
983af33b 14513 char *cond_string,
e7e0cddf 14514 char *extra_string,
983af33b
SDJ
14515 enum bptype type_wanted,
14516 enum bpdisp disposition,
14517 int thread,
14518 int task, int ignore_count,
14519 const struct breakpoint_ops *ops,
14520 int from_tty, int enabled,
44f238bb 14521 int internal, unsigned flags)
983af33b
SDJ
14522{
14523 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14524 extra_string,
983af33b
SDJ
14525 type_wanted, disposition,
14526 thread, task, ignore_count, ops, from_tty,
44f238bb 14527 enabled, internal, flags);
983af33b
SDJ
14528}
14529
14530/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14531 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14532
14533static void
f00aae0f
KS
14534decode_location_default (struct breakpoint *b,
14535 const struct event_location *location,
c2f4122d 14536 struct program_space *search_pspace,
983af33b
SDJ
14537 struct symtabs_and_lines *sals)
14538{
14539 struct linespec_result canonical;
14540
14541 init_linespec_result (&canonical);
c2f4122d 14542 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14543 (struct symtab *) NULL, 0,
14544 &canonical, multiple_symbols_all,
14545 b->filter);
14546
14547 /* We should get 0 or 1 resulting SALs. */
14548 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14549
14550 if (VEC_length (linespec_sals, canonical.sals) > 0)
14551 {
14552 struct linespec_sals *lsal;
14553
14554 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14555 *sals = lsal->sals;
14556 /* Arrange it so the destructor does not free the
14557 contents. */
14558 lsal->sals.sals = NULL;
14559 }
14560
14561 destroy_linespec_result (&canonical);
14562}
14563
28010a5d
PA
14564/* Prepare the global context for a re-set of breakpoint B. */
14565
14566static struct cleanup *
14567prepare_re_set_context (struct breakpoint *b)
14568{
28010a5d 14569 input_radix = b->input_radix;
28010a5d
PA
14570 set_language (b->language);
14571
c2f4122d 14572 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14573}
14574
c906108c
SS
14575/* Reset a breakpoint given it's struct breakpoint * BINT.
14576 The value we return ends up being the return value from catch_errors.
14577 Unused in this case. */
14578
14579static int
4efb68b1 14580breakpoint_re_set_one (void *bint)
c906108c 14581{
4a64f543 14582 /* Get past catch_errs. */
53a5351d 14583 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14584 struct cleanup *cleanups;
c906108c 14585
348d480f
PA
14586 cleanups = prepare_re_set_context (b);
14587 b->ops->re_set (b);
14588 do_cleanups (cleanups);
c906108c
SS
14589 return 0;
14590}
14591
c2f4122d
PA
14592/* Re-set breakpoint locations for the current program space.
14593 Locations bound to other program spaces are left untouched. */
14594
c906108c 14595void
69de3c6a 14596breakpoint_re_set (void)
c906108c 14597{
35df4500 14598 struct breakpoint *b, *b_tmp;
c906108c
SS
14599 enum language save_language;
14600 int save_input_radix;
6c95b8df 14601 struct cleanup *old_chain;
c5aa993b 14602
c906108c
SS
14603 save_language = current_language->la_language;
14604 save_input_radix = input_radix;
c2f4122d 14605 old_chain = save_current_space_and_thread ();
6c95b8df 14606
2a7f3dff
PA
14607 /* Note: we must not try to insert locations until after all
14608 breakpoints have been re-set. Otherwise, e.g., when re-setting
14609 breakpoint 1, we'd insert the locations of breakpoint 2, which
14610 hadn't been re-set yet, and thus may have stale locations. */
14611
35df4500 14612 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14613 {
4a64f543 14614 /* Format possible error msg. */
fe3f5fa8 14615 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14616 b->number);
14617 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14618 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14619 do_cleanups (cleanups);
c5aa993b 14620 }
c906108c
SS
14621 set_language (save_language);
14622 input_radix = save_input_radix;
e62c965a 14623
0756c555 14624 jit_breakpoint_re_set ();
4efc6507 14625
6c95b8df
PA
14626 do_cleanups (old_chain);
14627
af02033e
PP
14628 create_overlay_event_breakpoint ();
14629 create_longjmp_master_breakpoint ();
14630 create_std_terminate_master_breakpoint ();
186c406b 14631 create_exception_master_breakpoint ();
2a7f3dff
PA
14632
14633 /* Now we can insert. */
14634 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14635}
14636\f
c906108c
SS
14637/* Reset the thread number of this breakpoint:
14638
14639 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14640 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14641void
fba45db2 14642breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14643{
14644 if (b->thread != -1)
14645 {
39f77062 14646 if (in_thread_list (inferior_ptid))
5d5658a1 14647 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14648
14649 /* We're being called after following a fork. The new fork is
14650 selected as current, and unless this was a vfork will have a
14651 different program space from the original thread. Reset that
14652 as well. */
14653 b->loc->pspace = current_program_space;
c906108c
SS
14654 }
14655}
14656
03ac34d5
MS
14657/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14658 If from_tty is nonzero, it prints a message to that effect,
14659 which ends with a period (no newline). */
14660
c906108c 14661void
fba45db2 14662set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14663{
52f0bd74 14664 struct breakpoint *b;
c906108c
SS
14665
14666 if (count < 0)
14667 count = 0;
14668
14669 ALL_BREAKPOINTS (b)
14670 if (b->number == bptnum)
c5aa993b 14671 {
d77f58be
SS
14672 if (is_tracepoint (b))
14673 {
14674 if (from_tty && count != 0)
14675 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14676 bptnum);
14677 return;
14678 }
14679
c5aa993b 14680 b->ignore_count = count;
221ea385
KS
14681 if (from_tty)
14682 {
14683 if (count == 0)
3e43a32a
MS
14684 printf_filtered (_("Will stop next time "
14685 "breakpoint %d is reached."),
221ea385
KS
14686 bptnum);
14687 else if (count == 1)
a3f17187 14688 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14689 bptnum);
14690 else
3e43a32a
MS
14691 printf_filtered (_("Will ignore next %d "
14692 "crossings of breakpoint %d."),
221ea385
KS
14693 count, bptnum);
14694 }
8d3788bd 14695 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14696 return;
14697 }
c906108c 14698
8a3fe4f8 14699 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14700}
14701
c906108c
SS
14702/* Command to set ignore-count of breakpoint N to COUNT. */
14703
14704static void
fba45db2 14705ignore_command (char *args, int from_tty)
c906108c
SS
14706{
14707 char *p = args;
52f0bd74 14708 int num;
c906108c
SS
14709
14710 if (p == 0)
e2e0b3e5 14711 error_no_arg (_("a breakpoint number"));
c5aa993b 14712
c906108c 14713 num = get_number (&p);
5c44784c 14714 if (num == 0)
8a3fe4f8 14715 error (_("bad breakpoint number: '%s'"), args);
c906108c 14716 if (*p == 0)
8a3fe4f8 14717 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14718
14719 set_ignore_count (num,
14720 longest_to_int (value_as_long (parse_and_eval (p))),
14721 from_tty);
221ea385
KS
14722 if (from_tty)
14723 printf_filtered ("\n");
c906108c
SS
14724}
14725\f
14726/* Call FUNCTION on each of the breakpoints
14727 whose numbers are given in ARGS. */
14728
14729static void
95a42b64
TT
14730map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14731 void *),
14732 void *data)
c906108c 14733{
52f0bd74
AC
14734 int num;
14735 struct breakpoint *b, *tmp;
11cf8741 14736 int match;
197f0a60 14737 struct get_number_or_range_state state;
c906108c 14738
b9d61307 14739 if (args == 0 || *args == '\0')
e2e0b3e5 14740 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14741
197f0a60
TT
14742 init_number_or_range (&state, args);
14743
14744 while (!state.finished)
c906108c 14745 {
e799154c 14746 const char *p = state.string;
197f0a60 14747
11cf8741 14748 match = 0;
c5aa993b 14749
197f0a60 14750 num = get_number_or_range (&state);
5c44784c 14751 if (num == 0)
c5aa993b 14752 {
8a3fe4f8 14753 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14754 }
14755 else
14756 {
14757 ALL_BREAKPOINTS_SAFE (b, tmp)
14758 if (b->number == num)
14759 {
11cf8741 14760 match = 1;
cdac0397 14761 function (b, data);
11cf8741 14762 break;
5c44784c 14763 }
11cf8741 14764 if (match == 0)
a3f17187 14765 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14766 }
c906108c
SS
14767 }
14768}
14769
0d381245
VP
14770static struct bp_location *
14771find_location_by_number (char *number)
14772{
14773 char *dot = strchr (number, '.');
14774 char *p1;
14775 int bp_num;
14776 int loc_num;
14777 struct breakpoint *b;
14778 struct bp_location *loc;
14779
14780 *dot = '\0';
14781
14782 p1 = number;
197f0a60 14783 bp_num = get_number (&p1);
0d381245
VP
14784 if (bp_num == 0)
14785 error (_("Bad breakpoint number '%s'"), number);
14786
14787 ALL_BREAKPOINTS (b)
14788 if (b->number == bp_num)
14789 {
14790 break;
14791 }
14792
14793 if (!b || b->number != bp_num)
14794 error (_("Bad breakpoint number '%s'"), number);
14795
14796 p1 = dot+1;
197f0a60 14797 loc_num = get_number (&p1);
0d381245
VP
14798 if (loc_num == 0)
14799 error (_("Bad breakpoint location number '%s'"), number);
14800
14801 --loc_num;
14802 loc = b->loc;
14803 for (;loc_num && loc; --loc_num, loc = loc->next)
14804 ;
14805 if (!loc)
14806 error (_("Bad breakpoint location number '%s'"), dot+1);
14807
14808 return loc;
14809}
14810
14811
1900040c
MS
14812/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14813 If from_tty is nonzero, it prints a message to that effect,
14814 which ends with a period (no newline). */
14815
c906108c 14816void
fba45db2 14817disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14818{
14819 /* Never disable a watchpoint scope breakpoint; we want to
14820 hit them when we leave scope so we can delete both the
14821 watchpoint and its scope breakpoint at that time. */
14822 if (bpt->type == bp_watchpoint_scope)
14823 return;
14824
b5de0fa7 14825 bpt->enable_state = bp_disabled;
c906108c 14826
b775012e
LM
14827 /* Mark breakpoint locations modified. */
14828 mark_breakpoint_modified (bpt);
14829
d248b706
KY
14830 if (target_supports_enable_disable_tracepoint ()
14831 && current_trace_status ()->running && is_tracepoint (bpt))
14832 {
14833 struct bp_location *location;
14834
14835 for (location = bpt->loc; location; location = location->next)
14836 target_disable_tracepoint (location);
14837 }
14838
44702360 14839 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14840
8d3788bd 14841 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14842}
14843
51be5b68
PA
14844/* A callback for iterate_over_related_breakpoints. */
14845
14846static void
14847do_disable_breakpoint (struct breakpoint *b, void *ignore)
14848{
14849 disable_breakpoint (b);
14850}
14851
95a42b64
TT
14852/* A callback for map_breakpoint_numbers that calls
14853 disable_breakpoint. */
14854
14855static void
14856do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14857{
51be5b68 14858 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14859}
14860
c906108c 14861static void
fba45db2 14862disable_command (char *args, int from_tty)
c906108c 14863{
c906108c 14864 if (args == 0)
46c6471b
PA
14865 {
14866 struct breakpoint *bpt;
14867
14868 ALL_BREAKPOINTS (bpt)
14869 if (user_breakpoint_p (bpt))
14870 disable_breakpoint (bpt);
14871 }
9eaabc75 14872 else
0d381245 14873 {
9eaabc75
MW
14874 char *num = extract_arg (&args);
14875
14876 while (num)
d248b706 14877 {
9eaabc75 14878 if (strchr (num, '.'))
b775012e 14879 {
9eaabc75
MW
14880 struct bp_location *loc = find_location_by_number (num);
14881
14882 if (loc)
14883 {
14884 if (loc->enabled)
14885 {
14886 loc->enabled = 0;
14887 mark_breakpoint_location_modified (loc);
14888 }
14889 if (target_supports_enable_disable_tracepoint ()
14890 && current_trace_status ()->running && loc->owner
14891 && is_tracepoint (loc->owner))
14892 target_disable_tracepoint (loc);
14893 }
44702360 14894 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14895 }
9eaabc75
MW
14896 else
14897 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14898 num = extract_arg (&args);
d248b706 14899 }
0d381245 14900 }
c906108c
SS
14901}
14902
14903static void
816338b5
SS
14904enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14905 int count)
c906108c 14906{
afe38095 14907 int target_resources_ok;
c906108c
SS
14908
14909 if (bpt->type == bp_hardware_breakpoint)
14910 {
14911 int i;
c5aa993b 14912 i = hw_breakpoint_used_count ();
53a5351d 14913 target_resources_ok =
d92524f1 14914 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14915 i + 1, 0);
c906108c 14916 if (target_resources_ok == 0)
8a3fe4f8 14917 error (_("No hardware breakpoint support in the target."));
c906108c 14918 else if (target_resources_ok < 0)
8a3fe4f8 14919 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14920 }
14921
cc60f2e3 14922 if (is_watchpoint (bpt))
c906108c 14923 {
d07205c2 14924 /* Initialize it just to avoid a GCC false warning. */
f486487f 14925 enum enable_state orig_enable_state = bp_disabled;
dde02812 14926
492d29ea 14927 TRY
c906108c 14928 {
3a5c3e22
PA
14929 struct watchpoint *w = (struct watchpoint *) bpt;
14930
1e718ff1
TJB
14931 orig_enable_state = bpt->enable_state;
14932 bpt->enable_state = bp_enabled;
3a5c3e22 14933 update_watchpoint (w, 1 /* reparse */);
c906108c 14934 }
492d29ea 14935 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14936 {
1e718ff1 14937 bpt->enable_state = orig_enable_state;
dde02812
ES
14938 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14939 bpt->number);
14940 return;
c5aa993b 14941 }
492d29ea 14942 END_CATCH
c906108c 14943 }
0101ce28 14944
b775012e
LM
14945 bpt->enable_state = bp_enabled;
14946
14947 /* Mark breakpoint locations modified. */
14948 mark_breakpoint_modified (bpt);
14949
d248b706
KY
14950 if (target_supports_enable_disable_tracepoint ()
14951 && current_trace_status ()->running && is_tracepoint (bpt))
14952 {
14953 struct bp_location *location;
14954
14955 for (location = bpt->loc; location; location = location->next)
14956 target_enable_tracepoint (location);
14957 }
14958
b4c291bb 14959 bpt->disposition = disposition;
816338b5 14960 bpt->enable_count = count;
44702360 14961 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14962
8d3788bd 14963 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14964}
14965
fe3f5fa8 14966
c906108c 14967void
fba45db2 14968enable_breakpoint (struct breakpoint *bpt)
c906108c 14969{
816338b5 14970 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14971}
14972
14973static void
14974do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14975{
14976 enable_breakpoint (bpt);
c906108c
SS
14977}
14978
95a42b64
TT
14979/* A callback for map_breakpoint_numbers that calls
14980 enable_breakpoint. */
14981
14982static void
14983do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14984{
51be5b68 14985 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14986}
14987
c906108c
SS
14988/* The enable command enables the specified breakpoints (or all defined
14989 breakpoints) so they once again become (or continue to be) effective
1272ad14 14990 in stopping the inferior. */
c906108c 14991
c906108c 14992static void
fba45db2 14993enable_command (char *args, int from_tty)
c906108c 14994{
c906108c 14995 if (args == 0)
46c6471b
PA
14996 {
14997 struct breakpoint *bpt;
14998
14999 ALL_BREAKPOINTS (bpt)
15000 if (user_breakpoint_p (bpt))
15001 enable_breakpoint (bpt);
15002 }
9eaabc75 15003 else
0d381245 15004 {
9eaabc75
MW
15005 char *num = extract_arg (&args);
15006
15007 while (num)
d248b706 15008 {
9eaabc75 15009 if (strchr (num, '.'))
b775012e 15010 {
9eaabc75
MW
15011 struct bp_location *loc = find_location_by_number (num);
15012
15013 if (loc)
15014 {
15015 if (!loc->enabled)
15016 {
15017 loc->enabled = 1;
15018 mark_breakpoint_location_modified (loc);
15019 }
15020 if (target_supports_enable_disable_tracepoint ()
15021 && current_trace_status ()->running && loc->owner
15022 && is_tracepoint (loc->owner))
15023 target_enable_tracepoint (loc);
15024 }
44702360 15025 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15026 }
9eaabc75
MW
15027 else
15028 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15029 num = extract_arg (&args);
d248b706 15030 }
0d381245 15031 }
c906108c
SS
15032}
15033
816338b5
SS
15034/* This struct packages up disposition data for application to multiple
15035 breakpoints. */
15036
15037struct disp_data
15038{
15039 enum bpdisp disp;
15040 int count;
15041};
15042
c906108c 15043static void
51be5b68
PA
15044do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15045{
816338b5 15046 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15047
816338b5 15048 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15049}
15050
15051static void
15052do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15053{
816338b5 15054 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15055
15056 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15057}
15058
c906108c 15059static void
fba45db2 15060enable_once_command (char *args, int from_tty)
c906108c 15061{
51be5b68 15062 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15063}
15064
816338b5
SS
15065static void
15066do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15067{
15068 struct disp_data disp = { disp_disable, *(int *) countptr };
15069
15070 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15071}
15072
15073static void
15074enable_count_command (char *args, int from_tty)
15075{
b9d61307
SM
15076 int count;
15077
15078 if (args == NULL)
15079 error_no_arg (_("hit count"));
15080
15081 count = get_number (&args);
816338b5
SS
15082
15083 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15084}
15085
c906108c 15086static void
51be5b68 15087do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15088{
816338b5 15089 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15090
15091 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15092}
15093
c906108c 15094static void
fba45db2 15095enable_delete_command (char *args, int from_tty)
c906108c 15096{
51be5b68 15097 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15098}
15099\f
fa8d40ab
JJ
15100static void
15101set_breakpoint_cmd (char *args, int from_tty)
15102{
15103}
15104
15105static void
15106show_breakpoint_cmd (char *args, int from_tty)
15107{
15108}
15109
1f3b5d1b
PP
15110/* Invalidate last known value of any hardware watchpoint if
15111 the memory which that value represents has been written to by
15112 GDB itself. */
15113
15114static void
8de0566d
YQ
15115invalidate_bp_value_on_memory_change (struct inferior *inferior,
15116 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15117 const bfd_byte *data)
15118{
15119 struct breakpoint *bp;
15120
15121 ALL_BREAKPOINTS (bp)
15122 if (bp->enable_state == bp_enabled
3a5c3e22 15123 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15124 {
3a5c3e22 15125 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15126
3a5c3e22
PA
15127 if (wp->val_valid && wp->val)
15128 {
15129 struct bp_location *loc;
15130
15131 for (loc = bp->loc; loc != NULL; loc = loc->next)
15132 if (loc->loc_type == bp_loc_hardware_watchpoint
15133 && loc->address + loc->length > addr
15134 && addr + len > loc->address)
15135 {
15136 value_free (wp->val);
15137 wp->val = NULL;
15138 wp->val_valid = 0;
15139 }
15140 }
1f3b5d1b
PP
15141 }
15142}
15143
8181d85f
DJ
15144/* Create and insert a breakpoint for software single step. */
15145
15146void
6c95b8df 15147insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15148 struct address_space *aspace,
15149 CORE_ADDR next_pc)
8181d85f 15150{
7c16b83e
PA
15151 struct thread_info *tp = inferior_thread ();
15152 struct symtab_and_line sal;
15153 CORE_ADDR pc = next_pc;
8181d85f 15154
34b7e8a6
PA
15155 if (tp->control.single_step_breakpoints == NULL)
15156 {
15157 tp->control.single_step_breakpoints
5d5658a1 15158 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15159 }
8181d85f 15160
7c16b83e
PA
15161 sal = find_pc_line (pc, 0);
15162 sal.pc = pc;
15163 sal.section = find_pc_overlay (pc);
15164 sal.explicit_pc = 1;
34b7e8a6 15165 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15166
7c16b83e 15167 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15168}
15169
34b7e8a6 15170/* See breakpoint.h. */
f02253f1
HZ
15171
15172int
7c16b83e
PA
15173breakpoint_has_location_inserted_here (struct breakpoint *bp,
15174 struct address_space *aspace,
15175 CORE_ADDR pc)
1aafd4da 15176{
7c16b83e 15177 struct bp_location *loc;
1aafd4da 15178
7c16b83e
PA
15179 for (loc = bp->loc; loc != NULL; loc = loc->next)
15180 if (loc->inserted
15181 && breakpoint_location_address_match (loc, aspace, pc))
15182 return 1;
1aafd4da 15183
7c16b83e 15184 return 0;
ef370185
JB
15185}
15186
15187/* Check whether a software single-step breakpoint is inserted at
15188 PC. */
15189
15190int
15191single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15192 CORE_ADDR pc)
15193{
34b7e8a6
PA
15194 struct breakpoint *bpt;
15195
15196 ALL_BREAKPOINTS (bpt)
15197 {
15198 if (bpt->type == bp_single_step
15199 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15200 return 1;
15201 }
15202 return 0;
1aafd4da
UW
15203}
15204
1042e4c0
SS
15205/* Tracepoint-specific operations. */
15206
15207/* Set tracepoint count to NUM. */
15208static void
15209set_tracepoint_count (int num)
15210{
15211 tracepoint_count = num;
4fa62494 15212 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15213}
15214
70221824 15215static void
1042e4c0
SS
15216trace_command (char *arg, int from_tty)
15217{
55aa24fb 15218 struct breakpoint_ops *ops;
f00aae0f
KS
15219 struct event_location *location;
15220 struct cleanup *back_to;
55aa24fb 15221
f00aae0f
KS
15222 location = string_to_event_location (&arg, current_language);
15223 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15224 if (location != NULL
15225 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15226 ops = &tracepoint_probe_breakpoint_ops;
15227 else
15228 ops = &tracepoint_breakpoint_ops;
15229
558a9d82 15230 create_breakpoint (get_current_arch (),
f00aae0f
KS
15231 location,
15232 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15233 0 /* tempflag */,
15234 bp_tracepoint /* type_wanted */,
15235 0 /* Ignore count */,
15236 pending_break_support,
15237 ops,
15238 from_tty,
15239 1 /* enabled */,
15240 0 /* internal */, 0);
f00aae0f 15241 do_cleanups (back_to);
1042e4c0
SS
15242}
15243
70221824 15244static void
7a697b8d
SS
15245ftrace_command (char *arg, int from_tty)
15246{
f00aae0f
KS
15247 struct event_location *location;
15248 struct cleanup *back_to;
15249
15250 location = string_to_event_location (&arg, current_language);
15251 back_to = make_cleanup_delete_event_location (location);
558a9d82 15252 create_breakpoint (get_current_arch (),
f00aae0f
KS
15253 location,
15254 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15255 0 /* tempflag */,
15256 bp_fast_tracepoint /* type_wanted */,
15257 0 /* Ignore count */,
15258 pending_break_support,
15259 &tracepoint_breakpoint_ops,
15260 from_tty,
15261 1 /* enabled */,
15262 0 /* internal */, 0);
f00aae0f 15263 do_cleanups (back_to);
0fb4aa4b
PA
15264}
15265
15266/* strace command implementation. Creates a static tracepoint. */
15267
70221824 15268static void
0fb4aa4b
PA
15269strace_command (char *arg, int from_tty)
15270{
983af33b 15271 struct breakpoint_ops *ops;
f00aae0f
KS
15272 struct event_location *location;
15273 struct cleanup *back_to;
983af33b
SDJ
15274
15275 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15276 or with a normal static tracepoint. */
61012eef 15277 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15278 {
15279 ops = &strace_marker_breakpoint_ops;
15280 location = new_linespec_location (&arg);
15281 }
983af33b 15282 else
f00aae0f
KS
15283 {
15284 ops = &tracepoint_breakpoint_ops;
15285 location = string_to_event_location (&arg, current_language);
15286 }
983af33b 15287
f00aae0f 15288 back_to = make_cleanup_delete_event_location (location);
558a9d82 15289 create_breakpoint (get_current_arch (),
f00aae0f
KS
15290 location,
15291 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15292 0 /* tempflag */,
15293 bp_static_tracepoint /* type_wanted */,
15294 0 /* Ignore count */,
15295 pending_break_support,
15296 ops,
15297 from_tty,
15298 1 /* enabled */,
15299 0 /* internal */, 0);
f00aae0f 15300 do_cleanups (back_to);
7a697b8d
SS
15301}
15302
409873ef
SS
15303/* Set up a fake reader function that gets command lines from a linked
15304 list that was acquired during tracepoint uploading. */
15305
15306static struct uploaded_tp *this_utp;
3149d8c1 15307static int next_cmd;
409873ef
SS
15308
15309static char *
15310read_uploaded_action (void)
15311{
15312 char *rslt;
15313
3149d8c1 15314 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15315
3149d8c1 15316 next_cmd++;
409873ef
SS
15317
15318 return rslt;
15319}
15320
00bf0b85
SS
15321/* Given information about a tracepoint as recorded on a target (which
15322 can be either a live system or a trace file), attempt to create an
15323 equivalent GDB tracepoint. This is not a reliable process, since
15324 the target does not necessarily have all the information used when
15325 the tracepoint was originally defined. */
15326
d9b3f62e 15327struct tracepoint *
00bf0b85 15328create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15329{
409873ef 15330 char *addr_str, small_buf[100];
d9b3f62e 15331 struct tracepoint *tp;
f00aae0f
KS
15332 struct event_location *location;
15333 struct cleanup *cleanup;
fd9b8c24 15334
409873ef
SS
15335 if (utp->at_string)
15336 addr_str = utp->at_string;
15337 else
15338 {
15339 /* In the absence of a source location, fall back to raw
15340 address. Since there is no way to confirm that the address
15341 means the same thing as when the trace was started, warn the
15342 user. */
3e43a32a
MS
15343 warning (_("Uploaded tracepoint %d has no "
15344 "source location, using raw address"),
409873ef 15345 utp->number);
8c042590 15346 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15347 addr_str = small_buf;
15348 }
15349
15350 /* There's not much we can do with a sequence of bytecodes. */
15351 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15352 warning (_("Uploaded tracepoint %d condition "
15353 "has no source form, ignoring it"),
409873ef 15354 utp->number);
d5551862 15355
f00aae0f
KS
15356 location = string_to_event_location (&addr_str, current_language);
15357 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15358 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15359 location,
15360 utp->cond_string, -1, addr_str,
e7e0cddf 15361 0 /* parse cond/thread */,
8cdf0e15 15362 0 /* tempflag */,
0fb4aa4b 15363 utp->type /* type_wanted */,
8cdf0e15
VP
15364 0 /* Ignore count */,
15365 pending_break_support,
348d480f 15366 &tracepoint_breakpoint_ops,
8cdf0e15 15367 0 /* from_tty */,
84f4c1fe 15368 utp->enabled /* enabled */,
44f238bb
PA
15369 0 /* internal */,
15370 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15371 {
15372 do_cleanups (cleanup);
15373 return NULL;
15374 }
15375
15376 do_cleanups (cleanup);
fd9b8c24 15377
409873ef 15378 /* Get the tracepoint we just created. */
fd9b8c24
PA
15379 tp = get_tracepoint (tracepoint_count);
15380 gdb_assert (tp != NULL);
d5551862 15381
00bf0b85
SS
15382 if (utp->pass > 0)
15383 {
8c042590
PM
15384 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15385 tp->base.number);
00bf0b85 15386
409873ef 15387 trace_pass_command (small_buf, 0);
00bf0b85
SS
15388 }
15389
409873ef
SS
15390 /* If we have uploaded versions of the original commands, set up a
15391 special-purpose "reader" function and call the usual command line
15392 reader, then pass the result to the breakpoint command-setting
15393 function. */
3149d8c1 15394 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15395 {
409873ef 15396 struct command_line *cmd_list;
00bf0b85 15397
409873ef 15398 this_utp = utp;
3149d8c1 15399 next_cmd = 0;
d5551862 15400
409873ef
SS
15401 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15402
d9b3f62e 15403 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15404 }
3149d8c1
SS
15405 else if (!VEC_empty (char_ptr, utp->actions)
15406 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15407 warning (_("Uploaded tracepoint %d actions "
15408 "have no source form, ignoring them"),
409873ef 15409 utp->number);
00bf0b85 15410
f196051f
SS
15411 /* Copy any status information that might be available. */
15412 tp->base.hit_count = utp->hit_count;
15413 tp->traceframe_usage = utp->traceframe_usage;
15414
00bf0b85 15415 return tp;
d9b3f62e 15416}
00bf0b85 15417
1042e4c0
SS
15418/* Print information on tracepoint number TPNUM_EXP, or all if
15419 omitted. */
15420
15421static void
e5a67952 15422tracepoints_info (char *args, int from_tty)
1042e4c0 15423{
79a45e25 15424 struct ui_out *uiout = current_uiout;
e5a67952 15425 int num_printed;
1042e4c0 15426
e5a67952 15427 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15428
15429 if (num_printed == 0)
1042e4c0 15430 {
e5a67952 15431 if (args == NULL || *args == '\0')
d77f58be
SS
15432 ui_out_message (uiout, 0, "No tracepoints.\n");
15433 else
e5a67952 15434 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15435 }
ad443146
SS
15436
15437 default_collect_info ();
1042e4c0
SS
15438}
15439
4a64f543 15440/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15441 Not supported by all targets. */
15442static void
15443enable_trace_command (char *args, int from_tty)
15444{
15445 enable_command (args, from_tty);
15446}
15447
4a64f543 15448/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15449 Not supported by all targets. */
15450static void
15451disable_trace_command (char *args, int from_tty)
15452{
15453 disable_command (args, from_tty);
15454}
15455
4a64f543 15456/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15457static void
15458delete_trace_command (char *arg, int from_tty)
15459{
35df4500 15460 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15461
15462 dont_repeat ();
15463
15464 if (arg == 0)
15465 {
15466 int breaks_to_delete = 0;
15467
15468 /* Delete all breakpoints if no argument.
15469 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15470 have to be deleted with an explicit breakpoint number
15471 argument. */
1042e4c0 15472 ALL_TRACEPOINTS (b)
46c6471b 15473 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15474 {
15475 breaks_to_delete = 1;
15476 break;
15477 }
1042e4c0
SS
15478
15479 /* Ask user only if there are some breakpoints to delete. */
15480 if (!from_tty
15481 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15482 {
35df4500 15483 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15484 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15485 delete_breakpoint (b);
1042e4c0
SS
15486 }
15487 }
15488 else
51be5b68 15489 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15490}
15491
197f0a60
TT
15492/* Helper function for trace_pass_command. */
15493
15494static void
d9b3f62e 15495trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15496{
d9b3f62e 15497 tp->pass_count = count;
6f6484cd 15498 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15499 if (from_tty)
15500 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15501 tp->base.number, count);
197f0a60
TT
15502}
15503
1042e4c0
SS
15504/* Set passcount for tracepoint.
15505
15506 First command argument is passcount, second is tracepoint number.
15507 If tracepoint number omitted, apply to most recently defined.
15508 Also accepts special argument "all". */
15509
15510static void
15511trace_pass_command (char *args, int from_tty)
15512{
d9b3f62e 15513 struct tracepoint *t1;
1042e4c0 15514 unsigned int count;
1042e4c0
SS
15515
15516 if (args == 0 || *args == 0)
3e43a32a
MS
15517 error (_("passcount command requires an "
15518 "argument (count + optional TP num)"));
1042e4c0 15519
4a64f543 15520 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15521
529480d0 15522 args = skip_spaces (args);
1042e4c0
SS
15523 if (*args && strncasecmp (args, "all", 3) == 0)
15524 {
d9b3f62e
PA
15525 struct breakpoint *b;
15526
1042e4c0 15527 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15528 if (*args)
15529 error (_("Junk at end of arguments."));
1042e4c0 15530
d9b3f62e 15531 ALL_TRACEPOINTS (b)
197f0a60 15532 {
d9b3f62e 15533 t1 = (struct tracepoint *) b;
197f0a60
TT
15534 trace_pass_set_count (t1, count, from_tty);
15535 }
15536 }
15537 else if (*args == '\0')
1042e4c0 15538 {
5fa1d40e 15539 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15540 if (t1)
197f0a60
TT
15541 trace_pass_set_count (t1, count, from_tty);
15542 }
15543 else
15544 {
15545 struct get_number_or_range_state state;
15546
15547 init_number_or_range (&state, args);
15548 while (!state.finished)
1042e4c0 15549 {
5fa1d40e 15550 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15551 if (t1)
15552 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15553 }
15554 }
1042e4c0
SS
15555}
15556
d9b3f62e 15557struct tracepoint *
1042e4c0
SS
15558get_tracepoint (int num)
15559{
15560 struct breakpoint *t;
15561
15562 ALL_TRACEPOINTS (t)
15563 if (t->number == num)
d9b3f62e 15564 return (struct tracepoint *) t;
1042e4c0
SS
15565
15566 return NULL;
15567}
15568
d5551862
SS
15569/* Find the tracepoint with the given target-side number (which may be
15570 different from the tracepoint number after disconnecting and
15571 reconnecting). */
15572
d9b3f62e 15573struct tracepoint *
d5551862
SS
15574get_tracepoint_by_number_on_target (int num)
15575{
d9b3f62e 15576 struct breakpoint *b;
d5551862 15577
d9b3f62e
PA
15578 ALL_TRACEPOINTS (b)
15579 {
15580 struct tracepoint *t = (struct tracepoint *) b;
15581
15582 if (t->number_on_target == num)
15583 return t;
15584 }
d5551862
SS
15585
15586 return NULL;
15587}
15588
1042e4c0 15589/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15590 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15591 If the argument is missing, the most recent tracepoint
15592 (tracepoint_count) is returned. */
15593
d9b3f62e 15594struct tracepoint *
197f0a60 15595get_tracepoint_by_number (char **arg,
5fa1d40e 15596 struct get_number_or_range_state *state)
1042e4c0 15597{
1042e4c0
SS
15598 struct breakpoint *t;
15599 int tpnum;
15600 char *instring = arg == NULL ? NULL : *arg;
15601
197f0a60
TT
15602 if (state)
15603 {
15604 gdb_assert (!state->finished);
15605 tpnum = get_number_or_range (state);
15606 }
15607 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15608 tpnum = tracepoint_count;
1042e4c0 15609 else
197f0a60 15610 tpnum = get_number (arg);
1042e4c0
SS
15611
15612 if (tpnum <= 0)
15613 {
15614 if (instring && *instring)
15615 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15616 instring);
15617 else
5fa1d40e 15618 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15619 return NULL;
15620 }
15621
15622 ALL_TRACEPOINTS (t)
15623 if (t->number == tpnum)
15624 {
d9b3f62e 15625 return (struct tracepoint *) t;
1042e4c0
SS
15626 }
15627
1042e4c0
SS
15628 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15629 return NULL;
15630}
15631
d9b3f62e
PA
15632void
15633print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15634{
15635 if (b->thread != -1)
15636 fprintf_unfiltered (fp, " thread %d", b->thread);
15637
15638 if (b->task != 0)
15639 fprintf_unfiltered (fp, " task %d", b->task);
15640
15641 fprintf_unfiltered (fp, "\n");
15642}
15643
6149aea9
PA
15644/* Save information on user settable breakpoints (watchpoints, etc) to
15645 a new script file named FILENAME. If FILTER is non-NULL, call it
15646 on each breakpoint and only include the ones for which it returns
15647 non-zero. */
15648
1042e4c0 15649static void
6149aea9
PA
15650save_breakpoints (char *filename, int from_tty,
15651 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15652{
15653 struct breakpoint *tp;
6149aea9 15654 int any = 0;
1042e4c0 15655 struct cleanup *cleanup;
a7bdde9e 15656 struct ui_file *fp;
6149aea9 15657 int extra_trace_bits = 0;
1042e4c0 15658
6149aea9
PA
15659 if (filename == 0 || *filename == 0)
15660 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15661
15662 /* See if we have anything to save. */
6149aea9 15663 ALL_BREAKPOINTS (tp)
1042e4c0 15664 {
6149aea9 15665 /* Skip internal and momentary breakpoints. */
09d682a4 15666 if (!user_breakpoint_p (tp))
6149aea9
PA
15667 continue;
15668
15669 /* If we have a filter, only save the breakpoints it accepts. */
15670 if (filter && !filter (tp))
15671 continue;
15672
15673 any = 1;
15674
15675 if (is_tracepoint (tp))
15676 {
15677 extra_trace_bits = 1;
15678
15679 /* We can stop searching. */
15680 break;
15681 }
1042e4c0 15682 }
6149aea9
PA
15683
15684 if (!any)
1042e4c0 15685 {
6149aea9 15686 warning (_("Nothing to save."));
1042e4c0
SS
15687 return;
15688 }
15689
c718be47
PA
15690 filename = tilde_expand (filename);
15691 cleanup = make_cleanup (xfree, filename);
15692 fp = gdb_fopen (filename, "w");
059fb39f 15693 if (!fp)
6149aea9
PA
15694 error (_("Unable to open file '%s' for saving (%s)"),
15695 filename, safe_strerror (errno));
a7bdde9e 15696 make_cleanup_ui_file_delete (fp);
8bf6485c 15697
6149aea9
PA
15698 if (extra_trace_bits)
15699 save_trace_state_variables (fp);
8bf6485c 15700
6149aea9 15701 ALL_BREAKPOINTS (tp)
1042e4c0 15702 {
6149aea9 15703 /* Skip internal and momentary breakpoints. */
09d682a4 15704 if (!user_breakpoint_p (tp))
6149aea9 15705 continue;
8bf6485c 15706
6149aea9
PA
15707 /* If we have a filter, only save the breakpoints it accepts. */
15708 if (filter && !filter (tp))
15709 continue;
15710
348d480f 15711 tp->ops->print_recreate (tp, fp);
1042e4c0 15712
6149aea9
PA
15713 /* Note, we can't rely on tp->number for anything, as we can't
15714 assume the recreated breakpoint numbers will match. Use $bpnum
15715 instead. */
15716
15717 if (tp->cond_string)
15718 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15719
15720 if (tp->ignore_count)
15721 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15722
2d9442cc 15723 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15724 {
6c63c96a 15725 struct gdb_exception exception;
a7bdde9e 15726
6149aea9 15727 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15728
79a45e25 15729 ui_out_redirect (current_uiout, fp);
492d29ea 15730 TRY
1042e4c0 15731 {
79a45e25 15732 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15733 }
492d29ea
PA
15734 CATCH (ex, RETURN_MASK_ALL)
15735 {
6c63c96a 15736 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15737 throw_exception (ex);
15738 }
15739 END_CATCH
1042e4c0 15740
6c63c96a 15741 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15742 fprintf_unfiltered (fp, " end\n");
1042e4c0 15743 }
6149aea9
PA
15744
15745 if (tp->enable_state == bp_disabled)
99894e11 15746 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15747
15748 /* If this is a multi-location breakpoint, check if the locations
15749 should be individually disabled. Watchpoint locations are
15750 special, and not user visible. */
15751 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15752 {
15753 struct bp_location *loc;
15754 int n = 1;
15755
15756 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15757 if (!loc->enabled)
15758 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15759 }
1042e4c0 15760 }
8bf6485c 15761
6149aea9 15762 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15763 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15764
1042e4c0 15765 if (from_tty)
6149aea9 15766 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15767 do_cleanups (cleanup);
6149aea9
PA
15768}
15769
15770/* The `save breakpoints' command. */
15771
15772static void
15773save_breakpoints_command (char *args, int from_tty)
15774{
15775 save_breakpoints (args, from_tty, NULL);
15776}
15777
15778/* The `save tracepoints' command. */
15779
15780static void
15781save_tracepoints_command (char *args, int from_tty)
15782{
15783 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15784}
15785
15786/* Create a vector of all tracepoints. */
15787
15788VEC(breakpoint_p) *
eeae04df 15789all_tracepoints (void)
1042e4c0
SS
15790{
15791 VEC(breakpoint_p) *tp_vec = 0;
15792 struct breakpoint *tp;
15793
15794 ALL_TRACEPOINTS (tp)
15795 {
15796 VEC_safe_push (breakpoint_p, tp_vec, tp);
15797 }
15798
15799 return tp_vec;
15800}
15801
c906108c 15802\f
629500fa
KS
15803/* This help string is used to consolidate all the help string for specifying
15804 locations used by several commands. */
15805
15806#define LOCATION_HELP_STRING \
15807"Linespecs are colon-separated lists of location parameters, such as\n\
15808source filename, function name, label name, and line number.\n\
15809Example: To specify the start of a label named \"the_top\" in the\n\
15810function \"fact\" in the file \"factorial.c\", use\n\
15811\"factorial.c:fact:the_top\".\n\
15812\n\
15813Address locations begin with \"*\" and specify an exact address in the\n\
15814program. Example: To specify the fourth byte past the start function\n\
15815\"main\", use \"*main + 4\".\n\
15816\n\
15817Explicit locations are similar to linespecs but use an option/argument\n\
15818syntax to specify location parameters.\n\
15819Example: To specify the start of the label named \"the_top\" in the\n\
15820function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15821-function fact -label the_top\".\n"
15822
4a64f543
MS
15823/* This help string is used for the break, hbreak, tbreak and thbreak
15824 commands. It is defined as a macro to prevent duplication.
15825 COMMAND should be a string constant containing the name of the
15826 command. */
629500fa 15827
31e2b00f 15828#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15829command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15830PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15831probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15832guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15833`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15834LOCATION may be a linespec, address, or explicit location as described\n\
15835below.\n\
15836\n\
dc10affe
PA
15837With no LOCATION, uses current execution address of the selected\n\
15838stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15839\n\
15840THREADNUM is the number from \"info threads\".\n\
15841CONDITION is a boolean expression.\n\
629500fa 15842\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15843Multiple breakpoints at one place are permitted, and useful if their\n\
15844conditions are different.\n\
31e2b00f
AS
15845\n\
15846Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15847
44feb3ce
TT
15848/* List of subcommands for "catch". */
15849static struct cmd_list_element *catch_cmdlist;
15850
15851/* List of subcommands for "tcatch". */
15852static struct cmd_list_element *tcatch_cmdlist;
15853
9ac4176b 15854void
44feb3ce 15855add_catch_command (char *name, char *docstring,
82ae6c8d 15856 cmd_sfunc_ftype *sfunc,
625e8578 15857 completer_ftype *completer,
44feb3ce
TT
15858 void *user_data_catch,
15859 void *user_data_tcatch)
15860{
15861 struct cmd_list_element *command;
15862
15863 command = add_cmd (name, class_breakpoint, NULL, docstring,
15864 &catch_cmdlist);
15865 set_cmd_sfunc (command, sfunc);
15866 set_cmd_context (command, user_data_catch);
a96d9b2e 15867 set_cmd_completer (command, completer);
44feb3ce
TT
15868
15869 command = add_cmd (name, class_breakpoint, NULL, docstring,
15870 &tcatch_cmdlist);
15871 set_cmd_sfunc (command, sfunc);
15872 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15873 set_cmd_completer (command, completer);
44feb3ce
TT
15874}
15875
6149aea9
PA
15876static void
15877save_command (char *arg, int from_tty)
15878{
3e43a32a
MS
15879 printf_unfiltered (_("\"save\" must be followed by "
15880 "the name of a save subcommand.\n"));
635c7e8a 15881 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15882}
15883
84f4c1fe
PM
15884struct breakpoint *
15885iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15886 void *data)
15887{
35df4500 15888 struct breakpoint *b, *b_tmp;
84f4c1fe 15889
35df4500 15890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15891 {
15892 if ((*callback) (b, data))
15893 return b;
15894 }
15895
15896 return NULL;
15897}
15898
0574c78f
GB
15899/* Zero if any of the breakpoint's locations could be a location where
15900 functions have been inlined, nonzero otherwise. */
15901
15902static int
15903is_non_inline_function (struct breakpoint *b)
15904{
15905 /* The shared library event breakpoint is set on the address of a
15906 non-inline function. */
15907 if (b->type == bp_shlib_event)
15908 return 1;
15909
15910 return 0;
15911}
15912
15913/* Nonzero if the specified PC cannot be a location where functions
15914 have been inlined. */
15915
15916int
09ac7c10
TT
15917pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15918 const struct target_waitstatus *ws)
0574c78f
GB
15919{
15920 struct breakpoint *b;
15921 struct bp_location *bl;
15922
15923 ALL_BREAKPOINTS (b)
15924 {
15925 if (!is_non_inline_function (b))
15926 continue;
15927
15928 for (bl = b->loc; bl != NULL; bl = bl->next)
15929 {
15930 if (!bl->shlib_disabled
09ac7c10 15931 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15932 return 1;
15933 }
15934 }
15935
15936 return 0;
15937}
15938
2f202fde
JK
15939/* Remove any references to OBJFILE which is going to be freed. */
15940
15941void
15942breakpoint_free_objfile (struct objfile *objfile)
15943{
15944 struct bp_location **locp, *loc;
15945
15946 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15947 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15948 loc->symtab = NULL;
15949}
15950
2060206e
PA
15951void
15952initialize_breakpoint_ops (void)
15953{
15954 static int initialized = 0;
15955
15956 struct breakpoint_ops *ops;
15957
15958 if (initialized)
15959 return;
15960 initialized = 1;
15961
15962 /* The breakpoint_ops structure to be inherit by all kinds of
15963 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15964 internal and momentary breakpoints, etc.). */
15965 ops = &bkpt_base_breakpoint_ops;
15966 *ops = base_breakpoint_ops;
15967 ops->re_set = bkpt_re_set;
15968 ops->insert_location = bkpt_insert_location;
15969 ops->remove_location = bkpt_remove_location;
15970 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15971 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15972 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15973 ops->decode_location = bkpt_decode_location;
2060206e
PA
15974
15975 /* The breakpoint_ops structure to be used in regular breakpoints. */
15976 ops = &bkpt_breakpoint_ops;
15977 *ops = bkpt_base_breakpoint_ops;
15978 ops->re_set = bkpt_re_set;
15979 ops->resources_needed = bkpt_resources_needed;
15980 ops->print_it = bkpt_print_it;
15981 ops->print_mention = bkpt_print_mention;
15982 ops->print_recreate = bkpt_print_recreate;
15983
15984 /* Ranged breakpoints. */
15985 ops = &ranged_breakpoint_ops;
15986 *ops = bkpt_breakpoint_ops;
15987 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15988 ops->resources_needed = resources_needed_ranged_breakpoint;
15989 ops->print_it = print_it_ranged_breakpoint;
15990 ops->print_one = print_one_ranged_breakpoint;
15991 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15992 ops->print_mention = print_mention_ranged_breakpoint;
15993 ops->print_recreate = print_recreate_ranged_breakpoint;
15994
15995 /* Internal breakpoints. */
15996 ops = &internal_breakpoint_ops;
15997 *ops = bkpt_base_breakpoint_ops;
15998 ops->re_set = internal_bkpt_re_set;
15999 ops->check_status = internal_bkpt_check_status;
16000 ops->print_it = internal_bkpt_print_it;
16001 ops->print_mention = internal_bkpt_print_mention;
16002
16003 /* Momentary breakpoints. */
16004 ops = &momentary_breakpoint_ops;
16005 *ops = bkpt_base_breakpoint_ops;
16006 ops->re_set = momentary_bkpt_re_set;
16007 ops->check_status = momentary_bkpt_check_status;
16008 ops->print_it = momentary_bkpt_print_it;
16009 ops->print_mention = momentary_bkpt_print_mention;
16010
e2e4d78b
JK
16011 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16012 ops = &longjmp_breakpoint_ops;
16013 *ops = momentary_breakpoint_ops;
16014 ops->dtor = longjmp_bkpt_dtor;
16015
55aa24fb
SDJ
16016 /* Probe breakpoints. */
16017 ops = &bkpt_probe_breakpoint_ops;
16018 *ops = bkpt_breakpoint_ops;
16019 ops->insert_location = bkpt_probe_insert_location;
16020 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
16021 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16022 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 16023
2060206e
PA
16024 /* Watchpoints. */
16025 ops = &watchpoint_breakpoint_ops;
16026 *ops = base_breakpoint_ops;
3a5c3e22 16027 ops->dtor = dtor_watchpoint;
2060206e
PA
16028 ops->re_set = re_set_watchpoint;
16029 ops->insert_location = insert_watchpoint;
16030 ops->remove_location = remove_watchpoint;
16031 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16032 ops->check_status = check_status_watchpoint;
16033 ops->resources_needed = resources_needed_watchpoint;
16034 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16035 ops->print_it = print_it_watchpoint;
16036 ops->print_mention = print_mention_watchpoint;
16037 ops->print_recreate = print_recreate_watchpoint;
427cd150 16038 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16039
16040 /* Masked watchpoints. */
16041 ops = &masked_watchpoint_breakpoint_ops;
16042 *ops = watchpoint_breakpoint_ops;
16043 ops->insert_location = insert_masked_watchpoint;
16044 ops->remove_location = remove_masked_watchpoint;
16045 ops->resources_needed = resources_needed_masked_watchpoint;
16046 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16047 ops->print_it = print_it_masked_watchpoint;
16048 ops->print_one_detail = print_one_detail_masked_watchpoint;
16049 ops->print_mention = print_mention_masked_watchpoint;
16050 ops->print_recreate = print_recreate_masked_watchpoint;
16051
16052 /* Tracepoints. */
16053 ops = &tracepoint_breakpoint_ops;
16054 *ops = base_breakpoint_ops;
16055 ops->re_set = tracepoint_re_set;
16056 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16057 ops->print_one_detail = tracepoint_print_one_detail;
16058 ops->print_mention = tracepoint_print_mention;
16059 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16060 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16061 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16062 ops->decode_location = tracepoint_decode_location;
983af33b 16063
55aa24fb
SDJ
16064 /* Probe tracepoints. */
16065 ops = &tracepoint_probe_breakpoint_ops;
16066 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16067 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16068 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16069
983af33b
SDJ
16070 /* Static tracepoints with marker (`-m'). */
16071 ops = &strace_marker_breakpoint_ops;
16072 *ops = tracepoint_breakpoint_ops;
5f700d83 16073 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16074 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16075 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16076
16077 /* Fork catchpoints. */
16078 ops = &catch_fork_breakpoint_ops;
16079 *ops = base_breakpoint_ops;
16080 ops->insert_location = insert_catch_fork;
16081 ops->remove_location = remove_catch_fork;
16082 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16083 ops->print_it = print_it_catch_fork;
16084 ops->print_one = print_one_catch_fork;
16085 ops->print_mention = print_mention_catch_fork;
16086 ops->print_recreate = print_recreate_catch_fork;
16087
16088 /* Vfork catchpoints. */
16089 ops = &catch_vfork_breakpoint_ops;
16090 *ops = base_breakpoint_ops;
16091 ops->insert_location = insert_catch_vfork;
16092 ops->remove_location = remove_catch_vfork;
16093 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16094 ops->print_it = print_it_catch_vfork;
16095 ops->print_one = print_one_catch_vfork;
16096 ops->print_mention = print_mention_catch_vfork;
16097 ops->print_recreate = print_recreate_catch_vfork;
16098
16099 /* Exec catchpoints. */
16100 ops = &catch_exec_breakpoint_ops;
16101 *ops = base_breakpoint_ops;
16102 ops->dtor = dtor_catch_exec;
16103 ops->insert_location = insert_catch_exec;
16104 ops->remove_location = remove_catch_exec;
16105 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16106 ops->print_it = print_it_catch_exec;
16107 ops->print_one = print_one_catch_exec;
16108 ops->print_mention = print_mention_catch_exec;
16109 ops->print_recreate = print_recreate_catch_exec;
16110
edcc5120
TT
16111 /* Solib-related catchpoints. */
16112 ops = &catch_solib_breakpoint_ops;
16113 *ops = base_breakpoint_ops;
16114 ops->dtor = dtor_catch_solib;
16115 ops->insert_location = insert_catch_solib;
16116 ops->remove_location = remove_catch_solib;
16117 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16118 ops->check_status = check_status_catch_solib;
16119 ops->print_it = print_it_catch_solib;
16120 ops->print_one = print_one_catch_solib;
16121 ops->print_mention = print_mention_catch_solib;
16122 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16123
16124 ops = &dprintf_breakpoint_ops;
16125 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16126 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16127 ops->resources_needed = bkpt_resources_needed;
16128 ops->print_it = bkpt_print_it;
16129 ops->print_mention = bkpt_print_mention;
2d9442cc 16130 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16131 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16132 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16133}
16134
8bfd80db
YQ
16135/* Chain containing all defined "enable breakpoint" subcommands. */
16136
16137static struct cmd_list_element *enablebreaklist = NULL;
16138
c906108c 16139void
fba45db2 16140_initialize_breakpoint (void)
c906108c
SS
16141{
16142 struct cmd_list_element *c;
16143
2060206e
PA
16144 initialize_breakpoint_ops ();
16145
84acb35a 16146 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16147 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16148 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16149
55aa24fb
SDJ
16150 breakpoint_objfile_key
16151 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16152
c906108c
SS
16153 breakpoint_chain = 0;
16154 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16155 before a breakpoint is set. */
16156 breakpoint_count = 0;
16157
1042e4c0
SS
16158 tracepoint_count = 0;
16159
1bedd215
AC
16160 add_com ("ignore", class_breakpoint, ignore_command, _("\
16161Set ignore-count of breakpoint number N to COUNT.\n\
16162Usage is `ignore N COUNT'."));
c906108c 16163
1bedd215
AC
16164 add_com ("commands", class_breakpoint, commands_command, _("\
16165Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16166Give breakpoint number as argument after \"commands\".\n\
16167With no argument, the targeted breakpoint is the last one set.\n\
16168The commands themselves follow starting on the next line.\n\
16169Type a line containing \"end\" to indicate the end of them.\n\
16170Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16171then no output is printed when it is hit, except what the commands print."));
c906108c 16172
d55637df 16173 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16174Specify breakpoint number N to break only if COND is true.\n\
c906108c 16175Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16176expression to be evaluated whenever breakpoint N is reached."));
d55637df 16177 set_cmd_completer (c, condition_completer);
c906108c 16178
1bedd215 16179 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16180Set a temporary breakpoint.\n\
c906108c
SS
16181Like \"break\" except the breakpoint is only temporary,\n\
16182so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16183by using \"enable delete\" on the breakpoint number.\n\
16184\n"
16185BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16186 set_cmd_completer (c, location_completer);
c94fdfd0 16187
1bedd215 16188 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16189Set a hardware assisted breakpoint.\n\
c906108c 16190Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16191some target hardware may not have this support.\n\
16192\n"
16193BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16194 set_cmd_completer (c, location_completer);
c906108c 16195
1bedd215 16196 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16197Set a temporary hardware assisted breakpoint.\n\
c906108c 16198Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16199so it will be deleted when hit.\n\
16200\n"
16201BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16202 set_cmd_completer (c, location_completer);
c906108c 16203
1bedd215
AC
16204 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16205Enable some breakpoints.\n\
c906108c
SS
16206Give breakpoint numbers (separated by spaces) as arguments.\n\
16207With no subcommand, breakpoints are enabled until you command otherwise.\n\
16208This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16209With a subcommand you can enable temporarily."),
c906108c 16210 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16211
16212 add_com_alias ("en", "enable", class_breakpoint, 1);
16213
84951ab5 16214 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16215Enable some breakpoints.\n\
c906108c
SS
16216Give breakpoint numbers (separated by spaces) as arguments.\n\
16217This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16218May be abbreviated to simply \"enable\".\n"),
c5aa993b 16219 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16220
1a966eab
AC
16221 add_cmd ("once", no_class, enable_once_command, _("\
16222Enable breakpoints for one hit. Give breakpoint numbers.\n\
16223If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16224 &enablebreaklist);
16225
1a966eab
AC
16226 add_cmd ("delete", no_class, enable_delete_command, _("\
16227Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16228If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16229 &enablebreaklist);
16230
816338b5
SS
16231 add_cmd ("count", no_class, enable_count_command, _("\
16232Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16233If a breakpoint is hit while enabled in this fashion,\n\
16234the count is decremented; when it reaches zero, the breakpoint is disabled."),
16235 &enablebreaklist);
16236
1a966eab
AC
16237 add_cmd ("delete", no_class, enable_delete_command, _("\
16238Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16239If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16240 &enablelist);
16241
1a966eab
AC
16242 add_cmd ("once", no_class, enable_once_command, _("\
16243Enable breakpoints for one hit. Give breakpoint numbers.\n\
16244If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16245 &enablelist);
16246
16247 add_cmd ("count", no_class, enable_count_command, _("\
16248Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16249If a breakpoint is hit while enabled in this fashion,\n\
16250the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16251 &enablelist);
16252
1bedd215
AC
16253 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16254Disable some breakpoints.\n\
c906108c
SS
16255Arguments are breakpoint numbers with spaces in between.\n\
16256To disable all breakpoints, give no argument.\n\
64b9b334 16257A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16258 &disablelist, "disable ", 1, &cmdlist);
16259 add_com_alias ("dis", "disable", class_breakpoint, 1);
16260 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16261
1a966eab
AC
16262 add_cmd ("breakpoints", class_alias, disable_command, _("\
16263Disable some breakpoints.\n\
c906108c
SS
16264Arguments are breakpoint numbers with spaces in between.\n\
16265To disable all breakpoints, give no argument.\n\
64b9b334 16266A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16267This command may be abbreviated \"disable\"."),
c906108c
SS
16268 &disablelist);
16269
1bedd215
AC
16270 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16271Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16272Arguments are breakpoint numbers with spaces in between.\n\
16273To delete all breakpoints, give no argument.\n\
16274\n\
16275Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16276The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16277 &deletelist, "delete ", 1, &cmdlist);
16278 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16279 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16280
1a966eab
AC
16281 add_cmd ("breakpoints", class_alias, delete_command, _("\
16282Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16283Arguments are breakpoint numbers with spaces in between.\n\
16284To delete all breakpoints, give no argument.\n\
1a966eab 16285This command may be abbreviated \"delete\"."),
c906108c
SS
16286 &deletelist);
16287
1bedd215 16288 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16289Clear breakpoint at specified location.\n\
16290Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16291\n\
16292With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16293is executing in.\n"
16294"\n" LOCATION_HELP_STRING "\n\
1bedd215 16295See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16296 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16297
1bedd215 16298 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16299Set breakpoint at specified location.\n"
31e2b00f 16300BREAK_ARGS_HELP ("break")));
5ba2abeb 16301 set_cmd_completer (c, location_completer);
c94fdfd0 16302
c906108c
SS
16303 add_com_alias ("b", "break", class_run, 1);
16304 add_com_alias ("br", "break", class_run, 1);
16305 add_com_alias ("bre", "break", class_run, 1);
16306 add_com_alias ("brea", "break", class_run, 1);
16307
c906108c
SS
16308 if (dbx_commands)
16309 {
1bedd215
AC
16310 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16311Break in function/address or break at a line in the current file."),
c5aa993b
JM
16312 &stoplist, "stop ", 1, &cmdlist);
16313 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16314 _("Break in function or address."), &stoplist);
c5aa993b 16315 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16316 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16317 add_com ("status", class_info, breakpoints_info, _("\
16318Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16319The \"Type\" column indicates one of:\n\
16320\tbreakpoint - normal breakpoint\n\
16321\twatchpoint - watchpoint\n\
16322The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16323the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16324breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16325address and file/line number respectively.\n\
16326\n\
16327Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16328are set to the address of the last breakpoint listed unless the command\n\
16329is prefixed with \"server \".\n\n\
c906108c 16330Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16331breakpoint set."));
c906108c
SS
16332 }
16333
1bedd215 16334 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16335Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16336The \"Type\" column indicates one of:\n\
16337\tbreakpoint - normal breakpoint\n\
16338\twatchpoint - watchpoint\n\
16339The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16340the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16341breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16342address and file/line number respectively.\n\
16343\n\
16344Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16345are set to the address of the last breakpoint listed unless the command\n\
16346is prefixed with \"server \".\n\n\
c906108c 16347Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16348breakpoint set."));
c906108c 16349
6b04bdb7
MS
16350 add_info_alias ("b", "breakpoints", 1);
16351
1a966eab
AC
16352 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16353Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16354The \"Type\" column indicates one of:\n\
16355\tbreakpoint - normal breakpoint\n\
16356\twatchpoint - watchpoint\n\
16357\tlongjmp - internal breakpoint used to step through longjmp()\n\
16358\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16359\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16360\tfinish - internal breakpoint used by the \"finish\" command\n\
16361The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16362the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16363breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16364address and file/line number respectively.\n\
16365\n\
16366Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16367are set to the address of the last breakpoint listed unless the command\n\
16368is prefixed with \"server \".\n\n\
c906108c 16369Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16370breakpoint set."),
c906108c
SS
16371 &maintenanceinfolist);
16372
44feb3ce
TT
16373 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16374Set catchpoints to catch events."),
16375 &catch_cmdlist, "catch ",
16376 0/*allow-unknown*/, &cmdlist);
16377
16378 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16379Set temporary catchpoints to catch events."),
16380 &tcatch_cmdlist, "tcatch ",
16381 0/*allow-unknown*/, &cmdlist);
16382
44feb3ce
TT
16383 add_catch_command ("fork", _("Catch calls to fork."),
16384 catch_fork_command_1,
a96d9b2e 16385 NULL,
44feb3ce
TT
16386 (void *) (uintptr_t) catch_fork_permanent,
16387 (void *) (uintptr_t) catch_fork_temporary);
16388 add_catch_command ("vfork", _("Catch calls to vfork."),
16389 catch_fork_command_1,
a96d9b2e 16390 NULL,
44feb3ce
TT
16391 (void *) (uintptr_t) catch_vfork_permanent,
16392 (void *) (uintptr_t) catch_vfork_temporary);
16393 add_catch_command ("exec", _("Catch calls to exec."),
16394 catch_exec_command_1,
a96d9b2e
SDJ
16395 NULL,
16396 CATCH_PERMANENT,
16397 CATCH_TEMPORARY);
edcc5120
TT
16398 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16399Usage: catch load [REGEX]\n\
16400If REGEX is given, only stop for libraries matching the regular expression."),
16401 catch_load_command_1,
16402 NULL,
16403 CATCH_PERMANENT,
16404 CATCH_TEMPORARY);
16405 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16406Usage: catch unload [REGEX]\n\
16407If REGEX is given, only stop for libraries matching the regular expression."),
16408 catch_unload_command_1,
16409 NULL,
16410 CATCH_PERMANENT,
16411 CATCH_TEMPORARY);
c5aa993b 16412
1bedd215
AC
16413 c = add_com ("watch", class_breakpoint, watch_command, _("\
16414Set a watchpoint for an expression.\n\
06a64a0b 16415Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16416A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16417an expression changes.\n\
16418If -l or -location is given, this evaluates EXPRESSION and watches\n\
16419the memory to which it refers."));
65d12d83 16420 set_cmd_completer (c, expression_completer);
c906108c 16421
1bedd215
AC
16422 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16423Set a read watchpoint for an expression.\n\
06a64a0b 16424Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16425A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16426an expression is read.\n\
16427If -l or -location is given, this evaluates EXPRESSION and watches\n\
16428the memory to which it refers."));
65d12d83 16429 set_cmd_completer (c, expression_completer);
c906108c 16430
1bedd215
AC
16431 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16432Set a watchpoint for an expression.\n\
06a64a0b 16433Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16434A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16435an expression is either read or written.\n\
16436If -l or -location is given, this evaluates EXPRESSION and watches\n\
16437the memory to which it refers."));
65d12d83 16438 set_cmd_completer (c, expression_completer);
c906108c 16439
d77f58be 16440 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16441Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16442
920d2a44
AC
16443 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16444 respond to changes - contrary to the description. */
85c07804
AC
16445 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16446 &can_use_hw_watchpoints, _("\
16447Set debugger's willingness to use watchpoint hardware."), _("\
16448Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16449If zero, gdb will not use hardware for new watchpoints, even if\n\
16450such is available. (However, any hardware watchpoints that were\n\
16451created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16452hardware.)"),
16453 NULL,
920d2a44 16454 show_can_use_hw_watchpoints,
85c07804 16455 &setlist, &showlist);
c906108c
SS
16456
16457 can_use_hw_watchpoints = 1;
fa8d40ab 16458
1042e4c0
SS
16459 /* Tracepoint manipulation commands. */
16460
16461 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16462Set a tracepoint at specified location.\n\
1042e4c0
SS
16463\n"
16464BREAK_ARGS_HELP ("trace") "\n\
16465Do \"help tracepoints\" for info on other tracepoint commands."));
16466 set_cmd_completer (c, location_completer);
16467
16468 add_com_alias ("tp", "trace", class_alias, 0);
16469 add_com_alias ("tr", "trace", class_alias, 1);
16470 add_com_alias ("tra", "trace", class_alias, 1);
16471 add_com_alias ("trac", "trace", class_alias, 1);
16472
7a697b8d 16473 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16474Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16475\n"
16476BREAK_ARGS_HELP ("ftrace") "\n\
16477Do \"help tracepoints\" for info on other tracepoint commands."));
16478 set_cmd_completer (c, location_completer);
16479
0fb4aa4b 16480 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16481Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16482\n\
16483strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16484LOCATION may be a linespec, explicit, or address location (described below) \n\
16485or -m MARKER_ID.\n\n\
16486If a marker id is specified, probe the marker with that name. With\n\
16487no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16488Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16489This collects arbitrary user data passed in the probe point call to the\n\
16490tracing library. You can inspect it when analyzing the trace buffer,\n\
16491by printing the $_sdata variable like any other convenience variable.\n\
16492\n\
16493CONDITION is a boolean expression.\n\
629500fa 16494\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16495Multiple tracepoints at one place are permitted, and useful if their\n\
16496conditions are different.\n\
0fb4aa4b
PA
16497\n\
16498Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16499Do \"help tracepoints\" for info on other tracepoint commands."));
16500 set_cmd_completer (c, location_completer);
16501
1042e4c0 16502 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16503Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16504Convenience variable \"$tpnum\" contains the number of the\n\
16505last tracepoint set."));
16506
16507 add_info_alias ("tp", "tracepoints", 1);
16508
16509 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16510Delete specified tracepoints.\n\
16511Arguments are tracepoint numbers, separated by spaces.\n\
16512No argument means delete all tracepoints."),
16513 &deletelist);
7e20dfcd 16514 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16515
16516 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16517Disable specified tracepoints.\n\
16518Arguments are tracepoint numbers, separated by spaces.\n\
16519No argument means disable all tracepoints."),
16520 &disablelist);
16521 deprecate_cmd (c, "disable");
16522
16523 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16524Enable specified tracepoints.\n\
16525Arguments are tracepoint numbers, separated by spaces.\n\
16526No argument means enable all tracepoints."),
16527 &enablelist);
16528 deprecate_cmd (c, "enable");
16529
16530 add_com ("passcount", class_trace, trace_pass_command, _("\
16531Set the passcount for a tracepoint.\n\
16532The trace will end when the tracepoint has been passed 'count' times.\n\
16533Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16534if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16535
6149aea9
PA
16536 add_prefix_cmd ("save", class_breakpoint, save_command,
16537 _("Save breakpoint definitions as a script."),
16538 &save_cmdlist, "save ",
16539 0/*allow-unknown*/, &cmdlist);
16540
16541 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16542Save current breakpoint definitions as a script.\n\
cce7e648 16543This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16544catchpoints, tracepoints). Use the 'source' command in another debug\n\
16545session to restore them."),
16546 &save_cmdlist);
16547 set_cmd_completer (c, filename_completer);
16548
16549 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16550Save current tracepoint definitions as a script.\n\
6149aea9
PA
16551Use the 'source' command in another debug session to restore them."),
16552 &save_cmdlist);
1042e4c0
SS
16553 set_cmd_completer (c, filename_completer);
16554
6149aea9
PA
16555 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16556 deprecate_cmd (c, "save tracepoints");
16557
1bedd215 16558 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16559Breakpoint specific settings\n\
16560Configure various breakpoint-specific variables such as\n\
1bedd215 16561pending breakpoint behavior"),
fa8d40ab
JJ
16562 &breakpoint_set_cmdlist, "set breakpoint ",
16563 0/*allow-unknown*/, &setlist);
1bedd215 16564 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16565Breakpoint specific settings\n\
16566Configure various breakpoint-specific variables such as\n\
1bedd215 16567pending breakpoint behavior"),
fa8d40ab
JJ
16568 &breakpoint_show_cmdlist, "show breakpoint ",
16569 0/*allow-unknown*/, &showlist);
16570
7915a72c
AC
16571 add_setshow_auto_boolean_cmd ("pending", no_class,
16572 &pending_break_support, _("\
16573Set debugger's behavior regarding pending breakpoints."), _("\
16574Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16575If on, an unrecognized breakpoint location will cause gdb to create a\n\
16576pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16577an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16578user-query to see if a pending breakpoint should be created."),
2c5b56ce 16579 NULL,
920d2a44 16580 show_pending_break_support,
6e1d7d6c
AC
16581 &breakpoint_set_cmdlist,
16582 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16583
16584 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16585
16586 add_setshow_boolean_cmd ("auto-hw", no_class,
16587 &automatic_hardware_breakpoints, _("\
16588Set automatic usage of hardware breakpoints."), _("\
16589Show automatic usage of hardware breakpoints."), _("\
16590If set, the debugger will automatically use hardware breakpoints for\n\
16591breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16592a warning will be emitted for such breakpoints."),
16593 NULL,
16594 show_automatic_hardware_breakpoints,
16595 &breakpoint_set_cmdlist,
16596 &breakpoint_show_cmdlist);
74960c60 16597
a25a5a45
PA
16598 add_setshow_boolean_cmd ("always-inserted", class_support,
16599 &always_inserted_mode, _("\
74960c60
VP
16600Set mode for inserting breakpoints."), _("\
16601Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16602When this mode is on, breakpoints are inserted immediately as soon as\n\
16603they're created, kept inserted even when execution stops, and removed\n\
16604only when the user deletes them. When this mode is off (the default),\n\
16605breakpoints are inserted only when execution continues, and removed\n\
16606when execution stops."),
72d0e2c5
YQ
16607 NULL,
16608 &show_always_inserted_mode,
16609 &breakpoint_set_cmdlist,
16610 &breakpoint_show_cmdlist);
f1310107 16611
b775012e
LM
16612 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16613 condition_evaluation_enums,
16614 &condition_evaluation_mode_1, _("\
16615Set mode of breakpoint condition evaluation."), _("\
16616Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16617When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16618evaluated on the host's side by GDB. When it is set to \"target\",\n\
16619breakpoint conditions will be downloaded to the target (if the target\n\
16620supports such feature) and conditions will be evaluated on the target's side.\n\
16621If this is set to \"auto\" (default), this will be automatically set to\n\
16622\"target\" if it supports condition evaluation, otherwise it will\n\
16623be set to \"gdb\""),
16624 &set_condition_evaluation_mode,
16625 &show_condition_evaluation_mode,
16626 &breakpoint_set_cmdlist,
16627 &breakpoint_show_cmdlist);
16628
f1310107
TJB
16629 add_com ("break-range", class_breakpoint, break_range_command, _("\
16630Set a breakpoint for an address range.\n\
16631break-range START-LOCATION, END-LOCATION\n\
16632where START-LOCATION and END-LOCATION can be one of the following:\n\
16633 LINENUM, for that line in the current file,\n\
16634 FILE:LINENUM, for that line in that file,\n\
16635 +OFFSET, for that number of lines after the current line\n\
16636 or the start of the range\n\
16637 FUNCTION, for the first line in that function,\n\
16638 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16639 *ADDRESS, for the instruction at that address.\n\
16640\n\
16641The breakpoint will stop execution of the inferior whenever it executes\n\
16642an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16643range (including START-LOCATION and END-LOCATION)."));
16644
e7e0cddf 16645 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16646Set a dynamic printf at specified location.\n\
e7e0cddf 16647dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16648location may be a linespec, explicit, or address location.\n"
16649"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16650 set_cmd_completer (c, location_completer);
16651
16652 add_setshow_enum_cmd ("dprintf-style", class_support,
16653 dprintf_style_enums, &dprintf_style, _("\
16654Set the style of usage for dynamic printf."), _("\
16655Show the style of usage for dynamic printf."), _("\
16656This setting chooses how GDB will do a dynamic printf.\n\
16657If the value is \"gdb\", then the printing is done by GDB to its own\n\
16658console, as with the \"printf\" command.\n\
16659If the value is \"call\", the print is done by calling a function in your\n\
16660program; by default printf(), but you can choose a different function or\n\
16661output stream by setting dprintf-function and dprintf-channel."),
16662 update_dprintf_commands, NULL,
16663 &setlist, &showlist);
16664
16665 dprintf_function = xstrdup ("printf");
16666 add_setshow_string_cmd ("dprintf-function", class_support,
16667 &dprintf_function, _("\
16668Set the function to use for dynamic printf"), _("\
16669Show the function to use for dynamic printf"), NULL,
16670 update_dprintf_commands, NULL,
16671 &setlist, &showlist);
16672
16673 dprintf_channel = xstrdup ("");
16674 add_setshow_string_cmd ("dprintf-channel", class_support,
16675 &dprintf_channel, _("\
16676Set the channel to use for dynamic printf"), _("\
16677Show the channel to use for dynamic printf"), NULL,
16678 update_dprintf_commands, NULL,
16679 &setlist, &showlist);
16680
d3ce09f5
SS
16681 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16682 &disconnected_dprintf, _("\
16683Set whether dprintf continues after GDB disconnects."), _("\
16684Show whether dprintf continues after GDB disconnects."), _("\
16685Use this to let dprintf commands continue to hit and produce output\n\
16686even if GDB disconnects or detaches from the target."),
16687 NULL,
16688 NULL,
16689 &setlist, &showlist);
16690
16691 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16692agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16693(target agent only) This is useful for formatted output in user-defined commands."));
16694
765dc015 16695 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16696
16697 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16698 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16699}
This page took 3.092055 seconds and 4 git commands to generate.