gdb/
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 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"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.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"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
edcc5120 69#include "gdb_regex.h"
b775012e 70#include "ax-gdb.h"
c906108c 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"
7371cf6d 80#include "python/python.h"
104c1213 81
4a64f543 82/* Prototypes for local functions. */
c906108c 83
a14ed312 84static void enable_delete_command (char *, int);
c906108c 85
a14ed312 86static void enable_once_command (char *, int);
c906108c 87
816338b5
SS
88static void enable_count_command (char *, int);
89
a14ed312 90static void disable_command (char *, int);
c906108c 91
a14ed312 92static void enable_command (char *, int);
c906108c 93
95a42b64
TT
94static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
95 void *),
96 void *);
c906108c 97
a14ed312 98static void ignore_command (char *, int);
c906108c 99
4efb68b1 100static int breakpoint_re_set_one (void *);
c906108c 101
348d480f
PA
102static void breakpoint_re_set_default (struct breakpoint *);
103
983af33b
SDJ
104static void create_sals_from_address_default (char **,
105 struct linespec_result *,
106 enum bptype, char *,
107 char **);
108
109static void create_breakpoints_sal_default (struct gdbarch *,
110 struct linespec_result *,
111 struct linespec_sals *,
e7e0cddf 112 char *, char *, enum bptype,
983af33b
SDJ
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
44f238bb 116 int, int, int, unsigned);
983af33b
SDJ
117
118static void decode_linespec_default (struct breakpoint *, char **,
119 struct symtabs_and_lines *);
120
a14ed312 121static void clear_command (char *, int);
c906108c 122
a14ed312 123static void catch_command (char *, int);
c906108c 124
a9634178 125static int can_use_hardware_watchpoint (struct value *);
c906108c 126
98deb0da 127static void break_command_1 (char *, int, int);
c906108c 128
a14ed312 129static void mention (struct breakpoint *);
c906108c 130
348d480f
PA
131static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
132 enum bptype,
c0a91b2b 133 const struct breakpoint_ops *);
3742cc8b
YQ
134static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
135 const struct symtab_and_line *);
136
4a64f543
MS
137/* This function is used in gdbtk sources and thus can not be made
138 static. */
63c252f8 139struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 140 struct symtab_and_line,
c0a91b2b
TT
141 enum bptype,
142 const struct breakpoint_ops *);
c906108c 143
06edf0c0
PA
144static struct breakpoint *
145 momentary_breakpoint_from_master (struct breakpoint *orig,
146 enum bptype type,
c0a91b2b 147 const struct breakpoint_ops *ops);
06edf0c0 148
76897487
KB
149static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
150
a6d9a66e
UW
151static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
152 CORE_ADDR bpaddr,
88f7da05 153 enum bptype bptype);
76897487 154
6c95b8df
PA
155static void describe_other_breakpoints (struct gdbarch *,
156 struct program_space *, CORE_ADDR,
5af949e3 157 struct obj_section *, int);
c906108c 158
6c95b8df
PA
159static int breakpoint_address_match (struct address_space *aspace1,
160 CORE_ADDR addr1,
161 struct address_space *aspace2,
162 CORE_ADDR addr2);
163
85d721b8
PA
164static int watchpoint_locations_match (struct bp_location *loc1,
165 struct bp_location *loc2);
166
f1310107
TJB
167static int breakpoint_location_address_match (struct bp_location *bl,
168 struct address_space *aspace,
169 CORE_ADDR addr);
170
a14ed312 171static void breakpoints_info (char *, int);
c906108c 172
d77f58be
SS
173static void watchpoints_info (char *, int);
174
e5a67952
MS
175static int breakpoint_1 (char *, int,
176 int (*) (const struct breakpoint *));
c906108c 177
4efb68b1 178static int breakpoint_cond_eval (void *);
c906108c 179
4efb68b1 180static void cleanup_executing_breakpoints (void *);
c906108c 181
a14ed312 182static void commands_command (char *, int);
c906108c 183
a14ed312 184static void condition_command (char *, int);
c906108c 185
c5aa993b
JM
186typedef enum
187 {
188 mark_inserted,
189 mark_uninserted
190 }
191insertion_state_t;
c906108c 192
0bde7532 193static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 194static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 195
e514a9d6 196static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 197
4efb68b1 198static int watchpoint_check (void *);
c906108c 199
a14ed312 200static void maintenance_info_breakpoints (char *, int);
c906108c 201
a14ed312 202static int hw_breakpoint_used_count (void);
c906108c 203
a1398e0c
PA
204static int hw_watchpoint_use_count (struct breakpoint *);
205
206static int hw_watchpoint_used_count_others (struct breakpoint *except,
207 enum bptype type,
208 int *other_type_used);
c906108c 209
a14ed312 210static void hbreak_command (char *, int);
c906108c 211
a14ed312 212static void thbreak_command (char *, int);
c906108c 213
816338b5
SS
214static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
215 int count);
c906108c 216
a14ed312 217static void stop_command (char *arg, int from_tty);
7a292a7a 218
a14ed312 219static void stopin_command (char *arg, int from_tty);
7a292a7a 220
a14ed312 221static void stopat_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 224
d85310f7
MS
225static void catch_exception_command_1 (enum exception_event_kind ex_event,
226 char *arg, int tempflag, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
d03285ec
UW
230static void detach_single_step_breakpoints (void);
231
6c95b8df
PA
232static int single_step_breakpoint_inserted_here_p (struct address_space *,
233 CORE_ADDR pc);
1aafd4da 234
fe3f5fa8 235static void free_bp_location (struct bp_location *loc);
f431efe5
PA
236static void incref_bp_location (struct bp_location *loc);
237static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 238
39d61571 239static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 240
b60e7edf 241static void update_global_location_list (int);
a5606eee 242
b60e7edf 243static void update_global_location_list_nothrow (int);
74960c60 244
d77f58be 245static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
246
247static void insert_breakpoint_locations (void);
a5606eee 248
a96d9b2e
SDJ
249static int syscall_catchpoint_p (struct breakpoint *b);
250
1042e4c0
SS
251static void tracepoints_info (char *, int);
252
253static void delete_trace_command (char *, int);
254
255static void enable_trace_command (char *, int);
256
257static void disable_trace_command (char *, int);
258
259static void trace_pass_command (char *, int);
260
9c06b0b4
TJB
261static int is_masked_watchpoint (const struct breakpoint *b);
262
b775012e
LM
263static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
264
983af33b
SDJ
265/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
266 otherwise. */
267
268static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 269
edcc5120
TT
270static void init_catchpoint (struct breakpoint *b,
271 struct gdbarch *gdbarch, int tempflag,
272 char *cond_string,
273 const struct breakpoint_ops *ops);
274
2060206e
PA
275/* The abstract base class all breakpoint_ops structures inherit
276 from. */
277static struct breakpoint_ops base_breakpoint_ops;
278
279/* The breakpoint_ops structure to be inherited by all breakpoint_ops
280 that are implemented on top of software or hardware breakpoints
281 (user breakpoints, internal and momentary breakpoints, etc.). */
282static struct breakpoint_ops bkpt_base_breakpoint_ops;
283
284/* Internal breakpoints class type. */
06edf0c0 285static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
286
287/* Momentary breakpoints class type. */
06edf0c0
PA
288static struct breakpoint_ops momentary_breakpoint_ops;
289
2060206e
PA
290/* The breakpoint_ops structure to be used in regular user created
291 breakpoints. */
292struct breakpoint_ops bkpt_breakpoint_ops;
293
55aa24fb
SDJ
294/* Breakpoints set on probes. */
295static struct breakpoint_ops bkpt_probe_breakpoint_ops;
296
e7e0cddf
SS
297/* Dynamic printf class type. */
298static struct breakpoint_ops dprintf_breakpoint_ops;
299
5cea2a26
PA
300/* A reference-counted struct command_line. This lets multiple
301 breakpoints share a single command list. */
302struct counted_command_line
303{
304 /* The reference count. */
305 int refc;
306
307 /* The command list. */
308 struct command_line *commands;
309};
310
311struct command_line *
312breakpoint_commands (struct breakpoint *b)
313{
314 return b->commands ? b->commands->commands : NULL;
315}
3daf8fe5 316
f3b1572e
PA
317/* Flag indicating that a command has proceeded the inferior past the
318 current breakpoint. */
319
320static int breakpoint_proceeded;
321
956a9fb9 322const char *
2cec12e5
AR
323bpdisp_text (enum bpdisp disp)
324{
4a64f543
MS
325 /* NOTE: the following values are a part of MI protocol and
326 represent values of 'disp' field returned when inferior stops at
327 a breakpoint. */
bc043ef3 328 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 329
2cec12e5
AR
330 return bpdisps[(int) disp];
331}
c906108c 332
4a64f543 333/* Prototypes for exported functions. */
c906108c 334/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 335 if such is available. */
c906108c
SS
336static int can_use_hw_watchpoints;
337
920d2a44
AC
338static void
339show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342{
3e43a32a
MS
343 fprintf_filtered (file,
344 _("Debugger's willingness to use "
345 "watchpoint hardware is %s.\n"),
920d2a44
AC
346 value);
347}
348
fa8d40ab
JJ
349/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
350 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 351 for unrecognized breakpoint locations.
fa8d40ab
JJ
352 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
353static enum auto_boolean pending_break_support;
920d2a44
AC
354static void
355show_pending_break_support (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358{
3e43a32a
MS
359 fprintf_filtered (file,
360 _("Debugger's behavior regarding "
361 "pending breakpoints is %s.\n"),
920d2a44
AC
362 value);
363}
fa8d40ab 364
765dc015 365/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 366 set with "break" but falling in read-only memory.
765dc015
VP
367 If 0, gdb will warn about such breakpoints, but won't automatically
368 use hardware breakpoints. */
369static int automatic_hardware_breakpoints;
370static void
371show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
372 struct cmd_list_element *c,
373 const char *value)
374{
3e43a32a
MS
375 fprintf_filtered (file,
376 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
377 value);
378}
379
33e5cbd6
PA
380/* If on, gdb will keep breakpoints inserted even as inferior is
381 stopped, and immediately insert any new breakpoints. If off, gdb
382 will insert breakpoints into inferior only when resuming it, and
383 will remove breakpoints upon stop. If auto, GDB will behave as ON
384 if in non-stop mode, and as OFF if all-stop mode.*/
385
386static const char always_inserted_auto[] = "auto";
387static const char always_inserted_on[] = "on";
388static const char always_inserted_off[] = "off";
40478521 389static const char *const always_inserted_enums[] = {
33e5cbd6
PA
390 always_inserted_auto,
391 always_inserted_off,
392 always_inserted_on,
393 NULL
394};
395static const char *always_inserted_mode = always_inserted_auto;
396static void
74960c60 397show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 398 struct cmd_list_element *c, const char *value)
74960c60 399{
33e5cbd6 400 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
401 fprintf_filtered (file,
402 _("Always inserted breakpoint "
403 "mode is %s (currently %s).\n"),
33e5cbd6
PA
404 value,
405 breakpoints_always_inserted_mode () ? "on" : "off");
406 else
3e43a32a
MS
407 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
408 value);
74960c60
VP
409}
410
33e5cbd6
PA
411int
412breakpoints_always_inserted_mode (void)
413{
20388dd6
YQ
414 return (always_inserted_mode == always_inserted_on
415 || (always_inserted_mode == always_inserted_auto && non_stop));
33e5cbd6 416}
765dc015 417
b775012e
LM
418static const char condition_evaluation_both[] = "host or target";
419
420/* Modes for breakpoint condition evaluation. */
421static const char condition_evaluation_auto[] = "auto";
422static const char condition_evaluation_host[] = "host";
423static const char condition_evaluation_target[] = "target";
424static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429};
430
431/* Global that holds the current mode for breakpoint condition evaluation. */
432static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434/* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438/* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443static const char *
444translate_condition_evaluation_mode (const char *mode)
445{
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455}
456
457/* Discovers what condition_evaluation_auto translates to. */
458
459static const char *
460breakpoint_condition_evaluation_mode (void)
461{
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463}
464
465/* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468static int
469gdb_evaluates_breakpoint_condition_p (void)
470{
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474}
475
a14ed312 476void _initialize_breakpoint (void);
c906108c 477
c906108c
SS
478/* Are we executing breakpoint commands? */
479static int executing_breakpoint_commands;
480
c02f5703
MS
481/* Are overlay event breakpoints enabled? */
482static int overlay_events_enabled;
483
e09342b5
TJB
484/* See description in breakpoint.h. */
485int target_exact_watchpoints = 0;
486
c906108c 487/* Walk the following statement or block through all breakpoints.
e5dd4106 488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 489 current breakpoint. */
c906108c 490
5c44784c 491#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 492
5c44784c
JM
493#define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
c906108c 497
4a64f543
MS
498/* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 501
876fa593
JK
502#define ALL_BP_LOCATIONS(B,BP_TMP) \
503 for (BP_TMP = bp_location; \
504 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
505 BP_TMP++)
7cc221ef 506
b775012e
LM
507/* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
517 && (BP_LOCP_TMP < bp_location + bp_location_count \
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
1042e4c0
SS
521/* Iterator for tracepoints only. */
522
523#define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 525 if (is_tracepoint (B))
1042e4c0 526
7cc221ef 527/* Chains of all breakpoints defined. */
c906108c
SS
528
529struct breakpoint *breakpoint_chain;
530
876fa593
JK
531/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
532
533static struct bp_location **bp_location;
534
535/* Number of elements of BP_LOCATION. */
536
537static unsigned bp_location_count;
538
4a64f543
MS
539/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540 ADDRESS for the current elements of BP_LOCATION which get a valid
541 result from bp_location_has_shadow. You can use it for roughly
542 limiting the subrange of BP_LOCATION to scan for shadow bytes for
543 an address you need to read. */
876fa593
JK
544
545static CORE_ADDR bp_location_placed_address_before_address_max;
546
4a64f543
MS
547/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549 BP_LOCATION which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATION to
551 scan for shadow bytes for an address you need to read. */
876fa593
JK
552
553static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 554
4a64f543
MS
555/* The locations that no longer correspond to any breakpoint, unlinked
556 from bp_location array, but for which a hit may still be reported
557 by a target. */
20874c92
VP
558VEC(bp_location_p) *moribund_locations = NULL;
559
c906108c
SS
560/* Number of last breakpoint made. */
561
95a42b64
TT
562static int breakpoint_count;
563
86b17b60
PA
564/* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568static int prev_breakpoint_count;
c906108c 569
1042e4c0
SS
570/* Number of last tracepoint made. */
571
95a42b64 572static int tracepoint_count;
1042e4c0 573
6149aea9
PA
574static struct cmd_list_element *breakpoint_set_cmdlist;
575static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 576struct cmd_list_element *save_cmdlist;
6149aea9 577
468d015d
JJ
578/* Return whether a breakpoint is an active enabled breakpoint. */
579static int
580breakpoint_enabled (struct breakpoint *b)
581{
0d381245 582 return (b->enable_state == bp_enabled);
468d015d
JJ
583}
584
c906108c
SS
585/* Set breakpoint count to NUM. */
586
95a42b64 587static void
fba45db2 588set_breakpoint_count (int num)
c906108c 589{
86b17b60 590 prev_breakpoint_count = breakpoint_count;
c906108c 591 breakpoint_count = num;
4fa62494 592 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
593}
594
86b17b60
PA
595/* Used by `start_rbreak_breakpoints' below, to record the current
596 breakpoint count before "rbreak" creates any breakpoint. */
597static int rbreak_start_breakpoint_count;
598
95a42b64
TT
599/* Called at the start an "rbreak" command to record the first
600 breakpoint made. */
86b17b60 601
95a42b64
TT
602void
603start_rbreak_breakpoints (void)
604{
86b17b60 605 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
606}
607
608/* Called at the end of an "rbreak" command to record the last
609 breakpoint made. */
86b17b60 610
95a42b64
TT
611void
612end_rbreak_breakpoints (void)
613{
86b17b60 614 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
615}
616
4a64f543 617/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
618
619void
fba45db2 620clear_breakpoint_hit_counts (void)
c906108c
SS
621{
622 struct breakpoint *b;
623
624 ALL_BREAKPOINTS (b)
625 b->hit_count = 0;
626}
627
9add0f1b
TT
628/* Allocate a new counted_command_line with reference count of 1.
629 The new structure owns COMMANDS. */
630
631static struct counted_command_line *
632alloc_counted_command_line (struct command_line *commands)
633{
634 struct counted_command_line *result
635 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 636
9add0f1b
TT
637 result->refc = 1;
638 result->commands = commands;
639 return result;
640}
641
642/* Increment reference count. This does nothing if CMD is NULL. */
643
644static void
645incref_counted_command_line (struct counted_command_line *cmd)
646{
647 if (cmd)
648 ++cmd->refc;
649}
650
651/* Decrement reference count. If the reference count reaches 0,
652 destroy the counted_command_line. Sets *CMDP to NULL. This does
653 nothing if *CMDP is NULL. */
654
655static void
656decref_counted_command_line (struct counted_command_line **cmdp)
657{
658 if (*cmdp)
659 {
660 if (--(*cmdp)->refc == 0)
661 {
662 free_command_lines (&(*cmdp)->commands);
663 xfree (*cmdp);
664 }
665 *cmdp = NULL;
666 }
667}
668
669/* A cleanup function that calls decref_counted_command_line. */
670
671static void
672do_cleanup_counted_command_line (void *arg)
673{
674 decref_counted_command_line (arg);
675}
676
677/* Create a cleanup that calls decref_counted_command_line on the
678 argument. */
679
680static struct cleanup *
681make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
682{
683 return make_cleanup (do_cleanup_counted_command_line, cmdp);
684}
685
c906108c 686\f
48cb2d85
VP
687/* Return the breakpoint with the specified number, or NULL
688 if the number does not refer to an existing breakpoint. */
689
690struct breakpoint *
691get_breakpoint (int num)
692{
693 struct breakpoint *b;
694
695 ALL_BREAKPOINTS (b)
696 if (b->number == num)
697 return b;
698
699 return NULL;
700}
5c44784c 701
c906108c 702\f
adc36818 703
b775012e
LM
704/* Mark locations as "conditions have changed" in case the target supports
705 evaluating conditions on its side. */
706
707static void
708mark_breakpoint_modified (struct breakpoint *b)
709{
710 struct bp_location *loc;
711
712 /* This is only meaningful if the target is
713 evaluating conditions and if the user has
714 opted for condition evaluation on the target's
715 side. */
716 if (gdb_evaluates_breakpoint_condition_p ()
717 || !target_supports_evaluation_of_breakpoint_conditions ())
718 return;
719
720 if (!is_breakpoint (b))
721 return;
722
723 for (loc = b->loc; loc; loc = loc->next)
724 loc->condition_changed = condition_modified;
725}
726
727/* Mark location as "conditions have changed" in case the target supports
728 evaluating conditions on its side. */
729
730static void
731mark_breakpoint_location_modified (struct bp_location *loc)
732{
733 /* This is only meaningful if the target is
734 evaluating conditions and if the user has
735 opted for condition evaluation on the target's
736 side. */
737 if (gdb_evaluates_breakpoint_condition_p ()
738 || !target_supports_evaluation_of_breakpoint_conditions ())
739
740 return;
741
742 if (!is_breakpoint (loc->owner))
743 return;
744
745 loc->condition_changed = condition_modified;
746}
747
748/* Sets the condition-evaluation mode using the static global
749 condition_evaluation_mode. */
750
751static void
752set_condition_evaluation_mode (char *args, int from_tty,
753 struct cmd_list_element *c)
754{
755 struct breakpoint *b;
756 const char *old_mode, *new_mode;
757
758 if ((condition_evaluation_mode_1 == condition_evaluation_target)
759 && !target_supports_evaluation_of_breakpoint_conditions ())
760 {
761 condition_evaluation_mode_1 = condition_evaluation_mode;
762 warning (_("Target does not support breakpoint condition evaluation.\n"
763 "Using host evaluation mode instead."));
764 return;
765 }
766
767 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
768 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
769
abf1152a
JK
770 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
771 settings was "auto". */
772 condition_evaluation_mode = condition_evaluation_mode_1;
773
b775012e
LM
774 /* Only update the mode if the user picked a different one. */
775 if (new_mode != old_mode)
776 {
777 struct bp_location *loc, **loc_tmp;
778 /* If the user switched to a different evaluation mode, we
779 need to synch the changes with the target as follows:
780
781 "host" -> "target": Send all (valid) conditions to the target.
782 "target" -> "host": Remove all the conditions from the target.
783 */
784
b775012e
LM
785 if (new_mode == condition_evaluation_target)
786 {
787 /* Mark everything modified and synch conditions with the
788 target. */
789 ALL_BP_LOCATIONS (loc, loc_tmp)
790 mark_breakpoint_location_modified (loc);
791 }
792 else
793 {
794 /* Manually mark non-duplicate locations to synch conditions
795 with the target. We do this to remove all the conditions the
796 target knows about. */
797 ALL_BP_LOCATIONS (loc, loc_tmp)
798 if (is_breakpoint (loc->owner) && loc->inserted)
799 loc->needs_update = 1;
800 }
801
802 /* Do the update. */
803 update_global_location_list (1);
804 }
805
806 return;
807}
808
809/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
810 what "auto" is translating to. */
811
812static void
813show_condition_evaluation_mode (struct ui_file *file, int from_tty,
814 struct cmd_list_element *c, const char *value)
815{
816 if (condition_evaluation_mode == condition_evaluation_auto)
817 fprintf_filtered (file,
818 _("Breakpoint condition evaluation "
819 "mode is %s (currently %s).\n"),
820 value,
821 breakpoint_condition_evaluation_mode ());
822 else
823 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
824 value);
825}
826
827/* A comparison function for bp_location AP and BP that is used by
828 bsearch. This comparison function only cares about addresses, unlike
829 the more general bp_location_compare function. */
830
831static int
832bp_location_compare_addrs (const void *ap, const void *bp)
833{
834 struct bp_location *a = *(void **) ap;
835 struct bp_location *b = *(void **) bp;
836
837 if (a->address == b->address)
838 return 0;
839 else
840 return ((a->address > b->address) - (a->address < b->address));
841}
842
843/* Helper function to skip all bp_locations with addresses
844 less than ADDRESS. It returns the first bp_location that
845 is greater than or equal to ADDRESS. If none is found, just
846 return NULL. */
847
848static struct bp_location **
849get_first_locp_gte_addr (CORE_ADDR address)
850{
851 struct bp_location dummy_loc;
852 struct bp_location *dummy_locp = &dummy_loc;
853 struct bp_location **locp_found = NULL;
854
855 /* Initialize the dummy location's address field. */
856 memset (&dummy_loc, 0, sizeof (struct bp_location));
857 dummy_loc.address = address;
858
859 /* Find a close match to the first location at ADDRESS. */
860 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
861 sizeof (struct bp_location **),
862 bp_location_compare_addrs);
863
864 /* Nothing was found, nothing left to do. */
865 if (locp_found == NULL)
866 return NULL;
867
868 /* We may have found a location that is at ADDRESS but is not the first in the
869 location's list. Go backwards (if possible) and locate the first one. */
870 while ((locp_found - 1) >= bp_location
871 && (*(locp_found - 1))->address == address)
872 locp_found--;
873
874 return locp_found;
875}
876
adc36818
PM
877void
878set_breakpoint_condition (struct breakpoint *b, char *exp,
879 int from_tty)
880{
3a5c3e22
PA
881 xfree (b->cond_string);
882 b->cond_string = NULL;
adc36818 883
3a5c3e22 884 if (is_watchpoint (b))
adc36818 885 {
3a5c3e22
PA
886 struct watchpoint *w = (struct watchpoint *) b;
887
888 xfree (w->cond_exp);
889 w->cond_exp = NULL;
890 }
891 else
892 {
893 struct bp_location *loc;
894
895 for (loc = b->loc; loc; loc = loc->next)
896 {
897 xfree (loc->cond);
898 loc->cond = NULL;
b775012e
LM
899
900 /* No need to free the condition agent expression
901 bytecode (if we have one). We will handle this
902 when we go through update_global_location_list. */
3a5c3e22 903 }
adc36818 904 }
adc36818
PM
905
906 if (*exp == 0)
907 {
908 if (from_tty)
909 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
910 }
911 else
912 {
913 char *arg = exp;
cc59ec59 914
adc36818
PM
915 /* I don't know if it matters whether this is the string the user
916 typed in or the decompiled expression. */
917 b->cond_string = xstrdup (arg);
918 b->condition_not_parsed = 0;
919
920 if (is_watchpoint (b))
921 {
3a5c3e22
PA
922 struct watchpoint *w = (struct watchpoint *) b;
923
adc36818
PM
924 innermost_block = NULL;
925 arg = exp;
3a5c3e22 926 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
927 if (*arg)
928 error (_("Junk at end of expression"));
3a5c3e22 929 w->cond_exp_valid_block = innermost_block;
adc36818
PM
930 }
931 else
932 {
3a5c3e22
PA
933 struct bp_location *loc;
934
adc36818
PM
935 for (loc = b->loc; loc; loc = loc->next)
936 {
937 arg = exp;
938 loc->cond =
939 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
940 if (*arg)
941 error (_("Junk at end of expression"));
942 }
943 }
944 }
b775012e
LM
945 mark_breakpoint_modified (b);
946
adc36818 947 breakpoints_changed ();
8d3788bd 948 observer_notify_breakpoint_modified (b);
adc36818
PM
949}
950
c906108c
SS
951/* condition N EXP -- set break condition of breakpoint N to EXP. */
952
953static void
fba45db2 954condition_command (char *arg, int from_tty)
c906108c 955{
52f0bd74 956 struct breakpoint *b;
c906108c 957 char *p;
52f0bd74 958 int bnum;
c906108c
SS
959
960 if (arg == 0)
e2e0b3e5 961 error_no_arg (_("breakpoint number"));
c906108c
SS
962
963 p = arg;
964 bnum = get_number (&p);
5c44784c 965 if (bnum == 0)
8a3fe4f8 966 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
967
968 ALL_BREAKPOINTS (b)
969 if (b->number == bnum)
2f069f6f 970 {
7371cf6d
PM
971 /* Check if this breakpoint has a Python object assigned to
972 it, and if it has a definition of the "stop"
973 method. This method and conditions entered into GDB from
974 the CLI are mutually exclusive. */
975 if (b->py_bp_object
976 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
977 error (_("Cannot set a condition where a Python 'stop' "
978 "method has been defined in the breakpoint."));
2566ad2d 979 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
980
981 if (is_breakpoint (b))
982 update_global_location_list (1);
983
2f069f6f
JB
984 return;
985 }
c906108c 986
8a3fe4f8 987 error (_("No breakpoint number %d."), bnum);
c906108c
SS
988}
989
a7bdde9e
VP
990/* Check that COMMAND do not contain commands that are suitable
991 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
992 Throw if any such commands is found. */
993
a7bdde9e
VP
994static void
995check_no_tracepoint_commands (struct command_line *commands)
996{
997 struct command_line *c;
cc59ec59 998
a7bdde9e
VP
999 for (c = commands; c; c = c->next)
1000 {
1001 int i;
1002
1003 if (c->control_type == while_stepping_control)
3e43a32a
MS
1004 error (_("The 'while-stepping' command can "
1005 "only be used for tracepoints"));
a7bdde9e
VP
1006
1007 for (i = 0; i < c->body_count; ++i)
1008 check_no_tracepoint_commands ((c->body_list)[i]);
1009
1010 /* Not that command parsing removes leading whitespace and comment
4a64f543 1011 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1012 command directly. */
1013 if (strstr (c->line, "collect ") == c->line)
1014 error (_("The 'collect' command can only be used for tracepoints"));
1015
51661e93
VP
1016 if (strstr (c->line, "teval ") == c->line)
1017 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1018 }
1019}
1020
d77f58be
SS
1021/* Encapsulate tests for different types of tracepoints. */
1022
d9b3f62e
PA
1023static int
1024is_tracepoint_type (enum bptype type)
1025{
1026 return (type == bp_tracepoint
1027 || type == bp_fast_tracepoint
1028 || type == bp_static_tracepoint);
1029}
1030
a7bdde9e 1031int
d77f58be 1032is_tracepoint (const struct breakpoint *b)
a7bdde9e 1033{
d9b3f62e 1034 return is_tracepoint_type (b->type);
a7bdde9e 1035}
d9b3f62e 1036
e5dd4106 1037/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1038 breakpoint. This function will throw an exception if a problem is
1039 found. */
48cb2d85 1040
95a42b64
TT
1041static void
1042validate_commands_for_breakpoint (struct breakpoint *b,
1043 struct command_line *commands)
48cb2d85 1044{
d77f58be 1045 if (is_tracepoint (b))
a7bdde9e 1046 {
4a64f543
MS
1047 /* We need to verify that each top-level element of commands is
1048 valid for tracepoints, that there's at most one
1049 while-stepping element, and that while-stepping's body has
1050 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
1051 struct command_line *c;
1052 struct command_line *while_stepping = 0;
1053 for (c = commands; c; c = c->next)
1054 {
a7bdde9e
VP
1055 if (c->control_type == while_stepping_control)
1056 {
1057 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1058 error (_("The 'while-stepping' command "
1059 "cannot be used for fast tracepoint"));
0fb4aa4b 1060 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1061 error (_("The 'while-stepping' command "
1062 "cannot be used for static tracepoint"));
a7bdde9e
VP
1063
1064 if (while_stepping)
3e43a32a
MS
1065 error (_("The 'while-stepping' command "
1066 "can be used only once"));
a7bdde9e
VP
1067 else
1068 while_stepping = c;
1069 }
1070 }
1071 if (while_stepping)
1072 {
1073 struct command_line *c2;
1074
1075 gdb_assert (while_stepping->body_count == 1);
1076 c2 = while_stepping->body_list[0];
1077 for (; c2; c2 = c2->next)
1078 {
a7bdde9e
VP
1079 if (c2->control_type == while_stepping_control)
1080 error (_("The 'while-stepping' command cannot be nested"));
1081 }
1082 }
1083 }
1084 else
1085 {
1086 check_no_tracepoint_commands (commands);
1087 }
95a42b64
TT
1088}
1089
0fb4aa4b
PA
1090/* Return a vector of all the static tracepoints set at ADDR. The
1091 caller is responsible for releasing the vector. */
1092
1093VEC(breakpoint_p) *
1094static_tracepoints_here (CORE_ADDR addr)
1095{
1096 struct breakpoint *b;
1097 VEC(breakpoint_p) *found = 0;
1098 struct bp_location *loc;
1099
1100 ALL_BREAKPOINTS (b)
1101 if (b->type == bp_static_tracepoint)
1102 {
1103 for (loc = b->loc; loc; loc = loc->next)
1104 if (loc->address == addr)
1105 VEC_safe_push(breakpoint_p, found, b);
1106 }
1107
1108 return found;
1109}
1110
95a42b64 1111/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1112 validate that only allowed commands are included. */
95a42b64
TT
1113
1114void
4a64f543
MS
1115breakpoint_set_commands (struct breakpoint *b,
1116 struct command_line *commands)
95a42b64
TT
1117{
1118 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1119
9add0f1b
TT
1120 decref_counted_command_line (&b->commands);
1121 b->commands = alloc_counted_command_line (commands);
48cb2d85 1122 breakpoints_changed ();
8d3788bd 1123 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1124}
1125
45a43567
TT
1126/* Set the internal `silent' flag on the breakpoint. Note that this
1127 is not the same as the "silent" that may appear in the breakpoint's
1128 commands. */
1129
1130void
1131breakpoint_set_silent (struct breakpoint *b, int silent)
1132{
1133 int old_silent = b->silent;
1134
1135 b->silent = silent;
1136 if (old_silent != silent)
8d3788bd 1137 observer_notify_breakpoint_modified (b);
45a43567
TT
1138}
1139
1140/* Set the thread for this breakpoint. If THREAD is -1, make the
1141 breakpoint work for any thread. */
1142
1143void
1144breakpoint_set_thread (struct breakpoint *b, int thread)
1145{
1146 int old_thread = b->thread;
1147
1148 b->thread = thread;
1149 if (old_thread != thread)
8d3788bd 1150 observer_notify_breakpoint_modified (b);
45a43567
TT
1151}
1152
1153/* Set the task for this breakpoint. If TASK is 0, make the
1154 breakpoint work for any task. */
1155
1156void
1157breakpoint_set_task (struct breakpoint *b, int task)
1158{
1159 int old_task = b->task;
1160
1161 b->task = task;
1162 if (old_task != task)
8d3788bd 1163 observer_notify_breakpoint_modified (b);
45a43567
TT
1164}
1165
95a42b64
TT
1166void
1167check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1168{
1169 struct breakpoint *b = closure;
cc59ec59 1170
a7bdde9e
VP
1171 validate_actionline (&line, b);
1172}
1173
95a42b64
TT
1174/* A structure used to pass information through
1175 map_breakpoint_numbers. */
1176
1177struct commands_info
1178{
1179 /* True if the command was typed at a tty. */
1180 int from_tty;
86b17b60
PA
1181
1182 /* The breakpoint range spec. */
1183 char *arg;
1184
95a42b64
TT
1185 /* Non-NULL if the body of the commands are being read from this
1186 already-parsed command. */
1187 struct command_line *control;
86b17b60 1188
95a42b64
TT
1189 /* The command lines read from the user, or NULL if they have not
1190 yet been read. */
1191 struct counted_command_line *cmd;
1192};
1193
1194/* A callback for map_breakpoint_numbers that sets the commands for
1195 commands_command. */
1196
c906108c 1197static void
95a42b64 1198do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1199{
95a42b64 1200 struct commands_info *info = data;
c906108c 1201
95a42b64
TT
1202 if (info->cmd == NULL)
1203 {
1204 struct command_line *l;
5c44784c 1205
95a42b64
TT
1206 if (info->control != NULL)
1207 l = copy_command_lines (info->control->body_list[0]);
1208 else
86b17b60
PA
1209 {
1210 struct cleanup *old_chain;
1211 char *str;
c5aa993b 1212
3e43a32a
MS
1213 str = xstrprintf (_("Type commands for breakpoint(s) "
1214 "%s, one per line."),
86b17b60
PA
1215 info->arg);
1216
1217 old_chain = make_cleanup (xfree, str);
1218
1219 l = read_command_lines (str,
1220 info->from_tty, 1,
d77f58be 1221 (is_tracepoint (b)
86b17b60
PA
1222 ? check_tracepoint_command : 0),
1223 b);
1224
1225 do_cleanups (old_chain);
1226 }
a7bdde9e 1227
95a42b64
TT
1228 info->cmd = alloc_counted_command_line (l);
1229 }
1230
1231 /* If a breakpoint was on the list more than once, we don't need to
1232 do anything. */
1233 if (b->commands != info->cmd)
1234 {
1235 validate_commands_for_breakpoint (b, info->cmd->commands);
1236 incref_counted_command_line (info->cmd);
1237 decref_counted_command_line (&b->commands);
1238 b->commands = info->cmd;
1239 breakpoints_changed ();
8d3788bd 1240 observer_notify_breakpoint_modified (b);
c5aa993b 1241 }
95a42b64
TT
1242}
1243
1244static void
4a64f543
MS
1245commands_command_1 (char *arg, int from_tty,
1246 struct command_line *control)
95a42b64
TT
1247{
1248 struct cleanup *cleanups;
1249 struct commands_info info;
1250
1251 info.from_tty = from_tty;
1252 info.control = control;
1253 info.cmd = NULL;
1254 /* If we read command lines from the user, then `info' will hold an
1255 extra reference to the commands that we must clean up. */
1256 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1257
1258 if (arg == NULL || !*arg)
1259 {
86b17b60 1260 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1261 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1262 breakpoint_count);
95a42b64
TT
1263 else if (breakpoint_count > 0)
1264 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1265 else
1266 {
1267 /* So that we don't try to free the incoming non-NULL
1268 argument in the cleanup below. Mapping breakpoint
1269 numbers will fail in this case. */
1270 arg = NULL;
1271 }
95a42b64 1272 }
9766ced4
SS
1273 else
1274 /* The command loop has some static state, so we need to preserve
1275 our argument. */
1276 arg = xstrdup (arg);
86b17b60
PA
1277
1278 if (arg != NULL)
1279 make_cleanup (xfree, arg);
1280
1281 info.arg = arg;
95a42b64
TT
1282
1283 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1284
1285 if (info.cmd == NULL)
1286 error (_("No breakpoints specified."));
1287
1288 do_cleanups (cleanups);
1289}
1290
1291static void
1292commands_command (char *arg, int from_tty)
1293{
1294 commands_command_1 (arg, from_tty, NULL);
c906108c 1295}
40c03ae8
EZ
1296
1297/* Like commands_command, but instead of reading the commands from
1298 input stream, takes them from an already parsed command structure.
1299
1300 This is used by cli-script.c to DTRT with breakpoint commands
1301 that are part of if and while bodies. */
1302enum command_control_type
1303commands_from_control_command (char *arg, struct command_line *cmd)
1304{
95a42b64
TT
1305 commands_command_1 (arg, 0, cmd);
1306 return simple_control;
40c03ae8 1307}
876fa593
JK
1308
1309/* Return non-zero if BL->TARGET_INFO contains valid information. */
1310
1311static int
1312bp_location_has_shadow (struct bp_location *bl)
1313{
1314 if (bl->loc_type != bp_loc_software_breakpoint)
1315 return 0;
1316 if (!bl->inserted)
1317 return 0;
1318 if (bl->target_info.shadow_len == 0)
e5dd4106 1319 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1320 return 0;
1321 return 1;
1322}
1323
8defab1a 1324/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1325 by replacing any memory breakpoints with their shadowed contents.
1326
35c63cd8
JB
1327 If READBUF is not NULL, this buffer must not overlap with any of
1328 the breakpoint location's shadow_contents buffers. Otherwise,
1329 a failed assertion internal error will be raised.
1330
876fa593 1331 The range of shadowed area by each bp_location is:
35df4500
TJB
1332 bl->address - bp_location_placed_address_before_address_max
1333 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1334 The range we were requested to resolve shadows for is:
1335 memaddr ... memaddr + len
1336 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1337 memaddr + len <= (bl->address
1338 - bp_location_placed_address_before_address_max)
876fa593 1339 and:
35df4500 1340 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1341
8defab1a 1342void
f0ba3972
PA
1343breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1344 const gdb_byte *writebuf_org,
1345 ULONGEST memaddr, LONGEST len)
c906108c 1346{
4a64f543
MS
1347 /* Left boundary, right boundary and median element of our binary
1348 search. */
876fa593
JK
1349 unsigned bc_l, bc_r, bc;
1350
4a64f543
MS
1351 /* Find BC_L which is a leftmost element which may affect BUF
1352 content. It is safe to report lower value but a failure to
1353 report higher one. */
876fa593
JK
1354
1355 bc_l = 0;
1356 bc_r = bp_location_count;
1357 while (bc_l + 1 < bc_r)
1358 {
35df4500 1359 struct bp_location *bl;
876fa593
JK
1360
1361 bc = (bc_l + bc_r) / 2;
35df4500 1362 bl = bp_location[bc];
876fa593 1363
4a64f543
MS
1364 /* Check first BL->ADDRESS will not overflow due to the added
1365 constant. Then advance the left boundary only if we are sure
1366 the BC element can in no way affect the BUF content (MEMADDR
1367 to MEMADDR + LEN range).
876fa593 1368
4a64f543
MS
1369 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1370 offset so that we cannot miss a breakpoint with its shadow
1371 range tail still reaching MEMADDR. */
c5aa993b 1372
35df4500
TJB
1373 if ((bl->address + bp_location_shadow_len_after_address_max
1374 >= bl->address)
1375 && (bl->address + bp_location_shadow_len_after_address_max
1376 <= memaddr))
876fa593
JK
1377 bc_l = bc;
1378 else
1379 bc_r = bc;
1380 }
1381
128070bb
PA
1382 /* Due to the binary search above, we need to make sure we pick the
1383 first location that's at BC_L's address. E.g., if there are
1384 multiple locations at the same address, BC_L may end up pointing
1385 at a duplicate location, and miss the "master"/"inserted"
1386 location. Say, given locations L1, L2 and L3 at addresses A and
1387 B:
1388
1389 L1@A, L2@A, L3@B, ...
1390
1391 BC_L could end up pointing at location L2, while the "master"
1392 location could be L1. Since the `loc->inserted' flag is only set
1393 on "master" locations, we'd forget to restore the shadow of L1
1394 and L2. */
1395 while (bc_l > 0
1396 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1397 bc_l--;
1398
876fa593
JK
1399 /* Now do full processing of the found relevant range of elements. */
1400
1401 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1402 {
35df4500 1403 struct bp_location *bl = bp_location[bc];
876fa593
JK
1404 CORE_ADDR bp_addr = 0;
1405 int bp_size = 0;
1406 int bptoffset = 0;
1407
35df4500
TJB
1408 /* bp_location array has BL->OWNER always non-NULL. */
1409 if (bl->owner->type == bp_none)
8a3fe4f8 1410 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1411 bl->owner->number);
ffce0d52 1412
e5dd4106 1413 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1414 content. */
1415
35df4500
TJB
1416 if (bl->address >= bp_location_placed_address_before_address_max
1417 && memaddr + len <= (bl->address
1418 - bp_location_placed_address_before_address_max))
876fa593
JK
1419 break;
1420
35df4500 1421 if (!bp_location_has_shadow (bl))
c5aa993b 1422 continue;
35df4500 1423 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1424 current_program_space->aspace, 0))
1425 continue;
1426
c5aa993b
JM
1427 /* Addresses and length of the part of the breakpoint that
1428 we need to copy. */
35df4500
TJB
1429 bp_addr = bl->target_info.placed_address;
1430 bp_size = bl->target_info.shadow_len;
8defab1a 1431
c5aa993b
JM
1432 if (bp_addr + bp_size <= memaddr)
1433 /* The breakpoint is entirely before the chunk of memory we
1434 are reading. */
1435 continue;
8defab1a 1436
c5aa993b
JM
1437 if (bp_addr >= memaddr + len)
1438 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1439 reading. */
c5aa993b 1440 continue;
c5aa993b 1441
8defab1a
DJ
1442 /* Offset within shadow_contents. */
1443 if (bp_addr < memaddr)
1444 {
1445 /* Only copy the second part of the breakpoint. */
1446 bp_size -= memaddr - bp_addr;
1447 bptoffset = memaddr - bp_addr;
1448 bp_addr = memaddr;
1449 }
c5aa993b 1450
8defab1a
DJ
1451 if (bp_addr + bp_size > memaddr + len)
1452 {
1453 /* Only copy the first part of the breakpoint. */
1454 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1455 }
c5aa993b 1456
f0ba3972
PA
1457 if (readbuf != NULL)
1458 {
35c63cd8
JB
1459 /* Verify that the readbuf buffer does not overlap with
1460 the shadow_contents buffer. */
1461 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1462 || readbuf >= (bl->target_info.shadow_contents
1463 + bl->target_info.shadow_len));
1464
f0ba3972
PA
1465 /* Update the read buffer with this inserted breakpoint's
1466 shadow. */
1467 memcpy (readbuf + bp_addr - memaddr,
1468 bl->target_info.shadow_contents + bptoffset, bp_size);
1469 }
1470 else
1471 {
1472 struct gdbarch *gdbarch = bl->gdbarch;
1473 const unsigned char *bp;
1474 CORE_ADDR placed_address = bl->target_info.placed_address;
1475 unsigned placed_size = bl->target_info.placed_size;
1476
1477 /* Update the shadow with what we want to write to memory. */
1478 memcpy (bl->target_info.shadow_contents + bptoffset,
1479 writebuf_org + bp_addr - memaddr, bp_size);
1480
1481 /* Determine appropriate breakpoint contents and size for this
1482 address. */
1483 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1484
1485 /* Update the final write buffer with this inserted
1486 breakpoint's INSN. */
1487 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1488 }
c5aa993b 1489 }
c906108c 1490}
c906108c 1491\f
c5aa993b 1492
b775012e
LM
1493/* Return true if BPT is either a software breakpoint or a hardware
1494 breakpoint. */
1495
1496int
1497is_breakpoint (const struct breakpoint *bpt)
1498{
1499 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1500 || bpt->type == bp_hardware_breakpoint
1501 || bpt->type == bp_dprintf);
b775012e
LM
1502}
1503
60e1c644
PA
1504/* Return true if BPT is of any hardware watchpoint kind. */
1505
a5606eee 1506static int
d77f58be 1507is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1508{
1509 return (bpt->type == bp_hardware_watchpoint
1510 || bpt->type == bp_read_watchpoint
1511 || bpt->type == bp_access_watchpoint);
1512}
7270d8f2 1513
60e1c644
PA
1514/* Return true if BPT is of any watchpoint kind, hardware or
1515 software. */
1516
3a5c3e22 1517int
d77f58be 1518is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1519{
1520 return (is_hardware_watchpoint (bpt)
1521 || bpt->type == bp_watchpoint);
1522}
1523
3a5c3e22
PA
1524/* Returns true if the current thread and its running state are safe
1525 to evaluate or update watchpoint B. Watchpoints on local
1526 expressions need to be evaluated in the context of the thread that
1527 was current when the watchpoint was created, and, that thread needs
1528 to be stopped to be able to select the correct frame context.
1529 Watchpoints on global expressions can be evaluated on any thread,
1530 and in any state. It is presently left to the target allowing
1531 memory accesses when threads are running. */
f6bc2008
PA
1532
1533static int
3a5c3e22 1534watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1535{
d0d8b0c6
JK
1536 return (b->base.pspace == current_program_space
1537 && (ptid_equal (b->watchpoint_thread, null_ptid)
1538 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1539 && !is_executing (inferior_ptid))));
f6bc2008
PA
1540}
1541
d0fb5eae
JK
1542/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1543 associated bp_watchpoint_scope breakpoint. */
1544
1545static void
3a5c3e22 1546watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1547{
3a5c3e22 1548 struct breakpoint *b = &w->base;
d0fb5eae
JK
1549
1550 if (b->related_breakpoint != b)
1551 {
1552 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1553 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1554 b->related_breakpoint->disposition = disp_del_at_next_stop;
1555 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1556 b->related_breakpoint = b;
1557 }
1558 b->disposition = disp_del_at_next_stop;
1559}
1560
567e1b4e
JB
1561/* Assuming that B is a watchpoint:
1562 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1563 - Evaluate expression and store the result in B->val
567e1b4e
JB
1564 - Evaluate the condition if there is one, and store the result
1565 in b->loc->cond.
a5606eee
VP
1566 - Update the list of values that must be watched in B->loc.
1567
4a64f543
MS
1568 If the watchpoint disposition is disp_del_at_next_stop, then do
1569 nothing. If this is local watchpoint that is out of scope, delete
1570 it.
1571
1572 Even with `set breakpoint always-inserted on' the watchpoints are
1573 removed + inserted on each stop here. Normal breakpoints must
1574 never be removed because they might be missed by a running thread
1575 when debugging in non-stop mode. On the other hand, hardware
1576 watchpoints (is_hardware_watchpoint; processed here) are specific
1577 to each LWP since they are stored in each LWP's hardware debug
1578 registers. Therefore, such LWP must be stopped first in order to
1579 be able to modify its hardware watchpoints.
1580
1581 Hardware watchpoints must be reset exactly once after being
1582 presented to the user. It cannot be done sooner, because it would
1583 reset the data used to present the watchpoint hit to the user. And
1584 it must not be done later because it could display the same single
1585 watchpoint hit during multiple GDB stops. Note that the latter is
1586 relevant only to the hardware watchpoint types bp_read_watchpoint
1587 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1588 not user-visible - its hit is suppressed if the memory content has
1589 not changed.
1590
1591 The following constraints influence the location where we can reset
1592 hardware watchpoints:
1593
1594 * target_stopped_by_watchpoint and target_stopped_data_address are
1595 called several times when GDB stops.
1596
1597 [linux]
1598 * Multiple hardware watchpoints can be hit at the same time,
1599 causing GDB to stop. GDB only presents one hardware watchpoint
1600 hit at a time as the reason for stopping, and all the other hits
1601 are presented later, one after the other, each time the user
1602 requests the execution to be resumed. Execution is not resumed
1603 for the threads still having pending hit event stored in
1604 LWP_INFO->STATUS. While the watchpoint is already removed from
1605 the inferior on the first stop the thread hit event is kept being
1606 reported from its cached value by linux_nat_stopped_data_address
1607 until the real thread resume happens after the watchpoint gets
1608 presented and thus its LWP_INFO->STATUS gets reset.
1609
1610 Therefore the hardware watchpoint hit can get safely reset on the
1611 watchpoint removal from inferior. */
a79d3c27 1612
b40ce68a 1613static void
3a5c3e22 1614update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1615{
a5606eee 1616 int within_current_scope;
a5606eee 1617 struct frame_id saved_frame_id;
66076460 1618 int frame_saved;
a5606eee 1619
f6bc2008
PA
1620 /* If this is a local watchpoint, we only want to check if the
1621 watchpoint frame is in scope if the current thread is the thread
1622 that was used to create the watchpoint. */
1623 if (!watchpoint_in_thread_scope (b))
1624 return;
1625
3a5c3e22 1626 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1627 return;
1628
66076460 1629 frame_saved = 0;
a5606eee
VP
1630
1631 /* Determine if the watchpoint is within scope. */
1632 if (b->exp_valid_block == NULL)
1633 within_current_scope = 1;
1634 else
1635 {
b5db5dfc
UW
1636 struct frame_info *fi = get_current_frame ();
1637 struct gdbarch *frame_arch = get_frame_arch (fi);
1638 CORE_ADDR frame_pc = get_frame_pc (fi);
1639
1640 /* If we're in a function epilogue, unwinding may not work
1641 properly, so do not attempt to recreate locations at this
1642 point. See similar comments in watchpoint_check. */
1643 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1644 return;
66076460
DJ
1645
1646 /* Save the current frame's ID so we can restore it after
1647 evaluating the watchpoint expression on its own frame. */
1648 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1649 took a frame parameter, so that we didn't have to change the
1650 selected frame. */
1651 frame_saved = 1;
1652 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1653
a5606eee
VP
1654 fi = frame_find_by_id (b->watchpoint_frame);
1655 within_current_scope = (fi != NULL);
1656 if (within_current_scope)
1657 select_frame (fi);
1658 }
1659
b5db5dfc
UW
1660 /* We don't free locations. They are stored in the bp_location array
1661 and update_global_location_list will eventually delete them and
1662 remove breakpoints if needed. */
3a5c3e22 1663 b->base.loc = NULL;
b5db5dfc 1664
a5606eee
VP
1665 if (within_current_scope && reparse)
1666 {
1667 char *s;
d63d0675 1668
a5606eee
VP
1669 if (b->exp)
1670 {
1671 xfree (b->exp);
1672 b->exp = NULL;
1673 }
d63d0675 1674 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1675 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1676 /* If the meaning of expression itself changed, the old value is
1677 no longer relevant. We don't want to report a watchpoint hit
1678 to the user when the old value and the new value may actually
1679 be completely different objects. */
1680 value_free (b->val);
fa4727a6
DJ
1681 b->val = NULL;
1682 b->val_valid = 0;
60e1c644
PA
1683
1684 /* Note that unlike with breakpoints, the watchpoint's condition
1685 expression is stored in the breakpoint object, not in the
1686 locations (re)created below. */
3a5c3e22 1687 if (b->base.cond_string != NULL)
60e1c644
PA
1688 {
1689 if (b->cond_exp != NULL)
1690 {
1691 xfree (b->cond_exp);
1692 b->cond_exp = NULL;
1693 }
1694
3a5c3e22 1695 s = b->base.cond_string;
60e1c644
PA
1696 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1697 }
a5606eee 1698 }
a5606eee
VP
1699
1700 /* If we failed to parse the expression, for example because
1701 it refers to a global variable in a not-yet-loaded shared library,
1702 don't try to insert watchpoint. We don't automatically delete
1703 such watchpoint, though, since failure to parse expression
1704 is different from out-of-scope watchpoint. */
2d134ed3
PA
1705 if ( !target_has_execution)
1706 {
1707 /* Without execution, memory can't change. No use to try and
1708 set watchpoint locations. The watchpoint will be reset when
1709 the target gains execution, through breakpoint_re_set. */
1710 }
1711 else if (within_current_scope && b->exp)
a5606eee 1712 {
0cf6dd15 1713 int pc = 0;
fa4727a6 1714 struct value *val_chain, *v, *result, *next;
2d134ed3 1715 struct program_space *frame_pspace;
a5606eee 1716
0cf6dd15 1717 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1718
a5606eee
VP
1719 /* Avoid setting b->val if it's already set. The meaning of
1720 b->val is 'the last value' user saw, and we should update
1721 it only if we reported that last value to user. As it
9c06b0b4
TJB
1722 happens, the code that reports it updates b->val directly.
1723 We don't keep track of the memory value for masked
1724 watchpoints. */
3a5c3e22 1725 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1726 {
1727 b->val = v;
1728 b->val_valid = 1;
1729 }
a5606eee 1730
2d134ed3
PA
1731 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1732
a5606eee 1733 /* Look at each value on the value chain. */
9fa40276 1734 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1735 {
1736 /* If it's a memory location, and GDB actually needed
1737 its contents to evaluate the expression, then we
fa4727a6
DJ
1738 must watch it. If the first value returned is
1739 still lazy, that means an error occurred reading it;
1740 watch it anyway in case it becomes readable. */
a5606eee 1741 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1742 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1743 {
1744 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1745
a5606eee
VP
1746 /* We only watch structs and arrays if user asked
1747 for it explicitly, never if they just happen to
1748 appear in the middle of some value chain. */
fa4727a6 1749 if (v == result
a5606eee
VP
1750 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1751 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1752 {
1753 CORE_ADDR addr;
1754 int len, type;
1755 struct bp_location *loc, **tmp;
1756
42ae5230 1757 addr = value_address (v);
a5606eee
VP
1758 len = TYPE_LENGTH (value_type (v));
1759 type = hw_write;
3a5c3e22 1760 if (b->base.type == bp_read_watchpoint)
a5606eee 1761 type = hw_read;
3a5c3e22 1762 else if (b->base.type == bp_access_watchpoint)
a5606eee 1763 type = hw_access;
3a5c3e22
PA
1764
1765 loc = allocate_bp_location (&b->base);
1766 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1767 ;
1768 *tmp = loc;
a6d9a66e 1769 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1770
1771 loc->pspace = frame_pspace;
a5606eee
VP
1772 loc->address = addr;
1773 loc->length = len;
1774 loc->watchpoint_type = type;
1775 }
1776 }
9fa40276
TJB
1777 }
1778
1779 /* Change the type of breakpoint between hardware assisted or
1780 an ordinary watchpoint depending on the hardware support
1781 and free hardware slots. REPARSE is set when the inferior
1782 is started. */
a9634178 1783 if (reparse)
9fa40276 1784 {
e09342b5 1785 int reg_cnt;
9fa40276
TJB
1786 enum bp_loc_type loc_type;
1787 struct bp_location *bl;
a5606eee 1788
a9634178 1789 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1790
1791 if (reg_cnt)
9fa40276
TJB
1792 {
1793 int i, target_resources_ok, other_type_used;
a1398e0c 1794 enum bptype type;
9fa40276 1795
a9634178
TJB
1796 /* Use an exact watchpoint when there's only one memory region to be
1797 watched, and only one debug register is needed to watch it. */
1798 b->exact = target_exact_watchpoints && reg_cnt == 1;
1799
9fa40276 1800 /* We need to determine how many resources are already
e09342b5
TJB
1801 used for all other hardware watchpoints plus this one
1802 to see if we still have enough resources to also fit
a1398e0c
PA
1803 this watchpoint in as well. */
1804
1805 /* If this is a software watchpoint, we try to turn it
1806 to a hardware one -- count resources as if B was of
1807 hardware watchpoint type. */
1808 type = b->base.type;
1809 if (type == bp_watchpoint)
1810 type = bp_hardware_watchpoint;
1811
1812 /* This watchpoint may or may not have been placed on
1813 the list yet at this point (it won't be in the list
1814 if we're trying to create it for the first time,
1815 through watch_command), so always account for it
1816 manually. */
1817
1818 /* Count resources used by all watchpoints except B. */
1819 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1820
1821 /* Add in the resources needed for B. */
1822 i += hw_watchpoint_use_count (&b->base);
1823
1824 target_resources_ok
1825 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1826 if (target_resources_ok <= 0)
a9634178 1827 {
3a5c3e22 1828 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1829
1830 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1831 error (_("Target does not support this type of "
1832 "hardware watchpoint."));
9c06b0b4
TJB
1833 else if (target_resources_ok < 0 && !sw_mode)
1834 error (_("There are not enough available hardware "
1835 "resources for this watchpoint."));
a1398e0c
PA
1836
1837 /* Downgrade to software watchpoint. */
1838 b->base.type = bp_watchpoint;
1839 }
1840 else
1841 {
1842 /* If this was a software watchpoint, we've just
1843 found we have enough resources to turn it to a
1844 hardware watchpoint. Otherwise, this is a
1845 nop. */
1846 b->base.type = type;
a9634178 1847 }
9fa40276 1848 }
3a5c3e22 1849 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1850 error (_("Expression cannot be implemented with "
1851 "read/access watchpoint."));
9fa40276 1852 else
3a5c3e22 1853 b->base.type = bp_watchpoint;
9fa40276 1854
3a5c3e22 1855 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1856 : bp_loc_hardware_watchpoint);
3a5c3e22 1857 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1858 bl->loc_type = loc_type;
1859 }
1860
1861 for (v = val_chain; v; v = next)
1862 {
a5606eee
VP
1863 next = value_next (v);
1864 if (v != b->val)
1865 value_free (v);
1866 }
1867
c7437ca6
PA
1868 /* If a software watchpoint is not watching any memory, then the
1869 above left it without any location set up. But,
1870 bpstat_stop_status requires a location to be able to report
1871 stops, so make sure there's at least a dummy one. */
3a5c3e22 1872 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1873 {
3a5c3e22
PA
1874 struct breakpoint *base = &b->base;
1875 base->loc = allocate_bp_location (base);
1876 base->loc->pspace = frame_pspace;
1877 base->loc->address = -1;
1878 base->loc->length = -1;
1879 base->loc->watchpoint_type = -1;
c7437ca6 1880 }
a5606eee
VP
1881 }
1882 else if (!within_current_scope)
7270d8f2 1883 {
ac74f770
MS
1884 printf_filtered (_("\
1885Watchpoint %d deleted because the program has left the block\n\
1886in which its expression is valid.\n"),
3a5c3e22 1887 b->base.number);
d0fb5eae 1888 watchpoint_del_at_next_stop (b);
7270d8f2 1889 }
a5606eee
VP
1890
1891 /* Restore the selected frame. */
66076460
DJ
1892 if (frame_saved)
1893 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1894}
1895
a5606eee 1896
74960c60 1897/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1898 inserted in the inferior. We don't differentiate the type of BL's owner
1899 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1900 breakpoint_ops is not defined, because in insert_bp_location,
1901 tracepoint's insert_location will not be called. */
74960c60 1902static int
35df4500 1903should_be_inserted (struct bp_location *bl)
74960c60 1904{
35df4500 1905 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1906 return 0;
1907
35df4500 1908 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1909 return 0;
1910
35df4500 1911 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1912 return 0;
1913
f8eba3c6
TT
1914 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1915 return 0;
1916
56710373
PA
1917 /* This is set for example, when we're attached to the parent of a
1918 vfork, and have detached from the child. The child is running
1919 free, and we expect it to do an exec or exit, at which point the
1920 OS makes the parent schedulable again (and the target reports
1921 that the vfork is done). Until the child is done with the shared
1922 memory region, do not insert breakpoints in the parent, otherwise
1923 the child could still trip on the parent's breakpoints. Since
1924 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1925 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1926 return 0;
1927
74960c60
VP
1928 return 1;
1929}
1930
934709f0
PW
1931/* Same as should_be_inserted but does the check assuming
1932 that the location is not duplicated. */
1933
1934static int
1935unduplicated_should_be_inserted (struct bp_location *bl)
1936{
1937 int result;
1938 const int save_duplicate = bl->duplicate;
1939
1940 bl->duplicate = 0;
1941 result = should_be_inserted (bl);
1942 bl->duplicate = save_duplicate;
1943 return result;
1944}
1945
b775012e
LM
1946/* Parses a conditional described by an expression COND into an
1947 agent expression bytecode suitable for evaluation
1948 by the bytecode interpreter. Return NULL if there was
1949 any error during parsing. */
1950
1951static struct agent_expr *
1952parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
1953{
1954 struct agent_expr *aexpr = NULL;
1955 struct cleanup *old_chain = NULL;
1956 volatile struct gdb_exception ex;
1957
1958 if (!cond)
1959 return NULL;
1960
1961 /* We don't want to stop processing, so catch any errors
1962 that may show up. */
1963 TRY_CATCH (ex, RETURN_MASK_ERROR)
1964 {
1965 aexpr = gen_eval_for_expr (scope, cond);
1966 }
1967
1968 if (ex.reason < 0)
1969 {
1970 /* If we got here, it means the condition could not be parsed to a valid
1971 bytecode expression and thus can't be evaluated on the target's side.
1972 It's no use iterating through the conditions. */
1973 return NULL;
1974 }
1975
1976 /* We have a valid agent expression. */
1977 return aexpr;
1978}
1979
1980/* Based on location BL, create a list of breakpoint conditions to be
1981 passed on to the target. If we have duplicated locations with different
1982 conditions, we will add such conditions to the list. The idea is that the
1983 target will evaluate the list of conditions and will only notify GDB when
1984 one of them is true. */
1985
1986static void
1987build_target_condition_list (struct bp_location *bl)
1988{
1989 struct bp_location **locp = NULL, **loc2p;
1990 int null_condition_or_parse_error = 0;
1991 int modified = bl->needs_update;
1992 struct bp_location *loc;
1993
1994 /* This is only meaningful if the target is
1995 evaluating conditions and if the user has
1996 opted for condition evaluation on the target's
1997 side. */
1998 if (gdb_evaluates_breakpoint_condition_p ()
1999 || !target_supports_evaluation_of_breakpoint_conditions ())
2000 return;
2001
2002 /* Do a first pass to check for locations with no assigned
2003 conditions or conditions that fail to parse to a valid agent expression
2004 bytecode. If any of these happen, then it's no use to send conditions
2005 to the target since this location will always trigger and generate a
2006 response back to GDB. */
2007 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2008 {
2009 loc = (*loc2p);
2010 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2011 {
2012 if (modified)
2013 {
2014 struct agent_expr *aexpr;
2015
2016 /* Re-parse the conditions since something changed. In that
2017 case we already freed the condition bytecodes (see
2018 force_breakpoint_reinsertion). We just
2019 need to parse the condition to bytecodes again. */
2020 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2021 loc->cond_bytecode = aexpr;
2022
2023 /* Check if we managed to parse the conditional expression
2024 correctly. If not, we will not send this condition
2025 to the target. */
2026 if (aexpr)
2027 continue;
2028 }
2029
2030 /* If we have a NULL bytecode expression, it means something
2031 went wrong or we have a null condition expression. */
2032 if (!loc->cond_bytecode)
2033 {
2034 null_condition_or_parse_error = 1;
2035 break;
2036 }
2037 }
2038 }
2039
2040 /* If any of these happened, it means we will have to evaluate the conditions
2041 for the location's address on gdb's side. It is no use keeping bytecodes
2042 for all the other duplicate locations, thus we free all of them here.
2043
2044 This is so we have a finer control over which locations' conditions are
2045 being evaluated by GDB or the remote stub. */
2046 if (null_condition_or_parse_error)
2047 {
2048 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2049 {
2050 loc = (*loc2p);
2051 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2052 {
2053 /* Only go as far as the first NULL bytecode is
2054 located. */
2055 if (!loc->cond_bytecode)
2056 return;
2057
2058 free_agent_expr (loc->cond_bytecode);
2059 loc->cond_bytecode = NULL;
2060 }
2061 }
2062 }
2063
2064 /* No NULL conditions or failed bytecode generation. Build a condition list
2065 for this location's address. */
2066 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2067 {
2068 loc = (*loc2p);
2069 if (loc->cond
2070 && is_breakpoint (loc->owner)
2071 && loc->pspace->num == bl->pspace->num
2072 && loc->owner->enable_state == bp_enabled
2073 && loc->enabled)
2074 /* Add the condition to the vector. This will be used later to send the
2075 conditions to the target. */
2076 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2077 loc->cond_bytecode);
2078 }
2079
2080 return;
2081}
2082
35df4500
TJB
2083/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2084 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2085 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2086 Returns 0 for success, 1 if the bp_location type is not supported or
2087 -1 for failure.
879bfdc2 2088
4a64f543
MS
2089 NOTE drow/2003-09-09: This routine could be broken down to an
2090 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2091static int
35df4500 2092insert_bp_location (struct bp_location *bl,
26bb91f3 2093 struct ui_file *tmp_error_stream,
3fbb6ffa 2094 int *disabled_breaks,
26bb91f3 2095 int *hw_breakpoint_error)
879bfdc2
DJ
2096{
2097 int val = 0;
2098
b775012e 2099 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2100 return 0;
2101
35c63cd8
JB
2102 /* Note we don't initialize bl->target_info, as that wipes out
2103 the breakpoint location's shadow_contents if the breakpoint
2104 is still inserted at that location. This in turn breaks
2105 target_read_memory which depends on these buffers when
2106 a memory read is requested at the breakpoint location:
2107 Once the target_info has been wiped, we fail to see that
2108 we have a breakpoint inserted at that address and thus
2109 read the breakpoint instead of returning the data saved in
2110 the breakpoint location's shadow contents. */
35df4500
TJB
2111 bl->target_info.placed_address = bl->address;
2112 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2113 bl->target_info.length = bl->length;
8181d85f 2114
b775012e
LM
2115 /* When working with target-side conditions, we must pass all the conditions
2116 for the same breakpoint address down to the target since GDB will not
2117 insert those locations. With a list of breakpoint conditions, the target
2118 can decide when to stop and notify GDB. */
2119
2120 if (is_breakpoint (bl->owner))
2121 {
2122 build_target_condition_list (bl);
2123 /* Reset the condition modification marker. */
2124 bl->needs_update = 0;
2125 }
2126
35df4500
TJB
2127 if (bl->loc_type == bp_loc_software_breakpoint
2128 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2129 {
35df4500 2130 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2131 {
2132 /* If the explicitly specified breakpoint type
2133 is not hardware breakpoint, check the memory map to see
2134 if the breakpoint address is in read only memory or not.
4a64f543 2135
765dc015
VP
2136 Two important cases are:
2137 - location type is not hardware breakpoint, memory
2138 is readonly. We change the type of the location to
2139 hardware breakpoint.
4a64f543
MS
2140 - location type is hardware breakpoint, memory is
2141 read-write. This means we've previously made the
2142 location hardware one, but then the memory map changed,
2143 so we undo.
765dc015 2144
4a64f543
MS
2145 When breakpoints are removed, remove_breakpoints will use
2146 location types we've just set here, the only possible
2147 problem is that memory map has changed during running
2148 program, but it's not going to work anyway with current
2149 gdb. */
765dc015 2150 struct mem_region *mr
35df4500 2151 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2152
2153 if (mr)
2154 {
2155 if (automatic_hardware_breakpoints)
2156 {
765dc015
VP
2157 enum bp_loc_type new_type;
2158
2159 if (mr->attrib.mode != MEM_RW)
2160 new_type = bp_loc_hardware_breakpoint;
2161 else
2162 new_type = bp_loc_software_breakpoint;
2163
35df4500 2164 if (new_type != bl->loc_type)
765dc015
VP
2165 {
2166 static int said = 0;
cc59ec59 2167
35df4500 2168 bl->loc_type = new_type;
765dc015
VP
2169 if (!said)
2170 {
3e43a32a
MS
2171 fprintf_filtered (gdb_stdout,
2172 _("Note: automatically using "
2173 "hardware breakpoints for "
2174 "read-only addresses.\n"));
765dc015
VP
2175 said = 1;
2176 }
2177 }
2178 }
35df4500 2179 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2180 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2181 warning (_("cannot set software breakpoint "
2182 "at readonly address %s"),
35df4500 2183 paddress (bl->gdbarch, bl->address));
765dc015
VP
2184 }
2185 }
2186
879bfdc2
DJ
2187 /* First check to see if we have to handle an overlay. */
2188 if (overlay_debugging == ovly_off
35df4500
TJB
2189 || bl->section == NULL
2190 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2191 {
2192 /* No overlay handling: just set the breakpoint. */
2193
348d480f 2194 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2195 }
2196 else
2197 {
4a64f543 2198 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2199 Shall we set a breakpoint at the LMA? */
2200 if (!overlay_events_enabled)
2201 {
2202 /* Yes -- overlay event support is not active,
2203 so we must try to set a breakpoint at the LMA.
2204 This will not work for a hardware breakpoint. */
35df4500 2205 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2206 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2207 bl->owner->number);
879bfdc2
DJ
2208 else
2209 {
35df4500
TJB
2210 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2211 bl->section);
879bfdc2 2212 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2213 bl->overlay_target_info = bl->target_info;
2214 bl->overlay_target_info.placed_address = addr;
2215 val = target_insert_breakpoint (bl->gdbarch,
2216 &bl->overlay_target_info);
879bfdc2 2217 if (val != 0)
99361f52 2218 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2219 "Overlay breakpoint %d "
2220 "failed: in ROM?\n",
35df4500 2221 bl->owner->number);
879bfdc2
DJ
2222 }
2223 }
2224 /* Shall we set a breakpoint at the VMA? */
35df4500 2225 if (section_is_mapped (bl->section))
879bfdc2
DJ
2226 {
2227 /* Yes. This overlay section is mapped into memory. */
348d480f 2228 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2229 }
2230 else
2231 {
2232 /* No. This breakpoint will not be inserted.
2233 No error, but do not mark the bp as 'inserted'. */
2234 return 0;
2235 }
2236 }
2237
2238 if (val)
2239 {
2240 /* Can't set the breakpoint. */
35df4500 2241 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2242 {
4a64f543 2243 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2244 val = 0;
35df4500 2245 bl->shlib_disabled = 1;
8d3788bd 2246 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2247 if (!*disabled_breaks)
2248 {
2249 fprintf_unfiltered (tmp_error_stream,
2250 "Cannot insert breakpoint %d.\n",
2251 bl->owner->number);
2252 fprintf_unfiltered (tmp_error_stream,
2253 "Temporarily disabling shared "
2254 "library breakpoints:\n");
2255 }
2256 *disabled_breaks = 1;
879bfdc2 2257 fprintf_unfiltered (tmp_error_stream,
35df4500 2258 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2259 }
2260 else
879bfdc2 2261 {
35df4500 2262 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
2263 {
2264 *hw_breakpoint_error = 1;
3e43a32a
MS
2265 fprintf_unfiltered (tmp_error_stream,
2266 "Cannot insert hardware "
2267 "breakpoint %d.\n",
35df4500 2268 bl->owner->number);
879bfdc2
DJ
2269 }
2270 else
2271 {
2272 fprintf_unfiltered (tmp_error_stream,
2273 "Cannot insert breakpoint %d.\n",
35df4500 2274 bl->owner->number);
879bfdc2
DJ
2275 fprintf_filtered (tmp_error_stream,
2276 "Error accessing memory address ");
35df4500 2277 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2278 tmp_error_stream);
879bfdc2
DJ
2279 fprintf_filtered (tmp_error_stream, ": %s.\n",
2280 safe_strerror (val));
2281 }
2282
2283 }
2284 }
2285 else
35df4500 2286 bl->inserted = 1;
879bfdc2
DJ
2287
2288 return val;
2289 }
2290
35df4500 2291 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2292 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2293 watchpoints. It's not clear that it's necessary... */
35df4500 2294 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2295 {
77b06cd7
TJB
2296 gdb_assert (bl->owner->ops != NULL
2297 && bl->owner->ops->insert_location != NULL);
2298
2299 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2300
2301 /* If trying to set a read-watchpoint, and it turns out it's not
2302 supported, try emulating one with an access watchpoint. */
35df4500 2303 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2304 {
2305 struct bp_location *loc, **loc_temp;
2306
2307 /* But don't try to insert it, if there's already another
2308 hw_access location that would be considered a duplicate
2309 of this one. */
2310 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2311 if (loc != bl
85d721b8 2312 && loc->watchpoint_type == hw_access
35df4500 2313 && watchpoint_locations_match (bl, loc))
85d721b8 2314 {
35df4500
TJB
2315 bl->duplicate = 1;
2316 bl->inserted = 1;
2317 bl->target_info = loc->target_info;
2318 bl->watchpoint_type = hw_access;
85d721b8
PA
2319 val = 0;
2320 break;
2321 }
2322
2323 if (val == 1)
2324 {
77b06cd7
TJB
2325 bl->watchpoint_type = hw_access;
2326 val = bl->owner->ops->insert_location (bl);
2327
2328 if (val)
2329 /* Back to the original value. */
2330 bl->watchpoint_type = hw_read;
85d721b8
PA
2331 }
2332 }
2333
35df4500 2334 bl->inserted = (val == 0);
879bfdc2
DJ
2335 }
2336
35df4500 2337 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2338 {
77b06cd7
TJB
2339 gdb_assert (bl->owner->ops != NULL
2340 && bl->owner->ops->insert_location != NULL);
2341
2342 val = bl->owner->ops->insert_location (bl);
2343 if (val)
2344 {
2345 bl->owner->enable_state = bp_disabled;
2346
2347 if (val == 1)
2348 warning (_("\
2349Error inserting catchpoint %d: Your system does not support this type\n\
2350of catchpoint."), bl->owner->number);
2351 else
2352 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2353 }
2354
2355 bl->inserted = (val == 0);
1640b821
DJ
2356
2357 /* We've already printed an error message if there was a problem
2358 inserting this catchpoint, and we've disabled the catchpoint,
2359 so just return success. */
2360 return 0;
879bfdc2
DJ
2361 }
2362
2363 return 0;
2364}
2365
6c95b8df
PA
2366/* This function is called when program space PSPACE is about to be
2367 deleted. It takes care of updating breakpoints to not reference
2368 PSPACE anymore. */
2369
2370void
2371breakpoint_program_space_exit (struct program_space *pspace)
2372{
2373 struct breakpoint *b, *b_temp;
876fa593 2374 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2375
2376 /* Remove any breakpoint that was set through this program space. */
2377 ALL_BREAKPOINTS_SAFE (b, b_temp)
2378 {
2379 if (b->pspace == pspace)
2380 delete_breakpoint (b);
2381 }
2382
2383 /* Breakpoints set through other program spaces could have locations
2384 bound to PSPACE as well. Remove those. */
876fa593 2385 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2386 {
2387 struct bp_location *tmp;
2388
2389 if (loc->pspace == pspace)
2390 {
2bdf28a0 2391 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2392 if (loc->owner->loc == loc)
2393 loc->owner->loc = loc->next;
2394 else
2395 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2396 if (tmp->next == loc)
2397 {
2398 tmp->next = loc->next;
2399 break;
2400 }
2401 }
2402 }
2403
2404 /* Now update the global location list to permanently delete the
2405 removed locations above. */
2406 update_global_location_list (0);
2407}
2408
74960c60
VP
2409/* Make sure all breakpoints are inserted in inferior.
2410 Throws exception on any error.
2411 A breakpoint that is already inserted won't be inserted
2412 again, so calling this function twice is safe. */
2413void
2414insert_breakpoints (void)
2415{
2416 struct breakpoint *bpt;
2417
2418 ALL_BREAKPOINTS (bpt)
2419 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2420 {
2421 struct watchpoint *w = (struct watchpoint *) bpt;
2422
2423 update_watchpoint (w, 0 /* don't reparse. */);
2424 }
74960c60 2425
b60e7edf 2426 update_global_location_list (1);
74960c60 2427
c35b1492
PA
2428 /* update_global_location_list does not insert breakpoints when
2429 always_inserted_mode is not enabled. Explicitly insert them
2430 now. */
2431 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2432 insert_breakpoint_locations ();
2433}
2434
20388dd6
YQ
2435/* Invoke CALLBACK for each of bp_location. */
2436
2437void
2438iterate_over_bp_locations (walk_bp_location_callback callback)
2439{
2440 struct bp_location *loc, **loc_tmp;
2441
2442 ALL_BP_LOCATIONS (loc, loc_tmp)
2443 {
2444 callback (loc, NULL);
2445 }
2446}
2447
b775012e
LM
2448/* This is used when we need to synch breakpoint conditions between GDB and the
2449 target. It is the case with deleting and disabling of breakpoints when using
2450 always-inserted mode. */
2451
2452static void
2453update_inserted_breakpoint_locations (void)
2454{
2455 struct bp_location *bl, **blp_tmp;
2456 int error_flag = 0;
2457 int val = 0;
2458 int disabled_breaks = 0;
2459 int hw_breakpoint_error = 0;
2460
2461 struct ui_file *tmp_error_stream = mem_fileopen ();
2462 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2463
2464 /* Explicitly mark the warning -- this will only be printed if
2465 there was an error. */
2466 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2467
2468 save_current_space_and_thread ();
2469
2470 ALL_BP_LOCATIONS (bl, blp_tmp)
2471 {
2472 /* We only want to update software breakpoints and hardware
2473 breakpoints. */
2474 if (!is_breakpoint (bl->owner))
2475 continue;
2476
2477 /* We only want to update locations that are already inserted
2478 and need updating. This is to avoid unwanted insertion during
2479 deletion of breakpoints. */
2480 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2481 continue;
2482
2483 switch_to_program_space_and_thread (bl->pspace);
2484
2485 /* For targets that support global breakpoints, there's no need
2486 to select an inferior to insert breakpoint to. In fact, even
2487 if we aren't attached to any process yet, we should still
2488 insert breakpoints. */
2489 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2490 && ptid_equal (inferior_ptid, null_ptid))
2491 continue;
2492
2493 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2494 &hw_breakpoint_error);
2495 if (val)
2496 error_flag = val;
2497 }
2498
2499 if (error_flag)
2500 {
2501 target_terminal_ours_for_output ();
2502 error_stream (tmp_error_stream);
2503 }
2504
2505 do_cleanups (cleanups);
2506}
2507
c30eee59 2508/* Used when starting or continuing the program. */
c906108c 2509
74960c60
VP
2510static void
2511insert_breakpoint_locations (void)
c906108c 2512{
a5606eee 2513 struct breakpoint *bpt;
35df4500 2514 struct bp_location *bl, **blp_tmp;
eacd795a 2515 int error_flag = 0;
c906108c 2516 int val = 0;
3fbb6ffa 2517 int disabled_breaks = 0;
81d0cc19 2518 int hw_breakpoint_error = 0;
c906108c 2519
81d0cc19 2520 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2521 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2522
81d0cc19
GS
2523 /* Explicitly mark the warning -- this will only be printed if
2524 there was an error. */
2525 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2526
2527 save_current_space_and_thread ();
2528
35df4500 2529 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2530 {
b775012e 2531 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2532 continue;
2533
4a64f543
MS
2534 /* There is no point inserting thread-specific breakpoints if
2535 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2536 has BL->OWNER always non-NULL. */
35df4500
TJB
2537 if (bl->owner->thread != -1
2538 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2539 continue;
2540
35df4500 2541 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2542
2543 /* For targets that support global breakpoints, there's no need
2544 to select an inferior to insert breakpoint to. In fact, even
2545 if we aren't attached to any process yet, we should still
2546 insert breakpoints. */
2547 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2548 && ptid_equal (inferior_ptid, null_ptid))
2549 continue;
2550
3fbb6ffa
TJB
2551 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2552 &hw_breakpoint_error);
879bfdc2 2553 if (val)
eacd795a 2554 error_flag = val;
879bfdc2 2555 }
c906108c 2556
4a64f543
MS
2557 /* If we failed to insert all locations of a watchpoint, remove
2558 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2559 ALL_BREAKPOINTS (bpt)
2560 {
2561 int some_failed = 0;
2562 struct bp_location *loc;
2563
2564 if (!is_hardware_watchpoint (bpt))
2565 continue;
2566
d6b74ac4 2567 if (!breakpoint_enabled (bpt))
a5606eee 2568 continue;
74960c60
VP
2569
2570 if (bpt->disposition == disp_del_at_next_stop)
2571 continue;
a5606eee
VP
2572
2573 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2574 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2575 {
2576 some_failed = 1;
2577 break;
2578 }
2579 if (some_failed)
2580 {
2581 for (loc = bpt->loc; loc; loc = loc->next)
2582 if (loc->inserted)
2583 remove_breakpoint (loc, mark_uninserted);
2584
2585 hw_breakpoint_error = 1;
2586 fprintf_unfiltered (tmp_error_stream,
2587 "Could not insert hardware watchpoint %d.\n",
2588 bpt->number);
eacd795a 2589 error_flag = -1;
a5606eee
VP
2590 }
2591 }
2592
eacd795a 2593 if (error_flag)
81d0cc19
GS
2594 {
2595 /* If a hardware breakpoint or watchpoint was inserted, add a
2596 message about possibly exhausted resources. */
879bfdc2 2597 if (hw_breakpoint_error)
81d0cc19 2598 {
c6510018
MS
2599 fprintf_unfiltered (tmp_error_stream,
2600 "Could not insert hardware breakpoints:\n\
2601You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2602 }
81d0cc19
GS
2603 target_terminal_ours_for_output ();
2604 error_stream (tmp_error_stream);
2605 }
f7545552
TT
2606
2607 do_cleanups (cleanups);
c906108c
SS
2608}
2609
c30eee59
TJB
2610/* Used when the program stops.
2611 Returns zero if successful, or non-zero if there was a problem
2612 removing a breakpoint location. */
2613
c906108c 2614int
fba45db2 2615remove_breakpoints (void)
c906108c 2616{
35df4500 2617 struct bp_location *bl, **blp_tmp;
3a1bae8e 2618 int val = 0;
c906108c 2619
35df4500 2620 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2621 {
1e4d1764 2622 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2623 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2624 }
3a1bae8e 2625 return val;
c906108c
SS
2626}
2627
6c95b8df
PA
2628/* Remove breakpoints of process PID. */
2629
2630int
2631remove_breakpoints_pid (int pid)
2632{
35df4500 2633 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2634 int val;
2635 struct inferior *inf = find_inferior_pid (pid);
2636
35df4500 2637 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2638 {
35df4500 2639 if (bl->pspace != inf->pspace)
6c95b8df
PA
2640 continue;
2641
35df4500 2642 if (bl->inserted)
6c95b8df 2643 {
35df4500 2644 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2645 if (val != 0)
2646 return val;
2647 }
2648 }
2649 return 0;
2650}
2651
c906108c 2652int
fba45db2 2653reattach_breakpoints (int pid)
c906108c 2654{
6c95b8df 2655 struct cleanup *old_chain;
35df4500 2656 struct bp_location *bl, **blp_tmp;
c906108c 2657 int val;
86b887df 2658 struct ui_file *tmp_error_stream;
3fbb6ffa 2659 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2660 struct inferior *inf;
2661 struct thread_info *tp;
2662
2663 tp = any_live_thread_of_process (pid);
2664 if (tp == NULL)
2665 return 1;
2666
2667 inf = find_inferior_pid (pid);
2668 old_chain = save_inferior_ptid ();
2669
2670 inferior_ptid = tp->ptid;
a4954f26 2671
86b887df 2672 tmp_error_stream = mem_fileopen ();
a4954f26 2673 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2674
35df4500 2675 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2676 {
35df4500 2677 if (bl->pspace != inf->pspace)
6c95b8df
PA
2678 continue;
2679
35df4500 2680 if (bl->inserted)
c5aa993b 2681 {
35df4500 2682 bl->inserted = 0;
3fbb6ffa 2683 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2684 if (val != 0)
2685 {
ce696e05 2686 do_cleanups (old_chain);
c5aa993b
JM
2687 return val;
2688 }
2689 }
2690 }
ce696e05 2691 do_cleanups (old_chain);
c906108c
SS
2692 return 0;
2693}
2694
e58b0e63
PA
2695static int internal_breakpoint_number = -1;
2696
84f4c1fe
PM
2697/* Set the breakpoint number of B, depending on the value of INTERNAL.
2698 If INTERNAL is non-zero, the breakpoint number will be populated
2699 from internal_breakpoint_number and that variable decremented.
e5dd4106 2700 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2701 breakpoint_count and that value incremented. Internal breakpoints
2702 do not set the internal var bpnum. */
2703static void
2704set_breakpoint_number (int internal, struct breakpoint *b)
2705{
2706 if (internal)
2707 b->number = internal_breakpoint_number--;
2708 else
2709 {
2710 set_breakpoint_count (breakpoint_count + 1);
2711 b->number = breakpoint_count;
2712 }
2713}
2714
e62c965a 2715static struct breakpoint *
a6d9a66e 2716create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2717 CORE_ADDR address, enum bptype type,
c0a91b2b 2718 const struct breakpoint_ops *ops)
e62c965a 2719{
e62c965a
PP
2720 struct symtab_and_line sal;
2721 struct breakpoint *b;
2722
4a64f543 2723 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2724
2725 sal.pc = address;
2726 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2727 sal.pspace = current_program_space;
e62c965a 2728
06edf0c0 2729 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2730 b->number = internal_breakpoint_number--;
2731 b->disposition = disp_donttouch;
2732
2733 return b;
2734}
2735
17450429
PP
2736static const char *const longjmp_names[] =
2737 {
2738 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2739 };
2740#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2741
2742/* Per-objfile data private to breakpoint.c. */
2743struct breakpoint_objfile_data
2744{
2745 /* Minimal symbol for "_ovly_debug_event" (if any). */
2746 struct minimal_symbol *overlay_msym;
2747
2748 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2749 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2750
28106bc2
SDJ
2751 /* True if we have looked for longjmp probes. */
2752 int longjmp_searched;
2753
2754 /* SystemTap probe points for longjmp (if any). */
2755 VEC (probe_p) *longjmp_probes;
2756
17450429
PP
2757 /* Minimal symbol for "std::terminate()" (if any). */
2758 struct minimal_symbol *terminate_msym;
2759
2760 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2761 struct minimal_symbol *exception_msym;
28106bc2
SDJ
2762
2763 /* True if we have looked for exception probes. */
2764 int exception_searched;
2765
2766 /* SystemTap probe points for unwinding (if any). */
2767 VEC (probe_p) *exception_probes;
17450429
PP
2768};
2769
2770static const struct objfile_data *breakpoint_objfile_key;
2771
2772/* Minimal symbol not found sentinel. */
2773static struct minimal_symbol msym_not_found;
2774
2775/* Returns TRUE if MSYM point to the "not found" sentinel. */
2776
2777static int
2778msym_not_found_p (const struct minimal_symbol *msym)
2779{
2780 return msym == &msym_not_found;
2781}
2782
2783/* Return per-objfile data needed by breakpoint.c.
2784 Allocate the data if necessary. */
2785
2786static struct breakpoint_objfile_data *
2787get_breakpoint_objfile_data (struct objfile *objfile)
2788{
2789 struct breakpoint_objfile_data *bp_objfile_data;
2790
2791 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2792 if (bp_objfile_data == NULL)
2793 {
2794 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2795 sizeof (*bp_objfile_data));
2796
2797 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2798 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2799 }
2800 return bp_objfile_data;
2801}
2802
28106bc2
SDJ
2803static void
2804free_breakpoint_probes (struct objfile *obj, void *data)
2805{
2806 struct breakpoint_objfile_data *bp_objfile_data = data;
2807
2808 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2809 VEC_free (probe_p, bp_objfile_data->exception_probes);
2810}
2811
e62c965a 2812static void
af02033e 2813create_overlay_event_breakpoint (void)
e62c965a 2814{
69de3c6a 2815 struct objfile *objfile;
af02033e 2816 const char *const func_name = "_ovly_debug_event";
e62c965a 2817
69de3c6a
PP
2818 ALL_OBJFILES (objfile)
2819 {
2820 struct breakpoint *b;
17450429
PP
2821 struct breakpoint_objfile_data *bp_objfile_data;
2822 CORE_ADDR addr;
69de3c6a 2823
17450429
PP
2824 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2825
2826 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2827 continue;
2828
2829 if (bp_objfile_data->overlay_msym == NULL)
2830 {
2831 struct minimal_symbol *m;
2832
2833 m = lookup_minimal_symbol_text (func_name, objfile);
2834 if (m == NULL)
2835 {
2836 /* Avoid future lookups in this objfile. */
2837 bp_objfile_data->overlay_msym = &msym_not_found;
2838 continue;
2839 }
2840 bp_objfile_data->overlay_msym = m;
2841 }
e62c965a 2842
17450429
PP
2843 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2844 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2845 bp_overlay_event,
2846 &internal_breakpoint_ops);
69de3c6a 2847 b->addr_string = xstrdup (func_name);
e62c965a 2848
69de3c6a
PP
2849 if (overlay_debugging == ovly_auto)
2850 {
2851 b->enable_state = bp_enabled;
2852 overlay_events_enabled = 1;
2853 }
2854 else
2855 {
2856 b->enable_state = bp_disabled;
2857 overlay_events_enabled = 0;
2858 }
e62c965a
PP
2859 }
2860 update_global_location_list (1);
2861}
2862
0fd8e87f 2863static void
af02033e 2864create_longjmp_master_breakpoint (void)
0fd8e87f 2865{
6c95b8df 2866 struct program_space *pspace;
6c95b8df
PA
2867 struct cleanup *old_chain;
2868
2869 old_chain = save_current_program_space ();
0fd8e87f 2870
6c95b8df 2871 ALL_PSPACES (pspace)
af02033e
PP
2872 {
2873 struct objfile *objfile;
2874
2875 set_current_program_space (pspace);
2876
2877 ALL_OBJFILES (objfile)
0fd8e87f 2878 {
af02033e
PP
2879 int i;
2880 struct gdbarch *gdbarch;
17450429 2881 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2882
af02033e
PP
2883 gdbarch = get_objfile_arch (objfile);
2884 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2885 continue;
2886
17450429
PP
2887 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2888
28106bc2
SDJ
2889 if (!bp_objfile_data->longjmp_searched)
2890 {
2891 bp_objfile_data->longjmp_probes
2892 = find_probes_in_objfile (objfile, "libc", "longjmp");
2893 bp_objfile_data->longjmp_searched = 1;
2894 }
2895
2896 if (bp_objfile_data->longjmp_probes != NULL)
2897 {
2898 int i;
2899 struct probe *probe;
2900 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2901
2902 for (i = 0;
2903 VEC_iterate (probe_p,
2904 bp_objfile_data->longjmp_probes,
2905 i, probe);
2906 ++i)
2907 {
2908 struct breakpoint *b;
2909
2910 b = create_internal_breakpoint (gdbarch, probe->address,
2911 bp_longjmp_master,
2912 &internal_breakpoint_ops);
2913 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2914 b->enable_state = bp_disabled;
2915 }
2916
2917 continue;
2918 }
2919
17450429 2920 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2921 {
2922 struct breakpoint *b;
af02033e 2923 const char *func_name;
17450429 2924 CORE_ADDR addr;
6c95b8df 2925
17450429 2926 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2927 continue;
0fd8e87f 2928
17450429
PP
2929 func_name = longjmp_names[i];
2930 if (bp_objfile_data->longjmp_msym[i] == NULL)
2931 {
2932 struct minimal_symbol *m;
2933
2934 m = lookup_minimal_symbol_text (func_name, objfile);
2935 if (m == NULL)
2936 {
2937 /* Prevent future lookups in this objfile. */
2938 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2939 continue;
2940 }
2941 bp_objfile_data->longjmp_msym[i] = m;
2942 }
2943
2944 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
2945 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
2946 &internal_breakpoint_ops);
af02033e
PP
2947 b->addr_string = xstrdup (func_name);
2948 b->enable_state = bp_disabled;
2949 }
0fd8e87f 2950 }
af02033e 2951 }
0fd8e87f 2952 update_global_location_list (1);
6c95b8df
PA
2953
2954 do_cleanups (old_chain);
0fd8e87f
UW
2955}
2956
af02033e 2957/* Create a master std::terminate breakpoint. */
aa7d318d 2958static void
af02033e 2959create_std_terminate_master_breakpoint (void)
aa7d318d
TT
2960{
2961 struct program_space *pspace;
aa7d318d 2962 struct cleanup *old_chain;
af02033e 2963 const char *const func_name = "std::terminate()";
aa7d318d
TT
2964
2965 old_chain = save_current_program_space ();
2966
2967 ALL_PSPACES (pspace)
17450429
PP
2968 {
2969 struct objfile *objfile;
2970 CORE_ADDR addr;
2971
2972 set_current_program_space (pspace);
2973
aa7d318d
TT
2974 ALL_OBJFILES (objfile)
2975 {
2976 struct breakpoint *b;
17450429 2977 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 2978
17450429 2979 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 2980
17450429
PP
2981 if (msym_not_found_p (bp_objfile_data->terminate_msym))
2982 continue;
2983
2984 if (bp_objfile_data->terminate_msym == NULL)
2985 {
2986 struct minimal_symbol *m;
2987
2988 m = lookup_minimal_symbol (func_name, NULL, objfile);
2989 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
2990 && MSYMBOL_TYPE (m) != mst_file_text))
2991 {
2992 /* Prevent future lookups in this objfile. */
2993 bp_objfile_data->terminate_msym = &msym_not_found;
2994 continue;
2995 }
2996 bp_objfile_data->terminate_msym = m;
2997 }
aa7d318d 2998
17450429
PP
2999 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3000 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3001 bp_std_terminate_master,
3002 &internal_breakpoint_ops);
aa7d318d
TT
3003 b->addr_string = xstrdup (func_name);
3004 b->enable_state = bp_disabled;
3005 }
17450429
PP
3006 }
3007
aa7d318d
TT
3008 update_global_location_list (1);
3009
3010 do_cleanups (old_chain);
3011}
3012
186c406b
TT
3013/* Install a master breakpoint on the unwinder's debug hook. */
3014
70221824 3015static void
186c406b
TT
3016create_exception_master_breakpoint (void)
3017{
3018 struct objfile *objfile;
17450429 3019 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3020
3021 ALL_OBJFILES (objfile)
3022 {
17450429
PP
3023 struct breakpoint *b;
3024 struct gdbarch *gdbarch;
3025 struct breakpoint_objfile_data *bp_objfile_data;
3026 CORE_ADDR addr;
3027
3028 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3029
28106bc2
SDJ
3030 /* We prefer the SystemTap probe point if it exists. */
3031 if (!bp_objfile_data->exception_searched)
3032 {
3033 bp_objfile_data->exception_probes
3034 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3035 bp_objfile_data->exception_searched = 1;
3036 }
3037
3038 if (bp_objfile_data->exception_probes != NULL)
3039 {
3040 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3041 int i;
3042 struct probe *probe;
3043
3044 for (i = 0;
3045 VEC_iterate (probe_p,
3046 bp_objfile_data->exception_probes,
3047 i, probe);
3048 ++i)
3049 {
3050 struct breakpoint *b;
3051
3052 b = create_internal_breakpoint (gdbarch, probe->address,
3053 bp_exception_master,
3054 &internal_breakpoint_ops);
3055 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3056 b->enable_state = bp_disabled;
3057 }
3058
3059 continue;
3060 }
3061
3062 /* Otherwise, try the hook function. */
3063
17450429
PP
3064 if (msym_not_found_p (bp_objfile_data->exception_msym))
3065 continue;
3066
3067 gdbarch = get_objfile_arch (objfile);
186c406b 3068
17450429 3069 if (bp_objfile_data->exception_msym == NULL)
186c406b 3070 {
17450429 3071 struct minimal_symbol *debug_hook;
186c406b 3072
17450429
PP
3073 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3074 if (debug_hook == NULL)
3075 {
3076 bp_objfile_data->exception_msym = &msym_not_found;
3077 continue;
3078 }
3079
3080 bp_objfile_data->exception_msym = debug_hook;
186c406b 3081 }
17450429
PP
3082
3083 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3084 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3085 &current_target);
06edf0c0
PA
3086 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3087 &internal_breakpoint_ops);
17450429
PP
3088 b->addr_string = xstrdup (func_name);
3089 b->enable_state = bp_disabled;
186c406b
TT
3090 }
3091
3092 update_global_location_list (1);
3093}
3094
c906108c 3095void
fba45db2 3096update_breakpoints_after_exec (void)
c906108c 3097{
35df4500 3098 struct breakpoint *b, *b_tmp;
876fa593 3099 struct bp_location *bploc, **bplocp_tmp;
c906108c 3100
25b22b0a
PA
3101 /* We're about to delete breakpoints from GDB's lists. If the
3102 INSERTED flag is true, GDB will try to lift the breakpoints by
3103 writing the breakpoints' "shadow contents" back into memory. The
3104 "shadow contents" are NOT valid after an exec, so GDB should not
3105 do that. Instead, the target is responsible from marking
3106 breakpoints out as soon as it detects an exec. We don't do that
3107 here instead, because there may be other attempts to delete
3108 breakpoints after detecting an exec and before reaching here. */
876fa593 3109 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3110 if (bploc->pspace == current_program_space)
3111 gdb_assert (!bploc->inserted);
c906108c 3112
35df4500 3113 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3114 {
6c95b8df
PA
3115 if (b->pspace != current_program_space)
3116 continue;
3117
4a64f543 3118 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3119 if (b->type == bp_shlib_event)
3120 {
3121 delete_breakpoint (b);
3122 continue;
3123 }
c906108c 3124
4a64f543 3125 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3126 if (b->type == bp_jit_event)
3127 {
3128 delete_breakpoint (b);
3129 continue;
3130 }
3131
1900040c 3132 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3133 as must overlay event and longjmp master breakpoints. */
3134 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3135 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3136 || b->type == bp_exception_master)
c4093a6a
JM
3137 {
3138 delete_breakpoint (b);
3139 continue;
3140 }
3141
4a64f543 3142 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3143 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3144 {
3145 delete_breakpoint (b);
3146 continue;
3147 }
3148
611c83ae
PA
3149 /* Longjmp and longjmp-resume breakpoints are also meaningless
3150 after an exec. */
186c406b
TT
3151 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3152 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3153 {
3154 delete_breakpoint (b);
3155 continue;
3156 }
3157
ce78b96d
JB
3158 if (b->type == bp_catchpoint)
3159 {
3160 /* For now, none of the bp_catchpoint breakpoints need to
3161 do anything at this point. In the future, if some of
3162 the catchpoints need to something, we will need to add
3163 a new method, and call this method from here. */
3164 continue;
3165 }
3166
c5aa993b
JM
3167 /* bp_finish is a special case. The only way we ought to be able
3168 to see one of these when an exec() has happened, is if the user
3169 caught a vfork, and then said "finish". Ordinarily a finish just
3170 carries them to the call-site of the current callee, by setting
3171 a temporary bp there and resuming. But in this case, the finish
3172 will carry them entirely through the vfork & exec.
3173
3174 We don't want to allow a bp_finish to remain inserted now. But
3175 we can't safely delete it, 'cause finish_command has a handle to
3176 the bp on a bpstat, and will later want to delete it. There's a
3177 chance (and I've seen it happen) that if we delete the bp_finish
3178 here, that its storage will get reused by the time finish_command
3179 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3180 We really must allow finish_command to delete a bp_finish.
3181
e5dd4106 3182 In the absence of a general solution for the "how do we know
53a5351d
JM
3183 it's safe to delete something others may have handles to?"
3184 problem, what we'll do here is just uninsert the bp_finish, and
3185 let finish_command delete it.
3186
3187 (We know the bp_finish is "doomed" in the sense that it's
3188 momentary, and will be deleted as soon as finish_command sees
3189 the inferior stopped. So it doesn't matter that the bp's
3190 address is probably bogus in the new a.out, unlike e.g., the
3191 solib breakpoints.) */
c5aa993b 3192
c5aa993b
JM
3193 if (b->type == bp_finish)
3194 {
3195 continue;
3196 }
3197
3198 /* Without a symbolic address, we have little hope of the
3199 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3200 a.out. */
c5aa993b
JM
3201 if (b->addr_string == NULL)
3202 {
3203 delete_breakpoint (b);
3204 continue;
3205 }
c5aa993b 3206 }
1900040c 3207 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3208 create_overlay_event_breakpoint ();
3209 create_longjmp_master_breakpoint ();
3210 create_std_terminate_master_breakpoint ();
186c406b 3211 create_exception_master_breakpoint ();
c906108c
SS
3212}
3213
3214int
fba45db2 3215detach_breakpoints (int pid)
c906108c 3216{
35df4500 3217 struct bp_location *bl, **blp_tmp;
3a1bae8e 3218 int val = 0;
ce696e05 3219 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3220 struct inferior *inf = current_inferior ();
c5aa993b 3221
39f77062 3222 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 3223 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3224
6c95b8df 3225 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 3226 inferior_ptid = pid_to_ptid (pid);
35df4500 3227 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3228 {
35df4500 3229 if (bl->pspace != inf->pspace)
6c95b8df
PA
3230 continue;
3231
35df4500
TJB
3232 if (bl->inserted)
3233 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3234 }
d03285ec
UW
3235
3236 /* Detach single-step breakpoints as well. */
3237 detach_single_step_breakpoints ();
3238
ce696e05 3239 do_cleanups (old_chain);
3a1bae8e 3240 return val;
c906108c
SS
3241}
3242
35df4500 3243/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3244 Note that this is used to detach breakpoints from a child fork.
3245 When we get here, the child isn't in the inferior list, and neither
3246 do we have objects to represent its address space --- we should
35df4500 3247 *not* look at bl->pspace->aspace here. */
6c95b8df 3248
c906108c 3249static int
35df4500 3250remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3251{
3252 int val;
c5aa993b 3253
35df4500
TJB
3254 /* BL is never in moribund_locations by our callers. */
3255 gdb_assert (bl->owner != NULL);
2bdf28a0 3256
35df4500 3257 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3258 /* Permanent breakpoints cannot be inserted or removed. */
3259 return 0;
3260
74960c60
VP
3261 /* The type of none suggests that owner is actually deleted.
3262 This should not ever happen. */
35df4500 3263 gdb_assert (bl->owner->type != bp_none);
0bde7532 3264
35df4500
TJB
3265 if (bl->loc_type == bp_loc_software_breakpoint
3266 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3267 {
c02f5703
MS
3268 /* "Normal" instruction breakpoint: either the standard
3269 trap-instruction bp (bp_breakpoint), or a
3270 bp_hardware_breakpoint. */
3271
3272 /* First check to see if we have to handle an overlay. */
3273 if (overlay_debugging == ovly_off
35df4500
TJB
3274 || bl->section == NULL
3275 || !(section_is_overlay (bl->section)))
c02f5703
MS
3276 {
3277 /* No overlay handling: just remove the breakpoint. */
348d480f 3278 val = bl->owner->ops->remove_location (bl);
c02f5703 3279 }
c906108c
SS
3280 else
3281 {
4a64f543 3282 /* This breakpoint is in an overlay section.
c02f5703
MS
3283 Did we set a breakpoint at the LMA? */
3284 if (!overlay_events_enabled)
3285 {
3286 /* Yes -- overlay event support is not active, so we
3287 should have set a breakpoint at the LMA. Remove it.
3288 */
c02f5703
MS
3289 /* Ignore any failures: if the LMA is in ROM, we will
3290 have already warned when we failed to insert it. */
35df4500
TJB
3291 if (bl->loc_type == bp_loc_hardware_breakpoint)
3292 target_remove_hw_breakpoint (bl->gdbarch,
3293 &bl->overlay_target_info);
c02f5703 3294 else
35df4500
TJB
3295 target_remove_breakpoint (bl->gdbarch,
3296 &bl->overlay_target_info);
c02f5703
MS
3297 }
3298 /* Did we set a breakpoint at the VMA?
3299 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3300 if (bl->inserted)
c906108c 3301 {
c02f5703
MS
3302 /* Yes -- remove it. Previously we did not bother to
3303 remove the breakpoint if the section had been
3304 unmapped, but let's not rely on that being safe. We
3305 don't know what the overlay manager might do. */
aa67235e
UW
3306
3307 /* However, we should remove *software* breakpoints only
3308 if the section is still mapped, or else we overwrite
3309 wrong code with the saved shadow contents. */
348d480f
PA
3310 if (bl->loc_type == bp_loc_hardware_breakpoint
3311 || section_is_mapped (bl->section))
3312 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3313 else
3314 val = 0;
c906108c 3315 }
c02f5703
MS
3316 else
3317 {
3318 /* No -- not inserted, so no need to remove. No error. */
3319 val = 0;
3320 }
c906108c 3321 }
879d1e6b
UW
3322
3323 /* In some cases, we might not be able to remove a breakpoint
3324 in a shared library that has already been removed, but we
3325 have not yet processed the shlib unload event. */
35df4500 3326 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3327 val = 0;
3328
c906108c
SS
3329 if (val)
3330 return val;
35df4500 3331 bl->inserted = (is == mark_inserted);
c906108c 3332 }
35df4500 3333 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3334 {
77b06cd7
TJB
3335 gdb_assert (bl->owner->ops != NULL
3336 && bl->owner->ops->remove_location != NULL);
3337
35df4500 3338 bl->inserted = (is == mark_inserted);
77b06cd7 3339 bl->owner->ops->remove_location (bl);
2e70b7b9 3340
c906108c 3341 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3342 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3343 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3344 bl->owner->number);
c906108c 3345 }
35df4500
TJB
3346 else if (bl->owner->type == bp_catchpoint
3347 && breakpoint_enabled (bl->owner)
3348 && !bl->duplicate)
ce78b96d 3349 {
77b06cd7
TJB
3350 gdb_assert (bl->owner->ops != NULL
3351 && bl->owner->ops->remove_location != NULL);
ce78b96d 3352
77b06cd7 3353 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3354 if (val)
3355 return val;
77b06cd7 3356
35df4500 3357 bl->inserted = (is == mark_inserted);
ce78b96d 3358 }
c906108c
SS
3359
3360 return 0;
3361}
3362
6c95b8df 3363static int
35df4500 3364remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3365{
3366 int ret;
3367 struct cleanup *old_chain;
3368
35df4500
TJB
3369 /* BL is never in moribund_locations by our callers. */
3370 gdb_assert (bl->owner != NULL);
2bdf28a0 3371
35df4500 3372 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3373 /* Permanent breakpoints cannot be inserted or removed. */
3374 return 0;
3375
3376 /* The type of none suggests that owner is actually deleted.
3377 This should not ever happen. */
35df4500 3378 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3379
3380 old_chain = save_current_space_and_thread ();
3381
35df4500 3382 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3383
35df4500 3384 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3385
3386 do_cleanups (old_chain);
3387 return ret;
3388}
3389
c906108c
SS
3390/* Clear the "inserted" flag in all breakpoints. */
3391
25b22b0a 3392void
fba45db2 3393mark_breakpoints_out (void)
c906108c 3394{
35df4500 3395 struct bp_location *bl, **blp_tmp;
c906108c 3396
35df4500
TJB
3397 ALL_BP_LOCATIONS (bl, blp_tmp)
3398 if (bl->pspace == current_program_space)
3399 bl->inserted = 0;
c906108c
SS
3400}
3401
53a5351d
JM
3402/* Clear the "inserted" flag in all breakpoints and delete any
3403 breakpoints which should go away between runs of the program.
c906108c
SS
3404
3405 Plus other such housekeeping that has to be done for breakpoints
3406 between runs.
3407
53a5351d
JM
3408 Note: this function gets called at the end of a run (by
3409 generic_mourn_inferior) and when a run begins (by
4a64f543 3410 init_wait_for_inferior). */
c906108c
SS
3411
3412
3413
3414void
fba45db2 3415breakpoint_init_inferior (enum inf_context context)
c906108c 3416{
35df4500
TJB
3417 struct breakpoint *b, *b_tmp;
3418 struct bp_location *bl, **blp_tmp;
1c5cfe86 3419 int ix;
6c95b8df 3420 struct program_space *pspace = current_program_space;
c906108c 3421
50c71eaf
PA
3422 /* If breakpoint locations are shared across processes, then there's
3423 nothing to do. */
2567c7d9 3424 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
3425 return;
3426
35df4500 3427 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3428 {
35df4500
TJB
3429 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3430 if (bl->pspace == pspace
3431 && bl->owner->enable_state != bp_permanent)
3432 bl->inserted = 0;
6c95b8df 3433 }
075f6582 3434
35df4500 3435 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3436 {
6c95b8df
PA
3437 if (b->loc && b->loc->pspace != pspace)
3438 continue;
3439
c5aa993b
JM
3440 switch (b->type)
3441 {
3442 case bp_call_dummy:
c906108c 3443
c5aa993b 3444 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3445 cause problems when the inferior is rerun, so we better get
3446 rid of it. */
3447
3448 case bp_watchpoint_scope:
3449
3450 /* Also get rid of scope breakpoints. */
3451
3452 case bp_shlib_event:
3453
3454 /* Also remove solib event breakpoints. Their addresses may
3455 have changed since the last time we ran the program.
3456 Actually we may now be debugging against different target;
3457 and so the solib backend that installed this breakpoint may
3458 not be used in by the target. E.g.,
3459
3460 (gdb) file prog-linux
3461 (gdb) run # native linux target
3462 ...
3463 (gdb) kill
3464 (gdb) file prog-win.exe
3465 (gdb) tar rem :9999 # remote Windows gdbserver.
3466 */
c906108c 3467
f59f708a
PA
3468 case bp_step_resume:
3469
3470 /* Also remove step-resume breakpoints. */
3471
c5aa993b
JM
3472 delete_breakpoint (b);
3473 break;
c906108c 3474
c5aa993b
JM
3475 case bp_watchpoint:
3476 case bp_hardware_watchpoint:
3477 case bp_read_watchpoint:
3478 case bp_access_watchpoint:
3a5c3e22
PA
3479 {
3480 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3481
3a5c3e22
PA
3482 /* Likewise for watchpoints on local expressions. */
3483 if (w->exp_valid_block != NULL)
3484 delete_breakpoint (b);
3485 else if (context == inf_starting)
3486 {
3487 /* Reset val field to force reread of starting value in
3488 insert_breakpoints. */
3489 if (w->val)
3490 value_free (w->val);
3491 w->val = NULL;
3492 w->val_valid = 0;
c860120c 3493 }
3a5c3e22 3494 }
c5aa993b
JM
3495 break;
3496 default:
c5aa993b
JM
3497 break;
3498 }
3499 }
1c5cfe86
PA
3500
3501 /* Get rid of the moribund locations. */
35df4500
TJB
3502 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3503 decref_bp_location (&bl);
1c5cfe86 3504 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3505}
3506
6c95b8df
PA
3507/* These functions concern about actual breakpoints inserted in the
3508 target --- to e.g. check if we need to do decr_pc adjustment or if
3509 we need to hop over the bkpt --- so we check for address space
3510 match, not program space. */
3511
c2c6d25f
JM
3512/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3513 exists at PC. It returns ordinary_breakpoint_here if it's an
3514 ordinary breakpoint, or permanent_breakpoint_here if it's a
3515 permanent breakpoint.
3516 - When continuing from a location with an ordinary breakpoint, we
3517 actually single step once before calling insert_breakpoints.
e5dd4106 3518 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3519 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3520 the target, to advance the PC past the breakpoint. */
c906108c 3521
c2c6d25f 3522enum breakpoint_here
6c95b8df 3523breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3524{
35df4500 3525 struct bp_location *bl, **blp_tmp;
c2c6d25f 3526 int any_breakpoint_here = 0;
c906108c 3527
35df4500 3528 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3529 {
35df4500
TJB
3530 if (bl->loc_type != bp_loc_software_breakpoint
3531 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3532 continue;
3533
f1310107 3534 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3535 if ((breakpoint_enabled (bl->owner)
3536 || bl->owner->enable_state == bp_permanent)
f1310107 3537 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3538 {
3539 if (overlay_debugging
35df4500
TJB
3540 && section_is_overlay (bl->section)
3541 && !section_is_mapped (bl->section))
075f6582 3542 continue; /* unmapped overlay -- can't be a match */
35df4500 3543 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3544 return permanent_breakpoint_here;
3545 else
3546 any_breakpoint_here = 1;
3547 }
3548 }
c906108c 3549
c2c6d25f 3550 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3551}
3552
1c5cfe86
PA
3553/* Return true if there's a moribund breakpoint at PC. */
3554
3555int
6c95b8df 3556moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3557{
3558 struct bp_location *loc;
3559 int ix;
3560
3561 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3562 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3563 return 1;
3564
3565 return 0;
3566}
c2c6d25f 3567
c36b740a 3568/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3569 inserted using regular breakpoint_chain / bp_location array
3570 mechanism. This does not check for single-step breakpoints, which
3571 are inserted and removed using direct target manipulation. */
c906108c
SS
3572
3573int
4a64f543
MS
3574regular_breakpoint_inserted_here_p (struct address_space *aspace,
3575 CORE_ADDR pc)
c906108c 3576{
35df4500 3577 struct bp_location *bl, **blp_tmp;
c906108c 3578
35df4500 3579 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3580 {
35df4500
TJB
3581 if (bl->loc_type != bp_loc_software_breakpoint
3582 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3583 continue;
3584
35df4500 3585 if (bl->inserted
f1310107 3586 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3587 {
3588 if (overlay_debugging
35df4500
TJB
3589 && section_is_overlay (bl->section)
3590 && !section_is_mapped (bl->section))
075f6582
DJ
3591 continue; /* unmapped overlay -- can't be a match */
3592 else
3593 return 1;
3594 }
c5aa993b 3595 }
c36b740a
VP
3596 return 0;
3597}
3598
3599/* Returns non-zero iff there's either regular breakpoint
3600 or a single step breakpoint inserted at PC. */
3601
3602int
6c95b8df 3603breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3604{
6c95b8df 3605 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3606 return 1;
c906108c 3607
6c95b8df 3608 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3609 return 1;
3610
c906108c
SS
3611 return 0;
3612}
3613
4fa8626c
DJ
3614/* This function returns non-zero iff there is a software breakpoint
3615 inserted at PC. */
3616
3617int
3e43a32a
MS
3618software_breakpoint_inserted_here_p (struct address_space *aspace,
3619 CORE_ADDR pc)
4fa8626c 3620{
35df4500 3621 struct bp_location *bl, **blp_tmp;
4fa8626c 3622
35df4500 3623 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3624 {
35df4500 3625 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3626 continue;
3627
35df4500
TJB
3628 if (bl->inserted
3629 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3630 aspace, pc))
4fa8626c
DJ
3631 {
3632 if (overlay_debugging
35df4500
TJB
3633 && section_is_overlay (bl->section)
3634 && !section_is_mapped (bl->section))
4fa8626c
DJ
3635 continue; /* unmapped overlay -- can't be a match */
3636 else
3637 return 1;
3638 }
3639 }
3640
1aafd4da 3641 /* Also check for software single-step breakpoints. */
6c95b8df 3642 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3643 return 1;
3644
4fa8626c
DJ
3645 return 0;
3646}
3647
9093389c
PA
3648int
3649hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3650 CORE_ADDR addr, ULONGEST len)
3651{
3652 struct breakpoint *bpt;
3653
3654 ALL_BREAKPOINTS (bpt)
3655 {
3656 struct bp_location *loc;
3657
3658 if (bpt->type != bp_hardware_watchpoint
3659 && bpt->type != bp_access_watchpoint)
3660 continue;
3661
3662 if (!breakpoint_enabled (bpt))
3663 continue;
3664
3665 for (loc = bpt->loc; loc; loc = loc->next)
3666 if (loc->pspace->aspace == aspace && loc->inserted)
3667 {
3668 CORE_ADDR l, h;
3669
3670 /* Check for intersection. */
3671 l = max (loc->address, addr);
3672 h = min (loc->address + loc->length, addr + len);
3673 if (l < h)
3674 return 1;
3675 }
3676 }
3677 return 0;
3678}
3679
075f6582
DJ
3680/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3681 PC is valid for process/thread PTID. */
c906108c
SS
3682
3683int
6c95b8df
PA
3684breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3685 ptid_t ptid)
c906108c 3686{
35df4500 3687 struct bp_location *bl, **blp_tmp;
4a306c9a 3688 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3689 int thread = -1;
4a306c9a 3690 int task = 0;
a6f1cd96 3691
35df4500 3692 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3693 {
35df4500
TJB
3694 if (bl->loc_type != bp_loc_software_breakpoint
3695 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3696 continue;
3697
35df4500
TJB
3698 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3699 if (!breakpoint_enabled (bl->owner)
3700 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3701 continue;
3702
f1310107 3703 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3704 continue;
3705
35df4500 3706 if (bl->owner->thread != -1)
075f6582 3707 {
a6f1cd96
JB
3708 /* This is a thread-specific breakpoint. Check that ptid
3709 matches that thread. If thread hasn't been computed yet,
3710 it is now time to do so. */
3711 if (thread == -1)
3712 thread = pid_to_thread_id (ptid);
35df4500 3713 if (bl->owner->thread != thread)
a6f1cd96 3714 continue;
075f6582 3715 }
a6f1cd96 3716
35df4500 3717 if (bl->owner->task != 0)
4a306c9a
JB
3718 {
3719 /* This is a task-specific breakpoint. Check that ptid
3720 matches that task. If task hasn't been computed yet,
3721 it is now time to do so. */
3722 if (task == 0)
3723 task = ada_get_task_number (ptid);
35df4500 3724 if (bl->owner->task != task)
4a306c9a
JB
3725 continue;
3726 }
3727
a6f1cd96 3728 if (overlay_debugging
35df4500
TJB
3729 && section_is_overlay (bl->section)
3730 && !section_is_mapped (bl->section))
a6f1cd96
JB
3731 continue; /* unmapped overlay -- can't be a match */
3732
3733 return 1;
c5aa993b 3734 }
c906108c
SS
3735
3736 return 0;
3737}
c906108c 3738\f
c5aa993b 3739
c906108c
SS
3740/* bpstat stuff. External routines' interfaces are documented
3741 in breakpoint.h. */
3742
3743int
c326b90e 3744is_catchpoint (struct breakpoint *ep)
c906108c 3745{
533be4dd 3746 return (ep->type == bp_catchpoint);
c906108c
SS
3747}
3748
f431efe5
PA
3749/* Frees any storage that is part of a bpstat. Does not walk the
3750 'next' chain. */
3751
3752static void
198757a8
VP
3753bpstat_free (bpstat bs)
3754{
3755 if (bs->old_val != NULL)
3756 value_free (bs->old_val);
9add0f1b 3757 decref_counted_command_line (&bs->commands);
f431efe5 3758 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3759 xfree (bs);
3760}
3761
c906108c
SS
3762/* Clear a bpstat so that it says we are not at any breakpoint.
3763 Also free any storage that is part of a bpstat. */
3764
3765void
fba45db2 3766bpstat_clear (bpstat *bsp)
c906108c
SS
3767{
3768 bpstat p;
3769 bpstat q;
3770
3771 if (bsp == 0)
3772 return;
3773 p = *bsp;
3774 while (p != NULL)
3775 {
3776 q = p->next;
198757a8 3777 bpstat_free (p);
c906108c
SS
3778 p = q;
3779 }
3780 *bsp = NULL;
3781}
3782
3783/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3784 is part of the bpstat is copied as well. */
3785
3786bpstat
fba45db2 3787bpstat_copy (bpstat bs)
c906108c
SS
3788{
3789 bpstat p = NULL;
3790 bpstat tmp;
3791 bpstat retval = NULL;
3792
3793 if (bs == NULL)
3794 return bs;
3795
3796 for (; bs != NULL; bs = bs->next)
3797 {
3798 tmp = (bpstat) xmalloc (sizeof (*tmp));
3799 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3800 incref_counted_command_line (tmp->commands);
f431efe5 3801 incref_bp_location (tmp->bp_location_at);
31cc81e9 3802 if (bs->old_val != NULL)
3c3185ac
JK
3803 {
3804 tmp->old_val = value_copy (bs->old_val);
3805 release_value (tmp->old_val);
3806 }
31cc81e9 3807
c906108c
SS
3808 if (p == NULL)
3809 /* This is the first thing in the chain. */
3810 retval = tmp;
3811 else
3812 p->next = tmp;
3813 p = tmp;
3814 }
3815 p->next = NULL;
3816 return retval;
3817}
3818
4a64f543 3819/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3820
3821bpstat
fba45db2 3822bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3823{
c5aa993b
JM
3824 if (bsp == NULL)
3825 return NULL;
c906108c 3826
c5aa993b
JM
3827 for (; bsp != NULL; bsp = bsp->next)
3828 {
f431efe5 3829 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3830 return bsp;
3831 }
c906108c
SS
3832 return NULL;
3833}
3834
4a64f543
MS
3835/* Put in *NUM the breakpoint number of the first breakpoint we are
3836 stopped at. *BSP upon return is a bpstat which points to the
3837 remaining breakpoints stopped at (but which is not guaranteed to be
3838 good for anything but further calls to bpstat_num).
3839
8671a17b
PA
3840 Return 0 if passed a bpstat which does not indicate any breakpoints.
3841 Return -1 if stopped at a breakpoint that has been deleted since
3842 we set it.
3843 Return 1 otherwise. */
c906108c
SS
3844
3845int
8671a17b 3846bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3847{
3848 struct breakpoint *b;
3849
3850 if ((*bsp) == NULL)
3851 return 0; /* No more breakpoint values */
8671a17b 3852
4a64f543
MS
3853 /* We assume we'll never have several bpstats that correspond to a
3854 single breakpoint -- otherwise, this function might return the
3855 same number more than once and this will look ugly. */
f431efe5 3856 b = (*bsp)->breakpoint_at;
8671a17b
PA
3857 *bsp = (*bsp)->next;
3858 if (b == NULL)
3859 return -1; /* breakpoint that's been deleted since */
3860
3861 *num = b->number; /* We have its number */
3862 return 1;
c906108c
SS
3863}
3864
e93ca019 3865/* See breakpoint.h. */
c906108c
SS
3866
3867void
e93ca019 3868bpstat_clear_actions (void)
c906108c 3869{
e93ca019
JK
3870 struct thread_info *tp;
3871 bpstat bs;
3872
3873 if (ptid_equal (inferior_ptid, null_ptid))
3874 return;
3875
3876 tp = find_thread_ptid (inferior_ptid);
3877 if (tp == NULL)
3878 return;
3879
3880 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3881 {
9add0f1b 3882 decref_counted_command_line (&bs->commands);
abf85f46 3883
c906108c
SS
3884 if (bs->old_val != NULL)
3885 {
3886 value_free (bs->old_val);
3887 bs->old_val = NULL;
3888 }
3889 }
3890}
3891
f3b1572e
PA
3892/* Called when a command is about to proceed the inferior. */
3893
3894static void
3895breakpoint_about_to_proceed (void)
3896{
3897 if (!ptid_equal (inferior_ptid, null_ptid))
3898 {
3899 struct thread_info *tp = inferior_thread ();
3900
3901 /* Allow inferior function calls in breakpoint commands to not
3902 interrupt the command list. When the call finishes
3903 successfully, the inferior will be standing at the same
3904 breakpoint as if nothing happened. */
16c381f0 3905 if (tp->control.in_infcall)
f3b1572e
PA
3906 return;
3907 }
3908
3909 breakpoint_proceeded = 1;
3910}
3911
4a64f543
MS
3912/* Stub for cleaning up our state if we error-out of a breakpoint
3913 command. */
c906108c 3914static void
4efb68b1 3915cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3916{
3917 executing_breakpoint_commands = 0;
3918}
3919
abf85f46
JK
3920/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3921 or its equivalent. */
3922
3923static int
3924command_line_is_silent (struct command_line *cmd)
3925{
3926 return cmd && (strcmp ("silent", cmd->line) == 0
3927 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3928}
3929
4a64f543
MS
3930/* Execute all the commands associated with all the breakpoints at
3931 this location. Any of these commands could cause the process to
3932 proceed beyond this point, etc. We look out for such changes by
3933 checking the global "breakpoint_proceeded" after each command.
c906108c 3934
347bddb7
PA
3935 Returns true if a breakpoint command resumed the inferior. In that
3936 case, it is the caller's responsibility to recall it again with the
3937 bpstat of the current thread. */
3938
3939static int
3940bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
3941{
3942 bpstat bs;
3943 struct cleanup *old_chain;
347bddb7 3944 int again = 0;
c906108c
SS
3945
3946 /* Avoid endless recursion if a `source' command is contained
3947 in bs->commands. */
3948 if (executing_breakpoint_commands)
347bddb7 3949 return 0;
c906108c
SS
3950
3951 executing_breakpoint_commands = 1;
3952 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
3953
cf6c5ffb
TT
3954 prevent_dont_repeat ();
3955
4a64f543 3956 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
3957 bs = *bsp;
3958
3959 breakpoint_proceeded = 0;
3960 for (; bs != NULL; bs = bs->next)
3961 {
9add0f1b 3962 struct counted_command_line *ccmd;
6c50ab1c
JB
3963 struct command_line *cmd;
3964 struct cleanup *this_cmd_tree_chain;
3965
3966 /* Take ownership of the BSP's command tree, if it has one.
3967
3968 The command tree could legitimately contain commands like
3969 'step' and 'next', which call clear_proceed_status, which
3970 frees stop_bpstat's command tree. To make sure this doesn't
3971 free the tree we're executing out from under us, we need to
3972 take ownership of the tree ourselves. Since a given bpstat's
3973 commands are only executed once, we don't need to copy it; we
3974 can clear the pointer in the bpstat, and make sure we free
3975 the tree when we're done. */
9add0f1b
TT
3976 ccmd = bs->commands;
3977 bs->commands = NULL;
abf85f46
JK
3978 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
3979 cmd = ccmd ? ccmd->commands : NULL;
3980 if (command_line_is_silent (cmd))
3981 {
3982 /* The action has been already done by bpstat_stop_status. */
3983 cmd = cmd->next;
3984 }
6c50ab1c 3985
c906108c
SS
3986 while (cmd != NULL)
3987 {
3988 execute_control_command (cmd);
3989
3990 if (breakpoint_proceeded)
3991 break;
3992 else
3993 cmd = cmd->next;
3994 }
6c50ab1c
JB
3995
3996 /* We can free this command tree now. */
3997 do_cleanups (this_cmd_tree_chain);
3998
c906108c 3999 if (breakpoint_proceeded)
32c1e744
VP
4000 {
4001 if (target_can_async_p ())
347bddb7
PA
4002 /* If we are in async mode, then the target might be still
4003 running, not stopped at any breakpoint, so nothing for
4004 us to do here -- just return to the event loop. */
4005 ;
32c1e744
VP
4006 else
4007 /* In sync mode, when execute_control_command returns
4008 we're already standing on the next breakpoint.
347bddb7
PA
4009 Breakpoint commands for that stop were not run, since
4010 execute_command does not run breakpoint commands --
4011 only command_line_handler does, but that one is not
4012 involved in execution of breakpoint commands. So, we
4013 can now execute breakpoint commands. It should be
4014 noted that making execute_command do bpstat actions is
4015 not an option -- in this case we'll have recursive
4016 invocation of bpstat for each breakpoint with a
4017 command, and can easily blow up GDB stack. Instead, we
4018 return true, which will trigger the caller to recall us
4019 with the new stop_bpstat. */
4020 again = 1;
4021 break;
32c1e744 4022 }
c906108c 4023 }
c2b8ed2c 4024 do_cleanups (old_chain);
347bddb7
PA
4025 return again;
4026}
4027
4028void
4029bpstat_do_actions (void)
4030{
353d1d73
JK
4031 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4032
347bddb7
PA
4033 /* Do any commands attached to breakpoint we are stopped at. */
4034 while (!ptid_equal (inferior_ptid, null_ptid)
4035 && target_has_execution
4036 && !is_exited (inferior_ptid)
4037 && !is_executing (inferior_ptid))
4038 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4039 and only return when it is stopped at the next breakpoint, we
4040 keep doing breakpoint actions until it returns false to
4041 indicate the inferior was not resumed. */
16c381f0 4042 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4043 break;
353d1d73
JK
4044
4045 discard_cleanups (cleanup_if_error);
c906108c
SS
4046}
4047
fa4727a6
DJ
4048/* Print out the (old or new) value associated with a watchpoint. */
4049
4050static void
4051watchpoint_value_print (struct value *val, struct ui_file *stream)
4052{
4053 if (val == NULL)
4054 fprintf_unfiltered (stream, _("<unreadable>"));
4055 else
79a45b7d
TT
4056 {
4057 struct value_print_options opts;
4058 get_user_print_options (&opts);
4059 value_print (val, stream, &opts);
4060 }
fa4727a6
DJ
4061}
4062
e514a9d6 4063/* Generic routine for printing messages indicating why we
4a64f543 4064 stopped. The behavior of this function depends on the value
e514a9d6
JM
4065 'print_it' in the bpstat structure. Under some circumstances we
4066 may decide not to print anything here and delegate the task to
4a64f543 4067 normal_stop(). */
e514a9d6
JM
4068
4069static enum print_stop_action
4070print_bp_stop_message (bpstat bs)
4071{
4072 switch (bs->print_it)
4073 {
4074 case print_it_noop:
4a64f543 4075 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4076 return PRINT_UNKNOWN;
4077 break;
4078
4079 case print_it_done:
4080 /* We still want to print the frame, but we already printed the
4a64f543 4081 relevant messages. */
e514a9d6
JM
4082 return PRINT_SRC_AND_LOC;
4083 break;
4084
4085 case print_it_normal:
4f8d1dc6 4086 {
f431efe5
PA
4087 struct breakpoint *b = bs->breakpoint_at;
4088
1a6a67de
TJB
4089 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4090 which has since been deleted. */
4091 if (b == NULL)
4092 return PRINT_UNKNOWN;
4093
348d480f
PA
4094 /* Normal case. Call the breakpoint's print_it method. */
4095 return b->ops->print_it (bs);
4f8d1dc6 4096 }
348d480f 4097 break;
3086aeae 4098
e514a9d6 4099 default:
8e65ff28 4100 internal_error (__FILE__, __LINE__,
e2e0b3e5 4101 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4102 break;
c906108c 4103 }
c906108c
SS
4104}
4105
edcc5120
TT
4106/* A helper function that prints a shared library stopped event. */
4107
4108static void
4109print_solib_event (int is_catchpoint)
4110{
4111 int any_deleted
4112 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4113 int any_added
4114 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4115
4116 if (!is_catchpoint)
4117 {
4118 if (any_added || any_deleted)
4119 ui_out_text (current_uiout,
4120 _("Stopped due to shared library event:\n"));
4121 else
4122 ui_out_text (current_uiout,
4123 _("Stopped due to shared library event (no "
4124 "libraries added or removed)\n"));
4125 }
4126
4127 if (ui_out_is_mi_like_p (current_uiout))
4128 ui_out_field_string (current_uiout, "reason",
4129 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4130
4131 if (any_deleted)
4132 {
4133 struct cleanup *cleanup;
4134 char *name;
4135 int ix;
4136
4137 ui_out_text (current_uiout, _(" Inferior unloaded "));
4138 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4139 "removed");
4140 for (ix = 0;
4141 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4142 ix, name);
4143 ++ix)
4144 {
4145 if (ix > 0)
4146 ui_out_text (current_uiout, " ");
4147 ui_out_field_string (current_uiout, "library", name);
4148 ui_out_text (current_uiout, "\n");
4149 }
4150
4151 do_cleanups (cleanup);
4152 }
4153
4154 if (any_added)
4155 {
4156 struct so_list *iter;
4157 int ix;
4158 struct cleanup *cleanup;
4159
4160 ui_out_text (current_uiout, _(" Inferior loaded "));
4161 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4162 "added");
4163 for (ix = 0;
4164 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4165 ix, iter);
4166 ++ix)
4167 {
4168 if (ix > 0)
4169 ui_out_text (current_uiout, " ");
4170 ui_out_field_string (current_uiout, "library", iter->so_name);
4171 ui_out_text (current_uiout, "\n");
4172 }
4173
4174 do_cleanups (cleanup);
4175 }
4176}
4177
e514a9d6
JM
4178/* Print a message indicating what happened. This is called from
4179 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4180 list - a list of the eventpoints that caused this stop. KIND is
4181 the target_waitkind for the stopping event. This
e514a9d6
JM
4182 routine calls the generic print routine for printing a message
4183 about reasons for stopping. This will print (for example) the
4184 "Breakpoint n," part of the output. The return value of this
4185 routine is one of:
c906108c 4186
4a64f543 4187 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4188 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4189 code to print the location. An example is
c5aa993b
JM
4190 "Breakpoint 1, " which should be followed by
4191 the location.
917317f4 4192 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4193 to also print the location part of the message.
4194 An example is the catch/throw messages, which
4a64f543 4195 don't require a location appended to the end.
917317f4 4196 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4197 further info to be printed. */
c906108c 4198
917317f4 4199enum print_stop_action
36dfb11c 4200bpstat_print (bpstat bs, int kind)
c906108c
SS
4201{
4202 int val;
c5aa993b 4203
c906108c 4204 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4205 (Currently all watchpoints go on the bpstat whether hit or not.
4206 That probably could (should) be changed, provided care is taken
c906108c 4207 with respect to bpstat_explains_signal). */
e514a9d6
JM
4208 for (; bs; bs = bs->next)
4209 {
4210 val = print_bp_stop_message (bs);
4211 if (val == PRINT_SRC_ONLY
4212 || val == PRINT_SRC_AND_LOC
4213 || val == PRINT_NOTHING)
4214 return val;
4215 }
c906108c 4216
36dfb11c
TT
4217 /* If we had hit a shared library event breakpoint,
4218 print_bp_stop_message would print out this message. If we hit an
4219 OS-level shared library event, do the same thing. */
4220 if (kind == TARGET_WAITKIND_LOADED)
4221 {
edcc5120 4222 print_solib_event (0);
36dfb11c
TT
4223 return PRINT_NOTHING;
4224 }
4225
e514a9d6 4226 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4227 with and nothing was printed. */
917317f4 4228 return PRINT_UNKNOWN;
c906108c
SS
4229}
4230
4a64f543
MS
4231/* Evaluate the expression EXP and return 1 if value is zero. This is
4232 used inside a catch_errors to evaluate the breakpoint condition.
4233 The argument is a "struct expression *" that has been cast to a
4234 "char *" to make it pass through catch_errors. */
c906108c
SS
4235
4236static int
4efb68b1 4237breakpoint_cond_eval (void *exp)
c906108c 4238{
278cd55f 4239 struct value *mark = value_mark ();
c5aa993b 4240 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4241
c906108c
SS
4242 value_free_to_mark (mark);
4243 return i;
4244}
4245
5760d0ab 4246/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4247
4248static bpstat
5760d0ab 4249bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4250{
4251 bpstat bs;
4252
4253 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4254 bs->next = NULL;
4255 **bs_link_pointer = bs;
4256 *bs_link_pointer = &bs->next;
f431efe5
PA
4257 bs->breakpoint_at = bl->owner;
4258 bs->bp_location_at = bl;
4259 incref_bp_location (bl);
c906108c
SS
4260 /* If the condition is false, etc., don't do the commands. */
4261 bs->commands = NULL;
4262 bs->old_val = NULL;
4263 bs->print_it = print_it_normal;
4264 return bs;
4265}
4266\f
d983da9c
DJ
4267/* The target has stopped with waitstatus WS. Check if any hardware
4268 watchpoints have triggered, according to the target. */
4269
4270int
4271watchpoints_triggered (struct target_waitstatus *ws)
4272{
d92524f1 4273 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4274 CORE_ADDR addr;
4275 struct breakpoint *b;
4276
4277 if (!stopped_by_watchpoint)
4278 {
4279 /* We were not stopped by a watchpoint. Mark all watchpoints
4280 as not triggered. */
4281 ALL_BREAKPOINTS (b)
cc60f2e3 4282 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4283 {
4284 struct watchpoint *w = (struct watchpoint *) b;
4285
4286 w->watchpoint_triggered = watch_triggered_no;
4287 }
d983da9c
DJ
4288
4289 return 0;
4290 }
4291
4292 if (!target_stopped_data_address (&current_target, &addr))
4293 {
4294 /* We were stopped by a watchpoint, but we don't know where.
4295 Mark all watchpoints as unknown. */
4296 ALL_BREAKPOINTS (b)
cc60f2e3 4297 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4298 {
4299 struct watchpoint *w = (struct watchpoint *) b;
4300
4301 w->watchpoint_triggered = watch_triggered_unknown;
4302 }
d983da9c
DJ
4303
4304 return stopped_by_watchpoint;
4305 }
4306
4307 /* The target could report the data address. Mark watchpoints
4308 affected by this data address as triggered, and all others as not
4309 triggered. */
4310
4311 ALL_BREAKPOINTS (b)
cc60f2e3 4312 if (is_hardware_watchpoint (b))
d983da9c 4313 {
3a5c3e22 4314 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4315 struct bp_location *loc;
d983da9c 4316
3a5c3e22 4317 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4318 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4319 {
3a5c3e22 4320 if (is_masked_watchpoint (b))
9c06b0b4 4321 {
3a5c3e22
PA
4322 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4323 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4324
4325 if (newaddr == start)
4326 {
3a5c3e22 4327 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4328 break;
4329 }
4330 }
4331 /* Exact match not required. Within range is sufficient. */
4332 else if (target_watchpoint_addr_within_range (&current_target,
4333 addr, loc->address,
4334 loc->length))
4335 {
3a5c3e22 4336 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4337 break;
4338 }
4339 }
d983da9c
DJ
4340 }
4341
4342 return 1;
4343}
4344
c906108c
SS
4345/* Possible return values for watchpoint_check (this can't be an enum
4346 because of check_errors). */
4347/* The watchpoint has been deleted. */
4348#define WP_DELETED 1
4349/* The value has changed. */
4350#define WP_VALUE_CHANGED 2
4351/* The value has not changed. */
4352#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4353/* Ignore this watchpoint, no matter if the value changed or not. */
4354#define WP_IGNORE 4
c906108c
SS
4355
4356#define BP_TEMPFLAG 1
4357#define BP_HARDWAREFLAG 2
4358
4a64f543
MS
4359/* Evaluate watchpoint condition expression and check if its value
4360 changed.
553e4c11
JB
4361
4362 P should be a pointer to struct bpstat, but is defined as a void *
4363 in order for this function to be usable with catch_errors. */
c906108c
SS
4364
4365static int
4efb68b1 4366watchpoint_check (void *p)
c906108c
SS
4367{
4368 bpstat bs = (bpstat) p;
3a5c3e22 4369 struct watchpoint *b;
c906108c
SS
4370 struct frame_info *fr;
4371 int within_current_scope;
4372
f431efe5 4373 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4374 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4375 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4376
f6bc2008
PA
4377 /* If this is a local watchpoint, we only want to check if the
4378 watchpoint frame is in scope if the current thread is the thread
4379 that was used to create the watchpoint. */
4380 if (!watchpoint_in_thread_scope (b))
60e1c644 4381 return WP_IGNORE;
f6bc2008 4382
c906108c
SS
4383 if (b->exp_valid_block == NULL)
4384 within_current_scope = 1;
4385 else
4386 {
edb3359d
DJ
4387 struct frame_info *frame = get_current_frame ();
4388 struct gdbarch *frame_arch = get_frame_arch (frame);
4389 CORE_ADDR frame_pc = get_frame_pc (frame);
4390
4a64f543
MS
4391 /* in_function_epilogue_p() returns a non-zero value if we're
4392 still in the function but the stack frame has already been
4393 invalidated. Since we can't rely on the values of local
4394 variables after the stack has been destroyed, we are treating
4395 the watchpoint in that state as `not changed' without further
4396 checking. Don't mark watchpoints as changed if the current
4397 frame is in an epilogue - even if they are in some other
4398 frame, our view of the stack is likely to be wrong and
4399 frame_find_by_id could error out. */
a0f49112 4400 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4401 return WP_IGNORE;
a0f49112 4402
101dcfbe 4403 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4404 within_current_scope = (fr != NULL);
69fbadd5
DJ
4405
4406 /* If we've gotten confused in the unwinder, we might have
4407 returned a frame that can't describe this variable. */
edb3359d
DJ
4408 if (within_current_scope)
4409 {
4410 struct symbol *function;
4411
4412 function = get_frame_function (fr);
4413 if (function == NULL
4414 || !contained_in (b->exp_valid_block,
4415 SYMBOL_BLOCK_VALUE (function)))
4416 within_current_scope = 0;
4417 }
69fbadd5 4418
edb3359d 4419 if (within_current_scope)
c906108c
SS
4420 /* If we end up stopping, the current frame will get selected
4421 in normal_stop. So this call to select_frame won't affect
4422 the user. */
0f7d239c 4423 select_frame (fr);
c906108c 4424 }
c5aa993b 4425
c906108c
SS
4426 if (within_current_scope)
4427 {
4a64f543
MS
4428 /* We use value_{,free_to_}mark because it could be a *long*
4429 time before we return to the command level and call
4430 free_all_values. We can't call free_all_values because we
4431 might be in the middle of evaluating a function call. */
c906108c 4432
0cf6dd15 4433 int pc = 0;
9c06b0b4 4434 struct value *mark;
fa4727a6
DJ
4435 struct value *new_val;
4436
3a5c3e22 4437 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4438 /* Since we don't know the exact trigger address (from
4439 stopped_data_address), just tell the user we've triggered
4440 a mask watchpoint. */
4441 return WP_VALUE_CHANGED;
4442
4443 mark = value_mark ();
0cf6dd15 4444 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 4445
4a64f543
MS
4446 /* We use value_equal_contents instead of value_equal because
4447 the latter coerces an array to a pointer, thus comparing just
4448 the address of the array instead of its contents. This is
4449 not what we want. */
fa4727a6 4450 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4451 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4452 {
fa4727a6
DJ
4453 if (new_val != NULL)
4454 {
4455 release_value (new_val);
4456 value_free_to_mark (mark);
4457 }
c906108c
SS
4458 bs->old_val = b->val;
4459 b->val = new_val;
fa4727a6 4460 b->val_valid = 1;
c906108c
SS
4461 return WP_VALUE_CHANGED;
4462 }
4463 else
4464 {
60e1c644 4465 /* Nothing changed. */
c906108c 4466 value_free_to_mark (mark);
c906108c
SS
4467 return WP_VALUE_NOT_CHANGED;
4468 }
4469 }
4470 else
4471 {
79a45e25
PA
4472 struct ui_out *uiout = current_uiout;
4473
c906108c 4474 /* This seems like the only logical thing to do because
c5aa993b
JM
4475 if we temporarily ignored the watchpoint, then when
4476 we reenter the block in which it is valid it contains
4477 garbage (in the case of a function, it may have two
4478 garbage values, one before and one after the prologue).
4479 So we can't even detect the first assignment to it and
4480 watch after that (since the garbage may or may not equal
4481 the first value assigned). */
348d480f
PA
4482 /* We print all the stop information in
4483 breakpoint_ops->print_it, but in this case, by the time we
4484 call breakpoint_ops->print_it this bp will be deleted
4485 already. So we have no choice but print the information
4486 here. */
9dc5e2a9 4487 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4488 ui_out_field_string
4489 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4490 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4491 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4492 ui_out_text (uiout,
4493 " deleted because the program has left the block in\n\
8b93c638 4494which its expression is valid.\n");
4ce44c66 4495
cdac0397 4496 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4497 decref_counted_command_line (&b->base.commands);
d0fb5eae 4498 watchpoint_del_at_next_stop (b);
c906108c
SS
4499
4500 return WP_DELETED;
4501 }
4502}
4503
18a18393 4504/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4505 breakpoint location BL. This function does not check if we should
4506 stop, only if BL explains the stop. */
4507
18a18393 4508static int
6c95b8df 4509bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4510 struct address_space *aspace, CORE_ADDR bp_addr,
4511 const struct target_waitstatus *ws)
18a18393
VP
4512{
4513 struct breakpoint *b = bl->owner;
4514
348d480f 4515 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4516 gdb_assert (b != NULL);
4517
09ac7c10 4518 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4519}
4520
3a5c3e22
PA
4521/* Determine if the watched values have actually changed, and we
4522 should stop. If not, set BS->stop to 0. */
4523
18a18393
VP
4524static void
4525bpstat_check_watchpoint (bpstat bs)
4526{
2bdf28a0 4527 const struct bp_location *bl;
3a5c3e22 4528 struct watchpoint *b;
2bdf28a0
JK
4529
4530 /* BS is built for existing struct breakpoint. */
f431efe5 4531 bl = bs->bp_location_at;
2bdf28a0 4532 gdb_assert (bl != NULL);
3a5c3e22 4533 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4534 gdb_assert (b != NULL);
18a18393 4535
18a18393 4536 {
18a18393
VP
4537 int must_check_value = 0;
4538
3a5c3e22 4539 if (b->base.type == bp_watchpoint)
18a18393
VP
4540 /* For a software watchpoint, we must always check the
4541 watched value. */
4542 must_check_value = 1;
4543 else if (b->watchpoint_triggered == watch_triggered_yes)
4544 /* We have a hardware watchpoint (read, write, or access)
4545 and the target earlier reported an address watched by
4546 this watchpoint. */
4547 must_check_value = 1;
4548 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4549 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4550 /* We were stopped by a hardware watchpoint, but the target could
4551 not report the data address. We must check the watchpoint's
4552 value. Access and read watchpoints are out of luck; without
4553 a data address, we can't figure it out. */
4554 must_check_value = 1;
3a5c3e22 4555
18a18393
VP
4556 if (must_check_value)
4557 {
3e43a32a
MS
4558 char *message
4559 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4560 b->base.number);
18a18393
VP
4561 struct cleanup *cleanups = make_cleanup (xfree, message);
4562 int e = catch_errors (watchpoint_check, bs, message,
4563 RETURN_MASK_ALL);
4564 do_cleanups (cleanups);
4565 switch (e)
4566 {
4567 case WP_DELETED:
4568 /* We've already printed what needs to be printed. */
4569 bs->print_it = print_it_done;
4570 /* Stop. */
4571 break;
60e1c644
PA
4572 case WP_IGNORE:
4573 bs->print_it = print_it_noop;
4574 bs->stop = 0;
4575 break;
18a18393 4576 case WP_VALUE_CHANGED:
3a5c3e22 4577 if (b->base.type == bp_read_watchpoint)
18a18393 4578 {
85d721b8
PA
4579 /* There are two cases to consider here:
4580
4a64f543 4581 1. We're watching the triggered memory for reads.
85d721b8
PA
4582 In that case, trust the target, and always report
4583 the watchpoint hit to the user. Even though
4584 reads don't cause value changes, the value may
4585 have changed since the last time it was read, and
4586 since we're not trapping writes, we will not see
4587 those, and as such we should ignore our notion of
4588 old value.
4589
4a64f543 4590 2. We're watching the triggered memory for both
85d721b8
PA
4591 reads and writes. There are two ways this may
4592 happen:
4593
4a64f543 4594 2.1. This is a target that can't break on data
85d721b8
PA
4595 reads only, but can break on accesses (reads or
4596 writes), such as e.g., x86. We detect this case
4597 at the time we try to insert read watchpoints.
4598
4a64f543 4599 2.2. Otherwise, the target supports read
85d721b8
PA
4600 watchpoints, but, the user set an access or write
4601 watchpoint watching the same memory as this read
4602 watchpoint.
4603
4604 If we're watching memory writes as well as reads,
4605 ignore watchpoint hits when we find that the
4606 value hasn't changed, as reads don't cause
4607 changes. This still gives false positives when
4608 the program writes the same value to memory as
4609 what there was already in memory (we will confuse
4610 it for a read), but it's much better than
4611 nothing. */
4612
4613 int other_write_watchpoint = 0;
4614
4615 if (bl->watchpoint_type == hw_read)
4616 {
4617 struct breakpoint *other_b;
4618
4619 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4620 if (other_b->type == bp_hardware_watchpoint
4621 || other_b->type == bp_access_watchpoint)
85d721b8 4622 {
3a5c3e22
PA
4623 struct watchpoint *other_w =
4624 (struct watchpoint *) other_b;
4625
4626 if (other_w->watchpoint_triggered
4627 == watch_triggered_yes)
4628 {
4629 other_write_watchpoint = 1;
4630 break;
4631 }
85d721b8
PA
4632 }
4633 }
4634
4635 if (other_write_watchpoint
4636 || bl->watchpoint_type == hw_access)
4637 {
4638 /* We're watching the same memory for writes,
4639 and the value changed since the last time we
4640 updated it, so this trap must be for a write.
4641 Ignore it. */
4642 bs->print_it = print_it_noop;
4643 bs->stop = 0;
4644 }
18a18393
VP
4645 }
4646 break;
4647 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4648 if (b->base.type == bp_hardware_watchpoint
4649 || b->base.type == bp_watchpoint)
18a18393
VP
4650 {
4651 /* Don't stop: write watchpoints shouldn't fire if
4652 the value hasn't changed. */
4653 bs->print_it = print_it_noop;
4654 bs->stop = 0;
4655 }
4656 /* Stop. */
4657 break;
4658 default:
4659 /* Can't happen. */
4660 case 0:
4661 /* Error from catch_errors. */
3a5c3e22 4662 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4663 watchpoint_del_at_next_stop (b);
18a18393
VP
4664 /* We've already printed what needs to be printed. */
4665 bs->print_it = print_it_done;
4666 break;
4667 }
4668 }
4669 else /* must_check_value == 0 */
4670 {
4671 /* This is a case where some watchpoint(s) triggered, but
4672 not at the address of this watchpoint, or else no
4673 watchpoint triggered after all. So don't print
4674 anything for this watchpoint. */
4675 bs->print_it = print_it_noop;
4676 bs->stop = 0;
4677 }
4678 }
4679}
4680
4681
4682/* Check conditions (condition proper, frame, thread and ignore count)
4683 of breakpoint referred to by BS. If we should not stop for this
4684 breakpoint, set BS->stop to 0. */
f431efe5 4685
18a18393
VP
4686static void
4687bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4688{
4689 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4690 const struct bp_location *bl;
4691 struct breakpoint *b;
4692
4693 /* BS is built for existing struct breakpoint. */
f431efe5 4694 bl = bs->bp_location_at;
2bdf28a0 4695 gdb_assert (bl != NULL);
f431efe5 4696 b = bs->breakpoint_at;
2bdf28a0 4697 gdb_assert (b != NULL);
18a18393 4698
b775012e
LM
4699 /* Even if the target evaluated the condition on its end and notified GDB, we
4700 need to do so again since GDB does not know if we stopped due to a
4701 breakpoint or a single step breakpoint. */
4702
18a18393 4703 if (frame_id_p (b->frame_id)
edb3359d 4704 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4705 bs->stop = 0;
4706 else if (bs->stop)
4707 {
4708 int value_is_zero = 0;
60e1c644
PA
4709 struct expression *cond;
4710
7371cf6d
PM
4711 /* Evaluate Python breakpoints that have a "stop"
4712 method implemented. */
4713 if (b->py_bp_object)
4714 bs->stop = gdbpy_should_stop (b->py_bp_object);
4715
60e1c644 4716 if (is_watchpoint (b))
3a5c3e22
PA
4717 {
4718 struct watchpoint *w = (struct watchpoint *) b;
4719
4720 cond = w->cond_exp;
4721 }
60e1c644
PA
4722 else
4723 cond = bl->cond;
4724
f431efe5 4725 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4726 {
60e1c644 4727 int within_current_scope = 1;
3a5c3e22 4728 struct watchpoint * w;
60e1c644 4729
c5bc3a77
DJ
4730 /* We use value_mark and value_free_to_mark because it could
4731 be a long time before we return to the command level and
4732 call free_all_values. We can't call free_all_values
4733 because we might be in the middle of evaluating a
4734 function call. */
4735 struct value *mark = value_mark ();
4736
3a5c3e22
PA
4737 if (is_watchpoint (b))
4738 w = (struct watchpoint *) b;
4739 else
4740 w = NULL;
4741
edb3359d
DJ
4742 /* Need to select the frame, with all that implies so that
4743 the conditions will have the right context. Because we
4744 use the frame, we will not see an inlined function's
4745 variables when we arrive at a breakpoint at the start
4746 of the inlined function; the current frame will be the
4747 call site. */
3a5c3e22 4748 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4749 select_frame (get_current_frame ());
4750 else
4751 {
4752 struct frame_info *frame;
4753
4754 /* For local watchpoint expressions, which particular
4755 instance of a local is being watched matters, so we
4756 keep track of the frame to evaluate the expression
4757 in. To evaluate the condition however, it doesn't
4758 really matter which instantiation of the function
4759 where the condition makes sense triggers the
4760 watchpoint. This allows an expression like "watch
4761 global if q > 10" set in `func', catch writes to
4762 global on all threads that call `func', or catch
4763 writes on all recursive calls of `func' by a single
4764 thread. We simply always evaluate the condition in
4765 the innermost frame that's executing where it makes
4766 sense to evaluate the condition. It seems
4767 intuitive. */
3a5c3e22 4768 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4769 if (frame != NULL)
4770 select_frame (frame);
4771 else
4772 within_current_scope = 0;
4773 }
4774 if (within_current_scope)
4775 value_is_zero
4776 = catch_errors (breakpoint_cond_eval, cond,
4777 "Error in testing breakpoint condition:\n",
4778 RETURN_MASK_ALL);
4779 else
4780 {
4781 warning (_("Watchpoint condition cannot be tested "
4782 "in the current scope"));
4783 /* If we failed to set the right context for this
4784 watchpoint, unconditionally report it. */
4785 value_is_zero = 0;
4786 }
4a64f543 4787 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4788 value_free_to_mark (mark);
18a18393 4789 }
60e1c644
PA
4790
4791 if (cond && value_is_zero)
18a18393
VP
4792 {
4793 bs->stop = 0;
4794 }
4795 else if (b->thread != -1 && b->thread != thread_id)
4796 {
4797 bs->stop = 0;
4798 }
4799 else if (b->ignore_count > 0)
4800 {
4801 b->ignore_count--;
4802 annotate_ignore_count_change ();
4803 bs->stop = 0;
4a64f543 4804 /* Increase the hit count even though we don't stop. */
18a18393 4805 ++(b->hit_count);
8d3788bd 4806 observer_notify_breakpoint_modified (b);
18a18393
VP
4807 }
4808 }
4809}
4810
4811
9709f61c 4812/* Get a bpstat associated with having just stopped at address
d983da9c 4813 BP_ADDR in thread PTID.
c906108c 4814
d983da9c 4815 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4816 don't understand this stop. Result is a chain of bpstat's such
4817 that:
c906108c 4818
c5aa993b 4819 if we don't understand the stop, the result is a null pointer.
c906108c 4820
c5aa993b 4821 if we understand why we stopped, the result is not null.
c906108c 4822
c5aa993b
JM
4823 Each element of the chain refers to a particular breakpoint or
4824 watchpoint at which we have stopped. (We may have stopped for
4825 several reasons concurrently.)
c906108c 4826
c5aa993b
JM
4827 Each element of the chain has valid next, breakpoint_at,
4828 commands, FIXME??? fields. */
c906108c
SS
4829
4830bpstat
6c95b8df 4831bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4832 CORE_ADDR bp_addr, ptid_t ptid,
4833 const struct target_waitstatus *ws)
c906108c 4834{
0d381245 4835 struct breakpoint *b = NULL;
afe38095 4836 struct bp_location *bl;
20874c92 4837 struct bp_location *loc;
5760d0ab
JK
4838 /* First item of allocated bpstat's. */
4839 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4840 /* Pointer to the last thing in the chain currently. */
5760d0ab 4841 bpstat bs;
20874c92 4842 int ix;
429374b8 4843 int need_remove_insert;
f431efe5 4844 int removed_any;
c906108c 4845
f431efe5
PA
4846 /* First, build the bpstat chain with locations that explain a
4847 target stop, while being careful to not set the target running,
4848 as that may invalidate locations (in particular watchpoint
4849 locations are recreated). Resuming will happen here with
4850 breakpoint conditions or watchpoint expressions that include
4851 inferior function calls. */
c5aa993b 4852
429374b8
JK
4853 ALL_BREAKPOINTS (b)
4854 {
4855 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4856 continue;
a5606eee 4857
429374b8
JK
4858 for (bl = b->loc; bl != NULL; bl = bl->next)
4859 {
4a64f543
MS
4860 /* For hardware watchpoints, we look only at the first
4861 location. The watchpoint_check function will work on the
4862 entire expression, not the individual locations. For
4863 read watchpoints, the watchpoints_triggered function has
4864 checked all locations already. */
429374b8
JK
4865 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4866 break;
18a18393 4867
429374b8
JK
4868 if (bl->shlib_disabled)
4869 continue;
c5aa993b 4870
09ac7c10 4871 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4872 continue;
c5aa993b 4873
4a64f543
MS
4874 /* Come here if it's a watchpoint, or if the break address
4875 matches. */
c5aa993b 4876
4a64f543
MS
4877 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4878 explain stop. */
c5aa993b 4879
f431efe5
PA
4880 /* Assume we stop. Should we find a watchpoint that is not
4881 actually triggered, or if the condition of the breakpoint
4882 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4883 bs->stop = 1;
4884 bs->print = 1;
d983da9c 4885
f431efe5
PA
4886 /* If this is a scope breakpoint, mark the associated
4887 watchpoint as triggered so that we will handle the
4888 out-of-scope event. We'll get to the watchpoint next
4889 iteration. */
d0fb5eae 4890 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4891 {
4892 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4893
4894 w->watchpoint_triggered = watch_triggered_yes;
4895 }
f431efe5
PA
4896 }
4897 }
4898
4899 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4900 {
f1310107 4901 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4902 {
5760d0ab 4903 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4904 /* For hits of moribund locations, we should just proceed. */
4905 bs->stop = 0;
4906 bs->print = 0;
4907 bs->print_it = print_it_noop;
4908 }
4909 }
4910
edcc5120
TT
4911 /* A bit of special processing for shlib breakpoints. We need to
4912 process solib loading here, so that the lists of loaded and
4913 unloaded libraries are correct before we handle "catch load" and
4914 "catch unload". */
4915 for (bs = bs_head; bs != NULL; bs = bs->next)
4916 {
5d268276 4917 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4918 {
4919 handle_solib_event ();
4920 break;
4921 }
4922 }
4923
f431efe5
PA
4924 /* Now go through the locations that caused the target to stop, and
4925 check whether we're interested in reporting this stop to higher
4926 layers, or whether we should resume the target transparently. */
4927
4928 removed_any = 0;
4929
5760d0ab 4930 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4931 {
4932 if (!bs->stop)
4933 continue;
4934
f431efe5 4935 b = bs->breakpoint_at;
348d480f
PA
4936 b->ops->check_status (bs);
4937 if (bs->stop)
28010a5d 4938 {
348d480f 4939 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 4940
429374b8
JK
4941 if (bs->stop)
4942 {
4943 ++(b->hit_count);
8d3788bd 4944 observer_notify_breakpoint_modified (b);
c906108c 4945
4a64f543 4946 /* We will stop here. */
429374b8
JK
4947 if (b->disposition == disp_disable)
4948 {
816338b5
SS
4949 --(b->enable_count);
4950 if (b->enable_count <= 0
4951 && b->enable_state != bp_permanent)
429374b8 4952 b->enable_state = bp_disabled;
f431efe5 4953 removed_any = 1;
429374b8
JK
4954 }
4955 if (b->silent)
4956 bs->print = 0;
4957 bs->commands = b->commands;
9add0f1b 4958 incref_counted_command_line (bs->commands);
abf85f46
JK
4959 if (command_line_is_silent (bs->commands
4960 ? bs->commands->commands : NULL))
4961 bs->print = 0;
429374b8
JK
4962 }
4963
348d480f 4964 }
a9b3a50f
PA
4965
4966 /* Print nothing for this entry if we don't stop or don't
4967 print. */
4968 if (!bs->stop || !bs->print)
4969 bs->print_it = print_it_noop;
429374b8 4970 }
876fa593 4971
d983da9c
DJ
4972 /* If we aren't stopping, the value of some hardware watchpoint may
4973 not have changed, but the intermediate memory locations we are
4974 watching may have. Don't bother if we're stopping; this will get
4975 done later. */
d832cb68 4976 need_remove_insert = 0;
5760d0ab
JK
4977 if (! bpstat_causes_stop (bs_head))
4978 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 4979 if (!bs->stop
f431efe5
PA
4980 && bs->breakpoint_at
4981 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 4982 {
3a5c3e22
PA
4983 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
4984
4985 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 4986 need_remove_insert = 1;
d983da9c
DJ
4987 }
4988
d832cb68 4989 if (need_remove_insert)
2d134ed3 4990 update_global_location_list (1);
f431efe5
PA
4991 else if (removed_any)
4992 update_global_location_list (0);
d832cb68 4993
5760d0ab 4994 return bs_head;
c906108c 4995}
628fe4e4
JK
4996
4997static void
4998handle_jit_event (void)
4999{
5000 struct frame_info *frame;
5001 struct gdbarch *gdbarch;
5002
5003 /* Switch terminal for any messages produced by
5004 breakpoint_re_set. */
5005 target_terminal_ours_for_output ();
5006
5007 frame = get_current_frame ();
5008 gdbarch = get_frame_arch (frame);
5009
5010 jit_event_handler (gdbarch);
5011
5012 target_terminal_inferior ();
5013}
5014
edcc5120
TT
5015/* Handle an solib event by calling solib_add. */
5016
5017void
5018handle_solib_event (void)
5019{
5020 clear_program_space_solib_cache (current_inferior ()->pspace);
5021
5022 /* Check for any newly added shared libraries if we're supposed to
5023 be adding them automatically. Switch terminal for any messages
5024 produced by breakpoint_re_set. */
5025 target_terminal_ours_for_output ();
5026#ifdef SOLIB_ADD
5027 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5028#else
5029 solib_add (NULL, 0, &current_target, auto_solib_add);
5030#endif
5031 target_terminal_inferior ();
5032}
5033
628fe4e4
JK
5034/* Prepare WHAT final decision for infrun. */
5035
5036/* Decide what infrun needs to do with this bpstat. */
5037
c906108c 5038struct bpstat_what
0e30163f 5039bpstat_what (bpstat bs_head)
c906108c 5040{
c906108c 5041 struct bpstat_what retval;
628fe4e4 5042 int jit_event = 0;
0e30163f 5043 bpstat bs;
c906108c 5044
628fe4e4 5045 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5046 retval.call_dummy = STOP_NONE;
186c406b 5047 retval.is_longjmp = 0;
628fe4e4 5048
0e30163f 5049 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5050 {
628fe4e4
JK
5051 /* Extract this BS's action. After processing each BS, we check
5052 if its action overrides all we've seem so far. */
5053 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5054 enum bptype bptype;
5055
c906108c 5056 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5057 {
5058 /* I suspect this can happen if it was a momentary
5059 breakpoint which has since been deleted. */
5060 bptype = bp_none;
5061 }
20874c92 5062 else
f431efe5 5063 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5064
5065 switch (bptype)
c906108c
SS
5066 {
5067 case bp_none:
628fe4e4 5068 break;
c906108c
SS
5069 case bp_breakpoint:
5070 case bp_hardware_breakpoint:
5071 case bp_until:
5072 case bp_finish:
a9b3a50f 5073 case bp_shlib_event:
c906108c
SS
5074 if (bs->stop)
5075 {
5076 if (bs->print)
628fe4e4 5077 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5078 else
628fe4e4 5079 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5080 }
5081 else
628fe4e4 5082 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5083 break;
5084 case bp_watchpoint:
5085 case bp_hardware_watchpoint:
5086 case bp_read_watchpoint:
5087 case bp_access_watchpoint:
5088 if (bs->stop)
5089 {
5090 if (bs->print)
628fe4e4 5091 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5092 else
628fe4e4 5093 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5094 }
5095 else
628fe4e4
JK
5096 {
5097 /* There was a watchpoint, but we're not stopping.
5098 This requires no further action. */
5099 }
c906108c
SS
5100 break;
5101 case bp_longjmp:
186c406b 5102 case bp_exception:
628fe4e4 5103 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
186c406b 5104 retval.is_longjmp = bptype == bp_longjmp;
c906108c
SS
5105 break;
5106 case bp_longjmp_resume:
186c406b 5107 case bp_exception_resume:
628fe4e4 5108 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5109 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5110 break;
5111 case bp_step_resume:
5112 if (bs->stop)
628fe4e4
JK
5113 this_action = BPSTAT_WHAT_STEP_RESUME;
5114 else
c906108c 5115 {
628fe4e4
JK
5116 /* It is for the wrong frame. */
5117 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5118 }
c906108c 5119 break;
2c03e5be
PA
5120 case bp_hp_step_resume:
5121 if (bs->stop)
5122 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5123 else
5124 {
5125 /* It is for the wrong frame. */
5126 this_action = BPSTAT_WHAT_SINGLE;
5127 }
5128 break;
c906108c 5129 case bp_watchpoint_scope:
c4093a6a 5130 case bp_thread_event:
1900040c 5131 case bp_overlay_event:
0fd8e87f 5132 case bp_longjmp_master:
aa7d318d 5133 case bp_std_terminate_master:
186c406b 5134 case bp_exception_master:
628fe4e4 5135 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5136 break;
ce78b96d 5137 case bp_catchpoint:
c5aa993b
JM
5138 if (bs->stop)
5139 {
5140 if (bs->print)
628fe4e4 5141 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5142 else
628fe4e4 5143 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5144 }
5145 else
628fe4e4
JK
5146 {
5147 /* There was a catchpoint, but we're not stopping.
5148 This requires no further action. */
5149 }
5150 break;
628fe4e4
JK
5151 case bp_jit_event:
5152 jit_event = 1;
5153 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5154 break;
c906108c 5155 case bp_call_dummy:
53a5351d
JM
5156 /* Make sure the action is stop (silent or noisy),
5157 so infrun.c pops the dummy frame. */
aa7d318d 5158 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5159 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5160 break;
5161 case bp_std_terminate:
5162 /* Make sure the action is stop (silent or noisy),
5163 so infrun.c pops the dummy frame. */
aa7d318d 5164 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5165 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5166 break;
1042e4c0 5167 case bp_tracepoint:
7a697b8d 5168 case bp_fast_tracepoint:
0fb4aa4b 5169 case bp_static_tracepoint:
1042e4c0
SS
5170 /* Tracepoint hits should not be reported back to GDB, and
5171 if one got through somehow, it should have been filtered
5172 out already. */
5173 internal_error (__FILE__, __LINE__,
7a697b8d 5174 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5175 break;
5176 case bp_gnu_ifunc_resolver:
5177 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5178 this_action = BPSTAT_WHAT_SINGLE;
5179 break;
5180 case bp_gnu_ifunc_resolver_return:
5181 /* The breakpoint will be removed, execution will restart from the
5182 PC of the former breakpoint. */
5183 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5184 break;
e7e0cddf
SS
5185
5186 case bp_dprintf:
5187 this_action = BPSTAT_WHAT_STOP_SILENT;
5188 break;
5189
628fe4e4
JK
5190 default:
5191 internal_error (__FILE__, __LINE__,
5192 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5193 }
628fe4e4
JK
5194
5195 retval.main_action = max (retval.main_action, this_action);
c906108c 5196 }
628fe4e4 5197
0e30163f
JK
5198 /* These operations may affect the bs->breakpoint_at state so they are
5199 delayed after MAIN_ACTION is decided above. */
5200
628fe4e4
JK
5201 if (jit_event)
5202 {
5203 if (debug_infrun)
5204 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5205
5206 handle_jit_event ();
5207 }
5208
0e30163f
JK
5209 for (bs = bs_head; bs != NULL; bs = bs->next)
5210 {
5211 struct breakpoint *b = bs->breakpoint_at;
5212
5213 if (b == NULL)
5214 continue;
5215 switch (b->type)
5216 {
5217 case bp_gnu_ifunc_resolver:
5218 gnu_ifunc_resolver_stop (b);
5219 break;
5220 case bp_gnu_ifunc_resolver_return:
5221 gnu_ifunc_resolver_return_stop (b);
5222 break;
5223 }
5224 }
5225
c906108c
SS
5226 return retval;
5227}
5228
5229/* Nonzero if we should step constantly (e.g. watchpoints on machines
5230 without hardware support). This isn't related to a specific bpstat,
5231 just to things like whether watchpoints are set. */
5232
c5aa993b 5233int
fba45db2 5234bpstat_should_step (void)
c906108c
SS
5235{
5236 struct breakpoint *b;
cc59ec59 5237
c906108c 5238 ALL_BREAKPOINTS (b)
717a8278 5239 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5240 return 1;
c906108c
SS
5241 return 0;
5242}
5243
67822962
PA
5244int
5245bpstat_causes_stop (bpstat bs)
5246{
5247 for (; bs != NULL; bs = bs->next)
5248 if (bs->stop)
5249 return 1;
5250
5251 return 0;
5252}
5253
c906108c 5254\f
c5aa993b 5255
170b53b2
UW
5256/* Compute a string of spaces suitable to indent the next line
5257 so it starts at the position corresponding to the table column
5258 named COL_NAME in the currently active table of UIOUT. */
5259
5260static char *
5261wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5262{
5263 static char wrap_indent[80];
5264 int i, total_width, width, align;
5265 char *text;
5266
5267 total_width = 0;
5268 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5269 {
5270 if (strcmp (text, col_name) == 0)
5271 {
5272 gdb_assert (total_width < sizeof wrap_indent);
5273 memset (wrap_indent, ' ', total_width);
5274 wrap_indent[total_width] = 0;
5275
5276 return wrap_indent;
5277 }
5278
5279 total_width += width + 1;
5280 }
5281
5282 return NULL;
5283}
5284
b775012e
LM
5285/* Determine if the locations of this breakpoint will have their conditions
5286 evaluated by the target, host or a mix of both. Returns the following:
5287
5288 "host": Host evals condition.
5289 "host or target": Host or Target evals condition.
5290 "target": Target evals condition.
5291*/
5292
5293static const char *
5294bp_condition_evaluator (struct breakpoint *b)
5295{
5296 struct bp_location *bl;
5297 char host_evals = 0;
5298 char target_evals = 0;
5299
5300 if (!b)
5301 return NULL;
5302
5303 if (!is_breakpoint (b))
5304 return NULL;
5305
5306 if (gdb_evaluates_breakpoint_condition_p ()
5307 || !target_supports_evaluation_of_breakpoint_conditions ())
5308 return condition_evaluation_host;
5309
5310 for (bl = b->loc; bl; bl = bl->next)
5311 {
5312 if (bl->cond_bytecode)
5313 target_evals++;
5314 else
5315 host_evals++;
5316 }
5317
5318 if (host_evals && target_evals)
5319 return condition_evaluation_both;
5320 else if (target_evals)
5321 return condition_evaluation_target;
5322 else
5323 return condition_evaluation_host;
5324}
5325
5326/* Determine the breakpoint location's condition evaluator. This is
5327 similar to bp_condition_evaluator, but for locations. */
5328
5329static const char *
5330bp_location_condition_evaluator (struct bp_location *bl)
5331{
5332 if (bl && !is_breakpoint (bl->owner))
5333 return NULL;
5334
5335 if (gdb_evaluates_breakpoint_condition_p ()
5336 || !target_supports_evaluation_of_breakpoint_conditions ())
5337 return condition_evaluation_host;
5338
5339 if (bl && bl->cond_bytecode)
5340 return condition_evaluation_target;
5341 else
5342 return condition_evaluation_host;
5343}
5344
859825b8
JK
5345/* Print the LOC location out of the list of B->LOC locations. */
5346
170b53b2
UW
5347static void
5348print_breakpoint_location (struct breakpoint *b,
5349 struct bp_location *loc)
0d381245 5350{
79a45e25 5351 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5352 struct cleanup *old_chain = save_current_program_space ();
5353
859825b8
JK
5354 if (loc != NULL && loc->shlib_disabled)
5355 loc = NULL;
5356
6c95b8df
PA
5357 if (loc != NULL)
5358 set_current_program_space (loc->pspace);
5359
56435ebe
TT
5360 if (b->display_canonical)
5361 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 5362 else if (loc && loc->source_file)
0d381245
VP
5363 {
5364 struct symbol *sym
5365 = find_pc_sect_function (loc->address, loc->section);
5366 if (sym)
5367 {
5368 ui_out_text (uiout, "in ");
5369 ui_out_field_string (uiout, "func",
5370 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5371 ui_out_text (uiout, " ");
5372 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5373 ui_out_text (uiout, "at ");
0d381245 5374 }
f8eba3c6 5375 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
5376 ui_out_text (uiout, ":");
5377
5378 if (ui_out_is_mi_like_p (uiout))
5379 {
5380 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5381 char *fullname = symtab_to_fullname (sal.symtab);
5382
5383 if (fullname)
5384 ui_out_field_string (uiout, "fullname", fullname);
5385 }
5386
f8eba3c6 5387 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5388 }
859825b8 5389 else if (loc)
0d381245 5390 {
f99d8bf4
PA
5391 struct ui_file *stb = mem_fileopen ();
5392 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5393
f99d8bf4 5394 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5395 demangle, "");
0d381245 5396 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5397
5398 do_cleanups (stb_chain);
0d381245 5399 }
859825b8
JK
5400 else
5401 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5402
b775012e
LM
5403 if (loc && is_breakpoint (b)
5404 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5405 && bp_condition_evaluator (b) == condition_evaluation_both)
5406 {
5407 ui_out_text (uiout, " (");
5408 ui_out_field_string (uiout, "evaluated-by",
5409 bp_location_condition_evaluator (loc));
5410 ui_out_text (uiout, ")");
5411 }
5412
6c95b8df 5413 do_cleanups (old_chain);
0d381245
VP
5414}
5415
269b11a2
PA
5416static const char *
5417bptype_string (enum bptype type)
c906108c 5418{
c4093a6a
JM
5419 struct ep_type_description
5420 {
5421 enum bptype type;
5422 char *description;
5423 };
5424 static struct ep_type_description bptypes[] =
c906108c 5425 {
c5aa993b
JM
5426 {bp_none, "?deleted?"},
5427 {bp_breakpoint, "breakpoint"},
c906108c 5428 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5429 {bp_until, "until"},
5430 {bp_finish, "finish"},
5431 {bp_watchpoint, "watchpoint"},
c906108c 5432 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5433 {bp_read_watchpoint, "read watchpoint"},
5434 {bp_access_watchpoint, "acc watchpoint"},
5435 {bp_longjmp, "longjmp"},
5436 {bp_longjmp_resume, "longjmp resume"},
186c406b
TT
5437 {bp_exception, "exception"},
5438 {bp_exception_resume, "exception resume"},
c5aa993b 5439 {bp_step_resume, "step resume"},
2c03e5be 5440 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5441 {bp_watchpoint_scope, "watchpoint scope"},
5442 {bp_call_dummy, "call dummy"},
aa7d318d 5443 {bp_std_terminate, "std::terminate"},
c5aa993b 5444 {bp_shlib_event, "shlib events"},
c4093a6a 5445 {bp_thread_event, "thread events"},
1900040c 5446 {bp_overlay_event, "overlay events"},
0fd8e87f 5447 {bp_longjmp_master, "longjmp master"},
aa7d318d 5448 {bp_std_terminate_master, "std::terminate master"},
186c406b 5449 {bp_exception_master, "exception master"},
ce78b96d 5450 {bp_catchpoint, "catchpoint"},
1042e4c0 5451 {bp_tracepoint, "tracepoint"},
7a697b8d 5452 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5453 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5454 {bp_dprintf, "dprintf"},
4efc6507 5455 {bp_jit_event, "jit events"},
0e30163f
JK
5456 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5457 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5458 };
269b11a2
PA
5459
5460 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5461 || ((int) type != bptypes[(int) type].type))
5462 internal_error (__FILE__, __LINE__,
5463 _("bptypes table does not describe type #%d."),
5464 (int) type);
5465
5466 return bptypes[(int) type].description;
5467}
5468
5469/* Print B to gdb_stdout. */
5470
5471static void
5472print_one_breakpoint_location (struct breakpoint *b,
5473 struct bp_location *loc,
5474 int loc_number,
5475 struct bp_location **last_loc,
269b11a2
PA
5476 int allflag)
5477{
5478 struct command_line *l;
c2c6d25f 5479 static char bpenables[] = "nynny";
c906108c 5480
79a45e25 5481 struct ui_out *uiout = current_uiout;
0d381245
VP
5482 int header_of_multiple = 0;
5483 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5484 struct value_print_options opts;
5485
5486 get_user_print_options (&opts);
0d381245
VP
5487
5488 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5489 /* See comment in print_one_breakpoint concerning treatment of
5490 breakpoints with single disabled location. */
0d381245
VP
5491 if (loc == NULL
5492 && (b->loc != NULL
5493 && (b->loc->next != NULL || !b->loc->enabled)))
5494 header_of_multiple = 1;
5495 if (loc == NULL)
5496 loc = b->loc;
5497
c4093a6a
JM
5498 annotate_record ();
5499
5500 /* 1 */
5501 annotate_field (0);
0d381245
VP
5502 if (part_of_multiple)
5503 {
5504 char *formatted;
0c6773c1 5505 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5506 ui_out_field_string (uiout, "number", formatted);
5507 xfree (formatted);
5508 }
5509 else
5510 {
5511 ui_out_field_int (uiout, "number", b->number);
5512 }
c4093a6a
JM
5513
5514 /* 2 */
5515 annotate_field (1);
0d381245
VP
5516 if (part_of_multiple)
5517 ui_out_field_skip (uiout, "type");
269b11a2
PA
5518 else
5519 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5520
5521 /* 3 */
5522 annotate_field (2);
0d381245
VP
5523 if (part_of_multiple)
5524 ui_out_field_skip (uiout, "disp");
5525 else
2cec12e5 5526 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5527
c4093a6a
JM
5528
5529 /* 4 */
5530 annotate_field (3);
0d381245 5531 if (part_of_multiple)
54e52265 5532 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5533 else
4a64f543
MS
5534 ui_out_field_fmt (uiout, "enabled", "%c",
5535 bpenables[(int) b->enable_state]);
54e52265 5536 ui_out_spaces (uiout, 2);
0d381245 5537
c4093a6a
JM
5538
5539 /* 5 and 6 */
3086aeae 5540 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5541 {
4a64f543
MS
5542 /* Although the print_one can possibly print all locations,
5543 calling it here is not likely to get any nice result. So,
5544 make sure there's just one location. */
0d381245 5545 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5546 b->ops->print_one (b, last_loc);
0d381245 5547 }
3086aeae
DJ
5548 else
5549 switch (b->type)
5550 {
5551 case bp_none:
5552 internal_error (__FILE__, __LINE__,
e2e0b3e5 5553 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5554 break;
c906108c 5555
3086aeae
DJ
5556 case bp_watchpoint:
5557 case bp_hardware_watchpoint:
5558 case bp_read_watchpoint:
5559 case bp_access_watchpoint:
3a5c3e22
PA
5560 {
5561 struct watchpoint *w = (struct watchpoint *) b;
5562
5563 /* Field 4, the address, is omitted (which makes the columns
5564 not line up too nicely with the headers, but the effect
5565 is relatively readable). */
5566 if (opts.addressprint)
5567 ui_out_field_skip (uiout, "addr");
5568 annotate_field (5);
5569 ui_out_field_string (uiout, "what", w->exp_string);
5570 }
3086aeae
DJ
5571 break;
5572
3086aeae
DJ
5573 case bp_breakpoint:
5574 case bp_hardware_breakpoint:
5575 case bp_until:
5576 case bp_finish:
5577 case bp_longjmp:
5578 case bp_longjmp_resume:
186c406b
TT
5579 case bp_exception:
5580 case bp_exception_resume:
3086aeae 5581 case bp_step_resume:
2c03e5be 5582 case bp_hp_step_resume:
3086aeae
DJ
5583 case bp_watchpoint_scope:
5584 case bp_call_dummy:
aa7d318d 5585 case bp_std_terminate:
3086aeae
DJ
5586 case bp_shlib_event:
5587 case bp_thread_event:
5588 case bp_overlay_event:
0fd8e87f 5589 case bp_longjmp_master:
aa7d318d 5590 case bp_std_terminate_master:
186c406b 5591 case bp_exception_master:
1042e4c0 5592 case bp_tracepoint:
7a697b8d 5593 case bp_fast_tracepoint:
0fb4aa4b 5594 case bp_static_tracepoint:
e7e0cddf 5595 case bp_dprintf:
4efc6507 5596 case bp_jit_event:
0e30163f
JK
5597 case bp_gnu_ifunc_resolver:
5598 case bp_gnu_ifunc_resolver_return:
79a45b7d 5599 if (opts.addressprint)
3086aeae
DJ
5600 {
5601 annotate_field (4);
54e52265 5602 if (header_of_multiple)
0d381245 5603 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 5604 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 5605 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 5606 else
5af949e3
UW
5607 ui_out_field_core_addr (uiout, "addr",
5608 loc->gdbarch, loc->address);
3086aeae
DJ
5609 }
5610 annotate_field (5);
0d381245 5611 if (!header_of_multiple)
170b53b2 5612 print_breakpoint_location (b, loc);
0d381245 5613 if (b->loc)
a6d9a66e 5614 *last_loc = b->loc;
3086aeae
DJ
5615 break;
5616 }
c906108c 5617
6c95b8df
PA
5618
5619 /* For backward compatibility, don't display inferiors unless there
5620 are several. */
5621 if (loc != NULL
5622 && !header_of_multiple
5623 && (allflag
5624 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5625 && (number_of_program_spaces () > 1
5626 || number_of_inferiors () > 1)
4a64f543
MS
5627 /* LOC is for existing B, it cannot be in
5628 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
5629 && loc->owner->type != bp_catchpoint)))
5630 {
5631 struct inferior *inf;
5632 int first = 1;
5633
5634 for (inf = inferior_list; inf != NULL; inf = inf->next)
5635 {
5636 if (inf->pspace == loc->pspace)
5637 {
5638 if (first)
5639 {
5640 first = 0;
5641 ui_out_text (uiout, " inf ");
5642 }
5643 else
5644 ui_out_text (uiout, ", ");
5645 ui_out_text (uiout, plongest (inf->num));
5646 }
5647 }
5648 }
5649
4a306c9a 5650 if (!part_of_multiple)
c4093a6a 5651 {
4a306c9a
JB
5652 if (b->thread != -1)
5653 {
5654 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5655 "stop only in" line a little further down. */
4a306c9a
JB
5656 ui_out_text (uiout, " thread ");
5657 ui_out_field_int (uiout, "thread", b->thread);
5658 }
5659 else if (b->task != 0)
5660 {
5661 ui_out_text (uiout, " task ");
5662 ui_out_field_int (uiout, "task", b->task);
5663 }
c4093a6a 5664 }
f1310107 5665
8b93c638 5666 ui_out_text (uiout, "\n");
f1310107 5667
348d480f 5668 if (!part_of_multiple)
f1310107
TJB
5669 b->ops->print_one_detail (b, uiout);
5670
0d381245 5671 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5672 {
5673 annotate_field (6);
8b93c638 5674 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 5675 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 5676 the frame ID. */
5af949e3
UW
5677 ui_out_field_core_addr (uiout, "frame",
5678 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5679 ui_out_text (uiout, "\n");
c4093a6a
JM
5680 }
5681
28010a5d 5682 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5683 {
5684 annotate_field (7);
d77f58be 5685 if (is_tracepoint (b))
1042e4c0
SS
5686 ui_out_text (uiout, "\ttrace only if ");
5687 else
5688 ui_out_text (uiout, "\tstop only if ");
0101ce28 5689 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
5690
5691 /* Print whether the target is doing the breakpoint's condition
5692 evaluation. If GDB is doing the evaluation, don't print anything. */
5693 if (is_breakpoint (b)
5694 && breakpoint_condition_evaluation_mode ()
5695 == condition_evaluation_target)
5696 {
5697 ui_out_text (uiout, " (");
5698 ui_out_field_string (uiout, "evaluated-by",
5699 bp_condition_evaluator (b));
5700 ui_out_text (uiout, " evals)");
5701 }
0101ce28
JJ
5702 ui_out_text (uiout, "\n");
5703 }
5704
0d381245 5705 if (!part_of_multiple && b->thread != -1)
c4093a6a 5706 {
4a64f543 5707 /* FIXME should make an annotation for this. */
8b93c638
JM
5708 ui_out_text (uiout, "\tstop only in thread ");
5709 ui_out_field_int (uiout, "thread", b->thread);
5710 ui_out_text (uiout, "\n");
c4093a6a
JM
5711 }
5712
63c715c6 5713 if (!part_of_multiple && b->hit_count)
c4093a6a 5714 {
4a64f543 5715 /* FIXME should make an annotation for this. */
c326b90e 5716 if (is_catchpoint (b))
8b93c638 5717 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5718 else if (is_tracepoint (b))
5719 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5720 else
5721 ui_out_text (uiout, "\tbreakpoint");
5722 ui_out_text (uiout, " already hit ");
5723 ui_out_field_int (uiout, "times", b->hit_count);
5724 if (b->hit_count == 1)
5725 ui_out_text (uiout, " time\n");
5726 else
5727 ui_out_text (uiout, " times\n");
c4093a6a
JM
5728 }
5729
4a64f543
MS
5730 /* Output the count also if it is zero, but only if this is mi.
5731 FIXME: Should have a better test for this. */
9dc5e2a9 5732 if (ui_out_is_mi_like_p (uiout))
63c715c6 5733 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5734 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5735
0d381245 5736 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5737 {
5738 annotate_field (8);
8b93c638
JM
5739 ui_out_text (uiout, "\tignore next ");
5740 ui_out_field_int (uiout, "ignore", b->ignore_count);
5741 ui_out_text (uiout, " hits\n");
c4093a6a 5742 }
059fb39f 5743
816338b5
SS
5744 /* Note that an enable count of 1 corresponds to "enable once"
5745 behavior, which is reported by the combination of enablement and
5746 disposition, so we don't need to mention it here. */
5747 if (!part_of_multiple && b->enable_count > 1)
5748 {
5749 annotate_field (8);
5750 ui_out_text (uiout, "\tdisable after ");
5751 /* Tweak the wording to clarify that ignore and enable counts
5752 are distinct, and have additive effect. */
5753 if (b->ignore_count)
5754 ui_out_text (uiout, "additional ");
5755 else
5756 ui_out_text (uiout, "next ");
5757 ui_out_field_int (uiout, "enable", b->enable_count);
5758 ui_out_text (uiout, " hits\n");
5759 }
5760
f196051f
SS
5761 if (!part_of_multiple && is_tracepoint (b))
5762 {
5763 struct tracepoint *tp = (struct tracepoint *) b;
5764
5765 if (tp->traceframe_usage)
5766 {
5767 ui_out_text (uiout, "\ttrace buffer usage ");
5768 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5769 ui_out_text (uiout, " bytes\n");
5770 }
5771 }
5772
9add0f1b 5773 l = b->commands ? b->commands->commands : NULL;
059fb39f 5774 if (!part_of_multiple && l)
c4093a6a 5775 {
3b31d625
EZ
5776 struct cleanup *script_chain;
5777
c4093a6a 5778 annotate_field (9);
3b31d625 5779 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5780 print_command_lines (uiout, l, 4);
3b31d625 5781 do_cleanups (script_chain);
c4093a6a 5782 }
d24317b4 5783
d9b3f62e 5784 if (is_tracepoint (b))
1042e4c0 5785 {
d9b3f62e
PA
5786 struct tracepoint *t = (struct tracepoint *) b;
5787
5788 if (!part_of_multiple && t->pass_count)
5789 {
5790 annotate_field (10);
5791 ui_out_text (uiout, "\tpass count ");
5792 ui_out_field_int (uiout, "pass", t->pass_count);
5793 ui_out_text (uiout, " \n");
5794 }
1042e4c0
SS
5795 }
5796
d24317b4
VP
5797 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5798 {
3a5c3e22
PA
5799 if (is_watchpoint (b))
5800 {
5801 struct watchpoint *w = (struct watchpoint *) b;
5802
5803 ui_out_field_string (uiout, "original-location", w->exp_string);
5804 }
5805 else if (b->addr_string)
d24317b4 5806 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5807 }
c4093a6a 5808}
c5aa993b 5809
0d381245
VP
5810static void
5811print_one_breakpoint (struct breakpoint *b,
4a64f543 5812 struct bp_location **last_loc,
6c95b8df 5813 int allflag)
0d381245 5814{
8d3788bd 5815 struct cleanup *bkpt_chain;
79a45e25 5816 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5817
5818 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5819
12c5a436 5820 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5821 do_cleanups (bkpt_chain);
0d381245
VP
5822
5823 /* If this breakpoint has custom print function,
5824 it's already printed. Otherwise, print individual
5825 locations, if any. */
5826 if (b->ops == NULL || b->ops->print_one == NULL)
5827 {
4a64f543
MS
5828 /* If breakpoint has a single location that is disabled, we
5829 print it as if it had several locations, since otherwise it's
5830 hard to represent "breakpoint enabled, location disabled"
5831 situation.
5832
5833 Note that while hardware watchpoints have several locations
a3be7890 5834 internally, that's not a property exposed to user. */
0d381245 5835 if (b->loc
a5606eee 5836 && !is_hardware_watchpoint (b)
8d3788bd 5837 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5838 {
5839 struct bp_location *loc;
5840 int n = 1;
8d3788bd 5841
0d381245 5842 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5843 {
5844 struct cleanup *inner2 =
5845 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5846 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5847 do_cleanups (inner2);
5848 }
0d381245
VP
5849 }
5850 }
5851}
5852
a6d9a66e
UW
5853static int
5854breakpoint_address_bits (struct breakpoint *b)
5855{
5856 int print_address_bits = 0;
5857 struct bp_location *loc;
5858
5859 for (loc = b->loc; loc; loc = loc->next)
5860 {
c7437ca6
PA
5861 int addr_bit;
5862
5863 /* Software watchpoints that aren't watching memory don't have
5864 an address to print. */
5865 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5866 continue;
5867
5868 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5869 if (addr_bit > print_address_bits)
5870 print_address_bits = addr_bit;
5871 }
5872
5873 return print_address_bits;
5874}
0d381245 5875
c4093a6a
JM
5876struct captured_breakpoint_query_args
5877 {
5878 int bnum;
5879 };
c5aa993b 5880
c4093a6a 5881static int
2b65245e 5882do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5883{
5884 struct captured_breakpoint_query_args *args = data;
52f0bd74 5885 struct breakpoint *b;
a6d9a66e 5886 struct bp_location *dummy_loc = NULL;
cc59ec59 5887
c4093a6a
JM
5888 ALL_BREAKPOINTS (b)
5889 {
5890 if (args->bnum == b->number)
c5aa993b 5891 {
12c5a436 5892 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5893 return GDB_RC_OK;
c5aa993b 5894 }
c4093a6a
JM
5895 }
5896 return GDB_RC_NONE;
5897}
c5aa993b 5898
c4093a6a 5899enum gdb_rc
4a64f543
MS
5900gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5901 char **error_message)
c4093a6a
JM
5902{
5903 struct captured_breakpoint_query_args args;
cc59ec59 5904
c4093a6a
JM
5905 args.bnum = bnum;
5906 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5907 an error. */
b0b13bb4
DJ
5908 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5909 error_message, RETURN_MASK_ALL) < 0)
5910 return GDB_RC_FAIL;
5911 else
5912 return GDB_RC_OK;
c4093a6a 5913}
c5aa993b 5914
09d682a4
TT
5915/* Return true if this breakpoint was set by the user, false if it is
5916 internal or momentary. */
5917
5918int
5919user_breakpoint_p (struct breakpoint *b)
5920{
46c6471b 5921 return b->number > 0;
09d682a4
TT
5922}
5923
7f3b0473 5924/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5925 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5926 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5927 FILTER is non-NULL, call it on each breakpoint and only include the
5928 ones for which it returns non-zero. Return the total number of
5929 breakpoints listed. */
c906108c 5930
d77f58be 5931static int
e5a67952 5932breakpoint_1 (char *args, int allflag,
4a64f543 5933 int (*filter) (const struct breakpoint *))
c4093a6a 5934{
52f0bd74 5935 struct breakpoint *b;
a6d9a66e 5936 struct bp_location *last_loc = NULL;
7f3b0473 5937 int nr_printable_breakpoints;
3b31d625 5938 struct cleanup *bkpttbl_chain;
79a45b7d 5939 struct value_print_options opts;
a6d9a66e 5940 int print_address_bits = 0;
269b11a2 5941 int print_type_col_width = 14;
79a45e25 5942 struct ui_out *uiout = current_uiout;
269b11a2 5943
79a45b7d
TT
5944 get_user_print_options (&opts);
5945
4a64f543
MS
5946 /* Compute the number of rows in the table, as well as the size
5947 required for address fields. */
7f3b0473
AC
5948 nr_printable_breakpoints = 0;
5949 ALL_BREAKPOINTS (b)
e5a67952
MS
5950 {
5951 /* If we have a filter, only list the breakpoints it accepts. */
5952 if (filter && !filter (b))
5953 continue;
5954
5955 /* If we have an "args" string, it is a list of breakpoints to
5956 accept. Skip the others. */
5957 if (args != NULL && *args != '\0')
5958 {
5959 if (allflag && parse_and_eval_long (args) != b->number)
5960 continue;
5961 if (!allflag && !number_is_in_list (args, b->number))
5962 continue;
5963 }
269b11a2 5964
e5a67952
MS
5965 if (allflag || user_breakpoint_p (b))
5966 {
5967 int addr_bit, type_len;
a6d9a66e 5968
e5a67952
MS
5969 addr_bit = breakpoint_address_bits (b);
5970 if (addr_bit > print_address_bits)
5971 print_address_bits = addr_bit;
269b11a2 5972
e5a67952
MS
5973 type_len = strlen (bptype_string (b->type));
5974 if (type_len > print_type_col_width)
5975 print_type_col_width = type_len;
5976
5977 nr_printable_breakpoints++;
5978 }
5979 }
7f3b0473 5980
79a45b7d 5981 if (opts.addressprint)
3b31d625 5982 bkpttbl_chain
3e43a32a
MS
5983 = make_cleanup_ui_out_table_begin_end (uiout, 6,
5984 nr_printable_breakpoints,
3b31d625 5985 "BreakpointTable");
8b93c638 5986 else
3b31d625 5987 bkpttbl_chain
3e43a32a
MS
5988 = make_cleanup_ui_out_table_begin_end (uiout, 5,
5989 nr_printable_breakpoints,
3b31d625 5990 "BreakpointTable");
8b93c638 5991
7f3b0473 5992 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
5993 annotate_breakpoints_headers ();
5994 if (nr_printable_breakpoints > 0)
5995 annotate_field (0);
4a64f543 5996 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
5997 if (nr_printable_breakpoints > 0)
5998 annotate_field (1);
269b11a2 5999 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6000 "type", "Type"); /* 2 */
d7faa9e7
AC
6001 if (nr_printable_breakpoints > 0)
6002 annotate_field (2);
4a64f543 6003 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6004 if (nr_printable_breakpoints > 0)
6005 annotate_field (3);
54e52265 6006 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6007 if (opts.addressprint)
e5a67952
MS
6008 {
6009 if (nr_printable_breakpoints > 0)
6010 annotate_field (4);
6011 if (print_address_bits <= 32)
6012 ui_out_table_header (uiout, 10, ui_left,
6013 "addr", "Address"); /* 5 */
6014 else
6015 ui_out_table_header (uiout, 18, ui_left,
6016 "addr", "Address"); /* 5 */
6017 }
d7faa9e7
AC
6018 if (nr_printable_breakpoints > 0)
6019 annotate_field (5);
6020 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6021 ui_out_table_body (uiout);
6022 if (nr_printable_breakpoints > 0)
6023 annotate_breakpoints_table ();
7f3b0473 6024
c4093a6a 6025 ALL_BREAKPOINTS (b)
e5a67952
MS
6026 {
6027 QUIT;
6028 /* If we have a filter, only list the breakpoints it accepts. */
6029 if (filter && !filter (b))
6030 continue;
6031
6032 /* If we have an "args" string, it is a list of breakpoints to
6033 accept. Skip the others. */
6034
6035 if (args != NULL && *args != '\0')
6036 {
6037 if (allflag) /* maintenance info breakpoint */
6038 {
6039 if (parse_and_eval_long (args) != b->number)
6040 continue;
6041 }
6042 else /* all others */
6043 {
6044 if (!number_is_in_list (args, b->number))
6045 continue;
6046 }
6047 }
6048 /* We only print out user settable breakpoints unless the
6049 allflag is set. */
6050 if (allflag || user_breakpoint_p (b))
12c5a436 6051 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6052 }
6053
3b31d625 6054 do_cleanups (bkpttbl_chain);
698384cd 6055
7f3b0473 6056 if (nr_printable_breakpoints == 0)
c906108c 6057 {
4a64f543
MS
6058 /* If there's a filter, let the caller decide how to report
6059 empty list. */
d77f58be
SS
6060 if (!filter)
6061 {
e5a67952 6062 if (args == NULL || *args == '\0')
d77f58be
SS
6063 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6064 else
4a64f543 6065 ui_out_message (uiout, 0,
e5a67952
MS
6066 "No breakpoint or watchpoint matching '%s'.\n",
6067 args);
d77f58be 6068 }
c906108c
SS
6069 }
6070 else
c4093a6a 6071 {
a6d9a66e
UW
6072 if (last_loc && !server_command)
6073 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6074 }
c906108c 6075
4a64f543 6076 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6077 there have been breakpoints? */
c906108c 6078 annotate_breakpoints_table_end ();
d77f58be
SS
6079
6080 return nr_printable_breakpoints;
c906108c
SS
6081}
6082
ad443146
SS
6083/* Display the value of default-collect in a way that is generally
6084 compatible with the breakpoint list. */
6085
6086static void
6087default_collect_info (void)
6088{
79a45e25
PA
6089 struct ui_out *uiout = current_uiout;
6090
ad443146
SS
6091 /* If it has no value (which is frequently the case), say nothing; a
6092 message like "No default-collect." gets in user's face when it's
6093 not wanted. */
6094 if (!*default_collect)
6095 return;
6096
6097 /* The following phrase lines up nicely with per-tracepoint collect
6098 actions. */
6099 ui_out_text (uiout, "default collect ");
6100 ui_out_field_string (uiout, "default-collect", default_collect);
6101 ui_out_text (uiout, " \n");
6102}
6103
c906108c 6104static void
e5a67952 6105breakpoints_info (char *args, int from_tty)
c906108c 6106{
e5a67952 6107 breakpoint_1 (args, 0, NULL);
ad443146
SS
6108
6109 default_collect_info ();
d77f58be
SS
6110}
6111
6112static void
e5a67952 6113watchpoints_info (char *args, int from_tty)
d77f58be 6114{
e5a67952 6115 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6116 struct ui_out *uiout = current_uiout;
d77f58be
SS
6117
6118 if (num_printed == 0)
6119 {
e5a67952 6120 if (args == NULL || *args == '\0')
d77f58be
SS
6121 ui_out_message (uiout, 0, "No watchpoints.\n");
6122 else
e5a67952 6123 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6124 }
c906108c
SS
6125}
6126
7a292a7a 6127static void
e5a67952 6128maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6129{
e5a67952 6130 breakpoint_1 (args, 1, NULL);
ad443146
SS
6131
6132 default_collect_info ();
c906108c
SS
6133}
6134
0d381245 6135static int
714835d5 6136breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6137 struct program_space *pspace,
714835d5 6138 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6139{
6140 struct bp_location *bl = b->loc;
cc59ec59 6141
0d381245
VP
6142 for (; bl; bl = bl->next)
6143 {
6c95b8df
PA
6144 if (bl->pspace == pspace
6145 && bl->address == pc
0d381245
VP
6146 && (!overlay_debugging || bl->section == section))
6147 return 1;
6148 }
6149 return 0;
6150}
6151
672f9b60 6152/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6153 concerns with logical breakpoints, so we match program spaces, not
6154 address spaces. */
c906108c
SS
6155
6156static void
6c95b8df
PA
6157describe_other_breakpoints (struct gdbarch *gdbarch,
6158 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6159 struct obj_section *section, int thread)
c906108c 6160{
52f0bd74
AC
6161 int others = 0;
6162 struct breakpoint *b;
c906108c
SS
6163
6164 ALL_BREAKPOINTS (b)
672f9b60
KP
6165 others += (user_breakpoint_p (b)
6166 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6167 if (others > 0)
6168 {
a3f17187
AC
6169 if (others == 1)
6170 printf_filtered (_("Note: breakpoint "));
6171 else /* if (others == ???) */
6172 printf_filtered (_("Note: breakpoints "));
c906108c 6173 ALL_BREAKPOINTS (b)
672f9b60 6174 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6175 {
6176 others--;
6177 printf_filtered ("%d", b->number);
6178 if (b->thread == -1 && thread != -1)
6179 printf_filtered (" (all threads)");
6180 else if (b->thread != -1)
6181 printf_filtered (" (thread %d)", b->thread);
6182 printf_filtered ("%s%s ",
059fb39f 6183 ((b->enable_state == bp_disabled
f8eba3c6 6184 || b->enable_state == bp_call_disabled)
0d381245
VP
6185 ? " (disabled)"
6186 : b->enable_state == bp_permanent
6187 ? " (permanent)"
6188 : ""),
6189 (others > 1) ? ","
6190 : ((others == 1) ? " and" : ""));
6191 }
a3f17187 6192 printf_filtered (_("also set at pc "));
5af949e3 6193 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6194 printf_filtered (".\n");
6195 }
6196}
6197\f
c906108c 6198
e4f237da
KB
6199/* Return true iff it is meaningful to use the address member of
6200 BPT. For some breakpoint types, the address member is irrelevant
6201 and it makes no sense to attempt to compare it to other addresses
6202 (or use it for any other purpose either).
6203
4a64f543
MS
6204 More specifically, each of the following breakpoint types will
6205 always have a zero valued address and we don't want to mark
6206 breakpoints of any of these types to be a duplicate of an actual
6207 breakpoint at address zero:
e4f237da
KB
6208
6209 bp_watchpoint
2d134ed3
PA
6210 bp_catchpoint
6211
6212*/
e4f237da
KB
6213
6214static int
6215breakpoint_address_is_meaningful (struct breakpoint *bpt)
6216{
6217 enum bptype type = bpt->type;
6218
2d134ed3
PA
6219 return (type != bp_watchpoint && type != bp_catchpoint);
6220}
6221
6222/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6223 true if LOC1 and LOC2 represent the same watchpoint location. */
6224
6225static int
4a64f543
MS
6226watchpoint_locations_match (struct bp_location *loc1,
6227 struct bp_location *loc2)
2d134ed3 6228{
3a5c3e22
PA
6229 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6230 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6231
6232 /* Both of them must exist. */
6233 gdb_assert (w1 != NULL);
6234 gdb_assert (w2 != NULL);
2bdf28a0 6235
4a64f543
MS
6236 /* If the target can evaluate the condition expression in hardware,
6237 then we we need to insert both watchpoints even if they are at
6238 the same place. Otherwise the watchpoint will only trigger when
6239 the condition of whichever watchpoint was inserted evaluates to
6240 true, not giving a chance for GDB to check the condition of the
6241 other watchpoint. */
3a5c3e22 6242 if ((w1->cond_exp
4a64f543
MS
6243 && target_can_accel_watchpoint_condition (loc1->address,
6244 loc1->length,
0cf6dd15 6245 loc1->watchpoint_type,
3a5c3e22
PA
6246 w1->cond_exp))
6247 || (w2->cond_exp
4a64f543
MS
6248 && target_can_accel_watchpoint_condition (loc2->address,
6249 loc2->length,
0cf6dd15 6250 loc2->watchpoint_type,
3a5c3e22 6251 w2->cond_exp)))
0cf6dd15
TJB
6252 return 0;
6253
85d721b8
PA
6254 /* Note that this checks the owner's type, not the location's. In
6255 case the target does not support read watchpoints, but does
6256 support access watchpoints, we'll have bp_read_watchpoint
6257 watchpoints with hw_access locations. Those should be considered
6258 duplicates of hw_read locations. The hw_read locations will
6259 become hw_access locations later. */
2d134ed3
PA
6260 return (loc1->owner->type == loc2->owner->type
6261 && loc1->pspace->aspace == loc2->pspace->aspace
6262 && loc1->address == loc2->address
6263 && loc1->length == loc2->length);
e4f237da
KB
6264}
6265
6c95b8df
PA
6266/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6267 same breakpoint location. In most targets, this can only be true
6268 if ASPACE1 matches ASPACE2. On targets that have global
6269 breakpoints, the address space doesn't really matter. */
6270
6271static int
6272breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6273 struct address_space *aspace2, CORE_ADDR addr2)
6274{
6275 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6276 || aspace1 == aspace2)
6277 && addr1 == addr2);
6278}
6279
f1310107
TJB
6280/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6281 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6282 matches ASPACE2. On targets that have global breakpoints, the address
6283 space doesn't really matter. */
6284
6285static int
6286breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6287 int len1, struct address_space *aspace2,
6288 CORE_ADDR addr2)
6289{
6290 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6291 || aspace1 == aspace2)
6292 && addr2 >= addr1 && addr2 < addr1 + len1);
6293}
6294
6295/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6296 a ranged breakpoint. In most targets, a match happens only if ASPACE
6297 matches the breakpoint's address space. On targets that have global
6298 breakpoints, the address space doesn't really matter. */
6299
6300static int
6301breakpoint_location_address_match (struct bp_location *bl,
6302 struct address_space *aspace,
6303 CORE_ADDR addr)
6304{
6305 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6306 aspace, addr)
6307 || (bl->length
6308 && breakpoint_address_match_range (bl->pspace->aspace,
6309 bl->address, bl->length,
6310 aspace, addr)));
6311}
6312
1e4d1764
YQ
6313/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6314 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6315 true, otherwise returns false. */
6316
6317static int
6318tracepoint_locations_match (struct bp_location *loc1,
6319 struct bp_location *loc2)
6320{
6321 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6322 /* Since tracepoint locations are never duplicated with others', tracepoint
6323 locations at the same address of different tracepoints are regarded as
6324 different locations. */
6325 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6326 else
6327 return 0;
6328}
6329
2d134ed3
PA
6330/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6331 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6332 represent the same location. */
6333
6334static int
4a64f543
MS
6335breakpoint_locations_match (struct bp_location *loc1,
6336 struct bp_location *loc2)
2d134ed3 6337{
2bdf28a0
JK
6338 int hw_point1, hw_point2;
6339
6340 /* Both of them must not be in moribund_locations. */
6341 gdb_assert (loc1->owner != NULL);
6342 gdb_assert (loc2->owner != NULL);
6343
6344 hw_point1 = is_hardware_watchpoint (loc1->owner);
6345 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6346
6347 if (hw_point1 != hw_point2)
6348 return 0;
6349 else if (hw_point1)
6350 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6351 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6352 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6353 else
f1310107
TJB
6354 /* We compare bp_location.length in order to cover ranged breakpoints. */
6355 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6356 loc2->pspace->aspace, loc2->address)
6357 && loc1->length == loc2->length);
2d134ed3
PA
6358}
6359
76897487
KB
6360static void
6361breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6362 int bnum, int have_bnum)
6363{
f63fbe86
MS
6364 /* The longest string possibly returned by hex_string_custom
6365 is 50 chars. These must be at least that big for safety. */
6366 char astr1[64];
6367 char astr2[64];
76897487 6368
bb599908
PH
6369 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6370 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6371 if (have_bnum)
8a3fe4f8 6372 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6373 bnum, astr1, astr2);
6374 else
8a3fe4f8 6375 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6376}
6377
4a64f543
MS
6378/* Adjust a breakpoint's address to account for architectural
6379 constraints on breakpoint placement. Return the adjusted address.
6380 Note: Very few targets require this kind of adjustment. For most
6381 targets, this function is simply the identity function. */
76897487
KB
6382
6383static CORE_ADDR
a6d9a66e
UW
6384adjust_breakpoint_address (struct gdbarch *gdbarch,
6385 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6386{
a6d9a66e 6387 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6388 {
6389 /* Very few targets need any kind of breakpoint adjustment. */
6390 return bpaddr;
6391 }
88f7da05
KB
6392 else if (bptype == bp_watchpoint
6393 || bptype == bp_hardware_watchpoint
6394 || bptype == bp_read_watchpoint
6395 || bptype == bp_access_watchpoint
fe798b75 6396 || bptype == bp_catchpoint)
88f7da05
KB
6397 {
6398 /* Watchpoints and the various bp_catch_* eventpoints should not
6399 have their addresses modified. */
6400 return bpaddr;
6401 }
76897487
KB
6402 else
6403 {
6404 CORE_ADDR adjusted_bpaddr;
6405
6406 /* Some targets have architectural constraints on the placement
6407 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6408 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6409
6410 /* An adjusted breakpoint address can significantly alter
6411 a user's expectations. Print a warning if an adjustment
6412 is required. */
6413 if (adjusted_bpaddr != bpaddr)
6414 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6415
6416 return adjusted_bpaddr;
6417 }
6418}
6419
28010a5d
PA
6420void
6421init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6422 struct breakpoint *owner)
7cc221ef 6423{
7cc221ef
DJ
6424 memset (loc, 0, sizeof (*loc));
6425
348d480f
PA
6426 gdb_assert (ops != NULL);
6427
28010a5d
PA
6428 loc->ops = ops;
6429 loc->owner = owner;
511a6cd4 6430 loc->cond = NULL;
b775012e 6431 loc->cond_bytecode = NULL;
0d381245
VP
6432 loc->shlib_disabled = 0;
6433 loc->enabled = 1;
e049a4b5 6434
28010a5d 6435 switch (owner->type)
e049a4b5
DJ
6436 {
6437 case bp_breakpoint:
6438 case bp_until:
6439 case bp_finish:
6440 case bp_longjmp:
6441 case bp_longjmp_resume:
186c406b
TT
6442 case bp_exception:
6443 case bp_exception_resume:
e049a4b5 6444 case bp_step_resume:
2c03e5be 6445 case bp_hp_step_resume:
e049a4b5
DJ
6446 case bp_watchpoint_scope:
6447 case bp_call_dummy:
aa7d318d 6448 case bp_std_terminate:
e049a4b5
DJ
6449 case bp_shlib_event:
6450 case bp_thread_event:
6451 case bp_overlay_event:
4efc6507 6452 case bp_jit_event:
0fd8e87f 6453 case bp_longjmp_master:
aa7d318d 6454 case bp_std_terminate_master:
186c406b 6455 case bp_exception_master:
0e30163f
JK
6456 case bp_gnu_ifunc_resolver:
6457 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6458 case bp_dprintf:
e049a4b5 6459 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6460 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6461 break;
6462 case bp_hardware_breakpoint:
6463 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6464 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6465 break;
6466 case bp_hardware_watchpoint:
6467 case bp_read_watchpoint:
6468 case bp_access_watchpoint:
6469 loc->loc_type = bp_loc_hardware_watchpoint;
6470 break;
6471 case bp_watchpoint:
ce78b96d 6472 case bp_catchpoint:
15c3d785
PA
6473 case bp_tracepoint:
6474 case bp_fast_tracepoint:
0fb4aa4b 6475 case bp_static_tracepoint:
e049a4b5
DJ
6476 loc->loc_type = bp_loc_other;
6477 break;
6478 default:
e2e0b3e5 6479 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6480 }
6481
f431efe5 6482 loc->refc = 1;
28010a5d
PA
6483}
6484
6485/* Allocate a struct bp_location. */
6486
6487static struct bp_location *
6488allocate_bp_location (struct breakpoint *bpt)
6489{
348d480f
PA
6490 return bpt->ops->allocate_location (bpt);
6491}
7cc221ef 6492
f431efe5
PA
6493static void
6494free_bp_location (struct bp_location *loc)
fe3f5fa8 6495{
348d480f 6496 loc->ops->dtor (loc);
fe3f5fa8
VP
6497 xfree (loc);
6498}
6499
f431efe5
PA
6500/* Increment reference count. */
6501
6502static void
6503incref_bp_location (struct bp_location *bl)
6504{
6505 ++bl->refc;
6506}
6507
6508/* Decrement reference count. If the reference count reaches 0,
6509 destroy the bp_location. Sets *BLP to NULL. */
6510
6511static void
6512decref_bp_location (struct bp_location **blp)
6513{
0807b50c
PA
6514 gdb_assert ((*blp)->refc > 0);
6515
f431efe5
PA
6516 if (--(*blp)->refc == 0)
6517 free_bp_location (*blp);
6518 *blp = NULL;
6519}
6520
346774a9 6521/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6522
346774a9
PA
6523static void
6524add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6525{
346774a9 6526 struct breakpoint *b1;
c906108c 6527
346774a9
PA
6528 /* Add this breakpoint to the end of the chain so that a list of
6529 breakpoints will come out in order of increasing numbers. */
6530
6531 b1 = breakpoint_chain;
6532 if (b1 == 0)
6533 breakpoint_chain = b;
6534 else
6535 {
6536 while (b1->next)
6537 b1 = b1->next;
6538 b1->next = b;
6539 }
6540}
6541
6542/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6543
6544static void
6545init_raw_breakpoint_without_location (struct breakpoint *b,
6546 struct gdbarch *gdbarch,
28010a5d 6547 enum bptype bptype,
c0a91b2b 6548 const struct breakpoint_ops *ops)
346774a9 6549{
c906108c 6550 memset (b, 0, sizeof (*b));
2219d63c 6551
348d480f
PA
6552 gdb_assert (ops != NULL);
6553
28010a5d 6554 b->ops = ops;
4d28f7a8 6555 b->type = bptype;
a6d9a66e 6556 b->gdbarch = gdbarch;
c906108c
SS
6557 b->language = current_language->la_language;
6558 b->input_radix = input_radix;
6559 b->thread = -1;
b5de0fa7 6560 b->enable_state = bp_enabled;
c906108c
SS
6561 b->next = 0;
6562 b->silent = 0;
6563 b->ignore_count = 0;
6564 b->commands = NULL;
818dd999 6565 b->frame_id = null_frame_id;
0d381245 6566 b->condition_not_parsed = 0;
84f4c1fe 6567 b->py_bp_object = NULL;
d0fb5eae 6568 b->related_breakpoint = b;
346774a9
PA
6569}
6570
6571/* Helper to set_raw_breakpoint below. Creates a breakpoint
6572 that has type BPTYPE and has no locations as yet. */
346774a9
PA
6573
6574static struct breakpoint *
6575set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 6576 enum bptype bptype,
c0a91b2b 6577 const struct breakpoint_ops *ops)
346774a9
PA
6578{
6579 struct breakpoint *b = XNEW (struct breakpoint);
6580
348d480f 6581 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 6582 add_to_breakpoint_chain (b);
0d381245
VP
6583 return b;
6584}
6585
0e30163f
JK
6586/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6587 resolutions should be made as the user specified the location explicitly
6588 enough. */
6589
0d381245 6590static void
0e30163f 6591set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 6592{
2bdf28a0
JK
6593 gdb_assert (loc->owner != NULL);
6594
0d381245 6595 if (loc->owner->type == bp_breakpoint
1042e4c0 6596 || loc->owner->type == bp_hardware_breakpoint
d77f58be 6597 || is_tracepoint (loc->owner))
0d381245 6598 {
0e30163f 6599 int is_gnu_ifunc;
2c02bd72 6600 const char *function_name;
6a3a010b 6601 CORE_ADDR func_addr;
0e30163f 6602
2c02bd72 6603 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 6604 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
6605
6606 if (is_gnu_ifunc && !explicit_loc)
6607 {
6608 struct breakpoint *b = loc->owner;
6609
6610 gdb_assert (loc->pspace == current_program_space);
2c02bd72 6611 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
6612 &loc->requested_address))
6613 {
6614 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6615 loc->address = adjust_breakpoint_address (loc->gdbarch,
6616 loc->requested_address,
6617 b->type);
6618 }
6619 else if (b->type == bp_breakpoint && b->loc == loc
6620 && loc->next == NULL && b->related_breakpoint == b)
6621 {
6622 /* Create only the whole new breakpoint of this type but do not
6623 mess more complicated breakpoints with multiple locations. */
6624 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
6625 /* Remember the resolver's address for use by the return
6626 breakpoint. */
6627 loc->related_address = func_addr;
0e30163f
JK
6628 }
6629 }
6630
2c02bd72
DE
6631 if (function_name)
6632 loc->function_name = xstrdup (function_name);
0d381245
VP
6633 }
6634}
6635
a6d9a66e 6636/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 6637struct gdbarch *
a6d9a66e
UW
6638get_sal_arch (struct symtab_and_line sal)
6639{
6640 if (sal.section)
6641 return get_objfile_arch (sal.section->objfile);
6642 if (sal.symtab)
6643 return get_objfile_arch (sal.symtab->objfile);
6644
6645 return NULL;
6646}
6647
346774a9
PA
6648/* Low level routine for partially initializing a breakpoint of type
6649 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 6650 file name, and line number are provided by SAL.
0d381245
VP
6651
6652 It is expected that the caller will complete the initialization of
6653 the newly created breakpoint struct as well as output any status
c56053d2 6654 information regarding the creation of a new breakpoint. */
0d381245 6655
346774a9
PA
6656static void
6657init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 6658 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6659 const struct breakpoint_ops *ops)
0d381245 6660{
28010a5d 6661 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 6662
3742cc8b 6663 add_location_to_breakpoint (b, &sal);
0d381245 6664
6c95b8df
PA
6665 if (bptype != bp_catchpoint)
6666 gdb_assert (sal.pspace != NULL);
6667
f8eba3c6
TT
6668 /* Store the program space that was used to set the breakpoint,
6669 except for ordinary breakpoints, which are independent of the
6670 program space. */
6671 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6672 b->pspace = sal.pspace;
0d381245 6673
c906108c 6674 breakpoints_changed ();
346774a9 6675}
c906108c 6676
346774a9
PA
6677/* set_raw_breakpoint is a low level routine for allocating and
6678 partially initializing a breakpoint of type BPTYPE. The newly
6679 created breakpoint's address, section, source file name, and line
6680 number are provided by SAL. The newly created and partially
6681 initialized breakpoint is added to the breakpoint chain and
6682 is also returned as the value of this function.
6683
6684 It is expected that the caller will complete the initialization of
6685 the newly created breakpoint struct as well as output any status
6686 information regarding the creation of a new breakpoint. In
6687 particular, set_raw_breakpoint does NOT set the breakpoint
6688 number! Care should be taken to not allow an error to occur
6689 prior to completing the initialization of the breakpoint. If this
6690 should happen, a bogus breakpoint will be left on the chain. */
6691
6692struct breakpoint *
6693set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 6694 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6695 const struct breakpoint_ops *ops)
346774a9
PA
6696{
6697 struct breakpoint *b = XNEW (struct breakpoint);
6698
348d480f 6699 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 6700 add_to_breakpoint_chain (b);
c906108c
SS
6701 return b;
6702}
6703
c2c6d25f
JM
6704
6705/* Note that the breakpoint object B describes a permanent breakpoint
6706 instruction, hard-wired into the inferior's code. */
6707void
6708make_breakpoint_permanent (struct breakpoint *b)
6709{
0d381245 6710 struct bp_location *bl;
cc59ec59 6711
b5de0fa7 6712 b->enable_state = bp_permanent;
c2c6d25f 6713
4a64f543
MS
6714 /* By definition, permanent breakpoints are already present in the
6715 code. Mark all locations as inserted. For now,
6716 make_breakpoint_permanent is called in just one place, so it's
6717 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6718 multiple locations or not, but it's easy to implement. */
0d381245
VP
6719 for (bl = b->loc; bl; bl = bl->next)
6720 bl->inserted = 1;
c2c6d25f
JM
6721}
6722
53a5351d 6723/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6724 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6725 initiated the operation. */
c906108c
SS
6726
6727void
186c406b 6728set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6729{
35df4500 6730 struct breakpoint *b, *b_tmp;
186c406b 6731 int thread = tp->num;
0fd8e87f
UW
6732
6733 /* To avoid having to rescan all objfile symbols at every step,
6734 we maintain a list of continually-inserted but always disabled
6735 longjmp "master" breakpoints. Here, we simply create momentary
6736 clones of those and enable them for the requested thread. */
35df4500 6737 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6738 if (b->pspace == current_program_space
186c406b
TT
6739 && (b->type == bp_longjmp_master
6740 || b->type == bp_exception_master))
0fd8e87f 6741 {
06edf0c0
PA
6742 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6743 struct breakpoint *clone;
cc59ec59 6744
06edf0c0
PA
6745 clone = momentary_breakpoint_from_master (b, type,
6746 &momentary_breakpoint_ops);
0fd8e87f
UW
6747 clone->thread = thread;
6748 }
186c406b
TT
6749
6750 tp->initiating_frame = frame;
c906108c
SS
6751}
6752
611c83ae 6753/* Delete all longjmp breakpoints from THREAD. */
c906108c 6754void
611c83ae 6755delete_longjmp_breakpoint (int thread)
c906108c 6756{
35df4500 6757 struct breakpoint *b, *b_tmp;
c906108c 6758
35df4500 6759 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6760 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6761 {
6762 if (b->thread == thread)
6763 delete_breakpoint (b);
6764 }
c906108c
SS
6765}
6766
f59f708a
PA
6767void
6768delete_longjmp_breakpoint_at_next_stop (int thread)
6769{
6770 struct breakpoint *b, *b_tmp;
6771
6772 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6773 if (b->type == bp_longjmp || b->type == bp_exception)
6774 {
6775 if (b->thread == thread)
6776 b->disposition = disp_del_at_next_stop;
6777 }
6778}
6779
1900040c
MS
6780void
6781enable_overlay_breakpoints (void)
6782{
52f0bd74 6783 struct breakpoint *b;
1900040c
MS
6784
6785 ALL_BREAKPOINTS (b)
6786 if (b->type == bp_overlay_event)
6787 {
6788 b->enable_state = bp_enabled;
b60e7edf 6789 update_global_location_list (1);
c02f5703 6790 overlay_events_enabled = 1;
1900040c
MS
6791 }
6792}
6793
6794void
6795disable_overlay_breakpoints (void)
6796{
52f0bd74 6797 struct breakpoint *b;
1900040c
MS
6798
6799 ALL_BREAKPOINTS (b)
6800 if (b->type == bp_overlay_event)
6801 {
6802 b->enable_state = bp_disabled;
b60e7edf 6803 update_global_location_list (0);
c02f5703 6804 overlay_events_enabled = 0;
1900040c
MS
6805 }
6806}
6807
aa7d318d
TT
6808/* Set an active std::terminate breakpoint for each std::terminate
6809 master breakpoint. */
6810void
6811set_std_terminate_breakpoint (void)
6812{
35df4500 6813 struct breakpoint *b, *b_tmp;
aa7d318d 6814
35df4500 6815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6816 if (b->pspace == current_program_space
6817 && b->type == bp_std_terminate_master)
6818 {
06edf0c0
PA
6819 momentary_breakpoint_from_master (b, bp_std_terminate,
6820 &momentary_breakpoint_ops);
aa7d318d
TT
6821 }
6822}
6823
6824/* Delete all the std::terminate breakpoints. */
6825void
6826delete_std_terminate_breakpoint (void)
6827{
35df4500 6828 struct breakpoint *b, *b_tmp;
aa7d318d 6829
35df4500 6830 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6831 if (b->type == bp_std_terminate)
6832 delete_breakpoint (b);
6833}
6834
c4093a6a 6835struct breakpoint *
a6d9a66e 6836create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6837{
6838 struct breakpoint *b;
c4093a6a 6839
06edf0c0
PA
6840 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6841 &internal_breakpoint_ops);
6842
b5de0fa7 6843 b->enable_state = bp_enabled;
c4093a6a 6844 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6845 b->addr_string
6846 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6847
b60e7edf 6848 update_global_location_list_nothrow (1);
74960c60 6849
c4093a6a
JM
6850 return b;
6851}
6852
6853void
6854remove_thread_event_breakpoints (void)
6855{
35df4500 6856 struct breakpoint *b, *b_tmp;
c4093a6a 6857
35df4500 6858 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6859 if (b->type == bp_thread_event
6860 && b->loc->pspace == current_program_space)
c4093a6a
JM
6861 delete_breakpoint (b);
6862}
6863
0101ce28
JJ
6864struct lang_and_radix
6865 {
6866 enum language lang;
6867 int radix;
6868 };
6869
4efc6507
DE
6870/* Create a breakpoint for JIT code registration and unregistration. */
6871
6872struct breakpoint *
6873create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6874{
6875 struct breakpoint *b;
6876
06edf0c0
PA
6877 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6878 &internal_breakpoint_ops);
4efc6507
DE
6879 update_global_location_list_nothrow (1);
6880 return b;
6881}
0101ce28 6882
03673fc7
PP
6883/* Remove JIT code registration and unregistration breakpoint(s). */
6884
6885void
6886remove_jit_event_breakpoints (void)
6887{
6888 struct breakpoint *b, *b_tmp;
6889
6890 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6891 if (b->type == bp_jit_event
6892 && b->loc->pspace == current_program_space)
6893 delete_breakpoint (b);
6894}
6895
cae688ec
JJ
6896void
6897remove_solib_event_breakpoints (void)
6898{
35df4500 6899 struct breakpoint *b, *b_tmp;
cae688ec 6900
35df4500 6901 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6902 if (b->type == bp_shlib_event
6903 && b->loc->pspace == current_program_space)
cae688ec
JJ
6904 delete_breakpoint (b);
6905}
6906
6907struct breakpoint *
a6d9a66e 6908create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
6909{
6910 struct breakpoint *b;
6911
06edf0c0
PA
6912 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6913 &internal_breakpoint_ops);
b60e7edf 6914 update_global_location_list_nothrow (1);
cae688ec
JJ
6915 return b;
6916}
6917
6918/* Disable any breakpoints that are on code in shared libraries. Only
6919 apply to enabled breakpoints, disabled ones can just stay disabled. */
6920
6921void
cb851954 6922disable_breakpoints_in_shlibs (void)
cae688ec 6923{
876fa593 6924 struct bp_location *loc, **locp_tmp;
cae688ec 6925
876fa593 6926 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 6927 {
2bdf28a0 6928 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6929 struct breakpoint *b = loc->owner;
2bdf28a0 6930
4a64f543
MS
6931 /* We apply the check to all breakpoints, including disabled for
6932 those with loc->duplicate set. This is so that when breakpoint
6933 becomes enabled, or the duplicate is removed, gdb will try to
6934 insert all breakpoints. If we don't set shlib_disabled here,
6935 we'll try to insert those breakpoints and fail. */
1042e4c0 6936 if (((b->type == bp_breakpoint)
508ccb1f 6937 || (b->type == bp_jit_event)
1042e4c0 6938 || (b->type == bp_hardware_breakpoint)
d77f58be 6939 || (is_tracepoint (b)))
6c95b8df 6940 && loc->pspace == current_program_space
0d381245 6941 && !loc->shlib_disabled
a77053c2 6942#ifdef PC_SOLIB
0d381245 6943 && PC_SOLIB (loc->address)
a77053c2 6944#else
6c95b8df 6945 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
6946#endif
6947 )
0d381245
VP
6948 {
6949 loc->shlib_disabled = 1;
6950 }
cae688ec
JJ
6951 }
6952}
6953
1e4d1764
YQ
6954/* Disable any breakpoints and tracepoints that are in an unloaded shared
6955 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 6956 disabled. */
84acb35a 6957
75149521 6958static void
84acb35a
JJ
6959disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
6960{
876fa593 6961 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
6962 int disabled_shlib_breaks = 0;
6963
c86cf029
VP
6964 /* SunOS a.out shared libraries are always mapped, so do not
6965 disable breakpoints; they will only be reported as unloaded
6966 through clear_solib when GDB discards its shared library
6967 list. See clear_solib for more information. */
6968 if (exec_bfd != NULL
6969 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
6970 return;
6971
876fa593 6972 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 6973 {
2bdf28a0 6974 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 6975 struct breakpoint *b = loc->owner;
cc59ec59 6976
1e4d1764 6977 if (solib->pspace == loc->pspace
e2dd7057 6978 && !loc->shlib_disabled
1e4d1764
YQ
6979 && (((b->type == bp_breakpoint
6980 || b->type == bp_jit_event
6981 || b->type == bp_hardware_breakpoint)
6982 && (loc->loc_type == bp_loc_hardware_breakpoint
6983 || loc->loc_type == bp_loc_software_breakpoint))
6984 || is_tracepoint (b))
e2dd7057 6985 && solib_contains_address_p (solib, loc->address))
84acb35a 6986 {
e2dd7057
PP
6987 loc->shlib_disabled = 1;
6988 /* At this point, we cannot rely on remove_breakpoint
6989 succeeding so we must mark the breakpoint as not inserted
6990 to prevent future errors occurring in remove_breakpoints. */
6991 loc->inserted = 0;
8d3788bd
VP
6992
6993 /* This may cause duplicate notifications for the same breakpoint. */
6994 observer_notify_breakpoint_modified (b);
6995
e2dd7057
PP
6996 if (!disabled_shlib_breaks)
6997 {
6998 target_terminal_ours_for_output ();
3e43a32a
MS
6999 warning (_("Temporarily disabling breakpoints "
7000 "for unloaded shared library \"%s\""),
e2dd7057 7001 solib->so_name);
84acb35a 7002 }
e2dd7057 7003 disabled_shlib_breaks = 1;
84acb35a
JJ
7004 }
7005 }
84acb35a
JJ
7006}
7007
ce78b96d
JB
7008/* FORK & VFORK catchpoints. */
7009
e29a4733
PA
7010/* An instance of this type is used to represent a fork or vfork
7011 catchpoint. It includes a "struct breakpoint" as a kind of base
7012 class; users downcast to "struct breakpoint *" when needed. A
7013 breakpoint is really of this type iff its ops pointer points to
7014 CATCH_FORK_BREAKPOINT_OPS. */
7015
7016struct fork_catchpoint
7017{
7018 /* The base class. */
7019 struct breakpoint base;
7020
7021 /* Process id of a child process whose forking triggered this
7022 catchpoint. This field is only valid immediately after this
7023 catchpoint has triggered. */
7024 ptid_t forked_inferior_pid;
7025};
7026
4a64f543
MS
7027/* Implement the "insert" breakpoint_ops method for fork
7028 catchpoints. */
ce78b96d 7029
77b06cd7
TJB
7030static int
7031insert_catch_fork (struct bp_location *bl)
ce78b96d 7032{
77b06cd7 7033 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7034}
7035
4a64f543
MS
7036/* Implement the "remove" breakpoint_ops method for fork
7037 catchpoints. */
ce78b96d
JB
7038
7039static int
77b06cd7 7040remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7041{
7042 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7043}
7044
7045/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7046 catchpoints. */
7047
7048static int
f1310107 7049breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7050 struct address_space *aspace, CORE_ADDR bp_addr,
7051 const struct target_waitstatus *ws)
ce78b96d 7052{
e29a4733
PA
7053 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7054
f90263c1
TT
7055 if (ws->kind != TARGET_WAITKIND_FORKED)
7056 return 0;
7057
7058 c->forked_inferior_pid = ws->value.related_pid;
7059 return 1;
ce78b96d
JB
7060}
7061
4a64f543
MS
7062/* Implement the "print_it" breakpoint_ops method for fork
7063 catchpoints. */
ce78b96d
JB
7064
7065static enum print_stop_action
348d480f 7066print_it_catch_fork (bpstat bs)
ce78b96d 7067{
36dfb11c 7068 struct ui_out *uiout = current_uiout;
348d480f
PA
7069 struct breakpoint *b = bs->breakpoint_at;
7070 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7071
ce78b96d 7072 annotate_catchpoint (b->number);
36dfb11c
TT
7073 if (b->disposition == disp_del)
7074 ui_out_text (uiout, "\nTemporary catchpoint ");
7075 else
7076 ui_out_text (uiout, "\nCatchpoint ");
7077 if (ui_out_is_mi_like_p (uiout))
7078 {
7079 ui_out_field_string (uiout, "reason",
7080 async_reason_lookup (EXEC_ASYNC_FORK));
7081 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7082 }
7083 ui_out_field_int (uiout, "bkptno", b->number);
7084 ui_out_text (uiout, " (forked process ");
7085 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7086 ui_out_text (uiout, "), ");
ce78b96d
JB
7087 return PRINT_SRC_AND_LOC;
7088}
7089
4a64f543
MS
7090/* Implement the "print_one" breakpoint_ops method for fork
7091 catchpoints. */
ce78b96d
JB
7092
7093static void
a6d9a66e 7094print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7095{
e29a4733 7096 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7097 struct value_print_options opts;
79a45e25 7098 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7099
7100 get_user_print_options (&opts);
7101
4a64f543
MS
7102 /* Field 4, the address, is omitted (which makes the columns not
7103 line up too nicely with the headers, but the effect is relatively
7104 readable). */
79a45b7d 7105 if (opts.addressprint)
ce78b96d
JB
7106 ui_out_field_skip (uiout, "addr");
7107 annotate_field (5);
7108 ui_out_text (uiout, "fork");
e29a4733 7109 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7110 {
7111 ui_out_text (uiout, ", process ");
7112 ui_out_field_int (uiout, "what",
e29a4733 7113 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7114 ui_out_spaces (uiout, 1);
7115 }
7116}
7117
7118/* Implement the "print_mention" breakpoint_ops method for fork
7119 catchpoints. */
7120
7121static void
7122print_mention_catch_fork (struct breakpoint *b)
7123{
7124 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7125}
7126
6149aea9
PA
7127/* Implement the "print_recreate" breakpoint_ops method for fork
7128 catchpoints. */
7129
7130static void
7131print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7132{
7133 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7134 print_recreate_thread (b, fp);
6149aea9
PA
7135}
7136
ce78b96d
JB
7137/* The breakpoint_ops structure to be used in fork catchpoints. */
7138
2060206e 7139static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7140
4a64f543
MS
7141/* Implement the "insert" breakpoint_ops method for vfork
7142 catchpoints. */
ce78b96d 7143
77b06cd7
TJB
7144static int
7145insert_catch_vfork (struct bp_location *bl)
ce78b96d 7146{
77b06cd7 7147 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7148}
7149
4a64f543
MS
7150/* Implement the "remove" breakpoint_ops method for vfork
7151 catchpoints. */
ce78b96d
JB
7152
7153static int
77b06cd7 7154remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7155{
7156 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7157}
7158
7159/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7160 catchpoints. */
7161
7162static int
f1310107 7163breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7164 struct address_space *aspace, CORE_ADDR bp_addr,
7165 const struct target_waitstatus *ws)
ce78b96d 7166{
e29a4733
PA
7167 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7168
f90263c1
TT
7169 if (ws->kind != TARGET_WAITKIND_VFORKED)
7170 return 0;
7171
7172 c->forked_inferior_pid = ws->value.related_pid;
7173 return 1;
ce78b96d
JB
7174}
7175
4a64f543
MS
7176/* Implement the "print_it" breakpoint_ops method for vfork
7177 catchpoints. */
ce78b96d
JB
7178
7179static enum print_stop_action
348d480f 7180print_it_catch_vfork (bpstat bs)
ce78b96d 7181{
36dfb11c 7182 struct ui_out *uiout = current_uiout;
348d480f 7183 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7184 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7185
ce78b96d 7186 annotate_catchpoint (b->number);
36dfb11c
TT
7187 if (b->disposition == disp_del)
7188 ui_out_text (uiout, "\nTemporary catchpoint ");
7189 else
7190 ui_out_text (uiout, "\nCatchpoint ");
7191 if (ui_out_is_mi_like_p (uiout))
7192 {
7193 ui_out_field_string (uiout, "reason",
7194 async_reason_lookup (EXEC_ASYNC_VFORK));
7195 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7196 }
7197 ui_out_field_int (uiout, "bkptno", b->number);
7198 ui_out_text (uiout, " (vforked process ");
7199 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7200 ui_out_text (uiout, "), ");
ce78b96d
JB
7201 return PRINT_SRC_AND_LOC;
7202}
7203
4a64f543
MS
7204/* Implement the "print_one" breakpoint_ops method for vfork
7205 catchpoints. */
ce78b96d
JB
7206
7207static void
a6d9a66e 7208print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7209{
e29a4733 7210 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7211 struct value_print_options opts;
79a45e25 7212 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7213
7214 get_user_print_options (&opts);
4a64f543
MS
7215 /* Field 4, the address, is omitted (which makes the columns not
7216 line up too nicely with the headers, but the effect is relatively
7217 readable). */
79a45b7d 7218 if (opts.addressprint)
ce78b96d
JB
7219 ui_out_field_skip (uiout, "addr");
7220 annotate_field (5);
7221 ui_out_text (uiout, "vfork");
e29a4733 7222 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7223 {
7224 ui_out_text (uiout, ", process ");
7225 ui_out_field_int (uiout, "what",
e29a4733 7226 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7227 ui_out_spaces (uiout, 1);
7228 }
7229}
7230
7231/* Implement the "print_mention" breakpoint_ops method for vfork
7232 catchpoints. */
7233
7234static void
7235print_mention_catch_vfork (struct breakpoint *b)
7236{
7237 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7238}
7239
6149aea9
PA
7240/* Implement the "print_recreate" breakpoint_ops method for vfork
7241 catchpoints. */
7242
7243static void
7244print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7245{
7246 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7247 print_recreate_thread (b, fp);
6149aea9
PA
7248}
7249
ce78b96d
JB
7250/* The breakpoint_ops structure to be used in vfork catchpoints. */
7251
2060206e 7252static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7253
edcc5120
TT
7254/* An instance of this type is used to represent an solib catchpoint.
7255 It includes a "struct breakpoint" as a kind of base class; users
7256 downcast to "struct breakpoint *" when needed. A breakpoint is
7257 really of this type iff its ops pointer points to
7258 CATCH_SOLIB_BREAKPOINT_OPS. */
7259
7260struct solib_catchpoint
7261{
7262 /* The base class. */
7263 struct breakpoint base;
7264
7265 /* True for "catch load", false for "catch unload". */
7266 unsigned char is_load;
7267
7268 /* Regular expression to match, if any. COMPILED is only valid when
7269 REGEX is non-NULL. */
7270 char *regex;
7271 regex_t compiled;
7272};
7273
7274static void
7275dtor_catch_solib (struct breakpoint *b)
7276{
7277 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7278
7279 if (self->regex)
7280 regfree (&self->compiled);
7281 xfree (self->regex);
7282
7283 base_breakpoint_ops.dtor (b);
7284}
7285
7286static int
7287insert_catch_solib (struct bp_location *ignore)
7288{
7289 return 0;
7290}
7291
7292static int
7293remove_catch_solib (struct bp_location *ignore)
7294{
7295 return 0;
7296}
7297
7298static int
7299breakpoint_hit_catch_solib (const struct bp_location *bl,
7300 struct address_space *aspace,
7301 CORE_ADDR bp_addr,
7302 const struct target_waitstatus *ws)
7303{
7304 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7305 struct breakpoint *other;
7306
7307 if (ws->kind == TARGET_WAITKIND_LOADED)
7308 return 1;
7309
7310 ALL_BREAKPOINTS (other)
7311 {
7312 struct bp_location *other_bl;
7313
7314 if (other == bl->owner)
7315 continue;
7316
7317 if (other->type != bp_shlib_event)
7318 continue;
7319
7320 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7321 continue;
7322
7323 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7324 {
7325 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7326 return 1;
7327 }
7328 }
7329
7330 return 0;
7331}
7332
7333static void
7334check_status_catch_solib (struct bpstats *bs)
7335{
7336 struct solib_catchpoint *self
7337 = (struct solib_catchpoint *) bs->breakpoint_at;
7338 int ix;
7339
7340 if (self->is_load)
7341 {
7342 struct so_list *iter;
7343
7344 for (ix = 0;
7345 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7346 ix, iter);
7347 ++ix)
7348 {
7349 if (!self->regex
7350 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7351 return;
7352 }
7353 }
7354 else
7355 {
7356 char *iter;
7357
7358 for (ix = 0;
7359 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7360 ix, iter);
7361 ++ix)
7362 {
7363 if (!self->regex
7364 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7365 return;
7366 }
7367 }
7368
7369 bs->stop = 0;
7370 bs->print_it = print_it_noop;
7371}
7372
7373static enum print_stop_action
7374print_it_catch_solib (bpstat bs)
7375{
7376 struct breakpoint *b = bs->breakpoint_at;
7377 struct ui_out *uiout = current_uiout;
7378
7379 annotate_catchpoint (b->number);
7380 if (b->disposition == disp_del)
7381 ui_out_text (uiout, "\nTemporary catchpoint ");
7382 else
7383 ui_out_text (uiout, "\nCatchpoint ");
7384 ui_out_field_int (uiout, "bkptno", b->number);
7385 ui_out_text (uiout, "\n");
7386 if (ui_out_is_mi_like_p (uiout))
7387 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7388 print_solib_event (1);
7389 return PRINT_SRC_AND_LOC;
7390}
7391
7392static void
7393print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7394{
7395 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7396 struct value_print_options opts;
7397 struct ui_out *uiout = current_uiout;
7398 char *msg;
7399
7400 get_user_print_options (&opts);
7401 /* Field 4, the address, is omitted (which makes the columns not
7402 line up too nicely with the headers, but the effect is relatively
7403 readable). */
7404 if (opts.addressprint)
7405 {
7406 annotate_field (4);
7407 ui_out_field_skip (uiout, "addr");
7408 }
7409
7410 annotate_field (5);
7411 if (self->is_load)
7412 {
7413 if (self->regex)
7414 msg = xstrprintf (_("load of library matching %s"), self->regex);
7415 else
7416 msg = xstrdup (_("load of library"));
7417 }
7418 else
7419 {
7420 if (self->regex)
7421 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7422 else
7423 msg = xstrdup (_("unload of library"));
7424 }
7425 ui_out_field_string (uiout, "what", msg);
7426 xfree (msg);
7427}
7428
7429static void
7430print_mention_catch_solib (struct breakpoint *b)
7431{
7432 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7433
7434 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7435 self->is_load ? "load" : "unload");
7436}
7437
7438static void
7439print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7440{
7441 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7442
7443 fprintf_unfiltered (fp, "%s %s",
7444 b->disposition == disp_del ? "tcatch" : "catch",
7445 self->is_load ? "load" : "unload");
7446 if (self->regex)
7447 fprintf_unfiltered (fp, " %s", self->regex);
7448 fprintf_unfiltered (fp, "\n");
7449}
7450
7451static struct breakpoint_ops catch_solib_breakpoint_ops;
7452
7453/* A helper function that does all the work for "catch load" and
7454 "catch unload". */
7455
7456static void
7457catch_load_or_unload (char *arg, int from_tty, int is_load,
7458 struct cmd_list_element *command)
7459{
7460 struct solib_catchpoint *c;
7461 struct gdbarch *gdbarch = get_current_arch ();
7462 int tempflag;
7463 regex_t compiled;
7464 struct cleanup *cleanup;
7465
7466 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7467
7468 if (!arg)
7469 arg = "";
7470 arg = skip_spaces (arg);
7471
7472 c = XCNEW (struct solib_catchpoint);
7473 cleanup = make_cleanup (xfree, c);
7474
7475 if (*arg != '\0')
7476 {
7477 int errcode;
7478
7479 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7480 if (errcode != 0)
7481 {
7482 char *err = get_regcomp_error (errcode, &c->compiled);
7483
7484 make_cleanup (xfree, err);
7485 error (_("Invalid regexp (%s): %s"), err, arg);
7486 }
7487 c->regex = xstrdup (arg);
7488 }
7489
7490 c->is_load = is_load;
7491 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7492 &catch_solib_breakpoint_ops);
7493
7494 discard_cleanups (cleanup);
7495 install_breakpoint (0, &c->base, 1);
7496}
7497
7498static void
7499catch_load_command_1 (char *arg, int from_tty,
7500 struct cmd_list_element *command)
7501{
7502 catch_load_or_unload (arg, from_tty, 1, command);
7503}
7504
7505static void
7506catch_unload_command_1 (char *arg, int from_tty,
7507 struct cmd_list_element *command)
7508{
7509 catch_load_or_unload (arg, from_tty, 0, command);
7510}
7511
fa3064dd
YQ
7512DEF_VEC_I(int);
7513
be5c67c1
PA
7514/* An instance of this type is used to represent a syscall catchpoint.
7515 It includes a "struct breakpoint" as a kind of base class; users
7516 downcast to "struct breakpoint *" when needed. A breakpoint is
7517 really of this type iff its ops pointer points to
7518 CATCH_SYSCALL_BREAKPOINT_OPS. */
7519
7520struct syscall_catchpoint
7521{
7522 /* The base class. */
7523 struct breakpoint base;
7524
7525 /* Syscall numbers used for the 'catch syscall' feature. If no
7526 syscall has been specified for filtering, its value is NULL.
7527 Otherwise, it holds a list of all syscalls to be caught. The
7528 list elements are allocated with xmalloc. */
7529 VEC(int) *syscalls_to_be_caught;
7530};
7531
7532/* Implement the "dtor" breakpoint_ops method for syscall
7533 catchpoints. */
7534
7535static void
7536dtor_catch_syscall (struct breakpoint *b)
7537{
7538 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7539
7540 VEC_free (int, c->syscalls_to_be_caught);
348d480f 7541
2060206e 7542 base_breakpoint_ops.dtor (b);
be5c67c1
PA
7543}
7544
fa3064dd
YQ
7545static const struct inferior_data *catch_syscall_inferior_data = NULL;
7546
7547struct catch_syscall_inferior_data
7548{
7549 /* We keep a count of the number of times the user has requested a
7550 particular syscall to be tracked, and pass this information to the
7551 target. This lets capable targets implement filtering directly. */
7552
7553 /* Number of times that "any" syscall is requested. */
7554 int any_syscall_count;
7555
7556 /* Count of each system call. */
7557 VEC(int) *syscalls_counts;
7558
7559 /* This counts all syscall catch requests, so we can readily determine
7560 if any catching is necessary. */
7561 int total_syscalls_count;
7562};
7563
7564static struct catch_syscall_inferior_data*
7565get_catch_syscall_inferior_data (struct inferior *inf)
7566{
7567 struct catch_syscall_inferior_data *inf_data;
7568
7569 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7570 if (inf_data == NULL)
7571 {
7572 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7573 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7574 }
7575
7576 return inf_data;
7577}
7578
7579static void
7580catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7581{
7582 xfree (arg);
7583}
7584
7585
a96d9b2e
SDJ
7586/* Implement the "insert" breakpoint_ops method for syscall
7587 catchpoints. */
7588
77b06cd7
TJB
7589static int
7590insert_catch_syscall (struct bp_location *bl)
a96d9b2e 7591{
be5c67c1 7592 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7593 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7594 struct catch_syscall_inferior_data *inf_data
7595 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7596
fa3064dd 7597 ++inf_data->total_syscalls_count;
be5c67c1 7598 if (!c->syscalls_to_be_caught)
fa3064dd 7599 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
7600 else
7601 {
7602 int i, iter;
cc59ec59 7603
a96d9b2e 7604 for (i = 0;
be5c67c1 7605 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7606 i++)
7607 {
7608 int elem;
cc59ec59 7609
fa3064dd 7610 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 7611 {
fa3064dd 7612 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
7613 uintptr_t vec_addr_offset
7614 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 7615 uintptr_t vec_addr;
fa3064dd
YQ
7616 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7617 vec_addr = ((uintptr_t) VEC_address (int,
7618 inf_data->syscalls_counts)
7619 + vec_addr_offset);
a96d9b2e
SDJ
7620 memset ((void *) vec_addr, 0,
7621 (iter + 1 - old_size) * sizeof (int));
7622 }
fa3064dd
YQ
7623 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7624 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
7625 }
7626 }
7627
77b06cd7 7628 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7629 inf_data->total_syscalls_count != 0,
7630 inf_data->any_syscall_count,
7631 VEC_length (int,
7632 inf_data->syscalls_counts),
7633 VEC_address (int,
7634 inf_data->syscalls_counts));
a96d9b2e
SDJ
7635}
7636
7637/* Implement the "remove" breakpoint_ops method for syscall
7638 catchpoints. */
7639
7640static int
77b06cd7 7641remove_catch_syscall (struct bp_location *bl)
a96d9b2e 7642{
be5c67c1 7643 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7644 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7645 struct catch_syscall_inferior_data *inf_data
7646 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7647
fa3064dd 7648 --inf_data->total_syscalls_count;
be5c67c1 7649 if (!c->syscalls_to_be_caught)
fa3064dd 7650 --inf_data->any_syscall_count;
a96d9b2e
SDJ
7651 else
7652 {
7653 int i, iter;
cc59ec59 7654
a96d9b2e 7655 for (i = 0;
be5c67c1 7656 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7657 i++)
7658 {
7659 int elem;
fa3064dd 7660 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
7661 /* Shouldn't happen. */
7662 continue;
fa3064dd
YQ
7663 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7664 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
7665 }
7666 }
7667
7668 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7669 inf_data->total_syscalls_count != 0,
7670 inf_data->any_syscall_count,
7671 VEC_length (int,
7672 inf_data->syscalls_counts),
3e43a32a 7673 VEC_address (int,
fa3064dd 7674 inf_data->syscalls_counts));
a96d9b2e
SDJ
7675}
7676
7677/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7678 catchpoints. */
7679
7680static int
f1310107 7681breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
7682 struct address_space *aspace, CORE_ADDR bp_addr,
7683 const struct target_waitstatus *ws)
a96d9b2e 7684{
4a64f543
MS
7685 /* We must check if we are catching specific syscalls in this
7686 breakpoint. If we are, then we must guarantee that the called
7687 syscall is the same syscall we are catching. */
a96d9b2e 7688 int syscall_number = 0;
be5c67c1
PA
7689 const struct syscall_catchpoint *c
7690 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 7691
f90263c1
TT
7692 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7693 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
7694 return 0;
7695
f90263c1
TT
7696 syscall_number = ws->value.syscall_number;
7697
a96d9b2e 7698 /* Now, checking if the syscall is the same. */
be5c67c1 7699 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7700 {
7701 int i, iter;
cc59ec59 7702
a96d9b2e 7703 for (i = 0;
be5c67c1 7704 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7705 i++)
7706 if (syscall_number == iter)
7707 break;
7708 /* Not the same. */
7709 if (!iter)
7710 return 0;
7711 }
7712
7713 return 1;
7714}
7715
7716/* Implement the "print_it" breakpoint_ops method for syscall
7717 catchpoints. */
7718
7719static enum print_stop_action
348d480f 7720print_it_catch_syscall (bpstat bs)
a96d9b2e 7721{
36dfb11c 7722 struct ui_out *uiout = current_uiout;
348d480f 7723 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
7724 /* These are needed because we want to know in which state a
7725 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7726 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7727 must print "called syscall" or "returned from syscall". */
7728 ptid_t ptid;
7729 struct target_waitstatus last;
7730 struct syscall s;
a96d9b2e
SDJ
7731 char *syscall_id;
7732
7733 get_last_target_status (&ptid, &last);
7734
7735 get_syscall_by_number (last.value.syscall_number, &s);
7736
7737 annotate_catchpoint (b->number);
7738
36dfb11c
TT
7739 if (b->disposition == disp_del)
7740 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 7741 else
36dfb11c
TT
7742 ui_out_text (uiout, "\nCatchpoint ");
7743 if (ui_out_is_mi_like_p (uiout))
7744 {
7745 ui_out_field_string (uiout, "reason",
7746 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7747 ? EXEC_ASYNC_SYSCALL_ENTRY
7748 : EXEC_ASYNC_SYSCALL_RETURN));
7749 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7750 }
7751 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
7752
7753 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
7754 ui_out_text (uiout, " (call to syscall ");
7755 else
7756 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 7757
36dfb11c
TT
7758 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7759 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7760 if (s.name != NULL)
7761 ui_out_field_string (uiout, "syscall-name", s.name);
7762
7763 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
7764
7765 return PRINT_SRC_AND_LOC;
7766}
7767
7768/* Implement the "print_one" breakpoint_ops method for syscall
7769 catchpoints. */
7770
7771static void
7772print_one_catch_syscall (struct breakpoint *b,
f1310107 7773 struct bp_location **last_loc)
a96d9b2e 7774{
be5c67c1 7775 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7776 struct value_print_options opts;
79a45e25 7777 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7778
7779 get_user_print_options (&opts);
4a64f543
MS
7780 /* Field 4, the address, is omitted (which makes the columns not
7781 line up too nicely with the headers, but the effect is relatively
7782 readable). */
a96d9b2e
SDJ
7783 if (opts.addressprint)
7784 ui_out_field_skip (uiout, "addr");
7785 annotate_field (5);
7786
be5c67c1
PA
7787 if (c->syscalls_to_be_caught
7788 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7789 ui_out_text (uiout, "syscalls \"");
7790 else
7791 ui_out_text (uiout, "syscall \"");
7792
be5c67c1 7793 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7794 {
7795 int i, iter;
7796 char *text = xstrprintf ("%s", "");
cc59ec59 7797
a96d9b2e 7798 for (i = 0;
be5c67c1 7799 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7800 i++)
7801 {
7802 char *x = text;
7803 struct syscall s;
7804 get_syscall_by_number (iter, &s);
7805
7806 if (s.name != NULL)
7807 text = xstrprintf ("%s%s, ", text, s.name);
7808 else
7809 text = xstrprintf ("%s%d, ", text, iter);
7810
7811 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7812 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7813 on every call. */
7814 xfree (x);
7815 }
7816 /* Remove the last comma. */
7817 text[strlen (text) - 2] = '\0';
7818 ui_out_field_string (uiout, "what", text);
7819 }
7820 else
7821 ui_out_field_string (uiout, "what", "<any syscall>");
7822 ui_out_text (uiout, "\" ");
7823}
7824
7825/* Implement the "print_mention" breakpoint_ops method for syscall
7826 catchpoints. */
7827
7828static void
7829print_mention_catch_syscall (struct breakpoint *b)
7830{
be5c67c1
PA
7831 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7832
7833 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7834 {
7835 int i, iter;
7836
be5c67c1 7837 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7838 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7839 else
7840 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7841
7842 for (i = 0;
be5c67c1 7843 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7844 i++)
7845 {
7846 struct syscall s;
7847 get_syscall_by_number (iter, &s);
7848
7849 if (s.name)
7850 printf_filtered (" '%s' [%d]", s.name, s.number);
7851 else
7852 printf_filtered (" %d", s.number);
7853 }
7854 printf_filtered (")");
7855 }
7856 else
7857 printf_filtered (_("Catchpoint %d (any syscall)"),
7858 b->number);
7859}
7860
6149aea9
PA
7861/* Implement the "print_recreate" breakpoint_ops method for syscall
7862 catchpoints. */
7863
7864static void
7865print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7866{
be5c67c1
PA
7867 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7868
6149aea9
PA
7869 fprintf_unfiltered (fp, "catch syscall");
7870
be5c67c1 7871 if (c->syscalls_to_be_caught)
6149aea9
PA
7872 {
7873 int i, iter;
7874
7875 for (i = 0;
be5c67c1 7876 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
7877 i++)
7878 {
7879 struct syscall s;
7880
7881 get_syscall_by_number (iter, &s);
7882 if (s.name)
7883 fprintf_unfiltered (fp, " %s", s.name);
7884 else
7885 fprintf_unfiltered (fp, " %d", s.number);
7886 }
7887 }
d9b3f62e 7888 print_recreate_thread (b, fp);
6149aea9
PA
7889}
7890
a96d9b2e
SDJ
7891/* The breakpoint_ops structure to be used in syscall catchpoints. */
7892
2060206e 7893static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
7894
7895/* Returns non-zero if 'b' is a syscall catchpoint. */
7896
7897static int
7898syscall_catchpoint_p (struct breakpoint *b)
7899{
7900 return (b->ops == &catch_syscall_breakpoint_ops);
7901}
7902
346774a9
PA
7903/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
7904 is non-zero, then make the breakpoint temporary. If COND_STRING is
7905 not NULL, then store it in the breakpoint. OPS, if not NULL, is
7906 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 7907
346774a9
PA
7908static void
7909init_catchpoint (struct breakpoint *b,
7910 struct gdbarch *gdbarch, int tempflag,
7911 char *cond_string,
c0a91b2b 7912 const struct breakpoint_ops *ops)
c906108c 7913{
c5aa993b 7914 struct symtab_and_line sal;
346774a9 7915
fe39c653 7916 init_sal (&sal);
6c95b8df 7917 sal.pspace = current_program_space;
c5aa993b 7918
28010a5d 7919 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 7920
1b36a34b 7921 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 7922 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
7923}
7924
28010a5d 7925void
3ea46bff 7926install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
7927{
7928 add_to_breakpoint_chain (b);
3a5c3e22
PA
7929 set_breakpoint_number (internal, b);
7930 if (!internal)
7931 mention (b);
c56053d2 7932 observer_notify_breakpoint_created (b);
3ea46bff
YQ
7933
7934 if (update_gll)
7935 update_global_location_list (1);
c56053d2
PA
7936}
7937
9b70b993 7938static void
a6d9a66e
UW
7939create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7940 int tempflag, char *cond_string,
c0a91b2b 7941 const struct breakpoint_ops *ops)
c906108c 7942{
e29a4733 7943 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 7944
e29a4733
PA
7945 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7946
7947 c->forked_inferior_pid = null_ptid;
7948
3ea46bff 7949 install_breakpoint (0, &c->base, 1);
c906108c
SS
7950}
7951
fe798b75
JB
7952/* Exec catchpoints. */
7953
b4d90040
PA
7954/* An instance of this type is used to represent an exec catchpoint.
7955 It includes a "struct breakpoint" as a kind of base class; users
7956 downcast to "struct breakpoint *" when needed. A breakpoint is
7957 really of this type iff its ops pointer points to
7958 CATCH_EXEC_BREAKPOINT_OPS. */
7959
7960struct exec_catchpoint
7961{
7962 /* The base class. */
7963 struct breakpoint base;
7964
7965 /* Filename of a program whose exec triggered this catchpoint.
7966 This field is only valid immediately after this catchpoint has
7967 triggered. */
7968 char *exec_pathname;
7969};
7970
7971/* Implement the "dtor" breakpoint_ops method for exec
7972 catchpoints. */
7973
7974static void
7975dtor_catch_exec (struct breakpoint *b)
7976{
7977 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
7978
7979 xfree (c->exec_pathname);
348d480f 7980
2060206e 7981 base_breakpoint_ops.dtor (b);
b4d90040
PA
7982}
7983
77b06cd7
TJB
7984static int
7985insert_catch_exec (struct bp_location *bl)
c906108c 7986{
77b06cd7 7987 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 7988}
c906108c 7989
fe798b75 7990static int
77b06cd7 7991remove_catch_exec (struct bp_location *bl)
fe798b75
JB
7992{
7993 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
7994}
c906108c 7995
fe798b75 7996static int
f1310107 7997breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
7998 struct address_space *aspace, CORE_ADDR bp_addr,
7999 const struct target_waitstatus *ws)
fe798b75 8000{
b4d90040
PA
8001 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8002
f90263c1
TT
8003 if (ws->kind != TARGET_WAITKIND_EXECD)
8004 return 0;
8005
8006 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8007 return 1;
fe798b75 8008}
c906108c 8009
fe798b75 8010static enum print_stop_action
348d480f 8011print_it_catch_exec (bpstat bs)
fe798b75 8012{
36dfb11c 8013 struct ui_out *uiout = current_uiout;
348d480f 8014 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8015 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8016
fe798b75 8017 annotate_catchpoint (b->number);
36dfb11c
TT
8018 if (b->disposition == disp_del)
8019 ui_out_text (uiout, "\nTemporary catchpoint ");
8020 else
8021 ui_out_text (uiout, "\nCatchpoint ");
8022 if (ui_out_is_mi_like_p (uiout))
8023 {
8024 ui_out_field_string (uiout, "reason",
8025 async_reason_lookup (EXEC_ASYNC_EXEC));
8026 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8027 }
8028 ui_out_field_int (uiout, "bkptno", b->number);
8029 ui_out_text (uiout, " (exec'd ");
8030 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8031 ui_out_text (uiout, "), ");
8032
fe798b75 8033 return PRINT_SRC_AND_LOC;
c906108c
SS
8034}
8035
fe798b75 8036static void
a6d9a66e 8037print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8038{
b4d90040 8039 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8040 struct value_print_options opts;
79a45e25 8041 struct ui_out *uiout = current_uiout;
fe798b75
JB
8042
8043 get_user_print_options (&opts);
8044
8045 /* Field 4, the address, is omitted (which makes the columns
8046 not line up too nicely with the headers, but the effect
8047 is relatively readable). */
8048 if (opts.addressprint)
8049 ui_out_field_skip (uiout, "addr");
8050 annotate_field (5);
8051 ui_out_text (uiout, "exec");
b4d90040 8052 if (c->exec_pathname != NULL)
fe798b75
JB
8053 {
8054 ui_out_text (uiout, ", program \"");
b4d90040 8055 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8056 ui_out_text (uiout, "\" ");
8057 }
8058}
8059
8060static void
8061print_mention_catch_exec (struct breakpoint *b)
8062{
8063 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8064}
8065
6149aea9
PA
8066/* Implement the "print_recreate" breakpoint_ops method for exec
8067 catchpoints. */
8068
8069static void
8070print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8071{
8072 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8073 print_recreate_thread (b, fp);
6149aea9
PA
8074}
8075
2060206e 8076static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8077
a96d9b2e
SDJ
8078static void
8079create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8080 const struct breakpoint_ops *ops)
a96d9b2e 8081{
be5c67c1 8082 struct syscall_catchpoint *c;
a96d9b2e 8083 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8084
be5c67c1
PA
8085 c = XNEW (struct syscall_catchpoint);
8086 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8087 c->syscalls_to_be_caught = filter;
a96d9b2e 8088
3ea46bff 8089 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8090}
8091
c906108c 8092static int
fba45db2 8093hw_breakpoint_used_count (void)
c906108c 8094{
c906108c 8095 int i = 0;
f1310107
TJB
8096 struct breakpoint *b;
8097 struct bp_location *bl;
c906108c
SS
8098
8099 ALL_BREAKPOINTS (b)
c5aa993b 8100 {
d6b74ac4 8101 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8102 for (bl = b->loc; bl; bl = bl->next)
8103 {
8104 /* Special types of hardware breakpoints may use more than
8105 one register. */
348d480f 8106 i += b->ops->resources_needed (bl);
f1310107 8107 }
c5aa993b 8108 }
c906108c
SS
8109
8110 return i;
8111}
8112
a1398e0c
PA
8113/* Returns the resources B would use if it were a hardware
8114 watchpoint. */
8115
c906108c 8116static int
a1398e0c 8117hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8118{
c906108c 8119 int i = 0;
e09342b5 8120 struct bp_location *bl;
c906108c 8121
a1398e0c
PA
8122 if (!breakpoint_enabled (b))
8123 return 0;
8124
8125 for (bl = b->loc; bl; bl = bl->next)
8126 {
8127 /* Special types of hardware watchpoints may use more than
8128 one register. */
8129 i += b->ops->resources_needed (bl);
8130 }
8131
8132 return i;
8133}
8134
8135/* Returns the sum the used resources of all hardware watchpoints of
8136 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8137 the sum of the used resources of all hardware watchpoints of other
8138 types _not_ TYPE. */
8139
8140static int
8141hw_watchpoint_used_count_others (struct breakpoint *except,
8142 enum bptype type, int *other_type_used)
8143{
8144 int i = 0;
8145 struct breakpoint *b;
8146
c906108c
SS
8147 *other_type_used = 0;
8148 ALL_BREAKPOINTS (b)
e09342b5 8149 {
a1398e0c
PA
8150 if (b == except)
8151 continue;
e09342b5
TJB
8152 if (!breakpoint_enabled (b))
8153 continue;
8154
a1398e0c
PA
8155 if (b->type == type)
8156 i += hw_watchpoint_use_count (b);
8157 else if (is_hardware_watchpoint (b))
8158 *other_type_used = 1;
e09342b5
TJB
8159 }
8160
c906108c
SS
8161 return i;
8162}
8163
c906108c 8164void
fba45db2 8165disable_watchpoints_before_interactive_call_start (void)
c906108c 8166{
c5aa993b 8167 struct breakpoint *b;
c906108c
SS
8168
8169 ALL_BREAKPOINTS (b)
c5aa993b 8170 {
cc60f2e3 8171 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8172 {
b5de0fa7 8173 b->enable_state = bp_call_disabled;
b60e7edf 8174 update_global_location_list (0);
c5aa993b
JM
8175 }
8176 }
c906108c
SS
8177}
8178
8179void
fba45db2 8180enable_watchpoints_after_interactive_call_stop (void)
c906108c 8181{
c5aa993b 8182 struct breakpoint *b;
c906108c
SS
8183
8184 ALL_BREAKPOINTS (b)
c5aa993b 8185 {
cc60f2e3 8186 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8187 {
b5de0fa7 8188 b->enable_state = bp_enabled;
b60e7edf 8189 update_global_location_list (1);
c5aa993b
JM
8190 }
8191 }
c906108c
SS
8192}
8193
8bea4e01
UW
8194void
8195disable_breakpoints_before_startup (void)
8196{
6c95b8df 8197 current_program_space->executing_startup = 1;
f8eba3c6 8198 update_global_location_list (0);
8bea4e01
UW
8199}
8200
8201void
8202enable_breakpoints_after_startup (void)
8203{
6c95b8df 8204 current_program_space->executing_startup = 0;
f8eba3c6 8205 breakpoint_re_set ();
8bea4e01
UW
8206}
8207
c906108c
SS
8208
8209/* Set a breakpoint that will evaporate an end of command
8210 at address specified by SAL.
8211 Restrict it to frame FRAME if FRAME is nonzero. */
8212
8213struct breakpoint *
a6d9a66e
UW
8214set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8215 struct frame_id frame_id, enum bptype type)
c906108c 8216{
52f0bd74 8217 struct breakpoint *b;
edb3359d
DJ
8218
8219 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8220 one. */
8221 gdb_assert (!frame_id_inlined_p (frame_id));
8222
06edf0c0 8223 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8224 b->enable_state = bp_enabled;
8225 b->disposition = disp_donttouch;
818dd999 8226 b->frame_id = frame_id;
c906108c 8227
4a64f543
MS
8228 /* If we're debugging a multi-threaded program, then we want
8229 momentary breakpoints to be active in only a single thread of
8230 control. */
39f77062
KB
8231 if (in_thread_list (inferior_ptid))
8232 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8233
b60e7edf 8234 update_global_location_list_nothrow (1);
74960c60 8235
c906108c
SS
8236 return b;
8237}
611c83ae 8238
06edf0c0
PA
8239/* Make a momentary breakpoint based on the master breakpoint ORIG.
8240 The new breakpoint will have type TYPE, and use OPS as it
8241 breakpoint_ops. */
e58b0e63 8242
06edf0c0
PA
8243static struct breakpoint *
8244momentary_breakpoint_from_master (struct breakpoint *orig,
8245 enum bptype type,
c0a91b2b 8246 const struct breakpoint_ops *ops)
e58b0e63
PA
8247{
8248 struct breakpoint *copy;
8249
06edf0c0 8250 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8251 copy->loc = allocate_bp_location (copy);
0e30163f 8252 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8253
a6d9a66e 8254 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8255 copy->loc->requested_address = orig->loc->requested_address;
8256 copy->loc->address = orig->loc->address;
8257 copy->loc->section = orig->loc->section;
6c95b8df 8258 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8259 copy->loc->probe = orig->loc->probe;
e58b0e63 8260
f8eba3c6
TT
8261 if (orig->loc->source_file != NULL)
8262 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 8263
f8eba3c6 8264 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
8265 copy->frame_id = orig->frame_id;
8266 copy->thread = orig->thread;
6c95b8df 8267 copy->pspace = orig->pspace;
e58b0e63
PA
8268
8269 copy->enable_state = bp_enabled;
8270 copy->disposition = disp_donttouch;
8271 copy->number = internal_breakpoint_number--;
8272
8273 update_global_location_list_nothrow (0);
8274 return copy;
8275}
8276
06edf0c0
PA
8277/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8278 ORIG is NULL. */
8279
8280struct breakpoint *
8281clone_momentary_breakpoint (struct breakpoint *orig)
8282{
8283 /* If there's nothing to clone, then return nothing. */
8284 if (orig == NULL)
8285 return NULL;
8286
8287 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8288}
8289
611c83ae 8290struct breakpoint *
a6d9a66e
UW
8291set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8292 enum bptype type)
611c83ae
PA
8293{
8294 struct symtab_and_line sal;
8295
8296 sal = find_pc_line (pc, 0);
8297 sal.pc = pc;
8298 sal.section = find_pc_overlay (pc);
8299 sal.explicit_pc = 1;
8300
a6d9a66e 8301 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8302}
c906108c 8303\f
c5aa993b 8304
c906108c
SS
8305/* Tell the user we have just set a breakpoint B. */
8306
8307static void
fba45db2 8308mention (struct breakpoint *b)
c906108c 8309{
348d480f 8310 b->ops->print_mention (b);
79a45e25 8311 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8312 return;
c906108c
SS
8313 printf_filtered ("\n");
8314}
c906108c 8315\f
c5aa993b 8316
0d381245 8317static struct bp_location *
39d61571 8318add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8319 const struct symtab_and_line *sal)
8320{
8321 struct bp_location *loc, **tmp;
3742cc8b
YQ
8322 CORE_ADDR adjusted_address;
8323 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8324
8325 if (loc_gdbarch == NULL)
8326 loc_gdbarch = b->gdbarch;
8327
8328 /* Adjust the breakpoint's address prior to allocating a location.
8329 Once we call allocate_bp_location(), that mostly uninitialized
8330 location will be placed on the location chain. Adjustment of the
8331 breakpoint may cause target_read_memory() to be called and we do
8332 not want its scan of the location chain to find a breakpoint and
8333 location that's only been partially initialized. */
8334 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8335 sal->pc, b->type);
0d381245 8336
39d61571 8337 loc = allocate_bp_location (b);
0d381245
VP
8338 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8339 ;
8340 *tmp = loc;
3742cc8b 8341
0d381245 8342 loc->requested_address = sal->pc;
3742cc8b 8343 loc->address = adjusted_address;
6c95b8df 8344 loc->pspace = sal->pspace;
55aa24fb 8345 loc->probe = sal->probe;
6c95b8df 8346 gdb_assert (loc->pspace != NULL);
0d381245 8347 loc->section = sal->section;
3742cc8b 8348 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
8349
8350 if (sal->symtab != NULL)
8351 loc->source_file = xstrdup (sal->symtab->filename);
8352 loc->line_number = sal->line;
8353
0e30163f
JK
8354 set_breakpoint_location_function (loc,
8355 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8356 return loc;
8357}
514f746b
AR
8358\f
8359
8360/* Return 1 if LOC is pointing to a permanent breakpoint,
8361 return 0 otherwise. */
8362
8363static int
8364bp_loc_is_permanent (struct bp_location *loc)
8365{
8366 int len;
8367 CORE_ADDR addr;
1afeeb75 8368 const gdb_byte *bpoint;
514f746b 8369 gdb_byte *target_mem;
939c61fa
JK
8370 struct cleanup *cleanup;
8371 int retval = 0;
514f746b
AR
8372
8373 gdb_assert (loc != NULL);
8374
8375 addr = loc->address;
1afeeb75 8376 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8377
939c61fa 8378 /* Software breakpoints unsupported? */
1afeeb75 8379 if (bpoint == NULL)
939c61fa
JK
8380 return 0;
8381
514f746b
AR
8382 target_mem = alloca (len);
8383
939c61fa
JK
8384 /* Enable the automatic memory restoration from breakpoints while
8385 we read the memory. Otherwise we could say about our temporary
8386 breakpoints they are permanent. */
6c95b8df
PA
8387 cleanup = save_current_space_and_thread ();
8388
8389 switch_to_program_space_and_thread (loc->pspace);
8390 make_show_memory_breakpoints_cleanup (0);
939c61fa 8391
514f746b 8392 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8393 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8394 retval = 1;
514f746b 8395
939c61fa
JK
8396 do_cleanups (cleanup);
8397
8398 return retval;
514f746b
AR
8399}
8400
e7e0cddf
SS
8401/* The style in which to perform a dynamic printf. This is a user
8402 option because different output options have different tradeoffs;
8403 if GDB does the printing, there is better error handling if there
8404 is a problem with any of the arguments, but using an inferior
8405 function lets you have special-purpose printers and sending of
8406 output to the same place as compiled-in print functions. (Future
8407 styles may include the ability to do a target-side printf.) */
8408
8409static const char dprintf_style_gdb[] = "gdb";
8410static const char dprintf_style_call[] = "call";
8411static const char *const dprintf_style_enums[] = {
8412 dprintf_style_gdb,
8413 dprintf_style_call,
8414 NULL
8415};
8416static const char *dprintf_style = dprintf_style_gdb;
8417
8418/* The function to use for dynamic printf if the preferred style is to
8419 call into the inferior. The value is simply a string that is
8420 copied into the command, so it can be anything that GDB can
8421 evaluate to a callable address, not necessarily a function name. */
8422
8423static char *dprintf_function = "";
8424
8425/* The channel to use for dynamic printf if the preferred style is to
8426 call into the inferior; if a nonempty string, it will be passed to
8427 the call as the first argument, with the format string as the
8428 second. As with the dprintf function, this can be anything that
8429 GDB knows how to evaluate, so in addition to common choices like
8430 "stderr", this could be an app-specific expression like
8431 "mystreams[curlogger]". */
514f746b 8432
e7e0cddf
SS
8433static char *dprintf_channel = "";
8434
8435/* Build a command list for the dprintf corresponding to the current
8436 settings of the dprintf style options. */
8437
8438static void
8439update_dprintf_command_list (struct breakpoint *b)
8440{
8441 char *dprintf_args = b->extra_string;
8442 char *printf_line = NULL;
8443
8444 if (!dprintf_args)
8445 return;
8446
8447 dprintf_args = skip_spaces (dprintf_args);
8448
8449 /* Allow a comma, as it may have terminated a location, but don't
8450 insist on it. */
8451 if (*dprintf_args == ',')
8452 ++dprintf_args;
8453 dprintf_args = skip_spaces (dprintf_args);
8454
8455 if (*dprintf_args != '"')
8456 error (_("Bad format string, missing '\"'."));
8457
8458 if (strcmp (dprintf_style, "gdb") == 0)
8459 printf_line = xstrprintf ("printf %s", dprintf_args);
8460 else if (strcmp (dprintf_style, "call") == 0)
8461 {
8462 if (!dprintf_function)
8463 error (_("No function supplied for dprintf call"));
8464
8465 if (dprintf_channel && strlen (dprintf_channel) > 0)
8466 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8467 dprintf_function,
8468 dprintf_channel,
8469 dprintf_args);
8470 else
8471 printf_line = xstrprintf ("call (void) %s (%s)",
8472 dprintf_function,
8473 dprintf_args);
8474 }
8475 else
8476 internal_error (__FILE__, __LINE__,
8477 _("Invalid dprintf style."));
8478
8479 /* Manufacture a printf/continue sequence. */
8480 if (printf_line)
8481 {
8482 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8483
8484 cont_cmd_line = xmalloc (sizeof (struct command_line));
8485 cont_cmd_line->control_type = simple_control;
8486 cont_cmd_line->body_count = 0;
8487 cont_cmd_line->body_list = NULL;
8488 cont_cmd_line->next = NULL;
8489 cont_cmd_line->line = xstrdup ("continue");
8490
8491 printf_cmd_line = xmalloc (sizeof (struct command_line));
8492 printf_cmd_line->control_type = simple_control;
8493 printf_cmd_line->body_count = 0;
8494 printf_cmd_line->body_list = NULL;
8495 printf_cmd_line->next = cont_cmd_line;
8496 printf_cmd_line->line = printf_line;
8497
8498 breakpoint_set_commands (b, printf_cmd_line);
8499 }
8500}
8501
8502/* Update all dprintf commands, making their command lists reflect
8503 current style settings. */
8504
8505static void
8506update_dprintf_commands (char *args, int from_tty,
8507 struct cmd_list_element *c)
8508{
8509 struct breakpoint *b;
8510
8511 ALL_BREAKPOINTS (b)
8512 {
8513 if (b->type == bp_dprintf)
8514 update_dprintf_command_list (b);
8515 }
8516}
c3f6f71d 8517
018d34a4
VP
8518/* Create a breakpoint with SAL as location. Use ADDR_STRING
8519 as textual description of the location, and COND_STRING
db107f19 8520 as condition expression. */
018d34a4
VP
8521
8522static void
d9b3f62e
PA
8523init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8524 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8525 char *filter, char *cond_string,
e7e0cddf 8526 char *extra_string,
d9b3f62e
PA
8527 enum bptype type, enum bpdisp disposition,
8528 int thread, int task, int ignore_count,
c0a91b2b 8529 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8530 int enabled, int internal, unsigned flags,
8531 int display_canonical)
018d34a4 8532{
0d381245 8533 int i;
018d34a4
VP
8534
8535 if (type == bp_hardware_breakpoint)
8536 {
fbbd034e
AS
8537 int target_resources_ok;
8538
8539 i = hw_breakpoint_used_count ();
8540 target_resources_ok =
8541 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8542 i + 1, 0);
8543 if (target_resources_ok == 0)
8544 error (_("No hardware breakpoint support in the target."));
8545 else if (target_resources_ok < 0)
8546 error (_("Hardware breakpoints used exceeds limit."));
8547 }
8548
6c95b8df
PA
8549 gdb_assert (sals.nelts > 0);
8550
0d381245
VP
8551 for (i = 0; i < sals.nelts; ++i)
8552 {
8553 struct symtab_and_line sal = sals.sals[i];
8554 struct bp_location *loc;
8555
8556 if (from_tty)
5af949e3
UW
8557 {
8558 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8559 if (!loc_gdbarch)
8560 loc_gdbarch = gdbarch;
8561
8562 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8563 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8564 }
0d381245
VP
8565
8566 if (i == 0)
8567 {
d9b3f62e 8568 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8569 b->thread = thread;
4a306c9a 8570 b->task = task;
855a6e68 8571
0d381245 8572 b->cond_string = cond_string;
e7e0cddf 8573 b->extra_string = extra_string;
0d381245 8574 b->ignore_count = ignore_count;
41447f92 8575 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8576 b->disposition = disposition;
6c95b8df 8577
44f238bb
PA
8578 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8579 b->loc->inserted = 1;
8580
0fb4aa4b
PA
8581 if (type == bp_static_tracepoint)
8582 {
d9b3f62e 8583 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8584 struct static_tracepoint_marker marker;
8585
983af33b 8586 if (strace_marker_p (b))
0fb4aa4b
PA
8587 {
8588 /* We already know the marker exists, otherwise, we
8589 wouldn't see a sal for it. */
8590 char *p = &addr_string[3];
8591 char *endp;
8592 char *marker_str;
0fb4aa4b 8593
e9cafbcc 8594 p = skip_spaces (p);
0fb4aa4b 8595
e9cafbcc 8596 endp = skip_to_space (p);
0fb4aa4b
PA
8597
8598 marker_str = savestring (p, endp - p);
d9b3f62e 8599 t->static_trace_marker_id = marker_str;
0fb4aa4b 8600
3e43a32a
MS
8601 printf_filtered (_("Probed static tracepoint "
8602 "marker \"%s\"\n"),
d9b3f62e 8603 t->static_trace_marker_id);
0fb4aa4b
PA
8604 }
8605 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8606 {
d9b3f62e 8607 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8608 release_static_tracepoint_marker (&marker);
8609
3e43a32a
MS
8610 printf_filtered (_("Probed static tracepoint "
8611 "marker \"%s\"\n"),
d9b3f62e 8612 t->static_trace_marker_id);
0fb4aa4b
PA
8613 }
8614 else
3e43a32a
MS
8615 warning (_("Couldn't determine the static "
8616 "tracepoint marker to probe"));
0fb4aa4b
PA
8617 }
8618
0d381245
VP
8619 loc = b->loc;
8620 }
8621 else
018d34a4 8622 {
39d61571 8623 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8624 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8625 loc->inserted = 1;
0d381245
VP
8626 }
8627
514f746b
AR
8628 if (bp_loc_is_permanent (loc))
8629 make_breakpoint_permanent (b);
8630
0d381245
VP
8631 if (b->cond_string)
8632 {
8633 char *arg = b->cond_string;
d32a6982 8634 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 8635 if (*arg)
588ae58c 8636 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8637 }
e7e0cddf
SS
8638
8639 /* Dynamic printf requires and uses additional arguments on the
8640 command line, otherwise it's an error. */
8641 if (type == bp_dprintf)
8642 {
8643 if (b->extra_string)
8644 update_dprintf_command_list (b);
8645 else
8646 error (_("Format string required"));
8647 }
8648 else if (b->extra_string)
588ae58c 8649 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8650 }
018d34a4 8651
56435ebe 8652 b->display_canonical = display_canonical;
018d34a4
VP
8653 if (addr_string)
8654 b->addr_string = addr_string;
8655 else
8656 /* addr_string has to be used or breakpoint_re_set will delete
8657 me. */
5af949e3
UW
8658 b->addr_string
8659 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 8660 b->filter = filter;
d9b3f62e 8661}
018d34a4 8662
d9b3f62e
PA
8663static void
8664create_breakpoint_sal (struct gdbarch *gdbarch,
8665 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8666 char *filter, char *cond_string,
e7e0cddf 8667 char *extra_string,
d9b3f62e
PA
8668 enum bptype type, enum bpdisp disposition,
8669 int thread, int task, int ignore_count,
c0a91b2b 8670 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8671 int enabled, int internal, unsigned flags,
8672 int display_canonical)
d9b3f62e
PA
8673{
8674 struct breakpoint *b;
8675 struct cleanup *old_chain;
8676
8677 if (is_tracepoint_type (type))
8678 {
8679 struct tracepoint *t;
8680
8681 t = XCNEW (struct tracepoint);
8682 b = &t->base;
8683 }
8684 else
8685 b = XNEW (struct breakpoint);
8686
8687 old_chain = make_cleanup (xfree, b);
8688
8689 init_breakpoint_sal (b, gdbarch,
8690 sals, addr_string,
e7e0cddf 8691 filter, cond_string, extra_string,
d9b3f62e
PA
8692 type, disposition,
8693 thread, task, ignore_count,
8694 ops, from_tty,
44f238bb
PA
8695 enabled, internal, flags,
8696 display_canonical);
d9b3f62e
PA
8697 discard_cleanups (old_chain);
8698
3ea46bff 8699 install_breakpoint (internal, b, 0);
018d34a4
VP
8700}
8701
8702/* Add SALS.nelts breakpoints to the breakpoint table. For each
8703 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8704 value. COND_STRING, if not NULL, specified the condition to be
8705 used for all breakpoints. Essentially the only case where
8706 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8707 function. In that case, it's still not possible to specify
8708 separate conditions for different overloaded functions, so
8709 we take just a single condition string.
8710
c3f6f71d 8711 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8712 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8713 array contents). If the function fails (error() is called), the
8714 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8715 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8716
8717static void
8cdf0e15 8718create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8719 struct linespec_result *canonical,
e7e0cddf 8720 char *cond_string, char *extra_string,
8cdf0e15
VP
8721 enum bptype type, enum bpdisp disposition,
8722 int thread, int task, int ignore_count,
c0a91b2b 8723 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8724 int enabled, int internal, unsigned flags)
c906108c 8725{
018d34a4 8726 int i;
f8eba3c6 8727 struct linespec_sals *lsal;
cc59ec59 8728
f8eba3c6
TT
8729 if (canonical->pre_expanded)
8730 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8731
8732 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 8733 {
f8eba3c6
TT
8734 /* Note that 'addr_string' can be NULL in the case of a plain
8735 'break', without arguments. */
8736 char *addr_string = (canonical->addr_string
8737 ? xstrdup (canonical->addr_string)
8738 : NULL);
8739 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8740 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 8741
f8eba3c6
TT
8742 make_cleanup (xfree, filter_string);
8743 create_breakpoint_sal (gdbarch, lsal->sals,
8744 addr_string,
8745 filter_string,
e7e0cddf
SS
8746 cond_string, extra_string,
8747 type, disposition,
84f4c1fe 8748 thread, task, ignore_count, ops,
44f238bb 8749 from_tty, enabled, internal, flags,
56435ebe 8750 canonical->special_display);
f8eba3c6 8751 discard_cleanups (inner);
c3f6f71d 8752 }
c3f6f71d 8753}
c906108c 8754
9998af43 8755/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 8756 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 8757 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
8758 address strings. ADDRESS points to the end of the SAL.
8759
8760 The array and the line spec strings are allocated on the heap, it is
8761 the caller's responsibility to free them. */
c906108c 8762
b9362cc7 8763static void
c3f6f71d 8764parse_breakpoint_sals (char **address,
58438ac1 8765 struct linespec_result *canonical)
c3f6f71d
JM
8766{
8767 char *addr_start = *address;
cc59ec59 8768
c3f6f71d 8769 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 8770 breakpoint. */
c3f6f71d
JM
8771 if ((*address) == NULL
8772 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 8773 {
1bfeeb0f
JL
8774 /* The last displayed codepoint, if it's valid, is our default breakpoint
8775 address. */
8776 if (last_displayed_sal_is_valid ())
c906108c 8777 {
f8eba3c6 8778 struct linespec_sals lsal;
c3f6f71d 8779 struct symtab_and_line sal;
cc59ec59 8780
4a64f543 8781 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 8782 lsal.sals.sals = (struct symtab_and_line *)
c906108c 8783 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
8784
8785 /* Set sal's pspace, pc, symtab, and line to the values
8786 corresponding to the last call to print_frame_info. */
8787 get_last_displayed_sal (&sal);
8788 sal.section = find_pc_overlay (sal.pc);
00903456 8789
4a64f543 8790 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
8791 where PC is the last displayed codepoint's address. So
8792 make sure to set sal.explicit_pc to prevent GDB from
8793 trying to expand the list of sals to include all other
8794 instances with the same symtab and line. */
00903456
JK
8795 sal.explicit_pc = 1;
8796
f8eba3c6
TT
8797 lsal.sals.sals[0] = sal;
8798 lsal.sals.nelts = 1;
8799 lsal.canonical = NULL;
8800
8801 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
8802 }
8803 else
8a3fe4f8 8804 error (_("No default breakpoint address now."));
c906108c
SS
8805 }
8806 else
8807 {
c906108c 8808 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
8809 current_source_symtab (which is decode_line_1's default).
8810 This should produce the results we want almost all of the
f8eba3c6
TT
8811 time while leaving default_breakpoint_* alone. */
8812 if (last_displayed_sal_is_valid ())
8813 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8814 get_last_displayed_symtab (),
8815 get_last_displayed_line (),
8816 canonical, NULL, NULL);
c906108c 8817 else
f8eba3c6
TT
8818 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8819 (struct symtab *) NULL, 0,
8820 canonical, NULL, NULL);
c906108c 8821 }
c3f6f71d 8822}
c906108c 8823
c906108c 8824
c3f6f71d 8825/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8826 inserted as a breakpoint. If it can't throw an error. */
c906108c 8827
b9362cc7 8828static void
23e7acfb 8829breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
8830{
8831 int i;
cc59ec59 8832
c3f6f71d 8833 for (i = 0; i < sals->nelts; i++)
ee53e872 8834 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
8835}
8836
7a697b8d
SS
8837/* Fast tracepoints may have restrictions on valid locations. For
8838 instance, a fast tracepoint using a jump instead of a trap will
8839 likely have to overwrite more bytes than a trap would, and so can
8840 only be placed where the instruction is longer than the jump, or a
8841 multi-instruction sequence does not have a jump into the middle of
8842 it, etc. */
8843
8844static void
8845check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8846 struct symtabs_and_lines *sals)
8847{
8848 int i, rslt;
8849 struct symtab_and_line *sal;
8850 char *msg;
8851 struct cleanup *old_chain;
8852
8853 for (i = 0; i < sals->nelts; i++)
8854 {
f8eba3c6
TT
8855 struct gdbarch *sarch;
8856
7a697b8d
SS
8857 sal = &sals->sals[i];
8858
f8eba3c6
TT
8859 sarch = get_sal_arch (*sal);
8860 /* We fall back to GDBARCH if there is no architecture
8861 associated with SAL. */
8862 if (sarch == NULL)
8863 sarch = gdbarch;
8864 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
8865 NULL, &msg);
8866 old_chain = make_cleanup (xfree, msg);
8867
8868 if (!rslt)
8869 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 8870 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
8871
8872 do_cleanups (old_chain);
8873 }
8874}
8875
018d34a4
VP
8876/* Given TOK, a string specification of condition and thread, as
8877 accepted by the 'break' command, extract the condition
8878 string and thread number and set *COND_STRING and *THREAD.
4a64f543 8879 PC identifies the context at which the condition should be parsed.
018d34a4
VP
8880 If no condition is found, *COND_STRING is set to NULL.
8881 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
8882
8883static void
8884find_condition_and_thread (char *tok, CORE_ADDR pc,
e7e0cddf
SS
8885 char **cond_string, int *thread, int *task,
8886 char **rest)
018d34a4
VP
8887{
8888 *cond_string = NULL;
8889 *thread = -1;
8890 while (tok && *tok)
8891 {
8892 char *end_tok;
8893 int toklen;
8894 char *cond_start = NULL;
8895 char *cond_end = NULL;
cc59ec59 8896
e9cafbcc 8897 tok = skip_spaces (tok);
e7e0cddf
SS
8898
8899 if ((*tok == '"' || *tok == ',') && rest)
8900 {
8901 *rest = savestring (tok, strlen (tok));
8902 return;
8903 }
8904
e9cafbcc 8905 end_tok = skip_to_space (tok);
d634f2de 8906
018d34a4 8907 toklen = end_tok - tok;
d634f2de 8908
018d34a4
VP
8909 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8910 {
f7545552
TT
8911 struct expression *expr;
8912
018d34a4 8913 tok = cond_start = end_tok + 1;
f7545552
TT
8914 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
8915 xfree (expr);
018d34a4 8916 cond_end = tok;
d634f2de 8917 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
8918 }
8919 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8920 {
8921 char *tmptok;
d634f2de 8922
018d34a4
VP
8923 tok = end_tok + 1;
8924 tmptok = tok;
8925 *thread = strtol (tok, &tok, 0);
8926 if (tok == tmptok)
8927 error (_("Junk after thread keyword."));
8928 if (!valid_thread_id (*thread))
8929 error (_("Unknown thread %d."), *thread);
8930 }
4a306c9a
JB
8931 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8932 {
8933 char *tmptok;
8934
8935 tok = end_tok + 1;
8936 tmptok = tok;
8937 *task = strtol (tok, &tok, 0);
8938 if (tok == tmptok)
8939 error (_("Junk after task keyword."));
8940 if (!valid_task_id (*task))
b6199126 8941 error (_("Unknown task %d."), *task);
4a306c9a 8942 }
e7e0cddf
SS
8943 else if (rest)
8944 {
8945 *rest = savestring (tok, strlen (tok));
8946 tok += toklen;
8947 }
018d34a4
VP
8948 else
8949 error (_("Junk at end of arguments."));
8950 }
8951}
8952
0fb4aa4b
PA
8953/* Decode a static tracepoint marker spec. */
8954
8955static struct symtabs_and_lines
8956decode_static_tracepoint_spec (char **arg_p)
8957{
8958 VEC(static_tracepoint_marker_p) *markers = NULL;
8959 struct symtabs_and_lines sals;
8960 struct symtab_and_line sal;
8961 struct symbol *sym;
8962 struct cleanup *old_chain;
8963 char *p = &(*arg_p)[3];
8964 char *endp;
8965 char *marker_str;
8966 int i;
8967
e9cafbcc 8968 p = skip_spaces (p);
0fb4aa4b 8969
e9cafbcc 8970 endp = skip_to_space (p);
0fb4aa4b
PA
8971
8972 marker_str = savestring (p, endp - p);
8973 old_chain = make_cleanup (xfree, marker_str);
8974
8975 markers = target_static_tracepoint_markers_by_strid (marker_str);
8976 if (VEC_empty(static_tracepoint_marker_p, markers))
8977 error (_("No known static tracepoint marker named %s"), marker_str);
8978
8979 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8980 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
8981
8982 for (i = 0; i < sals.nelts; i++)
8983 {
8984 struct static_tracepoint_marker *marker;
8985
8986 marker = VEC_index (static_tracepoint_marker_p, markers, i);
8987
8988 init_sal (&sals.sals[i]);
8989
8990 sals.sals[i] = find_pc_line (marker->address, 0);
8991 sals.sals[i].pc = marker->address;
8992
8993 release_static_tracepoint_marker (marker);
8994 }
8995
8996 do_cleanups (old_chain);
8997
8998 *arg_p = endp;
8999 return sals;
9000}
9001
fd9b8c24
PA
9002/* Set a breakpoint. This function is shared between CLI and MI
9003 functions for setting a breakpoint. This function has two major
9004 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9005 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
9006 breakpoint location, address and thread. Otherwise, ARG is just
9007 the location of breakpoint, with condition and thread specified by
9008 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9009 the breakpoint number will be allocated from the internal
9010 breakpoint count. Returns true if any breakpoint was created;
9011 false otherwise. */
0101ce28 9012
8cdf0e15
VP
9013int
9014create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9015 char *arg, char *cond_string,
9016 int thread, char *extra_string,
8cdf0e15 9017 int parse_condition_and_thread,
0fb4aa4b 9018 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9019 int ignore_count,
9020 enum auto_boolean pending_break_support,
c0a91b2b 9021 const struct breakpoint_ops *ops,
44f238bb
PA
9022 int from_tty, int enabled, int internal,
9023 unsigned flags)
c3f6f71d 9024{
b78a6381 9025 volatile struct gdb_exception e;
f8eba3c6 9026 char *copy_arg = NULL;
c3f6f71d 9027 char *addr_start = arg;
7efd8fc2 9028 struct linespec_result canonical;
c3f6f71d 9029 struct cleanup *old_chain;
80c99de1 9030 struct cleanup *bkpt_chain = NULL;
05ff989b 9031 int i;
0101ce28 9032 int pending = 0;
4a306c9a 9033 int task = 0;
86b17b60 9034 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9035
348d480f
PA
9036 gdb_assert (ops != NULL);
9037
7efd8fc2 9038 init_linespec_result (&canonical);
c3f6f71d 9039
b78a6381
TT
9040 TRY_CATCH (e, RETURN_MASK_ALL)
9041 {
983af33b
SDJ
9042 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9043 addr_start, &copy_arg);
b78a6381 9044 }
0101ce28
JJ
9045
9046 /* If caller is interested in rc value from parse, set value. */
05ff989b 9047 switch (e.reason)
0101ce28 9048 {
983af33b
SDJ
9049 case GDB_NO_ERROR:
9050 if (VEC_empty (linespec_sals, canonical.sals))
9051 return 0;
9052 break;
05ff989b
AC
9053 case RETURN_ERROR:
9054 switch (e.error)
0101ce28 9055 {
05ff989b 9056 case NOT_FOUND_ERROR:
0101ce28 9057
05ff989b
AC
9058 /* If pending breakpoint support is turned off, throw
9059 error. */
fa8d40ab
JJ
9060
9061 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9062 throw_exception (e);
9063
9064 exception_print (gdb_stderr, e);
fa8d40ab 9065
05ff989b
AC
9066 /* If pending breakpoint support is auto query and the user
9067 selects no, then simply return the error code. */
059fb39f 9068 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9069 && !nquery (_("Make %s pending on future shared library load? "),
9070 bptype_string (type_wanted)))
fd9b8c24 9071 return 0;
fa8d40ab 9072
05ff989b
AC
9073 /* At this point, either the user was queried about setting
9074 a pending breakpoint and selected yes, or pending
9075 breakpoint behavior is on and thus a pending breakpoint
9076 is defaulted on behalf of the user. */
f8eba3c6
TT
9077 {
9078 struct linespec_sals lsal;
9079
9080 copy_arg = xstrdup (addr_start);
9081 lsal.canonical = xstrdup (copy_arg);
9082 lsal.sals.nelts = 1;
9083 lsal.sals.sals = XNEW (struct symtab_and_line);
9084 init_sal (&lsal.sals.sals[0]);
9085 pending = 1;
9086 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9087 }
05ff989b
AC
9088 break;
9089 default:
98deb0da 9090 throw_exception (e);
0101ce28 9091 }
2abae994 9092 break;
05ff989b 9093 default:
983af33b 9094 throw_exception (e);
0101ce28 9095 }
c3f6f71d 9096
4a64f543 9097 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9098 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9099
c3f6f71d
JM
9100 /* ----------------------------- SNIP -----------------------------
9101 Anything added to the cleanup chain beyond this point is assumed
9102 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9103 then the memory is not reclaimed. */
9104 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9105
c3f6f71d
JM
9106 /* Resolve all line numbers to PC's and verify that the addresses
9107 are ok for the target. */
0101ce28 9108 if (!pending)
f8eba3c6
TT
9109 {
9110 int ix;
9111 struct linespec_sals *iter;
9112
9113 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9114 breakpoint_sals_to_pc (&iter->sals);
9115 }
c3f6f71d 9116
7a697b8d 9117 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9118 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9119 {
9120 int ix;
9121 struct linespec_sals *iter;
9122
9123 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9124 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9125 }
7a697b8d 9126
c3f6f71d
JM
9127 /* Verify that condition can be parsed, before setting any
9128 breakpoints. Allocate a separate condition expression for each
4a64f543 9129 breakpoint. */
0101ce28 9130 if (!pending)
c3f6f71d 9131 {
f8eba3c6
TT
9132 struct linespec_sals *lsal;
9133
9134 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9135
2f069f6f 9136 if (parse_condition_and_thread)
72b2ff0e 9137 {
e7e0cddf 9138 char *rest;
72b2ff0e
VP
9139 /* Here we only parse 'arg' to separate condition
9140 from thread number, so parsing in context of first
9141 sal is OK. When setting the breakpoint we'll
9142 re-parse it in context of each sal. */
9143 cond_string = NULL;
9144 thread = -1;
e7e0cddf 9145 rest = NULL;
f8eba3c6 9146 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
e7e0cddf 9147 &thread, &task, &rest);
72b2ff0e
VP
9148 if (cond_string)
9149 make_cleanup (xfree, cond_string);
e7e0cddf
SS
9150 if (rest)
9151 make_cleanup (xfree, rest);
9152 if (rest)
9153 extra_string = rest;
72b2ff0e 9154 }
2f069f6f 9155 else
72b2ff0e
VP
9156 {
9157 /* Create a private copy of condition string. */
9158 if (cond_string)
9159 {
9160 cond_string = xstrdup (cond_string);
9161 make_cleanup (xfree, cond_string);
9162 }
e7e0cddf
SS
9163 /* Create a private copy of any extra string. */
9164 if (extra_string)
9165 {
9166 extra_string = xstrdup (extra_string);
9167 make_cleanup (xfree, extra_string);
9168 }
72b2ff0e 9169 }
0fb4aa4b 9170
983af33b 9171 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
e7e0cddf 9172 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9173 tempflag ? disp_del : disp_donttouch,
9174 thread, task, ignore_count, ops,
44f238bb 9175 from_tty, enabled, internal, flags);
c906108c 9176 }
0101ce28
JJ
9177 else
9178 {
0101ce28
JJ
9179 struct breakpoint *b;
9180
0101ce28
JJ
9181 make_cleanup (xfree, copy_arg);
9182
bfccc43c
YQ
9183 if (is_tracepoint_type (type_wanted))
9184 {
9185 struct tracepoint *t;
9186
9187 t = XCNEW (struct tracepoint);
9188 b = &t->base;
9189 }
9190 else
9191 b = XNEW (struct breakpoint);
9192
9193 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9194
f8eba3c6 9195 b->addr_string = copy_arg;
72b2ff0e 9196 b->cond_string = NULL;
e7e0cddf 9197 b->extra_string = NULL;
0101ce28 9198 b->ignore_count = ignore_count;
0101ce28 9199 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9200 b->condition_not_parsed = 1;
41447f92 9201 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9202 if ((type_wanted != bp_breakpoint
9203 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9204 b->pspace = current_program_space;
8bea4e01 9205
bfccc43c 9206 install_breakpoint (internal, b, 0);
0101ce28
JJ
9207 }
9208
f8eba3c6 9209 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9210 {
3e43a32a
MS
9211 warning (_("Multiple breakpoints were set.\nUse the "
9212 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9213 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9214 }
9215
80c99de1
PA
9216 /* That's it. Discard the cleanups for data inserted into the
9217 breakpoint. */
9218 discard_cleanups (bkpt_chain);
9219 /* But cleanup everything else. */
c3f6f71d 9220 do_cleanups (old_chain);
217dc9e2 9221
80c99de1 9222 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9223 update_global_location_list (1);
fd9b8c24
PA
9224
9225 return 1;
c3f6f71d 9226}
c906108c 9227
348d480f 9228/* Set a breakpoint.
72b2ff0e
VP
9229 ARG is a string describing breakpoint address,
9230 condition, and thread.
9231 FLAG specifies if a breakpoint is hardware on,
9232 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9233 and BP_TEMPFLAG. */
348d480f 9234
98deb0da 9235static void
72b2ff0e 9236break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9237{
72b2ff0e 9238 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9239 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9240 ? bp_hardware_breakpoint
9241 : bp_breakpoint);
55aa24fb
SDJ
9242 struct breakpoint_ops *ops;
9243 const char *arg_cp = arg;
9244
9245 /* Matching breakpoints on probes. */
9246 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9247 ops = &bkpt_probe_breakpoint_ops;
9248 else
9249 ops = &bkpt_breakpoint_ops;
c3f6f71d 9250
8cdf0e15
VP
9251 create_breakpoint (get_current_arch (),
9252 arg,
e7e0cddf 9253 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9254 tempflag, type_wanted,
8cdf0e15
VP
9255 0 /* Ignore count */,
9256 pending_break_support,
55aa24fb 9257 ops,
8cdf0e15 9258 from_tty,
84f4c1fe 9259 1 /* enabled */,
44f238bb
PA
9260 0 /* internal */,
9261 0);
c906108c
SS
9262}
9263
c906108c
SS
9264/* Helper function for break_command_1 and disassemble_command. */
9265
9266void
fba45db2 9267resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9268{
9269 CORE_ADDR pc;
9270
9271 if (sal->pc == 0 && sal->symtab != NULL)
9272 {
9273 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9274 error (_("No line %d in file \"%s\"."),
c906108c
SS
9275 sal->line, sal->symtab->filename);
9276 sal->pc = pc;
6a048695 9277
4a64f543
MS
9278 /* If this SAL corresponds to a breakpoint inserted using a line
9279 number, then skip the function prologue if necessary. */
6a048695 9280 if (sal->explicit_line)
059acae7 9281 skip_prologue_sal (sal);
c906108c
SS
9282 }
9283
9284 if (sal->section == 0 && sal->symtab != NULL)
9285 {
9286 struct blockvector *bv;
c5aa993b
JM
9287 struct block *b;
9288 struct symbol *sym;
c906108c 9289
801e3a5b 9290 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9291 if (bv != NULL)
9292 {
7f0df278 9293 sym = block_linkage_function (b);
c906108c
SS
9294 if (sym != NULL)
9295 {
9296 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 9297 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
9298 }
9299 else
9300 {
4a64f543
MS
9301 /* It really is worthwhile to have the section, so we'll
9302 just have to look harder. This case can be executed
9303 if we have line numbers but no functions (as can
9304 happen in assembly source). */
c906108c 9305
c5aa993b 9306 struct minimal_symbol *msym;
6c95b8df
PA
9307 struct cleanup *old_chain = save_current_space_and_thread ();
9308
9309 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9310
9311 msym = lookup_minimal_symbol_by_pc (sal->pc);
9312 if (msym)
714835d5 9313 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
9314
9315 do_cleanups (old_chain);
c906108c
SS
9316 }
9317 }
9318 }
9319}
9320
9321void
fba45db2 9322break_command (char *arg, int from_tty)
c906108c 9323{
db107f19 9324 break_command_1 (arg, 0, from_tty);
c906108c
SS
9325}
9326
c906108c 9327void
fba45db2 9328tbreak_command (char *arg, int from_tty)
c906108c 9329{
db107f19 9330 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9331}
9332
c906108c 9333static void
fba45db2 9334hbreak_command (char *arg, int from_tty)
c906108c 9335{
db107f19 9336 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9337}
9338
9339static void
fba45db2 9340thbreak_command (char *arg, int from_tty)
c906108c 9341{
db107f19 9342 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9343}
9344
9345static void
fba45db2 9346stop_command (char *arg, int from_tty)
c906108c 9347{
a3f17187 9348 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9349Usage: stop in <function | address>\n\
a3f17187 9350 stop at <line>\n"));
c906108c
SS
9351}
9352
9353static void
fba45db2 9354stopin_command (char *arg, int from_tty)
c906108c
SS
9355{
9356 int badInput = 0;
9357
c5aa993b 9358 if (arg == (char *) NULL)
c906108c
SS
9359 badInput = 1;
9360 else if (*arg != '*')
9361 {
9362 char *argptr = arg;
9363 int hasColon = 0;
9364
4a64f543 9365 /* Look for a ':'. If this is a line number specification, then
53a5351d 9366 say it is bad, otherwise, it should be an address or
4a64f543 9367 function/method name. */
c906108c 9368 while (*argptr && !hasColon)
c5aa993b
JM
9369 {
9370 hasColon = (*argptr == ':');
9371 argptr++;
9372 }
c906108c
SS
9373
9374 if (hasColon)
c5aa993b 9375 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9376 else
c5aa993b 9377 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9378 }
9379
9380 if (badInput)
a3f17187 9381 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9382 else
db107f19 9383 break_command_1 (arg, 0, from_tty);
c906108c
SS
9384}
9385
9386static void
fba45db2 9387stopat_command (char *arg, int from_tty)
c906108c
SS
9388{
9389 int badInput = 0;
9390
c5aa993b 9391 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9392 badInput = 1;
9393 else
9394 {
9395 char *argptr = arg;
9396 int hasColon = 0;
9397
4a64f543
MS
9398 /* Look for a ':'. If there is a '::' then get out, otherwise
9399 it is probably a line number. */
c906108c 9400 while (*argptr && !hasColon)
c5aa993b
JM
9401 {
9402 hasColon = (*argptr == ':');
9403 argptr++;
9404 }
c906108c
SS
9405
9406 if (hasColon)
c5aa993b 9407 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9408 else
c5aa993b 9409 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9410 }
9411
9412 if (badInput)
a3f17187 9413 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9414 else
db107f19 9415 break_command_1 (arg, 0, from_tty);
c906108c
SS
9416}
9417
e7e0cddf
SS
9418void dprintf_command (char *arg, int from_tty);
9419
9420/* The dynamic printf command is mostly like a regular breakpoint, but
9421 with a prewired command list consisting of a single output command,
9422 built from extra arguments supplied on the dprintf command
9423 line. */
9424
9425void
9426dprintf_command (char *arg, int from_tty)
9427{
9428 create_breakpoint (get_current_arch (),
9429 arg,
9430 NULL, 0, NULL, 1 /* parse arg */,
9431 0, bp_dprintf,
9432 0 /* Ignore count */,
9433 pending_break_support,
9434 &dprintf_breakpoint_ops,
9435 from_tty,
9436 1 /* enabled */,
9437 0 /* internal */,
9438 0);
9439}
9440
f1310107
TJB
9441/* Implement the "breakpoint_hit" breakpoint_ops method for
9442 ranged breakpoints. */
9443
9444static int
9445breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9446 struct address_space *aspace,
09ac7c10
TT
9447 CORE_ADDR bp_addr,
9448 const struct target_waitstatus *ws)
f1310107 9449{
09ac7c10
TT
9450 if (ws->kind != TARGET_WAITKIND_STOPPED
9451 || ws->value.sig != TARGET_SIGNAL_TRAP)
9452 return 0;
9453
f1310107
TJB
9454 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9455 bl->length, aspace, bp_addr);
9456}
9457
9458/* Implement the "resources_needed" breakpoint_ops method for
9459 ranged breakpoints. */
9460
9461static int
9462resources_needed_ranged_breakpoint (const struct bp_location *bl)
9463{
9464 return target_ranged_break_num_registers ();
9465}
9466
9467/* Implement the "print_it" breakpoint_ops method for
9468 ranged breakpoints. */
9469
9470static enum print_stop_action
348d480f 9471print_it_ranged_breakpoint (bpstat bs)
f1310107 9472{
348d480f 9473 struct breakpoint *b = bs->breakpoint_at;
f1310107 9474 struct bp_location *bl = b->loc;
79a45e25 9475 struct ui_out *uiout = current_uiout;
f1310107
TJB
9476
9477 gdb_assert (b->type == bp_hardware_breakpoint);
9478
9479 /* Ranged breakpoints have only one location. */
9480 gdb_assert (bl && bl->next == NULL);
9481
9482 annotate_breakpoint (b->number);
9483 if (b->disposition == disp_del)
9484 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9485 else
9486 ui_out_text (uiout, "\nRanged breakpoint ");
9487 if (ui_out_is_mi_like_p (uiout))
9488 {
9489 ui_out_field_string (uiout, "reason",
9490 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9491 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9492 }
9493 ui_out_field_int (uiout, "bkptno", b->number);
9494 ui_out_text (uiout, ", ");
9495
9496 return PRINT_SRC_AND_LOC;
9497}
9498
9499/* Implement the "print_one" breakpoint_ops method for
9500 ranged breakpoints. */
9501
9502static void
9503print_one_ranged_breakpoint (struct breakpoint *b,
9504 struct bp_location **last_loc)
9505{
9506 struct bp_location *bl = b->loc;
9507 struct value_print_options opts;
79a45e25 9508 struct ui_out *uiout = current_uiout;
f1310107
TJB
9509
9510 /* Ranged breakpoints have only one location. */
9511 gdb_assert (bl && bl->next == NULL);
9512
9513 get_user_print_options (&opts);
9514
9515 if (opts.addressprint)
9516 /* We don't print the address range here, it will be printed later
9517 by print_one_detail_ranged_breakpoint. */
9518 ui_out_field_skip (uiout, "addr");
9519 annotate_field (5);
9520 print_breakpoint_location (b, bl);
9521 *last_loc = bl;
9522}
9523
9524/* Implement the "print_one_detail" breakpoint_ops method for
9525 ranged breakpoints. */
9526
9527static void
9528print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9529 struct ui_out *uiout)
9530{
9531 CORE_ADDR address_start, address_end;
9532 struct bp_location *bl = b->loc;
f99d8bf4
PA
9533 struct ui_file *stb = mem_fileopen ();
9534 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
9535
9536 gdb_assert (bl);
9537
9538 address_start = bl->address;
9539 address_end = address_start + bl->length - 1;
9540
9541 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 9542 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
9543 print_core_address (bl->gdbarch, address_start),
9544 print_core_address (bl->gdbarch, address_end));
9545 ui_out_field_stream (uiout, "addr", stb);
9546 ui_out_text (uiout, "\n");
9547
9548 do_cleanups (cleanup);
9549}
9550
9551/* Implement the "print_mention" breakpoint_ops method for
9552 ranged breakpoints. */
9553
9554static void
9555print_mention_ranged_breakpoint (struct breakpoint *b)
9556{
9557 struct bp_location *bl = b->loc;
79a45e25 9558 struct ui_out *uiout = current_uiout;
f1310107
TJB
9559
9560 gdb_assert (bl);
9561 gdb_assert (b->type == bp_hardware_breakpoint);
9562
9563 if (ui_out_is_mi_like_p (uiout))
9564 return;
9565
9566 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9567 b->number, paddress (bl->gdbarch, bl->address),
9568 paddress (bl->gdbarch, bl->address + bl->length - 1));
9569}
9570
9571/* Implement the "print_recreate" breakpoint_ops method for
9572 ranged breakpoints. */
9573
9574static void
9575print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9576{
9577 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9578 b->addr_string_range_end);
d9b3f62e 9579 print_recreate_thread (b, fp);
f1310107
TJB
9580}
9581
9582/* The breakpoint_ops structure to be used in ranged breakpoints. */
9583
2060206e 9584static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9585
9586/* Find the address where the end of the breakpoint range should be
9587 placed, given the SAL of the end of the range. This is so that if
9588 the user provides a line number, the end of the range is set to the
9589 last instruction of the given line. */
9590
9591static CORE_ADDR
9592find_breakpoint_range_end (struct symtab_and_line sal)
9593{
9594 CORE_ADDR end;
9595
9596 /* If the user provided a PC value, use it. Otherwise,
9597 find the address of the end of the given location. */
9598 if (sal.explicit_pc)
9599 end = sal.pc;
9600 else
9601 {
9602 int ret;
9603 CORE_ADDR start;
9604
9605 ret = find_line_pc_range (sal, &start, &end);
9606 if (!ret)
9607 error (_("Could not find location of the end of the range."));
9608
9609 /* find_line_pc_range returns the start of the next line. */
9610 end--;
9611 }
9612
9613 return end;
9614}
9615
9616/* Implement the "break-range" CLI command. */
9617
9618static void
9619break_range_command (char *arg, int from_tty)
9620{
9621 char *arg_start, *addr_string_start, *addr_string_end;
9622 struct linespec_result canonical_start, canonical_end;
9623 int bp_count, can_use_bp, length;
9624 CORE_ADDR end;
9625 struct breakpoint *b;
9626 struct symtab_and_line sal_start, sal_end;
f1310107 9627 struct cleanup *cleanup_bkpt;
f8eba3c6 9628 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
9629
9630 /* We don't support software ranged breakpoints. */
9631 if (target_ranged_break_num_registers () < 0)
9632 error (_("This target does not support hardware ranged breakpoints."));
9633
9634 bp_count = hw_breakpoint_used_count ();
9635 bp_count += target_ranged_break_num_registers ();
9636 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9637 bp_count, 0);
9638 if (can_use_bp < 0)
9639 error (_("Hardware breakpoints used exceeds limit."));
9640
f8eba3c6 9641 arg = skip_spaces (arg);
f1310107
TJB
9642 if (arg == NULL || arg[0] == '\0')
9643 error(_("No address range specified."));
9644
f1310107
TJB
9645 init_linespec_result (&canonical_start);
9646
f8eba3c6
TT
9647 arg_start = arg;
9648 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 9649
f8eba3c6 9650 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
9651
9652 if (arg[0] != ',')
9653 error (_("Too few arguments."));
f8eba3c6 9654 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 9655 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
9656
9657 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9658
9659 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9660 || lsal_start->sals.nelts != 1)
f1310107
TJB
9661 error (_("Cannot create a ranged breakpoint with multiple locations."));
9662
f8eba3c6
TT
9663 sal_start = lsal_start->sals.sals[0];
9664 addr_string_start = savestring (arg_start, arg - arg_start);
9665 make_cleanup (xfree, addr_string_start);
f1310107
TJB
9666
9667 arg++; /* Skip the comma. */
f8eba3c6 9668 arg = skip_spaces (arg);
f1310107
TJB
9669
9670 /* Parse the end location. */
9671
f1310107
TJB
9672 init_linespec_result (&canonical_end);
9673 arg_start = arg;
9674
f8eba3c6 9675 /* We call decode_line_full directly here instead of using
f1310107
TJB
9676 parse_breakpoint_sals because we need to specify the start location's
9677 symtab and line as the default symtab and line for the end of the
9678 range. This makes it possible to have ranges like "foo.c:27, +14",
9679 where +14 means 14 lines from the start location. */
f8eba3c6
TT
9680 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9681 sal_start.symtab, sal_start.line,
9682 &canonical_end, NULL, NULL);
9683
9684 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 9685
f8eba3c6 9686 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 9687 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
9688
9689 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9690 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9691 || lsal_end->sals.nelts != 1)
f1310107
TJB
9692 error (_("Cannot create a ranged breakpoint with multiple locations."));
9693
f8eba3c6
TT
9694 sal_end = lsal_end->sals.sals[0];
9695 addr_string_end = savestring (arg_start, arg - arg_start);
9696 make_cleanup (xfree, addr_string_end);
f1310107
TJB
9697
9698 end = find_breakpoint_range_end (sal_end);
9699 if (sal_start.pc > end)
177b42fe 9700 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9701
9702 length = end - sal_start.pc + 1;
9703 if (length < 0)
9704 /* Length overflowed. */
9705 error (_("Address range too large."));
9706 else if (length == 1)
9707 {
9708 /* This range is simple enough to be handled by
9709 the `hbreak' command. */
9710 hbreak_command (addr_string_start, 1);
9711
9712 do_cleanups (cleanup_bkpt);
9713
9714 return;
9715 }
9716
9717 /* Now set up the breakpoint. */
9718 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9719 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9720 set_breakpoint_count (breakpoint_count + 1);
9721 b->number = breakpoint_count;
9722 b->disposition = disp_donttouch;
f8eba3c6
TT
9723 b->addr_string = xstrdup (addr_string_start);
9724 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
9725 b->loc->length = length;
9726
f8eba3c6 9727 do_cleanups (cleanup_bkpt);
f1310107
TJB
9728
9729 mention (b);
8d3788bd 9730 observer_notify_breakpoint_created (b);
f1310107
TJB
9731 update_global_location_list (1);
9732}
9733
4a64f543
MS
9734/* Return non-zero if EXP is verified as constant. Returned zero
9735 means EXP is variable. Also the constant detection may fail for
9736 some constant expressions and in such case still falsely return
9737 zero. */
2e6e3d9c 9738
65d79d4b
SDJ
9739static int
9740watchpoint_exp_is_const (const struct expression *exp)
9741{
9742 int i = exp->nelts;
9743
9744 while (i > 0)
9745 {
9746 int oplenp, argsp;
9747
9748 /* We are only interested in the descriptor of each element. */
9749 operator_length (exp, i, &oplenp, &argsp);
9750 i -= oplenp;
9751
9752 switch (exp->elts[i].opcode)
9753 {
9754 case BINOP_ADD:
9755 case BINOP_SUB:
9756 case BINOP_MUL:
9757 case BINOP_DIV:
9758 case BINOP_REM:
9759 case BINOP_MOD:
9760 case BINOP_LSH:
9761 case BINOP_RSH:
9762 case BINOP_LOGICAL_AND:
9763 case BINOP_LOGICAL_OR:
9764 case BINOP_BITWISE_AND:
9765 case BINOP_BITWISE_IOR:
9766 case BINOP_BITWISE_XOR:
9767 case BINOP_EQUAL:
9768 case BINOP_NOTEQUAL:
9769 case BINOP_LESS:
9770 case BINOP_GTR:
9771 case BINOP_LEQ:
9772 case BINOP_GEQ:
9773 case BINOP_REPEAT:
9774 case BINOP_COMMA:
9775 case BINOP_EXP:
9776 case BINOP_MIN:
9777 case BINOP_MAX:
9778 case BINOP_INTDIV:
9779 case BINOP_CONCAT:
9780 case BINOP_IN:
9781 case BINOP_RANGE:
9782 case TERNOP_COND:
9783 case TERNOP_SLICE:
9784 case TERNOP_SLICE_COUNT:
9785
9786 case OP_LONG:
9787 case OP_DOUBLE:
9788 case OP_DECFLOAT:
9789 case OP_LAST:
9790 case OP_COMPLEX:
9791 case OP_STRING:
9792 case OP_BITSTRING:
9793 case OP_ARRAY:
9794 case OP_TYPE:
9795 case OP_NAME:
9796 case OP_OBJC_NSSTRING:
9797
9798 case UNOP_NEG:
9799 case UNOP_LOGICAL_NOT:
9800 case UNOP_COMPLEMENT:
9801 case UNOP_ADDR:
9802 case UNOP_HIGH:
aeaa2474 9803 case UNOP_CAST:
4a64f543
MS
9804 /* Unary, binary and ternary operators: We have to check
9805 their operands. If they are constant, then so is the
9806 result of that operation. For instance, if A and B are
9807 determined to be constants, then so is "A + B".
9808
9809 UNOP_IND is one exception to the rule above, because the
9810 value of *ADDR is not necessarily a constant, even when
9811 ADDR is. */
65d79d4b
SDJ
9812 break;
9813
9814 case OP_VAR_VALUE:
9815 /* Check whether the associated symbol is a constant.
4a64f543 9816
65d79d4b 9817 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9818 possible that a buggy compiler could mark a variable as
9819 constant even when it is not, and TYPE_CONST would return
9820 true in this case, while SYMBOL_CLASS wouldn't.
9821
9822 We also have to check for function symbols because they
9823 are always constant. */
65d79d4b
SDJ
9824 {
9825 struct symbol *s = exp->elts[i + 2].symbol;
9826
9827 if (SYMBOL_CLASS (s) != LOC_BLOCK
9828 && SYMBOL_CLASS (s) != LOC_CONST
9829 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9830 return 0;
9831 break;
9832 }
9833
9834 /* The default action is to return 0 because we are using
9835 the optimistic approach here: If we don't know something,
9836 then it is not a constant. */
9837 default:
9838 return 0;
9839 }
9840 }
9841
9842 return 1;
9843}
9844
3a5c3e22
PA
9845/* Implement the "dtor" breakpoint_ops method for watchpoints. */
9846
9847static void
9848dtor_watchpoint (struct breakpoint *self)
9849{
9850 struct watchpoint *w = (struct watchpoint *) self;
9851
9852 xfree (w->cond_exp);
9853 xfree (w->exp);
9854 xfree (w->exp_string);
9855 xfree (w->exp_string_reparse);
9856 value_free (w->val);
9857
9858 base_breakpoint_ops.dtor (self);
9859}
9860
348d480f
PA
9861/* Implement the "re_set" breakpoint_ops method for watchpoints. */
9862
9863static void
9864re_set_watchpoint (struct breakpoint *b)
9865{
3a5c3e22
PA
9866 struct watchpoint *w = (struct watchpoint *) b;
9867
348d480f
PA
9868 /* Watchpoint can be either on expression using entirely global
9869 variables, or it can be on local variables.
9870
9871 Watchpoints of the first kind are never auto-deleted, and even
9872 persist across program restarts. Since they can use variables
9873 from shared libraries, we need to reparse expression as libraries
9874 are loaded and unloaded.
9875
9876 Watchpoints on local variables can also change meaning as result
9877 of solib event. For example, if a watchpoint uses both a local
9878 and a global variables in expression, it's a local watchpoint,
9879 but unloading of a shared library will make the expression
9880 invalid. This is not a very common use case, but we still
9881 re-evaluate expression, to avoid surprises to the user.
9882
9883 Note that for local watchpoints, we re-evaluate it only if
9884 watchpoints frame id is still valid. If it's not, it means the
9885 watchpoint is out of scope and will be deleted soon. In fact,
9886 I'm not sure we'll ever be called in this case.
9887
9888 If a local watchpoint's frame id is still valid, then
3a5c3e22 9889 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 9890
3a5c3e22
PA
9891 Don't do anything about disabled watchpoints, since they will be
9892 reevaluated again when enabled. */
9893 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
9894}
9895
77b06cd7
TJB
9896/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
9897
9898static int
9899insert_watchpoint (struct bp_location *bl)
9900{
3a5c3e22
PA
9901 struct watchpoint *w = (struct watchpoint *) bl->owner;
9902 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
9903
9904 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 9905 w->cond_exp);
77b06cd7
TJB
9906}
9907
9908/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
9909
9910static int
9911remove_watchpoint (struct bp_location *bl)
9912{
3a5c3e22
PA
9913 struct watchpoint *w = (struct watchpoint *) bl->owner;
9914 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
9915
9916 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 9917 w->cond_exp);
e09342b5
TJB
9918}
9919
e09342b5 9920static int
348d480f 9921breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
9922 struct address_space *aspace, CORE_ADDR bp_addr,
9923 const struct target_waitstatus *ws)
e09342b5 9924{
348d480f 9925 struct breakpoint *b = bl->owner;
3a5c3e22 9926 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 9927
348d480f
PA
9928 /* Continuable hardware watchpoints are treated as non-existent if the
9929 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9930 some data address). Otherwise gdb won't stop on a break instruction
9931 in the code (not from a breakpoint) when a hardware watchpoint has
9932 been defined. Also skip watchpoints which we know did not trigger
9933 (did not match the data address). */
9934 if (is_hardware_watchpoint (b)
3a5c3e22 9935 && w->watchpoint_triggered == watch_triggered_no)
348d480f 9936 return 0;
9c06b0b4 9937
348d480f 9938 return 1;
9c06b0b4
TJB
9939}
9940
348d480f
PA
9941static void
9942check_status_watchpoint (bpstat bs)
9c06b0b4 9943{
348d480f 9944 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 9945
348d480f 9946 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
9947}
9948
9949/* Implement the "resources_needed" breakpoint_ops method for
348d480f 9950 hardware watchpoints. */
9c06b0b4
TJB
9951
9952static int
348d480f 9953resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 9954{
3a5c3e22
PA
9955 struct watchpoint *w = (struct watchpoint *) bl->owner;
9956 int length = w->exact? 1 : bl->length;
348d480f
PA
9957
9958 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
9959}
9960
9961/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 9962 hardware watchpoints. */
9c06b0b4
TJB
9963
9964static int
348d480f 9965works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 9966{
efa80663
PA
9967 /* Read and access watchpoints only work with hardware support. */
9968 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
9969}
9970
9c06b0b4 9971static enum print_stop_action
348d480f 9972print_it_watchpoint (bpstat bs)
9c06b0b4 9973{
348d480f
PA
9974 struct cleanup *old_chain;
9975 struct breakpoint *b;
9976 const struct bp_location *bl;
f99d8bf4 9977 struct ui_file *stb;
348d480f 9978 enum print_stop_action result;
3a5c3e22 9979 struct watchpoint *w;
79a45e25 9980 struct ui_out *uiout = current_uiout;
348d480f
PA
9981
9982 gdb_assert (bs->bp_location_at != NULL);
9983
9984 bl = bs->bp_location_at;
9985 b = bs->breakpoint_at;
3a5c3e22 9986 w = (struct watchpoint *) b;
348d480f 9987
f99d8bf4
PA
9988 stb = mem_fileopen ();
9989 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
9990
9991 switch (b->type)
9992 {
348d480f 9993 case bp_watchpoint:
9c06b0b4
TJB
9994 case bp_hardware_watchpoint:
9995 annotate_watchpoint (b->number);
9996 if (ui_out_is_mi_like_p (uiout))
9997 ui_out_field_string
9998 (uiout, "reason",
9999 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10000 mention (b);
10001 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10002 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10003 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10004 ui_out_field_stream (uiout, "old", stb);
10005 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10006 watchpoint_value_print (w->val, stb);
348d480f
PA
10007 ui_out_field_stream (uiout, "new", stb);
10008 ui_out_text (uiout, "\n");
10009 /* More than one watchpoint may have been triggered. */
10010 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10011 break;
10012
10013 case bp_read_watchpoint:
10014 if (ui_out_is_mi_like_p (uiout))
10015 ui_out_field_string
10016 (uiout, "reason",
10017 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10018 mention (b);
10019 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10020 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10021 watchpoint_value_print (w->val, stb);
348d480f
PA
10022 ui_out_field_stream (uiout, "value", stb);
10023 ui_out_text (uiout, "\n");
10024 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10025 break;
10026
10027 case bp_access_watchpoint:
348d480f
PA
10028 if (bs->old_val != NULL)
10029 {
10030 annotate_watchpoint (b->number);
10031 if (ui_out_is_mi_like_p (uiout))
10032 ui_out_field_string
10033 (uiout, "reason",
10034 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10035 mention (b);
10036 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10037 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10038 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10039 ui_out_field_stream (uiout, "old", stb);
10040 ui_out_text (uiout, "\nNew value = ");
10041 }
10042 else
10043 {
10044 mention (b);
10045 if (ui_out_is_mi_like_p (uiout))
10046 ui_out_field_string
10047 (uiout, "reason",
10048 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10049 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10050 ui_out_text (uiout, "\nValue = ");
10051 }
f99d8bf4 10052 watchpoint_value_print (w->val, stb);
348d480f
PA
10053 ui_out_field_stream (uiout, "new", stb);
10054 ui_out_text (uiout, "\n");
10055 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10056 break;
10057 default:
348d480f 10058 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10059 }
10060
348d480f
PA
10061 do_cleanups (old_chain);
10062 return result;
10063}
10064
10065/* Implement the "print_mention" breakpoint_ops method for hardware
10066 watchpoints. */
10067
10068static void
10069print_mention_watchpoint (struct breakpoint *b)
10070{
10071 struct cleanup *ui_out_chain;
3a5c3e22 10072 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10073 struct ui_out *uiout = current_uiout;
348d480f
PA
10074
10075 switch (b->type)
10076 {
10077 case bp_watchpoint:
10078 ui_out_text (uiout, "Watchpoint ");
10079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10080 break;
10081 case bp_hardware_watchpoint:
10082 ui_out_text (uiout, "Hardware watchpoint ");
10083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10084 break;
10085 case bp_read_watchpoint:
10086 ui_out_text (uiout, "Hardware read watchpoint ");
10087 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10088 break;
10089 case bp_access_watchpoint:
10090 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10091 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10092 break;
10093 default:
10094 internal_error (__FILE__, __LINE__,
10095 _("Invalid hardware watchpoint type."));
10096 }
10097
10098 ui_out_field_int (uiout, "number", b->number);
10099 ui_out_text (uiout, ": ");
3a5c3e22 10100 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10101 do_cleanups (ui_out_chain);
10102}
10103
10104/* Implement the "print_recreate" breakpoint_ops method for
10105 watchpoints. */
10106
10107static void
10108print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10109{
3a5c3e22
PA
10110 struct watchpoint *w = (struct watchpoint *) b;
10111
348d480f
PA
10112 switch (b->type)
10113 {
10114 case bp_watchpoint:
10115 case bp_hardware_watchpoint:
10116 fprintf_unfiltered (fp, "watch");
10117 break;
10118 case bp_read_watchpoint:
10119 fprintf_unfiltered (fp, "rwatch");
10120 break;
10121 case bp_access_watchpoint:
10122 fprintf_unfiltered (fp, "awatch");
10123 break;
10124 default:
10125 internal_error (__FILE__, __LINE__,
10126 _("Invalid watchpoint type."));
10127 }
10128
3a5c3e22 10129 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10130 print_recreate_thread (b, fp);
348d480f
PA
10131}
10132
10133/* The breakpoint_ops structure to be used in hardware watchpoints. */
10134
2060206e 10135static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10136
10137/* Implement the "insert" breakpoint_ops method for
10138 masked hardware watchpoints. */
10139
10140static int
10141insert_masked_watchpoint (struct bp_location *bl)
10142{
3a5c3e22
PA
10143 struct watchpoint *w = (struct watchpoint *) bl->owner;
10144
10145 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10146 bl->watchpoint_type);
10147}
10148
10149/* Implement the "remove" breakpoint_ops method for
10150 masked hardware watchpoints. */
10151
10152static int
10153remove_masked_watchpoint (struct bp_location *bl)
10154{
3a5c3e22
PA
10155 struct watchpoint *w = (struct watchpoint *) bl->owner;
10156
10157 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10158 bl->watchpoint_type);
10159}
10160
10161/* Implement the "resources_needed" breakpoint_ops method for
10162 masked hardware watchpoints. */
10163
10164static int
10165resources_needed_masked_watchpoint (const struct bp_location *bl)
10166{
3a5c3e22
PA
10167 struct watchpoint *w = (struct watchpoint *) bl->owner;
10168
10169 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10170}
10171
10172/* Implement the "works_in_software_mode" breakpoint_ops method for
10173 masked hardware watchpoints. */
10174
10175static int
10176works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10177{
10178 return 0;
10179}
10180
10181/* Implement the "print_it" breakpoint_ops method for
10182 masked hardware watchpoints. */
10183
10184static enum print_stop_action
10185print_it_masked_watchpoint (bpstat bs)
10186{
10187 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10188 struct ui_out *uiout = current_uiout;
348d480f
PA
10189
10190 /* Masked watchpoints have only one location. */
10191 gdb_assert (b->loc && b->loc->next == NULL);
10192
10193 switch (b->type)
10194 {
10195 case bp_hardware_watchpoint:
10196 annotate_watchpoint (b->number);
10197 if (ui_out_is_mi_like_p (uiout))
10198 ui_out_field_string
10199 (uiout, "reason",
10200 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10201 break;
10202
10203 case bp_read_watchpoint:
10204 if (ui_out_is_mi_like_p (uiout))
10205 ui_out_field_string
10206 (uiout, "reason",
10207 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10208 break;
10209
10210 case bp_access_watchpoint:
10211 if (ui_out_is_mi_like_p (uiout))
10212 ui_out_field_string
10213 (uiout, "reason",
10214 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10215 break;
10216 default:
10217 internal_error (__FILE__, __LINE__,
10218 _("Invalid hardware watchpoint type."));
10219 }
10220
10221 mention (b);
9c06b0b4
TJB
10222 ui_out_text (uiout, _("\n\
10223Check the underlying instruction at PC for the memory\n\
10224address and value which triggered this watchpoint.\n"));
10225 ui_out_text (uiout, "\n");
10226
10227 /* More than one watchpoint may have been triggered. */
10228 return PRINT_UNKNOWN;
10229}
10230
10231/* Implement the "print_one_detail" breakpoint_ops method for
10232 masked hardware watchpoints. */
10233
10234static void
10235print_one_detail_masked_watchpoint (const struct breakpoint *b,
10236 struct ui_out *uiout)
10237{
3a5c3e22
PA
10238 struct watchpoint *w = (struct watchpoint *) b;
10239
9c06b0b4
TJB
10240 /* Masked watchpoints have only one location. */
10241 gdb_assert (b->loc && b->loc->next == NULL);
10242
10243 ui_out_text (uiout, "\tmask ");
3a5c3e22 10244 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10245 ui_out_text (uiout, "\n");
10246}
10247
10248/* Implement the "print_mention" breakpoint_ops method for
10249 masked hardware watchpoints. */
10250
10251static void
10252print_mention_masked_watchpoint (struct breakpoint *b)
10253{
3a5c3e22 10254 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10255 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10256 struct cleanup *ui_out_chain;
10257
10258 switch (b->type)
10259 {
10260 case bp_hardware_watchpoint:
10261 ui_out_text (uiout, "Masked hardware watchpoint ");
10262 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10263 break;
10264 case bp_read_watchpoint:
10265 ui_out_text (uiout, "Masked hardware read watchpoint ");
10266 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10267 break;
10268 case bp_access_watchpoint:
10269 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10270 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10271 break;
10272 default:
10273 internal_error (__FILE__, __LINE__,
10274 _("Invalid hardware watchpoint type."));
10275 }
10276
10277 ui_out_field_int (uiout, "number", b->number);
10278 ui_out_text (uiout, ": ");
3a5c3e22 10279 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10280 do_cleanups (ui_out_chain);
10281}
10282
10283/* Implement the "print_recreate" breakpoint_ops method for
10284 masked hardware watchpoints. */
10285
10286static void
10287print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10288{
3a5c3e22 10289 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10290 char tmp[40];
10291
10292 switch (b->type)
10293 {
10294 case bp_hardware_watchpoint:
10295 fprintf_unfiltered (fp, "watch");
10296 break;
10297 case bp_read_watchpoint:
10298 fprintf_unfiltered (fp, "rwatch");
10299 break;
10300 case bp_access_watchpoint:
10301 fprintf_unfiltered (fp, "awatch");
10302 break;
10303 default:
10304 internal_error (__FILE__, __LINE__,
10305 _("Invalid hardware watchpoint type."));
10306 }
10307
3a5c3e22
PA
10308 sprintf_vma (tmp, w->hw_wp_mask);
10309 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10310 print_recreate_thread (b, fp);
9c06b0b4
TJB
10311}
10312
10313/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10314
2060206e 10315static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10316
10317/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10318
10319static int
10320is_masked_watchpoint (const struct breakpoint *b)
10321{
10322 return b->ops == &masked_watchpoint_breakpoint_ops;
10323}
10324
53a5351d
JM
10325/* accessflag: hw_write: watch write,
10326 hw_read: watch read,
10327 hw_access: watch access (read or write) */
c906108c 10328static void
84f4c1fe
PM
10329watch_command_1 (char *arg, int accessflag, int from_tty,
10330 int just_location, int internal)
c906108c 10331{
a9634178 10332 volatile struct gdb_exception e;
d983da9c 10333 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10334 struct expression *exp;
60e1c644 10335 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10336 struct value *val, *mark, *result;
c906108c 10337 struct frame_info *frame;
c906108c
SS
10338 char *exp_start = NULL;
10339 char *exp_end = NULL;
9c06b0b4
TJB
10340 char *tok, *end_tok;
10341 int toklen = -1;
c906108c
SS
10342 char *cond_start = NULL;
10343 char *cond_end = NULL;
c906108c 10344 enum bptype bp_type;
37e4754d 10345 int thread = -1;
0cf6dd15 10346 int pc = 0;
9c06b0b4
TJB
10347 /* Flag to indicate whether we are going to use masks for
10348 the hardware watchpoint. */
10349 int use_mask = 0;
10350 CORE_ADDR mask = 0;
3a5c3e22 10351 struct watchpoint *w;
c906108c 10352
37e4754d
LM
10353 /* Make sure that we actually have parameters to parse. */
10354 if (arg != NULL && arg[0] != '\0')
10355 {
9c06b0b4 10356 char *value_start;
37e4754d 10357
9c06b0b4
TJB
10358 /* Look for "parameter value" pairs at the end
10359 of the arguments string. */
10360 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10361 {
10362 /* Skip whitespace at the end of the argument list. */
10363 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10364 tok--;
10365
10366 /* Find the beginning of the last token.
10367 This is the value of the parameter. */
10368 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10369 tok--;
10370 value_start = tok + 1;
10371
10372 /* Skip whitespace. */
10373 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10374 tok--;
10375
10376 end_tok = tok;
10377
10378 /* Find the beginning of the second to last token.
10379 This is the parameter itself. */
10380 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10381 tok--;
10382 tok++;
10383 toklen = end_tok - tok + 1;
10384
10385 if (toklen == 6 && !strncmp (tok, "thread", 6))
10386 {
10387 /* At this point we've found a "thread" token, which means
10388 the user is trying to set a watchpoint that triggers
10389 only in a specific thread. */
10390 char *endp;
37e4754d 10391
9c06b0b4
TJB
10392 if (thread != -1)
10393 error(_("You can specify only one thread."));
37e4754d 10394
9c06b0b4
TJB
10395 /* Extract the thread ID from the next token. */
10396 thread = strtol (value_start, &endp, 0);
37e4754d 10397
9c06b0b4
TJB
10398 /* Check if the user provided a valid numeric value for the
10399 thread ID. */
10400 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10401 error (_("Invalid thread ID specification %s."), value_start);
10402
10403 /* Check if the thread actually exists. */
10404 if (!valid_thread_id (thread))
10405 error (_("Unknown thread %d."), thread);
10406 }
10407 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10408 {
10409 /* We've found a "mask" token, which means the user wants to
10410 create a hardware watchpoint that is going to have the mask
10411 facility. */
10412 struct value *mask_value, *mark;
37e4754d 10413
9c06b0b4
TJB
10414 if (use_mask)
10415 error(_("You can specify only one mask."));
37e4754d 10416
9c06b0b4 10417 use_mask = just_location = 1;
37e4754d 10418
9c06b0b4
TJB
10419 mark = value_mark ();
10420 mask_value = parse_to_comma_and_eval (&value_start);
10421 mask = value_as_address (mask_value);
10422 value_free_to_mark (mark);
10423 }
10424 else
10425 /* We didn't recognize what we found. We should stop here. */
10426 break;
37e4754d 10427
9c06b0b4
TJB
10428 /* Truncate the string and get rid of the "parameter value" pair before
10429 the arguments string is parsed by the parse_exp_1 function. */
10430 *tok = '\0';
10431 }
37e4754d
LM
10432 }
10433
10434 /* Parse the rest of the arguments. */
c906108c
SS
10435 innermost_block = NULL;
10436 exp_start = arg;
10437 exp = parse_exp_1 (&arg, 0, 0);
10438 exp_end = arg;
fa8a61dc
TT
10439 /* Remove trailing whitespace from the expression before saving it.
10440 This makes the eventual display of the expression string a bit
10441 prettier. */
10442 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10443 --exp_end;
10444
65d79d4b
SDJ
10445 /* Checking if the expression is not constant. */
10446 if (watchpoint_exp_is_const (exp))
10447 {
10448 int len;
10449
10450 len = exp_end - exp_start;
10451 while (len > 0 && isspace (exp_start[len - 1]))
10452 len--;
10453 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10454 }
10455
c906108c
SS
10456 exp_valid_block = innermost_block;
10457 mark = value_mark ();
a1442452 10458 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
10459
10460 if (just_location)
10461 {
9c06b0b4
TJB
10462 int ret;
10463
06a64a0b 10464 exp_valid_block = NULL;
a1442452 10465 val = value_addr (result);
06a64a0b
TT
10466 release_value (val);
10467 value_free_to_mark (mark);
9c06b0b4
TJB
10468
10469 if (use_mask)
10470 {
10471 ret = target_masked_watch_num_registers (value_as_address (val),
10472 mask);
10473 if (ret == -1)
10474 error (_("This target does not support masked watchpoints."));
10475 else if (ret == -2)
10476 error (_("Invalid mask or memory region."));
10477 }
06a64a0b
TT
10478 }
10479 else if (val != NULL)
fa4727a6 10480 release_value (val);
c906108c 10481
e9cafbcc
TT
10482 tok = skip_spaces (arg);
10483 end_tok = skip_to_space (tok);
c906108c
SS
10484
10485 toklen = end_tok - tok;
10486 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10487 {
2d134ed3
PA
10488 struct expression *cond;
10489
60e1c644 10490 innermost_block = NULL;
c906108c
SS
10491 tok = cond_start = end_tok + 1;
10492 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
10493
10494 /* The watchpoint expression may not be local, but the condition
10495 may still be. E.g.: `watch global if local > 0'. */
10496 cond_exp_valid_block = innermost_block;
10497
2d134ed3 10498 xfree (cond);
c906108c
SS
10499 cond_end = tok;
10500 }
10501 if (*tok)
8a3fe4f8 10502 error (_("Junk at end of command."));
c906108c 10503
53a5351d 10504 if (accessflag == hw_read)
c5aa993b 10505 bp_type = bp_read_watchpoint;
53a5351d 10506 else if (accessflag == hw_access)
c5aa993b
JM
10507 bp_type = bp_access_watchpoint;
10508 else
10509 bp_type = bp_hardware_watchpoint;
c906108c 10510
d983da9c 10511 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
10512
10513 /* If the expression is "local", then set up a "watchpoint scope"
10514 breakpoint at the point where we've left the scope of the watchpoint
10515 expression. Create the scope breakpoint before the watchpoint, so
10516 that we will encounter it first in bpstat_stop_status. */
60e1c644 10517 if (exp_valid_block && frame)
d983da9c 10518 {
edb3359d
DJ
10519 if (frame_id_p (frame_unwind_caller_id (frame)))
10520 {
10521 scope_breakpoint
a6d9a66e
UW
10522 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10523 frame_unwind_caller_pc (frame),
06edf0c0
PA
10524 bp_watchpoint_scope,
10525 &momentary_breakpoint_ops);
d983da9c 10526
edb3359d 10527 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10528
edb3359d
DJ
10529 /* Automatically delete the breakpoint when it hits. */
10530 scope_breakpoint->disposition = disp_del;
d983da9c 10531
edb3359d
DJ
10532 /* Only break in the proper frame (help with recursion). */
10533 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 10534
edb3359d 10535 /* Set the address at which we will stop. */
a6d9a66e
UW
10536 scope_breakpoint->loc->gdbarch
10537 = frame_unwind_caller_arch (frame);
edb3359d
DJ
10538 scope_breakpoint->loc->requested_address
10539 = frame_unwind_caller_pc (frame);
10540 scope_breakpoint->loc->address
a6d9a66e
UW
10541 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10542 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10543 scope_breakpoint->type);
10544 }
d983da9c
DJ
10545 }
10546
c906108c 10547 /* Now set up the breakpoint. */
3a5c3e22
PA
10548
10549 w = XCNEW (struct watchpoint);
10550 b = &w->base;
348d480f 10551 if (use_mask)
3a5c3e22
PA
10552 init_raw_breakpoint_without_location (b, NULL, bp_type,
10553 &masked_watchpoint_breakpoint_ops);
348d480f 10554 else
3a5c3e22
PA
10555 init_raw_breakpoint_without_location (b, NULL, bp_type,
10556 &watchpoint_breakpoint_ops);
37e4754d 10557 b->thread = thread;
b5de0fa7 10558 b->disposition = disp_donttouch;
348d480f 10559 b->pspace = current_program_space;
3a5c3e22
PA
10560 w->exp = exp;
10561 w->exp_valid_block = exp_valid_block;
10562 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10563 if (just_location)
10564 {
10565 struct type *t = value_type (val);
10566 CORE_ADDR addr = value_as_address (val);
10567 char *name;
10568
10569 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10570 name = type_to_string (t);
10571
3a5c3e22 10572 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 10573 core_addr_to_string (addr));
06a64a0b
TT
10574 xfree (name);
10575
3a5c3e22 10576 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
10577 (int) (exp_end - exp_start), exp_start);
10578
06a64a0b
TT
10579 /* The above expression is in C. */
10580 b->language = language_c;
10581 }
10582 else
3a5c3e22 10583 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10584
10585 if (use_mask)
10586 {
3a5c3e22 10587 w->hw_wp_mask = mask;
9c06b0b4
TJB
10588 }
10589 else
10590 {
3a5c3e22
PA
10591 w->val = val;
10592 w->val_valid = 1;
9c06b0b4 10593 }
77b06cd7 10594
c906108c
SS
10595 if (cond_start)
10596 b->cond_string = savestring (cond_start, cond_end - cond_start);
10597 else
10598 b->cond_string = 0;
c5aa993b 10599
c906108c 10600 if (frame)
f6bc2008 10601 {
3a5c3e22
PA
10602 w->watchpoint_frame = get_frame_id (frame);
10603 w->watchpoint_thread = inferior_ptid;
f6bc2008 10604 }
c906108c 10605 else
f6bc2008 10606 {
3a5c3e22
PA
10607 w->watchpoint_frame = null_frame_id;
10608 w->watchpoint_thread = null_ptid;
f6bc2008 10609 }
c906108c 10610
d983da9c 10611 if (scope_breakpoint != NULL)
c906108c 10612 {
d983da9c
DJ
10613 /* The scope breakpoint is related to the watchpoint. We will
10614 need to act on them together. */
10615 b->related_breakpoint = scope_breakpoint;
10616 scope_breakpoint->related_breakpoint = b;
c906108c 10617 }
d983da9c 10618
06a64a0b
TT
10619 if (!just_location)
10620 value_free_to_mark (mark);
2d134ed3 10621
a9634178
TJB
10622 TRY_CATCH (e, RETURN_MASK_ALL)
10623 {
10624 /* Finally update the new watchpoint. This creates the locations
10625 that should be inserted. */
3a5c3e22 10626 update_watchpoint (w, 1);
a9634178
TJB
10627 }
10628 if (e.reason < 0)
10629 {
10630 delete_breakpoint (b);
10631 throw_exception (e);
10632 }
10633
3ea46bff 10634 install_breakpoint (internal, b, 1);
c906108c
SS
10635}
10636
e09342b5 10637/* Return count of debug registers needed to watch the given expression.
e09342b5 10638 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10639
c906108c 10640static int
a9634178 10641can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10642{
10643 int found_memory_cnt = 0;
2e70b7b9 10644 struct value *head = v;
c906108c
SS
10645
10646 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10647 if (!can_use_hw_watchpoints)
c906108c 10648 return 0;
c5aa993b 10649
5c44784c
JM
10650 /* Make sure that the value of the expression depends only upon
10651 memory contents, and values computed from them within GDB. If we
10652 find any register references or function calls, we can't use a
10653 hardware watchpoint.
10654
10655 The idea here is that evaluating an expression generates a series
10656 of values, one holding the value of every subexpression. (The
10657 expression a*b+c has five subexpressions: a, b, a*b, c, and
10658 a*b+c.) GDB's values hold almost enough information to establish
10659 the criteria given above --- they identify memory lvalues,
10660 register lvalues, computed values, etcetera. So we can evaluate
10661 the expression, and then scan the chain of values that leaves
10662 behind to decide whether we can detect any possible change to the
10663 expression's final value using only hardware watchpoints.
10664
10665 However, I don't think that the values returned by inferior
10666 function calls are special in any way. So this function may not
10667 notice that an expression involving an inferior function call
10668 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10669 for (; v; v = value_next (v))
c906108c 10670 {
5c44784c 10671 if (VALUE_LVAL (v) == lval_memory)
c906108c 10672 {
8464be76
DJ
10673 if (v != head && value_lazy (v))
10674 /* A lazy memory lvalue in the chain is one that GDB never
10675 needed to fetch; we either just used its address (e.g.,
10676 `a' in `a.b') or we never needed it at all (e.g., `a'
10677 in `a,b'). This doesn't apply to HEAD; if that is
10678 lazy then it was not readable, but watch it anyway. */
5c44784c 10679 ;
53a5351d 10680 else
5c44784c
JM
10681 {
10682 /* Ahh, memory we actually used! Check if we can cover
10683 it with hardware watchpoints. */
df407dfe 10684 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10685
10686 /* We only watch structs and arrays if user asked for it
10687 explicitly, never if they just happen to appear in a
10688 middle of some value chain. */
10689 if (v == head
10690 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10691 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10692 {
42ae5230 10693 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10694 int len;
10695 int num_regs;
10696
a9634178 10697 len = (target_exact_watchpoints
e09342b5
TJB
10698 && is_scalar_type_recursive (vtype))?
10699 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10700
e09342b5
TJB
10701 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10702 if (!num_regs)
2e70b7b9
MS
10703 return 0;
10704 else
e09342b5 10705 found_memory_cnt += num_regs;
2e70b7b9 10706 }
5c44784c 10707 }
c5aa993b 10708 }
5086187c
AC
10709 else if (VALUE_LVAL (v) != not_lval
10710 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10711 return 0; /* These are values from the history (e.g., $1). */
5086187c 10712 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10713 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10714 }
10715
10716 /* The expression itself looks suitable for using a hardware
10717 watchpoint, but give the target machine a chance to reject it. */
10718 return found_memory_cnt;
10719}
10720
8b93c638 10721void
84f4c1fe 10722watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10723{
84f4c1fe 10724 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10725}
10726
10727/* A helper function that looks for an argument at the start of a
10728 string. The argument must also either be at the end of the string,
10729 or be followed by whitespace. Returns 1 if it finds the argument,
10730 0 otherwise. If the argument is found, it updates *STR. */
10731
10732static int
10733check_for_argument (char **str, char *arg, int arg_len)
10734{
10735 if (strncmp (*str, arg, arg_len) == 0
10736 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10737 {
10738 *str += arg_len;
10739 return 1;
10740 }
10741 return 0;
10742}
10743
10744/* A helper function that looks for the "-location" argument and then
10745 calls watch_command_1. */
10746
10747static void
10748watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10749{
10750 int just_location = 0;
10751
10752 if (arg
10753 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10754 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10755 {
e9cafbcc 10756 arg = skip_spaces (arg);
06a64a0b
TT
10757 just_location = 1;
10758 }
10759
84f4c1fe 10760 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10761}
8926118c 10762
c5aa993b 10763static void
fba45db2 10764watch_command (char *arg, int from_tty)
c906108c 10765{
06a64a0b 10766 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10767}
10768
8b93c638 10769void
84f4c1fe 10770rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10771{
84f4c1fe 10772 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10773}
8926118c 10774
c5aa993b 10775static void
fba45db2 10776rwatch_command (char *arg, int from_tty)
c906108c 10777{
06a64a0b 10778 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10779}
10780
8b93c638 10781void
84f4c1fe 10782awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10783{
84f4c1fe 10784 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10785}
8926118c 10786
c5aa993b 10787static void
fba45db2 10788awatch_command (char *arg, int from_tty)
c906108c 10789{
06a64a0b 10790 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10791}
c906108c 10792\f
c5aa993b 10793
43ff13b4 10794/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
10795 because it uses the mechanisms of breakpoints. */
10796
bfec99b2
PA
10797struct until_break_command_continuation_args
10798{
10799 struct breakpoint *breakpoint;
10800 struct breakpoint *breakpoint2;
186c406b 10801 int thread_num;
bfec99b2
PA
10802};
10803
43ff13b4 10804/* This function is called by fetch_inferior_event via the
4a64f543 10805 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 10806 care of cleaning up the temporary breakpoints set up by the until
4a64f543 10807 command. */
c2c6d25f 10808static void
fa4cd53f 10809until_break_command_continuation (void *arg, int err)
43ff13b4 10810{
bfec99b2
PA
10811 struct until_break_command_continuation_args *a = arg;
10812
10813 delete_breakpoint (a->breakpoint);
10814 if (a->breakpoint2)
10815 delete_breakpoint (a->breakpoint2);
186c406b 10816 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
10817}
10818
c906108c 10819void
ae66c1fc 10820until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
10821{
10822 struct symtabs_and_lines sals;
10823 struct symtab_and_line sal;
206415a3 10824 struct frame_info *frame = get_selected_frame (NULL);
883bc8d1
PA
10825 struct gdbarch *frame_gdbarch = get_frame_arch (frame);
10826 struct frame_id stack_frame_id = get_stack_frame_id (frame);
10827 struct frame_id caller_frame_id = frame_unwind_caller_id (frame);
c906108c 10828 struct breakpoint *breakpoint;
f107f563 10829 struct breakpoint *breakpoint2 = NULL;
c906108c 10830 struct cleanup *old_chain;
186c406b
TT
10831 int thread;
10832 struct thread_info *tp;
c906108c
SS
10833
10834 clear_proceed_status ();
10835
10836 /* Set a breakpoint where the user wants it and at return from
4a64f543 10837 this function. */
c5aa993b 10838
1bfeeb0f 10839 if (last_displayed_sal_is_valid ())
f8eba3c6 10840 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 10841 get_last_displayed_symtab (),
f8eba3c6 10842 get_last_displayed_line ());
c906108c 10843 else
f8eba3c6
TT
10844 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10845 (struct symtab *) NULL, 0);
c5aa993b 10846
c906108c 10847 if (sals.nelts != 1)
8a3fe4f8 10848 error (_("Couldn't get information on specified line."));
c5aa993b 10849
c906108c 10850 sal = sals.sals[0];
4a64f543 10851 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 10852
c906108c 10853 if (*arg)
8a3fe4f8 10854 error (_("Junk at end of arguments."));
c5aa993b 10855
c906108c 10856 resolve_sal_pc (&sal);
c5aa993b 10857
186c406b
TT
10858 tp = inferior_thread ();
10859 thread = tp->num;
10860
883bc8d1
PA
10861 old_chain = make_cleanup (null_cleanup, NULL);
10862
10863 /* Installing a breakpoint invalidates the frame chain (as it may
10864 need to switch threads), so do any frame handling first. */
10865
ae66c1fc
EZ
10866 /* Keep within the current frame, or in frames called by the current
10867 one. */
edb3359d 10868
883bc8d1 10869 if (frame_id_p (caller_frame_id))
c906108c 10870 {
883bc8d1
PA
10871 struct symtab_and_line sal2;
10872
10873 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10874 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 10875 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
10876 sal2,
10877 caller_frame_id,
f107f563
VP
10878 bp_until);
10879 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 10880
883bc8d1 10881 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 10882 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 10883 }
c5aa993b 10884
c70a6932
JK
10885 /* set_momentary_breakpoint could invalidate FRAME. */
10886 frame = NULL;
10887
883bc8d1
PA
10888 if (anywhere)
10889 /* If the user told us to continue until a specified location,
10890 we don't specify a frame at which we need to stop. */
10891 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10892 null_frame_id, bp_until);
10893 else
10894 /* Otherwise, specify the selected frame, because we want to stop
10895 only at the very same frame. */
10896 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10897 stack_frame_id, bp_until);
10898 make_cleanup_delete_breakpoint (breakpoint);
10899
c906108c 10900 proceed (-1, TARGET_SIGNAL_DEFAULT, 0);
f107f563 10901
4a64f543
MS
10902 /* If we are running asynchronously, and proceed call above has
10903 actually managed to start the target, arrange for breakpoints to
10904 be deleted when the target stops. Otherwise, we're already
10905 stopped and delete breakpoints via cleanup chain. */
f107f563 10906
8ea051c5 10907 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 10908 {
bfec99b2
PA
10909 struct until_break_command_continuation_args *args;
10910 args = xmalloc (sizeof (*args));
f107f563 10911
bfec99b2
PA
10912 args->breakpoint = breakpoint;
10913 args->breakpoint2 = breakpoint2;
186c406b 10914 args->thread_num = thread;
f107f563
VP
10915
10916 discard_cleanups (old_chain);
95e54da7
PA
10917 add_continuation (inferior_thread (),
10918 until_break_command_continuation, args,
604ead4a 10919 xfree);
f107f563
VP
10920 }
10921 else
c5aa993b 10922 do_cleanups (old_chain);
c906108c 10923}
ae66c1fc 10924
c906108c
SS
10925/* This function attempts to parse an optional "if <cond>" clause
10926 from the arg string. If one is not found, it returns NULL.
c5aa993b 10927
c906108c
SS
10928 Else, it returns a pointer to the condition string. (It does not
10929 attempt to evaluate the string against a particular block.) And,
10930 it updates arg to point to the first character following the parsed
4a64f543 10931 if clause in the arg string. */
53a5351d 10932
c906108c 10933static char *
fba45db2 10934ep_parse_optional_if_clause (char **arg)
c906108c 10935{
c5aa993b
JM
10936 char *cond_string;
10937
10938 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 10939 return NULL;
c5aa993b 10940
4a64f543 10941 /* Skip the "if" keyword. */
c906108c 10942 (*arg) += 2;
c5aa993b 10943
c906108c 10944 /* Skip any extra leading whitespace, and record the start of the
4a64f543 10945 condition string. */
e9cafbcc 10946 *arg = skip_spaces (*arg);
c906108c 10947 cond_string = *arg;
c5aa993b 10948
4a64f543
MS
10949 /* Assume that the condition occupies the remainder of the arg
10950 string. */
c906108c 10951 (*arg) += strlen (cond_string);
c5aa993b 10952
c906108c
SS
10953 return cond_string;
10954}
c5aa993b 10955
c906108c
SS
10956/* Commands to deal with catching events, such as signals, exceptions,
10957 process start/exit, etc. */
c5aa993b
JM
10958
10959typedef enum
10960{
44feb3ce
TT
10961 catch_fork_temporary, catch_vfork_temporary,
10962 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
10963}
10964catch_fork_kind;
10965
c906108c 10966static void
cc59ec59
MS
10967catch_fork_command_1 (char *arg, int from_tty,
10968 struct cmd_list_element *command)
c906108c 10969{
a6d9a66e 10970 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 10971 char *cond_string = NULL;
44feb3ce
TT
10972 catch_fork_kind fork_kind;
10973 int tempflag;
10974
10975 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
10976 tempflag = (fork_kind == catch_fork_temporary
10977 || fork_kind == catch_vfork_temporary);
c5aa993b 10978
44feb3ce
TT
10979 if (!arg)
10980 arg = "";
e9cafbcc 10981 arg = skip_spaces (arg);
c5aa993b 10982
c906108c 10983 /* The allowed syntax is:
c5aa993b
JM
10984 catch [v]fork
10985 catch [v]fork if <cond>
10986
4a64f543 10987 First, check if there's an if clause. */
c906108c 10988 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 10989
c906108c 10990 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 10991 error (_("Junk at end of arguments."));
c5aa993b 10992
c906108c 10993 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 10994 and enable reporting of such events. */
c5aa993b
JM
10995 switch (fork_kind)
10996 {
44feb3ce
TT
10997 case catch_fork_temporary:
10998 case catch_fork_permanent:
a6d9a66e 10999 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11000 &catch_fork_breakpoint_ops);
c906108c 11001 break;
44feb3ce
TT
11002 case catch_vfork_temporary:
11003 case catch_vfork_permanent:
a6d9a66e 11004 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11005 &catch_vfork_breakpoint_ops);
c906108c 11006 break;
c5aa993b 11007 default:
8a3fe4f8 11008 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11009 break;
c5aa993b 11010 }
c906108c
SS
11011}
11012
11013static void
cc59ec59
MS
11014catch_exec_command_1 (char *arg, int from_tty,
11015 struct cmd_list_element *command)
c906108c 11016{
b4d90040 11017 struct exec_catchpoint *c;
a6d9a66e 11018 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11019 int tempflag;
c5aa993b 11020 char *cond_string = NULL;
c906108c 11021
44feb3ce
TT
11022 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11023
11024 if (!arg)
11025 arg = "";
e9cafbcc 11026 arg = skip_spaces (arg);
c906108c
SS
11027
11028 /* The allowed syntax is:
c5aa993b
JM
11029 catch exec
11030 catch exec if <cond>
c906108c 11031
4a64f543 11032 First, check if there's an if clause. */
c906108c
SS
11033 cond_string = ep_parse_optional_if_clause (&arg);
11034
11035 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11036 error (_("Junk at end of arguments."));
c906108c 11037
b4d90040
PA
11038 c = XNEW (struct exec_catchpoint);
11039 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11040 &catch_exec_breakpoint_ops);
11041 c->exec_pathname = NULL;
11042
3ea46bff 11043 install_breakpoint (0, &c->base, 1);
c906108c 11044}
c5aa993b 11045
3086aeae 11046static enum print_stop_action
348d480f 11047print_it_exception_catchpoint (bpstat bs)
3086aeae 11048{
79a45e25 11049 struct ui_out *uiout = current_uiout;
348d480f 11050 struct breakpoint *b = bs->breakpoint_at;
ade92717 11051 int bp_temp, bp_throw;
3086aeae 11052
ade92717 11053 annotate_catchpoint (b->number);
3086aeae 11054
ade92717
AR
11055 bp_throw = strstr (b->addr_string, "throw") != NULL;
11056 if (b->loc->address != b->loc->requested_address)
11057 breakpoint_adjustment_warning (b->loc->requested_address,
11058 b->loc->address,
11059 b->number, 1);
df2b6d2d 11060 bp_temp = b->disposition == disp_del;
ade92717
AR
11061 ui_out_text (uiout,
11062 bp_temp ? "Temporary catchpoint "
11063 : "Catchpoint ");
11064 if (!ui_out_is_mi_like_p (uiout))
11065 ui_out_field_int (uiout, "bkptno", b->number);
11066 ui_out_text (uiout,
c0b37c48
AR
11067 bp_throw ? " (exception thrown), "
11068 : " (exception caught), ");
ade92717
AR
11069 if (ui_out_is_mi_like_p (uiout))
11070 {
11071 ui_out_field_string (uiout, "reason",
11072 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11073 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11074 ui_out_field_int (uiout, "bkptno", b->number);
11075 }
3086aeae
DJ
11076 return PRINT_SRC_AND_LOC;
11077}
11078
11079static void
cc59ec59
MS
11080print_one_exception_catchpoint (struct breakpoint *b,
11081 struct bp_location **last_loc)
3086aeae 11082{
79a45b7d 11083 struct value_print_options opts;
79a45e25 11084 struct ui_out *uiout = current_uiout;
cc59ec59 11085
79a45b7d
TT
11086 get_user_print_options (&opts);
11087 if (opts.addressprint)
3086aeae
DJ
11088 {
11089 annotate_field (4);
604133b5
AR
11090 if (b->loc == NULL || b->loc->shlib_disabled)
11091 ui_out_field_string (uiout, "addr", "<PENDING>");
11092 else
5af949e3
UW
11093 ui_out_field_core_addr (uiout, "addr",
11094 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
11095 }
11096 annotate_field (5);
604133b5 11097 if (b->loc)
a6d9a66e 11098 *last_loc = b->loc;
3086aeae
DJ
11099 if (strstr (b->addr_string, "throw") != NULL)
11100 ui_out_field_string (uiout, "what", "exception throw");
11101 else
11102 ui_out_field_string (uiout, "what", "exception catch");
11103}
11104
11105static void
11106print_mention_exception_catchpoint (struct breakpoint *b)
11107{
79a45e25 11108 struct ui_out *uiout = current_uiout;
ade92717
AR
11109 int bp_temp;
11110 int bp_throw;
11111
df2b6d2d 11112 bp_temp = b->disposition == disp_del;
ade92717
AR
11113 bp_throw = strstr (b->addr_string, "throw") != NULL;
11114 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11115 : _("Catchpoint "));
11116 ui_out_field_int (uiout, "bkptno", b->number);
11117 ui_out_text (uiout, bp_throw ? _(" (throw)")
11118 : _(" (catch)"));
3086aeae
DJ
11119}
11120
6149aea9
PA
11121/* Implement the "print_recreate" breakpoint_ops method for throw and
11122 catch catchpoints. */
11123
11124static void
4a64f543
MS
11125print_recreate_exception_catchpoint (struct breakpoint *b,
11126 struct ui_file *fp)
6149aea9
PA
11127{
11128 int bp_temp;
11129 int bp_throw;
11130
11131 bp_temp = b->disposition == disp_del;
11132 bp_throw = strstr (b->addr_string, "throw") != NULL;
11133 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11134 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 11135 print_recreate_thread (b, fp);
6149aea9
PA
11136}
11137
2060206e 11138static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
11139
11140static int
11141handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11142 enum exception_event_kind ex_event, int from_tty)
11143{
604133b5
AR
11144 char *trigger_func_name;
11145
3086aeae 11146 if (ex_event == EX_EVENT_CATCH)
604133b5 11147 trigger_func_name = "__cxa_begin_catch";
3086aeae 11148 else
604133b5 11149 trigger_func_name = "__cxa_throw";
3086aeae 11150
8cdf0e15 11151 create_breakpoint (get_current_arch (),
e7e0cddf 11152 trigger_func_name, cond_string, -1, NULL,
8cdf0e15 11153 0 /* condition and thread are valid. */,
0fb4aa4b 11154 tempflag, bp_breakpoint,
8cdf0e15
VP
11155 0,
11156 AUTO_BOOLEAN_TRUE /* pending */,
11157 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe 11158 1 /* enabled */,
44f238bb
PA
11159 0 /* internal */,
11160 0);
3086aeae 11161
3086aeae
DJ
11162 return 1;
11163}
11164
4a64f543 11165/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
11166
11167static void
fba45db2
KB
11168catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11169 int tempflag, int from_tty)
c906108c 11170{
c5aa993b 11171 char *cond_string = NULL;
c5aa993b 11172
44feb3ce
TT
11173 if (!arg)
11174 arg = "";
e9cafbcc 11175 arg = skip_spaces (arg);
c5aa993b 11176
c906108c
SS
11177 cond_string = ep_parse_optional_if_clause (&arg);
11178
11179 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11180 error (_("Junk at end of arguments."));
c906108c 11181
059fb39f
PM
11182 if (ex_event != EX_EVENT_THROW
11183 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 11184 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 11185
3086aeae
DJ
11186 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11187 return;
11188
8a3fe4f8 11189 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
11190}
11191
44feb3ce
TT
11192/* Implementation of "catch catch" command. */
11193
11194static void
11195catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11196{
11197 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11198
44feb3ce
TT
11199 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11200}
11201
11202/* Implementation of "catch throw" command. */
11203
11204static void
11205catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11206{
11207 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11208
44feb3ce
TT
11209 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11210}
11211
9ac4176b 11212void
28010a5d
PA
11213init_ada_exception_breakpoint (struct breakpoint *b,
11214 struct gdbarch *gdbarch,
11215 struct symtab_and_line sal,
11216 char *addr_string,
c0a91b2b 11217 const struct breakpoint_ops *ops,
28010a5d
PA
11218 int tempflag,
11219 int from_tty)
f7f9143b 11220{
f7f9143b
JB
11221 if (from_tty)
11222 {
5af949e3
UW
11223 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11224 if (!loc_gdbarch)
11225 loc_gdbarch = gdbarch;
11226
6c95b8df
PA
11227 describe_other_breakpoints (loc_gdbarch,
11228 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11229 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11230 version for exception catchpoints, because two catchpoints
11231 used for different exception names will use the same address.
11232 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11233 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11234 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11235 the user what type of catchpoint it is. The above is good
11236 enough for now, though. */
11237 }
11238
28010a5d 11239 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11240
11241 b->enable_state = bp_enabled;
11242 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11243 b->addr_string = addr_string;
11244 b->language = language_ada;
f7f9143b
JB
11245}
11246
a96d9b2e
SDJ
11247/* Splits the argument using space as delimiter. Returns an xmalloc'd
11248 filter list, or NULL if no filtering is required. */
11249static VEC(int) *
11250catch_syscall_split_args (char *arg)
11251{
11252 VEC(int) *result = NULL;
29d0bb3d 11253 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11254
11255 while (*arg != '\0')
11256 {
11257 int i, syscall_number;
11258 char *endptr;
11259 char cur_name[128];
11260 struct syscall s;
11261
11262 /* Skip whitespace. */
11263 while (isspace (*arg))
11264 arg++;
11265
11266 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11267 cur_name[i] = arg[i];
11268 cur_name[i] = '\0';
11269 arg += i;
11270
11271 /* Check if the user provided a syscall name or a number. */
11272 syscall_number = (int) strtol (cur_name, &endptr, 0);
11273 if (*endptr == '\0')
bccd0dd2 11274 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11275 else
11276 {
11277 /* We have a name. Let's check if it's valid and convert it
11278 to a number. */
11279 get_syscall_by_name (cur_name, &s);
11280
11281 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11282 /* Here we have to issue an error instead of a warning,
11283 because GDB cannot do anything useful if there's no
11284 syscall number to be caught. */
a96d9b2e
SDJ
11285 error (_("Unknown syscall name '%s'."), cur_name);
11286 }
11287
11288 /* Ok, it's valid. */
11289 VEC_safe_push (int, result, s.number);
11290 }
11291
11292 discard_cleanups (cleanup);
11293 return result;
11294}
11295
11296/* Implement the "catch syscall" command. */
11297
11298static void
cc59ec59
MS
11299catch_syscall_command_1 (char *arg, int from_tty,
11300 struct cmd_list_element *command)
a96d9b2e
SDJ
11301{
11302 int tempflag;
11303 VEC(int) *filter;
11304 struct syscall s;
11305 struct gdbarch *gdbarch = get_current_arch ();
11306
11307 /* Checking if the feature if supported. */
11308 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11309 error (_("The feature 'catch syscall' is not supported on \
ea666128 11310this architecture yet."));
a96d9b2e
SDJ
11311
11312 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11313
e9cafbcc 11314 arg = skip_spaces (arg);
a96d9b2e
SDJ
11315
11316 /* We need to do this first "dummy" translation in order
11317 to get the syscall XML file loaded or, most important,
11318 to display a warning to the user if there's no XML file
11319 for his/her architecture. */
11320 get_syscall_by_number (0, &s);
11321
11322 /* The allowed syntax is:
11323 catch syscall
11324 catch syscall <name | number> [<name | number> ... <name | number>]
11325
11326 Let's check if there's a syscall name. */
11327
11328 if (arg != NULL)
11329 filter = catch_syscall_split_args (arg);
11330 else
11331 filter = NULL;
11332
11333 create_syscall_event_catchpoint (tempflag, filter,
11334 &catch_syscall_breakpoint_ops);
11335}
11336
c906108c 11337static void
fba45db2 11338catch_command (char *arg, int from_tty)
c906108c 11339{
44feb3ce 11340 error (_("Catch requires an event name."));
c906108c
SS
11341}
11342\f
11343
11344static void
fba45db2 11345tcatch_command (char *arg, int from_tty)
c906108c 11346{
44feb3ce 11347 error (_("Catch requires an event name."));
c906108c
SS
11348}
11349
8a2c437b
TT
11350/* A qsort comparison function that sorts breakpoints in order. */
11351
11352static int
11353compare_breakpoints (const void *a, const void *b)
11354{
11355 const breakpoint_p *ba = a;
11356 uintptr_t ua = (uintptr_t) *ba;
11357 const breakpoint_p *bb = b;
11358 uintptr_t ub = (uintptr_t) *bb;
11359
11360 if ((*ba)->number < (*bb)->number)
11361 return -1;
11362 else if ((*ba)->number > (*bb)->number)
11363 return 1;
11364
11365 /* Now sort by address, in case we see, e..g, two breakpoints with
11366 the number 0. */
11367 if (ua < ub)
11368 return -1;
11369 return ub > ub ? 1 : 0;
11370}
11371
80f8a6eb 11372/* Delete breakpoints by address or line. */
c906108c
SS
11373
11374static void
fba45db2 11375clear_command (char *arg, int from_tty)
c906108c 11376{
8a2c437b 11377 struct breakpoint *b, *prev;
d6e956e5
VP
11378 VEC(breakpoint_p) *found = 0;
11379 int ix;
c906108c
SS
11380 int default_match;
11381 struct symtabs_and_lines sals;
11382 struct symtab_and_line sal;
c906108c 11383 int i;
8a2c437b 11384 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11385
11386 if (arg)
11387 {
f8eba3c6
TT
11388 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11389 | DECODE_LINE_LIST_MODE));
c906108c
SS
11390 default_match = 0;
11391 }
11392 else
11393 {
c5aa993b 11394 sals.sals = (struct symtab_and_line *)
c906108c 11395 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11396 make_cleanup (xfree, sals.sals);
4a64f543 11397 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11398
11399 /* Set sal's line, symtab, pc, and pspace to the values
11400 corresponding to the last call to print_frame_info. If the
11401 codepoint is not valid, this will set all the fields to 0. */
11402 get_last_displayed_sal (&sal);
c906108c 11403 if (sal.symtab == 0)
8a3fe4f8 11404 error (_("No source file specified."));
c906108c
SS
11405
11406 sals.sals[0] = sal;
11407 sals.nelts = 1;
11408
11409 default_match = 1;
11410 }
11411
4a64f543
MS
11412 /* We don't call resolve_sal_pc here. That's not as bad as it
11413 seems, because all existing breakpoints typically have both
11414 file/line and pc set. So, if clear is given file/line, we can
11415 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11416
11417 We only support clearing given the address explicitly
11418 present in breakpoint table. Say, we've set breakpoint
4a64f543 11419 at file:line. There were several PC values for that file:line,
ed0616c6 11420 due to optimization, all in one block.
4a64f543
MS
11421
11422 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11423 PC corresponding to the same file:line, the breakpoint won't
11424 be cleared. We probably can still clear the breakpoint, but
11425 since the other PC value is never presented to user, user
11426 can only find it by guessing, and it does not seem important
11427 to support that. */
11428
4a64f543
MS
11429 /* For each line spec given, delete bps which correspond to it. Do
11430 it in two passes, solely to preserve the current behavior that
11431 from_tty is forced true if we delete more than one
11432 breakpoint. */
c906108c 11433
80f8a6eb 11434 found = NULL;
8a2c437b 11435 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11436 for (i = 0; i < sals.nelts; i++)
11437 {
4aac40c8
TT
11438 int is_abs, sal_name_len;
11439
c906108c 11440 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11441 If line given (pc == 0), clear all bpts on specified line.
11442 If defaulting, clear all bpts on default line
c906108c 11443 or at default pc.
c5aa993b
JM
11444
11445 defaulting sal.pc != 0 tests to do
11446
11447 0 1 pc
11448 1 1 pc _and_ line
11449 0 0 line
11450 1 0 <can't happen> */
c906108c
SS
11451
11452 sal = sals.sals[i];
4aac40c8
TT
11453 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11454 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 11455
4a64f543 11456 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11457 ALL_BREAKPOINTS (b)
c5aa993b 11458 {
0d381245 11459 int match = 0;
4a64f543 11460 /* Are we going to delete b? */
cc60f2e3 11461 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11462 {
11463 struct bp_location *loc = b->loc;
11464 for (; loc; loc = loc->next)
11465 {
f8eba3c6
TT
11466 /* If the user specified file:line, don't allow a PC
11467 match. This matches historical gdb behavior. */
11468 int pc_match = (!sal.explicit_line
11469 && sal.pc
11470 && (loc->pspace == sal.pspace)
11471 && (loc->address == sal.pc)
11472 && (!section_is_overlay (loc->section)
11473 || loc->section == sal.section));
4aac40c8
TT
11474 int line_match = 0;
11475
11476 if ((default_match || sal.explicit_line)
11477 && loc->source_file != NULL
11478 && sal.symtab != NULL
11479 && sal.pspace == loc->pspace
11480 && loc->line_number == sal.line)
11481 {
11482 if (filename_cmp (loc->source_file,
11483 sal.symtab->filename) == 0)
11484 line_match = 1;
11485 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11486 && compare_filenames_for_search (loc->source_file,
11487 sal.symtab->filename,
11488 sal_name_len))
11489 line_match = 1;
11490 }
11491
0d381245
VP
11492 if (pc_match || line_match)
11493 {
11494 match = 1;
11495 break;
11496 }
11497 }
11498 }
11499
11500 if (match)
d6e956e5 11501 VEC_safe_push(breakpoint_p, found, b);
c906108c 11502 }
80f8a6eb 11503 }
8a2c437b 11504
80f8a6eb 11505 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11506 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11507 {
11508 if (arg)
8a3fe4f8 11509 error (_("No breakpoint at %s."), arg);
80f8a6eb 11510 else
8a3fe4f8 11511 error (_("No breakpoint at this line."));
80f8a6eb 11512 }
c906108c 11513
8a2c437b
TT
11514 /* Remove duplicates from the vec. */
11515 qsort (VEC_address (breakpoint_p, found),
11516 VEC_length (breakpoint_p, found),
11517 sizeof (breakpoint_p),
11518 compare_breakpoints);
11519 prev = VEC_index (breakpoint_p, found, 0);
11520 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11521 {
11522 if (b == prev)
11523 {
11524 VEC_ordered_remove (breakpoint_p, found, ix);
11525 --ix;
11526 }
11527 }
11528
d6e956e5 11529 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11530 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11531 if (from_tty)
a3f17187 11532 {
d6e956e5 11533 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11534 printf_unfiltered (_("Deleted breakpoint "));
11535 else
11536 printf_unfiltered (_("Deleted breakpoints "));
11537 }
80f8a6eb 11538 breakpoints_changed ();
d6e956e5
VP
11539
11540 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11541 {
c5aa993b 11542 if (from_tty)
d6e956e5
VP
11543 printf_unfiltered ("%d ", b->number);
11544 delete_breakpoint (b);
c906108c 11545 }
80f8a6eb
MS
11546 if (from_tty)
11547 putchar_unfiltered ('\n');
8a2c437b
TT
11548
11549 do_cleanups (cleanups);
c906108c
SS
11550}
11551\f
11552/* Delete breakpoint in BS if they are `delete' breakpoints and
11553 all breakpoints that are marked for deletion, whether hit or not.
11554 This is called after any breakpoint is hit, or after errors. */
11555
11556void
fba45db2 11557breakpoint_auto_delete (bpstat bs)
c906108c 11558{
35df4500 11559 struct breakpoint *b, *b_tmp;
c906108c
SS
11560
11561 for (; bs; bs = bs->next)
f431efe5
PA
11562 if (bs->breakpoint_at
11563 && bs->breakpoint_at->disposition == disp_del
c906108c 11564 && bs->stop)
f431efe5 11565 delete_breakpoint (bs->breakpoint_at);
c906108c 11566
35df4500 11567 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11568 {
b5de0fa7 11569 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11570 delete_breakpoint (b);
11571 }
c906108c
SS
11572}
11573
4a64f543
MS
11574/* A comparison function for bp_location AP and BP being interfaced to
11575 qsort. Sort elements primarily by their ADDRESS (no matter what
11576 does breakpoint_address_is_meaningful say for its OWNER),
11577 secondarily by ordering first bp_permanent OWNERed elements and
11578 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11579 qsort being an unstable algorithm. */
876fa593
JK
11580
11581static int
494cfb0f 11582bp_location_compare (const void *ap, const void *bp)
876fa593 11583{
494cfb0f
JK
11584 struct bp_location *a = *(void **) ap;
11585 struct bp_location *b = *(void **) bp;
2bdf28a0 11586 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11587 int a_perm = a->owner->enable_state == bp_permanent;
11588 int b_perm = b->owner->enable_state == bp_permanent;
11589
11590 if (a->address != b->address)
11591 return (a->address > b->address) - (a->address < b->address);
11592
dea2aa5f
LM
11593 /* Sort locations at the same address by their pspace number, keeping
11594 locations of the same inferior (in a multi-inferior environment)
11595 grouped. */
11596
11597 if (a->pspace->num != b->pspace->num)
11598 return ((a->pspace->num > b->pspace->num)
11599 - (a->pspace->num < b->pspace->num));
11600
876fa593
JK
11601 /* Sort permanent breakpoints first. */
11602 if (a_perm != b_perm)
11603 return (a_perm < b_perm) - (a_perm > b_perm);
11604
c56a97f9
JK
11605 /* Make the internal GDB representation stable across GDB runs
11606 where A and B memory inside GDB can differ. Breakpoint locations of
11607 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11608
11609 if (a->owner->number != b->owner->number)
c56a97f9
JK
11610 return ((a->owner->number > b->owner->number)
11611 - (a->owner->number < b->owner->number));
876fa593
JK
11612
11613 return (a > b) - (a < b);
11614}
11615
876fa593 11616/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11617 bp_location_shadow_len_after_address_max according to the current
11618 content of the bp_location array. */
f7545552
TT
11619
11620static void
876fa593 11621bp_location_target_extensions_update (void)
f7545552 11622{
876fa593
JK
11623 struct bp_location *bl, **blp_tmp;
11624
11625 bp_location_placed_address_before_address_max = 0;
11626 bp_location_shadow_len_after_address_max = 0;
11627
11628 ALL_BP_LOCATIONS (bl, blp_tmp)
11629 {
11630 CORE_ADDR start, end, addr;
11631
11632 if (!bp_location_has_shadow (bl))
11633 continue;
11634
11635 start = bl->target_info.placed_address;
11636 end = start + bl->target_info.shadow_len;
11637
11638 gdb_assert (bl->address >= start);
11639 addr = bl->address - start;
11640 if (addr > bp_location_placed_address_before_address_max)
11641 bp_location_placed_address_before_address_max = addr;
11642
11643 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11644
11645 gdb_assert (bl->address < end);
11646 addr = end - bl->address;
11647 if (addr > bp_location_shadow_len_after_address_max)
11648 bp_location_shadow_len_after_address_max = addr;
11649 }
f7545552
TT
11650}
11651
1e4d1764
YQ
11652/* Download tracepoint locations if they haven't been. */
11653
11654static void
11655download_tracepoint_locations (void)
11656{
11657 struct bp_location *bl, **blp_tmp;
11658 struct cleanup *old_chain;
11659
11660 if (!target_can_download_tracepoint ())
11661 return;
11662
11663 old_chain = save_current_space_and_thread ();
11664
11665 ALL_BP_LOCATIONS (bl, blp_tmp)
11666 {
11667 struct tracepoint *t;
11668
11669 if (!is_tracepoint (bl->owner))
11670 continue;
11671
11672 if ((bl->owner->type == bp_fast_tracepoint
11673 ? !may_insert_fast_tracepoints
11674 : !may_insert_tracepoints))
11675 continue;
11676
11677 /* In tracepoint, locations are _never_ duplicated, so
11678 should_be_inserted is equivalent to
11679 unduplicated_should_be_inserted. */
11680 if (!should_be_inserted (bl) || bl->inserted)
11681 continue;
11682
11683 switch_to_program_space_and_thread (bl->pspace);
11684
11685 target_download_tracepoint (bl);
11686
11687 bl->inserted = 1;
11688 t = (struct tracepoint *) bl->owner;
11689 t->number_on_target = bl->owner->number;
11690 }
11691
11692 do_cleanups (old_chain);
11693}
11694
934709f0
PW
11695/* Swap the insertion/duplication state between two locations. */
11696
11697static void
11698swap_insertion (struct bp_location *left, struct bp_location *right)
11699{
11700 const int left_inserted = left->inserted;
11701 const int left_duplicate = left->duplicate;
b775012e 11702 const int left_needs_update = left->needs_update;
934709f0
PW
11703 const struct bp_target_info left_target_info = left->target_info;
11704
1e4d1764
YQ
11705 /* Locations of tracepoints can never be duplicated. */
11706 if (is_tracepoint (left->owner))
11707 gdb_assert (!left->duplicate);
11708 if (is_tracepoint (right->owner))
11709 gdb_assert (!right->duplicate);
11710
934709f0
PW
11711 left->inserted = right->inserted;
11712 left->duplicate = right->duplicate;
b775012e 11713 left->needs_update = right->needs_update;
934709f0
PW
11714 left->target_info = right->target_info;
11715 right->inserted = left_inserted;
11716 right->duplicate = left_duplicate;
b775012e 11717 right->needs_update = left_needs_update;
934709f0
PW
11718 right->target_info = left_target_info;
11719}
11720
b775012e
LM
11721/* Force the re-insertion of the locations at ADDRESS. This is called
11722 once a new/deleted/modified duplicate location is found and we are evaluating
11723 conditions on the target's side. Such conditions need to be updated on
11724 the target. */
11725
11726static void
11727force_breakpoint_reinsertion (struct bp_location *bl)
11728{
11729 struct bp_location **locp = NULL, **loc2p;
11730 struct bp_location *loc;
11731 CORE_ADDR address = 0;
11732 int pspace_num;
11733
11734 address = bl->address;
11735 pspace_num = bl->pspace->num;
11736
11737 /* This is only meaningful if the target is
11738 evaluating conditions and if the user has
11739 opted for condition evaluation on the target's
11740 side. */
11741 if (gdb_evaluates_breakpoint_condition_p ()
11742 || !target_supports_evaluation_of_breakpoint_conditions ())
11743 return;
11744
11745 /* Flag all breakpoint locations with this address and
11746 the same program space as the location
11747 as "its condition has changed". We need to
11748 update the conditions on the target's side. */
11749 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11750 {
11751 loc = *loc2p;
11752
11753 if (!is_breakpoint (loc->owner)
11754 || pspace_num != loc->pspace->num)
11755 continue;
11756
11757 /* Flag the location appropriately. We use a different state to
11758 let everyone know that we already updated the set of locations
11759 with addr bl->address and program space bl->pspace. This is so
11760 we don't have to keep calling these functions just to mark locations
11761 that have already been marked. */
11762 loc->condition_changed = condition_updated;
11763
11764 /* Free the agent expression bytecode as well. We will compute
11765 it later on. */
11766 if (loc->cond_bytecode)
11767 {
11768 free_agent_expr (loc->cond_bytecode);
11769 loc->cond_bytecode = NULL;
11770 }
11771 }
11772}
11773
4cd9bd08 11774/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
11775 into the inferior, only remove already-inserted locations that no
11776 longer should be inserted. Functions that delete a breakpoint or
11777 breakpoints should pass false, so that deleting a breakpoint
11778 doesn't have the side effect of inserting the locations of other
11779 breakpoints that are marked not-inserted, but should_be_inserted
11780 returns true on them.
11781
11782 This behaviour is useful is situations close to tear-down -- e.g.,
11783 after an exec, while the target still has execution, but breakpoint
11784 shadows of the previous executable image should *NOT* be restored
11785 to the new image; or before detaching, where the target still has
11786 execution and wants to delete breakpoints from GDB's lists, and all
11787 breakpoints had already been removed from the inferior. */
11788
0d381245 11789static void
b60e7edf 11790update_global_location_list (int should_insert)
0d381245 11791{
74960c60 11792 struct breakpoint *b;
876fa593 11793 struct bp_location **locp, *loc;
f7545552 11794 struct cleanup *cleanups;
b775012e
LM
11795 /* Last breakpoint location address that was marked for update. */
11796 CORE_ADDR last_addr = 0;
11797 /* Last breakpoint location program space that was marked for update. */
11798 int last_pspace_num = -1;
f7545552 11799
2d134ed3
PA
11800 /* Used in the duplicates detection below. When iterating over all
11801 bp_locations, points to the first bp_location of a given address.
11802 Breakpoints and watchpoints of different types are never
11803 duplicates of each other. Keep one pointer for each type of
11804 breakpoint/watchpoint, so we only need to loop over all locations
11805 once. */
11806 struct bp_location *bp_loc_first; /* breakpoint */
11807 struct bp_location *wp_loc_first; /* hardware watchpoint */
11808 struct bp_location *awp_loc_first; /* access watchpoint */
11809 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11810
4a64f543
MS
11811 /* Saved former bp_location array which we compare against the newly
11812 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
11813 struct bp_location **old_location, **old_locp;
11814 unsigned old_location_count;
11815
11816 old_location = bp_location;
11817 old_location_count = bp_location_count;
11818 bp_location = NULL;
11819 bp_location_count = 0;
11820 cleanups = make_cleanup (xfree, old_location);
0d381245 11821
74960c60 11822 ALL_BREAKPOINTS (b)
876fa593
JK
11823 for (loc = b->loc; loc; loc = loc->next)
11824 bp_location_count++;
11825
11826 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11827 locp = bp_location;
11828 ALL_BREAKPOINTS (b)
11829 for (loc = b->loc; loc; loc = loc->next)
11830 *locp++ = loc;
11831 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 11832 bp_location_compare);
876fa593
JK
11833
11834 bp_location_target_extensions_update ();
74960c60 11835
4a64f543
MS
11836 /* Identify bp_location instances that are no longer present in the
11837 new list, and therefore should be freed. Note that it's not
11838 necessary that those locations should be removed from inferior --
11839 if there's another location at the same address (previously
11840 marked as duplicate), we don't need to remove/insert the
11841 location.
876fa593 11842
4a64f543
MS
11843 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11844 and former bp_location array state respectively. */
876fa593
JK
11845
11846 locp = bp_location;
11847 for (old_locp = old_location; old_locp < old_location + old_location_count;
11848 old_locp++)
74960c60 11849 {
876fa593 11850 struct bp_location *old_loc = *old_locp;
c7d46a38 11851 struct bp_location **loc2p;
876fa593 11852
e5dd4106 11853 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11854 not, we have to free it. */
c7d46a38 11855 int found_object = 0;
20874c92
VP
11856 /* Tells if the location should remain inserted in the target. */
11857 int keep_in_target = 0;
11858 int removed = 0;
876fa593 11859
4a64f543
MS
11860 /* Skip LOCP entries which will definitely never be needed.
11861 Stop either at or being the one matching OLD_LOC. */
876fa593 11862 while (locp < bp_location + bp_location_count
c7d46a38 11863 && (*locp)->address < old_loc->address)
876fa593 11864 locp++;
c7d46a38
PA
11865
11866 for (loc2p = locp;
11867 (loc2p < bp_location + bp_location_count
11868 && (*loc2p)->address == old_loc->address);
11869 loc2p++)
11870 {
b775012e
LM
11871 /* Check if this is a new/duplicated location or a duplicated
11872 location that had its condition modified. If so, we want to send
11873 its condition to the target if evaluation of conditions is taking
11874 place there. */
11875 if ((*loc2p)->condition_changed == condition_modified
11876 && (last_addr != old_loc->address
11877 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11878 {
b775012e
LM
11879 force_breakpoint_reinsertion (*loc2p);
11880 last_pspace_num = old_loc->pspace->num;
c7d46a38 11881 }
b775012e
LM
11882
11883 if (*loc2p == old_loc)
11884 found_object = 1;
c7d46a38 11885 }
74960c60 11886
b775012e
LM
11887 /* We have already handled this address, update it so that we don't
11888 have to go through updates again. */
11889 last_addr = old_loc->address;
11890
11891 /* Target-side condition evaluation: Handle deleted locations. */
11892 if (!found_object)
11893 force_breakpoint_reinsertion (old_loc);
11894
4a64f543
MS
11895 /* If this location is no longer present, and inserted, look if
11896 there's maybe a new location at the same address. If so,
11897 mark that one inserted, and don't remove this one. This is
11898 needed so that we don't have a time window where a breakpoint
11899 at certain location is not inserted. */
74960c60 11900
876fa593 11901 if (old_loc->inserted)
0d381245 11902 {
4a64f543
MS
11903 /* If the location is inserted now, we might have to remove
11904 it. */
74960c60 11905
876fa593 11906 if (found_object && should_be_inserted (old_loc))
74960c60 11907 {
4a64f543
MS
11908 /* The location is still present in the location list,
11909 and still should be inserted. Don't do anything. */
20874c92 11910 keep_in_target = 1;
74960c60
VP
11911 }
11912 else
11913 {
b775012e
LM
11914 /* This location still exists, but it won't be kept in the
11915 target since it may have been disabled. We proceed to
11916 remove its target-side condition. */
11917
4a64f543
MS
11918 /* The location is either no longer present, or got
11919 disabled. See if there's another location at the
11920 same address, in which case we don't need to remove
11921 this one from the target. */
876fa593 11922
2bdf28a0 11923 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
11924 if (breakpoint_address_is_meaningful (old_loc->owner))
11925 {
876fa593 11926 for (loc2p = locp;
c7d46a38
PA
11927 (loc2p < bp_location + bp_location_count
11928 && (*loc2p)->address == old_loc->address);
876fa593
JK
11929 loc2p++)
11930 {
11931 struct bp_location *loc2 = *loc2p;
11932
2d134ed3 11933 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11934 {
85d721b8
PA
11935 /* Read watchpoint locations are switched to
11936 access watchpoints, if the former are not
11937 supported, but the latter are. */
11938 if (is_hardware_watchpoint (old_loc->owner))
11939 {
11940 gdb_assert (is_hardware_watchpoint (loc2->owner));
11941 loc2->watchpoint_type = old_loc->watchpoint_type;
11942 }
11943
934709f0
PW
11944 /* loc2 is a duplicated location. We need to check
11945 if it should be inserted in case it will be
11946 unduplicated. */
11947 if (loc2 != old_loc
11948 && unduplicated_should_be_inserted (loc2))
c7d46a38 11949 {
934709f0 11950 swap_insertion (old_loc, loc2);
c7d46a38
PA
11951 keep_in_target = 1;
11952 break;
11953 }
876fa593
JK
11954 }
11955 }
11956 }
74960c60
VP
11957 }
11958
20874c92
VP
11959 if (!keep_in_target)
11960 {
876fa593 11961 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 11962 {
4a64f543
MS
11963 /* This is just about all we can do. We could keep
11964 this location on the global list, and try to
11965 remove it next time, but there's no particular
11966 reason why we will succeed next time.
20874c92 11967
4a64f543
MS
11968 Note that at this point, old_loc->owner is still
11969 valid, as delete_breakpoint frees the breakpoint
11970 only after calling us. */
3e43a32a
MS
11971 printf_filtered (_("warning: Error removing "
11972 "breakpoint %d\n"),
876fa593 11973 old_loc->owner->number);
20874c92
VP
11974 }
11975 removed = 1;
11976 }
0d381245 11977 }
74960c60
VP
11978
11979 if (!found_object)
1c5cfe86 11980 {
db82e815
PA
11981 if (removed && non_stop
11982 && breakpoint_address_is_meaningful (old_loc->owner)
11983 && !is_hardware_watchpoint (old_loc->owner))
20874c92 11984 {
db82e815
PA
11985 /* This location was removed from the target. In
11986 non-stop mode, a race condition is possible where
11987 we've removed a breakpoint, but stop events for that
11988 breakpoint are already queued and will arrive later.
11989 We apply an heuristic to be able to distinguish such
11990 SIGTRAPs from other random SIGTRAPs: we keep this
11991 breakpoint location for a bit, and will retire it
11992 after we see some number of events. The theory here
11993 is that reporting of events should, "on the average",
11994 be fair, so after a while we'll see events from all
11995 threads that have anything of interest, and no longer
11996 need to keep this breakpoint location around. We
11997 don't hold locations forever so to reduce chances of
11998 mistaking a non-breakpoint SIGTRAP for a breakpoint
11999 SIGTRAP.
12000
12001 The heuristic failing can be disastrous on
12002 decr_pc_after_break targets.
12003
12004 On decr_pc_after_break targets, like e.g., x86-linux,
12005 if we fail to recognize a late breakpoint SIGTRAP,
12006 because events_till_retirement has reached 0 too
12007 soon, we'll fail to do the PC adjustment, and report
12008 a random SIGTRAP to the user. When the user resumes
12009 the inferior, it will most likely immediately crash
2dec564e 12010 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12011 corrupted, because of being resumed e.g., in the
12012 middle of a multi-byte instruction, or skipped a
12013 one-byte instruction. This was actually seen happen
12014 on native x86-linux, and should be less rare on
12015 targets that do not support new thread events, like
12016 remote, due to the heuristic depending on
12017 thread_count.
12018
12019 Mistaking a random SIGTRAP for a breakpoint trap
12020 causes similar symptoms (PC adjustment applied when
12021 it shouldn't), but then again, playing with SIGTRAPs
12022 behind the debugger's back is asking for trouble.
12023
12024 Since hardware watchpoint traps are always
12025 distinguishable from other traps, so we don't need to
12026 apply keep hardware watchpoint moribund locations
12027 around. We simply always ignore hardware watchpoint
12028 traps we can no longer explain. */
12029
876fa593
JK
12030 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12031 old_loc->owner = NULL;
20874c92 12032
876fa593 12033 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12034 }
12035 else
f431efe5
PA
12036 {
12037 old_loc->owner = NULL;
12038 decref_bp_location (&old_loc);
12039 }
20874c92 12040 }
74960c60 12041 }
1c5cfe86 12042
348d480f
PA
12043 /* Rescan breakpoints at the same address and section, marking the
12044 first one as "first" and any others as "duplicates". This is so
12045 that the bpt instruction is only inserted once. If we have a
12046 permanent breakpoint at the same place as BPT, make that one the
12047 official one, and the rest as duplicates. Permanent breakpoints
12048 are sorted first for the same address.
12049
12050 Do the same for hardware watchpoints, but also considering the
12051 watchpoint's type (regular/access/read) and length. */
12052
12053 bp_loc_first = NULL;
12054 wp_loc_first = NULL;
12055 awp_loc_first = NULL;
12056 rwp_loc_first = NULL;
12057 ALL_BP_LOCATIONS (loc, locp)
12058 {
12059 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12060 non-NULL. */
348d480f 12061 struct bp_location **loc_first_p;
d3fbdd86 12062 b = loc->owner;
348d480f 12063
f8eba3c6 12064 if (!should_be_inserted (loc)
348d480f 12065 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12066 /* Don't detect duplicate for tracepoint locations because they are
12067 never duplicated. See the comments in field `duplicate' of
12068 `struct bp_location'. */
348d480f 12069 || is_tracepoint (b))
b775012e
LM
12070 {
12071 /* Clear the condition modification flag. */
12072 loc->condition_changed = condition_unchanged;
12073 continue;
12074 }
348d480f
PA
12075
12076 /* Permanent breakpoint should always be inserted. */
12077 if (b->enable_state == bp_permanent && ! loc->inserted)
12078 internal_error (__FILE__, __LINE__,
12079 _("allegedly permanent breakpoint is not "
12080 "actually inserted"));
12081
12082 if (b->type == bp_hardware_watchpoint)
12083 loc_first_p = &wp_loc_first;
12084 else if (b->type == bp_read_watchpoint)
12085 loc_first_p = &rwp_loc_first;
12086 else if (b->type == bp_access_watchpoint)
12087 loc_first_p = &awp_loc_first;
12088 else
12089 loc_first_p = &bp_loc_first;
12090
12091 if (*loc_first_p == NULL
12092 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12093 || !breakpoint_locations_match (loc, *loc_first_p))
12094 {
12095 *loc_first_p = loc;
12096 loc->duplicate = 0;
b775012e
LM
12097
12098 if (is_breakpoint (loc->owner) && loc->condition_changed)
12099 {
12100 loc->needs_update = 1;
12101 /* Clear the condition modification flag. */
12102 loc->condition_changed = condition_unchanged;
12103 }
348d480f
PA
12104 continue;
12105 }
12106
934709f0
PW
12107
12108 /* This and the above ensure the invariant that the first location
12109 is not duplicated, and is the inserted one.
12110 All following are marked as duplicated, and are not inserted. */
12111 if (loc->inserted)
12112 swap_insertion (loc, *loc_first_p);
348d480f
PA
12113 loc->duplicate = 1;
12114
b775012e
LM
12115 /* Clear the condition modification flag. */
12116 loc->condition_changed = condition_unchanged;
12117
348d480f
PA
12118 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12119 && b->enable_state != bp_permanent)
12120 internal_error (__FILE__, __LINE__,
12121 _("another breakpoint was inserted on top of "
12122 "a permanent breakpoint"));
12123 }
12124
b775012e 12125 if (breakpoints_always_inserted_mode ()
348d480f
PA
12126 && (have_live_inferiors ()
12127 || (gdbarch_has_global_breakpoints (target_gdbarch))))
b775012e
LM
12128 {
12129 if (should_insert)
12130 insert_breakpoint_locations ();
12131 else
12132 {
12133 /* Though should_insert is false, we may need to update conditions
12134 on the target's side if it is evaluating such conditions. We
12135 only update conditions for locations that are marked
12136 "needs_update". */
12137 update_inserted_breakpoint_locations ();
12138 }
12139 }
348d480f 12140
1e4d1764
YQ
12141 if (should_insert)
12142 download_tracepoint_locations ();
12143
348d480f
PA
12144 do_cleanups (cleanups);
12145}
12146
12147void
12148breakpoint_retire_moribund (void)
12149{
12150 struct bp_location *loc;
12151 int ix;
12152
12153 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12154 if (--(loc->events_till_retirement) == 0)
12155 {
12156 decref_bp_location (&loc);
12157 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12158 --ix;
12159 }
12160}
12161
12162static void
12163update_global_location_list_nothrow (int inserting)
12164{
bfd189b1 12165 volatile struct gdb_exception e;
348d480f
PA
12166
12167 TRY_CATCH (e, RETURN_MASK_ERROR)
12168 update_global_location_list (inserting);
12169}
12170
12171/* Clear BKP from a BPS. */
12172
12173static void
12174bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12175{
12176 bpstat bs;
12177
12178 for (bs = bps; bs; bs = bs->next)
12179 if (bs->breakpoint_at == bpt)
12180 {
12181 bs->breakpoint_at = NULL;
12182 bs->old_val = NULL;
12183 /* bs->commands will be freed later. */
12184 }
12185}
12186
12187/* Callback for iterate_over_threads. */
12188static int
12189bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12190{
12191 struct breakpoint *bpt = data;
12192
12193 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12194 return 0;
12195}
12196
12197/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12198 callbacks. */
12199
12200static void
12201say_where (struct breakpoint *b)
12202{
79a45e25 12203 struct ui_out *uiout = current_uiout;
348d480f
PA
12204 struct value_print_options opts;
12205
12206 get_user_print_options (&opts);
12207
12208 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12209 single string. */
12210 if (b->loc == NULL)
12211 {
12212 printf_filtered (_(" (%s) pending."), b->addr_string);
12213 }
12214 else
12215 {
f8eba3c6 12216 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
12217 {
12218 printf_filtered (" at ");
12219 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12220 gdb_stdout);
12221 }
f8eba3c6
TT
12222 if (b->loc->source_file)
12223 {
12224 /* If there is a single location, we can print the location
12225 more nicely. */
12226 if (b->loc->next == NULL)
12227 printf_filtered (": file %s, line %d.",
12228 b->loc->source_file, b->loc->line_number);
12229 else
12230 /* This is not ideal, but each location may have a
12231 different file name, and this at least reflects the
12232 real situation somewhat. */
12233 printf_filtered (": %s.", b->addr_string);
12234 }
348d480f
PA
12235
12236 if (b->loc->next)
12237 {
12238 struct bp_location *loc = b->loc;
12239 int n = 0;
12240 for (; loc; loc = loc->next)
12241 ++n;
12242 printf_filtered (" (%d locations)", n);
12243 }
12244 }
12245}
12246
348d480f
PA
12247/* Default bp_location_ops methods. */
12248
12249static void
12250bp_location_dtor (struct bp_location *self)
12251{
12252 xfree (self->cond);
b775012e
LM
12253 if (self->cond_bytecode)
12254 free_agent_expr (self->cond_bytecode);
348d480f 12255 xfree (self->function_name);
f8eba3c6 12256 xfree (self->source_file);
348d480f
PA
12257}
12258
12259static const struct bp_location_ops bp_location_ops =
12260{
12261 bp_location_dtor
12262};
12263
2060206e
PA
12264/* Default breakpoint_ops methods all breakpoint_ops ultimately
12265 inherit from. */
348d480f 12266
2060206e
PA
12267static void
12268base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12269{
12270 decref_counted_command_line (&self->commands);
12271 xfree (self->cond_string);
348d480f 12272 xfree (self->addr_string);
f8eba3c6 12273 xfree (self->filter);
348d480f 12274 xfree (self->addr_string_range_end);
348d480f
PA
12275}
12276
2060206e
PA
12277static struct bp_location *
12278base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12279{
12280 struct bp_location *loc;
12281
12282 loc = XNEW (struct bp_location);
12283 init_bp_location (loc, &bp_location_ops, self);
12284 return loc;
12285}
12286
2060206e
PA
12287static void
12288base_breakpoint_re_set (struct breakpoint *b)
12289{
12290 /* Nothing to re-set. */
12291}
12292
12293#define internal_error_pure_virtual_called() \
12294 gdb_assert_not_reached ("pure virtual function called")
12295
12296static int
12297base_breakpoint_insert_location (struct bp_location *bl)
12298{
12299 internal_error_pure_virtual_called ();
12300}
12301
12302static int
12303base_breakpoint_remove_location (struct bp_location *bl)
12304{
12305 internal_error_pure_virtual_called ();
12306}
12307
12308static int
12309base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12310 struct address_space *aspace,
09ac7c10
TT
12311 CORE_ADDR bp_addr,
12312 const struct target_waitstatus *ws)
2060206e
PA
12313{
12314 internal_error_pure_virtual_called ();
12315}
12316
12317static void
12318base_breakpoint_check_status (bpstat bs)
12319{
12320 /* Always stop. */
12321}
12322
12323/* A "works_in_software_mode" breakpoint_ops method that just internal
12324 errors. */
12325
12326static int
12327base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12328{
12329 internal_error_pure_virtual_called ();
12330}
12331
12332/* A "resources_needed" breakpoint_ops method that just internal
12333 errors. */
12334
12335static int
12336base_breakpoint_resources_needed (const struct bp_location *bl)
12337{
12338 internal_error_pure_virtual_called ();
12339}
12340
12341static enum print_stop_action
12342base_breakpoint_print_it (bpstat bs)
12343{
12344 internal_error_pure_virtual_called ();
12345}
12346
12347static void
12348base_breakpoint_print_one_detail (const struct breakpoint *self,
12349 struct ui_out *uiout)
12350{
12351 /* nothing */
12352}
12353
12354static void
12355base_breakpoint_print_mention (struct breakpoint *b)
12356{
12357 internal_error_pure_virtual_called ();
12358}
12359
12360static void
12361base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12362{
12363 internal_error_pure_virtual_called ();
12364}
12365
983af33b
SDJ
12366static void
12367base_breakpoint_create_sals_from_address (char **arg,
12368 struct linespec_result *canonical,
12369 enum bptype type_wanted,
12370 char *addr_start,
12371 char **copy_arg)
12372{
12373 internal_error_pure_virtual_called ();
12374}
12375
12376static void
12377base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12378 struct linespec_result *c,
12379 struct linespec_sals *lsal,
12380 char *cond_string,
e7e0cddf 12381 char *extra_string,
983af33b
SDJ
12382 enum bptype type_wanted,
12383 enum bpdisp disposition,
12384 int thread,
12385 int task, int ignore_count,
12386 const struct breakpoint_ops *o,
12387 int from_tty, int enabled,
44f238bb 12388 int internal, unsigned flags)
983af33b
SDJ
12389{
12390 internal_error_pure_virtual_called ();
12391}
12392
12393static void
12394base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12395 struct symtabs_and_lines *sals)
12396{
12397 internal_error_pure_virtual_called ();
12398}
12399
2060206e
PA
12400static struct breakpoint_ops base_breakpoint_ops =
12401{
12402 base_breakpoint_dtor,
12403 base_breakpoint_allocate_location,
12404 base_breakpoint_re_set,
12405 base_breakpoint_insert_location,
12406 base_breakpoint_remove_location,
12407 base_breakpoint_breakpoint_hit,
12408 base_breakpoint_check_status,
12409 base_breakpoint_resources_needed,
12410 base_breakpoint_works_in_software_mode,
12411 base_breakpoint_print_it,
12412 NULL,
12413 base_breakpoint_print_one_detail,
12414 base_breakpoint_print_mention,
983af33b
SDJ
12415 base_breakpoint_print_recreate,
12416 base_breakpoint_create_sals_from_address,
12417 base_breakpoint_create_breakpoints_sal,
12418 base_breakpoint_decode_linespec,
2060206e
PA
12419};
12420
12421/* Default breakpoint_ops methods. */
12422
12423static void
348d480f
PA
12424bkpt_re_set (struct breakpoint *b)
12425{
06edf0c0
PA
12426 /* FIXME: is this still reachable? */
12427 if (b->addr_string == NULL)
12428 {
12429 /* Anything without a string can't be re-set. */
348d480f 12430 delete_breakpoint (b);
06edf0c0 12431 return;
348d480f 12432 }
06edf0c0
PA
12433
12434 breakpoint_re_set_default (b);
348d480f
PA
12435}
12436
2060206e 12437static int
348d480f
PA
12438bkpt_insert_location (struct bp_location *bl)
12439{
12440 if (bl->loc_type == bp_loc_hardware_breakpoint)
12441 return target_insert_hw_breakpoint (bl->gdbarch,
12442 &bl->target_info);
12443 else
12444 return target_insert_breakpoint (bl->gdbarch,
12445 &bl->target_info);
12446}
12447
2060206e 12448static int
348d480f
PA
12449bkpt_remove_location (struct bp_location *bl)
12450{
12451 if (bl->loc_type == bp_loc_hardware_breakpoint)
12452 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12453 else
12454 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12455}
12456
2060206e 12457static int
348d480f 12458bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12459 struct address_space *aspace, CORE_ADDR bp_addr,
12460 const struct target_waitstatus *ws)
348d480f
PA
12461{
12462 struct breakpoint *b = bl->owner;
12463
09ac7c10
TT
12464 if (ws->kind != TARGET_WAITKIND_STOPPED
12465 || ws->value.sig != TARGET_SIGNAL_TRAP)
12466 return 0;
12467
348d480f
PA
12468 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12469 aspace, bp_addr))
12470 return 0;
12471
12472 if (overlay_debugging /* unmapped overlay section */
12473 && section_is_overlay (bl->section)
12474 && !section_is_mapped (bl->section))
12475 return 0;
12476
12477 return 1;
12478}
12479
2060206e 12480static int
348d480f
PA
12481bkpt_resources_needed (const struct bp_location *bl)
12482{
12483 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12484
12485 return 1;
12486}
12487
2060206e 12488static enum print_stop_action
348d480f
PA
12489bkpt_print_it (bpstat bs)
12490{
348d480f
PA
12491 struct breakpoint *b;
12492 const struct bp_location *bl;
001c8c33 12493 int bp_temp;
79a45e25 12494 struct ui_out *uiout = current_uiout;
348d480f
PA
12495
12496 gdb_assert (bs->bp_location_at != NULL);
12497
12498 bl = bs->bp_location_at;
12499 b = bs->breakpoint_at;
12500
001c8c33
PA
12501 bp_temp = b->disposition == disp_del;
12502 if (bl->address != bl->requested_address)
12503 breakpoint_adjustment_warning (bl->requested_address,
12504 bl->address,
12505 b->number, 1);
12506 annotate_breakpoint (b->number);
12507 if (bp_temp)
12508 ui_out_text (uiout, "\nTemporary breakpoint ");
12509 else
12510 ui_out_text (uiout, "\nBreakpoint ");
12511 if (ui_out_is_mi_like_p (uiout))
348d480f 12512 {
001c8c33
PA
12513 ui_out_field_string (uiout, "reason",
12514 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12515 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12516 }
001c8c33
PA
12517 ui_out_field_int (uiout, "bkptno", b->number);
12518 ui_out_text (uiout, ", ");
06edf0c0 12519
001c8c33 12520 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12521}
12522
2060206e 12523static void
06edf0c0
PA
12524bkpt_print_mention (struct breakpoint *b)
12525{
79a45e25 12526 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12527 return;
12528
12529 switch (b->type)
12530 {
12531 case bp_breakpoint:
12532 case bp_gnu_ifunc_resolver:
12533 if (b->disposition == disp_del)
12534 printf_filtered (_("Temporary breakpoint"));
12535 else
12536 printf_filtered (_("Breakpoint"));
12537 printf_filtered (_(" %d"), b->number);
12538 if (b->type == bp_gnu_ifunc_resolver)
12539 printf_filtered (_(" at gnu-indirect-function resolver"));
12540 break;
12541 case bp_hardware_breakpoint:
12542 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12543 break;
e7e0cddf
SS
12544 case bp_dprintf:
12545 printf_filtered (_("Dprintf %d"), b->number);
12546 break;
06edf0c0
PA
12547 }
12548
12549 say_where (b);
12550}
12551
2060206e 12552static void
06edf0c0
PA
12553bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12554{
12555 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12556 fprintf_unfiltered (fp, "tbreak");
12557 else if (tp->type == bp_breakpoint)
12558 fprintf_unfiltered (fp, "break");
12559 else if (tp->type == bp_hardware_breakpoint
12560 && tp->disposition == disp_del)
12561 fprintf_unfiltered (fp, "thbreak");
12562 else if (tp->type == bp_hardware_breakpoint)
12563 fprintf_unfiltered (fp, "hbreak");
12564 else
12565 internal_error (__FILE__, __LINE__,
12566 _("unhandled breakpoint type %d"), (int) tp->type);
12567
2060206e 12568 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12569 print_recreate_thread (tp, fp);
06edf0c0
PA
12570}
12571
983af33b
SDJ
12572static void
12573bkpt_create_sals_from_address (char **arg,
12574 struct linespec_result *canonical,
12575 enum bptype type_wanted,
12576 char *addr_start, char **copy_arg)
12577{
12578 create_sals_from_address_default (arg, canonical, type_wanted,
12579 addr_start, copy_arg);
12580}
12581
12582static void
12583bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12584 struct linespec_result *canonical,
12585 struct linespec_sals *lsal,
12586 char *cond_string,
e7e0cddf 12587 char *extra_string,
983af33b
SDJ
12588 enum bptype type_wanted,
12589 enum bpdisp disposition,
12590 int thread,
12591 int task, int ignore_count,
12592 const struct breakpoint_ops *ops,
12593 int from_tty, int enabled,
44f238bb 12594 int internal, unsigned flags)
983af33b
SDJ
12595{
12596 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12597 cond_string, extra_string,
12598 type_wanted,
983af33b
SDJ
12599 disposition, thread, task,
12600 ignore_count, ops, from_tty,
44f238bb 12601 enabled, internal, flags);
983af33b
SDJ
12602}
12603
12604static void
12605bkpt_decode_linespec (struct breakpoint *b, char **s,
12606 struct symtabs_and_lines *sals)
12607{
12608 decode_linespec_default (b, s, sals);
12609}
12610
06edf0c0
PA
12611/* Virtual table for internal breakpoints. */
12612
12613static void
12614internal_bkpt_re_set (struct breakpoint *b)
12615{
12616 switch (b->type)
12617 {
12618 /* Delete overlay event and longjmp master breakpoints; they
12619 will be reset later by breakpoint_re_set. */
12620 case bp_overlay_event:
12621 case bp_longjmp_master:
12622 case bp_std_terminate_master:
12623 case bp_exception_master:
12624 delete_breakpoint (b);
12625 break;
12626
12627 /* This breakpoint is special, it's set up when the inferior
12628 starts and we really don't want to touch it. */
12629 case bp_shlib_event:
12630
12631 /* Like bp_shlib_event, this breakpoint type is special. Once
12632 it is set up, we do not want to touch it. */
12633 case bp_thread_event:
12634 break;
12635 }
12636}
12637
12638static void
12639internal_bkpt_check_status (bpstat bs)
12640{
a9b3a50f
PA
12641 if (bs->breakpoint_at->type == bp_shlib_event)
12642 {
12643 /* If requested, stop when the dynamic linker notifies GDB of
12644 events. This allows the user to get control and place
12645 breakpoints in initializer routines for dynamically loaded
12646 objects (among other things). */
12647 bs->stop = stop_on_solib_events;
12648 bs->print = stop_on_solib_events;
12649 }
12650 else
12651 bs->stop = 0;
06edf0c0
PA
12652}
12653
12654static enum print_stop_action
12655internal_bkpt_print_it (bpstat bs)
12656{
36dfb11c 12657 struct ui_out *uiout = current_uiout;
06edf0c0 12658 struct breakpoint *b;
06edf0c0 12659
06edf0c0
PA
12660 b = bs->breakpoint_at;
12661
06edf0c0
PA
12662 switch (b->type)
12663 {
348d480f
PA
12664 case bp_shlib_event:
12665 /* Did we stop because the user set the stop_on_solib_events
12666 variable? (If so, we report this as a generic, "Stopped due
12667 to shlib event" message.) */
edcc5120 12668 print_solib_event (0);
348d480f
PA
12669 break;
12670
12671 case bp_thread_event:
12672 /* Not sure how we will get here.
12673 GDB should not stop for these breakpoints. */
12674 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12675 break;
12676
12677 case bp_overlay_event:
12678 /* By analogy with the thread event, GDB should not stop for these. */
12679 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12680 break;
12681
12682 case bp_longjmp_master:
12683 /* These should never be enabled. */
12684 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12685 break;
12686
12687 case bp_std_terminate_master:
12688 /* These should never be enabled. */
12689 printf_filtered (_("std::terminate Master Breakpoint: "
12690 "gdb should not stop!\n"));
348d480f
PA
12691 break;
12692
12693 case bp_exception_master:
12694 /* These should never be enabled. */
12695 printf_filtered (_("Exception Master Breakpoint: "
12696 "gdb should not stop!\n"));
06edf0c0
PA
12697 break;
12698 }
12699
001c8c33 12700 return PRINT_NOTHING;
06edf0c0
PA
12701}
12702
12703static void
12704internal_bkpt_print_mention (struct breakpoint *b)
12705{
12706 /* Nothing to mention. These breakpoints are internal. */
12707}
12708
06edf0c0
PA
12709/* Virtual table for momentary breakpoints */
12710
12711static void
12712momentary_bkpt_re_set (struct breakpoint *b)
12713{
12714 /* Keep temporary breakpoints, which can be encountered when we step
12715 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12716 Otherwise these should have been blown away via the cleanup chain
12717 or by breakpoint_init_inferior when we rerun the executable. */
12718}
12719
12720static void
12721momentary_bkpt_check_status (bpstat bs)
12722{
12723 /* Nothing. The point of these breakpoints is causing a stop. */
12724}
12725
12726static enum print_stop_action
12727momentary_bkpt_print_it (bpstat bs)
12728{
79a45e25
PA
12729 struct ui_out *uiout = current_uiout;
12730
001c8c33 12731 if (ui_out_is_mi_like_p (uiout))
06edf0c0 12732 {
001c8c33 12733 struct breakpoint *b = bs->breakpoint_at;
348d480f 12734
001c8c33
PA
12735 switch (b->type)
12736 {
12737 case bp_finish:
12738 ui_out_field_string
12739 (uiout, "reason",
12740 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12741 break;
348d480f 12742
001c8c33
PA
12743 case bp_until:
12744 ui_out_field_string
12745 (uiout, "reason",
12746 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12747 break;
12748 }
348d480f
PA
12749 }
12750
001c8c33 12751 return PRINT_UNKNOWN;
348d480f
PA
12752}
12753
06edf0c0
PA
12754static void
12755momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12756{
06edf0c0 12757 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12758}
12759
55aa24fb
SDJ
12760/* Specific methods for probe breakpoints. */
12761
12762static int
12763bkpt_probe_insert_location (struct bp_location *bl)
12764{
12765 int v = bkpt_insert_location (bl);
12766
12767 if (v == 0)
12768 {
12769 /* The insertion was successful, now let's set the probe's semaphore
12770 if needed. */
12771 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12772 }
12773
12774 return v;
12775}
12776
12777static int
12778bkpt_probe_remove_location (struct bp_location *bl)
12779{
12780 /* Let's clear the semaphore before removing the location. */
12781 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12782
12783 return bkpt_remove_location (bl);
12784}
12785
12786static void
12787bkpt_probe_create_sals_from_address (char **arg,
12788 struct linespec_result *canonical,
12789 enum bptype type_wanted,
12790 char *addr_start, char **copy_arg)
12791{
12792 struct linespec_sals lsal;
12793
12794 lsal.sals = parse_probes (arg, canonical);
12795
12796 *copy_arg = xstrdup (canonical->addr_string);
12797 lsal.canonical = xstrdup (*copy_arg);
12798
12799 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12800}
12801
12802static void
12803bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12804 struct symtabs_and_lines *sals)
12805{
12806 *sals = parse_probes (s, NULL);
12807 if (!sals->sals)
12808 error (_("probe not found"));
12809}
12810
348d480f 12811/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12812
348d480f
PA
12813static void
12814tracepoint_re_set (struct breakpoint *b)
12815{
12816 breakpoint_re_set_default (b);
12817}
876fa593 12818
348d480f
PA
12819static int
12820tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12821 struct address_space *aspace, CORE_ADDR bp_addr,
12822 const struct target_waitstatus *ws)
348d480f
PA
12823{
12824 /* By definition, the inferior does not report stops at
12825 tracepoints. */
12826 return 0;
74960c60
VP
12827}
12828
12829static void
348d480f
PA
12830tracepoint_print_one_detail (const struct breakpoint *self,
12831 struct ui_out *uiout)
74960c60 12832{
d9b3f62e
PA
12833 struct tracepoint *tp = (struct tracepoint *) self;
12834 if (tp->static_trace_marker_id)
348d480f
PA
12835 {
12836 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12837
348d480f
PA
12838 ui_out_text (uiout, "\tmarker id is ");
12839 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 12840 tp->static_trace_marker_id);
348d480f
PA
12841 ui_out_text (uiout, "\n");
12842 }
0d381245
VP
12843}
12844
a474d7c2 12845static void
348d480f 12846tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12847{
79a45e25 12848 if (ui_out_is_mi_like_p (current_uiout))
348d480f 12849 return;
cc59ec59 12850
348d480f
PA
12851 switch (b->type)
12852 {
12853 case bp_tracepoint:
12854 printf_filtered (_("Tracepoint"));
12855 printf_filtered (_(" %d"), b->number);
12856 break;
12857 case bp_fast_tracepoint:
12858 printf_filtered (_("Fast tracepoint"));
12859 printf_filtered (_(" %d"), b->number);
12860 break;
12861 case bp_static_tracepoint:
12862 printf_filtered (_("Static tracepoint"));
12863 printf_filtered (_(" %d"), b->number);
12864 break;
12865 default:
12866 internal_error (__FILE__, __LINE__,
12867 _("unhandled tracepoint type %d"), (int) b->type);
12868 }
12869
12870 say_where (b);
a474d7c2
PA
12871}
12872
348d480f 12873static void
d9b3f62e 12874tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12875{
d9b3f62e
PA
12876 struct tracepoint *tp = (struct tracepoint *) self;
12877
12878 if (self->type == bp_fast_tracepoint)
348d480f 12879 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 12880 if (self->type == bp_static_tracepoint)
348d480f 12881 fprintf_unfiltered (fp, "strace");
d9b3f62e 12882 else if (self->type == bp_tracepoint)
348d480f
PA
12883 fprintf_unfiltered (fp, "trace");
12884 else
12885 internal_error (__FILE__, __LINE__,
d9b3f62e 12886 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12887
d9b3f62e
PA
12888 fprintf_unfiltered (fp, " %s", self->addr_string);
12889 print_recreate_thread (self, fp);
12890
12891 if (tp->pass_count)
12892 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12893}
12894
983af33b
SDJ
12895static void
12896tracepoint_create_sals_from_address (char **arg,
12897 struct linespec_result *canonical,
12898 enum bptype type_wanted,
12899 char *addr_start, char **copy_arg)
12900{
12901 create_sals_from_address_default (arg, canonical, type_wanted,
12902 addr_start, copy_arg);
12903}
12904
12905static void
12906tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12907 struct linespec_result *canonical,
12908 struct linespec_sals *lsal,
12909 char *cond_string,
e7e0cddf 12910 char *extra_string,
983af33b
SDJ
12911 enum bptype type_wanted,
12912 enum bpdisp disposition,
12913 int thread,
12914 int task, int ignore_count,
12915 const struct breakpoint_ops *ops,
12916 int from_tty, int enabled,
44f238bb 12917 int internal, unsigned flags)
983af33b
SDJ
12918{
12919 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12920 cond_string, extra_string,
12921 type_wanted,
983af33b
SDJ
12922 disposition, thread, task,
12923 ignore_count, ops, from_tty,
44f238bb 12924 enabled, internal, flags);
983af33b
SDJ
12925}
12926
12927static void
12928tracepoint_decode_linespec (struct breakpoint *b, char **s,
12929 struct symtabs_and_lines *sals)
12930{
12931 decode_linespec_default (b, s, sals);
12932}
12933
2060206e 12934struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12935
55aa24fb
SDJ
12936/* The breakpoint_ops structure to be use on tracepoints placed in a
12937 static probe. */
12938
12939static void
12940tracepoint_probe_create_sals_from_address (char **arg,
12941 struct linespec_result *canonical,
12942 enum bptype type_wanted,
12943 char *addr_start, char **copy_arg)
12944{
12945 /* We use the same method for breakpoint on probes. */
12946 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
12947 addr_start, copy_arg);
12948}
12949
12950static void
12951tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
12952 struct symtabs_and_lines *sals)
12953{
12954 /* We use the same method for breakpoint on probes. */
12955 bkpt_probe_decode_linespec (b, s, sals);
12956}
12957
12958static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
12959
983af33b
SDJ
12960/* The breakpoint_ops structure to be used on static tracepoints with
12961 markers (`-m'). */
12962
12963static void
12964strace_marker_create_sals_from_address (char **arg,
12965 struct linespec_result *canonical,
12966 enum bptype type_wanted,
12967 char *addr_start, char **copy_arg)
12968{
12969 struct linespec_sals lsal;
12970
12971 lsal.sals = decode_static_tracepoint_spec (arg);
12972
12973 *copy_arg = savestring (addr_start, *arg - addr_start);
12974
12975 canonical->addr_string = xstrdup (*copy_arg);
12976 lsal.canonical = xstrdup (*copy_arg);
12977 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12978}
12979
12980static void
12981strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
12982 struct linespec_result *canonical,
12983 struct linespec_sals *lsal,
12984 char *cond_string,
e7e0cddf 12985 char *extra_string,
983af33b
SDJ
12986 enum bptype type_wanted,
12987 enum bpdisp disposition,
12988 int thread,
12989 int task, int ignore_count,
12990 const struct breakpoint_ops *ops,
12991 int from_tty, int enabled,
44f238bb 12992 int internal, unsigned flags)
983af33b
SDJ
12993{
12994 int i;
12995
12996 /* If the user is creating a static tracepoint by marker id
12997 (strace -m MARKER_ID), then store the sals index, so that
12998 breakpoint_re_set can try to match up which of the newly
12999 found markers corresponds to this one, and, don't try to
13000 expand multiple locations for each sal, given than SALS
13001 already should contain all sals for MARKER_ID. */
13002
13003 for (i = 0; i < lsal->sals.nelts; ++i)
13004 {
13005 struct symtabs_and_lines expanded;
13006 struct tracepoint *tp;
13007 struct cleanup *old_chain;
13008 char *addr_string;
13009
13010 expanded.nelts = 1;
13011 expanded.sals = &lsal->sals.sals[i];
13012
13013 addr_string = xstrdup (canonical->addr_string);
13014 old_chain = make_cleanup (xfree, addr_string);
13015
13016 tp = XCNEW (struct tracepoint);
13017 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13018 addr_string, NULL,
e7e0cddf
SS
13019 cond_string, extra_string,
13020 type_wanted, disposition,
983af33b 13021 thread, task, ignore_count, ops,
44f238bb 13022 from_tty, enabled, internal, flags,
983af33b
SDJ
13023 canonical->special_display);
13024 /* Given that its possible to have multiple markers with
13025 the same string id, if the user is creating a static
13026 tracepoint by marker id ("strace -m MARKER_ID"), then
13027 store the sals index, so that breakpoint_re_set can
13028 try to match up which of the newly found markers
13029 corresponds to this one */
13030 tp->static_trace_marker_id_idx = i;
13031
13032 install_breakpoint (internal, &tp->base, 0);
13033
13034 discard_cleanups (old_chain);
13035 }
13036}
13037
13038static void
13039strace_marker_decode_linespec (struct breakpoint *b, char **s,
13040 struct symtabs_and_lines *sals)
13041{
13042 struct tracepoint *tp = (struct tracepoint *) b;
13043
13044 *sals = decode_static_tracepoint_spec (s);
13045 if (sals->nelts > tp->static_trace_marker_id_idx)
13046 {
13047 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13048 sals->nelts = 1;
13049 }
13050 else
13051 error (_("marker %s not found"), tp->static_trace_marker_id);
13052}
13053
13054static struct breakpoint_ops strace_marker_breakpoint_ops;
13055
13056static int
13057strace_marker_p (struct breakpoint *b)
13058{
13059 return b->ops == &strace_marker_breakpoint_ops;
13060}
13061
53a5351d 13062/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13063 structures. */
c906108c
SS
13064
13065void
fba45db2 13066delete_breakpoint (struct breakpoint *bpt)
c906108c 13067{
52f0bd74 13068 struct breakpoint *b;
c906108c 13069
8a3fe4f8 13070 gdb_assert (bpt != NULL);
c906108c 13071
4a64f543
MS
13072 /* Has this bp already been deleted? This can happen because
13073 multiple lists can hold pointers to bp's. bpstat lists are
13074 especial culprits.
13075
13076 One example of this happening is a watchpoint's scope bp. When
13077 the scope bp triggers, we notice that the watchpoint is out of
13078 scope, and delete it. We also delete its scope bp. But the
13079 scope bp is marked "auto-deleting", and is already on a bpstat.
13080 That bpstat is then checked for auto-deleting bp's, which are
13081 deleted.
13082
13083 A real solution to this problem might involve reference counts in
13084 bp's, and/or giving them pointers back to their referencing
13085 bpstat's, and teaching delete_breakpoint to only free a bp's
13086 storage when no more references were extent. A cheaper bandaid
13087 was chosen. */
c906108c
SS
13088 if (bpt->type == bp_none)
13089 return;
13090
4a64f543
MS
13091 /* At least avoid this stale reference until the reference counting
13092 of breakpoints gets resolved. */
d0fb5eae 13093 if (bpt->related_breakpoint != bpt)
e5a0a904 13094 {
d0fb5eae 13095 struct breakpoint *related;
3a5c3e22 13096 struct watchpoint *w;
d0fb5eae
JK
13097
13098 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13099 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13100 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13101 w = (struct watchpoint *) bpt;
13102 else
13103 w = NULL;
13104 if (w != NULL)
13105 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13106
13107 /* Unlink bpt from the bpt->related_breakpoint ring. */
13108 for (related = bpt; related->related_breakpoint != bpt;
13109 related = related->related_breakpoint);
13110 related->related_breakpoint = bpt->related_breakpoint;
13111 bpt->related_breakpoint = bpt;
e5a0a904
JK
13112 }
13113
a9634178
TJB
13114 /* watch_command_1 creates a watchpoint but only sets its number if
13115 update_watchpoint succeeds in creating its bp_locations. If there's
13116 a problem in that process, we'll be asked to delete the half-created
13117 watchpoint. In that case, don't announce the deletion. */
13118 if (bpt->number)
13119 observer_notify_breakpoint_deleted (bpt);
c906108c 13120
c906108c
SS
13121 if (breakpoint_chain == bpt)
13122 breakpoint_chain = bpt->next;
13123
c906108c
SS
13124 ALL_BREAKPOINTS (b)
13125 if (b->next == bpt)
c5aa993b
JM
13126 {
13127 b->next = bpt->next;
13128 break;
13129 }
c906108c 13130
f431efe5
PA
13131 /* Be sure no bpstat's are pointing at the breakpoint after it's
13132 been freed. */
13133 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13134 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13135 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13136 commands are associated with the bpstat; if we remove it here,
13137 then the later call to bpstat_do_actions (&stop_bpstat); in
13138 event-top.c won't do anything, and temporary breakpoints with
13139 commands won't work. */
13140
13141 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13142
4a64f543
MS
13143 /* Now that breakpoint is removed from breakpoint list, update the
13144 global location list. This will remove locations that used to
13145 belong to this breakpoint. Do this before freeing the breakpoint
13146 itself, since remove_breakpoint looks at location's owner. It
13147 might be better design to have location completely
13148 self-contained, but it's not the case now. */
b60e7edf 13149 update_global_location_list (0);
74960c60 13150
348d480f 13151 bpt->ops->dtor (bpt);
4a64f543
MS
13152 /* On the chance that someone will soon try again to delete this
13153 same bp, we mark it as deleted before freeing its storage. */
c906108c 13154 bpt->type = bp_none;
b8c9b27d 13155 xfree (bpt);
c906108c
SS
13156}
13157
4d6140d9
AC
13158static void
13159do_delete_breakpoint_cleanup (void *b)
13160{
13161 delete_breakpoint (b);
13162}
13163
13164struct cleanup *
13165make_cleanup_delete_breakpoint (struct breakpoint *b)
13166{
13167 return make_cleanup (do_delete_breakpoint_cleanup, b);
13168}
13169
51be5b68
PA
13170/* Iterator function to call a user-provided callback function once
13171 for each of B and its related breakpoints. */
13172
13173static void
13174iterate_over_related_breakpoints (struct breakpoint *b,
13175 void (*function) (struct breakpoint *,
13176 void *),
13177 void *data)
13178{
13179 struct breakpoint *related;
13180
13181 related = b;
13182 do
13183 {
13184 struct breakpoint *next;
13185
13186 /* FUNCTION may delete RELATED. */
13187 next = related->related_breakpoint;
13188
13189 if (next == related)
13190 {
13191 /* RELATED is the last ring entry. */
13192 function (related, data);
13193
13194 /* FUNCTION may have deleted it, so we'd never reach back to
13195 B. There's nothing left to do anyway, so just break
13196 out. */
13197 break;
13198 }
13199 else
13200 function (related, data);
13201
13202 related = next;
13203 }
13204 while (related != b);
13205}
95a42b64
TT
13206
13207static void
13208do_delete_breakpoint (struct breakpoint *b, void *ignore)
13209{
13210 delete_breakpoint (b);
13211}
13212
51be5b68
PA
13213/* A callback for map_breakpoint_numbers that calls
13214 delete_breakpoint. */
13215
13216static void
13217do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13218{
13219 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13220}
13221
c906108c 13222void
fba45db2 13223delete_command (char *arg, int from_tty)
c906108c 13224{
35df4500 13225 struct breakpoint *b, *b_tmp;
c906108c 13226
ea9365bb
TT
13227 dont_repeat ();
13228
c906108c
SS
13229 if (arg == 0)
13230 {
13231 int breaks_to_delete = 0;
13232
46c6471b
PA
13233 /* Delete all breakpoints if no argument. Do not delete
13234 internal breakpoints, these have to be deleted with an
13235 explicit breakpoint number argument. */
c5aa993b 13236 ALL_BREAKPOINTS (b)
46c6471b 13237 if (user_breakpoint_p (b))
973d738b
DJ
13238 {
13239 breaks_to_delete = 1;
13240 break;
13241 }
c906108c
SS
13242
13243 /* Ask user only if there are some breakpoints to delete. */
13244 if (!from_tty
e2e0b3e5 13245 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13246 {
35df4500 13247 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13248 if (user_breakpoint_p (b))
c5aa993b 13249 delete_breakpoint (b);
c906108c
SS
13250 }
13251 }
13252 else
51be5b68 13253 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13254}
13255
0d381245
VP
13256static int
13257all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13258{
0d381245 13259 for (; loc; loc = loc->next)
8645ff69
UW
13260 if (!loc->shlib_disabled
13261 && !loc->pspace->executing_startup)
0d381245
VP
13262 return 0;
13263 return 1;
fe3f5fa8
VP
13264}
13265
776592bf
DE
13266/* Subroutine of update_breakpoint_locations to simplify it.
13267 Return non-zero if multiple fns in list LOC have the same name.
13268 Null names are ignored. */
13269
13270static int
13271ambiguous_names_p (struct bp_location *loc)
13272{
13273 struct bp_location *l;
13274 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13275 (int (*) (const void *,
13276 const void *)) streq,
776592bf
DE
13277 NULL, xcalloc, xfree);
13278
13279 for (l = loc; l != NULL; l = l->next)
13280 {
13281 const char **slot;
13282 const char *name = l->function_name;
13283
13284 /* Allow for some names to be NULL, ignore them. */
13285 if (name == NULL)
13286 continue;
13287
13288 slot = (const char **) htab_find_slot (htab, (const void *) name,
13289 INSERT);
4a64f543
MS
13290 /* NOTE: We can assume slot != NULL here because xcalloc never
13291 returns NULL. */
776592bf
DE
13292 if (*slot != NULL)
13293 {
13294 htab_delete (htab);
13295 return 1;
13296 }
13297 *slot = name;
13298 }
13299
13300 htab_delete (htab);
13301 return 0;
13302}
13303
0fb4aa4b
PA
13304/* When symbols change, it probably means the sources changed as well,
13305 and it might mean the static tracepoint markers are no longer at
13306 the same address or line numbers they used to be at last we
13307 checked. Losing your static tracepoints whenever you rebuild is
13308 undesirable. This function tries to resync/rematch gdb static
13309 tracepoints with the markers on the target, for static tracepoints
13310 that have not been set by marker id. Static tracepoint that have
13311 been set by marker id are reset by marker id in breakpoint_re_set.
13312 The heuristic is:
13313
13314 1) For a tracepoint set at a specific address, look for a marker at
13315 the old PC. If one is found there, assume to be the same marker.
13316 If the name / string id of the marker found is different from the
13317 previous known name, assume that means the user renamed the marker
13318 in the sources, and output a warning.
13319
13320 2) For a tracepoint set at a given line number, look for a marker
13321 at the new address of the old line number. If one is found there,
13322 assume to be the same marker. If the name / string id of the
13323 marker found is different from the previous known name, assume that
13324 means the user renamed the marker in the sources, and output a
13325 warning.
13326
13327 3) If a marker is no longer found at the same address or line, it
13328 may mean the marker no longer exists. But it may also just mean
13329 the code changed a bit. Maybe the user added a few lines of code
13330 that made the marker move up or down (in line number terms). Ask
13331 the target for info about the marker with the string id as we knew
13332 it. If found, update line number and address in the matching
13333 static tracepoint. This will get confused if there's more than one
13334 marker with the same ID (possible in UST, although unadvised
13335 precisely because it confuses tools). */
13336
13337static struct symtab_and_line
13338update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13339{
d9b3f62e 13340 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13341 struct static_tracepoint_marker marker;
13342 CORE_ADDR pc;
13343 int i;
13344
13345 pc = sal.pc;
13346 if (sal.line)
13347 find_line_pc (sal.symtab, sal.line, &pc);
13348
13349 if (target_static_tracepoint_marker_at (pc, &marker))
13350 {
d9b3f62e 13351 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13352 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13353 b->number,
d9b3f62e 13354 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13355
d9b3f62e
PA
13356 xfree (tp->static_trace_marker_id);
13357 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13358 release_static_tracepoint_marker (&marker);
13359
13360 return sal;
13361 }
13362
13363 /* Old marker wasn't found on target at lineno. Try looking it up
13364 by string ID. */
13365 if (!sal.explicit_pc
13366 && sal.line != 0
13367 && sal.symtab != NULL
d9b3f62e 13368 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13369 {
13370 VEC(static_tracepoint_marker_p) *markers;
13371
13372 markers
d9b3f62e 13373 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13374
13375 if (!VEC_empty(static_tracepoint_marker_p, markers))
13376 {
80e1d417 13377 struct symtab_and_line sal2;
0fb4aa4b 13378 struct symbol *sym;
80e1d417 13379 struct static_tracepoint_marker *tpmarker;
79a45e25 13380 struct ui_out *uiout = current_uiout;
0fb4aa4b 13381
80e1d417 13382 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13383
d9b3f62e 13384 xfree (tp->static_trace_marker_id);
80e1d417 13385 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13386
13387 warning (_("marker for static tracepoint %d (%s) not "
13388 "found at previous line number"),
d9b3f62e 13389 b->number, tp->static_trace_marker_id);
0fb4aa4b 13390
80e1d417 13391 init_sal (&sal2);
0fb4aa4b 13392
80e1d417 13393 sal2.pc = tpmarker->address;
0fb4aa4b 13394
80e1d417
AS
13395 sal2 = find_pc_line (tpmarker->address, 0);
13396 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13397 ui_out_text (uiout, "Now in ");
13398 if (sym)
13399 {
13400 ui_out_field_string (uiout, "func",
13401 SYMBOL_PRINT_NAME (sym));
13402 ui_out_text (uiout, " at ");
13403 }
80e1d417 13404 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
13405 ui_out_text (uiout, ":");
13406
13407 if (ui_out_is_mi_like_p (uiout))
13408 {
80e1d417 13409 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
13410
13411 if (fullname)
13412 ui_out_field_string (uiout, "fullname", fullname);
13413 }
13414
80e1d417 13415 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13416 ui_out_text (uiout, "\n");
13417
80e1d417 13418 b->loc->line_number = sal2.line;
0fb4aa4b 13419
f8eba3c6 13420 xfree (b->loc->source_file);
0fb4aa4b 13421 if (sym)
80e1d417 13422 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 13423 else
f8eba3c6 13424 b->loc->source_file = NULL;
0fb4aa4b
PA
13425
13426 xfree (b->addr_string);
13427 b->addr_string = xstrprintf ("%s:%d",
80e1d417 13428 sal2.symtab->filename,
f8eba3c6 13429 b->loc->line_number);
0fb4aa4b
PA
13430
13431 /* Might be nice to check if function changed, and warn if
13432 so. */
13433
80e1d417 13434 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13435 }
13436 }
13437 return sal;
13438}
13439
8d3788bd
VP
13440/* Returns 1 iff locations A and B are sufficiently same that
13441 we don't need to report breakpoint as changed. */
13442
13443static int
13444locations_are_equal (struct bp_location *a, struct bp_location *b)
13445{
13446 while (a && b)
13447 {
13448 if (a->address != b->address)
13449 return 0;
13450
13451 if (a->shlib_disabled != b->shlib_disabled)
13452 return 0;
13453
13454 if (a->enabled != b->enabled)
13455 return 0;
13456
13457 a = a->next;
13458 b = b->next;
13459 }
13460
13461 if ((a == NULL) != (b == NULL))
13462 return 0;
13463
13464 return 1;
13465}
13466
f1310107
TJB
13467/* Create new breakpoint locations for B (a hardware or software breakpoint)
13468 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13469 a ranged breakpoint. */
13470
0e30163f 13471void
0d381245 13472update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13473 struct symtabs_and_lines sals,
13474 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13475{
13476 int i;
0d381245
VP
13477 struct bp_location *existing_locations = b->loc;
13478
f8eba3c6
TT
13479 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13480 {
13481 /* Ranged breakpoints have only one start location and one end
13482 location. */
13483 b->enable_state = bp_disabled;
13484 update_global_location_list (1);
13485 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13486 "multiple locations found\n"),
13487 b->number);
13488 return;
13489 }
f1310107 13490
4a64f543
MS
13491 /* If there's no new locations, and all existing locations are
13492 pending, don't do anything. This optimizes the common case where
13493 all locations are in the same shared library, that was unloaded.
13494 We'd like to retain the location, so that when the library is
13495 loaded again, we don't loose the enabled/disabled status of the
13496 individual locations. */
0d381245 13497 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13498 return;
13499
fe3f5fa8
VP
13500 b->loc = NULL;
13501
0d381245 13502 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13503 {
f8eba3c6
TT
13504 struct bp_location *new_loc;
13505
13506 switch_to_program_space_and_thread (sals.sals[i].pspace);
13507
13508 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13509
0d381245
VP
13510 /* Reparse conditions, they might contain references to the
13511 old symtab. */
13512 if (b->cond_string != NULL)
13513 {
f1310107 13514 char *s;
bfd189b1 13515 volatile struct gdb_exception e;
fe3f5fa8 13516
0d381245
VP
13517 s = b->cond_string;
13518 TRY_CATCH (e, RETURN_MASK_ERROR)
13519 {
13520 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
13521 0);
13522 }
13523 if (e.reason < 0)
13524 {
3e43a32a
MS
13525 warning (_("failed to reevaluate condition "
13526 "for breakpoint %d: %s"),
0d381245
VP
13527 b->number, e.message);
13528 new_loc->enabled = 0;
13529 }
13530 }
fe3f5fa8 13531
f1310107
TJB
13532 if (sals_end.nelts)
13533 {
13534 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13535
13536 new_loc->length = end - sals.sals[0].pc + 1;
13537 }
0d381245 13538 }
fe3f5fa8 13539
514f746b
AR
13540 /* Update locations of permanent breakpoints. */
13541 if (b->enable_state == bp_permanent)
13542 make_breakpoint_permanent (b);
13543
4a64f543
MS
13544 /* If possible, carry over 'disable' status from existing
13545 breakpoints. */
0d381245
VP
13546 {
13547 struct bp_location *e = existing_locations;
776592bf
DE
13548 /* If there are multiple breakpoints with the same function name,
13549 e.g. for inline functions, comparing function names won't work.
13550 Instead compare pc addresses; this is just a heuristic as things
13551 may have moved, but in practice it gives the correct answer
13552 often enough until a better solution is found. */
13553 int have_ambiguous_names = ambiguous_names_p (b->loc);
13554
0d381245
VP
13555 for (; e; e = e->next)
13556 {
13557 if (!e->enabled && e->function_name)
13558 {
13559 struct bp_location *l = b->loc;
776592bf
DE
13560 if (have_ambiguous_names)
13561 {
13562 for (; l; l = l->next)
f1310107 13563 if (breakpoint_locations_match (e, l))
776592bf
DE
13564 {
13565 l->enabled = 0;
13566 break;
13567 }
13568 }
13569 else
13570 {
13571 for (; l; l = l->next)
13572 if (l->function_name
13573 && strcmp (e->function_name, l->function_name) == 0)
13574 {
13575 l->enabled = 0;
13576 break;
13577 }
13578 }
0d381245
VP
13579 }
13580 }
13581 }
fe3f5fa8 13582
8d3788bd
VP
13583 if (!locations_are_equal (existing_locations, b->loc))
13584 observer_notify_breakpoint_modified (b);
13585
b60e7edf 13586 update_global_location_list (1);
fe3f5fa8
VP
13587}
13588
ef23e705
TJB
13589/* Find the SaL locations corresponding to the given ADDR_STRING.
13590 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13591
13592static struct symtabs_and_lines
13593addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13594{
13595 char *s;
02d20e4a 13596 struct symtabs_and_lines sals = {0};
f8eba3c6 13597 volatile struct gdb_exception e;
ef23e705 13598
983af33b 13599 gdb_assert (b->ops != NULL);
ef23e705 13600 s = addr_string;
ef23e705
TJB
13601
13602 TRY_CATCH (e, RETURN_MASK_ERROR)
13603 {
983af33b 13604 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
13605 }
13606 if (e.reason < 0)
13607 {
13608 int not_found_and_ok = 0;
13609 /* For pending breakpoints, it's expected that parsing will
13610 fail until the right shared library is loaded. User has
13611 already told to create pending breakpoints and don't need
13612 extra messages. If breakpoint is in bp_shlib_disabled
13613 state, then user already saw the message about that
13614 breakpoint being disabled, and don't want to see more
13615 errors. */
58438ac1 13616 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
13617 && (b->condition_not_parsed
13618 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13619 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13620 || b->enable_state == bp_disabled))
13621 not_found_and_ok = 1;
13622
13623 if (!not_found_and_ok)
13624 {
13625 /* We surely don't want to warn about the same breakpoint
13626 10 times. One solution, implemented here, is disable
13627 the breakpoint on error. Another solution would be to
13628 have separate 'warning emitted' flag. Since this
13629 happens only when a binary has changed, I don't know
13630 which approach is better. */
13631 b->enable_state = bp_disabled;
13632 throw_exception (e);
13633 }
13634 }
13635
58438ac1 13636 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 13637 {
f8eba3c6 13638 int i;
ef23e705 13639
f8eba3c6
TT
13640 for (i = 0; i < sals.nelts; ++i)
13641 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
13642 if (b->condition_not_parsed && s && s[0])
13643 {
13644 char *cond_string = 0;
13645 int thread = -1;
13646 int task = 0;
e7e0cddf 13647 char *extra_string = NULL;
ef23e705
TJB
13648
13649 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
13650 &cond_string, &thread, &task,
13651 &extra_string);
ef23e705
TJB
13652 if (cond_string)
13653 b->cond_string = cond_string;
13654 b->thread = thread;
13655 b->task = task;
e7e0cddf
SS
13656 if (extra_string)
13657 b->extra_string = extra_string;
ef23e705
TJB
13658 b->condition_not_parsed = 0;
13659 }
13660
983af33b 13661 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 13662 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 13663
58438ac1
TT
13664 *found = 1;
13665 }
13666 else
13667 *found = 0;
ef23e705
TJB
13668
13669 return sals;
13670}
13671
348d480f
PA
13672/* The default re_set method, for typical hardware or software
13673 breakpoints. Reevaluate the breakpoint and recreate its
13674 locations. */
13675
13676static void
28010a5d 13677breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
13678{
13679 int found;
f1310107 13680 struct symtabs_and_lines sals, sals_end;
ef23e705 13681 struct symtabs_and_lines expanded = {0};
f1310107 13682 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
13683
13684 sals = addr_string_to_sals (b, b->addr_string, &found);
13685 if (found)
13686 {
13687 make_cleanup (xfree, sals.sals);
f8eba3c6 13688 expanded = sals;
ef23e705
TJB
13689 }
13690
f1310107
TJB
13691 if (b->addr_string_range_end)
13692 {
13693 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13694 if (found)
13695 {
13696 make_cleanup (xfree, sals_end.sals);
f8eba3c6 13697 expanded_end = sals_end;
f1310107
TJB
13698 }
13699 }
13700
13701 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
13702}
13703
983af33b
SDJ
13704/* Default method for creating SALs from an address string. It basically
13705 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13706
13707static void
13708create_sals_from_address_default (char **arg,
13709 struct linespec_result *canonical,
13710 enum bptype type_wanted,
13711 char *addr_start, char **copy_arg)
13712{
13713 parse_breakpoint_sals (arg, canonical);
13714}
13715
13716/* Call create_breakpoints_sal for the given arguments. This is the default
13717 function for the `create_breakpoints_sal' method of
13718 breakpoint_ops. */
13719
13720static void
13721create_breakpoints_sal_default (struct gdbarch *gdbarch,
13722 struct linespec_result *canonical,
13723 struct linespec_sals *lsal,
13724 char *cond_string,
e7e0cddf 13725 char *extra_string,
983af33b
SDJ
13726 enum bptype type_wanted,
13727 enum bpdisp disposition,
13728 int thread,
13729 int task, int ignore_count,
13730 const struct breakpoint_ops *ops,
13731 int from_tty, int enabled,
44f238bb 13732 int internal, unsigned flags)
983af33b
SDJ
13733{
13734 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 13735 extra_string,
983af33b
SDJ
13736 type_wanted, disposition,
13737 thread, task, ignore_count, ops, from_tty,
44f238bb 13738 enabled, internal, flags);
983af33b
SDJ
13739}
13740
13741/* Decode the line represented by S by calling decode_line_full. This is the
13742 default function for the `decode_linespec' method of breakpoint_ops. */
13743
13744static void
13745decode_linespec_default (struct breakpoint *b, char **s,
13746 struct symtabs_and_lines *sals)
13747{
13748 struct linespec_result canonical;
13749
13750 init_linespec_result (&canonical);
13751 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13752 (struct symtab *) NULL, 0,
13753 &canonical, multiple_symbols_all,
13754 b->filter);
13755
13756 /* We should get 0 or 1 resulting SALs. */
13757 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13758
13759 if (VEC_length (linespec_sals, canonical.sals) > 0)
13760 {
13761 struct linespec_sals *lsal;
13762
13763 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13764 *sals = lsal->sals;
13765 /* Arrange it so the destructor does not free the
13766 contents. */
13767 lsal->sals.sals = NULL;
13768 }
13769
13770 destroy_linespec_result (&canonical);
13771}
13772
28010a5d
PA
13773/* Prepare the global context for a re-set of breakpoint B. */
13774
13775static struct cleanup *
13776prepare_re_set_context (struct breakpoint *b)
13777{
13778 struct cleanup *cleanups;
13779
13780 input_radix = b->input_radix;
13781 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
13782 if (b->pspace != NULL)
13783 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
13784 set_language (b->language);
13785
13786 return cleanups;
ef23e705
TJB
13787}
13788
c906108c
SS
13789/* Reset a breakpoint given it's struct breakpoint * BINT.
13790 The value we return ends up being the return value from catch_errors.
13791 Unused in this case. */
13792
13793static int
4efb68b1 13794breakpoint_re_set_one (void *bint)
c906108c 13795{
4a64f543 13796 /* Get past catch_errs. */
53a5351d 13797 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 13798 struct cleanup *cleanups;
c906108c 13799
348d480f
PA
13800 cleanups = prepare_re_set_context (b);
13801 b->ops->re_set (b);
13802 do_cleanups (cleanups);
c906108c
SS
13803 return 0;
13804}
13805
69de3c6a 13806/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 13807void
69de3c6a 13808breakpoint_re_set (void)
c906108c 13809{
35df4500 13810 struct breakpoint *b, *b_tmp;
c906108c
SS
13811 enum language save_language;
13812 int save_input_radix;
6c95b8df 13813 struct cleanup *old_chain;
c5aa993b 13814
c906108c
SS
13815 save_language = current_language->la_language;
13816 save_input_radix = input_radix;
6c95b8df
PA
13817 old_chain = save_current_program_space ();
13818
35df4500 13819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 13820 {
4a64f543 13821 /* Format possible error msg. */
fe3f5fa8 13822 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
13823 b->number);
13824 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 13825 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 13826 do_cleanups (cleanups);
c5aa993b 13827 }
c906108c
SS
13828 set_language (save_language);
13829 input_radix = save_input_radix;
e62c965a 13830
0756c555 13831 jit_breakpoint_re_set ();
4efc6507 13832
6c95b8df
PA
13833 do_cleanups (old_chain);
13834
af02033e
PP
13835 create_overlay_event_breakpoint ();
13836 create_longjmp_master_breakpoint ();
13837 create_std_terminate_master_breakpoint ();
186c406b 13838 create_exception_master_breakpoint ();
1bfeeb0f
JL
13839
13840 /* While we're at it, reset the skip list too. */
13841 skip_re_set ();
c906108c
SS
13842}
13843\f
c906108c
SS
13844/* Reset the thread number of this breakpoint:
13845
13846 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13847 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13848void
fba45db2 13849breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13850{
13851 if (b->thread != -1)
13852 {
39f77062
KB
13853 if (in_thread_list (inferior_ptid))
13854 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
13855
13856 /* We're being called after following a fork. The new fork is
13857 selected as current, and unless this was a vfork will have a
13858 different program space from the original thread. Reset that
13859 as well. */
13860 b->loc->pspace = current_program_space;
c906108c
SS
13861 }
13862}
13863
03ac34d5
MS
13864/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13865 If from_tty is nonzero, it prints a message to that effect,
13866 which ends with a period (no newline). */
13867
c906108c 13868void
fba45db2 13869set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13870{
52f0bd74 13871 struct breakpoint *b;
c906108c
SS
13872
13873 if (count < 0)
13874 count = 0;
13875
13876 ALL_BREAKPOINTS (b)
13877 if (b->number == bptnum)
c5aa993b 13878 {
d77f58be
SS
13879 if (is_tracepoint (b))
13880 {
13881 if (from_tty && count != 0)
13882 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13883 bptnum);
13884 return;
13885 }
13886
c5aa993b 13887 b->ignore_count = count;
221ea385
KS
13888 if (from_tty)
13889 {
13890 if (count == 0)
3e43a32a
MS
13891 printf_filtered (_("Will stop next time "
13892 "breakpoint %d is reached."),
221ea385
KS
13893 bptnum);
13894 else if (count == 1)
a3f17187 13895 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13896 bptnum);
13897 else
3e43a32a
MS
13898 printf_filtered (_("Will ignore next %d "
13899 "crossings of breakpoint %d."),
221ea385
KS
13900 count, bptnum);
13901 }
c5aa993b 13902 breakpoints_changed ();
8d3788bd 13903 observer_notify_breakpoint_modified (b);
c5aa993b
JM
13904 return;
13905 }
c906108c 13906
8a3fe4f8 13907 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13908}
13909
c906108c
SS
13910/* Command to set ignore-count of breakpoint N to COUNT. */
13911
13912static void
fba45db2 13913ignore_command (char *args, int from_tty)
c906108c
SS
13914{
13915 char *p = args;
52f0bd74 13916 int num;
c906108c
SS
13917
13918 if (p == 0)
e2e0b3e5 13919 error_no_arg (_("a breakpoint number"));
c5aa993b 13920
c906108c 13921 num = get_number (&p);
5c44784c 13922 if (num == 0)
8a3fe4f8 13923 error (_("bad breakpoint number: '%s'"), args);
c906108c 13924 if (*p == 0)
8a3fe4f8 13925 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13926
13927 set_ignore_count (num,
13928 longest_to_int (value_as_long (parse_and_eval (p))),
13929 from_tty);
221ea385
KS
13930 if (from_tty)
13931 printf_filtered ("\n");
c906108c
SS
13932}
13933\f
13934/* Call FUNCTION on each of the breakpoints
13935 whose numbers are given in ARGS. */
13936
13937static void
95a42b64
TT
13938map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
13939 void *),
13940 void *data)
c906108c 13941{
52f0bd74
AC
13942 int num;
13943 struct breakpoint *b, *tmp;
11cf8741 13944 int match;
197f0a60 13945 struct get_number_or_range_state state;
c906108c 13946
197f0a60 13947 if (args == 0)
e2e0b3e5 13948 error_no_arg (_("one or more breakpoint numbers"));
c906108c 13949
197f0a60
TT
13950 init_number_or_range (&state, args);
13951
13952 while (!state.finished)
c906108c 13953 {
197f0a60
TT
13954 char *p = state.string;
13955
11cf8741 13956 match = 0;
c5aa993b 13957
197f0a60 13958 num = get_number_or_range (&state);
5c44784c 13959 if (num == 0)
c5aa993b 13960 {
8a3fe4f8 13961 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
13962 }
13963 else
13964 {
13965 ALL_BREAKPOINTS_SAFE (b, tmp)
13966 if (b->number == num)
13967 {
11cf8741 13968 match = 1;
cdac0397 13969 function (b, data);
11cf8741 13970 break;
5c44784c 13971 }
11cf8741 13972 if (match == 0)
a3f17187 13973 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 13974 }
c906108c
SS
13975 }
13976}
13977
0d381245
VP
13978static struct bp_location *
13979find_location_by_number (char *number)
13980{
13981 char *dot = strchr (number, '.');
13982 char *p1;
13983 int bp_num;
13984 int loc_num;
13985 struct breakpoint *b;
13986 struct bp_location *loc;
13987
13988 *dot = '\0';
13989
13990 p1 = number;
197f0a60 13991 bp_num = get_number (&p1);
0d381245
VP
13992 if (bp_num == 0)
13993 error (_("Bad breakpoint number '%s'"), number);
13994
13995 ALL_BREAKPOINTS (b)
13996 if (b->number == bp_num)
13997 {
13998 break;
13999 }
14000
14001 if (!b || b->number != bp_num)
14002 error (_("Bad breakpoint number '%s'"), number);
14003
14004 p1 = dot+1;
197f0a60 14005 loc_num = get_number (&p1);
0d381245
VP
14006 if (loc_num == 0)
14007 error (_("Bad breakpoint location number '%s'"), number);
14008
14009 --loc_num;
14010 loc = b->loc;
14011 for (;loc_num && loc; --loc_num, loc = loc->next)
14012 ;
14013 if (!loc)
14014 error (_("Bad breakpoint location number '%s'"), dot+1);
14015
14016 return loc;
14017}
14018
14019
1900040c
MS
14020/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14021 If from_tty is nonzero, it prints a message to that effect,
14022 which ends with a period (no newline). */
14023
c906108c 14024void
fba45db2 14025disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14026{
14027 /* Never disable a watchpoint scope breakpoint; we want to
14028 hit them when we leave scope so we can delete both the
14029 watchpoint and its scope breakpoint at that time. */
14030 if (bpt->type == bp_watchpoint_scope)
14031 return;
14032
c2c6d25f 14033 /* You can't disable permanent breakpoints. */
b5de0fa7 14034 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14035 return;
14036
b5de0fa7 14037 bpt->enable_state = bp_disabled;
c906108c 14038
b775012e
LM
14039 /* Mark breakpoint locations modified. */
14040 mark_breakpoint_modified (bpt);
14041
d248b706
KY
14042 if (target_supports_enable_disable_tracepoint ()
14043 && current_trace_status ()->running && is_tracepoint (bpt))
14044 {
14045 struct bp_location *location;
14046
14047 for (location = bpt->loc; location; location = location->next)
14048 target_disable_tracepoint (location);
14049 }
14050
b60e7edf 14051 update_global_location_list (0);
c906108c 14052
8d3788bd 14053 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14054}
14055
51be5b68
PA
14056/* A callback for iterate_over_related_breakpoints. */
14057
14058static void
14059do_disable_breakpoint (struct breakpoint *b, void *ignore)
14060{
14061 disable_breakpoint (b);
14062}
14063
95a42b64
TT
14064/* A callback for map_breakpoint_numbers that calls
14065 disable_breakpoint. */
14066
14067static void
14068do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14069{
51be5b68 14070 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14071}
14072
c906108c 14073static void
fba45db2 14074disable_command (char *args, int from_tty)
c906108c 14075{
c906108c 14076 if (args == 0)
46c6471b
PA
14077 {
14078 struct breakpoint *bpt;
14079
14080 ALL_BREAKPOINTS (bpt)
14081 if (user_breakpoint_p (bpt))
14082 disable_breakpoint (bpt);
14083 }
0d381245
VP
14084 else if (strchr (args, '.'))
14085 {
14086 struct bp_location *loc = find_location_by_number (args);
14087 if (loc)
d248b706 14088 {
b775012e
LM
14089 if (loc->enabled)
14090 {
14091 loc->enabled = 0;
14092 mark_breakpoint_location_modified (loc);
14093 }
d248b706
KY
14094 if (target_supports_enable_disable_tracepoint ()
14095 && current_trace_status ()->running && loc->owner
14096 && is_tracepoint (loc->owner))
14097 target_disable_tracepoint (loc);
14098 }
b60e7edf 14099 update_global_location_list (0);
0d381245 14100 }
c906108c 14101 else
95a42b64 14102 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
14103}
14104
14105static void
816338b5
SS
14106enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14107 int count)
c906108c 14108{
afe38095 14109 int target_resources_ok;
c906108c
SS
14110
14111 if (bpt->type == bp_hardware_breakpoint)
14112 {
14113 int i;
c5aa993b 14114 i = hw_breakpoint_used_count ();
53a5351d 14115 target_resources_ok =
d92524f1 14116 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14117 i + 1, 0);
c906108c 14118 if (target_resources_ok == 0)
8a3fe4f8 14119 error (_("No hardware breakpoint support in the target."));
c906108c 14120 else if (target_resources_ok < 0)
8a3fe4f8 14121 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14122 }
14123
cc60f2e3 14124 if (is_watchpoint (bpt))
c906108c 14125 {
d07205c2
JK
14126 /* Initialize it just to avoid a GCC false warning. */
14127 enum enable_state orig_enable_state = 0;
bfd189b1 14128 volatile struct gdb_exception e;
dde02812
ES
14129
14130 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14131 {
3a5c3e22
PA
14132 struct watchpoint *w = (struct watchpoint *) bpt;
14133
1e718ff1
TJB
14134 orig_enable_state = bpt->enable_state;
14135 bpt->enable_state = bp_enabled;
3a5c3e22 14136 update_watchpoint (w, 1 /* reparse */);
c906108c 14137 }
dde02812 14138 if (e.reason < 0)
c5aa993b 14139 {
1e718ff1 14140 bpt->enable_state = orig_enable_state;
dde02812
ES
14141 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14142 bpt->number);
14143 return;
c5aa993b 14144 }
c906108c 14145 }
0101ce28 14146
b4c291bb
KH
14147 if (bpt->enable_state != bp_permanent)
14148 bpt->enable_state = bp_enabled;
d248b706 14149
b775012e
LM
14150 bpt->enable_state = bp_enabled;
14151
14152 /* Mark breakpoint locations modified. */
14153 mark_breakpoint_modified (bpt);
14154
d248b706
KY
14155 if (target_supports_enable_disable_tracepoint ()
14156 && current_trace_status ()->running && is_tracepoint (bpt))
14157 {
14158 struct bp_location *location;
14159
14160 for (location = bpt->loc; location; location = location->next)
14161 target_enable_tracepoint (location);
14162 }
14163
b4c291bb 14164 bpt->disposition = disposition;
816338b5 14165 bpt->enable_count = count;
b60e7edf 14166 update_global_location_list (1);
b4c291bb
KH
14167 breakpoints_changed ();
14168
8d3788bd 14169 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14170}
14171
fe3f5fa8 14172
c906108c 14173void
fba45db2 14174enable_breakpoint (struct breakpoint *bpt)
c906108c 14175{
816338b5 14176 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14177}
14178
14179static void
14180do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14181{
14182 enable_breakpoint (bpt);
c906108c
SS
14183}
14184
95a42b64
TT
14185/* A callback for map_breakpoint_numbers that calls
14186 enable_breakpoint. */
14187
14188static void
14189do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14190{
51be5b68 14191 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14192}
14193
c906108c
SS
14194/* The enable command enables the specified breakpoints (or all defined
14195 breakpoints) so they once again become (or continue to be) effective
1272ad14 14196 in stopping the inferior. */
c906108c 14197
c906108c 14198static void
fba45db2 14199enable_command (char *args, int from_tty)
c906108c 14200{
c906108c 14201 if (args == 0)
46c6471b
PA
14202 {
14203 struct breakpoint *bpt;
14204
14205 ALL_BREAKPOINTS (bpt)
14206 if (user_breakpoint_p (bpt))
14207 enable_breakpoint (bpt);
14208 }
0d381245
VP
14209 else if (strchr (args, '.'))
14210 {
14211 struct bp_location *loc = find_location_by_number (args);
14212 if (loc)
d248b706 14213 {
b775012e
LM
14214 if (!loc->enabled)
14215 {
14216 loc->enabled = 1;
14217 mark_breakpoint_location_modified (loc);
14218 }
d248b706
KY
14219 if (target_supports_enable_disable_tracepoint ()
14220 && current_trace_status ()->running && loc->owner
14221 && is_tracepoint (loc->owner))
14222 target_enable_tracepoint (loc);
14223 }
b60e7edf 14224 update_global_location_list (1);
0d381245 14225 }
c906108c 14226 else
95a42b64 14227 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
14228}
14229
816338b5
SS
14230/* This struct packages up disposition data for application to multiple
14231 breakpoints. */
14232
14233struct disp_data
14234{
14235 enum bpdisp disp;
14236 int count;
14237};
14238
c906108c 14239static void
51be5b68
PA
14240do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14241{
816338b5 14242 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14243
816338b5 14244 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14245}
14246
14247static void
14248do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14249{
816338b5 14250 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14251
14252 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14253}
14254
c906108c 14255static void
fba45db2 14256enable_once_command (char *args, int from_tty)
c906108c 14257{
51be5b68 14258 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14259}
14260
816338b5
SS
14261static void
14262do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14263{
14264 struct disp_data disp = { disp_disable, *(int *) countptr };
14265
14266 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14267}
14268
14269static void
14270enable_count_command (char *args, int from_tty)
14271{
14272 int count = get_number (&args);
14273
14274 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14275}
14276
c906108c 14277static void
51be5b68 14278do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14279{
816338b5 14280 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14281
14282 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14283}
14284
c906108c 14285static void
fba45db2 14286enable_delete_command (char *args, int from_tty)
c906108c 14287{
51be5b68 14288 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14289}
14290\f
fa8d40ab
JJ
14291static void
14292set_breakpoint_cmd (char *args, int from_tty)
14293{
14294}
14295
14296static void
14297show_breakpoint_cmd (char *args, int from_tty)
14298{
14299}
14300
1f3b5d1b
PP
14301/* Invalidate last known value of any hardware watchpoint if
14302 the memory which that value represents has been written to by
14303 GDB itself. */
14304
14305static void
14306invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14307 const bfd_byte *data)
14308{
14309 struct breakpoint *bp;
14310
14311 ALL_BREAKPOINTS (bp)
14312 if (bp->enable_state == bp_enabled
3a5c3e22 14313 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14314 {
3a5c3e22 14315 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14316
3a5c3e22
PA
14317 if (wp->val_valid && wp->val)
14318 {
14319 struct bp_location *loc;
14320
14321 for (loc = bp->loc; loc != NULL; loc = loc->next)
14322 if (loc->loc_type == bp_loc_hardware_watchpoint
14323 && loc->address + loc->length > addr
14324 && addr + len > loc->address)
14325 {
14326 value_free (wp->val);
14327 wp->val = NULL;
14328 wp->val_valid = 0;
14329 }
14330 }
1f3b5d1b
PP
14331 }
14332}
14333
1bfeeb0f
JL
14334/* Use the last displayed codepoint's values, or nothing
14335 if they aren't valid. */
c906108c
SS
14336
14337struct symtabs_and_lines
f8eba3c6 14338decode_line_spec_1 (char *string, int flags)
c906108c
SS
14339{
14340 struct symtabs_and_lines sals;
cc59ec59 14341
c906108c 14342 if (string == 0)
8a3fe4f8 14343 error (_("Empty line specification."));
1bfeeb0f 14344 if (last_displayed_sal_is_valid ())
f8eba3c6 14345 sals = decode_line_1 (&string, flags,
1bfeeb0f 14346 get_last_displayed_symtab (),
f8eba3c6 14347 get_last_displayed_line ());
c906108c 14348 else
f8eba3c6 14349 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 14350 if (*string)
8a3fe4f8 14351 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
14352 return sals;
14353}
8181d85f
DJ
14354
14355/* Create and insert a raw software breakpoint at PC. Return an
14356 identifier, which should be used to remove the breakpoint later.
14357 In general, places which call this should be using something on the
14358 breakpoint chain instead; this function should be eliminated
14359 someday. */
14360
14361void *
6c95b8df
PA
14362deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14363 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14364{
14365 struct bp_target_info *bp_tgt;
14366
6c95b8df 14367 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14368
6c95b8df 14369 bp_tgt->placed_address_space = aspace;
8181d85f 14370 bp_tgt->placed_address = pc;
6c95b8df 14371
a6d9a66e 14372 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14373 {
14374 /* Could not insert the breakpoint. */
14375 xfree (bp_tgt);
14376 return NULL;
14377 }
14378
14379 return bp_tgt;
14380}
14381
4a64f543
MS
14382/* Remove a breakpoint BP inserted by
14383 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14384
14385int
a6d9a66e 14386deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14387{
14388 struct bp_target_info *bp_tgt = bp;
14389 int ret;
14390
a6d9a66e 14391 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14392 xfree (bp_tgt);
14393
14394 return ret;
14395}
14396
4a64f543
MS
14397/* One (or perhaps two) breakpoints used for software single
14398 stepping. */
8181d85f
DJ
14399
14400static void *single_step_breakpoints[2];
a6d9a66e 14401static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14402
14403/* Create and insert a breakpoint for software single step. */
14404
14405void
6c95b8df 14406insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14407 struct address_space *aspace,
14408 CORE_ADDR next_pc)
8181d85f
DJ
14409{
14410 void **bpt_p;
14411
14412 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14413 {
14414 bpt_p = &single_step_breakpoints[0];
14415 single_step_gdbarch[0] = gdbarch;
14416 }
8181d85f
DJ
14417 else
14418 {
14419 gdb_assert (single_step_breakpoints[1] == NULL);
14420 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14421 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14422 }
14423
4a64f543
MS
14424 /* NOTE drow/2006-04-11: A future improvement to this function would
14425 be to only create the breakpoints once, and actually put them on
14426 the breakpoint chain. That would let us use set_raw_breakpoint.
14427 We could adjust the addresses each time they were needed. Doing
14428 this requires corresponding changes elsewhere where single step
14429 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14430
6c95b8df 14431 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14432 if (*bpt_p == NULL)
5af949e3
UW
14433 error (_("Could not insert single-step breakpoint at %s"),
14434 paddress (gdbarch, next_pc));
8181d85f
DJ
14435}
14436
f02253f1
HZ
14437/* Check if the breakpoints used for software single stepping
14438 were inserted or not. */
14439
14440int
14441single_step_breakpoints_inserted (void)
14442{
14443 return (single_step_breakpoints[0] != NULL
14444 || single_step_breakpoints[1] != NULL);
14445}
14446
8181d85f
DJ
14447/* Remove and delete any breakpoints used for software single step. */
14448
14449void
14450remove_single_step_breakpoints (void)
14451{
14452 gdb_assert (single_step_breakpoints[0] != NULL);
14453
14454 /* See insert_single_step_breakpoint for more about this deprecated
14455 call. */
a6d9a66e
UW
14456 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14457 single_step_breakpoints[0]);
14458 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14459 single_step_breakpoints[0] = NULL;
14460
14461 if (single_step_breakpoints[1] != NULL)
14462 {
a6d9a66e
UW
14463 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14464 single_step_breakpoints[1]);
14465 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14466 single_step_breakpoints[1] = NULL;
14467 }
14468}
14469
d03285ec
UW
14470/* Delete software single step breakpoints without removing them from
14471 the inferior. This is intended to be used if the inferior's address
14472 space where they were inserted is already gone, e.g. after exit or
14473 exec. */
14474
14475void
14476cancel_single_step_breakpoints (void)
14477{
14478 int i;
14479
14480 for (i = 0; i < 2; i++)
14481 if (single_step_breakpoints[i])
14482 {
14483 xfree (single_step_breakpoints[i]);
14484 single_step_breakpoints[i] = NULL;
14485 single_step_gdbarch[i] = NULL;
14486 }
14487}
14488
14489/* Detach software single-step breakpoints from INFERIOR_PTID without
14490 removing them. */
14491
14492static void
14493detach_single_step_breakpoints (void)
14494{
14495 int i;
14496
14497 for (i = 0; i < 2; i++)
14498 if (single_step_breakpoints[i])
14499 target_remove_breakpoint (single_step_gdbarch[i],
14500 single_step_breakpoints[i]);
14501}
14502
4a64f543
MS
14503/* Check whether a software single-step breakpoint is inserted at
14504 PC. */
1aafd4da
UW
14505
14506static int
cc59ec59
MS
14507single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14508 CORE_ADDR pc)
1aafd4da
UW
14509{
14510 int i;
14511
14512 for (i = 0; i < 2; i++)
14513 {
14514 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14515 if (bp_tgt
14516 && breakpoint_address_match (bp_tgt->placed_address_space,
14517 bp_tgt->placed_address,
14518 aspace, pc))
1aafd4da
UW
14519 return 1;
14520 }
14521
14522 return 0;
14523}
14524
a96d9b2e
SDJ
14525/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14526 non-zero otherwise. */
14527static int
14528is_syscall_catchpoint_enabled (struct breakpoint *bp)
14529{
14530 if (syscall_catchpoint_p (bp)
14531 && bp->enable_state != bp_disabled
14532 && bp->enable_state != bp_call_disabled)
14533 return 1;
14534 else
14535 return 0;
14536}
14537
14538int
14539catch_syscall_enabled (void)
14540{
fa3064dd
YQ
14541 struct catch_syscall_inferior_data *inf_data
14542 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 14543
fa3064dd 14544 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
14545}
14546
14547int
14548catching_syscall_number (int syscall_number)
14549{
14550 struct breakpoint *bp;
14551
14552 ALL_BREAKPOINTS (bp)
14553 if (is_syscall_catchpoint_enabled (bp))
14554 {
be5c67c1
PA
14555 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14556
14557 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
14558 {
14559 int i, iter;
14560 for (i = 0;
be5c67c1 14561 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
14562 i++)
14563 if (syscall_number == iter)
14564 return 1;
14565 }
14566 else
14567 return 1;
14568 }
14569
14570 return 0;
14571}
14572
14573/* Complete syscall names. Used by "catch syscall". */
14574static char **
14575catch_syscall_completer (struct cmd_list_element *cmd,
14576 char *text, char *word)
14577{
14578 const char **list = get_syscall_names ();
c38eea1a
MS
14579 char **retlist
14580 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 14581
c38eea1a
MS
14582 xfree (list);
14583 return retlist;
a96d9b2e
SDJ
14584}
14585
1042e4c0
SS
14586/* Tracepoint-specific operations. */
14587
14588/* Set tracepoint count to NUM. */
14589static void
14590set_tracepoint_count (int num)
14591{
14592 tracepoint_count = num;
4fa62494 14593 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14594}
14595
70221824 14596static void
1042e4c0
SS
14597trace_command (char *arg, int from_tty)
14598{
55aa24fb
SDJ
14599 struct breakpoint_ops *ops;
14600 const char *arg_cp = arg;
14601
14602 if (arg && probe_linespec_to_ops (&arg_cp))
14603 ops = &tracepoint_probe_breakpoint_ops;
14604 else
14605 ops = &tracepoint_breakpoint_ops;
14606
8cdf0e15
VP
14607 if (create_breakpoint (get_current_arch (),
14608 arg,
e7e0cddf 14609 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14610 0 /* tempflag */,
14611 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
14612 0 /* Ignore count */,
14613 pending_break_support,
55aa24fb 14614 ops,
8cdf0e15 14615 from_tty,
84f4c1fe 14616 1 /* enabled */,
44f238bb 14617 0 /* internal */, 0))
fd9b8c24 14618 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
14619}
14620
70221824 14621static void
7a697b8d
SS
14622ftrace_command (char *arg, int from_tty)
14623{
8cdf0e15
VP
14624 if (create_breakpoint (get_current_arch (),
14625 arg,
e7e0cddf 14626 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14627 0 /* tempflag */,
14628 bp_fast_tracepoint /* type_wanted */,
14629 0 /* Ignore count */,
14630 pending_break_support,
348d480f 14631 &tracepoint_breakpoint_ops,
0fb4aa4b 14632 from_tty,
84f4c1fe 14633 1 /* enabled */,
44f238bb 14634 0 /* internal */, 0))
0fb4aa4b
PA
14635 set_tracepoint_count (breakpoint_count);
14636}
14637
14638/* strace command implementation. Creates a static tracepoint. */
14639
70221824 14640static void
0fb4aa4b
PA
14641strace_command (char *arg, int from_tty)
14642{
983af33b
SDJ
14643 struct breakpoint_ops *ops;
14644
14645 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14646 or with a normal static tracepoint. */
14647 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14648 ops = &strace_marker_breakpoint_ops;
14649 else
14650 ops = &tracepoint_breakpoint_ops;
14651
0fb4aa4b
PA
14652 if (create_breakpoint (get_current_arch (),
14653 arg,
e7e0cddf 14654 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14655 0 /* tempflag */,
14656 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
14657 0 /* Ignore count */,
14658 pending_break_support,
983af33b 14659 ops,
8cdf0e15 14660 from_tty,
84f4c1fe 14661 1 /* enabled */,
44f238bb 14662 0 /* internal */, 0))
fd9b8c24 14663 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
14664}
14665
409873ef
SS
14666/* Set up a fake reader function that gets command lines from a linked
14667 list that was acquired during tracepoint uploading. */
14668
14669static struct uploaded_tp *this_utp;
3149d8c1 14670static int next_cmd;
409873ef
SS
14671
14672static char *
14673read_uploaded_action (void)
14674{
14675 char *rslt;
14676
3149d8c1 14677 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14678
3149d8c1 14679 next_cmd++;
409873ef
SS
14680
14681 return rslt;
14682}
14683
00bf0b85
SS
14684/* Given information about a tracepoint as recorded on a target (which
14685 can be either a live system or a trace file), attempt to create an
14686 equivalent GDB tracepoint. This is not a reliable process, since
14687 the target does not necessarily have all the information used when
14688 the tracepoint was originally defined. */
14689
d9b3f62e 14690struct tracepoint *
00bf0b85 14691create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14692{
409873ef 14693 char *addr_str, small_buf[100];
d9b3f62e 14694 struct tracepoint *tp;
fd9b8c24 14695
409873ef
SS
14696 if (utp->at_string)
14697 addr_str = utp->at_string;
14698 else
14699 {
14700 /* In the absence of a source location, fall back to raw
14701 address. Since there is no way to confirm that the address
14702 means the same thing as when the trace was started, warn the
14703 user. */
3e43a32a
MS
14704 warning (_("Uploaded tracepoint %d has no "
14705 "source location, using raw address"),
409873ef
SS
14706 utp->number);
14707 sprintf (small_buf, "*%s", hex_string (utp->addr));
14708 addr_str = small_buf;
14709 }
14710
14711 /* There's not much we can do with a sequence of bytecodes. */
14712 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14713 warning (_("Uploaded tracepoint %d condition "
14714 "has no source form, ignoring it"),
409873ef 14715 utp->number);
d5551862 14716
8cdf0e15 14717 if (!create_breakpoint (get_current_arch (),
409873ef 14718 addr_str,
e7e0cddf
SS
14719 utp->cond_string, -1, NULL,
14720 0 /* parse cond/thread */,
8cdf0e15 14721 0 /* tempflag */,
0fb4aa4b 14722 utp->type /* type_wanted */,
8cdf0e15
VP
14723 0 /* Ignore count */,
14724 pending_break_support,
348d480f 14725 &tracepoint_breakpoint_ops,
8cdf0e15 14726 0 /* from_tty */,
84f4c1fe 14727 utp->enabled /* enabled */,
44f238bb
PA
14728 0 /* internal */,
14729 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14730 return NULL;
14731
00bf0b85
SS
14732 set_tracepoint_count (breakpoint_count);
14733
409873ef 14734 /* Get the tracepoint we just created. */
fd9b8c24
PA
14735 tp = get_tracepoint (tracepoint_count);
14736 gdb_assert (tp != NULL);
d5551862 14737
00bf0b85
SS
14738 if (utp->pass > 0)
14739 {
d9b3f62e 14740 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 14741
409873ef 14742 trace_pass_command (small_buf, 0);
00bf0b85
SS
14743 }
14744
409873ef
SS
14745 /* If we have uploaded versions of the original commands, set up a
14746 special-purpose "reader" function and call the usual command line
14747 reader, then pass the result to the breakpoint command-setting
14748 function. */
3149d8c1 14749 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14750 {
409873ef 14751 struct command_line *cmd_list;
00bf0b85 14752
409873ef 14753 this_utp = utp;
3149d8c1 14754 next_cmd = 0;
d5551862 14755
409873ef
SS
14756 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14757
d9b3f62e 14758 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 14759 }
3149d8c1
SS
14760 else if (!VEC_empty (char_ptr, utp->actions)
14761 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14762 warning (_("Uploaded tracepoint %d actions "
14763 "have no source form, ignoring them"),
409873ef 14764 utp->number);
00bf0b85 14765
f196051f
SS
14766 /* Copy any status information that might be available. */
14767 tp->base.hit_count = utp->hit_count;
14768 tp->traceframe_usage = utp->traceframe_usage;
14769
00bf0b85 14770 return tp;
d9b3f62e 14771}
00bf0b85 14772
1042e4c0
SS
14773/* Print information on tracepoint number TPNUM_EXP, or all if
14774 omitted. */
14775
14776static void
e5a67952 14777tracepoints_info (char *args, int from_tty)
1042e4c0 14778{
79a45e25 14779 struct ui_out *uiout = current_uiout;
e5a67952 14780 int num_printed;
1042e4c0 14781
e5a67952 14782 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14783
14784 if (num_printed == 0)
1042e4c0 14785 {
e5a67952 14786 if (args == NULL || *args == '\0')
d77f58be
SS
14787 ui_out_message (uiout, 0, "No tracepoints.\n");
14788 else
e5a67952 14789 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 14790 }
ad443146
SS
14791
14792 default_collect_info ();
1042e4c0
SS
14793}
14794
4a64f543 14795/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14796 Not supported by all targets. */
14797static void
14798enable_trace_command (char *args, int from_tty)
14799{
14800 enable_command (args, from_tty);
14801}
14802
4a64f543 14803/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14804 Not supported by all targets. */
14805static void
14806disable_trace_command (char *args, int from_tty)
14807{
14808 disable_command (args, from_tty);
14809}
14810
4a64f543 14811/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
14812static void
14813delete_trace_command (char *arg, int from_tty)
14814{
35df4500 14815 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14816
14817 dont_repeat ();
14818
14819 if (arg == 0)
14820 {
14821 int breaks_to_delete = 0;
14822
14823 /* Delete all breakpoints if no argument.
14824 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14825 have to be deleted with an explicit breakpoint number
14826 argument. */
1042e4c0 14827 ALL_TRACEPOINTS (b)
46c6471b 14828 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14829 {
14830 breaks_to_delete = 1;
14831 break;
14832 }
1042e4c0
SS
14833
14834 /* Ask user only if there are some breakpoints to delete. */
14835 if (!from_tty
14836 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14837 {
35df4500 14838 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14839 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14840 delete_breakpoint (b);
1042e4c0
SS
14841 }
14842 }
14843 else
51be5b68 14844 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
14845}
14846
197f0a60
TT
14847/* Helper function for trace_pass_command. */
14848
14849static void
d9b3f62e 14850trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14851{
d9b3f62e
PA
14852 tp->pass_count = count;
14853 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
14854 if (from_tty)
14855 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 14856 tp->base.number, count);
197f0a60
TT
14857}
14858
1042e4c0
SS
14859/* Set passcount for tracepoint.
14860
14861 First command argument is passcount, second is tracepoint number.
14862 If tracepoint number omitted, apply to most recently defined.
14863 Also accepts special argument "all". */
14864
14865static void
14866trace_pass_command (char *args, int from_tty)
14867{
d9b3f62e 14868 struct tracepoint *t1;
1042e4c0 14869 unsigned int count;
1042e4c0
SS
14870
14871 if (args == 0 || *args == 0)
3e43a32a
MS
14872 error (_("passcount command requires an "
14873 "argument (count + optional TP num)"));
1042e4c0 14874
4a64f543 14875 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
14876
14877 while (*args && isspace ((int) *args))
14878 args++;
14879
14880 if (*args && strncasecmp (args, "all", 3) == 0)
14881 {
d9b3f62e
PA
14882 struct breakpoint *b;
14883
1042e4c0 14884 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14885 if (*args)
14886 error (_("Junk at end of arguments."));
1042e4c0 14887
d9b3f62e 14888 ALL_TRACEPOINTS (b)
197f0a60 14889 {
d9b3f62e 14890 t1 = (struct tracepoint *) b;
197f0a60
TT
14891 trace_pass_set_count (t1, count, from_tty);
14892 }
14893 }
14894 else if (*args == '\0')
1042e4c0 14895 {
197f0a60 14896 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 14897 if (t1)
197f0a60
TT
14898 trace_pass_set_count (t1, count, from_tty);
14899 }
14900 else
14901 {
14902 struct get_number_or_range_state state;
14903
14904 init_number_or_range (&state, args);
14905 while (!state.finished)
1042e4c0 14906 {
197f0a60
TT
14907 t1 = get_tracepoint_by_number (&args, &state, 1);
14908 if (t1)
14909 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14910 }
14911 }
1042e4c0
SS
14912}
14913
d9b3f62e 14914struct tracepoint *
1042e4c0
SS
14915get_tracepoint (int num)
14916{
14917 struct breakpoint *t;
14918
14919 ALL_TRACEPOINTS (t)
14920 if (t->number == num)
d9b3f62e 14921 return (struct tracepoint *) t;
1042e4c0
SS
14922
14923 return NULL;
14924}
14925
d5551862
SS
14926/* Find the tracepoint with the given target-side number (which may be
14927 different from the tracepoint number after disconnecting and
14928 reconnecting). */
14929
d9b3f62e 14930struct tracepoint *
d5551862
SS
14931get_tracepoint_by_number_on_target (int num)
14932{
d9b3f62e 14933 struct breakpoint *b;
d5551862 14934
d9b3f62e
PA
14935 ALL_TRACEPOINTS (b)
14936 {
14937 struct tracepoint *t = (struct tracepoint *) b;
14938
14939 if (t->number_on_target == num)
14940 return t;
14941 }
d5551862
SS
14942
14943 return NULL;
14944}
14945
1042e4c0 14946/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
14947 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
14948 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 14949 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 14950struct tracepoint *
197f0a60
TT
14951get_tracepoint_by_number (char **arg,
14952 struct get_number_or_range_state *state,
14953 int optional_p)
1042e4c0
SS
14954{
14955 extern int tracepoint_count;
14956 struct breakpoint *t;
14957 int tpnum;
14958 char *instring = arg == NULL ? NULL : *arg;
14959
197f0a60
TT
14960 if (state)
14961 {
14962 gdb_assert (!state->finished);
14963 tpnum = get_number_or_range (state);
14964 }
14965 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
14966 {
14967 if (optional_p)
14968 tpnum = tracepoint_count;
14969 else
14970 error_no_arg (_("tracepoint number"));
14971 }
14972 else
197f0a60 14973 tpnum = get_number (arg);
1042e4c0
SS
14974
14975 if (tpnum <= 0)
14976 {
14977 if (instring && *instring)
14978 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14979 instring);
14980 else
3e43a32a
MS
14981 printf_filtered (_("Tracepoint argument missing "
14982 "and no previous tracepoint\n"));
1042e4c0
SS
14983 return NULL;
14984 }
14985
14986 ALL_TRACEPOINTS (t)
14987 if (t->number == tpnum)
14988 {
d9b3f62e 14989 return (struct tracepoint *) t;
1042e4c0
SS
14990 }
14991
1042e4c0
SS
14992 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14993 return NULL;
14994}
14995
d9b3f62e
PA
14996void
14997print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14998{
14999 if (b->thread != -1)
15000 fprintf_unfiltered (fp, " thread %d", b->thread);
15001
15002 if (b->task != 0)
15003 fprintf_unfiltered (fp, " task %d", b->task);
15004
15005 fprintf_unfiltered (fp, "\n");
15006}
15007
6149aea9
PA
15008/* Save information on user settable breakpoints (watchpoints, etc) to
15009 a new script file named FILENAME. If FILTER is non-NULL, call it
15010 on each breakpoint and only include the ones for which it returns
15011 non-zero. */
15012
1042e4c0 15013static void
6149aea9
PA
15014save_breakpoints (char *filename, int from_tty,
15015 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15016{
15017 struct breakpoint *tp;
6149aea9 15018 int any = 0;
a7bdde9e 15019 char *pathname;
1042e4c0 15020 struct cleanup *cleanup;
a7bdde9e 15021 struct ui_file *fp;
6149aea9 15022 int extra_trace_bits = 0;
1042e4c0 15023
6149aea9
PA
15024 if (filename == 0 || *filename == 0)
15025 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15026
15027 /* See if we have anything to save. */
6149aea9 15028 ALL_BREAKPOINTS (tp)
1042e4c0 15029 {
6149aea9 15030 /* Skip internal and momentary breakpoints. */
09d682a4 15031 if (!user_breakpoint_p (tp))
6149aea9
PA
15032 continue;
15033
15034 /* If we have a filter, only save the breakpoints it accepts. */
15035 if (filter && !filter (tp))
15036 continue;
15037
15038 any = 1;
15039
15040 if (is_tracepoint (tp))
15041 {
15042 extra_trace_bits = 1;
15043
15044 /* We can stop searching. */
15045 break;
15046 }
1042e4c0 15047 }
6149aea9
PA
15048
15049 if (!any)
1042e4c0 15050 {
6149aea9 15051 warning (_("Nothing to save."));
1042e4c0
SS
15052 return;
15053 }
15054
6149aea9 15055 pathname = tilde_expand (filename);
1042e4c0 15056 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 15057 fp = gdb_fopen (pathname, "w");
059fb39f 15058 if (!fp)
6149aea9
PA
15059 error (_("Unable to open file '%s' for saving (%s)"),
15060 filename, safe_strerror (errno));
a7bdde9e 15061 make_cleanup_ui_file_delete (fp);
8bf6485c 15062
6149aea9
PA
15063 if (extra_trace_bits)
15064 save_trace_state_variables (fp);
8bf6485c 15065
6149aea9 15066 ALL_BREAKPOINTS (tp)
1042e4c0 15067 {
6149aea9 15068 /* Skip internal and momentary breakpoints. */
09d682a4 15069 if (!user_breakpoint_p (tp))
6149aea9 15070 continue;
8bf6485c 15071
6149aea9
PA
15072 /* If we have a filter, only save the breakpoints it accepts. */
15073 if (filter && !filter (tp))
15074 continue;
15075
348d480f 15076 tp->ops->print_recreate (tp, fp);
1042e4c0 15077
6149aea9
PA
15078 /* Note, we can't rely on tp->number for anything, as we can't
15079 assume the recreated breakpoint numbers will match. Use $bpnum
15080 instead. */
15081
15082 if (tp->cond_string)
15083 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15084
15085 if (tp->ignore_count)
15086 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15087
a7bdde9e 15088 if (tp->commands)
1042e4c0 15089 {
a7bdde9e
VP
15090 volatile struct gdb_exception ex;
15091
6149aea9 15092 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15093
79a45e25 15094 ui_out_redirect (current_uiout, fp);
14dba4b4 15095 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15096 {
79a45e25 15097 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15098 }
79a45e25 15099 ui_out_redirect (current_uiout, NULL);
1042e4c0 15100
a7bdde9e
VP
15101 if (ex.reason < 0)
15102 throw_exception (ex);
1042e4c0 15103
a7bdde9e 15104 fprintf_unfiltered (fp, " end\n");
1042e4c0 15105 }
6149aea9
PA
15106
15107 if (tp->enable_state == bp_disabled)
15108 fprintf_unfiltered (fp, "disable\n");
15109
15110 /* If this is a multi-location breakpoint, check if the locations
15111 should be individually disabled. Watchpoint locations are
15112 special, and not user visible. */
15113 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15114 {
15115 struct bp_location *loc;
15116 int n = 1;
15117
15118 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15119 if (!loc->enabled)
15120 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15121 }
1042e4c0 15122 }
8bf6485c 15123
6149aea9 15124 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15125 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15126
1042e4c0
SS
15127 do_cleanups (cleanup);
15128 if (from_tty)
6149aea9
PA
15129 printf_filtered (_("Saved to file '%s'.\n"), filename);
15130}
15131
15132/* The `save breakpoints' command. */
15133
15134static void
15135save_breakpoints_command (char *args, int from_tty)
15136{
15137 save_breakpoints (args, from_tty, NULL);
15138}
15139
15140/* The `save tracepoints' command. */
15141
15142static void
15143save_tracepoints_command (char *args, int from_tty)
15144{
15145 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15146}
15147
15148/* Create a vector of all tracepoints. */
15149
15150VEC(breakpoint_p) *
eeae04df 15151all_tracepoints (void)
1042e4c0
SS
15152{
15153 VEC(breakpoint_p) *tp_vec = 0;
15154 struct breakpoint *tp;
15155
15156 ALL_TRACEPOINTS (tp)
15157 {
15158 VEC_safe_push (breakpoint_p, tp_vec, tp);
15159 }
15160
15161 return tp_vec;
15162}
15163
c906108c 15164\f
4a64f543
MS
15165/* This help string is used for the break, hbreak, tbreak and thbreak
15166 commands. It is defined as a macro to prevent duplication.
15167 COMMAND should be a string constant containing the name of the
15168 command. */
31e2b00f
AS
15169#define BREAK_ARGS_HELP(command) \
15170command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15171LOCATION may be a line number, function name, or \"*\" and an address.\n\
15172If a line number is specified, break at start of code for that line.\n\
15173If a function is specified, break at start of code for that function.\n\
15174If an address is specified, break at that exact address.\n\
dc10affe
PA
15175With no LOCATION, uses current execution address of the selected\n\
15176stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15177\n\
15178THREADNUM is the number from \"info threads\".\n\
15179CONDITION is a boolean expression.\n\
15180\n\
d41c0fc8
PA
15181Multiple breakpoints at one place are permitted, and useful if their\n\
15182conditions are different.\n\
31e2b00f
AS
15183\n\
15184Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15185
44feb3ce
TT
15186/* List of subcommands for "catch". */
15187static struct cmd_list_element *catch_cmdlist;
15188
15189/* List of subcommands for "tcatch". */
15190static struct cmd_list_element *tcatch_cmdlist;
15191
9ac4176b 15192void
44feb3ce
TT
15193add_catch_command (char *name, char *docstring,
15194 void (*sfunc) (char *args, int from_tty,
15195 struct cmd_list_element *command),
a96d9b2e
SDJ
15196 char **(*completer) (struct cmd_list_element *cmd,
15197 char *text, char *word),
44feb3ce
TT
15198 void *user_data_catch,
15199 void *user_data_tcatch)
15200{
15201 struct cmd_list_element *command;
15202
15203 command = add_cmd (name, class_breakpoint, NULL, docstring,
15204 &catch_cmdlist);
15205 set_cmd_sfunc (command, sfunc);
15206 set_cmd_context (command, user_data_catch);
a96d9b2e 15207 set_cmd_completer (command, completer);
44feb3ce
TT
15208
15209 command = add_cmd (name, class_breakpoint, NULL, docstring,
15210 &tcatch_cmdlist);
15211 set_cmd_sfunc (command, sfunc);
15212 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15213 set_cmd_completer (command, completer);
44feb3ce
TT
15214}
15215
6c95b8df 15216static void
a79b8f6e 15217clear_syscall_counts (struct inferior *inf)
6c95b8df 15218{
fa3064dd
YQ
15219 struct catch_syscall_inferior_data *inf_data
15220 = get_catch_syscall_inferior_data (inf);
15221
15222 inf_data->total_syscalls_count = 0;
15223 inf_data->any_syscall_count = 0;
15224 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15225}
15226
6149aea9
PA
15227static void
15228save_command (char *arg, int from_tty)
15229{
3e43a32a
MS
15230 printf_unfiltered (_("\"save\" must be followed by "
15231 "the name of a save subcommand.\n"));
6149aea9
PA
15232 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15233}
15234
84f4c1fe
PM
15235struct breakpoint *
15236iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15237 void *data)
15238{
35df4500 15239 struct breakpoint *b, *b_tmp;
84f4c1fe 15240
35df4500 15241 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15242 {
15243 if ((*callback) (b, data))
15244 return b;
15245 }
15246
15247 return NULL;
15248}
15249
0574c78f
GB
15250/* Zero if any of the breakpoint's locations could be a location where
15251 functions have been inlined, nonzero otherwise. */
15252
15253static int
15254is_non_inline_function (struct breakpoint *b)
15255{
15256 /* The shared library event breakpoint is set on the address of a
15257 non-inline function. */
15258 if (b->type == bp_shlib_event)
15259 return 1;
15260
15261 return 0;
15262}
15263
15264/* Nonzero if the specified PC cannot be a location where functions
15265 have been inlined. */
15266
15267int
09ac7c10
TT
15268pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15269 const struct target_waitstatus *ws)
0574c78f
GB
15270{
15271 struct breakpoint *b;
15272 struct bp_location *bl;
15273
15274 ALL_BREAKPOINTS (b)
15275 {
15276 if (!is_non_inline_function (b))
15277 continue;
15278
15279 for (bl = b->loc; bl != NULL; bl = bl->next)
15280 {
15281 if (!bl->shlib_disabled
09ac7c10 15282 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15283 return 1;
15284 }
15285 }
15286
15287 return 0;
15288}
15289
2060206e
PA
15290void
15291initialize_breakpoint_ops (void)
15292{
15293 static int initialized = 0;
15294
15295 struct breakpoint_ops *ops;
15296
15297 if (initialized)
15298 return;
15299 initialized = 1;
15300
15301 /* The breakpoint_ops structure to be inherit by all kinds of
15302 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15303 internal and momentary breakpoints, etc.). */
15304 ops = &bkpt_base_breakpoint_ops;
15305 *ops = base_breakpoint_ops;
15306 ops->re_set = bkpt_re_set;
15307 ops->insert_location = bkpt_insert_location;
15308 ops->remove_location = bkpt_remove_location;
15309 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15310 ops->create_sals_from_address = bkpt_create_sals_from_address;
15311 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15312 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15313
15314 /* The breakpoint_ops structure to be used in regular breakpoints. */
15315 ops = &bkpt_breakpoint_ops;
15316 *ops = bkpt_base_breakpoint_ops;
15317 ops->re_set = bkpt_re_set;
15318 ops->resources_needed = bkpt_resources_needed;
15319 ops->print_it = bkpt_print_it;
15320 ops->print_mention = bkpt_print_mention;
15321 ops->print_recreate = bkpt_print_recreate;
15322
15323 /* Ranged breakpoints. */
15324 ops = &ranged_breakpoint_ops;
15325 *ops = bkpt_breakpoint_ops;
15326 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15327 ops->resources_needed = resources_needed_ranged_breakpoint;
15328 ops->print_it = print_it_ranged_breakpoint;
15329 ops->print_one = print_one_ranged_breakpoint;
15330 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15331 ops->print_mention = print_mention_ranged_breakpoint;
15332 ops->print_recreate = print_recreate_ranged_breakpoint;
15333
15334 /* Internal breakpoints. */
15335 ops = &internal_breakpoint_ops;
15336 *ops = bkpt_base_breakpoint_ops;
15337 ops->re_set = internal_bkpt_re_set;
15338 ops->check_status = internal_bkpt_check_status;
15339 ops->print_it = internal_bkpt_print_it;
15340 ops->print_mention = internal_bkpt_print_mention;
15341
15342 /* Momentary breakpoints. */
15343 ops = &momentary_breakpoint_ops;
15344 *ops = bkpt_base_breakpoint_ops;
15345 ops->re_set = momentary_bkpt_re_set;
15346 ops->check_status = momentary_bkpt_check_status;
15347 ops->print_it = momentary_bkpt_print_it;
15348 ops->print_mention = momentary_bkpt_print_mention;
15349
55aa24fb
SDJ
15350 /* Probe breakpoints. */
15351 ops = &bkpt_probe_breakpoint_ops;
15352 *ops = bkpt_breakpoint_ops;
15353 ops->insert_location = bkpt_probe_insert_location;
15354 ops->remove_location = bkpt_probe_remove_location;
15355 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15356 ops->decode_linespec = bkpt_probe_decode_linespec;
15357
2060206e
PA
15358 /* GNU v3 exception catchpoints. */
15359 ops = &gnu_v3_exception_catchpoint_ops;
15360 *ops = bkpt_breakpoint_ops;
15361 ops->print_it = print_it_exception_catchpoint;
15362 ops->print_one = print_one_exception_catchpoint;
15363 ops->print_mention = print_mention_exception_catchpoint;
15364 ops->print_recreate = print_recreate_exception_catchpoint;
15365
15366 /* Watchpoints. */
15367 ops = &watchpoint_breakpoint_ops;
15368 *ops = base_breakpoint_ops;
3a5c3e22 15369 ops->dtor = dtor_watchpoint;
2060206e
PA
15370 ops->re_set = re_set_watchpoint;
15371 ops->insert_location = insert_watchpoint;
15372 ops->remove_location = remove_watchpoint;
15373 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15374 ops->check_status = check_status_watchpoint;
15375 ops->resources_needed = resources_needed_watchpoint;
15376 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15377 ops->print_it = print_it_watchpoint;
15378 ops->print_mention = print_mention_watchpoint;
15379 ops->print_recreate = print_recreate_watchpoint;
15380
15381 /* Masked watchpoints. */
15382 ops = &masked_watchpoint_breakpoint_ops;
15383 *ops = watchpoint_breakpoint_ops;
15384 ops->insert_location = insert_masked_watchpoint;
15385 ops->remove_location = remove_masked_watchpoint;
15386 ops->resources_needed = resources_needed_masked_watchpoint;
15387 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15388 ops->print_it = print_it_masked_watchpoint;
15389 ops->print_one_detail = print_one_detail_masked_watchpoint;
15390 ops->print_mention = print_mention_masked_watchpoint;
15391 ops->print_recreate = print_recreate_masked_watchpoint;
15392
15393 /* Tracepoints. */
15394 ops = &tracepoint_breakpoint_ops;
15395 *ops = base_breakpoint_ops;
15396 ops->re_set = tracepoint_re_set;
15397 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15398 ops->print_one_detail = tracepoint_print_one_detail;
15399 ops->print_mention = tracepoint_print_mention;
15400 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15401 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15402 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15403 ops->decode_linespec = tracepoint_decode_linespec;
15404
55aa24fb
SDJ
15405 /* Probe tracepoints. */
15406 ops = &tracepoint_probe_breakpoint_ops;
15407 *ops = tracepoint_breakpoint_ops;
15408 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15409 ops->decode_linespec = tracepoint_probe_decode_linespec;
15410
983af33b
SDJ
15411 /* Static tracepoints with marker (`-m'). */
15412 ops = &strace_marker_breakpoint_ops;
15413 *ops = tracepoint_breakpoint_ops;
15414 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15415 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15416 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15417
15418 /* Fork catchpoints. */
15419 ops = &catch_fork_breakpoint_ops;
15420 *ops = base_breakpoint_ops;
15421 ops->insert_location = insert_catch_fork;
15422 ops->remove_location = remove_catch_fork;
15423 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15424 ops->print_it = print_it_catch_fork;
15425 ops->print_one = print_one_catch_fork;
15426 ops->print_mention = print_mention_catch_fork;
15427 ops->print_recreate = print_recreate_catch_fork;
15428
15429 /* Vfork catchpoints. */
15430 ops = &catch_vfork_breakpoint_ops;
15431 *ops = base_breakpoint_ops;
15432 ops->insert_location = insert_catch_vfork;
15433 ops->remove_location = remove_catch_vfork;
15434 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15435 ops->print_it = print_it_catch_vfork;
15436 ops->print_one = print_one_catch_vfork;
15437 ops->print_mention = print_mention_catch_vfork;
15438 ops->print_recreate = print_recreate_catch_vfork;
15439
15440 /* Exec catchpoints. */
15441 ops = &catch_exec_breakpoint_ops;
15442 *ops = base_breakpoint_ops;
15443 ops->dtor = dtor_catch_exec;
15444 ops->insert_location = insert_catch_exec;
15445 ops->remove_location = remove_catch_exec;
15446 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15447 ops->print_it = print_it_catch_exec;
15448 ops->print_one = print_one_catch_exec;
15449 ops->print_mention = print_mention_catch_exec;
15450 ops->print_recreate = print_recreate_catch_exec;
15451
15452 /* Syscall catchpoints. */
15453 ops = &catch_syscall_breakpoint_ops;
15454 *ops = base_breakpoint_ops;
15455 ops->dtor = dtor_catch_syscall;
15456 ops->insert_location = insert_catch_syscall;
15457 ops->remove_location = remove_catch_syscall;
15458 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15459 ops->print_it = print_it_catch_syscall;
15460 ops->print_one = print_one_catch_syscall;
15461 ops->print_mention = print_mention_catch_syscall;
15462 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15463
15464 /* Solib-related catchpoints. */
15465 ops = &catch_solib_breakpoint_ops;
15466 *ops = base_breakpoint_ops;
15467 ops->dtor = dtor_catch_solib;
15468 ops->insert_location = insert_catch_solib;
15469 ops->remove_location = remove_catch_solib;
15470 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15471 ops->check_status = check_status_catch_solib;
15472 ops->print_it = print_it_catch_solib;
15473 ops->print_one = print_one_catch_solib;
15474 ops->print_mention = print_mention_catch_solib;
15475 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15476
15477 ops = &dprintf_breakpoint_ops;
15478 *ops = bkpt_base_breakpoint_ops;
15479 ops->re_set = bkpt_re_set;
15480 ops->resources_needed = bkpt_resources_needed;
15481 ops->print_it = bkpt_print_it;
15482 ops->print_mention = bkpt_print_mention;
15483 ops->print_recreate = bkpt_print_recreate;
2060206e
PA
15484}
15485
c906108c 15486void
fba45db2 15487_initialize_breakpoint (void)
c906108c
SS
15488{
15489 struct cmd_list_element *c;
15490
2060206e
PA
15491 initialize_breakpoint_ops ();
15492
84acb35a 15493 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15494 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15495 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15496
55aa24fb
SDJ
15497 breakpoint_objfile_key
15498 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15499
fa3064dd
YQ
15500 catch_syscall_inferior_data
15501 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15502
c906108c
SS
15503 breakpoint_chain = 0;
15504 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15505 before a breakpoint is set. */
15506 breakpoint_count = 0;
15507
1042e4c0
SS
15508 tracepoint_count = 0;
15509
1bedd215
AC
15510 add_com ("ignore", class_breakpoint, ignore_command, _("\
15511Set ignore-count of breakpoint number N to COUNT.\n\
15512Usage is `ignore N COUNT'."));
c906108c 15513 if (xdb_commands)
c5aa993b 15514 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15515
1bedd215
AC
15516 add_com ("commands", class_breakpoint, commands_command, _("\
15517Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15518Give breakpoint number as argument after \"commands\".\n\
15519With no argument, the targeted breakpoint is the last one set.\n\
15520The commands themselves follow starting on the next line.\n\
15521Type a line containing \"end\" to indicate the end of them.\n\
15522Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15523then no output is printed when it is hit, except what the commands print."));
c906108c 15524
1bedd215
AC
15525 add_com ("condition", class_breakpoint, condition_command, _("\
15526Specify breakpoint number N to break only if COND is true.\n\
c906108c 15527Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15528expression to be evaluated whenever breakpoint N is reached."));
c906108c 15529
1bedd215 15530 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15531Set a temporary breakpoint.\n\
c906108c
SS
15532Like \"break\" except the breakpoint is only temporary,\n\
15533so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15534by using \"enable delete\" on the breakpoint number.\n\
15535\n"
15536BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15537 set_cmd_completer (c, location_completer);
c94fdfd0 15538
1bedd215 15539 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15540Set a hardware assisted breakpoint.\n\
c906108c 15541Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15542some target hardware may not have this support.\n\
15543\n"
15544BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15545 set_cmd_completer (c, location_completer);
c906108c 15546
1bedd215 15547 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15548Set a temporary hardware assisted breakpoint.\n\
c906108c 15549Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15550so it will be deleted when hit.\n\
15551\n"
15552BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15553 set_cmd_completer (c, location_completer);
c906108c 15554
1bedd215
AC
15555 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15556Enable some breakpoints.\n\
c906108c
SS
15557Give breakpoint numbers (separated by spaces) as arguments.\n\
15558With no subcommand, breakpoints are enabled until you command otherwise.\n\
15559This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15560With a subcommand you can enable temporarily."),
c906108c
SS
15561 &enablelist, "enable ", 1, &cmdlist);
15562 if (xdb_commands)
1bedd215
AC
15563 add_com ("ab", class_breakpoint, enable_command, _("\
15564Enable some breakpoints.\n\
c906108c
SS
15565Give breakpoint numbers (separated by spaces) as arguments.\n\
15566With no subcommand, breakpoints are enabled until you command otherwise.\n\
15567This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15568With a subcommand you can enable temporarily."));
c906108c
SS
15569
15570 add_com_alias ("en", "enable", class_breakpoint, 1);
15571
84951ab5 15572 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15573Enable some breakpoints.\n\
c906108c
SS
15574Give breakpoint numbers (separated by spaces) as arguments.\n\
15575This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15576May be abbreviated to simply \"enable\".\n"),
c5aa993b 15577 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15578
1a966eab
AC
15579 add_cmd ("once", no_class, enable_once_command, _("\
15580Enable breakpoints for one hit. Give breakpoint numbers.\n\
15581If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15582 &enablebreaklist);
15583
1a966eab
AC
15584 add_cmd ("delete", no_class, enable_delete_command, _("\
15585Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15586If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15587 &enablebreaklist);
15588
816338b5
SS
15589 add_cmd ("count", no_class, enable_count_command, _("\
15590Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15591If a breakpoint is hit while enabled in this fashion,\n\
15592the count is decremented; when it reaches zero, the breakpoint is disabled."),
15593 &enablebreaklist);
15594
1a966eab
AC
15595 add_cmd ("delete", no_class, enable_delete_command, _("\
15596Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15597If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15598 &enablelist);
15599
1a966eab
AC
15600 add_cmd ("once", no_class, enable_once_command, _("\
15601Enable breakpoints for one hit. Give breakpoint numbers.\n\
15602If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15603 &enablelist);
15604
15605 add_cmd ("count", no_class, enable_count_command, _("\
15606Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15607If a breakpoint is hit while enabled in this fashion,\n\
15608the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15609 &enablelist);
15610
1bedd215
AC
15611 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15612Disable some breakpoints.\n\
c906108c
SS
15613Arguments are breakpoint numbers with spaces in between.\n\
15614To disable all breakpoints, give no argument.\n\
64b9b334 15615A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15616 &disablelist, "disable ", 1, &cmdlist);
15617 add_com_alias ("dis", "disable", class_breakpoint, 1);
15618 add_com_alias ("disa", "disable", class_breakpoint, 1);
15619 if (xdb_commands)
1bedd215
AC
15620 add_com ("sb", class_breakpoint, disable_command, _("\
15621Disable some breakpoints.\n\
c906108c
SS
15622Arguments are breakpoint numbers with spaces in between.\n\
15623To disable all breakpoints, give no argument.\n\
64b9b334 15624A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15625
1a966eab
AC
15626 add_cmd ("breakpoints", class_alias, disable_command, _("\
15627Disable some breakpoints.\n\
c906108c
SS
15628Arguments are breakpoint numbers with spaces in between.\n\
15629To disable all breakpoints, give no argument.\n\
64b9b334 15630A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15631This command may be abbreviated \"disable\"."),
c906108c
SS
15632 &disablelist);
15633
1bedd215
AC
15634 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15635Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15636Arguments are breakpoint numbers with spaces in between.\n\
15637To delete all breakpoints, give no argument.\n\
15638\n\
15639Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15640The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15641 &deletelist, "delete ", 1, &cmdlist);
15642 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15643 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15644 if (xdb_commands)
1bedd215
AC
15645 add_com ("db", class_breakpoint, delete_command, _("\
15646Delete some breakpoints.\n\
c906108c 15647Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15648To delete all breakpoints, give no argument.\n"));
c906108c 15649
1a966eab
AC
15650 add_cmd ("breakpoints", class_alias, delete_command, _("\
15651Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15652Arguments are breakpoint numbers with spaces in between.\n\
15653To delete all breakpoints, give no argument.\n\
1a966eab 15654This command may be abbreviated \"delete\"."),
c906108c
SS
15655 &deletelist);
15656
1bedd215
AC
15657 add_com ("clear", class_breakpoint, clear_command, _("\
15658Clear breakpoint at specified line or function.\n\
c906108c
SS
15659Argument may be line number, function name, or \"*\" and an address.\n\
15660If line number is specified, all breakpoints in that line are cleared.\n\
15661If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15662If an address is specified, breakpoints at that address are cleared.\n\
15663\n\
15664With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15665is executing in.\n\
15666\n\
1bedd215 15667See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15668 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15669
1bedd215 15670 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15671Set breakpoint at specified line or function.\n"
15672BREAK_ARGS_HELP ("break")));
5ba2abeb 15673 set_cmd_completer (c, location_completer);
c94fdfd0 15674
c906108c
SS
15675 add_com_alias ("b", "break", class_run, 1);
15676 add_com_alias ("br", "break", class_run, 1);
15677 add_com_alias ("bre", "break", class_run, 1);
15678 add_com_alias ("brea", "break", class_run, 1);
15679
7681d515
PM
15680 if (xdb_commands)
15681 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15682
15683 if (dbx_commands)
15684 {
1bedd215
AC
15685 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15686Break in function/address or break at a line in the current file."),
c5aa993b
JM
15687 &stoplist, "stop ", 1, &cmdlist);
15688 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15689 _("Break in function or address."), &stoplist);
c5aa993b 15690 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15691 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15692 add_com ("status", class_info, breakpoints_info, _("\
15693Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15694The \"Type\" column indicates one of:\n\
15695\tbreakpoint - normal breakpoint\n\
15696\twatchpoint - watchpoint\n\
15697The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15698the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15699breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15700address and file/line number respectively.\n\
15701\n\
15702Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15703are set to the address of the last breakpoint listed unless the command\n\
15704is prefixed with \"server \".\n\n\
c906108c 15705Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15706breakpoint set."));
c906108c
SS
15707 }
15708
1bedd215 15709 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15710Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15711The \"Type\" column indicates one of:\n\
15712\tbreakpoint - normal breakpoint\n\
15713\twatchpoint - watchpoint\n\
15714The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15715the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15716breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15717address and file/line number respectively.\n\
15718\n\
15719Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15720are set to the address of the last breakpoint listed unless the command\n\
15721is prefixed with \"server \".\n\n\
c906108c 15722Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15723breakpoint set."));
c906108c 15724
6b04bdb7
MS
15725 add_info_alias ("b", "breakpoints", 1);
15726
c906108c 15727 if (xdb_commands)
1bedd215
AC
15728 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15729Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15730The \"Type\" column indicates one of:\n\
15731\tbreakpoint - normal breakpoint\n\
15732\twatchpoint - watchpoint\n\
15733The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15734the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15735breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15736address and file/line number respectively.\n\
15737\n\
15738Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15739are set to the address of the last breakpoint listed unless the command\n\
15740is prefixed with \"server \".\n\n\
c906108c 15741Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15742breakpoint set."));
c906108c 15743
1a966eab
AC
15744 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15745Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15746The \"Type\" column indicates one of:\n\
15747\tbreakpoint - normal breakpoint\n\
15748\twatchpoint - watchpoint\n\
15749\tlongjmp - internal breakpoint used to step through longjmp()\n\
15750\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15751\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15752\tfinish - internal breakpoint used by the \"finish\" command\n\
15753The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15754the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15755breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15756address and file/line number respectively.\n\
15757\n\
15758Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15759are set to the address of the last breakpoint listed unless the command\n\
15760is prefixed with \"server \".\n\n\
c906108c 15761Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15762breakpoint set."),
c906108c
SS
15763 &maintenanceinfolist);
15764
44feb3ce
TT
15765 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15766Set catchpoints to catch events."),
15767 &catch_cmdlist, "catch ",
15768 0/*allow-unknown*/, &cmdlist);
15769
15770 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15771Set temporary catchpoints to catch events."),
15772 &tcatch_cmdlist, "tcatch ",
15773 0/*allow-unknown*/, &cmdlist);
15774
15775 /* Add catch and tcatch sub-commands. */
15776 add_catch_command ("catch", _("\
88e7d25d 15777Catch an exception, when caught."),
44feb3ce 15778 catch_catch_command,
a96d9b2e 15779 NULL,
44feb3ce
TT
15780 CATCH_PERMANENT,
15781 CATCH_TEMPORARY);
15782 add_catch_command ("throw", _("\
88e7d25d 15783Catch an exception, when thrown."),
44feb3ce 15784 catch_throw_command,
a96d9b2e 15785 NULL,
44feb3ce
TT
15786 CATCH_PERMANENT,
15787 CATCH_TEMPORARY);
15788 add_catch_command ("fork", _("Catch calls to fork."),
15789 catch_fork_command_1,
a96d9b2e 15790 NULL,
44feb3ce
TT
15791 (void *) (uintptr_t) catch_fork_permanent,
15792 (void *) (uintptr_t) catch_fork_temporary);
15793 add_catch_command ("vfork", _("Catch calls to vfork."),
15794 catch_fork_command_1,
a96d9b2e 15795 NULL,
44feb3ce
TT
15796 (void *) (uintptr_t) catch_vfork_permanent,
15797 (void *) (uintptr_t) catch_vfork_temporary);
15798 add_catch_command ("exec", _("Catch calls to exec."),
15799 catch_exec_command_1,
a96d9b2e
SDJ
15800 NULL,
15801 CATCH_PERMANENT,
15802 CATCH_TEMPORARY);
edcc5120
TT
15803 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15804Usage: catch load [REGEX]\n\
15805If REGEX is given, only stop for libraries matching the regular expression."),
15806 catch_load_command_1,
15807 NULL,
15808 CATCH_PERMANENT,
15809 CATCH_TEMPORARY);
15810 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15811Usage: catch unload [REGEX]\n\
15812If REGEX is given, only stop for libraries matching the regular expression."),
15813 catch_unload_command_1,
15814 NULL,
15815 CATCH_PERMANENT,
15816 CATCH_TEMPORARY);
a96d9b2e
SDJ
15817 add_catch_command ("syscall", _("\
15818Catch system calls by their names and/or numbers.\n\
15819Arguments say which system calls to catch. If no arguments\n\
15820are given, every system call will be caught.\n\
15821Arguments, if given, should be one or more system call names\n\
15822(if your system supports that), or system call numbers."),
15823 catch_syscall_command_1,
15824 catch_syscall_completer,
44feb3ce
TT
15825 CATCH_PERMANENT,
15826 CATCH_TEMPORARY);
c5aa993b 15827
1bedd215
AC
15828 c = add_com ("watch", class_breakpoint, watch_command, _("\
15829Set a watchpoint for an expression.\n\
06a64a0b 15830Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15831A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15832an expression changes.\n\
15833If -l or -location is given, this evaluates EXPRESSION and watches\n\
15834the memory to which it refers."));
65d12d83 15835 set_cmd_completer (c, expression_completer);
c906108c 15836
1bedd215
AC
15837 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15838Set a read watchpoint for an expression.\n\
06a64a0b 15839Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15840A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15841an expression is read.\n\
15842If -l or -location is given, this evaluates EXPRESSION and watches\n\
15843the memory to which it refers."));
65d12d83 15844 set_cmd_completer (c, expression_completer);
c906108c 15845
1bedd215
AC
15846 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15847Set a watchpoint for an expression.\n\
06a64a0b 15848Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15849A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15850an expression is either read or written.\n\
15851If -l or -location is given, this evaluates EXPRESSION and watches\n\
15852the memory to which it refers."));
65d12d83 15853 set_cmd_completer (c, expression_completer);
c906108c 15854
d77f58be 15855 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 15856Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15857
920d2a44
AC
15858 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15859 respond to changes - contrary to the description. */
85c07804
AC
15860 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15861 &can_use_hw_watchpoints, _("\
15862Set debugger's willingness to use watchpoint hardware."), _("\
15863Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15864If zero, gdb will not use hardware for new watchpoints, even if\n\
15865such is available. (However, any hardware watchpoints that were\n\
15866created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15867hardware.)"),
15868 NULL,
920d2a44 15869 show_can_use_hw_watchpoints,
85c07804 15870 &setlist, &showlist);
c906108c
SS
15871
15872 can_use_hw_watchpoints = 1;
fa8d40ab 15873
1042e4c0
SS
15874 /* Tracepoint manipulation commands. */
15875
15876 c = add_com ("trace", class_breakpoint, trace_command, _("\
15877Set a tracepoint at specified line or function.\n\
15878\n"
15879BREAK_ARGS_HELP ("trace") "\n\
15880Do \"help tracepoints\" for info on other tracepoint commands."));
15881 set_cmd_completer (c, location_completer);
15882
15883 add_com_alias ("tp", "trace", class_alias, 0);
15884 add_com_alias ("tr", "trace", class_alias, 1);
15885 add_com_alias ("tra", "trace", class_alias, 1);
15886 add_com_alias ("trac", "trace", class_alias, 1);
15887
7a697b8d
SS
15888 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15889Set a fast tracepoint at specified line or function.\n\
15890\n"
15891BREAK_ARGS_HELP ("ftrace") "\n\
15892Do \"help tracepoints\" for info on other tracepoint commands."));
15893 set_cmd_completer (c, location_completer);
15894
0fb4aa4b
PA
15895 c = add_com ("strace", class_breakpoint, strace_command, _("\
15896Set a static tracepoint at specified line, function or marker.\n\
15897\n\
15898strace [LOCATION] [if CONDITION]\n\
15899LOCATION may be a line number, function name, \"*\" and an address,\n\
15900or -m MARKER_ID.\n\
15901If a line number is specified, probe the marker at start of code\n\
15902for that line. If a function is specified, probe the marker at start\n\
15903of code for that function. If an address is specified, probe the marker\n\
15904at that exact address. If a marker id is specified, probe the marker\n\
15905with that name. With no LOCATION, uses current execution address of\n\
15906the selected stack frame.\n\
15907Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15908This collects arbitrary user data passed in the probe point call to the\n\
15909tracing library. You can inspect it when analyzing the trace buffer,\n\
15910by printing the $_sdata variable like any other convenience variable.\n\
15911\n\
15912CONDITION is a boolean expression.\n\
15913\n\
d41c0fc8
PA
15914Multiple tracepoints at one place are permitted, and useful if their\n\
15915conditions are different.\n\
0fb4aa4b
PA
15916\n\
15917Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15918Do \"help tracepoints\" for info on other tracepoint commands."));
15919 set_cmd_completer (c, location_completer);
15920
1042e4c0 15921 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 15922Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15923Convenience variable \"$tpnum\" contains the number of the\n\
15924last tracepoint set."));
15925
15926 add_info_alias ("tp", "tracepoints", 1);
15927
15928 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15929Delete specified tracepoints.\n\
15930Arguments are tracepoint numbers, separated by spaces.\n\
15931No argument means delete all tracepoints."),
15932 &deletelist);
15933
15934 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15935Disable specified tracepoints.\n\
15936Arguments are tracepoint numbers, separated by spaces.\n\
15937No argument means disable all tracepoints."),
15938 &disablelist);
15939 deprecate_cmd (c, "disable");
15940
15941 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15942Enable specified tracepoints.\n\
15943Arguments are tracepoint numbers, separated by spaces.\n\
15944No argument means enable all tracepoints."),
15945 &enablelist);
15946 deprecate_cmd (c, "enable");
15947
15948 add_com ("passcount", class_trace, trace_pass_command, _("\
15949Set the passcount for a tracepoint.\n\
15950The trace will end when the tracepoint has been passed 'count' times.\n\
15951Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15952if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15953
6149aea9
PA
15954 add_prefix_cmd ("save", class_breakpoint, save_command,
15955 _("Save breakpoint definitions as a script."),
15956 &save_cmdlist, "save ",
15957 0/*allow-unknown*/, &cmdlist);
15958
15959 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15960Save current breakpoint definitions as a script.\n\
cce7e648 15961This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15962catchpoints, tracepoints). Use the 'source' command in another debug\n\
15963session to restore them."),
15964 &save_cmdlist);
15965 set_cmd_completer (c, filename_completer);
15966
15967 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15968Save current tracepoint definitions as a script.\n\
6149aea9
PA
15969Use the 'source' command in another debug session to restore them."),
15970 &save_cmdlist);
1042e4c0
SS
15971 set_cmd_completer (c, filename_completer);
15972
6149aea9
PA
15973 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15974 deprecate_cmd (c, "save tracepoints");
15975
1bedd215 15976 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15977Breakpoint specific settings\n\
15978Configure various breakpoint-specific variables such as\n\
1bedd215 15979pending breakpoint behavior"),
fa8d40ab
JJ
15980 &breakpoint_set_cmdlist, "set breakpoint ",
15981 0/*allow-unknown*/, &setlist);
1bedd215 15982 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15983Breakpoint specific settings\n\
15984Configure various breakpoint-specific variables such as\n\
1bedd215 15985pending breakpoint behavior"),
fa8d40ab
JJ
15986 &breakpoint_show_cmdlist, "show breakpoint ",
15987 0/*allow-unknown*/, &showlist);
15988
7915a72c
AC
15989 add_setshow_auto_boolean_cmd ("pending", no_class,
15990 &pending_break_support, _("\
15991Set debugger's behavior regarding pending breakpoints."), _("\
15992Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15993If on, an unrecognized breakpoint location will cause gdb to create a\n\
15994pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15995an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15996user-query to see if a pending breakpoint should be created."),
2c5b56ce 15997 NULL,
920d2a44 15998 show_pending_break_support,
6e1d7d6c
AC
15999 &breakpoint_set_cmdlist,
16000 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16001
16002 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16003
16004 add_setshow_boolean_cmd ("auto-hw", no_class,
16005 &automatic_hardware_breakpoints, _("\
16006Set automatic usage of hardware breakpoints."), _("\
16007Show automatic usage of hardware breakpoints."), _("\
16008If set, the debugger will automatically use hardware breakpoints for\n\
16009breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16010a warning will be emitted for such breakpoints."),
16011 NULL,
16012 show_automatic_hardware_breakpoints,
16013 &breakpoint_set_cmdlist,
16014 &breakpoint_show_cmdlist);
74960c60 16015
33e5cbd6
PA
16016 add_setshow_enum_cmd ("always-inserted", class_support,
16017 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
16018Set mode for inserting breakpoints."), _("\
16019Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16020When this mode is off, breakpoints are inserted in inferior when it is\n\
16021resumed, and removed when execution stops. When this mode is on,\n\
16022breakpoints are inserted immediately and removed only when the user\n\
16023deletes the breakpoint. When this mode is auto (which is the default),\n\
16024the behaviour depends on the non-stop setting (see help set non-stop).\n\
16025In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16026behaves as if always-inserted mode is on; if gdb is controlling the\n\
16027inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
16028 NULL,
16029 &show_always_inserted_mode,
16030 &breakpoint_set_cmdlist,
16031 &breakpoint_show_cmdlist);
f1310107 16032
b775012e
LM
16033 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16034 condition_evaluation_enums,
16035 &condition_evaluation_mode_1, _("\
16036Set mode of breakpoint condition evaluation."), _("\
16037Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16038When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16039evaluated on the host's side by GDB. When it is set to \"target\",\n\
16040breakpoint conditions will be downloaded to the target (if the target\n\
16041supports such feature) and conditions will be evaluated on the target's side.\n\
16042If this is set to \"auto\" (default), this will be automatically set to\n\
16043\"target\" if it supports condition evaluation, otherwise it will\n\
16044be set to \"gdb\""),
16045 &set_condition_evaluation_mode,
16046 &show_condition_evaluation_mode,
16047 &breakpoint_set_cmdlist,
16048 &breakpoint_show_cmdlist);
16049
f1310107
TJB
16050 add_com ("break-range", class_breakpoint, break_range_command, _("\
16051Set a breakpoint for an address range.\n\
16052break-range START-LOCATION, END-LOCATION\n\
16053where START-LOCATION and END-LOCATION can be one of the following:\n\
16054 LINENUM, for that line in the current file,\n\
16055 FILE:LINENUM, for that line in that file,\n\
16056 +OFFSET, for that number of lines after the current line\n\
16057 or the start of the range\n\
16058 FUNCTION, for the first line in that function,\n\
16059 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16060 *ADDRESS, for the instruction at that address.\n\
16061\n\
16062The breakpoint will stop execution of the inferior whenever it executes\n\
16063an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16064range (including START-LOCATION and END-LOCATION)."));
16065
e7e0cddf
SS
16066 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16067Set a dynamic printf at specified line or function.\n\
16068dprintf location,format string,arg1,arg2,...\n\
16069location may be a line number, function name, or \"*\" and an address.\n\
16070If a line number is specified, break at start of code for that line.\n\
16071If a function is specified, break at start of code for that function.\n\
16072"));
16073 set_cmd_completer (c, location_completer);
16074
16075 add_setshow_enum_cmd ("dprintf-style", class_support,
16076 dprintf_style_enums, &dprintf_style, _("\
16077Set the style of usage for dynamic printf."), _("\
16078Show the style of usage for dynamic printf."), _("\
16079This setting chooses how GDB will do a dynamic printf.\n\
16080If the value is \"gdb\", then the printing is done by GDB to its own\n\
16081console, as with the \"printf\" command.\n\
16082If the value is \"call\", the print is done by calling a function in your\n\
16083program; by default printf(), but you can choose a different function or\n\
16084output stream by setting dprintf-function and dprintf-channel."),
16085 update_dprintf_commands, NULL,
16086 &setlist, &showlist);
16087
16088 dprintf_function = xstrdup ("printf");
16089 add_setshow_string_cmd ("dprintf-function", class_support,
16090 &dprintf_function, _("\
16091Set the function to use for dynamic printf"), _("\
16092Show the function to use for dynamic printf"), NULL,
16093 update_dprintf_commands, NULL,
16094 &setlist, &showlist);
16095
16096 dprintf_channel = xstrdup ("");
16097 add_setshow_string_cmd ("dprintf-channel", class_support,
16098 &dprintf_channel, _("\
16099Set the channel to use for dynamic printf"), _("\
16100Show the channel to use for dynamic printf"), NULL,
16101 update_dprintf_commands, NULL,
16102 &setlist, &showlist);
16103
765dc015 16104 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16105
16106 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16107}
This page took 2.287495 seconds and 4 git commands to generate.