Really fail inserting software breakpoints on read-only regions
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 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"
60250e8b 54#include "exceptions.h"
765dc015 55#include "memattr.h"
f7f9143b 56#include "ada-lang.h"
d1aa2f50 57#include "top.h"
79a45b7d 58#include "valprint.h"
4efc6507 59#include "jit.h"
a96d9b2e 60#include "xml-syscall.h"
65d79d4b 61#include "parser-defs.h"
55aa24fb
SDJ
62#include "gdb_regex.h"
63#include "probe.h"
e9cafbcc 64#include "cli/cli-utils.h"
be34f849 65#include "continuations.h"
1bfeeb0f
JL
66#include "stack.h"
67#include "skip.h"
b775012e 68#include "ax-gdb.h"
e2e4d78b 69#include "dummy-frame.h"
c906108c 70
d3ce09f5
SS
71#include "format.h"
72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
104c1213 82
e7e8980f
YQ
83/* Enums for exception-handling support. */
84enum exception_event_kind
85{
86 EX_EVENT_THROW,
591f19e8 87 EX_EVENT_RETHROW,
e7e8980f
YQ
88 EX_EVENT_CATCH
89};
90
4a64f543 91/* Prototypes for local functions. */
c906108c 92
a14ed312 93static void enable_delete_command (char *, int);
c906108c 94
a14ed312 95static void enable_once_command (char *, int);
c906108c 96
816338b5
SS
97static void enable_count_command (char *, int);
98
a14ed312 99static void disable_command (char *, int);
c906108c 100
a14ed312 101static void enable_command (char *, int);
c906108c 102
95a42b64
TT
103static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
c906108c 106
a14ed312 107static void ignore_command (char *, int);
c906108c 108
4efb68b1 109static int breakpoint_re_set_one (void *);
c906108c 110
348d480f
PA
111static void breakpoint_re_set_default (struct breakpoint *);
112
983af33b
SDJ
113static void create_sals_from_address_default (char **,
114 struct linespec_result *,
115 enum bptype, char *,
116 char **);
117
118static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
e7e0cddf 120 char *, char *, enum bptype,
983af33b
SDJ
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
44f238bb 124 int, int, int, unsigned);
983af33b
SDJ
125
126static void decode_linespec_default (struct breakpoint *, char **,
127 struct symtabs_and_lines *);
128
a14ed312 129static void clear_command (char *, int);
c906108c 130
a14ed312 131static void catch_command (char *, int);
c906108c 132
a9634178 133static int can_use_hardware_watchpoint (struct value *);
c906108c 134
98deb0da 135static void break_command_1 (char *, int, int);
c906108c 136
a14ed312 137static void mention (struct breakpoint *);
c906108c 138
348d480f
PA
139static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
140 enum bptype,
c0a91b2b 141 const struct breakpoint_ops *);
3742cc8b
YQ
142static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
143 const struct symtab_and_line *);
144
4a64f543
MS
145/* This function is used in gdbtk sources and thus can not be made
146 static. */
63c252f8 147struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 148 struct symtab_and_line,
c0a91b2b
TT
149 enum bptype,
150 const struct breakpoint_ops *);
c906108c 151
06edf0c0
PA
152static struct breakpoint *
153 momentary_breakpoint_from_master (struct breakpoint *orig,
154 enum bptype type,
a1aa2221
LM
155 const struct breakpoint_ops *ops,
156 int loc_enabled);
06edf0c0 157
76897487
KB
158static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
a6d9a66e
UW
160static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
88f7da05 162 enum bptype bptype);
76897487 163
6c95b8df
PA
164static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
5af949e3 166 struct obj_section *, int);
c906108c 167
85d721b8
PA
168static int watchpoint_locations_match (struct bp_location *loc1,
169 struct bp_location *loc2);
170
f1310107
TJB
171static int breakpoint_location_address_match (struct bp_location *bl,
172 struct address_space *aspace,
173 CORE_ADDR addr);
174
a14ed312 175static void breakpoints_info (char *, int);
c906108c 176
d77f58be
SS
177static void watchpoints_info (char *, int);
178
e5a67952
MS
179static int breakpoint_1 (char *, int,
180 int (*) (const struct breakpoint *));
c906108c 181
4efb68b1 182static int breakpoint_cond_eval (void *);
c906108c 183
4efb68b1 184static void cleanup_executing_breakpoints (void *);
c906108c 185
a14ed312 186static void commands_command (char *, int);
c906108c 187
a14ed312 188static void condition_command (char *, int);
c906108c 189
c5aa993b
JM
190typedef enum
191 {
192 mark_inserted,
193 mark_uninserted
194 }
195insertion_state_t;
c906108c 196
0bde7532 197static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 198static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 199
e514a9d6 200static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 201
4efb68b1 202static int watchpoint_check (void *);
c906108c 203
a14ed312 204static void maintenance_info_breakpoints (char *, int);
c906108c 205
a14ed312 206static int hw_breakpoint_used_count (void);
c906108c 207
a1398e0c
PA
208static int hw_watchpoint_use_count (struct breakpoint *);
209
210static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
c906108c 213
a14ed312 214static void hbreak_command (char *, int);
c906108c 215
a14ed312 216static void thbreak_command (char *, int);
c906108c 217
816338b5
SS
218static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
c906108c 220
a14ed312 221static void stop_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopin_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopat_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void tcatch_command (char *arg, int from_tty);
7a292a7a 228
d03285ec
UW
229static void detach_single_step_breakpoints (void);
230
ef370185
JB
231static int find_single_step_breakpoint (struct address_space *aspace,
232 CORE_ADDR pc);
233
fe3f5fa8 234static void free_bp_location (struct bp_location *loc);
f431efe5
PA
235static void incref_bp_location (struct bp_location *loc);
236static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 237
39d61571 238static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 239
44702360
PA
240/* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242enum ugll_insert_mode
243{
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
a25a5a45
PA
261 /* May insert breakpoints iff breakpoints_should_be_inserted_now
262 claims breakpoints should be inserted now. */
04086b45
PA
263 UGLL_MAY_INSERT,
264
a25a5a45
PA
265 /* Insert locations now, irrespective of
266 breakpoints_should_be_inserted_now. E.g., say all threads are
267 stopped right now, and the user did "continue". We need to
268 insert breakpoints _before_ resuming the target, but
269 UGLL_MAY_INSERT wouldn't insert them, because
270 breakpoints_should_be_inserted_now returns false at that point,
271 as no thread is running yet. */
04086b45 272 UGLL_INSERT
44702360
PA
273};
274
275static void update_global_location_list (enum ugll_insert_mode);
a5606eee 276
44702360 277static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 278
d77f58be 279static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
280
281static void insert_breakpoint_locations (void);
a5606eee 282
a96d9b2e
SDJ
283static int syscall_catchpoint_p (struct breakpoint *b);
284
1042e4c0
SS
285static void tracepoints_info (char *, int);
286
287static void delete_trace_command (char *, int);
288
289static void enable_trace_command (char *, int);
290
291static void disable_trace_command (char *, int);
292
293static void trace_pass_command (char *, int);
294
558a9d82
YQ
295static void set_tracepoint_count (int num);
296
9c06b0b4
TJB
297static int is_masked_watchpoint (const struct breakpoint *b);
298
b775012e
LM
299static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
300
983af33b
SDJ
301/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
302 otherwise. */
303
304static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 305
2060206e
PA
306/* The abstract base class all breakpoint_ops structures inherit
307 from. */
ab04a2af 308struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
309
310/* The breakpoint_ops structure to be inherited by all breakpoint_ops
311 that are implemented on top of software or hardware breakpoints
312 (user breakpoints, internal and momentary breakpoints, etc.). */
313static struct breakpoint_ops bkpt_base_breakpoint_ops;
314
315/* Internal breakpoints class type. */
06edf0c0 316static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
317
318/* Momentary breakpoints class type. */
06edf0c0
PA
319static struct breakpoint_ops momentary_breakpoint_ops;
320
e2e4d78b
JK
321/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
322static struct breakpoint_ops longjmp_breakpoint_ops;
323
2060206e
PA
324/* The breakpoint_ops structure to be used in regular user created
325 breakpoints. */
326struct breakpoint_ops bkpt_breakpoint_ops;
327
55aa24fb
SDJ
328/* Breakpoints set on probes. */
329static struct breakpoint_ops bkpt_probe_breakpoint_ops;
330
e7e0cddf 331/* Dynamic printf class type. */
c5867ab6 332struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 333
9d497a19
PA
334/* One (or perhaps two) breakpoints used for software single
335 stepping. */
336
337static void *single_step_breakpoints[2];
338static struct gdbarch *single_step_gdbarch[2];
339
d3ce09f5
SS
340/* The style in which to perform a dynamic printf. This is a user
341 option because different output options have different tradeoffs;
342 if GDB does the printing, there is better error handling if there
343 is a problem with any of the arguments, but using an inferior
344 function lets you have special-purpose printers and sending of
345 output to the same place as compiled-in print functions. */
346
347static const char dprintf_style_gdb[] = "gdb";
348static const char dprintf_style_call[] = "call";
349static const char dprintf_style_agent[] = "agent";
350static const char *const dprintf_style_enums[] = {
351 dprintf_style_gdb,
352 dprintf_style_call,
353 dprintf_style_agent,
354 NULL
355};
356static const char *dprintf_style = dprintf_style_gdb;
357
358/* The function to use for dynamic printf if the preferred style is to
359 call into the inferior. The value is simply a string that is
360 copied into the command, so it can be anything that GDB can
361 evaluate to a callable address, not necessarily a function name. */
362
363static char *dprintf_function = "";
364
365/* The channel to use for dynamic printf if the preferred style is to
366 call into the inferior; if a nonempty string, it will be passed to
367 the call as the first argument, with the format string as the
368 second. As with the dprintf function, this can be anything that
369 GDB knows how to evaluate, so in addition to common choices like
370 "stderr", this could be an app-specific expression like
371 "mystreams[curlogger]". */
372
373static char *dprintf_channel = "";
374
375/* True if dprintf commands should continue to operate even if GDB
376 has disconnected. */
377static int disconnected_dprintf = 1;
378
5cea2a26
PA
379/* A reference-counted struct command_line. This lets multiple
380 breakpoints share a single command list. */
381struct counted_command_line
382{
383 /* The reference count. */
384 int refc;
385
386 /* The command list. */
387 struct command_line *commands;
388};
389
390struct command_line *
391breakpoint_commands (struct breakpoint *b)
392{
393 return b->commands ? b->commands->commands : NULL;
394}
3daf8fe5 395
f3b1572e
PA
396/* Flag indicating that a command has proceeded the inferior past the
397 current breakpoint. */
398
399static int breakpoint_proceeded;
400
956a9fb9 401const char *
2cec12e5
AR
402bpdisp_text (enum bpdisp disp)
403{
4a64f543
MS
404 /* NOTE: the following values are a part of MI protocol and
405 represent values of 'disp' field returned when inferior stops at
406 a breakpoint. */
bc043ef3 407 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 408
2cec12e5
AR
409 return bpdisps[(int) disp];
410}
c906108c 411
4a64f543 412/* Prototypes for exported functions. */
c906108c 413/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 414 if such is available. */
c906108c
SS
415static int can_use_hw_watchpoints;
416
920d2a44
AC
417static void
418show_can_use_hw_watchpoints (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 willingness to use "
424 "watchpoint hardware is %s.\n"),
920d2a44
AC
425 value);
426}
427
fa8d40ab
JJ
428/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
429 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 430 for unrecognized breakpoint locations.
fa8d40ab
JJ
431 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
432static enum auto_boolean pending_break_support;
920d2a44
AC
433static void
434show_pending_break_support (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 _("Debugger's behavior regarding "
440 "pending breakpoints is %s.\n"),
920d2a44
AC
441 value);
442}
fa8d40ab 443
765dc015 444/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 445 set with "break" but falling in read-only memory.
765dc015
VP
446 If 0, gdb will warn about such breakpoints, but won't automatically
447 use hardware breakpoints. */
448static int automatic_hardware_breakpoints;
449static void
450show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
451 struct cmd_list_element *c,
452 const char *value)
453{
3e43a32a
MS
454 fprintf_filtered (file,
455 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
456 value);
457}
458
a25a5a45
PA
459/* If on, GDB keeps breakpoints inserted even if the inferior is
460 stopped, and immediately inserts any new breakpoints as soon as
461 they're created. If off (default), GDB keeps breakpoints off of
462 the target as long as possible. That is, it delays inserting
463 breakpoints until the next resume, and removes them again when the
464 target fully stops. This is a bit safer in case GDB crashes while
465 processing user input. */
466static int always_inserted_mode = 0;
72d0e2c5 467
33e5cbd6 468static void
74960c60 469show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 470 struct cmd_list_element *c, const char *value)
74960c60 471{
a25a5a45
PA
472 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
473 value);
74960c60
VP
474}
475
33e5cbd6 476int
a25a5a45 477breakpoints_should_be_inserted_now (void)
33e5cbd6 478{
a25a5a45
PA
479 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
480 {
481 /* If breakpoints are global, they should be inserted even if no
482 thread under gdb's control is running, or even if there are
483 no threads under GDB's control yet. */
484 return 1;
485 }
486 else if (target_has_execution)
487 {
488 struct thread_info *tp;
489
490 if (always_inserted_mode)
491 {
492 /* The user wants breakpoints inserted even if all threads
493 are stopped. */
494 return 1;
495 }
496
497 ALL_NON_EXITED_THREADS (tp)
498 {
499 if (tp->executing)
500 return 1;
501 }
502 }
503 return 0;
33e5cbd6 504}
765dc015 505
b775012e
LM
506static const char condition_evaluation_both[] = "host or target";
507
508/* Modes for breakpoint condition evaluation. */
509static const char condition_evaluation_auto[] = "auto";
510static const char condition_evaluation_host[] = "host";
511static const char condition_evaluation_target[] = "target";
512static const char *const condition_evaluation_enums[] = {
513 condition_evaluation_auto,
514 condition_evaluation_host,
515 condition_evaluation_target,
516 NULL
517};
518
519/* Global that holds the current mode for breakpoint condition evaluation. */
520static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
521
522/* Global that we use to display information to the user (gets its value from
523 condition_evaluation_mode_1. */
524static const char *condition_evaluation_mode = condition_evaluation_auto;
525
526/* Translate a condition evaluation mode MODE into either "host"
527 or "target". This is used mostly to translate from "auto" to the
528 real setting that is being used. It returns the translated
529 evaluation mode. */
530
531static const char *
532translate_condition_evaluation_mode (const char *mode)
533{
534 if (mode == condition_evaluation_auto)
535 {
536 if (target_supports_evaluation_of_breakpoint_conditions ())
537 return condition_evaluation_target;
538 else
539 return condition_evaluation_host;
540 }
541 else
542 return mode;
543}
544
545/* Discovers what condition_evaluation_auto translates to. */
546
547static const char *
548breakpoint_condition_evaluation_mode (void)
549{
550 return translate_condition_evaluation_mode (condition_evaluation_mode);
551}
552
553/* Return true if GDB should evaluate breakpoint conditions or false
554 otherwise. */
555
556static int
557gdb_evaluates_breakpoint_condition_p (void)
558{
559 const char *mode = breakpoint_condition_evaluation_mode ();
560
561 return (mode == condition_evaluation_host);
562}
563
a14ed312 564void _initialize_breakpoint (void);
c906108c 565
c906108c
SS
566/* Are we executing breakpoint commands? */
567static int executing_breakpoint_commands;
568
c02f5703
MS
569/* Are overlay event breakpoints enabled? */
570static int overlay_events_enabled;
571
e09342b5
TJB
572/* See description in breakpoint.h. */
573int target_exact_watchpoints = 0;
574
c906108c 575/* Walk the following statement or block through all breakpoints.
e5dd4106 576 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 577 current breakpoint. */
c906108c 578
5c44784c 579#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 580
5c44784c
JM
581#define ALL_BREAKPOINTS_SAFE(B,TMP) \
582 for (B = breakpoint_chain; \
583 B ? (TMP=B->next, 1): 0; \
584 B = TMP)
c906108c 585
4a64f543
MS
586/* Similar iterator for the low-level breakpoints. SAFE variant is
587 not provided so update_global_location_list must not be called
588 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 589
876fa593
JK
590#define ALL_BP_LOCATIONS(B,BP_TMP) \
591 for (BP_TMP = bp_location; \
592 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
593 BP_TMP++)
7cc221ef 594
b775012e
LM
595/* Iterates through locations with address ADDRESS for the currently selected
596 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
597 to where the loop should start from.
598 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
599 appropriate location to start with. */
600
601#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
602 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
603 BP_LOCP_TMP = BP_LOCP_START; \
604 BP_LOCP_START \
605 && (BP_LOCP_TMP < bp_location + bp_location_count \
606 && (*BP_LOCP_TMP)->address == ADDRESS); \
607 BP_LOCP_TMP++)
608
1042e4c0
SS
609/* Iterator for tracepoints only. */
610
611#define ALL_TRACEPOINTS(B) \
612 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 613 if (is_tracepoint (B))
1042e4c0 614
7cc221ef 615/* Chains of all breakpoints defined. */
c906108c
SS
616
617struct breakpoint *breakpoint_chain;
618
876fa593
JK
619/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
620
621static struct bp_location **bp_location;
622
623/* Number of elements of BP_LOCATION. */
624
625static unsigned bp_location_count;
626
4a64f543
MS
627/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
628 ADDRESS for the current elements of BP_LOCATION which get a valid
629 result from bp_location_has_shadow. You can use it for roughly
630 limiting the subrange of BP_LOCATION to scan for shadow bytes for
631 an address you need to read. */
876fa593
JK
632
633static CORE_ADDR bp_location_placed_address_before_address_max;
634
4a64f543
MS
635/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
636 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
637 BP_LOCATION which get a valid result from bp_location_has_shadow.
638 You can use it for roughly limiting the subrange of BP_LOCATION to
639 scan for shadow bytes for an address you need to read. */
876fa593
JK
640
641static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 642
4a64f543
MS
643/* The locations that no longer correspond to any breakpoint, unlinked
644 from bp_location array, but for which a hit may still be reported
645 by a target. */
20874c92
VP
646VEC(bp_location_p) *moribund_locations = NULL;
647
c906108c
SS
648/* Number of last breakpoint made. */
649
95a42b64
TT
650static int breakpoint_count;
651
86b17b60
PA
652/* The value of `breakpoint_count' before the last command that
653 created breakpoints. If the last (break-like) command created more
654 than one breakpoint, then the difference between BREAKPOINT_COUNT
655 and PREV_BREAKPOINT_COUNT is more than one. */
656static int prev_breakpoint_count;
c906108c 657
1042e4c0
SS
658/* Number of last tracepoint made. */
659
95a42b64 660static int tracepoint_count;
1042e4c0 661
6149aea9
PA
662static struct cmd_list_element *breakpoint_set_cmdlist;
663static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 664struct cmd_list_element *save_cmdlist;
6149aea9 665
468d015d
JJ
666/* Return whether a breakpoint is an active enabled breakpoint. */
667static int
668breakpoint_enabled (struct breakpoint *b)
669{
0d381245 670 return (b->enable_state == bp_enabled);
468d015d
JJ
671}
672
c906108c
SS
673/* Set breakpoint count to NUM. */
674
95a42b64 675static void
fba45db2 676set_breakpoint_count (int num)
c906108c 677{
86b17b60 678 prev_breakpoint_count = breakpoint_count;
c906108c 679 breakpoint_count = num;
4fa62494 680 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
681}
682
86b17b60
PA
683/* Used by `start_rbreak_breakpoints' below, to record the current
684 breakpoint count before "rbreak" creates any breakpoint. */
685static int rbreak_start_breakpoint_count;
686
95a42b64
TT
687/* Called at the start an "rbreak" command to record the first
688 breakpoint made. */
86b17b60 689
95a42b64
TT
690void
691start_rbreak_breakpoints (void)
692{
86b17b60 693 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
694}
695
696/* Called at the end of an "rbreak" command to record the last
697 breakpoint made. */
86b17b60 698
95a42b64
TT
699void
700end_rbreak_breakpoints (void)
701{
86b17b60 702 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
703}
704
4a64f543 705/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
706
707void
fba45db2 708clear_breakpoint_hit_counts (void)
c906108c
SS
709{
710 struct breakpoint *b;
711
712 ALL_BREAKPOINTS (b)
713 b->hit_count = 0;
714}
715
9add0f1b
TT
716/* Allocate a new counted_command_line with reference count of 1.
717 The new structure owns COMMANDS. */
718
719static struct counted_command_line *
720alloc_counted_command_line (struct command_line *commands)
721{
722 struct counted_command_line *result
723 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 724
9add0f1b
TT
725 result->refc = 1;
726 result->commands = commands;
727 return result;
728}
729
730/* Increment reference count. This does nothing if CMD is NULL. */
731
732static void
733incref_counted_command_line (struct counted_command_line *cmd)
734{
735 if (cmd)
736 ++cmd->refc;
737}
738
739/* Decrement reference count. If the reference count reaches 0,
740 destroy the counted_command_line. Sets *CMDP to NULL. This does
741 nothing if *CMDP is NULL. */
742
743static void
744decref_counted_command_line (struct counted_command_line **cmdp)
745{
746 if (*cmdp)
747 {
748 if (--(*cmdp)->refc == 0)
749 {
750 free_command_lines (&(*cmdp)->commands);
751 xfree (*cmdp);
752 }
753 *cmdp = NULL;
754 }
755}
756
757/* A cleanup function that calls decref_counted_command_line. */
758
759static void
760do_cleanup_counted_command_line (void *arg)
761{
762 decref_counted_command_line (arg);
763}
764
765/* Create a cleanup that calls decref_counted_command_line on the
766 argument. */
767
768static struct cleanup *
769make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
770{
771 return make_cleanup (do_cleanup_counted_command_line, cmdp);
772}
773
c906108c 774\f
48cb2d85
VP
775/* Return the breakpoint with the specified number, or NULL
776 if the number does not refer to an existing breakpoint. */
777
778struct breakpoint *
779get_breakpoint (int num)
780{
781 struct breakpoint *b;
782
783 ALL_BREAKPOINTS (b)
784 if (b->number == num)
785 return b;
786
787 return NULL;
788}
5c44784c 789
c906108c 790\f
adc36818 791
b775012e
LM
792/* Mark locations as "conditions have changed" in case the target supports
793 evaluating conditions on its side. */
794
795static void
796mark_breakpoint_modified (struct breakpoint *b)
797{
798 struct bp_location *loc;
799
800 /* This is only meaningful if the target is
801 evaluating conditions and if the user has
802 opted for condition evaluation on the target's
803 side. */
804 if (gdb_evaluates_breakpoint_condition_p ()
805 || !target_supports_evaluation_of_breakpoint_conditions ())
806 return;
807
808 if (!is_breakpoint (b))
809 return;
810
811 for (loc = b->loc; loc; loc = loc->next)
812 loc->condition_changed = condition_modified;
813}
814
815/* Mark location as "conditions have changed" in case the target supports
816 evaluating conditions on its side. */
817
818static void
819mark_breakpoint_location_modified (struct bp_location *loc)
820{
821 /* This is only meaningful if the target is
822 evaluating conditions and if the user has
823 opted for condition evaluation on the target's
824 side. */
825 if (gdb_evaluates_breakpoint_condition_p ()
826 || !target_supports_evaluation_of_breakpoint_conditions ())
827
828 return;
829
830 if (!is_breakpoint (loc->owner))
831 return;
832
833 loc->condition_changed = condition_modified;
834}
835
836/* Sets the condition-evaluation mode using the static global
837 condition_evaluation_mode. */
838
839static void
840set_condition_evaluation_mode (char *args, int from_tty,
841 struct cmd_list_element *c)
842{
b775012e
LM
843 const char *old_mode, *new_mode;
844
845 if ((condition_evaluation_mode_1 == condition_evaluation_target)
846 && !target_supports_evaluation_of_breakpoint_conditions ())
847 {
848 condition_evaluation_mode_1 = condition_evaluation_mode;
849 warning (_("Target does not support breakpoint condition evaluation.\n"
850 "Using host evaluation mode instead."));
851 return;
852 }
853
854 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
855 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
856
abf1152a
JK
857 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
858 settings was "auto". */
859 condition_evaluation_mode = condition_evaluation_mode_1;
860
b775012e
LM
861 /* Only update the mode if the user picked a different one. */
862 if (new_mode != old_mode)
863 {
864 struct bp_location *loc, **loc_tmp;
865 /* If the user switched to a different evaluation mode, we
866 need to synch the changes with the target as follows:
867
868 "host" -> "target": Send all (valid) conditions to the target.
869 "target" -> "host": Remove all the conditions from the target.
870 */
871
b775012e
LM
872 if (new_mode == condition_evaluation_target)
873 {
874 /* Mark everything modified and synch conditions with the
875 target. */
876 ALL_BP_LOCATIONS (loc, loc_tmp)
877 mark_breakpoint_location_modified (loc);
878 }
879 else
880 {
881 /* Manually mark non-duplicate locations to synch conditions
882 with the target. We do this to remove all the conditions the
883 target knows about. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 if (is_breakpoint (loc->owner) && loc->inserted)
886 loc->needs_update = 1;
887 }
888
889 /* Do the update. */
44702360 890 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
891 }
892
893 return;
894}
895
896/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
897 what "auto" is translating to. */
898
899static void
900show_condition_evaluation_mode (struct ui_file *file, int from_tty,
901 struct cmd_list_element *c, const char *value)
902{
903 if (condition_evaluation_mode == condition_evaluation_auto)
904 fprintf_filtered (file,
905 _("Breakpoint condition evaluation "
906 "mode is %s (currently %s).\n"),
907 value,
908 breakpoint_condition_evaluation_mode ());
909 else
910 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
911 value);
912}
913
914/* A comparison function for bp_location AP and BP that is used by
915 bsearch. This comparison function only cares about addresses, unlike
916 the more general bp_location_compare function. */
917
918static int
919bp_location_compare_addrs (const void *ap, const void *bp)
920{
921 struct bp_location *a = *(void **) ap;
922 struct bp_location *b = *(void **) bp;
923
924 if (a->address == b->address)
925 return 0;
926 else
927 return ((a->address > b->address) - (a->address < b->address));
928}
929
930/* Helper function to skip all bp_locations with addresses
931 less than ADDRESS. It returns the first bp_location that
932 is greater than or equal to ADDRESS. If none is found, just
933 return NULL. */
934
935static struct bp_location **
936get_first_locp_gte_addr (CORE_ADDR address)
937{
938 struct bp_location dummy_loc;
939 struct bp_location *dummy_locp = &dummy_loc;
940 struct bp_location **locp_found = NULL;
941
942 /* Initialize the dummy location's address field. */
943 memset (&dummy_loc, 0, sizeof (struct bp_location));
944 dummy_loc.address = address;
945
946 /* Find a close match to the first location at ADDRESS. */
947 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
948 sizeof (struct bp_location **),
949 bp_location_compare_addrs);
950
951 /* Nothing was found, nothing left to do. */
952 if (locp_found == NULL)
953 return NULL;
954
955 /* We may have found a location that is at ADDRESS but is not the first in the
956 location's list. Go backwards (if possible) and locate the first one. */
957 while ((locp_found - 1) >= bp_location
958 && (*(locp_found - 1))->address == address)
959 locp_found--;
960
961 return locp_found;
962}
963
adc36818
PM
964void
965set_breakpoint_condition (struct breakpoint *b, char *exp,
966 int from_tty)
967{
3a5c3e22
PA
968 xfree (b->cond_string);
969 b->cond_string = NULL;
adc36818 970
3a5c3e22 971 if (is_watchpoint (b))
adc36818 972 {
3a5c3e22
PA
973 struct watchpoint *w = (struct watchpoint *) b;
974
975 xfree (w->cond_exp);
976 w->cond_exp = NULL;
977 }
978 else
979 {
980 struct bp_location *loc;
981
982 for (loc = b->loc; loc; loc = loc->next)
983 {
984 xfree (loc->cond);
985 loc->cond = NULL;
b775012e
LM
986
987 /* No need to free the condition agent expression
988 bytecode (if we have one). We will handle this
989 when we go through update_global_location_list. */
3a5c3e22 990 }
adc36818 991 }
adc36818
PM
992
993 if (*exp == 0)
994 {
995 if (from_tty)
996 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
997 }
998 else
999 {
bbc13ae3 1000 const char *arg = exp;
cc59ec59 1001
adc36818
PM
1002 /* I don't know if it matters whether this is the string the user
1003 typed in or the decompiled expression. */
1004 b->cond_string = xstrdup (arg);
1005 b->condition_not_parsed = 0;
1006
1007 if (is_watchpoint (b))
1008 {
3a5c3e22
PA
1009 struct watchpoint *w = (struct watchpoint *) b;
1010
adc36818
PM
1011 innermost_block = NULL;
1012 arg = exp;
1bb9788d 1013 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1014 if (*arg)
1015 error (_("Junk at end of expression"));
3a5c3e22 1016 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1017 }
1018 else
1019 {
3a5c3e22
PA
1020 struct bp_location *loc;
1021
adc36818
PM
1022 for (loc = b->loc; loc; loc = loc->next)
1023 {
1024 arg = exp;
1025 loc->cond =
1bb9788d
TT
1026 parse_exp_1 (&arg, loc->address,
1027 block_for_pc (loc->address), 0);
adc36818
PM
1028 if (*arg)
1029 error (_("Junk at end of expression"));
1030 }
1031 }
1032 }
b775012e
LM
1033 mark_breakpoint_modified (b);
1034
8d3788bd 1035 observer_notify_breakpoint_modified (b);
adc36818
PM
1036}
1037
d55637df
TT
1038/* Completion for the "condition" command. */
1039
1040static VEC (char_ptr) *
6f937416
PA
1041condition_completer (struct cmd_list_element *cmd,
1042 const char *text, const char *word)
d55637df 1043{
6f937416 1044 const char *space;
d55637df 1045
6f937416
PA
1046 text = skip_spaces_const (text);
1047 space = skip_to_space_const (text);
d55637df
TT
1048 if (*space == '\0')
1049 {
1050 int len;
1051 struct breakpoint *b;
1052 VEC (char_ptr) *result = NULL;
1053
1054 if (text[0] == '$')
1055 {
1056 /* We don't support completion of history indices. */
1057 if (isdigit (text[1]))
1058 return NULL;
1059 return complete_internalvar (&text[1]);
1060 }
1061
1062 /* We're completing the breakpoint number. */
1063 len = strlen (text);
1064
1065 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1066 {
1067 char number[50];
1068
1069 xsnprintf (number, sizeof (number), "%d", b->number);
1070
1071 if (strncmp (number, text, len) == 0)
1072 VEC_safe_push (char_ptr, result, xstrdup (number));
1073 }
d55637df
TT
1074
1075 return result;
1076 }
1077
1078 /* We're completing the expression part. */
6f937416 1079 text = skip_spaces_const (space);
d55637df
TT
1080 return expression_completer (cmd, text, word);
1081}
1082
c906108c
SS
1083/* condition N EXP -- set break condition of breakpoint N to EXP. */
1084
1085static void
fba45db2 1086condition_command (char *arg, int from_tty)
c906108c 1087{
52f0bd74 1088 struct breakpoint *b;
c906108c 1089 char *p;
52f0bd74 1090 int bnum;
c906108c
SS
1091
1092 if (arg == 0)
e2e0b3e5 1093 error_no_arg (_("breakpoint number"));
c906108c
SS
1094
1095 p = arg;
1096 bnum = get_number (&p);
5c44784c 1097 if (bnum == 0)
8a3fe4f8 1098 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1099
1100 ALL_BREAKPOINTS (b)
1101 if (b->number == bnum)
2f069f6f 1102 {
6dddc817
DE
1103 /* Check if this breakpoint has a "stop" method implemented in an
1104 extension language. This method and conditions entered into GDB
1105 from the CLI are mutually exclusive. */
1106 const struct extension_language_defn *extlang
1107 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1108
1109 if (extlang != NULL)
1110 {
1111 error (_("Only one stop condition allowed. There is currently"
1112 " a %s stop condition defined for this breakpoint."),
1113 ext_lang_capitalized_name (extlang));
1114 }
2566ad2d 1115 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1116
1117 if (is_breakpoint (b))
44702360 1118 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1119
2f069f6f
JB
1120 return;
1121 }
c906108c 1122
8a3fe4f8 1123 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1124}
1125
a7bdde9e
VP
1126/* Check that COMMAND do not contain commands that are suitable
1127 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1128 Throw if any such commands is found. */
1129
a7bdde9e
VP
1130static void
1131check_no_tracepoint_commands (struct command_line *commands)
1132{
1133 struct command_line *c;
cc59ec59 1134
a7bdde9e
VP
1135 for (c = commands; c; c = c->next)
1136 {
1137 int i;
1138
1139 if (c->control_type == while_stepping_control)
3e43a32a
MS
1140 error (_("The 'while-stepping' command can "
1141 "only be used for tracepoints"));
a7bdde9e
VP
1142
1143 for (i = 0; i < c->body_count; ++i)
1144 check_no_tracepoint_commands ((c->body_list)[i]);
1145
1146 /* Not that command parsing removes leading whitespace and comment
4a64f543 1147 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1148 command directly. */
1149 if (strstr (c->line, "collect ") == c->line)
1150 error (_("The 'collect' command can only be used for tracepoints"));
1151
51661e93
VP
1152 if (strstr (c->line, "teval ") == c->line)
1153 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1154 }
1155}
1156
d77f58be
SS
1157/* Encapsulate tests for different types of tracepoints. */
1158
d9b3f62e
PA
1159static int
1160is_tracepoint_type (enum bptype type)
1161{
1162 return (type == bp_tracepoint
1163 || type == bp_fast_tracepoint
1164 || type == bp_static_tracepoint);
1165}
1166
a7bdde9e 1167int
d77f58be 1168is_tracepoint (const struct breakpoint *b)
a7bdde9e 1169{
d9b3f62e 1170 return is_tracepoint_type (b->type);
a7bdde9e 1171}
d9b3f62e 1172
e5dd4106 1173/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1174 breakpoint. This function will throw an exception if a problem is
1175 found. */
48cb2d85 1176
95a42b64
TT
1177static void
1178validate_commands_for_breakpoint (struct breakpoint *b,
1179 struct command_line *commands)
48cb2d85 1180{
d77f58be 1181 if (is_tracepoint (b))
a7bdde9e 1182 {
c9a6ce02 1183 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1184 struct command_line *c;
1185 struct command_line *while_stepping = 0;
c9a6ce02
PA
1186
1187 /* Reset the while-stepping step count. The previous commands
1188 might have included a while-stepping action, while the new
1189 ones might not. */
1190 t->step_count = 0;
1191
1192 /* We need to verify that each top-level element of commands is
1193 valid for tracepoints, that there's at most one
1194 while-stepping element, and that the while-stepping's body
1195 has valid tracing commands excluding nested while-stepping.
1196 We also need to validate the tracepoint action line in the
1197 context of the tracepoint --- validate_actionline actually
1198 has side effects, like setting the tracepoint's
1199 while-stepping STEP_COUNT, in addition to checking if the
1200 collect/teval actions parse and make sense in the
1201 tracepoint's context. */
a7bdde9e
VP
1202 for (c = commands; c; c = c->next)
1203 {
a7bdde9e
VP
1204 if (c->control_type == while_stepping_control)
1205 {
1206 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1207 error (_("The 'while-stepping' command "
1208 "cannot be used for fast tracepoint"));
0fb4aa4b 1209 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1210 error (_("The 'while-stepping' command "
1211 "cannot be used for static tracepoint"));
a7bdde9e
VP
1212
1213 if (while_stepping)
3e43a32a
MS
1214 error (_("The 'while-stepping' command "
1215 "can be used only once"));
a7bdde9e
VP
1216 else
1217 while_stepping = c;
1218 }
c9a6ce02
PA
1219
1220 validate_actionline (c->line, b);
a7bdde9e
VP
1221 }
1222 if (while_stepping)
1223 {
1224 struct command_line *c2;
1225
1226 gdb_assert (while_stepping->body_count == 1);
1227 c2 = while_stepping->body_list[0];
1228 for (; c2; c2 = c2->next)
1229 {
a7bdde9e
VP
1230 if (c2->control_type == while_stepping_control)
1231 error (_("The 'while-stepping' command cannot be nested"));
1232 }
1233 }
1234 }
1235 else
1236 {
1237 check_no_tracepoint_commands (commands);
1238 }
95a42b64
TT
1239}
1240
0fb4aa4b
PA
1241/* Return a vector of all the static tracepoints set at ADDR. The
1242 caller is responsible for releasing the vector. */
1243
1244VEC(breakpoint_p) *
1245static_tracepoints_here (CORE_ADDR addr)
1246{
1247 struct breakpoint *b;
1248 VEC(breakpoint_p) *found = 0;
1249 struct bp_location *loc;
1250
1251 ALL_BREAKPOINTS (b)
1252 if (b->type == bp_static_tracepoint)
1253 {
1254 for (loc = b->loc; loc; loc = loc->next)
1255 if (loc->address == addr)
1256 VEC_safe_push(breakpoint_p, found, b);
1257 }
1258
1259 return found;
1260}
1261
95a42b64 1262/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1263 validate that only allowed commands are included. */
95a42b64
TT
1264
1265void
4a64f543
MS
1266breakpoint_set_commands (struct breakpoint *b,
1267 struct command_line *commands)
95a42b64
TT
1268{
1269 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1270
9add0f1b
TT
1271 decref_counted_command_line (&b->commands);
1272 b->commands = alloc_counted_command_line (commands);
8d3788bd 1273 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1274}
1275
45a43567
TT
1276/* Set the internal `silent' flag on the breakpoint. Note that this
1277 is not the same as the "silent" that may appear in the breakpoint's
1278 commands. */
1279
1280void
1281breakpoint_set_silent (struct breakpoint *b, int silent)
1282{
1283 int old_silent = b->silent;
1284
1285 b->silent = silent;
1286 if (old_silent != silent)
8d3788bd 1287 observer_notify_breakpoint_modified (b);
45a43567
TT
1288}
1289
1290/* Set the thread for this breakpoint. If THREAD is -1, make the
1291 breakpoint work for any thread. */
1292
1293void
1294breakpoint_set_thread (struct breakpoint *b, int thread)
1295{
1296 int old_thread = b->thread;
1297
1298 b->thread = thread;
1299 if (old_thread != thread)
8d3788bd 1300 observer_notify_breakpoint_modified (b);
45a43567
TT
1301}
1302
1303/* Set the task for this breakpoint. If TASK is 0, make the
1304 breakpoint work for any task. */
1305
1306void
1307breakpoint_set_task (struct breakpoint *b, int task)
1308{
1309 int old_task = b->task;
1310
1311 b->task = task;
1312 if (old_task != task)
8d3788bd 1313 observer_notify_breakpoint_modified (b);
45a43567
TT
1314}
1315
95a42b64
TT
1316void
1317check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1318{
1319 struct breakpoint *b = closure;
cc59ec59 1320
6f937416 1321 validate_actionline (line, b);
a7bdde9e
VP
1322}
1323
95a42b64
TT
1324/* A structure used to pass information through
1325 map_breakpoint_numbers. */
1326
1327struct commands_info
1328{
1329 /* True if the command was typed at a tty. */
1330 int from_tty;
86b17b60
PA
1331
1332 /* The breakpoint range spec. */
1333 char *arg;
1334
95a42b64
TT
1335 /* Non-NULL if the body of the commands are being read from this
1336 already-parsed command. */
1337 struct command_line *control;
86b17b60 1338
95a42b64
TT
1339 /* The command lines read from the user, or NULL if they have not
1340 yet been read. */
1341 struct counted_command_line *cmd;
1342};
1343
1344/* A callback for map_breakpoint_numbers that sets the commands for
1345 commands_command. */
1346
c906108c 1347static void
95a42b64 1348do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1349{
95a42b64 1350 struct commands_info *info = data;
c906108c 1351
95a42b64
TT
1352 if (info->cmd == NULL)
1353 {
1354 struct command_line *l;
5c44784c 1355
95a42b64
TT
1356 if (info->control != NULL)
1357 l = copy_command_lines (info->control->body_list[0]);
1358 else
86b17b60
PA
1359 {
1360 struct cleanup *old_chain;
1361 char *str;
c5aa993b 1362
3e43a32a
MS
1363 str = xstrprintf (_("Type commands for breakpoint(s) "
1364 "%s, one per line."),
86b17b60
PA
1365 info->arg);
1366
1367 old_chain = make_cleanup (xfree, str);
1368
1369 l = read_command_lines (str,
1370 info->from_tty, 1,
d77f58be 1371 (is_tracepoint (b)
86b17b60
PA
1372 ? check_tracepoint_command : 0),
1373 b);
1374
1375 do_cleanups (old_chain);
1376 }
a7bdde9e 1377
95a42b64
TT
1378 info->cmd = alloc_counted_command_line (l);
1379 }
1380
1381 /* If a breakpoint was on the list more than once, we don't need to
1382 do anything. */
1383 if (b->commands != info->cmd)
1384 {
1385 validate_commands_for_breakpoint (b, info->cmd->commands);
1386 incref_counted_command_line (info->cmd);
1387 decref_counted_command_line (&b->commands);
1388 b->commands = info->cmd;
8d3788bd 1389 observer_notify_breakpoint_modified (b);
c5aa993b 1390 }
95a42b64
TT
1391}
1392
1393static void
4a64f543
MS
1394commands_command_1 (char *arg, int from_tty,
1395 struct command_line *control)
95a42b64
TT
1396{
1397 struct cleanup *cleanups;
1398 struct commands_info info;
1399
1400 info.from_tty = from_tty;
1401 info.control = control;
1402 info.cmd = NULL;
1403 /* If we read command lines from the user, then `info' will hold an
1404 extra reference to the commands that we must clean up. */
1405 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1406
1407 if (arg == NULL || !*arg)
1408 {
86b17b60 1409 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1410 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1411 breakpoint_count);
95a42b64
TT
1412 else if (breakpoint_count > 0)
1413 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1414 else
1415 {
1416 /* So that we don't try to free the incoming non-NULL
1417 argument in the cleanup below. Mapping breakpoint
1418 numbers will fail in this case. */
1419 arg = NULL;
1420 }
95a42b64 1421 }
9766ced4
SS
1422 else
1423 /* The command loop has some static state, so we need to preserve
1424 our argument. */
1425 arg = xstrdup (arg);
86b17b60
PA
1426
1427 if (arg != NULL)
1428 make_cleanup (xfree, arg);
1429
1430 info.arg = arg;
95a42b64
TT
1431
1432 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1433
1434 if (info.cmd == NULL)
1435 error (_("No breakpoints specified."));
1436
1437 do_cleanups (cleanups);
1438}
1439
1440static void
1441commands_command (char *arg, int from_tty)
1442{
1443 commands_command_1 (arg, from_tty, NULL);
c906108c 1444}
40c03ae8
EZ
1445
1446/* Like commands_command, but instead of reading the commands from
1447 input stream, takes them from an already parsed command structure.
1448
1449 This is used by cli-script.c to DTRT with breakpoint commands
1450 that are part of if and while bodies. */
1451enum command_control_type
1452commands_from_control_command (char *arg, struct command_line *cmd)
1453{
95a42b64
TT
1454 commands_command_1 (arg, 0, cmd);
1455 return simple_control;
40c03ae8 1456}
876fa593
JK
1457
1458/* Return non-zero if BL->TARGET_INFO contains valid information. */
1459
1460static int
1461bp_location_has_shadow (struct bp_location *bl)
1462{
1463 if (bl->loc_type != bp_loc_software_breakpoint)
1464 return 0;
1465 if (!bl->inserted)
1466 return 0;
1467 if (bl->target_info.shadow_len == 0)
e5dd4106 1468 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1469 return 0;
1470 return 1;
1471}
1472
9d497a19
PA
1473/* Update BUF, which is LEN bytes read from the target address
1474 MEMADDR, by replacing a memory breakpoint with its shadowed
1475 contents.
1476
1477 If READBUF is not NULL, this buffer must not overlap with the of
1478 the breakpoint location's shadow_contents buffer. Otherwise, a
1479 failed assertion internal error will be raised. */
1480
1481static void
1482one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1483 const gdb_byte *writebuf_org,
1484 ULONGEST memaddr, LONGEST len,
1485 struct bp_target_info *target_info,
1486 struct gdbarch *gdbarch)
1487{
1488 /* Now do full processing of the found relevant range of elements. */
1489 CORE_ADDR bp_addr = 0;
1490 int bp_size = 0;
1491 int bptoffset = 0;
1492
1493 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1494 current_program_space->aspace, 0))
1495 {
1496 /* The breakpoint is inserted in a different address space. */
1497 return;
1498 }
1499
1500 /* Addresses and length of the part of the breakpoint that
1501 we need to copy. */
1502 bp_addr = target_info->placed_address;
1503 bp_size = target_info->shadow_len;
1504
1505 if (bp_addr + bp_size <= memaddr)
1506 {
1507 /* The breakpoint is entirely before the chunk of memory we are
1508 reading. */
1509 return;
1510 }
1511
1512 if (bp_addr >= memaddr + len)
1513 {
1514 /* The breakpoint is entirely after the chunk of memory we are
1515 reading. */
1516 return;
1517 }
1518
1519 /* Offset within shadow_contents. */
1520 if (bp_addr < memaddr)
1521 {
1522 /* Only copy the second part of the breakpoint. */
1523 bp_size -= memaddr - bp_addr;
1524 bptoffset = memaddr - bp_addr;
1525 bp_addr = memaddr;
1526 }
1527
1528 if (bp_addr + bp_size > memaddr + len)
1529 {
1530 /* Only copy the first part of the breakpoint. */
1531 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1532 }
1533
1534 if (readbuf != NULL)
1535 {
1536 /* Verify that the readbuf buffer does not overlap with the
1537 shadow_contents buffer. */
1538 gdb_assert (target_info->shadow_contents >= readbuf + len
1539 || readbuf >= (target_info->shadow_contents
1540 + target_info->shadow_len));
1541
1542 /* Update the read buffer with this inserted breakpoint's
1543 shadow. */
1544 memcpy (readbuf + bp_addr - memaddr,
1545 target_info->shadow_contents + bptoffset, bp_size);
1546 }
1547 else
1548 {
1549 const unsigned char *bp;
1550 CORE_ADDR placed_address = target_info->placed_address;
1551 int placed_size = target_info->placed_size;
1552
1553 /* Update the shadow with what we want to write to memory. */
1554 memcpy (target_info->shadow_contents + bptoffset,
1555 writebuf_org + bp_addr - memaddr, bp_size);
1556
1557 /* Determine appropriate breakpoint contents and size for this
1558 address. */
1559 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1560
1561 /* Update the final write buffer with this inserted
1562 breakpoint's INSN. */
1563 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1564 }
1565}
1566
8defab1a 1567/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1568 by replacing any memory breakpoints with their shadowed contents.
1569
35c63cd8
JB
1570 If READBUF is not NULL, this buffer must not overlap with any of
1571 the breakpoint location's shadow_contents buffers. Otherwise,
1572 a failed assertion internal error will be raised.
1573
876fa593 1574 The range of shadowed area by each bp_location is:
35df4500
TJB
1575 bl->address - bp_location_placed_address_before_address_max
1576 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1577 The range we were requested to resolve shadows for is:
1578 memaddr ... memaddr + len
1579 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1580 memaddr + len <= (bl->address
1581 - bp_location_placed_address_before_address_max)
876fa593 1582 and:
35df4500 1583 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1584
8defab1a 1585void
f0ba3972
PA
1586breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1587 const gdb_byte *writebuf_org,
1588 ULONGEST memaddr, LONGEST len)
c906108c 1589{
4a64f543
MS
1590 /* Left boundary, right boundary and median element of our binary
1591 search. */
876fa593 1592 unsigned bc_l, bc_r, bc;
9d497a19 1593 size_t i;
876fa593 1594
4a64f543
MS
1595 /* Find BC_L which is a leftmost element which may affect BUF
1596 content. It is safe to report lower value but a failure to
1597 report higher one. */
876fa593
JK
1598
1599 bc_l = 0;
1600 bc_r = bp_location_count;
1601 while (bc_l + 1 < bc_r)
1602 {
35df4500 1603 struct bp_location *bl;
876fa593
JK
1604
1605 bc = (bc_l + bc_r) / 2;
35df4500 1606 bl = bp_location[bc];
876fa593 1607
4a64f543
MS
1608 /* Check first BL->ADDRESS will not overflow due to the added
1609 constant. Then advance the left boundary only if we are sure
1610 the BC element can in no way affect the BUF content (MEMADDR
1611 to MEMADDR + LEN range).
876fa593 1612
4a64f543
MS
1613 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1614 offset so that we cannot miss a breakpoint with its shadow
1615 range tail still reaching MEMADDR. */
c5aa993b 1616
35df4500
TJB
1617 if ((bl->address + bp_location_shadow_len_after_address_max
1618 >= bl->address)
1619 && (bl->address + bp_location_shadow_len_after_address_max
1620 <= memaddr))
876fa593
JK
1621 bc_l = bc;
1622 else
1623 bc_r = bc;
1624 }
1625
128070bb
PA
1626 /* Due to the binary search above, we need to make sure we pick the
1627 first location that's at BC_L's address. E.g., if there are
1628 multiple locations at the same address, BC_L may end up pointing
1629 at a duplicate location, and miss the "master"/"inserted"
1630 location. Say, given locations L1, L2 and L3 at addresses A and
1631 B:
1632
1633 L1@A, L2@A, L3@B, ...
1634
1635 BC_L could end up pointing at location L2, while the "master"
1636 location could be L1. Since the `loc->inserted' flag is only set
1637 on "master" locations, we'd forget to restore the shadow of L1
1638 and L2. */
1639 while (bc_l > 0
1640 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1641 bc_l--;
1642
876fa593
JK
1643 /* Now do full processing of the found relevant range of elements. */
1644
1645 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1646 {
35df4500 1647 struct bp_location *bl = bp_location[bc];
876fa593
JK
1648 CORE_ADDR bp_addr = 0;
1649 int bp_size = 0;
1650 int bptoffset = 0;
1651
35df4500
TJB
1652 /* bp_location array has BL->OWNER always non-NULL. */
1653 if (bl->owner->type == bp_none)
8a3fe4f8 1654 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1655 bl->owner->number);
ffce0d52 1656
e5dd4106 1657 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1658 content. */
1659
35df4500
TJB
1660 if (bl->address >= bp_location_placed_address_before_address_max
1661 && memaddr + len <= (bl->address
1662 - bp_location_placed_address_before_address_max))
876fa593
JK
1663 break;
1664
35df4500 1665 if (!bp_location_has_shadow (bl))
c5aa993b 1666 continue;
6c95b8df 1667
9d497a19
PA
1668 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1669 memaddr, len, &bl->target_info, bl->gdbarch);
1670 }
c5aa993b 1671
9d497a19
PA
1672 /* Now process single-step breakpoints. These are not found in the
1673 bp_location array. */
1674 for (i = 0; i < 2; i++)
1675 {
1676 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1677
9d497a19
PA
1678 if (bp_tgt != NULL)
1679 {
1680 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1681
9d497a19
PA
1682 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1683 memaddr, len, bp_tgt, gdbarch);
1684 }
1685 }
c906108c 1686}
9d497a19 1687
c906108c 1688\f
c5aa993b 1689
b775012e
LM
1690/* Return true if BPT is either a software breakpoint or a hardware
1691 breakpoint. */
1692
1693int
1694is_breakpoint (const struct breakpoint *bpt)
1695{
1696 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1697 || bpt->type == bp_hardware_breakpoint
1698 || bpt->type == bp_dprintf);
b775012e
LM
1699}
1700
60e1c644
PA
1701/* Return true if BPT is of any hardware watchpoint kind. */
1702
a5606eee 1703static int
d77f58be 1704is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1705{
1706 return (bpt->type == bp_hardware_watchpoint
1707 || bpt->type == bp_read_watchpoint
1708 || bpt->type == bp_access_watchpoint);
1709}
7270d8f2 1710
60e1c644
PA
1711/* Return true if BPT is of any watchpoint kind, hardware or
1712 software. */
1713
3a5c3e22 1714int
d77f58be 1715is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1716{
1717 return (is_hardware_watchpoint (bpt)
1718 || bpt->type == bp_watchpoint);
1719}
1720
3a5c3e22
PA
1721/* Returns true if the current thread and its running state are safe
1722 to evaluate or update watchpoint B. Watchpoints on local
1723 expressions need to be evaluated in the context of the thread that
1724 was current when the watchpoint was created, and, that thread needs
1725 to be stopped to be able to select the correct frame context.
1726 Watchpoints on global expressions can be evaluated on any thread,
1727 and in any state. It is presently left to the target allowing
1728 memory accesses when threads are running. */
f6bc2008
PA
1729
1730static int
3a5c3e22 1731watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1732{
d0d8b0c6
JK
1733 return (b->base.pspace == current_program_space
1734 && (ptid_equal (b->watchpoint_thread, null_ptid)
1735 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1736 && !is_executing (inferior_ptid))));
f6bc2008
PA
1737}
1738
d0fb5eae
JK
1739/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1740 associated bp_watchpoint_scope breakpoint. */
1741
1742static void
3a5c3e22 1743watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1744{
3a5c3e22 1745 struct breakpoint *b = &w->base;
d0fb5eae
JK
1746
1747 if (b->related_breakpoint != b)
1748 {
1749 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1750 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1751 b->related_breakpoint->disposition = disp_del_at_next_stop;
1752 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1753 b->related_breakpoint = b;
1754 }
1755 b->disposition = disp_del_at_next_stop;
1756}
1757
bb9d5f81
PP
1758/* Extract a bitfield value from value VAL using the bit parameters contained in
1759 watchpoint W. */
1760
1761static struct value *
1762extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1763{
1764 struct value *bit_val;
1765
1766 if (val == NULL)
1767 return NULL;
1768
1769 bit_val = allocate_value (value_type (val));
1770
1771 unpack_value_bitfield (bit_val,
1772 w->val_bitpos,
1773 w->val_bitsize,
1774 value_contents_for_printing (val),
1775 value_offset (val),
1776 val);
1777
1778 return bit_val;
1779}
1780
567e1b4e
JB
1781/* Assuming that B is a watchpoint:
1782 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1783 - Evaluate expression and store the result in B->val
567e1b4e
JB
1784 - Evaluate the condition if there is one, and store the result
1785 in b->loc->cond.
a5606eee
VP
1786 - Update the list of values that must be watched in B->loc.
1787
4a64f543
MS
1788 If the watchpoint disposition is disp_del_at_next_stop, then do
1789 nothing. If this is local watchpoint that is out of scope, delete
1790 it.
1791
1792 Even with `set breakpoint always-inserted on' the watchpoints are
1793 removed + inserted on each stop here. Normal breakpoints must
1794 never be removed because they might be missed by a running thread
1795 when debugging in non-stop mode. On the other hand, hardware
1796 watchpoints (is_hardware_watchpoint; processed here) are specific
1797 to each LWP since they are stored in each LWP's hardware debug
1798 registers. Therefore, such LWP must be stopped first in order to
1799 be able to modify its hardware watchpoints.
1800
1801 Hardware watchpoints must be reset exactly once after being
1802 presented to the user. It cannot be done sooner, because it would
1803 reset the data used to present the watchpoint hit to the user. And
1804 it must not be done later because it could display the same single
1805 watchpoint hit during multiple GDB stops. Note that the latter is
1806 relevant only to the hardware watchpoint types bp_read_watchpoint
1807 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1808 not user-visible - its hit is suppressed if the memory content has
1809 not changed.
1810
1811 The following constraints influence the location where we can reset
1812 hardware watchpoints:
1813
1814 * target_stopped_by_watchpoint and target_stopped_data_address are
1815 called several times when GDB stops.
1816
1817 [linux]
1818 * Multiple hardware watchpoints can be hit at the same time,
1819 causing GDB to stop. GDB only presents one hardware watchpoint
1820 hit at a time as the reason for stopping, and all the other hits
1821 are presented later, one after the other, each time the user
1822 requests the execution to be resumed. Execution is not resumed
1823 for the threads still having pending hit event stored in
1824 LWP_INFO->STATUS. While the watchpoint is already removed from
1825 the inferior on the first stop the thread hit event is kept being
1826 reported from its cached value by linux_nat_stopped_data_address
1827 until the real thread resume happens after the watchpoint gets
1828 presented and thus its LWP_INFO->STATUS gets reset.
1829
1830 Therefore the hardware watchpoint hit can get safely reset on the
1831 watchpoint removal from inferior. */
a79d3c27 1832
b40ce68a 1833static void
3a5c3e22 1834update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1835{
a5606eee 1836 int within_current_scope;
a5606eee 1837 struct frame_id saved_frame_id;
66076460 1838 int frame_saved;
a5606eee 1839
f6bc2008
PA
1840 /* If this is a local watchpoint, we only want to check if the
1841 watchpoint frame is in scope if the current thread is the thread
1842 that was used to create the watchpoint. */
1843 if (!watchpoint_in_thread_scope (b))
1844 return;
1845
3a5c3e22 1846 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1847 return;
1848
66076460 1849 frame_saved = 0;
a5606eee
VP
1850
1851 /* Determine if the watchpoint is within scope. */
1852 if (b->exp_valid_block == NULL)
1853 within_current_scope = 1;
1854 else
1855 {
b5db5dfc
UW
1856 struct frame_info *fi = get_current_frame ();
1857 struct gdbarch *frame_arch = get_frame_arch (fi);
1858 CORE_ADDR frame_pc = get_frame_pc (fi);
1859
1860 /* If we're in a function epilogue, unwinding may not work
1861 properly, so do not attempt to recreate locations at this
1862 point. See similar comments in watchpoint_check. */
1863 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1864 return;
66076460
DJ
1865
1866 /* Save the current frame's ID so we can restore it after
1867 evaluating the watchpoint expression on its own frame. */
1868 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1869 took a frame parameter, so that we didn't have to change the
1870 selected frame. */
1871 frame_saved = 1;
1872 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1873
a5606eee
VP
1874 fi = frame_find_by_id (b->watchpoint_frame);
1875 within_current_scope = (fi != NULL);
1876 if (within_current_scope)
1877 select_frame (fi);
1878 }
1879
b5db5dfc
UW
1880 /* We don't free locations. They are stored in the bp_location array
1881 and update_global_location_list will eventually delete them and
1882 remove breakpoints if needed. */
3a5c3e22 1883 b->base.loc = NULL;
b5db5dfc 1884
a5606eee
VP
1885 if (within_current_scope && reparse)
1886 {
bbc13ae3 1887 const char *s;
d63d0675 1888
a5606eee
VP
1889 if (b->exp)
1890 {
1891 xfree (b->exp);
1892 b->exp = NULL;
1893 }
d63d0675 1894 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1895 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1896 /* If the meaning of expression itself changed, the old value is
1897 no longer relevant. We don't want to report a watchpoint hit
1898 to the user when the old value and the new value may actually
1899 be completely different objects. */
1900 value_free (b->val);
fa4727a6
DJ
1901 b->val = NULL;
1902 b->val_valid = 0;
60e1c644
PA
1903
1904 /* Note that unlike with breakpoints, the watchpoint's condition
1905 expression is stored in the breakpoint object, not in the
1906 locations (re)created below. */
3a5c3e22 1907 if (b->base.cond_string != NULL)
60e1c644
PA
1908 {
1909 if (b->cond_exp != NULL)
1910 {
1911 xfree (b->cond_exp);
1912 b->cond_exp = NULL;
1913 }
1914
3a5c3e22 1915 s = b->base.cond_string;
1bb9788d 1916 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1917 }
a5606eee 1918 }
a5606eee
VP
1919
1920 /* If we failed to parse the expression, for example because
1921 it refers to a global variable in a not-yet-loaded shared library,
1922 don't try to insert watchpoint. We don't automatically delete
1923 such watchpoint, though, since failure to parse expression
1924 is different from out-of-scope watchpoint. */
e8369a73 1925 if (!target_has_execution)
2d134ed3
PA
1926 {
1927 /* Without execution, memory can't change. No use to try and
1928 set watchpoint locations. The watchpoint will be reset when
1929 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1930 if (!can_use_hw_watchpoints)
1931 {
1932 if (b->base.ops->works_in_software_mode (&b->base))
1933 b->base.type = bp_watchpoint;
1934 else
638aa5a1
AB
1935 error (_("Can't set read/access watchpoint when "
1936 "hardware watchpoints are disabled."));
e8369a73 1937 }
2d134ed3
PA
1938 }
1939 else if (within_current_scope && b->exp)
a5606eee 1940 {
0cf6dd15 1941 int pc = 0;
fa4727a6 1942 struct value *val_chain, *v, *result, *next;
2d134ed3 1943 struct program_space *frame_pspace;
a5606eee 1944
3a1115a0 1945 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1946
a5606eee
VP
1947 /* Avoid setting b->val if it's already set. The meaning of
1948 b->val is 'the last value' user saw, and we should update
1949 it only if we reported that last value to user. As it
9c06b0b4
TJB
1950 happens, the code that reports it updates b->val directly.
1951 We don't keep track of the memory value for masked
1952 watchpoints. */
3a5c3e22 1953 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1954 {
bb9d5f81
PP
1955 if (b->val_bitsize != 0)
1956 {
1957 v = extract_bitfield_from_watchpoint_value (b, v);
1958 if (v != NULL)
1959 release_value (v);
1960 }
fa4727a6
DJ
1961 b->val = v;
1962 b->val_valid = 1;
1963 }
a5606eee 1964
2d134ed3
PA
1965 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1966
a5606eee 1967 /* Look at each value on the value chain. */
9fa40276 1968 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1969 {
1970 /* If it's a memory location, and GDB actually needed
1971 its contents to evaluate the expression, then we
fa4727a6
DJ
1972 must watch it. If the first value returned is
1973 still lazy, that means an error occurred reading it;
1974 watch it anyway in case it becomes readable. */
a5606eee 1975 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1976 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1977 {
1978 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1979
a5606eee
VP
1980 /* We only watch structs and arrays if user asked
1981 for it explicitly, never if they just happen to
1982 appear in the middle of some value chain. */
fa4727a6 1983 if (v == result
a5606eee
VP
1984 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1985 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1986 {
1987 CORE_ADDR addr;
744a8059 1988 int type;
a5606eee 1989 struct bp_location *loc, **tmp;
bb9d5f81
PP
1990 int bitpos = 0, bitsize = 0;
1991
1992 if (value_bitsize (v) != 0)
1993 {
1994 /* Extract the bit parameters out from the bitfield
1995 sub-expression. */
1996 bitpos = value_bitpos (v);
1997 bitsize = value_bitsize (v);
1998 }
1999 else if (v == result && b->val_bitsize != 0)
2000 {
2001 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2002 lvalue whose bit parameters are saved in the fields
2003 VAL_BITPOS and VAL_BITSIZE. */
2004 bitpos = b->val_bitpos;
2005 bitsize = b->val_bitsize;
2006 }
a5606eee 2007
42ae5230 2008 addr = value_address (v);
bb9d5f81
PP
2009 if (bitsize != 0)
2010 {
2011 /* Skip the bytes that don't contain the bitfield. */
2012 addr += bitpos / 8;
2013 }
2014
a5606eee 2015 type = hw_write;
3a5c3e22 2016 if (b->base.type == bp_read_watchpoint)
a5606eee 2017 type = hw_read;
3a5c3e22 2018 else if (b->base.type == bp_access_watchpoint)
a5606eee 2019 type = hw_access;
3a5c3e22
PA
2020
2021 loc = allocate_bp_location (&b->base);
2022 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2023 ;
2024 *tmp = loc;
a6d9a66e 2025 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2026
2027 loc->pspace = frame_pspace;
a5606eee 2028 loc->address = addr;
bb9d5f81
PP
2029
2030 if (bitsize != 0)
2031 {
2032 /* Just cover the bytes that make up the bitfield. */
2033 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2034 }
2035 else
2036 loc->length = TYPE_LENGTH (value_type (v));
2037
a5606eee
VP
2038 loc->watchpoint_type = type;
2039 }
2040 }
9fa40276
TJB
2041 }
2042
2043 /* Change the type of breakpoint between hardware assisted or
2044 an ordinary watchpoint depending on the hardware support
2045 and free hardware slots. REPARSE is set when the inferior
2046 is started. */
a9634178 2047 if (reparse)
9fa40276 2048 {
e09342b5 2049 int reg_cnt;
9fa40276
TJB
2050 enum bp_loc_type loc_type;
2051 struct bp_location *bl;
a5606eee 2052
a9634178 2053 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2054
2055 if (reg_cnt)
9fa40276
TJB
2056 {
2057 int i, target_resources_ok, other_type_used;
a1398e0c 2058 enum bptype type;
9fa40276 2059
a9634178
TJB
2060 /* Use an exact watchpoint when there's only one memory region to be
2061 watched, and only one debug register is needed to watch it. */
2062 b->exact = target_exact_watchpoints && reg_cnt == 1;
2063
9fa40276 2064 /* We need to determine how many resources are already
e09342b5
TJB
2065 used for all other hardware watchpoints plus this one
2066 to see if we still have enough resources to also fit
a1398e0c
PA
2067 this watchpoint in as well. */
2068
2069 /* If this is a software watchpoint, we try to turn it
2070 to a hardware one -- count resources as if B was of
2071 hardware watchpoint type. */
2072 type = b->base.type;
2073 if (type == bp_watchpoint)
2074 type = bp_hardware_watchpoint;
2075
2076 /* This watchpoint may or may not have been placed on
2077 the list yet at this point (it won't be in the list
2078 if we're trying to create it for the first time,
2079 through watch_command), so always account for it
2080 manually. */
2081
2082 /* Count resources used by all watchpoints except B. */
2083 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2084
2085 /* Add in the resources needed for B. */
2086 i += hw_watchpoint_use_count (&b->base);
2087
2088 target_resources_ok
2089 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2090 if (target_resources_ok <= 0)
a9634178 2091 {
3a5c3e22 2092 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2093
2094 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2095 error (_("Target does not support this type of "
2096 "hardware watchpoint."));
9c06b0b4
TJB
2097 else if (target_resources_ok < 0 && !sw_mode)
2098 error (_("There are not enough available hardware "
2099 "resources for this watchpoint."));
a1398e0c
PA
2100
2101 /* Downgrade to software watchpoint. */
2102 b->base.type = bp_watchpoint;
2103 }
2104 else
2105 {
2106 /* If this was a software watchpoint, we've just
2107 found we have enough resources to turn it to a
2108 hardware watchpoint. Otherwise, this is a
2109 nop. */
2110 b->base.type = type;
a9634178 2111 }
9fa40276 2112 }
3a5c3e22 2113 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2114 {
2115 if (!can_use_hw_watchpoints)
2116 error (_("Can't set read/access watchpoint when "
2117 "hardware watchpoints are disabled."));
2118 else
2119 error (_("Expression cannot be implemented with "
2120 "read/access watchpoint."));
2121 }
9fa40276 2122 else
3a5c3e22 2123 b->base.type = bp_watchpoint;
9fa40276 2124
3a5c3e22 2125 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2126 : bp_loc_hardware_watchpoint);
3a5c3e22 2127 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2128 bl->loc_type = loc_type;
2129 }
2130
2131 for (v = val_chain; v; v = next)
2132 {
a5606eee
VP
2133 next = value_next (v);
2134 if (v != b->val)
2135 value_free (v);
2136 }
2137
c7437ca6
PA
2138 /* If a software watchpoint is not watching any memory, then the
2139 above left it without any location set up. But,
2140 bpstat_stop_status requires a location to be able to report
2141 stops, so make sure there's at least a dummy one. */
3a5c3e22 2142 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2143 {
3a5c3e22
PA
2144 struct breakpoint *base = &b->base;
2145 base->loc = allocate_bp_location (base);
2146 base->loc->pspace = frame_pspace;
2147 base->loc->address = -1;
2148 base->loc->length = -1;
2149 base->loc->watchpoint_type = -1;
c7437ca6 2150 }
a5606eee
VP
2151 }
2152 else if (!within_current_scope)
7270d8f2 2153 {
ac74f770
MS
2154 printf_filtered (_("\
2155Watchpoint %d deleted because the program has left the block\n\
2156in which its expression is valid.\n"),
3a5c3e22 2157 b->base.number);
d0fb5eae 2158 watchpoint_del_at_next_stop (b);
7270d8f2 2159 }
a5606eee
VP
2160
2161 /* Restore the selected frame. */
66076460
DJ
2162 if (frame_saved)
2163 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2164}
2165
a5606eee 2166
74960c60 2167/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2168 inserted in the inferior. We don't differentiate the type of BL's owner
2169 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2170 breakpoint_ops is not defined, because in insert_bp_location,
2171 tracepoint's insert_location will not be called. */
74960c60 2172static int
35df4500 2173should_be_inserted (struct bp_location *bl)
74960c60 2174{
35df4500 2175 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2176 return 0;
2177
35df4500 2178 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2179 return 0;
2180
35df4500 2181 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2182 return 0;
2183
f8eba3c6
TT
2184 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2185 return 0;
2186
56710373
PA
2187 /* This is set for example, when we're attached to the parent of a
2188 vfork, and have detached from the child. The child is running
2189 free, and we expect it to do an exec or exit, at which point the
2190 OS makes the parent schedulable again (and the target reports
2191 that the vfork is done). Until the child is done with the shared
2192 memory region, do not insert breakpoints in the parent, otherwise
2193 the child could still trip on the parent's breakpoints. Since
2194 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2195 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2196 return 0;
2197
31e77af2
PA
2198 /* Don't insert a breakpoint if we're trying to step past its
2199 location. */
2200 if ((bl->loc_type == bp_loc_software_breakpoint
2201 || bl->loc_type == bp_loc_hardware_breakpoint)
2202 && stepping_past_instruction_at (bl->pspace->aspace,
2203 bl->address))
e558d7c1
PA
2204 {
2205 if (debug_infrun)
2206 {
2207 fprintf_unfiltered (gdb_stdlog,
2208 "infrun: skipping breakpoint: "
2209 "stepping past insn at: %s\n",
2210 paddress (bl->gdbarch, bl->address));
2211 }
2212 return 0;
2213 }
31e77af2 2214
74960c60
VP
2215 return 1;
2216}
2217
934709f0
PW
2218/* Same as should_be_inserted but does the check assuming
2219 that the location is not duplicated. */
2220
2221static int
2222unduplicated_should_be_inserted (struct bp_location *bl)
2223{
2224 int result;
2225 const int save_duplicate = bl->duplicate;
2226
2227 bl->duplicate = 0;
2228 result = should_be_inserted (bl);
2229 bl->duplicate = save_duplicate;
2230 return result;
2231}
2232
b775012e
LM
2233/* Parses a conditional described by an expression COND into an
2234 agent expression bytecode suitable for evaluation
2235 by the bytecode interpreter. Return NULL if there was
2236 any error during parsing. */
2237
2238static struct agent_expr *
2239parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2240{
2241 struct agent_expr *aexpr = NULL;
b775012e
LM
2242 volatile struct gdb_exception ex;
2243
2244 if (!cond)
2245 return NULL;
2246
2247 /* We don't want to stop processing, so catch any errors
2248 that may show up. */
2249 TRY_CATCH (ex, RETURN_MASK_ERROR)
2250 {
2251 aexpr = gen_eval_for_expr (scope, cond);
2252 }
2253
2254 if (ex.reason < 0)
2255 {
2256 /* If we got here, it means the condition could not be parsed to a valid
2257 bytecode expression and thus can't be evaluated on the target's side.
2258 It's no use iterating through the conditions. */
2259 return NULL;
2260 }
2261
2262 /* We have a valid agent expression. */
2263 return aexpr;
2264}
2265
2266/* Based on location BL, create a list of breakpoint conditions to be
2267 passed on to the target. If we have duplicated locations with different
2268 conditions, we will add such conditions to the list. The idea is that the
2269 target will evaluate the list of conditions and will only notify GDB when
2270 one of them is true. */
2271
2272static void
2273build_target_condition_list (struct bp_location *bl)
2274{
2275 struct bp_location **locp = NULL, **loc2p;
2276 int null_condition_or_parse_error = 0;
2277 int modified = bl->needs_update;
2278 struct bp_location *loc;
2279
8b4f3082
PA
2280 /* Release conditions left over from a previous insert. */
2281 VEC_free (agent_expr_p, bl->target_info.conditions);
2282
b775012e
LM
2283 /* This is only meaningful if the target is
2284 evaluating conditions and if the user has
2285 opted for condition evaluation on the target's
2286 side. */
2287 if (gdb_evaluates_breakpoint_condition_p ()
2288 || !target_supports_evaluation_of_breakpoint_conditions ())
2289 return;
2290
2291 /* Do a first pass to check for locations with no assigned
2292 conditions or conditions that fail to parse to a valid agent expression
2293 bytecode. If any of these happen, then it's no use to send conditions
2294 to the target since this location will always trigger and generate a
2295 response back to GDB. */
2296 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2297 {
2298 loc = (*loc2p);
2299 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2300 {
2301 if (modified)
2302 {
2303 struct agent_expr *aexpr;
2304
2305 /* Re-parse the conditions since something changed. In that
2306 case we already freed the condition bytecodes (see
2307 force_breakpoint_reinsertion). We just
2308 need to parse the condition to bytecodes again. */
2309 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2310 loc->cond_bytecode = aexpr;
2311
2312 /* Check if we managed to parse the conditional expression
2313 correctly. If not, we will not send this condition
2314 to the target. */
2315 if (aexpr)
2316 continue;
2317 }
2318
2319 /* If we have a NULL bytecode expression, it means something
2320 went wrong or we have a null condition expression. */
2321 if (!loc->cond_bytecode)
2322 {
2323 null_condition_or_parse_error = 1;
2324 break;
2325 }
2326 }
2327 }
2328
2329 /* If any of these happened, it means we will have to evaluate the conditions
2330 for the location's address on gdb's side. It is no use keeping bytecodes
2331 for all the other duplicate locations, thus we free all of them here.
2332
2333 This is so we have a finer control over which locations' conditions are
2334 being evaluated by GDB or the remote stub. */
2335 if (null_condition_or_parse_error)
2336 {
2337 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2338 {
2339 loc = (*loc2p);
2340 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2341 {
2342 /* Only go as far as the first NULL bytecode is
2343 located. */
2344 if (!loc->cond_bytecode)
2345 return;
2346
2347 free_agent_expr (loc->cond_bytecode);
2348 loc->cond_bytecode = NULL;
2349 }
2350 }
2351 }
2352
2353 /* No NULL conditions or failed bytecode generation. Build a condition list
2354 for this location's address. */
2355 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2356 {
2357 loc = (*loc2p);
2358 if (loc->cond
2359 && is_breakpoint (loc->owner)
2360 && loc->pspace->num == bl->pspace->num
2361 && loc->owner->enable_state == bp_enabled
2362 && loc->enabled)
2363 /* Add the condition to the vector. This will be used later to send the
2364 conditions to the target. */
2365 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2366 loc->cond_bytecode);
2367 }
2368
2369 return;
2370}
2371
d3ce09f5
SS
2372/* Parses a command described by string CMD into an agent expression
2373 bytecode suitable for evaluation by the bytecode interpreter.
2374 Return NULL if there was any error during parsing. */
2375
2376static struct agent_expr *
2377parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2378{
2379 struct cleanup *old_cleanups = 0;
2380 struct expression *expr, **argvec;
2381 struct agent_expr *aexpr = NULL;
d3ce09f5 2382 volatile struct gdb_exception ex;
bbc13ae3
KS
2383 const char *cmdrest;
2384 const char *format_start, *format_end;
d3ce09f5
SS
2385 struct format_piece *fpieces;
2386 int nargs;
2387 struct gdbarch *gdbarch = get_current_arch ();
2388
2389 if (!cmd)
2390 return NULL;
2391
2392 cmdrest = cmd;
2393
2394 if (*cmdrest == ',')
2395 ++cmdrest;
bbc13ae3 2396 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2397
2398 if (*cmdrest++ != '"')
2399 error (_("No format string following the location"));
2400
2401 format_start = cmdrest;
2402
2403 fpieces = parse_format_string (&cmdrest);
2404
2405 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2406
2407 format_end = cmdrest;
2408
2409 if (*cmdrest++ != '"')
2410 error (_("Bad format string, non-terminated '\"'."));
2411
bbc13ae3 2412 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2413
2414 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2415 error (_("Invalid argument syntax"));
2416
2417 if (*cmdrest == ',')
2418 cmdrest++;
bbc13ae3 2419 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2420
2421 /* For each argument, make an expression. */
2422
2423 argvec = (struct expression **) alloca (strlen (cmd)
2424 * sizeof (struct expression *));
2425
2426 nargs = 0;
2427 while (*cmdrest != '\0')
2428 {
bbc13ae3 2429 const char *cmd1;
d3ce09f5
SS
2430
2431 cmd1 = cmdrest;
2432 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2433 argvec[nargs++] = expr;
2434 cmdrest = cmd1;
2435 if (*cmdrest == ',')
2436 ++cmdrest;
2437 }
2438
2439 /* We don't want to stop processing, so catch any errors
2440 that may show up. */
2441 TRY_CATCH (ex, RETURN_MASK_ERROR)
2442 {
2443 aexpr = gen_printf (scope, gdbarch, 0, 0,
2444 format_start, format_end - format_start,
2445 fpieces, nargs, argvec);
2446 }
2447
752eb8b4
TT
2448 do_cleanups (old_cleanups);
2449
d3ce09f5
SS
2450 if (ex.reason < 0)
2451 {
2452 /* If we got here, it means the command could not be parsed to a valid
2453 bytecode expression and thus can't be evaluated on the target's side.
2454 It's no use iterating through the other commands. */
2455 return NULL;
2456 }
2457
d3ce09f5
SS
2458 /* We have a valid agent expression, return it. */
2459 return aexpr;
2460}
2461
2462/* Based on location BL, create a list of breakpoint commands to be
2463 passed on to the target. If we have duplicated locations with
2464 different commands, we will add any such to the list. */
2465
2466static void
2467build_target_command_list (struct bp_location *bl)
2468{
2469 struct bp_location **locp = NULL, **loc2p;
2470 int null_command_or_parse_error = 0;
2471 int modified = bl->needs_update;
2472 struct bp_location *loc;
2473
8b4f3082
PA
2474 /* Release commands left over from a previous insert. */
2475 VEC_free (agent_expr_p, bl->target_info.tcommands);
2476
41fac0cf 2477 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2478 return;
2479
41fac0cf
PA
2480 /* For now, limit to agent-style dprintf breakpoints. */
2481 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2482 return;
2483
41fac0cf
PA
2484 /* For now, if we have any duplicate location that isn't a dprintf,
2485 don't install the target-side commands, as that would make the
2486 breakpoint not be reported to the core, and we'd lose
2487 control. */
2488 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2489 {
2490 loc = (*loc2p);
2491 if (is_breakpoint (loc->owner)
2492 && loc->pspace->num == bl->pspace->num
2493 && loc->owner->type != bp_dprintf)
2494 return;
2495 }
2496
d3ce09f5
SS
2497 /* Do a first pass to check for locations with no assigned
2498 conditions or conditions that fail to parse to a valid agent expression
2499 bytecode. If any of these happen, then it's no use to send conditions
2500 to the target since this location will always trigger and generate a
2501 response back to GDB. */
2502 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2503 {
2504 loc = (*loc2p);
2505 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2506 {
2507 if (modified)
2508 {
2509 struct agent_expr *aexpr;
2510
2511 /* Re-parse the commands since something changed. In that
2512 case we already freed the command bytecodes (see
2513 force_breakpoint_reinsertion). We just
2514 need to parse the command to bytecodes again. */
2515 aexpr = parse_cmd_to_aexpr (bl->address,
2516 loc->owner->extra_string);
2517 loc->cmd_bytecode = aexpr;
2518
2519 if (!aexpr)
2520 continue;
2521 }
2522
2523 /* If we have a NULL bytecode expression, it means something
2524 went wrong or we have a null command expression. */
2525 if (!loc->cmd_bytecode)
2526 {
2527 null_command_or_parse_error = 1;
2528 break;
2529 }
2530 }
2531 }
2532
2533 /* If anything failed, then we're not doing target-side commands,
2534 and so clean up. */
2535 if (null_command_or_parse_error)
2536 {
2537 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2538 {
2539 loc = (*loc2p);
2540 if (is_breakpoint (loc->owner)
2541 && loc->pspace->num == bl->pspace->num)
2542 {
2543 /* Only go as far as the first NULL bytecode is
2544 located. */
40fb6c5e 2545 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2546 return;
2547
40fb6c5e
HZ
2548 free_agent_expr (loc->cmd_bytecode);
2549 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2550 }
2551 }
2552 }
2553
2554 /* No NULL commands or failed bytecode generation. Build a command list
2555 for this location's address. */
2556 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2557 {
2558 loc = (*loc2p);
2559 if (loc->owner->extra_string
2560 && is_breakpoint (loc->owner)
2561 && loc->pspace->num == bl->pspace->num
2562 && loc->owner->enable_state == bp_enabled
2563 && loc->enabled)
2564 /* Add the command to the vector. This will be used later
2565 to send the commands to the target. */
2566 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2567 loc->cmd_bytecode);
2568 }
2569
2570 bl->target_info.persist = 0;
2571 /* Maybe flag this location as persistent. */
2572 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2573 bl->target_info.persist = 1;
2574}
2575
35df4500
TJB
2576/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2577 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2578 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2579 Returns 0 for success, 1 if the bp_location type is not supported or
2580 -1 for failure.
879bfdc2 2581
4a64f543
MS
2582 NOTE drow/2003-09-09: This routine could be broken down to an
2583 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2584static int
35df4500 2585insert_bp_location (struct bp_location *bl,
26bb91f3 2586 struct ui_file *tmp_error_stream,
3fbb6ffa 2587 int *disabled_breaks,
dd61ec5c
MW
2588 int *hw_breakpoint_error,
2589 int *hw_bp_error_explained_already)
879bfdc2 2590{
0000e5cc
PA
2591 enum errors bp_err = GDB_NO_ERROR;
2592 const char *bp_err_message = NULL;
c90a6fb7 2593 volatile struct gdb_exception e;
879bfdc2 2594
b775012e 2595 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2596 return 0;
2597
35c63cd8
JB
2598 /* Note we don't initialize bl->target_info, as that wipes out
2599 the breakpoint location's shadow_contents if the breakpoint
2600 is still inserted at that location. This in turn breaks
2601 target_read_memory which depends on these buffers when
2602 a memory read is requested at the breakpoint location:
2603 Once the target_info has been wiped, we fail to see that
2604 we have a breakpoint inserted at that address and thus
2605 read the breakpoint instead of returning the data saved in
2606 the breakpoint location's shadow contents. */
35df4500
TJB
2607 bl->target_info.placed_address = bl->address;
2608 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2609 bl->target_info.length = bl->length;
8181d85f 2610
b775012e
LM
2611 /* When working with target-side conditions, we must pass all the conditions
2612 for the same breakpoint address down to the target since GDB will not
2613 insert those locations. With a list of breakpoint conditions, the target
2614 can decide when to stop and notify GDB. */
2615
2616 if (is_breakpoint (bl->owner))
2617 {
2618 build_target_condition_list (bl);
d3ce09f5
SS
2619 build_target_command_list (bl);
2620 /* Reset the modification marker. */
b775012e
LM
2621 bl->needs_update = 0;
2622 }
2623
35df4500
TJB
2624 if (bl->loc_type == bp_loc_software_breakpoint
2625 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2626 {
35df4500 2627 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2628 {
2629 /* If the explicitly specified breakpoint type
2630 is not hardware breakpoint, check the memory map to see
2631 if the breakpoint address is in read only memory or not.
4a64f543 2632
765dc015
VP
2633 Two important cases are:
2634 - location type is not hardware breakpoint, memory
2635 is readonly. We change the type of the location to
2636 hardware breakpoint.
4a64f543
MS
2637 - location type is hardware breakpoint, memory is
2638 read-write. This means we've previously made the
2639 location hardware one, but then the memory map changed,
2640 so we undo.
765dc015 2641
4a64f543
MS
2642 When breakpoints are removed, remove_breakpoints will use
2643 location types we've just set here, the only possible
2644 problem is that memory map has changed during running
2645 program, but it's not going to work anyway with current
2646 gdb. */
765dc015 2647 struct mem_region *mr
35df4500 2648 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2649
2650 if (mr)
2651 {
2652 if (automatic_hardware_breakpoints)
2653 {
765dc015
VP
2654 enum bp_loc_type new_type;
2655
2656 if (mr->attrib.mode != MEM_RW)
2657 new_type = bp_loc_hardware_breakpoint;
2658 else
2659 new_type = bp_loc_software_breakpoint;
2660
35df4500 2661 if (new_type != bl->loc_type)
765dc015
VP
2662 {
2663 static int said = 0;
cc59ec59 2664
35df4500 2665 bl->loc_type = new_type;
765dc015
VP
2666 if (!said)
2667 {
3e43a32a
MS
2668 fprintf_filtered (gdb_stdout,
2669 _("Note: automatically using "
2670 "hardware breakpoints for "
2671 "read-only addresses.\n"));
765dc015
VP
2672 said = 1;
2673 }
2674 }
2675 }
35df4500 2676 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2677 && mr->attrib.mode != MEM_RW)
2678 {
2679 fprintf_unfiltered (tmp_error_stream,
2680 _("Cannot insert breakpoint %d.\n"
2681 "Cannot set software breakpoint "
2682 "at read-only address %s\n"),
2683 bl->owner->number,
2684 paddress (bl->gdbarch, bl->address));
2685 return 1;
2686 }
765dc015
VP
2687 }
2688 }
2689
879bfdc2
DJ
2690 /* First check to see if we have to handle an overlay. */
2691 if (overlay_debugging == ovly_off
35df4500
TJB
2692 || bl->section == NULL
2693 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2694 {
2695 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2696 TRY_CATCH (e, RETURN_MASK_ALL)
2697 {
0000e5cc
PA
2698 int val;
2699
dd61ec5c 2700 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2701 if (val)
2702 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2703 }
2704 if (e.reason < 0)
2705 {
0000e5cc
PA
2706 bp_err = e.error;
2707 bp_err_message = e.message;
dd61ec5c 2708 }
879bfdc2
DJ
2709 }
2710 else
2711 {
4a64f543 2712 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2713 Shall we set a breakpoint at the LMA? */
2714 if (!overlay_events_enabled)
2715 {
2716 /* Yes -- overlay event support is not active,
2717 so we must try to set a breakpoint at the LMA.
2718 This will not work for a hardware breakpoint. */
35df4500 2719 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2720 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2721 bl->owner->number);
879bfdc2
DJ
2722 else
2723 {
35df4500
TJB
2724 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2725 bl->section);
879bfdc2 2726 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2727 bl->overlay_target_info = bl->target_info;
2728 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2729
2730 /* No overlay handling: just set the breakpoint. */
2731 TRY_CATCH (e, RETURN_MASK_ALL)
2732 {
2733 int val;
2734
2735 val = target_insert_breakpoint (bl->gdbarch,
2736 &bl->overlay_target_info);
2737 if (val)
2738 bp_err = GENERIC_ERROR;
2739 }
2740 if (e.reason < 0)
2741 {
2742 bp_err = e.error;
2743 bp_err_message = e.message;
2744 }
2745
2746 if (bp_err != GDB_NO_ERROR)
99361f52 2747 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2748 "Overlay breakpoint %d "
2749 "failed: in ROM?\n",
35df4500 2750 bl->owner->number);
879bfdc2
DJ
2751 }
2752 }
2753 /* Shall we set a breakpoint at the VMA? */
35df4500 2754 if (section_is_mapped (bl->section))
879bfdc2
DJ
2755 {
2756 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2757 TRY_CATCH (e, RETURN_MASK_ALL)
2758 {
0000e5cc
PA
2759 int val;
2760
dd61ec5c 2761 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2762 if (val)
2763 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2764 }
2765 if (e.reason < 0)
2766 {
0000e5cc
PA
2767 bp_err = e.error;
2768 bp_err_message = e.message;
dd61ec5c 2769 }
879bfdc2
DJ
2770 }
2771 else
2772 {
2773 /* No. This breakpoint will not be inserted.
2774 No error, but do not mark the bp as 'inserted'. */
2775 return 0;
2776 }
2777 }
2778
0000e5cc 2779 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2780 {
2781 /* Can't set the breakpoint. */
0000e5cc
PA
2782
2783 /* In some cases, we might not be able to insert a
2784 breakpoint in a shared library that has already been
2785 removed, but we have not yet processed the shlib unload
2786 event. Unfortunately, some targets that implement
076855f9
PA
2787 breakpoint insertion themselves can't tell why the
2788 breakpoint insertion failed (e.g., the remote target
2789 doesn't define error codes), so we must treat generic
2790 errors as memory errors. */
0000e5cc 2791 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2792 && bl->loc_type == bp_loc_software_breakpoint
08351840 2793 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2794 || shared_objfile_contains_address_p (bl->pspace,
2795 bl->address)))
879bfdc2 2796 {
4a64f543 2797 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2798 bl->shlib_disabled = 1;
8d3788bd 2799 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2800 if (!*disabled_breaks)
2801 {
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Cannot insert breakpoint %d.\n",
2804 bl->owner->number);
2805 fprintf_unfiltered (tmp_error_stream,
2806 "Temporarily disabling shared "
2807 "library breakpoints:\n");
2808 }
2809 *disabled_breaks = 1;
879bfdc2 2810 fprintf_unfiltered (tmp_error_stream,
35df4500 2811 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2812 return 0;
879bfdc2
DJ
2813 }
2814 else
879bfdc2 2815 {
35df4500 2816 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2817 {
0000e5cc
PA
2818 *hw_breakpoint_error = 1;
2819 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2820 fprintf_unfiltered (tmp_error_stream,
2821 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2822 bl->owner->number, bp_err_message ? ":" : ".\n");
2823 if (bp_err_message != NULL)
2824 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2825 }
2826 else
2827 {
0000e5cc
PA
2828 if (bp_err_message == NULL)
2829 {
2830 char *message
2831 = memory_error_message (TARGET_XFER_E_IO,
2832 bl->gdbarch, bl->address);
2833 struct cleanup *old_chain = make_cleanup (xfree, message);
2834
2835 fprintf_unfiltered (tmp_error_stream,
2836 "Cannot insert breakpoint %d.\n"
2837 "%s\n",
2838 bl->owner->number, message);
2839 do_cleanups (old_chain);
2840 }
2841 else
2842 {
2843 fprintf_unfiltered (tmp_error_stream,
2844 "Cannot insert breakpoint %d: %s\n",
2845 bl->owner->number,
2846 bp_err_message);
2847 }
879bfdc2 2848 }
0000e5cc 2849 return 1;
879bfdc2
DJ
2850
2851 }
2852 }
2853 else
35df4500 2854 bl->inserted = 1;
879bfdc2 2855
0000e5cc 2856 return 0;
879bfdc2
DJ
2857 }
2858
35df4500 2859 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2860 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2861 watchpoints. It's not clear that it's necessary... */
35df4500 2862 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2863 {
0000e5cc
PA
2864 int val;
2865
77b06cd7
TJB
2866 gdb_assert (bl->owner->ops != NULL
2867 && bl->owner->ops->insert_location != NULL);
2868
2869 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2870
2871 /* If trying to set a read-watchpoint, and it turns out it's not
2872 supported, try emulating one with an access watchpoint. */
35df4500 2873 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2874 {
2875 struct bp_location *loc, **loc_temp;
2876
2877 /* But don't try to insert it, if there's already another
2878 hw_access location that would be considered a duplicate
2879 of this one. */
2880 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2881 if (loc != bl
85d721b8 2882 && loc->watchpoint_type == hw_access
35df4500 2883 && watchpoint_locations_match (bl, loc))
85d721b8 2884 {
35df4500
TJB
2885 bl->duplicate = 1;
2886 bl->inserted = 1;
2887 bl->target_info = loc->target_info;
2888 bl->watchpoint_type = hw_access;
85d721b8
PA
2889 val = 0;
2890 break;
2891 }
2892
2893 if (val == 1)
2894 {
77b06cd7
TJB
2895 bl->watchpoint_type = hw_access;
2896 val = bl->owner->ops->insert_location (bl);
2897
2898 if (val)
2899 /* Back to the original value. */
2900 bl->watchpoint_type = hw_read;
85d721b8
PA
2901 }
2902 }
2903
35df4500 2904 bl->inserted = (val == 0);
879bfdc2
DJ
2905 }
2906
35df4500 2907 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2908 {
0000e5cc
PA
2909 int val;
2910
77b06cd7
TJB
2911 gdb_assert (bl->owner->ops != NULL
2912 && bl->owner->ops->insert_location != NULL);
2913
2914 val = bl->owner->ops->insert_location (bl);
2915 if (val)
2916 {
2917 bl->owner->enable_state = bp_disabled;
2918
2919 if (val == 1)
2920 warning (_("\
2921Error inserting catchpoint %d: Your system does not support this type\n\
2922of catchpoint."), bl->owner->number);
2923 else
2924 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2925 }
2926
2927 bl->inserted = (val == 0);
1640b821
DJ
2928
2929 /* We've already printed an error message if there was a problem
2930 inserting this catchpoint, and we've disabled the catchpoint,
2931 so just return success. */
2932 return 0;
879bfdc2
DJ
2933 }
2934
2935 return 0;
2936}
2937
6c95b8df
PA
2938/* This function is called when program space PSPACE is about to be
2939 deleted. It takes care of updating breakpoints to not reference
2940 PSPACE anymore. */
2941
2942void
2943breakpoint_program_space_exit (struct program_space *pspace)
2944{
2945 struct breakpoint *b, *b_temp;
876fa593 2946 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2947
2948 /* Remove any breakpoint that was set through this program space. */
2949 ALL_BREAKPOINTS_SAFE (b, b_temp)
2950 {
2951 if (b->pspace == pspace)
2952 delete_breakpoint (b);
2953 }
2954
2955 /* Breakpoints set through other program spaces could have locations
2956 bound to PSPACE as well. Remove those. */
876fa593 2957 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2958 {
2959 struct bp_location *tmp;
2960
2961 if (loc->pspace == pspace)
2962 {
2bdf28a0 2963 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2964 if (loc->owner->loc == loc)
2965 loc->owner->loc = loc->next;
2966 else
2967 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2968 if (tmp->next == loc)
2969 {
2970 tmp->next = loc->next;
2971 break;
2972 }
2973 }
2974 }
2975
2976 /* Now update the global location list to permanently delete the
2977 removed locations above. */
44702360 2978 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2979}
2980
74960c60
VP
2981/* Make sure all breakpoints are inserted in inferior.
2982 Throws exception on any error.
2983 A breakpoint that is already inserted won't be inserted
2984 again, so calling this function twice is safe. */
2985void
2986insert_breakpoints (void)
2987{
2988 struct breakpoint *bpt;
2989
2990 ALL_BREAKPOINTS (bpt)
2991 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2992 {
2993 struct watchpoint *w = (struct watchpoint *) bpt;
2994
2995 update_watchpoint (w, 0 /* don't reparse. */);
2996 }
74960c60 2997
04086b45
PA
2998 /* Updating watchpoints creates new locations, so update the global
2999 location list. Explicitly tell ugll to insert locations and
3000 ignore breakpoints_always_inserted_mode. */
3001 update_global_location_list (UGLL_INSERT);
74960c60
VP
3002}
3003
20388dd6
YQ
3004/* Invoke CALLBACK for each of bp_location. */
3005
3006void
3007iterate_over_bp_locations (walk_bp_location_callback callback)
3008{
3009 struct bp_location *loc, **loc_tmp;
3010
3011 ALL_BP_LOCATIONS (loc, loc_tmp)
3012 {
3013 callback (loc, NULL);
3014 }
3015}
3016
b775012e
LM
3017/* This is used when we need to synch breakpoint conditions between GDB and the
3018 target. It is the case with deleting and disabling of breakpoints when using
3019 always-inserted mode. */
3020
3021static void
3022update_inserted_breakpoint_locations (void)
3023{
3024 struct bp_location *bl, **blp_tmp;
3025 int error_flag = 0;
3026 int val = 0;
3027 int disabled_breaks = 0;
3028 int hw_breakpoint_error = 0;
dd61ec5c 3029 int hw_bp_details_reported = 0;
b775012e
LM
3030
3031 struct ui_file *tmp_error_stream = mem_fileopen ();
3032 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3033
3034 /* Explicitly mark the warning -- this will only be printed if
3035 there was an error. */
3036 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3037
3038 save_current_space_and_thread ();
3039
3040 ALL_BP_LOCATIONS (bl, blp_tmp)
3041 {
3042 /* We only want to update software breakpoints and hardware
3043 breakpoints. */
3044 if (!is_breakpoint (bl->owner))
3045 continue;
3046
3047 /* We only want to update locations that are already inserted
3048 and need updating. This is to avoid unwanted insertion during
3049 deletion of breakpoints. */
3050 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3051 continue;
3052
3053 switch_to_program_space_and_thread (bl->pspace);
3054
3055 /* For targets that support global breakpoints, there's no need
3056 to select an inferior to insert breakpoint to. In fact, even
3057 if we aren't attached to any process yet, we should still
3058 insert breakpoints. */
f5656ead 3059 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3060 && ptid_equal (inferior_ptid, null_ptid))
3061 continue;
3062
3063 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3064 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3065 if (val)
3066 error_flag = val;
3067 }
3068
3069 if (error_flag)
3070 {
3071 target_terminal_ours_for_output ();
3072 error_stream (tmp_error_stream);
3073 }
3074
3075 do_cleanups (cleanups);
3076}
3077
c30eee59 3078/* Used when starting or continuing the program. */
c906108c 3079
74960c60
VP
3080static void
3081insert_breakpoint_locations (void)
c906108c 3082{
a5606eee 3083 struct breakpoint *bpt;
35df4500 3084 struct bp_location *bl, **blp_tmp;
eacd795a 3085 int error_flag = 0;
c906108c 3086 int val = 0;
3fbb6ffa 3087 int disabled_breaks = 0;
81d0cc19 3088 int hw_breakpoint_error = 0;
dd61ec5c 3089 int hw_bp_error_explained_already = 0;
c906108c 3090
81d0cc19 3091 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3092 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3093
81d0cc19
GS
3094 /* Explicitly mark the warning -- this will only be printed if
3095 there was an error. */
3096 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3097
3098 save_current_space_and_thread ();
3099
35df4500 3100 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3101 {
b775012e 3102 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3103 continue;
3104
4a64f543
MS
3105 /* There is no point inserting thread-specific breakpoints if
3106 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3107 has BL->OWNER always non-NULL. */
35df4500
TJB
3108 if (bl->owner->thread != -1
3109 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3110 continue;
3111
35df4500 3112 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3113
3114 /* For targets that support global breakpoints, there's no need
3115 to select an inferior to insert breakpoint to. In fact, even
3116 if we aren't attached to any process yet, we should still
3117 insert breakpoints. */
f5656ead 3118 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3119 && ptid_equal (inferior_ptid, null_ptid))
3120 continue;
3121
3fbb6ffa 3122 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3123 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3124 if (val)
eacd795a 3125 error_flag = val;
879bfdc2 3126 }
c906108c 3127
4a64f543
MS
3128 /* If we failed to insert all locations of a watchpoint, remove
3129 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3130 ALL_BREAKPOINTS (bpt)
3131 {
3132 int some_failed = 0;
3133 struct bp_location *loc;
3134
3135 if (!is_hardware_watchpoint (bpt))
3136 continue;
3137
d6b74ac4 3138 if (!breakpoint_enabled (bpt))
a5606eee 3139 continue;
74960c60
VP
3140
3141 if (bpt->disposition == disp_del_at_next_stop)
3142 continue;
a5606eee
VP
3143
3144 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3145 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3146 {
3147 some_failed = 1;
3148 break;
3149 }
3150 if (some_failed)
3151 {
3152 for (loc = bpt->loc; loc; loc = loc->next)
3153 if (loc->inserted)
3154 remove_breakpoint (loc, mark_uninserted);
3155
3156 hw_breakpoint_error = 1;
3157 fprintf_unfiltered (tmp_error_stream,
3158 "Could not insert hardware watchpoint %d.\n",
3159 bpt->number);
eacd795a 3160 error_flag = -1;
a5606eee
VP
3161 }
3162 }
3163
eacd795a 3164 if (error_flag)
81d0cc19
GS
3165 {
3166 /* If a hardware breakpoint or watchpoint was inserted, add a
3167 message about possibly exhausted resources. */
dd61ec5c 3168 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3169 {
c6510018
MS
3170 fprintf_unfiltered (tmp_error_stream,
3171 "Could not insert hardware breakpoints:\n\
3172You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3173 }
81d0cc19
GS
3174 target_terminal_ours_for_output ();
3175 error_stream (tmp_error_stream);
3176 }
f7545552
TT
3177
3178 do_cleanups (cleanups);
c906108c
SS
3179}
3180
c30eee59
TJB
3181/* Used when the program stops.
3182 Returns zero if successful, or non-zero if there was a problem
3183 removing a breakpoint location. */
3184
c906108c 3185int
fba45db2 3186remove_breakpoints (void)
c906108c 3187{
35df4500 3188 struct bp_location *bl, **blp_tmp;
3a1bae8e 3189 int val = 0;
c906108c 3190
35df4500 3191 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3192 {
1e4d1764 3193 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3194 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3195 }
3a1bae8e 3196 return val;
c906108c
SS
3197}
3198
49fa26b0
PA
3199/* When a thread exits, remove breakpoints that are related to
3200 that thread. */
3201
3202static void
3203remove_threaded_breakpoints (struct thread_info *tp, int silent)
3204{
3205 struct breakpoint *b, *b_tmp;
3206
3207 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3208 {
96181529 3209 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3210 {
3211 b->disposition = disp_del_at_next_stop;
3212
3213 printf_filtered (_("\
46ecd527 3214Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3215 b->number, tp->num);
3216
3217 /* Hide it from the user. */
3218 b->number = 0;
3219 }
3220 }
3221}
3222
6c95b8df
PA
3223/* Remove breakpoints of process PID. */
3224
3225int
3226remove_breakpoints_pid (int pid)
3227{
35df4500 3228 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3229 int val;
3230 struct inferior *inf = find_inferior_pid (pid);
3231
35df4500 3232 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3233 {
35df4500 3234 if (bl->pspace != inf->pspace)
6c95b8df
PA
3235 continue;
3236
d3ce09f5
SS
3237 if (bl->owner->type == bp_dprintf)
3238 continue;
3239
35df4500 3240 if (bl->inserted)
6c95b8df 3241 {
35df4500 3242 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3243 if (val != 0)
3244 return val;
3245 }
3246 }
3247 return 0;
3248}
3249
c906108c 3250int
fba45db2 3251reattach_breakpoints (int pid)
c906108c 3252{
6c95b8df 3253 struct cleanup *old_chain;
35df4500 3254 struct bp_location *bl, **blp_tmp;
c906108c 3255 int val;
86b887df 3256 struct ui_file *tmp_error_stream;
dd61ec5c 3257 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3258 struct inferior *inf;
3259 struct thread_info *tp;
3260
3261 tp = any_live_thread_of_process (pid);
3262 if (tp == NULL)
3263 return 1;
3264
3265 inf = find_inferior_pid (pid);
3266 old_chain = save_inferior_ptid ();
3267
3268 inferior_ptid = tp->ptid;
a4954f26 3269
86b887df 3270 tmp_error_stream = mem_fileopen ();
a4954f26 3271 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3272
35df4500 3273 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3274 {
35df4500 3275 if (bl->pspace != inf->pspace)
6c95b8df
PA
3276 continue;
3277
35df4500 3278 if (bl->inserted)
c5aa993b 3279 {
35df4500 3280 bl->inserted = 0;
dd61ec5c 3281 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3282 if (val != 0)
3283 {
ce696e05 3284 do_cleanups (old_chain);
c5aa993b
JM
3285 return val;
3286 }
3287 }
3288 }
ce696e05 3289 do_cleanups (old_chain);
c906108c
SS
3290 return 0;
3291}
3292
e58b0e63
PA
3293static int internal_breakpoint_number = -1;
3294
84f4c1fe
PM
3295/* Set the breakpoint number of B, depending on the value of INTERNAL.
3296 If INTERNAL is non-zero, the breakpoint number will be populated
3297 from internal_breakpoint_number and that variable decremented.
e5dd4106 3298 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3299 breakpoint_count and that value incremented. Internal breakpoints
3300 do not set the internal var bpnum. */
3301static void
3302set_breakpoint_number (int internal, struct breakpoint *b)
3303{
3304 if (internal)
3305 b->number = internal_breakpoint_number--;
3306 else
3307 {
3308 set_breakpoint_count (breakpoint_count + 1);
3309 b->number = breakpoint_count;
3310 }
3311}
3312
e62c965a 3313static struct breakpoint *
a6d9a66e 3314create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3315 CORE_ADDR address, enum bptype type,
c0a91b2b 3316 const struct breakpoint_ops *ops)
e62c965a 3317{
e62c965a
PP
3318 struct symtab_and_line sal;
3319 struct breakpoint *b;
3320
4a64f543 3321 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3322
3323 sal.pc = address;
3324 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3325 sal.pspace = current_program_space;
e62c965a 3326
06edf0c0 3327 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3328 b->number = internal_breakpoint_number--;
3329 b->disposition = disp_donttouch;
3330
3331 return b;
3332}
3333
17450429
PP
3334static const char *const longjmp_names[] =
3335 {
3336 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3337 };
3338#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3339
3340/* Per-objfile data private to breakpoint.c. */
3341struct breakpoint_objfile_data
3342{
3343 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3344 struct bound_minimal_symbol overlay_msym;
17450429
PP
3345
3346 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3347 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3348
28106bc2
SDJ
3349 /* True if we have looked for longjmp probes. */
3350 int longjmp_searched;
3351
3352 /* SystemTap probe points for longjmp (if any). */
3353 VEC (probe_p) *longjmp_probes;
3354
17450429 3355 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3356 struct bound_minimal_symbol terminate_msym;
17450429
PP
3357
3358 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3359 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3360
3361 /* True if we have looked for exception probes. */
3362 int exception_searched;
3363
3364 /* SystemTap probe points for unwinding (if any). */
3365 VEC (probe_p) *exception_probes;
17450429
PP
3366};
3367
3368static const struct objfile_data *breakpoint_objfile_key;
3369
3370/* Minimal symbol not found sentinel. */
3371static struct minimal_symbol msym_not_found;
3372
3373/* Returns TRUE if MSYM point to the "not found" sentinel. */
3374
3375static int
3376msym_not_found_p (const struct minimal_symbol *msym)
3377{
3378 return msym == &msym_not_found;
3379}
3380
3381/* Return per-objfile data needed by breakpoint.c.
3382 Allocate the data if necessary. */
3383
3384static struct breakpoint_objfile_data *
3385get_breakpoint_objfile_data (struct objfile *objfile)
3386{
3387 struct breakpoint_objfile_data *bp_objfile_data;
3388
3389 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3390 if (bp_objfile_data == NULL)
3391 {
3392 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3393 sizeof (*bp_objfile_data));
3394
3395 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3396 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3397 }
3398 return bp_objfile_data;
3399}
3400
28106bc2
SDJ
3401static void
3402free_breakpoint_probes (struct objfile *obj, void *data)
3403{
3404 struct breakpoint_objfile_data *bp_objfile_data = data;
3405
3406 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3407 VEC_free (probe_p, bp_objfile_data->exception_probes);
3408}
3409
e62c965a 3410static void
af02033e 3411create_overlay_event_breakpoint (void)
e62c965a 3412{
69de3c6a 3413 struct objfile *objfile;
af02033e 3414 const char *const func_name = "_ovly_debug_event";
e62c965a 3415
69de3c6a
PP
3416 ALL_OBJFILES (objfile)
3417 {
3418 struct breakpoint *b;
17450429
PP
3419 struct breakpoint_objfile_data *bp_objfile_data;
3420 CORE_ADDR addr;
69de3c6a 3421
17450429
PP
3422 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3423
3b7344d5 3424 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3425 continue;
3426
3b7344d5 3427 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3428 {
3b7344d5 3429 struct bound_minimal_symbol m;
17450429
PP
3430
3431 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3432 if (m.minsym == NULL)
17450429
PP
3433 {
3434 /* Avoid future lookups in this objfile. */
3b7344d5 3435 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3436 continue;
3437 }
3438 bp_objfile_data->overlay_msym = m;
3439 }
e62c965a 3440
77e371c0 3441 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3442 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3443 bp_overlay_event,
3444 &internal_breakpoint_ops);
69de3c6a 3445 b->addr_string = xstrdup (func_name);
e62c965a 3446
69de3c6a
PP
3447 if (overlay_debugging == ovly_auto)
3448 {
3449 b->enable_state = bp_enabled;
3450 overlay_events_enabled = 1;
3451 }
3452 else
3453 {
3454 b->enable_state = bp_disabled;
3455 overlay_events_enabled = 0;
3456 }
e62c965a 3457 }
44702360 3458 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3459}
3460
0fd8e87f 3461static void
af02033e 3462create_longjmp_master_breakpoint (void)
0fd8e87f 3463{
6c95b8df 3464 struct program_space *pspace;
6c95b8df
PA
3465 struct cleanup *old_chain;
3466
3467 old_chain = save_current_program_space ();
0fd8e87f 3468
6c95b8df 3469 ALL_PSPACES (pspace)
af02033e
PP
3470 {
3471 struct objfile *objfile;
3472
3473 set_current_program_space (pspace);
3474
3475 ALL_OBJFILES (objfile)
0fd8e87f 3476 {
af02033e
PP
3477 int i;
3478 struct gdbarch *gdbarch;
17450429 3479 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3480
af02033e 3481 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3482
17450429
PP
3483 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3484
28106bc2
SDJ
3485 if (!bp_objfile_data->longjmp_searched)
3486 {
25f9533e
SDJ
3487 VEC (probe_p) *ret;
3488
3489 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3490 if (ret != NULL)
3491 {
3492 /* We are only interested in checking one element. */
3493 struct probe *p = VEC_index (probe_p, ret, 0);
3494
3495 if (!can_evaluate_probe_arguments (p))
3496 {
3497 /* We cannot use the probe interface here, because it does
3498 not know how to evaluate arguments. */
3499 VEC_free (probe_p, ret);
3500 ret = NULL;
3501 }
3502 }
3503 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3504 bp_objfile_data->longjmp_searched = 1;
3505 }
3506
3507 if (bp_objfile_data->longjmp_probes != NULL)
3508 {
3509 int i;
3510 struct probe *probe;
3511 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3512
3513 for (i = 0;
3514 VEC_iterate (probe_p,
3515 bp_objfile_data->longjmp_probes,
3516 i, probe);
3517 ++i)
3518 {
3519 struct breakpoint *b;
3520
729662a5
TT
3521 b = create_internal_breakpoint (gdbarch,
3522 get_probe_address (probe,
3523 objfile),
28106bc2
SDJ
3524 bp_longjmp_master,
3525 &internal_breakpoint_ops);
3526 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3527 b->enable_state = bp_disabled;
3528 }
3529
3530 continue;
3531 }
3532
0569175e
TSD
3533 if (!gdbarch_get_longjmp_target_p (gdbarch))
3534 continue;
3535
17450429 3536 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3537 {
3538 struct breakpoint *b;
af02033e 3539 const char *func_name;
17450429 3540 CORE_ADDR addr;
6c95b8df 3541
3b7344d5 3542 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3543 continue;
0fd8e87f 3544
17450429 3545 func_name = longjmp_names[i];
3b7344d5 3546 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3547 {
3b7344d5 3548 struct bound_minimal_symbol m;
17450429
PP
3549
3550 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3551 if (m.minsym == NULL)
17450429
PP
3552 {
3553 /* Prevent future lookups in this objfile. */
3b7344d5 3554 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3555 continue;
3556 }
3557 bp_objfile_data->longjmp_msym[i] = m;
3558 }
3559
77e371c0 3560 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3561 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3562 &internal_breakpoint_ops);
af02033e
PP
3563 b->addr_string = xstrdup (func_name);
3564 b->enable_state = bp_disabled;
3565 }
0fd8e87f 3566 }
af02033e 3567 }
44702360 3568 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3569
3570 do_cleanups (old_chain);
0fd8e87f
UW
3571}
3572
af02033e 3573/* Create a master std::terminate breakpoint. */
aa7d318d 3574static void
af02033e 3575create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3576{
3577 struct program_space *pspace;
aa7d318d 3578 struct cleanup *old_chain;
af02033e 3579 const char *const func_name = "std::terminate()";
aa7d318d
TT
3580
3581 old_chain = save_current_program_space ();
3582
3583 ALL_PSPACES (pspace)
17450429
PP
3584 {
3585 struct objfile *objfile;
3586 CORE_ADDR addr;
3587
3588 set_current_program_space (pspace);
3589
aa7d318d
TT
3590 ALL_OBJFILES (objfile)
3591 {
3592 struct breakpoint *b;
17450429 3593 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3594
17450429 3595 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3596
3b7344d5 3597 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3598 continue;
3599
3b7344d5 3600 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3601 {
3b7344d5 3602 struct bound_minimal_symbol m;
17450429
PP
3603
3604 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3605 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3606 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3607 {
3608 /* Prevent future lookups in this objfile. */
3b7344d5 3609 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3610 continue;
3611 }
3612 bp_objfile_data->terminate_msym = m;
3613 }
aa7d318d 3614
77e371c0 3615 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3616 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3617 bp_std_terminate_master,
3618 &internal_breakpoint_ops);
aa7d318d
TT
3619 b->addr_string = xstrdup (func_name);
3620 b->enable_state = bp_disabled;
3621 }
17450429
PP
3622 }
3623
44702360 3624 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3625
3626 do_cleanups (old_chain);
3627}
3628
186c406b
TT
3629/* Install a master breakpoint on the unwinder's debug hook. */
3630
70221824 3631static void
186c406b
TT
3632create_exception_master_breakpoint (void)
3633{
3634 struct objfile *objfile;
17450429 3635 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3636
3637 ALL_OBJFILES (objfile)
3638 {
17450429
PP
3639 struct breakpoint *b;
3640 struct gdbarch *gdbarch;
3641 struct breakpoint_objfile_data *bp_objfile_data;
3642 CORE_ADDR addr;
3643
3644 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3645
28106bc2
SDJ
3646 /* We prefer the SystemTap probe point if it exists. */
3647 if (!bp_objfile_data->exception_searched)
3648 {
25f9533e
SDJ
3649 VEC (probe_p) *ret;
3650
3651 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3652
3653 if (ret != NULL)
3654 {
3655 /* We are only interested in checking one element. */
3656 struct probe *p = VEC_index (probe_p, ret, 0);
3657
3658 if (!can_evaluate_probe_arguments (p))
3659 {
3660 /* We cannot use the probe interface here, because it does
3661 not know how to evaluate arguments. */
3662 VEC_free (probe_p, ret);
3663 ret = NULL;
3664 }
3665 }
3666 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3667 bp_objfile_data->exception_searched = 1;
3668 }
3669
3670 if (bp_objfile_data->exception_probes != NULL)
3671 {
3672 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3673 int i;
3674 struct probe *probe;
3675
3676 for (i = 0;
3677 VEC_iterate (probe_p,
3678 bp_objfile_data->exception_probes,
3679 i, probe);
3680 ++i)
3681 {
3682 struct breakpoint *b;
3683
729662a5
TT
3684 b = create_internal_breakpoint (gdbarch,
3685 get_probe_address (probe,
3686 objfile),
28106bc2
SDJ
3687 bp_exception_master,
3688 &internal_breakpoint_ops);
3689 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3690 b->enable_state = bp_disabled;
3691 }
3692
3693 continue;
3694 }
3695
3696 /* Otherwise, try the hook function. */
3697
3b7344d5 3698 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3699 continue;
3700
3701 gdbarch = get_objfile_arch (objfile);
186c406b 3702
3b7344d5 3703 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3704 {
3b7344d5 3705 struct bound_minimal_symbol debug_hook;
186c406b 3706
17450429 3707 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3708 if (debug_hook.minsym == NULL)
17450429 3709 {
3b7344d5 3710 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3711 continue;
3712 }
3713
3714 bp_objfile_data->exception_msym = debug_hook;
186c406b 3715 }
17450429 3716
77e371c0 3717 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3718 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3719 &current_target);
06edf0c0
PA
3720 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3721 &internal_breakpoint_ops);
17450429
PP
3722 b->addr_string = xstrdup (func_name);
3723 b->enable_state = bp_disabled;
186c406b
TT
3724 }
3725
44702360 3726 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3727}
3728
c906108c 3729void
fba45db2 3730update_breakpoints_after_exec (void)
c906108c 3731{
35df4500 3732 struct breakpoint *b, *b_tmp;
876fa593 3733 struct bp_location *bploc, **bplocp_tmp;
c906108c 3734
25b22b0a
PA
3735 /* We're about to delete breakpoints from GDB's lists. If the
3736 INSERTED flag is true, GDB will try to lift the breakpoints by
3737 writing the breakpoints' "shadow contents" back into memory. The
3738 "shadow contents" are NOT valid after an exec, so GDB should not
3739 do that. Instead, the target is responsible from marking
3740 breakpoints out as soon as it detects an exec. We don't do that
3741 here instead, because there may be other attempts to delete
3742 breakpoints after detecting an exec and before reaching here. */
876fa593 3743 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3744 if (bploc->pspace == current_program_space)
3745 gdb_assert (!bploc->inserted);
c906108c 3746
35df4500 3747 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3748 {
6c95b8df
PA
3749 if (b->pspace != current_program_space)
3750 continue;
3751
4a64f543 3752 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3753 if (b->type == bp_shlib_event)
3754 {
3755 delete_breakpoint (b);
3756 continue;
3757 }
c906108c 3758
4a64f543 3759 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3760 if (b->type == bp_jit_event)
3761 {
3762 delete_breakpoint (b);
3763 continue;
3764 }
3765
1900040c 3766 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3767 as must overlay event and longjmp master breakpoints. */
3768 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3769 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3770 || b->type == bp_exception_master)
c4093a6a
JM
3771 {
3772 delete_breakpoint (b);
3773 continue;
3774 }
3775
4a64f543 3776 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3777 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3778 {
3779 delete_breakpoint (b);
3780 continue;
3781 }
3782
611c83ae
PA
3783 /* Longjmp and longjmp-resume breakpoints are also meaningless
3784 after an exec. */
186c406b 3785 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3786 || b->type == bp_longjmp_call_dummy
186c406b 3787 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3788 {
3789 delete_breakpoint (b);
3790 continue;
3791 }
3792
ce78b96d
JB
3793 if (b->type == bp_catchpoint)
3794 {
3795 /* For now, none of the bp_catchpoint breakpoints need to
3796 do anything at this point. In the future, if some of
3797 the catchpoints need to something, we will need to add
3798 a new method, and call this method from here. */
3799 continue;
3800 }
3801
c5aa993b
JM
3802 /* bp_finish is a special case. The only way we ought to be able
3803 to see one of these when an exec() has happened, is if the user
3804 caught a vfork, and then said "finish". Ordinarily a finish just
3805 carries them to the call-site of the current callee, by setting
3806 a temporary bp there and resuming. But in this case, the finish
3807 will carry them entirely through the vfork & exec.
3808
3809 We don't want to allow a bp_finish to remain inserted now. But
3810 we can't safely delete it, 'cause finish_command has a handle to
3811 the bp on a bpstat, and will later want to delete it. There's a
3812 chance (and I've seen it happen) that if we delete the bp_finish
3813 here, that its storage will get reused by the time finish_command
3814 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3815 We really must allow finish_command to delete a bp_finish.
3816
e5dd4106 3817 In the absence of a general solution for the "how do we know
53a5351d
JM
3818 it's safe to delete something others may have handles to?"
3819 problem, what we'll do here is just uninsert the bp_finish, and
3820 let finish_command delete it.
3821
3822 (We know the bp_finish is "doomed" in the sense that it's
3823 momentary, and will be deleted as soon as finish_command sees
3824 the inferior stopped. So it doesn't matter that the bp's
3825 address is probably bogus in the new a.out, unlike e.g., the
3826 solib breakpoints.) */
c5aa993b 3827
c5aa993b
JM
3828 if (b->type == bp_finish)
3829 {
3830 continue;
3831 }
3832
3833 /* Without a symbolic address, we have little hope of the
3834 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3835 a.out. */
c5aa993b
JM
3836 if (b->addr_string == NULL)
3837 {
3838 delete_breakpoint (b);
3839 continue;
3840 }
c5aa993b 3841 }
1900040c 3842 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3843 create_overlay_event_breakpoint ();
3844 create_longjmp_master_breakpoint ();
3845 create_std_terminate_master_breakpoint ();
186c406b 3846 create_exception_master_breakpoint ();
c906108c
SS
3847}
3848
3849int
d80ee84f 3850detach_breakpoints (ptid_t ptid)
c906108c 3851{
35df4500 3852 struct bp_location *bl, **blp_tmp;
3a1bae8e 3853 int val = 0;
ce696e05 3854 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3855 struct inferior *inf = current_inferior ();
c5aa993b 3856
dfd4cc63 3857 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3858 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3859
6c95b8df 3860 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3861 inferior_ptid = ptid;
35df4500 3862 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3863 {
35df4500 3864 if (bl->pspace != inf->pspace)
6c95b8df
PA
3865 continue;
3866
bd9673a4
PW
3867 /* This function must physically remove breakpoints locations
3868 from the specified ptid, without modifying the breakpoint
3869 package's state. Locations of type bp_loc_other are only
3870 maintained at GDB side. So, there is no need to remove
3871 these bp_loc_other locations. Moreover, removing these
3872 would modify the breakpoint package's state. */
3873 if (bl->loc_type == bp_loc_other)
3874 continue;
3875
35df4500
TJB
3876 if (bl->inserted)
3877 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3878 }
d03285ec
UW
3879
3880 /* Detach single-step breakpoints as well. */
3881 detach_single_step_breakpoints ();
3882
ce696e05 3883 do_cleanups (old_chain);
3a1bae8e 3884 return val;
c906108c
SS
3885}
3886
35df4500 3887/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3888 Note that this is used to detach breakpoints from a child fork.
3889 When we get here, the child isn't in the inferior list, and neither
3890 do we have objects to represent its address space --- we should
35df4500 3891 *not* look at bl->pspace->aspace here. */
6c95b8df 3892
c906108c 3893static int
35df4500 3894remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3895{
3896 int val;
c5aa993b 3897
35df4500
TJB
3898 /* BL is never in moribund_locations by our callers. */
3899 gdb_assert (bl->owner != NULL);
2bdf28a0 3900
35df4500 3901 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3902 /* Permanent breakpoints cannot be inserted or removed. */
3903 return 0;
3904
74960c60
VP
3905 /* The type of none suggests that owner is actually deleted.
3906 This should not ever happen. */
35df4500 3907 gdb_assert (bl->owner->type != bp_none);
0bde7532 3908
35df4500
TJB
3909 if (bl->loc_type == bp_loc_software_breakpoint
3910 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3911 {
c02f5703
MS
3912 /* "Normal" instruction breakpoint: either the standard
3913 trap-instruction bp (bp_breakpoint), or a
3914 bp_hardware_breakpoint. */
3915
3916 /* First check to see if we have to handle an overlay. */
3917 if (overlay_debugging == ovly_off
35df4500
TJB
3918 || bl->section == NULL
3919 || !(section_is_overlay (bl->section)))
c02f5703
MS
3920 {
3921 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3922
3923 /* If we're trying to uninsert a memory breakpoint that we
3924 know is set in a dynamic object that is marked
3925 shlib_disabled, then either the dynamic object was
3926 removed with "remove-symbol-file" or with
3927 "nosharedlibrary". In the former case, we don't know
3928 whether another dynamic object might have loaded over the
3929 breakpoint's address -- the user might well let us know
3930 about it next with add-symbol-file (the whole point of
d03de421 3931 add-symbol-file is letting the user manually maintain a
08351840
PA
3932 list of dynamically loaded objects). If we have the
3933 breakpoint's shadow memory, that is, this is a software
3934 breakpoint managed by GDB, check whether the breakpoint
3935 is still inserted in memory, to avoid overwriting wrong
3936 code with stale saved shadow contents. Note that HW
3937 breakpoints don't have shadow memory, as they're
3938 implemented using a mechanism that is not dependent on
3939 being able to modify the target's memory, and as such
3940 they should always be removed. */
3941 if (bl->shlib_disabled
3942 && bl->target_info.shadow_len != 0
3943 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3944 val = 0;
3945 else
3946 val = bl->owner->ops->remove_location (bl);
c02f5703 3947 }
c906108c
SS
3948 else
3949 {
4a64f543 3950 /* This breakpoint is in an overlay section.
c02f5703
MS
3951 Did we set a breakpoint at the LMA? */
3952 if (!overlay_events_enabled)
3953 {
3954 /* Yes -- overlay event support is not active, so we
3955 should have set a breakpoint at the LMA. Remove it.
3956 */
c02f5703
MS
3957 /* Ignore any failures: if the LMA is in ROM, we will
3958 have already warned when we failed to insert it. */
35df4500
TJB
3959 if (bl->loc_type == bp_loc_hardware_breakpoint)
3960 target_remove_hw_breakpoint (bl->gdbarch,
3961 &bl->overlay_target_info);
c02f5703 3962 else
35df4500
TJB
3963 target_remove_breakpoint (bl->gdbarch,
3964 &bl->overlay_target_info);
c02f5703
MS
3965 }
3966 /* Did we set a breakpoint at the VMA?
3967 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3968 if (bl->inserted)
c906108c 3969 {
c02f5703
MS
3970 /* Yes -- remove it. Previously we did not bother to
3971 remove the breakpoint if the section had been
3972 unmapped, but let's not rely on that being safe. We
3973 don't know what the overlay manager might do. */
aa67235e
UW
3974
3975 /* However, we should remove *software* breakpoints only
3976 if the section is still mapped, or else we overwrite
3977 wrong code with the saved shadow contents. */
348d480f
PA
3978 if (bl->loc_type == bp_loc_hardware_breakpoint
3979 || section_is_mapped (bl->section))
3980 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3981 else
3982 val = 0;
c906108c 3983 }
c02f5703
MS
3984 else
3985 {
3986 /* No -- not inserted, so no need to remove. No error. */
3987 val = 0;
3988 }
c906108c 3989 }
879d1e6b 3990
08351840
PA
3991 /* In some cases, we might not be able to remove a breakpoint in
3992 a shared library that has already been removed, but we have
3993 not yet processed the shlib unload event. Similarly for an
3994 unloaded add-symbol-file object - the user might not yet have
3995 had the chance to remove-symbol-file it. shlib_disabled will
3996 be set if the library/object has already been removed, but
3997 the breakpoint hasn't been uninserted yet, e.g., after
3998 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3999 always-inserted mode. */
076855f9 4000 if (val
08351840
PA
4001 && (bl->loc_type == bp_loc_software_breakpoint
4002 && (bl->shlib_disabled
4003 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4004 || shared_objfile_contains_address_p (bl->pspace,
4005 bl->address))))
879d1e6b
UW
4006 val = 0;
4007
c906108c
SS
4008 if (val)
4009 return val;
35df4500 4010 bl->inserted = (is == mark_inserted);
c906108c 4011 }
35df4500 4012 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4013 {
77b06cd7
TJB
4014 gdb_assert (bl->owner->ops != NULL
4015 && bl->owner->ops->remove_location != NULL);
4016
35df4500 4017 bl->inserted = (is == mark_inserted);
77b06cd7 4018 bl->owner->ops->remove_location (bl);
2e70b7b9 4019
c906108c 4020 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4021 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4022 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4023 bl->owner->number);
c906108c 4024 }
35df4500
TJB
4025 else if (bl->owner->type == bp_catchpoint
4026 && breakpoint_enabled (bl->owner)
4027 && !bl->duplicate)
ce78b96d 4028 {
77b06cd7
TJB
4029 gdb_assert (bl->owner->ops != NULL
4030 && bl->owner->ops->remove_location != NULL);
ce78b96d 4031
77b06cd7 4032 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4033 if (val)
4034 return val;
77b06cd7 4035
35df4500 4036 bl->inserted = (is == mark_inserted);
ce78b96d 4037 }
c906108c
SS
4038
4039 return 0;
4040}
4041
6c95b8df 4042static int
35df4500 4043remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4044{
4045 int ret;
4046 struct cleanup *old_chain;
4047
35df4500
TJB
4048 /* BL is never in moribund_locations by our callers. */
4049 gdb_assert (bl->owner != NULL);
2bdf28a0 4050
35df4500 4051 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4052 /* Permanent breakpoints cannot be inserted or removed. */
4053 return 0;
4054
4055 /* The type of none suggests that owner is actually deleted.
4056 This should not ever happen. */
35df4500 4057 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4058
4059 old_chain = save_current_space_and_thread ();
4060
35df4500 4061 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4062
35df4500 4063 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4064
4065 do_cleanups (old_chain);
4066 return ret;
4067}
4068
c906108c
SS
4069/* Clear the "inserted" flag in all breakpoints. */
4070
25b22b0a 4071void
fba45db2 4072mark_breakpoints_out (void)
c906108c 4073{
35df4500 4074 struct bp_location *bl, **blp_tmp;
c906108c 4075
35df4500
TJB
4076 ALL_BP_LOCATIONS (bl, blp_tmp)
4077 if (bl->pspace == current_program_space)
4078 bl->inserted = 0;
c906108c
SS
4079}
4080
53a5351d
JM
4081/* Clear the "inserted" flag in all breakpoints and delete any
4082 breakpoints which should go away between runs of the program.
c906108c
SS
4083
4084 Plus other such housekeeping that has to be done for breakpoints
4085 between runs.
4086
53a5351d
JM
4087 Note: this function gets called at the end of a run (by
4088 generic_mourn_inferior) and when a run begins (by
4a64f543 4089 init_wait_for_inferior). */
c906108c
SS
4090
4091
4092
4093void
fba45db2 4094breakpoint_init_inferior (enum inf_context context)
c906108c 4095{
35df4500
TJB
4096 struct breakpoint *b, *b_tmp;
4097 struct bp_location *bl, **blp_tmp;
1c5cfe86 4098 int ix;
6c95b8df 4099 struct program_space *pspace = current_program_space;
c906108c 4100
50c71eaf
PA
4101 /* If breakpoint locations are shared across processes, then there's
4102 nothing to do. */
f5656ead 4103 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4104 return;
4105
35df4500 4106 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4107 {
35df4500
TJB
4108 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4109 if (bl->pspace == pspace
4110 && bl->owner->enable_state != bp_permanent)
4111 bl->inserted = 0;
6c95b8df 4112 }
075f6582 4113
35df4500 4114 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4115 {
6c95b8df
PA
4116 if (b->loc && b->loc->pspace != pspace)
4117 continue;
4118
c5aa993b
JM
4119 switch (b->type)
4120 {
4121 case bp_call_dummy:
e2e4d78b 4122 case bp_longjmp_call_dummy:
c906108c 4123
c5aa993b 4124 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4125 cause problems when the inferior is rerun, so we better get
4126 rid of it. */
4127
4128 case bp_watchpoint_scope:
4129
4130 /* Also get rid of scope breakpoints. */
4131
4132 case bp_shlib_event:
4133
4134 /* Also remove solib event breakpoints. Their addresses may
4135 have changed since the last time we ran the program.
4136 Actually we may now be debugging against different target;
4137 and so the solib backend that installed this breakpoint may
4138 not be used in by the target. E.g.,
4139
4140 (gdb) file prog-linux
4141 (gdb) run # native linux target
4142 ...
4143 (gdb) kill
4144 (gdb) file prog-win.exe
4145 (gdb) tar rem :9999 # remote Windows gdbserver.
4146 */
c906108c 4147
f59f708a
PA
4148 case bp_step_resume:
4149
4150 /* Also remove step-resume breakpoints. */
4151
c5aa993b
JM
4152 delete_breakpoint (b);
4153 break;
c906108c 4154
c5aa993b
JM
4155 case bp_watchpoint:
4156 case bp_hardware_watchpoint:
4157 case bp_read_watchpoint:
4158 case bp_access_watchpoint:
3a5c3e22
PA
4159 {
4160 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4161
3a5c3e22
PA
4162 /* Likewise for watchpoints on local expressions. */
4163 if (w->exp_valid_block != NULL)
4164 delete_breakpoint (b);
4165 else if (context == inf_starting)
4166 {
4167 /* Reset val field to force reread of starting value in
4168 insert_breakpoints. */
4169 if (w->val)
4170 value_free (w->val);
4171 w->val = NULL;
4172 w->val_valid = 0;
c860120c 4173 }
3a5c3e22 4174 }
c5aa993b
JM
4175 break;
4176 default:
c5aa993b
JM
4177 break;
4178 }
4179 }
1c5cfe86
PA
4180
4181 /* Get rid of the moribund locations. */
35df4500
TJB
4182 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4183 decref_bp_location (&bl);
1c5cfe86 4184 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4185}
4186
6c95b8df
PA
4187/* These functions concern about actual breakpoints inserted in the
4188 target --- to e.g. check if we need to do decr_pc adjustment or if
4189 we need to hop over the bkpt --- so we check for address space
4190 match, not program space. */
4191
c2c6d25f
JM
4192/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4193 exists at PC. It returns ordinary_breakpoint_here if it's an
4194 ordinary breakpoint, or permanent_breakpoint_here if it's a
4195 permanent breakpoint.
4196 - When continuing from a location with an ordinary breakpoint, we
4197 actually single step once before calling insert_breakpoints.
e5dd4106 4198 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4199 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4200 the target, to advance the PC past the breakpoint. */
c906108c 4201
c2c6d25f 4202enum breakpoint_here
6c95b8df 4203breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4204{
35df4500 4205 struct bp_location *bl, **blp_tmp;
c2c6d25f 4206 int any_breakpoint_here = 0;
c906108c 4207
35df4500 4208 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4209 {
35df4500
TJB
4210 if (bl->loc_type != bp_loc_software_breakpoint
4211 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4212 continue;
4213
f1310107 4214 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4215 if ((breakpoint_enabled (bl->owner)
4216 || bl->owner->enable_state == bp_permanent)
f1310107 4217 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4218 {
4219 if (overlay_debugging
35df4500
TJB
4220 && section_is_overlay (bl->section)
4221 && !section_is_mapped (bl->section))
075f6582 4222 continue; /* unmapped overlay -- can't be a match */
35df4500 4223 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4224 return permanent_breakpoint_here;
4225 else
4226 any_breakpoint_here = 1;
4227 }
4228 }
c906108c 4229
c2c6d25f 4230 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4231}
4232
1c5cfe86
PA
4233/* Return true if there's a moribund breakpoint at PC. */
4234
4235int
6c95b8df 4236moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4237{
4238 struct bp_location *loc;
4239 int ix;
4240
4241 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4242 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4243 return 1;
4244
4245 return 0;
4246}
c2c6d25f 4247
c36b740a 4248/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4249 inserted using regular breakpoint_chain / bp_location array
4250 mechanism. This does not check for single-step breakpoints, which
4251 are inserted and removed using direct target manipulation. */
c906108c
SS
4252
4253int
4a64f543
MS
4254regular_breakpoint_inserted_here_p (struct address_space *aspace,
4255 CORE_ADDR pc)
c906108c 4256{
35df4500 4257 struct bp_location *bl, **blp_tmp;
c906108c 4258
35df4500 4259 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4260 {
35df4500
TJB
4261 if (bl->loc_type != bp_loc_software_breakpoint
4262 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4263 continue;
4264
35df4500 4265 if (bl->inserted
f1310107 4266 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4267 {
4268 if (overlay_debugging
35df4500
TJB
4269 && section_is_overlay (bl->section)
4270 && !section_is_mapped (bl->section))
075f6582
DJ
4271 continue; /* unmapped overlay -- can't be a match */
4272 else
4273 return 1;
4274 }
c5aa993b 4275 }
c36b740a
VP
4276 return 0;
4277}
4278
4279/* Returns non-zero iff there's either regular breakpoint
4280 or a single step breakpoint inserted at PC. */
4281
4282int
6c95b8df 4283breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4284{
6c95b8df 4285 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4286 return 1;
c906108c 4287
6c95b8df 4288 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4289 return 1;
4290
c906108c
SS
4291 return 0;
4292}
4293
ef370185
JB
4294/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4295 software breakpoint inserted at PC. */
4fa8626c 4296
ef370185
JB
4297static struct bp_location *
4298find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4299 CORE_ADDR pc)
4fa8626c 4300{
35df4500 4301 struct bp_location *bl, **blp_tmp;
4fa8626c 4302
35df4500 4303 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4304 {
35df4500 4305 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4306 continue;
4307
35df4500
TJB
4308 if (bl->inserted
4309 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4310 aspace, pc))
4fa8626c
DJ
4311 {
4312 if (overlay_debugging
35df4500
TJB
4313 && section_is_overlay (bl->section)
4314 && !section_is_mapped (bl->section))
4fa8626c
DJ
4315 continue; /* unmapped overlay -- can't be a match */
4316 else
ef370185 4317 return bl;
4fa8626c
DJ
4318 }
4319 }
4320
ef370185
JB
4321 return NULL;
4322}
4323
4324/* This function returns non-zero iff there is a software breakpoint
4325 inserted at PC. */
4326
4327int
4328software_breakpoint_inserted_here_p (struct address_space *aspace,
4329 CORE_ADDR pc)
4330{
4331 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4332 return 1;
4333
1aafd4da 4334 /* Also check for software single-step breakpoints. */
6c95b8df 4335 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4336 return 1;
4337
4fa8626c
DJ
4338 return 0;
4339}
4340
9093389c
PA
4341int
4342hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4343 CORE_ADDR addr, ULONGEST len)
4344{
4345 struct breakpoint *bpt;
4346
4347 ALL_BREAKPOINTS (bpt)
4348 {
4349 struct bp_location *loc;
4350
4351 if (bpt->type != bp_hardware_watchpoint
4352 && bpt->type != bp_access_watchpoint)
4353 continue;
4354
4355 if (!breakpoint_enabled (bpt))
4356 continue;
4357
4358 for (loc = bpt->loc; loc; loc = loc->next)
4359 if (loc->pspace->aspace == aspace && loc->inserted)
4360 {
4361 CORE_ADDR l, h;
4362
4363 /* Check for intersection. */
4364 l = max (loc->address, addr);
4365 h = min (loc->address + loc->length, addr + len);
4366 if (l < h)
4367 return 1;
4368 }
4369 }
4370 return 0;
4371}
4372
075f6582
DJ
4373/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4374 PC is valid for process/thread PTID. */
c906108c
SS
4375
4376int
6c95b8df
PA
4377breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4378 ptid_t ptid)
c906108c 4379{
35df4500 4380 struct bp_location *bl, **blp_tmp;
4a306c9a 4381 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4382 int thread = -1;
4a306c9a 4383 int task = 0;
a6f1cd96 4384
35df4500 4385 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4386 {
35df4500
TJB
4387 if (bl->loc_type != bp_loc_software_breakpoint
4388 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4389 continue;
4390
35df4500
TJB
4391 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4392 if (!breakpoint_enabled (bl->owner)
4393 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4394 continue;
4395
f1310107 4396 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4397 continue;
4398
35df4500 4399 if (bl->owner->thread != -1)
075f6582 4400 {
a6f1cd96
JB
4401 /* This is a thread-specific breakpoint. Check that ptid
4402 matches that thread. If thread hasn't been computed yet,
4403 it is now time to do so. */
4404 if (thread == -1)
4405 thread = pid_to_thread_id (ptid);
35df4500 4406 if (bl->owner->thread != thread)
a6f1cd96 4407 continue;
075f6582 4408 }
a6f1cd96 4409
35df4500 4410 if (bl->owner->task != 0)
4a306c9a
JB
4411 {
4412 /* This is a task-specific breakpoint. Check that ptid
4413 matches that task. If task hasn't been computed yet,
4414 it is now time to do so. */
4415 if (task == 0)
4416 task = ada_get_task_number (ptid);
35df4500 4417 if (bl->owner->task != task)
4a306c9a
JB
4418 continue;
4419 }
4420
a6f1cd96 4421 if (overlay_debugging
35df4500
TJB
4422 && section_is_overlay (bl->section)
4423 && !section_is_mapped (bl->section))
a6f1cd96
JB
4424 continue; /* unmapped overlay -- can't be a match */
4425
4426 return 1;
c5aa993b 4427 }
c906108c
SS
4428
4429 return 0;
4430}
c906108c 4431\f
c5aa993b 4432
c906108c
SS
4433/* bpstat stuff. External routines' interfaces are documented
4434 in breakpoint.h. */
4435
4436int
c326b90e 4437is_catchpoint (struct breakpoint *ep)
c906108c 4438{
533be4dd 4439 return (ep->type == bp_catchpoint);
c906108c
SS
4440}
4441
f431efe5
PA
4442/* Frees any storage that is part of a bpstat. Does not walk the
4443 'next' chain. */
4444
4445static void
198757a8
VP
4446bpstat_free (bpstat bs)
4447{
4448 if (bs->old_val != NULL)
4449 value_free (bs->old_val);
9add0f1b 4450 decref_counted_command_line (&bs->commands);
f431efe5 4451 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4452 xfree (bs);
4453}
4454
c906108c
SS
4455/* Clear a bpstat so that it says we are not at any breakpoint.
4456 Also free any storage that is part of a bpstat. */
4457
4458void
fba45db2 4459bpstat_clear (bpstat *bsp)
c906108c
SS
4460{
4461 bpstat p;
4462 bpstat q;
4463
4464 if (bsp == 0)
4465 return;
4466 p = *bsp;
4467 while (p != NULL)
4468 {
4469 q = p->next;
198757a8 4470 bpstat_free (p);
c906108c
SS
4471 p = q;
4472 }
4473 *bsp = NULL;
4474}
4475
4476/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4477 is part of the bpstat is copied as well. */
4478
4479bpstat
fba45db2 4480bpstat_copy (bpstat bs)
c906108c
SS
4481{
4482 bpstat p = NULL;
4483 bpstat tmp;
4484 bpstat retval = NULL;
4485
4486 if (bs == NULL)
4487 return bs;
4488
4489 for (; bs != NULL; bs = bs->next)
4490 {
4491 tmp = (bpstat) xmalloc (sizeof (*tmp));
4492 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4493 incref_counted_command_line (tmp->commands);
f431efe5 4494 incref_bp_location (tmp->bp_location_at);
31cc81e9 4495 if (bs->old_val != NULL)
3c3185ac
JK
4496 {
4497 tmp->old_val = value_copy (bs->old_val);
4498 release_value (tmp->old_val);
4499 }
31cc81e9 4500
c906108c
SS
4501 if (p == NULL)
4502 /* This is the first thing in the chain. */
4503 retval = tmp;
4504 else
4505 p->next = tmp;
4506 p = tmp;
4507 }
4508 p->next = NULL;
4509 return retval;
4510}
4511
4a64f543 4512/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4513
4514bpstat
fba45db2 4515bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4516{
c5aa993b
JM
4517 if (bsp == NULL)
4518 return NULL;
c906108c 4519
c5aa993b
JM
4520 for (; bsp != NULL; bsp = bsp->next)
4521 {
f431efe5 4522 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4523 return bsp;
4524 }
c906108c
SS
4525 return NULL;
4526}
4527
ab04a2af
TT
4528/* See breakpoint.h. */
4529
47591c29 4530int
427cd150 4531bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4532{
ab04a2af
TT
4533 for (; bsp != NULL; bsp = bsp->next)
4534 {
427cd150
TT
4535 if (bsp->breakpoint_at == NULL)
4536 {
4537 /* A moribund location can never explain a signal other than
4538 GDB_SIGNAL_TRAP. */
4539 if (sig == GDB_SIGNAL_TRAP)
47591c29 4540 return 1;
427cd150
TT
4541 }
4542 else
47591c29
PA
4543 {
4544 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4545 sig))
4546 return 1;
4547 }
ab04a2af
TT
4548 }
4549
47591c29 4550 return 0;
ab04a2af
TT
4551}
4552
4a64f543
MS
4553/* Put in *NUM the breakpoint number of the first breakpoint we are
4554 stopped at. *BSP upon return is a bpstat which points to the
4555 remaining breakpoints stopped at (but which is not guaranteed to be
4556 good for anything but further calls to bpstat_num).
4557
8671a17b
PA
4558 Return 0 if passed a bpstat which does not indicate any breakpoints.
4559 Return -1 if stopped at a breakpoint that has been deleted since
4560 we set it.
4561 Return 1 otherwise. */
c906108c
SS
4562
4563int
8671a17b 4564bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4565{
4566 struct breakpoint *b;
4567
4568 if ((*bsp) == NULL)
4569 return 0; /* No more breakpoint values */
8671a17b 4570
4a64f543
MS
4571 /* We assume we'll never have several bpstats that correspond to a
4572 single breakpoint -- otherwise, this function might return the
4573 same number more than once and this will look ugly. */
f431efe5 4574 b = (*bsp)->breakpoint_at;
8671a17b
PA
4575 *bsp = (*bsp)->next;
4576 if (b == NULL)
4577 return -1; /* breakpoint that's been deleted since */
4578
4579 *num = b->number; /* We have its number */
4580 return 1;
c906108c
SS
4581}
4582
e93ca019 4583/* See breakpoint.h. */
c906108c
SS
4584
4585void
e93ca019 4586bpstat_clear_actions (void)
c906108c 4587{
e93ca019
JK
4588 struct thread_info *tp;
4589 bpstat bs;
4590
4591 if (ptid_equal (inferior_ptid, null_ptid))
4592 return;
4593
4594 tp = find_thread_ptid (inferior_ptid);
4595 if (tp == NULL)
4596 return;
4597
4598 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4599 {
9add0f1b 4600 decref_counted_command_line (&bs->commands);
abf85f46 4601
c906108c
SS
4602 if (bs->old_val != NULL)
4603 {
4604 value_free (bs->old_val);
4605 bs->old_val = NULL;
4606 }
4607 }
4608}
4609
f3b1572e
PA
4610/* Called when a command is about to proceed the inferior. */
4611
4612static void
4613breakpoint_about_to_proceed (void)
4614{
4615 if (!ptid_equal (inferior_ptid, null_ptid))
4616 {
4617 struct thread_info *tp = inferior_thread ();
4618
4619 /* Allow inferior function calls in breakpoint commands to not
4620 interrupt the command list. When the call finishes
4621 successfully, the inferior will be standing at the same
4622 breakpoint as if nothing happened. */
16c381f0 4623 if (tp->control.in_infcall)
f3b1572e
PA
4624 return;
4625 }
4626
4627 breakpoint_proceeded = 1;
4628}
4629
4a64f543
MS
4630/* Stub for cleaning up our state if we error-out of a breakpoint
4631 command. */
c906108c 4632static void
4efb68b1 4633cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4634{
4635 executing_breakpoint_commands = 0;
4636}
4637
abf85f46
JK
4638/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4639 or its equivalent. */
4640
4641static int
4642command_line_is_silent (struct command_line *cmd)
4643{
4644 return cmd && (strcmp ("silent", cmd->line) == 0
4645 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4646}
4647
4a64f543
MS
4648/* Execute all the commands associated with all the breakpoints at
4649 this location. Any of these commands could cause the process to
4650 proceed beyond this point, etc. We look out for such changes by
4651 checking the global "breakpoint_proceeded" after each command.
c906108c 4652
347bddb7
PA
4653 Returns true if a breakpoint command resumed the inferior. In that
4654 case, it is the caller's responsibility to recall it again with the
4655 bpstat of the current thread. */
4656
4657static int
4658bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4659{
4660 bpstat bs;
4661 struct cleanup *old_chain;
347bddb7 4662 int again = 0;
c906108c
SS
4663
4664 /* Avoid endless recursion if a `source' command is contained
4665 in bs->commands. */
4666 if (executing_breakpoint_commands)
347bddb7 4667 return 0;
c906108c
SS
4668
4669 executing_breakpoint_commands = 1;
4670 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4671
cf6c5ffb
TT
4672 prevent_dont_repeat ();
4673
4a64f543 4674 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4675 bs = *bsp;
4676
4677 breakpoint_proceeded = 0;
4678 for (; bs != NULL; bs = bs->next)
4679 {
9add0f1b 4680 struct counted_command_line *ccmd;
6c50ab1c
JB
4681 struct command_line *cmd;
4682 struct cleanup *this_cmd_tree_chain;
4683
4684 /* Take ownership of the BSP's command tree, if it has one.
4685
4686 The command tree could legitimately contain commands like
4687 'step' and 'next', which call clear_proceed_status, which
4688 frees stop_bpstat's command tree. To make sure this doesn't
4689 free the tree we're executing out from under us, we need to
4690 take ownership of the tree ourselves. Since a given bpstat's
4691 commands are only executed once, we don't need to copy it; we
4692 can clear the pointer in the bpstat, and make sure we free
4693 the tree when we're done. */
9add0f1b
TT
4694 ccmd = bs->commands;
4695 bs->commands = NULL;
abf85f46
JK
4696 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4697 cmd = ccmd ? ccmd->commands : NULL;
4698 if (command_line_is_silent (cmd))
4699 {
4700 /* The action has been already done by bpstat_stop_status. */
4701 cmd = cmd->next;
4702 }
6c50ab1c 4703
c906108c
SS
4704 while (cmd != NULL)
4705 {
4706 execute_control_command (cmd);
4707
4708 if (breakpoint_proceeded)
4709 break;
4710 else
4711 cmd = cmd->next;
4712 }
6c50ab1c
JB
4713
4714 /* We can free this command tree now. */
4715 do_cleanups (this_cmd_tree_chain);
4716
c906108c 4717 if (breakpoint_proceeded)
32c1e744
VP
4718 {
4719 if (target_can_async_p ())
347bddb7
PA
4720 /* If we are in async mode, then the target might be still
4721 running, not stopped at any breakpoint, so nothing for
4722 us to do here -- just return to the event loop. */
4723 ;
32c1e744
VP
4724 else
4725 /* In sync mode, when execute_control_command returns
4726 we're already standing on the next breakpoint.
347bddb7
PA
4727 Breakpoint commands for that stop were not run, since
4728 execute_command does not run breakpoint commands --
4729 only command_line_handler does, but that one is not
4730 involved in execution of breakpoint commands. So, we
4731 can now execute breakpoint commands. It should be
4732 noted that making execute_command do bpstat actions is
4733 not an option -- in this case we'll have recursive
4734 invocation of bpstat for each breakpoint with a
4735 command, and can easily blow up GDB stack. Instead, we
4736 return true, which will trigger the caller to recall us
4737 with the new stop_bpstat. */
4738 again = 1;
4739 break;
32c1e744 4740 }
c906108c 4741 }
c2b8ed2c 4742 do_cleanups (old_chain);
347bddb7
PA
4743 return again;
4744}
4745
4746void
4747bpstat_do_actions (void)
4748{
353d1d73
JK
4749 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4750
347bddb7
PA
4751 /* Do any commands attached to breakpoint we are stopped at. */
4752 while (!ptid_equal (inferior_ptid, null_ptid)
4753 && target_has_execution
4754 && !is_exited (inferior_ptid)
4755 && !is_executing (inferior_ptid))
4756 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4757 and only return when it is stopped at the next breakpoint, we
4758 keep doing breakpoint actions until it returns false to
4759 indicate the inferior was not resumed. */
16c381f0 4760 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4761 break;
353d1d73
JK
4762
4763 discard_cleanups (cleanup_if_error);
c906108c
SS
4764}
4765
fa4727a6
DJ
4766/* Print out the (old or new) value associated with a watchpoint. */
4767
4768static void
4769watchpoint_value_print (struct value *val, struct ui_file *stream)
4770{
4771 if (val == NULL)
4772 fprintf_unfiltered (stream, _("<unreadable>"));
4773 else
79a45b7d
TT
4774 {
4775 struct value_print_options opts;
4776 get_user_print_options (&opts);
4777 value_print (val, stream, &opts);
4778 }
fa4727a6
DJ
4779}
4780
e514a9d6 4781/* Generic routine for printing messages indicating why we
4a64f543 4782 stopped. The behavior of this function depends on the value
e514a9d6
JM
4783 'print_it' in the bpstat structure. Under some circumstances we
4784 may decide not to print anything here and delegate the task to
4a64f543 4785 normal_stop(). */
e514a9d6
JM
4786
4787static enum print_stop_action
4788print_bp_stop_message (bpstat bs)
4789{
4790 switch (bs->print_it)
4791 {
4792 case print_it_noop:
4a64f543 4793 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4794 return PRINT_UNKNOWN;
4795 break;
4796
4797 case print_it_done:
4798 /* We still want to print the frame, but we already printed the
4a64f543 4799 relevant messages. */
e514a9d6
JM
4800 return PRINT_SRC_AND_LOC;
4801 break;
4802
4803 case print_it_normal:
4f8d1dc6 4804 {
f431efe5
PA
4805 struct breakpoint *b = bs->breakpoint_at;
4806
1a6a67de
TJB
4807 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4808 which has since been deleted. */
4809 if (b == NULL)
4810 return PRINT_UNKNOWN;
4811
348d480f
PA
4812 /* Normal case. Call the breakpoint's print_it method. */
4813 return b->ops->print_it (bs);
4f8d1dc6 4814 }
348d480f 4815 break;
3086aeae 4816
e514a9d6 4817 default:
8e65ff28 4818 internal_error (__FILE__, __LINE__,
e2e0b3e5 4819 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4820 break;
c906108c 4821 }
c906108c
SS
4822}
4823
edcc5120
TT
4824/* A helper function that prints a shared library stopped event. */
4825
4826static void
4827print_solib_event (int is_catchpoint)
4828{
4829 int any_deleted
4830 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4831 int any_added
4832 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4833
4834 if (!is_catchpoint)
4835 {
4836 if (any_added || any_deleted)
4837 ui_out_text (current_uiout,
4838 _("Stopped due to shared library event:\n"));
4839 else
4840 ui_out_text (current_uiout,
4841 _("Stopped due to shared library event (no "
4842 "libraries added or removed)\n"));
4843 }
4844
4845 if (ui_out_is_mi_like_p (current_uiout))
4846 ui_out_field_string (current_uiout, "reason",
4847 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4848
4849 if (any_deleted)
4850 {
4851 struct cleanup *cleanup;
4852 char *name;
4853 int ix;
4854
4855 ui_out_text (current_uiout, _(" Inferior unloaded "));
4856 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4857 "removed");
4858 for (ix = 0;
4859 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4860 ix, name);
4861 ++ix)
4862 {
4863 if (ix > 0)
4864 ui_out_text (current_uiout, " ");
4865 ui_out_field_string (current_uiout, "library", name);
4866 ui_out_text (current_uiout, "\n");
4867 }
4868
4869 do_cleanups (cleanup);
4870 }
4871
4872 if (any_added)
4873 {
4874 struct so_list *iter;
4875 int ix;
4876 struct cleanup *cleanup;
4877
4878 ui_out_text (current_uiout, _(" Inferior loaded "));
4879 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4880 "added");
4881 for (ix = 0;
4882 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4883 ix, iter);
4884 ++ix)
4885 {
4886 if (ix > 0)
4887 ui_out_text (current_uiout, " ");
4888 ui_out_field_string (current_uiout, "library", iter->so_name);
4889 ui_out_text (current_uiout, "\n");
4890 }
4891
4892 do_cleanups (cleanup);
4893 }
4894}
4895
e514a9d6
JM
4896/* Print a message indicating what happened. This is called from
4897 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4898 list - a list of the eventpoints that caused this stop. KIND is
4899 the target_waitkind for the stopping event. This
e514a9d6
JM
4900 routine calls the generic print routine for printing a message
4901 about reasons for stopping. This will print (for example) the
4902 "Breakpoint n," part of the output. The return value of this
4903 routine is one of:
c906108c 4904
4a64f543 4905 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4906 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4907 code to print the location. An example is
c5aa993b
JM
4908 "Breakpoint 1, " which should be followed by
4909 the location.
917317f4 4910 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4911 to also print the location part of the message.
4912 An example is the catch/throw messages, which
4a64f543 4913 don't require a location appended to the end.
917317f4 4914 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4915 further info to be printed. */
c906108c 4916
917317f4 4917enum print_stop_action
36dfb11c 4918bpstat_print (bpstat bs, int kind)
c906108c
SS
4919{
4920 int val;
c5aa993b 4921
c906108c 4922 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4923 (Currently all watchpoints go on the bpstat whether hit or not.
4924 That probably could (should) be changed, provided care is taken
c906108c 4925 with respect to bpstat_explains_signal). */
e514a9d6
JM
4926 for (; bs; bs = bs->next)
4927 {
4928 val = print_bp_stop_message (bs);
4929 if (val == PRINT_SRC_ONLY
4930 || val == PRINT_SRC_AND_LOC
4931 || val == PRINT_NOTHING)
4932 return val;
4933 }
c906108c 4934
36dfb11c
TT
4935 /* If we had hit a shared library event breakpoint,
4936 print_bp_stop_message would print out this message. If we hit an
4937 OS-level shared library event, do the same thing. */
4938 if (kind == TARGET_WAITKIND_LOADED)
4939 {
edcc5120 4940 print_solib_event (0);
36dfb11c
TT
4941 return PRINT_NOTHING;
4942 }
4943
e514a9d6 4944 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4945 with and nothing was printed. */
917317f4 4946 return PRINT_UNKNOWN;
c906108c
SS
4947}
4948
c42bd95a
DE
4949/* Evaluate the expression EXP and return 1 if value is zero.
4950 This returns the inverse of the condition because it is called
4951 from catch_errors which returns 0 if an exception happened, and if an
4952 exception happens we want execution to stop.
4a64f543 4953 The argument is a "struct expression *" that has been cast to a
c42bd95a 4954 "void *" to make it pass through catch_errors. */
c906108c
SS
4955
4956static int
4efb68b1 4957breakpoint_cond_eval (void *exp)
c906108c 4958{
278cd55f 4959 struct value *mark = value_mark ();
c5aa993b 4960 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4961
c906108c
SS
4962 value_free_to_mark (mark);
4963 return i;
4964}
4965
5760d0ab 4966/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4967
4968static bpstat
5760d0ab 4969bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4970{
4971 bpstat bs;
4972
4973 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4974 bs->next = NULL;
4975 **bs_link_pointer = bs;
4976 *bs_link_pointer = &bs->next;
f431efe5
PA
4977 bs->breakpoint_at = bl->owner;
4978 bs->bp_location_at = bl;
4979 incref_bp_location (bl);
c906108c
SS
4980 /* If the condition is false, etc., don't do the commands. */
4981 bs->commands = NULL;
4982 bs->old_val = NULL;
4983 bs->print_it = print_it_normal;
4984 return bs;
4985}
4986\f
d983da9c
DJ
4987/* The target has stopped with waitstatus WS. Check if any hardware
4988 watchpoints have triggered, according to the target. */
4989
4990int
4991watchpoints_triggered (struct target_waitstatus *ws)
4992{
d92524f1 4993 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4994 CORE_ADDR addr;
4995 struct breakpoint *b;
4996
4997 if (!stopped_by_watchpoint)
4998 {
4999 /* We were not stopped by a watchpoint. Mark all watchpoints
5000 as not triggered. */
5001 ALL_BREAKPOINTS (b)
cc60f2e3 5002 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5003 {
5004 struct watchpoint *w = (struct watchpoint *) b;
5005
5006 w->watchpoint_triggered = watch_triggered_no;
5007 }
d983da9c
DJ
5008
5009 return 0;
5010 }
5011
5012 if (!target_stopped_data_address (&current_target, &addr))
5013 {
5014 /* We were stopped by a watchpoint, but we don't know where.
5015 Mark all watchpoints as unknown. */
5016 ALL_BREAKPOINTS (b)
cc60f2e3 5017 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5018 {
5019 struct watchpoint *w = (struct watchpoint *) b;
5020
5021 w->watchpoint_triggered = watch_triggered_unknown;
5022 }
d983da9c 5023
3c4797ba 5024 return 1;
d983da9c
DJ
5025 }
5026
5027 /* The target could report the data address. Mark watchpoints
5028 affected by this data address as triggered, and all others as not
5029 triggered. */
5030
5031 ALL_BREAKPOINTS (b)
cc60f2e3 5032 if (is_hardware_watchpoint (b))
d983da9c 5033 {
3a5c3e22 5034 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5035 struct bp_location *loc;
d983da9c 5036
3a5c3e22 5037 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5038 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5039 {
3a5c3e22 5040 if (is_masked_watchpoint (b))
9c06b0b4 5041 {
3a5c3e22
PA
5042 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5043 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5044
5045 if (newaddr == start)
5046 {
3a5c3e22 5047 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5048 break;
5049 }
5050 }
5051 /* Exact match not required. Within range is sufficient. */
5052 else if (target_watchpoint_addr_within_range (&current_target,
5053 addr, loc->address,
5054 loc->length))
5055 {
3a5c3e22 5056 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5057 break;
5058 }
5059 }
d983da9c
DJ
5060 }
5061
5062 return 1;
5063}
5064
c906108c
SS
5065/* Possible return values for watchpoint_check (this can't be an enum
5066 because of check_errors). */
5067/* The watchpoint has been deleted. */
5068#define WP_DELETED 1
5069/* The value has changed. */
5070#define WP_VALUE_CHANGED 2
5071/* The value has not changed. */
5072#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5073/* Ignore this watchpoint, no matter if the value changed or not. */
5074#define WP_IGNORE 4
c906108c
SS
5075
5076#define BP_TEMPFLAG 1
5077#define BP_HARDWAREFLAG 2
5078
4a64f543
MS
5079/* Evaluate watchpoint condition expression and check if its value
5080 changed.
553e4c11
JB
5081
5082 P should be a pointer to struct bpstat, but is defined as a void *
5083 in order for this function to be usable with catch_errors. */
c906108c
SS
5084
5085static int
4efb68b1 5086watchpoint_check (void *p)
c906108c
SS
5087{
5088 bpstat bs = (bpstat) p;
3a5c3e22 5089 struct watchpoint *b;
c906108c
SS
5090 struct frame_info *fr;
5091 int within_current_scope;
5092
f431efe5 5093 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5094 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5095 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5096
f6bc2008
PA
5097 /* If this is a local watchpoint, we only want to check if the
5098 watchpoint frame is in scope if the current thread is the thread
5099 that was used to create the watchpoint. */
5100 if (!watchpoint_in_thread_scope (b))
60e1c644 5101 return WP_IGNORE;
f6bc2008 5102
c906108c
SS
5103 if (b->exp_valid_block == NULL)
5104 within_current_scope = 1;
5105 else
5106 {
edb3359d
DJ
5107 struct frame_info *frame = get_current_frame ();
5108 struct gdbarch *frame_arch = get_frame_arch (frame);
5109 CORE_ADDR frame_pc = get_frame_pc (frame);
5110
4a64f543
MS
5111 /* in_function_epilogue_p() returns a non-zero value if we're
5112 still in the function but the stack frame has already been
5113 invalidated. Since we can't rely on the values of local
5114 variables after the stack has been destroyed, we are treating
5115 the watchpoint in that state as `not changed' without further
5116 checking. Don't mark watchpoints as changed if the current
5117 frame is in an epilogue - even if they are in some other
5118 frame, our view of the stack is likely to be wrong and
5119 frame_find_by_id could error out. */
a0f49112 5120 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5121 return WP_IGNORE;
a0f49112 5122
101dcfbe 5123 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5124 within_current_scope = (fr != NULL);
69fbadd5
DJ
5125
5126 /* If we've gotten confused in the unwinder, we might have
5127 returned a frame that can't describe this variable. */
edb3359d
DJ
5128 if (within_current_scope)
5129 {
5130 struct symbol *function;
5131
5132 function = get_frame_function (fr);
5133 if (function == NULL
5134 || !contained_in (b->exp_valid_block,
5135 SYMBOL_BLOCK_VALUE (function)))
5136 within_current_scope = 0;
5137 }
69fbadd5 5138
edb3359d 5139 if (within_current_scope)
c906108c
SS
5140 /* If we end up stopping, the current frame will get selected
5141 in normal_stop. So this call to select_frame won't affect
5142 the user. */
0f7d239c 5143 select_frame (fr);
c906108c 5144 }
c5aa993b 5145
c906108c
SS
5146 if (within_current_scope)
5147 {
4a64f543
MS
5148 /* We use value_{,free_to_}mark because it could be a *long*
5149 time before we return to the command level and call
5150 free_all_values. We can't call free_all_values because we
5151 might be in the middle of evaluating a function call. */
c906108c 5152
0cf6dd15 5153 int pc = 0;
9c06b0b4 5154 struct value *mark;
fa4727a6
DJ
5155 struct value *new_val;
5156
3a5c3e22 5157 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5158 /* Since we don't know the exact trigger address (from
5159 stopped_data_address), just tell the user we've triggered
5160 a mask watchpoint. */
5161 return WP_VALUE_CHANGED;
5162
5163 mark = value_mark ();
3a1115a0 5164 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5165
bb9d5f81
PP
5166 if (b->val_bitsize != 0)
5167 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5168
4a64f543
MS
5169 /* We use value_equal_contents instead of value_equal because
5170 the latter coerces an array to a pointer, thus comparing just
5171 the address of the array instead of its contents. This is
5172 not what we want. */
fa4727a6 5173 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5174 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5175 {
fa4727a6
DJ
5176 if (new_val != NULL)
5177 {
5178 release_value (new_val);
5179 value_free_to_mark (mark);
5180 }
c906108c
SS
5181 bs->old_val = b->val;
5182 b->val = new_val;
fa4727a6 5183 b->val_valid = 1;
c906108c
SS
5184 return WP_VALUE_CHANGED;
5185 }
5186 else
5187 {
60e1c644 5188 /* Nothing changed. */
c906108c 5189 value_free_to_mark (mark);
c906108c
SS
5190 return WP_VALUE_NOT_CHANGED;
5191 }
5192 }
5193 else
5194 {
79a45e25
PA
5195 struct ui_out *uiout = current_uiout;
5196
c906108c 5197 /* This seems like the only logical thing to do because
c5aa993b
JM
5198 if we temporarily ignored the watchpoint, then when
5199 we reenter the block in which it is valid it contains
5200 garbage (in the case of a function, it may have two
5201 garbage values, one before and one after the prologue).
5202 So we can't even detect the first assignment to it and
5203 watch after that (since the garbage may or may not equal
5204 the first value assigned). */
348d480f
PA
5205 /* We print all the stop information in
5206 breakpoint_ops->print_it, but in this case, by the time we
5207 call breakpoint_ops->print_it this bp will be deleted
5208 already. So we have no choice but print the information
5209 here. */
9dc5e2a9 5210 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5211 ui_out_field_string
5212 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5213 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5214 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5215 ui_out_text (uiout,
5216 " deleted because the program has left the block in\n\
8b93c638 5217which its expression is valid.\n");
4ce44c66 5218
cdac0397 5219 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5220 decref_counted_command_line (&b->base.commands);
d0fb5eae 5221 watchpoint_del_at_next_stop (b);
c906108c
SS
5222
5223 return WP_DELETED;
5224 }
5225}
5226
18a18393 5227/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5228 breakpoint location BL. This function does not check if we should
5229 stop, only if BL explains the stop. */
5230
18a18393 5231static int
6c95b8df 5232bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5233 struct address_space *aspace, CORE_ADDR bp_addr,
5234 const struct target_waitstatus *ws)
18a18393
VP
5235{
5236 struct breakpoint *b = bl->owner;
5237
348d480f 5238 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5239 gdb_assert (b != NULL);
5240
09ac7c10 5241 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5242}
5243
3a5c3e22
PA
5244/* Determine if the watched values have actually changed, and we
5245 should stop. If not, set BS->stop to 0. */
5246
18a18393
VP
5247static void
5248bpstat_check_watchpoint (bpstat bs)
5249{
2bdf28a0 5250 const struct bp_location *bl;
3a5c3e22 5251 struct watchpoint *b;
2bdf28a0
JK
5252
5253 /* BS is built for existing struct breakpoint. */
f431efe5 5254 bl = bs->bp_location_at;
2bdf28a0 5255 gdb_assert (bl != NULL);
3a5c3e22 5256 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5257 gdb_assert (b != NULL);
18a18393 5258
18a18393 5259 {
18a18393
VP
5260 int must_check_value = 0;
5261
3a5c3e22 5262 if (b->base.type == bp_watchpoint)
18a18393
VP
5263 /* For a software watchpoint, we must always check the
5264 watched value. */
5265 must_check_value = 1;
5266 else if (b->watchpoint_triggered == watch_triggered_yes)
5267 /* We have a hardware watchpoint (read, write, or access)
5268 and the target earlier reported an address watched by
5269 this watchpoint. */
5270 must_check_value = 1;
5271 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5272 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5273 /* We were stopped by a hardware watchpoint, but the target could
5274 not report the data address. We must check the watchpoint's
5275 value. Access and read watchpoints are out of luck; without
5276 a data address, we can't figure it out. */
5277 must_check_value = 1;
3a5c3e22 5278
18a18393
VP
5279 if (must_check_value)
5280 {
3e43a32a
MS
5281 char *message
5282 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5283 b->base.number);
18a18393
VP
5284 struct cleanup *cleanups = make_cleanup (xfree, message);
5285 int e = catch_errors (watchpoint_check, bs, message,
5286 RETURN_MASK_ALL);
5287 do_cleanups (cleanups);
5288 switch (e)
5289 {
5290 case WP_DELETED:
5291 /* We've already printed what needs to be printed. */
5292 bs->print_it = print_it_done;
5293 /* Stop. */
5294 break;
60e1c644
PA
5295 case WP_IGNORE:
5296 bs->print_it = print_it_noop;
5297 bs->stop = 0;
5298 break;
18a18393 5299 case WP_VALUE_CHANGED:
3a5c3e22 5300 if (b->base.type == bp_read_watchpoint)
18a18393 5301 {
85d721b8
PA
5302 /* There are two cases to consider here:
5303
4a64f543 5304 1. We're watching the triggered memory for reads.
85d721b8
PA
5305 In that case, trust the target, and always report
5306 the watchpoint hit to the user. Even though
5307 reads don't cause value changes, the value may
5308 have changed since the last time it was read, and
5309 since we're not trapping writes, we will not see
5310 those, and as such we should ignore our notion of
5311 old value.
5312
4a64f543 5313 2. We're watching the triggered memory for both
85d721b8
PA
5314 reads and writes. There are two ways this may
5315 happen:
5316
4a64f543 5317 2.1. This is a target that can't break on data
85d721b8
PA
5318 reads only, but can break on accesses (reads or
5319 writes), such as e.g., x86. We detect this case
5320 at the time we try to insert read watchpoints.
5321
4a64f543 5322 2.2. Otherwise, the target supports read
85d721b8
PA
5323 watchpoints, but, the user set an access or write
5324 watchpoint watching the same memory as this read
5325 watchpoint.
5326
5327 If we're watching memory writes as well as reads,
5328 ignore watchpoint hits when we find that the
5329 value hasn't changed, as reads don't cause
5330 changes. This still gives false positives when
5331 the program writes the same value to memory as
5332 what there was already in memory (we will confuse
5333 it for a read), but it's much better than
5334 nothing. */
5335
5336 int other_write_watchpoint = 0;
5337
5338 if (bl->watchpoint_type == hw_read)
5339 {
5340 struct breakpoint *other_b;
5341
5342 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5343 if (other_b->type == bp_hardware_watchpoint
5344 || other_b->type == bp_access_watchpoint)
85d721b8 5345 {
3a5c3e22
PA
5346 struct watchpoint *other_w =
5347 (struct watchpoint *) other_b;
5348
5349 if (other_w->watchpoint_triggered
5350 == watch_triggered_yes)
5351 {
5352 other_write_watchpoint = 1;
5353 break;
5354 }
85d721b8
PA
5355 }
5356 }
5357
5358 if (other_write_watchpoint
5359 || bl->watchpoint_type == hw_access)
5360 {
5361 /* We're watching the same memory for writes,
5362 and the value changed since the last time we
5363 updated it, so this trap must be for a write.
5364 Ignore it. */
5365 bs->print_it = print_it_noop;
5366 bs->stop = 0;
5367 }
18a18393
VP
5368 }
5369 break;
5370 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5371 if (b->base.type == bp_hardware_watchpoint
5372 || b->base.type == bp_watchpoint)
18a18393
VP
5373 {
5374 /* Don't stop: write watchpoints shouldn't fire if
5375 the value hasn't changed. */
5376 bs->print_it = print_it_noop;
5377 bs->stop = 0;
5378 }
5379 /* Stop. */
5380 break;
5381 default:
5382 /* Can't happen. */
5383 case 0:
5384 /* Error from catch_errors. */
3a5c3e22 5385 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5386 watchpoint_del_at_next_stop (b);
18a18393
VP
5387 /* We've already printed what needs to be printed. */
5388 bs->print_it = print_it_done;
5389 break;
5390 }
5391 }
5392 else /* must_check_value == 0 */
5393 {
5394 /* This is a case where some watchpoint(s) triggered, but
5395 not at the address of this watchpoint, or else no
5396 watchpoint triggered after all. So don't print
5397 anything for this watchpoint. */
5398 bs->print_it = print_it_noop;
5399 bs->stop = 0;
5400 }
5401 }
5402}
5403
7d4df6a4
DE
5404/* For breakpoints that are currently marked as telling gdb to stop,
5405 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5406 of breakpoint referred to by BS. If we should not stop for this
5407 breakpoint, set BS->stop to 0. */
f431efe5 5408
18a18393
VP
5409static void
5410bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5411{
2bdf28a0
JK
5412 const struct bp_location *bl;
5413 struct breakpoint *b;
7d4df6a4
DE
5414 int value_is_zero = 0;
5415 struct expression *cond;
5416
5417 gdb_assert (bs->stop);
2bdf28a0
JK
5418
5419 /* BS is built for existing struct breakpoint. */
f431efe5 5420 bl = bs->bp_location_at;
2bdf28a0 5421 gdb_assert (bl != NULL);
f431efe5 5422 b = bs->breakpoint_at;
2bdf28a0 5423 gdb_assert (b != NULL);
18a18393 5424
b775012e
LM
5425 /* Even if the target evaluated the condition on its end and notified GDB, we
5426 need to do so again since GDB does not know if we stopped due to a
5427 breakpoint or a single step breakpoint. */
5428
18a18393 5429 if (frame_id_p (b->frame_id)
edb3359d 5430 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5431 {
7d4df6a4
DE
5432 bs->stop = 0;
5433 return;
5434 }
60e1c644 5435
12ab52e9
PA
5436 /* If this is a thread/task-specific breakpoint, don't waste cpu
5437 evaluating the condition if this isn't the specified
5438 thread/task. */
5439 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5440 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5441
6c1b0f7b
DE
5442 {
5443 bs->stop = 0;
5444 return;
5445 }
5446
6dddc817
DE
5447 /* Evaluate extension language breakpoints that have a "stop" method
5448 implemented. */
5449 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5450
7d4df6a4
DE
5451 if (is_watchpoint (b))
5452 {
5453 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5454
7d4df6a4
DE
5455 cond = w->cond_exp;
5456 }
5457 else
5458 cond = bl->cond;
60e1c644 5459
7d4df6a4
DE
5460 if (cond && b->disposition != disp_del_at_next_stop)
5461 {
5462 int within_current_scope = 1;
5463 struct watchpoint * w;
60e1c644 5464
7d4df6a4
DE
5465 /* We use value_mark and value_free_to_mark because it could
5466 be a long time before we return to the command level and
5467 call free_all_values. We can't call free_all_values
5468 because we might be in the middle of evaluating a
5469 function call. */
5470 struct value *mark = value_mark ();
5471
5472 if (is_watchpoint (b))
5473 w = (struct watchpoint *) b;
5474 else
5475 w = NULL;
5476
5477 /* Need to select the frame, with all that implies so that
5478 the conditions will have the right context. Because we
5479 use the frame, we will not see an inlined function's
5480 variables when we arrive at a breakpoint at the start
5481 of the inlined function; the current frame will be the
5482 call site. */
5483 if (w == NULL || w->cond_exp_valid_block == NULL)
5484 select_frame (get_current_frame ());
5485 else
18a18393 5486 {
7d4df6a4
DE
5487 struct frame_info *frame;
5488
5489 /* For local watchpoint expressions, which particular
5490 instance of a local is being watched matters, so we
5491 keep track of the frame to evaluate the expression
5492 in. To evaluate the condition however, it doesn't
5493 really matter which instantiation of the function
5494 where the condition makes sense triggers the
5495 watchpoint. This allows an expression like "watch
5496 global if q > 10" set in `func', catch writes to
5497 global on all threads that call `func', or catch
5498 writes on all recursive calls of `func' by a single
5499 thread. We simply always evaluate the condition in
5500 the innermost frame that's executing where it makes
5501 sense to evaluate the condition. It seems
5502 intuitive. */
5503 frame = block_innermost_frame (w->cond_exp_valid_block);
5504 if (frame != NULL)
5505 select_frame (frame);
5506 else
5507 within_current_scope = 0;
18a18393 5508 }
7d4df6a4
DE
5509 if (within_current_scope)
5510 value_is_zero
5511 = catch_errors (breakpoint_cond_eval, cond,
5512 "Error in testing breakpoint condition:\n",
5513 RETURN_MASK_ALL);
5514 else
18a18393 5515 {
7d4df6a4
DE
5516 warning (_("Watchpoint condition cannot be tested "
5517 "in the current scope"));
5518 /* If we failed to set the right context for this
5519 watchpoint, unconditionally report it. */
5520 value_is_zero = 0;
18a18393 5521 }
7d4df6a4
DE
5522 /* FIXME-someday, should give breakpoint #. */
5523 value_free_to_mark (mark);
18a18393 5524 }
7d4df6a4
DE
5525
5526 if (cond && value_is_zero)
5527 {
5528 bs->stop = 0;
5529 }
7d4df6a4
DE
5530 else if (b->ignore_count > 0)
5531 {
5532 b->ignore_count--;
5533 bs->stop = 0;
5534 /* Increase the hit count even though we don't stop. */
5535 ++(b->hit_count);
5536 observer_notify_breakpoint_modified (b);
5537 }
18a18393
VP
5538}
5539
5540
9709f61c 5541/* Get a bpstat associated with having just stopped at address
d983da9c 5542 BP_ADDR in thread PTID.
c906108c 5543
d983da9c 5544 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5545 don't understand this stop. Result is a chain of bpstat's such
5546 that:
c906108c 5547
c5aa993b 5548 if we don't understand the stop, the result is a null pointer.
c906108c 5549
c5aa993b 5550 if we understand why we stopped, the result is not null.
c906108c 5551
c5aa993b
JM
5552 Each element of the chain refers to a particular breakpoint or
5553 watchpoint at which we have stopped. (We may have stopped for
5554 several reasons concurrently.)
c906108c 5555
c5aa993b
JM
5556 Each element of the chain has valid next, breakpoint_at,
5557 commands, FIXME??? fields. */
c906108c
SS
5558
5559bpstat
6c95b8df 5560bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5561 CORE_ADDR bp_addr, ptid_t ptid,
5562 const struct target_waitstatus *ws)
c906108c 5563{
0d381245 5564 struct breakpoint *b = NULL;
afe38095 5565 struct bp_location *bl;
20874c92 5566 struct bp_location *loc;
5760d0ab
JK
5567 /* First item of allocated bpstat's. */
5568 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5569 /* Pointer to the last thing in the chain currently. */
5760d0ab 5570 bpstat bs;
20874c92 5571 int ix;
429374b8 5572 int need_remove_insert;
f431efe5 5573 int removed_any;
c906108c 5574
f431efe5
PA
5575 /* First, build the bpstat chain with locations that explain a
5576 target stop, while being careful to not set the target running,
5577 as that may invalidate locations (in particular watchpoint
5578 locations are recreated). Resuming will happen here with
5579 breakpoint conditions or watchpoint expressions that include
5580 inferior function calls. */
c5aa993b 5581
429374b8
JK
5582 ALL_BREAKPOINTS (b)
5583 {
5584 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5585 continue;
a5606eee 5586
429374b8
JK
5587 for (bl = b->loc; bl != NULL; bl = bl->next)
5588 {
4a64f543
MS
5589 /* For hardware watchpoints, we look only at the first
5590 location. The watchpoint_check function will work on the
5591 entire expression, not the individual locations. For
5592 read watchpoints, the watchpoints_triggered function has
5593 checked all locations already. */
429374b8
JK
5594 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5595 break;
18a18393 5596
f6592439 5597 if (!bl->enabled || bl->shlib_disabled)
429374b8 5598 continue;
c5aa993b 5599
09ac7c10 5600 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5601 continue;
c5aa993b 5602
4a64f543
MS
5603 /* Come here if it's a watchpoint, or if the break address
5604 matches. */
c5aa993b 5605
4a64f543
MS
5606 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5607 explain stop. */
c5aa993b 5608
f431efe5
PA
5609 /* Assume we stop. Should we find a watchpoint that is not
5610 actually triggered, or if the condition of the breakpoint
5611 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5612 bs->stop = 1;
5613 bs->print = 1;
d983da9c 5614
f431efe5
PA
5615 /* If this is a scope breakpoint, mark the associated
5616 watchpoint as triggered so that we will handle the
5617 out-of-scope event. We'll get to the watchpoint next
5618 iteration. */
d0fb5eae 5619 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5620 {
5621 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5622
5623 w->watchpoint_triggered = watch_triggered_yes;
5624 }
f431efe5
PA
5625 }
5626 }
5627
5628 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5629 {
f1310107 5630 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5631 {
5760d0ab 5632 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5633 /* For hits of moribund locations, we should just proceed. */
5634 bs->stop = 0;
5635 bs->print = 0;
5636 bs->print_it = print_it_noop;
5637 }
5638 }
5639
edcc5120
TT
5640 /* A bit of special processing for shlib breakpoints. We need to
5641 process solib loading here, so that the lists of loaded and
5642 unloaded libraries are correct before we handle "catch load" and
5643 "catch unload". */
5644 for (bs = bs_head; bs != NULL; bs = bs->next)
5645 {
5d268276 5646 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5647 {
5648 handle_solib_event ();
5649 break;
5650 }
5651 }
5652
f431efe5
PA
5653 /* Now go through the locations that caused the target to stop, and
5654 check whether we're interested in reporting this stop to higher
5655 layers, or whether we should resume the target transparently. */
5656
5657 removed_any = 0;
5658
5760d0ab 5659 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5660 {
5661 if (!bs->stop)
5662 continue;
5663
f431efe5 5664 b = bs->breakpoint_at;
348d480f
PA
5665 b->ops->check_status (bs);
5666 if (bs->stop)
28010a5d 5667 {
348d480f 5668 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5669
429374b8
JK
5670 if (bs->stop)
5671 {
5672 ++(b->hit_count);
8d3788bd 5673 observer_notify_breakpoint_modified (b);
c906108c 5674
4a64f543 5675 /* We will stop here. */
429374b8
JK
5676 if (b->disposition == disp_disable)
5677 {
816338b5
SS
5678 --(b->enable_count);
5679 if (b->enable_count <= 0
5680 && b->enable_state != bp_permanent)
429374b8 5681 b->enable_state = bp_disabled;
f431efe5 5682 removed_any = 1;
429374b8
JK
5683 }
5684 if (b->silent)
5685 bs->print = 0;
5686 bs->commands = b->commands;
9add0f1b 5687 incref_counted_command_line (bs->commands);
abf85f46
JK
5688 if (command_line_is_silent (bs->commands
5689 ? bs->commands->commands : NULL))
5690 bs->print = 0;
9d6e6e84
HZ
5691
5692 b->ops->after_condition_true (bs);
429374b8
JK
5693 }
5694
348d480f 5695 }
a9b3a50f
PA
5696
5697 /* Print nothing for this entry if we don't stop or don't
5698 print. */
5699 if (!bs->stop || !bs->print)
5700 bs->print_it = print_it_noop;
429374b8 5701 }
876fa593 5702
d983da9c
DJ
5703 /* If we aren't stopping, the value of some hardware watchpoint may
5704 not have changed, but the intermediate memory locations we are
5705 watching may have. Don't bother if we're stopping; this will get
5706 done later. */
d832cb68 5707 need_remove_insert = 0;
5760d0ab
JK
5708 if (! bpstat_causes_stop (bs_head))
5709 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5710 if (!bs->stop
f431efe5
PA
5711 && bs->breakpoint_at
5712 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5713 {
3a5c3e22
PA
5714 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5715
5716 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5717 need_remove_insert = 1;
d983da9c
DJ
5718 }
5719
d832cb68 5720 if (need_remove_insert)
44702360 5721 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5722 else if (removed_any)
44702360 5723 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5724
5760d0ab 5725 return bs_head;
c906108c 5726}
628fe4e4
JK
5727
5728static void
5729handle_jit_event (void)
5730{
5731 struct frame_info *frame;
5732 struct gdbarch *gdbarch;
5733
5734 /* Switch terminal for any messages produced by
5735 breakpoint_re_set. */
5736 target_terminal_ours_for_output ();
5737
5738 frame = get_current_frame ();
5739 gdbarch = get_frame_arch (frame);
5740
5741 jit_event_handler (gdbarch);
5742
5743 target_terminal_inferior ();
5744}
5745
5746/* Prepare WHAT final decision for infrun. */
5747
5748/* Decide what infrun needs to do with this bpstat. */
5749
c906108c 5750struct bpstat_what
0e30163f 5751bpstat_what (bpstat bs_head)
c906108c 5752{
c906108c 5753 struct bpstat_what retval;
628fe4e4 5754 int jit_event = 0;
0e30163f 5755 bpstat bs;
c906108c 5756
628fe4e4 5757 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5758 retval.call_dummy = STOP_NONE;
186c406b 5759 retval.is_longjmp = 0;
628fe4e4 5760
0e30163f 5761 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5762 {
628fe4e4
JK
5763 /* Extract this BS's action. After processing each BS, we check
5764 if its action overrides all we've seem so far. */
5765 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5766 enum bptype bptype;
5767
c906108c 5768 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5769 {
5770 /* I suspect this can happen if it was a momentary
5771 breakpoint which has since been deleted. */
5772 bptype = bp_none;
5773 }
20874c92 5774 else
f431efe5 5775 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5776
5777 switch (bptype)
c906108c
SS
5778 {
5779 case bp_none:
628fe4e4 5780 break;
c906108c
SS
5781 case bp_breakpoint:
5782 case bp_hardware_breakpoint:
5783 case bp_until:
5784 case bp_finish:
a9b3a50f 5785 case bp_shlib_event:
c906108c
SS
5786 if (bs->stop)
5787 {
5788 if (bs->print)
628fe4e4 5789 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5790 else
628fe4e4 5791 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5792 }
5793 else
628fe4e4 5794 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5795 break;
5796 case bp_watchpoint:
5797 case bp_hardware_watchpoint:
5798 case bp_read_watchpoint:
5799 case bp_access_watchpoint:
5800 if (bs->stop)
5801 {
5802 if (bs->print)
628fe4e4 5803 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5804 else
628fe4e4 5805 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5806 }
5807 else
628fe4e4
JK
5808 {
5809 /* There was a watchpoint, but we're not stopping.
5810 This requires no further action. */
5811 }
c906108c
SS
5812 break;
5813 case bp_longjmp:
e2e4d78b 5814 case bp_longjmp_call_dummy:
186c406b 5815 case bp_exception:
628fe4e4 5816 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5817 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5818 break;
5819 case bp_longjmp_resume:
186c406b 5820 case bp_exception_resume:
628fe4e4 5821 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5822 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5823 break;
5824 case bp_step_resume:
5825 if (bs->stop)
628fe4e4
JK
5826 this_action = BPSTAT_WHAT_STEP_RESUME;
5827 else
c906108c 5828 {
628fe4e4
JK
5829 /* It is for the wrong frame. */
5830 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5831 }
c906108c 5832 break;
2c03e5be
PA
5833 case bp_hp_step_resume:
5834 if (bs->stop)
5835 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5836 else
5837 {
5838 /* It is for the wrong frame. */
5839 this_action = BPSTAT_WHAT_SINGLE;
5840 }
5841 break;
c906108c 5842 case bp_watchpoint_scope:
c4093a6a 5843 case bp_thread_event:
1900040c 5844 case bp_overlay_event:
0fd8e87f 5845 case bp_longjmp_master:
aa7d318d 5846 case bp_std_terminate_master:
186c406b 5847 case bp_exception_master:
628fe4e4 5848 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5849 break;
ce78b96d 5850 case bp_catchpoint:
c5aa993b
JM
5851 if (bs->stop)
5852 {
5853 if (bs->print)
628fe4e4 5854 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5855 else
628fe4e4 5856 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5857 }
5858 else
628fe4e4
JK
5859 {
5860 /* There was a catchpoint, but we're not stopping.
5861 This requires no further action. */
5862 }
5863 break;
628fe4e4
JK
5864 case bp_jit_event:
5865 jit_event = 1;
5866 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5867 break;
c906108c 5868 case bp_call_dummy:
53a5351d
JM
5869 /* Make sure the action is stop (silent or noisy),
5870 so infrun.c pops the dummy frame. */
aa7d318d 5871 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5872 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5873 break;
5874 case bp_std_terminate:
5875 /* Make sure the action is stop (silent or noisy),
5876 so infrun.c pops the dummy frame. */
aa7d318d 5877 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5878 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5879 break;
1042e4c0 5880 case bp_tracepoint:
7a697b8d 5881 case bp_fast_tracepoint:
0fb4aa4b 5882 case bp_static_tracepoint:
1042e4c0
SS
5883 /* Tracepoint hits should not be reported back to GDB, and
5884 if one got through somehow, it should have been filtered
5885 out already. */
5886 internal_error (__FILE__, __LINE__,
7a697b8d 5887 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5888 break;
5889 case bp_gnu_ifunc_resolver:
5890 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5891 this_action = BPSTAT_WHAT_SINGLE;
5892 break;
5893 case bp_gnu_ifunc_resolver_return:
5894 /* The breakpoint will be removed, execution will restart from the
5895 PC of the former breakpoint. */
5896 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5897 break;
e7e0cddf
SS
5898
5899 case bp_dprintf:
a11cfd87
HZ
5900 if (bs->stop)
5901 this_action = BPSTAT_WHAT_STOP_SILENT;
5902 else
5903 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5904 break;
5905
628fe4e4
JK
5906 default:
5907 internal_error (__FILE__, __LINE__,
5908 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5909 }
628fe4e4
JK
5910
5911 retval.main_action = max (retval.main_action, this_action);
c906108c 5912 }
628fe4e4 5913
0e30163f
JK
5914 /* These operations may affect the bs->breakpoint_at state so they are
5915 delayed after MAIN_ACTION is decided above. */
5916
628fe4e4
JK
5917 if (jit_event)
5918 {
5919 if (debug_infrun)
5920 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5921
5922 handle_jit_event ();
5923 }
5924
0e30163f
JK
5925 for (bs = bs_head; bs != NULL; bs = bs->next)
5926 {
5927 struct breakpoint *b = bs->breakpoint_at;
5928
5929 if (b == NULL)
5930 continue;
5931 switch (b->type)
5932 {
5933 case bp_gnu_ifunc_resolver:
5934 gnu_ifunc_resolver_stop (b);
5935 break;
5936 case bp_gnu_ifunc_resolver_return:
5937 gnu_ifunc_resolver_return_stop (b);
5938 break;
5939 }
5940 }
5941
c906108c
SS
5942 return retval;
5943}
5944
5945/* Nonzero if we should step constantly (e.g. watchpoints on machines
5946 without hardware support). This isn't related to a specific bpstat,
5947 just to things like whether watchpoints are set. */
5948
c5aa993b 5949int
fba45db2 5950bpstat_should_step (void)
c906108c
SS
5951{
5952 struct breakpoint *b;
cc59ec59 5953
c906108c 5954 ALL_BREAKPOINTS (b)
717a8278 5955 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5956 return 1;
c906108c
SS
5957 return 0;
5958}
5959
67822962
PA
5960int
5961bpstat_causes_stop (bpstat bs)
5962{
5963 for (; bs != NULL; bs = bs->next)
5964 if (bs->stop)
5965 return 1;
5966
5967 return 0;
5968}
5969
c906108c 5970\f
c5aa993b 5971
170b53b2
UW
5972/* Compute a string of spaces suitable to indent the next line
5973 so it starts at the position corresponding to the table column
5974 named COL_NAME in the currently active table of UIOUT. */
5975
5976static char *
5977wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5978{
5979 static char wrap_indent[80];
5980 int i, total_width, width, align;
5981 char *text;
5982
5983 total_width = 0;
5984 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5985 {
5986 if (strcmp (text, col_name) == 0)
5987 {
5988 gdb_assert (total_width < sizeof wrap_indent);
5989 memset (wrap_indent, ' ', total_width);
5990 wrap_indent[total_width] = 0;
5991
5992 return wrap_indent;
5993 }
5994
5995 total_width += width + 1;
5996 }
5997
5998 return NULL;
5999}
6000
b775012e
LM
6001/* Determine if the locations of this breakpoint will have their conditions
6002 evaluated by the target, host or a mix of both. Returns the following:
6003
6004 "host": Host evals condition.
6005 "host or target": Host or Target evals condition.
6006 "target": Target evals condition.
6007*/
6008
6009static const char *
6010bp_condition_evaluator (struct breakpoint *b)
6011{
6012 struct bp_location *bl;
6013 char host_evals = 0;
6014 char target_evals = 0;
6015
6016 if (!b)
6017 return NULL;
6018
6019 if (!is_breakpoint (b))
6020 return NULL;
6021
6022 if (gdb_evaluates_breakpoint_condition_p ()
6023 || !target_supports_evaluation_of_breakpoint_conditions ())
6024 return condition_evaluation_host;
6025
6026 for (bl = b->loc; bl; bl = bl->next)
6027 {
6028 if (bl->cond_bytecode)
6029 target_evals++;
6030 else
6031 host_evals++;
6032 }
6033
6034 if (host_evals && target_evals)
6035 return condition_evaluation_both;
6036 else if (target_evals)
6037 return condition_evaluation_target;
6038 else
6039 return condition_evaluation_host;
6040}
6041
6042/* Determine the breakpoint location's condition evaluator. This is
6043 similar to bp_condition_evaluator, but for locations. */
6044
6045static const char *
6046bp_location_condition_evaluator (struct bp_location *bl)
6047{
6048 if (bl && !is_breakpoint (bl->owner))
6049 return NULL;
6050
6051 if (gdb_evaluates_breakpoint_condition_p ()
6052 || !target_supports_evaluation_of_breakpoint_conditions ())
6053 return condition_evaluation_host;
6054
6055 if (bl && bl->cond_bytecode)
6056 return condition_evaluation_target;
6057 else
6058 return condition_evaluation_host;
6059}
6060
859825b8
JK
6061/* Print the LOC location out of the list of B->LOC locations. */
6062
170b53b2
UW
6063static void
6064print_breakpoint_location (struct breakpoint *b,
6065 struct bp_location *loc)
0d381245 6066{
79a45e25 6067 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6068 struct cleanup *old_chain = save_current_program_space ();
6069
859825b8
JK
6070 if (loc != NULL && loc->shlib_disabled)
6071 loc = NULL;
6072
6c95b8df
PA
6073 if (loc != NULL)
6074 set_current_program_space (loc->pspace);
6075
56435ebe
TT
6076 if (b->display_canonical)
6077 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6078 else if (loc && loc->symtab)
0d381245
VP
6079 {
6080 struct symbol *sym
6081 = find_pc_sect_function (loc->address, loc->section);
6082 if (sym)
6083 {
6084 ui_out_text (uiout, "in ");
6085 ui_out_field_string (uiout, "func",
6086 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6087 ui_out_text (uiout, " ");
6088 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6089 ui_out_text (uiout, "at ");
0d381245 6090 }
05cba821
JK
6091 ui_out_field_string (uiout, "file",
6092 symtab_to_filename_for_display (loc->symtab));
0d381245 6093 ui_out_text (uiout, ":");
05cba821 6094
0d381245 6095 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6096 ui_out_field_string (uiout, "fullname",
6097 symtab_to_fullname (loc->symtab));
0d381245 6098
f8eba3c6 6099 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6100 }
859825b8 6101 else if (loc)
0d381245 6102 {
f99d8bf4
PA
6103 struct ui_file *stb = mem_fileopen ();
6104 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6105
f99d8bf4 6106 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6107 demangle, "");
0d381245 6108 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6109
6110 do_cleanups (stb_chain);
0d381245 6111 }
859825b8
JK
6112 else
6113 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6114
b775012e
LM
6115 if (loc && is_breakpoint (b)
6116 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6117 && bp_condition_evaluator (b) == condition_evaluation_both)
6118 {
6119 ui_out_text (uiout, " (");
6120 ui_out_field_string (uiout, "evaluated-by",
6121 bp_location_condition_evaluator (loc));
6122 ui_out_text (uiout, ")");
6123 }
6124
6c95b8df 6125 do_cleanups (old_chain);
0d381245
VP
6126}
6127
269b11a2
PA
6128static const char *
6129bptype_string (enum bptype type)
c906108c 6130{
c4093a6a
JM
6131 struct ep_type_description
6132 {
6133 enum bptype type;
6134 char *description;
6135 };
6136 static struct ep_type_description bptypes[] =
c906108c 6137 {
c5aa993b
JM
6138 {bp_none, "?deleted?"},
6139 {bp_breakpoint, "breakpoint"},
c906108c 6140 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
6141 {bp_until, "until"},
6142 {bp_finish, "finish"},
6143 {bp_watchpoint, "watchpoint"},
c906108c 6144 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6145 {bp_read_watchpoint, "read watchpoint"},
6146 {bp_access_watchpoint, "acc watchpoint"},
6147 {bp_longjmp, "longjmp"},
6148 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6149 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6150 {bp_exception, "exception"},
6151 {bp_exception_resume, "exception resume"},
c5aa993b 6152 {bp_step_resume, "step resume"},
2c03e5be 6153 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6154 {bp_watchpoint_scope, "watchpoint scope"},
6155 {bp_call_dummy, "call dummy"},
aa7d318d 6156 {bp_std_terminate, "std::terminate"},
c5aa993b 6157 {bp_shlib_event, "shlib events"},
c4093a6a 6158 {bp_thread_event, "thread events"},
1900040c 6159 {bp_overlay_event, "overlay events"},
0fd8e87f 6160 {bp_longjmp_master, "longjmp master"},
aa7d318d 6161 {bp_std_terminate_master, "std::terminate master"},
186c406b 6162 {bp_exception_master, "exception master"},
ce78b96d 6163 {bp_catchpoint, "catchpoint"},
1042e4c0 6164 {bp_tracepoint, "tracepoint"},
7a697b8d 6165 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6166 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6167 {bp_dprintf, "dprintf"},
4efc6507 6168 {bp_jit_event, "jit events"},
0e30163f
JK
6169 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6170 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6171 };
269b11a2
PA
6172
6173 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6174 || ((int) type != bptypes[(int) type].type))
6175 internal_error (__FILE__, __LINE__,
6176 _("bptypes table does not describe type #%d."),
6177 (int) type);
6178
6179 return bptypes[(int) type].description;
6180}
6181
998580f1
MK
6182/* For MI, output a field named 'thread-groups' with a list as the value.
6183 For CLI, prefix the list with the string 'inf'. */
6184
6185static void
6186output_thread_groups (struct ui_out *uiout,
6187 const char *field_name,
6188 VEC(int) *inf_num,
6189 int mi_only)
6190{
752eb8b4 6191 struct cleanup *back_to;
998580f1
MK
6192 int is_mi = ui_out_is_mi_like_p (uiout);
6193 int inf;
6194 int i;
6195
6196 /* For backward compatibility, don't display inferiors in CLI unless
6197 there are several. Always display them for MI. */
6198 if (!is_mi && mi_only)
6199 return;
6200
752eb8b4
TT
6201 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6202
998580f1
MK
6203 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6204 {
6205 if (is_mi)
6206 {
6207 char mi_group[10];
6208
6209 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6210 ui_out_field_string (uiout, NULL, mi_group);
6211 }
6212 else
6213 {
6214 if (i == 0)
6215 ui_out_text (uiout, " inf ");
6216 else
6217 ui_out_text (uiout, ", ");
6218
6219 ui_out_text (uiout, plongest (inf));
6220 }
6221 }
6222
6223 do_cleanups (back_to);
6224}
6225
269b11a2
PA
6226/* Print B to gdb_stdout. */
6227
6228static void
6229print_one_breakpoint_location (struct breakpoint *b,
6230 struct bp_location *loc,
6231 int loc_number,
6232 struct bp_location **last_loc,
269b11a2
PA
6233 int allflag)
6234{
6235 struct command_line *l;
c2c6d25f 6236 static char bpenables[] = "nynny";
c906108c 6237
79a45e25 6238 struct ui_out *uiout = current_uiout;
0d381245
VP
6239 int header_of_multiple = 0;
6240 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6241 struct value_print_options opts;
6242
6243 get_user_print_options (&opts);
0d381245
VP
6244
6245 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6246 /* See comment in print_one_breakpoint concerning treatment of
6247 breakpoints with single disabled location. */
0d381245
VP
6248 if (loc == NULL
6249 && (b->loc != NULL
6250 && (b->loc->next != NULL || !b->loc->enabled)))
6251 header_of_multiple = 1;
6252 if (loc == NULL)
6253 loc = b->loc;
6254
c4093a6a
JM
6255 annotate_record ();
6256
6257 /* 1 */
6258 annotate_field (0);
0d381245
VP
6259 if (part_of_multiple)
6260 {
6261 char *formatted;
0c6773c1 6262 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6263 ui_out_field_string (uiout, "number", formatted);
6264 xfree (formatted);
6265 }
6266 else
6267 {
6268 ui_out_field_int (uiout, "number", b->number);
6269 }
c4093a6a
JM
6270
6271 /* 2 */
6272 annotate_field (1);
0d381245
VP
6273 if (part_of_multiple)
6274 ui_out_field_skip (uiout, "type");
269b11a2
PA
6275 else
6276 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6277
6278 /* 3 */
6279 annotate_field (2);
0d381245
VP
6280 if (part_of_multiple)
6281 ui_out_field_skip (uiout, "disp");
6282 else
2cec12e5 6283 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6284
c4093a6a
JM
6285
6286 /* 4 */
6287 annotate_field (3);
0d381245 6288 if (part_of_multiple)
54e52265 6289 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6290 else
4a64f543
MS
6291 ui_out_field_fmt (uiout, "enabled", "%c",
6292 bpenables[(int) b->enable_state]);
54e52265 6293 ui_out_spaces (uiout, 2);
0d381245 6294
c4093a6a
JM
6295
6296 /* 5 and 6 */
3086aeae 6297 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6298 {
4a64f543
MS
6299 /* Although the print_one can possibly print all locations,
6300 calling it here is not likely to get any nice result. So,
6301 make sure there's just one location. */
0d381245 6302 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6303 b->ops->print_one (b, last_loc);
0d381245 6304 }
3086aeae
DJ
6305 else
6306 switch (b->type)
6307 {
6308 case bp_none:
6309 internal_error (__FILE__, __LINE__,
e2e0b3e5 6310 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6311 break;
c906108c 6312
3086aeae
DJ
6313 case bp_watchpoint:
6314 case bp_hardware_watchpoint:
6315 case bp_read_watchpoint:
6316 case bp_access_watchpoint:
3a5c3e22
PA
6317 {
6318 struct watchpoint *w = (struct watchpoint *) b;
6319
6320 /* Field 4, the address, is omitted (which makes the columns
6321 not line up too nicely with the headers, but the effect
6322 is relatively readable). */
6323 if (opts.addressprint)
6324 ui_out_field_skip (uiout, "addr");
6325 annotate_field (5);
6326 ui_out_field_string (uiout, "what", w->exp_string);
6327 }
3086aeae
DJ
6328 break;
6329
3086aeae
DJ
6330 case bp_breakpoint:
6331 case bp_hardware_breakpoint:
6332 case bp_until:
6333 case bp_finish:
6334 case bp_longjmp:
6335 case bp_longjmp_resume:
e2e4d78b 6336 case bp_longjmp_call_dummy:
186c406b
TT
6337 case bp_exception:
6338 case bp_exception_resume:
3086aeae 6339 case bp_step_resume:
2c03e5be 6340 case bp_hp_step_resume:
3086aeae
DJ
6341 case bp_watchpoint_scope:
6342 case bp_call_dummy:
aa7d318d 6343 case bp_std_terminate:
3086aeae
DJ
6344 case bp_shlib_event:
6345 case bp_thread_event:
6346 case bp_overlay_event:
0fd8e87f 6347 case bp_longjmp_master:
aa7d318d 6348 case bp_std_terminate_master:
186c406b 6349 case bp_exception_master:
1042e4c0 6350 case bp_tracepoint:
7a697b8d 6351 case bp_fast_tracepoint:
0fb4aa4b 6352 case bp_static_tracepoint:
e7e0cddf 6353 case bp_dprintf:
4efc6507 6354 case bp_jit_event:
0e30163f
JK
6355 case bp_gnu_ifunc_resolver:
6356 case bp_gnu_ifunc_resolver_return:
79a45b7d 6357 if (opts.addressprint)
3086aeae
DJ
6358 {
6359 annotate_field (4);
54e52265 6360 if (header_of_multiple)
0d381245 6361 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6362 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6363 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6364 else
5af949e3
UW
6365 ui_out_field_core_addr (uiout, "addr",
6366 loc->gdbarch, loc->address);
3086aeae
DJ
6367 }
6368 annotate_field (5);
0d381245 6369 if (!header_of_multiple)
170b53b2 6370 print_breakpoint_location (b, loc);
0d381245 6371 if (b->loc)
a6d9a66e 6372 *last_loc = b->loc;
3086aeae
DJ
6373 break;
6374 }
c906108c 6375
6c95b8df 6376
998580f1 6377 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6378 {
6379 struct inferior *inf;
998580f1
MK
6380 VEC(int) *inf_num = NULL;
6381 int mi_only = 1;
6c95b8df 6382
998580f1 6383 ALL_INFERIORS (inf)
6c95b8df
PA
6384 {
6385 if (inf->pspace == loc->pspace)
998580f1 6386 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6387 }
998580f1
MK
6388
6389 /* For backward compatibility, don't display inferiors in CLI unless
6390 there are several. Always display for MI. */
6391 if (allflag
6392 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6393 && (number_of_program_spaces () > 1
6394 || number_of_inferiors () > 1)
6395 /* LOC is for existing B, it cannot be in
6396 moribund_locations and thus having NULL OWNER. */
6397 && loc->owner->type != bp_catchpoint))
6398 mi_only = 0;
6399 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6400 VEC_free (int, inf_num);
6c95b8df
PA
6401 }
6402
4a306c9a 6403 if (!part_of_multiple)
c4093a6a 6404 {
4a306c9a
JB
6405 if (b->thread != -1)
6406 {
6407 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6408 "stop only in" line a little further down. */
4a306c9a
JB
6409 ui_out_text (uiout, " thread ");
6410 ui_out_field_int (uiout, "thread", b->thread);
6411 }
6412 else if (b->task != 0)
6413 {
6414 ui_out_text (uiout, " task ");
6415 ui_out_field_int (uiout, "task", b->task);
6416 }
c4093a6a 6417 }
f1310107 6418
8b93c638 6419 ui_out_text (uiout, "\n");
f1310107 6420
348d480f 6421 if (!part_of_multiple)
f1310107
TJB
6422 b->ops->print_one_detail (b, uiout);
6423
0d381245 6424 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6425 {
6426 annotate_field (6);
8b93c638 6427 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6428 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6429 the frame ID. */
5af949e3
UW
6430 ui_out_field_core_addr (uiout, "frame",
6431 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6432 ui_out_text (uiout, "\n");
c4093a6a
JM
6433 }
6434
28010a5d 6435 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6436 {
6437 annotate_field (7);
d77f58be 6438 if (is_tracepoint (b))
1042e4c0
SS
6439 ui_out_text (uiout, "\ttrace only if ");
6440 else
6441 ui_out_text (uiout, "\tstop only if ");
0101ce28 6442 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6443
6444 /* Print whether the target is doing the breakpoint's condition
6445 evaluation. If GDB is doing the evaluation, don't print anything. */
6446 if (is_breakpoint (b)
6447 && breakpoint_condition_evaluation_mode ()
6448 == condition_evaluation_target)
6449 {
6450 ui_out_text (uiout, " (");
6451 ui_out_field_string (uiout, "evaluated-by",
6452 bp_condition_evaluator (b));
6453 ui_out_text (uiout, " evals)");
6454 }
0101ce28
JJ
6455 ui_out_text (uiout, "\n");
6456 }
6457
0d381245 6458 if (!part_of_multiple && b->thread != -1)
c4093a6a 6459 {
4a64f543 6460 /* FIXME should make an annotation for this. */
8b93c638
JM
6461 ui_out_text (uiout, "\tstop only in thread ");
6462 ui_out_field_int (uiout, "thread", b->thread);
6463 ui_out_text (uiout, "\n");
c4093a6a
JM
6464 }
6465
556ec64d
YQ
6466 if (!part_of_multiple)
6467 {
6468 if (b->hit_count)
31f56a27
YQ
6469 {
6470 /* FIXME should make an annotation for this. */
6471 if (is_catchpoint (b))
6472 ui_out_text (uiout, "\tcatchpoint");
6473 else if (is_tracepoint (b))
6474 ui_out_text (uiout, "\ttracepoint");
6475 else
6476 ui_out_text (uiout, "\tbreakpoint");
6477 ui_out_text (uiout, " already hit ");
6478 ui_out_field_int (uiout, "times", b->hit_count);
6479 if (b->hit_count == 1)
6480 ui_out_text (uiout, " time\n");
6481 else
6482 ui_out_text (uiout, " times\n");
6483 }
556ec64d
YQ
6484 else
6485 {
31f56a27
YQ
6486 /* Output the count also if it is zero, but only if this is mi. */
6487 if (ui_out_is_mi_like_p (uiout))
6488 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6489 }
6490 }
8b93c638 6491
0d381245 6492 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6493 {
6494 annotate_field (8);
8b93c638
JM
6495 ui_out_text (uiout, "\tignore next ");
6496 ui_out_field_int (uiout, "ignore", b->ignore_count);
6497 ui_out_text (uiout, " hits\n");
c4093a6a 6498 }
059fb39f 6499
816338b5
SS
6500 /* Note that an enable count of 1 corresponds to "enable once"
6501 behavior, which is reported by the combination of enablement and
6502 disposition, so we don't need to mention it here. */
6503 if (!part_of_multiple && b->enable_count > 1)
6504 {
6505 annotate_field (8);
6506 ui_out_text (uiout, "\tdisable after ");
6507 /* Tweak the wording to clarify that ignore and enable counts
6508 are distinct, and have additive effect. */
6509 if (b->ignore_count)
6510 ui_out_text (uiout, "additional ");
6511 else
6512 ui_out_text (uiout, "next ");
6513 ui_out_field_int (uiout, "enable", b->enable_count);
6514 ui_out_text (uiout, " hits\n");
6515 }
6516
f196051f
SS
6517 if (!part_of_multiple && is_tracepoint (b))
6518 {
6519 struct tracepoint *tp = (struct tracepoint *) b;
6520
6521 if (tp->traceframe_usage)
6522 {
6523 ui_out_text (uiout, "\ttrace buffer usage ");
6524 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6525 ui_out_text (uiout, " bytes\n");
6526 }
6527 }
d3ce09f5 6528
9add0f1b 6529 l = b->commands ? b->commands->commands : NULL;
059fb39f 6530 if (!part_of_multiple && l)
c4093a6a 6531 {
3b31d625
EZ
6532 struct cleanup *script_chain;
6533
c4093a6a 6534 annotate_field (9);
3b31d625 6535 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6536 print_command_lines (uiout, l, 4);
3b31d625 6537 do_cleanups (script_chain);
c4093a6a 6538 }
d24317b4 6539
d9b3f62e 6540 if (is_tracepoint (b))
1042e4c0 6541 {
d9b3f62e
PA
6542 struct tracepoint *t = (struct tracepoint *) b;
6543
6544 if (!part_of_multiple && t->pass_count)
6545 {
6546 annotate_field (10);
6547 ui_out_text (uiout, "\tpass count ");
6548 ui_out_field_int (uiout, "pass", t->pass_count);
6549 ui_out_text (uiout, " \n");
6550 }
f2a8bc8a
YQ
6551
6552 /* Don't display it when tracepoint or tracepoint location is
6553 pending. */
6554 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6555 {
6556 annotate_field (11);
6557
6558 if (ui_out_is_mi_like_p (uiout))
6559 ui_out_field_string (uiout, "installed",
6560 loc->inserted ? "y" : "n");
6561 else
6562 {
6563 if (loc->inserted)
6564 ui_out_text (uiout, "\t");
6565 else
6566 ui_out_text (uiout, "\tnot ");
6567 ui_out_text (uiout, "installed on target\n");
6568 }
6569 }
1042e4c0
SS
6570 }
6571
d24317b4
VP
6572 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6573 {
3a5c3e22
PA
6574 if (is_watchpoint (b))
6575 {
6576 struct watchpoint *w = (struct watchpoint *) b;
6577
6578 ui_out_field_string (uiout, "original-location", w->exp_string);
6579 }
6580 else if (b->addr_string)
d24317b4 6581 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6582 }
c4093a6a 6583}
c5aa993b 6584
0d381245
VP
6585static void
6586print_one_breakpoint (struct breakpoint *b,
4a64f543 6587 struct bp_location **last_loc,
6c95b8df 6588 int allflag)
0d381245 6589{
8d3788bd 6590 struct cleanup *bkpt_chain;
79a45e25 6591 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6592
6593 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6594
12c5a436 6595 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6596 do_cleanups (bkpt_chain);
0d381245
VP
6597
6598 /* If this breakpoint has custom print function,
6599 it's already printed. Otherwise, print individual
6600 locations, if any. */
6601 if (b->ops == NULL || b->ops->print_one == NULL)
6602 {
4a64f543
MS
6603 /* If breakpoint has a single location that is disabled, we
6604 print it as if it had several locations, since otherwise it's
6605 hard to represent "breakpoint enabled, location disabled"
6606 situation.
6607
6608 Note that while hardware watchpoints have several locations
a3be7890 6609 internally, that's not a property exposed to user. */
0d381245 6610 if (b->loc
a5606eee 6611 && !is_hardware_watchpoint (b)
8d3788bd 6612 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6613 {
6614 struct bp_location *loc;
6615 int n = 1;
8d3788bd 6616
0d381245 6617 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6618 {
6619 struct cleanup *inner2 =
6620 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6621 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6622 do_cleanups (inner2);
6623 }
0d381245
VP
6624 }
6625 }
6626}
6627
a6d9a66e
UW
6628static int
6629breakpoint_address_bits (struct breakpoint *b)
6630{
6631 int print_address_bits = 0;
6632 struct bp_location *loc;
6633
6634 for (loc = b->loc; loc; loc = loc->next)
6635 {
c7437ca6
PA
6636 int addr_bit;
6637
6638 /* Software watchpoints that aren't watching memory don't have
6639 an address to print. */
6640 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6641 continue;
6642
6643 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6644 if (addr_bit > print_address_bits)
6645 print_address_bits = addr_bit;
6646 }
6647
6648 return print_address_bits;
6649}
0d381245 6650
c4093a6a
JM
6651struct captured_breakpoint_query_args
6652 {
6653 int bnum;
6654 };
c5aa993b 6655
c4093a6a 6656static int
2b65245e 6657do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6658{
6659 struct captured_breakpoint_query_args *args = data;
52f0bd74 6660 struct breakpoint *b;
a6d9a66e 6661 struct bp_location *dummy_loc = NULL;
cc59ec59 6662
c4093a6a
JM
6663 ALL_BREAKPOINTS (b)
6664 {
6665 if (args->bnum == b->number)
c5aa993b 6666 {
12c5a436 6667 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6668 return GDB_RC_OK;
c5aa993b 6669 }
c4093a6a
JM
6670 }
6671 return GDB_RC_NONE;
6672}
c5aa993b 6673
c4093a6a 6674enum gdb_rc
4a64f543
MS
6675gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6676 char **error_message)
c4093a6a
JM
6677{
6678 struct captured_breakpoint_query_args args;
cc59ec59 6679
c4093a6a
JM
6680 args.bnum = bnum;
6681 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6682 an error. */
b0b13bb4
DJ
6683 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6684 error_message, RETURN_MASK_ALL) < 0)
6685 return GDB_RC_FAIL;
6686 else
6687 return GDB_RC_OK;
c4093a6a 6688}
c5aa993b 6689
09d682a4
TT
6690/* Return true if this breakpoint was set by the user, false if it is
6691 internal or momentary. */
6692
6693int
6694user_breakpoint_p (struct breakpoint *b)
6695{
46c6471b 6696 return b->number > 0;
09d682a4
TT
6697}
6698
7f3b0473 6699/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6700 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6701 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6702 FILTER is non-NULL, call it on each breakpoint and only include the
6703 ones for which it returns non-zero. Return the total number of
6704 breakpoints listed. */
c906108c 6705
d77f58be 6706static int
e5a67952 6707breakpoint_1 (char *args, int allflag,
4a64f543 6708 int (*filter) (const struct breakpoint *))
c4093a6a 6709{
52f0bd74 6710 struct breakpoint *b;
a6d9a66e 6711 struct bp_location *last_loc = NULL;
7f3b0473 6712 int nr_printable_breakpoints;
3b31d625 6713 struct cleanup *bkpttbl_chain;
79a45b7d 6714 struct value_print_options opts;
a6d9a66e 6715 int print_address_bits = 0;
269b11a2 6716 int print_type_col_width = 14;
79a45e25 6717 struct ui_out *uiout = current_uiout;
269b11a2 6718
79a45b7d
TT
6719 get_user_print_options (&opts);
6720
4a64f543
MS
6721 /* Compute the number of rows in the table, as well as the size
6722 required for address fields. */
7f3b0473
AC
6723 nr_printable_breakpoints = 0;
6724 ALL_BREAKPOINTS (b)
e5a67952
MS
6725 {
6726 /* If we have a filter, only list the breakpoints it accepts. */
6727 if (filter && !filter (b))
6728 continue;
6729
6730 /* If we have an "args" string, it is a list of breakpoints to
6731 accept. Skip the others. */
6732 if (args != NULL && *args != '\0')
6733 {
6734 if (allflag && parse_and_eval_long (args) != b->number)
6735 continue;
6736 if (!allflag && !number_is_in_list (args, b->number))
6737 continue;
6738 }
269b11a2 6739
e5a67952
MS
6740 if (allflag || user_breakpoint_p (b))
6741 {
6742 int addr_bit, type_len;
a6d9a66e 6743
e5a67952
MS
6744 addr_bit = breakpoint_address_bits (b);
6745 if (addr_bit > print_address_bits)
6746 print_address_bits = addr_bit;
269b11a2 6747
e5a67952
MS
6748 type_len = strlen (bptype_string (b->type));
6749 if (type_len > print_type_col_width)
6750 print_type_col_width = type_len;
6751
6752 nr_printable_breakpoints++;
6753 }
6754 }
7f3b0473 6755
79a45b7d 6756 if (opts.addressprint)
3b31d625 6757 bkpttbl_chain
3e43a32a
MS
6758 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6759 nr_printable_breakpoints,
3b31d625 6760 "BreakpointTable");
8b93c638 6761 else
3b31d625 6762 bkpttbl_chain
3e43a32a
MS
6763 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6764 nr_printable_breakpoints,
3b31d625 6765 "BreakpointTable");
8b93c638 6766
7f3b0473 6767 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6768 annotate_breakpoints_headers ();
6769 if (nr_printable_breakpoints > 0)
6770 annotate_field (0);
4a64f543 6771 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6772 if (nr_printable_breakpoints > 0)
6773 annotate_field (1);
269b11a2 6774 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6775 "type", "Type"); /* 2 */
d7faa9e7
AC
6776 if (nr_printable_breakpoints > 0)
6777 annotate_field (2);
4a64f543 6778 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6779 if (nr_printable_breakpoints > 0)
6780 annotate_field (3);
54e52265 6781 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6782 if (opts.addressprint)
e5a67952
MS
6783 {
6784 if (nr_printable_breakpoints > 0)
6785 annotate_field (4);
6786 if (print_address_bits <= 32)
6787 ui_out_table_header (uiout, 10, ui_left,
6788 "addr", "Address"); /* 5 */
6789 else
6790 ui_out_table_header (uiout, 18, ui_left,
6791 "addr", "Address"); /* 5 */
6792 }
d7faa9e7
AC
6793 if (nr_printable_breakpoints > 0)
6794 annotate_field (5);
6795 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6796 ui_out_table_body (uiout);
6797 if (nr_printable_breakpoints > 0)
6798 annotate_breakpoints_table ();
7f3b0473 6799
c4093a6a 6800 ALL_BREAKPOINTS (b)
e5a67952
MS
6801 {
6802 QUIT;
6803 /* If we have a filter, only list the breakpoints it accepts. */
6804 if (filter && !filter (b))
6805 continue;
6806
6807 /* If we have an "args" string, it is a list of breakpoints to
6808 accept. Skip the others. */
6809
6810 if (args != NULL && *args != '\0')
6811 {
6812 if (allflag) /* maintenance info breakpoint */
6813 {
6814 if (parse_and_eval_long (args) != b->number)
6815 continue;
6816 }
6817 else /* all others */
6818 {
6819 if (!number_is_in_list (args, b->number))
6820 continue;
6821 }
6822 }
6823 /* We only print out user settable breakpoints unless the
6824 allflag is set. */
6825 if (allflag || user_breakpoint_p (b))
12c5a436 6826 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6827 }
6828
3b31d625 6829 do_cleanups (bkpttbl_chain);
698384cd 6830
7f3b0473 6831 if (nr_printable_breakpoints == 0)
c906108c 6832 {
4a64f543
MS
6833 /* If there's a filter, let the caller decide how to report
6834 empty list. */
d77f58be
SS
6835 if (!filter)
6836 {
e5a67952 6837 if (args == NULL || *args == '\0')
d77f58be
SS
6838 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6839 else
4a64f543 6840 ui_out_message (uiout, 0,
e5a67952
MS
6841 "No breakpoint or watchpoint matching '%s'.\n",
6842 args);
d77f58be 6843 }
c906108c
SS
6844 }
6845 else
c4093a6a 6846 {
a6d9a66e
UW
6847 if (last_loc && !server_command)
6848 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6849 }
c906108c 6850
4a64f543 6851 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6852 there have been breakpoints? */
c906108c 6853 annotate_breakpoints_table_end ();
d77f58be
SS
6854
6855 return nr_printable_breakpoints;
c906108c
SS
6856}
6857
ad443146
SS
6858/* Display the value of default-collect in a way that is generally
6859 compatible with the breakpoint list. */
6860
6861static void
6862default_collect_info (void)
6863{
79a45e25
PA
6864 struct ui_out *uiout = current_uiout;
6865
ad443146
SS
6866 /* If it has no value (which is frequently the case), say nothing; a
6867 message like "No default-collect." gets in user's face when it's
6868 not wanted. */
6869 if (!*default_collect)
6870 return;
6871
6872 /* The following phrase lines up nicely with per-tracepoint collect
6873 actions. */
6874 ui_out_text (uiout, "default collect ");
6875 ui_out_field_string (uiout, "default-collect", default_collect);
6876 ui_out_text (uiout, " \n");
6877}
6878
c906108c 6879static void
e5a67952 6880breakpoints_info (char *args, int from_tty)
c906108c 6881{
e5a67952 6882 breakpoint_1 (args, 0, NULL);
ad443146
SS
6883
6884 default_collect_info ();
d77f58be
SS
6885}
6886
6887static void
e5a67952 6888watchpoints_info (char *args, int from_tty)
d77f58be 6889{
e5a67952 6890 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6891 struct ui_out *uiout = current_uiout;
d77f58be
SS
6892
6893 if (num_printed == 0)
6894 {
e5a67952 6895 if (args == NULL || *args == '\0')
d77f58be
SS
6896 ui_out_message (uiout, 0, "No watchpoints.\n");
6897 else
e5a67952 6898 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6899 }
c906108c
SS
6900}
6901
7a292a7a 6902static void
e5a67952 6903maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6904{
e5a67952 6905 breakpoint_1 (args, 1, NULL);
ad443146
SS
6906
6907 default_collect_info ();
c906108c
SS
6908}
6909
0d381245 6910static int
714835d5 6911breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6912 struct program_space *pspace,
714835d5 6913 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6914{
6915 struct bp_location *bl = b->loc;
cc59ec59 6916
0d381245
VP
6917 for (; bl; bl = bl->next)
6918 {
6c95b8df
PA
6919 if (bl->pspace == pspace
6920 && bl->address == pc
0d381245
VP
6921 && (!overlay_debugging || bl->section == section))
6922 return 1;
6923 }
6924 return 0;
6925}
6926
672f9b60 6927/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6928 concerns with logical breakpoints, so we match program spaces, not
6929 address spaces. */
c906108c
SS
6930
6931static void
6c95b8df
PA
6932describe_other_breakpoints (struct gdbarch *gdbarch,
6933 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6934 struct obj_section *section, int thread)
c906108c 6935{
52f0bd74
AC
6936 int others = 0;
6937 struct breakpoint *b;
c906108c
SS
6938
6939 ALL_BREAKPOINTS (b)
672f9b60
KP
6940 others += (user_breakpoint_p (b)
6941 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6942 if (others > 0)
6943 {
a3f17187
AC
6944 if (others == 1)
6945 printf_filtered (_("Note: breakpoint "));
6946 else /* if (others == ???) */
6947 printf_filtered (_("Note: breakpoints "));
c906108c 6948 ALL_BREAKPOINTS (b)
672f9b60 6949 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6950 {
6951 others--;
6952 printf_filtered ("%d", b->number);
6953 if (b->thread == -1 && thread != -1)
6954 printf_filtered (" (all threads)");
6955 else if (b->thread != -1)
6956 printf_filtered (" (thread %d)", b->thread);
6957 printf_filtered ("%s%s ",
059fb39f 6958 ((b->enable_state == bp_disabled
f8eba3c6 6959 || b->enable_state == bp_call_disabled)
0d381245
VP
6960 ? " (disabled)"
6961 : b->enable_state == bp_permanent
6962 ? " (permanent)"
6963 : ""),
6964 (others > 1) ? ","
6965 : ((others == 1) ? " and" : ""));
6966 }
a3f17187 6967 printf_filtered (_("also set at pc "));
5af949e3 6968 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6969 printf_filtered (".\n");
6970 }
6971}
6972\f
c906108c 6973
e4f237da
KB
6974/* Return true iff it is meaningful to use the address member of
6975 BPT. For some breakpoint types, the address member is irrelevant
6976 and it makes no sense to attempt to compare it to other addresses
6977 (or use it for any other purpose either).
6978
4a64f543
MS
6979 More specifically, each of the following breakpoint types will
6980 always have a zero valued address and we don't want to mark
6981 breakpoints of any of these types to be a duplicate of an actual
6982 breakpoint at address zero:
e4f237da
KB
6983
6984 bp_watchpoint
2d134ed3
PA
6985 bp_catchpoint
6986
6987*/
e4f237da
KB
6988
6989static int
6990breakpoint_address_is_meaningful (struct breakpoint *bpt)
6991{
6992 enum bptype type = bpt->type;
6993
2d134ed3
PA
6994 return (type != bp_watchpoint && type != bp_catchpoint);
6995}
6996
6997/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6998 true if LOC1 and LOC2 represent the same watchpoint location. */
6999
7000static int
4a64f543
MS
7001watchpoint_locations_match (struct bp_location *loc1,
7002 struct bp_location *loc2)
2d134ed3 7003{
3a5c3e22
PA
7004 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7005 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7006
7007 /* Both of them must exist. */
7008 gdb_assert (w1 != NULL);
7009 gdb_assert (w2 != NULL);
2bdf28a0 7010
4a64f543
MS
7011 /* If the target can evaluate the condition expression in hardware,
7012 then we we need to insert both watchpoints even if they are at
7013 the same place. Otherwise the watchpoint will only trigger when
7014 the condition of whichever watchpoint was inserted evaluates to
7015 true, not giving a chance for GDB to check the condition of the
7016 other watchpoint. */
3a5c3e22 7017 if ((w1->cond_exp
4a64f543
MS
7018 && target_can_accel_watchpoint_condition (loc1->address,
7019 loc1->length,
0cf6dd15 7020 loc1->watchpoint_type,
3a5c3e22
PA
7021 w1->cond_exp))
7022 || (w2->cond_exp
4a64f543
MS
7023 && target_can_accel_watchpoint_condition (loc2->address,
7024 loc2->length,
0cf6dd15 7025 loc2->watchpoint_type,
3a5c3e22 7026 w2->cond_exp)))
0cf6dd15
TJB
7027 return 0;
7028
85d721b8
PA
7029 /* Note that this checks the owner's type, not the location's. In
7030 case the target does not support read watchpoints, but does
7031 support access watchpoints, we'll have bp_read_watchpoint
7032 watchpoints with hw_access locations. Those should be considered
7033 duplicates of hw_read locations. The hw_read locations will
7034 become hw_access locations later. */
2d134ed3
PA
7035 return (loc1->owner->type == loc2->owner->type
7036 && loc1->pspace->aspace == loc2->pspace->aspace
7037 && loc1->address == loc2->address
7038 && loc1->length == loc2->length);
e4f237da
KB
7039}
7040
31e77af2 7041/* See breakpoint.h. */
6c95b8df 7042
31e77af2 7043int
6c95b8df
PA
7044breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7045 struct address_space *aspace2, CORE_ADDR addr2)
7046{
f5656ead 7047 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7048 || aspace1 == aspace2)
7049 && addr1 == addr2);
7050}
7051
f1310107
TJB
7052/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7053 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7054 matches ASPACE2. On targets that have global breakpoints, the address
7055 space doesn't really matter. */
7056
7057static int
7058breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7059 int len1, struct address_space *aspace2,
7060 CORE_ADDR addr2)
7061{
f5656ead 7062 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7063 || aspace1 == aspace2)
7064 && addr2 >= addr1 && addr2 < addr1 + len1);
7065}
7066
7067/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7068 a ranged breakpoint. In most targets, a match happens only if ASPACE
7069 matches the breakpoint's address space. On targets that have global
7070 breakpoints, the address space doesn't really matter. */
7071
7072static int
7073breakpoint_location_address_match (struct bp_location *bl,
7074 struct address_space *aspace,
7075 CORE_ADDR addr)
7076{
7077 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7078 aspace, addr)
7079 || (bl->length
7080 && breakpoint_address_match_range (bl->pspace->aspace,
7081 bl->address, bl->length,
7082 aspace, addr)));
7083}
7084
1e4d1764
YQ
7085/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7086 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7087 true, otherwise returns false. */
7088
7089static int
7090tracepoint_locations_match (struct bp_location *loc1,
7091 struct bp_location *loc2)
7092{
7093 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7094 /* Since tracepoint locations are never duplicated with others', tracepoint
7095 locations at the same address of different tracepoints are regarded as
7096 different locations. */
7097 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7098 else
7099 return 0;
7100}
7101
2d134ed3
PA
7102/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7103 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7104 represent the same location. */
7105
7106static int
4a64f543
MS
7107breakpoint_locations_match (struct bp_location *loc1,
7108 struct bp_location *loc2)
2d134ed3 7109{
2bdf28a0
JK
7110 int hw_point1, hw_point2;
7111
7112 /* Both of them must not be in moribund_locations. */
7113 gdb_assert (loc1->owner != NULL);
7114 gdb_assert (loc2->owner != NULL);
7115
7116 hw_point1 = is_hardware_watchpoint (loc1->owner);
7117 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7118
7119 if (hw_point1 != hw_point2)
7120 return 0;
7121 else if (hw_point1)
7122 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7123 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7124 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7125 else
f1310107
TJB
7126 /* We compare bp_location.length in order to cover ranged breakpoints. */
7127 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7128 loc2->pspace->aspace, loc2->address)
7129 && loc1->length == loc2->length);
2d134ed3
PA
7130}
7131
76897487
KB
7132static void
7133breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7134 int bnum, int have_bnum)
7135{
f63fbe86
MS
7136 /* The longest string possibly returned by hex_string_custom
7137 is 50 chars. These must be at least that big for safety. */
7138 char astr1[64];
7139 char astr2[64];
76897487 7140
bb599908
PH
7141 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7142 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7143 if (have_bnum)
8a3fe4f8 7144 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7145 bnum, astr1, astr2);
7146 else
8a3fe4f8 7147 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7148}
7149
4a64f543
MS
7150/* Adjust a breakpoint's address to account for architectural
7151 constraints on breakpoint placement. Return the adjusted address.
7152 Note: Very few targets require this kind of adjustment. For most
7153 targets, this function is simply the identity function. */
76897487
KB
7154
7155static CORE_ADDR
a6d9a66e
UW
7156adjust_breakpoint_address (struct gdbarch *gdbarch,
7157 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7158{
a6d9a66e 7159 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7160 {
7161 /* Very few targets need any kind of breakpoint adjustment. */
7162 return bpaddr;
7163 }
88f7da05
KB
7164 else if (bptype == bp_watchpoint
7165 || bptype == bp_hardware_watchpoint
7166 || bptype == bp_read_watchpoint
7167 || bptype == bp_access_watchpoint
fe798b75 7168 || bptype == bp_catchpoint)
88f7da05
KB
7169 {
7170 /* Watchpoints and the various bp_catch_* eventpoints should not
7171 have their addresses modified. */
7172 return bpaddr;
7173 }
76897487
KB
7174 else
7175 {
7176 CORE_ADDR adjusted_bpaddr;
7177
7178 /* Some targets have architectural constraints on the placement
7179 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7180 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7181
7182 /* An adjusted breakpoint address can significantly alter
7183 a user's expectations. Print a warning if an adjustment
7184 is required. */
7185 if (adjusted_bpaddr != bpaddr)
7186 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7187
7188 return adjusted_bpaddr;
7189 }
7190}
7191
28010a5d
PA
7192void
7193init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7194 struct breakpoint *owner)
7cc221ef 7195{
7cc221ef
DJ
7196 memset (loc, 0, sizeof (*loc));
7197
348d480f
PA
7198 gdb_assert (ops != NULL);
7199
28010a5d
PA
7200 loc->ops = ops;
7201 loc->owner = owner;
511a6cd4 7202 loc->cond = NULL;
b775012e 7203 loc->cond_bytecode = NULL;
0d381245
VP
7204 loc->shlib_disabled = 0;
7205 loc->enabled = 1;
e049a4b5 7206
28010a5d 7207 switch (owner->type)
e049a4b5
DJ
7208 {
7209 case bp_breakpoint:
7210 case bp_until:
7211 case bp_finish:
7212 case bp_longjmp:
7213 case bp_longjmp_resume:
e2e4d78b 7214 case bp_longjmp_call_dummy:
186c406b
TT
7215 case bp_exception:
7216 case bp_exception_resume:
e049a4b5 7217 case bp_step_resume:
2c03e5be 7218 case bp_hp_step_resume:
e049a4b5
DJ
7219 case bp_watchpoint_scope:
7220 case bp_call_dummy:
aa7d318d 7221 case bp_std_terminate:
e049a4b5
DJ
7222 case bp_shlib_event:
7223 case bp_thread_event:
7224 case bp_overlay_event:
4efc6507 7225 case bp_jit_event:
0fd8e87f 7226 case bp_longjmp_master:
aa7d318d 7227 case bp_std_terminate_master:
186c406b 7228 case bp_exception_master:
0e30163f
JK
7229 case bp_gnu_ifunc_resolver:
7230 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7231 case bp_dprintf:
e049a4b5 7232 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7233 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7234 break;
7235 case bp_hardware_breakpoint:
7236 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7237 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7238 break;
7239 case bp_hardware_watchpoint:
7240 case bp_read_watchpoint:
7241 case bp_access_watchpoint:
7242 loc->loc_type = bp_loc_hardware_watchpoint;
7243 break;
7244 case bp_watchpoint:
ce78b96d 7245 case bp_catchpoint:
15c3d785
PA
7246 case bp_tracepoint:
7247 case bp_fast_tracepoint:
0fb4aa4b 7248 case bp_static_tracepoint:
e049a4b5
DJ
7249 loc->loc_type = bp_loc_other;
7250 break;
7251 default:
e2e0b3e5 7252 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7253 }
7254
f431efe5 7255 loc->refc = 1;
28010a5d
PA
7256}
7257
7258/* Allocate a struct bp_location. */
7259
7260static struct bp_location *
7261allocate_bp_location (struct breakpoint *bpt)
7262{
348d480f
PA
7263 return bpt->ops->allocate_location (bpt);
7264}
7cc221ef 7265
f431efe5
PA
7266static void
7267free_bp_location (struct bp_location *loc)
fe3f5fa8 7268{
348d480f 7269 loc->ops->dtor (loc);
fe3f5fa8
VP
7270 xfree (loc);
7271}
7272
f431efe5
PA
7273/* Increment reference count. */
7274
7275static void
7276incref_bp_location (struct bp_location *bl)
7277{
7278 ++bl->refc;
7279}
7280
7281/* Decrement reference count. If the reference count reaches 0,
7282 destroy the bp_location. Sets *BLP to NULL. */
7283
7284static void
7285decref_bp_location (struct bp_location **blp)
7286{
0807b50c
PA
7287 gdb_assert ((*blp)->refc > 0);
7288
f431efe5
PA
7289 if (--(*blp)->refc == 0)
7290 free_bp_location (*blp);
7291 *blp = NULL;
7292}
7293
346774a9 7294/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7295
346774a9
PA
7296static void
7297add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7298{
346774a9 7299 struct breakpoint *b1;
c906108c 7300
346774a9
PA
7301 /* Add this breakpoint to the end of the chain so that a list of
7302 breakpoints will come out in order of increasing numbers. */
7303
7304 b1 = breakpoint_chain;
7305 if (b1 == 0)
7306 breakpoint_chain = b;
7307 else
7308 {
7309 while (b1->next)
7310 b1 = b1->next;
7311 b1->next = b;
7312 }
7313}
7314
7315/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7316
7317static void
7318init_raw_breakpoint_without_location (struct breakpoint *b,
7319 struct gdbarch *gdbarch,
28010a5d 7320 enum bptype bptype,
c0a91b2b 7321 const struct breakpoint_ops *ops)
346774a9 7322{
c906108c 7323 memset (b, 0, sizeof (*b));
2219d63c 7324
348d480f
PA
7325 gdb_assert (ops != NULL);
7326
28010a5d 7327 b->ops = ops;
4d28f7a8 7328 b->type = bptype;
a6d9a66e 7329 b->gdbarch = gdbarch;
c906108c
SS
7330 b->language = current_language->la_language;
7331 b->input_radix = input_radix;
7332 b->thread = -1;
b5de0fa7 7333 b->enable_state = bp_enabled;
c906108c
SS
7334 b->next = 0;
7335 b->silent = 0;
7336 b->ignore_count = 0;
7337 b->commands = NULL;
818dd999 7338 b->frame_id = null_frame_id;
0d381245 7339 b->condition_not_parsed = 0;
84f4c1fe 7340 b->py_bp_object = NULL;
d0fb5eae 7341 b->related_breakpoint = b;
346774a9
PA
7342}
7343
7344/* Helper to set_raw_breakpoint below. Creates a breakpoint
7345 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7346
7347static struct breakpoint *
7348set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7349 enum bptype bptype,
c0a91b2b 7350 const struct breakpoint_ops *ops)
346774a9
PA
7351{
7352 struct breakpoint *b = XNEW (struct breakpoint);
7353
348d480f 7354 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7355 add_to_breakpoint_chain (b);
0d381245
VP
7356 return b;
7357}
7358
0e30163f
JK
7359/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7360 resolutions should be made as the user specified the location explicitly
7361 enough. */
7362
0d381245 7363static void
0e30163f 7364set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7365{
2bdf28a0
JK
7366 gdb_assert (loc->owner != NULL);
7367
0d381245 7368 if (loc->owner->type == bp_breakpoint
1042e4c0 7369 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7370 || is_tracepoint (loc->owner))
0d381245 7371 {
0e30163f 7372 int is_gnu_ifunc;
2c02bd72 7373 const char *function_name;
6a3a010b 7374 CORE_ADDR func_addr;
0e30163f 7375
2c02bd72 7376 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7377 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7378
7379 if (is_gnu_ifunc && !explicit_loc)
7380 {
7381 struct breakpoint *b = loc->owner;
7382
7383 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7384 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7385 &loc->requested_address))
7386 {
7387 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7388 loc->address = adjust_breakpoint_address (loc->gdbarch,
7389 loc->requested_address,
7390 b->type);
7391 }
7392 else if (b->type == bp_breakpoint && b->loc == loc
7393 && loc->next == NULL && b->related_breakpoint == b)
7394 {
7395 /* Create only the whole new breakpoint of this type but do not
7396 mess more complicated breakpoints with multiple locations. */
7397 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7398 /* Remember the resolver's address for use by the return
7399 breakpoint. */
7400 loc->related_address = func_addr;
0e30163f
JK
7401 }
7402 }
7403
2c02bd72
DE
7404 if (function_name)
7405 loc->function_name = xstrdup (function_name);
0d381245
VP
7406 }
7407}
7408
a6d9a66e 7409/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7410struct gdbarch *
a6d9a66e
UW
7411get_sal_arch (struct symtab_and_line sal)
7412{
7413 if (sal.section)
7414 return get_objfile_arch (sal.section->objfile);
7415 if (sal.symtab)
7416 return get_objfile_arch (sal.symtab->objfile);
7417
7418 return NULL;
7419}
7420
346774a9
PA
7421/* Low level routine for partially initializing a breakpoint of type
7422 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7423 file name, and line number are provided by SAL.
0d381245
VP
7424
7425 It is expected that the caller will complete the initialization of
7426 the newly created breakpoint struct as well as output any status
c56053d2 7427 information regarding the creation of a new breakpoint. */
0d381245 7428
346774a9
PA
7429static void
7430init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7431 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7432 const struct breakpoint_ops *ops)
0d381245 7433{
28010a5d 7434 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7435
3742cc8b 7436 add_location_to_breakpoint (b, &sal);
0d381245 7437
6c95b8df
PA
7438 if (bptype != bp_catchpoint)
7439 gdb_assert (sal.pspace != NULL);
7440
f8eba3c6
TT
7441 /* Store the program space that was used to set the breakpoint,
7442 except for ordinary breakpoints, which are independent of the
7443 program space. */
7444 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7445 b->pspace = sal.pspace;
346774a9 7446}
c906108c 7447
346774a9
PA
7448/* set_raw_breakpoint is a low level routine for allocating and
7449 partially initializing a breakpoint of type BPTYPE. The newly
7450 created breakpoint's address, section, source file name, and line
7451 number are provided by SAL. The newly created and partially
7452 initialized breakpoint is added to the breakpoint chain and
7453 is also returned as the value of this function.
7454
7455 It is expected that the caller will complete the initialization of
7456 the newly created breakpoint struct as well as output any status
7457 information regarding the creation of a new breakpoint. In
7458 particular, set_raw_breakpoint does NOT set the breakpoint
7459 number! Care should be taken to not allow an error to occur
7460 prior to completing the initialization of the breakpoint. If this
7461 should happen, a bogus breakpoint will be left on the chain. */
7462
7463struct breakpoint *
7464set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7465 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7466 const struct breakpoint_ops *ops)
346774a9
PA
7467{
7468 struct breakpoint *b = XNEW (struct breakpoint);
7469
348d480f 7470 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7471 add_to_breakpoint_chain (b);
c906108c
SS
7472 return b;
7473}
7474
c2c6d25f
JM
7475
7476/* Note that the breakpoint object B describes a permanent breakpoint
7477 instruction, hard-wired into the inferior's code. */
7478void
7479make_breakpoint_permanent (struct breakpoint *b)
7480{
0d381245 7481 struct bp_location *bl;
cc59ec59 7482
b5de0fa7 7483 b->enable_state = bp_permanent;
c2c6d25f 7484
4a64f543
MS
7485 /* By definition, permanent breakpoints are already present in the
7486 code. Mark all locations as inserted. For now,
7487 make_breakpoint_permanent is called in just one place, so it's
7488 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7489 multiple locations or not, but it's easy to implement. */
0d381245
VP
7490 for (bl = b->loc; bl; bl = bl->next)
7491 bl->inserted = 1;
c2c6d25f
JM
7492}
7493
53a5351d 7494/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7495 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7496 initiated the operation. */
c906108c
SS
7497
7498void
186c406b 7499set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7500{
35df4500 7501 struct breakpoint *b, *b_tmp;
186c406b 7502 int thread = tp->num;
0fd8e87f
UW
7503
7504 /* To avoid having to rescan all objfile symbols at every step,
7505 we maintain a list of continually-inserted but always disabled
7506 longjmp "master" breakpoints. Here, we simply create momentary
7507 clones of those and enable them for the requested thread. */
35df4500 7508 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7509 if (b->pspace == current_program_space
186c406b
TT
7510 && (b->type == bp_longjmp_master
7511 || b->type == bp_exception_master))
0fd8e87f 7512 {
06edf0c0
PA
7513 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7514 struct breakpoint *clone;
cc59ec59 7515
e2e4d78b
JK
7516 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7517 after their removal. */
06edf0c0 7518 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7519 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7520 clone->thread = thread;
7521 }
186c406b
TT
7522
7523 tp->initiating_frame = frame;
c906108c
SS
7524}
7525
611c83ae 7526/* Delete all longjmp breakpoints from THREAD. */
c906108c 7527void
611c83ae 7528delete_longjmp_breakpoint (int thread)
c906108c 7529{
35df4500 7530 struct breakpoint *b, *b_tmp;
c906108c 7531
35df4500 7532 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7533 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7534 {
7535 if (b->thread == thread)
7536 delete_breakpoint (b);
7537 }
c906108c
SS
7538}
7539
f59f708a
PA
7540void
7541delete_longjmp_breakpoint_at_next_stop (int thread)
7542{
7543 struct breakpoint *b, *b_tmp;
7544
7545 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7546 if (b->type == bp_longjmp || b->type == bp_exception)
7547 {
7548 if (b->thread == thread)
7549 b->disposition = disp_del_at_next_stop;
7550 }
7551}
7552
e2e4d78b
JK
7553/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7554 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7555 pointer to any of them. Return NULL if this system cannot place longjmp
7556 breakpoints. */
7557
7558struct breakpoint *
7559set_longjmp_breakpoint_for_call_dummy (void)
7560{
7561 struct breakpoint *b, *retval = NULL;
7562
7563 ALL_BREAKPOINTS (b)
7564 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7565 {
7566 struct breakpoint *new_b;
7567
7568 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7569 &momentary_breakpoint_ops,
7570 1);
e2e4d78b
JK
7571 new_b->thread = pid_to_thread_id (inferior_ptid);
7572
7573 /* Link NEW_B into the chain of RETVAL breakpoints. */
7574
7575 gdb_assert (new_b->related_breakpoint == new_b);
7576 if (retval == NULL)
7577 retval = new_b;
7578 new_b->related_breakpoint = retval;
7579 while (retval->related_breakpoint != new_b->related_breakpoint)
7580 retval = retval->related_breakpoint;
7581 retval->related_breakpoint = new_b;
7582 }
7583
7584 return retval;
7585}
7586
7587/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7588 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7589 stack.
7590
7591 You should call this function only at places where it is safe to currently
7592 unwind the whole stack. Failed stack unwind would discard live dummy
7593 frames. */
7594
7595void
b67a2c6f 7596check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7597{
7598 struct breakpoint *b, *b_tmp;
7599
7600 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7601 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7602 {
7603 struct breakpoint *dummy_b = b->related_breakpoint;
7604
7605 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7606 dummy_b = dummy_b->related_breakpoint;
7607 if (dummy_b->type != bp_call_dummy
7608 || frame_find_by_id (dummy_b->frame_id) != NULL)
7609 continue;
7610
b67a2c6f 7611 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7612
7613 while (b->related_breakpoint != b)
7614 {
7615 if (b_tmp == b->related_breakpoint)
7616 b_tmp = b->related_breakpoint->next;
7617 delete_breakpoint (b->related_breakpoint);
7618 }
7619 delete_breakpoint (b);
7620 }
7621}
7622
1900040c
MS
7623void
7624enable_overlay_breakpoints (void)
7625{
52f0bd74 7626 struct breakpoint *b;
1900040c
MS
7627
7628 ALL_BREAKPOINTS (b)
7629 if (b->type == bp_overlay_event)
7630 {
7631 b->enable_state = bp_enabled;
44702360 7632 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7633 overlay_events_enabled = 1;
1900040c
MS
7634 }
7635}
7636
7637void
7638disable_overlay_breakpoints (void)
7639{
52f0bd74 7640 struct breakpoint *b;
1900040c
MS
7641
7642 ALL_BREAKPOINTS (b)
7643 if (b->type == bp_overlay_event)
7644 {
7645 b->enable_state = bp_disabled;
44702360 7646 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7647 overlay_events_enabled = 0;
1900040c
MS
7648 }
7649}
7650
aa7d318d
TT
7651/* Set an active std::terminate breakpoint for each std::terminate
7652 master breakpoint. */
7653void
7654set_std_terminate_breakpoint (void)
7655{
35df4500 7656 struct breakpoint *b, *b_tmp;
aa7d318d 7657
35df4500 7658 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7659 if (b->pspace == current_program_space
7660 && b->type == bp_std_terminate_master)
7661 {
06edf0c0 7662 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7663 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7664 }
7665}
7666
7667/* Delete all the std::terminate breakpoints. */
7668void
7669delete_std_terminate_breakpoint (void)
7670{
35df4500 7671 struct breakpoint *b, *b_tmp;
aa7d318d 7672
35df4500 7673 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7674 if (b->type == bp_std_terminate)
7675 delete_breakpoint (b);
7676}
7677
c4093a6a 7678struct breakpoint *
a6d9a66e 7679create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7680{
7681 struct breakpoint *b;
c4093a6a 7682
06edf0c0
PA
7683 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7684 &internal_breakpoint_ops);
7685
b5de0fa7 7686 b->enable_state = bp_enabled;
c4093a6a 7687 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7688 b->addr_string
7689 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7690
44702360 7691 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7692
c4093a6a
JM
7693 return b;
7694}
7695
7696void
7697remove_thread_event_breakpoints (void)
7698{
35df4500 7699 struct breakpoint *b, *b_tmp;
c4093a6a 7700
35df4500 7701 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7702 if (b->type == bp_thread_event
7703 && b->loc->pspace == current_program_space)
c4093a6a
JM
7704 delete_breakpoint (b);
7705}
7706
0101ce28
JJ
7707struct lang_and_radix
7708 {
7709 enum language lang;
7710 int radix;
7711 };
7712
4efc6507
DE
7713/* Create a breakpoint for JIT code registration and unregistration. */
7714
7715struct breakpoint *
7716create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7717{
7718 struct breakpoint *b;
7719
06edf0c0
PA
7720 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7721 &internal_breakpoint_ops);
44702360 7722 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7723 return b;
7724}
0101ce28 7725
03673fc7
PP
7726/* Remove JIT code registration and unregistration breakpoint(s). */
7727
7728void
7729remove_jit_event_breakpoints (void)
7730{
7731 struct breakpoint *b, *b_tmp;
7732
7733 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7734 if (b->type == bp_jit_event
7735 && b->loc->pspace == current_program_space)
7736 delete_breakpoint (b);
7737}
7738
cae688ec
JJ
7739void
7740remove_solib_event_breakpoints (void)
7741{
35df4500 7742 struct breakpoint *b, *b_tmp;
cae688ec 7743
35df4500 7744 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7745 if (b->type == bp_shlib_event
7746 && b->loc->pspace == current_program_space)
cae688ec
JJ
7747 delete_breakpoint (b);
7748}
7749
f37f681c
PA
7750/* See breakpoint.h. */
7751
7752void
7753remove_solib_event_breakpoints_at_next_stop (void)
7754{
7755 struct breakpoint *b, *b_tmp;
7756
7757 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7758 if (b->type == bp_shlib_event
7759 && b->loc->pspace == current_program_space)
7760 b->disposition = disp_del_at_next_stop;
7761}
7762
04086b45
PA
7763/* Helper for create_solib_event_breakpoint /
7764 create_and_insert_solib_event_breakpoint. Allows specifying which
7765 INSERT_MODE to pass through to update_global_location_list. */
7766
7767static struct breakpoint *
7768create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7769 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7770{
7771 struct breakpoint *b;
7772
06edf0c0
PA
7773 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7774 &internal_breakpoint_ops);
04086b45 7775 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7776 return b;
7777}
7778
04086b45
PA
7779struct breakpoint *
7780create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7781{
7782 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7783}
7784
f37f681c
PA
7785/* See breakpoint.h. */
7786
7787struct breakpoint *
7788create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7789{
7790 struct breakpoint *b;
7791
04086b45
PA
7792 /* Explicitly tell update_global_location_list to insert
7793 locations. */
7794 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7795 if (!b->loc->inserted)
7796 {
7797 delete_breakpoint (b);
7798 return NULL;
7799 }
7800 return b;
7801}
7802
cae688ec
JJ
7803/* Disable any breakpoints that are on code in shared libraries. Only
7804 apply to enabled breakpoints, disabled ones can just stay disabled. */
7805
7806void
cb851954 7807disable_breakpoints_in_shlibs (void)
cae688ec 7808{
876fa593 7809 struct bp_location *loc, **locp_tmp;
cae688ec 7810
876fa593 7811 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7812 {
2bdf28a0 7813 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7814 struct breakpoint *b = loc->owner;
2bdf28a0 7815
4a64f543
MS
7816 /* We apply the check to all breakpoints, including disabled for
7817 those with loc->duplicate set. This is so that when breakpoint
7818 becomes enabled, or the duplicate is removed, gdb will try to
7819 insert all breakpoints. If we don't set shlib_disabled here,
7820 we'll try to insert those breakpoints and fail. */
1042e4c0 7821 if (((b->type == bp_breakpoint)
508ccb1f 7822 || (b->type == bp_jit_event)
1042e4c0 7823 || (b->type == bp_hardware_breakpoint)
d77f58be 7824 || (is_tracepoint (b)))
6c95b8df 7825 && loc->pspace == current_program_space
0d381245 7826 && !loc->shlib_disabled
6c95b8df 7827 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7828 )
0d381245
VP
7829 {
7830 loc->shlib_disabled = 1;
7831 }
cae688ec
JJ
7832 }
7833}
7834
63644780
NB
7835/* Disable any breakpoints and tracepoints that are in SOLIB upon
7836 notification of unloaded_shlib. Only apply to enabled breakpoints,
7837 disabled ones can just stay disabled. */
84acb35a 7838
75149521 7839static void
84acb35a
JJ
7840disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7841{
876fa593 7842 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7843 int disabled_shlib_breaks = 0;
7844
c86cf029
VP
7845 /* SunOS a.out shared libraries are always mapped, so do not
7846 disable breakpoints; they will only be reported as unloaded
7847 through clear_solib when GDB discards its shared library
7848 list. See clear_solib for more information. */
7849 if (exec_bfd != NULL
7850 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7851 return;
7852
876fa593 7853 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7854 {
2bdf28a0 7855 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7856 struct breakpoint *b = loc->owner;
cc59ec59 7857
1e4d1764 7858 if (solib->pspace == loc->pspace
e2dd7057 7859 && !loc->shlib_disabled
1e4d1764
YQ
7860 && (((b->type == bp_breakpoint
7861 || b->type == bp_jit_event
7862 || b->type == bp_hardware_breakpoint)
7863 && (loc->loc_type == bp_loc_hardware_breakpoint
7864 || loc->loc_type == bp_loc_software_breakpoint))
7865 || is_tracepoint (b))
e2dd7057 7866 && solib_contains_address_p (solib, loc->address))
84acb35a 7867 {
e2dd7057
PP
7868 loc->shlib_disabled = 1;
7869 /* At this point, we cannot rely on remove_breakpoint
7870 succeeding so we must mark the breakpoint as not inserted
7871 to prevent future errors occurring in remove_breakpoints. */
7872 loc->inserted = 0;
8d3788bd
VP
7873
7874 /* This may cause duplicate notifications for the same breakpoint. */
7875 observer_notify_breakpoint_modified (b);
7876
e2dd7057
PP
7877 if (!disabled_shlib_breaks)
7878 {
7879 target_terminal_ours_for_output ();
3e43a32a
MS
7880 warning (_("Temporarily disabling breakpoints "
7881 "for unloaded shared library \"%s\""),
e2dd7057 7882 solib->so_name);
84acb35a 7883 }
e2dd7057 7884 disabled_shlib_breaks = 1;
84acb35a
JJ
7885 }
7886 }
84acb35a
JJ
7887}
7888
63644780
NB
7889/* Disable any breakpoints and tracepoints in OBJFILE upon
7890 notification of free_objfile. Only apply to enabled breakpoints,
7891 disabled ones can just stay disabled. */
7892
7893static void
7894disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7895{
7896 struct breakpoint *b;
7897
7898 if (objfile == NULL)
7899 return;
7900
d03de421
PA
7901 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7902 managed by the user with add-symbol-file/remove-symbol-file.
7903 Similarly to how breakpoints in shared libraries are handled in
7904 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7905 shlib_disabled so they end up uninserted on the next global
7906 location list update. Shared libraries not loaded by the user
7907 aren't handled here -- they're already handled in
7908 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7909 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7910 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7911 main objfile). */
7912 if ((objfile->flags & OBJF_SHARED) == 0
7913 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7914 return;
7915
7916 ALL_BREAKPOINTS (b)
7917 {
7918 struct bp_location *loc;
7919 int bp_modified = 0;
7920
7921 if (!is_breakpoint (b) && !is_tracepoint (b))
7922 continue;
7923
7924 for (loc = b->loc; loc != NULL; loc = loc->next)
7925 {
7926 CORE_ADDR loc_addr = loc->address;
7927
7928 if (loc->loc_type != bp_loc_hardware_breakpoint
7929 && loc->loc_type != bp_loc_software_breakpoint)
7930 continue;
7931
7932 if (loc->shlib_disabled != 0)
7933 continue;
7934
7935 if (objfile->pspace != loc->pspace)
7936 continue;
7937
7938 if (loc->loc_type != bp_loc_hardware_breakpoint
7939 && loc->loc_type != bp_loc_software_breakpoint)
7940 continue;
7941
7942 if (is_addr_in_objfile (loc_addr, objfile))
7943 {
7944 loc->shlib_disabled = 1;
08351840
PA
7945 /* At this point, we don't know whether the object was
7946 unmapped from the inferior or not, so leave the
7947 inserted flag alone. We'll handle failure to
7948 uninsert quietly, in case the object was indeed
7949 unmapped. */
63644780
NB
7950
7951 mark_breakpoint_location_modified (loc);
7952
7953 bp_modified = 1;
7954 }
7955 }
7956
7957 if (bp_modified)
7958 observer_notify_breakpoint_modified (b);
7959 }
7960}
7961
ce78b96d
JB
7962/* FORK & VFORK catchpoints. */
7963
e29a4733
PA
7964/* An instance of this type is used to represent a fork or vfork
7965 catchpoint. It includes a "struct breakpoint" as a kind of base
7966 class; users downcast to "struct breakpoint *" when needed. A
7967 breakpoint is really of this type iff its ops pointer points to
7968 CATCH_FORK_BREAKPOINT_OPS. */
7969
7970struct fork_catchpoint
7971{
7972 /* The base class. */
7973 struct breakpoint base;
7974
7975 /* Process id of a child process whose forking triggered this
7976 catchpoint. This field is only valid immediately after this
7977 catchpoint has triggered. */
7978 ptid_t forked_inferior_pid;
7979};
7980
4a64f543
MS
7981/* Implement the "insert" breakpoint_ops method for fork
7982 catchpoints. */
ce78b96d 7983
77b06cd7
TJB
7984static int
7985insert_catch_fork (struct bp_location *bl)
ce78b96d 7986{
dfd4cc63 7987 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7988}
7989
4a64f543
MS
7990/* Implement the "remove" breakpoint_ops method for fork
7991 catchpoints. */
ce78b96d
JB
7992
7993static int
77b06cd7 7994remove_catch_fork (struct bp_location *bl)
ce78b96d 7995{
dfd4cc63 7996 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7997}
7998
7999/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8000 catchpoints. */
8001
8002static int
f1310107 8003breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8004 struct address_space *aspace, CORE_ADDR bp_addr,
8005 const struct target_waitstatus *ws)
ce78b96d 8006{
e29a4733
PA
8007 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8008
f90263c1
TT
8009 if (ws->kind != TARGET_WAITKIND_FORKED)
8010 return 0;
8011
8012 c->forked_inferior_pid = ws->value.related_pid;
8013 return 1;
ce78b96d
JB
8014}
8015
4a64f543
MS
8016/* Implement the "print_it" breakpoint_ops method for fork
8017 catchpoints. */
ce78b96d
JB
8018
8019static enum print_stop_action
348d480f 8020print_it_catch_fork (bpstat bs)
ce78b96d 8021{
36dfb11c 8022 struct ui_out *uiout = current_uiout;
348d480f
PA
8023 struct breakpoint *b = bs->breakpoint_at;
8024 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8025
ce78b96d 8026 annotate_catchpoint (b->number);
36dfb11c
TT
8027 if (b->disposition == disp_del)
8028 ui_out_text (uiout, "\nTemporary catchpoint ");
8029 else
8030 ui_out_text (uiout, "\nCatchpoint ");
8031 if (ui_out_is_mi_like_p (uiout))
8032 {
8033 ui_out_field_string (uiout, "reason",
8034 async_reason_lookup (EXEC_ASYNC_FORK));
8035 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8036 }
8037 ui_out_field_int (uiout, "bkptno", b->number);
8038 ui_out_text (uiout, " (forked process ");
8039 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8040 ui_out_text (uiout, "), ");
ce78b96d
JB
8041 return PRINT_SRC_AND_LOC;
8042}
8043
4a64f543
MS
8044/* Implement the "print_one" breakpoint_ops method for fork
8045 catchpoints. */
ce78b96d
JB
8046
8047static void
a6d9a66e 8048print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8049{
e29a4733 8050 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8051 struct value_print_options opts;
79a45e25 8052 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8053
8054 get_user_print_options (&opts);
8055
4a64f543
MS
8056 /* Field 4, the address, is omitted (which makes the columns not
8057 line up too nicely with the headers, but the effect is relatively
8058 readable). */
79a45b7d 8059 if (opts.addressprint)
ce78b96d
JB
8060 ui_out_field_skip (uiout, "addr");
8061 annotate_field (5);
8062 ui_out_text (uiout, "fork");
e29a4733 8063 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8064 {
8065 ui_out_text (uiout, ", process ");
8066 ui_out_field_int (uiout, "what",
e29a4733 8067 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8068 ui_out_spaces (uiout, 1);
8069 }
8ac3646f
TT
8070
8071 if (ui_out_is_mi_like_p (uiout))
8072 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8073}
8074
8075/* Implement the "print_mention" breakpoint_ops method for fork
8076 catchpoints. */
8077
8078static void
8079print_mention_catch_fork (struct breakpoint *b)
8080{
8081 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8082}
8083
6149aea9
PA
8084/* Implement the "print_recreate" breakpoint_ops method for fork
8085 catchpoints. */
8086
8087static void
8088print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8089{
8090 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8091 print_recreate_thread (b, fp);
6149aea9
PA
8092}
8093
ce78b96d
JB
8094/* The breakpoint_ops structure to be used in fork catchpoints. */
8095
2060206e 8096static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8097
4a64f543
MS
8098/* Implement the "insert" breakpoint_ops method for vfork
8099 catchpoints. */
ce78b96d 8100
77b06cd7
TJB
8101static int
8102insert_catch_vfork (struct bp_location *bl)
ce78b96d 8103{
dfd4cc63 8104 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8105}
8106
4a64f543
MS
8107/* Implement the "remove" breakpoint_ops method for vfork
8108 catchpoints. */
ce78b96d
JB
8109
8110static int
77b06cd7 8111remove_catch_vfork (struct bp_location *bl)
ce78b96d 8112{
dfd4cc63 8113 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8114}
8115
8116/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8117 catchpoints. */
8118
8119static int
f1310107 8120breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8121 struct address_space *aspace, CORE_ADDR bp_addr,
8122 const struct target_waitstatus *ws)
ce78b96d 8123{
e29a4733
PA
8124 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8125
f90263c1
TT
8126 if (ws->kind != TARGET_WAITKIND_VFORKED)
8127 return 0;
8128
8129 c->forked_inferior_pid = ws->value.related_pid;
8130 return 1;
ce78b96d
JB
8131}
8132
4a64f543
MS
8133/* Implement the "print_it" breakpoint_ops method for vfork
8134 catchpoints. */
ce78b96d
JB
8135
8136static enum print_stop_action
348d480f 8137print_it_catch_vfork (bpstat bs)
ce78b96d 8138{
36dfb11c 8139 struct ui_out *uiout = current_uiout;
348d480f 8140 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8141 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8142
ce78b96d 8143 annotate_catchpoint (b->number);
36dfb11c
TT
8144 if (b->disposition == disp_del)
8145 ui_out_text (uiout, "\nTemporary catchpoint ");
8146 else
8147 ui_out_text (uiout, "\nCatchpoint ");
8148 if (ui_out_is_mi_like_p (uiout))
8149 {
8150 ui_out_field_string (uiout, "reason",
8151 async_reason_lookup (EXEC_ASYNC_VFORK));
8152 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8153 }
8154 ui_out_field_int (uiout, "bkptno", b->number);
8155 ui_out_text (uiout, " (vforked process ");
8156 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8157 ui_out_text (uiout, "), ");
ce78b96d
JB
8158 return PRINT_SRC_AND_LOC;
8159}
8160
4a64f543
MS
8161/* Implement the "print_one" breakpoint_ops method for vfork
8162 catchpoints. */
ce78b96d
JB
8163
8164static void
a6d9a66e 8165print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8166{
e29a4733 8167 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8168 struct value_print_options opts;
79a45e25 8169 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8170
8171 get_user_print_options (&opts);
4a64f543
MS
8172 /* Field 4, the address, is omitted (which makes the columns not
8173 line up too nicely with the headers, but the effect is relatively
8174 readable). */
79a45b7d 8175 if (opts.addressprint)
ce78b96d
JB
8176 ui_out_field_skip (uiout, "addr");
8177 annotate_field (5);
8178 ui_out_text (uiout, "vfork");
e29a4733 8179 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8180 {
8181 ui_out_text (uiout, ", process ");
8182 ui_out_field_int (uiout, "what",
e29a4733 8183 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8184 ui_out_spaces (uiout, 1);
8185 }
8ac3646f
TT
8186
8187 if (ui_out_is_mi_like_p (uiout))
8188 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8189}
8190
8191/* Implement the "print_mention" breakpoint_ops method for vfork
8192 catchpoints. */
8193
8194static void
8195print_mention_catch_vfork (struct breakpoint *b)
8196{
8197 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8198}
8199
6149aea9
PA
8200/* Implement the "print_recreate" breakpoint_ops method for vfork
8201 catchpoints. */
8202
8203static void
8204print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8205{
8206 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8207 print_recreate_thread (b, fp);
6149aea9
PA
8208}
8209
ce78b96d
JB
8210/* The breakpoint_ops structure to be used in vfork catchpoints. */
8211
2060206e 8212static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8213
edcc5120
TT
8214/* An instance of this type is used to represent an solib catchpoint.
8215 It includes a "struct breakpoint" as a kind of base class; users
8216 downcast to "struct breakpoint *" when needed. A breakpoint is
8217 really of this type iff its ops pointer points to
8218 CATCH_SOLIB_BREAKPOINT_OPS. */
8219
8220struct solib_catchpoint
8221{
8222 /* The base class. */
8223 struct breakpoint base;
8224
8225 /* True for "catch load", false for "catch unload". */
8226 unsigned char is_load;
8227
8228 /* Regular expression to match, if any. COMPILED is only valid when
8229 REGEX is non-NULL. */
8230 char *regex;
8231 regex_t compiled;
8232};
8233
8234static void
8235dtor_catch_solib (struct breakpoint *b)
8236{
8237 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8238
8239 if (self->regex)
8240 regfree (&self->compiled);
8241 xfree (self->regex);
8242
8243 base_breakpoint_ops.dtor (b);
8244}
8245
8246static int
8247insert_catch_solib (struct bp_location *ignore)
8248{
8249 return 0;
8250}
8251
8252static int
8253remove_catch_solib (struct bp_location *ignore)
8254{
8255 return 0;
8256}
8257
8258static int
8259breakpoint_hit_catch_solib (const struct bp_location *bl,
8260 struct address_space *aspace,
8261 CORE_ADDR bp_addr,
8262 const struct target_waitstatus *ws)
8263{
8264 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8265 struct breakpoint *other;
8266
8267 if (ws->kind == TARGET_WAITKIND_LOADED)
8268 return 1;
8269
8270 ALL_BREAKPOINTS (other)
8271 {
8272 struct bp_location *other_bl;
8273
8274 if (other == bl->owner)
8275 continue;
8276
8277 if (other->type != bp_shlib_event)
8278 continue;
8279
8280 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8281 continue;
8282
8283 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8284 {
8285 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8286 return 1;
8287 }
8288 }
8289
8290 return 0;
8291}
8292
8293static void
8294check_status_catch_solib (struct bpstats *bs)
8295{
8296 struct solib_catchpoint *self
8297 = (struct solib_catchpoint *) bs->breakpoint_at;
8298 int ix;
8299
8300 if (self->is_load)
8301 {
8302 struct so_list *iter;
8303
8304 for (ix = 0;
8305 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8306 ix, iter);
8307 ++ix)
8308 {
8309 if (!self->regex
8310 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8311 return;
8312 }
8313 }
8314 else
8315 {
8316 char *iter;
8317
8318 for (ix = 0;
8319 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8320 ix, iter);
8321 ++ix)
8322 {
8323 if (!self->regex
8324 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8325 return;
8326 }
8327 }
8328
8329 bs->stop = 0;
8330 bs->print_it = print_it_noop;
8331}
8332
8333static enum print_stop_action
8334print_it_catch_solib (bpstat bs)
8335{
8336 struct breakpoint *b = bs->breakpoint_at;
8337 struct ui_out *uiout = current_uiout;
8338
8339 annotate_catchpoint (b->number);
8340 if (b->disposition == disp_del)
8341 ui_out_text (uiout, "\nTemporary catchpoint ");
8342 else
8343 ui_out_text (uiout, "\nCatchpoint ");
8344 ui_out_field_int (uiout, "bkptno", b->number);
8345 ui_out_text (uiout, "\n");
8346 if (ui_out_is_mi_like_p (uiout))
8347 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8348 print_solib_event (1);
8349 return PRINT_SRC_AND_LOC;
8350}
8351
8352static void
8353print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8354{
8355 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8356 struct value_print_options opts;
8357 struct ui_out *uiout = current_uiout;
8358 char *msg;
8359
8360 get_user_print_options (&opts);
8361 /* Field 4, the address, is omitted (which makes the columns not
8362 line up too nicely with the headers, but the effect is relatively
8363 readable). */
8364 if (opts.addressprint)
8365 {
8366 annotate_field (4);
8367 ui_out_field_skip (uiout, "addr");
8368 }
8369
8370 annotate_field (5);
8371 if (self->is_load)
8372 {
8373 if (self->regex)
8374 msg = xstrprintf (_("load of library matching %s"), self->regex);
8375 else
8376 msg = xstrdup (_("load of library"));
8377 }
8378 else
8379 {
8380 if (self->regex)
8381 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8382 else
8383 msg = xstrdup (_("unload of library"));
8384 }
8385 ui_out_field_string (uiout, "what", msg);
8386 xfree (msg);
8ac3646f
TT
8387
8388 if (ui_out_is_mi_like_p (uiout))
8389 ui_out_field_string (uiout, "catch-type",
8390 self->is_load ? "load" : "unload");
edcc5120
TT
8391}
8392
8393static void
8394print_mention_catch_solib (struct breakpoint *b)
8395{
8396 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8397
8398 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8399 self->is_load ? "load" : "unload");
8400}
8401
8402static void
8403print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8404{
8405 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8406
8407 fprintf_unfiltered (fp, "%s %s",
8408 b->disposition == disp_del ? "tcatch" : "catch",
8409 self->is_load ? "load" : "unload");
8410 if (self->regex)
8411 fprintf_unfiltered (fp, " %s", self->regex);
8412 fprintf_unfiltered (fp, "\n");
8413}
8414
8415static struct breakpoint_ops catch_solib_breakpoint_ops;
8416
91985142
MG
8417/* Shared helper function (MI and CLI) for creating and installing
8418 a shared object event catchpoint. If IS_LOAD is non-zero then
8419 the events to be caught are load events, otherwise they are
8420 unload events. If IS_TEMP is non-zero the catchpoint is a
8421 temporary one. If ENABLED is non-zero the catchpoint is
8422 created in an enabled state. */
edcc5120 8423
91985142
MG
8424void
8425add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8426{
8427 struct solib_catchpoint *c;
8428 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8429 struct cleanup *cleanup;
8430
edcc5120
TT
8431 if (!arg)
8432 arg = "";
8433 arg = skip_spaces (arg);
8434
8435 c = XCNEW (struct solib_catchpoint);
8436 cleanup = make_cleanup (xfree, c);
8437
8438 if (*arg != '\0')
8439 {
8440 int errcode;
8441
8442 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8443 if (errcode != 0)
8444 {
8445 char *err = get_regcomp_error (errcode, &c->compiled);
8446
8447 make_cleanup (xfree, err);
8448 error (_("Invalid regexp (%s): %s"), err, arg);
8449 }
8450 c->regex = xstrdup (arg);
8451 }
8452
8453 c->is_load = is_load;
91985142 8454 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8455 &catch_solib_breakpoint_ops);
8456
91985142
MG
8457 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8458
edcc5120
TT
8459 discard_cleanups (cleanup);
8460 install_breakpoint (0, &c->base, 1);
8461}
8462
91985142
MG
8463/* A helper function that does all the work for "catch load" and
8464 "catch unload". */
8465
8466static void
8467catch_load_or_unload (char *arg, int from_tty, int is_load,
8468 struct cmd_list_element *command)
8469{
8470 int tempflag;
8471 const int enabled = 1;
8472
8473 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8474
8475 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8476}
8477
edcc5120
TT
8478static void
8479catch_load_command_1 (char *arg, int from_tty,
8480 struct cmd_list_element *command)
8481{
8482 catch_load_or_unload (arg, from_tty, 1, command);
8483}
8484
8485static void
8486catch_unload_command_1 (char *arg, int from_tty,
8487 struct cmd_list_element *command)
8488{
8489 catch_load_or_unload (arg, from_tty, 0, command);
8490}
8491
be5c67c1
PA
8492/* An instance of this type is used to represent a syscall catchpoint.
8493 It includes a "struct breakpoint" as a kind of base class; users
8494 downcast to "struct breakpoint *" when needed. A breakpoint is
8495 really of this type iff its ops pointer points to
8496 CATCH_SYSCALL_BREAKPOINT_OPS. */
8497
8498struct syscall_catchpoint
8499{
8500 /* The base class. */
8501 struct breakpoint base;
8502
8503 /* Syscall numbers used for the 'catch syscall' feature. If no
8504 syscall has been specified for filtering, its value is NULL.
8505 Otherwise, it holds a list of all syscalls to be caught. The
8506 list elements are allocated with xmalloc. */
8507 VEC(int) *syscalls_to_be_caught;
8508};
8509
8510/* Implement the "dtor" breakpoint_ops method for syscall
8511 catchpoints. */
8512
8513static void
8514dtor_catch_syscall (struct breakpoint *b)
8515{
8516 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8517
8518 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8519
2060206e 8520 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8521}
8522
fa3064dd
YQ
8523static const struct inferior_data *catch_syscall_inferior_data = NULL;
8524
8525struct catch_syscall_inferior_data
8526{
8527 /* We keep a count of the number of times the user has requested a
8528 particular syscall to be tracked, and pass this information to the
8529 target. This lets capable targets implement filtering directly. */
8530
8531 /* Number of times that "any" syscall is requested. */
8532 int any_syscall_count;
8533
8534 /* Count of each system call. */
8535 VEC(int) *syscalls_counts;
8536
8537 /* This counts all syscall catch requests, so we can readily determine
8538 if any catching is necessary. */
8539 int total_syscalls_count;
8540};
8541
8542static struct catch_syscall_inferior_data*
8543get_catch_syscall_inferior_data (struct inferior *inf)
8544{
8545 struct catch_syscall_inferior_data *inf_data;
8546
8547 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8548 if (inf_data == NULL)
8549 {
41bf6aca 8550 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8551 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8552 }
8553
8554 return inf_data;
8555}
8556
8557static void
8558catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8559{
8560 xfree (arg);
8561}
8562
8563
a96d9b2e
SDJ
8564/* Implement the "insert" breakpoint_ops method for syscall
8565 catchpoints. */
8566
77b06cd7
TJB
8567static int
8568insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8569{
be5c67c1 8570 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8571 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8572 struct catch_syscall_inferior_data *inf_data
8573 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8574
fa3064dd 8575 ++inf_data->total_syscalls_count;
be5c67c1 8576 if (!c->syscalls_to_be_caught)
fa3064dd 8577 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8578 else
8579 {
8580 int i, iter;
cc59ec59 8581
a96d9b2e 8582 for (i = 0;
be5c67c1 8583 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8584 i++)
8585 {
8586 int elem;
cc59ec59 8587
fa3064dd 8588 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8589 {
fa3064dd 8590 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8591 uintptr_t vec_addr_offset
8592 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8593 uintptr_t vec_addr;
fa3064dd
YQ
8594 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8595 vec_addr = ((uintptr_t) VEC_address (int,
8596 inf_data->syscalls_counts)
8597 + vec_addr_offset);
a96d9b2e
SDJ
8598 memset ((void *) vec_addr, 0,
8599 (iter + 1 - old_size) * sizeof (int));
8600 }
fa3064dd
YQ
8601 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8602 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8603 }
8604 }
8605
dfd4cc63 8606 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8607 inf_data->total_syscalls_count != 0,
8608 inf_data->any_syscall_count,
8609 VEC_length (int,
8610 inf_data->syscalls_counts),
8611 VEC_address (int,
8612 inf_data->syscalls_counts));
a96d9b2e
SDJ
8613}
8614
8615/* Implement the "remove" breakpoint_ops method for syscall
8616 catchpoints. */
8617
8618static int
77b06cd7 8619remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8620{
be5c67c1 8621 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8622 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8623 struct catch_syscall_inferior_data *inf_data
8624 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8625
fa3064dd 8626 --inf_data->total_syscalls_count;
be5c67c1 8627 if (!c->syscalls_to_be_caught)
fa3064dd 8628 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8629 else
8630 {
8631 int i, iter;
cc59ec59 8632
a96d9b2e 8633 for (i = 0;
be5c67c1 8634 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8635 i++)
8636 {
8637 int elem;
fa3064dd 8638 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8639 /* Shouldn't happen. */
8640 continue;
fa3064dd
YQ
8641 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8642 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8643 }
8644 }
8645
dfd4cc63 8646 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8647 inf_data->total_syscalls_count != 0,
8648 inf_data->any_syscall_count,
8649 VEC_length (int,
8650 inf_data->syscalls_counts),
3e43a32a 8651 VEC_address (int,
fa3064dd 8652 inf_data->syscalls_counts));
a96d9b2e
SDJ
8653}
8654
8655/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8656 catchpoints. */
8657
8658static int
f1310107 8659breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8660 struct address_space *aspace, CORE_ADDR bp_addr,
8661 const struct target_waitstatus *ws)
a96d9b2e 8662{
4a64f543
MS
8663 /* We must check if we are catching specific syscalls in this
8664 breakpoint. If we are, then we must guarantee that the called
8665 syscall is the same syscall we are catching. */
a96d9b2e 8666 int syscall_number = 0;
be5c67c1
PA
8667 const struct syscall_catchpoint *c
8668 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8669
f90263c1
TT
8670 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8671 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8672 return 0;
8673
f90263c1
TT
8674 syscall_number = ws->value.syscall_number;
8675
a96d9b2e 8676 /* Now, checking if the syscall is the same. */
be5c67c1 8677 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8678 {
8679 int i, iter;
cc59ec59 8680
a96d9b2e 8681 for (i = 0;
be5c67c1 8682 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8683 i++)
8684 if (syscall_number == iter)
4924df79
GKB
8685 return 1;
8686
8687 return 0;
a96d9b2e
SDJ
8688 }
8689
8690 return 1;
8691}
8692
8693/* Implement the "print_it" breakpoint_ops method for syscall
8694 catchpoints. */
8695
8696static enum print_stop_action
348d480f 8697print_it_catch_syscall (bpstat bs)
a96d9b2e 8698{
36dfb11c 8699 struct ui_out *uiout = current_uiout;
348d480f 8700 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8701 /* These are needed because we want to know in which state a
8702 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8703 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8704 must print "called syscall" or "returned from syscall". */
8705 ptid_t ptid;
8706 struct target_waitstatus last;
8707 struct syscall s;
a96d9b2e
SDJ
8708
8709 get_last_target_status (&ptid, &last);
8710
8711 get_syscall_by_number (last.value.syscall_number, &s);
8712
8713 annotate_catchpoint (b->number);
8714
36dfb11c
TT
8715 if (b->disposition == disp_del)
8716 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8717 else
36dfb11c
TT
8718 ui_out_text (uiout, "\nCatchpoint ");
8719 if (ui_out_is_mi_like_p (uiout))
8720 {
8721 ui_out_field_string (uiout, "reason",
8722 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8723 ? EXEC_ASYNC_SYSCALL_ENTRY
8724 : EXEC_ASYNC_SYSCALL_RETURN));
8725 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8726 }
8727 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8728
8729 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8730 ui_out_text (uiout, " (call to syscall ");
8731 else
8732 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8733
36dfb11c
TT
8734 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8735 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8736 if (s.name != NULL)
8737 ui_out_field_string (uiout, "syscall-name", s.name);
8738
8739 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8740
8741 return PRINT_SRC_AND_LOC;
8742}
8743
8744/* Implement the "print_one" breakpoint_ops method for syscall
8745 catchpoints. */
8746
8747static void
8748print_one_catch_syscall (struct breakpoint *b,
f1310107 8749 struct bp_location **last_loc)
a96d9b2e 8750{
be5c67c1 8751 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8752 struct value_print_options opts;
79a45e25 8753 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8754
8755 get_user_print_options (&opts);
4a64f543
MS
8756 /* Field 4, the address, is omitted (which makes the columns not
8757 line up too nicely with the headers, but the effect is relatively
8758 readable). */
a96d9b2e
SDJ
8759 if (opts.addressprint)
8760 ui_out_field_skip (uiout, "addr");
8761 annotate_field (5);
8762
be5c67c1
PA
8763 if (c->syscalls_to_be_caught
8764 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8765 ui_out_text (uiout, "syscalls \"");
8766 else
8767 ui_out_text (uiout, "syscall \"");
8768
be5c67c1 8769 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8770 {
8771 int i, iter;
8772 char *text = xstrprintf ("%s", "");
cc59ec59 8773
a96d9b2e 8774 for (i = 0;
be5c67c1 8775 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8776 i++)
8777 {
8778 char *x = text;
8779 struct syscall s;
8780 get_syscall_by_number (iter, &s);
8781
8782 if (s.name != NULL)
8783 text = xstrprintf ("%s%s, ", text, s.name);
8784 else
8785 text = xstrprintf ("%s%d, ", text, iter);
8786
8787 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8788 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8789 on every call. */
8790 xfree (x);
8791 }
8792 /* Remove the last comma. */
8793 text[strlen (text) - 2] = '\0';
8794 ui_out_field_string (uiout, "what", text);
8795 }
8796 else
8797 ui_out_field_string (uiout, "what", "<any syscall>");
8798 ui_out_text (uiout, "\" ");
8ac3646f
TT
8799
8800 if (ui_out_is_mi_like_p (uiout))
8801 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8802}
8803
8804/* Implement the "print_mention" breakpoint_ops method for syscall
8805 catchpoints. */
8806
8807static void
8808print_mention_catch_syscall (struct breakpoint *b)
8809{
be5c67c1
PA
8810 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8811
8812 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8813 {
8814 int i, iter;
8815
be5c67c1 8816 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8817 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8818 else
8819 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8820
8821 for (i = 0;
be5c67c1 8822 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8823 i++)
8824 {
8825 struct syscall s;
8826 get_syscall_by_number (iter, &s);
8827
8828 if (s.name)
8829 printf_filtered (" '%s' [%d]", s.name, s.number);
8830 else
8831 printf_filtered (" %d", s.number);
8832 }
8833 printf_filtered (")");
8834 }
8835 else
8836 printf_filtered (_("Catchpoint %d (any syscall)"),
8837 b->number);
8838}
8839
6149aea9
PA
8840/* Implement the "print_recreate" breakpoint_ops method for syscall
8841 catchpoints. */
8842
8843static void
8844print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8845{
be5c67c1
PA
8846 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8847
6149aea9
PA
8848 fprintf_unfiltered (fp, "catch syscall");
8849
be5c67c1 8850 if (c->syscalls_to_be_caught)
6149aea9
PA
8851 {
8852 int i, iter;
8853
8854 for (i = 0;
be5c67c1 8855 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8856 i++)
8857 {
8858 struct syscall s;
8859
8860 get_syscall_by_number (iter, &s);
8861 if (s.name)
8862 fprintf_unfiltered (fp, " %s", s.name);
8863 else
8864 fprintf_unfiltered (fp, " %d", s.number);
8865 }
8866 }
d9b3f62e 8867 print_recreate_thread (b, fp);
6149aea9
PA
8868}
8869
a96d9b2e
SDJ
8870/* The breakpoint_ops structure to be used in syscall catchpoints. */
8871
2060206e 8872static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8873
8874/* Returns non-zero if 'b' is a syscall catchpoint. */
8875
8876static int
8877syscall_catchpoint_p (struct breakpoint *b)
8878{
8879 return (b->ops == &catch_syscall_breakpoint_ops);
8880}
8881
346774a9
PA
8882/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8883 is non-zero, then make the breakpoint temporary. If COND_STRING is
8884 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8885 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8886
ab04a2af 8887void
346774a9
PA
8888init_catchpoint (struct breakpoint *b,
8889 struct gdbarch *gdbarch, int tempflag,
8890 char *cond_string,
c0a91b2b 8891 const struct breakpoint_ops *ops)
c906108c 8892{
c5aa993b 8893 struct symtab_and_line sal;
346774a9 8894
fe39c653 8895 init_sal (&sal);
6c95b8df 8896 sal.pspace = current_program_space;
c5aa993b 8897
28010a5d 8898 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8899
1b36a34b 8900 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8901 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8902}
8903
28010a5d 8904void
3ea46bff 8905install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8906{
8907 add_to_breakpoint_chain (b);
3a5c3e22 8908 set_breakpoint_number (internal, b);
558a9d82
YQ
8909 if (is_tracepoint (b))
8910 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8911 if (!internal)
8912 mention (b);
c56053d2 8913 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8914
8915 if (update_gll)
44702360 8916 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8917}
8918
9b70b993 8919static void
a6d9a66e
UW
8920create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8921 int tempflag, char *cond_string,
c0a91b2b 8922 const struct breakpoint_ops *ops)
c906108c 8923{
e29a4733 8924 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8925
e29a4733
PA
8926 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8927
8928 c->forked_inferior_pid = null_ptid;
8929
3ea46bff 8930 install_breakpoint (0, &c->base, 1);
c906108c
SS
8931}
8932
fe798b75
JB
8933/* Exec catchpoints. */
8934
b4d90040
PA
8935/* An instance of this type is used to represent an exec catchpoint.
8936 It includes a "struct breakpoint" as a kind of base class; users
8937 downcast to "struct breakpoint *" when needed. A breakpoint is
8938 really of this type iff its ops pointer points to
8939 CATCH_EXEC_BREAKPOINT_OPS. */
8940
8941struct exec_catchpoint
8942{
8943 /* The base class. */
8944 struct breakpoint base;
8945
8946 /* Filename of a program whose exec triggered this catchpoint.
8947 This field is only valid immediately after this catchpoint has
8948 triggered. */
8949 char *exec_pathname;
8950};
8951
8952/* Implement the "dtor" breakpoint_ops method for exec
8953 catchpoints. */
8954
8955static void
8956dtor_catch_exec (struct breakpoint *b)
8957{
8958 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8959
8960 xfree (c->exec_pathname);
348d480f 8961
2060206e 8962 base_breakpoint_ops.dtor (b);
b4d90040
PA
8963}
8964
77b06cd7
TJB
8965static int
8966insert_catch_exec (struct bp_location *bl)
c906108c 8967{
dfd4cc63 8968 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8969}
c906108c 8970
fe798b75 8971static int
77b06cd7 8972remove_catch_exec (struct bp_location *bl)
fe798b75 8973{
dfd4cc63 8974 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8975}
c906108c 8976
fe798b75 8977static int
f1310107 8978breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8979 struct address_space *aspace, CORE_ADDR bp_addr,
8980 const struct target_waitstatus *ws)
fe798b75 8981{
b4d90040
PA
8982 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8983
f90263c1
TT
8984 if (ws->kind != TARGET_WAITKIND_EXECD)
8985 return 0;
8986
8987 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8988 return 1;
fe798b75 8989}
c906108c 8990
fe798b75 8991static enum print_stop_action
348d480f 8992print_it_catch_exec (bpstat bs)
fe798b75 8993{
36dfb11c 8994 struct ui_out *uiout = current_uiout;
348d480f 8995 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8996 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8997
fe798b75 8998 annotate_catchpoint (b->number);
36dfb11c
TT
8999 if (b->disposition == disp_del)
9000 ui_out_text (uiout, "\nTemporary catchpoint ");
9001 else
9002 ui_out_text (uiout, "\nCatchpoint ");
9003 if (ui_out_is_mi_like_p (uiout))
9004 {
9005 ui_out_field_string (uiout, "reason",
9006 async_reason_lookup (EXEC_ASYNC_EXEC));
9007 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9008 }
9009 ui_out_field_int (uiout, "bkptno", b->number);
9010 ui_out_text (uiout, " (exec'd ");
9011 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
9012 ui_out_text (uiout, "), ");
9013
fe798b75 9014 return PRINT_SRC_AND_LOC;
c906108c
SS
9015}
9016
fe798b75 9017static void
a6d9a66e 9018print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 9019{
b4d90040 9020 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 9021 struct value_print_options opts;
79a45e25 9022 struct ui_out *uiout = current_uiout;
fe798b75
JB
9023
9024 get_user_print_options (&opts);
9025
9026 /* Field 4, the address, is omitted (which makes the columns
9027 not line up too nicely with the headers, but the effect
9028 is relatively readable). */
9029 if (opts.addressprint)
9030 ui_out_field_skip (uiout, "addr");
9031 annotate_field (5);
9032 ui_out_text (uiout, "exec");
b4d90040 9033 if (c->exec_pathname != NULL)
fe798b75
JB
9034 {
9035 ui_out_text (uiout, ", program \"");
b4d90040 9036 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9037 ui_out_text (uiout, "\" ");
9038 }
8ac3646f
TT
9039
9040 if (ui_out_is_mi_like_p (uiout))
9041 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9042}
9043
9044static void
9045print_mention_catch_exec (struct breakpoint *b)
9046{
9047 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9048}
9049
6149aea9
PA
9050/* Implement the "print_recreate" breakpoint_ops method for exec
9051 catchpoints. */
9052
9053static void
9054print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9055{
9056 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9057 print_recreate_thread (b, fp);
6149aea9
PA
9058}
9059
2060206e 9060static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9061
a96d9b2e
SDJ
9062static void
9063create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9064 const struct breakpoint_ops *ops)
a96d9b2e 9065{
be5c67c1 9066 struct syscall_catchpoint *c;
a96d9b2e 9067 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9068
be5c67c1
PA
9069 c = XNEW (struct syscall_catchpoint);
9070 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9071 c->syscalls_to_be_caught = filter;
a96d9b2e 9072
3ea46bff 9073 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9074}
9075
c906108c 9076static int
fba45db2 9077hw_breakpoint_used_count (void)
c906108c 9078{
c906108c 9079 int i = 0;
f1310107
TJB
9080 struct breakpoint *b;
9081 struct bp_location *bl;
c906108c
SS
9082
9083 ALL_BREAKPOINTS (b)
c5aa993b 9084 {
d6b74ac4 9085 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9086 for (bl = b->loc; bl; bl = bl->next)
9087 {
9088 /* Special types of hardware breakpoints may use more than
9089 one register. */
348d480f 9090 i += b->ops->resources_needed (bl);
f1310107 9091 }
c5aa993b 9092 }
c906108c
SS
9093
9094 return i;
9095}
9096
a1398e0c
PA
9097/* Returns the resources B would use if it were a hardware
9098 watchpoint. */
9099
c906108c 9100static int
a1398e0c 9101hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9102{
c906108c 9103 int i = 0;
e09342b5 9104 struct bp_location *bl;
c906108c 9105
a1398e0c
PA
9106 if (!breakpoint_enabled (b))
9107 return 0;
9108
9109 for (bl = b->loc; bl; bl = bl->next)
9110 {
9111 /* Special types of hardware watchpoints may use more than
9112 one register. */
9113 i += b->ops->resources_needed (bl);
9114 }
9115
9116 return i;
9117}
9118
9119/* Returns the sum the used resources of all hardware watchpoints of
9120 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9121 the sum of the used resources of all hardware watchpoints of other
9122 types _not_ TYPE. */
9123
9124static int
9125hw_watchpoint_used_count_others (struct breakpoint *except,
9126 enum bptype type, int *other_type_used)
9127{
9128 int i = 0;
9129 struct breakpoint *b;
9130
c906108c
SS
9131 *other_type_used = 0;
9132 ALL_BREAKPOINTS (b)
e09342b5 9133 {
a1398e0c
PA
9134 if (b == except)
9135 continue;
e09342b5
TJB
9136 if (!breakpoint_enabled (b))
9137 continue;
9138
a1398e0c
PA
9139 if (b->type == type)
9140 i += hw_watchpoint_use_count (b);
9141 else if (is_hardware_watchpoint (b))
9142 *other_type_used = 1;
e09342b5
TJB
9143 }
9144
c906108c
SS
9145 return i;
9146}
9147
c906108c 9148void
fba45db2 9149disable_watchpoints_before_interactive_call_start (void)
c906108c 9150{
c5aa993b 9151 struct breakpoint *b;
c906108c
SS
9152
9153 ALL_BREAKPOINTS (b)
c5aa993b 9154 {
cc60f2e3 9155 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9156 {
b5de0fa7 9157 b->enable_state = bp_call_disabled;
44702360 9158 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9159 }
9160 }
c906108c
SS
9161}
9162
9163void
fba45db2 9164enable_watchpoints_after_interactive_call_stop (void)
c906108c 9165{
c5aa993b 9166 struct breakpoint *b;
c906108c
SS
9167
9168 ALL_BREAKPOINTS (b)
c5aa993b 9169 {
cc60f2e3 9170 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9171 {
b5de0fa7 9172 b->enable_state = bp_enabled;
44702360 9173 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9174 }
9175 }
c906108c
SS
9176}
9177
8bea4e01
UW
9178void
9179disable_breakpoints_before_startup (void)
9180{
6c95b8df 9181 current_program_space->executing_startup = 1;
44702360 9182 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9183}
9184
9185void
9186enable_breakpoints_after_startup (void)
9187{
6c95b8df 9188 current_program_space->executing_startup = 0;
f8eba3c6 9189 breakpoint_re_set ();
8bea4e01
UW
9190}
9191
c906108c
SS
9192
9193/* Set a breakpoint that will evaporate an end of command
9194 at address specified by SAL.
9195 Restrict it to frame FRAME if FRAME is nonzero. */
9196
9197struct breakpoint *
a6d9a66e
UW
9198set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9199 struct frame_id frame_id, enum bptype type)
c906108c 9200{
52f0bd74 9201 struct breakpoint *b;
edb3359d 9202
193facb3
JK
9203 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9204 tail-called one. */
9205 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9206
06edf0c0 9207 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9208 b->enable_state = bp_enabled;
9209 b->disposition = disp_donttouch;
818dd999 9210 b->frame_id = frame_id;
c906108c 9211
4a64f543
MS
9212 /* If we're debugging a multi-threaded program, then we want
9213 momentary breakpoints to be active in only a single thread of
9214 control. */
39f77062
KB
9215 if (in_thread_list (inferior_ptid))
9216 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9217
44702360 9218 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9219
c906108c
SS
9220 return b;
9221}
611c83ae 9222
06edf0c0 9223/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9224 The new breakpoint will have type TYPE, use OPS as its
9225 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9226
06edf0c0
PA
9227static struct breakpoint *
9228momentary_breakpoint_from_master (struct breakpoint *orig,
9229 enum bptype type,
a1aa2221
LM
9230 const struct breakpoint_ops *ops,
9231 int loc_enabled)
e58b0e63
PA
9232{
9233 struct breakpoint *copy;
9234
06edf0c0 9235 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9236 copy->loc = allocate_bp_location (copy);
0e30163f 9237 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9238
a6d9a66e 9239 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9240 copy->loc->requested_address = orig->loc->requested_address;
9241 copy->loc->address = orig->loc->address;
9242 copy->loc->section = orig->loc->section;
6c95b8df 9243 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9244 copy->loc->probe = orig->loc->probe;
f8eba3c6 9245 copy->loc->line_number = orig->loc->line_number;
2f202fde 9246 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9247 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9248 copy->frame_id = orig->frame_id;
9249 copy->thread = orig->thread;
6c95b8df 9250 copy->pspace = orig->pspace;
e58b0e63
PA
9251
9252 copy->enable_state = bp_enabled;
9253 copy->disposition = disp_donttouch;
9254 copy->number = internal_breakpoint_number--;
9255
44702360 9256 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9257 return copy;
9258}
9259
06edf0c0
PA
9260/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9261 ORIG is NULL. */
9262
9263struct breakpoint *
9264clone_momentary_breakpoint (struct breakpoint *orig)
9265{
9266 /* If there's nothing to clone, then return nothing. */
9267 if (orig == NULL)
9268 return NULL;
9269
a1aa2221 9270 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9271}
9272
611c83ae 9273struct breakpoint *
a6d9a66e
UW
9274set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9275 enum bptype type)
611c83ae
PA
9276{
9277 struct symtab_and_line sal;
9278
9279 sal = find_pc_line (pc, 0);
9280 sal.pc = pc;
9281 sal.section = find_pc_overlay (pc);
9282 sal.explicit_pc = 1;
9283
a6d9a66e 9284 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9285}
c906108c 9286\f
c5aa993b 9287
c906108c
SS
9288/* Tell the user we have just set a breakpoint B. */
9289
9290static void
fba45db2 9291mention (struct breakpoint *b)
c906108c 9292{
348d480f 9293 b->ops->print_mention (b);
79a45e25 9294 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9295 return;
c906108c
SS
9296 printf_filtered ("\n");
9297}
c906108c 9298\f
c5aa993b 9299
0d381245 9300static struct bp_location *
39d61571 9301add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9302 const struct symtab_and_line *sal)
9303{
9304 struct bp_location *loc, **tmp;
3742cc8b
YQ
9305 CORE_ADDR adjusted_address;
9306 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9307
9308 if (loc_gdbarch == NULL)
9309 loc_gdbarch = b->gdbarch;
9310
9311 /* Adjust the breakpoint's address prior to allocating a location.
9312 Once we call allocate_bp_location(), that mostly uninitialized
9313 location will be placed on the location chain. Adjustment of the
9314 breakpoint may cause target_read_memory() to be called and we do
9315 not want its scan of the location chain to find a breakpoint and
9316 location that's only been partially initialized. */
9317 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9318 sal->pc, b->type);
0d381245 9319
d30113d4 9320 /* Sort the locations by their ADDRESS. */
39d61571 9321 loc = allocate_bp_location (b);
d30113d4
JK
9322 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9323 tmp = &((*tmp)->next))
0d381245 9324 ;
d30113d4 9325 loc->next = *tmp;
0d381245 9326 *tmp = loc;
3742cc8b 9327
0d381245 9328 loc->requested_address = sal->pc;
3742cc8b 9329 loc->address = adjusted_address;
6c95b8df 9330 loc->pspace = sal->pspace;
729662a5
TT
9331 loc->probe.probe = sal->probe;
9332 loc->probe.objfile = sal->objfile;
6c95b8df 9333 gdb_assert (loc->pspace != NULL);
0d381245 9334 loc->section = sal->section;
3742cc8b 9335 loc->gdbarch = loc_gdbarch;
f8eba3c6 9336 loc->line_number = sal->line;
2f202fde 9337 loc->symtab = sal->symtab;
f8eba3c6 9338
0e30163f
JK
9339 set_breakpoint_location_function (loc,
9340 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9341 return loc;
9342}
514f746b
AR
9343\f
9344
9345/* Return 1 if LOC is pointing to a permanent breakpoint,
9346 return 0 otherwise. */
9347
9348static int
9349bp_loc_is_permanent (struct bp_location *loc)
9350{
9351 int len;
9352 CORE_ADDR addr;
1afeeb75 9353 const gdb_byte *bpoint;
514f746b 9354 gdb_byte *target_mem;
939c61fa
JK
9355 struct cleanup *cleanup;
9356 int retval = 0;
514f746b
AR
9357
9358 gdb_assert (loc != NULL);
9359
9360 addr = loc->address;
1afeeb75 9361 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9362
939c61fa 9363 /* Software breakpoints unsupported? */
1afeeb75 9364 if (bpoint == NULL)
939c61fa
JK
9365 return 0;
9366
514f746b
AR
9367 target_mem = alloca (len);
9368
939c61fa
JK
9369 /* Enable the automatic memory restoration from breakpoints while
9370 we read the memory. Otherwise we could say about our temporary
9371 breakpoints they are permanent. */
6c95b8df
PA
9372 cleanup = save_current_space_and_thread ();
9373
9374 switch_to_program_space_and_thread (loc->pspace);
9375 make_show_memory_breakpoints_cleanup (0);
939c61fa 9376
514f746b 9377 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9378 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9379 retval = 1;
514f746b 9380
939c61fa
JK
9381 do_cleanups (cleanup);
9382
9383 return retval;
514f746b
AR
9384}
9385
e7e0cddf
SS
9386/* Build a command list for the dprintf corresponding to the current
9387 settings of the dprintf style options. */
9388
9389static void
9390update_dprintf_command_list (struct breakpoint *b)
9391{
9392 char *dprintf_args = b->extra_string;
9393 char *printf_line = NULL;
9394
9395 if (!dprintf_args)
9396 return;
9397
9398 dprintf_args = skip_spaces (dprintf_args);
9399
9400 /* Allow a comma, as it may have terminated a location, but don't
9401 insist on it. */
9402 if (*dprintf_args == ',')
9403 ++dprintf_args;
9404 dprintf_args = skip_spaces (dprintf_args);
9405
9406 if (*dprintf_args != '"')
9407 error (_("Bad format string, missing '\"'."));
9408
d3ce09f5 9409 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9410 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9411 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9412 {
9413 if (!dprintf_function)
9414 error (_("No function supplied for dprintf call"));
9415
9416 if (dprintf_channel && strlen (dprintf_channel) > 0)
9417 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9418 dprintf_function,
9419 dprintf_channel,
9420 dprintf_args);
9421 else
9422 printf_line = xstrprintf ("call (void) %s (%s)",
9423 dprintf_function,
9424 dprintf_args);
9425 }
d3ce09f5
SS
9426 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9427 {
9428 if (target_can_run_breakpoint_commands ())
9429 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9430 else
9431 {
9432 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9433 printf_line = xstrprintf ("printf %s", dprintf_args);
9434 }
9435 }
e7e0cddf
SS
9436 else
9437 internal_error (__FILE__, __LINE__,
9438 _("Invalid dprintf style."));
9439
f28045c2 9440 gdb_assert (printf_line != NULL);
9d6e6e84 9441 /* Manufacture a printf sequence. */
f28045c2 9442 {
9d6e6e84
HZ
9443 struct command_line *printf_cmd_line
9444 = xmalloc (sizeof (struct command_line));
e7e0cddf 9445
f28045c2
YQ
9446 printf_cmd_line = xmalloc (sizeof (struct command_line));
9447 printf_cmd_line->control_type = simple_control;
9448 printf_cmd_line->body_count = 0;
9449 printf_cmd_line->body_list = NULL;
9d6e6e84 9450 printf_cmd_line->next = NULL;
f28045c2 9451 printf_cmd_line->line = printf_line;
e7e0cddf 9452
f28045c2
YQ
9453 breakpoint_set_commands (b, printf_cmd_line);
9454 }
e7e0cddf
SS
9455}
9456
9457/* Update all dprintf commands, making their command lists reflect
9458 current style settings. */
9459
9460static void
9461update_dprintf_commands (char *args, int from_tty,
9462 struct cmd_list_element *c)
9463{
9464 struct breakpoint *b;
9465
9466 ALL_BREAKPOINTS (b)
9467 {
9468 if (b->type == bp_dprintf)
9469 update_dprintf_command_list (b);
9470 }
9471}
c3f6f71d 9472
018d34a4
VP
9473/* Create a breakpoint with SAL as location. Use ADDR_STRING
9474 as textual description of the location, and COND_STRING
db107f19 9475 as condition expression. */
018d34a4
VP
9476
9477static void
d9b3f62e
PA
9478init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9479 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9480 char *filter, char *cond_string,
e7e0cddf 9481 char *extra_string,
d9b3f62e
PA
9482 enum bptype type, enum bpdisp disposition,
9483 int thread, int task, int ignore_count,
c0a91b2b 9484 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9485 int enabled, int internal, unsigned flags,
9486 int display_canonical)
018d34a4 9487{
0d381245 9488 int i;
018d34a4
VP
9489
9490 if (type == bp_hardware_breakpoint)
9491 {
fbbd034e
AS
9492 int target_resources_ok;
9493
9494 i = hw_breakpoint_used_count ();
9495 target_resources_ok =
9496 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9497 i + 1, 0);
9498 if (target_resources_ok == 0)
9499 error (_("No hardware breakpoint support in the target."));
9500 else if (target_resources_ok < 0)
9501 error (_("Hardware breakpoints used exceeds limit."));
9502 }
9503
6c95b8df
PA
9504 gdb_assert (sals.nelts > 0);
9505
0d381245
VP
9506 for (i = 0; i < sals.nelts; ++i)
9507 {
9508 struct symtab_and_line sal = sals.sals[i];
9509 struct bp_location *loc;
9510
9511 if (from_tty)
5af949e3
UW
9512 {
9513 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9514 if (!loc_gdbarch)
9515 loc_gdbarch = gdbarch;
9516
9517 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9518 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9519 }
0d381245
VP
9520
9521 if (i == 0)
9522 {
d9b3f62e 9523 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9524 b->thread = thread;
4a306c9a 9525 b->task = task;
855a6e68 9526
0d381245 9527 b->cond_string = cond_string;
e7e0cddf 9528 b->extra_string = extra_string;
0d381245 9529 b->ignore_count = ignore_count;
41447f92 9530 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9531 b->disposition = disposition;
6c95b8df 9532
44f238bb
PA
9533 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9534 b->loc->inserted = 1;
9535
0fb4aa4b
PA
9536 if (type == bp_static_tracepoint)
9537 {
d9b3f62e 9538 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9539 struct static_tracepoint_marker marker;
9540
983af33b 9541 if (strace_marker_p (b))
0fb4aa4b
PA
9542 {
9543 /* We already know the marker exists, otherwise, we
9544 wouldn't see a sal for it. */
9545 char *p = &addr_string[3];
9546 char *endp;
9547 char *marker_str;
0fb4aa4b 9548
e9cafbcc 9549 p = skip_spaces (p);
0fb4aa4b 9550
e9cafbcc 9551 endp = skip_to_space (p);
0fb4aa4b
PA
9552
9553 marker_str = savestring (p, endp - p);
d9b3f62e 9554 t->static_trace_marker_id = marker_str;
0fb4aa4b 9555
3e43a32a
MS
9556 printf_filtered (_("Probed static tracepoint "
9557 "marker \"%s\"\n"),
d9b3f62e 9558 t->static_trace_marker_id);
0fb4aa4b
PA
9559 }
9560 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9561 {
d9b3f62e 9562 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9563 release_static_tracepoint_marker (&marker);
9564
3e43a32a
MS
9565 printf_filtered (_("Probed static tracepoint "
9566 "marker \"%s\"\n"),
d9b3f62e 9567 t->static_trace_marker_id);
0fb4aa4b
PA
9568 }
9569 else
3e43a32a
MS
9570 warning (_("Couldn't determine the static "
9571 "tracepoint marker to probe"));
0fb4aa4b
PA
9572 }
9573
0d381245
VP
9574 loc = b->loc;
9575 }
9576 else
018d34a4 9577 {
39d61571 9578 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9579 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9580 loc->inserted = 1;
0d381245
VP
9581 }
9582
514f746b
AR
9583 if (bp_loc_is_permanent (loc))
9584 make_breakpoint_permanent (b);
9585
0d381245
VP
9586 if (b->cond_string)
9587 {
bbc13ae3
KS
9588 const char *arg = b->cond_string;
9589
1bb9788d
TT
9590 loc->cond = parse_exp_1 (&arg, loc->address,
9591 block_for_pc (loc->address), 0);
0d381245 9592 if (*arg)
588ae58c 9593 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9594 }
e7e0cddf
SS
9595
9596 /* Dynamic printf requires and uses additional arguments on the
9597 command line, otherwise it's an error. */
9598 if (type == bp_dprintf)
9599 {
9600 if (b->extra_string)
9601 update_dprintf_command_list (b);
9602 else
9603 error (_("Format string required"));
9604 }
9605 else if (b->extra_string)
588ae58c 9606 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9607 }
018d34a4 9608
56435ebe 9609 b->display_canonical = display_canonical;
018d34a4
VP
9610 if (addr_string)
9611 b->addr_string = addr_string;
9612 else
9613 /* addr_string has to be used or breakpoint_re_set will delete
9614 me. */
5af949e3
UW
9615 b->addr_string
9616 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9617 b->filter = filter;
d9b3f62e 9618}
018d34a4 9619
d9b3f62e
PA
9620static void
9621create_breakpoint_sal (struct gdbarch *gdbarch,
9622 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9623 char *filter, char *cond_string,
e7e0cddf 9624 char *extra_string,
d9b3f62e
PA
9625 enum bptype type, enum bpdisp disposition,
9626 int thread, int task, int ignore_count,
c0a91b2b 9627 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9628 int enabled, int internal, unsigned flags,
9629 int display_canonical)
d9b3f62e
PA
9630{
9631 struct breakpoint *b;
9632 struct cleanup *old_chain;
9633
9634 if (is_tracepoint_type (type))
9635 {
9636 struct tracepoint *t;
9637
9638 t = XCNEW (struct tracepoint);
9639 b = &t->base;
9640 }
9641 else
9642 b = XNEW (struct breakpoint);
9643
9644 old_chain = make_cleanup (xfree, b);
9645
9646 init_breakpoint_sal (b, gdbarch,
9647 sals, addr_string,
e7e0cddf 9648 filter, cond_string, extra_string,
d9b3f62e
PA
9649 type, disposition,
9650 thread, task, ignore_count,
9651 ops, from_tty,
44f238bb
PA
9652 enabled, internal, flags,
9653 display_canonical);
d9b3f62e
PA
9654 discard_cleanups (old_chain);
9655
3ea46bff 9656 install_breakpoint (internal, b, 0);
018d34a4
VP
9657}
9658
9659/* Add SALS.nelts breakpoints to the breakpoint table. For each
9660 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9661 value. COND_STRING, if not NULL, specified the condition to be
9662 used for all breakpoints. Essentially the only case where
9663 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9664 function. In that case, it's still not possible to specify
9665 separate conditions for different overloaded functions, so
9666 we take just a single condition string.
9667
c3f6f71d 9668 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9669 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9670 array contents). If the function fails (error() is called), the
9671 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9672 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9673
9674static void
8cdf0e15 9675create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9676 struct linespec_result *canonical,
e7e0cddf 9677 char *cond_string, char *extra_string,
8cdf0e15
VP
9678 enum bptype type, enum bpdisp disposition,
9679 int thread, int task, int ignore_count,
c0a91b2b 9680 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9681 int enabled, int internal, unsigned flags)
c906108c 9682{
018d34a4 9683 int i;
f8eba3c6 9684 struct linespec_sals *lsal;
cc59ec59 9685
f8eba3c6
TT
9686 if (canonical->pre_expanded)
9687 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9688
9689 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9690 {
f8eba3c6
TT
9691 /* Note that 'addr_string' can be NULL in the case of a plain
9692 'break', without arguments. */
9693 char *addr_string = (canonical->addr_string
9694 ? xstrdup (canonical->addr_string)
9695 : NULL);
9696 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9697 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9698
f8eba3c6
TT
9699 make_cleanup (xfree, filter_string);
9700 create_breakpoint_sal (gdbarch, lsal->sals,
9701 addr_string,
9702 filter_string,
e7e0cddf
SS
9703 cond_string, extra_string,
9704 type, disposition,
84f4c1fe 9705 thread, task, ignore_count, ops,
44f238bb 9706 from_tty, enabled, internal, flags,
56435ebe 9707 canonical->special_display);
f8eba3c6 9708 discard_cleanups (inner);
c3f6f71d 9709 }
c3f6f71d 9710}
c906108c 9711
9998af43 9712/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9713 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9714 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9715 address strings. ADDRESS points to the end of the SAL.
9716
9717 The array and the line spec strings are allocated on the heap, it is
9718 the caller's responsibility to free them. */
c906108c 9719
b9362cc7 9720static void
c3f6f71d 9721parse_breakpoint_sals (char **address,
58438ac1 9722 struct linespec_result *canonical)
c3f6f71d 9723{
c3f6f71d 9724 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9725 breakpoint. */
c3f6f71d
JM
9726 if ((*address) == NULL
9727 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9728 {
1bfeeb0f
JL
9729 /* The last displayed codepoint, if it's valid, is our default breakpoint
9730 address. */
9731 if (last_displayed_sal_is_valid ())
c906108c 9732 {
f8eba3c6 9733 struct linespec_sals lsal;
c3f6f71d 9734 struct symtab_and_line sal;
1c8cdcb1 9735 CORE_ADDR pc;
cc59ec59 9736
4a64f543 9737 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9738 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9739 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9740
9741 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9742 corresponding to the last call to print_frame_info.
9743 Be sure to reinitialize LINE with NOTCURRENT == 0
9744 as the breakpoint line number is inappropriate otherwise.
9745 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9746 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9747 pc = sal.pc;
9748 sal = find_pc_line (pc, 0);
00903456 9749
4a64f543 9750 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9751 where PC is the last displayed codepoint's address. So
9752 make sure to set sal.explicit_pc to prevent GDB from
9753 trying to expand the list of sals to include all other
9754 instances with the same symtab and line. */
1c8cdcb1 9755 sal.pc = pc;
00903456
JK
9756 sal.explicit_pc = 1;
9757
f8eba3c6
TT
9758 lsal.sals.sals[0] = sal;
9759 lsal.sals.nelts = 1;
9760 lsal.canonical = NULL;
9761
9762 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9763 }
9764 else
8a3fe4f8 9765 error (_("No default breakpoint address now."));
c906108c
SS
9766 }
9767 else
9768 {
cc80f267
JK
9769 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9770
c906108c 9771 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9772 current_source_symtab (which is decode_line_1's default).
9773 This should produce the results we want almost all of the
cc80f267
JK
9774 time while leaving default_breakpoint_* alone.
9775
9776 ObjC: However, don't match an Objective-C method name which
9777 may have a '+' or '-' succeeded by a '['. */
9778 if (last_displayed_sal_is_valid ()
9779 && (!cursal.symtab
9780 || ((strchr ("+-", (*address)[0]) != NULL)
9781 && ((*address)[1] != '['))))
f8eba3c6
TT
9782 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9783 get_last_displayed_symtab (),
9784 get_last_displayed_line (),
9785 canonical, NULL, NULL);
c906108c 9786 else
f8eba3c6 9787 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9788 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9789 }
c3f6f71d 9790}
c906108c 9791
c906108c 9792
c3f6f71d 9793/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9794 inserted as a breakpoint. If it can't throw an error. */
c906108c 9795
b9362cc7 9796static void
23e7acfb 9797breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9798{
9799 int i;
cc59ec59 9800
c3f6f71d 9801 for (i = 0; i < sals->nelts; i++)
ee53e872 9802 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9803}
9804
7a697b8d
SS
9805/* Fast tracepoints may have restrictions on valid locations. For
9806 instance, a fast tracepoint using a jump instead of a trap will
9807 likely have to overwrite more bytes than a trap would, and so can
9808 only be placed where the instruction is longer than the jump, or a
9809 multi-instruction sequence does not have a jump into the middle of
9810 it, etc. */
9811
9812static void
9813check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9814 struct symtabs_and_lines *sals)
9815{
9816 int i, rslt;
9817 struct symtab_and_line *sal;
9818 char *msg;
9819 struct cleanup *old_chain;
9820
9821 for (i = 0; i < sals->nelts; i++)
9822 {
f8eba3c6
TT
9823 struct gdbarch *sarch;
9824
7a697b8d
SS
9825 sal = &sals->sals[i];
9826
f8eba3c6
TT
9827 sarch = get_sal_arch (*sal);
9828 /* We fall back to GDBARCH if there is no architecture
9829 associated with SAL. */
9830 if (sarch == NULL)
9831 sarch = gdbarch;
9832 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9833 NULL, &msg);
9834 old_chain = make_cleanup (xfree, msg);
9835
9836 if (!rslt)
9837 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9838 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9839
9840 do_cleanups (old_chain);
9841 }
9842}
9843
af4908ba
KS
9844/* Issue an invalid thread ID error. */
9845
9846static void ATTRIBUTE_NORETURN
9847invalid_thread_id_error (int id)
9848{
9849 error (_("Unknown thread %d."), id);
9850}
9851
018d34a4
VP
9852/* Given TOK, a string specification of condition and thread, as
9853 accepted by the 'break' command, extract the condition
9854 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9855 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9856 If no condition is found, *COND_STRING is set to NULL.
9857 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9858
9859static void
bbc13ae3 9860find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9861 char **cond_string, int *thread, int *task,
9862 char **rest)
018d34a4
VP
9863{
9864 *cond_string = NULL;
9865 *thread = -1;
ed1d1739
KS
9866 *task = 0;
9867 *rest = NULL;
9868
018d34a4
VP
9869 while (tok && *tok)
9870 {
bbc13ae3 9871 const char *end_tok;
018d34a4 9872 int toklen;
bbc13ae3
KS
9873 const char *cond_start = NULL;
9874 const char *cond_end = NULL;
cc59ec59 9875
bbc13ae3 9876 tok = skip_spaces_const (tok);
e7e0cddf
SS
9877
9878 if ((*tok == '"' || *tok == ',') && rest)
9879 {
9880 *rest = savestring (tok, strlen (tok));
9881 return;
9882 }
9883
bbc13ae3 9884 end_tok = skip_to_space_const (tok);
d634f2de 9885
018d34a4 9886 toklen = end_tok - tok;
d634f2de 9887
018d34a4
VP
9888 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9889 {
f7545552
TT
9890 struct expression *expr;
9891
018d34a4 9892 tok = cond_start = end_tok + 1;
1bb9788d 9893 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9894 xfree (expr);
018d34a4 9895 cond_end = tok;
d634f2de 9896 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9897 }
9898 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9899 {
9900 char *tmptok;
d634f2de 9901
018d34a4 9902 tok = end_tok + 1;
bbc13ae3 9903 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9904 if (tok == tmptok)
9905 error (_("Junk after thread keyword."));
9906 if (!valid_thread_id (*thread))
af4908ba 9907 invalid_thread_id_error (*thread);
bbc13ae3 9908 tok = tmptok;
018d34a4 9909 }
4a306c9a
JB
9910 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9911 {
9912 char *tmptok;
9913
9914 tok = end_tok + 1;
bbc13ae3 9915 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9916 if (tok == tmptok)
9917 error (_("Junk after task keyword."));
9918 if (!valid_task_id (*task))
b6199126 9919 error (_("Unknown task %d."), *task);
bbc13ae3 9920 tok = tmptok;
4a306c9a 9921 }
e7e0cddf
SS
9922 else if (rest)
9923 {
9924 *rest = savestring (tok, strlen (tok));
ccab2054 9925 return;
e7e0cddf 9926 }
018d34a4
VP
9927 else
9928 error (_("Junk at end of arguments."));
9929 }
9930}
9931
0fb4aa4b
PA
9932/* Decode a static tracepoint marker spec. */
9933
9934static struct symtabs_and_lines
9935decode_static_tracepoint_spec (char **arg_p)
9936{
9937 VEC(static_tracepoint_marker_p) *markers = NULL;
9938 struct symtabs_and_lines sals;
0fb4aa4b
PA
9939 struct cleanup *old_chain;
9940 char *p = &(*arg_p)[3];
9941 char *endp;
9942 char *marker_str;
9943 int i;
9944
e9cafbcc 9945 p = skip_spaces (p);
0fb4aa4b 9946
e9cafbcc 9947 endp = skip_to_space (p);
0fb4aa4b
PA
9948
9949 marker_str = savestring (p, endp - p);
9950 old_chain = make_cleanup (xfree, marker_str);
9951
9952 markers = target_static_tracepoint_markers_by_strid (marker_str);
9953 if (VEC_empty(static_tracepoint_marker_p, markers))
9954 error (_("No known static tracepoint marker named %s"), marker_str);
9955
9956 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9957 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9958
9959 for (i = 0; i < sals.nelts; i++)
9960 {
9961 struct static_tracepoint_marker *marker;
9962
9963 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9964
9965 init_sal (&sals.sals[i]);
9966
9967 sals.sals[i] = find_pc_line (marker->address, 0);
9968 sals.sals[i].pc = marker->address;
9969
9970 release_static_tracepoint_marker (marker);
9971 }
9972
9973 do_cleanups (old_chain);
9974
9975 *arg_p = endp;
9976 return sals;
9977}
9978
fd9b8c24
PA
9979/* Set a breakpoint. This function is shared between CLI and MI
9980 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9981 modes of operations, selected by the PARSE_ARG parameter. If
9982 non-zero, the function will parse ARG, extracting location,
9983 condition, thread and extra string. Otherwise, ARG is just the
9984 breakpoint's location, with condition, thread, and extra string
9985 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9986 If INTERNAL is non-zero, the breakpoint number will be allocated
9987 from the internal breakpoint count. Returns true if any breakpoint
9988 was created; false otherwise. */
0101ce28 9989
8cdf0e15
VP
9990int
9991create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9992 char *arg, char *cond_string,
9993 int thread, char *extra_string,
f6de8ec2 9994 int parse_arg,
0fb4aa4b 9995 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9996 int ignore_count,
9997 enum auto_boolean pending_break_support,
c0a91b2b 9998 const struct breakpoint_ops *ops,
44f238bb
PA
9999 int from_tty, int enabled, int internal,
10000 unsigned flags)
c3f6f71d 10001{
b78a6381 10002 volatile struct gdb_exception e;
f8eba3c6 10003 char *copy_arg = NULL;
c3f6f71d 10004 char *addr_start = arg;
7efd8fc2 10005 struct linespec_result canonical;
c3f6f71d 10006 struct cleanup *old_chain;
80c99de1 10007 struct cleanup *bkpt_chain = NULL;
0101ce28 10008 int pending = 0;
4a306c9a 10009 int task = 0;
86b17b60 10010 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10011
348d480f
PA
10012 gdb_assert (ops != NULL);
10013
7efd8fc2 10014 init_linespec_result (&canonical);
c3f6f71d 10015
b78a6381
TT
10016 TRY_CATCH (e, RETURN_MASK_ALL)
10017 {
983af33b
SDJ
10018 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10019 addr_start, &copy_arg);
b78a6381 10020 }
0101ce28
JJ
10021
10022 /* If caller is interested in rc value from parse, set value. */
05ff989b 10023 switch (e.reason)
0101ce28 10024 {
983af33b
SDJ
10025 case GDB_NO_ERROR:
10026 if (VEC_empty (linespec_sals, canonical.sals))
10027 return 0;
10028 break;
05ff989b
AC
10029 case RETURN_ERROR:
10030 switch (e.error)
0101ce28 10031 {
05ff989b 10032 case NOT_FOUND_ERROR:
0101ce28 10033
05ff989b
AC
10034 /* If pending breakpoint support is turned off, throw
10035 error. */
fa8d40ab
JJ
10036
10037 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10038 throw_exception (e);
10039
10040 exception_print (gdb_stderr, e);
fa8d40ab 10041
05ff989b
AC
10042 /* If pending breakpoint support is auto query and the user
10043 selects no, then simply return the error code. */
059fb39f 10044 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10045 && !nquery (_("Make %s pending on future shared library load? "),
10046 bptype_string (type_wanted)))
fd9b8c24 10047 return 0;
fa8d40ab 10048
05ff989b
AC
10049 /* At this point, either the user was queried about setting
10050 a pending breakpoint and selected yes, or pending
10051 breakpoint behavior is on and thus a pending breakpoint
10052 is defaulted on behalf of the user. */
f8eba3c6
TT
10053 {
10054 struct linespec_sals lsal;
10055
10056 copy_arg = xstrdup (addr_start);
10057 lsal.canonical = xstrdup (copy_arg);
10058 lsal.sals.nelts = 1;
10059 lsal.sals.sals = XNEW (struct symtab_and_line);
10060 init_sal (&lsal.sals.sals[0]);
10061 pending = 1;
10062 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10063 }
05ff989b
AC
10064 break;
10065 default:
98deb0da 10066 throw_exception (e);
0101ce28 10067 }
2abae994 10068 break;
05ff989b 10069 default:
983af33b 10070 throw_exception (e);
0101ce28 10071 }
c3f6f71d 10072
4a64f543 10073 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10074 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10075
c3f6f71d
JM
10076 /* ----------------------------- SNIP -----------------------------
10077 Anything added to the cleanup chain beyond this point is assumed
10078 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10079 then the memory is not reclaimed. */
10080 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10081
c3f6f71d
JM
10082 /* Resolve all line numbers to PC's and verify that the addresses
10083 are ok for the target. */
0101ce28 10084 if (!pending)
f8eba3c6
TT
10085 {
10086 int ix;
10087 struct linespec_sals *iter;
10088
10089 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10090 breakpoint_sals_to_pc (&iter->sals);
10091 }
c3f6f71d 10092
7a697b8d 10093 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10094 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10095 {
10096 int ix;
10097 struct linespec_sals *iter;
10098
10099 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10100 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10101 }
7a697b8d 10102
c3f6f71d
JM
10103 /* Verify that condition can be parsed, before setting any
10104 breakpoints. Allocate a separate condition expression for each
4a64f543 10105 breakpoint. */
0101ce28 10106 if (!pending)
c3f6f71d 10107 {
f6de8ec2 10108 if (parse_arg)
72b2ff0e 10109 {
0878d0fa 10110 char *rest;
52d361e1
YQ
10111 struct linespec_sals *lsal;
10112
10113 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10114
0878d0fa
YQ
10115 /* Here we only parse 'arg' to separate condition
10116 from thread number, so parsing in context of first
10117 sal is OK. When setting the breakpoint we'll
10118 re-parse it in context of each sal. */
10119
10120 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10121 &thread, &task, &rest);
10122 if (cond_string)
10123 make_cleanup (xfree, cond_string);
10124 if (rest)
10125 make_cleanup (xfree, rest);
10126 if (rest)
10127 extra_string = rest;
72b2ff0e 10128 }
2f069f6f 10129 else
72b2ff0e 10130 {
0878d0fa
YQ
10131 if (*arg != '\0')
10132 error (_("Garbage '%s' at end of location"), arg);
10133
10134 /* Create a private copy of condition string. */
10135 if (cond_string)
10136 {
10137 cond_string = xstrdup (cond_string);
10138 make_cleanup (xfree, cond_string);
10139 }
10140 /* Create a private copy of any extra string. */
10141 if (extra_string)
10142 {
10143 extra_string = xstrdup (extra_string);
10144 make_cleanup (xfree, extra_string);
10145 }
72b2ff0e 10146 }
0fb4aa4b 10147
52d361e1 10148 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10149 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10150 tempflag ? disp_del : disp_donttouch,
10151 thread, task, ignore_count, ops,
44f238bb 10152 from_tty, enabled, internal, flags);
c906108c 10153 }
0101ce28
JJ
10154 else
10155 {
0101ce28
JJ
10156 struct breakpoint *b;
10157
0101ce28
JJ
10158 make_cleanup (xfree, copy_arg);
10159
bfccc43c
YQ
10160 if (is_tracepoint_type (type_wanted))
10161 {
10162 struct tracepoint *t;
10163
10164 t = XCNEW (struct tracepoint);
10165 b = &t->base;
10166 }
10167 else
10168 b = XNEW (struct breakpoint);
10169
10170 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10171
f8eba3c6 10172 b->addr_string = copy_arg;
f6de8ec2 10173 if (parse_arg)
e12c7713
MK
10174 b->cond_string = NULL;
10175 else
10176 {
10177 /* Create a private copy of condition string. */
10178 if (cond_string)
10179 {
10180 cond_string = xstrdup (cond_string);
10181 make_cleanup (xfree, cond_string);
10182 }
10183 b->cond_string = cond_string;
10184 }
e7e0cddf 10185 b->extra_string = NULL;
0101ce28 10186 b->ignore_count = ignore_count;
0101ce28 10187 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10188 b->condition_not_parsed = 1;
41447f92 10189 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10190 if ((type_wanted != bp_breakpoint
10191 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10192 b->pspace = current_program_space;
8bea4e01 10193
bfccc43c 10194 install_breakpoint (internal, b, 0);
0101ce28
JJ
10195 }
10196
f8eba3c6 10197 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10198 {
3e43a32a
MS
10199 warning (_("Multiple breakpoints were set.\nUse the "
10200 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10201 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10202 }
10203
80c99de1
PA
10204 /* That's it. Discard the cleanups for data inserted into the
10205 breakpoint. */
10206 discard_cleanups (bkpt_chain);
10207 /* But cleanup everything else. */
c3f6f71d 10208 do_cleanups (old_chain);
217dc9e2 10209
80c99de1 10210 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10211 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10212
10213 return 1;
c3f6f71d 10214}
c906108c 10215
348d480f 10216/* Set a breakpoint.
72b2ff0e
VP
10217 ARG is a string describing breakpoint address,
10218 condition, and thread.
10219 FLAG specifies if a breakpoint is hardware on,
10220 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10221 and BP_TEMPFLAG. */
348d480f 10222
98deb0da 10223static void
72b2ff0e 10224break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10225{
72b2ff0e 10226 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10227 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10228 ? bp_hardware_breakpoint
10229 : bp_breakpoint);
55aa24fb
SDJ
10230 struct breakpoint_ops *ops;
10231 const char *arg_cp = arg;
10232
10233 /* Matching breakpoints on probes. */
10234 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10235 ops = &bkpt_probe_breakpoint_ops;
10236 else
10237 ops = &bkpt_breakpoint_ops;
c3f6f71d 10238
8cdf0e15
VP
10239 create_breakpoint (get_current_arch (),
10240 arg,
e7e0cddf 10241 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10242 tempflag, type_wanted,
8cdf0e15
VP
10243 0 /* Ignore count */,
10244 pending_break_support,
55aa24fb 10245 ops,
8cdf0e15 10246 from_tty,
84f4c1fe 10247 1 /* enabled */,
44f238bb
PA
10248 0 /* internal */,
10249 0);
c906108c
SS
10250}
10251
c906108c
SS
10252/* Helper function for break_command_1 and disassemble_command. */
10253
10254void
fba45db2 10255resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10256{
10257 CORE_ADDR pc;
10258
10259 if (sal->pc == 0 && sal->symtab != NULL)
10260 {
10261 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10262 error (_("No line %d in file \"%s\"."),
05cba821 10263 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10264 sal->pc = pc;
6a048695 10265
4a64f543
MS
10266 /* If this SAL corresponds to a breakpoint inserted using a line
10267 number, then skip the function prologue if necessary. */
6a048695 10268 if (sal->explicit_line)
059acae7 10269 skip_prologue_sal (sal);
c906108c
SS
10270 }
10271
10272 if (sal->section == 0 && sal->symtab != NULL)
10273 {
346d1dfe 10274 const struct blockvector *bv;
3977b71f 10275 const struct block *b;
c5aa993b 10276 struct symbol *sym;
c906108c 10277
801e3a5b 10278 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10279 if (bv != NULL)
10280 {
7f0df278 10281 sym = block_linkage_function (b);
c906108c
SS
10282 if (sym != NULL)
10283 {
10284 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10285 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10286 }
10287 else
10288 {
4a64f543
MS
10289 /* It really is worthwhile to have the section, so we'll
10290 just have to look harder. This case can be executed
10291 if we have line numbers but no functions (as can
10292 happen in assembly source). */
c906108c 10293
7cbd4a93 10294 struct bound_minimal_symbol msym;
6c95b8df
PA
10295 struct cleanup *old_chain = save_current_space_and_thread ();
10296
10297 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10298
10299 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10300 if (msym.minsym)
efd66ac6 10301 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10302
10303 do_cleanups (old_chain);
c906108c
SS
10304 }
10305 }
10306 }
10307}
10308
10309void
fba45db2 10310break_command (char *arg, int from_tty)
c906108c 10311{
db107f19 10312 break_command_1 (arg, 0, from_tty);
c906108c
SS
10313}
10314
c906108c 10315void
fba45db2 10316tbreak_command (char *arg, int from_tty)
c906108c 10317{
db107f19 10318 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10319}
10320
c906108c 10321static void
fba45db2 10322hbreak_command (char *arg, int from_tty)
c906108c 10323{
db107f19 10324 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10325}
10326
10327static void
fba45db2 10328thbreak_command (char *arg, int from_tty)
c906108c 10329{
db107f19 10330 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10331}
10332
10333static void
fba45db2 10334stop_command (char *arg, int from_tty)
c906108c 10335{
a3f17187 10336 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10337Usage: stop in <function | address>\n\
a3f17187 10338 stop at <line>\n"));
c906108c
SS
10339}
10340
10341static void
fba45db2 10342stopin_command (char *arg, int from_tty)
c906108c
SS
10343{
10344 int badInput = 0;
10345
c5aa993b 10346 if (arg == (char *) NULL)
c906108c
SS
10347 badInput = 1;
10348 else if (*arg != '*')
10349 {
10350 char *argptr = arg;
10351 int hasColon = 0;
10352
4a64f543 10353 /* Look for a ':'. If this is a line number specification, then
53a5351d 10354 say it is bad, otherwise, it should be an address or
4a64f543 10355 function/method name. */
c906108c 10356 while (*argptr && !hasColon)
c5aa993b
JM
10357 {
10358 hasColon = (*argptr == ':');
10359 argptr++;
10360 }
c906108c
SS
10361
10362 if (hasColon)
c5aa993b 10363 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10364 else
c5aa993b 10365 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10366 }
10367
10368 if (badInput)
a3f17187 10369 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10370 else
db107f19 10371 break_command_1 (arg, 0, from_tty);
c906108c
SS
10372}
10373
10374static void
fba45db2 10375stopat_command (char *arg, int from_tty)
c906108c
SS
10376{
10377 int badInput = 0;
10378
c5aa993b 10379 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10380 badInput = 1;
10381 else
10382 {
10383 char *argptr = arg;
10384 int hasColon = 0;
10385
4a64f543
MS
10386 /* Look for a ':'. If there is a '::' then get out, otherwise
10387 it is probably a line number. */
c906108c 10388 while (*argptr && !hasColon)
c5aa993b
JM
10389 {
10390 hasColon = (*argptr == ':');
10391 argptr++;
10392 }
c906108c
SS
10393
10394 if (hasColon)
c5aa993b 10395 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10396 else
c5aa993b 10397 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10398 }
10399
10400 if (badInput)
a3f17187 10401 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10402 else
db107f19 10403 break_command_1 (arg, 0, from_tty);
c906108c
SS
10404}
10405
e7e0cddf
SS
10406/* The dynamic printf command is mostly like a regular breakpoint, but
10407 with a prewired command list consisting of a single output command,
10408 built from extra arguments supplied on the dprintf command
10409 line. */
10410
da821c7b 10411static void
e7e0cddf
SS
10412dprintf_command (char *arg, int from_tty)
10413{
10414 create_breakpoint (get_current_arch (),
10415 arg,
10416 NULL, 0, NULL, 1 /* parse arg */,
10417 0, bp_dprintf,
10418 0 /* Ignore count */,
10419 pending_break_support,
10420 &dprintf_breakpoint_ops,
10421 from_tty,
10422 1 /* enabled */,
10423 0 /* internal */,
10424 0);
10425}
10426
d3ce09f5
SS
10427static void
10428agent_printf_command (char *arg, int from_tty)
10429{
10430 error (_("May only run agent-printf on the target"));
10431}
10432
f1310107
TJB
10433/* Implement the "breakpoint_hit" breakpoint_ops method for
10434 ranged breakpoints. */
10435
10436static int
10437breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10438 struct address_space *aspace,
09ac7c10
TT
10439 CORE_ADDR bp_addr,
10440 const struct target_waitstatus *ws)
f1310107 10441{
09ac7c10 10442 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10443 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10444 return 0;
10445
f1310107
TJB
10446 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10447 bl->length, aspace, bp_addr);
10448}
10449
10450/* Implement the "resources_needed" breakpoint_ops method for
10451 ranged breakpoints. */
10452
10453static int
10454resources_needed_ranged_breakpoint (const struct bp_location *bl)
10455{
10456 return target_ranged_break_num_registers ();
10457}
10458
10459/* Implement the "print_it" breakpoint_ops method for
10460 ranged breakpoints. */
10461
10462static enum print_stop_action
348d480f 10463print_it_ranged_breakpoint (bpstat bs)
f1310107 10464{
348d480f 10465 struct breakpoint *b = bs->breakpoint_at;
f1310107 10466 struct bp_location *bl = b->loc;
79a45e25 10467 struct ui_out *uiout = current_uiout;
f1310107
TJB
10468
10469 gdb_assert (b->type == bp_hardware_breakpoint);
10470
10471 /* Ranged breakpoints have only one location. */
10472 gdb_assert (bl && bl->next == NULL);
10473
10474 annotate_breakpoint (b->number);
10475 if (b->disposition == disp_del)
10476 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10477 else
10478 ui_out_text (uiout, "\nRanged breakpoint ");
10479 if (ui_out_is_mi_like_p (uiout))
10480 {
10481 ui_out_field_string (uiout, "reason",
10482 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10483 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10484 }
10485 ui_out_field_int (uiout, "bkptno", b->number);
10486 ui_out_text (uiout, ", ");
10487
10488 return PRINT_SRC_AND_LOC;
10489}
10490
10491/* Implement the "print_one" breakpoint_ops method for
10492 ranged breakpoints. */
10493
10494static void
10495print_one_ranged_breakpoint (struct breakpoint *b,
10496 struct bp_location **last_loc)
10497{
10498 struct bp_location *bl = b->loc;
10499 struct value_print_options opts;
79a45e25 10500 struct ui_out *uiout = current_uiout;
f1310107
TJB
10501
10502 /* Ranged breakpoints have only one location. */
10503 gdb_assert (bl && bl->next == NULL);
10504
10505 get_user_print_options (&opts);
10506
10507 if (opts.addressprint)
10508 /* We don't print the address range here, it will be printed later
10509 by print_one_detail_ranged_breakpoint. */
10510 ui_out_field_skip (uiout, "addr");
10511 annotate_field (5);
10512 print_breakpoint_location (b, bl);
10513 *last_loc = bl;
10514}
10515
10516/* Implement the "print_one_detail" breakpoint_ops method for
10517 ranged breakpoints. */
10518
10519static void
10520print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10521 struct ui_out *uiout)
10522{
10523 CORE_ADDR address_start, address_end;
10524 struct bp_location *bl = b->loc;
f99d8bf4
PA
10525 struct ui_file *stb = mem_fileopen ();
10526 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10527
10528 gdb_assert (bl);
10529
10530 address_start = bl->address;
10531 address_end = address_start + bl->length - 1;
10532
10533 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10534 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10535 print_core_address (bl->gdbarch, address_start),
10536 print_core_address (bl->gdbarch, address_end));
10537 ui_out_field_stream (uiout, "addr", stb);
10538 ui_out_text (uiout, "\n");
10539
10540 do_cleanups (cleanup);
10541}
10542
10543/* Implement the "print_mention" breakpoint_ops method for
10544 ranged breakpoints. */
10545
10546static void
10547print_mention_ranged_breakpoint (struct breakpoint *b)
10548{
10549 struct bp_location *bl = b->loc;
79a45e25 10550 struct ui_out *uiout = current_uiout;
f1310107
TJB
10551
10552 gdb_assert (bl);
10553 gdb_assert (b->type == bp_hardware_breakpoint);
10554
10555 if (ui_out_is_mi_like_p (uiout))
10556 return;
10557
10558 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10559 b->number, paddress (bl->gdbarch, bl->address),
10560 paddress (bl->gdbarch, bl->address + bl->length - 1));
10561}
10562
10563/* Implement the "print_recreate" breakpoint_ops method for
10564 ranged breakpoints. */
10565
10566static void
10567print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10568{
10569 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10570 b->addr_string_range_end);
d9b3f62e 10571 print_recreate_thread (b, fp);
f1310107
TJB
10572}
10573
10574/* The breakpoint_ops structure to be used in ranged breakpoints. */
10575
2060206e 10576static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10577
10578/* Find the address where the end of the breakpoint range should be
10579 placed, given the SAL of the end of the range. This is so that if
10580 the user provides a line number, the end of the range is set to the
10581 last instruction of the given line. */
10582
10583static CORE_ADDR
10584find_breakpoint_range_end (struct symtab_and_line sal)
10585{
10586 CORE_ADDR end;
10587
10588 /* If the user provided a PC value, use it. Otherwise,
10589 find the address of the end of the given location. */
10590 if (sal.explicit_pc)
10591 end = sal.pc;
10592 else
10593 {
10594 int ret;
10595 CORE_ADDR start;
10596
10597 ret = find_line_pc_range (sal, &start, &end);
10598 if (!ret)
10599 error (_("Could not find location of the end of the range."));
10600
10601 /* find_line_pc_range returns the start of the next line. */
10602 end--;
10603 }
10604
10605 return end;
10606}
10607
10608/* Implement the "break-range" CLI command. */
10609
10610static void
10611break_range_command (char *arg, int from_tty)
10612{
10613 char *arg_start, *addr_string_start, *addr_string_end;
10614 struct linespec_result canonical_start, canonical_end;
10615 int bp_count, can_use_bp, length;
10616 CORE_ADDR end;
10617 struct breakpoint *b;
10618 struct symtab_and_line sal_start, sal_end;
f1310107 10619 struct cleanup *cleanup_bkpt;
f8eba3c6 10620 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10621
10622 /* We don't support software ranged breakpoints. */
10623 if (target_ranged_break_num_registers () < 0)
10624 error (_("This target does not support hardware ranged breakpoints."));
10625
10626 bp_count = hw_breakpoint_used_count ();
10627 bp_count += target_ranged_break_num_registers ();
10628 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10629 bp_count, 0);
10630 if (can_use_bp < 0)
10631 error (_("Hardware breakpoints used exceeds limit."));
10632
f8eba3c6 10633 arg = skip_spaces (arg);
f1310107
TJB
10634 if (arg == NULL || arg[0] == '\0')
10635 error(_("No address range specified."));
10636
f1310107
TJB
10637 init_linespec_result (&canonical_start);
10638
f8eba3c6
TT
10639 arg_start = arg;
10640 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10641
f8eba3c6 10642 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10643
10644 if (arg[0] != ',')
10645 error (_("Too few arguments."));
f8eba3c6 10646 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10647 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10648
10649 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10650
10651 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10652 || lsal_start->sals.nelts != 1)
f1310107
TJB
10653 error (_("Cannot create a ranged breakpoint with multiple locations."));
10654
f8eba3c6
TT
10655 sal_start = lsal_start->sals.sals[0];
10656 addr_string_start = savestring (arg_start, arg - arg_start);
10657 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10658
10659 arg++; /* Skip the comma. */
f8eba3c6 10660 arg = skip_spaces (arg);
f1310107
TJB
10661
10662 /* Parse the end location. */
10663
f1310107
TJB
10664 init_linespec_result (&canonical_end);
10665 arg_start = arg;
10666
f8eba3c6 10667 /* We call decode_line_full directly here instead of using
f1310107
TJB
10668 parse_breakpoint_sals because we need to specify the start location's
10669 symtab and line as the default symtab and line for the end of the
10670 range. This makes it possible to have ranges like "foo.c:27, +14",
10671 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10672 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10673 sal_start.symtab, sal_start.line,
10674 &canonical_end, NULL, NULL);
10675
10676 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10677
f8eba3c6 10678 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10679 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10680
10681 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10682 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10683 || lsal_end->sals.nelts != 1)
f1310107
TJB
10684 error (_("Cannot create a ranged breakpoint with multiple locations."));
10685
f8eba3c6
TT
10686 sal_end = lsal_end->sals.sals[0];
10687 addr_string_end = savestring (arg_start, arg - arg_start);
10688 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10689
10690 end = find_breakpoint_range_end (sal_end);
10691 if (sal_start.pc > end)
177b42fe 10692 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10693
10694 length = end - sal_start.pc + 1;
10695 if (length < 0)
10696 /* Length overflowed. */
10697 error (_("Address range too large."));
10698 else if (length == 1)
10699 {
10700 /* This range is simple enough to be handled by
10701 the `hbreak' command. */
10702 hbreak_command (addr_string_start, 1);
10703
10704 do_cleanups (cleanup_bkpt);
10705
10706 return;
10707 }
10708
10709 /* Now set up the breakpoint. */
10710 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10711 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10712 set_breakpoint_count (breakpoint_count + 1);
10713 b->number = breakpoint_count;
10714 b->disposition = disp_donttouch;
f8eba3c6
TT
10715 b->addr_string = xstrdup (addr_string_start);
10716 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10717 b->loc->length = length;
10718
f8eba3c6 10719 do_cleanups (cleanup_bkpt);
f1310107
TJB
10720
10721 mention (b);
8d3788bd 10722 observer_notify_breakpoint_created (b);
44702360 10723 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10724}
10725
4a64f543
MS
10726/* Return non-zero if EXP is verified as constant. Returned zero
10727 means EXP is variable. Also the constant detection may fail for
10728 some constant expressions and in such case still falsely return
10729 zero. */
2e6e3d9c 10730
65d79d4b
SDJ
10731static int
10732watchpoint_exp_is_const (const struct expression *exp)
10733{
10734 int i = exp->nelts;
10735
10736 while (i > 0)
10737 {
10738 int oplenp, argsp;
10739
10740 /* We are only interested in the descriptor of each element. */
10741 operator_length (exp, i, &oplenp, &argsp);
10742 i -= oplenp;
10743
10744 switch (exp->elts[i].opcode)
10745 {
10746 case BINOP_ADD:
10747 case BINOP_SUB:
10748 case BINOP_MUL:
10749 case BINOP_DIV:
10750 case BINOP_REM:
10751 case BINOP_MOD:
10752 case BINOP_LSH:
10753 case BINOP_RSH:
10754 case BINOP_LOGICAL_AND:
10755 case BINOP_LOGICAL_OR:
10756 case BINOP_BITWISE_AND:
10757 case BINOP_BITWISE_IOR:
10758 case BINOP_BITWISE_XOR:
10759 case BINOP_EQUAL:
10760 case BINOP_NOTEQUAL:
10761 case BINOP_LESS:
10762 case BINOP_GTR:
10763 case BINOP_LEQ:
10764 case BINOP_GEQ:
10765 case BINOP_REPEAT:
10766 case BINOP_COMMA:
10767 case BINOP_EXP:
10768 case BINOP_MIN:
10769 case BINOP_MAX:
10770 case BINOP_INTDIV:
10771 case BINOP_CONCAT:
65d79d4b
SDJ
10772 case TERNOP_COND:
10773 case TERNOP_SLICE:
65d79d4b
SDJ
10774
10775 case OP_LONG:
10776 case OP_DOUBLE:
10777 case OP_DECFLOAT:
10778 case OP_LAST:
10779 case OP_COMPLEX:
10780 case OP_STRING:
65d79d4b
SDJ
10781 case OP_ARRAY:
10782 case OP_TYPE:
608b4967
TT
10783 case OP_TYPEOF:
10784 case OP_DECLTYPE:
6e72ca20 10785 case OP_TYPEID:
65d79d4b
SDJ
10786 case OP_NAME:
10787 case OP_OBJC_NSSTRING:
10788
10789 case UNOP_NEG:
10790 case UNOP_LOGICAL_NOT:
10791 case UNOP_COMPLEMENT:
10792 case UNOP_ADDR:
10793 case UNOP_HIGH:
aeaa2474 10794 case UNOP_CAST:
9eaf6705
TT
10795
10796 case UNOP_CAST_TYPE:
10797 case UNOP_REINTERPRET_CAST:
10798 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10799 /* Unary, binary and ternary operators: We have to check
10800 their operands. If they are constant, then so is the
10801 result of that operation. For instance, if A and B are
10802 determined to be constants, then so is "A + B".
10803
10804 UNOP_IND is one exception to the rule above, because the
10805 value of *ADDR is not necessarily a constant, even when
10806 ADDR is. */
65d79d4b
SDJ
10807 break;
10808
10809 case OP_VAR_VALUE:
10810 /* Check whether the associated symbol is a constant.
4a64f543 10811
65d79d4b 10812 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10813 possible that a buggy compiler could mark a variable as
10814 constant even when it is not, and TYPE_CONST would return
10815 true in this case, while SYMBOL_CLASS wouldn't.
10816
10817 We also have to check for function symbols because they
10818 are always constant. */
65d79d4b
SDJ
10819 {
10820 struct symbol *s = exp->elts[i + 2].symbol;
10821
10822 if (SYMBOL_CLASS (s) != LOC_BLOCK
10823 && SYMBOL_CLASS (s) != LOC_CONST
10824 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10825 return 0;
10826 break;
10827 }
10828
10829 /* The default action is to return 0 because we are using
10830 the optimistic approach here: If we don't know something,
10831 then it is not a constant. */
10832 default:
10833 return 0;
10834 }
10835 }
10836
10837 return 1;
10838}
10839
3a5c3e22
PA
10840/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10841
10842static void
10843dtor_watchpoint (struct breakpoint *self)
10844{
10845 struct watchpoint *w = (struct watchpoint *) self;
10846
10847 xfree (w->cond_exp);
10848 xfree (w->exp);
10849 xfree (w->exp_string);
10850 xfree (w->exp_string_reparse);
10851 value_free (w->val);
10852
10853 base_breakpoint_ops.dtor (self);
10854}
10855
348d480f
PA
10856/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10857
10858static void
10859re_set_watchpoint (struct breakpoint *b)
10860{
3a5c3e22
PA
10861 struct watchpoint *w = (struct watchpoint *) b;
10862
348d480f
PA
10863 /* Watchpoint can be either on expression using entirely global
10864 variables, or it can be on local variables.
10865
10866 Watchpoints of the first kind are never auto-deleted, and even
10867 persist across program restarts. Since they can use variables
10868 from shared libraries, we need to reparse expression as libraries
10869 are loaded and unloaded.
10870
10871 Watchpoints on local variables can also change meaning as result
10872 of solib event. For example, if a watchpoint uses both a local
10873 and a global variables in expression, it's a local watchpoint,
10874 but unloading of a shared library will make the expression
10875 invalid. This is not a very common use case, but we still
10876 re-evaluate expression, to avoid surprises to the user.
10877
10878 Note that for local watchpoints, we re-evaluate it only if
10879 watchpoints frame id is still valid. If it's not, it means the
10880 watchpoint is out of scope and will be deleted soon. In fact,
10881 I'm not sure we'll ever be called in this case.
10882
10883 If a local watchpoint's frame id is still valid, then
3a5c3e22 10884 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10885
3a5c3e22
PA
10886 Don't do anything about disabled watchpoints, since they will be
10887 reevaluated again when enabled. */
10888 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10889}
10890
77b06cd7
TJB
10891/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10892
10893static int
10894insert_watchpoint (struct bp_location *bl)
10895{
3a5c3e22
PA
10896 struct watchpoint *w = (struct watchpoint *) bl->owner;
10897 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10898
10899 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10900 w->cond_exp);
77b06cd7
TJB
10901}
10902
10903/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10904
10905static int
10906remove_watchpoint (struct bp_location *bl)
10907{
3a5c3e22
PA
10908 struct watchpoint *w = (struct watchpoint *) bl->owner;
10909 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10910
10911 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10912 w->cond_exp);
e09342b5
TJB
10913}
10914
e09342b5 10915static int
348d480f 10916breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10917 struct address_space *aspace, CORE_ADDR bp_addr,
10918 const struct target_waitstatus *ws)
e09342b5 10919{
348d480f 10920 struct breakpoint *b = bl->owner;
3a5c3e22 10921 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10922
348d480f
PA
10923 /* Continuable hardware watchpoints are treated as non-existent if the
10924 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10925 some data address). Otherwise gdb won't stop on a break instruction
10926 in the code (not from a breakpoint) when a hardware watchpoint has
10927 been defined. Also skip watchpoints which we know did not trigger
10928 (did not match the data address). */
10929 if (is_hardware_watchpoint (b)
3a5c3e22 10930 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10931 return 0;
9c06b0b4 10932
348d480f 10933 return 1;
9c06b0b4
TJB
10934}
10935
348d480f
PA
10936static void
10937check_status_watchpoint (bpstat bs)
9c06b0b4 10938{
348d480f 10939 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10940
348d480f 10941 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10942}
10943
10944/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10945 hardware watchpoints. */
9c06b0b4
TJB
10946
10947static int
348d480f 10948resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10949{
3a5c3e22
PA
10950 struct watchpoint *w = (struct watchpoint *) bl->owner;
10951 int length = w->exact? 1 : bl->length;
348d480f
PA
10952
10953 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10954}
10955
10956/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10957 hardware watchpoints. */
9c06b0b4
TJB
10958
10959static int
348d480f 10960works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10961{
efa80663
PA
10962 /* Read and access watchpoints only work with hardware support. */
10963 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10964}
10965
9c06b0b4 10966static enum print_stop_action
348d480f 10967print_it_watchpoint (bpstat bs)
9c06b0b4 10968{
348d480f
PA
10969 struct cleanup *old_chain;
10970 struct breakpoint *b;
f99d8bf4 10971 struct ui_file *stb;
348d480f 10972 enum print_stop_action result;
3a5c3e22 10973 struct watchpoint *w;
79a45e25 10974 struct ui_out *uiout = current_uiout;
348d480f
PA
10975
10976 gdb_assert (bs->bp_location_at != NULL);
10977
348d480f 10978 b = bs->breakpoint_at;
3a5c3e22 10979 w = (struct watchpoint *) b;
348d480f 10980
f99d8bf4
PA
10981 stb = mem_fileopen ();
10982 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10983
10984 switch (b->type)
10985 {
348d480f 10986 case bp_watchpoint:
9c06b0b4
TJB
10987 case bp_hardware_watchpoint:
10988 annotate_watchpoint (b->number);
10989 if (ui_out_is_mi_like_p (uiout))
10990 ui_out_field_string
10991 (uiout, "reason",
10992 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10993 mention (b);
10994 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10995 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10996 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10997 ui_out_field_stream (uiout, "old", stb);
10998 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10999 watchpoint_value_print (w->val, stb);
348d480f
PA
11000 ui_out_field_stream (uiout, "new", stb);
11001 ui_out_text (uiout, "\n");
11002 /* More than one watchpoint may have been triggered. */
11003 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11004 break;
11005
11006 case bp_read_watchpoint:
11007 if (ui_out_is_mi_like_p (uiout))
11008 ui_out_field_string
11009 (uiout, "reason",
11010 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11011 mention (b);
11012 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11013 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11014 watchpoint_value_print (w->val, stb);
348d480f
PA
11015 ui_out_field_stream (uiout, "value", stb);
11016 ui_out_text (uiout, "\n");
11017 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11018 break;
11019
11020 case bp_access_watchpoint:
348d480f
PA
11021 if (bs->old_val != NULL)
11022 {
11023 annotate_watchpoint (b->number);
11024 if (ui_out_is_mi_like_p (uiout))
11025 ui_out_field_string
11026 (uiout, "reason",
11027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11028 mention (b);
11029 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11030 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11031 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11032 ui_out_field_stream (uiout, "old", stb);
11033 ui_out_text (uiout, "\nNew value = ");
11034 }
11035 else
11036 {
11037 mention (b);
11038 if (ui_out_is_mi_like_p (uiout))
11039 ui_out_field_string
11040 (uiout, "reason",
11041 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11042 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11043 ui_out_text (uiout, "\nValue = ");
11044 }
f99d8bf4 11045 watchpoint_value_print (w->val, stb);
348d480f
PA
11046 ui_out_field_stream (uiout, "new", stb);
11047 ui_out_text (uiout, "\n");
11048 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11049 break;
11050 default:
348d480f 11051 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11052 }
11053
348d480f
PA
11054 do_cleanups (old_chain);
11055 return result;
11056}
11057
11058/* Implement the "print_mention" breakpoint_ops method for hardware
11059 watchpoints. */
11060
11061static void
11062print_mention_watchpoint (struct breakpoint *b)
11063{
11064 struct cleanup *ui_out_chain;
3a5c3e22 11065 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11066 struct ui_out *uiout = current_uiout;
348d480f
PA
11067
11068 switch (b->type)
11069 {
11070 case bp_watchpoint:
11071 ui_out_text (uiout, "Watchpoint ");
11072 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11073 break;
11074 case bp_hardware_watchpoint:
11075 ui_out_text (uiout, "Hardware watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11077 break;
11078 case bp_read_watchpoint:
11079 ui_out_text (uiout, "Hardware read watchpoint ");
11080 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11081 break;
11082 case bp_access_watchpoint:
11083 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11084 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11085 break;
11086 default:
11087 internal_error (__FILE__, __LINE__,
11088 _("Invalid hardware watchpoint type."));
11089 }
11090
11091 ui_out_field_int (uiout, "number", b->number);
11092 ui_out_text (uiout, ": ");
3a5c3e22 11093 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11094 do_cleanups (ui_out_chain);
11095}
11096
11097/* Implement the "print_recreate" breakpoint_ops method for
11098 watchpoints. */
11099
11100static void
11101print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11102{
3a5c3e22
PA
11103 struct watchpoint *w = (struct watchpoint *) b;
11104
348d480f
PA
11105 switch (b->type)
11106 {
11107 case bp_watchpoint:
11108 case bp_hardware_watchpoint:
11109 fprintf_unfiltered (fp, "watch");
11110 break;
11111 case bp_read_watchpoint:
11112 fprintf_unfiltered (fp, "rwatch");
11113 break;
11114 case bp_access_watchpoint:
11115 fprintf_unfiltered (fp, "awatch");
11116 break;
11117 default:
11118 internal_error (__FILE__, __LINE__,
11119 _("Invalid watchpoint type."));
11120 }
11121
3a5c3e22 11122 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11123 print_recreate_thread (b, fp);
348d480f
PA
11124}
11125
427cd150
TT
11126/* Implement the "explains_signal" breakpoint_ops method for
11127 watchpoints. */
11128
47591c29 11129static int
427cd150
TT
11130explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11131{
11132 /* A software watchpoint cannot cause a signal other than
11133 GDB_SIGNAL_TRAP. */
11134 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11135 return 0;
427cd150 11136
47591c29 11137 return 1;
427cd150
TT
11138}
11139
348d480f
PA
11140/* The breakpoint_ops structure to be used in hardware watchpoints. */
11141
2060206e 11142static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11143
11144/* Implement the "insert" breakpoint_ops method for
11145 masked hardware watchpoints. */
11146
11147static int
11148insert_masked_watchpoint (struct bp_location *bl)
11149{
3a5c3e22
PA
11150 struct watchpoint *w = (struct watchpoint *) bl->owner;
11151
11152 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11153 bl->watchpoint_type);
11154}
11155
11156/* Implement the "remove" breakpoint_ops method for
11157 masked hardware watchpoints. */
11158
11159static int
11160remove_masked_watchpoint (struct bp_location *bl)
11161{
3a5c3e22
PA
11162 struct watchpoint *w = (struct watchpoint *) bl->owner;
11163
11164 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11165 bl->watchpoint_type);
11166}
11167
11168/* Implement the "resources_needed" breakpoint_ops method for
11169 masked hardware watchpoints. */
11170
11171static int
11172resources_needed_masked_watchpoint (const struct bp_location *bl)
11173{
3a5c3e22
PA
11174 struct watchpoint *w = (struct watchpoint *) bl->owner;
11175
11176 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11177}
11178
11179/* Implement the "works_in_software_mode" breakpoint_ops method for
11180 masked hardware watchpoints. */
11181
11182static int
11183works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11184{
11185 return 0;
11186}
11187
11188/* Implement the "print_it" breakpoint_ops method for
11189 masked hardware watchpoints. */
11190
11191static enum print_stop_action
11192print_it_masked_watchpoint (bpstat bs)
11193{
11194 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11195 struct ui_out *uiout = current_uiout;
348d480f
PA
11196
11197 /* Masked watchpoints have only one location. */
11198 gdb_assert (b->loc && b->loc->next == NULL);
11199
11200 switch (b->type)
11201 {
11202 case bp_hardware_watchpoint:
11203 annotate_watchpoint (b->number);
11204 if (ui_out_is_mi_like_p (uiout))
11205 ui_out_field_string
11206 (uiout, "reason",
11207 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11208 break;
11209
11210 case bp_read_watchpoint:
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_access_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11222 break;
11223 default:
11224 internal_error (__FILE__, __LINE__,
11225 _("Invalid hardware watchpoint type."));
11226 }
11227
11228 mention (b);
9c06b0b4
TJB
11229 ui_out_text (uiout, _("\n\
11230Check the underlying instruction at PC for the memory\n\
11231address and value which triggered this watchpoint.\n"));
11232 ui_out_text (uiout, "\n");
11233
11234 /* More than one watchpoint may have been triggered. */
11235 return PRINT_UNKNOWN;
11236}
11237
11238/* Implement the "print_one_detail" breakpoint_ops method for
11239 masked hardware watchpoints. */
11240
11241static void
11242print_one_detail_masked_watchpoint (const struct breakpoint *b,
11243 struct ui_out *uiout)
11244{
3a5c3e22
PA
11245 struct watchpoint *w = (struct watchpoint *) b;
11246
9c06b0b4
TJB
11247 /* Masked watchpoints have only one location. */
11248 gdb_assert (b->loc && b->loc->next == NULL);
11249
11250 ui_out_text (uiout, "\tmask ");
3a5c3e22 11251 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11252 ui_out_text (uiout, "\n");
11253}
11254
11255/* Implement the "print_mention" breakpoint_ops method for
11256 masked hardware watchpoints. */
11257
11258static void
11259print_mention_masked_watchpoint (struct breakpoint *b)
11260{
3a5c3e22 11261 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11262 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11263 struct cleanup *ui_out_chain;
11264
11265 switch (b->type)
11266 {
11267 case bp_hardware_watchpoint:
11268 ui_out_text (uiout, "Masked hardware watchpoint ");
11269 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11270 break;
11271 case bp_read_watchpoint:
11272 ui_out_text (uiout, "Masked hardware read watchpoint ");
11273 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11274 break;
11275 case bp_access_watchpoint:
11276 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11277 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11278 break;
11279 default:
11280 internal_error (__FILE__, __LINE__,
11281 _("Invalid hardware watchpoint type."));
11282 }
11283
11284 ui_out_field_int (uiout, "number", b->number);
11285 ui_out_text (uiout, ": ");
3a5c3e22 11286 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11287 do_cleanups (ui_out_chain);
11288}
11289
11290/* Implement the "print_recreate" breakpoint_ops method for
11291 masked hardware watchpoints. */
11292
11293static void
11294print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11295{
3a5c3e22 11296 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11297 char tmp[40];
11298
11299 switch (b->type)
11300 {
11301 case bp_hardware_watchpoint:
11302 fprintf_unfiltered (fp, "watch");
11303 break;
11304 case bp_read_watchpoint:
11305 fprintf_unfiltered (fp, "rwatch");
11306 break;
11307 case bp_access_watchpoint:
11308 fprintf_unfiltered (fp, "awatch");
11309 break;
11310 default:
11311 internal_error (__FILE__, __LINE__,
11312 _("Invalid hardware watchpoint type."));
11313 }
11314
3a5c3e22
PA
11315 sprintf_vma (tmp, w->hw_wp_mask);
11316 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11317 print_recreate_thread (b, fp);
9c06b0b4
TJB
11318}
11319
11320/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11321
2060206e 11322static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11323
11324/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11325
11326static int
11327is_masked_watchpoint (const struct breakpoint *b)
11328{
11329 return b->ops == &masked_watchpoint_breakpoint_ops;
11330}
11331
53a5351d
JM
11332/* accessflag: hw_write: watch write,
11333 hw_read: watch read,
11334 hw_access: watch access (read or write) */
c906108c 11335static void
bbc13ae3 11336watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11337 int just_location, int internal)
c906108c 11338{
a9634178 11339 volatile struct gdb_exception e;
d983da9c 11340 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11341 struct expression *exp;
270140bd 11342 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11343 struct value *val, *mark, *result;
bb9d5f81 11344 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11345 struct frame_info *frame;
bbc13ae3
KS
11346 const char *exp_start = NULL;
11347 const char *exp_end = NULL;
11348 const char *tok, *end_tok;
9c06b0b4 11349 int toklen = -1;
bbc13ae3
KS
11350 const char *cond_start = NULL;
11351 const char *cond_end = NULL;
c906108c 11352 enum bptype bp_type;
37e4754d 11353 int thread = -1;
0cf6dd15 11354 int pc = 0;
9c06b0b4
TJB
11355 /* Flag to indicate whether we are going to use masks for
11356 the hardware watchpoint. */
11357 int use_mask = 0;
11358 CORE_ADDR mask = 0;
3a5c3e22 11359 struct watchpoint *w;
bbc13ae3
KS
11360 char *expression;
11361 struct cleanup *back_to;
c906108c 11362
37e4754d
LM
11363 /* Make sure that we actually have parameters to parse. */
11364 if (arg != NULL && arg[0] != '\0')
11365 {
bbc13ae3
KS
11366 const char *value_start;
11367
11368 exp_end = arg + strlen (arg);
37e4754d 11369
9c06b0b4
TJB
11370 /* Look for "parameter value" pairs at the end
11371 of the arguments string. */
bbc13ae3 11372 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11373 {
11374 /* Skip whitespace at the end of the argument list. */
11375 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11376 tok--;
11377
11378 /* Find the beginning of the last token.
11379 This is the value of the parameter. */
11380 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11381 tok--;
11382 value_start = tok + 1;
11383
11384 /* Skip whitespace. */
11385 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11386 tok--;
11387
11388 end_tok = tok;
11389
11390 /* Find the beginning of the second to last token.
11391 This is the parameter itself. */
11392 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11393 tok--;
11394 tok++;
11395 toklen = end_tok - tok + 1;
11396
11397 if (toklen == 6 && !strncmp (tok, "thread", 6))
11398 {
11399 /* At this point we've found a "thread" token, which means
11400 the user is trying to set a watchpoint that triggers
11401 only in a specific thread. */
11402 char *endp;
37e4754d 11403
9c06b0b4
TJB
11404 if (thread != -1)
11405 error(_("You can specify only one thread."));
37e4754d 11406
9c06b0b4
TJB
11407 /* Extract the thread ID from the next token. */
11408 thread = strtol (value_start, &endp, 0);
37e4754d 11409
9c06b0b4
TJB
11410 /* Check if the user provided a valid numeric value for the
11411 thread ID. */
11412 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11413 error (_("Invalid thread ID specification %s."), value_start);
11414
11415 /* Check if the thread actually exists. */
11416 if (!valid_thread_id (thread))
af4908ba 11417 invalid_thread_id_error (thread);
9c06b0b4
TJB
11418 }
11419 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11420 {
11421 /* We've found a "mask" token, which means the user wants to
11422 create a hardware watchpoint that is going to have the mask
11423 facility. */
11424 struct value *mask_value, *mark;
37e4754d 11425
9c06b0b4
TJB
11426 if (use_mask)
11427 error(_("You can specify only one mask."));
37e4754d 11428
9c06b0b4 11429 use_mask = just_location = 1;
37e4754d 11430
9c06b0b4
TJB
11431 mark = value_mark ();
11432 mask_value = parse_to_comma_and_eval (&value_start);
11433 mask = value_as_address (mask_value);
11434 value_free_to_mark (mark);
11435 }
11436 else
11437 /* We didn't recognize what we found. We should stop here. */
11438 break;
37e4754d 11439
9c06b0b4
TJB
11440 /* Truncate the string and get rid of the "parameter value" pair before
11441 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11442 exp_end = tok;
9c06b0b4 11443 }
37e4754d 11444 }
bbc13ae3
KS
11445 else
11446 exp_end = arg;
37e4754d 11447
bbc13ae3
KS
11448 /* Parse the rest of the arguments. From here on out, everything
11449 is in terms of a newly allocated string instead of the original
11450 ARG. */
c906108c 11451 innermost_block = NULL;
bbc13ae3
KS
11452 expression = savestring (arg, exp_end - arg);
11453 back_to = make_cleanup (xfree, expression);
11454 exp_start = arg = expression;
1bb9788d 11455 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11456 exp_end = arg;
fa8a61dc
TT
11457 /* Remove trailing whitespace from the expression before saving it.
11458 This makes the eventual display of the expression string a bit
11459 prettier. */
11460 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11461 --exp_end;
11462
65d79d4b
SDJ
11463 /* Checking if the expression is not constant. */
11464 if (watchpoint_exp_is_const (exp))
11465 {
11466 int len;
11467
11468 len = exp_end - exp_start;
11469 while (len > 0 && isspace (exp_start[len - 1]))
11470 len--;
11471 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11472 }
11473
c906108c
SS
11474 exp_valid_block = innermost_block;
11475 mark = value_mark ();
3a1115a0 11476 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11477
bb9d5f81
PP
11478 if (val != NULL && just_location)
11479 {
11480 saved_bitpos = value_bitpos (val);
11481 saved_bitsize = value_bitsize (val);
11482 }
11483
06a64a0b
TT
11484 if (just_location)
11485 {
9c06b0b4
TJB
11486 int ret;
11487
06a64a0b 11488 exp_valid_block = NULL;
a1442452 11489 val = value_addr (result);
06a64a0b
TT
11490 release_value (val);
11491 value_free_to_mark (mark);
9c06b0b4
TJB
11492
11493 if (use_mask)
11494 {
11495 ret = target_masked_watch_num_registers (value_as_address (val),
11496 mask);
11497 if (ret == -1)
11498 error (_("This target does not support masked watchpoints."));
11499 else if (ret == -2)
11500 error (_("Invalid mask or memory region."));
11501 }
06a64a0b
TT
11502 }
11503 else if (val != NULL)
fa4727a6 11504 release_value (val);
c906108c 11505
bbc13ae3
KS
11506 tok = skip_spaces_const (arg);
11507 end_tok = skip_to_space_const (tok);
c906108c
SS
11508
11509 toklen = end_tok - tok;
11510 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11511 {
2d134ed3
PA
11512 struct expression *cond;
11513
60e1c644 11514 innermost_block = NULL;
c906108c 11515 tok = cond_start = end_tok + 1;
1bb9788d 11516 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11517
11518 /* The watchpoint expression may not be local, but the condition
11519 may still be. E.g.: `watch global if local > 0'. */
11520 cond_exp_valid_block = innermost_block;
11521
2d134ed3 11522 xfree (cond);
c906108c
SS
11523 cond_end = tok;
11524 }
11525 if (*tok)
8a3fe4f8 11526 error (_("Junk at end of command."));
c906108c 11527
d983da9c 11528 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11529
11530 /* If the expression is "local", then set up a "watchpoint scope"
11531 breakpoint at the point where we've left the scope of the watchpoint
11532 expression. Create the scope breakpoint before the watchpoint, so
11533 that we will encounter it first in bpstat_stop_status. */
60e1c644 11534 if (exp_valid_block && frame)
d983da9c 11535 {
edb3359d
DJ
11536 if (frame_id_p (frame_unwind_caller_id (frame)))
11537 {
11538 scope_breakpoint
a6d9a66e
UW
11539 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11540 frame_unwind_caller_pc (frame),
06edf0c0
PA
11541 bp_watchpoint_scope,
11542 &momentary_breakpoint_ops);
d983da9c 11543
edb3359d 11544 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11545
edb3359d
DJ
11546 /* Automatically delete the breakpoint when it hits. */
11547 scope_breakpoint->disposition = disp_del;
d983da9c 11548
edb3359d
DJ
11549 /* Only break in the proper frame (help with recursion). */
11550 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11551
edb3359d 11552 /* Set the address at which we will stop. */
a6d9a66e
UW
11553 scope_breakpoint->loc->gdbarch
11554 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11555 scope_breakpoint->loc->requested_address
11556 = frame_unwind_caller_pc (frame);
11557 scope_breakpoint->loc->address
a6d9a66e
UW
11558 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11559 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11560 scope_breakpoint->type);
11561 }
d983da9c
DJ
11562 }
11563
e8369a73
AB
11564 /* Now set up the breakpoint. We create all watchpoints as hardware
11565 watchpoints here even if hardware watchpoints are turned off, a call
11566 to update_watchpoint later in this function will cause the type to
11567 drop back to bp_watchpoint (software watchpoint) if required. */
11568
11569 if (accessflag == hw_read)
11570 bp_type = bp_read_watchpoint;
11571 else if (accessflag == hw_access)
11572 bp_type = bp_access_watchpoint;
11573 else
11574 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11575
11576 w = XCNEW (struct watchpoint);
11577 b = &w->base;
348d480f 11578 if (use_mask)
3a5c3e22
PA
11579 init_raw_breakpoint_without_location (b, NULL, bp_type,
11580 &masked_watchpoint_breakpoint_ops);
348d480f 11581 else
3a5c3e22
PA
11582 init_raw_breakpoint_without_location (b, NULL, bp_type,
11583 &watchpoint_breakpoint_ops);
37e4754d 11584 b->thread = thread;
b5de0fa7 11585 b->disposition = disp_donttouch;
348d480f 11586 b->pspace = current_program_space;
3a5c3e22
PA
11587 w->exp = exp;
11588 w->exp_valid_block = exp_valid_block;
11589 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11590 if (just_location)
11591 {
11592 struct type *t = value_type (val);
11593 CORE_ADDR addr = value_as_address (val);
11594 char *name;
11595
11596 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11597 name = type_to_string (t);
11598
3a5c3e22 11599 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11600 core_addr_to_string (addr));
06a64a0b
TT
11601 xfree (name);
11602
3a5c3e22 11603 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11604 (int) (exp_end - exp_start), exp_start);
11605
06a64a0b
TT
11606 /* The above expression is in C. */
11607 b->language = language_c;
11608 }
11609 else
3a5c3e22 11610 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11611
11612 if (use_mask)
11613 {
3a5c3e22 11614 w->hw_wp_mask = mask;
9c06b0b4
TJB
11615 }
11616 else
11617 {
3a5c3e22 11618 w->val = val;
bb9d5f81
PP
11619 w->val_bitpos = saved_bitpos;
11620 w->val_bitsize = saved_bitsize;
3a5c3e22 11621 w->val_valid = 1;
9c06b0b4 11622 }
77b06cd7 11623
c906108c
SS
11624 if (cond_start)
11625 b->cond_string = savestring (cond_start, cond_end - cond_start);
11626 else
11627 b->cond_string = 0;
c5aa993b 11628
c906108c 11629 if (frame)
f6bc2008 11630 {
3a5c3e22
PA
11631 w->watchpoint_frame = get_frame_id (frame);
11632 w->watchpoint_thread = inferior_ptid;
f6bc2008 11633 }
c906108c 11634 else
f6bc2008 11635 {
3a5c3e22
PA
11636 w->watchpoint_frame = null_frame_id;
11637 w->watchpoint_thread = null_ptid;
f6bc2008 11638 }
c906108c 11639
d983da9c 11640 if (scope_breakpoint != NULL)
c906108c 11641 {
d983da9c
DJ
11642 /* The scope breakpoint is related to the watchpoint. We will
11643 need to act on them together. */
11644 b->related_breakpoint = scope_breakpoint;
11645 scope_breakpoint->related_breakpoint = b;
c906108c 11646 }
d983da9c 11647
06a64a0b
TT
11648 if (!just_location)
11649 value_free_to_mark (mark);
2d134ed3 11650
a9634178
TJB
11651 TRY_CATCH (e, RETURN_MASK_ALL)
11652 {
11653 /* Finally update the new watchpoint. This creates the locations
11654 that should be inserted. */
3a5c3e22 11655 update_watchpoint (w, 1);
a9634178
TJB
11656 }
11657 if (e.reason < 0)
11658 {
11659 delete_breakpoint (b);
11660 throw_exception (e);
11661 }
11662
3ea46bff 11663 install_breakpoint (internal, b, 1);
bbc13ae3 11664 do_cleanups (back_to);
c906108c
SS
11665}
11666
e09342b5 11667/* Return count of debug registers needed to watch the given expression.
e09342b5 11668 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11669
c906108c 11670static int
a9634178 11671can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11672{
11673 int found_memory_cnt = 0;
2e70b7b9 11674 struct value *head = v;
c906108c
SS
11675
11676 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11677 if (!can_use_hw_watchpoints)
c906108c 11678 return 0;
c5aa993b 11679
5c44784c
JM
11680 /* Make sure that the value of the expression depends only upon
11681 memory contents, and values computed from them within GDB. If we
11682 find any register references or function calls, we can't use a
11683 hardware watchpoint.
11684
11685 The idea here is that evaluating an expression generates a series
11686 of values, one holding the value of every subexpression. (The
11687 expression a*b+c has five subexpressions: a, b, a*b, c, and
11688 a*b+c.) GDB's values hold almost enough information to establish
11689 the criteria given above --- they identify memory lvalues,
11690 register lvalues, computed values, etcetera. So we can evaluate
11691 the expression, and then scan the chain of values that leaves
11692 behind to decide whether we can detect any possible change to the
11693 expression's final value using only hardware watchpoints.
11694
11695 However, I don't think that the values returned by inferior
11696 function calls are special in any way. So this function may not
11697 notice that an expression involving an inferior function call
11698 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11699 for (; v; v = value_next (v))
c906108c 11700 {
5c44784c 11701 if (VALUE_LVAL (v) == lval_memory)
c906108c 11702 {
8464be76
DJ
11703 if (v != head && value_lazy (v))
11704 /* A lazy memory lvalue in the chain is one that GDB never
11705 needed to fetch; we either just used its address (e.g.,
11706 `a' in `a.b') or we never needed it at all (e.g., `a'
11707 in `a,b'). This doesn't apply to HEAD; if that is
11708 lazy then it was not readable, but watch it anyway. */
5c44784c 11709 ;
53a5351d 11710 else
5c44784c
JM
11711 {
11712 /* Ahh, memory we actually used! Check if we can cover
11713 it with hardware watchpoints. */
df407dfe 11714 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11715
11716 /* We only watch structs and arrays if user asked for it
11717 explicitly, never if they just happen to appear in a
11718 middle of some value chain. */
11719 if (v == head
11720 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11721 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11722 {
42ae5230 11723 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11724 int len;
11725 int num_regs;
11726
a9634178 11727 len = (target_exact_watchpoints
e09342b5
TJB
11728 && is_scalar_type_recursive (vtype))?
11729 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11730
e09342b5
TJB
11731 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11732 if (!num_regs)
2e70b7b9
MS
11733 return 0;
11734 else
e09342b5 11735 found_memory_cnt += num_regs;
2e70b7b9 11736 }
5c44784c 11737 }
c5aa993b 11738 }
5086187c
AC
11739 else if (VALUE_LVAL (v) != not_lval
11740 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11741 return 0; /* These are values from the history (e.g., $1). */
5086187c 11742 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11743 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11744 }
11745
11746 /* The expression itself looks suitable for using a hardware
11747 watchpoint, but give the target machine a chance to reject it. */
11748 return found_memory_cnt;
11749}
11750
8b93c638 11751void
84f4c1fe 11752watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11753{
84f4c1fe 11754 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11755}
11756
06a64a0b
TT
11757/* A helper function that looks for the "-location" argument and then
11758 calls watch_command_1. */
11759
11760static void
11761watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11762{
11763 int just_location = 0;
11764
11765 if (arg
11766 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11767 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11768 {
e9cafbcc 11769 arg = skip_spaces (arg);
06a64a0b
TT
11770 just_location = 1;
11771 }
11772
84f4c1fe 11773 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11774}
8926118c 11775
c5aa993b 11776static void
fba45db2 11777watch_command (char *arg, int from_tty)
c906108c 11778{
06a64a0b 11779 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11780}
11781
8b93c638 11782void
84f4c1fe 11783rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11784{
84f4c1fe 11785 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11786}
8926118c 11787
c5aa993b 11788static void
fba45db2 11789rwatch_command (char *arg, int from_tty)
c906108c 11790{
06a64a0b 11791 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11792}
11793
8b93c638 11794void
84f4c1fe 11795awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11796{
84f4c1fe 11797 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11798}
8926118c 11799
c5aa993b 11800static void
fba45db2 11801awatch_command (char *arg, int from_tty)
c906108c 11802{
06a64a0b 11803 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11804}
c906108c 11805\f
c5aa993b 11806
43ff13b4 11807/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11808 because it uses the mechanisms of breakpoints. */
11809
bfec99b2
PA
11810struct until_break_command_continuation_args
11811{
11812 struct breakpoint *breakpoint;
11813 struct breakpoint *breakpoint2;
186c406b 11814 int thread_num;
bfec99b2
PA
11815};
11816
43ff13b4 11817/* This function is called by fetch_inferior_event via the
4a64f543 11818 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11819 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11820 command. */
c2c6d25f 11821static void
fa4cd53f 11822until_break_command_continuation (void *arg, int err)
43ff13b4 11823{
bfec99b2
PA
11824 struct until_break_command_continuation_args *a = arg;
11825
11826 delete_breakpoint (a->breakpoint);
11827 if (a->breakpoint2)
11828 delete_breakpoint (a->breakpoint2);
186c406b 11829 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11830}
11831
c906108c 11832void
ae66c1fc 11833until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11834{
11835 struct symtabs_and_lines sals;
11836 struct symtab_and_line sal;
8556afb4
PA
11837 struct frame_info *frame;
11838 struct gdbarch *frame_gdbarch;
11839 struct frame_id stack_frame_id;
11840 struct frame_id caller_frame_id;
c906108c 11841 struct breakpoint *breakpoint;
f107f563 11842 struct breakpoint *breakpoint2 = NULL;
c906108c 11843 struct cleanup *old_chain;
186c406b
TT
11844 int thread;
11845 struct thread_info *tp;
c906108c 11846
70509625 11847 clear_proceed_status (0);
c906108c
SS
11848
11849 /* Set a breakpoint where the user wants it and at return from
4a64f543 11850 this function. */
c5aa993b 11851
1bfeeb0f 11852 if (last_displayed_sal_is_valid ())
f8eba3c6 11853 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11854 get_last_displayed_symtab (),
f8eba3c6 11855 get_last_displayed_line ());
c906108c 11856 else
f8eba3c6
TT
11857 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11858 (struct symtab *) NULL, 0);
c5aa993b 11859
c906108c 11860 if (sals.nelts != 1)
8a3fe4f8 11861 error (_("Couldn't get information on specified line."));
c5aa993b 11862
c906108c 11863 sal = sals.sals[0];
4a64f543 11864 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11865
c906108c 11866 if (*arg)
8a3fe4f8 11867 error (_("Junk at end of arguments."));
c5aa993b 11868
c906108c 11869 resolve_sal_pc (&sal);
c5aa993b 11870
186c406b
TT
11871 tp = inferior_thread ();
11872 thread = tp->num;
11873
883bc8d1
PA
11874 old_chain = make_cleanup (null_cleanup, NULL);
11875
8556afb4
PA
11876 /* Note linespec handling above invalidates the frame chain.
11877 Installing a breakpoint also invalidates the frame chain (as it
11878 may need to switch threads), so do any frame handling before
11879 that. */
11880
11881 frame = get_selected_frame (NULL);
11882 frame_gdbarch = get_frame_arch (frame);
11883 stack_frame_id = get_stack_frame_id (frame);
11884 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11885
ae66c1fc
EZ
11886 /* Keep within the current frame, or in frames called by the current
11887 one. */
edb3359d 11888
883bc8d1 11889 if (frame_id_p (caller_frame_id))
c906108c 11890 {
883bc8d1
PA
11891 struct symtab_and_line sal2;
11892
11893 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11894 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11895 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11896 sal2,
11897 caller_frame_id,
f107f563
VP
11898 bp_until);
11899 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11900
883bc8d1 11901 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11902 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11903 }
c5aa993b 11904
c70a6932
JK
11905 /* set_momentary_breakpoint could invalidate FRAME. */
11906 frame = NULL;
11907
883bc8d1
PA
11908 if (anywhere)
11909 /* If the user told us to continue until a specified location,
11910 we don't specify a frame at which we need to stop. */
11911 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11912 null_frame_id, bp_until);
11913 else
11914 /* Otherwise, specify the selected frame, because we want to stop
11915 only at the very same frame. */
11916 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11917 stack_frame_id, bp_until);
11918 make_cleanup_delete_breakpoint (breakpoint);
11919
a493e3e2 11920 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11921
4a64f543
MS
11922 /* If we are running asynchronously, and proceed call above has
11923 actually managed to start the target, arrange for breakpoints to
11924 be deleted when the target stops. Otherwise, we're already
11925 stopped and delete breakpoints via cleanup chain. */
f107f563 11926
8ea051c5 11927 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11928 {
bfec99b2
PA
11929 struct until_break_command_continuation_args *args;
11930 args = xmalloc (sizeof (*args));
f107f563 11931
bfec99b2
PA
11932 args->breakpoint = breakpoint;
11933 args->breakpoint2 = breakpoint2;
186c406b 11934 args->thread_num = thread;
f107f563
VP
11935
11936 discard_cleanups (old_chain);
95e54da7
PA
11937 add_continuation (inferior_thread (),
11938 until_break_command_continuation, args,
604ead4a 11939 xfree);
f107f563
VP
11940 }
11941 else
c5aa993b 11942 do_cleanups (old_chain);
c906108c 11943}
ae66c1fc 11944
c906108c
SS
11945/* This function attempts to parse an optional "if <cond>" clause
11946 from the arg string. If one is not found, it returns NULL.
c5aa993b 11947
c906108c
SS
11948 Else, it returns a pointer to the condition string. (It does not
11949 attempt to evaluate the string against a particular block.) And,
11950 it updates arg to point to the first character following the parsed
4a64f543 11951 if clause in the arg string. */
53a5351d 11952
916703c0 11953char *
fba45db2 11954ep_parse_optional_if_clause (char **arg)
c906108c 11955{
c5aa993b
JM
11956 char *cond_string;
11957
11958 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11959 return NULL;
c5aa993b 11960
4a64f543 11961 /* Skip the "if" keyword. */
c906108c 11962 (*arg) += 2;
c5aa993b 11963
c906108c 11964 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11965 condition string. */
e9cafbcc 11966 *arg = skip_spaces (*arg);
c906108c 11967 cond_string = *arg;
c5aa993b 11968
4a64f543
MS
11969 /* Assume that the condition occupies the remainder of the arg
11970 string. */
c906108c 11971 (*arg) += strlen (cond_string);
c5aa993b 11972
c906108c
SS
11973 return cond_string;
11974}
c5aa993b 11975
c906108c
SS
11976/* Commands to deal with catching events, such as signals, exceptions,
11977 process start/exit, etc. */
c5aa993b
JM
11978
11979typedef enum
11980{
44feb3ce
TT
11981 catch_fork_temporary, catch_vfork_temporary,
11982 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11983}
11984catch_fork_kind;
11985
c906108c 11986static void
cc59ec59
MS
11987catch_fork_command_1 (char *arg, int from_tty,
11988 struct cmd_list_element *command)
c906108c 11989{
a6d9a66e 11990 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11991 char *cond_string = NULL;
44feb3ce
TT
11992 catch_fork_kind fork_kind;
11993 int tempflag;
11994
11995 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11996 tempflag = (fork_kind == catch_fork_temporary
11997 || fork_kind == catch_vfork_temporary);
c5aa993b 11998
44feb3ce
TT
11999 if (!arg)
12000 arg = "";
e9cafbcc 12001 arg = skip_spaces (arg);
c5aa993b 12002
c906108c 12003 /* The allowed syntax is:
c5aa993b
JM
12004 catch [v]fork
12005 catch [v]fork if <cond>
12006
4a64f543 12007 First, check if there's an if clause. */
c906108c 12008 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12009
c906108c 12010 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12011 error (_("Junk at end of arguments."));
c5aa993b 12012
c906108c 12013 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12014 and enable reporting of such events. */
c5aa993b
JM
12015 switch (fork_kind)
12016 {
44feb3ce
TT
12017 case catch_fork_temporary:
12018 case catch_fork_permanent:
a6d9a66e 12019 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12020 &catch_fork_breakpoint_ops);
c906108c 12021 break;
44feb3ce
TT
12022 case catch_vfork_temporary:
12023 case catch_vfork_permanent:
a6d9a66e 12024 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12025 &catch_vfork_breakpoint_ops);
c906108c 12026 break;
c5aa993b 12027 default:
8a3fe4f8 12028 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12029 break;
c5aa993b 12030 }
c906108c
SS
12031}
12032
12033static void
cc59ec59
MS
12034catch_exec_command_1 (char *arg, int from_tty,
12035 struct cmd_list_element *command)
c906108c 12036{
b4d90040 12037 struct exec_catchpoint *c;
a6d9a66e 12038 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12039 int tempflag;
c5aa993b 12040 char *cond_string = NULL;
c906108c 12041
44feb3ce
TT
12042 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12043
12044 if (!arg)
12045 arg = "";
e9cafbcc 12046 arg = skip_spaces (arg);
c906108c
SS
12047
12048 /* The allowed syntax is:
c5aa993b
JM
12049 catch exec
12050 catch exec if <cond>
c906108c 12051
4a64f543 12052 First, check if there's an if clause. */
c906108c
SS
12053 cond_string = ep_parse_optional_if_clause (&arg);
12054
12055 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12056 error (_("Junk at end of arguments."));
c906108c 12057
b4d90040
PA
12058 c = XNEW (struct exec_catchpoint);
12059 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12060 &catch_exec_breakpoint_ops);
12061 c->exec_pathname = NULL;
12062
3ea46bff 12063 install_breakpoint (0, &c->base, 1);
c906108c 12064}
c5aa993b 12065
9ac4176b 12066void
28010a5d
PA
12067init_ada_exception_breakpoint (struct breakpoint *b,
12068 struct gdbarch *gdbarch,
12069 struct symtab_and_line sal,
12070 char *addr_string,
c0a91b2b 12071 const struct breakpoint_ops *ops,
28010a5d 12072 int tempflag,
349774ef 12073 int enabled,
28010a5d 12074 int from_tty)
f7f9143b 12075{
f7f9143b
JB
12076 if (from_tty)
12077 {
5af949e3
UW
12078 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12079 if (!loc_gdbarch)
12080 loc_gdbarch = gdbarch;
12081
6c95b8df
PA
12082 describe_other_breakpoints (loc_gdbarch,
12083 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12084 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12085 version for exception catchpoints, because two catchpoints
12086 used for different exception names will use the same address.
12087 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12088 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12089 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12090 the user what type of catchpoint it is. The above is good
12091 enough for now, though. */
12092 }
12093
28010a5d 12094 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12095
349774ef 12096 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12097 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12098 b->addr_string = addr_string;
12099 b->language = language_ada;
f7f9143b
JB
12100}
12101
a96d9b2e
SDJ
12102/* Splits the argument using space as delimiter. Returns an xmalloc'd
12103 filter list, or NULL if no filtering is required. */
12104static VEC(int) *
12105catch_syscall_split_args (char *arg)
12106{
12107 VEC(int) *result = NULL;
29d0bb3d 12108 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12109
12110 while (*arg != '\0')
12111 {
12112 int i, syscall_number;
12113 char *endptr;
12114 char cur_name[128];
12115 struct syscall s;
12116
12117 /* Skip whitespace. */
529480d0 12118 arg = skip_spaces (arg);
a96d9b2e
SDJ
12119
12120 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12121 cur_name[i] = arg[i];
12122 cur_name[i] = '\0';
12123 arg += i;
12124
12125 /* Check if the user provided a syscall name or a number. */
12126 syscall_number = (int) strtol (cur_name, &endptr, 0);
12127 if (*endptr == '\0')
bccd0dd2 12128 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12129 else
12130 {
12131 /* We have a name. Let's check if it's valid and convert it
12132 to a number. */
12133 get_syscall_by_name (cur_name, &s);
12134
12135 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12136 /* Here we have to issue an error instead of a warning,
12137 because GDB cannot do anything useful if there's no
12138 syscall number to be caught. */
a96d9b2e
SDJ
12139 error (_("Unknown syscall name '%s'."), cur_name);
12140 }
12141
12142 /* Ok, it's valid. */
12143 VEC_safe_push (int, result, s.number);
12144 }
12145
12146 discard_cleanups (cleanup);
12147 return result;
12148}
12149
12150/* Implement the "catch syscall" command. */
12151
12152static void
cc59ec59
MS
12153catch_syscall_command_1 (char *arg, int from_tty,
12154 struct cmd_list_element *command)
a96d9b2e
SDJ
12155{
12156 int tempflag;
12157 VEC(int) *filter;
12158 struct syscall s;
12159 struct gdbarch *gdbarch = get_current_arch ();
12160
12161 /* Checking if the feature if supported. */
12162 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12163 error (_("The feature 'catch syscall' is not supported on \
ea666128 12164this architecture yet."));
a96d9b2e
SDJ
12165
12166 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12167
e9cafbcc 12168 arg = skip_spaces (arg);
a96d9b2e
SDJ
12169
12170 /* We need to do this first "dummy" translation in order
12171 to get the syscall XML file loaded or, most important,
12172 to display a warning to the user if there's no XML file
12173 for his/her architecture. */
12174 get_syscall_by_number (0, &s);
12175
12176 /* The allowed syntax is:
12177 catch syscall
12178 catch syscall <name | number> [<name | number> ... <name | number>]
12179
12180 Let's check if there's a syscall name. */
12181
12182 if (arg != NULL)
12183 filter = catch_syscall_split_args (arg);
12184 else
12185 filter = NULL;
12186
12187 create_syscall_event_catchpoint (tempflag, filter,
12188 &catch_syscall_breakpoint_ops);
12189}
12190
c906108c 12191static void
fba45db2 12192catch_command (char *arg, int from_tty)
c906108c 12193{
44feb3ce 12194 error (_("Catch requires an event name."));
c906108c
SS
12195}
12196\f
12197
12198static void
fba45db2 12199tcatch_command (char *arg, int from_tty)
c906108c 12200{
44feb3ce 12201 error (_("Catch requires an event name."));
c906108c
SS
12202}
12203
8a2c437b
TT
12204/* A qsort comparison function that sorts breakpoints in order. */
12205
12206static int
12207compare_breakpoints (const void *a, const void *b)
12208{
12209 const breakpoint_p *ba = a;
12210 uintptr_t ua = (uintptr_t) *ba;
12211 const breakpoint_p *bb = b;
12212 uintptr_t ub = (uintptr_t) *bb;
12213
12214 if ((*ba)->number < (*bb)->number)
12215 return -1;
12216 else if ((*ba)->number > (*bb)->number)
12217 return 1;
12218
12219 /* Now sort by address, in case we see, e..g, two breakpoints with
12220 the number 0. */
12221 if (ua < ub)
12222 return -1;
94b0e70d 12223 return ua > ub ? 1 : 0;
8a2c437b
TT
12224}
12225
80f8a6eb 12226/* Delete breakpoints by address or line. */
c906108c
SS
12227
12228static void
fba45db2 12229clear_command (char *arg, int from_tty)
c906108c 12230{
8a2c437b 12231 struct breakpoint *b, *prev;
d6e956e5
VP
12232 VEC(breakpoint_p) *found = 0;
12233 int ix;
c906108c
SS
12234 int default_match;
12235 struct symtabs_and_lines sals;
12236 struct symtab_and_line sal;
c906108c 12237 int i;
8a2c437b 12238 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12239
12240 if (arg)
12241 {
39cf75f7
DE
12242 sals = decode_line_with_current_source (arg,
12243 (DECODE_LINE_FUNFIRSTLINE
12244 | DECODE_LINE_LIST_MODE));
cf4ded82 12245 make_cleanup (xfree, sals.sals);
c906108c
SS
12246 default_match = 0;
12247 }
12248 else
12249 {
c5aa993b 12250 sals.sals = (struct symtab_and_line *)
c906108c 12251 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12252 make_cleanup (xfree, sals.sals);
4a64f543 12253 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12254
12255 /* Set sal's line, symtab, pc, and pspace to the values
12256 corresponding to the last call to print_frame_info. If the
12257 codepoint is not valid, this will set all the fields to 0. */
12258 get_last_displayed_sal (&sal);
c906108c 12259 if (sal.symtab == 0)
8a3fe4f8 12260 error (_("No source file specified."));
c906108c
SS
12261
12262 sals.sals[0] = sal;
12263 sals.nelts = 1;
12264
12265 default_match = 1;
12266 }
12267
4a64f543
MS
12268 /* We don't call resolve_sal_pc here. That's not as bad as it
12269 seems, because all existing breakpoints typically have both
12270 file/line and pc set. So, if clear is given file/line, we can
12271 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12272
12273 We only support clearing given the address explicitly
12274 present in breakpoint table. Say, we've set breakpoint
4a64f543 12275 at file:line. There were several PC values for that file:line,
ed0616c6 12276 due to optimization, all in one block.
4a64f543
MS
12277
12278 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12279 PC corresponding to the same file:line, the breakpoint won't
12280 be cleared. We probably can still clear the breakpoint, but
12281 since the other PC value is never presented to user, user
12282 can only find it by guessing, and it does not seem important
12283 to support that. */
12284
4a64f543
MS
12285 /* For each line spec given, delete bps which correspond to it. Do
12286 it in two passes, solely to preserve the current behavior that
12287 from_tty is forced true if we delete more than one
12288 breakpoint. */
c906108c 12289
80f8a6eb 12290 found = NULL;
8a2c437b 12291 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12292 for (i = 0; i < sals.nelts; i++)
12293 {
05cba821
JK
12294 const char *sal_fullname;
12295
c906108c 12296 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12297 If line given (pc == 0), clear all bpts on specified line.
12298 If defaulting, clear all bpts on default line
c906108c 12299 or at default pc.
c5aa993b
JM
12300
12301 defaulting sal.pc != 0 tests to do
12302
12303 0 1 pc
12304 1 1 pc _and_ line
12305 0 0 line
12306 1 0 <can't happen> */
c906108c
SS
12307
12308 sal = sals.sals[i];
05cba821
JK
12309 sal_fullname = (sal.symtab == NULL
12310 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12311
4a64f543 12312 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12313 ALL_BREAKPOINTS (b)
c5aa993b 12314 {
0d381245 12315 int match = 0;
4a64f543 12316 /* Are we going to delete b? */
cc60f2e3 12317 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12318 {
12319 struct bp_location *loc = b->loc;
12320 for (; loc; loc = loc->next)
12321 {
f8eba3c6
TT
12322 /* If the user specified file:line, don't allow a PC
12323 match. This matches historical gdb behavior. */
12324 int pc_match = (!sal.explicit_line
12325 && sal.pc
12326 && (loc->pspace == sal.pspace)
12327 && (loc->address == sal.pc)
12328 && (!section_is_overlay (loc->section)
12329 || loc->section == sal.section));
4aac40c8
TT
12330 int line_match = 0;
12331
12332 if ((default_match || sal.explicit_line)
2f202fde 12333 && loc->symtab != NULL
05cba821 12334 && sal_fullname != NULL
4aac40c8 12335 && sal.pspace == loc->pspace
05cba821
JK
12336 && loc->line_number == sal.line
12337 && filename_cmp (symtab_to_fullname (loc->symtab),
12338 sal_fullname) == 0)
12339 line_match = 1;
4aac40c8 12340
0d381245
VP
12341 if (pc_match || line_match)
12342 {
12343 match = 1;
12344 break;
12345 }
12346 }
12347 }
12348
12349 if (match)
d6e956e5 12350 VEC_safe_push(breakpoint_p, found, b);
c906108c 12351 }
80f8a6eb 12352 }
8a2c437b 12353
80f8a6eb 12354 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12355 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12356 {
12357 if (arg)
8a3fe4f8 12358 error (_("No breakpoint at %s."), arg);
80f8a6eb 12359 else
8a3fe4f8 12360 error (_("No breakpoint at this line."));
80f8a6eb 12361 }
c906108c 12362
8a2c437b
TT
12363 /* Remove duplicates from the vec. */
12364 qsort (VEC_address (breakpoint_p, found),
12365 VEC_length (breakpoint_p, found),
12366 sizeof (breakpoint_p),
12367 compare_breakpoints);
12368 prev = VEC_index (breakpoint_p, found, 0);
12369 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12370 {
12371 if (b == prev)
12372 {
12373 VEC_ordered_remove (breakpoint_p, found, ix);
12374 --ix;
12375 }
12376 }
12377
d6e956e5 12378 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12379 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12380 if (from_tty)
a3f17187 12381 {
d6e956e5 12382 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12383 printf_unfiltered (_("Deleted breakpoint "));
12384 else
12385 printf_unfiltered (_("Deleted breakpoints "));
12386 }
d6e956e5
VP
12387
12388 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12389 {
c5aa993b 12390 if (from_tty)
d6e956e5
VP
12391 printf_unfiltered ("%d ", b->number);
12392 delete_breakpoint (b);
c906108c 12393 }
80f8a6eb
MS
12394 if (from_tty)
12395 putchar_unfiltered ('\n');
8a2c437b
TT
12396
12397 do_cleanups (cleanups);
c906108c
SS
12398}
12399\f
12400/* Delete breakpoint in BS if they are `delete' breakpoints and
12401 all breakpoints that are marked for deletion, whether hit or not.
12402 This is called after any breakpoint is hit, or after errors. */
12403
12404void
fba45db2 12405breakpoint_auto_delete (bpstat bs)
c906108c 12406{
35df4500 12407 struct breakpoint *b, *b_tmp;
c906108c
SS
12408
12409 for (; bs; bs = bs->next)
f431efe5
PA
12410 if (bs->breakpoint_at
12411 && bs->breakpoint_at->disposition == disp_del
c906108c 12412 && bs->stop)
f431efe5 12413 delete_breakpoint (bs->breakpoint_at);
c906108c 12414
35df4500 12415 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12416 {
b5de0fa7 12417 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12418 delete_breakpoint (b);
12419 }
c906108c
SS
12420}
12421
4a64f543
MS
12422/* A comparison function for bp_location AP and BP being interfaced to
12423 qsort. Sort elements primarily by their ADDRESS (no matter what
12424 does breakpoint_address_is_meaningful say for its OWNER),
12425 secondarily by ordering first bp_permanent OWNERed elements and
12426 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12427 qsort being an unstable algorithm. */
876fa593
JK
12428
12429static int
494cfb0f 12430bp_location_compare (const void *ap, const void *bp)
876fa593 12431{
494cfb0f
JK
12432 struct bp_location *a = *(void **) ap;
12433 struct bp_location *b = *(void **) bp;
2bdf28a0 12434 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12435 int a_perm = a->owner->enable_state == bp_permanent;
12436 int b_perm = b->owner->enable_state == bp_permanent;
12437
12438 if (a->address != b->address)
12439 return (a->address > b->address) - (a->address < b->address);
12440
dea2aa5f
LM
12441 /* Sort locations at the same address by their pspace number, keeping
12442 locations of the same inferior (in a multi-inferior environment)
12443 grouped. */
12444
12445 if (a->pspace->num != b->pspace->num)
12446 return ((a->pspace->num > b->pspace->num)
12447 - (a->pspace->num < b->pspace->num));
12448
876fa593
JK
12449 /* Sort permanent breakpoints first. */
12450 if (a_perm != b_perm)
12451 return (a_perm < b_perm) - (a_perm > b_perm);
12452
c56a97f9
JK
12453 /* Make the internal GDB representation stable across GDB runs
12454 where A and B memory inside GDB can differ. Breakpoint locations of
12455 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12456
12457 if (a->owner->number != b->owner->number)
c56a97f9
JK
12458 return ((a->owner->number > b->owner->number)
12459 - (a->owner->number < b->owner->number));
876fa593
JK
12460
12461 return (a > b) - (a < b);
12462}
12463
876fa593 12464/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12465 bp_location_shadow_len_after_address_max according to the current
12466 content of the bp_location array. */
f7545552
TT
12467
12468static void
876fa593 12469bp_location_target_extensions_update (void)
f7545552 12470{
876fa593
JK
12471 struct bp_location *bl, **blp_tmp;
12472
12473 bp_location_placed_address_before_address_max = 0;
12474 bp_location_shadow_len_after_address_max = 0;
12475
12476 ALL_BP_LOCATIONS (bl, blp_tmp)
12477 {
12478 CORE_ADDR start, end, addr;
12479
12480 if (!bp_location_has_shadow (bl))
12481 continue;
12482
12483 start = bl->target_info.placed_address;
12484 end = start + bl->target_info.shadow_len;
12485
12486 gdb_assert (bl->address >= start);
12487 addr = bl->address - start;
12488 if (addr > bp_location_placed_address_before_address_max)
12489 bp_location_placed_address_before_address_max = addr;
12490
12491 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12492
12493 gdb_assert (bl->address < end);
12494 addr = end - bl->address;
12495 if (addr > bp_location_shadow_len_after_address_max)
12496 bp_location_shadow_len_after_address_max = addr;
12497 }
f7545552
TT
12498}
12499
1e4d1764
YQ
12500/* Download tracepoint locations if they haven't been. */
12501
12502static void
12503download_tracepoint_locations (void)
12504{
7ed2c994 12505 struct breakpoint *b;
1e4d1764
YQ
12506 struct cleanup *old_chain;
12507
12508 if (!target_can_download_tracepoint ())
12509 return;
12510
12511 old_chain = save_current_space_and_thread ();
12512
7ed2c994 12513 ALL_TRACEPOINTS (b)
1e4d1764 12514 {
7ed2c994 12515 struct bp_location *bl;
1e4d1764 12516 struct tracepoint *t;
f2a8bc8a 12517 int bp_location_downloaded = 0;
1e4d1764 12518
7ed2c994 12519 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12520 ? !may_insert_fast_tracepoints
12521 : !may_insert_tracepoints))
12522 continue;
12523
7ed2c994
YQ
12524 for (bl = b->loc; bl; bl = bl->next)
12525 {
12526 /* In tracepoint, locations are _never_ duplicated, so
12527 should_be_inserted is equivalent to
12528 unduplicated_should_be_inserted. */
12529 if (!should_be_inserted (bl) || bl->inserted)
12530 continue;
1e4d1764 12531
7ed2c994 12532 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12533
7ed2c994 12534 target_download_tracepoint (bl);
1e4d1764 12535
7ed2c994 12536 bl->inserted = 1;
f2a8bc8a 12537 bp_location_downloaded = 1;
7ed2c994
YQ
12538 }
12539 t = (struct tracepoint *) b;
12540 t->number_on_target = b->number;
f2a8bc8a
YQ
12541 if (bp_location_downloaded)
12542 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12543 }
12544
12545 do_cleanups (old_chain);
12546}
12547
934709f0
PW
12548/* Swap the insertion/duplication state between two locations. */
12549
12550static void
12551swap_insertion (struct bp_location *left, struct bp_location *right)
12552{
12553 const int left_inserted = left->inserted;
12554 const int left_duplicate = left->duplicate;
b775012e 12555 const int left_needs_update = left->needs_update;
934709f0
PW
12556 const struct bp_target_info left_target_info = left->target_info;
12557
1e4d1764
YQ
12558 /* Locations of tracepoints can never be duplicated. */
12559 if (is_tracepoint (left->owner))
12560 gdb_assert (!left->duplicate);
12561 if (is_tracepoint (right->owner))
12562 gdb_assert (!right->duplicate);
12563
934709f0
PW
12564 left->inserted = right->inserted;
12565 left->duplicate = right->duplicate;
b775012e 12566 left->needs_update = right->needs_update;
934709f0
PW
12567 left->target_info = right->target_info;
12568 right->inserted = left_inserted;
12569 right->duplicate = left_duplicate;
b775012e 12570 right->needs_update = left_needs_update;
934709f0
PW
12571 right->target_info = left_target_info;
12572}
12573
b775012e
LM
12574/* Force the re-insertion of the locations at ADDRESS. This is called
12575 once a new/deleted/modified duplicate location is found and we are evaluating
12576 conditions on the target's side. Such conditions need to be updated on
12577 the target. */
12578
12579static void
12580force_breakpoint_reinsertion (struct bp_location *bl)
12581{
12582 struct bp_location **locp = NULL, **loc2p;
12583 struct bp_location *loc;
12584 CORE_ADDR address = 0;
12585 int pspace_num;
12586
12587 address = bl->address;
12588 pspace_num = bl->pspace->num;
12589
12590 /* This is only meaningful if the target is
12591 evaluating conditions and if the user has
12592 opted for condition evaluation on the target's
12593 side. */
12594 if (gdb_evaluates_breakpoint_condition_p ()
12595 || !target_supports_evaluation_of_breakpoint_conditions ())
12596 return;
12597
12598 /* Flag all breakpoint locations with this address and
12599 the same program space as the location
12600 as "its condition has changed". We need to
12601 update the conditions on the target's side. */
12602 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12603 {
12604 loc = *loc2p;
12605
12606 if (!is_breakpoint (loc->owner)
12607 || pspace_num != loc->pspace->num)
12608 continue;
12609
12610 /* Flag the location appropriately. We use a different state to
12611 let everyone know that we already updated the set of locations
12612 with addr bl->address and program space bl->pspace. This is so
12613 we don't have to keep calling these functions just to mark locations
12614 that have already been marked. */
12615 loc->condition_changed = condition_updated;
12616
12617 /* Free the agent expression bytecode as well. We will compute
12618 it later on. */
12619 if (loc->cond_bytecode)
12620 {
12621 free_agent_expr (loc->cond_bytecode);
12622 loc->cond_bytecode = NULL;
12623 }
12624 }
12625}
44702360
PA
12626/* Called whether new breakpoints are created, or existing breakpoints
12627 deleted, to update the global location list and recompute which
12628 locations are duplicate of which.
b775012e 12629
04086b45
PA
12630 The INSERT_MODE flag determines whether locations may not, may, or
12631 shall be inserted now. See 'enum ugll_insert_mode' for more
12632 info. */
b60e7edf 12633
0d381245 12634static void
44702360 12635update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12636{
74960c60 12637 struct breakpoint *b;
876fa593 12638 struct bp_location **locp, *loc;
f7545552 12639 struct cleanup *cleanups;
b775012e
LM
12640 /* Last breakpoint location address that was marked for update. */
12641 CORE_ADDR last_addr = 0;
12642 /* Last breakpoint location program space that was marked for update. */
12643 int last_pspace_num = -1;
f7545552 12644
2d134ed3
PA
12645 /* Used in the duplicates detection below. When iterating over all
12646 bp_locations, points to the first bp_location of a given address.
12647 Breakpoints and watchpoints of different types are never
12648 duplicates of each other. Keep one pointer for each type of
12649 breakpoint/watchpoint, so we only need to loop over all locations
12650 once. */
12651 struct bp_location *bp_loc_first; /* breakpoint */
12652 struct bp_location *wp_loc_first; /* hardware watchpoint */
12653 struct bp_location *awp_loc_first; /* access watchpoint */
12654 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12655
4a64f543
MS
12656 /* Saved former bp_location array which we compare against the newly
12657 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12658 struct bp_location **old_location, **old_locp;
12659 unsigned old_location_count;
12660
12661 old_location = bp_location;
12662 old_location_count = bp_location_count;
12663 bp_location = NULL;
12664 bp_location_count = 0;
12665 cleanups = make_cleanup (xfree, old_location);
0d381245 12666
74960c60 12667 ALL_BREAKPOINTS (b)
876fa593
JK
12668 for (loc = b->loc; loc; loc = loc->next)
12669 bp_location_count++;
12670
12671 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12672 locp = bp_location;
12673 ALL_BREAKPOINTS (b)
12674 for (loc = b->loc; loc; loc = loc->next)
12675 *locp++ = loc;
12676 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12677 bp_location_compare);
876fa593
JK
12678
12679 bp_location_target_extensions_update ();
74960c60 12680
4a64f543
MS
12681 /* Identify bp_location instances that are no longer present in the
12682 new list, and therefore should be freed. Note that it's not
12683 necessary that those locations should be removed from inferior --
12684 if there's another location at the same address (previously
12685 marked as duplicate), we don't need to remove/insert the
12686 location.
876fa593 12687
4a64f543
MS
12688 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12689 and former bp_location array state respectively. */
876fa593
JK
12690
12691 locp = bp_location;
12692 for (old_locp = old_location; old_locp < old_location + old_location_count;
12693 old_locp++)
74960c60 12694 {
876fa593 12695 struct bp_location *old_loc = *old_locp;
c7d46a38 12696 struct bp_location **loc2p;
876fa593 12697
e5dd4106 12698 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12699 not, we have to free it. */
c7d46a38 12700 int found_object = 0;
20874c92
VP
12701 /* Tells if the location should remain inserted in the target. */
12702 int keep_in_target = 0;
12703 int removed = 0;
876fa593 12704
4a64f543
MS
12705 /* Skip LOCP entries which will definitely never be needed.
12706 Stop either at or being the one matching OLD_LOC. */
876fa593 12707 while (locp < bp_location + bp_location_count
c7d46a38 12708 && (*locp)->address < old_loc->address)
876fa593 12709 locp++;
c7d46a38
PA
12710
12711 for (loc2p = locp;
12712 (loc2p < bp_location + bp_location_count
12713 && (*loc2p)->address == old_loc->address);
12714 loc2p++)
12715 {
b775012e
LM
12716 /* Check if this is a new/duplicated location or a duplicated
12717 location that had its condition modified. If so, we want to send
12718 its condition to the target if evaluation of conditions is taking
12719 place there. */
12720 if ((*loc2p)->condition_changed == condition_modified
12721 && (last_addr != old_loc->address
12722 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12723 {
b775012e
LM
12724 force_breakpoint_reinsertion (*loc2p);
12725 last_pspace_num = old_loc->pspace->num;
c7d46a38 12726 }
b775012e
LM
12727
12728 if (*loc2p == old_loc)
12729 found_object = 1;
c7d46a38 12730 }
74960c60 12731
b775012e
LM
12732 /* We have already handled this address, update it so that we don't
12733 have to go through updates again. */
12734 last_addr = old_loc->address;
12735
12736 /* Target-side condition evaluation: Handle deleted locations. */
12737 if (!found_object)
12738 force_breakpoint_reinsertion (old_loc);
12739
4a64f543
MS
12740 /* If this location is no longer present, and inserted, look if
12741 there's maybe a new location at the same address. If so,
12742 mark that one inserted, and don't remove this one. This is
12743 needed so that we don't have a time window where a breakpoint
12744 at certain location is not inserted. */
74960c60 12745
876fa593 12746 if (old_loc->inserted)
0d381245 12747 {
4a64f543
MS
12748 /* If the location is inserted now, we might have to remove
12749 it. */
74960c60 12750
876fa593 12751 if (found_object && should_be_inserted (old_loc))
74960c60 12752 {
4a64f543
MS
12753 /* The location is still present in the location list,
12754 and still should be inserted. Don't do anything. */
20874c92 12755 keep_in_target = 1;
74960c60
VP
12756 }
12757 else
12758 {
b775012e
LM
12759 /* This location still exists, but it won't be kept in the
12760 target since it may have been disabled. We proceed to
12761 remove its target-side condition. */
12762
4a64f543
MS
12763 /* The location is either no longer present, or got
12764 disabled. See if there's another location at the
12765 same address, in which case we don't need to remove
12766 this one from the target. */
876fa593 12767
2bdf28a0 12768 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12769 if (breakpoint_address_is_meaningful (old_loc->owner))
12770 {
876fa593 12771 for (loc2p = locp;
c7d46a38
PA
12772 (loc2p < bp_location + bp_location_count
12773 && (*loc2p)->address == old_loc->address);
876fa593
JK
12774 loc2p++)
12775 {
12776 struct bp_location *loc2 = *loc2p;
12777
2d134ed3 12778 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12779 {
85d721b8
PA
12780 /* Read watchpoint locations are switched to
12781 access watchpoints, if the former are not
12782 supported, but the latter are. */
12783 if (is_hardware_watchpoint (old_loc->owner))
12784 {
12785 gdb_assert (is_hardware_watchpoint (loc2->owner));
12786 loc2->watchpoint_type = old_loc->watchpoint_type;
12787 }
12788
934709f0
PW
12789 /* loc2 is a duplicated location. We need to check
12790 if it should be inserted in case it will be
12791 unduplicated. */
12792 if (loc2 != old_loc
12793 && unduplicated_should_be_inserted (loc2))
c7d46a38 12794 {
934709f0 12795 swap_insertion (old_loc, loc2);
c7d46a38
PA
12796 keep_in_target = 1;
12797 break;
12798 }
876fa593
JK
12799 }
12800 }
12801 }
74960c60
VP
12802 }
12803
20874c92
VP
12804 if (!keep_in_target)
12805 {
876fa593 12806 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12807 {
4a64f543
MS
12808 /* This is just about all we can do. We could keep
12809 this location on the global list, and try to
12810 remove it next time, but there's no particular
12811 reason why we will succeed next time.
20874c92 12812
4a64f543
MS
12813 Note that at this point, old_loc->owner is still
12814 valid, as delete_breakpoint frees the breakpoint
12815 only after calling us. */
3e43a32a
MS
12816 printf_filtered (_("warning: Error removing "
12817 "breakpoint %d\n"),
876fa593 12818 old_loc->owner->number);
20874c92
VP
12819 }
12820 removed = 1;
12821 }
0d381245 12822 }
74960c60
VP
12823
12824 if (!found_object)
1c5cfe86 12825 {
db82e815
PA
12826 if (removed && non_stop
12827 && breakpoint_address_is_meaningful (old_loc->owner)
12828 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12829 {
db82e815
PA
12830 /* This location was removed from the target. In
12831 non-stop mode, a race condition is possible where
12832 we've removed a breakpoint, but stop events for that
12833 breakpoint are already queued and will arrive later.
12834 We apply an heuristic to be able to distinguish such
12835 SIGTRAPs from other random SIGTRAPs: we keep this
12836 breakpoint location for a bit, and will retire it
12837 after we see some number of events. The theory here
12838 is that reporting of events should, "on the average",
12839 be fair, so after a while we'll see events from all
12840 threads that have anything of interest, and no longer
12841 need to keep this breakpoint location around. We
12842 don't hold locations forever so to reduce chances of
12843 mistaking a non-breakpoint SIGTRAP for a breakpoint
12844 SIGTRAP.
12845
12846 The heuristic failing can be disastrous on
12847 decr_pc_after_break targets.
12848
12849 On decr_pc_after_break targets, like e.g., x86-linux,
12850 if we fail to recognize a late breakpoint SIGTRAP,
12851 because events_till_retirement has reached 0 too
12852 soon, we'll fail to do the PC adjustment, and report
12853 a random SIGTRAP to the user. When the user resumes
12854 the inferior, it will most likely immediately crash
2dec564e 12855 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12856 corrupted, because of being resumed e.g., in the
12857 middle of a multi-byte instruction, or skipped a
12858 one-byte instruction. This was actually seen happen
12859 on native x86-linux, and should be less rare on
12860 targets that do not support new thread events, like
12861 remote, due to the heuristic depending on
12862 thread_count.
12863
12864 Mistaking a random SIGTRAP for a breakpoint trap
12865 causes similar symptoms (PC adjustment applied when
12866 it shouldn't), but then again, playing with SIGTRAPs
12867 behind the debugger's back is asking for trouble.
12868
12869 Since hardware watchpoint traps are always
12870 distinguishable from other traps, so we don't need to
12871 apply keep hardware watchpoint moribund locations
12872 around. We simply always ignore hardware watchpoint
12873 traps we can no longer explain. */
12874
876fa593
JK
12875 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12876 old_loc->owner = NULL;
20874c92 12877
876fa593 12878 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12879 }
12880 else
f431efe5
PA
12881 {
12882 old_loc->owner = NULL;
12883 decref_bp_location (&old_loc);
12884 }
20874c92 12885 }
74960c60 12886 }
1c5cfe86 12887
348d480f
PA
12888 /* Rescan breakpoints at the same address and section, marking the
12889 first one as "first" and any others as "duplicates". This is so
12890 that the bpt instruction is only inserted once. If we have a
12891 permanent breakpoint at the same place as BPT, make that one the
12892 official one, and the rest as duplicates. Permanent breakpoints
12893 are sorted first for the same address.
12894
12895 Do the same for hardware watchpoints, but also considering the
12896 watchpoint's type (regular/access/read) and length. */
12897
12898 bp_loc_first = NULL;
12899 wp_loc_first = NULL;
12900 awp_loc_first = NULL;
12901 rwp_loc_first = NULL;
12902 ALL_BP_LOCATIONS (loc, locp)
12903 {
12904 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12905 non-NULL. */
348d480f 12906 struct bp_location **loc_first_p;
d3fbdd86 12907 b = loc->owner;
348d480f 12908
6f380991 12909 if (!unduplicated_should_be_inserted (loc)
348d480f 12910 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12911 /* Don't detect duplicate for tracepoint locations because they are
12912 never duplicated. See the comments in field `duplicate' of
12913 `struct bp_location'. */
348d480f 12914 || is_tracepoint (b))
b775012e
LM
12915 {
12916 /* Clear the condition modification flag. */
12917 loc->condition_changed = condition_unchanged;
12918 continue;
12919 }
348d480f
PA
12920
12921 /* Permanent breakpoint should always be inserted. */
12922 if (b->enable_state == bp_permanent && ! loc->inserted)
12923 internal_error (__FILE__, __LINE__,
12924 _("allegedly permanent breakpoint is not "
12925 "actually inserted"));
12926
12927 if (b->type == bp_hardware_watchpoint)
12928 loc_first_p = &wp_loc_first;
12929 else if (b->type == bp_read_watchpoint)
12930 loc_first_p = &rwp_loc_first;
12931 else if (b->type == bp_access_watchpoint)
12932 loc_first_p = &awp_loc_first;
12933 else
12934 loc_first_p = &bp_loc_first;
12935
12936 if (*loc_first_p == NULL
12937 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12938 || !breakpoint_locations_match (loc, *loc_first_p))
12939 {
12940 *loc_first_p = loc;
12941 loc->duplicate = 0;
b775012e
LM
12942
12943 if (is_breakpoint (loc->owner) && loc->condition_changed)
12944 {
12945 loc->needs_update = 1;
12946 /* Clear the condition modification flag. */
12947 loc->condition_changed = condition_unchanged;
12948 }
348d480f
PA
12949 continue;
12950 }
12951
934709f0
PW
12952
12953 /* This and the above ensure the invariant that the first location
12954 is not duplicated, and is the inserted one.
12955 All following are marked as duplicated, and are not inserted. */
12956 if (loc->inserted)
12957 swap_insertion (loc, *loc_first_p);
348d480f
PA
12958 loc->duplicate = 1;
12959
b775012e
LM
12960 /* Clear the condition modification flag. */
12961 loc->condition_changed = condition_unchanged;
12962
348d480f
PA
12963 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12964 && b->enable_state != bp_permanent)
12965 internal_error (__FILE__, __LINE__,
12966 _("another breakpoint was inserted on top of "
12967 "a permanent breakpoint"));
12968 }
12969
a25a5a45 12970 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12971 {
04086b45 12972 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12973 insert_breakpoint_locations ();
12974 else
12975 {
44702360
PA
12976 /* Even though the caller told us to not insert new
12977 locations, we may still need to update conditions on the
12978 target's side of breakpoints that were already inserted
12979 if the target is evaluating breakpoint conditions. We
b775012e
LM
12980 only update conditions for locations that are marked
12981 "needs_update". */
12982 update_inserted_breakpoint_locations ();
12983 }
12984 }
348d480f 12985
04086b45 12986 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12987 download_tracepoint_locations ();
12988
348d480f
PA
12989 do_cleanups (cleanups);
12990}
12991
12992void
12993breakpoint_retire_moribund (void)
12994{
12995 struct bp_location *loc;
12996 int ix;
12997
12998 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12999 if (--(loc->events_till_retirement) == 0)
13000 {
13001 decref_bp_location (&loc);
13002 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13003 --ix;
13004 }
13005}
13006
13007static void
44702360 13008update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13009{
bfd189b1 13010 volatile struct gdb_exception e;
348d480f
PA
13011
13012 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13013 update_global_location_list (insert_mode);
348d480f
PA
13014}
13015
13016/* Clear BKP from a BPS. */
13017
13018static void
13019bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13020{
13021 bpstat bs;
13022
13023 for (bs = bps; bs; bs = bs->next)
13024 if (bs->breakpoint_at == bpt)
13025 {
13026 bs->breakpoint_at = NULL;
13027 bs->old_val = NULL;
13028 /* bs->commands will be freed later. */
13029 }
13030}
13031
13032/* Callback for iterate_over_threads. */
13033static int
13034bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13035{
13036 struct breakpoint *bpt = data;
13037
13038 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13039 return 0;
13040}
13041
13042/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13043 callbacks. */
13044
13045static void
13046say_where (struct breakpoint *b)
13047{
13048 struct value_print_options opts;
13049
13050 get_user_print_options (&opts);
13051
13052 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13053 single string. */
13054 if (b->loc == NULL)
13055 {
13056 printf_filtered (_(" (%s) pending."), b->addr_string);
13057 }
13058 else
13059 {
2f202fde 13060 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13061 {
13062 printf_filtered (" at ");
13063 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13064 gdb_stdout);
13065 }
2f202fde 13066 if (b->loc->symtab != NULL)
f8eba3c6
TT
13067 {
13068 /* If there is a single location, we can print the location
13069 more nicely. */
13070 if (b->loc->next == NULL)
13071 printf_filtered (": file %s, line %d.",
05cba821
JK
13072 symtab_to_filename_for_display (b->loc->symtab),
13073 b->loc->line_number);
f8eba3c6
TT
13074 else
13075 /* This is not ideal, but each location may have a
13076 different file name, and this at least reflects the
13077 real situation somewhat. */
13078 printf_filtered (": %s.", b->addr_string);
13079 }
348d480f
PA
13080
13081 if (b->loc->next)
13082 {
13083 struct bp_location *loc = b->loc;
13084 int n = 0;
13085 for (; loc; loc = loc->next)
13086 ++n;
13087 printf_filtered (" (%d locations)", n);
13088 }
13089 }
13090}
13091
348d480f
PA
13092/* Default bp_location_ops methods. */
13093
13094static void
13095bp_location_dtor (struct bp_location *self)
13096{
13097 xfree (self->cond);
b775012e
LM
13098 if (self->cond_bytecode)
13099 free_agent_expr (self->cond_bytecode);
348d480f 13100 xfree (self->function_name);
8b4f3082
PA
13101
13102 VEC_free (agent_expr_p, self->target_info.conditions);
13103 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13104}
13105
13106static const struct bp_location_ops bp_location_ops =
13107{
13108 bp_location_dtor
13109};
13110
2060206e
PA
13111/* Default breakpoint_ops methods all breakpoint_ops ultimately
13112 inherit from. */
348d480f 13113
2060206e
PA
13114static void
13115base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13116{
13117 decref_counted_command_line (&self->commands);
13118 xfree (self->cond_string);
fb81d016 13119 xfree (self->extra_string);
348d480f 13120 xfree (self->addr_string);
f8eba3c6 13121 xfree (self->filter);
348d480f 13122 xfree (self->addr_string_range_end);
348d480f
PA
13123}
13124
2060206e
PA
13125static struct bp_location *
13126base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13127{
13128 struct bp_location *loc;
13129
13130 loc = XNEW (struct bp_location);
13131 init_bp_location (loc, &bp_location_ops, self);
13132 return loc;
13133}
13134
2060206e
PA
13135static void
13136base_breakpoint_re_set (struct breakpoint *b)
13137{
13138 /* Nothing to re-set. */
13139}
13140
13141#define internal_error_pure_virtual_called() \
13142 gdb_assert_not_reached ("pure virtual function called")
13143
13144static int
13145base_breakpoint_insert_location (struct bp_location *bl)
13146{
13147 internal_error_pure_virtual_called ();
13148}
13149
13150static int
13151base_breakpoint_remove_location (struct bp_location *bl)
13152{
13153 internal_error_pure_virtual_called ();
13154}
13155
13156static int
13157base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13158 struct address_space *aspace,
09ac7c10
TT
13159 CORE_ADDR bp_addr,
13160 const struct target_waitstatus *ws)
2060206e
PA
13161{
13162 internal_error_pure_virtual_called ();
13163}
13164
13165static void
13166base_breakpoint_check_status (bpstat bs)
13167{
13168 /* Always stop. */
13169}
13170
13171/* A "works_in_software_mode" breakpoint_ops method that just internal
13172 errors. */
13173
13174static int
13175base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13176{
13177 internal_error_pure_virtual_called ();
13178}
13179
13180/* A "resources_needed" breakpoint_ops method that just internal
13181 errors. */
13182
13183static int
13184base_breakpoint_resources_needed (const struct bp_location *bl)
13185{
13186 internal_error_pure_virtual_called ();
13187}
13188
13189static enum print_stop_action
13190base_breakpoint_print_it (bpstat bs)
13191{
13192 internal_error_pure_virtual_called ();
13193}
13194
13195static void
13196base_breakpoint_print_one_detail (const struct breakpoint *self,
13197 struct ui_out *uiout)
13198{
13199 /* nothing */
13200}
13201
13202static void
13203base_breakpoint_print_mention (struct breakpoint *b)
13204{
13205 internal_error_pure_virtual_called ();
13206}
13207
13208static void
13209base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13210{
13211 internal_error_pure_virtual_called ();
13212}
13213
983af33b
SDJ
13214static void
13215base_breakpoint_create_sals_from_address (char **arg,
13216 struct linespec_result *canonical,
13217 enum bptype type_wanted,
13218 char *addr_start,
13219 char **copy_arg)
13220{
13221 internal_error_pure_virtual_called ();
13222}
13223
13224static void
13225base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13226 struct linespec_result *c,
983af33b 13227 char *cond_string,
e7e0cddf 13228 char *extra_string,
983af33b
SDJ
13229 enum bptype type_wanted,
13230 enum bpdisp disposition,
13231 int thread,
13232 int task, int ignore_count,
13233 const struct breakpoint_ops *o,
13234 int from_tty, int enabled,
44f238bb 13235 int internal, unsigned flags)
983af33b
SDJ
13236{
13237 internal_error_pure_virtual_called ();
13238}
13239
13240static void
13241base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13242 struct symtabs_and_lines *sals)
13243{
13244 internal_error_pure_virtual_called ();
13245}
13246
ab04a2af
TT
13247/* The default 'explains_signal' method. */
13248
47591c29 13249static int
427cd150 13250base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13251{
47591c29 13252 return 1;
ab04a2af
TT
13253}
13254
9d6e6e84
HZ
13255/* The default "after_condition_true" method. */
13256
13257static void
13258base_breakpoint_after_condition_true (struct bpstats *bs)
13259{
13260 /* Nothing to do. */
13261}
13262
ab04a2af 13263struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13264{
13265 base_breakpoint_dtor,
13266 base_breakpoint_allocate_location,
13267 base_breakpoint_re_set,
13268 base_breakpoint_insert_location,
13269 base_breakpoint_remove_location,
13270 base_breakpoint_breakpoint_hit,
13271 base_breakpoint_check_status,
13272 base_breakpoint_resources_needed,
13273 base_breakpoint_works_in_software_mode,
13274 base_breakpoint_print_it,
13275 NULL,
13276 base_breakpoint_print_one_detail,
13277 base_breakpoint_print_mention,
983af33b
SDJ
13278 base_breakpoint_print_recreate,
13279 base_breakpoint_create_sals_from_address,
13280 base_breakpoint_create_breakpoints_sal,
13281 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13282 base_breakpoint_explains_signal,
13283 base_breakpoint_after_condition_true,
2060206e
PA
13284};
13285
13286/* Default breakpoint_ops methods. */
13287
13288static void
348d480f
PA
13289bkpt_re_set (struct breakpoint *b)
13290{
06edf0c0
PA
13291 /* FIXME: is this still reachable? */
13292 if (b->addr_string == NULL)
13293 {
13294 /* Anything without a string can't be re-set. */
348d480f 13295 delete_breakpoint (b);
06edf0c0 13296 return;
348d480f 13297 }
06edf0c0
PA
13298
13299 breakpoint_re_set_default (b);
348d480f
PA
13300}
13301
ef370185
JB
13302/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13303 inserted DEST, so we can remove it later, in case SRC is removed
13304 first. */
13305
13306static void
13307bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13308 const struct bp_target_info *src)
13309{
13310 dest->shadow_len = src->shadow_len;
13311 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
13312 dest->placed_size = src->placed_size;
13313}
13314
2060206e 13315static int
348d480f
PA
13316bkpt_insert_location (struct bp_location *bl)
13317{
13318 if (bl->loc_type == bp_loc_hardware_breakpoint)
13319 return target_insert_hw_breakpoint (bl->gdbarch,
13320 &bl->target_info);
13321 else
ef370185
JB
13322 {
13323 struct bp_target_info *bp_tgt = &bl->target_info;
13324 int ret;
13325 int sss_slot;
13326
13327 /* There is no need to insert a breakpoint if an unconditional
13328 raw/sss breakpoint is already inserted at that location. */
13329 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
13330 bp_tgt->placed_address);
13331 if (sss_slot >= 0)
13332 {
13333 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13334
13335 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13336 return 0;
13337 }
13338
13339 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13340 }
348d480f
PA
13341}
13342
2060206e 13343static int
348d480f
PA
13344bkpt_remove_location (struct bp_location *bl)
13345{
13346 if (bl->loc_type == bp_loc_hardware_breakpoint)
13347 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13348 else
ef370185
JB
13349 {
13350 struct bp_target_info *bp_tgt = &bl->target_info;
13351 struct address_space *aspace = bp_tgt->placed_address_space;
13352 CORE_ADDR address = bp_tgt->placed_address;
13353
13354 /* Only remove the breakpoint if there is no raw/sss breakpoint
13355 still inserted at this location. Otherwise, we would be
13356 effectively disabling the raw/sss breakpoint. */
13357 if (single_step_breakpoint_inserted_here_p (aspace, address))
13358 return 0;
13359
13360 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13361 }
348d480f
PA
13362}
13363
2060206e 13364static int
348d480f 13365bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13366 struct address_space *aspace, CORE_ADDR bp_addr,
13367 const struct target_waitstatus *ws)
348d480f 13368{
09ac7c10 13369 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13370 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13371 return 0;
13372
348d480f
PA
13373 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13374 aspace, bp_addr))
13375 return 0;
13376
13377 if (overlay_debugging /* unmapped overlay section */
13378 && section_is_overlay (bl->section)
13379 && !section_is_mapped (bl->section))
13380 return 0;
13381
13382 return 1;
13383}
13384
cd1608cc
PA
13385static int
13386dprintf_breakpoint_hit (const struct bp_location *bl,
13387 struct address_space *aspace, CORE_ADDR bp_addr,
13388 const struct target_waitstatus *ws)
13389{
13390 if (dprintf_style == dprintf_style_agent
13391 && target_can_run_breakpoint_commands ())
13392 {
13393 /* An agent-style dprintf never causes a stop. If we see a trap
13394 for this address it must be for a breakpoint that happens to
13395 be set at the same address. */
13396 return 0;
13397 }
13398
13399 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13400}
13401
2060206e 13402static int
348d480f
PA
13403bkpt_resources_needed (const struct bp_location *bl)
13404{
13405 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13406
13407 return 1;
13408}
13409
2060206e 13410static enum print_stop_action
348d480f
PA
13411bkpt_print_it (bpstat bs)
13412{
348d480f
PA
13413 struct breakpoint *b;
13414 const struct bp_location *bl;
001c8c33 13415 int bp_temp;
79a45e25 13416 struct ui_out *uiout = current_uiout;
348d480f
PA
13417
13418 gdb_assert (bs->bp_location_at != NULL);
13419
13420 bl = bs->bp_location_at;
13421 b = bs->breakpoint_at;
13422
001c8c33
PA
13423 bp_temp = b->disposition == disp_del;
13424 if (bl->address != bl->requested_address)
13425 breakpoint_adjustment_warning (bl->requested_address,
13426 bl->address,
13427 b->number, 1);
13428 annotate_breakpoint (b->number);
13429 if (bp_temp)
13430 ui_out_text (uiout, "\nTemporary breakpoint ");
13431 else
13432 ui_out_text (uiout, "\nBreakpoint ");
13433 if (ui_out_is_mi_like_p (uiout))
348d480f 13434 {
001c8c33
PA
13435 ui_out_field_string (uiout, "reason",
13436 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13437 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13438 }
001c8c33
PA
13439 ui_out_field_int (uiout, "bkptno", b->number);
13440 ui_out_text (uiout, ", ");
06edf0c0 13441
001c8c33 13442 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13443}
13444
2060206e 13445static void
06edf0c0
PA
13446bkpt_print_mention (struct breakpoint *b)
13447{
79a45e25 13448 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13449 return;
13450
13451 switch (b->type)
13452 {
13453 case bp_breakpoint:
13454 case bp_gnu_ifunc_resolver:
13455 if (b->disposition == disp_del)
13456 printf_filtered (_("Temporary breakpoint"));
13457 else
13458 printf_filtered (_("Breakpoint"));
13459 printf_filtered (_(" %d"), b->number);
13460 if (b->type == bp_gnu_ifunc_resolver)
13461 printf_filtered (_(" at gnu-indirect-function resolver"));
13462 break;
13463 case bp_hardware_breakpoint:
13464 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13465 break;
e7e0cddf
SS
13466 case bp_dprintf:
13467 printf_filtered (_("Dprintf %d"), b->number);
13468 break;
06edf0c0
PA
13469 }
13470
13471 say_where (b);
13472}
13473
2060206e 13474static void
06edf0c0
PA
13475bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13476{
13477 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13478 fprintf_unfiltered (fp, "tbreak");
13479 else if (tp->type == bp_breakpoint)
13480 fprintf_unfiltered (fp, "break");
13481 else if (tp->type == bp_hardware_breakpoint
13482 && tp->disposition == disp_del)
13483 fprintf_unfiltered (fp, "thbreak");
13484 else if (tp->type == bp_hardware_breakpoint)
13485 fprintf_unfiltered (fp, "hbreak");
13486 else
13487 internal_error (__FILE__, __LINE__,
13488 _("unhandled breakpoint type %d"), (int) tp->type);
13489
2060206e 13490 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13491 print_recreate_thread (tp, fp);
06edf0c0
PA
13492}
13493
983af33b
SDJ
13494static void
13495bkpt_create_sals_from_address (char **arg,
13496 struct linespec_result *canonical,
13497 enum bptype type_wanted,
13498 char *addr_start, char **copy_arg)
13499{
13500 create_sals_from_address_default (arg, canonical, type_wanted,
13501 addr_start, copy_arg);
13502}
13503
13504static void
13505bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13506 struct linespec_result *canonical,
983af33b 13507 char *cond_string,
e7e0cddf 13508 char *extra_string,
983af33b
SDJ
13509 enum bptype type_wanted,
13510 enum bpdisp disposition,
13511 int thread,
13512 int task, int ignore_count,
13513 const struct breakpoint_ops *ops,
13514 int from_tty, int enabled,
44f238bb 13515 int internal, unsigned flags)
983af33b 13516{
023fa29b 13517 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13518 cond_string, extra_string,
13519 type_wanted,
983af33b
SDJ
13520 disposition, thread, task,
13521 ignore_count, ops, from_tty,
44f238bb 13522 enabled, internal, flags);
983af33b
SDJ
13523}
13524
13525static void
13526bkpt_decode_linespec (struct breakpoint *b, char **s,
13527 struct symtabs_and_lines *sals)
13528{
13529 decode_linespec_default (b, s, sals);
13530}
13531
06edf0c0
PA
13532/* Virtual table for internal breakpoints. */
13533
13534static void
13535internal_bkpt_re_set (struct breakpoint *b)
13536{
13537 switch (b->type)
13538 {
13539 /* Delete overlay event and longjmp master breakpoints; they
13540 will be reset later by breakpoint_re_set. */
13541 case bp_overlay_event:
13542 case bp_longjmp_master:
13543 case bp_std_terminate_master:
13544 case bp_exception_master:
13545 delete_breakpoint (b);
13546 break;
13547
13548 /* This breakpoint is special, it's set up when the inferior
13549 starts and we really don't want to touch it. */
13550 case bp_shlib_event:
13551
13552 /* Like bp_shlib_event, this breakpoint type is special. Once
13553 it is set up, we do not want to touch it. */
13554 case bp_thread_event:
13555 break;
13556 }
13557}
13558
13559static void
13560internal_bkpt_check_status (bpstat bs)
13561{
a9b3a50f
PA
13562 if (bs->breakpoint_at->type == bp_shlib_event)
13563 {
13564 /* If requested, stop when the dynamic linker notifies GDB of
13565 events. This allows the user to get control and place
13566 breakpoints in initializer routines for dynamically loaded
13567 objects (among other things). */
13568 bs->stop = stop_on_solib_events;
13569 bs->print = stop_on_solib_events;
13570 }
13571 else
13572 bs->stop = 0;
06edf0c0
PA
13573}
13574
13575static enum print_stop_action
13576internal_bkpt_print_it (bpstat bs)
13577{
06edf0c0 13578 struct breakpoint *b;
06edf0c0 13579
06edf0c0
PA
13580 b = bs->breakpoint_at;
13581
06edf0c0
PA
13582 switch (b->type)
13583 {
348d480f
PA
13584 case bp_shlib_event:
13585 /* Did we stop because the user set the stop_on_solib_events
13586 variable? (If so, we report this as a generic, "Stopped due
13587 to shlib event" message.) */
edcc5120 13588 print_solib_event (0);
348d480f
PA
13589 break;
13590
13591 case bp_thread_event:
13592 /* Not sure how we will get here.
13593 GDB should not stop for these breakpoints. */
13594 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13595 break;
13596
13597 case bp_overlay_event:
13598 /* By analogy with the thread event, GDB should not stop for these. */
13599 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13600 break;
13601
13602 case bp_longjmp_master:
13603 /* These should never be enabled. */
13604 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13605 break;
13606
13607 case bp_std_terminate_master:
13608 /* These should never be enabled. */
13609 printf_filtered (_("std::terminate Master Breakpoint: "
13610 "gdb should not stop!\n"));
348d480f
PA
13611 break;
13612
13613 case bp_exception_master:
13614 /* These should never be enabled. */
13615 printf_filtered (_("Exception Master Breakpoint: "
13616 "gdb should not stop!\n"));
06edf0c0
PA
13617 break;
13618 }
13619
001c8c33 13620 return PRINT_NOTHING;
06edf0c0
PA
13621}
13622
13623static void
13624internal_bkpt_print_mention (struct breakpoint *b)
13625{
13626 /* Nothing to mention. These breakpoints are internal. */
13627}
13628
06edf0c0
PA
13629/* Virtual table for momentary breakpoints */
13630
13631static void
13632momentary_bkpt_re_set (struct breakpoint *b)
13633{
13634 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13635 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13636 Otherwise these should have been blown away via the cleanup chain
13637 or by breakpoint_init_inferior when we rerun the executable. */
13638}
13639
13640static void
13641momentary_bkpt_check_status (bpstat bs)
13642{
13643 /* Nothing. The point of these breakpoints is causing a stop. */
13644}
13645
13646static enum print_stop_action
13647momentary_bkpt_print_it (bpstat bs)
13648{
79a45e25
PA
13649 struct ui_out *uiout = current_uiout;
13650
001c8c33 13651 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13652 {
001c8c33 13653 struct breakpoint *b = bs->breakpoint_at;
348d480f 13654
001c8c33
PA
13655 switch (b->type)
13656 {
13657 case bp_finish:
13658 ui_out_field_string
13659 (uiout, "reason",
13660 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13661 break;
348d480f 13662
001c8c33
PA
13663 case bp_until:
13664 ui_out_field_string
13665 (uiout, "reason",
13666 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13667 break;
13668 }
348d480f
PA
13669 }
13670
001c8c33 13671 return PRINT_UNKNOWN;
348d480f
PA
13672}
13673
06edf0c0
PA
13674static void
13675momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13676{
06edf0c0 13677 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13678}
13679
e2e4d78b
JK
13680/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13681
13682 It gets cleared already on the removal of the first one of such placed
13683 breakpoints. This is OK as they get all removed altogether. */
13684
13685static void
13686longjmp_bkpt_dtor (struct breakpoint *self)
13687{
13688 struct thread_info *tp = find_thread_id (self->thread);
13689
13690 if (tp)
13691 tp->initiating_frame = null_frame_id;
13692
13693 momentary_breakpoint_ops.dtor (self);
13694}
13695
55aa24fb
SDJ
13696/* Specific methods for probe breakpoints. */
13697
13698static int
13699bkpt_probe_insert_location (struct bp_location *bl)
13700{
13701 int v = bkpt_insert_location (bl);
13702
13703 if (v == 0)
13704 {
13705 /* The insertion was successful, now let's set the probe's semaphore
13706 if needed. */
729662a5
TT
13707 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13708 bl->probe.objfile,
13709 bl->gdbarch);
55aa24fb
SDJ
13710 }
13711
13712 return v;
13713}
13714
13715static int
13716bkpt_probe_remove_location (struct bp_location *bl)
13717{
13718 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13719 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13720 bl->probe.objfile,
13721 bl->gdbarch);
55aa24fb
SDJ
13722
13723 return bkpt_remove_location (bl);
13724}
13725
13726static void
13727bkpt_probe_create_sals_from_address (char **arg,
13728 struct linespec_result *canonical,
13729 enum bptype type_wanted,
13730 char *addr_start, char **copy_arg)
13731{
13732 struct linespec_sals lsal;
13733
13734 lsal.sals = parse_probes (arg, canonical);
13735
13736 *copy_arg = xstrdup (canonical->addr_string);
13737 lsal.canonical = xstrdup (*copy_arg);
13738
13739 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13740}
13741
13742static void
13743bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13744 struct symtabs_and_lines *sals)
13745{
13746 *sals = parse_probes (s, NULL);
13747 if (!sals->sals)
13748 error (_("probe not found"));
13749}
13750
348d480f 13751/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13752
348d480f
PA
13753static void
13754tracepoint_re_set (struct breakpoint *b)
13755{
13756 breakpoint_re_set_default (b);
13757}
876fa593 13758
348d480f
PA
13759static int
13760tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13761 struct address_space *aspace, CORE_ADDR bp_addr,
13762 const struct target_waitstatus *ws)
348d480f
PA
13763{
13764 /* By definition, the inferior does not report stops at
13765 tracepoints. */
13766 return 0;
74960c60
VP
13767}
13768
13769static void
348d480f
PA
13770tracepoint_print_one_detail (const struct breakpoint *self,
13771 struct ui_out *uiout)
74960c60 13772{
d9b3f62e
PA
13773 struct tracepoint *tp = (struct tracepoint *) self;
13774 if (tp->static_trace_marker_id)
348d480f
PA
13775 {
13776 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13777
348d480f
PA
13778 ui_out_text (uiout, "\tmarker id is ");
13779 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13780 tp->static_trace_marker_id);
348d480f
PA
13781 ui_out_text (uiout, "\n");
13782 }
0d381245
VP
13783}
13784
a474d7c2 13785static void
348d480f 13786tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13787{
79a45e25 13788 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13789 return;
cc59ec59 13790
348d480f
PA
13791 switch (b->type)
13792 {
13793 case bp_tracepoint:
13794 printf_filtered (_("Tracepoint"));
13795 printf_filtered (_(" %d"), b->number);
13796 break;
13797 case bp_fast_tracepoint:
13798 printf_filtered (_("Fast tracepoint"));
13799 printf_filtered (_(" %d"), b->number);
13800 break;
13801 case bp_static_tracepoint:
13802 printf_filtered (_("Static tracepoint"));
13803 printf_filtered (_(" %d"), b->number);
13804 break;
13805 default:
13806 internal_error (__FILE__, __LINE__,
13807 _("unhandled tracepoint type %d"), (int) b->type);
13808 }
13809
13810 say_where (b);
a474d7c2
PA
13811}
13812
348d480f 13813static void
d9b3f62e 13814tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13815{
d9b3f62e
PA
13816 struct tracepoint *tp = (struct tracepoint *) self;
13817
13818 if (self->type == bp_fast_tracepoint)
348d480f 13819 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13820 if (self->type == bp_static_tracepoint)
348d480f 13821 fprintf_unfiltered (fp, "strace");
d9b3f62e 13822 else if (self->type == bp_tracepoint)
348d480f
PA
13823 fprintf_unfiltered (fp, "trace");
13824 else
13825 internal_error (__FILE__, __LINE__,
d9b3f62e 13826 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13827
d9b3f62e
PA
13828 fprintf_unfiltered (fp, " %s", self->addr_string);
13829 print_recreate_thread (self, fp);
13830
13831 if (tp->pass_count)
13832 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13833}
13834
983af33b
SDJ
13835static void
13836tracepoint_create_sals_from_address (char **arg,
13837 struct linespec_result *canonical,
13838 enum bptype type_wanted,
13839 char *addr_start, char **copy_arg)
13840{
13841 create_sals_from_address_default (arg, canonical, type_wanted,
13842 addr_start, copy_arg);
13843}
13844
13845static void
13846tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13847 struct linespec_result *canonical,
983af33b 13848 char *cond_string,
e7e0cddf 13849 char *extra_string,
983af33b
SDJ
13850 enum bptype type_wanted,
13851 enum bpdisp disposition,
13852 int thread,
13853 int task, int ignore_count,
13854 const struct breakpoint_ops *ops,
13855 int from_tty, int enabled,
44f238bb 13856 int internal, unsigned flags)
983af33b 13857{
023fa29b 13858 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13859 cond_string, extra_string,
13860 type_wanted,
983af33b
SDJ
13861 disposition, thread, task,
13862 ignore_count, ops, from_tty,
44f238bb 13863 enabled, internal, flags);
983af33b
SDJ
13864}
13865
13866static void
13867tracepoint_decode_linespec (struct breakpoint *b, char **s,
13868 struct symtabs_and_lines *sals)
13869{
13870 decode_linespec_default (b, s, sals);
13871}
13872
2060206e 13873struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13874
55aa24fb
SDJ
13875/* The breakpoint_ops structure to be use on tracepoints placed in a
13876 static probe. */
13877
13878static void
13879tracepoint_probe_create_sals_from_address (char **arg,
13880 struct linespec_result *canonical,
13881 enum bptype type_wanted,
13882 char *addr_start, char **copy_arg)
13883{
13884 /* We use the same method for breakpoint on probes. */
13885 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13886 addr_start, copy_arg);
13887}
13888
13889static void
13890tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13891 struct symtabs_and_lines *sals)
13892{
13893 /* We use the same method for breakpoint on probes. */
13894 bkpt_probe_decode_linespec (b, s, sals);
13895}
13896
13897static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13898
5c2b4418
HZ
13899/* Dprintf breakpoint_ops methods. */
13900
13901static void
13902dprintf_re_set (struct breakpoint *b)
13903{
13904 breakpoint_re_set_default (b);
13905
13906 /* This breakpoint could have been pending, and be resolved now, and
13907 if so, we should now have the extra string. If we don't, the
13908 dprintf was malformed when created, but we couldn't tell because
13909 we can't extract the extra string until the location is
13910 resolved. */
13911 if (b->loc != NULL && b->extra_string == NULL)
13912 error (_("Format string required"));
13913
13914 /* 1 - connect to target 1, that can run breakpoint commands.
13915 2 - create a dprintf, which resolves fine.
13916 3 - disconnect from target 1
13917 4 - connect to target 2, that can NOT run breakpoint commands.
13918
13919 After steps #3/#4, you'll want the dprintf command list to
13920 be updated, because target 1 and 2 may well return different
13921 answers for target_can_run_breakpoint_commands().
13922 Given absence of finer grained resetting, we get to do
13923 it all the time. */
13924 if (b->extra_string != NULL)
13925 update_dprintf_command_list (b);
13926}
13927
2d9442cc
HZ
13928/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13929
13930static void
13931dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13932{
13933 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13934 tp->extra_string);
13935 print_recreate_thread (tp, fp);
13936}
13937
9d6e6e84
HZ
13938/* Implement the "after_condition_true" breakpoint_ops method for
13939 dprintf.
13940
13941 dprintf's are implemented with regular commands in their command
13942 list, but we run the commands here instead of before presenting the
13943 stop to the user, as dprintf's don't actually cause a stop. This
13944 also makes it so that the commands of multiple dprintfs at the same
13945 address are all handled. */
13946
13947static void
13948dprintf_after_condition_true (struct bpstats *bs)
13949{
13950 struct cleanup *old_chain;
13951 struct bpstats tmp_bs = { NULL };
13952 struct bpstats *tmp_bs_p = &tmp_bs;
13953
13954 /* dprintf's never cause a stop. This wasn't set in the
13955 check_status hook instead because that would make the dprintf's
13956 condition not be evaluated. */
13957 bs->stop = 0;
13958
13959 /* Run the command list here. Take ownership of it instead of
13960 copying. We never want these commands to run later in
13961 bpstat_do_actions, if a breakpoint that causes a stop happens to
13962 be set at same address as this dprintf, or even if running the
13963 commands here throws. */
13964 tmp_bs.commands = bs->commands;
13965 bs->commands = NULL;
13966 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13967
13968 bpstat_do_actions_1 (&tmp_bs_p);
13969
13970 /* 'tmp_bs.commands' will usually be NULL by now, but
13971 bpstat_do_actions_1 may return early without processing the whole
13972 list. */
13973 do_cleanups (old_chain);
13974}
13975
983af33b
SDJ
13976/* The breakpoint_ops structure to be used on static tracepoints with
13977 markers (`-m'). */
13978
13979static void
13980strace_marker_create_sals_from_address (char **arg,
13981 struct linespec_result *canonical,
13982 enum bptype type_wanted,
13983 char *addr_start, char **copy_arg)
13984{
13985 struct linespec_sals lsal;
13986
13987 lsal.sals = decode_static_tracepoint_spec (arg);
13988
13989 *copy_arg = savestring (addr_start, *arg - addr_start);
13990
13991 canonical->addr_string = xstrdup (*copy_arg);
13992 lsal.canonical = xstrdup (*copy_arg);
13993 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13994}
13995
13996static void
13997strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13998 struct linespec_result *canonical,
983af33b 13999 char *cond_string,
e7e0cddf 14000 char *extra_string,
983af33b
SDJ
14001 enum bptype type_wanted,
14002 enum bpdisp disposition,
14003 int thread,
14004 int task, int ignore_count,
14005 const struct breakpoint_ops *ops,
14006 int from_tty, int enabled,
44f238bb 14007 int internal, unsigned flags)
983af33b
SDJ
14008{
14009 int i;
52d361e1
YQ
14010 struct linespec_sals *lsal = VEC_index (linespec_sals,
14011 canonical->sals, 0);
983af33b
SDJ
14012
14013 /* If the user is creating a static tracepoint by marker id
14014 (strace -m MARKER_ID), then store the sals index, so that
14015 breakpoint_re_set can try to match up which of the newly
14016 found markers corresponds to this one, and, don't try to
14017 expand multiple locations for each sal, given than SALS
14018 already should contain all sals for MARKER_ID. */
14019
14020 for (i = 0; i < lsal->sals.nelts; ++i)
14021 {
14022 struct symtabs_and_lines expanded;
14023 struct tracepoint *tp;
14024 struct cleanup *old_chain;
14025 char *addr_string;
14026
14027 expanded.nelts = 1;
14028 expanded.sals = &lsal->sals.sals[i];
14029
14030 addr_string = xstrdup (canonical->addr_string);
14031 old_chain = make_cleanup (xfree, addr_string);
14032
14033 tp = XCNEW (struct tracepoint);
14034 init_breakpoint_sal (&tp->base, gdbarch, expanded,
14035 addr_string, NULL,
e7e0cddf
SS
14036 cond_string, extra_string,
14037 type_wanted, disposition,
983af33b 14038 thread, task, ignore_count, ops,
44f238bb 14039 from_tty, enabled, internal, flags,
983af33b
SDJ
14040 canonical->special_display);
14041 /* Given that its possible to have multiple markers with
14042 the same string id, if the user is creating a static
14043 tracepoint by marker id ("strace -m MARKER_ID"), then
14044 store the sals index, so that breakpoint_re_set can
14045 try to match up which of the newly found markers
14046 corresponds to this one */
14047 tp->static_trace_marker_id_idx = i;
14048
14049 install_breakpoint (internal, &tp->base, 0);
14050
14051 discard_cleanups (old_chain);
14052 }
14053}
14054
14055static void
14056strace_marker_decode_linespec (struct breakpoint *b, char **s,
14057 struct symtabs_and_lines *sals)
14058{
14059 struct tracepoint *tp = (struct tracepoint *) b;
14060
14061 *sals = decode_static_tracepoint_spec (s);
14062 if (sals->nelts > tp->static_trace_marker_id_idx)
14063 {
14064 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14065 sals->nelts = 1;
14066 }
14067 else
14068 error (_("marker %s not found"), tp->static_trace_marker_id);
14069}
14070
14071static struct breakpoint_ops strace_marker_breakpoint_ops;
14072
14073static int
14074strace_marker_p (struct breakpoint *b)
14075{
14076 return b->ops == &strace_marker_breakpoint_ops;
14077}
14078
53a5351d 14079/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14080 structures. */
c906108c
SS
14081
14082void
fba45db2 14083delete_breakpoint (struct breakpoint *bpt)
c906108c 14084{
52f0bd74 14085 struct breakpoint *b;
c906108c 14086
8a3fe4f8 14087 gdb_assert (bpt != NULL);
c906108c 14088
4a64f543
MS
14089 /* Has this bp already been deleted? This can happen because
14090 multiple lists can hold pointers to bp's. bpstat lists are
14091 especial culprits.
14092
14093 One example of this happening is a watchpoint's scope bp. When
14094 the scope bp triggers, we notice that the watchpoint is out of
14095 scope, and delete it. We also delete its scope bp. But the
14096 scope bp is marked "auto-deleting", and is already on a bpstat.
14097 That bpstat is then checked for auto-deleting bp's, which are
14098 deleted.
14099
14100 A real solution to this problem might involve reference counts in
14101 bp's, and/or giving them pointers back to their referencing
14102 bpstat's, and teaching delete_breakpoint to only free a bp's
14103 storage when no more references were extent. A cheaper bandaid
14104 was chosen. */
c906108c
SS
14105 if (bpt->type == bp_none)
14106 return;
14107
4a64f543
MS
14108 /* At least avoid this stale reference until the reference counting
14109 of breakpoints gets resolved. */
d0fb5eae 14110 if (bpt->related_breakpoint != bpt)
e5a0a904 14111 {
d0fb5eae 14112 struct breakpoint *related;
3a5c3e22 14113 struct watchpoint *w;
d0fb5eae
JK
14114
14115 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14116 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14117 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14118 w = (struct watchpoint *) bpt;
14119 else
14120 w = NULL;
14121 if (w != NULL)
14122 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14123
14124 /* Unlink bpt from the bpt->related_breakpoint ring. */
14125 for (related = bpt; related->related_breakpoint != bpt;
14126 related = related->related_breakpoint);
14127 related->related_breakpoint = bpt->related_breakpoint;
14128 bpt->related_breakpoint = bpt;
e5a0a904
JK
14129 }
14130
a9634178
TJB
14131 /* watch_command_1 creates a watchpoint but only sets its number if
14132 update_watchpoint succeeds in creating its bp_locations. If there's
14133 a problem in that process, we'll be asked to delete the half-created
14134 watchpoint. In that case, don't announce the deletion. */
14135 if (bpt->number)
14136 observer_notify_breakpoint_deleted (bpt);
c906108c 14137
c906108c
SS
14138 if (breakpoint_chain == bpt)
14139 breakpoint_chain = bpt->next;
14140
c906108c
SS
14141 ALL_BREAKPOINTS (b)
14142 if (b->next == bpt)
c5aa993b
JM
14143 {
14144 b->next = bpt->next;
14145 break;
14146 }
c906108c 14147
f431efe5
PA
14148 /* Be sure no bpstat's are pointing at the breakpoint after it's
14149 been freed. */
14150 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14151 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14152 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14153 commands are associated with the bpstat; if we remove it here,
14154 then the later call to bpstat_do_actions (&stop_bpstat); in
14155 event-top.c won't do anything, and temporary breakpoints with
14156 commands won't work. */
14157
14158 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14159
4a64f543
MS
14160 /* Now that breakpoint is removed from breakpoint list, update the
14161 global location list. This will remove locations that used to
14162 belong to this breakpoint. Do this before freeing the breakpoint
14163 itself, since remove_breakpoint looks at location's owner. It
14164 might be better design to have location completely
14165 self-contained, but it's not the case now. */
44702360 14166 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14167
348d480f 14168 bpt->ops->dtor (bpt);
4a64f543
MS
14169 /* On the chance that someone will soon try again to delete this
14170 same bp, we mark it as deleted before freeing its storage. */
c906108c 14171 bpt->type = bp_none;
b8c9b27d 14172 xfree (bpt);
c906108c
SS
14173}
14174
4d6140d9
AC
14175static void
14176do_delete_breakpoint_cleanup (void *b)
14177{
14178 delete_breakpoint (b);
14179}
14180
14181struct cleanup *
14182make_cleanup_delete_breakpoint (struct breakpoint *b)
14183{
14184 return make_cleanup (do_delete_breakpoint_cleanup, b);
14185}
14186
51be5b68
PA
14187/* Iterator function to call a user-provided callback function once
14188 for each of B and its related breakpoints. */
14189
14190static void
14191iterate_over_related_breakpoints (struct breakpoint *b,
14192 void (*function) (struct breakpoint *,
14193 void *),
14194 void *data)
14195{
14196 struct breakpoint *related;
14197
14198 related = b;
14199 do
14200 {
14201 struct breakpoint *next;
14202
14203 /* FUNCTION may delete RELATED. */
14204 next = related->related_breakpoint;
14205
14206 if (next == related)
14207 {
14208 /* RELATED is the last ring entry. */
14209 function (related, data);
14210
14211 /* FUNCTION may have deleted it, so we'd never reach back to
14212 B. There's nothing left to do anyway, so just break
14213 out. */
14214 break;
14215 }
14216 else
14217 function (related, data);
14218
14219 related = next;
14220 }
14221 while (related != b);
14222}
95a42b64
TT
14223
14224static void
14225do_delete_breakpoint (struct breakpoint *b, void *ignore)
14226{
14227 delete_breakpoint (b);
14228}
14229
51be5b68
PA
14230/* A callback for map_breakpoint_numbers that calls
14231 delete_breakpoint. */
14232
14233static void
14234do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14235{
14236 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14237}
14238
c906108c 14239void
fba45db2 14240delete_command (char *arg, int from_tty)
c906108c 14241{
35df4500 14242 struct breakpoint *b, *b_tmp;
c906108c 14243
ea9365bb
TT
14244 dont_repeat ();
14245
c906108c
SS
14246 if (arg == 0)
14247 {
14248 int breaks_to_delete = 0;
14249
46c6471b
PA
14250 /* Delete all breakpoints if no argument. Do not delete
14251 internal breakpoints, these have to be deleted with an
14252 explicit breakpoint number argument. */
c5aa993b 14253 ALL_BREAKPOINTS (b)
46c6471b 14254 if (user_breakpoint_p (b))
973d738b
DJ
14255 {
14256 breaks_to_delete = 1;
14257 break;
14258 }
c906108c
SS
14259
14260 /* Ask user only if there are some breakpoints to delete. */
14261 if (!from_tty
e2e0b3e5 14262 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14263 {
35df4500 14264 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14265 if (user_breakpoint_p (b))
c5aa993b 14266 delete_breakpoint (b);
c906108c
SS
14267 }
14268 }
14269 else
51be5b68 14270 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14271}
14272
0d381245
VP
14273static int
14274all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14275{
0d381245 14276 for (; loc; loc = loc->next)
8645ff69
UW
14277 if (!loc->shlib_disabled
14278 && !loc->pspace->executing_startup)
0d381245
VP
14279 return 0;
14280 return 1;
fe3f5fa8
VP
14281}
14282
776592bf
DE
14283/* Subroutine of update_breakpoint_locations to simplify it.
14284 Return non-zero if multiple fns in list LOC have the same name.
14285 Null names are ignored. */
14286
14287static int
14288ambiguous_names_p (struct bp_location *loc)
14289{
14290 struct bp_location *l;
14291 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14292 (int (*) (const void *,
14293 const void *)) streq,
776592bf
DE
14294 NULL, xcalloc, xfree);
14295
14296 for (l = loc; l != NULL; l = l->next)
14297 {
14298 const char **slot;
14299 const char *name = l->function_name;
14300
14301 /* Allow for some names to be NULL, ignore them. */
14302 if (name == NULL)
14303 continue;
14304
14305 slot = (const char **) htab_find_slot (htab, (const void *) name,
14306 INSERT);
4a64f543
MS
14307 /* NOTE: We can assume slot != NULL here because xcalloc never
14308 returns NULL. */
776592bf
DE
14309 if (*slot != NULL)
14310 {
14311 htab_delete (htab);
14312 return 1;
14313 }
14314 *slot = name;
14315 }
14316
14317 htab_delete (htab);
14318 return 0;
14319}
14320
0fb4aa4b
PA
14321/* When symbols change, it probably means the sources changed as well,
14322 and it might mean the static tracepoint markers are no longer at
14323 the same address or line numbers they used to be at last we
14324 checked. Losing your static tracepoints whenever you rebuild is
14325 undesirable. This function tries to resync/rematch gdb static
14326 tracepoints with the markers on the target, for static tracepoints
14327 that have not been set by marker id. Static tracepoint that have
14328 been set by marker id are reset by marker id in breakpoint_re_set.
14329 The heuristic is:
14330
14331 1) For a tracepoint set at a specific address, look for a marker at
14332 the old PC. If one is found there, assume to be the same marker.
14333 If the name / string id of the marker found is different from the
14334 previous known name, assume that means the user renamed the marker
14335 in the sources, and output a warning.
14336
14337 2) For a tracepoint set at a given line number, look for a marker
14338 at the new address of the old line number. If one is found there,
14339 assume to be the same marker. If the name / string id of the
14340 marker found is different from the previous known name, assume that
14341 means the user renamed the marker in the sources, and output a
14342 warning.
14343
14344 3) If a marker is no longer found at the same address or line, it
14345 may mean the marker no longer exists. But it may also just mean
14346 the code changed a bit. Maybe the user added a few lines of code
14347 that made the marker move up or down (in line number terms). Ask
14348 the target for info about the marker with the string id as we knew
14349 it. If found, update line number and address in the matching
14350 static tracepoint. This will get confused if there's more than one
14351 marker with the same ID (possible in UST, although unadvised
14352 precisely because it confuses tools). */
14353
14354static struct symtab_and_line
14355update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14356{
d9b3f62e 14357 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14358 struct static_tracepoint_marker marker;
14359 CORE_ADDR pc;
0fb4aa4b
PA
14360
14361 pc = sal.pc;
14362 if (sal.line)
14363 find_line_pc (sal.symtab, sal.line, &pc);
14364
14365 if (target_static_tracepoint_marker_at (pc, &marker))
14366 {
d9b3f62e 14367 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14368 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14369 b->number,
d9b3f62e 14370 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14371
d9b3f62e
PA
14372 xfree (tp->static_trace_marker_id);
14373 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14374 release_static_tracepoint_marker (&marker);
14375
14376 return sal;
14377 }
14378
14379 /* Old marker wasn't found on target at lineno. Try looking it up
14380 by string ID. */
14381 if (!sal.explicit_pc
14382 && sal.line != 0
14383 && sal.symtab != NULL
d9b3f62e 14384 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14385 {
14386 VEC(static_tracepoint_marker_p) *markers;
14387
14388 markers
d9b3f62e 14389 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14390
14391 if (!VEC_empty(static_tracepoint_marker_p, markers))
14392 {
80e1d417 14393 struct symtab_and_line sal2;
0fb4aa4b 14394 struct symbol *sym;
80e1d417 14395 struct static_tracepoint_marker *tpmarker;
79a45e25 14396 struct ui_out *uiout = current_uiout;
0fb4aa4b 14397
80e1d417 14398 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14399
d9b3f62e 14400 xfree (tp->static_trace_marker_id);
80e1d417 14401 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14402
14403 warning (_("marker for static tracepoint %d (%s) not "
14404 "found at previous line number"),
d9b3f62e 14405 b->number, tp->static_trace_marker_id);
0fb4aa4b 14406
80e1d417 14407 init_sal (&sal2);
0fb4aa4b 14408
80e1d417 14409 sal2.pc = tpmarker->address;
0fb4aa4b 14410
80e1d417
AS
14411 sal2 = find_pc_line (tpmarker->address, 0);
14412 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14413 ui_out_text (uiout, "Now in ");
14414 if (sym)
14415 {
14416 ui_out_field_string (uiout, "func",
14417 SYMBOL_PRINT_NAME (sym));
14418 ui_out_text (uiout, " at ");
14419 }
05cba821
JK
14420 ui_out_field_string (uiout, "file",
14421 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14422 ui_out_text (uiout, ":");
14423
14424 if (ui_out_is_mi_like_p (uiout))
14425 {
0b0865da 14426 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14427
f35a17b5 14428 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14429 }
14430
80e1d417 14431 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14432 ui_out_text (uiout, "\n");
14433
80e1d417 14434 b->loc->line_number = sal2.line;
2f202fde 14435 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14436
14437 xfree (b->addr_string);
14438 b->addr_string = xstrprintf ("%s:%d",
05cba821 14439 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14440 b->loc->line_number);
0fb4aa4b
PA
14441
14442 /* Might be nice to check if function changed, and warn if
14443 so. */
14444
80e1d417 14445 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14446 }
14447 }
14448 return sal;
14449}
14450
8d3788bd
VP
14451/* Returns 1 iff locations A and B are sufficiently same that
14452 we don't need to report breakpoint as changed. */
14453
14454static int
14455locations_are_equal (struct bp_location *a, struct bp_location *b)
14456{
14457 while (a && b)
14458 {
14459 if (a->address != b->address)
14460 return 0;
14461
14462 if (a->shlib_disabled != b->shlib_disabled)
14463 return 0;
14464
14465 if (a->enabled != b->enabled)
14466 return 0;
14467
14468 a = a->next;
14469 b = b->next;
14470 }
14471
14472 if ((a == NULL) != (b == NULL))
14473 return 0;
14474
14475 return 1;
14476}
14477
f1310107
TJB
14478/* Create new breakpoint locations for B (a hardware or software breakpoint)
14479 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14480 a ranged breakpoint. */
14481
0e30163f 14482void
0d381245 14483update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14484 struct symtabs_and_lines sals,
14485 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14486{
14487 int i;
0d381245
VP
14488 struct bp_location *existing_locations = b->loc;
14489
f8eba3c6
TT
14490 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14491 {
14492 /* Ranged breakpoints have only one start location and one end
14493 location. */
14494 b->enable_state = bp_disabled;
44702360 14495 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14496 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14497 "multiple locations found\n"),
14498 b->number);
14499 return;
14500 }
f1310107 14501
4a64f543
MS
14502 /* If there's no new locations, and all existing locations are
14503 pending, don't do anything. This optimizes the common case where
14504 all locations are in the same shared library, that was unloaded.
14505 We'd like to retain the location, so that when the library is
14506 loaded again, we don't loose the enabled/disabled status of the
14507 individual locations. */
0d381245 14508 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14509 return;
14510
fe3f5fa8
VP
14511 b->loc = NULL;
14512
0d381245 14513 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14514 {
f8eba3c6
TT
14515 struct bp_location *new_loc;
14516
14517 switch_to_program_space_and_thread (sals.sals[i].pspace);
14518
14519 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14520
0d381245
VP
14521 /* Reparse conditions, they might contain references to the
14522 old symtab. */
14523 if (b->cond_string != NULL)
14524 {
bbc13ae3 14525 const char *s;
bfd189b1 14526 volatile struct gdb_exception e;
fe3f5fa8 14527
0d381245
VP
14528 s = b->cond_string;
14529 TRY_CATCH (e, RETURN_MASK_ERROR)
14530 {
1bb9788d
TT
14531 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14532 block_for_pc (sals.sals[i].pc),
0d381245
VP
14533 0);
14534 }
14535 if (e.reason < 0)
14536 {
3e43a32a
MS
14537 warning (_("failed to reevaluate condition "
14538 "for breakpoint %d: %s"),
0d381245
VP
14539 b->number, e.message);
14540 new_loc->enabled = 0;
14541 }
14542 }
fe3f5fa8 14543
f1310107
TJB
14544 if (sals_end.nelts)
14545 {
14546 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14547
14548 new_loc->length = end - sals.sals[0].pc + 1;
14549 }
0d381245 14550 }
fe3f5fa8 14551
514f746b
AR
14552 /* Update locations of permanent breakpoints. */
14553 if (b->enable_state == bp_permanent)
14554 make_breakpoint_permanent (b);
14555
4a64f543
MS
14556 /* If possible, carry over 'disable' status from existing
14557 breakpoints. */
0d381245
VP
14558 {
14559 struct bp_location *e = existing_locations;
776592bf
DE
14560 /* If there are multiple breakpoints with the same function name,
14561 e.g. for inline functions, comparing function names won't work.
14562 Instead compare pc addresses; this is just a heuristic as things
14563 may have moved, but in practice it gives the correct answer
14564 often enough until a better solution is found. */
14565 int have_ambiguous_names = ambiguous_names_p (b->loc);
14566
0d381245
VP
14567 for (; e; e = e->next)
14568 {
14569 if (!e->enabled && e->function_name)
14570 {
14571 struct bp_location *l = b->loc;
776592bf
DE
14572 if (have_ambiguous_names)
14573 {
14574 for (; l; l = l->next)
f1310107 14575 if (breakpoint_locations_match (e, l))
776592bf
DE
14576 {
14577 l->enabled = 0;
14578 break;
14579 }
14580 }
14581 else
14582 {
14583 for (; l; l = l->next)
14584 if (l->function_name
14585 && strcmp (e->function_name, l->function_name) == 0)
14586 {
14587 l->enabled = 0;
14588 break;
14589 }
14590 }
0d381245
VP
14591 }
14592 }
14593 }
fe3f5fa8 14594
8d3788bd
VP
14595 if (!locations_are_equal (existing_locations, b->loc))
14596 observer_notify_breakpoint_modified (b);
14597
44702360 14598 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14599}
14600
ef23e705
TJB
14601/* Find the SaL locations corresponding to the given ADDR_STRING.
14602 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14603
14604static struct symtabs_and_lines
14605addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14606{
14607 char *s;
02d20e4a 14608 struct symtabs_and_lines sals = {0};
f8eba3c6 14609 volatile struct gdb_exception e;
ef23e705 14610
983af33b 14611 gdb_assert (b->ops != NULL);
ef23e705 14612 s = addr_string;
ef23e705
TJB
14613
14614 TRY_CATCH (e, RETURN_MASK_ERROR)
14615 {
983af33b 14616 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14617 }
14618 if (e.reason < 0)
14619 {
14620 int not_found_and_ok = 0;
14621 /* For pending breakpoints, it's expected that parsing will
14622 fail until the right shared library is loaded. User has
14623 already told to create pending breakpoints and don't need
14624 extra messages. If breakpoint is in bp_shlib_disabled
14625 state, then user already saw the message about that
14626 breakpoint being disabled, and don't want to see more
14627 errors. */
58438ac1 14628 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14629 && (b->condition_not_parsed
14630 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14631 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14632 || b->enable_state == bp_disabled))
14633 not_found_and_ok = 1;
14634
14635 if (!not_found_and_ok)
14636 {
14637 /* We surely don't want to warn about the same breakpoint
14638 10 times. One solution, implemented here, is disable
14639 the breakpoint on error. Another solution would be to
14640 have separate 'warning emitted' flag. Since this
14641 happens only when a binary has changed, I don't know
14642 which approach is better. */
14643 b->enable_state = bp_disabled;
14644 throw_exception (e);
14645 }
14646 }
14647
58438ac1 14648 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14649 {
f8eba3c6 14650 int i;
ef23e705 14651
f8eba3c6
TT
14652 for (i = 0; i < sals.nelts; ++i)
14653 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14654 if (b->condition_not_parsed && s && s[0])
14655 {
ed1d1739
KS
14656 char *cond_string, *extra_string;
14657 int thread, task;
ef23e705
TJB
14658
14659 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14660 &cond_string, &thread, &task,
14661 &extra_string);
ef23e705
TJB
14662 if (cond_string)
14663 b->cond_string = cond_string;
14664 b->thread = thread;
14665 b->task = task;
e7e0cddf
SS
14666 if (extra_string)
14667 b->extra_string = extra_string;
ef23e705
TJB
14668 b->condition_not_parsed = 0;
14669 }
14670
983af33b 14671 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14672 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14673
58438ac1
TT
14674 *found = 1;
14675 }
14676 else
14677 *found = 0;
ef23e705
TJB
14678
14679 return sals;
14680}
14681
348d480f
PA
14682/* The default re_set method, for typical hardware or software
14683 breakpoints. Reevaluate the breakpoint and recreate its
14684 locations. */
14685
14686static void
28010a5d 14687breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14688{
14689 int found;
f1310107 14690 struct symtabs_and_lines sals, sals_end;
ef23e705 14691 struct symtabs_and_lines expanded = {0};
f1310107 14692 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14693
14694 sals = addr_string_to_sals (b, b->addr_string, &found);
14695 if (found)
14696 {
14697 make_cleanup (xfree, sals.sals);
f8eba3c6 14698 expanded = sals;
ef23e705
TJB
14699 }
14700
f1310107
TJB
14701 if (b->addr_string_range_end)
14702 {
14703 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14704 if (found)
14705 {
14706 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14707 expanded_end = sals_end;
f1310107
TJB
14708 }
14709 }
14710
14711 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14712}
14713
983af33b
SDJ
14714/* Default method for creating SALs from an address string. It basically
14715 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14716
14717static void
14718create_sals_from_address_default (char **arg,
14719 struct linespec_result *canonical,
14720 enum bptype type_wanted,
14721 char *addr_start, char **copy_arg)
14722{
14723 parse_breakpoint_sals (arg, canonical);
14724}
14725
14726/* Call create_breakpoints_sal for the given arguments. This is the default
14727 function for the `create_breakpoints_sal' method of
14728 breakpoint_ops. */
14729
14730static void
14731create_breakpoints_sal_default (struct gdbarch *gdbarch,
14732 struct linespec_result *canonical,
983af33b 14733 char *cond_string,
e7e0cddf 14734 char *extra_string,
983af33b
SDJ
14735 enum bptype type_wanted,
14736 enum bpdisp disposition,
14737 int thread,
14738 int task, int ignore_count,
14739 const struct breakpoint_ops *ops,
14740 int from_tty, int enabled,
44f238bb 14741 int internal, unsigned flags)
983af33b
SDJ
14742{
14743 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14744 extra_string,
983af33b
SDJ
14745 type_wanted, disposition,
14746 thread, task, ignore_count, ops, from_tty,
44f238bb 14747 enabled, internal, flags);
983af33b
SDJ
14748}
14749
14750/* Decode the line represented by S by calling decode_line_full. This is the
14751 default function for the `decode_linespec' method of breakpoint_ops. */
14752
14753static void
14754decode_linespec_default (struct breakpoint *b, char **s,
14755 struct symtabs_and_lines *sals)
14756{
14757 struct linespec_result canonical;
14758
14759 init_linespec_result (&canonical);
14760 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14761 (struct symtab *) NULL, 0,
14762 &canonical, multiple_symbols_all,
14763 b->filter);
14764
14765 /* We should get 0 or 1 resulting SALs. */
14766 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14767
14768 if (VEC_length (linespec_sals, canonical.sals) > 0)
14769 {
14770 struct linespec_sals *lsal;
14771
14772 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14773 *sals = lsal->sals;
14774 /* Arrange it so the destructor does not free the
14775 contents. */
14776 lsal->sals.sals = NULL;
14777 }
14778
14779 destroy_linespec_result (&canonical);
14780}
14781
28010a5d
PA
14782/* Prepare the global context for a re-set of breakpoint B. */
14783
14784static struct cleanup *
14785prepare_re_set_context (struct breakpoint *b)
14786{
14787 struct cleanup *cleanups;
14788
14789 input_radix = b->input_radix;
14790 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14791 if (b->pspace != NULL)
14792 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14793 set_language (b->language);
14794
14795 return cleanups;
ef23e705
TJB
14796}
14797
c906108c
SS
14798/* Reset a breakpoint given it's struct breakpoint * BINT.
14799 The value we return ends up being the return value from catch_errors.
14800 Unused in this case. */
14801
14802static int
4efb68b1 14803breakpoint_re_set_one (void *bint)
c906108c 14804{
4a64f543 14805 /* Get past catch_errs. */
53a5351d 14806 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14807 struct cleanup *cleanups;
c906108c 14808
348d480f
PA
14809 cleanups = prepare_re_set_context (b);
14810 b->ops->re_set (b);
14811 do_cleanups (cleanups);
c906108c
SS
14812 return 0;
14813}
14814
69de3c6a 14815/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14816void
69de3c6a 14817breakpoint_re_set (void)
c906108c 14818{
35df4500 14819 struct breakpoint *b, *b_tmp;
c906108c
SS
14820 enum language save_language;
14821 int save_input_radix;
6c95b8df 14822 struct cleanup *old_chain;
c5aa993b 14823
c906108c
SS
14824 save_language = current_language->la_language;
14825 save_input_radix = input_radix;
6c95b8df
PA
14826 old_chain = save_current_program_space ();
14827
35df4500 14828 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14829 {
4a64f543 14830 /* Format possible error msg. */
fe3f5fa8 14831 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14832 b->number);
14833 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14834 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14835 do_cleanups (cleanups);
c5aa993b 14836 }
c906108c
SS
14837 set_language (save_language);
14838 input_radix = save_input_radix;
e62c965a 14839
0756c555 14840 jit_breakpoint_re_set ();
4efc6507 14841
6c95b8df
PA
14842 do_cleanups (old_chain);
14843
af02033e
PP
14844 create_overlay_event_breakpoint ();
14845 create_longjmp_master_breakpoint ();
14846 create_std_terminate_master_breakpoint ();
186c406b 14847 create_exception_master_breakpoint ();
c906108c
SS
14848}
14849\f
c906108c
SS
14850/* Reset the thread number of this breakpoint:
14851
14852 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14853 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14854void
fba45db2 14855breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14856{
14857 if (b->thread != -1)
14858 {
39f77062
KB
14859 if (in_thread_list (inferior_ptid))
14860 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14861
14862 /* We're being called after following a fork. The new fork is
14863 selected as current, and unless this was a vfork will have a
14864 different program space from the original thread. Reset that
14865 as well. */
14866 b->loc->pspace = current_program_space;
c906108c
SS
14867 }
14868}
14869
03ac34d5
MS
14870/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14871 If from_tty is nonzero, it prints a message to that effect,
14872 which ends with a period (no newline). */
14873
c906108c 14874void
fba45db2 14875set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14876{
52f0bd74 14877 struct breakpoint *b;
c906108c
SS
14878
14879 if (count < 0)
14880 count = 0;
14881
14882 ALL_BREAKPOINTS (b)
14883 if (b->number == bptnum)
c5aa993b 14884 {
d77f58be
SS
14885 if (is_tracepoint (b))
14886 {
14887 if (from_tty && count != 0)
14888 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14889 bptnum);
14890 return;
14891 }
14892
c5aa993b 14893 b->ignore_count = count;
221ea385
KS
14894 if (from_tty)
14895 {
14896 if (count == 0)
3e43a32a
MS
14897 printf_filtered (_("Will stop next time "
14898 "breakpoint %d is reached."),
221ea385
KS
14899 bptnum);
14900 else if (count == 1)
a3f17187 14901 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14902 bptnum);
14903 else
3e43a32a
MS
14904 printf_filtered (_("Will ignore next %d "
14905 "crossings of breakpoint %d."),
221ea385
KS
14906 count, bptnum);
14907 }
8d3788bd 14908 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14909 return;
14910 }
c906108c 14911
8a3fe4f8 14912 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14913}
14914
c906108c
SS
14915/* Command to set ignore-count of breakpoint N to COUNT. */
14916
14917static void
fba45db2 14918ignore_command (char *args, int from_tty)
c906108c
SS
14919{
14920 char *p = args;
52f0bd74 14921 int num;
c906108c
SS
14922
14923 if (p == 0)
e2e0b3e5 14924 error_no_arg (_("a breakpoint number"));
c5aa993b 14925
c906108c 14926 num = get_number (&p);
5c44784c 14927 if (num == 0)
8a3fe4f8 14928 error (_("bad breakpoint number: '%s'"), args);
c906108c 14929 if (*p == 0)
8a3fe4f8 14930 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14931
14932 set_ignore_count (num,
14933 longest_to_int (value_as_long (parse_and_eval (p))),
14934 from_tty);
221ea385
KS
14935 if (from_tty)
14936 printf_filtered ("\n");
c906108c
SS
14937}
14938\f
14939/* Call FUNCTION on each of the breakpoints
14940 whose numbers are given in ARGS. */
14941
14942static void
95a42b64
TT
14943map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14944 void *),
14945 void *data)
c906108c 14946{
52f0bd74
AC
14947 int num;
14948 struct breakpoint *b, *tmp;
11cf8741 14949 int match;
197f0a60 14950 struct get_number_or_range_state state;
c906108c 14951
197f0a60 14952 if (args == 0)
e2e0b3e5 14953 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14954
197f0a60
TT
14955 init_number_or_range (&state, args);
14956
14957 while (!state.finished)
c906108c 14958 {
e799154c 14959 const char *p = state.string;
197f0a60 14960
11cf8741 14961 match = 0;
c5aa993b 14962
197f0a60 14963 num = get_number_or_range (&state);
5c44784c 14964 if (num == 0)
c5aa993b 14965 {
8a3fe4f8 14966 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14967 }
14968 else
14969 {
14970 ALL_BREAKPOINTS_SAFE (b, tmp)
14971 if (b->number == num)
14972 {
11cf8741 14973 match = 1;
cdac0397 14974 function (b, data);
11cf8741 14975 break;
5c44784c 14976 }
11cf8741 14977 if (match == 0)
a3f17187 14978 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14979 }
c906108c
SS
14980 }
14981}
14982
0d381245
VP
14983static struct bp_location *
14984find_location_by_number (char *number)
14985{
14986 char *dot = strchr (number, '.');
14987 char *p1;
14988 int bp_num;
14989 int loc_num;
14990 struct breakpoint *b;
14991 struct bp_location *loc;
14992
14993 *dot = '\0';
14994
14995 p1 = number;
197f0a60 14996 bp_num = get_number (&p1);
0d381245
VP
14997 if (bp_num == 0)
14998 error (_("Bad breakpoint number '%s'"), number);
14999
15000 ALL_BREAKPOINTS (b)
15001 if (b->number == bp_num)
15002 {
15003 break;
15004 }
15005
15006 if (!b || b->number != bp_num)
15007 error (_("Bad breakpoint number '%s'"), number);
15008
15009 p1 = dot+1;
197f0a60 15010 loc_num = get_number (&p1);
0d381245
VP
15011 if (loc_num == 0)
15012 error (_("Bad breakpoint location number '%s'"), number);
15013
15014 --loc_num;
15015 loc = b->loc;
15016 for (;loc_num && loc; --loc_num, loc = loc->next)
15017 ;
15018 if (!loc)
15019 error (_("Bad breakpoint location number '%s'"), dot+1);
15020
15021 return loc;
15022}
15023
15024
1900040c
MS
15025/* Set ignore-count of breakpoint number BPTNUM to COUNT.
15026 If from_tty is nonzero, it prints a message to that effect,
15027 which ends with a period (no newline). */
15028
c906108c 15029void
fba45db2 15030disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
15031{
15032 /* Never disable a watchpoint scope breakpoint; we want to
15033 hit them when we leave scope so we can delete both the
15034 watchpoint and its scope breakpoint at that time. */
15035 if (bpt->type == bp_watchpoint_scope)
15036 return;
15037
c2c6d25f 15038 /* You can't disable permanent breakpoints. */
b5de0fa7 15039 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
15040 return;
15041
b5de0fa7 15042 bpt->enable_state = bp_disabled;
c906108c 15043
b775012e
LM
15044 /* Mark breakpoint locations modified. */
15045 mark_breakpoint_modified (bpt);
15046
d248b706
KY
15047 if (target_supports_enable_disable_tracepoint ()
15048 && current_trace_status ()->running && is_tracepoint (bpt))
15049 {
15050 struct bp_location *location;
15051
15052 for (location = bpt->loc; location; location = location->next)
15053 target_disable_tracepoint (location);
15054 }
15055
44702360 15056 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15057
8d3788bd 15058 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15059}
15060
51be5b68
PA
15061/* A callback for iterate_over_related_breakpoints. */
15062
15063static void
15064do_disable_breakpoint (struct breakpoint *b, void *ignore)
15065{
15066 disable_breakpoint (b);
15067}
15068
95a42b64
TT
15069/* A callback for map_breakpoint_numbers that calls
15070 disable_breakpoint. */
15071
15072static void
15073do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15074{
51be5b68 15075 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15076}
15077
c906108c 15078static void
fba45db2 15079disable_command (char *args, int from_tty)
c906108c 15080{
c906108c 15081 if (args == 0)
46c6471b
PA
15082 {
15083 struct breakpoint *bpt;
15084
15085 ALL_BREAKPOINTS (bpt)
15086 if (user_breakpoint_p (bpt))
15087 disable_breakpoint (bpt);
15088 }
9eaabc75 15089 else
0d381245 15090 {
9eaabc75
MW
15091 char *num = extract_arg (&args);
15092
15093 while (num)
d248b706 15094 {
9eaabc75 15095 if (strchr (num, '.'))
b775012e 15096 {
9eaabc75
MW
15097 struct bp_location *loc = find_location_by_number (num);
15098
15099 if (loc)
15100 {
15101 if (loc->enabled)
15102 {
15103 loc->enabled = 0;
15104 mark_breakpoint_location_modified (loc);
15105 }
15106 if (target_supports_enable_disable_tracepoint ()
15107 && current_trace_status ()->running && loc->owner
15108 && is_tracepoint (loc->owner))
15109 target_disable_tracepoint (loc);
15110 }
44702360 15111 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15112 }
9eaabc75
MW
15113 else
15114 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15115 num = extract_arg (&args);
d248b706 15116 }
0d381245 15117 }
c906108c
SS
15118}
15119
15120static void
816338b5
SS
15121enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15122 int count)
c906108c 15123{
afe38095 15124 int target_resources_ok;
c906108c
SS
15125
15126 if (bpt->type == bp_hardware_breakpoint)
15127 {
15128 int i;
c5aa993b 15129 i = hw_breakpoint_used_count ();
53a5351d 15130 target_resources_ok =
d92524f1 15131 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15132 i + 1, 0);
c906108c 15133 if (target_resources_ok == 0)
8a3fe4f8 15134 error (_("No hardware breakpoint support in the target."));
c906108c 15135 else if (target_resources_ok < 0)
8a3fe4f8 15136 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15137 }
15138
cc60f2e3 15139 if (is_watchpoint (bpt))
c906108c 15140 {
d07205c2
JK
15141 /* Initialize it just to avoid a GCC false warning. */
15142 enum enable_state orig_enable_state = 0;
bfd189b1 15143 volatile struct gdb_exception e;
dde02812
ES
15144
15145 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15146 {
3a5c3e22
PA
15147 struct watchpoint *w = (struct watchpoint *) bpt;
15148
1e718ff1
TJB
15149 orig_enable_state = bpt->enable_state;
15150 bpt->enable_state = bp_enabled;
3a5c3e22 15151 update_watchpoint (w, 1 /* reparse */);
c906108c 15152 }
dde02812 15153 if (e.reason < 0)
c5aa993b 15154 {
1e718ff1 15155 bpt->enable_state = orig_enable_state;
dde02812
ES
15156 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15157 bpt->number);
15158 return;
c5aa993b 15159 }
c906108c 15160 }
0101ce28 15161
b4c291bb
KH
15162 if (bpt->enable_state != bp_permanent)
15163 bpt->enable_state = bp_enabled;
d248b706 15164
b775012e
LM
15165 bpt->enable_state = bp_enabled;
15166
15167 /* Mark breakpoint locations modified. */
15168 mark_breakpoint_modified (bpt);
15169
d248b706
KY
15170 if (target_supports_enable_disable_tracepoint ()
15171 && current_trace_status ()->running && is_tracepoint (bpt))
15172 {
15173 struct bp_location *location;
15174
15175 for (location = bpt->loc; location; location = location->next)
15176 target_enable_tracepoint (location);
15177 }
15178
b4c291bb 15179 bpt->disposition = disposition;
816338b5 15180 bpt->enable_count = count;
44702360 15181 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15182
8d3788bd 15183 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15184}
15185
fe3f5fa8 15186
c906108c 15187void
fba45db2 15188enable_breakpoint (struct breakpoint *bpt)
c906108c 15189{
816338b5 15190 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15191}
15192
15193static void
15194do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15195{
15196 enable_breakpoint (bpt);
c906108c
SS
15197}
15198
95a42b64
TT
15199/* A callback for map_breakpoint_numbers that calls
15200 enable_breakpoint. */
15201
15202static void
15203do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15204{
51be5b68 15205 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15206}
15207
c906108c
SS
15208/* The enable command enables the specified breakpoints (or all defined
15209 breakpoints) so they once again become (or continue to be) effective
1272ad14 15210 in stopping the inferior. */
c906108c 15211
c906108c 15212static void
fba45db2 15213enable_command (char *args, int from_tty)
c906108c 15214{
c906108c 15215 if (args == 0)
46c6471b
PA
15216 {
15217 struct breakpoint *bpt;
15218
15219 ALL_BREAKPOINTS (bpt)
15220 if (user_breakpoint_p (bpt))
15221 enable_breakpoint (bpt);
15222 }
9eaabc75 15223 else
0d381245 15224 {
9eaabc75
MW
15225 char *num = extract_arg (&args);
15226
15227 while (num)
d248b706 15228 {
9eaabc75 15229 if (strchr (num, '.'))
b775012e 15230 {
9eaabc75
MW
15231 struct bp_location *loc = find_location_by_number (num);
15232
15233 if (loc)
15234 {
15235 if (!loc->enabled)
15236 {
15237 loc->enabled = 1;
15238 mark_breakpoint_location_modified (loc);
15239 }
15240 if (target_supports_enable_disable_tracepoint ()
15241 && current_trace_status ()->running && loc->owner
15242 && is_tracepoint (loc->owner))
15243 target_enable_tracepoint (loc);
15244 }
44702360 15245 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15246 }
9eaabc75
MW
15247 else
15248 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15249 num = extract_arg (&args);
d248b706 15250 }
0d381245 15251 }
c906108c
SS
15252}
15253
816338b5
SS
15254/* This struct packages up disposition data for application to multiple
15255 breakpoints. */
15256
15257struct disp_data
15258{
15259 enum bpdisp disp;
15260 int count;
15261};
15262
c906108c 15263static void
51be5b68
PA
15264do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15265{
816338b5 15266 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15267
816338b5 15268 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15269}
15270
15271static void
15272do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15273{
816338b5 15274 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15275
15276 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15277}
15278
c906108c 15279static void
fba45db2 15280enable_once_command (char *args, int from_tty)
c906108c 15281{
51be5b68 15282 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15283}
15284
816338b5
SS
15285static void
15286do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15287{
15288 struct disp_data disp = { disp_disable, *(int *) countptr };
15289
15290 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15291}
15292
15293static void
15294enable_count_command (char *args, int from_tty)
15295{
15296 int count = get_number (&args);
15297
15298 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15299}
15300
c906108c 15301static void
51be5b68 15302do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15303{
816338b5 15304 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15305
15306 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15307}
15308
c906108c 15309static void
fba45db2 15310enable_delete_command (char *args, int from_tty)
c906108c 15311{
51be5b68 15312 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15313}
15314\f
fa8d40ab
JJ
15315static void
15316set_breakpoint_cmd (char *args, int from_tty)
15317{
15318}
15319
15320static void
15321show_breakpoint_cmd (char *args, int from_tty)
15322{
15323}
15324
1f3b5d1b
PP
15325/* Invalidate last known value of any hardware watchpoint if
15326 the memory which that value represents has been written to by
15327 GDB itself. */
15328
15329static void
8de0566d
YQ
15330invalidate_bp_value_on_memory_change (struct inferior *inferior,
15331 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15332 const bfd_byte *data)
15333{
15334 struct breakpoint *bp;
15335
15336 ALL_BREAKPOINTS (bp)
15337 if (bp->enable_state == bp_enabled
3a5c3e22 15338 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15339 {
3a5c3e22 15340 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15341
3a5c3e22
PA
15342 if (wp->val_valid && wp->val)
15343 {
15344 struct bp_location *loc;
15345
15346 for (loc = bp->loc; loc != NULL; loc = loc->next)
15347 if (loc->loc_type == bp_loc_hardware_watchpoint
15348 && loc->address + loc->length > addr
15349 && addr + len > loc->address)
15350 {
15351 value_free (wp->val);
15352 wp->val = NULL;
15353 wp->val_valid = 0;
15354 }
15355 }
1f3b5d1b
PP
15356 }
15357}
15358
8181d85f
DJ
15359/* Create and insert a raw software breakpoint at PC. Return an
15360 identifier, which should be used to remove the breakpoint later.
15361 In general, places which call this should be using something on the
15362 breakpoint chain instead; this function should be eliminated
15363 someday. */
15364
15365void *
6c95b8df
PA
15366deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15367 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15368{
15369 struct bp_target_info *bp_tgt;
ef370185 15370 struct bp_location *bl;
8181d85f 15371
41bf6aca 15372 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15373
6c95b8df 15374 bp_tgt->placed_address_space = aspace;
8181d85f 15375 bp_tgt->placed_address = pc;
6c95b8df 15376
ef370185
JB
15377 /* If an unconditional non-raw breakpoint is already inserted at
15378 that location, there's no need to insert another. However, with
15379 target-side evaluation of breakpoint conditions, if the
15380 breakpoint that is currently inserted on the target is
15381 conditional, we need to make it unconditional. Note that a
15382 breakpoint with target-side commands is not reported even if
15383 unconditional, so we need to remove the commands from the target
15384 as well. */
15385 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15386 if (bl != NULL
15387 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15388 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15389 {
15390 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15391 return bp_tgt;
15392 }
15393
a6d9a66e 15394 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15395 {
15396 /* Could not insert the breakpoint. */
15397 xfree (bp_tgt);
15398 return NULL;
15399 }
15400
15401 return bp_tgt;
15402}
15403
4a64f543
MS
15404/* Remove a breakpoint BP inserted by
15405 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15406
15407int
a6d9a66e 15408deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15409{
15410 struct bp_target_info *bp_tgt = bp;
ef370185
JB
15411 struct address_space *aspace = bp_tgt->placed_address_space;
15412 CORE_ADDR address = bp_tgt->placed_address;
15413 struct bp_location *bl;
8181d85f
DJ
15414 int ret;
15415
ef370185
JB
15416 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15417
15418 /* Only remove the raw breakpoint if there are no other non-raw
15419 breakpoints still inserted at this location. Otherwise, we would
15420 be effectively disabling those breakpoints. */
15421 if (bl == NULL)
15422 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15423 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15424 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15425 {
15426 /* The target is evaluating conditions, and when we inserted the
15427 software single-step breakpoint, we had made the breakpoint
15428 unconditional and command-less on the target side. Reinsert
15429 to restore the conditions/commands. */
15430 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15431 }
15432 else
15433 ret = 0;
15434
8181d85f
DJ
15435 xfree (bp_tgt);
15436
15437 return ret;
15438}
15439
8181d85f
DJ
15440/* Create and insert a breakpoint for software single step. */
15441
15442void
6c95b8df 15443insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15444 struct address_space *aspace,
15445 CORE_ADDR next_pc)
8181d85f
DJ
15446{
15447 void **bpt_p;
15448
15449 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15450 {
15451 bpt_p = &single_step_breakpoints[0];
15452 single_step_gdbarch[0] = gdbarch;
15453 }
8181d85f
DJ
15454 else
15455 {
15456 gdb_assert (single_step_breakpoints[1] == NULL);
15457 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15458 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15459 }
15460
4a64f543
MS
15461 /* NOTE drow/2006-04-11: A future improvement to this function would
15462 be to only create the breakpoints once, and actually put them on
15463 the breakpoint chain. That would let us use set_raw_breakpoint.
15464 We could adjust the addresses each time they were needed. Doing
15465 this requires corresponding changes elsewhere where single step
15466 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15467
6c95b8df 15468 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15469 if (*bpt_p == NULL)
5af949e3
UW
15470 error (_("Could not insert single-step breakpoint at %s"),
15471 paddress (gdbarch, next_pc));
8181d85f
DJ
15472}
15473
f02253f1
HZ
15474/* Check if the breakpoints used for software single stepping
15475 were inserted or not. */
15476
15477int
15478single_step_breakpoints_inserted (void)
15479{
15480 return (single_step_breakpoints[0] != NULL
15481 || single_step_breakpoints[1] != NULL);
15482}
15483
8181d85f
DJ
15484/* Remove and delete any breakpoints used for software single step. */
15485
15486void
15487remove_single_step_breakpoints (void)
15488{
15489 gdb_assert (single_step_breakpoints[0] != NULL);
15490
15491 /* See insert_single_step_breakpoint for more about this deprecated
15492 call. */
a6d9a66e
UW
15493 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15494 single_step_breakpoints[0]);
15495 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15496 single_step_breakpoints[0] = NULL;
15497
15498 if (single_step_breakpoints[1] != NULL)
15499 {
a6d9a66e
UW
15500 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15501 single_step_breakpoints[1]);
15502 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15503 single_step_breakpoints[1] = NULL;
15504 }
15505}
15506
d03285ec
UW
15507/* Delete software single step breakpoints without removing them from
15508 the inferior. This is intended to be used if the inferior's address
15509 space where they were inserted is already gone, e.g. after exit or
15510 exec. */
15511
15512void
15513cancel_single_step_breakpoints (void)
15514{
15515 int i;
15516
15517 for (i = 0; i < 2; i++)
15518 if (single_step_breakpoints[i])
15519 {
15520 xfree (single_step_breakpoints[i]);
15521 single_step_breakpoints[i] = NULL;
15522 single_step_gdbarch[i] = NULL;
15523 }
15524}
15525
15526/* Detach software single-step breakpoints from INFERIOR_PTID without
15527 removing them. */
15528
15529static void
15530detach_single_step_breakpoints (void)
15531{
15532 int i;
15533
15534 for (i = 0; i < 2; i++)
15535 if (single_step_breakpoints[i])
15536 target_remove_breakpoint (single_step_gdbarch[i],
15537 single_step_breakpoints[i]);
15538}
15539
ef370185
JB
15540/* Find the software single-step breakpoint that inserted at PC.
15541 Returns its slot if found, and -1 if not found. */
1aafd4da 15542
ef370185
JB
15543static int
15544find_single_step_breakpoint (struct address_space *aspace,
15545 CORE_ADDR pc)
1aafd4da
UW
15546{
15547 int i;
15548
15549 for (i = 0; i < 2; i++)
15550 {
15551 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15552 if (bp_tgt
15553 && breakpoint_address_match (bp_tgt->placed_address_space,
15554 bp_tgt->placed_address,
15555 aspace, pc))
ef370185 15556 return i;
1aafd4da
UW
15557 }
15558
ef370185
JB
15559 return -1;
15560}
15561
15562/* Check whether a software single-step breakpoint is inserted at
15563 PC. */
15564
15565int
15566single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15567 CORE_ADDR pc)
15568{
15569 return find_single_step_breakpoint (aspace, pc) >= 0;
1aafd4da
UW
15570}
15571
a96d9b2e
SDJ
15572/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15573 non-zero otherwise. */
15574static int
15575is_syscall_catchpoint_enabled (struct breakpoint *bp)
15576{
15577 if (syscall_catchpoint_p (bp)
15578 && bp->enable_state != bp_disabled
15579 && bp->enable_state != bp_call_disabled)
15580 return 1;
15581 else
15582 return 0;
15583}
15584
15585int
15586catch_syscall_enabled (void)
15587{
fa3064dd
YQ
15588 struct catch_syscall_inferior_data *inf_data
15589 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15590
fa3064dd 15591 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15592}
15593
15594int
15595catching_syscall_number (int syscall_number)
15596{
15597 struct breakpoint *bp;
15598
15599 ALL_BREAKPOINTS (bp)
15600 if (is_syscall_catchpoint_enabled (bp))
15601 {
be5c67c1
PA
15602 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15603
15604 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15605 {
15606 int i, iter;
15607 for (i = 0;
be5c67c1 15608 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15609 i++)
15610 if (syscall_number == iter)
15611 return 1;
15612 }
15613 else
15614 return 1;
15615 }
15616
15617 return 0;
15618}
15619
15620/* Complete syscall names. Used by "catch syscall". */
49c4e619 15621static VEC (char_ptr) *
a96d9b2e 15622catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15623 const char *text, const char *word)
a96d9b2e
SDJ
15624{
15625 const char **list = get_syscall_names ();
49c4e619 15626 VEC (char_ptr) *retlist
b45627a0 15627 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15628
c38eea1a
MS
15629 xfree (list);
15630 return retlist;
a96d9b2e
SDJ
15631}
15632
1042e4c0
SS
15633/* Tracepoint-specific operations. */
15634
15635/* Set tracepoint count to NUM. */
15636static void
15637set_tracepoint_count (int num)
15638{
15639 tracepoint_count = num;
4fa62494 15640 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15641}
15642
70221824 15643static void
1042e4c0
SS
15644trace_command (char *arg, int from_tty)
15645{
55aa24fb
SDJ
15646 struct breakpoint_ops *ops;
15647 const char *arg_cp = arg;
15648
15649 if (arg && probe_linespec_to_ops (&arg_cp))
15650 ops = &tracepoint_probe_breakpoint_ops;
15651 else
15652 ops = &tracepoint_breakpoint_ops;
15653
558a9d82
YQ
15654 create_breakpoint (get_current_arch (),
15655 arg,
15656 NULL, 0, NULL, 1 /* parse arg */,
15657 0 /* tempflag */,
15658 bp_tracepoint /* type_wanted */,
15659 0 /* Ignore count */,
15660 pending_break_support,
15661 ops,
15662 from_tty,
15663 1 /* enabled */,
15664 0 /* internal */, 0);
1042e4c0
SS
15665}
15666
70221824 15667static void
7a697b8d
SS
15668ftrace_command (char *arg, int from_tty)
15669{
558a9d82
YQ
15670 create_breakpoint (get_current_arch (),
15671 arg,
15672 NULL, 0, NULL, 1 /* parse arg */,
15673 0 /* tempflag */,
15674 bp_fast_tracepoint /* type_wanted */,
15675 0 /* Ignore count */,
15676 pending_break_support,
15677 &tracepoint_breakpoint_ops,
15678 from_tty,
15679 1 /* enabled */,
15680 0 /* internal */, 0);
0fb4aa4b
PA
15681}
15682
15683/* strace command implementation. Creates a static tracepoint. */
15684
70221824 15685static void
0fb4aa4b
PA
15686strace_command (char *arg, int from_tty)
15687{
983af33b
SDJ
15688 struct breakpoint_ops *ops;
15689
15690 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15691 or with a normal static tracepoint. */
15692 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15693 ops = &strace_marker_breakpoint_ops;
15694 else
15695 ops = &tracepoint_breakpoint_ops;
15696
558a9d82
YQ
15697 create_breakpoint (get_current_arch (),
15698 arg,
15699 NULL, 0, NULL, 1 /* parse arg */,
15700 0 /* tempflag */,
15701 bp_static_tracepoint /* type_wanted */,
15702 0 /* Ignore count */,
15703 pending_break_support,
15704 ops,
15705 from_tty,
15706 1 /* enabled */,
15707 0 /* internal */, 0);
7a697b8d
SS
15708}
15709
409873ef
SS
15710/* Set up a fake reader function that gets command lines from a linked
15711 list that was acquired during tracepoint uploading. */
15712
15713static struct uploaded_tp *this_utp;
3149d8c1 15714static int next_cmd;
409873ef
SS
15715
15716static char *
15717read_uploaded_action (void)
15718{
15719 char *rslt;
15720
3149d8c1 15721 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15722
3149d8c1 15723 next_cmd++;
409873ef
SS
15724
15725 return rslt;
15726}
15727
00bf0b85
SS
15728/* Given information about a tracepoint as recorded on a target (which
15729 can be either a live system or a trace file), attempt to create an
15730 equivalent GDB tracepoint. This is not a reliable process, since
15731 the target does not necessarily have all the information used when
15732 the tracepoint was originally defined. */
15733
d9b3f62e 15734struct tracepoint *
00bf0b85 15735create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15736{
409873ef 15737 char *addr_str, small_buf[100];
d9b3f62e 15738 struct tracepoint *tp;
fd9b8c24 15739
409873ef
SS
15740 if (utp->at_string)
15741 addr_str = utp->at_string;
15742 else
15743 {
15744 /* In the absence of a source location, fall back to raw
15745 address. Since there is no way to confirm that the address
15746 means the same thing as when the trace was started, warn the
15747 user. */
3e43a32a
MS
15748 warning (_("Uploaded tracepoint %d has no "
15749 "source location, using raw address"),
409873ef 15750 utp->number);
8c042590 15751 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15752 addr_str = small_buf;
15753 }
15754
15755 /* There's not much we can do with a sequence of bytecodes. */
15756 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15757 warning (_("Uploaded tracepoint %d condition "
15758 "has no source form, ignoring it"),
409873ef 15759 utp->number);
d5551862 15760
8cdf0e15 15761 if (!create_breakpoint (get_current_arch (),
409873ef 15762 addr_str,
e7e0cddf
SS
15763 utp->cond_string, -1, NULL,
15764 0 /* parse cond/thread */,
8cdf0e15 15765 0 /* tempflag */,
0fb4aa4b 15766 utp->type /* type_wanted */,
8cdf0e15
VP
15767 0 /* Ignore count */,
15768 pending_break_support,
348d480f 15769 &tracepoint_breakpoint_ops,
8cdf0e15 15770 0 /* from_tty */,
84f4c1fe 15771 utp->enabled /* enabled */,
44f238bb
PA
15772 0 /* internal */,
15773 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15774 return NULL;
15775
409873ef 15776 /* Get the tracepoint we just created. */
fd9b8c24
PA
15777 tp = get_tracepoint (tracepoint_count);
15778 gdb_assert (tp != NULL);
d5551862 15779
00bf0b85
SS
15780 if (utp->pass > 0)
15781 {
8c042590
PM
15782 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15783 tp->base.number);
00bf0b85 15784
409873ef 15785 trace_pass_command (small_buf, 0);
00bf0b85
SS
15786 }
15787
409873ef
SS
15788 /* If we have uploaded versions of the original commands, set up a
15789 special-purpose "reader" function and call the usual command line
15790 reader, then pass the result to the breakpoint command-setting
15791 function. */
3149d8c1 15792 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15793 {
409873ef 15794 struct command_line *cmd_list;
00bf0b85 15795
409873ef 15796 this_utp = utp;
3149d8c1 15797 next_cmd = 0;
d5551862 15798
409873ef
SS
15799 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15800
d9b3f62e 15801 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15802 }
3149d8c1
SS
15803 else if (!VEC_empty (char_ptr, utp->actions)
15804 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15805 warning (_("Uploaded tracepoint %d actions "
15806 "have no source form, ignoring them"),
409873ef 15807 utp->number);
00bf0b85 15808
f196051f
SS
15809 /* Copy any status information that might be available. */
15810 tp->base.hit_count = utp->hit_count;
15811 tp->traceframe_usage = utp->traceframe_usage;
15812
00bf0b85 15813 return tp;
d9b3f62e 15814}
00bf0b85 15815
1042e4c0
SS
15816/* Print information on tracepoint number TPNUM_EXP, or all if
15817 omitted. */
15818
15819static void
e5a67952 15820tracepoints_info (char *args, int from_tty)
1042e4c0 15821{
79a45e25 15822 struct ui_out *uiout = current_uiout;
e5a67952 15823 int num_printed;
1042e4c0 15824
e5a67952 15825 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15826
15827 if (num_printed == 0)
1042e4c0 15828 {
e5a67952 15829 if (args == NULL || *args == '\0')
d77f58be
SS
15830 ui_out_message (uiout, 0, "No tracepoints.\n");
15831 else
e5a67952 15832 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15833 }
ad443146
SS
15834
15835 default_collect_info ();
1042e4c0
SS
15836}
15837
4a64f543 15838/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15839 Not supported by all targets. */
15840static void
15841enable_trace_command (char *args, int from_tty)
15842{
15843 enable_command (args, from_tty);
15844}
15845
4a64f543 15846/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15847 Not supported by all targets. */
15848static void
15849disable_trace_command (char *args, int from_tty)
15850{
15851 disable_command (args, from_tty);
15852}
15853
4a64f543 15854/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15855static void
15856delete_trace_command (char *arg, int from_tty)
15857{
35df4500 15858 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15859
15860 dont_repeat ();
15861
15862 if (arg == 0)
15863 {
15864 int breaks_to_delete = 0;
15865
15866 /* Delete all breakpoints if no argument.
15867 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15868 have to be deleted with an explicit breakpoint number
15869 argument. */
1042e4c0 15870 ALL_TRACEPOINTS (b)
46c6471b 15871 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15872 {
15873 breaks_to_delete = 1;
15874 break;
15875 }
1042e4c0
SS
15876
15877 /* Ask user only if there are some breakpoints to delete. */
15878 if (!from_tty
15879 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15880 {
35df4500 15881 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15882 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15883 delete_breakpoint (b);
1042e4c0
SS
15884 }
15885 }
15886 else
51be5b68 15887 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15888}
15889
197f0a60
TT
15890/* Helper function for trace_pass_command. */
15891
15892static void
d9b3f62e 15893trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15894{
d9b3f62e 15895 tp->pass_count = count;
6f6484cd 15896 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15897 if (from_tty)
15898 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15899 tp->base.number, count);
197f0a60
TT
15900}
15901
1042e4c0
SS
15902/* Set passcount for tracepoint.
15903
15904 First command argument is passcount, second is tracepoint number.
15905 If tracepoint number omitted, apply to most recently defined.
15906 Also accepts special argument "all". */
15907
15908static void
15909trace_pass_command (char *args, int from_tty)
15910{
d9b3f62e 15911 struct tracepoint *t1;
1042e4c0 15912 unsigned int count;
1042e4c0
SS
15913
15914 if (args == 0 || *args == 0)
3e43a32a
MS
15915 error (_("passcount command requires an "
15916 "argument (count + optional TP num)"));
1042e4c0 15917
4a64f543 15918 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15919
529480d0 15920 args = skip_spaces (args);
1042e4c0
SS
15921 if (*args && strncasecmp (args, "all", 3) == 0)
15922 {
d9b3f62e
PA
15923 struct breakpoint *b;
15924
1042e4c0 15925 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15926 if (*args)
15927 error (_("Junk at end of arguments."));
1042e4c0 15928
d9b3f62e 15929 ALL_TRACEPOINTS (b)
197f0a60 15930 {
d9b3f62e 15931 t1 = (struct tracepoint *) b;
197f0a60
TT
15932 trace_pass_set_count (t1, count, from_tty);
15933 }
15934 }
15935 else if (*args == '\0')
1042e4c0 15936 {
5fa1d40e 15937 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15938 if (t1)
197f0a60
TT
15939 trace_pass_set_count (t1, count, from_tty);
15940 }
15941 else
15942 {
15943 struct get_number_or_range_state state;
15944
15945 init_number_or_range (&state, args);
15946 while (!state.finished)
1042e4c0 15947 {
5fa1d40e 15948 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15949 if (t1)
15950 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15951 }
15952 }
1042e4c0
SS
15953}
15954
d9b3f62e 15955struct tracepoint *
1042e4c0
SS
15956get_tracepoint (int num)
15957{
15958 struct breakpoint *t;
15959
15960 ALL_TRACEPOINTS (t)
15961 if (t->number == num)
d9b3f62e 15962 return (struct tracepoint *) t;
1042e4c0
SS
15963
15964 return NULL;
15965}
15966
d5551862
SS
15967/* Find the tracepoint with the given target-side number (which may be
15968 different from the tracepoint number after disconnecting and
15969 reconnecting). */
15970
d9b3f62e 15971struct tracepoint *
d5551862
SS
15972get_tracepoint_by_number_on_target (int num)
15973{
d9b3f62e 15974 struct breakpoint *b;
d5551862 15975
d9b3f62e
PA
15976 ALL_TRACEPOINTS (b)
15977 {
15978 struct tracepoint *t = (struct tracepoint *) b;
15979
15980 if (t->number_on_target == num)
15981 return t;
15982 }
d5551862
SS
15983
15984 return NULL;
15985}
15986
1042e4c0 15987/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15988 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15989 If the argument is missing, the most recent tracepoint
15990 (tracepoint_count) is returned. */
15991
d9b3f62e 15992struct tracepoint *
197f0a60 15993get_tracepoint_by_number (char **arg,
5fa1d40e 15994 struct get_number_or_range_state *state)
1042e4c0 15995{
1042e4c0
SS
15996 struct breakpoint *t;
15997 int tpnum;
15998 char *instring = arg == NULL ? NULL : *arg;
15999
197f0a60
TT
16000 if (state)
16001 {
16002 gdb_assert (!state->finished);
16003 tpnum = get_number_or_range (state);
16004 }
16005 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 16006 tpnum = tracepoint_count;
1042e4c0 16007 else
197f0a60 16008 tpnum = get_number (arg);
1042e4c0
SS
16009
16010 if (tpnum <= 0)
16011 {
16012 if (instring && *instring)
16013 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
16014 instring);
16015 else
5fa1d40e 16016 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
16017 return NULL;
16018 }
16019
16020 ALL_TRACEPOINTS (t)
16021 if (t->number == tpnum)
16022 {
d9b3f62e 16023 return (struct tracepoint *) t;
1042e4c0
SS
16024 }
16025
1042e4c0
SS
16026 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
16027 return NULL;
16028}
16029
d9b3f62e
PA
16030void
16031print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
16032{
16033 if (b->thread != -1)
16034 fprintf_unfiltered (fp, " thread %d", b->thread);
16035
16036 if (b->task != 0)
16037 fprintf_unfiltered (fp, " task %d", b->task);
16038
16039 fprintf_unfiltered (fp, "\n");
16040}
16041
6149aea9
PA
16042/* Save information on user settable breakpoints (watchpoints, etc) to
16043 a new script file named FILENAME. If FILTER is non-NULL, call it
16044 on each breakpoint and only include the ones for which it returns
16045 non-zero. */
16046
1042e4c0 16047static void
6149aea9
PA
16048save_breakpoints (char *filename, int from_tty,
16049 int (*filter) (const struct breakpoint *))
1042e4c0
SS
16050{
16051 struct breakpoint *tp;
6149aea9 16052 int any = 0;
1042e4c0 16053 struct cleanup *cleanup;
a7bdde9e 16054 struct ui_file *fp;
6149aea9 16055 int extra_trace_bits = 0;
1042e4c0 16056
6149aea9
PA
16057 if (filename == 0 || *filename == 0)
16058 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
16059
16060 /* See if we have anything to save. */
6149aea9 16061 ALL_BREAKPOINTS (tp)
1042e4c0 16062 {
6149aea9 16063 /* Skip internal and momentary breakpoints. */
09d682a4 16064 if (!user_breakpoint_p (tp))
6149aea9
PA
16065 continue;
16066
16067 /* If we have a filter, only save the breakpoints it accepts. */
16068 if (filter && !filter (tp))
16069 continue;
16070
16071 any = 1;
16072
16073 if (is_tracepoint (tp))
16074 {
16075 extra_trace_bits = 1;
16076
16077 /* We can stop searching. */
16078 break;
16079 }
1042e4c0 16080 }
6149aea9
PA
16081
16082 if (!any)
1042e4c0 16083 {
6149aea9 16084 warning (_("Nothing to save."));
1042e4c0
SS
16085 return;
16086 }
16087
c718be47
PA
16088 filename = tilde_expand (filename);
16089 cleanup = make_cleanup (xfree, filename);
16090 fp = gdb_fopen (filename, "w");
059fb39f 16091 if (!fp)
6149aea9
PA
16092 error (_("Unable to open file '%s' for saving (%s)"),
16093 filename, safe_strerror (errno));
a7bdde9e 16094 make_cleanup_ui_file_delete (fp);
8bf6485c 16095
6149aea9
PA
16096 if (extra_trace_bits)
16097 save_trace_state_variables (fp);
8bf6485c 16098
6149aea9 16099 ALL_BREAKPOINTS (tp)
1042e4c0 16100 {
6149aea9 16101 /* Skip internal and momentary breakpoints. */
09d682a4 16102 if (!user_breakpoint_p (tp))
6149aea9 16103 continue;
8bf6485c 16104
6149aea9
PA
16105 /* If we have a filter, only save the breakpoints it accepts. */
16106 if (filter && !filter (tp))
16107 continue;
16108
348d480f 16109 tp->ops->print_recreate (tp, fp);
1042e4c0 16110
6149aea9
PA
16111 /* Note, we can't rely on tp->number for anything, as we can't
16112 assume the recreated breakpoint numbers will match. Use $bpnum
16113 instead. */
16114
16115 if (tp->cond_string)
16116 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
16117
16118 if (tp->ignore_count)
16119 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
16120
2d9442cc 16121 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 16122 {
a7bdde9e
VP
16123 volatile struct gdb_exception ex;
16124
6149aea9 16125 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 16126
79a45e25 16127 ui_out_redirect (current_uiout, fp);
14dba4b4 16128 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 16129 {
79a45e25 16130 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 16131 }
79a45e25 16132 ui_out_redirect (current_uiout, NULL);
1042e4c0 16133
a7bdde9e
VP
16134 if (ex.reason < 0)
16135 throw_exception (ex);
1042e4c0 16136
a7bdde9e 16137 fprintf_unfiltered (fp, " end\n");
1042e4c0 16138 }
6149aea9
PA
16139
16140 if (tp->enable_state == bp_disabled)
16141 fprintf_unfiltered (fp, "disable\n");
16142
16143 /* If this is a multi-location breakpoint, check if the locations
16144 should be individually disabled. Watchpoint locations are
16145 special, and not user visible. */
16146 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
16147 {
16148 struct bp_location *loc;
16149 int n = 1;
16150
16151 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
16152 if (!loc->enabled)
16153 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
16154 }
1042e4c0 16155 }
8bf6485c 16156
6149aea9 16157 if (extra_trace_bits && *default_collect)
8bf6485c
SS
16158 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
16159
1042e4c0 16160 if (from_tty)
6149aea9 16161 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 16162 do_cleanups (cleanup);
6149aea9
PA
16163}
16164
16165/* The `save breakpoints' command. */
16166
16167static void
16168save_breakpoints_command (char *args, int from_tty)
16169{
16170 save_breakpoints (args, from_tty, NULL);
16171}
16172
16173/* The `save tracepoints' command. */
16174
16175static void
16176save_tracepoints_command (char *args, int from_tty)
16177{
16178 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16179}
16180
16181/* Create a vector of all tracepoints. */
16182
16183VEC(breakpoint_p) *
eeae04df 16184all_tracepoints (void)
1042e4c0
SS
16185{
16186 VEC(breakpoint_p) *tp_vec = 0;
16187 struct breakpoint *tp;
16188
16189 ALL_TRACEPOINTS (tp)
16190 {
16191 VEC_safe_push (breakpoint_p, tp_vec, tp);
16192 }
16193
16194 return tp_vec;
16195}
16196
c906108c 16197\f
4a64f543
MS
16198/* This help string is used for the break, hbreak, tbreak and thbreak
16199 commands. It is defined as a macro to prevent duplication.
16200 COMMAND should be a string constant containing the name of the
16201 command. */
31e2b00f 16202#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16203command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16204PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16205probe point. Accepted values are `-probe' (for a generic, automatically\n\
16206guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16207LOCATION may be a line number, function name, or \"*\" and an address.\n\
16208If a line number is specified, break at start of code for that line.\n\
16209If a function is specified, break at start of code for that function.\n\
16210If an address is specified, break at that exact address.\n\
dc10affe
PA
16211With no LOCATION, uses current execution address of the selected\n\
16212stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16213\n\
16214THREADNUM is the number from \"info threads\".\n\
16215CONDITION is a boolean expression.\n\
16216\n\
d41c0fc8
PA
16217Multiple breakpoints at one place are permitted, and useful if their\n\
16218conditions are different.\n\
31e2b00f
AS
16219\n\
16220Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16221
44feb3ce
TT
16222/* List of subcommands for "catch". */
16223static struct cmd_list_element *catch_cmdlist;
16224
16225/* List of subcommands for "tcatch". */
16226static struct cmd_list_element *tcatch_cmdlist;
16227
9ac4176b 16228void
44feb3ce 16229add_catch_command (char *name, char *docstring,
82ae6c8d 16230 cmd_sfunc_ftype *sfunc,
625e8578 16231 completer_ftype *completer,
44feb3ce
TT
16232 void *user_data_catch,
16233 void *user_data_tcatch)
16234{
16235 struct cmd_list_element *command;
16236
16237 command = add_cmd (name, class_breakpoint, NULL, docstring,
16238 &catch_cmdlist);
16239 set_cmd_sfunc (command, sfunc);
16240 set_cmd_context (command, user_data_catch);
a96d9b2e 16241 set_cmd_completer (command, completer);
44feb3ce
TT
16242
16243 command = add_cmd (name, class_breakpoint, NULL, docstring,
16244 &tcatch_cmdlist);
16245 set_cmd_sfunc (command, sfunc);
16246 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16247 set_cmd_completer (command, completer);
44feb3ce
TT
16248}
16249
6c95b8df 16250static void
a79b8f6e 16251clear_syscall_counts (struct inferior *inf)
6c95b8df 16252{
fa3064dd
YQ
16253 struct catch_syscall_inferior_data *inf_data
16254 = get_catch_syscall_inferior_data (inf);
16255
16256 inf_data->total_syscalls_count = 0;
16257 inf_data->any_syscall_count = 0;
16258 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16259}
16260
6149aea9
PA
16261static void
16262save_command (char *arg, int from_tty)
16263{
3e43a32a
MS
16264 printf_unfiltered (_("\"save\" must be followed by "
16265 "the name of a save subcommand.\n"));
635c7e8a 16266 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16267}
16268
84f4c1fe
PM
16269struct breakpoint *
16270iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16271 void *data)
16272{
35df4500 16273 struct breakpoint *b, *b_tmp;
84f4c1fe 16274
35df4500 16275 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16276 {
16277 if ((*callback) (b, data))
16278 return b;
16279 }
16280
16281 return NULL;
16282}
16283
0574c78f
GB
16284/* Zero if any of the breakpoint's locations could be a location where
16285 functions have been inlined, nonzero otherwise. */
16286
16287static int
16288is_non_inline_function (struct breakpoint *b)
16289{
16290 /* The shared library event breakpoint is set on the address of a
16291 non-inline function. */
16292 if (b->type == bp_shlib_event)
16293 return 1;
16294
16295 return 0;
16296}
16297
16298/* Nonzero if the specified PC cannot be a location where functions
16299 have been inlined. */
16300
16301int
09ac7c10
TT
16302pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16303 const struct target_waitstatus *ws)
0574c78f
GB
16304{
16305 struct breakpoint *b;
16306 struct bp_location *bl;
16307
16308 ALL_BREAKPOINTS (b)
16309 {
16310 if (!is_non_inline_function (b))
16311 continue;
16312
16313 for (bl = b->loc; bl != NULL; bl = bl->next)
16314 {
16315 if (!bl->shlib_disabled
09ac7c10 16316 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16317 return 1;
16318 }
16319 }
16320
16321 return 0;
16322}
16323
2f202fde
JK
16324/* Remove any references to OBJFILE which is going to be freed. */
16325
16326void
16327breakpoint_free_objfile (struct objfile *objfile)
16328{
16329 struct bp_location **locp, *loc;
16330
16331 ALL_BP_LOCATIONS (loc, locp)
16332 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16333 loc->symtab = NULL;
16334}
16335
2060206e
PA
16336void
16337initialize_breakpoint_ops (void)
16338{
16339 static int initialized = 0;
16340
16341 struct breakpoint_ops *ops;
16342
16343 if (initialized)
16344 return;
16345 initialized = 1;
16346
16347 /* The breakpoint_ops structure to be inherit by all kinds of
16348 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16349 internal and momentary breakpoints, etc.). */
16350 ops = &bkpt_base_breakpoint_ops;
16351 *ops = base_breakpoint_ops;
16352 ops->re_set = bkpt_re_set;
16353 ops->insert_location = bkpt_insert_location;
16354 ops->remove_location = bkpt_remove_location;
16355 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16356 ops->create_sals_from_address = bkpt_create_sals_from_address;
16357 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16358 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16359
16360 /* The breakpoint_ops structure to be used in regular breakpoints. */
16361 ops = &bkpt_breakpoint_ops;
16362 *ops = bkpt_base_breakpoint_ops;
16363 ops->re_set = bkpt_re_set;
16364 ops->resources_needed = bkpt_resources_needed;
16365 ops->print_it = bkpt_print_it;
16366 ops->print_mention = bkpt_print_mention;
16367 ops->print_recreate = bkpt_print_recreate;
16368
16369 /* Ranged breakpoints. */
16370 ops = &ranged_breakpoint_ops;
16371 *ops = bkpt_breakpoint_ops;
16372 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16373 ops->resources_needed = resources_needed_ranged_breakpoint;
16374 ops->print_it = print_it_ranged_breakpoint;
16375 ops->print_one = print_one_ranged_breakpoint;
16376 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16377 ops->print_mention = print_mention_ranged_breakpoint;
16378 ops->print_recreate = print_recreate_ranged_breakpoint;
16379
16380 /* Internal breakpoints. */
16381 ops = &internal_breakpoint_ops;
16382 *ops = bkpt_base_breakpoint_ops;
16383 ops->re_set = internal_bkpt_re_set;
16384 ops->check_status = internal_bkpt_check_status;
16385 ops->print_it = internal_bkpt_print_it;
16386 ops->print_mention = internal_bkpt_print_mention;
16387
16388 /* Momentary breakpoints. */
16389 ops = &momentary_breakpoint_ops;
16390 *ops = bkpt_base_breakpoint_ops;
16391 ops->re_set = momentary_bkpt_re_set;
16392 ops->check_status = momentary_bkpt_check_status;
16393 ops->print_it = momentary_bkpt_print_it;
16394 ops->print_mention = momentary_bkpt_print_mention;
16395
e2e4d78b
JK
16396 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16397 ops = &longjmp_breakpoint_ops;
16398 *ops = momentary_breakpoint_ops;
16399 ops->dtor = longjmp_bkpt_dtor;
16400
55aa24fb
SDJ
16401 /* Probe breakpoints. */
16402 ops = &bkpt_probe_breakpoint_ops;
16403 *ops = bkpt_breakpoint_ops;
16404 ops->insert_location = bkpt_probe_insert_location;
16405 ops->remove_location = bkpt_probe_remove_location;
16406 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16407 ops->decode_linespec = bkpt_probe_decode_linespec;
16408
2060206e
PA
16409 /* Watchpoints. */
16410 ops = &watchpoint_breakpoint_ops;
16411 *ops = base_breakpoint_ops;
3a5c3e22 16412 ops->dtor = dtor_watchpoint;
2060206e
PA
16413 ops->re_set = re_set_watchpoint;
16414 ops->insert_location = insert_watchpoint;
16415 ops->remove_location = remove_watchpoint;
16416 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16417 ops->check_status = check_status_watchpoint;
16418 ops->resources_needed = resources_needed_watchpoint;
16419 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16420 ops->print_it = print_it_watchpoint;
16421 ops->print_mention = print_mention_watchpoint;
16422 ops->print_recreate = print_recreate_watchpoint;
427cd150 16423 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16424
16425 /* Masked watchpoints. */
16426 ops = &masked_watchpoint_breakpoint_ops;
16427 *ops = watchpoint_breakpoint_ops;
16428 ops->insert_location = insert_masked_watchpoint;
16429 ops->remove_location = remove_masked_watchpoint;
16430 ops->resources_needed = resources_needed_masked_watchpoint;
16431 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16432 ops->print_it = print_it_masked_watchpoint;
16433 ops->print_one_detail = print_one_detail_masked_watchpoint;
16434 ops->print_mention = print_mention_masked_watchpoint;
16435 ops->print_recreate = print_recreate_masked_watchpoint;
16436
16437 /* Tracepoints. */
16438 ops = &tracepoint_breakpoint_ops;
16439 *ops = base_breakpoint_ops;
16440 ops->re_set = tracepoint_re_set;
16441 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16442 ops->print_one_detail = tracepoint_print_one_detail;
16443 ops->print_mention = tracepoint_print_mention;
16444 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16445 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16446 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16447 ops->decode_linespec = tracepoint_decode_linespec;
16448
55aa24fb
SDJ
16449 /* Probe tracepoints. */
16450 ops = &tracepoint_probe_breakpoint_ops;
16451 *ops = tracepoint_breakpoint_ops;
16452 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16453 ops->decode_linespec = tracepoint_probe_decode_linespec;
16454
983af33b
SDJ
16455 /* Static tracepoints with marker (`-m'). */
16456 ops = &strace_marker_breakpoint_ops;
16457 *ops = tracepoint_breakpoint_ops;
16458 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16459 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16460 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16461
16462 /* Fork catchpoints. */
16463 ops = &catch_fork_breakpoint_ops;
16464 *ops = base_breakpoint_ops;
16465 ops->insert_location = insert_catch_fork;
16466 ops->remove_location = remove_catch_fork;
16467 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16468 ops->print_it = print_it_catch_fork;
16469 ops->print_one = print_one_catch_fork;
16470 ops->print_mention = print_mention_catch_fork;
16471 ops->print_recreate = print_recreate_catch_fork;
16472
16473 /* Vfork catchpoints. */
16474 ops = &catch_vfork_breakpoint_ops;
16475 *ops = base_breakpoint_ops;
16476 ops->insert_location = insert_catch_vfork;
16477 ops->remove_location = remove_catch_vfork;
16478 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16479 ops->print_it = print_it_catch_vfork;
16480 ops->print_one = print_one_catch_vfork;
16481 ops->print_mention = print_mention_catch_vfork;
16482 ops->print_recreate = print_recreate_catch_vfork;
16483
16484 /* Exec catchpoints. */
16485 ops = &catch_exec_breakpoint_ops;
16486 *ops = base_breakpoint_ops;
16487 ops->dtor = dtor_catch_exec;
16488 ops->insert_location = insert_catch_exec;
16489 ops->remove_location = remove_catch_exec;
16490 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16491 ops->print_it = print_it_catch_exec;
16492 ops->print_one = print_one_catch_exec;
16493 ops->print_mention = print_mention_catch_exec;
16494 ops->print_recreate = print_recreate_catch_exec;
16495
16496 /* Syscall catchpoints. */
16497 ops = &catch_syscall_breakpoint_ops;
16498 *ops = base_breakpoint_ops;
16499 ops->dtor = dtor_catch_syscall;
16500 ops->insert_location = insert_catch_syscall;
16501 ops->remove_location = remove_catch_syscall;
16502 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16503 ops->print_it = print_it_catch_syscall;
16504 ops->print_one = print_one_catch_syscall;
16505 ops->print_mention = print_mention_catch_syscall;
16506 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16507
16508 /* Solib-related catchpoints. */
16509 ops = &catch_solib_breakpoint_ops;
16510 *ops = base_breakpoint_ops;
16511 ops->dtor = dtor_catch_solib;
16512 ops->insert_location = insert_catch_solib;
16513 ops->remove_location = remove_catch_solib;
16514 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16515 ops->check_status = check_status_catch_solib;
16516 ops->print_it = print_it_catch_solib;
16517 ops->print_one = print_one_catch_solib;
16518 ops->print_mention = print_mention_catch_solib;
16519 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16520
16521 ops = &dprintf_breakpoint_ops;
16522 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16523 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16524 ops->resources_needed = bkpt_resources_needed;
16525 ops->print_it = bkpt_print_it;
16526 ops->print_mention = bkpt_print_mention;
2d9442cc 16527 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16528 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16529 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16530}
16531
8bfd80db
YQ
16532/* Chain containing all defined "enable breakpoint" subcommands. */
16533
16534static struct cmd_list_element *enablebreaklist = NULL;
16535
c906108c 16536void
fba45db2 16537_initialize_breakpoint (void)
c906108c
SS
16538{
16539 struct cmd_list_element *c;
16540
2060206e
PA
16541 initialize_breakpoint_ops ();
16542
84acb35a 16543 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16544 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16545 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16546 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16547
55aa24fb
SDJ
16548 breakpoint_objfile_key
16549 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16550
fa3064dd 16551 catch_syscall_inferior_data
8e260fc0
TT
16552 = register_inferior_data_with_cleanup (NULL,
16553 catch_syscall_inferior_data_cleanup);
fa3064dd 16554
c906108c
SS
16555 breakpoint_chain = 0;
16556 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16557 before a breakpoint is set. */
16558 breakpoint_count = 0;
16559
1042e4c0
SS
16560 tracepoint_count = 0;
16561
1bedd215
AC
16562 add_com ("ignore", class_breakpoint, ignore_command, _("\
16563Set ignore-count of breakpoint number N to COUNT.\n\
16564Usage is `ignore N COUNT'."));
c906108c 16565 if (xdb_commands)
c5aa993b 16566 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16567
1bedd215
AC
16568 add_com ("commands", class_breakpoint, commands_command, _("\
16569Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16570Give breakpoint number as argument after \"commands\".\n\
16571With no argument, the targeted breakpoint is the last one set.\n\
16572The commands themselves follow starting on the next line.\n\
16573Type a line containing \"end\" to indicate the end of them.\n\
16574Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16575then no output is printed when it is hit, except what the commands print."));
c906108c 16576
d55637df 16577 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16578Specify breakpoint number N to break only if COND is true.\n\
c906108c 16579Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16580expression to be evaluated whenever breakpoint N is reached."));
d55637df 16581 set_cmd_completer (c, condition_completer);
c906108c 16582
1bedd215 16583 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16584Set a temporary breakpoint.\n\
c906108c
SS
16585Like \"break\" except the breakpoint is only temporary,\n\
16586so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16587by using \"enable delete\" on the breakpoint number.\n\
16588\n"
16589BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16590 set_cmd_completer (c, location_completer);
c94fdfd0 16591
1bedd215 16592 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16593Set a hardware assisted breakpoint.\n\
c906108c 16594Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16595some target hardware may not have this support.\n\
16596\n"
16597BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16598 set_cmd_completer (c, location_completer);
c906108c 16599
1bedd215 16600 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16601Set a temporary hardware assisted breakpoint.\n\
c906108c 16602Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16603so it will be deleted when hit.\n\
16604\n"
16605BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16606 set_cmd_completer (c, location_completer);
c906108c 16607
1bedd215
AC
16608 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16609Enable some breakpoints.\n\
c906108c
SS
16610Give breakpoint numbers (separated by spaces) as arguments.\n\
16611With no subcommand, breakpoints are enabled until you command otherwise.\n\
16612This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16613With a subcommand you can enable temporarily."),
c906108c
SS
16614 &enablelist, "enable ", 1, &cmdlist);
16615 if (xdb_commands)
1bedd215
AC
16616 add_com ("ab", class_breakpoint, enable_command, _("\
16617Enable some breakpoints.\n\
c906108c
SS
16618Give breakpoint numbers (separated by spaces) as arguments.\n\
16619With no subcommand, breakpoints are enabled until you command otherwise.\n\
16620This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16621With a subcommand you can enable temporarily."));
c906108c
SS
16622
16623 add_com_alias ("en", "enable", class_breakpoint, 1);
16624
84951ab5 16625 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16626Enable some breakpoints.\n\
c906108c
SS
16627Give breakpoint numbers (separated by spaces) as arguments.\n\
16628This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16629May be abbreviated to simply \"enable\".\n"),
c5aa993b 16630 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16631
1a966eab
AC
16632 add_cmd ("once", no_class, enable_once_command, _("\
16633Enable breakpoints for one hit. Give breakpoint numbers.\n\
16634If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16635 &enablebreaklist);
16636
1a966eab
AC
16637 add_cmd ("delete", no_class, enable_delete_command, _("\
16638Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16639If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16640 &enablebreaklist);
16641
816338b5
SS
16642 add_cmd ("count", no_class, enable_count_command, _("\
16643Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16644If a breakpoint is hit while enabled in this fashion,\n\
16645the count is decremented; when it reaches zero, the breakpoint is disabled."),
16646 &enablebreaklist);
16647
1a966eab
AC
16648 add_cmd ("delete", no_class, enable_delete_command, _("\
16649Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16650If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16651 &enablelist);
16652
1a966eab
AC
16653 add_cmd ("once", no_class, enable_once_command, _("\
16654Enable breakpoints for one hit. Give breakpoint numbers.\n\
16655If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16656 &enablelist);
16657
16658 add_cmd ("count", no_class, enable_count_command, _("\
16659Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16660If a breakpoint is hit while enabled in this fashion,\n\
16661the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16662 &enablelist);
16663
1bedd215
AC
16664 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16665Disable some breakpoints.\n\
c906108c
SS
16666Arguments are breakpoint numbers with spaces in between.\n\
16667To disable all breakpoints, give no argument.\n\
64b9b334 16668A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16669 &disablelist, "disable ", 1, &cmdlist);
16670 add_com_alias ("dis", "disable", class_breakpoint, 1);
16671 add_com_alias ("disa", "disable", class_breakpoint, 1);
16672 if (xdb_commands)
1bedd215
AC
16673 add_com ("sb", class_breakpoint, disable_command, _("\
16674Disable some breakpoints.\n\
c906108c
SS
16675Arguments are breakpoint numbers with spaces in between.\n\
16676To disable all breakpoints, give no argument.\n\
64b9b334 16677A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16678
1a966eab
AC
16679 add_cmd ("breakpoints", class_alias, disable_command, _("\
16680Disable some breakpoints.\n\
c906108c
SS
16681Arguments are breakpoint numbers with spaces in between.\n\
16682To disable all breakpoints, give no argument.\n\
64b9b334 16683A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16684This command may be abbreviated \"disable\"."),
c906108c
SS
16685 &disablelist);
16686
1bedd215
AC
16687 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16688Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16689Arguments are breakpoint numbers with spaces in between.\n\
16690To delete all breakpoints, give no argument.\n\
16691\n\
16692Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16693The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16694 &deletelist, "delete ", 1, &cmdlist);
16695 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16696 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16697 if (xdb_commands)
1bedd215
AC
16698 add_com ("db", class_breakpoint, delete_command, _("\
16699Delete some breakpoints.\n\
c906108c 16700Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16701To delete all breakpoints, give no argument.\n"));
c906108c 16702
1a966eab
AC
16703 add_cmd ("breakpoints", class_alias, delete_command, _("\
16704Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16705Arguments are breakpoint numbers with spaces in between.\n\
16706To delete all breakpoints, give no argument.\n\
1a966eab 16707This command may be abbreviated \"delete\"."),
c906108c
SS
16708 &deletelist);
16709
1bedd215
AC
16710 add_com ("clear", class_breakpoint, clear_command, _("\
16711Clear breakpoint at specified line or function.\n\
c906108c
SS
16712Argument may be line number, function name, or \"*\" and an address.\n\
16713If line number is specified, all breakpoints in that line are cleared.\n\
16714If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16715If an address is specified, breakpoints at that address are cleared.\n\
16716\n\
16717With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16718is executing in.\n\
16719\n\
1bedd215 16720See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16721 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16722
1bedd215 16723 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16724Set breakpoint at specified line or function.\n"
16725BREAK_ARGS_HELP ("break")));
5ba2abeb 16726 set_cmd_completer (c, location_completer);
c94fdfd0 16727
c906108c
SS
16728 add_com_alias ("b", "break", class_run, 1);
16729 add_com_alias ("br", "break", class_run, 1);
16730 add_com_alias ("bre", "break", class_run, 1);
16731 add_com_alias ("brea", "break", class_run, 1);
16732
7681d515
PM
16733 if (xdb_commands)
16734 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16735
16736 if (dbx_commands)
16737 {
1bedd215
AC
16738 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16739Break in function/address or break at a line in the current file."),
c5aa993b
JM
16740 &stoplist, "stop ", 1, &cmdlist);
16741 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16742 _("Break in function or address."), &stoplist);
c5aa993b 16743 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16744 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16745 add_com ("status", class_info, breakpoints_info, _("\
16746Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16747The \"Type\" column indicates one of:\n\
16748\tbreakpoint - normal breakpoint\n\
16749\twatchpoint - watchpoint\n\
16750The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16751the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16752breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16753address and file/line number respectively.\n\
16754\n\
16755Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16756are set to the address of the last breakpoint listed unless the command\n\
16757is prefixed with \"server \".\n\n\
c906108c 16758Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16759breakpoint set."));
c906108c
SS
16760 }
16761
1bedd215 16762 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16763Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16764The \"Type\" column indicates one of:\n\
16765\tbreakpoint - normal breakpoint\n\
16766\twatchpoint - watchpoint\n\
16767The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16768the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16769breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16770address and file/line number respectively.\n\
16771\n\
16772Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16773are set to the address of the last breakpoint listed unless the command\n\
16774is prefixed with \"server \".\n\n\
c906108c 16775Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16776breakpoint set."));
c906108c 16777
6b04bdb7
MS
16778 add_info_alias ("b", "breakpoints", 1);
16779
c906108c 16780 if (xdb_commands)
1bedd215
AC
16781 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16782Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16783The \"Type\" column indicates one of:\n\
16784\tbreakpoint - normal breakpoint\n\
16785\twatchpoint - watchpoint\n\
16786The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16787the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16788breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16789address and file/line number respectively.\n\
16790\n\
16791Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16792are set to the address of the last breakpoint listed unless the command\n\
16793is prefixed with \"server \".\n\n\
c906108c 16794Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16795breakpoint set."));
c906108c 16796
1a966eab
AC
16797 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16798Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16799The \"Type\" column indicates one of:\n\
16800\tbreakpoint - normal breakpoint\n\
16801\twatchpoint - watchpoint\n\
16802\tlongjmp - internal breakpoint used to step through longjmp()\n\
16803\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16804\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16805\tfinish - internal breakpoint used by the \"finish\" command\n\
16806The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16807the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16808breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16809address and file/line number respectively.\n\
16810\n\
16811Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16812are set to the address of the last breakpoint listed unless the command\n\
16813is prefixed with \"server \".\n\n\
c906108c 16814Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16815breakpoint set."),
c906108c
SS
16816 &maintenanceinfolist);
16817
44feb3ce
TT
16818 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16819Set catchpoints to catch events."),
16820 &catch_cmdlist, "catch ",
16821 0/*allow-unknown*/, &cmdlist);
16822
16823 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16824Set temporary catchpoints to catch events."),
16825 &tcatch_cmdlist, "tcatch ",
16826 0/*allow-unknown*/, &cmdlist);
16827
44feb3ce
TT
16828 add_catch_command ("fork", _("Catch calls to fork."),
16829 catch_fork_command_1,
a96d9b2e 16830 NULL,
44feb3ce
TT
16831 (void *) (uintptr_t) catch_fork_permanent,
16832 (void *) (uintptr_t) catch_fork_temporary);
16833 add_catch_command ("vfork", _("Catch calls to vfork."),
16834 catch_fork_command_1,
a96d9b2e 16835 NULL,
44feb3ce
TT
16836 (void *) (uintptr_t) catch_vfork_permanent,
16837 (void *) (uintptr_t) catch_vfork_temporary);
16838 add_catch_command ("exec", _("Catch calls to exec."),
16839 catch_exec_command_1,
a96d9b2e
SDJ
16840 NULL,
16841 CATCH_PERMANENT,
16842 CATCH_TEMPORARY);
edcc5120
TT
16843 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16844Usage: catch load [REGEX]\n\
16845If REGEX is given, only stop for libraries matching the regular expression."),
16846 catch_load_command_1,
16847 NULL,
16848 CATCH_PERMANENT,
16849 CATCH_TEMPORARY);
16850 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16851Usage: catch unload [REGEX]\n\
16852If REGEX is given, only stop for libraries matching the regular expression."),
16853 catch_unload_command_1,
16854 NULL,
16855 CATCH_PERMANENT,
16856 CATCH_TEMPORARY);
a96d9b2e
SDJ
16857 add_catch_command ("syscall", _("\
16858Catch system calls by their names and/or numbers.\n\
16859Arguments say which system calls to catch. If no arguments\n\
16860are given, every system call will be caught.\n\
16861Arguments, if given, should be one or more system call names\n\
16862(if your system supports that), or system call numbers."),
16863 catch_syscall_command_1,
16864 catch_syscall_completer,
44feb3ce
TT
16865 CATCH_PERMANENT,
16866 CATCH_TEMPORARY);
c5aa993b 16867
1bedd215
AC
16868 c = add_com ("watch", class_breakpoint, watch_command, _("\
16869Set a watchpoint for an expression.\n\
06a64a0b 16870Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16871A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16872an expression changes.\n\
16873If -l or -location is given, this evaluates EXPRESSION and watches\n\
16874the memory to which it refers."));
65d12d83 16875 set_cmd_completer (c, expression_completer);
c906108c 16876
1bedd215
AC
16877 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16878Set a read watchpoint for an expression.\n\
06a64a0b 16879Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16880A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16881an expression is read.\n\
16882If -l or -location is given, this evaluates EXPRESSION and watches\n\
16883the memory to which it refers."));
65d12d83 16884 set_cmd_completer (c, expression_completer);
c906108c 16885
1bedd215
AC
16886 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16887Set a watchpoint for an expression.\n\
06a64a0b 16888Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16889A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16890an expression is either read or written.\n\
16891If -l or -location is given, this evaluates EXPRESSION and watches\n\
16892the memory to which it refers."));
65d12d83 16893 set_cmd_completer (c, expression_completer);
c906108c 16894
d77f58be 16895 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16896Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16897
920d2a44
AC
16898 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16899 respond to changes - contrary to the description. */
85c07804
AC
16900 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16901 &can_use_hw_watchpoints, _("\
16902Set debugger's willingness to use watchpoint hardware."), _("\
16903Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16904If zero, gdb will not use hardware for new watchpoints, even if\n\
16905such is available. (However, any hardware watchpoints that were\n\
16906created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16907hardware.)"),
16908 NULL,
920d2a44 16909 show_can_use_hw_watchpoints,
85c07804 16910 &setlist, &showlist);
c906108c
SS
16911
16912 can_use_hw_watchpoints = 1;
fa8d40ab 16913
1042e4c0
SS
16914 /* Tracepoint manipulation commands. */
16915
16916 c = add_com ("trace", class_breakpoint, trace_command, _("\
16917Set a tracepoint at specified line or function.\n\
16918\n"
16919BREAK_ARGS_HELP ("trace") "\n\
16920Do \"help tracepoints\" for info on other tracepoint commands."));
16921 set_cmd_completer (c, location_completer);
16922
16923 add_com_alias ("tp", "trace", class_alias, 0);
16924 add_com_alias ("tr", "trace", class_alias, 1);
16925 add_com_alias ("tra", "trace", class_alias, 1);
16926 add_com_alias ("trac", "trace", class_alias, 1);
16927
7a697b8d
SS
16928 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16929Set a fast tracepoint at specified line or function.\n\
16930\n"
16931BREAK_ARGS_HELP ("ftrace") "\n\
16932Do \"help tracepoints\" for info on other tracepoint commands."));
16933 set_cmd_completer (c, location_completer);
16934
0fb4aa4b
PA
16935 c = add_com ("strace", class_breakpoint, strace_command, _("\
16936Set a static tracepoint at specified line, function or marker.\n\
16937\n\
16938strace [LOCATION] [if CONDITION]\n\
16939LOCATION may be a line number, function name, \"*\" and an address,\n\
16940or -m MARKER_ID.\n\
16941If a line number is specified, probe the marker at start of code\n\
16942for that line. If a function is specified, probe the marker at start\n\
16943of code for that function. If an address is specified, probe the marker\n\
16944at that exact address. If a marker id is specified, probe the marker\n\
16945with that name. With no LOCATION, uses current execution address of\n\
16946the selected stack frame.\n\
16947Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16948This collects arbitrary user data passed in the probe point call to the\n\
16949tracing library. You can inspect it when analyzing the trace buffer,\n\
16950by printing the $_sdata variable like any other convenience variable.\n\
16951\n\
16952CONDITION is a boolean expression.\n\
16953\n\
d41c0fc8
PA
16954Multiple tracepoints at one place are permitted, and useful if their\n\
16955conditions are different.\n\
0fb4aa4b
PA
16956\n\
16957Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16958Do \"help tracepoints\" for info on other tracepoint commands."));
16959 set_cmd_completer (c, location_completer);
16960
1042e4c0 16961 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16962Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16963Convenience variable \"$tpnum\" contains the number of the\n\
16964last tracepoint set."));
16965
16966 add_info_alias ("tp", "tracepoints", 1);
16967
16968 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16969Delete specified tracepoints.\n\
16970Arguments are tracepoint numbers, separated by spaces.\n\
16971No argument means delete all tracepoints."),
16972 &deletelist);
7e20dfcd 16973 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16974
16975 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16976Disable specified tracepoints.\n\
16977Arguments are tracepoint numbers, separated by spaces.\n\
16978No argument means disable all tracepoints."),
16979 &disablelist);
16980 deprecate_cmd (c, "disable");
16981
16982 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16983Enable specified tracepoints.\n\
16984Arguments are tracepoint numbers, separated by spaces.\n\
16985No argument means enable all tracepoints."),
16986 &enablelist);
16987 deprecate_cmd (c, "enable");
16988
16989 add_com ("passcount", class_trace, trace_pass_command, _("\
16990Set the passcount for a tracepoint.\n\
16991The trace will end when the tracepoint has been passed 'count' times.\n\
16992Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16993if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16994
6149aea9
PA
16995 add_prefix_cmd ("save", class_breakpoint, save_command,
16996 _("Save breakpoint definitions as a script."),
16997 &save_cmdlist, "save ",
16998 0/*allow-unknown*/, &cmdlist);
16999
17000 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
17001Save current breakpoint definitions as a script.\n\
cce7e648 17002This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
17003catchpoints, tracepoints). Use the 'source' command in another debug\n\
17004session to restore them."),
17005 &save_cmdlist);
17006 set_cmd_completer (c, filename_completer);
17007
17008 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 17009Save current tracepoint definitions as a script.\n\
6149aea9
PA
17010Use the 'source' command in another debug session to restore them."),
17011 &save_cmdlist);
1042e4c0
SS
17012 set_cmd_completer (c, filename_completer);
17013
6149aea9
PA
17014 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
17015 deprecate_cmd (c, "save tracepoints");
17016
1bedd215 17017 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
17018Breakpoint specific settings\n\
17019Configure various breakpoint-specific variables such as\n\
1bedd215 17020pending breakpoint behavior"),
fa8d40ab
JJ
17021 &breakpoint_set_cmdlist, "set breakpoint ",
17022 0/*allow-unknown*/, &setlist);
1bedd215 17023 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
17024Breakpoint specific settings\n\
17025Configure various breakpoint-specific variables such as\n\
1bedd215 17026pending breakpoint behavior"),
fa8d40ab
JJ
17027 &breakpoint_show_cmdlist, "show breakpoint ",
17028 0/*allow-unknown*/, &showlist);
17029
7915a72c
AC
17030 add_setshow_auto_boolean_cmd ("pending", no_class,
17031 &pending_break_support, _("\
17032Set debugger's behavior regarding pending breakpoints."), _("\
17033Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
17034If on, an unrecognized breakpoint location will cause gdb to create a\n\
17035pending breakpoint. If off, an unrecognized breakpoint location results in\n\
17036an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 17037user-query to see if a pending breakpoint should be created."),
2c5b56ce 17038 NULL,
920d2a44 17039 show_pending_break_support,
6e1d7d6c
AC
17040 &breakpoint_set_cmdlist,
17041 &breakpoint_show_cmdlist);
fa8d40ab
JJ
17042
17043 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
17044
17045 add_setshow_boolean_cmd ("auto-hw", no_class,
17046 &automatic_hardware_breakpoints, _("\
17047Set automatic usage of hardware breakpoints."), _("\
17048Show automatic usage of hardware breakpoints."), _("\
17049If set, the debugger will automatically use hardware breakpoints for\n\
17050breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
17051a warning will be emitted for such breakpoints."),
17052 NULL,
17053 show_automatic_hardware_breakpoints,
17054 &breakpoint_set_cmdlist,
17055 &breakpoint_show_cmdlist);
74960c60 17056
a25a5a45
PA
17057 add_setshow_boolean_cmd ("always-inserted", class_support,
17058 &always_inserted_mode, _("\
74960c60
VP
17059Set mode for inserting breakpoints."), _("\
17060Show mode for inserting breakpoints."), _("\
a25a5a45
PA
17061When this mode is on, breakpoints are inserted immediately as soon as\n\
17062they're created, kept inserted even when execution stops, and removed\n\
17063only when the user deletes them. When this mode is off (the default),\n\
17064breakpoints are inserted only when execution continues, and removed\n\
17065when execution stops."),
72d0e2c5
YQ
17066 NULL,
17067 &show_always_inserted_mode,
17068 &breakpoint_set_cmdlist,
17069 &breakpoint_show_cmdlist);
f1310107 17070
b775012e
LM
17071 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
17072 condition_evaluation_enums,
17073 &condition_evaluation_mode_1, _("\
17074Set mode of breakpoint condition evaluation."), _("\
17075Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 17076When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
17077evaluated on the host's side by GDB. When it is set to \"target\",\n\
17078breakpoint conditions will be downloaded to the target (if the target\n\
17079supports such feature) and conditions will be evaluated on the target's side.\n\
17080If this is set to \"auto\" (default), this will be automatically set to\n\
17081\"target\" if it supports condition evaluation, otherwise it will\n\
17082be set to \"gdb\""),
17083 &set_condition_evaluation_mode,
17084 &show_condition_evaluation_mode,
17085 &breakpoint_set_cmdlist,
17086 &breakpoint_show_cmdlist);
17087
f1310107
TJB
17088 add_com ("break-range", class_breakpoint, break_range_command, _("\
17089Set a breakpoint for an address range.\n\
17090break-range START-LOCATION, END-LOCATION\n\
17091where START-LOCATION and END-LOCATION can be one of the following:\n\
17092 LINENUM, for that line in the current file,\n\
17093 FILE:LINENUM, for that line in that file,\n\
17094 +OFFSET, for that number of lines after the current line\n\
17095 or the start of the range\n\
17096 FUNCTION, for the first line in that function,\n\
17097 FILE:FUNCTION, to distinguish among like-named static functions.\n\
17098 *ADDRESS, for the instruction at that address.\n\
17099\n\
17100The breakpoint will stop execution of the inferior whenever it executes\n\
17101an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
17102range (including START-LOCATION and END-LOCATION)."));
17103
e7e0cddf
SS
17104 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
17105Set a dynamic printf at specified line or function.\n\
17106dprintf location,format string,arg1,arg2,...\n\
17107location may be a line number, function name, or \"*\" and an address.\n\
17108If a line number is specified, break at start of code for that line.\n\
0e4777df 17109If a function is specified, break at start of code for that function."));
e7e0cddf
SS
17110 set_cmd_completer (c, location_completer);
17111
17112 add_setshow_enum_cmd ("dprintf-style", class_support,
17113 dprintf_style_enums, &dprintf_style, _("\
17114Set the style of usage for dynamic printf."), _("\
17115Show the style of usage for dynamic printf."), _("\
17116This setting chooses how GDB will do a dynamic printf.\n\
17117If the value is \"gdb\", then the printing is done by GDB to its own\n\
17118console, as with the \"printf\" command.\n\
17119If the value is \"call\", the print is done by calling a function in your\n\
17120program; by default printf(), but you can choose a different function or\n\
17121output stream by setting dprintf-function and dprintf-channel."),
17122 update_dprintf_commands, NULL,
17123 &setlist, &showlist);
17124
17125 dprintf_function = xstrdup ("printf");
17126 add_setshow_string_cmd ("dprintf-function", class_support,
17127 &dprintf_function, _("\
17128Set the function to use for dynamic printf"), _("\
17129Show the function to use for dynamic printf"), NULL,
17130 update_dprintf_commands, NULL,
17131 &setlist, &showlist);
17132
17133 dprintf_channel = xstrdup ("");
17134 add_setshow_string_cmd ("dprintf-channel", class_support,
17135 &dprintf_channel, _("\
17136Set the channel to use for dynamic printf"), _("\
17137Show the channel to use for dynamic printf"), NULL,
17138 update_dprintf_commands, NULL,
17139 &setlist, &showlist);
17140
d3ce09f5
SS
17141 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
17142 &disconnected_dprintf, _("\
17143Set whether dprintf continues after GDB disconnects."), _("\
17144Show whether dprintf continues after GDB disconnects."), _("\
17145Use this to let dprintf commands continue to hit and produce output\n\
17146even if GDB disconnects or detaches from the target."),
17147 NULL,
17148 NULL,
17149 &setlist, &showlist);
17150
17151 add_com ("agent-printf", class_vars, agent_printf_command, _("\
17152agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
17153(target agent only) This is useful for formatted output in user-defined commands."));
17154
765dc015 17155 automatic_hardware_breakpoints = 1;
f3b1572e
PA
17156
17157 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 17158 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 17159}
This page took 4.08008 seconds and 4 git commands to generate.