]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
New gdbarch functions: dtrace_parse_probe_argument, dtrace_probe_is_enabled, dtrace_e...
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
32d0add0 3 Copyright (C) 1986-2015 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"
5589af0e 69#include "interps.h"
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 breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305/* Internal breakpoints class type. */
06edf0c0 306static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
307
308/* Momentary breakpoints class type. */
06edf0c0
PA
309static struct breakpoint_ops momentary_breakpoint_ops;
310
e2e4d78b
JK
311/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312static struct breakpoint_ops longjmp_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347static char *dprintf_function = "";
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357static char *dprintf_channel = "";
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
a25a5a45
PA
474 if (always_inserted_mode)
475 {
476 /* The user wants breakpoints inserted even if all threads
477 are stopped. */
478 return 1;
479 }
480
b57bacec
PA
481 if (threads_are_executing ())
482 return 1;
a25a5a45
PA
483 }
484 return 0;
33e5cbd6 485}
765dc015 486
b775012e
LM
487static const char condition_evaluation_both[] = "host or target";
488
489/* Modes for breakpoint condition evaluation. */
490static const char condition_evaluation_auto[] = "auto";
491static const char condition_evaluation_host[] = "host";
492static const char condition_evaluation_target[] = "target";
493static const char *const condition_evaluation_enums[] = {
494 condition_evaluation_auto,
495 condition_evaluation_host,
496 condition_evaluation_target,
497 NULL
498};
499
500/* Global that holds the current mode for breakpoint condition evaluation. */
501static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
502
503/* Global that we use to display information to the user (gets its value from
504 condition_evaluation_mode_1. */
505static const char *condition_evaluation_mode = condition_evaluation_auto;
506
507/* Translate a condition evaluation mode MODE into either "host"
508 or "target". This is used mostly to translate from "auto" to the
509 real setting that is being used. It returns the translated
510 evaluation mode. */
511
512static const char *
513translate_condition_evaluation_mode (const char *mode)
514{
515 if (mode == condition_evaluation_auto)
516 {
517 if (target_supports_evaluation_of_breakpoint_conditions ())
518 return condition_evaluation_target;
519 else
520 return condition_evaluation_host;
521 }
522 else
523 return mode;
524}
525
526/* Discovers what condition_evaluation_auto translates to. */
527
528static const char *
529breakpoint_condition_evaluation_mode (void)
530{
531 return translate_condition_evaluation_mode (condition_evaluation_mode);
532}
533
534/* Return true if GDB should evaluate breakpoint conditions or false
535 otherwise. */
536
537static int
538gdb_evaluates_breakpoint_condition_p (void)
539{
540 const char *mode = breakpoint_condition_evaluation_mode ();
541
542 return (mode == condition_evaluation_host);
543}
544
a14ed312 545void _initialize_breakpoint (void);
c906108c 546
c906108c
SS
547/* Are we executing breakpoint commands? */
548static int executing_breakpoint_commands;
549
c02f5703
MS
550/* Are overlay event breakpoints enabled? */
551static int overlay_events_enabled;
552
e09342b5
TJB
553/* See description in breakpoint.h. */
554int target_exact_watchpoints = 0;
555
c906108c 556/* Walk the following statement or block through all breakpoints.
e5dd4106 557 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 558 current breakpoint. */
c906108c 559
5c44784c 560#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 561
5c44784c
JM
562#define ALL_BREAKPOINTS_SAFE(B,TMP) \
563 for (B = breakpoint_chain; \
564 B ? (TMP=B->next, 1): 0; \
565 B = TMP)
c906108c 566
4a64f543
MS
567/* Similar iterator for the low-level breakpoints. SAFE variant is
568 not provided so update_global_location_list must not be called
569 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 570
876fa593
JK
571#define ALL_BP_LOCATIONS(B,BP_TMP) \
572 for (BP_TMP = bp_location; \
573 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
574 BP_TMP++)
7cc221ef 575
b775012e
LM
576/* Iterates through locations with address ADDRESS for the currently selected
577 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
578 to where the loop should start from.
579 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
580 appropriate location to start with. */
581
582#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
583 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
584 BP_LOCP_TMP = BP_LOCP_START; \
585 BP_LOCP_START \
586 && (BP_LOCP_TMP < bp_location + bp_location_count \
587 && (*BP_LOCP_TMP)->address == ADDRESS); \
588 BP_LOCP_TMP++)
589
1042e4c0
SS
590/* Iterator for tracepoints only. */
591
592#define ALL_TRACEPOINTS(B) \
593 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 594 if (is_tracepoint (B))
1042e4c0 595
7cc221ef 596/* Chains of all breakpoints defined. */
c906108c
SS
597
598struct breakpoint *breakpoint_chain;
599
876fa593
JK
600/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
601
602static struct bp_location **bp_location;
603
604/* Number of elements of BP_LOCATION. */
605
606static unsigned bp_location_count;
607
4a64f543
MS
608/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
609 ADDRESS for the current elements of BP_LOCATION which get a valid
610 result from bp_location_has_shadow. You can use it for roughly
611 limiting the subrange of BP_LOCATION to scan for shadow bytes for
612 an address you need to read. */
876fa593
JK
613
614static CORE_ADDR bp_location_placed_address_before_address_max;
615
4a64f543
MS
616/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
617 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
618 BP_LOCATION which get a valid result from bp_location_has_shadow.
619 You can use it for roughly limiting the subrange of BP_LOCATION to
620 scan for shadow bytes for an address you need to read. */
876fa593
JK
621
622static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 623
4a64f543
MS
624/* The locations that no longer correspond to any breakpoint, unlinked
625 from bp_location array, but for which a hit may still be reported
626 by a target. */
20874c92
VP
627VEC(bp_location_p) *moribund_locations = NULL;
628
c906108c
SS
629/* Number of last breakpoint made. */
630
95a42b64
TT
631static int breakpoint_count;
632
86b17b60
PA
633/* The value of `breakpoint_count' before the last command that
634 created breakpoints. If the last (break-like) command created more
635 than one breakpoint, then the difference between BREAKPOINT_COUNT
636 and PREV_BREAKPOINT_COUNT is more than one. */
637static int prev_breakpoint_count;
c906108c 638
1042e4c0
SS
639/* Number of last tracepoint made. */
640
95a42b64 641static int tracepoint_count;
1042e4c0 642
6149aea9
PA
643static struct cmd_list_element *breakpoint_set_cmdlist;
644static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 645struct cmd_list_element *save_cmdlist;
6149aea9 646
468d015d
JJ
647/* Return whether a breakpoint is an active enabled breakpoint. */
648static int
649breakpoint_enabled (struct breakpoint *b)
650{
0d381245 651 return (b->enable_state == bp_enabled);
468d015d
JJ
652}
653
c906108c
SS
654/* Set breakpoint count to NUM. */
655
95a42b64 656static void
fba45db2 657set_breakpoint_count (int num)
c906108c 658{
86b17b60 659 prev_breakpoint_count = breakpoint_count;
c906108c 660 breakpoint_count = num;
4fa62494 661 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
662}
663
86b17b60
PA
664/* Used by `start_rbreak_breakpoints' below, to record the current
665 breakpoint count before "rbreak" creates any breakpoint. */
666static int rbreak_start_breakpoint_count;
667
95a42b64
TT
668/* Called at the start an "rbreak" command to record the first
669 breakpoint made. */
86b17b60 670
95a42b64
TT
671void
672start_rbreak_breakpoints (void)
673{
86b17b60 674 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
675}
676
677/* Called at the end of an "rbreak" command to record the last
678 breakpoint made. */
86b17b60 679
95a42b64
TT
680void
681end_rbreak_breakpoints (void)
682{
86b17b60 683 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
684}
685
4a64f543 686/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
687
688void
fba45db2 689clear_breakpoint_hit_counts (void)
c906108c
SS
690{
691 struct breakpoint *b;
692
693 ALL_BREAKPOINTS (b)
694 b->hit_count = 0;
695}
696
9add0f1b
TT
697/* Allocate a new counted_command_line with reference count of 1.
698 The new structure owns COMMANDS. */
699
700static struct counted_command_line *
701alloc_counted_command_line (struct command_line *commands)
702{
703 struct counted_command_line *result
704 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 705
9add0f1b
TT
706 result->refc = 1;
707 result->commands = commands;
708 return result;
709}
710
711/* Increment reference count. This does nothing if CMD is NULL. */
712
713static void
714incref_counted_command_line (struct counted_command_line *cmd)
715{
716 if (cmd)
717 ++cmd->refc;
718}
719
720/* Decrement reference count. If the reference count reaches 0,
721 destroy the counted_command_line. Sets *CMDP to NULL. This does
722 nothing if *CMDP is NULL. */
723
724static void
725decref_counted_command_line (struct counted_command_line **cmdp)
726{
727 if (*cmdp)
728 {
729 if (--(*cmdp)->refc == 0)
730 {
731 free_command_lines (&(*cmdp)->commands);
732 xfree (*cmdp);
733 }
734 *cmdp = NULL;
735 }
736}
737
738/* A cleanup function that calls decref_counted_command_line. */
739
740static void
741do_cleanup_counted_command_line (void *arg)
742{
743 decref_counted_command_line (arg);
744}
745
746/* Create a cleanup that calls decref_counted_command_line on the
747 argument. */
748
749static struct cleanup *
750make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
751{
752 return make_cleanup (do_cleanup_counted_command_line, cmdp);
753}
754
c906108c 755\f
48cb2d85
VP
756/* Return the breakpoint with the specified number, or NULL
757 if the number does not refer to an existing breakpoint. */
758
759struct breakpoint *
760get_breakpoint (int num)
761{
762 struct breakpoint *b;
763
764 ALL_BREAKPOINTS (b)
765 if (b->number == num)
766 return b;
767
768 return NULL;
769}
5c44784c 770
c906108c 771\f
adc36818 772
b775012e
LM
773/* Mark locations as "conditions have changed" in case the target supports
774 evaluating conditions on its side. */
775
776static void
777mark_breakpoint_modified (struct breakpoint *b)
778{
779 struct bp_location *loc;
780
781 /* This is only meaningful if the target is
782 evaluating conditions and if the user has
783 opted for condition evaluation on the target's
784 side. */
785 if (gdb_evaluates_breakpoint_condition_p ()
786 || !target_supports_evaluation_of_breakpoint_conditions ())
787 return;
788
789 if (!is_breakpoint (b))
790 return;
791
792 for (loc = b->loc; loc; loc = loc->next)
793 loc->condition_changed = condition_modified;
794}
795
796/* Mark location as "conditions have changed" in case the target supports
797 evaluating conditions on its side. */
798
799static void
800mark_breakpoint_location_modified (struct bp_location *loc)
801{
802 /* This is only meaningful if the target is
803 evaluating conditions and if the user has
804 opted for condition evaluation on the target's
805 side. */
806 if (gdb_evaluates_breakpoint_condition_p ()
807 || !target_supports_evaluation_of_breakpoint_conditions ())
808
809 return;
810
811 if (!is_breakpoint (loc->owner))
812 return;
813
814 loc->condition_changed = condition_modified;
815}
816
817/* Sets the condition-evaluation mode using the static global
818 condition_evaluation_mode. */
819
820static void
821set_condition_evaluation_mode (char *args, int from_tty,
822 struct cmd_list_element *c)
823{
b775012e
LM
824 const char *old_mode, *new_mode;
825
826 if ((condition_evaluation_mode_1 == condition_evaluation_target)
827 && !target_supports_evaluation_of_breakpoint_conditions ())
828 {
829 condition_evaluation_mode_1 = condition_evaluation_mode;
830 warning (_("Target does not support breakpoint condition evaluation.\n"
831 "Using host evaluation mode instead."));
832 return;
833 }
834
835 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
836 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
837
abf1152a
JK
838 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
839 settings was "auto". */
840 condition_evaluation_mode = condition_evaluation_mode_1;
841
b775012e
LM
842 /* Only update the mode if the user picked a different one. */
843 if (new_mode != old_mode)
844 {
845 struct bp_location *loc, **loc_tmp;
846 /* If the user switched to a different evaluation mode, we
847 need to synch the changes with the target as follows:
848
849 "host" -> "target": Send all (valid) conditions to the target.
850 "target" -> "host": Remove all the conditions from the target.
851 */
852
b775012e
LM
853 if (new_mode == condition_evaluation_target)
854 {
855 /* Mark everything modified and synch conditions with the
856 target. */
857 ALL_BP_LOCATIONS (loc, loc_tmp)
858 mark_breakpoint_location_modified (loc);
859 }
860 else
861 {
862 /* Manually mark non-duplicate locations to synch conditions
863 with the target. We do this to remove all the conditions the
864 target knows about. */
865 ALL_BP_LOCATIONS (loc, loc_tmp)
866 if (is_breakpoint (loc->owner) && loc->inserted)
867 loc->needs_update = 1;
868 }
869
870 /* Do the update. */
44702360 871 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
872 }
873
874 return;
875}
876
877/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
878 what "auto" is translating to. */
879
880static void
881show_condition_evaluation_mode (struct ui_file *file, int from_tty,
882 struct cmd_list_element *c, const char *value)
883{
884 if (condition_evaluation_mode == condition_evaluation_auto)
885 fprintf_filtered (file,
886 _("Breakpoint condition evaluation "
887 "mode is %s (currently %s).\n"),
888 value,
889 breakpoint_condition_evaluation_mode ());
890 else
891 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
892 value);
893}
894
895/* A comparison function for bp_location AP and BP that is used by
896 bsearch. This comparison function only cares about addresses, unlike
897 the more general bp_location_compare function. */
898
899static int
900bp_location_compare_addrs (const void *ap, const void *bp)
901{
902 struct bp_location *a = *(void **) ap;
903 struct bp_location *b = *(void **) bp;
904
905 if (a->address == b->address)
906 return 0;
907 else
908 return ((a->address > b->address) - (a->address < b->address));
909}
910
911/* Helper function to skip all bp_locations with addresses
912 less than ADDRESS. It returns the first bp_location that
913 is greater than or equal to ADDRESS. If none is found, just
914 return NULL. */
915
916static struct bp_location **
917get_first_locp_gte_addr (CORE_ADDR address)
918{
919 struct bp_location dummy_loc;
920 struct bp_location *dummy_locp = &dummy_loc;
921 struct bp_location **locp_found = NULL;
922
923 /* Initialize the dummy location's address field. */
924 memset (&dummy_loc, 0, sizeof (struct bp_location));
925 dummy_loc.address = address;
926
927 /* Find a close match to the first location at ADDRESS. */
928 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
929 sizeof (struct bp_location **),
930 bp_location_compare_addrs);
931
932 /* Nothing was found, nothing left to do. */
933 if (locp_found == NULL)
934 return NULL;
935
936 /* We may have found a location that is at ADDRESS but is not the first in the
937 location's list. Go backwards (if possible) and locate the first one. */
938 while ((locp_found - 1) >= bp_location
939 && (*(locp_found - 1))->address == address)
940 locp_found--;
941
942 return locp_found;
943}
944
adc36818
PM
945void
946set_breakpoint_condition (struct breakpoint *b, char *exp,
947 int from_tty)
948{
3a5c3e22
PA
949 xfree (b->cond_string);
950 b->cond_string = NULL;
adc36818 951
3a5c3e22 952 if (is_watchpoint (b))
adc36818 953 {
3a5c3e22
PA
954 struct watchpoint *w = (struct watchpoint *) b;
955
956 xfree (w->cond_exp);
957 w->cond_exp = NULL;
958 }
959 else
960 {
961 struct bp_location *loc;
962
963 for (loc = b->loc; loc; loc = loc->next)
964 {
965 xfree (loc->cond);
966 loc->cond = NULL;
b775012e
LM
967
968 /* No need to free the condition agent expression
969 bytecode (if we have one). We will handle this
970 when we go through update_global_location_list. */
3a5c3e22 971 }
adc36818 972 }
adc36818
PM
973
974 if (*exp == 0)
975 {
976 if (from_tty)
977 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
978 }
979 else
980 {
bbc13ae3 981 const char *arg = exp;
cc59ec59 982
adc36818
PM
983 /* I don't know if it matters whether this is the string the user
984 typed in or the decompiled expression. */
985 b->cond_string = xstrdup (arg);
986 b->condition_not_parsed = 0;
987
988 if (is_watchpoint (b))
989 {
3a5c3e22
PA
990 struct watchpoint *w = (struct watchpoint *) b;
991
adc36818
PM
992 innermost_block = NULL;
993 arg = exp;
1bb9788d 994 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
995 if (*arg)
996 error (_("Junk at end of expression"));
3a5c3e22 997 w->cond_exp_valid_block = innermost_block;
adc36818
PM
998 }
999 else
1000 {
3a5c3e22
PA
1001 struct bp_location *loc;
1002
adc36818
PM
1003 for (loc = b->loc; loc; loc = loc->next)
1004 {
1005 arg = exp;
1006 loc->cond =
1bb9788d
TT
1007 parse_exp_1 (&arg, loc->address,
1008 block_for_pc (loc->address), 0);
adc36818
PM
1009 if (*arg)
1010 error (_("Junk at end of expression"));
1011 }
1012 }
1013 }
b775012e
LM
1014 mark_breakpoint_modified (b);
1015
8d3788bd 1016 observer_notify_breakpoint_modified (b);
adc36818
PM
1017}
1018
d55637df
TT
1019/* Completion for the "condition" command. */
1020
1021static VEC (char_ptr) *
6f937416
PA
1022condition_completer (struct cmd_list_element *cmd,
1023 const char *text, const char *word)
d55637df 1024{
6f937416 1025 const char *space;
d55637df 1026
6f937416
PA
1027 text = skip_spaces_const (text);
1028 space = skip_to_space_const (text);
d55637df
TT
1029 if (*space == '\0')
1030 {
1031 int len;
1032 struct breakpoint *b;
1033 VEC (char_ptr) *result = NULL;
1034
1035 if (text[0] == '$')
1036 {
1037 /* We don't support completion of history indices. */
1038 if (isdigit (text[1]))
1039 return NULL;
1040 return complete_internalvar (&text[1]);
1041 }
1042
1043 /* We're completing the breakpoint number. */
1044 len = strlen (text);
1045
1046 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1047 {
1048 char number[50];
1049
1050 xsnprintf (number, sizeof (number), "%d", b->number);
1051
1052 if (strncmp (number, text, len) == 0)
1053 VEC_safe_push (char_ptr, result, xstrdup (number));
1054 }
d55637df
TT
1055
1056 return result;
1057 }
1058
1059 /* We're completing the expression part. */
6f937416 1060 text = skip_spaces_const (space);
d55637df
TT
1061 return expression_completer (cmd, text, word);
1062}
1063
c906108c
SS
1064/* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066static void
fba45db2 1067condition_command (char *arg, int from_tty)
c906108c 1068{
52f0bd74 1069 struct breakpoint *b;
c906108c 1070 char *p;
52f0bd74 1071 int bnum;
c906108c
SS
1072
1073 if (arg == 0)
e2e0b3e5 1074 error_no_arg (_("breakpoint number"));
c906108c
SS
1075
1076 p = arg;
1077 bnum = get_number (&p);
5c44784c 1078 if (bnum == 0)
8a3fe4f8 1079 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1080
1081 ALL_BREAKPOINTS (b)
1082 if (b->number == bnum)
2f069f6f 1083 {
6dddc817
DE
1084 /* Check if this breakpoint has a "stop" method implemented in an
1085 extension language. This method and conditions entered into GDB
1086 from the CLI are mutually exclusive. */
1087 const struct extension_language_defn *extlang
1088 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1089
1090 if (extlang != NULL)
1091 {
1092 error (_("Only one stop condition allowed. There is currently"
1093 " a %s stop condition defined for this breakpoint."),
1094 ext_lang_capitalized_name (extlang));
1095 }
2566ad2d 1096 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1097
1098 if (is_breakpoint (b))
44702360 1099 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1100
2f069f6f
JB
1101 return;
1102 }
c906108c 1103
8a3fe4f8 1104 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1105}
1106
a7bdde9e
VP
1107/* Check that COMMAND do not contain commands that are suitable
1108 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1109 Throw if any such commands is found. */
1110
a7bdde9e
VP
1111static void
1112check_no_tracepoint_commands (struct command_line *commands)
1113{
1114 struct command_line *c;
cc59ec59 1115
a7bdde9e
VP
1116 for (c = commands; c; c = c->next)
1117 {
1118 int i;
1119
1120 if (c->control_type == while_stepping_control)
3e43a32a
MS
1121 error (_("The 'while-stepping' command can "
1122 "only be used for tracepoints"));
a7bdde9e
VP
1123
1124 for (i = 0; i < c->body_count; ++i)
1125 check_no_tracepoint_commands ((c->body_list)[i]);
1126
1127 /* Not that command parsing removes leading whitespace and comment
4a64f543 1128 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1129 command directly. */
1130 if (strstr (c->line, "collect ") == c->line)
1131 error (_("The 'collect' command can only be used for tracepoints"));
1132
51661e93
VP
1133 if (strstr (c->line, "teval ") == c->line)
1134 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1135 }
1136}
1137
d77f58be
SS
1138/* Encapsulate tests for different types of tracepoints. */
1139
d9b3f62e
PA
1140static int
1141is_tracepoint_type (enum bptype type)
1142{
1143 return (type == bp_tracepoint
1144 || type == bp_fast_tracepoint
1145 || type == bp_static_tracepoint);
1146}
1147
a7bdde9e 1148int
d77f58be 1149is_tracepoint (const struct breakpoint *b)
a7bdde9e 1150{
d9b3f62e 1151 return is_tracepoint_type (b->type);
a7bdde9e 1152}
d9b3f62e 1153
e5dd4106 1154/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1155 breakpoint. This function will throw an exception if a problem is
1156 found. */
48cb2d85 1157
95a42b64
TT
1158static void
1159validate_commands_for_breakpoint (struct breakpoint *b,
1160 struct command_line *commands)
48cb2d85 1161{
d77f58be 1162 if (is_tracepoint (b))
a7bdde9e 1163 {
c9a6ce02 1164 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1165 struct command_line *c;
1166 struct command_line *while_stepping = 0;
c9a6ce02
PA
1167
1168 /* Reset the while-stepping step count. The previous commands
1169 might have included a while-stepping action, while the new
1170 ones might not. */
1171 t->step_count = 0;
1172
1173 /* We need to verify that each top-level element of commands is
1174 valid for tracepoints, that there's at most one
1175 while-stepping element, and that the while-stepping's body
1176 has valid tracing commands excluding nested while-stepping.
1177 We also need to validate the tracepoint action line in the
1178 context of the tracepoint --- validate_actionline actually
1179 has side effects, like setting the tracepoint's
1180 while-stepping STEP_COUNT, in addition to checking if the
1181 collect/teval actions parse and make sense in the
1182 tracepoint's context. */
a7bdde9e
VP
1183 for (c = commands; c; c = c->next)
1184 {
a7bdde9e
VP
1185 if (c->control_type == while_stepping_control)
1186 {
1187 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for fast tracepoint"));
0fb4aa4b 1190 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1191 error (_("The 'while-stepping' command "
1192 "cannot be used for static tracepoint"));
a7bdde9e
VP
1193
1194 if (while_stepping)
3e43a32a
MS
1195 error (_("The 'while-stepping' command "
1196 "can be used only once"));
a7bdde9e
VP
1197 else
1198 while_stepping = c;
1199 }
c9a6ce02
PA
1200
1201 validate_actionline (c->line, b);
a7bdde9e
VP
1202 }
1203 if (while_stepping)
1204 {
1205 struct command_line *c2;
1206
1207 gdb_assert (while_stepping->body_count == 1);
1208 c2 = while_stepping->body_list[0];
1209 for (; c2; c2 = c2->next)
1210 {
a7bdde9e
VP
1211 if (c2->control_type == while_stepping_control)
1212 error (_("The 'while-stepping' command cannot be nested"));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 check_no_tracepoint_commands (commands);
1219 }
95a42b64
TT
1220}
1221
0fb4aa4b
PA
1222/* Return a vector of all the static tracepoints set at ADDR. The
1223 caller is responsible for releasing the vector. */
1224
1225VEC(breakpoint_p) *
1226static_tracepoints_here (CORE_ADDR addr)
1227{
1228 struct breakpoint *b;
1229 VEC(breakpoint_p) *found = 0;
1230 struct bp_location *loc;
1231
1232 ALL_BREAKPOINTS (b)
1233 if (b->type == bp_static_tracepoint)
1234 {
1235 for (loc = b->loc; loc; loc = loc->next)
1236 if (loc->address == addr)
1237 VEC_safe_push(breakpoint_p, found, b);
1238 }
1239
1240 return found;
1241}
1242
95a42b64 1243/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1244 validate that only allowed commands are included. */
95a42b64
TT
1245
1246void
4a64f543
MS
1247breakpoint_set_commands (struct breakpoint *b,
1248 struct command_line *commands)
95a42b64
TT
1249{
1250 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1251
9add0f1b
TT
1252 decref_counted_command_line (&b->commands);
1253 b->commands = alloc_counted_command_line (commands);
8d3788bd 1254 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1255}
1256
45a43567
TT
1257/* Set the internal `silent' flag on the breakpoint. Note that this
1258 is not the same as the "silent" that may appear in the breakpoint's
1259 commands. */
1260
1261void
1262breakpoint_set_silent (struct breakpoint *b, int silent)
1263{
1264 int old_silent = b->silent;
1265
1266 b->silent = silent;
1267 if (old_silent != silent)
8d3788bd 1268 observer_notify_breakpoint_modified (b);
45a43567
TT
1269}
1270
1271/* Set the thread for this breakpoint. If THREAD is -1, make the
1272 breakpoint work for any thread. */
1273
1274void
1275breakpoint_set_thread (struct breakpoint *b, int thread)
1276{
1277 int old_thread = b->thread;
1278
1279 b->thread = thread;
1280 if (old_thread != thread)
8d3788bd 1281 observer_notify_breakpoint_modified (b);
45a43567
TT
1282}
1283
1284/* Set the task for this breakpoint. If TASK is 0, make the
1285 breakpoint work for any task. */
1286
1287void
1288breakpoint_set_task (struct breakpoint *b, int task)
1289{
1290 int old_task = b->task;
1291
1292 b->task = task;
1293 if (old_task != task)
8d3788bd 1294 observer_notify_breakpoint_modified (b);
45a43567
TT
1295}
1296
95a42b64
TT
1297void
1298check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1299{
1300 struct breakpoint *b = closure;
cc59ec59 1301
6f937416 1302 validate_actionline (line, b);
a7bdde9e
VP
1303}
1304
95a42b64
TT
1305/* A structure used to pass information through
1306 map_breakpoint_numbers. */
1307
1308struct commands_info
1309{
1310 /* True if the command was typed at a tty. */
1311 int from_tty;
86b17b60
PA
1312
1313 /* The breakpoint range spec. */
1314 char *arg;
1315
95a42b64
TT
1316 /* Non-NULL if the body of the commands are being read from this
1317 already-parsed command. */
1318 struct command_line *control;
86b17b60 1319
95a42b64
TT
1320 /* The command lines read from the user, or NULL if they have not
1321 yet been read. */
1322 struct counted_command_line *cmd;
1323};
1324
1325/* A callback for map_breakpoint_numbers that sets the commands for
1326 commands_command. */
1327
c906108c 1328static void
95a42b64 1329do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1330{
95a42b64 1331 struct commands_info *info = data;
c906108c 1332
95a42b64
TT
1333 if (info->cmd == NULL)
1334 {
1335 struct command_line *l;
5c44784c 1336
95a42b64
TT
1337 if (info->control != NULL)
1338 l = copy_command_lines (info->control->body_list[0]);
1339 else
86b17b60
PA
1340 {
1341 struct cleanup *old_chain;
1342 char *str;
c5aa993b 1343
3e43a32a
MS
1344 str = xstrprintf (_("Type commands for breakpoint(s) "
1345 "%s, one per line."),
86b17b60
PA
1346 info->arg);
1347
1348 old_chain = make_cleanup (xfree, str);
1349
1350 l = read_command_lines (str,
1351 info->from_tty, 1,
d77f58be 1352 (is_tracepoint (b)
86b17b60
PA
1353 ? check_tracepoint_command : 0),
1354 b);
1355
1356 do_cleanups (old_chain);
1357 }
a7bdde9e 1358
95a42b64
TT
1359 info->cmd = alloc_counted_command_line (l);
1360 }
1361
1362 /* If a breakpoint was on the list more than once, we don't need to
1363 do anything. */
1364 if (b->commands != info->cmd)
1365 {
1366 validate_commands_for_breakpoint (b, info->cmd->commands);
1367 incref_counted_command_line (info->cmd);
1368 decref_counted_command_line (&b->commands);
1369 b->commands = info->cmd;
8d3788bd 1370 observer_notify_breakpoint_modified (b);
c5aa993b 1371 }
95a42b64
TT
1372}
1373
1374static void
4a64f543
MS
1375commands_command_1 (char *arg, int from_tty,
1376 struct command_line *control)
95a42b64
TT
1377{
1378 struct cleanup *cleanups;
1379 struct commands_info info;
1380
1381 info.from_tty = from_tty;
1382 info.control = control;
1383 info.cmd = NULL;
1384 /* If we read command lines from the user, then `info' will hold an
1385 extra reference to the commands that we must clean up. */
1386 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1387
1388 if (arg == NULL || !*arg)
1389 {
86b17b60 1390 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1391 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1392 breakpoint_count);
95a42b64
TT
1393 else if (breakpoint_count > 0)
1394 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1395 else
1396 {
1397 /* So that we don't try to free the incoming non-NULL
1398 argument in the cleanup below. Mapping breakpoint
1399 numbers will fail in this case. */
1400 arg = NULL;
1401 }
95a42b64 1402 }
9766ced4
SS
1403 else
1404 /* The command loop has some static state, so we need to preserve
1405 our argument. */
1406 arg = xstrdup (arg);
86b17b60
PA
1407
1408 if (arg != NULL)
1409 make_cleanup (xfree, arg);
1410
1411 info.arg = arg;
95a42b64
TT
1412
1413 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1414
1415 if (info.cmd == NULL)
1416 error (_("No breakpoints specified."));
1417
1418 do_cleanups (cleanups);
1419}
1420
1421static void
1422commands_command (char *arg, int from_tty)
1423{
1424 commands_command_1 (arg, from_tty, NULL);
c906108c 1425}
40c03ae8
EZ
1426
1427/* Like commands_command, but instead of reading the commands from
1428 input stream, takes them from an already parsed command structure.
1429
1430 This is used by cli-script.c to DTRT with breakpoint commands
1431 that are part of if and while bodies. */
1432enum command_control_type
1433commands_from_control_command (char *arg, struct command_line *cmd)
1434{
95a42b64
TT
1435 commands_command_1 (arg, 0, cmd);
1436 return simple_control;
40c03ae8 1437}
876fa593
JK
1438
1439/* Return non-zero if BL->TARGET_INFO contains valid information. */
1440
1441static int
1442bp_location_has_shadow (struct bp_location *bl)
1443{
1444 if (bl->loc_type != bp_loc_software_breakpoint)
1445 return 0;
1446 if (!bl->inserted)
1447 return 0;
1448 if (bl->target_info.shadow_len == 0)
e5dd4106 1449 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1450 return 0;
1451 return 1;
1452}
1453
9d497a19
PA
1454/* Update BUF, which is LEN bytes read from the target address
1455 MEMADDR, by replacing a memory breakpoint with its shadowed
1456 contents.
1457
1458 If READBUF is not NULL, this buffer must not overlap with the of
1459 the breakpoint location's shadow_contents buffer. Otherwise, a
1460 failed assertion internal error will be raised. */
1461
1462static void
1463one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1464 const gdb_byte *writebuf_org,
1465 ULONGEST memaddr, LONGEST len,
1466 struct bp_target_info *target_info,
1467 struct gdbarch *gdbarch)
1468{
1469 /* Now do full processing of the found relevant range of elements. */
1470 CORE_ADDR bp_addr = 0;
1471 int bp_size = 0;
1472 int bptoffset = 0;
1473
1474 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1475 current_program_space->aspace, 0))
1476 {
1477 /* The breakpoint is inserted in a different address space. */
1478 return;
1479 }
1480
1481 /* Addresses and length of the part of the breakpoint that
1482 we need to copy. */
1483 bp_addr = target_info->placed_address;
1484 bp_size = target_info->shadow_len;
1485
1486 if (bp_addr + bp_size <= memaddr)
1487 {
1488 /* The breakpoint is entirely before the chunk of memory we are
1489 reading. */
1490 return;
1491 }
1492
1493 if (bp_addr >= memaddr + len)
1494 {
1495 /* The breakpoint is entirely after the chunk of memory we are
1496 reading. */
1497 return;
1498 }
1499
1500 /* Offset within shadow_contents. */
1501 if (bp_addr < memaddr)
1502 {
1503 /* Only copy the second part of the breakpoint. */
1504 bp_size -= memaddr - bp_addr;
1505 bptoffset = memaddr - bp_addr;
1506 bp_addr = memaddr;
1507 }
1508
1509 if (bp_addr + bp_size > memaddr + len)
1510 {
1511 /* Only copy the first part of the breakpoint. */
1512 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1513 }
1514
1515 if (readbuf != NULL)
1516 {
1517 /* Verify that the readbuf buffer does not overlap with the
1518 shadow_contents buffer. */
1519 gdb_assert (target_info->shadow_contents >= readbuf + len
1520 || readbuf >= (target_info->shadow_contents
1521 + target_info->shadow_len));
1522
1523 /* Update the read buffer with this inserted breakpoint's
1524 shadow. */
1525 memcpy (readbuf + bp_addr - memaddr,
1526 target_info->shadow_contents + bptoffset, bp_size);
1527 }
1528 else
1529 {
1530 const unsigned char *bp;
0d5ed153
MR
1531 CORE_ADDR addr = target_info->reqstd_address;
1532 int placed_size;
9d497a19
PA
1533
1534 /* Update the shadow with what we want to write to memory. */
1535 memcpy (target_info->shadow_contents + bptoffset,
1536 writebuf_org + bp_addr - memaddr, bp_size);
1537
1538 /* Determine appropriate breakpoint contents and size for this
1539 address. */
0d5ed153 1540 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1541
1542 /* Update the final write buffer with this inserted
1543 breakpoint's INSN. */
1544 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1545 }
1546}
1547
8defab1a 1548/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1549 by replacing any memory breakpoints with their shadowed contents.
1550
35c63cd8
JB
1551 If READBUF is not NULL, this buffer must not overlap with any of
1552 the breakpoint location's shadow_contents buffers. Otherwise,
1553 a failed assertion internal error will be raised.
1554
876fa593 1555 The range of shadowed area by each bp_location is:
35df4500
TJB
1556 bl->address - bp_location_placed_address_before_address_max
1557 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1558 The range we were requested to resolve shadows for is:
1559 memaddr ... memaddr + len
1560 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1561 memaddr + len <= (bl->address
1562 - bp_location_placed_address_before_address_max)
876fa593 1563 and:
35df4500 1564 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1565
8defab1a 1566void
f0ba3972
PA
1567breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1568 const gdb_byte *writebuf_org,
1569 ULONGEST memaddr, LONGEST len)
c906108c 1570{
4a64f543
MS
1571 /* Left boundary, right boundary and median element of our binary
1572 search. */
876fa593 1573 unsigned bc_l, bc_r, bc;
9d497a19 1574 size_t i;
876fa593 1575
4a64f543
MS
1576 /* Find BC_L which is a leftmost element which may affect BUF
1577 content. It is safe to report lower value but a failure to
1578 report higher one. */
876fa593
JK
1579
1580 bc_l = 0;
1581 bc_r = bp_location_count;
1582 while (bc_l + 1 < bc_r)
1583 {
35df4500 1584 struct bp_location *bl;
876fa593
JK
1585
1586 bc = (bc_l + bc_r) / 2;
35df4500 1587 bl = bp_location[bc];
876fa593 1588
4a64f543
MS
1589 /* Check first BL->ADDRESS will not overflow due to the added
1590 constant. Then advance the left boundary only if we are sure
1591 the BC element can in no way affect the BUF content (MEMADDR
1592 to MEMADDR + LEN range).
876fa593 1593
4a64f543
MS
1594 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1595 offset so that we cannot miss a breakpoint with its shadow
1596 range tail still reaching MEMADDR. */
c5aa993b 1597
35df4500
TJB
1598 if ((bl->address + bp_location_shadow_len_after_address_max
1599 >= bl->address)
1600 && (bl->address + bp_location_shadow_len_after_address_max
1601 <= memaddr))
876fa593
JK
1602 bc_l = bc;
1603 else
1604 bc_r = bc;
1605 }
1606
128070bb
PA
1607 /* Due to the binary search above, we need to make sure we pick the
1608 first location that's at BC_L's address. E.g., if there are
1609 multiple locations at the same address, BC_L may end up pointing
1610 at a duplicate location, and miss the "master"/"inserted"
1611 location. Say, given locations L1, L2 and L3 at addresses A and
1612 B:
1613
1614 L1@A, L2@A, L3@B, ...
1615
1616 BC_L could end up pointing at location L2, while the "master"
1617 location could be L1. Since the `loc->inserted' flag is only set
1618 on "master" locations, we'd forget to restore the shadow of L1
1619 and L2. */
1620 while (bc_l > 0
1621 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1622 bc_l--;
1623
876fa593
JK
1624 /* Now do full processing of the found relevant range of elements. */
1625
1626 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1627 {
35df4500 1628 struct bp_location *bl = bp_location[bc];
876fa593
JK
1629 CORE_ADDR bp_addr = 0;
1630 int bp_size = 0;
1631 int bptoffset = 0;
1632
35df4500
TJB
1633 /* bp_location array has BL->OWNER always non-NULL. */
1634 if (bl->owner->type == bp_none)
8a3fe4f8 1635 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1636 bl->owner->number);
ffce0d52 1637
e5dd4106 1638 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1639 content. */
1640
35df4500
TJB
1641 if (bl->address >= bp_location_placed_address_before_address_max
1642 && memaddr + len <= (bl->address
1643 - bp_location_placed_address_before_address_max))
876fa593
JK
1644 break;
1645
35df4500 1646 if (!bp_location_has_shadow (bl))
c5aa993b 1647 continue;
6c95b8df 1648
9d497a19
PA
1649 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1650 memaddr, len, &bl->target_info, bl->gdbarch);
1651 }
c906108c 1652}
9d497a19 1653
c906108c 1654\f
c5aa993b 1655
b775012e
LM
1656/* Return true if BPT is either a software breakpoint or a hardware
1657 breakpoint. */
1658
1659int
1660is_breakpoint (const struct breakpoint *bpt)
1661{
1662 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1663 || bpt->type == bp_hardware_breakpoint
1664 || bpt->type == bp_dprintf);
b775012e
LM
1665}
1666
60e1c644
PA
1667/* Return true if BPT is of any hardware watchpoint kind. */
1668
a5606eee 1669static int
d77f58be 1670is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1671{
1672 return (bpt->type == bp_hardware_watchpoint
1673 || bpt->type == bp_read_watchpoint
1674 || bpt->type == bp_access_watchpoint);
1675}
7270d8f2 1676
60e1c644
PA
1677/* Return true if BPT is of any watchpoint kind, hardware or
1678 software. */
1679
3a5c3e22 1680int
d77f58be 1681is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1682{
1683 return (is_hardware_watchpoint (bpt)
1684 || bpt->type == bp_watchpoint);
1685}
1686
3a5c3e22
PA
1687/* Returns true if the current thread and its running state are safe
1688 to evaluate or update watchpoint B. Watchpoints on local
1689 expressions need to be evaluated in the context of the thread that
1690 was current when the watchpoint was created, and, that thread needs
1691 to be stopped to be able to select the correct frame context.
1692 Watchpoints on global expressions can be evaluated on any thread,
1693 and in any state. It is presently left to the target allowing
1694 memory accesses when threads are running. */
f6bc2008
PA
1695
1696static int
3a5c3e22 1697watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1698{
d0d8b0c6
JK
1699 return (b->base.pspace == current_program_space
1700 && (ptid_equal (b->watchpoint_thread, null_ptid)
1701 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1702 && !is_executing (inferior_ptid))));
f6bc2008
PA
1703}
1704
d0fb5eae
JK
1705/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1706 associated bp_watchpoint_scope breakpoint. */
1707
1708static void
3a5c3e22 1709watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1710{
3a5c3e22 1711 struct breakpoint *b = &w->base;
d0fb5eae
JK
1712
1713 if (b->related_breakpoint != b)
1714 {
1715 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1716 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1717 b->related_breakpoint->disposition = disp_del_at_next_stop;
1718 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1719 b->related_breakpoint = b;
1720 }
1721 b->disposition = disp_del_at_next_stop;
1722}
1723
bb9d5f81
PP
1724/* Extract a bitfield value from value VAL using the bit parameters contained in
1725 watchpoint W. */
1726
1727static struct value *
1728extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1729{
1730 struct value *bit_val;
1731
1732 if (val == NULL)
1733 return NULL;
1734
1735 bit_val = allocate_value (value_type (val));
1736
1737 unpack_value_bitfield (bit_val,
1738 w->val_bitpos,
1739 w->val_bitsize,
1740 value_contents_for_printing (val),
1741 value_offset (val),
1742 val);
1743
1744 return bit_val;
1745}
1746
567e1b4e
JB
1747/* Assuming that B is a watchpoint:
1748 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1749 - Evaluate expression and store the result in B->val
567e1b4e
JB
1750 - Evaluate the condition if there is one, and store the result
1751 in b->loc->cond.
a5606eee
VP
1752 - Update the list of values that must be watched in B->loc.
1753
4a64f543
MS
1754 If the watchpoint disposition is disp_del_at_next_stop, then do
1755 nothing. If this is local watchpoint that is out of scope, delete
1756 it.
1757
1758 Even with `set breakpoint always-inserted on' the watchpoints are
1759 removed + inserted on each stop here. Normal breakpoints must
1760 never be removed because they might be missed by a running thread
1761 when debugging in non-stop mode. On the other hand, hardware
1762 watchpoints (is_hardware_watchpoint; processed here) are specific
1763 to each LWP since they are stored in each LWP's hardware debug
1764 registers. Therefore, such LWP must be stopped first in order to
1765 be able to modify its hardware watchpoints.
1766
1767 Hardware watchpoints must be reset exactly once after being
1768 presented to the user. It cannot be done sooner, because it would
1769 reset the data used to present the watchpoint hit to the user. And
1770 it must not be done later because it could display the same single
1771 watchpoint hit during multiple GDB stops. Note that the latter is
1772 relevant only to the hardware watchpoint types bp_read_watchpoint
1773 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1774 not user-visible - its hit is suppressed if the memory content has
1775 not changed.
1776
1777 The following constraints influence the location where we can reset
1778 hardware watchpoints:
1779
1780 * target_stopped_by_watchpoint and target_stopped_data_address are
1781 called several times when GDB stops.
1782
1783 [linux]
1784 * Multiple hardware watchpoints can be hit at the same time,
1785 causing GDB to stop. GDB only presents one hardware watchpoint
1786 hit at a time as the reason for stopping, and all the other hits
1787 are presented later, one after the other, each time the user
1788 requests the execution to be resumed. Execution is not resumed
1789 for the threads still having pending hit event stored in
1790 LWP_INFO->STATUS. While the watchpoint is already removed from
1791 the inferior on the first stop the thread hit event is kept being
1792 reported from its cached value by linux_nat_stopped_data_address
1793 until the real thread resume happens after the watchpoint gets
1794 presented and thus its LWP_INFO->STATUS gets reset.
1795
1796 Therefore the hardware watchpoint hit can get safely reset on the
1797 watchpoint removal from inferior. */
a79d3c27 1798
b40ce68a 1799static void
3a5c3e22 1800update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1801{
a5606eee 1802 int within_current_scope;
a5606eee 1803 struct frame_id saved_frame_id;
66076460 1804 int frame_saved;
a5606eee 1805
f6bc2008
PA
1806 /* If this is a local watchpoint, we only want to check if the
1807 watchpoint frame is in scope if the current thread is the thread
1808 that was used to create the watchpoint. */
1809 if (!watchpoint_in_thread_scope (b))
1810 return;
1811
3a5c3e22 1812 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1813 return;
1814
66076460 1815 frame_saved = 0;
a5606eee
VP
1816
1817 /* Determine if the watchpoint is within scope. */
1818 if (b->exp_valid_block == NULL)
1819 within_current_scope = 1;
1820 else
1821 {
b5db5dfc
UW
1822 struct frame_info *fi = get_current_frame ();
1823 struct gdbarch *frame_arch = get_frame_arch (fi);
1824 CORE_ADDR frame_pc = get_frame_pc (fi);
1825
1826 /* If we're in a function epilogue, unwinding may not work
1827 properly, so do not attempt to recreate locations at this
1828 point. See similar comments in watchpoint_check. */
1829 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1830 return;
66076460
DJ
1831
1832 /* Save the current frame's ID so we can restore it after
1833 evaluating the watchpoint expression on its own frame. */
1834 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1835 took a frame parameter, so that we didn't have to change the
1836 selected frame. */
1837 frame_saved = 1;
1838 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1839
a5606eee
VP
1840 fi = frame_find_by_id (b->watchpoint_frame);
1841 within_current_scope = (fi != NULL);
1842 if (within_current_scope)
1843 select_frame (fi);
1844 }
1845
b5db5dfc
UW
1846 /* We don't free locations. They are stored in the bp_location array
1847 and update_global_location_list will eventually delete them and
1848 remove breakpoints if needed. */
3a5c3e22 1849 b->base.loc = NULL;
b5db5dfc 1850
a5606eee
VP
1851 if (within_current_scope && reparse)
1852 {
bbc13ae3 1853 const char *s;
d63d0675 1854
a5606eee
VP
1855 if (b->exp)
1856 {
1857 xfree (b->exp);
1858 b->exp = NULL;
1859 }
d63d0675 1860 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1861 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1862 /* If the meaning of expression itself changed, the old value is
1863 no longer relevant. We don't want to report a watchpoint hit
1864 to the user when the old value and the new value may actually
1865 be completely different objects. */
1866 value_free (b->val);
fa4727a6
DJ
1867 b->val = NULL;
1868 b->val_valid = 0;
60e1c644
PA
1869
1870 /* Note that unlike with breakpoints, the watchpoint's condition
1871 expression is stored in the breakpoint object, not in the
1872 locations (re)created below. */
3a5c3e22 1873 if (b->base.cond_string != NULL)
60e1c644
PA
1874 {
1875 if (b->cond_exp != NULL)
1876 {
1877 xfree (b->cond_exp);
1878 b->cond_exp = NULL;
1879 }
1880
3a5c3e22 1881 s = b->base.cond_string;
1bb9788d 1882 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1883 }
a5606eee 1884 }
a5606eee
VP
1885
1886 /* If we failed to parse the expression, for example because
1887 it refers to a global variable in a not-yet-loaded shared library,
1888 don't try to insert watchpoint. We don't automatically delete
1889 such watchpoint, though, since failure to parse expression
1890 is different from out-of-scope watchpoint. */
e8369a73 1891 if (!target_has_execution)
2d134ed3
PA
1892 {
1893 /* Without execution, memory can't change. No use to try and
1894 set watchpoint locations. The watchpoint will be reset when
1895 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1896 if (!can_use_hw_watchpoints)
1897 {
1898 if (b->base.ops->works_in_software_mode (&b->base))
1899 b->base.type = bp_watchpoint;
1900 else
638aa5a1
AB
1901 error (_("Can't set read/access watchpoint when "
1902 "hardware watchpoints are disabled."));
e8369a73 1903 }
2d134ed3
PA
1904 }
1905 else if (within_current_scope && b->exp)
a5606eee 1906 {
0cf6dd15 1907 int pc = 0;
fa4727a6 1908 struct value *val_chain, *v, *result, *next;
2d134ed3 1909 struct program_space *frame_pspace;
a5606eee 1910
3a1115a0 1911 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1912
a5606eee
VP
1913 /* Avoid setting b->val if it's already set. The meaning of
1914 b->val is 'the last value' user saw, and we should update
1915 it only if we reported that last value to user. As it
9c06b0b4
TJB
1916 happens, the code that reports it updates b->val directly.
1917 We don't keep track of the memory value for masked
1918 watchpoints. */
3a5c3e22 1919 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1920 {
bb9d5f81
PP
1921 if (b->val_bitsize != 0)
1922 {
1923 v = extract_bitfield_from_watchpoint_value (b, v);
1924 if (v != NULL)
1925 release_value (v);
1926 }
fa4727a6
DJ
1927 b->val = v;
1928 b->val_valid = 1;
1929 }
a5606eee 1930
2d134ed3
PA
1931 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1932
a5606eee 1933 /* Look at each value on the value chain. */
9fa40276 1934 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1935 {
1936 /* If it's a memory location, and GDB actually needed
1937 its contents to evaluate the expression, then we
fa4727a6
DJ
1938 must watch it. If the first value returned is
1939 still lazy, that means an error occurred reading it;
1940 watch it anyway in case it becomes readable. */
a5606eee 1941 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1942 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1943 {
1944 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1945
a5606eee
VP
1946 /* We only watch structs and arrays if user asked
1947 for it explicitly, never if they just happen to
1948 appear in the middle of some value chain. */
fa4727a6 1949 if (v == result
a5606eee
VP
1950 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1951 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1952 {
1953 CORE_ADDR addr;
744a8059 1954 int type;
a5606eee 1955 struct bp_location *loc, **tmp;
bb9d5f81
PP
1956 int bitpos = 0, bitsize = 0;
1957
1958 if (value_bitsize (v) != 0)
1959 {
1960 /* Extract the bit parameters out from the bitfield
1961 sub-expression. */
1962 bitpos = value_bitpos (v);
1963 bitsize = value_bitsize (v);
1964 }
1965 else if (v == result && b->val_bitsize != 0)
1966 {
1967 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1968 lvalue whose bit parameters are saved in the fields
1969 VAL_BITPOS and VAL_BITSIZE. */
1970 bitpos = b->val_bitpos;
1971 bitsize = b->val_bitsize;
1972 }
a5606eee 1973
42ae5230 1974 addr = value_address (v);
bb9d5f81
PP
1975 if (bitsize != 0)
1976 {
1977 /* Skip the bytes that don't contain the bitfield. */
1978 addr += bitpos / 8;
1979 }
1980
a5606eee 1981 type = hw_write;
3a5c3e22 1982 if (b->base.type == bp_read_watchpoint)
a5606eee 1983 type = hw_read;
3a5c3e22 1984 else if (b->base.type == bp_access_watchpoint)
a5606eee 1985 type = hw_access;
3a5c3e22
PA
1986
1987 loc = allocate_bp_location (&b->base);
1988 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1989 ;
1990 *tmp = loc;
a6d9a66e 1991 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1992
1993 loc->pspace = frame_pspace;
a5606eee 1994 loc->address = addr;
bb9d5f81
PP
1995
1996 if (bitsize != 0)
1997 {
1998 /* Just cover the bytes that make up the bitfield. */
1999 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2000 }
2001 else
2002 loc->length = TYPE_LENGTH (value_type (v));
2003
a5606eee
VP
2004 loc->watchpoint_type = type;
2005 }
2006 }
9fa40276
TJB
2007 }
2008
2009 /* Change the type of breakpoint between hardware assisted or
2010 an ordinary watchpoint depending on the hardware support
2011 and free hardware slots. REPARSE is set when the inferior
2012 is started. */
a9634178 2013 if (reparse)
9fa40276 2014 {
e09342b5 2015 int reg_cnt;
9fa40276
TJB
2016 enum bp_loc_type loc_type;
2017 struct bp_location *bl;
a5606eee 2018
a9634178 2019 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2020
2021 if (reg_cnt)
9fa40276
TJB
2022 {
2023 int i, target_resources_ok, other_type_used;
a1398e0c 2024 enum bptype type;
9fa40276 2025
a9634178
TJB
2026 /* Use an exact watchpoint when there's only one memory region to be
2027 watched, and only one debug register is needed to watch it. */
2028 b->exact = target_exact_watchpoints && reg_cnt == 1;
2029
9fa40276 2030 /* We need to determine how many resources are already
e09342b5
TJB
2031 used for all other hardware watchpoints plus this one
2032 to see if we still have enough resources to also fit
a1398e0c
PA
2033 this watchpoint in as well. */
2034
2035 /* If this is a software watchpoint, we try to turn it
2036 to a hardware one -- count resources as if B was of
2037 hardware watchpoint type. */
2038 type = b->base.type;
2039 if (type == bp_watchpoint)
2040 type = bp_hardware_watchpoint;
2041
2042 /* This watchpoint may or may not have been placed on
2043 the list yet at this point (it won't be in the list
2044 if we're trying to create it for the first time,
2045 through watch_command), so always account for it
2046 manually. */
2047
2048 /* Count resources used by all watchpoints except B. */
2049 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2050
2051 /* Add in the resources needed for B. */
2052 i += hw_watchpoint_use_count (&b->base);
2053
2054 target_resources_ok
2055 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2056 if (target_resources_ok <= 0)
a9634178 2057 {
3a5c3e22 2058 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2059
2060 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2061 error (_("Target does not support this type of "
2062 "hardware watchpoint."));
9c06b0b4
TJB
2063 else if (target_resources_ok < 0 && !sw_mode)
2064 error (_("There are not enough available hardware "
2065 "resources for this watchpoint."));
a1398e0c
PA
2066
2067 /* Downgrade to software watchpoint. */
2068 b->base.type = bp_watchpoint;
2069 }
2070 else
2071 {
2072 /* If this was a software watchpoint, we've just
2073 found we have enough resources to turn it to a
2074 hardware watchpoint. Otherwise, this is a
2075 nop. */
2076 b->base.type = type;
a9634178 2077 }
9fa40276 2078 }
3a5c3e22 2079 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2080 {
2081 if (!can_use_hw_watchpoints)
2082 error (_("Can't set read/access watchpoint when "
2083 "hardware watchpoints are disabled."));
2084 else
2085 error (_("Expression cannot be implemented with "
2086 "read/access watchpoint."));
2087 }
9fa40276 2088 else
3a5c3e22 2089 b->base.type = bp_watchpoint;
9fa40276 2090
3a5c3e22 2091 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2092 : bp_loc_hardware_watchpoint);
3a5c3e22 2093 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2094 bl->loc_type = loc_type;
2095 }
2096
2097 for (v = val_chain; v; v = next)
2098 {
a5606eee
VP
2099 next = value_next (v);
2100 if (v != b->val)
2101 value_free (v);
2102 }
2103
c7437ca6
PA
2104 /* If a software watchpoint is not watching any memory, then the
2105 above left it without any location set up. But,
2106 bpstat_stop_status requires a location to be able to report
2107 stops, so make sure there's at least a dummy one. */
3a5c3e22 2108 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2109 {
3a5c3e22
PA
2110 struct breakpoint *base = &b->base;
2111 base->loc = allocate_bp_location (base);
2112 base->loc->pspace = frame_pspace;
2113 base->loc->address = -1;
2114 base->loc->length = -1;
2115 base->loc->watchpoint_type = -1;
c7437ca6 2116 }
a5606eee
VP
2117 }
2118 else if (!within_current_scope)
7270d8f2 2119 {
ac74f770
MS
2120 printf_filtered (_("\
2121Watchpoint %d deleted because the program has left the block\n\
2122in which its expression is valid.\n"),
3a5c3e22 2123 b->base.number);
d0fb5eae 2124 watchpoint_del_at_next_stop (b);
7270d8f2 2125 }
a5606eee
VP
2126
2127 /* Restore the selected frame. */
66076460
DJ
2128 if (frame_saved)
2129 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2130}
2131
a5606eee 2132
74960c60 2133/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2134 inserted in the inferior. We don't differentiate the type of BL's owner
2135 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2136 breakpoint_ops is not defined, because in insert_bp_location,
2137 tracepoint's insert_location will not be called. */
74960c60 2138static int
35df4500 2139should_be_inserted (struct bp_location *bl)
74960c60 2140{
35df4500 2141 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2142 return 0;
2143
35df4500 2144 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2145 return 0;
2146
35df4500 2147 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2148 return 0;
2149
f8eba3c6
TT
2150 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2151 return 0;
2152
56710373
PA
2153 /* This is set for example, when we're attached to the parent of a
2154 vfork, and have detached from the child. The child is running
2155 free, and we expect it to do an exec or exit, at which point the
2156 OS makes the parent schedulable again (and the target reports
2157 that the vfork is done). Until the child is done with the shared
2158 memory region, do not insert breakpoints in the parent, otherwise
2159 the child could still trip on the parent's breakpoints. Since
2160 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2161 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2162 return 0;
2163
31e77af2
PA
2164 /* Don't insert a breakpoint if we're trying to step past its
2165 location. */
2166 if ((bl->loc_type == bp_loc_software_breakpoint
2167 || bl->loc_type == bp_loc_hardware_breakpoint)
2168 && stepping_past_instruction_at (bl->pspace->aspace,
2169 bl->address))
e558d7c1
PA
2170 {
2171 if (debug_infrun)
2172 {
2173 fprintf_unfiltered (gdb_stdlog,
2174 "infrun: skipping breakpoint: "
2175 "stepping past insn at: %s\n",
2176 paddress (bl->gdbarch, bl->address));
2177 }
2178 return 0;
2179 }
31e77af2 2180
963f9c80
PA
2181 /* Don't insert watchpoints if we're trying to step past the
2182 instruction that triggered one. */
2183 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2184 && stepping_past_nonsteppable_watchpoint ())
2185 {
2186 if (debug_infrun)
2187 {
2188 fprintf_unfiltered (gdb_stdlog,
2189 "infrun: stepping past non-steppable watchpoint. "
2190 "skipping watchpoint at %s:%d\n",
2191 paddress (bl->gdbarch, bl->address),
2192 bl->length);
2193 }
2194 return 0;
2195 }
2196
74960c60
VP
2197 return 1;
2198}
2199
934709f0
PW
2200/* Same as should_be_inserted but does the check assuming
2201 that the location is not duplicated. */
2202
2203static int
2204unduplicated_should_be_inserted (struct bp_location *bl)
2205{
2206 int result;
2207 const int save_duplicate = bl->duplicate;
2208
2209 bl->duplicate = 0;
2210 result = should_be_inserted (bl);
2211 bl->duplicate = save_duplicate;
2212 return result;
2213}
2214
b775012e
LM
2215/* Parses a conditional described by an expression COND into an
2216 agent expression bytecode suitable for evaluation
2217 by the bytecode interpreter. Return NULL if there was
2218 any error during parsing. */
2219
2220static struct agent_expr *
2221parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2222{
2223 struct agent_expr *aexpr = NULL;
b775012e
LM
2224 volatile struct gdb_exception ex;
2225
2226 if (!cond)
2227 return NULL;
2228
2229 /* We don't want to stop processing, so catch any errors
2230 that may show up. */
2231 TRY_CATCH (ex, RETURN_MASK_ERROR)
2232 {
2233 aexpr = gen_eval_for_expr (scope, cond);
2234 }
2235
2236 if (ex.reason < 0)
2237 {
2238 /* If we got here, it means the condition could not be parsed to a valid
2239 bytecode expression and thus can't be evaluated on the target's side.
2240 It's no use iterating through the conditions. */
2241 return NULL;
2242 }
2243
2244 /* We have a valid agent expression. */
2245 return aexpr;
2246}
2247
2248/* Based on location BL, create a list of breakpoint conditions to be
2249 passed on to the target. If we have duplicated locations with different
2250 conditions, we will add such conditions to the list. The idea is that the
2251 target will evaluate the list of conditions and will only notify GDB when
2252 one of them is true. */
2253
2254static void
2255build_target_condition_list (struct bp_location *bl)
2256{
2257 struct bp_location **locp = NULL, **loc2p;
2258 int null_condition_or_parse_error = 0;
2259 int modified = bl->needs_update;
2260 struct bp_location *loc;
2261
8b4f3082
PA
2262 /* Release conditions left over from a previous insert. */
2263 VEC_free (agent_expr_p, bl->target_info.conditions);
2264
b775012e
LM
2265 /* This is only meaningful if the target is
2266 evaluating conditions and if the user has
2267 opted for condition evaluation on the target's
2268 side. */
2269 if (gdb_evaluates_breakpoint_condition_p ()
2270 || !target_supports_evaluation_of_breakpoint_conditions ())
2271 return;
2272
2273 /* Do a first pass to check for locations with no assigned
2274 conditions or conditions that fail to parse to a valid agent expression
2275 bytecode. If any of these happen, then it's no use to send conditions
2276 to the target since this location will always trigger and generate a
2277 response back to GDB. */
2278 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2279 {
2280 loc = (*loc2p);
2281 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2282 {
2283 if (modified)
2284 {
2285 struct agent_expr *aexpr;
2286
2287 /* Re-parse the conditions since something changed. In that
2288 case we already freed the condition bytecodes (see
2289 force_breakpoint_reinsertion). We just
2290 need to parse the condition to bytecodes again. */
2291 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2292 loc->cond_bytecode = aexpr;
2293
2294 /* Check if we managed to parse the conditional expression
2295 correctly. If not, we will not send this condition
2296 to the target. */
2297 if (aexpr)
2298 continue;
2299 }
2300
2301 /* If we have a NULL bytecode expression, it means something
2302 went wrong or we have a null condition expression. */
2303 if (!loc->cond_bytecode)
2304 {
2305 null_condition_or_parse_error = 1;
2306 break;
2307 }
2308 }
2309 }
2310
2311 /* If any of these happened, it means we will have to evaluate the conditions
2312 for the location's address on gdb's side. It is no use keeping bytecodes
2313 for all the other duplicate locations, thus we free all of them here.
2314
2315 This is so we have a finer control over which locations' conditions are
2316 being evaluated by GDB or the remote stub. */
2317 if (null_condition_or_parse_error)
2318 {
2319 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2320 {
2321 loc = (*loc2p);
2322 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2323 {
2324 /* Only go as far as the first NULL bytecode is
2325 located. */
2326 if (!loc->cond_bytecode)
2327 return;
2328
2329 free_agent_expr (loc->cond_bytecode);
2330 loc->cond_bytecode = NULL;
2331 }
2332 }
2333 }
2334
2335 /* No NULL conditions or failed bytecode generation. Build a condition list
2336 for this location's address. */
2337 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2338 {
2339 loc = (*loc2p);
2340 if (loc->cond
2341 && is_breakpoint (loc->owner)
2342 && loc->pspace->num == bl->pspace->num
2343 && loc->owner->enable_state == bp_enabled
2344 && loc->enabled)
2345 /* Add the condition to the vector. This will be used later to send the
2346 conditions to the target. */
2347 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2348 loc->cond_bytecode);
2349 }
2350
2351 return;
2352}
2353
d3ce09f5
SS
2354/* Parses a command described by string CMD into an agent expression
2355 bytecode suitable for evaluation by the bytecode interpreter.
2356 Return NULL if there was any error during parsing. */
2357
2358static struct agent_expr *
2359parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2360{
2361 struct cleanup *old_cleanups = 0;
2362 struct expression *expr, **argvec;
2363 struct agent_expr *aexpr = NULL;
d3ce09f5 2364 volatile struct gdb_exception ex;
bbc13ae3
KS
2365 const char *cmdrest;
2366 const char *format_start, *format_end;
d3ce09f5
SS
2367 struct format_piece *fpieces;
2368 int nargs;
2369 struct gdbarch *gdbarch = get_current_arch ();
2370
2371 if (!cmd)
2372 return NULL;
2373
2374 cmdrest = cmd;
2375
2376 if (*cmdrest == ',')
2377 ++cmdrest;
bbc13ae3 2378 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2379
2380 if (*cmdrest++ != '"')
2381 error (_("No format string following the location"));
2382
2383 format_start = cmdrest;
2384
2385 fpieces = parse_format_string (&cmdrest);
2386
2387 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2388
2389 format_end = cmdrest;
2390
2391 if (*cmdrest++ != '"')
2392 error (_("Bad format string, non-terminated '\"'."));
2393
bbc13ae3 2394 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2395
2396 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2397 error (_("Invalid argument syntax"));
2398
2399 if (*cmdrest == ',')
2400 cmdrest++;
bbc13ae3 2401 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2402
2403 /* For each argument, make an expression. */
2404
2405 argvec = (struct expression **) alloca (strlen (cmd)
2406 * sizeof (struct expression *));
2407
2408 nargs = 0;
2409 while (*cmdrest != '\0')
2410 {
bbc13ae3 2411 const char *cmd1;
d3ce09f5
SS
2412
2413 cmd1 = cmdrest;
2414 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2415 argvec[nargs++] = expr;
2416 cmdrest = cmd1;
2417 if (*cmdrest == ',')
2418 ++cmdrest;
2419 }
2420
2421 /* We don't want to stop processing, so catch any errors
2422 that may show up. */
2423 TRY_CATCH (ex, RETURN_MASK_ERROR)
2424 {
2425 aexpr = gen_printf (scope, gdbarch, 0, 0,
2426 format_start, format_end - format_start,
2427 fpieces, nargs, argvec);
2428 }
2429
752eb8b4
TT
2430 do_cleanups (old_cleanups);
2431
d3ce09f5
SS
2432 if (ex.reason < 0)
2433 {
2434 /* If we got here, it means the command could not be parsed to a valid
2435 bytecode expression and thus can't be evaluated on the target's side.
2436 It's no use iterating through the other commands. */
2437 return NULL;
2438 }
2439
d3ce09f5
SS
2440 /* We have a valid agent expression, return it. */
2441 return aexpr;
2442}
2443
2444/* Based on location BL, create a list of breakpoint commands to be
2445 passed on to the target. If we have duplicated locations with
2446 different commands, we will add any such to the list. */
2447
2448static void
2449build_target_command_list (struct bp_location *bl)
2450{
2451 struct bp_location **locp = NULL, **loc2p;
2452 int null_command_or_parse_error = 0;
2453 int modified = bl->needs_update;
2454 struct bp_location *loc;
2455
8b4f3082
PA
2456 /* Release commands left over from a previous insert. */
2457 VEC_free (agent_expr_p, bl->target_info.tcommands);
2458
41fac0cf 2459 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2460 return;
2461
41fac0cf
PA
2462 /* For now, limit to agent-style dprintf breakpoints. */
2463 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2464 return;
2465
41fac0cf
PA
2466 /* For now, if we have any duplicate location that isn't a dprintf,
2467 don't install the target-side commands, as that would make the
2468 breakpoint not be reported to the core, and we'd lose
2469 control. */
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
2473 if (is_breakpoint (loc->owner)
2474 && loc->pspace->num == bl->pspace->num
2475 && loc->owner->type != bp_dprintf)
2476 return;
2477 }
2478
d3ce09f5
SS
2479 /* Do a first pass to check for locations with no assigned
2480 conditions or conditions that fail to parse to a valid agent expression
2481 bytecode. If any of these happen, then it's no use to send conditions
2482 to the target since this location will always trigger and generate a
2483 response back to GDB. */
2484 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2485 {
2486 loc = (*loc2p);
2487 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2488 {
2489 if (modified)
2490 {
2491 struct agent_expr *aexpr;
2492
2493 /* Re-parse the commands since something changed. In that
2494 case we already freed the command bytecodes (see
2495 force_breakpoint_reinsertion). We just
2496 need to parse the command to bytecodes again. */
2497 aexpr = parse_cmd_to_aexpr (bl->address,
2498 loc->owner->extra_string);
2499 loc->cmd_bytecode = aexpr;
2500
2501 if (!aexpr)
2502 continue;
2503 }
2504
2505 /* If we have a NULL bytecode expression, it means something
2506 went wrong or we have a null command expression. */
2507 if (!loc->cmd_bytecode)
2508 {
2509 null_command_or_parse_error = 1;
2510 break;
2511 }
2512 }
2513 }
2514
2515 /* If anything failed, then we're not doing target-side commands,
2516 and so clean up. */
2517 if (null_command_or_parse_error)
2518 {
2519 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2520 {
2521 loc = (*loc2p);
2522 if (is_breakpoint (loc->owner)
2523 && loc->pspace->num == bl->pspace->num)
2524 {
2525 /* Only go as far as the first NULL bytecode is
2526 located. */
40fb6c5e 2527 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2528 return;
2529
40fb6c5e
HZ
2530 free_agent_expr (loc->cmd_bytecode);
2531 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2532 }
2533 }
2534 }
2535
2536 /* No NULL commands or failed bytecode generation. Build a command list
2537 for this location's address. */
2538 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2539 {
2540 loc = (*loc2p);
2541 if (loc->owner->extra_string
2542 && is_breakpoint (loc->owner)
2543 && loc->pspace->num == bl->pspace->num
2544 && loc->owner->enable_state == bp_enabled
2545 && loc->enabled)
2546 /* Add the command to the vector. This will be used later
2547 to send the commands to the target. */
2548 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2549 loc->cmd_bytecode);
2550 }
2551
2552 bl->target_info.persist = 0;
2553 /* Maybe flag this location as persistent. */
2554 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2555 bl->target_info.persist = 1;
2556}
2557
35df4500
TJB
2558/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2559 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2560 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2561 Returns 0 for success, 1 if the bp_location type is not supported or
2562 -1 for failure.
879bfdc2 2563
4a64f543
MS
2564 NOTE drow/2003-09-09: This routine could be broken down to an
2565 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2566static int
35df4500 2567insert_bp_location (struct bp_location *bl,
26bb91f3 2568 struct ui_file *tmp_error_stream,
3fbb6ffa 2569 int *disabled_breaks,
dd61ec5c
MW
2570 int *hw_breakpoint_error,
2571 int *hw_bp_error_explained_already)
879bfdc2 2572{
0000e5cc
PA
2573 enum errors bp_err = GDB_NO_ERROR;
2574 const char *bp_err_message = NULL;
c90a6fb7 2575 volatile struct gdb_exception e;
879bfdc2 2576
b775012e 2577 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2578 return 0;
2579
35c63cd8
JB
2580 /* Note we don't initialize bl->target_info, as that wipes out
2581 the breakpoint location's shadow_contents if the breakpoint
2582 is still inserted at that location. This in turn breaks
2583 target_read_memory which depends on these buffers when
2584 a memory read is requested at the breakpoint location:
2585 Once the target_info has been wiped, we fail to see that
2586 we have a breakpoint inserted at that address and thus
2587 read the breakpoint instead of returning the data saved in
2588 the breakpoint location's shadow contents. */
0d5ed153 2589 bl->target_info.reqstd_address = bl->address;
35df4500 2590 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2591 bl->target_info.length = bl->length;
8181d85f 2592
b775012e
LM
2593 /* When working with target-side conditions, we must pass all the conditions
2594 for the same breakpoint address down to the target since GDB will not
2595 insert those locations. With a list of breakpoint conditions, the target
2596 can decide when to stop and notify GDB. */
2597
2598 if (is_breakpoint (bl->owner))
2599 {
2600 build_target_condition_list (bl);
d3ce09f5
SS
2601 build_target_command_list (bl);
2602 /* Reset the modification marker. */
b775012e
LM
2603 bl->needs_update = 0;
2604 }
2605
35df4500
TJB
2606 if (bl->loc_type == bp_loc_software_breakpoint
2607 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2608 {
35df4500 2609 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2610 {
2611 /* If the explicitly specified breakpoint type
2612 is not hardware breakpoint, check the memory map to see
2613 if the breakpoint address is in read only memory or not.
4a64f543 2614
765dc015
VP
2615 Two important cases are:
2616 - location type is not hardware breakpoint, memory
2617 is readonly. We change the type of the location to
2618 hardware breakpoint.
4a64f543
MS
2619 - location type is hardware breakpoint, memory is
2620 read-write. This means we've previously made the
2621 location hardware one, but then the memory map changed,
2622 so we undo.
765dc015 2623
4a64f543
MS
2624 When breakpoints are removed, remove_breakpoints will use
2625 location types we've just set here, the only possible
2626 problem is that memory map has changed during running
2627 program, but it's not going to work anyway with current
2628 gdb. */
765dc015 2629 struct mem_region *mr
0d5ed153 2630 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2631
2632 if (mr)
2633 {
2634 if (automatic_hardware_breakpoints)
2635 {
765dc015
VP
2636 enum bp_loc_type new_type;
2637
2638 if (mr->attrib.mode != MEM_RW)
2639 new_type = bp_loc_hardware_breakpoint;
2640 else
2641 new_type = bp_loc_software_breakpoint;
2642
35df4500 2643 if (new_type != bl->loc_type)
765dc015
VP
2644 {
2645 static int said = 0;
cc59ec59 2646
35df4500 2647 bl->loc_type = new_type;
765dc015
VP
2648 if (!said)
2649 {
3e43a32a
MS
2650 fprintf_filtered (gdb_stdout,
2651 _("Note: automatically using "
2652 "hardware breakpoints for "
2653 "read-only addresses.\n"));
765dc015
VP
2654 said = 1;
2655 }
2656 }
2657 }
35df4500 2658 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2659 && mr->attrib.mode != MEM_RW)
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 _("Cannot insert breakpoint %d.\n"
2663 "Cannot set software breakpoint "
2664 "at read-only address %s\n"),
2665 bl->owner->number,
2666 paddress (bl->gdbarch, bl->address));
2667 return 1;
2668 }
765dc015
VP
2669 }
2670 }
2671
879bfdc2
DJ
2672 /* First check to see if we have to handle an overlay. */
2673 if (overlay_debugging == ovly_off
35df4500
TJB
2674 || bl->section == NULL
2675 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2676 {
2677 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2678 TRY_CATCH (e, RETURN_MASK_ALL)
2679 {
0000e5cc
PA
2680 int val;
2681
dd61ec5c 2682 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2683 if (val)
2684 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2685 }
2686 if (e.reason < 0)
2687 {
0000e5cc
PA
2688 bp_err = e.error;
2689 bp_err_message = e.message;
dd61ec5c 2690 }
879bfdc2
DJ
2691 }
2692 else
2693 {
4a64f543 2694 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2695 Shall we set a breakpoint at the LMA? */
2696 if (!overlay_events_enabled)
2697 {
2698 /* Yes -- overlay event support is not active,
2699 so we must try to set a breakpoint at the LMA.
2700 This will not work for a hardware breakpoint. */
35df4500 2701 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2702 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2703 bl->owner->number);
879bfdc2
DJ
2704 else
2705 {
35df4500
TJB
2706 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2707 bl->section);
879bfdc2 2708 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2709 bl->overlay_target_info = bl->target_info;
0d5ed153 2710 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2711
2712 /* No overlay handling: just set the breakpoint. */
2713 TRY_CATCH (e, RETURN_MASK_ALL)
2714 {
2715 int val;
2716
2717 val = target_insert_breakpoint (bl->gdbarch,
2718 &bl->overlay_target_info);
2719 if (val)
2720 bp_err = GENERIC_ERROR;
2721 }
2722 if (e.reason < 0)
2723 {
2724 bp_err = e.error;
2725 bp_err_message = e.message;
2726 }
2727
2728 if (bp_err != GDB_NO_ERROR)
99361f52 2729 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2730 "Overlay breakpoint %d "
2731 "failed: in ROM?\n",
35df4500 2732 bl->owner->number);
879bfdc2
DJ
2733 }
2734 }
2735 /* Shall we set a breakpoint at the VMA? */
35df4500 2736 if (section_is_mapped (bl->section))
879bfdc2
DJ
2737 {
2738 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2739 TRY_CATCH (e, RETURN_MASK_ALL)
2740 {
0000e5cc
PA
2741 int val;
2742
dd61ec5c 2743 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2744 if (val)
2745 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2746 }
2747 if (e.reason < 0)
2748 {
0000e5cc
PA
2749 bp_err = e.error;
2750 bp_err_message = e.message;
dd61ec5c 2751 }
879bfdc2
DJ
2752 }
2753 else
2754 {
2755 /* No. This breakpoint will not be inserted.
2756 No error, but do not mark the bp as 'inserted'. */
2757 return 0;
2758 }
2759 }
2760
0000e5cc 2761 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2762 {
2763 /* Can't set the breakpoint. */
0000e5cc
PA
2764
2765 /* In some cases, we might not be able to insert a
2766 breakpoint in a shared library that has already been
2767 removed, but we have not yet processed the shlib unload
2768 event. Unfortunately, some targets that implement
076855f9
PA
2769 breakpoint insertion themselves can't tell why the
2770 breakpoint insertion failed (e.g., the remote target
2771 doesn't define error codes), so we must treat generic
2772 errors as memory errors. */
0000e5cc 2773 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2774 && bl->loc_type == bp_loc_software_breakpoint
08351840 2775 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2776 || shared_objfile_contains_address_p (bl->pspace,
2777 bl->address)))
879bfdc2 2778 {
4a64f543 2779 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2780 bl->shlib_disabled = 1;
8d3788bd 2781 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2782 if (!*disabled_breaks)
2783 {
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Cannot insert breakpoint %d.\n",
2786 bl->owner->number);
2787 fprintf_unfiltered (tmp_error_stream,
2788 "Temporarily disabling shared "
2789 "library breakpoints:\n");
2790 }
2791 *disabled_breaks = 1;
879bfdc2 2792 fprintf_unfiltered (tmp_error_stream,
35df4500 2793 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2794 return 0;
879bfdc2
DJ
2795 }
2796 else
879bfdc2 2797 {
35df4500 2798 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2799 {
0000e5cc
PA
2800 *hw_breakpoint_error = 1;
2801 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2804 bl->owner->number, bp_err_message ? ":" : ".\n");
2805 if (bp_err_message != NULL)
2806 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2807 }
2808 else
2809 {
0000e5cc
PA
2810 if (bp_err_message == NULL)
2811 {
2812 char *message
2813 = memory_error_message (TARGET_XFER_E_IO,
2814 bl->gdbarch, bl->address);
2815 struct cleanup *old_chain = make_cleanup (xfree, message);
2816
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert breakpoint %d.\n"
2819 "%s\n",
2820 bl->owner->number, message);
2821 do_cleanups (old_chain);
2822 }
2823 else
2824 {
2825 fprintf_unfiltered (tmp_error_stream,
2826 "Cannot insert breakpoint %d: %s\n",
2827 bl->owner->number,
2828 bp_err_message);
2829 }
879bfdc2 2830 }
0000e5cc 2831 return 1;
879bfdc2
DJ
2832
2833 }
2834 }
2835 else
35df4500 2836 bl->inserted = 1;
879bfdc2 2837
0000e5cc 2838 return 0;
879bfdc2
DJ
2839 }
2840
35df4500 2841 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2842 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2843 watchpoints. It's not clear that it's necessary... */
35df4500 2844 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2845 {
0000e5cc
PA
2846 int val;
2847
77b06cd7
TJB
2848 gdb_assert (bl->owner->ops != NULL
2849 && bl->owner->ops->insert_location != NULL);
2850
2851 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2852
2853 /* If trying to set a read-watchpoint, and it turns out it's not
2854 supported, try emulating one with an access watchpoint. */
35df4500 2855 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2856 {
2857 struct bp_location *loc, **loc_temp;
2858
2859 /* But don't try to insert it, if there's already another
2860 hw_access location that would be considered a duplicate
2861 of this one. */
2862 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2863 if (loc != bl
85d721b8 2864 && loc->watchpoint_type == hw_access
35df4500 2865 && watchpoint_locations_match (bl, loc))
85d721b8 2866 {
35df4500
TJB
2867 bl->duplicate = 1;
2868 bl->inserted = 1;
2869 bl->target_info = loc->target_info;
2870 bl->watchpoint_type = hw_access;
85d721b8
PA
2871 val = 0;
2872 break;
2873 }
2874
2875 if (val == 1)
2876 {
77b06cd7
TJB
2877 bl->watchpoint_type = hw_access;
2878 val = bl->owner->ops->insert_location (bl);
2879
2880 if (val)
2881 /* Back to the original value. */
2882 bl->watchpoint_type = hw_read;
85d721b8
PA
2883 }
2884 }
2885
35df4500 2886 bl->inserted = (val == 0);
879bfdc2
DJ
2887 }
2888
35df4500 2889 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2890 {
0000e5cc
PA
2891 int val;
2892
77b06cd7
TJB
2893 gdb_assert (bl->owner->ops != NULL
2894 && bl->owner->ops->insert_location != NULL);
2895
2896 val = bl->owner->ops->insert_location (bl);
2897 if (val)
2898 {
2899 bl->owner->enable_state = bp_disabled;
2900
2901 if (val == 1)
2902 warning (_("\
2903Error inserting catchpoint %d: Your system does not support this type\n\
2904of catchpoint."), bl->owner->number);
2905 else
2906 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2907 }
2908
2909 bl->inserted = (val == 0);
1640b821
DJ
2910
2911 /* We've already printed an error message if there was a problem
2912 inserting this catchpoint, and we've disabled the catchpoint,
2913 so just return success. */
2914 return 0;
879bfdc2
DJ
2915 }
2916
2917 return 0;
2918}
2919
6c95b8df
PA
2920/* This function is called when program space PSPACE is about to be
2921 deleted. It takes care of updating breakpoints to not reference
2922 PSPACE anymore. */
2923
2924void
2925breakpoint_program_space_exit (struct program_space *pspace)
2926{
2927 struct breakpoint *b, *b_temp;
876fa593 2928 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2929
2930 /* Remove any breakpoint that was set through this program space. */
2931 ALL_BREAKPOINTS_SAFE (b, b_temp)
2932 {
2933 if (b->pspace == pspace)
2934 delete_breakpoint (b);
2935 }
2936
2937 /* Breakpoints set through other program spaces could have locations
2938 bound to PSPACE as well. Remove those. */
876fa593 2939 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2940 {
2941 struct bp_location *tmp;
2942
2943 if (loc->pspace == pspace)
2944 {
2bdf28a0 2945 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2946 if (loc->owner->loc == loc)
2947 loc->owner->loc = loc->next;
2948 else
2949 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2950 if (tmp->next == loc)
2951 {
2952 tmp->next = loc->next;
2953 break;
2954 }
2955 }
2956 }
2957
2958 /* Now update the global location list to permanently delete the
2959 removed locations above. */
44702360 2960 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2961}
2962
74960c60
VP
2963/* Make sure all breakpoints are inserted in inferior.
2964 Throws exception on any error.
2965 A breakpoint that is already inserted won't be inserted
2966 again, so calling this function twice is safe. */
2967void
2968insert_breakpoints (void)
2969{
2970 struct breakpoint *bpt;
2971
2972 ALL_BREAKPOINTS (bpt)
2973 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2974 {
2975 struct watchpoint *w = (struct watchpoint *) bpt;
2976
2977 update_watchpoint (w, 0 /* don't reparse. */);
2978 }
74960c60 2979
04086b45
PA
2980 /* Updating watchpoints creates new locations, so update the global
2981 location list. Explicitly tell ugll to insert locations and
2982 ignore breakpoints_always_inserted_mode. */
2983 update_global_location_list (UGLL_INSERT);
74960c60
VP
2984}
2985
20388dd6
YQ
2986/* Invoke CALLBACK for each of bp_location. */
2987
2988void
2989iterate_over_bp_locations (walk_bp_location_callback callback)
2990{
2991 struct bp_location *loc, **loc_tmp;
2992
2993 ALL_BP_LOCATIONS (loc, loc_tmp)
2994 {
2995 callback (loc, NULL);
2996 }
2997}
2998
b775012e
LM
2999/* This is used when we need to synch breakpoint conditions between GDB and the
3000 target. It is the case with deleting and disabling of breakpoints when using
3001 always-inserted mode. */
3002
3003static void
3004update_inserted_breakpoint_locations (void)
3005{
3006 struct bp_location *bl, **blp_tmp;
3007 int error_flag = 0;
3008 int val = 0;
3009 int disabled_breaks = 0;
3010 int hw_breakpoint_error = 0;
dd61ec5c 3011 int hw_bp_details_reported = 0;
b775012e
LM
3012
3013 struct ui_file *tmp_error_stream = mem_fileopen ();
3014 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3015
3016 /* Explicitly mark the warning -- this will only be printed if
3017 there was an error. */
3018 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3019
3020 save_current_space_and_thread ();
3021
3022 ALL_BP_LOCATIONS (bl, blp_tmp)
3023 {
3024 /* We only want to update software breakpoints and hardware
3025 breakpoints. */
3026 if (!is_breakpoint (bl->owner))
3027 continue;
3028
3029 /* We only want to update locations that are already inserted
3030 and need updating. This is to avoid unwanted insertion during
3031 deletion of breakpoints. */
3032 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3033 continue;
3034
3035 switch_to_program_space_and_thread (bl->pspace);
3036
3037 /* For targets that support global breakpoints, there's no need
3038 to select an inferior to insert breakpoint to. In fact, even
3039 if we aren't attached to any process yet, we should still
3040 insert breakpoints. */
f5656ead 3041 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3042 && ptid_equal (inferior_ptid, null_ptid))
3043 continue;
3044
3045 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3046 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3047 if (val)
3048 error_flag = val;
3049 }
3050
3051 if (error_flag)
3052 {
3053 target_terminal_ours_for_output ();
3054 error_stream (tmp_error_stream);
3055 }
3056
3057 do_cleanups (cleanups);
3058}
3059
c30eee59 3060/* Used when starting or continuing the program. */
c906108c 3061
74960c60
VP
3062static void
3063insert_breakpoint_locations (void)
c906108c 3064{
a5606eee 3065 struct breakpoint *bpt;
35df4500 3066 struct bp_location *bl, **blp_tmp;
eacd795a 3067 int error_flag = 0;
c906108c 3068 int val = 0;
3fbb6ffa 3069 int disabled_breaks = 0;
81d0cc19 3070 int hw_breakpoint_error = 0;
dd61ec5c 3071 int hw_bp_error_explained_already = 0;
c906108c 3072
81d0cc19 3073 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3074 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3075
81d0cc19
GS
3076 /* Explicitly mark the warning -- this will only be printed if
3077 there was an error. */
3078 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3079
3080 save_current_space_and_thread ();
3081
35df4500 3082 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3083 {
b775012e 3084 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3085 continue;
3086
4a64f543
MS
3087 /* There is no point inserting thread-specific breakpoints if
3088 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3089 has BL->OWNER always non-NULL. */
35df4500
TJB
3090 if (bl->owner->thread != -1
3091 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3092 continue;
3093
35df4500 3094 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3095
3096 /* For targets that support global breakpoints, there's no need
3097 to select an inferior to insert breakpoint to. In fact, even
3098 if we aren't attached to any process yet, we should still
3099 insert breakpoints. */
f5656ead 3100 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3101 && ptid_equal (inferior_ptid, null_ptid))
3102 continue;
3103
3fbb6ffa 3104 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3105 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3106 if (val)
eacd795a 3107 error_flag = val;
879bfdc2 3108 }
c906108c 3109
4a64f543
MS
3110 /* If we failed to insert all locations of a watchpoint, remove
3111 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3112 ALL_BREAKPOINTS (bpt)
3113 {
3114 int some_failed = 0;
3115 struct bp_location *loc;
3116
3117 if (!is_hardware_watchpoint (bpt))
3118 continue;
3119
d6b74ac4 3120 if (!breakpoint_enabled (bpt))
a5606eee 3121 continue;
74960c60
VP
3122
3123 if (bpt->disposition == disp_del_at_next_stop)
3124 continue;
a5606eee
VP
3125
3126 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3127 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3128 {
3129 some_failed = 1;
3130 break;
3131 }
3132 if (some_failed)
3133 {
3134 for (loc = bpt->loc; loc; loc = loc->next)
3135 if (loc->inserted)
3136 remove_breakpoint (loc, mark_uninserted);
3137
3138 hw_breakpoint_error = 1;
3139 fprintf_unfiltered (tmp_error_stream,
3140 "Could not insert hardware watchpoint %d.\n",
3141 bpt->number);
eacd795a 3142 error_flag = -1;
a5606eee
VP
3143 }
3144 }
3145
eacd795a 3146 if (error_flag)
81d0cc19
GS
3147 {
3148 /* If a hardware breakpoint or watchpoint was inserted, add a
3149 message about possibly exhausted resources. */
dd61ec5c 3150 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3151 {
c6510018
MS
3152 fprintf_unfiltered (tmp_error_stream,
3153 "Could not insert hardware breakpoints:\n\
3154You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3155 }
81d0cc19
GS
3156 target_terminal_ours_for_output ();
3157 error_stream (tmp_error_stream);
3158 }
f7545552
TT
3159
3160 do_cleanups (cleanups);
c906108c
SS
3161}
3162
c30eee59
TJB
3163/* Used when the program stops.
3164 Returns zero if successful, or non-zero if there was a problem
3165 removing a breakpoint location. */
3166
c906108c 3167int
fba45db2 3168remove_breakpoints (void)
c906108c 3169{
35df4500 3170 struct bp_location *bl, **blp_tmp;
3a1bae8e 3171 int val = 0;
c906108c 3172
35df4500 3173 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3174 {
1e4d1764 3175 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3176 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3177 }
3a1bae8e 3178 return val;
c906108c
SS
3179}
3180
49fa26b0
PA
3181/* When a thread exits, remove breakpoints that are related to
3182 that thread. */
3183
3184static void
3185remove_threaded_breakpoints (struct thread_info *tp, int silent)
3186{
3187 struct breakpoint *b, *b_tmp;
3188
3189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3190 {
96181529 3191 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3192 {
3193 b->disposition = disp_del_at_next_stop;
3194
3195 printf_filtered (_("\
46ecd527 3196Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3197 b->number, tp->num);
3198
3199 /* Hide it from the user. */
3200 b->number = 0;
3201 }
3202 }
3203}
3204
6c95b8df
PA
3205/* Remove breakpoints of process PID. */
3206
3207int
3208remove_breakpoints_pid (int pid)
3209{
35df4500 3210 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3211 int val;
3212 struct inferior *inf = find_inferior_pid (pid);
3213
35df4500 3214 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3215 {
35df4500 3216 if (bl->pspace != inf->pspace)
6c95b8df
PA
3217 continue;
3218
fc126975 3219 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3220 {
35df4500 3221 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3222 if (val != 0)
3223 return val;
3224 }
3225 }
3226 return 0;
3227}
3228
c906108c 3229int
fba45db2 3230reattach_breakpoints (int pid)
c906108c 3231{
6c95b8df 3232 struct cleanup *old_chain;
35df4500 3233 struct bp_location *bl, **blp_tmp;
c906108c 3234 int val;
86b887df 3235 struct ui_file *tmp_error_stream;
dd61ec5c 3236 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3237 struct inferior *inf;
3238 struct thread_info *tp;
3239
3240 tp = any_live_thread_of_process (pid);
3241 if (tp == NULL)
3242 return 1;
3243
3244 inf = find_inferior_pid (pid);
3245 old_chain = save_inferior_ptid ();
3246
3247 inferior_ptid = tp->ptid;
a4954f26 3248
86b887df 3249 tmp_error_stream = mem_fileopen ();
a4954f26 3250 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3251
35df4500 3252 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3253 {
35df4500 3254 if (bl->pspace != inf->pspace)
6c95b8df
PA
3255 continue;
3256
35df4500 3257 if (bl->inserted)
c5aa993b 3258 {
35df4500 3259 bl->inserted = 0;
dd61ec5c 3260 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3261 if (val != 0)
3262 {
ce696e05 3263 do_cleanups (old_chain);
c5aa993b
JM
3264 return val;
3265 }
3266 }
3267 }
ce696e05 3268 do_cleanups (old_chain);
c906108c
SS
3269 return 0;
3270}
3271
e58b0e63
PA
3272static int internal_breakpoint_number = -1;
3273
84f4c1fe
PM
3274/* Set the breakpoint number of B, depending on the value of INTERNAL.
3275 If INTERNAL is non-zero, the breakpoint number will be populated
3276 from internal_breakpoint_number and that variable decremented.
e5dd4106 3277 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3278 breakpoint_count and that value incremented. Internal breakpoints
3279 do not set the internal var bpnum. */
3280static void
3281set_breakpoint_number (int internal, struct breakpoint *b)
3282{
3283 if (internal)
3284 b->number = internal_breakpoint_number--;
3285 else
3286 {
3287 set_breakpoint_count (breakpoint_count + 1);
3288 b->number = breakpoint_count;
3289 }
3290}
3291
e62c965a 3292static struct breakpoint *
a6d9a66e 3293create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3294 CORE_ADDR address, enum bptype type,
c0a91b2b 3295 const struct breakpoint_ops *ops)
e62c965a 3296{
e62c965a
PP
3297 struct symtab_and_line sal;
3298 struct breakpoint *b;
3299
4a64f543 3300 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3301
3302 sal.pc = address;
3303 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3304 sal.pspace = current_program_space;
e62c965a 3305
06edf0c0 3306 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3307 b->number = internal_breakpoint_number--;
3308 b->disposition = disp_donttouch;
3309
3310 return b;
3311}
3312
17450429
PP
3313static const char *const longjmp_names[] =
3314 {
3315 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3316 };
3317#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3318
3319/* Per-objfile data private to breakpoint.c. */
3320struct breakpoint_objfile_data
3321{
3322 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3323 struct bound_minimal_symbol overlay_msym;
17450429
PP
3324
3325 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3326 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3327
28106bc2
SDJ
3328 /* True if we have looked for longjmp probes. */
3329 int longjmp_searched;
3330
3331 /* SystemTap probe points for longjmp (if any). */
3332 VEC (probe_p) *longjmp_probes;
3333
17450429 3334 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3335 struct bound_minimal_symbol terminate_msym;
17450429
PP
3336
3337 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3338 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3339
3340 /* True if we have looked for exception probes. */
3341 int exception_searched;
3342
3343 /* SystemTap probe points for unwinding (if any). */
3344 VEC (probe_p) *exception_probes;
17450429
PP
3345};
3346
3347static const struct objfile_data *breakpoint_objfile_key;
3348
3349/* Minimal symbol not found sentinel. */
3350static struct minimal_symbol msym_not_found;
3351
3352/* Returns TRUE if MSYM point to the "not found" sentinel. */
3353
3354static int
3355msym_not_found_p (const struct minimal_symbol *msym)
3356{
3357 return msym == &msym_not_found;
3358}
3359
3360/* Return per-objfile data needed by breakpoint.c.
3361 Allocate the data if necessary. */
3362
3363static struct breakpoint_objfile_data *
3364get_breakpoint_objfile_data (struct objfile *objfile)
3365{
3366 struct breakpoint_objfile_data *bp_objfile_data;
3367
3368 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3369 if (bp_objfile_data == NULL)
3370 {
3371 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3372 sizeof (*bp_objfile_data));
3373
3374 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3375 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3376 }
3377 return bp_objfile_data;
3378}
3379
28106bc2
SDJ
3380static void
3381free_breakpoint_probes (struct objfile *obj, void *data)
3382{
3383 struct breakpoint_objfile_data *bp_objfile_data = data;
3384
3385 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3386 VEC_free (probe_p, bp_objfile_data->exception_probes);
3387}
3388
e62c965a 3389static void
af02033e 3390create_overlay_event_breakpoint (void)
e62c965a 3391{
69de3c6a 3392 struct objfile *objfile;
af02033e 3393 const char *const func_name = "_ovly_debug_event";
e62c965a 3394
69de3c6a
PP
3395 ALL_OBJFILES (objfile)
3396 {
3397 struct breakpoint *b;
17450429
PP
3398 struct breakpoint_objfile_data *bp_objfile_data;
3399 CORE_ADDR addr;
69de3c6a 3400
17450429
PP
3401 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3402
3b7344d5 3403 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3404 continue;
3405
3b7344d5 3406 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3407 {
3b7344d5 3408 struct bound_minimal_symbol m;
17450429
PP
3409
3410 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3411 if (m.minsym == NULL)
17450429
PP
3412 {
3413 /* Avoid future lookups in this objfile. */
3b7344d5 3414 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3415 continue;
3416 }
3417 bp_objfile_data->overlay_msym = m;
3418 }
e62c965a 3419
77e371c0 3420 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3421 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3422 bp_overlay_event,
3423 &internal_breakpoint_ops);
69de3c6a 3424 b->addr_string = xstrdup (func_name);
e62c965a 3425
69de3c6a
PP
3426 if (overlay_debugging == ovly_auto)
3427 {
3428 b->enable_state = bp_enabled;
3429 overlay_events_enabled = 1;
3430 }
3431 else
3432 {
3433 b->enable_state = bp_disabled;
3434 overlay_events_enabled = 0;
3435 }
e62c965a 3436 }
44702360 3437 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3438}
3439
0fd8e87f 3440static void
af02033e 3441create_longjmp_master_breakpoint (void)
0fd8e87f 3442{
6c95b8df 3443 struct program_space *pspace;
6c95b8df
PA
3444 struct cleanup *old_chain;
3445
3446 old_chain = save_current_program_space ();
0fd8e87f 3447
6c95b8df 3448 ALL_PSPACES (pspace)
af02033e
PP
3449 {
3450 struct objfile *objfile;
3451
3452 set_current_program_space (pspace);
3453
3454 ALL_OBJFILES (objfile)
0fd8e87f 3455 {
af02033e
PP
3456 int i;
3457 struct gdbarch *gdbarch;
17450429 3458 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3459
af02033e 3460 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3461
17450429
PP
3462 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3463
28106bc2
SDJ
3464 if (!bp_objfile_data->longjmp_searched)
3465 {
25f9533e
SDJ
3466 VEC (probe_p) *ret;
3467
3468 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3469 if (ret != NULL)
3470 {
3471 /* We are only interested in checking one element. */
3472 struct probe *p = VEC_index (probe_p, ret, 0);
3473
3474 if (!can_evaluate_probe_arguments (p))
3475 {
3476 /* We cannot use the probe interface here, because it does
3477 not know how to evaluate arguments. */
3478 VEC_free (probe_p, ret);
3479 ret = NULL;
3480 }
3481 }
3482 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3483 bp_objfile_data->longjmp_searched = 1;
3484 }
3485
3486 if (bp_objfile_data->longjmp_probes != NULL)
3487 {
3488 int i;
3489 struct probe *probe;
3490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3491
3492 for (i = 0;
3493 VEC_iterate (probe_p,
3494 bp_objfile_data->longjmp_probes,
3495 i, probe);
3496 ++i)
3497 {
3498 struct breakpoint *b;
3499
729662a5
TT
3500 b = create_internal_breakpoint (gdbarch,
3501 get_probe_address (probe,
3502 objfile),
28106bc2
SDJ
3503 bp_longjmp_master,
3504 &internal_breakpoint_ops);
3505 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3506 b->enable_state = bp_disabled;
3507 }
3508
3509 continue;
3510 }
3511
0569175e
TSD
3512 if (!gdbarch_get_longjmp_target_p (gdbarch))
3513 continue;
3514
17450429 3515 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3516 {
3517 struct breakpoint *b;
af02033e 3518 const char *func_name;
17450429 3519 CORE_ADDR addr;
6c95b8df 3520
3b7344d5 3521 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3522 continue;
0fd8e87f 3523
17450429 3524 func_name = longjmp_names[i];
3b7344d5 3525 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3526 {
3b7344d5 3527 struct bound_minimal_symbol m;
17450429
PP
3528
3529 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3530 if (m.minsym == NULL)
17450429
PP
3531 {
3532 /* Prevent future lookups in this objfile. */
3b7344d5 3533 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3534 continue;
3535 }
3536 bp_objfile_data->longjmp_msym[i] = m;
3537 }
3538
77e371c0 3539 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3540 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3541 &internal_breakpoint_ops);
af02033e
PP
3542 b->addr_string = xstrdup (func_name);
3543 b->enable_state = bp_disabled;
3544 }
0fd8e87f 3545 }
af02033e 3546 }
44702360 3547 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3548
3549 do_cleanups (old_chain);
0fd8e87f
UW
3550}
3551
af02033e 3552/* Create a master std::terminate breakpoint. */
aa7d318d 3553static void
af02033e 3554create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3555{
3556 struct program_space *pspace;
aa7d318d 3557 struct cleanup *old_chain;
af02033e 3558 const char *const func_name = "std::terminate()";
aa7d318d
TT
3559
3560 old_chain = save_current_program_space ();
3561
3562 ALL_PSPACES (pspace)
17450429
PP
3563 {
3564 struct objfile *objfile;
3565 CORE_ADDR addr;
3566
3567 set_current_program_space (pspace);
3568
aa7d318d
TT
3569 ALL_OBJFILES (objfile)
3570 {
3571 struct breakpoint *b;
17450429 3572 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3573
17450429 3574 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3575
3b7344d5 3576 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3577 continue;
3578
3b7344d5 3579 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3580 {
3b7344d5 3581 struct bound_minimal_symbol m;
17450429
PP
3582
3583 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3584 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3585 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3586 {
3587 /* Prevent future lookups in this objfile. */
3b7344d5 3588 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3589 continue;
3590 }
3591 bp_objfile_data->terminate_msym = m;
3592 }
aa7d318d 3593
77e371c0 3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3595 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3596 bp_std_terminate_master,
3597 &internal_breakpoint_ops);
aa7d318d
TT
3598 b->addr_string = xstrdup (func_name);
3599 b->enable_state = bp_disabled;
3600 }
17450429
PP
3601 }
3602
44702360 3603 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3604
3605 do_cleanups (old_chain);
3606}
3607
186c406b
TT
3608/* Install a master breakpoint on the unwinder's debug hook. */
3609
70221824 3610static void
186c406b
TT
3611create_exception_master_breakpoint (void)
3612{
3613 struct objfile *objfile;
17450429 3614 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3615
3616 ALL_OBJFILES (objfile)
3617 {
17450429
PP
3618 struct breakpoint *b;
3619 struct gdbarch *gdbarch;
3620 struct breakpoint_objfile_data *bp_objfile_data;
3621 CORE_ADDR addr;
3622
3623 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3624
28106bc2
SDJ
3625 /* We prefer the SystemTap probe point if it exists. */
3626 if (!bp_objfile_data->exception_searched)
3627 {
25f9533e
SDJ
3628 VEC (probe_p) *ret;
3629
3630 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3631
3632 if (ret != NULL)
3633 {
3634 /* We are only interested in checking one element. */
3635 struct probe *p = VEC_index (probe_p, ret, 0);
3636
3637 if (!can_evaluate_probe_arguments (p))
3638 {
3639 /* We cannot use the probe interface here, because it does
3640 not know how to evaluate arguments. */
3641 VEC_free (probe_p, ret);
3642 ret = NULL;
3643 }
3644 }
3645 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3646 bp_objfile_data->exception_searched = 1;
3647 }
3648
3649 if (bp_objfile_data->exception_probes != NULL)
3650 {
3651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3652 int i;
3653 struct probe *probe;
3654
3655 for (i = 0;
3656 VEC_iterate (probe_p,
3657 bp_objfile_data->exception_probes,
3658 i, probe);
3659 ++i)
3660 {
3661 struct breakpoint *b;
3662
729662a5
TT
3663 b = create_internal_breakpoint (gdbarch,
3664 get_probe_address (probe,
3665 objfile),
28106bc2
SDJ
3666 bp_exception_master,
3667 &internal_breakpoint_ops);
3668 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3669 b->enable_state = bp_disabled;
3670 }
3671
3672 continue;
3673 }
3674
3675 /* Otherwise, try the hook function. */
3676
3b7344d5 3677 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3678 continue;
3679
3680 gdbarch = get_objfile_arch (objfile);
186c406b 3681
3b7344d5 3682 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3683 {
3b7344d5 3684 struct bound_minimal_symbol debug_hook;
186c406b 3685
17450429 3686 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3687 if (debug_hook.minsym == NULL)
17450429 3688 {
3b7344d5 3689 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3690 continue;
3691 }
3692
3693 bp_objfile_data->exception_msym = debug_hook;
186c406b 3694 }
17450429 3695
77e371c0 3696 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3697 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3698 &current_target);
06edf0c0
PA
3699 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3700 &internal_breakpoint_ops);
17450429
PP
3701 b->addr_string = xstrdup (func_name);
3702 b->enable_state = bp_disabled;
186c406b
TT
3703 }
3704
44702360 3705 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3706}
3707
c906108c 3708void
fba45db2 3709update_breakpoints_after_exec (void)
c906108c 3710{
35df4500 3711 struct breakpoint *b, *b_tmp;
876fa593 3712 struct bp_location *bploc, **bplocp_tmp;
c906108c 3713
25b22b0a
PA
3714 /* We're about to delete breakpoints from GDB's lists. If the
3715 INSERTED flag is true, GDB will try to lift the breakpoints by
3716 writing the breakpoints' "shadow contents" back into memory. The
3717 "shadow contents" are NOT valid after an exec, so GDB should not
3718 do that. Instead, the target is responsible from marking
3719 breakpoints out as soon as it detects an exec. We don't do that
3720 here instead, because there may be other attempts to delete
3721 breakpoints after detecting an exec and before reaching here. */
876fa593 3722 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3723 if (bploc->pspace == current_program_space)
3724 gdb_assert (!bploc->inserted);
c906108c 3725
35df4500 3726 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3727 {
6c95b8df
PA
3728 if (b->pspace != current_program_space)
3729 continue;
3730
4a64f543 3731 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3732 if (b->type == bp_shlib_event)
3733 {
3734 delete_breakpoint (b);
3735 continue;
3736 }
c906108c 3737
4a64f543 3738 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3739 if (b->type == bp_jit_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
3744
1900040c 3745 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3746 as must overlay event and longjmp master breakpoints. */
3747 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3748 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3749 || b->type == bp_exception_master)
c4093a6a
JM
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
3754
4a64f543 3755 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3756 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
7c16b83e
PA
3762 /* Just like single-step breakpoints. */
3763 if (b->type == bp_single_step)
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
3768
611c83ae
PA
3769 /* Longjmp and longjmp-resume breakpoints are also meaningless
3770 after an exec. */
186c406b 3771 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3772 || b->type == bp_longjmp_call_dummy
186c406b 3773 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3774 {
3775 delete_breakpoint (b);
3776 continue;
3777 }
3778
ce78b96d
JB
3779 if (b->type == bp_catchpoint)
3780 {
3781 /* For now, none of the bp_catchpoint breakpoints need to
3782 do anything at this point. In the future, if some of
3783 the catchpoints need to something, we will need to add
3784 a new method, and call this method from here. */
3785 continue;
3786 }
3787
c5aa993b
JM
3788 /* bp_finish is a special case. The only way we ought to be able
3789 to see one of these when an exec() has happened, is if the user
3790 caught a vfork, and then said "finish". Ordinarily a finish just
3791 carries them to the call-site of the current callee, by setting
3792 a temporary bp there and resuming. But in this case, the finish
3793 will carry them entirely through the vfork & exec.
3794
3795 We don't want to allow a bp_finish to remain inserted now. But
3796 we can't safely delete it, 'cause finish_command has a handle to
3797 the bp on a bpstat, and will later want to delete it. There's a
3798 chance (and I've seen it happen) that if we delete the bp_finish
3799 here, that its storage will get reused by the time finish_command
3800 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3801 We really must allow finish_command to delete a bp_finish.
3802
e5dd4106 3803 In the absence of a general solution for the "how do we know
53a5351d
JM
3804 it's safe to delete something others may have handles to?"
3805 problem, what we'll do here is just uninsert the bp_finish, and
3806 let finish_command delete it.
3807
3808 (We know the bp_finish is "doomed" in the sense that it's
3809 momentary, and will be deleted as soon as finish_command sees
3810 the inferior stopped. So it doesn't matter that the bp's
3811 address is probably bogus in the new a.out, unlike e.g., the
3812 solib breakpoints.) */
c5aa993b 3813
c5aa993b
JM
3814 if (b->type == bp_finish)
3815 {
3816 continue;
3817 }
3818
3819 /* Without a symbolic address, we have little hope of the
3820 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3821 a.out. */
c5aa993b
JM
3822 if (b->addr_string == NULL)
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
c5aa993b 3827 }
c906108c
SS
3828}
3829
3830int
d80ee84f 3831detach_breakpoints (ptid_t ptid)
c906108c 3832{
35df4500 3833 struct bp_location *bl, **blp_tmp;
3a1bae8e 3834 int val = 0;
ce696e05 3835 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3836 struct inferior *inf = current_inferior ();
c5aa993b 3837
dfd4cc63 3838 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3839 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3840
6c95b8df 3841 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3842 inferior_ptid = ptid;
35df4500 3843 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3844 {
35df4500 3845 if (bl->pspace != inf->pspace)
6c95b8df
PA
3846 continue;
3847
bd9673a4
PW
3848 /* This function must physically remove breakpoints locations
3849 from the specified ptid, without modifying the breakpoint
3850 package's state. Locations of type bp_loc_other are only
3851 maintained at GDB side. So, there is no need to remove
3852 these bp_loc_other locations. Moreover, removing these
3853 would modify the breakpoint package's state. */
3854 if (bl->loc_type == bp_loc_other)
3855 continue;
3856
35df4500
TJB
3857 if (bl->inserted)
3858 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3859 }
d03285ec 3860
ce696e05 3861 do_cleanups (old_chain);
3a1bae8e 3862 return val;
c906108c
SS
3863}
3864
35df4500 3865/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3866 Note that this is used to detach breakpoints from a child fork.
3867 When we get here, the child isn't in the inferior list, and neither
3868 do we have objects to represent its address space --- we should
35df4500 3869 *not* look at bl->pspace->aspace here. */
6c95b8df 3870
c906108c 3871static int
35df4500 3872remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3873{
3874 int val;
c5aa993b 3875
35df4500
TJB
3876 /* BL is never in moribund_locations by our callers. */
3877 gdb_assert (bl->owner != NULL);
2bdf28a0 3878
1a853c52 3879 if (bl->permanent)
c2c6d25f
JM
3880 /* Permanent breakpoints cannot be inserted or removed. */
3881 return 0;
3882
74960c60
VP
3883 /* The type of none suggests that owner is actually deleted.
3884 This should not ever happen. */
35df4500 3885 gdb_assert (bl->owner->type != bp_none);
0bde7532 3886
35df4500
TJB
3887 if (bl->loc_type == bp_loc_software_breakpoint
3888 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3889 {
c02f5703
MS
3890 /* "Normal" instruction breakpoint: either the standard
3891 trap-instruction bp (bp_breakpoint), or a
3892 bp_hardware_breakpoint. */
3893
3894 /* First check to see if we have to handle an overlay. */
3895 if (overlay_debugging == ovly_off
35df4500
TJB
3896 || bl->section == NULL
3897 || !(section_is_overlay (bl->section)))
c02f5703
MS
3898 {
3899 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3900
3901 /* If we're trying to uninsert a memory breakpoint that we
3902 know is set in a dynamic object that is marked
3903 shlib_disabled, then either the dynamic object was
3904 removed with "remove-symbol-file" or with
3905 "nosharedlibrary". In the former case, we don't know
3906 whether another dynamic object might have loaded over the
3907 breakpoint's address -- the user might well let us know
3908 about it next with add-symbol-file (the whole point of
d03de421 3909 add-symbol-file is letting the user manually maintain a
08351840
PA
3910 list of dynamically loaded objects). If we have the
3911 breakpoint's shadow memory, that is, this is a software
3912 breakpoint managed by GDB, check whether the breakpoint
3913 is still inserted in memory, to avoid overwriting wrong
3914 code with stale saved shadow contents. Note that HW
3915 breakpoints don't have shadow memory, as they're
3916 implemented using a mechanism that is not dependent on
3917 being able to modify the target's memory, and as such
3918 they should always be removed. */
3919 if (bl->shlib_disabled
3920 && bl->target_info.shadow_len != 0
3921 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3922 val = 0;
3923 else
3924 val = bl->owner->ops->remove_location (bl);
c02f5703 3925 }
c906108c
SS
3926 else
3927 {
4a64f543 3928 /* This breakpoint is in an overlay section.
c02f5703
MS
3929 Did we set a breakpoint at the LMA? */
3930 if (!overlay_events_enabled)
3931 {
3932 /* Yes -- overlay event support is not active, so we
3933 should have set a breakpoint at the LMA. Remove it.
3934 */
c02f5703
MS
3935 /* Ignore any failures: if the LMA is in ROM, we will
3936 have already warned when we failed to insert it. */
35df4500
TJB
3937 if (bl->loc_type == bp_loc_hardware_breakpoint)
3938 target_remove_hw_breakpoint (bl->gdbarch,
3939 &bl->overlay_target_info);
c02f5703 3940 else
35df4500
TJB
3941 target_remove_breakpoint (bl->gdbarch,
3942 &bl->overlay_target_info);
c02f5703
MS
3943 }
3944 /* Did we set a breakpoint at the VMA?
3945 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3946 if (bl->inserted)
c906108c 3947 {
c02f5703
MS
3948 /* Yes -- remove it. Previously we did not bother to
3949 remove the breakpoint if the section had been
3950 unmapped, but let's not rely on that being safe. We
3951 don't know what the overlay manager might do. */
aa67235e
UW
3952
3953 /* However, we should remove *software* breakpoints only
3954 if the section is still mapped, or else we overwrite
3955 wrong code with the saved shadow contents. */
348d480f
PA
3956 if (bl->loc_type == bp_loc_hardware_breakpoint
3957 || section_is_mapped (bl->section))
3958 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3959 else
3960 val = 0;
c906108c 3961 }
c02f5703
MS
3962 else
3963 {
3964 /* No -- not inserted, so no need to remove. No error. */
3965 val = 0;
3966 }
c906108c 3967 }
879d1e6b 3968
08351840
PA
3969 /* In some cases, we might not be able to remove a breakpoint in
3970 a shared library that has already been removed, but we have
3971 not yet processed the shlib unload event. Similarly for an
3972 unloaded add-symbol-file object - the user might not yet have
3973 had the chance to remove-symbol-file it. shlib_disabled will
3974 be set if the library/object has already been removed, but
3975 the breakpoint hasn't been uninserted yet, e.g., after
3976 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3977 always-inserted mode. */
076855f9 3978 if (val
08351840
PA
3979 && (bl->loc_type == bp_loc_software_breakpoint
3980 && (bl->shlib_disabled
3981 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3982 || shared_objfile_contains_address_p (bl->pspace,
3983 bl->address))))
879d1e6b
UW
3984 val = 0;
3985
c906108c
SS
3986 if (val)
3987 return val;
35df4500 3988 bl->inserted = (is == mark_inserted);
c906108c 3989 }
35df4500 3990 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3991 {
77b06cd7
TJB
3992 gdb_assert (bl->owner->ops != NULL
3993 && bl->owner->ops->remove_location != NULL);
3994
35df4500 3995 bl->inserted = (is == mark_inserted);
77b06cd7 3996 bl->owner->ops->remove_location (bl);
2e70b7b9 3997
c906108c 3998 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3999 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4000 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4001 bl->owner->number);
c906108c 4002 }
35df4500
TJB
4003 else if (bl->owner->type == bp_catchpoint
4004 && breakpoint_enabled (bl->owner)
4005 && !bl->duplicate)
ce78b96d 4006 {
77b06cd7
TJB
4007 gdb_assert (bl->owner->ops != NULL
4008 && bl->owner->ops->remove_location != NULL);
ce78b96d 4009
77b06cd7 4010 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4011 if (val)
4012 return val;
77b06cd7 4013
35df4500 4014 bl->inserted = (is == mark_inserted);
ce78b96d 4015 }
c906108c
SS
4016
4017 return 0;
4018}
4019
6c95b8df 4020static int
35df4500 4021remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4022{
4023 int ret;
4024 struct cleanup *old_chain;
4025
35df4500
TJB
4026 /* BL is never in moribund_locations by our callers. */
4027 gdb_assert (bl->owner != NULL);
2bdf28a0 4028
1a853c52 4029 if (bl->permanent)
6c95b8df
PA
4030 /* Permanent breakpoints cannot be inserted or removed. */
4031 return 0;
4032
4033 /* The type of none suggests that owner is actually deleted.
4034 This should not ever happen. */
35df4500 4035 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4036
4037 old_chain = save_current_space_and_thread ();
4038
35df4500 4039 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4040
35df4500 4041 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4042
4043 do_cleanups (old_chain);
4044 return ret;
4045}
4046
c906108c
SS
4047/* Clear the "inserted" flag in all breakpoints. */
4048
25b22b0a 4049void
fba45db2 4050mark_breakpoints_out (void)
c906108c 4051{
35df4500 4052 struct bp_location *bl, **blp_tmp;
c906108c 4053
35df4500 4054 ALL_BP_LOCATIONS (bl, blp_tmp)
1a853c52
PA
4055 if (bl->pspace == current_program_space
4056 && !bl->permanent)
35df4500 4057 bl->inserted = 0;
c906108c
SS
4058}
4059
53a5351d
JM
4060/* Clear the "inserted" flag in all breakpoints and delete any
4061 breakpoints which should go away between runs of the program.
c906108c
SS
4062
4063 Plus other such housekeeping that has to be done for breakpoints
4064 between runs.
4065
53a5351d
JM
4066 Note: this function gets called at the end of a run (by
4067 generic_mourn_inferior) and when a run begins (by
4a64f543 4068 init_wait_for_inferior). */
c906108c
SS
4069
4070
4071
4072void
fba45db2 4073breakpoint_init_inferior (enum inf_context context)
c906108c 4074{
35df4500
TJB
4075 struct breakpoint *b, *b_tmp;
4076 struct bp_location *bl, **blp_tmp;
1c5cfe86 4077 int ix;
6c95b8df 4078 struct program_space *pspace = current_program_space;
c906108c 4079
50c71eaf
PA
4080 /* If breakpoint locations are shared across processes, then there's
4081 nothing to do. */
f5656ead 4082 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4083 return;
4084
1a853c52 4085 mark_breakpoints_out ();
075f6582 4086
35df4500 4087 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4088 {
6c95b8df
PA
4089 if (b->loc && b->loc->pspace != pspace)
4090 continue;
4091
c5aa993b
JM
4092 switch (b->type)
4093 {
4094 case bp_call_dummy:
e2e4d78b 4095 case bp_longjmp_call_dummy:
c906108c 4096
c5aa993b 4097 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4098 cause problems when the inferior is rerun, so we better get
4099 rid of it. */
4100
4101 case bp_watchpoint_scope:
4102
4103 /* Also get rid of scope breakpoints. */
4104
4105 case bp_shlib_event:
4106
4107 /* Also remove solib event breakpoints. Their addresses may
4108 have changed since the last time we ran the program.
4109 Actually we may now be debugging against different target;
4110 and so the solib backend that installed this breakpoint may
4111 not be used in by the target. E.g.,
4112
4113 (gdb) file prog-linux
4114 (gdb) run # native linux target
4115 ...
4116 (gdb) kill
4117 (gdb) file prog-win.exe
4118 (gdb) tar rem :9999 # remote Windows gdbserver.
4119 */
c906108c 4120
f59f708a
PA
4121 case bp_step_resume:
4122
4123 /* Also remove step-resume breakpoints. */
4124
7c16b83e
PA
4125 case bp_single_step:
4126
4127 /* Also remove single-step breakpoints. */
4128
c5aa993b
JM
4129 delete_breakpoint (b);
4130 break;
c906108c 4131
c5aa993b
JM
4132 case bp_watchpoint:
4133 case bp_hardware_watchpoint:
4134 case bp_read_watchpoint:
4135 case bp_access_watchpoint:
3a5c3e22
PA
4136 {
4137 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4138
3a5c3e22
PA
4139 /* Likewise for watchpoints on local expressions. */
4140 if (w->exp_valid_block != NULL)
4141 delete_breakpoint (b);
4142 else if (context == inf_starting)
4143 {
4144 /* Reset val field to force reread of starting value in
4145 insert_breakpoints. */
4146 if (w->val)
4147 value_free (w->val);
4148 w->val = NULL;
4149 w->val_valid = 0;
c860120c 4150 }
3a5c3e22 4151 }
c5aa993b
JM
4152 break;
4153 default:
c5aa993b
JM
4154 break;
4155 }
4156 }
1c5cfe86
PA
4157
4158 /* Get rid of the moribund locations. */
35df4500
TJB
4159 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4160 decref_bp_location (&bl);
1c5cfe86 4161 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4162}
4163
6c95b8df
PA
4164/* These functions concern about actual breakpoints inserted in the
4165 target --- to e.g. check if we need to do decr_pc adjustment or if
4166 we need to hop over the bkpt --- so we check for address space
4167 match, not program space. */
4168
c2c6d25f
JM
4169/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4170 exists at PC. It returns ordinary_breakpoint_here if it's an
4171 ordinary breakpoint, or permanent_breakpoint_here if it's a
4172 permanent breakpoint.
4173 - When continuing from a location with an ordinary breakpoint, we
4174 actually single step once before calling insert_breakpoints.
e5dd4106 4175 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4176 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4177 the target, to advance the PC past the breakpoint. */
c906108c 4178
c2c6d25f 4179enum breakpoint_here
6c95b8df 4180breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4181{
35df4500 4182 struct bp_location *bl, **blp_tmp;
c2c6d25f 4183 int any_breakpoint_here = 0;
c906108c 4184
35df4500 4185 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4186 {
35df4500
TJB
4187 if (bl->loc_type != bp_loc_software_breakpoint
4188 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4189 continue;
4190
f1310107 4191 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4192 if ((breakpoint_enabled (bl->owner)
1a853c52 4193 || bl->permanent)
f1310107 4194 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4195 {
4196 if (overlay_debugging
35df4500
TJB
4197 && section_is_overlay (bl->section)
4198 && !section_is_mapped (bl->section))
075f6582 4199 continue; /* unmapped overlay -- can't be a match */
1a853c52 4200 else if (bl->permanent)
075f6582
DJ
4201 return permanent_breakpoint_here;
4202 else
4203 any_breakpoint_here = 1;
4204 }
4205 }
c906108c 4206
c2c6d25f 4207 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4208}
4209
1c5cfe86
PA
4210/* Return true if there's a moribund breakpoint at PC. */
4211
4212int
6c95b8df 4213moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4214{
4215 struct bp_location *loc;
4216 int ix;
4217
4218 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4219 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4220 return 1;
4221
4222 return 0;
4223}
c2c6d25f 4224
f7ce857f
PA
4225/* Returns non-zero iff BL is inserted at PC, in address space
4226 ASPACE. */
4227
4228static int
4229bp_location_inserted_here_p (struct bp_location *bl,
4230 struct address_space *aspace, CORE_ADDR pc)
4231{
4232 if (bl->inserted
4233 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4234 aspace, pc))
4235 {
4236 if (overlay_debugging
4237 && section_is_overlay (bl->section)
4238 && !section_is_mapped (bl->section))
4239 return 0; /* unmapped overlay -- can't be a match */
4240 else
4241 return 1;
4242 }
4243 return 0;
4244}
4245
a1fd2fa5 4246/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4247
4248int
a1fd2fa5 4249breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4250{
f7ce857f
PA
4251 struct bp_location **blp, **blp_tmp = NULL;
4252 struct bp_location *bl;
c906108c 4253
f7ce857f 4254 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4255 {
f7ce857f
PA
4256 struct bp_location *bl = *blp;
4257
35df4500
TJB
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4260 continue;
4261
f7ce857f
PA
4262 if (bp_location_inserted_here_p (bl, aspace, pc))
4263 return 1;
c5aa993b 4264 }
c36b740a
VP
4265 return 0;
4266}
4267
a1fd2fa5
PA
4268/* This function returns non-zero iff there is a software breakpoint
4269 inserted at PC. */
c36b740a
VP
4270
4271int
a1fd2fa5
PA
4272software_breakpoint_inserted_here_p (struct address_space *aspace,
4273 CORE_ADDR pc)
4fa8626c 4274{
f7ce857f
PA
4275 struct bp_location **blp, **blp_tmp = NULL;
4276 struct bp_location *bl;
4fa8626c 4277
f7ce857f 4278 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4279 {
f7ce857f
PA
4280 struct bp_location *bl = *blp;
4281
35df4500 4282 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4283 continue;
4284
f7ce857f
PA
4285 if (bp_location_inserted_here_p (bl, aspace, pc))
4286 return 1;
4fa8626c
DJ
4287 }
4288
4289 return 0;
9c02b525
PA
4290}
4291
4292/* See breakpoint.h. */
4293
4294int
4295hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4296 CORE_ADDR pc)
4297{
4298 struct bp_location **blp, **blp_tmp = NULL;
4299 struct bp_location *bl;
4300
4301 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4302 {
4303 struct bp_location *bl = *blp;
4304
4305 if (bl->loc_type != bp_loc_hardware_breakpoint)
4306 continue;
4307
4308 if (bp_location_inserted_here_p (bl, aspace, pc))
4309 return 1;
4310 }
4311
4312 return 0;
4fa8626c
DJ
4313}
4314
9093389c
PA
4315int
4316hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4317 CORE_ADDR addr, ULONGEST len)
4318{
4319 struct breakpoint *bpt;
4320
4321 ALL_BREAKPOINTS (bpt)
4322 {
4323 struct bp_location *loc;
4324
4325 if (bpt->type != bp_hardware_watchpoint
4326 && bpt->type != bp_access_watchpoint)
4327 continue;
4328
4329 if (!breakpoint_enabled (bpt))
4330 continue;
4331
4332 for (loc = bpt->loc; loc; loc = loc->next)
4333 if (loc->pspace->aspace == aspace && loc->inserted)
4334 {
4335 CORE_ADDR l, h;
4336
4337 /* Check for intersection. */
4338 l = max (loc->address, addr);
4339 h = min (loc->address + loc->length, addr + len);
4340 if (l < h)
4341 return 1;
4342 }
4343 }
4344 return 0;
4345}
c906108c 4346\f
c5aa993b 4347
c906108c
SS
4348/* bpstat stuff. External routines' interfaces are documented
4349 in breakpoint.h. */
4350
4351int
c326b90e 4352is_catchpoint (struct breakpoint *ep)
c906108c 4353{
533be4dd 4354 return (ep->type == bp_catchpoint);
c906108c
SS
4355}
4356
f431efe5
PA
4357/* Frees any storage that is part of a bpstat. Does not walk the
4358 'next' chain. */
4359
4360static void
198757a8
VP
4361bpstat_free (bpstat bs)
4362{
4363 if (bs->old_val != NULL)
4364 value_free (bs->old_val);
9add0f1b 4365 decref_counted_command_line (&bs->commands);
f431efe5 4366 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4367 xfree (bs);
4368}
4369
c906108c
SS
4370/* Clear a bpstat so that it says we are not at any breakpoint.
4371 Also free any storage that is part of a bpstat. */
4372
4373void
fba45db2 4374bpstat_clear (bpstat *bsp)
c906108c
SS
4375{
4376 bpstat p;
4377 bpstat q;
4378
4379 if (bsp == 0)
4380 return;
4381 p = *bsp;
4382 while (p != NULL)
4383 {
4384 q = p->next;
198757a8 4385 bpstat_free (p);
c906108c
SS
4386 p = q;
4387 }
4388 *bsp = NULL;
4389}
4390
4391/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4392 is part of the bpstat is copied as well. */
4393
4394bpstat
fba45db2 4395bpstat_copy (bpstat bs)
c906108c
SS
4396{
4397 bpstat p = NULL;
4398 bpstat tmp;
4399 bpstat retval = NULL;
4400
4401 if (bs == NULL)
4402 return bs;
4403
4404 for (; bs != NULL; bs = bs->next)
4405 {
4406 tmp = (bpstat) xmalloc (sizeof (*tmp));
4407 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4408 incref_counted_command_line (tmp->commands);
f431efe5 4409 incref_bp_location (tmp->bp_location_at);
31cc81e9 4410 if (bs->old_val != NULL)
3c3185ac
JK
4411 {
4412 tmp->old_val = value_copy (bs->old_val);
4413 release_value (tmp->old_val);
4414 }
31cc81e9 4415
c906108c
SS
4416 if (p == NULL)
4417 /* This is the first thing in the chain. */
4418 retval = tmp;
4419 else
4420 p->next = tmp;
4421 p = tmp;
4422 }
4423 p->next = NULL;
4424 return retval;
4425}
4426
4a64f543 4427/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4428
4429bpstat
fba45db2 4430bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4431{
c5aa993b
JM
4432 if (bsp == NULL)
4433 return NULL;
c906108c 4434
c5aa993b
JM
4435 for (; bsp != NULL; bsp = bsp->next)
4436 {
f431efe5 4437 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4438 return bsp;
4439 }
c906108c
SS
4440 return NULL;
4441}
4442
ab04a2af
TT
4443/* See breakpoint.h. */
4444
47591c29 4445int
427cd150 4446bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4447{
ab04a2af
TT
4448 for (; bsp != NULL; bsp = bsp->next)
4449 {
427cd150
TT
4450 if (bsp->breakpoint_at == NULL)
4451 {
4452 /* A moribund location can never explain a signal other than
4453 GDB_SIGNAL_TRAP. */
4454 if (sig == GDB_SIGNAL_TRAP)
47591c29 4455 return 1;
427cd150
TT
4456 }
4457 else
47591c29
PA
4458 {
4459 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4460 sig))
4461 return 1;
4462 }
ab04a2af
TT
4463 }
4464
47591c29 4465 return 0;
ab04a2af
TT
4466}
4467
4a64f543
MS
4468/* Put in *NUM the breakpoint number of the first breakpoint we are
4469 stopped at. *BSP upon return is a bpstat which points to the
4470 remaining breakpoints stopped at (but which is not guaranteed to be
4471 good for anything but further calls to bpstat_num).
4472
8671a17b
PA
4473 Return 0 if passed a bpstat which does not indicate any breakpoints.
4474 Return -1 if stopped at a breakpoint that has been deleted since
4475 we set it.
4476 Return 1 otherwise. */
c906108c
SS
4477
4478int
8671a17b 4479bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4480{
4481 struct breakpoint *b;
4482
4483 if ((*bsp) == NULL)
4484 return 0; /* No more breakpoint values */
8671a17b 4485
4a64f543
MS
4486 /* We assume we'll never have several bpstats that correspond to a
4487 single breakpoint -- otherwise, this function might return the
4488 same number more than once and this will look ugly. */
f431efe5 4489 b = (*bsp)->breakpoint_at;
8671a17b
PA
4490 *bsp = (*bsp)->next;
4491 if (b == NULL)
4492 return -1; /* breakpoint that's been deleted since */
4493
4494 *num = b->number; /* We have its number */
4495 return 1;
c906108c
SS
4496}
4497
e93ca019 4498/* See breakpoint.h. */
c906108c
SS
4499
4500void
e93ca019 4501bpstat_clear_actions (void)
c906108c 4502{
e93ca019
JK
4503 struct thread_info *tp;
4504 bpstat bs;
4505
4506 if (ptid_equal (inferior_ptid, null_ptid))
4507 return;
4508
4509 tp = find_thread_ptid (inferior_ptid);
4510 if (tp == NULL)
4511 return;
4512
4513 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4514 {
9add0f1b 4515 decref_counted_command_line (&bs->commands);
abf85f46 4516
c906108c
SS
4517 if (bs->old_val != NULL)
4518 {
4519 value_free (bs->old_val);
4520 bs->old_val = NULL;
4521 }
4522 }
4523}
4524
f3b1572e
PA
4525/* Called when a command is about to proceed the inferior. */
4526
4527static void
4528breakpoint_about_to_proceed (void)
4529{
4530 if (!ptid_equal (inferior_ptid, null_ptid))
4531 {
4532 struct thread_info *tp = inferior_thread ();
4533
4534 /* Allow inferior function calls in breakpoint commands to not
4535 interrupt the command list. When the call finishes
4536 successfully, the inferior will be standing at the same
4537 breakpoint as if nothing happened. */
16c381f0 4538 if (tp->control.in_infcall)
f3b1572e
PA
4539 return;
4540 }
4541
4542 breakpoint_proceeded = 1;
4543}
4544
4a64f543
MS
4545/* Stub for cleaning up our state if we error-out of a breakpoint
4546 command. */
c906108c 4547static void
4efb68b1 4548cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4549{
4550 executing_breakpoint_commands = 0;
4551}
4552
abf85f46
JK
4553/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4554 or its equivalent. */
4555
4556static int
4557command_line_is_silent (struct command_line *cmd)
4558{
4559 return cmd && (strcmp ("silent", cmd->line) == 0
4560 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4561}
4562
4a64f543
MS
4563/* Execute all the commands associated with all the breakpoints at
4564 this location. Any of these commands could cause the process to
4565 proceed beyond this point, etc. We look out for such changes by
4566 checking the global "breakpoint_proceeded" after each command.
c906108c 4567
347bddb7
PA
4568 Returns true if a breakpoint command resumed the inferior. In that
4569 case, it is the caller's responsibility to recall it again with the
4570 bpstat of the current thread. */
4571
4572static int
4573bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4574{
4575 bpstat bs;
4576 struct cleanup *old_chain;
347bddb7 4577 int again = 0;
c906108c
SS
4578
4579 /* Avoid endless recursion if a `source' command is contained
4580 in bs->commands. */
4581 if (executing_breakpoint_commands)
347bddb7 4582 return 0;
c906108c
SS
4583
4584 executing_breakpoint_commands = 1;
4585 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4586
cf6c5ffb
TT
4587 prevent_dont_repeat ();
4588
4a64f543 4589 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4590 bs = *bsp;
4591
4592 breakpoint_proceeded = 0;
4593 for (; bs != NULL; bs = bs->next)
4594 {
9add0f1b 4595 struct counted_command_line *ccmd;
6c50ab1c
JB
4596 struct command_line *cmd;
4597 struct cleanup *this_cmd_tree_chain;
4598
4599 /* Take ownership of the BSP's command tree, if it has one.
4600
4601 The command tree could legitimately contain commands like
4602 'step' and 'next', which call clear_proceed_status, which
4603 frees stop_bpstat's command tree. To make sure this doesn't
4604 free the tree we're executing out from under us, we need to
4605 take ownership of the tree ourselves. Since a given bpstat's
4606 commands are only executed once, we don't need to copy it; we
4607 can clear the pointer in the bpstat, and make sure we free
4608 the tree when we're done. */
9add0f1b
TT
4609 ccmd = bs->commands;
4610 bs->commands = NULL;
abf85f46
JK
4611 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4612 cmd = ccmd ? ccmd->commands : NULL;
4613 if (command_line_is_silent (cmd))
4614 {
4615 /* The action has been already done by bpstat_stop_status. */
4616 cmd = cmd->next;
4617 }
6c50ab1c 4618
c906108c
SS
4619 while (cmd != NULL)
4620 {
4621 execute_control_command (cmd);
4622
4623 if (breakpoint_proceeded)
4624 break;
4625 else
4626 cmd = cmd->next;
4627 }
6c50ab1c
JB
4628
4629 /* We can free this command tree now. */
4630 do_cleanups (this_cmd_tree_chain);
4631
c906108c 4632 if (breakpoint_proceeded)
32c1e744 4633 {
5589af0e 4634 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4635 /* If we are in async mode, then the target might be still
4636 running, not stopped at any breakpoint, so nothing for
4637 us to do here -- just return to the event loop. */
4638 ;
32c1e744
VP
4639 else
4640 /* In sync mode, when execute_control_command returns
4641 we're already standing on the next breakpoint.
347bddb7
PA
4642 Breakpoint commands for that stop were not run, since
4643 execute_command does not run breakpoint commands --
4644 only command_line_handler does, but that one is not
4645 involved in execution of breakpoint commands. So, we
4646 can now execute breakpoint commands. It should be
4647 noted that making execute_command do bpstat actions is
4648 not an option -- in this case we'll have recursive
4649 invocation of bpstat for each breakpoint with a
4650 command, and can easily blow up GDB stack. Instead, we
4651 return true, which will trigger the caller to recall us
4652 with the new stop_bpstat. */
4653 again = 1;
4654 break;
32c1e744 4655 }
c906108c 4656 }
c2b8ed2c 4657 do_cleanups (old_chain);
347bddb7
PA
4658 return again;
4659}
4660
4661void
4662bpstat_do_actions (void)
4663{
353d1d73
JK
4664 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4665
347bddb7
PA
4666 /* Do any commands attached to breakpoint we are stopped at. */
4667 while (!ptid_equal (inferior_ptid, null_ptid)
4668 && target_has_execution
4669 && !is_exited (inferior_ptid)
4670 && !is_executing (inferior_ptid))
4671 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4672 and only return when it is stopped at the next breakpoint, we
4673 keep doing breakpoint actions until it returns false to
4674 indicate the inferior was not resumed. */
16c381f0 4675 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4676 break;
353d1d73
JK
4677
4678 discard_cleanups (cleanup_if_error);
c906108c
SS
4679}
4680
fa4727a6
DJ
4681/* Print out the (old or new) value associated with a watchpoint. */
4682
4683static void
4684watchpoint_value_print (struct value *val, struct ui_file *stream)
4685{
4686 if (val == NULL)
4687 fprintf_unfiltered (stream, _("<unreadable>"));
4688 else
79a45b7d
TT
4689 {
4690 struct value_print_options opts;
4691 get_user_print_options (&opts);
4692 value_print (val, stream, &opts);
4693 }
fa4727a6
DJ
4694}
4695
e514a9d6 4696/* Generic routine for printing messages indicating why we
4a64f543 4697 stopped. The behavior of this function depends on the value
e514a9d6
JM
4698 'print_it' in the bpstat structure. Under some circumstances we
4699 may decide not to print anything here and delegate the task to
4a64f543 4700 normal_stop(). */
e514a9d6
JM
4701
4702static enum print_stop_action
4703print_bp_stop_message (bpstat bs)
4704{
4705 switch (bs->print_it)
4706 {
4707 case print_it_noop:
4a64f543 4708 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4709 return PRINT_UNKNOWN;
4710 break;
4711
4712 case print_it_done:
4713 /* We still want to print the frame, but we already printed the
4a64f543 4714 relevant messages. */
e514a9d6
JM
4715 return PRINT_SRC_AND_LOC;
4716 break;
4717
4718 case print_it_normal:
4f8d1dc6 4719 {
f431efe5
PA
4720 struct breakpoint *b = bs->breakpoint_at;
4721
1a6a67de
TJB
4722 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4723 which has since been deleted. */
4724 if (b == NULL)
4725 return PRINT_UNKNOWN;
4726
348d480f
PA
4727 /* Normal case. Call the breakpoint's print_it method. */
4728 return b->ops->print_it (bs);
4f8d1dc6 4729 }
348d480f 4730 break;
3086aeae 4731
e514a9d6 4732 default:
8e65ff28 4733 internal_error (__FILE__, __LINE__,
e2e0b3e5 4734 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4735 break;
c906108c 4736 }
c906108c
SS
4737}
4738
edcc5120
TT
4739/* A helper function that prints a shared library stopped event. */
4740
4741static void
4742print_solib_event (int is_catchpoint)
4743{
4744 int any_deleted
4745 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4746 int any_added
4747 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4748
4749 if (!is_catchpoint)
4750 {
4751 if (any_added || any_deleted)
4752 ui_out_text (current_uiout,
4753 _("Stopped due to shared library event:\n"));
4754 else
4755 ui_out_text (current_uiout,
4756 _("Stopped due to shared library event (no "
4757 "libraries added or removed)\n"));
4758 }
4759
4760 if (ui_out_is_mi_like_p (current_uiout))
4761 ui_out_field_string (current_uiout, "reason",
4762 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4763
4764 if (any_deleted)
4765 {
4766 struct cleanup *cleanup;
4767 char *name;
4768 int ix;
4769
4770 ui_out_text (current_uiout, _(" Inferior unloaded "));
4771 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4772 "removed");
4773 for (ix = 0;
4774 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4775 ix, name);
4776 ++ix)
4777 {
4778 if (ix > 0)
4779 ui_out_text (current_uiout, " ");
4780 ui_out_field_string (current_uiout, "library", name);
4781 ui_out_text (current_uiout, "\n");
4782 }
4783
4784 do_cleanups (cleanup);
4785 }
4786
4787 if (any_added)
4788 {
4789 struct so_list *iter;
4790 int ix;
4791 struct cleanup *cleanup;
4792
4793 ui_out_text (current_uiout, _(" Inferior loaded "));
4794 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4795 "added");
4796 for (ix = 0;
4797 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4798 ix, iter);
4799 ++ix)
4800 {
4801 if (ix > 0)
4802 ui_out_text (current_uiout, " ");
4803 ui_out_field_string (current_uiout, "library", iter->so_name);
4804 ui_out_text (current_uiout, "\n");
4805 }
4806
4807 do_cleanups (cleanup);
4808 }
4809}
4810
e514a9d6
JM
4811/* Print a message indicating what happened. This is called from
4812 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4813 list - a list of the eventpoints that caused this stop. KIND is
4814 the target_waitkind for the stopping event. This
e514a9d6
JM
4815 routine calls the generic print routine for printing a message
4816 about reasons for stopping. This will print (for example) the
4817 "Breakpoint n," part of the output. The return value of this
4818 routine is one of:
c906108c 4819
4a64f543 4820 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4821 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4822 code to print the location. An example is
c5aa993b
JM
4823 "Breakpoint 1, " which should be followed by
4824 the location.
917317f4 4825 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4826 to also print the location part of the message.
4827 An example is the catch/throw messages, which
4a64f543 4828 don't require a location appended to the end.
917317f4 4829 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4830 further info to be printed. */
c906108c 4831
917317f4 4832enum print_stop_action
36dfb11c 4833bpstat_print (bpstat bs, int kind)
c906108c
SS
4834{
4835 int val;
c5aa993b 4836
c906108c 4837 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4838 (Currently all watchpoints go on the bpstat whether hit or not.
4839 That probably could (should) be changed, provided care is taken
c906108c 4840 with respect to bpstat_explains_signal). */
e514a9d6
JM
4841 for (; bs; bs = bs->next)
4842 {
4843 val = print_bp_stop_message (bs);
4844 if (val == PRINT_SRC_ONLY
4845 || val == PRINT_SRC_AND_LOC
4846 || val == PRINT_NOTHING)
4847 return val;
4848 }
c906108c 4849
36dfb11c
TT
4850 /* If we had hit a shared library event breakpoint,
4851 print_bp_stop_message would print out this message. If we hit an
4852 OS-level shared library event, do the same thing. */
4853 if (kind == TARGET_WAITKIND_LOADED)
4854 {
edcc5120 4855 print_solib_event (0);
36dfb11c
TT
4856 return PRINT_NOTHING;
4857 }
4858
e514a9d6 4859 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4860 with and nothing was printed. */
917317f4 4861 return PRINT_UNKNOWN;
c906108c
SS
4862}
4863
c42bd95a
DE
4864/* Evaluate the expression EXP and return 1 if value is zero.
4865 This returns the inverse of the condition because it is called
4866 from catch_errors which returns 0 if an exception happened, and if an
4867 exception happens we want execution to stop.
4a64f543 4868 The argument is a "struct expression *" that has been cast to a
c42bd95a 4869 "void *" to make it pass through catch_errors. */
c906108c
SS
4870
4871static int
4efb68b1 4872breakpoint_cond_eval (void *exp)
c906108c 4873{
278cd55f 4874 struct value *mark = value_mark ();
c5aa993b 4875 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4876
c906108c
SS
4877 value_free_to_mark (mark);
4878 return i;
4879}
4880
5760d0ab 4881/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4882
4883static bpstat
5760d0ab 4884bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4885{
4886 bpstat bs;
4887
4888 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4889 bs->next = NULL;
4890 **bs_link_pointer = bs;
4891 *bs_link_pointer = &bs->next;
f431efe5
PA
4892 bs->breakpoint_at = bl->owner;
4893 bs->bp_location_at = bl;
4894 incref_bp_location (bl);
c906108c
SS
4895 /* If the condition is false, etc., don't do the commands. */
4896 bs->commands = NULL;
4897 bs->old_val = NULL;
4898 bs->print_it = print_it_normal;
4899 return bs;
4900}
4901\f
d983da9c
DJ
4902/* The target has stopped with waitstatus WS. Check if any hardware
4903 watchpoints have triggered, according to the target. */
4904
4905int
4906watchpoints_triggered (struct target_waitstatus *ws)
4907{
d92524f1 4908 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4909 CORE_ADDR addr;
4910 struct breakpoint *b;
4911
4912 if (!stopped_by_watchpoint)
4913 {
4914 /* We were not stopped by a watchpoint. Mark all watchpoints
4915 as not triggered. */
4916 ALL_BREAKPOINTS (b)
cc60f2e3 4917 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4918 {
4919 struct watchpoint *w = (struct watchpoint *) b;
4920
4921 w->watchpoint_triggered = watch_triggered_no;
4922 }
d983da9c
DJ
4923
4924 return 0;
4925 }
4926
4927 if (!target_stopped_data_address (&current_target, &addr))
4928 {
4929 /* We were stopped by a watchpoint, but we don't know where.
4930 Mark all watchpoints as unknown. */
4931 ALL_BREAKPOINTS (b)
cc60f2e3 4932 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_unknown;
4937 }
d983da9c 4938
3c4797ba 4939 return 1;
d983da9c
DJ
4940 }
4941
4942 /* The target could report the data address. Mark watchpoints
4943 affected by this data address as triggered, and all others as not
4944 triggered. */
4945
4946 ALL_BREAKPOINTS (b)
cc60f2e3 4947 if (is_hardware_watchpoint (b))
d983da9c 4948 {
3a5c3e22 4949 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4950 struct bp_location *loc;
d983da9c 4951
3a5c3e22 4952 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4953 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4954 {
3a5c3e22 4955 if (is_masked_watchpoint (b))
9c06b0b4 4956 {
3a5c3e22
PA
4957 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4958 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4959
4960 if (newaddr == start)
4961 {
3a5c3e22 4962 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4963 break;
4964 }
4965 }
4966 /* Exact match not required. Within range is sufficient. */
4967 else if (target_watchpoint_addr_within_range (&current_target,
4968 addr, loc->address,
4969 loc->length))
4970 {
3a5c3e22 4971 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4972 break;
4973 }
4974 }
d983da9c
DJ
4975 }
4976
4977 return 1;
4978}
4979
c906108c
SS
4980/* Possible return values for watchpoint_check (this can't be an enum
4981 because of check_errors). */
4982/* The watchpoint has been deleted. */
4983#define WP_DELETED 1
4984/* The value has changed. */
4985#define WP_VALUE_CHANGED 2
4986/* The value has not changed. */
4987#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4988/* Ignore this watchpoint, no matter if the value changed or not. */
4989#define WP_IGNORE 4
c906108c
SS
4990
4991#define BP_TEMPFLAG 1
4992#define BP_HARDWAREFLAG 2
4993
4a64f543
MS
4994/* Evaluate watchpoint condition expression and check if its value
4995 changed.
553e4c11
JB
4996
4997 P should be a pointer to struct bpstat, but is defined as a void *
4998 in order for this function to be usable with catch_errors. */
c906108c
SS
4999
5000static int
4efb68b1 5001watchpoint_check (void *p)
c906108c
SS
5002{
5003 bpstat bs = (bpstat) p;
3a5c3e22 5004 struct watchpoint *b;
c906108c
SS
5005 struct frame_info *fr;
5006 int within_current_scope;
5007
f431efe5 5008 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5009 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5010 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5011
f6bc2008
PA
5012 /* If this is a local watchpoint, we only want to check if the
5013 watchpoint frame is in scope if the current thread is the thread
5014 that was used to create the watchpoint. */
5015 if (!watchpoint_in_thread_scope (b))
60e1c644 5016 return WP_IGNORE;
f6bc2008 5017
c906108c
SS
5018 if (b->exp_valid_block == NULL)
5019 within_current_scope = 1;
5020 else
5021 {
edb3359d
DJ
5022 struct frame_info *frame = get_current_frame ();
5023 struct gdbarch *frame_arch = get_frame_arch (frame);
5024 CORE_ADDR frame_pc = get_frame_pc (frame);
5025
4a64f543
MS
5026 /* in_function_epilogue_p() returns a non-zero value if we're
5027 still in the function but the stack frame has already been
5028 invalidated. Since we can't rely on the values of local
5029 variables after the stack has been destroyed, we are treating
5030 the watchpoint in that state as `not changed' without further
5031 checking. Don't mark watchpoints as changed if the current
5032 frame is in an epilogue - even if they are in some other
5033 frame, our view of the stack is likely to be wrong and
5034 frame_find_by_id could error out. */
a0f49112 5035 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5036 return WP_IGNORE;
a0f49112 5037
101dcfbe 5038 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5039 within_current_scope = (fr != NULL);
69fbadd5
DJ
5040
5041 /* If we've gotten confused in the unwinder, we might have
5042 returned a frame that can't describe this variable. */
edb3359d
DJ
5043 if (within_current_scope)
5044 {
5045 struct symbol *function;
5046
5047 function = get_frame_function (fr);
5048 if (function == NULL
5049 || !contained_in (b->exp_valid_block,
5050 SYMBOL_BLOCK_VALUE (function)))
5051 within_current_scope = 0;
5052 }
69fbadd5 5053
edb3359d 5054 if (within_current_scope)
c906108c
SS
5055 /* If we end up stopping, the current frame will get selected
5056 in normal_stop. So this call to select_frame won't affect
5057 the user. */
0f7d239c 5058 select_frame (fr);
c906108c 5059 }
c5aa993b 5060
c906108c
SS
5061 if (within_current_scope)
5062 {
4a64f543
MS
5063 /* We use value_{,free_to_}mark because it could be a *long*
5064 time before we return to the command level and call
5065 free_all_values. We can't call free_all_values because we
5066 might be in the middle of evaluating a function call. */
c906108c 5067
0cf6dd15 5068 int pc = 0;
9c06b0b4 5069 struct value *mark;
fa4727a6
DJ
5070 struct value *new_val;
5071
3a5c3e22 5072 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5073 /* Since we don't know the exact trigger address (from
5074 stopped_data_address), just tell the user we've triggered
5075 a mask watchpoint. */
5076 return WP_VALUE_CHANGED;
5077
5078 mark = value_mark ();
3a1115a0 5079 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5080
bb9d5f81
PP
5081 if (b->val_bitsize != 0)
5082 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5083
4a64f543
MS
5084 /* We use value_equal_contents instead of value_equal because
5085 the latter coerces an array to a pointer, thus comparing just
5086 the address of the array instead of its contents. This is
5087 not what we want. */
fa4727a6 5088 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5089 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5090 {
fa4727a6
DJ
5091 if (new_val != NULL)
5092 {
5093 release_value (new_val);
5094 value_free_to_mark (mark);
5095 }
c906108c
SS
5096 bs->old_val = b->val;
5097 b->val = new_val;
fa4727a6 5098 b->val_valid = 1;
c906108c
SS
5099 return WP_VALUE_CHANGED;
5100 }
5101 else
5102 {
60e1c644 5103 /* Nothing changed. */
c906108c 5104 value_free_to_mark (mark);
c906108c
SS
5105 return WP_VALUE_NOT_CHANGED;
5106 }
5107 }
5108 else
5109 {
79a45e25
PA
5110 struct ui_out *uiout = current_uiout;
5111
c906108c 5112 /* This seems like the only logical thing to do because
c5aa993b
JM
5113 if we temporarily ignored the watchpoint, then when
5114 we reenter the block in which it is valid it contains
5115 garbage (in the case of a function, it may have two
5116 garbage values, one before and one after the prologue).
5117 So we can't even detect the first assignment to it and
5118 watch after that (since the garbage may or may not equal
5119 the first value assigned). */
348d480f
PA
5120 /* We print all the stop information in
5121 breakpoint_ops->print_it, but in this case, by the time we
5122 call breakpoint_ops->print_it this bp will be deleted
5123 already. So we have no choice but print the information
5124 here. */
9dc5e2a9 5125 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5126 ui_out_field_string
5127 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5128 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5129 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5130 ui_out_text (uiout,
5131 " deleted because the program has left the block in\n\
8b93c638 5132which its expression is valid.\n");
4ce44c66 5133
cdac0397 5134 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5135 decref_counted_command_line (&b->base.commands);
d0fb5eae 5136 watchpoint_del_at_next_stop (b);
c906108c
SS
5137
5138 return WP_DELETED;
5139 }
5140}
5141
18a18393 5142/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5143 breakpoint location BL. This function does not check if we should
5144 stop, only if BL explains the stop. */
5145
18a18393 5146static int
6c95b8df 5147bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5148 struct address_space *aspace, CORE_ADDR bp_addr,
5149 const struct target_waitstatus *ws)
18a18393
VP
5150{
5151 struct breakpoint *b = bl->owner;
5152
348d480f 5153 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5154 gdb_assert (b != NULL);
5155
09ac7c10 5156 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5157}
5158
3a5c3e22
PA
5159/* Determine if the watched values have actually changed, and we
5160 should stop. If not, set BS->stop to 0. */
5161
18a18393
VP
5162static void
5163bpstat_check_watchpoint (bpstat bs)
5164{
2bdf28a0 5165 const struct bp_location *bl;
3a5c3e22 5166 struct watchpoint *b;
2bdf28a0
JK
5167
5168 /* BS is built for existing struct breakpoint. */
f431efe5 5169 bl = bs->bp_location_at;
2bdf28a0 5170 gdb_assert (bl != NULL);
3a5c3e22 5171 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5172 gdb_assert (b != NULL);
18a18393 5173
18a18393 5174 {
18a18393
VP
5175 int must_check_value = 0;
5176
3a5c3e22 5177 if (b->base.type == bp_watchpoint)
18a18393
VP
5178 /* For a software watchpoint, we must always check the
5179 watched value. */
5180 must_check_value = 1;
5181 else if (b->watchpoint_triggered == watch_triggered_yes)
5182 /* We have a hardware watchpoint (read, write, or access)
5183 and the target earlier reported an address watched by
5184 this watchpoint. */
5185 must_check_value = 1;
5186 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5187 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5188 /* We were stopped by a hardware watchpoint, but the target could
5189 not report the data address. We must check the watchpoint's
5190 value. Access and read watchpoints are out of luck; without
5191 a data address, we can't figure it out. */
5192 must_check_value = 1;
3a5c3e22 5193
18a18393
VP
5194 if (must_check_value)
5195 {
3e43a32a
MS
5196 char *message
5197 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5198 b->base.number);
18a18393
VP
5199 struct cleanup *cleanups = make_cleanup (xfree, message);
5200 int e = catch_errors (watchpoint_check, bs, message,
5201 RETURN_MASK_ALL);
5202 do_cleanups (cleanups);
5203 switch (e)
5204 {
5205 case WP_DELETED:
5206 /* We've already printed what needs to be printed. */
5207 bs->print_it = print_it_done;
5208 /* Stop. */
5209 break;
60e1c644
PA
5210 case WP_IGNORE:
5211 bs->print_it = print_it_noop;
5212 bs->stop = 0;
5213 break;
18a18393 5214 case WP_VALUE_CHANGED:
3a5c3e22 5215 if (b->base.type == bp_read_watchpoint)
18a18393 5216 {
85d721b8
PA
5217 /* There are two cases to consider here:
5218
4a64f543 5219 1. We're watching the triggered memory for reads.
85d721b8
PA
5220 In that case, trust the target, and always report
5221 the watchpoint hit to the user. Even though
5222 reads don't cause value changes, the value may
5223 have changed since the last time it was read, and
5224 since we're not trapping writes, we will not see
5225 those, and as such we should ignore our notion of
5226 old value.
5227
4a64f543 5228 2. We're watching the triggered memory for both
85d721b8
PA
5229 reads and writes. There are two ways this may
5230 happen:
5231
4a64f543 5232 2.1. This is a target that can't break on data
85d721b8
PA
5233 reads only, but can break on accesses (reads or
5234 writes), such as e.g., x86. We detect this case
5235 at the time we try to insert read watchpoints.
5236
4a64f543 5237 2.2. Otherwise, the target supports read
85d721b8
PA
5238 watchpoints, but, the user set an access or write
5239 watchpoint watching the same memory as this read
5240 watchpoint.
5241
5242 If we're watching memory writes as well as reads,
5243 ignore watchpoint hits when we find that the
5244 value hasn't changed, as reads don't cause
5245 changes. This still gives false positives when
5246 the program writes the same value to memory as
5247 what there was already in memory (we will confuse
5248 it for a read), but it's much better than
5249 nothing. */
5250
5251 int other_write_watchpoint = 0;
5252
5253 if (bl->watchpoint_type == hw_read)
5254 {
5255 struct breakpoint *other_b;
5256
5257 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5258 if (other_b->type == bp_hardware_watchpoint
5259 || other_b->type == bp_access_watchpoint)
85d721b8 5260 {
3a5c3e22
PA
5261 struct watchpoint *other_w =
5262 (struct watchpoint *) other_b;
5263
5264 if (other_w->watchpoint_triggered
5265 == watch_triggered_yes)
5266 {
5267 other_write_watchpoint = 1;
5268 break;
5269 }
85d721b8
PA
5270 }
5271 }
5272
5273 if (other_write_watchpoint
5274 || bl->watchpoint_type == hw_access)
5275 {
5276 /* We're watching the same memory for writes,
5277 and the value changed since the last time we
5278 updated it, so this trap must be for a write.
5279 Ignore it. */
5280 bs->print_it = print_it_noop;
5281 bs->stop = 0;
5282 }
18a18393
VP
5283 }
5284 break;
5285 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5286 if (b->base.type == bp_hardware_watchpoint
5287 || b->base.type == bp_watchpoint)
18a18393
VP
5288 {
5289 /* Don't stop: write watchpoints shouldn't fire if
5290 the value hasn't changed. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 /* Stop. */
5295 break;
5296 default:
5297 /* Can't happen. */
5298 case 0:
5299 /* Error from catch_errors. */
3a5c3e22 5300 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5301 watchpoint_del_at_next_stop (b);
18a18393
VP
5302 /* We've already printed what needs to be printed. */
5303 bs->print_it = print_it_done;
5304 break;
5305 }
5306 }
5307 else /* must_check_value == 0 */
5308 {
5309 /* This is a case where some watchpoint(s) triggered, but
5310 not at the address of this watchpoint, or else no
5311 watchpoint triggered after all. So don't print
5312 anything for this watchpoint. */
5313 bs->print_it = print_it_noop;
5314 bs->stop = 0;
5315 }
5316 }
5317}
5318
7d4df6a4
DE
5319/* For breakpoints that are currently marked as telling gdb to stop,
5320 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5321 of breakpoint referred to by BS. If we should not stop for this
5322 breakpoint, set BS->stop to 0. */
f431efe5 5323
18a18393
VP
5324static void
5325bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5326{
2bdf28a0
JK
5327 const struct bp_location *bl;
5328 struct breakpoint *b;
7d4df6a4
DE
5329 int value_is_zero = 0;
5330 struct expression *cond;
5331
5332 gdb_assert (bs->stop);
2bdf28a0
JK
5333
5334 /* BS is built for existing struct breakpoint. */
f431efe5 5335 bl = bs->bp_location_at;
2bdf28a0 5336 gdb_assert (bl != NULL);
f431efe5 5337 b = bs->breakpoint_at;
2bdf28a0 5338 gdb_assert (b != NULL);
18a18393 5339
b775012e
LM
5340 /* Even if the target evaluated the condition on its end and notified GDB, we
5341 need to do so again since GDB does not know if we stopped due to a
5342 breakpoint or a single step breakpoint. */
5343
18a18393 5344 if (frame_id_p (b->frame_id)
edb3359d 5345 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5346 {
7d4df6a4
DE
5347 bs->stop = 0;
5348 return;
5349 }
60e1c644 5350
12ab52e9
PA
5351 /* If this is a thread/task-specific breakpoint, don't waste cpu
5352 evaluating the condition if this isn't the specified
5353 thread/task. */
5354 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5355 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5356
6c1b0f7b
DE
5357 {
5358 bs->stop = 0;
5359 return;
5360 }
5361
6dddc817
DE
5362 /* Evaluate extension language breakpoints that have a "stop" method
5363 implemented. */
5364 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5365
7d4df6a4
DE
5366 if (is_watchpoint (b))
5367 {
5368 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5369
7d4df6a4
DE
5370 cond = w->cond_exp;
5371 }
5372 else
5373 cond = bl->cond;
60e1c644 5374
7d4df6a4
DE
5375 if (cond && b->disposition != disp_del_at_next_stop)
5376 {
5377 int within_current_scope = 1;
5378 struct watchpoint * w;
60e1c644 5379
7d4df6a4
DE
5380 /* We use value_mark and value_free_to_mark because it could
5381 be a long time before we return to the command level and
5382 call free_all_values. We can't call free_all_values
5383 because we might be in the middle of evaluating a
5384 function call. */
5385 struct value *mark = value_mark ();
5386
5387 if (is_watchpoint (b))
5388 w = (struct watchpoint *) b;
5389 else
5390 w = NULL;
5391
5392 /* Need to select the frame, with all that implies so that
5393 the conditions will have the right context. Because we
5394 use the frame, we will not see an inlined function's
5395 variables when we arrive at a breakpoint at the start
5396 of the inlined function; the current frame will be the
5397 call site. */
5398 if (w == NULL || w->cond_exp_valid_block == NULL)
5399 select_frame (get_current_frame ());
5400 else
18a18393 5401 {
7d4df6a4
DE
5402 struct frame_info *frame;
5403
5404 /* For local watchpoint expressions, which particular
5405 instance of a local is being watched matters, so we
5406 keep track of the frame to evaluate the expression
5407 in. To evaluate the condition however, it doesn't
5408 really matter which instantiation of the function
5409 where the condition makes sense triggers the
5410 watchpoint. This allows an expression like "watch
5411 global if q > 10" set in `func', catch writes to
5412 global on all threads that call `func', or catch
5413 writes on all recursive calls of `func' by a single
5414 thread. We simply always evaluate the condition in
5415 the innermost frame that's executing where it makes
5416 sense to evaluate the condition. It seems
5417 intuitive. */
5418 frame = block_innermost_frame (w->cond_exp_valid_block);
5419 if (frame != NULL)
5420 select_frame (frame);
5421 else
5422 within_current_scope = 0;
18a18393 5423 }
7d4df6a4
DE
5424 if (within_current_scope)
5425 value_is_zero
5426 = catch_errors (breakpoint_cond_eval, cond,
5427 "Error in testing breakpoint condition:\n",
5428 RETURN_MASK_ALL);
5429 else
18a18393 5430 {
7d4df6a4
DE
5431 warning (_("Watchpoint condition cannot be tested "
5432 "in the current scope"));
5433 /* If we failed to set the right context for this
5434 watchpoint, unconditionally report it. */
5435 value_is_zero = 0;
18a18393 5436 }
7d4df6a4
DE
5437 /* FIXME-someday, should give breakpoint #. */
5438 value_free_to_mark (mark);
18a18393 5439 }
7d4df6a4
DE
5440
5441 if (cond && value_is_zero)
5442 {
5443 bs->stop = 0;
5444 }
7d4df6a4
DE
5445 else if (b->ignore_count > 0)
5446 {
5447 b->ignore_count--;
5448 bs->stop = 0;
5449 /* Increase the hit count even though we don't stop. */
5450 ++(b->hit_count);
5451 observer_notify_breakpoint_modified (b);
5452 }
18a18393
VP
5453}
5454
5455
9709f61c 5456/* Get a bpstat associated with having just stopped at address
d983da9c 5457 BP_ADDR in thread PTID.
c906108c 5458
d983da9c 5459 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5460 don't understand this stop. Result is a chain of bpstat's such
5461 that:
c906108c 5462
c5aa993b 5463 if we don't understand the stop, the result is a null pointer.
c906108c 5464
c5aa993b 5465 if we understand why we stopped, the result is not null.
c906108c 5466
c5aa993b
JM
5467 Each element of the chain refers to a particular breakpoint or
5468 watchpoint at which we have stopped. (We may have stopped for
5469 several reasons concurrently.)
c906108c 5470
c5aa993b
JM
5471 Each element of the chain has valid next, breakpoint_at,
5472 commands, FIXME??? fields. */
c906108c
SS
5473
5474bpstat
6c95b8df 5475bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5476 CORE_ADDR bp_addr, ptid_t ptid,
5477 const struct target_waitstatus *ws)
c906108c 5478{
0d381245 5479 struct breakpoint *b = NULL;
afe38095 5480 struct bp_location *bl;
20874c92 5481 struct bp_location *loc;
5760d0ab
JK
5482 /* First item of allocated bpstat's. */
5483 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5484 /* Pointer to the last thing in the chain currently. */
5760d0ab 5485 bpstat bs;
20874c92 5486 int ix;
429374b8 5487 int need_remove_insert;
f431efe5 5488 int removed_any;
c906108c 5489
f431efe5
PA
5490 /* First, build the bpstat chain with locations that explain a
5491 target stop, while being careful to not set the target running,
5492 as that may invalidate locations (in particular watchpoint
5493 locations are recreated). Resuming will happen here with
5494 breakpoint conditions or watchpoint expressions that include
5495 inferior function calls. */
c5aa993b 5496
429374b8
JK
5497 ALL_BREAKPOINTS (b)
5498 {
1a853c52 5499 if (!breakpoint_enabled (b))
429374b8 5500 continue;
a5606eee 5501
429374b8
JK
5502 for (bl = b->loc; bl != NULL; bl = bl->next)
5503 {
4a64f543
MS
5504 /* For hardware watchpoints, we look only at the first
5505 location. The watchpoint_check function will work on the
5506 entire expression, not the individual locations. For
5507 read watchpoints, the watchpoints_triggered function has
5508 checked all locations already. */
429374b8
JK
5509 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5510 break;
18a18393 5511
f6592439 5512 if (!bl->enabled || bl->shlib_disabled)
429374b8 5513 continue;
c5aa993b 5514
09ac7c10 5515 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5516 continue;
c5aa993b 5517
4a64f543
MS
5518 /* Come here if it's a watchpoint, or if the break address
5519 matches. */
c5aa993b 5520
4a64f543
MS
5521 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5522 explain stop. */
c5aa993b 5523
f431efe5
PA
5524 /* Assume we stop. Should we find a watchpoint that is not
5525 actually triggered, or if the condition of the breakpoint
5526 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5527 bs->stop = 1;
5528 bs->print = 1;
d983da9c 5529
f431efe5
PA
5530 /* If this is a scope breakpoint, mark the associated
5531 watchpoint as triggered so that we will handle the
5532 out-of-scope event. We'll get to the watchpoint next
5533 iteration. */
d0fb5eae 5534 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5535 {
5536 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5537
5538 w->watchpoint_triggered = watch_triggered_yes;
5539 }
f431efe5
PA
5540 }
5541 }
5542
7c16b83e 5543 /* Check if a moribund breakpoint explains the stop. */
f431efe5
PA
5544 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5545 {
f1310107 5546 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5547 {
5760d0ab 5548 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5549 /* For hits of moribund locations, we should just proceed. */
5550 bs->stop = 0;
5551 bs->print = 0;
5552 bs->print_it = print_it_noop;
5553 }
5554 }
5555
edcc5120
TT
5556 /* A bit of special processing for shlib breakpoints. We need to
5557 process solib loading here, so that the lists of loaded and
5558 unloaded libraries are correct before we handle "catch load" and
5559 "catch unload". */
5560 for (bs = bs_head; bs != NULL; bs = bs->next)
5561 {
5d268276 5562 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5563 {
5564 handle_solib_event ();
5565 break;
5566 }
5567 }
5568
f431efe5
PA
5569 /* Now go through the locations that caused the target to stop, and
5570 check whether we're interested in reporting this stop to higher
5571 layers, or whether we should resume the target transparently. */
5572
5573 removed_any = 0;
5574
5760d0ab 5575 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5576 {
5577 if (!bs->stop)
5578 continue;
5579
f431efe5 5580 b = bs->breakpoint_at;
348d480f
PA
5581 b->ops->check_status (bs);
5582 if (bs->stop)
28010a5d 5583 {
348d480f 5584 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5585
429374b8
JK
5586 if (bs->stop)
5587 {
5588 ++(b->hit_count);
8d3788bd 5589 observer_notify_breakpoint_modified (b);
c906108c 5590
4a64f543 5591 /* We will stop here. */
429374b8
JK
5592 if (b->disposition == disp_disable)
5593 {
816338b5 5594 --(b->enable_count);
1a853c52 5595 if (b->enable_count <= 0)
429374b8 5596 b->enable_state = bp_disabled;
f431efe5 5597 removed_any = 1;
429374b8
JK
5598 }
5599 if (b->silent)
5600 bs->print = 0;
5601 bs->commands = b->commands;
9add0f1b 5602 incref_counted_command_line (bs->commands);
abf85f46
JK
5603 if (command_line_is_silent (bs->commands
5604 ? bs->commands->commands : NULL))
5605 bs->print = 0;
9d6e6e84
HZ
5606
5607 b->ops->after_condition_true (bs);
429374b8
JK
5608 }
5609
348d480f 5610 }
a9b3a50f
PA
5611
5612 /* Print nothing for this entry if we don't stop or don't
5613 print. */
5614 if (!bs->stop || !bs->print)
5615 bs->print_it = print_it_noop;
429374b8 5616 }
876fa593 5617
d983da9c
DJ
5618 /* If we aren't stopping, the value of some hardware watchpoint may
5619 not have changed, but the intermediate memory locations we are
5620 watching may have. Don't bother if we're stopping; this will get
5621 done later. */
d832cb68 5622 need_remove_insert = 0;
5760d0ab
JK
5623 if (! bpstat_causes_stop (bs_head))
5624 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5625 if (!bs->stop
f431efe5
PA
5626 && bs->breakpoint_at
5627 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5628 {
3a5c3e22
PA
5629 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5630
5631 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5632 need_remove_insert = 1;
d983da9c
DJ
5633 }
5634
d832cb68 5635 if (need_remove_insert)
44702360 5636 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5637 else if (removed_any)
44702360 5638 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5639
5760d0ab 5640 return bs_head;
c906108c 5641}
628fe4e4
JK
5642
5643static void
5644handle_jit_event (void)
5645{
5646 struct frame_info *frame;
5647 struct gdbarch *gdbarch;
5648
5649 /* Switch terminal for any messages produced by
5650 breakpoint_re_set. */
5651 target_terminal_ours_for_output ();
5652
5653 frame = get_current_frame ();
5654 gdbarch = get_frame_arch (frame);
5655
5656 jit_event_handler (gdbarch);
5657
5658 target_terminal_inferior ();
5659}
5660
5661/* Prepare WHAT final decision for infrun. */
5662
5663/* Decide what infrun needs to do with this bpstat. */
5664
c906108c 5665struct bpstat_what
0e30163f 5666bpstat_what (bpstat bs_head)
c906108c 5667{
c906108c 5668 struct bpstat_what retval;
628fe4e4 5669 int jit_event = 0;
0e30163f 5670 bpstat bs;
c906108c 5671
628fe4e4 5672 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5673 retval.call_dummy = STOP_NONE;
186c406b 5674 retval.is_longjmp = 0;
628fe4e4 5675
0e30163f 5676 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5677 {
628fe4e4
JK
5678 /* Extract this BS's action. After processing each BS, we check
5679 if its action overrides all we've seem so far. */
5680 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5681 enum bptype bptype;
5682
c906108c 5683 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5684 {
5685 /* I suspect this can happen if it was a momentary
5686 breakpoint which has since been deleted. */
5687 bptype = bp_none;
5688 }
20874c92 5689 else
f431efe5 5690 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5691
5692 switch (bptype)
c906108c
SS
5693 {
5694 case bp_none:
628fe4e4 5695 break;
c906108c
SS
5696 case bp_breakpoint:
5697 case bp_hardware_breakpoint:
7c16b83e 5698 case bp_single_step:
c906108c
SS
5699 case bp_until:
5700 case bp_finish:
a9b3a50f 5701 case bp_shlib_event:
c906108c
SS
5702 if (bs->stop)
5703 {
5704 if (bs->print)
628fe4e4 5705 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5706 else
628fe4e4 5707 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5708 }
5709 else
628fe4e4 5710 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5711 break;
5712 case bp_watchpoint:
5713 case bp_hardware_watchpoint:
5714 case bp_read_watchpoint:
5715 case bp_access_watchpoint:
5716 if (bs->stop)
5717 {
5718 if (bs->print)
628fe4e4 5719 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5720 else
628fe4e4 5721 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5722 }
5723 else
628fe4e4
JK
5724 {
5725 /* There was a watchpoint, but we're not stopping.
5726 This requires no further action. */
5727 }
c906108c
SS
5728 break;
5729 case bp_longjmp:
e2e4d78b 5730 case bp_longjmp_call_dummy:
186c406b 5731 case bp_exception:
628fe4e4 5732 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5733 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5734 break;
5735 case bp_longjmp_resume:
186c406b 5736 case bp_exception_resume:
628fe4e4 5737 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5738 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5739 break;
5740 case bp_step_resume:
5741 if (bs->stop)
628fe4e4
JK
5742 this_action = BPSTAT_WHAT_STEP_RESUME;
5743 else
c906108c 5744 {
628fe4e4
JK
5745 /* It is for the wrong frame. */
5746 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5747 }
c906108c 5748 break;
2c03e5be
PA
5749 case bp_hp_step_resume:
5750 if (bs->stop)
5751 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5752 else
5753 {
5754 /* It is for the wrong frame. */
5755 this_action = BPSTAT_WHAT_SINGLE;
5756 }
5757 break;
c906108c 5758 case bp_watchpoint_scope:
c4093a6a 5759 case bp_thread_event:
1900040c 5760 case bp_overlay_event:
0fd8e87f 5761 case bp_longjmp_master:
aa7d318d 5762 case bp_std_terminate_master:
186c406b 5763 case bp_exception_master:
628fe4e4 5764 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5765 break;
ce78b96d 5766 case bp_catchpoint:
c5aa993b
JM
5767 if (bs->stop)
5768 {
5769 if (bs->print)
628fe4e4 5770 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5771 else
628fe4e4 5772 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5773 }
5774 else
628fe4e4
JK
5775 {
5776 /* There was a catchpoint, but we're not stopping.
5777 This requires no further action. */
5778 }
5779 break;
628fe4e4
JK
5780 case bp_jit_event:
5781 jit_event = 1;
5782 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5783 break;
c906108c 5784 case bp_call_dummy:
53a5351d
JM
5785 /* Make sure the action is stop (silent or noisy),
5786 so infrun.c pops the dummy frame. */
aa7d318d 5787 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5788 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5789 break;
5790 case bp_std_terminate:
5791 /* Make sure the action is stop (silent or noisy),
5792 so infrun.c pops the dummy frame. */
aa7d318d 5793 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5794 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5795 break;
1042e4c0 5796 case bp_tracepoint:
7a697b8d 5797 case bp_fast_tracepoint:
0fb4aa4b 5798 case bp_static_tracepoint:
1042e4c0
SS
5799 /* Tracepoint hits should not be reported back to GDB, and
5800 if one got through somehow, it should have been filtered
5801 out already. */
5802 internal_error (__FILE__, __LINE__,
7a697b8d 5803 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5804 break;
5805 case bp_gnu_ifunc_resolver:
5806 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5807 this_action = BPSTAT_WHAT_SINGLE;
5808 break;
5809 case bp_gnu_ifunc_resolver_return:
5810 /* The breakpoint will be removed, execution will restart from the
5811 PC of the former breakpoint. */
5812 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5813 break;
e7e0cddf
SS
5814
5815 case bp_dprintf:
a11cfd87
HZ
5816 if (bs->stop)
5817 this_action = BPSTAT_WHAT_STOP_SILENT;
5818 else
5819 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5820 break;
5821
628fe4e4
JK
5822 default:
5823 internal_error (__FILE__, __LINE__,
5824 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5825 }
628fe4e4
JK
5826
5827 retval.main_action = max (retval.main_action, this_action);
c906108c 5828 }
628fe4e4 5829
0e30163f
JK
5830 /* These operations may affect the bs->breakpoint_at state so they are
5831 delayed after MAIN_ACTION is decided above. */
5832
628fe4e4
JK
5833 if (jit_event)
5834 {
5835 if (debug_infrun)
5836 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5837
5838 handle_jit_event ();
5839 }
5840
0e30163f
JK
5841 for (bs = bs_head; bs != NULL; bs = bs->next)
5842 {
5843 struct breakpoint *b = bs->breakpoint_at;
5844
5845 if (b == NULL)
5846 continue;
5847 switch (b->type)
5848 {
5849 case bp_gnu_ifunc_resolver:
5850 gnu_ifunc_resolver_stop (b);
5851 break;
5852 case bp_gnu_ifunc_resolver_return:
5853 gnu_ifunc_resolver_return_stop (b);
5854 break;
5855 }
5856 }
5857
c906108c
SS
5858 return retval;
5859}
5860
5861/* Nonzero if we should step constantly (e.g. watchpoints on machines
5862 without hardware support). This isn't related to a specific bpstat,
5863 just to things like whether watchpoints are set. */
5864
c5aa993b 5865int
fba45db2 5866bpstat_should_step (void)
c906108c
SS
5867{
5868 struct breakpoint *b;
cc59ec59 5869
c906108c 5870 ALL_BREAKPOINTS (b)
717a8278 5871 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5872 return 1;
c906108c
SS
5873 return 0;
5874}
5875
67822962
PA
5876int
5877bpstat_causes_stop (bpstat bs)
5878{
5879 for (; bs != NULL; bs = bs->next)
5880 if (bs->stop)
5881 return 1;
5882
5883 return 0;
5884}
5885
c906108c 5886\f
c5aa993b 5887
170b53b2
UW
5888/* Compute a string of spaces suitable to indent the next line
5889 so it starts at the position corresponding to the table column
5890 named COL_NAME in the currently active table of UIOUT. */
5891
5892static char *
5893wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5894{
5895 static char wrap_indent[80];
5896 int i, total_width, width, align;
5897 char *text;
5898
5899 total_width = 0;
5900 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5901 {
5902 if (strcmp (text, col_name) == 0)
5903 {
5904 gdb_assert (total_width < sizeof wrap_indent);
5905 memset (wrap_indent, ' ', total_width);
5906 wrap_indent[total_width] = 0;
5907
5908 return wrap_indent;
5909 }
5910
5911 total_width += width + 1;
5912 }
5913
5914 return NULL;
5915}
5916
b775012e
LM
5917/* Determine if the locations of this breakpoint will have their conditions
5918 evaluated by the target, host or a mix of both. Returns the following:
5919
5920 "host": Host evals condition.
5921 "host or target": Host or Target evals condition.
5922 "target": Target evals condition.
5923*/
5924
5925static const char *
5926bp_condition_evaluator (struct breakpoint *b)
5927{
5928 struct bp_location *bl;
5929 char host_evals = 0;
5930 char target_evals = 0;
5931
5932 if (!b)
5933 return NULL;
5934
5935 if (!is_breakpoint (b))
5936 return NULL;
5937
5938 if (gdb_evaluates_breakpoint_condition_p ()
5939 || !target_supports_evaluation_of_breakpoint_conditions ())
5940 return condition_evaluation_host;
5941
5942 for (bl = b->loc; bl; bl = bl->next)
5943 {
5944 if (bl->cond_bytecode)
5945 target_evals++;
5946 else
5947 host_evals++;
5948 }
5949
5950 if (host_evals && target_evals)
5951 return condition_evaluation_both;
5952 else if (target_evals)
5953 return condition_evaluation_target;
5954 else
5955 return condition_evaluation_host;
5956}
5957
5958/* Determine the breakpoint location's condition evaluator. This is
5959 similar to bp_condition_evaluator, but for locations. */
5960
5961static const char *
5962bp_location_condition_evaluator (struct bp_location *bl)
5963{
5964 if (bl && !is_breakpoint (bl->owner))
5965 return NULL;
5966
5967 if (gdb_evaluates_breakpoint_condition_p ()
5968 || !target_supports_evaluation_of_breakpoint_conditions ())
5969 return condition_evaluation_host;
5970
5971 if (bl && bl->cond_bytecode)
5972 return condition_evaluation_target;
5973 else
5974 return condition_evaluation_host;
5975}
5976
859825b8
JK
5977/* Print the LOC location out of the list of B->LOC locations. */
5978
170b53b2
UW
5979static void
5980print_breakpoint_location (struct breakpoint *b,
5981 struct bp_location *loc)
0d381245 5982{
79a45e25 5983 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5984 struct cleanup *old_chain = save_current_program_space ();
5985
859825b8
JK
5986 if (loc != NULL && loc->shlib_disabled)
5987 loc = NULL;
5988
6c95b8df
PA
5989 if (loc != NULL)
5990 set_current_program_space (loc->pspace);
5991
56435ebe
TT
5992 if (b->display_canonical)
5993 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5994 else if (loc && loc->symtab)
0d381245
VP
5995 {
5996 struct symbol *sym
5997 = find_pc_sect_function (loc->address, loc->section);
5998 if (sym)
5999 {
6000 ui_out_text (uiout, "in ");
6001 ui_out_field_string (uiout, "func",
6002 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6003 ui_out_text (uiout, " ");
6004 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6005 ui_out_text (uiout, "at ");
0d381245 6006 }
05cba821
JK
6007 ui_out_field_string (uiout, "file",
6008 symtab_to_filename_for_display (loc->symtab));
0d381245 6009 ui_out_text (uiout, ":");
05cba821 6010
0d381245 6011 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6012 ui_out_field_string (uiout, "fullname",
6013 symtab_to_fullname (loc->symtab));
0d381245 6014
f8eba3c6 6015 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6016 }
859825b8 6017 else if (loc)
0d381245 6018 {
f99d8bf4
PA
6019 struct ui_file *stb = mem_fileopen ();
6020 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6021
f99d8bf4 6022 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6023 demangle, "");
0d381245 6024 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6025
6026 do_cleanups (stb_chain);
0d381245 6027 }
859825b8
JK
6028 else
6029 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6030
b775012e
LM
6031 if (loc && is_breakpoint (b)
6032 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6033 && bp_condition_evaluator (b) == condition_evaluation_both)
6034 {
6035 ui_out_text (uiout, " (");
6036 ui_out_field_string (uiout, "evaluated-by",
6037 bp_location_condition_evaluator (loc));
6038 ui_out_text (uiout, ")");
6039 }
6040
6c95b8df 6041 do_cleanups (old_chain);
0d381245
VP
6042}
6043
269b11a2
PA
6044static const char *
6045bptype_string (enum bptype type)
c906108c 6046{
c4093a6a
JM
6047 struct ep_type_description
6048 {
6049 enum bptype type;
6050 char *description;
6051 };
6052 static struct ep_type_description bptypes[] =
c906108c 6053 {
c5aa993b
JM
6054 {bp_none, "?deleted?"},
6055 {bp_breakpoint, "breakpoint"},
c906108c 6056 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6057 {bp_single_step, "sw single-step"},
c5aa993b
JM
6058 {bp_until, "until"},
6059 {bp_finish, "finish"},
6060 {bp_watchpoint, "watchpoint"},
c906108c 6061 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6062 {bp_read_watchpoint, "read watchpoint"},
6063 {bp_access_watchpoint, "acc watchpoint"},
6064 {bp_longjmp, "longjmp"},
6065 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6066 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6067 {bp_exception, "exception"},
6068 {bp_exception_resume, "exception resume"},
c5aa993b 6069 {bp_step_resume, "step resume"},
2c03e5be 6070 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6071 {bp_watchpoint_scope, "watchpoint scope"},
6072 {bp_call_dummy, "call dummy"},
aa7d318d 6073 {bp_std_terminate, "std::terminate"},
c5aa993b 6074 {bp_shlib_event, "shlib events"},
c4093a6a 6075 {bp_thread_event, "thread events"},
1900040c 6076 {bp_overlay_event, "overlay events"},
0fd8e87f 6077 {bp_longjmp_master, "longjmp master"},
aa7d318d 6078 {bp_std_terminate_master, "std::terminate master"},
186c406b 6079 {bp_exception_master, "exception master"},
ce78b96d 6080 {bp_catchpoint, "catchpoint"},
1042e4c0 6081 {bp_tracepoint, "tracepoint"},
7a697b8d 6082 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6083 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6084 {bp_dprintf, "dprintf"},
4efc6507 6085 {bp_jit_event, "jit events"},
0e30163f
JK
6086 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6087 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6088 };
269b11a2
PA
6089
6090 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6091 || ((int) type != bptypes[(int) type].type))
6092 internal_error (__FILE__, __LINE__,
6093 _("bptypes table does not describe type #%d."),
6094 (int) type);
6095
6096 return bptypes[(int) type].description;
6097}
6098
998580f1
MK
6099/* For MI, output a field named 'thread-groups' with a list as the value.
6100 For CLI, prefix the list with the string 'inf'. */
6101
6102static void
6103output_thread_groups (struct ui_out *uiout,
6104 const char *field_name,
6105 VEC(int) *inf_num,
6106 int mi_only)
6107{
752eb8b4 6108 struct cleanup *back_to;
998580f1
MK
6109 int is_mi = ui_out_is_mi_like_p (uiout);
6110 int inf;
6111 int i;
6112
6113 /* For backward compatibility, don't display inferiors in CLI unless
6114 there are several. Always display them for MI. */
6115 if (!is_mi && mi_only)
6116 return;
6117
752eb8b4
TT
6118 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6119
998580f1
MK
6120 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6121 {
6122 if (is_mi)
6123 {
6124 char mi_group[10];
6125
6126 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6127 ui_out_field_string (uiout, NULL, mi_group);
6128 }
6129 else
6130 {
6131 if (i == 0)
6132 ui_out_text (uiout, " inf ");
6133 else
6134 ui_out_text (uiout, ", ");
6135
6136 ui_out_text (uiout, plongest (inf));
6137 }
6138 }
6139
6140 do_cleanups (back_to);
6141}
6142
269b11a2
PA
6143/* Print B to gdb_stdout. */
6144
6145static void
6146print_one_breakpoint_location (struct breakpoint *b,
6147 struct bp_location *loc,
6148 int loc_number,
6149 struct bp_location **last_loc,
269b11a2
PA
6150 int allflag)
6151{
6152 struct command_line *l;
c2c6d25f 6153 static char bpenables[] = "nynny";
c906108c 6154
79a45e25 6155 struct ui_out *uiout = current_uiout;
0d381245
VP
6156 int header_of_multiple = 0;
6157 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6158 struct value_print_options opts;
6159
6160 get_user_print_options (&opts);
0d381245
VP
6161
6162 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6163 /* See comment in print_one_breakpoint concerning treatment of
6164 breakpoints with single disabled location. */
0d381245
VP
6165 if (loc == NULL
6166 && (b->loc != NULL
6167 && (b->loc->next != NULL || !b->loc->enabled)))
6168 header_of_multiple = 1;
6169 if (loc == NULL)
6170 loc = b->loc;
6171
c4093a6a
JM
6172 annotate_record ();
6173
6174 /* 1 */
6175 annotate_field (0);
0d381245
VP
6176 if (part_of_multiple)
6177 {
6178 char *formatted;
0c6773c1 6179 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6180 ui_out_field_string (uiout, "number", formatted);
6181 xfree (formatted);
6182 }
6183 else
6184 {
6185 ui_out_field_int (uiout, "number", b->number);
6186 }
c4093a6a
JM
6187
6188 /* 2 */
6189 annotate_field (1);
0d381245
VP
6190 if (part_of_multiple)
6191 ui_out_field_skip (uiout, "type");
269b11a2
PA
6192 else
6193 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6194
6195 /* 3 */
6196 annotate_field (2);
0d381245
VP
6197 if (part_of_multiple)
6198 ui_out_field_skip (uiout, "disp");
6199 else
2cec12e5 6200 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6201
c4093a6a
JM
6202
6203 /* 4 */
6204 annotate_field (3);
0d381245 6205 if (part_of_multiple)
54e52265 6206 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6207 else
4a64f543
MS
6208 ui_out_field_fmt (uiout, "enabled", "%c",
6209 bpenables[(int) b->enable_state]);
54e52265 6210 ui_out_spaces (uiout, 2);
0d381245 6211
c4093a6a
JM
6212
6213 /* 5 and 6 */
3086aeae 6214 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6215 {
4a64f543
MS
6216 /* Although the print_one can possibly print all locations,
6217 calling it here is not likely to get any nice result. So,
6218 make sure there's just one location. */
0d381245 6219 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6220 b->ops->print_one (b, last_loc);
0d381245 6221 }
3086aeae
DJ
6222 else
6223 switch (b->type)
6224 {
6225 case bp_none:
6226 internal_error (__FILE__, __LINE__,
e2e0b3e5 6227 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6228 break;
c906108c 6229
3086aeae
DJ
6230 case bp_watchpoint:
6231 case bp_hardware_watchpoint:
6232 case bp_read_watchpoint:
6233 case bp_access_watchpoint:
3a5c3e22
PA
6234 {
6235 struct watchpoint *w = (struct watchpoint *) b;
6236
6237 /* Field 4, the address, is omitted (which makes the columns
6238 not line up too nicely with the headers, but the effect
6239 is relatively readable). */
6240 if (opts.addressprint)
6241 ui_out_field_skip (uiout, "addr");
6242 annotate_field (5);
6243 ui_out_field_string (uiout, "what", w->exp_string);
6244 }
3086aeae
DJ
6245 break;
6246
3086aeae
DJ
6247 case bp_breakpoint:
6248 case bp_hardware_breakpoint:
7c16b83e 6249 case bp_single_step:
3086aeae
DJ
6250 case bp_until:
6251 case bp_finish:
6252 case bp_longjmp:
6253 case bp_longjmp_resume:
e2e4d78b 6254 case bp_longjmp_call_dummy:
186c406b
TT
6255 case bp_exception:
6256 case bp_exception_resume:
3086aeae 6257 case bp_step_resume:
2c03e5be 6258 case bp_hp_step_resume:
3086aeae
DJ
6259 case bp_watchpoint_scope:
6260 case bp_call_dummy:
aa7d318d 6261 case bp_std_terminate:
3086aeae
DJ
6262 case bp_shlib_event:
6263 case bp_thread_event:
6264 case bp_overlay_event:
0fd8e87f 6265 case bp_longjmp_master:
aa7d318d 6266 case bp_std_terminate_master:
186c406b 6267 case bp_exception_master:
1042e4c0 6268 case bp_tracepoint:
7a697b8d 6269 case bp_fast_tracepoint:
0fb4aa4b 6270 case bp_static_tracepoint:
e7e0cddf 6271 case bp_dprintf:
4efc6507 6272 case bp_jit_event:
0e30163f
JK
6273 case bp_gnu_ifunc_resolver:
6274 case bp_gnu_ifunc_resolver_return:
79a45b7d 6275 if (opts.addressprint)
3086aeae
DJ
6276 {
6277 annotate_field (4);
54e52265 6278 if (header_of_multiple)
0d381245 6279 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6280 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6281 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6282 else
5af949e3
UW
6283 ui_out_field_core_addr (uiout, "addr",
6284 loc->gdbarch, loc->address);
3086aeae
DJ
6285 }
6286 annotate_field (5);
0d381245 6287 if (!header_of_multiple)
170b53b2 6288 print_breakpoint_location (b, loc);
0d381245 6289 if (b->loc)
a6d9a66e 6290 *last_loc = b->loc;
3086aeae
DJ
6291 break;
6292 }
c906108c 6293
6c95b8df 6294
998580f1 6295 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6296 {
6297 struct inferior *inf;
998580f1
MK
6298 VEC(int) *inf_num = NULL;
6299 int mi_only = 1;
6c95b8df 6300
998580f1 6301 ALL_INFERIORS (inf)
6c95b8df
PA
6302 {
6303 if (inf->pspace == loc->pspace)
998580f1 6304 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6305 }
998580f1
MK
6306
6307 /* For backward compatibility, don't display inferiors in CLI unless
6308 there are several. Always display for MI. */
6309 if (allflag
6310 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6311 && (number_of_program_spaces () > 1
6312 || number_of_inferiors () > 1)
6313 /* LOC is for existing B, it cannot be in
6314 moribund_locations and thus having NULL OWNER. */
6315 && loc->owner->type != bp_catchpoint))
6316 mi_only = 0;
6317 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6318 VEC_free (int, inf_num);
6c95b8df
PA
6319 }
6320
4a306c9a 6321 if (!part_of_multiple)
c4093a6a 6322 {
4a306c9a
JB
6323 if (b->thread != -1)
6324 {
6325 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6326 "stop only in" line a little further down. */
4a306c9a
JB
6327 ui_out_text (uiout, " thread ");
6328 ui_out_field_int (uiout, "thread", b->thread);
6329 }
6330 else if (b->task != 0)
6331 {
6332 ui_out_text (uiout, " task ");
6333 ui_out_field_int (uiout, "task", b->task);
6334 }
c4093a6a 6335 }
f1310107 6336
8b93c638 6337 ui_out_text (uiout, "\n");
f1310107 6338
348d480f 6339 if (!part_of_multiple)
f1310107
TJB
6340 b->ops->print_one_detail (b, uiout);
6341
0d381245 6342 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6343 {
6344 annotate_field (6);
8b93c638 6345 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6346 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6347 the frame ID. */
5af949e3
UW
6348 ui_out_field_core_addr (uiout, "frame",
6349 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6350 ui_out_text (uiout, "\n");
c4093a6a
JM
6351 }
6352
28010a5d 6353 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6354 {
6355 annotate_field (7);
d77f58be 6356 if (is_tracepoint (b))
1042e4c0
SS
6357 ui_out_text (uiout, "\ttrace only if ");
6358 else
6359 ui_out_text (uiout, "\tstop only if ");
0101ce28 6360 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6361
6362 /* Print whether the target is doing the breakpoint's condition
6363 evaluation. If GDB is doing the evaluation, don't print anything. */
6364 if (is_breakpoint (b)
6365 && breakpoint_condition_evaluation_mode ()
6366 == condition_evaluation_target)
6367 {
6368 ui_out_text (uiout, " (");
6369 ui_out_field_string (uiout, "evaluated-by",
6370 bp_condition_evaluator (b));
6371 ui_out_text (uiout, " evals)");
6372 }
0101ce28
JJ
6373 ui_out_text (uiout, "\n");
6374 }
6375
0d381245 6376 if (!part_of_multiple && b->thread != -1)
c4093a6a 6377 {
4a64f543 6378 /* FIXME should make an annotation for this. */
8b93c638
JM
6379 ui_out_text (uiout, "\tstop only in thread ");
6380 ui_out_field_int (uiout, "thread", b->thread);
6381 ui_out_text (uiout, "\n");
c4093a6a
JM
6382 }
6383
556ec64d
YQ
6384 if (!part_of_multiple)
6385 {
6386 if (b->hit_count)
31f56a27
YQ
6387 {
6388 /* FIXME should make an annotation for this. */
6389 if (is_catchpoint (b))
6390 ui_out_text (uiout, "\tcatchpoint");
6391 else if (is_tracepoint (b))
6392 ui_out_text (uiout, "\ttracepoint");
6393 else
6394 ui_out_text (uiout, "\tbreakpoint");
6395 ui_out_text (uiout, " already hit ");
6396 ui_out_field_int (uiout, "times", b->hit_count);
6397 if (b->hit_count == 1)
6398 ui_out_text (uiout, " time\n");
6399 else
6400 ui_out_text (uiout, " times\n");
6401 }
556ec64d
YQ
6402 else
6403 {
31f56a27
YQ
6404 /* Output the count also if it is zero, but only if this is mi. */
6405 if (ui_out_is_mi_like_p (uiout))
6406 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6407 }
6408 }
8b93c638 6409
0d381245 6410 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6411 {
6412 annotate_field (8);
8b93c638
JM
6413 ui_out_text (uiout, "\tignore next ");
6414 ui_out_field_int (uiout, "ignore", b->ignore_count);
6415 ui_out_text (uiout, " hits\n");
c4093a6a 6416 }
059fb39f 6417
816338b5
SS
6418 /* Note that an enable count of 1 corresponds to "enable once"
6419 behavior, which is reported by the combination of enablement and
6420 disposition, so we don't need to mention it here. */
6421 if (!part_of_multiple && b->enable_count > 1)
6422 {
6423 annotate_field (8);
6424 ui_out_text (uiout, "\tdisable after ");
6425 /* Tweak the wording to clarify that ignore and enable counts
6426 are distinct, and have additive effect. */
6427 if (b->ignore_count)
6428 ui_out_text (uiout, "additional ");
6429 else
6430 ui_out_text (uiout, "next ");
6431 ui_out_field_int (uiout, "enable", b->enable_count);
6432 ui_out_text (uiout, " hits\n");
6433 }
6434
f196051f
SS
6435 if (!part_of_multiple && is_tracepoint (b))
6436 {
6437 struct tracepoint *tp = (struct tracepoint *) b;
6438
6439 if (tp->traceframe_usage)
6440 {
6441 ui_out_text (uiout, "\ttrace buffer usage ");
6442 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6443 ui_out_text (uiout, " bytes\n");
6444 }
6445 }
d3ce09f5 6446
9add0f1b 6447 l = b->commands ? b->commands->commands : NULL;
059fb39f 6448 if (!part_of_multiple && l)
c4093a6a 6449 {
3b31d625
EZ
6450 struct cleanup *script_chain;
6451
c4093a6a 6452 annotate_field (9);
3b31d625 6453 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6454 print_command_lines (uiout, l, 4);
3b31d625 6455 do_cleanups (script_chain);
c4093a6a 6456 }
d24317b4 6457
d9b3f62e 6458 if (is_tracepoint (b))
1042e4c0 6459 {
d9b3f62e
PA
6460 struct tracepoint *t = (struct tracepoint *) b;
6461
6462 if (!part_of_multiple && t->pass_count)
6463 {
6464 annotate_field (10);
6465 ui_out_text (uiout, "\tpass count ");
6466 ui_out_field_int (uiout, "pass", t->pass_count);
6467 ui_out_text (uiout, " \n");
6468 }
f2a8bc8a
YQ
6469
6470 /* Don't display it when tracepoint or tracepoint location is
6471 pending. */
6472 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6473 {
6474 annotate_field (11);
6475
6476 if (ui_out_is_mi_like_p (uiout))
6477 ui_out_field_string (uiout, "installed",
6478 loc->inserted ? "y" : "n");
6479 else
6480 {
6481 if (loc->inserted)
6482 ui_out_text (uiout, "\t");
6483 else
6484 ui_out_text (uiout, "\tnot ");
6485 ui_out_text (uiout, "installed on target\n");
6486 }
6487 }
1042e4c0
SS
6488 }
6489
d24317b4
VP
6490 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6491 {
3a5c3e22
PA
6492 if (is_watchpoint (b))
6493 {
6494 struct watchpoint *w = (struct watchpoint *) b;
6495
6496 ui_out_field_string (uiout, "original-location", w->exp_string);
6497 }
6498 else if (b->addr_string)
d24317b4 6499 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6500 }
c4093a6a 6501}
c5aa993b 6502
0d381245
VP
6503static void
6504print_one_breakpoint (struct breakpoint *b,
4a64f543 6505 struct bp_location **last_loc,
6c95b8df 6506 int allflag)
0d381245 6507{
8d3788bd 6508 struct cleanup *bkpt_chain;
79a45e25 6509 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6510
6511 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6512
12c5a436 6513 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6514 do_cleanups (bkpt_chain);
0d381245
VP
6515
6516 /* If this breakpoint has custom print function,
6517 it's already printed. Otherwise, print individual
6518 locations, if any. */
6519 if (b->ops == NULL || b->ops->print_one == NULL)
6520 {
4a64f543
MS
6521 /* If breakpoint has a single location that is disabled, we
6522 print it as if it had several locations, since otherwise it's
6523 hard to represent "breakpoint enabled, location disabled"
6524 situation.
6525
6526 Note that while hardware watchpoints have several locations
a3be7890 6527 internally, that's not a property exposed to user. */
0d381245 6528 if (b->loc
a5606eee 6529 && !is_hardware_watchpoint (b)
8d3788bd 6530 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6531 {
6532 struct bp_location *loc;
6533 int n = 1;
8d3788bd 6534
0d381245 6535 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6536 {
6537 struct cleanup *inner2 =
6538 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6539 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6540 do_cleanups (inner2);
6541 }
0d381245
VP
6542 }
6543 }
6544}
6545
a6d9a66e
UW
6546static int
6547breakpoint_address_bits (struct breakpoint *b)
6548{
6549 int print_address_bits = 0;
6550 struct bp_location *loc;
6551
6552 for (loc = b->loc; loc; loc = loc->next)
6553 {
c7437ca6
PA
6554 int addr_bit;
6555
6556 /* Software watchpoints that aren't watching memory don't have
6557 an address to print. */
6558 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6559 continue;
6560
6561 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6562 if (addr_bit > print_address_bits)
6563 print_address_bits = addr_bit;
6564 }
6565
6566 return print_address_bits;
6567}
0d381245 6568
c4093a6a
JM
6569struct captured_breakpoint_query_args
6570 {
6571 int bnum;
6572 };
c5aa993b 6573
c4093a6a 6574static int
2b65245e 6575do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6576{
6577 struct captured_breakpoint_query_args *args = data;
52f0bd74 6578 struct breakpoint *b;
a6d9a66e 6579 struct bp_location *dummy_loc = NULL;
cc59ec59 6580
c4093a6a
JM
6581 ALL_BREAKPOINTS (b)
6582 {
6583 if (args->bnum == b->number)
c5aa993b 6584 {
12c5a436 6585 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6586 return GDB_RC_OK;
c5aa993b 6587 }
c4093a6a
JM
6588 }
6589 return GDB_RC_NONE;
6590}
c5aa993b 6591
c4093a6a 6592enum gdb_rc
4a64f543
MS
6593gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6594 char **error_message)
c4093a6a
JM
6595{
6596 struct captured_breakpoint_query_args args;
cc59ec59 6597
c4093a6a
JM
6598 args.bnum = bnum;
6599 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6600 an error. */
b0b13bb4
DJ
6601 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6602 error_message, RETURN_MASK_ALL) < 0)
6603 return GDB_RC_FAIL;
6604 else
6605 return GDB_RC_OK;
c4093a6a 6606}
c5aa993b 6607
09d682a4
TT
6608/* Return true if this breakpoint was set by the user, false if it is
6609 internal or momentary. */
6610
6611int
6612user_breakpoint_p (struct breakpoint *b)
6613{
46c6471b 6614 return b->number > 0;
09d682a4
TT
6615}
6616
7f3b0473 6617/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6618 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6619 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6620 FILTER is non-NULL, call it on each breakpoint and only include the
6621 ones for which it returns non-zero. Return the total number of
6622 breakpoints listed. */
c906108c 6623
d77f58be 6624static int
e5a67952 6625breakpoint_1 (char *args, int allflag,
4a64f543 6626 int (*filter) (const struct breakpoint *))
c4093a6a 6627{
52f0bd74 6628 struct breakpoint *b;
a6d9a66e 6629 struct bp_location *last_loc = NULL;
7f3b0473 6630 int nr_printable_breakpoints;
3b31d625 6631 struct cleanup *bkpttbl_chain;
79a45b7d 6632 struct value_print_options opts;
a6d9a66e 6633 int print_address_bits = 0;
269b11a2 6634 int print_type_col_width = 14;
79a45e25 6635 struct ui_out *uiout = current_uiout;
269b11a2 6636
79a45b7d
TT
6637 get_user_print_options (&opts);
6638
4a64f543
MS
6639 /* Compute the number of rows in the table, as well as the size
6640 required for address fields. */
7f3b0473
AC
6641 nr_printable_breakpoints = 0;
6642 ALL_BREAKPOINTS (b)
e5a67952
MS
6643 {
6644 /* If we have a filter, only list the breakpoints it accepts. */
6645 if (filter && !filter (b))
6646 continue;
6647
6648 /* If we have an "args" string, it is a list of breakpoints to
6649 accept. Skip the others. */
6650 if (args != NULL && *args != '\0')
6651 {
6652 if (allflag && parse_and_eval_long (args) != b->number)
6653 continue;
6654 if (!allflag && !number_is_in_list (args, b->number))
6655 continue;
6656 }
269b11a2 6657
e5a67952
MS
6658 if (allflag || user_breakpoint_p (b))
6659 {
6660 int addr_bit, type_len;
a6d9a66e 6661
e5a67952
MS
6662 addr_bit = breakpoint_address_bits (b);
6663 if (addr_bit > print_address_bits)
6664 print_address_bits = addr_bit;
269b11a2 6665
e5a67952
MS
6666 type_len = strlen (bptype_string (b->type));
6667 if (type_len > print_type_col_width)
6668 print_type_col_width = type_len;
6669
6670 nr_printable_breakpoints++;
6671 }
6672 }
7f3b0473 6673
79a45b7d 6674 if (opts.addressprint)
3b31d625 6675 bkpttbl_chain
3e43a32a
MS
6676 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6677 nr_printable_breakpoints,
3b31d625 6678 "BreakpointTable");
8b93c638 6679 else
3b31d625 6680 bkpttbl_chain
3e43a32a
MS
6681 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6682 nr_printable_breakpoints,
3b31d625 6683 "BreakpointTable");
8b93c638 6684
7f3b0473 6685 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6686 annotate_breakpoints_headers ();
6687 if (nr_printable_breakpoints > 0)
6688 annotate_field (0);
4a64f543 6689 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6690 if (nr_printable_breakpoints > 0)
6691 annotate_field (1);
269b11a2 6692 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6693 "type", "Type"); /* 2 */
d7faa9e7
AC
6694 if (nr_printable_breakpoints > 0)
6695 annotate_field (2);
4a64f543 6696 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6697 if (nr_printable_breakpoints > 0)
6698 annotate_field (3);
54e52265 6699 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6700 if (opts.addressprint)
e5a67952
MS
6701 {
6702 if (nr_printable_breakpoints > 0)
6703 annotate_field (4);
6704 if (print_address_bits <= 32)
6705 ui_out_table_header (uiout, 10, ui_left,
6706 "addr", "Address"); /* 5 */
6707 else
6708 ui_out_table_header (uiout, 18, ui_left,
6709 "addr", "Address"); /* 5 */
6710 }
d7faa9e7
AC
6711 if (nr_printable_breakpoints > 0)
6712 annotate_field (5);
6713 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6714 ui_out_table_body (uiout);
6715 if (nr_printable_breakpoints > 0)
6716 annotate_breakpoints_table ();
7f3b0473 6717
c4093a6a 6718 ALL_BREAKPOINTS (b)
e5a67952
MS
6719 {
6720 QUIT;
6721 /* If we have a filter, only list the breakpoints it accepts. */
6722 if (filter && !filter (b))
6723 continue;
6724
6725 /* If we have an "args" string, it is a list of breakpoints to
6726 accept. Skip the others. */
6727
6728 if (args != NULL && *args != '\0')
6729 {
6730 if (allflag) /* maintenance info breakpoint */
6731 {
6732 if (parse_and_eval_long (args) != b->number)
6733 continue;
6734 }
6735 else /* all others */
6736 {
6737 if (!number_is_in_list (args, b->number))
6738 continue;
6739 }
6740 }
6741 /* We only print out user settable breakpoints unless the
6742 allflag is set. */
6743 if (allflag || user_breakpoint_p (b))
12c5a436 6744 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6745 }
6746
3b31d625 6747 do_cleanups (bkpttbl_chain);
698384cd 6748
7f3b0473 6749 if (nr_printable_breakpoints == 0)
c906108c 6750 {
4a64f543
MS
6751 /* If there's a filter, let the caller decide how to report
6752 empty list. */
d77f58be
SS
6753 if (!filter)
6754 {
e5a67952 6755 if (args == NULL || *args == '\0')
d77f58be
SS
6756 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6757 else
4a64f543 6758 ui_out_message (uiout, 0,
e5a67952
MS
6759 "No breakpoint or watchpoint matching '%s'.\n",
6760 args);
d77f58be 6761 }
c906108c
SS
6762 }
6763 else
c4093a6a 6764 {
a6d9a66e
UW
6765 if (last_loc && !server_command)
6766 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6767 }
c906108c 6768
4a64f543 6769 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6770 there have been breakpoints? */
c906108c 6771 annotate_breakpoints_table_end ();
d77f58be
SS
6772
6773 return nr_printable_breakpoints;
c906108c
SS
6774}
6775
ad443146
SS
6776/* Display the value of default-collect in a way that is generally
6777 compatible with the breakpoint list. */
6778
6779static void
6780default_collect_info (void)
6781{
79a45e25
PA
6782 struct ui_out *uiout = current_uiout;
6783
ad443146
SS
6784 /* If it has no value (which is frequently the case), say nothing; a
6785 message like "No default-collect." gets in user's face when it's
6786 not wanted. */
6787 if (!*default_collect)
6788 return;
6789
6790 /* The following phrase lines up nicely with per-tracepoint collect
6791 actions. */
6792 ui_out_text (uiout, "default collect ");
6793 ui_out_field_string (uiout, "default-collect", default_collect);
6794 ui_out_text (uiout, " \n");
6795}
6796
c906108c 6797static void
e5a67952 6798breakpoints_info (char *args, int from_tty)
c906108c 6799{
e5a67952 6800 breakpoint_1 (args, 0, NULL);
ad443146
SS
6801
6802 default_collect_info ();
d77f58be
SS
6803}
6804
6805static void
e5a67952 6806watchpoints_info (char *args, int from_tty)
d77f58be 6807{
e5a67952 6808 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6809 struct ui_out *uiout = current_uiout;
d77f58be
SS
6810
6811 if (num_printed == 0)
6812 {
e5a67952 6813 if (args == NULL || *args == '\0')
d77f58be
SS
6814 ui_out_message (uiout, 0, "No watchpoints.\n");
6815 else
e5a67952 6816 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6817 }
c906108c
SS
6818}
6819
7a292a7a 6820static void
e5a67952 6821maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6822{
e5a67952 6823 breakpoint_1 (args, 1, NULL);
ad443146
SS
6824
6825 default_collect_info ();
c906108c
SS
6826}
6827
0d381245 6828static int
714835d5 6829breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6830 struct program_space *pspace,
714835d5 6831 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6832{
6833 struct bp_location *bl = b->loc;
cc59ec59 6834
0d381245
VP
6835 for (; bl; bl = bl->next)
6836 {
6c95b8df
PA
6837 if (bl->pspace == pspace
6838 && bl->address == pc
0d381245
VP
6839 && (!overlay_debugging || bl->section == section))
6840 return 1;
6841 }
6842 return 0;
6843}
6844
672f9b60 6845/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6846 concerns with logical breakpoints, so we match program spaces, not
6847 address spaces. */
c906108c
SS
6848
6849static void
6c95b8df
PA
6850describe_other_breakpoints (struct gdbarch *gdbarch,
6851 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6852 struct obj_section *section, int thread)
c906108c 6853{
52f0bd74
AC
6854 int others = 0;
6855 struct breakpoint *b;
c906108c
SS
6856
6857 ALL_BREAKPOINTS (b)
672f9b60
KP
6858 others += (user_breakpoint_p (b)
6859 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6860 if (others > 0)
6861 {
a3f17187
AC
6862 if (others == 1)
6863 printf_filtered (_("Note: breakpoint "));
6864 else /* if (others == ???) */
6865 printf_filtered (_("Note: breakpoints "));
c906108c 6866 ALL_BREAKPOINTS (b)
672f9b60 6867 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6868 {
6869 others--;
6870 printf_filtered ("%d", b->number);
6871 if (b->thread == -1 && thread != -1)
6872 printf_filtered (" (all threads)");
6873 else if (b->thread != -1)
6874 printf_filtered (" (thread %d)", b->thread);
6875 printf_filtered ("%s%s ",
059fb39f 6876 ((b->enable_state == bp_disabled
f8eba3c6 6877 || b->enable_state == bp_call_disabled)
0d381245 6878 ? " (disabled)"
0d381245
VP
6879 : ""),
6880 (others > 1) ? ","
6881 : ((others == 1) ? " and" : ""));
6882 }
a3f17187 6883 printf_filtered (_("also set at pc "));
5af949e3 6884 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6885 printf_filtered (".\n");
6886 }
6887}
6888\f
c906108c 6889
e4f237da
KB
6890/* Return true iff it is meaningful to use the address member of
6891 BPT. For some breakpoint types, the address member is irrelevant
6892 and it makes no sense to attempt to compare it to other addresses
6893 (or use it for any other purpose either).
6894
4a64f543
MS
6895 More specifically, each of the following breakpoint types will
6896 always have a zero valued address and we don't want to mark
6897 breakpoints of any of these types to be a duplicate of an actual
6898 breakpoint at address zero:
e4f237da
KB
6899
6900 bp_watchpoint
2d134ed3
PA
6901 bp_catchpoint
6902
6903*/
e4f237da
KB
6904
6905static int
6906breakpoint_address_is_meaningful (struct breakpoint *bpt)
6907{
6908 enum bptype type = bpt->type;
6909
2d134ed3
PA
6910 return (type != bp_watchpoint && type != bp_catchpoint);
6911}
6912
6913/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6914 true if LOC1 and LOC2 represent the same watchpoint location. */
6915
6916static int
4a64f543
MS
6917watchpoint_locations_match (struct bp_location *loc1,
6918 struct bp_location *loc2)
2d134ed3 6919{
3a5c3e22
PA
6920 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6921 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6922
6923 /* Both of them must exist. */
6924 gdb_assert (w1 != NULL);
6925 gdb_assert (w2 != NULL);
2bdf28a0 6926
4a64f543
MS
6927 /* If the target can evaluate the condition expression in hardware,
6928 then we we need to insert both watchpoints even if they are at
6929 the same place. Otherwise the watchpoint will only trigger when
6930 the condition of whichever watchpoint was inserted evaluates to
6931 true, not giving a chance for GDB to check the condition of the
6932 other watchpoint. */
3a5c3e22 6933 if ((w1->cond_exp
4a64f543
MS
6934 && target_can_accel_watchpoint_condition (loc1->address,
6935 loc1->length,
0cf6dd15 6936 loc1->watchpoint_type,
3a5c3e22
PA
6937 w1->cond_exp))
6938 || (w2->cond_exp
4a64f543
MS
6939 && target_can_accel_watchpoint_condition (loc2->address,
6940 loc2->length,
0cf6dd15 6941 loc2->watchpoint_type,
3a5c3e22 6942 w2->cond_exp)))
0cf6dd15
TJB
6943 return 0;
6944
85d721b8
PA
6945 /* Note that this checks the owner's type, not the location's. In
6946 case the target does not support read watchpoints, but does
6947 support access watchpoints, we'll have bp_read_watchpoint
6948 watchpoints with hw_access locations. Those should be considered
6949 duplicates of hw_read locations. The hw_read locations will
6950 become hw_access locations later. */
2d134ed3
PA
6951 return (loc1->owner->type == loc2->owner->type
6952 && loc1->pspace->aspace == loc2->pspace->aspace
6953 && loc1->address == loc2->address
6954 && loc1->length == loc2->length);
e4f237da
KB
6955}
6956
31e77af2 6957/* See breakpoint.h. */
6c95b8df 6958
31e77af2 6959int
6c95b8df
PA
6960breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6961 struct address_space *aspace2, CORE_ADDR addr2)
6962{
f5656ead 6963 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6964 || aspace1 == aspace2)
6965 && addr1 == addr2);
6966}
6967
f1310107
TJB
6968/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6969 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6970 matches ASPACE2. On targets that have global breakpoints, the address
6971 space doesn't really matter. */
6972
6973static int
6974breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6975 int len1, struct address_space *aspace2,
6976 CORE_ADDR addr2)
6977{
f5656ead 6978 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6979 || aspace1 == aspace2)
6980 && addr2 >= addr1 && addr2 < addr1 + len1);
6981}
6982
6983/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6984 a ranged breakpoint. In most targets, a match happens only if ASPACE
6985 matches the breakpoint's address space. On targets that have global
6986 breakpoints, the address space doesn't really matter. */
6987
6988static int
6989breakpoint_location_address_match (struct bp_location *bl,
6990 struct address_space *aspace,
6991 CORE_ADDR addr)
6992{
6993 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6994 aspace, addr)
6995 || (bl->length
6996 && breakpoint_address_match_range (bl->pspace->aspace,
6997 bl->address, bl->length,
6998 aspace, addr)));
6999}
7000
1e4d1764
YQ
7001/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7002 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7003 true, otherwise returns false. */
7004
7005static int
7006tracepoint_locations_match (struct bp_location *loc1,
7007 struct bp_location *loc2)
7008{
7009 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7010 /* Since tracepoint locations are never duplicated with others', tracepoint
7011 locations at the same address of different tracepoints are regarded as
7012 different locations. */
7013 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7014 else
7015 return 0;
7016}
7017
2d134ed3
PA
7018/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7019 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7020 represent the same location. */
7021
7022static int
4a64f543
MS
7023breakpoint_locations_match (struct bp_location *loc1,
7024 struct bp_location *loc2)
2d134ed3 7025{
2bdf28a0
JK
7026 int hw_point1, hw_point2;
7027
7028 /* Both of them must not be in moribund_locations. */
7029 gdb_assert (loc1->owner != NULL);
7030 gdb_assert (loc2->owner != NULL);
7031
7032 hw_point1 = is_hardware_watchpoint (loc1->owner);
7033 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7034
7035 if (hw_point1 != hw_point2)
7036 return 0;
7037 else if (hw_point1)
7038 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7039 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7040 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7041 else
f1310107
TJB
7042 /* We compare bp_location.length in order to cover ranged breakpoints. */
7043 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7044 loc2->pspace->aspace, loc2->address)
7045 && loc1->length == loc2->length);
2d134ed3
PA
7046}
7047
76897487
KB
7048static void
7049breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7050 int bnum, int have_bnum)
7051{
f63fbe86
MS
7052 /* The longest string possibly returned by hex_string_custom
7053 is 50 chars. These must be at least that big for safety. */
7054 char astr1[64];
7055 char astr2[64];
76897487 7056
bb599908
PH
7057 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7058 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7059 if (have_bnum)
8a3fe4f8 7060 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7061 bnum, astr1, astr2);
7062 else
8a3fe4f8 7063 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7064}
7065
4a64f543
MS
7066/* Adjust a breakpoint's address to account for architectural
7067 constraints on breakpoint placement. Return the adjusted address.
7068 Note: Very few targets require this kind of adjustment. For most
7069 targets, this function is simply the identity function. */
76897487
KB
7070
7071static CORE_ADDR
a6d9a66e
UW
7072adjust_breakpoint_address (struct gdbarch *gdbarch,
7073 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7074{
a6d9a66e 7075 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7076 {
7077 /* Very few targets need any kind of breakpoint adjustment. */
7078 return bpaddr;
7079 }
88f7da05
KB
7080 else if (bptype == bp_watchpoint
7081 || bptype == bp_hardware_watchpoint
7082 || bptype == bp_read_watchpoint
7083 || bptype == bp_access_watchpoint
fe798b75 7084 || bptype == bp_catchpoint)
88f7da05
KB
7085 {
7086 /* Watchpoints and the various bp_catch_* eventpoints should not
7087 have their addresses modified. */
7088 return bpaddr;
7089 }
7c16b83e
PA
7090 else if (bptype == bp_single_step)
7091 {
7092 /* Single-step breakpoints should not have their addresses
7093 modified. If there's any architectural constrain that
7094 applies to this address, then it should have already been
7095 taken into account when the breakpoint was created in the
7096 first place. If we didn't do this, stepping through e.g.,
7097 Thumb-2 IT blocks would break. */
7098 return bpaddr;
7099 }
76897487
KB
7100 else
7101 {
7102 CORE_ADDR adjusted_bpaddr;
7103
7104 /* Some targets have architectural constraints on the placement
7105 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7106 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7107
7108 /* An adjusted breakpoint address can significantly alter
7109 a user's expectations. Print a warning if an adjustment
7110 is required. */
7111 if (adjusted_bpaddr != bpaddr)
7112 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7113
7114 return adjusted_bpaddr;
7115 }
7116}
7117
28010a5d
PA
7118void
7119init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7120 struct breakpoint *owner)
7cc221ef 7121{
7cc221ef
DJ
7122 memset (loc, 0, sizeof (*loc));
7123
348d480f
PA
7124 gdb_assert (ops != NULL);
7125
28010a5d
PA
7126 loc->ops = ops;
7127 loc->owner = owner;
511a6cd4 7128 loc->cond = NULL;
b775012e 7129 loc->cond_bytecode = NULL;
0d381245
VP
7130 loc->shlib_disabled = 0;
7131 loc->enabled = 1;
e049a4b5 7132
28010a5d 7133 switch (owner->type)
e049a4b5
DJ
7134 {
7135 case bp_breakpoint:
7c16b83e 7136 case bp_single_step:
e049a4b5
DJ
7137 case bp_until:
7138 case bp_finish:
7139 case bp_longjmp:
7140 case bp_longjmp_resume:
e2e4d78b 7141 case bp_longjmp_call_dummy:
186c406b
TT
7142 case bp_exception:
7143 case bp_exception_resume:
e049a4b5 7144 case bp_step_resume:
2c03e5be 7145 case bp_hp_step_resume:
e049a4b5
DJ
7146 case bp_watchpoint_scope:
7147 case bp_call_dummy:
aa7d318d 7148 case bp_std_terminate:
e049a4b5
DJ
7149 case bp_shlib_event:
7150 case bp_thread_event:
7151 case bp_overlay_event:
4efc6507 7152 case bp_jit_event:
0fd8e87f 7153 case bp_longjmp_master:
aa7d318d 7154 case bp_std_terminate_master:
186c406b 7155 case bp_exception_master:
0e30163f
JK
7156 case bp_gnu_ifunc_resolver:
7157 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7158 case bp_dprintf:
e049a4b5 7159 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7160 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7161 break;
7162 case bp_hardware_breakpoint:
7163 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7164 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7165 break;
7166 case bp_hardware_watchpoint:
7167 case bp_read_watchpoint:
7168 case bp_access_watchpoint:
7169 loc->loc_type = bp_loc_hardware_watchpoint;
7170 break;
7171 case bp_watchpoint:
ce78b96d 7172 case bp_catchpoint:
15c3d785
PA
7173 case bp_tracepoint:
7174 case bp_fast_tracepoint:
0fb4aa4b 7175 case bp_static_tracepoint:
e049a4b5
DJ
7176 loc->loc_type = bp_loc_other;
7177 break;
7178 default:
e2e0b3e5 7179 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7180 }
7181
f431efe5 7182 loc->refc = 1;
28010a5d
PA
7183}
7184
7185/* Allocate a struct bp_location. */
7186
7187static struct bp_location *
7188allocate_bp_location (struct breakpoint *bpt)
7189{
348d480f
PA
7190 return bpt->ops->allocate_location (bpt);
7191}
7cc221ef 7192
f431efe5
PA
7193static void
7194free_bp_location (struct bp_location *loc)
fe3f5fa8 7195{
348d480f 7196 loc->ops->dtor (loc);
fe3f5fa8
VP
7197 xfree (loc);
7198}
7199
f431efe5
PA
7200/* Increment reference count. */
7201
7202static void
7203incref_bp_location (struct bp_location *bl)
7204{
7205 ++bl->refc;
7206}
7207
7208/* Decrement reference count. If the reference count reaches 0,
7209 destroy the bp_location. Sets *BLP to NULL. */
7210
7211static void
7212decref_bp_location (struct bp_location **blp)
7213{
0807b50c
PA
7214 gdb_assert ((*blp)->refc > 0);
7215
f431efe5
PA
7216 if (--(*blp)->refc == 0)
7217 free_bp_location (*blp);
7218 *blp = NULL;
7219}
7220
346774a9 7221/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7222
346774a9
PA
7223static void
7224add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7225{
346774a9 7226 struct breakpoint *b1;
c906108c 7227
346774a9
PA
7228 /* Add this breakpoint to the end of the chain so that a list of
7229 breakpoints will come out in order of increasing numbers. */
7230
7231 b1 = breakpoint_chain;
7232 if (b1 == 0)
7233 breakpoint_chain = b;
7234 else
7235 {
7236 while (b1->next)
7237 b1 = b1->next;
7238 b1->next = b;
7239 }
7240}
7241
7242/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7243
7244static void
7245init_raw_breakpoint_without_location (struct breakpoint *b,
7246 struct gdbarch *gdbarch,
28010a5d 7247 enum bptype bptype,
c0a91b2b 7248 const struct breakpoint_ops *ops)
346774a9 7249{
c906108c 7250 memset (b, 0, sizeof (*b));
2219d63c 7251
348d480f
PA
7252 gdb_assert (ops != NULL);
7253
28010a5d 7254 b->ops = ops;
4d28f7a8 7255 b->type = bptype;
a6d9a66e 7256 b->gdbarch = gdbarch;
c906108c
SS
7257 b->language = current_language->la_language;
7258 b->input_radix = input_radix;
7259 b->thread = -1;
b5de0fa7 7260 b->enable_state = bp_enabled;
c906108c
SS
7261 b->next = 0;
7262 b->silent = 0;
7263 b->ignore_count = 0;
7264 b->commands = NULL;
818dd999 7265 b->frame_id = null_frame_id;
0d381245 7266 b->condition_not_parsed = 0;
84f4c1fe 7267 b->py_bp_object = NULL;
d0fb5eae 7268 b->related_breakpoint = b;
346774a9
PA
7269}
7270
7271/* Helper to set_raw_breakpoint below. Creates a breakpoint
7272 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7273
7274static struct breakpoint *
7275set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7276 enum bptype bptype,
c0a91b2b 7277 const struct breakpoint_ops *ops)
346774a9
PA
7278{
7279 struct breakpoint *b = XNEW (struct breakpoint);
7280
348d480f 7281 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7282 add_to_breakpoint_chain (b);
0d381245
VP
7283 return b;
7284}
7285
0e30163f
JK
7286/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7287 resolutions should be made as the user specified the location explicitly
7288 enough. */
7289
0d381245 7290static void
0e30163f 7291set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7292{
2bdf28a0
JK
7293 gdb_assert (loc->owner != NULL);
7294
0d381245 7295 if (loc->owner->type == bp_breakpoint
1042e4c0 7296 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7297 || is_tracepoint (loc->owner))
0d381245 7298 {
0e30163f 7299 int is_gnu_ifunc;
2c02bd72 7300 const char *function_name;
6a3a010b 7301 CORE_ADDR func_addr;
0e30163f 7302
2c02bd72 7303 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7304 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7305
7306 if (is_gnu_ifunc && !explicit_loc)
7307 {
7308 struct breakpoint *b = loc->owner;
7309
7310 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7311 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7312 &loc->requested_address))
7313 {
7314 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7315 loc->address = adjust_breakpoint_address (loc->gdbarch,
7316 loc->requested_address,
7317 b->type);
7318 }
7319 else if (b->type == bp_breakpoint && b->loc == loc
7320 && loc->next == NULL && b->related_breakpoint == b)
7321 {
7322 /* Create only the whole new breakpoint of this type but do not
7323 mess more complicated breakpoints with multiple locations. */
7324 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7325 /* Remember the resolver's address for use by the return
7326 breakpoint. */
7327 loc->related_address = func_addr;
0e30163f
JK
7328 }
7329 }
7330
2c02bd72
DE
7331 if (function_name)
7332 loc->function_name = xstrdup (function_name);
0d381245
VP
7333 }
7334}
7335
a6d9a66e 7336/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7337struct gdbarch *
a6d9a66e
UW
7338get_sal_arch (struct symtab_and_line sal)
7339{
7340 if (sal.section)
7341 return get_objfile_arch (sal.section->objfile);
7342 if (sal.symtab)
eb822aa6 7343 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7344
7345 return NULL;
7346}
7347
346774a9
PA
7348/* Low level routine for partially initializing a breakpoint of type
7349 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7350 file name, and line number are provided by SAL.
0d381245
VP
7351
7352 It is expected that the caller will complete the initialization of
7353 the newly created breakpoint struct as well as output any status
c56053d2 7354 information regarding the creation of a new breakpoint. */
0d381245 7355
346774a9
PA
7356static void
7357init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7358 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7359 const struct breakpoint_ops *ops)
0d381245 7360{
28010a5d 7361 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7362
3742cc8b 7363 add_location_to_breakpoint (b, &sal);
0d381245 7364
6c95b8df
PA
7365 if (bptype != bp_catchpoint)
7366 gdb_assert (sal.pspace != NULL);
7367
f8eba3c6
TT
7368 /* Store the program space that was used to set the breakpoint,
7369 except for ordinary breakpoints, which are independent of the
7370 program space. */
7371 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7372 b->pspace = sal.pspace;
346774a9 7373}
c906108c 7374
346774a9
PA
7375/* set_raw_breakpoint is a low level routine for allocating and
7376 partially initializing a breakpoint of type BPTYPE. The newly
7377 created breakpoint's address, section, source file name, and line
7378 number are provided by SAL. The newly created and partially
7379 initialized breakpoint is added to the breakpoint chain and
7380 is also returned as the value of this function.
7381
7382 It is expected that the caller will complete the initialization of
7383 the newly created breakpoint struct as well as output any status
7384 information regarding the creation of a new breakpoint. In
7385 particular, set_raw_breakpoint does NOT set the breakpoint
7386 number! Care should be taken to not allow an error to occur
7387 prior to completing the initialization of the breakpoint. If this
7388 should happen, a bogus breakpoint will be left on the chain. */
7389
7390struct breakpoint *
7391set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7392 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7393 const struct breakpoint_ops *ops)
346774a9
PA
7394{
7395 struct breakpoint *b = XNEW (struct breakpoint);
7396
348d480f 7397 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7398 add_to_breakpoint_chain (b);
c906108c
SS
7399 return b;
7400}
7401
c2c6d25f
JM
7402
7403/* Note that the breakpoint object B describes a permanent breakpoint
7404 instruction, hard-wired into the inferior's code. */
7405void
7406make_breakpoint_permanent (struct breakpoint *b)
7407{
0d381245 7408 struct bp_location *bl;
cc59ec59 7409
4a64f543
MS
7410 /* By definition, permanent breakpoints are already present in the
7411 code. Mark all locations as inserted. For now,
7412 make_breakpoint_permanent is called in just one place, so it's
7413 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7414 multiple locations or not, but it's easy to implement. */
0d381245 7415 for (bl = b->loc; bl; bl = bl->next)
1a853c52
PA
7416 {
7417 bl->permanent = 1;
7418 bl->inserted = 1;
7419 }
c2c6d25f
JM
7420}
7421
53a5351d 7422/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7423 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7424 initiated the operation. */
c906108c
SS
7425
7426void
186c406b 7427set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7428{
35df4500 7429 struct breakpoint *b, *b_tmp;
186c406b 7430 int thread = tp->num;
0fd8e87f
UW
7431
7432 /* To avoid having to rescan all objfile symbols at every step,
7433 we maintain a list of continually-inserted but always disabled
7434 longjmp "master" breakpoints. Here, we simply create momentary
7435 clones of those and enable them for the requested thread. */
35df4500 7436 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7437 if (b->pspace == current_program_space
186c406b
TT
7438 && (b->type == bp_longjmp_master
7439 || b->type == bp_exception_master))
0fd8e87f 7440 {
06edf0c0
PA
7441 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7442 struct breakpoint *clone;
cc59ec59 7443
e2e4d78b
JK
7444 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7445 after their removal. */
06edf0c0 7446 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7447 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7448 clone->thread = thread;
7449 }
186c406b
TT
7450
7451 tp->initiating_frame = frame;
c906108c
SS
7452}
7453
611c83ae 7454/* Delete all longjmp breakpoints from THREAD. */
c906108c 7455void
611c83ae 7456delete_longjmp_breakpoint (int thread)
c906108c 7457{
35df4500 7458 struct breakpoint *b, *b_tmp;
c906108c 7459
35df4500 7460 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7461 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7462 {
7463 if (b->thread == thread)
7464 delete_breakpoint (b);
7465 }
c906108c
SS
7466}
7467
f59f708a
PA
7468void
7469delete_longjmp_breakpoint_at_next_stop (int thread)
7470{
7471 struct breakpoint *b, *b_tmp;
7472
7473 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7474 if (b->type == bp_longjmp || b->type == bp_exception)
7475 {
7476 if (b->thread == thread)
7477 b->disposition = disp_del_at_next_stop;
7478 }
7479}
7480
e2e4d78b
JK
7481/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7482 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7483 pointer to any of them. Return NULL if this system cannot place longjmp
7484 breakpoints. */
7485
7486struct breakpoint *
7487set_longjmp_breakpoint_for_call_dummy (void)
7488{
7489 struct breakpoint *b, *retval = NULL;
7490
7491 ALL_BREAKPOINTS (b)
7492 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7493 {
7494 struct breakpoint *new_b;
7495
7496 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7497 &momentary_breakpoint_ops,
7498 1);
e2e4d78b
JK
7499 new_b->thread = pid_to_thread_id (inferior_ptid);
7500
7501 /* Link NEW_B into the chain of RETVAL breakpoints. */
7502
7503 gdb_assert (new_b->related_breakpoint == new_b);
7504 if (retval == NULL)
7505 retval = new_b;
7506 new_b->related_breakpoint = retval;
7507 while (retval->related_breakpoint != new_b->related_breakpoint)
7508 retval = retval->related_breakpoint;
7509 retval->related_breakpoint = new_b;
7510 }
7511
7512 return retval;
7513}
7514
7515/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7516 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7517 stack.
7518
7519 You should call this function only at places where it is safe to currently
7520 unwind the whole stack. Failed stack unwind would discard live dummy
7521 frames. */
7522
7523void
b67a2c6f 7524check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7525{
7526 struct breakpoint *b, *b_tmp;
7527
7528 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7529 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7530 {
7531 struct breakpoint *dummy_b = b->related_breakpoint;
7532
7533 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7534 dummy_b = dummy_b->related_breakpoint;
7535 if (dummy_b->type != bp_call_dummy
7536 || frame_find_by_id (dummy_b->frame_id) != NULL)
7537 continue;
7538
b67a2c6f 7539 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7540
7541 while (b->related_breakpoint != b)
7542 {
7543 if (b_tmp == b->related_breakpoint)
7544 b_tmp = b->related_breakpoint->next;
7545 delete_breakpoint (b->related_breakpoint);
7546 }
7547 delete_breakpoint (b);
7548 }
7549}
7550
1900040c
MS
7551void
7552enable_overlay_breakpoints (void)
7553{
52f0bd74 7554 struct breakpoint *b;
1900040c
MS
7555
7556 ALL_BREAKPOINTS (b)
7557 if (b->type == bp_overlay_event)
7558 {
7559 b->enable_state = bp_enabled;
44702360 7560 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7561 overlay_events_enabled = 1;
1900040c
MS
7562 }
7563}
7564
7565void
7566disable_overlay_breakpoints (void)
7567{
52f0bd74 7568 struct breakpoint *b;
1900040c
MS
7569
7570 ALL_BREAKPOINTS (b)
7571 if (b->type == bp_overlay_event)
7572 {
7573 b->enable_state = bp_disabled;
44702360 7574 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7575 overlay_events_enabled = 0;
1900040c
MS
7576 }
7577}
7578
aa7d318d
TT
7579/* Set an active std::terminate breakpoint for each std::terminate
7580 master breakpoint. */
7581void
7582set_std_terminate_breakpoint (void)
7583{
35df4500 7584 struct breakpoint *b, *b_tmp;
aa7d318d 7585
35df4500 7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7587 if (b->pspace == current_program_space
7588 && b->type == bp_std_terminate_master)
7589 {
06edf0c0 7590 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7591 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7592 }
7593}
7594
7595/* Delete all the std::terminate breakpoints. */
7596void
7597delete_std_terminate_breakpoint (void)
7598{
35df4500 7599 struct breakpoint *b, *b_tmp;
aa7d318d 7600
35df4500 7601 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7602 if (b->type == bp_std_terminate)
7603 delete_breakpoint (b);
7604}
7605
c4093a6a 7606struct breakpoint *
a6d9a66e 7607create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7608{
7609 struct breakpoint *b;
c4093a6a 7610
06edf0c0
PA
7611 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7612 &internal_breakpoint_ops);
7613
b5de0fa7 7614 b->enable_state = bp_enabled;
c4093a6a 7615 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7616 b->addr_string
7617 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7618
44702360 7619 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7620
c4093a6a
JM
7621 return b;
7622}
7623
7624void
7625remove_thread_event_breakpoints (void)
7626{
35df4500 7627 struct breakpoint *b, *b_tmp;
c4093a6a 7628
35df4500 7629 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7630 if (b->type == bp_thread_event
7631 && b->loc->pspace == current_program_space)
c4093a6a
JM
7632 delete_breakpoint (b);
7633}
7634
0101ce28
JJ
7635struct lang_and_radix
7636 {
7637 enum language lang;
7638 int radix;
7639 };
7640
4efc6507
DE
7641/* Create a breakpoint for JIT code registration and unregistration. */
7642
7643struct breakpoint *
7644create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7645{
7646 struct breakpoint *b;
7647
06edf0c0
PA
7648 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7649 &internal_breakpoint_ops);
44702360 7650 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7651 return b;
7652}
0101ce28 7653
03673fc7
PP
7654/* Remove JIT code registration and unregistration breakpoint(s). */
7655
7656void
7657remove_jit_event_breakpoints (void)
7658{
7659 struct breakpoint *b, *b_tmp;
7660
7661 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7662 if (b->type == bp_jit_event
7663 && b->loc->pspace == current_program_space)
7664 delete_breakpoint (b);
7665}
7666
cae688ec
JJ
7667void
7668remove_solib_event_breakpoints (void)
7669{
35df4500 7670 struct breakpoint *b, *b_tmp;
cae688ec 7671
35df4500 7672 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7673 if (b->type == bp_shlib_event
7674 && b->loc->pspace == current_program_space)
cae688ec
JJ
7675 delete_breakpoint (b);
7676}
7677
f37f681c
PA
7678/* See breakpoint.h. */
7679
7680void
7681remove_solib_event_breakpoints_at_next_stop (void)
7682{
7683 struct breakpoint *b, *b_tmp;
7684
7685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7686 if (b->type == bp_shlib_event
7687 && b->loc->pspace == current_program_space)
7688 b->disposition = disp_del_at_next_stop;
7689}
7690
04086b45
PA
7691/* Helper for create_solib_event_breakpoint /
7692 create_and_insert_solib_event_breakpoint. Allows specifying which
7693 INSERT_MODE to pass through to update_global_location_list. */
7694
7695static struct breakpoint *
7696create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7697 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7698{
7699 struct breakpoint *b;
7700
06edf0c0
PA
7701 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7702 &internal_breakpoint_ops);
04086b45 7703 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7704 return b;
7705}
7706
04086b45
PA
7707struct breakpoint *
7708create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7709{
7710 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7711}
7712
f37f681c
PA
7713/* See breakpoint.h. */
7714
7715struct breakpoint *
7716create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7717{
7718 struct breakpoint *b;
7719
04086b45
PA
7720 /* Explicitly tell update_global_location_list to insert
7721 locations. */
7722 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7723 if (!b->loc->inserted)
7724 {
7725 delete_breakpoint (b);
7726 return NULL;
7727 }
7728 return b;
7729}
7730
cae688ec
JJ
7731/* Disable any breakpoints that are on code in shared libraries. Only
7732 apply to enabled breakpoints, disabled ones can just stay disabled. */
7733
7734void
cb851954 7735disable_breakpoints_in_shlibs (void)
cae688ec 7736{
876fa593 7737 struct bp_location *loc, **locp_tmp;
cae688ec 7738
876fa593 7739 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7740 {
2bdf28a0 7741 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7742 struct breakpoint *b = loc->owner;
2bdf28a0 7743
4a64f543
MS
7744 /* We apply the check to all breakpoints, including disabled for
7745 those with loc->duplicate set. This is so that when breakpoint
7746 becomes enabled, or the duplicate is removed, gdb will try to
7747 insert all breakpoints. If we don't set shlib_disabled here,
7748 we'll try to insert those breakpoints and fail. */
1042e4c0 7749 if (((b->type == bp_breakpoint)
508ccb1f 7750 || (b->type == bp_jit_event)
1042e4c0 7751 || (b->type == bp_hardware_breakpoint)
d77f58be 7752 || (is_tracepoint (b)))
6c95b8df 7753 && loc->pspace == current_program_space
0d381245 7754 && !loc->shlib_disabled
6c95b8df 7755 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7756 )
0d381245
VP
7757 {
7758 loc->shlib_disabled = 1;
7759 }
cae688ec
JJ
7760 }
7761}
7762
63644780
NB
7763/* Disable any breakpoints and tracepoints that are in SOLIB upon
7764 notification of unloaded_shlib. Only apply to enabled breakpoints,
7765 disabled ones can just stay disabled. */
84acb35a 7766
75149521 7767static void
84acb35a
JJ
7768disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7769{
876fa593 7770 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7771 int disabled_shlib_breaks = 0;
7772
c86cf029
VP
7773 /* SunOS a.out shared libraries are always mapped, so do not
7774 disable breakpoints; they will only be reported as unloaded
7775 through clear_solib when GDB discards its shared library
7776 list. See clear_solib for more information. */
7777 if (exec_bfd != NULL
7778 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7779 return;
7780
876fa593 7781 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7782 {
2bdf28a0 7783 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7784 struct breakpoint *b = loc->owner;
cc59ec59 7785
1e4d1764 7786 if (solib->pspace == loc->pspace
e2dd7057 7787 && !loc->shlib_disabled
1e4d1764
YQ
7788 && (((b->type == bp_breakpoint
7789 || b->type == bp_jit_event
7790 || b->type == bp_hardware_breakpoint)
7791 && (loc->loc_type == bp_loc_hardware_breakpoint
7792 || loc->loc_type == bp_loc_software_breakpoint))
7793 || is_tracepoint (b))
e2dd7057 7794 && solib_contains_address_p (solib, loc->address))
84acb35a 7795 {
e2dd7057
PP
7796 loc->shlib_disabled = 1;
7797 /* At this point, we cannot rely on remove_breakpoint
7798 succeeding so we must mark the breakpoint as not inserted
7799 to prevent future errors occurring in remove_breakpoints. */
7800 loc->inserted = 0;
8d3788bd
VP
7801
7802 /* This may cause duplicate notifications for the same breakpoint. */
7803 observer_notify_breakpoint_modified (b);
7804
e2dd7057
PP
7805 if (!disabled_shlib_breaks)
7806 {
7807 target_terminal_ours_for_output ();
3e43a32a
MS
7808 warning (_("Temporarily disabling breakpoints "
7809 "for unloaded shared library \"%s\""),
e2dd7057 7810 solib->so_name);
84acb35a 7811 }
e2dd7057 7812 disabled_shlib_breaks = 1;
84acb35a
JJ
7813 }
7814 }
84acb35a
JJ
7815}
7816
63644780
NB
7817/* Disable any breakpoints and tracepoints in OBJFILE upon
7818 notification of free_objfile. Only apply to enabled breakpoints,
7819 disabled ones can just stay disabled. */
7820
7821static void
7822disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7823{
7824 struct breakpoint *b;
7825
7826 if (objfile == NULL)
7827 return;
7828
d03de421
PA
7829 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7830 managed by the user with add-symbol-file/remove-symbol-file.
7831 Similarly to how breakpoints in shared libraries are handled in
7832 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7833 shlib_disabled so they end up uninserted on the next global
7834 location list update. Shared libraries not loaded by the user
7835 aren't handled here -- they're already handled in
7836 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7837 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7838 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7839 main objfile). */
7840 if ((objfile->flags & OBJF_SHARED) == 0
7841 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7842 return;
7843
7844 ALL_BREAKPOINTS (b)
7845 {
7846 struct bp_location *loc;
7847 int bp_modified = 0;
7848
7849 if (!is_breakpoint (b) && !is_tracepoint (b))
7850 continue;
7851
7852 for (loc = b->loc; loc != NULL; loc = loc->next)
7853 {
7854 CORE_ADDR loc_addr = loc->address;
7855
7856 if (loc->loc_type != bp_loc_hardware_breakpoint
7857 && loc->loc_type != bp_loc_software_breakpoint)
7858 continue;
7859
7860 if (loc->shlib_disabled != 0)
7861 continue;
7862
7863 if (objfile->pspace != loc->pspace)
7864 continue;
7865
7866 if (loc->loc_type != bp_loc_hardware_breakpoint
7867 && loc->loc_type != bp_loc_software_breakpoint)
7868 continue;
7869
7870 if (is_addr_in_objfile (loc_addr, objfile))
7871 {
7872 loc->shlib_disabled = 1;
08351840
PA
7873 /* At this point, we don't know whether the object was
7874 unmapped from the inferior or not, so leave the
7875 inserted flag alone. We'll handle failure to
7876 uninsert quietly, in case the object was indeed
7877 unmapped. */
63644780
NB
7878
7879 mark_breakpoint_location_modified (loc);
7880
7881 bp_modified = 1;
7882 }
7883 }
7884
7885 if (bp_modified)
7886 observer_notify_breakpoint_modified (b);
7887 }
7888}
7889
ce78b96d
JB
7890/* FORK & VFORK catchpoints. */
7891
e29a4733
PA
7892/* An instance of this type is used to represent a fork or vfork
7893 catchpoint. It includes a "struct breakpoint" as a kind of base
7894 class; users downcast to "struct breakpoint *" when needed. A
7895 breakpoint is really of this type iff its ops pointer points to
7896 CATCH_FORK_BREAKPOINT_OPS. */
7897
7898struct fork_catchpoint
7899{
7900 /* The base class. */
7901 struct breakpoint base;
7902
7903 /* Process id of a child process whose forking triggered this
7904 catchpoint. This field is only valid immediately after this
7905 catchpoint has triggered. */
7906 ptid_t forked_inferior_pid;
7907};
7908
4a64f543
MS
7909/* Implement the "insert" breakpoint_ops method for fork
7910 catchpoints. */
ce78b96d 7911
77b06cd7
TJB
7912static int
7913insert_catch_fork (struct bp_location *bl)
ce78b96d 7914{
dfd4cc63 7915 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7916}
7917
4a64f543
MS
7918/* Implement the "remove" breakpoint_ops method for fork
7919 catchpoints. */
ce78b96d
JB
7920
7921static int
77b06cd7 7922remove_catch_fork (struct bp_location *bl)
ce78b96d 7923{
dfd4cc63 7924 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7925}
7926
7927/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7928 catchpoints. */
7929
7930static int
f1310107 7931breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7932 struct address_space *aspace, CORE_ADDR bp_addr,
7933 const struct target_waitstatus *ws)
ce78b96d 7934{
e29a4733
PA
7935 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7936
f90263c1
TT
7937 if (ws->kind != TARGET_WAITKIND_FORKED)
7938 return 0;
7939
7940 c->forked_inferior_pid = ws->value.related_pid;
7941 return 1;
ce78b96d
JB
7942}
7943
4a64f543
MS
7944/* Implement the "print_it" breakpoint_ops method for fork
7945 catchpoints. */
ce78b96d
JB
7946
7947static enum print_stop_action
348d480f 7948print_it_catch_fork (bpstat bs)
ce78b96d 7949{
36dfb11c 7950 struct ui_out *uiout = current_uiout;
348d480f
PA
7951 struct breakpoint *b = bs->breakpoint_at;
7952 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7953
ce78b96d 7954 annotate_catchpoint (b->number);
36dfb11c
TT
7955 if (b->disposition == disp_del)
7956 ui_out_text (uiout, "\nTemporary catchpoint ");
7957 else
7958 ui_out_text (uiout, "\nCatchpoint ");
7959 if (ui_out_is_mi_like_p (uiout))
7960 {
7961 ui_out_field_string (uiout, "reason",
7962 async_reason_lookup (EXEC_ASYNC_FORK));
7963 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7964 }
7965 ui_out_field_int (uiout, "bkptno", b->number);
7966 ui_out_text (uiout, " (forked process ");
7967 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7968 ui_out_text (uiout, "), ");
ce78b96d
JB
7969 return PRINT_SRC_AND_LOC;
7970}
7971
4a64f543
MS
7972/* Implement the "print_one" breakpoint_ops method for fork
7973 catchpoints. */
ce78b96d
JB
7974
7975static void
a6d9a66e 7976print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7977{
e29a4733 7978 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7979 struct value_print_options opts;
79a45e25 7980 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7981
7982 get_user_print_options (&opts);
7983
4a64f543
MS
7984 /* Field 4, the address, is omitted (which makes the columns not
7985 line up too nicely with the headers, but the effect is relatively
7986 readable). */
79a45b7d 7987 if (opts.addressprint)
ce78b96d
JB
7988 ui_out_field_skip (uiout, "addr");
7989 annotate_field (5);
7990 ui_out_text (uiout, "fork");
e29a4733 7991 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7992 {
7993 ui_out_text (uiout, ", process ");
7994 ui_out_field_int (uiout, "what",
e29a4733 7995 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7996 ui_out_spaces (uiout, 1);
7997 }
8ac3646f
TT
7998
7999 if (ui_out_is_mi_like_p (uiout))
8000 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8001}
8002
8003/* Implement the "print_mention" breakpoint_ops method for fork
8004 catchpoints. */
8005
8006static void
8007print_mention_catch_fork (struct breakpoint *b)
8008{
8009 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8010}
8011
6149aea9
PA
8012/* Implement the "print_recreate" breakpoint_ops method for fork
8013 catchpoints. */
8014
8015static void
8016print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8017{
8018 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8019 print_recreate_thread (b, fp);
6149aea9
PA
8020}
8021
ce78b96d
JB
8022/* The breakpoint_ops structure to be used in fork catchpoints. */
8023
2060206e 8024static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8025
4a64f543
MS
8026/* Implement the "insert" breakpoint_ops method for vfork
8027 catchpoints. */
ce78b96d 8028
77b06cd7
TJB
8029static int
8030insert_catch_vfork (struct bp_location *bl)
ce78b96d 8031{
dfd4cc63 8032 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8033}
8034
4a64f543
MS
8035/* Implement the "remove" breakpoint_ops method for vfork
8036 catchpoints. */
ce78b96d
JB
8037
8038static int
77b06cd7 8039remove_catch_vfork (struct bp_location *bl)
ce78b96d 8040{
dfd4cc63 8041 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8042}
8043
8044/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8045 catchpoints. */
8046
8047static int
f1310107 8048breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8049 struct address_space *aspace, CORE_ADDR bp_addr,
8050 const struct target_waitstatus *ws)
ce78b96d 8051{
e29a4733
PA
8052 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8053
f90263c1
TT
8054 if (ws->kind != TARGET_WAITKIND_VFORKED)
8055 return 0;
8056
8057 c->forked_inferior_pid = ws->value.related_pid;
8058 return 1;
ce78b96d
JB
8059}
8060
4a64f543
MS
8061/* Implement the "print_it" breakpoint_ops method for vfork
8062 catchpoints. */
ce78b96d
JB
8063
8064static enum print_stop_action
348d480f 8065print_it_catch_vfork (bpstat bs)
ce78b96d 8066{
36dfb11c 8067 struct ui_out *uiout = current_uiout;
348d480f 8068 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8069 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8070
ce78b96d 8071 annotate_catchpoint (b->number);
36dfb11c
TT
8072 if (b->disposition == disp_del)
8073 ui_out_text (uiout, "\nTemporary catchpoint ");
8074 else
8075 ui_out_text (uiout, "\nCatchpoint ");
8076 if (ui_out_is_mi_like_p (uiout))
8077 {
8078 ui_out_field_string (uiout, "reason",
8079 async_reason_lookup (EXEC_ASYNC_VFORK));
8080 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8081 }
8082 ui_out_field_int (uiout, "bkptno", b->number);
8083 ui_out_text (uiout, " (vforked process ");
8084 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8085 ui_out_text (uiout, "), ");
ce78b96d
JB
8086 return PRINT_SRC_AND_LOC;
8087}
8088
4a64f543
MS
8089/* Implement the "print_one" breakpoint_ops method for vfork
8090 catchpoints. */
ce78b96d
JB
8091
8092static void
a6d9a66e 8093print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8094{
e29a4733 8095 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8096 struct value_print_options opts;
79a45e25 8097 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8098
8099 get_user_print_options (&opts);
4a64f543
MS
8100 /* Field 4, the address, is omitted (which makes the columns not
8101 line up too nicely with the headers, but the effect is relatively
8102 readable). */
79a45b7d 8103 if (opts.addressprint)
ce78b96d
JB
8104 ui_out_field_skip (uiout, "addr");
8105 annotate_field (5);
8106 ui_out_text (uiout, "vfork");
e29a4733 8107 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8108 {
8109 ui_out_text (uiout, ", process ");
8110 ui_out_field_int (uiout, "what",
e29a4733 8111 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8112 ui_out_spaces (uiout, 1);
8113 }
8ac3646f
TT
8114
8115 if (ui_out_is_mi_like_p (uiout))
8116 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8117}
8118
8119/* Implement the "print_mention" breakpoint_ops method for vfork
8120 catchpoints. */
8121
8122static void
8123print_mention_catch_vfork (struct breakpoint *b)
8124{
8125 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8126}
8127
6149aea9
PA
8128/* Implement the "print_recreate" breakpoint_ops method for vfork
8129 catchpoints. */
8130
8131static void
8132print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8133{
8134 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8135 print_recreate_thread (b, fp);
6149aea9
PA
8136}
8137
ce78b96d
JB
8138/* The breakpoint_ops structure to be used in vfork catchpoints. */
8139
2060206e 8140static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8141
edcc5120
TT
8142/* An instance of this type is used to represent an solib catchpoint.
8143 It includes a "struct breakpoint" as a kind of base class; users
8144 downcast to "struct breakpoint *" when needed. A breakpoint is
8145 really of this type iff its ops pointer points to
8146 CATCH_SOLIB_BREAKPOINT_OPS. */
8147
8148struct solib_catchpoint
8149{
8150 /* The base class. */
8151 struct breakpoint base;
8152
8153 /* True for "catch load", false for "catch unload". */
8154 unsigned char is_load;
8155
8156 /* Regular expression to match, if any. COMPILED is only valid when
8157 REGEX is non-NULL. */
8158 char *regex;
8159 regex_t compiled;
8160};
8161
8162static void
8163dtor_catch_solib (struct breakpoint *b)
8164{
8165 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8166
8167 if (self->regex)
8168 regfree (&self->compiled);
8169 xfree (self->regex);
8170
8171 base_breakpoint_ops.dtor (b);
8172}
8173
8174static int
8175insert_catch_solib (struct bp_location *ignore)
8176{
8177 return 0;
8178}
8179
8180static int
8181remove_catch_solib (struct bp_location *ignore)
8182{
8183 return 0;
8184}
8185
8186static int
8187breakpoint_hit_catch_solib (const struct bp_location *bl,
8188 struct address_space *aspace,
8189 CORE_ADDR bp_addr,
8190 const struct target_waitstatus *ws)
8191{
8192 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8193 struct breakpoint *other;
8194
8195 if (ws->kind == TARGET_WAITKIND_LOADED)
8196 return 1;
8197
8198 ALL_BREAKPOINTS (other)
8199 {
8200 struct bp_location *other_bl;
8201
8202 if (other == bl->owner)
8203 continue;
8204
8205 if (other->type != bp_shlib_event)
8206 continue;
8207
8208 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8209 continue;
8210
8211 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8212 {
8213 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8214 return 1;
8215 }
8216 }
8217
8218 return 0;
8219}
8220
8221static void
8222check_status_catch_solib (struct bpstats *bs)
8223{
8224 struct solib_catchpoint *self
8225 = (struct solib_catchpoint *) bs->breakpoint_at;
8226 int ix;
8227
8228 if (self->is_load)
8229 {
8230 struct so_list *iter;
8231
8232 for (ix = 0;
8233 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8234 ix, iter);
8235 ++ix)
8236 {
8237 if (!self->regex
8238 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8239 return;
8240 }
8241 }
8242 else
8243 {
8244 char *iter;
8245
8246 for (ix = 0;
8247 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8248 ix, iter);
8249 ++ix)
8250 {
8251 if (!self->regex
8252 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8253 return;
8254 }
8255 }
8256
8257 bs->stop = 0;
8258 bs->print_it = print_it_noop;
8259}
8260
8261static enum print_stop_action
8262print_it_catch_solib (bpstat bs)
8263{
8264 struct breakpoint *b = bs->breakpoint_at;
8265 struct ui_out *uiout = current_uiout;
8266
8267 annotate_catchpoint (b->number);
8268 if (b->disposition == disp_del)
8269 ui_out_text (uiout, "\nTemporary catchpoint ");
8270 else
8271 ui_out_text (uiout, "\nCatchpoint ");
8272 ui_out_field_int (uiout, "bkptno", b->number);
8273 ui_out_text (uiout, "\n");
8274 if (ui_out_is_mi_like_p (uiout))
8275 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8276 print_solib_event (1);
8277 return PRINT_SRC_AND_LOC;
8278}
8279
8280static void
8281print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8282{
8283 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8284 struct value_print_options opts;
8285 struct ui_out *uiout = current_uiout;
8286 char *msg;
8287
8288 get_user_print_options (&opts);
8289 /* Field 4, the address, is omitted (which makes the columns not
8290 line up too nicely with the headers, but the effect is relatively
8291 readable). */
8292 if (opts.addressprint)
8293 {
8294 annotate_field (4);
8295 ui_out_field_skip (uiout, "addr");
8296 }
8297
8298 annotate_field (5);
8299 if (self->is_load)
8300 {
8301 if (self->regex)
8302 msg = xstrprintf (_("load of library matching %s"), self->regex);
8303 else
8304 msg = xstrdup (_("load of library"));
8305 }
8306 else
8307 {
8308 if (self->regex)
8309 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8310 else
8311 msg = xstrdup (_("unload of library"));
8312 }
8313 ui_out_field_string (uiout, "what", msg);
8314 xfree (msg);
8ac3646f
TT
8315
8316 if (ui_out_is_mi_like_p (uiout))
8317 ui_out_field_string (uiout, "catch-type",
8318 self->is_load ? "load" : "unload");
edcc5120
TT
8319}
8320
8321static void
8322print_mention_catch_solib (struct breakpoint *b)
8323{
8324 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8325
8326 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8327 self->is_load ? "load" : "unload");
8328}
8329
8330static void
8331print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8332{
8333 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8334
8335 fprintf_unfiltered (fp, "%s %s",
8336 b->disposition == disp_del ? "tcatch" : "catch",
8337 self->is_load ? "load" : "unload");
8338 if (self->regex)
8339 fprintf_unfiltered (fp, " %s", self->regex);
8340 fprintf_unfiltered (fp, "\n");
8341}
8342
8343static struct breakpoint_ops catch_solib_breakpoint_ops;
8344
91985142
MG
8345/* Shared helper function (MI and CLI) for creating and installing
8346 a shared object event catchpoint. If IS_LOAD is non-zero then
8347 the events to be caught are load events, otherwise they are
8348 unload events. If IS_TEMP is non-zero the catchpoint is a
8349 temporary one. If ENABLED is non-zero the catchpoint is
8350 created in an enabled state. */
edcc5120 8351
91985142
MG
8352void
8353add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8354{
8355 struct solib_catchpoint *c;
8356 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8357 struct cleanup *cleanup;
8358
edcc5120
TT
8359 if (!arg)
8360 arg = "";
8361 arg = skip_spaces (arg);
8362
8363 c = XCNEW (struct solib_catchpoint);
8364 cleanup = make_cleanup (xfree, c);
8365
8366 if (*arg != '\0')
8367 {
8368 int errcode;
8369
8370 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8371 if (errcode != 0)
8372 {
8373 char *err = get_regcomp_error (errcode, &c->compiled);
8374
8375 make_cleanup (xfree, err);
8376 error (_("Invalid regexp (%s): %s"), err, arg);
8377 }
8378 c->regex = xstrdup (arg);
8379 }
8380
8381 c->is_load = is_load;
91985142 8382 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8383 &catch_solib_breakpoint_ops);
8384
91985142
MG
8385 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8386
edcc5120
TT
8387 discard_cleanups (cleanup);
8388 install_breakpoint (0, &c->base, 1);
8389}
8390
91985142
MG
8391/* A helper function that does all the work for "catch load" and
8392 "catch unload". */
8393
8394static void
8395catch_load_or_unload (char *arg, int from_tty, int is_load,
8396 struct cmd_list_element *command)
8397{
8398 int tempflag;
8399 const int enabled = 1;
8400
8401 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8402
8403 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8404}
8405
edcc5120
TT
8406static void
8407catch_load_command_1 (char *arg, int from_tty,
8408 struct cmd_list_element *command)
8409{
8410 catch_load_or_unload (arg, from_tty, 1, command);
8411}
8412
8413static void
8414catch_unload_command_1 (char *arg, int from_tty,
8415 struct cmd_list_element *command)
8416{
8417 catch_load_or_unload (arg, from_tty, 0, command);
8418}
8419
be5c67c1
PA
8420/* An instance of this type is used to represent a syscall catchpoint.
8421 It includes a "struct breakpoint" as a kind of base class; users
8422 downcast to "struct breakpoint *" when needed. A breakpoint is
8423 really of this type iff its ops pointer points to
8424 CATCH_SYSCALL_BREAKPOINT_OPS. */
8425
8426struct syscall_catchpoint
8427{
8428 /* The base class. */
8429 struct breakpoint base;
8430
8431 /* Syscall numbers used for the 'catch syscall' feature. If no
8432 syscall has been specified for filtering, its value is NULL.
8433 Otherwise, it holds a list of all syscalls to be caught. The
8434 list elements are allocated with xmalloc. */
8435 VEC(int) *syscalls_to_be_caught;
8436};
8437
8438/* Implement the "dtor" breakpoint_ops method for syscall
8439 catchpoints. */
8440
8441static void
8442dtor_catch_syscall (struct breakpoint *b)
8443{
8444 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8445
8446 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8447
2060206e 8448 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8449}
8450
fa3064dd
YQ
8451static const struct inferior_data *catch_syscall_inferior_data = NULL;
8452
8453struct catch_syscall_inferior_data
8454{
8455 /* We keep a count of the number of times the user has requested a
8456 particular syscall to be tracked, and pass this information to the
8457 target. This lets capable targets implement filtering directly. */
8458
8459 /* Number of times that "any" syscall is requested. */
8460 int any_syscall_count;
8461
8462 /* Count of each system call. */
8463 VEC(int) *syscalls_counts;
8464
8465 /* This counts all syscall catch requests, so we can readily determine
8466 if any catching is necessary. */
8467 int total_syscalls_count;
8468};
8469
8470static struct catch_syscall_inferior_data*
8471get_catch_syscall_inferior_data (struct inferior *inf)
8472{
8473 struct catch_syscall_inferior_data *inf_data;
8474
8475 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8476 if (inf_data == NULL)
8477 {
41bf6aca 8478 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8479 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8480 }
8481
8482 return inf_data;
8483}
8484
8485static void
8486catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8487{
8488 xfree (arg);
8489}
8490
8491
a96d9b2e
SDJ
8492/* Implement the "insert" breakpoint_ops method for syscall
8493 catchpoints. */
8494
77b06cd7
TJB
8495static int
8496insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8497{
be5c67c1 8498 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8499 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8500 struct catch_syscall_inferior_data *inf_data
8501 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8502
fa3064dd 8503 ++inf_data->total_syscalls_count;
be5c67c1 8504 if (!c->syscalls_to_be_caught)
fa3064dd 8505 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8506 else
8507 {
8508 int i, iter;
cc59ec59 8509
a96d9b2e 8510 for (i = 0;
be5c67c1 8511 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8512 i++)
8513 {
8514 int elem;
cc59ec59 8515
fa3064dd 8516 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8517 {
fa3064dd 8518 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8519 uintptr_t vec_addr_offset
8520 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8521 uintptr_t vec_addr;
fa3064dd
YQ
8522 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8523 vec_addr = ((uintptr_t) VEC_address (int,
8524 inf_data->syscalls_counts)
8525 + vec_addr_offset);
a96d9b2e
SDJ
8526 memset ((void *) vec_addr, 0,
8527 (iter + 1 - old_size) * sizeof (int));
8528 }
fa3064dd
YQ
8529 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8530 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8531 }
8532 }
8533
dfd4cc63 8534 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8535 inf_data->total_syscalls_count != 0,
8536 inf_data->any_syscall_count,
8537 VEC_length (int,
8538 inf_data->syscalls_counts),
8539 VEC_address (int,
8540 inf_data->syscalls_counts));
a96d9b2e
SDJ
8541}
8542
8543/* Implement the "remove" breakpoint_ops method for syscall
8544 catchpoints. */
8545
8546static int
77b06cd7 8547remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8548{
be5c67c1 8549 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8550 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8551 struct catch_syscall_inferior_data *inf_data
8552 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8553
fa3064dd 8554 --inf_data->total_syscalls_count;
be5c67c1 8555 if (!c->syscalls_to_be_caught)
fa3064dd 8556 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8557 else
8558 {
8559 int i, iter;
cc59ec59 8560
a96d9b2e 8561 for (i = 0;
be5c67c1 8562 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8563 i++)
8564 {
8565 int elem;
fa3064dd 8566 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8567 /* Shouldn't happen. */
8568 continue;
fa3064dd
YQ
8569 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8570 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8571 }
8572 }
8573
dfd4cc63 8574 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8575 inf_data->total_syscalls_count != 0,
8576 inf_data->any_syscall_count,
8577 VEC_length (int,
8578 inf_data->syscalls_counts),
3e43a32a 8579 VEC_address (int,
fa3064dd 8580 inf_data->syscalls_counts));
a96d9b2e
SDJ
8581}
8582
8583/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8584 catchpoints. */
8585
8586static int
f1310107 8587breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8588 struct address_space *aspace, CORE_ADDR bp_addr,
8589 const struct target_waitstatus *ws)
a96d9b2e 8590{
4a64f543
MS
8591 /* We must check if we are catching specific syscalls in this
8592 breakpoint. If we are, then we must guarantee that the called
8593 syscall is the same syscall we are catching. */
a96d9b2e 8594 int syscall_number = 0;
be5c67c1
PA
8595 const struct syscall_catchpoint *c
8596 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8597
f90263c1
TT
8598 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8599 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8600 return 0;
8601
f90263c1
TT
8602 syscall_number = ws->value.syscall_number;
8603
a96d9b2e 8604 /* Now, checking if the syscall is the same. */
be5c67c1 8605 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8606 {
8607 int i, iter;
cc59ec59 8608
a96d9b2e 8609 for (i = 0;
be5c67c1 8610 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8611 i++)
8612 if (syscall_number == iter)
4924df79
GKB
8613 return 1;
8614
8615 return 0;
a96d9b2e
SDJ
8616 }
8617
8618 return 1;
8619}
8620
8621/* Implement the "print_it" breakpoint_ops method for syscall
8622 catchpoints. */
8623
8624static enum print_stop_action
348d480f 8625print_it_catch_syscall (bpstat bs)
a96d9b2e 8626{
36dfb11c 8627 struct ui_out *uiout = current_uiout;
348d480f 8628 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8629 /* These are needed because we want to know in which state a
8630 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8631 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8632 must print "called syscall" or "returned from syscall". */
8633 ptid_t ptid;
8634 struct target_waitstatus last;
8635 struct syscall s;
458c8db8 8636 struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
a96d9b2e
SDJ
8637
8638 get_last_target_status (&ptid, &last);
8639
458c8db8 8640 get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
a96d9b2e
SDJ
8641
8642 annotate_catchpoint (b->number);
8643
36dfb11c
TT
8644 if (b->disposition == disp_del)
8645 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8646 else
36dfb11c
TT
8647 ui_out_text (uiout, "\nCatchpoint ");
8648 if (ui_out_is_mi_like_p (uiout))
8649 {
8650 ui_out_field_string (uiout, "reason",
8651 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8652 ? EXEC_ASYNC_SYSCALL_ENTRY
8653 : EXEC_ASYNC_SYSCALL_RETURN));
8654 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8655 }
8656 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8657
8658 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8659 ui_out_text (uiout, " (call to syscall ");
8660 else
8661 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8662
36dfb11c
TT
8663 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8664 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8665 if (s.name != NULL)
8666 ui_out_field_string (uiout, "syscall-name", s.name);
8667
8668 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8669
8670 return PRINT_SRC_AND_LOC;
8671}
8672
8673/* Implement the "print_one" breakpoint_ops method for syscall
8674 catchpoints. */
8675
8676static void
8677print_one_catch_syscall (struct breakpoint *b,
f1310107 8678 struct bp_location **last_loc)
a96d9b2e 8679{
be5c67c1 8680 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8681 struct value_print_options opts;
79a45e25 8682 struct ui_out *uiout = current_uiout;
458c8db8 8683 struct gdbarch *gdbarch = b->loc->gdbarch;
a96d9b2e
SDJ
8684
8685 get_user_print_options (&opts);
4a64f543
MS
8686 /* Field 4, the address, is omitted (which makes the columns not
8687 line up too nicely with the headers, but the effect is relatively
8688 readable). */
a96d9b2e
SDJ
8689 if (opts.addressprint)
8690 ui_out_field_skip (uiout, "addr");
8691 annotate_field (5);
8692
be5c67c1
PA
8693 if (c->syscalls_to_be_caught
8694 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8695 ui_out_text (uiout, "syscalls \"");
8696 else
8697 ui_out_text (uiout, "syscall \"");
8698
be5c67c1 8699 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8700 {
8701 int i, iter;
8702 char *text = xstrprintf ("%s", "");
cc59ec59 8703
a96d9b2e 8704 for (i = 0;
be5c67c1 8705 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8706 i++)
8707 {
8708 char *x = text;
8709 struct syscall s;
458c8db8 8710 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8711
8712 if (s.name != NULL)
8713 text = xstrprintf ("%s%s, ", text, s.name);
8714 else
8715 text = xstrprintf ("%s%d, ", text, iter);
8716
8717 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8718 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8719 on every call. */
8720 xfree (x);
8721 }
8722 /* Remove the last comma. */
8723 text[strlen (text) - 2] = '\0';
8724 ui_out_field_string (uiout, "what", text);
8725 }
8726 else
8727 ui_out_field_string (uiout, "what", "<any syscall>");
8728 ui_out_text (uiout, "\" ");
8ac3646f
TT
8729
8730 if (ui_out_is_mi_like_p (uiout))
8731 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8732}
8733
8734/* Implement the "print_mention" breakpoint_ops method for syscall
8735 catchpoints. */
8736
8737static void
8738print_mention_catch_syscall (struct breakpoint *b)
8739{
be5c67c1 8740 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8741 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1
PA
8742
8743 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8744 {
8745 int i, iter;
8746
be5c67c1 8747 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8748 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8749 else
8750 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8751
8752 for (i = 0;
be5c67c1 8753 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8754 i++)
8755 {
8756 struct syscall s;
458c8db8 8757 get_syscall_by_number (gdbarch, iter, &s);
a96d9b2e
SDJ
8758
8759 if (s.name)
8760 printf_filtered (" '%s' [%d]", s.name, s.number);
8761 else
8762 printf_filtered (" %d", s.number);
8763 }
8764 printf_filtered (")");
8765 }
8766 else
8767 printf_filtered (_("Catchpoint %d (any syscall)"),
8768 b->number);
8769}
8770
6149aea9
PA
8771/* Implement the "print_recreate" breakpoint_ops method for syscall
8772 catchpoints. */
8773
8774static void
8775print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8776{
be5c67c1 8777 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
458c8db8 8778 struct gdbarch *gdbarch = b->loc->gdbarch;
be5c67c1 8779
6149aea9
PA
8780 fprintf_unfiltered (fp, "catch syscall");
8781
be5c67c1 8782 if (c->syscalls_to_be_caught)
6149aea9
PA
8783 {
8784 int i, iter;
8785
8786 for (i = 0;
be5c67c1 8787 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8788 i++)
8789 {
8790 struct syscall s;
8791
458c8db8 8792 get_syscall_by_number (gdbarch, iter, &s);
6149aea9
PA
8793 if (s.name)
8794 fprintf_unfiltered (fp, " %s", s.name);
8795 else
8796 fprintf_unfiltered (fp, " %d", s.number);
8797 }
8798 }
d9b3f62e 8799 print_recreate_thread (b, fp);
6149aea9
PA
8800}
8801
a96d9b2e
SDJ
8802/* The breakpoint_ops structure to be used in syscall catchpoints. */
8803
2060206e 8804static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8805
8806/* Returns non-zero if 'b' is a syscall catchpoint. */
8807
8808static int
8809syscall_catchpoint_p (struct breakpoint *b)
8810{
8811 return (b->ops == &catch_syscall_breakpoint_ops);
8812}
8813
346774a9
PA
8814/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8815 is non-zero, then make the breakpoint temporary. If COND_STRING is
8816 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8817 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8818
ab04a2af 8819void
346774a9
PA
8820init_catchpoint (struct breakpoint *b,
8821 struct gdbarch *gdbarch, int tempflag,
8822 char *cond_string,
c0a91b2b 8823 const struct breakpoint_ops *ops)
c906108c 8824{
c5aa993b 8825 struct symtab_and_line sal;
346774a9 8826
fe39c653 8827 init_sal (&sal);
6c95b8df 8828 sal.pspace = current_program_space;
c5aa993b 8829
28010a5d 8830 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8831
1b36a34b 8832 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8833 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8834}
8835
28010a5d 8836void
3ea46bff 8837install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8838{
8839 add_to_breakpoint_chain (b);
3a5c3e22 8840 set_breakpoint_number (internal, b);
558a9d82
YQ
8841 if (is_tracepoint (b))
8842 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8843 if (!internal)
8844 mention (b);
c56053d2 8845 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8846
8847 if (update_gll)
44702360 8848 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8849}
8850
9b70b993 8851static void
a6d9a66e
UW
8852create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8853 int tempflag, char *cond_string,
c0a91b2b 8854 const struct breakpoint_ops *ops)
c906108c 8855{
e29a4733 8856 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8857
e29a4733
PA
8858 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8859
8860 c->forked_inferior_pid = null_ptid;
8861
3ea46bff 8862 install_breakpoint (0, &c->base, 1);
c906108c
SS
8863}
8864
fe798b75
JB
8865/* Exec catchpoints. */
8866
b4d90040
PA
8867/* An instance of this type is used to represent an exec catchpoint.
8868 It includes a "struct breakpoint" as a kind of base class; users
8869 downcast to "struct breakpoint *" when needed. A breakpoint is
8870 really of this type iff its ops pointer points to
8871 CATCH_EXEC_BREAKPOINT_OPS. */
8872
8873struct exec_catchpoint
8874{
8875 /* The base class. */
8876 struct breakpoint base;
8877
8878 /* Filename of a program whose exec triggered this catchpoint.
8879 This field is only valid immediately after this catchpoint has
8880 triggered. */
8881 char *exec_pathname;
8882};
8883
8884/* Implement the "dtor" breakpoint_ops method for exec
8885 catchpoints. */
8886
8887static void
8888dtor_catch_exec (struct breakpoint *b)
8889{
8890 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8891
8892 xfree (c->exec_pathname);
348d480f 8893
2060206e 8894 base_breakpoint_ops.dtor (b);
b4d90040
PA
8895}
8896
77b06cd7
TJB
8897static int
8898insert_catch_exec (struct bp_location *bl)
c906108c 8899{
dfd4cc63 8900 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8901}
c906108c 8902
fe798b75 8903static int
77b06cd7 8904remove_catch_exec (struct bp_location *bl)
fe798b75 8905{
dfd4cc63 8906 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8907}
c906108c 8908
fe798b75 8909static int
f1310107 8910breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8911 struct address_space *aspace, CORE_ADDR bp_addr,
8912 const struct target_waitstatus *ws)
fe798b75 8913{
b4d90040
PA
8914 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8915
f90263c1
TT
8916 if (ws->kind != TARGET_WAITKIND_EXECD)
8917 return 0;
8918
8919 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8920 return 1;
fe798b75 8921}
c906108c 8922
fe798b75 8923static enum print_stop_action
348d480f 8924print_it_catch_exec (bpstat bs)
fe798b75 8925{
36dfb11c 8926 struct ui_out *uiout = current_uiout;
348d480f 8927 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8928 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8929
fe798b75 8930 annotate_catchpoint (b->number);
36dfb11c
TT
8931 if (b->disposition == disp_del)
8932 ui_out_text (uiout, "\nTemporary catchpoint ");
8933 else
8934 ui_out_text (uiout, "\nCatchpoint ");
8935 if (ui_out_is_mi_like_p (uiout))
8936 {
8937 ui_out_field_string (uiout, "reason",
8938 async_reason_lookup (EXEC_ASYNC_EXEC));
8939 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8940 }
8941 ui_out_field_int (uiout, "bkptno", b->number);
8942 ui_out_text (uiout, " (exec'd ");
8943 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8944 ui_out_text (uiout, "), ");
8945
fe798b75 8946 return PRINT_SRC_AND_LOC;
c906108c
SS
8947}
8948
fe798b75 8949static void
a6d9a66e 8950print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8951{
b4d90040 8952 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8953 struct value_print_options opts;
79a45e25 8954 struct ui_out *uiout = current_uiout;
fe798b75
JB
8955
8956 get_user_print_options (&opts);
8957
8958 /* Field 4, the address, is omitted (which makes the columns
8959 not line up too nicely with the headers, but the effect
8960 is relatively readable). */
8961 if (opts.addressprint)
8962 ui_out_field_skip (uiout, "addr");
8963 annotate_field (5);
8964 ui_out_text (uiout, "exec");
b4d90040 8965 if (c->exec_pathname != NULL)
fe798b75
JB
8966 {
8967 ui_out_text (uiout, ", program \"");
b4d90040 8968 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8969 ui_out_text (uiout, "\" ");
8970 }
8ac3646f
TT
8971
8972 if (ui_out_is_mi_like_p (uiout))
8973 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8974}
8975
8976static void
8977print_mention_catch_exec (struct breakpoint *b)
8978{
8979 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8980}
8981
6149aea9
PA
8982/* Implement the "print_recreate" breakpoint_ops method for exec
8983 catchpoints. */
8984
8985static void
8986print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8987{
8988 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8989 print_recreate_thread (b, fp);
6149aea9
PA
8990}
8991
2060206e 8992static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8993
a96d9b2e
SDJ
8994static void
8995create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8996 const struct breakpoint_ops *ops)
a96d9b2e 8997{
be5c67c1 8998 struct syscall_catchpoint *c;
a96d9b2e 8999 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9000
be5c67c1
PA
9001 c = XNEW (struct syscall_catchpoint);
9002 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9003 c->syscalls_to_be_caught = filter;
a96d9b2e 9004
3ea46bff 9005 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9006}
9007
c906108c 9008static int
fba45db2 9009hw_breakpoint_used_count (void)
c906108c 9010{
c906108c 9011 int i = 0;
f1310107
TJB
9012 struct breakpoint *b;
9013 struct bp_location *bl;
c906108c
SS
9014
9015 ALL_BREAKPOINTS (b)
c5aa993b 9016 {
d6b74ac4 9017 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9018 for (bl = b->loc; bl; bl = bl->next)
9019 {
9020 /* Special types of hardware breakpoints may use more than
9021 one register. */
348d480f 9022 i += b->ops->resources_needed (bl);
f1310107 9023 }
c5aa993b 9024 }
c906108c
SS
9025
9026 return i;
9027}
9028
a1398e0c
PA
9029/* Returns the resources B would use if it were a hardware
9030 watchpoint. */
9031
c906108c 9032static int
a1398e0c 9033hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9034{
c906108c 9035 int i = 0;
e09342b5 9036 struct bp_location *bl;
c906108c 9037
a1398e0c
PA
9038 if (!breakpoint_enabled (b))
9039 return 0;
9040
9041 for (bl = b->loc; bl; bl = bl->next)
9042 {
9043 /* Special types of hardware watchpoints may use more than
9044 one register. */
9045 i += b->ops->resources_needed (bl);
9046 }
9047
9048 return i;
9049}
9050
9051/* Returns the sum the used resources of all hardware watchpoints of
9052 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9053 the sum of the used resources of all hardware watchpoints of other
9054 types _not_ TYPE. */
9055
9056static int
9057hw_watchpoint_used_count_others (struct breakpoint *except,
9058 enum bptype type, int *other_type_used)
9059{
9060 int i = 0;
9061 struct breakpoint *b;
9062
c906108c
SS
9063 *other_type_used = 0;
9064 ALL_BREAKPOINTS (b)
e09342b5 9065 {
a1398e0c
PA
9066 if (b == except)
9067 continue;
e09342b5
TJB
9068 if (!breakpoint_enabled (b))
9069 continue;
9070
a1398e0c
PA
9071 if (b->type == type)
9072 i += hw_watchpoint_use_count (b);
9073 else if (is_hardware_watchpoint (b))
9074 *other_type_used = 1;
e09342b5
TJB
9075 }
9076
c906108c
SS
9077 return i;
9078}
9079
c906108c 9080void
fba45db2 9081disable_watchpoints_before_interactive_call_start (void)
c906108c 9082{
c5aa993b 9083 struct breakpoint *b;
c906108c
SS
9084
9085 ALL_BREAKPOINTS (b)
c5aa993b 9086 {
cc60f2e3 9087 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9088 {
b5de0fa7 9089 b->enable_state = bp_call_disabled;
44702360 9090 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9091 }
9092 }
c906108c
SS
9093}
9094
9095void
fba45db2 9096enable_watchpoints_after_interactive_call_stop (void)
c906108c 9097{
c5aa993b 9098 struct breakpoint *b;
c906108c
SS
9099
9100 ALL_BREAKPOINTS (b)
c5aa993b 9101 {
cc60f2e3 9102 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9103 {
b5de0fa7 9104 b->enable_state = bp_enabled;
44702360 9105 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9106 }
9107 }
c906108c
SS
9108}
9109
8bea4e01
UW
9110void
9111disable_breakpoints_before_startup (void)
9112{
6c95b8df 9113 current_program_space->executing_startup = 1;
44702360 9114 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9115}
9116
9117void
9118enable_breakpoints_after_startup (void)
9119{
6c95b8df 9120 current_program_space->executing_startup = 0;
f8eba3c6 9121 breakpoint_re_set ();
8bea4e01
UW
9122}
9123
7c16b83e
PA
9124/* Create a new single-step breakpoint for thread THREAD, with no
9125 locations. */
c906108c 9126
7c16b83e
PA
9127static struct breakpoint *
9128new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9129{
9130 struct breakpoint *b = XNEW (struct breakpoint);
9131
9132 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9133 &momentary_breakpoint_ops);
9134
9135 b->disposition = disp_donttouch;
9136 b->frame_id = null_frame_id;
9137
9138 b->thread = thread;
9139 gdb_assert (b->thread != 0);
9140
9141 add_to_breakpoint_chain (b);
9142
9143 return b;
9144}
9145
9146/* Set a momentary breakpoint of type TYPE at address specified by
9147 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9148 frame. */
c906108c
SS
9149
9150struct breakpoint *
a6d9a66e
UW
9151set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9152 struct frame_id frame_id, enum bptype type)
c906108c 9153{
52f0bd74 9154 struct breakpoint *b;
edb3359d 9155
193facb3
JK
9156 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9157 tail-called one. */
9158 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9159
06edf0c0 9160 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9161 b->enable_state = bp_enabled;
9162 b->disposition = disp_donttouch;
818dd999 9163 b->frame_id = frame_id;
c906108c 9164
4a64f543
MS
9165 /* If we're debugging a multi-threaded program, then we want
9166 momentary breakpoints to be active in only a single thread of
9167 control. */
39f77062
KB
9168 if (in_thread_list (inferior_ptid))
9169 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9170
44702360 9171 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9172
c906108c
SS
9173 return b;
9174}
611c83ae 9175
06edf0c0 9176/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9177 The new breakpoint will have type TYPE, use OPS as its
9178 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9179
06edf0c0
PA
9180static struct breakpoint *
9181momentary_breakpoint_from_master (struct breakpoint *orig,
9182 enum bptype type,
a1aa2221
LM
9183 const struct breakpoint_ops *ops,
9184 int loc_enabled)
e58b0e63
PA
9185{
9186 struct breakpoint *copy;
9187
06edf0c0 9188 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9189 copy->loc = allocate_bp_location (copy);
0e30163f 9190 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9191
a6d9a66e 9192 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9193 copy->loc->requested_address = orig->loc->requested_address;
9194 copy->loc->address = orig->loc->address;
9195 copy->loc->section = orig->loc->section;
6c95b8df 9196 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9197 copy->loc->probe = orig->loc->probe;
f8eba3c6 9198 copy->loc->line_number = orig->loc->line_number;
2f202fde 9199 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9200 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9201 copy->frame_id = orig->frame_id;
9202 copy->thread = orig->thread;
6c95b8df 9203 copy->pspace = orig->pspace;
e58b0e63
PA
9204
9205 copy->enable_state = bp_enabled;
9206 copy->disposition = disp_donttouch;
9207 copy->number = internal_breakpoint_number--;
9208
44702360 9209 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9210 return copy;
9211}
9212
06edf0c0
PA
9213/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9214 ORIG is NULL. */
9215
9216struct breakpoint *
9217clone_momentary_breakpoint (struct breakpoint *orig)
9218{
9219 /* If there's nothing to clone, then return nothing. */
9220 if (orig == NULL)
9221 return NULL;
9222
a1aa2221 9223 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9224}
9225
611c83ae 9226struct breakpoint *
a6d9a66e
UW
9227set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9228 enum bptype type)
611c83ae
PA
9229{
9230 struct symtab_and_line sal;
9231
9232 sal = find_pc_line (pc, 0);
9233 sal.pc = pc;
9234 sal.section = find_pc_overlay (pc);
9235 sal.explicit_pc = 1;
9236
a6d9a66e 9237 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9238}
c906108c 9239\f
c5aa993b 9240
c906108c
SS
9241/* Tell the user we have just set a breakpoint B. */
9242
9243static void
fba45db2 9244mention (struct breakpoint *b)
c906108c 9245{
348d480f 9246 b->ops->print_mention (b);
79a45e25 9247 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9248 return;
c906108c
SS
9249 printf_filtered ("\n");
9250}
c906108c 9251\f
c5aa993b 9252
1a853c52
PA
9253static int bp_loc_is_permanent (struct bp_location *loc);
9254
0d381245 9255static struct bp_location *
39d61571 9256add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9257 const struct symtab_and_line *sal)
9258{
9259 struct bp_location *loc, **tmp;
3742cc8b
YQ
9260 CORE_ADDR adjusted_address;
9261 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9262
9263 if (loc_gdbarch == NULL)
9264 loc_gdbarch = b->gdbarch;
9265
9266 /* Adjust the breakpoint's address prior to allocating a location.
9267 Once we call allocate_bp_location(), that mostly uninitialized
9268 location will be placed on the location chain. Adjustment of the
9269 breakpoint may cause target_read_memory() to be called and we do
9270 not want its scan of the location chain to find a breakpoint and
9271 location that's only been partially initialized. */
9272 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9273 sal->pc, b->type);
0d381245 9274
d30113d4 9275 /* Sort the locations by their ADDRESS. */
39d61571 9276 loc = allocate_bp_location (b);
d30113d4
JK
9277 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9278 tmp = &((*tmp)->next))
0d381245 9279 ;
d30113d4 9280 loc->next = *tmp;
0d381245 9281 *tmp = loc;
3742cc8b 9282
0d381245 9283 loc->requested_address = sal->pc;
3742cc8b 9284 loc->address = adjusted_address;
6c95b8df 9285 loc->pspace = sal->pspace;
729662a5
TT
9286 loc->probe.probe = sal->probe;
9287 loc->probe.objfile = sal->objfile;
6c95b8df 9288 gdb_assert (loc->pspace != NULL);
0d381245 9289 loc->section = sal->section;
3742cc8b 9290 loc->gdbarch = loc_gdbarch;
f8eba3c6 9291 loc->line_number = sal->line;
2f202fde 9292 loc->symtab = sal->symtab;
f8eba3c6 9293
0e30163f
JK
9294 set_breakpoint_location_function (loc,
9295 sal->explicit_pc || sal->explicit_line);
1a853c52
PA
9296
9297 if (bp_loc_is_permanent (loc))
9298 {
9299 loc->inserted = 1;
9300 loc->permanent = 1;
9301 }
9302
0d381245
VP
9303 return loc;
9304}
514f746b
AR
9305\f
9306
9307/* Return 1 if LOC is pointing to a permanent breakpoint,
9308 return 0 otherwise. */
9309
9310static int
9311bp_loc_is_permanent (struct bp_location *loc)
9312{
9313 int len;
9314 CORE_ADDR addr;
1afeeb75 9315 const gdb_byte *bpoint;
514f746b 9316 gdb_byte *target_mem;
939c61fa
JK
9317 struct cleanup *cleanup;
9318 int retval = 0;
514f746b
AR
9319
9320 gdb_assert (loc != NULL);
9321
e8af5d7a
JB
9322 /* bp_call_dummy breakpoint locations are usually memory locations
9323 where GDB just wrote a breakpoint instruction, making it look
9324 as if there is a permanent breakpoint at that location. Considering
9325 it permanent makes GDB rely on that breakpoint instruction to stop
9326 the program, thus removing the need to insert its own breakpoint
9327 there. This is normally expected to work, except that some versions
9328 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
9329 0x02 while interrupts disabled, Error state) instead of reporting
9330 a SIGTRAP. QEMU should probably be fixed, but in the interest of
9331 compatibility with versions that behave this way, we always consider
9332 bp_call_dummy breakpoint locations as non-permanent. */
9333 if (loc->owner->type == bp_call_dummy)
9334 return 0;
9335
514f746b 9336 addr = loc->address;
1afeeb75 9337 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9338
939c61fa 9339 /* Software breakpoints unsupported? */
1afeeb75 9340 if (bpoint == NULL)
939c61fa
JK
9341 return 0;
9342
514f746b
AR
9343 target_mem = alloca (len);
9344
939c61fa
JK
9345 /* Enable the automatic memory restoration from breakpoints while
9346 we read the memory. Otherwise we could say about our temporary
9347 breakpoints they are permanent. */
6c95b8df
PA
9348 cleanup = save_current_space_and_thread ();
9349
9350 switch_to_program_space_and_thread (loc->pspace);
9351 make_show_memory_breakpoints_cleanup (0);
939c61fa 9352
514f746b 9353 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9354 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9355 retval = 1;
514f746b 9356
939c61fa
JK
9357 do_cleanups (cleanup);
9358
9359 return retval;
514f746b
AR
9360}
9361
e7e0cddf
SS
9362/* Build a command list for the dprintf corresponding to the current
9363 settings of the dprintf style options. */
9364
9365static void
9366update_dprintf_command_list (struct breakpoint *b)
9367{
9368 char *dprintf_args = b->extra_string;
9369 char *printf_line = NULL;
9370
9371 if (!dprintf_args)
9372 return;
9373
9374 dprintf_args = skip_spaces (dprintf_args);
9375
9376 /* Allow a comma, as it may have terminated a location, but don't
9377 insist on it. */
9378 if (*dprintf_args == ',')
9379 ++dprintf_args;
9380 dprintf_args = skip_spaces (dprintf_args);
9381
9382 if (*dprintf_args != '"')
9383 error (_("Bad format string, missing '\"'."));
9384
d3ce09f5 9385 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9386 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9387 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9388 {
9389 if (!dprintf_function)
9390 error (_("No function supplied for dprintf call"));
9391
9392 if (dprintf_channel && strlen (dprintf_channel) > 0)
9393 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9394 dprintf_function,
9395 dprintf_channel,
9396 dprintf_args);
9397 else
9398 printf_line = xstrprintf ("call (void) %s (%s)",
9399 dprintf_function,
9400 dprintf_args);
9401 }
d3ce09f5
SS
9402 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9403 {
9404 if (target_can_run_breakpoint_commands ())
9405 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9406 else
9407 {
9408 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9409 printf_line = xstrprintf ("printf %s", dprintf_args);
9410 }
9411 }
e7e0cddf
SS
9412 else
9413 internal_error (__FILE__, __LINE__,
9414 _("Invalid dprintf style."));
9415
f28045c2 9416 gdb_assert (printf_line != NULL);
9d6e6e84 9417 /* Manufacture a printf sequence. */
f28045c2 9418 {
9d6e6e84
HZ
9419 struct command_line *printf_cmd_line
9420 = xmalloc (sizeof (struct command_line));
e7e0cddf 9421
f28045c2
YQ
9422 printf_cmd_line = xmalloc (sizeof (struct command_line));
9423 printf_cmd_line->control_type = simple_control;
9424 printf_cmd_line->body_count = 0;
9425 printf_cmd_line->body_list = NULL;
9d6e6e84 9426 printf_cmd_line->next = NULL;
f28045c2 9427 printf_cmd_line->line = printf_line;
e7e0cddf 9428
f28045c2
YQ
9429 breakpoint_set_commands (b, printf_cmd_line);
9430 }
e7e0cddf
SS
9431}
9432
9433/* Update all dprintf commands, making their command lists reflect
9434 current style settings. */
9435
9436static void
9437update_dprintf_commands (char *args, int from_tty,
9438 struct cmd_list_element *c)
9439{
9440 struct breakpoint *b;
9441
9442 ALL_BREAKPOINTS (b)
9443 {
9444 if (b->type == bp_dprintf)
9445 update_dprintf_command_list (b);
9446 }
9447}
c3f6f71d 9448
018d34a4
VP
9449/* Create a breakpoint with SAL as location. Use ADDR_STRING
9450 as textual description of the location, and COND_STRING
db107f19 9451 as condition expression. */
018d34a4
VP
9452
9453static void
d9b3f62e
PA
9454init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9455 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9456 char *filter, char *cond_string,
e7e0cddf 9457 char *extra_string,
d9b3f62e
PA
9458 enum bptype type, enum bpdisp disposition,
9459 int thread, int task, int ignore_count,
c0a91b2b 9460 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9461 int enabled, int internal, unsigned flags,
9462 int display_canonical)
018d34a4 9463{
0d381245 9464 int i;
018d34a4
VP
9465
9466 if (type == bp_hardware_breakpoint)
9467 {
fbbd034e
AS
9468 int target_resources_ok;
9469
9470 i = hw_breakpoint_used_count ();
9471 target_resources_ok =
9472 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9473 i + 1, 0);
9474 if (target_resources_ok == 0)
9475 error (_("No hardware breakpoint support in the target."));
9476 else if (target_resources_ok < 0)
9477 error (_("Hardware breakpoints used exceeds limit."));
9478 }
9479
6c95b8df
PA
9480 gdb_assert (sals.nelts > 0);
9481
0d381245
VP
9482 for (i = 0; i < sals.nelts; ++i)
9483 {
9484 struct symtab_and_line sal = sals.sals[i];
9485 struct bp_location *loc;
9486
9487 if (from_tty)
5af949e3
UW
9488 {
9489 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9490 if (!loc_gdbarch)
9491 loc_gdbarch = gdbarch;
9492
9493 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9494 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9495 }
0d381245
VP
9496
9497 if (i == 0)
9498 {
d9b3f62e 9499 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9500 b->thread = thread;
4a306c9a 9501 b->task = task;
855a6e68 9502
0d381245 9503 b->cond_string = cond_string;
e7e0cddf 9504 b->extra_string = extra_string;
0d381245 9505 b->ignore_count = ignore_count;
41447f92 9506 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9507 b->disposition = disposition;
6c95b8df 9508
44f238bb
PA
9509 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9510 b->loc->inserted = 1;
9511
0fb4aa4b
PA
9512 if (type == bp_static_tracepoint)
9513 {
d9b3f62e 9514 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9515 struct static_tracepoint_marker marker;
9516
983af33b 9517 if (strace_marker_p (b))
0fb4aa4b
PA
9518 {
9519 /* We already know the marker exists, otherwise, we
9520 wouldn't see a sal for it. */
9521 char *p = &addr_string[3];
9522 char *endp;
9523 char *marker_str;
0fb4aa4b 9524
e9cafbcc 9525 p = skip_spaces (p);
0fb4aa4b 9526
e9cafbcc 9527 endp = skip_to_space (p);
0fb4aa4b
PA
9528
9529 marker_str = savestring (p, endp - p);
d9b3f62e 9530 t->static_trace_marker_id = marker_str;
0fb4aa4b 9531
3e43a32a
MS
9532 printf_filtered (_("Probed static tracepoint "
9533 "marker \"%s\"\n"),
d9b3f62e 9534 t->static_trace_marker_id);
0fb4aa4b
PA
9535 }
9536 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9537 {
d9b3f62e 9538 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9539 release_static_tracepoint_marker (&marker);
9540
3e43a32a
MS
9541 printf_filtered (_("Probed static tracepoint "
9542 "marker \"%s\"\n"),
d9b3f62e 9543 t->static_trace_marker_id);
0fb4aa4b
PA
9544 }
9545 else
3e43a32a
MS
9546 warning (_("Couldn't determine the static "
9547 "tracepoint marker to probe"));
0fb4aa4b
PA
9548 }
9549
0d381245
VP
9550 loc = b->loc;
9551 }
9552 else
018d34a4 9553 {
39d61571 9554 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9555 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9556 loc->inserted = 1;
0d381245
VP
9557 }
9558
9559 if (b->cond_string)
9560 {
bbc13ae3
KS
9561 const char *arg = b->cond_string;
9562
1bb9788d
TT
9563 loc->cond = parse_exp_1 (&arg, loc->address,
9564 block_for_pc (loc->address), 0);
0d381245 9565 if (*arg)
588ae58c 9566 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9567 }
e7e0cddf
SS
9568
9569 /* Dynamic printf requires and uses additional arguments on the
9570 command line, otherwise it's an error. */
9571 if (type == bp_dprintf)
9572 {
9573 if (b->extra_string)
9574 update_dprintf_command_list (b);
9575 else
9576 error (_("Format string required"));
9577 }
9578 else if (b->extra_string)
588ae58c 9579 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9580 }
018d34a4 9581
56435ebe 9582 b->display_canonical = display_canonical;
018d34a4
VP
9583 if (addr_string)
9584 b->addr_string = addr_string;
9585 else
9586 /* addr_string has to be used or breakpoint_re_set will delete
9587 me. */
5af949e3
UW
9588 b->addr_string
9589 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9590 b->filter = filter;
d9b3f62e 9591}
018d34a4 9592
d9b3f62e
PA
9593static void
9594create_breakpoint_sal (struct gdbarch *gdbarch,
9595 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9596 char *filter, char *cond_string,
e7e0cddf 9597 char *extra_string,
d9b3f62e
PA
9598 enum bptype type, enum bpdisp disposition,
9599 int thread, int task, int ignore_count,
c0a91b2b 9600 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9601 int enabled, int internal, unsigned flags,
9602 int display_canonical)
d9b3f62e
PA
9603{
9604 struct breakpoint *b;
9605 struct cleanup *old_chain;
9606
9607 if (is_tracepoint_type (type))
9608 {
9609 struct tracepoint *t;
9610
9611 t = XCNEW (struct tracepoint);
9612 b = &t->base;
9613 }
9614 else
9615 b = XNEW (struct breakpoint);
9616
9617 old_chain = make_cleanup (xfree, b);
9618
9619 init_breakpoint_sal (b, gdbarch,
9620 sals, addr_string,
e7e0cddf 9621 filter, cond_string, extra_string,
d9b3f62e
PA
9622 type, disposition,
9623 thread, task, ignore_count,
9624 ops, from_tty,
44f238bb
PA
9625 enabled, internal, flags,
9626 display_canonical);
d9b3f62e
PA
9627 discard_cleanups (old_chain);
9628
3ea46bff 9629 install_breakpoint (internal, b, 0);
018d34a4
VP
9630}
9631
9632/* Add SALS.nelts breakpoints to the breakpoint table. For each
9633 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9634 value. COND_STRING, if not NULL, specified the condition to be
9635 used for all breakpoints. Essentially the only case where
9636 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9637 function. In that case, it's still not possible to specify
9638 separate conditions for different overloaded functions, so
9639 we take just a single condition string.
9640
c3f6f71d 9641 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9642 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9643 array contents). If the function fails (error() is called), the
9644 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9645 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9646
9647static void
8cdf0e15 9648create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9649 struct linespec_result *canonical,
e7e0cddf 9650 char *cond_string, char *extra_string,
8cdf0e15
VP
9651 enum bptype type, enum bpdisp disposition,
9652 int thread, int task, int ignore_count,
c0a91b2b 9653 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9654 int enabled, int internal, unsigned flags)
c906108c 9655{
018d34a4 9656 int i;
f8eba3c6 9657 struct linespec_sals *lsal;
cc59ec59 9658
f8eba3c6
TT
9659 if (canonical->pre_expanded)
9660 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9661
9662 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9663 {
f8eba3c6
TT
9664 /* Note that 'addr_string' can be NULL in the case of a plain
9665 'break', without arguments. */
9666 char *addr_string = (canonical->addr_string
9667 ? xstrdup (canonical->addr_string)
9668 : NULL);
9669 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9670 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9671
f8eba3c6
TT
9672 make_cleanup (xfree, filter_string);
9673 create_breakpoint_sal (gdbarch, lsal->sals,
9674 addr_string,
9675 filter_string,
e7e0cddf
SS
9676 cond_string, extra_string,
9677 type, disposition,
84f4c1fe 9678 thread, task, ignore_count, ops,
44f238bb 9679 from_tty, enabled, internal, flags,
56435ebe 9680 canonical->special_display);
f8eba3c6 9681 discard_cleanups (inner);
c3f6f71d 9682 }
c3f6f71d 9683}
c906108c 9684
9998af43 9685/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9686 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9687 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9688 address strings. ADDRESS points to the end of the SAL.
9689
9690 The array and the line spec strings are allocated on the heap, it is
9691 the caller's responsibility to free them. */
c906108c 9692
b9362cc7 9693static void
c3f6f71d 9694parse_breakpoint_sals (char **address,
58438ac1 9695 struct linespec_result *canonical)
c3f6f71d 9696{
c3f6f71d 9697 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9698 breakpoint. */
c3f6f71d
JM
9699 if ((*address) == NULL
9700 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9701 {
1bfeeb0f
JL
9702 /* The last displayed codepoint, if it's valid, is our default breakpoint
9703 address. */
9704 if (last_displayed_sal_is_valid ())
c906108c 9705 {
f8eba3c6 9706 struct linespec_sals lsal;
c3f6f71d 9707 struct symtab_and_line sal;
1c8cdcb1 9708 CORE_ADDR pc;
cc59ec59 9709
4a64f543 9710 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9711 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9712 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9713
9714 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9715 corresponding to the last call to print_frame_info.
9716 Be sure to reinitialize LINE with NOTCURRENT == 0
9717 as the breakpoint line number is inappropriate otherwise.
9718 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9719 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9720 pc = sal.pc;
9721 sal = find_pc_line (pc, 0);
00903456 9722
4a64f543 9723 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9724 where PC is the last displayed codepoint's address. So
9725 make sure to set sal.explicit_pc to prevent GDB from
9726 trying to expand the list of sals to include all other
9727 instances with the same symtab and line. */
1c8cdcb1 9728 sal.pc = pc;
00903456
JK
9729 sal.explicit_pc = 1;
9730
f8eba3c6
TT
9731 lsal.sals.sals[0] = sal;
9732 lsal.sals.nelts = 1;
9733 lsal.canonical = NULL;
9734
9735 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9736 }
9737 else
8a3fe4f8 9738 error (_("No default breakpoint address now."));
c906108c
SS
9739 }
9740 else
9741 {
cc80f267
JK
9742 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9743
c906108c 9744 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9745 current_source_symtab (which is decode_line_1's default).
9746 This should produce the results we want almost all of the
cc80f267
JK
9747 time while leaving default_breakpoint_* alone.
9748
9749 ObjC: However, don't match an Objective-C method name which
9750 may have a '+' or '-' succeeded by a '['. */
9751 if (last_displayed_sal_is_valid ()
9752 && (!cursal.symtab
9753 || ((strchr ("+-", (*address)[0]) != NULL)
9754 && ((*address)[1] != '['))))
f8eba3c6
TT
9755 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9756 get_last_displayed_symtab (),
9757 get_last_displayed_line (),
9758 canonical, NULL, NULL);
c906108c 9759 else
f8eba3c6 9760 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9761 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9762 }
c3f6f71d 9763}
c906108c 9764
c906108c 9765
c3f6f71d 9766/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9767 inserted as a breakpoint. If it can't throw an error. */
c906108c 9768
b9362cc7 9769static void
23e7acfb 9770breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9771{
9772 int i;
cc59ec59 9773
c3f6f71d 9774 for (i = 0; i < sals->nelts; i++)
ee53e872 9775 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9776}
9777
7a697b8d
SS
9778/* Fast tracepoints may have restrictions on valid locations. For
9779 instance, a fast tracepoint using a jump instead of a trap will
9780 likely have to overwrite more bytes than a trap would, and so can
9781 only be placed where the instruction is longer than the jump, or a
9782 multi-instruction sequence does not have a jump into the middle of
9783 it, etc. */
9784
9785static void
9786check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9787 struct symtabs_and_lines *sals)
9788{
9789 int i, rslt;
9790 struct symtab_and_line *sal;
9791 char *msg;
9792 struct cleanup *old_chain;
9793
9794 for (i = 0; i < sals->nelts; i++)
9795 {
f8eba3c6
TT
9796 struct gdbarch *sarch;
9797
7a697b8d
SS
9798 sal = &sals->sals[i];
9799
f8eba3c6
TT
9800 sarch = get_sal_arch (*sal);
9801 /* We fall back to GDBARCH if there is no architecture
9802 associated with SAL. */
9803 if (sarch == NULL)
9804 sarch = gdbarch;
9805 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9806 NULL, &msg);
9807 old_chain = make_cleanup (xfree, msg);
9808
9809 if (!rslt)
9810 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9811 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9812
9813 do_cleanups (old_chain);
9814 }
9815}
9816
af4908ba
KS
9817/* Issue an invalid thread ID error. */
9818
9819static void ATTRIBUTE_NORETURN
9820invalid_thread_id_error (int id)
9821{
9822 error (_("Unknown thread %d."), id);
9823}
9824
018d34a4
VP
9825/* Given TOK, a string specification of condition and thread, as
9826 accepted by the 'break' command, extract the condition
9827 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9828 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9829 If no condition is found, *COND_STRING is set to NULL.
9830 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9831
9832static void
bbc13ae3 9833find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9834 char **cond_string, int *thread, int *task,
9835 char **rest)
018d34a4
VP
9836{
9837 *cond_string = NULL;
9838 *thread = -1;
ed1d1739
KS
9839 *task = 0;
9840 *rest = NULL;
9841
018d34a4
VP
9842 while (tok && *tok)
9843 {
bbc13ae3 9844 const char *end_tok;
018d34a4 9845 int toklen;
bbc13ae3
KS
9846 const char *cond_start = NULL;
9847 const char *cond_end = NULL;
cc59ec59 9848
bbc13ae3 9849 tok = skip_spaces_const (tok);
e7e0cddf
SS
9850
9851 if ((*tok == '"' || *tok == ',') && rest)
9852 {
9853 *rest = savestring (tok, strlen (tok));
9854 return;
9855 }
9856
bbc13ae3 9857 end_tok = skip_to_space_const (tok);
d634f2de 9858
018d34a4 9859 toklen = end_tok - tok;
d634f2de 9860
018d34a4
VP
9861 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9862 {
f7545552
TT
9863 struct expression *expr;
9864
018d34a4 9865 tok = cond_start = end_tok + 1;
1bb9788d 9866 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9867 xfree (expr);
018d34a4 9868 cond_end = tok;
d634f2de 9869 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9870 }
9871 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9872 {
9873 char *tmptok;
d634f2de 9874
018d34a4 9875 tok = end_tok + 1;
bbc13ae3 9876 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9877 if (tok == tmptok)
9878 error (_("Junk after thread keyword."));
9879 if (!valid_thread_id (*thread))
af4908ba 9880 invalid_thread_id_error (*thread);
bbc13ae3 9881 tok = tmptok;
018d34a4 9882 }
4a306c9a
JB
9883 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9884 {
9885 char *tmptok;
9886
9887 tok = end_tok + 1;
bbc13ae3 9888 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9889 if (tok == tmptok)
9890 error (_("Junk after task keyword."));
9891 if (!valid_task_id (*task))
b6199126 9892 error (_("Unknown task %d."), *task);
bbc13ae3 9893 tok = tmptok;
4a306c9a 9894 }
e7e0cddf
SS
9895 else if (rest)
9896 {
9897 *rest = savestring (tok, strlen (tok));
ccab2054 9898 return;
e7e0cddf 9899 }
018d34a4
VP
9900 else
9901 error (_("Junk at end of arguments."));
9902 }
9903}
9904
0fb4aa4b
PA
9905/* Decode a static tracepoint marker spec. */
9906
9907static struct symtabs_and_lines
9908decode_static_tracepoint_spec (char **arg_p)
9909{
9910 VEC(static_tracepoint_marker_p) *markers = NULL;
9911 struct symtabs_and_lines sals;
0fb4aa4b
PA
9912 struct cleanup *old_chain;
9913 char *p = &(*arg_p)[3];
9914 char *endp;
9915 char *marker_str;
9916 int i;
9917
e9cafbcc 9918 p = skip_spaces (p);
0fb4aa4b 9919
e9cafbcc 9920 endp = skip_to_space (p);
0fb4aa4b
PA
9921
9922 marker_str = savestring (p, endp - p);
9923 old_chain = make_cleanup (xfree, marker_str);
9924
9925 markers = target_static_tracepoint_markers_by_strid (marker_str);
9926 if (VEC_empty(static_tracepoint_marker_p, markers))
9927 error (_("No known static tracepoint marker named %s"), marker_str);
9928
9929 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9930 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9931
9932 for (i = 0; i < sals.nelts; i++)
9933 {
9934 struct static_tracepoint_marker *marker;
9935
9936 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9937
9938 init_sal (&sals.sals[i]);
9939
9940 sals.sals[i] = find_pc_line (marker->address, 0);
9941 sals.sals[i].pc = marker->address;
9942
9943 release_static_tracepoint_marker (marker);
9944 }
9945
9946 do_cleanups (old_chain);
9947
9948 *arg_p = endp;
9949 return sals;
9950}
9951
fd9b8c24
PA
9952/* Set a breakpoint. This function is shared between CLI and MI
9953 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9954 modes of operations, selected by the PARSE_ARG parameter. If
9955 non-zero, the function will parse ARG, extracting location,
9956 condition, thread and extra string. Otherwise, ARG is just the
9957 breakpoint's location, with condition, thread, and extra string
9958 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9959 If INTERNAL is non-zero, the breakpoint number will be allocated
9960 from the internal breakpoint count. Returns true if any breakpoint
9961 was created; false otherwise. */
0101ce28 9962
8cdf0e15
VP
9963int
9964create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9965 char *arg, char *cond_string,
9966 int thread, char *extra_string,
f6de8ec2 9967 int parse_arg,
0fb4aa4b 9968 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9969 int ignore_count,
9970 enum auto_boolean pending_break_support,
c0a91b2b 9971 const struct breakpoint_ops *ops,
44f238bb
PA
9972 int from_tty, int enabled, int internal,
9973 unsigned flags)
c3f6f71d 9974{
b78a6381 9975 volatile struct gdb_exception e;
f8eba3c6 9976 char *copy_arg = NULL;
c3f6f71d 9977 char *addr_start = arg;
7efd8fc2 9978 struct linespec_result canonical;
c3f6f71d 9979 struct cleanup *old_chain;
80c99de1 9980 struct cleanup *bkpt_chain = NULL;
0101ce28 9981 int pending = 0;
4a306c9a 9982 int task = 0;
86b17b60 9983 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9984
348d480f
PA
9985 gdb_assert (ops != NULL);
9986
7efd8fc2 9987 init_linespec_result (&canonical);
c3f6f71d 9988
b78a6381
TT
9989 TRY_CATCH (e, RETURN_MASK_ALL)
9990 {
983af33b
SDJ
9991 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9992 addr_start, &copy_arg);
b78a6381 9993 }
0101ce28
JJ
9994
9995 /* If caller is interested in rc value from parse, set value. */
05ff989b 9996 switch (e.reason)
0101ce28 9997 {
983af33b
SDJ
9998 case GDB_NO_ERROR:
9999 if (VEC_empty (linespec_sals, canonical.sals))
10000 return 0;
10001 break;
05ff989b
AC
10002 case RETURN_ERROR:
10003 switch (e.error)
0101ce28 10004 {
05ff989b 10005 case NOT_FOUND_ERROR:
0101ce28 10006
05ff989b
AC
10007 /* If pending breakpoint support is turned off, throw
10008 error. */
fa8d40ab
JJ
10009
10010 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10011 throw_exception (e);
10012
10013 exception_print (gdb_stderr, e);
fa8d40ab 10014
05ff989b
AC
10015 /* If pending breakpoint support is auto query and the user
10016 selects no, then simply return the error code. */
059fb39f 10017 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10018 && !nquery (_("Make %s pending on future shared library load? "),
10019 bptype_string (type_wanted)))
fd9b8c24 10020 return 0;
fa8d40ab 10021
05ff989b
AC
10022 /* At this point, either the user was queried about setting
10023 a pending breakpoint and selected yes, or pending
10024 breakpoint behavior is on and thus a pending breakpoint
10025 is defaulted on behalf of the user. */
f8eba3c6
TT
10026 {
10027 struct linespec_sals lsal;
10028
10029 copy_arg = xstrdup (addr_start);
10030 lsal.canonical = xstrdup (copy_arg);
10031 lsal.sals.nelts = 1;
10032 lsal.sals.sals = XNEW (struct symtab_and_line);
10033 init_sal (&lsal.sals.sals[0]);
10034 pending = 1;
10035 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10036 }
05ff989b
AC
10037 break;
10038 default:
98deb0da 10039 throw_exception (e);
0101ce28 10040 }
2abae994 10041 break;
05ff989b 10042 default:
983af33b 10043 throw_exception (e);
0101ce28 10044 }
c3f6f71d 10045
4a64f543 10046 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10047 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10048
c3f6f71d
JM
10049 /* ----------------------------- SNIP -----------------------------
10050 Anything added to the cleanup chain beyond this point is assumed
10051 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10052 then the memory is not reclaimed. */
10053 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10054
c3f6f71d
JM
10055 /* Resolve all line numbers to PC's and verify that the addresses
10056 are ok for the target. */
0101ce28 10057 if (!pending)
f8eba3c6
TT
10058 {
10059 int ix;
10060 struct linespec_sals *iter;
10061
10062 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10063 breakpoint_sals_to_pc (&iter->sals);
10064 }
c3f6f71d 10065
7a697b8d 10066 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10067 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10068 {
10069 int ix;
10070 struct linespec_sals *iter;
10071
10072 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10073 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10074 }
7a697b8d 10075
c3f6f71d
JM
10076 /* Verify that condition can be parsed, before setting any
10077 breakpoints. Allocate a separate condition expression for each
4a64f543 10078 breakpoint. */
0101ce28 10079 if (!pending)
c3f6f71d 10080 {
f6de8ec2 10081 if (parse_arg)
72b2ff0e 10082 {
0878d0fa 10083 char *rest;
52d361e1
YQ
10084 struct linespec_sals *lsal;
10085
10086 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10087
0878d0fa
YQ
10088 /* Here we only parse 'arg' to separate condition
10089 from thread number, so parsing in context of first
10090 sal is OK. When setting the breakpoint we'll
10091 re-parse it in context of each sal. */
10092
10093 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10094 &thread, &task, &rest);
10095 if (cond_string)
10096 make_cleanup (xfree, cond_string);
10097 if (rest)
10098 make_cleanup (xfree, rest);
10099 if (rest)
10100 extra_string = rest;
72b2ff0e 10101 }
2f069f6f 10102 else
72b2ff0e 10103 {
0878d0fa
YQ
10104 if (*arg != '\0')
10105 error (_("Garbage '%s' at end of location"), arg);
10106
10107 /* Create a private copy of condition string. */
10108 if (cond_string)
10109 {
10110 cond_string = xstrdup (cond_string);
10111 make_cleanup (xfree, cond_string);
10112 }
10113 /* Create a private copy of any extra string. */
10114 if (extra_string)
10115 {
10116 extra_string = xstrdup (extra_string);
10117 make_cleanup (xfree, extra_string);
10118 }
72b2ff0e 10119 }
0fb4aa4b 10120
52d361e1 10121 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10122 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10123 tempflag ? disp_del : disp_donttouch,
10124 thread, task, ignore_count, ops,
44f238bb 10125 from_tty, enabled, internal, flags);
c906108c 10126 }
0101ce28
JJ
10127 else
10128 {
0101ce28
JJ
10129 struct breakpoint *b;
10130
0101ce28
JJ
10131 make_cleanup (xfree, copy_arg);
10132
bfccc43c
YQ
10133 if (is_tracepoint_type (type_wanted))
10134 {
10135 struct tracepoint *t;
10136
10137 t = XCNEW (struct tracepoint);
10138 b = &t->base;
10139 }
10140 else
10141 b = XNEW (struct breakpoint);
10142
10143 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10144
f8eba3c6 10145 b->addr_string = copy_arg;
f6de8ec2 10146 if (parse_arg)
e12c7713
MK
10147 b->cond_string = NULL;
10148 else
10149 {
10150 /* Create a private copy of condition string. */
10151 if (cond_string)
10152 {
10153 cond_string = xstrdup (cond_string);
10154 make_cleanup (xfree, cond_string);
10155 }
10156 b->cond_string = cond_string;
10157 }
e7e0cddf 10158 b->extra_string = NULL;
0101ce28 10159 b->ignore_count = ignore_count;
0101ce28 10160 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10161 b->condition_not_parsed = 1;
41447f92 10162 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10163 if ((type_wanted != bp_breakpoint
10164 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10165 b->pspace = current_program_space;
8bea4e01 10166
bfccc43c 10167 install_breakpoint (internal, b, 0);
0101ce28
JJ
10168 }
10169
f8eba3c6 10170 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10171 {
3e43a32a
MS
10172 warning (_("Multiple breakpoints were set.\nUse the "
10173 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10174 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10175 }
10176
80c99de1
PA
10177 /* That's it. Discard the cleanups for data inserted into the
10178 breakpoint. */
10179 discard_cleanups (bkpt_chain);
10180 /* But cleanup everything else. */
c3f6f71d 10181 do_cleanups (old_chain);
217dc9e2 10182
80c99de1 10183 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10184 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10185
10186 return 1;
c3f6f71d 10187}
c906108c 10188
348d480f 10189/* Set a breakpoint.
72b2ff0e
VP
10190 ARG is a string describing breakpoint address,
10191 condition, and thread.
10192 FLAG specifies if a breakpoint is hardware on,
10193 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10194 and BP_TEMPFLAG. */
348d480f 10195
98deb0da 10196static void
72b2ff0e 10197break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10198{
72b2ff0e 10199 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10200 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10201 ? bp_hardware_breakpoint
10202 : bp_breakpoint);
55aa24fb
SDJ
10203 struct breakpoint_ops *ops;
10204 const char *arg_cp = arg;
10205
10206 /* Matching breakpoints on probes. */
10207 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10208 ops = &bkpt_probe_breakpoint_ops;
10209 else
10210 ops = &bkpt_breakpoint_ops;
c3f6f71d 10211
8cdf0e15
VP
10212 create_breakpoint (get_current_arch (),
10213 arg,
e7e0cddf 10214 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10215 tempflag, type_wanted,
8cdf0e15
VP
10216 0 /* Ignore count */,
10217 pending_break_support,
55aa24fb 10218 ops,
8cdf0e15 10219 from_tty,
84f4c1fe 10220 1 /* enabled */,
44f238bb
PA
10221 0 /* internal */,
10222 0);
c906108c
SS
10223}
10224
c906108c
SS
10225/* Helper function for break_command_1 and disassemble_command. */
10226
10227void
fba45db2 10228resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10229{
10230 CORE_ADDR pc;
10231
10232 if (sal->pc == 0 && sal->symtab != NULL)
10233 {
10234 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10235 error (_("No line %d in file \"%s\"."),
05cba821 10236 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10237 sal->pc = pc;
6a048695 10238
4a64f543
MS
10239 /* If this SAL corresponds to a breakpoint inserted using a line
10240 number, then skip the function prologue if necessary. */
6a048695 10241 if (sal->explicit_line)
059acae7 10242 skip_prologue_sal (sal);
c906108c
SS
10243 }
10244
10245 if (sal->section == 0 && sal->symtab != NULL)
10246 {
346d1dfe 10247 const struct blockvector *bv;
3977b71f 10248 const struct block *b;
c5aa993b 10249 struct symbol *sym;
c906108c 10250
43f3e411
DE
10251 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10252 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10253 if (bv != NULL)
10254 {
7f0df278 10255 sym = block_linkage_function (b);
c906108c
SS
10256 if (sym != NULL)
10257 {
eb822aa6
DE
10258 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10259 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10260 sym);
c906108c
SS
10261 }
10262 else
10263 {
4a64f543
MS
10264 /* It really is worthwhile to have the section, so we'll
10265 just have to look harder. This case can be executed
10266 if we have line numbers but no functions (as can
10267 happen in assembly source). */
c906108c 10268
7cbd4a93 10269 struct bound_minimal_symbol msym;
6c95b8df
PA
10270 struct cleanup *old_chain = save_current_space_and_thread ();
10271
10272 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10273
10274 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10275 if (msym.minsym)
efd66ac6 10276 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10277
10278 do_cleanups (old_chain);
c906108c
SS
10279 }
10280 }
10281 }
10282}
10283
10284void
fba45db2 10285break_command (char *arg, int from_tty)
c906108c 10286{
db107f19 10287 break_command_1 (arg, 0, from_tty);
c906108c
SS
10288}
10289
c906108c 10290void
fba45db2 10291tbreak_command (char *arg, int from_tty)
c906108c 10292{
db107f19 10293 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10294}
10295
c906108c 10296static void
fba45db2 10297hbreak_command (char *arg, int from_tty)
c906108c 10298{
db107f19 10299 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10300}
10301
10302static void
fba45db2 10303thbreak_command (char *arg, int from_tty)
c906108c 10304{
db107f19 10305 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10306}
10307
10308static void
fba45db2 10309stop_command (char *arg, int from_tty)
c906108c 10310{
a3f17187 10311 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10312Usage: stop in <function | address>\n\
a3f17187 10313 stop at <line>\n"));
c906108c
SS
10314}
10315
10316static void
fba45db2 10317stopin_command (char *arg, int from_tty)
c906108c
SS
10318{
10319 int badInput = 0;
10320
c5aa993b 10321 if (arg == (char *) NULL)
c906108c
SS
10322 badInput = 1;
10323 else if (*arg != '*')
10324 {
10325 char *argptr = arg;
10326 int hasColon = 0;
10327
4a64f543 10328 /* Look for a ':'. If this is a line number specification, then
53a5351d 10329 say it is bad, otherwise, it should be an address or
4a64f543 10330 function/method name. */
c906108c 10331 while (*argptr && !hasColon)
c5aa993b
JM
10332 {
10333 hasColon = (*argptr == ':');
10334 argptr++;
10335 }
c906108c
SS
10336
10337 if (hasColon)
c5aa993b 10338 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10339 else
c5aa993b 10340 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10341 }
10342
10343 if (badInput)
a3f17187 10344 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10345 else
db107f19 10346 break_command_1 (arg, 0, from_tty);
c906108c
SS
10347}
10348
10349static void
fba45db2 10350stopat_command (char *arg, int from_tty)
c906108c
SS
10351{
10352 int badInput = 0;
10353
c5aa993b 10354 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10355 badInput = 1;
10356 else
10357 {
10358 char *argptr = arg;
10359 int hasColon = 0;
10360
4a64f543
MS
10361 /* Look for a ':'. If there is a '::' then get out, otherwise
10362 it is probably a line number. */
c906108c 10363 while (*argptr && !hasColon)
c5aa993b
JM
10364 {
10365 hasColon = (*argptr == ':');
10366 argptr++;
10367 }
c906108c
SS
10368
10369 if (hasColon)
c5aa993b 10370 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10371 else
c5aa993b 10372 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10373 }
10374
10375 if (badInput)
a3f17187 10376 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10377 else
db107f19 10378 break_command_1 (arg, 0, from_tty);
c906108c
SS
10379}
10380
e7e0cddf
SS
10381/* The dynamic printf command is mostly like a regular breakpoint, but
10382 with a prewired command list consisting of a single output command,
10383 built from extra arguments supplied on the dprintf command
10384 line. */
10385
da821c7b 10386static void
e7e0cddf
SS
10387dprintf_command (char *arg, int from_tty)
10388{
10389 create_breakpoint (get_current_arch (),
10390 arg,
10391 NULL, 0, NULL, 1 /* parse arg */,
10392 0, bp_dprintf,
10393 0 /* Ignore count */,
10394 pending_break_support,
10395 &dprintf_breakpoint_ops,
10396 from_tty,
10397 1 /* enabled */,
10398 0 /* internal */,
10399 0);
10400}
10401
d3ce09f5
SS
10402static void
10403agent_printf_command (char *arg, int from_tty)
10404{
10405 error (_("May only run agent-printf on the target"));
10406}
10407
f1310107
TJB
10408/* Implement the "breakpoint_hit" breakpoint_ops method for
10409 ranged breakpoints. */
10410
10411static int
10412breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10413 struct address_space *aspace,
09ac7c10
TT
10414 CORE_ADDR bp_addr,
10415 const struct target_waitstatus *ws)
f1310107 10416{
09ac7c10 10417 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10418 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10419 return 0;
10420
f1310107
TJB
10421 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10422 bl->length, aspace, bp_addr);
10423}
10424
10425/* Implement the "resources_needed" breakpoint_ops method for
10426 ranged breakpoints. */
10427
10428static int
10429resources_needed_ranged_breakpoint (const struct bp_location *bl)
10430{
10431 return target_ranged_break_num_registers ();
10432}
10433
10434/* Implement the "print_it" breakpoint_ops method for
10435 ranged breakpoints. */
10436
10437static enum print_stop_action
348d480f 10438print_it_ranged_breakpoint (bpstat bs)
f1310107 10439{
348d480f 10440 struct breakpoint *b = bs->breakpoint_at;
f1310107 10441 struct bp_location *bl = b->loc;
79a45e25 10442 struct ui_out *uiout = current_uiout;
f1310107
TJB
10443
10444 gdb_assert (b->type == bp_hardware_breakpoint);
10445
10446 /* Ranged breakpoints have only one location. */
10447 gdb_assert (bl && bl->next == NULL);
10448
10449 annotate_breakpoint (b->number);
10450 if (b->disposition == disp_del)
10451 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10452 else
10453 ui_out_text (uiout, "\nRanged breakpoint ");
10454 if (ui_out_is_mi_like_p (uiout))
10455 {
10456 ui_out_field_string (uiout, "reason",
10457 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10458 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10459 }
10460 ui_out_field_int (uiout, "bkptno", b->number);
10461 ui_out_text (uiout, ", ");
10462
10463 return PRINT_SRC_AND_LOC;
10464}
10465
10466/* Implement the "print_one" breakpoint_ops method for
10467 ranged breakpoints. */
10468
10469static void
10470print_one_ranged_breakpoint (struct breakpoint *b,
10471 struct bp_location **last_loc)
10472{
10473 struct bp_location *bl = b->loc;
10474 struct value_print_options opts;
79a45e25 10475 struct ui_out *uiout = current_uiout;
f1310107
TJB
10476
10477 /* Ranged breakpoints have only one location. */
10478 gdb_assert (bl && bl->next == NULL);
10479
10480 get_user_print_options (&opts);
10481
10482 if (opts.addressprint)
10483 /* We don't print the address range here, it will be printed later
10484 by print_one_detail_ranged_breakpoint. */
10485 ui_out_field_skip (uiout, "addr");
10486 annotate_field (5);
10487 print_breakpoint_location (b, bl);
10488 *last_loc = bl;
10489}
10490
10491/* Implement the "print_one_detail" breakpoint_ops method for
10492 ranged breakpoints. */
10493
10494static void
10495print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10496 struct ui_out *uiout)
10497{
10498 CORE_ADDR address_start, address_end;
10499 struct bp_location *bl = b->loc;
f99d8bf4
PA
10500 struct ui_file *stb = mem_fileopen ();
10501 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10502
10503 gdb_assert (bl);
10504
10505 address_start = bl->address;
10506 address_end = address_start + bl->length - 1;
10507
10508 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10509 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10510 print_core_address (bl->gdbarch, address_start),
10511 print_core_address (bl->gdbarch, address_end));
10512 ui_out_field_stream (uiout, "addr", stb);
10513 ui_out_text (uiout, "\n");
10514
10515 do_cleanups (cleanup);
10516}
10517
10518/* Implement the "print_mention" breakpoint_ops method for
10519 ranged breakpoints. */
10520
10521static void
10522print_mention_ranged_breakpoint (struct breakpoint *b)
10523{
10524 struct bp_location *bl = b->loc;
79a45e25 10525 struct ui_out *uiout = current_uiout;
f1310107
TJB
10526
10527 gdb_assert (bl);
10528 gdb_assert (b->type == bp_hardware_breakpoint);
10529
10530 if (ui_out_is_mi_like_p (uiout))
10531 return;
10532
10533 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10534 b->number, paddress (bl->gdbarch, bl->address),
10535 paddress (bl->gdbarch, bl->address + bl->length - 1));
10536}
10537
10538/* Implement the "print_recreate" breakpoint_ops method for
10539 ranged breakpoints. */
10540
10541static void
10542print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10543{
10544 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10545 b->addr_string_range_end);
d9b3f62e 10546 print_recreate_thread (b, fp);
f1310107
TJB
10547}
10548
10549/* The breakpoint_ops structure to be used in ranged breakpoints. */
10550
2060206e 10551static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10552
10553/* Find the address where the end of the breakpoint range should be
10554 placed, given the SAL of the end of the range. This is so that if
10555 the user provides a line number, the end of the range is set to the
10556 last instruction of the given line. */
10557
10558static CORE_ADDR
10559find_breakpoint_range_end (struct symtab_and_line sal)
10560{
10561 CORE_ADDR end;
10562
10563 /* If the user provided a PC value, use it. Otherwise,
10564 find the address of the end of the given location. */
10565 if (sal.explicit_pc)
10566 end = sal.pc;
10567 else
10568 {
10569 int ret;
10570 CORE_ADDR start;
10571
10572 ret = find_line_pc_range (sal, &start, &end);
10573 if (!ret)
10574 error (_("Could not find location of the end of the range."));
10575
10576 /* find_line_pc_range returns the start of the next line. */
10577 end--;
10578 }
10579
10580 return end;
10581}
10582
10583/* Implement the "break-range" CLI command. */
10584
10585static void
10586break_range_command (char *arg, int from_tty)
10587{
10588 char *arg_start, *addr_string_start, *addr_string_end;
10589 struct linespec_result canonical_start, canonical_end;
10590 int bp_count, can_use_bp, length;
10591 CORE_ADDR end;
10592 struct breakpoint *b;
10593 struct symtab_and_line sal_start, sal_end;
f1310107 10594 struct cleanup *cleanup_bkpt;
f8eba3c6 10595 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10596
10597 /* We don't support software ranged breakpoints. */
10598 if (target_ranged_break_num_registers () < 0)
10599 error (_("This target does not support hardware ranged breakpoints."));
10600
10601 bp_count = hw_breakpoint_used_count ();
10602 bp_count += target_ranged_break_num_registers ();
10603 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10604 bp_count, 0);
10605 if (can_use_bp < 0)
10606 error (_("Hardware breakpoints used exceeds limit."));
10607
f8eba3c6 10608 arg = skip_spaces (arg);
f1310107
TJB
10609 if (arg == NULL || arg[0] == '\0')
10610 error(_("No address range specified."));
10611
f1310107
TJB
10612 init_linespec_result (&canonical_start);
10613
f8eba3c6
TT
10614 arg_start = arg;
10615 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10616
f8eba3c6 10617 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10618
10619 if (arg[0] != ',')
10620 error (_("Too few arguments."));
f8eba3c6 10621 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10622 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10623
10624 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10625
10626 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10627 || lsal_start->sals.nelts != 1)
f1310107
TJB
10628 error (_("Cannot create a ranged breakpoint with multiple locations."));
10629
f8eba3c6
TT
10630 sal_start = lsal_start->sals.sals[0];
10631 addr_string_start = savestring (arg_start, arg - arg_start);
10632 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10633
10634 arg++; /* Skip the comma. */
f8eba3c6 10635 arg = skip_spaces (arg);
f1310107
TJB
10636
10637 /* Parse the end location. */
10638
f1310107
TJB
10639 init_linespec_result (&canonical_end);
10640 arg_start = arg;
10641
f8eba3c6 10642 /* We call decode_line_full directly here instead of using
f1310107
TJB
10643 parse_breakpoint_sals because we need to specify the start location's
10644 symtab and line as the default symtab and line for the end of the
10645 range. This makes it possible to have ranges like "foo.c:27, +14",
10646 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10647 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10648 sal_start.symtab, sal_start.line,
10649 &canonical_end, NULL, NULL);
10650
10651 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10652
f8eba3c6 10653 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10654 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10655
10656 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10657 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10658 || lsal_end->sals.nelts != 1)
f1310107
TJB
10659 error (_("Cannot create a ranged breakpoint with multiple locations."));
10660
f8eba3c6
TT
10661 sal_end = lsal_end->sals.sals[0];
10662 addr_string_end = savestring (arg_start, arg - arg_start);
10663 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10664
10665 end = find_breakpoint_range_end (sal_end);
10666 if (sal_start.pc > end)
177b42fe 10667 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10668
10669 length = end - sal_start.pc + 1;
10670 if (length < 0)
10671 /* Length overflowed. */
10672 error (_("Address range too large."));
10673 else if (length == 1)
10674 {
10675 /* This range is simple enough to be handled by
10676 the `hbreak' command. */
10677 hbreak_command (addr_string_start, 1);
10678
10679 do_cleanups (cleanup_bkpt);
10680
10681 return;
10682 }
10683
10684 /* Now set up the breakpoint. */
10685 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10686 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10687 set_breakpoint_count (breakpoint_count + 1);
10688 b->number = breakpoint_count;
10689 b->disposition = disp_donttouch;
f8eba3c6
TT
10690 b->addr_string = xstrdup (addr_string_start);
10691 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10692 b->loc->length = length;
10693
f8eba3c6 10694 do_cleanups (cleanup_bkpt);
f1310107
TJB
10695
10696 mention (b);
8d3788bd 10697 observer_notify_breakpoint_created (b);
44702360 10698 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10699}
10700
4a64f543
MS
10701/* Return non-zero if EXP is verified as constant. Returned zero
10702 means EXP is variable. Also the constant detection may fail for
10703 some constant expressions and in such case still falsely return
10704 zero. */
2e6e3d9c 10705
65d79d4b
SDJ
10706static int
10707watchpoint_exp_is_const (const struct expression *exp)
10708{
10709 int i = exp->nelts;
10710
10711 while (i > 0)
10712 {
10713 int oplenp, argsp;
10714
10715 /* We are only interested in the descriptor of each element. */
10716 operator_length (exp, i, &oplenp, &argsp);
10717 i -= oplenp;
10718
10719 switch (exp->elts[i].opcode)
10720 {
10721 case BINOP_ADD:
10722 case BINOP_SUB:
10723 case BINOP_MUL:
10724 case BINOP_DIV:
10725 case BINOP_REM:
10726 case BINOP_MOD:
10727 case BINOP_LSH:
10728 case BINOP_RSH:
10729 case BINOP_LOGICAL_AND:
10730 case BINOP_LOGICAL_OR:
10731 case BINOP_BITWISE_AND:
10732 case BINOP_BITWISE_IOR:
10733 case BINOP_BITWISE_XOR:
10734 case BINOP_EQUAL:
10735 case BINOP_NOTEQUAL:
10736 case BINOP_LESS:
10737 case BINOP_GTR:
10738 case BINOP_LEQ:
10739 case BINOP_GEQ:
10740 case BINOP_REPEAT:
10741 case BINOP_COMMA:
10742 case BINOP_EXP:
10743 case BINOP_MIN:
10744 case BINOP_MAX:
10745 case BINOP_INTDIV:
10746 case BINOP_CONCAT:
65d79d4b
SDJ
10747 case TERNOP_COND:
10748 case TERNOP_SLICE:
65d79d4b
SDJ
10749
10750 case OP_LONG:
10751 case OP_DOUBLE:
10752 case OP_DECFLOAT:
10753 case OP_LAST:
10754 case OP_COMPLEX:
10755 case OP_STRING:
65d79d4b
SDJ
10756 case OP_ARRAY:
10757 case OP_TYPE:
608b4967
TT
10758 case OP_TYPEOF:
10759 case OP_DECLTYPE:
6e72ca20 10760 case OP_TYPEID:
65d79d4b
SDJ
10761 case OP_NAME:
10762 case OP_OBJC_NSSTRING:
10763
10764 case UNOP_NEG:
10765 case UNOP_LOGICAL_NOT:
10766 case UNOP_COMPLEMENT:
10767 case UNOP_ADDR:
10768 case UNOP_HIGH:
aeaa2474 10769 case UNOP_CAST:
9eaf6705
TT
10770
10771 case UNOP_CAST_TYPE:
10772 case UNOP_REINTERPRET_CAST:
10773 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10774 /* Unary, binary and ternary operators: We have to check
10775 their operands. If they are constant, then so is the
10776 result of that operation. For instance, if A and B are
10777 determined to be constants, then so is "A + B".
10778
10779 UNOP_IND is one exception to the rule above, because the
10780 value of *ADDR is not necessarily a constant, even when
10781 ADDR is. */
65d79d4b
SDJ
10782 break;
10783
10784 case OP_VAR_VALUE:
10785 /* Check whether the associated symbol is a constant.
4a64f543 10786
65d79d4b 10787 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10788 possible that a buggy compiler could mark a variable as
10789 constant even when it is not, and TYPE_CONST would return
10790 true in this case, while SYMBOL_CLASS wouldn't.
10791
10792 We also have to check for function symbols because they
10793 are always constant. */
65d79d4b
SDJ
10794 {
10795 struct symbol *s = exp->elts[i + 2].symbol;
10796
10797 if (SYMBOL_CLASS (s) != LOC_BLOCK
10798 && SYMBOL_CLASS (s) != LOC_CONST
10799 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10800 return 0;
10801 break;
10802 }
10803
10804 /* The default action is to return 0 because we are using
10805 the optimistic approach here: If we don't know something,
10806 then it is not a constant. */
10807 default:
10808 return 0;
10809 }
10810 }
10811
10812 return 1;
10813}
10814
3a5c3e22
PA
10815/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10816
10817static void
10818dtor_watchpoint (struct breakpoint *self)
10819{
10820 struct watchpoint *w = (struct watchpoint *) self;
10821
10822 xfree (w->cond_exp);
10823 xfree (w->exp);
10824 xfree (w->exp_string);
10825 xfree (w->exp_string_reparse);
10826 value_free (w->val);
10827
10828 base_breakpoint_ops.dtor (self);
10829}
10830
348d480f
PA
10831/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10832
10833static void
10834re_set_watchpoint (struct breakpoint *b)
10835{
3a5c3e22
PA
10836 struct watchpoint *w = (struct watchpoint *) b;
10837
348d480f
PA
10838 /* Watchpoint can be either on expression using entirely global
10839 variables, or it can be on local variables.
10840
10841 Watchpoints of the first kind are never auto-deleted, and even
10842 persist across program restarts. Since they can use variables
10843 from shared libraries, we need to reparse expression as libraries
10844 are loaded and unloaded.
10845
10846 Watchpoints on local variables can also change meaning as result
10847 of solib event. For example, if a watchpoint uses both a local
10848 and a global variables in expression, it's a local watchpoint,
10849 but unloading of a shared library will make the expression
10850 invalid. This is not a very common use case, but we still
10851 re-evaluate expression, to avoid surprises to the user.
10852
10853 Note that for local watchpoints, we re-evaluate it only if
10854 watchpoints frame id is still valid. If it's not, it means the
10855 watchpoint is out of scope and will be deleted soon. In fact,
10856 I'm not sure we'll ever be called in this case.
10857
10858 If a local watchpoint's frame id is still valid, then
3a5c3e22 10859 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10860
3a5c3e22
PA
10861 Don't do anything about disabled watchpoints, since they will be
10862 reevaluated again when enabled. */
10863 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10864}
10865
77b06cd7
TJB
10866/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10867
10868static int
10869insert_watchpoint (struct bp_location *bl)
10870{
3a5c3e22
PA
10871 struct watchpoint *w = (struct watchpoint *) bl->owner;
10872 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10873
10874 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10875 w->cond_exp);
77b06cd7
TJB
10876}
10877
10878/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10879
10880static int
10881remove_watchpoint (struct bp_location *bl)
10882{
3a5c3e22
PA
10883 struct watchpoint *w = (struct watchpoint *) bl->owner;
10884 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10885
10886 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10887 w->cond_exp);
e09342b5
TJB
10888}
10889
e09342b5 10890static int
348d480f 10891breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10892 struct address_space *aspace, CORE_ADDR bp_addr,
10893 const struct target_waitstatus *ws)
e09342b5 10894{
348d480f 10895 struct breakpoint *b = bl->owner;
3a5c3e22 10896 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10897
348d480f
PA
10898 /* Continuable hardware watchpoints are treated as non-existent if the
10899 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10900 some data address). Otherwise gdb won't stop on a break instruction
10901 in the code (not from a breakpoint) when a hardware watchpoint has
10902 been defined. Also skip watchpoints which we know did not trigger
10903 (did not match the data address). */
10904 if (is_hardware_watchpoint (b)
3a5c3e22 10905 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10906 return 0;
9c06b0b4 10907
348d480f 10908 return 1;
9c06b0b4
TJB
10909}
10910
348d480f
PA
10911static void
10912check_status_watchpoint (bpstat bs)
9c06b0b4 10913{
348d480f 10914 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10915
348d480f 10916 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10917}
10918
10919/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10920 hardware watchpoints. */
9c06b0b4
TJB
10921
10922static int
348d480f 10923resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10924{
3a5c3e22
PA
10925 struct watchpoint *w = (struct watchpoint *) bl->owner;
10926 int length = w->exact? 1 : bl->length;
348d480f
PA
10927
10928 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10929}
10930
10931/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10932 hardware watchpoints. */
9c06b0b4
TJB
10933
10934static int
348d480f 10935works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10936{
efa80663
PA
10937 /* Read and access watchpoints only work with hardware support. */
10938 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10939}
10940
9c06b0b4 10941static enum print_stop_action
348d480f 10942print_it_watchpoint (bpstat bs)
9c06b0b4 10943{
348d480f
PA
10944 struct cleanup *old_chain;
10945 struct breakpoint *b;
f99d8bf4 10946 struct ui_file *stb;
348d480f 10947 enum print_stop_action result;
3a5c3e22 10948 struct watchpoint *w;
79a45e25 10949 struct ui_out *uiout = current_uiout;
348d480f
PA
10950
10951 gdb_assert (bs->bp_location_at != NULL);
10952
348d480f 10953 b = bs->breakpoint_at;
3a5c3e22 10954 w = (struct watchpoint *) b;
348d480f 10955
f99d8bf4
PA
10956 stb = mem_fileopen ();
10957 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10958
10959 switch (b->type)
10960 {
348d480f 10961 case bp_watchpoint:
9c06b0b4
TJB
10962 case bp_hardware_watchpoint:
10963 annotate_watchpoint (b->number);
10964 if (ui_out_is_mi_like_p (uiout))
10965 ui_out_field_string
10966 (uiout, "reason",
10967 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10968 mention (b);
10969 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10970 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10971 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10972 ui_out_field_stream (uiout, "old", stb);
10973 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10974 watchpoint_value_print (w->val, stb);
348d480f
PA
10975 ui_out_field_stream (uiout, "new", stb);
10976 ui_out_text (uiout, "\n");
10977 /* More than one watchpoint may have been triggered. */
10978 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10979 break;
10980
10981 case bp_read_watchpoint:
10982 if (ui_out_is_mi_like_p (uiout))
10983 ui_out_field_string
10984 (uiout, "reason",
10985 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10986 mention (b);
10987 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10988 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10989 watchpoint_value_print (w->val, stb);
348d480f
PA
10990 ui_out_field_stream (uiout, "value", stb);
10991 ui_out_text (uiout, "\n");
10992 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10993 break;
10994
10995 case bp_access_watchpoint:
348d480f
PA
10996 if (bs->old_val != NULL)
10997 {
10998 annotate_watchpoint (b->number);
10999 if (ui_out_is_mi_like_p (uiout))
11000 ui_out_field_string
11001 (uiout, "reason",
11002 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11003 mention (b);
11004 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11005 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11006 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11007 ui_out_field_stream (uiout, "old", stb);
11008 ui_out_text (uiout, "\nNew value = ");
11009 }
11010 else
11011 {
11012 mention (b);
11013 if (ui_out_is_mi_like_p (uiout))
11014 ui_out_field_string
11015 (uiout, "reason",
11016 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11017 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11018 ui_out_text (uiout, "\nValue = ");
11019 }
f99d8bf4 11020 watchpoint_value_print (w->val, stb);
348d480f
PA
11021 ui_out_field_stream (uiout, "new", stb);
11022 ui_out_text (uiout, "\n");
11023 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11024 break;
11025 default:
348d480f 11026 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11027 }
11028
348d480f
PA
11029 do_cleanups (old_chain);
11030 return result;
11031}
11032
11033/* Implement the "print_mention" breakpoint_ops method for hardware
11034 watchpoints. */
11035
11036static void
11037print_mention_watchpoint (struct breakpoint *b)
11038{
11039 struct cleanup *ui_out_chain;
3a5c3e22 11040 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11041 struct ui_out *uiout = current_uiout;
348d480f
PA
11042
11043 switch (b->type)
11044 {
11045 case bp_watchpoint:
11046 ui_out_text (uiout, "Watchpoint ");
11047 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11048 break;
11049 case bp_hardware_watchpoint:
11050 ui_out_text (uiout, "Hardware watchpoint ");
11051 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11052 break;
11053 case bp_read_watchpoint:
11054 ui_out_text (uiout, "Hardware read watchpoint ");
11055 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11056 break;
11057 case bp_access_watchpoint:
11058 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11059 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11060 break;
11061 default:
11062 internal_error (__FILE__, __LINE__,
11063 _("Invalid hardware watchpoint type."));
11064 }
11065
11066 ui_out_field_int (uiout, "number", b->number);
11067 ui_out_text (uiout, ": ");
3a5c3e22 11068 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11069 do_cleanups (ui_out_chain);
11070}
11071
11072/* Implement the "print_recreate" breakpoint_ops method for
11073 watchpoints. */
11074
11075static void
11076print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11077{
3a5c3e22
PA
11078 struct watchpoint *w = (struct watchpoint *) b;
11079
348d480f
PA
11080 switch (b->type)
11081 {
11082 case bp_watchpoint:
11083 case bp_hardware_watchpoint:
11084 fprintf_unfiltered (fp, "watch");
11085 break;
11086 case bp_read_watchpoint:
11087 fprintf_unfiltered (fp, "rwatch");
11088 break;
11089 case bp_access_watchpoint:
11090 fprintf_unfiltered (fp, "awatch");
11091 break;
11092 default:
11093 internal_error (__FILE__, __LINE__,
11094 _("Invalid watchpoint type."));
11095 }
11096
3a5c3e22 11097 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11098 print_recreate_thread (b, fp);
348d480f
PA
11099}
11100
427cd150
TT
11101/* Implement the "explains_signal" breakpoint_ops method for
11102 watchpoints. */
11103
47591c29 11104static int
427cd150
TT
11105explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11106{
11107 /* A software watchpoint cannot cause a signal other than
11108 GDB_SIGNAL_TRAP. */
11109 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11110 return 0;
427cd150 11111
47591c29 11112 return 1;
427cd150
TT
11113}
11114
348d480f
PA
11115/* The breakpoint_ops structure to be used in hardware watchpoints. */
11116
2060206e 11117static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11118
11119/* Implement the "insert" breakpoint_ops method for
11120 masked hardware watchpoints. */
11121
11122static int
11123insert_masked_watchpoint (struct bp_location *bl)
11124{
3a5c3e22
PA
11125 struct watchpoint *w = (struct watchpoint *) bl->owner;
11126
11127 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11128 bl->watchpoint_type);
11129}
11130
11131/* Implement the "remove" breakpoint_ops method for
11132 masked hardware watchpoints. */
11133
11134static int
11135remove_masked_watchpoint (struct bp_location *bl)
11136{
3a5c3e22
PA
11137 struct watchpoint *w = (struct watchpoint *) bl->owner;
11138
11139 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11140 bl->watchpoint_type);
11141}
11142
11143/* Implement the "resources_needed" breakpoint_ops method for
11144 masked hardware watchpoints. */
11145
11146static int
11147resources_needed_masked_watchpoint (const struct bp_location *bl)
11148{
3a5c3e22
PA
11149 struct watchpoint *w = (struct watchpoint *) bl->owner;
11150
11151 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11152}
11153
11154/* Implement the "works_in_software_mode" breakpoint_ops method for
11155 masked hardware watchpoints. */
11156
11157static int
11158works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11159{
11160 return 0;
11161}
11162
11163/* Implement the "print_it" breakpoint_ops method for
11164 masked hardware watchpoints. */
11165
11166static enum print_stop_action
11167print_it_masked_watchpoint (bpstat bs)
11168{
11169 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11170 struct ui_out *uiout = current_uiout;
348d480f
PA
11171
11172 /* Masked watchpoints have only one location. */
11173 gdb_assert (b->loc && b->loc->next == NULL);
11174
11175 switch (b->type)
11176 {
11177 case bp_hardware_watchpoint:
11178 annotate_watchpoint (b->number);
11179 if (ui_out_is_mi_like_p (uiout))
11180 ui_out_field_string
11181 (uiout, "reason",
11182 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11183 break;
11184
11185 case bp_read_watchpoint:
11186 if (ui_out_is_mi_like_p (uiout))
11187 ui_out_field_string
11188 (uiout, "reason",
11189 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11190 break;
11191
11192 case bp_access_watchpoint:
11193 if (ui_out_is_mi_like_p (uiout))
11194 ui_out_field_string
11195 (uiout, "reason",
11196 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11197 break;
11198 default:
11199 internal_error (__FILE__, __LINE__,
11200 _("Invalid hardware watchpoint type."));
11201 }
11202
11203 mention (b);
9c06b0b4
TJB
11204 ui_out_text (uiout, _("\n\
11205Check the underlying instruction at PC for the memory\n\
11206address and value which triggered this watchpoint.\n"));
11207 ui_out_text (uiout, "\n");
11208
11209 /* More than one watchpoint may have been triggered. */
11210 return PRINT_UNKNOWN;
11211}
11212
11213/* Implement the "print_one_detail" breakpoint_ops method for
11214 masked hardware watchpoints. */
11215
11216static void
11217print_one_detail_masked_watchpoint (const struct breakpoint *b,
11218 struct ui_out *uiout)
11219{
3a5c3e22
PA
11220 struct watchpoint *w = (struct watchpoint *) b;
11221
9c06b0b4
TJB
11222 /* Masked watchpoints have only one location. */
11223 gdb_assert (b->loc && b->loc->next == NULL);
11224
11225 ui_out_text (uiout, "\tmask ");
3a5c3e22 11226 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11227 ui_out_text (uiout, "\n");
11228}
11229
11230/* Implement the "print_mention" breakpoint_ops method for
11231 masked hardware watchpoints. */
11232
11233static void
11234print_mention_masked_watchpoint (struct breakpoint *b)
11235{
3a5c3e22 11236 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11237 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11238 struct cleanup *ui_out_chain;
11239
11240 switch (b->type)
11241 {
11242 case bp_hardware_watchpoint:
11243 ui_out_text (uiout, "Masked hardware watchpoint ");
11244 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11245 break;
11246 case bp_read_watchpoint:
11247 ui_out_text (uiout, "Masked hardware read watchpoint ");
11248 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11249 break;
11250 case bp_access_watchpoint:
11251 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11252 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11253 break;
11254 default:
11255 internal_error (__FILE__, __LINE__,
11256 _("Invalid hardware watchpoint type."));
11257 }
11258
11259 ui_out_field_int (uiout, "number", b->number);
11260 ui_out_text (uiout, ": ");
3a5c3e22 11261 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11262 do_cleanups (ui_out_chain);
11263}
11264
11265/* Implement the "print_recreate" breakpoint_ops method for
11266 masked hardware watchpoints. */
11267
11268static void
11269print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11270{
3a5c3e22 11271 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11272 char tmp[40];
11273
11274 switch (b->type)
11275 {
11276 case bp_hardware_watchpoint:
11277 fprintf_unfiltered (fp, "watch");
11278 break;
11279 case bp_read_watchpoint:
11280 fprintf_unfiltered (fp, "rwatch");
11281 break;
11282 case bp_access_watchpoint:
11283 fprintf_unfiltered (fp, "awatch");
11284 break;
11285 default:
11286 internal_error (__FILE__, __LINE__,
11287 _("Invalid hardware watchpoint type."));
11288 }
11289
3a5c3e22
PA
11290 sprintf_vma (tmp, w->hw_wp_mask);
11291 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11292 print_recreate_thread (b, fp);
9c06b0b4
TJB
11293}
11294
11295/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11296
2060206e 11297static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11298
11299/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11300
11301static int
11302is_masked_watchpoint (const struct breakpoint *b)
11303{
11304 return b->ops == &masked_watchpoint_breakpoint_ops;
11305}
11306
53a5351d
JM
11307/* accessflag: hw_write: watch write,
11308 hw_read: watch read,
11309 hw_access: watch access (read or write) */
c906108c 11310static void
bbc13ae3 11311watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11312 int just_location, int internal)
c906108c 11313{
a9634178 11314 volatile struct gdb_exception e;
d983da9c 11315 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11316 struct expression *exp;
270140bd 11317 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11318 struct value *val, *mark, *result;
bb9d5f81 11319 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11320 struct frame_info *frame;
bbc13ae3
KS
11321 const char *exp_start = NULL;
11322 const char *exp_end = NULL;
11323 const char *tok, *end_tok;
9c06b0b4 11324 int toklen = -1;
bbc13ae3
KS
11325 const char *cond_start = NULL;
11326 const char *cond_end = NULL;
c906108c 11327 enum bptype bp_type;
37e4754d 11328 int thread = -1;
0cf6dd15 11329 int pc = 0;
9c06b0b4
TJB
11330 /* Flag to indicate whether we are going to use masks for
11331 the hardware watchpoint. */
11332 int use_mask = 0;
11333 CORE_ADDR mask = 0;
3a5c3e22 11334 struct watchpoint *w;
bbc13ae3
KS
11335 char *expression;
11336 struct cleanup *back_to;
c906108c 11337
37e4754d
LM
11338 /* Make sure that we actually have parameters to parse. */
11339 if (arg != NULL && arg[0] != '\0')
11340 {
bbc13ae3
KS
11341 const char *value_start;
11342
11343 exp_end = arg + strlen (arg);
37e4754d 11344
9c06b0b4
TJB
11345 /* Look for "parameter value" pairs at the end
11346 of the arguments string. */
bbc13ae3 11347 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11348 {
11349 /* Skip whitespace at the end of the argument list. */
11350 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11351 tok--;
11352
11353 /* Find the beginning of the last token.
11354 This is the value of the parameter. */
11355 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11356 tok--;
11357 value_start = tok + 1;
11358
11359 /* Skip whitespace. */
11360 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11361 tok--;
11362
11363 end_tok = tok;
11364
11365 /* Find the beginning of the second to last token.
11366 This is the parameter itself. */
11367 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11368 tok--;
11369 tok++;
11370 toklen = end_tok - tok + 1;
11371
11372 if (toklen == 6 && !strncmp (tok, "thread", 6))
11373 {
11374 /* At this point we've found a "thread" token, which means
11375 the user is trying to set a watchpoint that triggers
11376 only in a specific thread. */
11377 char *endp;
37e4754d 11378
9c06b0b4
TJB
11379 if (thread != -1)
11380 error(_("You can specify only one thread."));
37e4754d 11381
9c06b0b4
TJB
11382 /* Extract the thread ID from the next token. */
11383 thread = strtol (value_start, &endp, 0);
37e4754d 11384
9c06b0b4
TJB
11385 /* Check if the user provided a valid numeric value for the
11386 thread ID. */
11387 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11388 error (_("Invalid thread ID specification %s."), value_start);
11389
11390 /* Check if the thread actually exists. */
11391 if (!valid_thread_id (thread))
af4908ba 11392 invalid_thread_id_error (thread);
9c06b0b4
TJB
11393 }
11394 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11395 {
11396 /* We've found a "mask" token, which means the user wants to
11397 create a hardware watchpoint that is going to have the mask
11398 facility. */
11399 struct value *mask_value, *mark;
37e4754d 11400
9c06b0b4
TJB
11401 if (use_mask)
11402 error(_("You can specify only one mask."));
37e4754d 11403
9c06b0b4 11404 use_mask = just_location = 1;
37e4754d 11405
9c06b0b4
TJB
11406 mark = value_mark ();
11407 mask_value = parse_to_comma_and_eval (&value_start);
11408 mask = value_as_address (mask_value);
11409 value_free_to_mark (mark);
11410 }
11411 else
11412 /* We didn't recognize what we found. We should stop here. */
11413 break;
37e4754d 11414
9c06b0b4
TJB
11415 /* Truncate the string and get rid of the "parameter value" pair before
11416 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11417 exp_end = tok;
9c06b0b4 11418 }
37e4754d 11419 }
bbc13ae3
KS
11420 else
11421 exp_end = arg;
37e4754d 11422
bbc13ae3
KS
11423 /* Parse the rest of the arguments. From here on out, everything
11424 is in terms of a newly allocated string instead of the original
11425 ARG. */
c906108c 11426 innermost_block = NULL;
bbc13ae3
KS
11427 expression = savestring (arg, exp_end - arg);
11428 back_to = make_cleanup (xfree, expression);
11429 exp_start = arg = expression;
1bb9788d 11430 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11431 exp_end = arg;
fa8a61dc
TT
11432 /* Remove trailing whitespace from the expression before saving it.
11433 This makes the eventual display of the expression string a bit
11434 prettier. */
11435 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11436 --exp_end;
11437
65d79d4b
SDJ
11438 /* Checking if the expression is not constant. */
11439 if (watchpoint_exp_is_const (exp))
11440 {
11441 int len;
11442
11443 len = exp_end - exp_start;
11444 while (len > 0 && isspace (exp_start[len - 1]))
11445 len--;
11446 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11447 }
11448
c906108c
SS
11449 exp_valid_block = innermost_block;
11450 mark = value_mark ();
3a1115a0 11451 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11452
bb9d5f81
PP
11453 if (val != NULL && just_location)
11454 {
11455 saved_bitpos = value_bitpos (val);
11456 saved_bitsize = value_bitsize (val);
11457 }
11458
06a64a0b
TT
11459 if (just_location)
11460 {
9c06b0b4
TJB
11461 int ret;
11462
06a64a0b 11463 exp_valid_block = NULL;
a1442452 11464 val = value_addr (result);
06a64a0b
TT
11465 release_value (val);
11466 value_free_to_mark (mark);
9c06b0b4
TJB
11467
11468 if (use_mask)
11469 {
11470 ret = target_masked_watch_num_registers (value_as_address (val),
11471 mask);
11472 if (ret == -1)
11473 error (_("This target does not support masked watchpoints."));
11474 else if (ret == -2)
11475 error (_("Invalid mask or memory region."));
11476 }
06a64a0b
TT
11477 }
11478 else if (val != NULL)
fa4727a6 11479 release_value (val);
c906108c 11480
bbc13ae3
KS
11481 tok = skip_spaces_const (arg);
11482 end_tok = skip_to_space_const (tok);
c906108c
SS
11483
11484 toklen = end_tok - tok;
11485 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11486 {
2d134ed3
PA
11487 struct expression *cond;
11488
60e1c644 11489 innermost_block = NULL;
c906108c 11490 tok = cond_start = end_tok + 1;
1bb9788d 11491 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11492
11493 /* The watchpoint expression may not be local, but the condition
11494 may still be. E.g.: `watch global if local > 0'. */
11495 cond_exp_valid_block = innermost_block;
11496
2d134ed3 11497 xfree (cond);
c906108c
SS
11498 cond_end = tok;
11499 }
11500 if (*tok)
8a3fe4f8 11501 error (_("Junk at end of command."));
c906108c 11502
d983da9c 11503 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11504
11505 /* If the expression is "local", then set up a "watchpoint scope"
11506 breakpoint at the point where we've left the scope of the watchpoint
11507 expression. Create the scope breakpoint before the watchpoint, so
11508 that we will encounter it first in bpstat_stop_status. */
60e1c644 11509 if (exp_valid_block && frame)
d983da9c 11510 {
edb3359d
DJ
11511 if (frame_id_p (frame_unwind_caller_id (frame)))
11512 {
11513 scope_breakpoint
a6d9a66e
UW
11514 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11515 frame_unwind_caller_pc (frame),
06edf0c0
PA
11516 bp_watchpoint_scope,
11517 &momentary_breakpoint_ops);
d983da9c 11518
edb3359d 11519 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11520
edb3359d
DJ
11521 /* Automatically delete the breakpoint when it hits. */
11522 scope_breakpoint->disposition = disp_del;
d983da9c 11523
edb3359d
DJ
11524 /* Only break in the proper frame (help with recursion). */
11525 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11526
edb3359d 11527 /* Set the address at which we will stop. */
a6d9a66e
UW
11528 scope_breakpoint->loc->gdbarch
11529 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11530 scope_breakpoint->loc->requested_address
11531 = frame_unwind_caller_pc (frame);
11532 scope_breakpoint->loc->address
a6d9a66e
UW
11533 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11534 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11535 scope_breakpoint->type);
11536 }
d983da9c
DJ
11537 }
11538
e8369a73
AB
11539 /* Now set up the breakpoint. We create all watchpoints as hardware
11540 watchpoints here even if hardware watchpoints are turned off, a call
11541 to update_watchpoint later in this function will cause the type to
11542 drop back to bp_watchpoint (software watchpoint) if required. */
11543
11544 if (accessflag == hw_read)
11545 bp_type = bp_read_watchpoint;
11546 else if (accessflag == hw_access)
11547 bp_type = bp_access_watchpoint;
11548 else
11549 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11550
11551 w = XCNEW (struct watchpoint);
11552 b = &w->base;
348d480f 11553 if (use_mask)
3a5c3e22
PA
11554 init_raw_breakpoint_without_location (b, NULL, bp_type,
11555 &masked_watchpoint_breakpoint_ops);
348d480f 11556 else
3a5c3e22
PA
11557 init_raw_breakpoint_without_location (b, NULL, bp_type,
11558 &watchpoint_breakpoint_ops);
37e4754d 11559 b->thread = thread;
b5de0fa7 11560 b->disposition = disp_donttouch;
348d480f 11561 b->pspace = current_program_space;
3a5c3e22
PA
11562 w->exp = exp;
11563 w->exp_valid_block = exp_valid_block;
11564 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11565 if (just_location)
11566 {
11567 struct type *t = value_type (val);
11568 CORE_ADDR addr = value_as_address (val);
11569 char *name;
11570
11571 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11572 name = type_to_string (t);
11573
3a5c3e22 11574 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11575 core_addr_to_string (addr));
06a64a0b
TT
11576 xfree (name);
11577
3a5c3e22 11578 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11579 (int) (exp_end - exp_start), exp_start);
11580
06a64a0b
TT
11581 /* The above expression is in C. */
11582 b->language = language_c;
11583 }
11584 else
3a5c3e22 11585 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11586
11587 if (use_mask)
11588 {
3a5c3e22 11589 w->hw_wp_mask = mask;
9c06b0b4
TJB
11590 }
11591 else
11592 {
3a5c3e22 11593 w->val = val;
bb9d5f81
PP
11594 w->val_bitpos = saved_bitpos;
11595 w->val_bitsize = saved_bitsize;
3a5c3e22 11596 w->val_valid = 1;
9c06b0b4 11597 }
77b06cd7 11598
c906108c
SS
11599 if (cond_start)
11600 b->cond_string = savestring (cond_start, cond_end - cond_start);
11601 else
11602 b->cond_string = 0;
c5aa993b 11603
c906108c 11604 if (frame)
f6bc2008 11605 {
3a5c3e22
PA
11606 w->watchpoint_frame = get_frame_id (frame);
11607 w->watchpoint_thread = inferior_ptid;
f6bc2008 11608 }
c906108c 11609 else
f6bc2008 11610 {
3a5c3e22
PA
11611 w->watchpoint_frame = null_frame_id;
11612 w->watchpoint_thread = null_ptid;
f6bc2008 11613 }
c906108c 11614
d983da9c 11615 if (scope_breakpoint != NULL)
c906108c 11616 {
d983da9c
DJ
11617 /* The scope breakpoint is related to the watchpoint. We will
11618 need to act on them together. */
11619 b->related_breakpoint = scope_breakpoint;
11620 scope_breakpoint->related_breakpoint = b;
c906108c 11621 }
d983da9c 11622
06a64a0b
TT
11623 if (!just_location)
11624 value_free_to_mark (mark);
2d134ed3 11625
a9634178
TJB
11626 TRY_CATCH (e, RETURN_MASK_ALL)
11627 {
11628 /* Finally update the new watchpoint. This creates the locations
11629 that should be inserted. */
3a5c3e22 11630 update_watchpoint (w, 1);
a9634178
TJB
11631 }
11632 if (e.reason < 0)
11633 {
11634 delete_breakpoint (b);
11635 throw_exception (e);
11636 }
11637
3ea46bff 11638 install_breakpoint (internal, b, 1);
bbc13ae3 11639 do_cleanups (back_to);
c906108c
SS
11640}
11641
e09342b5 11642/* Return count of debug registers needed to watch the given expression.
e09342b5 11643 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11644
c906108c 11645static int
a9634178 11646can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11647{
11648 int found_memory_cnt = 0;
2e70b7b9 11649 struct value *head = v;
c906108c
SS
11650
11651 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11652 if (!can_use_hw_watchpoints)
c906108c 11653 return 0;
c5aa993b 11654
5c44784c
JM
11655 /* Make sure that the value of the expression depends only upon
11656 memory contents, and values computed from them within GDB. If we
11657 find any register references or function calls, we can't use a
11658 hardware watchpoint.
11659
11660 The idea here is that evaluating an expression generates a series
11661 of values, one holding the value of every subexpression. (The
11662 expression a*b+c has five subexpressions: a, b, a*b, c, and
11663 a*b+c.) GDB's values hold almost enough information to establish
11664 the criteria given above --- they identify memory lvalues,
11665 register lvalues, computed values, etcetera. So we can evaluate
11666 the expression, and then scan the chain of values that leaves
11667 behind to decide whether we can detect any possible change to the
11668 expression's final value using only hardware watchpoints.
11669
11670 However, I don't think that the values returned by inferior
11671 function calls are special in any way. So this function may not
11672 notice that an expression involving an inferior function call
11673 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11674 for (; v; v = value_next (v))
c906108c 11675 {
5c44784c 11676 if (VALUE_LVAL (v) == lval_memory)
c906108c 11677 {
8464be76
DJ
11678 if (v != head && value_lazy (v))
11679 /* A lazy memory lvalue in the chain is one that GDB never
11680 needed to fetch; we either just used its address (e.g.,
11681 `a' in `a.b') or we never needed it at all (e.g., `a'
11682 in `a,b'). This doesn't apply to HEAD; if that is
11683 lazy then it was not readable, but watch it anyway. */
5c44784c 11684 ;
53a5351d 11685 else
5c44784c
JM
11686 {
11687 /* Ahh, memory we actually used! Check if we can cover
11688 it with hardware watchpoints. */
df407dfe 11689 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11690
11691 /* We only watch structs and arrays if user asked for it
11692 explicitly, never if they just happen to appear in a
11693 middle of some value chain. */
11694 if (v == head
11695 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11696 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11697 {
42ae5230 11698 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11699 int len;
11700 int num_regs;
11701
a9634178 11702 len = (target_exact_watchpoints
e09342b5
TJB
11703 && is_scalar_type_recursive (vtype))?
11704 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11705
e09342b5
TJB
11706 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11707 if (!num_regs)
2e70b7b9
MS
11708 return 0;
11709 else
e09342b5 11710 found_memory_cnt += num_regs;
2e70b7b9 11711 }
5c44784c 11712 }
c5aa993b 11713 }
5086187c
AC
11714 else if (VALUE_LVAL (v) != not_lval
11715 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11716 return 0; /* These are values from the history (e.g., $1). */
5086187c 11717 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11718 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11719 }
11720
11721 /* The expression itself looks suitable for using a hardware
11722 watchpoint, but give the target machine a chance to reject it. */
11723 return found_memory_cnt;
11724}
11725
8b93c638 11726void
84f4c1fe 11727watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11728{
84f4c1fe 11729 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11730}
11731
06a64a0b
TT
11732/* A helper function that looks for the "-location" argument and then
11733 calls watch_command_1. */
11734
11735static void
11736watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11737{
11738 int just_location = 0;
11739
11740 if (arg
11741 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11742 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11743 {
e9cafbcc 11744 arg = skip_spaces (arg);
06a64a0b
TT
11745 just_location = 1;
11746 }
11747
84f4c1fe 11748 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11749}
8926118c 11750
c5aa993b 11751static void
fba45db2 11752watch_command (char *arg, int from_tty)
c906108c 11753{
06a64a0b 11754 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11755}
11756
8b93c638 11757void
84f4c1fe 11758rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11759{
84f4c1fe 11760 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11761}
8926118c 11762
c5aa993b 11763static void
fba45db2 11764rwatch_command (char *arg, int from_tty)
c906108c 11765{
06a64a0b 11766 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11767}
11768
8b93c638 11769void
84f4c1fe 11770awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11771{
84f4c1fe 11772 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11773}
8926118c 11774
c5aa993b 11775static void
fba45db2 11776awatch_command (char *arg, int from_tty)
c906108c 11777{
06a64a0b 11778 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11779}
c906108c 11780\f
c5aa993b 11781
43ff13b4 11782/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11783 because it uses the mechanisms of breakpoints. */
11784
bfec99b2
PA
11785struct until_break_command_continuation_args
11786{
11787 struct breakpoint *breakpoint;
11788 struct breakpoint *breakpoint2;
186c406b 11789 int thread_num;
bfec99b2
PA
11790};
11791
43ff13b4 11792/* This function is called by fetch_inferior_event via the
4a64f543 11793 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11794 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11795 command. */
c2c6d25f 11796static void
fa4cd53f 11797until_break_command_continuation (void *arg, int err)
43ff13b4 11798{
bfec99b2
PA
11799 struct until_break_command_continuation_args *a = arg;
11800
11801 delete_breakpoint (a->breakpoint);
11802 if (a->breakpoint2)
11803 delete_breakpoint (a->breakpoint2);
186c406b 11804 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11805}
11806
c906108c 11807void
ae66c1fc 11808until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11809{
11810 struct symtabs_and_lines sals;
11811 struct symtab_and_line sal;
8556afb4
PA
11812 struct frame_info *frame;
11813 struct gdbarch *frame_gdbarch;
11814 struct frame_id stack_frame_id;
11815 struct frame_id caller_frame_id;
c906108c 11816 struct breakpoint *breakpoint;
f107f563 11817 struct breakpoint *breakpoint2 = NULL;
c906108c 11818 struct cleanup *old_chain;
186c406b
TT
11819 int thread;
11820 struct thread_info *tp;
c906108c 11821
70509625 11822 clear_proceed_status (0);
c906108c
SS
11823
11824 /* Set a breakpoint where the user wants it and at return from
4a64f543 11825 this function. */
c5aa993b 11826
1bfeeb0f 11827 if (last_displayed_sal_is_valid ())
f8eba3c6 11828 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11829 get_last_displayed_symtab (),
f8eba3c6 11830 get_last_displayed_line ());
c906108c 11831 else
f8eba3c6
TT
11832 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11833 (struct symtab *) NULL, 0);
c5aa993b 11834
c906108c 11835 if (sals.nelts != 1)
8a3fe4f8 11836 error (_("Couldn't get information on specified line."));
c5aa993b 11837
c906108c 11838 sal = sals.sals[0];
4a64f543 11839 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11840
c906108c 11841 if (*arg)
8a3fe4f8 11842 error (_("Junk at end of arguments."));
c5aa993b 11843
c906108c 11844 resolve_sal_pc (&sal);
c5aa993b 11845
186c406b
TT
11846 tp = inferior_thread ();
11847 thread = tp->num;
11848
883bc8d1
PA
11849 old_chain = make_cleanup (null_cleanup, NULL);
11850
8556afb4
PA
11851 /* Note linespec handling above invalidates the frame chain.
11852 Installing a breakpoint also invalidates the frame chain (as it
11853 may need to switch threads), so do any frame handling before
11854 that. */
11855
11856 frame = get_selected_frame (NULL);
11857 frame_gdbarch = get_frame_arch (frame);
11858 stack_frame_id = get_stack_frame_id (frame);
11859 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11860
ae66c1fc
EZ
11861 /* Keep within the current frame, or in frames called by the current
11862 one. */
edb3359d 11863
883bc8d1 11864 if (frame_id_p (caller_frame_id))
c906108c 11865 {
883bc8d1
PA
11866 struct symtab_and_line sal2;
11867
11868 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11869 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11870 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11871 sal2,
11872 caller_frame_id,
f107f563
VP
11873 bp_until);
11874 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11875
883bc8d1 11876 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11877 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11878 }
c5aa993b 11879
c70a6932
JK
11880 /* set_momentary_breakpoint could invalidate FRAME. */
11881 frame = NULL;
11882
883bc8d1
PA
11883 if (anywhere)
11884 /* If the user told us to continue until a specified location,
11885 we don't specify a frame at which we need to stop. */
11886 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11887 null_frame_id, bp_until);
11888 else
11889 /* Otherwise, specify the selected frame, because we want to stop
11890 only at the very same frame. */
11891 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11892 stack_frame_id, bp_until);
11893 make_cleanup_delete_breakpoint (breakpoint);
11894
a493e3e2 11895 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11896
4a64f543
MS
11897 /* If we are running asynchronously, and proceed call above has
11898 actually managed to start the target, arrange for breakpoints to
11899 be deleted when the target stops. Otherwise, we're already
11900 stopped and delete breakpoints via cleanup chain. */
f107f563 11901
8ea051c5 11902 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11903 {
bfec99b2
PA
11904 struct until_break_command_continuation_args *args;
11905 args = xmalloc (sizeof (*args));
f107f563 11906
bfec99b2
PA
11907 args->breakpoint = breakpoint;
11908 args->breakpoint2 = breakpoint2;
186c406b 11909 args->thread_num = thread;
f107f563
VP
11910
11911 discard_cleanups (old_chain);
95e54da7
PA
11912 add_continuation (inferior_thread (),
11913 until_break_command_continuation, args,
604ead4a 11914 xfree);
f107f563
VP
11915 }
11916 else
c5aa993b 11917 do_cleanups (old_chain);
c906108c 11918}
ae66c1fc 11919
c906108c
SS
11920/* This function attempts to parse an optional "if <cond>" clause
11921 from the arg string. If one is not found, it returns NULL.
c5aa993b 11922
c906108c
SS
11923 Else, it returns a pointer to the condition string. (It does not
11924 attempt to evaluate the string against a particular block.) And,
11925 it updates arg to point to the first character following the parsed
4a64f543 11926 if clause in the arg string. */
53a5351d 11927
916703c0 11928char *
fba45db2 11929ep_parse_optional_if_clause (char **arg)
c906108c 11930{
c5aa993b
JM
11931 char *cond_string;
11932
11933 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11934 return NULL;
c5aa993b 11935
4a64f543 11936 /* Skip the "if" keyword. */
c906108c 11937 (*arg) += 2;
c5aa993b 11938
c906108c 11939 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11940 condition string. */
e9cafbcc 11941 *arg = skip_spaces (*arg);
c906108c 11942 cond_string = *arg;
c5aa993b 11943
4a64f543
MS
11944 /* Assume that the condition occupies the remainder of the arg
11945 string. */
c906108c 11946 (*arg) += strlen (cond_string);
c5aa993b 11947
c906108c
SS
11948 return cond_string;
11949}
c5aa993b 11950
c906108c
SS
11951/* Commands to deal with catching events, such as signals, exceptions,
11952 process start/exit, etc. */
c5aa993b
JM
11953
11954typedef enum
11955{
44feb3ce
TT
11956 catch_fork_temporary, catch_vfork_temporary,
11957 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11958}
11959catch_fork_kind;
11960
c906108c 11961static void
cc59ec59
MS
11962catch_fork_command_1 (char *arg, int from_tty,
11963 struct cmd_list_element *command)
c906108c 11964{
a6d9a66e 11965 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11966 char *cond_string = NULL;
44feb3ce
TT
11967 catch_fork_kind fork_kind;
11968 int tempflag;
11969
11970 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11971 tempflag = (fork_kind == catch_fork_temporary
11972 || fork_kind == catch_vfork_temporary);
c5aa993b 11973
44feb3ce
TT
11974 if (!arg)
11975 arg = "";
e9cafbcc 11976 arg = skip_spaces (arg);
c5aa993b 11977
c906108c 11978 /* The allowed syntax is:
c5aa993b
JM
11979 catch [v]fork
11980 catch [v]fork if <cond>
11981
4a64f543 11982 First, check if there's an if clause. */
c906108c 11983 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11984
c906108c 11985 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11986 error (_("Junk at end of arguments."));
c5aa993b 11987
c906108c 11988 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11989 and enable reporting of such events. */
c5aa993b
JM
11990 switch (fork_kind)
11991 {
44feb3ce
TT
11992 case catch_fork_temporary:
11993 case catch_fork_permanent:
a6d9a66e 11994 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11995 &catch_fork_breakpoint_ops);
c906108c 11996 break;
44feb3ce
TT
11997 case catch_vfork_temporary:
11998 case catch_vfork_permanent:
a6d9a66e 11999 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12000 &catch_vfork_breakpoint_ops);
c906108c 12001 break;
c5aa993b 12002 default:
8a3fe4f8 12003 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12004 break;
c5aa993b 12005 }
c906108c
SS
12006}
12007
12008static void
cc59ec59
MS
12009catch_exec_command_1 (char *arg, int from_tty,
12010 struct cmd_list_element *command)
c906108c 12011{
b4d90040 12012 struct exec_catchpoint *c;
a6d9a66e 12013 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12014 int tempflag;
c5aa993b 12015 char *cond_string = NULL;
c906108c 12016
44feb3ce
TT
12017 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12018
12019 if (!arg)
12020 arg = "";
e9cafbcc 12021 arg = skip_spaces (arg);
c906108c
SS
12022
12023 /* The allowed syntax is:
c5aa993b
JM
12024 catch exec
12025 catch exec if <cond>
c906108c 12026
4a64f543 12027 First, check if there's an if clause. */
c906108c
SS
12028 cond_string = ep_parse_optional_if_clause (&arg);
12029
12030 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12031 error (_("Junk at end of arguments."));
c906108c 12032
b4d90040
PA
12033 c = XNEW (struct exec_catchpoint);
12034 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12035 &catch_exec_breakpoint_ops);
12036 c->exec_pathname = NULL;
12037
3ea46bff 12038 install_breakpoint (0, &c->base, 1);
c906108c 12039}
c5aa993b 12040
9ac4176b 12041void
28010a5d
PA
12042init_ada_exception_breakpoint (struct breakpoint *b,
12043 struct gdbarch *gdbarch,
12044 struct symtab_and_line sal,
12045 char *addr_string,
c0a91b2b 12046 const struct breakpoint_ops *ops,
28010a5d 12047 int tempflag,
349774ef 12048 int enabled,
28010a5d 12049 int from_tty)
f7f9143b 12050{
f7f9143b
JB
12051 if (from_tty)
12052 {
5af949e3
UW
12053 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12054 if (!loc_gdbarch)
12055 loc_gdbarch = gdbarch;
12056
6c95b8df
PA
12057 describe_other_breakpoints (loc_gdbarch,
12058 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12059 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12060 version for exception catchpoints, because two catchpoints
12061 used for different exception names will use the same address.
12062 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12063 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12064 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12065 the user what type of catchpoint it is. The above is good
12066 enough for now, though. */
12067 }
12068
28010a5d 12069 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12070
349774ef 12071 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12072 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12073 b->addr_string = addr_string;
12074 b->language = language_ada;
f7f9143b
JB
12075}
12076
a96d9b2e
SDJ
12077/* Splits the argument using space as delimiter. Returns an xmalloc'd
12078 filter list, or NULL if no filtering is required. */
12079static VEC(int) *
12080catch_syscall_split_args (char *arg)
12081{
12082 VEC(int) *result = NULL;
29d0bb3d 12083 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
458c8db8 12084 struct gdbarch *gdbarch = target_gdbarch ();
a96d9b2e
SDJ
12085
12086 while (*arg != '\0')
12087 {
12088 int i, syscall_number;
12089 char *endptr;
12090 char cur_name[128];
12091 struct syscall s;
12092
12093 /* Skip whitespace. */
529480d0 12094 arg = skip_spaces (arg);
a96d9b2e
SDJ
12095
12096 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12097 cur_name[i] = arg[i];
12098 cur_name[i] = '\0';
12099 arg += i;
12100
12101 /* Check if the user provided a syscall name or a number. */
12102 syscall_number = (int) strtol (cur_name, &endptr, 0);
12103 if (*endptr == '\0')
458c8db8 12104 get_syscall_by_number (gdbarch, syscall_number, &s);
a96d9b2e
SDJ
12105 else
12106 {
12107 /* We have a name. Let's check if it's valid and convert it
12108 to a number. */
458c8db8 12109 get_syscall_by_name (gdbarch, cur_name, &s);
a96d9b2e
SDJ
12110
12111 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12112 /* Here we have to issue an error instead of a warning,
12113 because GDB cannot do anything useful if there's no
12114 syscall number to be caught. */
a96d9b2e
SDJ
12115 error (_("Unknown syscall name '%s'."), cur_name);
12116 }
12117
12118 /* Ok, it's valid. */
12119 VEC_safe_push (int, result, s.number);
12120 }
12121
12122 discard_cleanups (cleanup);
12123 return result;
12124}
12125
12126/* Implement the "catch syscall" command. */
12127
12128static void
cc59ec59
MS
12129catch_syscall_command_1 (char *arg, int from_tty,
12130 struct cmd_list_element *command)
a96d9b2e
SDJ
12131{
12132 int tempflag;
12133 VEC(int) *filter;
12134 struct syscall s;
12135 struct gdbarch *gdbarch = get_current_arch ();
12136
12137 /* Checking if the feature if supported. */
12138 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12139 error (_("The feature 'catch syscall' is not supported on \
ea666128 12140this architecture yet."));
a96d9b2e
SDJ
12141
12142 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12143
e9cafbcc 12144 arg = skip_spaces (arg);
a96d9b2e
SDJ
12145
12146 /* We need to do this first "dummy" translation in order
12147 to get the syscall XML file loaded or, most important,
12148 to display a warning to the user if there's no XML file
12149 for his/her architecture. */
458c8db8 12150 get_syscall_by_number (gdbarch, 0, &s);
a96d9b2e
SDJ
12151
12152 /* The allowed syntax is:
12153 catch syscall
12154 catch syscall <name | number> [<name | number> ... <name | number>]
12155
12156 Let's check if there's a syscall name. */
12157
12158 if (arg != NULL)
12159 filter = catch_syscall_split_args (arg);
12160 else
12161 filter = NULL;
12162
12163 create_syscall_event_catchpoint (tempflag, filter,
12164 &catch_syscall_breakpoint_ops);
12165}
12166
c906108c 12167static void
fba45db2 12168catch_command (char *arg, int from_tty)
c906108c 12169{
44feb3ce 12170 error (_("Catch requires an event name."));
c906108c
SS
12171}
12172\f
12173
12174static void
fba45db2 12175tcatch_command (char *arg, int from_tty)
c906108c 12176{
44feb3ce 12177 error (_("Catch requires an event name."));
c906108c
SS
12178}
12179
8a2c437b
TT
12180/* A qsort comparison function that sorts breakpoints in order. */
12181
12182static int
12183compare_breakpoints (const void *a, const void *b)
12184{
12185 const breakpoint_p *ba = a;
12186 uintptr_t ua = (uintptr_t) *ba;
12187 const breakpoint_p *bb = b;
12188 uintptr_t ub = (uintptr_t) *bb;
12189
12190 if ((*ba)->number < (*bb)->number)
12191 return -1;
12192 else if ((*ba)->number > (*bb)->number)
12193 return 1;
12194
12195 /* Now sort by address, in case we see, e..g, two breakpoints with
12196 the number 0. */
12197 if (ua < ub)
12198 return -1;
94b0e70d 12199 return ua > ub ? 1 : 0;
8a2c437b
TT
12200}
12201
80f8a6eb 12202/* Delete breakpoints by address or line. */
c906108c
SS
12203
12204static void
fba45db2 12205clear_command (char *arg, int from_tty)
c906108c 12206{
8a2c437b 12207 struct breakpoint *b, *prev;
d6e956e5
VP
12208 VEC(breakpoint_p) *found = 0;
12209 int ix;
c906108c
SS
12210 int default_match;
12211 struct symtabs_and_lines sals;
12212 struct symtab_and_line sal;
c906108c 12213 int i;
8a2c437b 12214 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12215
12216 if (arg)
12217 {
39cf75f7
DE
12218 sals = decode_line_with_current_source (arg,
12219 (DECODE_LINE_FUNFIRSTLINE
12220 | DECODE_LINE_LIST_MODE));
cf4ded82 12221 make_cleanup (xfree, sals.sals);
c906108c
SS
12222 default_match = 0;
12223 }
12224 else
12225 {
c5aa993b 12226 sals.sals = (struct symtab_and_line *)
c906108c 12227 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12228 make_cleanup (xfree, sals.sals);
4a64f543 12229 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12230
12231 /* Set sal's line, symtab, pc, and pspace to the values
12232 corresponding to the last call to print_frame_info. If the
12233 codepoint is not valid, this will set all the fields to 0. */
12234 get_last_displayed_sal (&sal);
c906108c 12235 if (sal.symtab == 0)
8a3fe4f8 12236 error (_("No source file specified."));
c906108c
SS
12237
12238 sals.sals[0] = sal;
12239 sals.nelts = 1;
12240
12241 default_match = 1;
12242 }
12243
4a64f543
MS
12244 /* We don't call resolve_sal_pc here. That's not as bad as it
12245 seems, because all existing breakpoints typically have both
12246 file/line and pc set. So, if clear is given file/line, we can
12247 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12248
12249 We only support clearing given the address explicitly
12250 present in breakpoint table. Say, we've set breakpoint
4a64f543 12251 at file:line. There were several PC values for that file:line,
ed0616c6 12252 due to optimization, all in one block.
4a64f543
MS
12253
12254 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12255 PC corresponding to the same file:line, the breakpoint won't
12256 be cleared. We probably can still clear the breakpoint, but
12257 since the other PC value is never presented to user, user
12258 can only find it by guessing, and it does not seem important
12259 to support that. */
12260
4a64f543
MS
12261 /* For each line spec given, delete bps which correspond to it. Do
12262 it in two passes, solely to preserve the current behavior that
12263 from_tty is forced true if we delete more than one
12264 breakpoint. */
c906108c 12265
80f8a6eb 12266 found = NULL;
8a2c437b 12267 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12268 for (i = 0; i < sals.nelts; i++)
12269 {
05cba821
JK
12270 const char *sal_fullname;
12271
c906108c 12272 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12273 If line given (pc == 0), clear all bpts on specified line.
12274 If defaulting, clear all bpts on default line
c906108c 12275 or at default pc.
c5aa993b
JM
12276
12277 defaulting sal.pc != 0 tests to do
12278
12279 0 1 pc
12280 1 1 pc _and_ line
12281 0 0 line
12282 1 0 <can't happen> */
c906108c
SS
12283
12284 sal = sals.sals[i];
05cba821
JK
12285 sal_fullname = (sal.symtab == NULL
12286 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12287
4a64f543 12288 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12289 ALL_BREAKPOINTS (b)
c5aa993b 12290 {
0d381245 12291 int match = 0;
4a64f543 12292 /* Are we going to delete b? */
cc60f2e3 12293 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12294 {
12295 struct bp_location *loc = b->loc;
12296 for (; loc; loc = loc->next)
12297 {
f8eba3c6
TT
12298 /* If the user specified file:line, don't allow a PC
12299 match. This matches historical gdb behavior. */
12300 int pc_match = (!sal.explicit_line
12301 && sal.pc
12302 && (loc->pspace == sal.pspace)
12303 && (loc->address == sal.pc)
12304 && (!section_is_overlay (loc->section)
12305 || loc->section == sal.section));
4aac40c8
TT
12306 int line_match = 0;
12307
12308 if ((default_match || sal.explicit_line)
2f202fde 12309 && loc->symtab != NULL
05cba821 12310 && sal_fullname != NULL
4aac40c8 12311 && sal.pspace == loc->pspace
05cba821
JK
12312 && loc->line_number == sal.line
12313 && filename_cmp (symtab_to_fullname (loc->symtab),
12314 sal_fullname) == 0)
12315 line_match = 1;
4aac40c8 12316
0d381245
VP
12317 if (pc_match || line_match)
12318 {
12319 match = 1;
12320 break;
12321 }
12322 }
12323 }
12324
12325 if (match)
d6e956e5 12326 VEC_safe_push(breakpoint_p, found, b);
c906108c 12327 }
80f8a6eb 12328 }
8a2c437b 12329
80f8a6eb 12330 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12331 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12332 {
12333 if (arg)
8a3fe4f8 12334 error (_("No breakpoint at %s."), arg);
80f8a6eb 12335 else
8a3fe4f8 12336 error (_("No breakpoint at this line."));
80f8a6eb 12337 }
c906108c 12338
8a2c437b
TT
12339 /* Remove duplicates from the vec. */
12340 qsort (VEC_address (breakpoint_p, found),
12341 VEC_length (breakpoint_p, found),
12342 sizeof (breakpoint_p),
12343 compare_breakpoints);
12344 prev = VEC_index (breakpoint_p, found, 0);
12345 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12346 {
12347 if (b == prev)
12348 {
12349 VEC_ordered_remove (breakpoint_p, found, ix);
12350 --ix;
12351 }
12352 }
12353
d6e956e5 12354 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12355 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12356 if (from_tty)
a3f17187 12357 {
d6e956e5 12358 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12359 printf_unfiltered (_("Deleted breakpoint "));
12360 else
12361 printf_unfiltered (_("Deleted breakpoints "));
12362 }
d6e956e5
VP
12363
12364 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12365 {
c5aa993b 12366 if (from_tty)
d6e956e5
VP
12367 printf_unfiltered ("%d ", b->number);
12368 delete_breakpoint (b);
c906108c 12369 }
80f8a6eb
MS
12370 if (from_tty)
12371 putchar_unfiltered ('\n');
8a2c437b
TT
12372
12373 do_cleanups (cleanups);
c906108c
SS
12374}
12375\f
12376/* Delete breakpoint in BS if they are `delete' breakpoints and
12377 all breakpoints that are marked for deletion, whether hit or not.
12378 This is called after any breakpoint is hit, or after errors. */
12379
12380void
fba45db2 12381breakpoint_auto_delete (bpstat bs)
c906108c 12382{
35df4500 12383 struct breakpoint *b, *b_tmp;
c906108c
SS
12384
12385 for (; bs; bs = bs->next)
f431efe5
PA
12386 if (bs->breakpoint_at
12387 && bs->breakpoint_at->disposition == disp_del
c906108c 12388 && bs->stop)
f431efe5 12389 delete_breakpoint (bs->breakpoint_at);
c906108c 12390
35df4500 12391 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12392 {
b5de0fa7 12393 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12394 delete_breakpoint (b);
12395 }
c906108c
SS
12396}
12397
4a64f543
MS
12398/* A comparison function for bp_location AP and BP being interfaced to
12399 qsort. Sort elements primarily by their ADDRESS (no matter what
12400 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12401 secondarily by ordering first permanent elements and
4a64f543 12402 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12403 qsort being an unstable algorithm. */
876fa593
JK
12404
12405static int
494cfb0f 12406bp_location_compare (const void *ap, const void *bp)
876fa593 12407{
494cfb0f
JK
12408 struct bp_location *a = *(void **) ap;
12409 struct bp_location *b = *(void **) bp;
876fa593
JK
12410
12411 if (a->address != b->address)
12412 return (a->address > b->address) - (a->address < b->address);
12413
dea2aa5f
LM
12414 /* Sort locations at the same address by their pspace number, keeping
12415 locations of the same inferior (in a multi-inferior environment)
12416 grouped. */
12417
12418 if (a->pspace->num != b->pspace->num)
12419 return ((a->pspace->num > b->pspace->num)
12420 - (a->pspace->num < b->pspace->num));
12421
876fa593 12422 /* Sort permanent breakpoints first. */
1a853c52
PA
12423 if (a->permanent != b->permanent)
12424 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12425
c56a97f9
JK
12426 /* Make the internal GDB representation stable across GDB runs
12427 where A and B memory inside GDB can differ. Breakpoint locations of
12428 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12429
12430 if (a->owner->number != b->owner->number)
c56a97f9
JK
12431 return ((a->owner->number > b->owner->number)
12432 - (a->owner->number < b->owner->number));
876fa593
JK
12433
12434 return (a > b) - (a < b);
12435}
12436
876fa593 12437/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12438 bp_location_shadow_len_after_address_max according to the current
12439 content of the bp_location array. */
f7545552
TT
12440
12441static void
876fa593 12442bp_location_target_extensions_update (void)
f7545552 12443{
876fa593
JK
12444 struct bp_location *bl, **blp_tmp;
12445
12446 bp_location_placed_address_before_address_max = 0;
12447 bp_location_shadow_len_after_address_max = 0;
12448
12449 ALL_BP_LOCATIONS (bl, blp_tmp)
12450 {
12451 CORE_ADDR start, end, addr;
12452
12453 if (!bp_location_has_shadow (bl))
12454 continue;
12455
12456 start = bl->target_info.placed_address;
12457 end = start + bl->target_info.shadow_len;
12458
12459 gdb_assert (bl->address >= start);
12460 addr = bl->address - start;
12461 if (addr > bp_location_placed_address_before_address_max)
12462 bp_location_placed_address_before_address_max = addr;
12463
12464 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12465
12466 gdb_assert (bl->address < end);
12467 addr = end - bl->address;
12468 if (addr > bp_location_shadow_len_after_address_max)
12469 bp_location_shadow_len_after_address_max = addr;
12470 }
f7545552
TT
12471}
12472
1e4d1764
YQ
12473/* Download tracepoint locations if they haven't been. */
12474
12475static void
12476download_tracepoint_locations (void)
12477{
7ed2c994 12478 struct breakpoint *b;
1e4d1764
YQ
12479 struct cleanup *old_chain;
12480
12481 if (!target_can_download_tracepoint ())
12482 return;
12483
12484 old_chain = save_current_space_and_thread ();
12485
7ed2c994 12486 ALL_TRACEPOINTS (b)
1e4d1764 12487 {
7ed2c994 12488 struct bp_location *bl;
1e4d1764 12489 struct tracepoint *t;
f2a8bc8a 12490 int bp_location_downloaded = 0;
1e4d1764 12491
7ed2c994 12492 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12493 ? !may_insert_fast_tracepoints
12494 : !may_insert_tracepoints))
12495 continue;
12496
7ed2c994
YQ
12497 for (bl = b->loc; bl; bl = bl->next)
12498 {
12499 /* In tracepoint, locations are _never_ duplicated, so
12500 should_be_inserted is equivalent to
12501 unduplicated_should_be_inserted. */
12502 if (!should_be_inserted (bl) || bl->inserted)
12503 continue;
1e4d1764 12504
7ed2c994 12505 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12506
7ed2c994 12507 target_download_tracepoint (bl);
1e4d1764 12508
7ed2c994 12509 bl->inserted = 1;
f2a8bc8a 12510 bp_location_downloaded = 1;
7ed2c994
YQ
12511 }
12512 t = (struct tracepoint *) b;
12513 t->number_on_target = b->number;
f2a8bc8a
YQ
12514 if (bp_location_downloaded)
12515 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12516 }
12517
12518 do_cleanups (old_chain);
12519}
12520
934709f0
PW
12521/* Swap the insertion/duplication state between two locations. */
12522
12523static void
12524swap_insertion (struct bp_location *left, struct bp_location *right)
12525{
12526 const int left_inserted = left->inserted;
12527 const int left_duplicate = left->duplicate;
b775012e 12528 const int left_needs_update = left->needs_update;
934709f0
PW
12529 const struct bp_target_info left_target_info = left->target_info;
12530
1e4d1764
YQ
12531 /* Locations of tracepoints can never be duplicated. */
12532 if (is_tracepoint (left->owner))
12533 gdb_assert (!left->duplicate);
12534 if (is_tracepoint (right->owner))
12535 gdb_assert (!right->duplicate);
12536
934709f0
PW
12537 left->inserted = right->inserted;
12538 left->duplicate = right->duplicate;
b775012e 12539 left->needs_update = right->needs_update;
934709f0
PW
12540 left->target_info = right->target_info;
12541 right->inserted = left_inserted;
12542 right->duplicate = left_duplicate;
b775012e 12543 right->needs_update = left_needs_update;
934709f0
PW
12544 right->target_info = left_target_info;
12545}
12546
b775012e
LM
12547/* Force the re-insertion of the locations at ADDRESS. This is called
12548 once a new/deleted/modified duplicate location is found and we are evaluating
12549 conditions on the target's side. Such conditions need to be updated on
12550 the target. */
12551
12552static void
12553force_breakpoint_reinsertion (struct bp_location *bl)
12554{
12555 struct bp_location **locp = NULL, **loc2p;
12556 struct bp_location *loc;
12557 CORE_ADDR address = 0;
12558 int pspace_num;
12559
12560 address = bl->address;
12561 pspace_num = bl->pspace->num;
12562
12563 /* This is only meaningful if the target is
12564 evaluating conditions and if the user has
12565 opted for condition evaluation on the target's
12566 side. */
12567 if (gdb_evaluates_breakpoint_condition_p ()
12568 || !target_supports_evaluation_of_breakpoint_conditions ())
12569 return;
12570
12571 /* Flag all breakpoint locations with this address and
12572 the same program space as the location
12573 as "its condition has changed". We need to
12574 update the conditions on the target's side. */
12575 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12576 {
12577 loc = *loc2p;
12578
12579 if (!is_breakpoint (loc->owner)
12580 || pspace_num != loc->pspace->num)
12581 continue;
12582
12583 /* Flag the location appropriately. We use a different state to
12584 let everyone know that we already updated the set of locations
12585 with addr bl->address and program space bl->pspace. This is so
12586 we don't have to keep calling these functions just to mark locations
12587 that have already been marked. */
12588 loc->condition_changed = condition_updated;
12589
12590 /* Free the agent expression bytecode as well. We will compute
12591 it later on. */
12592 if (loc->cond_bytecode)
12593 {
12594 free_agent_expr (loc->cond_bytecode);
12595 loc->cond_bytecode = NULL;
12596 }
12597 }
12598}
44702360
PA
12599/* Called whether new breakpoints are created, or existing breakpoints
12600 deleted, to update the global location list and recompute which
12601 locations are duplicate of which.
b775012e 12602
04086b45
PA
12603 The INSERT_MODE flag determines whether locations may not, may, or
12604 shall be inserted now. See 'enum ugll_insert_mode' for more
12605 info. */
b60e7edf 12606
0d381245 12607static void
44702360 12608update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12609{
74960c60 12610 struct breakpoint *b;
876fa593 12611 struct bp_location **locp, *loc;
f7545552 12612 struct cleanup *cleanups;
b775012e
LM
12613 /* Last breakpoint location address that was marked for update. */
12614 CORE_ADDR last_addr = 0;
12615 /* Last breakpoint location program space that was marked for update. */
12616 int last_pspace_num = -1;
f7545552 12617
2d134ed3
PA
12618 /* Used in the duplicates detection below. When iterating over all
12619 bp_locations, points to the first bp_location of a given address.
12620 Breakpoints and watchpoints of different types are never
12621 duplicates of each other. Keep one pointer for each type of
12622 breakpoint/watchpoint, so we only need to loop over all locations
12623 once. */
12624 struct bp_location *bp_loc_first; /* breakpoint */
12625 struct bp_location *wp_loc_first; /* hardware watchpoint */
12626 struct bp_location *awp_loc_first; /* access watchpoint */
12627 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12628
4a64f543
MS
12629 /* Saved former bp_location array which we compare against the newly
12630 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12631 struct bp_location **old_location, **old_locp;
12632 unsigned old_location_count;
12633
12634 old_location = bp_location;
12635 old_location_count = bp_location_count;
12636 bp_location = NULL;
12637 bp_location_count = 0;
12638 cleanups = make_cleanup (xfree, old_location);
0d381245 12639
74960c60 12640 ALL_BREAKPOINTS (b)
876fa593
JK
12641 for (loc = b->loc; loc; loc = loc->next)
12642 bp_location_count++;
12643
12644 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12645 locp = bp_location;
12646 ALL_BREAKPOINTS (b)
12647 for (loc = b->loc; loc; loc = loc->next)
12648 *locp++ = loc;
12649 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12650 bp_location_compare);
876fa593
JK
12651
12652 bp_location_target_extensions_update ();
74960c60 12653
4a64f543
MS
12654 /* Identify bp_location instances that are no longer present in the
12655 new list, and therefore should be freed. Note that it's not
12656 necessary that those locations should be removed from inferior --
12657 if there's another location at the same address (previously
12658 marked as duplicate), we don't need to remove/insert the
12659 location.
876fa593 12660
4a64f543
MS
12661 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12662 and former bp_location array state respectively. */
876fa593
JK
12663
12664 locp = bp_location;
12665 for (old_locp = old_location; old_locp < old_location + old_location_count;
12666 old_locp++)
74960c60 12667 {
876fa593 12668 struct bp_location *old_loc = *old_locp;
c7d46a38 12669 struct bp_location **loc2p;
876fa593 12670
e5dd4106 12671 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12672 not, we have to free it. */
c7d46a38 12673 int found_object = 0;
20874c92
VP
12674 /* Tells if the location should remain inserted in the target. */
12675 int keep_in_target = 0;
12676 int removed = 0;
876fa593 12677
4a64f543
MS
12678 /* Skip LOCP entries which will definitely never be needed.
12679 Stop either at or being the one matching OLD_LOC. */
876fa593 12680 while (locp < bp_location + bp_location_count
c7d46a38 12681 && (*locp)->address < old_loc->address)
876fa593 12682 locp++;
c7d46a38
PA
12683
12684 for (loc2p = locp;
12685 (loc2p < bp_location + bp_location_count
12686 && (*loc2p)->address == old_loc->address);
12687 loc2p++)
12688 {
b775012e
LM
12689 /* Check if this is a new/duplicated location or a duplicated
12690 location that had its condition modified. If so, we want to send
12691 its condition to the target if evaluation of conditions is taking
12692 place there. */
12693 if ((*loc2p)->condition_changed == condition_modified
12694 && (last_addr != old_loc->address
12695 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12696 {
b775012e
LM
12697 force_breakpoint_reinsertion (*loc2p);
12698 last_pspace_num = old_loc->pspace->num;
c7d46a38 12699 }
b775012e
LM
12700
12701 if (*loc2p == old_loc)
12702 found_object = 1;
c7d46a38 12703 }
74960c60 12704
b775012e
LM
12705 /* We have already handled this address, update it so that we don't
12706 have to go through updates again. */
12707 last_addr = old_loc->address;
12708
12709 /* Target-side condition evaluation: Handle deleted locations. */
12710 if (!found_object)
12711 force_breakpoint_reinsertion (old_loc);
12712
4a64f543
MS
12713 /* If this location is no longer present, and inserted, look if
12714 there's maybe a new location at the same address. If so,
12715 mark that one inserted, and don't remove this one. This is
12716 needed so that we don't have a time window where a breakpoint
12717 at certain location is not inserted. */
74960c60 12718
876fa593 12719 if (old_loc->inserted)
0d381245 12720 {
4a64f543
MS
12721 /* If the location is inserted now, we might have to remove
12722 it. */
74960c60 12723
876fa593 12724 if (found_object && should_be_inserted (old_loc))
74960c60 12725 {
4a64f543
MS
12726 /* The location is still present in the location list,
12727 and still should be inserted. Don't do anything. */
20874c92 12728 keep_in_target = 1;
74960c60
VP
12729 }
12730 else
12731 {
b775012e
LM
12732 /* This location still exists, but it won't be kept in the
12733 target since it may have been disabled. We proceed to
12734 remove its target-side condition. */
12735
4a64f543
MS
12736 /* The location is either no longer present, or got
12737 disabled. See if there's another location at the
12738 same address, in which case we don't need to remove
12739 this one from the target. */
876fa593 12740
2bdf28a0 12741 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12742 if (breakpoint_address_is_meaningful (old_loc->owner))
12743 {
876fa593 12744 for (loc2p = locp;
c7d46a38
PA
12745 (loc2p < bp_location + bp_location_count
12746 && (*loc2p)->address == old_loc->address);
876fa593
JK
12747 loc2p++)
12748 {
12749 struct bp_location *loc2 = *loc2p;
12750
2d134ed3 12751 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12752 {
85d721b8
PA
12753 /* Read watchpoint locations are switched to
12754 access watchpoints, if the former are not
12755 supported, but the latter are. */
12756 if (is_hardware_watchpoint (old_loc->owner))
12757 {
12758 gdb_assert (is_hardware_watchpoint (loc2->owner));
12759 loc2->watchpoint_type = old_loc->watchpoint_type;
12760 }
12761
934709f0
PW
12762 /* loc2 is a duplicated location. We need to check
12763 if it should be inserted in case it will be
12764 unduplicated. */
12765 if (loc2 != old_loc
12766 && unduplicated_should_be_inserted (loc2))
c7d46a38 12767 {
934709f0 12768 swap_insertion (old_loc, loc2);
c7d46a38
PA
12769 keep_in_target = 1;
12770 break;
12771 }
876fa593
JK
12772 }
12773 }
12774 }
74960c60
VP
12775 }
12776
20874c92
VP
12777 if (!keep_in_target)
12778 {
876fa593 12779 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12780 {
4a64f543
MS
12781 /* This is just about all we can do. We could keep
12782 this location on the global list, and try to
12783 remove it next time, but there's no particular
12784 reason why we will succeed next time.
20874c92 12785
4a64f543
MS
12786 Note that at this point, old_loc->owner is still
12787 valid, as delete_breakpoint frees the breakpoint
12788 only after calling us. */
3e43a32a
MS
12789 printf_filtered (_("warning: Error removing "
12790 "breakpoint %d\n"),
876fa593 12791 old_loc->owner->number);
20874c92
VP
12792 }
12793 removed = 1;
12794 }
0d381245 12795 }
74960c60
VP
12796
12797 if (!found_object)
1c5cfe86 12798 {
db82e815
PA
12799 if (removed && non_stop
12800 && breakpoint_address_is_meaningful (old_loc->owner)
12801 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12802 {
db82e815
PA
12803 /* This location was removed from the target. In
12804 non-stop mode, a race condition is possible where
12805 we've removed a breakpoint, but stop events for that
12806 breakpoint are already queued and will arrive later.
12807 We apply an heuristic to be able to distinguish such
12808 SIGTRAPs from other random SIGTRAPs: we keep this
12809 breakpoint location for a bit, and will retire it
12810 after we see some number of events. The theory here
12811 is that reporting of events should, "on the average",
12812 be fair, so after a while we'll see events from all
12813 threads that have anything of interest, and no longer
12814 need to keep this breakpoint location around. We
12815 don't hold locations forever so to reduce chances of
12816 mistaking a non-breakpoint SIGTRAP for a breakpoint
12817 SIGTRAP.
12818
12819 The heuristic failing can be disastrous on
12820 decr_pc_after_break targets.
12821
12822 On decr_pc_after_break targets, like e.g., x86-linux,
12823 if we fail to recognize a late breakpoint SIGTRAP,
12824 because events_till_retirement has reached 0 too
12825 soon, we'll fail to do the PC adjustment, and report
12826 a random SIGTRAP to the user. When the user resumes
12827 the inferior, it will most likely immediately crash
2dec564e 12828 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12829 corrupted, because of being resumed e.g., in the
12830 middle of a multi-byte instruction, or skipped a
12831 one-byte instruction. This was actually seen happen
12832 on native x86-linux, and should be less rare on
12833 targets that do not support new thread events, like
12834 remote, due to the heuristic depending on
12835 thread_count.
12836
12837 Mistaking a random SIGTRAP for a breakpoint trap
12838 causes similar symptoms (PC adjustment applied when
12839 it shouldn't), but then again, playing with SIGTRAPs
12840 behind the debugger's back is asking for trouble.
12841
12842 Since hardware watchpoint traps are always
12843 distinguishable from other traps, so we don't need to
12844 apply keep hardware watchpoint moribund locations
12845 around. We simply always ignore hardware watchpoint
12846 traps we can no longer explain. */
12847
876fa593
JK
12848 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12849 old_loc->owner = NULL;
20874c92 12850
876fa593 12851 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12852 }
12853 else
f431efe5
PA
12854 {
12855 old_loc->owner = NULL;
12856 decref_bp_location (&old_loc);
12857 }
20874c92 12858 }
74960c60 12859 }
1c5cfe86 12860
348d480f
PA
12861 /* Rescan breakpoints at the same address and section, marking the
12862 first one as "first" and any others as "duplicates". This is so
12863 that the bpt instruction is only inserted once. If we have a
12864 permanent breakpoint at the same place as BPT, make that one the
12865 official one, and the rest as duplicates. Permanent breakpoints
12866 are sorted first for the same address.
12867
12868 Do the same for hardware watchpoints, but also considering the
12869 watchpoint's type (regular/access/read) and length. */
12870
12871 bp_loc_first = NULL;
12872 wp_loc_first = NULL;
12873 awp_loc_first = NULL;
12874 rwp_loc_first = NULL;
12875 ALL_BP_LOCATIONS (loc, locp)
12876 {
12877 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12878 non-NULL. */
348d480f 12879 struct bp_location **loc_first_p;
d3fbdd86 12880 b = loc->owner;
348d480f 12881
6f380991 12882 if (!unduplicated_should_be_inserted (loc)
348d480f 12883 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12884 /* Don't detect duplicate for tracepoint locations because they are
12885 never duplicated. See the comments in field `duplicate' of
12886 `struct bp_location'. */
348d480f 12887 || is_tracepoint (b))
b775012e
LM
12888 {
12889 /* Clear the condition modification flag. */
12890 loc->condition_changed = condition_unchanged;
12891 continue;
12892 }
348d480f
PA
12893
12894 /* Permanent breakpoint should always be inserted. */
1a853c52 12895 if (loc->permanent && ! loc->inserted)
348d480f
PA
12896 internal_error (__FILE__, __LINE__,
12897 _("allegedly permanent breakpoint is not "
12898 "actually inserted"));
12899
12900 if (b->type == bp_hardware_watchpoint)
12901 loc_first_p = &wp_loc_first;
12902 else if (b->type == bp_read_watchpoint)
12903 loc_first_p = &rwp_loc_first;
12904 else if (b->type == bp_access_watchpoint)
12905 loc_first_p = &awp_loc_first;
12906 else
12907 loc_first_p = &bp_loc_first;
12908
12909 if (*loc_first_p == NULL
12910 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12911 || !breakpoint_locations_match (loc, *loc_first_p))
12912 {
12913 *loc_first_p = loc;
12914 loc->duplicate = 0;
b775012e
LM
12915
12916 if (is_breakpoint (loc->owner) && loc->condition_changed)
12917 {
12918 loc->needs_update = 1;
12919 /* Clear the condition modification flag. */
12920 loc->condition_changed = condition_unchanged;
12921 }
348d480f
PA
12922 continue;
12923 }
12924
934709f0
PW
12925
12926 /* This and the above ensure the invariant that the first location
12927 is not duplicated, and is the inserted one.
12928 All following are marked as duplicated, and are not inserted. */
12929 if (loc->inserted)
12930 swap_insertion (loc, *loc_first_p);
348d480f
PA
12931 loc->duplicate = 1;
12932
b775012e
LM
12933 /* Clear the condition modification flag. */
12934 loc->condition_changed = condition_unchanged;
12935
1a853c52
PA
12936 if (loc->inserted && !loc->permanent
12937 && (*loc_first_p)->permanent)
348d480f
PA
12938 internal_error (__FILE__, __LINE__,
12939 _("another breakpoint was inserted on top of "
12940 "a permanent breakpoint"));
12941 }
12942
a25a5a45 12943 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12944 {
04086b45 12945 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12946 insert_breakpoint_locations ();
12947 else
12948 {
44702360
PA
12949 /* Even though the caller told us to not insert new
12950 locations, we may still need to update conditions on the
12951 target's side of breakpoints that were already inserted
12952 if the target is evaluating breakpoint conditions. We
b775012e
LM
12953 only update conditions for locations that are marked
12954 "needs_update". */
12955 update_inserted_breakpoint_locations ();
12956 }
12957 }
348d480f 12958
04086b45 12959 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12960 download_tracepoint_locations ();
12961
348d480f
PA
12962 do_cleanups (cleanups);
12963}
12964
12965void
12966breakpoint_retire_moribund (void)
12967{
12968 struct bp_location *loc;
12969 int ix;
12970
12971 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12972 if (--(loc->events_till_retirement) == 0)
12973 {
12974 decref_bp_location (&loc);
12975 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12976 --ix;
12977 }
12978}
12979
12980static void
44702360 12981update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12982{
bfd189b1 12983 volatile struct gdb_exception e;
348d480f
PA
12984
12985 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 12986 update_global_location_list (insert_mode);
348d480f
PA
12987}
12988
12989/* Clear BKP from a BPS. */
12990
12991static void
12992bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12993{
12994 bpstat bs;
12995
12996 for (bs = bps; bs; bs = bs->next)
12997 if (bs->breakpoint_at == bpt)
12998 {
12999 bs->breakpoint_at = NULL;
13000 bs->old_val = NULL;
13001 /* bs->commands will be freed later. */
13002 }
13003}
13004
13005/* Callback for iterate_over_threads. */
13006static int
13007bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13008{
13009 struct breakpoint *bpt = data;
13010
13011 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13012 return 0;
13013}
13014
13015/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13016 callbacks. */
13017
13018static void
13019say_where (struct breakpoint *b)
13020{
13021 struct value_print_options opts;
13022
13023 get_user_print_options (&opts);
13024
13025 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13026 single string. */
13027 if (b->loc == NULL)
13028 {
13029 printf_filtered (_(" (%s) pending."), b->addr_string);
13030 }
13031 else
13032 {
2f202fde 13033 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13034 {
13035 printf_filtered (" at ");
13036 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13037 gdb_stdout);
13038 }
2f202fde 13039 if (b->loc->symtab != NULL)
f8eba3c6
TT
13040 {
13041 /* If there is a single location, we can print the location
13042 more nicely. */
13043 if (b->loc->next == NULL)
13044 printf_filtered (": file %s, line %d.",
05cba821
JK
13045 symtab_to_filename_for_display (b->loc->symtab),
13046 b->loc->line_number);
f8eba3c6
TT
13047 else
13048 /* This is not ideal, but each location may have a
13049 different file name, and this at least reflects the
13050 real situation somewhat. */
13051 printf_filtered (": %s.", b->addr_string);
13052 }
348d480f
PA
13053
13054 if (b->loc->next)
13055 {
13056 struct bp_location *loc = b->loc;
13057 int n = 0;
13058 for (; loc; loc = loc->next)
13059 ++n;
13060 printf_filtered (" (%d locations)", n);
13061 }
13062 }
13063}
13064
348d480f
PA
13065/* Default bp_location_ops methods. */
13066
13067static void
13068bp_location_dtor (struct bp_location *self)
13069{
13070 xfree (self->cond);
b775012e
LM
13071 if (self->cond_bytecode)
13072 free_agent_expr (self->cond_bytecode);
348d480f 13073 xfree (self->function_name);
8b4f3082
PA
13074
13075 VEC_free (agent_expr_p, self->target_info.conditions);
13076 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13077}
13078
13079static const struct bp_location_ops bp_location_ops =
13080{
13081 bp_location_dtor
13082};
13083
2060206e
PA
13084/* Default breakpoint_ops methods all breakpoint_ops ultimately
13085 inherit from. */
348d480f 13086
2060206e
PA
13087static void
13088base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13089{
13090 decref_counted_command_line (&self->commands);
13091 xfree (self->cond_string);
fb81d016 13092 xfree (self->extra_string);
348d480f 13093 xfree (self->addr_string);
f8eba3c6 13094 xfree (self->filter);
348d480f 13095 xfree (self->addr_string_range_end);
348d480f
PA
13096}
13097
2060206e
PA
13098static struct bp_location *
13099base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13100{
13101 struct bp_location *loc;
13102
13103 loc = XNEW (struct bp_location);
13104 init_bp_location (loc, &bp_location_ops, self);
13105 return loc;
13106}
13107
2060206e
PA
13108static void
13109base_breakpoint_re_set (struct breakpoint *b)
13110{
13111 /* Nothing to re-set. */
13112}
13113
13114#define internal_error_pure_virtual_called() \
13115 gdb_assert_not_reached ("pure virtual function called")
13116
13117static int
13118base_breakpoint_insert_location (struct bp_location *bl)
13119{
13120 internal_error_pure_virtual_called ();
13121}
13122
13123static int
13124base_breakpoint_remove_location (struct bp_location *bl)
13125{
13126 internal_error_pure_virtual_called ();
13127}
13128
13129static int
13130base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13131 struct address_space *aspace,
09ac7c10
TT
13132 CORE_ADDR bp_addr,
13133 const struct target_waitstatus *ws)
2060206e
PA
13134{
13135 internal_error_pure_virtual_called ();
13136}
13137
13138static void
13139base_breakpoint_check_status (bpstat bs)
13140{
13141 /* Always stop. */
13142}
13143
13144/* A "works_in_software_mode" breakpoint_ops method that just internal
13145 errors. */
13146
13147static int
13148base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13149{
13150 internal_error_pure_virtual_called ();
13151}
13152
13153/* A "resources_needed" breakpoint_ops method that just internal
13154 errors. */
13155
13156static int
13157base_breakpoint_resources_needed (const struct bp_location *bl)
13158{
13159 internal_error_pure_virtual_called ();
13160}
13161
13162static enum print_stop_action
13163base_breakpoint_print_it (bpstat bs)
13164{
13165 internal_error_pure_virtual_called ();
13166}
13167
13168static void
13169base_breakpoint_print_one_detail (const struct breakpoint *self,
13170 struct ui_out *uiout)
13171{
13172 /* nothing */
13173}
13174
13175static void
13176base_breakpoint_print_mention (struct breakpoint *b)
13177{
13178 internal_error_pure_virtual_called ();
13179}
13180
13181static void
13182base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13183{
13184 internal_error_pure_virtual_called ();
13185}
13186
983af33b
SDJ
13187static void
13188base_breakpoint_create_sals_from_address (char **arg,
13189 struct linespec_result *canonical,
13190 enum bptype type_wanted,
13191 char *addr_start,
13192 char **copy_arg)
13193{
13194 internal_error_pure_virtual_called ();
13195}
13196
13197static void
13198base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13199 struct linespec_result *c,
983af33b 13200 char *cond_string,
e7e0cddf 13201 char *extra_string,
983af33b
SDJ
13202 enum bptype type_wanted,
13203 enum bpdisp disposition,
13204 int thread,
13205 int task, int ignore_count,
13206 const struct breakpoint_ops *o,
13207 int from_tty, int enabled,
44f238bb 13208 int internal, unsigned flags)
983af33b
SDJ
13209{
13210 internal_error_pure_virtual_called ();
13211}
13212
13213static void
13214base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13215 struct symtabs_and_lines *sals)
13216{
13217 internal_error_pure_virtual_called ();
13218}
13219
ab04a2af
TT
13220/* The default 'explains_signal' method. */
13221
47591c29 13222static int
427cd150 13223base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13224{
47591c29 13225 return 1;
ab04a2af
TT
13226}
13227
9d6e6e84
HZ
13228/* The default "after_condition_true" method. */
13229
13230static void
13231base_breakpoint_after_condition_true (struct bpstats *bs)
13232{
13233 /* Nothing to do. */
13234}
13235
ab04a2af 13236struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13237{
13238 base_breakpoint_dtor,
13239 base_breakpoint_allocate_location,
13240 base_breakpoint_re_set,
13241 base_breakpoint_insert_location,
13242 base_breakpoint_remove_location,
13243 base_breakpoint_breakpoint_hit,
13244 base_breakpoint_check_status,
13245 base_breakpoint_resources_needed,
13246 base_breakpoint_works_in_software_mode,
13247 base_breakpoint_print_it,
13248 NULL,
13249 base_breakpoint_print_one_detail,
13250 base_breakpoint_print_mention,
983af33b
SDJ
13251 base_breakpoint_print_recreate,
13252 base_breakpoint_create_sals_from_address,
13253 base_breakpoint_create_breakpoints_sal,
13254 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13255 base_breakpoint_explains_signal,
13256 base_breakpoint_after_condition_true,
2060206e
PA
13257};
13258
13259/* Default breakpoint_ops methods. */
13260
13261static void
348d480f
PA
13262bkpt_re_set (struct breakpoint *b)
13263{
06edf0c0
PA
13264 /* FIXME: is this still reachable? */
13265 if (b->addr_string == NULL)
13266 {
13267 /* Anything without a string can't be re-set. */
348d480f 13268 delete_breakpoint (b);
06edf0c0 13269 return;
348d480f 13270 }
06edf0c0
PA
13271
13272 breakpoint_re_set_default (b);
348d480f
PA
13273}
13274
2060206e 13275static int
348d480f
PA
13276bkpt_insert_location (struct bp_location *bl)
13277{
13278 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13279 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13280 else
7c16b83e 13281 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13282}
13283
2060206e 13284static int
348d480f
PA
13285bkpt_remove_location (struct bp_location *bl)
13286{
13287 if (bl->loc_type == bp_loc_hardware_breakpoint)
13288 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13289 else
7c16b83e 13290 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13291}
13292
2060206e 13293static int
348d480f 13294bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13295 struct address_space *aspace, CORE_ADDR bp_addr,
13296 const struct target_waitstatus *ws)
348d480f 13297{
09ac7c10 13298 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13299 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13300 return 0;
13301
348d480f
PA
13302 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13303 aspace, bp_addr))
13304 return 0;
13305
13306 if (overlay_debugging /* unmapped overlay section */
13307 && section_is_overlay (bl->section)
13308 && !section_is_mapped (bl->section))
13309 return 0;
13310
13311 return 1;
13312}
13313
cd1608cc
PA
13314static int
13315dprintf_breakpoint_hit (const struct bp_location *bl,
13316 struct address_space *aspace, CORE_ADDR bp_addr,
13317 const struct target_waitstatus *ws)
13318{
13319 if (dprintf_style == dprintf_style_agent
13320 && target_can_run_breakpoint_commands ())
13321 {
13322 /* An agent-style dprintf never causes a stop. If we see a trap
13323 for this address it must be for a breakpoint that happens to
13324 be set at the same address. */
13325 return 0;
13326 }
13327
13328 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13329}
13330
2060206e 13331static int
348d480f
PA
13332bkpt_resources_needed (const struct bp_location *bl)
13333{
13334 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13335
13336 return 1;
13337}
13338
2060206e 13339static enum print_stop_action
348d480f
PA
13340bkpt_print_it (bpstat bs)
13341{
348d480f
PA
13342 struct breakpoint *b;
13343 const struct bp_location *bl;
001c8c33 13344 int bp_temp;
79a45e25 13345 struct ui_out *uiout = current_uiout;
348d480f
PA
13346
13347 gdb_assert (bs->bp_location_at != NULL);
13348
13349 bl = bs->bp_location_at;
13350 b = bs->breakpoint_at;
13351
001c8c33
PA
13352 bp_temp = b->disposition == disp_del;
13353 if (bl->address != bl->requested_address)
13354 breakpoint_adjustment_warning (bl->requested_address,
13355 bl->address,
13356 b->number, 1);
13357 annotate_breakpoint (b->number);
13358 if (bp_temp)
13359 ui_out_text (uiout, "\nTemporary breakpoint ");
13360 else
13361 ui_out_text (uiout, "\nBreakpoint ");
13362 if (ui_out_is_mi_like_p (uiout))
348d480f 13363 {
001c8c33
PA
13364 ui_out_field_string (uiout, "reason",
13365 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13366 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13367 }
001c8c33
PA
13368 ui_out_field_int (uiout, "bkptno", b->number);
13369 ui_out_text (uiout, ", ");
06edf0c0 13370
001c8c33 13371 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13372}
13373
2060206e 13374static void
06edf0c0
PA
13375bkpt_print_mention (struct breakpoint *b)
13376{
79a45e25 13377 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13378 return;
13379
13380 switch (b->type)
13381 {
13382 case bp_breakpoint:
13383 case bp_gnu_ifunc_resolver:
13384 if (b->disposition == disp_del)
13385 printf_filtered (_("Temporary breakpoint"));
13386 else
13387 printf_filtered (_("Breakpoint"));
13388 printf_filtered (_(" %d"), b->number);
13389 if (b->type == bp_gnu_ifunc_resolver)
13390 printf_filtered (_(" at gnu-indirect-function resolver"));
13391 break;
13392 case bp_hardware_breakpoint:
13393 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13394 break;
e7e0cddf
SS
13395 case bp_dprintf:
13396 printf_filtered (_("Dprintf %d"), b->number);
13397 break;
06edf0c0
PA
13398 }
13399
13400 say_where (b);
13401}
13402
2060206e 13403static void
06edf0c0
PA
13404bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13405{
13406 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13407 fprintf_unfiltered (fp, "tbreak");
13408 else if (tp->type == bp_breakpoint)
13409 fprintf_unfiltered (fp, "break");
13410 else if (tp->type == bp_hardware_breakpoint
13411 && tp->disposition == disp_del)
13412 fprintf_unfiltered (fp, "thbreak");
13413 else if (tp->type == bp_hardware_breakpoint)
13414 fprintf_unfiltered (fp, "hbreak");
13415 else
13416 internal_error (__FILE__, __LINE__,
13417 _("unhandled breakpoint type %d"), (int) tp->type);
13418
2060206e 13419 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13420 print_recreate_thread (tp, fp);
06edf0c0
PA
13421}
13422
983af33b
SDJ
13423static void
13424bkpt_create_sals_from_address (char **arg,
13425 struct linespec_result *canonical,
13426 enum bptype type_wanted,
13427 char *addr_start, char **copy_arg)
13428{
13429 create_sals_from_address_default (arg, canonical, type_wanted,
13430 addr_start, copy_arg);
13431}
13432
13433static void
13434bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13435 struct linespec_result *canonical,
983af33b 13436 char *cond_string,
e7e0cddf 13437 char *extra_string,
983af33b
SDJ
13438 enum bptype type_wanted,
13439 enum bpdisp disposition,
13440 int thread,
13441 int task, int ignore_count,
13442 const struct breakpoint_ops *ops,
13443 int from_tty, int enabled,
44f238bb 13444 int internal, unsigned flags)
983af33b 13445{
023fa29b 13446 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13447 cond_string, extra_string,
13448 type_wanted,
983af33b
SDJ
13449 disposition, thread, task,
13450 ignore_count, ops, from_tty,
44f238bb 13451 enabled, internal, flags);
983af33b
SDJ
13452}
13453
13454static void
13455bkpt_decode_linespec (struct breakpoint *b, char **s,
13456 struct symtabs_and_lines *sals)
13457{
13458 decode_linespec_default (b, s, sals);
13459}
13460
06edf0c0
PA
13461/* Virtual table for internal breakpoints. */
13462
13463static void
13464internal_bkpt_re_set (struct breakpoint *b)
13465{
13466 switch (b->type)
13467 {
13468 /* Delete overlay event and longjmp master breakpoints; they
13469 will be reset later by breakpoint_re_set. */
13470 case bp_overlay_event:
13471 case bp_longjmp_master:
13472 case bp_std_terminate_master:
13473 case bp_exception_master:
13474 delete_breakpoint (b);
13475 break;
13476
13477 /* This breakpoint is special, it's set up when the inferior
13478 starts and we really don't want to touch it. */
13479 case bp_shlib_event:
13480
13481 /* Like bp_shlib_event, this breakpoint type is special. Once
13482 it is set up, we do not want to touch it. */
13483 case bp_thread_event:
13484 break;
13485 }
13486}
13487
13488static void
13489internal_bkpt_check_status (bpstat bs)
13490{
a9b3a50f
PA
13491 if (bs->breakpoint_at->type == bp_shlib_event)
13492 {
13493 /* If requested, stop when the dynamic linker notifies GDB of
13494 events. This allows the user to get control and place
13495 breakpoints in initializer routines for dynamically loaded
13496 objects (among other things). */
13497 bs->stop = stop_on_solib_events;
13498 bs->print = stop_on_solib_events;
13499 }
13500 else
13501 bs->stop = 0;
06edf0c0
PA
13502}
13503
13504static enum print_stop_action
13505internal_bkpt_print_it (bpstat bs)
13506{
06edf0c0 13507 struct breakpoint *b;
06edf0c0 13508
06edf0c0
PA
13509 b = bs->breakpoint_at;
13510
06edf0c0
PA
13511 switch (b->type)
13512 {
348d480f
PA
13513 case bp_shlib_event:
13514 /* Did we stop because the user set the stop_on_solib_events
13515 variable? (If so, we report this as a generic, "Stopped due
13516 to shlib event" message.) */
edcc5120 13517 print_solib_event (0);
348d480f
PA
13518 break;
13519
13520 case bp_thread_event:
13521 /* Not sure how we will get here.
13522 GDB should not stop for these breakpoints. */
13523 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13524 break;
13525
13526 case bp_overlay_event:
13527 /* By analogy with the thread event, GDB should not stop for these. */
13528 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13529 break;
13530
13531 case bp_longjmp_master:
13532 /* These should never be enabled. */
13533 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13534 break;
13535
13536 case bp_std_terminate_master:
13537 /* These should never be enabled. */
13538 printf_filtered (_("std::terminate Master Breakpoint: "
13539 "gdb should not stop!\n"));
348d480f
PA
13540 break;
13541
13542 case bp_exception_master:
13543 /* These should never be enabled. */
13544 printf_filtered (_("Exception Master Breakpoint: "
13545 "gdb should not stop!\n"));
06edf0c0
PA
13546 break;
13547 }
13548
001c8c33 13549 return PRINT_NOTHING;
06edf0c0
PA
13550}
13551
13552static void
13553internal_bkpt_print_mention (struct breakpoint *b)
13554{
13555 /* Nothing to mention. These breakpoints are internal. */
13556}
13557
06edf0c0
PA
13558/* Virtual table for momentary breakpoints */
13559
13560static void
13561momentary_bkpt_re_set (struct breakpoint *b)
13562{
13563 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13564 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13565 Otherwise these should have been blown away via the cleanup chain
13566 or by breakpoint_init_inferior when we rerun the executable. */
13567}
13568
13569static void
13570momentary_bkpt_check_status (bpstat bs)
13571{
13572 /* Nothing. The point of these breakpoints is causing a stop. */
13573}
13574
13575static enum print_stop_action
13576momentary_bkpt_print_it (bpstat bs)
13577{
79a45e25
PA
13578 struct ui_out *uiout = current_uiout;
13579
001c8c33 13580 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13581 {
001c8c33 13582 struct breakpoint *b = bs->breakpoint_at;
348d480f 13583
001c8c33
PA
13584 switch (b->type)
13585 {
13586 case bp_finish:
13587 ui_out_field_string
13588 (uiout, "reason",
13589 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13590 break;
348d480f 13591
001c8c33
PA
13592 case bp_until:
13593 ui_out_field_string
13594 (uiout, "reason",
13595 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13596 break;
13597 }
348d480f
PA
13598 }
13599
001c8c33 13600 return PRINT_UNKNOWN;
348d480f
PA
13601}
13602
06edf0c0
PA
13603static void
13604momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13605{
06edf0c0 13606 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13607}
13608
e2e4d78b
JK
13609/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13610
13611 It gets cleared already on the removal of the first one of such placed
13612 breakpoints. This is OK as they get all removed altogether. */
13613
13614static void
13615longjmp_bkpt_dtor (struct breakpoint *self)
13616{
13617 struct thread_info *tp = find_thread_id (self->thread);
13618
13619 if (tp)
13620 tp->initiating_frame = null_frame_id;
13621
13622 momentary_breakpoint_ops.dtor (self);
13623}
13624
55aa24fb
SDJ
13625/* Specific methods for probe breakpoints. */
13626
13627static int
13628bkpt_probe_insert_location (struct bp_location *bl)
13629{
13630 int v = bkpt_insert_location (bl);
13631
13632 if (v == 0)
13633 {
13634 /* The insertion was successful, now let's set the probe's semaphore
13635 if needed. */
0ea5cda8
SDJ
13636 if (bl->probe.probe->pops->set_semaphore != NULL)
13637 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13638 bl->probe.objfile,
13639 bl->gdbarch);
55aa24fb
SDJ
13640 }
13641
13642 return v;
13643}
13644
13645static int
13646bkpt_probe_remove_location (struct bp_location *bl)
13647{
13648 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13649 if (bl->probe.probe->pops->clear_semaphore != NULL)
13650 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13651 bl->probe.objfile,
13652 bl->gdbarch);
55aa24fb
SDJ
13653
13654 return bkpt_remove_location (bl);
13655}
13656
13657static void
13658bkpt_probe_create_sals_from_address (char **arg,
13659 struct linespec_result *canonical,
13660 enum bptype type_wanted,
13661 char *addr_start, char **copy_arg)
13662{
13663 struct linespec_sals lsal;
13664
13665 lsal.sals = parse_probes (arg, canonical);
13666
13667 *copy_arg = xstrdup (canonical->addr_string);
13668 lsal.canonical = xstrdup (*copy_arg);
13669
13670 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13671}
13672
13673static void
13674bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13675 struct symtabs_and_lines *sals)
13676{
13677 *sals = parse_probes (s, NULL);
13678 if (!sals->sals)
13679 error (_("probe not found"));
13680}
13681
348d480f 13682/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13683
348d480f
PA
13684static void
13685tracepoint_re_set (struct breakpoint *b)
13686{
13687 breakpoint_re_set_default (b);
13688}
876fa593 13689
348d480f
PA
13690static int
13691tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13692 struct address_space *aspace, CORE_ADDR bp_addr,
13693 const struct target_waitstatus *ws)
348d480f
PA
13694{
13695 /* By definition, the inferior does not report stops at
13696 tracepoints. */
13697 return 0;
74960c60
VP
13698}
13699
13700static void
348d480f
PA
13701tracepoint_print_one_detail (const struct breakpoint *self,
13702 struct ui_out *uiout)
74960c60 13703{
d9b3f62e
PA
13704 struct tracepoint *tp = (struct tracepoint *) self;
13705 if (tp->static_trace_marker_id)
348d480f
PA
13706 {
13707 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13708
348d480f
PA
13709 ui_out_text (uiout, "\tmarker id is ");
13710 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13711 tp->static_trace_marker_id);
348d480f
PA
13712 ui_out_text (uiout, "\n");
13713 }
0d381245
VP
13714}
13715
a474d7c2 13716static void
348d480f 13717tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13718{
79a45e25 13719 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13720 return;
cc59ec59 13721
348d480f
PA
13722 switch (b->type)
13723 {
13724 case bp_tracepoint:
13725 printf_filtered (_("Tracepoint"));
13726 printf_filtered (_(" %d"), b->number);
13727 break;
13728 case bp_fast_tracepoint:
13729 printf_filtered (_("Fast tracepoint"));
13730 printf_filtered (_(" %d"), b->number);
13731 break;
13732 case bp_static_tracepoint:
13733 printf_filtered (_("Static tracepoint"));
13734 printf_filtered (_(" %d"), b->number);
13735 break;
13736 default:
13737 internal_error (__FILE__, __LINE__,
13738 _("unhandled tracepoint type %d"), (int) b->type);
13739 }
13740
13741 say_where (b);
a474d7c2
PA
13742}
13743
348d480f 13744static void
d9b3f62e 13745tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13746{
d9b3f62e
PA
13747 struct tracepoint *tp = (struct tracepoint *) self;
13748
13749 if (self->type == bp_fast_tracepoint)
348d480f 13750 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13751 if (self->type == bp_static_tracepoint)
348d480f 13752 fprintf_unfiltered (fp, "strace");
d9b3f62e 13753 else if (self->type == bp_tracepoint)
348d480f
PA
13754 fprintf_unfiltered (fp, "trace");
13755 else
13756 internal_error (__FILE__, __LINE__,
d9b3f62e 13757 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13758
d9b3f62e
PA
13759 fprintf_unfiltered (fp, " %s", self->addr_string);
13760 print_recreate_thread (self, fp);
13761
13762 if (tp->pass_count)
13763 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13764}
13765
983af33b
SDJ
13766static void
13767tracepoint_create_sals_from_address (char **arg,
13768 struct linespec_result *canonical,
13769 enum bptype type_wanted,
13770 char *addr_start, char **copy_arg)
13771{
13772 create_sals_from_address_default (arg, canonical, type_wanted,
13773 addr_start, copy_arg);
13774}
13775
13776static void
13777tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13778 struct linespec_result *canonical,
983af33b 13779 char *cond_string,
e7e0cddf 13780 char *extra_string,
983af33b
SDJ
13781 enum bptype type_wanted,
13782 enum bpdisp disposition,
13783 int thread,
13784 int task, int ignore_count,
13785 const struct breakpoint_ops *ops,
13786 int from_tty, int enabled,
44f238bb 13787 int internal, unsigned flags)
983af33b 13788{
023fa29b 13789 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13790 cond_string, extra_string,
13791 type_wanted,
983af33b
SDJ
13792 disposition, thread, task,
13793 ignore_count, ops, from_tty,
44f238bb 13794 enabled, internal, flags);
983af33b
SDJ
13795}
13796
13797static void
13798tracepoint_decode_linespec (struct breakpoint *b, char **s,
13799 struct symtabs_and_lines *sals)
13800{
13801 decode_linespec_default (b, s, sals);
13802}
13803
2060206e 13804struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13805
55aa24fb
SDJ
13806/* The breakpoint_ops structure to be use on tracepoints placed in a
13807 static probe. */
13808
13809static void
13810tracepoint_probe_create_sals_from_address (char **arg,
13811 struct linespec_result *canonical,
13812 enum bptype type_wanted,
13813 char *addr_start, char **copy_arg)
13814{
13815 /* We use the same method for breakpoint on probes. */
13816 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13817 addr_start, copy_arg);
13818}
13819
13820static void
13821tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13822 struct symtabs_and_lines *sals)
13823{
13824 /* We use the same method for breakpoint on probes. */
13825 bkpt_probe_decode_linespec (b, s, sals);
13826}
13827
13828static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13829
5c2b4418
HZ
13830/* Dprintf breakpoint_ops methods. */
13831
13832static void
13833dprintf_re_set (struct breakpoint *b)
13834{
13835 breakpoint_re_set_default (b);
13836
13837 /* This breakpoint could have been pending, and be resolved now, and
13838 if so, we should now have the extra string. If we don't, the
13839 dprintf was malformed when created, but we couldn't tell because
13840 we can't extract the extra string until the location is
13841 resolved. */
13842 if (b->loc != NULL && b->extra_string == NULL)
13843 error (_("Format string required"));
13844
13845 /* 1 - connect to target 1, that can run breakpoint commands.
13846 2 - create a dprintf, which resolves fine.
13847 3 - disconnect from target 1
13848 4 - connect to target 2, that can NOT run breakpoint commands.
13849
13850 After steps #3/#4, you'll want the dprintf command list to
13851 be updated, because target 1 and 2 may well return different
13852 answers for target_can_run_breakpoint_commands().
13853 Given absence of finer grained resetting, we get to do
13854 it all the time. */
13855 if (b->extra_string != NULL)
13856 update_dprintf_command_list (b);
13857}
13858
2d9442cc
HZ
13859/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13860
13861static void
13862dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13863{
13864 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13865 tp->extra_string);
13866 print_recreate_thread (tp, fp);
13867}
13868
9d6e6e84
HZ
13869/* Implement the "after_condition_true" breakpoint_ops method for
13870 dprintf.
13871
13872 dprintf's are implemented with regular commands in their command
13873 list, but we run the commands here instead of before presenting the
13874 stop to the user, as dprintf's don't actually cause a stop. This
13875 also makes it so that the commands of multiple dprintfs at the same
13876 address are all handled. */
13877
13878static void
13879dprintf_after_condition_true (struct bpstats *bs)
13880{
13881 struct cleanup *old_chain;
13882 struct bpstats tmp_bs = { NULL };
13883 struct bpstats *tmp_bs_p = &tmp_bs;
13884
13885 /* dprintf's never cause a stop. This wasn't set in the
13886 check_status hook instead because that would make the dprintf's
13887 condition not be evaluated. */
13888 bs->stop = 0;
13889
13890 /* Run the command list here. Take ownership of it instead of
13891 copying. We never want these commands to run later in
13892 bpstat_do_actions, if a breakpoint that causes a stop happens to
13893 be set at same address as this dprintf, or even if running the
13894 commands here throws. */
13895 tmp_bs.commands = bs->commands;
13896 bs->commands = NULL;
13897 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13898
13899 bpstat_do_actions_1 (&tmp_bs_p);
13900
13901 /* 'tmp_bs.commands' will usually be NULL by now, but
13902 bpstat_do_actions_1 may return early without processing the whole
13903 list. */
13904 do_cleanups (old_chain);
13905}
13906
983af33b
SDJ
13907/* The breakpoint_ops structure to be used on static tracepoints with
13908 markers (`-m'). */
13909
13910static void
13911strace_marker_create_sals_from_address (char **arg,
13912 struct linespec_result *canonical,
13913 enum bptype type_wanted,
13914 char *addr_start, char **copy_arg)
13915{
13916 struct linespec_sals lsal;
13917
13918 lsal.sals = decode_static_tracepoint_spec (arg);
13919
13920 *copy_arg = savestring (addr_start, *arg - addr_start);
13921
13922 canonical->addr_string = xstrdup (*copy_arg);
13923 lsal.canonical = xstrdup (*copy_arg);
13924 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13925}
13926
13927static void
13928strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13929 struct linespec_result *canonical,
983af33b 13930 char *cond_string,
e7e0cddf 13931 char *extra_string,
983af33b
SDJ
13932 enum bptype type_wanted,
13933 enum bpdisp disposition,
13934 int thread,
13935 int task, int ignore_count,
13936 const struct breakpoint_ops *ops,
13937 int from_tty, int enabled,
44f238bb 13938 int internal, unsigned flags)
983af33b
SDJ
13939{
13940 int i;
52d361e1
YQ
13941 struct linespec_sals *lsal = VEC_index (linespec_sals,
13942 canonical->sals, 0);
983af33b
SDJ
13943
13944 /* If the user is creating a static tracepoint by marker id
13945 (strace -m MARKER_ID), then store the sals index, so that
13946 breakpoint_re_set can try to match up which of the newly
13947 found markers corresponds to this one, and, don't try to
13948 expand multiple locations for each sal, given than SALS
13949 already should contain all sals for MARKER_ID. */
13950
13951 for (i = 0; i < lsal->sals.nelts; ++i)
13952 {
13953 struct symtabs_and_lines expanded;
13954 struct tracepoint *tp;
13955 struct cleanup *old_chain;
13956 char *addr_string;
13957
13958 expanded.nelts = 1;
13959 expanded.sals = &lsal->sals.sals[i];
13960
13961 addr_string = xstrdup (canonical->addr_string);
13962 old_chain = make_cleanup (xfree, addr_string);
13963
13964 tp = XCNEW (struct tracepoint);
13965 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13966 addr_string, NULL,
e7e0cddf
SS
13967 cond_string, extra_string,
13968 type_wanted, disposition,
983af33b 13969 thread, task, ignore_count, ops,
44f238bb 13970 from_tty, enabled, internal, flags,
983af33b
SDJ
13971 canonical->special_display);
13972 /* Given that its possible to have multiple markers with
13973 the same string id, if the user is creating a static
13974 tracepoint by marker id ("strace -m MARKER_ID"), then
13975 store the sals index, so that breakpoint_re_set can
13976 try to match up which of the newly found markers
13977 corresponds to this one */
13978 tp->static_trace_marker_id_idx = i;
13979
13980 install_breakpoint (internal, &tp->base, 0);
13981
13982 discard_cleanups (old_chain);
13983 }
13984}
13985
13986static void
13987strace_marker_decode_linespec (struct breakpoint *b, char **s,
13988 struct symtabs_and_lines *sals)
13989{
13990 struct tracepoint *tp = (struct tracepoint *) b;
13991
13992 *sals = decode_static_tracepoint_spec (s);
13993 if (sals->nelts > tp->static_trace_marker_id_idx)
13994 {
13995 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13996 sals->nelts = 1;
13997 }
13998 else
13999 error (_("marker %s not found"), tp->static_trace_marker_id);
14000}
14001
14002static struct breakpoint_ops strace_marker_breakpoint_ops;
14003
14004static int
14005strace_marker_p (struct breakpoint *b)
14006{
14007 return b->ops == &strace_marker_breakpoint_ops;
14008}
14009
53a5351d 14010/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14011 structures. */
c906108c
SS
14012
14013void
fba45db2 14014delete_breakpoint (struct breakpoint *bpt)
c906108c 14015{
52f0bd74 14016 struct breakpoint *b;
c906108c 14017
8a3fe4f8 14018 gdb_assert (bpt != NULL);
c906108c 14019
4a64f543
MS
14020 /* Has this bp already been deleted? This can happen because
14021 multiple lists can hold pointers to bp's. bpstat lists are
14022 especial culprits.
14023
14024 One example of this happening is a watchpoint's scope bp. When
14025 the scope bp triggers, we notice that the watchpoint is out of
14026 scope, and delete it. We also delete its scope bp. But the
14027 scope bp is marked "auto-deleting", and is already on a bpstat.
14028 That bpstat is then checked for auto-deleting bp's, which are
14029 deleted.
14030
14031 A real solution to this problem might involve reference counts in
14032 bp's, and/or giving them pointers back to their referencing
14033 bpstat's, and teaching delete_breakpoint to only free a bp's
14034 storage when no more references were extent. A cheaper bandaid
14035 was chosen. */
c906108c
SS
14036 if (bpt->type == bp_none)
14037 return;
14038
4a64f543
MS
14039 /* At least avoid this stale reference until the reference counting
14040 of breakpoints gets resolved. */
d0fb5eae 14041 if (bpt->related_breakpoint != bpt)
e5a0a904 14042 {
d0fb5eae 14043 struct breakpoint *related;
3a5c3e22 14044 struct watchpoint *w;
d0fb5eae
JK
14045
14046 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14047 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14048 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14049 w = (struct watchpoint *) bpt;
14050 else
14051 w = NULL;
14052 if (w != NULL)
14053 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14054
14055 /* Unlink bpt from the bpt->related_breakpoint ring. */
14056 for (related = bpt; related->related_breakpoint != bpt;
14057 related = related->related_breakpoint);
14058 related->related_breakpoint = bpt->related_breakpoint;
14059 bpt->related_breakpoint = bpt;
e5a0a904
JK
14060 }
14061
a9634178
TJB
14062 /* watch_command_1 creates a watchpoint but only sets its number if
14063 update_watchpoint succeeds in creating its bp_locations. If there's
14064 a problem in that process, we'll be asked to delete the half-created
14065 watchpoint. In that case, don't announce the deletion. */
14066 if (bpt->number)
14067 observer_notify_breakpoint_deleted (bpt);
c906108c 14068
c906108c
SS
14069 if (breakpoint_chain == bpt)
14070 breakpoint_chain = bpt->next;
14071
c906108c
SS
14072 ALL_BREAKPOINTS (b)
14073 if (b->next == bpt)
c5aa993b
JM
14074 {
14075 b->next = bpt->next;
14076 break;
14077 }
c906108c 14078
f431efe5
PA
14079 /* Be sure no bpstat's are pointing at the breakpoint after it's
14080 been freed. */
14081 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14082 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14083 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14084 commands are associated with the bpstat; if we remove it here,
14085 then the later call to bpstat_do_actions (&stop_bpstat); in
14086 event-top.c won't do anything, and temporary breakpoints with
14087 commands won't work. */
14088
14089 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14090
4a64f543
MS
14091 /* Now that breakpoint is removed from breakpoint list, update the
14092 global location list. This will remove locations that used to
14093 belong to this breakpoint. Do this before freeing the breakpoint
14094 itself, since remove_breakpoint looks at location's owner. It
14095 might be better design to have location completely
14096 self-contained, but it's not the case now. */
44702360 14097 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14098
348d480f 14099 bpt->ops->dtor (bpt);
4a64f543
MS
14100 /* On the chance that someone will soon try again to delete this
14101 same bp, we mark it as deleted before freeing its storage. */
c906108c 14102 bpt->type = bp_none;
b8c9b27d 14103 xfree (bpt);
c906108c
SS
14104}
14105
4d6140d9
AC
14106static void
14107do_delete_breakpoint_cleanup (void *b)
14108{
14109 delete_breakpoint (b);
14110}
14111
14112struct cleanup *
14113make_cleanup_delete_breakpoint (struct breakpoint *b)
14114{
14115 return make_cleanup (do_delete_breakpoint_cleanup, b);
14116}
14117
51be5b68
PA
14118/* Iterator function to call a user-provided callback function once
14119 for each of B and its related breakpoints. */
14120
14121static void
14122iterate_over_related_breakpoints (struct breakpoint *b,
14123 void (*function) (struct breakpoint *,
14124 void *),
14125 void *data)
14126{
14127 struct breakpoint *related;
14128
14129 related = b;
14130 do
14131 {
14132 struct breakpoint *next;
14133
14134 /* FUNCTION may delete RELATED. */
14135 next = related->related_breakpoint;
14136
14137 if (next == related)
14138 {
14139 /* RELATED is the last ring entry. */
14140 function (related, data);
14141
14142 /* FUNCTION may have deleted it, so we'd never reach back to
14143 B. There's nothing left to do anyway, so just break
14144 out. */
14145 break;
14146 }
14147 else
14148 function (related, data);
14149
14150 related = next;
14151 }
14152 while (related != b);
14153}
95a42b64
TT
14154
14155static void
14156do_delete_breakpoint (struct breakpoint *b, void *ignore)
14157{
14158 delete_breakpoint (b);
14159}
14160
51be5b68
PA
14161/* A callback for map_breakpoint_numbers that calls
14162 delete_breakpoint. */
14163
14164static void
14165do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14166{
14167 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14168}
14169
c906108c 14170void
fba45db2 14171delete_command (char *arg, int from_tty)
c906108c 14172{
35df4500 14173 struct breakpoint *b, *b_tmp;
c906108c 14174
ea9365bb
TT
14175 dont_repeat ();
14176
c906108c
SS
14177 if (arg == 0)
14178 {
14179 int breaks_to_delete = 0;
14180
46c6471b
PA
14181 /* Delete all breakpoints if no argument. Do not delete
14182 internal breakpoints, these have to be deleted with an
14183 explicit breakpoint number argument. */
c5aa993b 14184 ALL_BREAKPOINTS (b)
46c6471b 14185 if (user_breakpoint_p (b))
973d738b
DJ
14186 {
14187 breaks_to_delete = 1;
14188 break;
14189 }
c906108c
SS
14190
14191 /* Ask user only if there are some breakpoints to delete. */
14192 if (!from_tty
e2e0b3e5 14193 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14194 {
35df4500 14195 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14196 if (user_breakpoint_p (b))
c5aa993b 14197 delete_breakpoint (b);
c906108c
SS
14198 }
14199 }
14200 else
51be5b68 14201 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14202}
14203
0d381245
VP
14204static int
14205all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14206{
0d381245 14207 for (; loc; loc = loc->next)
8645ff69
UW
14208 if (!loc->shlib_disabled
14209 && !loc->pspace->executing_startup)
0d381245
VP
14210 return 0;
14211 return 1;
fe3f5fa8
VP
14212}
14213
776592bf
DE
14214/* Subroutine of update_breakpoint_locations to simplify it.
14215 Return non-zero if multiple fns in list LOC have the same name.
14216 Null names are ignored. */
14217
14218static int
14219ambiguous_names_p (struct bp_location *loc)
14220{
14221 struct bp_location *l;
14222 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14223 (int (*) (const void *,
14224 const void *)) streq,
776592bf
DE
14225 NULL, xcalloc, xfree);
14226
14227 for (l = loc; l != NULL; l = l->next)
14228 {
14229 const char **slot;
14230 const char *name = l->function_name;
14231
14232 /* Allow for some names to be NULL, ignore them. */
14233 if (name == NULL)
14234 continue;
14235
14236 slot = (const char **) htab_find_slot (htab, (const void *) name,
14237 INSERT);
4a64f543
MS
14238 /* NOTE: We can assume slot != NULL here because xcalloc never
14239 returns NULL. */
776592bf
DE
14240 if (*slot != NULL)
14241 {
14242 htab_delete (htab);
14243 return 1;
14244 }
14245 *slot = name;
14246 }
14247
14248 htab_delete (htab);
14249 return 0;
14250}
14251
0fb4aa4b
PA
14252/* When symbols change, it probably means the sources changed as well,
14253 and it might mean the static tracepoint markers are no longer at
14254 the same address or line numbers they used to be at last we
14255 checked. Losing your static tracepoints whenever you rebuild is
14256 undesirable. This function tries to resync/rematch gdb static
14257 tracepoints with the markers on the target, for static tracepoints
14258 that have not been set by marker id. Static tracepoint that have
14259 been set by marker id are reset by marker id in breakpoint_re_set.
14260 The heuristic is:
14261
14262 1) For a tracepoint set at a specific address, look for a marker at
14263 the old PC. If one is found there, assume to be the same marker.
14264 If the name / string id of the marker found is different from the
14265 previous known name, assume that means the user renamed the marker
14266 in the sources, and output a warning.
14267
14268 2) For a tracepoint set at a given line number, look for a marker
14269 at the new address of the old line number. If one is found there,
14270 assume to be the same marker. If the name / string id of the
14271 marker found is different from the previous known name, assume that
14272 means the user renamed the marker in the sources, and output a
14273 warning.
14274
14275 3) If a marker is no longer found at the same address or line, it
14276 may mean the marker no longer exists. But it may also just mean
14277 the code changed a bit. Maybe the user added a few lines of code
14278 that made the marker move up or down (in line number terms). Ask
14279 the target for info about the marker with the string id as we knew
14280 it. If found, update line number and address in the matching
14281 static tracepoint. This will get confused if there's more than one
14282 marker with the same ID (possible in UST, although unadvised
14283 precisely because it confuses tools). */
14284
14285static struct symtab_and_line
14286update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14287{
d9b3f62e 14288 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14289 struct static_tracepoint_marker marker;
14290 CORE_ADDR pc;
0fb4aa4b
PA
14291
14292 pc = sal.pc;
14293 if (sal.line)
14294 find_line_pc (sal.symtab, sal.line, &pc);
14295
14296 if (target_static_tracepoint_marker_at (pc, &marker))
14297 {
d9b3f62e 14298 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14299 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14300 b->number,
d9b3f62e 14301 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14302
d9b3f62e
PA
14303 xfree (tp->static_trace_marker_id);
14304 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14305 release_static_tracepoint_marker (&marker);
14306
14307 return sal;
14308 }
14309
14310 /* Old marker wasn't found on target at lineno. Try looking it up
14311 by string ID. */
14312 if (!sal.explicit_pc
14313 && sal.line != 0
14314 && sal.symtab != NULL
d9b3f62e 14315 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14316 {
14317 VEC(static_tracepoint_marker_p) *markers;
14318
14319 markers
d9b3f62e 14320 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14321
14322 if (!VEC_empty(static_tracepoint_marker_p, markers))
14323 {
80e1d417 14324 struct symtab_and_line sal2;
0fb4aa4b 14325 struct symbol *sym;
80e1d417 14326 struct static_tracepoint_marker *tpmarker;
79a45e25 14327 struct ui_out *uiout = current_uiout;
0fb4aa4b 14328
80e1d417 14329 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14330
d9b3f62e 14331 xfree (tp->static_trace_marker_id);
80e1d417 14332 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14333
14334 warning (_("marker for static tracepoint %d (%s) not "
14335 "found at previous line number"),
d9b3f62e 14336 b->number, tp->static_trace_marker_id);
0fb4aa4b 14337
80e1d417 14338 init_sal (&sal2);
0fb4aa4b 14339
80e1d417 14340 sal2.pc = tpmarker->address;
0fb4aa4b 14341
80e1d417
AS
14342 sal2 = find_pc_line (tpmarker->address, 0);
14343 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14344 ui_out_text (uiout, "Now in ");
14345 if (sym)
14346 {
14347 ui_out_field_string (uiout, "func",
14348 SYMBOL_PRINT_NAME (sym));
14349 ui_out_text (uiout, " at ");
14350 }
05cba821
JK
14351 ui_out_field_string (uiout, "file",
14352 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14353 ui_out_text (uiout, ":");
14354
14355 if (ui_out_is_mi_like_p (uiout))
14356 {
0b0865da 14357 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14358
f35a17b5 14359 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14360 }
14361
80e1d417 14362 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14363 ui_out_text (uiout, "\n");
14364
80e1d417 14365 b->loc->line_number = sal2.line;
2f202fde 14366 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14367
14368 xfree (b->addr_string);
14369 b->addr_string = xstrprintf ("%s:%d",
05cba821 14370 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14371 b->loc->line_number);
0fb4aa4b
PA
14372
14373 /* Might be nice to check if function changed, and warn if
14374 so. */
14375
80e1d417 14376 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14377 }
14378 }
14379 return sal;
14380}
14381
8d3788bd
VP
14382/* Returns 1 iff locations A and B are sufficiently same that
14383 we don't need to report breakpoint as changed. */
14384
14385static int
14386locations_are_equal (struct bp_location *a, struct bp_location *b)
14387{
14388 while (a && b)
14389 {
14390 if (a->address != b->address)
14391 return 0;
14392
14393 if (a->shlib_disabled != b->shlib_disabled)
14394 return 0;
14395
14396 if (a->enabled != b->enabled)
14397 return 0;
14398
14399 a = a->next;
14400 b = b->next;
14401 }
14402
14403 if ((a == NULL) != (b == NULL))
14404 return 0;
14405
14406 return 1;
14407}
14408
f1310107
TJB
14409/* Create new breakpoint locations for B (a hardware or software breakpoint)
14410 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14411 a ranged breakpoint. */
14412
0e30163f 14413void
0d381245 14414update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14415 struct symtabs_and_lines sals,
14416 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14417{
14418 int i;
0d381245
VP
14419 struct bp_location *existing_locations = b->loc;
14420
f8eba3c6
TT
14421 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14422 {
14423 /* Ranged breakpoints have only one start location and one end
14424 location. */
14425 b->enable_state = bp_disabled;
44702360 14426 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14427 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14428 "multiple locations found\n"),
14429 b->number);
14430 return;
14431 }
f1310107 14432
4a64f543
MS
14433 /* If there's no new locations, and all existing locations are
14434 pending, don't do anything. This optimizes the common case where
14435 all locations are in the same shared library, that was unloaded.
14436 We'd like to retain the location, so that when the library is
14437 loaded again, we don't loose the enabled/disabled status of the
14438 individual locations. */
0d381245 14439 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14440 return;
14441
fe3f5fa8
VP
14442 b->loc = NULL;
14443
0d381245 14444 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14445 {
f8eba3c6
TT
14446 struct bp_location *new_loc;
14447
14448 switch_to_program_space_and_thread (sals.sals[i].pspace);
14449
14450 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14451
0d381245
VP
14452 /* Reparse conditions, they might contain references to the
14453 old symtab. */
14454 if (b->cond_string != NULL)
14455 {
bbc13ae3 14456 const char *s;
bfd189b1 14457 volatile struct gdb_exception e;
fe3f5fa8 14458
0d381245
VP
14459 s = b->cond_string;
14460 TRY_CATCH (e, RETURN_MASK_ERROR)
14461 {
1bb9788d
TT
14462 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14463 block_for_pc (sals.sals[i].pc),
0d381245
VP
14464 0);
14465 }
14466 if (e.reason < 0)
14467 {
3e43a32a
MS
14468 warning (_("failed to reevaluate condition "
14469 "for breakpoint %d: %s"),
0d381245
VP
14470 b->number, e.message);
14471 new_loc->enabled = 0;
14472 }
14473 }
fe3f5fa8 14474
f1310107
TJB
14475 if (sals_end.nelts)
14476 {
14477 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14478
14479 new_loc->length = end - sals.sals[0].pc + 1;
14480 }
0d381245 14481 }
fe3f5fa8 14482
4a64f543
MS
14483 /* If possible, carry over 'disable' status from existing
14484 breakpoints. */
0d381245
VP
14485 {
14486 struct bp_location *e = existing_locations;
776592bf
DE
14487 /* If there are multiple breakpoints with the same function name,
14488 e.g. for inline functions, comparing function names won't work.
14489 Instead compare pc addresses; this is just a heuristic as things
14490 may have moved, but in practice it gives the correct answer
14491 often enough until a better solution is found. */
14492 int have_ambiguous_names = ambiguous_names_p (b->loc);
14493
0d381245
VP
14494 for (; e; e = e->next)
14495 {
14496 if (!e->enabled && e->function_name)
14497 {
14498 struct bp_location *l = b->loc;
776592bf
DE
14499 if (have_ambiguous_names)
14500 {
14501 for (; l; l = l->next)
f1310107 14502 if (breakpoint_locations_match (e, l))
776592bf
DE
14503 {
14504 l->enabled = 0;
14505 break;
14506 }
14507 }
14508 else
14509 {
14510 for (; l; l = l->next)
14511 if (l->function_name
14512 && strcmp (e->function_name, l->function_name) == 0)
14513 {
14514 l->enabled = 0;
14515 break;
14516 }
14517 }
0d381245
VP
14518 }
14519 }
14520 }
fe3f5fa8 14521
8d3788bd
VP
14522 if (!locations_are_equal (existing_locations, b->loc))
14523 observer_notify_breakpoint_modified (b);
14524
44702360 14525 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14526}
14527
ef23e705
TJB
14528/* Find the SaL locations corresponding to the given ADDR_STRING.
14529 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14530
14531static struct symtabs_and_lines
14532addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14533{
14534 char *s;
02d20e4a 14535 struct symtabs_and_lines sals = {0};
f8eba3c6 14536 volatile struct gdb_exception e;
ef23e705 14537
983af33b 14538 gdb_assert (b->ops != NULL);
ef23e705 14539 s = addr_string;
ef23e705
TJB
14540
14541 TRY_CATCH (e, RETURN_MASK_ERROR)
14542 {
983af33b 14543 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14544 }
14545 if (e.reason < 0)
14546 {
14547 int not_found_and_ok = 0;
14548 /* For pending breakpoints, it's expected that parsing will
14549 fail until the right shared library is loaded. User has
14550 already told to create pending breakpoints and don't need
14551 extra messages. If breakpoint is in bp_shlib_disabled
14552 state, then user already saw the message about that
14553 breakpoint being disabled, and don't want to see more
14554 errors. */
58438ac1 14555 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14556 && (b->condition_not_parsed
14557 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14558 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14559 || b->enable_state == bp_disabled))
14560 not_found_and_ok = 1;
14561
14562 if (!not_found_and_ok)
14563 {
14564 /* We surely don't want to warn about the same breakpoint
14565 10 times. One solution, implemented here, is disable
14566 the breakpoint on error. Another solution would be to
14567 have separate 'warning emitted' flag. Since this
14568 happens only when a binary has changed, I don't know
14569 which approach is better. */
14570 b->enable_state = bp_disabled;
14571 throw_exception (e);
14572 }
14573 }
14574
58438ac1 14575 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14576 {
f8eba3c6 14577 int i;
ef23e705 14578
f8eba3c6
TT
14579 for (i = 0; i < sals.nelts; ++i)
14580 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14581 if (b->condition_not_parsed && s && s[0])
14582 {
ed1d1739
KS
14583 char *cond_string, *extra_string;
14584 int thread, task;
ef23e705
TJB
14585
14586 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14587 &cond_string, &thread, &task,
14588 &extra_string);
ef23e705
TJB
14589 if (cond_string)
14590 b->cond_string = cond_string;
14591 b->thread = thread;
14592 b->task = task;
e7e0cddf
SS
14593 if (extra_string)
14594 b->extra_string = extra_string;
ef23e705
TJB
14595 b->condition_not_parsed = 0;
14596 }
14597
983af33b 14598 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14599 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14600
58438ac1
TT
14601 *found = 1;
14602 }
14603 else
14604 *found = 0;
ef23e705
TJB
14605
14606 return sals;
14607}
14608
348d480f
PA
14609/* The default re_set method, for typical hardware or software
14610 breakpoints. Reevaluate the breakpoint and recreate its
14611 locations. */
14612
14613static void
28010a5d 14614breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14615{
14616 int found;
f1310107 14617 struct symtabs_and_lines sals, sals_end;
ef23e705 14618 struct symtabs_and_lines expanded = {0};
f1310107 14619 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14620
14621 sals = addr_string_to_sals (b, b->addr_string, &found);
14622 if (found)
14623 {
14624 make_cleanup (xfree, sals.sals);
f8eba3c6 14625 expanded = sals;
ef23e705
TJB
14626 }
14627
f1310107
TJB
14628 if (b->addr_string_range_end)
14629 {
14630 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14631 if (found)
14632 {
14633 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14634 expanded_end = sals_end;
f1310107
TJB
14635 }
14636 }
14637
14638 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14639}
14640
983af33b
SDJ
14641/* Default method for creating SALs from an address string. It basically
14642 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14643
14644static void
14645create_sals_from_address_default (char **arg,
14646 struct linespec_result *canonical,
14647 enum bptype type_wanted,
14648 char *addr_start, char **copy_arg)
14649{
14650 parse_breakpoint_sals (arg, canonical);
14651}
14652
14653/* Call create_breakpoints_sal for the given arguments. This is the default
14654 function for the `create_breakpoints_sal' method of
14655 breakpoint_ops. */
14656
14657static void
14658create_breakpoints_sal_default (struct gdbarch *gdbarch,
14659 struct linespec_result *canonical,
983af33b 14660 char *cond_string,
e7e0cddf 14661 char *extra_string,
983af33b
SDJ
14662 enum bptype type_wanted,
14663 enum bpdisp disposition,
14664 int thread,
14665 int task, int ignore_count,
14666 const struct breakpoint_ops *ops,
14667 int from_tty, int enabled,
44f238bb 14668 int internal, unsigned flags)
983af33b
SDJ
14669{
14670 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14671 extra_string,
983af33b
SDJ
14672 type_wanted, disposition,
14673 thread, task, ignore_count, ops, from_tty,
44f238bb 14674 enabled, internal, flags);
983af33b
SDJ
14675}
14676
14677/* Decode the line represented by S by calling decode_line_full. This is the
14678 default function for the `decode_linespec' method of breakpoint_ops. */
14679
14680static void
14681decode_linespec_default (struct breakpoint *b, char **s,
14682 struct symtabs_and_lines *sals)
14683{
14684 struct linespec_result canonical;
14685
14686 init_linespec_result (&canonical);
14687 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14688 (struct symtab *) NULL, 0,
14689 &canonical, multiple_symbols_all,
14690 b->filter);
14691
14692 /* We should get 0 or 1 resulting SALs. */
14693 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14694
14695 if (VEC_length (linespec_sals, canonical.sals) > 0)
14696 {
14697 struct linespec_sals *lsal;
14698
14699 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14700 *sals = lsal->sals;
14701 /* Arrange it so the destructor does not free the
14702 contents. */
14703 lsal->sals.sals = NULL;
14704 }
14705
14706 destroy_linespec_result (&canonical);
14707}
14708
28010a5d
PA
14709/* Prepare the global context for a re-set of breakpoint B. */
14710
14711static struct cleanup *
14712prepare_re_set_context (struct breakpoint *b)
14713{
14714 struct cleanup *cleanups;
14715
14716 input_radix = b->input_radix;
14717 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14718 if (b->pspace != NULL)
14719 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14720 set_language (b->language);
14721
14722 return cleanups;
ef23e705
TJB
14723}
14724
c906108c
SS
14725/* Reset a breakpoint given it's struct breakpoint * BINT.
14726 The value we return ends up being the return value from catch_errors.
14727 Unused in this case. */
14728
14729static int
4efb68b1 14730breakpoint_re_set_one (void *bint)
c906108c 14731{
4a64f543 14732 /* Get past catch_errs. */
53a5351d 14733 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14734 struct cleanup *cleanups;
c906108c 14735
348d480f
PA
14736 cleanups = prepare_re_set_context (b);
14737 b->ops->re_set (b);
14738 do_cleanups (cleanups);
c906108c
SS
14739 return 0;
14740}
14741
69de3c6a 14742/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14743void
69de3c6a 14744breakpoint_re_set (void)
c906108c 14745{
35df4500 14746 struct breakpoint *b, *b_tmp;
c906108c
SS
14747 enum language save_language;
14748 int save_input_radix;
6c95b8df 14749 struct cleanup *old_chain;
c5aa993b 14750
c906108c
SS
14751 save_language = current_language->la_language;
14752 save_input_radix = input_radix;
6c95b8df
PA
14753 old_chain = save_current_program_space ();
14754
35df4500 14755 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14756 {
4a64f543 14757 /* Format possible error msg. */
fe3f5fa8 14758 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14759 b->number);
14760 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14761 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14762 do_cleanups (cleanups);
c5aa993b 14763 }
c906108c
SS
14764 set_language (save_language);
14765 input_radix = save_input_radix;
e62c965a 14766
0756c555 14767 jit_breakpoint_re_set ();
4efc6507 14768
6c95b8df
PA
14769 do_cleanups (old_chain);
14770
af02033e
PP
14771 create_overlay_event_breakpoint ();
14772 create_longjmp_master_breakpoint ();
14773 create_std_terminate_master_breakpoint ();
186c406b 14774 create_exception_master_breakpoint ();
c906108c
SS
14775}
14776\f
c906108c
SS
14777/* Reset the thread number of this breakpoint:
14778
14779 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14780 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14781void
fba45db2 14782breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14783{
14784 if (b->thread != -1)
14785 {
39f77062
KB
14786 if (in_thread_list (inferior_ptid))
14787 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14788
14789 /* We're being called after following a fork. The new fork is
14790 selected as current, and unless this was a vfork will have a
14791 different program space from the original thread. Reset that
14792 as well. */
14793 b->loc->pspace = current_program_space;
c906108c
SS
14794 }
14795}
14796
03ac34d5
MS
14797/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14798 If from_tty is nonzero, it prints a message to that effect,
14799 which ends with a period (no newline). */
14800
c906108c 14801void
fba45db2 14802set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14803{
52f0bd74 14804 struct breakpoint *b;
c906108c
SS
14805
14806 if (count < 0)
14807 count = 0;
14808
14809 ALL_BREAKPOINTS (b)
14810 if (b->number == bptnum)
c5aa993b 14811 {
d77f58be
SS
14812 if (is_tracepoint (b))
14813 {
14814 if (from_tty && count != 0)
14815 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14816 bptnum);
14817 return;
14818 }
14819
c5aa993b 14820 b->ignore_count = count;
221ea385
KS
14821 if (from_tty)
14822 {
14823 if (count == 0)
3e43a32a
MS
14824 printf_filtered (_("Will stop next time "
14825 "breakpoint %d is reached."),
221ea385
KS
14826 bptnum);
14827 else if (count == 1)
a3f17187 14828 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14829 bptnum);
14830 else
3e43a32a
MS
14831 printf_filtered (_("Will ignore next %d "
14832 "crossings of breakpoint %d."),
221ea385
KS
14833 count, bptnum);
14834 }
8d3788bd 14835 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14836 return;
14837 }
c906108c 14838
8a3fe4f8 14839 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14840}
14841
c906108c
SS
14842/* Command to set ignore-count of breakpoint N to COUNT. */
14843
14844static void
fba45db2 14845ignore_command (char *args, int from_tty)
c906108c
SS
14846{
14847 char *p = args;
52f0bd74 14848 int num;
c906108c
SS
14849
14850 if (p == 0)
e2e0b3e5 14851 error_no_arg (_("a breakpoint number"));
c5aa993b 14852
c906108c 14853 num = get_number (&p);
5c44784c 14854 if (num == 0)
8a3fe4f8 14855 error (_("bad breakpoint number: '%s'"), args);
c906108c 14856 if (*p == 0)
8a3fe4f8 14857 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14858
14859 set_ignore_count (num,
14860 longest_to_int (value_as_long (parse_and_eval (p))),
14861 from_tty);
221ea385
KS
14862 if (from_tty)
14863 printf_filtered ("\n");
c906108c
SS
14864}
14865\f
14866/* Call FUNCTION on each of the breakpoints
14867 whose numbers are given in ARGS. */
14868
14869static void
95a42b64
TT
14870map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14871 void *),
14872 void *data)
c906108c 14873{
52f0bd74
AC
14874 int num;
14875 struct breakpoint *b, *tmp;
11cf8741 14876 int match;
197f0a60 14877 struct get_number_or_range_state state;
c906108c 14878
b9d61307 14879 if (args == 0 || *args == '\0')
e2e0b3e5 14880 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14881
197f0a60
TT
14882 init_number_or_range (&state, args);
14883
14884 while (!state.finished)
c906108c 14885 {
e799154c 14886 const char *p = state.string;
197f0a60 14887
11cf8741 14888 match = 0;
c5aa993b 14889
197f0a60 14890 num = get_number_or_range (&state);
5c44784c 14891 if (num == 0)
c5aa993b 14892 {
8a3fe4f8 14893 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14894 }
14895 else
14896 {
14897 ALL_BREAKPOINTS_SAFE (b, tmp)
14898 if (b->number == num)
14899 {
11cf8741 14900 match = 1;
cdac0397 14901 function (b, data);
11cf8741 14902 break;
5c44784c 14903 }
11cf8741 14904 if (match == 0)
a3f17187 14905 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14906 }
c906108c
SS
14907 }
14908}
14909
0d381245
VP
14910static struct bp_location *
14911find_location_by_number (char *number)
14912{
14913 char *dot = strchr (number, '.');
14914 char *p1;
14915 int bp_num;
14916 int loc_num;
14917 struct breakpoint *b;
14918 struct bp_location *loc;
14919
14920 *dot = '\0';
14921
14922 p1 = number;
197f0a60 14923 bp_num = get_number (&p1);
0d381245
VP
14924 if (bp_num == 0)
14925 error (_("Bad breakpoint number '%s'"), number);
14926
14927 ALL_BREAKPOINTS (b)
14928 if (b->number == bp_num)
14929 {
14930 break;
14931 }
14932
14933 if (!b || b->number != bp_num)
14934 error (_("Bad breakpoint number '%s'"), number);
14935
14936 p1 = dot+1;
197f0a60 14937 loc_num = get_number (&p1);
0d381245
VP
14938 if (loc_num == 0)
14939 error (_("Bad breakpoint location number '%s'"), number);
14940
14941 --loc_num;
14942 loc = b->loc;
14943 for (;loc_num && loc; --loc_num, loc = loc->next)
14944 ;
14945 if (!loc)
14946 error (_("Bad breakpoint location number '%s'"), dot+1);
14947
14948 return loc;
14949}
14950
14951
1900040c
MS
14952/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14953 If from_tty is nonzero, it prints a message to that effect,
14954 which ends with a period (no newline). */
14955
c906108c 14956void
fba45db2 14957disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14958{
14959 /* Never disable a watchpoint scope breakpoint; we want to
14960 hit them when we leave scope so we can delete both the
14961 watchpoint and its scope breakpoint at that time. */
14962 if (bpt->type == bp_watchpoint_scope)
14963 return;
14964
b5de0fa7 14965 bpt->enable_state = bp_disabled;
c906108c 14966
b775012e
LM
14967 /* Mark breakpoint locations modified. */
14968 mark_breakpoint_modified (bpt);
14969
d248b706
KY
14970 if (target_supports_enable_disable_tracepoint ()
14971 && current_trace_status ()->running && is_tracepoint (bpt))
14972 {
14973 struct bp_location *location;
14974
14975 for (location = bpt->loc; location; location = location->next)
14976 target_disable_tracepoint (location);
14977 }
14978
44702360 14979 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14980
8d3788bd 14981 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14982}
14983
51be5b68
PA
14984/* A callback for iterate_over_related_breakpoints. */
14985
14986static void
14987do_disable_breakpoint (struct breakpoint *b, void *ignore)
14988{
14989 disable_breakpoint (b);
14990}
14991
95a42b64
TT
14992/* A callback for map_breakpoint_numbers that calls
14993 disable_breakpoint. */
14994
14995static void
14996do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14997{
51be5b68 14998 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14999}
15000
c906108c 15001static void
fba45db2 15002disable_command (char *args, int from_tty)
c906108c 15003{
c906108c 15004 if (args == 0)
46c6471b
PA
15005 {
15006 struct breakpoint *bpt;
15007
15008 ALL_BREAKPOINTS (bpt)
15009 if (user_breakpoint_p (bpt))
15010 disable_breakpoint (bpt);
15011 }
9eaabc75 15012 else
0d381245 15013 {
9eaabc75
MW
15014 char *num = extract_arg (&args);
15015
15016 while (num)
d248b706 15017 {
9eaabc75 15018 if (strchr (num, '.'))
b775012e 15019 {
9eaabc75
MW
15020 struct bp_location *loc = find_location_by_number (num);
15021
15022 if (loc)
15023 {
15024 if (loc->enabled)
15025 {
15026 loc->enabled = 0;
15027 mark_breakpoint_location_modified (loc);
15028 }
15029 if (target_supports_enable_disable_tracepoint ()
15030 && current_trace_status ()->running && loc->owner
15031 && is_tracepoint (loc->owner))
15032 target_disable_tracepoint (loc);
15033 }
44702360 15034 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15035 }
9eaabc75
MW
15036 else
15037 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15038 num = extract_arg (&args);
d248b706 15039 }
0d381245 15040 }
c906108c
SS
15041}
15042
15043static void
816338b5
SS
15044enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15045 int count)
c906108c 15046{
afe38095 15047 int target_resources_ok;
c906108c
SS
15048
15049 if (bpt->type == bp_hardware_breakpoint)
15050 {
15051 int i;
c5aa993b 15052 i = hw_breakpoint_used_count ();
53a5351d 15053 target_resources_ok =
d92524f1 15054 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15055 i + 1, 0);
c906108c 15056 if (target_resources_ok == 0)
8a3fe4f8 15057 error (_("No hardware breakpoint support in the target."));
c906108c 15058 else if (target_resources_ok < 0)
8a3fe4f8 15059 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15060 }
15061
cc60f2e3 15062 if (is_watchpoint (bpt))
c906108c 15063 {
d07205c2
JK
15064 /* Initialize it just to avoid a GCC false warning. */
15065 enum enable_state orig_enable_state = 0;
bfd189b1 15066 volatile struct gdb_exception e;
dde02812
ES
15067
15068 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15069 {
3a5c3e22
PA
15070 struct watchpoint *w = (struct watchpoint *) bpt;
15071
1e718ff1
TJB
15072 orig_enable_state = bpt->enable_state;
15073 bpt->enable_state = bp_enabled;
3a5c3e22 15074 update_watchpoint (w, 1 /* reparse */);
c906108c 15075 }
dde02812 15076 if (e.reason < 0)
c5aa993b 15077 {
1e718ff1 15078 bpt->enable_state = orig_enable_state;
dde02812
ES
15079 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15080 bpt->number);
15081 return;
c5aa993b 15082 }
c906108c 15083 }
0101ce28 15084
b775012e
LM
15085 bpt->enable_state = bp_enabled;
15086
15087 /* Mark breakpoint locations modified. */
15088 mark_breakpoint_modified (bpt);
15089
d248b706
KY
15090 if (target_supports_enable_disable_tracepoint ()
15091 && current_trace_status ()->running && is_tracepoint (bpt))
15092 {
15093 struct bp_location *location;
15094
15095 for (location = bpt->loc; location; location = location->next)
15096 target_enable_tracepoint (location);
15097 }
15098
b4c291bb 15099 bpt->disposition = disposition;
816338b5 15100 bpt->enable_count = count;
44702360 15101 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15102
8d3788bd 15103 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15104}
15105
fe3f5fa8 15106
c906108c 15107void
fba45db2 15108enable_breakpoint (struct breakpoint *bpt)
c906108c 15109{
816338b5 15110 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15111}
15112
15113static void
15114do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15115{
15116 enable_breakpoint (bpt);
c906108c
SS
15117}
15118
95a42b64
TT
15119/* A callback for map_breakpoint_numbers that calls
15120 enable_breakpoint. */
15121
15122static void
15123do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15124{
51be5b68 15125 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15126}
15127
c906108c
SS
15128/* The enable command enables the specified breakpoints (or all defined
15129 breakpoints) so they once again become (or continue to be) effective
1272ad14 15130 in stopping the inferior. */
c906108c 15131
c906108c 15132static void
fba45db2 15133enable_command (char *args, int from_tty)
c906108c 15134{
c906108c 15135 if (args == 0)
46c6471b
PA
15136 {
15137 struct breakpoint *bpt;
15138
15139 ALL_BREAKPOINTS (bpt)
15140 if (user_breakpoint_p (bpt))
15141 enable_breakpoint (bpt);
15142 }
9eaabc75 15143 else
0d381245 15144 {
9eaabc75
MW
15145 char *num = extract_arg (&args);
15146
15147 while (num)
d248b706 15148 {
9eaabc75 15149 if (strchr (num, '.'))
b775012e 15150 {
9eaabc75
MW
15151 struct bp_location *loc = find_location_by_number (num);
15152
15153 if (loc)
15154 {
15155 if (!loc->enabled)
15156 {
15157 loc->enabled = 1;
15158 mark_breakpoint_location_modified (loc);
15159 }
15160 if (target_supports_enable_disable_tracepoint ()
15161 && current_trace_status ()->running && loc->owner
15162 && is_tracepoint (loc->owner))
15163 target_enable_tracepoint (loc);
15164 }
44702360 15165 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15166 }
9eaabc75
MW
15167 else
15168 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15169 num = extract_arg (&args);
d248b706 15170 }
0d381245 15171 }
c906108c
SS
15172}
15173
816338b5
SS
15174/* This struct packages up disposition data for application to multiple
15175 breakpoints. */
15176
15177struct disp_data
15178{
15179 enum bpdisp disp;
15180 int count;
15181};
15182
c906108c 15183static void
51be5b68
PA
15184do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15185{
816338b5 15186 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15187
816338b5 15188 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15189}
15190
15191static void
15192do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15193{
816338b5 15194 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15195
15196 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15197}
15198
c906108c 15199static void
fba45db2 15200enable_once_command (char *args, int from_tty)
c906108c 15201{
51be5b68 15202 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15203}
15204
816338b5
SS
15205static void
15206do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15207{
15208 struct disp_data disp = { disp_disable, *(int *) countptr };
15209
15210 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15211}
15212
15213static void
15214enable_count_command (char *args, int from_tty)
15215{
b9d61307
SM
15216 int count;
15217
15218 if (args == NULL)
15219 error_no_arg (_("hit count"));
15220
15221 count = get_number (&args);
816338b5
SS
15222
15223 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15224}
15225
c906108c 15226static void
51be5b68 15227do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15228{
816338b5 15229 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15230
15231 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15232}
15233
c906108c 15234static void
fba45db2 15235enable_delete_command (char *args, int from_tty)
c906108c 15236{
51be5b68 15237 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15238}
15239\f
fa8d40ab
JJ
15240static void
15241set_breakpoint_cmd (char *args, int from_tty)
15242{
15243}
15244
15245static void
15246show_breakpoint_cmd (char *args, int from_tty)
15247{
15248}
15249
1f3b5d1b
PP
15250/* Invalidate last known value of any hardware watchpoint if
15251 the memory which that value represents has been written to by
15252 GDB itself. */
15253
15254static void
8de0566d
YQ
15255invalidate_bp_value_on_memory_change (struct inferior *inferior,
15256 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15257 const bfd_byte *data)
15258{
15259 struct breakpoint *bp;
15260
15261 ALL_BREAKPOINTS (bp)
15262 if (bp->enable_state == bp_enabled
3a5c3e22 15263 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15264 {
3a5c3e22 15265 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15266
3a5c3e22
PA
15267 if (wp->val_valid && wp->val)
15268 {
15269 struct bp_location *loc;
15270
15271 for (loc = bp->loc; loc != NULL; loc = loc->next)
15272 if (loc->loc_type == bp_loc_hardware_watchpoint
15273 && loc->address + loc->length > addr
15274 && addr + len > loc->address)
15275 {
15276 value_free (wp->val);
15277 wp->val = NULL;
15278 wp->val_valid = 0;
15279 }
15280 }
1f3b5d1b
PP
15281 }
15282}
15283
8181d85f
DJ
15284/* Create and insert a breakpoint for software single step. */
15285
15286void
6c95b8df 15287insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15288 struct address_space *aspace,
15289 CORE_ADDR next_pc)
8181d85f 15290{
7c16b83e
PA
15291 struct thread_info *tp = inferior_thread ();
15292 struct symtab_and_line sal;
15293 CORE_ADDR pc = next_pc;
8181d85f 15294
34b7e8a6
PA
15295 if (tp->control.single_step_breakpoints == NULL)
15296 {
15297 tp->control.single_step_breakpoints
15298 = new_single_step_breakpoint (tp->num, gdbarch);
15299 }
8181d85f 15300
7c16b83e
PA
15301 sal = find_pc_line (pc, 0);
15302 sal.pc = pc;
15303 sal.section = find_pc_overlay (pc);
15304 sal.explicit_pc = 1;
34b7e8a6 15305 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15306
7c16b83e 15307 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15308}
15309
34b7e8a6 15310/* See breakpoint.h. */
f02253f1
HZ
15311
15312int
7c16b83e
PA
15313breakpoint_has_location_inserted_here (struct breakpoint *bp,
15314 struct address_space *aspace,
15315 CORE_ADDR pc)
1aafd4da 15316{
7c16b83e 15317 struct bp_location *loc;
1aafd4da 15318
7c16b83e
PA
15319 for (loc = bp->loc; loc != NULL; loc = loc->next)
15320 if (loc->inserted
15321 && breakpoint_location_address_match (loc, aspace, pc))
15322 return 1;
1aafd4da 15323
7c16b83e 15324 return 0;
ef370185
JB
15325}
15326
15327/* Check whether a software single-step breakpoint is inserted at
15328 PC. */
15329
15330int
15331single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15332 CORE_ADDR pc)
15333{
34b7e8a6
PA
15334 struct breakpoint *bpt;
15335
15336 ALL_BREAKPOINTS (bpt)
15337 {
15338 if (bpt->type == bp_single_step
15339 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15340 return 1;
15341 }
15342 return 0;
1aafd4da
UW
15343}
15344
a96d9b2e
SDJ
15345/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15346 non-zero otherwise. */
15347static int
15348is_syscall_catchpoint_enabled (struct breakpoint *bp)
15349{
15350 if (syscall_catchpoint_p (bp)
15351 && bp->enable_state != bp_disabled
15352 && bp->enable_state != bp_call_disabled)
15353 return 1;
15354 else
15355 return 0;
15356}
15357
15358int
15359catch_syscall_enabled (void)
15360{
fa3064dd
YQ
15361 struct catch_syscall_inferior_data *inf_data
15362 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15363
fa3064dd 15364 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15365}
15366
15367int
15368catching_syscall_number (int syscall_number)
15369{
15370 struct breakpoint *bp;
15371
15372 ALL_BREAKPOINTS (bp)
15373 if (is_syscall_catchpoint_enabled (bp))
15374 {
be5c67c1
PA
15375 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15376
15377 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15378 {
15379 int i, iter;
15380 for (i = 0;
be5c67c1 15381 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15382 i++)
15383 if (syscall_number == iter)
15384 return 1;
15385 }
15386 else
15387 return 1;
15388 }
15389
15390 return 0;
15391}
15392
15393/* Complete syscall names. Used by "catch syscall". */
49c4e619 15394static VEC (char_ptr) *
a96d9b2e 15395catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15396 const char *text, const char *word)
a96d9b2e 15397{
458c8db8 15398 const char **list = get_syscall_names (get_current_arch ());
49c4e619 15399 VEC (char_ptr) *retlist
b45627a0 15400 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15401
c38eea1a
MS
15402 xfree (list);
15403 return retlist;
a96d9b2e
SDJ
15404}
15405
1042e4c0
SS
15406/* Tracepoint-specific operations. */
15407
15408/* Set tracepoint count to NUM. */
15409static void
15410set_tracepoint_count (int num)
15411{
15412 tracepoint_count = num;
4fa62494 15413 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15414}
15415
70221824 15416static void
1042e4c0
SS
15417trace_command (char *arg, int from_tty)
15418{
55aa24fb
SDJ
15419 struct breakpoint_ops *ops;
15420 const char *arg_cp = arg;
15421
15422 if (arg && probe_linespec_to_ops (&arg_cp))
15423 ops = &tracepoint_probe_breakpoint_ops;
15424 else
15425 ops = &tracepoint_breakpoint_ops;
15426
558a9d82
YQ
15427 create_breakpoint (get_current_arch (),
15428 arg,
15429 NULL, 0, NULL, 1 /* parse arg */,
15430 0 /* tempflag */,
15431 bp_tracepoint /* type_wanted */,
15432 0 /* Ignore count */,
15433 pending_break_support,
15434 ops,
15435 from_tty,
15436 1 /* enabled */,
15437 0 /* internal */, 0);
1042e4c0
SS
15438}
15439
70221824 15440static void
7a697b8d
SS
15441ftrace_command (char *arg, int from_tty)
15442{
558a9d82
YQ
15443 create_breakpoint (get_current_arch (),
15444 arg,
15445 NULL, 0, NULL, 1 /* parse arg */,
15446 0 /* tempflag */,
15447 bp_fast_tracepoint /* type_wanted */,
15448 0 /* Ignore count */,
15449 pending_break_support,
15450 &tracepoint_breakpoint_ops,
15451 from_tty,
15452 1 /* enabled */,
15453 0 /* internal */, 0);
0fb4aa4b
PA
15454}
15455
15456/* strace command implementation. Creates a static tracepoint. */
15457
70221824 15458static void
0fb4aa4b
PA
15459strace_command (char *arg, int from_tty)
15460{
983af33b
SDJ
15461 struct breakpoint_ops *ops;
15462
15463 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15464 or with a normal static tracepoint. */
15465 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15466 ops = &strace_marker_breakpoint_ops;
15467 else
15468 ops = &tracepoint_breakpoint_ops;
15469
558a9d82
YQ
15470 create_breakpoint (get_current_arch (),
15471 arg,
15472 NULL, 0, NULL, 1 /* parse arg */,
15473 0 /* tempflag */,
15474 bp_static_tracepoint /* type_wanted */,
15475 0 /* Ignore count */,
15476 pending_break_support,
15477 ops,
15478 from_tty,
15479 1 /* enabled */,
15480 0 /* internal */, 0);
7a697b8d
SS
15481}
15482
409873ef
SS
15483/* Set up a fake reader function that gets command lines from a linked
15484 list that was acquired during tracepoint uploading. */
15485
15486static struct uploaded_tp *this_utp;
3149d8c1 15487static int next_cmd;
409873ef
SS
15488
15489static char *
15490read_uploaded_action (void)
15491{
15492 char *rslt;
15493
3149d8c1 15494 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15495
3149d8c1 15496 next_cmd++;
409873ef
SS
15497
15498 return rslt;
15499}
15500
00bf0b85
SS
15501/* Given information about a tracepoint as recorded on a target (which
15502 can be either a live system or a trace file), attempt to create an
15503 equivalent GDB tracepoint. This is not a reliable process, since
15504 the target does not necessarily have all the information used when
15505 the tracepoint was originally defined. */
15506
d9b3f62e 15507struct tracepoint *
00bf0b85 15508create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15509{
409873ef 15510 char *addr_str, small_buf[100];
d9b3f62e 15511 struct tracepoint *tp;
fd9b8c24 15512
409873ef
SS
15513 if (utp->at_string)
15514 addr_str = utp->at_string;
15515 else
15516 {
15517 /* In the absence of a source location, fall back to raw
15518 address. Since there is no way to confirm that the address
15519 means the same thing as when the trace was started, warn the
15520 user. */
3e43a32a
MS
15521 warning (_("Uploaded tracepoint %d has no "
15522 "source location, using raw address"),
409873ef 15523 utp->number);
8c042590 15524 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15525 addr_str = small_buf;
15526 }
15527
15528 /* There's not much we can do with a sequence of bytecodes. */
15529 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15530 warning (_("Uploaded tracepoint %d condition "
15531 "has no source form, ignoring it"),
409873ef 15532 utp->number);
d5551862 15533
8cdf0e15 15534 if (!create_breakpoint (get_current_arch (),
409873ef 15535 addr_str,
e7e0cddf
SS
15536 utp->cond_string, -1, NULL,
15537 0 /* parse cond/thread */,
8cdf0e15 15538 0 /* tempflag */,
0fb4aa4b 15539 utp->type /* type_wanted */,
8cdf0e15
VP
15540 0 /* Ignore count */,
15541 pending_break_support,
348d480f 15542 &tracepoint_breakpoint_ops,
8cdf0e15 15543 0 /* from_tty */,
84f4c1fe 15544 utp->enabled /* enabled */,
44f238bb
PA
15545 0 /* internal */,
15546 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15547 return NULL;
15548
409873ef 15549 /* Get the tracepoint we just created. */
fd9b8c24
PA
15550 tp = get_tracepoint (tracepoint_count);
15551 gdb_assert (tp != NULL);
d5551862 15552
00bf0b85
SS
15553 if (utp->pass > 0)
15554 {
8c042590
PM
15555 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15556 tp->base.number);
00bf0b85 15557
409873ef 15558 trace_pass_command (small_buf, 0);
00bf0b85
SS
15559 }
15560
409873ef
SS
15561 /* If we have uploaded versions of the original commands, set up a
15562 special-purpose "reader" function and call the usual command line
15563 reader, then pass the result to the breakpoint command-setting
15564 function. */
3149d8c1 15565 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15566 {
409873ef 15567 struct command_line *cmd_list;
00bf0b85 15568
409873ef 15569 this_utp = utp;
3149d8c1 15570 next_cmd = 0;
d5551862 15571
409873ef
SS
15572 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15573
d9b3f62e 15574 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15575 }
3149d8c1
SS
15576 else if (!VEC_empty (char_ptr, utp->actions)
15577 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15578 warning (_("Uploaded tracepoint %d actions "
15579 "have no source form, ignoring them"),
409873ef 15580 utp->number);
00bf0b85 15581
f196051f
SS
15582 /* Copy any status information that might be available. */
15583 tp->base.hit_count = utp->hit_count;
15584 tp->traceframe_usage = utp->traceframe_usage;
15585
00bf0b85 15586 return tp;
d9b3f62e 15587}
00bf0b85 15588
1042e4c0
SS
15589/* Print information on tracepoint number TPNUM_EXP, or all if
15590 omitted. */
15591
15592static void
e5a67952 15593tracepoints_info (char *args, int from_tty)
1042e4c0 15594{
79a45e25 15595 struct ui_out *uiout = current_uiout;
e5a67952 15596 int num_printed;
1042e4c0 15597
e5a67952 15598 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15599
15600 if (num_printed == 0)
1042e4c0 15601 {
e5a67952 15602 if (args == NULL || *args == '\0')
d77f58be
SS
15603 ui_out_message (uiout, 0, "No tracepoints.\n");
15604 else
e5a67952 15605 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15606 }
ad443146
SS
15607
15608 default_collect_info ();
1042e4c0
SS
15609}
15610
4a64f543 15611/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15612 Not supported by all targets. */
15613static void
15614enable_trace_command (char *args, int from_tty)
15615{
15616 enable_command (args, from_tty);
15617}
15618
4a64f543 15619/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15620 Not supported by all targets. */
15621static void
15622disable_trace_command (char *args, int from_tty)
15623{
15624 disable_command (args, from_tty);
15625}
15626
4a64f543 15627/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15628static void
15629delete_trace_command (char *arg, int from_tty)
15630{
35df4500 15631 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15632
15633 dont_repeat ();
15634
15635 if (arg == 0)
15636 {
15637 int breaks_to_delete = 0;
15638
15639 /* Delete all breakpoints if no argument.
15640 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15641 have to be deleted with an explicit breakpoint number
15642 argument. */
1042e4c0 15643 ALL_TRACEPOINTS (b)
46c6471b 15644 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15645 {
15646 breaks_to_delete = 1;
15647 break;
15648 }
1042e4c0
SS
15649
15650 /* Ask user only if there are some breakpoints to delete. */
15651 if (!from_tty
15652 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15653 {
35df4500 15654 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15655 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15656 delete_breakpoint (b);
1042e4c0
SS
15657 }
15658 }
15659 else
51be5b68 15660 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15661}
15662
197f0a60
TT
15663/* Helper function for trace_pass_command. */
15664
15665static void
d9b3f62e 15666trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15667{
d9b3f62e 15668 tp->pass_count = count;
6f6484cd 15669 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15670 if (from_tty)
15671 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15672 tp->base.number, count);
197f0a60
TT
15673}
15674
1042e4c0
SS
15675/* Set passcount for tracepoint.
15676
15677 First command argument is passcount, second is tracepoint number.
15678 If tracepoint number omitted, apply to most recently defined.
15679 Also accepts special argument "all". */
15680
15681static void
15682trace_pass_command (char *args, int from_tty)
15683{
d9b3f62e 15684 struct tracepoint *t1;
1042e4c0 15685 unsigned int count;
1042e4c0
SS
15686
15687 if (args == 0 || *args == 0)
3e43a32a
MS
15688 error (_("passcount command requires an "
15689 "argument (count + optional TP num)"));
1042e4c0 15690
4a64f543 15691 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15692
529480d0 15693 args = skip_spaces (args);
1042e4c0
SS
15694 if (*args && strncasecmp (args, "all", 3) == 0)
15695 {
d9b3f62e
PA
15696 struct breakpoint *b;
15697
1042e4c0 15698 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15699 if (*args)
15700 error (_("Junk at end of arguments."));
1042e4c0 15701
d9b3f62e 15702 ALL_TRACEPOINTS (b)
197f0a60 15703 {
d9b3f62e 15704 t1 = (struct tracepoint *) b;
197f0a60
TT
15705 trace_pass_set_count (t1, count, from_tty);
15706 }
15707 }
15708 else if (*args == '\0')
1042e4c0 15709 {
5fa1d40e 15710 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15711 if (t1)
197f0a60
TT
15712 trace_pass_set_count (t1, count, from_tty);
15713 }
15714 else
15715 {
15716 struct get_number_or_range_state state;
15717
15718 init_number_or_range (&state, args);
15719 while (!state.finished)
1042e4c0 15720 {
5fa1d40e 15721 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15722 if (t1)
15723 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15724 }
15725 }
1042e4c0
SS
15726}
15727
d9b3f62e 15728struct tracepoint *
1042e4c0
SS
15729get_tracepoint (int num)
15730{
15731 struct breakpoint *t;
15732
15733 ALL_TRACEPOINTS (t)
15734 if (t->number == num)
d9b3f62e 15735 return (struct tracepoint *) t;
1042e4c0
SS
15736
15737 return NULL;
15738}
15739
d5551862
SS
15740/* Find the tracepoint with the given target-side number (which may be
15741 different from the tracepoint number after disconnecting and
15742 reconnecting). */
15743
d9b3f62e 15744struct tracepoint *
d5551862
SS
15745get_tracepoint_by_number_on_target (int num)
15746{
d9b3f62e 15747 struct breakpoint *b;
d5551862 15748
d9b3f62e
PA
15749 ALL_TRACEPOINTS (b)
15750 {
15751 struct tracepoint *t = (struct tracepoint *) b;
15752
15753 if (t->number_on_target == num)
15754 return t;
15755 }
d5551862
SS
15756
15757 return NULL;
15758}
15759
1042e4c0 15760/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15761 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15762 If the argument is missing, the most recent tracepoint
15763 (tracepoint_count) is returned. */
15764
d9b3f62e 15765struct tracepoint *
197f0a60 15766get_tracepoint_by_number (char **arg,
5fa1d40e 15767 struct get_number_or_range_state *state)
1042e4c0 15768{
1042e4c0
SS
15769 struct breakpoint *t;
15770 int tpnum;
15771 char *instring = arg == NULL ? NULL : *arg;
15772
197f0a60
TT
15773 if (state)
15774 {
15775 gdb_assert (!state->finished);
15776 tpnum = get_number_or_range (state);
15777 }
15778 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15779 tpnum = tracepoint_count;
1042e4c0 15780 else
197f0a60 15781 tpnum = get_number (arg);
1042e4c0
SS
15782
15783 if (tpnum <= 0)
15784 {
15785 if (instring && *instring)
15786 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15787 instring);
15788 else
5fa1d40e 15789 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15790 return NULL;
15791 }
15792
15793 ALL_TRACEPOINTS (t)
15794 if (t->number == tpnum)
15795 {
d9b3f62e 15796 return (struct tracepoint *) t;
1042e4c0
SS
15797 }
15798
1042e4c0
SS
15799 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15800 return NULL;
15801}
15802
d9b3f62e
PA
15803void
15804print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15805{
15806 if (b->thread != -1)
15807 fprintf_unfiltered (fp, " thread %d", b->thread);
15808
15809 if (b->task != 0)
15810 fprintf_unfiltered (fp, " task %d", b->task);
15811
15812 fprintf_unfiltered (fp, "\n");
15813}
15814
6149aea9
PA
15815/* Save information on user settable breakpoints (watchpoints, etc) to
15816 a new script file named FILENAME. If FILTER is non-NULL, call it
15817 on each breakpoint and only include the ones for which it returns
15818 non-zero. */
15819
1042e4c0 15820static void
6149aea9
PA
15821save_breakpoints (char *filename, int from_tty,
15822 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15823{
15824 struct breakpoint *tp;
6149aea9 15825 int any = 0;
1042e4c0 15826 struct cleanup *cleanup;
a7bdde9e 15827 struct ui_file *fp;
6149aea9 15828 int extra_trace_bits = 0;
1042e4c0 15829
6149aea9
PA
15830 if (filename == 0 || *filename == 0)
15831 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15832
15833 /* See if we have anything to save. */
6149aea9 15834 ALL_BREAKPOINTS (tp)
1042e4c0 15835 {
6149aea9 15836 /* Skip internal and momentary breakpoints. */
09d682a4 15837 if (!user_breakpoint_p (tp))
6149aea9
PA
15838 continue;
15839
15840 /* If we have a filter, only save the breakpoints it accepts. */
15841 if (filter && !filter (tp))
15842 continue;
15843
15844 any = 1;
15845
15846 if (is_tracepoint (tp))
15847 {
15848 extra_trace_bits = 1;
15849
15850 /* We can stop searching. */
15851 break;
15852 }
1042e4c0 15853 }
6149aea9
PA
15854
15855 if (!any)
1042e4c0 15856 {
6149aea9 15857 warning (_("Nothing to save."));
1042e4c0
SS
15858 return;
15859 }
15860
c718be47
PA
15861 filename = tilde_expand (filename);
15862 cleanup = make_cleanup (xfree, filename);
15863 fp = gdb_fopen (filename, "w");
059fb39f 15864 if (!fp)
6149aea9
PA
15865 error (_("Unable to open file '%s' for saving (%s)"),
15866 filename, safe_strerror (errno));
a7bdde9e 15867 make_cleanup_ui_file_delete (fp);
8bf6485c 15868
6149aea9
PA
15869 if (extra_trace_bits)
15870 save_trace_state_variables (fp);
8bf6485c 15871
6149aea9 15872 ALL_BREAKPOINTS (tp)
1042e4c0 15873 {
6149aea9 15874 /* Skip internal and momentary breakpoints. */
09d682a4 15875 if (!user_breakpoint_p (tp))
6149aea9 15876 continue;
8bf6485c 15877
6149aea9
PA
15878 /* If we have a filter, only save the breakpoints it accepts. */
15879 if (filter && !filter (tp))
15880 continue;
15881
348d480f 15882 tp->ops->print_recreate (tp, fp);
1042e4c0 15883
6149aea9
PA
15884 /* Note, we can't rely on tp->number for anything, as we can't
15885 assume the recreated breakpoint numbers will match. Use $bpnum
15886 instead. */
15887
15888 if (tp->cond_string)
15889 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15890
15891 if (tp->ignore_count)
15892 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15893
2d9442cc 15894 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15895 {
a7bdde9e
VP
15896 volatile struct gdb_exception ex;
15897
6149aea9 15898 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15899
79a45e25 15900 ui_out_redirect (current_uiout, fp);
14dba4b4 15901 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15902 {
79a45e25 15903 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15904 }
79a45e25 15905 ui_out_redirect (current_uiout, NULL);
1042e4c0 15906
a7bdde9e
VP
15907 if (ex.reason < 0)
15908 throw_exception (ex);
1042e4c0 15909
a7bdde9e 15910 fprintf_unfiltered (fp, " end\n");
1042e4c0 15911 }
6149aea9
PA
15912
15913 if (tp->enable_state == bp_disabled)
99894e11 15914 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15915
15916 /* If this is a multi-location breakpoint, check if the locations
15917 should be individually disabled. Watchpoint locations are
15918 special, and not user visible. */
15919 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15920 {
15921 struct bp_location *loc;
15922 int n = 1;
15923
15924 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15925 if (!loc->enabled)
15926 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15927 }
1042e4c0 15928 }
8bf6485c 15929
6149aea9 15930 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15931 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15932
1042e4c0 15933 if (from_tty)
6149aea9 15934 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15935 do_cleanups (cleanup);
6149aea9
PA
15936}
15937
15938/* The `save breakpoints' command. */
15939
15940static void
15941save_breakpoints_command (char *args, int from_tty)
15942{
15943 save_breakpoints (args, from_tty, NULL);
15944}
15945
15946/* The `save tracepoints' command. */
15947
15948static void
15949save_tracepoints_command (char *args, int from_tty)
15950{
15951 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15952}
15953
15954/* Create a vector of all tracepoints. */
15955
15956VEC(breakpoint_p) *
eeae04df 15957all_tracepoints (void)
1042e4c0
SS
15958{
15959 VEC(breakpoint_p) *tp_vec = 0;
15960 struct breakpoint *tp;
15961
15962 ALL_TRACEPOINTS (tp)
15963 {
15964 VEC_safe_push (breakpoint_p, tp_vec, tp);
15965 }
15966
15967 return tp_vec;
15968}
15969
c906108c 15970\f
4a64f543
MS
15971/* This help string is used for the break, hbreak, tbreak and thbreak
15972 commands. It is defined as a macro to prevent duplication.
15973 COMMAND should be a string constant containing the name of the
15974 command. */
31e2b00f 15975#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15976command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15977PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15978probe point. Accepted values are `-probe' (for a generic, automatically\n\
15979guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15980LOCATION may be a line number, function name, or \"*\" and an address.\n\
15981If a line number is specified, break at start of code for that line.\n\
15982If a function is specified, break at start of code for that function.\n\
15983If an address is specified, break at that exact address.\n\
dc10affe
PA
15984With no LOCATION, uses current execution address of the selected\n\
15985stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15986\n\
15987THREADNUM is the number from \"info threads\".\n\
15988CONDITION is a boolean expression.\n\
15989\n\
d41c0fc8
PA
15990Multiple breakpoints at one place are permitted, and useful if their\n\
15991conditions are different.\n\
31e2b00f
AS
15992\n\
15993Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15994
44feb3ce
TT
15995/* List of subcommands for "catch". */
15996static struct cmd_list_element *catch_cmdlist;
15997
15998/* List of subcommands for "tcatch". */
15999static struct cmd_list_element *tcatch_cmdlist;
16000
9ac4176b 16001void
44feb3ce 16002add_catch_command (char *name, char *docstring,
82ae6c8d 16003 cmd_sfunc_ftype *sfunc,
625e8578 16004 completer_ftype *completer,
44feb3ce
TT
16005 void *user_data_catch,
16006 void *user_data_tcatch)
16007{
16008 struct cmd_list_element *command;
16009
16010 command = add_cmd (name, class_breakpoint, NULL, docstring,
16011 &catch_cmdlist);
16012 set_cmd_sfunc (command, sfunc);
16013 set_cmd_context (command, user_data_catch);
a96d9b2e 16014 set_cmd_completer (command, completer);
44feb3ce
TT
16015
16016 command = add_cmd (name, class_breakpoint, NULL, docstring,
16017 &tcatch_cmdlist);
16018 set_cmd_sfunc (command, sfunc);
16019 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16020 set_cmd_completer (command, completer);
44feb3ce
TT
16021}
16022
6c95b8df 16023static void
a79b8f6e 16024clear_syscall_counts (struct inferior *inf)
6c95b8df 16025{
fa3064dd
YQ
16026 struct catch_syscall_inferior_data *inf_data
16027 = get_catch_syscall_inferior_data (inf);
16028
16029 inf_data->total_syscalls_count = 0;
16030 inf_data->any_syscall_count = 0;
16031 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16032}
16033
6149aea9
PA
16034static void
16035save_command (char *arg, int from_tty)
16036{
3e43a32a
MS
16037 printf_unfiltered (_("\"save\" must be followed by "
16038 "the name of a save subcommand.\n"));
635c7e8a 16039 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16040}
16041
84f4c1fe
PM
16042struct breakpoint *
16043iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16044 void *data)
16045{
35df4500 16046 struct breakpoint *b, *b_tmp;
84f4c1fe 16047
35df4500 16048 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16049 {
16050 if ((*callback) (b, data))
16051 return b;
16052 }
16053
16054 return NULL;
16055}
16056
0574c78f
GB
16057/* Zero if any of the breakpoint's locations could be a location where
16058 functions have been inlined, nonzero otherwise. */
16059
16060static int
16061is_non_inline_function (struct breakpoint *b)
16062{
16063 /* The shared library event breakpoint is set on the address of a
16064 non-inline function. */
16065 if (b->type == bp_shlib_event)
16066 return 1;
16067
16068 return 0;
16069}
16070
16071/* Nonzero if the specified PC cannot be a location where functions
16072 have been inlined. */
16073
16074int
09ac7c10
TT
16075pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16076 const struct target_waitstatus *ws)
0574c78f
GB
16077{
16078 struct breakpoint *b;
16079 struct bp_location *bl;
16080
16081 ALL_BREAKPOINTS (b)
16082 {
16083 if (!is_non_inline_function (b))
16084 continue;
16085
16086 for (bl = b->loc; bl != NULL; bl = bl->next)
16087 {
16088 if (!bl->shlib_disabled
09ac7c10 16089 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16090 return 1;
16091 }
16092 }
16093
16094 return 0;
16095}
16096
2f202fde
JK
16097/* Remove any references to OBJFILE which is going to be freed. */
16098
16099void
16100breakpoint_free_objfile (struct objfile *objfile)
16101{
16102 struct bp_location **locp, *loc;
16103
16104 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 16105 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
16106 loc->symtab = NULL;
16107}
16108
2060206e
PA
16109void
16110initialize_breakpoint_ops (void)
16111{
16112 static int initialized = 0;
16113
16114 struct breakpoint_ops *ops;
16115
16116 if (initialized)
16117 return;
16118 initialized = 1;
16119
16120 /* The breakpoint_ops structure to be inherit by all kinds of
16121 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16122 internal and momentary breakpoints, etc.). */
16123 ops = &bkpt_base_breakpoint_ops;
16124 *ops = base_breakpoint_ops;
16125 ops->re_set = bkpt_re_set;
16126 ops->insert_location = bkpt_insert_location;
16127 ops->remove_location = bkpt_remove_location;
16128 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16129 ops->create_sals_from_address = bkpt_create_sals_from_address;
16130 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16131 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16132
16133 /* The breakpoint_ops structure to be used in regular breakpoints. */
16134 ops = &bkpt_breakpoint_ops;
16135 *ops = bkpt_base_breakpoint_ops;
16136 ops->re_set = bkpt_re_set;
16137 ops->resources_needed = bkpt_resources_needed;
16138 ops->print_it = bkpt_print_it;
16139 ops->print_mention = bkpt_print_mention;
16140 ops->print_recreate = bkpt_print_recreate;
16141
16142 /* Ranged breakpoints. */
16143 ops = &ranged_breakpoint_ops;
16144 *ops = bkpt_breakpoint_ops;
16145 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16146 ops->resources_needed = resources_needed_ranged_breakpoint;
16147 ops->print_it = print_it_ranged_breakpoint;
16148 ops->print_one = print_one_ranged_breakpoint;
16149 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16150 ops->print_mention = print_mention_ranged_breakpoint;
16151 ops->print_recreate = print_recreate_ranged_breakpoint;
16152
16153 /* Internal breakpoints. */
16154 ops = &internal_breakpoint_ops;
16155 *ops = bkpt_base_breakpoint_ops;
16156 ops->re_set = internal_bkpt_re_set;
16157 ops->check_status = internal_bkpt_check_status;
16158 ops->print_it = internal_bkpt_print_it;
16159 ops->print_mention = internal_bkpt_print_mention;
16160
16161 /* Momentary breakpoints. */
16162 ops = &momentary_breakpoint_ops;
16163 *ops = bkpt_base_breakpoint_ops;
16164 ops->re_set = momentary_bkpt_re_set;
16165 ops->check_status = momentary_bkpt_check_status;
16166 ops->print_it = momentary_bkpt_print_it;
16167 ops->print_mention = momentary_bkpt_print_mention;
16168
e2e4d78b
JK
16169 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16170 ops = &longjmp_breakpoint_ops;
16171 *ops = momentary_breakpoint_ops;
16172 ops->dtor = longjmp_bkpt_dtor;
16173
55aa24fb
SDJ
16174 /* Probe breakpoints. */
16175 ops = &bkpt_probe_breakpoint_ops;
16176 *ops = bkpt_breakpoint_ops;
16177 ops->insert_location = bkpt_probe_insert_location;
16178 ops->remove_location = bkpt_probe_remove_location;
16179 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16180 ops->decode_linespec = bkpt_probe_decode_linespec;
16181
2060206e
PA
16182 /* Watchpoints. */
16183 ops = &watchpoint_breakpoint_ops;
16184 *ops = base_breakpoint_ops;
3a5c3e22 16185 ops->dtor = dtor_watchpoint;
2060206e
PA
16186 ops->re_set = re_set_watchpoint;
16187 ops->insert_location = insert_watchpoint;
16188 ops->remove_location = remove_watchpoint;
16189 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16190 ops->check_status = check_status_watchpoint;
16191 ops->resources_needed = resources_needed_watchpoint;
16192 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16193 ops->print_it = print_it_watchpoint;
16194 ops->print_mention = print_mention_watchpoint;
16195 ops->print_recreate = print_recreate_watchpoint;
427cd150 16196 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16197
16198 /* Masked watchpoints. */
16199 ops = &masked_watchpoint_breakpoint_ops;
16200 *ops = watchpoint_breakpoint_ops;
16201 ops->insert_location = insert_masked_watchpoint;
16202 ops->remove_location = remove_masked_watchpoint;
16203 ops->resources_needed = resources_needed_masked_watchpoint;
16204 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16205 ops->print_it = print_it_masked_watchpoint;
16206 ops->print_one_detail = print_one_detail_masked_watchpoint;
16207 ops->print_mention = print_mention_masked_watchpoint;
16208 ops->print_recreate = print_recreate_masked_watchpoint;
16209
16210 /* Tracepoints. */
16211 ops = &tracepoint_breakpoint_ops;
16212 *ops = base_breakpoint_ops;
16213 ops->re_set = tracepoint_re_set;
16214 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16215 ops->print_one_detail = tracepoint_print_one_detail;
16216 ops->print_mention = tracepoint_print_mention;
16217 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16218 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16219 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16220 ops->decode_linespec = tracepoint_decode_linespec;
16221
55aa24fb
SDJ
16222 /* Probe tracepoints. */
16223 ops = &tracepoint_probe_breakpoint_ops;
16224 *ops = tracepoint_breakpoint_ops;
16225 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16226 ops->decode_linespec = tracepoint_probe_decode_linespec;
16227
983af33b
SDJ
16228 /* Static tracepoints with marker (`-m'). */
16229 ops = &strace_marker_breakpoint_ops;
16230 *ops = tracepoint_breakpoint_ops;
16231 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16232 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16233 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16234
16235 /* Fork catchpoints. */
16236 ops = &catch_fork_breakpoint_ops;
16237 *ops = base_breakpoint_ops;
16238 ops->insert_location = insert_catch_fork;
16239 ops->remove_location = remove_catch_fork;
16240 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16241 ops->print_it = print_it_catch_fork;
16242 ops->print_one = print_one_catch_fork;
16243 ops->print_mention = print_mention_catch_fork;
16244 ops->print_recreate = print_recreate_catch_fork;
16245
16246 /* Vfork catchpoints. */
16247 ops = &catch_vfork_breakpoint_ops;
16248 *ops = base_breakpoint_ops;
16249 ops->insert_location = insert_catch_vfork;
16250 ops->remove_location = remove_catch_vfork;
16251 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16252 ops->print_it = print_it_catch_vfork;
16253 ops->print_one = print_one_catch_vfork;
16254 ops->print_mention = print_mention_catch_vfork;
16255 ops->print_recreate = print_recreate_catch_vfork;
16256
16257 /* Exec catchpoints. */
16258 ops = &catch_exec_breakpoint_ops;
16259 *ops = base_breakpoint_ops;
16260 ops->dtor = dtor_catch_exec;
16261 ops->insert_location = insert_catch_exec;
16262 ops->remove_location = remove_catch_exec;
16263 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16264 ops->print_it = print_it_catch_exec;
16265 ops->print_one = print_one_catch_exec;
16266 ops->print_mention = print_mention_catch_exec;
16267 ops->print_recreate = print_recreate_catch_exec;
16268
16269 /* Syscall catchpoints. */
16270 ops = &catch_syscall_breakpoint_ops;
16271 *ops = base_breakpoint_ops;
16272 ops->dtor = dtor_catch_syscall;
16273 ops->insert_location = insert_catch_syscall;
16274 ops->remove_location = remove_catch_syscall;
16275 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16276 ops->print_it = print_it_catch_syscall;
16277 ops->print_one = print_one_catch_syscall;
16278 ops->print_mention = print_mention_catch_syscall;
16279 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16280
16281 /* Solib-related catchpoints. */
16282 ops = &catch_solib_breakpoint_ops;
16283 *ops = base_breakpoint_ops;
16284 ops->dtor = dtor_catch_solib;
16285 ops->insert_location = insert_catch_solib;
16286 ops->remove_location = remove_catch_solib;
16287 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16288 ops->check_status = check_status_catch_solib;
16289 ops->print_it = print_it_catch_solib;
16290 ops->print_one = print_one_catch_solib;
16291 ops->print_mention = print_mention_catch_solib;
16292 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16293
16294 ops = &dprintf_breakpoint_ops;
16295 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16296 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16297 ops->resources_needed = bkpt_resources_needed;
16298 ops->print_it = bkpt_print_it;
16299 ops->print_mention = bkpt_print_mention;
2d9442cc 16300 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16301 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16302 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16303}
16304
8bfd80db
YQ
16305/* Chain containing all defined "enable breakpoint" subcommands. */
16306
16307static struct cmd_list_element *enablebreaklist = NULL;
16308
c906108c 16309void
fba45db2 16310_initialize_breakpoint (void)
c906108c
SS
16311{
16312 struct cmd_list_element *c;
16313
2060206e
PA
16314 initialize_breakpoint_ops ();
16315
84acb35a 16316 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16317 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16318 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16319 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16320
55aa24fb
SDJ
16321 breakpoint_objfile_key
16322 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16323
fa3064dd 16324 catch_syscall_inferior_data
8e260fc0
TT
16325 = register_inferior_data_with_cleanup (NULL,
16326 catch_syscall_inferior_data_cleanup);
fa3064dd 16327
c906108c
SS
16328 breakpoint_chain = 0;
16329 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16330 before a breakpoint is set. */
16331 breakpoint_count = 0;
16332
1042e4c0
SS
16333 tracepoint_count = 0;
16334
1bedd215
AC
16335 add_com ("ignore", class_breakpoint, ignore_command, _("\
16336Set ignore-count of breakpoint number N to COUNT.\n\
16337Usage is `ignore N COUNT'."));
c906108c 16338 if (xdb_commands)
c5aa993b 16339 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16340
1bedd215
AC
16341 add_com ("commands", class_breakpoint, commands_command, _("\
16342Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16343Give breakpoint number as argument after \"commands\".\n\
16344With no argument, the targeted breakpoint is the last one set.\n\
16345The commands themselves follow starting on the next line.\n\
16346Type a line containing \"end\" to indicate the end of them.\n\
16347Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16348then no output is printed when it is hit, except what the commands print."));
c906108c 16349
d55637df 16350 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16351Specify breakpoint number N to break only if COND is true.\n\
c906108c 16352Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16353expression to be evaluated whenever breakpoint N is reached."));
d55637df 16354 set_cmd_completer (c, condition_completer);
c906108c 16355
1bedd215 16356 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16357Set a temporary breakpoint.\n\
c906108c
SS
16358Like \"break\" except the breakpoint is only temporary,\n\
16359so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16360by using \"enable delete\" on the breakpoint number.\n\
16361\n"
16362BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16363 set_cmd_completer (c, location_completer);
c94fdfd0 16364
1bedd215 16365 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16366Set a hardware assisted breakpoint.\n\
c906108c 16367Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16368some target hardware may not have this support.\n\
16369\n"
16370BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16371 set_cmd_completer (c, location_completer);
c906108c 16372
1bedd215 16373 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16374Set a temporary hardware assisted breakpoint.\n\
c906108c 16375Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16376so it will be deleted when hit.\n\
16377\n"
16378BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16379 set_cmd_completer (c, location_completer);
c906108c 16380
1bedd215
AC
16381 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16382Enable some breakpoints.\n\
c906108c
SS
16383Give breakpoint numbers (separated by spaces) as arguments.\n\
16384With no subcommand, breakpoints are enabled until you command otherwise.\n\
16385This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16386With a subcommand you can enable temporarily."),
c906108c
SS
16387 &enablelist, "enable ", 1, &cmdlist);
16388 if (xdb_commands)
1bedd215
AC
16389 add_com ("ab", class_breakpoint, enable_command, _("\
16390Enable some breakpoints.\n\
c906108c
SS
16391Give breakpoint numbers (separated by spaces) as arguments.\n\
16392With no subcommand, breakpoints are enabled until you command otherwise.\n\
16393This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16394With a subcommand you can enable temporarily."));
c906108c
SS
16395
16396 add_com_alias ("en", "enable", class_breakpoint, 1);
16397
84951ab5 16398 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16399Enable some breakpoints.\n\
c906108c
SS
16400Give breakpoint numbers (separated by spaces) as arguments.\n\
16401This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16402May be abbreviated to simply \"enable\".\n"),
c5aa993b 16403 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16404
1a966eab
AC
16405 add_cmd ("once", no_class, enable_once_command, _("\
16406Enable breakpoints for one hit. Give breakpoint numbers.\n\
16407If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16408 &enablebreaklist);
16409
1a966eab
AC
16410 add_cmd ("delete", no_class, enable_delete_command, _("\
16411Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16412If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16413 &enablebreaklist);
16414
816338b5
SS
16415 add_cmd ("count", no_class, enable_count_command, _("\
16416Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16417If a breakpoint is hit while enabled in this fashion,\n\
16418the count is decremented; when it reaches zero, the breakpoint is disabled."),
16419 &enablebreaklist);
16420
1a966eab
AC
16421 add_cmd ("delete", no_class, enable_delete_command, _("\
16422Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16423If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16424 &enablelist);
16425
1a966eab
AC
16426 add_cmd ("once", no_class, enable_once_command, _("\
16427Enable breakpoints for one hit. Give breakpoint numbers.\n\
16428If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16429 &enablelist);
16430
16431 add_cmd ("count", no_class, enable_count_command, _("\
16432Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16433If a breakpoint is hit while enabled in this fashion,\n\
16434the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16435 &enablelist);
16436
1bedd215
AC
16437 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16438Disable some breakpoints.\n\
c906108c
SS
16439Arguments are breakpoint numbers with spaces in between.\n\
16440To disable all breakpoints, give no argument.\n\
64b9b334 16441A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16442 &disablelist, "disable ", 1, &cmdlist);
16443 add_com_alias ("dis", "disable", class_breakpoint, 1);
16444 add_com_alias ("disa", "disable", class_breakpoint, 1);
16445 if (xdb_commands)
1bedd215
AC
16446 add_com ("sb", class_breakpoint, disable_command, _("\
16447Disable some breakpoints.\n\
c906108c
SS
16448Arguments are breakpoint numbers with spaces in between.\n\
16449To disable all breakpoints, give no argument.\n\
64b9b334 16450A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16451
1a966eab
AC
16452 add_cmd ("breakpoints", class_alias, disable_command, _("\
16453Disable some breakpoints.\n\
c906108c
SS
16454Arguments are breakpoint numbers with spaces in between.\n\
16455To disable all breakpoints, give no argument.\n\
64b9b334 16456A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16457This command may be abbreviated \"disable\"."),
c906108c
SS
16458 &disablelist);
16459
1bedd215
AC
16460 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16461Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16462Arguments are breakpoint numbers with spaces in between.\n\
16463To delete all breakpoints, give no argument.\n\
16464\n\
16465Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16466The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16467 &deletelist, "delete ", 1, &cmdlist);
16468 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16469 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16470 if (xdb_commands)
1bedd215
AC
16471 add_com ("db", class_breakpoint, delete_command, _("\
16472Delete some breakpoints.\n\
c906108c 16473Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16474To delete all breakpoints, give no argument.\n"));
c906108c 16475
1a966eab
AC
16476 add_cmd ("breakpoints", class_alias, delete_command, _("\
16477Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16478Arguments are breakpoint numbers with spaces in between.\n\
16479To delete all breakpoints, give no argument.\n\
1a966eab 16480This command may be abbreviated \"delete\"."),
c906108c
SS
16481 &deletelist);
16482
1bedd215
AC
16483 add_com ("clear", class_breakpoint, clear_command, _("\
16484Clear breakpoint at specified line or function.\n\
c906108c
SS
16485Argument may be line number, function name, or \"*\" and an address.\n\
16486If line number is specified, all breakpoints in that line are cleared.\n\
16487If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16488If an address is specified, breakpoints at that address are cleared.\n\
16489\n\
16490With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16491is executing in.\n\
16492\n\
1bedd215 16493See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16494 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16495
1bedd215 16496 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16497Set breakpoint at specified line or function.\n"
16498BREAK_ARGS_HELP ("break")));
5ba2abeb 16499 set_cmd_completer (c, location_completer);
c94fdfd0 16500
c906108c
SS
16501 add_com_alias ("b", "break", class_run, 1);
16502 add_com_alias ("br", "break", class_run, 1);
16503 add_com_alias ("bre", "break", class_run, 1);
16504 add_com_alias ("brea", "break", class_run, 1);
16505
7681d515
PM
16506 if (xdb_commands)
16507 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16508
16509 if (dbx_commands)
16510 {
1bedd215
AC
16511 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16512Break in function/address or break at a line in the current file."),
c5aa993b
JM
16513 &stoplist, "stop ", 1, &cmdlist);
16514 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16515 _("Break in function or address."), &stoplist);
c5aa993b 16516 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16517 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16518 add_com ("status", class_info, breakpoints_info, _("\
16519Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16520The \"Type\" column indicates one of:\n\
16521\tbreakpoint - normal breakpoint\n\
16522\twatchpoint - watchpoint\n\
16523The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16524the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16525breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16526address and file/line number respectively.\n\
16527\n\
16528Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16529are set to the address of the last breakpoint listed unless the command\n\
16530is prefixed with \"server \".\n\n\
c906108c 16531Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16532breakpoint set."));
c906108c
SS
16533 }
16534
1bedd215 16535 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16536Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16537The \"Type\" column indicates one of:\n\
16538\tbreakpoint - normal breakpoint\n\
16539\twatchpoint - watchpoint\n\
16540The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16541the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16542breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16543address and file/line number respectively.\n\
16544\n\
16545Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16546are set to the address of the last breakpoint listed unless the command\n\
16547is prefixed with \"server \".\n\n\
c906108c 16548Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16549breakpoint set."));
c906108c 16550
6b04bdb7
MS
16551 add_info_alias ("b", "breakpoints", 1);
16552
c906108c 16553 if (xdb_commands)
1bedd215
AC
16554 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16555Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16556The \"Type\" column indicates one of:\n\
16557\tbreakpoint - normal breakpoint\n\
16558\twatchpoint - watchpoint\n\
16559The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16560the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16561breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16562address and file/line number respectively.\n\
16563\n\
16564Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16565are set to the address of the last breakpoint listed unless the command\n\
16566is prefixed with \"server \".\n\n\
c906108c 16567Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16568breakpoint set."));
c906108c 16569
1a966eab
AC
16570 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16571Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16572The \"Type\" column indicates one of:\n\
16573\tbreakpoint - normal breakpoint\n\
16574\twatchpoint - watchpoint\n\
16575\tlongjmp - internal breakpoint used to step through longjmp()\n\
16576\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16577\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16578\tfinish - internal breakpoint used by the \"finish\" command\n\
16579The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16580the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16581breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16582address and file/line number respectively.\n\
16583\n\
16584Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16585are set to the address of the last breakpoint listed unless the command\n\
16586is prefixed with \"server \".\n\n\
c906108c 16587Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16588breakpoint set."),
c906108c
SS
16589 &maintenanceinfolist);
16590
44feb3ce
TT
16591 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16592Set catchpoints to catch events."),
16593 &catch_cmdlist, "catch ",
16594 0/*allow-unknown*/, &cmdlist);
16595
16596 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16597Set temporary catchpoints to catch events."),
16598 &tcatch_cmdlist, "tcatch ",
16599 0/*allow-unknown*/, &cmdlist);
16600
44feb3ce
TT
16601 add_catch_command ("fork", _("Catch calls to fork."),
16602 catch_fork_command_1,
a96d9b2e 16603 NULL,
44feb3ce
TT
16604 (void *) (uintptr_t) catch_fork_permanent,
16605 (void *) (uintptr_t) catch_fork_temporary);
16606 add_catch_command ("vfork", _("Catch calls to vfork."),
16607 catch_fork_command_1,
a96d9b2e 16608 NULL,
44feb3ce
TT
16609 (void *) (uintptr_t) catch_vfork_permanent,
16610 (void *) (uintptr_t) catch_vfork_temporary);
16611 add_catch_command ("exec", _("Catch calls to exec."),
16612 catch_exec_command_1,
a96d9b2e
SDJ
16613 NULL,
16614 CATCH_PERMANENT,
16615 CATCH_TEMPORARY);
edcc5120
TT
16616 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16617Usage: catch load [REGEX]\n\
16618If REGEX is given, only stop for libraries matching the regular expression."),
16619 catch_load_command_1,
16620 NULL,
16621 CATCH_PERMANENT,
16622 CATCH_TEMPORARY);
16623 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16624Usage: catch unload [REGEX]\n\
16625If REGEX is given, only stop for libraries matching the regular expression."),
16626 catch_unload_command_1,
16627 NULL,
16628 CATCH_PERMANENT,
16629 CATCH_TEMPORARY);
a96d9b2e
SDJ
16630 add_catch_command ("syscall", _("\
16631Catch system calls by their names and/or numbers.\n\
16632Arguments say which system calls to catch. If no arguments\n\
16633are given, every system call will be caught.\n\
16634Arguments, if given, should be one or more system call names\n\
16635(if your system supports that), or system call numbers."),
16636 catch_syscall_command_1,
16637 catch_syscall_completer,
44feb3ce
TT
16638 CATCH_PERMANENT,
16639 CATCH_TEMPORARY);
c5aa993b 16640
1bedd215
AC
16641 c = add_com ("watch", class_breakpoint, watch_command, _("\
16642Set a watchpoint for an expression.\n\
06a64a0b 16643Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16644A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16645an expression changes.\n\
16646If -l or -location is given, this evaluates EXPRESSION and watches\n\
16647the memory to which it refers."));
65d12d83 16648 set_cmd_completer (c, expression_completer);
c906108c 16649
1bedd215
AC
16650 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16651Set a read watchpoint for an expression.\n\
06a64a0b 16652Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16653A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16654an expression is read.\n\
16655If -l or -location is given, this evaluates EXPRESSION and watches\n\
16656the memory to which it refers."));
65d12d83 16657 set_cmd_completer (c, expression_completer);
c906108c 16658
1bedd215
AC
16659 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16660Set a watchpoint for an expression.\n\
06a64a0b 16661Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16662A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16663an expression is either read or written.\n\
16664If -l or -location is given, this evaluates EXPRESSION and watches\n\
16665the memory to which it refers."));
65d12d83 16666 set_cmd_completer (c, expression_completer);
c906108c 16667
d77f58be 16668 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16669Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16670
920d2a44
AC
16671 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16672 respond to changes - contrary to the description. */
85c07804
AC
16673 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16674 &can_use_hw_watchpoints, _("\
16675Set debugger's willingness to use watchpoint hardware."), _("\
16676Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16677If zero, gdb will not use hardware for new watchpoints, even if\n\
16678such is available. (However, any hardware watchpoints that were\n\
16679created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16680hardware.)"),
16681 NULL,
920d2a44 16682 show_can_use_hw_watchpoints,
85c07804 16683 &setlist, &showlist);
c906108c
SS
16684
16685 can_use_hw_watchpoints = 1;
fa8d40ab 16686
1042e4c0
SS
16687 /* Tracepoint manipulation commands. */
16688
16689 c = add_com ("trace", class_breakpoint, trace_command, _("\
16690Set a tracepoint at specified line or function.\n\
16691\n"
16692BREAK_ARGS_HELP ("trace") "\n\
16693Do \"help tracepoints\" for info on other tracepoint commands."));
16694 set_cmd_completer (c, location_completer);
16695
16696 add_com_alias ("tp", "trace", class_alias, 0);
16697 add_com_alias ("tr", "trace", class_alias, 1);
16698 add_com_alias ("tra", "trace", class_alias, 1);
16699 add_com_alias ("trac", "trace", class_alias, 1);
16700
7a697b8d
SS
16701 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16702Set a fast tracepoint at specified line or function.\n\
16703\n"
16704BREAK_ARGS_HELP ("ftrace") "\n\
16705Do \"help tracepoints\" for info on other tracepoint commands."));
16706 set_cmd_completer (c, location_completer);
16707
0fb4aa4b
PA
16708 c = add_com ("strace", class_breakpoint, strace_command, _("\
16709Set a static tracepoint at specified line, function or marker.\n\
16710\n\
16711strace [LOCATION] [if CONDITION]\n\
16712LOCATION may be a line number, function name, \"*\" and an address,\n\
16713or -m MARKER_ID.\n\
16714If a line number is specified, probe the marker at start of code\n\
16715for that line. If a function is specified, probe the marker at start\n\
16716of code for that function. If an address is specified, probe the marker\n\
16717at that exact address. If a marker id is specified, probe the marker\n\
16718with that name. With no LOCATION, uses current execution address of\n\
16719the selected stack frame.\n\
16720Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16721This collects arbitrary user data passed in the probe point call to the\n\
16722tracing library. You can inspect it when analyzing the trace buffer,\n\
16723by printing the $_sdata variable like any other convenience variable.\n\
16724\n\
16725CONDITION is a boolean expression.\n\
16726\n\
d41c0fc8
PA
16727Multiple tracepoints at one place are permitted, and useful if their\n\
16728conditions are different.\n\
0fb4aa4b
PA
16729\n\
16730Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16731Do \"help tracepoints\" for info on other tracepoint commands."));
16732 set_cmd_completer (c, location_completer);
16733
1042e4c0 16734 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16735Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16736Convenience variable \"$tpnum\" contains the number of the\n\
16737last tracepoint set."));
16738
16739 add_info_alias ("tp", "tracepoints", 1);
16740
16741 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16742Delete specified tracepoints.\n\
16743Arguments are tracepoint numbers, separated by spaces.\n\
16744No argument means delete all tracepoints."),
16745 &deletelist);
7e20dfcd 16746 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16747
16748 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16749Disable specified tracepoints.\n\
16750Arguments are tracepoint numbers, separated by spaces.\n\
16751No argument means disable all tracepoints."),
16752 &disablelist);
16753 deprecate_cmd (c, "disable");
16754
16755 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16756Enable specified tracepoints.\n\
16757Arguments are tracepoint numbers, separated by spaces.\n\
16758No argument means enable all tracepoints."),
16759 &enablelist);
16760 deprecate_cmd (c, "enable");
16761
16762 add_com ("passcount", class_trace, trace_pass_command, _("\
16763Set the passcount for a tracepoint.\n\
16764The trace will end when the tracepoint has been passed 'count' times.\n\
16765Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16766if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16767
6149aea9
PA
16768 add_prefix_cmd ("save", class_breakpoint, save_command,
16769 _("Save breakpoint definitions as a script."),
16770 &save_cmdlist, "save ",
16771 0/*allow-unknown*/, &cmdlist);
16772
16773 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16774Save current breakpoint definitions as a script.\n\
cce7e648 16775This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16776catchpoints, tracepoints). Use the 'source' command in another debug\n\
16777session to restore them."),
16778 &save_cmdlist);
16779 set_cmd_completer (c, filename_completer);
16780
16781 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16782Save current tracepoint definitions as a script.\n\
6149aea9
PA
16783Use the 'source' command in another debug session to restore them."),
16784 &save_cmdlist);
1042e4c0
SS
16785 set_cmd_completer (c, filename_completer);
16786
6149aea9
PA
16787 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16788 deprecate_cmd (c, "save tracepoints");
16789
1bedd215 16790 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16791Breakpoint specific settings\n\
16792Configure various breakpoint-specific variables such as\n\
1bedd215 16793pending breakpoint behavior"),
fa8d40ab
JJ
16794 &breakpoint_set_cmdlist, "set breakpoint ",
16795 0/*allow-unknown*/, &setlist);
1bedd215 16796 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16797Breakpoint specific settings\n\
16798Configure various breakpoint-specific variables such as\n\
1bedd215 16799pending breakpoint behavior"),
fa8d40ab
JJ
16800 &breakpoint_show_cmdlist, "show breakpoint ",
16801 0/*allow-unknown*/, &showlist);
16802
7915a72c
AC
16803 add_setshow_auto_boolean_cmd ("pending", no_class,
16804 &pending_break_support, _("\
16805Set debugger's behavior regarding pending breakpoints."), _("\
16806Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16807If on, an unrecognized breakpoint location will cause gdb to create a\n\
16808pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16809an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16810user-query to see if a pending breakpoint should be created."),
2c5b56ce 16811 NULL,
920d2a44 16812 show_pending_break_support,
6e1d7d6c
AC
16813 &breakpoint_set_cmdlist,
16814 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16815
16816 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16817
16818 add_setshow_boolean_cmd ("auto-hw", no_class,
16819 &automatic_hardware_breakpoints, _("\
16820Set automatic usage of hardware breakpoints."), _("\
16821Show automatic usage of hardware breakpoints."), _("\
16822If set, the debugger will automatically use hardware breakpoints for\n\
16823breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16824a warning will be emitted for such breakpoints."),
16825 NULL,
16826 show_automatic_hardware_breakpoints,
16827 &breakpoint_set_cmdlist,
16828 &breakpoint_show_cmdlist);
74960c60 16829
a25a5a45
PA
16830 add_setshow_boolean_cmd ("always-inserted", class_support,
16831 &always_inserted_mode, _("\
74960c60
VP
16832Set mode for inserting breakpoints."), _("\
16833Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16834When this mode is on, breakpoints are inserted immediately as soon as\n\
16835they're created, kept inserted even when execution stops, and removed\n\
16836only when the user deletes them. When this mode is off (the default),\n\
16837breakpoints are inserted only when execution continues, and removed\n\
16838when execution stops."),
72d0e2c5
YQ
16839 NULL,
16840 &show_always_inserted_mode,
16841 &breakpoint_set_cmdlist,
16842 &breakpoint_show_cmdlist);
f1310107 16843
b775012e
LM
16844 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16845 condition_evaluation_enums,
16846 &condition_evaluation_mode_1, _("\
16847Set mode of breakpoint condition evaluation."), _("\
16848Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16849When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16850evaluated on the host's side by GDB. When it is set to \"target\",\n\
16851breakpoint conditions will be downloaded to the target (if the target\n\
16852supports such feature) and conditions will be evaluated on the target's side.\n\
16853If this is set to \"auto\" (default), this will be automatically set to\n\
16854\"target\" if it supports condition evaluation, otherwise it will\n\
16855be set to \"gdb\""),
16856 &set_condition_evaluation_mode,
16857 &show_condition_evaluation_mode,
16858 &breakpoint_set_cmdlist,
16859 &breakpoint_show_cmdlist);
16860
f1310107
TJB
16861 add_com ("break-range", class_breakpoint, break_range_command, _("\
16862Set a breakpoint for an address range.\n\
16863break-range START-LOCATION, END-LOCATION\n\
16864where START-LOCATION and END-LOCATION can be one of the following:\n\
16865 LINENUM, for that line in the current file,\n\
16866 FILE:LINENUM, for that line in that file,\n\
16867 +OFFSET, for that number of lines after the current line\n\
16868 or the start of the range\n\
16869 FUNCTION, for the first line in that function,\n\
16870 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16871 *ADDRESS, for the instruction at that address.\n\
16872\n\
16873The breakpoint will stop execution of the inferior whenever it executes\n\
16874an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16875range (including START-LOCATION and END-LOCATION)."));
16876
e7e0cddf
SS
16877 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16878Set a dynamic printf at specified line or function.\n\
16879dprintf location,format string,arg1,arg2,...\n\
16880location may be a line number, function name, or \"*\" and an address.\n\
16881If a line number is specified, break at start of code for that line.\n\
0e4777df 16882If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16883 set_cmd_completer (c, location_completer);
16884
16885 add_setshow_enum_cmd ("dprintf-style", class_support,
16886 dprintf_style_enums, &dprintf_style, _("\
16887Set the style of usage for dynamic printf."), _("\
16888Show the style of usage for dynamic printf."), _("\
16889This setting chooses how GDB will do a dynamic printf.\n\
16890If the value is \"gdb\", then the printing is done by GDB to its own\n\
16891console, as with the \"printf\" command.\n\
16892If the value is \"call\", the print is done by calling a function in your\n\
16893program; by default printf(), but you can choose a different function or\n\
16894output stream by setting dprintf-function and dprintf-channel."),
16895 update_dprintf_commands, NULL,
16896 &setlist, &showlist);
16897
16898 dprintf_function = xstrdup ("printf");
16899 add_setshow_string_cmd ("dprintf-function", class_support,
16900 &dprintf_function, _("\
16901Set the function to use for dynamic printf"), _("\
16902Show the function to use for dynamic printf"), NULL,
16903 update_dprintf_commands, NULL,
16904 &setlist, &showlist);
16905
16906 dprintf_channel = xstrdup ("");
16907 add_setshow_string_cmd ("dprintf-channel", class_support,
16908 &dprintf_channel, _("\
16909Set the channel to use for dynamic printf"), _("\
16910Show the channel to use for dynamic printf"), NULL,
16911 update_dprintf_commands, NULL,
16912 &setlist, &showlist);
16913
d3ce09f5
SS
16914 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16915 &disconnected_dprintf, _("\
16916Set whether dprintf continues after GDB disconnects."), _("\
16917Show whether dprintf continues after GDB disconnects."), _("\
16918Use this to let dprintf commands continue to hit and produce output\n\
16919even if GDB disconnects or detaches from the target."),
16920 NULL,
16921 NULL,
16922 &setlist, &showlist);
16923
16924 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16925agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16926(target agent only) This is useful for formatted output in user-defined commands."));
16927
765dc015 16928 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16929
16930 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16931 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16932}