thread.c: cleanup breakpoint deletion
[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"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
a96d9b2e 59#include "xml-syscall.h"
65d79d4b 60#include "parser-defs.h"
55aa24fb
SDJ
61#include "gdb_regex.h"
62#include "probe.h"
e9cafbcc 63#include "cli/cli-utils.h"
be34f849 64#include "continuations.h"
1bfeeb0f
JL
65#include "stack.h"
66#include "skip.h"
b775012e 67#include "ax-gdb.h"
e2e4d78b 68#include "dummy-frame.h"
c906108c 69
d3ce09f5
SS
70#include "format.h"
71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
104c1213 81
e7e8980f
YQ
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
591f19e8 86 EX_EVENT_RETHROW,
e7e8980f
YQ
87 EX_EVENT_CATCH
88};
89
4a64f543 90/* Prototypes for local functions. */
c906108c 91
a14ed312 92static void enable_delete_command (char *, int);
c906108c 93
a14ed312 94static void enable_once_command (char *, int);
c906108c 95
816338b5
SS
96static void enable_count_command (char *, int);
97
a14ed312 98static void disable_command (char *, int);
c906108c 99
a14ed312 100static void enable_command (char *, int);
c906108c 101
95a42b64
TT
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
c906108c 105
a14ed312 106static void ignore_command (char *, int);
c906108c 107
4efb68b1 108static int breakpoint_re_set_one (void *);
c906108c 109
348d480f
PA
110static void breakpoint_re_set_default (struct breakpoint *);
111
983af33b
SDJ
112static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
e7e0cddf 119 char *, char *, enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b
SDJ
124
125static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
a14ed312 128static void clear_command (char *, int);
c906108c 129
a14ed312 130static void catch_command (char *, int);
c906108c 131
a9634178 132static int can_use_hardware_watchpoint (struct value *);
c906108c 133
98deb0da 134static void break_command_1 (char *, int, int);
c906108c 135
a14ed312 136static void mention (struct breakpoint *);
c906108c 137
348d480f
PA
138static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
c0a91b2b 140 const struct breakpoint_ops *);
3742cc8b
YQ
141static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
4a64f543
MS
144/* This function is used in gdbtk sources and thus can not be made
145 static. */
63c252f8 146struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 147 struct symtab_and_line,
c0a91b2b
TT
148 enum bptype,
149 const struct breakpoint_ops *);
c906108c 150
06edf0c0
PA
151static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
a1aa2221
LM
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
06edf0c0 156
76897487
KB
157static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
a6d9a66e
UW
159static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
88f7da05 161 enum bptype bptype);
76897487 162
6c95b8df
PA
163static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
5af949e3 165 struct obj_section *, int);
c906108c 166
85d721b8
PA
167static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
f1310107
TJB
170static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
a14ed312 174static void breakpoints_info (char *, int);
c906108c 175
d77f58be
SS
176static void watchpoints_info (char *, int);
177
e5a67952
MS
178static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
c906108c 180
4efb68b1 181static int breakpoint_cond_eval (void *);
c906108c 182
4efb68b1 183static void cleanup_executing_breakpoints (void *);
c906108c 184
a14ed312 185static void commands_command (char *, int);
c906108c 186
a14ed312 187static void condition_command (char *, int);
c906108c 188
c5aa993b
JM
189typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194insertion_state_t;
c906108c 195
0bde7532 196static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 197static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 198
e514a9d6 199static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 200
4efb68b1 201static int watchpoint_check (void *);
c906108c 202
a14ed312 203static void maintenance_info_breakpoints (char *, int);
c906108c 204
a14ed312 205static int hw_breakpoint_used_count (void);
c906108c 206
a1398e0c
PA
207static int hw_watchpoint_use_count (struct breakpoint *);
208
209static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
c906108c 212
a14ed312 213static void hbreak_command (char *, int);
c906108c 214
a14ed312 215static void thbreak_command (char *, int);
c906108c 216
816338b5
SS
217static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
c906108c 219
a14ed312 220static void stop_command (char *arg, int from_tty);
7a292a7a 221
a14ed312 222static void stopin_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopat_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void tcatch_command (char *arg, int from_tty);
7a292a7a 227
fe3f5fa8 228static void free_bp_location (struct bp_location *loc);
f431efe5
PA
229static void incref_bp_location (struct bp_location *loc);
230static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 231
39d61571 232static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 233
44702360
PA
234/* update_global_location_list's modes of operation wrt to whether to
235 insert locations now. */
236enum ugll_insert_mode
237{
238 /* Don't insert any breakpoint locations into the inferior, only
239 remove already-inserted locations that no longer should be
240 inserted. Functions that delete a breakpoint or breakpoints
241 should specify this mode, so that deleting a breakpoint doesn't
242 have the side effect of inserting the locations of other
243 breakpoints that are marked not-inserted, but should_be_inserted
244 returns true on them.
245
246 This behavior is useful is situations close to tear-down -- e.g.,
247 after an exec, while the target still has execution, but
248 breakpoint shadows of the previous executable image should *NOT*
249 be restored to the new image; or before detaching, where the
250 target still has execution and wants to delete breakpoints from
251 GDB's lists, and all breakpoints had already been removed from
252 the inferior. */
253 UGLL_DONT_INSERT,
254
a25a5a45
PA
255 /* May insert breakpoints iff breakpoints_should_be_inserted_now
256 claims breakpoints should be inserted now. */
04086b45
PA
257 UGLL_MAY_INSERT,
258
a25a5a45
PA
259 /* Insert locations now, irrespective of
260 breakpoints_should_be_inserted_now. E.g., say all threads are
261 stopped right now, and the user did "continue". We need to
262 insert breakpoints _before_ resuming the target, but
263 UGLL_MAY_INSERT wouldn't insert them, because
264 breakpoints_should_be_inserted_now returns false at that point,
265 as no thread is running yet. */
04086b45 266 UGLL_INSERT
44702360
PA
267};
268
269static void update_global_location_list (enum ugll_insert_mode);
a5606eee 270
44702360 271static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 272
d77f58be 273static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
274
275static void insert_breakpoint_locations (void);
a5606eee 276
a96d9b2e
SDJ
277static int syscall_catchpoint_p (struct breakpoint *b);
278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The abstract base class all breakpoint_ops structures inherit
301 from. */
ab04a2af 302struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
303
304/* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309/* Internal breakpoints class type. */
06edf0c0 310static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
311
312/* Momentary breakpoints class type. */
06edf0c0
PA
313static struct breakpoint_ops momentary_breakpoint_ops;
314
e2e4d78b
JK
315/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316static struct breakpoint_ops longjmp_breakpoint_ops;
317
2060206e
PA
318/* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320struct breakpoint_ops bkpt_breakpoint_ops;
321
55aa24fb
SDJ
322/* Breakpoints set on probes. */
323static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
e7e0cddf 325/* Dynamic printf class type. */
c5867ab6 326struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 327
9d497a19
PA
328/* One (or perhaps two) breakpoints used for software single
329 stepping. */
330
7c16b83e 331static struct breakpoint *single_step_breakpoints;
9d497a19 332
d3ce09f5
SS
333/* The style in which to perform a dynamic printf. This is a user
334 option because different output options have different tradeoffs;
335 if GDB does the printing, there is better error handling if there
336 is a problem with any of the arguments, but using an inferior
337 function lets you have special-purpose printers and sending of
338 output to the same place as compiled-in print functions. */
339
340static const char dprintf_style_gdb[] = "gdb";
341static const char dprintf_style_call[] = "call";
342static const char dprintf_style_agent[] = "agent";
343static const char *const dprintf_style_enums[] = {
344 dprintf_style_gdb,
345 dprintf_style_call,
346 dprintf_style_agent,
347 NULL
348};
349static const char *dprintf_style = dprintf_style_gdb;
350
351/* The function to use for dynamic printf if the preferred style is to
352 call into the inferior. The value is simply a string that is
353 copied into the command, so it can be anything that GDB can
354 evaluate to a callable address, not necessarily a function name. */
355
356static char *dprintf_function = "";
357
358/* The channel to use for dynamic printf if the preferred style is to
359 call into the inferior; if a nonempty string, it will be passed to
360 the call as the first argument, with the format string as the
361 second. As with the dprintf function, this can be anything that
362 GDB knows how to evaluate, so in addition to common choices like
363 "stderr", this could be an app-specific expression like
364 "mystreams[curlogger]". */
365
366static char *dprintf_channel = "";
367
368/* True if dprintf commands should continue to operate even if GDB
369 has disconnected. */
370static int disconnected_dprintf = 1;
371
5cea2a26
PA
372/* A reference-counted struct command_line. This lets multiple
373 breakpoints share a single command list. */
374struct counted_command_line
375{
376 /* The reference count. */
377 int refc;
378
379 /* The command list. */
380 struct command_line *commands;
381};
382
383struct command_line *
384breakpoint_commands (struct breakpoint *b)
385{
386 return b->commands ? b->commands->commands : NULL;
387}
3daf8fe5 388
f3b1572e
PA
389/* Flag indicating that a command has proceeded the inferior past the
390 current breakpoint. */
391
392static int breakpoint_proceeded;
393
956a9fb9 394const char *
2cec12e5
AR
395bpdisp_text (enum bpdisp disp)
396{
4a64f543
MS
397 /* NOTE: the following values are a part of MI protocol and
398 represent values of 'disp' field returned when inferior stops at
399 a breakpoint. */
bc043ef3 400 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 401
2cec12e5
AR
402 return bpdisps[(int) disp];
403}
c906108c 404
4a64f543 405/* Prototypes for exported functions. */
c906108c 406/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 407 if such is available. */
c906108c
SS
408static int can_use_hw_watchpoints;
409
920d2a44
AC
410static void
411show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
412 struct cmd_list_element *c,
413 const char *value)
414{
3e43a32a
MS
415 fprintf_filtered (file,
416 _("Debugger's willingness to use "
417 "watchpoint hardware is %s.\n"),
920d2a44
AC
418 value);
419}
420
fa8d40ab
JJ
421/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
422 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 423 for unrecognized breakpoint locations.
fa8d40ab
JJ
424 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
425static enum auto_boolean pending_break_support;
920d2a44
AC
426static void
427show_pending_break_support (struct ui_file *file, int from_tty,
428 struct cmd_list_element *c,
429 const char *value)
430{
3e43a32a
MS
431 fprintf_filtered (file,
432 _("Debugger's behavior regarding "
433 "pending breakpoints is %s.\n"),
920d2a44
AC
434 value);
435}
fa8d40ab 436
765dc015 437/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 438 set with "break" but falling in read-only memory.
765dc015
VP
439 If 0, gdb will warn about such breakpoints, but won't automatically
440 use hardware breakpoints. */
441static int automatic_hardware_breakpoints;
442static void
443show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
444 struct cmd_list_element *c,
445 const char *value)
446{
3e43a32a
MS
447 fprintf_filtered (file,
448 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
449 value);
450}
451
a25a5a45
PA
452/* If on, GDB keeps breakpoints inserted even if the inferior is
453 stopped, and immediately inserts any new breakpoints as soon as
454 they're created. If off (default), GDB keeps breakpoints off of
455 the target as long as possible. That is, it delays inserting
456 breakpoints until the next resume, and removes them again when the
457 target fully stops. This is a bit safer in case GDB crashes while
458 processing user input. */
459static int always_inserted_mode = 0;
72d0e2c5 460
33e5cbd6 461static void
74960c60 462show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 463 struct cmd_list_element *c, const char *value)
74960c60 464{
a25a5a45
PA
465 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
466 value);
74960c60
VP
467}
468
b57bacec
PA
469/* See breakpoint.h. */
470
33e5cbd6 471int
a25a5a45 472breakpoints_should_be_inserted_now (void)
33e5cbd6 473{
a25a5a45
PA
474 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
475 {
476 /* If breakpoints are global, they should be inserted even if no
477 thread under gdb's control is running, or even if there are
478 no threads under GDB's control yet. */
479 return 1;
480 }
481 else if (target_has_execution)
482 {
a25a5a45
PA
483 if (always_inserted_mode)
484 {
485 /* The user wants breakpoints inserted even if all threads
486 are stopped. */
487 return 1;
488 }
489
b57bacec
PA
490 if (threads_are_executing ())
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593
JK
580#define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_location; \
582 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_location + bp_location_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
876fa593
JK
609/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
610
611static struct bp_location **bp_location;
612
613/* Number of elements of BP_LOCATION. */
614
615static unsigned bp_location_count;
616
4a64f543
MS
617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
876fa593
JK
622
623static CORE_ADDR bp_location_placed_address_before_address_max;
624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
876fa593
JK
630
631static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 632
4a64f543
MS
633/* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
468d015d
JJ
656/* Return whether a breakpoint is an active enabled breakpoint. */
657static int
658breakpoint_enabled (struct breakpoint *b)
659{
0d381245 660 return (b->enable_state == bp_enabled);
468d015d
JJ
661}
662
c906108c
SS
663/* Set breakpoint count to NUM. */
664
95a42b64 665static void
fba45db2 666set_breakpoint_count (int num)
c906108c 667{
86b17b60 668 prev_breakpoint_count = breakpoint_count;
c906108c 669 breakpoint_count = num;
4fa62494 670 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
671}
672
86b17b60
PA
673/* Used by `start_rbreak_breakpoints' below, to record the current
674 breakpoint count before "rbreak" creates any breakpoint. */
675static int rbreak_start_breakpoint_count;
676
95a42b64
TT
677/* Called at the start an "rbreak" command to record the first
678 breakpoint made. */
86b17b60 679
95a42b64
TT
680void
681start_rbreak_breakpoints (void)
682{
86b17b60 683 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
684}
685
686/* Called at the end of an "rbreak" command to record the last
687 breakpoint made. */
86b17b60 688
95a42b64
TT
689void
690end_rbreak_breakpoints (void)
691{
86b17b60 692 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
693}
694
4a64f543 695/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
696
697void
fba45db2 698clear_breakpoint_hit_counts (void)
c906108c
SS
699{
700 struct breakpoint *b;
701
702 ALL_BREAKPOINTS (b)
703 b->hit_count = 0;
704}
705
9add0f1b
TT
706/* Allocate a new counted_command_line with reference count of 1.
707 The new structure owns COMMANDS. */
708
709static struct counted_command_line *
710alloc_counted_command_line (struct command_line *commands)
711{
712 struct counted_command_line *result
713 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 714
9add0f1b
TT
715 result->refc = 1;
716 result->commands = commands;
717 return result;
718}
719
720/* Increment reference count. This does nothing if CMD is NULL. */
721
722static void
723incref_counted_command_line (struct counted_command_line *cmd)
724{
725 if (cmd)
726 ++cmd->refc;
727}
728
729/* Decrement reference count. If the reference count reaches 0,
730 destroy the counted_command_line. Sets *CMDP to NULL. This does
731 nothing if *CMDP is NULL. */
732
733static void
734decref_counted_command_line (struct counted_command_line **cmdp)
735{
736 if (*cmdp)
737 {
738 if (--(*cmdp)->refc == 0)
739 {
740 free_command_lines (&(*cmdp)->commands);
741 xfree (*cmdp);
742 }
743 *cmdp = NULL;
744 }
745}
746
747/* A cleanup function that calls decref_counted_command_line. */
748
749static void
750do_cleanup_counted_command_line (void *arg)
751{
752 decref_counted_command_line (arg);
753}
754
755/* Create a cleanup that calls decref_counted_command_line on the
756 argument. */
757
758static struct cleanup *
759make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
760{
761 return make_cleanup (do_cleanup_counted_command_line, cmdp);
762}
763
c906108c 764\f
48cb2d85
VP
765/* Return the breakpoint with the specified number, or NULL
766 if the number does not refer to an existing breakpoint. */
767
768struct breakpoint *
769get_breakpoint (int num)
770{
771 struct breakpoint *b;
772
773 ALL_BREAKPOINTS (b)
774 if (b->number == num)
775 return b;
776
777 return NULL;
778}
5c44784c 779
c906108c 780\f
adc36818 781
b775012e
LM
782/* Mark locations as "conditions have changed" in case the target supports
783 evaluating conditions on its side. */
784
785static void
786mark_breakpoint_modified (struct breakpoint *b)
787{
788 struct bp_location *loc;
789
790 /* This is only meaningful if the target is
791 evaluating conditions and if the user has
792 opted for condition evaluation on the target's
793 side. */
794 if (gdb_evaluates_breakpoint_condition_p ()
795 || !target_supports_evaluation_of_breakpoint_conditions ())
796 return;
797
798 if (!is_breakpoint (b))
799 return;
800
801 for (loc = b->loc; loc; loc = loc->next)
802 loc->condition_changed = condition_modified;
803}
804
805/* Mark location as "conditions have changed" in case the target supports
806 evaluating conditions on its side. */
807
808static void
809mark_breakpoint_location_modified (struct bp_location *loc)
810{
811 /* This is only meaningful if the target is
812 evaluating conditions and if the user has
813 opted for condition evaluation on the target's
814 side. */
815 if (gdb_evaluates_breakpoint_condition_p ()
816 || !target_supports_evaluation_of_breakpoint_conditions ())
817
818 return;
819
820 if (!is_breakpoint (loc->owner))
821 return;
822
823 loc->condition_changed = condition_modified;
824}
825
826/* Sets the condition-evaluation mode using the static global
827 condition_evaluation_mode. */
828
829static void
830set_condition_evaluation_mode (char *args, int from_tty,
831 struct cmd_list_element *c)
832{
b775012e
LM
833 const char *old_mode, *new_mode;
834
835 if ((condition_evaluation_mode_1 == condition_evaluation_target)
836 && !target_supports_evaluation_of_breakpoint_conditions ())
837 {
838 condition_evaluation_mode_1 = condition_evaluation_mode;
839 warning (_("Target does not support breakpoint condition evaluation.\n"
840 "Using host evaluation mode instead."));
841 return;
842 }
843
844 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
845 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
846
abf1152a
JK
847 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
848 settings was "auto". */
849 condition_evaluation_mode = condition_evaluation_mode_1;
850
b775012e
LM
851 /* Only update the mode if the user picked a different one. */
852 if (new_mode != old_mode)
853 {
854 struct bp_location *loc, **loc_tmp;
855 /* If the user switched to a different evaluation mode, we
856 need to synch the changes with the target as follows:
857
858 "host" -> "target": Send all (valid) conditions to the target.
859 "target" -> "host": Remove all the conditions from the target.
860 */
861
b775012e
LM
862 if (new_mode == condition_evaluation_target)
863 {
864 /* Mark everything modified and synch conditions with the
865 target. */
866 ALL_BP_LOCATIONS (loc, loc_tmp)
867 mark_breakpoint_location_modified (loc);
868 }
869 else
870 {
871 /* Manually mark non-duplicate locations to synch conditions
872 with the target. We do this to remove all the conditions the
873 target knows about. */
874 ALL_BP_LOCATIONS (loc, loc_tmp)
875 if (is_breakpoint (loc->owner) && loc->inserted)
876 loc->needs_update = 1;
877 }
878
879 /* Do the update. */
44702360 880 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
881 }
882
883 return;
884}
885
886/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
887 what "auto" is translating to. */
888
889static void
890show_condition_evaluation_mode (struct ui_file *file, int from_tty,
891 struct cmd_list_element *c, const char *value)
892{
893 if (condition_evaluation_mode == condition_evaluation_auto)
894 fprintf_filtered (file,
895 _("Breakpoint condition evaluation "
896 "mode is %s (currently %s).\n"),
897 value,
898 breakpoint_condition_evaluation_mode ());
899 else
900 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
901 value);
902}
903
904/* A comparison function for bp_location AP and BP that is used by
905 bsearch. This comparison function only cares about addresses, unlike
906 the more general bp_location_compare function. */
907
908static int
909bp_location_compare_addrs (const void *ap, const void *bp)
910{
911 struct bp_location *a = *(void **) ap;
912 struct bp_location *b = *(void **) bp;
913
914 if (a->address == b->address)
915 return 0;
916 else
917 return ((a->address > b->address) - (a->address < b->address));
918}
919
920/* Helper function to skip all bp_locations with addresses
921 less than ADDRESS. It returns the first bp_location that
922 is greater than or equal to ADDRESS. If none is found, just
923 return NULL. */
924
925static struct bp_location **
926get_first_locp_gte_addr (CORE_ADDR address)
927{
928 struct bp_location dummy_loc;
929 struct bp_location *dummy_locp = &dummy_loc;
930 struct bp_location **locp_found = NULL;
931
932 /* Initialize the dummy location's address field. */
933 memset (&dummy_loc, 0, sizeof (struct bp_location));
934 dummy_loc.address = address;
935
936 /* Find a close match to the first location at ADDRESS. */
937 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
938 sizeof (struct bp_location **),
939 bp_location_compare_addrs);
940
941 /* Nothing was found, nothing left to do. */
942 if (locp_found == NULL)
943 return NULL;
944
945 /* We may have found a location that is at ADDRESS but is not the first in the
946 location's list. Go backwards (if possible) and locate the first one. */
947 while ((locp_found - 1) >= bp_location
948 && (*(locp_found - 1))->address == address)
949 locp_found--;
950
951 return locp_found;
952}
953
adc36818
PM
954void
955set_breakpoint_condition (struct breakpoint *b, char *exp,
956 int from_tty)
957{
3a5c3e22
PA
958 xfree (b->cond_string);
959 b->cond_string = NULL;
adc36818 960
3a5c3e22 961 if (is_watchpoint (b))
adc36818 962 {
3a5c3e22
PA
963 struct watchpoint *w = (struct watchpoint *) b;
964
965 xfree (w->cond_exp);
966 w->cond_exp = NULL;
967 }
968 else
969 {
970 struct bp_location *loc;
971
972 for (loc = b->loc; loc; loc = loc->next)
973 {
974 xfree (loc->cond);
975 loc->cond = NULL;
b775012e
LM
976
977 /* No need to free the condition agent expression
978 bytecode (if we have one). We will handle this
979 when we go through update_global_location_list. */
3a5c3e22 980 }
adc36818 981 }
adc36818
PM
982
983 if (*exp == 0)
984 {
985 if (from_tty)
986 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
987 }
988 else
989 {
bbc13ae3 990 const char *arg = exp;
cc59ec59 991
adc36818
PM
992 /* I don't know if it matters whether this is the string the user
993 typed in or the decompiled expression. */
994 b->cond_string = xstrdup (arg);
995 b->condition_not_parsed = 0;
996
997 if (is_watchpoint (b))
998 {
3a5c3e22
PA
999 struct watchpoint *w = (struct watchpoint *) b;
1000
adc36818
PM
1001 innermost_block = NULL;
1002 arg = exp;
1bb9788d 1003 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1004 if (*arg)
1005 error (_("Junk at end of expression"));
3a5c3e22 1006 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1007 }
1008 else
1009 {
3a5c3e22
PA
1010 struct bp_location *loc;
1011
adc36818
PM
1012 for (loc = b->loc; loc; loc = loc->next)
1013 {
1014 arg = exp;
1015 loc->cond =
1bb9788d
TT
1016 parse_exp_1 (&arg, loc->address,
1017 block_for_pc (loc->address), 0);
adc36818
PM
1018 if (*arg)
1019 error (_("Junk at end of expression"));
1020 }
1021 }
1022 }
b775012e
LM
1023 mark_breakpoint_modified (b);
1024
8d3788bd 1025 observer_notify_breakpoint_modified (b);
adc36818
PM
1026}
1027
d55637df
TT
1028/* Completion for the "condition" command. */
1029
1030static VEC (char_ptr) *
6f937416
PA
1031condition_completer (struct cmd_list_element *cmd,
1032 const char *text, const char *word)
d55637df 1033{
6f937416 1034 const char *space;
d55637df 1035
6f937416
PA
1036 text = skip_spaces_const (text);
1037 space = skip_to_space_const (text);
d55637df
TT
1038 if (*space == '\0')
1039 {
1040 int len;
1041 struct breakpoint *b;
1042 VEC (char_ptr) *result = NULL;
1043
1044 if (text[0] == '$')
1045 {
1046 /* We don't support completion of history indices. */
1047 if (isdigit (text[1]))
1048 return NULL;
1049 return complete_internalvar (&text[1]);
1050 }
1051
1052 /* We're completing the breakpoint number. */
1053 len = strlen (text);
1054
1055 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1056 {
1057 char number[50];
1058
1059 xsnprintf (number, sizeof (number), "%d", b->number);
1060
1061 if (strncmp (number, text, len) == 0)
1062 VEC_safe_push (char_ptr, result, xstrdup (number));
1063 }
d55637df
TT
1064
1065 return result;
1066 }
1067
1068 /* We're completing the expression part. */
6f937416 1069 text = skip_spaces_const (space);
d55637df
TT
1070 return expression_completer (cmd, text, word);
1071}
1072
c906108c
SS
1073/* condition N EXP -- set break condition of breakpoint N to EXP. */
1074
1075static void
fba45db2 1076condition_command (char *arg, int from_tty)
c906108c 1077{
52f0bd74 1078 struct breakpoint *b;
c906108c 1079 char *p;
52f0bd74 1080 int bnum;
c906108c
SS
1081
1082 if (arg == 0)
e2e0b3e5 1083 error_no_arg (_("breakpoint number"));
c906108c
SS
1084
1085 p = arg;
1086 bnum = get_number (&p);
5c44784c 1087 if (bnum == 0)
8a3fe4f8 1088 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1089
1090 ALL_BREAKPOINTS (b)
1091 if (b->number == bnum)
2f069f6f 1092 {
6dddc817
DE
1093 /* Check if this breakpoint has a "stop" method implemented in an
1094 extension language. This method and conditions entered into GDB
1095 from the CLI are mutually exclusive. */
1096 const struct extension_language_defn *extlang
1097 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1098
1099 if (extlang != NULL)
1100 {
1101 error (_("Only one stop condition allowed. There is currently"
1102 " a %s stop condition defined for this breakpoint."),
1103 ext_lang_capitalized_name (extlang));
1104 }
2566ad2d 1105 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1106
1107 if (is_breakpoint (b))
44702360 1108 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1109
2f069f6f
JB
1110 return;
1111 }
c906108c 1112
8a3fe4f8 1113 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1114}
1115
a7bdde9e
VP
1116/* Check that COMMAND do not contain commands that are suitable
1117 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1118 Throw if any such commands is found. */
1119
a7bdde9e
VP
1120static void
1121check_no_tracepoint_commands (struct command_line *commands)
1122{
1123 struct command_line *c;
cc59ec59 1124
a7bdde9e
VP
1125 for (c = commands; c; c = c->next)
1126 {
1127 int i;
1128
1129 if (c->control_type == while_stepping_control)
3e43a32a
MS
1130 error (_("The 'while-stepping' command can "
1131 "only be used for tracepoints"));
a7bdde9e
VP
1132
1133 for (i = 0; i < c->body_count; ++i)
1134 check_no_tracepoint_commands ((c->body_list)[i]);
1135
1136 /* Not that command parsing removes leading whitespace and comment
4a64f543 1137 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1138 command directly. */
1139 if (strstr (c->line, "collect ") == c->line)
1140 error (_("The 'collect' command can only be used for tracepoints"));
1141
51661e93
VP
1142 if (strstr (c->line, "teval ") == c->line)
1143 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1144 }
1145}
1146
d77f58be
SS
1147/* Encapsulate tests for different types of tracepoints. */
1148
d9b3f62e
PA
1149static int
1150is_tracepoint_type (enum bptype type)
1151{
1152 return (type == bp_tracepoint
1153 || type == bp_fast_tracepoint
1154 || type == bp_static_tracepoint);
1155}
1156
a7bdde9e 1157int
d77f58be 1158is_tracepoint (const struct breakpoint *b)
a7bdde9e 1159{
d9b3f62e 1160 return is_tracepoint_type (b->type);
a7bdde9e 1161}
d9b3f62e 1162
e5dd4106 1163/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1164 breakpoint. This function will throw an exception if a problem is
1165 found. */
48cb2d85 1166
95a42b64
TT
1167static void
1168validate_commands_for_breakpoint (struct breakpoint *b,
1169 struct command_line *commands)
48cb2d85 1170{
d77f58be 1171 if (is_tracepoint (b))
a7bdde9e 1172 {
c9a6ce02 1173 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1174 struct command_line *c;
1175 struct command_line *while_stepping = 0;
c9a6ce02
PA
1176
1177 /* Reset the while-stepping step count. The previous commands
1178 might have included a while-stepping action, while the new
1179 ones might not. */
1180 t->step_count = 0;
1181
1182 /* We need to verify that each top-level element of commands is
1183 valid for tracepoints, that there's at most one
1184 while-stepping element, and that the while-stepping's body
1185 has valid tracing commands excluding nested while-stepping.
1186 We also need to validate the tracepoint action line in the
1187 context of the tracepoint --- validate_actionline actually
1188 has side effects, like setting the tracepoint's
1189 while-stepping STEP_COUNT, in addition to checking if the
1190 collect/teval actions parse and make sense in the
1191 tracepoint's context. */
a7bdde9e
VP
1192 for (c = commands; c; c = c->next)
1193 {
a7bdde9e
VP
1194 if (c->control_type == while_stepping_control)
1195 {
1196 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1197 error (_("The 'while-stepping' command "
1198 "cannot be used for fast tracepoint"));
0fb4aa4b 1199 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1200 error (_("The 'while-stepping' command "
1201 "cannot be used for static tracepoint"));
a7bdde9e
VP
1202
1203 if (while_stepping)
3e43a32a
MS
1204 error (_("The 'while-stepping' command "
1205 "can be used only once"));
a7bdde9e
VP
1206 else
1207 while_stepping = c;
1208 }
c9a6ce02
PA
1209
1210 validate_actionline (c->line, b);
a7bdde9e
VP
1211 }
1212 if (while_stepping)
1213 {
1214 struct command_line *c2;
1215
1216 gdb_assert (while_stepping->body_count == 1);
1217 c2 = while_stepping->body_list[0];
1218 for (; c2; c2 = c2->next)
1219 {
a7bdde9e
VP
1220 if (c2->control_type == while_stepping_control)
1221 error (_("The 'while-stepping' command cannot be nested"));
1222 }
1223 }
1224 }
1225 else
1226 {
1227 check_no_tracepoint_commands (commands);
1228 }
95a42b64
TT
1229}
1230
0fb4aa4b
PA
1231/* Return a vector of all the static tracepoints set at ADDR. The
1232 caller is responsible for releasing the vector. */
1233
1234VEC(breakpoint_p) *
1235static_tracepoints_here (CORE_ADDR addr)
1236{
1237 struct breakpoint *b;
1238 VEC(breakpoint_p) *found = 0;
1239 struct bp_location *loc;
1240
1241 ALL_BREAKPOINTS (b)
1242 if (b->type == bp_static_tracepoint)
1243 {
1244 for (loc = b->loc; loc; loc = loc->next)
1245 if (loc->address == addr)
1246 VEC_safe_push(breakpoint_p, found, b);
1247 }
1248
1249 return found;
1250}
1251
95a42b64 1252/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1253 validate that only allowed commands are included. */
95a42b64
TT
1254
1255void
4a64f543
MS
1256breakpoint_set_commands (struct breakpoint *b,
1257 struct command_line *commands)
95a42b64
TT
1258{
1259 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1260
9add0f1b
TT
1261 decref_counted_command_line (&b->commands);
1262 b->commands = alloc_counted_command_line (commands);
8d3788bd 1263 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1264}
1265
45a43567
TT
1266/* Set the internal `silent' flag on the breakpoint. Note that this
1267 is not the same as the "silent" that may appear in the breakpoint's
1268 commands. */
1269
1270void
1271breakpoint_set_silent (struct breakpoint *b, int silent)
1272{
1273 int old_silent = b->silent;
1274
1275 b->silent = silent;
1276 if (old_silent != silent)
8d3788bd 1277 observer_notify_breakpoint_modified (b);
45a43567
TT
1278}
1279
1280/* Set the thread for this breakpoint. If THREAD is -1, make the
1281 breakpoint work for any thread. */
1282
1283void
1284breakpoint_set_thread (struct breakpoint *b, int thread)
1285{
1286 int old_thread = b->thread;
1287
1288 b->thread = thread;
1289 if (old_thread != thread)
8d3788bd 1290 observer_notify_breakpoint_modified (b);
45a43567
TT
1291}
1292
1293/* Set the task for this breakpoint. If TASK is 0, make the
1294 breakpoint work for any task. */
1295
1296void
1297breakpoint_set_task (struct breakpoint *b, int task)
1298{
1299 int old_task = b->task;
1300
1301 b->task = task;
1302 if (old_task != task)
8d3788bd 1303 observer_notify_breakpoint_modified (b);
45a43567
TT
1304}
1305
95a42b64
TT
1306void
1307check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1308{
1309 struct breakpoint *b = closure;
cc59ec59 1310
6f937416 1311 validate_actionline (line, b);
a7bdde9e
VP
1312}
1313
95a42b64
TT
1314/* A structure used to pass information through
1315 map_breakpoint_numbers. */
1316
1317struct commands_info
1318{
1319 /* True if the command was typed at a tty. */
1320 int from_tty;
86b17b60
PA
1321
1322 /* The breakpoint range spec. */
1323 char *arg;
1324
95a42b64
TT
1325 /* Non-NULL if the body of the commands are being read from this
1326 already-parsed command. */
1327 struct command_line *control;
86b17b60 1328
95a42b64
TT
1329 /* The command lines read from the user, or NULL if they have not
1330 yet been read. */
1331 struct counted_command_line *cmd;
1332};
1333
1334/* A callback for map_breakpoint_numbers that sets the commands for
1335 commands_command. */
1336
c906108c 1337static void
95a42b64 1338do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1339{
95a42b64 1340 struct commands_info *info = data;
c906108c 1341
95a42b64
TT
1342 if (info->cmd == NULL)
1343 {
1344 struct command_line *l;
5c44784c 1345
95a42b64
TT
1346 if (info->control != NULL)
1347 l = copy_command_lines (info->control->body_list[0]);
1348 else
86b17b60
PA
1349 {
1350 struct cleanup *old_chain;
1351 char *str;
c5aa993b 1352
3e43a32a
MS
1353 str = xstrprintf (_("Type commands for breakpoint(s) "
1354 "%s, one per line."),
86b17b60
PA
1355 info->arg);
1356
1357 old_chain = make_cleanup (xfree, str);
1358
1359 l = read_command_lines (str,
1360 info->from_tty, 1,
d77f58be 1361 (is_tracepoint (b)
86b17b60
PA
1362 ? check_tracepoint_command : 0),
1363 b);
1364
1365 do_cleanups (old_chain);
1366 }
a7bdde9e 1367
95a42b64
TT
1368 info->cmd = alloc_counted_command_line (l);
1369 }
1370
1371 /* If a breakpoint was on the list more than once, we don't need to
1372 do anything. */
1373 if (b->commands != info->cmd)
1374 {
1375 validate_commands_for_breakpoint (b, info->cmd->commands);
1376 incref_counted_command_line (info->cmd);
1377 decref_counted_command_line (&b->commands);
1378 b->commands = info->cmd;
8d3788bd 1379 observer_notify_breakpoint_modified (b);
c5aa993b 1380 }
95a42b64
TT
1381}
1382
1383static void
4a64f543
MS
1384commands_command_1 (char *arg, int from_tty,
1385 struct command_line *control)
95a42b64
TT
1386{
1387 struct cleanup *cleanups;
1388 struct commands_info info;
1389
1390 info.from_tty = from_tty;
1391 info.control = control;
1392 info.cmd = NULL;
1393 /* If we read command lines from the user, then `info' will hold an
1394 extra reference to the commands that we must clean up. */
1395 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1396
1397 if (arg == NULL || !*arg)
1398 {
86b17b60 1399 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1400 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1401 breakpoint_count);
95a42b64
TT
1402 else if (breakpoint_count > 0)
1403 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1404 else
1405 {
1406 /* So that we don't try to free the incoming non-NULL
1407 argument in the cleanup below. Mapping breakpoint
1408 numbers will fail in this case. */
1409 arg = NULL;
1410 }
95a42b64 1411 }
9766ced4
SS
1412 else
1413 /* The command loop has some static state, so we need to preserve
1414 our argument. */
1415 arg = xstrdup (arg);
86b17b60
PA
1416
1417 if (arg != NULL)
1418 make_cleanup (xfree, arg);
1419
1420 info.arg = arg;
95a42b64
TT
1421
1422 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1423
1424 if (info.cmd == NULL)
1425 error (_("No breakpoints specified."));
1426
1427 do_cleanups (cleanups);
1428}
1429
1430static void
1431commands_command (char *arg, int from_tty)
1432{
1433 commands_command_1 (arg, from_tty, NULL);
c906108c 1434}
40c03ae8
EZ
1435
1436/* Like commands_command, but instead of reading the commands from
1437 input stream, takes them from an already parsed command structure.
1438
1439 This is used by cli-script.c to DTRT with breakpoint commands
1440 that are part of if and while bodies. */
1441enum command_control_type
1442commands_from_control_command (char *arg, struct command_line *cmd)
1443{
95a42b64
TT
1444 commands_command_1 (arg, 0, cmd);
1445 return simple_control;
40c03ae8 1446}
876fa593
JK
1447
1448/* Return non-zero if BL->TARGET_INFO contains valid information. */
1449
1450static int
1451bp_location_has_shadow (struct bp_location *bl)
1452{
1453 if (bl->loc_type != bp_loc_software_breakpoint)
1454 return 0;
1455 if (!bl->inserted)
1456 return 0;
1457 if (bl->target_info.shadow_len == 0)
e5dd4106 1458 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1459 return 0;
1460 return 1;
1461}
1462
9d497a19
PA
1463/* Update BUF, which is LEN bytes read from the target address
1464 MEMADDR, by replacing a memory breakpoint with its shadowed
1465 contents.
1466
1467 If READBUF is not NULL, this buffer must not overlap with the of
1468 the breakpoint location's shadow_contents buffer. Otherwise, a
1469 failed assertion internal error will be raised. */
1470
1471static void
1472one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1473 const gdb_byte *writebuf_org,
1474 ULONGEST memaddr, LONGEST len,
1475 struct bp_target_info *target_info,
1476 struct gdbarch *gdbarch)
1477{
1478 /* Now do full processing of the found relevant range of elements. */
1479 CORE_ADDR bp_addr = 0;
1480 int bp_size = 0;
1481 int bptoffset = 0;
1482
1483 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1484 current_program_space->aspace, 0))
1485 {
1486 /* The breakpoint is inserted in a different address space. */
1487 return;
1488 }
1489
1490 /* Addresses and length of the part of the breakpoint that
1491 we need to copy. */
1492 bp_addr = target_info->placed_address;
1493 bp_size = target_info->shadow_len;
1494
1495 if (bp_addr + bp_size <= memaddr)
1496 {
1497 /* The breakpoint is entirely before the chunk of memory we are
1498 reading. */
1499 return;
1500 }
1501
1502 if (bp_addr >= memaddr + len)
1503 {
1504 /* The breakpoint is entirely after the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 /* Offset within shadow_contents. */
1510 if (bp_addr < memaddr)
1511 {
1512 /* Only copy the second part of the breakpoint. */
1513 bp_size -= memaddr - bp_addr;
1514 bptoffset = memaddr - bp_addr;
1515 bp_addr = memaddr;
1516 }
1517
1518 if (bp_addr + bp_size > memaddr + len)
1519 {
1520 /* Only copy the first part of the breakpoint. */
1521 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1522 }
1523
1524 if (readbuf != NULL)
1525 {
1526 /* Verify that the readbuf buffer does not overlap with the
1527 shadow_contents buffer. */
1528 gdb_assert (target_info->shadow_contents >= readbuf + len
1529 || readbuf >= (target_info->shadow_contents
1530 + target_info->shadow_len));
1531
1532 /* Update the read buffer with this inserted breakpoint's
1533 shadow. */
1534 memcpy (readbuf + bp_addr - memaddr,
1535 target_info->shadow_contents + bptoffset, bp_size);
1536 }
1537 else
1538 {
1539 const unsigned char *bp;
0d5ed153
MR
1540 CORE_ADDR addr = target_info->reqstd_address;
1541 int placed_size;
9d497a19
PA
1542
1543 /* Update the shadow with what we want to write to memory. */
1544 memcpy (target_info->shadow_contents + bptoffset,
1545 writebuf_org + bp_addr - memaddr, bp_size);
1546
1547 /* Determine appropriate breakpoint contents and size for this
1548 address. */
0d5ed153 1549 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1550
1551 /* Update the final write buffer with this inserted
1552 breakpoint's INSN. */
1553 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1554 }
1555}
1556
8defab1a 1557/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1558 by replacing any memory breakpoints with their shadowed contents.
1559
35c63cd8
JB
1560 If READBUF is not NULL, this buffer must not overlap with any of
1561 the breakpoint location's shadow_contents buffers. Otherwise,
1562 a failed assertion internal error will be raised.
1563
876fa593 1564 The range of shadowed area by each bp_location is:
35df4500
TJB
1565 bl->address - bp_location_placed_address_before_address_max
1566 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1567 The range we were requested to resolve shadows for is:
1568 memaddr ... memaddr + len
1569 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1570 memaddr + len <= (bl->address
1571 - bp_location_placed_address_before_address_max)
876fa593 1572 and:
35df4500 1573 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1574
8defab1a 1575void
f0ba3972
PA
1576breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1577 const gdb_byte *writebuf_org,
1578 ULONGEST memaddr, LONGEST len)
c906108c 1579{
4a64f543
MS
1580 /* Left boundary, right boundary and median element of our binary
1581 search. */
876fa593 1582 unsigned bc_l, bc_r, bc;
9d497a19 1583 size_t i;
876fa593 1584
4a64f543
MS
1585 /* Find BC_L which is a leftmost element which may affect BUF
1586 content. It is safe to report lower value but a failure to
1587 report higher one. */
876fa593
JK
1588
1589 bc_l = 0;
1590 bc_r = bp_location_count;
1591 while (bc_l + 1 < bc_r)
1592 {
35df4500 1593 struct bp_location *bl;
876fa593
JK
1594
1595 bc = (bc_l + bc_r) / 2;
35df4500 1596 bl = bp_location[bc];
876fa593 1597
4a64f543
MS
1598 /* Check first BL->ADDRESS will not overflow due to the added
1599 constant. Then advance the left boundary only if we are sure
1600 the BC element can in no way affect the BUF content (MEMADDR
1601 to MEMADDR + LEN range).
876fa593 1602
4a64f543
MS
1603 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1604 offset so that we cannot miss a breakpoint with its shadow
1605 range tail still reaching MEMADDR. */
c5aa993b 1606
35df4500
TJB
1607 if ((bl->address + bp_location_shadow_len_after_address_max
1608 >= bl->address)
1609 && (bl->address + bp_location_shadow_len_after_address_max
1610 <= memaddr))
876fa593
JK
1611 bc_l = bc;
1612 else
1613 bc_r = bc;
1614 }
1615
128070bb
PA
1616 /* Due to the binary search above, we need to make sure we pick the
1617 first location that's at BC_L's address. E.g., if there are
1618 multiple locations at the same address, BC_L may end up pointing
1619 at a duplicate location, and miss the "master"/"inserted"
1620 location. Say, given locations L1, L2 and L3 at addresses A and
1621 B:
1622
1623 L1@A, L2@A, L3@B, ...
1624
1625 BC_L could end up pointing at location L2, while the "master"
1626 location could be L1. Since the `loc->inserted' flag is only set
1627 on "master" locations, we'd forget to restore the shadow of L1
1628 and L2. */
1629 while (bc_l > 0
1630 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1631 bc_l--;
1632
876fa593
JK
1633 /* Now do full processing of the found relevant range of elements. */
1634
1635 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1636 {
35df4500 1637 struct bp_location *bl = bp_location[bc];
876fa593
JK
1638 CORE_ADDR bp_addr = 0;
1639 int bp_size = 0;
1640 int bptoffset = 0;
1641
35df4500
TJB
1642 /* bp_location array has BL->OWNER always non-NULL. */
1643 if (bl->owner->type == bp_none)
8a3fe4f8 1644 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1645 bl->owner->number);
ffce0d52 1646
e5dd4106 1647 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1648 content. */
1649
35df4500
TJB
1650 if (bl->address >= bp_location_placed_address_before_address_max
1651 && memaddr + len <= (bl->address
1652 - bp_location_placed_address_before_address_max))
876fa593
JK
1653 break;
1654
35df4500 1655 if (!bp_location_has_shadow (bl))
c5aa993b 1656 continue;
6c95b8df 1657
9d497a19
PA
1658 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1659 memaddr, len, &bl->target_info, bl->gdbarch);
1660 }
c906108c 1661}
9d497a19 1662
c906108c 1663\f
c5aa993b 1664
b775012e
LM
1665/* Return true if BPT is either a software breakpoint or a hardware
1666 breakpoint. */
1667
1668int
1669is_breakpoint (const struct breakpoint *bpt)
1670{
1671 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1672 || bpt->type == bp_hardware_breakpoint
1673 || bpt->type == bp_dprintf);
b775012e
LM
1674}
1675
60e1c644
PA
1676/* Return true if BPT is of any hardware watchpoint kind. */
1677
a5606eee 1678static int
d77f58be 1679is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1680{
1681 return (bpt->type == bp_hardware_watchpoint
1682 || bpt->type == bp_read_watchpoint
1683 || bpt->type == bp_access_watchpoint);
1684}
7270d8f2 1685
60e1c644
PA
1686/* Return true if BPT is of any watchpoint kind, hardware or
1687 software. */
1688
3a5c3e22 1689int
d77f58be 1690is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1691{
1692 return (is_hardware_watchpoint (bpt)
1693 || bpt->type == bp_watchpoint);
1694}
1695
3a5c3e22
PA
1696/* Returns true if the current thread and its running state are safe
1697 to evaluate or update watchpoint B. Watchpoints on local
1698 expressions need to be evaluated in the context of the thread that
1699 was current when the watchpoint was created, and, that thread needs
1700 to be stopped to be able to select the correct frame context.
1701 Watchpoints on global expressions can be evaluated on any thread,
1702 and in any state. It is presently left to the target allowing
1703 memory accesses when threads are running. */
f6bc2008
PA
1704
1705static int
3a5c3e22 1706watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1707{
d0d8b0c6
JK
1708 return (b->base.pspace == current_program_space
1709 && (ptid_equal (b->watchpoint_thread, null_ptid)
1710 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1711 && !is_executing (inferior_ptid))));
f6bc2008
PA
1712}
1713
d0fb5eae
JK
1714/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1715 associated bp_watchpoint_scope breakpoint. */
1716
1717static void
3a5c3e22 1718watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1719{
3a5c3e22 1720 struct breakpoint *b = &w->base;
d0fb5eae
JK
1721
1722 if (b->related_breakpoint != b)
1723 {
1724 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1725 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1726 b->related_breakpoint->disposition = disp_del_at_next_stop;
1727 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1728 b->related_breakpoint = b;
1729 }
1730 b->disposition = disp_del_at_next_stop;
1731}
1732
bb9d5f81
PP
1733/* Extract a bitfield value from value VAL using the bit parameters contained in
1734 watchpoint W. */
1735
1736static struct value *
1737extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1738{
1739 struct value *bit_val;
1740
1741 if (val == NULL)
1742 return NULL;
1743
1744 bit_val = allocate_value (value_type (val));
1745
1746 unpack_value_bitfield (bit_val,
1747 w->val_bitpos,
1748 w->val_bitsize,
1749 value_contents_for_printing (val),
1750 value_offset (val),
1751 val);
1752
1753 return bit_val;
1754}
1755
567e1b4e
JB
1756/* Assuming that B is a watchpoint:
1757 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1758 - Evaluate expression and store the result in B->val
567e1b4e
JB
1759 - Evaluate the condition if there is one, and store the result
1760 in b->loc->cond.
a5606eee
VP
1761 - Update the list of values that must be watched in B->loc.
1762
4a64f543
MS
1763 If the watchpoint disposition is disp_del_at_next_stop, then do
1764 nothing. If this is local watchpoint that is out of scope, delete
1765 it.
1766
1767 Even with `set breakpoint always-inserted on' the watchpoints are
1768 removed + inserted on each stop here. Normal breakpoints must
1769 never be removed because they might be missed by a running thread
1770 when debugging in non-stop mode. On the other hand, hardware
1771 watchpoints (is_hardware_watchpoint; processed here) are specific
1772 to each LWP since they are stored in each LWP's hardware debug
1773 registers. Therefore, such LWP must be stopped first in order to
1774 be able to modify its hardware watchpoints.
1775
1776 Hardware watchpoints must be reset exactly once after being
1777 presented to the user. It cannot be done sooner, because it would
1778 reset the data used to present the watchpoint hit to the user. And
1779 it must not be done later because it could display the same single
1780 watchpoint hit during multiple GDB stops. Note that the latter is
1781 relevant only to the hardware watchpoint types bp_read_watchpoint
1782 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1783 not user-visible - its hit is suppressed if the memory content has
1784 not changed.
1785
1786 The following constraints influence the location where we can reset
1787 hardware watchpoints:
1788
1789 * target_stopped_by_watchpoint and target_stopped_data_address are
1790 called several times when GDB stops.
1791
1792 [linux]
1793 * Multiple hardware watchpoints can be hit at the same time,
1794 causing GDB to stop. GDB only presents one hardware watchpoint
1795 hit at a time as the reason for stopping, and all the other hits
1796 are presented later, one after the other, each time the user
1797 requests the execution to be resumed. Execution is not resumed
1798 for the threads still having pending hit event stored in
1799 LWP_INFO->STATUS. While the watchpoint is already removed from
1800 the inferior on the first stop the thread hit event is kept being
1801 reported from its cached value by linux_nat_stopped_data_address
1802 until the real thread resume happens after the watchpoint gets
1803 presented and thus its LWP_INFO->STATUS gets reset.
1804
1805 Therefore the hardware watchpoint hit can get safely reset on the
1806 watchpoint removal from inferior. */
a79d3c27 1807
b40ce68a 1808static void
3a5c3e22 1809update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1810{
a5606eee 1811 int within_current_scope;
a5606eee 1812 struct frame_id saved_frame_id;
66076460 1813 int frame_saved;
a5606eee 1814
f6bc2008
PA
1815 /* If this is a local watchpoint, we only want to check if the
1816 watchpoint frame is in scope if the current thread is the thread
1817 that was used to create the watchpoint. */
1818 if (!watchpoint_in_thread_scope (b))
1819 return;
1820
3a5c3e22 1821 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1822 return;
1823
66076460 1824 frame_saved = 0;
a5606eee
VP
1825
1826 /* Determine if the watchpoint is within scope. */
1827 if (b->exp_valid_block == NULL)
1828 within_current_scope = 1;
1829 else
1830 {
b5db5dfc
UW
1831 struct frame_info *fi = get_current_frame ();
1832 struct gdbarch *frame_arch = get_frame_arch (fi);
1833 CORE_ADDR frame_pc = get_frame_pc (fi);
1834
1835 /* If we're in a function epilogue, unwinding may not work
1836 properly, so do not attempt to recreate locations at this
1837 point. See similar comments in watchpoint_check. */
1838 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1839 return;
66076460
DJ
1840
1841 /* Save the current frame's ID so we can restore it after
1842 evaluating the watchpoint expression on its own frame. */
1843 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1844 took a frame parameter, so that we didn't have to change the
1845 selected frame. */
1846 frame_saved = 1;
1847 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1848
a5606eee
VP
1849 fi = frame_find_by_id (b->watchpoint_frame);
1850 within_current_scope = (fi != NULL);
1851 if (within_current_scope)
1852 select_frame (fi);
1853 }
1854
b5db5dfc
UW
1855 /* We don't free locations. They are stored in the bp_location array
1856 and update_global_location_list will eventually delete them and
1857 remove breakpoints if needed. */
3a5c3e22 1858 b->base.loc = NULL;
b5db5dfc 1859
a5606eee
VP
1860 if (within_current_scope && reparse)
1861 {
bbc13ae3 1862 const char *s;
d63d0675 1863
a5606eee
VP
1864 if (b->exp)
1865 {
1866 xfree (b->exp);
1867 b->exp = NULL;
1868 }
d63d0675 1869 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1870 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1871 /* If the meaning of expression itself changed, the old value is
1872 no longer relevant. We don't want to report a watchpoint hit
1873 to the user when the old value and the new value may actually
1874 be completely different objects. */
1875 value_free (b->val);
fa4727a6
DJ
1876 b->val = NULL;
1877 b->val_valid = 0;
60e1c644
PA
1878
1879 /* Note that unlike with breakpoints, the watchpoint's condition
1880 expression is stored in the breakpoint object, not in the
1881 locations (re)created below. */
3a5c3e22 1882 if (b->base.cond_string != NULL)
60e1c644
PA
1883 {
1884 if (b->cond_exp != NULL)
1885 {
1886 xfree (b->cond_exp);
1887 b->cond_exp = NULL;
1888 }
1889
3a5c3e22 1890 s = b->base.cond_string;
1bb9788d 1891 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1892 }
a5606eee 1893 }
a5606eee
VP
1894
1895 /* If we failed to parse the expression, for example because
1896 it refers to a global variable in a not-yet-loaded shared library,
1897 don't try to insert watchpoint. We don't automatically delete
1898 such watchpoint, though, since failure to parse expression
1899 is different from out-of-scope watchpoint. */
e8369a73 1900 if (!target_has_execution)
2d134ed3
PA
1901 {
1902 /* Without execution, memory can't change. No use to try and
1903 set watchpoint locations. The watchpoint will be reset when
1904 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1905 if (!can_use_hw_watchpoints)
1906 {
1907 if (b->base.ops->works_in_software_mode (&b->base))
1908 b->base.type = bp_watchpoint;
1909 else
638aa5a1
AB
1910 error (_("Can't set read/access watchpoint when "
1911 "hardware watchpoints are disabled."));
e8369a73 1912 }
2d134ed3
PA
1913 }
1914 else if (within_current_scope && b->exp)
a5606eee 1915 {
0cf6dd15 1916 int pc = 0;
fa4727a6 1917 struct value *val_chain, *v, *result, *next;
2d134ed3 1918 struct program_space *frame_pspace;
a5606eee 1919
3a1115a0 1920 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1921
a5606eee
VP
1922 /* Avoid setting b->val if it's already set. The meaning of
1923 b->val is 'the last value' user saw, and we should update
1924 it only if we reported that last value to user. As it
9c06b0b4
TJB
1925 happens, the code that reports it updates b->val directly.
1926 We don't keep track of the memory value for masked
1927 watchpoints. */
3a5c3e22 1928 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1929 {
bb9d5f81
PP
1930 if (b->val_bitsize != 0)
1931 {
1932 v = extract_bitfield_from_watchpoint_value (b, v);
1933 if (v != NULL)
1934 release_value (v);
1935 }
fa4727a6
DJ
1936 b->val = v;
1937 b->val_valid = 1;
1938 }
a5606eee 1939
2d134ed3
PA
1940 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1941
a5606eee 1942 /* Look at each value on the value chain. */
9fa40276 1943 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1944 {
1945 /* If it's a memory location, and GDB actually needed
1946 its contents to evaluate the expression, then we
fa4727a6
DJ
1947 must watch it. If the first value returned is
1948 still lazy, that means an error occurred reading it;
1949 watch it anyway in case it becomes readable. */
a5606eee 1950 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1951 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1952 {
1953 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1954
a5606eee
VP
1955 /* We only watch structs and arrays if user asked
1956 for it explicitly, never if they just happen to
1957 appear in the middle of some value chain. */
fa4727a6 1958 if (v == result
a5606eee
VP
1959 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1960 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1961 {
1962 CORE_ADDR addr;
744a8059 1963 int type;
a5606eee 1964 struct bp_location *loc, **tmp;
bb9d5f81
PP
1965 int bitpos = 0, bitsize = 0;
1966
1967 if (value_bitsize (v) != 0)
1968 {
1969 /* Extract the bit parameters out from the bitfield
1970 sub-expression. */
1971 bitpos = value_bitpos (v);
1972 bitsize = value_bitsize (v);
1973 }
1974 else if (v == result && b->val_bitsize != 0)
1975 {
1976 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1977 lvalue whose bit parameters are saved in the fields
1978 VAL_BITPOS and VAL_BITSIZE. */
1979 bitpos = b->val_bitpos;
1980 bitsize = b->val_bitsize;
1981 }
a5606eee 1982
42ae5230 1983 addr = value_address (v);
bb9d5f81
PP
1984 if (bitsize != 0)
1985 {
1986 /* Skip the bytes that don't contain the bitfield. */
1987 addr += bitpos / 8;
1988 }
1989
a5606eee 1990 type = hw_write;
3a5c3e22 1991 if (b->base.type == bp_read_watchpoint)
a5606eee 1992 type = hw_read;
3a5c3e22 1993 else if (b->base.type == bp_access_watchpoint)
a5606eee 1994 type = hw_access;
3a5c3e22
PA
1995
1996 loc = allocate_bp_location (&b->base);
1997 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1998 ;
1999 *tmp = loc;
a6d9a66e 2000 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2001
2002 loc->pspace = frame_pspace;
a5606eee 2003 loc->address = addr;
bb9d5f81
PP
2004
2005 if (bitsize != 0)
2006 {
2007 /* Just cover the bytes that make up the bitfield. */
2008 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2009 }
2010 else
2011 loc->length = TYPE_LENGTH (value_type (v));
2012
a5606eee
VP
2013 loc->watchpoint_type = type;
2014 }
2015 }
9fa40276
TJB
2016 }
2017
2018 /* Change the type of breakpoint between hardware assisted or
2019 an ordinary watchpoint depending on the hardware support
2020 and free hardware slots. REPARSE is set when the inferior
2021 is started. */
a9634178 2022 if (reparse)
9fa40276 2023 {
e09342b5 2024 int reg_cnt;
9fa40276
TJB
2025 enum bp_loc_type loc_type;
2026 struct bp_location *bl;
a5606eee 2027
a9634178 2028 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2029
2030 if (reg_cnt)
9fa40276
TJB
2031 {
2032 int i, target_resources_ok, other_type_used;
a1398e0c 2033 enum bptype type;
9fa40276 2034
a9634178
TJB
2035 /* Use an exact watchpoint when there's only one memory region to be
2036 watched, and only one debug register is needed to watch it. */
2037 b->exact = target_exact_watchpoints && reg_cnt == 1;
2038
9fa40276 2039 /* We need to determine how many resources are already
e09342b5
TJB
2040 used for all other hardware watchpoints plus this one
2041 to see if we still have enough resources to also fit
a1398e0c
PA
2042 this watchpoint in as well. */
2043
2044 /* If this is a software watchpoint, we try to turn it
2045 to a hardware one -- count resources as if B was of
2046 hardware watchpoint type. */
2047 type = b->base.type;
2048 if (type == bp_watchpoint)
2049 type = bp_hardware_watchpoint;
2050
2051 /* This watchpoint may or may not have been placed on
2052 the list yet at this point (it won't be in the list
2053 if we're trying to create it for the first time,
2054 through watch_command), so always account for it
2055 manually. */
2056
2057 /* Count resources used by all watchpoints except B. */
2058 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2059
2060 /* Add in the resources needed for B. */
2061 i += hw_watchpoint_use_count (&b->base);
2062
2063 target_resources_ok
2064 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2065 if (target_resources_ok <= 0)
a9634178 2066 {
3a5c3e22 2067 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2068
2069 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2070 error (_("Target does not support this type of "
2071 "hardware watchpoint."));
9c06b0b4
TJB
2072 else if (target_resources_ok < 0 && !sw_mode)
2073 error (_("There are not enough available hardware "
2074 "resources for this watchpoint."));
a1398e0c
PA
2075
2076 /* Downgrade to software watchpoint. */
2077 b->base.type = bp_watchpoint;
2078 }
2079 else
2080 {
2081 /* If this was a software watchpoint, we've just
2082 found we have enough resources to turn it to a
2083 hardware watchpoint. Otherwise, this is a
2084 nop. */
2085 b->base.type = type;
a9634178 2086 }
9fa40276 2087 }
3a5c3e22 2088 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2089 {
2090 if (!can_use_hw_watchpoints)
2091 error (_("Can't set read/access watchpoint when "
2092 "hardware watchpoints are disabled."));
2093 else
2094 error (_("Expression cannot be implemented with "
2095 "read/access watchpoint."));
2096 }
9fa40276 2097 else
3a5c3e22 2098 b->base.type = bp_watchpoint;
9fa40276 2099
3a5c3e22 2100 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2101 : bp_loc_hardware_watchpoint);
3a5c3e22 2102 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2103 bl->loc_type = loc_type;
2104 }
2105
2106 for (v = val_chain; v; v = next)
2107 {
a5606eee
VP
2108 next = value_next (v);
2109 if (v != b->val)
2110 value_free (v);
2111 }
2112
c7437ca6
PA
2113 /* If a software watchpoint is not watching any memory, then the
2114 above left it without any location set up. But,
2115 bpstat_stop_status requires a location to be able to report
2116 stops, so make sure there's at least a dummy one. */
3a5c3e22 2117 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2118 {
3a5c3e22
PA
2119 struct breakpoint *base = &b->base;
2120 base->loc = allocate_bp_location (base);
2121 base->loc->pspace = frame_pspace;
2122 base->loc->address = -1;
2123 base->loc->length = -1;
2124 base->loc->watchpoint_type = -1;
c7437ca6 2125 }
a5606eee
VP
2126 }
2127 else if (!within_current_scope)
7270d8f2 2128 {
ac74f770
MS
2129 printf_filtered (_("\
2130Watchpoint %d deleted because the program has left the block\n\
2131in which its expression is valid.\n"),
3a5c3e22 2132 b->base.number);
d0fb5eae 2133 watchpoint_del_at_next_stop (b);
7270d8f2 2134 }
a5606eee
VP
2135
2136 /* Restore the selected frame. */
66076460
DJ
2137 if (frame_saved)
2138 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2139}
2140
a5606eee 2141
74960c60 2142/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2143 inserted in the inferior. We don't differentiate the type of BL's owner
2144 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2145 breakpoint_ops is not defined, because in insert_bp_location,
2146 tracepoint's insert_location will not be called. */
74960c60 2147static int
35df4500 2148should_be_inserted (struct bp_location *bl)
74960c60 2149{
35df4500 2150 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2151 return 0;
2152
35df4500 2153 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2154 return 0;
2155
35df4500 2156 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2157 return 0;
2158
f8eba3c6
TT
2159 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2160 return 0;
2161
56710373
PA
2162 /* This is set for example, when we're attached to the parent of a
2163 vfork, and have detached from the child. The child is running
2164 free, and we expect it to do an exec or exit, at which point the
2165 OS makes the parent schedulable again (and the target reports
2166 that the vfork is done). Until the child is done with the shared
2167 memory region, do not insert breakpoints in the parent, otherwise
2168 the child could still trip on the parent's breakpoints. Since
2169 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2170 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2171 return 0;
2172
31e77af2
PA
2173 /* Don't insert a breakpoint if we're trying to step past its
2174 location. */
2175 if ((bl->loc_type == bp_loc_software_breakpoint
2176 || bl->loc_type == bp_loc_hardware_breakpoint)
2177 && stepping_past_instruction_at (bl->pspace->aspace,
2178 bl->address))
e558d7c1
PA
2179 {
2180 if (debug_infrun)
2181 {
2182 fprintf_unfiltered (gdb_stdlog,
2183 "infrun: skipping breakpoint: "
2184 "stepping past insn at: %s\n",
2185 paddress (bl->gdbarch, bl->address));
2186 }
2187 return 0;
2188 }
31e77af2 2189
963f9c80
PA
2190 /* Don't insert watchpoints if we're trying to step past the
2191 instruction that triggered one. */
2192 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2193 && stepping_past_nonsteppable_watchpoint ())
2194 {
2195 if (debug_infrun)
2196 {
2197 fprintf_unfiltered (gdb_stdlog,
2198 "infrun: stepping past non-steppable watchpoint. "
2199 "skipping watchpoint at %s:%d\n",
2200 paddress (bl->gdbarch, bl->address),
2201 bl->length);
2202 }
2203 return 0;
2204 }
2205
74960c60
VP
2206 return 1;
2207}
2208
934709f0
PW
2209/* Same as should_be_inserted but does the check assuming
2210 that the location is not duplicated. */
2211
2212static int
2213unduplicated_should_be_inserted (struct bp_location *bl)
2214{
2215 int result;
2216 const int save_duplicate = bl->duplicate;
2217
2218 bl->duplicate = 0;
2219 result = should_be_inserted (bl);
2220 bl->duplicate = save_duplicate;
2221 return result;
2222}
2223
b775012e
LM
2224/* Parses a conditional described by an expression COND into an
2225 agent expression bytecode suitable for evaluation
2226 by the bytecode interpreter. Return NULL if there was
2227 any error during parsing. */
2228
2229static struct agent_expr *
2230parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2231{
2232 struct agent_expr *aexpr = NULL;
b775012e
LM
2233 volatile struct gdb_exception ex;
2234
2235 if (!cond)
2236 return NULL;
2237
2238 /* We don't want to stop processing, so catch any errors
2239 that may show up. */
2240 TRY_CATCH (ex, RETURN_MASK_ERROR)
2241 {
2242 aexpr = gen_eval_for_expr (scope, cond);
2243 }
2244
2245 if (ex.reason < 0)
2246 {
2247 /* If we got here, it means the condition could not be parsed to a valid
2248 bytecode expression and thus can't be evaluated on the target's side.
2249 It's no use iterating through the conditions. */
2250 return NULL;
2251 }
2252
2253 /* We have a valid agent expression. */
2254 return aexpr;
2255}
2256
2257/* Based on location BL, create a list of breakpoint conditions to be
2258 passed on to the target. If we have duplicated locations with different
2259 conditions, we will add such conditions to the list. The idea is that the
2260 target will evaluate the list of conditions and will only notify GDB when
2261 one of them is true. */
2262
2263static void
2264build_target_condition_list (struct bp_location *bl)
2265{
2266 struct bp_location **locp = NULL, **loc2p;
2267 int null_condition_or_parse_error = 0;
2268 int modified = bl->needs_update;
2269 struct bp_location *loc;
2270
8b4f3082
PA
2271 /* Release conditions left over from a previous insert. */
2272 VEC_free (agent_expr_p, bl->target_info.conditions);
2273
b775012e
LM
2274 /* This is only meaningful if the target is
2275 evaluating conditions and if the user has
2276 opted for condition evaluation on the target's
2277 side. */
2278 if (gdb_evaluates_breakpoint_condition_p ()
2279 || !target_supports_evaluation_of_breakpoint_conditions ())
2280 return;
2281
2282 /* Do a first pass to check for locations with no assigned
2283 conditions or conditions that fail to parse to a valid agent expression
2284 bytecode. If any of these happen, then it's no use to send conditions
2285 to the target since this location will always trigger and generate a
2286 response back to GDB. */
2287 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2288 {
2289 loc = (*loc2p);
2290 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2291 {
2292 if (modified)
2293 {
2294 struct agent_expr *aexpr;
2295
2296 /* Re-parse the conditions since something changed. In that
2297 case we already freed the condition bytecodes (see
2298 force_breakpoint_reinsertion). We just
2299 need to parse the condition to bytecodes again. */
2300 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2301 loc->cond_bytecode = aexpr;
2302
2303 /* Check if we managed to parse the conditional expression
2304 correctly. If not, we will not send this condition
2305 to the target. */
2306 if (aexpr)
2307 continue;
2308 }
2309
2310 /* If we have a NULL bytecode expression, it means something
2311 went wrong or we have a null condition expression. */
2312 if (!loc->cond_bytecode)
2313 {
2314 null_condition_or_parse_error = 1;
2315 break;
2316 }
2317 }
2318 }
2319
2320 /* If any of these happened, it means we will have to evaluate the conditions
2321 for the location's address on gdb's side. It is no use keeping bytecodes
2322 for all the other duplicate locations, thus we free all of them here.
2323
2324 This is so we have a finer control over which locations' conditions are
2325 being evaluated by GDB or the remote stub. */
2326 if (null_condition_or_parse_error)
2327 {
2328 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2329 {
2330 loc = (*loc2p);
2331 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2332 {
2333 /* Only go as far as the first NULL bytecode is
2334 located. */
2335 if (!loc->cond_bytecode)
2336 return;
2337
2338 free_agent_expr (loc->cond_bytecode);
2339 loc->cond_bytecode = NULL;
2340 }
2341 }
2342 }
2343
2344 /* No NULL conditions or failed bytecode generation. Build a condition list
2345 for this location's address. */
2346 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2347 {
2348 loc = (*loc2p);
2349 if (loc->cond
2350 && is_breakpoint (loc->owner)
2351 && loc->pspace->num == bl->pspace->num
2352 && loc->owner->enable_state == bp_enabled
2353 && loc->enabled)
2354 /* Add the condition to the vector. This will be used later to send the
2355 conditions to the target. */
2356 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2357 loc->cond_bytecode);
2358 }
2359
2360 return;
2361}
2362
d3ce09f5
SS
2363/* Parses a command described by string CMD into an agent expression
2364 bytecode suitable for evaluation by the bytecode interpreter.
2365 Return NULL if there was any error during parsing. */
2366
2367static struct agent_expr *
2368parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2369{
2370 struct cleanup *old_cleanups = 0;
2371 struct expression *expr, **argvec;
2372 struct agent_expr *aexpr = NULL;
d3ce09f5 2373 volatile struct gdb_exception ex;
bbc13ae3
KS
2374 const char *cmdrest;
2375 const char *format_start, *format_end;
d3ce09f5
SS
2376 struct format_piece *fpieces;
2377 int nargs;
2378 struct gdbarch *gdbarch = get_current_arch ();
2379
2380 if (!cmd)
2381 return NULL;
2382
2383 cmdrest = cmd;
2384
2385 if (*cmdrest == ',')
2386 ++cmdrest;
bbc13ae3 2387 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2388
2389 if (*cmdrest++ != '"')
2390 error (_("No format string following the location"));
2391
2392 format_start = cmdrest;
2393
2394 fpieces = parse_format_string (&cmdrest);
2395
2396 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2397
2398 format_end = cmdrest;
2399
2400 if (*cmdrest++ != '"')
2401 error (_("Bad format string, non-terminated '\"'."));
2402
bbc13ae3 2403 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2404
2405 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2406 error (_("Invalid argument syntax"));
2407
2408 if (*cmdrest == ',')
2409 cmdrest++;
bbc13ae3 2410 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2411
2412 /* For each argument, make an expression. */
2413
2414 argvec = (struct expression **) alloca (strlen (cmd)
2415 * sizeof (struct expression *));
2416
2417 nargs = 0;
2418 while (*cmdrest != '\0')
2419 {
bbc13ae3 2420 const char *cmd1;
d3ce09f5
SS
2421
2422 cmd1 = cmdrest;
2423 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2424 argvec[nargs++] = expr;
2425 cmdrest = cmd1;
2426 if (*cmdrest == ',')
2427 ++cmdrest;
2428 }
2429
2430 /* We don't want to stop processing, so catch any errors
2431 that may show up. */
2432 TRY_CATCH (ex, RETURN_MASK_ERROR)
2433 {
2434 aexpr = gen_printf (scope, gdbarch, 0, 0,
2435 format_start, format_end - format_start,
2436 fpieces, nargs, argvec);
2437 }
2438
752eb8b4
TT
2439 do_cleanups (old_cleanups);
2440
d3ce09f5
SS
2441 if (ex.reason < 0)
2442 {
2443 /* If we got here, it means the command could not be parsed to a valid
2444 bytecode expression and thus can't be evaluated on the target's side.
2445 It's no use iterating through the other commands. */
2446 return NULL;
2447 }
2448
d3ce09f5
SS
2449 /* We have a valid agent expression, return it. */
2450 return aexpr;
2451}
2452
2453/* Based on location BL, create a list of breakpoint commands to be
2454 passed on to the target. If we have duplicated locations with
2455 different commands, we will add any such to the list. */
2456
2457static void
2458build_target_command_list (struct bp_location *bl)
2459{
2460 struct bp_location **locp = NULL, **loc2p;
2461 int null_command_or_parse_error = 0;
2462 int modified = bl->needs_update;
2463 struct bp_location *loc;
2464
8b4f3082
PA
2465 /* Release commands left over from a previous insert. */
2466 VEC_free (agent_expr_p, bl->target_info.tcommands);
2467
41fac0cf 2468 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2469 return;
2470
41fac0cf
PA
2471 /* For now, limit to agent-style dprintf breakpoints. */
2472 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2473 return;
2474
41fac0cf
PA
2475 /* For now, if we have any duplicate location that isn't a dprintf,
2476 don't install the target-side commands, as that would make the
2477 breakpoint not be reported to the core, and we'd lose
2478 control. */
2479 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2480 {
2481 loc = (*loc2p);
2482 if (is_breakpoint (loc->owner)
2483 && loc->pspace->num == bl->pspace->num
2484 && loc->owner->type != bp_dprintf)
2485 return;
2486 }
2487
d3ce09f5
SS
2488 /* Do a first pass to check for locations with no assigned
2489 conditions or conditions that fail to parse to a valid agent expression
2490 bytecode. If any of these happen, then it's no use to send conditions
2491 to the target since this location will always trigger and generate a
2492 response back to GDB. */
2493 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2494 {
2495 loc = (*loc2p);
2496 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2497 {
2498 if (modified)
2499 {
2500 struct agent_expr *aexpr;
2501
2502 /* Re-parse the commands since something changed. In that
2503 case we already freed the command bytecodes (see
2504 force_breakpoint_reinsertion). We just
2505 need to parse the command to bytecodes again. */
2506 aexpr = parse_cmd_to_aexpr (bl->address,
2507 loc->owner->extra_string);
2508 loc->cmd_bytecode = aexpr;
2509
2510 if (!aexpr)
2511 continue;
2512 }
2513
2514 /* If we have a NULL bytecode expression, it means something
2515 went wrong or we have a null command expression. */
2516 if (!loc->cmd_bytecode)
2517 {
2518 null_command_or_parse_error = 1;
2519 break;
2520 }
2521 }
2522 }
2523
2524 /* If anything failed, then we're not doing target-side commands,
2525 and so clean up. */
2526 if (null_command_or_parse_error)
2527 {
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner)
2532 && loc->pspace->num == bl->pspace->num)
2533 {
2534 /* Only go as far as the first NULL bytecode is
2535 located. */
40fb6c5e 2536 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2537 return;
2538
40fb6c5e
HZ
2539 free_agent_expr (loc->cmd_bytecode);
2540 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2541 }
2542 }
2543 }
2544
2545 /* No NULL commands or failed bytecode generation. Build a command list
2546 for this location's address. */
2547 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2548 {
2549 loc = (*loc2p);
2550 if (loc->owner->extra_string
2551 && is_breakpoint (loc->owner)
2552 && loc->pspace->num == bl->pspace->num
2553 && loc->owner->enable_state == bp_enabled
2554 && loc->enabled)
2555 /* Add the command to the vector. This will be used later
2556 to send the commands to the target. */
2557 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2558 loc->cmd_bytecode);
2559 }
2560
2561 bl->target_info.persist = 0;
2562 /* Maybe flag this location as persistent. */
2563 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2564 bl->target_info.persist = 1;
2565}
2566
35df4500
TJB
2567/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2568 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2569 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2570 Returns 0 for success, 1 if the bp_location type is not supported or
2571 -1 for failure.
879bfdc2 2572
4a64f543
MS
2573 NOTE drow/2003-09-09: This routine could be broken down to an
2574 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2575static int
35df4500 2576insert_bp_location (struct bp_location *bl,
26bb91f3 2577 struct ui_file *tmp_error_stream,
3fbb6ffa 2578 int *disabled_breaks,
dd61ec5c
MW
2579 int *hw_breakpoint_error,
2580 int *hw_bp_error_explained_already)
879bfdc2 2581{
0000e5cc
PA
2582 enum errors bp_err = GDB_NO_ERROR;
2583 const char *bp_err_message = NULL;
c90a6fb7 2584 volatile struct gdb_exception e;
879bfdc2 2585
b775012e 2586 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2587 return 0;
2588
35c63cd8
JB
2589 /* Note we don't initialize bl->target_info, as that wipes out
2590 the breakpoint location's shadow_contents if the breakpoint
2591 is still inserted at that location. This in turn breaks
2592 target_read_memory which depends on these buffers when
2593 a memory read is requested at the breakpoint location:
2594 Once the target_info has been wiped, we fail to see that
2595 we have a breakpoint inserted at that address and thus
2596 read the breakpoint instead of returning the data saved in
2597 the breakpoint location's shadow contents. */
0d5ed153 2598 bl->target_info.reqstd_address = bl->address;
35df4500 2599 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2600 bl->target_info.length = bl->length;
8181d85f 2601
b775012e
LM
2602 /* When working with target-side conditions, we must pass all the conditions
2603 for the same breakpoint address down to the target since GDB will not
2604 insert those locations. With a list of breakpoint conditions, the target
2605 can decide when to stop and notify GDB. */
2606
2607 if (is_breakpoint (bl->owner))
2608 {
2609 build_target_condition_list (bl);
d3ce09f5
SS
2610 build_target_command_list (bl);
2611 /* Reset the modification marker. */
b775012e
LM
2612 bl->needs_update = 0;
2613 }
2614
35df4500
TJB
2615 if (bl->loc_type == bp_loc_software_breakpoint
2616 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2617 {
35df4500 2618 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2619 {
2620 /* If the explicitly specified breakpoint type
2621 is not hardware breakpoint, check the memory map to see
2622 if the breakpoint address is in read only memory or not.
4a64f543 2623
765dc015
VP
2624 Two important cases are:
2625 - location type is not hardware breakpoint, memory
2626 is readonly. We change the type of the location to
2627 hardware breakpoint.
4a64f543
MS
2628 - location type is hardware breakpoint, memory is
2629 read-write. This means we've previously made the
2630 location hardware one, but then the memory map changed,
2631 so we undo.
765dc015 2632
4a64f543
MS
2633 When breakpoints are removed, remove_breakpoints will use
2634 location types we've just set here, the only possible
2635 problem is that memory map has changed during running
2636 program, but it's not going to work anyway with current
2637 gdb. */
765dc015 2638 struct mem_region *mr
0d5ed153 2639 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2640
2641 if (mr)
2642 {
2643 if (automatic_hardware_breakpoints)
2644 {
765dc015
VP
2645 enum bp_loc_type new_type;
2646
2647 if (mr->attrib.mode != MEM_RW)
2648 new_type = bp_loc_hardware_breakpoint;
2649 else
2650 new_type = bp_loc_software_breakpoint;
2651
35df4500 2652 if (new_type != bl->loc_type)
765dc015
VP
2653 {
2654 static int said = 0;
cc59ec59 2655
35df4500 2656 bl->loc_type = new_type;
765dc015
VP
2657 if (!said)
2658 {
3e43a32a
MS
2659 fprintf_filtered (gdb_stdout,
2660 _("Note: automatically using "
2661 "hardware breakpoints for "
2662 "read-only addresses.\n"));
765dc015
VP
2663 said = 1;
2664 }
2665 }
2666 }
35df4500 2667 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2668 && mr->attrib.mode != MEM_RW)
2669 {
2670 fprintf_unfiltered (tmp_error_stream,
2671 _("Cannot insert breakpoint %d.\n"
2672 "Cannot set software breakpoint "
2673 "at read-only address %s\n"),
2674 bl->owner->number,
2675 paddress (bl->gdbarch, bl->address));
2676 return 1;
2677 }
765dc015
VP
2678 }
2679 }
2680
879bfdc2
DJ
2681 /* First check to see if we have to handle an overlay. */
2682 if (overlay_debugging == ovly_off
35df4500
TJB
2683 || bl->section == NULL
2684 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2685 {
2686 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2687 TRY_CATCH (e, RETURN_MASK_ALL)
2688 {
0000e5cc
PA
2689 int val;
2690
dd61ec5c 2691 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2692 if (val)
2693 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2694 }
2695 if (e.reason < 0)
2696 {
0000e5cc
PA
2697 bp_err = e.error;
2698 bp_err_message = e.message;
dd61ec5c 2699 }
879bfdc2
DJ
2700 }
2701 else
2702 {
4a64f543 2703 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2704 Shall we set a breakpoint at the LMA? */
2705 if (!overlay_events_enabled)
2706 {
2707 /* Yes -- overlay event support is not active,
2708 so we must try to set a breakpoint at the LMA.
2709 This will not work for a hardware breakpoint. */
35df4500 2710 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2711 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2712 bl->owner->number);
879bfdc2
DJ
2713 else
2714 {
35df4500
TJB
2715 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2716 bl->section);
879bfdc2 2717 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2718 bl->overlay_target_info = bl->target_info;
0d5ed153 2719 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2720
2721 /* No overlay handling: just set the breakpoint. */
2722 TRY_CATCH (e, RETURN_MASK_ALL)
2723 {
2724 int val;
2725
2726 val = target_insert_breakpoint (bl->gdbarch,
2727 &bl->overlay_target_info);
2728 if (val)
2729 bp_err = GENERIC_ERROR;
2730 }
2731 if (e.reason < 0)
2732 {
2733 bp_err = e.error;
2734 bp_err_message = e.message;
2735 }
2736
2737 if (bp_err != GDB_NO_ERROR)
99361f52 2738 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2739 "Overlay breakpoint %d "
2740 "failed: in ROM?\n",
35df4500 2741 bl->owner->number);
879bfdc2
DJ
2742 }
2743 }
2744 /* Shall we set a breakpoint at the VMA? */
35df4500 2745 if (section_is_mapped (bl->section))
879bfdc2
DJ
2746 {
2747 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2748 TRY_CATCH (e, RETURN_MASK_ALL)
2749 {
0000e5cc
PA
2750 int val;
2751
dd61ec5c 2752 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2753 if (val)
2754 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2755 }
2756 if (e.reason < 0)
2757 {
0000e5cc
PA
2758 bp_err = e.error;
2759 bp_err_message = e.message;
dd61ec5c 2760 }
879bfdc2
DJ
2761 }
2762 else
2763 {
2764 /* No. This breakpoint will not be inserted.
2765 No error, but do not mark the bp as 'inserted'. */
2766 return 0;
2767 }
2768 }
2769
0000e5cc 2770 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2771 {
2772 /* Can't set the breakpoint. */
0000e5cc
PA
2773
2774 /* In some cases, we might not be able to insert a
2775 breakpoint in a shared library that has already been
2776 removed, but we have not yet processed the shlib unload
2777 event. Unfortunately, some targets that implement
076855f9
PA
2778 breakpoint insertion themselves can't tell why the
2779 breakpoint insertion failed (e.g., the remote target
2780 doesn't define error codes), so we must treat generic
2781 errors as memory errors. */
0000e5cc 2782 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2783 && bl->loc_type == bp_loc_software_breakpoint
08351840 2784 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2785 || shared_objfile_contains_address_p (bl->pspace,
2786 bl->address)))
879bfdc2 2787 {
4a64f543 2788 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2789 bl->shlib_disabled = 1;
8d3788bd 2790 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2791 if (!*disabled_breaks)
2792 {
2793 fprintf_unfiltered (tmp_error_stream,
2794 "Cannot insert breakpoint %d.\n",
2795 bl->owner->number);
2796 fprintf_unfiltered (tmp_error_stream,
2797 "Temporarily disabling shared "
2798 "library breakpoints:\n");
2799 }
2800 *disabled_breaks = 1;
879bfdc2 2801 fprintf_unfiltered (tmp_error_stream,
35df4500 2802 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2803 return 0;
879bfdc2
DJ
2804 }
2805 else
879bfdc2 2806 {
35df4500 2807 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2808 {
0000e5cc
PA
2809 *hw_breakpoint_error = 1;
2810 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2811 fprintf_unfiltered (tmp_error_stream,
2812 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2813 bl->owner->number, bp_err_message ? ":" : ".\n");
2814 if (bp_err_message != NULL)
2815 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2816 }
2817 else
2818 {
0000e5cc
PA
2819 if (bp_err_message == NULL)
2820 {
2821 char *message
2822 = memory_error_message (TARGET_XFER_E_IO,
2823 bl->gdbarch, bl->address);
2824 struct cleanup *old_chain = make_cleanup (xfree, message);
2825
2826 fprintf_unfiltered (tmp_error_stream,
2827 "Cannot insert breakpoint %d.\n"
2828 "%s\n",
2829 bl->owner->number, message);
2830 do_cleanups (old_chain);
2831 }
2832 else
2833 {
2834 fprintf_unfiltered (tmp_error_stream,
2835 "Cannot insert breakpoint %d: %s\n",
2836 bl->owner->number,
2837 bp_err_message);
2838 }
879bfdc2 2839 }
0000e5cc 2840 return 1;
879bfdc2
DJ
2841
2842 }
2843 }
2844 else
35df4500 2845 bl->inserted = 1;
879bfdc2 2846
0000e5cc 2847 return 0;
879bfdc2
DJ
2848 }
2849
35df4500 2850 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2851 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2852 watchpoints. It's not clear that it's necessary... */
35df4500 2853 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2854 {
0000e5cc
PA
2855 int val;
2856
77b06cd7
TJB
2857 gdb_assert (bl->owner->ops != NULL
2858 && bl->owner->ops->insert_location != NULL);
2859
2860 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2861
2862 /* If trying to set a read-watchpoint, and it turns out it's not
2863 supported, try emulating one with an access watchpoint. */
35df4500 2864 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2865 {
2866 struct bp_location *loc, **loc_temp;
2867
2868 /* But don't try to insert it, if there's already another
2869 hw_access location that would be considered a duplicate
2870 of this one. */
2871 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2872 if (loc != bl
85d721b8 2873 && loc->watchpoint_type == hw_access
35df4500 2874 && watchpoint_locations_match (bl, loc))
85d721b8 2875 {
35df4500
TJB
2876 bl->duplicate = 1;
2877 bl->inserted = 1;
2878 bl->target_info = loc->target_info;
2879 bl->watchpoint_type = hw_access;
85d721b8
PA
2880 val = 0;
2881 break;
2882 }
2883
2884 if (val == 1)
2885 {
77b06cd7
TJB
2886 bl->watchpoint_type = hw_access;
2887 val = bl->owner->ops->insert_location (bl);
2888
2889 if (val)
2890 /* Back to the original value. */
2891 bl->watchpoint_type = hw_read;
85d721b8
PA
2892 }
2893 }
2894
35df4500 2895 bl->inserted = (val == 0);
879bfdc2
DJ
2896 }
2897
35df4500 2898 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2899 {
0000e5cc
PA
2900 int val;
2901
77b06cd7
TJB
2902 gdb_assert (bl->owner->ops != NULL
2903 && bl->owner->ops->insert_location != NULL);
2904
2905 val = bl->owner->ops->insert_location (bl);
2906 if (val)
2907 {
2908 bl->owner->enable_state = bp_disabled;
2909
2910 if (val == 1)
2911 warning (_("\
2912Error inserting catchpoint %d: Your system does not support this type\n\
2913of catchpoint."), bl->owner->number);
2914 else
2915 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2916 }
2917
2918 bl->inserted = (val == 0);
1640b821
DJ
2919
2920 /* We've already printed an error message if there was a problem
2921 inserting this catchpoint, and we've disabled the catchpoint,
2922 so just return success. */
2923 return 0;
879bfdc2
DJ
2924 }
2925
2926 return 0;
2927}
2928
6c95b8df
PA
2929/* This function is called when program space PSPACE is about to be
2930 deleted. It takes care of updating breakpoints to not reference
2931 PSPACE anymore. */
2932
2933void
2934breakpoint_program_space_exit (struct program_space *pspace)
2935{
2936 struct breakpoint *b, *b_temp;
876fa593 2937 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2938
2939 /* Remove any breakpoint that was set through this program space. */
2940 ALL_BREAKPOINTS_SAFE (b, b_temp)
2941 {
2942 if (b->pspace == pspace)
2943 delete_breakpoint (b);
2944 }
2945
2946 /* Breakpoints set through other program spaces could have locations
2947 bound to PSPACE as well. Remove those. */
876fa593 2948 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2949 {
2950 struct bp_location *tmp;
2951
2952 if (loc->pspace == pspace)
2953 {
2bdf28a0 2954 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2955 if (loc->owner->loc == loc)
2956 loc->owner->loc = loc->next;
2957 else
2958 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2959 if (tmp->next == loc)
2960 {
2961 tmp->next = loc->next;
2962 break;
2963 }
2964 }
2965 }
2966
2967 /* Now update the global location list to permanently delete the
2968 removed locations above. */
44702360 2969 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2970}
2971
74960c60
VP
2972/* Make sure all breakpoints are inserted in inferior.
2973 Throws exception on any error.
2974 A breakpoint that is already inserted won't be inserted
2975 again, so calling this function twice is safe. */
2976void
2977insert_breakpoints (void)
2978{
2979 struct breakpoint *bpt;
2980
2981 ALL_BREAKPOINTS (bpt)
2982 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2983 {
2984 struct watchpoint *w = (struct watchpoint *) bpt;
2985
2986 update_watchpoint (w, 0 /* don't reparse. */);
2987 }
74960c60 2988
04086b45
PA
2989 /* Updating watchpoints creates new locations, so update the global
2990 location list. Explicitly tell ugll to insert locations and
2991 ignore breakpoints_always_inserted_mode. */
2992 update_global_location_list (UGLL_INSERT);
74960c60
VP
2993}
2994
20388dd6
YQ
2995/* Invoke CALLBACK for each of bp_location. */
2996
2997void
2998iterate_over_bp_locations (walk_bp_location_callback callback)
2999{
3000 struct bp_location *loc, **loc_tmp;
3001
3002 ALL_BP_LOCATIONS (loc, loc_tmp)
3003 {
3004 callback (loc, NULL);
3005 }
3006}
3007
b775012e
LM
3008/* This is used when we need to synch breakpoint conditions between GDB and the
3009 target. It is the case with deleting and disabling of breakpoints when using
3010 always-inserted mode. */
3011
3012static void
3013update_inserted_breakpoint_locations (void)
3014{
3015 struct bp_location *bl, **blp_tmp;
3016 int error_flag = 0;
3017 int val = 0;
3018 int disabled_breaks = 0;
3019 int hw_breakpoint_error = 0;
dd61ec5c 3020 int hw_bp_details_reported = 0;
b775012e
LM
3021
3022 struct ui_file *tmp_error_stream = mem_fileopen ();
3023 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3024
3025 /* Explicitly mark the warning -- this will only be printed if
3026 there was an error. */
3027 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3028
3029 save_current_space_and_thread ();
3030
3031 ALL_BP_LOCATIONS (bl, blp_tmp)
3032 {
3033 /* We only want to update software breakpoints and hardware
3034 breakpoints. */
3035 if (!is_breakpoint (bl->owner))
3036 continue;
3037
3038 /* We only want to update locations that are already inserted
3039 and need updating. This is to avoid unwanted insertion during
3040 deletion of breakpoints. */
3041 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3042 continue;
3043
3044 switch_to_program_space_and_thread (bl->pspace);
3045
3046 /* For targets that support global breakpoints, there's no need
3047 to select an inferior to insert breakpoint to. In fact, even
3048 if we aren't attached to any process yet, we should still
3049 insert breakpoints. */
f5656ead 3050 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3051 && ptid_equal (inferior_ptid, null_ptid))
3052 continue;
3053
3054 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3055 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3056 if (val)
3057 error_flag = val;
3058 }
3059
3060 if (error_flag)
3061 {
3062 target_terminal_ours_for_output ();
3063 error_stream (tmp_error_stream);
3064 }
3065
3066 do_cleanups (cleanups);
3067}
3068
c30eee59 3069/* Used when starting or continuing the program. */
c906108c 3070
74960c60
VP
3071static void
3072insert_breakpoint_locations (void)
c906108c 3073{
a5606eee 3074 struct breakpoint *bpt;
35df4500 3075 struct bp_location *bl, **blp_tmp;
eacd795a 3076 int error_flag = 0;
c906108c 3077 int val = 0;
3fbb6ffa 3078 int disabled_breaks = 0;
81d0cc19 3079 int hw_breakpoint_error = 0;
dd61ec5c 3080 int hw_bp_error_explained_already = 0;
c906108c 3081
81d0cc19 3082 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3083 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3084
81d0cc19
GS
3085 /* Explicitly mark the warning -- this will only be printed if
3086 there was an error. */
3087 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3088
3089 save_current_space_and_thread ();
3090
35df4500 3091 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3092 {
b775012e 3093 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3094 continue;
3095
4a64f543
MS
3096 /* There is no point inserting thread-specific breakpoints if
3097 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3098 has BL->OWNER always non-NULL. */
35df4500
TJB
3099 if (bl->owner->thread != -1
3100 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3101 continue;
3102
35df4500 3103 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3104
3105 /* For targets that support global breakpoints, there's no need
3106 to select an inferior to insert breakpoint to. In fact, even
3107 if we aren't attached to any process yet, we should still
3108 insert breakpoints. */
f5656ead 3109 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3110 && ptid_equal (inferior_ptid, null_ptid))
3111 continue;
3112
3fbb6ffa 3113 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3114 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3115 if (val)
eacd795a 3116 error_flag = val;
879bfdc2 3117 }
c906108c 3118
4a64f543
MS
3119 /* If we failed to insert all locations of a watchpoint, remove
3120 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3121 ALL_BREAKPOINTS (bpt)
3122 {
3123 int some_failed = 0;
3124 struct bp_location *loc;
3125
3126 if (!is_hardware_watchpoint (bpt))
3127 continue;
3128
d6b74ac4 3129 if (!breakpoint_enabled (bpt))
a5606eee 3130 continue;
74960c60
VP
3131
3132 if (bpt->disposition == disp_del_at_next_stop)
3133 continue;
a5606eee
VP
3134
3135 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3136 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3137 {
3138 some_failed = 1;
3139 break;
3140 }
3141 if (some_failed)
3142 {
3143 for (loc = bpt->loc; loc; loc = loc->next)
3144 if (loc->inserted)
3145 remove_breakpoint (loc, mark_uninserted);
3146
3147 hw_breakpoint_error = 1;
3148 fprintf_unfiltered (tmp_error_stream,
3149 "Could not insert hardware watchpoint %d.\n",
3150 bpt->number);
eacd795a 3151 error_flag = -1;
a5606eee
VP
3152 }
3153 }
3154
eacd795a 3155 if (error_flag)
81d0cc19
GS
3156 {
3157 /* If a hardware breakpoint or watchpoint was inserted, add a
3158 message about possibly exhausted resources. */
dd61ec5c 3159 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3160 {
c6510018
MS
3161 fprintf_unfiltered (tmp_error_stream,
3162 "Could not insert hardware breakpoints:\n\
3163You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3164 }
81d0cc19
GS
3165 target_terminal_ours_for_output ();
3166 error_stream (tmp_error_stream);
3167 }
f7545552
TT
3168
3169 do_cleanups (cleanups);
c906108c
SS
3170}
3171
c30eee59
TJB
3172/* Used when the program stops.
3173 Returns zero if successful, or non-zero if there was a problem
3174 removing a breakpoint location. */
3175
c906108c 3176int
fba45db2 3177remove_breakpoints (void)
c906108c 3178{
35df4500 3179 struct bp_location *bl, **blp_tmp;
3a1bae8e 3180 int val = 0;
c906108c 3181
35df4500 3182 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3183 {
1e4d1764 3184 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3185 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3186 }
3a1bae8e 3187 return val;
c906108c
SS
3188}
3189
49fa26b0
PA
3190/* When a thread exits, remove breakpoints that are related to
3191 that thread. */
3192
3193static void
3194remove_threaded_breakpoints (struct thread_info *tp, int silent)
3195{
3196 struct breakpoint *b, *b_tmp;
3197
3198 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3199 {
96181529 3200 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3201 {
3202 b->disposition = disp_del_at_next_stop;
3203
3204 printf_filtered (_("\
46ecd527 3205Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3206 b->number, tp->num);
3207
3208 /* Hide it from the user. */
3209 b->number = 0;
3210 }
3211 }
3212}
3213
6c95b8df
PA
3214/* Remove breakpoints of process PID. */
3215
3216int
3217remove_breakpoints_pid (int pid)
3218{
35df4500 3219 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3220 int val;
3221 struct inferior *inf = find_inferior_pid (pid);
3222
35df4500 3223 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3224 {
35df4500 3225 if (bl->pspace != inf->pspace)
6c95b8df
PA
3226 continue;
3227
d3ce09f5
SS
3228 if (bl->owner->type == bp_dprintf)
3229 continue;
3230
35df4500 3231 if (bl->inserted)
6c95b8df 3232 {
35df4500 3233 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3234 if (val != 0)
3235 return val;
3236 }
3237 }
3238 return 0;
3239}
3240
c906108c 3241int
fba45db2 3242reattach_breakpoints (int pid)
c906108c 3243{
6c95b8df 3244 struct cleanup *old_chain;
35df4500 3245 struct bp_location *bl, **blp_tmp;
c906108c 3246 int val;
86b887df 3247 struct ui_file *tmp_error_stream;
dd61ec5c 3248 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3249 struct inferior *inf;
3250 struct thread_info *tp;
3251
3252 tp = any_live_thread_of_process (pid);
3253 if (tp == NULL)
3254 return 1;
3255
3256 inf = find_inferior_pid (pid);
3257 old_chain = save_inferior_ptid ();
3258
3259 inferior_ptid = tp->ptid;
a4954f26 3260
86b887df 3261 tmp_error_stream = mem_fileopen ();
a4954f26 3262 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3263
35df4500 3264 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3265 {
35df4500 3266 if (bl->pspace != inf->pspace)
6c95b8df
PA
3267 continue;
3268
35df4500 3269 if (bl->inserted)
c5aa993b 3270 {
35df4500 3271 bl->inserted = 0;
dd61ec5c 3272 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3273 if (val != 0)
3274 {
ce696e05 3275 do_cleanups (old_chain);
c5aa993b
JM
3276 return val;
3277 }
3278 }
3279 }
ce696e05 3280 do_cleanups (old_chain);
c906108c
SS
3281 return 0;
3282}
3283
e58b0e63
PA
3284static int internal_breakpoint_number = -1;
3285
84f4c1fe
PM
3286/* Set the breakpoint number of B, depending on the value of INTERNAL.
3287 If INTERNAL is non-zero, the breakpoint number will be populated
3288 from internal_breakpoint_number and that variable decremented.
e5dd4106 3289 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3290 breakpoint_count and that value incremented. Internal breakpoints
3291 do not set the internal var bpnum. */
3292static void
3293set_breakpoint_number (int internal, struct breakpoint *b)
3294{
3295 if (internal)
3296 b->number = internal_breakpoint_number--;
3297 else
3298 {
3299 set_breakpoint_count (breakpoint_count + 1);
3300 b->number = breakpoint_count;
3301 }
3302}
3303
e62c965a 3304static struct breakpoint *
a6d9a66e 3305create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3306 CORE_ADDR address, enum bptype type,
c0a91b2b 3307 const struct breakpoint_ops *ops)
e62c965a 3308{
e62c965a
PP
3309 struct symtab_and_line sal;
3310 struct breakpoint *b;
3311
4a64f543 3312 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3313
3314 sal.pc = address;
3315 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3316 sal.pspace = current_program_space;
e62c965a 3317
06edf0c0 3318 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3319 b->number = internal_breakpoint_number--;
3320 b->disposition = disp_donttouch;
3321
3322 return b;
3323}
3324
17450429
PP
3325static const char *const longjmp_names[] =
3326 {
3327 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3328 };
3329#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3330
3331/* Per-objfile data private to breakpoint.c. */
3332struct breakpoint_objfile_data
3333{
3334 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3335 struct bound_minimal_symbol overlay_msym;
17450429
PP
3336
3337 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3338 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3339
28106bc2
SDJ
3340 /* True if we have looked for longjmp probes. */
3341 int longjmp_searched;
3342
3343 /* SystemTap probe points for longjmp (if any). */
3344 VEC (probe_p) *longjmp_probes;
3345
17450429 3346 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3347 struct bound_minimal_symbol terminate_msym;
17450429
PP
3348
3349 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3350 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3351
3352 /* True if we have looked for exception probes. */
3353 int exception_searched;
3354
3355 /* SystemTap probe points for unwinding (if any). */
3356 VEC (probe_p) *exception_probes;
17450429
PP
3357};
3358
3359static const struct objfile_data *breakpoint_objfile_key;
3360
3361/* Minimal symbol not found sentinel. */
3362static struct minimal_symbol msym_not_found;
3363
3364/* Returns TRUE if MSYM point to the "not found" sentinel. */
3365
3366static int
3367msym_not_found_p (const struct minimal_symbol *msym)
3368{
3369 return msym == &msym_not_found;
3370}
3371
3372/* Return per-objfile data needed by breakpoint.c.
3373 Allocate the data if necessary. */
3374
3375static struct breakpoint_objfile_data *
3376get_breakpoint_objfile_data (struct objfile *objfile)
3377{
3378 struct breakpoint_objfile_data *bp_objfile_data;
3379
3380 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3381 if (bp_objfile_data == NULL)
3382 {
3383 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3384 sizeof (*bp_objfile_data));
3385
3386 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3387 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3388 }
3389 return bp_objfile_data;
3390}
3391
28106bc2
SDJ
3392static void
3393free_breakpoint_probes (struct objfile *obj, void *data)
3394{
3395 struct breakpoint_objfile_data *bp_objfile_data = data;
3396
3397 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3398 VEC_free (probe_p, bp_objfile_data->exception_probes);
3399}
3400
e62c965a 3401static void
af02033e 3402create_overlay_event_breakpoint (void)
e62c965a 3403{
69de3c6a 3404 struct objfile *objfile;
af02033e 3405 const char *const func_name = "_ovly_debug_event";
e62c965a 3406
69de3c6a
PP
3407 ALL_OBJFILES (objfile)
3408 {
3409 struct breakpoint *b;
17450429
PP
3410 struct breakpoint_objfile_data *bp_objfile_data;
3411 CORE_ADDR addr;
69de3c6a 3412
17450429
PP
3413 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3414
3b7344d5 3415 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3416 continue;
3417
3b7344d5 3418 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3419 {
3b7344d5 3420 struct bound_minimal_symbol m;
17450429
PP
3421
3422 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3423 if (m.minsym == NULL)
17450429
PP
3424 {
3425 /* Avoid future lookups in this objfile. */
3b7344d5 3426 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3427 continue;
3428 }
3429 bp_objfile_data->overlay_msym = m;
3430 }
e62c965a 3431
77e371c0 3432 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3433 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3434 bp_overlay_event,
3435 &internal_breakpoint_ops);
69de3c6a 3436 b->addr_string = xstrdup (func_name);
e62c965a 3437
69de3c6a
PP
3438 if (overlay_debugging == ovly_auto)
3439 {
3440 b->enable_state = bp_enabled;
3441 overlay_events_enabled = 1;
3442 }
3443 else
3444 {
3445 b->enable_state = bp_disabled;
3446 overlay_events_enabled = 0;
3447 }
e62c965a 3448 }
44702360 3449 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3450}
3451
0fd8e87f 3452static void
af02033e 3453create_longjmp_master_breakpoint (void)
0fd8e87f 3454{
6c95b8df 3455 struct program_space *pspace;
6c95b8df
PA
3456 struct cleanup *old_chain;
3457
3458 old_chain = save_current_program_space ();
0fd8e87f 3459
6c95b8df 3460 ALL_PSPACES (pspace)
af02033e
PP
3461 {
3462 struct objfile *objfile;
3463
3464 set_current_program_space (pspace);
3465
3466 ALL_OBJFILES (objfile)
0fd8e87f 3467 {
af02033e
PP
3468 int i;
3469 struct gdbarch *gdbarch;
17450429 3470 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3471
af02033e 3472 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3473
17450429
PP
3474 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3475
28106bc2
SDJ
3476 if (!bp_objfile_data->longjmp_searched)
3477 {
25f9533e
SDJ
3478 VEC (probe_p) *ret;
3479
3480 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3481 if (ret != NULL)
3482 {
3483 /* We are only interested in checking one element. */
3484 struct probe *p = VEC_index (probe_p, ret, 0);
3485
3486 if (!can_evaluate_probe_arguments (p))
3487 {
3488 /* We cannot use the probe interface here, because it does
3489 not know how to evaluate arguments. */
3490 VEC_free (probe_p, ret);
3491 ret = NULL;
3492 }
3493 }
3494 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3495 bp_objfile_data->longjmp_searched = 1;
3496 }
3497
3498 if (bp_objfile_data->longjmp_probes != NULL)
3499 {
3500 int i;
3501 struct probe *probe;
3502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3503
3504 for (i = 0;
3505 VEC_iterate (probe_p,
3506 bp_objfile_data->longjmp_probes,
3507 i, probe);
3508 ++i)
3509 {
3510 struct breakpoint *b;
3511
729662a5
TT
3512 b = create_internal_breakpoint (gdbarch,
3513 get_probe_address (probe,
3514 objfile),
28106bc2
SDJ
3515 bp_longjmp_master,
3516 &internal_breakpoint_ops);
3517 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3518 b->enable_state = bp_disabled;
3519 }
3520
3521 continue;
3522 }
3523
0569175e
TSD
3524 if (!gdbarch_get_longjmp_target_p (gdbarch))
3525 continue;
3526
17450429 3527 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3528 {
3529 struct breakpoint *b;
af02033e 3530 const char *func_name;
17450429 3531 CORE_ADDR addr;
6c95b8df 3532
3b7344d5 3533 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3534 continue;
0fd8e87f 3535
17450429 3536 func_name = longjmp_names[i];
3b7344d5 3537 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3538 {
3b7344d5 3539 struct bound_minimal_symbol m;
17450429
PP
3540
3541 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3542 if (m.minsym == NULL)
17450429
PP
3543 {
3544 /* Prevent future lookups in this objfile. */
3b7344d5 3545 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3546 continue;
3547 }
3548 bp_objfile_data->longjmp_msym[i] = m;
3549 }
3550
77e371c0 3551 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3552 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3553 &internal_breakpoint_ops);
af02033e
PP
3554 b->addr_string = xstrdup (func_name);
3555 b->enable_state = bp_disabled;
3556 }
0fd8e87f 3557 }
af02033e 3558 }
44702360 3559 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3560
3561 do_cleanups (old_chain);
0fd8e87f
UW
3562}
3563
af02033e 3564/* Create a master std::terminate breakpoint. */
aa7d318d 3565static void
af02033e 3566create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3567{
3568 struct program_space *pspace;
aa7d318d 3569 struct cleanup *old_chain;
af02033e 3570 const char *const func_name = "std::terminate()";
aa7d318d
TT
3571
3572 old_chain = save_current_program_space ();
3573
3574 ALL_PSPACES (pspace)
17450429
PP
3575 {
3576 struct objfile *objfile;
3577 CORE_ADDR addr;
3578
3579 set_current_program_space (pspace);
3580
aa7d318d
TT
3581 ALL_OBJFILES (objfile)
3582 {
3583 struct breakpoint *b;
17450429 3584 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3585
17450429 3586 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3587
3b7344d5 3588 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3589 continue;
3590
3b7344d5 3591 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3592 {
3b7344d5 3593 struct bound_minimal_symbol m;
17450429
PP
3594
3595 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3596 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3597 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3598 {
3599 /* Prevent future lookups in this objfile. */
3b7344d5 3600 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3601 continue;
3602 }
3603 bp_objfile_data->terminate_msym = m;
3604 }
aa7d318d 3605
77e371c0 3606 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3607 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3608 bp_std_terminate_master,
3609 &internal_breakpoint_ops);
aa7d318d
TT
3610 b->addr_string = xstrdup (func_name);
3611 b->enable_state = bp_disabled;
3612 }
17450429
PP
3613 }
3614
44702360 3615 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3616
3617 do_cleanups (old_chain);
3618}
3619
186c406b
TT
3620/* Install a master breakpoint on the unwinder's debug hook. */
3621
70221824 3622static void
186c406b
TT
3623create_exception_master_breakpoint (void)
3624{
3625 struct objfile *objfile;
17450429 3626 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3627
3628 ALL_OBJFILES (objfile)
3629 {
17450429
PP
3630 struct breakpoint *b;
3631 struct gdbarch *gdbarch;
3632 struct breakpoint_objfile_data *bp_objfile_data;
3633 CORE_ADDR addr;
3634
3635 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3636
28106bc2
SDJ
3637 /* We prefer the SystemTap probe point if it exists. */
3638 if (!bp_objfile_data->exception_searched)
3639 {
25f9533e
SDJ
3640 VEC (probe_p) *ret;
3641
3642 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3643
3644 if (ret != NULL)
3645 {
3646 /* We are only interested in checking one element. */
3647 struct probe *p = VEC_index (probe_p, ret, 0);
3648
3649 if (!can_evaluate_probe_arguments (p))
3650 {
3651 /* We cannot use the probe interface here, because it does
3652 not know how to evaluate arguments. */
3653 VEC_free (probe_p, ret);
3654 ret = NULL;
3655 }
3656 }
3657 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3658 bp_objfile_data->exception_searched = 1;
3659 }
3660
3661 if (bp_objfile_data->exception_probes != NULL)
3662 {
3663 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3664 int i;
3665 struct probe *probe;
3666
3667 for (i = 0;
3668 VEC_iterate (probe_p,
3669 bp_objfile_data->exception_probes,
3670 i, probe);
3671 ++i)
3672 {
3673 struct breakpoint *b;
3674
729662a5
TT
3675 b = create_internal_breakpoint (gdbarch,
3676 get_probe_address (probe,
3677 objfile),
28106bc2
SDJ
3678 bp_exception_master,
3679 &internal_breakpoint_ops);
3680 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3681 b->enable_state = bp_disabled;
3682 }
3683
3684 continue;
3685 }
3686
3687 /* Otherwise, try the hook function. */
3688
3b7344d5 3689 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3690 continue;
3691
3692 gdbarch = get_objfile_arch (objfile);
186c406b 3693
3b7344d5 3694 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3695 {
3b7344d5 3696 struct bound_minimal_symbol debug_hook;
186c406b 3697
17450429 3698 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3699 if (debug_hook.minsym == NULL)
17450429 3700 {
3b7344d5 3701 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3702 continue;
3703 }
3704
3705 bp_objfile_data->exception_msym = debug_hook;
186c406b 3706 }
17450429 3707
77e371c0 3708 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3709 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3710 &current_target);
06edf0c0
PA
3711 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3712 &internal_breakpoint_ops);
17450429
PP
3713 b->addr_string = xstrdup (func_name);
3714 b->enable_state = bp_disabled;
186c406b
TT
3715 }
3716
44702360 3717 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3718}
3719
c906108c 3720void
fba45db2 3721update_breakpoints_after_exec (void)
c906108c 3722{
35df4500 3723 struct breakpoint *b, *b_tmp;
876fa593 3724 struct bp_location *bploc, **bplocp_tmp;
c906108c 3725
25b22b0a
PA
3726 /* We're about to delete breakpoints from GDB's lists. If the
3727 INSERTED flag is true, GDB will try to lift the breakpoints by
3728 writing the breakpoints' "shadow contents" back into memory. The
3729 "shadow contents" are NOT valid after an exec, so GDB should not
3730 do that. Instead, the target is responsible from marking
3731 breakpoints out as soon as it detects an exec. We don't do that
3732 here instead, because there may be other attempts to delete
3733 breakpoints after detecting an exec and before reaching here. */
876fa593 3734 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3735 if (bploc->pspace == current_program_space)
3736 gdb_assert (!bploc->inserted);
c906108c 3737
35df4500 3738 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3739 {
6c95b8df
PA
3740 if (b->pspace != current_program_space)
3741 continue;
3742
4a64f543 3743 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3744 if (b->type == bp_shlib_event)
3745 {
3746 delete_breakpoint (b);
3747 continue;
3748 }
c906108c 3749
4a64f543 3750 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3751 if (b->type == bp_jit_event)
3752 {
3753 delete_breakpoint (b);
3754 continue;
3755 }
3756
1900040c 3757 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3758 as must overlay event and longjmp master breakpoints. */
3759 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3760 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3761 || b->type == bp_exception_master)
c4093a6a
JM
3762 {
3763 delete_breakpoint (b);
3764 continue;
3765 }
3766
4a64f543 3767 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3768 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3769 {
3770 delete_breakpoint (b);
3771 continue;
3772 }
3773
7c16b83e
PA
3774 /* Just like single-step breakpoints. */
3775 if (b->type == bp_single_step)
3776 {
3777 delete_breakpoint (b);
3778 continue;
3779 }
3780
611c83ae
PA
3781 /* Longjmp and longjmp-resume breakpoints are also meaningless
3782 after an exec. */
186c406b 3783 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3784 || b->type == bp_longjmp_call_dummy
186c406b 3785 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3786 {
3787 delete_breakpoint (b);
3788 continue;
3789 }
3790
ce78b96d
JB
3791 if (b->type == bp_catchpoint)
3792 {
3793 /* For now, none of the bp_catchpoint breakpoints need to
3794 do anything at this point. In the future, if some of
3795 the catchpoints need to something, we will need to add
3796 a new method, and call this method from here. */
3797 continue;
3798 }
3799
c5aa993b
JM
3800 /* bp_finish is a special case. The only way we ought to be able
3801 to see one of these when an exec() has happened, is if the user
3802 caught a vfork, and then said "finish". Ordinarily a finish just
3803 carries them to the call-site of the current callee, by setting
3804 a temporary bp there and resuming. But in this case, the finish
3805 will carry them entirely through the vfork & exec.
3806
3807 We don't want to allow a bp_finish to remain inserted now. But
3808 we can't safely delete it, 'cause finish_command has a handle to
3809 the bp on a bpstat, and will later want to delete it. There's a
3810 chance (and I've seen it happen) that if we delete the bp_finish
3811 here, that its storage will get reused by the time finish_command
3812 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3813 We really must allow finish_command to delete a bp_finish.
3814
e5dd4106 3815 In the absence of a general solution for the "how do we know
53a5351d
JM
3816 it's safe to delete something others may have handles to?"
3817 problem, what we'll do here is just uninsert the bp_finish, and
3818 let finish_command delete it.
3819
3820 (We know the bp_finish is "doomed" in the sense that it's
3821 momentary, and will be deleted as soon as finish_command sees
3822 the inferior stopped. So it doesn't matter that the bp's
3823 address is probably bogus in the new a.out, unlike e.g., the
3824 solib breakpoints.) */
c5aa993b 3825
c5aa993b
JM
3826 if (b->type == bp_finish)
3827 {
3828 continue;
3829 }
3830
3831 /* Without a symbolic address, we have little hope of the
3832 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3833 a.out. */
c5aa993b
JM
3834 if (b->addr_string == NULL)
3835 {
3836 delete_breakpoint (b);
3837 continue;
3838 }
c5aa993b 3839 }
c906108c
SS
3840}
3841
3842int
d80ee84f 3843detach_breakpoints (ptid_t ptid)
c906108c 3844{
35df4500 3845 struct bp_location *bl, **blp_tmp;
3a1bae8e 3846 int val = 0;
ce696e05 3847 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3848 struct inferior *inf = current_inferior ();
c5aa993b 3849
dfd4cc63 3850 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3851 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3852
6c95b8df 3853 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3854 inferior_ptid = ptid;
35df4500 3855 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3856 {
35df4500 3857 if (bl->pspace != inf->pspace)
6c95b8df
PA
3858 continue;
3859
bd9673a4
PW
3860 /* This function must physically remove breakpoints locations
3861 from the specified ptid, without modifying the breakpoint
3862 package's state. Locations of type bp_loc_other are only
3863 maintained at GDB side. So, there is no need to remove
3864 these bp_loc_other locations. Moreover, removing these
3865 would modify the breakpoint package's state. */
3866 if (bl->loc_type == bp_loc_other)
3867 continue;
3868
35df4500
TJB
3869 if (bl->inserted)
3870 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3871 }
d03285ec 3872
ce696e05 3873 do_cleanups (old_chain);
3a1bae8e 3874 return val;
c906108c
SS
3875}
3876
35df4500 3877/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3878 Note that this is used to detach breakpoints from a child fork.
3879 When we get here, the child isn't in the inferior list, and neither
3880 do we have objects to represent its address space --- we should
35df4500 3881 *not* look at bl->pspace->aspace here. */
6c95b8df 3882
c906108c 3883static int
35df4500 3884remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3885{
3886 int val;
c5aa993b 3887
35df4500
TJB
3888 /* BL is never in moribund_locations by our callers. */
3889 gdb_assert (bl->owner != NULL);
2bdf28a0 3890
35df4500 3891 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3892 /* Permanent breakpoints cannot be inserted or removed. */
3893 return 0;
3894
74960c60
VP
3895 /* The type of none suggests that owner is actually deleted.
3896 This should not ever happen. */
35df4500 3897 gdb_assert (bl->owner->type != bp_none);
0bde7532 3898
35df4500
TJB
3899 if (bl->loc_type == bp_loc_software_breakpoint
3900 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3901 {
c02f5703
MS
3902 /* "Normal" instruction breakpoint: either the standard
3903 trap-instruction bp (bp_breakpoint), or a
3904 bp_hardware_breakpoint. */
3905
3906 /* First check to see if we have to handle an overlay. */
3907 if (overlay_debugging == ovly_off
35df4500
TJB
3908 || bl->section == NULL
3909 || !(section_is_overlay (bl->section)))
c02f5703
MS
3910 {
3911 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3912
3913 /* If we're trying to uninsert a memory breakpoint that we
3914 know is set in a dynamic object that is marked
3915 shlib_disabled, then either the dynamic object was
3916 removed with "remove-symbol-file" or with
3917 "nosharedlibrary". In the former case, we don't know
3918 whether another dynamic object might have loaded over the
3919 breakpoint's address -- the user might well let us know
3920 about it next with add-symbol-file (the whole point of
d03de421 3921 add-symbol-file is letting the user manually maintain a
08351840
PA
3922 list of dynamically loaded objects). If we have the
3923 breakpoint's shadow memory, that is, this is a software
3924 breakpoint managed by GDB, check whether the breakpoint
3925 is still inserted in memory, to avoid overwriting wrong
3926 code with stale saved shadow contents. Note that HW
3927 breakpoints don't have shadow memory, as they're
3928 implemented using a mechanism that is not dependent on
3929 being able to modify the target's memory, and as such
3930 they should always be removed. */
3931 if (bl->shlib_disabled
3932 && bl->target_info.shadow_len != 0
3933 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3934 val = 0;
3935 else
3936 val = bl->owner->ops->remove_location (bl);
c02f5703 3937 }
c906108c
SS
3938 else
3939 {
4a64f543 3940 /* This breakpoint is in an overlay section.
c02f5703
MS
3941 Did we set a breakpoint at the LMA? */
3942 if (!overlay_events_enabled)
3943 {
3944 /* Yes -- overlay event support is not active, so we
3945 should have set a breakpoint at the LMA. Remove it.
3946 */
c02f5703
MS
3947 /* Ignore any failures: if the LMA is in ROM, we will
3948 have already warned when we failed to insert it. */
35df4500
TJB
3949 if (bl->loc_type == bp_loc_hardware_breakpoint)
3950 target_remove_hw_breakpoint (bl->gdbarch,
3951 &bl->overlay_target_info);
c02f5703 3952 else
35df4500
TJB
3953 target_remove_breakpoint (bl->gdbarch,
3954 &bl->overlay_target_info);
c02f5703
MS
3955 }
3956 /* Did we set a breakpoint at the VMA?
3957 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3958 if (bl->inserted)
c906108c 3959 {
c02f5703
MS
3960 /* Yes -- remove it. Previously we did not bother to
3961 remove the breakpoint if the section had been
3962 unmapped, but let's not rely on that being safe. We
3963 don't know what the overlay manager might do. */
aa67235e
UW
3964
3965 /* However, we should remove *software* breakpoints only
3966 if the section is still mapped, or else we overwrite
3967 wrong code with the saved shadow contents. */
348d480f
PA
3968 if (bl->loc_type == bp_loc_hardware_breakpoint
3969 || section_is_mapped (bl->section))
3970 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3971 else
3972 val = 0;
c906108c 3973 }
c02f5703
MS
3974 else
3975 {
3976 /* No -- not inserted, so no need to remove. No error. */
3977 val = 0;
3978 }
c906108c 3979 }
879d1e6b 3980
08351840
PA
3981 /* In some cases, we might not be able to remove a breakpoint in
3982 a shared library that has already been removed, but we have
3983 not yet processed the shlib unload event. Similarly for an
3984 unloaded add-symbol-file object - the user might not yet have
3985 had the chance to remove-symbol-file it. shlib_disabled will
3986 be set if the library/object has already been removed, but
3987 the breakpoint hasn't been uninserted yet, e.g., after
3988 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3989 always-inserted mode. */
076855f9 3990 if (val
08351840
PA
3991 && (bl->loc_type == bp_loc_software_breakpoint
3992 && (bl->shlib_disabled
3993 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3994 || shared_objfile_contains_address_p (bl->pspace,
3995 bl->address))))
879d1e6b
UW
3996 val = 0;
3997
c906108c
SS
3998 if (val)
3999 return val;
35df4500 4000 bl->inserted = (is == mark_inserted);
c906108c 4001 }
35df4500 4002 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4003 {
77b06cd7
TJB
4004 gdb_assert (bl->owner->ops != NULL
4005 && bl->owner->ops->remove_location != NULL);
4006
35df4500 4007 bl->inserted = (is == mark_inserted);
77b06cd7 4008 bl->owner->ops->remove_location (bl);
2e70b7b9 4009
c906108c 4010 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4011 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4012 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4013 bl->owner->number);
c906108c 4014 }
35df4500
TJB
4015 else if (bl->owner->type == bp_catchpoint
4016 && breakpoint_enabled (bl->owner)
4017 && !bl->duplicate)
ce78b96d 4018 {
77b06cd7
TJB
4019 gdb_assert (bl->owner->ops != NULL
4020 && bl->owner->ops->remove_location != NULL);
ce78b96d 4021
77b06cd7 4022 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4023 if (val)
4024 return val;
77b06cd7 4025
35df4500 4026 bl->inserted = (is == mark_inserted);
ce78b96d 4027 }
c906108c
SS
4028
4029 return 0;
4030}
4031
6c95b8df 4032static int
35df4500 4033remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4034{
4035 int ret;
4036 struct cleanup *old_chain;
4037
35df4500
TJB
4038 /* BL is never in moribund_locations by our callers. */
4039 gdb_assert (bl->owner != NULL);
2bdf28a0 4040
35df4500 4041 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4042 /* Permanent breakpoints cannot be inserted or removed. */
4043 return 0;
4044
4045 /* The type of none suggests that owner is actually deleted.
4046 This should not ever happen. */
35df4500 4047 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4048
4049 old_chain = save_current_space_and_thread ();
4050
35df4500 4051 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4052
35df4500 4053 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4054
4055 do_cleanups (old_chain);
4056 return ret;
4057}
4058
c906108c
SS
4059/* Clear the "inserted" flag in all breakpoints. */
4060
25b22b0a 4061void
fba45db2 4062mark_breakpoints_out (void)
c906108c 4063{
35df4500 4064 struct bp_location *bl, **blp_tmp;
c906108c 4065
35df4500
TJB
4066 ALL_BP_LOCATIONS (bl, blp_tmp)
4067 if (bl->pspace == current_program_space)
4068 bl->inserted = 0;
c906108c
SS
4069}
4070
53a5351d
JM
4071/* Clear the "inserted" flag in all breakpoints and delete any
4072 breakpoints which should go away between runs of the program.
c906108c
SS
4073
4074 Plus other such housekeeping that has to be done for breakpoints
4075 between runs.
4076
53a5351d
JM
4077 Note: this function gets called at the end of a run (by
4078 generic_mourn_inferior) and when a run begins (by
4a64f543 4079 init_wait_for_inferior). */
c906108c
SS
4080
4081
4082
4083void
fba45db2 4084breakpoint_init_inferior (enum inf_context context)
c906108c 4085{
35df4500
TJB
4086 struct breakpoint *b, *b_tmp;
4087 struct bp_location *bl, **blp_tmp;
1c5cfe86 4088 int ix;
6c95b8df 4089 struct program_space *pspace = current_program_space;
c906108c 4090
50c71eaf
PA
4091 /* If breakpoint locations are shared across processes, then there's
4092 nothing to do. */
f5656ead 4093 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4094 return;
4095
35df4500 4096 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4097 {
35df4500
TJB
4098 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4099 if (bl->pspace == pspace
4100 && bl->owner->enable_state != bp_permanent)
4101 bl->inserted = 0;
6c95b8df 4102 }
075f6582 4103
35df4500 4104 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4105 {
6c95b8df
PA
4106 if (b->loc && b->loc->pspace != pspace)
4107 continue;
4108
c5aa993b
JM
4109 switch (b->type)
4110 {
4111 case bp_call_dummy:
e2e4d78b 4112 case bp_longjmp_call_dummy:
c906108c 4113
c5aa993b 4114 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4115 cause problems when the inferior is rerun, so we better get
4116 rid of it. */
4117
4118 case bp_watchpoint_scope:
4119
4120 /* Also get rid of scope breakpoints. */
4121
4122 case bp_shlib_event:
4123
4124 /* Also remove solib event breakpoints. Their addresses may
4125 have changed since the last time we ran the program.
4126 Actually we may now be debugging against different target;
4127 and so the solib backend that installed this breakpoint may
4128 not be used in by the target. E.g.,
4129
4130 (gdb) file prog-linux
4131 (gdb) run # native linux target
4132 ...
4133 (gdb) kill
4134 (gdb) file prog-win.exe
4135 (gdb) tar rem :9999 # remote Windows gdbserver.
4136 */
c906108c 4137
f59f708a
PA
4138 case bp_step_resume:
4139
4140 /* Also remove step-resume breakpoints. */
4141
7c16b83e
PA
4142 case bp_single_step:
4143
4144 /* Also remove single-step breakpoints. */
4145
c5aa993b
JM
4146 delete_breakpoint (b);
4147 break;
c906108c 4148
c5aa993b
JM
4149 case bp_watchpoint:
4150 case bp_hardware_watchpoint:
4151 case bp_read_watchpoint:
4152 case bp_access_watchpoint:
3a5c3e22
PA
4153 {
4154 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4155
3a5c3e22
PA
4156 /* Likewise for watchpoints on local expressions. */
4157 if (w->exp_valid_block != NULL)
4158 delete_breakpoint (b);
4159 else if (context == inf_starting)
4160 {
4161 /* Reset val field to force reread of starting value in
4162 insert_breakpoints. */
4163 if (w->val)
4164 value_free (w->val);
4165 w->val = NULL;
4166 w->val_valid = 0;
c860120c 4167 }
3a5c3e22 4168 }
c5aa993b
JM
4169 break;
4170 default:
c5aa993b
JM
4171 break;
4172 }
4173 }
1c5cfe86
PA
4174
4175 /* Get rid of the moribund locations. */
35df4500
TJB
4176 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4177 decref_bp_location (&bl);
1c5cfe86 4178 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4179}
4180
6c95b8df
PA
4181/* These functions concern about actual breakpoints inserted in the
4182 target --- to e.g. check if we need to do decr_pc adjustment or if
4183 we need to hop over the bkpt --- so we check for address space
4184 match, not program space. */
4185
c2c6d25f
JM
4186/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4187 exists at PC. It returns ordinary_breakpoint_here if it's an
4188 ordinary breakpoint, or permanent_breakpoint_here if it's a
4189 permanent breakpoint.
4190 - When continuing from a location with an ordinary breakpoint, we
4191 actually single step once before calling insert_breakpoints.
e5dd4106 4192 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4193 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4194 the target, to advance the PC past the breakpoint. */
c906108c 4195
c2c6d25f 4196enum breakpoint_here
6c95b8df 4197breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4198{
35df4500 4199 struct bp_location *bl, **blp_tmp;
c2c6d25f 4200 int any_breakpoint_here = 0;
c906108c 4201
35df4500 4202 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4203 {
35df4500
TJB
4204 if (bl->loc_type != bp_loc_software_breakpoint
4205 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4206 continue;
4207
f1310107 4208 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4209 if ((breakpoint_enabled (bl->owner)
4210 || bl->owner->enable_state == bp_permanent)
f1310107 4211 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4212 {
4213 if (overlay_debugging
35df4500
TJB
4214 && section_is_overlay (bl->section)
4215 && !section_is_mapped (bl->section))
075f6582 4216 continue; /* unmapped overlay -- can't be a match */
35df4500 4217 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4218 return permanent_breakpoint_here;
4219 else
4220 any_breakpoint_here = 1;
4221 }
4222 }
c906108c 4223
c2c6d25f 4224 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4225}
4226
1c5cfe86
PA
4227/* Return true if there's a moribund breakpoint at PC. */
4228
4229int
6c95b8df 4230moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4231{
4232 struct bp_location *loc;
4233 int ix;
4234
4235 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4236 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4237 return 1;
4238
4239 return 0;
4240}
c2c6d25f 4241
a1fd2fa5 4242/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4243
4244int
a1fd2fa5 4245breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4246{
35df4500 4247 struct bp_location *bl, **blp_tmp;
c906108c 4248
35df4500 4249 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4250 {
35df4500
TJB
4251 if (bl->loc_type != bp_loc_software_breakpoint
4252 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4253 continue;
4254
35df4500 4255 if (bl->inserted
f1310107 4256 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4257 {
4258 if (overlay_debugging
35df4500
TJB
4259 && section_is_overlay (bl->section)
4260 && !section_is_mapped (bl->section))
075f6582
DJ
4261 continue; /* unmapped overlay -- can't be a match */
4262 else
4263 return 1;
4264 }
c5aa993b 4265 }
c36b740a
VP
4266 return 0;
4267}
4268
a1fd2fa5
PA
4269/* This function returns non-zero iff there is a software breakpoint
4270 inserted at PC. */
c36b740a
VP
4271
4272int
a1fd2fa5
PA
4273software_breakpoint_inserted_here_p (struct address_space *aspace,
4274 CORE_ADDR pc)
4fa8626c 4275{
35df4500 4276 struct bp_location *bl, **blp_tmp;
4fa8626c 4277
35df4500 4278 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4279 {
35df4500 4280 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4281 continue;
4282
35df4500
TJB
4283 if (bl->inserted
4284 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4285 aspace, pc))
4fa8626c
DJ
4286 {
4287 if (overlay_debugging
35df4500
TJB
4288 && section_is_overlay (bl->section)
4289 && !section_is_mapped (bl->section))
4fa8626c
DJ
4290 continue; /* unmapped overlay -- can't be a match */
4291 else
a1fd2fa5 4292 return 1;
4fa8626c
DJ
4293 }
4294 }
4295
4296 return 0;
4297}
4298
9093389c
PA
4299int
4300hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4301 CORE_ADDR addr, ULONGEST len)
4302{
4303 struct breakpoint *bpt;
4304
4305 ALL_BREAKPOINTS (bpt)
4306 {
4307 struct bp_location *loc;
4308
4309 if (bpt->type != bp_hardware_watchpoint
4310 && bpt->type != bp_access_watchpoint)
4311 continue;
4312
4313 if (!breakpoint_enabled (bpt))
4314 continue;
4315
4316 for (loc = bpt->loc; loc; loc = loc->next)
4317 if (loc->pspace->aspace == aspace && loc->inserted)
4318 {
4319 CORE_ADDR l, h;
4320
4321 /* Check for intersection. */
4322 l = max (loc->address, addr);
4323 h = min (loc->address + loc->length, addr + len);
4324 if (l < h)
4325 return 1;
4326 }
4327 }
4328 return 0;
4329}
4330
075f6582
DJ
4331/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4332 PC is valid for process/thread PTID. */
c906108c
SS
4333
4334int
6c95b8df
PA
4335breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4336 ptid_t ptid)
c906108c 4337{
35df4500 4338 struct bp_location *bl, **blp_tmp;
4a306c9a 4339 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4340 int thread = -1;
4a306c9a 4341 int task = 0;
a6f1cd96 4342
35df4500 4343 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4344 {
35df4500
TJB
4345 if (bl->loc_type != bp_loc_software_breakpoint
4346 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4347 continue;
4348
35df4500
TJB
4349 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4350 if (!breakpoint_enabled (bl->owner)
4351 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4352 continue;
4353
f1310107 4354 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4355 continue;
4356
35df4500 4357 if (bl->owner->thread != -1)
075f6582 4358 {
a6f1cd96
JB
4359 /* This is a thread-specific breakpoint. Check that ptid
4360 matches that thread. If thread hasn't been computed yet,
4361 it is now time to do so. */
4362 if (thread == -1)
4363 thread = pid_to_thread_id (ptid);
35df4500 4364 if (bl->owner->thread != thread)
a6f1cd96 4365 continue;
075f6582 4366 }
a6f1cd96 4367
35df4500 4368 if (bl->owner->task != 0)
4a306c9a
JB
4369 {
4370 /* This is a task-specific breakpoint. Check that ptid
4371 matches that task. If task hasn't been computed yet,
4372 it is now time to do so. */
4373 if (task == 0)
4374 task = ada_get_task_number (ptid);
35df4500 4375 if (bl->owner->task != task)
4a306c9a
JB
4376 continue;
4377 }
4378
a6f1cd96 4379 if (overlay_debugging
35df4500
TJB
4380 && section_is_overlay (bl->section)
4381 && !section_is_mapped (bl->section))
a6f1cd96
JB
4382 continue; /* unmapped overlay -- can't be a match */
4383
4384 return 1;
c5aa993b 4385 }
c906108c
SS
4386
4387 return 0;
4388}
c906108c 4389\f
c5aa993b 4390
c906108c
SS
4391/* bpstat stuff. External routines' interfaces are documented
4392 in breakpoint.h. */
4393
4394int
c326b90e 4395is_catchpoint (struct breakpoint *ep)
c906108c 4396{
533be4dd 4397 return (ep->type == bp_catchpoint);
c906108c
SS
4398}
4399
f431efe5
PA
4400/* Frees any storage that is part of a bpstat. Does not walk the
4401 'next' chain. */
4402
4403static void
198757a8
VP
4404bpstat_free (bpstat bs)
4405{
4406 if (bs->old_val != NULL)
4407 value_free (bs->old_val);
9add0f1b 4408 decref_counted_command_line (&bs->commands);
f431efe5 4409 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4410 xfree (bs);
4411}
4412
c906108c
SS
4413/* Clear a bpstat so that it says we are not at any breakpoint.
4414 Also free any storage that is part of a bpstat. */
4415
4416void
fba45db2 4417bpstat_clear (bpstat *bsp)
c906108c
SS
4418{
4419 bpstat p;
4420 bpstat q;
4421
4422 if (bsp == 0)
4423 return;
4424 p = *bsp;
4425 while (p != NULL)
4426 {
4427 q = p->next;
198757a8 4428 bpstat_free (p);
c906108c
SS
4429 p = q;
4430 }
4431 *bsp = NULL;
4432}
4433
4434/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4435 is part of the bpstat is copied as well. */
4436
4437bpstat
fba45db2 4438bpstat_copy (bpstat bs)
c906108c
SS
4439{
4440 bpstat p = NULL;
4441 bpstat tmp;
4442 bpstat retval = NULL;
4443
4444 if (bs == NULL)
4445 return bs;
4446
4447 for (; bs != NULL; bs = bs->next)
4448 {
4449 tmp = (bpstat) xmalloc (sizeof (*tmp));
4450 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4451 incref_counted_command_line (tmp->commands);
f431efe5 4452 incref_bp_location (tmp->bp_location_at);
31cc81e9 4453 if (bs->old_val != NULL)
3c3185ac
JK
4454 {
4455 tmp->old_val = value_copy (bs->old_val);
4456 release_value (tmp->old_val);
4457 }
31cc81e9 4458
c906108c
SS
4459 if (p == NULL)
4460 /* This is the first thing in the chain. */
4461 retval = tmp;
4462 else
4463 p->next = tmp;
4464 p = tmp;
4465 }
4466 p->next = NULL;
4467 return retval;
4468}
4469
4a64f543 4470/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4471
4472bpstat
fba45db2 4473bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4474{
c5aa993b
JM
4475 if (bsp == NULL)
4476 return NULL;
c906108c 4477
c5aa993b
JM
4478 for (; bsp != NULL; bsp = bsp->next)
4479 {
f431efe5 4480 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4481 return bsp;
4482 }
c906108c
SS
4483 return NULL;
4484}
4485
ab04a2af
TT
4486/* See breakpoint.h. */
4487
47591c29 4488int
427cd150 4489bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4490{
ab04a2af
TT
4491 for (; bsp != NULL; bsp = bsp->next)
4492 {
427cd150
TT
4493 if (bsp->breakpoint_at == NULL)
4494 {
4495 /* A moribund location can never explain a signal other than
4496 GDB_SIGNAL_TRAP. */
4497 if (sig == GDB_SIGNAL_TRAP)
47591c29 4498 return 1;
427cd150
TT
4499 }
4500 else
47591c29
PA
4501 {
4502 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4503 sig))
4504 return 1;
4505 }
ab04a2af
TT
4506 }
4507
47591c29 4508 return 0;
ab04a2af
TT
4509}
4510
4a64f543
MS
4511/* Put in *NUM the breakpoint number of the first breakpoint we are
4512 stopped at. *BSP upon return is a bpstat which points to the
4513 remaining breakpoints stopped at (but which is not guaranteed to be
4514 good for anything but further calls to bpstat_num).
4515
8671a17b
PA
4516 Return 0 if passed a bpstat which does not indicate any breakpoints.
4517 Return -1 if stopped at a breakpoint that has been deleted since
4518 we set it.
4519 Return 1 otherwise. */
c906108c
SS
4520
4521int
8671a17b 4522bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4523{
4524 struct breakpoint *b;
4525
4526 if ((*bsp) == NULL)
4527 return 0; /* No more breakpoint values */
8671a17b 4528
4a64f543
MS
4529 /* We assume we'll never have several bpstats that correspond to a
4530 single breakpoint -- otherwise, this function might return the
4531 same number more than once and this will look ugly. */
f431efe5 4532 b = (*bsp)->breakpoint_at;
8671a17b
PA
4533 *bsp = (*bsp)->next;
4534 if (b == NULL)
4535 return -1; /* breakpoint that's been deleted since */
4536
4537 *num = b->number; /* We have its number */
4538 return 1;
c906108c
SS
4539}
4540
e93ca019 4541/* See breakpoint.h. */
c906108c
SS
4542
4543void
e93ca019 4544bpstat_clear_actions (void)
c906108c 4545{
e93ca019
JK
4546 struct thread_info *tp;
4547 bpstat bs;
4548
4549 if (ptid_equal (inferior_ptid, null_ptid))
4550 return;
4551
4552 tp = find_thread_ptid (inferior_ptid);
4553 if (tp == NULL)
4554 return;
4555
4556 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4557 {
9add0f1b 4558 decref_counted_command_line (&bs->commands);
abf85f46 4559
c906108c
SS
4560 if (bs->old_val != NULL)
4561 {
4562 value_free (bs->old_val);
4563 bs->old_val = NULL;
4564 }
4565 }
4566}
4567
f3b1572e
PA
4568/* Called when a command is about to proceed the inferior. */
4569
4570static void
4571breakpoint_about_to_proceed (void)
4572{
4573 if (!ptid_equal (inferior_ptid, null_ptid))
4574 {
4575 struct thread_info *tp = inferior_thread ();
4576
4577 /* Allow inferior function calls in breakpoint commands to not
4578 interrupt the command list. When the call finishes
4579 successfully, the inferior will be standing at the same
4580 breakpoint as if nothing happened. */
16c381f0 4581 if (tp->control.in_infcall)
f3b1572e
PA
4582 return;
4583 }
4584
4585 breakpoint_proceeded = 1;
4586}
4587
4a64f543
MS
4588/* Stub for cleaning up our state if we error-out of a breakpoint
4589 command. */
c906108c 4590static void
4efb68b1 4591cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4592{
4593 executing_breakpoint_commands = 0;
4594}
4595
abf85f46
JK
4596/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4597 or its equivalent. */
4598
4599static int
4600command_line_is_silent (struct command_line *cmd)
4601{
4602 return cmd && (strcmp ("silent", cmd->line) == 0
4603 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4604}
4605
4a64f543
MS
4606/* Execute all the commands associated with all the breakpoints at
4607 this location. Any of these commands could cause the process to
4608 proceed beyond this point, etc. We look out for such changes by
4609 checking the global "breakpoint_proceeded" after each command.
c906108c 4610
347bddb7
PA
4611 Returns true if a breakpoint command resumed the inferior. In that
4612 case, it is the caller's responsibility to recall it again with the
4613 bpstat of the current thread. */
4614
4615static int
4616bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4617{
4618 bpstat bs;
4619 struct cleanup *old_chain;
347bddb7 4620 int again = 0;
c906108c
SS
4621
4622 /* Avoid endless recursion if a `source' command is contained
4623 in bs->commands. */
4624 if (executing_breakpoint_commands)
347bddb7 4625 return 0;
c906108c
SS
4626
4627 executing_breakpoint_commands = 1;
4628 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4629
cf6c5ffb
TT
4630 prevent_dont_repeat ();
4631
4a64f543 4632 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4633 bs = *bsp;
4634
4635 breakpoint_proceeded = 0;
4636 for (; bs != NULL; bs = bs->next)
4637 {
9add0f1b 4638 struct counted_command_line *ccmd;
6c50ab1c
JB
4639 struct command_line *cmd;
4640 struct cleanup *this_cmd_tree_chain;
4641
4642 /* Take ownership of the BSP's command tree, if it has one.
4643
4644 The command tree could legitimately contain commands like
4645 'step' and 'next', which call clear_proceed_status, which
4646 frees stop_bpstat's command tree. To make sure this doesn't
4647 free the tree we're executing out from under us, we need to
4648 take ownership of the tree ourselves. Since a given bpstat's
4649 commands are only executed once, we don't need to copy it; we
4650 can clear the pointer in the bpstat, and make sure we free
4651 the tree when we're done. */
9add0f1b
TT
4652 ccmd = bs->commands;
4653 bs->commands = NULL;
abf85f46
JK
4654 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4655 cmd = ccmd ? ccmd->commands : NULL;
4656 if (command_line_is_silent (cmd))
4657 {
4658 /* The action has been already done by bpstat_stop_status. */
4659 cmd = cmd->next;
4660 }
6c50ab1c 4661
c906108c
SS
4662 while (cmd != NULL)
4663 {
4664 execute_control_command (cmd);
4665
4666 if (breakpoint_proceeded)
4667 break;
4668 else
4669 cmd = cmd->next;
4670 }
6c50ab1c
JB
4671
4672 /* We can free this command tree now. */
4673 do_cleanups (this_cmd_tree_chain);
4674
c906108c 4675 if (breakpoint_proceeded)
32c1e744
VP
4676 {
4677 if (target_can_async_p ())
347bddb7
PA
4678 /* If we are in async mode, then the target might be still
4679 running, not stopped at any breakpoint, so nothing for
4680 us to do here -- just return to the event loop. */
4681 ;
32c1e744
VP
4682 else
4683 /* In sync mode, when execute_control_command returns
4684 we're already standing on the next breakpoint.
347bddb7
PA
4685 Breakpoint commands for that stop were not run, since
4686 execute_command does not run breakpoint commands --
4687 only command_line_handler does, but that one is not
4688 involved in execution of breakpoint commands. So, we
4689 can now execute breakpoint commands. It should be
4690 noted that making execute_command do bpstat actions is
4691 not an option -- in this case we'll have recursive
4692 invocation of bpstat for each breakpoint with a
4693 command, and can easily blow up GDB stack. Instead, we
4694 return true, which will trigger the caller to recall us
4695 with the new stop_bpstat. */
4696 again = 1;
4697 break;
32c1e744 4698 }
c906108c 4699 }
c2b8ed2c 4700 do_cleanups (old_chain);
347bddb7
PA
4701 return again;
4702}
4703
4704void
4705bpstat_do_actions (void)
4706{
353d1d73
JK
4707 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4708
347bddb7
PA
4709 /* Do any commands attached to breakpoint we are stopped at. */
4710 while (!ptid_equal (inferior_ptid, null_ptid)
4711 && target_has_execution
4712 && !is_exited (inferior_ptid)
4713 && !is_executing (inferior_ptid))
4714 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4715 and only return when it is stopped at the next breakpoint, we
4716 keep doing breakpoint actions until it returns false to
4717 indicate the inferior was not resumed. */
16c381f0 4718 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4719 break;
353d1d73
JK
4720
4721 discard_cleanups (cleanup_if_error);
c906108c
SS
4722}
4723
fa4727a6
DJ
4724/* Print out the (old or new) value associated with a watchpoint. */
4725
4726static void
4727watchpoint_value_print (struct value *val, struct ui_file *stream)
4728{
4729 if (val == NULL)
4730 fprintf_unfiltered (stream, _("<unreadable>"));
4731 else
79a45b7d
TT
4732 {
4733 struct value_print_options opts;
4734 get_user_print_options (&opts);
4735 value_print (val, stream, &opts);
4736 }
fa4727a6
DJ
4737}
4738
e514a9d6 4739/* Generic routine for printing messages indicating why we
4a64f543 4740 stopped. The behavior of this function depends on the value
e514a9d6
JM
4741 'print_it' in the bpstat structure. Under some circumstances we
4742 may decide not to print anything here and delegate the task to
4a64f543 4743 normal_stop(). */
e514a9d6
JM
4744
4745static enum print_stop_action
4746print_bp_stop_message (bpstat bs)
4747{
4748 switch (bs->print_it)
4749 {
4750 case print_it_noop:
4a64f543 4751 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4752 return PRINT_UNKNOWN;
4753 break;
4754
4755 case print_it_done:
4756 /* We still want to print the frame, but we already printed the
4a64f543 4757 relevant messages. */
e514a9d6
JM
4758 return PRINT_SRC_AND_LOC;
4759 break;
4760
4761 case print_it_normal:
4f8d1dc6 4762 {
f431efe5
PA
4763 struct breakpoint *b = bs->breakpoint_at;
4764
1a6a67de
TJB
4765 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4766 which has since been deleted. */
4767 if (b == NULL)
4768 return PRINT_UNKNOWN;
4769
348d480f
PA
4770 /* Normal case. Call the breakpoint's print_it method. */
4771 return b->ops->print_it (bs);
4f8d1dc6 4772 }
348d480f 4773 break;
3086aeae 4774
e514a9d6 4775 default:
8e65ff28 4776 internal_error (__FILE__, __LINE__,
e2e0b3e5 4777 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4778 break;
c906108c 4779 }
c906108c
SS
4780}
4781
edcc5120
TT
4782/* A helper function that prints a shared library stopped event. */
4783
4784static void
4785print_solib_event (int is_catchpoint)
4786{
4787 int any_deleted
4788 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4789 int any_added
4790 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4791
4792 if (!is_catchpoint)
4793 {
4794 if (any_added || any_deleted)
4795 ui_out_text (current_uiout,
4796 _("Stopped due to shared library event:\n"));
4797 else
4798 ui_out_text (current_uiout,
4799 _("Stopped due to shared library event (no "
4800 "libraries added or removed)\n"));
4801 }
4802
4803 if (ui_out_is_mi_like_p (current_uiout))
4804 ui_out_field_string (current_uiout, "reason",
4805 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4806
4807 if (any_deleted)
4808 {
4809 struct cleanup *cleanup;
4810 char *name;
4811 int ix;
4812
4813 ui_out_text (current_uiout, _(" Inferior unloaded "));
4814 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4815 "removed");
4816 for (ix = 0;
4817 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4818 ix, name);
4819 ++ix)
4820 {
4821 if (ix > 0)
4822 ui_out_text (current_uiout, " ");
4823 ui_out_field_string (current_uiout, "library", name);
4824 ui_out_text (current_uiout, "\n");
4825 }
4826
4827 do_cleanups (cleanup);
4828 }
4829
4830 if (any_added)
4831 {
4832 struct so_list *iter;
4833 int ix;
4834 struct cleanup *cleanup;
4835
4836 ui_out_text (current_uiout, _(" Inferior loaded "));
4837 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4838 "added");
4839 for (ix = 0;
4840 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4841 ix, iter);
4842 ++ix)
4843 {
4844 if (ix > 0)
4845 ui_out_text (current_uiout, " ");
4846 ui_out_field_string (current_uiout, "library", iter->so_name);
4847 ui_out_text (current_uiout, "\n");
4848 }
4849
4850 do_cleanups (cleanup);
4851 }
4852}
4853
e514a9d6
JM
4854/* Print a message indicating what happened. This is called from
4855 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4856 list - a list of the eventpoints that caused this stop. KIND is
4857 the target_waitkind for the stopping event. This
e514a9d6
JM
4858 routine calls the generic print routine for printing a message
4859 about reasons for stopping. This will print (for example) the
4860 "Breakpoint n," part of the output. The return value of this
4861 routine is one of:
c906108c 4862
4a64f543 4863 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4864 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4865 code to print the location. An example is
c5aa993b
JM
4866 "Breakpoint 1, " which should be followed by
4867 the location.
917317f4 4868 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4869 to also print the location part of the message.
4870 An example is the catch/throw messages, which
4a64f543 4871 don't require a location appended to the end.
917317f4 4872 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4873 further info to be printed. */
c906108c 4874
917317f4 4875enum print_stop_action
36dfb11c 4876bpstat_print (bpstat bs, int kind)
c906108c
SS
4877{
4878 int val;
c5aa993b 4879
c906108c 4880 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4881 (Currently all watchpoints go on the bpstat whether hit or not.
4882 That probably could (should) be changed, provided care is taken
c906108c 4883 with respect to bpstat_explains_signal). */
e514a9d6
JM
4884 for (; bs; bs = bs->next)
4885 {
4886 val = print_bp_stop_message (bs);
4887 if (val == PRINT_SRC_ONLY
4888 || val == PRINT_SRC_AND_LOC
4889 || val == PRINT_NOTHING)
4890 return val;
4891 }
c906108c 4892
36dfb11c
TT
4893 /* If we had hit a shared library event breakpoint,
4894 print_bp_stop_message would print out this message. If we hit an
4895 OS-level shared library event, do the same thing. */
4896 if (kind == TARGET_WAITKIND_LOADED)
4897 {
edcc5120 4898 print_solib_event (0);
36dfb11c
TT
4899 return PRINT_NOTHING;
4900 }
4901
e514a9d6 4902 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4903 with and nothing was printed. */
917317f4 4904 return PRINT_UNKNOWN;
c906108c
SS
4905}
4906
c42bd95a
DE
4907/* Evaluate the expression EXP and return 1 if value is zero.
4908 This returns the inverse of the condition because it is called
4909 from catch_errors which returns 0 if an exception happened, and if an
4910 exception happens we want execution to stop.
4a64f543 4911 The argument is a "struct expression *" that has been cast to a
c42bd95a 4912 "void *" to make it pass through catch_errors. */
c906108c
SS
4913
4914static int
4efb68b1 4915breakpoint_cond_eval (void *exp)
c906108c 4916{
278cd55f 4917 struct value *mark = value_mark ();
c5aa993b 4918 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4919
c906108c
SS
4920 value_free_to_mark (mark);
4921 return i;
4922}
4923
5760d0ab 4924/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4925
4926static bpstat
5760d0ab 4927bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4928{
4929 bpstat bs;
4930
4931 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4932 bs->next = NULL;
4933 **bs_link_pointer = bs;
4934 *bs_link_pointer = &bs->next;
f431efe5
PA
4935 bs->breakpoint_at = bl->owner;
4936 bs->bp_location_at = bl;
4937 incref_bp_location (bl);
c906108c
SS
4938 /* If the condition is false, etc., don't do the commands. */
4939 bs->commands = NULL;
4940 bs->old_val = NULL;
4941 bs->print_it = print_it_normal;
4942 return bs;
4943}
4944\f
d983da9c
DJ
4945/* The target has stopped with waitstatus WS. Check if any hardware
4946 watchpoints have triggered, according to the target. */
4947
4948int
4949watchpoints_triggered (struct target_waitstatus *ws)
4950{
d92524f1 4951 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4952 CORE_ADDR addr;
4953 struct breakpoint *b;
4954
4955 if (!stopped_by_watchpoint)
4956 {
4957 /* We were not stopped by a watchpoint. Mark all watchpoints
4958 as not triggered. */
4959 ALL_BREAKPOINTS (b)
cc60f2e3 4960 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4961 {
4962 struct watchpoint *w = (struct watchpoint *) b;
4963
4964 w->watchpoint_triggered = watch_triggered_no;
4965 }
d983da9c
DJ
4966
4967 return 0;
4968 }
4969
4970 if (!target_stopped_data_address (&current_target, &addr))
4971 {
4972 /* We were stopped by a watchpoint, but we don't know where.
4973 Mark all watchpoints as unknown. */
4974 ALL_BREAKPOINTS (b)
cc60f2e3 4975 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4976 {
4977 struct watchpoint *w = (struct watchpoint *) b;
4978
4979 w->watchpoint_triggered = watch_triggered_unknown;
4980 }
d983da9c 4981
3c4797ba 4982 return 1;
d983da9c
DJ
4983 }
4984
4985 /* The target could report the data address. Mark watchpoints
4986 affected by this data address as triggered, and all others as not
4987 triggered. */
4988
4989 ALL_BREAKPOINTS (b)
cc60f2e3 4990 if (is_hardware_watchpoint (b))
d983da9c 4991 {
3a5c3e22 4992 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4993 struct bp_location *loc;
d983da9c 4994
3a5c3e22 4995 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4996 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4997 {
3a5c3e22 4998 if (is_masked_watchpoint (b))
9c06b0b4 4999 {
3a5c3e22
PA
5000 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5001 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5002
5003 if (newaddr == start)
5004 {
3a5c3e22 5005 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5006 break;
5007 }
5008 }
5009 /* Exact match not required. Within range is sufficient. */
5010 else if (target_watchpoint_addr_within_range (&current_target,
5011 addr, loc->address,
5012 loc->length))
5013 {
3a5c3e22 5014 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5015 break;
5016 }
5017 }
d983da9c
DJ
5018 }
5019
5020 return 1;
5021}
5022
c906108c
SS
5023/* Possible return values for watchpoint_check (this can't be an enum
5024 because of check_errors). */
5025/* The watchpoint has been deleted. */
5026#define WP_DELETED 1
5027/* The value has changed. */
5028#define WP_VALUE_CHANGED 2
5029/* The value has not changed. */
5030#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5031/* Ignore this watchpoint, no matter if the value changed or not. */
5032#define WP_IGNORE 4
c906108c
SS
5033
5034#define BP_TEMPFLAG 1
5035#define BP_HARDWAREFLAG 2
5036
4a64f543
MS
5037/* Evaluate watchpoint condition expression and check if its value
5038 changed.
553e4c11
JB
5039
5040 P should be a pointer to struct bpstat, but is defined as a void *
5041 in order for this function to be usable with catch_errors. */
c906108c
SS
5042
5043static int
4efb68b1 5044watchpoint_check (void *p)
c906108c
SS
5045{
5046 bpstat bs = (bpstat) p;
3a5c3e22 5047 struct watchpoint *b;
c906108c
SS
5048 struct frame_info *fr;
5049 int within_current_scope;
5050
f431efe5 5051 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5052 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5053 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5054
f6bc2008
PA
5055 /* If this is a local watchpoint, we only want to check if the
5056 watchpoint frame is in scope if the current thread is the thread
5057 that was used to create the watchpoint. */
5058 if (!watchpoint_in_thread_scope (b))
60e1c644 5059 return WP_IGNORE;
f6bc2008 5060
c906108c
SS
5061 if (b->exp_valid_block == NULL)
5062 within_current_scope = 1;
5063 else
5064 {
edb3359d
DJ
5065 struct frame_info *frame = get_current_frame ();
5066 struct gdbarch *frame_arch = get_frame_arch (frame);
5067 CORE_ADDR frame_pc = get_frame_pc (frame);
5068
4a64f543
MS
5069 /* in_function_epilogue_p() returns a non-zero value if we're
5070 still in the function but the stack frame has already been
5071 invalidated. Since we can't rely on the values of local
5072 variables after the stack has been destroyed, we are treating
5073 the watchpoint in that state as `not changed' without further
5074 checking. Don't mark watchpoints as changed if the current
5075 frame is in an epilogue - even if they are in some other
5076 frame, our view of the stack is likely to be wrong and
5077 frame_find_by_id could error out. */
a0f49112 5078 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5079 return WP_IGNORE;
a0f49112 5080
101dcfbe 5081 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5082 within_current_scope = (fr != NULL);
69fbadd5
DJ
5083
5084 /* If we've gotten confused in the unwinder, we might have
5085 returned a frame that can't describe this variable. */
edb3359d
DJ
5086 if (within_current_scope)
5087 {
5088 struct symbol *function;
5089
5090 function = get_frame_function (fr);
5091 if (function == NULL
5092 || !contained_in (b->exp_valid_block,
5093 SYMBOL_BLOCK_VALUE (function)))
5094 within_current_scope = 0;
5095 }
69fbadd5 5096
edb3359d 5097 if (within_current_scope)
c906108c
SS
5098 /* If we end up stopping, the current frame will get selected
5099 in normal_stop. So this call to select_frame won't affect
5100 the user. */
0f7d239c 5101 select_frame (fr);
c906108c 5102 }
c5aa993b 5103
c906108c
SS
5104 if (within_current_scope)
5105 {
4a64f543
MS
5106 /* We use value_{,free_to_}mark because it could be a *long*
5107 time before we return to the command level and call
5108 free_all_values. We can't call free_all_values because we
5109 might be in the middle of evaluating a function call. */
c906108c 5110
0cf6dd15 5111 int pc = 0;
9c06b0b4 5112 struct value *mark;
fa4727a6
DJ
5113 struct value *new_val;
5114
3a5c3e22 5115 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5116 /* Since we don't know the exact trigger address (from
5117 stopped_data_address), just tell the user we've triggered
5118 a mask watchpoint. */
5119 return WP_VALUE_CHANGED;
5120
5121 mark = value_mark ();
3a1115a0 5122 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5123
bb9d5f81
PP
5124 if (b->val_bitsize != 0)
5125 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5126
4a64f543
MS
5127 /* We use value_equal_contents instead of value_equal because
5128 the latter coerces an array to a pointer, thus comparing just
5129 the address of the array instead of its contents. This is
5130 not what we want. */
fa4727a6 5131 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5132 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5133 {
fa4727a6
DJ
5134 if (new_val != NULL)
5135 {
5136 release_value (new_val);
5137 value_free_to_mark (mark);
5138 }
c906108c
SS
5139 bs->old_val = b->val;
5140 b->val = new_val;
fa4727a6 5141 b->val_valid = 1;
c906108c
SS
5142 return WP_VALUE_CHANGED;
5143 }
5144 else
5145 {
60e1c644 5146 /* Nothing changed. */
c906108c 5147 value_free_to_mark (mark);
c906108c
SS
5148 return WP_VALUE_NOT_CHANGED;
5149 }
5150 }
5151 else
5152 {
79a45e25
PA
5153 struct ui_out *uiout = current_uiout;
5154
c906108c 5155 /* This seems like the only logical thing to do because
c5aa993b
JM
5156 if we temporarily ignored the watchpoint, then when
5157 we reenter the block in which it is valid it contains
5158 garbage (in the case of a function, it may have two
5159 garbage values, one before and one after the prologue).
5160 So we can't even detect the first assignment to it and
5161 watch after that (since the garbage may or may not equal
5162 the first value assigned). */
348d480f
PA
5163 /* We print all the stop information in
5164 breakpoint_ops->print_it, but in this case, by the time we
5165 call breakpoint_ops->print_it this bp will be deleted
5166 already. So we have no choice but print the information
5167 here. */
9dc5e2a9 5168 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5169 ui_out_field_string
5170 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5171 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5172 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5173 ui_out_text (uiout,
5174 " deleted because the program has left the block in\n\
8b93c638 5175which its expression is valid.\n");
4ce44c66 5176
cdac0397 5177 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5178 decref_counted_command_line (&b->base.commands);
d0fb5eae 5179 watchpoint_del_at_next_stop (b);
c906108c
SS
5180
5181 return WP_DELETED;
5182 }
5183}
5184
18a18393 5185/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5186 breakpoint location BL. This function does not check if we should
5187 stop, only if BL explains the stop. */
5188
18a18393 5189static int
6c95b8df 5190bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5191 struct address_space *aspace, CORE_ADDR bp_addr,
5192 const struct target_waitstatus *ws)
18a18393
VP
5193{
5194 struct breakpoint *b = bl->owner;
5195
348d480f 5196 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5197 gdb_assert (b != NULL);
5198
09ac7c10 5199 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5200}
5201
3a5c3e22
PA
5202/* Determine if the watched values have actually changed, and we
5203 should stop. If not, set BS->stop to 0. */
5204
18a18393
VP
5205static void
5206bpstat_check_watchpoint (bpstat bs)
5207{
2bdf28a0 5208 const struct bp_location *bl;
3a5c3e22 5209 struct watchpoint *b;
2bdf28a0
JK
5210
5211 /* BS is built for existing struct breakpoint. */
f431efe5 5212 bl = bs->bp_location_at;
2bdf28a0 5213 gdb_assert (bl != NULL);
3a5c3e22 5214 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5215 gdb_assert (b != NULL);
18a18393 5216
18a18393 5217 {
18a18393
VP
5218 int must_check_value = 0;
5219
3a5c3e22 5220 if (b->base.type == bp_watchpoint)
18a18393
VP
5221 /* For a software watchpoint, we must always check the
5222 watched value. */
5223 must_check_value = 1;
5224 else if (b->watchpoint_triggered == watch_triggered_yes)
5225 /* We have a hardware watchpoint (read, write, or access)
5226 and the target earlier reported an address watched by
5227 this watchpoint. */
5228 must_check_value = 1;
5229 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5230 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5231 /* We were stopped by a hardware watchpoint, but the target could
5232 not report the data address. We must check the watchpoint's
5233 value. Access and read watchpoints are out of luck; without
5234 a data address, we can't figure it out. */
5235 must_check_value = 1;
3a5c3e22 5236
18a18393
VP
5237 if (must_check_value)
5238 {
3e43a32a
MS
5239 char *message
5240 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5241 b->base.number);
18a18393
VP
5242 struct cleanup *cleanups = make_cleanup (xfree, message);
5243 int e = catch_errors (watchpoint_check, bs, message,
5244 RETURN_MASK_ALL);
5245 do_cleanups (cleanups);
5246 switch (e)
5247 {
5248 case WP_DELETED:
5249 /* We've already printed what needs to be printed. */
5250 bs->print_it = print_it_done;
5251 /* Stop. */
5252 break;
60e1c644
PA
5253 case WP_IGNORE:
5254 bs->print_it = print_it_noop;
5255 bs->stop = 0;
5256 break;
18a18393 5257 case WP_VALUE_CHANGED:
3a5c3e22 5258 if (b->base.type == bp_read_watchpoint)
18a18393 5259 {
85d721b8
PA
5260 /* There are two cases to consider here:
5261
4a64f543 5262 1. We're watching the triggered memory for reads.
85d721b8
PA
5263 In that case, trust the target, and always report
5264 the watchpoint hit to the user. Even though
5265 reads don't cause value changes, the value may
5266 have changed since the last time it was read, and
5267 since we're not trapping writes, we will not see
5268 those, and as such we should ignore our notion of
5269 old value.
5270
4a64f543 5271 2. We're watching the triggered memory for both
85d721b8
PA
5272 reads and writes. There are two ways this may
5273 happen:
5274
4a64f543 5275 2.1. This is a target that can't break on data
85d721b8
PA
5276 reads only, but can break on accesses (reads or
5277 writes), such as e.g., x86. We detect this case
5278 at the time we try to insert read watchpoints.
5279
4a64f543 5280 2.2. Otherwise, the target supports read
85d721b8
PA
5281 watchpoints, but, the user set an access or write
5282 watchpoint watching the same memory as this read
5283 watchpoint.
5284
5285 If we're watching memory writes as well as reads,
5286 ignore watchpoint hits when we find that the
5287 value hasn't changed, as reads don't cause
5288 changes. This still gives false positives when
5289 the program writes the same value to memory as
5290 what there was already in memory (we will confuse
5291 it for a read), but it's much better than
5292 nothing. */
5293
5294 int other_write_watchpoint = 0;
5295
5296 if (bl->watchpoint_type == hw_read)
5297 {
5298 struct breakpoint *other_b;
5299
5300 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5301 if (other_b->type == bp_hardware_watchpoint
5302 || other_b->type == bp_access_watchpoint)
85d721b8 5303 {
3a5c3e22
PA
5304 struct watchpoint *other_w =
5305 (struct watchpoint *) other_b;
5306
5307 if (other_w->watchpoint_triggered
5308 == watch_triggered_yes)
5309 {
5310 other_write_watchpoint = 1;
5311 break;
5312 }
85d721b8
PA
5313 }
5314 }
5315
5316 if (other_write_watchpoint
5317 || bl->watchpoint_type == hw_access)
5318 {
5319 /* We're watching the same memory for writes,
5320 and the value changed since the last time we
5321 updated it, so this trap must be for a write.
5322 Ignore it. */
5323 bs->print_it = print_it_noop;
5324 bs->stop = 0;
5325 }
18a18393
VP
5326 }
5327 break;
5328 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5329 if (b->base.type == bp_hardware_watchpoint
5330 || b->base.type == bp_watchpoint)
18a18393
VP
5331 {
5332 /* Don't stop: write watchpoints shouldn't fire if
5333 the value hasn't changed. */
5334 bs->print_it = print_it_noop;
5335 bs->stop = 0;
5336 }
5337 /* Stop. */
5338 break;
5339 default:
5340 /* Can't happen. */
5341 case 0:
5342 /* Error from catch_errors. */
3a5c3e22 5343 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5344 watchpoint_del_at_next_stop (b);
18a18393
VP
5345 /* We've already printed what needs to be printed. */
5346 bs->print_it = print_it_done;
5347 break;
5348 }
5349 }
5350 else /* must_check_value == 0 */
5351 {
5352 /* This is a case where some watchpoint(s) triggered, but
5353 not at the address of this watchpoint, or else no
5354 watchpoint triggered after all. So don't print
5355 anything for this watchpoint. */
5356 bs->print_it = print_it_noop;
5357 bs->stop = 0;
5358 }
5359 }
5360}
5361
7d4df6a4
DE
5362/* For breakpoints that are currently marked as telling gdb to stop,
5363 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5364 of breakpoint referred to by BS. If we should not stop for this
5365 breakpoint, set BS->stop to 0. */
f431efe5 5366
18a18393
VP
5367static void
5368bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5369{
2bdf28a0
JK
5370 const struct bp_location *bl;
5371 struct breakpoint *b;
7d4df6a4
DE
5372 int value_is_zero = 0;
5373 struct expression *cond;
5374
5375 gdb_assert (bs->stop);
2bdf28a0
JK
5376
5377 /* BS is built for existing struct breakpoint. */
f431efe5 5378 bl = bs->bp_location_at;
2bdf28a0 5379 gdb_assert (bl != NULL);
f431efe5 5380 b = bs->breakpoint_at;
2bdf28a0 5381 gdb_assert (b != NULL);
18a18393 5382
b775012e
LM
5383 /* Even if the target evaluated the condition on its end and notified GDB, we
5384 need to do so again since GDB does not know if we stopped due to a
5385 breakpoint or a single step breakpoint. */
5386
18a18393 5387 if (frame_id_p (b->frame_id)
edb3359d 5388 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5389 {
7d4df6a4
DE
5390 bs->stop = 0;
5391 return;
5392 }
60e1c644 5393
12ab52e9
PA
5394 /* If this is a thread/task-specific breakpoint, don't waste cpu
5395 evaluating the condition if this isn't the specified
5396 thread/task. */
5397 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5398 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5399
6c1b0f7b
DE
5400 {
5401 bs->stop = 0;
5402 return;
5403 }
5404
6dddc817
DE
5405 /* Evaluate extension language breakpoints that have a "stop" method
5406 implemented. */
5407 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5408
7d4df6a4
DE
5409 if (is_watchpoint (b))
5410 {
5411 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5412
7d4df6a4
DE
5413 cond = w->cond_exp;
5414 }
5415 else
5416 cond = bl->cond;
60e1c644 5417
7d4df6a4
DE
5418 if (cond && b->disposition != disp_del_at_next_stop)
5419 {
5420 int within_current_scope = 1;
5421 struct watchpoint * w;
60e1c644 5422
7d4df6a4
DE
5423 /* We use value_mark and value_free_to_mark because it could
5424 be a long time before we return to the command level and
5425 call free_all_values. We can't call free_all_values
5426 because we might be in the middle of evaluating a
5427 function call. */
5428 struct value *mark = value_mark ();
5429
5430 if (is_watchpoint (b))
5431 w = (struct watchpoint *) b;
5432 else
5433 w = NULL;
5434
5435 /* Need to select the frame, with all that implies so that
5436 the conditions will have the right context. Because we
5437 use the frame, we will not see an inlined function's
5438 variables when we arrive at a breakpoint at the start
5439 of the inlined function; the current frame will be the
5440 call site. */
5441 if (w == NULL || w->cond_exp_valid_block == NULL)
5442 select_frame (get_current_frame ());
5443 else
18a18393 5444 {
7d4df6a4
DE
5445 struct frame_info *frame;
5446
5447 /* For local watchpoint expressions, which particular
5448 instance of a local is being watched matters, so we
5449 keep track of the frame to evaluate the expression
5450 in. To evaluate the condition however, it doesn't
5451 really matter which instantiation of the function
5452 where the condition makes sense triggers the
5453 watchpoint. This allows an expression like "watch
5454 global if q > 10" set in `func', catch writes to
5455 global on all threads that call `func', or catch
5456 writes on all recursive calls of `func' by a single
5457 thread. We simply always evaluate the condition in
5458 the innermost frame that's executing where it makes
5459 sense to evaluate the condition. It seems
5460 intuitive. */
5461 frame = block_innermost_frame (w->cond_exp_valid_block);
5462 if (frame != NULL)
5463 select_frame (frame);
5464 else
5465 within_current_scope = 0;
18a18393 5466 }
7d4df6a4
DE
5467 if (within_current_scope)
5468 value_is_zero
5469 = catch_errors (breakpoint_cond_eval, cond,
5470 "Error in testing breakpoint condition:\n",
5471 RETURN_MASK_ALL);
5472 else
18a18393 5473 {
7d4df6a4
DE
5474 warning (_("Watchpoint condition cannot be tested "
5475 "in the current scope"));
5476 /* If we failed to set the right context for this
5477 watchpoint, unconditionally report it. */
5478 value_is_zero = 0;
18a18393 5479 }
7d4df6a4
DE
5480 /* FIXME-someday, should give breakpoint #. */
5481 value_free_to_mark (mark);
18a18393 5482 }
7d4df6a4
DE
5483
5484 if (cond && value_is_zero)
5485 {
5486 bs->stop = 0;
5487 }
7d4df6a4
DE
5488 else if (b->ignore_count > 0)
5489 {
5490 b->ignore_count--;
5491 bs->stop = 0;
5492 /* Increase the hit count even though we don't stop. */
5493 ++(b->hit_count);
5494 observer_notify_breakpoint_modified (b);
5495 }
18a18393
VP
5496}
5497
5498
9709f61c 5499/* Get a bpstat associated with having just stopped at address
d983da9c 5500 BP_ADDR in thread PTID.
c906108c 5501
d983da9c 5502 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5503 don't understand this stop. Result is a chain of bpstat's such
5504 that:
c906108c 5505
c5aa993b 5506 if we don't understand the stop, the result is a null pointer.
c906108c 5507
c5aa993b 5508 if we understand why we stopped, the result is not null.
c906108c 5509
c5aa993b
JM
5510 Each element of the chain refers to a particular breakpoint or
5511 watchpoint at which we have stopped. (We may have stopped for
5512 several reasons concurrently.)
c906108c 5513
c5aa993b
JM
5514 Each element of the chain has valid next, breakpoint_at,
5515 commands, FIXME??? fields. */
c906108c
SS
5516
5517bpstat
6c95b8df 5518bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5519 CORE_ADDR bp_addr, ptid_t ptid,
5520 const struct target_waitstatus *ws)
c906108c 5521{
0d381245 5522 struct breakpoint *b = NULL;
afe38095 5523 struct bp_location *bl;
20874c92 5524 struct bp_location *loc;
5760d0ab
JK
5525 /* First item of allocated bpstat's. */
5526 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5527 /* Pointer to the last thing in the chain currently. */
5760d0ab 5528 bpstat bs;
20874c92 5529 int ix;
429374b8 5530 int need_remove_insert;
f431efe5 5531 int removed_any;
c906108c 5532
f431efe5
PA
5533 /* First, build the bpstat chain with locations that explain a
5534 target stop, while being careful to not set the target running,
5535 as that may invalidate locations (in particular watchpoint
5536 locations are recreated). Resuming will happen here with
5537 breakpoint conditions or watchpoint expressions that include
5538 inferior function calls. */
c5aa993b 5539
429374b8
JK
5540 ALL_BREAKPOINTS (b)
5541 {
5542 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5543 continue;
a5606eee 5544
429374b8
JK
5545 for (bl = b->loc; bl != NULL; bl = bl->next)
5546 {
4a64f543
MS
5547 /* For hardware watchpoints, we look only at the first
5548 location. The watchpoint_check function will work on the
5549 entire expression, not the individual locations. For
5550 read watchpoints, the watchpoints_triggered function has
5551 checked all locations already. */
429374b8
JK
5552 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5553 break;
18a18393 5554
f6592439 5555 if (!bl->enabled || bl->shlib_disabled)
429374b8 5556 continue;
c5aa993b 5557
09ac7c10 5558 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5559 continue;
c5aa993b 5560
4a64f543
MS
5561 /* Come here if it's a watchpoint, or if the break address
5562 matches. */
c5aa993b 5563
4a64f543
MS
5564 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5565 explain stop. */
c5aa993b 5566
f431efe5
PA
5567 /* Assume we stop. Should we find a watchpoint that is not
5568 actually triggered, or if the condition of the breakpoint
5569 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5570 bs->stop = 1;
5571 bs->print = 1;
d983da9c 5572
f431efe5
PA
5573 /* If this is a scope breakpoint, mark the associated
5574 watchpoint as triggered so that we will handle the
5575 out-of-scope event. We'll get to the watchpoint next
5576 iteration. */
d0fb5eae 5577 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5578 {
5579 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5580
5581 w->watchpoint_triggered = watch_triggered_yes;
5582 }
f431efe5
PA
5583 }
5584 }
5585
7c16b83e 5586 /* Check if a moribund breakpoint explains the stop. */
f431efe5
PA
5587 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5588 {
f1310107 5589 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5590 {
5760d0ab 5591 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5592 /* For hits of moribund locations, we should just proceed. */
5593 bs->stop = 0;
5594 bs->print = 0;
5595 bs->print_it = print_it_noop;
5596 }
5597 }
5598
edcc5120
TT
5599 /* A bit of special processing for shlib breakpoints. We need to
5600 process solib loading here, so that the lists of loaded and
5601 unloaded libraries are correct before we handle "catch load" and
5602 "catch unload". */
5603 for (bs = bs_head; bs != NULL; bs = bs->next)
5604 {
5d268276 5605 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5606 {
5607 handle_solib_event ();
5608 break;
5609 }
5610 }
5611
f431efe5
PA
5612 /* Now go through the locations that caused the target to stop, and
5613 check whether we're interested in reporting this stop to higher
5614 layers, or whether we should resume the target transparently. */
5615
5616 removed_any = 0;
5617
5760d0ab 5618 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5619 {
5620 if (!bs->stop)
5621 continue;
5622
f431efe5 5623 b = bs->breakpoint_at;
348d480f
PA
5624 b->ops->check_status (bs);
5625 if (bs->stop)
28010a5d 5626 {
348d480f 5627 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5628
429374b8
JK
5629 if (bs->stop)
5630 {
5631 ++(b->hit_count);
8d3788bd 5632 observer_notify_breakpoint_modified (b);
c906108c 5633
4a64f543 5634 /* We will stop here. */
429374b8
JK
5635 if (b->disposition == disp_disable)
5636 {
816338b5
SS
5637 --(b->enable_count);
5638 if (b->enable_count <= 0
5639 && b->enable_state != bp_permanent)
429374b8 5640 b->enable_state = bp_disabled;
f431efe5 5641 removed_any = 1;
429374b8
JK
5642 }
5643 if (b->silent)
5644 bs->print = 0;
5645 bs->commands = b->commands;
9add0f1b 5646 incref_counted_command_line (bs->commands);
abf85f46
JK
5647 if (command_line_is_silent (bs->commands
5648 ? bs->commands->commands : NULL))
5649 bs->print = 0;
9d6e6e84
HZ
5650
5651 b->ops->after_condition_true (bs);
429374b8
JK
5652 }
5653
348d480f 5654 }
a9b3a50f
PA
5655
5656 /* Print nothing for this entry if we don't stop or don't
5657 print. */
5658 if (!bs->stop || !bs->print)
5659 bs->print_it = print_it_noop;
429374b8 5660 }
876fa593 5661
d983da9c
DJ
5662 /* If we aren't stopping, the value of some hardware watchpoint may
5663 not have changed, but the intermediate memory locations we are
5664 watching may have. Don't bother if we're stopping; this will get
5665 done later. */
d832cb68 5666 need_remove_insert = 0;
5760d0ab
JK
5667 if (! bpstat_causes_stop (bs_head))
5668 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5669 if (!bs->stop
f431efe5
PA
5670 && bs->breakpoint_at
5671 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5672 {
3a5c3e22
PA
5673 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5674
5675 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5676 need_remove_insert = 1;
d983da9c
DJ
5677 }
5678
d832cb68 5679 if (need_remove_insert)
44702360 5680 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5681 else if (removed_any)
44702360 5682 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5683
5760d0ab 5684 return bs_head;
c906108c 5685}
628fe4e4
JK
5686
5687static void
5688handle_jit_event (void)
5689{
5690 struct frame_info *frame;
5691 struct gdbarch *gdbarch;
5692
5693 /* Switch terminal for any messages produced by
5694 breakpoint_re_set. */
5695 target_terminal_ours_for_output ();
5696
5697 frame = get_current_frame ();
5698 gdbarch = get_frame_arch (frame);
5699
5700 jit_event_handler (gdbarch);
5701
5702 target_terminal_inferior ();
5703}
5704
5705/* Prepare WHAT final decision for infrun. */
5706
5707/* Decide what infrun needs to do with this bpstat. */
5708
c906108c 5709struct bpstat_what
0e30163f 5710bpstat_what (bpstat bs_head)
c906108c 5711{
c906108c 5712 struct bpstat_what retval;
628fe4e4 5713 int jit_event = 0;
0e30163f 5714 bpstat bs;
c906108c 5715
628fe4e4 5716 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5717 retval.call_dummy = STOP_NONE;
186c406b 5718 retval.is_longjmp = 0;
628fe4e4 5719
0e30163f 5720 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5721 {
628fe4e4
JK
5722 /* Extract this BS's action. After processing each BS, we check
5723 if its action overrides all we've seem so far. */
5724 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5725 enum bptype bptype;
5726
c906108c 5727 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5728 {
5729 /* I suspect this can happen if it was a momentary
5730 breakpoint which has since been deleted. */
5731 bptype = bp_none;
5732 }
20874c92 5733 else
f431efe5 5734 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5735
5736 switch (bptype)
c906108c
SS
5737 {
5738 case bp_none:
628fe4e4 5739 break;
c906108c
SS
5740 case bp_breakpoint:
5741 case bp_hardware_breakpoint:
7c16b83e 5742 case bp_single_step:
c906108c
SS
5743 case bp_until:
5744 case bp_finish:
a9b3a50f 5745 case bp_shlib_event:
c906108c
SS
5746 if (bs->stop)
5747 {
5748 if (bs->print)
628fe4e4 5749 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5750 else
628fe4e4 5751 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5752 }
5753 else
628fe4e4 5754 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5755 break;
5756 case bp_watchpoint:
5757 case bp_hardware_watchpoint:
5758 case bp_read_watchpoint:
5759 case bp_access_watchpoint:
5760 if (bs->stop)
5761 {
5762 if (bs->print)
628fe4e4 5763 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5764 else
628fe4e4 5765 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5766 }
5767 else
628fe4e4
JK
5768 {
5769 /* There was a watchpoint, but we're not stopping.
5770 This requires no further action. */
5771 }
c906108c
SS
5772 break;
5773 case bp_longjmp:
e2e4d78b 5774 case bp_longjmp_call_dummy:
186c406b 5775 case bp_exception:
628fe4e4 5776 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5777 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5778 break;
5779 case bp_longjmp_resume:
186c406b 5780 case bp_exception_resume:
628fe4e4 5781 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5782 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5783 break;
5784 case bp_step_resume:
5785 if (bs->stop)
628fe4e4
JK
5786 this_action = BPSTAT_WHAT_STEP_RESUME;
5787 else
c906108c 5788 {
628fe4e4
JK
5789 /* It is for the wrong frame. */
5790 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5791 }
c906108c 5792 break;
2c03e5be
PA
5793 case bp_hp_step_resume:
5794 if (bs->stop)
5795 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5796 else
5797 {
5798 /* It is for the wrong frame. */
5799 this_action = BPSTAT_WHAT_SINGLE;
5800 }
5801 break;
c906108c 5802 case bp_watchpoint_scope:
c4093a6a 5803 case bp_thread_event:
1900040c 5804 case bp_overlay_event:
0fd8e87f 5805 case bp_longjmp_master:
aa7d318d 5806 case bp_std_terminate_master:
186c406b 5807 case bp_exception_master:
628fe4e4 5808 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5809 break;
ce78b96d 5810 case bp_catchpoint:
c5aa993b
JM
5811 if (bs->stop)
5812 {
5813 if (bs->print)
628fe4e4 5814 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5815 else
628fe4e4 5816 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5817 }
5818 else
628fe4e4
JK
5819 {
5820 /* There was a catchpoint, but we're not stopping.
5821 This requires no further action. */
5822 }
5823 break;
628fe4e4
JK
5824 case bp_jit_event:
5825 jit_event = 1;
5826 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5827 break;
c906108c 5828 case bp_call_dummy:
53a5351d
JM
5829 /* Make sure the action is stop (silent or noisy),
5830 so infrun.c pops the dummy frame. */
aa7d318d 5831 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5832 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5833 break;
5834 case bp_std_terminate:
5835 /* Make sure the action is stop (silent or noisy),
5836 so infrun.c pops the dummy frame. */
aa7d318d 5837 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5838 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5839 break;
1042e4c0 5840 case bp_tracepoint:
7a697b8d 5841 case bp_fast_tracepoint:
0fb4aa4b 5842 case bp_static_tracepoint:
1042e4c0
SS
5843 /* Tracepoint hits should not be reported back to GDB, and
5844 if one got through somehow, it should have been filtered
5845 out already. */
5846 internal_error (__FILE__, __LINE__,
7a697b8d 5847 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5848 break;
5849 case bp_gnu_ifunc_resolver:
5850 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5851 this_action = BPSTAT_WHAT_SINGLE;
5852 break;
5853 case bp_gnu_ifunc_resolver_return:
5854 /* The breakpoint will be removed, execution will restart from the
5855 PC of the former breakpoint. */
5856 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5857 break;
e7e0cddf
SS
5858
5859 case bp_dprintf:
a11cfd87
HZ
5860 if (bs->stop)
5861 this_action = BPSTAT_WHAT_STOP_SILENT;
5862 else
5863 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5864 break;
5865
628fe4e4
JK
5866 default:
5867 internal_error (__FILE__, __LINE__,
5868 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5869 }
628fe4e4
JK
5870
5871 retval.main_action = max (retval.main_action, this_action);
c906108c 5872 }
628fe4e4 5873
0e30163f
JK
5874 /* These operations may affect the bs->breakpoint_at state so they are
5875 delayed after MAIN_ACTION is decided above. */
5876
628fe4e4
JK
5877 if (jit_event)
5878 {
5879 if (debug_infrun)
5880 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5881
5882 handle_jit_event ();
5883 }
5884
0e30163f
JK
5885 for (bs = bs_head; bs != NULL; bs = bs->next)
5886 {
5887 struct breakpoint *b = bs->breakpoint_at;
5888
5889 if (b == NULL)
5890 continue;
5891 switch (b->type)
5892 {
5893 case bp_gnu_ifunc_resolver:
5894 gnu_ifunc_resolver_stop (b);
5895 break;
5896 case bp_gnu_ifunc_resolver_return:
5897 gnu_ifunc_resolver_return_stop (b);
5898 break;
5899 }
5900 }
5901
c906108c
SS
5902 return retval;
5903}
5904
5905/* Nonzero if we should step constantly (e.g. watchpoints on machines
5906 without hardware support). This isn't related to a specific bpstat,
5907 just to things like whether watchpoints are set. */
5908
c5aa993b 5909int
fba45db2 5910bpstat_should_step (void)
c906108c
SS
5911{
5912 struct breakpoint *b;
cc59ec59 5913
c906108c 5914 ALL_BREAKPOINTS (b)
717a8278 5915 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5916 return 1;
c906108c
SS
5917 return 0;
5918}
5919
67822962
PA
5920int
5921bpstat_causes_stop (bpstat bs)
5922{
5923 for (; bs != NULL; bs = bs->next)
5924 if (bs->stop)
5925 return 1;
5926
5927 return 0;
5928}
5929
c906108c 5930\f
c5aa993b 5931
170b53b2
UW
5932/* Compute a string of spaces suitable to indent the next line
5933 so it starts at the position corresponding to the table column
5934 named COL_NAME in the currently active table of UIOUT. */
5935
5936static char *
5937wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5938{
5939 static char wrap_indent[80];
5940 int i, total_width, width, align;
5941 char *text;
5942
5943 total_width = 0;
5944 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5945 {
5946 if (strcmp (text, col_name) == 0)
5947 {
5948 gdb_assert (total_width < sizeof wrap_indent);
5949 memset (wrap_indent, ' ', total_width);
5950 wrap_indent[total_width] = 0;
5951
5952 return wrap_indent;
5953 }
5954
5955 total_width += width + 1;
5956 }
5957
5958 return NULL;
5959}
5960
b775012e
LM
5961/* Determine if the locations of this breakpoint will have their conditions
5962 evaluated by the target, host or a mix of both. Returns the following:
5963
5964 "host": Host evals condition.
5965 "host or target": Host or Target evals condition.
5966 "target": Target evals condition.
5967*/
5968
5969static const char *
5970bp_condition_evaluator (struct breakpoint *b)
5971{
5972 struct bp_location *bl;
5973 char host_evals = 0;
5974 char target_evals = 0;
5975
5976 if (!b)
5977 return NULL;
5978
5979 if (!is_breakpoint (b))
5980 return NULL;
5981
5982 if (gdb_evaluates_breakpoint_condition_p ()
5983 || !target_supports_evaluation_of_breakpoint_conditions ())
5984 return condition_evaluation_host;
5985
5986 for (bl = b->loc; bl; bl = bl->next)
5987 {
5988 if (bl->cond_bytecode)
5989 target_evals++;
5990 else
5991 host_evals++;
5992 }
5993
5994 if (host_evals && target_evals)
5995 return condition_evaluation_both;
5996 else if (target_evals)
5997 return condition_evaluation_target;
5998 else
5999 return condition_evaluation_host;
6000}
6001
6002/* Determine the breakpoint location's condition evaluator. This is
6003 similar to bp_condition_evaluator, but for locations. */
6004
6005static const char *
6006bp_location_condition_evaluator (struct bp_location *bl)
6007{
6008 if (bl && !is_breakpoint (bl->owner))
6009 return NULL;
6010
6011 if (gdb_evaluates_breakpoint_condition_p ()
6012 || !target_supports_evaluation_of_breakpoint_conditions ())
6013 return condition_evaluation_host;
6014
6015 if (bl && bl->cond_bytecode)
6016 return condition_evaluation_target;
6017 else
6018 return condition_evaluation_host;
6019}
6020
859825b8
JK
6021/* Print the LOC location out of the list of B->LOC locations. */
6022
170b53b2
UW
6023static void
6024print_breakpoint_location (struct breakpoint *b,
6025 struct bp_location *loc)
0d381245 6026{
79a45e25 6027 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6028 struct cleanup *old_chain = save_current_program_space ();
6029
859825b8
JK
6030 if (loc != NULL && loc->shlib_disabled)
6031 loc = NULL;
6032
6c95b8df
PA
6033 if (loc != NULL)
6034 set_current_program_space (loc->pspace);
6035
56435ebe
TT
6036 if (b->display_canonical)
6037 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6038 else if (loc && loc->symtab)
0d381245
VP
6039 {
6040 struct symbol *sym
6041 = find_pc_sect_function (loc->address, loc->section);
6042 if (sym)
6043 {
6044 ui_out_text (uiout, "in ");
6045 ui_out_field_string (uiout, "func",
6046 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6047 ui_out_text (uiout, " ");
6048 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6049 ui_out_text (uiout, "at ");
0d381245 6050 }
05cba821
JK
6051 ui_out_field_string (uiout, "file",
6052 symtab_to_filename_for_display (loc->symtab));
0d381245 6053 ui_out_text (uiout, ":");
05cba821 6054
0d381245 6055 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6056 ui_out_field_string (uiout, "fullname",
6057 symtab_to_fullname (loc->symtab));
0d381245 6058
f8eba3c6 6059 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6060 }
859825b8 6061 else if (loc)
0d381245 6062 {
f99d8bf4
PA
6063 struct ui_file *stb = mem_fileopen ();
6064 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6065
f99d8bf4 6066 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6067 demangle, "");
0d381245 6068 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6069
6070 do_cleanups (stb_chain);
0d381245 6071 }
859825b8
JK
6072 else
6073 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6074
b775012e
LM
6075 if (loc && is_breakpoint (b)
6076 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6077 && bp_condition_evaluator (b) == condition_evaluation_both)
6078 {
6079 ui_out_text (uiout, " (");
6080 ui_out_field_string (uiout, "evaluated-by",
6081 bp_location_condition_evaluator (loc));
6082 ui_out_text (uiout, ")");
6083 }
6084
6c95b8df 6085 do_cleanups (old_chain);
0d381245
VP
6086}
6087
269b11a2
PA
6088static const char *
6089bptype_string (enum bptype type)
c906108c 6090{
c4093a6a
JM
6091 struct ep_type_description
6092 {
6093 enum bptype type;
6094 char *description;
6095 };
6096 static struct ep_type_description bptypes[] =
c906108c 6097 {
c5aa993b
JM
6098 {bp_none, "?deleted?"},
6099 {bp_breakpoint, "breakpoint"},
c906108c 6100 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6101 {bp_single_step, "sw single-step"},
c5aa993b
JM
6102 {bp_until, "until"},
6103 {bp_finish, "finish"},
6104 {bp_watchpoint, "watchpoint"},
c906108c 6105 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6106 {bp_read_watchpoint, "read watchpoint"},
6107 {bp_access_watchpoint, "acc watchpoint"},
6108 {bp_longjmp, "longjmp"},
6109 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6110 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6111 {bp_exception, "exception"},
6112 {bp_exception_resume, "exception resume"},
c5aa993b 6113 {bp_step_resume, "step resume"},
2c03e5be 6114 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6115 {bp_watchpoint_scope, "watchpoint scope"},
6116 {bp_call_dummy, "call dummy"},
aa7d318d 6117 {bp_std_terminate, "std::terminate"},
c5aa993b 6118 {bp_shlib_event, "shlib events"},
c4093a6a 6119 {bp_thread_event, "thread events"},
1900040c 6120 {bp_overlay_event, "overlay events"},
0fd8e87f 6121 {bp_longjmp_master, "longjmp master"},
aa7d318d 6122 {bp_std_terminate_master, "std::terminate master"},
186c406b 6123 {bp_exception_master, "exception master"},
ce78b96d 6124 {bp_catchpoint, "catchpoint"},
1042e4c0 6125 {bp_tracepoint, "tracepoint"},
7a697b8d 6126 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6127 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6128 {bp_dprintf, "dprintf"},
4efc6507 6129 {bp_jit_event, "jit events"},
0e30163f
JK
6130 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6131 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6132 };
269b11a2
PA
6133
6134 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6135 || ((int) type != bptypes[(int) type].type))
6136 internal_error (__FILE__, __LINE__,
6137 _("bptypes table does not describe type #%d."),
6138 (int) type);
6139
6140 return bptypes[(int) type].description;
6141}
6142
998580f1
MK
6143/* For MI, output a field named 'thread-groups' with a list as the value.
6144 For CLI, prefix the list with the string 'inf'. */
6145
6146static void
6147output_thread_groups (struct ui_out *uiout,
6148 const char *field_name,
6149 VEC(int) *inf_num,
6150 int mi_only)
6151{
752eb8b4 6152 struct cleanup *back_to;
998580f1
MK
6153 int is_mi = ui_out_is_mi_like_p (uiout);
6154 int inf;
6155 int i;
6156
6157 /* For backward compatibility, don't display inferiors in CLI unless
6158 there are several. Always display them for MI. */
6159 if (!is_mi && mi_only)
6160 return;
6161
752eb8b4
TT
6162 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6163
998580f1
MK
6164 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6165 {
6166 if (is_mi)
6167 {
6168 char mi_group[10];
6169
6170 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6171 ui_out_field_string (uiout, NULL, mi_group);
6172 }
6173 else
6174 {
6175 if (i == 0)
6176 ui_out_text (uiout, " inf ");
6177 else
6178 ui_out_text (uiout, ", ");
6179
6180 ui_out_text (uiout, plongest (inf));
6181 }
6182 }
6183
6184 do_cleanups (back_to);
6185}
6186
269b11a2
PA
6187/* Print B to gdb_stdout. */
6188
6189static void
6190print_one_breakpoint_location (struct breakpoint *b,
6191 struct bp_location *loc,
6192 int loc_number,
6193 struct bp_location **last_loc,
269b11a2
PA
6194 int allflag)
6195{
6196 struct command_line *l;
c2c6d25f 6197 static char bpenables[] = "nynny";
c906108c 6198
79a45e25 6199 struct ui_out *uiout = current_uiout;
0d381245
VP
6200 int header_of_multiple = 0;
6201 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6202 struct value_print_options opts;
6203
6204 get_user_print_options (&opts);
0d381245
VP
6205
6206 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6207 /* See comment in print_one_breakpoint concerning treatment of
6208 breakpoints with single disabled location. */
0d381245
VP
6209 if (loc == NULL
6210 && (b->loc != NULL
6211 && (b->loc->next != NULL || !b->loc->enabled)))
6212 header_of_multiple = 1;
6213 if (loc == NULL)
6214 loc = b->loc;
6215
c4093a6a
JM
6216 annotate_record ();
6217
6218 /* 1 */
6219 annotate_field (0);
0d381245
VP
6220 if (part_of_multiple)
6221 {
6222 char *formatted;
0c6773c1 6223 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6224 ui_out_field_string (uiout, "number", formatted);
6225 xfree (formatted);
6226 }
6227 else
6228 {
6229 ui_out_field_int (uiout, "number", b->number);
6230 }
c4093a6a
JM
6231
6232 /* 2 */
6233 annotate_field (1);
0d381245
VP
6234 if (part_of_multiple)
6235 ui_out_field_skip (uiout, "type");
269b11a2
PA
6236 else
6237 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6238
6239 /* 3 */
6240 annotate_field (2);
0d381245
VP
6241 if (part_of_multiple)
6242 ui_out_field_skip (uiout, "disp");
6243 else
2cec12e5 6244 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6245
c4093a6a
JM
6246
6247 /* 4 */
6248 annotate_field (3);
0d381245 6249 if (part_of_multiple)
54e52265 6250 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6251 else
4a64f543
MS
6252 ui_out_field_fmt (uiout, "enabled", "%c",
6253 bpenables[(int) b->enable_state]);
54e52265 6254 ui_out_spaces (uiout, 2);
0d381245 6255
c4093a6a
JM
6256
6257 /* 5 and 6 */
3086aeae 6258 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6259 {
4a64f543
MS
6260 /* Although the print_one can possibly print all locations,
6261 calling it here is not likely to get any nice result. So,
6262 make sure there's just one location. */
0d381245 6263 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6264 b->ops->print_one (b, last_loc);
0d381245 6265 }
3086aeae
DJ
6266 else
6267 switch (b->type)
6268 {
6269 case bp_none:
6270 internal_error (__FILE__, __LINE__,
e2e0b3e5 6271 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6272 break;
c906108c 6273
3086aeae
DJ
6274 case bp_watchpoint:
6275 case bp_hardware_watchpoint:
6276 case bp_read_watchpoint:
6277 case bp_access_watchpoint:
3a5c3e22
PA
6278 {
6279 struct watchpoint *w = (struct watchpoint *) b;
6280
6281 /* Field 4, the address, is omitted (which makes the columns
6282 not line up too nicely with the headers, but the effect
6283 is relatively readable). */
6284 if (opts.addressprint)
6285 ui_out_field_skip (uiout, "addr");
6286 annotate_field (5);
6287 ui_out_field_string (uiout, "what", w->exp_string);
6288 }
3086aeae
DJ
6289 break;
6290
3086aeae
DJ
6291 case bp_breakpoint:
6292 case bp_hardware_breakpoint:
7c16b83e 6293 case bp_single_step:
3086aeae
DJ
6294 case bp_until:
6295 case bp_finish:
6296 case bp_longjmp:
6297 case bp_longjmp_resume:
e2e4d78b 6298 case bp_longjmp_call_dummy:
186c406b
TT
6299 case bp_exception:
6300 case bp_exception_resume:
3086aeae 6301 case bp_step_resume:
2c03e5be 6302 case bp_hp_step_resume:
3086aeae
DJ
6303 case bp_watchpoint_scope:
6304 case bp_call_dummy:
aa7d318d 6305 case bp_std_terminate:
3086aeae
DJ
6306 case bp_shlib_event:
6307 case bp_thread_event:
6308 case bp_overlay_event:
0fd8e87f 6309 case bp_longjmp_master:
aa7d318d 6310 case bp_std_terminate_master:
186c406b 6311 case bp_exception_master:
1042e4c0 6312 case bp_tracepoint:
7a697b8d 6313 case bp_fast_tracepoint:
0fb4aa4b 6314 case bp_static_tracepoint:
e7e0cddf 6315 case bp_dprintf:
4efc6507 6316 case bp_jit_event:
0e30163f
JK
6317 case bp_gnu_ifunc_resolver:
6318 case bp_gnu_ifunc_resolver_return:
79a45b7d 6319 if (opts.addressprint)
3086aeae
DJ
6320 {
6321 annotate_field (4);
54e52265 6322 if (header_of_multiple)
0d381245 6323 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6324 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6325 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6326 else
5af949e3
UW
6327 ui_out_field_core_addr (uiout, "addr",
6328 loc->gdbarch, loc->address);
3086aeae
DJ
6329 }
6330 annotate_field (5);
0d381245 6331 if (!header_of_multiple)
170b53b2 6332 print_breakpoint_location (b, loc);
0d381245 6333 if (b->loc)
a6d9a66e 6334 *last_loc = b->loc;
3086aeae
DJ
6335 break;
6336 }
c906108c 6337
6c95b8df 6338
998580f1 6339 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6340 {
6341 struct inferior *inf;
998580f1
MK
6342 VEC(int) *inf_num = NULL;
6343 int mi_only = 1;
6c95b8df 6344
998580f1 6345 ALL_INFERIORS (inf)
6c95b8df
PA
6346 {
6347 if (inf->pspace == loc->pspace)
998580f1 6348 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6349 }
998580f1
MK
6350
6351 /* For backward compatibility, don't display inferiors in CLI unless
6352 there are several. Always display for MI. */
6353 if (allflag
6354 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6355 && (number_of_program_spaces () > 1
6356 || number_of_inferiors () > 1)
6357 /* LOC is for existing B, it cannot be in
6358 moribund_locations and thus having NULL OWNER. */
6359 && loc->owner->type != bp_catchpoint))
6360 mi_only = 0;
6361 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6362 VEC_free (int, inf_num);
6c95b8df
PA
6363 }
6364
4a306c9a 6365 if (!part_of_multiple)
c4093a6a 6366 {
4a306c9a
JB
6367 if (b->thread != -1)
6368 {
6369 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6370 "stop only in" line a little further down. */
4a306c9a
JB
6371 ui_out_text (uiout, " thread ");
6372 ui_out_field_int (uiout, "thread", b->thread);
6373 }
6374 else if (b->task != 0)
6375 {
6376 ui_out_text (uiout, " task ");
6377 ui_out_field_int (uiout, "task", b->task);
6378 }
c4093a6a 6379 }
f1310107 6380
8b93c638 6381 ui_out_text (uiout, "\n");
f1310107 6382
348d480f 6383 if (!part_of_multiple)
f1310107
TJB
6384 b->ops->print_one_detail (b, uiout);
6385
0d381245 6386 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6387 {
6388 annotate_field (6);
8b93c638 6389 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6390 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6391 the frame ID. */
5af949e3
UW
6392 ui_out_field_core_addr (uiout, "frame",
6393 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6394 ui_out_text (uiout, "\n");
c4093a6a
JM
6395 }
6396
28010a5d 6397 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6398 {
6399 annotate_field (7);
d77f58be 6400 if (is_tracepoint (b))
1042e4c0
SS
6401 ui_out_text (uiout, "\ttrace only if ");
6402 else
6403 ui_out_text (uiout, "\tstop only if ");
0101ce28 6404 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6405
6406 /* Print whether the target is doing the breakpoint's condition
6407 evaluation. If GDB is doing the evaluation, don't print anything. */
6408 if (is_breakpoint (b)
6409 && breakpoint_condition_evaluation_mode ()
6410 == condition_evaluation_target)
6411 {
6412 ui_out_text (uiout, " (");
6413 ui_out_field_string (uiout, "evaluated-by",
6414 bp_condition_evaluator (b));
6415 ui_out_text (uiout, " evals)");
6416 }
0101ce28
JJ
6417 ui_out_text (uiout, "\n");
6418 }
6419
0d381245 6420 if (!part_of_multiple && b->thread != -1)
c4093a6a 6421 {
4a64f543 6422 /* FIXME should make an annotation for this. */
8b93c638
JM
6423 ui_out_text (uiout, "\tstop only in thread ");
6424 ui_out_field_int (uiout, "thread", b->thread);
6425 ui_out_text (uiout, "\n");
c4093a6a
JM
6426 }
6427
556ec64d
YQ
6428 if (!part_of_multiple)
6429 {
6430 if (b->hit_count)
31f56a27
YQ
6431 {
6432 /* FIXME should make an annotation for this. */
6433 if (is_catchpoint (b))
6434 ui_out_text (uiout, "\tcatchpoint");
6435 else if (is_tracepoint (b))
6436 ui_out_text (uiout, "\ttracepoint");
6437 else
6438 ui_out_text (uiout, "\tbreakpoint");
6439 ui_out_text (uiout, " already hit ");
6440 ui_out_field_int (uiout, "times", b->hit_count);
6441 if (b->hit_count == 1)
6442 ui_out_text (uiout, " time\n");
6443 else
6444 ui_out_text (uiout, " times\n");
6445 }
556ec64d
YQ
6446 else
6447 {
31f56a27
YQ
6448 /* Output the count also if it is zero, but only if this is mi. */
6449 if (ui_out_is_mi_like_p (uiout))
6450 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6451 }
6452 }
8b93c638 6453
0d381245 6454 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6455 {
6456 annotate_field (8);
8b93c638
JM
6457 ui_out_text (uiout, "\tignore next ");
6458 ui_out_field_int (uiout, "ignore", b->ignore_count);
6459 ui_out_text (uiout, " hits\n");
c4093a6a 6460 }
059fb39f 6461
816338b5
SS
6462 /* Note that an enable count of 1 corresponds to "enable once"
6463 behavior, which is reported by the combination of enablement and
6464 disposition, so we don't need to mention it here. */
6465 if (!part_of_multiple && b->enable_count > 1)
6466 {
6467 annotate_field (8);
6468 ui_out_text (uiout, "\tdisable after ");
6469 /* Tweak the wording to clarify that ignore and enable counts
6470 are distinct, and have additive effect. */
6471 if (b->ignore_count)
6472 ui_out_text (uiout, "additional ");
6473 else
6474 ui_out_text (uiout, "next ");
6475 ui_out_field_int (uiout, "enable", b->enable_count);
6476 ui_out_text (uiout, " hits\n");
6477 }
6478
f196051f
SS
6479 if (!part_of_multiple && is_tracepoint (b))
6480 {
6481 struct tracepoint *tp = (struct tracepoint *) b;
6482
6483 if (tp->traceframe_usage)
6484 {
6485 ui_out_text (uiout, "\ttrace buffer usage ");
6486 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6487 ui_out_text (uiout, " bytes\n");
6488 }
6489 }
d3ce09f5 6490
9add0f1b 6491 l = b->commands ? b->commands->commands : NULL;
059fb39f 6492 if (!part_of_multiple && l)
c4093a6a 6493 {
3b31d625
EZ
6494 struct cleanup *script_chain;
6495
c4093a6a 6496 annotate_field (9);
3b31d625 6497 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6498 print_command_lines (uiout, l, 4);
3b31d625 6499 do_cleanups (script_chain);
c4093a6a 6500 }
d24317b4 6501
d9b3f62e 6502 if (is_tracepoint (b))
1042e4c0 6503 {
d9b3f62e
PA
6504 struct tracepoint *t = (struct tracepoint *) b;
6505
6506 if (!part_of_multiple && t->pass_count)
6507 {
6508 annotate_field (10);
6509 ui_out_text (uiout, "\tpass count ");
6510 ui_out_field_int (uiout, "pass", t->pass_count);
6511 ui_out_text (uiout, " \n");
6512 }
f2a8bc8a
YQ
6513
6514 /* Don't display it when tracepoint or tracepoint location is
6515 pending. */
6516 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6517 {
6518 annotate_field (11);
6519
6520 if (ui_out_is_mi_like_p (uiout))
6521 ui_out_field_string (uiout, "installed",
6522 loc->inserted ? "y" : "n");
6523 else
6524 {
6525 if (loc->inserted)
6526 ui_out_text (uiout, "\t");
6527 else
6528 ui_out_text (uiout, "\tnot ");
6529 ui_out_text (uiout, "installed on target\n");
6530 }
6531 }
1042e4c0
SS
6532 }
6533
d24317b4
VP
6534 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6535 {
3a5c3e22
PA
6536 if (is_watchpoint (b))
6537 {
6538 struct watchpoint *w = (struct watchpoint *) b;
6539
6540 ui_out_field_string (uiout, "original-location", w->exp_string);
6541 }
6542 else if (b->addr_string)
d24317b4 6543 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6544 }
c4093a6a 6545}
c5aa993b 6546
0d381245
VP
6547static void
6548print_one_breakpoint (struct breakpoint *b,
4a64f543 6549 struct bp_location **last_loc,
6c95b8df 6550 int allflag)
0d381245 6551{
8d3788bd 6552 struct cleanup *bkpt_chain;
79a45e25 6553 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6554
6555 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6556
12c5a436 6557 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6558 do_cleanups (bkpt_chain);
0d381245
VP
6559
6560 /* If this breakpoint has custom print function,
6561 it's already printed. Otherwise, print individual
6562 locations, if any. */
6563 if (b->ops == NULL || b->ops->print_one == NULL)
6564 {
4a64f543
MS
6565 /* If breakpoint has a single location that is disabled, we
6566 print it as if it had several locations, since otherwise it's
6567 hard to represent "breakpoint enabled, location disabled"
6568 situation.
6569
6570 Note that while hardware watchpoints have several locations
a3be7890 6571 internally, that's not a property exposed to user. */
0d381245 6572 if (b->loc
a5606eee 6573 && !is_hardware_watchpoint (b)
8d3788bd 6574 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6575 {
6576 struct bp_location *loc;
6577 int n = 1;
8d3788bd 6578
0d381245 6579 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6580 {
6581 struct cleanup *inner2 =
6582 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6583 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6584 do_cleanups (inner2);
6585 }
0d381245
VP
6586 }
6587 }
6588}
6589
a6d9a66e
UW
6590static int
6591breakpoint_address_bits (struct breakpoint *b)
6592{
6593 int print_address_bits = 0;
6594 struct bp_location *loc;
6595
6596 for (loc = b->loc; loc; loc = loc->next)
6597 {
c7437ca6
PA
6598 int addr_bit;
6599
6600 /* Software watchpoints that aren't watching memory don't have
6601 an address to print. */
6602 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6603 continue;
6604
6605 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6606 if (addr_bit > print_address_bits)
6607 print_address_bits = addr_bit;
6608 }
6609
6610 return print_address_bits;
6611}
0d381245 6612
c4093a6a
JM
6613struct captured_breakpoint_query_args
6614 {
6615 int bnum;
6616 };
c5aa993b 6617
c4093a6a 6618static int
2b65245e 6619do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6620{
6621 struct captured_breakpoint_query_args *args = data;
52f0bd74 6622 struct breakpoint *b;
a6d9a66e 6623 struct bp_location *dummy_loc = NULL;
cc59ec59 6624
c4093a6a
JM
6625 ALL_BREAKPOINTS (b)
6626 {
6627 if (args->bnum == b->number)
c5aa993b 6628 {
12c5a436 6629 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6630 return GDB_RC_OK;
c5aa993b 6631 }
c4093a6a
JM
6632 }
6633 return GDB_RC_NONE;
6634}
c5aa993b 6635
c4093a6a 6636enum gdb_rc
4a64f543
MS
6637gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6638 char **error_message)
c4093a6a
JM
6639{
6640 struct captured_breakpoint_query_args args;
cc59ec59 6641
c4093a6a
JM
6642 args.bnum = bnum;
6643 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6644 an error. */
b0b13bb4
DJ
6645 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6646 error_message, RETURN_MASK_ALL) < 0)
6647 return GDB_RC_FAIL;
6648 else
6649 return GDB_RC_OK;
c4093a6a 6650}
c5aa993b 6651
09d682a4
TT
6652/* Return true if this breakpoint was set by the user, false if it is
6653 internal or momentary. */
6654
6655int
6656user_breakpoint_p (struct breakpoint *b)
6657{
46c6471b 6658 return b->number > 0;
09d682a4
TT
6659}
6660
7f3b0473 6661/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6662 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6663 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6664 FILTER is non-NULL, call it on each breakpoint and only include the
6665 ones for which it returns non-zero. Return the total number of
6666 breakpoints listed. */
c906108c 6667
d77f58be 6668static int
e5a67952 6669breakpoint_1 (char *args, int allflag,
4a64f543 6670 int (*filter) (const struct breakpoint *))
c4093a6a 6671{
52f0bd74 6672 struct breakpoint *b;
a6d9a66e 6673 struct bp_location *last_loc = NULL;
7f3b0473 6674 int nr_printable_breakpoints;
3b31d625 6675 struct cleanup *bkpttbl_chain;
79a45b7d 6676 struct value_print_options opts;
a6d9a66e 6677 int print_address_bits = 0;
269b11a2 6678 int print_type_col_width = 14;
79a45e25 6679 struct ui_out *uiout = current_uiout;
269b11a2 6680
79a45b7d
TT
6681 get_user_print_options (&opts);
6682
4a64f543
MS
6683 /* Compute the number of rows in the table, as well as the size
6684 required for address fields. */
7f3b0473
AC
6685 nr_printable_breakpoints = 0;
6686 ALL_BREAKPOINTS (b)
e5a67952
MS
6687 {
6688 /* If we have a filter, only list the breakpoints it accepts. */
6689 if (filter && !filter (b))
6690 continue;
6691
6692 /* If we have an "args" string, it is a list of breakpoints to
6693 accept. Skip the others. */
6694 if (args != NULL && *args != '\0')
6695 {
6696 if (allflag && parse_and_eval_long (args) != b->number)
6697 continue;
6698 if (!allflag && !number_is_in_list (args, b->number))
6699 continue;
6700 }
269b11a2 6701
e5a67952
MS
6702 if (allflag || user_breakpoint_p (b))
6703 {
6704 int addr_bit, type_len;
a6d9a66e 6705
e5a67952
MS
6706 addr_bit = breakpoint_address_bits (b);
6707 if (addr_bit > print_address_bits)
6708 print_address_bits = addr_bit;
269b11a2 6709
e5a67952
MS
6710 type_len = strlen (bptype_string (b->type));
6711 if (type_len > print_type_col_width)
6712 print_type_col_width = type_len;
6713
6714 nr_printable_breakpoints++;
6715 }
6716 }
7f3b0473 6717
79a45b7d 6718 if (opts.addressprint)
3b31d625 6719 bkpttbl_chain
3e43a32a
MS
6720 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6721 nr_printable_breakpoints,
3b31d625 6722 "BreakpointTable");
8b93c638 6723 else
3b31d625 6724 bkpttbl_chain
3e43a32a
MS
6725 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6726 nr_printable_breakpoints,
3b31d625 6727 "BreakpointTable");
8b93c638 6728
7f3b0473 6729 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6730 annotate_breakpoints_headers ();
6731 if (nr_printable_breakpoints > 0)
6732 annotate_field (0);
4a64f543 6733 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6734 if (nr_printable_breakpoints > 0)
6735 annotate_field (1);
269b11a2 6736 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6737 "type", "Type"); /* 2 */
d7faa9e7
AC
6738 if (nr_printable_breakpoints > 0)
6739 annotate_field (2);
4a64f543 6740 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6741 if (nr_printable_breakpoints > 0)
6742 annotate_field (3);
54e52265 6743 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6744 if (opts.addressprint)
e5a67952
MS
6745 {
6746 if (nr_printable_breakpoints > 0)
6747 annotate_field (4);
6748 if (print_address_bits <= 32)
6749 ui_out_table_header (uiout, 10, ui_left,
6750 "addr", "Address"); /* 5 */
6751 else
6752 ui_out_table_header (uiout, 18, ui_left,
6753 "addr", "Address"); /* 5 */
6754 }
d7faa9e7
AC
6755 if (nr_printable_breakpoints > 0)
6756 annotate_field (5);
6757 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6758 ui_out_table_body (uiout);
6759 if (nr_printable_breakpoints > 0)
6760 annotate_breakpoints_table ();
7f3b0473 6761
c4093a6a 6762 ALL_BREAKPOINTS (b)
e5a67952
MS
6763 {
6764 QUIT;
6765 /* If we have a filter, only list the breakpoints it accepts. */
6766 if (filter && !filter (b))
6767 continue;
6768
6769 /* If we have an "args" string, it is a list of breakpoints to
6770 accept. Skip the others. */
6771
6772 if (args != NULL && *args != '\0')
6773 {
6774 if (allflag) /* maintenance info breakpoint */
6775 {
6776 if (parse_and_eval_long (args) != b->number)
6777 continue;
6778 }
6779 else /* all others */
6780 {
6781 if (!number_is_in_list (args, b->number))
6782 continue;
6783 }
6784 }
6785 /* We only print out user settable breakpoints unless the
6786 allflag is set. */
6787 if (allflag || user_breakpoint_p (b))
12c5a436 6788 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6789 }
6790
3b31d625 6791 do_cleanups (bkpttbl_chain);
698384cd 6792
7f3b0473 6793 if (nr_printable_breakpoints == 0)
c906108c 6794 {
4a64f543
MS
6795 /* If there's a filter, let the caller decide how to report
6796 empty list. */
d77f58be
SS
6797 if (!filter)
6798 {
e5a67952 6799 if (args == NULL || *args == '\0')
d77f58be
SS
6800 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6801 else
4a64f543 6802 ui_out_message (uiout, 0,
e5a67952
MS
6803 "No breakpoint or watchpoint matching '%s'.\n",
6804 args);
d77f58be 6805 }
c906108c
SS
6806 }
6807 else
c4093a6a 6808 {
a6d9a66e
UW
6809 if (last_loc && !server_command)
6810 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6811 }
c906108c 6812
4a64f543 6813 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6814 there have been breakpoints? */
c906108c 6815 annotate_breakpoints_table_end ();
d77f58be
SS
6816
6817 return nr_printable_breakpoints;
c906108c
SS
6818}
6819
ad443146
SS
6820/* Display the value of default-collect in a way that is generally
6821 compatible with the breakpoint list. */
6822
6823static void
6824default_collect_info (void)
6825{
79a45e25
PA
6826 struct ui_out *uiout = current_uiout;
6827
ad443146
SS
6828 /* If it has no value (which is frequently the case), say nothing; a
6829 message like "No default-collect." gets in user's face when it's
6830 not wanted. */
6831 if (!*default_collect)
6832 return;
6833
6834 /* The following phrase lines up nicely with per-tracepoint collect
6835 actions. */
6836 ui_out_text (uiout, "default collect ");
6837 ui_out_field_string (uiout, "default-collect", default_collect);
6838 ui_out_text (uiout, " \n");
6839}
6840
c906108c 6841static void
e5a67952 6842breakpoints_info (char *args, int from_tty)
c906108c 6843{
e5a67952 6844 breakpoint_1 (args, 0, NULL);
ad443146
SS
6845
6846 default_collect_info ();
d77f58be
SS
6847}
6848
6849static void
e5a67952 6850watchpoints_info (char *args, int from_tty)
d77f58be 6851{
e5a67952 6852 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6853 struct ui_out *uiout = current_uiout;
d77f58be
SS
6854
6855 if (num_printed == 0)
6856 {
e5a67952 6857 if (args == NULL || *args == '\0')
d77f58be
SS
6858 ui_out_message (uiout, 0, "No watchpoints.\n");
6859 else
e5a67952 6860 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6861 }
c906108c
SS
6862}
6863
7a292a7a 6864static void
e5a67952 6865maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6866{
e5a67952 6867 breakpoint_1 (args, 1, NULL);
ad443146
SS
6868
6869 default_collect_info ();
c906108c
SS
6870}
6871
0d381245 6872static int
714835d5 6873breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6874 struct program_space *pspace,
714835d5 6875 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6876{
6877 struct bp_location *bl = b->loc;
cc59ec59 6878
0d381245
VP
6879 for (; bl; bl = bl->next)
6880 {
6c95b8df
PA
6881 if (bl->pspace == pspace
6882 && bl->address == pc
0d381245
VP
6883 && (!overlay_debugging || bl->section == section))
6884 return 1;
6885 }
6886 return 0;
6887}
6888
672f9b60 6889/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6890 concerns with logical breakpoints, so we match program spaces, not
6891 address spaces. */
c906108c
SS
6892
6893static void
6c95b8df
PA
6894describe_other_breakpoints (struct gdbarch *gdbarch,
6895 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6896 struct obj_section *section, int thread)
c906108c 6897{
52f0bd74
AC
6898 int others = 0;
6899 struct breakpoint *b;
c906108c
SS
6900
6901 ALL_BREAKPOINTS (b)
672f9b60
KP
6902 others += (user_breakpoint_p (b)
6903 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6904 if (others > 0)
6905 {
a3f17187
AC
6906 if (others == 1)
6907 printf_filtered (_("Note: breakpoint "));
6908 else /* if (others == ???) */
6909 printf_filtered (_("Note: breakpoints "));
c906108c 6910 ALL_BREAKPOINTS (b)
672f9b60 6911 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6912 {
6913 others--;
6914 printf_filtered ("%d", b->number);
6915 if (b->thread == -1 && thread != -1)
6916 printf_filtered (" (all threads)");
6917 else if (b->thread != -1)
6918 printf_filtered (" (thread %d)", b->thread);
6919 printf_filtered ("%s%s ",
059fb39f 6920 ((b->enable_state == bp_disabled
f8eba3c6 6921 || b->enable_state == bp_call_disabled)
0d381245
VP
6922 ? " (disabled)"
6923 : b->enable_state == bp_permanent
6924 ? " (permanent)"
6925 : ""),
6926 (others > 1) ? ","
6927 : ((others == 1) ? " and" : ""));
6928 }
a3f17187 6929 printf_filtered (_("also set at pc "));
5af949e3 6930 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6931 printf_filtered (".\n");
6932 }
6933}
6934\f
c906108c 6935
e4f237da
KB
6936/* Return true iff it is meaningful to use the address member of
6937 BPT. For some breakpoint types, the address member is irrelevant
6938 and it makes no sense to attempt to compare it to other addresses
6939 (or use it for any other purpose either).
6940
4a64f543
MS
6941 More specifically, each of the following breakpoint types will
6942 always have a zero valued address and we don't want to mark
6943 breakpoints of any of these types to be a duplicate of an actual
6944 breakpoint at address zero:
e4f237da
KB
6945
6946 bp_watchpoint
2d134ed3
PA
6947 bp_catchpoint
6948
6949*/
e4f237da
KB
6950
6951static int
6952breakpoint_address_is_meaningful (struct breakpoint *bpt)
6953{
6954 enum bptype type = bpt->type;
6955
2d134ed3
PA
6956 return (type != bp_watchpoint && type != bp_catchpoint);
6957}
6958
6959/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6960 true if LOC1 and LOC2 represent the same watchpoint location. */
6961
6962static int
4a64f543
MS
6963watchpoint_locations_match (struct bp_location *loc1,
6964 struct bp_location *loc2)
2d134ed3 6965{
3a5c3e22
PA
6966 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6967 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6968
6969 /* Both of them must exist. */
6970 gdb_assert (w1 != NULL);
6971 gdb_assert (w2 != NULL);
2bdf28a0 6972
4a64f543
MS
6973 /* If the target can evaluate the condition expression in hardware,
6974 then we we need to insert both watchpoints even if they are at
6975 the same place. Otherwise the watchpoint will only trigger when
6976 the condition of whichever watchpoint was inserted evaluates to
6977 true, not giving a chance for GDB to check the condition of the
6978 other watchpoint. */
3a5c3e22 6979 if ((w1->cond_exp
4a64f543
MS
6980 && target_can_accel_watchpoint_condition (loc1->address,
6981 loc1->length,
0cf6dd15 6982 loc1->watchpoint_type,
3a5c3e22
PA
6983 w1->cond_exp))
6984 || (w2->cond_exp
4a64f543
MS
6985 && target_can_accel_watchpoint_condition (loc2->address,
6986 loc2->length,
0cf6dd15 6987 loc2->watchpoint_type,
3a5c3e22 6988 w2->cond_exp)))
0cf6dd15
TJB
6989 return 0;
6990
85d721b8
PA
6991 /* Note that this checks the owner's type, not the location's. In
6992 case the target does not support read watchpoints, but does
6993 support access watchpoints, we'll have bp_read_watchpoint
6994 watchpoints with hw_access locations. Those should be considered
6995 duplicates of hw_read locations. The hw_read locations will
6996 become hw_access locations later. */
2d134ed3
PA
6997 return (loc1->owner->type == loc2->owner->type
6998 && loc1->pspace->aspace == loc2->pspace->aspace
6999 && loc1->address == loc2->address
7000 && loc1->length == loc2->length);
e4f237da
KB
7001}
7002
31e77af2 7003/* See breakpoint.h. */
6c95b8df 7004
31e77af2 7005int
6c95b8df
PA
7006breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7007 struct address_space *aspace2, CORE_ADDR addr2)
7008{
f5656ead 7009 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7010 || aspace1 == aspace2)
7011 && addr1 == addr2);
7012}
7013
f1310107
TJB
7014/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7015 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7016 matches ASPACE2. On targets that have global breakpoints, the address
7017 space doesn't really matter. */
7018
7019static int
7020breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7021 int len1, struct address_space *aspace2,
7022 CORE_ADDR addr2)
7023{
f5656ead 7024 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7025 || aspace1 == aspace2)
7026 && addr2 >= addr1 && addr2 < addr1 + len1);
7027}
7028
7029/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7030 a ranged breakpoint. In most targets, a match happens only if ASPACE
7031 matches the breakpoint's address space. On targets that have global
7032 breakpoints, the address space doesn't really matter. */
7033
7034static int
7035breakpoint_location_address_match (struct bp_location *bl,
7036 struct address_space *aspace,
7037 CORE_ADDR addr)
7038{
7039 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7040 aspace, addr)
7041 || (bl->length
7042 && breakpoint_address_match_range (bl->pspace->aspace,
7043 bl->address, bl->length,
7044 aspace, addr)));
7045}
7046
1e4d1764
YQ
7047/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7048 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7049 true, otherwise returns false. */
7050
7051static int
7052tracepoint_locations_match (struct bp_location *loc1,
7053 struct bp_location *loc2)
7054{
7055 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7056 /* Since tracepoint locations are never duplicated with others', tracepoint
7057 locations at the same address of different tracepoints are regarded as
7058 different locations. */
7059 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7060 else
7061 return 0;
7062}
7063
2d134ed3
PA
7064/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7065 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7066 represent the same location. */
7067
7068static int
4a64f543
MS
7069breakpoint_locations_match (struct bp_location *loc1,
7070 struct bp_location *loc2)
2d134ed3 7071{
2bdf28a0
JK
7072 int hw_point1, hw_point2;
7073
7074 /* Both of them must not be in moribund_locations. */
7075 gdb_assert (loc1->owner != NULL);
7076 gdb_assert (loc2->owner != NULL);
7077
7078 hw_point1 = is_hardware_watchpoint (loc1->owner);
7079 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7080
7081 if (hw_point1 != hw_point2)
7082 return 0;
7083 else if (hw_point1)
7084 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7085 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7086 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7087 else
f1310107
TJB
7088 /* We compare bp_location.length in order to cover ranged breakpoints. */
7089 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7090 loc2->pspace->aspace, loc2->address)
7091 && loc1->length == loc2->length);
2d134ed3
PA
7092}
7093
76897487
KB
7094static void
7095breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7096 int bnum, int have_bnum)
7097{
f63fbe86
MS
7098 /* The longest string possibly returned by hex_string_custom
7099 is 50 chars. These must be at least that big for safety. */
7100 char astr1[64];
7101 char astr2[64];
76897487 7102
bb599908
PH
7103 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7104 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7105 if (have_bnum)
8a3fe4f8 7106 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7107 bnum, astr1, astr2);
7108 else
8a3fe4f8 7109 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7110}
7111
4a64f543
MS
7112/* Adjust a breakpoint's address to account for architectural
7113 constraints on breakpoint placement. Return the adjusted address.
7114 Note: Very few targets require this kind of adjustment. For most
7115 targets, this function is simply the identity function. */
76897487
KB
7116
7117static CORE_ADDR
a6d9a66e
UW
7118adjust_breakpoint_address (struct gdbarch *gdbarch,
7119 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7120{
a6d9a66e 7121 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7122 {
7123 /* Very few targets need any kind of breakpoint adjustment. */
7124 return bpaddr;
7125 }
88f7da05
KB
7126 else if (bptype == bp_watchpoint
7127 || bptype == bp_hardware_watchpoint
7128 || bptype == bp_read_watchpoint
7129 || bptype == bp_access_watchpoint
fe798b75 7130 || bptype == bp_catchpoint)
88f7da05
KB
7131 {
7132 /* Watchpoints and the various bp_catch_* eventpoints should not
7133 have their addresses modified. */
7134 return bpaddr;
7135 }
7c16b83e
PA
7136 else if (bptype == bp_single_step)
7137 {
7138 /* Single-step breakpoints should not have their addresses
7139 modified. If there's any architectural constrain that
7140 applies to this address, then it should have already been
7141 taken into account when the breakpoint was created in the
7142 first place. If we didn't do this, stepping through e.g.,
7143 Thumb-2 IT blocks would break. */
7144 return bpaddr;
7145 }
76897487
KB
7146 else
7147 {
7148 CORE_ADDR adjusted_bpaddr;
7149
7150 /* Some targets have architectural constraints on the placement
7151 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7152 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7153
7154 /* An adjusted breakpoint address can significantly alter
7155 a user's expectations. Print a warning if an adjustment
7156 is required. */
7157 if (adjusted_bpaddr != bpaddr)
7158 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7159
7160 return adjusted_bpaddr;
7161 }
7162}
7163
28010a5d
PA
7164void
7165init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7166 struct breakpoint *owner)
7cc221ef 7167{
7cc221ef
DJ
7168 memset (loc, 0, sizeof (*loc));
7169
348d480f
PA
7170 gdb_assert (ops != NULL);
7171
28010a5d
PA
7172 loc->ops = ops;
7173 loc->owner = owner;
511a6cd4 7174 loc->cond = NULL;
b775012e 7175 loc->cond_bytecode = NULL;
0d381245
VP
7176 loc->shlib_disabled = 0;
7177 loc->enabled = 1;
e049a4b5 7178
28010a5d 7179 switch (owner->type)
e049a4b5
DJ
7180 {
7181 case bp_breakpoint:
7c16b83e 7182 case bp_single_step:
e049a4b5
DJ
7183 case bp_until:
7184 case bp_finish:
7185 case bp_longjmp:
7186 case bp_longjmp_resume:
e2e4d78b 7187 case bp_longjmp_call_dummy:
186c406b
TT
7188 case bp_exception:
7189 case bp_exception_resume:
e049a4b5 7190 case bp_step_resume:
2c03e5be 7191 case bp_hp_step_resume:
e049a4b5
DJ
7192 case bp_watchpoint_scope:
7193 case bp_call_dummy:
aa7d318d 7194 case bp_std_terminate:
e049a4b5
DJ
7195 case bp_shlib_event:
7196 case bp_thread_event:
7197 case bp_overlay_event:
4efc6507 7198 case bp_jit_event:
0fd8e87f 7199 case bp_longjmp_master:
aa7d318d 7200 case bp_std_terminate_master:
186c406b 7201 case bp_exception_master:
0e30163f
JK
7202 case bp_gnu_ifunc_resolver:
7203 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7204 case bp_dprintf:
e049a4b5 7205 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7206 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7207 break;
7208 case bp_hardware_breakpoint:
7209 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7210 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7211 break;
7212 case bp_hardware_watchpoint:
7213 case bp_read_watchpoint:
7214 case bp_access_watchpoint:
7215 loc->loc_type = bp_loc_hardware_watchpoint;
7216 break;
7217 case bp_watchpoint:
ce78b96d 7218 case bp_catchpoint:
15c3d785
PA
7219 case bp_tracepoint:
7220 case bp_fast_tracepoint:
0fb4aa4b 7221 case bp_static_tracepoint:
e049a4b5
DJ
7222 loc->loc_type = bp_loc_other;
7223 break;
7224 default:
e2e0b3e5 7225 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7226 }
7227
f431efe5 7228 loc->refc = 1;
28010a5d
PA
7229}
7230
7231/* Allocate a struct bp_location. */
7232
7233static struct bp_location *
7234allocate_bp_location (struct breakpoint *bpt)
7235{
348d480f
PA
7236 return bpt->ops->allocate_location (bpt);
7237}
7cc221ef 7238
f431efe5
PA
7239static void
7240free_bp_location (struct bp_location *loc)
fe3f5fa8 7241{
348d480f 7242 loc->ops->dtor (loc);
fe3f5fa8
VP
7243 xfree (loc);
7244}
7245
f431efe5
PA
7246/* Increment reference count. */
7247
7248static void
7249incref_bp_location (struct bp_location *bl)
7250{
7251 ++bl->refc;
7252}
7253
7254/* Decrement reference count. If the reference count reaches 0,
7255 destroy the bp_location. Sets *BLP to NULL. */
7256
7257static void
7258decref_bp_location (struct bp_location **blp)
7259{
0807b50c
PA
7260 gdb_assert ((*blp)->refc > 0);
7261
f431efe5
PA
7262 if (--(*blp)->refc == 0)
7263 free_bp_location (*blp);
7264 *blp = NULL;
7265}
7266
346774a9 7267/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7268
346774a9
PA
7269static void
7270add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7271{
346774a9 7272 struct breakpoint *b1;
c906108c 7273
346774a9
PA
7274 /* Add this breakpoint to the end of the chain so that a list of
7275 breakpoints will come out in order of increasing numbers. */
7276
7277 b1 = breakpoint_chain;
7278 if (b1 == 0)
7279 breakpoint_chain = b;
7280 else
7281 {
7282 while (b1->next)
7283 b1 = b1->next;
7284 b1->next = b;
7285 }
7286}
7287
7288/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7289
7290static void
7291init_raw_breakpoint_without_location (struct breakpoint *b,
7292 struct gdbarch *gdbarch,
28010a5d 7293 enum bptype bptype,
c0a91b2b 7294 const struct breakpoint_ops *ops)
346774a9 7295{
c906108c 7296 memset (b, 0, sizeof (*b));
2219d63c 7297
348d480f
PA
7298 gdb_assert (ops != NULL);
7299
28010a5d 7300 b->ops = ops;
4d28f7a8 7301 b->type = bptype;
a6d9a66e 7302 b->gdbarch = gdbarch;
c906108c
SS
7303 b->language = current_language->la_language;
7304 b->input_radix = input_radix;
7305 b->thread = -1;
b5de0fa7 7306 b->enable_state = bp_enabled;
c906108c
SS
7307 b->next = 0;
7308 b->silent = 0;
7309 b->ignore_count = 0;
7310 b->commands = NULL;
818dd999 7311 b->frame_id = null_frame_id;
0d381245 7312 b->condition_not_parsed = 0;
84f4c1fe 7313 b->py_bp_object = NULL;
d0fb5eae 7314 b->related_breakpoint = b;
346774a9
PA
7315}
7316
7317/* Helper to set_raw_breakpoint below. Creates a breakpoint
7318 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7319
7320static struct breakpoint *
7321set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7322 enum bptype bptype,
c0a91b2b 7323 const struct breakpoint_ops *ops)
346774a9
PA
7324{
7325 struct breakpoint *b = XNEW (struct breakpoint);
7326
348d480f 7327 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7328 add_to_breakpoint_chain (b);
0d381245
VP
7329 return b;
7330}
7331
0e30163f
JK
7332/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7333 resolutions should be made as the user specified the location explicitly
7334 enough. */
7335
0d381245 7336static void
0e30163f 7337set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7338{
2bdf28a0
JK
7339 gdb_assert (loc->owner != NULL);
7340
0d381245 7341 if (loc->owner->type == bp_breakpoint
1042e4c0 7342 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7343 || is_tracepoint (loc->owner))
0d381245 7344 {
0e30163f 7345 int is_gnu_ifunc;
2c02bd72 7346 const char *function_name;
6a3a010b 7347 CORE_ADDR func_addr;
0e30163f 7348
2c02bd72 7349 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7350 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7351
7352 if (is_gnu_ifunc && !explicit_loc)
7353 {
7354 struct breakpoint *b = loc->owner;
7355
7356 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7357 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7358 &loc->requested_address))
7359 {
7360 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7361 loc->address = adjust_breakpoint_address (loc->gdbarch,
7362 loc->requested_address,
7363 b->type);
7364 }
7365 else if (b->type == bp_breakpoint && b->loc == loc
7366 && loc->next == NULL && b->related_breakpoint == b)
7367 {
7368 /* Create only the whole new breakpoint of this type but do not
7369 mess more complicated breakpoints with multiple locations. */
7370 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7371 /* Remember the resolver's address for use by the return
7372 breakpoint. */
7373 loc->related_address = func_addr;
0e30163f
JK
7374 }
7375 }
7376
2c02bd72
DE
7377 if (function_name)
7378 loc->function_name = xstrdup (function_name);
0d381245
VP
7379 }
7380}
7381
a6d9a66e 7382/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7383struct gdbarch *
a6d9a66e
UW
7384get_sal_arch (struct symtab_and_line sal)
7385{
7386 if (sal.section)
7387 return get_objfile_arch (sal.section->objfile);
7388 if (sal.symtab)
7389 return get_objfile_arch (sal.symtab->objfile);
7390
7391 return NULL;
7392}
7393
346774a9
PA
7394/* Low level routine for partially initializing a breakpoint of type
7395 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7396 file name, and line number are provided by SAL.
0d381245
VP
7397
7398 It is expected that the caller will complete the initialization of
7399 the newly created breakpoint struct as well as output any status
c56053d2 7400 information regarding the creation of a new breakpoint. */
0d381245 7401
346774a9
PA
7402static void
7403init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7404 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7405 const struct breakpoint_ops *ops)
0d381245 7406{
28010a5d 7407 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7408
3742cc8b 7409 add_location_to_breakpoint (b, &sal);
0d381245 7410
6c95b8df
PA
7411 if (bptype != bp_catchpoint)
7412 gdb_assert (sal.pspace != NULL);
7413
f8eba3c6
TT
7414 /* Store the program space that was used to set the breakpoint,
7415 except for ordinary breakpoints, which are independent of the
7416 program space. */
7417 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7418 b->pspace = sal.pspace;
346774a9 7419}
c906108c 7420
346774a9
PA
7421/* set_raw_breakpoint is a low level routine for allocating and
7422 partially initializing a breakpoint of type BPTYPE. The newly
7423 created breakpoint's address, section, source file name, and line
7424 number are provided by SAL. The newly created and partially
7425 initialized breakpoint is added to the breakpoint chain and
7426 is also returned as the value of this function.
7427
7428 It is expected that the caller will complete the initialization of
7429 the newly created breakpoint struct as well as output any status
7430 information regarding the creation of a new breakpoint. In
7431 particular, set_raw_breakpoint does NOT set the breakpoint
7432 number! Care should be taken to not allow an error to occur
7433 prior to completing the initialization of the breakpoint. If this
7434 should happen, a bogus breakpoint will be left on the chain. */
7435
7436struct breakpoint *
7437set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7438 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7439 const struct breakpoint_ops *ops)
346774a9
PA
7440{
7441 struct breakpoint *b = XNEW (struct breakpoint);
7442
348d480f 7443 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7444 add_to_breakpoint_chain (b);
c906108c
SS
7445 return b;
7446}
7447
c2c6d25f
JM
7448
7449/* Note that the breakpoint object B describes a permanent breakpoint
7450 instruction, hard-wired into the inferior's code. */
7451void
7452make_breakpoint_permanent (struct breakpoint *b)
7453{
0d381245 7454 struct bp_location *bl;
cc59ec59 7455
b5de0fa7 7456 b->enable_state = bp_permanent;
c2c6d25f 7457
4a64f543
MS
7458 /* By definition, permanent breakpoints are already present in the
7459 code. Mark all locations as inserted. For now,
7460 make_breakpoint_permanent is called in just one place, so it's
7461 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7462 multiple locations or not, but it's easy to implement. */
0d381245
VP
7463 for (bl = b->loc; bl; bl = bl->next)
7464 bl->inserted = 1;
c2c6d25f
JM
7465}
7466
53a5351d 7467/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7468 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7469 initiated the operation. */
c906108c
SS
7470
7471void
186c406b 7472set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7473{
35df4500 7474 struct breakpoint *b, *b_tmp;
186c406b 7475 int thread = tp->num;
0fd8e87f
UW
7476
7477 /* To avoid having to rescan all objfile symbols at every step,
7478 we maintain a list of continually-inserted but always disabled
7479 longjmp "master" breakpoints. Here, we simply create momentary
7480 clones of those and enable them for the requested thread. */
35df4500 7481 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7482 if (b->pspace == current_program_space
186c406b
TT
7483 && (b->type == bp_longjmp_master
7484 || b->type == bp_exception_master))
0fd8e87f 7485 {
06edf0c0
PA
7486 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7487 struct breakpoint *clone;
cc59ec59 7488
e2e4d78b
JK
7489 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7490 after their removal. */
06edf0c0 7491 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7492 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7493 clone->thread = thread;
7494 }
186c406b
TT
7495
7496 tp->initiating_frame = frame;
c906108c
SS
7497}
7498
611c83ae 7499/* Delete all longjmp breakpoints from THREAD. */
c906108c 7500void
611c83ae 7501delete_longjmp_breakpoint (int thread)
c906108c 7502{
35df4500 7503 struct breakpoint *b, *b_tmp;
c906108c 7504
35df4500 7505 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7506 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7507 {
7508 if (b->thread == thread)
7509 delete_breakpoint (b);
7510 }
c906108c
SS
7511}
7512
f59f708a
PA
7513void
7514delete_longjmp_breakpoint_at_next_stop (int thread)
7515{
7516 struct breakpoint *b, *b_tmp;
7517
7518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7519 if (b->type == bp_longjmp || b->type == bp_exception)
7520 {
7521 if (b->thread == thread)
7522 b->disposition = disp_del_at_next_stop;
7523 }
7524}
7525
e2e4d78b
JK
7526/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7527 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7528 pointer to any of them. Return NULL if this system cannot place longjmp
7529 breakpoints. */
7530
7531struct breakpoint *
7532set_longjmp_breakpoint_for_call_dummy (void)
7533{
7534 struct breakpoint *b, *retval = NULL;
7535
7536 ALL_BREAKPOINTS (b)
7537 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7538 {
7539 struct breakpoint *new_b;
7540
7541 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7542 &momentary_breakpoint_ops,
7543 1);
e2e4d78b
JK
7544 new_b->thread = pid_to_thread_id (inferior_ptid);
7545
7546 /* Link NEW_B into the chain of RETVAL breakpoints. */
7547
7548 gdb_assert (new_b->related_breakpoint == new_b);
7549 if (retval == NULL)
7550 retval = new_b;
7551 new_b->related_breakpoint = retval;
7552 while (retval->related_breakpoint != new_b->related_breakpoint)
7553 retval = retval->related_breakpoint;
7554 retval->related_breakpoint = new_b;
7555 }
7556
7557 return retval;
7558}
7559
7560/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7561 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7562 stack.
7563
7564 You should call this function only at places where it is safe to currently
7565 unwind the whole stack. Failed stack unwind would discard live dummy
7566 frames. */
7567
7568void
b67a2c6f 7569check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7570{
7571 struct breakpoint *b, *b_tmp;
7572
7573 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7574 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7575 {
7576 struct breakpoint *dummy_b = b->related_breakpoint;
7577
7578 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7579 dummy_b = dummy_b->related_breakpoint;
7580 if (dummy_b->type != bp_call_dummy
7581 || frame_find_by_id (dummy_b->frame_id) != NULL)
7582 continue;
7583
b67a2c6f 7584 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7585
7586 while (b->related_breakpoint != b)
7587 {
7588 if (b_tmp == b->related_breakpoint)
7589 b_tmp = b->related_breakpoint->next;
7590 delete_breakpoint (b->related_breakpoint);
7591 }
7592 delete_breakpoint (b);
7593 }
7594}
7595
1900040c
MS
7596void
7597enable_overlay_breakpoints (void)
7598{
52f0bd74 7599 struct breakpoint *b;
1900040c
MS
7600
7601 ALL_BREAKPOINTS (b)
7602 if (b->type == bp_overlay_event)
7603 {
7604 b->enable_state = bp_enabled;
44702360 7605 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7606 overlay_events_enabled = 1;
1900040c
MS
7607 }
7608}
7609
7610void
7611disable_overlay_breakpoints (void)
7612{
52f0bd74 7613 struct breakpoint *b;
1900040c
MS
7614
7615 ALL_BREAKPOINTS (b)
7616 if (b->type == bp_overlay_event)
7617 {
7618 b->enable_state = bp_disabled;
44702360 7619 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7620 overlay_events_enabled = 0;
1900040c
MS
7621 }
7622}
7623
aa7d318d
TT
7624/* Set an active std::terminate breakpoint for each std::terminate
7625 master breakpoint. */
7626void
7627set_std_terminate_breakpoint (void)
7628{
35df4500 7629 struct breakpoint *b, *b_tmp;
aa7d318d 7630
35df4500 7631 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7632 if (b->pspace == current_program_space
7633 && b->type == bp_std_terminate_master)
7634 {
06edf0c0 7635 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7636 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7637 }
7638}
7639
7640/* Delete all the std::terminate breakpoints. */
7641void
7642delete_std_terminate_breakpoint (void)
7643{
35df4500 7644 struct breakpoint *b, *b_tmp;
aa7d318d 7645
35df4500 7646 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7647 if (b->type == bp_std_terminate)
7648 delete_breakpoint (b);
7649}
7650
c4093a6a 7651struct breakpoint *
a6d9a66e 7652create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7653{
7654 struct breakpoint *b;
c4093a6a 7655
06edf0c0
PA
7656 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7657 &internal_breakpoint_ops);
7658
b5de0fa7 7659 b->enable_state = bp_enabled;
c4093a6a 7660 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7661 b->addr_string
7662 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7663
44702360 7664 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7665
c4093a6a
JM
7666 return b;
7667}
7668
7669void
7670remove_thread_event_breakpoints (void)
7671{
35df4500 7672 struct breakpoint *b, *b_tmp;
c4093a6a 7673
35df4500 7674 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7675 if (b->type == bp_thread_event
7676 && b->loc->pspace == current_program_space)
c4093a6a
JM
7677 delete_breakpoint (b);
7678}
7679
0101ce28
JJ
7680struct lang_and_radix
7681 {
7682 enum language lang;
7683 int radix;
7684 };
7685
4efc6507
DE
7686/* Create a breakpoint for JIT code registration and unregistration. */
7687
7688struct breakpoint *
7689create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7690{
7691 struct breakpoint *b;
7692
06edf0c0
PA
7693 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7694 &internal_breakpoint_ops);
44702360 7695 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7696 return b;
7697}
0101ce28 7698
03673fc7
PP
7699/* Remove JIT code registration and unregistration breakpoint(s). */
7700
7701void
7702remove_jit_event_breakpoints (void)
7703{
7704 struct breakpoint *b, *b_tmp;
7705
7706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7707 if (b->type == bp_jit_event
7708 && b->loc->pspace == current_program_space)
7709 delete_breakpoint (b);
7710}
7711
cae688ec
JJ
7712void
7713remove_solib_event_breakpoints (void)
7714{
35df4500 7715 struct breakpoint *b, *b_tmp;
cae688ec 7716
35df4500 7717 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7718 if (b->type == bp_shlib_event
7719 && b->loc->pspace == current_program_space)
cae688ec
JJ
7720 delete_breakpoint (b);
7721}
7722
f37f681c
PA
7723/* See breakpoint.h. */
7724
7725void
7726remove_solib_event_breakpoints_at_next_stop (void)
7727{
7728 struct breakpoint *b, *b_tmp;
7729
7730 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7731 if (b->type == bp_shlib_event
7732 && b->loc->pspace == current_program_space)
7733 b->disposition = disp_del_at_next_stop;
7734}
7735
04086b45
PA
7736/* Helper for create_solib_event_breakpoint /
7737 create_and_insert_solib_event_breakpoint. Allows specifying which
7738 INSERT_MODE to pass through to update_global_location_list. */
7739
7740static struct breakpoint *
7741create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7742 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7743{
7744 struct breakpoint *b;
7745
06edf0c0
PA
7746 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7747 &internal_breakpoint_ops);
04086b45 7748 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7749 return b;
7750}
7751
04086b45
PA
7752struct breakpoint *
7753create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7754{
7755 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7756}
7757
f37f681c
PA
7758/* See breakpoint.h. */
7759
7760struct breakpoint *
7761create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7762{
7763 struct breakpoint *b;
7764
04086b45
PA
7765 /* Explicitly tell update_global_location_list to insert
7766 locations. */
7767 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7768 if (!b->loc->inserted)
7769 {
7770 delete_breakpoint (b);
7771 return NULL;
7772 }
7773 return b;
7774}
7775
cae688ec
JJ
7776/* Disable any breakpoints that are on code in shared libraries. Only
7777 apply to enabled breakpoints, disabled ones can just stay disabled. */
7778
7779void
cb851954 7780disable_breakpoints_in_shlibs (void)
cae688ec 7781{
876fa593 7782 struct bp_location *loc, **locp_tmp;
cae688ec 7783
876fa593 7784 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7785 {
2bdf28a0 7786 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7787 struct breakpoint *b = loc->owner;
2bdf28a0 7788
4a64f543
MS
7789 /* We apply the check to all breakpoints, including disabled for
7790 those with loc->duplicate set. This is so that when breakpoint
7791 becomes enabled, or the duplicate is removed, gdb will try to
7792 insert all breakpoints. If we don't set shlib_disabled here,
7793 we'll try to insert those breakpoints and fail. */
1042e4c0 7794 if (((b->type == bp_breakpoint)
508ccb1f 7795 || (b->type == bp_jit_event)
1042e4c0 7796 || (b->type == bp_hardware_breakpoint)
d77f58be 7797 || (is_tracepoint (b)))
6c95b8df 7798 && loc->pspace == current_program_space
0d381245 7799 && !loc->shlib_disabled
6c95b8df 7800 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7801 )
0d381245
VP
7802 {
7803 loc->shlib_disabled = 1;
7804 }
cae688ec
JJ
7805 }
7806}
7807
63644780
NB
7808/* Disable any breakpoints and tracepoints that are in SOLIB upon
7809 notification of unloaded_shlib. Only apply to enabled breakpoints,
7810 disabled ones can just stay disabled. */
84acb35a 7811
75149521 7812static void
84acb35a
JJ
7813disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7814{
876fa593 7815 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7816 int disabled_shlib_breaks = 0;
7817
c86cf029
VP
7818 /* SunOS a.out shared libraries are always mapped, so do not
7819 disable breakpoints; they will only be reported as unloaded
7820 through clear_solib when GDB discards its shared library
7821 list. See clear_solib for more information. */
7822 if (exec_bfd != NULL
7823 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7824 return;
7825
876fa593 7826 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7827 {
2bdf28a0 7828 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7829 struct breakpoint *b = loc->owner;
cc59ec59 7830
1e4d1764 7831 if (solib->pspace == loc->pspace
e2dd7057 7832 && !loc->shlib_disabled
1e4d1764
YQ
7833 && (((b->type == bp_breakpoint
7834 || b->type == bp_jit_event
7835 || b->type == bp_hardware_breakpoint)
7836 && (loc->loc_type == bp_loc_hardware_breakpoint
7837 || loc->loc_type == bp_loc_software_breakpoint))
7838 || is_tracepoint (b))
e2dd7057 7839 && solib_contains_address_p (solib, loc->address))
84acb35a 7840 {
e2dd7057
PP
7841 loc->shlib_disabled = 1;
7842 /* At this point, we cannot rely on remove_breakpoint
7843 succeeding so we must mark the breakpoint as not inserted
7844 to prevent future errors occurring in remove_breakpoints. */
7845 loc->inserted = 0;
8d3788bd
VP
7846
7847 /* This may cause duplicate notifications for the same breakpoint. */
7848 observer_notify_breakpoint_modified (b);
7849
e2dd7057
PP
7850 if (!disabled_shlib_breaks)
7851 {
7852 target_terminal_ours_for_output ();
3e43a32a
MS
7853 warning (_("Temporarily disabling breakpoints "
7854 "for unloaded shared library \"%s\""),
e2dd7057 7855 solib->so_name);
84acb35a 7856 }
e2dd7057 7857 disabled_shlib_breaks = 1;
84acb35a
JJ
7858 }
7859 }
84acb35a
JJ
7860}
7861
63644780
NB
7862/* Disable any breakpoints and tracepoints in OBJFILE upon
7863 notification of free_objfile. Only apply to enabled breakpoints,
7864 disabled ones can just stay disabled. */
7865
7866static void
7867disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7868{
7869 struct breakpoint *b;
7870
7871 if (objfile == NULL)
7872 return;
7873
d03de421
PA
7874 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7875 managed by the user with add-symbol-file/remove-symbol-file.
7876 Similarly to how breakpoints in shared libraries are handled in
7877 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7878 shlib_disabled so they end up uninserted on the next global
7879 location list update. Shared libraries not loaded by the user
7880 aren't handled here -- they're already handled in
7881 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7882 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7883 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7884 main objfile). */
7885 if ((objfile->flags & OBJF_SHARED) == 0
7886 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7887 return;
7888
7889 ALL_BREAKPOINTS (b)
7890 {
7891 struct bp_location *loc;
7892 int bp_modified = 0;
7893
7894 if (!is_breakpoint (b) && !is_tracepoint (b))
7895 continue;
7896
7897 for (loc = b->loc; loc != NULL; loc = loc->next)
7898 {
7899 CORE_ADDR loc_addr = loc->address;
7900
7901 if (loc->loc_type != bp_loc_hardware_breakpoint
7902 && loc->loc_type != bp_loc_software_breakpoint)
7903 continue;
7904
7905 if (loc->shlib_disabled != 0)
7906 continue;
7907
7908 if (objfile->pspace != loc->pspace)
7909 continue;
7910
7911 if (loc->loc_type != bp_loc_hardware_breakpoint
7912 && loc->loc_type != bp_loc_software_breakpoint)
7913 continue;
7914
7915 if (is_addr_in_objfile (loc_addr, objfile))
7916 {
7917 loc->shlib_disabled = 1;
08351840
PA
7918 /* At this point, we don't know whether the object was
7919 unmapped from the inferior or not, so leave the
7920 inserted flag alone. We'll handle failure to
7921 uninsert quietly, in case the object was indeed
7922 unmapped. */
63644780
NB
7923
7924 mark_breakpoint_location_modified (loc);
7925
7926 bp_modified = 1;
7927 }
7928 }
7929
7930 if (bp_modified)
7931 observer_notify_breakpoint_modified (b);
7932 }
7933}
7934
ce78b96d
JB
7935/* FORK & VFORK catchpoints. */
7936
e29a4733
PA
7937/* An instance of this type is used to represent a fork or vfork
7938 catchpoint. It includes a "struct breakpoint" as a kind of base
7939 class; users downcast to "struct breakpoint *" when needed. A
7940 breakpoint is really of this type iff its ops pointer points to
7941 CATCH_FORK_BREAKPOINT_OPS. */
7942
7943struct fork_catchpoint
7944{
7945 /* The base class. */
7946 struct breakpoint base;
7947
7948 /* Process id of a child process whose forking triggered this
7949 catchpoint. This field is only valid immediately after this
7950 catchpoint has triggered. */
7951 ptid_t forked_inferior_pid;
7952};
7953
4a64f543
MS
7954/* Implement the "insert" breakpoint_ops method for fork
7955 catchpoints. */
ce78b96d 7956
77b06cd7
TJB
7957static int
7958insert_catch_fork (struct bp_location *bl)
ce78b96d 7959{
dfd4cc63 7960 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7961}
7962
4a64f543
MS
7963/* Implement the "remove" breakpoint_ops method for fork
7964 catchpoints. */
ce78b96d
JB
7965
7966static int
77b06cd7 7967remove_catch_fork (struct bp_location *bl)
ce78b96d 7968{
dfd4cc63 7969 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7970}
7971
7972/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7973 catchpoints. */
7974
7975static int
f1310107 7976breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7977 struct address_space *aspace, CORE_ADDR bp_addr,
7978 const struct target_waitstatus *ws)
ce78b96d 7979{
e29a4733
PA
7980 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7981
f90263c1
TT
7982 if (ws->kind != TARGET_WAITKIND_FORKED)
7983 return 0;
7984
7985 c->forked_inferior_pid = ws->value.related_pid;
7986 return 1;
ce78b96d
JB
7987}
7988
4a64f543
MS
7989/* Implement the "print_it" breakpoint_ops method for fork
7990 catchpoints. */
ce78b96d
JB
7991
7992static enum print_stop_action
348d480f 7993print_it_catch_fork (bpstat bs)
ce78b96d 7994{
36dfb11c 7995 struct ui_out *uiout = current_uiout;
348d480f
PA
7996 struct breakpoint *b = bs->breakpoint_at;
7997 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7998
ce78b96d 7999 annotate_catchpoint (b->number);
36dfb11c
TT
8000 if (b->disposition == disp_del)
8001 ui_out_text (uiout, "\nTemporary catchpoint ");
8002 else
8003 ui_out_text (uiout, "\nCatchpoint ");
8004 if (ui_out_is_mi_like_p (uiout))
8005 {
8006 ui_out_field_string (uiout, "reason",
8007 async_reason_lookup (EXEC_ASYNC_FORK));
8008 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8009 }
8010 ui_out_field_int (uiout, "bkptno", b->number);
8011 ui_out_text (uiout, " (forked process ");
8012 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8013 ui_out_text (uiout, "), ");
ce78b96d
JB
8014 return PRINT_SRC_AND_LOC;
8015}
8016
4a64f543
MS
8017/* Implement the "print_one" breakpoint_ops method for fork
8018 catchpoints. */
ce78b96d
JB
8019
8020static void
a6d9a66e 8021print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8022{
e29a4733 8023 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8024 struct value_print_options opts;
79a45e25 8025 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8026
8027 get_user_print_options (&opts);
8028
4a64f543
MS
8029 /* Field 4, the address, is omitted (which makes the columns not
8030 line up too nicely with the headers, but the effect is relatively
8031 readable). */
79a45b7d 8032 if (opts.addressprint)
ce78b96d
JB
8033 ui_out_field_skip (uiout, "addr");
8034 annotate_field (5);
8035 ui_out_text (uiout, "fork");
e29a4733 8036 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8037 {
8038 ui_out_text (uiout, ", process ");
8039 ui_out_field_int (uiout, "what",
e29a4733 8040 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8041 ui_out_spaces (uiout, 1);
8042 }
8ac3646f
TT
8043
8044 if (ui_out_is_mi_like_p (uiout))
8045 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8046}
8047
8048/* Implement the "print_mention" breakpoint_ops method for fork
8049 catchpoints. */
8050
8051static void
8052print_mention_catch_fork (struct breakpoint *b)
8053{
8054 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8055}
8056
6149aea9
PA
8057/* Implement the "print_recreate" breakpoint_ops method for fork
8058 catchpoints. */
8059
8060static void
8061print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8062{
8063 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8064 print_recreate_thread (b, fp);
6149aea9
PA
8065}
8066
ce78b96d
JB
8067/* The breakpoint_ops structure to be used in fork catchpoints. */
8068
2060206e 8069static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8070
4a64f543
MS
8071/* Implement the "insert" breakpoint_ops method for vfork
8072 catchpoints. */
ce78b96d 8073
77b06cd7
TJB
8074static int
8075insert_catch_vfork (struct bp_location *bl)
ce78b96d 8076{
dfd4cc63 8077 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8078}
8079
4a64f543
MS
8080/* Implement the "remove" breakpoint_ops method for vfork
8081 catchpoints. */
ce78b96d
JB
8082
8083static int
77b06cd7 8084remove_catch_vfork (struct bp_location *bl)
ce78b96d 8085{
dfd4cc63 8086 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8087}
8088
8089/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8090 catchpoints. */
8091
8092static int
f1310107 8093breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8094 struct address_space *aspace, CORE_ADDR bp_addr,
8095 const struct target_waitstatus *ws)
ce78b96d 8096{
e29a4733
PA
8097 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8098
f90263c1
TT
8099 if (ws->kind != TARGET_WAITKIND_VFORKED)
8100 return 0;
8101
8102 c->forked_inferior_pid = ws->value.related_pid;
8103 return 1;
ce78b96d
JB
8104}
8105
4a64f543
MS
8106/* Implement the "print_it" breakpoint_ops method for vfork
8107 catchpoints. */
ce78b96d
JB
8108
8109static enum print_stop_action
348d480f 8110print_it_catch_vfork (bpstat bs)
ce78b96d 8111{
36dfb11c 8112 struct ui_out *uiout = current_uiout;
348d480f 8113 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8114 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8115
ce78b96d 8116 annotate_catchpoint (b->number);
36dfb11c
TT
8117 if (b->disposition == disp_del)
8118 ui_out_text (uiout, "\nTemporary catchpoint ");
8119 else
8120 ui_out_text (uiout, "\nCatchpoint ");
8121 if (ui_out_is_mi_like_p (uiout))
8122 {
8123 ui_out_field_string (uiout, "reason",
8124 async_reason_lookup (EXEC_ASYNC_VFORK));
8125 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8126 }
8127 ui_out_field_int (uiout, "bkptno", b->number);
8128 ui_out_text (uiout, " (vforked process ");
8129 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8130 ui_out_text (uiout, "), ");
ce78b96d
JB
8131 return PRINT_SRC_AND_LOC;
8132}
8133
4a64f543
MS
8134/* Implement the "print_one" breakpoint_ops method for vfork
8135 catchpoints. */
ce78b96d
JB
8136
8137static void
a6d9a66e 8138print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8139{
e29a4733 8140 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8141 struct value_print_options opts;
79a45e25 8142 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8143
8144 get_user_print_options (&opts);
4a64f543
MS
8145 /* Field 4, the address, is omitted (which makes the columns not
8146 line up too nicely with the headers, but the effect is relatively
8147 readable). */
79a45b7d 8148 if (opts.addressprint)
ce78b96d
JB
8149 ui_out_field_skip (uiout, "addr");
8150 annotate_field (5);
8151 ui_out_text (uiout, "vfork");
e29a4733 8152 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8153 {
8154 ui_out_text (uiout, ", process ");
8155 ui_out_field_int (uiout, "what",
e29a4733 8156 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8157 ui_out_spaces (uiout, 1);
8158 }
8ac3646f
TT
8159
8160 if (ui_out_is_mi_like_p (uiout))
8161 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8162}
8163
8164/* Implement the "print_mention" breakpoint_ops method for vfork
8165 catchpoints. */
8166
8167static void
8168print_mention_catch_vfork (struct breakpoint *b)
8169{
8170 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8171}
8172
6149aea9
PA
8173/* Implement the "print_recreate" breakpoint_ops method for vfork
8174 catchpoints. */
8175
8176static void
8177print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8178{
8179 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8180 print_recreate_thread (b, fp);
6149aea9
PA
8181}
8182
ce78b96d
JB
8183/* The breakpoint_ops structure to be used in vfork catchpoints. */
8184
2060206e 8185static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8186
edcc5120
TT
8187/* An instance of this type is used to represent an solib catchpoint.
8188 It includes a "struct breakpoint" as a kind of base class; users
8189 downcast to "struct breakpoint *" when needed. A breakpoint is
8190 really of this type iff its ops pointer points to
8191 CATCH_SOLIB_BREAKPOINT_OPS. */
8192
8193struct solib_catchpoint
8194{
8195 /* The base class. */
8196 struct breakpoint base;
8197
8198 /* True for "catch load", false for "catch unload". */
8199 unsigned char is_load;
8200
8201 /* Regular expression to match, if any. COMPILED is only valid when
8202 REGEX is non-NULL. */
8203 char *regex;
8204 regex_t compiled;
8205};
8206
8207static void
8208dtor_catch_solib (struct breakpoint *b)
8209{
8210 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8211
8212 if (self->regex)
8213 regfree (&self->compiled);
8214 xfree (self->regex);
8215
8216 base_breakpoint_ops.dtor (b);
8217}
8218
8219static int
8220insert_catch_solib (struct bp_location *ignore)
8221{
8222 return 0;
8223}
8224
8225static int
8226remove_catch_solib (struct bp_location *ignore)
8227{
8228 return 0;
8229}
8230
8231static int
8232breakpoint_hit_catch_solib (const struct bp_location *bl,
8233 struct address_space *aspace,
8234 CORE_ADDR bp_addr,
8235 const struct target_waitstatus *ws)
8236{
8237 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8238 struct breakpoint *other;
8239
8240 if (ws->kind == TARGET_WAITKIND_LOADED)
8241 return 1;
8242
8243 ALL_BREAKPOINTS (other)
8244 {
8245 struct bp_location *other_bl;
8246
8247 if (other == bl->owner)
8248 continue;
8249
8250 if (other->type != bp_shlib_event)
8251 continue;
8252
8253 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8254 continue;
8255
8256 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8257 {
8258 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8259 return 1;
8260 }
8261 }
8262
8263 return 0;
8264}
8265
8266static void
8267check_status_catch_solib (struct bpstats *bs)
8268{
8269 struct solib_catchpoint *self
8270 = (struct solib_catchpoint *) bs->breakpoint_at;
8271 int ix;
8272
8273 if (self->is_load)
8274 {
8275 struct so_list *iter;
8276
8277 for (ix = 0;
8278 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8279 ix, iter);
8280 ++ix)
8281 {
8282 if (!self->regex
8283 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8284 return;
8285 }
8286 }
8287 else
8288 {
8289 char *iter;
8290
8291 for (ix = 0;
8292 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8293 ix, iter);
8294 ++ix)
8295 {
8296 if (!self->regex
8297 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8298 return;
8299 }
8300 }
8301
8302 bs->stop = 0;
8303 bs->print_it = print_it_noop;
8304}
8305
8306static enum print_stop_action
8307print_it_catch_solib (bpstat bs)
8308{
8309 struct breakpoint *b = bs->breakpoint_at;
8310 struct ui_out *uiout = current_uiout;
8311
8312 annotate_catchpoint (b->number);
8313 if (b->disposition == disp_del)
8314 ui_out_text (uiout, "\nTemporary catchpoint ");
8315 else
8316 ui_out_text (uiout, "\nCatchpoint ");
8317 ui_out_field_int (uiout, "bkptno", b->number);
8318 ui_out_text (uiout, "\n");
8319 if (ui_out_is_mi_like_p (uiout))
8320 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8321 print_solib_event (1);
8322 return PRINT_SRC_AND_LOC;
8323}
8324
8325static void
8326print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8327{
8328 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8329 struct value_print_options opts;
8330 struct ui_out *uiout = current_uiout;
8331 char *msg;
8332
8333 get_user_print_options (&opts);
8334 /* Field 4, the address, is omitted (which makes the columns not
8335 line up too nicely with the headers, but the effect is relatively
8336 readable). */
8337 if (opts.addressprint)
8338 {
8339 annotate_field (4);
8340 ui_out_field_skip (uiout, "addr");
8341 }
8342
8343 annotate_field (5);
8344 if (self->is_load)
8345 {
8346 if (self->regex)
8347 msg = xstrprintf (_("load of library matching %s"), self->regex);
8348 else
8349 msg = xstrdup (_("load of library"));
8350 }
8351 else
8352 {
8353 if (self->regex)
8354 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8355 else
8356 msg = xstrdup (_("unload of library"));
8357 }
8358 ui_out_field_string (uiout, "what", msg);
8359 xfree (msg);
8ac3646f
TT
8360
8361 if (ui_out_is_mi_like_p (uiout))
8362 ui_out_field_string (uiout, "catch-type",
8363 self->is_load ? "load" : "unload");
edcc5120
TT
8364}
8365
8366static void
8367print_mention_catch_solib (struct breakpoint *b)
8368{
8369 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8370
8371 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8372 self->is_load ? "load" : "unload");
8373}
8374
8375static void
8376print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8377{
8378 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8379
8380 fprintf_unfiltered (fp, "%s %s",
8381 b->disposition == disp_del ? "tcatch" : "catch",
8382 self->is_load ? "load" : "unload");
8383 if (self->regex)
8384 fprintf_unfiltered (fp, " %s", self->regex);
8385 fprintf_unfiltered (fp, "\n");
8386}
8387
8388static struct breakpoint_ops catch_solib_breakpoint_ops;
8389
91985142
MG
8390/* Shared helper function (MI and CLI) for creating and installing
8391 a shared object event catchpoint. If IS_LOAD is non-zero then
8392 the events to be caught are load events, otherwise they are
8393 unload events. If IS_TEMP is non-zero the catchpoint is a
8394 temporary one. If ENABLED is non-zero the catchpoint is
8395 created in an enabled state. */
edcc5120 8396
91985142
MG
8397void
8398add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8399{
8400 struct solib_catchpoint *c;
8401 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8402 struct cleanup *cleanup;
8403
edcc5120
TT
8404 if (!arg)
8405 arg = "";
8406 arg = skip_spaces (arg);
8407
8408 c = XCNEW (struct solib_catchpoint);
8409 cleanup = make_cleanup (xfree, c);
8410
8411 if (*arg != '\0')
8412 {
8413 int errcode;
8414
8415 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8416 if (errcode != 0)
8417 {
8418 char *err = get_regcomp_error (errcode, &c->compiled);
8419
8420 make_cleanup (xfree, err);
8421 error (_("Invalid regexp (%s): %s"), err, arg);
8422 }
8423 c->regex = xstrdup (arg);
8424 }
8425
8426 c->is_load = is_load;
91985142 8427 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8428 &catch_solib_breakpoint_ops);
8429
91985142
MG
8430 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8431
edcc5120
TT
8432 discard_cleanups (cleanup);
8433 install_breakpoint (0, &c->base, 1);
8434}
8435
91985142
MG
8436/* A helper function that does all the work for "catch load" and
8437 "catch unload". */
8438
8439static void
8440catch_load_or_unload (char *arg, int from_tty, int is_load,
8441 struct cmd_list_element *command)
8442{
8443 int tempflag;
8444 const int enabled = 1;
8445
8446 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8447
8448 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8449}
8450
edcc5120
TT
8451static void
8452catch_load_command_1 (char *arg, int from_tty,
8453 struct cmd_list_element *command)
8454{
8455 catch_load_or_unload (arg, from_tty, 1, command);
8456}
8457
8458static void
8459catch_unload_command_1 (char *arg, int from_tty,
8460 struct cmd_list_element *command)
8461{
8462 catch_load_or_unload (arg, from_tty, 0, command);
8463}
8464
be5c67c1
PA
8465/* An instance of this type is used to represent a syscall catchpoint.
8466 It includes a "struct breakpoint" as a kind of base class; users
8467 downcast to "struct breakpoint *" when needed. A breakpoint is
8468 really of this type iff its ops pointer points to
8469 CATCH_SYSCALL_BREAKPOINT_OPS. */
8470
8471struct syscall_catchpoint
8472{
8473 /* The base class. */
8474 struct breakpoint base;
8475
8476 /* Syscall numbers used for the 'catch syscall' feature. If no
8477 syscall has been specified for filtering, its value is NULL.
8478 Otherwise, it holds a list of all syscalls to be caught. The
8479 list elements are allocated with xmalloc. */
8480 VEC(int) *syscalls_to_be_caught;
8481};
8482
8483/* Implement the "dtor" breakpoint_ops method for syscall
8484 catchpoints. */
8485
8486static void
8487dtor_catch_syscall (struct breakpoint *b)
8488{
8489 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8490
8491 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8492
2060206e 8493 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8494}
8495
fa3064dd
YQ
8496static const struct inferior_data *catch_syscall_inferior_data = NULL;
8497
8498struct catch_syscall_inferior_data
8499{
8500 /* We keep a count of the number of times the user has requested a
8501 particular syscall to be tracked, and pass this information to the
8502 target. This lets capable targets implement filtering directly. */
8503
8504 /* Number of times that "any" syscall is requested. */
8505 int any_syscall_count;
8506
8507 /* Count of each system call. */
8508 VEC(int) *syscalls_counts;
8509
8510 /* This counts all syscall catch requests, so we can readily determine
8511 if any catching is necessary. */
8512 int total_syscalls_count;
8513};
8514
8515static struct catch_syscall_inferior_data*
8516get_catch_syscall_inferior_data (struct inferior *inf)
8517{
8518 struct catch_syscall_inferior_data *inf_data;
8519
8520 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8521 if (inf_data == NULL)
8522 {
41bf6aca 8523 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8524 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8525 }
8526
8527 return inf_data;
8528}
8529
8530static void
8531catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8532{
8533 xfree (arg);
8534}
8535
8536
a96d9b2e
SDJ
8537/* Implement the "insert" breakpoint_ops method for syscall
8538 catchpoints. */
8539
77b06cd7
TJB
8540static int
8541insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8542{
be5c67c1 8543 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8544 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8545 struct catch_syscall_inferior_data *inf_data
8546 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8547
fa3064dd 8548 ++inf_data->total_syscalls_count;
be5c67c1 8549 if (!c->syscalls_to_be_caught)
fa3064dd 8550 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8551 else
8552 {
8553 int i, iter;
cc59ec59 8554
a96d9b2e 8555 for (i = 0;
be5c67c1 8556 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8557 i++)
8558 {
8559 int elem;
cc59ec59 8560
fa3064dd 8561 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8562 {
fa3064dd 8563 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8564 uintptr_t vec_addr_offset
8565 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8566 uintptr_t vec_addr;
fa3064dd
YQ
8567 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8568 vec_addr = ((uintptr_t) VEC_address (int,
8569 inf_data->syscalls_counts)
8570 + vec_addr_offset);
a96d9b2e
SDJ
8571 memset ((void *) vec_addr, 0,
8572 (iter + 1 - old_size) * sizeof (int));
8573 }
fa3064dd
YQ
8574 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8575 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8576 }
8577 }
8578
dfd4cc63 8579 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8580 inf_data->total_syscalls_count != 0,
8581 inf_data->any_syscall_count,
8582 VEC_length (int,
8583 inf_data->syscalls_counts),
8584 VEC_address (int,
8585 inf_data->syscalls_counts));
a96d9b2e
SDJ
8586}
8587
8588/* Implement the "remove" breakpoint_ops method for syscall
8589 catchpoints. */
8590
8591static int
77b06cd7 8592remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8593{
be5c67c1 8594 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8595 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8596 struct catch_syscall_inferior_data *inf_data
8597 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8598
fa3064dd 8599 --inf_data->total_syscalls_count;
be5c67c1 8600 if (!c->syscalls_to_be_caught)
fa3064dd 8601 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8602 else
8603 {
8604 int i, iter;
cc59ec59 8605
a96d9b2e 8606 for (i = 0;
be5c67c1 8607 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8608 i++)
8609 {
8610 int elem;
fa3064dd 8611 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8612 /* Shouldn't happen. */
8613 continue;
fa3064dd
YQ
8614 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8615 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8616 }
8617 }
8618
dfd4cc63 8619 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8620 inf_data->total_syscalls_count != 0,
8621 inf_data->any_syscall_count,
8622 VEC_length (int,
8623 inf_data->syscalls_counts),
3e43a32a 8624 VEC_address (int,
fa3064dd 8625 inf_data->syscalls_counts));
a96d9b2e
SDJ
8626}
8627
8628/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8629 catchpoints. */
8630
8631static int
f1310107 8632breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8633 struct address_space *aspace, CORE_ADDR bp_addr,
8634 const struct target_waitstatus *ws)
a96d9b2e 8635{
4a64f543
MS
8636 /* We must check if we are catching specific syscalls in this
8637 breakpoint. If we are, then we must guarantee that the called
8638 syscall is the same syscall we are catching. */
a96d9b2e 8639 int syscall_number = 0;
be5c67c1
PA
8640 const struct syscall_catchpoint *c
8641 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8642
f90263c1
TT
8643 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8644 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8645 return 0;
8646
f90263c1
TT
8647 syscall_number = ws->value.syscall_number;
8648
a96d9b2e 8649 /* Now, checking if the syscall is the same. */
be5c67c1 8650 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8651 {
8652 int i, iter;
cc59ec59 8653
a96d9b2e 8654 for (i = 0;
be5c67c1 8655 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8656 i++)
8657 if (syscall_number == iter)
4924df79
GKB
8658 return 1;
8659
8660 return 0;
a96d9b2e
SDJ
8661 }
8662
8663 return 1;
8664}
8665
8666/* Implement the "print_it" breakpoint_ops method for syscall
8667 catchpoints. */
8668
8669static enum print_stop_action
348d480f 8670print_it_catch_syscall (bpstat bs)
a96d9b2e 8671{
36dfb11c 8672 struct ui_out *uiout = current_uiout;
348d480f 8673 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8674 /* These are needed because we want to know in which state a
8675 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8676 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8677 must print "called syscall" or "returned from syscall". */
8678 ptid_t ptid;
8679 struct target_waitstatus last;
8680 struct syscall s;
a96d9b2e
SDJ
8681
8682 get_last_target_status (&ptid, &last);
8683
8684 get_syscall_by_number (last.value.syscall_number, &s);
8685
8686 annotate_catchpoint (b->number);
8687
36dfb11c
TT
8688 if (b->disposition == disp_del)
8689 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8690 else
36dfb11c
TT
8691 ui_out_text (uiout, "\nCatchpoint ");
8692 if (ui_out_is_mi_like_p (uiout))
8693 {
8694 ui_out_field_string (uiout, "reason",
8695 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8696 ? EXEC_ASYNC_SYSCALL_ENTRY
8697 : EXEC_ASYNC_SYSCALL_RETURN));
8698 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8699 }
8700 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8701
8702 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8703 ui_out_text (uiout, " (call to syscall ");
8704 else
8705 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8706
36dfb11c
TT
8707 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8708 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8709 if (s.name != NULL)
8710 ui_out_field_string (uiout, "syscall-name", s.name);
8711
8712 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8713
8714 return PRINT_SRC_AND_LOC;
8715}
8716
8717/* Implement the "print_one" breakpoint_ops method for syscall
8718 catchpoints. */
8719
8720static void
8721print_one_catch_syscall (struct breakpoint *b,
f1310107 8722 struct bp_location **last_loc)
a96d9b2e 8723{
be5c67c1 8724 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8725 struct value_print_options opts;
79a45e25 8726 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8727
8728 get_user_print_options (&opts);
4a64f543
MS
8729 /* Field 4, the address, is omitted (which makes the columns not
8730 line up too nicely with the headers, but the effect is relatively
8731 readable). */
a96d9b2e
SDJ
8732 if (opts.addressprint)
8733 ui_out_field_skip (uiout, "addr");
8734 annotate_field (5);
8735
be5c67c1
PA
8736 if (c->syscalls_to_be_caught
8737 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8738 ui_out_text (uiout, "syscalls \"");
8739 else
8740 ui_out_text (uiout, "syscall \"");
8741
be5c67c1 8742 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8743 {
8744 int i, iter;
8745 char *text = xstrprintf ("%s", "");
cc59ec59 8746
a96d9b2e 8747 for (i = 0;
be5c67c1 8748 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8749 i++)
8750 {
8751 char *x = text;
8752 struct syscall s;
8753 get_syscall_by_number (iter, &s);
8754
8755 if (s.name != NULL)
8756 text = xstrprintf ("%s%s, ", text, s.name);
8757 else
8758 text = xstrprintf ("%s%d, ", text, iter);
8759
8760 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8761 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8762 on every call. */
8763 xfree (x);
8764 }
8765 /* Remove the last comma. */
8766 text[strlen (text) - 2] = '\0';
8767 ui_out_field_string (uiout, "what", text);
8768 }
8769 else
8770 ui_out_field_string (uiout, "what", "<any syscall>");
8771 ui_out_text (uiout, "\" ");
8ac3646f
TT
8772
8773 if (ui_out_is_mi_like_p (uiout))
8774 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8775}
8776
8777/* Implement the "print_mention" breakpoint_ops method for syscall
8778 catchpoints. */
8779
8780static void
8781print_mention_catch_syscall (struct breakpoint *b)
8782{
be5c67c1
PA
8783 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8784
8785 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8786 {
8787 int i, iter;
8788
be5c67c1 8789 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8790 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8791 else
8792 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8793
8794 for (i = 0;
be5c67c1 8795 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8796 i++)
8797 {
8798 struct syscall s;
8799 get_syscall_by_number (iter, &s);
8800
8801 if (s.name)
8802 printf_filtered (" '%s' [%d]", s.name, s.number);
8803 else
8804 printf_filtered (" %d", s.number);
8805 }
8806 printf_filtered (")");
8807 }
8808 else
8809 printf_filtered (_("Catchpoint %d (any syscall)"),
8810 b->number);
8811}
8812
6149aea9
PA
8813/* Implement the "print_recreate" breakpoint_ops method for syscall
8814 catchpoints. */
8815
8816static void
8817print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8818{
be5c67c1
PA
8819 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8820
6149aea9
PA
8821 fprintf_unfiltered (fp, "catch syscall");
8822
be5c67c1 8823 if (c->syscalls_to_be_caught)
6149aea9
PA
8824 {
8825 int i, iter;
8826
8827 for (i = 0;
be5c67c1 8828 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8829 i++)
8830 {
8831 struct syscall s;
8832
8833 get_syscall_by_number (iter, &s);
8834 if (s.name)
8835 fprintf_unfiltered (fp, " %s", s.name);
8836 else
8837 fprintf_unfiltered (fp, " %d", s.number);
8838 }
8839 }
d9b3f62e 8840 print_recreate_thread (b, fp);
6149aea9
PA
8841}
8842
a96d9b2e
SDJ
8843/* The breakpoint_ops structure to be used in syscall catchpoints. */
8844
2060206e 8845static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8846
8847/* Returns non-zero if 'b' is a syscall catchpoint. */
8848
8849static int
8850syscall_catchpoint_p (struct breakpoint *b)
8851{
8852 return (b->ops == &catch_syscall_breakpoint_ops);
8853}
8854
346774a9
PA
8855/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8856 is non-zero, then make the breakpoint temporary. If COND_STRING is
8857 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8858 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8859
ab04a2af 8860void
346774a9
PA
8861init_catchpoint (struct breakpoint *b,
8862 struct gdbarch *gdbarch, int tempflag,
8863 char *cond_string,
c0a91b2b 8864 const struct breakpoint_ops *ops)
c906108c 8865{
c5aa993b 8866 struct symtab_and_line sal;
346774a9 8867
fe39c653 8868 init_sal (&sal);
6c95b8df 8869 sal.pspace = current_program_space;
c5aa993b 8870
28010a5d 8871 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8872
1b36a34b 8873 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8874 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8875}
8876
28010a5d 8877void
3ea46bff 8878install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8879{
8880 add_to_breakpoint_chain (b);
3a5c3e22 8881 set_breakpoint_number (internal, b);
558a9d82
YQ
8882 if (is_tracepoint (b))
8883 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8884 if (!internal)
8885 mention (b);
c56053d2 8886 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8887
8888 if (update_gll)
44702360 8889 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8890}
8891
9b70b993 8892static void
a6d9a66e
UW
8893create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8894 int tempflag, char *cond_string,
c0a91b2b 8895 const struct breakpoint_ops *ops)
c906108c 8896{
e29a4733 8897 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8898
e29a4733
PA
8899 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8900
8901 c->forked_inferior_pid = null_ptid;
8902
3ea46bff 8903 install_breakpoint (0, &c->base, 1);
c906108c
SS
8904}
8905
fe798b75
JB
8906/* Exec catchpoints. */
8907
b4d90040
PA
8908/* An instance of this type is used to represent an exec catchpoint.
8909 It includes a "struct breakpoint" as a kind of base class; users
8910 downcast to "struct breakpoint *" when needed. A breakpoint is
8911 really of this type iff its ops pointer points to
8912 CATCH_EXEC_BREAKPOINT_OPS. */
8913
8914struct exec_catchpoint
8915{
8916 /* The base class. */
8917 struct breakpoint base;
8918
8919 /* Filename of a program whose exec triggered this catchpoint.
8920 This field is only valid immediately after this catchpoint has
8921 triggered. */
8922 char *exec_pathname;
8923};
8924
8925/* Implement the "dtor" breakpoint_ops method for exec
8926 catchpoints. */
8927
8928static void
8929dtor_catch_exec (struct breakpoint *b)
8930{
8931 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8932
8933 xfree (c->exec_pathname);
348d480f 8934
2060206e 8935 base_breakpoint_ops.dtor (b);
b4d90040
PA
8936}
8937
77b06cd7
TJB
8938static int
8939insert_catch_exec (struct bp_location *bl)
c906108c 8940{
dfd4cc63 8941 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8942}
c906108c 8943
fe798b75 8944static int
77b06cd7 8945remove_catch_exec (struct bp_location *bl)
fe798b75 8946{
dfd4cc63 8947 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8948}
c906108c 8949
fe798b75 8950static int
f1310107 8951breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8952 struct address_space *aspace, CORE_ADDR bp_addr,
8953 const struct target_waitstatus *ws)
fe798b75 8954{
b4d90040
PA
8955 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8956
f90263c1
TT
8957 if (ws->kind != TARGET_WAITKIND_EXECD)
8958 return 0;
8959
8960 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8961 return 1;
fe798b75 8962}
c906108c 8963
fe798b75 8964static enum print_stop_action
348d480f 8965print_it_catch_exec (bpstat bs)
fe798b75 8966{
36dfb11c 8967 struct ui_out *uiout = current_uiout;
348d480f 8968 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8969 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8970
fe798b75 8971 annotate_catchpoint (b->number);
36dfb11c
TT
8972 if (b->disposition == disp_del)
8973 ui_out_text (uiout, "\nTemporary catchpoint ");
8974 else
8975 ui_out_text (uiout, "\nCatchpoint ");
8976 if (ui_out_is_mi_like_p (uiout))
8977 {
8978 ui_out_field_string (uiout, "reason",
8979 async_reason_lookup (EXEC_ASYNC_EXEC));
8980 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8981 }
8982 ui_out_field_int (uiout, "bkptno", b->number);
8983 ui_out_text (uiout, " (exec'd ");
8984 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8985 ui_out_text (uiout, "), ");
8986
fe798b75 8987 return PRINT_SRC_AND_LOC;
c906108c
SS
8988}
8989
fe798b75 8990static void
a6d9a66e 8991print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8992{
b4d90040 8993 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8994 struct value_print_options opts;
79a45e25 8995 struct ui_out *uiout = current_uiout;
fe798b75
JB
8996
8997 get_user_print_options (&opts);
8998
8999 /* Field 4, the address, is omitted (which makes the columns
9000 not line up too nicely with the headers, but the effect
9001 is relatively readable). */
9002 if (opts.addressprint)
9003 ui_out_field_skip (uiout, "addr");
9004 annotate_field (5);
9005 ui_out_text (uiout, "exec");
b4d90040 9006 if (c->exec_pathname != NULL)
fe798b75
JB
9007 {
9008 ui_out_text (uiout, ", program \"");
b4d90040 9009 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9010 ui_out_text (uiout, "\" ");
9011 }
8ac3646f
TT
9012
9013 if (ui_out_is_mi_like_p (uiout))
9014 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9015}
9016
9017static void
9018print_mention_catch_exec (struct breakpoint *b)
9019{
9020 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9021}
9022
6149aea9
PA
9023/* Implement the "print_recreate" breakpoint_ops method for exec
9024 catchpoints. */
9025
9026static void
9027print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9028{
9029 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9030 print_recreate_thread (b, fp);
6149aea9
PA
9031}
9032
2060206e 9033static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9034
a96d9b2e
SDJ
9035static void
9036create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9037 const struct breakpoint_ops *ops)
a96d9b2e 9038{
be5c67c1 9039 struct syscall_catchpoint *c;
a96d9b2e 9040 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9041
be5c67c1
PA
9042 c = XNEW (struct syscall_catchpoint);
9043 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9044 c->syscalls_to_be_caught = filter;
a96d9b2e 9045
3ea46bff 9046 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9047}
9048
c906108c 9049static int
fba45db2 9050hw_breakpoint_used_count (void)
c906108c 9051{
c906108c 9052 int i = 0;
f1310107
TJB
9053 struct breakpoint *b;
9054 struct bp_location *bl;
c906108c
SS
9055
9056 ALL_BREAKPOINTS (b)
c5aa993b 9057 {
d6b74ac4 9058 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9059 for (bl = b->loc; bl; bl = bl->next)
9060 {
9061 /* Special types of hardware breakpoints may use more than
9062 one register. */
348d480f 9063 i += b->ops->resources_needed (bl);
f1310107 9064 }
c5aa993b 9065 }
c906108c
SS
9066
9067 return i;
9068}
9069
a1398e0c
PA
9070/* Returns the resources B would use if it were a hardware
9071 watchpoint. */
9072
c906108c 9073static int
a1398e0c 9074hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9075{
c906108c 9076 int i = 0;
e09342b5 9077 struct bp_location *bl;
c906108c 9078
a1398e0c
PA
9079 if (!breakpoint_enabled (b))
9080 return 0;
9081
9082 for (bl = b->loc; bl; bl = bl->next)
9083 {
9084 /* Special types of hardware watchpoints may use more than
9085 one register. */
9086 i += b->ops->resources_needed (bl);
9087 }
9088
9089 return i;
9090}
9091
9092/* Returns the sum the used resources of all hardware watchpoints of
9093 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9094 the sum of the used resources of all hardware watchpoints of other
9095 types _not_ TYPE. */
9096
9097static int
9098hw_watchpoint_used_count_others (struct breakpoint *except,
9099 enum bptype type, int *other_type_used)
9100{
9101 int i = 0;
9102 struct breakpoint *b;
9103
c906108c
SS
9104 *other_type_used = 0;
9105 ALL_BREAKPOINTS (b)
e09342b5 9106 {
a1398e0c
PA
9107 if (b == except)
9108 continue;
e09342b5
TJB
9109 if (!breakpoint_enabled (b))
9110 continue;
9111
a1398e0c
PA
9112 if (b->type == type)
9113 i += hw_watchpoint_use_count (b);
9114 else if (is_hardware_watchpoint (b))
9115 *other_type_used = 1;
e09342b5
TJB
9116 }
9117
c906108c
SS
9118 return i;
9119}
9120
c906108c 9121void
fba45db2 9122disable_watchpoints_before_interactive_call_start (void)
c906108c 9123{
c5aa993b 9124 struct breakpoint *b;
c906108c
SS
9125
9126 ALL_BREAKPOINTS (b)
c5aa993b 9127 {
cc60f2e3 9128 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9129 {
b5de0fa7 9130 b->enable_state = bp_call_disabled;
44702360 9131 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9132 }
9133 }
c906108c
SS
9134}
9135
9136void
fba45db2 9137enable_watchpoints_after_interactive_call_stop (void)
c906108c 9138{
c5aa993b 9139 struct breakpoint *b;
c906108c
SS
9140
9141 ALL_BREAKPOINTS (b)
c5aa993b 9142 {
cc60f2e3 9143 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9144 {
b5de0fa7 9145 b->enable_state = bp_enabled;
44702360 9146 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9147 }
9148 }
c906108c
SS
9149}
9150
8bea4e01
UW
9151void
9152disable_breakpoints_before_startup (void)
9153{
6c95b8df 9154 current_program_space->executing_startup = 1;
44702360 9155 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9156}
9157
9158void
9159enable_breakpoints_after_startup (void)
9160{
6c95b8df 9161 current_program_space->executing_startup = 0;
f8eba3c6 9162 breakpoint_re_set ();
8bea4e01
UW
9163}
9164
7c16b83e
PA
9165/* Create a new single-step breakpoint for thread THREAD, with no
9166 locations. */
c906108c 9167
7c16b83e
PA
9168static struct breakpoint *
9169new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9170{
9171 struct breakpoint *b = XNEW (struct breakpoint);
9172
9173 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9174 &momentary_breakpoint_ops);
9175
9176 b->disposition = disp_donttouch;
9177 b->frame_id = null_frame_id;
9178
9179 b->thread = thread;
9180 gdb_assert (b->thread != 0);
9181
9182 add_to_breakpoint_chain (b);
9183
9184 return b;
9185}
9186
9187/* Set a momentary breakpoint of type TYPE at address specified by
9188 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9189 frame. */
c906108c
SS
9190
9191struct breakpoint *
a6d9a66e
UW
9192set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9193 struct frame_id frame_id, enum bptype type)
c906108c 9194{
52f0bd74 9195 struct breakpoint *b;
edb3359d 9196
193facb3
JK
9197 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9198 tail-called one. */
9199 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9200
06edf0c0 9201 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9202 b->enable_state = bp_enabled;
9203 b->disposition = disp_donttouch;
818dd999 9204 b->frame_id = frame_id;
c906108c 9205
4a64f543
MS
9206 /* If we're debugging a multi-threaded program, then we want
9207 momentary breakpoints to be active in only a single thread of
9208 control. */
39f77062
KB
9209 if (in_thread_list (inferior_ptid))
9210 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9211
44702360 9212 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9213
c906108c
SS
9214 return b;
9215}
611c83ae 9216
06edf0c0 9217/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9218 The new breakpoint will have type TYPE, use OPS as its
9219 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9220
06edf0c0
PA
9221static struct breakpoint *
9222momentary_breakpoint_from_master (struct breakpoint *orig,
9223 enum bptype type,
a1aa2221
LM
9224 const struct breakpoint_ops *ops,
9225 int loc_enabled)
e58b0e63
PA
9226{
9227 struct breakpoint *copy;
9228
06edf0c0 9229 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9230 copy->loc = allocate_bp_location (copy);
0e30163f 9231 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9232
a6d9a66e 9233 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9234 copy->loc->requested_address = orig->loc->requested_address;
9235 copy->loc->address = orig->loc->address;
9236 copy->loc->section = orig->loc->section;
6c95b8df 9237 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9238 copy->loc->probe = orig->loc->probe;
f8eba3c6 9239 copy->loc->line_number = orig->loc->line_number;
2f202fde 9240 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9241 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9242 copy->frame_id = orig->frame_id;
9243 copy->thread = orig->thread;
6c95b8df 9244 copy->pspace = orig->pspace;
e58b0e63
PA
9245
9246 copy->enable_state = bp_enabled;
9247 copy->disposition = disp_donttouch;
9248 copy->number = internal_breakpoint_number--;
9249
44702360 9250 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9251 return copy;
9252}
9253
06edf0c0
PA
9254/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9255 ORIG is NULL. */
9256
9257struct breakpoint *
9258clone_momentary_breakpoint (struct breakpoint *orig)
9259{
9260 /* If there's nothing to clone, then return nothing. */
9261 if (orig == NULL)
9262 return NULL;
9263
a1aa2221 9264 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9265}
9266
611c83ae 9267struct breakpoint *
a6d9a66e
UW
9268set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9269 enum bptype type)
611c83ae
PA
9270{
9271 struct symtab_and_line sal;
9272
9273 sal = find_pc_line (pc, 0);
9274 sal.pc = pc;
9275 sal.section = find_pc_overlay (pc);
9276 sal.explicit_pc = 1;
9277
a6d9a66e 9278 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9279}
c906108c 9280\f
c5aa993b 9281
c906108c
SS
9282/* Tell the user we have just set a breakpoint B. */
9283
9284static void
fba45db2 9285mention (struct breakpoint *b)
c906108c 9286{
348d480f 9287 b->ops->print_mention (b);
79a45e25 9288 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9289 return;
c906108c
SS
9290 printf_filtered ("\n");
9291}
c906108c 9292\f
c5aa993b 9293
0d381245 9294static struct bp_location *
39d61571 9295add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9296 const struct symtab_and_line *sal)
9297{
9298 struct bp_location *loc, **tmp;
3742cc8b
YQ
9299 CORE_ADDR adjusted_address;
9300 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9301
9302 if (loc_gdbarch == NULL)
9303 loc_gdbarch = b->gdbarch;
9304
9305 /* Adjust the breakpoint's address prior to allocating a location.
9306 Once we call allocate_bp_location(), that mostly uninitialized
9307 location will be placed on the location chain. Adjustment of the
9308 breakpoint may cause target_read_memory() to be called and we do
9309 not want its scan of the location chain to find a breakpoint and
9310 location that's only been partially initialized. */
9311 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9312 sal->pc, b->type);
0d381245 9313
d30113d4 9314 /* Sort the locations by their ADDRESS. */
39d61571 9315 loc = allocate_bp_location (b);
d30113d4
JK
9316 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9317 tmp = &((*tmp)->next))
0d381245 9318 ;
d30113d4 9319 loc->next = *tmp;
0d381245 9320 *tmp = loc;
3742cc8b 9321
0d381245 9322 loc->requested_address = sal->pc;
3742cc8b 9323 loc->address = adjusted_address;
6c95b8df 9324 loc->pspace = sal->pspace;
729662a5
TT
9325 loc->probe.probe = sal->probe;
9326 loc->probe.objfile = sal->objfile;
6c95b8df 9327 gdb_assert (loc->pspace != NULL);
0d381245 9328 loc->section = sal->section;
3742cc8b 9329 loc->gdbarch = loc_gdbarch;
f8eba3c6 9330 loc->line_number = sal->line;
2f202fde 9331 loc->symtab = sal->symtab;
f8eba3c6 9332
0e30163f
JK
9333 set_breakpoint_location_function (loc,
9334 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9335 return loc;
9336}
514f746b
AR
9337\f
9338
9339/* Return 1 if LOC is pointing to a permanent breakpoint,
9340 return 0 otherwise. */
9341
9342static int
9343bp_loc_is_permanent (struct bp_location *loc)
9344{
9345 int len;
9346 CORE_ADDR addr;
1afeeb75 9347 const gdb_byte *bpoint;
514f746b 9348 gdb_byte *target_mem;
939c61fa
JK
9349 struct cleanup *cleanup;
9350 int retval = 0;
514f746b
AR
9351
9352 gdb_assert (loc != NULL);
9353
9354 addr = loc->address;
1afeeb75 9355 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9356
939c61fa 9357 /* Software breakpoints unsupported? */
1afeeb75 9358 if (bpoint == NULL)
939c61fa
JK
9359 return 0;
9360
514f746b
AR
9361 target_mem = alloca (len);
9362
939c61fa
JK
9363 /* Enable the automatic memory restoration from breakpoints while
9364 we read the memory. Otherwise we could say about our temporary
9365 breakpoints they are permanent. */
6c95b8df
PA
9366 cleanup = save_current_space_and_thread ();
9367
9368 switch_to_program_space_and_thread (loc->pspace);
9369 make_show_memory_breakpoints_cleanup (0);
939c61fa 9370
514f746b 9371 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9372 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9373 retval = 1;
514f746b 9374
939c61fa
JK
9375 do_cleanups (cleanup);
9376
9377 return retval;
514f746b
AR
9378}
9379
e7e0cddf
SS
9380/* Build a command list for the dprintf corresponding to the current
9381 settings of the dprintf style options. */
9382
9383static void
9384update_dprintf_command_list (struct breakpoint *b)
9385{
9386 char *dprintf_args = b->extra_string;
9387 char *printf_line = NULL;
9388
9389 if (!dprintf_args)
9390 return;
9391
9392 dprintf_args = skip_spaces (dprintf_args);
9393
9394 /* Allow a comma, as it may have terminated a location, but don't
9395 insist on it. */
9396 if (*dprintf_args == ',')
9397 ++dprintf_args;
9398 dprintf_args = skip_spaces (dprintf_args);
9399
9400 if (*dprintf_args != '"')
9401 error (_("Bad format string, missing '\"'."));
9402
d3ce09f5 9403 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9404 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9405 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9406 {
9407 if (!dprintf_function)
9408 error (_("No function supplied for dprintf call"));
9409
9410 if (dprintf_channel && strlen (dprintf_channel) > 0)
9411 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9412 dprintf_function,
9413 dprintf_channel,
9414 dprintf_args);
9415 else
9416 printf_line = xstrprintf ("call (void) %s (%s)",
9417 dprintf_function,
9418 dprintf_args);
9419 }
d3ce09f5
SS
9420 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9421 {
9422 if (target_can_run_breakpoint_commands ())
9423 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9424 else
9425 {
9426 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9427 printf_line = xstrprintf ("printf %s", dprintf_args);
9428 }
9429 }
e7e0cddf
SS
9430 else
9431 internal_error (__FILE__, __LINE__,
9432 _("Invalid dprintf style."));
9433
f28045c2 9434 gdb_assert (printf_line != NULL);
9d6e6e84 9435 /* Manufacture a printf sequence. */
f28045c2 9436 {
9d6e6e84
HZ
9437 struct command_line *printf_cmd_line
9438 = xmalloc (sizeof (struct command_line));
e7e0cddf 9439
f28045c2
YQ
9440 printf_cmd_line = xmalloc (sizeof (struct command_line));
9441 printf_cmd_line->control_type = simple_control;
9442 printf_cmd_line->body_count = 0;
9443 printf_cmd_line->body_list = NULL;
9d6e6e84 9444 printf_cmd_line->next = NULL;
f28045c2 9445 printf_cmd_line->line = printf_line;
e7e0cddf 9446
f28045c2
YQ
9447 breakpoint_set_commands (b, printf_cmd_line);
9448 }
e7e0cddf
SS
9449}
9450
9451/* Update all dprintf commands, making their command lists reflect
9452 current style settings. */
9453
9454static void
9455update_dprintf_commands (char *args, int from_tty,
9456 struct cmd_list_element *c)
9457{
9458 struct breakpoint *b;
9459
9460 ALL_BREAKPOINTS (b)
9461 {
9462 if (b->type == bp_dprintf)
9463 update_dprintf_command_list (b);
9464 }
9465}
c3f6f71d 9466
018d34a4
VP
9467/* Create a breakpoint with SAL as location. Use ADDR_STRING
9468 as textual description of the location, and COND_STRING
db107f19 9469 as condition expression. */
018d34a4
VP
9470
9471static void
d9b3f62e
PA
9472init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9473 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9474 char *filter, char *cond_string,
e7e0cddf 9475 char *extra_string,
d9b3f62e
PA
9476 enum bptype type, enum bpdisp disposition,
9477 int thread, int task, int ignore_count,
c0a91b2b 9478 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9479 int enabled, int internal, unsigned flags,
9480 int display_canonical)
018d34a4 9481{
0d381245 9482 int i;
018d34a4
VP
9483
9484 if (type == bp_hardware_breakpoint)
9485 {
fbbd034e
AS
9486 int target_resources_ok;
9487
9488 i = hw_breakpoint_used_count ();
9489 target_resources_ok =
9490 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9491 i + 1, 0);
9492 if (target_resources_ok == 0)
9493 error (_("No hardware breakpoint support in the target."));
9494 else if (target_resources_ok < 0)
9495 error (_("Hardware breakpoints used exceeds limit."));
9496 }
9497
6c95b8df
PA
9498 gdb_assert (sals.nelts > 0);
9499
0d381245
VP
9500 for (i = 0; i < sals.nelts; ++i)
9501 {
9502 struct symtab_and_line sal = sals.sals[i];
9503 struct bp_location *loc;
9504
9505 if (from_tty)
5af949e3
UW
9506 {
9507 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9508 if (!loc_gdbarch)
9509 loc_gdbarch = gdbarch;
9510
9511 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9512 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9513 }
0d381245
VP
9514
9515 if (i == 0)
9516 {
d9b3f62e 9517 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9518 b->thread = thread;
4a306c9a 9519 b->task = task;
855a6e68 9520
0d381245 9521 b->cond_string = cond_string;
e7e0cddf 9522 b->extra_string = extra_string;
0d381245 9523 b->ignore_count = ignore_count;
41447f92 9524 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9525 b->disposition = disposition;
6c95b8df 9526
44f238bb
PA
9527 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9528 b->loc->inserted = 1;
9529
0fb4aa4b
PA
9530 if (type == bp_static_tracepoint)
9531 {
d9b3f62e 9532 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9533 struct static_tracepoint_marker marker;
9534
983af33b 9535 if (strace_marker_p (b))
0fb4aa4b
PA
9536 {
9537 /* We already know the marker exists, otherwise, we
9538 wouldn't see a sal for it. */
9539 char *p = &addr_string[3];
9540 char *endp;
9541 char *marker_str;
0fb4aa4b 9542
e9cafbcc 9543 p = skip_spaces (p);
0fb4aa4b 9544
e9cafbcc 9545 endp = skip_to_space (p);
0fb4aa4b
PA
9546
9547 marker_str = savestring (p, endp - p);
d9b3f62e 9548 t->static_trace_marker_id = marker_str;
0fb4aa4b 9549
3e43a32a
MS
9550 printf_filtered (_("Probed static tracepoint "
9551 "marker \"%s\"\n"),
d9b3f62e 9552 t->static_trace_marker_id);
0fb4aa4b
PA
9553 }
9554 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9555 {
d9b3f62e 9556 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9557 release_static_tracepoint_marker (&marker);
9558
3e43a32a
MS
9559 printf_filtered (_("Probed static tracepoint "
9560 "marker \"%s\"\n"),
d9b3f62e 9561 t->static_trace_marker_id);
0fb4aa4b
PA
9562 }
9563 else
3e43a32a
MS
9564 warning (_("Couldn't determine the static "
9565 "tracepoint marker to probe"));
0fb4aa4b
PA
9566 }
9567
0d381245
VP
9568 loc = b->loc;
9569 }
9570 else
018d34a4 9571 {
39d61571 9572 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9573 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9574 loc->inserted = 1;
0d381245
VP
9575 }
9576
514f746b
AR
9577 if (bp_loc_is_permanent (loc))
9578 make_breakpoint_permanent (b);
9579
0d381245
VP
9580 if (b->cond_string)
9581 {
bbc13ae3
KS
9582 const char *arg = b->cond_string;
9583
1bb9788d
TT
9584 loc->cond = parse_exp_1 (&arg, loc->address,
9585 block_for_pc (loc->address), 0);
0d381245 9586 if (*arg)
588ae58c 9587 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9588 }
e7e0cddf
SS
9589
9590 /* Dynamic printf requires and uses additional arguments on the
9591 command line, otherwise it's an error. */
9592 if (type == bp_dprintf)
9593 {
9594 if (b->extra_string)
9595 update_dprintf_command_list (b);
9596 else
9597 error (_("Format string required"));
9598 }
9599 else if (b->extra_string)
588ae58c 9600 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9601 }
018d34a4 9602
56435ebe 9603 b->display_canonical = display_canonical;
018d34a4
VP
9604 if (addr_string)
9605 b->addr_string = addr_string;
9606 else
9607 /* addr_string has to be used or breakpoint_re_set will delete
9608 me. */
5af949e3
UW
9609 b->addr_string
9610 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9611 b->filter = filter;
d9b3f62e 9612}
018d34a4 9613
d9b3f62e
PA
9614static void
9615create_breakpoint_sal (struct gdbarch *gdbarch,
9616 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9617 char *filter, char *cond_string,
e7e0cddf 9618 char *extra_string,
d9b3f62e
PA
9619 enum bptype type, enum bpdisp disposition,
9620 int thread, int task, int ignore_count,
c0a91b2b 9621 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9622 int enabled, int internal, unsigned flags,
9623 int display_canonical)
d9b3f62e
PA
9624{
9625 struct breakpoint *b;
9626 struct cleanup *old_chain;
9627
9628 if (is_tracepoint_type (type))
9629 {
9630 struct tracepoint *t;
9631
9632 t = XCNEW (struct tracepoint);
9633 b = &t->base;
9634 }
9635 else
9636 b = XNEW (struct breakpoint);
9637
9638 old_chain = make_cleanup (xfree, b);
9639
9640 init_breakpoint_sal (b, gdbarch,
9641 sals, addr_string,
e7e0cddf 9642 filter, cond_string, extra_string,
d9b3f62e
PA
9643 type, disposition,
9644 thread, task, ignore_count,
9645 ops, from_tty,
44f238bb
PA
9646 enabled, internal, flags,
9647 display_canonical);
d9b3f62e
PA
9648 discard_cleanups (old_chain);
9649
3ea46bff 9650 install_breakpoint (internal, b, 0);
018d34a4
VP
9651}
9652
9653/* Add SALS.nelts breakpoints to the breakpoint table. For each
9654 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9655 value. COND_STRING, if not NULL, specified the condition to be
9656 used for all breakpoints. Essentially the only case where
9657 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9658 function. In that case, it's still not possible to specify
9659 separate conditions for different overloaded functions, so
9660 we take just a single condition string.
9661
c3f6f71d 9662 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9663 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9664 array contents). If the function fails (error() is called), the
9665 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9666 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9667
9668static void
8cdf0e15 9669create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9670 struct linespec_result *canonical,
e7e0cddf 9671 char *cond_string, char *extra_string,
8cdf0e15
VP
9672 enum bptype type, enum bpdisp disposition,
9673 int thread, int task, int ignore_count,
c0a91b2b 9674 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9675 int enabled, int internal, unsigned flags)
c906108c 9676{
018d34a4 9677 int i;
f8eba3c6 9678 struct linespec_sals *lsal;
cc59ec59 9679
f8eba3c6
TT
9680 if (canonical->pre_expanded)
9681 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9682
9683 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9684 {
f8eba3c6
TT
9685 /* Note that 'addr_string' can be NULL in the case of a plain
9686 'break', without arguments. */
9687 char *addr_string = (canonical->addr_string
9688 ? xstrdup (canonical->addr_string)
9689 : NULL);
9690 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9691 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9692
f8eba3c6
TT
9693 make_cleanup (xfree, filter_string);
9694 create_breakpoint_sal (gdbarch, lsal->sals,
9695 addr_string,
9696 filter_string,
e7e0cddf
SS
9697 cond_string, extra_string,
9698 type, disposition,
84f4c1fe 9699 thread, task, ignore_count, ops,
44f238bb 9700 from_tty, enabled, internal, flags,
56435ebe 9701 canonical->special_display);
f8eba3c6 9702 discard_cleanups (inner);
c3f6f71d 9703 }
c3f6f71d 9704}
c906108c 9705
9998af43 9706/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9707 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9708 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9709 address strings. ADDRESS points to the end of the SAL.
9710
9711 The array and the line spec strings are allocated on the heap, it is
9712 the caller's responsibility to free them. */
c906108c 9713
b9362cc7 9714static void
c3f6f71d 9715parse_breakpoint_sals (char **address,
58438ac1 9716 struct linespec_result *canonical)
c3f6f71d 9717{
c3f6f71d 9718 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9719 breakpoint. */
c3f6f71d
JM
9720 if ((*address) == NULL
9721 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9722 {
1bfeeb0f
JL
9723 /* The last displayed codepoint, if it's valid, is our default breakpoint
9724 address. */
9725 if (last_displayed_sal_is_valid ())
c906108c 9726 {
f8eba3c6 9727 struct linespec_sals lsal;
c3f6f71d 9728 struct symtab_and_line sal;
1c8cdcb1 9729 CORE_ADDR pc;
cc59ec59 9730
4a64f543 9731 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9732 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9733 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9734
9735 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9736 corresponding to the last call to print_frame_info.
9737 Be sure to reinitialize LINE with NOTCURRENT == 0
9738 as the breakpoint line number is inappropriate otherwise.
9739 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9740 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9741 pc = sal.pc;
9742 sal = find_pc_line (pc, 0);
00903456 9743
4a64f543 9744 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9745 where PC is the last displayed codepoint's address. So
9746 make sure to set sal.explicit_pc to prevent GDB from
9747 trying to expand the list of sals to include all other
9748 instances with the same symtab and line. */
1c8cdcb1 9749 sal.pc = pc;
00903456
JK
9750 sal.explicit_pc = 1;
9751
f8eba3c6
TT
9752 lsal.sals.sals[0] = sal;
9753 lsal.sals.nelts = 1;
9754 lsal.canonical = NULL;
9755
9756 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9757 }
9758 else
8a3fe4f8 9759 error (_("No default breakpoint address now."));
c906108c
SS
9760 }
9761 else
9762 {
cc80f267
JK
9763 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9764
c906108c 9765 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9766 current_source_symtab (which is decode_line_1's default).
9767 This should produce the results we want almost all of the
cc80f267
JK
9768 time while leaving default_breakpoint_* alone.
9769
9770 ObjC: However, don't match an Objective-C method name which
9771 may have a '+' or '-' succeeded by a '['. */
9772 if (last_displayed_sal_is_valid ()
9773 && (!cursal.symtab
9774 || ((strchr ("+-", (*address)[0]) != NULL)
9775 && ((*address)[1] != '['))))
f8eba3c6
TT
9776 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9777 get_last_displayed_symtab (),
9778 get_last_displayed_line (),
9779 canonical, NULL, NULL);
c906108c 9780 else
f8eba3c6 9781 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9782 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9783 }
c3f6f71d 9784}
c906108c 9785
c906108c 9786
c3f6f71d 9787/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9788 inserted as a breakpoint. If it can't throw an error. */
c906108c 9789
b9362cc7 9790static void
23e7acfb 9791breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9792{
9793 int i;
cc59ec59 9794
c3f6f71d 9795 for (i = 0; i < sals->nelts; i++)
ee53e872 9796 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9797}
9798
7a697b8d
SS
9799/* Fast tracepoints may have restrictions on valid locations. For
9800 instance, a fast tracepoint using a jump instead of a trap will
9801 likely have to overwrite more bytes than a trap would, and so can
9802 only be placed where the instruction is longer than the jump, or a
9803 multi-instruction sequence does not have a jump into the middle of
9804 it, etc. */
9805
9806static void
9807check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9808 struct symtabs_and_lines *sals)
9809{
9810 int i, rslt;
9811 struct symtab_and_line *sal;
9812 char *msg;
9813 struct cleanup *old_chain;
9814
9815 for (i = 0; i < sals->nelts; i++)
9816 {
f8eba3c6
TT
9817 struct gdbarch *sarch;
9818
7a697b8d
SS
9819 sal = &sals->sals[i];
9820
f8eba3c6
TT
9821 sarch = get_sal_arch (*sal);
9822 /* We fall back to GDBARCH if there is no architecture
9823 associated with SAL. */
9824 if (sarch == NULL)
9825 sarch = gdbarch;
9826 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9827 NULL, &msg);
9828 old_chain = make_cleanup (xfree, msg);
9829
9830 if (!rslt)
9831 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9832 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9833
9834 do_cleanups (old_chain);
9835 }
9836}
9837
af4908ba
KS
9838/* Issue an invalid thread ID error. */
9839
9840static void ATTRIBUTE_NORETURN
9841invalid_thread_id_error (int id)
9842{
9843 error (_("Unknown thread %d."), id);
9844}
9845
018d34a4
VP
9846/* Given TOK, a string specification of condition and thread, as
9847 accepted by the 'break' command, extract the condition
9848 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9849 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9850 If no condition is found, *COND_STRING is set to NULL.
9851 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9852
9853static void
bbc13ae3 9854find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9855 char **cond_string, int *thread, int *task,
9856 char **rest)
018d34a4
VP
9857{
9858 *cond_string = NULL;
9859 *thread = -1;
ed1d1739
KS
9860 *task = 0;
9861 *rest = NULL;
9862
018d34a4
VP
9863 while (tok && *tok)
9864 {
bbc13ae3 9865 const char *end_tok;
018d34a4 9866 int toklen;
bbc13ae3
KS
9867 const char *cond_start = NULL;
9868 const char *cond_end = NULL;
cc59ec59 9869
bbc13ae3 9870 tok = skip_spaces_const (tok);
e7e0cddf
SS
9871
9872 if ((*tok == '"' || *tok == ',') && rest)
9873 {
9874 *rest = savestring (tok, strlen (tok));
9875 return;
9876 }
9877
bbc13ae3 9878 end_tok = skip_to_space_const (tok);
d634f2de 9879
018d34a4 9880 toklen = end_tok - tok;
d634f2de 9881
018d34a4
VP
9882 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9883 {
f7545552
TT
9884 struct expression *expr;
9885
018d34a4 9886 tok = cond_start = end_tok + 1;
1bb9788d 9887 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9888 xfree (expr);
018d34a4 9889 cond_end = tok;
d634f2de 9890 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9891 }
9892 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9893 {
9894 char *tmptok;
d634f2de 9895
018d34a4 9896 tok = end_tok + 1;
bbc13ae3 9897 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9898 if (tok == tmptok)
9899 error (_("Junk after thread keyword."));
9900 if (!valid_thread_id (*thread))
af4908ba 9901 invalid_thread_id_error (*thread);
bbc13ae3 9902 tok = tmptok;
018d34a4 9903 }
4a306c9a
JB
9904 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9905 {
9906 char *tmptok;
9907
9908 tok = end_tok + 1;
bbc13ae3 9909 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9910 if (tok == tmptok)
9911 error (_("Junk after task keyword."));
9912 if (!valid_task_id (*task))
b6199126 9913 error (_("Unknown task %d."), *task);
bbc13ae3 9914 tok = tmptok;
4a306c9a 9915 }
e7e0cddf
SS
9916 else if (rest)
9917 {
9918 *rest = savestring (tok, strlen (tok));
ccab2054 9919 return;
e7e0cddf 9920 }
018d34a4
VP
9921 else
9922 error (_("Junk at end of arguments."));
9923 }
9924}
9925
0fb4aa4b
PA
9926/* Decode a static tracepoint marker spec. */
9927
9928static struct symtabs_and_lines
9929decode_static_tracepoint_spec (char **arg_p)
9930{
9931 VEC(static_tracepoint_marker_p) *markers = NULL;
9932 struct symtabs_and_lines sals;
0fb4aa4b
PA
9933 struct cleanup *old_chain;
9934 char *p = &(*arg_p)[3];
9935 char *endp;
9936 char *marker_str;
9937 int i;
9938
e9cafbcc 9939 p = skip_spaces (p);
0fb4aa4b 9940
e9cafbcc 9941 endp = skip_to_space (p);
0fb4aa4b
PA
9942
9943 marker_str = savestring (p, endp - p);
9944 old_chain = make_cleanup (xfree, marker_str);
9945
9946 markers = target_static_tracepoint_markers_by_strid (marker_str);
9947 if (VEC_empty(static_tracepoint_marker_p, markers))
9948 error (_("No known static tracepoint marker named %s"), marker_str);
9949
9950 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9951 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9952
9953 for (i = 0; i < sals.nelts; i++)
9954 {
9955 struct static_tracepoint_marker *marker;
9956
9957 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9958
9959 init_sal (&sals.sals[i]);
9960
9961 sals.sals[i] = find_pc_line (marker->address, 0);
9962 sals.sals[i].pc = marker->address;
9963
9964 release_static_tracepoint_marker (marker);
9965 }
9966
9967 do_cleanups (old_chain);
9968
9969 *arg_p = endp;
9970 return sals;
9971}
9972
fd9b8c24
PA
9973/* Set a breakpoint. This function is shared between CLI and MI
9974 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9975 modes of operations, selected by the PARSE_ARG parameter. If
9976 non-zero, the function will parse ARG, extracting location,
9977 condition, thread and extra string. Otherwise, ARG is just the
9978 breakpoint's location, with condition, thread, and extra string
9979 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9980 If INTERNAL is non-zero, the breakpoint number will be allocated
9981 from the internal breakpoint count. Returns true if any breakpoint
9982 was created; false otherwise. */
0101ce28 9983
8cdf0e15
VP
9984int
9985create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9986 char *arg, char *cond_string,
9987 int thread, char *extra_string,
f6de8ec2 9988 int parse_arg,
0fb4aa4b 9989 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9990 int ignore_count,
9991 enum auto_boolean pending_break_support,
c0a91b2b 9992 const struct breakpoint_ops *ops,
44f238bb
PA
9993 int from_tty, int enabled, int internal,
9994 unsigned flags)
c3f6f71d 9995{
b78a6381 9996 volatile struct gdb_exception e;
f8eba3c6 9997 char *copy_arg = NULL;
c3f6f71d 9998 char *addr_start = arg;
7efd8fc2 9999 struct linespec_result canonical;
c3f6f71d 10000 struct cleanup *old_chain;
80c99de1 10001 struct cleanup *bkpt_chain = NULL;
0101ce28 10002 int pending = 0;
4a306c9a 10003 int task = 0;
86b17b60 10004 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10005
348d480f
PA
10006 gdb_assert (ops != NULL);
10007
7efd8fc2 10008 init_linespec_result (&canonical);
c3f6f71d 10009
b78a6381
TT
10010 TRY_CATCH (e, RETURN_MASK_ALL)
10011 {
983af33b
SDJ
10012 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10013 addr_start, &copy_arg);
b78a6381 10014 }
0101ce28
JJ
10015
10016 /* If caller is interested in rc value from parse, set value. */
05ff989b 10017 switch (e.reason)
0101ce28 10018 {
983af33b
SDJ
10019 case GDB_NO_ERROR:
10020 if (VEC_empty (linespec_sals, canonical.sals))
10021 return 0;
10022 break;
05ff989b
AC
10023 case RETURN_ERROR:
10024 switch (e.error)
0101ce28 10025 {
05ff989b 10026 case NOT_FOUND_ERROR:
0101ce28 10027
05ff989b
AC
10028 /* If pending breakpoint support is turned off, throw
10029 error. */
fa8d40ab
JJ
10030
10031 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10032 throw_exception (e);
10033
10034 exception_print (gdb_stderr, e);
fa8d40ab 10035
05ff989b
AC
10036 /* If pending breakpoint support is auto query and the user
10037 selects no, then simply return the error code. */
059fb39f 10038 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10039 && !nquery (_("Make %s pending on future shared library load? "),
10040 bptype_string (type_wanted)))
fd9b8c24 10041 return 0;
fa8d40ab 10042
05ff989b
AC
10043 /* At this point, either the user was queried about setting
10044 a pending breakpoint and selected yes, or pending
10045 breakpoint behavior is on and thus a pending breakpoint
10046 is defaulted on behalf of the user. */
f8eba3c6
TT
10047 {
10048 struct linespec_sals lsal;
10049
10050 copy_arg = xstrdup (addr_start);
10051 lsal.canonical = xstrdup (copy_arg);
10052 lsal.sals.nelts = 1;
10053 lsal.sals.sals = XNEW (struct symtab_and_line);
10054 init_sal (&lsal.sals.sals[0]);
10055 pending = 1;
10056 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10057 }
05ff989b
AC
10058 break;
10059 default:
98deb0da 10060 throw_exception (e);
0101ce28 10061 }
2abae994 10062 break;
05ff989b 10063 default:
983af33b 10064 throw_exception (e);
0101ce28 10065 }
c3f6f71d 10066
4a64f543 10067 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10068 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10069
c3f6f71d
JM
10070 /* ----------------------------- SNIP -----------------------------
10071 Anything added to the cleanup chain beyond this point is assumed
10072 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10073 then the memory is not reclaimed. */
10074 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10075
c3f6f71d
JM
10076 /* Resolve all line numbers to PC's and verify that the addresses
10077 are ok for the target. */
0101ce28 10078 if (!pending)
f8eba3c6
TT
10079 {
10080 int ix;
10081 struct linespec_sals *iter;
10082
10083 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10084 breakpoint_sals_to_pc (&iter->sals);
10085 }
c3f6f71d 10086
7a697b8d 10087 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10088 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10089 {
10090 int ix;
10091 struct linespec_sals *iter;
10092
10093 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10094 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10095 }
7a697b8d 10096
c3f6f71d
JM
10097 /* Verify that condition can be parsed, before setting any
10098 breakpoints. Allocate a separate condition expression for each
4a64f543 10099 breakpoint. */
0101ce28 10100 if (!pending)
c3f6f71d 10101 {
f6de8ec2 10102 if (parse_arg)
72b2ff0e 10103 {
0878d0fa 10104 char *rest;
52d361e1
YQ
10105 struct linespec_sals *lsal;
10106
10107 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10108
0878d0fa
YQ
10109 /* Here we only parse 'arg' to separate condition
10110 from thread number, so parsing in context of first
10111 sal is OK. When setting the breakpoint we'll
10112 re-parse it in context of each sal. */
10113
10114 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10115 &thread, &task, &rest);
10116 if (cond_string)
10117 make_cleanup (xfree, cond_string);
10118 if (rest)
10119 make_cleanup (xfree, rest);
10120 if (rest)
10121 extra_string = rest;
72b2ff0e 10122 }
2f069f6f 10123 else
72b2ff0e 10124 {
0878d0fa
YQ
10125 if (*arg != '\0')
10126 error (_("Garbage '%s' at end of location"), arg);
10127
10128 /* Create a private copy of condition string. */
10129 if (cond_string)
10130 {
10131 cond_string = xstrdup (cond_string);
10132 make_cleanup (xfree, cond_string);
10133 }
10134 /* Create a private copy of any extra string. */
10135 if (extra_string)
10136 {
10137 extra_string = xstrdup (extra_string);
10138 make_cleanup (xfree, extra_string);
10139 }
72b2ff0e 10140 }
0fb4aa4b 10141
52d361e1 10142 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10143 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10144 tempflag ? disp_del : disp_donttouch,
10145 thread, task, ignore_count, ops,
44f238bb 10146 from_tty, enabled, internal, flags);
c906108c 10147 }
0101ce28
JJ
10148 else
10149 {
0101ce28
JJ
10150 struct breakpoint *b;
10151
0101ce28
JJ
10152 make_cleanup (xfree, copy_arg);
10153
bfccc43c
YQ
10154 if (is_tracepoint_type (type_wanted))
10155 {
10156 struct tracepoint *t;
10157
10158 t = XCNEW (struct tracepoint);
10159 b = &t->base;
10160 }
10161 else
10162 b = XNEW (struct breakpoint);
10163
10164 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10165
f8eba3c6 10166 b->addr_string = copy_arg;
f6de8ec2 10167 if (parse_arg)
e12c7713
MK
10168 b->cond_string = NULL;
10169 else
10170 {
10171 /* Create a private copy of condition string. */
10172 if (cond_string)
10173 {
10174 cond_string = xstrdup (cond_string);
10175 make_cleanup (xfree, cond_string);
10176 }
10177 b->cond_string = cond_string;
10178 }
e7e0cddf 10179 b->extra_string = NULL;
0101ce28 10180 b->ignore_count = ignore_count;
0101ce28 10181 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10182 b->condition_not_parsed = 1;
41447f92 10183 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10184 if ((type_wanted != bp_breakpoint
10185 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10186 b->pspace = current_program_space;
8bea4e01 10187
bfccc43c 10188 install_breakpoint (internal, b, 0);
0101ce28
JJ
10189 }
10190
f8eba3c6 10191 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10192 {
3e43a32a
MS
10193 warning (_("Multiple breakpoints were set.\nUse the "
10194 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10195 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10196 }
10197
80c99de1
PA
10198 /* That's it. Discard the cleanups for data inserted into the
10199 breakpoint. */
10200 discard_cleanups (bkpt_chain);
10201 /* But cleanup everything else. */
c3f6f71d 10202 do_cleanups (old_chain);
217dc9e2 10203
80c99de1 10204 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10205 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10206
10207 return 1;
c3f6f71d 10208}
c906108c 10209
348d480f 10210/* Set a breakpoint.
72b2ff0e
VP
10211 ARG is a string describing breakpoint address,
10212 condition, and thread.
10213 FLAG specifies if a breakpoint is hardware on,
10214 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10215 and BP_TEMPFLAG. */
348d480f 10216
98deb0da 10217static void
72b2ff0e 10218break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10219{
72b2ff0e 10220 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10221 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10222 ? bp_hardware_breakpoint
10223 : bp_breakpoint);
55aa24fb
SDJ
10224 struct breakpoint_ops *ops;
10225 const char *arg_cp = arg;
10226
10227 /* Matching breakpoints on probes. */
10228 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10229 ops = &bkpt_probe_breakpoint_ops;
10230 else
10231 ops = &bkpt_breakpoint_ops;
c3f6f71d 10232
8cdf0e15
VP
10233 create_breakpoint (get_current_arch (),
10234 arg,
e7e0cddf 10235 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10236 tempflag, type_wanted,
8cdf0e15
VP
10237 0 /* Ignore count */,
10238 pending_break_support,
55aa24fb 10239 ops,
8cdf0e15 10240 from_tty,
84f4c1fe 10241 1 /* enabled */,
44f238bb
PA
10242 0 /* internal */,
10243 0);
c906108c
SS
10244}
10245
c906108c
SS
10246/* Helper function for break_command_1 and disassemble_command. */
10247
10248void
fba45db2 10249resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10250{
10251 CORE_ADDR pc;
10252
10253 if (sal->pc == 0 && sal->symtab != NULL)
10254 {
10255 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10256 error (_("No line %d in file \"%s\"."),
05cba821 10257 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10258 sal->pc = pc;
6a048695 10259
4a64f543
MS
10260 /* If this SAL corresponds to a breakpoint inserted using a line
10261 number, then skip the function prologue if necessary. */
6a048695 10262 if (sal->explicit_line)
059acae7 10263 skip_prologue_sal (sal);
c906108c
SS
10264 }
10265
10266 if (sal->section == 0 && sal->symtab != NULL)
10267 {
346d1dfe 10268 const struct blockvector *bv;
3977b71f 10269 const struct block *b;
c5aa993b 10270 struct symbol *sym;
c906108c 10271
801e3a5b 10272 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10273 if (bv != NULL)
10274 {
7f0df278 10275 sym = block_linkage_function (b);
c906108c
SS
10276 if (sym != NULL)
10277 {
10278 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10279 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10280 }
10281 else
10282 {
4a64f543
MS
10283 /* It really is worthwhile to have the section, so we'll
10284 just have to look harder. This case can be executed
10285 if we have line numbers but no functions (as can
10286 happen in assembly source). */
c906108c 10287
7cbd4a93 10288 struct bound_minimal_symbol msym;
6c95b8df
PA
10289 struct cleanup *old_chain = save_current_space_and_thread ();
10290
10291 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10292
10293 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10294 if (msym.minsym)
efd66ac6 10295 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10296
10297 do_cleanups (old_chain);
c906108c
SS
10298 }
10299 }
10300 }
10301}
10302
10303void
fba45db2 10304break_command (char *arg, int from_tty)
c906108c 10305{
db107f19 10306 break_command_1 (arg, 0, from_tty);
c906108c
SS
10307}
10308
c906108c 10309void
fba45db2 10310tbreak_command (char *arg, int from_tty)
c906108c 10311{
db107f19 10312 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10313}
10314
c906108c 10315static void
fba45db2 10316hbreak_command (char *arg, int from_tty)
c906108c 10317{
db107f19 10318 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10319}
10320
10321static void
fba45db2 10322thbreak_command (char *arg, int from_tty)
c906108c 10323{
db107f19 10324 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10325}
10326
10327static void
fba45db2 10328stop_command (char *arg, int from_tty)
c906108c 10329{
a3f17187 10330 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10331Usage: stop in <function | address>\n\
a3f17187 10332 stop at <line>\n"));
c906108c
SS
10333}
10334
10335static void
fba45db2 10336stopin_command (char *arg, int from_tty)
c906108c
SS
10337{
10338 int badInput = 0;
10339
c5aa993b 10340 if (arg == (char *) NULL)
c906108c
SS
10341 badInput = 1;
10342 else if (*arg != '*')
10343 {
10344 char *argptr = arg;
10345 int hasColon = 0;
10346
4a64f543 10347 /* Look for a ':'. If this is a line number specification, then
53a5351d 10348 say it is bad, otherwise, it should be an address or
4a64f543 10349 function/method name. */
c906108c 10350 while (*argptr && !hasColon)
c5aa993b
JM
10351 {
10352 hasColon = (*argptr == ':');
10353 argptr++;
10354 }
c906108c
SS
10355
10356 if (hasColon)
c5aa993b 10357 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10358 else
c5aa993b 10359 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10360 }
10361
10362 if (badInput)
a3f17187 10363 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10364 else
db107f19 10365 break_command_1 (arg, 0, from_tty);
c906108c
SS
10366}
10367
10368static void
fba45db2 10369stopat_command (char *arg, int from_tty)
c906108c
SS
10370{
10371 int badInput = 0;
10372
c5aa993b 10373 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10374 badInput = 1;
10375 else
10376 {
10377 char *argptr = arg;
10378 int hasColon = 0;
10379
4a64f543
MS
10380 /* Look for a ':'. If there is a '::' then get out, otherwise
10381 it is probably a line number. */
c906108c 10382 while (*argptr && !hasColon)
c5aa993b
JM
10383 {
10384 hasColon = (*argptr == ':');
10385 argptr++;
10386 }
c906108c
SS
10387
10388 if (hasColon)
c5aa993b 10389 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10390 else
c5aa993b 10391 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10392 }
10393
10394 if (badInput)
a3f17187 10395 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10396 else
db107f19 10397 break_command_1 (arg, 0, from_tty);
c906108c
SS
10398}
10399
e7e0cddf
SS
10400/* The dynamic printf command is mostly like a regular breakpoint, but
10401 with a prewired command list consisting of a single output command,
10402 built from extra arguments supplied on the dprintf command
10403 line. */
10404
da821c7b 10405static void
e7e0cddf
SS
10406dprintf_command (char *arg, int from_tty)
10407{
10408 create_breakpoint (get_current_arch (),
10409 arg,
10410 NULL, 0, NULL, 1 /* parse arg */,
10411 0, bp_dprintf,
10412 0 /* Ignore count */,
10413 pending_break_support,
10414 &dprintf_breakpoint_ops,
10415 from_tty,
10416 1 /* enabled */,
10417 0 /* internal */,
10418 0);
10419}
10420
d3ce09f5
SS
10421static void
10422agent_printf_command (char *arg, int from_tty)
10423{
10424 error (_("May only run agent-printf on the target"));
10425}
10426
f1310107
TJB
10427/* Implement the "breakpoint_hit" breakpoint_ops method for
10428 ranged breakpoints. */
10429
10430static int
10431breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10432 struct address_space *aspace,
09ac7c10
TT
10433 CORE_ADDR bp_addr,
10434 const struct target_waitstatus *ws)
f1310107 10435{
09ac7c10 10436 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10437 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10438 return 0;
10439
f1310107
TJB
10440 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10441 bl->length, aspace, bp_addr);
10442}
10443
10444/* Implement the "resources_needed" breakpoint_ops method for
10445 ranged breakpoints. */
10446
10447static int
10448resources_needed_ranged_breakpoint (const struct bp_location *bl)
10449{
10450 return target_ranged_break_num_registers ();
10451}
10452
10453/* Implement the "print_it" breakpoint_ops method for
10454 ranged breakpoints. */
10455
10456static enum print_stop_action
348d480f 10457print_it_ranged_breakpoint (bpstat bs)
f1310107 10458{
348d480f 10459 struct breakpoint *b = bs->breakpoint_at;
f1310107 10460 struct bp_location *bl = b->loc;
79a45e25 10461 struct ui_out *uiout = current_uiout;
f1310107
TJB
10462
10463 gdb_assert (b->type == bp_hardware_breakpoint);
10464
10465 /* Ranged breakpoints have only one location. */
10466 gdb_assert (bl && bl->next == NULL);
10467
10468 annotate_breakpoint (b->number);
10469 if (b->disposition == disp_del)
10470 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10471 else
10472 ui_out_text (uiout, "\nRanged breakpoint ");
10473 if (ui_out_is_mi_like_p (uiout))
10474 {
10475 ui_out_field_string (uiout, "reason",
10476 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10477 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10478 }
10479 ui_out_field_int (uiout, "bkptno", b->number);
10480 ui_out_text (uiout, ", ");
10481
10482 return PRINT_SRC_AND_LOC;
10483}
10484
10485/* Implement the "print_one" breakpoint_ops method for
10486 ranged breakpoints. */
10487
10488static void
10489print_one_ranged_breakpoint (struct breakpoint *b,
10490 struct bp_location **last_loc)
10491{
10492 struct bp_location *bl = b->loc;
10493 struct value_print_options opts;
79a45e25 10494 struct ui_out *uiout = current_uiout;
f1310107
TJB
10495
10496 /* Ranged breakpoints have only one location. */
10497 gdb_assert (bl && bl->next == NULL);
10498
10499 get_user_print_options (&opts);
10500
10501 if (opts.addressprint)
10502 /* We don't print the address range here, it will be printed later
10503 by print_one_detail_ranged_breakpoint. */
10504 ui_out_field_skip (uiout, "addr");
10505 annotate_field (5);
10506 print_breakpoint_location (b, bl);
10507 *last_loc = bl;
10508}
10509
10510/* Implement the "print_one_detail" breakpoint_ops method for
10511 ranged breakpoints. */
10512
10513static void
10514print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10515 struct ui_out *uiout)
10516{
10517 CORE_ADDR address_start, address_end;
10518 struct bp_location *bl = b->loc;
f99d8bf4
PA
10519 struct ui_file *stb = mem_fileopen ();
10520 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10521
10522 gdb_assert (bl);
10523
10524 address_start = bl->address;
10525 address_end = address_start + bl->length - 1;
10526
10527 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10528 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10529 print_core_address (bl->gdbarch, address_start),
10530 print_core_address (bl->gdbarch, address_end));
10531 ui_out_field_stream (uiout, "addr", stb);
10532 ui_out_text (uiout, "\n");
10533
10534 do_cleanups (cleanup);
10535}
10536
10537/* Implement the "print_mention" breakpoint_ops method for
10538 ranged breakpoints. */
10539
10540static void
10541print_mention_ranged_breakpoint (struct breakpoint *b)
10542{
10543 struct bp_location *bl = b->loc;
79a45e25 10544 struct ui_out *uiout = current_uiout;
f1310107
TJB
10545
10546 gdb_assert (bl);
10547 gdb_assert (b->type == bp_hardware_breakpoint);
10548
10549 if (ui_out_is_mi_like_p (uiout))
10550 return;
10551
10552 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10553 b->number, paddress (bl->gdbarch, bl->address),
10554 paddress (bl->gdbarch, bl->address + bl->length - 1));
10555}
10556
10557/* Implement the "print_recreate" breakpoint_ops method for
10558 ranged breakpoints. */
10559
10560static void
10561print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10562{
10563 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10564 b->addr_string_range_end);
d9b3f62e 10565 print_recreate_thread (b, fp);
f1310107
TJB
10566}
10567
10568/* The breakpoint_ops structure to be used in ranged breakpoints. */
10569
2060206e 10570static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10571
10572/* Find the address where the end of the breakpoint range should be
10573 placed, given the SAL of the end of the range. This is so that if
10574 the user provides a line number, the end of the range is set to the
10575 last instruction of the given line. */
10576
10577static CORE_ADDR
10578find_breakpoint_range_end (struct symtab_and_line sal)
10579{
10580 CORE_ADDR end;
10581
10582 /* If the user provided a PC value, use it. Otherwise,
10583 find the address of the end of the given location. */
10584 if (sal.explicit_pc)
10585 end = sal.pc;
10586 else
10587 {
10588 int ret;
10589 CORE_ADDR start;
10590
10591 ret = find_line_pc_range (sal, &start, &end);
10592 if (!ret)
10593 error (_("Could not find location of the end of the range."));
10594
10595 /* find_line_pc_range returns the start of the next line. */
10596 end--;
10597 }
10598
10599 return end;
10600}
10601
10602/* Implement the "break-range" CLI command. */
10603
10604static void
10605break_range_command (char *arg, int from_tty)
10606{
10607 char *arg_start, *addr_string_start, *addr_string_end;
10608 struct linespec_result canonical_start, canonical_end;
10609 int bp_count, can_use_bp, length;
10610 CORE_ADDR end;
10611 struct breakpoint *b;
10612 struct symtab_and_line sal_start, sal_end;
f1310107 10613 struct cleanup *cleanup_bkpt;
f8eba3c6 10614 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10615
10616 /* We don't support software ranged breakpoints. */
10617 if (target_ranged_break_num_registers () < 0)
10618 error (_("This target does not support hardware ranged breakpoints."));
10619
10620 bp_count = hw_breakpoint_used_count ();
10621 bp_count += target_ranged_break_num_registers ();
10622 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10623 bp_count, 0);
10624 if (can_use_bp < 0)
10625 error (_("Hardware breakpoints used exceeds limit."));
10626
f8eba3c6 10627 arg = skip_spaces (arg);
f1310107
TJB
10628 if (arg == NULL || arg[0] == '\0')
10629 error(_("No address range specified."));
10630
f1310107
TJB
10631 init_linespec_result (&canonical_start);
10632
f8eba3c6
TT
10633 arg_start = arg;
10634 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10635
f8eba3c6 10636 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10637
10638 if (arg[0] != ',')
10639 error (_("Too few arguments."));
f8eba3c6 10640 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10641 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10642
10643 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10644
10645 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10646 || lsal_start->sals.nelts != 1)
f1310107
TJB
10647 error (_("Cannot create a ranged breakpoint with multiple locations."));
10648
f8eba3c6
TT
10649 sal_start = lsal_start->sals.sals[0];
10650 addr_string_start = savestring (arg_start, arg - arg_start);
10651 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10652
10653 arg++; /* Skip the comma. */
f8eba3c6 10654 arg = skip_spaces (arg);
f1310107
TJB
10655
10656 /* Parse the end location. */
10657
f1310107
TJB
10658 init_linespec_result (&canonical_end);
10659 arg_start = arg;
10660
f8eba3c6 10661 /* We call decode_line_full directly here instead of using
f1310107
TJB
10662 parse_breakpoint_sals because we need to specify the start location's
10663 symtab and line as the default symtab and line for the end of the
10664 range. This makes it possible to have ranges like "foo.c:27, +14",
10665 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10666 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10667 sal_start.symtab, sal_start.line,
10668 &canonical_end, NULL, NULL);
10669
10670 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10671
f8eba3c6 10672 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10673 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10674
10675 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10676 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10677 || lsal_end->sals.nelts != 1)
f1310107
TJB
10678 error (_("Cannot create a ranged breakpoint with multiple locations."));
10679
f8eba3c6
TT
10680 sal_end = lsal_end->sals.sals[0];
10681 addr_string_end = savestring (arg_start, arg - arg_start);
10682 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10683
10684 end = find_breakpoint_range_end (sal_end);
10685 if (sal_start.pc > end)
177b42fe 10686 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10687
10688 length = end - sal_start.pc + 1;
10689 if (length < 0)
10690 /* Length overflowed. */
10691 error (_("Address range too large."));
10692 else if (length == 1)
10693 {
10694 /* This range is simple enough to be handled by
10695 the `hbreak' command. */
10696 hbreak_command (addr_string_start, 1);
10697
10698 do_cleanups (cleanup_bkpt);
10699
10700 return;
10701 }
10702
10703 /* Now set up the breakpoint. */
10704 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10705 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10706 set_breakpoint_count (breakpoint_count + 1);
10707 b->number = breakpoint_count;
10708 b->disposition = disp_donttouch;
f8eba3c6
TT
10709 b->addr_string = xstrdup (addr_string_start);
10710 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10711 b->loc->length = length;
10712
f8eba3c6 10713 do_cleanups (cleanup_bkpt);
f1310107
TJB
10714
10715 mention (b);
8d3788bd 10716 observer_notify_breakpoint_created (b);
44702360 10717 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10718}
10719
4a64f543
MS
10720/* Return non-zero if EXP is verified as constant. Returned zero
10721 means EXP is variable. Also the constant detection may fail for
10722 some constant expressions and in such case still falsely return
10723 zero. */
2e6e3d9c 10724
65d79d4b
SDJ
10725static int
10726watchpoint_exp_is_const (const struct expression *exp)
10727{
10728 int i = exp->nelts;
10729
10730 while (i > 0)
10731 {
10732 int oplenp, argsp;
10733
10734 /* We are only interested in the descriptor of each element. */
10735 operator_length (exp, i, &oplenp, &argsp);
10736 i -= oplenp;
10737
10738 switch (exp->elts[i].opcode)
10739 {
10740 case BINOP_ADD:
10741 case BINOP_SUB:
10742 case BINOP_MUL:
10743 case BINOP_DIV:
10744 case BINOP_REM:
10745 case BINOP_MOD:
10746 case BINOP_LSH:
10747 case BINOP_RSH:
10748 case BINOP_LOGICAL_AND:
10749 case BINOP_LOGICAL_OR:
10750 case BINOP_BITWISE_AND:
10751 case BINOP_BITWISE_IOR:
10752 case BINOP_BITWISE_XOR:
10753 case BINOP_EQUAL:
10754 case BINOP_NOTEQUAL:
10755 case BINOP_LESS:
10756 case BINOP_GTR:
10757 case BINOP_LEQ:
10758 case BINOP_GEQ:
10759 case BINOP_REPEAT:
10760 case BINOP_COMMA:
10761 case BINOP_EXP:
10762 case BINOP_MIN:
10763 case BINOP_MAX:
10764 case BINOP_INTDIV:
10765 case BINOP_CONCAT:
65d79d4b
SDJ
10766 case TERNOP_COND:
10767 case TERNOP_SLICE:
65d79d4b
SDJ
10768
10769 case OP_LONG:
10770 case OP_DOUBLE:
10771 case OP_DECFLOAT:
10772 case OP_LAST:
10773 case OP_COMPLEX:
10774 case OP_STRING:
65d79d4b
SDJ
10775 case OP_ARRAY:
10776 case OP_TYPE:
608b4967
TT
10777 case OP_TYPEOF:
10778 case OP_DECLTYPE:
6e72ca20 10779 case OP_TYPEID:
65d79d4b
SDJ
10780 case OP_NAME:
10781 case OP_OBJC_NSSTRING:
10782
10783 case UNOP_NEG:
10784 case UNOP_LOGICAL_NOT:
10785 case UNOP_COMPLEMENT:
10786 case UNOP_ADDR:
10787 case UNOP_HIGH:
aeaa2474 10788 case UNOP_CAST:
9eaf6705
TT
10789
10790 case UNOP_CAST_TYPE:
10791 case UNOP_REINTERPRET_CAST:
10792 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10793 /* Unary, binary and ternary operators: We have to check
10794 their operands. If they are constant, then so is the
10795 result of that operation. For instance, if A and B are
10796 determined to be constants, then so is "A + B".
10797
10798 UNOP_IND is one exception to the rule above, because the
10799 value of *ADDR is not necessarily a constant, even when
10800 ADDR is. */
65d79d4b
SDJ
10801 break;
10802
10803 case OP_VAR_VALUE:
10804 /* Check whether the associated symbol is a constant.
4a64f543 10805
65d79d4b 10806 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10807 possible that a buggy compiler could mark a variable as
10808 constant even when it is not, and TYPE_CONST would return
10809 true in this case, while SYMBOL_CLASS wouldn't.
10810
10811 We also have to check for function symbols because they
10812 are always constant. */
65d79d4b
SDJ
10813 {
10814 struct symbol *s = exp->elts[i + 2].symbol;
10815
10816 if (SYMBOL_CLASS (s) != LOC_BLOCK
10817 && SYMBOL_CLASS (s) != LOC_CONST
10818 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10819 return 0;
10820 break;
10821 }
10822
10823 /* The default action is to return 0 because we are using
10824 the optimistic approach here: If we don't know something,
10825 then it is not a constant. */
10826 default:
10827 return 0;
10828 }
10829 }
10830
10831 return 1;
10832}
10833
3a5c3e22
PA
10834/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10835
10836static void
10837dtor_watchpoint (struct breakpoint *self)
10838{
10839 struct watchpoint *w = (struct watchpoint *) self;
10840
10841 xfree (w->cond_exp);
10842 xfree (w->exp);
10843 xfree (w->exp_string);
10844 xfree (w->exp_string_reparse);
10845 value_free (w->val);
10846
10847 base_breakpoint_ops.dtor (self);
10848}
10849
348d480f
PA
10850/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10851
10852static void
10853re_set_watchpoint (struct breakpoint *b)
10854{
3a5c3e22
PA
10855 struct watchpoint *w = (struct watchpoint *) b;
10856
348d480f
PA
10857 /* Watchpoint can be either on expression using entirely global
10858 variables, or it can be on local variables.
10859
10860 Watchpoints of the first kind are never auto-deleted, and even
10861 persist across program restarts. Since they can use variables
10862 from shared libraries, we need to reparse expression as libraries
10863 are loaded and unloaded.
10864
10865 Watchpoints on local variables can also change meaning as result
10866 of solib event. For example, if a watchpoint uses both a local
10867 and a global variables in expression, it's a local watchpoint,
10868 but unloading of a shared library will make the expression
10869 invalid. This is not a very common use case, but we still
10870 re-evaluate expression, to avoid surprises to the user.
10871
10872 Note that for local watchpoints, we re-evaluate it only if
10873 watchpoints frame id is still valid. If it's not, it means the
10874 watchpoint is out of scope and will be deleted soon. In fact,
10875 I'm not sure we'll ever be called in this case.
10876
10877 If a local watchpoint's frame id is still valid, then
3a5c3e22 10878 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10879
3a5c3e22
PA
10880 Don't do anything about disabled watchpoints, since they will be
10881 reevaluated again when enabled. */
10882 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10883}
10884
77b06cd7
TJB
10885/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10886
10887static int
10888insert_watchpoint (struct bp_location *bl)
10889{
3a5c3e22
PA
10890 struct watchpoint *w = (struct watchpoint *) bl->owner;
10891 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10892
10893 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10894 w->cond_exp);
77b06cd7
TJB
10895}
10896
10897/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10898
10899static int
10900remove_watchpoint (struct bp_location *bl)
10901{
3a5c3e22
PA
10902 struct watchpoint *w = (struct watchpoint *) bl->owner;
10903 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10904
10905 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10906 w->cond_exp);
e09342b5
TJB
10907}
10908
e09342b5 10909static int
348d480f 10910breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10911 struct address_space *aspace, CORE_ADDR bp_addr,
10912 const struct target_waitstatus *ws)
e09342b5 10913{
348d480f 10914 struct breakpoint *b = bl->owner;
3a5c3e22 10915 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10916
348d480f
PA
10917 /* Continuable hardware watchpoints are treated as non-existent if the
10918 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10919 some data address). Otherwise gdb won't stop on a break instruction
10920 in the code (not from a breakpoint) when a hardware watchpoint has
10921 been defined. Also skip watchpoints which we know did not trigger
10922 (did not match the data address). */
10923 if (is_hardware_watchpoint (b)
3a5c3e22 10924 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10925 return 0;
9c06b0b4 10926
348d480f 10927 return 1;
9c06b0b4
TJB
10928}
10929
348d480f
PA
10930static void
10931check_status_watchpoint (bpstat bs)
9c06b0b4 10932{
348d480f 10933 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10934
348d480f 10935 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10936}
10937
10938/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10939 hardware watchpoints. */
9c06b0b4
TJB
10940
10941static int
348d480f 10942resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10943{
3a5c3e22
PA
10944 struct watchpoint *w = (struct watchpoint *) bl->owner;
10945 int length = w->exact? 1 : bl->length;
348d480f
PA
10946
10947 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10948}
10949
10950/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10951 hardware watchpoints. */
9c06b0b4
TJB
10952
10953static int
348d480f 10954works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10955{
efa80663
PA
10956 /* Read and access watchpoints only work with hardware support. */
10957 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10958}
10959
9c06b0b4 10960static enum print_stop_action
348d480f 10961print_it_watchpoint (bpstat bs)
9c06b0b4 10962{
348d480f
PA
10963 struct cleanup *old_chain;
10964 struct breakpoint *b;
f99d8bf4 10965 struct ui_file *stb;
348d480f 10966 enum print_stop_action result;
3a5c3e22 10967 struct watchpoint *w;
79a45e25 10968 struct ui_out *uiout = current_uiout;
348d480f
PA
10969
10970 gdb_assert (bs->bp_location_at != NULL);
10971
348d480f 10972 b = bs->breakpoint_at;
3a5c3e22 10973 w = (struct watchpoint *) b;
348d480f 10974
f99d8bf4
PA
10975 stb = mem_fileopen ();
10976 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10977
10978 switch (b->type)
10979 {
348d480f 10980 case bp_watchpoint:
9c06b0b4
TJB
10981 case bp_hardware_watchpoint:
10982 annotate_watchpoint (b->number);
10983 if (ui_out_is_mi_like_p (uiout))
10984 ui_out_field_string
10985 (uiout, "reason",
10986 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10987 mention (b);
10988 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10989 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10990 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10991 ui_out_field_stream (uiout, "old", stb);
10992 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10993 watchpoint_value_print (w->val, stb);
348d480f
PA
10994 ui_out_field_stream (uiout, "new", stb);
10995 ui_out_text (uiout, "\n");
10996 /* More than one watchpoint may have been triggered. */
10997 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10998 break;
10999
11000 case bp_read_watchpoint:
11001 if (ui_out_is_mi_like_p (uiout))
11002 ui_out_field_string
11003 (uiout, "reason",
11004 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11005 mention (b);
11006 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11007 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11008 watchpoint_value_print (w->val, stb);
348d480f
PA
11009 ui_out_field_stream (uiout, "value", stb);
11010 ui_out_text (uiout, "\n");
11011 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11012 break;
11013
11014 case bp_access_watchpoint:
348d480f
PA
11015 if (bs->old_val != NULL)
11016 {
11017 annotate_watchpoint (b->number);
11018 if (ui_out_is_mi_like_p (uiout))
11019 ui_out_field_string
11020 (uiout, "reason",
11021 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11022 mention (b);
11023 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11024 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11025 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11026 ui_out_field_stream (uiout, "old", stb);
11027 ui_out_text (uiout, "\nNew value = ");
11028 }
11029 else
11030 {
11031 mention (b);
11032 if (ui_out_is_mi_like_p (uiout))
11033 ui_out_field_string
11034 (uiout, "reason",
11035 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11036 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11037 ui_out_text (uiout, "\nValue = ");
11038 }
f99d8bf4 11039 watchpoint_value_print (w->val, stb);
348d480f
PA
11040 ui_out_field_stream (uiout, "new", stb);
11041 ui_out_text (uiout, "\n");
11042 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11043 break;
11044 default:
348d480f 11045 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11046 }
11047
348d480f
PA
11048 do_cleanups (old_chain);
11049 return result;
11050}
11051
11052/* Implement the "print_mention" breakpoint_ops method for hardware
11053 watchpoints. */
11054
11055static void
11056print_mention_watchpoint (struct breakpoint *b)
11057{
11058 struct cleanup *ui_out_chain;
3a5c3e22 11059 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11060 struct ui_out *uiout = current_uiout;
348d480f
PA
11061
11062 switch (b->type)
11063 {
11064 case bp_watchpoint:
11065 ui_out_text (uiout, "Watchpoint ");
11066 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11067 break;
11068 case bp_hardware_watchpoint:
11069 ui_out_text (uiout, "Hardware watchpoint ");
11070 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11071 break;
11072 case bp_read_watchpoint:
11073 ui_out_text (uiout, "Hardware read watchpoint ");
11074 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11075 break;
11076 case bp_access_watchpoint:
11077 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11078 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11079 break;
11080 default:
11081 internal_error (__FILE__, __LINE__,
11082 _("Invalid hardware watchpoint type."));
11083 }
11084
11085 ui_out_field_int (uiout, "number", b->number);
11086 ui_out_text (uiout, ": ");
3a5c3e22 11087 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11088 do_cleanups (ui_out_chain);
11089}
11090
11091/* Implement the "print_recreate" breakpoint_ops method for
11092 watchpoints. */
11093
11094static void
11095print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11096{
3a5c3e22
PA
11097 struct watchpoint *w = (struct watchpoint *) b;
11098
348d480f
PA
11099 switch (b->type)
11100 {
11101 case bp_watchpoint:
11102 case bp_hardware_watchpoint:
11103 fprintf_unfiltered (fp, "watch");
11104 break;
11105 case bp_read_watchpoint:
11106 fprintf_unfiltered (fp, "rwatch");
11107 break;
11108 case bp_access_watchpoint:
11109 fprintf_unfiltered (fp, "awatch");
11110 break;
11111 default:
11112 internal_error (__FILE__, __LINE__,
11113 _("Invalid watchpoint type."));
11114 }
11115
3a5c3e22 11116 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11117 print_recreate_thread (b, fp);
348d480f
PA
11118}
11119
427cd150
TT
11120/* Implement the "explains_signal" breakpoint_ops method for
11121 watchpoints. */
11122
47591c29 11123static int
427cd150
TT
11124explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11125{
11126 /* A software watchpoint cannot cause a signal other than
11127 GDB_SIGNAL_TRAP. */
11128 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11129 return 0;
427cd150 11130
47591c29 11131 return 1;
427cd150
TT
11132}
11133
348d480f
PA
11134/* The breakpoint_ops structure to be used in hardware watchpoints. */
11135
2060206e 11136static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11137
11138/* Implement the "insert" breakpoint_ops method for
11139 masked hardware watchpoints. */
11140
11141static int
11142insert_masked_watchpoint (struct bp_location *bl)
11143{
3a5c3e22
PA
11144 struct watchpoint *w = (struct watchpoint *) bl->owner;
11145
11146 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11147 bl->watchpoint_type);
11148}
11149
11150/* Implement the "remove" breakpoint_ops method for
11151 masked hardware watchpoints. */
11152
11153static int
11154remove_masked_watchpoint (struct bp_location *bl)
11155{
3a5c3e22
PA
11156 struct watchpoint *w = (struct watchpoint *) bl->owner;
11157
11158 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11159 bl->watchpoint_type);
11160}
11161
11162/* Implement the "resources_needed" breakpoint_ops method for
11163 masked hardware watchpoints. */
11164
11165static int
11166resources_needed_masked_watchpoint (const struct bp_location *bl)
11167{
3a5c3e22
PA
11168 struct watchpoint *w = (struct watchpoint *) bl->owner;
11169
11170 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11171}
11172
11173/* Implement the "works_in_software_mode" breakpoint_ops method for
11174 masked hardware watchpoints. */
11175
11176static int
11177works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11178{
11179 return 0;
11180}
11181
11182/* Implement the "print_it" breakpoint_ops method for
11183 masked hardware watchpoints. */
11184
11185static enum print_stop_action
11186print_it_masked_watchpoint (bpstat bs)
11187{
11188 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11189 struct ui_out *uiout = current_uiout;
348d480f
PA
11190
11191 /* Masked watchpoints have only one location. */
11192 gdb_assert (b->loc && b->loc->next == NULL);
11193
11194 switch (b->type)
11195 {
11196 case bp_hardware_watchpoint:
11197 annotate_watchpoint (b->number);
11198 if (ui_out_is_mi_like_p (uiout))
11199 ui_out_field_string
11200 (uiout, "reason",
11201 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11202 break;
11203
11204 case bp_read_watchpoint:
11205 if (ui_out_is_mi_like_p (uiout))
11206 ui_out_field_string
11207 (uiout, "reason",
11208 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11209 break;
11210
11211 case bp_access_watchpoint:
11212 if (ui_out_is_mi_like_p (uiout))
11213 ui_out_field_string
11214 (uiout, "reason",
11215 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11216 break;
11217 default:
11218 internal_error (__FILE__, __LINE__,
11219 _("Invalid hardware watchpoint type."));
11220 }
11221
11222 mention (b);
9c06b0b4
TJB
11223 ui_out_text (uiout, _("\n\
11224Check the underlying instruction at PC for the memory\n\
11225address and value which triggered this watchpoint.\n"));
11226 ui_out_text (uiout, "\n");
11227
11228 /* More than one watchpoint may have been triggered. */
11229 return PRINT_UNKNOWN;
11230}
11231
11232/* Implement the "print_one_detail" breakpoint_ops method for
11233 masked hardware watchpoints. */
11234
11235static void
11236print_one_detail_masked_watchpoint (const struct breakpoint *b,
11237 struct ui_out *uiout)
11238{
3a5c3e22
PA
11239 struct watchpoint *w = (struct watchpoint *) b;
11240
9c06b0b4
TJB
11241 /* Masked watchpoints have only one location. */
11242 gdb_assert (b->loc && b->loc->next == NULL);
11243
11244 ui_out_text (uiout, "\tmask ");
3a5c3e22 11245 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11246 ui_out_text (uiout, "\n");
11247}
11248
11249/* Implement the "print_mention" breakpoint_ops method for
11250 masked hardware watchpoints. */
11251
11252static void
11253print_mention_masked_watchpoint (struct breakpoint *b)
11254{
3a5c3e22 11255 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11256 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11257 struct cleanup *ui_out_chain;
11258
11259 switch (b->type)
11260 {
11261 case bp_hardware_watchpoint:
11262 ui_out_text (uiout, "Masked hardware watchpoint ");
11263 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11264 break;
11265 case bp_read_watchpoint:
11266 ui_out_text (uiout, "Masked hardware read watchpoint ");
11267 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11268 break;
11269 case bp_access_watchpoint:
11270 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11271 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11272 break;
11273 default:
11274 internal_error (__FILE__, __LINE__,
11275 _("Invalid hardware watchpoint type."));
11276 }
11277
11278 ui_out_field_int (uiout, "number", b->number);
11279 ui_out_text (uiout, ": ");
3a5c3e22 11280 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11281 do_cleanups (ui_out_chain);
11282}
11283
11284/* Implement the "print_recreate" breakpoint_ops method for
11285 masked hardware watchpoints. */
11286
11287static void
11288print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11289{
3a5c3e22 11290 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11291 char tmp[40];
11292
11293 switch (b->type)
11294 {
11295 case bp_hardware_watchpoint:
11296 fprintf_unfiltered (fp, "watch");
11297 break;
11298 case bp_read_watchpoint:
11299 fprintf_unfiltered (fp, "rwatch");
11300 break;
11301 case bp_access_watchpoint:
11302 fprintf_unfiltered (fp, "awatch");
11303 break;
11304 default:
11305 internal_error (__FILE__, __LINE__,
11306 _("Invalid hardware watchpoint type."));
11307 }
11308
3a5c3e22
PA
11309 sprintf_vma (tmp, w->hw_wp_mask);
11310 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11311 print_recreate_thread (b, fp);
9c06b0b4
TJB
11312}
11313
11314/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11315
2060206e 11316static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11317
11318/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11319
11320static int
11321is_masked_watchpoint (const struct breakpoint *b)
11322{
11323 return b->ops == &masked_watchpoint_breakpoint_ops;
11324}
11325
53a5351d
JM
11326/* accessflag: hw_write: watch write,
11327 hw_read: watch read,
11328 hw_access: watch access (read or write) */
c906108c 11329static void
bbc13ae3 11330watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11331 int just_location, int internal)
c906108c 11332{
a9634178 11333 volatile struct gdb_exception e;
d983da9c 11334 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11335 struct expression *exp;
270140bd 11336 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11337 struct value *val, *mark, *result;
bb9d5f81 11338 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11339 struct frame_info *frame;
bbc13ae3
KS
11340 const char *exp_start = NULL;
11341 const char *exp_end = NULL;
11342 const char *tok, *end_tok;
9c06b0b4 11343 int toklen = -1;
bbc13ae3
KS
11344 const char *cond_start = NULL;
11345 const char *cond_end = NULL;
c906108c 11346 enum bptype bp_type;
37e4754d 11347 int thread = -1;
0cf6dd15 11348 int pc = 0;
9c06b0b4
TJB
11349 /* Flag to indicate whether we are going to use masks for
11350 the hardware watchpoint. */
11351 int use_mask = 0;
11352 CORE_ADDR mask = 0;
3a5c3e22 11353 struct watchpoint *w;
bbc13ae3
KS
11354 char *expression;
11355 struct cleanup *back_to;
c906108c 11356
37e4754d
LM
11357 /* Make sure that we actually have parameters to parse. */
11358 if (arg != NULL && arg[0] != '\0')
11359 {
bbc13ae3
KS
11360 const char *value_start;
11361
11362 exp_end = arg + strlen (arg);
37e4754d 11363
9c06b0b4
TJB
11364 /* Look for "parameter value" pairs at the end
11365 of the arguments string. */
bbc13ae3 11366 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11367 {
11368 /* Skip whitespace at the end of the argument list. */
11369 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11370 tok--;
11371
11372 /* Find the beginning of the last token.
11373 This is the value of the parameter. */
11374 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11375 tok--;
11376 value_start = tok + 1;
11377
11378 /* Skip whitespace. */
11379 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11380 tok--;
11381
11382 end_tok = tok;
11383
11384 /* Find the beginning of the second to last token.
11385 This is the parameter itself. */
11386 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11387 tok--;
11388 tok++;
11389 toklen = end_tok - tok + 1;
11390
11391 if (toklen == 6 && !strncmp (tok, "thread", 6))
11392 {
11393 /* At this point we've found a "thread" token, which means
11394 the user is trying to set a watchpoint that triggers
11395 only in a specific thread. */
11396 char *endp;
37e4754d 11397
9c06b0b4
TJB
11398 if (thread != -1)
11399 error(_("You can specify only one thread."));
37e4754d 11400
9c06b0b4
TJB
11401 /* Extract the thread ID from the next token. */
11402 thread = strtol (value_start, &endp, 0);
37e4754d 11403
9c06b0b4
TJB
11404 /* Check if the user provided a valid numeric value for the
11405 thread ID. */
11406 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11407 error (_("Invalid thread ID specification %s."), value_start);
11408
11409 /* Check if the thread actually exists. */
11410 if (!valid_thread_id (thread))
af4908ba 11411 invalid_thread_id_error (thread);
9c06b0b4
TJB
11412 }
11413 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11414 {
11415 /* We've found a "mask" token, which means the user wants to
11416 create a hardware watchpoint that is going to have the mask
11417 facility. */
11418 struct value *mask_value, *mark;
37e4754d 11419
9c06b0b4
TJB
11420 if (use_mask)
11421 error(_("You can specify only one mask."));
37e4754d 11422
9c06b0b4 11423 use_mask = just_location = 1;
37e4754d 11424
9c06b0b4
TJB
11425 mark = value_mark ();
11426 mask_value = parse_to_comma_and_eval (&value_start);
11427 mask = value_as_address (mask_value);
11428 value_free_to_mark (mark);
11429 }
11430 else
11431 /* We didn't recognize what we found. We should stop here. */
11432 break;
37e4754d 11433
9c06b0b4
TJB
11434 /* Truncate the string and get rid of the "parameter value" pair before
11435 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11436 exp_end = tok;
9c06b0b4 11437 }
37e4754d 11438 }
bbc13ae3
KS
11439 else
11440 exp_end = arg;
37e4754d 11441
bbc13ae3
KS
11442 /* Parse the rest of the arguments. From here on out, everything
11443 is in terms of a newly allocated string instead of the original
11444 ARG. */
c906108c 11445 innermost_block = NULL;
bbc13ae3
KS
11446 expression = savestring (arg, exp_end - arg);
11447 back_to = make_cleanup (xfree, expression);
11448 exp_start = arg = expression;
1bb9788d 11449 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11450 exp_end = arg;
fa8a61dc
TT
11451 /* Remove trailing whitespace from the expression before saving it.
11452 This makes the eventual display of the expression string a bit
11453 prettier. */
11454 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11455 --exp_end;
11456
65d79d4b
SDJ
11457 /* Checking if the expression is not constant. */
11458 if (watchpoint_exp_is_const (exp))
11459 {
11460 int len;
11461
11462 len = exp_end - exp_start;
11463 while (len > 0 && isspace (exp_start[len - 1]))
11464 len--;
11465 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11466 }
11467
c906108c
SS
11468 exp_valid_block = innermost_block;
11469 mark = value_mark ();
3a1115a0 11470 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11471
bb9d5f81
PP
11472 if (val != NULL && just_location)
11473 {
11474 saved_bitpos = value_bitpos (val);
11475 saved_bitsize = value_bitsize (val);
11476 }
11477
06a64a0b
TT
11478 if (just_location)
11479 {
9c06b0b4
TJB
11480 int ret;
11481
06a64a0b 11482 exp_valid_block = NULL;
a1442452 11483 val = value_addr (result);
06a64a0b
TT
11484 release_value (val);
11485 value_free_to_mark (mark);
9c06b0b4
TJB
11486
11487 if (use_mask)
11488 {
11489 ret = target_masked_watch_num_registers (value_as_address (val),
11490 mask);
11491 if (ret == -1)
11492 error (_("This target does not support masked watchpoints."));
11493 else if (ret == -2)
11494 error (_("Invalid mask or memory region."));
11495 }
06a64a0b
TT
11496 }
11497 else if (val != NULL)
fa4727a6 11498 release_value (val);
c906108c 11499
bbc13ae3
KS
11500 tok = skip_spaces_const (arg);
11501 end_tok = skip_to_space_const (tok);
c906108c
SS
11502
11503 toklen = end_tok - tok;
11504 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11505 {
2d134ed3
PA
11506 struct expression *cond;
11507
60e1c644 11508 innermost_block = NULL;
c906108c 11509 tok = cond_start = end_tok + 1;
1bb9788d 11510 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11511
11512 /* The watchpoint expression may not be local, but the condition
11513 may still be. E.g.: `watch global if local > 0'. */
11514 cond_exp_valid_block = innermost_block;
11515
2d134ed3 11516 xfree (cond);
c906108c
SS
11517 cond_end = tok;
11518 }
11519 if (*tok)
8a3fe4f8 11520 error (_("Junk at end of command."));
c906108c 11521
d983da9c 11522 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11523
11524 /* If the expression is "local", then set up a "watchpoint scope"
11525 breakpoint at the point where we've left the scope of the watchpoint
11526 expression. Create the scope breakpoint before the watchpoint, so
11527 that we will encounter it first in bpstat_stop_status. */
60e1c644 11528 if (exp_valid_block && frame)
d983da9c 11529 {
edb3359d
DJ
11530 if (frame_id_p (frame_unwind_caller_id (frame)))
11531 {
11532 scope_breakpoint
a6d9a66e
UW
11533 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11534 frame_unwind_caller_pc (frame),
06edf0c0
PA
11535 bp_watchpoint_scope,
11536 &momentary_breakpoint_ops);
d983da9c 11537
edb3359d 11538 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11539
edb3359d
DJ
11540 /* Automatically delete the breakpoint when it hits. */
11541 scope_breakpoint->disposition = disp_del;
d983da9c 11542
edb3359d
DJ
11543 /* Only break in the proper frame (help with recursion). */
11544 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11545
edb3359d 11546 /* Set the address at which we will stop. */
a6d9a66e
UW
11547 scope_breakpoint->loc->gdbarch
11548 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11549 scope_breakpoint->loc->requested_address
11550 = frame_unwind_caller_pc (frame);
11551 scope_breakpoint->loc->address
a6d9a66e
UW
11552 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11553 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11554 scope_breakpoint->type);
11555 }
d983da9c
DJ
11556 }
11557
e8369a73
AB
11558 /* Now set up the breakpoint. We create all watchpoints as hardware
11559 watchpoints here even if hardware watchpoints are turned off, a call
11560 to update_watchpoint later in this function will cause the type to
11561 drop back to bp_watchpoint (software watchpoint) if required. */
11562
11563 if (accessflag == hw_read)
11564 bp_type = bp_read_watchpoint;
11565 else if (accessflag == hw_access)
11566 bp_type = bp_access_watchpoint;
11567 else
11568 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11569
11570 w = XCNEW (struct watchpoint);
11571 b = &w->base;
348d480f 11572 if (use_mask)
3a5c3e22
PA
11573 init_raw_breakpoint_without_location (b, NULL, bp_type,
11574 &masked_watchpoint_breakpoint_ops);
348d480f 11575 else
3a5c3e22
PA
11576 init_raw_breakpoint_without_location (b, NULL, bp_type,
11577 &watchpoint_breakpoint_ops);
37e4754d 11578 b->thread = thread;
b5de0fa7 11579 b->disposition = disp_donttouch;
348d480f 11580 b->pspace = current_program_space;
3a5c3e22
PA
11581 w->exp = exp;
11582 w->exp_valid_block = exp_valid_block;
11583 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11584 if (just_location)
11585 {
11586 struct type *t = value_type (val);
11587 CORE_ADDR addr = value_as_address (val);
11588 char *name;
11589
11590 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11591 name = type_to_string (t);
11592
3a5c3e22 11593 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11594 core_addr_to_string (addr));
06a64a0b
TT
11595 xfree (name);
11596
3a5c3e22 11597 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11598 (int) (exp_end - exp_start), exp_start);
11599
06a64a0b
TT
11600 /* The above expression is in C. */
11601 b->language = language_c;
11602 }
11603 else
3a5c3e22 11604 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11605
11606 if (use_mask)
11607 {
3a5c3e22 11608 w->hw_wp_mask = mask;
9c06b0b4
TJB
11609 }
11610 else
11611 {
3a5c3e22 11612 w->val = val;
bb9d5f81
PP
11613 w->val_bitpos = saved_bitpos;
11614 w->val_bitsize = saved_bitsize;
3a5c3e22 11615 w->val_valid = 1;
9c06b0b4 11616 }
77b06cd7 11617
c906108c
SS
11618 if (cond_start)
11619 b->cond_string = savestring (cond_start, cond_end - cond_start);
11620 else
11621 b->cond_string = 0;
c5aa993b 11622
c906108c 11623 if (frame)
f6bc2008 11624 {
3a5c3e22
PA
11625 w->watchpoint_frame = get_frame_id (frame);
11626 w->watchpoint_thread = inferior_ptid;
f6bc2008 11627 }
c906108c 11628 else
f6bc2008 11629 {
3a5c3e22
PA
11630 w->watchpoint_frame = null_frame_id;
11631 w->watchpoint_thread = null_ptid;
f6bc2008 11632 }
c906108c 11633
d983da9c 11634 if (scope_breakpoint != NULL)
c906108c 11635 {
d983da9c
DJ
11636 /* The scope breakpoint is related to the watchpoint. We will
11637 need to act on them together. */
11638 b->related_breakpoint = scope_breakpoint;
11639 scope_breakpoint->related_breakpoint = b;
c906108c 11640 }
d983da9c 11641
06a64a0b
TT
11642 if (!just_location)
11643 value_free_to_mark (mark);
2d134ed3 11644
a9634178
TJB
11645 TRY_CATCH (e, RETURN_MASK_ALL)
11646 {
11647 /* Finally update the new watchpoint. This creates the locations
11648 that should be inserted. */
3a5c3e22 11649 update_watchpoint (w, 1);
a9634178
TJB
11650 }
11651 if (e.reason < 0)
11652 {
11653 delete_breakpoint (b);
11654 throw_exception (e);
11655 }
11656
3ea46bff 11657 install_breakpoint (internal, b, 1);
bbc13ae3 11658 do_cleanups (back_to);
c906108c
SS
11659}
11660
e09342b5 11661/* Return count of debug registers needed to watch the given expression.
e09342b5 11662 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11663
c906108c 11664static int
a9634178 11665can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11666{
11667 int found_memory_cnt = 0;
2e70b7b9 11668 struct value *head = v;
c906108c
SS
11669
11670 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11671 if (!can_use_hw_watchpoints)
c906108c 11672 return 0;
c5aa993b 11673
5c44784c
JM
11674 /* Make sure that the value of the expression depends only upon
11675 memory contents, and values computed from them within GDB. If we
11676 find any register references or function calls, we can't use a
11677 hardware watchpoint.
11678
11679 The idea here is that evaluating an expression generates a series
11680 of values, one holding the value of every subexpression. (The
11681 expression a*b+c has five subexpressions: a, b, a*b, c, and
11682 a*b+c.) GDB's values hold almost enough information to establish
11683 the criteria given above --- they identify memory lvalues,
11684 register lvalues, computed values, etcetera. So we can evaluate
11685 the expression, and then scan the chain of values that leaves
11686 behind to decide whether we can detect any possible change to the
11687 expression's final value using only hardware watchpoints.
11688
11689 However, I don't think that the values returned by inferior
11690 function calls are special in any way. So this function may not
11691 notice that an expression involving an inferior function call
11692 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11693 for (; v; v = value_next (v))
c906108c 11694 {
5c44784c 11695 if (VALUE_LVAL (v) == lval_memory)
c906108c 11696 {
8464be76
DJ
11697 if (v != head && value_lazy (v))
11698 /* A lazy memory lvalue in the chain is one that GDB never
11699 needed to fetch; we either just used its address (e.g.,
11700 `a' in `a.b') or we never needed it at all (e.g., `a'
11701 in `a,b'). This doesn't apply to HEAD; if that is
11702 lazy then it was not readable, but watch it anyway. */
5c44784c 11703 ;
53a5351d 11704 else
5c44784c
JM
11705 {
11706 /* Ahh, memory we actually used! Check if we can cover
11707 it with hardware watchpoints. */
df407dfe 11708 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11709
11710 /* We only watch structs and arrays if user asked for it
11711 explicitly, never if they just happen to appear in a
11712 middle of some value chain. */
11713 if (v == head
11714 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11715 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11716 {
42ae5230 11717 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11718 int len;
11719 int num_regs;
11720
a9634178 11721 len = (target_exact_watchpoints
e09342b5
TJB
11722 && is_scalar_type_recursive (vtype))?
11723 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11724
e09342b5
TJB
11725 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11726 if (!num_regs)
2e70b7b9
MS
11727 return 0;
11728 else
e09342b5 11729 found_memory_cnt += num_regs;
2e70b7b9 11730 }
5c44784c 11731 }
c5aa993b 11732 }
5086187c
AC
11733 else if (VALUE_LVAL (v) != not_lval
11734 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11735 return 0; /* These are values from the history (e.g., $1). */
5086187c 11736 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11737 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11738 }
11739
11740 /* The expression itself looks suitable for using a hardware
11741 watchpoint, but give the target machine a chance to reject it. */
11742 return found_memory_cnt;
11743}
11744
8b93c638 11745void
84f4c1fe 11746watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11747{
84f4c1fe 11748 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11749}
11750
06a64a0b
TT
11751/* A helper function that looks for the "-location" argument and then
11752 calls watch_command_1. */
11753
11754static void
11755watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11756{
11757 int just_location = 0;
11758
11759 if (arg
11760 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11761 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11762 {
e9cafbcc 11763 arg = skip_spaces (arg);
06a64a0b
TT
11764 just_location = 1;
11765 }
11766
84f4c1fe 11767 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11768}
8926118c 11769
c5aa993b 11770static void
fba45db2 11771watch_command (char *arg, int from_tty)
c906108c 11772{
06a64a0b 11773 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11774}
11775
8b93c638 11776void
84f4c1fe 11777rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11778{
84f4c1fe 11779 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11780}
8926118c 11781
c5aa993b 11782static void
fba45db2 11783rwatch_command (char *arg, int from_tty)
c906108c 11784{
06a64a0b 11785 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11786}
11787
8b93c638 11788void
84f4c1fe 11789awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11790{
84f4c1fe 11791 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11792}
8926118c 11793
c5aa993b 11794static void
fba45db2 11795awatch_command (char *arg, int from_tty)
c906108c 11796{
06a64a0b 11797 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11798}
c906108c 11799\f
c5aa993b 11800
43ff13b4 11801/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11802 because it uses the mechanisms of breakpoints. */
11803
bfec99b2
PA
11804struct until_break_command_continuation_args
11805{
11806 struct breakpoint *breakpoint;
11807 struct breakpoint *breakpoint2;
186c406b 11808 int thread_num;
bfec99b2
PA
11809};
11810
43ff13b4 11811/* This function is called by fetch_inferior_event via the
4a64f543 11812 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11813 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11814 command. */
c2c6d25f 11815static void
fa4cd53f 11816until_break_command_continuation (void *arg, int err)
43ff13b4 11817{
bfec99b2
PA
11818 struct until_break_command_continuation_args *a = arg;
11819
11820 delete_breakpoint (a->breakpoint);
11821 if (a->breakpoint2)
11822 delete_breakpoint (a->breakpoint2);
186c406b 11823 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11824}
11825
c906108c 11826void
ae66c1fc 11827until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11828{
11829 struct symtabs_and_lines sals;
11830 struct symtab_and_line sal;
8556afb4
PA
11831 struct frame_info *frame;
11832 struct gdbarch *frame_gdbarch;
11833 struct frame_id stack_frame_id;
11834 struct frame_id caller_frame_id;
c906108c 11835 struct breakpoint *breakpoint;
f107f563 11836 struct breakpoint *breakpoint2 = NULL;
c906108c 11837 struct cleanup *old_chain;
186c406b
TT
11838 int thread;
11839 struct thread_info *tp;
c906108c 11840
70509625 11841 clear_proceed_status (0);
c906108c
SS
11842
11843 /* Set a breakpoint where the user wants it and at return from
4a64f543 11844 this function. */
c5aa993b 11845
1bfeeb0f 11846 if (last_displayed_sal_is_valid ())
f8eba3c6 11847 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11848 get_last_displayed_symtab (),
f8eba3c6 11849 get_last_displayed_line ());
c906108c 11850 else
f8eba3c6
TT
11851 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11852 (struct symtab *) NULL, 0);
c5aa993b 11853
c906108c 11854 if (sals.nelts != 1)
8a3fe4f8 11855 error (_("Couldn't get information on specified line."));
c5aa993b 11856
c906108c 11857 sal = sals.sals[0];
4a64f543 11858 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11859
c906108c 11860 if (*arg)
8a3fe4f8 11861 error (_("Junk at end of arguments."));
c5aa993b 11862
c906108c 11863 resolve_sal_pc (&sal);
c5aa993b 11864
186c406b
TT
11865 tp = inferior_thread ();
11866 thread = tp->num;
11867
883bc8d1
PA
11868 old_chain = make_cleanup (null_cleanup, NULL);
11869
8556afb4
PA
11870 /* Note linespec handling above invalidates the frame chain.
11871 Installing a breakpoint also invalidates the frame chain (as it
11872 may need to switch threads), so do any frame handling before
11873 that. */
11874
11875 frame = get_selected_frame (NULL);
11876 frame_gdbarch = get_frame_arch (frame);
11877 stack_frame_id = get_stack_frame_id (frame);
11878 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11879
ae66c1fc
EZ
11880 /* Keep within the current frame, or in frames called by the current
11881 one. */
edb3359d 11882
883bc8d1 11883 if (frame_id_p (caller_frame_id))
c906108c 11884 {
883bc8d1
PA
11885 struct symtab_and_line sal2;
11886
11887 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11888 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11889 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11890 sal2,
11891 caller_frame_id,
f107f563
VP
11892 bp_until);
11893 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11894
883bc8d1 11895 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11896 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11897 }
c5aa993b 11898
c70a6932
JK
11899 /* set_momentary_breakpoint could invalidate FRAME. */
11900 frame = NULL;
11901
883bc8d1
PA
11902 if (anywhere)
11903 /* If the user told us to continue until a specified location,
11904 we don't specify a frame at which we need to stop. */
11905 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11906 null_frame_id, bp_until);
11907 else
11908 /* Otherwise, specify the selected frame, because we want to stop
11909 only at the very same frame. */
11910 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11911 stack_frame_id, bp_until);
11912 make_cleanup_delete_breakpoint (breakpoint);
11913
a493e3e2 11914 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11915
4a64f543
MS
11916 /* If we are running asynchronously, and proceed call above has
11917 actually managed to start the target, arrange for breakpoints to
11918 be deleted when the target stops. Otherwise, we're already
11919 stopped and delete breakpoints via cleanup chain. */
f107f563 11920
8ea051c5 11921 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11922 {
bfec99b2
PA
11923 struct until_break_command_continuation_args *args;
11924 args = xmalloc (sizeof (*args));
f107f563 11925
bfec99b2
PA
11926 args->breakpoint = breakpoint;
11927 args->breakpoint2 = breakpoint2;
186c406b 11928 args->thread_num = thread;
f107f563
VP
11929
11930 discard_cleanups (old_chain);
95e54da7
PA
11931 add_continuation (inferior_thread (),
11932 until_break_command_continuation, args,
604ead4a 11933 xfree);
f107f563
VP
11934 }
11935 else
c5aa993b 11936 do_cleanups (old_chain);
c906108c 11937}
ae66c1fc 11938
c906108c
SS
11939/* This function attempts to parse an optional "if <cond>" clause
11940 from the arg string. If one is not found, it returns NULL.
c5aa993b 11941
c906108c
SS
11942 Else, it returns a pointer to the condition string. (It does not
11943 attempt to evaluate the string against a particular block.) And,
11944 it updates arg to point to the first character following the parsed
4a64f543 11945 if clause in the arg string. */
53a5351d 11946
916703c0 11947char *
fba45db2 11948ep_parse_optional_if_clause (char **arg)
c906108c 11949{
c5aa993b
JM
11950 char *cond_string;
11951
11952 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11953 return NULL;
c5aa993b 11954
4a64f543 11955 /* Skip the "if" keyword. */
c906108c 11956 (*arg) += 2;
c5aa993b 11957
c906108c 11958 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11959 condition string. */
e9cafbcc 11960 *arg = skip_spaces (*arg);
c906108c 11961 cond_string = *arg;
c5aa993b 11962
4a64f543
MS
11963 /* Assume that the condition occupies the remainder of the arg
11964 string. */
c906108c 11965 (*arg) += strlen (cond_string);
c5aa993b 11966
c906108c
SS
11967 return cond_string;
11968}
c5aa993b 11969
c906108c
SS
11970/* Commands to deal with catching events, such as signals, exceptions,
11971 process start/exit, etc. */
c5aa993b
JM
11972
11973typedef enum
11974{
44feb3ce
TT
11975 catch_fork_temporary, catch_vfork_temporary,
11976 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11977}
11978catch_fork_kind;
11979
c906108c 11980static void
cc59ec59
MS
11981catch_fork_command_1 (char *arg, int from_tty,
11982 struct cmd_list_element *command)
c906108c 11983{
a6d9a66e 11984 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11985 char *cond_string = NULL;
44feb3ce
TT
11986 catch_fork_kind fork_kind;
11987 int tempflag;
11988
11989 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11990 tempflag = (fork_kind == catch_fork_temporary
11991 || fork_kind == catch_vfork_temporary);
c5aa993b 11992
44feb3ce
TT
11993 if (!arg)
11994 arg = "";
e9cafbcc 11995 arg = skip_spaces (arg);
c5aa993b 11996
c906108c 11997 /* The allowed syntax is:
c5aa993b
JM
11998 catch [v]fork
11999 catch [v]fork if <cond>
12000
4a64f543 12001 First, check if there's an if clause. */
c906108c 12002 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12003
c906108c 12004 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12005 error (_("Junk at end of arguments."));
c5aa993b 12006
c906108c 12007 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12008 and enable reporting of such events. */
c5aa993b
JM
12009 switch (fork_kind)
12010 {
44feb3ce
TT
12011 case catch_fork_temporary:
12012 case catch_fork_permanent:
a6d9a66e 12013 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12014 &catch_fork_breakpoint_ops);
c906108c 12015 break;
44feb3ce
TT
12016 case catch_vfork_temporary:
12017 case catch_vfork_permanent:
a6d9a66e 12018 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12019 &catch_vfork_breakpoint_ops);
c906108c 12020 break;
c5aa993b 12021 default:
8a3fe4f8 12022 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12023 break;
c5aa993b 12024 }
c906108c
SS
12025}
12026
12027static void
cc59ec59
MS
12028catch_exec_command_1 (char *arg, int from_tty,
12029 struct cmd_list_element *command)
c906108c 12030{
b4d90040 12031 struct exec_catchpoint *c;
a6d9a66e 12032 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12033 int tempflag;
c5aa993b 12034 char *cond_string = NULL;
c906108c 12035
44feb3ce
TT
12036 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12037
12038 if (!arg)
12039 arg = "";
e9cafbcc 12040 arg = skip_spaces (arg);
c906108c
SS
12041
12042 /* The allowed syntax is:
c5aa993b
JM
12043 catch exec
12044 catch exec if <cond>
c906108c 12045
4a64f543 12046 First, check if there's an if clause. */
c906108c
SS
12047 cond_string = ep_parse_optional_if_clause (&arg);
12048
12049 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12050 error (_("Junk at end of arguments."));
c906108c 12051
b4d90040
PA
12052 c = XNEW (struct exec_catchpoint);
12053 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12054 &catch_exec_breakpoint_ops);
12055 c->exec_pathname = NULL;
12056
3ea46bff 12057 install_breakpoint (0, &c->base, 1);
c906108c 12058}
c5aa993b 12059
9ac4176b 12060void
28010a5d
PA
12061init_ada_exception_breakpoint (struct breakpoint *b,
12062 struct gdbarch *gdbarch,
12063 struct symtab_and_line sal,
12064 char *addr_string,
c0a91b2b 12065 const struct breakpoint_ops *ops,
28010a5d 12066 int tempflag,
349774ef 12067 int enabled,
28010a5d 12068 int from_tty)
f7f9143b 12069{
f7f9143b
JB
12070 if (from_tty)
12071 {
5af949e3
UW
12072 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12073 if (!loc_gdbarch)
12074 loc_gdbarch = gdbarch;
12075
6c95b8df
PA
12076 describe_other_breakpoints (loc_gdbarch,
12077 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12078 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12079 version for exception catchpoints, because two catchpoints
12080 used for different exception names will use the same address.
12081 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12082 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12083 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12084 the user what type of catchpoint it is. The above is good
12085 enough for now, though. */
12086 }
12087
28010a5d 12088 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12089
349774ef 12090 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12091 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12092 b->addr_string = addr_string;
12093 b->language = language_ada;
f7f9143b
JB
12094}
12095
a96d9b2e
SDJ
12096/* Splits the argument using space as delimiter. Returns an xmalloc'd
12097 filter list, or NULL if no filtering is required. */
12098static VEC(int) *
12099catch_syscall_split_args (char *arg)
12100{
12101 VEC(int) *result = NULL;
29d0bb3d 12102 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12103
12104 while (*arg != '\0')
12105 {
12106 int i, syscall_number;
12107 char *endptr;
12108 char cur_name[128];
12109 struct syscall s;
12110
12111 /* Skip whitespace. */
529480d0 12112 arg = skip_spaces (arg);
a96d9b2e
SDJ
12113
12114 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12115 cur_name[i] = arg[i];
12116 cur_name[i] = '\0';
12117 arg += i;
12118
12119 /* Check if the user provided a syscall name or a number. */
12120 syscall_number = (int) strtol (cur_name, &endptr, 0);
12121 if (*endptr == '\0')
bccd0dd2 12122 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12123 else
12124 {
12125 /* We have a name. Let's check if it's valid and convert it
12126 to a number. */
12127 get_syscall_by_name (cur_name, &s);
12128
12129 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12130 /* Here we have to issue an error instead of a warning,
12131 because GDB cannot do anything useful if there's no
12132 syscall number to be caught. */
a96d9b2e
SDJ
12133 error (_("Unknown syscall name '%s'."), cur_name);
12134 }
12135
12136 /* Ok, it's valid. */
12137 VEC_safe_push (int, result, s.number);
12138 }
12139
12140 discard_cleanups (cleanup);
12141 return result;
12142}
12143
12144/* Implement the "catch syscall" command. */
12145
12146static void
cc59ec59
MS
12147catch_syscall_command_1 (char *arg, int from_tty,
12148 struct cmd_list_element *command)
a96d9b2e
SDJ
12149{
12150 int tempflag;
12151 VEC(int) *filter;
12152 struct syscall s;
12153 struct gdbarch *gdbarch = get_current_arch ();
12154
12155 /* Checking if the feature if supported. */
12156 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12157 error (_("The feature 'catch syscall' is not supported on \
ea666128 12158this architecture yet."));
a96d9b2e
SDJ
12159
12160 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12161
e9cafbcc 12162 arg = skip_spaces (arg);
a96d9b2e
SDJ
12163
12164 /* We need to do this first "dummy" translation in order
12165 to get the syscall XML file loaded or, most important,
12166 to display a warning to the user if there's no XML file
12167 for his/her architecture. */
12168 get_syscall_by_number (0, &s);
12169
12170 /* The allowed syntax is:
12171 catch syscall
12172 catch syscall <name | number> [<name | number> ... <name | number>]
12173
12174 Let's check if there's a syscall name. */
12175
12176 if (arg != NULL)
12177 filter = catch_syscall_split_args (arg);
12178 else
12179 filter = NULL;
12180
12181 create_syscall_event_catchpoint (tempflag, filter,
12182 &catch_syscall_breakpoint_ops);
12183}
12184
c906108c 12185static void
fba45db2 12186catch_command (char *arg, int from_tty)
c906108c 12187{
44feb3ce 12188 error (_("Catch requires an event name."));
c906108c
SS
12189}
12190\f
12191
12192static void
fba45db2 12193tcatch_command (char *arg, int from_tty)
c906108c 12194{
44feb3ce 12195 error (_("Catch requires an event name."));
c906108c
SS
12196}
12197
8a2c437b
TT
12198/* A qsort comparison function that sorts breakpoints in order. */
12199
12200static int
12201compare_breakpoints (const void *a, const void *b)
12202{
12203 const breakpoint_p *ba = a;
12204 uintptr_t ua = (uintptr_t) *ba;
12205 const breakpoint_p *bb = b;
12206 uintptr_t ub = (uintptr_t) *bb;
12207
12208 if ((*ba)->number < (*bb)->number)
12209 return -1;
12210 else if ((*ba)->number > (*bb)->number)
12211 return 1;
12212
12213 /* Now sort by address, in case we see, e..g, two breakpoints with
12214 the number 0. */
12215 if (ua < ub)
12216 return -1;
94b0e70d 12217 return ua > ub ? 1 : 0;
8a2c437b
TT
12218}
12219
80f8a6eb 12220/* Delete breakpoints by address or line. */
c906108c
SS
12221
12222static void
fba45db2 12223clear_command (char *arg, int from_tty)
c906108c 12224{
8a2c437b 12225 struct breakpoint *b, *prev;
d6e956e5
VP
12226 VEC(breakpoint_p) *found = 0;
12227 int ix;
c906108c
SS
12228 int default_match;
12229 struct symtabs_and_lines sals;
12230 struct symtab_and_line sal;
c906108c 12231 int i;
8a2c437b 12232 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12233
12234 if (arg)
12235 {
39cf75f7
DE
12236 sals = decode_line_with_current_source (arg,
12237 (DECODE_LINE_FUNFIRSTLINE
12238 | DECODE_LINE_LIST_MODE));
cf4ded82 12239 make_cleanup (xfree, sals.sals);
c906108c
SS
12240 default_match = 0;
12241 }
12242 else
12243 {
c5aa993b 12244 sals.sals = (struct symtab_and_line *)
c906108c 12245 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12246 make_cleanup (xfree, sals.sals);
4a64f543 12247 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12248
12249 /* Set sal's line, symtab, pc, and pspace to the values
12250 corresponding to the last call to print_frame_info. If the
12251 codepoint is not valid, this will set all the fields to 0. */
12252 get_last_displayed_sal (&sal);
c906108c 12253 if (sal.symtab == 0)
8a3fe4f8 12254 error (_("No source file specified."));
c906108c
SS
12255
12256 sals.sals[0] = sal;
12257 sals.nelts = 1;
12258
12259 default_match = 1;
12260 }
12261
4a64f543
MS
12262 /* We don't call resolve_sal_pc here. That's not as bad as it
12263 seems, because all existing breakpoints typically have both
12264 file/line and pc set. So, if clear is given file/line, we can
12265 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12266
12267 We only support clearing given the address explicitly
12268 present in breakpoint table. Say, we've set breakpoint
4a64f543 12269 at file:line. There were several PC values for that file:line,
ed0616c6 12270 due to optimization, all in one block.
4a64f543
MS
12271
12272 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12273 PC corresponding to the same file:line, the breakpoint won't
12274 be cleared. We probably can still clear the breakpoint, but
12275 since the other PC value is never presented to user, user
12276 can only find it by guessing, and it does not seem important
12277 to support that. */
12278
4a64f543
MS
12279 /* For each line spec given, delete bps which correspond to it. Do
12280 it in two passes, solely to preserve the current behavior that
12281 from_tty is forced true if we delete more than one
12282 breakpoint. */
c906108c 12283
80f8a6eb 12284 found = NULL;
8a2c437b 12285 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12286 for (i = 0; i < sals.nelts; i++)
12287 {
05cba821
JK
12288 const char *sal_fullname;
12289
c906108c 12290 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12291 If line given (pc == 0), clear all bpts on specified line.
12292 If defaulting, clear all bpts on default line
c906108c 12293 or at default pc.
c5aa993b
JM
12294
12295 defaulting sal.pc != 0 tests to do
12296
12297 0 1 pc
12298 1 1 pc _and_ line
12299 0 0 line
12300 1 0 <can't happen> */
c906108c
SS
12301
12302 sal = sals.sals[i];
05cba821
JK
12303 sal_fullname = (sal.symtab == NULL
12304 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12305
4a64f543 12306 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12307 ALL_BREAKPOINTS (b)
c5aa993b 12308 {
0d381245 12309 int match = 0;
4a64f543 12310 /* Are we going to delete b? */
cc60f2e3 12311 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12312 {
12313 struct bp_location *loc = b->loc;
12314 for (; loc; loc = loc->next)
12315 {
f8eba3c6
TT
12316 /* If the user specified file:line, don't allow a PC
12317 match. This matches historical gdb behavior. */
12318 int pc_match = (!sal.explicit_line
12319 && sal.pc
12320 && (loc->pspace == sal.pspace)
12321 && (loc->address == sal.pc)
12322 && (!section_is_overlay (loc->section)
12323 || loc->section == sal.section));
4aac40c8
TT
12324 int line_match = 0;
12325
12326 if ((default_match || sal.explicit_line)
2f202fde 12327 && loc->symtab != NULL
05cba821 12328 && sal_fullname != NULL
4aac40c8 12329 && sal.pspace == loc->pspace
05cba821
JK
12330 && loc->line_number == sal.line
12331 && filename_cmp (symtab_to_fullname (loc->symtab),
12332 sal_fullname) == 0)
12333 line_match = 1;
4aac40c8 12334
0d381245
VP
12335 if (pc_match || line_match)
12336 {
12337 match = 1;
12338 break;
12339 }
12340 }
12341 }
12342
12343 if (match)
d6e956e5 12344 VEC_safe_push(breakpoint_p, found, b);
c906108c 12345 }
80f8a6eb 12346 }
8a2c437b 12347
80f8a6eb 12348 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12349 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12350 {
12351 if (arg)
8a3fe4f8 12352 error (_("No breakpoint at %s."), arg);
80f8a6eb 12353 else
8a3fe4f8 12354 error (_("No breakpoint at this line."));
80f8a6eb 12355 }
c906108c 12356
8a2c437b
TT
12357 /* Remove duplicates from the vec. */
12358 qsort (VEC_address (breakpoint_p, found),
12359 VEC_length (breakpoint_p, found),
12360 sizeof (breakpoint_p),
12361 compare_breakpoints);
12362 prev = VEC_index (breakpoint_p, found, 0);
12363 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12364 {
12365 if (b == prev)
12366 {
12367 VEC_ordered_remove (breakpoint_p, found, ix);
12368 --ix;
12369 }
12370 }
12371
d6e956e5 12372 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12373 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12374 if (from_tty)
a3f17187 12375 {
d6e956e5 12376 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12377 printf_unfiltered (_("Deleted breakpoint "));
12378 else
12379 printf_unfiltered (_("Deleted breakpoints "));
12380 }
d6e956e5
VP
12381
12382 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12383 {
c5aa993b 12384 if (from_tty)
d6e956e5
VP
12385 printf_unfiltered ("%d ", b->number);
12386 delete_breakpoint (b);
c906108c 12387 }
80f8a6eb
MS
12388 if (from_tty)
12389 putchar_unfiltered ('\n');
8a2c437b
TT
12390
12391 do_cleanups (cleanups);
c906108c
SS
12392}
12393\f
12394/* Delete breakpoint in BS if they are `delete' breakpoints and
12395 all breakpoints that are marked for deletion, whether hit or not.
12396 This is called after any breakpoint is hit, or after errors. */
12397
12398void
fba45db2 12399breakpoint_auto_delete (bpstat bs)
c906108c 12400{
35df4500 12401 struct breakpoint *b, *b_tmp;
c906108c
SS
12402
12403 for (; bs; bs = bs->next)
f431efe5
PA
12404 if (bs->breakpoint_at
12405 && bs->breakpoint_at->disposition == disp_del
c906108c 12406 && bs->stop)
f431efe5 12407 delete_breakpoint (bs->breakpoint_at);
c906108c 12408
35df4500 12409 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12410 {
b5de0fa7 12411 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12412 delete_breakpoint (b);
12413 }
c906108c
SS
12414}
12415
4a64f543
MS
12416/* A comparison function for bp_location AP and BP being interfaced to
12417 qsort. Sort elements primarily by their ADDRESS (no matter what
12418 does breakpoint_address_is_meaningful say for its OWNER),
12419 secondarily by ordering first bp_permanent OWNERed elements and
12420 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12421 qsort being an unstable algorithm. */
876fa593
JK
12422
12423static int
494cfb0f 12424bp_location_compare (const void *ap, const void *bp)
876fa593 12425{
494cfb0f
JK
12426 struct bp_location *a = *(void **) ap;
12427 struct bp_location *b = *(void **) bp;
2bdf28a0 12428 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12429 int a_perm = a->owner->enable_state == bp_permanent;
12430 int b_perm = b->owner->enable_state == bp_permanent;
12431
12432 if (a->address != b->address)
12433 return (a->address > b->address) - (a->address < b->address);
12434
dea2aa5f
LM
12435 /* Sort locations at the same address by their pspace number, keeping
12436 locations of the same inferior (in a multi-inferior environment)
12437 grouped. */
12438
12439 if (a->pspace->num != b->pspace->num)
12440 return ((a->pspace->num > b->pspace->num)
12441 - (a->pspace->num < b->pspace->num));
12442
876fa593
JK
12443 /* Sort permanent breakpoints first. */
12444 if (a_perm != b_perm)
12445 return (a_perm < b_perm) - (a_perm > b_perm);
12446
c56a97f9
JK
12447 /* Make the internal GDB representation stable across GDB runs
12448 where A and B memory inside GDB can differ. Breakpoint locations of
12449 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12450
12451 if (a->owner->number != b->owner->number)
c56a97f9
JK
12452 return ((a->owner->number > b->owner->number)
12453 - (a->owner->number < b->owner->number));
876fa593
JK
12454
12455 return (a > b) - (a < b);
12456}
12457
876fa593 12458/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12459 bp_location_shadow_len_after_address_max according to the current
12460 content of the bp_location array. */
f7545552
TT
12461
12462static void
876fa593 12463bp_location_target_extensions_update (void)
f7545552 12464{
876fa593
JK
12465 struct bp_location *bl, **blp_tmp;
12466
12467 bp_location_placed_address_before_address_max = 0;
12468 bp_location_shadow_len_after_address_max = 0;
12469
12470 ALL_BP_LOCATIONS (bl, blp_tmp)
12471 {
12472 CORE_ADDR start, end, addr;
12473
12474 if (!bp_location_has_shadow (bl))
12475 continue;
12476
12477 start = bl->target_info.placed_address;
12478 end = start + bl->target_info.shadow_len;
12479
12480 gdb_assert (bl->address >= start);
12481 addr = bl->address - start;
12482 if (addr > bp_location_placed_address_before_address_max)
12483 bp_location_placed_address_before_address_max = addr;
12484
12485 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12486
12487 gdb_assert (bl->address < end);
12488 addr = end - bl->address;
12489 if (addr > bp_location_shadow_len_after_address_max)
12490 bp_location_shadow_len_after_address_max = addr;
12491 }
f7545552
TT
12492}
12493
1e4d1764
YQ
12494/* Download tracepoint locations if they haven't been. */
12495
12496static void
12497download_tracepoint_locations (void)
12498{
7ed2c994 12499 struct breakpoint *b;
1e4d1764
YQ
12500 struct cleanup *old_chain;
12501
12502 if (!target_can_download_tracepoint ())
12503 return;
12504
12505 old_chain = save_current_space_and_thread ();
12506
7ed2c994 12507 ALL_TRACEPOINTS (b)
1e4d1764 12508 {
7ed2c994 12509 struct bp_location *bl;
1e4d1764 12510 struct tracepoint *t;
f2a8bc8a 12511 int bp_location_downloaded = 0;
1e4d1764 12512
7ed2c994 12513 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12514 ? !may_insert_fast_tracepoints
12515 : !may_insert_tracepoints))
12516 continue;
12517
7ed2c994
YQ
12518 for (bl = b->loc; bl; bl = bl->next)
12519 {
12520 /* In tracepoint, locations are _never_ duplicated, so
12521 should_be_inserted is equivalent to
12522 unduplicated_should_be_inserted. */
12523 if (!should_be_inserted (bl) || bl->inserted)
12524 continue;
1e4d1764 12525
7ed2c994 12526 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12527
7ed2c994 12528 target_download_tracepoint (bl);
1e4d1764 12529
7ed2c994 12530 bl->inserted = 1;
f2a8bc8a 12531 bp_location_downloaded = 1;
7ed2c994
YQ
12532 }
12533 t = (struct tracepoint *) b;
12534 t->number_on_target = b->number;
f2a8bc8a
YQ
12535 if (bp_location_downloaded)
12536 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12537 }
12538
12539 do_cleanups (old_chain);
12540}
12541
934709f0
PW
12542/* Swap the insertion/duplication state between two locations. */
12543
12544static void
12545swap_insertion (struct bp_location *left, struct bp_location *right)
12546{
12547 const int left_inserted = left->inserted;
12548 const int left_duplicate = left->duplicate;
b775012e 12549 const int left_needs_update = left->needs_update;
934709f0
PW
12550 const struct bp_target_info left_target_info = left->target_info;
12551
1e4d1764
YQ
12552 /* Locations of tracepoints can never be duplicated. */
12553 if (is_tracepoint (left->owner))
12554 gdb_assert (!left->duplicate);
12555 if (is_tracepoint (right->owner))
12556 gdb_assert (!right->duplicate);
12557
934709f0
PW
12558 left->inserted = right->inserted;
12559 left->duplicate = right->duplicate;
b775012e 12560 left->needs_update = right->needs_update;
934709f0
PW
12561 left->target_info = right->target_info;
12562 right->inserted = left_inserted;
12563 right->duplicate = left_duplicate;
b775012e 12564 right->needs_update = left_needs_update;
934709f0
PW
12565 right->target_info = left_target_info;
12566}
12567
b775012e
LM
12568/* Force the re-insertion of the locations at ADDRESS. This is called
12569 once a new/deleted/modified duplicate location is found and we are evaluating
12570 conditions on the target's side. Such conditions need to be updated on
12571 the target. */
12572
12573static void
12574force_breakpoint_reinsertion (struct bp_location *bl)
12575{
12576 struct bp_location **locp = NULL, **loc2p;
12577 struct bp_location *loc;
12578 CORE_ADDR address = 0;
12579 int pspace_num;
12580
12581 address = bl->address;
12582 pspace_num = bl->pspace->num;
12583
12584 /* This is only meaningful if the target is
12585 evaluating conditions and if the user has
12586 opted for condition evaluation on the target's
12587 side. */
12588 if (gdb_evaluates_breakpoint_condition_p ()
12589 || !target_supports_evaluation_of_breakpoint_conditions ())
12590 return;
12591
12592 /* Flag all breakpoint locations with this address and
12593 the same program space as the location
12594 as "its condition has changed". We need to
12595 update the conditions on the target's side. */
12596 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12597 {
12598 loc = *loc2p;
12599
12600 if (!is_breakpoint (loc->owner)
12601 || pspace_num != loc->pspace->num)
12602 continue;
12603
12604 /* Flag the location appropriately. We use a different state to
12605 let everyone know that we already updated the set of locations
12606 with addr bl->address and program space bl->pspace. This is so
12607 we don't have to keep calling these functions just to mark locations
12608 that have already been marked. */
12609 loc->condition_changed = condition_updated;
12610
12611 /* Free the agent expression bytecode as well. We will compute
12612 it later on. */
12613 if (loc->cond_bytecode)
12614 {
12615 free_agent_expr (loc->cond_bytecode);
12616 loc->cond_bytecode = NULL;
12617 }
12618 }
12619}
44702360
PA
12620/* Called whether new breakpoints are created, or existing breakpoints
12621 deleted, to update the global location list and recompute which
12622 locations are duplicate of which.
b775012e 12623
04086b45
PA
12624 The INSERT_MODE flag determines whether locations may not, may, or
12625 shall be inserted now. See 'enum ugll_insert_mode' for more
12626 info. */
b60e7edf 12627
0d381245 12628static void
44702360 12629update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12630{
74960c60 12631 struct breakpoint *b;
876fa593 12632 struct bp_location **locp, *loc;
f7545552 12633 struct cleanup *cleanups;
b775012e
LM
12634 /* Last breakpoint location address that was marked for update. */
12635 CORE_ADDR last_addr = 0;
12636 /* Last breakpoint location program space that was marked for update. */
12637 int last_pspace_num = -1;
f7545552 12638
2d134ed3
PA
12639 /* Used in the duplicates detection below. When iterating over all
12640 bp_locations, points to the first bp_location of a given address.
12641 Breakpoints and watchpoints of different types are never
12642 duplicates of each other. Keep one pointer for each type of
12643 breakpoint/watchpoint, so we only need to loop over all locations
12644 once. */
12645 struct bp_location *bp_loc_first; /* breakpoint */
12646 struct bp_location *wp_loc_first; /* hardware watchpoint */
12647 struct bp_location *awp_loc_first; /* access watchpoint */
12648 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12649
4a64f543
MS
12650 /* Saved former bp_location array which we compare against the newly
12651 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12652 struct bp_location **old_location, **old_locp;
12653 unsigned old_location_count;
12654
12655 old_location = bp_location;
12656 old_location_count = bp_location_count;
12657 bp_location = NULL;
12658 bp_location_count = 0;
12659 cleanups = make_cleanup (xfree, old_location);
0d381245 12660
74960c60 12661 ALL_BREAKPOINTS (b)
876fa593
JK
12662 for (loc = b->loc; loc; loc = loc->next)
12663 bp_location_count++;
12664
12665 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12666 locp = bp_location;
12667 ALL_BREAKPOINTS (b)
12668 for (loc = b->loc; loc; loc = loc->next)
12669 *locp++ = loc;
12670 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12671 bp_location_compare);
876fa593
JK
12672
12673 bp_location_target_extensions_update ();
74960c60 12674
4a64f543
MS
12675 /* Identify bp_location instances that are no longer present in the
12676 new list, and therefore should be freed. Note that it's not
12677 necessary that those locations should be removed from inferior --
12678 if there's another location at the same address (previously
12679 marked as duplicate), we don't need to remove/insert the
12680 location.
876fa593 12681
4a64f543
MS
12682 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12683 and former bp_location array state respectively. */
876fa593
JK
12684
12685 locp = bp_location;
12686 for (old_locp = old_location; old_locp < old_location + old_location_count;
12687 old_locp++)
74960c60 12688 {
876fa593 12689 struct bp_location *old_loc = *old_locp;
c7d46a38 12690 struct bp_location **loc2p;
876fa593 12691
e5dd4106 12692 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12693 not, we have to free it. */
c7d46a38 12694 int found_object = 0;
20874c92
VP
12695 /* Tells if the location should remain inserted in the target. */
12696 int keep_in_target = 0;
12697 int removed = 0;
876fa593 12698
4a64f543
MS
12699 /* Skip LOCP entries which will definitely never be needed.
12700 Stop either at or being the one matching OLD_LOC. */
876fa593 12701 while (locp < bp_location + bp_location_count
c7d46a38 12702 && (*locp)->address < old_loc->address)
876fa593 12703 locp++;
c7d46a38
PA
12704
12705 for (loc2p = locp;
12706 (loc2p < bp_location + bp_location_count
12707 && (*loc2p)->address == old_loc->address);
12708 loc2p++)
12709 {
b775012e
LM
12710 /* Check if this is a new/duplicated location or a duplicated
12711 location that had its condition modified. If so, we want to send
12712 its condition to the target if evaluation of conditions is taking
12713 place there. */
12714 if ((*loc2p)->condition_changed == condition_modified
12715 && (last_addr != old_loc->address
12716 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12717 {
b775012e
LM
12718 force_breakpoint_reinsertion (*loc2p);
12719 last_pspace_num = old_loc->pspace->num;
c7d46a38 12720 }
b775012e
LM
12721
12722 if (*loc2p == old_loc)
12723 found_object = 1;
c7d46a38 12724 }
74960c60 12725
b775012e
LM
12726 /* We have already handled this address, update it so that we don't
12727 have to go through updates again. */
12728 last_addr = old_loc->address;
12729
12730 /* Target-side condition evaluation: Handle deleted locations. */
12731 if (!found_object)
12732 force_breakpoint_reinsertion (old_loc);
12733
4a64f543
MS
12734 /* If this location is no longer present, and inserted, look if
12735 there's maybe a new location at the same address. If so,
12736 mark that one inserted, and don't remove this one. This is
12737 needed so that we don't have a time window where a breakpoint
12738 at certain location is not inserted. */
74960c60 12739
876fa593 12740 if (old_loc->inserted)
0d381245 12741 {
4a64f543
MS
12742 /* If the location is inserted now, we might have to remove
12743 it. */
74960c60 12744
876fa593 12745 if (found_object && should_be_inserted (old_loc))
74960c60 12746 {
4a64f543
MS
12747 /* The location is still present in the location list,
12748 and still should be inserted. Don't do anything. */
20874c92 12749 keep_in_target = 1;
74960c60
VP
12750 }
12751 else
12752 {
b775012e
LM
12753 /* This location still exists, but it won't be kept in the
12754 target since it may have been disabled. We proceed to
12755 remove its target-side condition. */
12756
4a64f543
MS
12757 /* The location is either no longer present, or got
12758 disabled. See if there's another location at the
12759 same address, in which case we don't need to remove
12760 this one from the target. */
876fa593 12761
2bdf28a0 12762 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12763 if (breakpoint_address_is_meaningful (old_loc->owner))
12764 {
876fa593 12765 for (loc2p = locp;
c7d46a38
PA
12766 (loc2p < bp_location + bp_location_count
12767 && (*loc2p)->address == old_loc->address);
876fa593
JK
12768 loc2p++)
12769 {
12770 struct bp_location *loc2 = *loc2p;
12771
2d134ed3 12772 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12773 {
85d721b8
PA
12774 /* Read watchpoint locations are switched to
12775 access watchpoints, if the former are not
12776 supported, but the latter are. */
12777 if (is_hardware_watchpoint (old_loc->owner))
12778 {
12779 gdb_assert (is_hardware_watchpoint (loc2->owner));
12780 loc2->watchpoint_type = old_loc->watchpoint_type;
12781 }
12782
934709f0
PW
12783 /* loc2 is a duplicated location. We need to check
12784 if it should be inserted in case it will be
12785 unduplicated. */
12786 if (loc2 != old_loc
12787 && unduplicated_should_be_inserted (loc2))
c7d46a38 12788 {
934709f0 12789 swap_insertion (old_loc, loc2);
c7d46a38
PA
12790 keep_in_target = 1;
12791 break;
12792 }
876fa593
JK
12793 }
12794 }
12795 }
74960c60
VP
12796 }
12797
20874c92
VP
12798 if (!keep_in_target)
12799 {
876fa593 12800 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12801 {
4a64f543
MS
12802 /* This is just about all we can do. We could keep
12803 this location on the global list, and try to
12804 remove it next time, but there's no particular
12805 reason why we will succeed next time.
20874c92 12806
4a64f543
MS
12807 Note that at this point, old_loc->owner is still
12808 valid, as delete_breakpoint frees the breakpoint
12809 only after calling us. */
3e43a32a
MS
12810 printf_filtered (_("warning: Error removing "
12811 "breakpoint %d\n"),
876fa593 12812 old_loc->owner->number);
20874c92
VP
12813 }
12814 removed = 1;
12815 }
0d381245 12816 }
74960c60
VP
12817
12818 if (!found_object)
1c5cfe86 12819 {
db82e815
PA
12820 if (removed && non_stop
12821 && breakpoint_address_is_meaningful (old_loc->owner)
12822 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12823 {
db82e815
PA
12824 /* This location was removed from the target. In
12825 non-stop mode, a race condition is possible where
12826 we've removed a breakpoint, but stop events for that
12827 breakpoint are already queued and will arrive later.
12828 We apply an heuristic to be able to distinguish such
12829 SIGTRAPs from other random SIGTRAPs: we keep this
12830 breakpoint location for a bit, and will retire it
12831 after we see some number of events. The theory here
12832 is that reporting of events should, "on the average",
12833 be fair, so after a while we'll see events from all
12834 threads that have anything of interest, and no longer
12835 need to keep this breakpoint location around. We
12836 don't hold locations forever so to reduce chances of
12837 mistaking a non-breakpoint SIGTRAP for a breakpoint
12838 SIGTRAP.
12839
12840 The heuristic failing can be disastrous on
12841 decr_pc_after_break targets.
12842
12843 On decr_pc_after_break targets, like e.g., x86-linux,
12844 if we fail to recognize a late breakpoint SIGTRAP,
12845 because events_till_retirement has reached 0 too
12846 soon, we'll fail to do the PC adjustment, and report
12847 a random SIGTRAP to the user. When the user resumes
12848 the inferior, it will most likely immediately crash
2dec564e 12849 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12850 corrupted, because of being resumed e.g., in the
12851 middle of a multi-byte instruction, or skipped a
12852 one-byte instruction. This was actually seen happen
12853 on native x86-linux, and should be less rare on
12854 targets that do not support new thread events, like
12855 remote, due to the heuristic depending on
12856 thread_count.
12857
12858 Mistaking a random SIGTRAP for a breakpoint trap
12859 causes similar symptoms (PC adjustment applied when
12860 it shouldn't), but then again, playing with SIGTRAPs
12861 behind the debugger's back is asking for trouble.
12862
12863 Since hardware watchpoint traps are always
12864 distinguishable from other traps, so we don't need to
12865 apply keep hardware watchpoint moribund locations
12866 around. We simply always ignore hardware watchpoint
12867 traps we can no longer explain. */
12868
876fa593
JK
12869 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12870 old_loc->owner = NULL;
20874c92 12871
876fa593 12872 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12873 }
12874 else
f431efe5
PA
12875 {
12876 old_loc->owner = NULL;
12877 decref_bp_location (&old_loc);
12878 }
20874c92 12879 }
74960c60 12880 }
1c5cfe86 12881
348d480f
PA
12882 /* Rescan breakpoints at the same address and section, marking the
12883 first one as "first" and any others as "duplicates". This is so
12884 that the bpt instruction is only inserted once. If we have a
12885 permanent breakpoint at the same place as BPT, make that one the
12886 official one, and the rest as duplicates. Permanent breakpoints
12887 are sorted first for the same address.
12888
12889 Do the same for hardware watchpoints, but also considering the
12890 watchpoint's type (regular/access/read) and length. */
12891
12892 bp_loc_first = NULL;
12893 wp_loc_first = NULL;
12894 awp_loc_first = NULL;
12895 rwp_loc_first = NULL;
12896 ALL_BP_LOCATIONS (loc, locp)
12897 {
12898 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12899 non-NULL. */
348d480f 12900 struct bp_location **loc_first_p;
d3fbdd86 12901 b = loc->owner;
348d480f 12902
6f380991 12903 if (!unduplicated_should_be_inserted (loc)
348d480f 12904 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12905 /* Don't detect duplicate for tracepoint locations because they are
12906 never duplicated. See the comments in field `duplicate' of
12907 `struct bp_location'. */
348d480f 12908 || is_tracepoint (b))
b775012e
LM
12909 {
12910 /* Clear the condition modification flag. */
12911 loc->condition_changed = condition_unchanged;
12912 continue;
12913 }
348d480f
PA
12914
12915 /* Permanent breakpoint should always be inserted. */
12916 if (b->enable_state == bp_permanent && ! loc->inserted)
12917 internal_error (__FILE__, __LINE__,
12918 _("allegedly permanent breakpoint is not "
12919 "actually inserted"));
12920
12921 if (b->type == bp_hardware_watchpoint)
12922 loc_first_p = &wp_loc_first;
12923 else if (b->type == bp_read_watchpoint)
12924 loc_first_p = &rwp_loc_first;
12925 else if (b->type == bp_access_watchpoint)
12926 loc_first_p = &awp_loc_first;
12927 else
12928 loc_first_p = &bp_loc_first;
12929
12930 if (*loc_first_p == NULL
12931 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12932 || !breakpoint_locations_match (loc, *loc_first_p))
12933 {
12934 *loc_first_p = loc;
12935 loc->duplicate = 0;
b775012e
LM
12936
12937 if (is_breakpoint (loc->owner) && loc->condition_changed)
12938 {
12939 loc->needs_update = 1;
12940 /* Clear the condition modification flag. */
12941 loc->condition_changed = condition_unchanged;
12942 }
348d480f
PA
12943 continue;
12944 }
12945
934709f0
PW
12946
12947 /* This and the above ensure the invariant that the first location
12948 is not duplicated, and is the inserted one.
12949 All following are marked as duplicated, and are not inserted. */
12950 if (loc->inserted)
12951 swap_insertion (loc, *loc_first_p);
348d480f
PA
12952 loc->duplicate = 1;
12953
b775012e
LM
12954 /* Clear the condition modification flag. */
12955 loc->condition_changed = condition_unchanged;
12956
348d480f
PA
12957 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12958 && b->enable_state != bp_permanent)
12959 internal_error (__FILE__, __LINE__,
12960 _("another breakpoint was inserted on top of "
12961 "a permanent breakpoint"));
12962 }
12963
a25a5a45 12964 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12965 {
04086b45 12966 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12967 insert_breakpoint_locations ();
12968 else
12969 {
44702360
PA
12970 /* Even though the caller told us to not insert new
12971 locations, we may still need to update conditions on the
12972 target's side of breakpoints that were already inserted
12973 if the target is evaluating breakpoint conditions. We
b775012e
LM
12974 only update conditions for locations that are marked
12975 "needs_update". */
12976 update_inserted_breakpoint_locations ();
12977 }
12978 }
348d480f 12979
04086b45 12980 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12981 download_tracepoint_locations ();
12982
348d480f
PA
12983 do_cleanups (cleanups);
12984}
12985
12986void
12987breakpoint_retire_moribund (void)
12988{
12989 struct bp_location *loc;
12990 int ix;
12991
12992 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12993 if (--(loc->events_till_retirement) == 0)
12994 {
12995 decref_bp_location (&loc);
12996 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12997 --ix;
12998 }
12999}
13000
13001static void
44702360 13002update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13003{
bfd189b1 13004 volatile struct gdb_exception e;
348d480f
PA
13005
13006 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13007 update_global_location_list (insert_mode);
348d480f
PA
13008}
13009
13010/* Clear BKP from a BPS. */
13011
13012static void
13013bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13014{
13015 bpstat bs;
13016
13017 for (bs = bps; bs; bs = bs->next)
13018 if (bs->breakpoint_at == bpt)
13019 {
13020 bs->breakpoint_at = NULL;
13021 bs->old_val = NULL;
13022 /* bs->commands will be freed later. */
13023 }
13024}
13025
13026/* Callback for iterate_over_threads. */
13027static int
13028bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13029{
13030 struct breakpoint *bpt = data;
13031
13032 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13033 return 0;
13034}
13035
13036/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13037 callbacks. */
13038
13039static void
13040say_where (struct breakpoint *b)
13041{
13042 struct value_print_options opts;
13043
13044 get_user_print_options (&opts);
13045
13046 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13047 single string. */
13048 if (b->loc == NULL)
13049 {
13050 printf_filtered (_(" (%s) pending."), b->addr_string);
13051 }
13052 else
13053 {
2f202fde 13054 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13055 {
13056 printf_filtered (" at ");
13057 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13058 gdb_stdout);
13059 }
2f202fde 13060 if (b->loc->symtab != NULL)
f8eba3c6
TT
13061 {
13062 /* If there is a single location, we can print the location
13063 more nicely. */
13064 if (b->loc->next == NULL)
13065 printf_filtered (": file %s, line %d.",
05cba821
JK
13066 symtab_to_filename_for_display (b->loc->symtab),
13067 b->loc->line_number);
f8eba3c6
TT
13068 else
13069 /* This is not ideal, but each location may have a
13070 different file name, and this at least reflects the
13071 real situation somewhat. */
13072 printf_filtered (": %s.", b->addr_string);
13073 }
348d480f
PA
13074
13075 if (b->loc->next)
13076 {
13077 struct bp_location *loc = b->loc;
13078 int n = 0;
13079 for (; loc; loc = loc->next)
13080 ++n;
13081 printf_filtered (" (%d locations)", n);
13082 }
13083 }
13084}
13085
348d480f
PA
13086/* Default bp_location_ops methods. */
13087
13088static void
13089bp_location_dtor (struct bp_location *self)
13090{
13091 xfree (self->cond);
b775012e
LM
13092 if (self->cond_bytecode)
13093 free_agent_expr (self->cond_bytecode);
348d480f 13094 xfree (self->function_name);
8b4f3082
PA
13095
13096 VEC_free (agent_expr_p, self->target_info.conditions);
13097 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13098}
13099
13100static const struct bp_location_ops bp_location_ops =
13101{
13102 bp_location_dtor
13103};
13104
2060206e
PA
13105/* Default breakpoint_ops methods all breakpoint_ops ultimately
13106 inherit from. */
348d480f 13107
2060206e
PA
13108static void
13109base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13110{
13111 decref_counted_command_line (&self->commands);
13112 xfree (self->cond_string);
fb81d016 13113 xfree (self->extra_string);
348d480f 13114 xfree (self->addr_string);
f8eba3c6 13115 xfree (self->filter);
348d480f 13116 xfree (self->addr_string_range_end);
348d480f
PA
13117}
13118
2060206e
PA
13119static struct bp_location *
13120base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13121{
13122 struct bp_location *loc;
13123
13124 loc = XNEW (struct bp_location);
13125 init_bp_location (loc, &bp_location_ops, self);
13126 return loc;
13127}
13128
2060206e
PA
13129static void
13130base_breakpoint_re_set (struct breakpoint *b)
13131{
13132 /* Nothing to re-set. */
13133}
13134
13135#define internal_error_pure_virtual_called() \
13136 gdb_assert_not_reached ("pure virtual function called")
13137
13138static int
13139base_breakpoint_insert_location (struct bp_location *bl)
13140{
13141 internal_error_pure_virtual_called ();
13142}
13143
13144static int
13145base_breakpoint_remove_location (struct bp_location *bl)
13146{
13147 internal_error_pure_virtual_called ();
13148}
13149
13150static int
13151base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13152 struct address_space *aspace,
09ac7c10
TT
13153 CORE_ADDR bp_addr,
13154 const struct target_waitstatus *ws)
2060206e
PA
13155{
13156 internal_error_pure_virtual_called ();
13157}
13158
13159static void
13160base_breakpoint_check_status (bpstat bs)
13161{
13162 /* Always stop. */
13163}
13164
13165/* A "works_in_software_mode" breakpoint_ops method that just internal
13166 errors. */
13167
13168static int
13169base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13170{
13171 internal_error_pure_virtual_called ();
13172}
13173
13174/* A "resources_needed" breakpoint_ops method that just internal
13175 errors. */
13176
13177static int
13178base_breakpoint_resources_needed (const struct bp_location *bl)
13179{
13180 internal_error_pure_virtual_called ();
13181}
13182
13183static enum print_stop_action
13184base_breakpoint_print_it (bpstat bs)
13185{
13186 internal_error_pure_virtual_called ();
13187}
13188
13189static void
13190base_breakpoint_print_one_detail (const struct breakpoint *self,
13191 struct ui_out *uiout)
13192{
13193 /* nothing */
13194}
13195
13196static void
13197base_breakpoint_print_mention (struct breakpoint *b)
13198{
13199 internal_error_pure_virtual_called ();
13200}
13201
13202static void
13203base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13204{
13205 internal_error_pure_virtual_called ();
13206}
13207
983af33b
SDJ
13208static void
13209base_breakpoint_create_sals_from_address (char **arg,
13210 struct linespec_result *canonical,
13211 enum bptype type_wanted,
13212 char *addr_start,
13213 char **copy_arg)
13214{
13215 internal_error_pure_virtual_called ();
13216}
13217
13218static void
13219base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13220 struct linespec_result *c,
983af33b 13221 char *cond_string,
e7e0cddf 13222 char *extra_string,
983af33b
SDJ
13223 enum bptype type_wanted,
13224 enum bpdisp disposition,
13225 int thread,
13226 int task, int ignore_count,
13227 const struct breakpoint_ops *o,
13228 int from_tty, int enabled,
44f238bb 13229 int internal, unsigned flags)
983af33b
SDJ
13230{
13231 internal_error_pure_virtual_called ();
13232}
13233
13234static void
13235base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13236 struct symtabs_and_lines *sals)
13237{
13238 internal_error_pure_virtual_called ();
13239}
13240
ab04a2af
TT
13241/* The default 'explains_signal' method. */
13242
47591c29 13243static int
427cd150 13244base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13245{
47591c29 13246 return 1;
ab04a2af
TT
13247}
13248
9d6e6e84
HZ
13249/* The default "after_condition_true" method. */
13250
13251static void
13252base_breakpoint_after_condition_true (struct bpstats *bs)
13253{
13254 /* Nothing to do. */
13255}
13256
ab04a2af 13257struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13258{
13259 base_breakpoint_dtor,
13260 base_breakpoint_allocate_location,
13261 base_breakpoint_re_set,
13262 base_breakpoint_insert_location,
13263 base_breakpoint_remove_location,
13264 base_breakpoint_breakpoint_hit,
13265 base_breakpoint_check_status,
13266 base_breakpoint_resources_needed,
13267 base_breakpoint_works_in_software_mode,
13268 base_breakpoint_print_it,
13269 NULL,
13270 base_breakpoint_print_one_detail,
13271 base_breakpoint_print_mention,
983af33b
SDJ
13272 base_breakpoint_print_recreate,
13273 base_breakpoint_create_sals_from_address,
13274 base_breakpoint_create_breakpoints_sal,
13275 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13276 base_breakpoint_explains_signal,
13277 base_breakpoint_after_condition_true,
2060206e
PA
13278};
13279
13280/* Default breakpoint_ops methods. */
13281
13282static void
348d480f
PA
13283bkpt_re_set (struct breakpoint *b)
13284{
06edf0c0
PA
13285 /* FIXME: is this still reachable? */
13286 if (b->addr_string == NULL)
13287 {
13288 /* Anything without a string can't be re-set. */
348d480f 13289 delete_breakpoint (b);
06edf0c0 13290 return;
348d480f 13291 }
06edf0c0
PA
13292
13293 breakpoint_re_set_default (b);
348d480f
PA
13294}
13295
2060206e 13296static int
348d480f
PA
13297bkpt_insert_location (struct bp_location *bl)
13298{
13299 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13300 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13301 else
7c16b83e 13302 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13303}
13304
2060206e 13305static int
348d480f
PA
13306bkpt_remove_location (struct bp_location *bl)
13307{
13308 if (bl->loc_type == bp_loc_hardware_breakpoint)
13309 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13310 else
7c16b83e 13311 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13312}
13313
2060206e 13314static int
348d480f 13315bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13316 struct address_space *aspace, CORE_ADDR bp_addr,
13317 const struct target_waitstatus *ws)
348d480f 13318{
09ac7c10 13319 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13320 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13321 return 0;
13322
348d480f
PA
13323 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13324 aspace, bp_addr))
13325 return 0;
13326
13327 if (overlay_debugging /* unmapped overlay section */
13328 && section_is_overlay (bl->section)
13329 && !section_is_mapped (bl->section))
13330 return 0;
13331
13332 return 1;
13333}
13334
cd1608cc
PA
13335static int
13336dprintf_breakpoint_hit (const struct bp_location *bl,
13337 struct address_space *aspace, CORE_ADDR bp_addr,
13338 const struct target_waitstatus *ws)
13339{
13340 if (dprintf_style == dprintf_style_agent
13341 && target_can_run_breakpoint_commands ())
13342 {
13343 /* An agent-style dprintf never causes a stop. If we see a trap
13344 for this address it must be for a breakpoint that happens to
13345 be set at the same address. */
13346 return 0;
13347 }
13348
13349 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13350}
13351
2060206e 13352static int
348d480f
PA
13353bkpt_resources_needed (const struct bp_location *bl)
13354{
13355 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13356
13357 return 1;
13358}
13359
2060206e 13360static enum print_stop_action
348d480f
PA
13361bkpt_print_it (bpstat bs)
13362{
348d480f
PA
13363 struct breakpoint *b;
13364 const struct bp_location *bl;
001c8c33 13365 int bp_temp;
79a45e25 13366 struct ui_out *uiout = current_uiout;
348d480f
PA
13367
13368 gdb_assert (bs->bp_location_at != NULL);
13369
13370 bl = bs->bp_location_at;
13371 b = bs->breakpoint_at;
13372
001c8c33
PA
13373 bp_temp = b->disposition == disp_del;
13374 if (bl->address != bl->requested_address)
13375 breakpoint_adjustment_warning (bl->requested_address,
13376 bl->address,
13377 b->number, 1);
13378 annotate_breakpoint (b->number);
13379 if (bp_temp)
13380 ui_out_text (uiout, "\nTemporary breakpoint ");
13381 else
13382 ui_out_text (uiout, "\nBreakpoint ");
13383 if (ui_out_is_mi_like_p (uiout))
348d480f 13384 {
001c8c33
PA
13385 ui_out_field_string (uiout, "reason",
13386 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13387 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13388 }
001c8c33
PA
13389 ui_out_field_int (uiout, "bkptno", b->number);
13390 ui_out_text (uiout, ", ");
06edf0c0 13391
001c8c33 13392 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13393}
13394
2060206e 13395static void
06edf0c0
PA
13396bkpt_print_mention (struct breakpoint *b)
13397{
79a45e25 13398 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13399 return;
13400
13401 switch (b->type)
13402 {
13403 case bp_breakpoint:
13404 case bp_gnu_ifunc_resolver:
13405 if (b->disposition == disp_del)
13406 printf_filtered (_("Temporary breakpoint"));
13407 else
13408 printf_filtered (_("Breakpoint"));
13409 printf_filtered (_(" %d"), b->number);
13410 if (b->type == bp_gnu_ifunc_resolver)
13411 printf_filtered (_(" at gnu-indirect-function resolver"));
13412 break;
13413 case bp_hardware_breakpoint:
13414 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13415 break;
e7e0cddf
SS
13416 case bp_dprintf:
13417 printf_filtered (_("Dprintf %d"), b->number);
13418 break;
06edf0c0
PA
13419 }
13420
13421 say_where (b);
13422}
13423
2060206e 13424static void
06edf0c0
PA
13425bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13426{
13427 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13428 fprintf_unfiltered (fp, "tbreak");
13429 else if (tp->type == bp_breakpoint)
13430 fprintf_unfiltered (fp, "break");
13431 else if (tp->type == bp_hardware_breakpoint
13432 && tp->disposition == disp_del)
13433 fprintf_unfiltered (fp, "thbreak");
13434 else if (tp->type == bp_hardware_breakpoint)
13435 fprintf_unfiltered (fp, "hbreak");
13436 else
13437 internal_error (__FILE__, __LINE__,
13438 _("unhandled breakpoint type %d"), (int) tp->type);
13439
2060206e 13440 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13441 print_recreate_thread (tp, fp);
06edf0c0
PA
13442}
13443
983af33b
SDJ
13444static void
13445bkpt_create_sals_from_address (char **arg,
13446 struct linespec_result *canonical,
13447 enum bptype type_wanted,
13448 char *addr_start, char **copy_arg)
13449{
13450 create_sals_from_address_default (arg, canonical, type_wanted,
13451 addr_start, copy_arg);
13452}
13453
13454static void
13455bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13456 struct linespec_result *canonical,
983af33b 13457 char *cond_string,
e7e0cddf 13458 char *extra_string,
983af33b
SDJ
13459 enum bptype type_wanted,
13460 enum bpdisp disposition,
13461 int thread,
13462 int task, int ignore_count,
13463 const struct breakpoint_ops *ops,
13464 int from_tty, int enabled,
44f238bb 13465 int internal, unsigned flags)
983af33b 13466{
023fa29b 13467 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13468 cond_string, extra_string,
13469 type_wanted,
983af33b
SDJ
13470 disposition, thread, task,
13471 ignore_count, ops, from_tty,
44f238bb 13472 enabled, internal, flags);
983af33b
SDJ
13473}
13474
13475static void
13476bkpt_decode_linespec (struct breakpoint *b, char **s,
13477 struct symtabs_and_lines *sals)
13478{
13479 decode_linespec_default (b, s, sals);
13480}
13481
06edf0c0
PA
13482/* Virtual table for internal breakpoints. */
13483
13484static void
13485internal_bkpt_re_set (struct breakpoint *b)
13486{
13487 switch (b->type)
13488 {
13489 /* Delete overlay event and longjmp master breakpoints; they
13490 will be reset later by breakpoint_re_set. */
13491 case bp_overlay_event:
13492 case bp_longjmp_master:
13493 case bp_std_terminate_master:
13494 case bp_exception_master:
13495 delete_breakpoint (b);
13496 break;
13497
13498 /* This breakpoint is special, it's set up when the inferior
13499 starts and we really don't want to touch it. */
13500 case bp_shlib_event:
13501
13502 /* Like bp_shlib_event, this breakpoint type is special. Once
13503 it is set up, we do not want to touch it. */
13504 case bp_thread_event:
13505 break;
13506 }
13507}
13508
13509static void
13510internal_bkpt_check_status (bpstat bs)
13511{
a9b3a50f
PA
13512 if (bs->breakpoint_at->type == bp_shlib_event)
13513 {
13514 /* If requested, stop when the dynamic linker notifies GDB of
13515 events. This allows the user to get control and place
13516 breakpoints in initializer routines for dynamically loaded
13517 objects (among other things). */
13518 bs->stop = stop_on_solib_events;
13519 bs->print = stop_on_solib_events;
13520 }
13521 else
13522 bs->stop = 0;
06edf0c0
PA
13523}
13524
13525static enum print_stop_action
13526internal_bkpt_print_it (bpstat bs)
13527{
06edf0c0 13528 struct breakpoint *b;
06edf0c0 13529
06edf0c0
PA
13530 b = bs->breakpoint_at;
13531
06edf0c0
PA
13532 switch (b->type)
13533 {
348d480f
PA
13534 case bp_shlib_event:
13535 /* Did we stop because the user set the stop_on_solib_events
13536 variable? (If so, we report this as a generic, "Stopped due
13537 to shlib event" message.) */
edcc5120 13538 print_solib_event (0);
348d480f
PA
13539 break;
13540
13541 case bp_thread_event:
13542 /* Not sure how we will get here.
13543 GDB should not stop for these breakpoints. */
13544 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13545 break;
13546
13547 case bp_overlay_event:
13548 /* By analogy with the thread event, GDB should not stop for these. */
13549 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13550 break;
13551
13552 case bp_longjmp_master:
13553 /* These should never be enabled. */
13554 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13555 break;
13556
13557 case bp_std_terminate_master:
13558 /* These should never be enabled. */
13559 printf_filtered (_("std::terminate Master Breakpoint: "
13560 "gdb should not stop!\n"));
348d480f
PA
13561 break;
13562
13563 case bp_exception_master:
13564 /* These should never be enabled. */
13565 printf_filtered (_("Exception Master Breakpoint: "
13566 "gdb should not stop!\n"));
06edf0c0
PA
13567 break;
13568 }
13569
001c8c33 13570 return PRINT_NOTHING;
06edf0c0
PA
13571}
13572
13573static void
13574internal_bkpt_print_mention (struct breakpoint *b)
13575{
13576 /* Nothing to mention. These breakpoints are internal. */
13577}
13578
06edf0c0
PA
13579/* Virtual table for momentary breakpoints */
13580
13581static void
13582momentary_bkpt_re_set (struct breakpoint *b)
13583{
13584 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13585 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13586 Otherwise these should have been blown away via the cleanup chain
13587 or by breakpoint_init_inferior when we rerun the executable. */
13588}
13589
13590static void
13591momentary_bkpt_check_status (bpstat bs)
13592{
13593 /* Nothing. The point of these breakpoints is causing a stop. */
13594}
13595
13596static enum print_stop_action
13597momentary_bkpt_print_it (bpstat bs)
13598{
79a45e25
PA
13599 struct ui_out *uiout = current_uiout;
13600
001c8c33 13601 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13602 {
001c8c33 13603 struct breakpoint *b = bs->breakpoint_at;
348d480f 13604
001c8c33
PA
13605 switch (b->type)
13606 {
13607 case bp_finish:
13608 ui_out_field_string
13609 (uiout, "reason",
13610 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13611 break;
348d480f 13612
001c8c33
PA
13613 case bp_until:
13614 ui_out_field_string
13615 (uiout, "reason",
13616 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13617 break;
13618 }
348d480f
PA
13619 }
13620
001c8c33 13621 return PRINT_UNKNOWN;
348d480f
PA
13622}
13623
06edf0c0
PA
13624static void
13625momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13626{
06edf0c0 13627 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13628}
13629
e2e4d78b
JK
13630/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13631
13632 It gets cleared already on the removal of the first one of such placed
13633 breakpoints. This is OK as they get all removed altogether. */
13634
13635static void
13636longjmp_bkpt_dtor (struct breakpoint *self)
13637{
13638 struct thread_info *tp = find_thread_id (self->thread);
13639
13640 if (tp)
13641 tp->initiating_frame = null_frame_id;
13642
13643 momentary_breakpoint_ops.dtor (self);
13644}
13645
55aa24fb
SDJ
13646/* Specific methods for probe breakpoints. */
13647
13648static int
13649bkpt_probe_insert_location (struct bp_location *bl)
13650{
13651 int v = bkpt_insert_location (bl);
13652
13653 if (v == 0)
13654 {
13655 /* The insertion was successful, now let's set the probe's semaphore
13656 if needed. */
0ea5cda8
SDJ
13657 if (bl->probe.probe->pops->set_semaphore != NULL)
13658 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13659 bl->probe.objfile,
13660 bl->gdbarch);
55aa24fb
SDJ
13661 }
13662
13663 return v;
13664}
13665
13666static int
13667bkpt_probe_remove_location (struct bp_location *bl)
13668{
13669 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13670 if (bl->probe.probe->pops->clear_semaphore != NULL)
13671 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13672 bl->probe.objfile,
13673 bl->gdbarch);
55aa24fb
SDJ
13674
13675 return bkpt_remove_location (bl);
13676}
13677
13678static void
13679bkpt_probe_create_sals_from_address (char **arg,
13680 struct linespec_result *canonical,
13681 enum bptype type_wanted,
13682 char *addr_start, char **copy_arg)
13683{
13684 struct linespec_sals lsal;
13685
13686 lsal.sals = parse_probes (arg, canonical);
13687
13688 *copy_arg = xstrdup (canonical->addr_string);
13689 lsal.canonical = xstrdup (*copy_arg);
13690
13691 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13692}
13693
13694static void
13695bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13696 struct symtabs_and_lines *sals)
13697{
13698 *sals = parse_probes (s, NULL);
13699 if (!sals->sals)
13700 error (_("probe not found"));
13701}
13702
348d480f 13703/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13704
348d480f
PA
13705static void
13706tracepoint_re_set (struct breakpoint *b)
13707{
13708 breakpoint_re_set_default (b);
13709}
876fa593 13710
348d480f
PA
13711static int
13712tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13713 struct address_space *aspace, CORE_ADDR bp_addr,
13714 const struct target_waitstatus *ws)
348d480f
PA
13715{
13716 /* By definition, the inferior does not report stops at
13717 tracepoints. */
13718 return 0;
74960c60
VP
13719}
13720
13721static void
348d480f
PA
13722tracepoint_print_one_detail (const struct breakpoint *self,
13723 struct ui_out *uiout)
74960c60 13724{
d9b3f62e
PA
13725 struct tracepoint *tp = (struct tracepoint *) self;
13726 if (tp->static_trace_marker_id)
348d480f
PA
13727 {
13728 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13729
348d480f
PA
13730 ui_out_text (uiout, "\tmarker id is ");
13731 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13732 tp->static_trace_marker_id);
348d480f
PA
13733 ui_out_text (uiout, "\n");
13734 }
0d381245
VP
13735}
13736
a474d7c2 13737static void
348d480f 13738tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13739{
79a45e25 13740 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13741 return;
cc59ec59 13742
348d480f
PA
13743 switch (b->type)
13744 {
13745 case bp_tracepoint:
13746 printf_filtered (_("Tracepoint"));
13747 printf_filtered (_(" %d"), b->number);
13748 break;
13749 case bp_fast_tracepoint:
13750 printf_filtered (_("Fast tracepoint"));
13751 printf_filtered (_(" %d"), b->number);
13752 break;
13753 case bp_static_tracepoint:
13754 printf_filtered (_("Static tracepoint"));
13755 printf_filtered (_(" %d"), b->number);
13756 break;
13757 default:
13758 internal_error (__FILE__, __LINE__,
13759 _("unhandled tracepoint type %d"), (int) b->type);
13760 }
13761
13762 say_where (b);
a474d7c2
PA
13763}
13764
348d480f 13765static void
d9b3f62e 13766tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13767{
d9b3f62e
PA
13768 struct tracepoint *tp = (struct tracepoint *) self;
13769
13770 if (self->type == bp_fast_tracepoint)
348d480f 13771 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13772 if (self->type == bp_static_tracepoint)
348d480f 13773 fprintf_unfiltered (fp, "strace");
d9b3f62e 13774 else if (self->type == bp_tracepoint)
348d480f
PA
13775 fprintf_unfiltered (fp, "trace");
13776 else
13777 internal_error (__FILE__, __LINE__,
d9b3f62e 13778 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13779
d9b3f62e
PA
13780 fprintf_unfiltered (fp, " %s", self->addr_string);
13781 print_recreate_thread (self, fp);
13782
13783 if (tp->pass_count)
13784 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13785}
13786
983af33b
SDJ
13787static void
13788tracepoint_create_sals_from_address (char **arg,
13789 struct linespec_result *canonical,
13790 enum bptype type_wanted,
13791 char *addr_start, char **copy_arg)
13792{
13793 create_sals_from_address_default (arg, canonical, type_wanted,
13794 addr_start, copy_arg);
13795}
13796
13797static void
13798tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13799 struct linespec_result *canonical,
983af33b 13800 char *cond_string,
e7e0cddf 13801 char *extra_string,
983af33b
SDJ
13802 enum bptype type_wanted,
13803 enum bpdisp disposition,
13804 int thread,
13805 int task, int ignore_count,
13806 const struct breakpoint_ops *ops,
13807 int from_tty, int enabled,
44f238bb 13808 int internal, unsigned flags)
983af33b 13809{
023fa29b 13810 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13811 cond_string, extra_string,
13812 type_wanted,
983af33b
SDJ
13813 disposition, thread, task,
13814 ignore_count, ops, from_tty,
44f238bb 13815 enabled, internal, flags);
983af33b
SDJ
13816}
13817
13818static void
13819tracepoint_decode_linespec (struct breakpoint *b, char **s,
13820 struct symtabs_and_lines *sals)
13821{
13822 decode_linespec_default (b, s, sals);
13823}
13824
2060206e 13825struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13826
55aa24fb
SDJ
13827/* The breakpoint_ops structure to be use on tracepoints placed in a
13828 static probe. */
13829
13830static void
13831tracepoint_probe_create_sals_from_address (char **arg,
13832 struct linespec_result *canonical,
13833 enum bptype type_wanted,
13834 char *addr_start, char **copy_arg)
13835{
13836 /* We use the same method for breakpoint on probes. */
13837 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13838 addr_start, copy_arg);
13839}
13840
13841static void
13842tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13843 struct symtabs_and_lines *sals)
13844{
13845 /* We use the same method for breakpoint on probes. */
13846 bkpt_probe_decode_linespec (b, s, sals);
13847}
13848
13849static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13850
5c2b4418
HZ
13851/* Dprintf breakpoint_ops methods. */
13852
13853static void
13854dprintf_re_set (struct breakpoint *b)
13855{
13856 breakpoint_re_set_default (b);
13857
13858 /* This breakpoint could have been pending, and be resolved now, and
13859 if so, we should now have the extra string. If we don't, the
13860 dprintf was malformed when created, but we couldn't tell because
13861 we can't extract the extra string until the location is
13862 resolved. */
13863 if (b->loc != NULL && b->extra_string == NULL)
13864 error (_("Format string required"));
13865
13866 /* 1 - connect to target 1, that can run breakpoint commands.
13867 2 - create a dprintf, which resolves fine.
13868 3 - disconnect from target 1
13869 4 - connect to target 2, that can NOT run breakpoint commands.
13870
13871 After steps #3/#4, you'll want the dprintf command list to
13872 be updated, because target 1 and 2 may well return different
13873 answers for target_can_run_breakpoint_commands().
13874 Given absence of finer grained resetting, we get to do
13875 it all the time. */
13876 if (b->extra_string != NULL)
13877 update_dprintf_command_list (b);
13878}
13879
2d9442cc
HZ
13880/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13881
13882static void
13883dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13884{
13885 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13886 tp->extra_string);
13887 print_recreate_thread (tp, fp);
13888}
13889
9d6e6e84
HZ
13890/* Implement the "after_condition_true" breakpoint_ops method for
13891 dprintf.
13892
13893 dprintf's are implemented with regular commands in their command
13894 list, but we run the commands here instead of before presenting the
13895 stop to the user, as dprintf's don't actually cause a stop. This
13896 also makes it so that the commands of multiple dprintfs at the same
13897 address are all handled. */
13898
13899static void
13900dprintf_after_condition_true (struct bpstats *bs)
13901{
13902 struct cleanup *old_chain;
13903 struct bpstats tmp_bs = { NULL };
13904 struct bpstats *tmp_bs_p = &tmp_bs;
13905
13906 /* dprintf's never cause a stop. This wasn't set in the
13907 check_status hook instead because that would make the dprintf's
13908 condition not be evaluated. */
13909 bs->stop = 0;
13910
13911 /* Run the command list here. Take ownership of it instead of
13912 copying. We never want these commands to run later in
13913 bpstat_do_actions, if a breakpoint that causes a stop happens to
13914 be set at same address as this dprintf, or even if running the
13915 commands here throws. */
13916 tmp_bs.commands = bs->commands;
13917 bs->commands = NULL;
13918 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13919
13920 bpstat_do_actions_1 (&tmp_bs_p);
13921
13922 /* 'tmp_bs.commands' will usually be NULL by now, but
13923 bpstat_do_actions_1 may return early without processing the whole
13924 list. */
13925 do_cleanups (old_chain);
13926}
13927
983af33b
SDJ
13928/* The breakpoint_ops structure to be used on static tracepoints with
13929 markers (`-m'). */
13930
13931static void
13932strace_marker_create_sals_from_address (char **arg,
13933 struct linespec_result *canonical,
13934 enum bptype type_wanted,
13935 char *addr_start, char **copy_arg)
13936{
13937 struct linespec_sals lsal;
13938
13939 lsal.sals = decode_static_tracepoint_spec (arg);
13940
13941 *copy_arg = savestring (addr_start, *arg - addr_start);
13942
13943 canonical->addr_string = xstrdup (*copy_arg);
13944 lsal.canonical = xstrdup (*copy_arg);
13945 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13946}
13947
13948static void
13949strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13950 struct linespec_result *canonical,
983af33b 13951 char *cond_string,
e7e0cddf 13952 char *extra_string,
983af33b
SDJ
13953 enum bptype type_wanted,
13954 enum bpdisp disposition,
13955 int thread,
13956 int task, int ignore_count,
13957 const struct breakpoint_ops *ops,
13958 int from_tty, int enabled,
44f238bb 13959 int internal, unsigned flags)
983af33b
SDJ
13960{
13961 int i;
52d361e1
YQ
13962 struct linespec_sals *lsal = VEC_index (linespec_sals,
13963 canonical->sals, 0);
983af33b
SDJ
13964
13965 /* If the user is creating a static tracepoint by marker id
13966 (strace -m MARKER_ID), then store the sals index, so that
13967 breakpoint_re_set can try to match up which of the newly
13968 found markers corresponds to this one, and, don't try to
13969 expand multiple locations for each sal, given than SALS
13970 already should contain all sals for MARKER_ID. */
13971
13972 for (i = 0; i < lsal->sals.nelts; ++i)
13973 {
13974 struct symtabs_and_lines expanded;
13975 struct tracepoint *tp;
13976 struct cleanup *old_chain;
13977 char *addr_string;
13978
13979 expanded.nelts = 1;
13980 expanded.sals = &lsal->sals.sals[i];
13981
13982 addr_string = xstrdup (canonical->addr_string);
13983 old_chain = make_cleanup (xfree, addr_string);
13984
13985 tp = XCNEW (struct tracepoint);
13986 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13987 addr_string, NULL,
e7e0cddf
SS
13988 cond_string, extra_string,
13989 type_wanted, disposition,
983af33b 13990 thread, task, ignore_count, ops,
44f238bb 13991 from_tty, enabled, internal, flags,
983af33b
SDJ
13992 canonical->special_display);
13993 /* Given that its possible to have multiple markers with
13994 the same string id, if the user is creating a static
13995 tracepoint by marker id ("strace -m MARKER_ID"), then
13996 store the sals index, so that breakpoint_re_set can
13997 try to match up which of the newly found markers
13998 corresponds to this one */
13999 tp->static_trace_marker_id_idx = i;
14000
14001 install_breakpoint (internal, &tp->base, 0);
14002
14003 discard_cleanups (old_chain);
14004 }
14005}
14006
14007static void
14008strace_marker_decode_linespec (struct breakpoint *b, char **s,
14009 struct symtabs_and_lines *sals)
14010{
14011 struct tracepoint *tp = (struct tracepoint *) b;
14012
14013 *sals = decode_static_tracepoint_spec (s);
14014 if (sals->nelts > tp->static_trace_marker_id_idx)
14015 {
14016 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14017 sals->nelts = 1;
14018 }
14019 else
14020 error (_("marker %s not found"), tp->static_trace_marker_id);
14021}
14022
14023static struct breakpoint_ops strace_marker_breakpoint_ops;
14024
14025static int
14026strace_marker_p (struct breakpoint *b)
14027{
14028 return b->ops == &strace_marker_breakpoint_ops;
14029}
14030
53a5351d 14031/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14032 structures. */
c906108c
SS
14033
14034void
fba45db2 14035delete_breakpoint (struct breakpoint *bpt)
c906108c 14036{
52f0bd74 14037 struct breakpoint *b;
c906108c 14038
8a3fe4f8 14039 gdb_assert (bpt != NULL);
c906108c 14040
4a64f543
MS
14041 /* Has this bp already been deleted? This can happen because
14042 multiple lists can hold pointers to bp's. bpstat lists are
14043 especial culprits.
14044
14045 One example of this happening is a watchpoint's scope bp. When
14046 the scope bp triggers, we notice that the watchpoint is out of
14047 scope, and delete it. We also delete its scope bp. But the
14048 scope bp is marked "auto-deleting", and is already on a bpstat.
14049 That bpstat is then checked for auto-deleting bp's, which are
14050 deleted.
14051
14052 A real solution to this problem might involve reference counts in
14053 bp's, and/or giving them pointers back to their referencing
14054 bpstat's, and teaching delete_breakpoint to only free a bp's
14055 storage when no more references were extent. A cheaper bandaid
14056 was chosen. */
c906108c
SS
14057 if (bpt->type == bp_none)
14058 return;
14059
4a64f543
MS
14060 /* At least avoid this stale reference until the reference counting
14061 of breakpoints gets resolved. */
d0fb5eae 14062 if (bpt->related_breakpoint != bpt)
e5a0a904 14063 {
d0fb5eae 14064 struct breakpoint *related;
3a5c3e22 14065 struct watchpoint *w;
d0fb5eae
JK
14066
14067 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14068 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14069 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14070 w = (struct watchpoint *) bpt;
14071 else
14072 w = NULL;
14073 if (w != NULL)
14074 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14075
14076 /* Unlink bpt from the bpt->related_breakpoint ring. */
14077 for (related = bpt; related->related_breakpoint != bpt;
14078 related = related->related_breakpoint);
14079 related->related_breakpoint = bpt->related_breakpoint;
14080 bpt->related_breakpoint = bpt;
e5a0a904
JK
14081 }
14082
a9634178
TJB
14083 /* watch_command_1 creates a watchpoint but only sets its number if
14084 update_watchpoint succeeds in creating its bp_locations. If there's
14085 a problem in that process, we'll be asked to delete the half-created
14086 watchpoint. In that case, don't announce the deletion. */
14087 if (bpt->number)
14088 observer_notify_breakpoint_deleted (bpt);
c906108c 14089
c906108c
SS
14090 if (breakpoint_chain == bpt)
14091 breakpoint_chain = bpt->next;
14092
c906108c
SS
14093 ALL_BREAKPOINTS (b)
14094 if (b->next == bpt)
c5aa993b
JM
14095 {
14096 b->next = bpt->next;
14097 break;
14098 }
c906108c 14099
f431efe5
PA
14100 /* Be sure no bpstat's are pointing at the breakpoint after it's
14101 been freed. */
14102 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14103 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14104 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14105 commands are associated with the bpstat; if we remove it here,
14106 then the later call to bpstat_do_actions (&stop_bpstat); in
14107 event-top.c won't do anything, and temporary breakpoints with
14108 commands won't work. */
14109
14110 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14111
4a64f543
MS
14112 /* Now that breakpoint is removed from breakpoint list, update the
14113 global location list. This will remove locations that used to
14114 belong to this breakpoint. Do this before freeing the breakpoint
14115 itself, since remove_breakpoint looks at location's owner. It
14116 might be better design to have location completely
14117 self-contained, but it's not the case now. */
44702360 14118 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14119
348d480f 14120 bpt->ops->dtor (bpt);
4a64f543
MS
14121 /* On the chance that someone will soon try again to delete this
14122 same bp, we mark it as deleted before freeing its storage. */
c906108c 14123 bpt->type = bp_none;
b8c9b27d 14124 xfree (bpt);
c906108c
SS
14125}
14126
4d6140d9
AC
14127static void
14128do_delete_breakpoint_cleanup (void *b)
14129{
14130 delete_breakpoint (b);
14131}
14132
14133struct cleanup *
14134make_cleanup_delete_breakpoint (struct breakpoint *b)
14135{
14136 return make_cleanup (do_delete_breakpoint_cleanup, b);
14137}
14138
51be5b68
PA
14139/* Iterator function to call a user-provided callback function once
14140 for each of B and its related breakpoints. */
14141
14142static void
14143iterate_over_related_breakpoints (struct breakpoint *b,
14144 void (*function) (struct breakpoint *,
14145 void *),
14146 void *data)
14147{
14148 struct breakpoint *related;
14149
14150 related = b;
14151 do
14152 {
14153 struct breakpoint *next;
14154
14155 /* FUNCTION may delete RELATED. */
14156 next = related->related_breakpoint;
14157
14158 if (next == related)
14159 {
14160 /* RELATED is the last ring entry. */
14161 function (related, data);
14162
14163 /* FUNCTION may have deleted it, so we'd never reach back to
14164 B. There's nothing left to do anyway, so just break
14165 out. */
14166 break;
14167 }
14168 else
14169 function (related, data);
14170
14171 related = next;
14172 }
14173 while (related != b);
14174}
95a42b64
TT
14175
14176static void
14177do_delete_breakpoint (struct breakpoint *b, void *ignore)
14178{
14179 delete_breakpoint (b);
14180}
14181
51be5b68
PA
14182/* A callback for map_breakpoint_numbers that calls
14183 delete_breakpoint. */
14184
14185static void
14186do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14187{
14188 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14189}
14190
c906108c 14191void
fba45db2 14192delete_command (char *arg, int from_tty)
c906108c 14193{
35df4500 14194 struct breakpoint *b, *b_tmp;
c906108c 14195
ea9365bb
TT
14196 dont_repeat ();
14197
c906108c
SS
14198 if (arg == 0)
14199 {
14200 int breaks_to_delete = 0;
14201
46c6471b
PA
14202 /* Delete all breakpoints if no argument. Do not delete
14203 internal breakpoints, these have to be deleted with an
14204 explicit breakpoint number argument. */
c5aa993b 14205 ALL_BREAKPOINTS (b)
46c6471b 14206 if (user_breakpoint_p (b))
973d738b
DJ
14207 {
14208 breaks_to_delete = 1;
14209 break;
14210 }
c906108c
SS
14211
14212 /* Ask user only if there are some breakpoints to delete. */
14213 if (!from_tty
e2e0b3e5 14214 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14215 {
35df4500 14216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14217 if (user_breakpoint_p (b))
c5aa993b 14218 delete_breakpoint (b);
c906108c
SS
14219 }
14220 }
14221 else
51be5b68 14222 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14223}
14224
0d381245
VP
14225static int
14226all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14227{
0d381245 14228 for (; loc; loc = loc->next)
8645ff69
UW
14229 if (!loc->shlib_disabled
14230 && !loc->pspace->executing_startup)
0d381245
VP
14231 return 0;
14232 return 1;
fe3f5fa8
VP
14233}
14234
776592bf
DE
14235/* Subroutine of update_breakpoint_locations to simplify it.
14236 Return non-zero if multiple fns in list LOC have the same name.
14237 Null names are ignored. */
14238
14239static int
14240ambiguous_names_p (struct bp_location *loc)
14241{
14242 struct bp_location *l;
14243 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14244 (int (*) (const void *,
14245 const void *)) streq,
776592bf
DE
14246 NULL, xcalloc, xfree);
14247
14248 for (l = loc; l != NULL; l = l->next)
14249 {
14250 const char **slot;
14251 const char *name = l->function_name;
14252
14253 /* Allow for some names to be NULL, ignore them. */
14254 if (name == NULL)
14255 continue;
14256
14257 slot = (const char **) htab_find_slot (htab, (const void *) name,
14258 INSERT);
4a64f543
MS
14259 /* NOTE: We can assume slot != NULL here because xcalloc never
14260 returns NULL. */
776592bf
DE
14261 if (*slot != NULL)
14262 {
14263 htab_delete (htab);
14264 return 1;
14265 }
14266 *slot = name;
14267 }
14268
14269 htab_delete (htab);
14270 return 0;
14271}
14272
0fb4aa4b
PA
14273/* When symbols change, it probably means the sources changed as well,
14274 and it might mean the static tracepoint markers are no longer at
14275 the same address or line numbers they used to be at last we
14276 checked. Losing your static tracepoints whenever you rebuild is
14277 undesirable. This function tries to resync/rematch gdb static
14278 tracepoints with the markers on the target, for static tracepoints
14279 that have not been set by marker id. Static tracepoint that have
14280 been set by marker id are reset by marker id in breakpoint_re_set.
14281 The heuristic is:
14282
14283 1) For a tracepoint set at a specific address, look for a marker at
14284 the old PC. If one is found there, assume to be the same marker.
14285 If the name / string id of the marker found is different from the
14286 previous known name, assume that means the user renamed the marker
14287 in the sources, and output a warning.
14288
14289 2) For a tracepoint set at a given line number, look for a marker
14290 at the new address of the old line number. If one is found there,
14291 assume to be the same marker. If the name / string id of the
14292 marker found is different from the previous known name, assume that
14293 means the user renamed the marker in the sources, and output a
14294 warning.
14295
14296 3) If a marker is no longer found at the same address or line, it
14297 may mean the marker no longer exists. But it may also just mean
14298 the code changed a bit. Maybe the user added a few lines of code
14299 that made the marker move up or down (in line number terms). Ask
14300 the target for info about the marker with the string id as we knew
14301 it. If found, update line number and address in the matching
14302 static tracepoint. This will get confused if there's more than one
14303 marker with the same ID (possible in UST, although unadvised
14304 precisely because it confuses tools). */
14305
14306static struct symtab_and_line
14307update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14308{
d9b3f62e 14309 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14310 struct static_tracepoint_marker marker;
14311 CORE_ADDR pc;
0fb4aa4b
PA
14312
14313 pc = sal.pc;
14314 if (sal.line)
14315 find_line_pc (sal.symtab, sal.line, &pc);
14316
14317 if (target_static_tracepoint_marker_at (pc, &marker))
14318 {
d9b3f62e 14319 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14320 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14321 b->number,
d9b3f62e 14322 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14323
d9b3f62e
PA
14324 xfree (tp->static_trace_marker_id);
14325 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14326 release_static_tracepoint_marker (&marker);
14327
14328 return sal;
14329 }
14330
14331 /* Old marker wasn't found on target at lineno. Try looking it up
14332 by string ID. */
14333 if (!sal.explicit_pc
14334 && sal.line != 0
14335 && sal.symtab != NULL
d9b3f62e 14336 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14337 {
14338 VEC(static_tracepoint_marker_p) *markers;
14339
14340 markers
d9b3f62e 14341 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14342
14343 if (!VEC_empty(static_tracepoint_marker_p, markers))
14344 {
80e1d417 14345 struct symtab_and_line sal2;
0fb4aa4b 14346 struct symbol *sym;
80e1d417 14347 struct static_tracepoint_marker *tpmarker;
79a45e25 14348 struct ui_out *uiout = current_uiout;
0fb4aa4b 14349
80e1d417 14350 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14351
d9b3f62e 14352 xfree (tp->static_trace_marker_id);
80e1d417 14353 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14354
14355 warning (_("marker for static tracepoint %d (%s) not "
14356 "found at previous line number"),
d9b3f62e 14357 b->number, tp->static_trace_marker_id);
0fb4aa4b 14358
80e1d417 14359 init_sal (&sal2);
0fb4aa4b 14360
80e1d417 14361 sal2.pc = tpmarker->address;
0fb4aa4b 14362
80e1d417
AS
14363 sal2 = find_pc_line (tpmarker->address, 0);
14364 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14365 ui_out_text (uiout, "Now in ");
14366 if (sym)
14367 {
14368 ui_out_field_string (uiout, "func",
14369 SYMBOL_PRINT_NAME (sym));
14370 ui_out_text (uiout, " at ");
14371 }
05cba821
JK
14372 ui_out_field_string (uiout, "file",
14373 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14374 ui_out_text (uiout, ":");
14375
14376 if (ui_out_is_mi_like_p (uiout))
14377 {
0b0865da 14378 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14379
f35a17b5 14380 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14381 }
14382
80e1d417 14383 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14384 ui_out_text (uiout, "\n");
14385
80e1d417 14386 b->loc->line_number = sal2.line;
2f202fde 14387 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14388
14389 xfree (b->addr_string);
14390 b->addr_string = xstrprintf ("%s:%d",
05cba821 14391 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14392 b->loc->line_number);
0fb4aa4b
PA
14393
14394 /* Might be nice to check if function changed, and warn if
14395 so. */
14396
80e1d417 14397 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14398 }
14399 }
14400 return sal;
14401}
14402
8d3788bd
VP
14403/* Returns 1 iff locations A and B are sufficiently same that
14404 we don't need to report breakpoint as changed. */
14405
14406static int
14407locations_are_equal (struct bp_location *a, struct bp_location *b)
14408{
14409 while (a && b)
14410 {
14411 if (a->address != b->address)
14412 return 0;
14413
14414 if (a->shlib_disabled != b->shlib_disabled)
14415 return 0;
14416
14417 if (a->enabled != b->enabled)
14418 return 0;
14419
14420 a = a->next;
14421 b = b->next;
14422 }
14423
14424 if ((a == NULL) != (b == NULL))
14425 return 0;
14426
14427 return 1;
14428}
14429
f1310107
TJB
14430/* Create new breakpoint locations for B (a hardware or software breakpoint)
14431 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14432 a ranged breakpoint. */
14433
0e30163f 14434void
0d381245 14435update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14436 struct symtabs_and_lines sals,
14437 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14438{
14439 int i;
0d381245
VP
14440 struct bp_location *existing_locations = b->loc;
14441
f8eba3c6
TT
14442 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14443 {
14444 /* Ranged breakpoints have only one start location and one end
14445 location. */
14446 b->enable_state = bp_disabled;
44702360 14447 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14448 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14449 "multiple locations found\n"),
14450 b->number);
14451 return;
14452 }
f1310107 14453
4a64f543
MS
14454 /* If there's no new locations, and all existing locations are
14455 pending, don't do anything. This optimizes the common case where
14456 all locations are in the same shared library, that was unloaded.
14457 We'd like to retain the location, so that when the library is
14458 loaded again, we don't loose the enabled/disabled status of the
14459 individual locations. */
0d381245 14460 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14461 return;
14462
fe3f5fa8
VP
14463 b->loc = NULL;
14464
0d381245 14465 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14466 {
f8eba3c6
TT
14467 struct bp_location *new_loc;
14468
14469 switch_to_program_space_and_thread (sals.sals[i].pspace);
14470
14471 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14472
0d381245
VP
14473 /* Reparse conditions, they might contain references to the
14474 old symtab. */
14475 if (b->cond_string != NULL)
14476 {
bbc13ae3 14477 const char *s;
bfd189b1 14478 volatile struct gdb_exception e;
fe3f5fa8 14479
0d381245
VP
14480 s = b->cond_string;
14481 TRY_CATCH (e, RETURN_MASK_ERROR)
14482 {
1bb9788d
TT
14483 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14484 block_for_pc (sals.sals[i].pc),
0d381245
VP
14485 0);
14486 }
14487 if (e.reason < 0)
14488 {
3e43a32a
MS
14489 warning (_("failed to reevaluate condition "
14490 "for breakpoint %d: %s"),
0d381245
VP
14491 b->number, e.message);
14492 new_loc->enabled = 0;
14493 }
14494 }
fe3f5fa8 14495
f1310107
TJB
14496 if (sals_end.nelts)
14497 {
14498 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14499
14500 new_loc->length = end - sals.sals[0].pc + 1;
14501 }
0d381245 14502 }
fe3f5fa8 14503
514f746b
AR
14504 /* Update locations of permanent breakpoints. */
14505 if (b->enable_state == bp_permanent)
14506 make_breakpoint_permanent (b);
14507
4a64f543
MS
14508 /* If possible, carry over 'disable' status from existing
14509 breakpoints. */
0d381245
VP
14510 {
14511 struct bp_location *e = existing_locations;
776592bf
DE
14512 /* If there are multiple breakpoints with the same function name,
14513 e.g. for inline functions, comparing function names won't work.
14514 Instead compare pc addresses; this is just a heuristic as things
14515 may have moved, but in practice it gives the correct answer
14516 often enough until a better solution is found. */
14517 int have_ambiguous_names = ambiguous_names_p (b->loc);
14518
0d381245
VP
14519 for (; e; e = e->next)
14520 {
14521 if (!e->enabled && e->function_name)
14522 {
14523 struct bp_location *l = b->loc;
776592bf
DE
14524 if (have_ambiguous_names)
14525 {
14526 for (; l; l = l->next)
f1310107 14527 if (breakpoint_locations_match (e, l))
776592bf
DE
14528 {
14529 l->enabled = 0;
14530 break;
14531 }
14532 }
14533 else
14534 {
14535 for (; l; l = l->next)
14536 if (l->function_name
14537 && strcmp (e->function_name, l->function_name) == 0)
14538 {
14539 l->enabled = 0;
14540 break;
14541 }
14542 }
0d381245
VP
14543 }
14544 }
14545 }
fe3f5fa8 14546
8d3788bd
VP
14547 if (!locations_are_equal (existing_locations, b->loc))
14548 observer_notify_breakpoint_modified (b);
14549
44702360 14550 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14551}
14552
ef23e705
TJB
14553/* Find the SaL locations corresponding to the given ADDR_STRING.
14554 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14555
14556static struct symtabs_and_lines
14557addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14558{
14559 char *s;
02d20e4a 14560 struct symtabs_and_lines sals = {0};
f8eba3c6 14561 volatile struct gdb_exception e;
ef23e705 14562
983af33b 14563 gdb_assert (b->ops != NULL);
ef23e705 14564 s = addr_string;
ef23e705
TJB
14565
14566 TRY_CATCH (e, RETURN_MASK_ERROR)
14567 {
983af33b 14568 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14569 }
14570 if (e.reason < 0)
14571 {
14572 int not_found_and_ok = 0;
14573 /* For pending breakpoints, it's expected that parsing will
14574 fail until the right shared library is loaded. User has
14575 already told to create pending breakpoints and don't need
14576 extra messages. If breakpoint is in bp_shlib_disabled
14577 state, then user already saw the message about that
14578 breakpoint being disabled, and don't want to see more
14579 errors. */
58438ac1 14580 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14581 && (b->condition_not_parsed
14582 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14583 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14584 || b->enable_state == bp_disabled))
14585 not_found_and_ok = 1;
14586
14587 if (!not_found_and_ok)
14588 {
14589 /* We surely don't want to warn about the same breakpoint
14590 10 times. One solution, implemented here, is disable
14591 the breakpoint on error. Another solution would be to
14592 have separate 'warning emitted' flag. Since this
14593 happens only when a binary has changed, I don't know
14594 which approach is better. */
14595 b->enable_state = bp_disabled;
14596 throw_exception (e);
14597 }
14598 }
14599
58438ac1 14600 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14601 {
f8eba3c6 14602 int i;
ef23e705 14603
f8eba3c6
TT
14604 for (i = 0; i < sals.nelts; ++i)
14605 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14606 if (b->condition_not_parsed && s && s[0])
14607 {
ed1d1739
KS
14608 char *cond_string, *extra_string;
14609 int thread, task;
ef23e705
TJB
14610
14611 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14612 &cond_string, &thread, &task,
14613 &extra_string);
ef23e705
TJB
14614 if (cond_string)
14615 b->cond_string = cond_string;
14616 b->thread = thread;
14617 b->task = task;
e7e0cddf
SS
14618 if (extra_string)
14619 b->extra_string = extra_string;
ef23e705
TJB
14620 b->condition_not_parsed = 0;
14621 }
14622
983af33b 14623 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14624 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14625
58438ac1
TT
14626 *found = 1;
14627 }
14628 else
14629 *found = 0;
ef23e705
TJB
14630
14631 return sals;
14632}
14633
348d480f
PA
14634/* The default re_set method, for typical hardware or software
14635 breakpoints. Reevaluate the breakpoint and recreate its
14636 locations. */
14637
14638static void
28010a5d 14639breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14640{
14641 int found;
f1310107 14642 struct symtabs_and_lines sals, sals_end;
ef23e705 14643 struct symtabs_and_lines expanded = {0};
f1310107 14644 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14645
14646 sals = addr_string_to_sals (b, b->addr_string, &found);
14647 if (found)
14648 {
14649 make_cleanup (xfree, sals.sals);
f8eba3c6 14650 expanded = sals;
ef23e705
TJB
14651 }
14652
f1310107
TJB
14653 if (b->addr_string_range_end)
14654 {
14655 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14656 if (found)
14657 {
14658 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14659 expanded_end = sals_end;
f1310107
TJB
14660 }
14661 }
14662
14663 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14664}
14665
983af33b
SDJ
14666/* Default method for creating SALs from an address string. It basically
14667 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14668
14669static void
14670create_sals_from_address_default (char **arg,
14671 struct linespec_result *canonical,
14672 enum bptype type_wanted,
14673 char *addr_start, char **copy_arg)
14674{
14675 parse_breakpoint_sals (arg, canonical);
14676}
14677
14678/* Call create_breakpoints_sal for the given arguments. This is the default
14679 function for the `create_breakpoints_sal' method of
14680 breakpoint_ops. */
14681
14682static void
14683create_breakpoints_sal_default (struct gdbarch *gdbarch,
14684 struct linespec_result *canonical,
983af33b 14685 char *cond_string,
e7e0cddf 14686 char *extra_string,
983af33b
SDJ
14687 enum bptype type_wanted,
14688 enum bpdisp disposition,
14689 int thread,
14690 int task, int ignore_count,
14691 const struct breakpoint_ops *ops,
14692 int from_tty, int enabled,
44f238bb 14693 int internal, unsigned flags)
983af33b
SDJ
14694{
14695 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14696 extra_string,
983af33b
SDJ
14697 type_wanted, disposition,
14698 thread, task, ignore_count, ops, from_tty,
44f238bb 14699 enabled, internal, flags);
983af33b
SDJ
14700}
14701
14702/* Decode the line represented by S by calling decode_line_full. This is the
14703 default function for the `decode_linespec' method of breakpoint_ops. */
14704
14705static void
14706decode_linespec_default (struct breakpoint *b, char **s,
14707 struct symtabs_and_lines *sals)
14708{
14709 struct linespec_result canonical;
14710
14711 init_linespec_result (&canonical);
14712 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14713 (struct symtab *) NULL, 0,
14714 &canonical, multiple_symbols_all,
14715 b->filter);
14716
14717 /* We should get 0 or 1 resulting SALs. */
14718 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14719
14720 if (VEC_length (linespec_sals, canonical.sals) > 0)
14721 {
14722 struct linespec_sals *lsal;
14723
14724 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14725 *sals = lsal->sals;
14726 /* Arrange it so the destructor does not free the
14727 contents. */
14728 lsal->sals.sals = NULL;
14729 }
14730
14731 destroy_linespec_result (&canonical);
14732}
14733
28010a5d
PA
14734/* Prepare the global context for a re-set of breakpoint B. */
14735
14736static struct cleanup *
14737prepare_re_set_context (struct breakpoint *b)
14738{
14739 struct cleanup *cleanups;
14740
14741 input_radix = b->input_radix;
14742 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14743 if (b->pspace != NULL)
14744 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14745 set_language (b->language);
14746
14747 return cleanups;
ef23e705
TJB
14748}
14749
c906108c
SS
14750/* Reset a breakpoint given it's struct breakpoint * BINT.
14751 The value we return ends up being the return value from catch_errors.
14752 Unused in this case. */
14753
14754static int
4efb68b1 14755breakpoint_re_set_one (void *bint)
c906108c 14756{
4a64f543 14757 /* Get past catch_errs. */
53a5351d 14758 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14759 struct cleanup *cleanups;
c906108c 14760
348d480f
PA
14761 cleanups = prepare_re_set_context (b);
14762 b->ops->re_set (b);
14763 do_cleanups (cleanups);
c906108c
SS
14764 return 0;
14765}
14766
69de3c6a 14767/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14768void
69de3c6a 14769breakpoint_re_set (void)
c906108c 14770{
35df4500 14771 struct breakpoint *b, *b_tmp;
c906108c
SS
14772 enum language save_language;
14773 int save_input_radix;
6c95b8df 14774 struct cleanup *old_chain;
c5aa993b 14775
c906108c
SS
14776 save_language = current_language->la_language;
14777 save_input_radix = input_radix;
6c95b8df
PA
14778 old_chain = save_current_program_space ();
14779
35df4500 14780 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14781 {
4a64f543 14782 /* Format possible error msg. */
fe3f5fa8 14783 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14784 b->number);
14785 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14786 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14787 do_cleanups (cleanups);
c5aa993b 14788 }
c906108c
SS
14789 set_language (save_language);
14790 input_radix = save_input_radix;
e62c965a 14791
0756c555 14792 jit_breakpoint_re_set ();
4efc6507 14793
6c95b8df
PA
14794 do_cleanups (old_chain);
14795
af02033e
PP
14796 create_overlay_event_breakpoint ();
14797 create_longjmp_master_breakpoint ();
14798 create_std_terminate_master_breakpoint ();
186c406b 14799 create_exception_master_breakpoint ();
c906108c
SS
14800}
14801\f
c906108c
SS
14802/* Reset the thread number of this breakpoint:
14803
14804 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14805 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14806void
fba45db2 14807breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14808{
14809 if (b->thread != -1)
14810 {
39f77062
KB
14811 if (in_thread_list (inferior_ptid))
14812 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14813
14814 /* We're being called after following a fork. The new fork is
14815 selected as current, and unless this was a vfork will have a
14816 different program space from the original thread. Reset that
14817 as well. */
14818 b->loc->pspace = current_program_space;
c906108c
SS
14819 }
14820}
14821
03ac34d5
MS
14822/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14823 If from_tty is nonzero, it prints a message to that effect,
14824 which ends with a period (no newline). */
14825
c906108c 14826void
fba45db2 14827set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14828{
52f0bd74 14829 struct breakpoint *b;
c906108c
SS
14830
14831 if (count < 0)
14832 count = 0;
14833
14834 ALL_BREAKPOINTS (b)
14835 if (b->number == bptnum)
c5aa993b 14836 {
d77f58be
SS
14837 if (is_tracepoint (b))
14838 {
14839 if (from_tty && count != 0)
14840 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14841 bptnum);
14842 return;
14843 }
14844
c5aa993b 14845 b->ignore_count = count;
221ea385
KS
14846 if (from_tty)
14847 {
14848 if (count == 0)
3e43a32a
MS
14849 printf_filtered (_("Will stop next time "
14850 "breakpoint %d is reached."),
221ea385
KS
14851 bptnum);
14852 else if (count == 1)
a3f17187 14853 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14854 bptnum);
14855 else
3e43a32a
MS
14856 printf_filtered (_("Will ignore next %d "
14857 "crossings of breakpoint %d."),
221ea385
KS
14858 count, bptnum);
14859 }
8d3788bd 14860 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14861 return;
14862 }
c906108c 14863
8a3fe4f8 14864 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14865}
14866
c906108c
SS
14867/* Command to set ignore-count of breakpoint N to COUNT. */
14868
14869static void
fba45db2 14870ignore_command (char *args, int from_tty)
c906108c
SS
14871{
14872 char *p = args;
52f0bd74 14873 int num;
c906108c
SS
14874
14875 if (p == 0)
e2e0b3e5 14876 error_no_arg (_("a breakpoint number"));
c5aa993b 14877
c906108c 14878 num = get_number (&p);
5c44784c 14879 if (num == 0)
8a3fe4f8 14880 error (_("bad breakpoint number: '%s'"), args);
c906108c 14881 if (*p == 0)
8a3fe4f8 14882 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14883
14884 set_ignore_count (num,
14885 longest_to_int (value_as_long (parse_and_eval (p))),
14886 from_tty);
221ea385
KS
14887 if (from_tty)
14888 printf_filtered ("\n");
c906108c
SS
14889}
14890\f
14891/* Call FUNCTION on each of the breakpoints
14892 whose numbers are given in ARGS. */
14893
14894static void
95a42b64
TT
14895map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14896 void *),
14897 void *data)
c906108c 14898{
52f0bd74
AC
14899 int num;
14900 struct breakpoint *b, *tmp;
11cf8741 14901 int match;
197f0a60 14902 struct get_number_or_range_state state;
c906108c 14903
197f0a60 14904 if (args == 0)
e2e0b3e5 14905 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14906
197f0a60
TT
14907 init_number_or_range (&state, args);
14908
14909 while (!state.finished)
c906108c 14910 {
e799154c 14911 const char *p = state.string;
197f0a60 14912
11cf8741 14913 match = 0;
c5aa993b 14914
197f0a60 14915 num = get_number_or_range (&state);
5c44784c 14916 if (num == 0)
c5aa993b 14917 {
8a3fe4f8 14918 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14919 }
14920 else
14921 {
14922 ALL_BREAKPOINTS_SAFE (b, tmp)
14923 if (b->number == num)
14924 {
11cf8741 14925 match = 1;
cdac0397 14926 function (b, data);
11cf8741 14927 break;
5c44784c 14928 }
11cf8741 14929 if (match == 0)
a3f17187 14930 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14931 }
c906108c
SS
14932 }
14933}
14934
0d381245
VP
14935static struct bp_location *
14936find_location_by_number (char *number)
14937{
14938 char *dot = strchr (number, '.');
14939 char *p1;
14940 int bp_num;
14941 int loc_num;
14942 struct breakpoint *b;
14943 struct bp_location *loc;
14944
14945 *dot = '\0';
14946
14947 p1 = number;
197f0a60 14948 bp_num = get_number (&p1);
0d381245
VP
14949 if (bp_num == 0)
14950 error (_("Bad breakpoint number '%s'"), number);
14951
14952 ALL_BREAKPOINTS (b)
14953 if (b->number == bp_num)
14954 {
14955 break;
14956 }
14957
14958 if (!b || b->number != bp_num)
14959 error (_("Bad breakpoint number '%s'"), number);
14960
14961 p1 = dot+1;
197f0a60 14962 loc_num = get_number (&p1);
0d381245
VP
14963 if (loc_num == 0)
14964 error (_("Bad breakpoint location number '%s'"), number);
14965
14966 --loc_num;
14967 loc = b->loc;
14968 for (;loc_num && loc; --loc_num, loc = loc->next)
14969 ;
14970 if (!loc)
14971 error (_("Bad breakpoint location number '%s'"), dot+1);
14972
14973 return loc;
14974}
14975
14976
1900040c
MS
14977/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14978 If from_tty is nonzero, it prints a message to that effect,
14979 which ends with a period (no newline). */
14980
c906108c 14981void
fba45db2 14982disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14983{
14984 /* Never disable a watchpoint scope breakpoint; we want to
14985 hit them when we leave scope so we can delete both the
14986 watchpoint and its scope breakpoint at that time. */
14987 if (bpt->type == bp_watchpoint_scope)
14988 return;
14989
c2c6d25f 14990 /* You can't disable permanent breakpoints. */
b5de0fa7 14991 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14992 return;
14993
b5de0fa7 14994 bpt->enable_state = bp_disabled;
c906108c 14995
b775012e
LM
14996 /* Mark breakpoint locations modified. */
14997 mark_breakpoint_modified (bpt);
14998
d248b706
KY
14999 if (target_supports_enable_disable_tracepoint ()
15000 && current_trace_status ()->running && is_tracepoint (bpt))
15001 {
15002 struct bp_location *location;
15003
15004 for (location = bpt->loc; location; location = location->next)
15005 target_disable_tracepoint (location);
15006 }
15007
44702360 15008 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15009
8d3788bd 15010 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15011}
15012
51be5b68
PA
15013/* A callback for iterate_over_related_breakpoints. */
15014
15015static void
15016do_disable_breakpoint (struct breakpoint *b, void *ignore)
15017{
15018 disable_breakpoint (b);
15019}
15020
95a42b64
TT
15021/* A callback for map_breakpoint_numbers that calls
15022 disable_breakpoint. */
15023
15024static void
15025do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15026{
51be5b68 15027 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15028}
15029
c906108c 15030static void
fba45db2 15031disable_command (char *args, int from_tty)
c906108c 15032{
c906108c 15033 if (args == 0)
46c6471b
PA
15034 {
15035 struct breakpoint *bpt;
15036
15037 ALL_BREAKPOINTS (bpt)
15038 if (user_breakpoint_p (bpt))
15039 disable_breakpoint (bpt);
15040 }
9eaabc75 15041 else
0d381245 15042 {
9eaabc75
MW
15043 char *num = extract_arg (&args);
15044
15045 while (num)
d248b706 15046 {
9eaabc75 15047 if (strchr (num, '.'))
b775012e 15048 {
9eaabc75
MW
15049 struct bp_location *loc = find_location_by_number (num);
15050
15051 if (loc)
15052 {
15053 if (loc->enabled)
15054 {
15055 loc->enabled = 0;
15056 mark_breakpoint_location_modified (loc);
15057 }
15058 if (target_supports_enable_disable_tracepoint ()
15059 && current_trace_status ()->running && loc->owner
15060 && is_tracepoint (loc->owner))
15061 target_disable_tracepoint (loc);
15062 }
44702360 15063 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15064 }
9eaabc75
MW
15065 else
15066 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15067 num = extract_arg (&args);
d248b706 15068 }
0d381245 15069 }
c906108c
SS
15070}
15071
15072static void
816338b5
SS
15073enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15074 int count)
c906108c 15075{
afe38095 15076 int target_resources_ok;
c906108c
SS
15077
15078 if (bpt->type == bp_hardware_breakpoint)
15079 {
15080 int i;
c5aa993b 15081 i = hw_breakpoint_used_count ();
53a5351d 15082 target_resources_ok =
d92524f1 15083 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15084 i + 1, 0);
c906108c 15085 if (target_resources_ok == 0)
8a3fe4f8 15086 error (_("No hardware breakpoint support in the target."));
c906108c 15087 else if (target_resources_ok < 0)
8a3fe4f8 15088 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15089 }
15090
cc60f2e3 15091 if (is_watchpoint (bpt))
c906108c 15092 {
d07205c2
JK
15093 /* Initialize it just to avoid a GCC false warning. */
15094 enum enable_state orig_enable_state = 0;
bfd189b1 15095 volatile struct gdb_exception e;
dde02812
ES
15096
15097 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15098 {
3a5c3e22
PA
15099 struct watchpoint *w = (struct watchpoint *) bpt;
15100
1e718ff1
TJB
15101 orig_enable_state = bpt->enable_state;
15102 bpt->enable_state = bp_enabled;
3a5c3e22 15103 update_watchpoint (w, 1 /* reparse */);
c906108c 15104 }
dde02812 15105 if (e.reason < 0)
c5aa993b 15106 {
1e718ff1 15107 bpt->enable_state = orig_enable_state;
dde02812
ES
15108 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15109 bpt->number);
15110 return;
c5aa993b 15111 }
c906108c 15112 }
0101ce28 15113
b4c291bb
KH
15114 if (bpt->enable_state != bp_permanent)
15115 bpt->enable_state = bp_enabled;
d248b706 15116
b775012e
LM
15117 bpt->enable_state = bp_enabled;
15118
15119 /* Mark breakpoint locations modified. */
15120 mark_breakpoint_modified (bpt);
15121
d248b706
KY
15122 if (target_supports_enable_disable_tracepoint ()
15123 && current_trace_status ()->running && is_tracepoint (bpt))
15124 {
15125 struct bp_location *location;
15126
15127 for (location = bpt->loc; location; location = location->next)
15128 target_enable_tracepoint (location);
15129 }
15130
b4c291bb 15131 bpt->disposition = disposition;
816338b5 15132 bpt->enable_count = count;
44702360 15133 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15134
8d3788bd 15135 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15136}
15137
fe3f5fa8 15138
c906108c 15139void
fba45db2 15140enable_breakpoint (struct breakpoint *bpt)
c906108c 15141{
816338b5 15142 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15143}
15144
15145static void
15146do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15147{
15148 enable_breakpoint (bpt);
c906108c
SS
15149}
15150
95a42b64
TT
15151/* A callback for map_breakpoint_numbers that calls
15152 enable_breakpoint. */
15153
15154static void
15155do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15156{
51be5b68 15157 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15158}
15159
c906108c
SS
15160/* The enable command enables the specified breakpoints (or all defined
15161 breakpoints) so they once again become (or continue to be) effective
1272ad14 15162 in stopping the inferior. */
c906108c 15163
c906108c 15164static void
fba45db2 15165enable_command (char *args, int from_tty)
c906108c 15166{
c906108c 15167 if (args == 0)
46c6471b
PA
15168 {
15169 struct breakpoint *bpt;
15170
15171 ALL_BREAKPOINTS (bpt)
15172 if (user_breakpoint_p (bpt))
15173 enable_breakpoint (bpt);
15174 }
9eaabc75 15175 else
0d381245 15176 {
9eaabc75
MW
15177 char *num = extract_arg (&args);
15178
15179 while (num)
d248b706 15180 {
9eaabc75 15181 if (strchr (num, '.'))
b775012e 15182 {
9eaabc75
MW
15183 struct bp_location *loc = find_location_by_number (num);
15184
15185 if (loc)
15186 {
15187 if (!loc->enabled)
15188 {
15189 loc->enabled = 1;
15190 mark_breakpoint_location_modified (loc);
15191 }
15192 if (target_supports_enable_disable_tracepoint ()
15193 && current_trace_status ()->running && loc->owner
15194 && is_tracepoint (loc->owner))
15195 target_enable_tracepoint (loc);
15196 }
44702360 15197 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15198 }
9eaabc75
MW
15199 else
15200 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15201 num = extract_arg (&args);
d248b706 15202 }
0d381245 15203 }
c906108c
SS
15204}
15205
816338b5
SS
15206/* This struct packages up disposition data for application to multiple
15207 breakpoints. */
15208
15209struct disp_data
15210{
15211 enum bpdisp disp;
15212 int count;
15213};
15214
c906108c 15215static void
51be5b68
PA
15216do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15217{
816338b5 15218 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15219
816338b5 15220 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15221}
15222
15223static void
15224do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15225{
816338b5 15226 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15227
15228 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15229}
15230
c906108c 15231static void
fba45db2 15232enable_once_command (char *args, int from_tty)
c906108c 15233{
51be5b68 15234 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15235}
15236
816338b5
SS
15237static void
15238do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15239{
15240 struct disp_data disp = { disp_disable, *(int *) countptr };
15241
15242 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15243}
15244
15245static void
15246enable_count_command (char *args, int from_tty)
15247{
15248 int count = get_number (&args);
15249
15250 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15251}
15252
c906108c 15253static void
51be5b68 15254do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15255{
816338b5 15256 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15257
15258 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15259}
15260
c906108c 15261static void
fba45db2 15262enable_delete_command (char *args, int from_tty)
c906108c 15263{
51be5b68 15264 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15265}
15266\f
fa8d40ab
JJ
15267static void
15268set_breakpoint_cmd (char *args, int from_tty)
15269{
15270}
15271
15272static void
15273show_breakpoint_cmd (char *args, int from_tty)
15274{
15275}
15276
1f3b5d1b
PP
15277/* Invalidate last known value of any hardware watchpoint if
15278 the memory which that value represents has been written to by
15279 GDB itself. */
15280
15281static void
8de0566d
YQ
15282invalidate_bp_value_on_memory_change (struct inferior *inferior,
15283 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15284 const bfd_byte *data)
15285{
15286 struct breakpoint *bp;
15287
15288 ALL_BREAKPOINTS (bp)
15289 if (bp->enable_state == bp_enabled
3a5c3e22 15290 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15291 {
3a5c3e22 15292 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15293
3a5c3e22
PA
15294 if (wp->val_valid && wp->val)
15295 {
15296 struct bp_location *loc;
15297
15298 for (loc = bp->loc; loc != NULL; loc = loc->next)
15299 if (loc->loc_type == bp_loc_hardware_watchpoint
15300 && loc->address + loc->length > addr
15301 && addr + len > loc->address)
15302 {
15303 value_free (wp->val);
15304 wp->val = NULL;
15305 wp->val_valid = 0;
15306 }
15307 }
1f3b5d1b
PP
15308 }
15309}
15310
8181d85f
DJ
15311/* Create and insert a breakpoint for software single step. */
15312
15313void
6c95b8df 15314insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15315 struct address_space *aspace,
15316 CORE_ADDR next_pc)
8181d85f 15317{
7c16b83e
PA
15318 struct thread_info *tp = inferior_thread ();
15319 struct symtab_and_line sal;
15320 CORE_ADDR pc = next_pc;
8181d85f 15321
7c16b83e
PA
15322 if (single_step_breakpoints == NULL)
15323 single_step_breakpoints = new_single_step_breakpoint (tp->num, gdbarch);
8181d85f 15324
7c16b83e
PA
15325 sal = find_pc_line (pc, 0);
15326 sal.pc = pc;
15327 sal.section = find_pc_overlay (pc);
15328 sal.explicit_pc = 1;
15329 add_location_to_breakpoint (single_step_breakpoints, &sal);
8181d85f 15330
7c16b83e 15331 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15332}
15333
f02253f1
HZ
15334/* Check if the breakpoints used for software single stepping
15335 were inserted or not. */
15336
15337int
15338single_step_breakpoints_inserted (void)
15339{
7c16b83e 15340 return (single_step_breakpoints != NULL);
f02253f1
HZ
15341}
15342
8181d85f
DJ
15343/* Remove and delete any breakpoints used for software single step. */
15344
15345void
15346remove_single_step_breakpoints (void)
15347{
7c16b83e 15348 gdb_assert (single_step_breakpoints != NULL);
8181d85f 15349
7c16b83e 15350 delete_breakpoint (single_step_breakpoints);
8181d85f 15351
7c16b83e 15352 single_step_breakpoints = NULL;
8181d85f
DJ
15353}
15354
d03285ec
UW
15355/* Delete software single step breakpoints without removing them from
15356 the inferior. This is intended to be used if the inferior's address
15357 space where they were inserted is already gone, e.g. after exit or
15358 exec. */
15359
15360void
15361cancel_single_step_breakpoints (void)
15362{
7c16b83e
PA
15363 /* We don't really need to (or should) delete them here. After an
15364 exit, breakpoint_init_inferior deletes it. After an exec,
15365 update_breakpoints_after_exec does it. Just clear our
15366 reference. */
15367 single_step_breakpoints = NULL;
d03285ec
UW
15368}
15369
7c16b83e 15370/* Check whether any location of BP is inserted at PC. */
1aafd4da 15371
ef370185 15372static int
7c16b83e
PA
15373breakpoint_has_location_inserted_here (struct breakpoint *bp,
15374 struct address_space *aspace,
15375 CORE_ADDR pc)
1aafd4da 15376{
7c16b83e 15377 struct bp_location *loc;
1aafd4da 15378
7c16b83e
PA
15379 for (loc = bp->loc; loc != NULL; loc = loc->next)
15380 if (loc->inserted
15381 && breakpoint_location_address_match (loc, aspace, pc))
15382 return 1;
1aafd4da 15383
7c16b83e 15384 return 0;
ef370185
JB
15385}
15386
15387/* Check whether a software single-step breakpoint is inserted at
15388 PC. */
15389
15390int
15391single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15392 CORE_ADDR pc)
15393{
7c16b83e
PA
15394 return (single_step_breakpoints != NULL
15395 && breakpoint_has_location_inserted_here (single_step_breakpoints,
15396 aspace, pc));
1aafd4da
UW
15397}
15398
a96d9b2e
SDJ
15399/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15400 non-zero otherwise. */
15401static int
15402is_syscall_catchpoint_enabled (struct breakpoint *bp)
15403{
15404 if (syscall_catchpoint_p (bp)
15405 && bp->enable_state != bp_disabled
15406 && bp->enable_state != bp_call_disabled)
15407 return 1;
15408 else
15409 return 0;
15410}
15411
15412int
15413catch_syscall_enabled (void)
15414{
fa3064dd
YQ
15415 struct catch_syscall_inferior_data *inf_data
15416 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15417
fa3064dd 15418 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15419}
15420
15421int
15422catching_syscall_number (int syscall_number)
15423{
15424 struct breakpoint *bp;
15425
15426 ALL_BREAKPOINTS (bp)
15427 if (is_syscall_catchpoint_enabled (bp))
15428 {
be5c67c1
PA
15429 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15430
15431 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15432 {
15433 int i, iter;
15434 for (i = 0;
be5c67c1 15435 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15436 i++)
15437 if (syscall_number == iter)
15438 return 1;
15439 }
15440 else
15441 return 1;
15442 }
15443
15444 return 0;
15445}
15446
15447/* Complete syscall names. Used by "catch syscall". */
49c4e619 15448static VEC (char_ptr) *
a96d9b2e 15449catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15450 const char *text, const char *word)
a96d9b2e
SDJ
15451{
15452 const char **list = get_syscall_names ();
49c4e619 15453 VEC (char_ptr) *retlist
b45627a0 15454 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15455
c38eea1a
MS
15456 xfree (list);
15457 return retlist;
a96d9b2e
SDJ
15458}
15459
1042e4c0
SS
15460/* Tracepoint-specific operations. */
15461
15462/* Set tracepoint count to NUM. */
15463static void
15464set_tracepoint_count (int num)
15465{
15466 tracepoint_count = num;
4fa62494 15467 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15468}
15469
70221824 15470static void
1042e4c0
SS
15471trace_command (char *arg, int from_tty)
15472{
55aa24fb
SDJ
15473 struct breakpoint_ops *ops;
15474 const char *arg_cp = arg;
15475
15476 if (arg && probe_linespec_to_ops (&arg_cp))
15477 ops = &tracepoint_probe_breakpoint_ops;
15478 else
15479 ops = &tracepoint_breakpoint_ops;
15480
558a9d82
YQ
15481 create_breakpoint (get_current_arch (),
15482 arg,
15483 NULL, 0, NULL, 1 /* parse arg */,
15484 0 /* tempflag */,
15485 bp_tracepoint /* type_wanted */,
15486 0 /* Ignore count */,
15487 pending_break_support,
15488 ops,
15489 from_tty,
15490 1 /* enabled */,
15491 0 /* internal */, 0);
1042e4c0
SS
15492}
15493
70221824 15494static void
7a697b8d
SS
15495ftrace_command (char *arg, int from_tty)
15496{
558a9d82
YQ
15497 create_breakpoint (get_current_arch (),
15498 arg,
15499 NULL, 0, NULL, 1 /* parse arg */,
15500 0 /* tempflag */,
15501 bp_fast_tracepoint /* type_wanted */,
15502 0 /* Ignore count */,
15503 pending_break_support,
15504 &tracepoint_breakpoint_ops,
15505 from_tty,
15506 1 /* enabled */,
15507 0 /* internal */, 0);
0fb4aa4b
PA
15508}
15509
15510/* strace command implementation. Creates a static tracepoint. */
15511
70221824 15512static void
0fb4aa4b
PA
15513strace_command (char *arg, int from_tty)
15514{
983af33b
SDJ
15515 struct breakpoint_ops *ops;
15516
15517 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15518 or with a normal static tracepoint. */
15519 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15520 ops = &strace_marker_breakpoint_ops;
15521 else
15522 ops = &tracepoint_breakpoint_ops;
15523
558a9d82
YQ
15524 create_breakpoint (get_current_arch (),
15525 arg,
15526 NULL, 0, NULL, 1 /* parse arg */,
15527 0 /* tempflag */,
15528 bp_static_tracepoint /* type_wanted */,
15529 0 /* Ignore count */,
15530 pending_break_support,
15531 ops,
15532 from_tty,
15533 1 /* enabled */,
15534 0 /* internal */, 0);
7a697b8d
SS
15535}
15536
409873ef
SS
15537/* Set up a fake reader function that gets command lines from a linked
15538 list that was acquired during tracepoint uploading. */
15539
15540static struct uploaded_tp *this_utp;
3149d8c1 15541static int next_cmd;
409873ef
SS
15542
15543static char *
15544read_uploaded_action (void)
15545{
15546 char *rslt;
15547
3149d8c1 15548 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15549
3149d8c1 15550 next_cmd++;
409873ef
SS
15551
15552 return rslt;
15553}
15554
00bf0b85
SS
15555/* Given information about a tracepoint as recorded on a target (which
15556 can be either a live system or a trace file), attempt to create an
15557 equivalent GDB tracepoint. This is not a reliable process, since
15558 the target does not necessarily have all the information used when
15559 the tracepoint was originally defined. */
15560
d9b3f62e 15561struct tracepoint *
00bf0b85 15562create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15563{
409873ef 15564 char *addr_str, small_buf[100];
d9b3f62e 15565 struct tracepoint *tp;
fd9b8c24 15566
409873ef
SS
15567 if (utp->at_string)
15568 addr_str = utp->at_string;
15569 else
15570 {
15571 /* In the absence of a source location, fall back to raw
15572 address. Since there is no way to confirm that the address
15573 means the same thing as when the trace was started, warn the
15574 user. */
3e43a32a
MS
15575 warning (_("Uploaded tracepoint %d has no "
15576 "source location, using raw address"),
409873ef 15577 utp->number);
8c042590 15578 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15579 addr_str = small_buf;
15580 }
15581
15582 /* There's not much we can do with a sequence of bytecodes. */
15583 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15584 warning (_("Uploaded tracepoint %d condition "
15585 "has no source form, ignoring it"),
409873ef 15586 utp->number);
d5551862 15587
8cdf0e15 15588 if (!create_breakpoint (get_current_arch (),
409873ef 15589 addr_str,
e7e0cddf
SS
15590 utp->cond_string, -1, NULL,
15591 0 /* parse cond/thread */,
8cdf0e15 15592 0 /* tempflag */,
0fb4aa4b 15593 utp->type /* type_wanted */,
8cdf0e15
VP
15594 0 /* Ignore count */,
15595 pending_break_support,
348d480f 15596 &tracepoint_breakpoint_ops,
8cdf0e15 15597 0 /* from_tty */,
84f4c1fe 15598 utp->enabled /* enabled */,
44f238bb
PA
15599 0 /* internal */,
15600 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15601 return NULL;
15602
409873ef 15603 /* Get the tracepoint we just created. */
fd9b8c24
PA
15604 tp = get_tracepoint (tracepoint_count);
15605 gdb_assert (tp != NULL);
d5551862 15606
00bf0b85
SS
15607 if (utp->pass > 0)
15608 {
8c042590
PM
15609 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15610 tp->base.number);
00bf0b85 15611
409873ef 15612 trace_pass_command (small_buf, 0);
00bf0b85
SS
15613 }
15614
409873ef
SS
15615 /* If we have uploaded versions of the original commands, set up a
15616 special-purpose "reader" function and call the usual command line
15617 reader, then pass the result to the breakpoint command-setting
15618 function. */
3149d8c1 15619 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15620 {
409873ef 15621 struct command_line *cmd_list;
00bf0b85 15622
409873ef 15623 this_utp = utp;
3149d8c1 15624 next_cmd = 0;
d5551862 15625
409873ef
SS
15626 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15627
d9b3f62e 15628 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15629 }
3149d8c1
SS
15630 else if (!VEC_empty (char_ptr, utp->actions)
15631 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15632 warning (_("Uploaded tracepoint %d actions "
15633 "have no source form, ignoring them"),
409873ef 15634 utp->number);
00bf0b85 15635
f196051f
SS
15636 /* Copy any status information that might be available. */
15637 tp->base.hit_count = utp->hit_count;
15638 tp->traceframe_usage = utp->traceframe_usage;
15639
00bf0b85 15640 return tp;
d9b3f62e 15641}
00bf0b85 15642
1042e4c0
SS
15643/* Print information on tracepoint number TPNUM_EXP, or all if
15644 omitted. */
15645
15646static void
e5a67952 15647tracepoints_info (char *args, int from_tty)
1042e4c0 15648{
79a45e25 15649 struct ui_out *uiout = current_uiout;
e5a67952 15650 int num_printed;
1042e4c0 15651
e5a67952 15652 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15653
15654 if (num_printed == 0)
1042e4c0 15655 {
e5a67952 15656 if (args == NULL || *args == '\0')
d77f58be
SS
15657 ui_out_message (uiout, 0, "No tracepoints.\n");
15658 else
e5a67952 15659 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15660 }
ad443146
SS
15661
15662 default_collect_info ();
1042e4c0
SS
15663}
15664
4a64f543 15665/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15666 Not supported by all targets. */
15667static void
15668enable_trace_command (char *args, int from_tty)
15669{
15670 enable_command (args, from_tty);
15671}
15672
4a64f543 15673/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15674 Not supported by all targets. */
15675static void
15676disable_trace_command (char *args, int from_tty)
15677{
15678 disable_command (args, from_tty);
15679}
15680
4a64f543 15681/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15682static void
15683delete_trace_command (char *arg, int from_tty)
15684{
35df4500 15685 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15686
15687 dont_repeat ();
15688
15689 if (arg == 0)
15690 {
15691 int breaks_to_delete = 0;
15692
15693 /* Delete all breakpoints if no argument.
15694 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15695 have to be deleted with an explicit breakpoint number
15696 argument. */
1042e4c0 15697 ALL_TRACEPOINTS (b)
46c6471b 15698 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15699 {
15700 breaks_to_delete = 1;
15701 break;
15702 }
1042e4c0
SS
15703
15704 /* Ask user only if there are some breakpoints to delete. */
15705 if (!from_tty
15706 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15707 {
35df4500 15708 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15709 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15710 delete_breakpoint (b);
1042e4c0
SS
15711 }
15712 }
15713 else
51be5b68 15714 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15715}
15716
197f0a60
TT
15717/* Helper function for trace_pass_command. */
15718
15719static void
d9b3f62e 15720trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15721{
d9b3f62e 15722 tp->pass_count = count;
6f6484cd 15723 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15724 if (from_tty)
15725 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15726 tp->base.number, count);
197f0a60
TT
15727}
15728
1042e4c0
SS
15729/* Set passcount for tracepoint.
15730
15731 First command argument is passcount, second is tracepoint number.
15732 If tracepoint number omitted, apply to most recently defined.
15733 Also accepts special argument "all". */
15734
15735static void
15736trace_pass_command (char *args, int from_tty)
15737{
d9b3f62e 15738 struct tracepoint *t1;
1042e4c0 15739 unsigned int count;
1042e4c0
SS
15740
15741 if (args == 0 || *args == 0)
3e43a32a
MS
15742 error (_("passcount command requires an "
15743 "argument (count + optional TP num)"));
1042e4c0 15744
4a64f543 15745 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15746
529480d0 15747 args = skip_spaces (args);
1042e4c0
SS
15748 if (*args && strncasecmp (args, "all", 3) == 0)
15749 {
d9b3f62e
PA
15750 struct breakpoint *b;
15751
1042e4c0 15752 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15753 if (*args)
15754 error (_("Junk at end of arguments."));
1042e4c0 15755
d9b3f62e 15756 ALL_TRACEPOINTS (b)
197f0a60 15757 {
d9b3f62e 15758 t1 = (struct tracepoint *) b;
197f0a60
TT
15759 trace_pass_set_count (t1, count, from_tty);
15760 }
15761 }
15762 else if (*args == '\0')
1042e4c0 15763 {
5fa1d40e 15764 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15765 if (t1)
197f0a60
TT
15766 trace_pass_set_count (t1, count, from_tty);
15767 }
15768 else
15769 {
15770 struct get_number_or_range_state state;
15771
15772 init_number_or_range (&state, args);
15773 while (!state.finished)
1042e4c0 15774 {
5fa1d40e 15775 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15776 if (t1)
15777 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15778 }
15779 }
1042e4c0
SS
15780}
15781
d9b3f62e 15782struct tracepoint *
1042e4c0
SS
15783get_tracepoint (int num)
15784{
15785 struct breakpoint *t;
15786
15787 ALL_TRACEPOINTS (t)
15788 if (t->number == num)
d9b3f62e 15789 return (struct tracepoint *) t;
1042e4c0
SS
15790
15791 return NULL;
15792}
15793
d5551862
SS
15794/* Find the tracepoint with the given target-side number (which may be
15795 different from the tracepoint number after disconnecting and
15796 reconnecting). */
15797
d9b3f62e 15798struct tracepoint *
d5551862
SS
15799get_tracepoint_by_number_on_target (int num)
15800{
d9b3f62e 15801 struct breakpoint *b;
d5551862 15802
d9b3f62e
PA
15803 ALL_TRACEPOINTS (b)
15804 {
15805 struct tracepoint *t = (struct tracepoint *) b;
15806
15807 if (t->number_on_target == num)
15808 return t;
15809 }
d5551862
SS
15810
15811 return NULL;
15812}
15813
1042e4c0 15814/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15815 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15816 If the argument is missing, the most recent tracepoint
15817 (tracepoint_count) is returned. */
15818
d9b3f62e 15819struct tracepoint *
197f0a60 15820get_tracepoint_by_number (char **arg,
5fa1d40e 15821 struct get_number_or_range_state *state)
1042e4c0 15822{
1042e4c0
SS
15823 struct breakpoint *t;
15824 int tpnum;
15825 char *instring = arg == NULL ? NULL : *arg;
15826
197f0a60
TT
15827 if (state)
15828 {
15829 gdb_assert (!state->finished);
15830 tpnum = get_number_or_range (state);
15831 }
15832 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15833 tpnum = tracepoint_count;
1042e4c0 15834 else
197f0a60 15835 tpnum = get_number (arg);
1042e4c0
SS
15836
15837 if (tpnum <= 0)
15838 {
15839 if (instring && *instring)
15840 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15841 instring);
15842 else
5fa1d40e 15843 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15844 return NULL;
15845 }
15846
15847 ALL_TRACEPOINTS (t)
15848 if (t->number == tpnum)
15849 {
d9b3f62e 15850 return (struct tracepoint *) t;
1042e4c0
SS
15851 }
15852
1042e4c0
SS
15853 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15854 return NULL;
15855}
15856
d9b3f62e
PA
15857void
15858print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15859{
15860 if (b->thread != -1)
15861 fprintf_unfiltered (fp, " thread %d", b->thread);
15862
15863 if (b->task != 0)
15864 fprintf_unfiltered (fp, " task %d", b->task);
15865
15866 fprintf_unfiltered (fp, "\n");
15867}
15868
6149aea9
PA
15869/* Save information on user settable breakpoints (watchpoints, etc) to
15870 a new script file named FILENAME. If FILTER is non-NULL, call it
15871 on each breakpoint and only include the ones for which it returns
15872 non-zero. */
15873
1042e4c0 15874static void
6149aea9
PA
15875save_breakpoints (char *filename, int from_tty,
15876 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15877{
15878 struct breakpoint *tp;
6149aea9 15879 int any = 0;
1042e4c0 15880 struct cleanup *cleanup;
a7bdde9e 15881 struct ui_file *fp;
6149aea9 15882 int extra_trace_bits = 0;
1042e4c0 15883
6149aea9
PA
15884 if (filename == 0 || *filename == 0)
15885 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15886
15887 /* See if we have anything to save. */
6149aea9 15888 ALL_BREAKPOINTS (tp)
1042e4c0 15889 {
6149aea9 15890 /* Skip internal and momentary breakpoints. */
09d682a4 15891 if (!user_breakpoint_p (tp))
6149aea9
PA
15892 continue;
15893
15894 /* If we have a filter, only save the breakpoints it accepts. */
15895 if (filter && !filter (tp))
15896 continue;
15897
15898 any = 1;
15899
15900 if (is_tracepoint (tp))
15901 {
15902 extra_trace_bits = 1;
15903
15904 /* We can stop searching. */
15905 break;
15906 }
1042e4c0 15907 }
6149aea9
PA
15908
15909 if (!any)
1042e4c0 15910 {
6149aea9 15911 warning (_("Nothing to save."));
1042e4c0
SS
15912 return;
15913 }
15914
c718be47
PA
15915 filename = tilde_expand (filename);
15916 cleanup = make_cleanup (xfree, filename);
15917 fp = gdb_fopen (filename, "w");
059fb39f 15918 if (!fp)
6149aea9
PA
15919 error (_("Unable to open file '%s' for saving (%s)"),
15920 filename, safe_strerror (errno));
a7bdde9e 15921 make_cleanup_ui_file_delete (fp);
8bf6485c 15922
6149aea9
PA
15923 if (extra_trace_bits)
15924 save_trace_state_variables (fp);
8bf6485c 15925
6149aea9 15926 ALL_BREAKPOINTS (tp)
1042e4c0 15927 {
6149aea9 15928 /* Skip internal and momentary breakpoints. */
09d682a4 15929 if (!user_breakpoint_p (tp))
6149aea9 15930 continue;
8bf6485c 15931
6149aea9
PA
15932 /* If we have a filter, only save the breakpoints it accepts. */
15933 if (filter && !filter (tp))
15934 continue;
15935
348d480f 15936 tp->ops->print_recreate (tp, fp);
1042e4c0 15937
6149aea9
PA
15938 /* Note, we can't rely on tp->number for anything, as we can't
15939 assume the recreated breakpoint numbers will match. Use $bpnum
15940 instead. */
15941
15942 if (tp->cond_string)
15943 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15944
15945 if (tp->ignore_count)
15946 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15947
2d9442cc 15948 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15949 {
a7bdde9e
VP
15950 volatile struct gdb_exception ex;
15951
6149aea9 15952 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15953
79a45e25 15954 ui_out_redirect (current_uiout, fp);
14dba4b4 15955 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15956 {
79a45e25 15957 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15958 }
79a45e25 15959 ui_out_redirect (current_uiout, NULL);
1042e4c0 15960
a7bdde9e
VP
15961 if (ex.reason < 0)
15962 throw_exception (ex);
1042e4c0 15963
a7bdde9e 15964 fprintf_unfiltered (fp, " end\n");
1042e4c0 15965 }
6149aea9
PA
15966
15967 if (tp->enable_state == bp_disabled)
99894e11 15968 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15969
15970 /* If this is a multi-location breakpoint, check if the locations
15971 should be individually disabled. Watchpoint locations are
15972 special, and not user visible. */
15973 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15974 {
15975 struct bp_location *loc;
15976 int n = 1;
15977
15978 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15979 if (!loc->enabled)
15980 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15981 }
1042e4c0 15982 }
8bf6485c 15983
6149aea9 15984 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15985 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15986
1042e4c0 15987 if (from_tty)
6149aea9 15988 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15989 do_cleanups (cleanup);
6149aea9
PA
15990}
15991
15992/* The `save breakpoints' command. */
15993
15994static void
15995save_breakpoints_command (char *args, int from_tty)
15996{
15997 save_breakpoints (args, from_tty, NULL);
15998}
15999
16000/* The `save tracepoints' command. */
16001
16002static void
16003save_tracepoints_command (char *args, int from_tty)
16004{
16005 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16006}
16007
16008/* Create a vector of all tracepoints. */
16009
16010VEC(breakpoint_p) *
eeae04df 16011all_tracepoints (void)
1042e4c0
SS
16012{
16013 VEC(breakpoint_p) *tp_vec = 0;
16014 struct breakpoint *tp;
16015
16016 ALL_TRACEPOINTS (tp)
16017 {
16018 VEC_safe_push (breakpoint_p, tp_vec, tp);
16019 }
16020
16021 return tp_vec;
16022}
16023
c906108c 16024\f
4a64f543
MS
16025/* This help string is used for the break, hbreak, tbreak and thbreak
16026 commands. It is defined as a macro to prevent duplication.
16027 COMMAND should be a string constant containing the name of the
16028 command. */
31e2b00f 16029#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16030command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16031PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16032probe point. Accepted values are `-probe' (for a generic, automatically\n\
16033guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16034LOCATION may be a line number, function name, or \"*\" and an address.\n\
16035If a line number is specified, break at start of code for that line.\n\
16036If a function is specified, break at start of code for that function.\n\
16037If an address is specified, break at that exact address.\n\
dc10affe
PA
16038With no LOCATION, uses current execution address of the selected\n\
16039stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16040\n\
16041THREADNUM is the number from \"info threads\".\n\
16042CONDITION is a boolean expression.\n\
16043\n\
d41c0fc8
PA
16044Multiple breakpoints at one place are permitted, and useful if their\n\
16045conditions are different.\n\
31e2b00f
AS
16046\n\
16047Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16048
44feb3ce
TT
16049/* List of subcommands for "catch". */
16050static struct cmd_list_element *catch_cmdlist;
16051
16052/* List of subcommands for "tcatch". */
16053static struct cmd_list_element *tcatch_cmdlist;
16054
9ac4176b 16055void
44feb3ce 16056add_catch_command (char *name, char *docstring,
82ae6c8d 16057 cmd_sfunc_ftype *sfunc,
625e8578 16058 completer_ftype *completer,
44feb3ce
TT
16059 void *user_data_catch,
16060 void *user_data_tcatch)
16061{
16062 struct cmd_list_element *command;
16063
16064 command = add_cmd (name, class_breakpoint, NULL, docstring,
16065 &catch_cmdlist);
16066 set_cmd_sfunc (command, sfunc);
16067 set_cmd_context (command, user_data_catch);
a96d9b2e 16068 set_cmd_completer (command, completer);
44feb3ce
TT
16069
16070 command = add_cmd (name, class_breakpoint, NULL, docstring,
16071 &tcatch_cmdlist);
16072 set_cmd_sfunc (command, sfunc);
16073 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16074 set_cmd_completer (command, completer);
44feb3ce
TT
16075}
16076
6c95b8df 16077static void
a79b8f6e 16078clear_syscall_counts (struct inferior *inf)
6c95b8df 16079{
fa3064dd
YQ
16080 struct catch_syscall_inferior_data *inf_data
16081 = get_catch_syscall_inferior_data (inf);
16082
16083 inf_data->total_syscalls_count = 0;
16084 inf_data->any_syscall_count = 0;
16085 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16086}
16087
6149aea9
PA
16088static void
16089save_command (char *arg, int from_tty)
16090{
3e43a32a
MS
16091 printf_unfiltered (_("\"save\" must be followed by "
16092 "the name of a save subcommand.\n"));
635c7e8a 16093 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16094}
16095
84f4c1fe
PM
16096struct breakpoint *
16097iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16098 void *data)
16099{
35df4500 16100 struct breakpoint *b, *b_tmp;
84f4c1fe 16101
35df4500 16102 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16103 {
16104 if ((*callback) (b, data))
16105 return b;
16106 }
16107
16108 return NULL;
16109}
16110
0574c78f
GB
16111/* Zero if any of the breakpoint's locations could be a location where
16112 functions have been inlined, nonzero otherwise. */
16113
16114static int
16115is_non_inline_function (struct breakpoint *b)
16116{
16117 /* The shared library event breakpoint is set on the address of a
16118 non-inline function. */
16119 if (b->type == bp_shlib_event)
16120 return 1;
16121
16122 return 0;
16123}
16124
16125/* Nonzero if the specified PC cannot be a location where functions
16126 have been inlined. */
16127
16128int
09ac7c10
TT
16129pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16130 const struct target_waitstatus *ws)
0574c78f
GB
16131{
16132 struct breakpoint *b;
16133 struct bp_location *bl;
16134
16135 ALL_BREAKPOINTS (b)
16136 {
16137 if (!is_non_inline_function (b))
16138 continue;
16139
16140 for (bl = b->loc; bl != NULL; bl = bl->next)
16141 {
16142 if (!bl->shlib_disabled
09ac7c10 16143 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16144 return 1;
16145 }
16146 }
16147
16148 return 0;
16149}
16150
2f202fde
JK
16151/* Remove any references to OBJFILE which is going to be freed. */
16152
16153void
16154breakpoint_free_objfile (struct objfile *objfile)
16155{
16156 struct bp_location **locp, *loc;
16157
16158 ALL_BP_LOCATIONS (loc, locp)
16159 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16160 loc->symtab = NULL;
16161}
16162
2060206e
PA
16163void
16164initialize_breakpoint_ops (void)
16165{
16166 static int initialized = 0;
16167
16168 struct breakpoint_ops *ops;
16169
16170 if (initialized)
16171 return;
16172 initialized = 1;
16173
16174 /* The breakpoint_ops structure to be inherit by all kinds of
16175 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16176 internal and momentary breakpoints, etc.). */
16177 ops = &bkpt_base_breakpoint_ops;
16178 *ops = base_breakpoint_ops;
16179 ops->re_set = bkpt_re_set;
16180 ops->insert_location = bkpt_insert_location;
16181 ops->remove_location = bkpt_remove_location;
16182 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16183 ops->create_sals_from_address = bkpt_create_sals_from_address;
16184 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16185 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16186
16187 /* The breakpoint_ops structure to be used in regular breakpoints. */
16188 ops = &bkpt_breakpoint_ops;
16189 *ops = bkpt_base_breakpoint_ops;
16190 ops->re_set = bkpt_re_set;
16191 ops->resources_needed = bkpt_resources_needed;
16192 ops->print_it = bkpt_print_it;
16193 ops->print_mention = bkpt_print_mention;
16194 ops->print_recreate = bkpt_print_recreate;
16195
16196 /* Ranged breakpoints. */
16197 ops = &ranged_breakpoint_ops;
16198 *ops = bkpt_breakpoint_ops;
16199 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16200 ops->resources_needed = resources_needed_ranged_breakpoint;
16201 ops->print_it = print_it_ranged_breakpoint;
16202 ops->print_one = print_one_ranged_breakpoint;
16203 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16204 ops->print_mention = print_mention_ranged_breakpoint;
16205 ops->print_recreate = print_recreate_ranged_breakpoint;
16206
16207 /* Internal breakpoints. */
16208 ops = &internal_breakpoint_ops;
16209 *ops = bkpt_base_breakpoint_ops;
16210 ops->re_set = internal_bkpt_re_set;
16211 ops->check_status = internal_bkpt_check_status;
16212 ops->print_it = internal_bkpt_print_it;
16213 ops->print_mention = internal_bkpt_print_mention;
16214
16215 /* Momentary breakpoints. */
16216 ops = &momentary_breakpoint_ops;
16217 *ops = bkpt_base_breakpoint_ops;
16218 ops->re_set = momentary_bkpt_re_set;
16219 ops->check_status = momentary_bkpt_check_status;
16220 ops->print_it = momentary_bkpt_print_it;
16221 ops->print_mention = momentary_bkpt_print_mention;
16222
e2e4d78b
JK
16223 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16224 ops = &longjmp_breakpoint_ops;
16225 *ops = momentary_breakpoint_ops;
16226 ops->dtor = longjmp_bkpt_dtor;
16227
55aa24fb
SDJ
16228 /* Probe breakpoints. */
16229 ops = &bkpt_probe_breakpoint_ops;
16230 *ops = bkpt_breakpoint_ops;
16231 ops->insert_location = bkpt_probe_insert_location;
16232 ops->remove_location = bkpt_probe_remove_location;
16233 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16234 ops->decode_linespec = bkpt_probe_decode_linespec;
16235
2060206e
PA
16236 /* Watchpoints. */
16237 ops = &watchpoint_breakpoint_ops;
16238 *ops = base_breakpoint_ops;
3a5c3e22 16239 ops->dtor = dtor_watchpoint;
2060206e
PA
16240 ops->re_set = re_set_watchpoint;
16241 ops->insert_location = insert_watchpoint;
16242 ops->remove_location = remove_watchpoint;
16243 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16244 ops->check_status = check_status_watchpoint;
16245 ops->resources_needed = resources_needed_watchpoint;
16246 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16247 ops->print_it = print_it_watchpoint;
16248 ops->print_mention = print_mention_watchpoint;
16249 ops->print_recreate = print_recreate_watchpoint;
427cd150 16250 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16251
16252 /* Masked watchpoints. */
16253 ops = &masked_watchpoint_breakpoint_ops;
16254 *ops = watchpoint_breakpoint_ops;
16255 ops->insert_location = insert_masked_watchpoint;
16256 ops->remove_location = remove_masked_watchpoint;
16257 ops->resources_needed = resources_needed_masked_watchpoint;
16258 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16259 ops->print_it = print_it_masked_watchpoint;
16260 ops->print_one_detail = print_one_detail_masked_watchpoint;
16261 ops->print_mention = print_mention_masked_watchpoint;
16262 ops->print_recreate = print_recreate_masked_watchpoint;
16263
16264 /* Tracepoints. */
16265 ops = &tracepoint_breakpoint_ops;
16266 *ops = base_breakpoint_ops;
16267 ops->re_set = tracepoint_re_set;
16268 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16269 ops->print_one_detail = tracepoint_print_one_detail;
16270 ops->print_mention = tracepoint_print_mention;
16271 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16272 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16273 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16274 ops->decode_linespec = tracepoint_decode_linespec;
16275
55aa24fb
SDJ
16276 /* Probe tracepoints. */
16277 ops = &tracepoint_probe_breakpoint_ops;
16278 *ops = tracepoint_breakpoint_ops;
16279 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16280 ops->decode_linespec = tracepoint_probe_decode_linespec;
16281
983af33b
SDJ
16282 /* Static tracepoints with marker (`-m'). */
16283 ops = &strace_marker_breakpoint_ops;
16284 *ops = tracepoint_breakpoint_ops;
16285 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16286 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16287 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16288
16289 /* Fork catchpoints. */
16290 ops = &catch_fork_breakpoint_ops;
16291 *ops = base_breakpoint_ops;
16292 ops->insert_location = insert_catch_fork;
16293 ops->remove_location = remove_catch_fork;
16294 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16295 ops->print_it = print_it_catch_fork;
16296 ops->print_one = print_one_catch_fork;
16297 ops->print_mention = print_mention_catch_fork;
16298 ops->print_recreate = print_recreate_catch_fork;
16299
16300 /* Vfork catchpoints. */
16301 ops = &catch_vfork_breakpoint_ops;
16302 *ops = base_breakpoint_ops;
16303 ops->insert_location = insert_catch_vfork;
16304 ops->remove_location = remove_catch_vfork;
16305 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16306 ops->print_it = print_it_catch_vfork;
16307 ops->print_one = print_one_catch_vfork;
16308 ops->print_mention = print_mention_catch_vfork;
16309 ops->print_recreate = print_recreate_catch_vfork;
16310
16311 /* Exec catchpoints. */
16312 ops = &catch_exec_breakpoint_ops;
16313 *ops = base_breakpoint_ops;
16314 ops->dtor = dtor_catch_exec;
16315 ops->insert_location = insert_catch_exec;
16316 ops->remove_location = remove_catch_exec;
16317 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16318 ops->print_it = print_it_catch_exec;
16319 ops->print_one = print_one_catch_exec;
16320 ops->print_mention = print_mention_catch_exec;
16321 ops->print_recreate = print_recreate_catch_exec;
16322
16323 /* Syscall catchpoints. */
16324 ops = &catch_syscall_breakpoint_ops;
16325 *ops = base_breakpoint_ops;
16326 ops->dtor = dtor_catch_syscall;
16327 ops->insert_location = insert_catch_syscall;
16328 ops->remove_location = remove_catch_syscall;
16329 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16330 ops->print_it = print_it_catch_syscall;
16331 ops->print_one = print_one_catch_syscall;
16332 ops->print_mention = print_mention_catch_syscall;
16333 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16334
16335 /* Solib-related catchpoints. */
16336 ops = &catch_solib_breakpoint_ops;
16337 *ops = base_breakpoint_ops;
16338 ops->dtor = dtor_catch_solib;
16339 ops->insert_location = insert_catch_solib;
16340 ops->remove_location = remove_catch_solib;
16341 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16342 ops->check_status = check_status_catch_solib;
16343 ops->print_it = print_it_catch_solib;
16344 ops->print_one = print_one_catch_solib;
16345 ops->print_mention = print_mention_catch_solib;
16346 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16347
16348 ops = &dprintf_breakpoint_ops;
16349 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16350 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16351 ops->resources_needed = bkpt_resources_needed;
16352 ops->print_it = bkpt_print_it;
16353 ops->print_mention = bkpt_print_mention;
2d9442cc 16354 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16355 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16356 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16357}
16358
8bfd80db
YQ
16359/* Chain containing all defined "enable breakpoint" subcommands. */
16360
16361static struct cmd_list_element *enablebreaklist = NULL;
16362
c906108c 16363void
fba45db2 16364_initialize_breakpoint (void)
c906108c
SS
16365{
16366 struct cmd_list_element *c;
16367
2060206e
PA
16368 initialize_breakpoint_ops ();
16369
84acb35a 16370 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16371 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16372 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16373 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16374
55aa24fb
SDJ
16375 breakpoint_objfile_key
16376 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16377
fa3064dd 16378 catch_syscall_inferior_data
8e260fc0
TT
16379 = register_inferior_data_with_cleanup (NULL,
16380 catch_syscall_inferior_data_cleanup);
fa3064dd 16381
c906108c
SS
16382 breakpoint_chain = 0;
16383 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16384 before a breakpoint is set. */
16385 breakpoint_count = 0;
16386
1042e4c0
SS
16387 tracepoint_count = 0;
16388
1bedd215
AC
16389 add_com ("ignore", class_breakpoint, ignore_command, _("\
16390Set ignore-count of breakpoint number N to COUNT.\n\
16391Usage is `ignore N COUNT'."));
c906108c 16392 if (xdb_commands)
c5aa993b 16393 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16394
1bedd215
AC
16395 add_com ("commands", class_breakpoint, commands_command, _("\
16396Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16397Give breakpoint number as argument after \"commands\".\n\
16398With no argument, the targeted breakpoint is the last one set.\n\
16399The commands themselves follow starting on the next line.\n\
16400Type a line containing \"end\" to indicate the end of them.\n\
16401Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16402then no output is printed when it is hit, except what the commands print."));
c906108c 16403
d55637df 16404 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16405Specify breakpoint number N to break only if COND is true.\n\
c906108c 16406Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16407expression to be evaluated whenever breakpoint N is reached."));
d55637df 16408 set_cmd_completer (c, condition_completer);
c906108c 16409
1bedd215 16410 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16411Set a temporary breakpoint.\n\
c906108c
SS
16412Like \"break\" except the breakpoint is only temporary,\n\
16413so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16414by using \"enable delete\" on the breakpoint number.\n\
16415\n"
16416BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16417 set_cmd_completer (c, location_completer);
c94fdfd0 16418
1bedd215 16419 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16420Set a hardware assisted breakpoint.\n\
c906108c 16421Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16422some target hardware may not have this support.\n\
16423\n"
16424BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16425 set_cmd_completer (c, location_completer);
c906108c 16426
1bedd215 16427 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16428Set a temporary hardware assisted breakpoint.\n\
c906108c 16429Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16430so it will be deleted when hit.\n\
16431\n"
16432BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16433 set_cmd_completer (c, location_completer);
c906108c 16434
1bedd215
AC
16435 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16436Enable some breakpoints.\n\
c906108c
SS
16437Give breakpoint numbers (separated by spaces) as arguments.\n\
16438With no subcommand, breakpoints are enabled until you command otherwise.\n\
16439This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16440With a subcommand you can enable temporarily."),
c906108c
SS
16441 &enablelist, "enable ", 1, &cmdlist);
16442 if (xdb_commands)
1bedd215
AC
16443 add_com ("ab", class_breakpoint, enable_command, _("\
16444Enable some breakpoints.\n\
c906108c
SS
16445Give breakpoint numbers (separated by spaces) as arguments.\n\
16446With no subcommand, breakpoints are enabled until you command otherwise.\n\
16447This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16448With a subcommand you can enable temporarily."));
c906108c
SS
16449
16450 add_com_alias ("en", "enable", class_breakpoint, 1);
16451
84951ab5 16452 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16453Enable some breakpoints.\n\
c906108c
SS
16454Give breakpoint numbers (separated by spaces) as arguments.\n\
16455This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16456May be abbreviated to simply \"enable\".\n"),
c5aa993b 16457 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16458
1a966eab
AC
16459 add_cmd ("once", no_class, enable_once_command, _("\
16460Enable breakpoints for one hit. Give breakpoint numbers.\n\
16461If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16462 &enablebreaklist);
16463
1a966eab
AC
16464 add_cmd ("delete", no_class, enable_delete_command, _("\
16465Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16466If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16467 &enablebreaklist);
16468
816338b5
SS
16469 add_cmd ("count", no_class, enable_count_command, _("\
16470Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16471If a breakpoint is hit while enabled in this fashion,\n\
16472the count is decremented; when it reaches zero, the breakpoint is disabled."),
16473 &enablebreaklist);
16474
1a966eab
AC
16475 add_cmd ("delete", no_class, enable_delete_command, _("\
16476Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16477If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16478 &enablelist);
16479
1a966eab
AC
16480 add_cmd ("once", no_class, enable_once_command, _("\
16481Enable breakpoints for one hit. Give breakpoint numbers.\n\
16482If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16483 &enablelist);
16484
16485 add_cmd ("count", no_class, enable_count_command, _("\
16486Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16487If a breakpoint is hit while enabled in this fashion,\n\
16488the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16489 &enablelist);
16490
1bedd215
AC
16491 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16492Disable some breakpoints.\n\
c906108c
SS
16493Arguments are breakpoint numbers with spaces in between.\n\
16494To disable all breakpoints, give no argument.\n\
64b9b334 16495A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16496 &disablelist, "disable ", 1, &cmdlist);
16497 add_com_alias ("dis", "disable", class_breakpoint, 1);
16498 add_com_alias ("disa", "disable", class_breakpoint, 1);
16499 if (xdb_commands)
1bedd215
AC
16500 add_com ("sb", class_breakpoint, disable_command, _("\
16501Disable some breakpoints.\n\
c906108c
SS
16502Arguments are breakpoint numbers with spaces in between.\n\
16503To disable all breakpoints, give no argument.\n\
64b9b334 16504A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16505
1a966eab
AC
16506 add_cmd ("breakpoints", class_alias, disable_command, _("\
16507Disable some breakpoints.\n\
c906108c
SS
16508Arguments are breakpoint numbers with spaces in between.\n\
16509To disable all breakpoints, give no argument.\n\
64b9b334 16510A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16511This command may be abbreviated \"disable\"."),
c906108c
SS
16512 &disablelist);
16513
1bedd215
AC
16514 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16515Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16516Arguments are breakpoint numbers with spaces in between.\n\
16517To delete all breakpoints, give no argument.\n\
16518\n\
16519Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16520The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16521 &deletelist, "delete ", 1, &cmdlist);
16522 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16523 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16524 if (xdb_commands)
1bedd215
AC
16525 add_com ("db", class_breakpoint, delete_command, _("\
16526Delete some breakpoints.\n\
c906108c 16527Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16528To delete all breakpoints, give no argument.\n"));
c906108c 16529
1a966eab
AC
16530 add_cmd ("breakpoints", class_alias, delete_command, _("\
16531Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16532Arguments are breakpoint numbers with spaces in between.\n\
16533To delete all breakpoints, give no argument.\n\
1a966eab 16534This command may be abbreviated \"delete\"."),
c906108c
SS
16535 &deletelist);
16536
1bedd215
AC
16537 add_com ("clear", class_breakpoint, clear_command, _("\
16538Clear breakpoint at specified line or function.\n\
c906108c
SS
16539Argument may be line number, function name, or \"*\" and an address.\n\
16540If line number is specified, all breakpoints in that line are cleared.\n\
16541If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16542If an address is specified, breakpoints at that address are cleared.\n\
16543\n\
16544With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16545is executing in.\n\
16546\n\
1bedd215 16547See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16548 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16549
1bedd215 16550 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16551Set breakpoint at specified line or function.\n"
16552BREAK_ARGS_HELP ("break")));
5ba2abeb 16553 set_cmd_completer (c, location_completer);
c94fdfd0 16554
c906108c
SS
16555 add_com_alias ("b", "break", class_run, 1);
16556 add_com_alias ("br", "break", class_run, 1);
16557 add_com_alias ("bre", "break", class_run, 1);
16558 add_com_alias ("brea", "break", class_run, 1);
16559
7681d515
PM
16560 if (xdb_commands)
16561 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16562
16563 if (dbx_commands)
16564 {
1bedd215
AC
16565 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16566Break in function/address or break at a line in the current file."),
c5aa993b
JM
16567 &stoplist, "stop ", 1, &cmdlist);
16568 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16569 _("Break in function or address."), &stoplist);
c5aa993b 16570 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16571 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16572 add_com ("status", class_info, breakpoints_info, _("\
16573Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16574The \"Type\" column indicates one of:\n\
16575\tbreakpoint - normal breakpoint\n\
16576\twatchpoint - watchpoint\n\
16577The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16578the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16579breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16580address and file/line number respectively.\n\
16581\n\
16582Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16583are set to the address of the last breakpoint listed unless the command\n\
16584is prefixed with \"server \".\n\n\
c906108c 16585Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16586breakpoint set."));
c906108c
SS
16587 }
16588
1bedd215 16589 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16590Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16591The \"Type\" column indicates one of:\n\
16592\tbreakpoint - normal breakpoint\n\
16593\twatchpoint - watchpoint\n\
16594The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16595the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16596breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16597address and file/line number respectively.\n\
16598\n\
16599Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16600are set to the address of the last breakpoint listed unless the command\n\
16601is prefixed with \"server \".\n\n\
c906108c 16602Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16603breakpoint set."));
c906108c 16604
6b04bdb7
MS
16605 add_info_alias ("b", "breakpoints", 1);
16606
c906108c 16607 if (xdb_commands)
1bedd215
AC
16608 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16609Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16610The \"Type\" column indicates one of:\n\
16611\tbreakpoint - normal breakpoint\n\
16612\twatchpoint - watchpoint\n\
16613The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16614the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16615breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16616address and file/line number respectively.\n\
16617\n\
16618Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16619are set to the address of the last breakpoint listed unless the command\n\
16620is prefixed with \"server \".\n\n\
c906108c 16621Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16622breakpoint set."));
c906108c 16623
1a966eab
AC
16624 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16625Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16626The \"Type\" column indicates one of:\n\
16627\tbreakpoint - normal breakpoint\n\
16628\twatchpoint - watchpoint\n\
16629\tlongjmp - internal breakpoint used to step through longjmp()\n\
16630\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16631\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16632\tfinish - internal breakpoint used by the \"finish\" command\n\
16633The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16634the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16635breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16636address and file/line number respectively.\n\
16637\n\
16638Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16639are set to the address of the last breakpoint listed unless the command\n\
16640is prefixed with \"server \".\n\n\
c906108c 16641Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16642breakpoint set."),
c906108c
SS
16643 &maintenanceinfolist);
16644
44feb3ce
TT
16645 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16646Set catchpoints to catch events."),
16647 &catch_cmdlist, "catch ",
16648 0/*allow-unknown*/, &cmdlist);
16649
16650 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16651Set temporary catchpoints to catch events."),
16652 &tcatch_cmdlist, "tcatch ",
16653 0/*allow-unknown*/, &cmdlist);
16654
44feb3ce
TT
16655 add_catch_command ("fork", _("Catch calls to fork."),
16656 catch_fork_command_1,
a96d9b2e 16657 NULL,
44feb3ce
TT
16658 (void *) (uintptr_t) catch_fork_permanent,
16659 (void *) (uintptr_t) catch_fork_temporary);
16660 add_catch_command ("vfork", _("Catch calls to vfork."),
16661 catch_fork_command_1,
a96d9b2e 16662 NULL,
44feb3ce
TT
16663 (void *) (uintptr_t) catch_vfork_permanent,
16664 (void *) (uintptr_t) catch_vfork_temporary);
16665 add_catch_command ("exec", _("Catch calls to exec."),
16666 catch_exec_command_1,
a96d9b2e
SDJ
16667 NULL,
16668 CATCH_PERMANENT,
16669 CATCH_TEMPORARY);
edcc5120
TT
16670 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16671Usage: catch load [REGEX]\n\
16672If REGEX is given, only stop for libraries matching the regular expression."),
16673 catch_load_command_1,
16674 NULL,
16675 CATCH_PERMANENT,
16676 CATCH_TEMPORARY);
16677 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16678Usage: catch unload [REGEX]\n\
16679If REGEX is given, only stop for libraries matching the regular expression."),
16680 catch_unload_command_1,
16681 NULL,
16682 CATCH_PERMANENT,
16683 CATCH_TEMPORARY);
a96d9b2e
SDJ
16684 add_catch_command ("syscall", _("\
16685Catch system calls by their names and/or numbers.\n\
16686Arguments say which system calls to catch. If no arguments\n\
16687are given, every system call will be caught.\n\
16688Arguments, if given, should be one or more system call names\n\
16689(if your system supports that), or system call numbers."),
16690 catch_syscall_command_1,
16691 catch_syscall_completer,
44feb3ce
TT
16692 CATCH_PERMANENT,
16693 CATCH_TEMPORARY);
c5aa993b 16694
1bedd215
AC
16695 c = add_com ("watch", class_breakpoint, watch_command, _("\
16696Set a watchpoint for an expression.\n\
06a64a0b 16697Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16698A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16699an expression changes.\n\
16700If -l or -location is given, this evaluates EXPRESSION and watches\n\
16701the memory to which it refers."));
65d12d83 16702 set_cmd_completer (c, expression_completer);
c906108c 16703
1bedd215
AC
16704 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16705Set a read watchpoint for an expression.\n\
06a64a0b 16706Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16707A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16708an expression is read.\n\
16709If -l or -location is given, this evaluates EXPRESSION and watches\n\
16710the memory to which it refers."));
65d12d83 16711 set_cmd_completer (c, expression_completer);
c906108c 16712
1bedd215
AC
16713 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16714Set a watchpoint for an expression.\n\
06a64a0b 16715Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16716A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16717an expression is either read or written.\n\
16718If -l or -location is given, this evaluates EXPRESSION and watches\n\
16719the memory to which it refers."));
65d12d83 16720 set_cmd_completer (c, expression_completer);
c906108c 16721
d77f58be 16722 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16723Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16724
920d2a44
AC
16725 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16726 respond to changes - contrary to the description. */
85c07804
AC
16727 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16728 &can_use_hw_watchpoints, _("\
16729Set debugger's willingness to use watchpoint hardware."), _("\
16730Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16731If zero, gdb will not use hardware for new watchpoints, even if\n\
16732such is available. (However, any hardware watchpoints that were\n\
16733created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16734hardware.)"),
16735 NULL,
920d2a44 16736 show_can_use_hw_watchpoints,
85c07804 16737 &setlist, &showlist);
c906108c
SS
16738
16739 can_use_hw_watchpoints = 1;
fa8d40ab 16740
1042e4c0
SS
16741 /* Tracepoint manipulation commands. */
16742
16743 c = add_com ("trace", class_breakpoint, trace_command, _("\
16744Set a tracepoint at specified line or function.\n\
16745\n"
16746BREAK_ARGS_HELP ("trace") "\n\
16747Do \"help tracepoints\" for info on other tracepoint commands."));
16748 set_cmd_completer (c, location_completer);
16749
16750 add_com_alias ("tp", "trace", class_alias, 0);
16751 add_com_alias ("tr", "trace", class_alias, 1);
16752 add_com_alias ("tra", "trace", class_alias, 1);
16753 add_com_alias ("trac", "trace", class_alias, 1);
16754
7a697b8d
SS
16755 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16756Set a fast tracepoint at specified line or function.\n\
16757\n"
16758BREAK_ARGS_HELP ("ftrace") "\n\
16759Do \"help tracepoints\" for info on other tracepoint commands."));
16760 set_cmd_completer (c, location_completer);
16761
0fb4aa4b
PA
16762 c = add_com ("strace", class_breakpoint, strace_command, _("\
16763Set a static tracepoint at specified line, function or marker.\n\
16764\n\
16765strace [LOCATION] [if CONDITION]\n\
16766LOCATION may be a line number, function name, \"*\" and an address,\n\
16767or -m MARKER_ID.\n\
16768If a line number is specified, probe the marker at start of code\n\
16769for that line. If a function is specified, probe the marker at start\n\
16770of code for that function. If an address is specified, probe the marker\n\
16771at that exact address. If a marker id is specified, probe the marker\n\
16772with that name. With no LOCATION, uses current execution address of\n\
16773the selected stack frame.\n\
16774Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16775This collects arbitrary user data passed in the probe point call to the\n\
16776tracing library. You can inspect it when analyzing the trace buffer,\n\
16777by printing the $_sdata variable like any other convenience variable.\n\
16778\n\
16779CONDITION is a boolean expression.\n\
16780\n\
d41c0fc8
PA
16781Multiple tracepoints at one place are permitted, and useful if their\n\
16782conditions are different.\n\
0fb4aa4b
PA
16783\n\
16784Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16785Do \"help tracepoints\" for info on other tracepoint commands."));
16786 set_cmd_completer (c, location_completer);
16787
1042e4c0 16788 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16789Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16790Convenience variable \"$tpnum\" contains the number of the\n\
16791last tracepoint set."));
16792
16793 add_info_alias ("tp", "tracepoints", 1);
16794
16795 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16796Delete specified tracepoints.\n\
16797Arguments are tracepoint numbers, separated by spaces.\n\
16798No argument means delete all tracepoints."),
16799 &deletelist);
7e20dfcd 16800 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16801
16802 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16803Disable specified tracepoints.\n\
16804Arguments are tracepoint numbers, separated by spaces.\n\
16805No argument means disable all tracepoints."),
16806 &disablelist);
16807 deprecate_cmd (c, "disable");
16808
16809 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16810Enable specified tracepoints.\n\
16811Arguments are tracepoint numbers, separated by spaces.\n\
16812No argument means enable all tracepoints."),
16813 &enablelist);
16814 deprecate_cmd (c, "enable");
16815
16816 add_com ("passcount", class_trace, trace_pass_command, _("\
16817Set the passcount for a tracepoint.\n\
16818The trace will end when the tracepoint has been passed 'count' times.\n\
16819Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16820if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16821
6149aea9
PA
16822 add_prefix_cmd ("save", class_breakpoint, save_command,
16823 _("Save breakpoint definitions as a script."),
16824 &save_cmdlist, "save ",
16825 0/*allow-unknown*/, &cmdlist);
16826
16827 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16828Save current breakpoint definitions as a script.\n\
cce7e648 16829This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16830catchpoints, tracepoints). Use the 'source' command in another debug\n\
16831session to restore them."),
16832 &save_cmdlist);
16833 set_cmd_completer (c, filename_completer);
16834
16835 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16836Save current tracepoint definitions as a script.\n\
6149aea9
PA
16837Use the 'source' command in another debug session to restore them."),
16838 &save_cmdlist);
1042e4c0
SS
16839 set_cmd_completer (c, filename_completer);
16840
6149aea9
PA
16841 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16842 deprecate_cmd (c, "save tracepoints");
16843
1bedd215 16844 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16845Breakpoint specific settings\n\
16846Configure various breakpoint-specific variables such as\n\
1bedd215 16847pending breakpoint behavior"),
fa8d40ab
JJ
16848 &breakpoint_set_cmdlist, "set breakpoint ",
16849 0/*allow-unknown*/, &setlist);
1bedd215 16850 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16851Breakpoint specific settings\n\
16852Configure various breakpoint-specific variables such as\n\
1bedd215 16853pending breakpoint behavior"),
fa8d40ab
JJ
16854 &breakpoint_show_cmdlist, "show breakpoint ",
16855 0/*allow-unknown*/, &showlist);
16856
7915a72c
AC
16857 add_setshow_auto_boolean_cmd ("pending", no_class,
16858 &pending_break_support, _("\
16859Set debugger's behavior regarding pending breakpoints."), _("\
16860Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16861If on, an unrecognized breakpoint location will cause gdb to create a\n\
16862pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16863an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16864user-query to see if a pending breakpoint should be created."),
2c5b56ce 16865 NULL,
920d2a44 16866 show_pending_break_support,
6e1d7d6c
AC
16867 &breakpoint_set_cmdlist,
16868 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16869
16870 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16871
16872 add_setshow_boolean_cmd ("auto-hw", no_class,
16873 &automatic_hardware_breakpoints, _("\
16874Set automatic usage of hardware breakpoints."), _("\
16875Show automatic usage of hardware breakpoints."), _("\
16876If set, the debugger will automatically use hardware breakpoints for\n\
16877breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16878a warning will be emitted for such breakpoints."),
16879 NULL,
16880 show_automatic_hardware_breakpoints,
16881 &breakpoint_set_cmdlist,
16882 &breakpoint_show_cmdlist);
74960c60 16883
a25a5a45
PA
16884 add_setshow_boolean_cmd ("always-inserted", class_support,
16885 &always_inserted_mode, _("\
74960c60
VP
16886Set mode for inserting breakpoints."), _("\
16887Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16888When this mode is on, breakpoints are inserted immediately as soon as\n\
16889they're created, kept inserted even when execution stops, and removed\n\
16890only when the user deletes them. When this mode is off (the default),\n\
16891breakpoints are inserted only when execution continues, and removed\n\
16892when execution stops."),
72d0e2c5
YQ
16893 NULL,
16894 &show_always_inserted_mode,
16895 &breakpoint_set_cmdlist,
16896 &breakpoint_show_cmdlist);
f1310107 16897
b775012e
LM
16898 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16899 condition_evaluation_enums,
16900 &condition_evaluation_mode_1, _("\
16901Set mode of breakpoint condition evaluation."), _("\
16902Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16903When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16904evaluated on the host's side by GDB. When it is set to \"target\",\n\
16905breakpoint conditions will be downloaded to the target (if the target\n\
16906supports such feature) and conditions will be evaluated on the target's side.\n\
16907If this is set to \"auto\" (default), this will be automatically set to\n\
16908\"target\" if it supports condition evaluation, otherwise it will\n\
16909be set to \"gdb\""),
16910 &set_condition_evaluation_mode,
16911 &show_condition_evaluation_mode,
16912 &breakpoint_set_cmdlist,
16913 &breakpoint_show_cmdlist);
16914
f1310107
TJB
16915 add_com ("break-range", class_breakpoint, break_range_command, _("\
16916Set a breakpoint for an address range.\n\
16917break-range START-LOCATION, END-LOCATION\n\
16918where START-LOCATION and END-LOCATION can be one of the following:\n\
16919 LINENUM, for that line in the current file,\n\
16920 FILE:LINENUM, for that line in that file,\n\
16921 +OFFSET, for that number of lines after the current line\n\
16922 or the start of the range\n\
16923 FUNCTION, for the first line in that function,\n\
16924 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16925 *ADDRESS, for the instruction at that address.\n\
16926\n\
16927The breakpoint will stop execution of the inferior whenever it executes\n\
16928an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16929range (including START-LOCATION and END-LOCATION)."));
16930
e7e0cddf
SS
16931 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16932Set a dynamic printf at specified line or function.\n\
16933dprintf location,format string,arg1,arg2,...\n\
16934location may be a line number, function name, or \"*\" and an address.\n\
16935If a line number is specified, break at start of code for that line.\n\
0e4777df 16936If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16937 set_cmd_completer (c, location_completer);
16938
16939 add_setshow_enum_cmd ("dprintf-style", class_support,
16940 dprintf_style_enums, &dprintf_style, _("\
16941Set the style of usage for dynamic printf."), _("\
16942Show the style of usage for dynamic printf."), _("\
16943This setting chooses how GDB will do a dynamic printf.\n\
16944If the value is \"gdb\", then the printing is done by GDB to its own\n\
16945console, as with the \"printf\" command.\n\
16946If the value is \"call\", the print is done by calling a function in your\n\
16947program; by default printf(), but you can choose a different function or\n\
16948output stream by setting dprintf-function and dprintf-channel."),
16949 update_dprintf_commands, NULL,
16950 &setlist, &showlist);
16951
16952 dprintf_function = xstrdup ("printf");
16953 add_setshow_string_cmd ("dprintf-function", class_support,
16954 &dprintf_function, _("\
16955Set the function to use for dynamic printf"), _("\
16956Show the function to use for dynamic printf"), NULL,
16957 update_dprintf_commands, NULL,
16958 &setlist, &showlist);
16959
16960 dprintf_channel = xstrdup ("");
16961 add_setshow_string_cmd ("dprintf-channel", class_support,
16962 &dprintf_channel, _("\
16963Set the channel to use for dynamic printf"), _("\
16964Show the channel to use for dynamic printf"), NULL,
16965 update_dprintf_commands, NULL,
16966 &setlist, &showlist);
16967
d3ce09f5
SS
16968 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16969 &disconnected_dprintf, _("\
16970Set whether dprintf continues after GDB disconnects."), _("\
16971Show whether dprintf continues after GDB disconnects."), _("\
16972Use this to let dprintf commands continue to hit and produce output\n\
16973even if GDB disconnects or detaches from the target."),
16974 NULL,
16975 NULL,
16976 &setlist, &showlist);
16977
16978 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16979agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16980(target agent only) This is useful for formatted output in user-defined commands."));
16981
765dc015 16982 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16983
16984 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16985 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16986}
This page took 3.448383 seconds and 4 git commands to generate.