Add default_breakpoint_from_pc
[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"
325fac50 83#include <algorithm>
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
95a42b64
TT
105static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
c906108c 108
a14ed312 109static void ignore_command (char *, int);
c906108c 110
4efb68b1 111static int breakpoint_re_set_one (void *);
c906108c 112
348d480f
PA
113static void breakpoint_re_set_default (struct breakpoint *);
114
f00aae0f
KS
115static void
116 create_sals_from_location_default (const struct event_location *location,
117 struct linespec_result *canonical,
118 enum bptype type_wanted);
983af33b
SDJ
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
e7e0cddf 122 char *, char *, enum bptype,
983af33b
SDJ
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
44f238bb 126 int, int, int, unsigned);
983af33b 127
f00aae0f
KS
128static void decode_location_default (struct breakpoint *b,
129 const struct event_location *location,
c2f4122d 130 struct program_space *search_pspace,
f00aae0f 131 struct symtabs_and_lines *sals);
983af33b 132
a14ed312 133static void clear_command (char *, int);
c906108c 134
a14ed312 135static void catch_command (char *, int);
c906108c 136
a9634178 137static int can_use_hardware_watchpoint (struct value *);
c906108c 138
98deb0da 139static void break_command_1 (char *, int, int);
c906108c 140
a14ed312 141static void mention (struct breakpoint *);
c906108c 142
348d480f
PA
143static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
c0a91b2b 145 const struct breakpoint_ops *);
3742cc8b
YQ
146static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
4a64f543
MS
149/* This function is used in gdbtk sources and thus can not be made
150 static. */
63c252f8 151struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 152 struct symtab_and_line,
c0a91b2b
TT
153 enum bptype,
154 const struct breakpoint_ops *);
c906108c 155
06edf0c0
PA
156static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
a1aa2221
LM
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
06edf0c0 161
76897487
KB
162static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
a6d9a66e
UW
164static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
88f7da05 166 enum bptype bptype);
76897487 167
6c95b8df
PA
168static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
5af949e3 170 struct obj_section *, int);
c906108c 171
85d721b8
PA
172static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
f1310107
TJB
175static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
d35ae833
PA
179static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
a14ed312 183static void breakpoints_info (char *, int);
c906108c 184
d77f58be
SS
185static void watchpoints_info (char *, int);
186
e5a67952
MS
187static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
c906108c 189
4efb68b1 190static int breakpoint_cond_eval (void *);
c906108c 191
4efb68b1 192static void cleanup_executing_breakpoints (void *);
c906108c 193
a14ed312 194static void commands_command (char *, int);
c906108c 195
a14ed312 196static void condition_command (char *, int);
c906108c 197
834c0d03 198static int remove_breakpoint (struct bp_location *);
b2b6a7da 199static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 200
e514a9d6 201static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 202
4efb68b1 203static int watchpoint_check (void *);
c906108c 204
a14ed312 205static void maintenance_info_breakpoints (char *, int);
c906108c 206
a14ed312 207static int hw_breakpoint_used_count (void);
c906108c 208
a1398e0c
PA
209static int hw_watchpoint_use_count (struct breakpoint *);
210
211static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
c906108c 214
a14ed312 215static void hbreak_command (char *, int);
c906108c 216
a14ed312 217static void thbreak_command (char *, int);
c906108c 218
816338b5
SS
219static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
c906108c 221
a14ed312 222static void stop_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopin_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void stopat_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
fe3f5fa8 230static void free_bp_location (struct bp_location *loc);
f431efe5
PA
231static void incref_bp_location (struct bp_location *loc);
232static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 233
39d61571 234static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 235
44702360
PA
236/* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238enum ugll_insert_mode
239{
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
a25a5a45
PA
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
04086b45
PA
259 UGLL_MAY_INSERT,
260
a25a5a45
PA
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
04086b45 268 UGLL_INSERT
44702360
PA
269};
270
271static void update_global_location_list (enum ugll_insert_mode);
a5606eee 272
44702360 273static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 274
d77f58be 275static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
276
277static void insert_breakpoint_locations (void);
a5606eee 278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305/* Internal breakpoints class type. */
06edf0c0 306static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
307
308/* Momentary breakpoints class type. */
06edf0c0
PA
309static struct breakpoint_ops momentary_breakpoint_ops;
310
e2e4d78b
JK
311/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312static struct breakpoint_ops longjmp_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347static char *dprintf_function = "";
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357static char *dprintf_channel = "";
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
372316f1
PA
474 struct thread_info *tp;
475
a25a5a45
PA
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
b57bacec
PA
483 if (threads_are_executing ())
484 return 1;
372316f1
PA
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593
JK
580#define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_location; \
582 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_location + bp_location_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
876fa593
JK
609/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
610
611static struct bp_location **bp_location;
612
613/* Number of elements of BP_LOCATION. */
614
615static unsigned bp_location_count;
616
4a64f543
MS
617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
876fa593
JK
622
623static CORE_ADDR bp_location_placed_address_before_address_max;
624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
876fa593
JK
630
631static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 632
4a64f543
MS
633/* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
badd37ce
SDJ
656/* See declaration at breakpoint.h. */
657
658struct breakpoint *
659breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661{
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671}
672
468d015d
JJ
673/* Return whether a breakpoint is an active enabled breakpoint. */
674static int
675breakpoint_enabled (struct breakpoint *b)
676{
0d381245 677 return (b->enable_state == bp_enabled);
468d015d
JJ
678}
679
c906108c
SS
680/* Set breakpoint count to NUM. */
681
95a42b64 682static void
fba45db2 683set_breakpoint_count (int num)
c906108c 684{
86b17b60 685 prev_breakpoint_count = breakpoint_count;
c906108c 686 breakpoint_count = num;
4fa62494 687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
688}
689
86b17b60
PA
690/* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692static int rbreak_start_breakpoint_count;
693
95a42b64
TT
694/* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
86b17b60 696
95a42b64
TT
697void
698start_rbreak_breakpoints (void)
699{
86b17b60 700 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
701}
702
703/* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
86b17b60 705
95a42b64
TT
706void
707end_rbreak_breakpoints (void)
708{
86b17b60 709 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
710}
711
4a64f543 712/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
713
714void
fba45db2 715clear_breakpoint_hit_counts (void)
c906108c
SS
716{
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721}
722
9add0f1b
TT
723/* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726static struct counted_command_line *
727alloc_counted_command_line (struct command_line *commands)
728{
8d749320 729 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 730
9add0f1b
TT
731 result->refc = 1;
732 result->commands = commands;
8d749320 733
9add0f1b
TT
734 return result;
735}
736
737/* Increment reference count. This does nothing if CMD is NULL. */
738
739static void
740incref_counted_command_line (struct counted_command_line *cmd)
741{
742 if (cmd)
743 ++cmd->refc;
744}
745
746/* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750static void
751decref_counted_command_line (struct counted_command_line **cmdp)
752{
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762}
763
764/* A cleanup function that calls decref_counted_command_line. */
765
766static void
767do_cleanup_counted_command_line (void *arg)
768{
9a3c8263 769 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
770}
771
772/* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775static struct cleanup *
776make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777{
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779}
780
c906108c 781\f
48cb2d85
VP
782/* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785struct breakpoint *
786get_breakpoint (int num)
787{
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795}
5c44784c 796
c906108c 797\f
adc36818 798
b775012e
LM
799/* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802static void
803mark_breakpoint_modified (struct breakpoint *b)
804{
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820}
821
822/* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825static void
826mark_breakpoint_location_modified (struct bp_location *loc)
827{
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841}
842
843/* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846static void
847set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849{
b775012e
LM
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
abf1152a
JK
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
b775012e
LM
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
b775012e
LM
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
44702360 897 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
898 }
899
900 return;
901}
902
903/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906static void
907show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909{
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919}
920
921/* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_location_compare function. */
924
925static int
926bp_location_compare_addrs (const void *ap, const void *bp)
927{
9a3c8263
SM
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935}
936
937/* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942static struct bp_location **
943get_first_locp_gte_addr (CORE_ADDR address)
944{
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 memset (&dummy_loc, 0, sizeof (struct bp_location));
951 dummy_loc.address = address;
952
953 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
954 locp_found = ((struct bp_location **)
955 bsearch (&dummy_locp, bp_location, bp_location_count,
956 sizeof (struct bp_location **),
957 bp_location_compare_addrs));
b775012e
LM
958
959 /* Nothing was found, nothing left to do. */
960 if (locp_found == NULL)
961 return NULL;
962
963 /* We may have found a location that is at ADDRESS but is not the first in the
964 location's list. Go backwards (if possible) and locate the first one. */
965 while ((locp_found - 1) >= bp_location
966 && (*(locp_found - 1))->address == address)
967 locp_found--;
968
969 return locp_found;
970}
971
adc36818 972void
7a26bd4d 973set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
974 int from_tty)
975{
3a5c3e22
PA
976 xfree (b->cond_string);
977 b->cond_string = NULL;
adc36818 978
3a5c3e22 979 if (is_watchpoint (b))
adc36818 980 {
3a5c3e22
PA
981 struct watchpoint *w = (struct watchpoint *) b;
982
983 xfree (w->cond_exp);
984 w->cond_exp = NULL;
985 }
986 else
987 {
988 struct bp_location *loc;
989
990 for (loc = b->loc; loc; loc = loc->next)
991 {
992 xfree (loc->cond);
993 loc->cond = NULL;
b775012e
LM
994
995 /* No need to free the condition agent expression
996 bytecode (if we have one). We will handle this
997 when we go through update_global_location_list. */
3a5c3e22 998 }
adc36818 999 }
adc36818
PM
1000
1001 if (*exp == 0)
1002 {
1003 if (from_tty)
1004 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1005 }
1006 else
1007 {
bbc13ae3 1008 const char *arg = exp;
cc59ec59 1009
adc36818
PM
1010 /* I don't know if it matters whether this is the string the user
1011 typed in or the decompiled expression. */
1012 b->cond_string = xstrdup (arg);
1013 b->condition_not_parsed = 0;
1014
1015 if (is_watchpoint (b))
1016 {
3a5c3e22
PA
1017 struct watchpoint *w = (struct watchpoint *) b;
1018
adc36818
PM
1019 innermost_block = NULL;
1020 arg = exp;
1bb9788d 1021 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1022 if (*arg)
1023 error (_("Junk at end of expression"));
3a5c3e22 1024 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1025 }
1026 else
1027 {
3a5c3e22
PA
1028 struct bp_location *loc;
1029
adc36818
PM
1030 for (loc = b->loc; loc; loc = loc->next)
1031 {
1032 arg = exp;
1033 loc->cond =
1bb9788d
TT
1034 parse_exp_1 (&arg, loc->address,
1035 block_for_pc (loc->address), 0);
adc36818
PM
1036 if (*arg)
1037 error (_("Junk at end of expression"));
1038 }
1039 }
1040 }
b775012e
LM
1041 mark_breakpoint_modified (b);
1042
8d3788bd 1043 observer_notify_breakpoint_modified (b);
adc36818
PM
1044}
1045
d55637df
TT
1046/* Completion for the "condition" command. */
1047
1048static VEC (char_ptr) *
6f937416
PA
1049condition_completer (struct cmd_list_element *cmd,
1050 const char *text, const char *word)
d55637df 1051{
6f937416 1052 const char *space;
d55637df 1053
6f937416
PA
1054 text = skip_spaces_const (text);
1055 space = skip_to_space_const (text);
d55637df
TT
1056 if (*space == '\0')
1057 {
1058 int len;
1059 struct breakpoint *b;
1060 VEC (char_ptr) *result = NULL;
1061
1062 if (text[0] == '$')
1063 {
1064 /* We don't support completion of history indices. */
1065 if (isdigit (text[1]))
1066 return NULL;
1067 return complete_internalvar (&text[1]);
1068 }
1069
1070 /* We're completing the breakpoint number. */
1071 len = strlen (text);
1072
1073 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1074 {
1075 char number[50];
1076
1077 xsnprintf (number, sizeof (number), "%d", b->number);
1078
1079 if (strncmp (number, text, len) == 0)
1080 VEC_safe_push (char_ptr, result, xstrdup (number));
1081 }
d55637df
TT
1082
1083 return result;
1084 }
1085
1086 /* We're completing the expression part. */
6f937416 1087 text = skip_spaces_const (space);
d55637df
TT
1088 return expression_completer (cmd, text, word);
1089}
1090
c906108c
SS
1091/* condition N EXP -- set break condition of breakpoint N to EXP. */
1092
1093static void
fba45db2 1094condition_command (char *arg, int from_tty)
c906108c 1095{
52f0bd74 1096 struct breakpoint *b;
c906108c 1097 char *p;
52f0bd74 1098 int bnum;
c906108c
SS
1099
1100 if (arg == 0)
e2e0b3e5 1101 error_no_arg (_("breakpoint number"));
c906108c
SS
1102
1103 p = arg;
1104 bnum = get_number (&p);
5c44784c 1105 if (bnum == 0)
8a3fe4f8 1106 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1107
1108 ALL_BREAKPOINTS (b)
1109 if (b->number == bnum)
2f069f6f 1110 {
6dddc817
DE
1111 /* Check if this breakpoint has a "stop" method implemented in an
1112 extension language. This method and conditions entered into GDB
1113 from the CLI are mutually exclusive. */
1114 const struct extension_language_defn *extlang
1115 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1116
1117 if (extlang != NULL)
1118 {
1119 error (_("Only one stop condition allowed. There is currently"
1120 " a %s stop condition defined for this breakpoint."),
1121 ext_lang_capitalized_name (extlang));
1122 }
2566ad2d 1123 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1124
1125 if (is_breakpoint (b))
44702360 1126 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1127
2f069f6f
JB
1128 return;
1129 }
c906108c 1130
8a3fe4f8 1131 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1132}
1133
a7bdde9e
VP
1134/* Check that COMMAND do not contain commands that are suitable
1135 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1136 Throw if any such commands is found. */
1137
a7bdde9e
VP
1138static void
1139check_no_tracepoint_commands (struct command_line *commands)
1140{
1141 struct command_line *c;
cc59ec59 1142
a7bdde9e
VP
1143 for (c = commands; c; c = c->next)
1144 {
1145 int i;
1146
1147 if (c->control_type == while_stepping_control)
3e43a32a
MS
1148 error (_("The 'while-stepping' command can "
1149 "only be used for tracepoints"));
a7bdde9e
VP
1150
1151 for (i = 0; i < c->body_count; ++i)
1152 check_no_tracepoint_commands ((c->body_list)[i]);
1153
1154 /* Not that command parsing removes leading whitespace and comment
4a64f543 1155 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1156 command directly. */
1157 if (strstr (c->line, "collect ") == c->line)
1158 error (_("The 'collect' command can only be used for tracepoints"));
1159
51661e93
VP
1160 if (strstr (c->line, "teval ") == c->line)
1161 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1162 }
1163}
1164
d77f58be
SS
1165/* Encapsulate tests for different types of tracepoints. */
1166
d9b3f62e
PA
1167static int
1168is_tracepoint_type (enum bptype type)
1169{
1170 return (type == bp_tracepoint
1171 || type == bp_fast_tracepoint
1172 || type == bp_static_tracepoint);
1173}
1174
a7bdde9e 1175int
d77f58be 1176is_tracepoint (const struct breakpoint *b)
a7bdde9e 1177{
d9b3f62e 1178 return is_tracepoint_type (b->type);
a7bdde9e 1179}
d9b3f62e 1180
e5dd4106 1181/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1182 breakpoint. This function will throw an exception if a problem is
1183 found. */
48cb2d85 1184
95a42b64
TT
1185static void
1186validate_commands_for_breakpoint (struct breakpoint *b,
1187 struct command_line *commands)
48cb2d85 1188{
d77f58be 1189 if (is_tracepoint (b))
a7bdde9e 1190 {
c9a6ce02 1191 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1192 struct command_line *c;
1193 struct command_line *while_stepping = 0;
c9a6ce02
PA
1194
1195 /* Reset the while-stepping step count. The previous commands
1196 might have included a while-stepping action, while the new
1197 ones might not. */
1198 t->step_count = 0;
1199
1200 /* We need to verify that each top-level element of commands is
1201 valid for tracepoints, that there's at most one
1202 while-stepping element, and that the while-stepping's body
1203 has valid tracing commands excluding nested while-stepping.
1204 We also need to validate the tracepoint action line in the
1205 context of the tracepoint --- validate_actionline actually
1206 has side effects, like setting the tracepoint's
1207 while-stepping STEP_COUNT, in addition to checking if the
1208 collect/teval actions parse and make sense in the
1209 tracepoint's context. */
a7bdde9e
VP
1210 for (c = commands; c; c = c->next)
1211 {
a7bdde9e
VP
1212 if (c->control_type == while_stepping_control)
1213 {
1214 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1215 error (_("The 'while-stepping' command "
1216 "cannot be used for fast tracepoint"));
0fb4aa4b 1217 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1218 error (_("The 'while-stepping' command "
1219 "cannot be used for static tracepoint"));
a7bdde9e
VP
1220
1221 if (while_stepping)
3e43a32a
MS
1222 error (_("The 'while-stepping' command "
1223 "can be used only once"));
a7bdde9e
VP
1224 else
1225 while_stepping = c;
1226 }
c9a6ce02
PA
1227
1228 validate_actionline (c->line, b);
a7bdde9e
VP
1229 }
1230 if (while_stepping)
1231 {
1232 struct command_line *c2;
1233
1234 gdb_assert (while_stepping->body_count == 1);
1235 c2 = while_stepping->body_list[0];
1236 for (; c2; c2 = c2->next)
1237 {
a7bdde9e
VP
1238 if (c2->control_type == while_stepping_control)
1239 error (_("The 'while-stepping' command cannot be nested"));
1240 }
1241 }
1242 }
1243 else
1244 {
1245 check_no_tracepoint_commands (commands);
1246 }
95a42b64
TT
1247}
1248
0fb4aa4b
PA
1249/* Return a vector of all the static tracepoints set at ADDR. The
1250 caller is responsible for releasing the vector. */
1251
1252VEC(breakpoint_p) *
1253static_tracepoints_here (CORE_ADDR addr)
1254{
1255 struct breakpoint *b;
1256 VEC(breakpoint_p) *found = 0;
1257 struct bp_location *loc;
1258
1259 ALL_BREAKPOINTS (b)
1260 if (b->type == bp_static_tracepoint)
1261 {
1262 for (loc = b->loc; loc; loc = loc->next)
1263 if (loc->address == addr)
1264 VEC_safe_push(breakpoint_p, found, b);
1265 }
1266
1267 return found;
1268}
1269
95a42b64 1270/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1271 validate that only allowed commands are included. */
95a42b64
TT
1272
1273void
4a64f543
MS
1274breakpoint_set_commands (struct breakpoint *b,
1275 struct command_line *commands)
95a42b64
TT
1276{
1277 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1278
9add0f1b
TT
1279 decref_counted_command_line (&b->commands);
1280 b->commands = alloc_counted_command_line (commands);
8d3788bd 1281 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1282}
1283
45a43567
TT
1284/* Set the internal `silent' flag on the breakpoint. Note that this
1285 is not the same as the "silent" that may appear in the breakpoint's
1286 commands. */
1287
1288void
1289breakpoint_set_silent (struct breakpoint *b, int silent)
1290{
1291 int old_silent = b->silent;
1292
1293 b->silent = silent;
1294 if (old_silent != silent)
8d3788bd 1295 observer_notify_breakpoint_modified (b);
45a43567
TT
1296}
1297
1298/* Set the thread for this breakpoint. If THREAD is -1, make the
1299 breakpoint work for any thread. */
1300
1301void
1302breakpoint_set_thread (struct breakpoint *b, int thread)
1303{
1304 int old_thread = b->thread;
1305
1306 b->thread = thread;
1307 if (old_thread != thread)
8d3788bd 1308 observer_notify_breakpoint_modified (b);
45a43567
TT
1309}
1310
1311/* Set the task for this breakpoint. If TASK is 0, make the
1312 breakpoint work for any task. */
1313
1314void
1315breakpoint_set_task (struct breakpoint *b, int task)
1316{
1317 int old_task = b->task;
1318
1319 b->task = task;
1320 if (old_task != task)
8d3788bd 1321 observer_notify_breakpoint_modified (b);
45a43567
TT
1322}
1323
95a42b64
TT
1324void
1325check_tracepoint_command (char *line, void *closure)
a7bdde9e 1326{
9a3c8263 1327 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1328
6f937416 1329 validate_actionline (line, b);
a7bdde9e
VP
1330}
1331
95a42b64
TT
1332/* A structure used to pass information through
1333 map_breakpoint_numbers. */
1334
1335struct commands_info
1336{
1337 /* True if the command was typed at a tty. */
1338 int from_tty;
86b17b60
PA
1339
1340 /* The breakpoint range spec. */
1341 char *arg;
1342
95a42b64
TT
1343 /* Non-NULL if the body of the commands are being read from this
1344 already-parsed command. */
1345 struct command_line *control;
86b17b60 1346
95a42b64
TT
1347 /* The command lines read from the user, or NULL if they have not
1348 yet been read. */
1349 struct counted_command_line *cmd;
1350};
1351
1352/* A callback for map_breakpoint_numbers that sets the commands for
1353 commands_command. */
1354
c906108c 1355static void
95a42b64 1356do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1357{
9a3c8263 1358 struct commands_info *info = (struct commands_info *) data;
c906108c 1359
95a42b64
TT
1360 if (info->cmd == NULL)
1361 {
1362 struct command_line *l;
5c44784c 1363
95a42b64
TT
1364 if (info->control != NULL)
1365 l = copy_command_lines (info->control->body_list[0]);
1366 else
86b17b60
PA
1367 {
1368 struct cleanup *old_chain;
1369 char *str;
c5aa993b 1370
3e43a32a
MS
1371 str = xstrprintf (_("Type commands for breakpoint(s) "
1372 "%s, one per line."),
86b17b60
PA
1373 info->arg);
1374
1375 old_chain = make_cleanup (xfree, str);
1376
1377 l = read_command_lines (str,
1378 info->from_tty, 1,
d77f58be 1379 (is_tracepoint (b)
86b17b60
PA
1380 ? check_tracepoint_command : 0),
1381 b);
1382
1383 do_cleanups (old_chain);
1384 }
a7bdde9e 1385
95a42b64
TT
1386 info->cmd = alloc_counted_command_line (l);
1387 }
1388
1389 /* If a breakpoint was on the list more than once, we don't need to
1390 do anything. */
1391 if (b->commands != info->cmd)
1392 {
1393 validate_commands_for_breakpoint (b, info->cmd->commands);
1394 incref_counted_command_line (info->cmd);
1395 decref_counted_command_line (&b->commands);
1396 b->commands = info->cmd;
8d3788bd 1397 observer_notify_breakpoint_modified (b);
c5aa993b 1398 }
95a42b64
TT
1399}
1400
1401static void
4a64f543
MS
1402commands_command_1 (char *arg, int from_tty,
1403 struct command_line *control)
95a42b64
TT
1404{
1405 struct cleanup *cleanups;
1406 struct commands_info info;
1407
1408 info.from_tty = from_tty;
1409 info.control = control;
1410 info.cmd = NULL;
1411 /* If we read command lines from the user, then `info' will hold an
1412 extra reference to the commands that we must clean up. */
1413 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1414
1415 if (arg == NULL || !*arg)
1416 {
86b17b60 1417 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1418 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1419 breakpoint_count);
95a42b64
TT
1420 else if (breakpoint_count > 0)
1421 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1422 else
1423 {
1424 /* So that we don't try to free the incoming non-NULL
1425 argument in the cleanup below. Mapping breakpoint
1426 numbers will fail in this case. */
1427 arg = NULL;
1428 }
95a42b64 1429 }
9766ced4
SS
1430 else
1431 /* The command loop has some static state, so we need to preserve
1432 our argument. */
1433 arg = xstrdup (arg);
86b17b60
PA
1434
1435 if (arg != NULL)
1436 make_cleanup (xfree, arg);
1437
1438 info.arg = arg;
95a42b64
TT
1439
1440 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1441
1442 if (info.cmd == NULL)
1443 error (_("No breakpoints specified."));
1444
1445 do_cleanups (cleanups);
1446}
1447
1448static void
1449commands_command (char *arg, int from_tty)
1450{
1451 commands_command_1 (arg, from_tty, NULL);
c906108c 1452}
40c03ae8
EZ
1453
1454/* Like commands_command, but instead of reading the commands from
1455 input stream, takes them from an already parsed command structure.
1456
1457 This is used by cli-script.c to DTRT with breakpoint commands
1458 that are part of if and while bodies. */
1459enum command_control_type
1460commands_from_control_command (char *arg, struct command_line *cmd)
1461{
95a42b64
TT
1462 commands_command_1 (arg, 0, cmd);
1463 return simple_control;
40c03ae8 1464}
876fa593
JK
1465
1466/* Return non-zero if BL->TARGET_INFO contains valid information. */
1467
1468static int
1469bp_location_has_shadow (struct bp_location *bl)
1470{
1471 if (bl->loc_type != bp_loc_software_breakpoint)
1472 return 0;
1473 if (!bl->inserted)
1474 return 0;
1475 if (bl->target_info.shadow_len == 0)
e5dd4106 1476 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1477 return 0;
1478 return 1;
1479}
1480
9d497a19
PA
1481/* Update BUF, which is LEN bytes read from the target address
1482 MEMADDR, by replacing a memory breakpoint with its shadowed
1483 contents.
1484
1485 If READBUF is not NULL, this buffer must not overlap with the of
1486 the breakpoint location's shadow_contents buffer. Otherwise, a
1487 failed assertion internal error will be raised. */
1488
1489static void
1490one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1491 const gdb_byte *writebuf_org,
1492 ULONGEST memaddr, LONGEST len,
1493 struct bp_target_info *target_info,
1494 struct gdbarch *gdbarch)
1495{
1496 /* Now do full processing of the found relevant range of elements. */
1497 CORE_ADDR bp_addr = 0;
1498 int bp_size = 0;
1499 int bptoffset = 0;
1500
1501 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1502 current_program_space->aspace, 0))
1503 {
1504 /* The breakpoint is inserted in a different address space. */
1505 return;
1506 }
1507
1508 /* Addresses and length of the part of the breakpoint that
1509 we need to copy. */
1510 bp_addr = target_info->placed_address;
1511 bp_size = target_info->shadow_len;
1512
1513 if (bp_addr + bp_size <= memaddr)
1514 {
1515 /* The breakpoint is entirely before the chunk of memory we are
1516 reading. */
1517 return;
1518 }
1519
1520 if (bp_addr >= memaddr + len)
1521 {
1522 /* The breakpoint is entirely after the chunk of memory we are
1523 reading. */
1524 return;
1525 }
1526
1527 /* Offset within shadow_contents. */
1528 if (bp_addr < memaddr)
1529 {
1530 /* Only copy the second part of the breakpoint. */
1531 bp_size -= memaddr - bp_addr;
1532 bptoffset = memaddr - bp_addr;
1533 bp_addr = memaddr;
1534 }
1535
1536 if (bp_addr + bp_size > memaddr + len)
1537 {
1538 /* Only copy the first part of the breakpoint. */
1539 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1540 }
1541
1542 if (readbuf != NULL)
1543 {
1544 /* Verify that the readbuf buffer does not overlap with the
1545 shadow_contents buffer. */
1546 gdb_assert (target_info->shadow_contents >= readbuf + len
1547 || readbuf >= (target_info->shadow_contents
1548 + target_info->shadow_len));
1549
1550 /* Update the read buffer with this inserted breakpoint's
1551 shadow. */
1552 memcpy (readbuf + bp_addr - memaddr,
1553 target_info->shadow_contents + bptoffset, bp_size);
1554 }
1555 else
1556 {
1557 const unsigned char *bp;
0d5ed153
MR
1558 CORE_ADDR addr = target_info->reqstd_address;
1559 int placed_size;
9d497a19
PA
1560
1561 /* Update the shadow with what we want to write to memory. */
1562 memcpy (target_info->shadow_contents + bptoffset,
1563 writebuf_org + bp_addr - memaddr, bp_size);
1564
1565 /* Determine appropriate breakpoint contents and size for this
1566 address. */
0d5ed153 1567 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1568
1569 /* Update the final write buffer with this inserted
1570 breakpoint's INSN. */
1571 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1572 }
1573}
1574
8defab1a 1575/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1576 by replacing any memory breakpoints with their shadowed contents.
1577
35c63cd8
JB
1578 If READBUF is not NULL, this buffer must not overlap with any of
1579 the breakpoint location's shadow_contents buffers. Otherwise,
1580 a failed assertion internal error will be raised.
1581
876fa593 1582 The range of shadowed area by each bp_location is:
35df4500
TJB
1583 bl->address - bp_location_placed_address_before_address_max
1584 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1585 The range we were requested to resolve shadows for is:
1586 memaddr ... memaddr + len
1587 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1588 memaddr + len <= (bl->address
1589 - bp_location_placed_address_before_address_max)
876fa593 1590 and:
35df4500 1591 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1592
8defab1a 1593void
f0ba3972
PA
1594breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1595 const gdb_byte *writebuf_org,
1596 ULONGEST memaddr, LONGEST len)
c906108c 1597{
4a64f543
MS
1598 /* Left boundary, right boundary and median element of our binary
1599 search. */
876fa593
JK
1600 unsigned bc_l, bc_r, bc;
1601
4a64f543
MS
1602 /* Find BC_L which is a leftmost element which may affect BUF
1603 content. It is safe to report lower value but a failure to
1604 report higher one. */
876fa593
JK
1605
1606 bc_l = 0;
1607 bc_r = bp_location_count;
1608 while (bc_l + 1 < bc_r)
1609 {
35df4500 1610 struct bp_location *bl;
876fa593
JK
1611
1612 bc = (bc_l + bc_r) / 2;
35df4500 1613 bl = bp_location[bc];
876fa593 1614
4a64f543
MS
1615 /* Check first BL->ADDRESS will not overflow due to the added
1616 constant. Then advance the left boundary only if we are sure
1617 the BC element can in no way affect the BUF content (MEMADDR
1618 to MEMADDR + LEN range).
876fa593 1619
4a64f543
MS
1620 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1621 offset so that we cannot miss a breakpoint with its shadow
1622 range tail still reaching MEMADDR. */
c5aa993b 1623
35df4500
TJB
1624 if ((bl->address + bp_location_shadow_len_after_address_max
1625 >= bl->address)
1626 && (bl->address + bp_location_shadow_len_after_address_max
1627 <= memaddr))
876fa593
JK
1628 bc_l = bc;
1629 else
1630 bc_r = bc;
1631 }
1632
128070bb
PA
1633 /* Due to the binary search above, we need to make sure we pick the
1634 first location that's at BC_L's address. E.g., if there are
1635 multiple locations at the same address, BC_L may end up pointing
1636 at a duplicate location, and miss the "master"/"inserted"
1637 location. Say, given locations L1, L2 and L3 at addresses A and
1638 B:
1639
1640 L1@A, L2@A, L3@B, ...
1641
1642 BC_L could end up pointing at location L2, while the "master"
1643 location could be L1. Since the `loc->inserted' flag is only set
1644 on "master" locations, we'd forget to restore the shadow of L1
1645 and L2. */
1646 while (bc_l > 0
1647 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1648 bc_l--;
1649
876fa593
JK
1650 /* Now do full processing of the found relevant range of elements. */
1651
1652 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1653 {
35df4500 1654 struct bp_location *bl = bp_location[bc];
876fa593 1655
35df4500
TJB
1656 /* bp_location array has BL->OWNER always non-NULL. */
1657 if (bl->owner->type == bp_none)
8a3fe4f8 1658 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1659 bl->owner->number);
ffce0d52 1660
e5dd4106 1661 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1662 content. */
1663
35df4500
TJB
1664 if (bl->address >= bp_location_placed_address_before_address_max
1665 && memaddr + len <= (bl->address
1666 - bp_location_placed_address_before_address_max))
876fa593
JK
1667 break;
1668
35df4500 1669 if (!bp_location_has_shadow (bl))
c5aa993b 1670 continue;
6c95b8df 1671
9d497a19
PA
1672 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1673 memaddr, len, &bl->target_info, bl->gdbarch);
1674 }
c906108c 1675}
9d497a19 1676
c906108c 1677\f
c5aa993b 1678
b775012e
LM
1679/* Return true if BPT is either a software breakpoint or a hardware
1680 breakpoint. */
1681
1682int
1683is_breakpoint (const struct breakpoint *bpt)
1684{
1685 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1686 || bpt->type == bp_hardware_breakpoint
1687 || bpt->type == bp_dprintf);
b775012e
LM
1688}
1689
60e1c644
PA
1690/* Return true if BPT is of any hardware watchpoint kind. */
1691
a5606eee 1692static int
d77f58be 1693is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1694{
1695 return (bpt->type == bp_hardware_watchpoint
1696 || bpt->type == bp_read_watchpoint
1697 || bpt->type == bp_access_watchpoint);
1698}
7270d8f2 1699
60e1c644
PA
1700/* Return true if BPT is of any watchpoint kind, hardware or
1701 software. */
1702
3a5c3e22 1703int
d77f58be 1704is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1705{
1706 return (is_hardware_watchpoint (bpt)
1707 || bpt->type == bp_watchpoint);
1708}
1709
3a5c3e22
PA
1710/* Returns true if the current thread and its running state are safe
1711 to evaluate or update watchpoint B. Watchpoints on local
1712 expressions need to be evaluated in the context of the thread that
1713 was current when the watchpoint was created, and, that thread needs
1714 to be stopped to be able to select the correct frame context.
1715 Watchpoints on global expressions can be evaluated on any thread,
1716 and in any state. It is presently left to the target allowing
1717 memory accesses when threads are running. */
f6bc2008
PA
1718
1719static int
3a5c3e22 1720watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1721{
d0d8b0c6
JK
1722 return (b->base.pspace == current_program_space
1723 && (ptid_equal (b->watchpoint_thread, null_ptid)
1724 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1725 && !is_executing (inferior_ptid))));
f6bc2008
PA
1726}
1727
d0fb5eae
JK
1728/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1729 associated bp_watchpoint_scope breakpoint. */
1730
1731static void
3a5c3e22 1732watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1733{
3a5c3e22 1734 struct breakpoint *b = &w->base;
d0fb5eae
JK
1735
1736 if (b->related_breakpoint != b)
1737 {
1738 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1739 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1740 b->related_breakpoint->disposition = disp_del_at_next_stop;
1741 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1742 b->related_breakpoint = b;
1743 }
1744 b->disposition = disp_del_at_next_stop;
1745}
1746
bb9d5f81
PP
1747/* Extract a bitfield value from value VAL using the bit parameters contained in
1748 watchpoint W. */
1749
1750static struct value *
1751extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1752{
1753 struct value *bit_val;
1754
1755 if (val == NULL)
1756 return NULL;
1757
1758 bit_val = allocate_value (value_type (val));
1759
1760 unpack_value_bitfield (bit_val,
1761 w->val_bitpos,
1762 w->val_bitsize,
1763 value_contents_for_printing (val),
1764 value_offset (val),
1765 val);
1766
1767 return bit_val;
1768}
1769
c6d81124
PA
1770/* Allocate a dummy location and add it to B, which must be a software
1771 watchpoint. This is required because even if a software watchpoint
1772 is not watching any memory, bpstat_stop_status requires a location
1773 to be able to report stops. */
1774
1775static void
1776software_watchpoint_add_no_memory_location (struct breakpoint *b,
1777 struct program_space *pspace)
1778{
1779 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1780
1781 b->loc = allocate_bp_location (b);
1782 b->loc->pspace = pspace;
1783 b->loc->address = -1;
1784 b->loc->length = -1;
1785}
1786
1787/* Returns true if B is a software watchpoint that is not watching any
1788 memory (e.g., "watch $pc"). */
1789
1790static int
1791is_no_memory_software_watchpoint (struct breakpoint *b)
1792{
1793 return (b->type == bp_watchpoint
1794 && b->loc != NULL
1795 && b->loc->next == NULL
1796 && b->loc->address == -1
1797 && b->loc->length == -1);
1798}
1799
567e1b4e
JB
1800/* Assuming that B is a watchpoint:
1801 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1802 - Evaluate expression and store the result in B->val
567e1b4e
JB
1803 - Evaluate the condition if there is one, and store the result
1804 in b->loc->cond.
a5606eee
VP
1805 - Update the list of values that must be watched in B->loc.
1806
4a64f543
MS
1807 If the watchpoint disposition is disp_del_at_next_stop, then do
1808 nothing. If this is local watchpoint that is out of scope, delete
1809 it.
1810
1811 Even with `set breakpoint always-inserted on' the watchpoints are
1812 removed + inserted on each stop here. Normal breakpoints must
1813 never be removed because they might be missed by a running thread
1814 when debugging in non-stop mode. On the other hand, hardware
1815 watchpoints (is_hardware_watchpoint; processed here) are specific
1816 to each LWP since they are stored in each LWP's hardware debug
1817 registers. Therefore, such LWP must be stopped first in order to
1818 be able to modify its hardware watchpoints.
1819
1820 Hardware watchpoints must be reset exactly once after being
1821 presented to the user. It cannot be done sooner, because it would
1822 reset the data used to present the watchpoint hit to the user. And
1823 it must not be done later because it could display the same single
1824 watchpoint hit during multiple GDB stops. Note that the latter is
1825 relevant only to the hardware watchpoint types bp_read_watchpoint
1826 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1827 not user-visible - its hit is suppressed if the memory content has
1828 not changed.
1829
1830 The following constraints influence the location where we can reset
1831 hardware watchpoints:
1832
1833 * target_stopped_by_watchpoint and target_stopped_data_address are
1834 called several times when GDB stops.
1835
1836 [linux]
1837 * Multiple hardware watchpoints can be hit at the same time,
1838 causing GDB to stop. GDB only presents one hardware watchpoint
1839 hit at a time as the reason for stopping, and all the other hits
1840 are presented later, one after the other, each time the user
1841 requests the execution to be resumed. Execution is not resumed
1842 for the threads still having pending hit event stored in
1843 LWP_INFO->STATUS. While the watchpoint is already removed from
1844 the inferior on the first stop the thread hit event is kept being
1845 reported from its cached value by linux_nat_stopped_data_address
1846 until the real thread resume happens after the watchpoint gets
1847 presented and thus its LWP_INFO->STATUS gets reset.
1848
1849 Therefore the hardware watchpoint hit can get safely reset on the
1850 watchpoint removal from inferior. */
a79d3c27 1851
b40ce68a 1852static void
3a5c3e22 1853update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1854{
a5606eee 1855 int within_current_scope;
a5606eee 1856 struct frame_id saved_frame_id;
66076460 1857 int frame_saved;
a5606eee 1858
f6bc2008
PA
1859 /* If this is a local watchpoint, we only want to check if the
1860 watchpoint frame is in scope if the current thread is the thread
1861 that was used to create the watchpoint. */
1862 if (!watchpoint_in_thread_scope (b))
1863 return;
1864
3a5c3e22 1865 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1866 return;
1867
66076460 1868 frame_saved = 0;
a5606eee
VP
1869
1870 /* Determine if the watchpoint is within scope. */
1871 if (b->exp_valid_block == NULL)
1872 within_current_scope = 1;
1873 else
1874 {
b5db5dfc
UW
1875 struct frame_info *fi = get_current_frame ();
1876 struct gdbarch *frame_arch = get_frame_arch (fi);
1877 CORE_ADDR frame_pc = get_frame_pc (fi);
1878
c9cf6e20
MG
1879 /* If we're at a point where the stack has been destroyed
1880 (e.g. in a function epilogue), unwinding may not work
1881 properly. Do not attempt to recreate locations at this
b5db5dfc 1882 point. See similar comments in watchpoint_check. */
c9cf6e20 1883 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1884 return;
66076460
DJ
1885
1886 /* Save the current frame's ID so we can restore it after
1887 evaluating the watchpoint expression on its own frame. */
1888 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1889 took a frame parameter, so that we didn't have to change the
1890 selected frame. */
1891 frame_saved = 1;
1892 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1893
a5606eee
VP
1894 fi = frame_find_by_id (b->watchpoint_frame);
1895 within_current_scope = (fi != NULL);
1896 if (within_current_scope)
1897 select_frame (fi);
1898 }
1899
b5db5dfc
UW
1900 /* We don't free locations. They are stored in the bp_location array
1901 and update_global_location_list will eventually delete them and
1902 remove breakpoints if needed. */
3a5c3e22 1903 b->base.loc = NULL;
b5db5dfc 1904
a5606eee
VP
1905 if (within_current_scope && reparse)
1906 {
bbc13ae3 1907 const char *s;
d63d0675 1908
a5606eee
VP
1909 if (b->exp)
1910 {
1911 xfree (b->exp);
1912 b->exp = NULL;
1913 }
d63d0675 1914 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1915 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1916 /* If the meaning of expression itself changed, the old value is
1917 no longer relevant. We don't want to report a watchpoint hit
1918 to the user when the old value and the new value may actually
1919 be completely different objects. */
1920 value_free (b->val);
fa4727a6
DJ
1921 b->val = NULL;
1922 b->val_valid = 0;
60e1c644
PA
1923
1924 /* Note that unlike with breakpoints, the watchpoint's condition
1925 expression is stored in the breakpoint object, not in the
1926 locations (re)created below. */
3a5c3e22 1927 if (b->base.cond_string != NULL)
60e1c644
PA
1928 {
1929 if (b->cond_exp != NULL)
1930 {
1931 xfree (b->cond_exp);
1932 b->cond_exp = NULL;
1933 }
1934
3a5c3e22 1935 s = b->base.cond_string;
1bb9788d 1936 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1937 }
a5606eee 1938 }
a5606eee
VP
1939
1940 /* If we failed to parse the expression, for example because
1941 it refers to a global variable in a not-yet-loaded shared library,
1942 don't try to insert watchpoint. We don't automatically delete
1943 such watchpoint, though, since failure to parse expression
1944 is different from out-of-scope watchpoint. */
e8369a73 1945 if (!target_has_execution)
2d134ed3
PA
1946 {
1947 /* Without execution, memory can't change. No use to try and
1948 set watchpoint locations. The watchpoint will be reset when
1949 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1950 if (!can_use_hw_watchpoints)
1951 {
1952 if (b->base.ops->works_in_software_mode (&b->base))
1953 b->base.type = bp_watchpoint;
1954 else
638aa5a1
AB
1955 error (_("Can't set read/access watchpoint when "
1956 "hardware watchpoints are disabled."));
e8369a73 1957 }
2d134ed3
PA
1958 }
1959 else if (within_current_scope && b->exp)
a5606eee 1960 {
0cf6dd15 1961 int pc = 0;
fa4727a6 1962 struct value *val_chain, *v, *result, *next;
2d134ed3 1963 struct program_space *frame_pspace;
a5606eee 1964
3a1115a0 1965 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1966
a5606eee
VP
1967 /* Avoid setting b->val if it's already set. The meaning of
1968 b->val is 'the last value' user saw, and we should update
1969 it only if we reported that last value to user. As it
9c06b0b4
TJB
1970 happens, the code that reports it updates b->val directly.
1971 We don't keep track of the memory value for masked
1972 watchpoints. */
3a5c3e22 1973 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1974 {
bb9d5f81
PP
1975 if (b->val_bitsize != 0)
1976 {
1977 v = extract_bitfield_from_watchpoint_value (b, v);
1978 if (v != NULL)
1979 release_value (v);
1980 }
fa4727a6
DJ
1981 b->val = v;
1982 b->val_valid = 1;
1983 }
a5606eee 1984
2d134ed3
PA
1985 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1986
a5606eee 1987 /* Look at each value on the value chain. */
9fa40276 1988 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1989 {
1990 /* If it's a memory location, and GDB actually needed
1991 its contents to evaluate the expression, then we
fa4727a6
DJ
1992 must watch it. If the first value returned is
1993 still lazy, that means an error occurred reading it;
1994 watch it anyway in case it becomes readable. */
a5606eee 1995 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1996 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1997 {
1998 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1999
a5606eee
VP
2000 /* We only watch structs and arrays if user asked
2001 for it explicitly, never if they just happen to
2002 appear in the middle of some value chain. */
fa4727a6 2003 if (v == result
a5606eee
VP
2004 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2005 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2006 {
2007 CORE_ADDR addr;
f486487f 2008 enum target_hw_bp_type type;
a5606eee 2009 struct bp_location *loc, **tmp;
bb9d5f81
PP
2010 int bitpos = 0, bitsize = 0;
2011
2012 if (value_bitsize (v) != 0)
2013 {
2014 /* Extract the bit parameters out from the bitfield
2015 sub-expression. */
2016 bitpos = value_bitpos (v);
2017 bitsize = value_bitsize (v);
2018 }
2019 else if (v == result && b->val_bitsize != 0)
2020 {
2021 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2022 lvalue whose bit parameters are saved in the fields
2023 VAL_BITPOS and VAL_BITSIZE. */
2024 bitpos = b->val_bitpos;
2025 bitsize = b->val_bitsize;
2026 }
a5606eee 2027
42ae5230 2028 addr = value_address (v);
bb9d5f81
PP
2029 if (bitsize != 0)
2030 {
2031 /* Skip the bytes that don't contain the bitfield. */
2032 addr += bitpos / 8;
2033 }
2034
a5606eee 2035 type = hw_write;
3a5c3e22 2036 if (b->base.type == bp_read_watchpoint)
a5606eee 2037 type = hw_read;
3a5c3e22 2038 else if (b->base.type == bp_access_watchpoint)
a5606eee 2039 type = hw_access;
3a5c3e22
PA
2040
2041 loc = allocate_bp_location (&b->base);
2042 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2043 ;
2044 *tmp = loc;
a6d9a66e 2045 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2046
2047 loc->pspace = frame_pspace;
a5606eee 2048 loc->address = addr;
bb9d5f81
PP
2049
2050 if (bitsize != 0)
2051 {
2052 /* Just cover the bytes that make up the bitfield. */
2053 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2054 }
2055 else
2056 loc->length = TYPE_LENGTH (value_type (v));
2057
a5606eee
VP
2058 loc->watchpoint_type = type;
2059 }
2060 }
9fa40276
TJB
2061 }
2062
2063 /* Change the type of breakpoint between hardware assisted or
2064 an ordinary watchpoint depending on the hardware support
2065 and free hardware slots. REPARSE is set when the inferior
2066 is started. */
a9634178 2067 if (reparse)
9fa40276 2068 {
e09342b5 2069 int reg_cnt;
9fa40276
TJB
2070 enum bp_loc_type loc_type;
2071 struct bp_location *bl;
a5606eee 2072
a9634178 2073 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2074
2075 if (reg_cnt)
9fa40276
TJB
2076 {
2077 int i, target_resources_ok, other_type_used;
a1398e0c 2078 enum bptype type;
9fa40276 2079
a9634178
TJB
2080 /* Use an exact watchpoint when there's only one memory region to be
2081 watched, and only one debug register is needed to watch it. */
2082 b->exact = target_exact_watchpoints && reg_cnt == 1;
2083
9fa40276 2084 /* We need to determine how many resources are already
e09342b5
TJB
2085 used for all other hardware watchpoints plus this one
2086 to see if we still have enough resources to also fit
a1398e0c
PA
2087 this watchpoint in as well. */
2088
2089 /* If this is a software watchpoint, we try to turn it
2090 to a hardware one -- count resources as if B was of
2091 hardware watchpoint type. */
2092 type = b->base.type;
2093 if (type == bp_watchpoint)
2094 type = bp_hardware_watchpoint;
2095
2096 /* This watchpoint may or may not have been placed on
2097 the list yet at this point (it won't be in the list
2098 if we're trying to create it for the first time,
2099 through watch_command), so always account for it
2100 manually. */
2101
2102 /* Count resources used by all watchpoints except B. */
2103 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2104
2105 /* Add in the resources needed for B. */
2106 i += hw_watchpoint_use_count (&b->base);
2107
2108 target_resources_ok
2109 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2110 if (target_resources_ok <= 0)
a9634178 2111 {
3a5c3e22 2112 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2113
2114 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2115 error (_("Target does not support this type of "
2116 "hardware watchpoint."));
9c06b0b4
TJB
2117 else if (target_resources_ok < 0 && !sw_mode)
2118 error (_("There are not enough available hardware "
2119 "resources for this watchpoint."));
a1398e0c
PA
2120
2121 /* Downgrade to software watchpoint. */
2122 b->base.type = bp_watchpoint;
2123 }
2124 else
2125 {
2126 /* If this was a software watchpoint, we've just
2127 found we have enough resources to turn it to a
2128 hardware watchpoint. Otherwise, this is a
2129 nop. */
2130 b->base.type = type;
a9634178 2131 }
9fa40276 2132 }
3a5c3e22 2133 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2134 {
2135 if (!can_use_hw_watchpoints)
2136 error (_("Can't set read/access watchpoint when "
2137 "hardware watchpoints are disabled."));
2138 else
2139 error (_("Expression cannot be implemented with "
2140 "read/access watchpoint."));
2141 }
9fa40276 2142 else
3a5c3e22 2143 b->base.type = bp_watchpoint;
9fa40276 2144
3a5c3e22 2145 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2146 : bp_loc_hardware_watchpoint);
3a5c3e22 2147 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2148 bl->loc_type = loc_type;
2149 }
2150
2151 for (v = val_chain; v; v = next)
2152 {
a5606eee
VP
2153 next = value_next (v);
2154 if (v != b->val)
2155 value_free (v);
2156 }
2157
c7437ca6
PA
2158 /* If a software watchpoint is not watching any memory, then the
2159 above left it without any location set up. But,
2160 bpstat_stop_status requires a location to be able to report
2161 stops, so make sure there's at least a dummy one. */
3a5c3e22 2162 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2163 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2164 }
2165 else if (!within_current_scope)
7270d8f2 2166 {
ac74f770
MS
2167 printf_filtered (_("\
2168Watchpoint %d deleted because the program has left the block\n\
2169in which its expression is valid.\n"),
3a5c3e22 2170 b->base.number);
d0fb5eae 2171 watchpoint_del_at_next_stop (b);
7270d8f2 2172 }
a5606eee
VP
2173
2174 /* Restore the selected frame. */
66076460
DJ
2175 if (frame_saved)
2176 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2177}
2178
a5606eee 2179
74960c60 2180/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2181 inserted in the inferior. We don't differentiate the type of BL's owner
2182 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2183 breakpoint_ops is not defined, because in insert_bp_location,
2184 tracepoint's insert_location will not be called. */
74960c60 2185static int
35df4500 2186should_be_inserted (struct bp_location *bl)
74960c60 2187{
35df4500 2188 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2189 return 0;
2190
35df4500 2191 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2192 return 0;
2193
35df4500 2194 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2195 return 0;
2196
f8eba3c6
TT
2197 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2198 return 0;
2199
56710373
PA
2200 /* This is set for example, when we're attached to the parent of a
2201 vfork, and have detached from the child. The child is running
2202 free, and we expect it to do an exec or exit, at which point the
2203 OS makes the parent schedulable again (and the target reports
2204 that the vfork is done). Until the child is done with the shared
2205 memory region, do not insert breakpoints in the parent, otherwise
2206 the child could still trip on the parent's breakpoints. Since
2207 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2208 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2209 return 0;
2210
31e77af2 2211 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2212 location, except if the breakpoint is a single-step breakpoint,
2213 and the breakpoint's thread is the thread which is stepping past
2214 a breakpoint. */
31e77af2
PA
2215 if ((bl->loc_type == bp_loc_software_breakpoint
2216 || bl->loc_type == bp_loc_hardware_breakpoint)
2217 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2218 bl->address)
2219 /* The single-step breakpoint may be inserted at the location
2220 we're trying to step if the instruction branches to itself.
2221 However, the instruction won't be executed at all and it may
2222 break the semantics of the instruction, for example, the
2223 instruction is a conditional branch or updates some flags.
2224 We can't fix it unless GDB is able to emulate the instruction
2225 or switch to displaced stepping. */
2226 && !(bl->owner->type == bp_single_step
2227 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2228 {
2229 if (debug_infrun)
2230 {
2231 fprintf_unfiltered (gdb_stdlog,
2232 "infrun: skipping breakpoint: "
2233 "stepping past insn at: %s\n",
2234 paddress (bl->gdbarch, bl->address));
2235 }
2236 return 0;
2237 }
31e77af2 2238
963f9c80
PA
2239 /* Don't insert watchpoints if we're trying to step past the
2240 instruction that triggered one. */
2241 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2242 && stepping_past_nonsteppable_watchpoint ())
2243 {
2244 if (debug_infrun)
2245 {
2246 fprintf_unfiltered (gdb_stdlog,
2247 "infrun: stepping past non-steppable watchpoint. "
2248 "skipping watchpoint at %s:%d\n",
2249 paddress (bl->gdbarch, bl->address),
2250 bl->length);
2251 }
2252 return 0;
2253 }
2254
74960c60
VP
2255 return 1;
2256}
2257
934709f0
PW
2258/* Same as should_be_inserted but does the check assuming
2259 that the location is not duplicated. */
2260
2261static int
2262unduplicated_should_be_inserted (struct bp_location *bl)
2263{
2264 int result;
2265 const int save_duplicate = bl->duplicate;
2266
2267 bl->duplicate = 0;
2268 result = should_be_inserted (bl);
2269 bl->duplicate = save_duplicate;
2270 return result;
2271}
2272
b775012e
LM
2273/* Parses a conditional described by an expression COND into an
2274 agent expression bytecode suitable for evaluation
2275 by the bytecode interpreter. Return NULL if there was
2276 any error during parsing. */
2277
2278static struct agent_expr *
2279parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2280{
2281 struct agent_expr *aexpr = NULL;
b775012e
LM
2282
2283 if (!cond)
2284 return NULL;
2285
2286 /* We don't want to stop processing, so catch any errors
2287 that may show up. */
492d29ea 2288 TRY
b775012e
LM
2289 {
2290 aexpr = gen_eval_for_expr (scope, cond);
2291 }
2292
492d29ea 2293 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2294 {
2295 /* If we got here, it means the condition could not be parsed to a valid
2296 bytecode expression and thus can't be evaluated on the target's side.
2297 It's no use iterating through the conditions. */
2298 return NULL;
2299 }
492d29ea 2300 END_CATCH
b775012e
LM
2301
2302 /* We have a valid agent expression. */
2303 return aexpr;
2304}
2305
2306/* Based on location BL, create a list of breakpoint conditions to be
2307 passed on to the target. If we have duplicated locations with different
2308 conditions, we will add such conditions to the list. The idea is that the
2309 target will evaluate the list of conditions and will only notify GDB when
2310 one of them is true. */
2311
2312static void
2313build_target_condition_list (struct bp_location *bl)
2314{
2315 struct bp_location **locp = NULL, **loc2p;
2316 int null_condition_or_parse_error = 0;
2317 int modified = bl->needs_update;
2318 struct bp_location *loc;
2319
8b4f3082
PA
2320 /* Release conditions left over from a previous insert. */
2321 VEC_free (agent_expr_p, bl->target_info.conditions);
2322
b775012e
LM
2323 /* This is only meaningful if the target is
2324 evaluating conditions and if the user has
2325 opted for condition evaluation on the target's
2326 side. */
2327 if (gdb_evaluates_breakpoint_condition_p ()
2328 || !target_supports_evaluation_of_breakpoint_conditions ())
2329 return;
2330
2331 /* Do a first pass to check for locations with no assigned
2332 conditions or conditions that fail to parse to a valid agent expression
2333 bytecode. If any of these happen, then it's no use to send conditions
2334 to the target since this location will always trigger and generate a
2335 response back to GDB. */
2336 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2337 {
2338 loc = (*loc2p);
2339 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2340 {
2341 if (modified)
2342 {
2343 struct agent_expr *aexpr;
2344
2345 /* Re-parse the conditions since something changed. In that
2346 case we already freed the condition bytecodes (see
2347 force_breakpoint_reinsertion). We just
2348 need to parse the condition to bytecodes again. */
2349 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2350 loc->cond_bytecode = aexpr;
b775012e
LM
2351 }
2352
2353 /* If we have a NULL bytecode expression, it means something
2354 went wrong or we have a null condition expression. */
2355 if (!loc->cond_bytecode)
2356 {
2357 null_condition_or_parse_error = 1;
2358 break;
2359 }
2360 }
2361 }
2362
2363 /* If any of these happened, it means we will have to evaluate the conditions
2364 for the location's address on gdb's side. It is no use keeping bytecodes
2365 for all the other duplicate locations, thus we free all of them here.
2366
2367 This is so we have a finer control over which locations' conditions are
2368 being evaluated by GDB or the remote stub. */
2369 if (null_condition_or_parse_error)
2370 {
2371 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2372 {
2373 loc = (*loc2p);
2374 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2375 {
2376 /* Only go as far as the first NULL bytecode is
2377 located. */
2378 if (!loc->cond_bytecode)
2379 return;
2380
2381 free_agent_expr (loc->cond_bytecode);
2382 loc->cond_bytecode = NULL;
2383 }
2384 }
2385 }
2386
2387 /* No NULL conditions or failed bytecode generation. Build a condition list
2388 for this location's address. */
2389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390 {
2391 loc = (*loc2p);
2392 if (loc->cond
2393 && is_breakpoint (loc->owner)
2394 && loc->pspace->num == bl->pspace->num
2395 && loc->owner->enable_state == bp_enabled
2396 && loc->enabled)
2397 /* Add the condition to the vector. This will be used later to send the
2398 conditions to the target. */
2399 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2400 loc->cond_bytecode);
2401 }
2402
2403 return;
2404}
2405
d3ce09f5
SS
2406/* Parses a command described by string CMD into an agent expression
2407 bytecode suitable for evaluation by the bytecode interpreter.
2408 Return NULL if there was any error during parsing. */
2409
2410static struct agent_expr *
2411parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2412{
2413 struct cleanup *old_cleanups = 0;
2414 struct expression *expr, **argvec;
2415 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2416 const char *cmdrest;
2417 const char *format_start, *format_end;
d3ce09f5
SS
2418 struct format_piece *fpieces;
2419 int nargs;
2420 struct gdbarch *gdbarch = get_current_arch ();
2421
2422 if (!cmd)
2423 return NULL;
2424
2425 cmdrest = cmd;
2426
2427 if (*cmdrest == ',')
2428 ++cmdrest;
bbc13ae3 2429 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2430
2431 if (*cmdrest++ != '"')
2432 error (_("No format string following the location"));
2433
2434 format_start = cmdrest;
2435
2436 fpieces = parse_format_string (&cmdrest);
2437
2438 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2439
2440 format_end = cmdrest;
2441
2442 if (*cmdrest++ != '"')
2443 error (_("Bad format string, non-terminated '\"'."));
2444
bbc13ae3 2445 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2446
2447 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2448 error (_("Invalid argument syntax"));
2449
2450 if (*cmdrest == ',')
2451 cmdrest++;
bbc13ae3 2452 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2453
2454 /* For each argument, make an expression. */
2455
2456 argvec = (struct expression **) alloca (strlen (cmd)
2457 * sizeof (struct expression *));
2458
2459 nargs = 0;
2460 while (*cmdrest != '\0')
2461 {
bbc13ae3 2462 const char *cmd1;
d3ce09f5
SS
2463
2464 cmd1 = cmdrest;
2465 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2466 argvec[nargs++] = expr;
2467 cmdrest = cmd1;
2468 if (*cmdrest == ',')
2469 ++cmdrest;
2470 }
2471
2472 /* We don't want to stop processing, so catch any errors
2473 that may show up. */
492d29ea 2474 TRY
d3ce09f5
SS
2475 {
2476 aexpr = gen_printf (scope, gdbarch, 0, 0,
2477 format_start, format_end - format_start,
2478 fpieces, nargs, argvec);
2479 }
492d29ea 2480 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2481 {
2482 /* If we got here, it means the command could not be parsed to a valid
2483 bytecode expression and thus can't be evaluated on the target's side.
2484 It's no use iterating through the other commands. */
492d29ea 2485 aexpr = NULL;
d3ce09f5 2486 }
492d29ea
PA
2487 END_CATCH
2488
2489 do_cleanups (old_cleanups);
d3ce09f5 2490
d3ce09f5
SS
2491 /* We have a valid agent expression, return it. */
2492 return aexpr;
2493}
2494
2495/* Based on location BL, create a list of breakpoint commands to be
2496 passed on to the target. If we have duplicated locations with
2497 different commands, we will add any such to the list. */
2498
2499static void
2500build_target_command_list (struct bp_location *bl)
2501{
2502 struct bp_location **locp = NULL, **loc2p;
2503 int null_command_or_parse_error = 0;
2504 int modified = bl->needs_update;
2505 struct bp_location *loc;
2506
8b4f3082
PA
2507 /* Release commands left over from a previous insert. */
2508 VEC_free (agent_expr_p, bl->target_info.tcommands);
2509
41fac0cf 2510 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2511 return;
2512
41fac0cf
PA
2513 /* For now, limit to agent-style dprintf breakpoints. */
2514 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2515 return;
2516
41fac0cf
PA
2517 /* For now, if we have any duplicate location that isn't a dprintf,
2518 don't install the target-side commands, as that would make the
2519 breakpoint not be reported to the core, and we'd lose
2520 control. */
2521 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2522 {
2523 loc = (*loc2p);
2524 if (is_breakpoint (loc->owner)
2525 && loc->pspace->num == bl->pspace->num
2526 && loc->owner->type != bp_dprintf)
2527 return;
2528 }
2529
d3ce09f5
SS
2530 /* Do a first pass to check for locations with no assigned
2531 conditions or conditions that fail to parse to a valid agent expression
2532 bytecode. If any of these happen, then it's no use to send conditions
2533 to the target since this location will always trigger and generate a
2534 response back to GDB. */
2535 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2536 {
2537 loc = (*loc2p);
2538 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2539 {
2540 if (modified)
2541 {
2542 struct agent_expr *aexpr;
2543
2544 /* Re-parse the commands since something changed. In that
2545 case we already freed the command bytecodes (see
2546 force_breakpoint_reinsertion). We just
2547 need to parse the command to bytecodes again. */
2548 aexpr = parse_cmd_to_aexpr (bl->address,
2549 loc->owner->extra_string);
2550 loc->cmd_bytecode = aexpr;
d3ce09f5
SS
2551 }
2552
2553 /* If we have a NULL bytecode expression, it means something
2554 went wrong or we have a null command expression. */
2555 if (!loc->cmd_bytecode)
2556 {
2557 null_command_or_parse_error = 1;
2558 break;
2559 }
2560 }
2561 }
2562
2563 /* If anything failed, then we're not doing target-side commands,
2564 and so clean up. */
2565 if (null_command_or_parse_error)
2566 {
2567 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2568 {
2569 loc = (*loc2p);
2570 if (is_breakpoint (loc->owner)
2571 && loc->pspace->num == bl->pspace->num)
2572 {
2573 /* Only go as far as the first NULL bytecode is
2574 located. */
40fb6c5e 2575 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2576 return;
2577
40fb6c5e
HZ
2578 free_agent_expr (loc->cmd_bytecode);
2579 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2580 }
2581 }
2582 }
2583
2584 /* No NULL commands or failed bytecode generation. Build a command list
2585 for this location's address. */
2586 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2587 {
2588 loc = (*loc2p);
2589 if (loc->owner->extra_string
2590 && is_breakpoint (loc->owner)
2591 && loc->pspace->num == bl->pspace->num
2592 && loc->owner->enable_state == bp_enabled
2593 && loc->enabled)
2594 /* Add the command to the vector. This will be used later
2595 to send the commands to the target. */
2596 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2597 loc->cmd_bytecode);
2598 }
2599
2600 bl->target_info.persist = 0;
2601 /* Maybe flag this location as persistent. */
2602 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2603 bl->target_info.persist = 1;
2604}
2605
cd6c3b4f
YQ
2606/* Return the kind of breakpoint on address *ADDR. */
2607
2608static int
2609breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2610{
2611 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2612}
2613
35df4500
TJB
2614/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2615 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2616 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2617 Returns 0 for success, 1 if the bp_location type is not supported or
2618 -1 for failure.
879bfdc2 2619
4a64f543
MS
2620 NOTE drow/2003-09-09: This routine could be broken down to an
2621 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2622static int
35df4500 2623insert_bp_location (struct bp_location *bl,
26bb91f3 2624 struct ui_file *tmp_error_stream,
3fbb6ffa 2625 int *disabled_breaks,
dd61ec5c
MW
2626 int *hw_breakpoint_error,
2627 int *hw_bp_error_explained_already)
879bfdc2 2628{
0000e5cc
PA
2629 enum errors bp_err = GDB_NO_ERROR;
2630 const char *bp_err_message = NULL;
879bfdc2 2631
b775012e 2632 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2633 return 0;
2634
35c63cd8
JB
2635 /* Note we don't initialize bl->target_info, as that wipes out
2636 the breakpoint location's shadow_contents if the breakpoint
2637 is still inserted at that location. This in turn breaks
2638 target_read_memory which depends on these buffers when
2639 a memory read is requested at the breakpoint location:
2640 Once the target_info has been wiped, we fail to see that
2641 we have a breakpoint inserted at that address and thus
2642 read the breakpoint instead of returning the data saved in
2643 the breakpoint location's shadow contents. */
0d5ed153 2644 bl->target_info.reqstd_address = bl->address;
35df4500 2645 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2646 bl->target_info.length = bl->length;
8181d85f 2647
b775012e
LM
2648 /* When working with target-side conditions, we must pass all the conditions
2649 for the same breakpoint address down to the target since GDB will not
2650 insert those locations. With a list of breakpoint conditions, the target
2651 can decide when to stop and notify GDB. */
2652
2653 if (is_breakpoint (bl->owner))
2654 {
2655 build_target_condition_list (bl);
d3ce09f5
SS
2656 build_target_command_list (bl);
2657 /* Reset the modification marker. */
b775012e
LM
2658 bl->needs_update = 0;
2659 }
2660
35df4500
TJB
2661 if (bl->loc_type == bp_loc_software_breakpoint
2662 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2663 {
35df4500 2664 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2665 {
2666 /* If the explicitly specified breakpoint type
2667 is not hardware breakpoint, check the memory map to see
2668 if the breakpoint address is in read only memory or not.
4a64f543 2669
765dc015
VP
2670 Two important cases are:
2671 - location type is not hardware breakpoint, memory
2672 is readonly. We change the type of the location to
2673 hardware breakpoint.
4a64f543
MS
2674 - location type is hardware breakpoint, memory is
2675 read-write. This means we've previously made the
2676 location hardware one, but then the memory map changed,
2677 so we undo.
765dc015 2678
4a64f543
MS
2679 When breakpoints are removed, remove_breakpoints will use
2680 location types we've just set here, the only possible
2681 problem is that memory map has changed during running
2682 program, but it's not going to work anyway with current
2683 gdb. */
765dc015 2684 struct mem_region *mr
0d5ed153 2685 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2686
2687 if (mr)
2688 {
2689 if (automatic_hardware_breakpoints)
2690 {
765dc015
VP
2691 enum bp_loc_type new_type;
2692
2693 if (mr->attrib.mode != MEM_RW)
2694 new_type = bp_loc_hardware_breakpoint;
2695 else
2696 new_type = bp_loc_software_breakpoint;
2697
35df4500 2698 if (new_type != bl->loc_type)
765dc015
VP
2699 {
2700 static int said = 0;
cc59ec59 2701
35df4500 2702 bl->loc_type = new_type;
765dc015
VP
2703 if (!said)
2704 {
3e43a32a
MS
2705 fprintf_filtered (gdb_stdout,
2706 _("Note: automatically using "
2707 "hardware breakpoints for "
2708 "read-only addresses.\n"));
765dc015
VP
2709 said = 1;
2710 }
2711 }
2712 }
35df4500 2713 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2714 && mr->attrib.mode != MEM_RW)
2715 {
2716 fprintf_unfiltered (tmp_error_stream,
2717 _("Cannot insert breakpoint %d.\n"
2718 "Cannot set software breakpoint "
2719 "at read-only address %s\n"),
2720 bl->owner->number,
2721 paddress (bl->gdbarch, bl->address));
2722 return 1;
2723 }
765dc015
VP
2724 }
2725 }
2726
879bfdc2
DJ
2727 /* First check to see if we have to handle an overlay. */
2728 if (overlay_debugging == ovly_off
35df4500
TJB
2729 || bl->section == NULL
2730 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2731 {
2732 /* No overlay handling: just set the breakpoint. */
492d29ea 2733 TRY
dd61ec5c 2734 {
0000e5cc
PA
2735 int val;
2736
dd61ec5c 2737 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2738 if (val)
2739 bp_err = GENERIC_ERROR;
dd61ec5c 2740 }
492d29ea 2741 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2742 {
0000e5cc
PA
2743 bp_err = e.error;
2744 bp_err_message = e.message;
dd61ec5c 2745 }
492d29ea 2746 END_CATCH
879bfdc2
DJ
2747 }
2748 else
2749 {
4a64f543 2750 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2751 Shall we set a breakpoint at the LMA? */
2752 if (!overlay_events_enabled)
2753 {
2754 /* Yes -- overlay event support is not active,
2755 so we must try to set a breakpoint at the LMA.
2756 This will not work for a hardware breakpoint. */
35df4500 2757 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2758 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2759 bl->owner->number);
879bfdc2
DJ
2760 else
2761 {
35df4500
TJB
2762 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2763 bl->section);
879bfdc2 2764 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2765 bl->overlay_target_info = bl->target_info;
0d5ed153 2766 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2767
2768 /* No overlay handling: just set the breakpoint. */
492d29ea 2769 TRY
0000e5cc
PA
2770 {
2771 int val;
2772
579c6ad9 2773 bl->overlay_target_info.kind
cd6c3b4f
YQ
2774 = breakpoint_kind (bl, &addr);
2775 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2776 val = target_insert_breakpoint (bl->gdbarch,
2777 &bl->overlay_target_info);
2778 if (val)
2779 bp_err = GENERIC_ERROR;
2780 }
492d29ea 2781 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2782 {
2783 bp_err = e.error;
2784 bp_err_message = e.message;
2785 }
492d29ea 2786 END_CATCH
0000e5cc
PA
2787
2788 if (bp_err != GDB_NO_ERROR)
99361f52 2789 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2790 "Overlay breakpoint %d "
2791 "failed: in ROM?\n",
35df4500 2792 bl->owner->number);
879bfdc2
DJ
2793 }
2794 }
2795 /* Shall we set a breakpoint at the VMA? */
35df4500 2796 if (section_is_mapped (bl->section))
879bfdc2
DJ
2797 {
2798 /* Yes. This overlay section is mapped into memory. */
492d29ea 2799 TRY
dd61ec5c 2800 {
0000e5cc
PA
2801 int val;
2802
dd61ec5c 2803 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2804 if (val)
2805 bp_err = GENERIC_ERROR;
dd61ec5c 2806 }
492d29ea 2807 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2808 {
0000e5cc
PA
2809 bp_err = e.error;
2810 bp_err_message = e.message;
dd61ec5c 2811 }
492d29ea 2812 END_CATCH
879bfdc2
DJ
2813 }
2814 else
2815 {
2816 /* No. This breakpoint will not be inserted.
2817 No error, but do not mark the bp as 'inserted'. */
2818 return 0;
2819 }
2820 }
2821
0000e5cc 2822 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2823 {
2824 /* Can't set the breakpoint. */
0000e5cc
PA
2825
2826 /* In some cases, we might not be able to insert a
2827 breakpoint in a shared library that has already been
2828 removed, but we have not yet processed the shlib unload
2829 event. Unfortunately, some targets that implement
076855f9
PA
2830 breakpoint insertion themselves can't tell why the
2831 breakpoint insertion failed (e.g., the remote target
2832 doesn't define error codes), so we must treat generic
2833 errors as memory errors. */
0000e5cc 2834 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2835 && bl->loc_type == bp_loc_software_breakpoint
08351840 2836 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2837 || shared_objfile_contains_address_p (bl->pspace,
2838 bl->address)))
879bfdc2 2839 {
4a64f543 2840 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2841 bl->shlib_disabled = 1;
8d3788bd 2842 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2843 if (!*disabled_breaks)
2844 {
2845 fprintf_unfiltered (tmp_error_stream,
2846 "Cannot insert breakpoint %d.\n",
2847 bl->owner->number);
2848 fprintf_unfiltered (tmp_error_stream,
2849 "Temporarily disabling shared "
2850 "library breakpoints:\n");
2851 }
2852 *disabled_breaks = 1;
879bfdc2 2853 fprintf_unfiltered (tmp_error_stream,
35df4500 2854 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2855 return 0;
879bfdc2
DJ
2856 }
2857 else
879bfdc2 2858 {
35df4500 2859 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2860 {
0000e5cc
PA
2861 *hw_breakpoint_error = 1;
2862 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2863 fprintf_unfiltered (tmp_error_stream,
2864 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2865 bl->owner->number, bp_err_message ? ":" : ".\n");
2866 if (bp_err_message != NULL)
2867 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2868 }
2869 else
2870 {
0000e5cc
PA
2871 if (bp_err_message == NULL)
2872 {
2873 char *message
2874 = memory_error_message (TARGET_XFER_E_IO,
2875 bl->gdbarch, bl->address);
2876 struct cleanup *old_chain = make_cleanup (xfree, message);
2877
2878 fprintf_unfiltered (tmp_error_stream,
2879 "Cannot insert breakpoint %d.\n"
2880 "%s\n",
2881 bl->owner->number, message);
2882 do_cleanups (old_chain);
2883 }
2884 else
2885 {
2886 fprintf_unfiltered (tmp_error_stream,
2887 "Cannot insert breakpoint %d: %s\n",
2888 bl->owner->number,
2889 bp_err_message);
2890 }
879bfdc2 2891 }
0000e5cc 2892 return 1;
879bfdc2
DJ
2893
2894 }
2895 }
2896 else
35df4500 2897 bl->inserted = 1;
879bfdc2 2898
0000e5cc 2899 return 0;
879bfdc2
DJ
2900 }
2901
35df4500 2902 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2903 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2904 watchpoints. It's not clear that it's necessary... */
35df4500 2905 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2906 {
0000e5cc
PA
2907 int val;
2908
77b06cd7
TJB
2909 gdb_assert (bl->owner->ops != NULL
2910 && bl->owner->ops->insert_location != NULL);
2911
2912 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2913
2914 /* If trying to set a read-watchpoint, and it turns out it's not
2915 supported, try emulating one with an access watchpoint. */
35df4500 2916 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2917 {
2918 struct bp_location *loc, **loc_temp;
2919
2920 /* But don't try to insert it, if there's already another
2921 hw_access location that would be considered a duplicate
2922 of this one. */
2923 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2924 if (loc != bl
85d721b8 2925 && loc->watchpoint_type == hw_access
35df4500 2926 && watchpoint_locations_match (bl, loc))
85d721b8 2927 {
35df4500
TJB
2928 bl->duplicate = 1;
2929 bl->inserted = 1;
2930 bl->target_info = loc->target_info;
2931 bl->watchpoint_type = hw_access;
85d721b8
PA
2932 val = 0;
2933 break;
2934 }
2935
2936 if (val == 1)
2937 {
77b06cd7
TJB
2938 bl->watchpoint_type = hw_access;
2939 val = bl->owner->ops->insert_location (bl);
2940
2941 if (val)
2942 /* Back to the original value. */
2943 bl->watchpoint_type = hw_read;
85d721b8
PA
2944 }
2945 }
2946
35df4500 2947 bl->inserted = (val == 0);
879bfdc2
DJ
2948 }
2949
35df4500 2950 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2951 {
0000e5cc
PA
2952 int val;
2953
77b06cd7
TJB
2954 gdb_assert (bl->owner->ops != NULL
2955 && bl->owner->ops->insert_location != NULL);
2956
2957 val = bl->owner->ops->insert_location (bl);
2958 if (val)
2959 {
2960 bl->owner->enable_state = bp_disabled;
2961
2962 if (val == 1)
2963 warning (_("\
2964Error inserting catchpoint %d: Your system does not support this type\n\
2965of catchpoint."), bl->owner->number);
2966 else
2967 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2968 }
2969
2970 bl->inserted = (val == 0);
1640b821
DJ
2971
2972 /* We've already printed an error message if there was a problem
2973 inserting this catchpoint, and we've disabled the catchpoint,
2974 so just return success. */
2975 return 0;
879bfdc2
DJ
2976 }
2977
2978 return 0;
2979}
2980
6c95b8df
PA
2981/* This function is called when program space PSPACE is about to be
2982 deleted. It takes care of updating breakpoints to not reference
2983 PSPACE anymore. */
2984
2985void
2986breakpoint_program_space_exit (struct program_space *pspace)
2987{
2988 struct breakpoint *b, *b_temp;
876fa593 2989 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2990
2991 /* Remove any breakpoint that was set through this program space. */
2992 ALL_BREAKPOINTS_SAFE (b, b_temp)
2993 {
2994 if (b->pspace == pspace)
2995 delete_breakpoint (b);
2996 }
2997
2998 /* Breakpoints set through other program spaces could have locations
2999 bound to PSPACE as well. Remove those. */
876fa593 3000 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
3001 {
3002 struct bp_location *tmp;
3003
3004 if (loc->pspace == pspace)
3005 {
2bdf28a0 3006 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3007 if (loc->owner->loc == loc)
3008 loc->owner->loc = loc->next;
3009 else
3010 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3011 if (tmp->next == loc)
3012 {
3013 tmp->next = loc->next;
3014 break;
3015 }
3016 }
3017 }
3018
3019 /* Now update the global location list to permanently delete the
3020 removed locations above. */
44702360 3021 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3022}
3023
74960c60
VP
3024/* Make sure all breakpoints are inserted in inferior.
3025 Throws exception on any error.
3026 A breakpoint that is already inserted won't be inserted
3027 again, so calling this function twice is safe. */
3028void
3029insert_breakpoints (void)
3030{
3031 struct breakpoint *bpt;
3032
3033 ALL_BREAKPOINTS (bpt)
3034 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3035 {
3036 struct watchpoint *w = (struct watchpoint *) bpt;
3037
3038 update_watchpoint (w, 0 /* don't reparse. */);
3039 }
74960c60 3040
04086b45
PA
3041 /* Updating watchpoints creates new locations, so update the global
3042 location list. Explicitly tell ugll to insert locations and
3043 ignore breakpoints_always_inserted_mode. */
3044 update_global_location_list (UGLL_INSERT);
74960c60
VP
3045}
3046
20388dd6
YQ
3047/* Invoke CALLBACK for each of bp_location. */
3048
3049void
3050iterate_over_bp_locations (walk_bp_location_callback callback)
3051{
3052 struct bp_location *loc, **loc_tmp;
3053
3054 ALL_BP_LOCATIONS (loc, loc_tmp)
3055 {
3056 callback (loc, NULL);
3057 }
3058}
3059
b775012e
LM
3060/* This is used when we need to synch breakpoint conditions between GDB and the
3061 target. It is the case with deleting and disabling of breakpoints when using
3062 always-inserted mode. */
3063
3064static void
3065update_inserted_breakpoint_locations (void)
3066{
3067 struct bp_location *bl, **blp_tmp;
3068 int error_flag = 0;
3069 int val = 0;
3070 int disabled_breaks = 0;
3071 int hw_breakpoint_error = 0;
dd61ec5c 3072 int hw_bp_details_reported = 0;
b775012e
LM
3073
3074 struct ui_file *tmp_error_stream = mem_fileopen ();
3075 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3076
3077 /* Explicitly mark the warning -- this will only be printed if
3078 there was an error. */
3079 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3080
3081 save_current_space_and_thread ();
3082
3083 ALL_BP_LOCATIONS (bl, blp_tmp)
3084 {
3085 /* We only want to update software breakpoints and hardware
3086 breakpoints. */
3087 if (!is_breakpoint (bl->owner))
3088 continue;
3089
3090 /* We only want to update locations that are already inserted
3091 and need updating. This is to avoid unwanted insertion during
3092 deletion of breakpoints. */
3093 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3094 continue;
3095
3096 switch_to_program_space_and_thread (bl->pspace);
3097
3098 /* For targets that support global breakpoints, there's no need
3099 to select an inferior to insert breakpoint to. In fact, even
3100 if we aren't attached to any process yet, we should still
3101 insert breakpoints. */
f5656ead 3102 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3103 && ptid_equal (inferior_ptid, null_ptid))
3104 continue;
3105
3106 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3107 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3108 if (val)
3109 error_flag = val;
3110 }
3111
3112 if (error_flag)
3113 {
3114 target_terminal_ours_for_output ();
3115 error_stream (tmp_error_stream);
3116 }
3117
3118 do_cleanups (cleanups);
3119}
3120
c30eee59 3121/* Used when starting or continuing the program. */
c906108c 3122
74960c60
VP
3123static void
3124insert_breakpoint_locations (void)
c906108c 3125{
a5606eee 3126 struct breakpoint *bpt;
35df4500 3127 struct bp_location *bl, **blp_tmp;
eacd795a 3128 int error_flag = 0;
c906108c 3129 int val = 0;
3fbb6ffa 3130 int disabled_breaks = 0;
81d0cc19 3131 int hw_breakpoint_error = 0;
dd61ec5c 3132 int hw_bp_error_explained_already = 0;
c906108c 3133
81d0cc19 3134 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3135 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3136
81d0cc19
GS
3137 /* Explicitly mark the warning -- this will only be printed if
3138 there was an error. */
3139 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3140
3141 save_current_space_and_thread ();
3142
35df4500 3143 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3144 {
b775012e 3145 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3146 continue;
3147
4a64f543
MS
3148 /* There is no point inserting thread-specific breakpoints if
3149 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3150 has BL->OWNER always non-NULL. */
35df4500 3151 if (bl->owner->thread != -1
5d5658a1 3152 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3153 continue;
3154
35df4500 3155 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3156
3157 /* For targets that support global breakpoints, there's no need
3158 to select an inferior to insert breakpoint to. In fact, even
3159 if we aren't attached to any process yet, we should still
3160 insert breakpoints. */
f5656ead 3161 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3162 && ptid_equal (inferior_ptid, null_ptid))
3163 continue;
3164
3fbb6ffa 3165 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3166 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3167 if (val)
eacd795a 3168 error_flag = val;
879bfdc2 3169 }
c906108c 3170
4a64f543
MS
3171 /* If we failed to insert all locations of a watchpoint, remove
3172 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3173 ALL_BREAKPOINTS (bpt)
3174 {
3175 int some_failed = 0;
3176 struct bp_location *loc;
3177
3178 if (!is_hardware_watchpoint (bpt))
3179 continue;
3180
d6b74ac4 3181 if (!breakpoint_enabled (bpt))
a5606eee 3182 continue;
74960c60
VP
3183
3184 if (bpt->disposition == disp_del_at_next_stop)
3185 continue;
a5606eee
VP
3186
3187 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3188 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3189 {
3190 some_failed = 1;
3191 break;
3192 }
3193 if (some_failed)
3194 {
3195 for (loc = bpt->loc; loc; loc = loc->next)
3196 if (loc->inserted)
834c0d03 3197 remove_breakpoint (loc);
a5606eee
VP
3198
3199 hw_breakpoint_error = 1;
3200 fprintf_unfiltered (tmp_error_stream,
3201 "Could not insert hardware watchpoint %d.\n",
3202 bpt->number);
eacd795a 3203 error_flag = -1;
a5606eee
VP
3204 }
3205 }
3206
eacd795a 3207 if (error_flag)
81d0cc19
GS
3208 {
3209 /* If a hardware breakpoint or watchpoint was inserted, add a
3210 message about possibly exhausted resources. */
dd61ec5c 3211 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3212 {
c6510018
MS
3213 fprintf_unfiltered (tmp_error_stream,
3214 "Could not insert hardware breakpoints:\n\
3215You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3216 }
81d0cc19
GS
3217 target_terminal_ours_for_output ();
3218 error_stream (tmp_error_stream);
3219 }
f7545552
TT
3220
3221 do_cleanups (cleanups);
c906108c
SS
3222}
3223
c30eee59
TJB
3224/* Used when the program stops.
3225 Returns zero if successful, or non-zero if there was a problem
3226 removing a breakpoint location. */
3227
c906108c 3228int
fba45db2 3229remove_breakpoints (void)
c906108c 3230{
35df4500 3231 struct bp_location *bl, **blp_tmp;
3a1bae8e 3232 int val = 0;
c906108c 3233
35df4500 3234 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3235 {
1e4d1764 3236 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3237 val |= remove_breakpoint (bl);
c5aa993b 3238 }
3a1bae8e 3239 return val;
c906108c
SS
3240}
3241
49fa26b0
PA
3242/* When a thread exits, remove breakpoints that are related to
3243 that thread. */
3244
3245static void
3246remove_threaded_breakpoints (struct thread_info *tp, int silent)
3247{
3248 struct breakpoint *b, *b_tmp;
3249
3250 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3251 {
5d5658a1 3252 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3253 {
3254 b->disposition = disp_del_at_next_stop;
3255
3256 printf_filtered (_("\
43792cf0
PA
3257Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3258 b->number, print_thread_id (tp));
49fa26b0
PA
3259
3260 /* Hide it from the user. */
3261 b->number = 0;
3262 }
3263 }
3264}
3265
6c95b8df
PA
3266/* Remove breakpoints of process PID. */
3267
3268int
3269remove_breakpoints_pid (int pid)
3270{
35df4500 3271 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3272 int val;
3273 struct inferior *inf = find_inferior_pid (pid);
3274
35df4500 3275 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3276 {
35df4500 3277 if (bl->pspace != inf->pspace)
6c95b8df
PA
3278 continue;
3279
fc126975 3280 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3281 {
834c0d03 3282 val = remove_breakpoint (bl);
6c95b8df
PA
3283 if (val != 0)
3284 return val;
3285 }
3286 }
3287 return 0;
3288}
3289
c906108c 3290int
fba45db2 3291reattach_breakpoints (int pid)
c906108c 3292{
6c95b8df 3293 struct cleanup *old_chain;
35df4500 3294 struct bp_location *bl, **blp_tmp;
c906108c 3295 int val;
86b887df 3296 struct ui_file *tmp_error_stream;
dd61ec5c 3297 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3298 struct inferior *inf;
3299 struct thread_info *tp;
3300
3301 tp = any_live_thread_of_process (pid);
3302 if (tp == NULL)
3303 return 1;
3304
3305 inf = find_inferior_pid (pid);
3306 old_chain = save_inferior_ptid ();
3307
3308 inferior_ptid = tp->ptid;
a4954f26 3309
86b887df 3310 tmp_error_stream = mem_fileopen ();
a4954f26 3311 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3312
35df4500 3313 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3314 {
35df4500 3315 if (bl->pspace != inf->pspace)
6c95b8df
PA
3316 continue;
3317
35df4500 3318 if (bl->inserted)
c5aa993b 3319 {
35df4500 3320 bl->inserted = 0;
dd61ec5c 3321 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3322 if (val != 0)
3323 {
ce696e05 3324 do_cleanups (old_chain);
c5aa993b
JM
3325 return val;
3326 }
3327 }
3328 }
ce696e05 3329 do_cleanups (old_chain);
c906108c
SS
3330 return 0;
3331}
3332
e58b0e63
PA
3333static int internal_breakpoint_number = -1;
3334
84f4c1fe
PM
3335/* Set the breakpoint number of B, depending on the value of INTERNAL.
3336 If INTERNAL is non-zero, the breakpoint number will be populated
3337 from internal_breakpoint_number and that variable decremented.
e5dd4106 3338 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3339 breakpoint_count and that value incremented. Internal breakpoints
3340 do not set the internal var bpnum. */
3341static void
3342set_breakpoint_number (int internal, struct breakpoint *b)
3343{
3344 if (internal)
3345 b->number = internal_breakpoint_number--;
3346 else
3347 {
3348 set_breakpoint_count (breakpoint_count + 1);
3349 b->number = breakpoint_count;
3350 }
3351}
3352
e62c965a 3353static struct breakpoint *
a6d9a66e 3354create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3355 CORE_ADDR address, enum bptype type,
c0a91b2b 3356 const struct breakpoint_ops *ops)
e62c965a 3357{
e62c965a
PP
3358 struct symtab_and_line sal;
3359 struct breakpoint *b;
3360
4a64f543 3361 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3362
3363 sal.pc = address;
3364 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3365 sal.pspace = current_program_space;
e62c965a 3366
06edf0c0 3367 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3368 b->number = internal_breakpoint_number--;
3369 b->disposition = disp_donttouch;
3370
3371 return b;
3372}
3373
17450429
PP
3374static const char *const longjmp_names[] =
3375 {
3376 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3377 };
3378#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3379
3380/* Per-objfile data private to breakpoint.c. */
3381struct breakpoint_objfile_data
3382{
3383 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3384 struct bound_minimal_symbol overlay_msym;
17450429
PP
3385
3386 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3387 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3388
28106bc2
SDJ
3389 /* True if we have looked for longjmp probes. */
3390 int longjmp_searched;
3391
3392 /* SystemTap probe points for longjmp (if any). */
3393 VEC (probe_p) *longjmp_probes;
3394
17450429 3395 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3396 struct bound_minimal_symbol terminate_msym;
17450429
PP
3397
3398 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3399 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3400
3401 /* True if we have looked for exception probes. */
3402 int exception_searched;
3403
3404 /* SystemTap probe points for unwinding (if any). */
3405 VEC (probe_p) *exception_probes;
17450429
PP
3406};
3407
3408static const struct objfile_data *breakpoint_objfile_key;
3409
3410/* Minimal symbol not found sentinel. */
3411static struct minimal_symbol msym_not_found;
3412
3413/* Returns TRUE if MSYM point to the "not found" sentinel. */
3414
3415static int
3416msym_not_found_p (const struct minimal_symbol *msym)
3417{
3418 return msym == &msym_not_found;
3419}
3420
3421/* Return per-objfile data needed by breakpoint.c.
3422 Allocate the data if necessary. */
3423
3424static struct breakpoint_objfile_data *
3425get_breakpoint_objfile_data (struct objfile *objfile)
3426{
3427 struct breakpoint_objfile_data *bp_objfile_data;
3428
9a3c8263
SM
3429 bp_objfile_data = ((struct breakpoint_objfile_data *)
3430 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3431 if (bp_objfile_data == NULL)
3432 {
8d749320
SM
3433 bp_objfile_data =
3434 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3435
3436 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3437 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3438 }
3439 return bp_objfile_data;
3440}
3441
28106bc2
SDJ
3442static void
3443free_breakpoint_probes (struct objfile *obj, void *data)
3444{
9a3c8263
SM
3445 struct breakpoint_objfile_data *bp_objfile_data
3446 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3447
3448 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3449 VEC_free (probe_p, bp_objfile_data->exception_probes);
3450}
3451
e62c965a 3452static void
af02033e 3453create_overlay_event_breakpoint (void)
e62c965a 3454{
69de3c6a 3455 struct objfile *objfile;
af02033e 3456 const char *const func_name = "_ovly_debug_event";
e62c965a 3457
69de3c6a
PP
3458 ALL_OBJFILES (objfile)
3459 {
3460 struct breakpoint *b;
17450429
PP
3461 struct breakpoint_objfile_data *bp_objfile_data;
3462 CORE_ADDR addr;
67994074 3463 struct explicit_location explicit_loc;
69de3c6a 3464
17450429
PP
3465 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3466
3b7344d5 3467 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3468 continue;
3469
3b7344d5 3470 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3471 {
3b7344d5 3472 struct bound_minimal_symbol m;
17450429
PP
3473
3474 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3475 if (m.minsym == NULL)
17450429
PP
3476 {
3477 /* Avoid future lookups in this objfile. */
3b7344d5 3478 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3479 continue;
3480 }
3481 bp_objfile_data->overlay_msym = m;
3482 }
e62c965a 3483
77e371c0 3484 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3485 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3486 bp_overlay_event,
3487 &internal_breakpoint_ops);
67994074
KS
3488 initialize_explicit_location (&explicit_loc);
3489 explicit_loc.function_name = ASTRDUP (func_name);
3490 b->location = new_explicit_location (&explicit_loc);
e62c965a 3491
69de3c6a
PP
3492 if (overlay_debugging == ovly_auto)
3493 {
3494 b->enable_state = bp_enabled;
3495 overlay_events_enabled = 1;
3496 }
3497 else
3498 {
3499 b->enable_state = bp_disabled;
3500 overlay_events_enabled = 0;
3501 }
e62c965a 3502 }
e62c965a
PP
3503}
3504
0fd8e87f 3505static void
af02033e 3506create_longjmp_master_breakpoint (void)
0fd8e87f 3507{
6c95b8df 3508 struct program_space *pspace;
6c95b8df
PA
3509 struct cleanup *old_chain;
3510
3511 old_chain = save_current_program_space ();
0fd8e87f 3512
6c95b8df 3513 ALL_PSPACES (pspace)
af02033e
PP
3514 {
3515 struct objfile *objfile;
3516
3517 set_current_program_space (pspace);
3518
3519 ALL_OBJFILES (objfile)
0fd8e87f 3520 {
af02033e
PP
3521 int i;
3522 struct gdbarch *gdbarch;
17450429 3523 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3524
af02033e 3525 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3526
17450429
PP
3527 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3528
28106bc2
SDJ
3529 if (!bp_objfile_data->longjmp_searched)
3530 {
25f9533e
SDJ
3531 VEC (probe_p) *ret;
3532
3533 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3534 if (ret != NULL)
3535 {
3536 /* We are only interested in checking one element. */
3537 struct probe *p = VEC_index (probe_p, ret, 0);
3538
3539 if (!can_evaluate_probe_arguments (p))
3540 {
3541 /* We cannot use the probe interface here, because it does
3542 not know how to evaluate arguments. */
3543 VEC_free (probe_p, ret);
3544 ret = NULL;
3545 }
3546 }
3547 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3548 bp_objfile_data->longjmp_searched = 1;
3549 }
3550
3551 if (bp_objfile_data->longjmp_probes != NULL)
3552 {
3553 int i;
3554 struct probe *probe;
3555 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3556
3557 for (i = 0;
3558 VEC_iterate (probe_p,
3559 bp_objfile_data->longjmp_probes,
3560 i, probe);
3561 ++i)
3562 {
3563 struct breakpoint *b;
3564
729662a5
TT
3565 b = create_internal_breakpoint (gdbarch,
3566 get_probe_address (probe,
3567 objfile),
28106bc2
SDJ
3568 bp_longjmp_master,
3569 &internal_breakpoint_ops);
5b56227b
KS
3570 b->location
3571 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3572 b->enable_state = bp_disabled;
3573 }
3574
3575 continue;
3576 }
3577
0569175e
TSD
3578 if (!gdbarch_get_longjmp_target_p (gdbarch))
3579 continue;
3580
17450429 3581 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3582 {
3583 struct breakpoint *b;
af02033e 3584 const char *func_name;
17450429 3585 CORE_ADDR addr;
67994074 3586 struct explicit_location explicit_loc;
6c95b8df 3587
3b7344d5 3588 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3589 continue;
0fd8e87f 3590
17450429 3591 func_name = longjmp_names[i];
3b7344d5 3592 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3593 {
3b7344d5 3594 struct bound_minimal_symbol m;
17450429
PP
3595
3596 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3597 if (m.minsym == NULL)
17450429
PP
3598 {
3599 /* Prevent future lookups in this objfile. */
3b7344d5 3600 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3601 continue;
3602 }
3603 bp_objfile_data->longjmp_msym[i] = m;
3604 }
3605
77e371c0 3606 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3607 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3608 &internal_breakpoint_ops);
67994074
KS
3609 initialize_explicit_location (&explicit_loc);
3610 explicit_loc.function_name = ASTRDUP (func_name);
3611 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3612 b->enable_state = bp_disabled;
3613 }
0fd8e87f 3614 }
af02033e 3615 }
6c95b8df
PA
3616
3617 do_cleanups (old_chain);
0fd8e87f
UW
3618}
3619
af02033e 3620/* Create a master std::terminate breakpoint. */
aa7d318d 3621static void
af02033e 3622create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3623{
3624 struct program_space *pspace;
aa7d318d 3625 struct cleanup *old_chain;
af02033e 3626 const char *const func_name = "std::terminate()";
aa7d318d
TT
3627
3628 old_chain = save_current_program_space ();
3629
3630 ALL_PSPACES (pspace)
17450429
PP
3631 {
3632 struct objfile *objfile;
3633 CORE_ADDR addr;
3634
3635 set_current_program_space (pspace);
3636
aa7d318d
TT
3637 ALL_OBJFILES (objfile)
3638 {
3639 struct breakpoint *b;
17450429 3640 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3641 struct explicit_location explicit_loc;
aa7d318d 3642
17450429 3643 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3644
3b7344d5 3645 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3646 continue;
3647
3b7344d5 3648 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3649 {
3b7344d5 3650 struct bound_minimal_symbol m;
17450429
PP
3651
3652 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3653 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3654 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3655 {
3656 /* Prevent future lookups in this objfile. */
3b7344d5 3657 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3658 continue;
3659 }
3660 bp_objfile_data->terminate_msym = m;
3661 }
aa7d318d 3662
77e371c0 3663 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3664 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3665 bp_std_terminate_master,
3666 &internal_breakpoint_ops);
67994074
KS
3667 initialize_explicit_location (&explicit_loc);
3668 explicit_loc.function_name = ASTRDUP (func_name);
3669 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3670 b->enable_state = bp_disabled;
3671 }
17450429
PP
3672 }
3673
aa7d318d
TT
3674 do_cleanups (old_chain);
3675}
3676
186c406b
TT
3677/* Install a master breakpoint on the unwinder's debug hook. */
3678
70221824 3679static void
186c406b
TT
3680create_exception_master_breakpoint (void)
3681{
3682 struct objfile *objfile;
17450429 3683 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3684
3685 ALL_OBJFILES (objfile)
3686 {
17450429
PP
3687 struct breakpoint *b;
3688 struct gdbarch *gdbarch;
3689 struct breakpoint_objfile_data *bp_objfile_data;
3690 CORE_ADDR addr;
67994074 3691 struct explicit_location explicit_loc;
17450429
PP
3692
3693 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3694
28106bc2
SDJ
3695 /* We prefer the SystemTap probe point if it exists. */
3696 if (!bp_objfile_data->exception_searched)
3697 {
25f9533e
SDJ
3698 VEC (probe_p) *ret;
3699
3700 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3701
3702 if (ret != NULL)
3703 {
3704 /* We are only interested in checking one element. */
3705 struct probe *p = VEC_index (probe_p, ret, 0);
3706
3707 if (!can_evaluate_probe_arguments (p))
3708 {
3709 /* We cannot use the probe interface here, because it does
3710 not know how to evaluate arguments. */
3711 VEC_free (probe_p, ret);
3712 ret = NULL;
3713 }
3714 }
3715 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3716 bp_objfile_data->exception_searched = 1;
3717 }
3718
3719 if (bp_objfile_data->exception_probes != NULL)
3720 {
3721 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3722 int i;
3723 struct probe *probe;
3724
3725 for (i = 0;
3726 VEC_iterate (probe_p,
3727 bp_objfile_data->exception_probes,
3728 i, probe);
3729 ++i)
3730 {
3731 struct breakpoint *b;
3732
729662a5
TT
3733 b = create_internal_breakpoint (gdbarch,
3734 get_probe_address (probe,
3735 objfile),
28106bc2
SDJ
3736 bp_exception_master,
3737 &internal_breakpoint_ops);
5b56227b
KS
3738 b->location
3739 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3740 b->enable_state = bp_disabled;
3741 }
3742
3743 continue;
3744 }
3745
3746 /* Otherwise, try the hook function. */
3747
3b7344d5 3748 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3749 continue;
3750
3751 gdbarch = get_objfile_arch (objfile);
186c406b 3752
3b7344d5 3753 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3754 {
3b7344d5 3755 struct bound_minimal_symbol debug_hook;
186c406b 3756
17450429 3757 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3758 if (debug_hook.minsym == NULL)
17450429 3759 {
3b7344d5 3760 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3761 continue;
3762 }
3763
3764 bp_objfile_data->exception_msym = debug_hook;
186c406b 3765 }
17450429 3766
77e371c0 3767 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3768 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3769 &current_target);
06edf0c0
PA
3770 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3771 &internal_breakpoint_ops);
67994074
KS
3772 initialize_explicit_location (&explicit_loc);
3773 explicit_loc.function_name = ASTRDUP (func_name);
3774 b->location = new_explicit_location (&explicit_loc);
17450429 3775 b->enable_state = bp_disabled;
186c406b 3776 }
186c406b
TT
3777}
3778
9ef9e6a6
KS
3779/* Does B have a location spec? */
3780
3781static int
3782breakpoint_event_location_empty_p (const struct breakpoint *b)
3783{
3784 return b->location != NULL && event_location_empty_p (b->location);
3785}
3786
c906108c 3787void
fba45db2 3788update_breakpoints_after_exec (void)
c906108c 3789{
35df4500 3790 struct breakpoint *b, *b_tmp;
876fa593 3791 struct bp_location *bploc, **bplocp_tmp;
c906108c 3792
25b22b0a
PA
3793 /* We're about to delete breakpoints from GDB's lists. If the
3794 INSERTED flag is true, GDB will try to lift the breakpoints by
3795 writing the breakpoints' "shadow contents" back into memory. The
3796 "shadow contents" are NOT valid after an exec, so GDB should not
3797 do that. Instead, the target is responsible from marking
3798 breakpoints out as soon as it detects an exec. We don't do that
3799 here instead, because there may be other attempts to delete
3800 breakpoints after detecting an exec and before reaching here. */
876fa593 3801 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3802 if (bploc->pspace == current_program_space)
3803 gdb_assert (!bploc->inserted);
c906108c 3804
35df4500 3805 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3806 {
6c95b8df
PA
3807 if (b->pspace != current_program_space)
3808 continue;
3809
4a64f543 3810 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3811 if (b->type == bp_shlib_event)
3812 {
3813 delete_breakpoint (b);
3814 continue;
3815 }
c906108c 3816
4a64f543 3817 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3818 if (b->type == bp_jit_event)
3819 {
3820 delete_breakpoint (b);
3821 continue;
3822 }
3823
1900040c 3824 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3825 as must overlay event and longjmp master breakpoints. */
3826 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3827 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3828 || b->type == bp_exception_master)
c4093a6a
JM
3829 {
3830 delete_breakpoint (b);
3831 continue;
3832 }
3833
4a64f543 3834 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3835 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3836 {
3837 delete_breakpoint (b);
3838 continue;
3839 }
3840
7c16b83e
PA
3841 /* Just like single-step breakpoints. */
3842 if (b->type == bp_single_step)
3843 {
3844 delete_breakpoint (b);
3845 continue;
3846 }
3847
611c83ae
PA
3848 /* Longjmp and longjmp-resume breakpoints are also meaningless
3849 after an exec. */
186c406b 3850 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3851 || b->type == bp_longjmp_call_dummy
186c406b 3852 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3853 {
3854 delete_breakpoint (b);
3855 continue;
3856 }
3857
ce78b96d
JB
3858 if (b->type == bp_catchpoint)
3859 {
3860 /* For now, none of the bp_catchpoint breakpoints need to
3861 do anything at this point. In the future, if some of
3862 the catchpoints need to something, we will need to add
3863 a new method, and call this method from here. */
3864 continue;
3865 }
3866
c5aa993b
JM
3867 /* bp_finish is a special case. The only way we ought to be able
3868 to see one of these when an exec() has happened, is if the user
3869 caught a vfork, and then said "finish". Ordinarily a finish just
3870 carries them to the call-site of the current callee, by setting
3871 a temporary bp there and resuming. But in this case, the finish
3872 will carry them entirely through the vfork & exec.
3873
3874 We don't want to allow a bp_finish to remain inserted now. But
3875 we can't safely delete it, 'cause finish_command has a handle to
3876 the bp on a bpstat, and will later want to delete it. There's a
3877 chance (and I've seen it happen) that if we delete the bp_finish
3878 here, that its storage will get reused by the time finish_command
3879 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3880 We really must allow finish_command to delete a bp_finish.
3881
e5dd4106 3882 In the absence of a general solution for the "how do we know
53a5351d
JM
3883 it's safe to delete something others may have handles to?"
3884 problem, what we'll do here is just uninsert the bp_finish, and
3885 let finish_command delete it.
3886
3887 (We know the bp_finish is "doomed" in the sense that it's
3888 momentary, and will be deleted as soon as finish_command sees
3889 the inferior stopped. So it doesn't matter that the bp's
3890 address is probably bogus in the new a.out, unlike e.g., the
3891 solib breakpoints.) */
c5aa993b 3892
c5aa993b
JM
3893 if (b->type == bp_finish)
3894 {
3895 continue;
3896 }
3897
3898 /* Without a symbolic address, we have little hope of the
3899 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3900 a.out. */
9ef9e6a6 3901 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3902 {
3903 delete_breakpoint (b);
3904 continue;
3905 }
c5aa993b 3906 }
c906108c
SS
3907}
3908
3909int
d80ee84f 3910detach_breakpoints (ptid_t ptid)
c906108c 3911{
35df4500 3912 struct bp_location *bl, **blp_tmp;
3a1bae8e 3913 int val = 0;
ce696e05 3914 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3915 struct inferior *inf = current_inferior ();
c5aa993b 3916
dfd4cc63 3917 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3918 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3919
6c95b8df 3920 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3921 inferior_ptid = ptid;
35df4500 3922 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3923 {
35df4500 3924 if (bl->pspace != inf->pspace)
6c95b8df
PA
3925 continue;
3926
bd9673a4
PW
3927 /* This function must physically remove breakpoints locations
3928 from the specified ptid, without modifying the breakpoint
3929 package's state. Locations of type bp_loc_other are only
3930 maintained at GDB side. So, there is no need to remove
3931 these bp_loc_other locations. Moreover, removing these
3932 would modify the breakpoint package's state. */
3933 if (bl->loc_type == bp_loc_other)
3934 continue;
3935
35df4500 3936 if (bl->inserted)
b2b6a7da 3937 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3938 }
d03285ec 3939
ce696e05 3940 do_cleanups (old_chain);
3a1bae8e 3941 return val;
c906108c
SS
3942}
3943
35df4500 3944/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3945 Note that this is used to detach breakpoints from a child fork.
3946 When we get here, the child isn't in the inferior list, and neither
3947 do we have objects to represent its address space --- we should
35df4500 3948 *not* look at bl->pspace->aspace here. */
6c95b8df 3949
c906108c 3950static int
b2b6a7da 3951remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3952{
3953 int val;
c5aa993b 3954
35df4500
TJB
3955 /* BL is never in moribund_locations by our callers. */
3956 gdb_assert (bl->owner != NULL);
2bdf28a0 3957
74960c60
VP
3958 /* The type of none suggests that owner is actually deleted.
3959 This should not ever happen. */
35df4500 3960 gdb_assert (bl->owner->type != bp_none);
0bde7532 3961
35df4500
TJB
3962 if (bl->loc_type == bp_loc_software_breakpoint
3963 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3964 {
c02f5703
MS
3965 /* "Normal" instruction breakpoint: either the standard
3966 trap-instruction bp (bp_breakpoint), or a
3967 bp_hardware_breakpoint. */
3968
3969 /* First check to see if we have to handle an overlay. */
3970 if (overlay_debugging == ovly_off
35df4500
TJB
3971 || bl->section == NULL
3972 || !(section_is_overlay (bl->section)))
c02f5703
MS
3973 {
3974 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3975
3976 /* If we're trying to uninsert a memory breakpoint that we
3977 know is set in a dynamic object that is marked
3978 shlib_disabled, then either the dynamic object was
3979 removed with "remove-symbol-file" or with
3980 "nosharedlibrary". In the former case, we don't know
3981 whether another dynamic object might have loaded over the
3982 breakpoint's address -- the user might well let us know
3983 about it next with add-symbol-file (the whole point of
d03de421 3984 add-symbol-file is letting the user manually maintain a
08351840
PA
3985 list of dynamically loaded objects). If we have the
3986 breakpoint's shadow memory, that is, this is a software
3987 breakpoint managed by GDB, check whether the breakpoint
3988 is still inserted in memory, to avoid overwriting wrong
3989 code with stale saved shadow contents. Note that HW
3990 breakpoints don't have shadow memory, as they're
3991 implemented using a mechanism that is not dependent on
3992 being able to modify the target's memory, and as such
3993 they should always be removed. */
3994 if (bl->shlib_disabled
3995 && bl->target_info.shadow_len != 0
3996 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3997 val = 0;
3998 else
73971819 3999 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 4000 }
c906108c
SS
4001 else
4002 {
4a64f543 4003 /* This breakpoint is in an overlay section.
c02f5703
MS
4004 Did we set a breakpoint at the LMA? */
4005 if (!overlay_events_enabled)
4006 {
4007 /* Yes -- overlay event support is not active, so we
4008 should have set a breakpoint at the LMA. Remove it.
4009 */
c02f5703
MS
4010 /* Ignore any failures: if the LMA is in ROM, we will
4011 have already warned when we failed to insert it. */
35df4500
TJB
4012 if (bl->loc_type == bp_loc_hardware_breakpoint)
4013 target_remove_hw_breakpoint (bl->gdbarch,
4014 &bl->overlay_target_info);
c02f5703 4015 else
35df4500 4016 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4017 &bl->overlay_target_info,
4018 reason);
c02f5703
MS
4019 }
4020 /* Did we set a breakpoint at the VMA?
4021 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4022 if (bl->inserted)
c906108c 4023 {
c02f5703
MS
4024 /* Yes -- remove it. Previously we did not bother to
4025 remove the breakpoint if the section had been
4026 unmapped, but let's not rely on that being safe. We
4027 don't know what the overlay manager might do. */
aa67235e
UW
4028
4029 /* However, we should remove *software* breakpoints only
4030 if the section is still mapped, or else we overwrite
4031 wrong code with the saved shadow contents. */
348d480f
PA
4032 if (bl->loc_type == bp_loc_hardware_breakpoint
4033 || section_is_mapped (bl->section))
73971819 4034 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4035 else
4036 val = 0;
c906108c 4037 }
c02f5703
MS
4038 else
4039 {
4040 /* No -- not inserted, so no need to remove. No error. */
4041 val = 0;
4042 }
c906108c 4043 }
879d1e6b 4044
08351840
PA
4045 /* In some cases, we might not be able to remove a breakpoint in
4046 a shared library that has already been removed, but we have
4047 not yet processed the shlib unload event. Similarly for an
4048 unloaded add-symbol-file object - the user might not yet have
4049 had the chance to remove-symbol-file it. shlib_disabled will
4050 be set if the library/object has already been removed, but
4051 the breakpoint hasn't been uninserted yet, e.g., after
4052 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4053 always-inserted mode. */
076855f9 4054 if (val
08351840
PA
4055 && (bl->loc_type == bp_loc_software_breakpoint
4056 && (bl->shlib_disabled
4057 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4058 || shared_objfile_contains_address_p (bl->pspace,
4059 bl->address))))
879d1e6b
UW
4060 val = 0;
4061
c906108c
SS
4062 if (val)
4063 return val;
b2b6a7da 4064 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4065 }
35df4500 4066 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4067 {
77b06cd7
TJB
4068 gdb_assert (bl->owner->ops != NULL
4069 && bl->owner->ops->remove_location != NULL);
4070
b2b6a7da 4071 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4072 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4073
c906108c 4074 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4075 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4076 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4077 bl->owner->number);
c906108c 4078 }
35df4500
TJB
4079 else if (bl->owner->type == bp_catchpoint
4080 && breakpoint_enabled (bl->owner)
4081 && !bl->duplicate)
ce78b96d 4082 {
77b06cd7
TJB
4083 gdb_assert (bl->owner->ops != NULL
4084 && bl->owner->ops->remove_location != NULL);
ce78b96d 4085
73971819 4086 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4087 if (val)
4088 return val;
77b06cd7 4089
b2b6a7da 4090 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4091 }
c906108c
SS
4092
4093 return 0;
4094}
4095
6c95b8df 4096static int
834c0d03 4097remove_breakpoint (struct bp_location *bl)
6c95b8df
PA
4098{
4099 int ret;
4100 struct cleanup *old_chain;
4101
35df4500
TJB
4102 /* BL is never in moribund_locations by our callers. */
4103 gdb_assert (bl->owner != NULL);
2bdf28a0 4104
6c95b8df
PA
4105 /* The type of none suggests that owner is actually deleted.
4106 This should not ever happen. */
35df4500 4107 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4108
4109 old_chain = save_current_space_and_thread ();
4110
35df4500 4111 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4112
b2b6a7da 4113 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4114
4115 do_cleanups (old_chain);
4116 return ret;
4117}
4118
c906108c
SS
4119/* Clear the "inserted" flag in all breakpoints. */
4120
25b22b0a 4121void
fba45db2 4122mark_breakpoints_out (void)
c906108c 4123{
35df4500 4124 struct bp_location *bl, **blp_tmp;
c906108c 4125
35df4500 4126 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4127 if (bl->pspace == current_program_space)
35df4500 4128 bl->inserted = 0;
c906108c
SS
4129}
4130
53a5351d
JM
4131/* Clear the "inserted" flag in all breakpoints and delete any
4132 breakpoints which should go away between runs of the program.
c906108c
SS
4133
4134 Plus other such housekeeping that has to be done for breakpoints
4135 between runs.
4136
53a5351d
JM
4137 Note: this function gets called at the end of a run (by
4138 generic_mourn_inferior) and when a run begins (by
4a64f543 4139 init_wait_for_inferior). */
c906108c
SS
4140
4141
4142
4143void
fba45db2 4144breakpoint_init_inferior (enum inf_context context)
c906108c 4145{
35df4500 4146 struct breakpoint *b, *b_tmp;
870f88f7 4147 struct bp_location *bl;
1c5cfe86 4148 int ix;
6c95b8df 4149 struct program_space *pspace = current_program_space;
c906108c 4150
50c71eaf
PA
4151 /* If breakpoint locations are shared across processes, then there's
4152 nothing to do. */
f5656ead 4153 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4154 return;
4155
1a853c52 4156 mark_breakpoints_out ();
075f6582 4157
35df4500 4158 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4159 {
6c95b8df
PA
4160 if (b->loc && b->loc->pspace != pspace)
4161 continue;
4162
c5aa993b
JM
4163 switch (b->type)
4164 {
4165 case bp_call_dummy:
e2e4d78b 4166 case bp_longjmp_call_dummy:
c906108c 4167
c5aa993b 4168 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4169 cause problems when the inferior is rerun, so we better get
4170 rid of it. */
4171
4172 case bp_watchpoint_scope:
4173
4174 /* Also get rid of scope breakpoints. */
4175
4176 case bp_shlib_event:
4177
4178 /* Also remove solib event breakpoints. Their addresses may
4179 have changed since the last time we ran the program.
4180 Actually we may now be debugging against different target;
4181 and so the solib backend that installed this breakpoint may
4182 not be used in by the target. E.g.,
4183
4184 (gdb) file prog-linux
4185 (gdb) run # native linux target
4186 ...
4187 (gdb) kill
4188 (gdb) file prog-win.exe
4189 (gdb) tar rem :9999 # remote Windows gdbserver.
4190 */
c906108c 4191
f59f708a
PA
4192 case bp_step_resume:
4193
4194 /* Also remove step-resume breakpoints. */
4195
7c16b83e
PA
4196 case bp_single_step:
4197
4198 /* Also remove single-step breakpoints. */
4199
c5aa993b
JM
4200 delete_breakpoint (b);
4201 break;
c906108c 4202
c5aa993b
JM
4203 case bp_watchpoint:
4204 case bp_hardware_watchpoint:
4205 case bp_read_watchpoint:
4206 case bp_access_watchpoint:
3a5c3e22
PA
4207 {
4208 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4209
3a5c3e22
PA
4210 /* Likewise for watchpoints on local expressions. */
4211 if (w->exp_valid_block != NULL)
4212 delete_breakpoint (b);
63000888 4213 else
3a5c3e22 4214 {
63000888
PA
4215 /* Get rid of existing locations, which are no longer
4216 valid. New ones will be created in
4217 update_watchpoint, when the inferior is restarted.
4218 The next update_global_location_list call will
4219 garbage collect them. */
4220 b->loc = NULL;
4221
4222 if (context == inf_starting)
4223 {
4224 /* Reset val field to force reread of starting value in
4225 insert_breakpoints. */
4226 if (w->val)
4227 value_free (w->val);
4228 w->val = NULL;
4229 w->val_valid = 0;
4230 }
4231 }
3a5c3e22 4232 }
c5aa993b
JM
4233 break;
4234 default:
c5aa993b
JM
4235 break;
4236 }
4237 }
1c5cfe86
PA
4238
4239 /* Get rid of the moribund locations. */
35df4500
TJB
4240 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4241 decref_bp_location (&bl);
1c5cfe86 4242 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4243}
4244
6c95b8df
PA
4245/* These functions concern about actual breakpoints inserted in the
4246 target --- to e.g. check if we need to do decr_pc adjustment or if
4247 we need to hop over the bkpt --- so we check for address space
4248 match, not program space. */
4249
c2c6d25f
JM
4250/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4251 exists at PC. It returns ordinary_breakpoint_here if it's an
4252 ordinary breakpoint, or permanent_breakpoint_here if it's a
4253 permanent breakpoint.
4254 - When continuing from a location with an ordinary breakpoint, we
4255 actually single step once before calling insert_breakpoints.
e5dd4106 4256 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4257 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4258 the target, to advance the PC past the breakpoint. */
c906108c 4259
c2c6d25f 4260enum breakpoint_here
6c95b8df 4261breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4262{
35df4500 4263 struct bp_location *bl, **blp_tmp;
c2c6d25f 4264 int any_breakpoint_here = 0;
c906108c 4265
35df4500 4266 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4267 {
35df4500
TJB
4268 if (bl->loc_type != bp_loc_software_breakpoint
4269 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4270 continue;
4271
f1310107 4272 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4273 if ((breakpoint_enabled (bl->owner)
1a853c52 4274 || bl->permanent)
f1310107 4275 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4276 {
4277 if (overlay_debugging
35df4500
TJB
4278 && section_is_overlay (bl->section)
4279 && !section_is_mapped (bl->section))
075f6582 4280 continue; /* unmapped overlay -- can't be a match */
1a853c52 4281 else if (bl->permanent)
075f6582
DJ
4282 return permanent_breakpoint_here;
4283 else
4284 any_breakpoint_here = 1;
4285 }
4286 }
c906108c 4287
f486487f 4288 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4289}
4290
d35ae833
PA
4291/* See breakpoint.h. */
4292
4293int
4294breakpoint_in_range_p (struct address_space *aspace,
4295 CORE_ADDR addr, ULONGEST len)
4296{
4297 struct bp_location *bl, **blp_tmp;
4298
4299 ALL_BP_LOCATIONS (bl, blp_tmp)
4300 {
4301 if (bl->loc_type != bp_loc_software_breakpoint
4302 && bl->loc_type != bp_loc_hardware_breakpoint)
4303 continue;
4304
4305 if ((breakpoint_enabled (bl->owner)
4306 || bl->permanent)
4307 && breakpoint_location_address_range_overlap (bl, aspace,
4308 addr, len))
4309 {
4310 if (overlay_debugging
4311 && section_is_overlay (bl->section)
4312 && !section_is_mapped (bl->section))
4313 {
4314 /* Unmapped overlay -- can't be a match. */
4315 continue;
4316 }
4317
4318 return 1;
4319 }
4320 }
4321
4322 return 0;
4323}
4324
1c5cfe86
PA
4325/* Return true if there's a moribund breakpoint at PC. */
4326
4327int
6c95b8df 4328moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4329{
4330 struct bp_location *loc;
4331 int ix;
4332
4333 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4334 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4335 return 1;
4336
4337 return 0;
4338}
c2c6d25f 4339
f7ce857f
PA
4340/* Returns non-zero iff BL is inserted at PC, in address space
4341 ASPACE. */
4342
4343static int
4344bp_location_inserted_here_p (struct bp_location *bl,
4345 struct address_space *aspace, CORE_ADDR pc)
4346{
4347 if (bl->inserted
4348 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4349 aspace, pc))
4350 {
4351 if (overlay_debugging
4352 && section_is_overlay (bl->section)
4353 && !section_is_mapped (bl->section))
4354 return 0; /* unmapped overlay -- can't be a match */
4355 else
4356 return 1;
4357 }
4358 return 0;
4359}
4360
a1fd2fa5 4361/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4362
4363int
a1fd2fa5 4364breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4365{
f7ce857f 4366 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4367
f7ce857f 4368 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4369 {
f7ce857f
PA
4370 struct bp_location *bl = *blp;
4371
35df4500
TJB
4372 if (bl->loc_type != bp_loc_software_breakpoint
4373 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4374 continue;
4375
f7ce857f
PA
4376 if (bp_location_inserted_here_p (bl, aspace, pc))
4377 return 1;
c5aa993b 4378 }
c36b740a
VP
4379 return 0;
4380}
4381
a1fd2fa5
PA
4382/* This function returns non-zero iff there is a software breakpoint
4383 inserted at PC. */
c36b740a
VP
4384
4385int
a1fd2fa5
PA
4386software_breakpoint_inserted_here_p (struct address_space *aspace,
4387 CORE_ADDR pc)
4fa8626c 4388{
f7ce857f 4389 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4390
f7ce857f 4391 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4392 {
f7ce857f
PA
4393 struct bp_location *bl = *blp;
4394
35df4500 4395 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4396 continue;
4397
f7ce857f
PA
4398 if (bp_location_inserted_here_p (bl, aspace, pc))
4399 return 1;
4fa8626c
DJ
4400 }
4401
4402 return 0;
9c02b525
PA
4403}
4404
4405/* See breakpoint.h. */
4406
4407int
4408hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4409 CORE_ADDR pc)
4410{
4411 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4412
4413 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4414 {
4415 struct bp_location *bl = *blp;
4416
4417 if (bl->loc_type != bp_loc_hardware_breakpoint)
4418 continue;
4419
4420 if (bp_location_inserted_here_p (bl, aspace, pc))
4421 return 1;
4422 }
4423
4424 return 0;
4fa8626c
DJ
4425}
4426
9093389c
PA
4427int
4428hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4429 CORE_ADDR addr, ULONGEST len)
4430{
4431 struct breakpoint *bpt;
4432
4433 ALL_BREAKPOINTS (bpt)
4434 {
4435 struct bp_location *loc;
4436
4437 if (bpt->type != bp_hardware_watchpoint
4438 && bpt->type != bp_access_watchpoint)
4439 continue;
4440
4441 if (!breakpoint_enabled (bpt))
4442 continue;
4443
4444 for (loc = bpt->loc; loc; loc = loc->next)
4445 if (loc->pspace->aspace == aspace && loc->inserted)
4446 {
4447 CORE_ADDR l, h;
4448
4449 /* Check for intersection. */
768adc05
PA
4450 l = std::max<CORE_ADDR> (loc->address, addr);
4451 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4452 if (l < h)
4453 return 1;
4454 }
4455 }
4456 return 0;
4457}
c906108c 4458\f
c5aa993b 4459
c906108c
SS
4460/* bpstat stuff. External routines' interfaces are documented
4461 in breakpoint.h. */
4462
4463int
c326b90e 4464is_catchpoint (struct breakpoint *ep)
c906108c 4465{
533be4dd 4466 return (ep->type == bp_catchpoint);
c906108c
SS
4467}
4468
f431efe5
PA
4469/* Frees any storage that is part of a bpstat. Does not walk the
4470 'next' chain. */
4471
4472static void
198757a8
VP
4473bpstat_free (bpstat bs)
4474{
4475 if (bs->old_val != NULL)
4476 value_free (bs->old_val);
9add0f1b 4477 decref_counted_command_line (&bs->commands);
f431efe5 4478 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4479 xfree (bs);
4480}
4481
c906108c
SS
4482/* Clear a bpstat so that it says we are not at any breakpoint.
4483 Also free any storage that is part of a bpstat. */
4484
4485void
fba45db2 4486bpstat_clear (bpstat *bsp)
c906108c
SS
4487{
4488 bpstat p;
4489 bpstat q;
4490
4491 if (bsp == 0)
4492 return;
4493 p = *bsp;
4494 while (p != NULL)
4495 {
4496 q = p->next;
198757a8 4497 bpstat_free (p);
c906108c
SS
4498 p = q;
4499 }
4500 *bsp = NULL;
4501}
4502
4503/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4504 is part of the bpstat is copied as well. */
4505
4506bpstat
fba45db2 4507bpstat_copy (bpstat bs)
c906108c
SS
4508{
4509 bpstat p = NULL;
4510 bpstat tmp;
4511 bpstat retval = NULL;
4512
4513 if (bs == NULL)
4514 return bs;
4515
4516 for (; bs != NULL; bs = bs->next)
4517 {
4518 tmp = (bpstat) xmalloc (sizeof (*tmp));
4519 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4520 incref_counted_command_line (tmp->commands);
f431efe5 4521 incref_bp_location (tmp->bp_location_at);
31cc81e9 4522 if (bs->old_val != NULL)
3c3185ac
JK
4523 {
4524 tmp->old_val = value_copy (bs->old_val);
4525 release_value (tmp->old_val);
4526 }
31cc81e9 4527
c906108c
SS
4528 if (p == NULL)
4529 /* This is the first thing in the chain. */
4530 retval = tmp;
4531 else
4532 p->next = tmp;
4533 p = tmp;
4534 }
4535 p->next = NULL;
4536 return retval;
4537}
4538
4a64f543 4539/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4540
4541bpstat
fba45db2 4542bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4543{
c5aa993b
JM
4544 if (bsp == NULL)
4545 return NULL;
c906108c 4546
c5aa993b
JM
4547 for (; bsp != NULL; bsp = bsp->next)
4548 {
f431efe5 4549 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4550 return bsp;
4551 }
c906108c
SS
4552 return NULL;
4553}
4554
ab04a2af
TT
4555/* See breakpoint.h. */
4556
47591c29 4557int
427cd150 4558bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4559{
ab04a2af
TT
4560 for (; bsp != NULL; bsp = bsp->next)
4561 {
427cd150
TT
4562 if (bsp->breakpoint_at == NULL)
4563 {
4564 /* A moribund location can never explain a signal other than
4565 GDB_SIGNAL_TRAP. */
4566 if (sig == GDB_SIGNAL_TRAP)
47591c29 4567 return 1;
427cd150
TT
4568 }
4569 else
47591c29
PA
4570 {
4571 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4572 sig))
4573 return 1;
4574 }
ab04a2af
TT
4575 }
4576
47591c29 4577 return 0;
ab04a2af
TT
4578}
4579
4a64f543
MS
4580/* Put in *NUM the breakpoint number of the first breakpoint we are
4581 stopped at. *BSP upon return is a bpstat which points to the
4582 remaining breakpoints stopped at (but which is not guaranteed to be
4583 good for anything but further calls to bpstat_num).
4584
8671a17b
PA
4585 Return 0 if passed a bpstat which does not indicate any breakpoints.
4586 Return -1 if stopped at a breakpoint that has been deleted since
4587 we set it.
4588 Return 1 otherwise. */
c906108c
SS
4589
4590int
8671a17b 4591bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4592{
4593 struct breakpoint *b;
4594
4595 if ((*bsp) == NULL)
4596 return 0; /* No more breakpoint values */
8671a17b 4597
4a64f543
MS
4598 /* We assume we'll never have several bpstats that correspond to a
4599 single breakpoint -- otherwise, this function might return the
4600 same number more than once and this will look ugly. */
f431efe5 4601 b = (*bsp)->breakpoint_at;
8671a17b
PA
4602 *bsp = (*bsp)->next;
4603 if (b == NULL)
4604 return -1; /* breakpoint that's been deleted since */
4605
4606 *num = b->number; /* We have its number */
4607 return 1;
c906108c
SS
4608}
4609
e93ca019 4610/* See breakpoint.h. */
c906108c
SS
4611
4612void
e93ca019 4613bpstat_clear_actions (void)
c906108c 4614{
e93ca019
JK
4615 struct thread_info *tp;
4616 bpstat bs;
4617
4618 if (ptid_equal (inferior_ptid, null_ptid))
4619 return;
4620
4621 tp = find_thread_ptid (inferior_ptid);
4622 if (tp == NULL)
4623 return;
4624
4625 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4626 {
9add0f1b 4627 decref_counted_command_line (&bs->commands);
abf85f46 4628
c906108c
SS
4629 if (bs->old_val != NULL)
4630 {
4631 value_free (bs->old_val);
4632 bs->old_val = NULL;
4633 }
4634 }
4635}
4636
f3b1572e
PA
4637/* Called when a command is about to proceed the inferior. */
4638
4639static void
4640breakpoint_about_to_proceed (void)
4641{
4642 if (!ptid_equal (inferior_ptid, null_ptid))
4643 {
4644 struct thread_info *tp = inferior_thread ();
4645
4646 /* Allow inferior function calls in breakpoint commands to not
4647 interrupt the command list. When the call finishes
4648 successfully, the inferior will be standing at the same
4649 breakpoint as if nothing happened. */
16c381f0 4650 if (tp->control.in_infcall)
f3b1572e
PA
4651 return;
4652 }
4653
4654 breakpoint_proceeded = 1;
4655}
4656
4a64f543
MS
4657/* Stub for cleaning up our state if we error-out of a breakpoint
4658 command. */
c906108c 4659static void
4efb68b1 4660cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4661{
4662 executing_breakpoint_commands = 0;
4663}
4664
abf85f46
JK
4665/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4666 or its equivalent. */
4667
4668static int
4669command_line_is_silent (struct command_line *cmd)
4670{
4f45d445 4671 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4672}
4673
4a64f543
MS
4674/* Execute all the commands associated with all the breakpoints at
4675 this location. Any of these commands could cause the process to
4676 proceed beyond this point, etc. We look out for such changes by
4677 checking the global "breakpoint_proceeded" after each command.
c906108c 4678
347bddb7
PA
4679 Returns true if a breakpoint command resumed the inferior. In that
4680 case, it is the caller's responsibility to recall it again with the
4681 bpstat of the current thread. */
4682
4683static int
4684bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4685{
4686 bpstat bs;
4687 struct cleanup *old_chain;
347bddb7 4688 int again = 0;
c906108c
SS
4689
4690 /* Avoid endless recursion if a `source' command is contained
4691 in bs->commands. */
4692 if (executing_breakpoint_commands)
347bddb7 4693 return 0;
c906108c
SS
4694
4695 executing_breakpoint_commands = 1;
4696 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4697
cf6c5ffb
TT
4698 prevent_dont_repeat ();
4699
4a64f543 4700 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4701 bs = *bsp;
4702
4703 breakpoint_proceeded = 0;
4704 for (; bs != NULL; bs = bs->next)
4705 {
9add0f1b 4706 struct counted_command_line *ccmd;
6c50ab1c
JB
4707 struct command_line *cmd;
4708 struct cleanup *this_cmd_tree_chain;
4709
4710 /* Take ownership of the BSP's command tree, if it has one.
4711
4712 The command tree could legitimately contain commands like
4713 'step' and 'next', which call clear_proceed_status, which
4714 frees stop_bpstat's command tree. To make sure this doesn't
4715 free the tree we're executing out from under us, we need to
4716 take ownership of the tree ourselves. Since a given bpstat's
4717 commands are only executed once, we don't need to copy it; we
4718 can clear the pointer in the bpstat, and make sure we free
4719 the tree when we're done. */
9add0f1b
TT
4720 ccmd = bs->commands;
4721 bs->commands = NULL;
abf85f46
JK
4722 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4723 cmd = ccmd ? ccmd->commands : NULL;
4724 if (command_line_is_silent (cmd))
4725 {
4726 /* The action has been already done by bpstat_stop_status. */
4727 cmd = cmd->next;
4728 }
6c50ab1c 4729
c906108c
SS
4730 while (cmd != NULL)
4731 {
4732 execute_control_command (cmd);
4733
4734 if (breakpoint_proceeded)
4735 break;
4736 else
4737 cmd = cmd->next;
4738 }
6c50ab1c
JB
4739
4740 /* We can free this command tree now. */
4741 do_cleanups (this_cmd_tree_chain);
4742
c906108c 4743 if (breakpoint_proceeded)
32c1e744 4744 {
cb814510 4745 if (current_ui->async)
347bddb7
PA
4746 /* If we are in async mode, then the target might be still
4747 running, not stopped at any breakpoint, so nothing for
4748 us to do here -- just return to the event loop. */
4749 ;
32c1e744
VP
4750 else
4751 /* In sync mode, when execute_control_command returns
4752 we're already standing on the next breakpoint.
347bddb7
PA
4753 Breakpoint commands for that stop were not run, since
4754 execute_command does not run breakpoint commands --
4755 only command_line_handler does, but that one is not
4756 involved in execution of breakpoint commands. So, we
4757 can now execute breakpoint commands. It should be
4758 noted that making execute_command do bpstat actions is
4759 not an option -- in this case we'll have recursive
4760 invocation of bpstat for each breakpoint with a
4761 command, and can easily blow up GDB stack. Instead, we
4762 return true, which will trigger the caller to recall us
4763 with the new stop_bpstat. */
4764 again = 1;
4765 break;
32c1e744 4766 }
c906108c 4767 }
c2b8ed2c 4768 do_cleanups (old_chain);
347bddb7
PA
4769 return again;
4770}
4771
4772void
4773bpstat_do_actions (void)
4774{
353d1d73
JK
4775 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4776
347bddb7
PA
4777 /* Do any commands attached to breakpoint we are stopped at. */
4778 while (!ptid_equal (inferior_ptid, null_ptid)
4779 && target_has_execution
4780 && !is_exited (inferior_ptid)
4781 && !is_executing (inferior_ptid))
4782 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4783 and only return when it is stopped at the next breakpoint, we
4784 keep doing breakpoint actions until it returns false to
4785 indicate the inferior was not resumed. */
16c381f0 4786 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4787 break;
353d1d73
JK
4788
4789 discard_cleanups (cleanup_if_error);
c906108c
SS
4790}
4791
fa4727a6
DJ
4792/* Print out the (old or new) value associated with a watchpoint. */
4793
4794static void
4795watchpoint_value_print (struct value *val, struct ui_file *stream)
4796{
4797 if (val == NULL)
4798 fprintf_unfiltered (stream, _("<unreadable>"));
4799 else
79a45b7d
TT
4800 {
4801 struct value_print_options opts;
4802 get_user_print_options (&opts);
4803 value_print (val, stream, &opts);
4804 }
fa4727a6
DJ
4805}
4806
f303dbd6
PA
4807/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4808 debugging multiple threads. */
4809
4810void
4811maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4812{
4813 if (ui_out_is_mi_like_p (uiout))
4814 return;
4815
4816 ui_out_text (uiout, "\n");
4817
4818 if (show_thread_that_caused_stop ())
4819 {
4820 const char *name;
4821 struct thread_info *thr = inferior_thread ();
4822
4823 ui_out_text (uiout, "Thread ");
4824 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4825
4826 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4827 if (name != NULL)
4828 {
4829 ui_out_text (uiout, " \"");
4830 ui_out_field_fmt (uiout, "name", "%s", name);
4831 ui_out_text (uiout, "\"");
4832 }
4833
4834 ui_out_text (uiout, " hit ");
4835 }
4836}
4837
e514a9d6 4838/* Generic routine for printing messages indicating why we
4a64f543 4839 stopped. The behavior of this function depends on the value
e514a9d6
JM
4840 'print_it' in the bpstat structure. Under some circumstances we
4841 may decide not to print anything here and delegate the task to
4a64f543 4842 normal_stop(). */
e514a9d6
JM
4843
4844static enum print_stop_action
4845print_bp_stop_message (bpstat bs)
4846{
4847 switch (bs->print_it)
4848 {
4849 case print_it_noop:
4a64f543 4850 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4851 return PRINT_UNKNOWN;
4852 break;
4853
4854 case print_it_done:
4855 /* We still want to print the frame, but we already printed the
4a64f543 4856 relevant messages. */
e514a9d6
JM
4857 return PRINT_SRC_AND_LOC;
4858 break;
4859
4860 case print_it_normal:
4f8d1dc6 4861 {
f431efe5
PA
4862 struct breakpoint *b = bs->breakpoint_at;
4863
1a6a67de
TJB
4864 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4865 which has since been deleted. */
4866 if (b == NULL)
4867 return PRINT_UNKNOWN;
4868
348d480f
PA
4869 /* Normal case. Call the breakpoint's print_it method. */
4870 return b->ops->print_it (bs);
4f8d1dc6 4871 }
348d480f 4872 break;
3086aeae 4873
e514a9d6 4874 default:
8e65ff28 4875 internal_error (__FILE__, __LINE__,
e2e0b3e5 4876 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4877 break;
c906108c 4878 }
c906108c
SS
4879}
4880
edcc5120
TT
4881/* A helper function that prints a shared library stopped event. */
4882
4883static void
4884print_solib_event (int is_catchpoint)
4885{
4886 int any_deleted
4887 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4888 int any_added
4889 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4890
4891 if (!is_catchpoint)
4892 {
4893 if (any_added || any_deleted)
4894 ui_out_text (current_uiout,
4895 _("Stopped due to shared library event:\n"));
4896 else
4897 ui_out_text (current_uiout,
4898 _("Stopped due to shared library event (no "
4899 "libraries added or removed)\n"));
4900 }
4901
4902 if (ui_out_is_mi_like_p (current_uiout))
4903 ui_out_field_string (current_uiout, "reason",
4904 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4905
4906 if (any_deleted)
4907 {
4908 struct cleanup *cleanup;
4909 char *name;
4910 int ix;
4911
4912 ui_out_text (current_uiout, _(" Inferior unloaded "));
4913 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4914 "removed");
4915 for (ix = 0;
4916 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4917 ix, name);
4918 ++ix)
4919 {
4920 if (ix > 0)
4921 ui_out_text (current_uiout, " ");
4922 ui_out_field_string (current_uiout, "library", name);
4923 ui_out_text (current_uiout, "\n");
4924 }
4925
4926 do_cleanups (cleanup);
4927 }
4928
4929 if (any_added)
4930 {
4931 struct so_list *iter;
4932 int ix;
4933 struct cleanup *cleanup;
4934
4935 ui_out_text (current_uiout, _(" Inferior loaded "));
4936 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4937 "added");
4938 for (ix = 0;
4939 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4940 ix, iter);
4941 ++ix)
4942 {
4943 if (ix > 0)
4944 ui_out_text (current_uiout, " ");
4945 ui_out_field_string (current_uiout, "library", iter->so_name);
4946 ui_out_text (current_uiout, "\n");
4947 }
4948
4949 do_cleanups (cleanup);
4950 }
4951}
4952
e514a9d6
JM
4953/* Print a message indicating what happened. This is called from
4954 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4955 list - a list of the eventpoints that caused this stop. KIND is
4956 the target_waitkind for the stopping event. This
e514a9d6
JM
4957 routine calls the generic print routine for printing a message
4958 about reasons for stopping. This will print (for example) the
4959 "Breakpoint n," part of the output. The return value of this
4960 routine is one of:
c906108c 4961
4a64f543 4962 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4963 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4964 code to print the location. An example is
c5aa993b
JM
4965 "Breakpoint 1, " which should be followed by
4966 the location.
917317f4 4967 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4968 to also print the location part of the message.
4969 An example is the catch/throw messages, which
4a64f543 4970 don't require a location appended to the end.
917317f4 4971 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4972 further info to be printed. */
c906108c 4973
917317f4 4974enum print_stop_action
36dfb11c 4975bpstat_print (bpstat bs, int kind)
c906108c 4976{
f486487f 4977 enum print_stop_action val;
c5aa993b 4978
c906108c 4979 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4980 (Currently all watchpoints go on the bpstat whether hit or not.
4981 That probably could (should) be changed, provided care is taken
c906108c 4982 with respect to bpstat_explains_signal). */
e514a9d6
JM
4983 for (; bs; bs = bs->next)
4984 {
4985 val = print_bp_stop_message (bs);
4986 if (val == PRINT_SRC_ONLY
4987 || val == PRINT_SRC_AND_LOC
4988 || val == PRINT_NOTHING)
4989 return val;
4990 }
c906108c 4991
36dfb11c
TT
4992 /* If we had hit a shared library event breakpoint,
4993 print_bp_stop_message would print out this message. If we hit an
4994 OS-level shared library event, do the same thing. */
4995 if (kind == TARGET_WAITKIND_LOADED)
4996 {
edcc5120 4997 print_solib_event (0);
36dfb11c
TT
4998 return PRINT_NOTHING;
4999 }
5000
e514a9d6 5001 /* We reached the end of the chain, or we got a null BS to start
4a64f543 5002 with and nothing was printed. */
917317f4 5003 return PRINT_UNKNOWN;
c906108c
SS
5004}
5005
c42bd95a
DE
5006/* Evaluate the expression EXP and return 1 if value is zero.
5007 This returns the inverse of the condition because it is called
5008 from catch_errors which returns 0 if an exception happened, and if an
5009 exception happens we want execution to stop.
4a64f543 5010 The argument is a "struct expression *" that has been cast to a
c42bd95a 5011 "void *" to make it pass through catch_errors. */
c906108c
SS
5012
5013static int
4efb68b1 5014breakpoint_cond_eval (void *exp)
c906108c 5015{
278cd55f 5016 struct value *mark = value_mark ();
c5aa993b 5017 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 5018
c906108c
SS
5019 value_free_to_mark (mark);
5020 return i;
5021}
5022
5760d0ab 5023/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5024
5025static bpstat
5760d0ab 5026bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5027{
5028 bpstat bs;
5029
5030 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5031 bs->next = NULL;
5032 **bs_link_pointer = bs;
5033 *bs_link_pointer = &bs->next;
f431efe5
PA
5034 bs->breakpoint_at = bl->owner;
5035 bs->bp_location_at = bl;
5036 incref_bp_location (bl);
c906108c
SS
5037 /* If the condition is false, etc., don't do the commands. */
5038 bs->commands = NULL;
5039 bs->old_val = NULL;
5040 bs->print_it = print_it_normal;
5041 return bs;
5042}
5043\f
d983da9c
DJ
5044/* The target has stopped with waitstatus WS. Check if any hardware
5045 watchpoints have triggered, according to the target. */
5046
5047int
5048watchpoints_triggered (struct target_waitstatus *ws)
5049{
d92524f1 5050 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5051 CORE_ADDR addr;
5052 struct breakpoint *b;
5053
5054 if (!stopped_by_watchpoint)
5055 {
5056 /* We were not stopped by a watchpoint. Mark all watchpoints
5057 as not triggered. */
5058 ALL_BREAKPOINTS (b)
cc60f2e3 5059 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5060 {
5061 struct watchpoint *w = (struct watchpoint *) b;
5062
5063 w->watchpoint_triggered = watch_triggered_no;
5064 }
d983da9c
DJ
5065
5066 return 0;
5067 }
5068
5069 if (!target_stopped_data_address (&current_target, &addr))
5070 {
5071 /* We were stopped by a watchpoint, but we don't know where.
5072 Mark all watchpoints as unknown. */
5073 ALL_BREAKPOINTS (b)
cc60f2e3 5074 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5075 {
5076 struct watchpoint *w = (struct watchpoint *) b;
5077
5078 w->watchpoint_triggered = watch_triggered_unknown;
5079 }
d983da9c 5080
3c4797ba 5081 return 1;
d983da9c
DJ
5082 }
5083
5084 /* The target could report the data address. Mark watchpoints
5085 affected by this data address as triggered, and all others as not
5086 triggered. */
5087
5088 ALL_BREAKPOINTS (b)
cc60f2e3 5089 if (is_hardware_watchpoint (b))
d983da9c 5090 {
3a5c3e22 5091 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5092 struct bp_location *loc;
d983da9c 5093
3a5c3e22 5094 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5095 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5096 {
3a5c3e22 5097 if (is_masked_watchpoint (b))
9c06b0b4 5098 {
3a5c3e22
PA
5099 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5100 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5101
5102 if (newaddr == start)
5103 {
3a5c3e22 5104 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5105 break;
5106 }
5107 }
5108 /* Exact match not required. Within range is sufficient. */
5109 else if (target_watchpoint_addr_within_range (&current_target,
5110 addr, loc->address,
5111 loc->length))
5112 {
3a5c3e22 5113 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5114 break;
5115 }
5116 }
d983da9c
DJ
5117 }
5118
5119 return 1;
5120}
5121
c906108c
SS
5122/* Possible return values for watchpoint_check (this can't be an enum
5123 because of check_errors). */
5124/* The watchpoint has been deleted. */
5125#define WP_DELETED 1
5126/* The value has changed. */
5127#define WP_VALUE_CHANGED 2
5128/* The value has not changed. */
5129#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5130/* Ignore this watchpoint, no matter if the value changed or not. */
5131#define WP_IGNORE 4
c906108c
SS
5132
5133#define BP_TEMPFLAG 1
5134#define BP_HARDWAREFLAG 2
5135
4a64f543
MS
5136/* Evaluate watchpoint condition expression and check if its value
5137 changed.
553e4c11
JB
5138
5139 P should be a pointer to struct bpstat, but is defined as a void *
5140 in order for this function to be usable with catch_errors. */
c906108c
SS
5141
5142static int
4efb68b1 5143watchpoint_check (void *p)
c906108c
SS
5144{
5145 bpstat bs = (bpstat) p;
3a5c3e22 5146 struct watchpoint *b;
c906108c
SS
5147 struct frame_info *fr;
5148 int within_current_scope;
5149
f431efe5 5150 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5151 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5152 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5153
f6bc2008
PA
5154 /* If this is a local watchpoint, we only want to check if the
5155 watchpoint frame is in scope if the current thread is the thread
5156 that was used to create the watchpoint. */
5157 if (!watchpoint_in_thread_scope (b))
60e1c644 5158 return WP_IGNORE;
f6bc2008 5159
c906108c
SS
5160 if (b->exp_valid_block == NULL)
5161 within_current_scope = 1;
5162 else
5163 {
edb3359d
DJ
5164 struct frame_info *frame = get_current_frame ();
5165 struct gdbarch *frame_arch = get_frame_arch (frame);
5166 CORE_ADDR frame_pc = get_frame_pc (frame);
5167
c9cf6e20 5168 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5169 still in the function but the stack frame has already been
5170 invalidated. Since we can't rely on the values of local
5171 variables after the stack has been destroyed, we are treating
5172 the watchpoint in that state as `not changed' without further
5173 checking. Don't mark watchpoints as changed if the current
5174 frame is in an epilogue - even if they are in some other
5175 frame, our view of the stack is likely to be wrong and
5176 frame_find_by_id could error out. */
c9cf6e20 5177 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5178 return WP_IGNORE;
a0f49112 5179
101dcfbe 5180 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5181 within_current_scope = (fr != NULL);
69fbadd5
DJ
5182
5183 /* If we've gotten confused in the unwinder, we might have
5184 returned a frame that can't describe this variable. */
edb3359d
DJ
5185 if (within_current_scope)
5186 {
5187 struct symbol *function;
5188
5189 function = get_frame_function (fr);
5190 if (function == NULL
5191 || !contained_in (b->exp_valid_block,
5192 SYMBOL_BLOCK_VALUE (function)))
5193 within_current_scope = 0;
5194 }
69fbadd5 5195
edb3359d 5196 if (within_current_scope)
c906108c
SS
5197 /* If we end up stopping, the current frame will get selected
5198 in normal_stop. So this call to select_frame won't affect
5199 the user. */
0f7d239c 5200 select_frame (fr);
c906108c 5201 }
c5aa993b 5202
c906108c
SS
5203 if (within_current_scope)
5204 {
4a64f543
MS
5205 /* We use value_{,free_to_}mark because it could be a *long*
5206 time before we return to the command level and call
5207 free_all_values. We can't call free_all_values because we
5208 might be in the middle of evaluating a function call. */
c906108c 5209
0cf6dd15 5210 int pc = 0;
9c06b0b4 5211 struct value *mark;
fa4727a6
DJ
5212 struct value *new_val;
5213
3a5c3e22 5214 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5215 /* Since we don't know the exact trigger address (from
5216 stopped_data_address), just tell the user we've triggered
5217 a mask watchpoint. */
5218 return WP_VALUE_CHANGED;
5219
5220 mark = value_mark ();
3a1115a0 5221 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5222
bb9d5f81
PP
5223 if (b->val_bitsize != 0)
5224 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5225
4a64f543
MS
5226 /* We use value_equal_contents instead of value_equal because
5227 the latter coerces an array to a pointer, thus comparing just
5228 the address of the array instead of its contents. This is
5229 not what we want. */
fa4727a6 5230 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5231 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5232 {
fa4727a6
DJ
5233 if (new_val != NULL)
5234 {
5235 release_value (new_val);
5236 value_free_to_mark (mark);
5237 }
c906108c
SS
5238 bs->old_val = b->val;
5239 b->val = new_val;
fa4727a6 5240 b->val_valid = 1;
c906108c
SS
5241 return WP_VALUE_CHANGED;
5242 }
5243 else
5244 {
60e1c644 5245 /* Nothing changed. */
c906108c 5246 value_free_to_mark (mark);
c906108c
SS
5247 return WP_VALUE_NOT_CHANGED;
5248 }
5249 }
5250 else
5251 {
5252 /* This seems like the only logical thing to do because
c5aa993b
JM
5253 if we temporarily ignored the watchpoint, then when
5254 we reenter the block in which it is valid it contains
5255 garbage (in the case of a function, it may have two
5256 garbage values, one before and one after the prologue).
5257 So we can't even detect the first assignment to it and
5258 watch after that (since the garbage may or may not equal
5259 the first value assigned). */
348d480f
PA
5260 /* We print all the stop information in
5261 breakpoint_ops->print_it, but in this case, by the time we
5262 call breakpoint_ops->print_it this bp will be deleted
5263 already. So we have no choice but print the information
5264 here. */
468afe6c 5265
0e454242 5266 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5267 {
5268 struct ui_out *uiout = current_uiout;
5269
5270 if (ui_out_is_mi_like_p (uiout))
5271 ui_out_field_string
5272 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5273 ui_out_text (uiout, "\nWatchpoint ");
5274 ui_out_field_int (uiout, "wpnum", b->base.number);
5275 ui_out_text (uiout,
5276 " deleted because the program has left the block in\n"
5277 "which its expression is valid.\n");
5278 }
4ce44c66 5279
cdac0397 5280 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5281 decref_counted_command_line (&b->base.commands);
d0fb5eae 5282 watchpoint_del_at_next_stop (b);
c906108c
SS
5283
5284 return WP_DELETED;
5285 }
5286}
5287
18a18393 5288/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5289 breakpoint location BL. This function does not check if we should
5290 stop, only if BL explains the stop. */
5291
18a18393 5292static int
6c95b8df 5293bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5294 struct address_space *aspace, CORE_ADDR bp_addr,
5295 const struct target_waitstatus *ws)
18a18393
VP
5296{
5297 struct breakpoint *b = bl->owner;
5298
348d480f 5299 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5300 gdb_assert (b != NULL);
5301
09ac7c10 5302 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5303}
5304
3a5c3e22
PA
5305/* Determine if the watched values have actually changed, and we
5306 should stop. If not, set BS->stop to 0. */
5307
18a18393
VP
5308static void
5309bpstat_check_watchpoint (bpstat bs)
5310{
2bdf28a0 5311 const struct bp_location *bl;
3a5c3e22 5312 struct watchpoint *b;
2bdf28a0
JK
5313
5314 /* BS is built for existing struct breakpoint. */
f431efe5 5315 bl = bs->bp_location_at;
2bdf28a0 5316 gdb_assert (bl != NULL);
3a5c3e22 5317 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5318 gdb_assert (b != NULL);
18a18393 5319
18a18393 5320 {
18a18393
VP
5321 int must_check_value = 0;
5322
3a5c3e22 5323 if (b->base.type == bp_watchpoint)
18a18393
VP
5324 /* For a software watchpoint, we must always check the
5325 watched value. */
5326 must_check_value = 1;
5327 else if (b->watchpoint_triggered == watch_triggered_yes)
5328 /* We have a hardware watchpoint (read, write, or access)
5329 and the target earlier reported an address watched by
5330 this watchpoint. */
5331 must_check_value = 1;
5332 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5333 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5334 /* We were stopped by a hardware watchpoint, but the target could
5335 not report the data address. We must check the watchpoint's
5336 value. Access and read watchpoints are out of luck; without
5337 a data address, we can't figure it out. */
5338 must_check_value = 1;
3a5c3e22 5339
18a18393
VP
5340 if (must_check_value)
5341 {
3e43a32a
MS
5342 char *message
5343 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5344 b->base.number);
18a18393
VP
5345 struct cleanup *cleanups = make_cleanup (xfree, message);
5346 int e = catch_errors (watchpoint_check, bs, message,
5347 RETURN_MASK_ALL);
5348 do_cleanups (cleanups);
5349 switch (e)
5350 {
5351 case WP_DELETED:
5352 /* We've already printed what needs to be printed. */
5353 bs->print_it = print_it_done;
5354 /* Stop. */
5355 break;
60e1c644
PA
5356 case WP_IGNORE:
5357 bs->print_it = print_it_noop;
5358 bs->stop = 0;
5359 break;
18a18393 5360 case WP_VALUE_CHANGED:
3a5c3e22 5361 if (b->base.type == bp_read_watchpoint)
18a18393 5362 {
85d721b8
PA
5363 /* There are two cases to consider here:
5364
4a64f543 5365 1. We're watching the triggered memory for reads.
85d721b8
PA
5366 In that case, trust the target, and always report
5367 the watchpoint hit to the user. Even though
5368 reads don't cause value changes, the value may
5369 have changed since the last time it was read, and
5370 since we're not trapping writes, we will not see
5371 those, and as such we should ignore our notion of
5372 old value.
5373
4a64f543 5374 2. We're watching the triggered memory for both
85d721b8
PA
5375 reads and writes. There are two ways this may
5376 happen:
5377
4a64f543 5378 2.1. This is a target that can't break on data
85d721b8
PA
5379 reads only, but can break on accesses (reads or
5380 writes), such as e.g., x86. We detect this case
5381 at the time we try to insert read watchpoints.
5382
4a64f543 5383 2.2. Otherwise, the target supports read
85d721b8
PA
5384 watchpoints, but, the user set an access or write
5385 watchpoint watching the same memory as this read
5386 watchpoint.
5387
5388 If we're watching memory writes as well as reads,
5389 ignore watchpoint hits when we find that the
5390 value hasn't changed, as reads don't cause
5391 changes. This still gives false positives when
5392 the program writes the same value to memory as
5393 what there was already in memory (we will confuse
5394 it for a read), but it's much better than
5395 nothing. */
5396
5397 int other_write_watchpoint = 0;
5398
5399 if (bl->watchpoint_type == hw_read)
5400 {
5401 struct breakpoint *other_b;
5402
5403 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5404 if (other_b->type == bp_hardware_watchpoint
5405 || other_b->type == bp_access_watchpoint)
85d721b8 5406 {
3a5c3e22
PA
5407 struct watchpoint *other_w =
5408 (struct watchpoint *) other_b;
5409
5410 if (other_w->watchpoint_triggered
5411 == watch_triggered_yes)
5412 {
5413 other_write_watchpoint = 1;
5414 break;
5415 }
85d721b8
PA
5416 }
5417 }
5418
5419 if (other_write_watchpoint
5420 || bl->watchpoint_type == hw_access)
5421 {
5422 /* We're watching the same memory for writes,
5423 and the value changed since the last time we
5424 updated it, so this trap must be for a write.
5425 Ignore it. */
5426 bs->print_it = print_it_noop;
5427 bs->stop = 0;
5428 }
18a18393
VP
5429 }
5430 break;
5431 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5432 if (b->base.type == bp_hardware_watchpoint
5433 || b->base.type == bp_watchpoint)
18a18393
VP
5434 {
5435 /* Don't stop: write watchpoints shouldn't fire if
5436 the value hasn't changed. */
5437 bs->print_it = print_it_noop;
5438 bs->stop = 0;
5439 }
5440 /* Stop. */
5441 break;
5442 default:
5443 /* Can't happen. */
5444 case 0:
5445 /* Error from catch_errors. */
468afe6c 5446 {
0e454242 5447 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5448 {
5449 printf_filtered (_("Watchpoint %d deleted.\n"),
5450 b->base.number);
5451 }
5452 watchpoint_del_at_next_stop (b);
5453 /* We've already printed what needs to be printed. */
5454 bs->print_it = print_it_done;
5455 }
18a18393
VP
5456 break;
5457 }
5458 }
5459 else /* must_check_value == 0 */
5460 {
5461 /* This is a case where some watchpoint(s) triggered, but
5462 not at the address of this watchpoint, or else no
5463 watchpoint triggered after all. So don't print
5464 anything for this watchpoint. */
5465 bs->print_it = print_it_noop;
5466 bs->stop = 0;
5467 }
5468 }
5469}
5470
7d4df6a4
DE
5471/* For breakpoints that are currently marked as telling gdb to stop,
5472 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5473 of breakpoint referred to by BS. If we should not stop for this
5474 breakpoint, set BS->stop to 0. */
f431efe5 5475
18a18393
VP
5476static void
5477bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5478{
2bdf28a0
JK
5479 const struct bp_location *bl;
5480 struct breakpoint *b;
7d4df6a4
DE
5481 int value_is_zero = 0;
5482 struct expression *cond;
5483
5484 gdb_assert (bs->stop);
2bdf28a0
JK
5485
5486 /* BS is built for existing struct breakpoint. */
f431efe5 5487 bl = bs->bp_location_at;
2bdf28a0 5488 gdb_assert (bl != NULL);
f431efe5 5489 b = bs->breakpoint_at;
2bdf28a0 5490 gdb_assert (b != NULL);
18a18393 5491
b775012e
LM
5492 /* Even if the target evaluated the condition on its end and notified GDB, we
5493 need to do so again since GDB does not know if we stopped due to a
5494 breakpoint or a single step breakpoint. */
5495
18a18393 5496 if (frame_id_p (b->frame_id)
edb3359d 5497 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5498 {
7d4df6a4
DE
5499 bs->stop = 0;
5500 return;
5501 }
60e1c644 5502
12ab52e9
PA
5503 /* If this is a thread/task-specific breakpoint, don't waste cpu
5504 evaluating the condition if this isn't the specified
5505 thread/task. */
5d5658a1 5506 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5507 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5508
6c1b0f7b
DE
5509 {
5510 bs->stop = 0;
5511 return;
5512 }
5513
6dddc817
DE
5514 /* Evaluate extension language breakpoints that have a "stop" method
5515 implemented. */
5516 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5517
7d4df6a4
DE
5518 if (is_watchpoint (b))
5519 {
5520 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5521
7d4df6a4
DE
5522 cond = w->cond_exp;
5523 }
5524 else
5525 cond = bl->cond;
60e1c644 5526
7d4df6a4
DE
5527 if (cond && b->disposition != disp_del_at_next_stop)
5528 {
5529 int within_current_scope = 1;
5530 struct watchpoint * w;
60e1c644 5531
7d4df6a4
DE
5532 /* We use value_mark and value_free_to_mark because it could
5533 be a long time before we return to the command level and
5534 call free_all_values. We can't call free_all_values
5535 because we might be in the middle of evaluating a
5536 function call. */
5537 struct value *mark = value_mark ();
5538
5539 if (is_watchpoint (b))
5540 w = (struct watchpoint *) b;
5541 else
5542 w = NULL;
5543
5544 /* Need to select the frame, with all that implies so that
5545 the conditions will have the right context. Because we
5546 use the frame, we will not see an inlined function's
5547 variables when we arrive at a breakpoint at the start
5548 of the inlined function; the current frame will be the
5549 call site. */
5550 if (w == NULL || w->cond_exp_valid_block == NULL)
5551 select_frame (get_current_frame ());
5552 else
18a18393 5553 {
7d4df6a4
DE
5554 struct frame_info *frame;
5555
5556 /* For local watchpoint expressions, which particular
5557 instance of a local is being watched matters, so we
5558 keep track of the frame to evaluate the expression
5559 in. To evaluate the condition however, it doesn't
5560 really matter which instantiation of the function
5561 where the condition makes sense triggers the
5562 watchpoint. This allows an expression like "watch
5563 global if q > 10" set in `func', catch writes to
5564 global on all threads that call `func', or catch
5565 writes on all recursive calls of `func' by a single
5566 thread. We simply always evaluate the condition in
5567 the innermost frame that's executing where it makes
5568 sense to evaluate the condition. It seems
5569 intuitive. */
5570 frame = block_innermost_frame (w->cond_exp_valid_block);
5571 if (frame != NULL)
5572 select_frame (frame);
5573 else
5574 within_current_scope = 0;
18a18393 5575 }
7d4df6a4
DE
5576 if (within_current_scope)
5577 value_is_zero
5578 = catch_errors (breakpoint_cond_eval, cond,
5579 "Error in testing breakpoint condition:\n",
5580 RETURN_MASK_ALL);
5581 else
18a18393 5582 {
7d4df6a4
DE
5583 warning (_("Watchpoint condition cannot be tested "
5584 "in the current scope"));
5585 /* If we failed to set the right context for this
5586 watchpoint, unconditionally report it. */
5587 value_is_zero = 0;
18a18393 5588 }
7d4df6a4
DE
5589 /* FIXME-someday, should give breakpoint #. */
5590 value_free_to_mark (mark);
18a18393 5591 }
7d4df6a4
DE
5592
5593 if (cond && value_is_zero)
5594 {
5595 bs->stop = 0;
5596 }
7d4df6a4
DE
5597 else if (b->ignore_count > 0)
5598 {
5599 b->ignore_count--;
5600 bs->stop = 0;
5601 /* Increase the hit count even though we don't stop. */
5602 ++(b->hit_count);
5603 observer_notify_breakpoint_modified (b);
5604 }
18a18393
VP
5605}
5606
1cf4d951
PA
5607/* Returns true if we need to track moribund locations of LOC's type
5608 on the current target. */
5609
5610static int
5611need_moribund_for_location_type (struct bp_location *loc)
5612{
5613 return ((loc->loc_type == bp_loc_software_breakpoint
5614 && !target_supports_stopped_by_sw_breakpoint ())
5615 || (loc->loc_type == bp_loc_hardware_breakpoint
5616 && !target_supports_stopped_by_hw_breakpoint ()));
5617}
5618
18a18393 5619
9709f61c 5620/* Get a bpstat associated with having just stopped at address
d983da9c 5621 BP_ADDR in thread PTID.
c906108c 5622
d983da9c 5623 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5624 don't understand this stop. Result is a chain of bpstat's such
5625 that:
c906108c 5626
c5aa993b 5627 if we don't understand the stop, the result is a null pointer.
c906108c 5628
c5aa993b 5629 if we understand why we stopped, the result is not null.
c906108c 5630
c5aa993b
JM
5631 Each element of the chain refers to a particular breakpoint or
5632 watchpoint at which we have stopped. (We may have stopped for
5633 several reasons concurrently.)
c906108c 5634
c5aa993b
JM
5635 Each element of the chain has valid next, breakpoint_at,
5636 commands, FIXME??? fields. */
c906108c
SS
5637
5638bpstat
6c95b8df 5639bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5640 CORE_ADDR bp_addr, ptid_t ptid,
5641 const struct target_waitstatus *ws)
c906108c 5642{
0d381245 5643 struct breakpoint *b = NULL;
afe38095 5644 struct bp_location *bl;
20874c92 5645 struct bp_location *loc;
5760d0ab
JK
5646 /* First item of allocated bpstat's. */
5647 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5648 /* Pointer to the last thing in the chain currently. */
5760d0ab 5649 bpstat bs;
20874c92 5650 int ix;
429374b8 5651 int need_remove_insert;
f431efe5 5652 int removed_any;
c906108c 5653
f431efe5
PA
5654 /* First, build the bpstat chain with locations that explain a
5655 target stop, while being careful to not set the target running,
5656 as that may invalidate locations (in particular watchpoint
5657 locations are recreated). Resuming will happen here with
5658 breakpoint conditions or watchpoint expressions that include
5659 inferior function calls. */
c5aa993b 5660
429374b8
JK
5661 ALL_BREAKPOINTS (b)
5662 {
1a853c52 5663 if (!breakpoint_enabled (b))
429374b8 5664 continue;
a5606eee 5665
429374b8
JK
5666 for (bl = b->loc; bl != NULL; bl = bl->next)
5667 {
4a64f543
MS
5668 /* For hardware watchpoints, we look only at the first
5669 location. The watchpoint_check function will work on the
5670 entire expression, not the individual locations. For
5671 read watchpoints, the watchpoints_triggered function has
5672 checked all locations already. */
429374b8
JK
5673 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5674 break;
18a18393 5675
f6592439 5676 if (!bl->enabled || bl->shlib_disabled)
429374b8 5677 continue;
c5aa993b 5678
09ac7c10 5679 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5680 continue;
c5aa993b 5681
4a64f543
MS
5682 /* Come here if it's a watchpoint, or if the break address
5683 matches. */
c5aa993b 5684
4a64f543
MS
5685 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5686 explain stop. */
c5aa993b 5687
f431efe5
PA
5688 /* Assume we stop. Should we find a watchpoint that is not
5689 actually triggered, or if the condition of the breakpoint
5690 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5691 bs->stop = 1;
5692 bs->print = 1;
d983da9c 5693
f431efe5
PA
5694 /* If this is a scope breakpoint, mark the associated
5695 watchpoint as triggered so that we will handle the
5696 out-of-scope event. We'll get to the watchpoint next
5697 iteration. */
d0fb5eae 5698 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5699 {
5700 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5701
5702 w->watchpoint_triggered = watch_triggered_yes;
5703 }
f431efe5
PA
5704 }
5705 }
5706
7c16b83e 5707 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5708 if (!target_supports_stopped_by_sw_breakpoint ()
5709 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5710 {
1cf4d951 5711 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5712 {
1cf4d951
PA
5713 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5714 && need_moribund_for_location_type (loc))
5715 {
5716 bs = bpstat_alloc (loc, &bs_link);
5717 /* For hits of moribund locations, we should just proceed. */
5718 bs->stop = 0;
5719 bs->print = 0;
5720 bs->print_it = print_it_noop;
5721 }
f431efe5
PA
5722 }
5723 }
5724
edcc5120
TT
5725 /* A bit of special processing for shlib breakpoints. We need to
5726 process solib loading here, so that the lists of loaded and
5727 unloaded libraries are correct before we handle "catch load" and
5728 "catch unload". */
5729 for (bs = bs_head; bs != NULL; bs = bs->next)
5730 {
5d268276 5731 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5732 {
5733 handle_solib_event ();
5734 break;
5735 }
5736 }
5737
f431efe5
PA
5738 /* Now go through the locations that caused the target to stop, and
5739 check whether we're interested in reporting this stop to higher
5740 layers, or whether we should resume the target transparently. */
5741
5742 removed_any = 0;
5743
5760d0ab 5744 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5745 {
5746 if (!bs->stop)
5747 continue;
5748
f431efe5 5749 b = bs->breakpoint_at;
348d480f
PA
5750 b->ops->check_status (bs);
5751 if (bs->stop)
28010a5d 5752 {
348d480f 5753 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5754
429374b8
JK
5755 if (bs->stop)
5756 {
5757 ++(b->hit_count);
8d3788bd 5758 observer_notify_breakpoint_modified (b);
c906108c 5759
4a64f543 5760 /* We will stop here. */
429374b8
JK
5761 if (b->disposition == disp_disable)
5762 {
816338b5 5763 --(b->enable_count);
1a853c52 5764 if (b->enable_count <= 0)
429374b8 5765 b->enable_state = bp_disabled;
f431efe5 5766 removed_any = 1;
429374b8
JK
5767 }
5768 if (b->silent)
5769 bs->print = 0;
5770 bs->commands = b->commands;
9add0f1b 5771 incref_counted_command_line (bs->commands);
abf85f46
JK
5772 if (command_line_is_silent (bs->commands
5773 ? bs->commands->commands : NULL))
5774 bs->print = 0;
9d6e6e84
HZ
5775
5776 b->ops->after_condition_true (bs);
429374b8
JK
5777 }
5778
348d480f 5779 }
a9b3a50f
PA
5780
5781 /* Print nothing for this entry if we don't stop or don't
5782 print. */
5783 if (!bs->stop || !bs->print)
5784 bs->print_it = print_it_noop;
429374b8 5785 }
876fa593 5786
d983da9c
DJ
5787 /* If we aren't stopping, the value of some hardware watchpoint may
5788 not have changed, but the intermediate memory locations we are
5789 watching may have. Don't bother if we're stopping; this will get
5790 done later. */
d832cb68 5791 need_remove_insert = 0;
5760d0ab
JK
5792 if (! bpstat_causes_stop (bs_head))
5793 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5794 if (!bs->stop
f431efe5
PA
5795 && bs->breakpoint_at
5796 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5797 {
3a5c3e22
PA
5798 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5799
5800 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5801 need_remove_insert = 1;
d983da9c
DJ
5802 }
5803
d832cb68 5804 if (need_remove_insert)
44702360 5805 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5806 else if (removed_any)
44702360 5807 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5808
5760d0ab 5809 return bs_head;
c906108c 5810}
628fe4e4
JK
5811
5812static void
5813handle_jit_event (void)
5814{
5815 struct frame_info *frame;
5816 struct gdbarch *gdbarch;
5817
243a9253
PA
5818 if (debug_infrun)
5819 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5820
628fe4e4
JK
5821 /* Switch terminal for any messages produced by
5822 breakpoint_re_set. */
5823 target_terminal_ours_for_output ();
5824
5825 frame = get_current_frame ();
5826 gdbarch = get_frame_arch (frame);
5827
5828 jit_event_handler (gdbarch);
5829
5830 target_terminal_inferior ();
5831}
5832
5833/* Prepare WHAT final decision for infrun. */
5834
5835/* Decide what infrun needs to do with this bpstat. */
5836
c906108c 5837struct bpstat_what
0e30163f 5838bpstat_what (bpstat bs_head)
c906108c 5839{
c906108c 5840 struct bpstat_what retval;
0e30163f 5841 bpstat bs;
c906108c 5842
628fe4e4 5843 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5844 retval.call_dummy = STOP_NONE;
186c406b 5845 retval.is_longjmp = 0;
628fe4e4 5846
0e30163f 5847 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5848 {
628fe4e4
JK
5849 /* Extract this BS's action. After processing each BS, we check
5850 if its action overrides all we've seem so far. */
5851 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5852 enum bptype bptype;
5853
c906108c 5854 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5855 {
5856 /* I suspect this can happen if it was a momentary
5857 breakpoint which has since been deleted. */
5858 bptype = bp_none;
5859 }
20874c92 5860 else
f431efe5 5861 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5862
5863 switch (bptype)
c906108c
SS
5864 {
5865 case bp_none:
628fe4e4 5866 break;
c906108c
SS
5867 case bp_breakpoint:
5868 case bp_hardware_breakpoint:
7c16b83e 5869 case bp_single_step:
c906108c
SS
5870 case bp_until:
5871 case bp_finish:
a9b3a50f 5872 case bp_shlib_event:
c906108c
SS
5873 if (bs->stop)
5874 {
5875 if (bs->print)
628fe4e4 5876 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5877 else
628fe4e4 5878 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5879 }
5880 else
628fe4e4 5881 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5882 break;
5883 case bp_watchpoint:
5884 case bp_hardware_watchpoint:
5885 case bp_read_watchpoint:
5886 case bp_access_watchpoint:
5887 if (bs->stop)
5888 {
5889 if (bs->print)
628fe4e4 5890 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5891 else
628fe4e4 5892 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5893 }
5894 else
628fe4e4
JK
5895 {
5896 /* There was a watchpoint, but we're not stopping.
5897 This requires no further action. */
5898 }
c906108c
SS
5899 break;
5900 case bp_longjmp:
e2e4d78b 5901 case bp_longjmp_call_dummy:
186c406b 5902 case bp_exception:
0a39bb32
PA
5903 if (bs->stop)
5904 {
5905 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5906 retval.is_longjmp = bptype != bp_exception;
5907 }
5908 else
5909 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5910 break;
5911 case bp_longjmp_resume:
186c406b 5912 case bp_exception_resume:
0a39bb32
PA
5913 if (bs->stop)
5914 {
5915 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5916 retval.is_longjmp = bptype == bp_longjmp_resume;
5917 }
5918 else
5919 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5920 break;
5921 case bp_step_resume:
5922 if (bs->stop)
628fe4e4
JK
5923 this_action = BPSTAT_WHAT_STEP_RESUME;
5924 else
c906108c 5925 {
628fe4e4
JK
5926 /* It is for the wrong frame. */
5927 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5928 }
c906108c 5929 break;
2c03e5be
PA
5930 case bp_hp_step_resume:
5931 if (bs->stop)
5932 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5933 else
5934 {
5935 /* It is for the wrong frame. */
5936 this_action = BPSTAT_WHAT_SINGLE;
5937 }
5938 break;
c906108c 5939 case bp_watchpoint_scope:
c4093a6a 5940 case bp_thread_event:
1900040c 5941 case bp_overlay_event:
0fd8e87f 5942 case bp_longjmp_master:
aa7d318d 5943 case bp_std_terminate_master:
186c406b 5944 case bp_exception_master:
628fe4e4 5945 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5946 break;
ce78b96d 5947 case bp_catchpoint:
c5aa993b
JM
5948 if (bs->stop)
5949 {
5950 if (bs->print)
628fe4e4 5951 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5952 else
628fe4e4 5953 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5954 }
5955 else
628fe4e4
JK
5956 {
5957 /* There was a catchpoint, but we're not stopping.
5958 This requires no further action. */
5959 }
5960 break;
628fe4e4 5961 case bp_jit_event:
628fe4e4 5962 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5963 break;
c906108c 5964 case bp_call_dummy:
53a5351d
JM
5965 /* Make sure the action is stop (silent or noisy),
5966 so infrun.c pops the dummy frame. */
aa7d318d 5967 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5968 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5969 break;
5970 case bp_std_terminate:
5971 /* Make sure the action is stop (silent or noisy),
5972 so infrun.c pops the dummy frame. */
aa7d318d 5973 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5974 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5975 break;
1042e4c0 5976 case bp_tracepoint:
7a697b8d 5977 case bp_fast_tracepoint:
0fb4aa4b 5978 case bp_static_tracepoint:
1042e4c0
SS
5979 /* Tracepoint hits should not be reported back to GDB, and
5980 if one got through somehow, it should have been filtered
5981 out already. */
5982 internal_error (__FILE__, __LINE__,
7a697b8d 5983 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5984 break;
5985 case bp_gnu_ifunc_resolver:
5986 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5987 this_action = BPSTAT_WHAT_SINGLE;
5988 break;
5989 case bp_gnu_ifunc_resolver_return:
5990 /* The breakpoint will be removed, execution will restart from the
5991 PC of the former breakpoint. */
5992 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5993 break;
e7e0cddf
SS
5994
5995 case bp_dprintf:
a11cfd87
HZ
5996 if (bs->stop)
5997 this_action = BPSTAT_WHAT_STOP_SILENT;
5998 else
5999 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
6000 break;
6001
628fe4e4
JK
6002 default:
6003 internal_error (__FILE__, __LINE__,
6004 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 6005 }
628fe4e4 6006
325fac50 6007 retval.main_action = std::max (retval.main_action, this_action);
c906108c 6008 }
628fe4e4 6009
243a9253
PA
6010 return retval;
6011}
628fe4e4 6012
243a9253
PA
6013void
6014bpstat_run_callbacks (bpstat bs_head)
6015{
6016 bpstat bs;
628fe4e4 6017
0e30163f
JK
6018 for (bs = bs_head; bs != NULL; bs = bs->next)
6019 {
6020 struct breakpoint *b = bs->breakpoint_at;
6021
6022 if (b == NULL)
6023 continue;
6024 switch (b->type)
6025 {
243a9253
PA
6026 case bp_jit_event:
6027 handle_jit_event ();
6028 break;
0e30163f
JK
6029 case bp_gnu_ifunc_resolver:
6030 gnu_ifunc_resolver_stop (b);
6031 break;
6032 case bp_gnu_ifunc_resolver_return:
6033 gnu_ifunc_resolver_return_stop (b);
6034 break;
6035 }
6036 }
c906108c
SS
6037}
6038
6039/* Nonzero if we should step constantly (e.g. watchpoints on machines
6040 without hardware support). This isn't related to a specific bpstat,
6041 just to things like whether watchpoints are set. */
6042
c5aa993b 6043int
fba45db2 6044bpstat_should_step (void)
c906108c
SS
6045{
6046 struct breakpoint *b;
cc59ec59 6047
c906108c 6048 ALL_BREAKPOINTS (b)
717a8278 6049 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6050 return 1;
c906108c
SS
6051 return 0;
6052}
6053
67822962
PA
6054int
6055bpstat_causes_stop (bpstat bs)
6056{
6057 for (; bs != NULL; bs = bs->next)
6058 if (bs->stop)
6059 return 1;
6060
6061 return 0;
6062}
6063
c906108c 6064\f
c5aa993b 6065
170b53b2
UW
6066/* Compute a string of spaces suitable to indent the next line
6067 so it starts at the position corresponding to the table column
6068 named COL_NAME in the currently active table of UIOUT. */
6069
6070static char *
6071wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6072{
6073 static char wrap_indent[80];
6074 int i, total_width, width, align;
6075 char *text;
6076
6077 total_width = 0;
6078 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6079 {
6080 if (strcmp (text, col_name) == 0)
6081 {
6082 gdb_assert (total_width < sizeof wrap_indent);
6083 memset (wrap_indent, ' ', total_width);
6084 wrap_indent[total_width] = 0;
6085
6086 return wrap_indent;
6087 }
6088
6089 total_width += width + 1;
6090 }
6091
6092 return NULL;
6093}
6094
b775012e
LM
6095/* Determine if the locations of this breakpoint will have their conditions
6096 evaluated by the target, host or a mix of both. Returns the following:
6097
6098 "host": Host evals condition.
6099 "host or target": Host or Target evals condition.
6100 "target": Target evals condition.
6101*/
6102
6103static const char *
6104bp_condition_evaluator (struct breakpoint *b)
6105{
6106 struct bp_location *bl;
6107 char host_evals = 0;
6108 char target_evals = 0;
6109
6110 if (!b)
6111 return NULL;
6112
6113 if (!is_breakpoint (b))
6114 return NULL;
6115
6116 if (gdb_evaluates_breakpoint_condition_p ()
6117 || !target_supports_evaluation_of_breakpoint_conditions ())
6118 return condition_evaluation_host;
6119
6120 for (bl = b->loc; bl; bl = bl->next)
6121 {
6122 if (bl->cond_bytecode)
6123 target_evals++;
6124 else
6125 host_evals++;
6126 }
6127
6128 if (host_evals && target_evals)
6129 return condition_evaluation_both;
6130 else if (target_evals)
6131 return condition_evaluation_target;
6132 else
6133 return condition_evaluation_host;
6134}
6135
6136/* Determine the breakpoint location's condition evaluator. This is
6137 similar to bp_condition_evaluator, but for locations. */
6138
6139static const char *
6140bp_location_condition_evaluator (struct bp_location *bl)
6141{
6142 if (bl && !is_breakpoint (bl->owner))
6143 return NULL;
6144
6145 if (gdb_evaluates_breakpoint_condition_p ()
6146 || !target_supports_evaluation_of_breakpoint_conditions ())
6147 return condition_evaluation_host;
6148
6149 if (bl && bl->cond_bytecode)
6150 return condition_evaluation_target;
6151 else
6152 return condition_evaluation_host;
6153}
6154
859825b8
JK
6155/* Print the LOC location out of the list of B->LOC locations. */
6156
170b53b2
UW
6157static void
6158print_breakpoint_location (struct breakpoint *b,
6159 struct bp_location *loc)
0d381245 6160{
79a45e25 6161 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6162 struct cleanup *old_chain = save_current_program_space ();
6163
859825b8
JK
6164 if (loc != NULL && loc->shlib_disabled)
6165 loc = NULL;
6166
6c95b8df
PA
6167 if (loc != NULL)
6168 set_current_program_space (loc->pspace);
6169
56435ebe 6170 if (b->display_canonical)
f00aae0f
KS
6171 ui_out_field_string (uiout, "what",
6172 event_location_to_string (b->location));
2f202fde 6173 else if (loc && loc->symtab)
0d381245
VP
6174 {
6175 struct symbol *sym
6176 = find_pc_sect_function (loc->address, loc->section);
6177 if (sym)
6178 {
6179 ui_out_text (uiout, "in ");
6180 ui_out_field_string (uiout, "func",
6181 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6182 ui_out_text (uiout, " ");
6183 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6184 ui_out_text (uiout, "at ");
0d381245 6185 }
05cba821
JK
6186 ui_out_field_string (uiout, "file",
6187 symtab_to_filename_for_display (loc->symtab));
0d381245 6188 ui_out_text (uiout, ":");
05cba821 6189
0d381245 6190 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6191 ui_out_field_string (uiout, "fullname",
6192 symtab_to_fullname (loc->symtab));
0d381245 6193
f8eba3c6 6194 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6195 }
859825b8 6196 else if (loc)
0d381245 6197 {
f99d8bf4
PA
6198 struct ui_file *stb = mem_fileopen ();
6199 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6200
f99d8bf4 6201 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6202 demangle, "");
0d381245 6203 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6204
6205 do_cleanups (stb_chain);
0d381245 6206 }
859825b8 6207 else
f00aae0f
KS
6208 {
6209 ui_out_field_string (uiout, "pending",
6210 event_location_to_string (b->location));
6211 /* If extra_string is available, it could be holding a condition
6212 or dprintf arguments. In either case, make sure it is printed,
6213 too, but only for non-MI streams. */
6214 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6215 {
6216 if (b->type == bp_dprintf)
6217 ui_out_text (uiout, ",");
6218 else
6219 ui_out_text (uiout, " ");
6220 ui_out_text (uiout, b->extra_string);
6221 }
6222 }
6c95b8df 6223
b775012e
LM
6224 if (loc && is_breakpoint (b)
6225 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6226 && bp_condition_evaluator (b) == condition_evaluation_both)
6227 {
6228 ui_out_text (uiout, " (");
6229 ui_out_field_string (uiout, "evaluated-by",
6230 bp_location_condition_evaluator (loc));
6231 ui_out_text (uiout, ")");
6232 }
6233
6c95b8df 6234 do_cleanups (old_chain);
0d381245
VP
6235}
6236
269b11a2
PA
6237static const char *
6238bptype_string (enum bptype type)
c906108c 6239{
c4093a6a
JM
6240 struct ep_type_description
6241 {
6242 enum bptype type;
6243 char *description;
6244 };
6245 static struct ep_type_description bptypes[] =
c906108c 6246 {
c5aa993b
JM
6247 {bp_none, "?deleted?"},
6248 {bp_breakpoint, "breakpoint"},
c906108c 6249 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6250 {bp_single_step, "sw single-step"},
c5aa993b
JM
6251 {bp_until, "until"},
6252 {bp_finish, "finish"},
6253 {bp_watchpoint, "watchpoint"},
c906108c 6254 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6255 {bp_read_watchpoint, "read watchpoint"},
6256 {bp_access_watchpoint, "acc watchpoint"},
6257 {bp_longjmp, "longjmp"},
6258 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6259 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6260 {bp_exception, "exception"},
6261 {bp_exception_resume, "exception resume"},
c5aa993b 6262 {bp_step_resume, "step resume"},
2c03e5be 6263 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6264 {bp_watchpoint_scope, "watchpoint scope"},
6265 {bp_call_dummy, "call dummy"},
aa7d318d 6266 {bp_std_terminate, "std::terminate"},
c5aa993b 6267 {bp_shlib_event, "shlib events"},
c4093a6a 6268 {bp_thread_event, "thread events"},
1900040c 6269 {bp_overlay_event, "overlay events"},
0fd8e87f 6270 {bp_longjmp_master, "longjmp master"},
aa7d318d 6271 {bp_std_terminate_master, "std::terminate master"},
186c406b 6272 {bp_exception_master, "exception master"},
ce78b96d 6273 {bp_catchpoint, "catchpoint"},
1042e4c0 6274 {bp_tracepoint, "tracepoint"},
7a697b8d 6275 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6276 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6277 {bp_dprintf, "dprintf"},
4efc6507 6278 {bp_jit_event, "jit events"},
0e30163f
JK
6279 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6280 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6281 };
269b11a2
PA
6282
6283 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6284 || ((int) type != bptypes[(int) type].type))
6285 internal_error (__FILE__, __LINE__,
6286 _("bptypes table does not describe type #%d."),
6287 (int) type);
6288
6289 return bptypes[(int) type].description;
6290}
6291
998580f1
MK
6292/* For MI, output a field named 'thread-groups' with a list as the value.
6293 For CLI, prefix the list with the string 'inf'. */
6294
6295static void
6296output_thread_groups (struct ui_out *uiout,
6297 const char *field_name,
6298 VEC(int) *inf_num,
6299 int mi_only)
6300{
752eb8b4 6301 struct cleanup *back_to;
998580f1
MK
6302 int is_mi = ui_out_is_mi_like_p (uiout);
6303 int inf;
6304 int i;
6305
6306 /* For backward compatibility, don't display inferiors in CLI unless
6307 there are several. Always display them for MI. */
6308 if (!is_mi && mi_only)
6309 return;
6310
752eb8b4
TT
6311 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6312
998580f1
MK
6313 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6314 {
6315 if (is_mi)
6316 {
6317 char mi_group[10];
6318
6319 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6320 ui_out_field_string (uiout, NULL, mi_group);
6321 }
6322 else
6323 {
6324 if (i == 0)
6325 ui_out_text (uiout, " inf ");
6326 else
6327 ui_out_text (uiout, ", ");
6328
6329 ui_out_text (uiout, plongest (inf));
6330 }
6331 }
6332
6333 do_cleanups (back_to);
6334}
6335
269b11a2
PA
6336/* Print B to gdb_stdout. */
6337
6338static void
6339print_one_breakpoint_location (struct breakpoint *b,
6340 struct bp_location *loc,
6341 int loc_number,
6342 struct bp_location **last_loc,
269b11a2
PA
6343 int allflag)
6344{
6345 struct command_line *l;
c2c6d25f 6346 static char bpenables[] = "nynny";
c906108c 6347
79a45e25 6348 struct ui_out *uiout = current_uiout;
0d381245
VP
6349 int header_of_multiple = 0;
6350 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6351 struct value_print_options opts;
6352
6353 get_user_print_options (&opts);
0d381245
VP
6354
6355 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6356 /* See comment in print_one_breakpoint concerning treatment of
6357 breakpoints with single disabled location. */
0d381245
VP
6358 if (loc == NULL
6359 && (b->loc != NULL
6360 && (b->loc->next != NULL || !b->loc->enabled)))
6361 header_of_multiple = 1;
6362 if (loc == NULL)
6363 loc = b->loc;
6364
c4093a6a
JM
6365 annotate_record ();
6366
6367 /* 1 */
6368 annotate_field (0);
0d381245
VP
6369 if (part_of_multiple)
6370 {
6371 char *formatted;
0c6773c1 6372 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6373 ui_out_field_string (uiout, "number", formatted);
6374 xfree (formatted);
6375 }
6376 else
6377 {
6378 ui_out_field_int (uiout, "number", b->number);
6379 }
c4093a6a
JM
6380
6381 /* 2 */
6382 annotate_field (1);
0d381245
VP
6383 if (part_of_multiple)
6384 ui_out_field_skip (uiout, "type");
269b11a2
PA
6385 else
6386 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6387
6388 /* 3 */
6389 annotate_field (2);
0d381245
VP
6390 if (part_of_multiple)
6391 ui_out_field_skip (uiout, "disp");
6392 else
2cec12e5 6393 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6394
c4093a6a
JM
6395
6396 /* 4 */
6397 annotate_field (3);
0d381245 6398 if (part_of_multiple)
54e52265 6399 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6400 else
4a64f543
MS
6401 ui_out_field_fmt (uiout, "enabled", "%c",
6402 bpenables[(int) b->enable_state]);
54e52265 6403 ui_out_spaces (uiout, 2);
0d381245 6404
c4093a6a
JM
6405
6406 /* 5 and 6 */
3086aeae 6407 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6408 {
4a64f543
MS
6409 /* Although the print_one can possibly print all locations,
6410 calling it here is not likely to get any nice result. So,
6411 make sure there's just one location. */
0d381245 6412 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6413 b->ops->print_one (b, last_loc);
0d381245 6414 }
3086aeae
DJ
6415 else
6416 switch (b->type)
6417 {
6418 case bp_none:
6419 internal_error (__FILE__, __LINE__,
e2e0b3e5 6420 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6421 break;
c906108c 6422
3086aeae
DJ
6423 case bp_watchpoint:
6424 case bp_hardware_watchpoint:
6425 case bp_read_watchpoint:
6426 case bp_access_watchpoint:
3a5c3e22
PA
6427 {
6428 struct watchpoint *w = (struct watchpoint *) b;
6429
6430 /* Field 4, the address, is omitted (which makes the columns
6431 not line up too nicely with the headers, but the effect
6432 is relatively readable). */
6433 if (opts.addressprint)
6434 ui_out_field_skip (uiout, "addr");
6435 annotate_field (5);
6436 ui_out_field_string (uiout, "what", w->exp_string);
6437 }
3086aeae
DJ
6438 break;
6439
3086aeae
DJ
6440 case bp_breakpoint:
6441 case bp_hardware_breakpoint:
7c16b83e 6442 case bp_single_step:
3086aeae
DJ
6443 case bp_until:
6444 case bp_finish:
6445 case bp_longjmp:
6446 case bp_longjmp_resume:
e2e4d78b 6447 case bp_longjmp_call_dummy:
186c406b
TT
6448 case bp_exception:
6449 case bp_exception_resume:
3086aeae 6450 case bp_step_resume:
2c03e5be 6451 case bp_hp_step_resume:
3086aeae
DJ
6452 case bp_watchpoint_scope:
6453 case bp_call_dummy:
aa7d318d 6454 case bp_std_terminate:
3086aeae
DJ
6455 case bp_shlib_event:
6456 case bp_thread_event:
6457 case bp_overlay_event:
0fd8e87f 6458 case bp_longjmp_master:
aa7d318d 6459 case bp_std_terminate_master:
186c406b 6460 case bp_exception_master:
1042e4c0 6461 case bp_tracepoint:
7a697b8d 6462 case bp_fast_tracepoint:
0fb4aa4b 6463 case bp_static_tracepoint:
e7e0cddf 6464 case bp_dprintf:
4efc6507 6465 case bp_jit_event:
0e30163f
JK
6466 case bp_gnu_ifunc_resolver:
6467 case bp_gnu_ifunc_resolver_return:
79a45b7d 6468 if (opts.addressprint)
3086aeae
DJ
6469 {
6470 annotate_field (4);
54e52265 6471 if (header_of_multiple)
0d381245 6472 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6473 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6474 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6475 else
5af949e3
UW
6476 ui_out_field_core_addr (uiout, "addr",
6477 loc->gdbarch, loc->address);
3086aeae
DJ
6478 }
6479 annotate_field (5);
0d381245 6480 if (!header_of_multiple)
170b53b2 6481 print_breakpoint_location (b, loc);
0d381245 6482 if (b->loc)
a6d9a66e 6483 *last_loc = b->loc;
3086aeae
DJ
6484 break;
6485 }
c906108c 6486
6c95b8df 6487
998580f1 6488 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6489 {
6490 struct inferior *inf;
998580f1
MK
6491 VEC(int) *inf_num = NULL;
6492 int mi_only = 1;
6c95b8df 6493
998580f1 6494 ALL_INFERIORS (inf)
6c95b8df
PA
6495 {
6496 if (inf->pspace == loc->pspace)
998580f1 6497 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6498 }
998580f1
MK
6499
6500 /* For backward compatibility, don't display inferiors in CLI unless
6501 there are several. Always display for MI. */
6502 if (allflag
6503 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6504 && (number_of_program_spaces () > 1
6505 || number_of_inferiors () > 1)
6506 /* LOC is for existing B, it cannot be in
6507 moribund_locations and thus having NULL OWNER. */
6508 && loc->owner->type != bp_catchpoint))
6509 mi_only = 0;
6510 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6511 VEC_free (int, inf_num);
6c95b8df
PA
6512 }
6513
4a306c9a 6514 if (!part_of_multiple)
c4093a6a 6515 {
4a306c9a
JB
6516 if (b->thread != -1)
6517 {
6518 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6519 "stop only in" line a little further down. */
4a306c9a
JB
6520 ui_out_text (uiout, " thread ");
6521 ui_out_field_int (uiout, "thread", b->thread);
6522 }
6523 else if (b->task != 0)
6524 {
6525 ui_out_text (uiout, " task ");
6526 ui_out_field_int (uiout, "task", b->task);
6527 }
c4093a6a 6528 }
f1310107 6529
8b93c638 6530 ui_out_text (uiout, "\n");
f1310107 6531
348d480f 6532 if (!part_of_multiple)
f1310107
TJB
6533 b->ops->print_one_detail (b, uiout);
6534
0d381245 6535 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6536 {
6537 annotate_field (6);
8b93c638 6538 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6539 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6540 the frame ID. */
5af949e3
UW
6541 ui_out_field_core_addr (uiout, "frame",
6542 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6543 ui_out_text (uiout, "\n");
c4093a6a
JM
6544 }
6545
28010a5d 6546 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6547 {
6548 annotate_field (7);
d77f58be 6549 if (is_tracepoint (b))
1042e4c0
SS
6550 ui_out_text (uiout, "\ttrace only if ");
6551 else
6552 ui_out_text (uiout, "\tstop only if ");
0101ce28 6553 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6554
6555 /* Print whether the target is doing the breakpoint's condition
6556 evaluation. If GDB is doing the evaluation, don't print anything. */
6557 if (is_breakpoint (b)
6558 && breakpoint_condition_evaluation_mode ()
6559 == condition_evaluation_target)
6560 {
6561 ui_out_text (uiout, " (");
6562 ui_out_field_string (uiout, "evaluated-by",
6563 bp_condition_evaluator (b));
6564 ui_out_text (uiout, " evals)");
6565 }
0101ce28
JJ
6566 ui_out_text (uiout, "\n");
6567 }
6568
0d381245 6569 if (!part_of_multiple && b->thread != -1)
c4093a6a 6570 {
4a64f543 6571 /* FIXME should make an annotation for this. */
8b93c638 6572 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6573 if (ui_out_is_mi_like_p (uiout))
6574 ui_out_field_int (uiout, "thread", b->thread);
6575 else
6576 {
6577 struct thread_info *thr = find_thread_global_id (b->thread);
6578
6579 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6580 }
8b93c638 6581 ui_out_text (uiout, "\n");
c4093a6a
JM
6582 }
6583
556ec64d
YQ
6584 if (!part_of_multiple)
6585 {
6586 if (b->hit_count)
31f56a27
YQ
6587 {
6588 /* FIXME should make an annotation for this. */
6589 if (is_catchpoint (b))
6590 ui_out_text (uiout, "\tcatchpoint");
6591 else if (is_tracepoint (b))
6592 ui_out_text (uiout, "\ttracepoint");
6593 else
6594 ui_out_text (uiout, "\tbreakpoint");
6595 ui_out_text (uiout, " already hit ");
6596 ui_out_field_int (uiout, "times", b->hit_count);
6597 if (b->hit_count == 1)
6598 ui_out_text (uiout, " time\n");
6599 else
6600 ui_out_text (uiout, " times\n");
6601 }
556ec64d
YQ
6602 else
6603 {
31f56a27
YQ
6604 /* Output the count also if it is zero, but only if this is mi. */
6605 if (ui_out_is_mi_like_p (uiout))
6606 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6607 }
6608 }
8b93c638 6609
0d381245 6610 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6611 {
6612 annotate_field (8);
8b93c638
JM
6613 ui_out_text (uiout, "\tignore next ");
6614 ui_out_field_int (uiout, "ignore", b->ignore_count);
6615 ui_out_text (uiout, " hits\n");
c4093a6a 6616 }
059fb39f 6617
816338b5
SS
6618 /* Note that an enable count of 1 corresponds to "enable once"
6619 behavior, which is reported by the combination of enablement and
6620 disposition, so we don't need to mention it here. */
6621 if (!part_of_multiple && b->enable_count > 1)
6622 {
6623 annotate_field (8);
6624 ui_out_text (uiout, "\tdisable after ");
6625 /* Tweak the wording to clarify that ignore and enable counts
6626 are distinct, and have additive effect. */
6627 if (b->ignore_count)
6628 ui_out_text (uiout, "additional ");
6629 else
6630 ui_out_text (uiout, "next ");
6631 ui_out_field_int (uiout, "enable", b->enable_count);
6632 ui_out_text (uiout, " hits\n");
6633 }
6634
f196051f
SS
6635 if (!part_of_multiple && is_tracepoint (b))
6636 {
6637 struct tracepoint *tp = (struct tracepoint *) b;
6638
6639 if (tp->traceframe_usage)
6640 {
6641 ui_out_text (uiout, "\ttrace buffer usage ");
6642 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6643 ui_out_text (uiout, " bytes\n");
6644 }
6645 }
d3ce09f5 6646
9add0f1b 6647 l = b->commands ? b->commands->commands : NULL;
059fb39f 6648 if (!part_of_multiple && l)
c4093a6a 6649 {
3b31d625
EZ
6650 struct cleanup *script_chain;
6651
c4093a6a 6652 annotate_field (9);
3b31d625 6653 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6654 print_command_lines (uiout, l, 4);
3b31d625 6655 do_cleanups (script_chain);
c4093a6a 6656 }
d24317b4 6657
d9b3f62e 6658 if (is_tracepoint (b))
1042e4c0 6659 {
d9b3f62e
PA
6660 struct tracepoint *t = (struct tracepoint *) b;
6661
6662 if (!part_of_multiple && t->pass_count)
6663 {
6664 annotate_field (10);
6665 ui_out_text (uiout, "\tpass count ");
6666 ui_out_field_int (uiout, "pass", t->pass_count);
6667 ui_out_text (uiout, " \n");
6668 }
f2a8bc8a
YQ
6669
6670 /* Don't display it when tracepoint or tracepoint location is
6671 pending. */
6672 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6673 {
6674 annotate_field (11);
6675
6676 if (ui_out_is_mi_like_p (uiout))
6677 ui_out_field_string (uiout, "installed",
6678 loc->inserted ? "y" : "n");
6679 else
6680 {
6681 if (loc->inserted)
6682 ui_out_text (uiout, "\t");
6683 else
6684 ui_out_text (uiout, "\tnot ");
6685 ui_out_text (uiout, "installed on target\n");
6686 }
6687 }
1042e4c0
SS
6688 }
6689
d24317b4
VP
6690 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6691 {
3a5c3e22
PA
6692 if (is_watchpoint (b))
6693 {
6694 struct watchpoint *w = (struct watchpoint *) b;
6695
6696 ui_out_field_string (uiout, "original-location", w->exp_string);
6697 }
f00aae0f
KS
6698 else if (b->location != NULL
6699 && event_location_to_string (b->location) != NULL)
6700 ui_out_field_string (uiout, "original-location",
6701 event_location_to_string (b->location));
d24317b4 6702 }
c4093a6a 6703}
c5aa993b 6704
0d381245
VP
6705static void
6706print_one_breakpoint (struct breakpoint *b,
4a64f543 6707 struct bp_location **last_loc,
6c95b8df 6708 int allflag)
0d381245 6709{
8d3788bd 6710 struct cleanup *bkpt_chain;
79a45e25 6711 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6712
6713 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6714
12c5a436 6715 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6716 do_cleanups (bkpt_chain);
0d381245
VP
6717
6718 /* If this breakpoint has custom print function,
6719 it's already printed. Otherwise, print individual
6720 locations, if any. */
6721 if (b->ops == NULL || b->ops->print_one == NULL)
6722 {
4a64f543
MS
6723 /* If breakpoint has a single location that is disabled, we
6724 print it as if it had several locations, since otherwise it's
6725 hard to represent "breakpoint enabled, location disabled"
6726 situation.
6727
6728 Note that while hardware watchpoints have several locations
a3be7890 6729 internally, that's not a property exposed to user. */
0d381245 6730 if (b->loc
a5606eee 6731 && !is_hardware_watchpoint (b)
8d3788bd 6732 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6733 {
6734 struct bp_location *loc;
6735 int n = 1;
8d3788bd 6736
0d381245 6737 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6738 {
6739 struct cleanup *inner2 =
6740 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6741 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6742 do_cleanups (inner2);
6743 }
0d381245
VP
6744 }
6745 }
6746}
6747
a6d9a66e
UW
6748static int
6749breakpoint_address_bits (struct breakpoint *b)
6750{
6751 int print_address_bits = 0;
6752 struct bp_location *loc;
6753
c6d81124
PA
6754 /* Software watchpoints that aren't watching memory don't have an
6755 address to print. */
6756 if (is_no_memory_software_watchpoint (b))
6757 return 0;
6758
a6d9a66e
UW
6759 for (loc = b->loc; loc; loc = loc->next)
6760 {
c7437ca6
PA
6761 int addr_bit;
6762
c7437ca6 6763 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6764 if (addr_bit > print_address_bits)
6765 print_address_bits = addr_bit;
6766 }
6767
6768 return print_address_bits;
6769}
0d381245 6770
c4093a6a
JM
6771struct captured_breakpoint_query_args
6772 {
6773 int bnum;
6774 };
c5aa993b 6775
c4093a6a 6776static int
2b65245e 6777do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6778{
9a3c8263
SM
6779 struct captured_breakpoint_query_args *args
6780 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6781 struct breakpoint *b;
a6d9a66e 6782 struct bp_location *dummy_loc = NULL;
cc59ec59 6783
c4093a6a
JM
6784 ALL_BREAKPOINTS (b)
6785 {
6786 if (args->bnum == b->number)
c5aa993b 6787 {
12c5a436 6788 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6789 return GDB_RC_OK;
c5aa993b 6790 }
c4093a6a
JM
6791 }
6792 return GDB_RC_NONE;
6793}
c5aa993b 6794
c4093a6a 6795enum gdb_rc
4a64f543
MS
6796gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6797 char **error_message)
c4093a6a
JM
6798{
6799 struct captured_breakpoint_query_args args;
cc59ec59 6800
c4093a6a
JM
6801 args.bnum = bnum;
6802 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6803 an error. */
b0b13bb4
DJ
6804 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6805 error_message, RETURN_MASK_ALL) < 0)
6806 return GDB_RC_FAIL;
6807 else
6808 return GDB_RC_OK;
c4093a6a 6809}
c5aa993b 6810
09d682a4
TT
6811/* Return true if this breakpoint was set by the user, false if it is
6812 internal or momentary. */
6813
6814int
6815user_breakpoint_p (struct breakpoint *b)
6816{
46c6471b 6817 return b->number > 0;
09d682a4
TT
6818}
6819
93daf339
TT
6820/* See breakpoint.h. */
6821
6822int
6823pending_breakpoint_p (struct breakpoint *b)
6824{
6825 return b->loc == NULL;
6826}
6827
7f3b0473 6828/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6829 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6830 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6831 FILTER is non-NULL, call it on each breakpoint and only include the
6832 ones for which it returns non-zero. Return the total number of
6833 breakpoints listed. */
c906108c 6834
d77f58be 6835static int
e5a67952 6836breakpoint_1 (char *args, int allflag,
4a64f543 6837 int (*filter) (const struct breakpoint *))
c4093a6a 6838{
52f0bd74 6839 struct breakpoint *b;
a6d9a66e 6840 struct bp_location *last_loc = NULL;
7f3b0473 6841 int nr_printable_breakpoints;
3b31d625 6842 struct cleanup *bkpttbl_chain;
79a45b7d 6843 struct value_print_options opts;
a6d9a66e 6844 int print_address_bits = 0;
269b11a2 6845 int print_type_col_width = 14;
79a45e25 6846 struct ui_out *uiout = current_uiout;
269b11a2 6847
79a45b7d
TT
6848 get_user_print_options (&opts);
6849
4a64f543
MS
6850 /* Compute the number of rows in the table, as well as the size
6851 required for address fields. */
7f3b0473
AC
6852 nr_printable_breakpoints = 0;
6853 ALL_BREAKPOINTS (b)
e5a67952
MS
6854 {
6855 /* If we have a filter, only list the breakpoints it accepts. */
6856 if (filter && !filter (b))
6857 continue;
6858
6859 /* If we have an "args" string, it is a list of breakpoints to
6860 accept. Skip the others. */
6861 if (args != NULL && *args != '\0')
6862 {
6863 if (allflag && parse_and_eval_long (args) != b->number)
6864 continue;
6865 if (!allflag && !number_is_in_list (args, b->number))
6866 continue;
6867 }
269b11a2 6868
e5a67952
MS
6869 if (allflag || user_breakpoint_p (b))
6870 {
6871 int addr_bit, type_len;
a6d9a66e 6872
e5a67952
MS
6873 addr_bit = breakpoint_address_bits (b);
6874 if (addr_bit > print_address_bits)
6875 print_address_bits = addr_bit;
269b11a2 6876
e5a67952
MS
6877 type_len = strlen (bptype_string (b->type));
6878 if (type_len > print_type_col_width)
6879 print_type_col_width = type_len;
6880
6881 nr_printable_breakpoints++;
6882 }
6883 }
7f3b0473 6884
79a45b7d 6885 if (opts.addressprint)
3b31d625 6886 bkpttbl_chain
3e43a32a
MS
6887 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6888 nr_printable_breakpoints,
3b31d625 6889 "BreakpointTable");
8b93c638 6890 else
3b31d625 6891 bkpttbl_chain
3e43a32a
MS
6892 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6893 nr_printable_breakpoints,
3b31d625 6894 "BreakpointTable");
8b93c638 6895
7f3b0473 6896 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6897 annotate_breakpoints_headers ();
6898 if (nr_printable_breakpoints > 0)
6899 annotate_field (0);
4a64f543 6900 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6901 if (nr_printable_breakpoints > 0)
6902 annotate_field (1);
269b11a2 6903 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6904 "type", "Type"); /* 2 */
d7faa9e7
AC
6905 if (nr_printable_breakpoints > 0)
6906 annotate_field (2);
4a64f543 6907 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6908 if (nr_printable_breakpoints > 0)
6909 annotate_field (3);
54e52265 6910 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6911 if (opts.addressprint)
e5a67952
MS
6912 {
6913 if (nr_printable_breakpoints > 0)
6914 annotate_field (4);
6915 if (print_address_bits <= 32)
6916 ui_out_table_header (uiout, 10, ui_left,
6917 "addr", "Address"); /* 5 */
6918 else
6919 ui_out_table_header (uiout, 18, ui_left,
6920 "addr", "Address"); /* 5 */
6921 }
d7faa9e7
AC
6922 if (nr_printable_breakpoints > 0)
6923 annotate_field (5);
6924 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6925 ui_out_table_body (uiout);
6926 if (nr_printable_breakpoints > 0)
6927 annotate_breakpoints_table ();
7f3b0473 6928
c4093a6a 6929 ALL_BREAKPOINTS (b)
e5a67952
MS
6930 {
6931 QUIT;
6932 /* If we have a filter, only list the breakpoints it accepts. */
6933 if (filter && !filter (b))
6934 continue;
6935
6936 /* If we have an "args" string, it is a list of breakpoints to
6937 accept. Skip the others. */
6938
6939 if (args != NULL && *args != '\0')
6940 {
6941 if (allflag) /* maintenance info breakpoint */
6942 {
6943 if (parse_and_eval_long (args) != b->number)
6944 continue;
6945 }
6946 else /* all others */
6947 {
6948 if (!number_is_in_list (args, b->number))
6949 continue;
6950 }
6951 }
6952 /* We only print out user settable breakpoints unless the
6953 allflag is set. */
6954 if (allflag || user_breakpoint_p (b))
12c5a436 6955 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6956 }
6957
3b31d625 6958 do_cleanups (bkpttbl_chain);
698384cd 6959
7f3b0473 6960 if (nr_printable_breakpoints == 0)
c906108c 6961 {
4a64f543
MS
6962 /* If there's a filter, let the caller decide how to report
6963 empty list. */
d77f58be
SS
6964 if (!filter)
6965 {
e5a67952 6966 if (args == NULL || *args == '\0')
d77f58be
SS
6967 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6968 else
4a64f543 6969 ui_out_message (uiout, 0,
e5a67952
MS
6970 "No breakpoint or watchpoint matching '%s'.\n",
6971 args);
d77f58be 6972 }
c906108c
SS
6973 }
6974 else
c4093a6a 6975 {
a6d9a66e
UW
6976 if (last_loc && !server_command)
6977 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6978 }
c906108c 6979
4a64f543 6980 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6981 there have been breakpoints? */
c906108c 6982 annotate_breakpoints_table_end ();
d77f58be
SS
6983
6984 return nr_printable_breakpoints;
c906108c
SS
6985}
6986
ad443146
SS
6987/* Display the value of default-collect in a way that is generally
6988 compatible with the breakpoint list. */
6989
6990static void
6991default_collect_info (void)
6992{
79a45e25
PA
6993 struct ui_out *uiout = current_uiout;
6994
ad443146
SS
6995 /* If it has no value (which is frequently the case), say nothing; a
6996 message like "No default-collect." gets in user's face when it's
6997 not wanted. */
6998 if (!*default_collect)
6999 return;
7000
7001 /* The following phrase lines up nicely with per-tracepoint collect
7002 actions. */
7003 ui_out_text (uiout, "default collect ");
7004 ui_out_field_string (uiout, "default-collect", default_collect);
7005 ui_out_text (uiout, " \n");
7006}
7007
c906108c 7008static void
e5a67952 7009breakpoints_info (char *args, int from_tty)
c906108c 7010{
e5a67952 7011 breakpoint_1 (args, 0, NULL);
ad443146
SS
7012
7013 default_collect_info ();
d77f58be
SS
7014}
7015
7016static void
e5a67952 7017watchpoints_info (char *args, int from_tty)
d77f58be 7018{
e5a67952 7019 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 7020 struct ui_out *uiout = current_uiout;
d77f58be
SS
7021
7022 if (num_printed == 0)
7023 {
e5a67952 7024 if (args == NULL || *args == '\0')
d77f58be
SS
7025 ui_out_message (uiout, 0, "No watchpoints.\n");
7026 else
e5a67952 7027 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 7028 }
c906108c
SS
7029}
7030
7a292a7a 7031static void
e5a67952 7032maintenance_info_breakpoints (char *args, int from_tty)
c906108c 7033{
e5a67952 7034 breakpoint_1 (args, 1, NULL);
ad443146
SS
7035
7036 default_collect_info ();
c906108c
SS
7037}
7038
0d381245 7039static int
714835d5 7040breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7041 struct program_space *pspace,
714835d5 7042 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7043{
7044 struct bp_location *bl = b->loc;
cc59ec59 7045
0d381245
VP
7046 for (; bl; bl = bl->next)
7047 {
6c95b8df
PA
7048 if (bl->pspace == pspace
7049 && bl->address == pc
0d381245
VP
7050 && (!overlay_debugging || bl->section == section))
7051 return 1;
7052 }
7053 return 0;
7054}
7055
672f9b60 7056/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7057 concerns with logical breakpoints, so we match program spaces, not
7058 address spaces. */
c906108c
SS
7059
7060static void
6c95b8df
PA
7061describe_other_breakpoints (struct gdbarch *gdbarch,
7062 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7063 struct obj_section *section, int thread)
c906108c 7064{
52f0bd74
AC
7065 int others = 0;
7066 struct breakpoint *b;
c906108c
SS
7067
7068 ALL_BREAKPOINTS (b)
672f9b60
KP
7069 others += (user_breakpoint_p (b)
7070 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7071 if (others > 0)
7072 {
a3f17187
AC
7073 if (others == 1)
7074 printf_filtered (_("Note: breakpoint "));
7075 else /* if (others == ???) */
7076 printf_filtered (_("Note: breakpoints "));
c906108c 7077 ALL_BREAKPOINTS (b)
672f9b60 7078 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7079 {
7080 others--;
7081 printf_filtered ("%d", b->number);
7082 if (b->thread == -1 && thread != -1)
7083 printf_filtered (" (all threads)");
7084 else if (b->thread != -1)
7085 printf_filtered (" (thread %d)", b->thread);
7086 printf_filtered ("%s%s ",
059fb39f 7087 ((b->enable_state == bp_disabled
f8eba3c6 7088 || b->enable_state == bp_call_disabled)
0d381245 7089 ? " (disabled)"
0d381245
VP
7090 : ""),
7091 (others > 1) ? ","
7092 : ((others == 1) ? " and" : ""));
7093 }
a3f17187 7094 printf_filtered (_("also set at pc "));
5af949e3 7095 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7096 printf_filtered (".\n");
7097 }
7098}
7099\f
c906108c 7100
e4f237da 7101/* Return true iff it is meaningful to use the address member of
244558af
LM
7102 BPT locations. For some breakpoint types, the locations' address members
7103 are irrelevant and it makes no sense to attempt to compare them to other
7104 addresses (or use them for any other purpose either).
e4f237da 7105
4a64f543 7106 More specifically, each of the following breakpoint types will
244558af 7107 always have a zero valued location address and we don't want to mark
4a64f543 7108 breakpoints of any of these types to be a duplicate of an actual
244558af 7109 breakpoint location at address zero:
e4f237da
KB
7110
7111 bp_watchpoint
2d134ed3
PA
7112 bp_catchpoint
7113
7114*/
e4f237da
KB
7115
7116static int
7117breakpoint_address_is_meaningful (struct breakpoint *bpt)
7118{
7119 enum bptype type = bpt->type;
7120
2d134ed3
PA
7121 return (type != bp_watchpoint && type != bp_catchpoint);
7122}
7123
7124/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7125 true if LOC1 and LOC2 represent the same watchpoint location. */
7126
7127static int
4a64f543
MS
7128watchpoint_locations_match (struct bp_location *loc1,
7129 struct bp_location *loc2)
2d134ed3 7130{
3a5c3e22
PA
7131 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7132 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7133
7134 /* Both of them must exist. */
7135 gdb_assert (w1 != NULL);
7136 gdb_assert (w2 != NULL);
2bdf28a0 7137
4a64f543
MS
7138 /* If the target can evaluate the condition expression in hardware,
7139 then we we need to insert both watchpoints even if they are at
7140 the same place. Otherwise the watchpoint will only trigger when
7141 the condition of whichever watchpoint was inserted evaluates to
7142 true, not giving a chance for GDB to check the condition of the
7143 other watchpoint. */
3a5c3e22 7144 if ((w1->cond_exp
4a64f543
MS
7145 && target_can_accel_watchpoint_condition (loc1->address,
7146 loc1->length,
0cf6dd15 7147 loc1->watchpoint_type,
3a5c3e22
PA
7148 w1->cond_exp))
7149 || (w2->cond_exp
4a64f543
MS
7150 && target_can_accel_watchpoint_condition (loc2->address,
7151 loc2->length,
0cf6dd15 7152 loc2->watchpoint_type,
3a5c3e22 7153 w2->cond_exp)))
0cf6dd15
TJB
7154 return 0;
7155
85d721b8
PA
7156 /* Note that this checks the owner's type, not the location's. In
7157 case the target does not support read watchpoints, but does
7158 support access watchpoints, we'll have bp_read_watchpoint
7159 watchpoints with hw_access locations. Those should be considered
7160 duplicates of hw_read locations. The hw_read locations will
7161 become hw_access locations later. */
2d134ed3
PA
7162 return (loc1->owner->type == loc2->owner->type
7163 && loc1->pspace->aspace == loc2->pspace->aspace
7164 && loc1->address == loc2->address
7165 && loc1->length == loc2->length);
e4f237da
KB
7166}
7167
31e77af2 7168/* See breakpoint.h. */
6c95b8df 7169
31e77af2 7170int
6c95b8df
PA
7171breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7172 struct address_space *aspace2, CORE_ADDR addr2)
7173{
f5656ead 7174 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7175 || aspace1 == aspace2)
7176 && addr1 == addr2);
7177}
7178
f1310107
TJB
7179/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7180 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7181 matches ASPACE2. On targets that have global breakpoints, the address
7182 space doesn't really matter. */
7183
7184static int
7185breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7186 int len1, struct address_space *aspace2,
7187 CORE_ADDR addr2)
7188{
f5656ead 7189 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7190 || aspace1 == aspace2)
7191 && addr2 >= addr1 && addr2 < addr1 + len1);
7192}
7193
7194/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7195 a ranged breakpoint. In most targets, a match happens only if ASPACE
7196 matches the breakpoint's address space. On targets that have global
7197 breakpoints, the address space doesn't really matter. */
7198
7199static int
7200breakpoint_location_address_match (struct bp_location *bl,
7201 struct address_space *aspace,
7202 CORE_ADDR addr)
7203{
7204 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7205 aspace, addr)
7206 || (bl->length
7207 && breakpoint_address_match_range (bl->pspace->aspace,
7208 bl->address, bl->length,
7209 aspace, addr)));
7210}
7211
d35ae833
PA
7212/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7213 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7214 match happens only if ASPACE matches the breakpoint's address
7215 space. On targets that have global breakpoints, the address space
7216 doesn't really matter. */
7217
7218static int
7219breakpoint_location_address_range_overlap (struct bp_location *bl,
7220 struct address_space *aspace,
7221 CORE_ADDR addr, int len)
7222{
7223 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7224 || bl->pspace->aspace == aspace)
7225 {
7226 int bl_len = bl->length != 0 ? bl->length : 1;
7227
7228 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7229 return 1;
7230 }
7231 return 0;
7232}
7233
1e4d1764
YQ
7234/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7235 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7236 true, otherwise returns false. */
7237
7238static int
7239tracepoint_locations_match (struct bp_location *loc1,
7240 struct bp_location *loc2)
7241{
7242 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7243 /* Since tracepoint locations are never duplicated with others', tracepoint
7244 locations at the same address of different tracepoints are regarded as
7245 different locations. */
7246 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7247 else
7248 return 0;
7249}
7250
2d134ed3
PA
7251/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7252 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7253 represent the same location. */
7254
7255static int
4a64f543
MS
7256breakpoint_locations_match (struct bp_location *loc1,
7257 struct bp_location *loc2)
2d134ed3 7258{
2bdf28a0
JK
7259 int hw_point1, hw_point2;
7260
7261 /* Both of them must not be in moribund_locations. */
7262 gdb_assert (loc1->owner != NULL);
7263 gdb_assert (loc2->owner != NULL);
7264
7265 hw_point1 = is_hardware_watchpoint (loc1->owner);
7266 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7267
7268 if (hw_point1 != hw_point2)
7269 return 0;
7270 else if (hw_point1)
7271 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7272 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7273 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7274 else
f1310107
TJB
7275 /* We compare bp_location.length in order to cover ranged breakpoints. */
7276 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7277 loc2->pspace->aspace, loc2->address)
7278 && loc1->length == loc2->length);
2d134ed3
PA
7279}
7280
76897487
KB
7281static void
7282breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7283 int bnum, int have_bnum)
7284{
f63fbe86
MS
7285 /* The longest string possibly returned by hex_string_custom
7286 is 50 chars. These must be at least that big for safety. */
7287 char astr1[64];
7288 char astr2[64];
76897487 7289
bb599908
PH
7290 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7291 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7292 if (have_bnum)
8a3fe4f8 7293 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7294 bnum, astr1, astr2);
7295 else
8a3fe4f8 7296 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7297}
7298
4a64f543
MS
7299/* Adjust a breakpoint's address to account for architectural
7300 constraints on breakpoint placement. Return the adjusted address.
7301 Note: Very few targets require this kind of adjustment. For most
7302 targets, this function is simply the identity function. */
76897487
KB
7303
7304static CORE_ADDR
a6d9a66e
UW
7305adjust_breakpoint_address (struct gdbarch *gdbarch,
7306 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7307{
a6d9a66e 7308 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7309 {
7310 /* Very few targets need any kind of breakpoint adjustment. */
7311 return bpaddr;
7312 }
88f7da05
KB
7313 else if (bptype == bp_watchpoint
7314 || bptype == bp_hardware_watchpoint
7315 || bptype == bp_read_watchpoint
7316 || bptype == bp_access_watchpoint
fe798b75 7317 || bptype == bp_catchpoint)
88f7da05
KB
7318 {
7319 /* Watchpoints and the various bp_catch_* eventpoints should not
7320 have their addresses modified. */
7321 return bpaddr;
7322 }
7c16b83e
PA
7323 else if (bptype == bp_single_step)
7324 {
7325 /* Single-step breakpoints should not have their addresses
7326 modified. If there's any architectural constrain that
7327 applies to this address, then it should have already been
7328 taken into account when the breakpoint was created in the
7329 first place. If we didn't do this, stepping through e.g.,
7330 Thumb-2 IT blocks would break. */
7331 return bpaddr;
7332 }
76897487
KB
7333 else
7334 {
7335 CORE_ADDR adjusted_bpaddr;
7336
7337 /* Some targets have architectural constraints on the placement
7338 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7339 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7340
7341 /* An adjusted breakpoint address can significantly alter
7342 a user's expectations. Print a warning if an adjustment
7343 is required. */
7344 if (adjusted_bpaddr != bpaddr)
7345 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7346
7347 return adjusted_bpaddr;
7348 }
7349}
7350
28010a5d
PA
7351void
7352init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7353 struct breakpoint *owner)
7cc221ef 7354{
7cc221ef
DJ
7355 memset (loc, 0, sizeof (*loc));
7356
348d480f
PA
7357 gdb_assert (ops != NULL);
7358
28010a5d
PA
7359 loc->ops = ops;
7360 loc->owner = owner;
511a6cd4 7361 loc->cond = NULL;
b775012e 7362 loc->cond_bytecode = NULL;
0d381245
VP
7363 loc->shlib_disabled = 0;
7364 loc->enabled = 1;
e049a4b5 7365
28010a5d 7366 switch (owner->type)
e049a4b5
DJ
7367 {
7368 case bp_breakpoint:
7c16b83e 7369 case bp_single_step:
e049a4b5
DJ
7370 case bp_until:
7371 case bp_finish:
7372 case bp_longjmp:
7373 case bp_longjmp_resume:
e2e4d78b 7374 case bp_longjmp_call_dummy:
186c406b
TT
7375 case bp_exception:
7376 case bp_exception_resume:
e049a4b5 7377 case bp_step_resume:
2c03e5be 7378 case bp_hp_step_resume:
e049a4b5
DJ
7379 case bp_watchpoint_scope:
7380 case bp_call_dummy:
aa7d318d 7381 case bp_std_terminate:
e049a4b5
DJ
7382 case bp_shlib_event:
7383 case bp_thread_event:
7384 case bp_overlay_event:
4efc6507 7385 case bp_jit_event:
0fd8e87f 7386 case bp_longjmp_master:
aa7d318d 7387 case bp_std_terminate_master:
186c406b 7388 case bp_exception_master:
0e30163f
JK
7389 case bp_gnu_ifunc_resolver:
7390 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7391 case bp_dprintf:
e049a4b5 7392 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7393 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7394 break;
7395 case bp_hardware_breakpoint:
7396 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7397 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7398 break;
7399 case bp_hardware_watchpoint:
7400 case bp_read_watchpoint:
7401 case bp_access_watchpoint:
7402 loc->loc_type = bp_loc_hardware_watchpoint;
7403 break;
7404 case bp_watchpoint:
ce78b96d 7405 case bp_catchpoint:
15c3d785
PA
7406 case bp_tracepoint:
7407 case bp_fast_tracepoint:
0fb4aa4b 7408 case bp_static_tracepoint:
e049a4b5
DJ
7409 loc->loc_type = bp_loc_other;
7410 break;
7411 default:
e2e0b3e5 7412 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7413 }
7414
f431efe5 7415 loc->refc = 1;
28010a5d
PA
7416}
7417
7418/* Allocate a struct bp_location. */
7419
7420static struct bp_location *
7421allocate_bp_location (struct breakpoint *bpt)
7422{
348d480f
PA
7423 return bpt->ops->allocate_location (bpt);
7424}
7cc221ef 7425
f431efe5
PA
7426static void
7427free_bp_location (struct bp_location *loc)
fe3f5fa8 7428{
348d480f 7429 loc->ops->dtor (loc);
fe3f5fa8
VP
7430 xfree (loc);
7431}
7432
f431efe5
PA
7433/* Increment reference count. */
7434
7435static void
7436incref_bp_location (struct bp_location *bl)
7437{
7438 ++bl->refc;
7439}
7440
7441/* Decrement reference count. If the reference count reaches 0,
7442 destroy the bp_location. Sets *BLP to NULL. */
7443
7444static void
7445decref_bp_location (struct bp_location **blp)
7446{
0807b50c
PA
7447 gdb_assert ((*blp)->refc > 0);
7448
f431efe5
PA
7449 if (--(*blp)->refc == 0)
7450 free_bp_location (*blp);
7451 *blp = NULL;
7452}
7453
346774a9 7454/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7455
346774a9
PA
7456static void
7457add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7458{
346774a9 7459 struct breakpoint *b1;
c906108c 7460
346774a9
PA
7461 /* Add this breakpoint to the end of the chain so that a list of
7462 breakpoints will come out in order of increasing numbers. */
7463
7464 b1 = breakpoint_chain;
7465 if (b1 == 0)
7466 breakpoint_chain = b;
7467 else
7468 {
7469 while (b1->next)
7470 b1 = b1->next;
7471 b1->next = b;
7472 }
7473}
7474
7475/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7476
7477static void
7478init_raw_breakpoint_without_location (struct breakpoint *b,
7479 struct gdbarch *gdbarch,
28010a5d 7480 enum bptype bptype,
c0a91b2b 7481 const struct breakpoint_ops *ops)
346774a9 7482{
c906108c 7483 memset (b, 0, sizeof (*b));
2219d63c 7484
348d480f
PA
7485 gdb_assert (ops != NULL);
7486
28010a5d 7487 b->ops = ops;
4d28f7a8 7488 b->type = bptype;
a6d9a66e 7489 b->gdbarch = gdbarch;
c906108c
SS
7490 b->language = current_language->la_language;
7491 b->input_radix = input_radix;
7492 b->thread = -1;
b5de0fa7 7493 b->enable_state = bp_enabled;
c906108c
SS
7494 b->next = 0;
7495 b->silent = 0;
7496 b->ignore_count = 0;
7497 b->commands = NULL;
818dd999 7498 b->frame_id = null_frame_id;
0d381245 7499 b->condition_not_parsed = 0;
84f4c1fe 7500 b->py_bp_object = NULL;
d0fb5eae 7501 b->related_breakpoint = b;
f00aae0f 7502 b->location = NULL;
346774a9
PA
7503}
7504
7505/* Helper to set_raw_breakpoint below. Creates a breakpoint
7506 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7507
7508static struct breakpoint *
7509set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7510 enum bptype bptype,
c0a91b2b 7511 const struct breakpoint_ops *ops)
346774a9
PA
7512{
7513 struct breakpoint *b = XNEW (struct breakpoint);
7514
348d480f 7515 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7516 add_to_breakpoint_chain (b);
0d381245
VP
7517 return b;
7518}
7519
0e30163f
JK
7520/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7521 resolutions should be made as the user specified the location explicitly
7522 enough. */
7523
0d381245 7524static void
0e30163f 7525set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7526{
2bdf28a0
JK
7527 gdb_assert (loc->owner != NULL);
7528
0d381245 7529 if (loc->owner->type == bp_breakpoint
1042e4c0 7530 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7531 || is_tracepoint (loc->owner))
0d381245 7532 {
0e30163f 7533 int is_gnu_ifunc;
2c02bd72 7534 const char *function_name;
6a3a010b 7535 CORE_ADDR func_addr;
0e30163f 7536
2c02bd72 7537 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7538 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7539
7540 if (is_gnu_ifunc && !explicit_loc)
7541 {
7542 struct breakpoint *b = loc->owner;
7543
7544 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7545 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7546 &loc->requested_address))
7547 {
7548 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7549 loc->address = adjust_breakpoint_address (loc->gdbarch,
7550 loc->requested_address,
7551 b->type);
7552 }
7553 else if (b->type == bp_breakpoint && b->loc == loc
7554 && loc->next == NULL && b->related_breakpoint == b)
7555 {
7556 /* Create only the whole new breakpoint of this type but do not
7557 mess more complicated breakpoints with multiple locations. */
7558 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7559 /* Remember the resolver's address for use by the return
7560 breakpoint. */
7561 loc->related_address = func_addr;
0e30163f
JK
7562 }
7563 }
7564
2c02bd72
DE
7565 if (function_name)
7566 loc->function_name = xstrdup (function_name);
0d381245
VP
7567 }
7568}
7569
a6d9a66e 7570/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7571struct gdbarch *
a6d9a66e
UW
7572get_sal_arch (struct symtab_and_line sal)
7573{
7574 if (sal.section)
7575 return get_objfile_arch (sal.section->objfile);
7576 if (sal.symtab)
eb822aa6 7577 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7578
7579 return NULL;
7580}
7581
346774a9
PA
7582/* Low level routine for partially initializing a breakpoint of type
7583 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7584 file name, and line number are provided by SAL.
0d381245
VP
7585
7586 It is expected that the caller will complete the initialization of
7587 the newly created breakpoint struct as well as output any status
c56053d2 7588 information regarding the creation of a new breakpoint. */
0d381245 7589
346774a9
PA
7590static void
7591init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7592 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7593 const struct breakpoint_ops *ops)
0d381245 7594{
28010a5d 7595 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7596
3742cc8b 7597 add_location_to_breakpoint (b, &sal);
0d381245 7598
6c95b8df
PA
7599 if (bptype != bp_catchpoint)
7600 gdb_assert (sal.pspace != NULL);
7601
f8eba3c6
TT
7602 /* Store the program space that was used to set the breakpoint,
7603 except for ordinary breakpoints, which are independent of the
7604 program space. */
7605 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7606 b->pspace = sal.pspace;
346774a9 7607}
c906108c 7608
346774a9
PA
7609/* set_raw_breakpoint is a low level routine for allocating and
7610 partially initializing a breakpoint of type BPTYPE. The newly
7611 created breakpoint's address, section, source file name, and line
7612 number are provided by SAL. The newly created and partially
7613 initialized breakpoint is added to the breakpoint chain and
7614 is also returned as the value of this function.
7615
7616 It is expected that the caller will complete the initialization of
7617 the newly created breakpoint struct as well as output any status
7618 information regarding the creation of a new breakpoint. In
7619 particular, set_raw_breakpoint does NOT set the breakpoint
7620 number! Care should be taken to not allow an error to occur
7621 prior to completing the initialization of the breakpoint. If this
7622 should happen, a bogus breakpoint will be left on the chain. */
7623
7624struct breakpoint *
7625set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7626 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7627 const struct breakpoint_ops *ops)
346774a9
PA
7628{
7629 struct breakpoint *b = XNEW (struct breakpoint);
7630
348d480f 7631 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7632 add_to_breakpoint_chain (b);
c906108c
SS
7633 return b;
7634}
7635
53a5351d 7636/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7637 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7638 initiated the operation. */
c906108c
SS
7639
7640void
186c406b 7641set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7642{
35df4500 7643 struct breakpoint *b, *b_tmp;
5d5658a1 7644 int thread = tp->global_num;
0fd8e87f
UW
7645
7646 /* To avoid having to rescan all objfile symbols at every step,
7647 we maintain a list of continually-inserted but always disabled
7648 longjmp "master" breakpoints. Here, we simply create momentary
7649 clones of those and enable them for the requested thread. */
35df4500 7650 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7651 if (b->pspace == current_program_space
186c406b
TT
7652 && (b->type == bp_longjmp_master
7653 || b->type == bp_exception_master))
0fd8e87f 7654 {
06edf0c0
PA
7655 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7656 struct breakpoint *clone;
cc59ec59 7657
e2e4d78b
JK
7658 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7659 after their removal. */
06edf0c0 7660 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7661 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7662 clone->thread = thread;
7663 }
186c406b
TT
7664
7665 tp->initiating_frame = frame;
c906108c
SS
7666}
7667
611c83ae 7668/* Delete all longjmp breakpoints from THREAD. */
c906108c 7669void
611c83ae 7670delete_longjmp_breakpoint (int thread)
c906108c 7671{
35df4500 7672 struct breakpoint *b, *b_tmp;
c906108c 7673
35df4500 7674 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7675 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7676 {
7677 if (b->thread == thread)
7678 delete_breakpoint (b);
7679 }
c906108c
SS
7680}
7681
f59f708a
PA
7682void
7683delete_longjmp_breakpoint_at_next_stop (int thread)
7684{
7685 struct breakpoint *b, *b_tmp;
7686
7687 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7688 if (b->type == bp_longjmp || b->type == bp_exception)
7689 {
7690 if (b->thread == thread)
7691 b->disposition = disp_del_at_next_stop;
7692 }
7693}
7694
e2e4d78b
JK
7695/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7696 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7697 pointer to any of them. Return NULL if this system cannot place longjmp
7698 breakpoints. */
7699
7700struct breakpoint *
7701set_longjmp_breakpoint_for_call_dummy (void)
7702{
7703 struct breakpoint *b, *retval = NULL;
7704
7705 ALL_BREAKPOINTS (b)
7706 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7707 {
7708 struct breakpoint *new_b;
7709
7710 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7711 &momentary_breakpoint_ops,
7712 1);
5d5658a1 7713 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7714
7715 /* Link NEW_B into the chain of RETVAL breakpoints. */
7716
7717 gdb_assert (new_b->related_breakpoint == new_b);
7718 if (retval == NULL)
7719 retval = new_b;
7720 new_b->related_breakpoint = retval;
7721 while (retval->related_breakpoint != new_b->related_breakpoint)
7722 retval = retval->related_breakpoint;
7723 retval->related_breakpoint = new_b;
7724 }
7725
7726 return retval;
7727}
7728
7729/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7730 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7731 stack.
7732
7733 You should call this function only at places where it is safe to currently
7734 unwind the whole stack. Failed stack unwind would discard live dummy
7735 frames. */
7736
7737void
b67a2c6f 7738check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7739{
7740 struct breakpoint *b, *b_tmp;
7741
7742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7743 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7744 {
7745 struct breakpoint *dummy_b = b->related_breakpoint;
7746
7747 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7748 dummy_b = dummy_b->related_breakpoint;
7749 if (dummy_b->type != bp_call_dummy
7750 || frame_find_by_id (dummy_b->frame_id) != NULL)
7751 continue;
7752
b67a2c6f 7753 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7754
7755 while (b->related_breakpoint != b)
7756 {
7757 if (b_tmp == b->related_breakpoint)
7758 b_tmp = b->related_breakpoint->next;
7759 delete_breakpoint (b->related_breakpoint);
7760 }
7761 delete_breakpoint (b);
7762 }
7763}
7764
1900040c
MS
7765void
7766enable_overlay_breakpoints (void)
7767{
52f0bd74 7768 struct breakpoint *b;
1900040c
MS
7769
7770 ALL_BREAKPOINTS (b)
7771 if (b->type == bp_overlay_event)
7772 {
7773 b->enable_state = bp_enabled;
44702360 7774 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7775 overlay_events_enabled = 1;
1900040c
MS
7776 }
7777}
7778
7779void
7780disable_overlay_breakpoints (void)
7781{
52f0bd74 7782 struct breakpoint *b;
1900040c
MS
7783
7784 ALL_BREAKPOINTS (b)
7785 if (b->type == bp_overlay_event)
7786 {
7787 b->enable_state = bp_disabled;
44702360 7788 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7789 overlay_events_enabled = 0;
1900040c
MS
7790 }
7791}
7792
aa7d318d
TT
7793/* Set an active std::terminate breakpoint for each std::terminate
7794 master breakpoint. */
7795void
7796set_std_terminate_breakpoint (void)
7797{
35df4500 7798 struct breakpoint *b, *b_tmp;
aa7d318d 7799
35df4500 7800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7801 if (b->pspace == current_program_space
7802 && b->type == bp_std_terminate_master)
7803 {
06edf0c0 7804 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7805 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7806 }
7807}
7808
7809/* Delete all the std::terminate breakpoints. */
7810void
7811delete_std_terminate_breakpoint (void)
7812{
35df4500 7813 struct breakpoint *b, *b_tmp;
aa7d318d 7814
35df4500 7815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7816 if (b->type == bp_std_terminate)
7817 delete_breakpoint (b);
7818}
7819
c4093a6a 7820struct breakpoint *
a6d9a66e 7821create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7822{
7823 struct breakpoint *b;
c4093a6a 7824
06edf0c0
PA
7825 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7826 &internal_breakpoint_ops);
7827
b5de0fa7 7828 b->enable_state = bp_enabled;
f00aae0f 7829 /* location has to be used or breakpoint_re_set will delete me. */
305e13e6 7830 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7831
44702360 7832 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7833
c4093a6a
JM
7834 return b;
7835}
7836
0101ce28
JJ
7837struct lang_and_radix
7838 {
7839 enum language lang;
7840 int radix;
7841 };
7842
4efc6507
DE
7843/* Create a breakpoint for JIT code registration and unregistration. */
7844
7845struct breakpoint *
7846create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7847{
2a7f3dff
PA
7848 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7849 &internal_breakpoint_ops);
4efc6507 7850}
0101ce28 7851
03673fc7
PP
7852/* Remove JIT code registration and unregistration breakpoint(s). */
7853
7854void
7855remove_jit_event_breakpoints (void)
7856{
7857 struct breakpoint *b, *b_tmp;
7858
7859 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7860 if (b->type == bp_jit_event
7861 && b->loc->pspace == current_program_space)
7862 delete_breakpoint (b);
7863}
7864
cae688ec
JJ
7865void
7866remove_solib_event_breakpoints (void)
7867{
35df4500 7868 struct breakpoint *b, *b_tmp;
cae688ec 7869
35df4500 7870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7871 if (b->type == bp_shlib_event
7872 && b->loc->pspace == current_program_space)
cae688ec
JJ
7873 delete_breakpoint (b);
7874}
7875
f37f681c
PA
7876/* See breakpoint.h. */
7877
7878void
7879remove_solib_event_breakpoints_at_next_stop (void)
7880{
7881 struct breakpoint *b, *b_tmp;
7882
7883 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7884 if (b->type == bp_shlib_event
7885 && b->loc->pspace == current_program_space)
7886 b->disposition = disp_del_at_next_stop;
7887}
7888
04086b45
PA
7889/* Helper for create_solib_event_breakpoint /
7890 create_and_insert_solib_event_breakpoint. Allows specifying which
7891 INSERT_MODE to pass through to update_global_location_list. */
7892
7893static struct breakpoint *
7894create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7895 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7896{
7897 struct breakpoint *b;
7898
06edf0c0
PA
7899 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7900 &internal_breakpoint_ops);
04086b45 7901 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7902 return b;
7903}
7904
04086b45
PA
7905struct breakpoint *
7906create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7907{
7908 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7909}
7910
f37f681c
PA
7911/* See breakpoint.h. */
7912
7913struct breakpoint *
7914create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7915{
7916 struct breakpoint *b;
7917
04086b45
PA
7918 /* Explicitly tell update_global_location_list to insert
7919 locations. */
7920 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7921 if (!b->loc->inserted)
7922 {
7923 delete_breakpoint (b);
7924 return NULL;
7925 }
7926 return b;
7927}
7928
cae688ec
JJ
7929/* Disable any breakpoints that are on code in shared libraries. Only
7930 apply to enabled breakpoints, disabled ones can just stay disabled. */
7931
7932void
cb851954 7933disable_breakpoints_in_shlibs (void)
cae688ec 7934{
876fa593 7935 struct bp_location *loc, **locp_tmp;
cae688ec 7936
876fa593 7937 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7938 {
2bdf28a0 7939 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7940 struct breakpoint *b = loc->owner;
2bdf28a0 7941
4a64f543
MS
7942 /* We apply the check to all breakpoints, including disabled for
7943 those with loc->duplicate set. This is so that when breakpoint
7944 becomes enabled, or the duplicate is removed, gdb will try to
7945 insert all breakpoints. If we don't set shlib_disabled here,
7946 we'll try to insert those breakpoints and fail. */
1042e4c0 7947 if (((b->type == bp_breakpoint)
508ccb1f 7948 || (b->type == bp_jit_event)
1042e4c0 7949 || (b->type == bp_hardware_breakpoint)
d77f58be 7950 || (is_tracepoint (b)))
6c95b8df 7951 && loc->pspace == current_program_space
0d381245 7952 && !loc->shlib_disabled
6c95b8df 7953 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7954 )
0d381245
VP
7955 {
7956 loc->shlib_disabled = 1;
7957 }
cae688ec
JJ
7958 }
7959}
7960
63644780
NB
7961/* Disable any breakpoints and tracepoints that are in SOLIB upon
7962 notification of unloaded_shlib. Only apply to enabled breakpoints,
7963 disabled ones can just stay disabled. */
84acb35a 7964
75149521 7965static void
84acb35a
JJ
7966disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7967{
876fa593 7968 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7969 int disabled_shlib_breaks = 0;
7970
c86cf029
VP
7971 /* SunOS a.out shared libraries are always mapped, so do not
7972 disable breakpoints; they will only be reported as unloaded
7973 through clear_solib when GDB discards its shared library
7974 list. See clear_solib for more information. */
7975 if (exec_bfd != NULL
7976 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7977 return;
7978
876fa593 7979 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7980 {
2bdf28a0 7981 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7982 struct breakpoint *b = loc->owner;
cc59ec59 7983
1e4d1764 7984 if (solib->pspace == loc->pspace
e2dd7057 7985 && !loc->shlib_disabled
1e4d1764
YQ
7986 && (((b->type == bp_breakpoint
7987 || b->type == bp_jit_event
7988 || b->type == bp_hardware_breakpoint)
7989 && (loc->loc_type == bp_loc_hardware_breakpoint
7990 || loc->loc_type == bp_loc_software_breakpoint))
7991 || is_tracepoint (b))
e2dd7057 7992 && solib_contains_address_p (solib, loc->address))
84acb35a 7993 {
e2dd7057
PP
7994 loc->shlib_disabled = 1;
7995 /* At this point, we cannot rely on remove_breakpoint
7996 succeeding so we must mark the breakpoint as not inserted
7997 to prevent future errors occurring in remove_breakpoints. */
7998 loc->inserted = 0;
8d3788bd
VP
7999
8000 /* This may cause duplicate notifications for the same breakpoint. */
8001 observer_notify_breakpoint_modified (b);
8002
e2dd7057
PP
8003 if (!disabled_shlib_breaks)
8004 {
8005 target_terminal_ours_for_output ();
3e43a32a
MS
8006 warning (_("Temporarily disabling breakpoints "
8007 "for unloaded shared library \"%s\""),
e2dd7057 8008 solib->so_name);
84acb35a 8009 }
e2dd7057 8010 disabled_shlib_breaks = 1;
84acb35a
JJ
8011 }
8012 }
84acb35a
JJ
8013}
8014
63644780
NB
8015/* Disable any breakpoints and tracepoints in OBJFILE upon
8016 notification of free_objfile. Only apply to enabled breakpoints,
8017 disabled ones can just stay disabled. */
8018
8019static void
8020disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8021{
8022 struct breakpoint *b;
8023
8024 if (objfile == NULL)
8025 return;
8026
d03de421
PA
8027 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8028 managed by the user with add-symbol-file/remove-symbol-file.
8029 Similarly to how breakpoints in shared libraries are handled in
8030 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
8031 shlib_disabled so they end up uninserted on the next global
8032 location list update. Shared libraries not loaded by the user
8033 aren't handled here -- they're already handled in
8034 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8035 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8036 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8037 main objfile). */
8038 if ((objfile->flags & OBJF_SHARED) == 0
8039 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8040 return;
8041
8042 ALL_BREAKPOINTS (b)
8043 {
8044 struct bp_location *loc;
8045 int bp_modified = 0;
8046
8047 if (!is_breakpoint (b) && !is_tracepoint (b))
8048 continue;
8049
8050 for (loc = b->loc; loc != NULL; loc = loc->next)
8051 {
8052 CORE_ADDR loc_addr = loc->address;
8053
8054 if (loc->loc_type != bp_loc_hardware_breakpoint
8055 && loc->loc_type != bp_loc_software_breakpoint)
8056 continue;
8057
8058 if (loc->shlib_disabled != 0)
8059 continue;
8060
8061 if (objfile->pspace != loc->pspace)
8062 continue;
8063
8064 if (loc->loc_type != bp_loc_hardware_breakpoint
8065 && loc->loc_type != bp_loc_software_breakpoint)
8066 continue;
8067
8068 if (is_addr_in_objfile (loc_addr, objfile))
8069 {
8070 loc->shlib_disabled = 1;
08351840
PA
8071 /* At this point, we don't know whether the object was
8072 unmapped from the inferior or not, so leave the
8073 inserted flag alone. We'll handle failure to
8074 uninsert quietly, in case the object was indeed
8075 unmapped. */
63644780
NB
8076
8077 mark_breakpoint_location_modified (loc);
8078
8079 bp_modified = 1;
8080 }
8081 }
8082
8083 if (bp_modified)
8084 observer_notify_breakpoint_modified (b);
8085 }
8086}
8087
ce78b96d
JB
8088/* FORK & VFORK catchpoints. */
8089
e29a4733
PA
8090/* An instance of this type is used to represent a fork or vfork
8091 catchpoint. It includes a "struct breakpoint" as a kind of base
8092 class; users downcast to "struct breakpoint *" when needed. A
8093 breakpoint is really of this type iff its ops pointer points to
8094 CATCH_FORK_BREAKPOINT_OPS. */
8095
8096struct fork_catchpoint
8097{
8098 /* The base class. */
8099 struct breakpoint base;
8100
8101 /* Process id of a child process whose forking triggered this
8102 catchpoint. This field is only valid immediately after this
8103 catchpoint has triggered. */
8104 ptid_t forked_inferior_pid;
8105};
8106
4a64f543
MS
8107/* Implement the "insert" breakpoint_ops method for fork
8108 catchpoints. */
ce78b96d 8109
77b06cd7
TJB
8110static int
8111insert_catch_fork (struct bp_location *bl)
ce78b96d 8112{
dfd4cc63 8113 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8114}
8115
4a64f543
MS
8116/* Implement the "remove" breakpoint_ops method for fork
8117 catchpoints. */
ce78b96d
JB
8118
8119static int
73971819 8120remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8121{
dfd4cc63 8122 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8123}
8124
8125/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8126 catchpoints. */
8127
8128static int
f1310107 8129breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8130 struct address_space *aspace, CORE_ADDR bp_addr,
8131 const struct target_waitstatus *ws)
ce78b96d 8132{
e29a4733
PA
8133 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8134
f90263c1
TT
8135 if (ws->kind != TARGET_WAITKIND_FORKED)
8136 return 0;
8137
8138 c->forked_inferior_pid = ws->value.related_pid;
8139 return 1;
ce78b96d
JB
8140}
8141
4a64f543
MS
8142/* Implement the "print_it" breakpoint_ops method for fork
8143 catchpoints. */
ce78b96d
JB
8144
8145static enum print_stop_action
348d480f 8146print_it_catch_fork (bpstat bs)
ce78b96d 8147{
36dfb11c 8148 struct ui_out *uiout = current_uiout;
348d480f
PA
8149 struct breakpoint *b = bs->breakpoint_at;
8150 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8151
ce78b96d 8152 annotate_catchpoint (b->number);
f303dbd6 8153 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8154 if (b->disposition == disp_del)
f303dbd6 8155 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8156 else
f303dbd6 8157 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8158 if (ui_out_is_mi_like_p (uiout))
8159 {
8160 ui_out_field_string (uiout, "reason",
8161 async_reason_lookup (EXEC_ASYNC_FORK));
8162 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8163 }
8164 ui_out_field_int (uiout, "bkptno", b->number);
8165 ui_out_text (uiout, " (forked process ");
8166 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8167 ui_out_text (uiout, "), ");
ce78b96d
JB
8168 return PRINT_SRC_AND_LOC;
8169}
8170
4a64f543
MS
8171/* Implement the "print_one" breakpoint_ops method for fork
8172 catchpoints. */
ce78b96d
JB
8173
8174static void
a6d9a66e 8175print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8176{
e29a4733 8177 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8178 struct value_print_options opts;
79a45e25 8179 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8180
8181 get_user_print_options (&opts);
8182
4a64f543
MS
8183 /* Field 4, the address, is omitted (which makes the columns not
8184 line up too nicely with the headers, but the effect is relatively
8185 readable). */
79a45b7d 8186 if (opts.addressprint)
ce78b96d
JB
8187 ui_out_field_skip (uiout, "addr");
8188 annotate_field (5);
8189 ui_out_text (uiout, "fork");
e29a4733 8190 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8191 {
8192 ui_out_text (uiout, ", process ");
8193 ui_out_field_int (uiout, "what",
e29a4733 8194 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8195 ui_out_spaces (uiout, 1);
8196 }
8ac3646f
TT
8197
8198 if (ui_out_is_mi_like_p (uiout))
8199 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8200}
8201
8202/* Implement the "print_mention" breakpoint_ops method for fork
8203 catchpoints. */
8204
8205static void
8206print_mention_catch_fork (struct breakpoint *b)
8207{
8208 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8209}
8210
6149aea9
PA
8211/* Implement the "print_recreate" breakpoint_ops method for fork
8212 catchpoints. */
8213
8214static void
8215print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8216{
8217 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8218 print_recreate_thread (b, fp);
6149aea9
PA
8219}
8220
ce78b96d
JB
8221/* The breakpoint_ops structure to be used in fork catchpoints. */
8222
2060206e 8223static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8224
4a64f543
MS
8225/* Implement the "insert" breakpoint_ops method for vfork
8226 catchpoints. */
ce78b96d 8227
77b06cd7
TJB
8228static int
8229insert_catch_vfork (struct bp_location *bl)
ce78b96d 8230{
dfd4cc63 8231 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8232}
8233
4a64f543
MS
8234/* Implement the "remove" breakpoint_ops method for vfork
8235 catchpoints. */
ce78b96d
JB
8236
8237static int
73971819 8238remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8239{
dfd4cc63 8240 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8241}
8242
8243/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8244 catchpoints. */
8245
8246static int
f1310107 8247breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8248 struct address_space *aspace, CORE_ADDR bp_addr,
8249 const struct target_waitstatus *ws)
ce78b96d 8250{
e29a4733
PA
8251 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8252
f90263c1
TT
8253 if (ws->kind != TARGET_WAITKIND_VFORKED)
8254 return 0;
8255
8256 c->forked_inferior_pid = ws->value.related_pid;
8257 return 1;
ce78b96d
JB
8258}
8259
4a64f543
MS
8260/* Implement the "print_it" breakpoint_ops method for vfork
8261 catchpoints. */
ce78b96d
JB
8262
8263static enum print_stop_action
348d480f 8264print_it_catch_vfork (bpstat bs)
ce78b96d 8265{
36dfb11c 8266 struct ui_out *uiout = current_uiout;
348d480f 8267 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8268 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8269
ce78b96d 8270 annotate_catchpoint (b->number);
f303dbd6 8271 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8272 if (b->disposition == disp_del)
f303dbd6 8273 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8274 else
f303dbd6 8275 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8276 if (ui_out_is_mi_like_p (uiout))
8277 {
8278 ui_out_field_string (uiout, "reason",
8279 async_reason_lookup (EXEC_ASYNC_VFORK));
8280 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8281 }
8282 ui_out_field_int (uiout, "bkptno", b->number);
8283 ui_out_text (uiout, " (vforked process ");
8284 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8285 ui_out_text (uiout, "), ");
ce78b96d
JB
8286 return PRINT_SRC_AND_LOC;
8287}
8288
4a64f543
MS
8289/* Implement the "print_one" breakpoint_ops method for vfork
8290 catchpoints. */
ce78b96d
JB
8291
8292static void
a6d9a66e 8293print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8294{
e29a4733 8295 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8296 struct value_print_options opts;
79a45e25 8297 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8298
8299 get_user_print_options (&opts);
4a64f543
MS
8300 /* Field 4, the address, is omitted (which makes the columns not
8301 line up too nicely with the headers, but the effect is relatively
8302 readable). */
79a45b7d 8303 if (opts.addressprint)
ce78b96d
JB
8304 ui_out_field_skip (uiout, "addr");
8305 annotate_field (5);
8306 ui_out_text (uiout, "vfork");
e29a4733 8307 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8308 {
8309 ui_out_text (uiout, ", process ");
8310 ui_out_field_int (uiout, "what",
e29a4733 8311 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8312 ui_out_spaces (uiout, 1);
8313 }
8ac3646f
TT
8314
8315 if (ui_out_is_mi_like_p (uiout))
8316 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8317}
8318
8319/* Implement the "print_mention" breakpoint_ops method for vfork
8320 catchpoints. */
8321
8322static void
8323print_mention_catch_vfork (struct breakpoint *b)
8324{
8325 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8326}
8327
6149aea9
PA
8328/* Implement the "print_recreate" breakpoint_ops method for vfork
8329 catchpoints. */
8330
8331static void
8332print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8333{
8334 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8335 print_recreate_thread (b, fp);
6149aea9
PA
8336}
8337
ce78b96d
JB
8338/* The breakpoint_ops structure to be used in vfork catchpoints. */
8339
2060206e 8340static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8341
edcc5120
TT
8342/* An instance of this type is used to represent an solib catchpoint.
8343 It includes a "struct breakpoint" as a kind of base class; users
8344 downcast to "struct breakpoint *" when needed. A breakpoint is
8345 really of this type iff its ops pointer points to
8346 CATCH_SOLIB_BREAKPOINT_OPS. */
8347
8348struct solib_catchpoint
8349{
8350 /* The base class. */
8351 struct breakpoint base;
8352
8353 /* True for "catch load", false for "catch unload". */
8354 unsigned char is_load;
8355
8356 /* Regular expression to match, if any. COMPILED is only valid when
8357 REGEX is non-NULL. */
8358 char *regex;
8359 regex_t compiled;
8360};
8361
8362static void
8363dtor_catch_solib (struct breakpoint *b)
8364{
8365 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8366
8367 if (self->regex)
8368 regfree (&self->compiled);
8369 xfree (self->regex);
8370
8371 base_breakpoint_ops.dtor (b);
8372}
8373
8374static int
8375insert_catch_solib (struct bp_location *ignore)
8376{
8377 return 0;
8378}
8379
8380static int
73971819 8381remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8382{
8383 return 0;
8384}
8385
8386static int
8387breakpoint_hit_catch_solib (const struct bp_location *bl,
8388 struct address_space *aspace,
8389 CORE_ADDR bp_addr,
8390 const struct target_waitstatus *ws)
8391{
8392 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8393 struct breakpoint *other;
8394
8395 if (ws->kind == TARGET_WAITKIND_LOADED)
8396 return 1;
8397
8398 ALL_BREAKPOINTS (other)
8399 {
8400 struct bp_location *other_bl;
8401
8402 if (other == bl->owner)
8403 continue;
8404
8405 if (other->type != bp_shlib_event)
8406 continue;
8407
8408 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8409 continue;
8410
8411 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8412 {
8413 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8414 return 1;
8415 }
8416 }
8417
8418 return 0;
8419}
8420
8421static void
8422check_status_catch_solib (struct bpstats *bs)
8423{
8424 struct solib_catchpoint *self
8425 = (struct solib_catchpoint *) bs->breakpoint_at;
8426 int ix;
8427
8428 if (self->is_load)
8429 {
8430 struct so_list *iter;
8431
8432 for (ix = 0;
8433 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8434 ix, iter);
8435 ++ix)
8436 {
8437 if (!self->regex
8438 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8439 return;
8440 }
8441 }
8442 else
8443 {
8444 char *iter;
8445
8446 for (ix = 0;
8447 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8448 ix, iter);
8449 ++ix)
8450 {
8451 if (!self->regex
8452 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8453 return;
8454 }
8455 }
8456
8457 bs->stop = 0;
8458 bs->print_it = print_it_noop;
8459}
8460
8461static enum print_stop_action
8462print_it_catch_solib (bpstat bs)
8463{
8464 struct breakpoint *b = bs->breakpoint_at;
8465 struct ui_out *uiout = current_uiout;
8466
8467 annotate_catchpoint (b->number);
f303dbd6 8468 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8469 if (b->disposition == disp_del)
f303dbd6 8470 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8471 else
f303dbd6 8472 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8473 ui_out_field_int (uiout, "bkptno", b->number);
8474 ui_out_text (uiout, "\n");
8475 if (ui_out_is_mi_like_p (uiout))
8476 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8477 print_solib_event (1);
8478 return PRINT_SRC_AND_LOC;
8479}
8480
8481static void
8482print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8483{
8484 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8485 struct value_print_options opts;
8486 struct ui_out *uiout = current_uiout;
8487 char *msg;
8488
8489 get_user_print_options (&opts);
8490 /* Field 4, the address, is omitted (which makes the columns not
8491 line up too nicely with the headers, but the effect is relatively
8492 readable). */
8493 if (opts.addressprint)
8494 {
8495 annotate_field (4);
8496 ui_out_field_skip (uiout, "addr");
8497 }
8498
8499 annotate_field (5);
8500 if (self->is_load)
8501 {
8502 if (self->regex)
8503 msg = xstrprintf (_("load of library matching %s"), self->regex);
8504 else
8505 msg = xstrdup (_("load of library"));
8506 }
8507 else
8508 {
8509 if (self->regex)
8510 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8511 else
8512 msg = xstrdup (_("unload of library"));
8513 }
8514 ui_out_field_string (uiout, "what", msg);
8515 xfree (msg);
8ac3646f
TT
8516
8517 if (ui_out_is_mi_like_p (uiout))
8518 ui_out_field_string (uiout, "catch-type",
8519 self->is_load ? "load" : "unload");
edcc5120
TT
8520}
8521
8522static void
8523print_mention_catch_solib (struct breakpoint *b)
8524{
8525 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8526
8527 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8528 self->is_load ? "load" : "unload");
8529}
8530
8531static void
8532print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8533{
8534 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8535
8536 fprintf_unfiltered (fp, "%s %s",
8537 b->disposition == disp_del ? "tcatch" : "catch",
8538 self->is_load ? "load" : "unload");
8539 if (self->regex)
8540 fprintf_unfiltered (fp, " %s", self->regex);
8541 fprintf_unfiltered (fp, "\n");
8542}
8543
8544static struct breakpoint_ops catch_solib_breakpoint_ops;
8545
91985142
MG
8546/* Shared helper function (MI and CLI) for creating and installing
8547 a shared object event catchpoint. If IS_LOAD is non-zero then
8548 the events to be caught are load events, otherwise they are
8549 unload events. If IS_TEMP is non-zero the catchpoint is a
8550 temporary one. If ENABLED is non-zero the catchpoint is
8551 created in an enabled state. */
edcc5120 8552
91985142
MG
8553void
8554add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8555{
8556 struct solib_catchpoint *c;
8557 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8558 struct cleanup *cleanup;
8559
edcc5120
TT
8560 if (!arg)
8561 arg = "";
8562 arg = skip_spaces (arg);
8563
8564 c = XCNEW (struct solib_catchpoint);
8565 cleanup = make_cleanup (xfree, c);
8566
8567 if (*arg != '\0')
8568 {
8569 int errcode;
8570
8571 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8572 if (errcode != 0)
8573 {
8574 char *err = get_regcomp_error (errcode, &c->compiled);
8575
8576 make_cleanup (xfree, err);
8577 error (_("Invalid regexp (%s): %s"), err, arg);
8578 }
8579 c->regex = xstrdup (arg);
8580 }
8581
8582 c->is_load = is_load;
91985142 8583 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8584 &catch_solib_breakpoint_ops);
8585
91985142
MG
8586 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8587
edcc5120
TT
8588 discard_cleanups (cleanup);
8589 install_breakpoint (0, &c->base, 1);
8590}
8591
91985142
MG
8592/* A helper function that does all the work for "catch load" and
8593 "catch unload". */
8594
8595static void
8596catch_load_or_unload (char *arg, int from_tty, int is_load,
8597 struct cmd_list_element *command)
8598{
8599 int tempflag;
8600 const int enabled = 1;
8601
8602 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8603
8604 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8605}
8606
edcc5120
TT
8607static void
8608catch_load_command_1 (char *arg, int from_tty,
8609 struct cmd_list_element *command)
8610{
8611 catch_load_or_unload (arg, from_tty, 1, command);
8612}
8613
8614static void
8615catch_unload_command_1 (char *arg, int from_tty,
8616 struct cmd_list_element *command)
8617{
8618 catch_load_or_unload (arg, from_tty, 0, command);
8619}
8620
346774a9
PA
8621/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8622 is non-zero, then make the breakpoint temporary. If COND_STRING is
8623 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8624 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8625
ab04a2af 8626void
346774a9
PA
8627init_catchpoint (struct breakpoint *b,
8628 struct gdbarch *gdbarch, int tempflag,
8629 char *cond_string,
c0a91b2b 8630 const struct breakpoint_ops *ops)
c906108c 8631{
c5aa993b 8632 struct symtab_and_line sal;
346774a9 8633
fe39c653 8634 init_sal (&sal);
6c95b8df 8635 sal.pspace = current_program_space;
c5aa993b 8636
28010a5d 8637 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8638
1b36a34b 8639 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8640 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8641}
8642
28010a5d 8643void
3ea46bff 8644install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8645{
8646 add_to_breakpoint_chain (b);
3a5c3e22 8647 set_breakpoint_number (internal, b);
558a9d82
YQ
8648 if (is_tracepoint (b))
8649 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8650 if (!internal)
8651 mention (b);
c56053d2 8652 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8653
8654 if (update_gll)
44702360 8655 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8656}
8657
9b70b993 8658static void
a6d9a66e
UW
8659create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8660 int tempflag, char *cond_string,
c0a91b2b 8661 const struct breakpoint_ops *ops)
c906108c 8662{
e29a4733 8663 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8664
e29a4733
PA
8665 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8666
8667 c->forked_inferior_pid = null_ptid;
8668
3ea46bff 8669 install_breakpoint (0, &c->base, 1);
c906108c
SS
8670}
8671
fe798b75
JB
8672/* Exec catchpoints. */
8673
b4d90040
PA
8674/* An instance of this type is used to represent an exec catchpoint.
8675 It includes a "struct breakpoint" as a kind of base class; users
8676 downcast to "struct breakpoint *" when needed. A breakpoint is
8677 really of this type iff its ops pointer points to
8678 CATCH_EXEC_BREAKPOINT_OPS. */
8679
8680struct exec_catchpoint
8681{
8682 /* The base class. */
8683 struct breakpoint base;
8684
8685 /* Filename of a program whose exec triggered this catchpoint.
8686 This field is only valid immediately after this catchpoint has
8687 triggered. */
8688 char *exec_pathname;
8689};
8690
8691/* Implement the "dtor" breakpoint_ops method for exec
8692 catchpoints. */
8693
8694static void
8695dtor_catch_exec (struct breakpoint *b)
8696{
8697 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8698
8699 xfree (c->exec_pathname);
348d480f 8700
2060206e 8701 base_breakpoint_ops.dtor (b);
b4d90040
PA
8702}
8703
77b06cd7
TJB
8704static int
8705insert_catch_exec (struct bp_location *bl)
c906108c 8706{
dfd4cc63 8707 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8708}
c906108c 8709
fe798b75 8710static int
73971819 8711remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8712{
dfd4cc63 8713 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8714}
c906108c 8715
fe798b75 8716static int
f1310107 8717breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8718 struct address_space *aspace, CORE_ADDR bp_addr,
8719 const struct target_waitstatus *ws)
fe798b75 8720{
b4d90040
PA
8721 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8722
f90263c1
TT
8723 if (ws->kind != TARGET_WAITKIND_EXECD)
8724 return 0;
8725
8726 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8727 return 1;
fe798b75 8728}
c906108c 8729
fe798b75 8730static enum print_stop_action
348d480f 8731print_it_catch_exec (bpstat bs)
fe798b75 8732{
36dfb11c 8733 struct ui_out *uiout = current_uiout;
348d480f 8734 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8735 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8736
fe798b75 8737 annotate_catchpoint (b->number);
f303dbd6 8738 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8739 if (b->disposition == disp_del)
f303dbd6 8740 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8741 else
f303dbd6 8742 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8743 if (ui_out_is_mi_like_p (uiout))
8744 {
8745 ui_out_field_string (uiout, "reason",
8746 async_reason_lookup (EXEC_ASYNC_EXEC));
8747 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8748 }
8749 ui_out_field_int (uiout, "bkptno", b->number);
8750 ui_out_text (uiout, " (exec'd ");
8751 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8752 ui_out_text (uiout, "), ");
8753
fe798b75 8754 return PRINT_SRC_AND_LOC;
c906108c
SS
8755}
8756
fe798b75 8757static void
a6d9a66e 8758print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8759{
b4d90040 8760 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8761 struct value_print_options opts;
79a45e25 8762 struct ui_out *uiout = current_uiout;
fe798b75
JB
8763
8764 get_user_print_options (&opts);
8765
8766 /* Field 4, the address, is omitted (which makes the columns
8767 not line up too nicely with the headers, but the effect
8768 is relatively readable). */
8769 if (opts.addressprint)
8770 ui_out_field_skip (uiout, "addr");
8771 annotate_field (5);
8772 ui_out_text (uiout, "exec");
b4d90040 8773 if (c->exec_pathname != NULL)
fe798b75
JB
8774 {
8775 ui_out_text (uiout, ", program \"");
b4d90040 8776 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8777 ui_out_text (uiout, "\" ");
8778 }
8ac3646f
TT
8779
8780 if (ui_out_is_mi_like_p (uiout))
8781 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8782}
8783
8784static void
8785print_mention_catch_exec (struct breakpoint *b)
8786{
8787 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8788}
8789
6149aea9
PA
8790/* Implement the "print_recreate" breakpoint_ops method for exec
8791 catchpoints. */
8792
8793static void
8794print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8795{
8796 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8797 print_recreate_thread (b, fp);
6149aea9
PA
8798}
8799
2060206e 8800static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8801
c906108c 8802static int
fba45db2 8803hw_breakpoint_used_count (void)
c906108c 8804{
c906108c 8805 int i = 0;
f1310107
TJB
8806 struct breakpoint *b;
8807 struct bp_location *bl;
c906108c
SS
8808
8809 ALL_BREAKPOINTS (b)
c5aa993b 8810 {
d6b74ac4 8811 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8812 for (bl = b->loc; bl; bl = bl->next)
8813 {
8814 /* Special types of hardware breakpoints may use more than
8815 one register. */
348d480f 8816 i += b->ops->resources_needed (bl);
f1310107 8817 }
c5aa993b 8818 }
c906108c
SS
8819
8820 return i;
8821}
8822
a1398e0c
PA
8823/* Returns the resources B would use if it were a hardware
8824 watchpoint. */
8825
c906108c 8826static int
a1398e0c 8827hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8828{
c906108c 8829 int i = 0;
e09342b5 8830 struct bp_location *bl;
c906108c 8831
a1398e0c
PA
8832 if (!breakpoint_enabled (b))
8833 return 0;
8834
8835 for (bl = b->loc; bl; bl = bl->next)
8836 {
8837 /* Special types of hardware watchpoints may use more than
8838 one register. */
8839 i += b->ops->resources_needed (bl);
8840 }
8841
8842 return i;
8843}
8844
8845/* Returns the sum the used resources of all hardware watchpoints of
8846 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8847 the sum of the used resources of all hardware watchpoints of other
8848 types _not_ TYPE. */
8849
8850static int
8851hw_watchpoint_used_count_others (struct breakpoint *except,
8852 enum bptype type, int *other_type_used)
8853{
8854 int i = 0;
8855 struct breakpoint *b;
8856
c906108c
SS
8857 *other_type_used = 0;
8858 ALL_BREAKPOINTS (b)
e09342b5 8859 {
a1398e0c
PA
8860 if (b == except)
8861 continue;
e09342b5
TJB
8862 if (!breakpoint_enabled (b))
8863 continue;
8864
a1398e0c
PA
8865 if (b->type == type)
8866 i += hw_watchpoint_use_count (b);
8867 else if (is_hardware_watchpoint (b))
8868 *other_type_used = 1;
e09342b5
TJB
8869 }
8870
c906108c
SS
8871 return i;
8872}
8873
c906108c 8874void
fba45db2 8875disable_watchpoints_before_interactive_call_start (void)
c906108c 8876{
c5aa993b 8877 struct breakpoint *b;
c906108c
SS
8878
8879 ALL_BREAKPOINTS (b)
c5aa993b 8880 {
cc60f2e3 8881 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8882 {
b5de0fa7 8883 b->enable_state = bp_call_disabled;
44702360 8884 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8885 }
8886 }
c906108c
SS
8887}
8888
8889void
fba45db2 8890enable_watchpoints_after_interactive_call_stop (void)
c906108c 8891{
c5aa993b 8892 struct breakpoint *b;
c906108c
SS
8893
8894 ALL_BREAKPOINTS (b)
c5aa993b 8895 {
cc60f2e3 8896 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8897 {
b5de0fa7 8898 b->enable_state = bp_enabled;
44702360 8899 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8900 }
8901 }
c906108c
SS
8902}
8903
8bea4e01
UW
8904void
8905disable_breakpoints_before_startup (void)
8906{
6c95b8df 8907 current_program_space->executing_startup = 1;
44702360 8908 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8909}
8910
8911void
8912enable_breakpoints_after_startup (void)
8913{
6c95b8df 8914 current_program_space->executing_startup = 0;
f8eba3c6 8915 breakpoint_re_set ();
8bea4e01
UW
8916}
8917
7c16b83e
PA
8918/* Create a new single-step breakpoint for thread THREAD, with no
8919 locations. */
c906108c 8920
7c16b83e
PA
8921static struct breakpoint *
8922new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8923{
8924 struct breakpoint *b = XNEW (struct breakpoint);
8925
8926 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8927 &momentary_breakpoint_ops);
8928
8929 b->disposition = disp_donttouch;
8930 b->frame_id = null_frame_id;
8931
8932 b->thread = thread;
8933 gdb_assert (b->thread != 0);
8934
8935 add_to_breakpoint_chain (b);
8936
8937 return b;
8938}
8939
8940/* Set a momentary breakpoint of type TYPE at address specified by
8941 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8942 frame. */
c906108c
SS
8943
8944struct breakpoint *
a6d9a66e
UW
8945set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8946 struct frame_id frame_id, enum bptype type)
c906108c 8947{
52f0bd74 8948 struct breakpoint *b;
edb3359d 8949
193facb3
JK
8950 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8951 tail-called one. */
8952 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8953
06edf0c0 8954 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8955 b->enable_state = bp_enabled;
8956 b->disposition = disp_donttouch;
818dd999 8957 b->frame_id = frame_id;
c906108c 8958
4a64f543
MS
8959 /* If we're debugging a multi-threaded program, then we want
8960 momentary breakpoints to be active in only a single thread of
8961 control. */
39f77062 8962 if (in_thread_list (inferior_ptid))
5d5658a1 8963 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8964
44702360 8965 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8966
c906108c
SS
8967 return b;
8968}
611c83ae 8969
06edf0c0 8970/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8971 The new breakpoint will have type TYPE, use OPS as its
8972 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8973
06edf0c0
PA
8974static struct breakpoint *
8975momentary_breakpoint_from_master (struct breakpoint *orig,
8976 enum bptype type,
a1aa2221
LM
8977 const struct breakpoint_ops *ops,
8978 int loc_enabled)
e58b0e63
PA
8979{
8980 struct breakpoint *copy;
8981
06edf0c0 8982 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8983 copy->loc = allocate_bp_location (copy);
0e30163f 8984 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8985
a6d9a66e 8986 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8987 copy->loc->requested_address = orig->loc->requested_address;
8988 copy->loc->address = orig->loc->address;
8989 copy->loc->section = orig->loc->section;
6c95b8df 8990 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8991 copy->loc->probe = orig->loc->probe;
f8eba3c6 8992 copy->loc->line_number = orig->loc->line_number;
2f202fde 8993 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8994 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8995 copy->frame_id = orig->frame_id;
8996 copy->thread = orig->thread;
6c95b8df 8997 copy->pspace = orig->pspace;
e58b0e63
PA
8998
8999 copy->enable_state = bp_enabled;
9000 copy->disposition = disp_donttouch;
9001 copy->number = internal_breakpoint_number--;
9002
44702360 9003 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9004 return copy;
9005}
9006
06edf0c0
PA
9007/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9008 ORIG is NULL. */
9009
9010struct breakpoint *
9011clone_momentary_breakpoint (struct breakpoint *orig)
9012{
9013 /* If there's nothing to clone, then return nothing. */
9014 if (orig == NULL)
9015 return NULL;
9016
a1aa2221 9017 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9018}
9019
611c83ae 9020struct breakpoint *
a6d9a66e
UW
9021set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9022 enum bptype type)
611c83ae
PA
9023{
9024 struct symtab_and_line sal;
9025
9026 sal = find_pc_line (pc, 0);
9027 sal.pc = pc;
9028 sal.section = find_pc_overlay (pc);
9029 sal.explicit_pc = 1;
9030
a6d9a66e 9031 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9032}
c906108c 9033\f
c5aa993b 9034
c906108c
SS
9035/* Tell the user we have just set a breakpoint B. */
9036
9037static void
fba45db2 9038mention (struct breakpoint *b)
c906108c 9039{
348d480f 9040 b->ops->print_mention (b);
79a45e25 9041 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9042 return;
c906108c
SS
9043 printf_filtered ("\n");
9044}
c906108c 9045\f
c5aa993b 9046
1a853c52
PA
9047static int bp_loc_is_permanent (struct bp_location *loc);
9048
0d381245 9049static struct bp_location *
39d61571 9050add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9051 const struct symtab_and_line *sal)
9052{
9053 struct bp_location *loc, **tmp;
3742cc8b
YQ
9054 CORE_ADDR adjusted_address;
9055 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9056
9057 if (loc_gdbarch == NULL)
9058 loc_gdbarch = b->gdbarch;
9059
9060 /* Adjust the breakpoint's address prior to allocating a location.
9061 Once we call allocate_bp_location(), that mostly uninitialized
9062 location will be placed on the location chain. Adjustment of the
9063 breakpoint may cause target_read_memory() to be called and we do
9064 not want its scan of the location chain to find a breakpoint and
9065 location that's only been partially initialized. */
9066 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9067 sal->pc, b->type);
0d381245 9068
d30113d4 9069 /* Sort the locations by their ADDRESS. */
39d61571 9070 loc = allocate_bp_location (b);
d30113d4
JK
9071 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9072 tmp = &((*tmp)->next))
0d381245 9073 ;
d30113d4 9074 loc->next = *tmp;
0d381245 9075 *tmp = loc;
3742cc8b 9076
0d381245 9077 loc->requested_address = sal->pc;
3742cc8b 9078 loc->address = adjusted_address;
6c95b8df 9079 loc->pspace = sal->pspace;
729662a5
TT
9080 loc->probe.probe = sal->probe;
9081 loc->probe.objfile = sal->objfile;
6c95b8df 9082 gdb_assert (loc->pspace != NULL);
0d381245 9083 loc->section = sal->section;
3742cc8b 9084 loc->gdbarch = loc_gdbarch;
f8eba3c6 9085 loc->line_number = sal->line;
2f202fde 9086 loc->symtab = sal->symtab;
f8eba3c6 9087
0e30163f
JK
9088 set_breakpoint_location_function (loc,
9089 sal->explicit_pc || sal->explicit_line);
1a853c52 9090
6ae88661
LM
9091 /* While by definition, permanent breakpoints are already present in the
9092 code, we don't mark the location as inserted. Normally one would expect
9093 that GDB could rely on that breakpoint instruction to stop the program,
9094 thus removing the need to insert its own breakpoint, except that executing
9095 the breakpoint instruction can kill the target instead of reporting a
9096 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9097 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9098 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9099 breakpoint be inserted normally results in QEMU knowing about the GDB
9100 breakpoint, and thus trap before the breakpoint instruction is executed.
9101 (If GDB later needs to continue execution past the permanent breakpoint,
9102 it manually increments the PC, thus avoiding executing the breakpoint
9103 instruction.) */
1a853c52 9104 if (bp_loc_is_permanent (loc))
6ae88661 9105 loc->permanent = 1;
1a853c52 9106
0d381245
VP
9107 return loc;
9108}
514f746b
AR
9109\f
9110
1cf4d951 9111/* See breakpoint.h. */
514f746b 9112
1cf4d951
PA
9113int
9114program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9115{
9116 int len;
9117 CORE_ADDR addr;
1afeeb75 9118 const gdb_byte *bpoint;
514f746b 9119 gdb_byte *target_mem;
939c61fa
JK
9120 struct cleanup *cleanup;
9121 int retval = 0;
514f746b 9122
1cf4d951
PA
9123 addr = address;
9124 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9125
9126 /* Software breakpoints unsupported? */
9127 if (bpoint == NULL)
9128 return 0;
9129
224c3ddb 9130 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9131
9132 /* Enable the automatic memory restoration from breakpoints while
9133 we read the memory. Otherwise we could say about our temporary
9134 breakpoints they are permanent. */
9135 cleanup = make_show_memory_breakpoints_cleanup (0);
9136
9137 if (target_read_memory (address, target_mem, len) == 0
9138 && memcmp (target_mem, bpoint, len) == 0)
9139 retval = 1;
9140
9141 do_cleanups (cleanup);
9142
9143 return retval;
9144}
9145
9146/* Return 1 if LOC is pointing to a permanent breakpoint,
9147 return 0 otherwise. */
9148
9149static int
9150bp_loc_is_permanent (struct bp_location *loc)
9151{
9152 struct cleanup *cleanup;
9153 int retval;
9154
514f746b
AR
9155 gdb_assert (loc != NULL);
9156
244558af
LM
9157 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9158 attempt to read from the addresses the locations of these breakpoint types
9159 point to. program_breakpoint_here_p, below, will attempt to read
9160 memory. */
9161 if (!breakpoint_address_is_meaningful (loc->owner))
9162 return 0;
9163
6c95b8df 9164 cleanup = save_current_space_and_thread ();
6c95b8df 9165 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9166
1cf4d951 9167 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9168
939c61fa
JK
9169 do_cleanups (cleanup);
9170
9171 return retval;
514f746b
AR
9172}
9173
e7e0cddf
SS
9174/* Build a command list for the dprintf corresponding to the current
9175 settings of the dprintf style options. */
9176
9177static void
9178update_dprintf_command_list (struct breakpoint *b)
9179{
9180 char *dprintf_args = b->extra_string;
9181 char *printf_line = NULL;
9182
9183 if (!dprintf_args)
9184 return;
9185
9186 dprintf_args = skip_spaces (dprintf_args);
9187
9188 /* Allow a comma, as it may have terminated a location, but don't
9189 insist on it. */
9190 if (*dprintf_args == ',')
9191 ++dprintf_args;
9192 dprintf_args = skip_spaces (dprintf_args);
9193
9194 if (*dprintf_args != '"')
9195 error (_("Bad format string, missing '\"'."));
9196
d3ce09f5 9197 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9198 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9199 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9200 {
9201 if (!dprintf_function)
9202 error (_("No function supplied for dprintf call"));
9203
9204 if (dprintf_channel && strlen (dprintf_channel) > 0)
9205 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9206 dprintf_function,
9207 dprintf_channel,
9208 dprintf_args);
9209 else
9210 printf_line = xstrprintf ("call (void) %s (%s)",
9211 dprintf_function,
9212 dprintf_args);
9213 }
d3ce09f5
SS
9214 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9215 {
9216 if (target_can_run_breakpoint_commands ())
9217 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9218 else
9219 {
9220 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9221 printf_line = xstrprintf ("printf %s", dprintf_args);
9222 }
9223 }
e7e0cddf
SS
9224 else
9225 internal_error (__FILE__, __LINE__,
9226 _("Invalid dprintf style."));
9227
f28045c2 9228 gdb_assert (printf_line != NULL);
9d6e6e84 9229 /* Manufacture a printf sequence. */
f28045c2 9230 {
8d749320 9231 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9232
f28045c2
YQ
9233 printf_cmd_line->control_type = simple_control;
9234 printf_cmd_line->body_count = 0;
9235 printf_cmd_line->body_list = NULL;
9d6e6e84 9236 printf_cmd_line->next = NULL;
f28045c2 9237 printf_cmd_line->line = printf_line;
e7e0cddf 9238
f28045c2
YQ
9239 breakpoint_set_commands (b, printf_cmd_line);
9240 }
e7e0cddf
SS
9241}
9242
9243/* Update all dprintf commands, making their command lists reflect
9244 current style settings. */
9245
9246static void
9247update_dprintf_commands (char *args, int from_tty,
9248 struct cmd_list_element *c)
9249{
9250 struct breakpoint *b;
9251
9252 ALL_BREAKPOINTS (b)
9253 {
9254 if (b->type == bp_dprintf)
9255 update_dprintf_command_list (b);
9256 }
9257}
c3f6f71d 9258
f00aae0f
KS
9259/* Create a breakpoint with SAL as location. Use LOCATION
9260 as a description of the location, and COND_STRING
b35a8b2f
DE
9261 as condition expression. If LOCATION is NULL then create an
9262 "address location" from the address in the SAL. */
018d34a4
VP
9263
9264static void
d9b3f62e 9265init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9266 struct symtabs_and_lines sals,
9267 struct event_location *location,
f8eba3c6 9268 char *filter, char *cond_string,
e7e0cddf 9269 char *extra_string,
d9b3f62e
PA
9270 enum bptype type, enum bpdisp disposition,
9271 int thread, int task, int ignore_count,
c0a91b2b 9272 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9273 int enabled, int internal, unsigned flags,
9274 int display_canonical)
018d34a4 9275{
0d381245 9276 int i;
018d34a4
VP
9277
9278 if (type == bp_hardware_breakpoint)
9279 {
fbbd034e
AS
9280 int target_resources_ok;
9281
9282 i = hw_breakpoint_used_count ();
9283 target_resources_ok =
9284 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9285 i + 1, 0);
9286 if (target_resources_ok == 0)
9287 error (_("No hardware breakpoint support in the target."));
9288 else if (target_resources_ok < 0)
9289 error (_("Hardware breakpoints used exceeds limit."));
9290 }
9291
6c95b8df
PA
9292 gdb_assert (sals.nelts > 0);
9293
0d381245
VP
9294 for (i = 0; i < sals.nelts; ++i)
9295 {
9296 struct symtab_and_line sal = sals.sals[i];
9297 struct bp_location *loc;
9298
9299 if (from_tty)
5af949e3
UW
9300 {
9301 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9302 if (!loc_gdbarch)
9303 loc_gdbarch = gdbarch;
9304
9305 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9306 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9307 }
0d381245
VP
9308
9309 if (i == 0)
9310 {
d9b3f62e 9311 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9312 b->thread = thread;
4a306c9a 9313 b->task = task;
855a6e68 9314
0d381245 9315 b->cond_string = cond_string;
e7e0cddf 9316 b->extra_string = extra_string;
0d381245 9317 b->ignore_count = ignore_count;
41447f92 9318 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9319 b->disposition = disposition;
6c95b8df 9320
44f238bb
PA
9321 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9322 b->loc->inserted = 1;
9323
0fb4aa4b
PA
9324 if (type == bp_static_tracepoint)
9325 {
d9b3f62e 9326 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9327 struct static_tracepoint_marker marker;
9328
983af33b 9329 if (strace_marker_p (b))
0fb4aa4b
PA
9330 {
9331 /* We already know the marker exists, otherwise, we
9332 wouldn't see a sal for it. */
f00aae0f
KS
9333 const char *p = &event_location_to_string (b->location)[3];
9334 const char *endp;
0fb4aa4b 9335 char *marker_str;
0fb4aa4b 9336
f00aae0f 9337 p = skip_spaces_const (p);
0fb4aa4b 9338
f00aae0f 9339 endp = skip_to_space_const (p);
0fb4aa4b
PA
9340
9341 marker_str = savestring (p, endp - p);
d9b3f62e 9342 t->static_trace_marker_id = marker_str;
0fb4aa4b 9343
3e43a32a
MS
9344 printf_filtered (_("Probed static tracepoint "
9345 "marker \"%s\"\n"),
d9b3f62e 9346 t->static_trace_marker_id);
0fb4aa4b
PA
9347 }
9348 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9349 {
d9b3f62e 9350 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9351 release_static_tracepoint_marker (&marker);
9352
3e43a32a
MS
9353 printf_filtered (_("Probed static tracepoint "
9354 "marker \"%s\"\n"),
d9b3f62e 9355 t->static_trace_marker_id);
0fb4aa4b
PA
9356 }
9357 else
3e43a32a
MS
9358 warning (_("Couldn't determine the static "
9359 "tracepoint marker to probe"));
0fb4aa4b
PA
9360 }
9361
0d381245
VP
9362 loc = b->loc;
9363 }
9364 else
018d34a4 9365 {
39d61571 9366 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9367 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9368 loc->inserted = 1;
0d381245
VP
9369 }
9370
9371 if (b->cond_string)
9372 {
bbc13ae3
KS
9373 const char *arg = b->cond_string;
9374
1bb9788d
TT
9375 loc->cond = parse_exp_1 (&arg, loc->address,
9376 block_for_pc (loc->address), 0);
0d381245 9377 if (*arg)
588ae58c 9378 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9379 }
e7e0cddf
SS
9380
9381 /* Dynamic printf requires and uses additional arguments on the
9382 command line, otherwise it's an error. */
9383 if (type == bp_dprintf)
9384 {
9385 if (b->extra_string)
9386 update_dprintf_command_list (b);
9387 else
9388 error (_("Format string required"));
9389 }
9390 else if (b->extra_string)
588ae58c 9391 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9392 }
018d34a4 9393
56435ebe 9394 b->display_canonical = display_canonical;
f00aae0f
KS
9395 if (location != NULL)
9396 b->location = location;
018d34a4 9397 else
305e13e6
JB
9398 {
9399 const char *addr_string = NULL;
9400 int addr_string_len = 0;
9401
9402 if (location != NULL)
9403 addr_string = event_location_to_string (location);
9404 if (addr_string != NULL)
9405 addr_string_len = strlen (addr_string);
9406
9407 b->location = new_address_location (b->loc->address,
9408 addr_string, addr_string_len);
9409 }
f8eba3c6 9410 b->filter = filter;
d9b3f62e 9411}
018d34a4 9412
d9b3f62e
PA
9413static void
9414create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9415 struct symtabs_and_lines sals,
9416 struct event_location *location,
f8eba3c6 9417 char *filter, char *cond_string,
e7e0cddf 9418 char *extra_string,
d9b3f62e
PA
9419 enum bptype type, enum bpdisp disposition,
9420 int thread, int task, int ignore_count,
c0a91b2b 9421 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9422 int enabled, int internal, unsigned flags,
9423 int display_canonical)
d9b3f62e
PA
9424{
9425 struct breakpoint *b;
9426 struct cleanup *old_chain;
9427
9428 if (is_tracepoint_type (type))
9429 {
9430 struct tracepoint *t;
9431
9432 t = XCNEW (struct tracepoint);
9433 b = &t->base;
9434 }
9435 else
9436 b = XNEW (struct breakpoint);
9437
9438 old_chain = make_cleanup (xfree, b);
9439
9440 init_breakpoint_sal (b, gdbarch,
f00aae0f 9441 sals, location,
e7e0cddf 9442 filter, cond_string, extra_string,
d9b3f62e
PA
9443 type, disposition,
9444 thread, task, ignore_count,
9445 ops, from_tty,
44f238bb
PA
9446 enabled, internal, flags,
9447 display_canonical);
d9b3f62e
PA
9448 discard_cleanups (old_chain);
9449
3ea46bff 9450 install_breakpoint (internal, b, 0);
018d34a4
VP
9451}
9452
9453/* Add SALS.nelts breakpoints to the breakpoint table. For each
9454 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9455 value. COND_STRING, if not NULL, specified the condition to be
9456 used for all breakpoints. Essentially the only case where
9457 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9458 function. In that case, it's still not possible to specify
9459 separate conditions for different overloaded functions, so
9460 we take just a single condition string.
9461
c3f6f71d 9462 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9463 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9464 array contents). If the function fails (error() is called), the
9465 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9466 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9467
9468static void
8cdf0e15 9469create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9470 struct linespec_result *canonical,
e7e0cddf 9471 char *cond_string, char *extra_string,
8cdf0e15
VP
9472 enum bptype type, enum bpdisp disposition,
9473 int thread, int task, int ignore_count,
c0a91b2b 9474 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9475 int enabled, int internal, unsigned flags)
c906108c 9476{
018d34a4 9477 int i;
f8eba3c6 9478 struct linespec_sals *lsal;
cc59ec59 9479
f8eba3c6
TT
9480 if (canonical->pre_expanded)
9481 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9482
9483 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9484 {
f00aae0f 9485 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9486 'break', without arguments. */
f00aae0f
KS
9487 struct event_location *location
9488 = (canonical->location != NULL
9489 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9490 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9491 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9492
f8eba3c6
TT
9493 make_cleanup (xfree, filter_string);
9494 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9495 location,
f8eba3c6 9496 filter_string,
e7e0cddf
SS
9497 cond_string, extra_string,
9498 type, disposition,
84f4c1fe 9499 thread, task, ignore_count, ops,
44f238bb 9500 from_tty, enabled, internal, flags,
56435ebe 9501 canonical->special_display);
f8eba3c6 9502 discard_cleanups (inner);
c3f6f71d 9503 }
c3f6f71d 9504}
c906108c 9505
f00aae0f 9506/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9507 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9508 addresses found. LOCATION points to the end of the SAL (for
9509 linespec locations).
9998af43
TJB
9510
9511 The array and the line spec strings are allocated on the heap, it is
9512 the caller's responsibility to free them. */
c906108c 9513
b9362cc7 9514static void
f00aae0f 9515parse_breakpoint_sals (const struct event_location *location,
58438ac1 9516 struct linespec_result *canonical)
c3f6f71d 9517{
f00aae0f
KS
9518 struct symtab_and_line cursal;
9519
9520 if (event_location_type (location) == LINESPEC_LOCATION)
9521 {
9522 const char *address = get_linespec_location (location);
9523
9524 if (address == NULL)
9525 {
9526 /* The last displayed codepoint, if it's valid, is our default
9527 breakpoint address. */
9528 if (last_displayed_sal_is_valid ())
9529 {
9530 struct linespec_sals lsal;
9531 struct symtab_and_line sal;
9532 CORE_ADDR pc;
9533
9534 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9535 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9536
9537 /* Set sal's pspace, pc, symtab, and line to the values
9538 corresponding to the last call to print_frame_info.
9539 Be sure to reinitialize LINE with NOTCURRENT == 0
9540 as the breakpoint line number is inappropriate otherwise.
9541 find_pc_line would adjust PC, re-set it back. */
9542 get_last_displayed_sal (&sal);
9543 pc = sal.pc;
9544 sal = find_pc_line (pc, 0);
9545
9546 /* "break" without arguments is equivalent to "break *PC"
9547 where PC is the last displayed codepoint's address. So
9548 make sure to set sal.explicit_pc to prevent GDB from
9549 trying to expand the list of sals to include all other
9550 instances with the same symtab and line. */
9551 sal.pc = pc;
9552 sal.explicit_pc = 1;
9553
9554 lsal.sals.sals[0] = sal;
9555 lsal.sals.nelts = 1;
9556 lsal.canonical = NULL;
9557
9558 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9559 return;
9560 }
9561 else
9562 error (_("No default breakpoint address now."));
c906108c 9563 }
c906108c 9564 }
f00aae0f
KS
9565
9566 /* Force almost all breakpoints to be in terms of the
9567 current_source_symtab (which is decode_line_1's default).
9568 This should produce the results we want almost all of the
9569 time while leaving default_breakpoint_* alone.
9570
9571 ObjC: However, don't match an Objective-C method name which
9572 may have a '+' or '-' succeeded by a '['. */
9573 cursal = get_current_source_symtab_and_line ();
9574 if (last_displayed_sal_is_valid ())
c906108c 9575 {
f00aae0f 9576 const char *address = NULL;
cc80f267 9577
f00aae0f
KS
9578 if (event_location_type (location) == LINESPEC_LOCATION)
9579 address = get_linespec_location (location);
cc80f267 9580
f00aae0f
KS
9581 if (!cursal.symtab
9582 || (address != NULL
9583 && strchr ("+-", address[0]) != NULL
9584 && address[1] != '['))
9585 {
c2f4122d 9586 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9587 get_last_displayed_symtab (),
9588 get_last_displayed_line (),
9589 canonical, NULL, NULL);
9590 return;
9591 }
c906108c 9592 }
f00aae0f 9593
c2f4122d 9594 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9595 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9596}
c906108c 9597
c906108c 9598
c3f6f71d 9599/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9600 inserted as a breakpoint. If it can't throw an error. */
c906108c 9601
b9362cc7 9602static void
23e7acfb 9603breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9604{
9605 int i;
cc59ec59 9606
c3f6f71d 9607 for (i = 0; i < sals->nelts; i++)
ee53e872 9608 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9609}
9610
7a697b8d
SS
9611/* Fast tracepoints may have restrictions on valid locations. For
9612 instance, a fast tracepoint using a jump instead of a trap will
9613 likely have to overwrite more bytes than a trap would, and so can
9614 only be placed where the instruction is longer than the jump, or a
9615 multi-instruction sequence does not have a jump into the middle of
9616 it, etc. */
9617
9618static void
9619check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9620 struct symtabs_and_lines *sals)
9621{
9622 int i, rslt;
9623 struct symtab_and_line *sal;
9624 char *msg;
9625 struct cleanup *old_chain;
9626
9627 for (i = 0; i < sals->nelts; i++)
9628 {
f8eba3c6
TT
9629 struct gdbarch *sarch;
9630
7a697b8d
SS
9631 sal = &sals->sals[i];
9632
f8eba3c6
TT
9633 sarch = get_sal_arch (*sal);
9634 /* We fall back to GDBARCH if there is no architecture
9635 associated with SAL. */
9636 if (sarch == NULL)
9637 sarch = gdbarch;
6b940e6a 9638 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9639 old_chain = make_cleanup (xfree, msg);
9640
9641 if (!rslt)
9642 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9643 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9644
9645 do_cleanups (old_chain);
9646 }
9647}
9648
018d34a4
VP
9649/* Given TOK, a string specification of condition and thread, as
9650 accepted by the 'break' command, extract the condition
9651 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9652 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9653 If no condition is found, *COND_STRING is set to NULL.
9654 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9655
9656static void
bbc13ae3 9657find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9658 char **cond_string, int *thread, int *task,
9659 char **rest)
018d34a4
VP
9660{
9661 *cond_string = NULL;
9662 *thread = -1;
ed1d1739
KS
9663 *task = 0;
9664 *rest = NULL;
9665
018d34a4
VP
9666 while (tok && *tok)
9667 {
bbc13ae3 9668 const char *end_tok;
018d34a4 9669 int toklen;
bbc13ae3
KS
9670 const char *cond_start = NULL;
9671 const char *cond_end = NULL;
cc59ec59 9672
bbc13ae3 9673 tok = skip_spaces_const (tok);
e7e0cddf
SS
9674
9675 if ((*tok == '"' || *tok == ',') && rest)
9676 {
9677 *rest = savestring (tok, strlen (tok));
9678 return;
9679 }
9680
bbc13ae3 9681 end_tok = skip_to_space_const (tok);
d634f2de 9682
018d34a4 9683 toklen = end_tok - tok;
d634f2de 9684
018d34a4
VP
9685 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9686 {
f7545552
TT
9687 struct expression *expr;
9688
018d34a4 9689 tok = cond_start = end_tok + 1;
1bb9788d 9690 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9691 xfree (expr);
018d34a4 9692 cond_end = tok;
d634f2de 9693 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9694 }
9695 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9696 {
5d5658a1
PA
9697 const char *tmptok;
9698 struct thread_info *thr;
d634f2de 9699
018d34a4 9700 tok = end_tok + 1;
5d5658a1 9701 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9702 if (tok == tmptok)
9703 error (_("Junk after thread keyword."));
5d5658a1 9704 *thread = thr->global_num;
bbc13ae3 9705 tok = tmptok;
018d34a4 9706 }
4a306c9a
JB
9707 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9708 {
9709 char *tmptok;
9710
9711 tok = end_tok + 1;
bbc13ae3 9712 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9713 if (tok == tmptok)
9714 error (_("Junk after task keyword."));
9715 if (!valid_task_id (*task))
b6199126 9716 error (_("Unknown task %d."), *task);
bbc13ae3 9717 tok = tmptok;
4a306c9a 9718 }
e7e0cddf
SS
9719 else if (rest)
9720 {
9721 *rest = savestring (tok, strlen (tok));
ccab2054 9722 return;
e7e0cddf 9723 }
018d34a4
VP
9724 else
9725 error (_("Junk at end of arguments."));
9726 }
9727}
9728
0fb4aa4b
PA
9729/* Decode a static tracepoint marker spec. */
9730
9731static struct symtabs_and_lines
f00aae0f 9732decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9733{
9734 VEC(static_tracepoint_marker_p) *markers = NULL;
9735 struct symtabs_and_lines sals;
0fb4aa4b 9736 struct cleanup *old_chain;
f00aae0f
KS
9737 const char *p = &(*arg_p)[3];
9738 const char *endp;
0fb4aa4b
PA
9739 char *marker_str;
9740 int i;
9741
f00aae0f 9742 p = skip_spaces_const (p);
0fb4aa4b 9743
f00aae0f 9744 endp = skip_to_space_const (p);
0fb4aa4b
PA
9745
9746 marker_str = savestring (p, endp - p);
9747 old_chain = make_cleanup (xfree, marker_str);
9748
9749 markers = target_static_tracepoint_markers_by_strid (marker_str);
9750 if (VEC_empty(static_tracepoint_marker_p, markers))
9751 error (_("No known static tracepoint marker named %s"), marker_str);
9752
9753 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9754 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9755
9756 for (i = 0; i < sals.nelts; i++)
9757 {
9758 struct static_tracepoint_marker *marker;
9759
9760 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9761
9762 init_sal (&sals.sals[i]);
9763
9764 sals.sals[i] = find_pc_line (marker->address, 0);
9765 sals.sals[i].pc = marker->address;
9766
9767 release_static_tracepoint_marker (marker);
9768 }
9769
9770 do_cleanups (old_chain);
9771
9772 *arg_p = endp;
9773 return sals;
9774}
9775
f00aae0f 9776/* See breakpoint.h. */
0101ce28 9777
8cdf0e15
VP
9778int
9779create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9780 const struct event_location *location, char *cond_string,
e7e0cddf 9781 int thread, char *extra_string,
f00aae0f 9782 int parse_extra,
0fb4aa4b 9783 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9784 int ignore_count,
9785 enum auto_boolean pending_break_support,
c0a91b2b 9786 const struct breakpoint_ops *ops,
44f238bb
PA
9787 int from_tty, int enabled, int internal,
9788 unsigned flags)
c3f6f71d 9789{
7efd8fc2 9790 struct linespec_result canonical;
c3f6f71d 9791 struct cleanup *old_chain;
80c99de1 9792 struct cleanup *bkpt_chain = NULL;
0101ce28 9793 int pending = 0;
4a306c9a 9794 int task = 0;
86b17b60 9795 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9796
348d480f
PA
9797 gdb_assert (ops != NULL);
9798
f00aae0f
KS
9799 /* If extra_string isn't useful, set it to NULL. */
9800 if (extra_string != NULL && *extra_string == '\0')
9801 extra_string = NULL;
9802
7efd8fc2 9803 init_linespec_result (&canonical);
c3f6f71d 9804
492d29ea 9805 TRY
b78a6381 9806 {
f00aae0f 9807 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9808 }
492d29ea 9809 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9810 {
492d29ea
PA
9811 /* If caller is interested in rc value from parse, set
9812 value. */
9813 if (e.error == NOT_FOUND_ERROR)
0101ce28 9814 {
05ff989b
AC
9815 /* If pending breakpoint support is turned off, throw
9816 error. */
fa8d40ab
JJ
9817
9818 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9819 throw_exception (e);
9820
9821 exception_print (gdb_stderr, e);
fa8d40ab 9822
05ff989b
AC
9823 /* If pending breakpoint support is auto query and the user
9824 selects no, then simply return the error code. */
059fb39f 9825 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9826 && !nquery (_("Make %s pending on future shared library load? "),
9827 bptype_string (type_wanted)))
fd9b8c24 9828 return 0;
fa8d40ab 9829
05ff989b
AC
9830 /* At this point, either the user was queried about setting
9831 a pending breakpoint and selected yes, or pending
9832 breakpoint behavior is on and thus a pending breakpoint
9833 is defaulted on behalf of the user. */
f00aae0f 9834 pending = 1;
0101ce28 9835 }
492d29ea
PA
9836 else
9837 throw_exception (e);
0101ce28 9838 }
492d29ea
PA
9839 END_CATCH
9840
f00aae0f 9841 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9842 return 0;
c3f6f71d 9843
4a64f543 9844 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9845 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9846
c3f6f71d
JM
9847 /* ----------------------------- SNIP -----------------------------
9848 Anything added to the cleanup chain beyond this point is assumed
9849 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9850 then the memory is not reclaimed. */
9851 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9852
c3f6f71d
JM
9853 /* Resolve all line numbers to PC's and verify that the addresses
9854 are ok for the target. */
0101ce28 9855 if (!pending)
f8eba3c6
TT
9856 {
9857 int ix;
9858 struct linespec_sals *iter;
9859
9860 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9861 breakpoint_sals_to_pc (&iter->sals);
9862 }
c3f6f71d 9863
7a697b8d 9864 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9865 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9866 {
9867 int ix;
9868 struct linespec_sals *iter;
9869
9870 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9871 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9872 }
7a697b8d 9873
c3f6f71d
JM
9874 /* Verify that condition can be parsed, before setting any
9875 breakpoints. Allocate a separate condition expression for each
4a64f543 9876 breakpoint. */
0101ce28 9877 if (!pending)
c3f6f71d 9878 {
f00aae0f 9879 if (parse_extra)
72b2ff0e 9880 {
0878d0fa 9881 char *rest;
52d361e1
YQ
9882 struct linespec_sals *lsal;
9883
9884 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9885
0878d0fa
YQ
9886 /* Here we only parse 'arg' to separate condition
9887 from thread number, so parsing in context of first
9888 sal is OK. When setting the breakpoint we'll
9889 re-parse it in context of each sal. */
9890
f00aae0f
KS
9891 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9892 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9893 if (cond_string)
9894 make_cleanup (xfree, cond_string);
9895 if (rest)
9896 make_cleanup (xfree, rest);
9897 if (rest)
9898 extra_string = rest;
f00aae0f
KS
9899 else
9900 extra_string = NULL;
72b2ff0e 9901 }
2f069f6f 9902 else
72b2ff0e 9903 {
f00aae0f
KS
9904 if (type_wanted != bp_dprintf
9905 && extra_string != NULL && *extra_string != '\0')
9906 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9907
9908 /* Create a private copy of condition string. */
9909 if (cond_string)
9910 {
9911 cond_string = xstrdup (cond_string);
9912 make_cleanup (xfree, cond_string);
9913 }
9914 /* Create a private copy of any extra string. */
9915 if (extra_string)
9916 {
9917 extra_string = xstrdup (extra_string);
9918 make_cleanup (xfree, extra_string);
9919 }
72b2ff0e 9920 }
0fb4aa4b 9921
52d361e1 9922 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9923 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9924 tempflag ? disp_del : disp_donttouch,
9925 thread, task, ignore_count, ops,
44f238bb 9926 from_tty, enabled, internal, flags);
c906108c 9927 }
0101ce28
JJ
9928 else
9929 {
0101ce28
JJ
9930 struct breakpoint *b;
9931
bfccc43c
YQ
9932 if (is_tracepoint_type (type_wanted))
9933 {
9934 struct tracepoint *t;
9935
9936 t = XCNEW (struct tracepoint);
9937 b = &t->base;
9938 }
9939 else
9940 b = XNEW (struct breakpoint);
9941
9942 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9943 b->location = copy_event_location (location);
bfccc43c 9944
f00aae0f
KS
9945 if (parse_extra)
9946 b->cond_string = NULL;
e12c7713
MK
9947 else
9948 {
9949 /* Create a private copy of condition string. */
9950 if (cond_string)
9951 {
9952 cond_string = xstrdup (cond_string);
9953 make_cleanup (xfree, cond_string);
9954 }
9955 b->cond_string = cond_string;
15630549 9956 b->thread = thread;
e12c7713 9957 }
f00aae0f
KS
9958
9959 /* Create a private copy of any extra string. */
9960 if (extra_string != NULL)
9961 {
9962 extra_string = xstrdup (extra_string);
9963 make_cleanup (xfree, extra_string);
9964 }
9965 b->extra_string = extra_string;
0101ce28 9966 b->ignore_count = ignore_count;
0101ce28 9967 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9968 b->condition_not_parsed = 1;
41447f92 9969 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9970 if ((type_wanted != bp_breakpoint
9971 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9972 b->pspace = current_program_space;
8bea4e01 9973
bfccc43c 9974 install_breakpoint (internal, b, 0);
0101ce28
JJ
9975 }
9976
f8eba3c6 9977 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9978 {
3e43a32a
MS
9979 warning (_("Multiple breakpoints were set.\nUse the "
9980 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9981 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9982 }
9983
80c99de1
PA
9984 /* That's it. Discard the cleanups for data inserted into the
9985 breakpoint. */
9986 discard_cleanups (bkpt_chain);
9987 /* But cleanup everything else. */
c3f6f71d 9988 do_cleanups (old_chain);
217dc9e2 9989
80c99de1 9990 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9991 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9992
9993 return 1;
c3f6f71d 9994}
c906108c 9995
348d480f 9996/* Set a breakpoint.
72b2ff0e
VP
9997 ARG is a string describing breakpoint address,
9998 condition, and thread.
9999 FLAG specifies if a breakpoint is hardware on,
10000 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10001 and BP_TEMPFLAG. */
348d480f 10002
98deb0da 10003static void
72b2ff0e 10004break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10005{
72b2ff0e 10006 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10007 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10008 ? bp_hardware_breakpoint
10009 : bp_breakpoint);
55aa24fb 10010 struct breakpoint_ops *ops;
f00aae0f
KS
10011 struct event_location *location;
10012 struct cleanup *cleanup;
10013
10014 location = string_to_event_location (&arg, current_language);
10015 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
10016
10017 /* Matching breakpoints on probes. */
5b56227b
KS
10018 if (location != NULL
10019 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
10020 ops = &bkpt_probe_breakpoint_ops;
10021 else
10022 ops = &bkpt_breakpoint_ops;
c3f6f71d 10023
8cdf0e15 10024 create_breakpoint (get_current_arch (),
f00aae0f
KS
10025 location,
10026 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 10027 tempflag, type_wanted,
8cdf0e15
VP
10028 0 /* Ignore count */,
10029 pending_break_support,
55aa24fb 10030 ops,
8cdf0e15 10031 from_tty,
84f4c1fe 10032 1 /* enabled */,
44f238bb
PA
10033 0 /* internal */,
10034 0);
f00aae0f 10035 do_cleanups (cleanup);
c906108c
SS
10036}
10037
c906108c
SS
10038/* Helper function for break_command_1 and disassemble_command. */
10039
10040void
fba45db2 10041resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10042{
10043 CORE_ADDR pc;
10044
10045 if (sal->pc == 0 && sal->symtab != NULL)
10046 {
10047 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10048 error (_("No line %d in file \"%s\"."),
05cba821 10049 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10050 sal->pc = pc;
6a048695 10051
4a64f543
MS
10052 /* If this SAL corresponds to a breakpoint inserted using a line
10053 number, then skip the function prologue if necessary. */
6a048695 10054 if (sal->explicit_line)
059acae7 10055 skip_prologue_sal (sal);
c906108c
SS
10056 }
10057
10058 if (sal->section == 0 && sal->symtab != NULL)
10059 {
346d1dfe 10060 const struct blockvector *bv;
3977b71f 10061 const struct block *b;
c5aa993b 10062 struct symbol *sym;
c906108c 10063
43f3e411
DE
10064 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10065 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10066 if (bv != NULL)
10067 {
7f0df278 10068 sym = block_linkage_function (b);
c906108c
SS
10069 if (sym != NULL)
10070 {
eb822aa6
DE
10071 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10072 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10073 sym);
c906108c
SS
10074 }
10075 else
10076 {
4a64f543
MS
10077 /* It really is worthwhile to have the section, so we'll
10078 just have to look harder. This case can be executed
10079 if we have line numbers but no functions (as can
10080 happen in assembly source). */
c906108c 10081
7cbd4a93 10082 struct bound_minimal_symbol msym;
6c95b8df
PA
10083 struct cleanup *old_chain = save_current_space_and_thread ();
10084
10085 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10086
10087 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10088 if (msym.minsym)
efd66ac6 10089 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10090
10091 do_cleanups (old_chain);
c906108c
SS
10092 }
10093 }
10094 }
10095}
10096
10097void
fba45db2 10098break_command (char *arg, int from_tty)
c906108c 10099{
db107f19 10100 break_command_1 (arg, 0, from_tty);
c906108c
SS
10101}
10102
c906108c 10103void
fba45db2 10104tbreak_command (char *arg, int from_tty)
c906108c 10105{
db107f19 10106 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10107}
10108
c906108c 10109static void
fba45db2 10110hbreak_command (char *arg, int from_tty)
c906108c 10111{
db107f19 10112 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10113}
10114
10115static void
fba45db2 10116thbreak_command (char *arg, int from_tty)
c906108c 10117{
db107f19 10118 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10119}
10120
10121static void
fba45db2 10122stop_command (char *arg, int from_tty)
c906108c 10123{
a3f17187 10124 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10125Usage: stop in <function | address>\n\
a3f17187 10126 stop at <line>\n"));
c906108c
SS
10127}
10128
10129static void
fba45db2 10130stopin_command (char *arg, int from_tty)
c906108c
SS
10131{
10132 int badInput = 0;
10133
c5aa993b 10134 if (arg == (char *) NULL)
c906108c
SS
10135 badInput = 1;
10136 else if (*arg != '*')
10137 {
10138 char *argptr = arg;
10139 int hasColon = 0;
10140
4a64f543 10141 /* Look for a ':'. If this is a line number specification, then
53a5351d 10142 say it is bad, otherwise, it should be an address or
4a64f543 10143 function/method name. */
c906108c 10144 while (*argptr && !hasColon)
c5aa993b
JM
10145 {
10146 hasColon = (*argptr == ':');
10147 argptr++;
10148 }
c906108c
SS
10149
10150 if (hasColon)
c5aa993b 10151 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10152 else
c5aa993b 10153 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10154 }
10155
10156 if (badInput)
a3f17187 10157 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10158 else
db107f19 10159 break_command_1 (arg, 0, from_tty);
c906108c
SS
10160}
10161
10162static void
fba45db2 10163stopat_command (char *arg, int from_tty)
c906108c
SS
10164{
10165 int badInput = 0;
10166
c5aa993b 10167 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10168 badInput = 1;
10169 else
10170 {
10171 char *argptr = arg;
10172 int hasColon = 0;
10173
4a64f543
MS
10174 /* Look for a ':'. If there is a '::' then get out, otherwise
10175 it is probably a line number. */
c906108c 10176 while (*argptr && !hasColon)
c5aa993b
JM
10177 {
10178 hasColon = (*argptr == ':');
10179 argptr++;
10180 }
c906108c
SS
10181
10182 if (hasColon)
c5aa993b 10183 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10184 else
c5aa993b 10185 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10186 }
10187
10188 if (badInput)
a3f17187 10189 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10190 else
db107f19 10191 break_command_1 (arg, 0, from_tty);
c906108c
SS
10192}
10193
e7e0cddf
SS
10194/* The dynamic printf command is mostly like a regular breakpoint, but
10195 with a prewired command list consisting of a single output command,
10196 built from extra arguments supplied on the dprintf command
10197 line. */
10198
da821c7b 10199static void
e7e0cddf
SS
10200dprintf_command (char *arg, int from_tty)
10201{
f00aae0f
KS
10202 struct event_location *location;
10203 struct cleanup *cleanup;
10204
10205 location = string_to_event_location (&arg, current_language);
10206 cleanup = make_cleanup_delete_event_location (location);
10207
10208 /* If non-NULL, ARG should have been advanced past the location;
10209 the next character must be ','. */
10210 if (arg != NULL)
10211 {
10212 if (arg[0] != ',' || arg[1] == '\0')
10213 error (_("Format string required"));
10214 else
10215 {
10216 /* Skip the comma. */
10217 ++arg;
10218 }
10219 }
10220
e7e0cddf 10221 create_breakpoint (get_current_arch (),
f00aae0f
KS
10222 location,
10223 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10224 0, bp_dprintf,
10225 0 /* Ignore count */,
10226 pending_break_support,
10227 &dprintf_breakpoint_ops,
10228 from_tty,
10229 1 /* enabled */,
10230 0 /* internal */,
10231 0);
f00aae0f 10232 do_cleanups (cleanup);
e7e0cddf
SS
10233}
10234
d3ce09f5
SS
10235static void
10236agent_printf_command (char *arg, int from_tty)
10237{
10238 error (_("May only run agent-printf on the target"));
10239}
10240
f1310107
TJB
10241/* Implement the "breakpoint_hit" breakpoint_ops method for
10242 ranged breakpoints. */
10243
10244static int
10245breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10246 struct address_space *aspace,
09ac7c10
TT
10247 CORE_ADDR bp_addr,
10248 const struct target_waitstatus *ws)
f1310107 10249{
09ac7c10 10250 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10251 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10252 return 0;
10253
f1310107
TJB
10254 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10255 bl->length, aspace, bp_addr);
10256}
10257
10258/* Implement the "resources_needed" breakpoint_ops method for
10259 ranged breakpoints. */
10260
10261static int
10262resources_needed_ranged_breakpoint (const struct bp_location *bl)
10263{
10264 return target_ranged_break_num_registers ();
10265}
10266
10267/* Implement the "print_it" breakpoint_ops method for
10268 ranged breakpoints. */
10269
10270static enum print_stop_action
348d480f 10271print_it_ranged_breakpoint (bpstat bs)
f1310107 10272{
348d480f 10273 struct breakpoint *b = bs->breakpoint_at;
f1310107 10274 struct bp_location *bl = b->loc;
79a45e25 10275 struct ui_out *uiout = current_uiout;
f1310107
TJB
10276
10277 gdb_assert (b->type == bp_hardware_breakpoint);
10278
10279 /* Ranged breakpoints have only one location. */
10280 gdb_assert (bl && bl->next == NULL);
10281
10282 annotate_breakpoint (b->number);
f303dbd6
PA
10283
10284 maybe_print_thread_hit_breakpoint (uiout);
10285
f1310107 10286 if (b->disposition == disp_del)
f303dbd6 10287 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10288 else
f303dbd6 10289 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10290 if (ui_out_is_mi_like_p (uiout))
10291 {
10292 ui_out_field_string (uiout, "reason",
10293 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10294 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10295 }
10296 ui_out_field_int (uiout, "bkptno", b->number);
10297 ui_out_text (uiout, ", ");
10298
10299 return PRINT_SRC_AND_LOC;
10300}
10301
10302/* Implement the "print_one" breakpoint_ops method for
10303 ranged breakpoints. */
10304
10305static void
10306print_one_ranged_breakpoint (struct breakpoint *b,
10307 struct bp_location **last_loc)
10308{
10309 struct bp_location *bl = b->loc;
10310 struct value_print_options opts;
79a45e25 10311 struct ui_out *uiout = current_uiout;
f1310107
TJB
10312
10313 /* Ranged breakpoints have only one location. */
10314 gdb_assert (bl && bl->next == NULL);
10315
10316 get_user_print_options (&opts);
10317
10318 if (opts.addressprint)
10319 /* We don't print the address range here, it will be printed later
10320 by print_one_detail_ranged_breakpoint. */
10321 ui_out_field_skip (uiout, "addr");
10322 annotate_field (5);
10323 print_breakpoint_location (b, bl);
10324 *last_loc = bl;
10325}
10326
10327/* Implement the "print_one_detail" breakpoint_ops method for
10328 ranged breakpoints. */
10329
10330static void
10331print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10332 struct ui_out *uiout)
10333{
10334 CORE_ADDR address_start, address_end;
10335 struct bp_location *bl = b->loc;
f99d8bf4
PA
10336 struct ui_file *stb = mem_fileopen ();
10337 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10338
10339 gdb_assert (bl);
10340
10341 address_start = bl->address;
10342 address_end = address_start + bl->length - 1;
10343
10344 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10345 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10346 print_core_address (bl->gdbarch, address_start),
10347 print_core_address (bl->gdbarch, address_end));
10348 ui_out_field_stream (uiout, "addr", stb);
10349 ui_out_text (uiout, "\n");
10350
10351 do_cleanups (cleanup);
10352}
10353
10354/* Implement the "print_mention" breakpoint_ops method for
10355 ranged breakpoints. */
10356
10357static void
10358print_mention_ranged_breakpoint (struct breakpoint *b)
10359{
10360 struct bp_location *bl = b->loc;
79a45e25 10361 struct ui_out *uiout = current_uiout;
f1310107
TJB
10362
10363 gdb_assert (bl);
10364 gdb_assert (b->type == bp_hardware_breakpoint);
10365
10366 if (ui_out_is_mi_like_p (uiout))
10367 return;
10368
10369 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10370 b->number, paddress (bl->gdbarch, bl->address),
10371 paddress (bl->gdbarch, bl->address + bl->length - 1));
10372}
10373
10374/* Implement the "print_recreate" breakpoint_ops method for
10375 ranged breakpoints. */
10376
10377static void
10378print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10379{
f00aae0f
KS
10380 fprintf_unfiltered (fp, "break-range %s, %s",
10381 event_location_to_string (b->location),
10382 event_location_to_string (b->location_range_end));
d9b3f62e 10383 print_recreate_thread (b, fp);
f1310107
TJB
10384}
10385
10386/* The breakpoint_ops structure to be used in ranged breakpoints. */
10387
2060206e 10388static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10389
10390/* Find the address where the end of the breakpoint range should be
10391 placed, given the SAL of the end of the range. This is so that if
10392 the user provides a line number, the end of the range is set to the
10393 last instruction of the given line. */
10394
10395static CORE_ADDR
10396find_breakpoint_range_end (struct symtab_and_line sal)
10397{
10398 CORE_ADDR end;
10399
10400 /* If the user provided a PC value, use it. Otherwise,
10401 find the address of the end of the given location. */
10402 if (sal.explicit_pc)
10403 end = sal.pc;
10404 else
10405 {
10406 int ret;
10407 CORE_ADDR start;
10408
10409 ret = find_line_pc_range (sal, &start, &end);
10410 if (!ret)
10411 error (_("Could not find location of the end of the range."));
10412
10413 /* find_line_pc_range returns the start of the next line. */
10414 end--;
10415 }
10416
10417 return end;
10418}
10419
10420/* Implement the "break-range" CLI command. */
10421
10422static void
10423break_range_command (char *arg, int from_tty)
10424{
870f88f7 10425 char *arg_start, *addr_string_start;
f1310107
TJB
10426 struct linespec_result canonical_start, canonical_end;
10427 int bp_count, can_use_bp, length;
10428 CORE_ADDR end;
10429 struct breakpoint *b;
10430 struct symtab_and_line sal_start, sal_end;
f1310107 10431 struct cleanup *cleanup_bkpt;
f8eba3c6 10432 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10433 struct event_location *start_location, *end_location;
f1310107
TJB
10434
10435 /* We don't support software ranged breakpoints. */
10436 if (target_ranged_break_num_registers () < 0)
10437 error (_("This target does not support hardware ranged breakpoints."));
10438
10439 bp_count = hw_breakpoint_used_count ();
10440 bp_count += target_ranged_break_num_registers ();
10441 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10442 bp_count, 0);
10443 if (can_use_bp < 0)
10444 error (_("Hardware breakpoints used exceeds limit."));
10445
f8eba3c6 10446 arg = skip_spaces (arg);
f1310107
TJB
10447 if (arg == NULL || arg[0] == '\0')
10448 error(_("No address range specified."));
10449
f1310107
TJB
10450 init_linespec_result (&canonical_start);
10451
f8eba3c6 10452 arg_start = arg;
f00aae0f
KS
10453 start_location = string_to_event_location (&arg, current_language);
10454 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10455 parse_breakpoint_sals (start_location, &canonical_start);
10456 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10457
10458 if (arg[0] != ',')
10459 error (_("Too few arguments."));
f8eba3c6 10460 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10461 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10462
10463 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10464
10465 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10466 || lsal_start->sals.nelts != 1)
f1310107
TJB
10467 error (_("Cannot create a ranged breakpoint with multiple locations."));
10468
f8eba3c6
TT
10469 sal_start = lsal_start->sals.sals[0];
10470 addr_string_start = savestring (arg_start, arg - arg_start);
10471 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10472
10473 arg++; /* Skip the comma. */
f8eba3c6 10474 arg = skip_spaces (arg);
f1310107
TJB
10475
10476 /* Parse the end location. */
10477
f1310107
TJB
10478 init_linespec_result (&canonical_end);
10479 arg_start = arg;
10480
f8eba3c6 10481 /* We call decode_line_full directly here instead of using
f1310107
TJB
10482 parse_breakpoint_sals because we need to specify the start location's
10483 symtab and line as the default symtab and line for the end of the
10484 range. This makes it possible to have ranges like "foo.c:27, +14",
10485 where +14 means 14 lines from the start location. */
f00aae0f
KS
10486 end_location = string_to_event_location (&arg, current_language);
10487 make_cleanup_delete_event_location (end_location);
c2f4122d 10488 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10489 sal_start.symtab, sal_start.line,
10490 &canonical_end, NULL, NULL);
10491
10492 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10493
f8eba3c6 10494 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10495 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10496
10497 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10498 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10499 || lsal_end->sals.nelts != 1)
f1310107
TJB
10500 error (_("Cannot create a ranged breakpoint with multiple locations."));
10501
f8eba3c6 10502 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10503
10504 end = find_breakpoint_range_end (sal_end);
10505 if (sal_start.pc > end)
177b42fe 10506 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10507
10508 length = end - sal_start.pc + 1;
10509 if (length < 0)
10510 /* Length overflowed. */
10511 error (_("Address range too large."));
10512 else if (length == 1)
10513 {
10514 /* This range is simple enough to be handled by
10515 the `hbreak' command. */
10516 hbreak_command (addr_string_start, 1);
10517
10518 do_cleanups (cleanup_bkpt);
10519
10520 return;
10521 }
10522
10523 /* Now set up the breakpoint. */
10524 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10525 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10526 set_breakpoint_count (breakpoint_count + 1);
10527 b->number = breakpoint_count;
10528 b->disposition = disp_donttouch;
f00aae0f
KS
10529 b->location = copy_event_location (start_location);
10530 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10531 b->loc->length = length;
10532
f8eba3c6 10533 do_cleanups (cleanup_bkpt);
f1310107
TJB
10534
10535 mention (b);
8d3788bd 10536 observer_notify_breakpoint_created (b);
44702360 10537 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10538}
10539
4a64f543
MS
10540/* Return non-zero if EXP is verified as constant. Returned zero
10541 means EXP is variable. Also the constant detection may fail for
10542 some constant expressions and in such case still falsely return
10543 zero. */
2e6e3d9c 10544
65d79d4b
SDJ
10545static int
10546watchpoint_exp_is_const (const struct expression *exp)
10547{
10548 int i = exp->nelts;
10549
10550 while (i > 0)
10551 {
10552 int oplenp, argsp;
10553
10554 /* We are only interested in the descriptor of each element. */
10555 operator_length (exp, i, &oplenp, &argsp);
10556 i -= oplenp;
10557
10558 switch (exp->elts[i].opcode)
10559 {
10560 case BINOP_ADD:
10561 case BINOP_SUB:
10562 case BINOP_MUL:
10563 case BINOP_DIV:
10564 case BINOP_REM:
10565 case BINOP_MOD:
10566 case BINOP_LSH:
10567 case BINOP_RSH:
10568 case BINOP_LOGICAL_AND:
10569 case BINOP_LOGICAL_OR:
10570 case BINOP_BITWISE_AND:
10571 case BINOP_BITWISE_IOR:
10572 case BINOP_BITWISE_XOR:
10573 case BINOP_EQUAL:
10574 case BINOP_NOTEQUAL:
10575 case BINOP_LESS:
10576 case BINOP_GTR:
10577 case BINOP_LEQ:
10578 case BINOP_GEQ:
10579 case BINOP_REPEAT:
10580 case BINOP_COMMA:
10581 case BINOP_EXP:
10582 case BINOP_MIN:
10583 case BINOP_MAX:
10584 case BINOP_INTDIV:
10585 case BINOP_CONCAT:
65d79d4b
SDJ
10586 case TERNOP_COND:
10587 case TERNOP_SLICE:
65d79d4b
SDJ
10588
10589 case OP_LONG:
10590 case OP_DOUBLE:
10591 case OP_DECFLOAT:
10592 case OP_LAST:
10593 case OP_COMPLEX:
10594 case OP_STRING:
65d79d4b
SDJ
10595 case OP_ARRAY:
10596 case OP_TYPE:
608b4967
TT
10597 case OP_TYPEOF:
10598 case OP_DECLTYPE:
6e72ca20 10599 case OP_TYPEID:
65d79d4b
SDJ
10600 case OP_NAME:
10601 case OP_OBJC_NSSTRING:
10602
10603 case UNOP_NEG:
10604 case UNOP_LOGICAL_NOT:
10605 case UNOP_COMPLEMENT:
10606 case UNOP_ADDR:
10607 case UNOP_HIGH:
aeaa2474 10608 case UNOP_CAST:
9eaf6705
TT
10609
10610 case UNOP_CAST_TYPE:
10611 case UNOP_REINTERPRET_CAST:
10612 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10613 /* Unary, binary and ternary operators: We have to check
10614 their operands. If they are constant, then so is the
10615 result of that operation. For instance, if A and B are
10616 determined to be constants, then so is "A + B".
10617
10618 UNOP_IND is one exception to the rule above, because the
10619 value of *ADDR is not necessarily a constant, even when
10620 ADDR is. */
65d79d4b
SDJ
10621 break;
10622
10623 case OP_VAR_VALUE:
10624 /* Check whether the associated symbol is a constant.
4a64f543 10625
65d79d4b 10626 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10627 possible that a buggy compiler could mark a variable as
10628 constant even when it is not, and TYPE_CONST would return
10629 true in this case, while SYMBOL_CLASS wouldn't.
10630
10631 We also have to check for function symbols because they
10632 are always constant. */
65d79d4b
SDJ
10633 {
10634 struct symbol *s = exp->elts[i + 2].symbol;
10635
10636 if (SYMBOL_CLASS (s) != LOC_BLOCK
10637 && SYMBOL_CLASS (s) != LOC_CONST
10638 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10639 return 0;
10640 break;
10641 }
10642
10643 /* The default action is to return 0 because we are using
10644 the optimistic approach here: If we don't know something,
10645 then it is not a constant. */
10646 default:
10647 return 0;
10648 }
10649 }
10650
10651 return 1;
10652}
10653
3a5c3e22
PA
10654/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10655
10656static void
10657dtor_watchpoint (struct breakpoint *self)
10658{
10659 struct watchpoint *w = (struct watchpoint *) self;
10660
10661 xfree (w->cond_exp);
10662 xfree (w->exp);
10663 xfree (w->exp_string);
10664 xfree (w->exp_string_reparse);
10665 value_free (w->val);
10666
10667 base_breakpoint_ops.dtor (self);
10668}
10669
348d480f
PA
10670/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10671
10672static void
10673re_set_watchpoint (struct breakpoint *b)
10674{
3a5c3e22
PA
10675 struct watchpoint *w = (struct watchpoint *) b;
10676
348d480f
PA
10677 /* Watchpoint can be either on expression using entirely global
10678 variables, or it can be on local variables.
10679
10680 Watchpoints of the first kind are never auto-deleted, and even
10681 persist across program restarts. Since they can use variables
10682 from shared libraries, we need to reparse expression as libraries
10683 are loaded and unloaded.
10684
10685 Watchpoints on local variables can also change meaning as result
10686 of solib event. For example, if a watchpoint uses both a local
10687 and a global variables in expression, it's a local watchpoint,
10688 but unloading of a shared library will make the expression
10689 invalid. This is not a very common use case, but we still
10690 re-evaluate expression, to avoid surprises to the user.
10691
10692 Note that for local watchpoints, we re-evaluate it only if
10693 watchpoints frame id is still valid. If it's not, it means the
10694 watchpoint is out of scope and will be deleted soon. In fact,
10695 I'm not sure we'll ever be called in this case.
10696
10697 If a local watchpoint's frame id is still valid, then
3a5c3e22 10698 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10699
3a5c3e22
PA
10700 Don't do anything about disabled watchpoints, since they will be
10701 reevaluated again when enabled. */
10702 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10703}
10704
77b06cd7
TJB
10705/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10706
10707static int
10708insert_watchpoint (struct bp_location *bl)
10709{
3a5c3e22
PA
10710 struct watchpoint *w = (struct watchpoint *) bl->owner;
10711 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10712
10713 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10714 w->cond_exp);
77b06cd7
TJB
10715}
10716
10717/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10718
10719static int
73971819 10720remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10721{
3a5c3e22
PA
10722 struct watchpoint *w = (struct watchpoint *) bl->owner;
10723 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10724
10725 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10726 w->cond_exp);
e09342b5
TJB
10727}
10728
e09342b5 10729static int
348d480f 10730breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10731 struct address_space *aspace, CORE_ADDR bp_addr,
10732 const struct target_waitstatus *ws)
e09342b5 10733{
348d480f 10734 struct breakpoint *b = bl->owner;
3a5c3e22 10735 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10736
348d480f
PA
10737 /* Continuable hardware watchpoints are treated as non-existent if the
10738 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10739 some data address). Otherwise gdb won't stop on a break instruction
10740 in the code (not from a breakpoint) when a hardware watchpoint has
10741 been defined. Also skip watchpoints which we know did not trigger
10742 (did not match the data address). */
10743 if (is_hardware_watchpoint (b)
3a5c3e22 10744 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10745 return 0;
9c06b0b4 10746
348d480f 10747 return 1;
9c06b0b4
TJB
10748}
10749
348d480f
PA
10750static void
10751check_status_watchpoint (bpstat bs)
9c06b0b4 10752{
348d480f 10753 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10754
348d480f 10755 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10756}
10757
10758/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10759 hardware watchpoints. */
9c06b0b4
TJB
10760
10761static int
348d480f 10762resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10763{
3a5c3e22
PA
10764 struct watchpoint *w = (struct watchpoint *) bl->owner;
10765 int length = w->exact? 1 : bl->length;
348d480f
PA
10766
10767 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10768}
10769
10770/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10771 hardware watchpoints. */
9c06b0b4
TJB
10772
10773static int
348d480f 10774works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10775{
efa80663
PA
10776 /* Read and access watchpoints only work with hardware support. */
10777 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10778}
10779
9c06b0b4 10780static enum print_stop_action
348d480f 10781print_it_watchpoint (bpstat bs)
9c06b0b4 10782{
348d480f
PA
10783 struct cleanup *old_chain;
10784 struct breakpoint *b;
f99d8bf4 10785 struct ui_file *stb;
348d480f 10786 enum print_stop_action result;
3a5c3e22 10787 struct watchpoint *w;
79a45e25 10788 struct ui_out *uiout = current_uiout;
348d480f
PA
10789
10790 gdb_assert (bs->bp_location_at != NULL);
10791
348d480f 10792 b = bs->breakpoint_at;
3a5c3e22 10793 w = (struct watchpoint *) b;
348d480f 10794
f99d8bf4
PA
10795 stb = mem_fileopen ();
10796 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10797
f303dbd6
PA
10798 annotate_watchpoint (b->number);
10799 maybe_print_thread_hit_breakpoint (uiout);
10800
9c06b0b4
TJB
10801 switch (b->type)
10802 {
348d480f 10803 case bp_watchpoint:
9c06b0b4 10804 case bp_hardware_watchpoint:
9c06b0b4
TJB
10805 if (ui_out_is_mi_like_p (uiout))
10806 ui_out_field_string
10807 (uiout, "reason",
10808 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10809 mention (b);
10810 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10811 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10812 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10813 ui_out_field_stream (uiout, "old", stb);
10814 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10815 watchpoint_value_print (w->val, stb);
348d480f
PA
10816 ui_out_field_stream (uiout, "new", stb);
10817 ui_out_text (uiout, "\n");
10818 /* More than one watchpoint may have been triggered. */
10819 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10820 break;
10821
10822 case bp_read_watchpoint:
10823 if (ui_out_is_mi_like_p (uiout))
10824 ui_out_field_string
10825 (uiout, "reason",
10826 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10827 mention (b);
10828 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10829 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10830 watchpoint_value_print (w->val, stb);
348d480f
PA
10831 ui_out_field_stream (uiout, "value", stb);
10832 ui_out_text (uiout, "\n");
10833 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10834 break;
10835
10836 case bp_access_watchpoint:
348d480f
PA
10837 if (bs->old_val != NULL)
10838 {
348d480f
PA
10839 if (ui_out_is_mi_like_p (uiout))
10840 ui_out_field_string
10841 (uiout, "reason",
10842 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10843 mention (b);
10844 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10845 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10846 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10847 ui_out_field_stream (uiout, "old", stb);
10848 ui_out_text (uiout, "\nNew value = ");
10849 }
10850 else
10851 {
10852 mention (b);
10853 if (ui_out_is_mi_like_p (uiout))
10854 ui_out_field_string
10855 (uiout, "reason",
10856 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10857 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10858 ui_out_text (uiout, "\nValue = ");
10859 }
f99d8bf4 10860 watchpoint_value_print (w->val, stb);
348d480f
PA
10861 ui_out_field_stream (uiout, "new", stb);
10862 ui_out_text (uiout, "\n");
10863 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10864 break;
10865 default:
348d480f 10866 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10867 }
10868
348d480f
PA
10869 do_cleanups (old_chain);
10870 return result;
10871}
10872
10873/* Implement the "print_mention" breakpoint_ops method for hardware
10874 watchpoints. */
10875
10876static void
10877print_mention_watchpoint (struct breakpoint *b)
10878{
10879 struct cleanup *ui_out_chain;
3a5c3e22 10880 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10881 struct ui_out *uiout = current_uiout;
348d480f
PA
10882
10883 switch (b->type)
10884 {
10885 case bp_watchpoint:
10886 ui_out_text (uiout, "Watchpoint ");
10887 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10888 break;
10889 case bp_hardware_watchpoint:
10890 ui_out_text (uiout, "Hardware watchpoint ");
10891 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10892 break;
10893 case bp_read_watchpoint:
10894 ui_out_text (uiout, "Hardware read watchpoint ");
10895 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10896 break;
10897 case bp_access_watchpoint:
10898 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10899 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10900 break;
10901 default:
10902 internal_error (__FILE__, __LINE__,
10903 _("Invalid hardware watchpoint type."));
10904 }
10905
10906 ui_out_field_int (uiout, "number", b->number);
10907 ui_out_text (uiout, ": ");
3a5c3e22 10908 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10909 do_cleanups (ui_out_chain);
10910}
10911
10912/* Implement the "print_recreate" breakpoint_ops method for
10913 watchpoints. */
10914
10915static void
10916print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10917{
3a5c3e22
PA
10918 struct watchpoint *w = (struct watchpoint *) b;
10919
348d480f
PA
10920 switch (b->type)
10921 {
10922 case bp_watchpoint:
10923 case bp_hardware_watchpoint:
10924 fprintf_unfiltered (fp, "watch");
10925 break;
10926 case bp_read_watchpoint:
10927 fprintf_unfiltered (fp, "rwatch");
10928 break;
10929 case bp_access_watchpoint:
10930 fprintf_unfiltered (fp, "awatch");
10931 break;
10932 default:
10933 internal_error (__FILE__, __LINE__,
10934 _("Invalid watchpoint type."));
10935 }
10936
3a5c3e22 10937 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10938 print_recreate_thread (b, fp);
348d480f
PA
10939}
10940
427cd150
TT
10941/* Implement the "explains_signal" breakpoint_ops method for
10942 watchpoints. */
10943
47591c29 10944static int
427cd150
TT
10945explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10946{
10947 /* A software watchpoint cannot cause a signal other than
10948 GDB_SIGNAL_TRAP. */
10949 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10950 return 0;
427cd150 10951
47591c29 10952 return 1;
427cd150
TT
10953}
10954
348d480f
PA
10955/* The breakpoint_ops structure to be used in hardware watchpoints. */
10956
2060206e 10957static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10958
10959/* Implement the "insert" breakpoint_ops method for
10960 masked hardware watchpoints. */
10961
10962static int
10963insert_masked_watchpoint (struct bp_location *bl)
10964{
3a5c3e22
PA
10965 struct watchpoint *w = (struct watchpoint *) bl->owner;
10966
10967 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10968 bl->watchpoint_type);
10969}
10970
10971/* Implement the "remove" breakpoint_ops method for
10972 masked hardware watchpoints. */
10973
10974static int
73971819 10975remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10976{
3a5c3e22
PA
10977 struct watchpoint *w = (struct watchpoint *) bl->owner;
10978
10979 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10980 bl->watchpoint_type);
10981}
10982
10983/* Implement the "resources_needed" breakpoint_ops method for
10984 masked hardware watchpoints. */
10985
10986static int
10987resources_needed_masked_watchpoint (const struct bp_location *bl)
10988{
3a5c3e22
PA
10989 struct watchpoint *w = (struct watchpoint *) bl->owner;
10990
10991 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10992}
10993
10994/* Implement the "works_in_software_mode" breakpoint_ops method for
10995 masked hardware watchpoints. */
10996
10997static int
10998works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10999{
11000 return 0;
11001}
11002
11003/* Implement the "print_it" breakpoint_ops method for
11004 masked hardware watchpoints. */
11005
11006static enum print_stop_action
11007print_it_masked_watchpoint (bpstat bs)
11008{
11009 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11010 struct ui_out *uiout = current_uiout;
348d480f
PA
11011
11012 /* Masked watchpoints have only one location. */
11013 gdb_assert (b->loc && b->loc->next == NULL);
11014
f303dbd6
PA
11015 annotate_watchpoint (b->number);
11016 maybe_print_thread_hit_breakpoint (uiout);
11017
348d480f
PA
11018 switch (b->type)
11019 {
11020 case bp_hardware_watchpoint:
348d480f
PA
11021 if (ui_out_is_mi_like_p (uiout))
11022 ui_out_field_string
11023 (uiout, "reason",
11024 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11025 break;
11026
11027 case bp_read_watchpoint:
11028 if (ui_out_is_mi_like_p (uiout))
11029 ui_out_field_string
11030 (uiout, "reason",
11031 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11032 break;
11033
11034 case bp_access_watchpoint:
11035 if (ui_out_is_mi_like_p (uiout))
11036 ui_out_field_string
11037 (uiout, "reason",
11038 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11039 break;
11040 default:
11041 internal_error (__FILE__, __LINE__,
11042 _("Invalid hardware watchpoint type."));
11043 }
11044
11045 mention (b);
9c06b0b4
TJB
11046 ui_out_text (uiout, _("\n\
11047Check the underlying instruction at PC for the memory\n\
11048address and value which triggered this watchpoint.\n"));
11049 ui_out_text (uiout, "\n");
11050
11051 /* More than one watchpoint may have been triggered. */
11052 return PRINT_UNKNOWN;
11053}
11054
11055/* Implement the "print_one_detail" breakpoint_ops method for
11056 masked hardware watchpoints. */
11057
11058static void
11059print_one_detail_masked_watchpoint (const struct breakpoint *b,
11060 struct ui_out *uiout)
11061{
3a5c3e22
PA
11062 struct watchpoint *w = (struct watchpoint *) b;
11063
9c06b0b4
TJB
11064 /* Masked watchpoints have only one location. */
11065 gdb_assert (b->loc && b->loc->next == NULL);
11066
11067 ui_out_text (uiout, "\tmask ");
3a5c3e22 11068 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11069 ui_out_text (uiout, "\n");
11070}
11071
11072/* Implement the "print_mention" breakpoint_ops method for
11073 masked hardware watchpoints. */
11074
11075static void
11076print_mention_masked_watchpoint (struct breakpoint *b)
11077{
3a5c3e22 11078 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11079 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11080 struct cleanup *ui_out_chain;
11081
11082 switch (b->type)
11083 {
11084 case bp_hardware_watchpoint:
11085 ui_out_text (uiout, "Masked hardware watchpoint ");
11086 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11087 break;
11088 case bp_read_watchpoint:
11089 ui_out_text (uiout, "Masked hardware read watchpoint ");
11090 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11091 break;
11092 case bp_access_watchpoint:
11093 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11094 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11095 break;
11096 default:
11097 internal_error (__FILE__, __LINE__,
11098 _("Invalid hardware watchpoint type."));
11099 }
11100
11101 ui_out_field_int (uiout, "number", b->number);
11102 ui_out_text (uiout, ": ");
3a5c3e22 11103 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11104 do_cleanups (ui_out_chain);
11105}
11106
11107/* Implement the "print_recreate" breakpoint_ops method for
11108 masked hardware watchpoints. */
11109
11110static void
11111print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11112{
3a5c3e22 11113 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11114 char tmp[40];
11115
11116 switch (b->type)
11117 {
11118 case bp_hardware_watchpoint:
11119 fprintf_unfiltered (fp, "watch");
11120 break;
11121 case bp_read_watchpoint:
11122 fprintf_unfiltered (fp, "rwatch");
11123 break;
11124 case bp_access_watchpoint:
11125 fprintf_unfiltered (fp, "awatch");
11126 break;
11127 default:
11128 internal_error (__FILE__, __LINE__,
11129 _("Invalid hardware watchpoint type."));
11130 }
11131
3a5c3e22
PA
11132 sprintf_vma (tmp, w->hw_wp_mask);
11133 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11134 print_recreate_thread (b, fp);
9c06b0b4
TJB
11135}
11136
11137/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11138
2060206e 11139static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11140
11141/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11142
11143static int
11144is_masked_watchpoint (const struct breakpoint *b)
11145{
11146 return b->ops == &masked_watchpoint_breakpoint_ops;
11147}
11148
53a5351d
JM
11149/* accessflag: hw_write: watch write,
11150 hw_read: watch read,
11151 hw_access: watch access (read or write) */
c906108c 11152static void
bbc13ae3 11153watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11154 int just_location, int internal)
c906108c 11155{
d983da9c 11156 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11157 struct expression *exp;
270140bd 11158 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11159 struct value *val, *mark, *result;
bb9d5f81 11160 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11161 struct frame_info *frame;
bbc13ae3
KS
11162 const char *exp_start = NULL;
11163 const char *exp_end = NULL;
11164 const char *tok, *end_tok;
9c06b0b4 11165 int toklen = -1;
bbc13ae3
KS
11166 const char *cond_start = NULL;
11167 const char *cond_end = NULL;
c906108c 11168 enum bptype bp_type;
37e4754d 11169 int thread = -1;
0cf6dd15 11170 int pc = 0;
9c06b0b4
TJB
11171 /* Flag to indicate whether we are going to use masks for
11172 the hardware watchpoint. */
11173 int use_mask = 0;
11174 CORE_ADDR mask = 0;
3a5c3e22 11175 struct watchpoint *w;
bbc13ae3
KS
11176 char *expression;
11177 struct cleanup *back_to;
c906108c 11178
37e4754d
LM
11179 /* Make sure that we actually have parameters to parse. */
11180 if (arg != NULL && arg[0] != '\0')
11181 {
bbc13ae3
KS
11182 const char *value_start;
11183
11184 exp_end = arg + strlen (arg);
37e4754d 11185
9c06b0b4
TJB
11186 /* Look for "parameter value" pairs at the end
11187 of the arguments string. */
bbc13ae3 11188 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11189 {
11190 /* Skip whitespace at the end of the argument list. */
11191 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11192 tok--;
11193
11194 /* Find the beginning of the last token.
11195 This is the value of the parameter. */
11196 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11197 tok--;
11198 value_start = tok + 1;
11199
11200 /* Skip whitespace. */
11201 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11202 tok--;
11203
11204 end_tok = tok;
11205
11206 /* Find the beginning of the second to last token.
11207 This is the parameter itself. */
11208 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11209 tok--;
11210 tok++;
11211 toklen = end_tok - tok + 1;
11212
61012eef 11213 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11214 {
5d5658a1 11215 struct thread_info *thr;
9c06b0b4
TJB
11216 /* At this point we've found a "thread" token, which means
11217 the user is trying to set a watchpoint that triggers
11218 only in a specific thread. */
5d5658a1 11219 const char *endp;
37e4754d 11220
9c06b0b4
TJB
11221 if (thread != -1)
11222 error(_("You can specify only one thread."));
37e4754d 11223
9c06b0b4 11224 /* Extract the thread ID from the next token. */
5d5658a1 11225 thr = parse_thread_id (value_start, &endp);
37e4754d 11226
5d5658a1 11227 /* Check if the user provided a valid thread ID. */
9c06b0b4 11228 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11229 invalid_thread_id_error (value_start);
9c06b0b4 11230
5d5658a1 11231 thread = thr->global_num;
9c06b0b4 11232 }
61012eef 11233 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11234 {
11235 /* We've found a "mask" token, which means the user wants to
11236 create a hardware watchpoint that is going to have the mask
11237 facility. */
11238 struct value *mask_value, *mark;
37e4754d 11239
9c06b0b4
TJB
11240 if (use_mask)
11241 error(_("You can specify only one mask."));
37e4754d 11242
9c06b0b4 11243 use_mask = just_location = 1;
37e4754d 11244
9c06b0b4
TJB
11245 mark = value_mark ();
11246 mask_value = parse_to_comma_and_eval (&value_start);
11247 mask = value_as_address (mask_value);
11248 value_free_to_mark (mark);
11249 }
11250 else
11251 /* We didn't recognize what we found. We should stop here. */
11252 break;
37e4754d 11253
9c06b0b4
TJB
11254 /* Truncate the string and get rid of the "parameter value" pair before
11255 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11256 exp_end = tok;
9c06b0b4 11257 }
37e4754d 11258 }
bbc13ae3
KS
11259 else
11260 exp_end = arg;
37e4754d 11261
bbc13ae3
KS
11262 /* Parse the rest of the arguments. From here on out, everything
11263 is in terms of a newly allocated string instead of the original
11264 ARG. */
c906108c 11265 innermost_block = NULL;
bbc13ae3
KS
11266 expression = savestring (arg, exp_end - arg);
11267 back_to = make_cleanup (xfree, expression);
11268 exp_start = arg = expression;
1bb9788d 11269 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11270 exp_end = arg;
fa8a61dc
TT
11271 /* Remove trailing whitespace from the expression before saving it.
11272 This makes the eventual display of the expression string a bit
11273 prettier. */
11274 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11275 --exp_end;
11276
65d79d4b
SDJ
11277 /* Checking if the expression is not constant. */
11278 if (watchpoint_exp_is_const (exp))
11279 {
11280 int len;
11281
11282 len = exp_end - exp_start;
11283 while (len > 0 && isspace (exp_start[len - 1]))
11284 len--;
11285 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11286 }
11287
c906108c
SS
11288 exp_valid_block = innermost_block;
11289 mark = value_mark ();
3a1115a0 11290 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11291
bb9d5f81
PP
11292 if (val != NULL && just_location)
11293 {
11294 saved_bitpos = value_bitpos (val);
11295 saved_bitsize = value_bitsize (val);
11296 }
11297
06a64a0b
TT
11298 if (just_location)
11299 {
9c06b0b4
TJB
11300 int ret;
11301
06a64a0b 11302 exp_valid_block = NULL;
a1442452 11303 val = value_addr (result);
06a64a0b
TT
11304 release_value (val);
11305 value_free_to_mark (mark);
9c06b0b4
TJB
11306
11307 if (use_mask)
11308 {
11309 ret = target_masked_watch_num_registers (value_as_address (val),
11310 mask);
11311 if (ret == -1)
11312 error (_("This target does not support masked watchpoints."));
11313 else if (ret == -2)
11314 error (_("Invalid mask or memory region."));
11315 }
06a64a0b
TT
11316 }
11317 else if (val != NULL)
fa4727a6 11318 release_value (val);
c906108c 11319
bbc13ae3
KS
11320 tok = skip_spaces_const (arg);
11321 end_tok = skip_to_space_const (tok);
c906108c
SS
11322
11323 toklen = end_tok - tok;
11324 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11325 {
2d134ed3
PA
11326 struct expression *cond;
11327
60e1c644 11328 innermost_block = NULL;
c906108c 11329 tok = cond_start = end_tok + 1;
1bb9788d 11330 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11331
11332 /* The watchpoint expression may not be local, but the condition
11333 may still be. E.g.: `watch global if local > 0'. */
11334 cond_exp_valid_block = innermost_block;
11335
2d134ed3 11336 xfree (cond);
c906108c
SS
11337 cond_end = tok;
11338 }
11339 if (*tok)
8a3fe4f8 11340 error (_("Junk at end of command."));
c906108c 11341
d983da9c 11342 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11343
11344 /* If the expression is "local", then set up a "watchpoint scope"
11345 breakpoint at the point where we've left the scope of the watchpoint
11346 expression. Create the scope breakpoint before the watchpoint, so
11347 that we will encounter it first in bpstat_stop_status. */
60e1c644 11348 if (exp_valid_block && frame)
d983da9c 11349 {
edb3359d
DJ
11350 if (frame_id_p (frame_unwind_caller_id (frame)))
11351 {
11352 scope_breakpoint
a6d9a66e
UW
11353 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11354 frame_unwind_caller_pc (frame),
06edf0c0
PA
11355 bp_watchpoint_scope,
11356 &momentary_breakpoint_ops);
d983da9c 11357
edb3359d 11358 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11359
edb3359d
DJ
11360 /* Automatically delete the breakpoint when it hits. */
11361 scope_breakpoint->disposition = disp_del;
d983da9c 11362
edb3359d
DJ
11363 /* Only break in the proper frame (help with recursion). */
11364 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11365
edb3359d 11366 /* Set the address at which we will stop. */
a6d9a66e
UW
11367 scope_breakpoint->loc->gdbarch
11368 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11369 scope_breakpoint->loc->requested_address
11370 = frame_unwind_caller_pc (frame);
11371 scope_breakpoint->loc->address
a6d9a66e
UW
11372 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11373 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11374 scope_breakpoint->type);
11375 }
d983da9c
DJ
11376 }
11377
e8369a73
AB
11378 /* Now set up the breakpoint. We create all watchpoints as hardware
11379 watchpoints here even if hardware watchpoints are turned off, a call
11380 to update_watchpoint later in this function will cause the type to
11381 drop back to bp_watchpoint (software watchpoint) if required. */
11382
11383 if (accessflag == hw_read)
11384 bp_type = bp_read_watchpoint;
11385 else if (accessflag == hw_access)
11386 bp_type = bp_access_watchpoint;
11387 else
11388 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11389
11390 w = XCNEW (struct watchpoint);
11391 b = &w->base;
348d480f 11392 if (use_mask)
3a5c3e22
PA
11393 init_raw_breakpoint_without_location (b, NULL, bp_type,
11394 &masked_watchpoint_breakpoint_ops);
348d480f 11395 else
3a5c3e22
PA
11396 init_raw_breakpoint_without_location (b, NULL, bp_type,
11397 &watchpoint_breakpoint_ops);
37e4754d 11398 b->thread = thread;
b5de0fa7 11399 b->disposition = disp_donttouch;
348d480f 11400 b->pspace = current_program_space;
3a5c3e22
PA
11401 w->exp = exp;
11402 w->exp_valid_block = exp_valid_block;
11403 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11404 if (just_location)
11405 {
11406 struct type *t = value_type (val);
11407 CORE_ADDR addr = value_as_address (val);
11408 char *name;
11409
11410 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11411 name = type_to_string (t);
11412
3a5c3e22 11413 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11414 core_addr_to_string (addr));
06a64a0b
TT
11415 xfree (name);
11416
3a5c3e22 11417 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11418 (int) (exp_end - exp_start), exp_start);
11419
06a64a0b
TT
11420 /* The above expression is in C. */
11421 b->language = language_c;
11422 }
11423 else
3a5c3e22 11424 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11425
11426 if (use_mask)
11427 {
3a5c3e22 11428 w->hw_wp_mask = mask;
9c06b0b4
TJB
11429 }
11430 else
11431 {
3a5c3e22 11432 w->val = val;
bb9d5f81
PP
11433 w->val_bitpos = saved_bitpos;
11434 w->val_bitsize = saved_bitsize;
3a5c3e22 11435 w->val_valid = 1;
9c06b0b4 11436 }
77b06cd7 11437
c906108c
SS
11438 if (cond_start)
11439 b->cond_string = savestring (cond_start, cond_end - cond_start);
11440 else
11441 b->cond_string = 0;
c5aa993b 11442
c906108c 11443 if (frame)
f6bc2008 11444 {
3a5c3e22
PA
11445 w->watchpoint_frame = get_frame_id (frame);
11446 w->watchpoint_thread = inferior_ptid;
f6bc2008 11447 }
c906108c 11448 else
f6bc2008 11449 {
3a5c3e22
PA
11450 w->watchpoint_frame = null_frame_id;
11451 w->watchpoint_thread = null_ptid;
f6bc2008 11452 }
c906108c 11453
d983da9c 11454 if (scope_breakpoint != NULL)
c906108c 11455 {
d983da9c
DJ
11456 /* The scope breakpoint is related to the watchpoint. We will
11457 need to act on them together. */
11458 b->related_breakpoint = scope_breakpoint;
11459 scope_breakpoint->related_breakpoint = b;
c906108c 11460 }
d983da9c 11461
06a64a0b
TT
11462 if (!just_location)
11463 value_free_to_mark (mark);
2d134ed3 11464
492d29ea 11465 TRY
a9634178
TJB
11466 {
11467 /* Finally update the new watchpoint. This creates the locations
11468 that should be inserted. */
3a5c3e22 11469 update_watchpoint (w, 1);
a9634178 11470 }
492d29ea 11471 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11472 {
11473 delete_breakpoint (b);
11474 throw_exception (e);
11475 }
492d29ea 11476 END_CATCH
a9634178 11477
3ea46bff 11478 install_breakpoint (internal, b, 1);
bbc13ae3 11479 do_cleanups (back_to);
c906108c
SS
11480}
11481
e09342b5 11482/* Return count of debug registers needed to watch the given expression.
e09342b5 11483 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11484
c906108c 11485static int
a9634178 11486can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11487{
11488 int found_memory_cnt = 0;
2e70b7b9 11489 struct value *head = v;
c906108c
SS
11490
11491 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11492 if (!can_use_hw_watchpoints)
c906108c 11493 return 0;
c5aa993b 11494
5c44784c
JM
11495 /* Make sure that the value of the expression depends only upon
11496 memory contents, and values computed from them within GDB. If we
11497 find any register references or function calls, we can't use a
11498 hardware watchpoint.
11499
11500 The idea here is that evaluating an expression generates a series
11501 of values, one holding the value of every subexpression. (The
11502 expression a*b+c has five subexpressions: a, b, a*b, c, and
11503 a*b+c.) GDB's values hold almost enough information to establish
11504 the criteria given above --- they identify memory lvalues,
11505 register lvalues, computed values, etcetera. So we can evaluate
11506 the expression, and then scan the chain of values that leaves
11507 behind to decide whether we can detect any possible change to the
11508 expression's final value using only hardware watchpoints.
11509
11510 However, I don't think that the values returned by inferior
11511 function calls are special in any way. So this function may not
11512 notice that an expression involving an inferior function call
11513 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11514 for (; v; v = value_next (v))
c906108c 11515 {
5c44784c 11516 if (VALUE_LVAL (v) == lval_memory)
c906108c 11517 {
8464be76
DJ
11518 if (v != head && value_lazy (v))
11519 /* A lazy memory lvalue in the chain is one that GDB never
11520 needed to fetch; we either just used its address (e.g.,
11521 `a' in `a.b') or we never needed it at all (e.g., `a'
11522 in `a,b'). This doesn't apply to HEAD; if that is
11523 lazy then it was not readable, but watch it anyway. */
5c44784c 11524 ;
53a5351d 11525 else
5c44784c
JM
11526 {
11527 /* Ahh, memory we actually used! Check if we can cover
11528 it with hardware watchpoints. */
df407dfe 11529 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11530
11531 /* We only watch structs and arrays if user asked for it
11532 explicitly, never if they just happen to appear in a
11533 middle of some value chain. */
11534 if (v == head
11535 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11536 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11537 {
42ae5230 11538 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11539 int len;
11540 int num_regs;
11541
a9634178 11542 len = (target_exact_watchpoints
e09342b5
TJB
11543 && is_scalar_type_recursive (vtype))?
11544 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11545
e09342b5
TJB
11546 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11547 if (!num_regs)
2e70b7b9
MS
11548 return 0;
11549 else
e09342b5 11550 found_memory_cnt += num_regs;
2e70b7b9 11551 }
5c44784c 11552 }
c5aa993b 11553 }
5086187c
AC
11554 else if (VALUE_LVAL (v) != not_lval
11555 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11556 return 0; /* These are values from the history (e.g., $1). */
5086187c 11557 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11558 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11559 }
11560
11561 /* The expression itself looks suitable for using a hardware
11562 watchpoint, but give the target machine a chance to reject it. */
11563 return found_memory_cnt;
11564}
11565
8b93c638 11566void
84f4c1fe 11567watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11568{
84f4c1fe 11569 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11570}
11571
06a64a0b
TT
11572/* A helper function that looks for the "-location" argument and then
11573 calls watch_command_1. */
11574
11575static void
11576watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11577{
11578 int just_location = 0;
11579
11580 if (arg
11581 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11582 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11583 {
e9cafbcc 11584 arg = skip_spaces (arg);
06a64a0b
TT
11585 just_location = 1;
11586 }
11587
84f4c1fe 11588 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11589}
8926118c 11590
c5aa993b 11591static void
fba45db2 11592watch_command (char *arg, int from_tty)
c906108c 11593{
06a64a0b 11594 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11595}
11596
8b93c638 11597void
84f4c1fe 11598rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11599{
84f4c1fe 11600 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11601}
8926118c 11602
c5aa993b 11603static void
fba45db2 11604rwatch_command (char *arg, int from_tty)
c906108c 11605{
06a64a0b 11606 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11607}
11608
8b93c638 11609void
84f4c1fe 11610awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11611{
84f4c1fe 11612 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11613}
8926118c 11614
c5aa993b 11615static void
fba45db2 11616awatch_command (char *arg, int from_tty)
c906108c 11617{
06a64a0b 11618 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11619}
c906108c 11620\f
c5aa993b 11621
cfc31633
PA
11622/* Data for the FSM that manages the until(location)/advance commands
11623 in infcmd.c. Here because it uses the mechanisms of
11624 breakpoints. */
c906108c 11625
cfc31633 11626struct until_break_fsm
bfec99b2 11627{
cfc31633
PA
11628 /* The base class. */
11629 struct thread_fsm thread_fsm;
11630
11631 /* The thread that as current when the command was executed. */
11632 int thread;
11633
11634 /* The breakpoint set at the destination location. */
11635 struct breakpoint *location_breakpoint;
11636
11637 /* Breakpoint set at the return address in the caller frame. May be
11638 NULL. */
11639 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11640};
11641
8980e177
PA
11642static void until_break_fsm_clean_up (struct thread_fsm *self,
11643 struct thread_info *thread);
11644static int until_break_fsm_should_stop (struct thread_fsm *self,
11645 struct thread_info *thread);
cfc31633
PA
11646static enum async_reply_reason
11647 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11648
11649/* until_break_fsm's vtable. */
11650
11651static struct thread_fsm_ops until_break_fsm_ops =
11652{
11653 NULL, /* dtor */
11654 until_break_fsm_clean_up,
11655 until_break_fsm_should_stop,
11656 NULL, /* return_value */
11657 until_break_fsm_async_reply_reason,
11658};
11659
11660/* Allocate a new until_break_command_fsm. */
11661
11662static struct until_break_fsm *
8980e177 11663new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11664 struct breakpoint *location_breakpoint,
11665 struct breakpoint *caller_breakpoint)
11666{
11667 struct until_break_fsm *sm;
11668
11669 sm = XCNEW (struct until_break_fsm);
8980e177 11670 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11671
11672 sm->thread = thread;
11673 sm->location_breakpoint = location_breakpoint;
11674 sm->caller_breakpoint = caller_breakpoint;
11675
11676 return sm;
11677}
11678
11679/* Implementation of the 'should_stop' FSM method for the
11680 until(location)/advance commands. */
11681
11682static int
8980e177
PA
11683until_break_fsm_should_stop (struct thread_fsm *self,
11684 struct thread_info *tp)
cfc31633
PA
11685{
11686 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11687
11688 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11689 sm->location_breakpoint) != NULL
11690 || (sm->caller_breakpoint != NULL
11691 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11692 sm->caller_breakpoint) != NULL))
11693 thread_fsm_set_finished (self);
11694
11695 return 1;
11696}
11697
11698/* Implementation of the 'clean_up' FSM method for the
11699 until(location)/advance commands. */
11700
c2c6d25f 11701static void
8980e177
PA
11702until_break_fsm_clean_up (struct thread_fsm *self,
11703 struct thread_info *thread)
43ff13b4 11704{
cfc31633 11705 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11706
cfc31633
PA
11707 /* Clean up our temporary breakpoints. */
11708 if (sm->location_breakpoint != NULL)
11709 {
11710 delete_breakpoint (sm->location_breakpoint);
11711 sm->location_breakpoint = NULL;
11712 }
11713 if (sm->caller_breakpoint != NULL)
11714 {
11715 delete_breakpoint (sm->caller_breakpoint);
11716 sm->caller_breakpoint = NULL;
11717 }
11718 delete_longjmp_breakpoint (sm->thread);
11719}
11720
11721/* Implementation of the 'async_reply_reason' FSM method for the
11722 until(location)/advance commands. */
11723
11724static enum async_reply_reason
11725until_break_fsm_async_reply_reason (struct thread_fsm *self)
11726{
11727 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11728}
11729
c906108c 11730void
ae66c1fc 11731until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11732{
11733 struct symtabs_and_lines sals;
11734 struct symtab_and_line sal;
8556afb4
PA
11735 struct frame_info *frame;
11736 struct gdbarch *frame_gdbarch;
11737 struct frame_id stack_frame_id;
11738 struct frame_id caller_frame_id;
cfc31633
PA
11739 struct breakpoint *location_breakpoint;
11740 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11741 struct cleanup *old_chain, *cleanup;
186c406b
TT
11742 int thread;
11743 struct thread_info *tp;
f00aae0f 11744 struct event_location *location;
cfc31633 11745 struct until_break_fsm *sm;
c906108c 11746
70509625 11747 clear_proceed_status (0);
c906108c
SS
11748
11749 /* Set a breakpoint where the user wants it and at return from
4a64f543 11750 this function. */
c5aa993b 11751
f00aae0f
KS
11752 location = string_to_event_location (&arg, current_language);
11753 cleanup = make_cleanup_delete_event_location (location);
11754
1bfeeb0f 11755 if (last_displayed_sal_is_valid ())
c2f4122d 11756 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11757 get_last_displayed_symtab (),
f8eba3c6 11758 get_last_displayed_line ());
c906108c 11759 else
f00aae0f 11760 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11761 NULL, (struct symtab *) NULL, 0);
c5aa993b 11762
c906108c 11763 if (sals.nelts != 1)
8a3fe4f8 11764 error (_("Couldn't get information on specified line."));
c5aa993b 11765
c906108c 11766 sal = sals.sals[0];
4a64f543 11767 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11768
c906108c 11769 if (*arg)
8a3fe4f8 11770 error (_("Junk at end of arguments."));
c5aa993b 11771
c906108c 11772 resolve_sal_pc (&sal);
c5aa993b 11773
186c406b 11774 tp = inferior_thread ();
5d5658a1 11775 thread = tp->global_num;
186c406b 11776
883bc8d1
PA
11777 old_chain = make_cleanup (null_cleanup, NULL);
11778
8556afb4
PA
11779 /* Note linespec handling above invalidates the frame chain.
11780 Installing a breakpoint also invalidates the frame chain (as it
11781 may need to switch threads), so do any frame handling before
11782 that. */
11783
11784 frame = get_selected_frame (NULL);
11785 frame_gdbarch = get_frame_arch (frame);
11786 stack_frame_id = get_stack_frame_id (frame);
11787 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11788
ae66c1fc
EZ
11789 /* Keep within the current frame, or in frames called by the current
11790 one. */
edb3359d 11791
883bc8d1 11792 if (frame_id_p (caller_frame_id))
c906108c 11793 {
883bc8d1 11794 struct symtab_and_line sal2;
cfc31633 11795 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11796
11797 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11798 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11799 caller_gdbarch = frame_unwind_caller_arch (frame);
11800 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11801 sal2,
11802 caller_frame_id,
11803 bp_until);
11804 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11805
883bc8d1 11806 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11807 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11808 }
c5aa993b 11809
c70a6932
JK
11810 /* set_momentary_breakpoint could invalidate FRAME. */
11811 frame = NULL;
11812
883bc8d1
PA
11813 if (anywhere)
11814 /* If the user told us to continue until a specified location,
11815 we don't specify a frame at which we need to stop. */
cfc31633
PA
11816 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11817 null_frame_id, bp_until);
883bc8d1
PA
11818 else
11819 /* Otherwise, specify the selected frame, because we want to stop
11820 only at the very same frame. */
cfc31633
PA
11821 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11822 stack_frame_id, bp_until);
11823 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11824
8980e177 11825 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11826 location_breakpoint, caller_breakpoint);
cfc31633 11827 tp->thread_fsm = &sm->thread_fsm;
f107f563 11828
cfc31633 11829 discard_cleanups (old_chain);
f107f563 11830
cfc31633 11831 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11832
11833 do_cleanups (cleanup);
c906108c 11834}
ae66c1fc 11835
c906108c
SS
11836/* This function attempts to parse an optional "if <cond>" clause
11837 from the arg string. If one is not found, it returns NULL.
c5aa993b 11838
c906108c
SS
11839 Else, it returns a pointer to the condition string. (It does not
11840 attempt to evaluate the string against a particular block.) And,
11841 it updates arg to point to the first character following the parsed
4a64f543 11842 if clause in the arg string. */
53a5351d 11843
916703c0 11844char *
fba45db2 11845ep_parse_optional_if_clause (char **arg)
c906108c 11846{
c5aa993b
JM
11847 char *cond_string;
11848
11849 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11850 return NULL;
c5aa993b 11851
4a64f543 11852 /* Skip the "if" keyword. */
c906108c 11853 (*arg) += 2;
c5aa993b 11854
c906108c 11855 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11856 condition string. */
e9cafbcc 11857 *arg = skip_spaces (*arg);
c906108c 11858 cond_string = *arg;
c5aa993b 11859
4a64f543
MS
11860 /* Assume that the condition occupies the remainder of the arg
11861 string. */
c906108c 11862 (*arg) += strlen (cond_string);
c5aa993b 11863
c906108c
SS
11864 return cond_string;
11865}
c5aa993b 11866
c906108c
SS
11867/* Commands to deal with catching events, such as signals, exceptions,
11868 process start/exit, etc. */
c5aa993b
JM
11869
11870typedef enum
11871{
44feb3ce
TT
11872 catch_fork_temporary, catch_vfork_temporary,
11873 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11874}
11875catch_fork_kind;
11876
c906108c 11877static void
cc59ec59
MS
11878catch_fork_command_1 (char *arg, int from_tty,
11879 struct cmd_list_element *command)
c906108c 11880{
a6d9a66e 11881 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11882 char *cond_string = NULL;
44feb3ce
TT
11883 catch_fork_kind fork_kind;
11884 int tempflag;
11885
11886 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11887 tempflag = (fork_kind == catch_fork_temporary
11888 || fork_kind == catch_vfork_temporary);
c5aa993b 11889
44feb3ce
TT
11890 if (!arg)
11891 arg = "";
e9cafbcc 11892 arg = skip_spaces (arg);
c5aa993b 11893
c906108c 11894 /* The allowed syntax is:
c5aa993b
JM
11895 catch [v]fork
11896 catch [v]fork if <cond>
11897
4a64f543 11898 First, check if there's an if clause. */
c906108c 11899 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11900
c906108c 11901 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11902 error (_("Junk at end of arguments."));
c5aa993b 11903
c906108c 11904 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11905 and enable reporting of such events. */
c5aa993b
JM
11906 switch (fork_kind)
11907 {
44feb3ce
TT
11908 case catch_fork_temporary:
11909 case catch_fork_permanent:
a6d9a66e 11910 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11911 &catch_fork_breakpoint_ops);
c906108c 11912 break;
44feb3ce
TT
11913 case catch_vfork_temporary:
11914 case catch_vfork_permanent:
a6d9a66e 11915 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11916 &catch_vfork_breakpoint_ops);
c906108c 11917 break;
c5aa993b 11918 default:
8a3fe4f8 11919 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11920 break;
c5aa993b 11921 }
c906108c
SS
11922}
11923
11924static void
cc59ec59
MS
11925catch_exec_command_1 (char *arg, int from_tty,
11926 struct cmd_list_element *command)
c906108c 11927{
b4d90040 11928 struct exec_catchpoint *c;
a6d9a66e 11929 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11930 int tempflag;
c5aa993b 11931 char *cond_string = NULL;
c906108c 11932
44feb3ce
TT
11933 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11934
11935 if (!arg)
11936 arg = "";
e9cafbcc 11937 arg = skip_spaces (arg);
c906108c
SS
11938
11939 /* The allowed syntax is:
c5aa993b
JM
11940 catch exec
11941 catch exec if <cond>
c906108c 11942
4a64f543 11943 First, check if there's an if clause. */
c906108c
SS
11944 cond_string = ep_parse_optional_if_clause (&arg);
11945
11946 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11947 error (_("Junk at end of arguments."));
c906108c 11948
b4d90040
PA
11949 c = XNEW (struct exec_catchpoint);
11950 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11951 &catch_exec_breakpoint_ops);
11952 c->exec_pathname = NULL;
11953
3ea46bff 11954 install_breakpoint (0, &c->base, 1);
c906108c 11955}
c5aa993b 11956
9ac4176b 11957void
28010a5d
PA
11958init_ada_exception_breakpoint (struct breakpoint *b,
11959 struct gdbarch *gdbarch,
11960 struct symtab_and_line sal,
11961 char *addr_string,
c0a91b2b 11962 const struct breakpoint_ops *ops,
28010a5d 11963 int tempflag,
349774ef 11964 int enabled,
28010a5d 11965 int from_tty)
f7f9143b 11966{
f7f9143b
JB
11967 if (from_tty)
11968 {
5af949e3
UW
11969 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11970 if (!loc_gdbarch)
11971 loc_gdbarch = gdbarch;
11972
6c95b8df
PA
11973 describe_other_breakpoints (loc_gdbarch,
11974 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11975 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11976 version for exception catchpoints, because two catchpoints
11977 used for different exception names will use the same address.
11978 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11979 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11980 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11981 the user what type of catchpoint it is. The above is good
11982 enough for now, though. */
11983 }
11984
28010a5d 11985 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11986
349774ef 11987 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11988 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11989 b->location = string_to_event_location (&addr_string,
11990 language_def (language_ada));
f7f9143b 11991 b->language = language_ada;
f7f9143b
JB
11992}
11993
c906108c 11994static void
fba45db2 11995catch_command (char *arg, int from_tty)
c906108c 11996{
44feb3ce 11997 error (_("Catch requires an event name."));
c906108c
SS
11998}
11999\f
12000
12001static void
fba45db2 12002tcatch_command (char *arg, int from_tty)
c906108c 12003{
44feb3ce 12004 error (_("Catch requires an event name."));
c906108c
SS
12005}
12006
8a2c437b
TT
12007/* A qsort comparison function that sorts breakpoints in order. */
12008
12009static int
12010compare_breakpoints (const void *a, const void *b)
12011{
9a3c8263 12012 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 12013 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 12014 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
12015 uintptr_t ub = (uintptr_t) *bb;
12016
12017 if ((*ba)->number < (*bb)->number)
12018 return -1;
12019 else if ((*ba)->number > (*bb)->number)
12020 return 1;
12021
12022 /* Now sort by address, in case we see, e..g, two breakpoints with
12023 the number 0. */
12024 if (ua < ub)
12025 return -1;
94b0e70d 12026 return ua > ub ? 1 : 0;
8a2c437b
TT
12027}
12028
80f8a6eb 12029/* Delete breakpoints by address or line. */
c906108c
SS
12030
12031static void
fba45db2 12032clear_command (char *arg, int from_tty)
c906108c 12033{
8a2c437b 12034 struct breakpoint *b, *prev;
d6e956e5
VP
12035 VEC(breakpoint_p) *found = 0;
12036 int ix;
c906108c
SS
12037 int default_match;
12038 struct symtabs_and_lines sals;
12039 struct symtab_and_line sal;
c906108c 12040 int i;
8a2c437b 12041 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12042
12043 if (arg)
12044 {
39cf75f7
DE
12045 sals = decode_line_with_current_source (arg,
12046 (DECODE_LINE_FUNFIRSTLINE
12047 | DECODE_LINE_LIST_MODE));
cf4ded82 12048 make_cleanup (xfree, sals.sals);
c906108c
SS
12049 default_match = 0;
12050 }
12051 else
12052 {
8d749320 12053 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12054 make_cleanup (xfree, sals.sals);
4a64f543 12055 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12056
12057 /* Set sal's line, symtab, pc, and pspace to the values
12058 corresponding to the last call to print_frame_info. If the
12059 codepoint is not valid, this will set all the fields to 0. */
12060 get_last_displayed_sal (&sal);
c906108c 12061 if (sal.symtab == 0)
8a3fe4f8 12062 error (_("No source file specified."));
c906108c
SS
12063
12064 sals.sals[0] = sal;
12065 sals.nelts = 1;
12066
12067 default_match = 1;
12068 }
12069
4a64f543
MS
12070 /* We don't call resolve_sal_pc here. That's not as bad as it
12071 seems, because all existing breakpoints typically have both
12072 file/line and pc set. So, if clear is given file/line, we can
12073 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12074
12075 We only support clearing given the address explicitly
12076 present in breakpoint table. Say, we've set breakpoint
4a64f543 12077 at file:line. There were several PC values for that file:line,
ed0616c6 12078 due to optimization, all in one block.
4a64f543
MS
12079
12080 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12081 PC corresponding to the same file:line, the breakpoint won't
12082 be cleared. We probably can still clear the breakpoint, but
12083 since the other PC value is never presented to user, user
12084 can only find it by guessing, and it does not seem important
12085 to support that. */
12086
4a64f543
MS
12087 /* For each line spec given, delete bps which correspond to it. Do
12088 it in two passes, solely to preserve the current behavior that
12089 from_tty is forced true if we delete more than one
12090 breakpoint. */
c906108c 12091
80f8a6eb 12092 found = NULL;
8a2c437b 12093 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12094 for (i = 0; i < sals.nelts; i++)
12095 {
05cba821
JK
12096 const char *sal_fullname;
12097
c906108c 12098 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12099 If line given (pc == 0), clear all bpts on specified line.
12100 If defaulting, clear all bpts on default line
c906108c 12101 or at default pc.
c5aa993b
JM
12102
12103 defaulting sal.pc != 0 tests to do
12104
12105 0 1 pc
12106 1 1 pc _and_ line
12107 0 0 line
12108 1 0 <can't happen> */
c906108c
SS
12109
12110 sal = sals.sals[i];
05cba821
JK
12111 sal_fullname = (sal.symtab == NULL
12112 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12113
4a64f543 12114 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12115 ALL_BREAKPOINTS (b)
c5aa993b 12116 {
0d381245 12117 int match = 0;
4a64f543 12118 /* Are we going to delete b? */
cc60f2e3 12119 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12120 {
12121 struct bp_location *loc = b->loc;
12122 for (; loc; loc = loc->next)
12123 {
f8eba3c6
TT
12124 /* If the user specified file:line, don't allow a PC
12125 match. This matches historical gdb behavior. */
12126 int pc_match = (!sal.explicit_line
12127 && sal.pc
12128 && (loc->pspace == sal.pspace)
12129 && (loc->address == sal.pc)
12130 && (!section_is_overlay (loc->section)
12131 || loc->section == sal.section));
4aac40c8
TT
12132 int line_match = 0;
12133
12134 if ((default_match || sal.explicit_line)
2f202fde 12135 && loc->symtab != NULL
05cba821 12136 && sal_fullname != NULL
4aac40c8 12137 && sal.pspace == loc->pspace
05cba821
JK
12138 && loc->line_number == sal.line
12139 && filename_cmp (symtab_to_fullname (loc->symtab),
12140 sal_fullname) == 0)
12141 line_match = 1;
4aac40c8 12142
0d381245
VP
12143 if (pc_match || line_match)
12144 {
12145 match = 1;
12146 break;
12147 }
12148 }
12149 }
12150
12151 if (match)
d6e956e5 12152 VEC_safe_push(breakpoint_p, found, b);
c906108c 12153 }
80f8a6eb 12154 }
8a2c437b 12155
80f8a6eb 12156 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12157 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12158 {
12159 if (arg)
8a3fe4f8 12160 error (_("No breakpoint at %s."), arg);
80f8a6eb 12161 else
8a3fe4f8 12162 error (_("No breakpoint at this line."));
80f8a6eb 12163 }
c906108c 12164
8a2c437b
TT
12165 /* Remove duplicates from the vec. */
12166 qsort (VEC_address (breakpoint_p, found),
12167 VEC_length (breakpoint_p, found),
12168 sizeof (breakpoint_p),
12169 compare_breakpoints);
12170 prev = VEC_index (breakpoint_p, found, 0);
12171 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12172 {
12173 if (b == prev)
12174 {
12175 VEC_ordered_remove (breakpoint_p, found, ix);
12176 --ix;
12177 }
12178 }
12179
d6e956e5 12180 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12181 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12182 if (from_tty)
a3f17187 12183 {
d6e956e5 12184 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12185 printf_unfiltered (_("Deleted breakpoint "));
12186 else
12187 printf_unfiltered (_("Deleted breakpoints "));
12188 }
d6e956e5
VP
12189
12190 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12191 {
c5aa993b 12192 if (from_tty)
d6e956e5
VP
12193 printf_unfiltered ("%d ", b->number);
12194 delete_breakpoint (b);
c906108c 12195 }
80f8a6eb
MS
12196 if (from_tty)
12197 putchar_unfiltered ('\n');
8a2c437b
TT
12198
12199 do_cleanups (cleanups);
c906108c
SS
12200}
12201\f
12202/* Delete breakpoint in BS if they are `delete' breakpoints and
12203 all breakpoints that are marked for deletion, whether hit or not.
12204 This is called after any breakpoint is hit, or after errors. */
12205
12206void
fba45db2 12207breakpoint_auto_delete (bpstat bs)
c906108c 12208{
35df4500 12209 struct breakpoint *b, *b_tmp;
c906108c
SS
12210
12211 for (; bs; bs = bs->next)
f431efe5
PA
12212 if (bs->breakpoint_at
12213 && bs->breakpoint_at->disposition == disp_del
c906108c 12214 && bs->stop)
f431efe5 12215 delete_breakpoint (bs->breakpoint_at);
c906108c 12216
35df4500 12217 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12218 {
b5de0fa7 12219 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12220 delete_breakpoint (b);
12221 }
c906108c
SS
12222}
12223
4a64f543
MS
12224/* A comparison function for bp_location AP and BP being interfaced to
12225 qsort. Sort elements primarily by their ADDRESS (no matter what
12226 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12227 secondarily by ordering first permanent elements and
4a64f543 12228 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12229 qsort being an unstable algorithm. */
876fa593
JK
12230
12231static int
494cfb0f 12232bp_location_compare (const void *ap, const void *bp)
876fa593 12233{
9a3c8263
SM
12234 const struct bp_location *a = *(const struct bp_location **) ap;
12235 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12236
12237 if (a->address != b->address)
12238 return (a->address > b->address) - (a->address < b->address);
12239
dea2aa5f
LM
12240 /* Sort locations at the same address by their pspace number, keeping
12241 locations of the same inferior (in a multi-inferior environment)
12242 grouped. */
12243
12244 if (a->pspace->num != b->pspace->num)
12245 return ((a->pspace->num > b->pspace->num)
12246 - (a->pspace->num < b->pspace->num));
12247
876fa593 12248 /* Sort permanent breakpoints first. */
1a853c52
PA
12249 if (a->permanent != b->permanent)
12250 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12251
c56a97f9
JK
12252 /* Make the internal GDB representation stable across GDB runs
12253 where A and B memory inside GDB can differ. Breakpoint locations of
12254 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12255
12256 if (a->owner->number != b->owner->number)
c56a97f9
JK
12257 return ((a->owner->number > b->owner->number)
12258 - (a->owner->number < b->owner->number));
876fa593
JK
12259
12260 return (a > b) - (a < b);
12261}
12262
876fa593 12263/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12264 bp_location_shadow_len_after_address_max according to the current
12265 content of the bp_location array. */
f7545552
TT
12266
12267static void
876fa593 12268bp_location_target_extensions_update (void)
f7545552 12269{
876fa593
JK
12270 struct bp_location *bl, **blp_tmp;
12271
12272 bp_location_placed_address_before_address_max = 0;
12273 bp_location_shadow_len_after_address_max = 0;
12274
12275 ALL_BP_LOCATIONS (bl, blp_tmp)
12276 {
12277 CORE_ADDR start, end, addr;
12278
12279 if (!bp_location_has_shadow (bl))
12280 continue;
12281
12282 start = bl->target_info.placed_address;
12283 end = start + bl->target_info.shadow_len;
12284
12285 gdb_assert (bl->address >= start);
12286 addr = bl->address - start;
12287 if (addr > bp_location_placed_address_before_address_max)
12288 bp_location_placed_address_before_address_max = addr;
12289
12290 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12291
12292 gdb_assert (bl->address < end);
12293 addr = end - bl->address;
12294 if (addr > bp_location_shadow_len_after_address_max)
12295 bp_location_shadow_len_after_address_max = addr;
12296 }
f7545552
TT
12297}
12298
1e4d1764
YQ
12299/* Download tracepoint locations if they haven't been. */
12300
12301static void
12302download_tracepoint_locations (void)
12303{
7ed2c994 12304 struct breakpoint *b;
1e4d1764 12305 struct cleanup *old_chain;
dd2e65cc 12306 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12307
12308 old_chain = save_current_space_and_thread ();
12309
7ed2c994 12310 ALL_TRACEPOINTS (b)
1e4d1764 12311 {
7ed2c994 12312 struct bp_location *bl;
1e4d1764 12313 struct tracepoint *t;
f2a8bc8a 12314 int bp_location_downloaded = 0;
1e4d1764 12315
7ed2c994 12316 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12317 ? !may_insert_fast_tracepoints
12318 : !may_insert_tracepoints))
12319 continue;
12320
dd2e65cc
YQ
12321 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12322 {
12323 if (target_can_download_tracepoint ())
12324 can_download_tracepoint = TRIBOOL_TRUE;
12325 else
12326 can_download_tracepoint = TRIBOOL_FALSE;
12327 }
12328
12329 if (can_download_tracepoint == TRIBOOL_FALSE)
12330 break;
12331
7ed2c994
YQ
12332 for (bl = b->loc; bl; bl = bl->next)
12333 {
12334 /* In tracepoint, locations are _never_ duplicated, so
12335 should_be_inserted is equivalent to
12336 unduplicated_should_be_inserted. */
12337 if (!should_be_inserted (bl) || bl->inserted)
12338 continue;
1e4d1764 12339
7ed2c994 12340 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12341
7ed2c994 12342 target_download_tracepoint (bl);
1e4d1764 12343
7ed2c994 12344 bl->inserted = 1;
f2a8bc8a 12345 bp_location_downloaded = 1;
7ed2c994
YQ
12346 }
12347 t = (struct tracepoint *) b;
12348 t->number_on_target = b->number;
f2a8bc8a
YQ
12349 if (bp_location_downloaded)
12350 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12351 }
12352
12353 do_cleanups (old_chain);
12354}
12355
934709f0
PW
12356/* Swap the insertion/duplication state between two locations. */
12357
12358static void
12359swap_insertion (struct bp_location *left, struct bp_location *right)
12360{
12361 const int left_inserted = left->inserted;
12362 const int left_duplicate = left->duplicate;
b775012e 12363 const int left_needs_update = left->needs_update;
934709f0
PW
12364 const struct bp_target_info left_target_info = left->target_info;
12365
1e4d1764
YQ
12366 /* Locations of tracepoints can never be duplicated. */
12367 if (is_tracepoint (left->owner))
12368 gdb_assert (!left->duplicate);
12369 if (is_tracepoint (right->owner))
12370 gdb_assert (!right->duplicate);
12371
934709f0
PW
12372 left->inserted = right->inserted;
12373 left->duplicate = right->duplicate;
b775012e 12374 left->needs_update = right->needs_update;
934709f0
PW
12375 left->target_info = right->target_info;
12376 right->inserted = left_inserted;
12377 right->duplicate = left_duplicate;
b775012e 12378 right->needs_update = left_needs_update;
934709f0
PW
12379 right->target_info = left_target_info;
12380}
12381
b775012e
LM
12382/* Force the re-insertion of the locations at ADDRESS. This is called
12383 once a new/deleted/modified duplicate location is found and we are evaluating
12384 conditions on the target's side. Such conditions need to be updated on
12385 the target. */
12386
12387static void
12388force_breakpoint_reinsertion (struct bp_location *bl)
12389{
12390 struct bp_location **locp = NULL, **loc2p;
12391 struct bp_location *loc;
12392 CORE_ADDR address = 0;
12393 int pspace_num;
12394
12395 address = bl->address;
12396 pspace_num = bl->pspace->num;
12397
12398 /* This is only meaningful if the target is
12399 evaluating conditions and if the user has
12400 opted for condition evaluation on the target's
12401 side. */
12402 if (gdb_evaluates_breakpoint_condition_p ()
12403 || !target_supports_evaluation_of_breakpoint_conditions ())
12404 return;
12405
12406 /* Flag all breakpoint locations with this address and
12407 the same program space as the location
12408 as "its condition has changed". We need to
12409 update the conditions on the target's side. */
12410 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12411 {
12412 loc = *loc2p;
12413
12414 if (!is_breakpoint (loc->owner)
12415 || pspace_num != loc->pspace->num)
12416 continue;
12417
12418 /* Flag the location appropriately. We use a different state to
12419 let everyone know that we already updated the set of locations
12420 with addr bl->address and program space bl->pspace. This is so
12421 we don't have to keep calling these functions just to mark locations
12422 that have already been marked. */
12423 loc->condition_changed = condition_updated;
12424
12425 /* Free the agent expression bytecode as well. We will compute
12426 it later on. */
12427 if (loc->cond_bytecode)
12428 {
12429 free_agent_expr (loc->cond_bytecode);
12430 loc->cond_bytecode = NULL;
12431 }
12432 }
12433}
44702360
PA
12434/* Called whether new breakpoints are created, or existing breakpoints
12435 deleted, to update the global location list and recompute which
12436 locations are duplicate of which.
b775012e 12437
04086b45
PA
12438 The INSERT_MODE flag determines whether locations may not, may, or
12439 shall be inserted now. See 'enum ugll_insert_mode' for more
12440 info. */
b60e7edf 12441
0d381245 12442static void
44702360 12443update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12444{
74960c60 12445 struct breakpoint *b;
876fa593 12446 struct bp_location **locp, *loc;
f7545552 12447 struct cleanup *cleanups;
b775012e
LM
12448 /* Last breakpoint location address that was marked for update. */
12449 CORE_ADDR last_addr = 0;
12450 /* Last breakpoint location program space that was marked for update. */
12451 int last_pspace_num = -1;
f7545552 12452
2d134ed3
PA
12453 /* Used in the duplicates detection below. When iterating over all
12454 bp_locations, points to the first bp_location of a given address.
12455 Breakpoints and watchpoints of different types are never
12456 duplicates of each other. Keep one pointer for each type of
12457 breakpoint/watchpoint, so we only need to loop over all locations
12458 once. */
12459 struct bp_location *bp_loc_first; /* breakpoint */
12460 struct bp_location *wp_loc_first; /* hardware watchpoint */
12461 struct bp_location *awp_loc_first; /* access watchpoint */
12462 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12463
4a64f543
MS
12464 /* Saved former bp_location array which we compare against the newly
12465 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12466 struct bp_location **old_location, **old_locp;
12467 unsigned old_location_count;
12468
12469 old_location = bp_location;
12470 old_location_count = bp_location_count;
12471 bp_location = NULL;
12472 bp_location_count = 0;
12473 cleanups = make_cleanup (xfree, old_location);
0d381245 12474
74960c60 12475 ALL_BREAKPOINTS (b)
876fa593
JK
12476 for (loc = b->loc; loc; loc = loc->next)
12477 bp_location_count++;
12478
8d749320 12479 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12480 locp = bp_location;
12481 ALL_BREAKPOINTS (b)
12482 for (loc = b->loc; loc; loc = loc->next)
12483 *locp++ = loc;
12484 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12485 bp_location_compare);
876fa593
JK
12486
12487 bp_location_target_extensions_update ();
74960c60 12488
4a64f543
MS
12489 /* Identify bp_location instances that are no longer present in the
12490 new list, and therefore should be freed. Note that it's not
12491 necessary that those locations should be removed from inferior --
12492 if there's another location at the same address (previously
12493 marked as duplicate), we don't need to remove/insert the
12494 location.
876fa593 12495
4a64f543
MS
12496 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12497 and former bp_location array state respectively. */
876fa593
JK
12498
12499 locp = bp_location;
12500 for (old_locp = old_location; old_locp < old_location + old_location_count;
12501 old_locp++)
74960c60 12502 {
876fa593 12503 struct bp_location *old_loc = *old_locp;
c7d46a38 12504 struct bp_location **loc2p;
876fa593 12505
e5dd4106 12506 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12507 not, we have to free it. */
c7d46a38 12508 int found_object = 0;
20874c92
VP
12509 /* Tells if the location should remain inserted in the target. */
12510 int keep_in_target = 0;
12511 int removed = 0;
876fa593 12512
4a64f543
MS
12513 /* Skip LOCP entries which will definitely never be needed.
12514 Stop either at or being the one matching OLD_LOC. */
876fa593 12515 while (locp < bp_location + bp_location_count
c7d46a38 12516 && (*locp)->address < old_loc->address)
876fa593 12517 locp++;
c7d46a38
PA
12518
12519 for (loc2p = locp;
12520 (loc2p < bp_location + bp_location_count
12521 && (*loc2p)->address == old_loc->address);
12522 loc2p++)
12523 {
b775012e
LM
12524 /* Check if this is a new/duplicated location or a duplicated
12525 location that had its condition modified. If so, we want to send
12526 its condition to the target if evaluation of conditions is taking
12527 place there. */
12528 if ((*loc2p)->condition_changed == condition_modified
12529 && (last_addr != old_loc->address
12530 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12531 {
b775012e
LM
12532 force_breakpoint_reinsertion (*loc2p);
12533 last_pspace_num = old_loc->pspace->num;
c7d46a38 12534 }
b775012e
LM
12535
12536 if (*loc2p == old_loc)
12537 found_object = 1;
c7d46a38 12538 }
74960c60 12539
b775012e
LM
12540 /* We have already handled this address, update it so that we don't
12541 have to go through updates again. */
12542 last_addr = old_loc->address;
12543
12544 /* Target-side condition evaluation: Handle deleted locations. */
12545 if (!found_object)
12546 force_breakpoint_reinsertion (old_loc);
12547
4a64f543
MS
12548 /* If this location is no longer present, and inserted, look if
12549 there's maybe a new location at the same address. If so,
12550 mark that one inserted, and don't remove this one. This is
12551 needed so that we don't have a time window where a breakpoint
12552 at certain location is not inserted. */
74960c60 12553
876fa593 12554 if (old_loc->inserted)
0d381245 12555 {
4a64f543
MS
12556 /* If the location is inserted now, we might have to remove
12557 it. */
74960c60 12558
876fa593 12559 if (found_object && should_be_inserted (old_loc))
74960c60 12560 {
4a64f543
MS
12561 /* The location is still present in the location list,
12562 and still should be inserted. Don't do anything. */
20874c92 12563 keep_in_target = 1;
74960c60
VP
12564 }
12565 else
12566 {
b775012e
LM
12567 /* This location still exists, but it won't be kept in the
12568 target since it may have been disabled. We proceed to
12569 remove its target-side condition. */
12570
4a64f543
MS
12571 /* The location is either no longer present, or got
12572 disabled. See if there's another location at the
12573 same address, in which case we don't need to remove
12574 this one from the target. */
876fa593 12575
2bdf28a0 12576 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12577 if (breakpoint_address_is_meaningful (old_loc->owner))
12578 {
876fa593 12579 for (loc2p = locp;
c7d46a38
PA
12580 (loc2p < bp_location + bp_location_count
12581 && (*loc2p)->address == old_loc->address);
876fa593
JK
12582 loc2p++)
12583 {
12584 struct bp_location *loc2 = *loc2p;
12585
2d134ed3 12586 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12587 {
85d721b8
PA
12588 /* Read watchpoint locations are switched to
12589 access watchpoints, if the former are not
12590 supported, but the latter are. */
12591 if (is_hardware_watchpoint (old_loc->owner))
12592 {
12593 gdb_assert (is_hardware_watchpoint (loc2->owner));
12594 loc2->watchpoint_type = old_loc->watchpoint_type;
12595 }
12596
934709f0
PW
12597 /* loc2 is a duplicated location. We need to check
12598 if it should be inserted in case it will be
12599 unduplicated. */
12600 if (loc2 != old_loc
12601 && unduplicated_should_be_inserted (loc2))
c7d46a38 12602 {
934709f0 12603 swap_insertion (old_loc, loc2);
c7d46a38
PA
12604 keep_in_target = 1;
12605 break;
12606 }
876fa593
JK
12607 }
12608 }
12609 }
74960c60
VP
12610 }
12611
20874c92
VP
12612 if (!keep_in_target)
12613 {
834c0d03 12614 if (remove_breakpoint (old_loc))
20874c92 12615 {
4a64f543
MS
12616 /* This is just about all we can do. We could keep
12617 this location on the global list, and try to
12618 remove it next time, but there's no particular
12619 reason why we will succeed next time.
20874c92 12620
4a64f543
MS
12621 Note that at this point, old_loc->owner is still
12622 valid, as delete_breakpoint frees the breakpoint
12623 only after calling us. */
3e43a32a
MS
12624 printf_filtered (_("warning: Error removing "
12625 "breakpoint %d\n"),
876fa593 12626 old_loc->owner->number);
20874c92
VP
12627 }
12628 removed = 1;
12629 }
0d381245 12630 }
74960c60
VP
12631
12632 if (!found_object)
1c5cfe86 12633 {
fbea99ea 12634 if (removed && target_is_non_stop_p ()
1cf4d951 12635 && need_moribund_for_location_type (old_loc))
20874c92 12636 {
db82e815
PA
12637 /* This location was removed from the target. In
12638 non-stop mode, a race condition is possible where
12639 we've removed a breakpoint, but stop events for that
12640 breakpoint are already queued and will arrive later.
12641 We apply an heuristic to be able to distinguish such
12642 SIGTRAPs from other random SIGTRAPs: we keep this
12643 breakpoint location for a bit, and will retire it
12644 after we see some number of events. The theory here
12645 is that reporting of events should, "on the average",
12646 be fair, so after a while we'll see events from all
12647 threads that have anything of interest, and no longer
12648 need to keep this breakpoint location around. We
12649 don't hold locations forever so to reduce chances of
12650 mistaking a non-breakpoint SIGTRAP for a breakpoint
12651 SIGTRAP.
12652
12653 The heuristic failing can be disastrous on
12654 decr_pc_after_break targets.
12655
12656 On decr_pc_after_break targets, like e.g., x86-linux,
12657 if we fail to recognize a late breakpoint SIGTRAP,
12658 because events_till_retirement has reached 0 too
12659 soon, we'll fail to do the PC adjustment, and report
12660 a random SIGTRAP to the user. When the user resumes
12661 the inferior, it will most likely immediately crash
2dec564e 12662 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12663 corrupted, because of being resumed e.g., in the
12664 middle of a multi-byte instruction, or skipped a
12665 one-byte instruction. This was actually seen happen
12666 on native x86-linux, and should be less rare on
12667 targets that do not support new thread events, like
12668 remote, due to the heuristic depending on
12669 thread_count.
12670
12671 Mistaking a random SIGTRAP for a breakpoint trap
12672 causes similar symptoms (PC adjustment applied when
12673 it shouldn't), but then again, playing with SIGTRAPs
12674 behind the debugger's back is asking for trouble.
12675
12676 Since hardware watchpoint traps are always
12677 distinguishable from other traps, so we don't need to
12678 apply keep hardware watchpoint moribund locations
12679 around. We simply always ignore hardware watchpoint
12680 traps we can no longer explain. */
12681
876fa593
JK
12682 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12683 old_loc->owner = NULL;
20874c92 12684
876fa593 12685 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12686 }
12687 else
f431efe5
PA
12688 {
12689 old_loc->owner = NULL;
12690 decref_bp_location (&old_loc);
12691 }
20874c92 12692 }
74960c60 12693 }
1c5cfe86 12694
348d480f
PA
12695 /* Rescan breakpoints at the same address and section, marking the
12696 first one as "first" and any others as "duplicates". This is so
12697 that the bpt instruction is only inserted once. If we have a
12698 permanent breakpoint at the same place as BPT, make that one the
12699 official one, and the rest as duplicates. Permanent breakpoints
12700 are sorted first for the same address.
12701
12702 Do the same for hardware watchpoints, but also considering the
12703 watchpoint's type (regular/access/read) and length. */
12704
12705 bp_loc_first = NULL;
12706 wp_loc_first = NULL;
12707 awp_loc_first = NULL;
12708 rwp_loc_first = NULL;
12709 ALL_BP_LOCATIONS (loc, locp)
12710 {
12711 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12712 non-NULL. */
348d480f 12713 struct bp_location **loc_first_p;
d3fbdd86 12714 b = loc->owner;
348d480f 12715
6f380991 12716 if (!unduplicated_should_be_inserted (loc)
348d480f 12717 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12718 /* Don't detect duplicate for tracepoint locations because they are
12719 never duplicated. See the comments in field `duplicate' of
12720 `struct bp_location'. */
348d480f 12721 || is_tracepoint (b))
b775012e
LM
12722 {
12723 /* Clear the condition modification flag. */
12724 loc->condition_changed = condition_unchanged;
12725 continue;
12726 }
348d480f 12727
348d480f
PA
12728 if (b->type == bp_hardware_watchpoint)
12729 loc_first_p = &wp_loc_first;
12730 else if (b->type == bp_read_watchpoint)
12731 loc_first_p = &rwp_loc_first;
12732 else if (b->type == bp_access_watchpoint)
12733 loc_first_p = &awp_loc_first;
12734 else
12735 loc_first_p = &bp_loc_first;
12736
12737 if (*loc_first_p == NULL
12738 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12739 || !breakpoint_locations_match (loc, *loc_first_p))
12740 {
12741 *loc_first_p = loc;
12742 loc->duplicate = 0;
b775012e
LM
12743
12744 if (is_breakpoint (loc->owner) && loc->condition_changed)
12745 {
12746 loc->needs_update = 1;
12747 /* Clear the condition modification flag. */
12748 loc->condition_changed = condition_unchanged;
12749 }
348d480f
PA
12750 continue;
12751 }
12752
934709f0
PW
12753
12754 /* This and the above ensure the invariant that the first location
12755 is not duplicated, and is the inserted one.
12756 All following are marked as duplicated, and are not inserted. */
12757 if (loc->inserted)
12758 swap_insertion (loc, *loc_first_p);
348d480f
PA
12759 loc->duplicate = 1;
12760
b775012e
LM
12761 /* Clear the condition modification flag. */
12762 loc->condition_changed = condition_unchanged;
348d480f
PA
12763 }
12764
a25a5a45 12765 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12766 {
04086b45 12767 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12768 insert_breakpoint_locations ();
12769 else
12770 {
44702360
PA
12771 /* Even though the caller told us to not insert new
12772 locations, we may still need to update conditions on the
12773 target's side of breakpoints that were already inserted
12774 if the target is evaluating breakpoint conditions. We
b775012e
LM
12775 only update conditions for locations that are marked
12776 "needs_update". */
12777 update_inserted_breakpoint_locations ();
12778 }
12779 }
348d480f 12780
04086b45 12781 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12782 download_tracepoint_locations ();
12783
348d480f
PA
12784 do_cleanups (cleanups);
12785}
12786
12787void
12788breakpoint_retire_moribund (void)
12789{
12790 struct bp_location *loc;
12791 int ix;
12792
12793 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12794 if (--(loc->events_till_retirement) == 0)
12795 {
12796 decref_bp_location (&loc);
12797 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12798 --ix;
12799 }
12800}
12801
12802static void
44702360 12803update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12804{
348d480f 12805
492d29ea
PA
12806 TRY
12807 {
12808 update_global_location_list (insert_mode);
12809 }
12810 CATCH (e, RETURN_MASK_ERROR)
12811 {
12812 }
12813 END_CATCH
348d480f
PA
12814}
12815
12816/* Clear BKP from a BPS. */
12817
12818static void
12819bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12820{
12821 bpstat bs;
12822
12823 for (bs = bps; bs; bs = bs->next)
12824 if (bs->breakpoint_at == bpt)
12825 {
12826 bs->breakpoint_at = NULL;
12827 bs->old_val = NULL;
12828 /* bs->commands will be freed later. */
12829 }
12830}
12831
12832/* Callback for iterate_over_threads. */
12833static int
12834bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12835{
9a3c8263 12836 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12837
12838 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12839 return 0;
12840}
12841
12842/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12843 callbacks. */
12844
12845static void
12846say_where (struct breakpoint *b)
12847{
12848 struct value_print_options opts;
12849
12850 get_user_print_options (&opts);
12851
12852 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12853 single string. */
12854 if (b->loc == NULL)
12855 {
f00aae0f
KS
12856 /* For pending locations, the output differs slightly based
12857 on b->extra_string. If this is non-NULL, it contains either
12858 a condition or dprintf arguments. */
12859 if (b->extra_string == NULL)
12860 {
12861 printf_filtered (_(" (%s) pending."),
12862 event_location_to_string (b->location));
12863 }
12864 else if (b->type == bp_dprintf)
12865 {
12866 printf_filtered (_(" (%s,%s) pending."),
12867 event_location_to_string (b->location),
12868 b->extra_string);
12869 }
12870 else
12871 {
12872 printf_filtered (_(" (%s %s) pending."),
12873 event_location_to_string (b->location),
12874 b->extra_string);
12875 }
348d480f
PA
12876 }
12877 else
12878 {
2f202fde 12879 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12880 {
12881 printf_filtered (" at ");
12882 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12883 gdb_stdout);
12884 }
2f202fde 12885 if (b->loc->symtab != NULL)
f8eba3c6
TT
12886 {
12887 /* If there is a single location, we can print the location
12888 more nicely. */
12889 if (b->loc->next == NULL)
12890 printf_filtered (": file %s, line %d.",
05cba821
JK
12891 symtab_to_filename_for_display (b->loc->symtab),
12892 b->loc->line_number);
f8eba3c6
TT
12893 else
12894 /* This is not ideal, but each location may have a
12895 different file name, and this at least reflects the
12896 real situation somewhat. */
f00aae0f
KS
12897 printf_filtered (": %s.",
12898 event_location_to_string (b->location));
f8eba3c6 12899 }
348d480f
PA
12900
12901 if (b->loc->next)
12902 {
12903 struct bp_location *loc = b->loc;
12904 int n = 0;
12905 for (; loc; loc = loc->next)
12906 ++n;
12907 printf_filtered (" (%d locations)", n);
12908 }
12909 }
12910}
12911
348d480f
PA
12912/* Default bp_location_ops methods. */
12913
12914static void
12915bp_location_dtor (struct bp_location *self)
12916{
12917 xfree (self->cond);
b775012e
LM
12918 if (self->cond_bytecode)
12919 free_agent_expr (self->cond_bytecode);
348d480f 12920 xfree (self->function_name);
8b4f3082
PA
12921
12922 VEC_free (agent_expr_p, self->target_info.conditions);
12923 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12924}
12925
12926static const struct bp_location_ops bp_location_ops =
12927{
12928 bp_location_dtor
12929};
12930
2060206e
PA
12931/* Default breakpoint_ops methods all breakpoint_ops ultimately
12932 inherit from. */
348d480f 12933
2060206e
PA
12934static void
12935base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12936{
12937 decref_counted_command_line (&self->commands);
12938 xfree (self->cond_string);
fb81d016 12939 xfree (self->extra_string);
f8eba3c6 12940 xfree (self->filter);
f00aae0f
KS
12941 delete_event_location (self->location);
12942 delete_event_location (self->location_range_end);
348d480f
PA
12943}
12944
2060206e
PA
12945static struct bp_location *
12946base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12947{
12948 struct bp_location *loc;
12949
12950 loc = XNEW (struct bp_location);
12951 init_bp_location (loc, &bp_location_ops, self);
12952 return loc;
12953}
12954
2060206e
PA
12955static void
12956base_breakpoint_re_set (struct breakpoint *b)
12957{
12958 /* Nothing to re-set. */
12959}
12960
12961#define internal_error_pure_virtual_called() \
12962 gdb_assert_not_reached ("pure virtual function called")
12963
12964static int
12965base_breakpoint_insert_location (struct bp_location *bl)
12966{
12967 internal_error_pure_virtual_called ();
12968}
12969
12970static int
73971819
PA
12971base_breakpoint_remove_location (struct bp_location *bl,
12972 enum remove_bp_reason reason)
2060206e
PA
12973{
12974 internal_error_pure_virtual_called ();
12975}
12976
12977static int
12978base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12979 struct address_space *aspace,
09ac7c10
TT
12980 CORE_ADDR bp_addr,
12981 const struct target_waitstatus *ws)
2060206e
PA
12982{
12983 internal_error_pure_virtual_called ();
12984}
12985
12986static void
12987base_breakpoint_check_status (bpstat bs)
12988{
12989 /* Always stop. */
12990}
12991
12992/* A "works_in_software_mode" breakpoint_ops method that just internal
12993 errors. */
12994
12995static int
12996base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12997{
12998 internal_error_pure_virtual_called ();
12999}
13000
13001/* A "resources_needed" breakpoint_ops method that just internal
13002 errors. */
13003
13004static int
13005base_breakpoint_resources_needed (const struct bp_location *bl)
13006{
13007 internal_error_pure_virtual_called ();
13008}
13009
13010static enum print_stop_action
13011base_breakpoint_print_it (bpstat bs)
13012{
13013 internal_error_pure_virtual_called ();
13014}
13015
13016static void
13017base_breakpoint_print_one_detail (const struct breakpoint *self,
13018 struct ui_out *uiout)
13019{
13020 /* nothing */
13021}
13022
13023static void
13024base_breakpoint_print_mention (struct breakpoint *b)
13025{
13026 internal_error_pure_virtual_called ();
13027}
13028
13029static void
13030base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13031{
13032 internal_error_pure_virtual_called ();
13033}
13034
983af33b 13035static void
f00aae0f
KS
13036base_breakpoint_create_sals_from_location
13037 (const struct event_location *location,
13038 struct linespec_result *canonical,
13039 enum bptype type_wanted)
983af33b
SDJ
13040{
13041 internal_error_pure_virtual_called ();
13042}
13043
13044static void
13045base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13046 struct linespec_result *c,
983af33b 13047 char *cond_string,
e7e0cddf 13048 char *extra_string,
983af33b
SDJ
13049 enum bptype type_wanted,
13050 enum bpdisp disposition,
13051 int thread,
13052 int task, int ignore_count,
13053 const struct breakpoint_ops *o,
13054 int from_tty, int enabled,
44f238bb 13055 int internal, unsigned flags)
983af33b
SDJ
13056{
13057 internal_error_pure_virtual_called ();
13058}
13059
13060static void
f00aae0f
KS
13061base_breakpoint_decode_location (struct breakpoint *b,
13062 const struct event_location *location,
c2f4122d 13063 struct program_space *search_pspace,
983af33b
SDJ
13064 struct symtabs_and_lines *sals)
13065{
13066 internal_error_pure_virtual_called ();
13067}
13068
ab04a2af
TT
13069/* The default 'explains_signal' method. */
13070
47591c29 13071static int
427cd150 13072base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13073{
47591c29 13074 return 1;
ab04a2af
TT
13075}
13076
9d6e6e84
HZ
13077/* The default "after_condition_true" method. */
13078
13079static void
13080base_breakpoint_after_condition_true (struct bpstats *bs)
13081{
13082 /* Nothing to do. */
13083}
13084
ab04a2af 13085struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13086{
13087 base_breakpoint_dtor,
13088 base_breakpoint_allocate_location,
13089 base_breakpoint_re_set,
13090 base_breakpoint_insert_location,
13091 base_breakpoint_remove_location,
13092 base_breakpoint_breakpoint_hit,
13093 base_breakpoint_check_status,
13094 base_breakpoint_resources_needed,
13095 base_breakpoint_works_in_software_mode,
13096 base_breakpoint_print_it,
13097 NULL,
13098 base_breakpoint_print_one_detail,
13099 base_breakpoint_print_mention,
983af33b 13100 base_breakpoint_print_recreate,
5f700d83 13101 base_breakpoint_create_sals_from_location,
983af33b 13102 base_breakpoint_create_breakpoints_sal,
5f700d83 13103 base_breakpoint_decode_location,
9d6e6e84
HZ
13104 base_breakpoint_explains_signal,
13105 base_breakpoint_after_condition_true,
2060206e
PA
13106};
13107
13108/* Default breakpoint_ops methods. */
13109
13110static void
348d480f
PA
13111bkpt_re_set (struct breakpoint *b)
13112{
06edf0c0 13113 /* FIXME: is this still reachable? */
9ef9e6a6 13114 if (breakpoint_event_location_empty_p (b))
06edf0c0 13115 {
f00aae0f 13116 /* Anything without a location can't be re-set. */
348d480f 13117 delete_breakpoint (b);
06edf0c0 13118 return;
348d480f 13119 }
06edf0c0
PA
13120
13121 breakpoint_re_set_default (b);
348d480f
PA
13122}
13123
2060206e 13124static int
348d480f
PA
13125bkpt_insert_location (struct bp_location *bl)
13126{
cd6c3b4f
YQ
13127 CORE_ADDR addr = bl->target_info.reqstd_address;
13128
579c6ad9 13129 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
13130 bl->target_info.placed_address = addr;
13131
348d480f 13132 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13133 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13134 else
7c16b83e 13135 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13136}
13137
2060206e 13138static int
73971819 13139bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
13140{
13141 if (bl->loc_type == bp_loc_hardware_breakpoint)
13142 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13143 else
73971819 13144 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
13145}
13146
2060206e 13147static int
348d480f 13148bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13149 struct address_space *aspace, CORE_ADDR bp_addr,
13150 const struct target_waitstatus *ws)
348d480f 13151{
09ac7c10 13152 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13153 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13154 return 0;
13155
348d480f
PA
13156 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13157 aspace, bp_addr))
13158 return 0;
13159
13160 if (overlay_debugging /* unmapped overlay section */
13161 && section_is_overlay (bl->section)
13162 && !section_is_mapped (bl->section))
13163 return 0;
13164
13165 return 1;
13166}
13167
cd1608cc
PA
13168static int
13169dprintf_breakpoint_hit (const struct bp_location *bl,
13170 struct address_space *aspace, CORE_ADDR bp_addr,
13171 const struct target_waitstatus *ws)
13172{
13173 if (dprintf_style == dprintf_style_agent
13174 && target_can_run_breakpoint_commands ())
13175 {
13176 /* An agent-style dprintf never causes a stop. If we see a trap
13177 for this address it must be for a breakpoint that happens to
13178 be set at the same address. */
13179 return 0;
13180 }
13181
13182 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13183}
13184
2060206e 13185static int
348d480f
PA
13186bkpt_resources_needed (const struct bp_location *bl)
13187{
13188 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13189
13190 return 1;
13191}
13192
2060206e 13193static enum print_stop_action
348d480f
PA
13194bkpt_print_it (bpstat bs)
13195{
348d480f
PA
13196 struct breakpoint *b;
13197 const struct bp_location *bl;
001c8c33 13198 int bp_temp;
79a45e25 13199 struct ui_out *uiout = current_uiout;
348d480f
PA
13200
13201 gdb_assert (bs->bp_location_at != NULL);
13202
13203 bl = bs->bp_location_at;
13204 b = bs->breakpoint_at;
13205
001c8c33
PA
13206 bp_temp = b->disposition == disp_del;
13207 if (bl->address != bl->requested_address)
13208 breakpoint_adjustment_warning (bl->requested_address,
13209 bl->address,
13210 b->number, 1);
13211 annotate_breakpoint (b->number);
f303dbd6
PA
13212 maybe_print_thread_hit_breakpoint (uiout);
13213
001c8c33 13214 if (bp_temp)
f303dbd6 13215 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13216 else
f303dbd6 13217 ui_out_text (uiout, "Breakpoint ");
001c8c33 13218 if (ui_out_is_mi_like_p (uiout))
348d480f 13219 {
001c8c33
PA
13220 ui_out_field_string (uiout, "reason",
13221 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13222 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13223 }
001c8c33
PA
13224 ui_out_field_int (uiout, "bkptno", b->number);
13225 ui_out_text (uiout, ", ");
06edf0c0 13226
001c8c33 13227 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13228}
13229
2060206e 13230static void
06edf0c0
PA
13231bkpt_print_mention (struct breakpoint *b)
13232{
79a45e25 13233 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13234 return;
13235
13236 switch (b->type)
13237 {
13238 case bp_breakpoint:
13239 case bp_gnu_ifunc_resolver:
13240 if (b->disposition == disp_del)
13241 printf_filtered (_("Temporary breakpoint"));
13242 else
13243 printf_filtered (_("Breakpoint"));
13244 printf_filtered (_(" %d"), b->number);
13245 if (b->type == bp_gnu_ifunc_resolver)
13246 printf_filtered (_(" at gnu-indirect-function resolver"));
13247 break;
13248 case bp_hardware_breakpoint:
13249 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13250 break;
e7e0cddf
SS
13251 case bp_dprintf:
13252 printf_filtered (_("Dprintf %d"), b->number);
13253 break;
06edf0c0
PA
13254 }
13255
13256 say_where (b);
13257}
13258
2060206e 13259static void
06edf0c0
PA
13260bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13261{
13262 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13263 fprintf_unfiltered (fp, "tbreak");
13264 else if (tp->type == bp_breakpoint)
13265 fprintf_unfiltered (fp, "break");
13266 else if (tp->type == bp_hardware_breakpoint
13267 && tp->disposition == disp_del)
13268 fprintf_unfiltered (fp, "thbreak");
13269 else if (tp->type == bp_hardware_breakpoint)
13270 fprintf_unfiltered (fp, "hbreak");
13271 else
13272 internal_error (__FILE__, __LINE__,
13273 _("unhandled breakpoint type %d"), (int) tp->type);
13274
f00aae0f
KS
13275 fprintf_unfiltered (fp, " %s",
13276 event_location_to_string (tp->location));
13277
13278 /* Print out extra_string if this breakpoint is pending. It might
13279 contain, for example, conditions that were set by the user. */
13280 if (tp->loc == NULL && tp->extra_string != NULL)
13281 fprintf_unfiltered (fp, " %s", tp->extra_string);
13282
dd11a36c 13283 print_recreate_thread (tp, fp);
06edf0c0
PA
13284}
13285
983af33b 13286static void
f00aae0f
KS
13287bkpt_create_sals_from_location (const struct event_location *location,
13288 struct linespec_result *canonical,
13289 enum bptype type_wanted)
983af33b 13290{
f00aae0f 13291 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13292}
13293
13294static void
13295bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13296 struct linespec_result *canonical,
983af33b 13297 char *cond_string,
e7e0cddf 13298 char *extra_string,
983af33b
SDJ
13299 enum bptype type_wanted,
13300 enum bpdisp disposition,
13301 int thread,
13302 int task, int ignore_count,
13303 const struct breakpoint_ops *ops,
13304 int from_tty, int enabled,
44f238bb 13305 int internal, unsigned flags)
983af33b 13306{
023fa29b 13307 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13308 cond_string, extra_string,
13309 type_wanted,
983af33b
SDJ
13310 disposition, thread, task,
13311 ignore_count, ops, from_tty,
44f238bb 13312 enabled, internal, flags);
983af33b
SDJ
13313}
13314
13315static void
f00aae0f
KS
13316bkpt_decode_location (struct breakpoint *b,
13317 const struct event_location *location,
c2f4122d 13318 struct program_space *search_pspace,
983af33b
SDJ
13319 struct symtabs_and_lines *sals)
13320{
c2f4122d 13321 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13322}
13323
06edf0c0
PA
13324/* Virtual table for internal breakpoints. */
13325
13326static void
13327internal_bkpt_re_set (struct breakpoint *b)
13328{
13329 switch (b->type)
13330 {
13331 /* Delete overlay event and longjmp master breakpoints; they
13332 will be reset later by breakpoint_re_set. */
13333 case bp_overlay_event:
13334 case bp_longjmp_master:
13335 case bp_std_terminate_master:
13336 case bp_exception_master:
13337 delete_breakpoint (b);
13338 break;
13339
13340 /* This breakpoint is special, it's set up when the inferior
13341 starts and we really don't want to touch it. */
13342 case bp_shlib_event:
13343
13344 /* Like bp_shlib_event, this breakpoint type is special. Once
13345 it is set up, we do not want to touch it. */
13346 case bp_thread_event:
13347 break;
13348 }
13349}
13350
13351static void
13352internal_bkpt_check_status (bpstat bs)
13353{
a9b3a50f
PA
13354 if (bs->breakpoint_at->type == bp_shlib_event)
13355 {
13356 /* If requested, stop when the dynamic linker notifies GDB of
13357 events. This allows the user to get control and place
13358 breakpoints in initializer routines for dynamically loaded
13359 objects (among other things). */
13360 bs->stop = stop_on_solib_events;
13361 bs->print = stop_on_solib_events;
13362 }
13363 else
13364 bs->stop = 0;
06edf0c0
PA
13365}
13366
13367static enum print_stop_action
13368internal_bkpt_print_it (bpstat bs)
13369{
06edf0c0 13370 struct breakpoint *b;
06edf0c0 13371
06edf0c0
PA
13372 b = bs->breakpoint_at;
13373
06edf0c0
PA
13374 switch (b->type)
13375 {
348d480f
PA
13376 case bp_shlib_event:
13377 /* Did we stop because the user set the stop_on_solib_events
13378 variable? (If so, we report this as a generic, "Stopped due
13379 to shlib event" message.) */
edcc5120 13380 print_solib_event (0);
348d480f
PA
13381 break;
13382
13383 case bp_thread_event:
13384 /* Not sure how we will get here.
13385 GDB should not stop for these breakpoints. */
13386 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13387 break;
13388
13389 case bp_overlay_event:
13390 /* By analogy with the thread event, GDB should not stop for these. */
13391 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13392 break;
13393
13394 case bp_longjmp_master:
13395 /* These should never be enabled. */
13396 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13397 break;
13398
13399 case bp_std_terminate_master:
13400 /* These should never be enabled. */
13401 printf_filtered (_("std::terminate Master Breakpoint: "
13402 "gdb should not stop!\n"));
348d480f
PA
13403 break;
13404
13405 case bp_exception_master:
13406 /* These should never be enabled. */
13407 printf_filtered (_("Exception Master Breakpoint: "
13408 "gdb should not stop!\n"));
06edf0c0
PA
13409 break;
13410 }
13411
001c8c33 13412 return PRINT_NOTHING;
06edf0c0
PA
13413}
13414
13415static void
13416internal_bkpt_print_mention (struct breakpoint *b)
13417{
13418 /* Nothing to mention. These breakpoints are internal. */
13419}
13420
06edf0c0
PA
13421/* Virtual table for momentary breakpoints */
13422
13423static void
13424momentary_bkpt_re_set (struct breakpoint *b)
13425{
13426 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13427 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13428 Otherwise these should have been blown away via the cleanup chain
13429 or by breakpoint_init_inferior when we rerun the executable. */
13430}
13431
13432static void
13433momentary_bkpt_check_status (bpstat bs)
13434{
13435 /* Nothing. The point of these breakpoints is causing a stop. */
13436}
13437
13438static enum print_stop_action
13439momentary_bkpt_print_it (bpstat bs)
13440{
001c8c33 13441 return PRINT_UNKNOWN;
348d480f
PA
13442}
13443
06edf0c0
PA
13444static void
13445momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13446{
06edf0c0 13447 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13448}
13449
e2e4d78b
JK
13450/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13451
13452 It gets cleared already on the removal of the first one of such placed
13453 breakpoints. This is OK as they get all removed altogether. */
13454
13455static void
13456longjmp_bkpt_dtor (struct breakpoint *self)
13457{
5d5658a1 13458 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13459
13460 if (tp)
13461 tp->initiating_frame = null_frame_id;
13462
13463 momentary_breakpoint_ops.dtor (self);
13464}
13465
55aa24fb
SDJ
13466/* Specific methods for probe breakpoints. */
13467
13468static int
13469bkpt_probe_insert_location (struct bp_location *bl)
13470{
13471 int v = bkpt_insert_location (bl);
13472
13473 if (v == 0)
13474 {
13475 /* The insertion was successful, now let's set the probe's semaphore
13476 if needed. */
0ea5cda8
SDJ
13477 if (bl->probe.probe->pops->set_semaphore != NULL)
13478 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13479 bl->probe.objfile,
13480 bl->gdbarch);
55aa24fb
SDJ
13481 }
13482
13483 return v;
13484}
13485
13486static int
73971819
PA
13487bkpt_probe_remove_location (struct bp_location *bl,
13488 enum remove_bp_reason reason)
55aa24fb
SDJ
13489{
13490 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13491 if (bl->probe.probe->pops->clear_semaphore != NULL)
13492 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13493 bl->probe.objfile,
13494 bl->gdbarch);
55aa24fb 13495
73971819 13496 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13497}
13498
13499static void
f00aae0f 13500bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13501 struct linespec_result *canonical,
f00aae0f 13502 enum bptype type_wanted)
55aa24fb
SDJ
13503{
13504 struct linespec_sals lsal;
13505
c2f4122d 13506 lsal.sals = parse_probes (location, NULL, canonical);
f00aae0f 13507 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13508 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13509}
13510
13511static void
f00aae0f
KS
13512bkpt_probe_decode_location (struct breakpoint *b,
13513 const struct event_location *location,
c2f4122d 13514 struct program_space *search_pspace,
55aa24fb
SDJ
13515 struct symtabs_and_lines *sals)
13516{
c2f4122d 13517 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13518 if (!sals->sals)
13519 error (_("probe not found"));
13520}
13521
348d480f 13522/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13523
348d480f
PA
13524static void
13525tracepoint_re_set (struct breakpoint *b)
13526{
13527 breakpoint_re_set_default (b);
13528}
876fa593 13529
348d480f
PA
13530static int
13531tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13532 struct address_space *aspace, CORE_ADDR bp_addr,
13533 const struct target_waitstatus *ws)
348d480f
PA
13534{
13535 /* By definition, the inferior does not report stops at
13536 tracepoints. */
13537 return 0;
74960c60
VP
13538}
13539
13540static void
348d480f
PA
13541tracepoint_print_one_detail (const struct breakpoint *self,
13542 struct ui_out *uiout)
74960c60 13543{
d9b3f62e
PA
13544 struct tracepoint *tp = (struct tracepoint *) self;
13545 if (tp->static_trace_marker_id)
348d480f
PA
13546 {
13547 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13548
348d480f
PA
13549 ui_out_text (uiout, "\tmarker id is ");
13550 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13551 tp->static_trace_marker_id);
348d480f
PA
13552 ui_out_text (uiout, "\n");
13553 }
0d381245
VP
13554}
13555
a474d7c2 13556static void
348d480f 13557tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13558{
79a45e25 13559 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13560 return;
cc59ec59 13561
348d480f
PA
13562 switch (b->type)
13563 {
13564 case bp_tracepoint:
13565 printf_filtered (_("Tracepoint"));
13566 printf_filtered (_(" %d"), b->number);
13567 break;
13568 case bp_fast_tracepoint:
13569 printf_filtered (_("Fast tracepoint"));
13570 printf_filtered (_(" %d"), b->number);
13571 break;
13572 case bp_static_tracepoint:
13573 printf_filtered (_("Static tracepoint"));
13574 printf_filtered (_(" %d"), b->number);
13575 break;
13576 default:
13577 internal_error (__FILE__, __LINE__,
13578 _("unhandled tracepoint type %d"), (int) b->type);
13579 }
13580
13581 say_where (b);
a474d7c2
PA
13582}
13583
348d480f 13584static void
d9b3f62e 13585tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13586{
d9b3f62e
PA
13587 struct tracepoint *tp = (struct tracepoint *) self;
13588
13589 if (self->type == bp_fast_tracepoint)
348d480f 13590 fprintf_unfiltered (fp, "ftrace");
c93e8391 13591 else if (self->type == bp_static_tracepoint)
348d480f 13592 fprintf_unfiltered (fp, "strace");
d9b3f62e 13593 else if (self->type == bp_tracepoint)
348d480f
PA
13594 fprintf_unfiltered (fp, "trace");
13595 else
13596 internal_error (__FILE__, __LINE__,
d9b3f62e 13597 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13598
f00aae0f
KS
13599 fprintf_unfiltered (fp, " %s",
13600 event_location_to_string (self->location));
d9b3f62e
PA
13601 print_recreate_thread (self, fp);
13602
13603 if (tp->pass_count)
13604 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13605}
13606
983af33b 13607static void
f00aae0f
KS
13608tracepoint_create_sals_from_location (const struct event_location *location,
13609 struct linespec_result *canonical,
13610 enum bptype type_wanted)
983af33b 13611{
f00aae0f 13612 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13613}
13614
13615static void
13616tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13617 struct linespec_result *canonical,
983af33b 13618 char *cond_string,
e7e0cddf 13619 char *extra_string,
983af33b
SDJ
13620 enum bptype type_wanted,
13621 enum bpdisp disposition,
13622 int thread,
13623 int task, int ignore_count,
13624 const struct breakpoint_ops *ops,
13625 int from_tty, int enabled,
44f238bb 13626 int internal, unsigned flags)
983af33b 13627{
023fa29b 13628 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13629 cond_string, extra_string,
13630 type_wanted,
983af33b
SDJ
13631 disposition, thread, task,
13632 ignore_count, ops, from_tty,
44f238bb 13633 enabled, internal, flags);
983af33b
SDJ
13634}
13635
13636static void
f00aae0f
KS
13637tracepoint_decode_location (struct breakpoint *b,
13638 const struct event_location *location,
c2f4122d 13639 struct program_space *search_pspace,
983af33b
SDJ
13640 struct symtabs_and_lines *sals)
13641{
c2f4122d 13642 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13643}
13644
2060206e 13645struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13646
55aa24fb
SDJ
13647/* The breakpoint_ops structure to be use on tracepoints placed in a
13648 static probe. */
13649
13650static void
f00aae0f
KS
13651tracepoint_probe_create_sals_from_location
13652 (const struct event_location *location,
13653 struct linespec_result *canonical,
13654 enum bptype type_wanted)
55aa24fb
SDJ
13655{
13656 /* We use the same method for breakpoint on probes. */
f00aae0f 13657 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13658}
13659
13660static void
f00aae0f
KS
13661tracepoint_probe_decode_location (struct breakpoint *b,
13662 const struct event_location *location,
c2f4122d 13663 struct program_space *search_pspace,
55aa24fb
SDJ
13664 struct symtabs_and_lines *sals)
13665{
13666 /* We use the same method for breakpoint on probes. */
c2f4122d 13667 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13668}
13669
13670static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13671
5c2b4418
HZ
13672/* Dprintf breakpoint_ops methods. */
13673
13674static void
13675dprintf_re_set (struct breakpoint *b)
13676{
13677 breakpoint_re_set_default (b);
13678
f00aae0f
KS
13679 /* extra_string should never be non-NULL for dprintf. */
13680 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13681
13682 /* 1 - connect to target 1, that can run breakpoint commands.
13683 2 - create a dprintf, which resolves fine.
13684 3 - disconnect from target 1
13685 4 - connect to target 2, that can NOT run breakpoint commands.
13686
13687 After steps #3/#4, you'll want the dprintf command list to
13688 be updated, because target 1 and 2 may well return different
13689 answers for target_can_run_breakpoint_commands().
13690 Given absence of finer grained resetting, we get to do
13691 it all the time. */
13692 if (b->extra_string != NULL)
13693 update_dprintf_command_list (b);
13694}
13695
2d9442cc
HZ
13696/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13697
13698static void
13699dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13700{
f00aae0f
KS
13701 fprintf_unfiltered (fp, "dprintf %s,%s",
13702 event_location_to_string (tp->location),
2d9442cc
HZ
13703 tp->extra_string);
13704 print_recreate_thread (tp, fp);
13705}
13706
9d6e6e84
HZ
13707/* Implement the "after_condition_true" breakpoint_ops method for
13708 dprintf.
13709
13710 dprintf's are implemented with regular commands in their command
13711 list, but we run the commands here instead of before presenting the
13712 stop to the user, as dprintf's don't actually cause a stop. This
13713 also makes it so that the commands of multiple dprintfs at the same
13714 address are all handled. */
13715
13716static void
13717dprintf_after_condition_true (struct bpstats *bs)
13718{
13719 struct cleanup *old_chain;
13720 struct bpstats tmp_bs = { NULL };
13721 struct bpstats *tmp_bs_p = &tmp_bs;
13722
13723 /* dprintf's never cause a stop. This wasn't set in the
13724 check_status hook instead because that would make the dprintf's
13725 condition not be evaluated. */
13726 bs->stop = 0;
13727
13728 /* Run the command list here. Take ownership of it instead of
13729 copying. We never want these commands to run later in
13730 bpstat_do_actions, if a breakpoint that causes a stop happens to
13731 be set at same address as this dprintf, or even if running the
13732 commands here throws. */
13733 tmp_bs.commands = bs->commands;
13734 bs->commands = NULL;
13735 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13736
13737 bpstat_do_actions_1 (&tmp_bs_p);
13738
13739 /* 'tmp_bs.commands' will usually be NULL by now, but
13740 bpstat_do_actions_1 may return early without processing the whole
13741 list. */
13742 do_cleanups (old_chain);
13743}
13744
983af33b
SDJ
13745/* The breakpoint_ops structure to be used on static tracepoints with
13746 markers (`-m'). */
13747
13748static void
f00aae0f 13749strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13750 struct linespec_result *canonical,
f00aae0f 13751 enum bptype type_wanted)
983af33b
SDJ
13752{
13753 struct linespec_sals lsal;
f00aae0f
KS
13754 const char *arg_start, *arg;
13755 char *str;
13756 struct cleanup *cleanup;
983af33b 13757
f00aae0f
KS
13758 arg = arg_start = get_linespec_location (location);
13759 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13760
f00aae0f
KS
13761 str = savestring (arg_start, arg - arg_start);
13762 cleanup = make_cleanup (xfree, str);
13763 canonical->location = new_linespec_location (&str);
13764 do_cleanups (cleanup);
983af33b 13765
f00aae0f 13766 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13767 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13768}
13769
13770static void
13771strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13772 struct linespec_result *canonical,
983af33b 13773 char *cond_string,
e7e0cddf 13774 char *extra_string,
983af33b
SDJ
13775 enum bptype type_wanted,
13776 enum bpdisp disposition,
13777 int thread,
13778 int task, int ignore_count,
13779 const struct breakpoint_ops *ops,
13780 int from_tty, int enabled,
44f238bb 13781 int internal, unsigned flags)
983af33b
SDJ
13782{
13783 int i;
52d361e1
YQ
13784 struct linespec_sals *lsal = VEC_index (linespec_sals,
13785 canonical->sals, 0);
983af33b
SDJ
13786
13787 /* If the user is creating a static tracepoint by marker id
13788 (strace -m MARKER_ID), then store the sals index, so that
13789 breakpoint_re_set can try to match up which of the newly
13790 found markers corresponds to this one, and, don't try to
13791 expand multiple locations for each sal, given than SALS
13792 already should contain all sals for MARKER_ID. */
13793
13794 for (i = 0; i < lsal->sals.nelts; ++i)
13795 {
13796 struct symtabs_and_lines expanded;
13797 struct tracepoint *tp;
13798 struct cleanup *old_chain;
f00aae0f 13799 struct event_location *location;
983af33b
SDJ
13800
13801 expanded.nelts = 1;
13802 expanded.sals = &lsal->sals.sals[i];
13803
f00aae0f
KS
13804 location = copy_event_location (canonical->location);
13805 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13806
13807 tp = XCNEW (struct tracepoint);
13808 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13809 location, NULL,
e7e0cddf
SS
13810 cond_string, extra_string,
13811 type_wanted, disposition,
983af33b 13812 thread, task, ignore_count, ops,
44f238bb 13813 from_tty, enabled, internal, flags,
983af33b
SDJ
13814 canonical->special_display);
13815 /* Given that its possible to have multiple markers with
13816 the same string id, if the user is creating a static
13817 tracepoint by marker id ("strace -m MARKER_ID"), then
13818 store the sals index, so that breakpoint_re_set can
13819 try to match up which of the newly found markers
13820 corresponds to this one */
13821 tp->static_trace_marker_id_idx = i;
13822
13823 install_breakpoint (internal, &tp->base, 0);
13824
13825 discard_cleanups (old_chain);
13826 }
13827}
13828
13829static void
f00aae0f
KS
13830strace_marker_decode_location (struct breakpoint *b,
13831 const struct event_location *location,
c2f4122d 13832 struct program_space *search_pspace,
983af33b
SDJ
13833 struct symtabs_and_lines *sals)
13834{
13835 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13836 const char *s = get_linespec_location (location);
983af33b 13837
f00aae0f 13838 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13839 if (sals->nelts > tp->static_trace_marker_id_idx)
13840 {
13841 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13842 sals->nelts = 1;
13843 }
13844 else
13845 error (_("marker %s not found"), tp->static_trace_marker_id);
13846}
13847
13848static struct breakpoint_ops strace_marker_breakpoint_ops;
13849
13850static int
13851strace_marker_p (struct breakpoint *b)
13852{
13853 return b->ops == &strace_marker_breakpoint_ops;
13854}
13855
53a5351d 13856/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13857 structures. */
c906108c
SS
13858
13859void
fba45db2 13860delete_breakpoint (struct breakpoint *bpt)
c906108c 13861{
52f0bd74 13862 struct breakpoint *b;
c906108c 13863
8a3fe4f8 13864 gdb_assert (bpt != NULL);
c906108c 13865
4a64f543
MS
13866 /* Has this bp already been deleted? This can happen because
13867 multiple lists can hold pointers to bp's. bpstat lists are
13868 especial culprits.
13869
13870 One example of this happening is a watchpoint's scope bp. When
13871 the scope bp triggers, we notice that the watchpoint is out of
13872 scope, and delete it. We also delete its scope bp. But the
13873 scope bp is marked "auto-deleting", and is already on a bpstat.
13874 That bpstat is then checked for auto-deleting bp's, which are
13875 deleted.
13876
13877 A real solution to this problem might involve reference counts in
13878 bp's, and/or giving them pointers back to their referencing
13879 bpstat's, and teaching delete_breakpoint to only free a bp's
13880 storage when no more references were extent. A cheaper bandaid
13881 was chosen. */
c906108c
SS
13882 if (bpt->type == bp_none)
13883 return;
13884
4a64f543
MS
13885 /* At least avoid this stale reference until the reference counting
13886 of breakpoints gets resolved. */
d0fb5eae 13887 if (bpt->related_breakpoint != bpt)
e5a0a904 13888 {
d0fb5eae 13889 struct breakpoint *related;
3a5c3e22 13890 struct watchpoint *w;
d0fb5eae
JK
13891
13892 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13893 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13894 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13895 w = (struct watchpoint *) bpt;
13896 else
13897 w = NULL;
13898 if (w != NULL)
13899 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13900
13901 /* Unlink bpt from the bpt->related_breakpoint ring. */
13902 for (related = bpt; related->related_breakpoint != bpt;
13903 related = related->related_breakpoint);
13904 related->related_breakpoint = bpt->related_breakpoint;
13905 bpt->related_breakpoint = bpt;
e5a0a904
JK
13906 }
13907
a9634178
TJB
13908 /* watch_command_1 creates a watchpoint but only sets its number if
13909 update_watchpoint succeeds in creating its bp_locations. If there's
13910 a problem in that process, we'll be asked to delete the half-created
13911 watchpoint. In that case, don't announce the deletion. */
13912 if (bpt->number)
13913 observer_notify_breakpoint_deleted (bpt);
c906108c 13914
c906108c
SS
13915 if (breakpoint_chain == bpt)
13916 breakpoint_chain = bpt->next;
13917
c906108c
SS
13918 ALL_BREAKPOINTS (b)
13919 if (b->next == bpt)
c5aa993b
JM
13920 {
13921 b->next = bpt->next;
13922 break;
13923 }
c906108c 13924
f431efe5
PA
13925 /* Be sure no bpstat's are pointing at the breakpoint after it's
13926 been freed. */
13927 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13928 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13929 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13930 commands are associated with the bpstat; if we remove it here,
13931 then the later call to bpstat_do_actions (&stop_bpstat); in
13932 event-top.c won't do anything, and temporary breakpoints with
13933 commands won't work. */
13934
13935 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13936
4a64f543
MS
13937 /* Now that breakpoint is removed from breakpoint list, update the
13938 global location list. This will remove locations that used to
13939 belong to this breakpoint. Do this before freeing the breakpoint
13940 itself, since remove_breakpoint looks at location's owner. It
13941 might be better design to have location completely
13942 self-contained, but it's not the case now. */
44702360 13943 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13944
348d480f 13945 bpt->ops->dtor (bpt);
4a64f543
MS
13946 /* On the chance that someone will soon try again to delete this
13947 same bp, we mark it as deleted before freeing its storage. */
c906108c 13948 bpt->type = bp_none;
b8c9b27d 13949 xfree (bpt);
c906108c
SS
13950}
13951
4d6140d9
AC
13952static void
13953do_delete_breakpoint_cleanup (void *b)
13954{
9a3c8263 13955 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13956}
13957
13958struct cleanup *
13959make_cleanup_delete_breakpoint (struct breakpoint *b)
13960{
13961 return make_cleanup (do_delete_breakpoint_cleanup, b);
13962}
13963
51be5b68
PA
13964/* Iterator function to call a user-provided callback function once
13965 for each of B and its related breakpoints. */
13966
13967static void
13968iterate_over_related_breakpoints (struct breakpoint *b,
13969 void (*function) (struct breakpoint *,
13970 void *),
13971 void *data)
13972{
13973 struct breakpoint *related;
13974
13975 related = b;
13976 do
13977 {
13978 struct breakpoint *next;
13979
13980 /* FUNCTION may delete RELATED. */
13981 next = related->related_breakpoint;
13982
13983 if (next == related)
13984 {
13985 /* RELATED is the last ring entry. */
13986 function (related, data);
13987
13988 /* FUNCTION may have deleted it, so we'd never reach back to
13989 B. There's nothing left to do anyway, so just break
13990 out. */
13991 break;
13992 }
13993 else
13994 function (related, data);
13995
13996 related = next;
13997 }
13998 while (related != b);
13999}
95a42b64
TT
14000
14001static void
14002do_delete_breakpoint (struct breakpoint *b, void *ignore)
14003{
14004 delete_breakpoint (b);
14005}
14006
51be5b68
PA
14007/* A callback for map_breakpoint_numbers that calls
14008 delete_breakpoint. */
14009
14010static void
14011do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14012{
14013 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14014}
14015
c906108c 14016void
fba45db2 14017delete_command (char *arg, int from_tty)
c906108c 14018{
35df4500 14019 struct breakpoint *b, *b_tmp;
c906108c 14020
ea9365bb
TT
14021 dont_repeat ();
14022
c906108c
SS
14023 if (arg == 0)
14024 {
14025 int breaks_to_delete = 0;
14026
46c6471b
PA
14027 /* Delete all breakpoints if no argument. Do not delete
14028 internal breakpoints, these have to be deleted with an
14029 explicit breakpoint number argument. */
c5aa993b 14030 ALL_BREAKPOINTS (b)
46c6471b 14031 if (user_breakpoint_p (b))
973d738b
DJ
14032 {
14033 breaks_to_delete = 1;
14034 break;
14035 }
c906108c
SS
14036
14037 /* Ask user only if there are some breakpoints to delete. */
14038 if (!from_tty
e2e0b3e5 14039 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14040 {
35df4500 14041 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14042 if (user_breakpoint_p (b))
c5aa993b 14043 delete_breakpoint (b);
c906108c
SS
14044 }
14045 }
14046 else
51be5b68 14047 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14048}
14049
c2f4122d
PA
14050/* Return true if all locations of B bound to PSPACE are pending. If
14051 PSPACE is NULL, all locations of all program spaces are
14052 considered. */
14053
0d381245 14054static int
c2f4122d 14055all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 14056{
c2f4122d
PA
14057 struct bp_location *loc;
14058
14059 for (loc = b->loc; loc != NULL; loc = loc->next)
14060 if ((pspace == NULL
14061 || loc->pspace == pspace)
14062 && !loc->shlib_disabled
8645ff69 14063 && !loc->pspace->executing_startup)
0d381245
VP
14064 return 0;
14065 return 1;
fe3f5fa8
VP
14066}
14067
776592bf
DE
14068/* Subroutine of update_breakpoint_locations to simplify it.
14069 Return non-zero if multiple fns in list LOC have the same name.
14070 Null names are ignored. */
14071
14072static int
14073ambiguous_names_p (struct bp_location *loc)
14074{
14075 struct bp_location *l;
14076 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14077 (int (*) (const void *,
14078 const void *)) streq,
776592bf
DE
14079 NULL, xcalloc, xfree);
14080
14081 for (l = loc; l != NULL; l = l->next)
14082 {
14083 const char **slot;
14084 const char *name = l->function_name;
14085
14086 /* Allow for some names to be NULL, ignore them. */
14087 if (name == NULL)
14088 continue;
14089
14090 slot = (const char **) htab_find_slot (htab, (const void *) name,
14091 INSERT);
4a64f543
MS
14092 /* NOTE: We can assume slot != NULL here because xcalloc never
14093 returns NULL. */
776592bf
DE
14094 if (*slot != NULL)
14095 {
14096 htab_delete (htab);
14097 return 1;
14098 }
14099 *slot = name;
14100 }
14101
14102 htab_delete (htab);
14103 return 0;
14104}
14105
0fb4aa4b
PA
14106/* When symbols change, it probably means the sources changed as well,
14107 and it might mean the static tracepoint markers are no longer at
14108 the same address or line numbers they used to be at last we
14109 checked. Losing your static tracepoints whenever you rebuild is
14110 undesirable. This function tries to resync/rematch gdb static
14111 tracepoints with the markers on the target, for static tracepoints
14112 that have not been set by marker id. Static tracepoint that have
14113 been set by marker id are reset by marker id in breakpoint_re_set.
14114 The heuristic is:
14115
14116 1) For a tracepoint set at a specific address, look for a marker at
14117 the old PC. If one is found there, assume to be the same marker.
14118 If the name / string id of the marker found is different from the
14119 previous known name, assume that means the user renamed the marker
14120 in the sources, and output a warning.
14121
14122 2) For a tracepoint set at a given line number, look for a marker
14123 at the new address of the old line number. If one is found there,
14124 assume to be the same marker. If the name / string id of the
14125 marker found is different from the previous known name, assume that
14126 means the user renamed the marker in the sources, and output a
14127 warning.
14128
14129 3) If a marker is no longer found at the same address or line, it
14130 may mean the marker no longer exists. But it may also just mean
14131 the code changed a bit. Maybe the user added a few lines of code
14132 that made the marker move up or down (in line number terms). Ask
14133 the target for info about the marker with the string id as we knew
14134 it. If found, update line number and address in the matching
14135 static tracepoint. This will get confused if there's more than one
14136 marker with the same ID (possible in UST, although unadvised
14137 precisely because it confuses tools). */
14138
14139static struct symtab_and_line
14140update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14141{
d9b3f62e 14142 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14143 struct static_tracepoint_marker marker;
14144 CORE_ADDR pc;
0fb4aa4b
PA
14145
14146 pc = sal.pc;
14147 if (sal.line)
14148 find_line_pc (sal.symtab, sal.line, &pc);
14149
14150 if (target_static_tracepoint_marker_at (pc, &marker))
14151 {
d9b3f62e 14152 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14153 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14154 b->number,
d9b3f62e 14155 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14156
d9b3f62e
PA
14157 xfree (tp->static_trace_marker_id);
14158 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14159 release_static_tracepoint_marker (&marker);
14160
14161 return sal;
14162 }
14163
14164 /* Old marker wasn't found on target at lineno. Try looking it up
14165 by string ID. */
14166 if (!sal.explicit_pc
14167 && sal.line != 0
14168 && sal.symtab != NULL
d9b3f62e 14169 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14170 {
14171 VEC(static_tracepoint_marker_p) *markers;
14172
14173 markers
d9b3f62e 14174 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14175
14176 if (!VEC_empty(static_tracepoint_marker_p, markers))
14177 {
80e1d417 14178 struct symtab_and_line sal2;
0fb4aa4b 14179 struct symbol *sym;
80e1d417 14180 struct static_tracepoint_marker *tpmarker;
79a45e25 14181 struct ui_out *uiout = current_uiout;
67994074 14182 struct explicit_location explicit_loc;
0fb4aa4b 14183
80e1d417 14184 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14185
d9b3f62e 14186 xfree (tp->static_trace_marker_id);
80e1d417 14187 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14188
14189 warning (_("marker for static tracepoint %d (%s) not "
14190 "found at previous line number"),
d9b3f62e 14191 b->number, tp->static_trace_marker_id);
0fb4aa4b 14192
80e1d417 14193 init_sal (&sal2);
0fb4aa4b 14194
80e1d417 14195 sal2.pc = tpmarker->address;
0fb4aa4b 14196
80e1d417
AS
14197 sal2 = find_pc_line (tpmarker->address, 0);
14198 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14199 ui_out_text (uiout, "Now in ");
14200 if (sym)
14201 {
14202 ui_out_field_string (uiout, "func",
14203 SYMBOL_PRINT_NAME (sym));
14204 ui_out_text (uiout, " at ");
14205 }
05cba821
JK
14206 ui_out_field_string (uiout, "file",
14207 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14208 ui_out_text (uiout, ":");
14209
14210 if (ui_out_is_mi_like_p (uiout))
14211 {
0b0865da 14212 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14213
f35a17b5 14214 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14215 }
14216
80e1d417 14217 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14218 ui_out_text (uiout, "\n");
14219
80e1d417 14220 b->loc->line_number = sal2.line;
2f202fde 14221 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14222
f00aae0f 14223 delete_event_location (b->location);
67994074
KS
14224 initialize_explicit_location (&explicit_loc);
14225 explicit_loc.source_filename
00e52e53 14226 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14227 explicit_loc.line_offset.offset = b->loc->line_number;
14228 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14229 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14230
14231 /* Might be nice to check if function changed, and warn if
14232 so. */
14233
80e1d417 14234 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14235 }
14236 }
14237 return sal;
14238}
14239
8d3788bd
VP
14240/* Returns 1 iff locations A and B are sufficiently same that
14241 we don't need to report breakpoint as changed. */
14242
14243static int
14244locations_are_equal (struct bp_location *a, struct bp_location *b)
14245{
14246 while (a && b)
14247 {
14248 if (a->address != b->address)
14249 return 0;
14250
14251 if (a->shlib_disabled != b->shlib_disabled)
14252 return 0;
14253
14254 if (a->enabled != b->enabled)
14255 return 0;
14256
14257 a = a->next;
14258 b = b->next;
14259 }
14260
14261 if ((a == NULL) != (b == NULL))
14262 return 0;
14263
14264 return 1;
14265}
14266
c2f4122d
PA
14267/* Split all locations of B that are bound to PSPACE out of B's
14268 location list to a separate list and return that list's head. If
14269 PSPACE is NULL, hoist out all locations of B. */
14270
14271static struct bp_location *
14272hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14273{
14274 struct bp_location head;
14275 struct bp_location *i = b->loc;
14276 struct bp_location **i_link = &b->loc;
14277 struct bp_location *hoisted = &head;
14278
14279 if (pspace == NULL)
14280 {
14281 i = b->loc;
14282 b->loc = NULL;
14283 return i;
14284 }
14285
14286 head.next = NULL;
14287
14288 while (i != NULL)
14289 {
14290 if (i->pspace == pspace)
14291 {
14292 *i_link = i->next;
14293 i->next = NULL;
14294 hoisted->next = i;
14295 hoisted = i;
14296 }
14297 else
14298 i_link = &i->next;
14299 i = *i_link;
14300 }
14301
14302 return head.next;
14303}
14304
14305/* Create new breakpoint locations for B (a hardware or software
14306 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14307 zero, then B is a ranged breakpoint. Only recreates locations for
14308 FILTER_PSPACE. Locations of other program spaces are left
14309 untouched. */
f1310107 14310
0e30163f 14311void
0d381245 14312update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14313 struct program_space *filter_pspace,
f1310107
TJB
14314 struct symtabs_and_lines sals,
14315 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14316{
14317 int i;
c2f4122d 14318 struct bp_location *existing_locations;
0d381245 14319
f8eba3c6
TT
14320 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14321 {
14322 /* Ranged breakpoints have only one start location and one end
14323 location. */
14324 b->enable_state = bp_disabled;
f8eba3c6
TT
14325 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14326 "multiple locations found\n"),
14327 b->number);
14328 return;
14329 }
f1310107 14330
4a64f543
MS
14331 /* If there's no new locations, and all existing locations are
14332 pending, don't do anything. This optimizes the common case where
14333 all locations are in the same shared library, that was unloaded.
14334 We'd like to retain the location, so that when the library is
14335 loaded again, we don't loose the enabled/disabled status of the
14336 individual locations. */
c2f4122d 14337 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14338 return;
14339
c2f4122d 14340 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14341
0d381245 14342 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14343 {
f8eba3c6
TT
14344 struct bp_location *new_loc;
14345
14346 switch_to_program_space_and_thread (sals.sals[i].pspace);
14347
14348 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14349
0d381245
VP
14350 /* Reparse conditions, they might contain references to the
14351 old symtab. */
14352 if (b->cond_string != NULL)
14353 {
bbc13ae3 14354 const char *s;
fe3f5fa8 14355
0d381245 14356 s = b->cond_string;
492d29ea 14357 TRY
0d381245 14358 {
1bb9788d
TT
14359 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14360 block_for_pc (sals.sals[i].pc),
0d381245
VP
14361 0);
14362 }
492d29ea 14363 CATCH (e, RETURN_MASK_ERROR)
0d381245 14364 {
3e43a32a
MS
14365 warning (_("failed to reevaluate condition "
14366 "for breakpoint %d: %s"),
0d381245
VP
14367 b->number, e.message);
14368 new_loc->enabled = 0;
14369 }
492d29ea 14370 END_CATCH
0d381245 14371 }
fe3f5fa8 14372
f1310107
TJB
14373 if (sals_end.nelts)
14374 {
14375 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14376
14377 new_loc->length = end - sals.sals[0].pc + 1;
14378 }
0d381245 14379 }
fe3f5fa8 14380
4a64f543
MS
14381 /* If possible, carry over 'disable' status from existing
14382 breakpoints. */
0d381245
VP
14383 {
14384 struct bp_location *e = existing_locations;
776592bf
DE
14385 /* If there are multiple breakpoints with the same function name,
14386 e.g. for inline functions, comparing function names won't work.
14387 Instead compare pc addresses; this is just a heuristic as things
14388 may have moved, but in practice it gives the correct answer
14389 often enough until a better solution is found. */
14390 int have_ambiguous_names = ambiguous_names_p (b->loc);
14391
0d381245
VP
14392 for (; e; e = e->next)
14393 {
14394 if (!e->enabled && e->function_name)
14395 {
14396 struct bp_location *l = b->loc;
776592bf
DE
14397 if (have_ambiguous_names)
14398 {
14399 for (; l; l = l->next)
f1310107 14400 if (breakpoint_locations_match (e, l))
776592bf
DE
14401 {
14402 l->enabled = 0;
14403 break;
14404 }
14405 }
14406 else
14407 {
14408 for (; l; l = l->next)
14409 if (l->function_name
14410 && strcmp (e->function_name, l->function_name) == 0)
14411 {
14412 l->enabled = 0;
14413 break;
14414 }
14415 }
0d381245
VP
14416 }
14417 }
14418 }
fe3f5fa8 14419
8d3788bd
VP
14420 if (!locations_are_equal (existing_locations, b->loc))
14421 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14422}
14423
f00aae0f 14424/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14425 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14426
14427static struct symtabs_and_lines
f00aae0f 14428location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14429 struct program_space *search_pspace, int *found)
ef23e705 14430{
02d20e4a 14431 struct symtabs_and_lines sals = {0};
492d29ea 14432 struct gdb_exception exception = exception_none;
ef23e705 14433
983af33b 14434 gdb_assert (b->ops != NULL);
ef23e705 14435
492d29ea 14436 TRY
ef23e705 14437 {
c2f4122d 14438 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14439 }
492d29ea 14440 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14441 {
14442 int not_found_and_ok = 0;
492d29ea
PA
14443
14444 exception = e;
14445
ef23e705
TJB
14446 /* For pending breakpoints, it's expected that parsing will
14447 fail until the right shared library is loaded. User has
14448 already told to create pending breakpoints and don't need
14449 extra messages. If breakpoint is in bp_shlib_disabled
14450 state, then user already saw the message about that
14451 breakpoint being disabled, and don't want to see more
14452 errors. */
58438ac1 14453 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14454 && (b->condition_not_parsed
14455 || (b->loc != NULL
14456 && search_pspace != NULL
14457 && b->loc->pspace != search_pspace)
ef23e705 14458 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14459 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14460 || b->enable_state == bp_disabled))
14461 not_found_and_ok = 1;
14462
14463 if (!not_found_and_ok)
14464 {
14465 /* We surely don't want to warn about the same breakpoint
14466 10 times. One solution, implemented here, is disable
14467 the breakpoint on error. Another solution would be to
14468 have separate 'warning emitted' flag. Since this
14469 happens only when a binary has changed, I don't know
14470 which approach is better. */
14471 b->enable_state = bp_disabled;
14472 throw_exception (e);
14473 }
14474 }
492d29ea 14475 END_CATCH
ef23e705 14476
492d29ea 14477 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14478 {
f8eba3c6 14479 int i;
ef23e705 14480
f8eba3c6
TT
14481 for (i = 0; i < sals.nelts; ++i)
14482 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14483 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14484 {
ed1d1739
KS
14485 char *cond_string, *extra_string;
14486 int thread, task;
ef23e705 14487
f00aae0f 14488 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14489 &cond_string, &thread, &task,
14490 &extra_string);
f00aae0f 14491 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14492 if (cond_string)
14493 b->cond_string = cond_string;
14494 b->thread = thread;
14495 b->task = task;
e7e0cddf 14496 if (extra_string)
f00aae0f
KS
14497 {
14498 xfree (b->extra_string);
14499 b->extra_string = extra_string;
14500 }
ef23e705
TJB
14501 b->condition_not_parsed = 0;
14502 }
14503
983af33b 14504 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14505 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14506
58438ac1
TT
14507 *found = 1;
14508 }
14509 else
14510 *found = 0;
ef23e705
TJB
14511
14512 return sals;
14513}
14514
348d480f
PA
14515/* The default re_set method, for typical hardware or software
14516 breakpoints. Reevaluate the breakpoint and recreate its
14517 locations. */
14518
14519static void
28010a5d 14520breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14521{
14522 int found;
f1310107 14523 struct symtabs_and_lines sals, sals_end;
ef23e705 14524 struct symtabs_and_lines expanded = {0};
f1310107 14525 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14526 struct program_space *filter_pspace = current_program_space;
ef23e705 14527
c2f4122d 14528 sals = location_to_sals (b, b->location, filter_pspace, &found);
ef23e705
TJB
14529 if (found)
14530 {
14531 make_cleanup (xfree, sals.sals);
f8eba3c6 14532 expanded = sals;
ef23e705
TJB
14533 }
14534
f00aae0f 14535 if (b->location_range_end != NULL)
f1310107 14536 {
c2f4122d
PA
14537 sals_end = location_to_sals (b, b->location_range_end,
14538 filter_pspace, &found);
f1310107
TJB
14539 if (found)
14540 {
14541 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14542 expanded_end = sals_end;
f1310107
TJB
14543 }
14544 }
14545
c2f4122d 14546 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14547}
14548
983af33b
SDJ
14549/* Default method for creating SALs from an address string. It basically
14550 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14551
14552static void
f00aae0f
KS
14553create_sals_from_location_default (const struct event_location *location,
14554 struct linespec_result *canonical,
14555 enum bptype type_wanted)
983af33b 14556{
f00aae0f 14557 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14558}
14559
14560/* Call create_breakpoints_sal for the given arguments. This is the default
14561 function for the `create_breakpoints_sal' method of
14562 breakpoint_ops. */
14563
14564static void
14565create_breakpoints_sal_default (struct gdbarch *gdbarch,
14566 struct linespec_result *canonical,
983af33b 14567 char *cond_string,
e7e0cddf 14568 char *extra_string,
983af33b
SDJ
14569 enum bptype type_wanted,
14570 enum bpdisp disposition,
14571 int thread,
14572 int task, int ignore_count,
14573 const struct breakpoint_ops *ops,
14574 int from_tty, int enabled,
44f238bb 14575 int internal, unsigned flags)
983af33b
SDJ
14576{
14577 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14578 extra_string,
983af33b
SDJ
14579 type_wanted, disposition,
14580 thread, task, ignore_count, ops, from_tty,
44f238bb 14581 enabled, internal, flags);
983af33b
SDJ
14582}
14583
14584/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14585 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14586
14587static void
f00aae0f
KS
14588decode_location_default (struct breakpoint *b,
14589 const struct event_location *location,
c2f4122d 14590 struct program_space *search_pspace,
983af33b
SDJ
14591 struct symtabs_and_lines *sals)
14592{
14593 struct linespec_result canonical;
14594
14595 init_linespec_result (&canonical);
c2f4122d 14596 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14597 (struct symtab *) NULL, 0,
14598 &canonical, multiple_symbols_all,
14599 b->filter);
14600
14601 /* We should get 0 or 1 resulting SALs. */
14602 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14603
14604 if (VEC_length (linespec_sals, canonical.sals) > 0)
14605 {
14606 struct linespec_sals *lsal;
14607
14608 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14609 *sals = lsal->sals;
14610 /* Arrange it so the destructor does not free the
14611 contents. */
14612 lsal->sals.sals = NULL;
14613 }
14614
14615 destroy_linespec_result (&canonical);
14616}
14617
28010a5d
PA
14618/* Prepare the global context for a re-set of breakpoint B. */
14619
14620static struct cleanup *
14621prepare_re_set_context (struct breakpoint *b)
14622{
28010a5d 14623 input_radix = b->input_radix;
28010a5d
PA
14624 set_language (b->language);
14625
c2f4122d 14626 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14627}
14628
c906108c
SS
14629/* Reset a breakpoint given it's struct breakpoint * BINT.
14630 The value we return ends up being the return value from catch_errors.
14631 Unused in this case. */
14632
14633static int
4efb68b1 14634breakpoint_re_set_one (void *bint)
c906108c 14635{
4a64f543 14636 /* Get past catch_errs. */
53a5351d 14637 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14638 struct cleanup *cleanups;
c906108c 14639
348d480f
PA
14640 cleanups = prepare_re_set_context (b);
14641 b->ops->re_set (b);
14642 do_cleanups (cleanups);
c906108c
SS
14643 return 0;
14644}
14645
c2f4122d
PA
14646/* Re-set breakpoint locations for the current program space.
14647 Locations bound to other program spaces are left untouched. */
14648
c906108c 14649void
69de3c6a 14650breakpoint_re_set (void)
c906108c 14651{
35df4500 14652 struct breakpoint *b, *b_tmp;
c906108c
SS
14653 enum language save_language;
14654 int save_input_radix;
6c95b8df 14655 struct cleanup *old_chain;
c5aa993b 14656
c906108c
SS
14657 save_language = current_language->la_language;
14658 save_input_radix = input_radix;
c2f4122d 14659 old_chain = save_current_space_and_thread ();
6c95b8df 14660
2a7f3dff
PA
14661 /* Note: we must not try to insert locations until after all
14662 breakpoints have been re-set. Otherwise, e.g., when re-setting
14663 breakpoint 1, we'd insert the locations of breakpoint 2, which
14664 hadn't been re-set yet, and thus may have stale locations. */
14665
35df4500 14666 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14667 {
4a64f543 14668 /* Format possible error msg. */
fe3f5fa8 14669 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14670 b->number);
14671 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14672 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14673 do_cleanups (cleanups);
c5aa993b 14674 }
c906108c
SS
14675 set_language (save_language);
14676 input_radix = save_input_radix;
e62c965a 14677
0756c555 14678 jit_breakpoint_re_set ();
4efc6507 14679
6c95b8df
PA
14680 do_cleanups (old_chain);
14681
af02033e
PP
14682 create_overlay_event_breakpoint ();
14683 create_longjmp_master_breakpoint ();
14684 create_std_terminate_master_breakpoint ();
186c406b 14685 create_exception_master_breakpoint ();
2a7f3dff
PA
14686
14687 /* Now we can insert. */
14688 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14689}
14690\f
c906108c
SS
14691/* Reset the thread number of this breakpoint:
14692
14693 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14694 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14695void
fba45db2 14696breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14697{
14698 if (b->thread != -1)
14699 {
39f77062 14700 if (in_thread_list (inferior_ptid))
5d5658a1 14701 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14702
14703 /* We're being called after following a fork. The new fork is
14704 selected as current, and unless this was a vfork will have a
14705 different program space from the original thread. Reset that
14706 as well. */
14707 b->loc->pspace = current_program_space;
c906108c
SS
14708 }
14709}
14710
03ac34d5
MS
14711/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14712 If from_tty is nonzero, it prints a message to that effect,
14713 which ends with a period (no newline). */
14714
c906108c 14715void
fba45db2 14716set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14717{
52f0bd74 14718 struct breakpoint *b;
c906108c
SS
14719
14720 if (count < 0)
14721 count = 0;
14722
14723 ALL_BREAKPOINTS (b)
14724 if (b->number == bptnum)
c5aa993b 14725 {
d77f58be
SS
14726 if (is_tracepoint (b))
14727 {
14728 if (from_tty && count != 0)
14729 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14730 bptnum);
14731 return;
14732 }
14733
c5aa993b 14734 b->ignore_count = count;
221ea385
KS
14735 if (from_tty)
14736 {
14737 if (count == 0)
3e43a32a
MS
14738 printf_filtered (_("Will stop next time "
14739 "breakpoint %d is reached."),
221ea385
KS
14740 bptnum);
14741 else if (count == 1)
a3f17187 14742 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14743 bptnum);
14744 else
3e43a32a
MS
14745 printf_filtered (_("Will ignore next %d "
14746 "crossings of breakpoint %d."),
221ea385
KS
14747 count, bptnum);
14748 }
8d3788bd 14749 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14750 return;
14751 }
c906108c 14752
8a3fe4f8 14753 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14754}
14755
c906108c
SS
14756/* Command to set ignore-count of breakpoint N to COUNT. */
14757
14758static void
fba45db2 14759ignore_command (char *args, int from_tty)
c906108c
SS
14760{
14761 char *p = args;
52f0bd74 14762 int num;
c906108c
SS
14763
14764 if (p == 0)
e2e0b3e5 14765 error_no_arg (_("a breakpoint number"));
c5aa993b 14766
c906108c 14767 num = get_number (&p);
5c44784c 14768 if (num == 0)
8a3fe4f8 14769 error (_("bad breakpoint number: '%s'"), args);
c906108c 14770 if (*p == 0)
8a3fe4f8 14771 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14772
14773 set_ignore_count (num,
14774 longest_to_int (value_as_long (parse_and_eval (p))),
14775 from_tty);
221ea385
KS
14776 if (from_tty)
14777 printf_filtered ("\n");
c906108c
SS
14778}
14779\f
14780/* Call FUNCTION on each of the breakpoints
14781 whose numbers are given in ARGS. */
14782
14783static void
95a42b64
TT
14784map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14785 void *),
14786 void *data)
c906108c 14787{
52f0bd74
AC
14788 int num;
14789 struct breakpoint *b, *tmp;
c906108c 14790
b9d61307 14791 if (args == 0 || *args == '\0')
e2e0b3e5 14792 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14793
bfd28288 14794 number_or_range_parser parser (args);
197f0a60 14795
bfd28288 14796 while (!parser.finished ())
c906108c 14797 {
bfd28288
PA
14798 const char *p = parser.cur_tok ();
14799 bool match = false;
197f0a60 14800
bfd28288 14801 num = parser.get_number ();
5c44784c 14802 if (num == 0)
c5aa993b 14803 {
8a3fe4f8 14804 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14805 }
14806 else
14807 {
14808 ALL_BREAKPOINTS_SAFE (b, tmp)
14809 if (b->number == num)
14810 {
bfd28288 14811 match = true;
cdac0397 14812 function (b, data);
11cf8741 14813 break;
5c44784c 14814 }
bfd28288 14815 if (!match)
a3f17187 14816 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14817 }
c906108c
SS
14818 }
14819}
14820
0d381245
VP
14821static struct bp_location *
14822find_location_by_number (char *number)
14823{
14824 char *dot = strchr (number, '.');
14825 char *p1;
14826 int bp_num;
14827 int loc_num;
14828 struct breakpoint *b;
14829 struct bp_location *loc;
14830
14831 *dot = '\0';
14832
14833 p1 = number;
197f0a60 14834 bp_num = get_number (&p1);
0d381245
VP
14835 if (bp_num == 0)
14836 error (_("Bad breakpoint number '%s'"), number);
14837
14838 ALL_BREAKPOINTS (b)
14839 if (b->number == bp_num)
14840 {
14841 break;
14842 }
14843
14844 if (!b || b->number != bp_num)
14845 error (_("Bad breakpoint number '%s'"), number);
14846
14847 p1 = dot+1;
197f0a60 14848 loc_num = get_number (&p1);
0d381245
VP
14849 if (loc_num == 0)
14850 error (_("Bad breakpoint location number '%s'"), number);
14851
14852 --loc_num;
14853 loc = b->loc;
14854 for (;loc_num && loc; --loc_num, loc = loc->next)
14855 ;
14856 if (!loc)
14857 error (_("Bad breakpoint location number '%s'"), dot+1);
14858
14859 return loc;
14860}
14861
14862
1900040c
MS
14863/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14864 If from_tty is nonzero, it prints a message to that effect,
14865 which ends with a period (no newline). */
14866
c906108c 14867void
fba45db2 14868disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14869{
14870 /* Never disable a watchpoint scope breakpoint; we want to
14871 hit them when we leave scope so we can delete both the
14872 watchpoint and its scope breakpoint at that time. */
14873 if (bpt->type == bp_watchpoint_scope)
14874 return;
14875
b5de0fa7 14876 bpt->enable_state = bp_disabled;
c906108c 14877
b775012e
LM
14878 /* Mark breakpoint locations modified. */
14879 mark_breakpoint_modified (bpt);
14880
d248b706
KY
14881 if (target_supports_enable_disable_tracepoint ()
14882 && current_trace_status ()->running && is_tracepoint (bpt))
14883 {
14884 struct bp_location *location;
14885
14886 for (location = bpt->loc; location; location = location->next)
14887 target_disable_tracepoint (location);
14888 }
14889
44702360 14890 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14891
8d3788bd 14892 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14893}
14894
51be5b68
PA
14895/* A callback for iterate_over_related_breakpoints. */
14896
14897static void
14898do_disable_breakpoint (struct breakpoint *b, void *ignore)
14899{
14900 disable_breakpoint (b);
14901}
14902
95a42b64
TT
14903/* A callback for map_breakpoint_numbers that calls
14904 disable_breakpoint. */
14905
14906static void
14907do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14908{
51be5b68 14909 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14910}
14911
c906108c 14912static void
fba45db2 14913disable_command (char *args, int from_tty)
c906108c 14914{
c906108c 14915 if (args == 0)
46c6471b
PA
14916 {
14917 struct breakpoint *bpt;
14918
14919 ALL_BREAKPOINTS (bpt)
14920 if (user_breakpoint_p (bpt))
14921 disable_breakpoint (bpt);
14922 }
9eaabc75 14923 else
0d381245 14924 {
9eaabc75
MW
14925 char *num = extract_arg (&args);
14926
14927 while (num)
d248b706 14928 {
9eaabc75 14929 if (strchr (num, '.'))
b775012e 14930 {
9eaabc75
MW
14931 struct bp_location *loc = find_location_by_number (num);
14932
14933 if (loc)
14934 {
14935 if (loc->enabled)
14936 {
14937 loc->enabled = 0;
14938 mark_breakpoint_location_modified (loc);
14939 }
14940 if (target_supports_enable_disable_tracepoint ()
14941 && current_trace_status ()->running && loc->owner
14942 && is_tracepoint (loc->owner))
14943 target_disable_tracepoint (loc);
14944 }
44702360 14945 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14946 }
9eaabc75
MW
14947 else
14948 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14949 num = extract_arg (&args);
d248b706 14950 }
0d381245 14951 }
c906108c
SS
14952}
14953
14954static void
816338b5
SS
14955enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14956 int count)
c906108c 14957{
afe38095 14958 int target_resources_ok;
c906108c
SS
14959
14960 if (bpt->type == bp_hardware_breakpoint)
14961 {
14962 int i;
c5aa993b 14963 i = hw_breakpoint_used_count ();
53a5351d 14964 target_resources_ok =
d92524f1 14965 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14966 i + 1, 0);
c906108c 14967 if (target_resources_ok == 0)
8a3fe4f8 14968 error (_("No hardware breakpoint support in the target."));
c906108c 14969 else if (target_resources_ok < 0)
8a3fe4f8 14970 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14971 }
14972
cc60f2e3 14973 if (is_watchpoint (bpt))
c906108c 14974 {
d07205c2 14975 /* Initialize it just to avoid a GCC false warning. */
f486487f 14976 enum enable_state orig_enable_state = bp_disabled;
dde02812 14977
492d29ea 14978 TRY
c906108c 14979 {
3a5c3e22
PA
14980 struct watchpoint *w = (struct watchpoint *) bpt;
14981
1e718ff1
TJB
14982 orig_enable_state = bpt->enable_state;
14983 bpt->enable_state = bp_enabled;
3a5c3e22 14984 update_watchpoint (w, 1 /* reparse */);
c906108c 14985 }
492d29ea 14986 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14987 {
1e718ff1 14988 bpt->enable_state = orig_enable_state;
dde02812
ES
14989 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14990 bpt->number);
14991 return;
c5aa993b 14992 }
492d29ea 14993 END_CATCH
c906108c 14994 }
0101ce28 14995
b775012e
LM
14996 bpt->enable_state = bp_enabled;
14997
14998 /* Mark breakpoint locations modified. */
14999 mark_breakpoint_modified (bpt);
15000
d248b706
KY
15001 if (target_supports_enable_disable_tracepoint ()
15002 && current_trace_status ()->running && is_tracepoint (bpt))
15003 {
15004 struct bp_location *location;
15005
15006 for (location = bpt->loc; location; location = location->next)
15007 target_enable_tracepoint (location);
15008 }
15009
b4c291bb 15010 bpt->disposition = disposition;
816338b5 15011 bpt->enable_count = count;
44702360 15012 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15013
8d3788bd 15014 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15015}
15016
fe3f5fa8 15017
c906108c 15018void
fba45db2 15019enable_breakpoint (struct breakpoint *bpt)
c906108c 15020{
816338b5 15021 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15022}
15023
15024static void
15025do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15026{
15027 enable_breakpoint (bpt);
c906108c
SS
15028}
15029
95a42b64
TT
15030/* A callback for map_breakpoint_numbers that calls
15031 enable_breakpoint. */
15032
15033static void
15034do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15035{
51be5b68 15036 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15037}
15038
c906108c
SS
15039/* The enable command enables the specified breakpoints (or all defined
15040 breakpoints) so they once again become (or continue to be) effective
1272ad14 15041 in stopping the inferior. */
c906108c 15042
c906108c 15043static void
fba45db2 15044enable_command (char *args, int from_tty)
c906108c 15045{
c906108c 15046 if (args == 0)
46c6471b
PA
15047 {
15048 struct breakpoint *bpt;
15049
15050 ALL_BREAKPOINTS (bpt)
15051 if (user_breakpoint_p (bpt))
15052 enable_breakpoint (bpt);
15053 }
9eaabc75 15054 else
0d381245 15055 {
9eaabc75
MW
15056 char *num = extract_arg (&args);
15057
15058 while (num)
d248b706 15059 {
9eaabc75 15060 if (strchr (num, '.'))
b775012e 15061 {
9eaabc75
MW
15062 struct bp_location *loc = find_location_by_number (num);
15063
15064 if (loc)
15065 {
15066 if (!loc->enabled)
15067 {
15068 loc->enabled = 1;
15069 mark_breakpoint_location_modified (loc);
15070 }
15071 if (target_supports_enable_disable_tracepoint ()
15072 && current_trace_status ()->running && loc->owner
15073 && is_tracepoint (loc->owner))
15074 target_enable_tracepoint (loc);
15075 }
44702360 15076 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15077 }
9eaabc75
MW
15078 else
15079 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15080 num = extract_arg (&args);
d248b706 15081 }
0d381245 15082 }
c906108c
SS
15083}
15084
816338b5
SS
15085/* This struct packages up disposition data for application to multiple
15086 breakpoints. */
15087
15088struct disp_data
15089{
15090 enum bpdisp disp;
15091 int count;
15092};
15093
c906108c 15094static void
51be5b68
PA
15095do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15096{
816338b5 15097 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15098
816338b5 15099 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15100}
15101
15102static void
15103do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15104{
816338b5 15105 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15106
15107 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15108}
15109
c906108c 15110static void
fba45db2 15111enable_once_command (char *args, int from_tty)
c906108c 15112{
51be5b68 15113 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15114}
15115
816338b5
SS
15116static void
15117do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15118{
15119 struct disp_data disp = { disp_disable, *(int *) countptr };
15120
15121 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15122}
15123
15124static void
15125enable_count_command (char *args, int from_tty)
15126{
b9d61307
SM
15127 int count;
15128
15129 if (args == NULL)
15130 error_no_arg (_("hit count"));
15131
15132 count = get_number (&args);
816338b5
SS
15133
15134 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15135}
15136
c906108c 15137static void
51be5b68 15138do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15139{
816338b5 15140 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15141
15142 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15143}
15144
c906108c 15145static void
fba45db2 15146enable_delete_command (char *args, int from_tty)
c906108c 15147{
51be5b68 15148 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15149}
15150\f
fa8d40ab
JJ
15151static void
15152set_breakpoint_cmd (char *args, int from_tty)
15153{
15154}
15155
15156static void
15157show_breakpoint_cmd (char *args, int from_tty)
15158{
15159}
15160
1f3b5d1b
PP
15161/* Invalidate last known value of any hardware watchpoint if
15162 the memory which that value represents has been written to by
15163 GDB itself. */
15164
15165static void
8de0566d
YQ
15166invalidate_bp_value_on_memory_change (struct inferior *inferior,
15167 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15168 const bfd_byte *data)
15169{
15170 struct breakpoint *bp;
15171
15172 ALL_BREAKPOINTS (bp)
15173 if (bp->enable_state == bp_enabled
3a5c3e22 15174 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15175 {
3a5c3e22 15176 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15177
3a5c3e22
PA
15178 if (wp->val_valid && wp->val)
15179 {
15180 struct bp_location *loc;
15181
15182 for (loc = bp->loc; loc != NULL; loc = loc->next)
15183 if (loc->loc_type == bp_loc_hardware_watchpoint
15184 && loc->address + loc->length > addr
15185 && addr + len > loc->address)
15186 {
15187 value_free (wp->val);
15188 wp->val = NULL;
15189 wp->val_valid = 0;
15190 }
15191 }
1f3b5d1b
PP
15192 }
15193}
15194
8181d85f
DJ
15195/* Create and insert a breakpoint for software single step. */
15196
15197void
6c95b8df 15198insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15199 struct address_space *aspace,
15200 CORE_ADDR next_pc)
8181d85f 15201{
7c16b83e
PA
15202 struct thread_info *tp = inferior_thread ();
15203 struct symtab_and_line sal;
15204 CORE_ADDR pc = next_pc;
8181d85f 15205
34b7e8a6
PA
15206 if (tp->control.single_step_breakpoints == NULL)
15207 {
15208 tp->control.single_step_breakpoints
5d5658a1 15209 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15210 }
8181d85f 15211
7c16b83e
PA
15212 sal = find_pc_line (pc, 0);
15213 sal.pc = pc;
15214 sal.section = find_pc_overlay (pc);
15215 sal.explicit_pc = 1;
34b7e8a6 15216 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15217
7c16b83e 15218 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15219}
15220
34b7e8a6 15221/* See breakpoint.h. */
f02253f1
HZ
15222
15223int
7c16b83e
PA
15224breakpoint_has_location_inserted_here (struct breakpoint *bp,
15225 struct address_space *aspace,
15226 CORE_ADDR pc)
1aafd4da 15227{
7c16b83e 15228 struct bp_location *loc;
1aafd4da 15229
7c16b83e
PA
15230 for (loc = bp->loc; loc != NULL; loc = loc->next)
15231 if (loc->inserted
15232 && breakpoint_location_address_match (loc, aspace, pc))
15233 return 1;
1aafd4da 15234
7c16b83e 15235 return 0;
ef370185
JB
15236}
15237
15238/* Check whether a software single-step breakpoint is inserted at
15239 PC. */
15240
15241int
15242single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15243 CORE_ADDR pc)
15244{
34b7e8a6
PA
15245 struct breakpoint *bpt;
15246
15247 ALL_BREAKPOINTS (bpt)
15248 {
15249 if (bpt->type == bp_single_step
15250 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15251 return 1;
15252 }
15253 return 0;
1aafd4da
UW
15254}
15255
1042e4c0
SS
15256/* Tracepoint-specific operations. */
15257
15258/* Set tracepoint count to NUM. */
15259static void
15260set_tracepoint_count (int num)
15261{
15262 tracepoint_count = num;
4fa62494 15263 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15264}
15265
70221824 15266static void
1042e4c0
SS
15267trace_command (char *arg, int from_tty)
15268{
55aa24fb 15269 struct breakpoint_ops *ops;
f00aae0f
KS
15270 struct event_location *location;
15271 struct cleanup *back_to;
55aa24fb 15272
f00aae0f
KS
15273 location = string_to_event_location (&arg, current_language);
15274 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15275 if (location != NULL
15276 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15277 ops = &tracepoint_probe_breakpoint_ops;
15278 else
15279 ops = &tracepoint_breakpoint_ops;
15280
558a9d82 15281 create_breakpoint (get_current_arch (),
f00aae0f
KS
15282 location,
15283 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15284 0 /* tempflag */,
15285 bp_tracepoint /* type_wanted */,
15286 0 /* Ignore count */,
15287 pending_break_support,
15288 ops,
15289 from_tty,
15290 1 /* enabled */,
15291 0 /* internal */, 0);
f00aae0f 15292 do_cleanups (back_to);
1042e4c0
SS
15293}
15294
70221824 15295static void
7a697b8d
SS
15296ftrace_command (char *arg, int from_tty)
15297{
f00aae0f
KS
15298 struct event_location *location;
15299 struct cleanup *back_to;
15300
15301 location = string_to_event_location (&arg, current_language);
15302 back_to = make_cleanup_delete_event_location (location);
558a9d82 15303 create_breakpoint (get_current_arch (),
f00aae0f
KS
15304 location,
15305 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15306 0 /* tempflag */,
15307 bp_fast_tracepoint /* type_wanted */,
15308 0 /* Ignore count */,
15309 pending_break_support,
15310 &tracepoint_breakpoint_ops,
15311 from_tty,
15312 1 /* enabled */,
15313 0 /* internal */, 0);
f00aae0f 15314 do_cleanups (back_to);
0fb4aa4b
PA
15315}
15316
15317/* strace command implementation. Creates a static tracepoint. */
15318
70221824 15319static void
0fb4aa4b
PA
15320strace_command (char *arg, int from_tty)
15321{
983af33b 15322 struct breakpoint_ops *ops;
f00aae0f
KS
15323 struct event_location *location;
15324 struct cleanup *back_to;
983af33b
SDJ
15325
15326 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15327 or with a normal static tracepoint. */
61012eef 15328 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15329 {
15330 ops = &strace_marker_breakpoint_ops;
15331 location = new_linespec_location (&arg);
15332 }
983af33b 15333 else
f00aae0f
KS
15334 {
15335 ops = &tracepoint_breakpoint_ops;
15336 location = string_to_event_location (&arg, current_language);
15337 }
983af33b 15338
f00aae0f 15339 back_to = make_cleanup_delete_event_location (location);
558a9d82 15340 create_breakpoint (get_current_arch (),
f00aae0f
KS
15341 location,
15342 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15343 0 /* tempflag */,
15344 bp_static_tracepoint /* type_wanted */,
15345 0 /* Ignore count */,
15346 pending_break_support,
15347 ops,
15348 from_tty,
15349 1 /* enabled */,
15350 0 /* internal */, 0);
f00aae0f 15351 do_cleanups (back_to);
7a697b8d
SS
15352}
15353
409873ef
SS
15354/* Set up a fake reader function that gets command lines from a linked
15355 list that was acquired during tracepoint uploading. */
15356
15357static struct uploaded_tp *this_utp;
3149d8c1 15358static int next_cmd;
409873ef
SS
15359
15360static char *
15361read_uploaded_action (void)
15362{
15363 char *rslt;
15364
3149d8c1 15365 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15366
3149d8c1 15367 next_cmd++;
409873ef
SS
15368
15369 return rslt;
15370}
15371
00bf0b85
SS
15372/* Given information about a tracepoint as recorded on a target (which
15373 can be either a live system or a trace file), attempt to create an
15374 equivalent GDB tracepoint. This is not a reliable process, since
15375 the target does not necessarily have all the information used when
15376 the tracepoint was originally defined. */
15377
d9b3f62e 15378struct tracepoint *
00bf0b85 15379create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15380{
409873ef 15381 char *addr_str, small_buf[100];
d9b3f62e 15382 struct tracepoint *tp;
f00aae0f
KS
15383 struct event_location *location;
15384 struct cleanup *cleanup;
fd9b8c24 15385
409873ef
SS
15386 if (utp->at_string)
15387 addr_str = utp->at_string;
15388 else
15389 {
15390 /* In the absence of a source location, fall back to raw
15391 address. Since there is no way to confirm that the address
15392 means the same thing as when the trace was started, warn the
15393 user. */
3e43a32a
MS
15394 warning (_("Uploaded tracepoint %d has no "
15395 "source location, using raw address"),
409873ef 15396 utp->number);
8c042590 15397 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15398 addr_str = small_buf;
15399 }
15400
15401 /* There's not much we can do with a sequence of bytecodes. */
15402 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15403 warning (_("Uploaded tracepoint %d condition "
15404 "has no source form, ignoring it"),
409873ef 15405 utp->number);
d5551862 15406
f00aae0f
KS
15407 location = string_to_event_location (&addr_str, current_language);
15408 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15409 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15410 location,
15411 utp->cond_string, -1, addr_str,
e7e0cddf 15412 0 /* parse cond/thread */,
8cdf0e15 15413 0 /* tempflag */,
0fb4aa4b 15414 utp->type /* type_wanted */,
8cdf0e15
VP
15415 0 /* Ignore count */,
15416 pending_break_support,
348d480f 15417 &tracepoint_breakpoint_ops,
8cdf0e15 15418 0 /* from_tty */,
84f4c1fe 15419 utp->enabled /* enabled */,
44f238bb
PA
15420 0 /* internal */,
15421 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15422 {
15423 do_cleanups (cleanup);
15424 return NULL;
15425 }
15426
15427 do_cleanups (cleanup);
fd9b8c24 15428
409873ef 15429 /* Get the tracepoint we just created. */
fd9b8c24
PA
15430 tp = get_tracepoint (tracepoint_count);
15431 gdb_assert (tp != NULL);
d5551862 15432
00bf0b85
SS
15433 if (utp->pass > 0)
15434 {
8c042590
PM
15435 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15436 tp->base.number);
00bf0b85 15437
409873ef 15438 trace_pass_command (small_buf, 0);
00bf0b85
SS
15439 }
15440
409873ef
SS
15441 /* If we have uploaded versions of the original commands, set up a
15442 special-purpose "reader" function and call the usual command line
15443 reader, then pass the result to the breakpoint command-setting
15444 function. */
3149d8c1 15445 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15446 {
409873ef 15447 struct command_line *cmd_list;
00bf0b85 15448
409873ef 15449 this_utp = utp;
3149d8c1 15450 next_cmd = 0;
d5551862 15451
409873ef
SS
15452 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15453
d9b3f62e 15454 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15455 }
3149d8c1
SS
15456 else if (!VEC_empty (char_ptr, utp->actions)
15457 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15458 warning (_("Uploaded tracepoint %d actions "
15459 "have no source form, ignoring them"),
409873ef 15460 utp->number);
00bf0b85 15461
f196051f
SS
15462 /* Copy any status information that might be available. */
15463 tp->base.hit_count = utp->hit_count;
15464 tp->traceframe_usage = utp->traceframe_usage;
15465
00bf0b85 15466 return tp;
d9b3f62e 15467}
00bf0b85 15468
1042e4c0
SS
15469/* Print information on tracepoint number TPNUM_EXP, or all if
15470 omitted. */
15471
15472static void
e5a67952 15473tracepoints_info (char *args, int from_tty)
1042e4c0 15474{
79a45e25 15475 struct ui_out *uiout = current_uiout;
e5a67952 15476 int num_printed;
1042e4c0 15477
e5a67952 15478 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15479
15480 if (num_printed == 0)
1042e4c0 15481 {
e5a67952 15482 if (args == NULL || *args == '\0')
d77f58be
SS
15483 ui_out_message (uiout, 0, "No tracepoints.\n");
15484 else
e5a67952 15485 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15486 }
ad443146
SS
15487
15488 default_collect_info ();
1042e4c0
SS
15489}
15490
4a64f543 15491/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15492 Not supported by all targets. */
15493static void
15494enable_trace_command (char *args, int from_tty)
15495{
15496 enable_command (args, from_tty);
15497}
15498
4a64f543 15499/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15500 Not supported by all targets. */
15501static void
15502disable_trace_command (char *args, int from_tty)
15503{
15504 disable_command (args, from_tty);
15505}
15506
4a64f543 15507/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15508static void
15509delete_trace_command (char *arg, int from_tty)
15510{
35df4500 15511 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15512
15513 dont_repeat ();
15514
15515 if (arg == 0)
15516 {
15517 int breaks_to_delete = 0;
15518
15519 /* Delete all breakpoints if no argument.
15520 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15521 have to be deleted with an explicit breakpoint number
15522 argument. */
1042e4c0 15523 ALL_TRACEPOINTS (b)
46c6471b 15524 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15525 {
15526 breaks_to_delete = 1;
15527 break;
15528 }
1042e4c0
SS
15529
15530 /* Ask user only if there are some breakpoints to delete. */
15531 if (!from_tty
15532 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15533 {
35df4500 15534 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15535 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15536 delete_breakpoint (b);
1042e4c0
SS
15537 }
15538 }
15539 else
51be5b68 15540 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15541}
15542
197f0a60
TT
15543/* Helper function for trace_pass_command. */
15544
15545static void
d9b3f62e 15546trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15547{
d9b3f62e 15548 tp->pass_count = count;
6f6484cd 15549 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15550 if (from_tty)
15551 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15552 tp->base.number, count);
197f0a60
TT
15553}
15554
1042e4c0
SS
15555/* Set passcount for tracepoint.
15556
15557 First command argument is passcount, second is tracepoint number.
15558 If tracepoint number omitted, apply to most recently defined.
15559 Also accepts special argument "all". */
15560
15561static void
15562trace_pass_command (char *args, int from_tty)
15563{
d9b3f62e 15564 struct tracepoint *t1;
1042e4c0 15565 unsigned int count;
1042e4c0
SS
15566
15567 if (args == 0 || *args == 0)
3e43a32a
MS
15568 error (_("passcount command requires an "
15569 "argument (count + optional TP num)"));
1042e4c0 15570
4a64f543 15571 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15572
529480d0 15573 args = skip_spaces (args);
1042e4c0
SS
15574 if (*args && strncasecmp (args, "all", 3) == 0)
15575 {
d9b3f62e
PA
15576 struct breakpoint *b;
15577
1042e4c0 15578 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15579 if (*args)
15580 error (_("Junk at end of arguments."));
1042e4c0 15581
d9b3f62e 15582 ALL_TRACEPOINTS (b)
197f0a60 15583 {
d9b3f62e 15584 t1 = (struct tracepoint *) b;
197f0a60
TT
15585 trace_pass_set_count (t1, count, from_tty);
15586 }
15587 }
15588 else if (*args == '\0')
1042e4c0 15589 {
5fa1d40e 15590 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15591 if (t1)
197f0a60
TT
15592 trace_pass_set_count (t1, count, from_tty);
15593 }
15594 else
15595 {
bfd28288
PA
15596 number_or_range_parser parser (args);
15597 while (!parser.finished ())
1042e4c0 15598 {
bfd28288 15599 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15600 if (t1)
15601 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15602 }
15603 }
1042e4c0
SS
15604}
15605
d9b3f62e 15606struct tracepoint *
1042e4c0
SS
15607get_tracepoint (int num)
15608{
15609 struct breakpoint *t;
15610
15611 ALL_TRACEPOINTS (t)
15612 if (t->number == num)
d9b3f62e 15613 return (struct tracepoint *) t;
1042e4c0
SS
15614
15615 return NULL;
15616}
15617
d5551862
SS
15618/* Find the tracepoint with the given target-side number (which may be
15619 different from the tracepoint number after disconnecting and
15620 reconnecting). */
15621
d9b3f62e 15622struct tracepoint *
d5551862
SS
15623get_tracepoint_by_number_on_target (int num)
15624{
d9b3f62e 15625 struct breakpoint *b;
d5551862 15626
d9b3f62e
PA
15627 ALL_TRACEPOINTS (b)
15628 {
15629 struct tracepoint *t = (struct tracepoint *) b;
15630
15631 if (t->number_on_target == num)
15632 return t;
15633 }
d5551862
SS
15634
15635 return NULL;
15636}
15637
1042e4c0 15638/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15639 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15640 If the argument is missing, the most recent tracepoint
15641 (tracepoint_count) is returned. */
15642
d9b3f62e 15643struct tracepoint *
197f0a60 15644get_tracepoint_by_number (char **arg,
bfd28288 15645 number_or_range_parser *parser)
1042e4c0 15646{
1042e4c0
SS
15647 struct breakpoint *t;
15648 int tpnum;
15649 char *instring = arg == NULL ? NULL : *arg;
15650
bfd28288 15651 if (parser != NULL)
197f0a60 15652 {
bfd28288
PA
15653 gdb_assert (!parser->finished ());
15654 tpnum = parser->get_number ();
197f0a60
TT
15655 }
15656 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15657 tpnum = tracepoint_count;
1042e4c0 15658 else
197f0a60 15659 tpnum = get_number (arg);
1042e4c0
SS
15660
15661 if (tpnum <= 0)
15662 {
15663 if (instring && *instring)
15664 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15665 instring);
15666 else
5fa1d40e 15667 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15668 return NULL;
15669 }
15670
15671 ALL_TRACEPOINTS (t)
15672 if (t->number == tpnum)
15673 {
d9b3f62e 15674 return (struct tracepoint *) t;
1042e4c0
SS
15675 }
15676
1042e4c0
SS
15677 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15678 return NULL;
15679}
15680
d9b3f62e
PA
15681void
15682print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15683{
15684 if (b->thread != -1)
15685 fprintf_unfiltered (fp, " thread %d", b->thread);
15686
15687 if (b->task != 0)
15688 fprintf_unfiltered (fp, " task %d", b->task);
15689
15690 fprintf_unfiltered (fp, "\n");
15691}
15692
6149aea9
PA
15693/* Save information on user settable breakpoints (watchpoints, etc) to
15694 a new script file named FILENAME. If FILTER is non-NULL, call it
15695 on each breakpoint and only include the ones for which it returns
15696 non-zero. */
15697
1042e4c0 15698static void
6149aea9
PA
15699save_breakpoints (char *filename, int from_tty,
15700 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15701{
15702 struct breakpoint *tp;
6149aea9 15703 int any = 0;
1042e4c0 15704 struct cleanup *cleanup;
a7bdde9e 15705 struct ui_file *fp;
6149aea9 15706 int extra_trace_bits = 0;
1042e4c0 15707
6149aea9
PA
15708 if (filename == 0 || *filename == 0)
15709 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15710
15711 /* See if we have anything to save. */
6149aea9 15712 ALL_BREAKPOINTS (tp)
1042e4c0 15713 {
6149aea9 15714 /* Skip internal and momentary breakpoints. */
09d682a4 15715 if (!user_breakpoint_p (tp))
6149aea9
PA
15716 continue;
15717
15718 /* If we have a filter, only save the breakpoints it accepts. */
15719 if (filter && !filter (tp))
15720 continue;
15721
15722 any = 1;
15723
15724 if (is_tracepoint (tp))
15725 {
15726 extra_trace_bits = 1;
15727
15728 /* We can stop searching. */
15729 break;
15730 }
1042e4c0 15731 }
6149aea9
PA
15732
15733 if (!any)
1042e4c0 15734 {
6149aea9 15735 warning (_("Nothing to save."));
1042e4c0
SS
15736 return;
15737 }
15738
c718be47
PA
15739 filename = tilde_expand (filename);
15740 cleanup = make_cleanup (xfree, filename);
15741 fp = gdb_fopen (filename, "w");
059fb39f 15742 if (!fp)
6149aea9
PA
15743 error (_("Unable to open file '%s' for saving (%s)"),
15744 filename, safe_strerror (errno));
a7bdde9e 15745 make_cleanup_ui_file_delete (fp);
8bf6485c 15746
6149aea9
PA
15747 if (extra_trace_bits)
15748 save_trace_state_variables (fp);
8bf6485c 15749
6149aea9 15750 ALL_BREAKPOINTS (tp)
1042e4c0 15751 {
6149aea9 15752 /* Skip internal and momentary breakpoints. */
09d682a4 15753 if (!user_breakpoint_p (tp))
6149aea9 15754 continue;
8bf6485c 15755
6149aea9
PA
15756 /* If we have a filter, only save the breakpoints it accepts. */
15757 if (filter && !filter (tp))
15758 continue;
15759
348d480f 15760 tp->ops->print_recreate (tp, fp);
1042e4c0 15761
6149aea9
PA
15762 /* Note, we can't rely on tp->number for anything, as we can't
15763 assume the recreated breakpoint numbers will match. Use $bpnum
15764 instead. */
15765
15766 if (tp->cond_string)
15767 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15768
15769 if (tp->ignore_count)
15770 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15771
2d9442cc 15772 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15773 {
6149aea9 15774 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15775
79a45e25 15776 ui_out_redirect (current_uiout, fp);
492d29ea 15777 TRY
1042e4c0 15778 {
79a45e25 15779 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15780 }
492d29ea
PA
15781 CATCH (ex, RETURN_MASK_ALL)
15782 {
6c63c96a 15783 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15784 throw_exception (ex);
15785 }
15786 END_CATCH
1042e4c0 15787
6c63c96a 15788 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15789 fprintf_unfiltered (fp, " end\n");
1042e4c0 15790 }
6149aea9
PA
15791
15792 if (tp->enable_state == bp_disabled)
99894e11 15793 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15794
15795 /* If this is a multi-location breakpoint, check if the locations
15796 should be individually disabled. Watchpoint locations are
15797 special, and not user visible. */
15798 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15799 {
15800 struct bp_location *loc;
15801 int n = 1;
15802
15803 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15804 if (!loc->enabled)
15805 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15806 }
1042e4c0 15807 }
8bf6485c 15808
6149aea9 15809 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15810 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15811
1042e4c0 15812 if (from_tty)
6149aea9 15813 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15814 do_cleanups (cleanup);
6149aea9
PA
15815}
15816
15817/* The `save breakpoints' command. */
15818
15819static void
15820save_breakpoints_command (char *args, int from_tty)
15821{
15822 save_breakpoints (args, from_tty, NULL);
15823}
15824
15825/* The `save tracepoints' command. */
15826
15827static void
15828save_tracepoints_command (char *args, int from_tty)
15829{
15830 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15831}
15832
15833/* Create a vector of all tracepoints. */
15834
15835VEC(breakpoint_p) *
eeae04df 15836all_tracepoints (void)
1042e4c0
SS
15837{
15838 VEC(breakpoint_p) *tp_vec = 0;
15839 struct breakpoint *tp;
15840
15841 ALL_TRACEPOINTS (tp)
15842 {
15843 VEC_safe_push (breakpoint_p, tp_vec, tp);
15844 }
15845
15846 return tp_vec;
15847}
15848
c906108c 15849\f
629500fa
KS
15850/* This help string is used to consolidate all the help string for specifying
15851 locations used by several commands. */
15852
15853#define LOCATION_HELP_STRING \
15854"Linespecs are colon-separated lists of location parameters, such as\n\
15855source filename, function name, label name, and line number.\n\
15856Example: To specify the start of a label named \"the_top\" in the\n\
15857function \"fact\" in the file \"factorial.c\", use\n\
15858\"factorial.c:fact:the_top\".\n\
15859\n\
15860Address locations begin with \"*\" and specify an exact address in the\n\
15861program. Example: To specify the fourth byte past the start function\n\
15862\"main\", use \"*main + 4\".\n\
15863\n\
15864Explicit locations are similar to linespecs but use an option/argument\n\
15865syntax to specify location parameters.\n\
15866Example: To specify the start of the label named \"the_top\" in the\n\
15867function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15868-function fact -label the_top\".\n"
15869
4a64f543
MS
15870/* This help string is used for the break, hbreak, tbreak and thbreak
15871 commands. It is defined as a macro to prevent duplication.
15872 COMMAND should be a string constant containing the name of the
15873 command. */
629500fa 15874
31e2b00f 15875#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15876command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15877PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15878probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15879guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15880`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15881LOCATION may be a linespec, address, or explicit location as described\n\
15882below.\n\
15883\n\
dc10affe
PA
15884With no LOCATION, uses current execution address of the selected\n\
15885stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15886\n\
15887THREADNUM is the number from \"info threads\".\n\
15888CONDITION is a boolean expression.\n\
629500fa 15889\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15890Multiple breakpoints at one place are permitted, and useful if their\n\
15891conditions are different.\n\
31e2b00f
AS
15892\n\
15893Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15894
44feb3ce
TT
15895/* List of subcommands for "catch". */
15896static struct cmd_list_element *catch_cmdlist;
15897
15898/* List of subcommands for "tcatch". */
15899static struct cmd_list_element *tcatch_cmdlist;
15900
9ac4176b 15901void
44feb3ce 15902add_catch_command (char *name, char *docstring,
82ae6c8d 15903 cmd_sfunc_ftype *sfunc,
625e8578 15904 completer_ftype *completer,
44feb3ce
TT
15905 void *user_data_catch,
15906 void *user_data_tcatch)
15907{
15908 struct cmd_list_element *command;
15909
15910 command = add_cmd (name, class_breakpoint, NULL, docstring,
15911 &catch_cmdlist);
15912 set_cmd_sfunc (command, sfunc);
15913 set_cmd_context (command, user_data_catch);
a96d9b2e 15914 set_cmd_completer (command, completer);
44feb3ce
TT
15915
15916 command = add_cmd (name, class_breakpoint, NULL, docstring,
15917 &tcatch_cmdlist);
15918 set_cmd_sfunc (command, sfunc);
15919 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15920 set_cmd_completer (command, completer);
44feb3ce
TT
15921}
15922
6149aea9
PA
15923static void
15924save_command (char *arg, int from_tty)
15925{
3e43a32a
MS
15926 printf_unfiltered (_("\"save\" must be followed by "
15927 "the name of a save subcommand.\n"));
635c7e8a 15928 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15929}
15930
84f4c1fe
PM
15931struct breakpoint *
15932iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15933 void *data)
15934{
35df4500 15935 struct breakpoint *b, *b_tmp;
84f4c1fe 15936
35df4500 15937 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15938 {
15939 if ((*callback) (b, data))
15940 return b;
15941 }
15942
15943 return NULL;
15944}
15945
0574c78f
GB
15946/* Zero if any of the breakpoint's locations could be a location where
15947 functions have been inlined, nonzero otherwise. */
15948
15949static int
15950is_non_inline_function (struct breakpoint *b)
15951{
15952 /* The shared library event breakpoint is set on the address of a
15953 non-inline function. */
15954 if (b->type == bp_shlib_event)
15955 return 1;
15956
15957 return 0;
15958}
15959
15960/* Nonzero if the specified PC cannot be a location where functions
15961 have been inlined. */
15962
15963int
09ac7c10
TT
15964pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15965 const struct target_waitstatus *ws)
0574c78f
GB
15966{
15967 struct breakpoint *b;
15968 struct bp_location *bl;
15969
15970 ALL_BREAKPOINTS (b)
15971 {
15972 if (!is_non_inline_function (b))
15973 continue;
15974
15975 for (bl = b->loc; bl != NULL; bl = bl->next)
15976 {
15977 if (!bl->shlib_disabled
09ac7c10 15978 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15979 return 1;
15980 }
15981 }
15982
15983 return 0;
15984}
15985
2f202fde
JK
15986/* Remove any references to OBJFILE which is going to be freed. */
15987
15988void
15989breakpoint_free_objfile (struct objfile *objfile)
15990{
15991 struct bp_location **locp, *loc;
15992
15993 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15994 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15995 loc->symtab = NULL;
15996}
15997
2060206e
PA
15998void
15999initialize_breakpoint_ops (void)
16000{
16001 static int initialized = 0;
16002
16003 struct breakpoint_ops *ops;
16004
16005 if (initialized)
16006 return;
16007 initialized = 1;
16008
16009 /* The breakpoint_ops structure to be inherit by all kinds of
16010 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16011 internal and momentary breakpoints, etc.). */
16012 ops = &bkpt_base_breakpoint_ops;
16013 *ops = base_breakpoint_ops;
16014 ops->re_set = bkpt_re_set;
16015 ops->insert_location = bkpt_insert_location;
16016 ops->remove_location = bkpt_remove_location;
16017 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 16018 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 16019 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 16020 ops->decode_location = bkpt_decode_location;
2060206e
PA
16021
16022 /* The breakpoint_ops structure to be used in regular breakpoints. */
16023 ops = &bkpt_breakpoint_ops;
16024 *ops = bkpt_base_breakpoint_ops;
16025 ops->re_set = bkpt_re_set;
16026 ops->resources_needed = bkpt_resources_needed;
16027 ops->print_it = bkpt_print_it;
16028 ops->print_mention = bkpt_print_mention;
16029 ops->print_recreate = bkpt_print_recreate;
16030
16031 /* Ranged breakpoints. */
16032 ops = &ranged_breakpoint_ops;
16033 *ops = bkpt_breakpoint_ops;
16034 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16035 ops->resources_needed = resources_needed_ranged_breakpoint;
16036 ops->print_it = print_it_ranged_breakpoint;
16037 ops->print_one = print_one_ranged_breakpoint;
16038 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16039 ops->print_mention = print_mention_ranged_breakpoint;
16040 ops->print_recreate = print_recreate_ranged_breakpoint;
16041
16042 /* Internal breakpoints. */
16043 ops = &internal_breakpoint_ops;
16044 *ops = bkpt_base_breakpoint_ops;
16045 ops->re_set = internal_bkpt_re_set;
16046 ops->check_status = internal_bkpt_check_status;
16047 ops->print_it = internal_bkpt_print_it;
16048 ops->print_mention = internal_bkpt_print_mention;
16049
16050 /* Momentary breakpoints. */
16051 ops = &momentary_breakpoint_ops;
16052 *ops = bkpt_base_breakpoint_ops;
16053 ops->re_set = momentary_bkpt_re_set;
16054 ops->check_status = momentary_bkpt_check_status;
16055 ops->print_it = momentary_bkpt_print_it;
16056 ops->print_mention = momentary_bkpt_print_mention;
16057
e2e4d78b
JK
16058 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16059 ops = &longjmp_breakpoint_ops;
16060 *ops = momentary_breakpoint_ops;
16061 ops->dtor = longjmp_bkpt_dtor;
16062
55aa24fb
SDJ
16063 /* Probe breakpoints. */
16064 ops = &bkpt_probe_breakpoint_ops;
16065 *ops = bkpt_breakpoint_ops;
16066 ops->insert_location = bkpt_probe_insert_location;
16067 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
16068 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16069 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 16070
2060206e
PA
16071 /* Watchpoints. */
16072 ops = &watchpoint_breakpoint_ops;
16073 *ops = base_breakpoint_ops;
3a5c3e22 16074 ops->dtor = dtor_watchpoint;
2060206e
PA
16075 ops->re_set = re_set_watchpoint;
16076 ops->insert_location = insert_watchpoint;
16077 ops->remove_location = remove_watchpoint;
16078 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16079 ops->check_status = check_status_watchpoint;
16080 ops->resources_needed = resources_needed_watchpoint;
16081 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16082 ops->print_it = print_it_watchpoint;
16083 ops->print_mention = print_mention_watchpoint;
16084 ops->print_recreate = print_recreate_watchpoint;
427cd150 16085 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16086
16087 /* Masked watchpoints. */
16088 ops = &masked_watchpoint_breakpoint_ops;
16089 *ops = watchpoint_breakpoint_ops;
16090 ops->insert_location = insert_masked_watchpoint;
16091 ops->remove_location = remove_masked_watchpoint;
16092 ops->resources_needed = resources_needed_masked_watchpoint;
16093 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16094 ops->print_it = print_it_masked_watchpoint;
16095 ops->print_one_detail = print_one_detail_masked_watchpoint;
16096 ops->print_mention = print_mention_masked_watchpoint;
16097 ops->print_recreate = print_recreate_masked_watchpoint;
16098
16099 /* Tracepoints. */
16100 ops = &tracepoint_breakpoint_ops;
16101 *ops = base_breakpoint_ops;
16102 ops->re_set = tracepoint_re_set;
16103 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16104 ops->print_one_detail = tracepoint_print_one_detail;
16105 ops->print_mention = tracepoint_print_mention;
16106 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16107 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16108 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16109 ops->decode_location = tracepoint_decode_location;
983af33b 16110
55aa24fb
SDJ
16111 /* Probe tracepoints. */
16112 ops = &tracepoint_probe_breakpoint_ops;
16113 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16114 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16115 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16116
983af33b
SDJ
16117 /* Static tracepoints with marker (`-m'). */
16118 ops = &strace_marker_breakpoint_ops;
16119 *ops = tracepoint_breakpoint_ops;
5f700d83 16120 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16121 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16122 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16123
16124 /* Fork catchpoints. */
16125 ops = &catch_fork_breakpoint_ops;
16126 *ops = base_breakpoint_ops;
16127 ops->insert_location = insert_catch_fork;
16128 ops->remove_location = remove_catch_fork;
16129 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16130 ops->print_it = print_it_catch_fork;
16131 ops->print_one = print_one_catch_fork;
16132 ops->print_mention = print_mention_catch_fork;
16133 ops->print_recreate = print_recreate_catch_fork;
16134
16135 /* Vfork catchpoints. */
16136 ops = &catch_vfork_breakpoint_ops;
16137 *ops = base_breakpoint_ops;
16138 ops->insert_location = insert_catch_vfork;
16139 ops->remove_location = remove_catch_vfork;
16140 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16141 ops->print_it = print_it_catch_vfork;
16142 ops->print_one = print_one_catch_vfork;
16143 ops->print_mention = print_mention_catch_vfork;
16144 ops->print_recreate = print_recreate_catch_vfork;
16145
16146 /* Exec catchpoints. */
16147 ops = &catch_exec_breakpoint_ops;
16148 *ops = base_breakpoint_ops;
16149 ops->dtor = dtor_catch_exec;
16150 ops->insert_location = insert_catch_exec;
16151 ops->remove_location = remove_catch_exec;
16152 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16153 ops->print_it = print_it_catch_exec;
16154 ops->print_one = print_one_catch_exec;
16155 ops->print_mention = print_mention_catch_exec;
16156 ops->print_recreate = print_recreate_catch_exec;
16157
edcc5120
TT
16158 /* Solib-related catchpoints. */
16159 ops = &catch_solib_breakpoint_ops;
16160 *ops = base_breakpoint_ops;
16161 ops->dtor = dtor_catch_solib;
16162 ops->insert_location = insert_catch_solib;
16163 ops->remove_location = remove_catch_solib;
16164 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16165 ops->check_status = check_status_catch_solib;
16166 ops->print_it = print_it_catch_solib;
16167 ops->print_one = print_one_catch_solib;
16168 ops->print_mention = print_mention_catch_solib;
16169 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16170
16171 ops = &dprintf_breakpoint_ops;
16172 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16173 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16174 ops->resources_needed = bkpt_resources_needed;
16175 ops->print_it = bkpt_print_it;
16176 ops->print_mention = bkpt_print_mention;
2d9442cc 16177 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16178 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16179 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16180}
16181
8bfd80db
YQ
16182/* Chain containing all defined "enable breakpoint" subcommands. */
16183
16184static struct cmd_list_element *enablebreaklist = NULL;
16185
c906108c 16186void
fba45db2 16187_initialize_breakpoint (void)
c906108c
SS
16188{
16189 struct cmd_list_element *c;
16190
2060206e
PA
16191 initialize_breakpoint_ops ();
16192
84acb35a 16193 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16194 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16195 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16196
55aa24fb
SDJ
16197 breakpoint_objfile_key
16198 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16199
c906108c
SS
16200 breakpoint_chain = 0;
16201 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16202 before a breakpoint is set. */
16203 breakpoint_count = 0;
16204
1042e4c0
SS
16205 tracepoint_count = 0;
16206
1bedd215
AC
16207 add_com ("ignore", class_breakpoint, ignore_command, _("\
16208Set ignore-count of breakpoint number N to COUNT.\n\
16209Usage is `ignore N COUNT'."));
c906108c 16210
1bedd215
AC
16211 add_com ("commands", class_breakpoint, commands_command, _("\
16212Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16213Give breakpoint number as argument after \"commands\".\n\
16214With no argument, the targeted breakpoint is the last one set.\n\
16215The commands themselves follow starting on the next line.\n\
16216Type a line containing \"end\" to indicate the end of them.\n\
16217Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16218then no output is printed when it is hit, except what the commands print."));
c906108c 16219
d55637df 16220 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16221Specify breakpoint number N to break only if COND is true.\n\
c906108c 16222Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16223expression to be evaluated whenever breakpoint N is reached."));
d55637df 16224 set_cmd_completer (c, condition_completer);
c906108c 16225
1bedd215 16226 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16227Set a temporary breakpoint.\n\
c906108c
SS
16228Like \"break\" except the breakpoint is only temporary,\n\
16229so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16230by using \"enable delete\" on the breakpoint number.\n\
16231\n"
16232BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16233 set_cmd_completer (c, location_completer);
c94fdfd0 16234
1bedd215 16235 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16236Set a hardware assisted breakpoint.\n\
c906108c 16237Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16238some target hardware may not have this support.\n\
16239\n"
16240BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16241 set_cmd_completer (c, location_completer);
c906108c 16242
1bedd215 16243 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16244Set a temporary hardware assisted breakpoint.\n\
c906108c 16245Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16246so it will be deleted when hit.\n\
16247\n"
16248BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16249 set_cmd_completer (c, location_completer);
c906108c 16250
1bedd215
AC
16251 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16252Enable some breakpoints.\n\
c906108c
SS
16253Give breakpoint numbers (separated by spaces) as arguments.\n\
16254With no subcommand, breakpoints are enabled until you command otherwise.\n\
16255This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16256With a subcommand you can enable temporarily."),
c906108c 16257 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16258
16259 add_com_alias ("en", "enable", class_breakpoint, 1);
16260
84951ab5 16261 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16262Enable some breakpoints.\n\
c906108c
SS
16263Give breakpoint numbers (separated by spaces) as arguments.\n\
16264This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16265May be abbreviated to simply \"enable\".\n"),
c5aa993b 16266 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16267
1a966eab
AC
16268 add_cmd ("once", no_class, enable_once_command, _("\
16269Enable breakpoints for one hit. Give breakpoint numbers.\n\
16270If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16271 &enablebreaklist);
16272
1a966eab
AC
16273 add_cmd ("delete", no_class, enable_delete_command, _("\
16274Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16275If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16276 &enablebreaklist);
16277
816338b5
SS
16278 add_cmd ("count", no_class, enable_count_command, _("\
16279Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16280If a breakpoint is hit while enabled in this fashion,\n\
16281the count is decremented; when it reaches zero, the breakpoint is disabled."),
16282 &enablebreaklist);
16283
1a966eab
AC
16284 add_cmd ("delete", no_class, enable_delete_command, _("\
16285Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16286If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16287 &enablelist);
16288
1a966eab
AC
16289 add_cmd ("once", no_class, enable_once_command, _("\
16290Enable breakpoints for one hit. Give breakpoint numbers.\n\
16291If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16292 &enablelist);
16293
16294 add_cmd ("count", no_class, enable_count_command, _("\
16295Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16296If a breakpoint is hit while enabled in this fashion,\n\
16297the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16298 &enablelist);
16299
1bedd215
AC
16300 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16301Disable some breakpoints.\n\
c906108c
SS
16302Arguments are breakpoint numbers with spaces in between.\n\
16303To disable all breakpoints, give no argument.\n\
64b9b334 16304A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16305 &disablelist, "disable ", 1, &cmdlist);
16306 add_com_alias ("dis", "disable", class_breakpoint, 1);
16307 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16308
1a966eab
AC
16309 add_cmd ("breakpoints", class_alias, disable_command, _("\
16310Disable some breakpoints.\n\
c906108c
SS
16311Arguments are breakpoint numbers with spaces in between.\n\
16312To disable all breakpoints, give no argument.\n\
64b9b334 16313A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16314This command may be abbreviated \"disable\"."),
c906108c
SS
16315 &disablelist);
16316
1bedd215
AC
16317 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16318Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16319Arguments are breakpoint numbers with spaces in between.\n\
16320To delete all breakpoints, give no argument.\n\
16321\n\
16322Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16323The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16324 &deletelist, "delete ", 1, &cmdlist);
16325 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16326 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16327
1a966eab
AC
16328 add_cmd ("breakpoints", class_alias, delete_command, _("\
16329Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16330Arguments are breakpoint numbers with spaces in between.\n\
16331To delete all breakpoints, give no argument.\n\
1a966eab 16332This command may be abbreviated \"delete\"."),
c906108c
SS
16333 &deletelist);
16334
1bedd215 16335 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16336Clear breakpoint at specified location.\n\
16337Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16338\n\
16339With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16340is executing in.\n"
16341"\n" LOCATION_HELP_STRING "\n\
1bedd215 16342See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16343 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16344
1bedd215 16345 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16346Set breakpoint at specified location.\n"
31e2b00f 16347BREAK_ARGS_HELP ("break")));
5ba2abeb 16348 set_cmd_completer (c, location_completer);
c94fdfd0 16349
c906108c
SS
16350 add_com_alias ("b", "break", class_run, 1);
16351 add_com_alias ("br", "break", class_run, 1);
16352 add_com_alias ("bre", "break", class_run, 1);
16353 add_com_alias ("brea", "break", class_run, 1);
16354
c906108c
SS
16355 if (dbx_commands)
16356 {
1bedd215
AC
16357 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16358Break in function/address or break at a line in the current file."),
c5aa993b
JM
16359 &stoplist, "stop ", 1, &cmdlist);
16360 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16361 _("Break in function or address."), &stoplist);
c5aa993b 16362 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16363 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16364 add_com ("status", class_info, breakpoints_info, _("\
16365Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16366The \"Type\" column indicates one of:\n\
16367\tbreakpoint - normal breakpoint\n\
16368\twatchpoint - watchpoint\n\
16369The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16370the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16371breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16372address and file/line number respectively.\n\
16373\n\
16374Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16375are set to the address of the last breakpoint listed unless the command\n\
16376is prefixed with \"server \".\n\n\
c906108c 16377Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16378breakpoint set."));
c906108c
SS
16379 }
16380
1bedd215 16381 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16382Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16383The \"Type\" column indicates one of:\n\
16384\tbreakpoint - normal breakpoint\n\
16385\twatchpoint - watchpoint\n\
16386The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16387the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16388breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16389address and file/line number respectively.\n\
16390\n\
16391Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16392are set to the address of the last breakpoint listed unless the command\n\
16393is prefixed with \"server \".\n\n\
c906108c 16394Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16395breakpoint set."));
c906108c 16396
6b04bdb7
MS
16397 add_info_alias ("b", "breakpoints", 1);
16398
1a966eab
AC
16399 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16400Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16401The \"Type\" column indicates one of:\n\
16402\tbreakpoint - normal breakpoint\n\
16403\twatchpoint - watchpoint\n\
16404\tlongjmp - internal breakpoint used to step through longjmp()\n\
16405\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16406\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16407\tfinish - internal breakpoint used by the \"finish\" command\n\
16408The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16409the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16410breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16411address and file/line number respectively.\n\
16412\n\
16413Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16414are set to the address of the last breakpoint listed unless the command\n\
16415is prefixed with \"server \".\n\n\
c906108c 16416Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16417breakpoint set."),
c906108c
SS
16418 &maintenanceinfolist);
16419
44feb3ce
TT
16420 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16421Set catchpoints to catch events."),
16422 &catch_cmdlist, "catch ",
16423 0/*allow-unknown*/, &cmdlist);
16424
16425 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16426Set temporary catchpoints to catch events."),
16427 &tcatch_cmdlist, "tcatch ",
16428 0/*allow-unknown*/, &cmdlist);
16429
44feb3ce
TT
16430 add_catch_command ("fork", _("Catch calls to fork."),
16431 catch_fork_command_1,
a96d9b2e 16432 NULL,
44feb3ce
TT
16433 (void *) (uintptr_t) catch_fork_permanent,
16434 (void *) (uintptr_t) catch_fork_temporary);
16435 add_catch_command ("vfork", _("Catch calls to vfork."),
16436 catch_fork_command_1,
a96d9b2e 16437 NULL,
44feb3ce
TT
16438 (void *) (uintptr_t) catch_vfork_permanent,
16439 (void *) (uintptr_t) catch_vfork_temporary);
16440 add_catch_command ("exec", _("Catch calls to exec."),
16441 catch_exec_command_1,
a96d9b2e
SDJ
16442 NULL,
16443 CATCH_PERMANENT,
16444 CATCH_TEMPORARY);
edcc5120
TT
16445 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16446Usage: catch load [REGEX]\n\
16447If REGEX is given, only stop for libraries matching the regular expression."),
16448 catch_load_command_1,
16449 NULL,
16450 CATCH_PERMANENT,
16451 CATCH_TEMPORARY);
16452 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16453Usage: catch unload [REGEX]\n\
16454If REGEX is given, only stop for libraries matching the regular expression."),
16455 catch_unload_command_1,
16456 NULL,
16457 CATCH_PERMANENT,
16458 CATCH_TEMPORARY);
c5aa993b 16459
1bedd215
AC
16460 c = add_com ("watch", class_breakpoint, watch_command, _("\
16461Set a watchpoint for an expression.\n\
06a64a0b 16462Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16463A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16464an expression changes.\n\
16465If -l or -location is given, this evaluates EXPRESSION and watches\n\
16466the memory to which it refers."));
65d12d83 16467 set_cmd_completer (c, expression_completer);
c906108c 16468
1bedd215
AC
16469 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16470Set a read watchpoint for an expression.\n\
06a64a0b 16471Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16472A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16473an expression is read.\n\
16474If -l or -location is given, this evaluates EXPRESSION and watches\n\
16475the memory to which it refers."));
65d12d83 16476 set_cmd_completer (c, expression_completer);
c906108c 16477
1bedd215
AC
16478 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16479Set a watchpoint for an expression.\n\
06a64a0b 16480Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16481A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16482an expression is either read or written.\n\
16483If -l or -location is given, this evaluates EXPRESSION and watches\n\
16484the memory to which it refers."));
65d12d83 16485 set_cmd_completer (c, expression_completer);
c906108c 16486
d77f58be 16487 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16488Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16489
920d2a44
AC
16490 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16491 respond to changes - contrary to the description. */
85c07804
AC
16492 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16493 &can_use_hw_watchpoints, _("\
16494Set debugger's willingness to use watchpoint hardware."), _("\
16495Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16496If zero, gdb will not use hardware for new watchpoints, even if\n\
16497such is available. (However, any hardware watchpoints that were\n\
16498created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16499hardware.)"),
16500 NULL,
920d2a44 16501 show_can_use_hw_watchpoints,
85c07804 16502 &setlist, &showlist);
c906108c
SS
16503
16504 can_use_hw_watchpoints = 1;
fa8d40ab 16505
1042e4c0
SS
16506 /* Tracepoint manipulation commands. */
16507
16508 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16509Set a tracepoint at specified location.\n\
1042e4c0
SS
16510\n"
16511BREAK_ARGS_HELP ("trace") "\n\
16512Do \"help tracepoints\" for info on other tracepoint commands."));
16513 set_cmd_completer (c, location_completer);
16514
16515 add_com_alias ("tp", "trace", class_alias, 0);
16516 add_com_alias ("tr", "trace", class_alias, 1);
16517 add_com_alias ("tra", "trace", class_alias, 1);
16518 add_com_alias ("trac", "trace", class_alias, 1);
16519
7a697b8d 16520 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16521Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16522\n"
16523BREAK_ARGS_HELP ("ftrace") "\n\
16524Do \"help tracepoints\" for info on other tracepoint commands."));
16525 set_cmd_completer (c, location_completer);
16526
0fb4aa4b 16527 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16528Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16529\n\
16530strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16531LOCATION may be a linespec, explicit, or address location (described below) \n\
16532or -m MARKER_ID.\n\n\
16533If a marker id is specified, probe the marker with that name. With\n\
16534no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16535Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16536This collects arbitrary user data passed in the probe point call to the\n\
16537tracing library. You can inspect it when analyzing the trace buffer,\n\
16538by printing the $_sdata variable like any other convenience variable.\n\
16539\n\
16540CONDITION is a boolean expression.\n\
629500fa 16541\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16542Multiple tracepoints at one place are permitted, and useful if their\n\
16543conditions are different.\n\
0fb4aa4b
PA
16544\n\
16545Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16546Do \"help tracepoints\" for info on other tracepoint commands."));
16547 set_cmd_completer (c, location_completer);
16548
1042e4c0 16549 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16550Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16551Convenience variable \"$tpnum\" contains the number of the\n\
16552last tracepoint set."));
16553
16554 add_info_alias ("tp", "tracepoints", 1);
16555
16556 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16557Delete specified tracepoints.\n\
16558Arguments are tracepoint numbers, separated by spaces.\n\
16559No argument means delete all tracepoints."),
16560 &deletelist);
7e20dfcd 16561 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16562
16563 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16564Disable specified tracepoints.\n\
16565Arguments are tracepoint numbers, separated by spaces.\n\
16566No argument means disable all tracepoints."),
16567 &disablelist);
16568 deprecate_cmd (c, "disable");
16569
16570 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16571Enable specified tracepoints.\n\
16572Arguments are tracepoint numbers, separated by spaces.\n\
16573No argument means enable all tracepoints."),
16574 &enablelist);
16575 deprecate_cmd (c, "enable");
16576
16577 add_com ("passcount", class_trace, trace_pass_command, _("\
16578Set the passcount for a tracepoint.\n\
16579The trace will end when the tracepoint has been passed 'count' times.\n\
16580Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16581if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16582
6149aea9
PA
16583 add_prefix_cmd ("save", class_breakpoint, save_command,
16584 _("Save breakpoint definitions as a script."),
16585 &save_cmdlist, "save ",
16586 0/*allow-unknown*/, &cmdlist);
16587
16588 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16589Save current breakpoint definitions as a script.\n\
cce7e648 16590This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16591catchpoints, tracepoints). Use the 'source' command in another debug\n\
16592session to restore them."),
16593 &save_cmdlist);
16594 set_cmd_completer (c, filename_completer);
16595
16596 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16597Save current tracepoint definitions as a script.\n\
6149aea9
PA
16598Use the 'source' command in another debug session to restore them."),
16599 &save_cmdlist);
1042e4c0
SS
16600 set_cmd_completer (c, filename_completer);
16601
6149aea9
PA
16602 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16603 deprecate_cmd (c, "save tracepoints");
16604
1bedd215 16605 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16606Breakpoint specific settings\n\
16607Configure various breakpoint-specific variables such as\n\
1bedd215 16608pending breakpoint behavior"),
fa8d40ab
JJ
16609 &breakpoint_set_cmdlist, "set breakpoint ",
16610 0/*allow-unknown*/, &setlist);
1bedd215 16611 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16612Breakpoint specific settings\n\
16613Configure various breakpoint-specific variables such as\n\
1bedd215 16614pending breakpoint behavior"),
fa8d40ab
JJ
16615 &breakpoint_show_cmdlist, "show breakpoint ",
16616 0/*allow-unknown*/, &showlist);
16617
7915a72c
AC
16618 add_setshow_auto_boolean_cmd ("pending", no_class,
16619 &pending_break_support, _("\
16620Set debugger's behavior regarding pending breakpoints."), _("\
16621Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16622If on, an unrecognized breakpoint location will cause gdb to create a\n\
16623pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16624an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16625user-query to see if a pending breakpoint should be created."),
2c5b56ce 16626 NULL,
920d2a44 16627 show_pending_break_support,
6e1d7d6c
AC
16628 &breakpoint_set_cmdlist,
16629 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16630
16631 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16632
16633 add_setshow_boolean_cmd ("auto-hw", no_class,
16634 &automatic_hardware_breakpoints, _("\
16635Set automatic usage of hardware breakpoints."), _("\
16636Show automatic usage of hardware breakpoints."), _("\
16637If set, the debugger will automatically use hardware breakpoints for\n\
16638breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16639a warning will be emitted for such breakpoints."),
16640 NULL,
16641 show_automatic_hardware_breakpoints,
16642 &breakpoint_set_cmdlist,
16643 &breakpoint_show_cmdlist);
74960c60 16644
a25a5a45
PA
16645 add_setshow_boolean_cmd ("always-inserted", class_support,
16646 &always_inserted_mode, _("\
74960c60
VP
16647Set mode for inserting breakpoints."), _("\
16648Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16649When this mode is on, breakpoints are inserted immediately as soon as\n\
16650they're created, kept inserted even when execution stops, and removed\n\
16651only when the user deletes them. When this mode is off (the default),\n\
16652breakpoints are inserted only when execution continues, and removed\n\
16653when execution stops."),
72d0e2c5
YQ
16654 NULL,
16655 &show_always_inserted_mode,
16656 &breakpoint_set_cmdlist,
16657 &breakpoint_show_cmdlist);
f1310107 16658
b775012e
LM
16659 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16660 condition_evaluation_enums,
16661 &condition_evaluation_mode_1, _("\
16662Set mode of breakpoint condition evaluation."), _("\
16663Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16664When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16665evaluated on the host's side by GDB. When it is set to \"target\",\n\
16666breakpoint conditions will be downloaded to the target (if the target\n\
16667supports such feature) and conditions will be evaluated on the target's side.\n\
16668If this is set to \"auto\" (default), this will be automatically set to\n\
16669\"target\" if it supports condition evaluation, otherwise it will\n\
16670be set to \"gdb\""),
16671 &set_condition_evaluation_mode,
16672 &show_condition_evaluation_mode,
16673 &breakpoint_set_cmdlist,
16674 &breakpoint_show_cmdlist);
16675
f1310107
TJB
16676 add_com ("break-range", class_breakpoint, break_range_command, _("\
16677Set a breakpoint for an address range.\n\
16678break-range START-LOCATION, END-LOCATION\n\
16679where START-LOCATION and END-LOCATION can be one of the following:\n\
16680 LINENUM, for that line in the current file,\n\
16681 FILE:LINENUM, for that line in that file,\n\
16682 +OFFSET, for that number of lines after the current line\n\
16683 or the start of the range\n\
16684 FUNCTION, for the first line in that function,\n\
16685 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16686 *ADDRESS, for the instruction at that address.\n\
16687\n\
16688The breakpoint will stop execution of the inferior whenever it executes\n\
16689an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16690range (including START-LOCATION and END-LOCATION)."));
16691
e7e0cddf 16692 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16693Set a dynamic printf at specified location.\n\
e7e0cddf 16694dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16695location may be a linespec, explicit, or address location.\n"
16696"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16697 set_cmd_completer (c, location_completer);
16698
16699 add_setshow_enum_cmd ("dprintf-style", class_support,
16700 dprintf_style_enums, &dprintf_style, _("\
16701Set the style of usage for dynamic printf."), _("\
16702Show the style of usage for dynamic printf."), _("\
16703This setting chooses how GDB will do a dynamic printf.\n\
16704If the value is \"gdb\", then the printing is done by GDB to its own\n\
16705console, as with the \"printf\" command.\n\
16706If the value is \"call\", the print is done by calling a function in your\n\
16707program; by default printf(), but you can choose a different function or\n\
16708output stream by setting dprintf-function and dprintf-channel."),
16709 update_dprintf_commands, NULL,
16710 &setlist, &showlist);
16711
16712 dprintf_function = xstrdup ("printf");
16713 add_setshow_string_cmd ("dprintf-function", class_support,
16714 &dprintf_function, _("\
16715Set the function to use for dynamic printf"), _("\
16716Show the function to use for dynamic printf"), NULL,
16717 update_dprintf_commands, NULL,
16718 &setlist, &showlist);
16719
16720 dprintf_channel = xstrdup ("");
16721 add_setshow_string_cmd ("dprintf-channel", class_support,
16722 &dprintf_channel, _("\
16723Set the channel to use for dynamic printf"), _("\
16724Show the channel to use for dynamic printf"), NULL,
16725 update_dprintf_commands, NULL,
16726 &setlist, &showlist);
16727
d3ce09f5
SS
16728 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16729 &disconnected_dprintf, _("\
16730Set whether dprintf continues after GDB disconnects."), _("\
16731Show whether dprintf continues after GDB disconnects."), _("\
16732Use this to let dprintf commands continue to hit and produce output\n\
16733even if GDB disconnects or detaches from the target."),
16734 NULL,
16735 NULL,
16736 &setlist, &showlist);
16737
16738 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16739agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16740(target agent only) This is useful for formatted output in user-defined commands."));
16741
765dc015 16742 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16743
16744 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16745 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16746}
This page took 3.451674 seconds and 4 git commands to generate.