]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Add markers for release 2.26
[thirdparty/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5 69#include "format.h"
f00aae0f 70#include "location.h"
cfc31633 71#include "thread-fsm.h"
d3ce09f5 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
104c1213 82
e7e8980f
YQ
83/* Enums for exception-handling support. */
84enum exception_event_kind
85{
86 EX_EVENT_THROW,
591f19e8 87 EX_EVENT_RETHROW,
e7e8980f
YQ
88 EX_EVENT_CATCH
89};
90
4a64f543 91/* Prototypes for local functions. */
c906108c 92
a14ed312 93static void enable_delete_command (char *, int);
c906108c 94
a14ed312 95static void enable_once_command (char *, int);
c906108c 96
816338b5
SS
97static void enable_count_command (char *, int);
98
a14ed312 99static void disable_command (char *, int);
c906108c 100
a14ed312 101static void enable_command (char *, int);
c906108c 102
95a42b64
TT
103static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
c906108c 106
a14ed312 107static void ignore_command (char *, int);
c906108c 108
4efb68b1 109static int breakpoint_re_set_one (void *);
c906108c 110
348d480f
PA
111static void breakpoint_re_set_default (struct breakpoint *);
112
f00aae0f
KS
113static void
114 create_sals_from_location_default (const struct event_location *location,
115 struct linespec_result *canonical,
116 enum bptype type_wanted);
983af33b
SDJ
117
118static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
e7e0cddf 120 char *, char *, enum bptype,
983af33b
SDJ
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
44f238bb 124 int, int, int, unsigned);
983af33b 125
f00aae0f
KS
126static void decode_location_default (struct breakpoint *b,
127 const struct event_location *location,
128 struct symtabs_and_lines *sals);
983af33b 129
a14ed312 130static void clear_command (char *, int);
c906108c 131
a14ed312 132static void catch_command (char *, int);
c906108c 133
a9634178 134static int can_use_hardware_watchpoint (struct value *);
c906108c 135
98deb0da 136static void break_command_1 (char *, int, int);
c906108c 137
a14ed312 138static void mention (struct breakpoint *);
c906108c 139
348d480f
PA
140static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141 enum bptype,
c0a91b2b 142 const struct breakpoint_ops *);
3742cc8b
YQ
143static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144 const struct symtab_and_line *);
145
4a64f543
MS
146/* This function is used in gdbtk sources and thus can not be made
147 static. */
63c252f8 148struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 149 struct symtab_and_line,
c0a91b2b
TT
150 enum bptype,
151 const struct breakpoint_ops *);
c906108c 152
06edf0c0
PA
153static struct breakpoint *
154 momentary_breakpoint_from_master (struct breakpoint *orig,
155 enum bptype type,
a1aa2221
LM
156 const struct breakpoint_ops *ops,
157 int loc_enabled);
06edf0c0 158
76897487
KB
159static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
a6d9a66e
UW
161static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162 CORE_ADDR bpaddr,
88f7da05 163 enum bptype bptype);
76897487 164
6c95b8df
PA
165static void describe_other_breakpoints (struct gdbarch *,
166 struct program_space *, CORE_ADDR,
5af949e3 167 struct obj_section *, int);
c906108c 168
85d721b8
PA
169static int watchpoint_locations_match (struct bp_location *loc1,
170 struct bp_location *loc2);
171
f1310107
TJB
172static int breakpoint_location_address_match (struct bp_location *bl,
173 struct address_space *aspace,
174 CORE_ADDR addr);
175
d35ae833
PA
176static int breakpoint_location_address_range_overlap (struct bp_location *,
177 struct address_space *,
178 CORE_ADDR, int);
179
a14ed312 180static void breakpoints_info (char *, int);
c906108c 181
d77f58be
SS
182static void watchpoints_info (char *, int);
183
e5a67952
MS
184static int breakpoint_1 (char *, int,
185 int (*) (const struct breakpoint *));
c906108c 186
4efb68b1 187static int breakpoint_cond_eval (void *);
c906108c 188
4efb68b1 189static void cleanup_executing_breakpoints (void *);
c906108c 190
a14ed312 191static void commands_command (char *, int);
c906108c 192
a14ed312 193static void condition_command (char *, int);
c906108c 194
c5aa993b
JM
195typedef enum
196 {
197 mark_inserted,
198 mark_uninserted
199 }
200insertion_state_t;
c906108c 201
0bde7532 202static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 203static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 204
e514a9d6 205static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 206
4efb68b1 207static int watchpoint_check (void *);
c906108c 208
a14ed312 209static void maintenance_info_breakpoints (char *, int);
c906108c 210
a14ed312 211static int hw_breakpoint_used_count (void);
c906108c 212
a1398e0c
PA
213static int hw_watchpoint_use_count (struct breakpoint *);
214
215static int hw_watchpoint_used_count_others (struct breakpoint *except,
216 enum bptype type,
217 int *other_type_used);
c906108c 218
a14ed312 219static void hbreak_command (char *, int);
c906108c 220
a14ed312 221static void thbreak_command (char *, int);
c906108c 222
816338b5
SS
223static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
224 int count);
c906108c 225
a14ed312 226static void stop_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void stopin_command (char *arg, int from_tty);
7a292a7a 229
a14ed312 230static void stopat_command (char *arg, int from_tty);
7a292a7a 231
a14ed312 232static void tcatch_command (char *arg, int from_tty);
7a292a7a 233
fe3f5fa8 234static void free_bp_location (struct bp_location *loc);
f431efe5
PA
235static void incref_bp_location (struct bp_location *loc);
236static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 237
39d61571 238static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 239
44702360
PA
240/* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242enum ugll_insert_mode
243{
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
a25a5a45
PA
261 /* May insert breakpoints iff breakpoints_should_be_inserted_now
262 claims breakpoints should be inserted now. */
04086b45
PA
263 UGLL_MAY_INSERT,
264
a25a5a45
PA
265 /* Insert locations now, irrespective of
266 breakpoints_should_be_inserted_now. E.g., say all threads are
267 stopped right now, and the user did "continue". We need to
268 insert breakpoints _before_ resuming the target, but
269 UGLL_MAY_INSERT wouldn't insert them, because
270 breakpoints_should_be_inserted_now returns false at that point,
271 as no thread is running yet. */
04086b45 272 UGLL_INSERT
44702360
PA
273};
274
275static void update_global_location_list (enum ugll_insert_mode);
a5606eee 276
44702360 277static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 278
d77f58be 279static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
280
281static void insert_breakpoint_locations (void);
a5606eee 282
1042e4c0
SS
283static void tracepoints_info (char *, int);
284
285static void delete_trace_command (char *, int);
286
287static void enable_trace_command (char *, int);
288
289static void disable_trace_command (char *, int);
290
291static void trace_pass_command (char *, int);
292
558a9d82
YQ
293static void set_tracepoint_count (int num);
294
9c06b0b4
TJB
295static int is_masked_watchpoint (const struct breakpoint *b);
296
b775012e
LM
297static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
298
983af33b
SDJ
299/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
300 otherwise. */
301
302static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 303
2060206e
PA
304/* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309/* Internal breakpoints class type. */
06edf0c0 310static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
311
312/* Momentary breakpoints class type. */
06edf0c0
PA
313static struct breakpoint_ops momentary_breakpoint_ops;
314
e2e4d78b
JK
315/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316static struct breakpoint_ops longjmp_breakpoint_ops;
317
2060206e
PA
318/* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320struct breakpoint_ops bkpt_breakpoint_ops;
321
55aa24fb
SDJ
322/* Breakpoints set on probes. */
323static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
e7e0cddf 325/* Dynamic printf class type. */
c5867ab6 326struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 327
d3ce09f5
SS
328/* The style in which to perform a dynamic printf. This is a user
329 option because different output options have different tradeoffs;
330 if GDB does the printing, there is better error handling if there
331 is a problem with any of the arguments, but using an inferior
332 function lets you have special-purpose printers and sending of
333 output to the same place as compiled-in print functions. */
334
335static const char dprintf_style_gdb[] = "gdb";
336static const char dprintf_style_call[] = "call";
337static const char dprintf_style_agent[] = "agent";
338static const char *const dprintf_style_enums[] = {
339 dprintf_style_gdb,
340 dprintf_style_call,
341 dprintf_style_agent,
342 NULL
343};
344static const char *dprintf_style = dprintf_style_gdb;
345
346/* The function to use for dynamic printf if the preferred style is to
347 call into the inferior. The value is simply a string that is
348 copied into the command, so it can be anything that GDB can
349 evaluate to a callable address, not necessarily a function name. */
350
351static char *dprintf_function = "";
352
353/* The channel to use for dynamic printf if the preferred style is to
354 call into the inferior; if a nonempty string, it will be passed to
355 the call as the first argument, with the format string as the
356 second. As with the dprintf function, this can be anything that
357 GDB knows how to evaluate, so in addition to common choices like
358 "stderr", this could be an app-specific expression like
359 "mystreams[curlogger]". */
360
361static char *dprintf_channel = "";
362
363/* True if dprintf commands should continue to operate even if GDB
364 has disconnected. */
365static int disconnected_dprintf = 1;
366
5cea2a26
PA
367/* A reference-counted struct command_line. This lets multiple
368 breakpoints share a single command list. */
369struct counted_command_line
370{
371 /* The reference count. */
372 int refc;
373
374 /* The command list. */
375 struct command_line *commands;
376};
377
378struct command_line *
379breakpoint_commands (struct breakpoint *b)
380{
381 return b->commands ? b->commands->commands : NULL;
382}
3daf8fe5 383
f3b1572e
PA
384/* Flag indicating that a command has proceeded the inferior past the
385 current breakpoint. */
386
387static int breakpoint_proceeded;
388
956a9fb9 389const char *
2cec12e5
AR
390bpdisp_text (enum bpdisp disp)
391{
4a64f543
MS
392 /* NOTE: the following values are a part of MI protocol and
393 represent values of 'disp' field returned when inferior stops at
394 a breakpoint. */
bc043ef3 395 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 396
2cec12e5
AR
397 return bpdisps[(int) disp];
398}
c906108c 399
4a64f543 400/* Prototypes for exported functions. */
c906108c 401/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 402 if such is available. */
c906108c
SS
403static int can_use_hw_watchpoints;
404
920d2a44
AC
405static void
406show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c,
408 const char *value)
409{
3e43a32a
MS
410 fprintf_filtered (file,
411 _("Debugger's willingness to use "
412 "watchpoint hardware is %s.\n"),
920d2a44
AC
413 value);
414}
415
fa8d40ab
JJ
416/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
417 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 418 for unrecognized breakpoint locations.
fa8d40ab
JJ
419 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
420static enum auto_boolean pending_break_support;
920d2a44
AC
421static void
422show_pending_break_support (struct ui_file *file, int from_tty,
423 struct cmd_list_element *c,
424 const char *value)
425{
3e43a32a
MS
426 fprintf_filtered (file,
427 _("Debugger's behavior regarding "
428 "pending breakpoints is %s.\n"),
920d2a44
AC
429 value);
430}
fa8d40ab 431
765dc015 432/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 433 set with "break" but falling in read-only memory.
765dc015
VP
434 If 0, gdb will warn about such breakpoints, but won't automatically
435 use hardware breakpoints. */
436static int automatic_hardware_breakpoints;
437static void
438show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
439 struct cmd_list_element *c,
440 const char *value)
441{
3e43a32a
MS
442 fprintf_filtered (file,
443 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
444 value);
445}
446
a25a5a45
PA
447/* If on, GDB keeps breakpoints inserted even if the inferior is
448 stopped, and immediately inserts any new breakpoints as soon as
449 they're created. If off (default), GDB keeps breakpoints off of
450 the target as long as possible. That is, it delays inserting
451 breakpoints until the next resume, and removes them again when the
452 target fully stops. This is a bit safer in case GDB crashes while
453 processing user input. */
454static int always_inserted_mode = 0;
72d0e2c5 455
33e5cbd6 456static void
74960c60 457show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 458 struct cmd_list_element *c, const char *value)
74960c60 459{
a25a5a45
PA
460 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
461 value);
74960c60
VP
462}
463
b57bacec
PA
464/* See breakpoint.h. */
465
33e5cbd6 466int
a25a5a45 467breakpoints_should_be_inserted_now (void)
33e5cbd6 468{
a25a5a45
PA
469 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
470 {
471 /* If breakpoints are global, they should be inserted even if no
472 thread under gdb's control is running, or even if there are
473 no threads under GDB's control yet. */
474 return 1;
475 }
476 else if (target_has_execution)
477 {
372316f1
PA
478 struct thread_info *tp;
479
a25a5a45
PA
480 if (always_inserted_mode)
481 {
482 /* The user wants breakpoints inserted even if all threads
483 are stopped. */
484 return 1;
485 }
486
b57bacec
PA
487 if (threads_are_executing ())
488 return 1;
372316f1
PA
489
490 /* Don't remove breakpoints yet if, even though all threads are
491 stopped, we still have events to process. */
492 ALL_NON_EXITED_THREADS (tp)
493 if (tp->resumed
494 && tp->suspend.waitstatus_pending_p)
495 return 1;
a25a5a45
PA
496 }
497 return 0;
33e5cbd6 498}
765dc015 499
b775012e
LM
500static const char condition_evaluation_both[] = "host or target";
501
502/* Modes for breakpoint condition evaluation. */
503static const char condition_evaluation_auto[] = "auto";
504static const char condition_evaluation_host[] = "host";
505static const char condition_evaluation_target[] = "target";
506static const char *const condition_evaluation_enums[] = {
507 condition_evaluation_auto,
508 condition_evaluation_host,
509 condition_evaluation_target,
510 NULL
511};
512
513/* Global that holds the current mode for breakpoint condition evaluation. */
514static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
515
516/* Global that we use to display information to the user (gets its value from
517 condition_evaluation_mode_1. */
518static const char *condition_evaluation_mode = condition_evaluation_auto;
519
520/* Translate a condition evaluation mode MODE into either "host"
521 or "target". This is used mostly to translate from "auto" to the
522 real setting that is being used. It returns the translated
523 evaluation mode. */
524
525static const char *
526translate_condition_evaluation_mode (const char *mode)
527{
528 if (mode == condition_evaluation_auto)
529 {
530 if (target_supports_evaluation_of_breakpoint_conditions ())
531 return condition_evaluation_target;
532 else
533 return condition_evaluation_host;
534 }
535 else
536 return mode;
537}
538
539/* Discovers what condition_evaluation_auto translates to. */
540
541static const char *
542breakpoint_condition_evaluation_mode (void)
543{
544 return translate_condition_evaluation_mode (condition_evaluation_mode);
545}
546
547/* Return true if GDB should evaluate breakpoint conditions or false
548 otherwise. */
549
550static int
551gdb_evaluates_breakpoint_condition_p (void)
552{
553 const char *mode = breakpoint_condition_evaluation_mode ();
554
555 return (mode == condition_evaluation_host);
556}
557
a14ed312 558void _initialize_breakpoint (void);
c906108c 559
c906108c
SS
560/* Are we executing breakpoint commands? */
561static int executing_breakpoint_commands;
562
c02f5703
MS
563/* Are overlay event breakpoints enabled? */
564static int overlay_events_enabled;
565
e09342b5
TJB
566/* See description in breakpoint.h. */
567int target_exact_watchpoints = 0;
568
c906108c 569/* Walk the following statement or block through all breakpoints.
e5dd4106 570 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 571 current breakpoint. */
c906108c 572
5c44784c 573#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 574
5c44784c
JM
575#define ALL_BREAKPOINTS_SAFE(B,TMP) \
576 for (B = breakpoint_chain; \
577 B ? (TMP=B->next, 1): 0; \
578 B = TMP)
c906108c 579
4a64f543
MS
580/* Similar iterator for the low-level breakpoints. SAFE variant is
581 not provided so update_global_location_list must not be called
582 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 583
876fa593
JK
584#define ALL_BP_LOCATIONS(B,BP_TMP) \
585 for (BP_TMP = bp_location; \
586 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
587 BP_TMP++)
7cc221ef 588
b775012e
LM
589/* Iterates through locations with address ADDRESS for the currently selected
590 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
591 to where the loop should start from.
592 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
593 appropriate location to start with. */
594
595#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
596 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
597 BP_LOCP_TMP = BP_LOCP_START; \
598 BP_LOCP_START \
599 && (BP_LOCP_TMP < bp_location + bp_location_count \
600 && (*BP_LOCP_TMP)->address == ADDRESS); \
601 BP_LOCP_TMP++)
602
1042e4c0
SS
603/* Iterator for tracepoints only. */
604
605#define ALL_TRACEPOINTS(B) \
606 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 607 if (is_tracepoint (B))
1042e4c0 608
7cc221ef 609/* Chains of all breakpoints defined. */
c906108c
SS
610
611struct breakpoint *breakpoint_chain;
612
876fa593
JK
613/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
614
615static struct bp_location **bp_location;
616
617/* Number of elements of BP_LOCATION. */
618
619static unsigned bp_location_count;
620
4a64f543
MS
621/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
622 ADDRESS for the current elements of BP_LOCATION which get a valid
623 result from bp_location_has_shadow. You can use it for roughly
624 limiting the subrange of BP_LOCATION to scan for shadow bytes for
625 an address you need to read. */
876fa593
JK
626
627static CORE_ADDR bp_location_placed_address_before_address_max;
628
4a64f543
MS
629/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
630 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
631 BP_LOCATION which get a valid result from bp_location_has_shadow.
632 You can use it for roughly limiting the subrange of BP_LOCATION to
633 scan for shadow bytes for an address you need to read. */
876fa593
JK
634
635static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 636
4a64f543
MS
637/* The locations that no longer correspond to any breakpoint, unlinked
638 from bp_location array, but for which a hit may still be reported
639 by a target. */
20874c92
VP
640VEC(bp_location_p) *moribund_locations = NULL;
641
c906108c
SS
642/* Number of last breakpoint made. */
643
95a42b64
TT
644static int breakpoint_count;
645
86b17b60
PA
646/* The value of `breakpoint_count' before the last command that
647 created breakpoints. If the last (break-like) command created more
648 than one breakpoint, then the difference between BREAKPOINT_COUNT
649 and PREV_BREAKPOINT_COUNT is more than one. */
650static int prev_breakpoint_count;
c906108c 651
1042e4c0
SS
652/* Number of last tracepoint made. */
653
95a42b64 654static int tracepoint_count;
1042e4c0 655
6149aea9
PA
656static struct cmd_list_element *breakpoint_set_cmdlist;
657static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 658struct cmd_list_element *save_cmdlist;
6149aea9 659
badd37ce
SDJ
660/* See declaration at breakpoint.h. */
661
662struct breakpoint *
663breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
664 void *user_data)
665{
666 struct breakpoint *b = NULL;
667
668 ALL_BREAKPOINTS (b)
669 {
670 if (func (b, user_data) != 0)
671 break;
672 }
673
674 return b;
675}
676
468d015d
JJ
677/* Return whether a breakpoint is an active enabled breakpoint. */
678static int
679breakpoint_enabled (struct breakpoint *b)
680{
0d381245 681 return (b->enable_state == bp_enabled);
468d015d
JJ
682}
683
c906108c
SS
684/* Set breakpoint count to NUM. */
685
95a42b64 686static void
fba45db2 687set_breakpoint_count (int num)
c906108c 688{
86b17b60 689 prev_breakpoint_count = breakpoint_count;
c906108c 690 breakpoint_count = num;
4fa62494 691 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
692}
693
86b17b60
PA
694/* Used by `start_rbreak_breakpoints' below, to record the current
695 breakpoint count before "rbreak" creates any breakpoint. */
696static int rbreak_start_breakpoint_count;
697
95a42b64
TT
698/* Called at the start an "rbreak" command to record the first
699 breakpoint made. */
86b17b60 700
95a42b64
TT
701void
702start_rbreak_breakpoints (void)
703{
86b17b60 704 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
705}
706
707/* Called at the end of an "rbreak" command to record the last
708 breakpoint made. */
86b17b60 709
95a42b64
TT
710void
711end_rbreak_breakpoints (void)
712{
86b17b60 713 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
714}
715
4a64f543 716/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
717
718void
fba45db2 719clear_breakpoint_hit_counts (void)
c906108c
SS
720{
721 struct breakpoint *b;
722
723 ALL_BREAKPOINTS (b)
724 b->hit_count = 0;
725}
726
9add0f1b
TT
727/* Allocate a new counted_command_line with reference count of 1.
728 The new structure owns COMMANDS. */
729
730static struct counted_command_line *
731alloc_counted_command_line (struct command_line *commands)
732{
8d749320 733 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 734
9add0f1b
TT
735 result->refc = 1;
736 result->commands = commands;
8d749320 737
9add0f1b
TT
738 return result;
739}
740
741/* Increment reference count. This does nothing if CMD is NULL. */
742
743static void
744incref_counted_command_line (struct counted_command_line *cmd)
745{
746 if (cmd)
747 ++cmd->refc;
748}
749
750/* Decrement reference count. If the reference count reaches 0,
751 destroy the counted_command_line. Sets *CMDP to NULL. This does
752 nothing if *CMDP is NULL. */
753
754static void
755decref_counted_command_line (struct counted_command_line **cmdp)
756{
757 if (*cmdp)
758 {
759 if (--(*cmdp)->refc == 0)
760 {
761 free_command_lines (&(*cmdp)->commands);
762 xfree (*cmdp);
763 }
764 *cmdp = NULL;
765 }
766}
767
768/* A cleanup function that calls decref_counted_command_line. */
769
770static void
771do_cleanup_counted_command_line (void *arg)
772{
9a3c8263 773 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
774}
775
776/* Create a cleanup that calls decref_counted_command_line on the
777 argument. */
778
779static struct cleanup *
780make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
781{
782 return make_cleanup (do_cleanup_counted_command_line, cmdp);
783}
784
c906108c 785\f
48cb2d85
VP
786/* Return the breakpoint with the specified number, or NULL
787 if the number does not refer to an existing breakpoint. */
788
789struct breakpoint *
790get_breakpoint (int num)
791{
792 struct breakpoint *b;
793
794 ALL_BREAKPOINTS (b)
795 if (b->number == num)
796 return b;
797
798 return NULL;
799}
5c44784c 800
c906108c 801\f
adc36818 802
b775012e
LM
803/* Mark locations as "conditions have changed" in case the target supports
804 evaluating conditions on its side. */
805
806static void
807mark_breakpoint_modified (struct breakpoint *b)
808{
809 struct bp_location *loc;
810
811 /* This is only meaningful if the target is
812 evaluating conditions and if the user has
813 opted for condition evaluation on the target's
814 side. */
815 if (gdb_evaluates_breakpoint_condition_p ()
816 || !target_supports_evaluation_of_breakpoint_conditions ())
817 return;
818
819 if (!is_breakpoint (b))
820 return;
821
822 for (loc = b->loc; loc; loc = loc->next)
823 loc->condition_changed = condition_modified;
824}
825
826/* Mark location as "conditions have changed" in case the target supports
827 evaluating conditions on its side. */
828
829static void
830mark_breakpoint_location_modified (struct bp_location *loc)
831{
832 /* This is only meaningful if the target is
833 evaluating conditions and if the user has
834 opted for condition evaluation on the target's
835 side. */
836 if (gdb_evaluates_breakpoint_condition_p ()
837 || !target_supports_evaluation_of_breakpoint_conditions ())
838
839 return;
840
841 if (!is_breakpoint (loc->owner))
842 return;
843
844 loc->condition_changed = condition_modified;
845}
846
847/* Sets the condition-evaluation mode using the static global
848 condition_evaluation_mode. */
849
850static void
851set_condition_evaluation_mode (char *args, int from_tty,
852 struct cmd_list_element *c)
853{
b775012e
LM
854 const char *old_mode, *new_mode;
855
856 if ((condition_evaluation_mode_1 == condition_evaluation_target)
857 && !target_supports_evaluation_of_breakpoint_conditions ())
858 {
859 condition_evaluation_mode_1 = condition_evaluation_mode;
860 warning (_("Target does not support breakpoint condition evaluation.\n"
861 "Using host evaluation mode instead."));
862 return;
863 }
864
865 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
866 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
867
abf1152a
JK
868 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
869 settings was "auto". */
870 condition_evaluation_mode = condition_evaluation_mode_1;
871
b775012e
LM
872 /* Only update the mode if the user picked a different one. */
873 if (new_mode != old_mode)
874 {
875 struct bp_location *loc, **loc_tmp;
876 /* If the user switched to a different evaluation mode, we
877 need to synch the changes with the target as follows:
878
879 "host" -> "target": Send all (valid) conditions to the target.
880 "target" -> "host": Remove all the conditions from the target.
881 */
882
b775012e
LM
883 if (new_mode == condition_evaluation_target)
884 {
885 /* Mark everything modified and synch conditions with the
886 target. */
887 ALL_BP_LOCATIONS (loc, loc_tmp)
888 mark_breakpoint_location_modified (loc);
889 }
890 else
891 {
892 /* Manually mark non-duplicate locations to synch conditions
893 with the target. We do this to remove all the conditions the
894 target knows about. */
895 ALL_BP_LOCATIONS (loc, loc_tmp)
896 if (is_breakpoint (loc->owner) && loc->inserted)
897 loc->needs_update = 1;
898 }
899
900 /* Do the update. */
44702360 901 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
902 }
903
904 return;
905}
906
907/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
908 what "auto" is translating to. */
909
910static void
911show_condition_evaluation_mode (struct ui_file *file, int from_tty,
912 struct cmd_list_element *c, const char *value)
913{
914 if (condition_evaluation_mode == condition_evaluation_auto)
915 fprintf_filtered (file,
916 _("Breakpoint condition evaluation "
917 "mode is %s (currently %s).\n"),
918 value,
919 breakpoint_condition_evaluation_mode ());
920 else
921 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
922 value);
923}
924
925/* A comparison function for bp_location AP and BP that is used by
926 bsearch. This comparison function only cares about addresses, unlike
927 the more general bp_location_compare function. */
928
929static int
930bp_location_compare_addrs (const void *ap, const void *bp)
931{
9a3c8263
SM
932 const struct bp_location *a = *(const struct bp_location **) ap;
933 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
934
935 if (a->address == b->address)
936 return 0;
937 else
938 return ((a->address > b->address) - (a->address < b->address));
939}
940
941/* Helper function to skip all bp_locations with addresses
942 less than ADDRESS. It returns the first bp_location that
943 is greater than or equal to ADDRESS. If none is found, just
944 return NULL. */
945
946static struct bp_location **
947get_first_locp_gte_addr (CORE_ADDR address)
948{
949 struct bp_location dummy_loc;
950 struct bp_location *dummy_locp = &dummy_loc;
951 struct bp_location **locp_found = NULL;
952
953 /* Initialize the dummy location's address field. */
954 memset (&dummy_loc, 0, sizeof (struct bp_location));
955 dummy_loc.address = address;
956
957 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
958 locp_found = ((struct bp_location **)
959 bsearch (&dummy_locp, bp_location, bp_location_count,
960 sizeof (struct bp_location **),
961 bp_location_compare_addrs));
b775012e
LM
962
963 /* Nothing was found, nothing left to do. */
964 if (locp_found == NULL)
965 return NULL;
966
967 /* We may have found a location that is at ADDRESS but is not the first in the
968 location's list. Go backwards (if possible) and locate the first one. */
969 while ((locp_found - 1) >= bp_location
970 && (*(locp_found - 1))->address == address)
971 locp_found--;
972
973 return locp_found;
974}
975
adc36818 976void
7a26bd4d 977set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
978 int from_tty)
979{
3a5c3e22
PA
980 xfree (b->cond_string);
981 b->cond_string = NULL;
adc36818 982
3a5c3e22 983 if (is_watchpoint (b))
adc36818 984 {
3a5c3e22
PA
985 struct watchpoint *w = (struct watchpoint *) b;
986
987 xfree (w->cond_exp);
988 w->cond_exp = NULL;
989 }
990 else
991 {
992 struct bp_location *loc;
993
994 for (loc = b->loc; loc; loc = loc->next)
995 {
996 xfree (loc->cond);
997 loc->cond = NULL;
b775012e
LM
998
999 /* No need to free the condition agent expression
1000 bytecode (if we have one). We will handle this
1001 when we go through update_global_location_list. */
3a5c3e22 1002 }
adc36818 1003 }
adc36818
PM
1004
1005 if (*exp == 0)
1006 {
1007 if (from_tty)
1008 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1009 }
1010 else
1011 {
bbc13ae3 1012 const char *arg = exp;
cc59ec59 1013
adc36818
PM
1014 /* I don't know if it matters whether this is the string the user
1015 typed in or the decompiled expression. */
1016 b->cond_string = xstrdup (arg);
1017 b->condition_not_parsed = 0;
1018
1019 if (is_watchpoint (b))
1020 {
3a5c3e22
PA
1021 struct watchpoint *w = (struct watchpoint *) b;
1022
adc36818
PM
1023 innermost_block = NULL;
1024 arg = exp;
1bb9788d 1025 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1026 if (*arg)
1027 error (_("Junk at end of expression"));
3a5c3e22 1028 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1029 }
1030 else
1031 {
3a5c3e22
PA
1032 struct bp_location *loc;
1033
adc36818
PM
1034 for (loc = b->loc; loc; loc = loc->next)
1035 {
1036 arg = exp;
1037 loc->cond =
1bb9788d
TT
1038 parse_exp_1 (&arg, loc->address,
1039 block_for_pc (loc->address), 0);
adc36818
PM
1040 if (*arg)
1041 error (_("Junk at end of expression"));
1042 }
1043 }
1044 }
b775012e
LM
1045 mark_breakpoint_modified (b);
1046
8d3788bd 1047 observer_notify_breakpoint_modified (b);
adc36818
PM
1048}
1049
d55637df
TT
1050/* Completion for the "condition" command. */
1051
1052static VEC (char_ptr) *
6f937416
PA
1053condition_completer (struct cmd_list_element *cmd,
1054 const char *text, const char *word)
d55637df 1055{
6f937416 1056 const char *space;
d55637df 1057
6f937416
PA
1058 text = skip_spaces_const (text);
1059 space = skip_to_space_const (text);
d55637df
TT
1060 if (*space == '\0')
1061 {
1062 int len;
1063 struct breakpoint *b;
1064 VEC (char_ptr) *result = NULL;
1065
1066 if (text[0] == '$')
1067 {
1068 /* We don't support completion of history indices. */
1069 if (isdigit (text[1]))
1070 return NULL;
1071 return complete_internalvar (&text[1]);
1072 }
1073
1074 /* We're completing the breakpoint number. */
1075 len = strlen (text);
1076
1077 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1078 {
1079 char number[50];
1080
1081 xsnprintf (number, sizeof (number), "%d", b->number);
1082
1083 if (strncmp (number, text, len) == 0)
1084 VEC_safe_push (char_ptr, result, xstrdup (number));
1085 }
d55637df
TT
1086
1087 return result;
1088 }
1089
1090 /* We're completing the expression part. */
6f937416 1091 text = skip_spaces_const (space);
d55637df
TT
1092 return expression_completer (cmd, text, word);
1093}
1094
c906108c
SS
1095/* condition N EXP -- set break condition of breakpoint N to EXP. */
1096
1097static void
fba45db2 1098condition_command (char *arg, int from_tty)
c906108c 1099{
52f0bd74 1100 struct breakpoint *b;
c906108c 1101 char *p;
52f0bd74 1102 int bnum;
c906108c
SS
1103
1104 if (arg == 0)
e2e0b3e5 1105 error_no_arg (_("breakpoint number"));
c906108c
SS
1106
1107 p = arg;
1108 bnum = get_number (&p);
5c44784c 1109 if (bnum == 0)
8a3fe4f8 1110 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1111
1112 ALL_BREAKPOINTS (b)
1113 if (b->number == bnum)
2f069f6f 1114 {
6dddc817
DE
1115 /* Check if this breakpoint has a "stop" method implemented in an
1116 extension language. This method and conditions entered into GDB
1117 from the CLI are mutually exclusive. */
1118 const struct extension_language_defn *extlang
1119 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1120
1121 if (extlang != NULL)
1122 {
1123 error (_("Only one stop condition allowed. There is currently"
1124 " a %s stop condition defined for this breakpoint."),
1125 ext_lang_capitalized_name (extlang));
1126 }
2566ad2d 1127 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1128
1129 if (is_breakpoint (b))
44702360 1130 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1131
2f069f6f
JB
1132 return;
1133 }
c906108c 1134
8a3fe4f8 1135 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1136}
1137
a7bdde9e
VP
1138/* Check that COMMAND do not contain commands that are suitable
1139 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1140 Throw if any such commands is found. */
1141
a7bdde9e
VP
1142static void
1143check_no_tracepoint_commands (struct command_line *commands)
1144{
1145 struct command_line *c;
cc59ec59 1146
a7bdde9e
VP
1147 for (c = commands; c; c = c->next)
1148 {
1149 int i;
1150
1151 if (c->control_type == while_stepping_control)
3e43a32a
MS
1152 error (_("The 'while-stepping' command can "
1153 "only be used for tracepoints"));
a7bdde9e
VP
1154
1155 for (i = 0; i < c->body_count; ++i)
1156 check_no_tracepoint_commands ((c->body_list)[i]);
1157
1158 /* Not that command parsing removes leading whitespace and comment
4a64f543 1159 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1160 command directly. */
1161 if (strstr (c->line, "collect ") == c->line)
1162 error (_("The 'collect' command can only be used for tracepoints"));
1163
51661e93
VP
1164 if (strstr (c->line, "teval ") == c->line)
1165 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1166 }
1167}
1168
d77f58be
SS
1169/* Encapsulate tests for different types of tracepoints. */
1170
d9b3f62e
PA
1171static int
1172is_tracepoint_type (enum bptype type)
1173{
1174 return (type == bp_tracepoint
1175 || type == bp_fast_tracepoint
1176 || type == bp_static_tracepoint);
1177}
1178
a7bdde9e 1179int
d77f58be 1180is_tracepoint (const struct breakpoint *b)
a7bdde9e 1181{
d9b3f62e 1182 return is_tracepoint_type (b->type);
a7bdde9e 1183}
d9b3f62e 1184
e5dd4106 1185/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1186 breakpoint. This function will throw an exception if a problem is
1187 found. */
48cb2d85 1188
95a42b64
TT
1189static void
1190validate_commands_for_breakpoint (struct breakpoint *b,
1191 struct command_line *commands)
48cb2d85 1192{
d77f58be 1193 if (is_tracepoint (b))
a7bdde9e 1194 {
c9a6ce02 1195 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1196 struct command_line *c;
1197 struct command_line *while_stepping = 0;
c9a6ce02
PA
1198
1199 /* Reset the while-stepping step count. The previous commands
1200 might have included a while-stepping action, while the new
1201 ones might not. */
1202 t->step_count = 0;
1203
1204 /* We need to verify that each top-level element of commands is
1205 valid for tracepoints, that there's at most one
1206 while-stepping element, and that the while-stepping's body
1207 has valid tracing commands excluding nested while-stepping.
1208 We also need to validate the tracepoint action line in the
1209 context of the tracepoint --- validate_actionline actually
1210 has side effects, like setting the tracepoint's
1211 while-stepping STEP_COUNT, in addition to checking if the
1212 collect/teval actions parse and make sense in the
1213 tracepoint's context. */
a7bdde9e
VP
1214 for (c = commands; c; c = c->next)
1215 {
a7bdde9e
VP
1216 if (c->control_type == while_stepping_control)
1217 {
1218 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1219 error (_("The 'while-stepping' command "
1220 "cannot be used for fast tracepoint"));
0fb4aa4b 1221 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1222 error (_("The 'while-stepping' command "
1223 "cannot be used for static tracepoint"));
a7bdde9e
VP
1224
1225 if (while_stepping)
3e43a32a
MS
1226 error (_("The 'while-stepping' command "
1227 "can be used only once"));
a7bdde9e
VP
1228 else
1229 while_stepping = c;
1230 }
c9a6ce02
PA
1231
1232 validate_actionline (c->line, b);
a7bdde9e
VP
1233 }
1234 if (while_stepping)
1235 {
1236 struct command_line *c2;
1237
1238 gdb_assert (while_stepping->body_count == 1);
1239 c2 = while_stepping->body_list[0];
1240 for (; c2; c2 = c2->next)
1241 {
a7bdde9e
VP
1242 if (c2->control_type == while_stepping_control)
1243 error (_("The 'while-stepping' command cannot be nested"));
1244 }
1245 }
1246 }
1247 else
1248 {
1249 check_no_tracepoint_commands (commands);
1250 }
95a42b64
TT
1251}
1252
0fb4aa4b
PA
1253/* Return a vector of all the static tracepoints set at ADDR. The
1254 caller is responsible for releasing the vector. */
1255
1256VEC(breakpoint_p) *
1257static_tracepoints_here (CORE_ADDR addr)
1258{
1259 struct breakpoint *b;
1260 VEC(breakpoint_p) *found = 0;
1261 struct bp_location *loc;
1262
1263 ALL_BREAKPOINTS (b)
1264 if (b->type == bp_static_tracepoint)
1265 {
1266 for (loc = b->loc; loc; loc = loc->next)
1267 if (loc->address == addr)
1268 VEC_safe_push(breakpoint_p, found, b);
1269 }
1270
1271 return found;
1272}
1273
95a42b64 1274/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1275 validate that only allowed commands are included. */
95a42b64
TT
1276
1277void
4a64f543
MS
1278breakpoint_set_commands (struct breakpoint *b,
1279 struct command_line *commands)
95a42b64
TT
1280{
1281 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1282
9add0f1b
TT
1283 decref_counted_command_line (&b->commands);
1284 b->commands = alloc_counted_command_line (commands);
8d3788bd 1285 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1286}
1287
45a43567
TT
1288/* Set the internal `silent' flag on the breakpoint. Note that this
1289 is not the same as the "silent" that may appear in the breakpoint's
1290 commands. */
1291
1292void
1293breakpoint_set_silent (struct breakpoint *b, int silent)
1294{
1295 int old_silent = b->silent;
1296
1297 b->silent = silent;
1298 if (old_silent != silent)
8d3788bd 1299 observer_notify_breakpoint_modified (b);
45a43567
TT
1300}
1301
1302/* Set the thread for this breakpoint. If THREAD is -1, make the
1303 breakpoint work for any thread. */
1304
1305void
1306breakpoint_set_thread (struct breakpoint *b, int thread)
1307{
1308 int old_thread = b->thread;
1309
1310 b->thread = thread;
1311 if (old_thread != thread)
8d3788bd 1312 observer_notify_breakpoint_modified (b);
45a43567
TT
1313}
1314
1315/* Set the task for this breakpoint. If TASK is 0, make the
1316 breakpoint work for any task. */
1317
1318void
1319breakpoint_set_task (struct breakpoint *b, int task)
1320{
1321 int old_task = b->task;
1322
1323 b->task = task;
1324 if (old_task != task)
8d3788bd 1325 observer_notify_breakpoint_modified (b);
45a43567
TT
1326}
1327
95a42b64
TT
1328void
1329check_tracepoint_command (char *line, void *closure)
a7bdde9e 1330{
9a3c8263 1331 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1332
6f937416 1333 validate_actionline (line, b);
a7bdde9e
VP
1334}
1335
95a42b64
TT
1336/* A structure used to pass information through
1337 map_breakpoint_numbers. */
1338
1339struct commands_info
1340{
1341 /* True if the command was typed at a tty. */
1342 int from_tty;
86b17b60
PA
1343
1344 /* The breakpoint range spec. */
1345 char *arg;
1346
95a42b64
TT
1347 /* Non-NULL if the body of the commands are being read from this
1348 already-parsed command. */
1349 struct command_line *control;
86b17b60 1350
95a42b64
TT
1351 /* The command lines read from the user, or NULL if they have not
1352 yet been read. */
1353 struct counted_command_line *cmd;
1354};
1355
1356/* A callback for map_breakpoint_numbers that sets the commands for
1357 commands_command. */
1358
c906108c 1359static void
95a42b64 1360do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1361{
9a3c8263 1362 struct commands_info *info = (struct commands_info *) data;
c906108c 1363
95a42b64
TT
1364 if (info->cmd == NULL)
1365 {
1366 struct command_line *l;
5c44784c 1367
95a42b64
TT
1368 if (info->control != NULL)
1369 l = copy_command_lines (info->control->body_list[0]);
1370 else
86b17b60
PA
1371 {
1372 struct cleanup *old_chain;
1373 char *str;
c5aa993b 1374
3e43a32a
MS
1375 str = xstrprintf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
86b17b60
PA
1377 info->arg);
1378
1379 old_chain = make_cleanup (xfree, str);
1380
1381 l = read_command_lines (str,
1382 info->from_tty, 1,
d77f58be 1383 (is_tracepoint (b)
86b17b60
PA
1384 ? check_tracepoint_command : 0),
1385 b);
1386
1387 do_cleanups (old_chain);
1388 }
a7bdde9e 1389
95a42b64
TT
1390 info->cmd = alloc_counted_command_line (l);
1391 }
1392
1393 /* If a breakpoint was on the list more than once, we don't need to
1394 do anything. */
1395 if (b->commands != info->cmd)
1396 {
1397 validate_commands_for_breakpoint (b, info->cmd->commands);
1398 incref_counted_command_line (info->cmd);
1399 decref_counted_command_line (&b->commands);
1400 b->commands = info->cmd;
8d3788bd 1401 observer_notify_breakpoint_modified (b);
c5aa993b 1402 }
95a42b64
TT
1403}
1404
1405static void
4a64f543
MS
1406commands_command_1 (char *arg, int from_tty,
1407 struct command_line *control)
95a42b64
TT
1408{
1409 struct cleanup *cleanups;
1410 struct commands_info info;
1411
1412 info.from_tty = from_tty;
1413 info.control = control;
1414 info.cmd = NULL;
1415 /* If we read command lines from the user, then `info' will hold an
1416 extra reference to the commands that we must clean up. */
1417 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1418
1419 if (arg == NULL || !*arg)
1420 {
86b17b60 1421 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1422 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1423 breakpoint_count);
95a42b64
TT
1424 else if (breakpoint_count > 0)
1425 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1426 else
1427 {
1428 /* So that we don't try to free the incoming non-NULL
1429 argument in the cleanup below. Mapping breakpoint
1430 numbers will fail in this case. */
1431 arg = NULL;
1432 }
95a42b64 1433 }
9766ced4
SS
1434 else
1435 /* The command loop has some static state, so we need to preserve
1436 our argument. */
1437 arg = xstrdup (arg);
86b17b60
PA
1438
1439 if (arg != NULL)
1440 make_cleanup (xfree, arg);
1441
1442 info.arg = arg;
95a42b64
TT
1443
1444 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1445
1446 if (info.cmd == NULL)
1447 error (_("No breakpoints specified."));
1448
1449 do_cleanups (cleanups);
1450}
1451
1452static void
1453commands_command (char *arg, int from_tty)
1454{
1455 commands_command_1 (arg, from_tty, NULL);
c906108c 1456}
40c03ae8
EZ
1457
1458/* Like commands_command, but instead of reading the commands from
1459 input stream, takes them from an already parsed command structure.
1460
1461 This is used by cli-script.c to DTRT with breakpoint commands
1462 that are part of if and while bodies. */
1463enum command_control_type
1464commands_from_control_command (char *arg, struct command_line *cmd)
1465{
95a42b64
TT
1466 commands_command_1 (arg, 0, cmd);
1467 return simple_control;
40c03ae8 1468}
876fa593
JK
1469
1470/* Return non-zero if BL->TARGET_INFO contains valid information. */
1471
1472static int
1473bp_location_has_shadow (struct bp_location *bl)
1474{
1475 if (bl->loc_type != bp_loc_software_breakpoint)
1476 return 0;
1477 if (!bl->inserted)
1478 return 0;
1479 if (bl->target_info.shadow_len == 0)
e5dd4106 1480 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1481 return 0;
1482 return 1;
1483}
1484
9d497a19
PA
1485/* Update BUF, which is LEN bytes read from the target address
1486 MEMADDR, by replacing a memory breakpoint with its shadowed
1487 contents.
1488
1489 If READBUF is not NULL, this buffer must not overlap with the of
1490 the breakpoint location's shadow_contents buffer. Otherwise, a
1491 failed assertion internal error will be raised. */
1492
1493static void
1494one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1495 const gdb_byte *writebuf_org,
1496 ULONGEST memaddr, LONGEST len,
1497 struct bp_target_info *target_info,
1498 struct gdbarch *gdbarch)
1499{
1500 /* Now do full processing of the found relevant range of elements. */
1501 CORE_ADDR bp_addr = 0;
1502 int bp_size = 0;
1503 int bptoffset = 0;
1504
1505 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1506 current_program_space->aspace, 0))
1507 {
1508 /* The breakpoint is inserted in a different address space. */
1509 return;
1510 }
1511
1512 /* Addresses and length of the part of the breakpoint that
1513 we need to copy. */
1514 bp_addr = target_info->placed_address;
1515 bp_size = target_info->shadow_len;
1516
1517 if (bp_addr + bp_size <= memaddr)
1518 {
1519 /* The breakpoint is entirely before the chunk of memory we are
1520 reading. */
1521 return;
1522 }
1523
1524 if (bp_addr >= memaddr + len)
1525 {
1526 /* The breakpoint is entirely after the chunk of memory we are
1527 reading. */
1528 return;
1529 }
1530
1531 /* Offset within shadow_contents. */
1532 if (bp_addr < memaddr)
1533 {
1534 /* Only copy the second part of the breakpoint. */
1535 bp_size -= memaddr - bp_addr;
1536 bptoffset = memaddr - bp_addr;
1537 bp_addr = memaddr;
1538 }
1539
1540 if (bp_addr + bp_size > memaddr + len)
1541 {
1542 /* Only copy the first part of the breakpoint. */
1543 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1544 }
1545
1546 if (readbuf != NULL)
1547 {
1548 /* Verify that the readbuf buffer does not overlap with the
1549 shadow_contents buffer. */
1550 gdb_assert (target_info->shadow_contents >= readbuf + len
1551 || readbuf >= (target_info->shadow_contents
1552 + target_info->shadow_len));
1553
1554 /* Update the read buffer with this inserted breakpoint's
1555 shadow. */
1556 memcpy (readbuf + bp_addr - memaddr,
1557 target_info->shadow_contents + bptoffset, bp_size);
1558 }
1559 else
1560 {
1561 const unsigned char *bp;
0d5ed153
MR
1562 CORE_ADDR addr = target_info->reqstd_address;
1563 int placed_size;
9d497a19
PA
1564
1565 /* Update the shadow with what we want to write to memory. */
1566 memcpy (target_info->shadow_contents + bptoffset,
1567 writebuf_org + bp_addr - memaddr, bp_size);
1568
1569 /* Determine appropriate breakpoint contents and size for this
1570 address. */
0d5ed153 1571 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1572
1573 /* Update the final write buffer with this inserted
1574 breakpoint's INSN. */
1575 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1576 }
1577}
1578
8defab1a 1579/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1580 by replacing any memory breakpoints with their shadowed contents.
1581
35c63cd8
JB
1582 If READBUF is not NULL, this buffer must not overlap with any of
1583 the breakpoint location's shadow_contents buffers. Otherwise,
1584 a failed assertion internal error will be raised.
1585
876fa593 1586 The range of shadowed area by each bp_location is:
35df4500
TJB
1587 bl->address - bp_location_placed_address_before_address_max
1588 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1589 The range we were requested to resolve shadows for is:
1590 memaddr ... memaddr + len
1591 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1592 memaddr + len <= (bl->address
1593 - bp_location_placed_address_before_address_max)
876fa593 1594 and:
35df4500 1595 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1596
8defab1a 1597void
f0ba3972
PA
1598breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1599 const gdb_byte *writebuf_org,
1600 ULONGEST memaddr, LONGEST len)
c906108c 1601{
4a64f543
MS
1602 /* Left boundary, right boundary and median element of our binary
1603 search. */
876fa593 1604 unsigned bc_l, bc_r, bc;
9d497a19 1605 size_t i;
876fa593 1606
4a64f543
MS
1607 /* Find BC_L which is a leftmost element which may affect BUF
1608 content. It is safe to report lower value but a failure to
1609 report higher one. */
876fa593
JK
1610
1611 bc_l = 0;
1612 bc_r = bp_location_count;
1613 while (bc_l + 1 < bc_r)
1614 {
35df4500 1615 struct bp_location *bl;
876fa593
JK
1616
1617 bc = (bc_l + bc_r) / 2;
35df4500 1618 bl = bp_location[bc];
876fa593 1619
4a64f543
MS
1620 /* Check first BL->ADDRESS will not overflow due to the added
1621 constant. Then advance the left boundary only if we are sure
1622 the BC element can in no way affect the BUF content (MEMADDR
1623 to MEMADDR + LEN range).
876fa593 1624
4a64f543
MS
1625 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1626 offset so that we cannot miss a breakpoint with its shadow
1627 range tail still reaching MEMADDR. */
c5aa993b 1628
35df4500
TJB
1629 if ((bl->address + bp_location_shadow_len_after_address_max
1630 >= bl->address)
1631 && (bl->address + bp_location_shadow_len_after_address_max
1632 <= memaddr))
876fa593
JK
1633 bc_l = bc;
1634 else
1635 bc_r = bc;
1636 }
1637
128070bb
PA
1638 /* Due to the binary search above, we need to make sure we pick the
1639 first location that's at BC_L's address. E.g., if there are
1640 multiple locations at the same address, BC_L may end up pointing
1641 at a duplicate location, and miss the "master"/"inserted"
1642 location. Say, given locations L1, L2 and L3 at addresses A and
1643 B:
1644
1645 L1@A, L2@A, L3@B, ...
1646
1647 BC_L could end up pointing at location L2, while the "master"
1648 location could be L1. Since the `loc->inserted' flag is only set
1649 on "master" locations, we'd forget to restore the shadow of L1
1650 and L2. */
1651 while (bc_l > 0
1652 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1653 bc_l--;
1654
876fa593
JK
1655 /* Now do full processing of the found relevant range of elements. */
1656
1657 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1658 {
35df4500 1659 struct bp_location *bl = bp_location[bc];
876fa593
JK
1660 CORE_ADDR bp_addr = 0;
1661 int bp_size = 0;
1662 int bptoffset = 0;
1663
35df4500
TJB
1664 /* bp_location array has BL->OWNER always non-NULL. */
1665 if (bl->owner->type == bp_none)
8a3fe4f8 1666 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1667 bl->owner->number);
ffce0d52 1668
e5dd4106 1669 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1670 content. */
1671
35df4500
TJB
1672 if (bl->address >= bp_location_placed_address_before_address_max
1673 && memaddr + len <= (bl->address
1674 - bp_location_placed_address_before_address_max))
876fa593
JK
1675 break;
1676
35df4500 1677 if (!bp_location_has_shadow (bl))
c5aa993b 1678 continue;
6c95b8df 1679
9d497a19
PA
1680 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1681 memaddr, len, &bl->target_info, bl->gdbarch);
1682 }
c906108c 1683}
9d497a19 1684
c906108c 1685\f
c5aa993b 1686
b775012e
LM
1687/* Return true if BPT is either a software breakpoint or a hardware
1688 breakpoint. */
1689
1690int
1691is_breakpoint (const struct breakpoint *bpt)
1692{
1693 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1694 || bpt->type == bp_hardware_breakpoint
1695 || bpt->type == bp_dprintf);
b775012e
LM
1696}
1697
60e1c644
PA
1698/* Return true if BPT is of any hardware watchpoint kind. */
1699
a5606eee 1700static int
d77f58be 1701is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1702{
1703 return (bpt->type == bp_hardware_watchpoint
1704 || bpt->type == bp_read_watchpoint
1705 || bpt->type == bp_access_watchpoint);
1706}
7270d8f2 1707
60e1c644
PA
1708/* Return true if BPT is of any watchpoint kind, hardware or
1709 software. */
1710
3a5c3e22 1711int
d77f58be 1712is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1713{
1714 return (is_hardware_watchpoint (bpt)
1715 || bpt->type == bp_watchpoint);
1716}
1717
3a5c3e22
PA
1718/* Returns true if the current thread and its running state are safe
1719 to evaluate or update watchpoint B. Watchpoints on local
1720 expressions need to be evaluated in the context of the thread that
1721 was current when the watchpoint was created, and, that thread needs
1722 to be stopped to be able to select the correct frame context.
1723 Watchpoints on global expressions can be evaluated on any thread,
1724 and in any state. It is presently left to the target allowing
1725 memory accesses when threads are running. */
f6bc2008
PA
1726
1727static int
3a5c3e22 1728watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1729{
d0d8b0c6
JK
1730 return (b->base.pspace == current_program_space
1731 && (ptid_equal (b->watchpoint_thread, null_ptid)
1732 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1733 && !is_executing (inferior_ptid))));
f6bc2008
PA
1734}
1735
d0fb5eae
JK
1736/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1737 associated bp_watchpoint_scope breakpoint. */
1738
1739static void
3a5c3e22 1740watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1741{
3a5c3e22 1742 struct breakpoint *b = &w->base;
d0fb5eae
JK
1743
1744 if (b->related_breakpoint != b)
1745 {
1746 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1747 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1748 b->related_breakpoint->disposition = disp_del_at_next_stop;
1749 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1750 b->related_breakpoint = b;
1751 }
1752 b->disposition = disp_del_at_next_stop;
1753}
1754
bb9d5f81
PP
1755/* Extract a bitfield value from value VAL using the bit parameters contained in
1756 watchpoint W. */
1757
1758static struct value *
1759extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1760{
1761 struct value *bit_val;
1762
1763 if (val == NULL)
1764 return NULL;
1765
1766 bit_val = allocate_value (value_type (val));
1767
1768 unpack_value_bitfield (bit_val,
1769 w->val_bitpos,
1770 w->val_bitsize,
1771 value_contents_for_printing (val),
1772 value_offset (val),
1773 val);
1774
1775 return bit_val;
1776}
1777
567e1b4e
JB
1778/* Assuming that B is a watchpoint:
1779 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1780 - Evaluate expression and store the result in B->val
567e1b4e
JB
1781 - Evaluate the condition if there is one, and store the result
1782 in b->loc->cond.
a5606eee
VP
1783 - Update the list of values that must be watched in B->loc.
1784
4a64f543
MS
1785 If the watchpoint disposition is disp_del_at_next_stop, then do
1786 nothing. If this is local watchpoint that is out of scope, delete
1787 it.
1788
1789 Even with `set breakpoint always-inserted on' the watchpoints are
1790 removed + inserted on each stop here. Normal breakpoints must
1791 never be removed because they might be missed by a running thread
1792 when debugging in non-stop mode. On the other hand, hardware
1793 watchpoints (is_hardware_watchpoint; processed here) are specific
1794 to each LWP since they are stored in each LWP's hardware debug
1795 registers. Therefore, such LWP must be stopped first in order to
1796 be able to modify its hardware watchpoints.
1797
1798 Hardware watchpoints must be reset exactly once after being
1799 presented to the user. It cannot be done sooner, because it would
1800 reset the data used to present the watchpoint hit to the user. And
1801 it must not be done later because it could display the same single
1802 watchpoint hit during multiple GDB stops. Note that the latter is
1803 relevant only to the hardware watchpoint types bp_read_watchpoint
1804 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1805 not user-visible - its hit is suppressed if the memory content has
1806 not changed.
1807
1808 The following constraints influence the location where we can reset
1809 hardware watchpoints:
1810
1811 * target_stopped_by_watchpoint and target_stopped_data_address are
1812 called several times when GDB stops.
1813
1814 [linux]
1815 * Multiple hardware watchpoints can be hit at the same time,
1816 causing GDB to stop. GDB only presents one hardware watchpoint
1817 hit at a time as the reason for stopping, and all the other hits
1818 are presented later, one after the other, each time the user
1819 requests the execution to be resumed. Execution is not resumed
1820 for the threads still having pending hit event stored in
1821 LWP_INFO->STATUS. While the watchpoint is already removed from
1822 the inferior on the first stop the thread hit event is kept being
1823 reported from its cached value by linux_nat_stopped_data_address
1824 until the real thread resume happens after the watchpoint gets
1825 presented and thus its LWP_INFO->STATUS gets reset.
1826
1827 Therefore the hardware watchpoint hit can get safely reset on the
1828 watchpoint removal from inferior. */
a79d3c27 1829
b40ce68a 1830static void
3a5c3e22 1831update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1832{
a5606eee 1833 int within_current_scope;
a5606eee 1834 struct frame_id saved_frame_id;
66076460 1835 int frame_saved;
a5606eee 1836
f6bc2008
PA
1837 /* If this is a local watchpoint, we only want to check if the
1838 watchpoint frame is in scope if the current thread is the thread
1839 that was used to create the watchpoint. */
1840 if (!watchpoint_in_thread_scope (b))
1841 return;
1842
3a5c3e22 1843 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1844 return;
1845
66076460 1846 frame_saved = 0;
a5606eee
VP
1847
1848 /* Determine if the watchpoint is within scope. */
1849 if (b->exp_valid_block == NULL)
1850 within_current_scope = 1;
1851 else
1852 {
b5db5dfc
UW
1853 struct frame_info *fi = get_current_frame ();
1854 struct gdbarch *frame_arch = get_frame_arch (fi);
1855 CORE_ADDR frame_pc = get_frame_pc (fi);
1856
c9cf6e20
MG
1857 /* If we're at a point where the stack has been destroyed
1858 (e.g. in a function epilogue), unwinding may not work
1859 properly. Do not attempt to recreate locations at this
b5db5dfc 1860 point. See similar comments in watchpoint_check. */
c9cf6e20 1861 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1862 return;
66076460
DJ
1863
1864 /* Save the current frame's ID so we can restore it after
1865 evaluating the watchpoint expression on its own frame. */
1866 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1867 took a frame parameter, so that we didn't have to change the
1868 selected frame. */
1869 frame_saved = 1;
1870 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1871
a5606eee
VP
1872 fi = frame_find_by_id (b->watchpoint_frame);
1873 within_current_scope = (fi != NULL);
1874 if (within_current_scope)
1875 select_frame (fi);
1876 }
1877
b5db5dfc
UW
1878 /* We don't free locations. They are stored in the bp_location array
1879 and update_global_location_list will eventually delete them and
1880 remove breakpoints if needed. */
3a5c3e22 1881 b->base.loc = NULL;
b5db5dfc 1882
a5606eee
VP
1883 if (within_current_scope && reparse)
1884 {
bbc13ae3 1885 const char *s;
d63d0675 1886
a5606eee
VP
1887 if (b->exp)
1888 {
1889 xfree (b->exp);
1890 b->exp = NULL;
1891 }
d63d0675 1892 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1893 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1894 /* If the meaning of expression itself changed, the old value is
1895 no longer relevant. We don't want to report a watchpoint hit
1896 to the user when the old value and the new value may actually
1897 be completely different objects. */
1898 value_free (b->val);
fa4727a6
DJ
1899 b->val = NULL;
1900 b->val_valid = 0;
60e1c644
PA
1901
1902 /* Note that unlike with breakpoints, the watchpoint's condition
1903 expression is stored in the breakpoint object, not in the
1904 locations (re)created below. */
3a5c3e22 1905 if (b->base.cond_string != NULL)
60e1c644
PA
1906 {
1907 if (b->cond_exp != NULL)
1908 {
1909 xfree (b->cond_exp);
1910 b->cond_exp = NULL;
1911 }
1912
3a5c3e22 1913 s = b->base.cond_string;
1bb9788d 1914 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1915 }
a5606eee 1916 }
a5606eee
VP
1917
1918 /* If we failed to parse the expression, for example because
1919 it refers to a global variable in a not-yet-loaded shared library,
1920 don't try to insert watchpoint. We don't automatically delete
1921 such watchpoint, though, since failure to parse expression
1922 is different from out-of-scope watchpoint. */
e8369a73 1923 if (!target_has_execution)
2d134ed3
PA
1924 {
1925 /* Without execution, memory can't change. No use to try and
1926 set watchpoint locations. The watchpoint will be reset when
1927 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1928 if (!can_use_hw_watchpoints)
1929 {
1930 if (b->base.ops->works_in_software_mode (&b->base))
1931 b->base.type = bp_watchpoint;
1932 else
638aa5a1
AB
1933 error (_("Can't set read/access watchpoint when "
1934 "hardware watchpoints are disabled."));
e8369a73 1935 }
2d134ed3
PA
1936 }
1937 else if (within_current_scope && b->exp)
a5606eee 1938 {
0cf6dd15 1939 int pc = 0;
fa4727a6 1940 struct value *val_chain, *v, *result, *next;
2d134ed3 1941 struct program_space *frame_pspace;
a5606eee 1942
3a1115a0 1943 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1944
a5606eee
VP
1945 /* Avoid setting b->val if it's already set. The meaning of
1946 b->val is 'the last value' user saw, and we should update
1947 it only if we reported that last value to user. As it
9c06b0b4
TJB
1948 happens, the code that reports it updates b->val directly.
1949 We don't keep track of the memory value for masked
1950 watchpoints. */
3a5c3e22 1951 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1952 {
bb9d5f81
PP
1953 if (b->val_bitsize != 0)
1954 {
1955 v = extract_bitfield_from_watchpoint_value (b, v);
1956 if (v != NULL)
1957 release_value (v);
1958 }
fa4727a6
DJ
1959 b->val = v;
1960 b->val_valid = 1;
1961 }
a5606eee 1962
2d134ed3
PA
1963 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1964
a5606eee 1965 /* Look at each value on the value chain. */
9fa40276 1966 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1967 {
1968 /* If it's a memory location, and GDB actually needed
1969 its contents to evaluate the expression, then we
fa4727a6
DJ
1970 must watch it. If the first value returned is
1971 still lazy, that means an error occurred reading it;
1972 watch it anyway in case it becomes readable. */
a5606eee 1973 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1974 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1975 {
1976 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1977
a5606eee
VP
1978 /* We only watch structs and arrays if user asked
1979 for it explicitly, never if they just happen to
1980 appear in the middle of some value chain. */
fa4727a6 1981 if (v == result
a5606eee
VP
1982 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1983 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1984 {
1985 CORE_ADDR addr;
f486487f 1986 enum target_hw_bp_type type;
a5606eee 1987 struct bp_location *loc, **tmp;
bb9d5f81
PP
1988 int bitpos = 0, bitsize = 0;
1989
1990 if (value_bitsize (v) != 0)
1991 {
1992 /* Extract the bit parameters out from the bitfield
1993 sub-expression. */
1994 bitpos = value_bitpos (v);
1995 bitsize = value_bitsize (v);
1996 }
1997 else if (v == result && b->val_bitsize != 0)
1998 {
1999 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2000 lvalue whose bit parameters are saved in the fields
2001 VAL_BITPOS and VAL_BITSIZE. */
2002 bitpos = b->val_bitpos;
2003 bitsize = b->val_bitsize;
2004 }
a5606eee 2005
42ae5230 2006 addr = value_address (v);
bb9d5f81
PP
2007 if (bitsize != 0)
2008 {
2009 /* Skip the bytes that don't contain the bitfield. */
2010 addr += bitpos / 8;
2011 }
2012
a5606eee 2013 type = hw_write;
3a5c3e22 2014 if (b->base.type == bp_read_watchpoint)
a5606eee 2015 type = hw_read;
3a5c3e22 2016 else if (b->base.type == bp_access_watchpoint)
a5606eee 2017 type = hw_access;
3a5c3e22
PA
2018
2019 loc = allocate_bp_location (&b->base);
2020 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2021 ;
2022 *tmp = loc;
a6d9a66e 2023 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2024
2025 loc->pspace = frame_pspace;
a5606eee 2026 loc->address = addr;
bb9d5f81
PP
2027
2028 if (bitsize != 0)
2029 {
2030 /* Just cover the bytes that make up the bitfield. */
2031 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2032 }
2033 else
2034 loc->length = TYPE_LENGTH (value_type (v));
2035
a5606eee
VP
2036 loc->watchpoint_type = type;
2037 }
2038 }
9fa40276
TJB
2039 }
2040
2041 /* Change the type of breakpoint between hardware assisted or
2042 an ordinary watchpoint depending on the hardware support
2043 and free hardware slots. REPARSE is set when the inferior
2044 is started. */
a9634178 2045 if (reparse)
9fa40276 2046 {
e09342b5 2047 int reg_cnt;
9fa40276
TJB
2048 enum bp_loc_type loc_type;
2049 struct bp_location *bl;
a5606eee 2050
a9634178 2051 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2052
2053 if (reg_cnt)
9fa40276
TJB
2054 {
2055 int i, target_resources_ok, other_type_used;
a1398e0c 2056 enum bptype type;
9fa40276 2057
a9634178
TJB
2058 /* Use an exact watchpoint when there's only one memory region to be
2059 watched, and only one debug register is needed to watch it. */
2060 b->exact = target_exact_watchpoints && reg_cnt == 1;
2061
9fa40276 2062 /* We need to determine how many resources are already
e09342b5
TJB
2063 used for all other hardware watchpoints plus this one
2064 to see if we still have enough resources to also fit
a1398e0c
PA
2065 this watchpoint in as well. */
2066
2067 /* If this is a software watchpoint, we try to turn it
2068 to a hardware one -- count resources as if B was of
2069 hardware watchpoint type. */
2070 type = b->base.type;
2071 if (type == bp_watchpoint)
2072 type = bp_hardware_watchpoint;
2073
2074 /* This watchpoint may or may not have been placed on
2075 the list yet at this point (it won't be in the list
2076 if we're trying to create it for the first time,
2077 through watch_command), so always account for it
2078 manually. */
2079
2080 /* Count resources used by all watchpoints except B. */
2081 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2082
2083 /* Add in the resources needed for B. */
2084 i += hw_watchpoint_use_count (&b->base);
2085
2086 target_resources_ok
2087 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2088 if (target_resources_ok <= 0)
a9634178 2089 {
3a5c3e22 2090 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2091
2092 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2093 error (_("Target does not support this type of "
2094 "hardware watchpoint."));
9c06b0b4
TJB
2095 else if (target_resources_ok < 0 && !sw_mode)
2096 error (_("There are not enough available hardware "
2097 "resources for this watchpoint."));
a1398e0c
PA
2098
2099 /* Downgrade to software watchpoint. */
2100 b->base.type = bp_watchpoint;
2101 }
2102 else
2103 {
2104 /* If this was a software watchpoint, we've just
2105 found we have enough resources to turn it to a
2106 hardware watchpoint. Otherwise, this is a
2107 nop. */
2108 b->base.type = type;
a9634178 2109 }
9fa40276 2110 }
3a5c3e22 2111 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2112 {
2113 if (!can_use_hw_watchpoints)
2114 error (_("Can't set read/access watchpoint when "
2115 "hardware watchpoints are disabled."));
2116 else
2117 error (_("Expression cannot be implemented with "
2118 "read/access watchpoint."));
2119 }
9fa40276 2120 else
3a5c3e22 2121 b->base.type = bp_watchpoint;
9fa40276 2122
3a5c3e22 2123 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2124 : bp_loc_hardware_watchpoint);
3a5c3e22 2125 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2126 bl->loc_type = loc_type;
2127 }
2128
2129 for (v = val_chain; v; v = next)
2130 {
a5606eee
VP
2131 next = value_next (v);
2132 if (v != b->val)
2133 value_free (v);
2134 }
2135
c7437ca6
PA
2136 /* If a software watchpoint is not watching any memory, then the
2137 above left it without any location set up. But,
2138 bpstat_stop_status requires a location to be able to report
2139 stops, so make sure there's at least a dummy one. */
3a5c3e22 2140 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2141 {
3a5c3e22
PA
2142 struct breakpoint *base = &b->base;
2143 base->loc = allocate_bp_location (base);
2144 base->loc->pspace = frame_pspace;
2145 base->loc->address = -1;
2146 base->loc->length = -1;
2147 base->loc->watchpoint_type = -1;
c7437ca6 2148 }
a5606eee
VP
2149 }
2150 else if (!within_current_scope)
7270d8f2 2151 {
ac74f770
MS
2152 printf_filtered (_("\
2153Watchpoint %d deleted because the program has left the block\n\
2154in which its expression is valid.\n"),
3a5c3e22 2155 b->base.number);
d0fb5eae 2156 watchpoint_del_at_next_stop (b);
7270d8f2 2157 }
a5606eee
VP
2158
2159 /* Restore the selected frame. */
66076460
DJ
2160 if (frame_saved)
2161 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2162}
2163
a5606eee 2164
74960c60 2165/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2166 inserted in the inferior. We don't differentiate the type of BL's owner
2167 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2168 breakpoint_ops is not defined, because in insert_bp_location,
2169 tracepoint's insert_location will not be called. */
74960c60 2170static int
35df4500 2171should_be_inserted (struct bp_location *bl)
74960c60 2172{
35df4500 2173 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2174 return 0;
2175
35df4500 2176 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2177 return 0;
2178
35df4500 2179 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2180 return 0;
2181
f8eba3c6
TT
2182 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2183 return 0;
2184
56710373
PA
2185 /* This is set for example, when we're attached to the parent of a
2186 vfork, and have detached from the child. The child is running
2187 free, and we expect it to do an exec or exit, at which point the
2188 OS makes the parent schedulable again (and the target reports
2189 that the vfork is done). Until the child is done with the shared
2190 memory region, do not insert breakpoints in the parent, otherwise
2191 the child could still trip on the parent's breakpoints. Since
2192 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2193 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2194 return 0;
2195
31e77af2
PA
2196 /* Don't insert a breakpoint if we're trying to step past its
2197 location. */
2198 if ((bl->loc_type == bp_loc_software_breakpoint
2199 || bl->loc_type == bp_loc_hardware_breakpoint)
2200 && stepping_past_instruction_at (bl->pspace->aspace,
2201 bl->address))
e558d7c1
PA
2202 {
2203 if (debug_infrun)
2204 {
2205 fprintf_unfiltered (gdb_stdlog,
2206 "infrun: skipping breakpoint: "
2207 "stepping past insn at: %s\n",
2208 paddress (bl->gdbarch, bl->address));
2209 }
2210 return 0;
2211 }
31e77af2 2212
963f9c80
PA
2213 /* Don't insert watchpoints if we're trying to step past the
2214 instruction that triggered one. */
2215 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2216 && stepping_past_nonsteppable_watchpoint ())
2217 {
2218 if (debug_infrun)
2219 {
2220 fprintf_unfiltered (gdb_stdlog,
2221 "infrun: stepping past non-steppable watchpoint. "
2222 "skipping watchpoint at %s:%d\n",
2223 paddress (bl->gdbarch, bl->address),
2224 bl->length);
2225 }
2226 return 0;
2227 }
2228
74960c60
VP
2229 return 1;
2230}
2231
934709f0
PW
2232/* Same as should_be_inserted but does the check assuming
2233 that the location is not duplicated. */
2234
2235static int
2236unduplicated_should_be_inserted (struct bp_location *bl)
2237{
2238 int result;
2239 const int save_duplicate = bl->duplicate;
2240
2241 bl->duplicate = 0;
2242 result = should_be_inserted (bl);
2243 bl->duplicate = save_duplicate;
2244 return result;
2245}
2246
b775012e
LM
2247/* Parses a conditional described by an expression COND into an
2248 agent expression bytecode suitable for evaluation
2249 by the bytecode interpreter. Return NULL if there was
2250 any error during parsing. */
2251
2252static struct agent_expr *
2253parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2254{
2255 struct agent_expr *aexpr = NULL;
b775012e
LM
2256
2257 if (!cond)
2258 return NULL;
2259
2260 /* We don't want to stop processing, so catch any errors
2261 that may show up. */
492d29ea 2262 TRY
b775012e
LM
2263 {
2264 aexpr = gen_eval_for_expr (scope, cond);
2265 }
2266
492d29ea 2267 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2268 {
2269 /* If we got here, it means the condition could not be parsed to a valid
2270 bytecode expression and thus can't be evaluated on the target's side.
2271 It's no use iterating through the conditions. */
2272 return NULL;
2273 }
492d29ea 2274 END_CATCH
b775012e
LM
2275
2276 /* We have a valid agent expression. */
2277 return aexpr;
2278}
2279
2280/* Based on location BL, create a list of breakpoint conditions to be
2281 passed on to the target. If we have duplicated locations with different
2282 conditions, we will add such conditions to the list. The idea is that the
2283 target will evaluate the list of conditions and will only notify GDB when
2284 one of them is true. */
2285
2286static void
2287build_target_condition_list (struct bp_location *bl)
2288{
2289 struct bp_location **locp = NULL, **loc2p;
2290 int null_condition_or_parse_error = 0;
2291 int modified = bl->needs_update;
2292 struct bp_location *loc;
2293
8b4f3082
PA
2294 /* Release conditions left over from a previous insert. */
2295 VEC_free (agent_expr_p, bl->target_info.conditions);
2296
b775012e
LM
2297 /* This is only meaningful if the target is
2298 evaluating conditions and if the user has
2299 opted for condition evaluation on the target's
2300 side. */
2301 if (gdb_evaluates_breakpoint_condition_p ()
2302 || !target_supports_evaluation_of_breakpoint_conditions ())
2303 return;
2304
2305 /* Do a first pass to check for locations with no assigned
2306 conditions or conditions that fail to parse to a valid agent expression
2307 bytecode. If any of these happen, then it's no use to send conditions
2308 to the target since this location will always trigger and generate a
2309 response back to GDB. */
2310 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2311 {
2312 loc = (*loc2p);
2313 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2314 {
2315 if (modified)
2316 {
2317 struct agent_expr *aexpr;
2318
2319 /* Re-parse the conditions since something changed. In that
2320 case we already freed the condition bytecodes (see
2321 force_breakpoint_reinsertion). We just
2322 need to parse the condition to bytecodes again. */
2323 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2324 loc->cond_bytecode = aexpr;
2325
2326 /* Check if we managed to parse the conditional expression
2327 correctly. If not, we will not send this condition
2328 to the target. */
2329 if (aexpr)
2330 continue;
2331 }
2332
2333 /* If we have a NULL bytecode expression, it means something
2334 went wrong or we have a null condition expression. */
2335 if (!loc->cond_bytecode)
2336 {
2337 null_condition_or_parse_error = 1;
2338 break;
2339 }
2340 }
2341 }
2342
2343 /* If any of these happened, it means we will have to evaluate the conditions
2344 for the location's address on gdb's side. It is no use keeping bytecodes
2345 for all the other duplicate locations, thus we free all of them here.
2346
2347 This is so we have a finer control over which locations' conditions are
2348 being evaluated by GDB or the remote stub. */
2349 if (null_condition_or_parse_error)
2350 {
2351 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2352 {
2353 loc = (*loc2p);
2354 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2355 {
2356 /* Only go as far as the first NULL bytecode is
2357 located. */
2358 if (!loc->cond_bytecode)
2359 return;
2360
2361 free_agent_expr (loc->cond_bytecode);
2362 loc->cond_bytecode = NULL;
2363 }
2364 }
2365 }
2366
2367 /* No NULL conditions or failed bytecode generation. Build a condition list
2368 for this location's address. */
2369 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2370 {
2371 loc = (*loc2p);
2372 if (loc->cond
2373 && is_breakpoint (loc->owner)
2374 && loc->pspace->num == bl->pspace->num
2375 && loc->owner->enable_state == bp_enabled
2376 && loc->enabled)
2377 /* Add the condition to the vector. This will be used later to send the
2378 conditions to the target. */
2379 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2380 loc->cond_bytecode);
2381 }
2382
2383 return;
2384}
2385
d3ce09f5
SS
2386/* Parses a command described by string CMD into an agent expression
2387 bytecode suitable for evaluation by the bytecode interpreter.
2388 Return NULL if there was any error during parsing. */
2389
2390static struct agent_expr *
2391parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2392{
2393 struct cleanup *old_cleanups = 0;
2394 struct expression *expr, **argvec;
2395 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2396 const char *cmdrest;
2397 const char *format_start, *format_end;
d3ce09f5
SS
2398 struct format_piece *fpieces;
2399 int nargs;
2400 struct gdbarch *gdbarch = get_current_arch ();
2401
2402 if (!cmd)
2403 return NULL;
2404
2405 cmdrest = cmd;
2406
2407 if (*cmdrest == ',')
2408 ++cmdrest;
bbc13ae3 2409 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2410
2411 if (*cmdrest++ != '"')
2412 error (_("No format string following the location"));
2413
2414 format_start = cmdrest;
2415
2416 fpieces = parse_format_string (&cmdrest);
2417
2418 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2419
2420 format_end = cmdrest;
2421
2422 if (*cmdrest++ != '"')
2423 error (_("Bad format string, non-terminated '\"'."));
2424
bbc13ae3 2425 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2426
2427 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2428 error (_("Invalid argument syntax"));
2429
2430 if (*cmdrest == ',')
2431 cmdrest++;
bbc13ae3 2432 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2433
2434 /* For each argument, make an expression. */
2435
2436 argvec = (struct expression **) alloca (strlen (cmd)
2437 * sizeof (struct expression *));
2438
2439 nargs = 0;
2440 while (*cmdrest != '\0')
2441 {
bbc13ae3 2442 const char *cmd1;
d3ce09f5
SS
2443
2444 cmd1 = cmdrest;
2445 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2446 argvec[nargs++] = expr;
2447 cmdrest = cmd1;
2448 if (*cmdrest == ',')
2449 ++cmdrest;
2450 }
2451
2452 /* We don't want to stop processing, so catch any errors
2453 that may show up. */
492d29ea 2454 TRY
d3ce09f5
SS
2455 {
2456 aexpr = gen_printf (scope, gdbarch, 0, 0,
2457 format_start, format_end - format_start,
2458 fpieces, nargs, argvec);
2459 }
492d29ea 2460 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2461 {
2462 /* If we got here, it means the command could not be parsed to a valid
2463 bytecode expression and thus can't be evaluated on the target's side.
2464 It's no use iterating through the other commands. */
492d29ea 2465 aexpr = NULL;
d3ce09f5 2466 }
492d29ea
PA
2467 END_CATCH
2468
2469 do_cleanups (old_cleanups);
d3ce09f5 2470
d3ce09f5
SS
2471 /* We have a valid agent expression, return it. */
2472 return aexpr;
2473}
2474
2475/* Based on location BL, create a list of breakpoint commands to be
2476 passed on to the target. If we have duplicated locations with
2477 different commands, we will add any such to the list. */
2478
2479static void
2480build_target_command_list (struct bp_location *bl)
2481{
2482 struct bp_location **locp = NULL, **loc2p;
2483 int null_command_or_parse_error = 0;
2484 int modified = bl->needs_update;
2485 struct bp_location *loc;
2486
8b4f3082
PA
2487 /* Release commands left over from a previous insert. */
2488 VEC_free (agent_expr_p, bl->target_info.tcommands);
2489
41fac0cf 2490 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2491 return;
2492
41fac0cf
PA
2493 /* For now, limit to agent-style dprintf breakpoints. */
2494 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2495 return;
2496
41fac0cf
PA
2497 /* For now, if we have any duplicate location that isn't a dprintf,
2498 don't install the target-side commands, as that would make the
2499 breakpoint not be reported to the core, and we'd lose
2500 control. */
2501 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2502 {
2503 loc = (*loc2p);
2504 if (is_breakpoint (loc->owner)
2505 && loc->pspace->num == bl->pspace->num
2506 && loc->owner->type != bp_dprintf)
2507 return;
2508 }
2509
d3ce09f5
SS
2510 /* Do a first pass to check for locations with no assigned
2511 conditions or conditions that fail to parse to a valid agent expression
2512 bytecode. If any of these happen, then it's no use to send conditions
2513 to the target since this location will always trigger and generate a
2514 response back to GDB. */
2515 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2516 {
2517 loc = (*loc2p);
2518 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2519 {
2520 if (modified)
2521 {
2522 struct agent_expr *aexpr;
2523
2524 /* Re-parse the commands since something changed. In that
2525 case we already freed the command bytecodes (see
2526 force_breakpoint_reinsertion). We just
2527 need to parse the command to bytecodes again. */
2528 aexpr = parse_cmd_to_aexpr (bl->address,
2529 loc->owner->extra_string);
2530 loc->cmd_bytecode = aexpr;
2531
2532 if (!aexpr)
2533 continue;
2534 }
2535
2536 /* If we have a NULL bytecode expression, it means something
2537 went wrong or we have a null command expression. */
2538 if (!loc->cmd_bytecode)
2539 {
2540 null_command_or_parse_error = 1;
2541 break;
2542 }
2543 }
2544 }
2545
2546 /* If anything failed, then we're not doing target-side commands,
2547 and so clean up. */
2548 if (null_command_or_parse_error)
2549 {
2550 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2551 {
2552 loc = (*loc2p);
2553 if (is_breakpoint (loc->owner)
2554 && loc->pspace->num == bl->pspace->num)
2555 {
2556 /* Only go as far as the first NULL bytecode is
2557 located. */
40fb6c5e 2558 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2559 return;
2560
40fb6c5e
HZ
2561 free_agent_expr (loc->cmd_bytecode);
2562 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2563 }
2564 }
2565 }
2566
2567 /* No NULL commands or failed bytecode generation. Build a command list
2568 for this location's address. */
2569 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2570 {
2571 loc = (*loc2p);
2572 if (loc->owner->extra_string
2573 && is_breakpoint (loc->owner)
2574 && loc->pspace->num == bl->pspace->num
2575 && loc->owner->enable_state == bp_enabled
2576 && loc->enabled)
2577 /* Add the command to the vector. This will be used later
2578 to send the commands to the target. */
2579 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2580 loc->cmd_bytecode);
2581 }
2582
2583 bl->target_info.persist = 0;
2584 /* Maybe flag this location as persistent. */
2585 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2586 bl->target_info.persist = 1;
2587}
2588
35df4500
TJB
2589/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2590 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2591 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2592 Returns 0 for success, 1 if the bp_location type is not supported or
2593 -1 for failure.
879bfdc2 2594
4a64f543
MS
2595 NOTE drow/2003-09-09: This routine could be broken down to an
2596 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2597static int
35df4500 2598insert_bp_location (struct bp_location *bl,
26bb91f3 2599 struct ui_file *tmp_error_stream,
3fbb6ffa 2600 int *disabled_breaks,
dd61ec5c
MW
2601 int *hw_breakpoint_error,
2602 int *hw_bp_error_explained_already)
879bfdc2 2603{
0000e5cc
PA
2604 enum errors bp_err = GDB_NO_ERROR;
2605 const char *bp_err_message = NULL;
879bfdc2 2606
b775012e 2607 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2608 return 0;
2609
35c63cd8
JB
2610 /* Note we don't initialize bl->target_info, as that wipes out
2611 the breakpoint location's shadow_contents if the breakpoint
2612 is still inserted at that location. This in turn breaks
2613 target_read_memory which depends on these buffers when
2614 a memory read is requested at the breakpoint location:
2615 Once the target_info has been wiped, we fail to see that
2616 we have a breakpoint inserted at that address and thus
2617 read the breakpoint instead of returning the data saved in
2618 the breakpoint location's shadow contents. */
0d5ed153 2619 bl->target_info.reqstd_address = bl->address;
35df4500 2620 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2621 bl->target_info.length = bl->length;
8181d85f 2622
b775012e
LM
2623 /* When working with target-side conditions, we must pass all the conditions
2624 for the same breakpoint address down to the target since GDB will not
2625 insert those locations. With a list of breakpoint conditions, the target
2626 can decide when to stop and notify GDB. */
2627
2628 if (is_breakpoint (bl->owner))
2629 {
2630 build_target_condition_list (bl);
d3ce09f5
SS
2631 build_target_command_list (bl);
2632 /* Reset the modification marker. */
b775012e
LM
2633 bl->needs_update = 0;
2634 }
2635
35df4500
TJB
2636 if (bl->loc_type == bp_loc_software_breakpoint
2637 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2638 {
35df4500 2639 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2640 {
2641 /* If the explicitly specified breakpoint type
2642 is not hardware breakpoint, check the memory map to see
2643 if the breakpoint address is in read only memory or not.
4a64f543 2644
765dc015
VP
2645 Two important cases are:
2646 - location type is not hardware breakpoint, memory
2647 is readonly. We change the type of the location to
2648 hardware breakpoint.
4a64f543
MS
2649 - location type is hardware breakpoint, memory is
2650 read-write. This means we've previously made the
2651 location hardware one, but then the memory map changed,
2652 so we undo.
765dc015 2653
4a64f543
MS
2654 When breakpoints are removed, remove_breakpoints will use
2655 location types we've just set here, the only possible
2656 problem is that memory map has changed during running
2657 program, but it's not going to work anyway with current
2658 gdb. */
765dc015 2659 struct mem_region *mr
0d5ed153 2660 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2661
2662 if (mr)
2663 {
2664 if (automatic_hardware_breakpoints)
2665 {
765dc015
VP
2666 enum bp_loc_type new_type;
2667
2668 if (mr->attrib.mode != MEM_RW)
2669 new_type = bp_loc_hardware_breakpoint;
2670 else
2671 new_type = bp_loc_software_breakpoint;
2672
35df4500 2673 if (new_type != bl->loc_type)
765dc015
VP
2674 {
2675 static int said = 0;
cc59ec59 2676
35df4500 2677 bl->loc_type = new_type;
765dc015
VP
2678 if (!said)
2679 {
3e43a32a
MS
2680 fprintf_filtered (gdb_stdout,
2681 _("Note: automatically using "
2682 "hardware breakpoints for "
2683 "read-only addresses.\n"));
765dc015
VP
2684 said = 1;
2685 }
2686 }
2687 }
35df4500 2688 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2689 && mr->attrib.mode != MEM_RW)
2690 {
2691 fprintf_unfiltered (tmp_error_stream,
2692 _("Cannot insert breakpoint %d.\n"
2693 "Cannot set software breakpoint "
2694 "at read-only address %s\n"),
2695 bl->owner->number,
2696 paddress (bl->gdbarch, bl->address));
2697 return 1;
2698 }
765dc015
VP
2699 }
2700 }
2701
879bfdc2
DJ
2702 /* First check to see if we have to handle an overlay. */
2703 if (overlay_debugging == ovly_off
35df4500
TJB
2704 || bl->section == NULL
2705 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2706 {
2707 /* No overlay handling: just set the breakpoint. */
492d29ea 2708 TRY
dd61ec5c 2709 {
0000e5cc
PA
2710 int val;
2711
dd61ec5c 2712 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2713 if (val)
2714 bp_err = GENERIC_ERROR;
dd61ec5c 2715 }
492d29ea 2716 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2717 {
0000e5cc
PA
2718 bp_err = e.error;
2719 bp_err_message = e.message;
dd61ec5c 2720 }
492d29ea 2721 END_CATCH
879bfdc2
DJ
2722 }
2723 else
2724 {
4a64f543 2725 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2726 Shall we set a breakpoint at the LMA? */
2727 if (!overlay_events_enabled)
2728 {
2729 /* Yes -- overlay event support is not active,
2730 so we must try to set a breakpoint at the LMA.
2731 This will not work for a hardware breakpoint. */
35df4500 2732 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2733 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2734 bl->owner->number);
879bfdc2
DJ
2735 else
2736 {
35df4500
TJB
2737 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2738 bl->section);
879bfdc2 2739 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2740 bl->overlay_target_info = bl->target_info;
0d5ed153 2741 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2742
2743 /* No overlay handling: just set the breakpoint. */
492d29ea 2744 TRY
0000e5cc
PA
2745 {
2746 int val;
2747
2748 val = target_insert_breakpoint (bl->gdbarch,
2749 &bl->overlay_target_info);
2750 if (val)
2751 bp_err = GENERIC_ERROR;
2752 }
492d29ea 2753 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2754 {
2755 bp_err = e.error;
2756 bp_err_message = e.message;
2757 }
492d29ea 2758 END_CATCH
0000e5cc
PA
2759
2760 if (bp_err != GDB_NO_ERROR)
99361f52 2761 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2762 "Overlay breakpoint %d "
2763 "failed: in ROM?\n",
35df4500 2764 bl->owner->number);
879bfdc2
DJ
2765 }
2766 }
2767 /* Shall we set a breakpoint at the VMA? */
35df4500 2768 if (section_is_mapped (bl->section))
879bfdc2
DJ
2769 {
2770 /* Yes. This overlay section is mapped into memory. */
492d29ea 2771 TRY
dd61ec5c 2772 {
0000e5cc
PA
2773 int val;
2774
dd61ec5c 2775 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2776 if (val)
2777 bp_err = GENERIC_ERROR;
dd61ec5c 2778 }
492d29ea 2779 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2780 {
0000e5cc
PA
2781 bp_err = e.error;
2782 bp_err_message = e.message;
dd61ec5c 2783 }
492d29ea 2784 END_CATCH
879bfdc2
DJ
2785 }
2786 else
2787 {
2788 /* No. This breakpoint will not be inserted.
2789 No error, but do not mark the bp as 'inserted'. */
2790 return 0;
2791 }
2792 }
2793
0000e5cc 2794 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2795 {
2796 /* Can't set the breakpoint. */
0000e5cc
PA
2797
2798 /* In some cases, we might not be able to insert a
2799 breakpoint in a shared library that has already been
2800 removed, but we have not yet processed the shlib unload
2801 event. Unfortunately, some targets that implement
076855f9
PA
2802 breakpoint insertion themselves can't tell why the
2803 breakpoint insertion failed (e.g., the remote target
2804 doesn't define error codes), so we must treat generic
2805 errors as memory errors. */
0000e5cc 2806 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2807 && bl->loc_type == bp_loc_software_breakpoint
08351840 2808 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2809 || shared_objfile_contains_address_p (bl->pspace,
2810 bl->address)))
879bfdc2 2811 {
4a64f543 2812 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2813 bl->shlib_disabled = 1;
8d3788bd 2814 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2815 if (!*disabled_breaks)
2816 {
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert breakpoint %d.\n",
2819 bl->owner->number);
2820 fprintf_unfiltered (tmp_error_stream,
2821 "Temporarily disabling shared "
2822 "library breakpoints:\n");
2823 }
2824 *disabled_breaks = 1;
879bfdc2 2825 fprintf_unfiltered (tmp_error_stream,
35df4500 2826 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2827 return 0;
879bfdc2
DJ
2828 }
2829 else
879bfdc2 2830 {
35df4500 2831 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2832 {
0000e5cc
PA
2833 *hw_breakpoint_error = 1;
2834 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2835 fprintf_unfiltered (tmp_error_stream,
2836 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2837 bl->owner->number, bp_err_message ? ":" : ".\n");
2838 if (bp_err_message != NULL)
2839 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2840 }
2841 else
2842 {
0000e5cc
PA
2843 if (bp_err_message == NULL)
2844 {
2845 char *message
2846 = memory_error_message (TARGET_XFER_E_IO,
2847 bl->gdbarch, bl->address);
2848 struct cleanup *old_chain = make_cleanup (xfree, message);
2849
2850 fprintf_unfiltered (tmp_error_stream,
2851 "Cannot insert breakpoint %d.\n"
2852 "%s\n",
2853 bl->owner->number, message);
2854 do_cleanups (old_chain);
2855 }
2856 else
2857 {
2858 fprintf_unfiltered (tmp_error_stream,
2859 "Cannot insert breakpoint %d: %s\n",
2860 bl->owner->number,
2861 bp_err_message);
2862 }
879bfdc2 2863 }
0000e5cc 2864 return 1;
879bfdc2
DJ
2865
2866 }
2867 }
2868 else
35df4500 2869 bl->inserted = 1;
879bfdc2 2870
0000e5cc 2871 return 0;
879bfdc2
DJ
2872 }
2873
35df4500 2874 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2875 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2876 watchpoints. It's not clear that it's necessary... */
35df4500 2877 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2878 {
0000e5cc
PA
2879 int val;
2880
77b06cd7
TJB
2881 gdb_assert (bl->owner->ops != NULL
2882 && bl->owner->ops->insert_location != NULL);
2883
2884 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2885
2886 /* If trying to set a read-watchpoint, and it turns out it's not
2887 supported, try emulating one with an access watchpoint. */
35df4500 2888 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2889 {
2890 struct bp_location *loc, **loc_temp;
2891
2892 /* But don't try to insert it, if there's already another
2893 hw_access location that would be considered a duplicate
2894 of this one. */
2895 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2896 if (loc != bl
85d721b8 2897 && loc->watchpoint_type == hw_access
35df4500 2898 && watchpoint_locations_match (bl, loc))
85d721b8 2899 {
35df4500
TJB
2900 bl->duplicate = 1;
2901 bl->inserted = 1;
2902 bl->target_info = loc->target_info;
2903 bl->watchpoint_type = hw_access;
85d721b8
PA
2904 val = 0;
2905 break;
2906 }
2907
2908 if (val == 1)
2909 {
77b06cd7
TJB
2910 bl->watchpoint_type = hw_access;
2911 val = bl->owner->ops->insert_location (bl);
2912
2913 if (val)
2914 /* Back to the original value. */
2915 bl->watchpoint_type = hw_read;
85d721b8
PA
2916 }
2917 }
2918
35df4500 2919 bl->inserted = (val == 0);
879bfdc2
DJ
2920 }
2921
35df4500 2922 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2923 {
0000e5cc
PA
2924 int val;
2925
77b06cd7
TJB
2926 gdb_assert (bl->owner->ops != NULL
2927 && bl->owner->ops->insert_location != NULL);
2928
2929 val = bl->owner->ops->insert_location (bl);
2930 if (val)
2931 {
2932 bl->owner->enable_state = bp_disabled;
2933
2934 if (val == 1)
2935 warning (_("\
2936Error inserting catchpoint %d: Your system does not support this type\n\
2937of catchpoint."), bl->owner->number);
2938 else
2939 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2940 }
2941
2942 bl->inserted = (val == 0);
1640b821
DJ
2943
2944 /* We've already printed an error message if there was a problem
2945 inserting this catchpoint, and we've disabled the catchpoint,
2946 so just return success. */
2947 return 0;
879bfdc2
DJ
2948 }
2949
2950 return 0;
2951}
2952
6c95b8df
PA
2953/* This function is called when program space PSPACE is about to be
2954 deleted. It takes care of updating breakpoints to not reference
2955 PSPACE anymore. */
2956
2957void
2958breakpoint_program_space_exit (struct program_space *pspace)
2959{
2960 struct breakpoint *b, *b_temp;
876fa593 2961 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2962
2963 /* Remove any breakpoint that was set through this program space. */
2964 ALL_BREAKPOINTS_SAFE (b, b_temp)
2965 {
2966 if (b->pspace == pspace)
2967 delete_breakpoint (b);
2968 }
2969
2970 /* Breakpoints set through other program spaces could have locations
2971 bound to PSPACE as well. Remove those. */
876fa593 2972 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2973 {
2974 struct bp_location *tmp;
2975
2976 if (loc->pspace == pspace)
2977 {
2bdf28a0 2978 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2979 if (loc->owner->loc == loc)
2980 loc->owner->loc = loc->next;
2981 else
2982 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2983 if (tmp->next == loc)
2984 {
2985 tmp->next = loc->next;
2986 break;
2987 }
2988 }
2989 }
2990
2991 /* Now update the global location list to permanently delete the
2992 removed locations above. */
44702360 2993 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2994}
2995
74960c60
VP
2996/* Make sure all breakpoints are inserted in inferior.
2997 Throws exception on any error.
2998 A breakpoint that is already inserted won't be inserted
2999 again, so calling this function twice is safe. */
3000void
3001insert_breakpoints (void)
3002{
3003 struct breakpoint *bpt;
3004
3005 ALL_BREAKPOINTS (bpt)
3006 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3007 {
3008 struct watchpoint *w = (struct watchpoint *) bpt;
3009
3010 update_watchpoint (w, 0 /* don't reparse. */);
3011 }
74960c60 3012
04086b45
PA
3013 /* Updating watchpoints creates new locations, so update the global
3014 location list. Explicitly tell ugll to insert locations and
3015 ignore breakpoints_always_inserted_mode. */
3016 update_global_location_list (UGLL_INSERT);
74960c60
VP
3017}
3018
20388dd6
YQ
3019/* Invoke CALLBACK for each of bp_location. */
3020
3021void
3022iterate_over_bp_locations (walk_bp_location_callback callback)
3023{
3024 struct bp_location *loc, **loc_tmp;
3025
3026 ALL_BP_LOCATIONS (loc, loc_tmp)
3027 {
3028 callback (loc, NULL);
3029 }
3030}
3031
b775012e
LM
3032/* This is used when we need to synch breakpoint conditions between GDB and the
3033 target. It is the case with deleting and disabling of breakpoints when using
3034 always-inserted mode. */
3035
3036static void
3037update_inserted_breakpoint_locations (void)
3038{
3039 struct bp_location *bl, **blp_tmp;
3040 int error_flag = 0;
3041 int val = 0;
3042 int disabled_breaks = 0;
3043 int hw_breakpoint_error = 0;
dd61ec5c 3044 int hw_bp_details_reported = 0;
b775012e
LM
3045
3046 struct ui_file *tmp_error_stream = mem_fileopen ();
3047 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3048
3049 /* Explicitly mark the warning -- this will only be printed if
3050 there was an error. */
3051 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3052
3053 save_current_space_and_thread ();
3054
3055 ALL_BP_LOCATIONS (bl, blp_tmp)
3056 {
3057 /* We only want to update software breakpoints and hardware
3058 breakpoints. */
3059 if (!is_breakpoint (bl->owner))
3060 continue;
3061
3062 /* We only want to update locations that are already inserted
3063 and need updating. This is to avoid unwanted insertion during
3064 deletion of breakpoints. */
3065 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3066 continue;
3067
3068 switch_to_program_space_and_thread (bl->pspace);
3069
3070 /* For targets that support global breakpoints, there's no need
3071 to select an inferior to insert breakpoint to. In fact, even
3072 if we aren't attached to any process yet, we should still
3073 insert breakpoints. */
f5656ead 3074 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3075 && ptid_equal (inferior_ptid, null_ptid))
3076 continue;
3077
3078 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3079 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3080 if (val)
3081 error_flag = val;
3082 }
3083
3084 if (error_flag)
3085 {
3086 target_terminal_ours_for_output ();
3087 error_stream (tmp_error_stream);
3088 }
3089
3090 do_cleanups (cleanups);
3091}
3092
c30eee59 3093/* Used when starting or continuing the program. */
c906108c 3094
74960c60
VP
3095static void
3096insert_breakpoint_locations (void)
c906108c 3097{
a5606eee 3098 struct breakpoint *bpt;
35df4500 3099 struct bp_location *bl, **blp_tmp;
eacd795a 3100 int error_flag = 0;
c906108c 3101 int val = 0;
3fbb6ffa 3102 int disabled_breaks = 0;
81d0cc19 3103 int hw_breakpoint_error = 0;
dd61ec5c 3104 int hw_bp_error_explained_already = 0;
c906108c 3105
81d0cc19 3106 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3107 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3108
81d0cc19
GS
3109 /* Explicitly mark the warning -- this will only be printed if
3110 there was an error. */
3111 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3112
3113 save_current_space_and_thread ();
3114
35df4500 3115 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3116 {
b775012e 3117 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3118 continue;
3119
4a64f543
MS
3120 /* There is no point inserting thread-specific breakpoints if
3121 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3122 has BL->OWNER always non-NULL. */
35df4500
TJB
3123 if (bl->owner->thread != -1
3124 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3125 continue;
3126
35df4500 3127 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3128
3129 /* For targets that support global breakpoints, there's no need
3130 to select an inferior to insert breakpoint to. In fact, even
3131 if we aren't attached to any process yet, we should still
3132 insert breakpoints. */
f5656ead 3133 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3134 && ptid_equal (inferior_ptid, null_ptid))
3135 continue;
3136
3fbb6ffa 3137 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3138 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3139 if (val)
eacd795a 3140 error_flag = val;
879bfdc2 3141 }
c906108c 3142
4a64f543
MS
3143 /* If we failed to insert all locations of a watchpoint, remove
3144 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3145 ALL_BREAKPOINTS (bpt)
3146 {
3147 int some_failed = 0;
3148 struct bp_location *loc;
3149
3150 if (!is_hardware_watchpoint (bpt))
3151 continue;
3152
d6b74ac4 3153 if (!breakpoint_enabled (bpt))
a5606eee 3154 continue;
74960c60
VP
3155
3156 if (bpt->disposition == disp_del_at_next_stop)
3157 continue;
a5606eee
VP
3158
3159 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3160 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3161 {
3162 some_failed = 1;
3163 break;
3164 }
3165 if (some_failed)
3166 {
3167 for (loc = bpt->loc; loc; loc = loc->next)
3168 if (loc->inserted)
3169 remove_breakpoint (loc, mark_uninserted);
3170
3171 hw_breakpoint_error = 1;
3172 fprintf_unfiltered (tmp_error_stream,
3173 "Could not insert hardware watchpoint %d.\n",
3174 bpt->number);
eacd795a 3175 error_flag = -1;
a5606eee
VP
3176 }
3177 }
3178
eacd795a 3179 if (error_flag)
81d0cc19
GS
3180 {
3181 /* If a hardware breakpoint or watchpoint was inserted, add a
3182 message about possibly exhausted resources. */
dd61ec5c 3183 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3184 {
c6510018
MS
3185 fprintf_unfiltered (tmp_error_stream,
3186 "Could not insert hardware breakpoints:\n\
3187You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3188 }
81d0cc19
GS
3189 target_terminal_ours_for_output ();
3190 error_stream (tmp_error_stream);
3191 }
f7545552
TT
3192
3193 do_cleanups (cleanups);
c906108c
SS
3194}
3195
c30eee59
TJB
3196/* Used when the program stops.
3197 Returns zero if successful, or non-zero if there was a problem
3198 removing a breakpoint location. */
3199
c906108c 3200int
fba45db2 3201remove_breakpoints (void)
c906108c 3202{
35df4500 3203 struct bp_location *bl, **blp_tmp;
3a1bae8e 3204 int val = 0;
c906108c 3205
35df4500 3206 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3207 {
1e4d1764 3208 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3209 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3210 }
3a1bae8e 3211 return val;
c906108c
SS
3212}
3213
49fa26b0
PA
3214/* When a thread exits, remove breakpoints that are related to
3215 that thread. */
3216
3217static void
3218remove_threaded_breakpoints (struct thread_info *tp, int silent)
3219{
3220 struct breakpoint *b, *b_tmp;
3221
3222 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3223 {
96181529 3224 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3225 {
3226 b->disposition = disp_del_at_next_stop;
3227
3228 printf_filtered (_("\
46ecd527 3229Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3230 b->number, tp->num);
3231
3232 /* Hide it from the user. */
3233 b->number = 0;
3234 }
3235 }
3236}
3237
6c95b8df
PA
3238/* Remove breakpoints of process PID. */
3239
3240int
3241remove_breakpoints_pid (int pid)
3242{
35df4500 3243 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3244 int val;
3245 struct inferior *inf = find_inferior_pid (pid);
3246
35df4500 3247 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3248 {
35df4500 3249 if (bl->pspace != inf->pspace)
6c95b8df
PA
3250 continue;
3251
fc126975 3252 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3253 {
35df4500 3254 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3255 if (val != 0)
3256 return val;
3257 }
3258 }
3259 return 0;
3260}
3261
c906108c 3262int
fba45db2 3263reattach_breakpoints (int pid)
c906108c 3264{
6c95b8df 3265 struct cleanup *old_chain;
35df4500 3266 struct bp_location *bl, **blp_tmp;
c906108c 3267 int val;
86b887df 3268 struct ui_file *tmp_error_stream;
dd61ec5c 3269 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3270 struct inferior *inf;
3271 struct thread_info *tp;
3272
3273 tp = any_live_thread_of_process (pid);
3274 if (tp == NULL)
3275 return 1;
3276
3277 inf = find_inferior_pid (pid);
3278 old_chain = save_inferior_ptid ();
3279
3280 inferior_ptid = tp->ptid;
a4954f26 3281
86b887df 3282 tmp_error_stream = mem_fileopen ();
a4954f26 3283 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3284
35df4500 3285 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3286 {
35df4500 3287 if (bl->pspace != inf->pspace)
6c95b8df
PA
3288 continue;
3289
35df4500 3290 if (bl->inserted)
c5aa993b 3291 {
35df4500 3292 bl->inserted = 0;
dd61ec5c 3293 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3294 if (val != 0)
3295 {
ce696e05 3296 do_cleanups (old_chain);
c5aa993b
JM
3297 return val;
3298 }
3299 }
3300 }
ce696e05 3301 do_cleanups (old_chain);
c906108c
SS
3302 return 0;
3303}
3304
e58b0e63
PA
3305static int internal_breakpoint_number = -1;
3306
84f4c1fe
PM
3307/* Set the breakpoint number of B, depending on the value of INTERNAL.
3308 If INTERNAL is non-zero, the breakpoint number will be populated
3309 from internal_breakpoint_number and that variable decremented.
e5dd4106 3310 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3311 breakpoint_count and that value incremented. Internal breakpoints
3312 do not set the internal var bpnum. */
3313static void
3314set_breakpoint_number (int internal, struct breakpoint *b)
3315{
3316 if (internal)
3317 b->number = internal_breakpoint_number--;
3318 else
3319 {
3320 set_breakpoint_count (breakpoint_count + 1);
3321 b->number = breakpoint_count;
3322 }
3323}
3324
e62c965a 3325static struct breakpoint *
a6d9a66e 3326create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3327 CORE_ADDR address, enum bptype type,
c0a91b2b 3328 const struct breakpoint_ops *ops)
e62c965a 3329{
e62c965a
PP
3330 struct symtab_and_line sal;
3331 struct breakpoint *b;
3332
4a64f543 3333 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3334
3335 sal.pc = address;
3336 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3337 sal.pspace = current_program_space;
e62c965a 3338
06edf0c0 3339 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3340 b->number = internal_breakpoint_number--;
3341 b->disposition = disp_donttouch;
3342
3343 return b;
3344}
3345
17450429
PP
3346static const char *const longjmp_names[] =
3347 {
3348 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3349 };
3350#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3351
3352/* Per-objfile data private to breakpoint.c. */
3353struct breakpoint_objfile_data
3354{
3355 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3356 struct bound_minimal_symbol overlay_msym;
17450429
PP
3357
3358 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3359 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3360
28106bc2
SDJ
3361 /* True if we have looked for longjmp probes. */
3362 int longjmp_searched;
3363
3364 /* SystemTap probe points for longjmp (if any). */
3365 VEC (probe_p) *longjmp_probes;
3366
17450429 3367 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3368 struct bound_minimal_symbol terminate_msym;
17450429
PP
3369
3370 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3371 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3372
3373 /* True if we have looked for exception probes. */
3374 int exception_searched;
3375
3376 /* SystemTap probe points for unwinding (if any). */
3377 VEC (probe_p) *exception_probes;
17450429
PP
3378};
3379
3380static const struct objfile_data *breakpoint_objfile_key;
3381
3382/* Minimal symbol not found sentinel. */
3383static struct minimal_symbol msym_not_found;
3384
3385/* Returns TRUE if MSYM point to the "not found" sentinel. */
3386
3387static int
3388msym_not_found_p (const struct minimal_symbol *msym)
3389{
3390 return msym == &msym_not_found;
3391}
3392
3393/* Return per-objfile data needed by breakpoint.c.
3394 Allocate the data if necessary. */
3395
3396static struct breakpoint_objfile_data *
3397get_breakpoint_objfile_data (struct objfile *objfile)
3398{
3399 struct breakpoint_objfile_data *bp_objfile_data;
3400
9a3c8263
SM
3401 bp_objfile_data = ((struct breakpoint_objfile_data *)
3402 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3403 if (bp_objfile_data == NULL)
3404 {
8d749320
SM
3405 bp_objfile_data =
3406 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3407
3408 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3409 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3410 }
3411 return bp_objfile_data;
3412}
3413
28106bc2
SDJ
3414static void
3415free_breakpoint_probes (struct objfile *obj, void *data)
3416{
9a3c8263
SM
3417 struct breakpoint_objfile_data *bp_objfile_data
3418 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3419
3420 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3421 VEC_free (probe_p, bp_objfile_data->exception_probes);
3422}
3423
e62c965a 3424static void
af02033e 3425create_overlay_event_breakpoint (void)
e62c965a 3426{
69de3c6a 3427 struct objfile *objfile;
af02033e 3428 const char *const func_name = "_ovly_debug_event";
e62c965a 3429
69de3c6a
PP
3430 ALL_OBJFILES (objfile)
3431 {
3432 struct breakpoint *b;
17450429
PP
3433 struct breakpoint_objfile_data *bp_objfile_data;
3434 CORE_ADDR addr;
67994074 3435 struct explicit_location explicit_loc;
69de3c6a 3436
17450429
PP
3437 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3438
3b7344d5 3439 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3440 continue;
3441
3b7344d5 3442 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3443 {
3b7344d5 3444 struct bound_minimal_symbol m;
17450429
PP
3445
3446 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3447 if (m.minsym == NULL)
17450429
PP
3448 {
3449 /* Avoid future lookups in this objfile. */
3b7344d5 3450 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3451 continue;
3452 }
3453 bp_objfile_data->overlay_msym = m;
3454 }
e62c965a 3455
77e371c0 3456 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3457 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3458 bp_overlay_event,
3459 &internal_breakpoint_ops);
67994074
KS
3460 initialize_explicit_location (&explicit_loc);
3461 explicit_loc.function_name = ASTRDUP (func_name);
3462 b->location = new_explicit_location (&explicit_loc);
e62c965a 3463
69de3c6a
PP
3464 if (overlay_debugging == ovly_auto)
3465 {
3466 b->enable_state = bp_enabled;
3467 overlay_events_enabled = 1;
3468 }
3469 else
3470 {
3471 b->enable_state = bp_disabled;
3472 overlay_events_enabled = 0;
3473 }
e62c965a 3474 }
44702360 3475 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3476}
3477
0fd8e87f 3478static void
af02033e 3479create_longjmp_master_breakpoint (void)
0fd8e87f 3480{
6c95b8df 3481 struct program_space *pspace;
6c95b8df
PA
3482 struct cleanup *old_chain;
3483
3484 old_chain = save_current_program_space ();
0fd8e87f 3485
6c95b8df 3486 ALL_PSPACES (pspace)
af02033e
PP
3487 {
3488 struct objfile *objfile;
3489
3490 set_current_program_space (pspace);
3491
3492 ALL_OBJFILES (objfile)
0fd8e87f 3493 {
af02033e
PP
3494 int i;
3495 struct gdbarch *gdbarch;
17450429 3496 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3497
af02033e 3498 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3499
17450429
PP
3500 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3501
28106bc2
SDJ
3502 if (!bp_objfile_data->longjmp_searched)
3503 {
25f9533e
SDJ
3504 VEC (probe_p) *ret;
3505
3506 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3507 if (ret != NULL)
3508 {
3509 /* We are only interested in checking one element. */
3510 struct probe *p = VEC_index (probe_p, ret, 0);
3511
3512 if (!can_evaluate_probe_arguments (p))
3513 {
3514 /* We cannot use the probe interface here, because it does
3515 not know how to evaluate arguments. */
3516 VEC_free (probe_p, ret);
3517 ret = NULL;
3518 }
3519 }
3520 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3521 bp_objfile_data->longjmp_searched = 1;
3522 }
3523
3524 if (bp_objfile_data->longjmp_probes != NULL)
3525 {
3526 int i;
3527 struct probe *probe;
3528 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3529
3530 for (i = 0;
3531 VEC_iterate (probe_p,
3532 bp_objfile_data->longjmp_probes,
3533 i, probe);
3534 ++i)
3535 {
3536 struct breakpoint *b;
3537
729662a5
TT
3538 b = create_internal_breakpoint (gdbarch,
3539 get_probe_address (probe,
3540 objfile),
28106bc2
SDJ
3541 bp_longjmp_master,
3542 &internal_breakpoint_ops);
5b56227b
KS
3543 b->location
3544 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3545 b->enable_state = bp_disabled;
3546 }
3547
3548 continue;
3549 }
3550
0569175e
TSD
3551 if (!gdbarch_get_longjmp_target_p (gdbarch))
3552 continue;
3553
17450429 3554 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3555 {
3556 struct breakpoint *b;
af02033e 3557 const char *func_name;
17450429 3558 CORE_ADDR addr;
67994074 3559 struct explicit_location explicit_loc;
6c95b8df 3560
3b7344d5 3561 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3562 continue;
0fd8e87f 3563
17450429 3564 func_name = longjmp_names[i];
3b7344d5 3565 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3566 {
3b7344d5 3567 struct bound_minimal_symbol m;
17450429
PP
3568
3569 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3570 if (m.minsym == NULL)
17450429
PP
3571 {
3572 /* Prevent future lookups in this objfile. */
3b7344d5 3573 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3574 continue;
3575 }
3576 bp_objfile_data->longjmp_msym[i] = m;
3577 }
3578
77e371c0 3579 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3580 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3581 &internal_breakpoint_ops);
67994074
KS
3582 initialize_explicit_location (&explicit_loc);
3583 explicit_loc.function_name = ASTRDUP (func_name);
3584 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3585 b->enable_state = bp_disabled;
3586 }
0fd8e87f 3587 }
af02033e 3588 }
44702360 3589 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3590
3591 do_cleanups (old_chain);
0fd8e87f
UW
3592}
3593
af02033e 3594/* Create a master std::terminate breakpoint. */
aa7d318d 3595static void
af02033e 3596create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3597{
3598 struct program_space *pspace;
aa7d318d 3599 struct cleanup *old_chain;
af02033e 3600 const char *const func_name = "std::terminate()";
aa7d318d
TT
3601
3602 old_chain = save_current_program_space ();
3603
3604 ALL_PSPACES (pspace)
17450429
PP
3605 {
3606 struct objfile *objfile;
3607 CORE_ADDR addr;
3608
3609 set_current_program_space (pspace);
3610
aa7d318d
TT
3611 ALL_OBJFILES (objfile)
3612 {
3613 struct breakpoint *b;
17450429 3614 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3615 struct explicit_location explicit_loc;
aa7d318d 3616
17450429 3617 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3618
3b7344d5 3619 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3620 continue;
3621
3b7344d5 3622 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3623 {
3b7344d5 3624 struct bound_minimal_symbol m;
17450429
PP
3625
3626 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3627 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3628 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3629 {
3630 /* Prevent future lookups in this objfile. */
3b7344d5 3631 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3632 continue;
3633 }
3634 bp_objfile_data->terminate_msym = m;
3635 }
aa7d318d 3636
77e371c0 3637 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3638 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3639 bp_std_terminate_master,
3640 &internal_breakpoint_ops);
67994074
KS
3641 initialize_explicit_location (&explicit_loc);
3642 explicit_loc.function_name = ASTRDUP (func_name);
3643 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3644 b->enable_state = bp_disabled;
3645 }
17450429
PP
3646 }
3647
44702360 3648 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3649
3650 do_cleanups (old_chain);
3651}
3652
186c406b
TT
3653/* Install a master breakpoint on the unwinder's debug hook. */
3654
70221824 3655static void
186c406b
TT
3656create_exception_master_breakpoint (void)
3657{
3658 struct objfile *objfile;
17450429 3659 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3660
3661 ALL_OBJFILES (objfile)
3662 {
17450429
PP
3663 struct breakpoint *b;
3664 struct gdbarch *gdbarch;
3665 struct breakpoint_objfile_data *bp_objfile_data;
3666 CORE_ADDR addr;
67994074 3667 struct explicit_location explicit_loc;
17450429
PP
3668
3669 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3670
28106bc2
SDJ
3671 /* We prefer the SystemTap probe point if it exists. */
3672 if (!bp_objfile_data->exception_searched)
3673 {
25f9533e
SDJ
3674 VEC (probe_p) *ret;
3675
3676 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3677
3678 if (ret != NULL)
3679 {
3680 /* We are only interested in checking one element. */
3681 struct probe *p = VEC_index (probe_p, ret, 0);
3682
3683 if (!can_evaluate_probe_arguments (p))
3684 {
3685 /* We cannot use the probe interface here, because it does
3686 not know how to evaluate arguments. */
3687 VEC_free (probe_p, ret);
3688 ret = NULL;
3689 }
3690 }
3691 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3692 bp_objfile_data->exception_searched = 1;
3693 }
3694
3695 if (bp_objfile_data->exception_probes != NULL)
3696 {
3697 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3698 int i;
3699 struct probe *probe;
3700
3701 for (i = 0;
3702 VEC_iterate (probe_p,
3703 bp_objfile_data->exception_probes,
3704 i, probe);
3705 ++i)
3706 {
3707 struct breakpoint *b;
3708
729662a5
TT
3709 b = create_internal_breakpoint (gdbarch,
3710 get_probe_address (probe,
3711 objfile),
28106bc2
SDJ
3712 bp_exception_master,
3713 &internal_breakpoint_ops);
5b56227b
KS
3714 b->location
3715 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3716 b->enable_state = bp_disabled;
3717 }
3718
3719 continue;
3720 }
3721
3722 /* Otherwise, try the hook function. */
3723
3b7344d5 3724 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3725 continue;
3726
3727 gdbarch = get_objfile_arch (objfile);
186c406b 3728
3b7344d5 3729 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3730 {
3b7344d5 3731 struct bound_minimal_symbol debug_hook;
186c406b 3732
17450429 3733 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3734 if (debug_hook.minsym == NULL)
17450429 3735 {
3b7344d5 3736 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3737 continue;
3738 }
3739
3740 bp_objfile_data->exception_msym = debug_hook;
186c406b 3741 }
17450429 3742
77e371c0 3743 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3744 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3745 &current_target);
06edf0c0
PA
3746 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3747 &internal_breakpoint_ops);
67994074
KS
3748 initialize_explicit_location (&explicit_loc);
3749 explicit_loc.function_name = ASTRDUP (func_name);
3750 b->location = new_explicit_location (&explicit_loc);
17450429 3751 b->enable_state = bp_disabled;
186c406b
TT
3752 }
3753
44702360 3754 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3755}
3756
c906108c 3757void
fba45db2 3758update_breakpoints_after_exec (void)
c906108c 3759{
35df4500 3760 struct breakpoint *b, *b_tmp;
876fa593 3761 struct bp_location *bploc, **bplocp_tmp;
c906108c 3762
25b22b0a
PA
3763 /* We're about to delete breakpoints from GDB's lists. If the
3764 INSERTED flag is true, GDB will try to lift the breakpoints by
3765 writing the breakpoints' "shadow contents" back into memory. The
3766 "shadow contents" are NOT valid after an exec, so GDB should not
3767 do that. Instead, the target is responsible from marking
3768 breakpoints out as soon as it detects an exec. We don't do that
3769 here instead, because there may be other attempts to delete
3770 breakpoints after detecting an exec and before reaching here. */
876fa593 3771 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3772 if (bploc->pspace == current_program_space)
3773 gdb_assert (!bploc->inserted);
c906108c 3774
35df4500 3775 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3776 {
6c95b8df
PA
3777 if (b->pspace != current_program_space)
3778 continue;
3779
4a64f543 3780 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3781 if (b->type == bp_shlib_event)
3782 {
3783 delete_breakpoint (b);
3784 continue;
3785 }
c906108c 3786
4a64f543 3787 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3788 if (b->type == bp_jit_event)
3789 {
3790 delete_breakpoint (b);
3791 continue;
3792 }
3793
1900040c 3794 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3795 as must overlay event and longjmp master breakpoints. */
3796 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3797 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3798 || b->type == bp_exception_master)
c4093a6a
JM
3799 {
3800 delete_breakpoint (b);
3801 continue;
3802 }
3803
4a64f543 3804 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3805 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3806 {
3807 delete_breakpoint (b);
3808 continue;
3809 }
3810
7c16b83e
PA
3811 /* Just like single-step breakpoints. */
3812 if (b->type == bp_single_step)
3813 {
3814 delete_breakpoint (b);
3815 continue;
3816 }
3817
611c83ae
PA
3818 /* Longjmp and longjmp-resume breakpoints are also meaningless
3819 after an exec. */
186c406b 3820 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3821 || b->type == bp_longjmp_call_dummy
186c406b 3822 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
3827
ce78b96d
JB
3828 if (b->type == bp_catchpoint)
3829 {
3830 /* For now, none of the bp_catchpoint breakpoints need to
3831 do anything at this point. In the future, if some of
3832 the catchpoints need to something, we will need to add
3833 a new method, and call this method from here. */
3834 continue;
3835 }
3836
c5aa993b
JM
3837 /* bp_finish is a special case. The only way we ought to be able
3838 to see one of these when an exec() has happened, is if the user
3839 caught a vfork, and then said "finish". Ordinarily a finish just
3840 carries them to the call-site of the current callee, by setting
3841 a temporary bp there and resuming. But in this case, the finish
3842 will carry them entirely through the vfork & exec.
3843
3844 We don't want to allow a bp_finish to remain inserted now. But
3845 we can't safely delete it, 'cause finish_command has a handle to
3846 the bp on a bpstat, and will later want to delete it. There's a
3847 chance (and I've seen it happen) that if we delete the bp_finish
3848 here, that its storage will get reused by the time finish_command
3849 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3850 We really must allow finish_command to delete a bp_finish.
3851
e5dd4106 3852 In the absence of a general solution for the "how do we know
53a5351d
JM
3853 it's safe to delete something others may have handles to?"
3854 problem, what we'll do here is just uninsert the bp_finish, and
3855 let finish_command delete it.
3856
3857 (We know the bp_finish is "doomed" in the sense that it's
3858 momentary, and will be deleted as soon as finish_command sees
3859 the inferior stopped. So it doesn't matter that the bp's
3860 address is probably bogus in the new a.out, unlike e.g., the
3861 solib breakpoints.) */
c5aa993b 3862
c5aa993b
JM
3863 if (b->type == bp_finish)
3864 {
3865 continue;
3866 }
3867
3868 /* Without a symbolic address, we have little hope of the
3869 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3870 a.out. */
f00aae0f 3871 if (event_location_empty_p (b->location))
c5aa993b
JM
3872 {
3873 delete_breakpoint (b);
3874 continue;
3875 }
c5aa993b 3876 }
c906108c
SS
3877}
3878
3879int
d80ee84f 3880detach_breakpoints (ptid_t ptid)
c906108c 3881{
35df4500 3882 struct bp_location *bl, **blp_tmp;
3a1bae8e 3883 int val = 0;
ce696e05 3884 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3885 struct inferior *inf = current_inferior ();
c5aa993b 3886
dfd4cc63 3887 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3888 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3889
6c95b8df 3890 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3891 inferior_ptid = ptid;
35df4500 3892 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3893 {
35df4500 3894 if (bl->pspace != inf->pspace)
6c95b8df
PA
3895 continue;
3896
bd9673a4
PW
3897 /* This function must physically remove breakpoints locations
3898 from the specified ptid, without modifying the breakpoint
3899 package's state. Locations of type bp_loc_other are only
3900 maintained at GDB side. So, there is no need to remove
3901 these bp_loc_other locations. Moreover, removing these
3902 would modify the breakpoint package's state. */
3903 if (bl->loc_type == bp_loc_other)
3904 continue;
3905
35df4500
TJB
3906 if (bl->inserted)
3907 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3908 }
d03285ec 3909
ce696e05 3910 do_cleanups (old_chain);
3a1bae8e 3911 return val;
c906108c
SS
3912}
3913
35df4500 3914/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3915 Note that this is used to detach breakpoints from a child fork.
3916 When we get here, the child isn't in the inferior list, and neither
3917 do we have objects to represent its address space --- we should
35df4500 3918 *not* look at bl->pspace->aspace here. */
6c95b8df 3919
c906108c 3920static int
35df4500 3921remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3922{
3923 int val;
c5aa993b 3924
35df4500
TJB
3925 /* BL is never in moribund_locations by our callers. */
3926 gdb_assert (bl->owner != NULL);
2bdf28a0 3927
74960c60
VP
3928 /* The type of none suggests that owner is actually deleted.
3929 This should not ever happen. */
35df4500 3930 gdb_assert (bl->owner->type != bp_none);
0bde7532 3931
35df4500
TJB
3932 if (bl->loc_type == bp_loc_software_breakpoint
3933 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3934 {
c02f5703
MS
3935 /* "Normal" instruction breakpoint: either the standard
3936 trap-instruction bp (bp_breakpoint), or a
3937 bp_hardware_breakpoint. */
3938
3939 /* First check to see if we have to handle an overlay. */
3940 if (overlay_debugging == ovly_off
35df4500
TJB
3941 || bl->section == NULL
3942 || !(section_is_overlay (bl->section)))
c02f5703
MS
3943 {
3944 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3945
3946 /* If we're trying to uninsert a memory breakpoint that we
3947 know is set in a dynamic object that is marked
3948 shlib_disabled, then either the dynamic object was
3949 removed with "remove-symbol-file" or with
3950 "nosharedlibrary". In the former case, we don't know
3951 whether another dynamic object might have loaded over the
3952 breakpoint's address -- the user might well let us know
3953 about it next with add-symbol-file (the whole point of
d03de421 3954 add-symbol-file is letting the user manually maintain a
08351840
PA
3955 list of dynamically loaded objects). If we have the
3956 breakpoint's shadow memory, that is, this is a software
3957 breakpoint managed by GDB, check whether the breakpoint
3958 is still inserted in memory, to avoid overwriting wrong
3959 code with stale saved shadow contents. Note that HW
3960 breakpoints don't have shadow memory, as they're
3961 implemented using a mechanism that is not dependent on
3962 being able to modify the target's memory, and as such
3963 they should always be removed. */
3964 if (bl->shlib_disabled
3965 && bl->target_info.shadow_len != 0
3966 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3967 val = 0;
3968 else
3969 val = bl->owner->ops->remove_location (bl);
c02f5703 3970 }
c906108c
SS
3971 else
3972 {
4a64f543 3973 /* This breakpoint is in an overlay section.
c02f5703
MS
3974 Did we set a breakpoint at the LMA? */
3975 if (!overlay_events_enabled)
3976 {
3977 /* Yes -- overlay event support is not active, so we
3978 should have set a breakpoint at the LMA. Remove it.
3979 */
c02f5703
MS
3980 /* Ignore any failures: if the LMA is in ROM, we will
3981 have already warned when we failed to insert it. */
35df4500
TJB
3982 if (bl->loc_type == bp_loc_hardware_breakpoint)
3983 target_remove_hw_breakpoint (bl->gdbarch,
3984 &bl->overlay_target_info);
c02f5703 3985 else
35df4500
TJB
3986 target_remove_breakpoint (bl->gdbarch,
3987 &bl->overlay_target_info);
c02f5703
MS
3988 }
3989 /* Did we set a breakpoint at the VMA?
3990 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3991 if (bl->inserted)
c906108c 3992 {
c02f5703
MS
3993 /* Yes -- remove it. Previously we did not bother to
3994 remove the breakpoint if the section had been
3995 unmapped, but let's not rely on that being safe. We
3996 don't know what the overlay manager might do. */
aa67235e
UW
3997
3998 /* However, we should remove *software* breakpoints only
3999 if the section is still mapped, or else we overwrite
4000 wrong code with the saved shadow contents. */
348d480f
PA
4001 if (bl->loc_type == bp_loc_hardware_breakpoint
4002 || section_is_mapped (bl->section))
4003 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
4004 else
4005 val = 0;
c906108c 4006 }
c02f5703
MS
4007 else
4008 {
4009 /* No -- not inserted, so no need to remove. No error. */
4010 val = 0;
4011 }
c906108c 4012 }
879d1e6b 4013
08351840
PA
4014 /* In some cases, we might not be able to remove a breakpoint in
4015 a shared library that has already been removed, but we have
4016 not yet processed the shlib unload event. Similarly for an
4017 unloaded add-symbol-file object - the user might not yet have
4018 had the chance to remove-symbol-file it. shlib_disabled will
4019 be set if the library/object has already been removed, but
4020 the breakpoint hasn't been uninserted yet, e.g., after
4021 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4022 always-inserted mode. */
076855f9 4023 if (val
08351840
PA
4024 && (bl->loc_type == bp_loc_software_breakpoint
4025 && (bl->shlib_disabled
4026 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4027 || shared_objfile_contains_address_p (bl->pspace,
4028 bl->address))))
879d1e6b
UW
4029 val = 0;
4030
c906108c
SS
4031 if (val)
4032 return val;
35df4500 4033 bl->inserted = (is == mark_inserted);
c906108c 4034 }
35df4500 4035 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4036 {
77b06cd7
TJB
4037 gdb_assert (bl->owner->ops != NULL
4038 && bl->owner->ops->remove_location != NULL);
4039
35df4500 4040 bl->inserted = (is == mark_inserted);
77b06cd7 4041 bl->owner->ops->remove_location (bl);
2e70b7b9 4042
c906108c 4043 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4044 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4045 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4046 bl->owner->number);
c906108c 4047 }
35df4500
TJB
4048 else if (bl->owner->type == bp_catchpoint
4049 && breakpoint_enabled (bl->owner)
4050 && !bl->duplicate)
ce78b96d 4051 {
77b06cd7
TJB
4052 gdb_assert (bl->owner->ops != NULL
4053 && bl->owner->ops->remove_location != NULL);
ce78b96d 4054
77b06cd7 4055 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4056 if (val)
4057 return val;
77b06cd7 4058
35df4500 4059 bl->inserted = (is == mark_inserted);
ce78b96d 4060 }
c906108c
SS
4061
4062 return 0;
4063}
4064
6c95b8df 4065static int
35df4500 4066remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4067{
4068 int ret;
4069 struct cleanup *old_chain;
4070
35df4500
TJB
4071 /* BL is never in moribund_locations by our callers. */
4072 gdb_assert (bl->owner != NULL);
2bdf28a0 4073
6c95b8df
PA
4074 /* The type of none suggests that owner is actually deleted.
4075 This should not ever happen. */
35df4500 4076 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4077
4078 old_chain = save_current_space_and_thread ();
4079
35df4500 4080 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4081
35df4500 4082 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4083
4084 do_cleanups (old_chain);
4085 return ret;
4086}
4087
c906108c
SS
4088/* Clear the "inserted" flag in all breakpoints. */
4089
25b22b0a 4090void
fba45db2 4091mark_breakpoints_out (void)
c906108c 4092{
35df4500 4093 struct bp_location *bl, **blp_tmp;
c906108c 4094
35df4500 4095 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4096 if (bl->pspace == current_program_space)
35df4500 4097 bl->inserted = 0;
c906108c
SS
4098}
4099
53a5351d
JM
4100/* Clear the "inserted" flag in all breakpoints and delete any
4101 breakpoints which should go away between runs of the program.
c906108c
SS
4102
4103 Plus other such housekeeping that has to be done for breakpoints
4104 between runs.
4105
53a5351d
JM
4106 Note: this function gets called at the end of a run (by
4107 generic_mourn_inferior) and when a run begins (by
4a64f543 4108 init_wait_for_inferior). */
c906108c
SS
4109
4110
4111
4112void
fba45db2 4113breakpoint_init_inferior (enum inf_context context)
c906108c 4114{
35df4500
TJB
4115 struct breakpoint *b, *b_tmp;
4116 struct bp_location *bl, **blp_tmp;
1c5cfe86 4117 int ix;
6c95b8df 4118 struct program_space *pspace = current_program_space;
c906108c 4119
50c71eaf
PA
4120 /* If breakpoint locations are shared across processes, then there's
4121 nothing to do. */
f5656ead 4122 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4123 return;
4124
1a853c52 4125 mark_breakpoints_out ();
075f6582 4126
35df4500 4127 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4128 {
6c95b8df
PA
4129 if (b->loc && b->loc->pspace != pspace)
4130 continue;
4131
c5aa993b
JM
4132 switch (b->type)
4133 {
4134 case bp_call_dummy:
e2e4d78b 4135 case bp_longjmp_call_dummy:
c906108c 4136
c5aa993b 4137 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4138 cause problems when the inferior is rerun, so we better get
4139 rid of it. */
4140
4141 case bp_watchpoint_scope:
4142
4143 /* Also get rid of scope breakpoints. */
4144
4145 case bp_shlib_event:
4146
4147 /* Also remove solib event breakpoints. Their addresses may
4148 have changed since the last time we ran the program.
4149 Actually we may now be debugging against different target;
4150 and so the solib backend that installed this breakpoint may
4151 not be used in by the target. E.g.,
4152
4153 (gdb) file prog-linux
4154 (gdb) run # native linux target
4155 ...
4156 (gdb) kill
4157 (gdb) file prog-win.exe
4158 (gdb) tar rem :9999 # remote Windows gdbserver.
4159 */
c906108c 4160
f59f708a
PA
4161 case bp_step_resume:
4162
4163 /* Also remove step-resume breakpoints. */
4164
7c16b83e
PA
4165 case bp_single_step:
4166
4167 /* Also remove single-step breakpoints. */
4168
c5aa993b
JM
4169 delete_breakpoint (b);
4170 break;
c906108c 4171
c5aa993b
JM
4172 case bp_watchpoint:
4173 case bp_hardware_watchpoint:
4174 case bp_read_watchpoint:
4175 case bp_access_watchpoint:
3a5c3e22
PA
4176 {
4177 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4178
3a5c3e22
PA
4179 /* Likewise for watchpoints on local expressions. */
4180 if (w->exp_valid_block != NULL)
4181 delete_breakpoint (b);
4182 else if (context == inf_starting)
4183 {
4184 /* Reset val field to force reread of starting value in
4185 insert_breakpoints. */
4186 if (w->val)
4187 value_free (w->val);
4188 w->val = NULL;
4189 w->val_valid = 0;
c860120c 4190 }
3a5c3e22 4191 }
c5aa993b
JM
4192 break;
4193 default:
c5aa993b
JM
4194 break;
4195 }
4196 }
1c5cfe86
PA
4197
4198 /* Get rid of the moribund locations. */
35df4500
TJB
4199 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4200 decref_bp_location (&bl);
1c5cfe86 4201 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4202}
4203
6c95b8df
PA
4204/* These functions concern about actual breakpoints inserted in the
4205 target --- to e.g. check if we need to do decr_pc adjustment or if
4206 we need to hop over the bkpt --- so we check for address space
4207 match, not program space. */
4208
c2c6d25f
JM
4209/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4210 exists at PC. It returns ordinary_breakpoint_here if it's an
4211 ordinary breakpoint, or permanent_breakpoint_here if it's a
4212 permanent breakpoint.
4213 - When continuing from a location with an ordinary breakpoint, we
4214 actually single step once before calling insert_breakpoints.
e5dd4106 4215 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4216 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4217 the target, to advance the PC past the breakpoint. */
c906108c 4218
c2c6d25f 4219enum breakpoint_here
6c95b8df 4220breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4221{
35df4500 4222 struct bp_location *bl, **blp_tmp;
c2c6d25f 4223 int any_breakpoint_here = 0;
c906108c 4224
35df4500 4225 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4226 {
35df4500
TJB
4227 if (bl->loc_type != bp_loc_software_breakpoint
4228 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4229 continue;
4230
f1310107 4231 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4232 if ((breakpoint_enabled (bl->owner)
1a853c52 4233 || bl->permanent)
f1310107 4234 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4235 {
4236 if (overlay_debugging
35df4500
TJB
4237 && section_is_overlay (bl->section)
4238 && !section_is_mapped (bl->section))
075f6582 4239 continue; /* unmapped overlay -- can't be a match */
1a853c52 4240 else if (bl->permanent)
075f6582
DJ
4241 return permanent_breakpoint_here;
4242 else
4243 any_breakpoint_here = 1;
4244 }
4245 }
c906108c 4246
f486487f 4247 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4248}
4249
d35ae833
PA
4250/* See breakpoint.h. */
4251
4252int
4253breakpoint_in_range_p (struct address_space *aspace,
4254 CORE_ADDR addr, ULONGEST len)
4255{
4256 struct bp_location *bl, **blp_tmp;
4257
4258 ALL_BP_LOCATIONS (bl, blp_tmp)
4259 {
4260 if (bl->loc_type != bp_loc_software_breakpoint
4261 && bl->loc_type != bp_loc_hardware_breakpoint)
4262 continue;
4263
4264 if ((breakpoint_enabled (bl->owner)
4265 || bl->permanent)
4266 && breakpoint_location_address_range_overlap (bl, aspace,
4267 addr, len))
4268 {
4269 if (overlay_debugging
4270 && section_is_overlay (bl->section)
4271 && !section_is_mapped (bl->section))
4272 {
4273 /* Unmapped overlay -- can't be a match. */
4274 continue;
4275 }
4276
4277 return 1;
4278 }
4279 }
4280
4281 return 0;
4282}
4283
1c5cfe86
PA
4284/* Return true if there's a moribund breakpoint at PC. */
4285
4286int
6c95b8df 4287moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4288{
4289 struct bp_location *loc;
4290 int ix;
4291
4292 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4293 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4294 return 1;
4295
4296 return 0;
4297}
c2c6d25f 4298
f7ce857f
PA
4299/* Returns non-zero iff BL is inserted at PC, in address space
4300 ASPACE. */
4301
4302static int
4303bp_location_inserted_here_p (struct bp_location *bl,
4304 struct address_space *aspace, CORE_ADDR pc)
4305{
4306 if (bl->inserted
4307 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4308 aspace, pc))
4309 {
4310 if (overlay_debugging
4311 && section_is_overlay (bl->section)
4312 && !section_is_mapped (bl->section))
4313 return 0; /* unmapped overlay -- can't be a match */
4314 else
4315 return 1;
4316 }
4317 return 0;
4318}
4319
a1fd2fa5 4320/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4321
4322int
a1fd2fa5 4323breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4324{
f7ce857f
PA
4325 struct bp_location **blp, **blp_tmp = NULL;
4326 struct bp_location *bl;
c906108c 4327
f7ce857f 4328 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4329 {
f7ce857f
PA
4330 struct bp_location *bl = *blp;
4331
35df4500
TJB
4332 if (bl->loc_type != bp_loc_software_breakpoint
4333 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4334 continue;
4335
f7ce857f
PA
4336 if (bp_location_inserted_here_p (bl, aspace, pc))
4337 return 1;
c5aa993b 4338 }
c36b740a
VP
4339 return 0;
4340}
4341
a1fd2fa5
PA
4342/* This function returns non-zero iff there is a software breakpoint
4343 inserted at PC. */
c36b740a
VP
4344
4345int
a1fd2fa5
PA
4346software_breakpoint_inserted_here_p (struct address_space *aspace,
4347 CORE_ADDR pc)
4fa8626c 4348{
f7ce857f
PA
4349 struct bp_location **blp, **blp_tmp = NULL;
4350 struct bp_location *bl;
4fa8626c 4351
f7ce857f 4352 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4353 {
f7ce857f
PA
4354 struct bp_location *bl = *blp;
4355
35df4500 4356 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4357 continue;
4358
f7ce857f
PA
4359 if (bp_location_inserted_here_p (bl, aspace, pc))
4360 return 1;
4fa8626c
DJ
4361 }
4362
4363 return 0;
9c02b525
PA
4364}
4365
4366/* See breakpoint.h. */
4367
4368int
4369hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4370 CORE_ADDR pc)
4371{
4372 struct bp_location **blp, **blp_tmp = NULL;
4373 struct bp_location *bl;
4374
4375 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4376 {
4377 struct bp_location *bl = *blp;
4378
4379 if (bl->loc_type != bp_loc_hardware_breakpoint)
4380 continue;
4381
4382 if (bp_location_inserted_here_p (bl, aspace, pc))
4383 return 1;
4384 }
4385
4386 return 0;
4fa8626c
DJ
4387}
4388
9093389c
PA
4389int
4390hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4391 CORE_ADDR addr, ULONGEST len)
4392{
4393 struct breakpoint *bpt;
4394
4395 ALL_BREAKPOINTS (bpt)
4396 {
4397 struct bp_location *loc;
4398
4399 if (bpt->type != bp_hardware_watchpoint
4400 && bpt->type != bp_access_watchpoint)
4401 continue;
4402
4403 if (!breakpoint_enabled (bpt))
4404 continue;
4405
4406 for (loc = bpt->loc; loc; loc = loc->next)
4407 if (loc->pspace->aspace == aspace && loc->inserted)
4408 {
4409 CORE_ADDR l, h;
4410
4411 /* Check for intersection. */
4412 l = max (loc->address, addr);
4413 h = min (loc->address + loc->length, addr + len);
4414 if (l < h)
4415 return 1;
4416 }
4417 }
4418 return 0;
4419}
c906108c 4420\f
c5aa993b 4421
c906108c
SS
4422/* bpstat stuff. External routines' interfaces are documented
4423 in breakpoint.h. */
4424
4425int
c326b90e 4426is_catchpoint (struct breakpoint *ep)
c906108c 4427{
533be4dd 4428 return (ep->type == bp_catchpoint);
c906108c
SS
4429}
4430
f431efe5
PA
4431/* Frees any storage that is part of a bpstat. Does not walk the
4432 'next' chain. */
4433
4434static void
198757a8
VP
4435bpstat_free (bpstat bs)
4436{
4437 if (bs->old_val != NULL)
4438 value_free (bs->old_val);
9add0f1b 4439 decref_counted_command_line (&bs->commands);
f431efe5 4440 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4441 xfree (bs);
4442}
4443
c906108c
SS
4444/* Clear a bpstat so that it says we are not at any breakpoint.
4445 Also free any storage that is part of a bpstat. */
4446
4447void
fba45db2 4448bpstat_clear (bpstat *bsp)
c906108c
SS
4449{
4450 bpstat p;
4451 bpstat q;
4452
4453 if (bsp == 0)
4454 return;
4455 p = *bsp;
4456 while (p != NULL)
4457 {
4458 q = p->next;
198757a8 4459 bpstat_free (p);
c906108c
SS
4460 p = q;
4461 }
4462 *bsp = NULL;
4463}
4464
4465/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4466 is part of the bpstat is copied as well. */
4467
4468bpstat
fba45db2 4469bpstat_copy (bpstat bs)
c906108c
SS
4470{
4471 bpstat p = NULL;
4472 bpstat tmp;
4473 bpstat retval = NULL;
4474
4475 if (bs == NULL)
4476 return bs;
4477
4478 for (; bs != NULL; bs = bs->next)
4479 {
4480 tmp = (bpstat) xmalloc (sizeof (*tmp));
4481 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4482 incref_counted_command_line (tmp->commands);
f431efe5 4483 incref_bp_location (tmp->bp_location_at);
31cc81e9 4484 if (bs->old_val != NULL)
3c3185ac
JK
4485 {
4486 tmp->old_val = value_copy (bs->old_val);
4487 release_value (tmp->old_val);
4488 }
31cc81e9 4489
c906108c
SS
4490 if (p == NULL)
4491 /* This is the first thing in the chain. */
4492 retval = tmp;
4493 else
4494 p->next = tmp;
4495 p = tmp;
4496 }
4497 p->next = NULL;
4498 return retval;
4499}
4500
4a64f543 4501/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4502
4503bpstat
fba45db2 4504bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4505{
c5aa993b
JM
4506 if (bsp == NULL)
4507 return NULL;
c906108c 4508
c5aa993b
JM
4509 for (; bsp != NULL; bsp = bsp->next)
4510 {
f431efe5 4511 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4512 return bsp;
4513 }
c906108c
SS
4514 return NULL;
4515}
4516
ab04a2af
TT
4517/* See breakpoint.h. */
4518
47591c29 4519int
427cd150 4520bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4521{
ab04a2af
TT
4522 for (; bsp != NULL; bsp = bsp->next)
4523 {
427cd150
TT
4524 if (bsp->breakpoint_at == NULL)
4525 {
4526 /* A moribund location can never explain a signal other than
4527 GDB_SIGNAL_TRAP. */
4528 if (sig == GDB_SIGNAL_TRAP)
47591c29 4529 return 1;
427cd150
TT
4530 }
4531 else
47591c29
PA
4532 {
4533 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4534 sig))
4535 return 1;
4536 }
ab04a2af
TT
4537 }
4538
47591c29 4539 return 0;
ab04a2af
TT
4540}
4541
4a64f543
MS
4542/* Put in *NUM the breakpoint number of the first breakpoint we are
4543 stopped at. *BSP upon return is a bpstat which points to the
4544 remaining breakpoints stopped at (but which is not guaranteed to be
4545 good for anything but further calls to bpstat_num).
4546
8671a17b
PA
4547 Return 0 if passed a bpstat which does not indicate any breakpoints.
4548 Return -1 if stopped at a breakpoint that has been deleted since
4549 we set it.
4550 Return 1 otherwise. */
c906108c
SS
4551
4552int
8671a17b 4553bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4554{
4555 struct breakpoint *b;
4556
4557 if ((*bsp) == NULL)
4558 return 0; /* No more breakpoint values */
8671a17b 4559
4a64f543
MS
4560 /* We assume we'll never have several bpstats that correspond to a
4561 single breakpoint -- otherwise, this function might return the
4562 same number more than once and this will look ugly. */
f431efe5 4563 b = (*bsp)->breakpoint_at;
8671a17b
PA
4564 *bsp = (*bsp)->next;
4565 if (b == NULL)
4566 return -1; /* breakpoint that's been deleted since */
4567
4568 *num = b->number; /* We have its number */
4569 return 1;
c906108c
SS
4570}
4571
e93ca019 4572/* See breakpoint.h. */
c906108c
SS
4573
4574void
e93ca019 4575bpstat_clear_actions (void)
c906108c 4576{
e93ca019
JK
4577 struct thread_info *tp;
4578 bpstat bs;
4579
4580 if (ptid_equal (inferior_ptid, null_ptid))
4581 return;
4582
4583 tp = find_thread_ptid (inferior_ptid);
4584 if (tp == NULL)
4585 return;
4586
4587 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4588 {
9add0f1b 4589 decref_counted_command_line (&bs->commands);
abf85f46 4590
c906108c
SS
4591 if (bs->old_val != NULL)
4592 {
4593 value_free (bs->old_val);
4594 bs->old_val = NULL;
4595 }
4596 }
4597}
4598
f3b1572e
PA
4599/* Called when a command is about to proceed the inferior. */
4600
4601static void
4602breakpoint_about_to_proceed (void)
4603{
4604 if (!ptid_equal (inferior_ptid, null_ptid))
4605 {
4606 struct thread_info *tp = inferior_thread ();
4607
4608 /* Allow inferior function calls in breakpoint commands to not
4609 interrupt the command list. When the call finishes
4610 successfully, the inferior will be standing at the same
4611 breakpoint as if nothing happened. */
16c381f0 4612 if (tp->control.in_infcall)
f3b1572e
PA
4613 return;
4614 }
4615
4616 breakpoint_proceeded = 1;
4617}
4618
4a64f543
MS
4619/* Stub for cleaning up our state if we error-out of a breakpoint
4620 command. */
c906108c 4621static void
4efb68b1 4622cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4623{
4624 executing_breakpoint_commands = 0;
4625}
4626
abf85f46
JK
4627/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4628 or its equivalent. */
4629
4630static int
4631command_line_is_silent (struct command_line *cmd)
4632{
4f45d445 4633 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4634}
4635
4a64f543
MS
4636/* Execute all the commands associated with all the breakpoints at
4637 this location. Any of these commands could cause the process to
4638 proceed beyond this point, etc. We look out for such changes by
4639 checking the global "breakpoint_proceeded" after each command.
c906108c 4640
347bddb7
PA
4641 Returns true if a breakpoint command resumed the inferior. In that
4642 case, it is the caller's responsibility to recall it again with the
4643 bpstat of the current thread. */
4644
4645static int
4646bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4647{
4648 bpstat bs;
4649 struct cleanup *old_chain;
347bddb7 4650 int again = 0;
c906108c
SS
4651
4652 /* Avoid endless recursion if a `source' command is contained
4653 in bs->commands. */
4654 if (executing_breakpoint_commands)
347bddb7 4655 return 0;
c906108c
SS
4656
4657 executing_breakpoint_commands = 1;
4658 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4659
cf6c5ffb
TT
4660 prevent_dont_repeat ();
4661
4a64f543 4662 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4663 bs = *bsp;
4664
4665 breakpoint_proceeded = 0;
4666 for (; bs != NULL; bs = bs->next)
4667 {
9add0f1b 4668 struct counted_command_line *ccmd;
6c50ab1c
JB
4669 struct command_line *cmd;
4670 struct cleanup *this_cmd_tree_chain;
4671
4672 /* Take ownership of the BSP's command tree, if it has one.
4673
4674 The command tree could legitimately contain commands like
4675 'step' and 'next', which call clear_proceed_status, which
4676 frees stop_bpstat's command tree. To make sure this doesn't
4677 free the tree we're executing out from under us, we need to
4678 take ownership of the tree ourselves. Since a given bpstat's
4679 commands are only executed once, we don't need to copy it; we
4680 can clear the pointer in the bpstat, and make sure we free
4681 the tree when we're done. */
9add0f1b
TT
4682 ccmd = bs->commands;
4683 bs->commands = NULL;
abf85f46
JK
4684 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4685 cmd = ccmd ? ccmd->commands : NULL;
4686 if (command_line_is_silent (cmd))
4687 {
4688 /* The action has been already done by bpstat_stop_status. */
4689 cmd = cmd->next;
4690 }
6c50ab1c 4691
c906108c
SS
4692 while (cmd != NULL)
4693 {
4694 execute_control_command (cmd);
4695
4696 if (breakpoint_proceeded)
4697 break;
4698 else
4699 cmd = cmd->next;
4700 }
6c50ab1c
JB
4701
4702 /* We can free this command tree now. */
4703 do_cleanups (this_cmd_tree_chain);
4704
c906108c 4705 if (breakpoint_proceeded)
32c1e744 4706 {
0b333c5e 4707 if (interpreter_async)
347bddb7
PA
4708 /* If we are in async mode, then the target might be still
4709 running, not stopped at any breakpoint, so nothing for
4710 us to do here -- just return to the event loop. */
4711 ;
32c1e744
VP
4712 else
4713 /* In sync mode, when execute_control_command returns
4714 we're already standing on the next breakpoint.
347bddb7
PA
4715 Breakpoint commands for that stop were not run, since
4716 execute_command does not run breakpoint commands --
4717 only command_line_handler does, but that one is not
4718 involved in execution of breakpoint commands. So, we
4719 can now execute breakpoint commands. It should be
4720 noted that making execute_command do bpstat actions is
4721 not an option -- in this case we'll have recursive
4722 invocation of bpstat for each breakpoint with a
4723 command, and can easily blow up GDB stack. Instead, we
4724 return true, which will trigger the caller to recall us
4725 with the new stop_bpstat. */
4726 again = 1;
4727 break;
32c1e744 4728 }
c906108c 4729 }
c2b8ed2c 4730 do_cleanups (old_chain);
347bddb7
PA
4731 return again;
4732}
4733
4734void
4735bpstat_do_actions (void)
4736{
353d1d73
JK
4737 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4738
347bddb7
PA
4739 /* Do any commands attached to breakpoint we are stopped at. */
4740 while (!ptid_equal (inferior_ptid, null_ptid)
4741 && target_has_execution
4742 && !is_exited (inferior_ptid)
4743 && !is_executing (inferior_ptid))
4744 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4745 and only return when it is stopped at the next breakpoint, we
4746 keep doing breakpoint actions until it returns false to
4747 indicate the inferior was not resumed. */
16c381f0 4748 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4749 break;
353d1d73
JK
4750
4751 discard_cleanups (cleanup_if_error);
c906108c
SS
4752}
4753
fa4727a6
DJ
4754/* Print out the (old or new) value associated with a watchpoint. */
4755
4756static void
4757watchpoint_value_print (struct value *val, struct ui_file *stream)
4758{
4759 if (val == NULL)
4760 fprintf_unfiltered (stream, _("<unreadable>"));
4761 else
79a45b7d
TT
4762 {
4763 struct value_print_options opts;
4764 get_user_print_options (&opts);
4765 value_print (val, stream, &opts);
4766 }
fa4727a6
DJ
4767}
4768
e514a9d6 4769/* Generic routine for printing messages indicating why we
4a64f543 4770 stopped. The behavior of this function depends on the value
e514a9d6
JM
4771 'print_it' in the bpstat structure. Under some circumstances we
4772 may decide not to print anything here and delegate the task to
4a64f543 4773 normal_stop(). */
e514a9d6
JM
4774
4775static enum print_stop_action
4776print_bp_stop_message (bpstat bs)
4777{
4778 switch (bs->print_it)
4779 {
4780 case print_it_noop:
4a64f543 4781 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4782 return PRINT_UNKNOWN;
4783 break;
4784
4785 case print_it_done:
4786 /* We still want to print the frame, but we already printed the
4a64f543 4787 relevant messages. */
e514a9d6
JM
4788 return PRINT_SRC_AND_LOC;
4789 break;
4790
4791 case print_it_normal:
4f8d1dc6 4792 {
f431efe5
PA
4793 struct breakpoint *b = bs->breakpoint_at;
4794
1a6a67de
TJB
4795 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4796 which has since been deleted. */
4797 if (b == NULL)
4798 return PRINT_UNKNOWN;
4799
348d480f
PA
4800 /* Normal case. Call the breakpoint's print_it method. */
4801 return b->ops->print_it (bs);
4f8d1dc6 4802 }
348d480f 4803 break;
3086aeae 4804
e514a9d6 4805 default:
8e65ff28 4806 internal_error (__FILE__, __LINE__,
e2e0b3e5 4807 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4808 break;
c906108c 4809 }
c906108c
SS
4810}
4811
edcc5120
TT
4812/* A helper function that prints a shared library stopped event. */
4813
4814static void
4815print_solib_event (int is_catchpoint)
4816{
4817 int any_deleted
4818 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4819 int any_added
4820 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4821
4822 if (!is_catchpoint)
4823 {
4824 if (any_added || any_deleted)
4825 ui_out_text (current_uiout,
4826 _("Stopped due to shared library event:\n"));
4827 else
4828 ui_out_text (current_uiout,
4829 _("Stopped due to shared library event (no "
4830 "libraries added or removed)\n"));
4831 }
4832
4833 if (ui_out_is_mi_like_p (current_uiout))
4834 ui_out_field_string (current_uiout, "reason",
4835 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4836
4837 if (any_deleted)
4838 {
4839 struct cleanup *cleanup;
4840 char *name;
4841 int ix;
4842
4843 ui_out_text (current_uiout, _(" Inferior unloaded "));
4844 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4845 "removed");
4846 for (ix = 0;
4847 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4848 ix, name);
4849 ++ix)
4850 {
4851 if (ix > 0)
4852 ui_out_text (current_uiout, " ");
4853 ui_out_field_string (current_uiout, "library", name);
4854 ui_out_text (current_uiout, "\n");
4855 }
4856
4857 do_cleanups (cleanup);
4858 }
4859
4860 if (any_added)
4861 {
4862 struct so_list *iter;
4863 int ix;
4864 struct cleanup *cleanup;
4865
4866 ui_out_text (current_uiout, _(" Inferior loaded "));
4867 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4868 "added");
4869 for (ix = 0;
4870 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4871 ix, iter);
4872 ++ix)
4873 {
4874 if (ix > 0)
4875 ui_out_text (current_uiout, " ");
4876 ui_out_field_string (current_uiout, "library", iter->so_name);
4877 ui_out_text (current_uiout, "\n");
4878 }
4879
4880 do_cleanups (cleanup);
4881 }
4882}
4883
e514a9d6
JM
4884/* Print a message indicating what happened. This is called from
4885 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4886 list - a list of the eventpoints that caused this stop. KIND is
4887 the target_waitkind for the stopping event. This
e514a9d6
JM
4888 routine calls the generic print routine for printing a message
4889 about reasons for stopping. This will print (for example) the
4890 "Breakpoint n," part of the output. The return value of this
4891 routine is one of:
c906108c 4892
4a64f543 4893 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4894 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4895 code to print the location. An example is
c5aa993b
JM
4896 "Breakpoint 1, " which should be followed by
4897 the location.
917317f4 4898 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4899 to also print the location part of the message.
4900 An example is the catch/throw messages, which
4a64f543 4901 don't require a location appended to the end.
917317f4 4902 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4903 further info to be printed. */
c906108c 4904
917317f4 4905enum print_stop_action
36dfb11c 4906bpstat_print (bpstat bs, int kind)
c906108c 4907{
f486487f 4908 enum print_stop_action val;
c5aa993b 4909
c906108c 4910 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4911 (Currently all watchpoints go on the bpstat whether hit or not.
4912 That probably could (should) be changed, provided care is taken
c906108c 4913 with respect to bpstat_explains_signal). */
e514a9d6
JM
4914 for (; bs; bs = bs->next)
4915 {
4916 val = print_bp_stop_message (bs);
4917 if (val == PRINT_SRC_ONLY
4918 || val == PRINT_SRC_AND_LOC
4919 || val == PRINT_NOTHING)
4920 return val;
4921 }
c906108c 4922
36dfb11c
TT
4923 /* If we had hit a shared library event breakpoint,
4924 print_bp_stop_message would print out this message. If we hit an
4925 OS-level shared library event, do the same thing. */
4926 if (kind == TARGET_WAITKIND_LOADED)
4927 {
edcc5120 4928 print_solib_event (0);
36dfb11c
TT
4929 return PRINT_NOTHING;
4930 }
4931
e514a9d6 4932 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4933 with and nothing was printed. */
917317f4 4934 return PRINT_UNKNOWN;
c906108c
SS
4935}
4936
c42bd95a
DE
4937/* Evaluate the expression EXP and return 1 if value is zero.
4938 This returns the inverse of the condition because it is called
4939 from catch_errors which returns 0 if an exception happened, and if an
4940 exception happens we want execution to stop.
4a64f543 4941 The argument is a "struct expression *" that has been cast to a
c42bd95a 4942 "void *" to make it pass through catch_errors. */
c906108c
SS
4943
4944static int
4efb68b1 4945breakpoint_cond_eval (void *exp)
c906108c 4946{
278cd55f 4947 struct value *mark = value_mark ();
c5aa993b 4948 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4949
c906108c
SS
4950 value_free_to_mark (mark);
4951 return i;
4952}
4953
5760d0ab 4954/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4955
4956static bpstat
5760d0ab 4957bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4958{
4959 bpstat bs;
4960
4961 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4962 bs->next = NULL;
4963 **bs_link_pointer = bs;
4964 *bs_link_pointer = &bs->next;
f431efe5
PA
4965 bs->breakpoint_at = bl->owner;
4966 bs->bp_location_at = bl;
4967 incref_bp_location (bl);
c906108c
SS
4968 /* If the condition is false, etc., don't do the commands. */
4969 bs->commands = NULL;
4970 bs->old_val = NULL;
4971 bs->print_it = print_it_normal;
4972 return bs;
4973}
4974\f
d983da9c
DJ
4975/* The target has stopped with waitstatus WS. Check if any hardware
4976 watchpoints have triggered, according to the target. */
4977
4978int
4979watchpoints_triggered (struct target_waitstatus *ws)
4980{
d92524f1 4981 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4982 CORE_ADDR addr;
4983 struct breakpoint *b;
4984
4985 if (!stopped_by_watchpoint)
4986 {
4987 /* We were not stopped by a watchpoint. Mark all watchpoints
4988 as not triggered. */
4989 ALL_BREAKPOINTS (b)
cc60f2e3 4990 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4991 {
4992 struct watchpoint *w = (struct watchpoint *) b;
4993
4994 w->watchpoint_triggered = watch_triggered_no;
4995 }
d983da9c
DJ
4996
4997 return 0;
4998 }
4999
5000 if (!target_stopped_data_address (&current_target, &addr))
5001 {
5002 /* We were stopped by a watchpoint, but we don't know where.
5003 Mark all watchpoints as unknown. */
5004 ALL_BREAKPOINTS (b)
cc60f2e3 5005 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5006 {
5007 struct watchpoint *w = (struct watchpoint *) b;
5008
5009 w->watchpoint_triggered = watch_triggered_unknown;
5010 }
d983da9c 5011
3c4797ba 5012 return 1;
d983da9c
DJ
5013 }
5014
5015 /* The target could report the data address. Mark watchpoints
5016 affected by this data address as triggered, and all others as not
5017 triggered. */
5018
5019 ALL_BREAKPOINTS (b)
cc60f2e3 5020 if (is_hardware_watchpoint (b))
d983da9c 5021 {
3a5c3e22 5022 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5023 struct bp_location *loc;
d983da9c 5024
3a5c3e22 5025 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5026 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5027 {
3a5c3e22 5028 if (is_masked_watchpoint (b))
9c06b0b4 5029 {
3a5c3e22
PA
5030 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5031 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5032
5033 if (newaddr == start)
5034 {
3a5c3e22 5035 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5036 break;
5037 }
5038 }
5039 /* Exact match not required. Within range is sufficient. */
5040 else if (target_watchpoint_addr_within_range (&current_target,
5041 addr, loc->address,
5042 loc->length))
5043 {
3a5c3e22 5044 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5045 break;
5046 }
5047 }
d983da9c
DJ
5048 }
5049
5050 return 1;
5051}
5052
c906108c
SS
5053/* Possible return values for watchpoint_check (this can't be an enum
5054 because of check_errors). */
5055/* The watchpoint has been deleted. */
5056#define WP_DELETED 1
5057/* The value has changed. */
5058#define WP_VALUE_CHANGED 2
5059/* The value has not changed. */
5060#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5061/* Ignore this watchpoint, no matter if the value changed or not. */
5062#define WP_IGNORE 4
c906108c
SS
5063
5064#define BP_TEMPFLAG 1
5065#define BP_HARDWAREFLAG 2
5066
4a64f543
MS
5067/* Evaluate watchpoint condition expression and check if its value
5068 changed.
553e4c11
JB
5069
5070 P should be a pointer to struct bpstat, but is defined as a void *
5071 in order for this function to be usable with catch_errors. */
c906108c
SS
5072
5073static int
4efb68b1 5074watchpoint_check (void *p)
c906108c
SS
5075{
5076 bpstat bs = (bpstat) p;
3a5c3e22 5077 struct watchpoint *b;
c906108c
SS
5078 struct frame_info *fr;
5079 int within_current_scope;
5080
f431efe5 5081 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5082 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5083 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5084
f6bc2008
PA
5085 /* If this is a local watchpoint, we only want to check if the
5086 watchpoint frame is in scope if the current thread is the thread
5087 that was used to create the watchpoint. */
5088 if (!watchpoint_in_thread_scope (b))
60e1c644 5089 return WP_IGNORE;
f6bc2008 5090
c906108c
SS
5091 if (b->exp_valid_block == NULL)
5092 within_current_scope = 1;
5093 else
5094 {
edb3359d
DJ
5095 struct frame_info *frame = get_current_frame ();
5096 struct gdbarch *frame_arch = get_frame_arch (frame);
5097 CORE_ADDR frame_pc = get_frame_pc (frame);
5098
c9cf6e20 5099 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5100 still in the function but the stack frame has already been
5101 invalidated. Since we can't rely on the values of local
5102 variables after the stack has been destroyed, we are treating
5103 the watchpoint in that state as `not changed' without further
5104 checking. Don't mark watchpoints as changed if the current
5105 frame is in an epilogue - even if they are in some other
5106 frame, our view of the stack is likely to be wrong and
5107 frame_find_by_id could error out. */
c9cf6e20 5108 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5109 return WP_IGNORE;
a0f49112 5110
101dcfbe 5111 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5112 within_current_scope = (fr != NULL);
69fbadd5
DJ
5113
5114 /* If we've gotten confused in the unwinder, we might have
5115 returned a frame that can't describe this variable. */
edb3359d
DJ
5116 if (within_current_scope)
5117 {
5118 struct symbol *function;
5119
5120 function = get_frame_function (fr);
5121 if (function == NULL
5122 || !contained_in (b->exp_valid_block,
5123 SYMBOL_BLOCK_VALUE (function)))
5124 within_current_scope = 0;
5125 }
69fbadd5 5126
edb3359d 5127 if (within_current_scope)
c906108c
SS
5128 /* If we end up stopping, the current frame will get selected
5129 in normal_stop. So this call to select_frame won't affect
5130 the user. */
0f7d239c 5131 select_frame (fr);
c906108c 5132 }
c5aa993b 5133
c906108c
SS
5134 if (within_current_scope)
5135 {
4a64f543
MS
5136 /* We use value_{,free_to_}mark because it could be a *long*
5137 time before we return to the command level and call
5138 free_all_values. We can't call free_all_values because we
5139 might be in the middle of evaluating a function call. */
c906108c 5140
0cf6dd15 5141 int pc = 0;
9c06b0b4 5142 struct value *mark;
fa4727a6
DJ
5143 struct value *new_val;
5144
3a5c3e22 5145 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5146 /* Since we don't know the exact trigger address (from
5147 stopped_data_address), just tell the user we've triggered
5148 a mask watchpoint. */
5149 return WP_VALUE_CHANGED;
5150
5151 mark = value_mark ();
3a1115a0 5152 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5153
bb9d5f81
PP
5154 if (b->val_bitsize != 0)
5155 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5156
4a64f543
MS
5157 /* We use value_equal_contents instead of value_equal because
5158 the latter coerces an array to a pointer, thus comparing just
5159 the address of the array instead of its contents. This is
5160 not what we want. */
fa4727a6 5161 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5162 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5163 {
fa4727a6
DJ
5164 if (new_val != NULL)
5165 {
5166 release_value (new_val);
5167 value_free_to_mark (mark);
5168 }
c906108c
SS
5169 bs->old_val = b->val;
5170 b->val = new_val;
fa4727a6 5171 b->val_valid = 1;
c906108c
SS
5172 return WP_VALUE_CHANGED;
5173 }
5174 else
5175 {
60e1c644 5176 /* Nothing changed. */
c906108c 5177 value_free_to_mark (mark);
c906108c
SS
5178 return WP_VALUE_NOT_CHANGED;
5179 }
5180 }
5181 else
5182 {
79a45e25
PA
5183 struct ui_out *uiout = current_uiout;
5184
c906108c 5185 /* This seems like the only logical thing to do because
c5aa993b
JM
5186 if we temporarily ignored the watchpoint, then when
5187 we reenter the block in which it is valid it contains
5188 garbage (in the case of a function, it may have two
5189 garbage values, one before and one after the prologue).
5190 So we can't even detect the first assignment to it and
5191 watch after that (since the garbage may or may not equal
5192 the first value assigned). */
348d480f
PA
5193 /* We print all the stop information in
5194 breakpoint_ops->print_it, but in this case, by the time we
5195 call breakpoint_ops->print_it this bp will be deleted
5196 already. So we have no choice but print the information
5197 here. */
9dc5e2a9 5198 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5199 ui_out_field_string
5200 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5201 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5202 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5203 ui_out_text (uiout,
5204 " deleted because the program has left the block in\n\
8b93c638 5205which its expression is valid.\n");
4ce44c66 5206
cdac0397 5207 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5208 decref_counted_command_line (&b->base.commands);
d0fb5eae 5209 watchpoint_del_at_next_stop (b);
c906108c
SS
5210
5211 return WP_DELETED;
5212 }
5213}
5214
18a18393 5215/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5216 breakpoint location BL. This function does not check if we should
5217 stop, only if BL explains the stop. */
5218
18a18393 5219static int
6c95b8df 5220bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5221 struct address_space *aspace, CORE_ADDR bp_addr,
5222 const struct target_waitstatus *ws)
18a18393
VP
5223{
5224 struct breakpoint *b = bl->owner;
5225
348d480f 5226 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5227 gdb_assert (b != NULL);
5228
09ac7c10 5229 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5230}
5231
3a5c3e22
PA
5232/* Determine if the watched values have actually changed, and we
5233 should stop. If not, set BS->stop to 0. */
5234
18a18393
VP
5235static void
5236bpstat_check_watchpoint (bpstat bs)
5237{
2bdf28a0 5238 const struct bp_location *bl;
3a5c3e22 5239 struct watchpoint *b;
2bdf28a0
JK
5240
5241 /* BS is built for existing struct breakpoint. */
f431efe5 5242 bl = bs->bp_location_at;
2bdf28a0 5243 gdb_assert (bl != NULL);
3a5c3e22 5244 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5245 gdb_assert (b != NULL);
18a18393 5246
18a18393 5247 {
18a18393
VP
5248 int must_check_value = 0;
5249
3a5c3e22 5250 if (b->base.type == bp_watchpoint)
18a18393
VP
5251 /* For a software watchpoint, we must always check the
5252 watched value. */
5253 must_check_value = 1;
5254 else if (b->watchpoint_triggered == watch_triggered_yes)
5255 /* We have a hardware watchpoint (read, write, or access)
5256 and the target earlier reported an address watched by
5257 this watchpoint. */
5258 must_check_value = 1;
5259 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5260 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5261 /* We were stopped by a hardware watchpoint, but the target could
5262 not report the data address. We must check the watchpoint's
5263 value. Access and read watchpoints are out of luck; without
5264 a data address, we can't figure it out. */
5265 must_check_value = 1;
3a5c3e22 5266
18a18393
VP
5267 if (must_check_value)
5268 {
3e43a32a
MS
5269 char *message
5270 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5271 b->base.number);
18a18393
VP
5272 struct cleanup *cleanups = make_cleanup (xfree, message);
5273 int e = catch_errors (watchpoint_check, bs, message,
5274 RETURN_MASK_ALL);
5275 do_cleanups (cleanups);
5276 switch (e)
5277 {
5278 case WP_DELETED:
5279 /* We've already printed what needs to be printed. */
5280 bs->print_it = print_it_done;
5281 /* Stop. */
5282 break;
60e1c644
PA
5283 case WP_IGNORE:
5284 bs->print_it = print_it_noop;
5285 bs->stop = 0;
5286 break;
18a18393 5287 case WP_VALUE_CHANGED:
3a5c3e22 5288 if (b->base.type == bp_read_watchpoint)
18a18393 5289 {
85d721b8
PA
5290 /* There are two cases to consider here:
5291
4a64f543 5292 1. We're watching the triggered memory for reads.
85d721b8
PA
5293 In that case, trust the target, and always report
5294 the watchpoint hit to the user. Even though
5295 reads don't cause value changes, the value may
5296 have changed since the last time it was read, and
5297 since we're not trapping writes, we will not see
5298 those, and as such we should ignore our notion of
5299 old value.
5300
4a64f543 5301 2. We're watching the triggered memory for both
85d721b8
PA
5302 reads and writes. There are two ways this may
5303 happen:
5304
4a64f543 5305 2.1. This is a target that can't break on data
85d721b8
PA
5306 reads only, but can break on accesses (reads or
5307 writes), such as e.g., x86. We detect this case
5308 at the time we try to insert read watchpoints.
5309
4a64f543 5310 2.2. Otherwise, the target supports read
85d721b8
PA
5311 watchpoints, but, the user set an access or write
5312 watchpoint watching the same memory as this read
5313 watchpoint.
5314
5315 If we're watching memory writes as well as reads,
5316 ignore watchpoint hits when we find that the
5317 value hasn't changed, as reads don't cause
5318 changes. This still gives false positives when
5319 the program writes the same value to memory as
5320 what there was already in memory (we will confuse
5321 it for a read), but it's much better than
5322 nothing. */
5323
5324 int other_write_watchpoint = 0;
5325
5326 if (bl->watchpoint_type == hw_read)
5327 {
5328 struct breakpoint *other_b;
5329
5330 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5331 if (other_b->type == bp_hardware_watchpoint
5332 || other_b->type == bp_access_watchpoint)
85d721b8 5333 {
3a5c3e22
PA
5334 struct watchpoint *other_w =
5335 (struct watchpoint *) other_b;
5336
5337 if (other_w->watchpoint_triggered
5338 == watch_triggered_yes)
5339 {
5340 other_write_watchpoint = 1;
5341 break;
5342 }
85d721b8
PA
5343 }
5344 }
5345
5346 if (other_write_watchpoint
5347 || bl->watchpoint_type == hw_access)
5348 {
5349 /* We're watching the same memory for writes,
5350 and the value changed since the last time we
5351 updated it, so this trap must be for a write.
5352 Ignore it. */
5353 bs->print_it = print_it_noop;
5354 bs->stop = 0;
5355 }
18a18393
VP
5356 }
5357 break;
5358 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5359 if (b->base.type == bp_hardware_watchpoint
5360 || b->base.type == bp_watchpoint)
18a18393
VP
5361 {
5362 /* Don't stop: write watchpoints shouldn't fire if
5363 the value hasn't changed. */
5364 bs->print_it = print_it_noop;
5365 bs->stop = 0;
5366 }
5367 /* Stop. */
5368 break;
5369 default:
5370 /* Can't happen. */
5371 case 0:
5372 /* Error from catch_errors. */
3a5c3e22 5373 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5374 watchpoint_del_at_next_stop (b);
18a18393
VP
5375 /* We've already printed what needs to be printed. */
5376 bs->print_it = print_it_done;
5377 break;
5378 }
5379 }
5380 else /* must_check_value == 0 */
5381 {
5382 /* This is a case where some watchpoint(s) triggered, but
5383 not at the address of this watchpoint, or else no
5384 watchpoint triggered after all. So don't print
5385 anything for this watchpoint. */
5386 bs->print_it = print_it_noop;
5387 bs->stop = 0;
5388 }
5389 }
5390}
5391
7d4df6a4
DE
5392/* For breakpoints that are currently marked as telling gdb to stop,
5393 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5394 of breakpoint referred to by BS. If we should not stop for this
5395 breakpoint, set BS->stop to 0. */
f431efe5 5396
18a18393
VP
5397static void
5398bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5399{
2bdf28a0
JK
5400 const struct bp_location *bl;
5401 struct breakpoint *b;
7d4df6a4
DE
5402 int value_is_zero = 0;
5403 struct expression *cond;
5404
5405 gdb_assert (bs->stop);
2bdf28a0
JK
5406
5407 /* BS is built for existing struct breakpoint. */
f431efe5 5408 bl = bs->bp_location_at;
2bdf28a0 5409 gdb_assert (bl != NULL);
f431efe5 5410 b = bs->breakpoint_at;
2bdf28a0 5411 gdb_assert (b != NULL);
18a18393 5412
b775012e
LM
5413 /* Even if the target evaluated the condition on its end and notified GDB, we
5414 need to do so again since GDB does not know if we stopped due to a
5415 breakpoint or a single step breakpoint. */
5416
18a18393 5417 if (frame_id_p (b->frame_id)
edb3359d 5418 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5419 {
7d4df6a4
DE
5420 bs->stop = 0;
5421 return;
5422 }
60e1c644 5423
12ab52e9
PA
5424 /* If this is a thread/task-specific breakpoint, don't waste cpu
5425 evaluating the condition if this isn't the specified
5426 thread/task. */
5427 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5428 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5429
6c1b0f7b
DE
5430 {
5431 bs->stop = 0;
5432 return;
5433 }
5434
6dddc817
DE
5435 /* Evaluate extension language breakpoints that have a "stop" method
5436 implemented. */
5437 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5438
7d4df6a4
DE
5439 if (is_watchpoint (b))
5440 {
5441 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5442
7d4df6a4
DE
5443 cond = w->cond_exp;
5444 }
5445 else
5446 cond = bl->cond;
60e1c644 5447
7d4df6a4
DE
5448 if (cond && b->disposition != disp_del_at_next_stop)
5449 {
5450 int within_current_scope = 1;
5451 struct watchpoint * w;
60e1c644 5452
7d4df6a4
DE
5453 /* We use value_mark and value_free_to_mark because it could
5454 be a long time before we return to the command level and
5455 call free_all_values. We can't call free_all_values
5456 because we might be in the middle of evaluating a
5457 function call. */
5458 struct value *mark = value_mark ();
5459
5460 if (is_watchpoint (b))
5461 w = (struct watchpoint *) b;
5462 else
5463 w = NULL;
5464
5465 /* Need to select the frame, with all that implies so that
5466 the conditions will have the right context. Because we
5467 use the frame, we will not see an inlined function's
5468 variables when we arrive at a breakpoint at the start
5469 of the inlined function; the current frame will be the
5470 call site. */
5471 if (w == NULL || w->cond_exp_valid_block == NULL)
5472 select_frame (get_current_frame ());
5473 else
18a18393 5474 {
7d4df6a4
DE
5475 struct frame_info *frame;
5476
5477 /* For local watchpoint expressions, which particular
5478 instance of a local is being watched matters, so we
5479 keep track of the frame to evaluate the expression
5480 in. To evaluate the condition however, it doesn't
5481 really matter which instantiation of the function
5482 where the condition makes sense triggers the
5483 watchpoint. This allows an expression like "watch
5484 global if q > 10" set in `func', catch writes to
5485 global on all threads that call `func', or catch
5486 writes on all recursive calls of `func' by a single
5487 thread. We simply always evaluate the condition in
5488 the innermost frame that's executing where it makes
5489 sense to evaluate the condition. It seems
5490 intuitive. */
5491 frame = block_innermost_frame (w->cond_exp_valid_block);
5492 if (frame != NULL)
5493 select_frame (frame);
5494 else
5495 within_current_scope = 0;
18a18393 5496 }
7d4df6a4
DE
5497 if (within_current_scope)
5498 value_is_zero
5499 = catch_errors (breakpoint_cond_eval, cond,
5500 "Error in testing breakpoint condition:\n",
5501 RETURN_MASK_ALL);
5502 else
18a18393 5503 {
7d4df6a4
DE
5504 warning (_("Watchpoint condition cannot be tested "
5505 "in the current scope"));
5506 /* If we failed to set the right context for this
5507 watchpoint, unconditionally report it. */
5508 value_is_zero = 0;
18a18393 5509 }
7d4df6a4
DE
5510 /* FIXME-someday, should give breakpoint #. */
5511 value_free_to_mark (mark);
18a18393 5512 }
7d4df6a4
DE
5513
5514 if (cond && value_is_zero)
5515 {
5516 bs->stop = 0;
5517 }
7d4df6a4
DE
5518 else if (b->ignore_count > 0)
5519 {
5520 b->ignore_count--;
5521 bs->stop = 0;
5522 /* Increase the hit count even though we don't stop. */
5523 ++(b->hit_count);
5524 observer_notify_breakpoint_modified (b);
5525 }
18a18393
VP
5526}
5527
1cf4d951
PA
5528/* Returns true if we need to track moribund locations of LOC's type
5529 on the current target. */
5530
5531static int
5532need_moribund_for_location_type (struct bp_location *loc)
5533{
5534 return ((loc->loc_type == bp_loc_software_breakpoint
5535 && !target_supports_stopped_by_sw_breakpoint ())
5536 || (loc->loc_type == bp_loc_hardware_breakpoint
5537 && !target_supports_stopped_by_hw_breakpoint ()));
5538}
5539
18a18393 5540
9709f61c 5541/* Get a bpstat associated with having just stopped at address
d983da9c 5542 BP_ADDR in thread PTID.
c906108c 5543
d983da9c 5544 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5545 don't understand this stop. Result is a chain of bpstat's such
5546 that:
c906108c 5547
c5aa993b 5548 if we don't understand the stop, the result is a null pointer.
c906108c 5549
c5aa993b 5550 if we understand why we stopped, the result is not null.
c906108c 5551
c5aa993b
JM
5552 Each element of the chain refers to a particular breakpoint or
5553 watchpoint at which we have stopped. (We may have stopped for
5554 several reasons concurrently.)
c906108c 5555
c5aa993b
JM
5556 Each element of the chain has valid next, breakpoint_at,
5557 commands, FIXME??? fields. */
c906108c
SS
5558
5559bpstat
6c95b8df 5560bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5561 CORE_ADDR bp_addr, ptid_t ptid,
5562 const struct target_waitstatus *ws)
c906108c 5563{
0d381245 5564 struct breakpoint *b = NULL;
afe38095 5565 struct bp_location *bl;
20874c92 5566 struct bp_location *loc;
5760d0ab
JK
5567 /* First item of allocated bpstat's. */
5568 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5569 /* Pointer to the last thing in the chain currently. */
5760d0ab 5570 bpstat bs;
20874c92 5571 int ix;
429374b8 5572 int need_remove_insert;
f431efe5 5573 int removed_any;
c906108c 5574
f431efe5
PA
5575 /* First, build the bpstat chain with locations that explain a
5576 target stop, while being careful to not set the target running,
5577 as that may invalidate locations (in particular watchpoint
5578 locations are recreated). Resuming will happen here with
5579 breakpoint conditions or watchpoint expressions that include
5580 inferior function calls. */
c5aa993b 5581
429374b8
JK
5582 ALL_BREAKPOINTS (b)
5583 {
1a853c52 5584 if (!breakpoint_enabled (b))
429374b8 5585 continue;
a5606eee 5586
429374b8
JK
5587 for (bl = b->loc; bl != NULL; bl = bl->next)
5588 {
4a64f543
MS
5589 /* For hardware watchpoints, we look only at the first
5590 location. The watchpoint_check function will work on the
5591 entire expression, not the individual locations. For
5592 read watchpoints, the watchpoints_triggered function has
5593 checked all locations already. */
429374b8
JK
5594 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5595 break;
18a18393 5596
f6592439 5597 if (!bl->enabled || bl->shlib_disabled)
429374b8 5598 continue;
c5aa993b 5599
09ac7c10 5600 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5601 continue;
c5aa993b 5602
4a64f543
MS
5603 /* Come here if it's a watchpoint, or if the break address
5604 matches. */
c5aa993b 5605
4a64f543
MS
5606 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5607 explain stop. */
c5aa993b 5608
f431efe5
PA
5609 /* Assume we stop. Should we find a watchpoint that is not
5610 actually triggered, or if the condition of the breakpoint
5611 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5612 bs->stop = 1;
5613 bs->print = 1;
d983da9c 5614
f431efe5
PA
5615 /* If this is a scope breakpoint, mark the associated
5616 watchpoint as triggered so that we will handle the
5617 out-of-scope event. We'll get to the watchpoint next
5618 iteration. */
d0fb5eae 5619 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5620 {
5621 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5622
5623 w->watchpoint_triggered = watch_triggered_yes;
5624 }
f431efe5
PA
5625 }
5626 }
5627
7c16b83e 5628 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5629 if (!target_supports_stopped_by_sw_breakpoint ()
5630 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5631 {
1cf4d951 5632 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5633 {
1cf4d951
PA
5634 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5635 && need_moribund_for_location_type (loc))
5636 {
5637 bs = bpstat_alloc (loc, &bs_link);
5638 /* For hits of moribund locations, we should just proceed. */
5639 bs->stop = 0;
5640 bs->print = 0;
5641 bs->print_it = print_it_noop;
5642 }
f431efe5
PA
5643 }
5644 }
5645
edcc5120
TT
5646 /* A bit of special processing for shlib breakpoints. We need to
5647 process solib loading here, so that the lists of loaded and
5648 unloaded libraries are correct before we handle "catch load" and
5649 "catch unload". */
5650 for (bs = bs_head; bs != NULL; bs = bs->next)
5651 {
5d268276 5652 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5653 {
5654 handle_solib_event ();
5655 break;
5656 }
5657 }
5658
f431efe5
PA
5659 /* Now go through the locations that caused the target to stop, and
5660 check whether we're interested in reporting this stop to higher
5661 layers, or whether we should resume the target transparently. */
5662
5663 removed_any = 0;
5664
5760d0ab 5665 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5666 {
5667 if (!bs->stop)
5668 continue;
5669
f431efe5 5670 b = bs->breakpoint_at;
348d480f
PA
5671 b->ops->check_status (bs);
5672 if (bs->stop)
28010a5d 5673 {
348d480f 5674 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5675
429374b8
JK
5676 if (bs->stop)
5677 {
5678 ++(b->hit_count);
8d3788bd 5679 observer_notify_breakpoint_modified (b);
c906108c 5680
4a64f543 5681 /* We will stop here. */
429374b8
JK
5682 if (b->disposition == disp_disable)
5683 {
816338b5 5684 --(b->enable_count);
1a853c52 5685 if (b->enable_count <= 0)
429374b8 5686 b->enable_state = bp_disabled;
f431efe5 5687 removed_any = 1;
429374b8
JK
5688 }
5689 if (b->silent)
5690 bs->print = 0;
5691 bs->commands = b->commands;
9add0f1b 5692 incref_counted_command_line (bs->commands);
abf85f46
JK
5693 if (command_line_is_silent (bs->commands
5694 ? bs->commands->commands : NULL))
5695 bs->print = 0;
9d6e6e84
HZ
5696
5697 b->ops->after_condition_true (bs);
429374b8
JK
5698 }
5699
348d480f 5700 }
a9b3a50f
PA
5701
5702 /* Print nothing for this entry if we don't stop or don't
5703 print. */
5704 if (!bs->stop || !bs->print)
5705 bs->print_it = print_it_noop;
429374b8 5706 }
876fa593 5707
d983da9c
DJ
5708 /* If we aren't stopping, the value of some hardware watchpoint may
5709 not have changed, but the intermediate memory locations we are
5710 watching may have. Don't bother if we're stopping; this will get
5711 done later. */
d832cb68 5712 need_remove_insert = 0;
5760d0ab
JK
5713 if (! bpstat_causes_stop (bs_head))
5714 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5715 if (!bs->stop
f431efe5
PA
5716 && bs->breakpoint_at
5717 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5718 {
3a5c3e22
PA
5719 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5720
5721 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5722 need_remove_insert = 1;
d983da9c
DJ
5723 }
5724
d832cb68 5725 if (need_remove_insert)
44702360 5726 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5727 else if (removed_any)
44702360 5728 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5729
5760d0ab 5730 return bs_head;
c906108c 5731}
628fe4e4
JK
5732
5733static void
5734handle_jit_event (void)
5735{
5736 struct frame_info *frame;
5737 struct gdbarch *gdbarch;
5738
243a9253
PA
5739 if (debug_infrun)
5740 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5741
628fe4e4
JK
5742 /* Switch terminal for any messages produced by
5743 breakpoint_re_set. */
5744 target_terminal_ours_for_output ();
5745
5746 frame = get_current_frame ();
5747 gdbarch = get_frame_arch (frame);
5748
5749 jit_event_handler (gdbarch);
5750
5751 target_terminal_inferior ();
5752}
5753
5754/* Prepare WHAT final decision for infrun. */
5755
5756/* Decide what infrun needs to do with this bpstat. */
5757
c906108c 5758struct bpstat_what
0e30163f 5759bpstat_what (bpstat bs_head)
c906108c 5760{
c906108c 5761 struct bpstat_what retval;
628fe4e4 5762 int jit_event = 0;
0e30163f 5763 bpstat bs;
c906108c 5764
628fe4e4 5765 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5766 retval.call_dummy = STOP_NONE;
186c406b 5767 retval.is_longjmp = 0;
628fe4e4 5768
0e30163f 5769 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5770 {
628fe4e4
JK
5771 /* Extract this BS's action. After processing each BS, we check
5772 if its action overrides all we've seem so far. */
5773 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5774 enum bptype bptype;
5775
c906108c 5776 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5777 {
5778 /* I suspect this can happen if it was a momentary
5779 breakpoint which has since been deleted. */
5780 bptype = bp_none;
5781 }
20874c92 5782 else
f431efe5 5783 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5784
5785 switch (bptype)
c906108c
SS
5786 {
5787 case bp_none:
628fe4e4 5788 break;
c906108c
SS
5789 case bp_breakpoint:
5790 case bp_hardware_breakpoint:
7c16b83e 5791 case bp_single_step:
c906108c
SS
5792 case bp_until:
5793 case bp_finish:
a9b3a50f 5794 case bp_shlib_event:
c906108c
SS
5795 if (bs->stop)
5796 {
5797 if (bs->print)
628fe4e4 5798 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5799 else
628fe4e4 5800 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5801 }
5802 else
628fe4e4 5803 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5804 break;
5805 case bp_watchpoint:
5806 case bp_hardware_watchpoint:
5807 case bp_read_watchpoint:
5808 case bp_access_watchpoint:
5809 if (bs->stop)
5810 {
5811 if (bs->print)
628fe4e4 5812 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5813 else
628fe4e4 5814 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5815 }
5816 else
628fe4e4
JK
5817 {
5818 /* There was a watchpoint, but we're not stopping.
5819 This requires no further action. */
5820 }
c906108c
SS
5821 break;
5822 case bp_longjmp:
e2e4d78b 5823 case bp_longjmp_call_dummy:
186c406b 5824 case bp_exception:
0a39bb32
PA
5825 if (bs->stop)
5826 {
5827 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5828 retval.is_longjmp = bptype != bp_exception;
5829 }
5830 else
5831 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5832 break;
5833 case bp_longjmp_resume:
186c406b 5834 case bp_exception_resume:
0a39bb32
PA
5835 if (bs->stop)
5836 {
5837 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5838 retval.is_longjmp = bptype == bp_longjmp_resume;
5839 }
5840 else
5841 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5842 break;
5843 case bp_step_resume:
5844 if (bs->stop)
628fe4e4
JK
5845 this_action = BPSTAT_WHAT_STEP_RESUME;
5846 else
c906108c 5847 {
628fe4e4
JK
5848 /* It is for the wrong frame. */
5849 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5850 }
c906108c 5851 break;
2c03e5be
PA
5852 case bp_hp_step_resume:
5853 if (bs->stop)
5854 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5855 else
5856 {
5857 /* It is for the wrong frame. */
5858 this_action = BPSTAT_WHAT_SINGLE;
5859 }
5860 break;
c906108c 5861 case bp_watchpoint_scope:
c4093a6a 5862 case bp_thread_event:
1900040c 5863 case bp_overlay_event:
0fd8e87f 5864 case bp_longjmp_master:
aa7d318d 5865 case bp_std_terminate_master:
186c406b 5866 case bp_exception_master:
628fe4e4 5867 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5868 break;
ce78b96d 5869 case bp_catchpoint:
c5aa993b
JM
5870 if (bs->stop)
5871 {
5872 if (bs->print)
628fe4e4 5873 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5874 else
628fe4e4 5875 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5876 }
5877 else
628fe4e4
JK
5878 {
5879 /* There was a catchpoint, but we're not stopping.
5880 This requires no further action. */
5881 }
5882 break;
628fe4e4
JK
5883 case bp_jit_event:
5884 jit_event = 1;
5885 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5886 break;
c906108c 5887 case bp_call_dummy:
53a5351d
JM
5888 /* Make sure the action is stop (silent or noisy),
5889 so infrun.c pops the dummy frame. */
aa7d318d 5890 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5891 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5892 break;
5893 case bp_std_terminate:
5894 /* Make sure the action is stop (silent or noisy),
5895 so infrun.c pops the dummy frame. */
aa7d318d 5896 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5897 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5898 break;
1042e4c0 5899 case bp_tracepoint:
7a697b8d 5900 case bp_fast_tracepoint:
0fb4aa4b 5901 case bp_static_tracepoint:
1042e4c0
SS
5902 /* Tracepoint hits should not be reported back to GDB, and
5903 if one got through somehow, it should have been filtered
5904 out already. */
5905 internal_error (__FILE__, __LINE__,
7a697b8d 5906 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5907 break;
5908 case bp_gnu_ifunc_resolver:
5909 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5910 this_action = BPSTAT_WHAT_SINGLE;
5911 break;
5912 case bp_gnu_ifunc_resolver_return:
5913 /* The breakpoint will be removed, execution will restart from the
5914 PC of the former breakpoint. */
5915 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5916 break;
e7e0cddf
SS
5917
5918 case bp_dprintf:
a11cfd87
HZ
5919 if (bs->stop)
5920 this_action = BPSTAT_WHAT_STOP_SILENT;
5921 else
5922 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5923 break;
5924
628fe4e4
JK
5925 default:
5926 internal_error (__FILE__, __LINE__,
5927 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5928 }
628fe4e4
JK
5929
5930 retval.main_action = max (retval.main_action, this_action);
c906108c 5931 }
628fe4e4 5932
243a9253
PA
5933 return retval;
5934}
628fe4e4 5935
243a9253
PA
5936void
5937bpstat_run_callbacks (bpstat bs_head)
5938{
5939 bpstat bs;
628fe4e4 5940
0e30163f
JK
5941 for (bs = bs_head; bs != NULL; bs = bs->next)
5942 {
5943 struct breakpoint *b = bs->breakpoint_at;
5944
5945 if (b == NULL)
5946 continue;
5947 switch (b->type)
5948 {
243a9253
PA
5949 case bp_jit_event:
5950 handle_jit_event ();
5951 break;
0e30163f
JK
5952 case bp_gnu_ifunc_resolver:
5953 gnu_ifunc_resolver_stop (b);
5954 break;
5955 case bp_gnu_ifunc_resolver_return:
5956 gnu_ifunc_resolver_return_stop (b);
5957 break;
5958 }
5959 }
c906108c
SS
5960}
5961
5962/* Nonzero if we should step constantly (e.g. watchpoints on machines
5963 without hardware support). This isn't related to a specific bpstat,
5964 just to things like whether watchpoints are set. */
5965
c5aa993b 5966int
fba45db2 5967bpstat_should_step (void)
c906108c
SS
5968{
5969 struct breakpoint *b;
cc59ec59 5970
c906108c 5971 ALL_BREAKPOINTS (b)
717a8278 5972 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5973 return 1;
c906108c
SS
5974 return 0;
5975}
5976
67822962
PA
5977int
5978bpstat_causes_stop (bpstat bs)
5979{
5980 for (; bs != NULL; bs = bs->next)
5981 if (bs->stop)
5982 return 1;
5983
5984 return 0;
5985}
5986
c906108c 5987\f
c5aa993b 5988
170b53b2
UW
5989/* Compute a string of spaces suitable to indent the next line
5990 so it starts at the position corresponding to the table column
5991 named COL_NAME in the currently active table of UIOUT. */
5992
5993static char *
5994wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5995{
5996 static char wrap_indent[80];
5997 int i, total_width, width, align;
5998 char *text;
5999
6000 total_width = 0;
6001 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6002 {
6003 if (strcmp (text, col_name) == 0)
6004 {
6005 gdb_assert (total_width < sizeof wrap_indent);
6006 memset (wrap_indent, ' ', total_width);
6007 wrap_indent[total_width] = 0;
6008
6009 return wrap_indent;
6010 }
6011
6012 total_width += width + 1;
6013 }
6014
6015 return NULL;
6016}
6017
b775012e
LM
6018/* Determine if the locations of this breakpoint will have their conditions
6019 evaluated by the target, host or a mix of both. Returns the following:
6020
6021 "host": Host evals condition.
6022 "host or target": Host or Target evals condition.
6023 "target": Target evals condition.
6024*/
6025
6026static const char *
6027bp_condition_evaluator (struct breakpoint *b)
6028{
6029 struct bp_location *bl;
6030 char host_evals = 0;
6031 char target_evals = 0;
6032
6033 if (!b)
6034 return NULL;
6035
6036 if (!is_breakpoint (b))
6037 return NULL;
6038
6039 if (gdb_evaluates_breakpoint_condition_p ()
6040 || !target_supports_evaluation_of_breakpoint_conditions ())
6041 return condition_evaluation_host;
6042
6043 for (bl = b->loc; bl; bl = bl->next)
6044 {
6045 if (bl->cond_bytecode)
6046 target_evals++;
6047 else
6048 host_evals++;
6049 }
6050
6051 if (host_evals && target_evals)
6052 return condition_evaluation_both;
6053 else if (target_evals)
6054 return condition_evaluation_target;
6055 else
6056 return condition_evaluation_host;
6057}
6058
6059/* Determine the breakpoint location's condition evaluator. This is
6060 similar to bp_condition_evaluator, but for locations. */
6061
6062static const char *
6063bp_location_condition_evaluator (struct bp_location *bl)
6064{
6065 if (bl && !is_breakpoint (bl->owner))
6066 return NULL;
6067
6068 if (gdb_evaluates_breakpoint_condition_p ()
6069 || !target_supports_evaluation_of_breakpoint_conditions ())
6070 return condition_evaluation_host;
6071
6072 if (bl && bl->cond_bytecode)
6073 return condition_evaluation_target;
6074 else
6075 return condition_evaluation_host;
6076}
6077
859825b8
JK
6078/* Print the LOC location out of the list of B->LOC locations. */
6079
170b53b2
UW
6080static void
6081print_breakpoint_location (struct breakpoint *b,
6082 struct bp_location *loc)
0d381245 6083{
79a45e25 6084 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6085 struct cleanup *old_chain = save_current_program_space ();
6086
859825b8
JK
6087 if (loc != NULL && loc->shlib_disabled)
6088 loc = NULL;
6089
6c95b8df
PA
6090 if (loc != NULL)
6091 set_current_program_space (loc->pspace);
6092
56435ebe 6093 if (b->display_canonical)
f00aae0f
KS
6094 ui_out_field_string (uiout, "what",
6095 event_location_to_string (b->location));
2f202fde 6096 else if (loc && loc->symtab)
0d381245
VP
6097 {
6098 struct symbol *sym
6099 = find_pc_sect_function (loc->address, loc->section);
6100 if (sym)
6101 {
6102 ui_out_text (uiout, "in ");
6103 ui_out_field_string (uiout, "func",
6104 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6105 ui_out_text (uiout, " ");
6106 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6107 ui_out_text (uiout, "at ");
0d381245 6108 }
05cba821
JK
6109 ui_out_field_string (uiout, "file",
6110 symtab_to_filename_for_display (loc->symtab));
0d381245 6111 ui_out_text (uiout, ":");
05cba821 6112
0d381245 6113 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6114 ui_out_field_string (uiout, "fullname",
6115 symtab_to_fullname (loc->symtab));
0d381245 6116
f8eba3c6 6117 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6118 }
859825b8 6119 else if (loc)
0d381245 6120 {
f99d8bf4
PA
6121 struct ui_file *stb = mem_fileopen ();
6122 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6123
f99d8bf4 6124 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6125 demangle, "");
0d381245 6126 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6127
6128 do_cleanups (stb_chain);
0d381245 6129 }
859825b8 6130 else
f00aae0f
KS
6131 {
6132 ui_out_field_string (uiout, "pending",
6133 event_location_to_string (b->location));
6134 /* If extra_string is available, it could be holding a condition
6135 or dprintf arguments. In either case, make sure it is printed,
6136 too, but only for non-MI streams. */
6137 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6138 {
6139 if (b->type == bp_dprintf)
6140 ui_out_text (uiout, ",");
6141 else
6142 ui_out_text (uiout, " ");
6143 ui_out_text (uiout, b->extra_string);
6144 }
6145 }
6c95b8df 6146
b775012e
LM
6147 if (loc && is_breakpoint (b)
6148 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6149 && bp_condition_evaluator (b) == condition_evaluation_both)
6150 {
6151 ui_out_text (uiout, " (");
6152 ui_out_field_string (uiout, "evaluated-by",
6153 bp_location_condition_evaluator (loc));
6154 ui_out_text (uiout, ")");
6155 }
6156
6c95b8df 6157 do_cleanups (old_chain);
0d381245
VP
6158}
6159
269b11a2
PA
6160static const char *
6161bptype_string (enum bptype type)
c906108c 6162{
c4093a6a
JM
6163 struct ep_type_description
6164 {
6165 enum bptype type;
6166 char *description;
6167 };
6168 static struct ep_type_description bptypes[] =
c906108c 6169 {
c5aa993b
JM
6170 {bp_none, "?deleted?"},
6171 {bp_breakpoint, "breakpoint"},
c906108c 6172 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6173 {bp_single_step, "sw single-step"},
c5aa993b
JM
6174 {bp_until, "until"},
6175 {bp_finish, "finish"},
6176 {bp_watchpoint, "watchpoint"},
c906108c 6177 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6178 {bp_read_watchpoint, "read watchpoint"},
6179 {bp_access_watchpoint, "acc watchpoint"},
6180 {bp_longjmp, "longjmp"},
6181 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6182 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6183 {bp_exception, "exception"},
6184 {bp_exception_resume, "exception resume"},
c5aa993b 6185 {bp_step_resume, "step resume"},
2c03e5be 6186 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6187 {bp_watchpoint_scope, "watchpoint scope"},
6188 {bp_call_dummy, "call dummy"},
aa7d318d 6189 {bp_std_terminate, "std::terminate"},
c5aa993b 6190 {bp_shlib_event, "shlib events"},
c4093a6a 6191 {bp_thread_event, "thread events"},
1900040c 6192 {bp_overlay_event, "overlay events"},
0fd8e87f 6193 {bp_longjmp_master, "longjmp master"},
aa7d318d 6194 {bp_std_terminate_master, "std::terminate master"},
186c406b 6195 {bp_exception_master, "exception master"},
ce78b96d 6196 {bp_catchpoint, "catchpoint"},
1042e4c0 6197 {bp_tracepoint, "tracepoint"},
7a697b8d 6198 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6199 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6200 {bp_dprintf, "dprintf"},
4efc6507 6201 {bp_jit_event, "jit events"},
0e30163f
JK
6202 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6203 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6204 };
269b11a2
PA
6205
6206 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6207 || ((int) type != bptypes[(int) type].type))
6208 internal_error (__FILE__, __LINE__,
6209 _("bptypes table does not describe type #%d."),
6210 (int) type);
6211
6212 return bptypes[(int) type].description;
6213}
6214
998580f1
MK
6215/* For MI, output a field named 'thread-groups' with a list as the value.
6216 For CLI, prefix the list with the string 'inf'. */
6217
6218static void
6219output_thread_groups (struct ui_out *uiout,
6220 const char *field_name,
6221 VEC(int) *inf_num,
6222 int mi_only)
6223{
752eb8b4 6224 struct cleanup *back_to;
998580f1
MK
6225 int is_mi = ui_out_is_mi_like_p (uiout);
6226 int inf;
6227 int i;
6228
6229 /* For backward compatibility, don't display inferiors in CLI unless
6230 there are several. Always display them for MI. */
6231 if (!is_mi && mi_only)
6232 return;
6233
752eb8b4
TT
6234 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6235
998580f1
MK
6236 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6237 {
6238 if (is_mi)
6239 {
6240 char mi_group[10];
6241
6242 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6243 ui_out_field_string (uiout, NULL, mi_group);
6244 }
6245 else
6246 {
6247 if (i == 0)
6248 ui_out_text (uiout, " inf ");
6249 else
6250 ui_out_text (uiout, ", ");
6251
6252 ui_out_text (uiout, plongest (inf));
6253 }
6254 }
6255
6256 do_cleanups (back_to);
6257}
6258
269b11a2
PA
6259/* Print B to gdb_stdout. */
6260
6261static void
6262print_one_breakpoint_location (struct breakpoint *b,
6263 struct bp_location *loc,
6264 int loc_number,
6265 struct bp_location **last_loc,
269b11a2
PA
6266 int allflag)
6267{
6268 struct command_line *l;
c2c6d25f 6269 static char bpenables[] = "nynny";
c906108c 6270
79a45e25 6271 struct ui_out *uiout = current_uiout;
0d381245
VP
6272 int header_of_multiple = 0;
6273 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6274 struct value_print_options opts;
6275
6276 get_user_print_options (&opts);
0d381245
VP
6277
6278 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6279 /* See comment in print_one_breakpoint concerning treatment of
6280 breakpoints with single disabled location. */
0d381245
VP
6281 if (loc == NULL
6282 && (b->loc != NULL
6283 && (b->loc->next != NULL || !b->loc->enabled)))
6284 header_of_multiple = 1;
6285 if (loc == NULL)
6286 loc = b->loc;
6287
c4093a6a
JM
6288 annotate_record ();
6289
6290 /* 1 */
6291 annotate_field (0);
0d381245
VP
6292 if (part_of_multiple)
6293 {
6294 char *formatted;
0c6773c1 6295 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6296 ui_out_field_string (uiout, "number", formatted);
6297 xfree (formatted);
6298 }
6299 else
6300 {
6301 ui_out_field_int (uiout, "number", b->number);
6302 }
c4093a6a
JM
6303
6304 /* 2 */
6305 annotate_field (1);
0d381245
VP
6306 if (part_of_multiple)
6307 ui_out_field_skip (uiout, "type");
269b11a2
PA
6308 else
6309 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6310
6311 /* 3 */
6312 annotate_field (2);
0d381245
VP
6313 if (part_of_multiple)
6314 ui_out_field_skip (uiout, "disp");
6315 else
2cec12e5 6316 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6317
c4093a6a
JM
6318
6319 /* 4 */
6320 annotate_field (3);
0d381245 6321 if (part_of_multiple)
54e52265 6322 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6323 else
4a64f543
MS
6324 ui_out_field_fmt (uiout, "enabled", "%c",
6325 bpenables[(int) b->enable_state]);
54e52265 6326 ui_out_spaces (uiout, 2);
0d381245 6327
c4093a6a
JM
6328
6329 /* 5 and 6 */
3086aeae 6330 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6331 {
4a64f543
MS
6332 /* Although the print_one can possibly print all locations,
6333 calling it here is not likely to get any nice result. So,
6334 make sure there's just one location. */
0d381245 6335 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6336 b->ops->print_one (b, last_loc);
0d381245 6337 }
3086aeae
DJ
6338 else
6339 switch (b->type)
6340 {
6341 case bp_none:
6342 internal_error (__FILE__, __LINE__,
e2e0b3e5 6343 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6344 break;
c906108c 6345
3086aeae
DJ
6346 case bp_watchpoint:
6347 case bp_hardware_watchpoint:
6348 case bp_read_watchpoint:
6349 case bp_access_watchpoint:
3a5c3e22
PA
6350 {
6351 struct watchpoint *w = (struct watchpoint *) b;
6352
6353 /* Field 4, the address, is omitted (which makes the columns
6354 not line up too nicely with the headers, but the effect
6355 is relatively readable). */
6356 if (opts.addressprint)
6357 ui_out_field_skip (uiout, "addr");
6358 annotate_field (5);
6359 ui_out_field_string (uiout, "what", w->exp_string);
6360 }
3086aeae
DJ
6361 break;
6362
3086aeae
DJ
6363 case bp_breakpoint:
6364 case bp_hardware_breakpoint:
7c16b83e 6365 case bp_single_step:
3086aeae
DJ
6366 case bp_until:
6367 case bp_finish:
6368 case bp_longjmp:
6369 case bp_longjmp_resume:
e2e4d78b 6370 case bp_longjmp_call_dummy:
186c406b
TT
6371 case bp_exception:
6372 case bp_exception_resume:
3086aeae 6373 case bp_step_resume:
2c03e5be 6374 case bp_hp_step_resume:
3086aeae
DJ
6375 case bp_watchpoint_scope:
6376 case bp_call_dummy:
aa7d318d 6377 case bp_std_terminate:
3086aeae
DJ
6378 case bp_shlib_event:
6379 case bp_thread_event:
6380 case bp_overlay_event:
0fd8e87f 6381 case bp_longjmp_master:
aa7d318d 6382 case bp_std_terminate_master:
186c406b 6383 case bp_exception_master:
1042e4c0 6384 case bp_tracepoint:
7a697b8d 6385 case bp_fast_tracepoint:
0fb4aa4b 6386 case bp_static_tracepoint:
e7e0cddf 6387 case bp_dprintf:
4efc6507 6388 case bp_jit_event:
0e30163f
JK
6389 case bp_gnu_ifunc_resolver:
6390 case bp_gnu_ifunc_resolver_return:
79a45b7d 6391 if (opts.addressprint)
3086aeae
DJ
6392 {
6393 annotate_field (4);
54e52265 6394 if (header_of_multiple)
0d381245 6395 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6396 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6397 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6398 else
5af949e3
UW
6399 ui_out_field_core_addr (uiout, "addr",
6400 loc->gdbarch, loc->address);
3086aeae
DJ
6401 }
6402 annotate_field (5);
0d381245 6403 if (!header_of_multiple)
170b53b2 6404 print_breakpoint_location (b, loc);
0d381245 6405 if (b->loc)
a6d9a66e 6406 *last_loc = b->loc;
3086aeae
DJ
6407 break;
6408 }
c906108c 6409
6c95b8df 6410
998580f1 6411 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6412 {
6413 struct inferior *inf;
998580f1
MK
6414 VEC(int) *inf_num = NULL;
6415 int mi_only = 1;
6c95b8df 6416
998580f1 6417 ALL_INFERIORS (inf)
6c95b8df
PA
6418 {
6419 if (inf->pspace == loc->pspace)
998580f1 6420 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6421 }
998580f1
MK
6422
6423 /* For backward compatibility, don't display inferiors in CLI unless
6424 there are several. Always display for MI. */
6425 if (allflag
6426 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6427 && (number_of_program_spaces () > 1
6428 || number_of_inferiors () > 1)
6429 /* LOC is for existing B, it cannot be in
6430 moribund_locations and thus having NULL OWNER. */
6431 && loc->owner->type != bp_catchpoint))
6432 mi_only = 0;
6433 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6434 VEC_free (int, inf_num);
6c95b8df
PA
6435 }
6436
4a306c9a 6437 if (!part_of_multiple)
c4093a6a 6438 {
4a306c9a
JB
6439 if (b->thread != -1)
6440 {
6441 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6442 "stop only in" line a little further down. */
4a306c9a
JB
6443 ui_out_text (uiout, " thread ");
6444 ui_out_field_int (uiout, "thread", b->thread);
6445 }
6446 else if (b->task != 0)
6447 {
6448 ui_out_text (uiout, " task ");
6449 ui_out_field_int (uiout, "task", b->task);
6450 }
c4093a6a 6451 }
f1310107 6452
8b93c638 6453 ui_out_text (uiout, "\n");
f1310107 6454
348d480f 6455 if (!part_of_multiple)
f1310107
TJB
6456 b->ops->print_one_detail (b, uiout);
6457
0d381245 6458 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6459 {
6460 annotate_field (6);
8b93c638 6461 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6462 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6463 the frame ID. */
5af949e3
UW
6464 ui_out_field_core_addr (uiout, "frame",
6465 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6466 ui_out_text (uiout, "\n");
c4093a6a
JM
6467 }
6468
28010a5d 6469 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6470 {
6471 annotate_field (7);
d77f58be 6472 if (is_tracepoint (b))
1042e4c0
SS
6473 ui_out_text (uiout, "\ttrace only if ");
6474 else
6475 ui_out_text (uiout, "\tstop only if ");
0101ce28 6476 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6477
6478 /* Print whether the target is doing the breakpoint's condition
6479 evaluation. If GDB is doing the evaluation, don't print anything. */
6480 if (is_breakpoint (b)
6481 && breakpoint_condition_evaluation_mode ()
6482 == condition_evaluation_target)
6483 {
6484 ui_out_text (uiout, " (");
6485 ui_out_field_string (uiout, "evaluated-by",
6486 bp_condition_evaluator (b));
6487 ui_out_text (uiout, " evals)");
6488 }
0101ce28
JJ
6489 ui_out_text (uiout, "\n");
6490 }
6491
0d381245 6492 if (!part_of_multiple && b->thread != -1)
c4093a6a 6493 {
4a64f543 6494 /* FIXME should make an annotation for this. */
8b93c638
JM
6495 ui_out_text (uiout, "\tstop only in thread ");
6496 ui_out_field_int (uiout, "thread", b->thread);
6497 ui_out_text (uiout, "\n");
c4093a6a
JM
6498 }
6499
556ec64d
YQ
6500 if (!part_of_multiple)
6501 {
6502 if (b->hit_count)
31f56a27
YQ
6503 {
6504 /* FIXME should make an annotation for this. */
6505 if (is_catchpoint (b))
6506 ui_out_text (uiout, "\tcatchpoint");
6507 else if (is_tracepoint (b))
6508 ui_out_text (uiout, "\ttracepoint");
6509 else
6510 ui_out_text (uiout, "\tbreakpoint");
6511 ui_out_text (uiout, " already hit ");
6512 ui_out_field_int (uiout, "times", b->hit_count);
6513 if (b->hit_count == 1)
6514 ui_out_text (uiout, " time\n");
6515 else
6516 ui_out_text (uiout, " times\n");
6517 }
556ec64d
YQ
6518 else
6519 {
31f56a27
YQ
6520 /* Output the count also if it is zero, but only if this is mi. */
6521 if (ui_out_is_mi_like_p (uiout))
6522 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6523 }
6524 }
8b93c638 6525
0d381245 6526 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6527 {
6528 annotate_field (8);
8b93c638
JM
6529 ui_out_text (uiout, "\tignore next ");
6530 ui_out_field_int (uiout, "ignore", b->ignore_count);
6531 ui_out_text (uiout, " hits\n");
c4093a6a 6532 }
059fb39f 6533
816338b5
SS
6534 /* Note that an enable count of 1 corresponds to "enable once"
6535 behavior, which is reported by the combination of enablement and
6536 disposition, so we don't need to mention it here. */
6537 if (!part_of_multiple && b->enable_count > 1)
6538 {
6539 annotate_field (8);
6540 ui_out_text (uiout, "\tdisable after ");
6541 /* Tweak the wording to clarify that ignore and enable counts
6542 are distinct, and have additive effect. */
6543 if (b->ignore_count)
6544 ui_out_text (uiout, "additional ");
6545 else
6546 ui_out_text (uiout, "next ");
6547 ui_out_field_int (uiout, "enable", b->enable_count);
6548 ui_out_text (uiout, " hits\n");
6549 }
6550
f196051f
SS
6551 if (!part_of_multiple && is_tracepoint (b))
6552 {
6553 struct tracepoint *tp = (struct tracepoint *) b;
6554
6555 if (tp->traceframe_usage)
6556 {
6557 ui_out_text (uiout, "\ttrace buffer usage ");
6558 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6559 ui_out_text (uiout, " bytes\n");
6560 }
6561 }
d3ce09f5 6562
9add0f1b 6563 l = b->commands ? b->commands->commands : NULL;
059fb39f 6564 if (!part_of_multiple && l)
c4093a6a 6565 {
3b31d625
EZ
6566 struct cleanup *script_chain;
6567
c4093a6a 6568 annotate_field (9);
3b31d625 6569 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6570 print_command_lines (uiout, l, 4);
3b31d625 6571 do_cleanups (script_chain);
c4093a6a 6572 }
d24317b4 6573
d9b3f62e 6574 if (is_tracepoint (b))
1042e4c0 6575 {
d9b3f62e
PA
6576 struct tracepoint *t = (struct tracepoint *) b;
6577
6578 if (!part_of_multiple && t->pass_count)
6579 {
6580 annotate_field (10);
6581 ui_out_text (uiout, "\tpass count ");
6582 ui_out_field_int (uiout, "pass", t->pass_count);
6583 ui_out_text (uiout, " \n");
6584 }
f2a8bc8a
YQ
6585
6586 /* Don't display it when tracepoint or tracepoint location is
6587 pending. */
6588 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6589 {
6590 annotate_field (11);
6591
6592 if (ui_out_is_mi_like_p (uiout))
6593 ui_out_field_string (uiout, "installed",
6594 loc->inserted ? "y" : "n");
6595 else
6596 {
6597 if (loc->inserted)
6598 ui_out_text (uiout, "\t");
6599 else
6600 ui_out_text (uiout, "\tnot ");
6601 ui_out_text (uiout, "installed on target\n");
6602 }
6603 }
1042e4c0
SS
6604 }
6605
d24317b4
VP
6606 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6607 {
3a5c3e22
PA
6608 if (is_watchpoint (b))
6609 {
6610 struct watchpoint *w = (struct watchpoint *) b;
6611
6612 ui_out_field_string (uiout, "original-location", w->exp_string);
6613 }
f00aae0f
KS
6614 else if (b->location != NULL
6615 && event_location_to_string (b->location) != NULL)
6616 ui_out_field_string (uiout, "original-location",
6617 event_location_to_string (b->location));
d24317b4 6618 }
c4093a6a 6619}
c5aa993b 6620
0d381245
VP
6621static void
6622print_one_breakpoint (struct breakpoint *b,
4a64f543 6623 struct bp_location **last_loc,
6c95b8df 6624 int allflag)
0d381245 6625{
8d3788bd 6626 struct cleanup *bkpt_chain;
79a45e25 6627 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6628
6629 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6630
12c5a436 6631 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6632 do_cleanups (bkpt_chain);
0d381245
VP
6633
6634 /* If this breakpoint has custom print function,
6635 it's already printed. Otherwise, print individual
6636 locations, if any. */
6637 if (b->ops == NULL || b->ops->print_one == NULL)
6638 {
4a64f543
MS
6639 /* If breakpoint has a single location that is disabled, we
6640 print it as if it had several locations, since otherwise it's
6641 hard to represent "breakpoint enabled, location disabled"
6642 situation.
6643
6644 Note that while hardware watchpoints have several locations
a3be7890 6645 internally, that's not a property exposed to user. */
0d381245 6646 if (b->loc
a5606eee 6647 && !is_hardware_watchpoint (b)
8d3788bd 6648 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6649 {
6650 struct bp_location *loc;
6651 int n = 1;
8d3788bd 6652
0d381245 6653 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6654 {
6655 struct cleanup *inner2 =
6656 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6657 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6658 do_cleanups (inner2);
6659 }
0d381245
VP
6660 }
6661 }
6662}
6663
a6d9a66e
UW
6664static int
6665breakpoint_address_bits (struct breakpoint *b)
6666{
6667 int print_address_bits = 0;
6668 struct bp_location *loc;
6669
6670 for (loc = b->loc; loc; loc = loc->next)
6671 {
c7437ca6
PA
6672 int addr_bit;
6673
6674 /* Software watchpoints that aren't watching memory don't have
6675 an address to print. */
6676 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6677 continue;
6678
6679 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6680 if (addr_bit > print_address_bits)
6681 print_address_bits = addr_bit;
6682 }
6683
6684 return print_address_bits;
6685}
0d381245 6686
c4093a6a
JM
6687struct captured_breakpoint_query_args
6688 {
6689 int bnum;
6690 };
c5aa993b 6691
c4093a6a 6692static int
2b65245e 6693do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6694{
9a3c8263
SM
6695 struct captured_breakpoint_query_args *args
6696 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6697 struct breakpoint *b;
a6d9a66e 6698 struct bp_location *dummy_loc = NULL;
cc59ec59 6699
c4093a6a
JM
6700 ALL_BREAKPOINTS (b)
6701 {
6702 if (args->bnum == b->number)
c5aa993b 6703 {
12c5a436 6704 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6705 return GDB_RC_OK;
c5aa993b 6706 }
c4093a6a
JM
6707 }
6708 return GDB_RC_NONE;
6709}
c5aa993b 6710
c4093a6a 6711enum gdb_rc
4a64f543
MS
6712gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6713 char **error_message)
c4093a6a
JM
6714{
6715 struct captured_breakpoint_query_args args;
cc59ec59 6716
c4093a6a
JM
6717 args.bnum = bnum;
6718 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6719 an error. */
b0b13bb4
DJ
6720 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6721 error_message, RETURN_MASK_ALL) < 0)
6722 return GDB_RC_FAIL;
6723 else
6724 return GDB_RC_OK;
c4093a6a 6725}
c5aa993b 6726
09d682a4
TT
6727/* Return true if this breakpoint was set by the user, false if it is
6728 internal or momentary. */
6729
6730int
6731user_breakpoint_p (struct breakpoint *b)
6732{
46c6471b 6733 return b->number > 0;
09d682a4
TT
6734}
6735
7f3b0473 6736/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6737 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6738 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6739 FILTER is non-NULL, call it on each breakpoint and only include the
6740 ones for which it returns non-zero. Return the total number of
6741 breakpoints listed. */
c906108c 6742
d77f58be 6743static int
e5a67952 6744breakpoint_1 (char *args, int allflag,
4a64f543 6745 int (*filter) (const struct breakpoint *))
c4093a6a 6746{
52f0bd74 6747 struct breakpoint *b;
a6d9a66e 6748 struct bp_location *last_loc = NULL;
7f3b0473 6749 int nr_printable_breakpoints;
3b31d625 6750 struct cleanup *bkpttbl_chain;
79a45b7d 6751 struct value_print_options opts;
a6d9a66e 6752 int print_address_bits = 0;
269b11a2 6753 int print_type_col_width = 14;
79a45e25 6754 struct ui_out *uiout = current_uiout;
269b11a2 6755
79a45b7d
TT
6756 get_user_print_options (&opts);
6757
4a64f543
MS
6758 /* Compute the number of rows in the table, as well as the size
6759 required for address fields. */
7f3b0473
AC
6760 nr_printable_breakpoints = 0;
6761 ALL_BREAKPOINTS (b)
e5a67952
MS
6762 {
6763 /* If we have a filter, only list the breakpoints it accepts. */
6764 if (filter && !filter (b))
6765 continue;
6766
6767 /* If we have an "args" string, it is a list of breakpoints to
6768 accept. Skip the others. */
6769 if (args != NULL && *args != '\0')
6770 {
6771 if (allflag && parse_and_eval_long (args) != b->number)
6772 continue;
6773 if (!allflag && !number_is_in_list (args, b->number))
6774 continue;
6775 }
269b11a2 6776
e5a67952
MS
6777 if (allflag || user_breakpoint_p (b))
6778 {
6779 int addr_bit, type_len;
a6d9a66e 6780
e5a67952
MS
6781 addr_bit = breakpoint_address_bits (b);
6782 if (addr_bit > print_address_bits)
6783 print_address_bits = addr_bit;
269b11a2 6784
e5a67952
MS
6785 type_len = strlen (bptype_string (b->type));
6786 if (type_len > print_type_col_width)
6787 print_type_col_width = type_len;
6788
6789 nr_printable_breakpoints++;
6790 }
6791 }
7f3b0473 6792
79a45b7d 6793 if (opts.addressprint)
3b31d625 6794 bkpttbl_chain
3e43a32a
MS
6795 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6796 nr_printable_breakpoints,
3b31d625 6797 "BreakpointTable");
8b93c638 6798 else
3b31d625 6799 bkpttbl_chain
3e43a32a
MS
6800 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6801 nr_printable_breakpoints,
3b31d625 6802 "BreakpointTable");
8b93c638 6803
7f3b0473 6804 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6805 annotate_breakpoints_headers ();
6806 if (nr_printable_breakpoints > 0)
6807 annotate_field (0);
4a64f543 6808 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6809 if (nr_printable_breakpoints > 0)
6810 annotate_field (1);
269b11a2 6811 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6812 "type", "Type"); /* 2 */
d7faa9e7
AC
6813 if (nr_printable_breakpoints > 0)
6814 annotate_field (2);
4a64f543 6815 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6816 if (nr_printable_breakpoints > 0)
6817 annotate_field (3);
54e52265 6818 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6819 if (opts.addressprint)
e5a67952
MS
6820 {
6821 if (nr_printable_breakpoints > 0)
6822 annotate_field (4);
6823 if (print_address_bits <= 32)
6824 ui_out_table_header (uiout, 10, ui_left,
6825 "addr", "Address"); /* 5 */
6826 else
6827 ui_out_table_header (uiout, 18, ui_left,
6828 "addr", "Address"); /* 5 */
6829 }
d7faa9e7
AC
6830 if (nr_printable_breakpoints > 0)
6831 annotate_field (5);
6832 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6833 ui_out_table_body (uiout);
6834 if (nr_printable_breakpoints > 0)
6835 annotate_breakpoints_table ();
7f3b0473 6836
c4093a6a 6837 ALL_BREAKPOINTS (b)
e5a67952
MS
6838 {
6839 QUIT;
6840 /* If we have a filter, only list the breakpoints it accepts. */
6841 if (filter && !filter (b))
6842 continue;
6843
6844 /* If we have an "args" string, it is a list of breakpoints to
6845 accept. Skip the others. */
6846
6847 if (args != NULL && *args != '\0')
6848 {
6849 if (allflag) /* maintenance info breakpoint */
6850 {
6851 if (parse_and_eval_long (args) != b->number)
6852 continue;
6853 }
6854 else /* all others */
6855 {
6856 if (!number_is_in_list (args, b->number))
6857 continue;
6858 }
6859 }
6860 /* We only print out user settable breakpoints unless the
6861 allflag is set. */
6862 if (allflag || user_breakpoint_p (b))
12c5a436 6863 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6864 }
6865
3b31d625 6866 do_cleanups (bkpttbl_chain);
698384cd 6867
7f3b0473 6868 if (nr_printable_breakpoints == 0)
c906108c 6869 {
4a64f543
MS
6870 /* If there's a filter, let the caller decide how to report
6871 empty list. */
d77f58be
SS
6872 if (!filter)
6873 {
e5a67952 6874 if (args == NULL || *args == '\0')
d77f58be
SS
6875 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6876 else
4a64f543 6877 ui_out_message (uiout, 0,
e5a67952
MS
6878 "No breakpoint or watchpoint matching '%s'.\n",
6879 args);
d77f58be 6880 }
c906108c
SS
6881 }
6882 else
c4093a6a 6883 {
a6d9a66e
UW
6884 if (last_loc && !server_command)
6885 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6886 }
c906108c 6887
4a64f543 6888 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6889 there have been breakpoints? */
c906108c 6890 annotate_breakpoints_table_end ();
d77f58be
SS
6891
6892 return nr_printable_breakpoints;
c906108c
SS
6893}
6894
ad443146
SS
6895/* Display the value of default-collect in a way that is generally
6896 compatible with the breakpoint list. */
6897
6898static void
6899default_collect_info (void)
6900{
79a45e25
PA
6901 struct ui_out *uiout = current_uiout;
6902
ad443146
SS
6903 /* If it has no value (which is frequently the case), say nothing; a
6904 message like "No default-collect." gets in user's face when it's
6905 not wanted. */
6906 if (!*default_collect)
6907 return;
6908
6909 /* The following phrase lines up nicely with per-tracepoint collect
6910 actions. */
6911 ui_out_text (uiout, "default collect ");
6912 ui_out_field_string (uiout, "default-collect", default_collect);
6913 ui_out_text (uiout, " \n");
6914}
6915
c906108c 6916static void
e5a67952 6917breakpoints_info (char *args, int from_tty)
c906108c 6918{
e5a67952 6919 breakpoint_1 (args, 0, NULL);
ad443146
SS
6920
6921 default_collect_info ();
d77f58be
SS
6922}
6923
6924static void
e5a67952 6925watchpoints_info (char *args, int from_tty)
d77f58be 6926{
e5a67952 6927 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6928 struct ui_out *uiout = current_uiout;
d77f58be
SS
6929
6930 if (num_printed == 0)
6931 {
e5a67952 6932 if (args == NULL || *args == '\0')
d77f58be
SS
6933 ui_out_message (uiout, 0, "No watchpoints.\n");
6934 else
e5a67952 6935 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6936 }
c906108c
SS
6937}
6938
7a292a7a 6939static void
e5a67952 6940maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6941{
e5a67952 6942 breakpoint_1 (args, 1, NULL);
ad443146
SS
6943
6944 default_collect_info ();
c906108c
SS
6945}
6946
0d381245 6947static int
714835d5 6948breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6949 struct program_space *pspace,
714835d5 6950 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6951{
6952 struct bp_location *bl = b->loc;
cc59ec59 6953
0d381245
VP
6954 for (; bl; bl = bl->next)
6955 {
6c95b8df
PA
6956 if (bl->pspace == pspace
6957 && bl->address == pc
0d381245
VP
6958 && (!overlay_debugging || bl->section == section))
6959 return 1;
6960 }
6961 return 0;
6962}
6963
672f9b60 6964/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6965 concerns with logical breakpoints, so we match program spaces, not
6966 address spaces. */
c906108c
SS
6967
6968static void
6c95b8df
PA
6969describe_other_breakpoints (struct gdbarch *gdbarch,
6970 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6971 struct obj_section *section, int thread)
c906108c 6972{
52f0bd74
AC
6973 int others = 0;
6974 struct breakpoint *b;
c906108c
SS
6975
6976 ALL_BREAKPOINTS (b)
672f9b60
KP
6977 others += (user_breakpoint_p (b)
6978 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6979 if (others > 0)
6980 {
a3f17187
AC
6981 if (others == 1)
6982 printf_filtered (_("Note: breakpoint "));
6983 else /* if (others == ???) */
6984 printf_filtered (_("Note: breakpoints "));
c906108c 6985 ALL_BREAKPOINTS (b)
672f9b60 6986 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6987 {
6988 others--;
6989 printf_filtered ("%d", b->number);
6990 if (b->thread == -1 && thread != -1)
6991 printf_filtered (" (all threads)");
6992 else if (b->thread != -1)
6993 printf_filtered (" (thread %d)", b->thread);
6994 printf_filtered ("%s%s ",
059fb39f 6995 ((b->enable_state == bp_disabled
f8eba3c6 6996 || b->enable_state == bp_call_disabled)
0d381245 6997 ? " (disabled)"
0d381245
VP
6998 : ""),
6999 (others > 1) ? ","
7000 : ((others == 1) ? " and" : ""));
7001 }
a3f17187 7002 printf_filtered (_("also set at pc "));
5af949e3 7003 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7004 printf_filtered (".\n");
7005 }
7006}
7007\f
c906108c 7008
e4f237da 7009/* Return true iff it is meaningful to use the address member of
244558af
LM
7010 BPT locations. For some breakpoint types, the locations' address members
7011 are irrelevant and it makes no sense to attempt to compare them to other
7012 addresses (or use them for any other purpose either).
e4f237da 7013
4a64f543 7014 More specifically, each of the following breakpoint types will
244558af 7015 always have a zero valued location address and we don't want to mark
4a64f543 7016 breakpoints of any of these types to be a duplicate of an actual
244558af 7017 breakpoint location at address zero:
e4f237da
KB
7018
7019 bp_watchpoint
2d134ed3
PA
7020 bp_catchpoint
7021
7022*/
e4f237da
KB
7023
7024static int
7025breakpoint_address_is_meaningful (struct breakpoint *bpt)
7026{
7027 enum bptype type = bpt->type;
7028
2d134ed3
PA
7029 return (type != bp_watchpoint && type != bp_catchpoint);
7030}
7031
7032/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7033 true if LOC1 and LOC2 represent the same watchpoint location. */
7034
7035static int
4a64f543
MS
7036watchpoint_locations_match (struct bp_location *loc1,
7037 struct bp_location *loc2)
2d134ed3 7038{
3a5c3e22
PA
7039 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7040 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7041
7042 /* Both of them must exist. */
7043 gdb_assert (w1 != NULL);
7044 gdb_assert (w2 != NULL);
2bdf28a0 7045
4a64f543
MS
7046 /* If the target can evaluate the condition expression in hardware,
7047 then we we need to insert both watchpoints even if they are at
7048 the same place. Otherwise the watchpoint will only trigger when
7049 the condition of whichever watchpoint was inserted evaluates to
7050 true, not giving a chance for GDB to check the condition of the
7051 other watchpoint. */
3a5c3e22 7052 if ((w1->cond_exp
4a64f543
MS
7053 && target_can_accel_watchpoint_condition (loc1->address,
7054 loc1->length,
0cf6dd15 7055 loc1->watchpoint_type,
3a5c3e22
PA
7056 w1->cond_exp))
7057 || (w2->cond_exp
4a64f543
MS
7058 && target_can_accel_watchpoint_condition (loc2->address,
7059 loc2->length,
0cf6dd15 7060 loc2->watchpoint_type,
3a5c3e22 7061 w2->cond_exp)))
0cf6dd15
TJB
7062 return 0;
7063
85d721b8
PA
7064 /* Note that this checks the owner's type, not the location's. In
7065 case the target does not support read watchpoints, but does
7066 support access watchpoints, we'll have bp_read_watchpoint
7067 watchpoints with hw_access locations. Those should be considered
7068 duplicates of hw_read locations. The hw_read locations will
7069 become hw_access locations later. */
2d134ed3
PA
7070 return (loc1->owner->type == loc2->owner->type
7071 && loc1->pspace->aspace == loc2->pspace->aspace
7072 && loc1->address == loc2->address
7073 && loc1->length == loc2->length);
e4f237da
KB
7074}
7075
31e77af2 7076/* See breakpoint.h. */
6c95b8df 7077
31e77af2 7078int
6c95b8df
PA
7079breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7080 struct address_space *aspace2, CORE_ADDR addr2)
7081{
f5656ead 7082 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7083 || aspace1 == aspace2)
7084 && addr1 == addr2);
7085}
7086
f1310107
TJB
7087/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7088 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7089 matches ASPACE2. On targets that have global breakpoints, the address
7090 space doesn't really matter. */
7091
7092static int
7093breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7094 int len1, struct address_space *aspace2,
7095 CORE_ADDR addr2)
7096{
f5656ead 7097 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7098 || aspace1 == aspace2)
7099 && addr2 >= addr1 && addr2 < addr1 + len1);
7100}
7101
7102/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7103 a ranged breakpoint. In most targets, a match happens only if ASPACE
7104 matches the breakpoint's address space. On targets that have global
7105 breakpoints, the address space doesn't really matter. */
7106
7107static int
7108breakpoint_location_address_match (struct bp_location *bl,
7109 struct address_space *aspace,
7110 CORE_ADDR addr)
7111{
7112 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7113 aspace, addr)
7114 || (bl->length
7115 && breakpoint_address_match_range (bl->pspace->aspace,
7116 bl->address, bl->length,
7117 aspace, addr)));
7118}
7119
d35ae833
PA
7120/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7121 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7122 match happens only if ASPACE matches the breakpoint's address
7123 space. On targets that have global breakpoints, the address space
7124 doesn't really matter. */
7125
7126static int
7127breakpoint_location_address_range_overlap (struct bp_location *bl,
7128 struct address_space *aspace,
7129 CORE_ADDR addr, int len)
7130{
7131 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7132 || bl->pspace->aspace == aspace)
7133 {
7134 int bl_len = bl->length != 0 ? bl->length : 1;
7135
7136 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7137 return 1;
7138 }
7139 return 0;
7140}
7141
1e4d1764
YQ
7142/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7143 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7144 true, otherwise returns false. */
7145
7146static int
7147tracepoint_locations_match (struct bp_location *loc1,
7148 struct bp_location *loc2)
7149{
7150 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7151 /* Since tracepoint locations are never duplicated with others', tracepoint
7152 locations at the same address of different tracepoints are regarded as
7153 different locations. */
7154 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7155 else
7156 return 0;
7157}
7158
2d134ed3
PA
7159/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7160 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7161 represent the same location. */
7162
7163static int
4a64f543
MS
7164breakpoint_locations_match (struct bp_location *loc1,
7165 struct bp_location *loc2)
2d134ed3 7166{
2bdf28a0
JK
7167 int hw_point1, hw_point2;
7168
7169 /* Both of them must not be in moribund_locations. */
7170 gdb_assert (loc1->owner != NULL);
7171 gdb_assert (loc2->owner != NULL);
7172
7173 hw_point1 = is_hardware_watchpoint (loc1->owner);
7174 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7175
7176 if (hw_point1 != hw_point2)
7177 return 0;
7178 else if (hw_point1)
7179 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7180 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7181 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7182 else
f1310107
TJB
7183 /* We compare bp_location.length in order to cover ranged breakpoints. */
7184 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7185 loc2->pspace->aspace, loc2->address)
7186 && loc1->length == loc2->length);
2d134ed3
PA
7187}
7188
76897487
KB
7189static void
7190breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7191 int bnum, int have_bnum)
7192{
f63fbe86
MS
7193 /* The longest string possibly returned by hex_string_custom
7194 is 50 chars. These must be at least that big for safety. */
7195 char astr1[64];
7196 char astr2[64];
76897487 7197
bb599908
PH
7198 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7199 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7200 if (have_bnum)
8a3fe4f8 7201 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7202 bnum, astr1, astr2);
7203 else
8a3fe4f8 7204 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7205}
7206
4a64f543
MS
7207/* Adjust a breakpoint's address to account for architectural
7208 constraints on breakpoint placement. Return the adjusted address.
7209 Note: Very few targets require this kind of adjustment. For most
7210 targets, this function is simply the identity function. */
76897487
KB
7211
7212static CORE_ADDR
a6d9a66e
UW
7213adjust_breakpoint_address (struct gdbarch *gdbarch,
7214 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7215{
a6d9a66e 7216 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7217 {
7218 /* Very few targets need any kind of breakpoint adjustment. */
7219 return bpaddr;
7220 }
88f7da05
KB
7221 else if (bptype == bp_watchpoint
7222 || bptype == bp_hardware_watchpoint
7223 || bptype == bp_read_watchpoint
7224 || bptype == bp_access_watchpoint
fe798b75 7225 || bptype == bp_catchpoint)
88f7da05
KB
7226 {
7227 /* Watchpoints and the various bp_catch_* eventpoints should not
7228 have their addresses modified. */
7229 return bpaddr;
7230 }
7c16b83e
PA
7231 else if (bptype == bp_single_step)
7232 {
7233 /* Single-step breakpoints should not have their addresses
7234 modified. If there's any architectural constrain that
7235 applies to this address, then it should have already been
7236 taken into account when the breakpoint was created in the
7237 first place. If we didn't do this, stepping through e.g.,
7238 Thumb-2 IT blocks would break. */
7239 return bpaddr;
7240 }
76897487
KB
7241 else
7242 {
7243 CORE_ADDR adjusted_bpaddr;
7244
7245 /* Some targets have architectural constraints on the placement
7246 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7247 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7248
7249 /* An adjusted breakpoint address can significantly alter
7250 a user's expectations. Print a warning if an adjustment
7251 is required. */
7252 if (adjusted_bpaddr != bpaddr)
7253 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7254
7255 return adjusted_bpaddr;
7256 }
7257}
7258
28010a5d
PA
7259void
7260init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7261 struct breakpoint *owner)
7cc221ef 7262{
7cc221ef
DJ
7263 memset (loc, 0, sizeof (*loc));
7264
348d480f
PA
7265 gdb_assert (ops != NULL);
7266
28010a5d
PA
7267 loc->ops = ops;
7268 loc->owner = owner;
511a6cd4 7269 loc->cond = NULL;
b775012e 7270 loc->cond_bytecode = NULL;
0d381245
VP
7271 loc->shlib_disabled = 0;
7272 loc->enabled = 1;
e049a4b5 7273
28010a5d 7274 switch (owner->type)
e049a4b5
DJ
7275 {
7276 case bp_breakpoint:
7c16b83e 7277 case bp_single_step:
e049a4b5
DJ
7278 case bp_until:
7279 case bp_finish:
7280 case bp_longjmp:
7281 case bp_longjmp_resume:
e2e4d78b 7282 case bp_longjmp_call_dummy:
186c406b
TT
7283 case bp_exception:
7284 case bp_exception_resume:
e049a4b5 7285 case bp_step_resume:
2c03e5be 7286 case bp_hp_step_resume:
e049a4b5
DJ
7287 case bp_watchpoint_scope:
7288 case bp_call_dummy:
aa7d318d 7289 case bp_std_terminate:
e049a4b5
DJ
7290 case bp_shlib_event:
7291 case bp_thread_event:
7292 case bp_overlay_event:
4efc6507 7293 case bp_jit_event:
0fd8e87f 7294 case bp_longjmp_master:
aa7d318d 7295 case bp_std_terminate_master:
186c406b 7296 case bp_exception_master:
0e30163f
JK
7297 case bp_gnu_ifunc_resolver:
7298 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7299 case bp_dprintf:
e049a4b5 7300 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7301 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7302 break;
7303 case bp_hardware_breakpoint:
7304 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7305 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7306 break;
7307 case bp_hardware_watchpoint:
7308 case bp_read_watchpoint:
7309 case bp_access_watchpoint:
7310 loc->loc_type = bp_loc_hardware_watchpoint;
7311 break;
7312 case bp_watchpoint:
ce78b96d 7313 case bp_catchpoint:
15c3d785
PA
7314 case bp_tracepoint:
7315 case bp_fast_tracepoint:
0fb4aa4b 7316 case bp_static_tracepoint:
e049a4b5
DJ
7317 loc->loc_type = bp_loc_other;
7318 break;
7319 default:
e2e0b3e5 7320 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7321 }
7322
f431efe5 7323 loc->refc = 1;
28010a5d
PA
7324}
7325
7326/* Allocate a struct bp_location. */
7327
7328static struct bp_location *
7329allocate_bp_location (struct breakpoint *bpt)
7330{
348d480f
PA
7331 return bpt->ops->allocate_location (bpt);
7332}
7cc221ef 7333
f431efe5
PA
7334static void
7335free_bp_location (struct bp_location *loc)
fe3f5fa8 7336{
348d480f 7337 loc->ops->dtor (loc);
fe3f5fa8
VP
7338 xfree (loc);
7339}
7340
f431efe5
PA
7341/* Increment reference count. */
7342
7343static void
7344incref_bp_location (struct bp_location *bl)
7345{
7346 ++bl->refc;
7347}
7348
7349/* Decrement reference count. If the reference count reaches 0,
7350 destroy the bp_location. Sets *BLP to NULL. */
7351
7352static void
7353decref_bp_location (struct bp_location **blp)
7354{
0807b50c
PA
7355 gdb_assert ((*blp)->refc > 0);
7356
f431efe5
PA
7357 if (--(*blp)->refc == 0)
7358 free_bp_location (*blp);
7359 *blp = NULL;
7360}
7361
346774a9 7362/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7363
346774a9
PA
7364static void
7365add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7366{
346774a9 7367 struct breakpoint *b1;
c906108c 7368
346774a9
PA
7369 /* Add this breakpoint to the end of the chain so that a list of
7370 breakpoints will come out in order of increasing numbers. */
7371
7372 b1 = breakpoint_chain;
7373 if (b1 == 0)
7374 breakpoint_chain = b;
7375 else
7376 {
7377 while (b1->next)
7378 b1 = b1->next;
7379 b1->next = b;
7380 }
7381}
7382
7383/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7384
7385static void
7386init_raw_breakpoint_without_location (struct breakpoint *b,
7387 struct gdbarch *gdbarch,
28010a5d 7388 enum bptype bptype,
c0a91b2b 7389 const struct breakpoint_ops *ops)
346774a9 7390{
c906108c 7391 memset (b, 0, sizeof (*b));
2219d63c 7392
348d480f
PA
7393 gdb_assert (ops != NULL);
7394
28010a5d 7395 b->ops = ops;
4d28f7a8 7396 b->type = bptype;
a6d9a66e 7397 b->gdbarch = gdbarch;
c906108c
SS
7398 b->language = current_language->la_language;
7399 b->input_radix = input_radix;
7400 b->thread = -1;
b5de0fa7 7401 b->enable_state = bp_enabled;
c906108c
SS
7402 b->next = 0;
7403 b->silent = 0;
7404 b->ignore_count = 0;
7405 b->commands = NULL;
818dd999 7406 b->frame_id = null_frame_id;
0d381245 7407 b->condition_not_parsed = 0;
84f4c1fe 7408 b->py_bp_object = NULL;
d0fb5eae 7409 b->related_breakpoint = b;
f00aae0f 7410 b->location = NULL;
346774a9
PA
7411}
7412
7413/* Helper to set_raw_breakpoint below. Creates a breakpoint
7414 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7415
7416static struct breakpoint *
7417set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7418 enum bptype bptype,
c0a91b2b 7419 const struct breakpoint_ops *ops)
346774a9
PA
7420{
7421 struct breakpoint *b = XNEW (struct breakpoint);
7422
348d480f 7423 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7424 add_to_breakpoint_chain (b);
0d381245
VP
7425 return b;
7426}
7427
0e30163f
JK
7428/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7429 resolutions should be made as the user specified the location explicitly
7430 enough. */
7431
0d381245 7432static void
0e30163f 7433set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7434{
2bdf28a0
JK
7435 gdb_assert (loc->owner != NULL);
7436
0d381245 7437 if (loc->owner->type == bp_breakpoint
1042e4c0 7438 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7439 || is_tracepoint (loc->owner))
0d381245 7440 {
0e30163f 7441 int is_gnu_ifunc;
2c02bd72 7442 const char *function_name;
6a3a010b 7443 CORE_ADDR func_addr;
0e30163f 7444
2c02bd72 7445 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7446 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7447
7448 if (is_gnu_ifunc && !explicit_loc)
7449 {
7450 struct breakpoint *b = loc->owner;
7451
7452 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7453 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7454 &loc->requested_address))
7455 {
7456 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7457 loc->address = adjust_breakpoint_address (loc->gdbarch,
7458 loc->requested_address,
7459 b->type);
7460 }
7461 else if (b->type == bp_breakpoint && b->loc == loc
7462 && loc->next == NULL && b->related_breakpoint == b)
7463 {
7464 /* Create only the whole new breakpoint of this type but do not
7465 mess more complicated breakpoints with multiple locations. */
7466 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7467 /* Remember the resolver's address for use by the return
7468 breakpoint. */
7469 loc->related_address = func_addr;
0e30163f
JK
7470 }
7471 }
7472
2c02bd72
DE
7473 if (function_name)
7474 loc->function_name = xstrdup (function_name);
0d381245
VP
7475 }
7476}
7477
a6d9a66e 7478/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7479struct gdbarch *
a6d9a66e
UW
7480get_sal_arch (struct symtab_and_line sal)
7481{
7482 if (sal.section)
7483 return get_objfile_arch (sal.section->objfile);
7484 if (sal.symtab)
eb822aa6 7485 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7486
7487 return NULL;
7488}
7489
346774a9
PA
7490/* Low level routine for partially initializing a breakpoint of type
7491 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7492 file name, and line number are provided by SAL.
0d381245
VP
7493
7494 It is expected that the caller will complete the initialization of
7495 the newly created breakpoint struct as well as output any status
c56053d2 7496 information regarding the creation of a new breakpoint. */
0d381245 7497
346774a9
PA
7498static void
7499init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7500 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7501 const struct breakpoint_ops *ops)
0d381245 7502{
28010a5d 7503 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7504
3742cc8b 7505 add_location_to_breakpoint (b, &sal);
0d381245 7506
6c95b8df
PA
7507 if (bptype != bp_catchpoint)
7508 gdb_assert (sal.pspace != NULL);
7509
f8eba3c6
TT
7510 /* Store the program space that was used to set the breakpoint,
7511 except for ordinary breakpoints, which are independent of the
7512 program space. */
7513 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7514 b->pspace = sal.pspace;
346774a9 7515}
c906108c 7516
346774a9
PA
7517/* set_raw_breakpoint is a low level routine for allocating and
7518 partially initializing a breakpoint of type BPTYPE. The newly
7519 created breakpoint's address, section, source file name, and line
7520 number are provided by SAL. The newly created and partially
7521 initialized breakpoint is added to the breakpoint chain and
7522 is also returned as the value of this function.
7523
7524 It is expected that the caller will complete the initialization of
7525 the newly created breakpoint struct as well as output any status
7526 information regarding the creation of a new breakpoint. In
7527 particular, set_raw_breakpoint does NOT set the breakpoint
7528 number! Care should be taken to not allow an error to occur
7529 prior to completing the initialization of the breakpoint. If this
7530 should happen, a bogus breakpoint will be left on the chain. */
7531
7532struct breakpoint *
7533set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7534 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7535 const struct breakpoint_ops *ops)
346774a9
PA
7536{
7537 struct breakpoint *b = XNEW (struct breakpoint);
7538
348d480f 7539 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7540 add_to_breakpoint_chain (b);
c906108c
SS
7541 return b;
7542}
7543
53a5351d 7544/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7545 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7546 initiated the operation. */
c906108c
SS
7547
7548void
186c406b 7549set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7550{
35df4500 7551 struct breakpoint *b, *b_tmp;
186c406b 7552 int thread = tp->num;
0fd8e87f
UW
7553
7554 /* To avoid having to rescan all objfile symbols at every step,
7555 we maintain a list of continually-inserted but always disabled
7556 longjmp "master" breakpoints. Here, we simply create momentary
7557 clones of those and enable them for the requested thread. */
35df4500 7558 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7559 if (b->pspace == current_program_space
186c406b
TT
7560 && (b->type == bp_longjmp_master
7561 || b->type == bp_exception_master))
0fd8e87f 7562 {
06edf0c0
PA
7563 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7564 struct breakpoint *clone;
cc59ec59 7565
e2e4d78b
JK
7566 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7567 after their removal. */
06edf0c0 7568 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7569 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7570 clone->thread = thread;
7571 }
186c406b
TT
7572
7573 tp->initiating_frame = frame;
c906108c
SS
7574}
7575
611c83ae 7576/* Delete all longjmp breakpoints from THREAD. */
c906108c 7577void
611c83ae 7578delete_longjmp_breakpoint (int thread)
c906108c 7579{
35df4500 7580 struct breakpoint *b, *b_tmp;
c906108c 7581
35df4500 7582 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7583 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7584 {
7585 if (b->thread == thread)
7586 delete_breakpoint (b);
7587 }
c906108c
SS
7588}
7589
f59f708a
PA
7590void
7591delete_longjmp_breakpoint_at_next_stop (int thread)
7592{
7593 struct breakpoint *b, *b_tmp;
7594
7595 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7596 if (b->type == bp_longjmp || b->type == bp_exception)
7597 {
7598 if (b->thread == thread)
7599 b->disposition = disp_del_at_next_stop;
7600 }
7601}
7602
e2e4d78b
JK
7603/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7604 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7605 pointer to any of them. Return NULL if this system cannot place longjmp
7606 breakpoints. */
7607
7608struct breakpoint *
7609set_longjmp_breakpoint_for_call_dummy (void)
7610{
7611 struct breakpoint *b, *retval = NULL;
7612
7613 ALL_BREAKPOINTS (b)
7614 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7615 {
7616 struct breakpoint *new_b;
7617
7618 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7619 &momentary_breakpoint_ops,
7620 1);
e2e4d78b
JK
7621 new_b->thread = pid_to_thread_id (inferior_ptid);
7622
7623 /* Link NEW_B into the chain of RETVAL breakpoints. */
7624
7625 gdb_assert (new_b->related_breakpoint == new_b);
7626 if (retval == NULL)
7627 retval = new_b;
7628 new_b->related_breakpoint = retval;
7629 while (retval->related_breakpoint != new_b->related_breakpoint)
7630 retval = retval->related_breakpoint;
7631 retval->related_breakpoint = new_b;
7632 }
7633
7634 return retval;
7635}
7636
7637/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7638 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7639 stack.
7640
7641 You should call this function only at places where it is safe to currently
7642 unwind the whole stack. Failed stack unwind would discard live dummy
7643 frames. */
7644
7645void
b67a2c6f 7646check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7647{
7648 struct breakpoint *b, *b_tmp;
7649
7650 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7651 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7652 {
7653 struct breakpoint *dummy_b = b->related_breakpoint;
7654
7655 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7656 dummy_b = dummy_b->related_breakpoint;
7657 if (dummy_b->type != bp_call_dummy
7658 || frame_find_by_id (dummy_b->frame_id) != NULL)
7659 continue;
7660
b67a2c6f 7661 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7662
7663 while (b->related_breakpoint != b)
7664 {
7665 if (b_tmp == b->related_breakpoint)
7666 b_tmp = b->related_breakpoint->next;
7667 delete_breakpoint (b->related_breakpoint);
7668 }
7669 delete_breakpoint (b);
7670 }
7671}
7672
1900040c
MS
7673void
7674enable_overlay_breakpoints (void)
7675{
52f0bd74 7676 struct breakpoint *b;
1900040c
MS
7677
7678 ALL_BREAKPOINTS (b)
7679 if (b->type == bp_overlay_event)
7680 {
7681 b->enable_state = bp_enabled;
44702360 7682 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7683 overlay_events_enabled = 1;
1900040c
MS
7684 }
7685}
7686
7687void
7688disable_overlay_breakpoints (void)
7689{
52f0bd74 7690 struct breakpoint *b;
1900040c
MS
7691
7692 ALL_BREAKPOINTS (b)
7693 if (b->type == bp_overlay_event)
7694 {
7695 b->enable_state = bp_disabled;
44702360 7696 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7697 overlay_events_enabled = 0;
1900040c
MS
7698 }
7699}
7700
aa7d318d
TT
7701/* Set an active std::terminate breakpoint for each std::terminate
7702 master breakpoint. */
7703void
7704set_std_terminate_breakpoint (void)
7705{
35df4500 7706 struct breakpoint *b, *b_tmp;
aa7d318d 7707
35df4500 7708 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7709 if (b->pspace == current_program_space
7710 && b->type == bp_std_terminate_master)
7711 {
06edf0c0 7712 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7713 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7714 }
7715}
7716
7717/* Delete all the std::terminate breakpoints. */
7718void
7719delete_std_terminate_breakpoint (void)
7720{
35df4500 7721 struct breakpoint *b, *b_tmp;
aa7d318d 7722
35df4500 7723 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7724 if (b->type == bp_std_terminate)
7725 delete_breakpoint (b);
7726}
7727
c4093a6a 7728struct breakpoint *
a6d9a66e 7729create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7730{
7731 struct breakpoint *b;
c4093a6a 7732
06edf0c0
PA
7733 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7734 &internal_breakpoint_ops);
7735
b5de0fa7 7736 b->enable_state = bp_enabled;
f00aae0f 7737 /* location has to be used or breakpoint_re_set will delete me. */
a06efdd6 7738 b->location = new_address_location (b->loc->address);
c4093a6a 7739
44702360 7740 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7741
c4093a6a
JM
7742 return b;
7743}
7744
7745void
7746remove_thread_event_breakpoints (void)
7747{
35df4500 7748 struct breakpoint *b, *b_tmp;
c4093a6a 7749
35df4500 7750 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7751 if (b->type == bp_thread_event
7752 && b->loc->pspace == current_program_space)
c4093a6a
JM
7753 delete_breakpoint (b);
7754}
7755
0101ce28
JJ
7756struct lang_and_radix
7757 {
7758 enum language lang;
7759 int radix;
7760 };
7761
4efc6507
DE
7762/* Create a breakpoint for JIT code registration and unregistration. */
7763
7764struct breakpoint *
7765create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7766{
7767 struct breakpoint *b;
7768
06edf0c0
PA
7769 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7770 &internal_breakpoint_ops);
44702360 7771 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7772 return b;
7773}
0101ce28 7774
03673fc7
PP
7775/* Remove JIT code registration and unregistration breakpoint(s). */
7776
7777void
7778remove_jit_event_breakpoints (void)
7779{
7780 struct breakpoint *b, *b_tmp;
7781
7782 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7783 if (b->type == bp_jit_event
7784 && b->loc->pspace == current_program_space)
7785 delete_breakpoint (b);
7786}
7787
cae688ec
JJ
7788void
7789remove_solib_event_breakpoints (void)
7790{
35df4500 7791 struct breakpoint *b, *b_tmp;
cae688ec 7792
35df4500 7793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7794 if (b->type == bp_shlib_event
7795 && b->loc->pspace == current_program_space)
cae688ec
JJ
7796 delete_breakpoint (b);
7797}
7798
f37f681c
PA
7799/* See breakpoint.h. */
7800
7801void
7802remove_solib_event_breakpoints_at_next_stop (void)
7803{
7804 struct breakpoint *b, *b_tmp;
7805
7806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7807 if (b->type == bp_shlib_event
7808 && b->loc->pspace == current_program_space)
7809 b->disposition = disp_del_at_next_stop;
7810}
7811
04086b45
PA
7812/* Helper for create_solib_event_breakpoint /
7813 create_and_insert_solib_event_breakpoint. Allows specifying which
7814 INSERT_MODE to pass through to update_global_location_list. */
7815
7816static struct breakpoint *
7817create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7818 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7819{
7820 struct breakpoint *b;
7821
06edf0c0
PA
7822 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7823 &internal_breakpoint_ops);
04086b45 7824 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7825 return b;
7826}
7827
04086b45
PA
7828struct breakpoint *
7829create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7830{
7831 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7832}
7833
f37f681c
PA
7834/* See breakpoint.h. */
7835
7836struct breakpoint *
7837create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7838{
7839 struct breakpoint *b;
7840
04086b45
PA
7841 /* Explicitly tell update_global_location_list to insert
7842 locations. */
7843 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7844 if (!b->loc->inserted)
7845 {
7846 delete_breakpoint (b);
7847 return NULL;
7848 }
7849 return b;
7850}
7851
cae688ec
JJ
7852/* Disable any breakpoints that are on code in shared libraries. Only
7853 apply to enabled breakpoints, disabled ones can just stay disabled. */
7854
7855void
cb851954 7856disable_breakpoints_in_shlibs (void)
cae688ec 7857{
876fa593 7858 struct bp_location *loc, **locp_tmp;
cae688ec 7859
876fa593 7860 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7861 {
2bdf28a0 7862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7863 struct breakpoint *b = loc->owner;
2bdf28a0 7864
4a64f543
MS
7865 /* We apply the check to all breakpoints, including disabled for
7866 those with loc->duplicate set. This is so that when breakpoint
7867 becomes enabled, or the duplicate is removed, gdb will try to
7868 insert all breakpoints. If we don't set shlib_disabled here,
7869 we'll try to insert those breakpoints and fail. */
1042e4c0 7870 if (((b->type == bp_breakpoint)
508ccb1f 7871 || (b->type == bp_jit_event)
1042e4c0 7872 || (b->type == bp_hardware_breakpoint)
d77f58be 7873 || (is_tracepoint (b)))
6c95b8df 7874 && loc->pspace == current_program_space
0d381245 7875 && !loc->shlib_disabled
6c95b8df 7876 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7877 )
0d381245
VP
7878 {
7879 loc->shlib_disabled = 1;
7880 }
cae688ec
JJ
7881 }
7882}
7883
63644780
NB
7884/* Disable any breakpoints and tracepoints that are in SOLIB upon
7885 notification of unloaded_shlib. Only apply to enabled breakpoints,
7886 disabled ones can just stay disabled. */
84acb35a 7887
75149521 7888static void
84acb35a
JJ
7889disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7890{
876fa593 7891 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7892 int disabled_shlib_breaks = 0;
7893
c86cf029
VP
7894 /* SunOS a.out shared libraries are always mapped, so do not
7895 disable breakpoints; they will only be reported as unloaded
7896 through clear_solib when GDB discards its shared library
7897 list. See clear_solib for more information. */
7898 if (exec_bfd != NULL
7899 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7900 return;
7901
876fa593 7902 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7903 {
2bdf28a0 7904 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7905 struct breakpoint *b = loc->owner;
cc59ec59 7906
1e4d1764 7907 if (solib->pspace == loc->pspace
e2dd7057 7908 && !loc->shlib_disabled
1e4d1764
YQ
7909 && (((b->type == bp_breakpoint
7910 || b->type == bp_jit_event
7911 || b->type == bp_hardware_breakpoint)
7912 && (loc->loc_type == bp_loc_hardware_breakpoint
7913 || loc->loc_type == bp_loc_software_breakpoint))
7914 || is_tracepoint (b))
e2dd7057 7915 && solib_contains_address_p (solib, loc->address))
84acb35a 7916 {
e2dd7057
PP
7917 loc->shlib_disabled = 1;
7918 /* At this point, we cannot rely on remove_breakpoint
7919 succeeding so we must mark the breakpoint as not inserted
7920 to prevent future errors occurring in remove_breakpoints. */
7921 loc->inserted = 0;
8d3788bd
VP
7922
7923 /* This may cause duplicate notifications for the same breakpoint. */
7924 observer_notify_breakpoint_modified (b);
7925
e2dd7057
PP
7926 if (!disabled_shlib_breaks)
7927 {
7928 target_terminal_ours_for_output ();
3e43a32a
MS
7929 warning (_("Temporarily disabling breakpoints "
7930 "for unloaded shared library \"%s\""),
e2dd7057 7931 solib->so_name);
84acb35a 7932 }
e2dd7057 7933 disabled_shlib_breaks = 1;
84acb35a
JJ
7934 }
7935 }
84acb35a
JJ
7936}
7937
63644780
NB
7938/* Disable any breakpoints and tracepoints in OBJFILE upon
7939 notification of free_objfile. Only apply to enabled breakpoints,
7940 disabled ones can just stay disabled. */
7941
7942static void
7943disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7944{
7945 struct breakpoint *b;
7946
7947 if (objfile == NULL)
7948 return;
7949
d03de421
PA
7950 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7951 managed by the user with add-symbol-file/remove-symbol-file.
7952 Similarly to how breakpoints in shared libraries are handled in
7953 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7954 shlib_disabled so they end up uninserted on the next global
7955 location list update. Shared libraries not loaded by the user
7956 aren't handled here -- they're already handled in
7957 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7958 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7959 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7960 main objfile). */
7961 if ((objfile->flags & OBJF_SHARED) == 0
7962 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7963 return;
7964
7965 ALL_BREAKPOINTS (b)
7966 {
7967 struct bp_location *loc;
7968 int bp_modified = 0;
7969
7970 if (!is_breakpoint (b) && !is_tracepoint (b))
7971 continue;
7972
7973 for (loc = b->loc; loc != NULL; loc = loc->next)
7974 {
7975 CORE_ADDR loc_addr = loc->address;
7976
7977 if (loc->loc_type != bp_loc_hardware_breakpoint
7978 && loc->loc_type != bp_loc_software_breakpoint)
7979 continue;
7980
7981 if (loc->shlib_disabled != 0)
7982 continue;
7983
7984 if (objfile->pspace != loc->pspace)
7985 continue;
7986
7987 if (loc->loc_type != bp_loc_hardware_breakpoint
7988 && loc->loc_type != bp_loc_software_breakpoint)
7989 continue;
7990
7991 if (is_addr_in_objfile (loc_addr, objfile))
7992 {
7993 loc->shlib_disabled = 1;
08351840
PA
7994 /* At this point, we don't know whether the object was
7995 unmapped from the inferior or not, so leave the
7996 inserted flag alone. We'll handle failure to
7997 uninsert quietly, in case the object was indeed
7998 unmapped. */
63644780
NB
7999
8000 mark_breakpoint_location_modified (loc);
8001
8002 bp_modified = 1;
8003 }
8004 }
8005
8006 if (bp_modified)
8007 observer_notify_breakpoint_modified (b);
8008 }
8009}
8010
ce78b96d
JB
8011/* FORK & VFORK catchpoints. */
8012
e29a4733
PA
8013/* An instance of this type is used to represent a fork or vfork
8014 catchpoint. It includes a "struct breakpoint" as a kind of base
8015 class; users downcast to "struct breakpoint *" when needed. A
8016 breakpoint is really of this type iff its ops pointer points to
8017 CATCH_FORK_BREAKPOINT_OPS. */
8018
8019struct fork_catchpoint
8020{
8021 /* The base class. */
8022 struct breakpoint base;
8023
8024 /* Process id of a child process whose forking triggered this
8025 catchpoint. This field is only valid immediately after this
8026 catchpoint has triggered. */
8027 ptid_t forked_inferior_pid;
8028};
8029
4a64f543
MS
8030/* Implement the "insert" breakpoint_ops method for fork
8031 catchpoints. */
ce78b96d 8032
77b06cd7
TJB
8033static int
8034insert_catch_fork (struct bp_location *bl)
ce78b96d 8035{
dfd4cc63 8036 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8037}
8038
4a64f543
MS
8039/* Implement the "remove" breakpoint_ops method for fork
8040 catchpoints. */
ce78b96d
JB
8041
8042static int
77b06cd7 8043remove_catch_fork (struct bp_location *bl)
ce78b96d 8044{
dfd4cc63 8045 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8046}
8047
8048/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8049 catchpoints. */
8050
8051static int
f1310107 8052breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8053 struct address_space *aspace, CORE_ADDR bp_addr,
8054 const struct target_waitstatus *ws)
ce78b96d 8055{
e29a4733
PA
8056 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8057
f90263c1
TT
8058 if (ws->kind != TARGET_WAITKIND_FORKED)
8059 return 0;
8060
8061 c->forked_inferior_pid = ws->value.related_pid;
8062 return 1;
ce78b96d
JB
8063}
8064
4a64f543
MS
8065/* Implement the "print_it" breakpoint_ops method for fork
8066 catchpoints. */
ce78b96d
JB
8067
8068static enum print_stop_action
348d480f 8069print_it_catch_fork (bpstat bs)
ce78b96d 8070{
36dfb11c 8071 struct ui_out *uiout = current_uiout;
348d480f
PA
8072 struct breakpoint *b = bs->breakpoint_at;
8073 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8074
ce78b96d 8075 annotate_catchpoint (b->number);
36dfb11c
TT
8076 if (b->disposition == disp_del)
8077 ui_out_text (uiout, "\nTemporary catchpoint ");
8078 else
8079 ui_out_text (uiout, "\nCatchpoint ");
8080 if (ui_out_is_mi_like_p (uiout))
8081 {
8082 ui_out_field_string (uiout, "reason",
8083 async_reason_lookup (EXEC_ASYNC_FORK));
8084 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8085 }
8086 ui_out_field_int (uiout, "bkptno", b->number);
8087 ui_out_text (uiout, " (forked process ");
8088 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8089 ui_out_text (uiout, "), ");
ce78b96d
JB
8090 return PRINT_SRC_AND_LOC;
8091}
8092
4a64f543
MS
8093/* Implement the "print_one" breakpoint_ops method for fork
8094 catchpoints. */
ce78b96d
JB
8095
8096static void
a6d9a66e 8097print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8098{
e29a4733 8099 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8100 struct value_print_options opts;
79a45e25 8101 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8102
8103 get_user_print_options (&opts);
8104
4a64f543
MS
8105 /* Field 4, the address, is omitted (which makes the columns not
8106 line up too nicely with the headers, but the effect is relatively
8107 readable). */
79a45b7d 8108 if (opts.addressprint)
ce78b96d
JB
8109 ui_out_field_skip (uiout, "addr");
8110 annotate_field (5);
8111 ui_out_text (uiout, "fork");
e29a4733 8112 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8113 {
8114 ui_out_text (uiout, ", process ");
8115 ui_out_field_int (uiout, "what",
e29a4733 8116 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8117 ui_out_spaces (uiout, 1);
8118 }
8ac3646f
TT
8119
8120 if (ui_out_is_mi_like_p (uiout))
8121 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8122}
8123
8124/* Implement the "print_mention" breakpoint_ops method for fork
8125 catchpoints. */
8126
8127static void
8128print_mention_catch_fork (struct breakpoint *b)
8129{
8130 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8131}
8132
6149aea9
PA
8133/* Implement the "print_recreate" breakpoint_ops method for fork
8134 catchpoints. */
8135
8136static void
8137print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8138{
8139 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8140 print_recreate_thread (b, fp);
6149aea9
PA
8141}
8142
ce78b96d
JB
8143/* The breakpoint_ops structure to be used in fork catchpoints. */
8144
2060206e 8145static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8146
4a64f543
MS
8147/* Implement the "insert" breakpoint_ops method for vfork
8148 catchpoints. */
ce78b96d 8149
77b06cd7
TJB
8150static int
8151insert_catch_vfork (struct bp_location *bl)
ce78b96d 8152{
dfd4cc63 8153 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8154}
8155
4a64f543
MS
8156/* Implement the "remove" breakpoint_ops method for vfork
8157 catchpoints. */
ce78b96d
JB
8158
8159static int
77b06cd7 8160remove_catch_vfork (struct bp_location *bl)
ce78b96d 8161{
dfd4cc63 8162 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8163}
8164
8165/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8166 catchpoints. */
8167
8168static int
f1310107 8169breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8170 struct address_space *aspace, CORE_ADDR bp_addr,
8171 const struct target_waitstatus *ws)
ce78b96d 8172{
e29a4733
PA
8173 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8174
f90263c1
TT
8175 if (ws->kind != TARGET_WAITKIND_VFORKED)
8176 return 0;
8177
8178 c->forked_inferior_pid = ws->value.related_pid;
8179 return 1;
ce78b96d
JB
8180}
8181
4a64f543
MS
8182/* Implement the "print_it" breakpoint_ops method for vfork
8183 catchpoints. */
ce78b96d
JB
8184
8185static enum print_stop_action
348d480f 8186print_it_catch_vfork (bpstat bs)
ce78b96d 8187{
36dfb11c 8188 struct ui_out *uiout = current_uiout;
348d480f 8189 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8190 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8191
ce78b96d 8192 annotate_catchpoint (b->number);
36dfb11c
TT
8193 if (b->disposition == disp_del)
8194 ui_out_text (uiout, "\nTemporary catchpoint ");
8195 else
8196 ui_out_text (uiout, "\nCatchpoint ");
8197 if (ui_out_is_mi_like_p (uiout))
8198 {
8199 ui_out_field_string (uiout, "reason",
8200 async_reason_lookup (EXEC_ASYNC_VFORK));
8201 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8202 }
8203 ui_out_field_int (uiout, "bkptno", b->number);
8204 ui_out_text (uiout, " (vforked process ");
8205 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8206 ui_out_text (uiout, "), ");
ce78b96d
JB
8207 return PRINT_SRC_AND_LOC;
8208}
8209
4a64f543
MS
8210/* Implement the "print_one" breakpoint_ops method for vfork
8211 catchpoints. */
ce78b96d
JB
8212
8213static void
a6d9a66e 8214print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8215{
e29a4733 8216 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8217 struct value_print_options opts;
79a45e25 8218 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8219
8220 get_user_print_options (&opts);
4a64f543
MS
8221 /* Field 4, the address, is omitted (which makes the columns not
8222 line up too nicely with the headers, but the effect is relatively
8223 readable). */
79a45b7d 8224 if (opts.addressprint)
ce78b96d
JB
8225 ui_out_field_skip (uiout, "addr");
8226 annotate_field (5);
8227 ui_out_text (uiout, "vfork");
e29a4733 8228 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8229 {
8230 ui_out_text (uiout, ", process ");
8231 ui_out_field_int (uiout, "what",
e29a4733 8232 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8233 ui_out_spaces (uiout, 1);
8234 }
8ac3646f
TT
8235
8236 if (ui_out_is_mi_like_p (uiout))
8237 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8238}
8239
8240/* Implement the "print_mention" breakpoint_ops method for vfork
8241 catchpoints. */
8242
8243static void
8244print_mention_catch_vfork (struct breakpoint *b)
8245{
8246 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8247}
8248
6149aea9
PA
8249/* Implement the "print_recreate" breakpoint_ops method for vfork
8250 catchpoints. */
8251
8252static void
8253print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8254{
8255 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8256 print_recreate_thread (b, fp);
6149aea9
PA
8257}
8258
ce78b96d
JB
8259/* The breakpoint_ops structure to be used in vfork catchpoints. */
8260
2060206e 8261static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8262
edcc5120
TT
8263/* An instance of this type is used to represent an solib catchpoint.
8264 It includes a "struct breakpoint" as a kind of base class; users
8265 downcast to "struct breakpoint *" when needed. A breakpoint is
8266 really of this type iff its ops pointer points to
8267 CATCH_SOLIB_BREAKPOINT_OPS. */
8268
8269struct solib_catchpoint
8270{
8271 /* The base class. */
8272 struct breakpoint base;
8273
8274 /* True for "catch load", false for "catch unload". */
8275 unsigned char is_load;
8276
8277 /* Regular expression to match, if any. COMPILED is only valid when
8278 REGEX is non-NULL. */
8279 char *regex;
8280 regex_t compiled;
8281};
8282
8283static void
8284dtor_catch_solib (struct breakpoint *b)
8285{
8286 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8287
8288 if (self->regex)
8289 regfree (&self->compiled);
8290 xfree (self->regex);
8291
8292 base_breakpoint_ops.dtor (b);
8293}
8294
8295static int
8296insert_catch_solib (struct bp_location *ignore)
8297{
8298 return 0;
8299}
8300
8301static int
8302remove_catch_solib (struct bp_location *ignore)
8303{
8304 return 0;
8305}
8306
8307static int
8308breakpoint_hit_catch_solib (const struct bp_location *bl,
8309 struct address_space *aspace,
8310 CORE_ADDR bp_addr,
8311 const struct target_waitstatus *ws)
8312{
8313 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8314 struct breakpoint *other;
8315
8316 if (ws->kind == TARGET_WAITKIND_LOADED)
8317 return 1;
8318
8319 ALL_BREAKPOINTS (other)
8320 {
8321 struct bp_location *other_bl;
8322
8323 if (other == bl->owner)
8324 continue;
8325
8326 if (other->type != bp_shlib_event)
8327 continue;
8328
8329 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8330 continue;
8331
8332 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8333 {
8334 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8335 return 1;
8336 }
8337 }
8338
8339 return 0;
8340}
8341
8342static void
8343check_status_catch_solib (struct bpstats *bs)
8344{
8345 struct solib_catchpoint *self
8346 = (struct solib_catchpoint *) bs->breakpoint_at;
8347 int ix;
8348
8349 if (self->is_load)
8350 {
8351 struct so_list *iter;
8352
8353 for (ix = 0;
8354 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8355 ix, iter);
8356 ++ix)
8357 {
8358 if (!self->regex
8359 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8360 return;
8361 }
8362 }
8363 else
8364 {
8365 char *iter;
8366
8367 for (ix = 0;
8368 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8369 ix, iter);
8370 ++ix)
8371 {
8372 if (!self->regex
8373 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8374 return;
8375 }
8376 }
8377
8378 bs->stop = 0;
8379 bs->print_it = print_it_noop;
8380}
8381
8382static enum print_stop_action
8383print_it_catch_solib (bpstat bs)
8384{
8385 struct breakpoint *b = bs->breakpoint_at;
8386 struct ui_out *uiout = current_uiout;
8387
8388 annotate_catchpoint (b->number);
8389 if (b->disposition == disp_del)
8390 ui_out_text (uiout, "\nTemporary catchpoint ");
8391 else
8392 ui_out_text (uiout, "\nCatchpoint ");
8393 ui_out_field_int (uiout, "bkptno", b->number);
8394 ui_out_text (uiout, "\n");
8395 if (ui_out_is_mi_like_p (uiout))
8396 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8397 print_solib_event (1);
8398 return PRINT_SRC_AND_LOC;
8399}
8400
8401static void
8402print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8403{
8404 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8405 struct value_print_options opts;
8406 struct ui_out *uiout = current_uiout;
8407 char *msg;
8408
8409 get_user_print_options (&opts);
8410 /* Field 4, the address, is omitted (which makes the columns not
8411 line up too nicely with the headers, but the effect is relatively
8412 readable). */
8413 if (opts.addressprint)
8414 {
8415 annotate_field (4);
8416 ui_out_field_skip (uiout, "addr");
8417 }
8418
8419 annotate_field (5);
8420 if (self->is_load)
8421 {
8422 if (self->regex)
8423 msg = xstrprintf (_("load of library matching %s"), self->regex);
8424 else
8425 msg = xstrdup (_("load of library"));
8426 }
8427 else
8428 {
8429 if (self->regex)
8430 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8431 else
8432 msg = xstrdup (_("unload of library"));
8433 }
8434 ui_out_field_string (uiout, "what", msg);
8435 xfree (msg);
8ac3646f
TT
8436
8437 if (ui_out_is_mi_like_p (uiout))
8438 ui_out_field_string (uiout, "catch-type",
8439 self->is_load ? "load" : "unload");
edcc5120
TT
8440}
8441
8442static void
8443print_mention_catch_solib (struct breakpoint *b)
8444{
8445 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8446
8447 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8448 self->is_load ? "load" : "unload");
8449}
8450
8451static void
8452print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8453{
8454 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8455
8456 fprintf_unfiltered (fp, "%s %s",
8457 b->disposition == disp_del ? "tcatch" : "catch",
8458 self->is_load ? "load" : "unload");
8459 if (self->regex)
8460 fprintf_unfiltered (fp, " %s", self->regex);
8461 fprintf_unfiltered (fp, "\n");
8462}
8463
8464static struct breakpoint_ops catch_solib_breakpoint_ops;
8465
91985142
MG
8466/* Shared helper function (MI and CLI) for creating and installing
8467 a shared object event catchpoint. If IS_LOAD is non-zero then
8468 the events to be caught are load events, otherwise they are
8469 unload events. If IS_TEMP is non-zero the catchpoint is a
8470 temporary one. If ENABLED is non-zero the catchpoint is
8471 created in an enabled state. */
edcc5120 8472
91985142
MG
8473void
8474add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8475{
8476 struct solib_catchpoint *c;
8477 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8478 struct cleanup *cleanup;
8479
edcc5120
TT
8480 if (!arg)
8481 arg = "";
8482 arg = skip_spaces (arg);
8483
8484 c = XCNEW (struct solib_catchpoint);
8485 cleanup = make_cleanup (xfree, c);
8486
8487 if (*arg != '\0')
8488 {
8489 int errcode;
8490
8491 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8492 if (errcode != 0)
8493 {
8494 char *err = get_regcomp_error (errcode, &c->compiled);
8495
8496 make_cleanup (xfree, err);
8497 error (_("Invalid regexp (%s): %s"), err, arg);
8498 }
8499 c->regex = xstrdup (arg);
8500 }
8501
8502 c->is_load = is_load;
91985142 8503 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8504 &catch_solib_breakpoint_ops);
8505
91985142
MG
8506 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8507
edcc5120
TT
8508 discard_cleanups (cleanup);
8509 install_breakpoint (0, &c->base, 1);
8510}
8511
91985142
MG
8512/* A helper function that does all the work for "catch load" and
8513 "catch unload". */
8514
8515static void
8516catch_load_or_unload (char *arg, int from_tty, int is_load,
8517 struct cmd_list_element *command)
8518{
8519 int tempflag;
8520 const int enabled = 1;
8521
8522 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8523
8524 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8525}
8526
edcc5120
TT
8527static void
8528catch_load_command_1 (char *arg, int from_tty,
8529 struct cmd_list_element *command)
8530{
8531 catch_load_or_unload (arg, from_tty, 1, command);
8532}
8533
8534static void
8535catch_unload_command_1 (char *arg, int from_tty,
8536 struct cmd_list_element *command)
8537{
8538 catch_load_or_unload (arg, from_tty, 0, command);
8539}
8540
346774a9
PA
8541/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8542 is non-zero, then make the breakpoint temporary. If COND_STRING is
8543 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8544 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8545
ab04a2af 8546void
346774a9
PA
8547init_catchpoint (struct breakpoint *b,
8548 struct gdbarch *gdbarch, int tempflag,
8549 char *cond_string,
c0a91b2b 8550 const struct breakpoint_ops *ops)
c906108c 8551{
c5aa993b 8552 struct symtab_and_line sal;
346774a9 8553
fe39c653 8554 init_sal (&sal);
6c95b8df 8555 sal.pspace = current_program_space;
c5aa993b 8556
28010a5d 8557 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8558
1b36a34b 8559 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8560 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8561}
8562
28010a5d 8563void
3ea46bff 8564install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8565{
8566 add_to_breakpoint_chain (b);
3a5c3e22 8567 set_breakpoint_number (internal, b);
558a9d82
YQ
8568 if (is_tracepoint (b))
8569 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8570 if (!internal)
8571 mention (b);
c56053d2 8572 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8573
8574 if (update_gll)
44702360 8575 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8576}
8577
9b70b993 8578static void
a6d9a66e
UW
8579create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8580 int tempflag, char *cond_string,
c0a91b2b 8581 const struct breakpoint_ops *ops)
c906108c 8582{
e29a4733 8583 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8584
e29a4733
PA
8585 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8586
8587 c->forked_inferior_pid = null_ptid;
8588
3ea46bff 8589 install_breakpoint (0, &c->base, 1);
c906108c
SS
8590}
8591
fe798b75
JB
8592/* Exec catchpoints. */
8593
b4d90040
PA
8594/* An instance of this type is used to represent an exec catchpoint.
8595 It includes a "struct breakpoint" as a kind of base class; users
8596 downcast to "struct breakpoint *" when needed. A breakpoint is
8597 really of this type iff its ops pointer points to
8598 CATCH_EXEC_BREAKPOINT_OPS. */
8599
8600struct exec_catchpoint
8601{
8602 /* The base class. */
8603 struct breakpoint base;
8604
8605 /* Filename of a program whose exec triggered this catchpoint.
8606 This field is only valid immediately after this catchpoint has
8607 triggered. */
8608 char *exec_pathname;
8609};
8610
8611/* Implement the "dtor" breakpoint_ops method for exec
8612 catchpoints. */
8613
8614static void
8615dtor_catch_exec (struct breakpoint *b)
8616{
8617 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8618
8619 xfree (c->exec_pathname);
348d480f 8620
2060206e 8621 base_breakpoint_ops.dtor (b);
b4d90040
PA
8622}
8623
77b06cd7
TJB
8624static int
8625insert_catch_exec (struct bp_location *bl)
c906108c 8626{
dfd4cc63 8627 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8628}
c906108c 8629
fe798b75 8630static int
77b06cd7 8631remove_catch_exec (struct bp_location *bl)
fe798b75 8632{
dfd4cc63 8633 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8634}
c906108c 8635
fe798b75 8636static int
f1310107 8637breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8638 struct address_space *aspace, CORE_ADDR bp_addr,
8639 const struct target_waitstatus *ws)
fe798b75 8640{
b4d90040
PA
8641 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8642
f90263c1
TT
8643 if (ws->kind != TARGET_WAITKIND_EXECD)
8644 return 0;
8645
8646 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8647 return 1;
fe798b75 8648}
c906108c 8649
fe798b75 8650static enum print_stop_action
348d480f 8651print_it_catch_exec (bpstat bs)
fe798b75 8652{
36dfb11c 8653 struct ui_out *uiout = current_uiout;
348d480f 8654 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8655 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8656
fe798b75 8657 annotate_catchpoint (b->number);
36dfb11c
TT
8658 if (b->disposition == disp_del)
8659 ui_out_text (uiout, "\nTemporary catchpoint ");
8660 else
8661 ui_out_text (uiout, "\nCatchpoint ");
8662 if (ui_out_is_mi_like_p (uiout))
8663 {
8664 ui_out_field_string (uiout, "reason",
8665 async_reason_lookup (EXEC_ASYNC_EXEC));
8666 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8667 }
8668 ui_out_field_int (uiout, "bkptno", b->number);
8669 ui_out_text (uiout, " (exec'd ");
8670 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8671 ui_out_text (uiout, "), ");
8672
fe798b75 8673 return PRINT_SRC_AND_LOC;
c906108c
SS
8674}
8675
fe798b75 8676static void
a6d9a66e 8677print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8678{
b4d90040 8679 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8680 struct value_print_options opts;
79a45e25 8681 struct ui_out *uiout = current_uiout;
fe798b75
JB
8682
8683 get_user_print_options (&opts);
8684
8685 /* Field 4, the address, is omitted (which makes the columns
8686 not line up too nicely with the headers, but the effect
8687 is relatively readable). */
8688 if (opts.addressprint)
8689 ui_out_field_skip (uiout, "addr");
8690 annotate_field (5);
8691 ui_out_text (uiout, "exec");
b4d90040 8692 if (c->exec_pathname != NULL)
fe798b75
JB
8693 {
8694 ui_out_text (uiout, ", program \"");
b4d90040 8695 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8696 ui_out_text (uiout, "\" ");
8697 }
8ac3646f
TT
8698
8699 if (ui_out_is_mi_like_p (uiout))
8700 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8701}
8702
8703static void
8704print_mention_catch_exec (struct breakpoint *b)
8705{
8706 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8707}
8708
6149aea9
PA
8709/* Implement the "print_recreate" breakpoint_ops method for exec
8710 catchpoints. */
8711
8712static void
8713print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8714{
8715 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8716 print_recreate_thread (b, fp);
6149aea9
PA
8717}
8718
2060206e 8719static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8720
c906108c 8721static int
fba45db2 8722hw_breakpoint_used_count (void)
c906108c 8723{
c906108c 8724 int i = 0;
f1310107
TJB
8725 struct breakpoint *b;
8726 struct bp_location *bl;
c906108c
SS
8727
8728 ALL_BREAKPOINTS (b)
c5aa993b 8729 {
d6b74ac4 8730 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8731 for (bl = b->loc; bl; bl = bl->next)
8732 {
8733 /* Special types of hardware breakpoints may use more than
8734 one register. */
348d480f 8735 i += b->ops->resources_needed (bl);
f1310107 8736 }
c5aa993b 8737 }
c906108c
SS
8738
8739 return i;
8740}
8741
a1398e0c
PA
8742/* Returns the resources B would use if it were a hardware
8743 watchpoint. */
8744
c906108c 8745static int
a1398e0c 8746hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8747{
c906108c 8748 int i = 0;
e09342b5 8749 struct bp_location *bl;
c906108c 8750
a1398e0c
PA
8751 if (!breakpoint_enabled (b))
8752 return 0;
8753
8754 for (bl = b->loc; bl; bl = bl->next)
8755 {
8756 /* Special types of hardware watchpoints may use more than
8757 one register. */
8758 i += b->ops->resources_needed (bl);
8759 }
8760
8761 return i;
8762}
8763
8764/* Returns the sum the used resources of all hardware watchpoints of
8765 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8766 the sum of the used resources of all hardware watchpoints of other
8767 types _not_ TYPE. */
8768
8769static int
8770hw_watchpoint_used_count_others (struct breakpoint *except,
8771 enum bptype type, int *other_type_used)
8772{
8773 int i = 0;
8774 struct breakpoint *b;
8775
c906108c
SS
8776 *other_type_used = 0;
8777 ALL_BREAKPOINTS (b)
e09342b5 8778 {
a1398e0c
PA
8779 if (b == except)
8780 continue;
e09342b5
TJB
8781 if (!breakpoint_enabled (b))
8782 continue;
8783
a1398e0c
PA
8784 if (b->type == type)
8785 i += hw_watchpoint_use_count (b);
8786 else if (is_hardware_watchpoint (b))
8787 *other_type_used = 1;
e09342b5
TJB
8788 }
8789
c906108c
SS
8790 return i;
8791}
8792
c906108c 8793void
fba45db2 8794disable_watchpoints_before_interactive_call_start (void)
c906108c 8795{
c5aa993b 8796 struct breakpoint *b;
c906108c
SS
8797
8798 ALL_BREAKPOINTS (b)
c5aa993b 8799 {
cc60f2e3 8800 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8801 {
b5de0fa7 8802 b->enable_state = bp_call_disabled;
44702360 8803 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8804 }
8805 }
c906108c
SS
8806}
8807
8808void
fba45db2 8809enable_watchpoints_after_interactive_call_stop (void)
c906108c 8810{
c5aa993b 8811 struct breakpoint *b;
c906108c
SS
8812
8813 ALL_BREAKPOINTS (b)
c5aa993b 8814 {
cc60f2e3 8815 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8816 {
b5de0fa7 8817 b->enable_state = bp_enabled;
44702360 8818 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8819 }
8820 }
c906108c
SS
8821}
8822
8bea4e01
UW
8823void
8824disable_breakpoints_before_startup (void)
8825{
6c95b8df 8826 current_program_space->executing_startup = 1;
44702360 8827 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8828}
8829
8830void
8831enable_breakpoints_after_startup (void)
8832{
6c95b8df 8833 current_program_space->executing_startup = 0;
f8eba3c6 8834 breakpoint_re_set ();
8bea4e01
UW
8835}
8836
7c16b83e
PA
8837/* Create a new single-step breakpoint for thread THREAD, with no
8838 locations. */
c906108c 8839
7c16b83e
PA
8840static struct breakpoint *
8841new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8842{
8843 struct breakpoint *b = XNEW (struct breakpoint);
8844
8845 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8846 &momentary_breakpoint_ops);
8847
8848 b->disposition = disp_donttouch;
8849 b->frame_id = null_frame_id;
8850
8851 b->thread = thread;
8852 gdb_assert (b->thread != 0);
8853
8854 add_to_breakpoint_chain (b);
8855
8856 return b;
8857}
8858
8859/* Set a momentary breakpoint of type TYPE at address specified by
8860 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8861 frame. */
c906108c
SS
8862
8863struct breakpoint *
a6d9a66e
UW
8864set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8865 struct frame_id frame_id, enum bptype type)
c906108c 8866{
52f0bd74 8867 struct breakpoint *b;
edb3359d 8868
193facb3
JK
8869 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8870 tail-called one. */
8871 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8872
06edf0c0 8873 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8874 b->enable_state = bp_enabled;
8875 b->disposition = disp_donttouch;
818dd999 8876 b->frame_id = frame_id;
c906108c 8877
4a64f543
MS
8878 /* If we're debugging a multi-threaded program, then we want
8879 momentary breakpoints to be active in only a single thread of
8880 control. */
39f77062
KB
8881 if (in_thread_list (inferior_ptid))
8882 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8883
44702360 8884 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8885
c906108c
SS
8886 return b;
8887}
611c83ae 8888
06edf0c0 8889/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8890 The new breakpoint will have type TYPE, use OPS as its
8891 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8892
06edf0c0
PA
8893static struct breakpoint *
8894momentary_breakpoint_from_master (struct breakpoint *orig,
8895 enum bptype type,
a1aa2221
LM
8896 const struct breakpoint_ops *ops,
8897 int loc_enabled)
e58b0e63
PA
8898{
8899 struct breakpoint *copy;
8900
06edf0c0 8901 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8902 copy->loc = allocate_bp_location (copy);
0e30163f 8903 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8904
a6d9a66e 8905 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8906 copy->loc->requested_address = orig->loc->requested_address;
8907 copy->loc->address = orig->loc->address;
8908 copy->loc->section = orig->loc->section;
6c95b8df 8909 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8910 copy->loc->probe = orig->loc->probe;
f8eba3c6 8911 copy->loc->line_number = orig->loc->line_number;
2f202fde 8912 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8913 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8914 copy->frame_id = orig->frame_id;
8915 copy->thread = orig->thread;
6c95b8df 8916 copy->pspace = orig->pspace;
e58b0e63
PA
8917
8918 copy->enable_state = bp_enabled;
8919 copy->disposition = disp_donttouch;
8920 copy->number = internal_breakpoint_number--;
8921
44702360 8922 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8923 return copy;
8924}
8925
06edf0c0
PA
8926/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8927 ORIG is NULL. */
8928
8929struct breakpoint *
8930clone_momentary_breakpoint (struct breakpoint *orig)
8931{
8932 /* If there's nothing to clone, then return nothing. */
8933 if (orig == NULL)
8934 return NULL;
8935
a1aa2221 8936 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8937}
8938
611c83ae 8939struct breakpoint *
a6d9a66e
UW
8940set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8941 enum bptype type)
611c83ae
PA
8942{
8943 struct symtab_and_line sal;
8944
8945 sal = find_pc_line (pc, 0);
8946 sal.pc = pc;
8947 sal.section = find_pc_overlay (pc);
8948 sal.explicit_pc = 1;
8949
a6d9a66e 8950 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8951}
c906108c 8952\f
c5aa993b 8953
c906108c
SS
8954/* Tell the user we have just set a breakpoint B. */
8955
8956static void
fba45db2 8957mention (struct breakpoint *b)
c906108c 8958{
348d480f 8959 b->ops->print_mention (b);
79a45e25 8960 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8961 return;
c906108c
SS
8962 printf_filtered ("\n");
8963}
c906108c 8964\f
c5aa993b 8965
1a853c52
PA
8966static int bp_loc_is_permanent (struct bp_location *loc);
8967
0d381245 8968static struct bp_location *
39d61571 8969add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8970 const struct symtab_and_line *sal)
8971{
8972 struct bp_location *loc, **tmp;
3742cc8b
YQ
8973 CORE_ADDR adjusted_address;
8974 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8975
8976 if (loc_gdbarch == NULL)
8977 loc_gdbarch = b->gdbarch;
8978
8979 /* Adjust the breakpoint's address prior to allocating a location.
8980 Once we call allocate_bp_location(), that mostly uninitialized
8981 location will be placed on the location chain. Adjustment of the
8982 breakpoint may cause target_read_memory() to be called and we do
8983 not want its scan of the location chain to find a breakpoint and
8984 location that's only been partially initialized. */
8985 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8986 sal->pc, b->type);
0d381245 8987
d30113d4 8988 /* Sort the locations by their ADDRESS. */
39d61571 8989 loc = allocate_bp_location (b);
d30113d4
JK
8990 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8991 tmp = &((*tmp)->next))
0d381245 8992 ;
d30113d4 8993 loc->next = *tmp;
0d381245 8994 *tmp = loc;
3742cc8b 8995
0d381245 8996 loc->requested_address = sal->pc;
3742cc8b 8997 loc->address = adjusted_address;
6c95b8df 8998 loc->pspace = sal->pspace;
729662a5
TT
8999 loc->probe.probe = sal->probe;
9000 loc->probe.objfile = sal->objfile;
6c95b8df 9001 gdb_assert (loc->pspace != NULL);
0d381245 9002 loc->section = sal->section;
3742cc8b 9003 loc->gdbarch = loc_gdbarch;
f8eba3c6 9004 loc->line_number = sal->line;
2f202fde 9005 loc->symtab = sal->symtab;
f8eba3c6 9006
0e30163f
JK
9007 set_breakpoint_location_function (loc,
9008 sal->explicit_pc || sal->explicit_line);
1a853c52 9009
6ae88661
LM
9010 /* While by definition, permanent breakpoints are already present in the
9011 code, we don't mark the location as inserted. Normally one would expect
9012 that GDB could rely on that breakpoint instruction to stop the program,
9013 thus removing the need to insert its own breakpoint, except that executing
9014 the breakpoint instruction can kill the target instead of reporting a
9015 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9016 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9017 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9018 breakpoint be inserted normally results in QEMU knowing about the GDB
9019 breakpoint, and thus trap before the breakpoint instruction is executed.
9020 (If GDB later needs to continue execution past the permanent breakpoint,
9021 it manually increments the PC, thus avoiding executing the breakpoint
9022 instruction.) */
1a853c52 9023 if (bp_loc_is_permanent (loc))
6ae88661 9024 loc->permanent = 1;
1a853c52 9025
0d381245
VP
9026 return loc;
9027}
514f746b
AR
9028\f
9029
1cf4d951 9030/* See breakpoint.h. */
514f746b 9031
1cf4d951
PA
9032int
9033program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9034{
9035 int len;
9036 CORE_ADDR addr;
1afeeb75 9037 const gdb_byte *bpoint;
514f746b 9038 gdb_byte *target_mem;
939c61fa
JK
9039 struct cleanup *cleanup;
9040 int retval = 0;
514f746b 9041
1cf4d951
PA
9042 addr = address;
9043 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9044
9045 /* Software breakpoints unsupported? */
9046 if (bpoint == NULL)
9047 return 0;
9048
224c3ddb 9049 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9050
9051 /* Enable the automatic memory restoration from breakpoints while
9052 we read the memory. Otherwise we could say about our temporary
9053 breakpoints they are permanent. */
9054 cleanup = make_show_memory_breakpoints_cleanup (0);
9055
9056 if (target_read_memory (address, target_mem, len) == 0
9057 && memcmp (target_mem, bpoint, len) == 0)
9058 retval = 1;
9059
9060 do_cleanups (cleanup);
9061
9062 return retval;
9063}
9064
9065/* Return 1 if LOC is pointing to a permanent breakpoint,
9066 return 0 otherwise. */
9067
9068static int
9069bp_loc_is_permanent (struct bp_location *loc)
9070{
9071 struct cleanup *cleanup;
9072 int retval;
9073
514f746b
AR
9074 gdb_assert (loc != NULL);
9075
244558af
LM
9076 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9077 attempt to read from the addresses the locations of these breakpoint types
9078 point to. program_breakpoint_here_p, below, will attempt to read
9079 memory. */
9080 if (!breakpoint_address_is_meaningful (loc->owner))
9081 return 0;
9082
6c95b8df 9083 cleanup = save_current_space_and_thread ();
6c95b8df 9084 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9085
1cf4d951 9086 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9087
939c61fa
JK
9088 do_cleanups (cleanup);
9089
9090 return retval;
514f746b
AR
9091}
9092
e7e0cddf
SS
9093/* Build a command list for the dprintf corresponding to the current
9094 settings of the dprintf style options. */
9095
9096static void
9097update_dprintf_command_list (struct breakpoint *b)
9098{
9099 char *dprintf_args = b->extra_string;
9100 char *printf_line = NULL;
9101
9102 if (!dprintf_args)
9103 return;
9104
9105 dprintf_args = skip_spaces (dprintf_args);
9106
9107 /* Allow a comma, as it may have terminated a location, but don't
9108 insist on it. */
9109 if (*dprintf_args == ',')
9110 ++dprintf_args;
9111 dprintf_args = skip_spaces (dprintf_args);
9112
9113 if (*dprintf_args != '"')
9114 error (_("Bad format string, missing '\"'."));
9115
d3ce09f5 9116 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9117 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9118 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9119 {
9120 if (!dprintf_function)
9121 error (_("No function supplied for dprintf call"));
9122
9123 if (dprintf_channel && strlen (dprintf_channel) > 0)
9124 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9125 dprintf_function,
9126 dprintf_channel,
9127 dprintf_args);
9128 else
9129 printf_line = xstrprintf ("call (void) %s (%s)",
9130 dprintf_function,
9131 dprintf_args);
9132 }
d3ce09f5
SS
9133 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9134 {
9135 if (target_can_run_breakpoint_commands ())
9136 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9137 else
9138 {
9139 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9140 printf_line = xstrprintf ("printf %s", dprintf_args);
9141 }
9142 }
e7e0cddf
SS
9143 else
9144 internal_error (__FILE__, __LINE__,
9145 _("Invalid dprintf style."));
9146
f28045c2 9147 gdb_assert (printf_line != NULL);
9d6e6e84 9148 /* Manufacture a printf sequence. */
f28045c2 9149 {
8d749320 9150 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9151
f28045c2
YQ
9152 printf_cmd_line->control_type = simple_control;
9153 printf_cmd_line->body_count = 0;
9154 printf_cmd_line->body_list = NULL;
9d6e6e84 9155 printf_cmd_line->next = NULL;
f28045c2 9156 printf_cmd_line->line = printf_line;
e7e0cddf 9157
f28045c2
YQ
9158 breakpoint_set_commands (b, printf_cmd_line);
9159 }
e7e0cddf
SS
9160}
9161
9162/* Update all dprintf commands, making their command lists reflect
9163 current style settings. */
9164
9165static void
9166update_dprintf_commands (char *args, int from_tty,
9167 struct cmd_list_element *c)
9168{
9169 struct breakpoint *b;
9170
9171 ALL_BREAKPOINTS (b)
9172 {
9173 if (b->type == bp_dprintf)
9174 update_dprintf_command_list (b);
9175 }
9176}
c3f6f71d 9177
f00aae0f
KS
9178/* Create a breakpoint with SAL as location. Use LOCATION
9179 as a description of the location, and COND_STRING
db107f19 9180 as condition expression. */
018d34a4
VP
9181
9182static void
d9b3f62e 9183init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9184 struct symtabs_and_lines sals,
9185 struct event_location *location,
f8eba3c6 9186 char *filter, char *cond_string,
e7e0cddf 9187 char *extra_string,
d9b3f62e
PA
9188 enum bptype type, enum bpdisp disposition,
9189 int thread, int task, int ignore_count,
c0a91b2b 9190 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9191 int enabled, int internal, unsigned flags,
9192 int display_canonical)
018d34a4 9193{
0d381245 9194 int i;
018d34a4
VP
9195
9196 if (type == bp_hardware_breakpoint)
9197 {
fbbd034e
AS
9198 int target_resources_ok;
9199
9200 i = hw_breakpoint_used_count ();
9201 target_resources_ok =
9202 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9203 i + 1, 0);
9204 if (target_resources_ok == 0)
9205 error (_("No hardware breakpoint support in the target."));
9206 else if (target_resources_ok < 0)
9207 error (_("Hardware breakpoints used exceeds limit."));
9208 }
9209
6c95b8df
PA
9210 gdb_assert (sals.nelts > 0);
9211
0d381245
VP
9212 for (i = 0; i < sals.nelts; ++i)
9213 {
9214 struct symtab_and_line sal = sals.sals[i];
9215 struct bp_location *loc;
9216
9217 if (from_tty)
5af949e3
UW
9218 {
9219 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9220 if (!loc_gdbarch)
9221 loc_gdbarch = gdbarch;
9222
9223 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9224 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9225 }
0d381245
VP
9226
9227 if (i == 0)
9228 {
d9b3f62e 9229 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9230 b->thread = thread;
4a306c9a 9231 b->task = task;
855a6e68 9232
0d381245 9233 b->cond_string = cond_string;
e7e0cddf 9234 b->extra_string = extra_string;
0d381245 9235 b->ignore_count = ignore_count;
41447f92 9236 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9237 b->disposition = disposition;
6c95b8df 9238
44f238bb
PA
9239 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9240 b->loc->inserted = 1;
9241
0fb4aa4b
PA
9242 if (type == bp_static_tracepoint)
9243 {
d9b3f62e 9244 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9245 struct static_tracepoint_marker marker;
9246
983af33b 9247 if (strace_marker_p (b))
0fb4aa4b
PA
9248 {
9249 /* We already know the marker exists, otherwise, we
9250 wouldn't see a sal for it. */
f00aae0f
KS
9251 const char *p = &event_location_to_string (b->location)[3];
9252 const char *endp;
0fb4aa4b 9253 char *marker_str;
0fb4aa4b 9254
f00aae0f 9255 p = skip_spaces_const (p);
0fb4aa4b 9256
f00aae0f 9257 endp = skip_to_space_const (p);
0fb4aa4b
PA
9258
9259 marker_str = savestring (p, endp - p);
d9b3f62e 9260 t->static_trace_marker_id = marker_str;
0fb4aa4b 9261
3e43a32a
MS
9262 printf_filtered (_("Probed static tracepoint "
9263 "marker \"%s\"\n"),
d9b3f62e 9264 t->static_trace_marker_id);
0fb4aa4b
PA
9265 }
9266 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9267 {
d9b3f62e 9268 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9269 release_static_tracepoint_marker (&marker);
9270
3e43a32a
MS
9271 printf_filtered (_("Probed static tracepoint "
9272 "marker \"%s\"\n"),
d9b3f62e 9273 t->static_trace_marker_id);
0fb4aa4b
PA
9274 }
9275 else
3e43a32a
MS
9276 warning (_("Couldn't determine the static "
9277 "tracepoint marker to probe"));
0fb4aa4b
PA
9278 }
9279
0d381245
VP
9280 loc = b->loc;
9281 }
9282 else
018d34a4 9283 {
39d61571 9284 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9285 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9286 loc->inserted = 1;
0d381245
VP
9287 }
9288
9289 if (b->cond_string)
9290 {
bbc13ae3
KS
9291 const char *arg = b->cond_string;
9292
1bb9788d
TT
9293 loc->cond = parse_exp_1 (&arg, loc->address,
9294 block_for_pc (loc->address), 0);
0d381245 9295 if (*arg)
588ae58c 9296 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9297 }
e7e0cddf
SS
9298
9299 /* Dynamic printf requires and uses additional arguments on the
9300 command line, otherwise it's an error. */
9301 if (type == bp_dprintf)
9302 {
9303 if (b->extra_string)
9304 update_dprintf_command_list (b);
9305 else
9306 error (_("Format string required"));
9307 }
9308 else if (b->extra_string)
588ae58c 9309 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9310 }
018d34a4 9311
56435ebe 9312 b->display_canonical = display_canonical;
f00aae0f
KS
9313 if (location != NULL)
9314 b->location = location;
018d34a4 9315 else
a06efdd6 9316 b->location = new_address_location (b->loc->address);
f8eba3c6 9317 b->filter = filter;
d9b3f62e 9318}
018d34a4 9319
d9b3f62e
PA
9320static void
9321create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9322 struct symtabs_and_lines sals,
9323 struct event_location *location,
f8eba3c6 9324 char *filter, char *cond_string,
e7e0cddf 9325 char *extra_string,
d9b3f62e
PA
9326 enum bptype type, enum bpdisp disposition,
9327 int thread, int task, int ignore_count,
c0a91b2b 9328 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9329 int enabled, int internal, unsigned flags,
9330 int display_canonical)
d9b3f62e
PA
9331{
9332 struct breakpoint *b;
9333 struct cleanup *old_chain;
9334
9335 if (is_tracepoint_type (type))
9336 {
9337 struct tracepoint *t;
9338
9339 t = XCNEW (struct tracepoint);
9340 b = &t->base;
9341 }
9342 else
9343 b = XNEW (struct breakpoint);
9344
9345 old_chain = make_cleanup (xfree, b);
9346
9347 init_breakpoint_sal (b, gdbarch,
f00aae0f 9348 sals, location,
e7e0cddf 9349 filter, cond_string, extra_string,
d9b3f62e
PA
9350 type, disposition,
9351 thread, task, ignore_count,
9352 ops, from_tty,
44f238bb
PA
9353 enabled, internal, flags,
9354 display_canonical);
d9b3f62e
PA
9355 discard_cleanups (old_chain);
9356
3ea46bff 9357 install_breakpoint (internal, b, 0);
018d34a4
VP
9358}
9359
9360/* Add SALS.nelts breakpoints to the breakpoint table. For each
9361 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9362 value. COND_STRING, if not NULL, specified the condition to be
9363 used for all breakpoints. Essentially the only case where
9364 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9365 function. In that case, it's still not possible to specify
9366 separate conditions for different overloaded functions, so
9367 we take just a single condition string.
9368
c3f6f71d 9369 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9370 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9371 array contents). If the function fails (error() is called), the
9372 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9373 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9374
9375static void
8cdf0e15 9376create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9377 struct linespec_result *canonical,
e7e0cddf 9378 char *cond_string, char *extra_string,
8cdf0e15
VP
9379 enum bptype type, enum bpdisp disposition,
9380 int thread, int task, int ignore_count,
c0a91b2b 9381 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9382 int enabled, int internal, unsigned flags)
c906108c 9383{
018d34a4 9384 int i;
f8eba3c6 9385 struct linespec_sals *lsal;
cc59ec59 9386
f8eba3c6
TT
9387 if (canonical->pre_expanded)
9388 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9389
9390 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9391 {
f00aae0f 9392 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9393 'break', without arguments. */
f00aae0f
KS
9394 struct event_location *location
9395 = (canonical->location != NULL
9396 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9397 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9398 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9399
f8eba3c6
TT
9400 make_cleanup (xfree, filter_string);
9401 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9402 location,
f8eba3c6 9403 filter_string,
e7e0cddf
SS
9404 cond_string, extra_string,
9405 type, disposition,
84f4c1fe 9406 thread, task, ignore_count, ops,
44f238bb 9407 from_tty, enabled, internal, flags,
56435ebe 9408 canonical->special_display);
f8eba3c6 9409 discard_cleanups (inner);
c3f6f71d 9410 }
c3f6f71d 9411}
c906108c 9412
f00aae0f 9413/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9414 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9415 addresses found. LOCATION points to the end of the SAL (for
9416 linespec locations).
9998af43
TJB
9417
9418 The array and the line spec strings are allocated on the heap, it is
9419 the caller's responsibility to free them. */
c906108c 9420
b9362cc7 9421static void
f00aae0f 9422parse_breakpoint_sals (const struct event_location *location,
58438ac1 9423 struct linespec_result *canonical)
c3f6f71d 9424{
f00aae0f
KS
9425 struct symtab_and_line cursal;
9426
9427 if (event_location_type (location) == LINESPEC_LOCATION)
9428 {
9429 const char *address = get_linespec_location (location);
9430
9431 if (address == NULL)
9432 {
9433 /* The last displayed codepoint, if it's valid, is our default
9434 breakpoint address. */
9435 if (last_displayed_sal_is_valid ())
9436 {
9437 struct linespec_sals lsal;
9438 struct symtab_and_line sal;
9439 CORE_ADDR pc;
9440
9441 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9442 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9443
9444 /* Set sal's pspace, pc, symtab, and line to the values
9445 corresponding to the last call to print_frame_info.
9446 Be sure to reinitialize LINE with NOTCURRENT == 0
9447 as the breakpoint line number is inappropriate otherwise.
9448 find_pc_line would adjust PC, re-set it back. */
9449 get_last_displayed_sal (&sal);
9450 pc = sal.pc;
9451 sal = find_pc_line (pc, 0);
9452
9453 /* "break" without arguments is equivalent to "break *PC"
9454 where PC is the last displayed codepoint's address. So
9455 make sure to set sal.explicit_pc to prevent GDB from
9456 trying to expand the list of sals to include all other
9457 instances with the same symtab and line. */
9458 sal.pc = pc;
9459 sal.explicit_pc = 1;
9460
9461 lsal.sals.sals[0] = sal;
9462 lsal.sals.nelts = 1;
9463 lsal.canonical = NULL;
9464
9465 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9466 return;
9467 }
9468 else
9469 error (_("No default breakpoint address now."));
c906108c 9470 }
c906108c 9471 }
f00aae0f
KS
9472
9473 /* Force almost all breakpoints to be in terms of the
9474 current_source_symtab (which is decode_line_1's default).
9475 This should produce the results we want almost all of the
9476 time while leaving default_breakpoint_* alone.
9477
9478 ObjC: However, don't match an Objective-C method name which
9479 may have a '+' or '-' succeeded by a '['. */
9480 cursal = get_current_source_symtab_and_line ();
9481 if (last_displayed_sal_is_valid ())
c906108c 9482 {
f00aae0f 9483 const char *address = NULL;
cc80f267 9484
f00aae0f
KS
9485 if (event_location_type (location) == LINESPEC_LOCATION)
9486 address = get_linespec_location (location);
cc80f267 9487
f00aae0f
KS
9488 if (!cursal.symtab
9489 || (address != NULL
9490 && strchr ("+-", address[0]) != NULL
9491 && address[1] != '['))
9492 {
9493 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9494 get_last_displayed_symtab (),
9495 get_last_displayed_line (),
9496 canonical, NULL, NULL);
9497 return;
9498 }
c906108c 9499 }
f00aae0f
KS
9500
9501 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9502 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9503}
c906108c 9504
c906108c 9505
c3f6f71d 9506/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9507 inserted as a breakpoint. If it can't throw an error. */
c906108c 9508
b9362cc7 9509static void
23e7acfb 9510breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9511{
9512 int i;
cc59ec59 9513
c3f6f71d 9514 for (i = 0; i < sals->nelts; i++)
ee53e872 9515 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9516}
9517
7a697b8d
SS
9518/* Fast tracepoints may have restrictions on valid locations. For
9519 instance, a fast tracepoint using a jump instead of a trap will
9520 likely have to overwrite more bytes than a trap would, and so can
9521 only be placed where the instruction is longer than the jump, or a
9522 multi-instruction sequence does not have a jump into the middle of
9523 it, etc. */
9524
9525static void
9526check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9527 struct symtabs_and_lines *sals)
9528{
9529 int i, rslt;
9530 struct symtab_and_line *sal;
9531 char *msg;
9532 struct cleanup *old_chain;
9533
9534 for (i = 0; i < sals->nelts; i++)
9535 {
f8eba3c6
TT
9536 struct gdbarch *sarch;
9537
7a697b8d
SS
9538 sal = &sals->sals[i];
9539
f8eba3c6
TT
9540 sarch = get_sal_arch (*sal);
9541 /* We fall back to GDBARCH if there is no architecture
9542 associated with SAL. */
9543 if (sarch == NULL)
9544 sarch = gdbarch;
6b940e6a 9545 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9546 old_chain = make_cleanup (xfree, msg);
9547
9548 if (!rslt)
9549 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9550 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9551
9552 do_cleanups (old_chain);
9553 }
9554}
9555
af4908ba
KS
9556/* Issue an invalid thread ID error. */
9557
9558static void ATTRIBUTE_NORETURN
9559invalid_thread_id_error (int id)
9560{
9561 error (_("Unknown thread %d."), id);
9562}
9563
018d34a4
VP
9564/* Given TOK, a string specification of condition and thread, as
9565 accepted by the 'break' command, extract the condition
9566 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9567 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9568 If no condition is found, *COND_STRING is set to NULL.
9569 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9570
9571static void
bbc13ae3 9572find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9573 char **cond_string, int *thread, int *task,
9574 char **rest)
018d34a4
VP
9575{
9576 *cond_string = NULL;
9577 *thread = -1;
ed1d1739
KS
9578 *task = 0;
9579 *rest = NULL;
9580
018d34a4
VP
9581 while (tok && *tok)
9582 {
bbc13ae3 9583 const char *end_tok;
018d34a4 9584 int toklen;
bbc13ae3
KS
9585 const char *cond_start = NULL;
9586 const char *cond_end = NULL;
cc59ec59 9587
bbc13ae3 9588 tok = skip_spaces_const (tok);
e7e0cddf
SS
9589
9590 if ((*tok == '"' || *tok == ',') && rest)
9591 {
9592 *rest = savestring (tok, strlen (tok));
9593 return;
9594 }
9595
bbc13ae3 9596 end_tok = skip_to_space_const (tok);
d634f2de 9597
018d34a4 9598 toklen = end_tok - tok;
d634f2de 9599
018d34a4
VP
9600 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9601 {
f7545552
TT
9602 struct expression *expr;
9603
018d34a4 9604 tok = cond_start = end_tok + 1;
1bb9788d 9605 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9606 xfree (expr);
018d34a4 9607 cond_end = tok;
d634f2de 9608 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9609 }
9610 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9611 {
9612 char *tmptok;
d634f2de 9613
018d34a4 9614 tok = end_tok + 1;
bbc13ae3 9615 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9616 if (tok == tmptok)
9617 error (_("Junk after thread keyword."));
9618 if (!valid_thread_id (*thread))
af4908ba 9619 invalid_thread_id_error (*thread);
bbc13ae3 9620 tok = tmptok;
018d34a4 9621 }
4a306c9a
JB
9622 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9623 {
9624 char *tmptok;
9625
9626 tok = end_tok + 1;
bbc13ae3 9627 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9628 if (tok == tmptok)
9629 error (_("Junk after task keyword."));
9630 if (!valid_task_id (*task))
b6199126 9631 error (_("Unknown task %d."), *task);
bbc13ae3 9632 tok = tmptok;
4a306c9a 9633 }
e7e0cddf
SS
9634 else if (rest)
9635 {
9636 *rest = savestring (tok, strlen (tok));
ccab2054 9637 return;
e7e0cddf 9638 }
018d34a4
VP
9639 else
9640 error (_("Junk at end of arguments."));
9641 }
9642}
9643
0fb4aa4b
PA
9644/* Decode a static tracepoint marker spec. */
9645
9646static struct symtabs_and_lines
f00aae0f 9647decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9648{
9649 VEC(static_tracepoint_marker_p) *markers = NULL;
9650 struct symtabs_and_lines sals;
0fb4aa4b 9651 struct cleanup *old_chain;
f00aae0f
KS
9652 const char *p = &(*arg_p)[3];
9653 const char *endp;
0fb4aa4b
PA
9654 char *marker_str;
9655 int i;
9656
f00aae0f 9657 p = skip_spaces_const (p);
0fb4aa4b 9658
f00aae0f 9659 endp = skip_to_space_const (p);
0fb4aa4b
PA
9660
9661 marker_str = savestring (p, endp - p);
9662 old_chain = make_cleanup (xfree, marker_str);
9663
9664 markers = target_static_tracepoint_markers_by_strid (marker_str);
9665 if (VEC_empty(static_tracepoint_marker_p, markers))
9666 error (_("No known static tracepoint marker named %s"), marker_str);
9667
9668 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9669 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9670
9671 for (i = 0; i < sals.nelts; i++)
9672 {
9673 struct static_tracepoint_marker *marker;
9674
9675 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9676
9677 init_sal (&sals.sals[i]);
9678
9679 sals.sals[i] = find_pc_line (marker->address, 0);
9680 sals.sals[i].pc = marker->address;
9681
9682 release_static_tracepoint_marker (marker);
9683 }
9684
9685 do_cleanups (old_chain);
9686
9687 *arg_p = endp;
9688 return sals;
9689}
9690
f00aae0f 9691/* See breakpoint.h. */
0101ce28 9692
8cdf0e15
VP
9693int
9694create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9695 const struct event_location *location, char *cond_string,
e7e0cddf 9696 int thread, char *extra_string,
f00aae0f 9697 int parse_extra,
0fb4aa4b 9698 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9699 int ignore_count,
9700 enum auto_boolean pending_break_support,
c0a91b2b 9701 const struct breakpoint_ops *ops,
44f238bb
PA
9702 int from_tty, int enabled, int internal,
9703 unsigned flags)
c3f6f71d 9704{
7efd8fc2 9705 struct linespec_result canonical;
c3f6f71d 9706 struct cleanup *old_chain;
80c99de1 9707 struct cleanup *bkpt_chain = NULL;
0101ce28 9708 int pending = 0;
4a306c9a 9709 int task = 0;
86b17b60 9710 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9711
348d480f
PA
9712 gdb_assert (ops != NULL);
9713
f00aae0f
KS
9714 /* If extra_string isn't useful, set it to NULL. */
9715 if (extra_string != NULL && *extra_string == '\0')
9716 extra_string = NULL;
9717
7efd8fc2 9718 init_linespec_result (&canonical);
c3f6f71d 9719
492d29ea 9720 TRY
b78a6381 9721 {
f00aae0f 9722 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9723 }
492d29ea 9724 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9725 {
492d29ea
PA
9726 /* If caller is interested in rc value from parse, set
9727 value. */
9728 if (e.error == NOT_FOUND_ERROR)
0101ce28 9729 {
05ff989b
AC
9730 /* If pending breakpoint support is turned off, throw
9731 error. */
fa8d40ab
JJ
9732
9733 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9734 throw_exception (e);
9735
9736 exception_print (gdb_stderr, e);
fa8d40ab 9737
05ff989b
AC
9738 /* If pending breakpoint support is auto query and the user
9739 selects no, then simply return the error code. */
059fb39f 9740 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9741 && !nquery (_("Make %s pending on future shared library load? "),
9742 bptype_string (type_wanted)))
fd9b8c24 9743 return 0;
fa8d40ab 9744
05ff989b
AC
9745 /* At this point, either the user was queried about setting
9746 a pending breakpoint and selected yes, or pending
9747 breakpoint behavior is on and thus a pending breakpoint
9748 is defaulted on behalf of the user. */
f00aae0f 9749 pending = 1;
0101ce28 9750 }
492d29ea
PA
9751 else
9752 throw_exception (e);
0101ce28 9753 }
492d29ea
PA
9754 END_CATCH
9755
f00aae0f 9756 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9757 return 0;
c3f6f71d 9758
4a64f543 9759 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9760 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9761
c3f6f71d
JM
9762 /* ----------------------------- SNIP -----------------------------
9763 Anything added to the cleanup chain beyond this point is assumed
9764 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9765 then the memory is not reclaimed. */
9766 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9767
c3f6f71d
JM
9768 /* Resolve all line numbers to PC's and verify that the addresses
9769 are ok for the target. */
0101ce28 9770 if (!pending)
f8eba3c6
TT
9771 {
9772 int ix;
9773 struct linespec_sals *iter;
9774
9775 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9776 breakpoint_sals_to_pc (&iter->sals);
9777 }
c3f6f71d 9778
7a697b8d 9779 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9780 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9781 {
9782 int ix;
9783 struct linespec_sals *iter;
9784
9785 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9786 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9787 }
7a697b8d 9788
c3f6f71d
JM
9789 /* Verify that condition can be parsed, before setting any
9790 breakpoints. Allocate a separate condition expression for each
4a64f543 9791 breakpoint. */
0101ce28 9792 if (!pending)
c3f6f71d 9793 {
f00aae0f 9794 if (parse_extra)
72b2ff0e 9795 {
0878d0fa 9796 char *rest;
52d361e1
YQ
9797 struct linespec_sals *lsal;
9798
9799 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9800
0878d0fa
YQ
9801 /* Here we only parse 'arg' to separate condition
9802 from thread number, so parsing in context of first
9803 sal is OK. When setting the breakpoint we'll
9804 re-parse it in context of each sal. */
9805
f00aae0f
KS
9806 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9807 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9808 if (cond_string)
9809 make_cleanup (xfree, cond_string);
9810 if (rest)
9811 make_cleanup (xfree, rest);
9812 if (rest)
9813 extra_string = rest;
f00aae0f
KS
9814 else
9815 extra_string = NULL;
72b2ff0e 9816 }
2f069f6f 9817 else
72b2ff0e 9818 {
f00aae0f
KS
9819 if (type_wanted != bp_dprintf
9820 && extra_string != NULL && *extra_string != '\0')
9821 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9822
9823 /* Create a private copy of condition string. */
9824 if (cond_string)
9825 {
9826 cond_string = xstrdup (cond_string);
9827 make_cleanup (xfree, cond_string);
9828 }
9829 /* Create a private copy of any extra string. */
9830 if (extra_string)
9831 {
9832 extra_string = xstrdup (extra_string);
9833 make_cleanup (xfree, extra_string);
9834 }
72b2ff0e 9835 }
0fb4aa4b 9836
52d361e1 9837 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9838 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9839 tempflag ? disp_del : disp_donttouch,
9840 thread, task, ignore_count, ops,
44f238bb 9841 from_tty, enabled, internal, flags);
c906108c 9842 }
0101ce28
JJ
9843 else
9844 {
0101ce28
JJ
9845 struct breakpoint *b;
9846
bfccc43c
YQ
9847 if (is_tracepoint_type (type_wanted))
9848 {
9849 struct tracepoint *t;
9850
9851 t = XCNEW (struct tracepoint);
9852 b = &t->base;
9853 }
9854 else
9855 b = XNEW (struct breakpoint);
9856
9857 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9858 b->location = copy_event_location (location);
bfccc43c 9859
f00aae0f
KS
9860 if (parse_extra)
9861 b->cond_string = NULL;
e12c7713
MK
9862 else
9863 {
9864 /* Create a private copy of condition string. */
9865 if (cond_string)
9866 {
9867 cond_string = xstrdup (cond_string);
9868 make_cleanup (xfree, cond_string);
9869 }
9870 b->cond_string = cond_string;
15630549 9871 b->thread = thread;
e12c7713 9872 }
f00aae0f
KS
9873
9874 /* Create a private copy of any extra string. */
9875 if (extra_string != NULL)
9876 {
9877 extra_string = xstrdup (extra_string);
9878 make_cleanup (xfree, extra_string);
9879 }
9880 b->extra_string = extra_string;
0101ce28 9881 b->ignore_count = ignore_count;
0101ce28 9882 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9883 b->condition_not_parsed = 1;
41447f92 9884 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9885 if ((type_wanted != bp_breakpoint
9886 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9887 b->pspace = current_program_space;
8bea4e01 9888
bfccc43c 9889 install_breakpoint (internal, b, 0);
0101ce28
JJ
9890 }
9891
f8eba3c6 9892 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9893 {
3e43a32a
MS
9894 warning (_("Multiple breakpoints were set.\nUse the "
9895 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9896 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9897 }
9898
80c99de1
PA
9899 /* That's it. Discard the cleanups for data inserted into the
9900 breakpoint. */
9901 discard_cleanups (bkpt_chain);
9902 /* But cleanup everything else. */
c3f6f71d 9903 do_cleanups (old_chain);
217dc9e2 9904
80c99de1 9905 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9906 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9907
9908 return 1;
c3f6f71d 9909}
c906108c 9910
348d480f 9911/* Set a breakpoint.
72b2ff0e
VP
9912 ARG is a string describing breakpoint address,
9913 condition, and thread.
9914 FLAG specifies if a breakpoint is hardware on,
9915 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9916 and BP_TEMPFLAG. */
348d480f 9917
98deb0da 9918static void
72b2ff0e 9919break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9920{
72b2ff0e 9921 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9922 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9923 ? bp_hardware_breakpoint
9924 : bp_breakpoint);
55aa24fb 9925 struct breakpoint_ops *ops;
f00aae0f
KS
9926 struct event_location *location;
9927 struct cleanup *cleanup;
9928
9929 location = string_to_event_location (&arg, current_language);
9930 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9931
9932 /* Matching breakpoints on probes. */
5b56227b
KS
9933 if (location != NULL
9934 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9935 ops = &bkpt_probe_breakpoint_ops;
9936 else
9937 ops = &bkpt_breakpoint_ops;
c3f6f71d 9938
8cdf0e15 9939 create_breakpoint (get_current_arch (),
f00aae0f
KS
9940 location,
9941 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9942 tempflag, type_wanted,
8cdf0e15
VP
9943 0 /* Ignore count */,
9944 pending_break_support,
55aa24fb 9945 ops,
8cdf0e15 9946 from_tty,
84f4c1fe 9947 1 /* enabled */,
44f238bb
PA
9948 0 /* internal */,
9949 0);
f00aae0f 9950 do_cleanups (cleanup);
c906108c
SS
9951}
9952
c906108c
SS
9953/* Helper function for break_command_1 and disassemble_command. */
9954
9955void
fba45db2 9956resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9957{
9958 CORE_ADDR pc;
9959
9960 if (sal->pc == 0 && sal->symtab != NULL)
9961 {
9962 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9963 error (_("No line %d in file \"%s\"."),
05cba821 9964 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9965 sal->pc = pc;
6a048695 9966
4a64f543
MS
9967 /* If this SAL corresponds to a breakpoint inserted using a line
9968 number, then skip the function prologue if necessary. */
6a048695 9969 if (sal->explicit_line)
059acae7 9970 skip_prologue_sal (sal);
c906108c
SS
9971 }
9972
9973 if (sal->section == 0 && sal->symtab != NULL)
9974 {
346d1dfe 9975 const struct blockvector *bv;
3977b71f 9976 const struct block *b;
c5aa993b 9977 struct symbol *sym;
c906108c 9978
43f3e411
DE
9979 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9980 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9981 if (bv != NULL)
9982 {
7f0df278 9983 sym = block_linkage_function (b);
c906108c
SS
9984 if (sym != NULL)
9985 {
eb822aa6
DE
9986 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9987 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9988 sym);
c906108c
SS
9989 }
9990 else
9991 {
4a64f543
MS
9992 /* It really is worthwhile to have the section, so we'll
9993 just have to look harder. This case can be executed
9994 if we have line numbers but no functions (as can
9995 happen in assembly source). */
c906108c 9996
7cbd4a93 9997 struct bound_minimal_symbol msym;
6c95b8df
PA
9998 struct cleanup *old_chain = save_current_space_and_thread ();
9999
10000 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10001
10002 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10003 if (msym.minsym)
efd66ac6 10004 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10005
10006 do_cleanups (old_chain);
c906108c
SS
10007 }
10008 }
10009 }
10010}
10011
10012void
fba45db2 10013break_command (char *arg, int from_tty)
c906108c 10014{
db107f19 10015 break_command_1 (arg, 0, from_tty);
c906108c
SS
10016}
10017
c906108c 10018void
fba45db2 10019tbreak_command (char *arg, int from_tty)
c906108c 10020{
db107f19 10021 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10022}
10023
c906108c 10024static void
fba45db2 10025hbreak_command (char *arg, int from_tty)
c906108c 10026{
db107f19 10027 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10028}
10029
10030static void
fba45db2 10031thbreak_command (char *arg, int from_tty)
c906108c 10032{
db107f19 10033 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10034}
10035
10036static void
fba45db2 10037stop_command (char *arg, int from_tty)
c906108c 10038{
a3f17187 10039 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10040Usage: stop in <function | address>\n\
a3f17187 10041 stop at <line>\n"));
c906108c
SS
10042}
10043
10044static void
fba45db2 10045stopin_command (char *arg, int from_tty)
c906108c
SS
10046{
10047 int badInput = 0;
10048
c5aa993b 10049 if (arg == (char *) NULL)
c906108c
SS
10050 badInput = 1;
10051 else if (*arg != '*')
10052 {
10053 char *argptr = arg;
10054 int hasColon = 0;
10055
4a64f543 10056 /* Look for a ':'. If this is a line number specification, then
53a5351d 10057 say it is bad, otherwise, it should be an address or
4a64f543 10058 function/method name. */
c906108c 10059 while (*argptr && !hasColon)
c5aa993b
JM
10060 {
10061 hasColon = (*argptr == ':');
10062 argptr++;
10063 }
c906108c
SS
10064
10065 if (hasColon)
c5aa993b 10066 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10067 else
c5aa993b 10068 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10069 }
10070
10071 if (badInput)
a3f17187 10072 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10073 else
db107f19 10074 break_command_1 (arg, 0, from_tty);
c906108c
SS
10075}
10076
10077static void
fba45db2 10078stopat_command (char *arg, int from_tty)
c906108c
SS
10079{
10080 int badInput = 0;
10081
c5aa993b 10082 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10083 badInput = 1;
10084 else
10085 {
10086 char *argptr = arg;
10087 int hasColon = 0;
10088
4a64f543
MS
10089 /* Look for a ':'. If there is a '::' then get out, otherwise
10090 it is probably a line number. */
c906108c 10091 while (*argptr && !hasColon)
c5aa993b
JM
10092 {
10093 hasColon = (*argptr == ':');
10094 argptr++;
10095 }
c906108c
SS
10096
10097 if (hasColon)
c5aa993b 10098 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10099 else
c5aa993b 10100 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10101 }
10102
10103 if (badInput)
a3f17187 10104 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10105 else
db107f19 10106 break_command_1 (arg, 0, from_tty);
c906108c
SS
10107}
10108
e7e0cddf
SS
10109/* The dynamic printf command is mostly like a regular breakpoint, but
10110 with a prewired command list consisting of a single output command,
10111 built from extra arguments supplied on the dprintf command
10112 line. */
10113
da821c7b 10114static void
e7e0cddf
SS
10115dprintf_command (char *arg, int from_tty)
10116{
f00aae0f
KS
10117 struct event_location *location;
10118 struct cleanup *cleanup;
10119
10120 location = string_to_event_location (&arg, current_language);
10121 cleanup = make_cleanup_delete_event_location (location);
10122
10123 /* If non-NULL, ARG should have been advanced past the location;
10124 the next character must be ','. */
10125 if (arg != NULL)
10126 {
10127 if (arg[0] != ',' || arg[1] == '\0')
10128 error (_("Format string required"));
10129 else
10130 {
10131 /* Skip the comma. */
10132 ++arg;
10133 }
10134 }
10135
e7e0cddf 10136 create_breakpoint (get_current_arch (),
f00aae0f
KS
10137 location,
10138 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10139 0, bp_dprintf,
10140 0 /* Ignore count */,
10141 pending_break_support,
10142 &dprintf_breakpoint_ops,
10143 from_tty,
10144 1 /* enabled */,
10145 0 /* internal */,
10146 0);
f00aae0f 10147 do_cleanups (cleanup);
e7e0cddf
SS
10148}
10149
d3ce09f5
SS
10150static void
10151agent_printf_command (char *arg, int from_tty)
10152{
10153 error (_("May only run agent-printf on the target"));
10154}
10155
f1310107
TJB
10156/* Implement the "breakpoint_hit" breakpoint_ops method for
10157 ranged breakpoints. */
10158
10159static int
10160breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10161 struct address_space *aspace,
09ac7c10
TT
10162 CORE_ADDR bp_addr,
10163 const struct target_waitstatus *ws)
f1310107 10164{
09ac7c10 10165 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10166 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10167 return 0;
10168
f1310107
TJB
10169 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10170 bl->length, aspace, bp_addr);
10171}
10172
10173/* Implement the "resources_needed" breakpoint_ops method for
10174 ranged breakpoints. */
10175
10176static int
10177resources_needed_ranged_breakpoint (const struct bp_location *bl)
10178{
10179 return target_ranged_break_num_registers ();
10180}
10181
10182/* Implement the "print_it" breakpoint_ops method for
10183 ranged breakpoints. */
10184
10185static enum print_stop_action
348d480f 10186print_it_ranged_breakpoint (bpstat bs)
f1310107 10187{
348d480f 10188 struct breakpoint *b = bs->breakpoint_at;
f1310107 10189 struct bp_location *bl = b->loc;
79a45e25 10190 struct ui_out *uiout = current_uiout;
f1310107
TJB
10191
10192 gdb_assert (b->type == bp_hardware_breakpoint);
10193
10194 /* Ranged breakpoints have only one location. */
10195 gdb_assert (bl && bl->next == NULL);
10196
10197 annotate_breakpoint (b->number);
10198 if (b->disposition == disp_del)
10199 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10200 else
10201 ui_out_text (uiout, "\nRanged breakpoint ");
10202 if (ui_out_is_mi_like_p (uiout))
10203 {
10204 ui_out_field_string (uiout, "reason",
10205 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10206 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10207 }
10208 ui_out_field_int (uiout, "bkptno", b->number);
10209 ui_out_text (uiout, ", ");
10210
10211 return PRINT_SRC_AND_LOC;
10212}
10213
10214/* Implement the "print_one" breakpoint_ops method for
10215 ranged breakpoints. */
10216
10217static void
10218print_one_ranged_breakpoint (struct breakpoint *b,
10219 struct bp_location **last_loc)
10220{
10221 struct bp_location *bl = b->loc;
10222 struct value_print_options opts;
79a45e25 10223 struct ui_out *uiout = current_uiout;
f1310107
TJB
10224
10225 /* Ranged breakpoints have only one location. */
10226 gdb_assert (bl && bl->next == NULL);
10227
10228 get_user_print_options (&opts);
10229
10230 if (opts.addressprint)
10231 /* We don't print the address range here, it will be printed later
10232 by print_one_detail_ranged_breakpoint. */
10233 ui_out_field_skip (uiout, "addr");
10234 annotate_field (5);
10235 print_breakpoint_location (b, bl);
10236 *last_loc = bl;
10237}
10238
10239/* Implement the "print_one_detail" breakpoint_ops method for
10240 ranged breakpoints. */
10241
10242static void
10243print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10244 struct ui_out *uiout)
10245{
10246 CORE_ADDR address_start, address_end;
10247 struct bp_location *bl = b->loc;
f99d8bf4
PA
10248 struct ui_file *stb = mem_fileopen ();
10249 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10250
10251 gdb_assert (bl);
10252
10253 address_start = bl->address;
10254 address_end = address_start + bl->length - 1;
10255
10256 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10257 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10258 print_core_address (bl->gdbarch, address_start),
10259 print_core_address (bl->gdbarch, address_end));
10260 ui_out_field_stream (uiout, "addr", stb);
10261 ui_out_text (uiout, "\n");
10262
10263 do_cleanups (cleanup);
10264}
10265
10266/* Implement the "print_mention" breakpoint_ops method for
10267 ranged breakpoints. */
10268
10269static void
10270print_mention_ranged_breakpoint (struct breakpoint *b)
10271{
10272 struct bp_location *bl = b->loc;
79a45e25 10273 struct ui_out *uiout = current_uiout;
f1310107
TJB
10274
10275 gdb_assert (bl);
10276 gdb_assert (b->type == bp_hardware_breakpoint);
10277
10278 if (ui_out_is_mi_like_p (uiout))
10279 return;
10280
10281 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10282 b->number, paddress (bl->gdbarch, bl->address),
10283 paddress (bl->gdbarch, bl->address + bl->length - 1));
10284}
10285
10286/* Implement the "print_recreate" breakpoint_ops method for
10287 ranged breakpoints. */
10288
10289static void
10290print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10291{
f00aae0f
KS
10292 fprintf_unfiltered (fp, "break-range %s, %s",
10293 event_location_to_string (b->location),
10294 event_location_to_string (b->location_range_end));
d9b3f62e 10295 print_recreate_thread (b, fp);
f1310107
TJB
10296}
10297
10298/* The breakpoint_ops structure to be used in ranged breakpoints. */
10299
2060206e 10300static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10301
10302/* Find the address where the end of the breakpoint range should be
10303 placed, given the SAL of the end of the range. This is so that if
10304 the user provides a line number, the end of the range is set to the
10305 last instruction of the given line. */
10306
10307static CORE_ADDR
10308find_breakpoint_range_end (struct symtab_and_line sal)
10309{
10310 CORE_ADDR end;
10311
10312 /* If the user provided a PC value, use it. Otherwise,
10313 find the address of the end of the given location. */
10314 if (sal.explicit_pc)
10315 end = sal.pc;
10316 else
10317 {
10318 int ret;
10319 CORE_ADDR start;
10320
10321 ret = find_line_pc_range (sal, &start, &end);
10322 if (!ret)
10323 error (_("Could not find location of the end of the range."));
10324
10325 /* find_line_pc_range returns the start of the next line. */
10326 end--;
10327 }
10328
10329 return end;
10330}
10331
10332/* Implement the "break-range" CLI command. */
10333
10334static void
10335break_range_command (char *arg, int from_tty)
10336{
10337 char *arg_start, *addr_string_start, *addr_string_end;
10338 struct linespec_result canonical_start, canonical_end;
10339 int bp_count, can_use_bp, length;
10340 CORE_ADDR end;
10341 struct breakpoint *b;
10342 struct symtab_and_line sal_start, sal_end;
f1310107 10343 struct cleanup *cleanup_bkpt;
f8eba3c6 10344 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10345 struct event_location *start_location, *end_location;
f1310107
TJB
10346
10347 /* We don't support software ranged breakpoints. */
10348 if (target_ranged_break_num_registers () < 0)
10349 error (_("This target does not support hardware ranged breakpoints."));
10350
10351 bp_count = hw_breakpoint_used_count ();
10352 bp_count += target_ranged_break_num_registers ();
10353 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10354 bp_count, 0);
10355 if (can_use_bp < 0)
10356 error (_("Hardware breakpoints used exceeds limit."));
10357
f8eba3c6 10358 arg = skip_spaces (arg);
f1310107
TJB
10359 if (arg == NULL || arg[0] == '\0')
10360 error(_("No address range specified."));
10361
f1310107
TJB
10362 init_linespec_result (&canonical_start);
10363
f8eba3c6 10364 arg_start = arg;
f00aae0f
KS
10365 start_location = string_to_event_location (&arg, current_language);
10366 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10367 parse_breakpoint_sals (start_location, &canonical_start);
10368 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10369
10370 if (arg[0] != ',')
10371 error (_("Too few arguments."));
f8eba3c6 10372 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10373 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10374
10375 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10376
10377 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10378 || lsal_start->sals.nelts != 1)
f1310107
TJB
10379 error (_("Cannot create a ranged breakpoint with multiple locations."));
10380
f8eba3c6
TT
10381 sal_start = lsal_start->sals.sals[0];
10382 addr_string_start = savestring (arg_start, arg - arg_start);
10383 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10384
10385 arg++; /* Skip the comma. */
f8eba3c6 10386 arg = skip_spaces (arg);
f1310107
TJB
10387
10388 /* Parse the end location. */
10389
f1310107
TJB
10390 init_linespec_result (&canonical_end);
10391 arg_start = arg;
10392
f8eba3c6 10393 /* We call decode_line_full directly here instead of using
f1310107
TJB
10394 parse_breakpoint_sals because we need to specify the start location's
10395 symtab and line as the default symtab and line for the end of the
10396 range. This makes it possible to have ranges like "foo.c:27, +14",
10397 where +14 means 14 lines from the start location. */
f00aae0f
KS
10398 end_location = string_to_event_location (&arg, current_language);
10399 make_cleanup_delete_event_location (end_location);
10400 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6
TT
10401 sal_start.symtab, sal_start.line,
10402 &canonical_end, NULL, NULL);
10403
10404 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10405
f8eba3c6 10406 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10407 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10408
10409 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10410 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10411 || lsal_end->sals.nelts != 1)
f1310107
TJB
10412 error (_("Cannot create a ranged breakpoint with multiple locations."));
10413
f8eba3c6 10414 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10415
10416 end = find_breakpoint_range_end (sal_end);
10417 if (sal_start.pc > end)
177b42fe 10418 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10419
10420 length = end - sal_start.pc + 1;
10421 if (length < 0)
10422 /* Length overflowed. */
10423 error (_("Address range too large."));
10424 else if (length == 1)
10425 {
10426 /* This range is simple enough to be handled by
10427 the `hbreak' command. */
10428 hbreak_command (addr_string_start, 1);
10429
10430 do_cleanups (cleanup_bkpt);
10431
10432 return;
10433 }
10434
10435 /* Now set up the breakpoint. */
10436 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10437 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10438 set_breakpoint_count (breakpoint_count + 1);
10439 b->number = breakpoint_count;
10440 b->disposition = disp_donttouch;
f00aae0f
KS
10441 b->location = copy_event_location (start_location);
10442 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10443 b->loc->length = length;
10444
f8eba3c6 10445 do_cleanups (cleanup_bkpt);
f1310107
TJB
10446
10447 mention (b);
8d3788bd 10448 observer_notify_breakpoint_created (b);
44702360 10449 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10450}
10451
4a64f543
MS
10452/* Return non-zero if EXP is verified as constant. Returned zero
10453 means EXP is variable. Also the constant detection may fail for
10454 some constant expressions and in such case still falsely return
10455 zero. */
2e6e3d9c 10456
65d79d4b
SDJ
10457static int
10458watchpoint_exp_is_const (const struct expression *exp)
10459{
10460 int i = exp->nelts;
10461
10462 while (i > 0)
10463 {
10464 int oplenp, argsp;
10465
10466 /* We are only interested in the descriptor of each element. */
10467 operator_length (exp, i, &oplenp, &argsp);
10468 i -= oplenp;
10469
10470 switch (exp->elts[i].opcode)
10471 {
10472 case BINOP_ADD:
10473 case BINOP_SUB:
10474 case BINOP_MUL:
10475 case BINOP_DIV:
10476 case BINOP_REM:
10477 case BINOP_MOD:
10478 case BINOP_LSH:
10479 case BINOP_RSH:
10480 case BINOP_LOGICAL_AND:
10481 case BINOP_LOGICAL_OR:
10482 case BINOP_BITWISE_AND:
10483 case BINOP_BITWISE_IOR:
10484 case BINOP_BITWISE_XOR:
10485 case BINOP_EQUAL:
10486 case BINOP_NOTEQUAL:
10487 case BINOP_LESS:
10488 case BINOP_GTR:
10489 case BINOP_LEQ:
10490 case BINOP_GEQ:
10491 case BINOP_REPEAT:
10492 case BINOP_COMMA:
10493 case BINOP_EXP:
10494 case BINOP_MIN:
10495 case BINOP_MAX:
10496 case BINOP_INTDIV:
10497 case BINOP_CONCAT:
65d79d4b
SDJ
10498 case TERNOP_COND:
10499 case TERNOP_SLICE:
65d79d4b
SDJ
10500
10501 case OP_LONG:
10502 case OP_DOUBLE:
10503 case OP_DECFLOAT:
10504 case OP_LAST:
10505 case OP_COMPLEX:
10506 case OP_STRING:
65d79d4b
SDJ
10507 case OP_ARRAY:
10508 case OP_TYPE:
608b4967
TT
10509 case OP_TYPEOF:
10510 case OP_DECLTYPE:
6e72ca20 10511 case OP_TYPEID:
65d79d4b
SDJ
10512 case OP_NAME:
10513 case OP_OBJC_NSSTRING:
10514
10515 case UNOP_NEG:
10516 case UNOP_LOGICAL_NOT:
10517 case UNOP_COMPLEMENT:
10518 case UNOP_ADDR:
10519 case UNOP_HIGH:
aeaa2474 10520 case UNOP_CAST:
9eaf6705
TT
10521
10522 case UNOP_CAST_TYPE:
10523 case UNOP_REINTERPRET_CAST:
10524 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10525 /* Unary, binary and ternary operators: We have to check
10526 their operands. If they are constant, then so is the
10527 result of that operation. For instance, if A and B are
10528 determined to be constants, then so is "A + B".
10529
10530 UNOP_IND is one exception to the rule above, because the
10531 value of *ADDR is not necessarily a constant, even when
10532 ADDR is. */
65d79d4b
SDJ
10533 break;
10534
10535 case OP_VAR_VALUE:
10536 /* Check whether the associated symbol is a constant.
4a64f543 10537
65d79d4b 10538 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10539 possible that a buggy compiler could mark a variable as
10540 constant even when it is not, and TYPE_CONST would return
10541 true in this case, while SYMBOL_CLASS wouldn't.
10542
10543 We also have to check for function symbols because they
10544 are always constant. */
65d79d4b
SDJ
10545 {
10546 struct symbol *s = exp->elts[i + 2].symbol;
10547
10548 if (SYMBOL_CLASS (s) != LOC_BLOCK
10549 && SYMBOL_CLASS (s) != LOC_CONST
10550 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10551 return 0;
10552 break;
10553 }
10554
10555 /* The default action is to return 0 because we are using
10556 the optimistic approach here: If we don't know something,
10557 then it is not a constant. */
10558 default:
10559 return 0;
10560 }
10561 }
10562
10563 return 1;
10564}
10565
3a5c3e22
PA
10566/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10567
10568static void
10569dtor_watchpoint (struct breakpoint *self)
10570{
10571 struct watchpoint *w = (struct watchpoint *) self;
10572
10573 xfree (w->cond_exp);
10574 xfree (w->exp);
10575 xfree (w->exp_string);
10576 xfree (w->exp_string_reparse);
10577 value_free (w->val);
10578
10579 base_breakpoint_ops.dtor (self);
10580}
10581
348d480f
PA
10582/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10583
10584static void
10585re_set_watchpoint (struct breakpoint *b)
10586{
3a5c3e22
PA
10587 struct watchpoint *w = (struct watchpoint *) b;
10588
348d480f
PA
10589 /* Watchpoint can be either on expression using entirely global
10590 variables, or it can be on local variables.
10591
10592 Watchpoints of the first kind are never auto-deleted, and even
10593 persist across program restarts. Since they can use variables
10594 from shared libraries, we need to reparse expression as libraries
10595 are loaded and unloaded.
10596
10597 Watchpoints on local variables can also change meaning as result
10598 of solib event. For example, if a watchpoint uses both a local
10599 and a global variables in expression, it's a local watchpoint,
10600 but unloading of a shared library will make the expression
10601 invalid. This is not a very common use case, but we still
10602 re-evaluate expression, to avoid surprises to the user.
10603
10604 Note that for local watchpoints, we re-evaluate it only if
10605 watchpoints frame id is still valid. If it's not, it means the
10606 watchpoint is out of scope and will be deleted soon. In fact,
10607 I'm not sure we'll ever be called in this case.
10608
10609 If a local watchpoint's frame id is still valid, then
3a5c3e22 10610 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10611
3a5c3e22
PA
10612 Don't do anything about disabled watchpoints, since they will be
10613 reevaluated again when enabled. */
10614 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10615}
10616
77b06cd7
TJB
10617/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10618
10619static int
10620insert_watchpoint (struct bp_location *bl)
10621{
3a5c3e22
PA
10622 struct watchpoint *w = (struct watchpoint *) bl->owner;
10623 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10624
10625 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10626 w->cond_exp);
77b06cd7
TJB
10627}
10628
10629/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10630
10631static int
10632remove_watchpoint (struct bp_location *bl)
10633{
3a5c3e22
PA
10634 struct watchpoint *w = (struct watchpoint *) bl->owner;
10635 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10636
10637 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10638 w->cond_exp);
e09342b5
TJB
10639}
10640
e09342b5 10641static int
348d480f 10642breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10643 struct address_space *aspace, CORE_ADDR bp_addr,
10644 const struct target_waitstatus *ws)
e09342b5 10645{
348d480f 10646 struct breakpoint *b = bl->owner;
3a5c3e22 10647 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10648
348d480f
PA
10649 /* Continuable hardware watchpoints are treated as non-existent if the
10650 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10651 some data address). Otherwise gdb won't stop on a break instruction
10652 in the code (not from a breakpoint) when a hardware watchpoint has
10653 been defined. Also skip watchpoints which we know did not trigger
10654 (did not match the data address). */
10655 if (is_hardware_watchpoint (b)
3a5c3e22 10656 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10657 return 0;
9c06b0b4 10658
348d480f 10659 return 1;
9c06b0b4
TJB
10660}
10661
348d480f
PA
10662static void
10663check_status_watchpoint (bpstat bs)
9c06b0b4 10664{
348d480f 10665 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10666
348d480f 10667 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10668}
10669
10670/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10671 hardware watchpoints. */
9c06b0b4
TJB
10672
10673static int
348d480f 10674resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10675{
3a5c3e22
PA
10676 struct watchpoint *w = (struct watchpoint *) bl->owner;
10677 int length = w->exact? 1 : bl->length;
348d480f
PA
10678
10679 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10680}
10681
10682/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10683 hardware watchpoints. */
9c06b0b4
TJB
10684
10685static int
348d480f 10686works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10687{
efa80663
PA
10688 /* Read and access watchpoints only work with hardware support. */
10689 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10690}
10691
9c06b0b4 10692static enum print_stop_action
348d480f 10693print_it_watchpoint (bpstat bs)
9c06b0b4 10694{
348d480f
PA
10695 struct cleanup *old_chain;
10696 struct breakpoint *b;
f99d8bf4 10697 struct ui_file *stb;
348d480f 10698 enum print_stop_action result;
3a5c3e22 10699 struct watchpoint *w;
79a45e25 10700 struct ui_out *uiout = current_uiout;
348d480f
PA
10701
10702 gdb_assert (bs->bp_location_at != NULL);
10703
348d480f 10704 b = bs->breakpoint_at;
3a5c3e22 10705 w = (struct watchpoint *) b;
348d480f 10706
f99d8bf4
PA
10707 stb = mem_fileopen ();
10708 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10709
10710 switch (b->type)
10711 {
348d480f 10712 case bp_watchpoint:
9c06b0b4
TJB
10713 case bp_hardware_watchpoint:
10714 annotate_watchpoint (b->number);
10715 if (ui_out_is_mi_like_p (uiout))
10716 ui_out_field_string
10717 (uiout, "reason",
10718 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10719 mention (b);
10720 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10721 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10722 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10723 ui_out_field_stream (uiout, "old", stb);
10724 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10725 watchpoint_value_print (w->val, stb);
348d480f
PA
10726 ui_out_field_stream (uiout, "new", stb);
10727 ui_out_text (uiout, "\n");
10728 /* More than one watchpoint may have been triggered. */
10729 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10730 break;
10731
10732 case bp_read_watchpoint:
10733 if (ui_out_is_mi_like_p (uiout))
10734 ui_out_field_string
10735 (uiout, "reason",
10736 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10737 mention (b);
10738 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10739 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10740 watchpoint_value_print (w->val, stb);
348d480f
PA
10741 ui_out_field_stream (uiout, "value", stb);
10742 ui_out_text (uiout, "\n");
10743 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10744 break;
10745
10746 case bp_access_watchpoint:
348d480f
PA
10747 if (bs->old_val != NULL)
10748 {
10749 annotate_watchpoint (b->number);
10750 if (ui_out_is_mi_like_p (uiout))
10751 ui_out_field_string
10752 (uiout, "reason",
10753 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10754 mention (b);
10755 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10756 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10757 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10758 ui_out_field_stream (uiout, "old", stb);
10759 ui_out_text (uiout, "\nNew value = ");
10760 }
10761 else
10762 {
10763 mention (b);
10764 if (ui_out_is_mi_like_p (uiout))
10765 ui_out_field_string
10766 (uiout, "reason",
10767 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10768 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10769 ui_out_text (uiout, "\nValue = ");
10770 }
f99d8bf4 10771 watchpoint_value_print (w->val, stb);
348d480f
PA
10772 ui_out_field_stream (uiout, "new", stb);
10773 ui_out_text (uiout, "\n");
10774 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10775 break;
10776 default:
348d480f 10777 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10778 }
10779
348d480f
PA
10780 do_cleanups (old_chain);
10781 return result;
10782}
10783
10784/* Implement the "print_mention" breakpoint_ops method for hardware
10785 watchpoints. */
10786
10787static void
10788print_mention_watchpoint (struct breakpoint *b)
10789{
10790 struct cleanup *ui_out_chain;
3a5c3e22 10791 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10792 struct ui_out *uiout = current_uiout;
348d480f
PA
10793
10794 switch (b->type)
10795 {
10796 case bp_watchpoint:
10797 ui_out_text (uiout, "Watchpoint ");
10798 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10799 break;
10800 case bp_hardware_watchpoint:
10801 ui_out_text (uiout, "Hardware watchpoint ");
10802 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10803 break;
10804 case bp_read_watchpoint:
10805 ui_out_text (uiout, "Hardware read watchpoint ");
10806 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10807 break;
10808 case bp_access_watchpoint:
10809 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10810 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10811 break;
10812 default:
10813 internal_error (__FILE__, __LINE__,
10814 _("Invalid hardware watchpoint type."));
10815 }
10816
10817 ui_out_field_int (uiout, "number", b->number);
10818 ui_out_text (uiout, ": ");
3a5c3e22 10819 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10820 do_cleanups (ui_out_chain);
10821}
10822
10823/* Implement the "print_recreate" breakpoint_ops method for
10824 watchpoints. */
10825
10826static void
10827print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10828{
3a5c3e22
PA
10829 struct watchpoint *w = (struct watchpoint *) b;
10830
348d480f
PA
10831 switch (b->type)
10832 {
10833 case bp_watchpoint:
10834 case bp_hardware_watchpoint:
10835 fprintf_unfiltered (fp, "watch");
10836 break;
10837 case bp_read_watchpoint:
10838 fprintf_unfiltered (fp, "rwatch");
10839 break;
10840 case bp_access_watchpoint:
10841 fprintf_unfiltered (fp, "awatch");
10842 break;
10843 default:
10844 internal_error (__FILE__, __LINE__,
10845 _("Invalid watchpoint type."));
10846 }
10847
3a5c3e22 10848 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10849 print_recreate_thread (b, fp);
348d480f
PA
10850}
10851
427cd150
TT
10852/* Implement the "explains_signal" breakpoint_ops method for
10853 watchpoints. */
10854
47591c29 10855static int
427cd150
TT
10856explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10857{
10858 /* A software watchpoint cannot cause a signal other than
10859 GDB_SIGNAL_TRAP. */
10860 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10861 return 0;
427cd150 10862
47591c29 10863 return 1;
427cd150
TT
10864}
10865
348d480f
PA
10866/* The breakpoint_ops structure to be used in hardware watchpoints. */
10867
2060206e 10868static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10869
10870/* Implement the "insert" breakpoint_ops method for
10871 masked hardware watchpoints. */
10872
10873static int
10874insert_masked_watchpoint (struct bp_location *bl)
10875{
3a5c3e22
PA
10876 struct watchpoint *w = (struct watchpoint *) bl->owner;
10877
10878 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10879 bl->watchpoint_type);
10880}
10881
10882/* Implement the "remove" breakpoint_ops method for
10883 masked hardware watchpoints. */
10884
10885static int
10886remove_masked_watchpoint (struct bp_location *bl)
10887{
3a5c3e22
PA
10888 struct watchpoint *w = (struct watchpoint *) bl->owner;
10889
10890 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10891 bl->watchpoint_type);
10892}
10893
10894/* Implement the "resources_needed" breakpoint_ops method for
10895 masked hardware watchpoints. */
10896
10897static int
10898resources_needed_masked_watchpoint (const struct bp_location *bl)
10899{
3a5c3e22
PA
10900 struct watchpoint *w = (struct watchpoint *) bl->owner;
10901
10902 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10903}
10904
10905/* Implement the "works_in_software_mode" breakpoint_ops method for
10906 masked hardware watchpoints. */
10907
10908static int
10909works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10910{
10911 return 0;
10912}
10913
10914/* Implement the "print_it" breakpoint_ops method for
10915 masked hardware watchpoints. */
10916
10917static enum print_stop_action
10918print_it_masked_watchpoint (bpstat bs)
10919{
10920 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10921 struct ui_out *uiout = current_uiout;
348d480f
PA
10922
10923 /* Masked watchpoints have only one location. */
10924 gdb_assert (b->loc && b->loc->next == NULL);
10925
10926 switch (b->type)
10927 {
10928 case bp_hardware_watchpoint:
10929 annotate_watchpoint (b->number);
10930 if (ui_out_is_mi_like_p (uiout))
10931 ui_out_field_string
10932 (uiout, "reason",
10933 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10934 break;
10935
10936 case bp_read_watchpoint:
10937 if (ui_out_is_mi_like_p (uiout))
10938 ui_out_field_string
10939 (uiout, "reason",
10940 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10941 break;
10942
10943 case bp_access_watchpoint:
10944 if (ui_out_is_mi_like_p (uiout))
10945 ui_out_field_string
10946 (uiout, "reason",
10947 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10948 break;
10949 default:
10950 internal_error (__FILE__, __LINE__,
10951 _("Invalid hardware watchpoint type."));
10952 }
10953
10954 mention (b);
9c06b0b4
TJB
10955 ui_out_text (uiout, _("\n\
10956Check the underlying instruction at PC for the memory\n\
10957address and value which triggered this watchpoint.\n"));
10958 ui_out_text (uiout, "\n");
10959
10960 /* More than one watchpoint may have been triggered. */
10961 return PRINT_UNKNOWN;
10962}
10963
10964/* Implement the "print_one_detail" breakpoint_ops method for
10965 masked hardware watchpoints. */
10966
10967static void
10968print_one_detail_masked_watchpoint (const struct breakpoint *b,
10969 struct ui_out *uiout)
10970{
3a5c3e22
PA
10971 struct watchpoint *w = (struct watchpoint *) b;
10972
9c06b0b4
TJB
10973 /* Masked watchpoints have only one location. */
10974 gdb_assert (b->loc && b->loc->next == NULL);
10975
10976 ui_out_text (uiout, "\tmask ");
3a5c3e22 10977 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10978 ui_out_text (uiout, "\n");
10979}
10980
10981/* Implement the "print_mention" breakpoint_ops method for
10982 masked hardware watchpoints. */
10983
10984static void
10985print_mention_masked_watchpoint (struct breakpoint *b)
10986{
3a5c3e22 10987 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10988 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10989 struct cleanup *ui_out_chain;
10990
10991 switch (b->type)
10992 {
10993 case bp_hardware_watchpoint:
10994 ui_out_text (uiout, "Masked hardware watchpoint ");
10995 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10996 break;
10997 case bp_read_watchpoint:
10998 ui_out_text (uiout, "Masked hardware read watchpoint ");
10999 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11000 break;
11001 case bp_access_watchpoint:
11002 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11003 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11004 break;
11005 default:
11006 internal_error (__FILE__, __LINE__,
11007 _("Invalid hardware watchpoint type."));
11008 }
11009
11010 ui_out_field_int (uiout, "number", b->number);
11011 ui_out_text (uiout, ": ");
3a5c3e22 11012 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11013 do_cleanups (ui_out_chain);
11014}
11015
11016/* Implement the "print_recreate" breakpoint_ops method for
11017 masked hardware watchpoints. */
11018
11019static void
11020print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11021{
3a5c3e22 11022 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11023 char tmp[40];
11024
11025 switch (b->type)
11026 {
11027 case bp_hardware_watchpoint:
11028 fprintf_unfiltered (fp, "watch");
11029 break;
11030 case bp_read_watchpoint:
11031 fprintf_unfiltered (fp, "rwatch");
11032 break;
11033 case bp_access_watchpoint:
11034 fprintf_unfiltered (fp, "awatch");
11035 break;
11036 default:
11037 internal_error (__FILE__, __LINE__,
11038 _("Invalid hardware watchpoint type."));
11039 }
11040
3a5c3e22
PA
11041 sprintf_vma (tmp, w->hw_wp_mask);
11042 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11043 print_recreate_thread (b, fp);
9c06b0b4
TJB
11044}
11045
11046/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11047
2060206e 11048static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11049
11050/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11051
11052static int
11053is_masked_watchpoint (const struct breakpoint *b)
11054{
11055 return b->ops == &masked_watchpoint_breakpoint_ops;
11056}
11057
53a5351d
JM
11058/* accessflag: hw_write: watch write,
11059 hw_read: watch read,
11060 hw_access: watch access (read or write) */
c906108c 11061static void
bbc13ae3 11062watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11063 int just_location, int internal)
c906108c 11064{
d983da9c 11065 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11066 struct expression *exp;
270140bd 11067 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11068 struct value *val, *mark, *result;
bb9d5f81 11069 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11070 struct frame_info *frame;
bbc13ae3
KS
11071 const char *exp_start = NULL;
11072 const char *exp_end = NULL;
11073 const char *tok, *end_tok;
9c06b0b4 11074 int toklen = -1;
bbc13ae3
KS
11075 const char *cond_start = NULL;
11076 const char *cond_end = NULL;
c906108c 11077 enum bptype bp_type;
37e4754d 11078 int thread = -1;
0cf6dd15 11079 int pc = 0;
9c06b0b4
TJB
11080 /* Flag to indicate whether we are going to use masks for
11081 the hardware watchpoint. */
11082 int use_mask = 0;
11083 CORE_ADDR mask = 0;
3a5c3e22 11084 struct watchpoint *w;
bbc13ae3
KS
11085 char *expression;
11086 struct cleanup *back_to;
c906108c 11087
37e4754d
LM
11088 /* Make sure that we actually have parameters to parse. */
11089 if (arg != NULL && arg[0] != '\0')
11090 {
bbc13ae3
KS
11091 const char *value_start;
11092
11093 exp_end = arg + strlen (arg);
37e4754d 11094
9c06b0b4
TJB
11095 /* Look for "parameter value" pairs at the end
11096 of the arguments string. */
bbc13ae3 11097 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11098 {
11099 /* Skip whitespace at the end of the argument list. */
11100 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11101 tok--;
11102
11103 /* Find the beginning of the last token.
11104 This is the value of the parameter. */
11105 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11106 tok--;
11107 value_start = tok + 1;
11108
11109 /* Skip whitespace. */
11110 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11111 tok--;
11112
11113 end_tok = tok;
11114
11115 /* Find the beginning of the second to last token.
11116 This is the parameter itself. */
11117 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11118 tok--;
11119 tok++;
11120 toklen = end_tok - tok + 1;
11121
61012eef 11122 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11123 {
11124 /* At this point we've found a "thread" token, which means
11125 the user is trying to set a watchpoint that triggers
11126 only in a specific thread. */
11127 char *endp;
37e4754d 11128
9c06b0b4
TJB
11129 if (thread != -1)
11130 error(_("You can specify only one thread."));
37e4754d 11131
9c06b0b4
TJB
11132 /* Extract the thread ID from the next token. */
11133 thread = strtol (value_start, &endp, 0);
37e4754d 11134
9c06b0b4
TJB
11135 /* Check if the user provided a valid numeric value for the
11136 thread ID. */
11137 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11138 error (_("Invalid thread ID specification %s."), value_start);
11139
11140 /* Check if the thread actually exists. */
11141 if (!valid_thread_id (thread))
af4908ba 11142 invalid_thread_id_error (thread);
9c06b0b4 11143 }
61012eef 11144 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11145 {
11146 /* We've found a "mask" token, which means the user wants to
11147 create a hardware watchpoint that is going to have the mask
11148 facility. */
11149 struct value *mask_value, *mark;
37e4754d 11150
9c06b0b4
TJB
11151 if (use_mask)
11152 error(_("You can specify only one mask."));
37e4754d 11153
9c06b0b4 11154 use_mask = just_location = 1;
37e4754d 11155
9c06b0b4
TJB
11156 mark = value_mark ();
11157 mask_value = parse_to_comma_and_eval (&value_start);
11158 mask = value_as_address (mask_value);
11159 value_free_to_mark (mark);
11160 }
11161 else
11162 /* We didn't recognize what we found. We should stop here. */
11163 break;
37e4754d 11164
9c06b0b4
TJB
11165 /* Truncate the string and get rid of the "parameter value" pair before
11166 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11167 exp_end = tok;
9c06b0b4 11168 }
37e4754d 11169 }
bbc13ae3
KS
11170 else
11171 exp_end = arg;
37e4754d 11172
bbc13ae3
KS
11173 /* Parse the rest of the arguments. From here on out, everything
11174 is in terms of a newly allocated string instead of the original
11175 ARG. */
c906108c 11176 innermost_block = NULL;
bbc13ae3
KS
11177 expression = savestring (arg, exp_end - arg);
11178 back_to = make_cleanup (xfree, expression);
11179 exp_start = arg = expression;
1bb9788d 11180 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11181 exp_end = arg;
fa8a61dc
TT
11182 /* Remove trailing whitespace from the expression before saving it.
11183 This makes the eventual display of the expression string a bit
11184 prettier. */
11185 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11186 --exp_end;
11187
65d79d4b
SDJ
11188 /* Checking if the expression is not constant. */
11189 if (watchpoint_exp_is_const (exp))
11190 {
11191 int len;
11192
11193 len = exp_end - exp_start;
11194 while (len > 0 && isspace (exp_start[len - 1]))
11195 len--;
11196 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11197 }
11198
c906108c
SS
11199 exp_valid_block = innermost_block;
11200 mark = value_mark ();
3a1115a0 11201 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11202
bb9d5f81
PP
11203 if (val != NULL && just_location)
11204 {
11205 saved_bitpos = value_bitpos (val);
11206 saved_bitsize = value_bitsize (val);
11207 }
11208
06a64a0b
TT
11209 if (just_location)
11210 {
9c06b0b4
TJB
11211 int ret;
11212
06a64a0b 11213 exp_valid_block = NULL;
a1442452 11214 val = value_addr (result);
06a64a0b
TT
11215 release_value (val);
11216 value_free_to_mark (mark);
9c06b0b4
TJB
11217
11218 if (use_mask)
11219 {
11220 ret = target_masked_watch_num_registers (value_as_address (val),
11221 mask);
11222 if (ret == -1)
11223 error (_("This target does not support masked watchpoints."));
11224 else if (ret == -2)
11225 error (_("Invalid mask or memory region."));
11226 }
06a64a0b
TT
11227 }
11228 else if (val != NULL)
fa4727a6 11229 release_value (val);
c906108c 11230
bbc13ae3
KS
11231 tok = skip_spaces_const (arg);
11232 end_tok = skip_to_space_const (tok);
c906108c
SS
11233
11234 toklen = end_tok - tok;
11235 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11236 {
2d134ed3
PA
11237 struct expression *cond;
11238
60e1c644 11239 innermost_block = NULL;
c906108c 11240 tok = cond_start = end_tok + 1;
1bb9788d 11241 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11242
11243 /* The watchpoint expression may not be local, but the condition
11244 may still be. E.g.: `watch global if local > 0'. */
11245 cond_exp_valid_block = innermost_block;
11246
2d134ed3 11247 xfree (cond);
c906108c
SS
11248 cond_end = tok;
11249 }
11250 if (*tok)
8a3fe4f8 11251 error (_("Junk at end of command."));
c906108c 11252
d983da9c 11253 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11254
11255 /* If the expression is "local", then set up a "watchpoint scope"
11256 breakpoint at the point where we've left the scope of the watchpoint
11257 expression. Create the scope breakpoint before the watchpoint, so
11258 that we will encounter it first in bpstat_stop_status. */
60e1c644 11259 if (exp_valid_block && frame)
d983da9c 11260 {
edb3359d
DJ
11261 if (frame_id_p (frame_unwind_caller_id (frame)))
11262 {
11263 scope_breakpoint
a6d9a66e
UW
11264 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11265 frame_unwind_caller_pc (frame),
06edf0c0
PA
11266 bp_watchpoint_scope,
11267 &momentary_breakpoint_ops);
d983da9c 11268
edb3359d 11269 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11270
edb3359d
DJ
11271 /* Automatically delete the breakpoint when it hits. */
11272 scope_breakpoint->disposition = disp_del;
d983da9c 11273
edb3359d
DJ
11274 /* Only break in the proper frame (help with recursion). */
11275 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11276
edb3359d 11277 /* Set the address at which we will stop. */
a6d9a66e
UW
11278 scope_breakpoint->loc->gdbarch
11279 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11280 scope_breakpoint->loc->requested_address
11281 = frame_unwind_caller_pc (frame);
11282 scope_breakpoint->loc->address
a6d9a66e
UW
11283 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11284 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11285 scope_breakpoint->type);
11286 }
d983da9c
DJ
11287 }
11288
e8369a73
AB
11289 /* Now set up the breakpoint. We create all watchpoints as hardware
11290 watchpoints here even if hardware watchpoints are turned off, a call
11291 to update_watchpoint later in this function will cause the type to
11292 drop back to bp_watchpoint (software watchpoint) if required. */
11293
11294 if (accessflag == hw_read)
11295 bp_type = bp_read_watchpoint;
11296 else if (accessflag == hw_access)
11297 bp_type = bp_access_watchpoint;
11298 else
11299 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11300
11301 w = XCNEW (struct watchpoint);
11302 b = &w->base;
348d480f 11303 if (use_mask)
3a5c3e22
PA
11304 init_raw_breakpoint_without_location (b, NULL, bp_type,
11305 &masked_watchpoint_breakpoint_ops);
348d480f 11306 else
3a5c3e22
PA
11307 init_raw_breakpoint_without_location (b, NULL, bp_type,
11308 &watchpoint_breakpoint_ops);
37e4754d 11309 b->thread = thread;
b5de0fa7 11310 b->disposition = disp_donttouch;
348d480f 11311 b->pspace = current_program_space;
3a5c3e22
PA
11312 w->exp = exp;
11313 w->exp_valid_block = exp_valid_block;
11314 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11315 if (just_location)
11316 {
11317 struct type *t = value_type (val);
11318 CORE_ADDR addr = value_as_address (val);
11319 char *name;
11320
11321 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11322 name = type_to_string (t);
11323
3a5c3e22 11324 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11325 core_addr_to_string (addr));
06a64a0b
TT
11326 xfree (name);
11327
3a5c3e22 11328 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11329 (int) (exp_end - exp_start), exp_start);
11330
06a64a0b
TT
11331 /* The above expression is in C. */
11332 b->language = language_c;
11333 }
11334 else
3a5c3e22 11335 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11336
11337 if (use_mask)
11338 {
3a5c3e22 11339 w->hw_wp_mask = mask;
9c06b0b4
TJB
11340 }
11341 else
11342 {
3a5c3e22 11343 w->val = val;
bb9d5f81
PP
11344 w->val_bitpos = saved_bitpos;
11345 w->val_bitsize = saved_bitsize;
3a5c3e22 11346 w->val_valid = 1;
9c06b0b4 11347 }
77b06cd7 11348
c906108c
SS
11349 if (cond_start)
11350 b->cond_string = savestring (cond_start, cond_end - cond_start);
11351 else
11352 b->cond_string = 0;
c5aa993b 11353
c906108c 11354 if (frame)
f6bc2008 11355 {
3a5c3e22
PA
11356 w->watchpoint_frame = get_frame_id (frame);
11357 w->watchpoint_thread = inferior_ptid;
f6bc2008 11358 }
c906108c 11359 else
f6bc2008 11360 {
3a5c3e22
PA
11361 w->watchpoint_frame = null_frame_id;
11362 w->watchpoint_thread = null_ptid;
f6bc2008 11363 }
c906108c 11364
d983da9c 11365 if (scope_breakpoint != NULL)
c906108c 11366 {
d983da9c
DJ
11367 /* The scope breakpoint is related to the watchpoint. We will
11368 need to act on them together. */
11369 b->related_breakpoint = scope_breakpoint;
11370 scope_breakpoint->related_breakpoint = b;
c906108c 11371 }
d983da9c 11372
06a64a0b
TT
11373 if (!just_location)
11374 value_free_to_mark (mark);
2d134ed3 11375
492d29ea 11376 TRY
a9634178
TJB
11377 {
11378 /* Finally update the new watchpoint. This creates the locations
11379 that should be inserted. */
3a5c3e22 11380 update_watchpoint (w, 1);
a9634178 11381 }
492d29ea 11382 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11383 {
11384 delete_breakpoint (b);
11385 throw_exception (e);
11386 }
492d29ea 11387 END_CATCH
a9634178 11388
3ea46bff 11389 install_breakpoint (internal, b, 1);
bbc13ae3 11390 do_cleanups (back_to);
c906108c
SS
11391}
11392
e09342b5 11393/* Return count of debug registers needed to watch the given expression.
e09342b5 11394 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11395
c906108c 11396static int
a9634178 11397can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11398{
11399 int found_memory_cnt = 0;
2e70b7b9 11400 struct value *head = v;
c906108c
SS
11401
11402 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11403 if (!can_use_hw_watchpoints)
c906108c 11404 return 0;
c5aa993b 11405
5c44784c
JM
11406 /* Make sure that the value of the expression depends only upon
11407 memory contents, and values computed from them within GDB. If we
11408 find any register references or function calls, we can't use a
11409 hardware watchpoint.
11410
11411 The idea here is that evaluating an expression generates a series
11412 of values, one holding the value of every subexpression. (The
11413 expression a*b+c has five subexpressions: a, b, a*b, c, and
11414 a*b+c.) GDB's values hold almost enough information to establish
11415 the criteria given above --- they identify memory lvalues,
11416 register lvalues, computed values, etcetera. So we can evaluate
11417 the expression, and then scan the chain of values that leaves
11418 behind to decide whether we can detect any possible change to the
11419 expression's final value using only hardware watchpoints.
11420
11421 However, I don't think that the values returned by inferior
11422 function calls are special in any way. So this function may not
11423 notice that an expression involving an inferior function call
11424 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11425 for (; v; v = value_next (v))
c906108c 11426 {
5c44784c 11427 if (VALUE_LVAL (v) == lval_memory)
c906108c 11428 {
8464be76
DJ
11429 if (v != head && value_lazy (v))
11430 /* A lazy memory lvalue in the chain is one that GDB never
11431 needed to fetch; we either just used its address (e.g.,
11432 `a' in `a.b') or we never needed it at all (e.g., `a'
11433 in `a,b'). This doesn't apply to HEAD; if that is
11434 lazy then it was not readable, but watch it anyway. */
5c44784c 11435 ;
53a5351d 11436 else
5c44784c
JM
11437 {
11438 /* Ahh, memory we actually used! Check if we can cover
11439 it with hardware watchpoints. */
df407dfe 11440 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11441
11442 /* We only watch structs and arrays if user asked for it
11443 explicitly, never if they just happen to appear in a
11444 middle of some value chain. */
11445 if (v == head
11446 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11447 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11448 {
42ae5230 11449 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11450 int len;
11451 int num_regs;
11452
a9634178 11453 len = (target_exact_watchpoints
e09342b5
TJB
11454 && is_scalar_type_recursive (vtype))?
11455 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11456
e09342b5
TJB
11457 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11458 if (!num_regs)
2e70b7b9
MS
11459 return 0;
11460 else
e09342b5 11461 found_memory_cnt += num_regs;
2e70b7b9 11462 }
5c44784c 11463 }
c5aa993b 11464 }
5086187c
AC
11465 else if (VALUE_LVAL (v) != not_lval
11466 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11467 return 0; /* These are values from the history (e.g., $1). */
5086187c 11468 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11469 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11470 }
11471
11472 /* The expression itself looks suitable for using a hardware
11473 watchpoint, but give the target machine a chance to reject it. */
11474 return found_memory_cnt;
11475}
11476
8b93c638 11477void
84f4c1fe 11478watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11479{
84f4c1fe 11480 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11481}
11482
06a64a0b
TT
11483/* A helper function that looks for the "-location" argument and then
11484 calls watch_command_1. */
11485
11486static void
11487watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11488{
11489 int just_location = 0;
11490
11491 if (arg
11492 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11493 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11494 {
e9cafbcc 11495 arg = skip_spaces (arg);
06a64a0b
TT
11496 just_location = 1;
11497 }
11498
84f4c1fe 11499 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11500}
8926118c 11501
c5aa993b 11502static void
fba45db2 11503watch_command (char *arg, int from_tty)
c906108c 11504{
06a64a0b 11505 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11506}
11507
8b93c638 11508void
84f4c1fe 11509rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11510{
84f4c1fe 11511 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11512}
8926118c 11513
c5aa993b 11514static void
fba45db2 11515rwatch_command (char *arg, int from_tty)
c906108c 11516{
06a64a0b 11517 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11518}
11519
8b93c638 11520void
84f4c1fe 11521awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11522{
84f4c1fe 11523 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11524}
8926118c 11525
c5aa993b 11526static void
fba45db2 11527awatch_command (char *arg, int from_tty)
c906108c 11528{
06a64a0b 11529 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11530}
c906108c 11531\f
c5aa993b 11532
cfc31633
PA
11533/* Data for the FSM that manages the until(location)/advance commands
11534 in infcmd.c. Here because it uses the mechanisms of
11535 breakpoints. */
c906108c 11536
cfc31633 11537struct until_break_fsm
bfec99b2 11538{
cfc31633
PA
11539 /* The base class. */
11540 struct thread_fsm thread_fsm;
11541
11542 /* The thread that as current when the command was executed. */
11543 int thread;
11544
11545 /* The breakpoint set at the destination location. */
11546 struct breakpoint *location_breakpoint;
11547
11548 /* Breakpoint set at the return address in the caller frame. May be
11549 NULL. */
11550 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11551};
11552
cfc31633
PA
11553static void until_break_fsm_clean_up (struct thread_fsm *self);
11554static int until_break_fsm_should_stop (struct thread_fsm *self);
11555static enum async_reply_reason
11556 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11557
11558/* until_break_fsm's vtable. */
11559
11560static struct thread_fsm_ops until_break_fsm_ops =
11561{
11562 NULL, /* dtor */
11563 until_break_fsm_clean_up,
11564 until_break_fsm_should_stop,
11565 NULL, /* return_value */
11566 until_break_fsm_async_reply_reason,
11567};
11568
11569/* Allocate a new until_break_command_fsm. */
11570
11571static struct until_break_fsm *
11572new_until_break_fsm (int thread,
11573 struct breakpoint *location_breakpoint,
11574 struct breakpoint *caller_breakpoint)
11575{
11576 struct until_break_fsm *sm;
11577
11578 sm = XCNEW (struct until_break_fsm);
11579 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11580
11581 sm->thread = thread;
11582 sm->location_breakpoint = location_breakpoint;
11583 sm->caller_breakpoint = caller_breakpoint;
11584
11585 return sm;
11586}
11587
11588/* Implementation of the 'should_stop' FSM method for the
11589 until(location)/advance commands. */
11590
11591static int
11592until_break_fsm_should_stop (struct thread_fsm *self)
11593{
11594 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11595 struct thread_info *tp = inferior_thread ();
11596
11597 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11598 sm->location_breakpoint) != NULL
11599 || (sm->caller_breakpoint != NULL
11600 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11601 sm->caller_breakpoint) != NULL))
11602 thread_fsm_set_finished (self);
11603
11604 return 1;
11605}
11606
11607/* Implementation of the 'clean_up' FSM method for the
11608 until(location)/advance commands. */
11609
c2c6d25f 11610static void
cfc31633 11611until_break_fsm_clean_up (struct thread_fsm *self)
43ff13b4 11612{
cfc31633 11613 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11614
cfc31633
PA
11615 /* Clean up our temporary breakpoints. */
11616 if (sm->location_breakpoint != NULL)
11617 {
11618 delete_breakpoint (sm->location_breakpoint);
11619 sm->location_breakpoint = NULL;
11620 }
11621 if (sm->caller_breakpoint != NULL)
11622 {
11623 delete_breakpoint (sm->caller_breakpoint);
11624 sm->caller_breakpoint = NULL;
11625 }
11626 delete_longjmp_breakpoint (sm->thread);
11627}
11628
11629/* Implementation of the 'async_reply_reason' FSM method for the
11630 until(location)/advance commands. */
11631
11632static enum async_reply_reason
11633until_break_fsm_async_reply_reason (struct thread_fsm *self)
11634{
11635 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11636}
11637
c906108c 11638void
ae66c1fc 11639until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11640{
11641 struct symtabs_and_lines sals;
11642 struct symtab_and_line sal;
8556afb4
PA
11643 struct frame_info *frame;
11644 struct gdbarch *frame_gdbarch;
11645 struct frame_id stack_frame_id;
11646 struct frame_id caller_frame_id;
cfc31633
PA
11647 struct breakpoint *location_breakpoint;
11648 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11649 struct cleanup *old_chain, *cleanup;
186c406b
TT
11650 int thread;
11651 struct thread_info *tp;
f00aae0f 11652 struct event_location *location;
cfc31633 11653 struct until_break_fsm *sm;
c906108c 11654
70509625 11655 clear_proceed_status (0);
c906108c
SS
11656
11657 /* Set a breakpoint where the user wants it and at return from
4a64f543 11658 this function. */
c5aa993b 11659
f00aae0f
KS
11660 location = string_to_event_location (&arg, current_language);
11661 cleanup = make_cleanup_delete_event_location (location);
11662
1bfeeb0f 11663 if (last_displayed_sal_is_valid ())
f00aae0f 11664 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11665 get_last_displayed_symtab (),
f8eba3c6 11666 get_last_displayed_line ());
c906108c 11667 else
f00aae0f 11668 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6 11669 (struct symtab *) NULL, 0);
c5aa993b 11670
c906108c 11671 if (sals.nelts != 1)
8a3fe4f8 11672 error (_("Couldn't get information on specified line."));
c5aa993b 11673
c906108c 11674 sal = sals.sals[0];
4a64f543 11675 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11676
c906108c 11677 if (*arg)
8a3fe4f8 11678 error (_("Junk at end of arguments."));
c5aa993b 11679
c906108c 11680 resolve_sal_pc (&sal);
c5aa993b 11681
186c406b
TT
11682 tp = inferior_thread ();
11683 thread = tp->num;
11684
883bc8d1
PA
11685 old_chain = make_cleanup (null_cleanup, NULL);
11686
8556afb4
PA
11687 /* Note linespec handling above invalidates the frame chain.
11688 Installing a breakpoint also invalidates the frame chain (as it
11689 may need to switch threads), so do any frame handling before
11690 that. */
11691
11692 frame = get_selected_frame (NULL);
11693 frame_gdbarch = get_frame_arch (frame);
11694 stack_frame_id = get_stack_frame_id (frame);
11695 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11696
ae66c1fc
EZ
11697 /* Keep within the current frame, or in frames called by the current
11698 one. */
edb3359d 11699
883bc8d1 11700 if (frame_id_p (caller_frame_id))
c906108c 11701 {
883bc8d1 11702 struct symtab_and_line sal2;
cfc31633 11703 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11704
11705 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11706 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11707 caller_gdbarch = frame_unwind_caller_arch (frame);
11708 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11709 sal2,
11710 caller_frame_id,
11711 bp_until);
11712 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11713
883bc8d1 11714 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11715 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11716 }
c5aa993b 11717
c70a6932
JK
11718 /* set_momentary_breakpoint could invalidate FRAME. */
11719 frame = NULL;
11720
883bc8d1
PA
11721 if (anywhere)
11722 /* If the user told us to continue until a specified location,
11723 we don't specify a frame at which we need to stop. */
cfc31633
PA
11724 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11725 null_frame_id, bp_until);
883bc8d1
PA
11726 else
11727 /* Otherwise, specify the selected frame, because we want to stop
11728 only at the very same frame. */
cfc31633
PA
11729 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11730 stack_frame_id, bp_until);
11731 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11732
cfc31633
PA
11733 sm = new_until_break_fsm (tp->num, location_breakpoint, caller_breakpoint);
11734 tp->thread_fsm = &sm->thread_fsm;
f107f563 11735
cfc31633 11736 discard_cleanups (old_chain);
f107f563 11737
cfc31633 11738 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11739
11740 do_cleanups (cleanup);
c906108c 11741}
ae66c1fc 11742
c906108c
SS
11743/* This function attempts to parse an optional "if <cond>" clause
11744 from the arg string. If one is not found, it returns NULL.
c5aa993b 11745
c906108c
SS
11746 Else, it returns a pointer to the condition string. (It does not
11747 attempt to evaluate the string against a particular block.) And,
11748 it updates arg to point to the first character following the parsed
4a64f543 11749 if clause in the arg string. */
53a5351d 11750
916703c0 11751char *
fba45db2 11752ep_parse_optional_if_clause (char **arg)
c906108c 11753{
c5aa993b
JM
11754 char *cond_string;
11755
11756 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11757 return NULL;
c5aa993b 11758
4a64f543 11759 /* Skip the "if" keyword. */
c906108c 11760 (*arg) += 2;
c5aa993b 11761
c906108c 11762 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11763 condition string. */
e9cafbcc 11764 *arg = skip_spaces (*arg);
c906108c 11765 cond_string = *arg;
c5aa993b 11766
4a64f543
MS
11767 /* Assume that the condition occupies the remainder of the arg
11768 string. */
c906108c 11769 (*arg) += strlen (cond_string);
c5aa993b 11770
c906108c
SS
11771 return cond_string;
11772}
c5aa993b 11773
c906108c
SS
11774/* Commands to deal with catching events, such as signals, exceptions,
11775 process start/exit, etc. */
c5aa993b
JM
11776
11777typedef enum
11778{
44feb3ce
TT
11779 catch_fork_temporary, catch_vfork_temporary,
11780 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11781}
11782catch_fork_kind;
11783
c906108c 11784static void
cc59ec59
MS
11785catch_fork_command_1 (char *arg, int from_tty,
11786 struct cmd_list_element *command)
c906108c 11787{
a6d9a66e 11788 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11789 char *cond_string = NULL;
44feb3ce
TT
11790 catch_fork_kind fork_kind;
11791 int tempflag;
11792
11793 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11794 tempflag = (fork_kind == catch_fork_temporary
11795 || fork_kind == catch_vfork_temporary);
c5aa993b 11796
44feb3ce
TT
11797 if (!arg)
11798 arg = "";
e9cafbcc 11799 arg = skip_spaces (arg);
c5aa993b 11800
c906108c 11801 /* The allowed syntax is:
c5aa993b
JM
11802 catch [v]fork
11803 catch [v]fork if <cond>
11804
4a64f543 11805 First, check if there's an if clause. */
c906108c 11806 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11807
c906108c 11808 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11809 error (_("Junk at end of arguments."));
c5aa993b 11810
c906108c 11811 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11812 and enable reporting of such events. */
c5aa993b
JM
11813 switch (fork_kind)
11814 {
44feb3ce
TT
11815 case catch_fork_temporary:
11816 case catch_fork_permanent:
a6d9a66e 11817 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11818 &catch_fork_breakpoint_ops);
c906108c 11819 break;
44feb3ce
TT
11820 case catch_vfork_temporary:
11821 case catch_vfork_permanent:
a6d9a66e 11822 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11823 &catch_vfork_breakpoint_ops);
c906108c 11824 break;
c5aa993b 11825 default:
8a3fe4f8 11826 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11827 break;
c5aa993b 11828 }
c906108c
SS
11829}
11830
11831static void
cc59ec59
MS
11832catch_exec_command_1 (char *arg, int from_tty,
11833 struct cmd_list_element *command)
c906108c 11834{
b4d90040 11835 struct exec_catchpoint *c;
a6d9a66e 11836 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11837 int tempflag;
c5aa993b 11838 char *cond_string = NULL;
c906108c 11839
44feb3ce
TT
11840 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11841
11842 if (!arg)
11843 arg = "";
e9cafbcc 11844 arg = skip_spaces (arg);
c906108c
SS
11845
11846 /* The allowed syntax is:
c5aa993b
JM
11847 catch exec
11848 catch exec if <cond>
c906108c 11849
4a64f543 11850 First, check if there's an if clause. */
c906108c
SS
11851 cond_string = ep_parse_optional_if_clause (&arg);
11852
11853 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11854 error (_("Junk at end of arguments."));
c906108c 11855
b4d90040
PA
11856 c = XNEW (struct exec_catchpoint);
11857 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11858 &catch_exec_breakpoint_ops);
11859 c->exec_pathname = NULL;
11860
3ea46bff 11861 install_breakpoint (0, &c->base, 1);
c906108c 11862}
c5aa993b 11863
9ac4176b 11864void
28010a5d
PA
11865init_ada_exception_breakpoint (struct breakpoint *b,
11866 struct gdbarch *gdbarch,
11867 struct symtab_and_line sal,
11868 char *addr_string,
c0a91b2b 11869 const struct breakpoint_ops *ops,
28010a5d 11870 int tempflag,
349774ef 11871 int enabled,
28010a5d 11872 int from_tty)
f7f9143b 11873{
f7f9143b
JB
11874 if (from_tty)
11875 {
5af949e3
UW
11876 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11877 if (!loc_gdbarch)
11878 loc_gdbarch = gdbarch;
11879
6c95b8df
PA
11880 describe_other_breakpoints (loc_gdbarch,
11881 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11882 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11883 version for exception catchpoints, because two catchpoints
11884 used for different exception names will use the same address.
11885 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11886 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11887 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11888 the user what type of catchpoint it is. The above is good
11889 enough for now, though. */
11890 }
11891
28010a5d 11892 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11893
349774ef 11894 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11895 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11896 b->location = string_to_event_location (&addr_string,
11897 language_def (language_ada));
f7f9143b 11898 b->language = language_ada;
f7f9143b
JB
11899}
11900
c906108c 11901static void
fba45db2 11902catch_command (char *arg, int from_tty)
c906108c 11903{
44feb3ce 11904 error (_("Catch requires an event name."));
c906108c
SS
11905}
11906\f
11907
11908static void
fba45db2 11909tcatch_command (char *arg, int from_tty)
c906108c 11910{
44feb3ce 11911 error (_("Catch requires an event name."));
c906108c
SS
11912}
11913
8a2c437b
TT
11914/* A qsort comparison function that sorts breakpoints in order. */
11915
11916static int
11917compare_breakpoints (const void *a, const void *b)
11918{
9a3c8263 11919 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11920 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11921 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11922 uintptr_t ub = (uintptr_t) *bb;
11923
11924 if ((*ba)->number < (*bb)->number)
11925 return -1;
11926 else if ((*ba)->number > (*bb)->number)
11927 return 1;
11928
11929 /* Now sort by address, in case we see, e..g, two breakpoints with
11930 the number 0. */
11931 if (ua < ub)
11932 return -1;
94b0e70d 11933 return ua > ub ? 1 : 0;
8a2c437b
TT
11934}
11935
80f8a6eb 11936/* Delete breakpoints by address or line. */
c906108c
SS
11937
11938static void
fba45db2 11939clear_command (char *arg, int from_tty)
c906108c 11940{
8a2c437b 11941 struct breakpoint *b, *prev;
d6e956e5
VP
11942 VEC(breakpoint_p) *found = 0;
11943 int ix;
c906108c
SS
11944 int default_match;
11945 struct symtabs_and_lines sals;
11946 struct symtab_and_line sal;
c906108c 11947 int i;
8a2c437b 11948 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11949
11950 if (arg)
11951 {
39cf75f7
DE
11952 sals = decode_line_with_current_source (arg,
11953 (DECODE_LINE_FUNFIRSTLINE
11954 | DECODE_LINE_LIST_MODE));
cf4ded82 11955 make_cleanup (xfree, sals.sals);
c906108c
SS
11956 default_match = 0;
11957 }
11958 else
11959 {
8d749320 11960 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11961 make_cleanup (xfree, sals.sals);
4a64f543 11962 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11963
11964 /* Set sal's line, symtab, pc, and pspace to the values
11965 corresponding to the last call to print_frame_info. If the
11966 codepoint is not valid, this will set all the fields to 0. */
11967 get_last_displayed_sal (&sal);
c906108c 11968 if (sal.symtab == 0)
8a3fe4f8 11969 error (_("No source file specified."));
c906108c
SS
11970
11971 sals.sals[0] = sal;
11972 sals.nelts = 1;
11973
11974 default_match = 1;
11975 }
11976
4a64f543
MS
11977 /* We don't call resolve_sal_pc here. That's not as bad as it
11978 seems, because all existing breakpoints typically have both
11979 file/line and pc set. So, if clear is given file/line, we can
11980 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11981
11982 We only support clearing given the address explicitly
11983 present in breakpoint table. Say, we've set breakpoint
4a64f543 11984 at file:line. There were several PC values for that file:line,
ed0616c6 11985 due to optimization, all in one block.
4a64f543
MS
11986
11987 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11988 PC corresponding to the same file:line, the breakpoint won't
11989 be cleared. We probably can still clear the breakpoint, but
11990 since the other PC value is never presented to user, user
11991 can only find it by guessing, and it does not seem important
11992 to support that. */
11993
4a64f543
MS
11994 /* For each line spec given, delete bps which correspond to it. Do
11995 it in two passes, solely to preserve the current behavior that
11996 from_tty is forced true if we delete more than one
11997 breakpoint. */
c906108c 11998
80f8a6eb 11999 found = NULL;
8a2c437b 12000 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12001 for (i = 0; i < sals.nelts; i++)
12002 {
05cba821
JK
12003 const char *sal_fullname;
12004
c906108c 12005 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12006 If line given (pc == 0), clear all bpts on specified line.
12007 If defaulting, clear all bpts on default line
c906108c 12008 or at default pc.
c5aa993b
JM
12009
12010 defaulting sal.pc != 0 tests to do
12011
12012 0 1 pc
12013 1 1 pc _and_ line
12014 0 0 line
12015 1 0 <can't happen> */
c906108c
SS
12016
12017 sal = sals.sals[i];
05cba821
JK
12018 sal_fullname = (sal.symtab == NULL
12019 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12020
4a64f543 12021 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12022 ALL_BREAKPOINTS (b)
c5aa993b 12023 {
0d381245 12024 int match = 0;
4a64f543 12025 /* Are we going to delete b? */
cc60f2e3 12026 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12027 {
12028 struct bp_location *loc = b->loc;
12029 for (; loc; loc = loc->next)
12030 {
f8eba3c6
TT
12031 /* If the user specified file:line, don't allow a PC
12032 match. This matches historical gdb behavior. */
12033 int pc_match = (!sal.explicit_line
12034 && sal.pc
12035 && (loc->pspace == sal.pspace)
12036 && (loc->address == sal.pc)
12037 && (!section_is_overlay (loc->section)
12038 || loc->section == sal.section));
4aac40c8
TT
12039 int line_match = 0;
12040
12041 if ((default_match || sal.explicit_line)
2f202fde 12042 && loc->symtab != NULL
05cba821 12043 && sal_fullname != NULL
4aac40c8 12044 && sal.pspace == loc->pspace
05cba821
JK
12045 && loc->line_number == sal.line
12046 && filename_cmp (symtab_to_fullname (loc->symtab),
12047 sal_fullname) == 0)
12048 line_match = 1;
4aac40c8 12049
0d381245
VP
12050 if (pc_match || line_match)
12051 {
12052 match = 1;
12053 break;
12054 }
12055 }
12056 }
12057
12058 if (match)
d6e956e5 12059 VEC_safe_push(breakpoint_p, found, b);
c906108c 12060 }
80f8a6eb 12061 }
8a2c437b 12062
80f8a6eb 12063 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12064 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12065 {
12066 if (arg)
8a3fe4f8 12067 error (_("No breakpoint at %s."), arg);
80f8a6eb 12068 else
8a3fe4f8 12069 error (_("No breakpoint at this line."));
80f8a6eb 12070 }
c906108c 12071
8a2c437b
TT
12072 /* Remove duplicates from the vec. */
12073 qsort (VEC_address (breakpoint_p, found),
12074 VEC_length (breakpoint_p, found),
12075 sizeof (breakpoint_p),
12076 compare_breakpoints);
12077 prev = VEC_index (breakpoint_p, found, 0);
12078 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12079 {
12080 if (b == prev)
12081 {
12082 VEC_ordered_remove (breakpoint_p, found, ix);
12083 --ix;
12084 }
12085 }
12086
d6e956e5 12087 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12088 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12089 if (from_tty)
a3f17187 12090 {
d6e956e5 12091 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12092 printf_unfiltered (_("Deleted breakpoint "));
12093 else
12094 printf_unfiltered (_("Deleted breakpoints "));
12095 }
d6e956e5
VP
12096
12097 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12098 {
c5aa993b 12099 if (from_tty)
d6e956e5
VP
12100 printf_unfiltered ("%d ", b->number);
12101 delete_breakpoint (b);
c906108c 12102 }
80f8a6eb
MS
12103 if (from_tty)
12104 putchar_unfiltered ('\n');
8a2c437b
TT
12105
12106 do_cleanups (cleanups);
c906108c
SS
12107}
12108\f
12109/* Delete breakpoint in BS if they are `delete' breakpoints and
12110 all breakpoints that are marked for deletion, whether hit or not.
12111 This is called after any breakpoint is hit, or after errors. */
12112
12113void
fba45db2 12114breakpoint_auto_delete (bpstat bs)
c906108c 12115{
35df4500 12116 struct breakpoint *b, *b_tmp;
c906108c
SS
12117
12118 for (; bs; bs = bs->next)
f431efe5
PA
12119 if (bs->breakpoint_at
12120 && bs->breakpoint_at->disposition == disp_del
c906108c 12121 && bs->stop)
f431efe5 12122 delete_breakpoint (bs->breakpoint_at);
c906108c 12123
35df4500 12124 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12125 {
b5de0fa7 12126 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12127 delete_breakpoint (b);
12128 }
c906108c
SS
12129}
12130
4a64f543
MS
12131/* A comparison function for bp_location AP and BP being interfaced to
12132 qsort. Sort elements primarily by their ADDRESS (no matter what
12133 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12134 secondarily by ordering first permanent elements and
4a64f543 12135 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12136 qsort being an unstable algorithm. */
876fa593
JK
12137
12138static int
494cfb0f 12139bp_location_compare (const void *ap, const void *bp)
876fa593 12140{
9a3c8263
SM
12141 const struct bp_location *a = *(const struct bp_location **) ap;
12142 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12143
12144 if (a->address != b->address)
12145 return (a->address > b->address) - (a->address < b->address);
12146
dea2aa5f
LM
12147 /* Sort locations at the same address by their pspace number, keeping
12148 locations of the same inferior (in a multi-inferior environment)
12149 grouped. */
12150
12151 if (a->pspace->num != b->pspace->num)
12152 return ((a->pspace->num > b->pspace->num)
12153 - (a->pspace->num < b->pspace->num));
12154
876fa593 12155 /* Sort permanent breakpoints first. */
1a853c52
PA
12156 if (a->permanent != b->permanent)
12157 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12158
c56a97f9
JK
12159 /* Make the internal GDB representation stable across GDB runs
12160 where A and B memory inside GDB can differ. Breakpoint locations of
12161 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12162
12163 if (a->owner->number != b->owner->number)
c56a97f9
JK
12164 return ((a->owner->number > b->owner->number)
12165 - (a->owner->number < b->owner->number));
876fa593
JK
12166
12167 return (a > b) - (a < b);
12168}
12169
876fa593 12170/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12171 bp_location_shadow_len_after_address_max according to the current
12172 content of the bp_location array. */
f7545552
TT
12173
12174static void
876fa593 12175bp_location_target_extensions_update (void)
f7545552 12176{
876fa593
JK
12177 struct bp_location *bl, **blp_tmp;
12178
12179 bp_location_placed_address_before_address_max = 0;
12180 bp_location_shadow_len_after_address_max = 0;
12181
12182 ALL_BP_LOCATIONS (bl, blp_tmp)
12183 {
12184 CORE_ADDR start, end, addr;
12185
12186 if (!bp_location_has_shadow (bl))
12187 continue;
12188
12189 start = bl->target_info.placed_address;
12190 end = start + bl->target_info.shadow_len;
12191
12192 gdb_assert (bl->address >= start);
12193 addr = bl->address - start;
12194 if (addr > bp_location_placed_address_before_address_max)
12195 bp_location_placed_address_before_address_max = addr;
12196
12197 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12198
12199 gdb_assert (bl->address < end);
12200 addr = end - bl->address;
12201 if (addr > bp_location_shadow_len_after_address_max)
12202 bp_location_shadow_len_after_address_max = addr;
12203 }
f7545552
TT
12204}
12205
1e4d1764
YQ
12206/* Download tracepoint locations if they haven't been. */
12207
12208static void
12209download_tracepoint_locations (void)
12210{
7ed2c994 12211 struct breakpoint *b;
1e4d1764 12212 struct cleanup *old_chain;
dd2e65cc 12213 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12214
12215 old_chain = save_current_space_and_thread ();
12216
7ed2c994 12217 ALL_TRACEPOINTS (b)
1e4d1764 12218 {
7ed2c994 12219 struct bp_location *bl;
1e4d1764 12220 struct tracepoint *t;
f2a8bc8a 12221 int bp_location_downloaded = 0;
1e4d1764 12222
7ed2c994 12223 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12224 ? !may_insert_fast_tracepoints
12225 : !may_insert_tracepoints))
12226 continue;
12227
dd2e65cc
YQ
12228 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12229 {
12230 if (target_can_download_tracepoint ())
12231 can_download_tracepoint = TRIBOOL_TRUE;
12232 else
12233 can_download_tracepoint = TRIBOOL_FALSE;
12234 }
12235
12236 if (can_download_tracepoint == TRIBOOL_FALSE)
12237 break;
12238
7ed2c994
YQ
12239 for (bl = b->loc; bl; bl = bl->next)
12240 {
12241 /* In tracepoint, locations are _never_ duplicated, so
12242 should_be_inserted is equivalent to
12243 unduplicated_should_be_inserted. */
12244 if (!should_be_inserted (bl) || bl->inserted)
12245 continue;
1e4d1764 12246
7ed2c994 12247 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12248
7ed2c994 12249 target_download_tracepoint (bl);
1e4d1764 12250
7ed2c994 12251 bl->inserted = 1;
f2a8bc8a 12252 bp_location_downloaded = 1;
7ed2c994
YQ
12253 }
12254 t = (struct tracepoint *) b;
12255 t->number_on_target = b->number;
f2a8bc8a
YQ
12256 if (bp_location_downloaded)
12257 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12258 }
12259
12260 do_cleanups (old_chain);
12261}
12262
934709f0
PW
12263/* Swap the insertion/duplication state between two locations. */
12264
12265static void
12266swap_insertion (struct bp_location *left, struct bp_location *right)
12267{
12268 const int left_inserted = left->inserted;
12269 const int left_duplicate = left->duplicate;
b775012e 12270 const int left_needs_update = left->needs_update;
934709f0
PW
12271 const struct bp_target_info left_target_info = left->target_info;
12272
1e4d1764
YQ
12273 /* Locations of tracepoints can never be duplicated. */
12274 if (is_tracepoint (left->owner))
12275 gdb_assert (!left->duplicate);
12276 if (is_tracepoint (right->owner))
12277 gdb_assert (!right->duplicate);
12278
934709f0
PW
12279 left->inserted = right->inserted;
12280 left->duplicate = right->duplicate;
b775012e 12281 left->needs_update = right->needs_update;
934709f0
PW
12282 left->target_info = right->target_info;
12283 right->inserted = left_inserted;
12284 right->duplicate = left_duplicate;
b775012e 12285 right->needs_update = left_needs_update;
934709f0
PW
12286 right->target_info = left_target_info;
12287}
12288
b775012e
LM
12289/* Force the re-insertion of the locations at ADDRESS. This is called
12290 once a new/deleted/modified duplicate location is found and we are evaluating
12291 conditions on the target's side. Such conditions need to be updated on
12292 the target. */
12293
12294static void
12295force_breakpoint_reinsertion (struct bp_location *bl)
12296{
12297 struct bp_location **locp = NULL, **loc2p;
12298 struct bp_location *loc;
12299 CORE_ADDR address = 0;
12300 int pspace_num;
12301
12302 address = bl->address;
12303 pspace_num = bl->pspace->num;
12304
12305 /* This is only meaningful if the target is
12306 evaluating conditions and if the user has
12307 opted for condition evaluation on the target's
12308 side. */
12309 if (gdb_evaluates_breakpoint_condition_p ()
12310 || !target_supports_evaluation_of_breakpoint_conditions ())
12311 return;
12312
12313 /* Flag all breakpoint locations with this address and
12314 the same program space as the location
12315 as "its condition has changed". We need to
12316 update the conditions on the target's side. */
12317 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12318 {
12319 loc = *loc2p;
12320
12321 if (!is_breakpoint (loc->owner)
12322 || pspace_num != loc->pspace->num)
12323 continue;
12324
12325 /* Flag the location appropriately. We use a different state to
12326 let everyone know that we already updated the set of locations
12327 with addr bl->address and program space bl->pspace. This is so
12328 we don't have to keep calling these functions just to mark locations
12329 that have already been marked. */
12330 loc->condition_changed = condition_updated;
12331
12332 /* Free the agent expression bytecode as well. We will compute
12333 it later on. */
12334 if (loc->cond_bytecode)
12335 {
12336 free_agent_expr (loc->cond_bytecode);
12337 loc->cond_bytecode = NULL;
12338 }
12339 }
12340}
44702360
PA
12341/* Called whether new breakpoints are created, or existing breakpoints
12342 deleted, to update the global location list and recompute which
12343 locations are duplicate of which.
b775012e 12344
04086b45
PA
12345 The INSERT_MODE flag determines whether locations may not, may, or
12346 shall be inserted now. See 'enum ugll_insert_mode' for more
12347 info. */
b60e7edf 12348
0d381245 12349static void
44702360 12350update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12351{
74960c60 12352 struct breakpoint *b;
876fa593 12353 struct bp_location **locp, *loc;
f7545552 12354 struct cleanup *cleanups;
b775012e
LM
12355 /* Last breakpoint location address that was marked for update. */
12356 CORE_ADDR last_addr = 0;
12357 /* Last breakpoint location program space that was marked for update. */
12358 int last_pspace_num = -1;
f7545552 12359
2d134ed3
PA
12360 /* Used in the duplicates detection below. When iterating over all
12361 bp_locations, points to the first bp_location of a given address.
12362 Breakpoints and watchpoints of different types are never
12363 duplicates of each other. Keep one pointer for each type of
12364 breakpoint/watchpoint, so we only need to loop over all locations
12365 once. */
12366 struct bp_location *bp_loc_first; /* breakpoint */
12367 struct bp_location *wp_loc_first; /* hardware watchpoint */
12368 struct bp_location *awp_loc_first; /* access watchpoint */
12369 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12370
4a64f543
MS
12371 /* Saved former bp_location array which we compare against the newly
12372 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12373 struct bp_location **old_location, **old_locp;
12374 unsigned old_location_count;
12375
12376 old_location = bp_location;
12377 old_location_count = bp_location_count;
12378 bp_location = NULL;
12379 bp_location_count = 0;
12380 cleanups = make_cleanup (xfree, old_location);
0d381245 12381
74960c60 12382 ALL_BREAKPOINTS (b)
876fa593
JK
12383 for (loc = b->loc; loc; loc = loc->next)
12384 bp_location_count++;
12385
8d749320 12386 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12387 locp = bp_location;
12388 ALL_BREAKPOINTS (b)
12389 for (loc = b->loc; loc; loc = loc->next)
12390 *locp++ = loc;
12391 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12392 bp_location_compare);
876fa593
JK
12393
12394 bp_location_target_extensions_update ();
74960c60 12395
4a64f543
MS
12396 /* Identify bp_location instances that are no longer present in the
12397 new list, and therefore should be freed. Note that it's not
12398 necessary that those locations should be removed from inferior --
12399 if there's another location at the same address (previously
12400 marked as duplicate), we don't need to remove/insert the
12401 location.
876fa593 12402
4a64f543
MS
12403 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12404 and former bp_location array state respectively. */
876fa593
JK
12405
12406 locp = bp_location;
12407 for (old_locp = old_location; old_locp < old_location + old_location_count;
12408 old_locp++)
74960c60 12409 {
876fa593 12410 struct bp_location *old_loc = *old_locp;
c7d46a38 12411 struct bp_location **loc2p;
876fa593 12412
e5dd4106 12413 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12414 not, we have to free it. */
c7d46a38 12415 int found_object = 0;
20874c92
VP
12416 /* Tells if the location should remain inserted in the target. */
12417 int keep_in_target = 0;
12418 int removed = 0;
876fa593 12419
4a64f543
MS
12420 /* Skip LOCP entries which will definitely never be needed.
12421 Stop either at or being the one matching OLD_LOC. */
876fa593 12422 while (locp < bp_location + bp_location_count
c7d46a38 12423 && (*locp)->address < old_loc->address)
876fa593 12424 locp++;
c7d46a38
PA
12425
12426 for (loc2p = locp;
12427 (loc2p < bp_location + bp_location_count
12428 && (*loc2p)->address == old_loc->address);
12429 loc2p++)
12430 {
b775012e
LM
12431 /* Check if this is a new/duplicated location or a duplicated
12432 location that had its condition modified. If so, we want to send
12433 its condition to the target if evaluation of conditions is taking
12434 place there. */
12435 if ((*loc2p)->condition_changed == condition_modified
12436 && (last_addr != old_loc->address
12437 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12438 {
b775012e
LM
12439 force_breakpoint_reinsertion (*loc2p);
12440 last_pspace_num = old_loc->pspace->num;
c7d46a38 12441 }
b775012e
LM
12442
12443 if (*loc2p == old_loc)
12444 found_object = 1;
c7d46a38 12445 }
74960c60 12446
b775012e
LM
12447 /* We have already handled this address, update it so that we don't
12448 have to go through updates again. */
12449 last_addr = old_loc->address;
12450
12451 /* Target-side condition evaluation: Handle deleted locations. */
12452 if (!found_object)
12453 force_breakpoint_reinsertion (old_loc);
12454
4a64f543
MS
12455 /* If this location is no longer present, and inserted, look if
12456 there's maybe a new location at the same address. If so,
12457 mark that one inserted, and don't remove this one. This is
12458 needed so that we don't have a time window where a breakpoint
12459 at certain location is not inserted. */
74960c60 12460
876fa593 12461 if (old_loc->inserted)
0d381245 12462 {
4a64f543
MS
12463 /* If the location is inserted now, we might have to remove
12464 it. */
74960c60 12465
876fa593 12466 if (found_object && should_be_inserted (old_loc))
74960c60 12467 {
4a64f543
MS
12468 /* The location is still present in the location list,
12469 and still should be inserted. Don't do anything. */
20874c92 12470 keep_in_target = 1;
74960c60
VP
12471 }
12472 else
12473 {
b775012e
LM
12474 /* This location still exists, but it won't be kept in the
12475 target since it may have been disabled. We proceed to
12476 remove its target-side condition. */
12477
4a64f543
MS
12478 /* The location is either no longer present, or got
12479 disabled. See if there's another location at the
12480 same address, in which case we don't need to remove
12481 this one from the target. */
876fa593 12482
2bdf28a0 12483 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12484 if (breakpoint_address_is_meaningful (old_loc->owner))
12485 {
876fa593 12486 for (loc2p = locp;
c7d46a38
PA
12487 (loc2p < bp_location + bp_location_count
12488 && (*loc2p)->address == old_loc->address);
876fa593
JK
12489 loc2p++)
12490 {
12491 struct bp_location *loc2 = *loc2p;
12492
2d134ed3 12493 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12494 {
85d721b8
PA
12495 /* Read watchpoint locations are switched to
12496 access watchpoints, if the former are not
12497 supported, but the latter are. */
12498 if (is_hardware_watchpoint (old_loc->owner))
12499 {
12500 gdb_assert (is_hardware_watchpoint (loc2->owner));
12501 loc2->watchpoint_type = old_loc->watchpoint_type;
12502 }
12503
934709f0
PW
12504 /* loc2 is a duplicated location. We need to check
12505 if it should be inserted in case it will be
12506 unduplicated. */
12507 if (loc2 != old_loc
12508 && unduplicated_should_be_inserted (loc2))
c7d46a38 12509 {
934709f0 12510 swap_insertion (old_loc, loc2);
c7d46a38
PA
12511 keep_in_target = 1;
12512 break;
12513 }
876fa593
JK
12514 }
12515 }
12516 }
74960c60
VP
12517 }
12518
20874c92
VP
12519 if (!keep_in_target)
12520 {
876fa593 12521 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12522 {
4a64f543
MS
12523 /* This is just about all we can do. We could keep
12524 this location on the global list, and try to
12525 remove it next time, but there's no particular
12526 reason why we will succeed next time.
20874c92 12527
4a64f543
MS
12528 Note that at this point, old_loc->owner is still
12529 valid, as delete_breakpoint frees the breakpoint
12530 only after calling us. */
3e43a32a
MS
12531 printf_filtered (_("warning: Error removing "
12532 "breakpoint %d\n"),
876fa593 12533 old_loc->owner->number);
20874c92
VP
12534 }
12535 removed = 1;
12536 }
0d381245 12537 }
74960c60
VP
12538
12539 if (!found_object)
1c5cfe86 12540 {
fbea99ea 12541 if (removed && target_is_non_stop_p ()
1cf4d951 12542 && need_moribund_for_location_type (old_loc))
20874c92 12543 {
db82e815
PA
12544 /* This location was removed from the target. In
12545 non-stop mode, a race condition is possible where
12546 we've removed a breakpoint, but stop events for that
12547 breakpoint are already queued and will arrive later.
12548 We apply an heuristic to be able to distinguish such
12549 SIGTRAPs from other random SIGTRAPs: we keep this
12550 breakpoint location for a bit, and will retire it
12551 after we see some number of events. The theory here
12552 is that reporting of events should, "on the average",
12553 be fair, so after a while we'll see events from all
12554 threads that have anything of interest, and no longer
12555 need to keep this breakpoint location around. We
12556 don't hold locations forever so to reduce chances of
12557 mistaking a non-breakpoint SIGTRAP for a breakpoint
12558 SIGTRAP.
12559
12560 The heuristic failing can be disastrous on
12561 decr_pc_after_break targets.
12562
12563 On decr_pc_after_break targets, like e.g., x86-linux,
12564 if we fail to recognize a late breakpoint SIGTRAP,
12565 because events_till_retirement has reached 0 too
12566 soon, we'll fail to do the PC adjustment, and report
12567 a random SIGTRAP to the user. When the user resumes
12568 the inferior, it will most likely immediately crash
2dec564e 12569 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12570 corrupted, because of being resumed e.g., in the
12571 middle of a multi-byte instruction, or skipped a
12572 one-byte instruction. This was actually seen happen
12573 on native x86-linux, and should be less rare on
12574 targets that do not support new thread events, like
12575 remote, due to the heuristic depending on
12576 thread_count.
12577
12578 Mistaking a random SIGTRAP for a breakpoint trap
12579 causes similar symptoms (PC adjustment applied when
12580 it shouldn't), but then again, playing with SIGTRAPs
12581 behind the debugger's back is asking for trouble.
12582
12583 Since hardware watchpoint traps are always
12584 distinguishable from other traps, so we don't need to
12585 apply keep hardware watchpoint moribund locations
12586 around. We simply always ignore hardware watchpoint
12587 traps we can no longer explain. */
12588
876fa593
JK
12589 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12590 old_loc->owner = NULL;
20874c92 12591
876fa593 12592 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12593 }
12594 else
f431efe5
PA
12595 {
12596 old_loc->owner = NULL;
12597 decref_bp_location (&old_loc);
12598 }
20874c92 12599 }
74960c60 12600 }
1c5cfe86 12601
348d480f
PA
12602 /* Rescan breakpoints at the same address and section, marking the
12603 first one as "first" and any others as "duplicates". This is so
12604 that the bpt instruction is only inserted once. If we have a
12605 permanent breakpoint at the same place as BPT, make that one the
12606 official one, and the rest as duplicates. Permanent breakpoints
12607 are sorted first for the same address.
12608
12609 Do the same for hardware watchpoints, but also considering the
12610 watchpoint's type (regular/access/read) and length. */
12611
12612 bp_loc_first = NULL;
12613 wp_loc_first = NULL;
12614 awp_loc_first = NULL;
12615 rwp_loc_first = NULL;
12616 ALL_BP_LOCATIONS (loc, locp)
12617 {
12618 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12619 non-NULL. */
348d480f 12620 struct bp_location **loc_first_p;
d3fbdd86 12621 b = loc->owner;
348d480f 12622
6f380991 12623 if (!unduplicated_should_be_inserted (loc)
348d480f 12624 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12625 /* Don't detect duplicate for tracepoint locations because they are
12626 never duplicated. See the comments in field `duplicate' of
12627 `struct bp_location'. */
348d480f 12628 || is_tracepoint (b))
b775012e
LM
12629 {
12630 /* Clear the condition modification flag. */
12631 loc->condition_changed = condition_unchanged;
12632 continue;
12633 }
348d480f 12634
348d480f
PA
12635 if (b->type == bp_hardware_watchpoint)
12636 loc_first_p = &wp_loc_first;
12637 else if (b->type == bp_read_watchpoint)
12638 loc_first_p = &rwp_loc_first;
12639 else if (b->type == bp_access_watchpoint)
12640 loc_first_p = &awp_loc_first;
12641 else
12642 loc_first_p = &bp_loc_first;
12643
12644 if (*loc_first_p == NULL
12645 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12646 || !breakpoint_locations_match (loc, *loc_first_p))
12647 {
12648 *loc_first_p = loc;
12649 loc->duplicate = 0;
b775012e
LM
12650
12651 if (is_breakpoint (loc->owner) && loc->condition_changed)
12652 {
12653 loc->needs_update = 1;
12654 /* Clear the condition modification flag. */
12655 loc->condition_changed = condition_unchanged;
12656 }
348d480f
PA
12657 continue;
12658 }
12659
934709f0
PW
12660
12661 /* This and the above ensure the invariant that the first location
12662 is not duplicated, and is the inserted one.
12663 All following are marked as duplicated, and are not inserted. */
12664 if (loc->inserted)
12665 swap_insertion (loc, *loc_first_p);
348d480f
PA
12666 loc->duplicate = 1;
12667
b775012e
LM
12668 /* Clear the condition modification flag. */
12669 loc->condition_changed = condition_unchanged;
348d480f
PA
12670 }
12671
a25a5a45 12672 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12673 {
04086b45 12674 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12675 insert_breakpoint_locations ();
12676 else
12677 {
44702360
PA
12678 /* Even though the caller told us to not insert new
12679 locations, we may still need to update conditions on the
12680 target's side of breakpoints that were already inserted
12681 if the target is evaluating breakpoint conditions. We
b775012e
LM
12682 only update conditions for locations that are marked
12683 "needs_update". */
12684 update_inserted_breakpoint_locations ();
12685 }
12686 }
348d480f 12687
04086b45 12688 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12689 download_tracepoint_locations ();
12690
348d480f
PA
12691 do_cleanups (cleanups);
12692}
12693
12694void
12695breakpoint_retire_moribund (void)
12696{
12697 struct bp_location *loc;
12698 int ix;
12699
12700 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12701 if (--(loc->events_till_retirement) == 0)
12702 {
12703 decref_bp_location (&loc);
12704 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12705 --ix;
12706 }
12707}
12708
12709static void
44702360 12710update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12711{
348d480f 12712
492d29ea
PA
12713 TRY
12714 {
12715 update_global_location_list (insert_mode);
12716 }
12717 CATCH (e, RETURN_MASK_ERROR)
12718 {
12719 }
12720 END_CATCH
348d480f
PA
12721}
12722
12723/* Clear BKP from a BPS. */
12724
12725static void
12726bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12727{
12728 bpstat bs;
12729
12730 for (bs = bps; bs; bs = bs->next)
12731 if (bs->breakpoint_at == bpt)
12732 {
12733 bs->breakpoint_at = NULL;
12734 bs->old_val = NULL;
12735 /* bs->commands will be freed later. */
12736 }
12737}
12738
12739/* Callback for iterate_over_threads. */
12740static int
12741bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12742{
9a3c8263 12743 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12744
12745 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12746 return 0;
12747}
12748
12749/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12750 callbacks. */
12751
12752static void
12753say_where (struct breakpoint *b)
12754{
12755 struct value_print_options opts;
12756
12757 get_user_print_options (&opts);
12758
12759 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12760 single string. */
12761 if (b->loc == NULL)
12762 {
f00aae0f
KS
12763 /* For pending locations, the output differs slightly based
12764 on b->extra_string. If this is non-NULL, it contains either
12765 a condition or dprintf arguments. */
12766 if (b->extra_string == NULL)
12767 {
12768 printf_filtered (_(" (%s) pending."),
12769 event_location_to_string (b->location));
12770 }
12771 else if (b->type == bp_dprintf)
12772 {
12773 printf_filtered (_(" (%s,%s) pending."),
12774 event_location_to_string (b->location),
12775 b->extra_string);
12776 }
12777 else
12778 {
12779 printf_filtered (_(" (%s %s) pending."),
12780 event_location_to_string (b->location),
12781 b->extra_string);
12782 }
348d480f
PA
12783 }
12784 else
12785 {
2f202fde 12786 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12787 {
12788 printf_filtered (" at ");
12789 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12790 gdb_stdout);
12791 }
2f202fde 12792 if (b->loc->symtab != NULL)
f8eba3c6
TT
12793 {
12794 /* If there is a single location, we can print the location
12795 more nicely. */
12796 if (b->loc->next == NULL)
12797 printf_filtered (": file %s, line %d.",
05cba821
JK
12798 symtab_to_filename_for_display (b->loc->symtab),
12799 b->loc->line_number);
f8eba3c6
TT
12800 else
12801 /* This is not ideal, but each location may have a
12802 different file name, and this at least reflects the
12803 real situation somewhat. */
f00aae0f
KS
12804 printf_filtered (": %s.",
12805 event_location_to_string (b->location));
f8eba3c6 12806 }
348d480f
PA
12807
12808 if (b->loc->next)
12809 {
12810 struct bp_location *loc = b->loc;
12811 int n = 0;
12812 for (; loc; loc = loc->next)
12813 ++n;
12814 printf_filtered (" (%d locations)", n);
12815 }
12816 }
12817}
12818
348d480f
PA
12819/* Default bp_location_ops methods. */
12820
12821static void
12822bp_location_dtor (struct bp_location *self)
12823{
12824 xfree (self->cond);
b775012e
LM
12825 if (self->cond_bytecode)
12826 free_agent_expr (self->cond_bytecode);
348d480f 12827 xfree (self->function_name);
8b4f3082
PA
12828
12829 VEC_free (agent_expr_p, self->target_info.conditions);
12830 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12831}
12832
12833static const struct bp_location_ops bp_location_ops =
12834{
12835 bp_location_dtor
12836};
12837
2060206e
PA
12838/* Default breakpoint_ops methods all breakpoint_ops ultimately
12839 inherit from. */
348d480f 12840
2060206e
PA
12841static void
12842base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12843{
12844 decref_counted_command_line (&self->commands);
12845 xfree (self->cond_string);
fb81d016 12846 xfree (self->extra_string);
f8eba3c6 12847 xfree (self->filter);
f00aae0f
KS
12848 delete_event_location (self->location);
12849 delete_event_location (self->location_range_end);
348d480f
PA
12850}
12851
2060206e
PA
12852static struct bp_location *
12853base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12854{
12855 struct bp_location *loc;
12856
12857 loc = XNEW (struct bp_location);
12858 init_bp_location (loc, &bp_location_ops, self);
12859 return loc;
12860}
12861
2060206e
PA
12862static void
12863base_breakpoint_re_set (struct breakpoint *b)
12864{
12865 /* Nothing to re-set. */
12866}
12867
12868#define internal_error_pure_virtual_called() \
12869 gdb_assert_not_reached ("pure virtual function called")
12870
12871static int
12872base_breakpoint_insert_location (struct bp_location *bl)
12873{
12874 internal_error_pure_virtual_called ();
12875}
12876
12877static int
12878base_breakpoint_remove_location (struct bp_location *bl)
12879{
12880 internal_error_pure_virtual_called ();
12881}
12882
12883static int
12884base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12885 struct address_space *aspace,
09ac7c10
TT
12886 CORE_ADDR bp_addr,
12887 const struct target_waitstatus *ws)
2060206e
PA
12888{
12889 internal_error_pure_virtual_called ();
12890}
12891
12892static void
12893base_breakpoint_check_status (bpstat bs)
12894{
12895 /* Always stop. */
12896}
12897
12898/* A "works_in_software_mode" breakpoint_ops method that just internal
12899 errors. */
12900
12901static int
12902base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12903{
12904 internal_error_pure_virtual_called ();
12905}
12906
12907/* A "resources_needed" breakpoint_ops method that just internal
12908 errors. */
12909
12910static int
12911base_breakpoint_resources_needed (const struct bp_location *bl)
12912{
12913 internal_error_pure_virtual_called ();
12914}
12915
12916static enum print_stop_action
12917base_breakpoint_print_it (bpstat bs)
12918{
12919 internal_error_pure_virtual_called ();
12920}
12921
12922static void
12923base_breakpoint_print_one_detail (const struct breakpoint *self,
12924 struct ui_out *uiout)
12925{
12926 /* nothing */
12927}
12928
12929static void
12930base_breakpoint_print_mention (struct breakpoint *b)
12931{
12932 internal_error_pure_virtual_called ();
12933}
12934
12935static void
12936base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12937{
12938 internal_error_pure_virtual_called ();
12939}
12940
983af33b 12941static void
f00aae0f
KS
12942base_breakpoint_create_sals_from_location
12943 (const struct event_location *location,
12944 struct linespec_result *canonical,
12945 enum bptype type_wanted)
983af33b
SDJ
12946{
12947 internal_error_pure_virtual_called ();
12948}
12949
12950static void
12951base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12952 struct linespec_result *c,
983af33b 12953 char *cond_string,
e7e0cddf 12954 char *extra_string,
983af33b
SDJ
12955 enum bptype type_wanted,
12956 enum bpdisp disposition,
12957 int thread,
12958 int task, int ignore_count,
12959 const struct breakpoint_ops *o,
12960 int from_tty, int enabled,
44f238bb 12961 int internal, unsigned flags)
983af33b
SDJ
12962{
12963 internal_error_pure_virtual_called ();
12964}
12965
12966static void
f00aae0f
KS
12967base_breakpoint_decode_location (struct breakpoint *b,
12968 const struct event_location *location,
983af33b
SDJ
12969 struct symtabs_and_lines *sals)
12970{
12971 internal_error_pure_virtual_called ();
12972}
12973
ab04a2af
TT
12974/* The default 'explains_signal' method. */
12975
47591c29 12976static int
427cd150 12977base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12978{
47591c29 12979 return 1;
ab04a2af
TT
12980}
12981
9d6e6e84
HZ
12982/* The default "after_condition_true" method. */
12983
12984static void
12985base_breakpoint_after_condition_true (struct bpstats *bs)
12986{
12987 /* Nothing to do. */
12988}
12989
ab04a2af 12990struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12991{
12992 base_breakpoint_dtor,
12993 base_breakpoint_allocate_location,
12994 base_breakpoint_re_set,
12995 base_breakpoint_insert_location,
12996 base_breakpoint_remove_location,
12997 base_breakpoint_breakpoint_hit,
12998 base_breakpoint_check_status,
12999 base_breakpoint_resources_needed,
13000 base_breakpoint_works_in_software_mode,
13001 base_breakpoint_print_it,
13002 NULL,
13003 base_breakpoint_print_one_detail,
13004 base_breakpoint_print_mention,
983af33b 13005 base_breakpoint_print_recreate,
5f700d83 13006 base_breakpoint_create_sals_from_location,
983af33b 13007 base_breakpoint_create_breakpoints_sal,
5f700d83 13008 base_breakpoint_decode_location,
9d6e6e84
HZ
13009 base_breakpoint_explains_signal,
13010 base_breakpoint_after_condition_true,
2060206e
PA
13011};
13012
13013/* Default breakpoint_ops methods. */
13014
13015static void
348d480f
PA
13016bkpt_re_set (struct breakpoint *b)
13017{
06edf0c0 13018 /* FIXME: is this still reachable? */
f00aae0f 13019 if (event_location_empty_p (b->location))
06edf0c0 13020 {
f00aae0f 13021 /* Anything without a location can't be re-set. */
348d480f 13022 delete_breakpoint (b);
06edf0c0 13023 return;
348d480f 13024 }
06edf0c0
PA
13025
13026 breakpoint_re_set_default (b);
348d480f
PA
13027}
13028
2060206e 13029static int
348d480f
PA
13030bkpt_insert_location (struct bp_location *bl)
13031{
13032 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13033 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13034 else
7c16b83e 13035 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13036}
13037
2060206e 13038static int
348d480f
PA
13039bkpt_remove_location (struct bp_location *bl)
13040{
13041 if (bl->loc_type == bp_loc_hardware_breakpoint)
13042 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13043 else
7c16b83e 13044 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13045}
13046
2060206e 13047static int
348d480f 13048bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13049 struct address_space *aspace, CORE_ADDR bp_addr,
13050 const struct target_waitstatus *ws)
348d480f 13051{
09ac7c10 13052 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13053 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13054 return 0;
13055
348d480f
PA
13056 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13057 aspace, bp_addr))
13058 return 0;
13059
13060 if (overlay_debugging /* unmapped overlay section */
13061 && section_is_overlay (bl->section)
13062 && !section_is_mapped (bl->section))
13063 return 0;
13064
13065 return 1;
13066}
13067
cd1608cc
PA
13068static int
13069dprintf_breakpoint_hit (const struct bp_location *bl,
13070 struct address_space *aspace, CORE_ADDR bp_addr,
13071 const struct target_waitstatus *ws)
13072{
13073 if (dprintf_style == dprintf_style_agent
13074 && target_can_run_breakpoint_commands ())
13075 {
13076 /* An agent-style dprintf never causes a stop. If we see a trap
13077 for this address it must be for a breakpoint that happens to
13078 be set at the same address. */
13079 return 0;
13080 }
13081
13082 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13083}
13084
2060206e 13085static int
348d480f
PA
13086bkpt_resources_needed (const struct bp_location *bl)
13087{
13088 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13089
13090 return 1;
13091}
13092
2060206e 13093static enum print_stop_action
348d480f
PA
13094bkpt_print_it (bpstat bs)
13095{
348d480f
PA
13096 struct breakpoint *b;
13097 const struct bp_location *bl;
001c8c33 13098 int bp_temp;
79a45e25 13099 struct ui_out *uiout = current_uiout;
348d480f
PA
13100
13101 gdb_assert (bs->bp_location_at != NULL);
13102
13103 bl = bs->bp_location_at;
13104 b = bs->breakpoint_at;
13105
001c8c33
PA
13106 bp_temp = b->disposition == disp_del;
13107 if (bl->address != bl->requested_address)
13108 breakpoint_adjustment_warning (bl->requested_address,
13109 bl->address,
13110 b->number, 1);
13111 annotate_breakpoint (b->number);
13112 if (bp_temp)
13113 ui_out_text (uiout, "\nTemporary breakpoint ");
13114 else
13115 ui_out_text (uiout, "\nBreakpoint ");
13116 if (ui_out_is_mi_like_p (uiout))
348d480f 13117 {
001c8c33
PA
13118 ui_out_field_string (uiout, "reason",
13119 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13120 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13121 }
001c8c33
PA
13122 ui_out_field_int (uiout, "bkptno", b->number);
13123 ui_out_text (uiout, ", ");
06edf0c0 13124
001c8c33 13125 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13126}
13127
2060206e 13128static void
06edf0c0
PA
13129bkpt_print_mention (struct breakpoint *b)
13130{
79a45e25 13131 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13132 return;
13133
13134 switch (b->type)
13135 {
13136 case bp_breakpoint:
13137 case bp_gnu_ifunc_resolver:
13138 if (b->disposition == disp_del)
13139 printf_filtered (_("Temporary breakpoint"));
13140 else
13141 printf_filtered (_("Breakpoint"));
13142 printf_filtered (_(" %d"), b->number);
13143 if (b->type == bp_gnu_ifunc_resolver)
13144 printf_filtered (_(" at gnu-indirect-function resolver"));
13145 break;
13146 case bp_hardware_breakpoint:
13147 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13148 break;
e7e0cddf
SS
13149 case bp_dprintf:
13150 printf_filtered (_("Dprintf %d"), b->number);
13151 break;
06edf0c0
PA
13152 }
13153
13154 say_where (b);
13155}
13156
2060206e 13157static void
06edf0c0
PA
13158bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13159{
13160 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13161 fprintf_unfiltered (fp, "tbreak");
13162 else if (tp->type == bp_breakpoint)
13163 fprintf_unfiltered (fp, "break");
13164 else if (tp->type == bp_hardware_breakpoint
13165 && tp->disposition == disp_del)
13166 fprintf_unfiltered (fp, "thbreak");
13167 else if (tp->type == bp_hardware_breakpoint)
13168 fprintf_unfiltered (fp, "hbreak");
13169 else
13170 internal_error (__FILE__, __LINE__,
13171 _("unhandled breakpoint type %d"), (int) tp->type);
13172
f00aae0f
KS
13173 fprintf_unfiltered (fp, " %s",
13174 event_location_to_string (tp->location));
13175
13176 /* Print out extra_string if this breakpoint is pending. It might
13177 contain, for example, conditions that were set by the user. */
13178 if (tp->loc == NULL && tp->extra_string != NULL)
13179 fprintf_unfiltered (fp, " %s", tp->extra_string);
13180
dd11a36c 13181 print_recreate_thread (tp, fp);
06edf0c0
PA
13182}
13183
983af33b 13184static void
f00aae0f
KS
13185bkpt_create_sals_from_location (const struct event_location *location,
13186 struct linespec_result *canonical,
13187 enum bptype type_wanted)
983af33b 13188{
f00aae0f 13189 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13190}
13191
13192static void
13193bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13194 struct linespec_result *canonical,
983af33b 13195 char *cond_string,
e7e0cddf 13196 char *extra_string,
983af33b
SDJ
13197 enum bptype type_wanted,
13198 enum bpdisp disposition,
13199 int thread,
13200 int task, int ignore_count,
13201 const struct breakpoint_ops *ops,
13202 int from_tty, int enabled,
44f238bb 13203 int internal, unsigned flags)
983af33b 13204{
023fa29b 13205 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13206 cond_string, extra_string,
13207 type_wanted,
983af33b
SDJ
13208 disposition, thread, task,
13209 ignore_count, ops, from_tty,
44f238bb 13210 enabled, internal, flags);
983af33b
SDJ
13211}
13212
13213static void
f00aae0f
KS
13214bkpt_decode_location (struct breakpoint *b,
13215 const struct event_location *location,
983af33b
SDJ
13216 struct symtabs_and_lines *sals)
13217{
f00aae0f 13218 decode_location_default (b, location, sals);
983af33b
SDJ
13219}
13220
06edf0c0
PA
13221/* Virtual table for internal breakpoints. */
13222
13223static void
13224internal_bkpt_re_set (struct breakpoint *b)
13225{
13226 switch (b->type)
13227 {
13228 /* Delete overlay event and longjmp master breakpoints; they
13229 will be reset later by breakpoint_re_set. */
13230 case bp_overlay_event:
13231 case bp_longjmp_master:
13232 case bp_std_terminate_master:
13233 case bp_exception_master:
13234 delete_breakpoint (b);
13235 break;
13236
13237 /* This breakpoint is special, it's set up when the inferior
13238 starts and we really don't want to touch it. */
13239 case bp_shlib_event:
13240
13241 /* Like bp_shlib_event, this breakpoint type is special. Once
13242 it is set up, we do not want to touch it. */
13243 case bp_thread_event:
13244 break;
13245 }
13246}
13247
13248static void
13249internal_bkpt_check_status (bpstat bs)
13250{
a9b3a50f
PA
13251 if (bs->breakpoint_at->type == bp_shlib_event)
13252 {
13253 /* If requested, stop when the dynamic linker notifies GDB of
13254 events. This allows the user to get control and place
13255 breakpoints in initializer routines for dynamically loaded
13256 objects (among other things). */
13257 bs->stop = stop_on_solib_events;
13258 bs->print = stop_on_solib_events;
13259 }
13260 else
13261 bs->stop = 0;
06edf0c0
PA
13262}
13263
13264static enum print_stop_action
13265internal_bkpt_print_it (bpstat bs)
13266{
06edf0c0 13267 struct breakpoint *b;
06edf0c0 13268
06edf0c0
PA
13269 b = bs->breakpoint_at;
13270
06edf0c0
PA
13271 switch (b->type)
13272 {
348d480f
PA
13273 case bp_shlib_event:
13274 /* Did we stop because the user set the stop_on_solib_events
13275 variable? (If so, we report this as a generic, "Stopped due
13276 to shlib event" message.) */
edcc5120 13277 print_solib_event (0);
348d480f
PA
13278 break;
13279
13280 case bp_thread_event:
13281 /* Not sure how we will get here.
13282 GDB should not stop for these breakpoints. */
13283 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13284 break;
13285
13286 case bp_overlay_event:
13287 /* By analogy with the thread event, GDB should not stop for these. */
13288 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13289 break;
13290
13291 case bp_longjmp_master:
13292 /* These should never be enabled. */
13293 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13294 break;
13295
13296 case bp_std_terminate_master:
13297 /* These should never be enabled. */
13298 printf_filtered (_("std::terminate Master Breakpoint: "
13299 "gdb should not stop!\n"));
348d480f
PA
13300 break;
13301
13302 case bp_exception_master:
13303 /* These should never be enabled. */
13304 printf_filtered (_("Exception Master Breakpoint: "
13305 "gdb should not stop!\n"));
06edf0c0
PA
13306 break;
13307 }
13308
001c8c33 13309 return PRINT_NOTHING;
06edf0c0
PA
13310}
13311
13312static void
13313internal_bkpt_print_mention (struct breakpoint *b)
13314{
13315 /* Nothing to mention. These breakpoints are internal. */
13316}
13317
06edf0c0
PA
13318/* Virtual table for momentary breakpoints */
13319
13320static void
13321momentary_bkpt_re_set (struct breakpoint *b)
13322{
13323 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13324 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13325 Otherwise these should have been blown away via the cleanup chain
13326 or by breakpoint_init_inferior when we rerun the executable. */
13327}
13328
13329static void
13330momentary_bkpt_check_status (bpstat bs)
13331{
13332 /* Nothing. The point of these breakpoints is causing a stop. */
13333}
13334
13335static enum print_stop_action
13336momentary_bkpt_print_it (bpstat bs)
13337{
001c8c33 13338 return PRINT_UNKNOWN;
348d480f
PA
13339}
13340
06edf0c0
PA
13341static void
13342momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13343{
06edf0c0 13344 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13345}
13346
e2e4d78b
JK
13347/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13348
13349 It gets cleared already on the removal of the first one of such placed
13350 breakpoints. This is OK as they get all removed altogether. */
13351
13352static void
13353longjmp_bkpt_dtor (struct breakpoint *self)
13354{
13355 struct thread_info *tp = find_thread_id (self->thread);
13356
13357 if (tp)
13358 tp->initiating_frame = null_frame_id;
13359
13360 momentary_breakpoint_ops.dtor (self);
13361}
13362
55aa24fb
SDJ
13363/* Specific methods for probe breakpoints. */
13364
13365static int
13366bkpt_probe_insert_location (struct bp_location *bl)
13367{
13368 int v = bkpt_insert_location (bl);
13369
13370 if (v == 0)
13371 {
13372 /* The insertion was successful, now let's set the probe's semaphore
13373 if needed. */
0ea5cda8
SDJ
13374 if (bl->probe.probe->pops->set_semaphore != NULL)
13375 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13376 bl->probe.objfile,
13377 bl->gdbarch);
55aa24fb
SDJ
13378 }
13379
13380 return v;
13381}
13382
13383static int
13384bkpt_probe_remove_location (struct bp_location *bl)
13385{
13386 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13387 if (bl->probe.probe->pops->clear_semaphore != NULL)
13388 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13389 bl->probe.objfile,
13390 bl->gdbarch);
55aa24fb
SDJ
13391
13392 return bkpt_remove_location (bl);
13393}
13394
13395static void
f00aae0f 13396bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13397 struct linespec_result *canonical,
f00aae0f 13398 enum bptype type_wanted)
55aa24fb
SDJ
13399{
13400 struct linespec_sals lsal;
13401
f00aae0f
KS
13402 lsal.sals = parse_probes (location, canonical);
13403 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13404 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13405}
13406
13407static void
f00aae0f
KS
13408bkpt_probe_decode_location (struct breakpoint *b,
13409 const struct event_location *location,
55aa24fb
SDJ
13410 struct symtabs_and_lines *sals)
13411{
f00aae0f 13412 *sals = parse_probes (location, NULL);
55aa24fb
SDJ
13413 if (!sals->sals)
13414 error (_("probe not found"));
13415}
13416
348d480f 13417/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13418
348d480f
PA
13419static void
13420tracepoint_re_set (struct breakpoint *b)
13421{
13422 breakpoint_re_set_default (b);
13423}
876fa593 13424
348d480f
PA
13425static int
13426tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13427 struct address_space *aspace, CORE_ADDR bp_addr,
13428 const struct target_waitstatus *ws)
348d480f
PA
13429{
13430 /* By definition, the inferior does not report stops at
13431 tracepoints. */
13432 return 0;
74960c60
VP
13433}
13434
13435static void
348d480f
PA
13436tracepoint_print_one_detail (const struct breakpoint *self,
13437 struct ui_out *uiout)
74960c60 13438{
d9b3f62e
PA
13439 struct tracepoint *tp = (struct tracepoint *) self;
13440 if (tp->static_trace_marker_id)
348d480f
PA
13441 {
13442 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13443
348d480f
PA
13444 ui_out_text (uiout, "\tmarker id is ");
13445 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13446 tp->static_trace_marker_id);
348d480f
PA
13447 ui_out_text (uiout, "\n");
13448 }
0d381245
VP
13449}
13450
a474d7c2 13451static void
348d480f 13452tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13453{
79a45e25 13454 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13455 return;
cc59ec59 13456
348d480f
PA
13457 switch (b->type)
13458 {
13459 case bp_tracepoint:
13460 printf_filtered (_("Tracepoint"));
13461 printf_filtered (_(" %d"), b->number);
13462 break;
13463 case bp_fast_tracepoint:
13464 printf_filtered (_("Fast tracepoint"));
13465 printf_filtered (_(" %d"), b->number);
13466 break;
13467 case bp_static_tracepoint:
13468 printf_filtered (_("Static tracepoint"));
13469 printf_filtered (_(" %d"), b->number);
13470 break;
13471 default:
13472 internal_error (__FILE__, __LINE__,
13473 _("unhandled tracepoint type %d"), (int) b->type);
13474 }
13475
13476 say_where (b);
a474d7c2
PA
13477}
13478
348d480f 13479static void
d9b3f62e 13480tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13481{
d9b3f62e
PA
13482 struct tracepoint *tp = (struct tracepoint *) self;
13483
13484 if (self->type == bp_fast_tracepoint)
348d480f 13485 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13486 if (self->type == bp_static_tracepoint)
348d480f 13487 fprintf_unfiltered (fp, "strace");
d9b3f62e 13488 else if (self->type == bp_tracepoint)
348d480f
PA
13489 fprintf_unfiltered (fp, "trace");
13490 else
13491 internal_error (__FILE__, __LINE__,
d9b3f62e 13492 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13493
f00aae0f
KS
13494 fprintf_unfiltered (fp, " %s",
13495 event_location_to_string (self->location));
d9b3f62e
PA
13496 print_recreate_thread (self, fp);
13497
13498 if (tp->pass_count)
13499 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13500}
13501
983af33b 13502static void
f00aae0f
KS
13503tracepoint_create_sals_from_location (const struct event_location *location,
13504 struct linespec_result *canonical,
13505 enum bptype type_wanted)
983af33b 13506{
f00aae0f 13507 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13508}
13509
13510static void
13511tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13512 struct linespec_result *canonical,
983af33b 13513 char *cond_string,
e7e0cddf 13514 char *extra_string,
983af33b
SDJ
13515 enum bptype type_wanted,
13516 enum bpdisp disposition,
13517 int thread,
13518 int task, int ignore_count,
13519 const struct breakpoint_ops *ops,
13520 int from_tty, int enabled,
44f238bb 13521 int internal, unsigned flags)
983af33b 13522{
023fa29b 13523 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13524 cond_string, extra_string,
13525 type_wanted,
983af33b
SDJ
13526 disposition, thread, task,
13527 ignore_count, ops, from_tty,
44f238bb 13528 enabled, internal, flags);
983af33b
SDJ
13529}
13530
13531static void
f00aae0f
KS
13532tracepoint_decode_location (struct breakpoint *b,
13533 const struct event_location *location,
983af33b
SDJ
13534 struct symtabs_and_lines *sals)
13535{
f00aae0f 13536 decode_location_default (b, location, sals);
983af33b
SDJ
13537}
13538
2060206e 13539struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13540
55aa24fb
SDJ
13541/* The breakpoint_ops structure to be use on tracepoints placed in a
13542 static probe. */
13543
13544static void
f00aae0f
KS
13545tracepoint_probe_create_sals_from_location
13546 (const struct event_location *location,
13547 struct linespec_result *canonical,
13548 enum bptype type_wanted)
55aa24fb
SDJ
13549{
13550 /* We use the same method for breakpoint on probes. */
f00aae0f 13551 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13552}
13553
13554static void
f00aae0f
KS
13555tracepoint_probe_decode_location (struct breakpoint *b,
13556 const struct event_location *location,
55aa24fb
SDJ
13557 struct symtabs_and_lines *sals)
13558{
13559 /* We use the same method for breakpoint on probes. */
f00aae0f 13560 bkpt_probe_decode_location (b, location, sals);
55aa24fb
SDJ
13561}
13562
13563static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13564
5c2b4418
HZ
13565/* Dprintf breakpoint_ops methods. */
13566
13567static void
13568dprintf_re_set (struct breakpoint *b)
13569{
13570 breakpoint_re_set_default (b);
13571
f00aae0f
KS
13572 /* extra_string should never be non-NULL for dprintf. */
13573 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13574
13575 /* 1 - connect to target 1, that can run breakpoint commands.
13576 2 - create a dprintf, which resolves fine.
13577 3 - disconnect from target 1
13578 4 - connect to target 2, that can NOT run breakpoint commands.
13579
13580 After steps #3/#4, you'll want the dprintf command list to
13581 be updated, because target 1 and 2 may well return different
13582 answers for target_can_run_breakpoint_commands().
13583 Given absence of finer grained resetting, we get to do
13584 it all the time. */
13585 if (b->extra_string != NULL)
13586 update_dprintf_command_list (b);
13587}
13588
2d9442cc
HZ
13589/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13590
13591static void
13592dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13593{
f00aae0f
KS
13594 fprintf_unfiltered (fp, "dprintf %s,%s",
13595 event_location_to_string (tp->location),
2d9442cc
HZ
13596 tp->extra_string);
13597 print_recreate_thread (tp, fp);
13598}
13599
9d6e6e84
HZ
13600/* Implement the "after_condition_true" breakpoint_ops method for
13601 dprintf.
13602
13603 dprintf's are implemented with regular commands in their command
13604 list, but we run the commands here instead of before presenting the
13605 stop to the user, as dprintf's don't actually cause a stop. This
13606 also makes it so that the commands of multiple dprintfs at the same
13607 address are all handled. */
13608
13609static void
13610dprintf_after_condition_true (struct bpstats *bs)
13611{
13612 struct cleanup *old_chain;
13613 struct bpstats tmp_bs = { NULL };
13614 struct bpstats *tmp_bs_p = &tmp_bs;
13615
13616 /* dprintf's never cause a stop. This wasn't set in the
13617 check_status hook instead because that would make the dprintf's
13618 condition not be evaluated. */
13619 bs->stop = 0;
13620
13621 /* Run the command list here. Take ownership of it instead of
13622 copying. We never want these commands to run later in
13623 bpstat_do_actions, if a breakpoint that causes a stop happens to
13624 be set at same address as this dprintf, or even if running the
13625 commands here throws. */
13626 tmp_bs.commands = bs->commands;
13627 bs->commands = NULL;
13628 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13629
13630 bpstat_do_actions_1 (&tmp_bs_p);
13631
13632 /* 'tmp_bs.commands' will usually be NULL by now, but
13633 bpstat_do_actions_1 may return early without processing the whole
13634 list. */
13635 do_cleanups (old_chain);
13636}
13637
983af33b
SDJ
13638/* The breakpoint_ops structure to be used on static tracepoints with
13639 markers (`-m'). */
13640
13641static void
f00aae0f 13642strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13643 struct linespec_result *canonical,
f00aae0f 13644 enum bptype type_wanted)
983af33b
SDJ
13645{
13646 struct linespec_sals lsal;
f00aae0f
KS
13647 const char *arg_start, *arg;
13648 char *str;
13649 struct cleanup *cleanup;
983af33b 13650
f00aae0f
KS
13651 arg = arg_start = get_linespec_location (location);
13652 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13653
f00aae0f
KS
13654 str = savestring (arg_start, arg - arg_start);
13655 cleanup = make_cleanup (xfree, str);
13656 canonical->location = new_linespec_location (&str);
13657 do_cleanups (cleanup);
983af33b 13658
f00aae0f 13659 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13660 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13661}
13662
13663static void
13664strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13665 struct linespec_result *canonical,
983af33b 13666 char *cond_string,
e7e0cddf 13667 char *extra_string,
983af33b
SDJ
13668 enum bptype type_wanted,
13669 enum bpdisp disposition,
13670 int thread,
13671 int task, int ignore_count,
13672 const struct breakpoint_ops *ops,
13673 int from_tty, int enabled,
44f238bb 13674 int internal, unsigned flags)
983af33b
SDJ
13675{
13676 int i;
52d361e1
YQ
13677 struct linespec_sals *lsal = VEC_index (linespec_sals,
13678 canonical->sals, 0);
983af33b
SDJ
13679
13680 /* If the user is creating a static tracepoint by marker id
13681 (strace -m MARKER_ID), then store the sals index, so that
13682 breakpoint_re_set can try to match up which of the newly
13683 found markers corresponds to this one, and, don't try to
13684 expand multiple locations for each sal, given than SALS
13685 already should contain all sals for MARKER_ID. */
13686
13687 for (i = 0; i < lsal->sals.nelts; ++i)
13688 {
13689 struct symtabs_and_lines expanded;
13690 struct tracepoint *tp;
13691 struct cleanup *old_chain;
f00aae0f 13692 struct event_location *location;
983af33b
SDJ
13693
13694 expanded.nelts = 1;
13695 expanded.sals = &lsal->sals.sals[i];
13696
f00aae0f
KS
13697 location = copy_event_location (canonical->location);
13698 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13699
13700 tp = XCNEW (struct tracepoint);
13701 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13702 location, NULL,
e7e0cddf
SS
13703 cond_string, extra_string,
13704 type_wanted, disposition,
983af33b 13705 thread, task, ignore_count, ops,
44f238bb 13706 from_tty, enabled, internal, flags,
983af33b
SDJ
13707 canonical->special_display);
13708 /* Given that its possible to have multiple markers with
13709 the same string id, if the user is creating a static
13710 tracepoint by marker id ("strace -m MARKER_ID"), then
13711 store the sals index, so that breakpoint_re_set can
13712 try to match up which of the newly found markers
13713 corresponds to this one */
13714 tp->static_trace_marker_id_idx = i;
13715
13716 install_breakpoint (internal, &tp->base, 0);
13717
13718 discard_cleanups (old_chain);
13719 }
13720}
13721
13722static void
f00aae0f
KS
13723strace_marker_decode_location (struct breakpoint *b,
13724 const struct event_location *location,
983af33b
SDJ
13725 struct symtabs_and_lines *sals)
13726{
13727 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13728 const char *s = get_linespec_location (location);
983af33b 13729
f00aae0f 13730 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13731 if (sals->nelts > tp->static_trace_marker_id_idx)
13732 {
13733 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13734 sals->nelts = 1;
13735 }
13736 else
13737 error (_("marker %s not found"), tp->static_trace_marker_id);
13738}
13739
13740static struct breakpoint_ops strace_marker_breakpoint_ops;
13741
13742static int
13743strace_marker_p (struct breakpoint *b)
13744{
13745 return b->ops == &strace_marker_breakpoint_ops;
13746}
13747
53a5351d 13748/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13749 structures. */
c906108c
SS
13750
13751void
fba45db2 13752delete_breakpoint (struct breakpoint *bpt)
c906108c 13753{
52f0bd74 13754 struct breakpoint *b;
c906108c 13755
8a3fe4f8 13756 gdb_assert (bpt != NULL);
c906108c 13757
4a64f543
MS
13758 /* Has this bp already been deleted? This can happen because
13759 multiple lists can hold pointers to bp's. bpstat lists are
13760 especial culprits.
13761
13762 One example of this happening is a watchpoint's scope bp. When
13763 the scope bp triggers, we notice that the watchpoint is out of
13764 scope, and delete it. We also delete its scope bp. But the
13765 scope bp is marked "auto-deleting", and is already on a bpstat.
13766 That bpstat is then checked for auto-deleting bp's, which are
13767 deleted.
13768
13769 A real solution to this problem might involve reference counts in
13770 bp's, and/or giving them pointers back to their referencing
13771 bpstat's, and teaching delete_breakpoint to only free a bp's
13772 storage when no more references were extent. A cheaper bandaid
13773 was chosen. */
c906108c
SS
13774 if (bpt->type == bp_none)
13775 return;
13776
4a64f543
MS
13777 /* At least avoid this stale reference until the reference counting
13778 of breakpoints gets resolved. */
d0fb5eae 13779 if (bpt->related_breakpoint != bpt)
e5a0a904 13780 {
d0fb5eae 13781 struct breakpoint *related;
3a5c3e22 13782 struct watchpoint *w;
d0fb5eae
JK
13783
13784 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13785 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13786 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13787 w = (struct watchpoint *) bpt;
13788 else
13789 w = NULL;
13790 if (w != NULL)
13791 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13792
13793 /* Unlink bpt from the bpt->related_breakpoint ring. */
13794 for (related = bpt; related->related_breakpoint != bpt;
13795 related = related->related_breakpoint);
13796 related->related_breakpoint = bpt->related_breakpoint;
13797 bpt->related_breakpoint = bpt;
e5a0a904
JK
13798 }
13799
a9634178
TJB
13800 /* watch_command_1 creates a watchpoint but only sets its number if
13801 update_watchpoint succeeds in creating its bp_locations. If there's
13802 a problem in that process, we'll be asked to delete the half-created
13803 watchpoint. In that case, don't announce the deletion. */
13804 if (bpt->number)
13805 observer_notify_breakpoint_deleted (bpt);
c906108c 13806
c906108c
SS
13807 if (breakpoint_chain == bpt)
13808 breakpoint_chain = bpt->next;
13809
c906108c
SS
13810 ALL_BREAKPOINTS (b)
13811 if (b->next == bpt)
c5aa993b
JM
13812 {
13813 b->next = bpt->next;
13814 break;
13815 }
c906108c 13816
f431efe5
PA
13817 /* Be sure no bpstat's are pointing at the breakpoint after it's
13818 been freed. */
13819 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13820 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13821 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13822 commands are associated with the bpstat; if we remove it here,
13823 then the later call to bpstat_do_actions (&stop_bpstat); in
13824 event-top.c won't do anything, and temporary breakpoints with
13825 commands won't work. */
13826
13827 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13828
4a64f543
MS
13829 /* Now that breakpoint is removed from breakpoint list, update the
13830 global location list. This will remove locations that used to
13831 belong to this breakpoint. Do this before freeing the breakpoint
13832 itself, since remove_breakpoint looks at location's owner. It
13833 might be better design to have location completely
13834 self-contained, but it's not the case now. */
44702360 13835 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13836
348d480f 13837 bpt->ops->dtor (bpt);
4a64f543
MS
13838 /* On the chance that someone will soon try again to delete this
13839 same bp, we mark it as deleted before freeing its storage. */
c906108c 13840 bpt->type = bp_none;
b8c9b27d 13841 xfree (bpt);
c906108c
SS
13842}
13843
4d6140d9
AC
13844static void
13845do_delete_breakpoint_cleanup (void *b)
13846{
9a3c8263 13847 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13848}
13849
13850struct cleanup *
13851make_cleanup_delete_breakpoint (struct breakpoint *b)
13852{
13853 return make_cleanup (do_delete_breakpoint_cleanup, b);
13854}
13855
51be5b68
PA
13856/* Iterator function to call a user-provided callback function once
13857 for each of B and its related breakpoints. */
13858
13859static void
13860iterate_over_related_breakpoints (struct breakpoint *b,
13861 void (*function) (struct breakpoint *,
13862 void *),
13863 void *data)
13864{
13865 struct breakpoint *related;
13866
13867 related = b;
13868 do
13869 {
13870 struct breakpoint *next;
13871
13872 /* FUNCTION may delete RELATED. */
13873 next = related->related_breakpoint;
13874
13875 if (next == related)
13876 {
13877 /* RELATED is the last ring entry. */
13878 function (related, data);
13879
13880 /* FUNCTION may have deleted it, so we'd never reach back to
13881 B. There's nothing left to do anyway, so just break
13882 out. */
13883 break;
13884 }
13885 else
13886 function (related, data);
13887
13888 related = next;
13889 }
13890 while (related != b);
13891}
95a42b64
TT
13892
13893static void
13894do_delete_breakpoint (struct breakpoint *b, void *ignore)
13895{
13896 delete_breakpoint (b);
13897}
13898
51be5b68
PA
13899/* A callback for map_breakpoint_numbers that calls
13900 delete_breakpoint. */
13901
13902static void
13903do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13904{
13905 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13906}
13907
c906108c 13908void
fba45db2 13909delete_command (char *arg, int from_tty)
c906108c 13910{
35df4500 13911 struct breakpoint *b, *b_tmp;
c906108c 13912
ea9365bb
TT
13913 dont_repeat ();
13914
c906108c
SS
13915 if (arg == 0)
13916 {
13917 int breaks_to_delete = 0;
13918
46c6471b
PA
13919 /* Delete all breakpoints if no argument. Do not delete
13920 internal breakpoints, these have to be deleted with an
13921 explicit breakpoint number argument. */
c5aa993b 13922 ALL_BREAKPOINTS (b)
46c6471b 13923 if (user_breakpoint_p (b))
973d738b
DJ
13924 {
13925 breaks_to_delete = 1;
13926 break;
13927 }
c906108c
SS
13928
13929 /* Ask user only if there are some breakpoints to delete. */
13930 if (!from_tty
e2e0b3e5 13931 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13932 {
35df4500 13933 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13934 if (user_breakpoint_p (b))
c5aa993b 13935 delete_breakpoint (b);
c906108c
SS
13936 }
13937 }
13938 else
51be5b68 13939 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13940}
13941
0d381245
VP
13942static int
13943all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13944{
0d381245 13945 for (; loc; loc = loc->next)
8645ff69
UW
13946 if (!loc->shlib_disabled
13947 && !loc->pspace->executing_startup)
0d381245
VP
13948 return 0;
13949 return 1;
fe3f5fa8
VP
13950}
13951
776592bf
DE
13952/* Subroutine of update_breakpoint_locations to simplify it.
13953 Return non-zero if multiple fns in list LOC have the same name.
13954 Null names are ignored. */
13955
13956static int
13957ambiguous_names_p (struct bp_location *loc)
13958{
13959 struct bp_location *l;
13960 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13961 (int (*) (const void *,
13962 const void *)) streq,
776592bf
DE
13963 NULL, xcalloc, xfree);
13964
13965 for (l = loc; l != NULL; l = l->next)
13966 {
13967 const char **slot;
13968 const char *name = l->function_name;
13969
13970 /* Allow for some names to be NULL, ignore them. */
13971 if (name == NULL)
13972 continue;
13973
13974 slot = (const char **) htab_find_slot (htab, (const void *) name,
13975 INSERT);
4a64f543
MS
13976 /* NOTE: We can assume slot != NULL here because xcalloc never
13977 returns NULL. */
776592bf
DE
13978 if (*slot != NULL)
13979 {
13980 htab_delete (htab);
13981 return 1;
13982 }
13983 *slot = name;
13984 }
13985
13986 htab_delete (htab);
13987 return 0;
13988}
13989
0fb4aa4b
PA
13990/* When symbols change, it probably means the sources changed as well,
13991 and it might mean the static tracepoint markers are no longer at
13992 the same address or line numbers they used to be at last we
13993 checked. Losing your static tracepoints whenever you rebuild is
13994 undesirable. This function tries to resync/rematch gdb static
13995 tracepoints with the markers on the target, for static tracepoints
13996 that have not been set by marker id. Static tracepoint that have
13997 been set by marker id are reset by marker id in breakpoint_re_set.
13998 The heuristic is:
13999
14000 1) For a tracepoint set at a specific address, look for a marker at
14001 the old PC. If one is found there, assume to be the same marker.
14002 If the name / string id of the marker found is different from the
14003 previous known name, assume that means the user renamed the marker
14004 in the sources, and output a warning.
14005
14006 2) For a tracepoint set at a given line number, look for a marker
14007 at the new address of the old line number. If one is found there,
14008 assume to be the same marker. If the name / string id of the
14009 marker found is different from the previous known name, assume that
14010 means the user renamed the marker in the sources, and output a
14011 warning.
14012
14013 3) If a marker is no longer found at the same address or line, it
14014 may mean the marker no longer exists. But it may also just mean
14015 the code changed a bit. Maybe the user added a few lines of code
14016 that made the marker move up or down (in line number terms). Ask
14017 the target for info about the marker with the string id as we knew
14018 it. If found, update line number and address in the matching
14019 static tracepoint. This will get confused if there's more than one
14020 marker with the same ID (possible in UST, although unadvised
14021 precisely because it confuses tools). */
14022
14023static struct symtab_and_line
14024update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14025{
d9b3f62e 14026 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14027 struct static_tracepoint_marker marker;
14028 CORE_ADDR pc;
0fb4aa4b
PA
14029
14030 pc = sal.pc;
14031 if (sal.line)
14032 find_line_pc (sal.symtab, sal.line, &pc);
14033
14034 if (target_static_tracepoint_marker_at (pc, &marker))
14035 {
d9b3f62e 14036 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14037 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14038 b->number,
d9b3f62e 14039 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14040
d9b3f62e
PA
14041 xfree (tp->static_trace_marker_id);
14042 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14043 release_static_tracepoint_marker (&marker);
14044
14045 return sal;
14046 }
14047
14048 /* Old marker wasn't found on target at lineno. Try looking it up
14049 by string ID. */
14050 if (!sal.explicit_pc
14051 && sal.line != 0
14052 && sal.symtab != NULL
d9b3f62e 14053 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14054 {
14055 VEC(static_tracepoint_marker_p) *markers;
14056
14057 markers
d9b3f62e 14058 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14059
14060 if (!VEC_empty(static_tracepoint_marker_p, markers))
14061 {
80e1d417 14062 struct symtab_and_line sal2;
0fb4aa4b 14063 struct symbol *sym;
80e1d417 14064 struct static_tracepoint_marker *tpmarker;
79a45e25 14065 struct ui_out *uiout = current_uiout;
67994074 14066 struct explicit_location explicit_loc;
0fb4aa4b 14067
80e1d417 14068 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14069
d9b3f62e 14070 xfree (tp->static_trace_marker_id);
80e1d417 14071 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14072
14073 warning (_("marker for static tracepoint %d (%s) not "
14074 "found at previous line number"),
d9b3f62e 14075 b->number, tp->static_trace_marker_id);
0fb4aa4b 14076
80e1d417 14077 init_sal (&sal2);
0fb4aa4b 14078
80e1d417 14079 sal2.pc = tpmarker->address;
0fb4aa4b 14080
80e1d417
AS
14081 sal2 = find_pc_line (tpmarker->address, 0);
14082 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14083 ui_out_text (uiout, "Now in ");
14084 if (sym)
14085 {
14086 ui_out_field_string (uiout, "func",
14087 SYMBOL_PRINT_NAME (sym));
14088 ui_out_text (uiout, " at ");
14089 }
05cba821
JK
14090 ui_out_field_string (uiout, "file",
14091 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14092 ui_out_text (uiout, ":");
14093
14094 if (ui_out_is_mi_like_p (uiout))
14095 {
0b0865da 14096 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14097
f35a17b5 14098 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14099 }
14100
80e1d417 14101 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14102 ui_out_text (uiout, "\n");
14103
80e1d417 14104 b->loc->line_number = sal2.line;
2f202fde 14105 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14106
f00aae0f 14107 delete_event_location (b->location);
67994074
KS
14108 initialize_explicit_location (&explicit_loc);
14109 explicit_loc.source_filename
00e52e53 14110 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14111 explicit_loc.line_offset.offset = b->loc->line_number;
14112 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14113 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14114
14115 /* Might be nice to check if function changed, and warn if
14116 so. */
14117
80e1d417 14118 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14119 }
14120 }
14121 return sal;
14122}
14123
8d3788bd
VP
14124/* Returns 1 iff locations A and B are sufficiently same that
14125 we don't need to report breakpoint as changed. */
14126
14127static int
14128locations_are_equal (struct bp_location *a, struct bp_location *b)
14129{
14130 while (a && b)
14131 {
14132 if (a->address != b->address)
14133 return 0;
14134
14135 if (a->shlib_disabled != b->shlib_disabled)
14136 return 0;
14137
14138 if (a->enabled != b->enabled)
14139 return 0;
14140
14141 a = a->next;
14142 b = b->next;
14143 }
14144
14145 if ((a == NULL) != (b == NULL))
14146 return 0;
14147
14148 return 1;
14149}
14150
f1310107
TJB
14151/* Create new breakpoint locations for B (a hardware or software breakpoint)
14152 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14153 a ranged breakpoint. */
14154
0e30163f 14155void
0d381245 14156update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14157 struct symtabs_and_lines sals,
14158 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14159{
14160 int i;
0d381245
VP
14161 struct bp_location *existing_locations = b->loc;
14162
f8eba3c6
TT
14163 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14164 {
14165 /* Ranged breakpoints have only one start location and one end
14166 location. */
14167 b->enable_state = bp_disabled;
44702360 14168 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14169 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14170 "multiple locations found\n"),
14171 b->number);
14172 return;
14173 }
f1310107 14174
4a64f543
MS
14175 /* If there's no new locations, and all existing locations are
14176 pending, don't do anything. This optimizes the common case where
14177 all locations are in the same shared library, that was unloaded.
14178 We'd like to retain the location, so that when the library is
14179 loaded again, we don't loose the enabled/disabled status of the
14180 individual locations. */
0d381245 14181 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14182 return;
14183
fe3f5fa8
VP
14184 b->loc = NULL;
14185
0d381245 14186 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14187 {
f8eba3c6
TT
14188 struct bp_location *new_loc;
14189
14190 switch_to_program_space_and_thread (sals.sals[i].pspace);
14191
14192 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14193
0d381245
VP
14194 /* Reparse conditions, they might contain references to the
14195 old symtab. */
14196 if (b->cond_string != NULL)
14197 {
bbc13ae3 14198 const char *s;
fe3f5fa8 14199
0d381245 14200 s = b->cond_string;
492d29ea 14201 TRY
0d381245 14202 {
1bb9788d
TT
14203 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14204 block_for_pc (sals.sals[i].pc),
0d381245
VP
14205 0);
14206 }
492d29ea 14207 CATCH (e, RETURN_MASK_ERROR)
0d381245 14208 {
3e43a32a
MS
14209 warning (_("failed to reevaluate condition "
14210 "for breakpoint %d: %s"),
0d381245
VP
14211 b->number, e.message);
14212 new_loc->enabled = 0;
14213 }
492d29ea 14214 END_CATCH
0d381245 14215 }
fe3f5fa8 14216
f1310107
TJB
14217 if (sals_end.nelts)
14218 {
14219 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14220
14221 new_loc->length = end - sals.sals[0].pc + 1;
14222 }
0d381245 14223 }
fe3f5fa8 14224
4a64f543
MS
14225 /* If possible, carry over 'disable' status from existing
14226 breakpoints. */
0d381245
VP
14227 {
14228 struct bp_location *e = existing_locations;
776592bf
DE
14229 /* If there are multiple breakpoints with the same function name,
14230 e.g. for inline functions, comparing function names won't work.
14231 Instead compare pc addresses; this is just a heuristic as things
14232 may have moved, but in practice it gives the correct answer
14233 often enough until a better solution is found. */
14234 int have_ambiguous_names = ambiguous_names_p (b->loc);
14235
0d381245
VP
14236 for (; e; e = e->next)
14237 {
14238 if (!e->enabled && e->function_name)
14239 {
14240 struct bp_location *l = b->loc;
776592bf
DE
14241 if (have_ambiguous_names)
14242 {
14243 for (; l; l = l->next)
f1310107 14244 if (breakpoint_locations_match (e, l))
776592bf
DE
14245 {
14246 l->enabled = 0;
14247 break;
14248 }
14249 }
14250 else
14251 {
14252 for (; l; l = l->next)
14253 if (l->function_name
14254 && strcmp (e->function_name, l->function_name) == 0)
14255 {
14256 l->enabled = 0;
14257 break;
14258 }
14259 }
0d381245
VP
14260 }
14261 }
14262 }
fe3f5fa8 14263
8d3788bd
VP
14264 if (!locations_are_equal (existing_locations, b->loc))
14265 observer_notify_breakpoint_modified (b);
14266
44702360 14267 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14268}
14269
f00aae0f 14270/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14271 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14272
14273static struct symtabs_and_lines
f00aae0f
KS
14274location_to_sals (struct breakpoint *b, struct event_location *location,
14275 int *found)
ef23e705 14276{
02d20e4a 14277 struct symtabs_and_lines sals = {0};
492d29ea 14278 struct gdb_exception exception = exception_none;
ef23e705 14279
983af33b 14280 gdb_assert (b->ops != NULL);
ef23e705 14281
492d29ea 14282 TRY
ef23e705 14283 {
f00aae0f 14284 b->ops->decode_location (b, location, &sals);
ef23e705 14285 }
492d29ea 14286 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14287 {
14288 int not_found_and_ok = 0;
492d29ea
PA
14289
14290 exception = e;
14291
ef23e705
TJB
14292 /* For pending breakpoints, it's expected that parsing will
14293 fail until the right shared library is loaded. User has
14294 already told to create pending breakpoints and don't need
14295 extra messages. If breakpoint is in bp_shlib_disabled
14296 state, then user already saw the message about that
14297 breakpoint being disabled, and don't want to see more
14298 errors. */
58438ac1 14299 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14300 && (b->condition_not_parsed
14301 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14302 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14303 || b->enable_state == bp_disabled))
14304 not_found_and_ok = 1;
14305
14306 if (!not_found_and_ok)
14307 {
14308 /* We surely don't want to warn about the same breakpoint
14309 10 times. One solution, implemented here, is disable
14310 the breakpoint on error. Another solution would be to
14311 have separate 'warning emitted' flag. Since this
14312 happens only when a binary has changed, I don't know
14313 which approach is better. */
14314 b->enable_state = bp_disabled;
14315 throw_exception (e);
14316 }
14317 }
492d29ea 14318 END_CATCH
ef23e705 14319
492d29ea 14320 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14321 {
f8eba3c6 14322 int i;
ef23e705 14323
f8eba3c6
TT
14324 for (i = 0; i < sals.nelts; ++i)
14325 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14326 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14327 {
ed1d1739
KS
14328 char *cond_string, *extra_string;
14329 int thread, task;
ef23e705 14330
f00aae0f 14331 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14332 &cond_string, &thread, &task,
14333 &extra_string);
f00aae0f 14334 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14335 if (cond_string)
14336 b->cond_string = cond_string;
14337 b->thread = thread;
14338 b->task = task;
e7e0cddf 14339 if (extra_string)
f00aae0f
KS
14340 {
14341 xfree (b->extra_string);
14342 b->extra_string = extra_string;
14343 }
ef23e705
TJB
14344 b->condition_not_parsed = 0;
14345 }
14346
983af33b 14347 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14348 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14349
58438ac1
TT
14350 *found = 1;
14351 }
14352 else
14353 *found = 0;
ef23e705
TJB
14354
14355 return sals;
14356}
14357
348d480f
PA
14358/* The default re_set method, for typical hardware or software
14359 breakpoints. Reevaluate the breakpoint and recreate its
14360 locations. */
14361
14362static void
28010a5d 14363breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14364{
14365 int found;
f1310107 14366 struct symtabs_and_lines sals, sals_end;
ef23e705 14367 struct symtabs_and_lines expanded = {0};
f1310107 14368 struct symtabs_and_lines expanded_end = {0};
ef23e705 14369
f00aae0f 14370 sals = location_to_sals (b, b->location, &found);
ef23e705
TJB
14371 if (found)
14372 {
14373 make_cleanup (xfree, sals.sals);
f8eba3c6 14374 expanded = sals;
ef23e705
TJB
14375 }
14376
f00aae0f 14377 if (b->location_range_end != NULL)
f1310107 14378 {
f00aae0f 14379 sals_end = location_to_sals (b, b->location_range_end, &found);
f1310107
TJB
14380 if (found)
14381 {
14382 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14383 expanded_end = sals_end;
f1310107
TJB
14384 }
14385 }
14386
14387 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14388}
14389
983af33b
SDJ
14390/* Default method for creating SALs from an address string. It basically
14391 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14392
14393static void
f00aae0f
KS
14394create_sals_from_location_default (const struct event_location *location,
14395 struct linespec_result *canonical,
14396 enum bptype type_wanted)
983af33b 14397{
f00aae0f 14398 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14399}
14400
14401/* Call create_breakpoints_sal for the given arguments. This is the default
14402 function for the `create_breakpoints_sal' method of
14403 breakpoint_ops. */
14404
14405static void
14406create_breakpoints_sal_default (struct gdbarch *gdbarch,
14407 struct linespec_result *canonical,
983af33b 14408 char *cond_string,
e7e0cddf 14409 char *extra_string,
983af33b
SDJ
14410 enum bptype type_wanted,
14411 enum bpdisp disposition,
14412 int thread,
14413 int task, int ignore_count,
14414 const struct breakpoint_ops *ops,
14415 int from_tty, int enabled,
44f238bb 14416 int internal, unsigned flags)
983af33b
SDJ
14417{
14418 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14419 extra_string,
983af33b
SDJ
14420 type_wanted, disposition,
14421 thread, task, ignore_count, ops, from_tty,
44f238bb 14422 enabled, internal, flags);
983af33b
SDJ
14423}
14424
14425/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14426 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14427
14428static void
f00aae0f
KS
14429decode_location_default (struct breakpoint *b,
14430 const struct event_location *location,
983af33b
SDJ
14431 struct symtabs_and_lines *sals)
14432{
14433 struct linespec_result canonical;
14434
14435 init_linespec_result (&canonical);
f00aae0f 14436 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
983af33b
SDJ
14437 (struct symtab *) NULL, 0,
14438 &canonical, multiple_symbols_all,
14439 b->filter);
14440
14441 /* We should get 0 or 1 resulting SALs. */
14442 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14443
14444 if (VEC_length (linespec_sals, canonical.sals) > 0)
14445 {
14446 struct linespec_sals *lsal;
14447
14448 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14449 *sals = lsal->sals;
14450 /* Arrange it so the destructor does not free the
14451 contents. */
14452 lsal->sals.sals = NULL;
14453 }
14454
14455 destroy_linespec_result (&canonical);
14456}
14457
28010a5d
PA
14458/* Prepare the global context for a re-set of breakpoint B. */
14459
14460static struct cleanup *
14461prepare_re_set_context (struct breakpoint *b)
14462{
14463 struct cleanup *cleanups;
14464
14465 input_radix = b->input_radix;
14466 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14467 if (b->pspace != NULL)
14468 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14469 set_language (b->language);
14470
14471 return cleanups;
ef23e705
TJB
14472}
14473
c906108c
SS
14474/* Reset a breakpoint given it's struct breakpoint * BINT.
14475 The value we return ends up being the return value from catch_errors.
14476 Unused in this case. */
14477
14478static int
4efb68b1 14479breakpoint_re_set_one (void *bint)
c906108c 14480{
4a64f543 14481 /* Get past catch_errs. */
53a5351d 14482 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14483 struct cleanup *cleanups;
c906108c 14484
348d480f
PA
14485 cleanups = prepare_re_set_context (b);
14486 b->ops->re_set (b);
14487 do_cleanups (cleanups);
c906108c
SS
14488 return 0;
14489}
14490
69de3c6a 14491/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14492void
69de3c6a 14493breakpoint_re_set (void)
c906108c 14494{
35df4500 14495 struct breakpoint *b, *b_tmp;
c906108c
SS
14496 enum language save_language;
14497 int save_input_radix;
6c95b8df 14498 struct cleanup *old_chain;
c5aa993b 14499
c906108c
SS
14500 save_language = current_language->la_language;
14501 save_input_radix = input_radix;
6c95b8df
PA
14502 old_chain = save_current_program_space ();
14503
35df4500 14504 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14505 {
4a64f543 14506 /* Format possible error msg. */
fe3f5fa8 14507 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14508 b->number);
14509 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14510 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14511 do_cleanups (cleanups);
c5aa993b 14512 }
c906108c
SS
14513 set_language (save_language);
14514 input_radix = save_input_radix;
e62c965a 14515
0756c555 14516 jit_breakpoint_re_set ();
4efc6507 14517
6c95b8df
PA
14518 do_cleanups (old_chain);
14519
af02033e
PP
14520 create_overlay_event_breakpoint ();
14521 create_longjmp_master_breakpoint ();
14522 create_std_terminate_master_breakpoint ();
186c406b 14523 create_exception_master_breakpoint ();
c906108c
SS
14524}
14525\f
c906108c
SS
14526/* Reset the thread number of this breakpoint:
14527
14528 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14529 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14530void
fba45db2 14531breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14532{
14533 if (b->thread != -1)
14534 {
39f77062
KB
14535 if (in_thread_list (inferior_ptid))
14536 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14537
14538 /* We're being called after following a fork. The new fork is
14539 selected as current, and unless this was a vfork will have a
14540 different program space from the original thread. Reset that
14541 as well. */
14542 b->loc->pspace = current_program_space;
c906108c
SS
14543 }
14544}
14545
03ac34d5
MS
14546/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14547 If from_tty is nonzero, it prints a message to that effect,
14548 which ends with a period (no newline). */
14549
c906108c 14550void
fba45db2 14551set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14552{
52f0bd74 14553 struct breakpoint *b;
c906108c
SS
14554
14555 if (count < 0)
14556 count = 0;
14557
14558 ALL_BREAKPOINTS (b)
14559 if (b->number == bptnum)
c5aa993b 14560 {
d77f58be
SS
14561 if (is_tracepoint (b))
14562 {
14563 if (from_tty && count != 0)
14564 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14565 bptnum);
14566 return;
14567 }
14568
c5aa993b 14569 b->ignore_count = count;
221ea385
KS
14570 if (from_tty)
14571 {
14572 if (count == 0)
3e43a32a
MS
14573 printf_filtered (_("Will stop next time "
14574 "breakpoint %d is reached."),
221ea385
KS
14575 bptnum);
14576 else if (count == 1)
a3f17187 14577 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14578 bptnum);
14579 else
3e43a32a
MS
14580 printf_filtered (_("Will ignore next %d "
14581 "crossings of breakpoint %d."),
221ea385
KS
14582 count, bptnum);
14583 }
8d3788bd 14584 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14585 return;
14586 }
c906108c 14587
8a3fe4f8 14588 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14589}
14590
c906108c
SS
14591/* Command to set ignore-count of breakpoint N to COUNT. */
14592
14593static void
fba45db2 14594ignore_command (char *args, int from_tty)
c906108c
SS
14595{
14596 char *p = args;
52f0bd74 14597 int num;
c906108c
SS
14598
14599 if (p == 0)
e2e0b3e5 14600 error_no_arg (_("a breakpoint number"));
c5aa993b 14601
c906108c 14602 num = get_number (&p);
5c44784c 14603 if (num == 0)
8a3fe4f8 14604 error (_("bad breakpoint number: '%s'"), args);
c906108c 14605 if (*p == 0)
8a3fe4f8 14606 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14607
14608 set_ignore_count (num,
14609 longest_to_int (value_as_long (parse_and_eval (p))),
14610 from_tty);
221ea385
KS
14611 if (from_tty)
14612 printf_filtered ("\n");
c906108c
SS
14613}
14614\f
14615/* Call FUNCTION on each of the breakpoints
14616 whose numbers are given in ARGS. */
14617
14618static void
95a42b64
TT
14619map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14620 void *),
14621 void *data)
c906108c 14622{
52f0bd74
AC
14623 int num;
14624 struct breakpoint *b, *tmp;
11cf8741 14625 int match;
197f0a60 14626 struct get_number_or_range_state state;
c906108c 14627
b9d61307 14628 if (args == 0 || *args == '\0')
e2e0b3e5 14629 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14630
197f0a60
TT
14631 init_number_or_range (&state, args);
14632
14633 while (!state.finished)
c906108c 14634 {
e799154c 14635 const char *p = state.string;
197f0a60 14636
11cf8741 14637 match = 0;
c5aa993b 14638
197f0a60 14639 num = get_number_or_range (&state);
5c44784c 14640 if (num == 0)
c5aa993b 14641 {
8a3fe4f8 14642 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14643 }
14644 else
14645 {
14646 ALL_BREAKPOINTS_SAFE (b, tmp)
14647 if (b->number == num)
14648 {
11cf8741 14649 match = 1;
cdac0397 14650 function (b, data);
11cf8741 14651 break;
5c44784c 14652 }
11cf8741 14653 if (match == 0)
a3f17187 14654 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14655 }
c906108c
SS
14656 }
14657}
14658
0d381245
VP
14659static struct bp_location *
14660find_location_by_number (char *number)
14661{
14662 char *dot = strchr (number, '.');
14663 char *p1;
14664 int bp_num;
14665 int loc_num;
14666 struct breakpoint *b;
14667 struct bp_location *loc;
14668
14669 *dot = '\0';
14670
14671 p1 = number;
197f0a60 14672 bp_num = get_number (&p1);
0d381245
VP
14673 if (bp_num == 0)
14674 error (_("Bad breakpoint number '%s'"), number);
14675
14676 ALL_BREAKPOINTS (b)
14677 if (b->number == bp_num)
14678 {
14679 break;
14680 }
14681
14682 if (!b || b->number != bp_num)
14683 error (_("Bad breakpoint number '%s'"), number);
14684
14685 p1 = dot+1;
197f0a60 14686 loc_num = get_number (&p1);
0d381245
VP
14687 if (loc_num == 0)
14688 error (_("Bad breakpoint location number '%s'"), number);
14689
14690 --loc_num;
14691 loc = b->loc;
14692 for (;loc_num && loc; --loc_num, loc = loc->next)
14693 ;
14694 if (!loc)
14695 error (_("Bad breakpoint location number '%s'"), dot+1);
14696
14697 return loc;
14698}
14699
14700
1900040c
MS
14701/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14702 If from_tty is nonzero, it prints a message to that effect,
14703 which ends with a period (no newline). */
14704
c906108c 14705void
fba45db2 14706disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14707{
14708 /* Never disable a watchpoint scope breakpoint; we want to
14709 hit them when we leave scope so we can delete both the
14710 watchpoint and its scope breakpoint at that time. */
14711 if (bpt->type == bp_watchpoint_scope)
14712 return;
14713
b5de0fa7 14714 bpt->enable_state = bp_disabled;
c906108c 14715
b775012e
LM
14716 /* Mark breakpoint locations modified. */
14717 mark_breakpoint_modified (bpt);
14718
d248b706
KY
14719 if (target_supports_enable_disable_tracepoint ()
14720 && current_trace_status ()->running && is_tracepoint (bpt))
14721 {
14722 struct bp_location *location;
14723
14724 for (location = bpt->loc; location; location = location->next)
14725 target_disable_tracepoint (location);
14726 }
14727
44702360 14728 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14729
8d3788bd 14730 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14731}
14732
51be5b68
PA
14733/* A callback for iterate_over_related_breakpoints. */
14734
14735static void
14736do_disable_breakpoint (struct breakpoint *b, void *ignore)
14737{
14738 disable_breakpoint (b);
14739}
14740
95a42b64
TT
14741/* A callback for map_breakpoint_numbers that calls
14742 disable_breakpoint. */
14743
14744static void
14745do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14746{
51be5b68 14747 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14748}
14749
c906108c 14750static void
fba45db2 14751disable_command (char *args, int from_tty)
c906108c 14752{
c906108c 14753 if (args == 0)
46c6471b
PA
14754 {
14755 struct breakpoint *bpt;
14756
14757 ALL_BREAKPOINTS (bpt)
14758 if (user_breakpoint_p (bpt))
14759 disable_breakpoint (bpt);
14760 }
9eaabc75 14761 else
0d381245 14762 {
9eaabc75
MW
14763 char *num = extract_arg (&args);
14764
14765 while (num)
d248b706 14766 {
9eaabc75 14767 if (strchr (num, '.'))
b775012e 14768 {
9eaabc75
MW
14769 struct bp_location *loc = find_location_by_number (num);
14770
14771 if (loc)
14772 {
14773 if (loc->enabled)
14774 {
14775 loc->enabled = 0;
14776 mark_breakpoint_location_modified (loc);
14777 }
14778 if (target_supports_enable_disable_tracepoint ()
14779 && current_trace_status ()->running && loc->owner
14780 && is_tracepoint (loc->owner))
14781 target_disable_tracepoint (loc);
14782 }
44702360 14783 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14784 }
9eaabc75
MW
14785 else
14786 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14787 num = extract_arg (&args);
d248b706 14788 }
0d381245 14789 }
c906108c
SS
14790}
14791
14792static void
816338b5
SS
14793enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14794 int count)
c906108c 14795{
afe38095 14796 int target_resources_ok;
c906108c
SS
14797
14798 if (bpt->type == bp_hardware_breakpoint)
14799 {
14800 int i;
c5aa993b 14801 i = hw_breakpoint_used_count ();
53a5351d 14802 target_resources_ok =
d92524f1 14803 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14804 i + 1, 0);
c906108c 14805 if (target_resources_ok == 0)
8a3fe4f8 14806 error (_("No hardware breakpoint support in the target."));
c906108c 14807 else if (target_resources_ok < 0)
8a3fe4f8 14808 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14809 }
14810
cc60f2e3 14811 if (is_watchpoint (bpt))
c906108c 14812 {
d07205c2 14813 /* Initialize it just to avoid a GCC false warning. */
f486487f 14814 enum enable_state orig_enable_state = bp_disabled;
dde02812 14815
492d29ea 14816 TRY
c906108c 14817 {
3a5c3e22
PA
14818 struct watchpoint *w = (struct watchpoint *) bpt;
14819
1e718ff1
TJB
14820 orig_enable_state = bpt->enable_state;
14821 bpt->enable_state = bp_enabled;
3a5c3e22 14822 update_watchpoint (w, 1 /* reparse */);
c906108c 14823 }
492d29ea 14824 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14825 {
1e718ff1 14826 bpt->enable_state = orig_enable_state;
dde02812
ES
14827 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14828 bpt->number);
14829 return;
c5aa993b 14830 }
492d29ea 14831 END_CATCH
c906108c 14832 }
0101ce28 14833
b775012e
LM
14834 bpt->enable_state = bp_enabled;
14835
14836 /* Mark breakpoint locations modified. */
14837 mark_breakpoint_modified (bpt);
14838
d248b706
KY
14839 if (target_supports_enable_disable_tracepoint ()
14840 && current_trace_status ()->running && is_tracepoint (bpt))
14841 {
14842 struct bp_location *location;
14843
14844 for (location = bpt->loc; location; location = location->next)
14845 target_enable_tracepoint (location);
14846 }
14847
b4c291bb 14848 bpt->disposition = disposition;
816338b5 14849 bpt->enable_count = count;
44702360 14850 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14851
8d3788bd 14852 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14853}
14854
fe3f5fa8 14855
c906108c 14856void
fba45db2 14857enable_breakpoint (struct breakpoint *bpt)
c906108c 14858{
816338b5 14859 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14860}
14861
14862static void
14863do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14864{
14865 enable_breakpoint (bpt);
c906108c
SS
14866}
14867
95a42b64
TT
14868/* A callback for map_breakpoint_numbers that calls
14869 enable_breakpoint. */
14870
14871static void
14872do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14873{
51be5b68 14874 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14875}
14876
c906108c
SS
14877/* The enable command enables the specified breakpoints (or all defined
14878 breakpoints) so they once again become (or continue to be) effective
1272ad14 14879 in stopping the inferior. */
c906108c 14880
c906108c 14881static void
fba45db2 14882enable_command (char *args, int from_tty)
c906108c 14883{
c906108c 14884 if (args == 0)
46c6471b
PA
14885 {
14886 struct breakpoint *bpt;
14887
14888 ALL_BREAKPOINTS (bpt)
14889 if (user_breakpoint_p (bpt))
14890 enable_breakpoint (bpt);
14891 }
9eaabc75 14892 else
0d381245 14893 {
9eaabc75
MW
14894 char *num = extract_arg (&args);
14895
14896 while (num)
d248b706 14897 {
9eaabc75 14898 if (strchr (num, '.'))
b775012e 14899 {
9eaabc75
MW
14900 struct bp_location *loc = find_location_by_number (num);
14901
14902 if (loc)
14903 {
14904 if (!loc->enabled)
14905 {
14906 loc->enabled = 1;
14907 mark_breakpoint_location_modified (loc);
14908 }
14909 if (target_supports_enable_disable_tracepoint ()
14910 && current_trace_status ()->running && loc->owner
14911 && is_tracepoint (loc->owner))
14912 target_enable_tracepoint (loc);
14913 }
44702360 14914 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14915 }
9eaabc75
MW
14916 else
14917 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14918 num = extract_arg (&args);
d248b706 14919 }
0d381245 14920 }
c906108c
SS
14921}
14922
816338b5
SS
14923/* This struct packages up disposition data for application to multiple
14924 breakpoints. */
14925
14926struct disp_data
14927{
14928 enum bpdisp disp;
14929 int count;
14930};
14931
c906108c 14932static void
51be5b68
PA
14933do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14934{
816338b5 14935 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14936
816338b5 14937 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14938}
14939
14940static void
14941do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14942{
816338b5 14943 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14944
14945 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14946}
14947
c906108c 14948static void
fba45db2 14949enable_once_command (char *args, int from_tty)
c906108c 14950{
51be5b68 14951 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14952}
14953
816338b5
SS
14954static void
14955do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14956{
14957 struct disp_data disp = { disp_disable, *(int *) countptr };
14958
14959 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14960}
14961
14962static void
14963enable_count_command (char *args, int from_tty)
14964{
b9d61307
SM
14965 int count;
14966
14967 if (args == NULL)
14968 error_no_arg (_("hit count"));
14969
14970 count = get_number (&args);
816338b5
SS
14971
14972 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14973}
14974
c906108c 14975static void
51be5b68 14976do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14977{
816338b5 14978 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14979
14980 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14981}
14982
c906108c 14983static void
fba45db2 14984enable_delete_command (char *args, int from_tty)
c906108c 14985{
51be5b68 14986 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14987}
14988\f
fa8d40ab
JJ
14989static void
14990set_breakpoint_cmd (char *args, int from_tty)
14991{
14992}
14993
14994static void
14995show_breakpoint_cmd (char *args, int from_tty)
14996{
14997}
14998
1f3b5d1b
PP
14999/* Invalidate last known value of any hardware watchpoint if
15000 the memory which that value represents has been written to by
15001 GDB itself. */
15002
15003static void
8de0566d
YQ
15004invalidate_bp_value_on_memory_change (struct inferior *inferior,
15005 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15006 const bfd_byte *data)
15007{
15008 struct breakpoint *bp;
15009
15010 ALL_BREAKPOINTS (bp)
15011 if (bp->enable_state == bp_enabled
3a5c3e22 15012 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15013 {
3a5c3e22 15014 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15015
3a5c3e22
PA
15016 if (wp->val_valid && wp->val)
15017 {
15018 struct bp_location *loc;
15019
15020 for (loc = bp->loc; loc != NULL; loc = loc->next)
15021 if (loc->loc_type == bp_loc_hardware_watchpoint
15022 && loc->address + loc->length > addr
15023 && addr + len > loc->address)
15024 {
15025 value_free (wp->val);
15026 wp->val = NULL;
15027 wp->val_valid = 0;
15028 }
15029 }
1f3b5d1b
PP
15030 }
15031}
15032
8181d85f
DJ
15033/* Create and insert a breakpoint for software single step. */
15034
15035void
6c95b8df 15036insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15037 struct address_space *aspace,
15038 CORE_ADDR next_pc)
8181d85f 15039{
7c16b83e
PA
15040 struct thread_info *tp = inferior_thread ();
15041 struct symtab_and_line sal;
15042 CORE_ADDR pc = next_pc;
8181d85f 15043
34b7e8a6
PA
15044 if (tp->control.single_step_breakpoints == NULL)
15045 {
15046 tp->control.single_step_breakpoints
15047 = new_single_step_breakpoint (tp->num, gdbarch);
15048 }
8181d85f 15049
7c16b83e
PA
15050 sal = find_pc_line (pc, 0);
15051 sal.pc = pc;
15052 sal.section = find_pc_overlay (pc);
15053 sal.explicit_pc = 1;
34b7e8a6 15054 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15055
7c16b83e 15056 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15057}
15058
34b7e8a6 15059/* See breakpoint.h. */
f02253f1
HZ
15060
15061int
7c16b83e
PA
15062breakpoint_has_location_inserted_here (struct breakpoint *bp,
15063 struct address_space *aspace,
15064 CORE_ADDR pc)
1aafd4da 15065{
7c16b83e 15066 struct bp_location *loc;
1aafd4da 15067
7c16b83e
PA
15068 for (loc = bp->loc; loc != NULL; loc = loc->next)
15069 if (loc->inserted
15070 && breakpoint_location_address_match (loc, aspace, pc))
15071 return 1;
1aafd4da 15072
7c16b83e 15073 return 0;
ef370185
JB
15074}
15075
15076/* Check whether a software single-step breakpoint is inserted at
15077 PC. */
15078
15079int
15080single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15081 CORE_ADDR pc)
15082{
34b7e8a6
PA
15083 struct breakpoint *bpt;
15084
15085 ALL_BREAKPOINTS (bpt)
15086 {
15087 if (bpt->type == bp_single_step
15088 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15089 return 1;
15090 }
15091 return 0;
1aafd4da
UW
15092}
15093
1042e4c0
SS
15094/* Tracepoint-specific operations. */
15095
15096/* Set tracepoint count to NUM. */
15097static void
15098set_tracepoint_count (int num)
15099{
15100 tracepoint_count = num;
4fa62494 15101 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15102}
15103
70221824 15104static void
1042e4c0
SS
15105trace_command (char *arg, int from_tty)
15106{
55aa24fb 15107 struct breakpoint_ops *ops;
f00aae0f
KS
15108 struct event_location *location;
15109 struct cleanup *back_to;
55aa24fb 15110
f00aae0f
KS
15111 location = string_to_event_location (&arg, current_language);
15112 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15113 if (location != NULL
15114 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15115 ops = &tracepoint_probe_breakpoint_ops;
15116 else
15117 ops = &tracepoint_breakpoint_ops;
15118
558a9d82 15119 create_breakpoint (get_current_arch (),
f00aae0f
KS
15120 location,
15121 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15122 0 /* tempflag */,
15123 bp_tracepoint /* type_wanted */,
15124 0 /* Ignore count */,
15125 pending_break_support,
15126 ops,
15127 from_tty,
15128 1 /* enabled */,
15129 0 /* internal */, 0);
f00aae0f 15130 do_cleanups (back_to);
1042e4c0
SS
15131}
15132
70221824 15133static void
7a697b8d
SS
15134ftrace_command (char *arg, int from_tty)
15135{
f00aae0f
KS
15136 struct event_location *location;
15137 struct cleanup *back_to;
15138
15139 location = string_to_event_location (&arg, current_language);
15140 back_to = make_cleanup_delete_event_location (location);
558a9d82 15141 create_breakpoint (get_current_arch (),
f00aae0f
KS
15142 location,
15143 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15144 0 /* tempflag */,
15145 bp_fast_tracepoint /* type_wanted */,
15146 0 /* Ignore count */,
15147 pending_break_support,
15148 &tracepoint_breakpoint_ops,
15149 from_tty,
15150 1 /* enabled */,
15151 0 /* internal */, 0);
f00aae0f 15152 do_cleanups (back_to);
0fb4aa4b
PA
15153}
15154
15155/* strace command implementation. Creates a static tracepoint. */
15156
70221824 15157static void
0fb4aa4b
PA
15158strace_command (char *arg, int from_tty)
15159{
983af33b 15160 struct breakpoint_ops *ops;
f00aae0f
KS
15161 struct event_location *location;
15162 struct cleanup *back_to;
983af33b
SDJ
15163
15164 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15165 or with a normal static tracepoint. */
61012eef 15166 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15167 {
15168 ops = &strace_marker_breakpoint_ops;
15169 location = new_linespec_location (&arg);
15170 }
983af33b 15171 else
f00aae0f
KS
15172 {
15173 ops = &tracepoint_breakpoint_ops;
15174 location = string_to_event_location (&arg, current_language);
15175 }
983af33b 15176
f00aae0f 15177 back_to = make_cleanup_delete_event_location (location);
558a9d82 15178 create_breakpoint (get_current_arch (),
f00aae0f
KS
15179 location,
15180 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15181 0 /* tempflag */,
15182 bp_static_tracepoint /* type_wanted */,
15183 0 /* Ignore count */,
15184 pending_break_support,
15185 ops,
15186 from_tty,
15187 1 /* enabled */,
15188 0 /* internal */, 0);
f00aae0f 15189 do_cleanups (back_to);
7a697b8d
SS
15190}
15191
409873ef
SS
15192/* Set up a fake reader function that gets command lines from a linked
15193 list that was acquired during tracepoint uploading. */
15194
15195static struct uploaded_tp *this_utp;
3149d8c1 15196static int next_cmd;
409873ef
SS
15197
15198static char *
15199read_uploaded_action (void)
15200{
15201 char *rslt;
15202
3149d8c1 15203 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15204
3149d8c1 15205 next_cmd++;
409873ef
SS
15206
15207 return rslt;
15208}
15209
00bf0b85
SS
15210/* Given information about a tracepoint as recorded on a target (which
15211 can be either a live system or a trace file), attempt to create an
15212 equivalent GDB tracepoint. This is not a reliable process, since
15213 the target does not necessarily have all the information used when
15214 the tracepoint was originally defined. */
15215
d9b3f62e 15216struct tracepoint *
00bf0b85 15217create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15218{
409873ef 15219 char *addr_str, small_buf[100];
d9b3f62e 15220 struct tracepoint *tp;
f00aae0f
KS
15221 struct event_location *location;
15222 struct cleanup *cleanup;
fd9b8c24 15223
409873ef
SS
15224 if (utp->at_string)
15225 addr_str = utp->at_string;
15226 else
15227 {
15228 /* In the absence of a source location, fall back to raw
15229 address. Since there is no way to confirm that the address
15230 means the same thing as when the trace was started, warn the
15231 user. */
3e43a32a
MS
15232 warning (_("Uploaded tracepoint %d has no "
15233 "source location, using raw address"),
409873ef 15234 utp->number);
8c042590 15235 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15236 addr_str = small_buf;
15237 }
15238
15239 /* There's not much we can do with a sequence of bytecodes. */
15240 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15241 warning (_("Uploaded tracepoint %d condition "
15242 "has no source form, ignoring it"),
409873ef 15243 utp->number);
d5551862 15244
f00aae0f
KS
15245 location = string_to_event_location (&addr_str, current_language);
15246 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15247 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15248 location,
15249 utp->cond_string, -1, addr_str,
e7e0cddf 15250 0 /* parse cond/thread */,
8cdf0e15 15251 0 /* tempflag */,
0fb4aa4b 15252 utp->type /* type_wanted */,
8cdf0e15
VP
15253 0 /* Ignore count */,
15254 pending_break_support,
348d480f 15255 &tracepoint_breakpoint_ops,
8cdf0e15 15256 0 /* from_tty */,
84f4c1fe 15257 utp->enabled /* enabled */,
44f238bb
PA
15258 0 /* internal */,
15259 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15260 {
15261 do_cleanups (cleanup);
15262 return NULL;
15263 }
15264
15265 do_cleanups (cleanup);
fd9b8c24 15266
409873ef 15267 /* Get the tracepoint we just created. */
fd9b8c24
PA
15268 tp = get_tracepoint (tracepoint_count);
15269 gdb_assert (tp != NULL);
d5551862 15270
00bf0b85
SS
15271 if (utp->pass > 0)
15272 {
8c042590
PM
15273 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15274 tp->base.number);
00bf0b85 15275
409873ef 15276 trace_pass_command (small_buf, 0);
00bf0b85
SS
15277 }
15278
409873ef
SS
15279 /* If we have uploaded versions of the original commands, set up a
15280 special-purpose "reader" function and call the usual command line
15281 reader, then pass the result to the breakpoint command-setting
15282 function. */
3149d8c1 15283 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15284 {
409873ef 15285 struct command_line *cmd_list;
00bf0b85 15286
409873ef 15287 this_utp = utp;
3149d8c1 15288 next_cmd = 0;
d5551862 15289
409873ef
SS
15290 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15291
d9b3f62e 15292 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15293 }
3149d8c1
SS
15294 else if (!VEC_empty (char_ptr, utp->actions)
15295 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15296 warning (_("Uploaded tracepoint %d actions "
15297 "have no source form, ignoring them"),
409873ef 15298 utp->number);
00bf0b85 15299
f196051f
SS
15300 /* Copy any status information that might be available. */
15301 tp->base.hit_count = utp->hit_count;
15302 tp->traceframe_usage = utp->traceframe_usage;
15303
00bf0b85 15304 return tp;
d9b3f62e 15305}
00bf0b85 15306
1042e4c0
SS
15307/* Print information on tracepoint number TPNUM_EXP, or all if
15308 omitted. */
15309
15310static void
e5a67952 15311tracepoints_info (char *args, int from_tty)
1042e4c0 15312{
79a45e25 15313 struct ui_out *uiout = current_uiout;
e5a67952 15314 int num_printed;
1042e4c0 15315
e5a67952 15316 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15317
15318 if (num_printed == 0)
1042e4c0 15319 {
e5a67952 15320 if (args == NULL || *args == '\0')
d77f58be
SS
15321 ui_out_message (uiout, 0, "No tracepoints.\n");
15322 else
e5a67952 15323 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15324 }
ad443146
SS
15325
15326 default_collect_info ();
1042e4c0
SS
15327}
15328
4a64f543 15329/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15330 Not supported by all targets. */
15331static void
15332enable_trace_command (char *args, int from_tty)
15333{
15334 enable_command (args, from_tty);
15335}
15336
4a64f543 15337/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15338 Not supported by all targets. */
15339static void
15340disable_trace_command (char *args, int from_tty)
15341{
15342 disable_command (args, from_tty);
15343}
15344
4a64f543 15345/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15346static void
15347delete_trace_command (char *arg, int from_tty)
15348{
35df4500 15349 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15350
15351 dont_repeat ();
15352
15353 if (arg == 0)
15354 {
15355 int breaks_to_delete = 0;
15356
15357 /* Delete all breakpoints if no argument.
15358 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15359 have to be deleted with an explicit breakpoint number
15360 argument. */
1042e4c0 15361 ALL_TRACEPOINTS (b)
46c6471b 15362 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15363 {
15364 breaks_to_delete = 1;
15365 break;
15366 }
1042e4c0
SS
15367
15368 /* Ask user only if there are some breakpoints to delete. */
15369 if (!from_tty
15370 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15371 {
35df4500 15372 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15373 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15374 delete_breakpoint (b);
1042e4c0
SS
15375 }
15376 }
15377 else
51be5b68 15378 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15379}
15380
197f0a60
TT
15381/* Helper function for trace_pass_command. */
15382
15383static void
d9b3f62e 15384trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15385{
d9b3f62e 15386 tp->pass_count = count;
6f6484cd 15387 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15388 if (from_tty)
15389 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15390 tp->base.number, count);
197f0a60
TT
15391}
15392
1042e4c0
SS
15393/* Set passcount for tracepoint.
15394
15395 First command argument is passcount, second is tracepoint number.
15396 If tracepoint number omitted, apply to most recently defined.
15397 Also accepts special argument "all". */
15398
15399static void
15400trace_pass_command (char *args, int from_tty)
15401{
d9b3f62e 15402 struct tracepoint *t1;
1042e4c0 15403 unsigned int count;
1042e4c0
SS
15404
15405 if (args == 0 || *args == 0)
3e43a32a
MS
15406 error (_("passcount command requires an "
15407 "argument (count + optional TP num)"));
1042e4c0 15408
4a64f543 15409 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15410
529480d0 15411 args = skip_spaces (args);
1042e4c0
SS
15412 if (*args && strncasecmp (args, "all", 3) == 0)
15413 {
d9b3f62e
PA
15414 struct breakpoint *b;
15415
1042e4c0 15416 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15417 if (*args)
15418 error (_("Junk at end of arguments."));
1042e4c0 15419
d9b3f62e 15420 ALL_TRACEPOINTS (b)
197f0a60 15421 {
d9b3f62e 15422 t1 = (struct tracepoint *) b;
197f0a60
TT
15423 trace_pass_set_count (t1, count, from_tty);
15424 }
15425 }
15426 else if (*args == '\0')
1042e4c0 15427 {
5fa1d40e 15428 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15429 if (t1)
197f0a60
TT
15430 trace_pass_set_count (t1, count, from_tty);
15431 }
15432 else
15433 {
15434 struct get_number_or_range_state state;
15435
15436 init_number_or_range (&state, args);
15437 while (!state.finished)
1042e4c0 15438 {
5fa1d40e 15439 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15440 if (t1)
15441 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15442 }
15443 }
1042e4c0
SS
15444}
15445
d9b3f62e 15446struct tracepoint *
1042e4c0
SS
15447get_tracepoint (int num)
15448{
15449 struct breakpoint *t;
15450
15451 ALL_TRACEPOINTS (t)
15452 if (t->number == num)
d9b3f62e 15453 return (struct tracepoint *) t;
1042e4c0
SS
15454
15455 return NULL;
15456}
15457
d5551862
SS
15458/* Find the tracepoint with the given target-side number (which may be
15459 different from the tracepoint number after disconnecting and
15460 reconnecting). */
15461
d9b3f62e 15462struct tracepoint *
d5551862
SS
15463get_tracepoint_by_number_on_target (int num)
15464{
d9b3f62e 15465 struct breakpoint *b;
d5551862 15466
d9b3f62e
PA
15467 ALL_TRACEPOINTS (b)
15468 {
15469 struct tracepoint *t = (struct tracepoint *) b;
15470
15471 if (t->number_on_target == num)
15472 return t;
15473 }
d5551862
SS
15474
15475 return NULL;
15476}
15477
1042e4c0 15478/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15479 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15480 If the argument is missing, the most recent tracepoint
15481 (tracepoint_count) is returned. */
15482
d9b3f62e 15483struct tracepoint *
197f0a60 15484get_tracepoint_by_number (char **arg,
5fa1d40e 15485 struct get_number_or_range_state *state)
1042e4c0 15486{
1042e4c0
SS
15487 struct breakpoint *t;
15488 int tpnum;
15489 char *instring = arg == NULL ? NULL : *arg;
15490
197f0a60
TT
15491 if (state)
15492 {
15493 gdb_assert (!state->finished);
15494 tpnum = get_number_or_range (state);
15495 }
15496 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15497 tpnum = tracepoint_count;
1042e4c0 15498 else
197f0a60 15499 tpnum = get_number (arg);
1042e4c0
SS
15500
15501 if (tpnum <= 0)
15502 {
15503 if (instring && *instring)
15504 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15505 instring);
15506 else
5fa1d40e 15507 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15508 return NULL;
15509 }
15510
15511 ALL_TRACEPOINTS (t)
15512 if (t->number == tpnum)
15513 {
d9b3f62e 15514 return (struct tracepoint *) t;
1042e4c0
SS
15515 }
15516
1042e4c0
SS
15517 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15518 return NULL;
15519}
15520
d9b3f62e
PA
15521void
15522print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15523{
15524 if (b->thread != -1)
15525 fprintf_unfiltered (fp, " thread %d", b->thread);
15526
15527 if (b->task != 0)
15528 fprintf_unfiltered (fp, " task %d", b->task);
15529
15530 fprintf_unfiltered (fp, "\n");
15531}
15532
6149aea9
PA
15533/* Save information on user settable breakpoints (watchpoints, etc) to
15534 a new script file named FILENAME. If FILTER is non-NULL, call it
15535 on each breakpoint and only include the ones for which it returns
15536 non-zero. */
15537
1042e4c0 15538static void
6149aea9
PA
15539save_breakpoints (char *filename, int from_tty,
15540 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15541{
15542 struct breakpoint *tp;
6149aea9 15543 int any = 0;
1042e4c0 15544 struct cleanup *cleanup;
a7bdde9e 15545 struct ui_file *fp;
6149aea9 15546 int extra_trace_bits = 0;
1042e4c0 15547
6149aea9
PA
15548 if (filename == 0 || *filename == 0)
15549 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15550
15551 /* See if we have anything to save. */
6149aea9 15552 ALL_BREAKPOINTS (tp)
1042e4c0 15553 {
6149aea9 15554 /* Skip internal and momentary breakpoints. */
09d682a4 15555 if (!user_breakpoint_p (tp))
6149aea9
PA
15556 continue;
15557
15558 /* If we have a filter, only save the breakpoints it accepts. */
15559 if (filter && !filter (tp))
15560 continue;
15561
15562 any = 1;
15563
15564 if (is_tracepoint (tp))
15565 {
15566 extra_trace_bits = 1;
15567
15568 /* We can stop searching. */
15569 break;
15570 }
1042e4c0 15571 }
6149aea9
PA
15572
15573 if (!any)
1042e4c0 15574 {
6149aea9 15575 warning (_("Nothing to save."));
1042e4c0
SS
15576 return;
15577 }
15578
c718be47
PA
15579 filename = tilde_expand (filename);
15580 cleanup = make_cleanup (xfree, filename);
15581 fp = gdb_fopen (filename, "w");
059fb39f 15582 if (!fp)
6149aea9
PA
15583 error (_("Unable to open file '%s' for saving (%s)"),
15584 filename, safe_strerror (errno));
a7bdde9e 15585 make_cleanup_ui_file_delete (fp);
8bf6485c 15586
6149aea9
PA
15587 if (extra_trace_bits)
15588 save_trace_state_variables (fp);
8bf6485c 15589
6149aea9 15590 ALL_BREAKPOINTS (tp)
1042e4c0 15591 {
6149aea9 15592 /* Skip internal and momentary breakpoints. */
09d682a4 15593 if (!user_breakpoint_p (tp))
6149aea9 15594 continue;
8bf6485c 15595
6149aea9
PA
15596 /* If we have a filter, only save the breakpoints it accepts. */
15597 if (filter && !filter (tp))
15598 continue;
15599
348d480f 15600 tp->ops->print_recreate (tp, fp);
1042e4c0 15601
6149aea9
PA
15602 /* Note, we can't rely on tp->number for anything, as we can't
15603 assume the recreated breakpoint numbers will match. Use $bpnum
15604 instead. */
15605
15606 if (tp->cond_string)
15607 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15608
15609 if (tp->ignore_count)
15610 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15611
2d9442cc 15612 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15613 {
6c63c96a 15614 struct gdb_exception exception;
a7bdde9e 15615
6149aea9 15616 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15617
79a45e25 15618 ui_out_redirect (current_uiout, fp);
492d29ea 15619 TRY
1042e4c0 15620 {
79a45e25 15621 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15622 }
492d29ea
PA
15623 CATCH (ex, RETURN_MASK_ALL)
15624 {
6c63c96a 15625 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15626 throw_exception (ex);
15627 }
15628 END_CATCH
1042e4c0 15629
6c63c96a 15630 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15631 fprintf_unfiltered (fp, " end\n");
1042e4c0 15632 }
6149aea9
PA
15633
15634 if (tp->enable_state == bp_disabled)
99894e11 15635 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15636
15637 /* If this is a multi-location breakpoint, check if the locations
15638 should be individually disabled. Watchpoint locations are
15639 special, and not user visible. */
15640 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15641 {
15642 struct bp_location *loc;
15643 int n = 1;
15644
15645 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15646 if (!loc->enabled)
15647 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15648 }
1042e4c0 15649 }
8bf6485c 15650
6149aea9 15651 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15652 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15653
1042e4c0 15654 if (from_tty)
6149aea9 15655 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15656 do_cleanups (cleanup);
6149aea9
PA
15657}
15658
15659/* The `save breakpoints' command. */
15660
15661static void
15662save_breakpoints_command (char *args, int from_tty)
15663{
15664 save_breakpoints (args, from_tty, NULL);
15665}
15666
15667/* The `save tracepoints' command. */
15668
15669static void
15670save_tracepoints_command (char *args, int from_tty)
15671{
15672 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15673}
15674
15675/* Create a vector of all tracepoints. */
15676
15677VEC(breakpoint_p) *
eeae04df 15678all_tracepoints (void)
1042e4c0
SS
15679{
15680 VEC(breakpoint_p) *tp_vec = 0;
15681 struct breakpoint *tp;
15682
15683 ALL_TRACEPOINTS (tp)
15684 {
15685 VEC_safe_push (breakpoint_p, tp_vec, tp);
15686 }
15687
15688 return tp_vec;
15689}
15690
c906108c 15691\f
629500fa
KS
15692/* This help string is used to consolidate all the help string for specifying
15693 locations used by several commands. */
15694
15695#define LOCATION_HELP_STRING \
15696"Linespecs are colon-separated lists of location parameters, such as\n\
15697source filename, function name, label name, and line number.\n\
15698Example: To specify the start of a label named \"the_top\" in the\n\
15699function \"fact\" in the file \"factorial.c\", use\n\
15700\"factorial.c:fact:the_top\".\n\
15701\n\
15702Address locations begin with \"*\" and specify an exact address in the\n\
15703program. Example: To specify the fourth byte past the start function\n\
15704\"main\", use \"*main + 4\".\n\
15705\n\
15706Explicit locations are similar to linespecs but use an option/argument\n\
15707syntax to specify location parameters.\n\
15708Example: To specify the start of the label named \"the_top\" in the\n\
15709function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15710-function fact -label the_top\".\n"
15711
4a64f543
MS
15712/* This help string is used for the break, hbreak, tbreak and thbreak
15713 commands. It is defined as a macro to prevent duplication.
15714 COMMAND should be a string constant containing the name of the
15715 command. */
629500fa 15716
31e2b00f 15717#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15718command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15719PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15720probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15721guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15722`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15723LOCATION may be a linespec, address, or explicit location as described\n\
15724below.\n\
15725\n\
dc10affe
PA
15726With no LOCATION, uses current execution address of the selected\n\
15727stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15728\n\
15729THREADNUM is the number from \"info threads\".\n\
15730CONDITION is a boolean expression.\n\
629500fa 15731\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15732Multiple breakpoints at one place are permitted, and useful if their\n\
15733conditions are different.\n\
31e2b00f
AS
15734\n\
15735Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15736
44feb3ce
TT
15737/* List of subcommands for "catch". */
15738static struct cmd_list_element *catch_cmdlist;
15739
15740/* List of subcommands for "tcatch". */
15741static struct cmd_list_element *tcatch_cmdlist;
15742
9ac4176b 15743void
44feb3ce 15744add_catch_command (char *name, char *docstring,
82ae6c8d 15745 cmd_sfunc_ftype *sfunc,
625e8578 15746 completer_ftype *completer,
44feb3ce
TT
15747 void *user_data_catch,
15748 void *user_data_tcatch)
15749{
15750 struct cmd_list_element *command;
15751
15752 command = add_cmd (name, class_breakpoint, NULL, docstring,
15753 &catch_cmdlist);
15754 set_cmd_sfunc (command, sfunc);
15755 set_cmd_context (command, user_data_catch);
a96d9b2e 15756 set_cmd_completer (command, completer);
44feb3ce
TT
15757
15758 command = add_cmd (name, class_breakpoint, NULL, docstring,
15759 &tcatch_cmdlist);
15760 set_cmd_sfunc (command, sfunc);
15761 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15762 set_cmd_completer (command, completer);
44feb3ce
TT
15763}
15764
6149aea9
PA
15765static void
15766save_command (char *arg, int from_tty)
15767{
3e43a32a
MS
15768 printf_unfiltered (_("\"save\" must be followed by "
15769 "the name of a save subcommand.\n"));
635c7e8a 15770 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15771}
15772
84f4c1fe
PM
15773struct breakpoint *
15774iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15775 void *data)
15776{
35df4500 15777 struct breakpoint *b, *b_tmp;
84f4c1fe 15778
35df4500 15779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15780 {
15781 if ((*callback) (b, data))
15782 return b;
15783 }
15784
15785 return NULL;
15786}
15787
0574c78f
GB
15788/* Zero if any of the breakpoint's locations could be a location where
15789 functions have been inlined, nonzero otherwise. */
15790
15791static int
15792is_non_inline_function (struct breakpoint *b)
15793{
15794 /* The shared library event breakpoint is set on the address of a
15795 non-inline function. */
15796 if (b->type == bp_shlib_event)
15797 return 1;
15798
15799 return 0;
15800}
15801
15802/* Nonzero if the specified PC cannot be a location where functions
15803 have been inlined. */
15804
15805int
09ac7c10
TT
15806pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15807 const struct target_waitstatus *ws)
0574c78f
GB
15808{
15809 struct breakpoint *b;
15810 struct bp_location *bl;
15811
15812 ALL_BREAKPOINTS (b)
15813 {
15814 if (!is_non_inline_function (b))
15815 continue;
15816
15817 for (bl = b->loc; bl != NULL; bl = bl->next)
15818 {
15819 if (!bl->shlib_disabled
09ac7c10 15820 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15821 return 1;
15822 }
15823 }
15824
15825 return 0;
15826}
15827
2f202fde
JK
15828/* Remove any references to OBJFILE which is going to be freed. */
15829
15830void
15831breakpoint_free_objfile (struct objfile *objfile)
15832{
15833 struct bp_location **locp, *loc;
15834
15835 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15836 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15837 loc->symtab = NULL;
15838}
15839
2060206e
PA
15840void
15841initialize_breakpoint_ops (void)
15842{
15843 static int initialized = 0;
15844
15845 struct breakpoint_ops *ops;
15846
15847 if (initialized)
15848 return;
15849 initialized = 1;
15850
15851 /* The breakpoint_ops structure to be inherit by all kinds of
15852 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15853 internal and momentary breakpoints, etc.). */
15854 ops = &bkpt_base_breakpoint_ops;
15855 *ops = base_breakpoint_ops;
15856 ops->re_set = bkpt_re_set;
15857 ops->insert_location = bkpt_insert_location;
15858 ops->remove_location = bkpt_remove_location;
15859 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15860 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15861 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15862 ops->decode_location = bkpt_decode_location;
2060206e
PA
15863
15864 /* The breakpoint_ops structure to be used in regular breakpoints. */
15865 ops = &bkpt_breakpoint_ops;
15866 *ops = bkpt_base_breakpoint_ops;
15867 ops->re_set = bkpt_re_set;
15868 ops->resources_needed = bkpt_resources_needed;
15869 ops->print_it = bkpt_print_it;
15870 ops->print_mention = bkpt_print_mention;
15871 ops->print_recreate = bkpt_print_recreate;
15872
15873 /* Ranged breakpoints. */
15874 ops = &ranged_breakpoint_ops;
15875 *ops = bkpt_breakpoint_ops;
15876 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15877 ops->resources_needed = resources_needed_ranged_breakpoint;
15878 ops->print_it = print_it_ranged_breakpoint;
15879 ops->print_one = print_one_ranged_breakpoint;
15880 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15881 ops->print_mention = print_mention_ranged_breakpoint;
15882 ops->print_recreate = print_recreate_ranged_breakpoint;
15883
15884 /* Internal breakpoints. */
15885 ops = &internal_breakpoint_ops;
15886 *ops = bkpt_base_breakpoint_ops;
15887 ops->re_set = internal_bkpt_re_set;
15888 ops->check_status = internal_bkpt_check_status;
15889 ops->print_it = internal_bkpt_print_it;
15890 ops->print_mention = internal_bkpt_print_mention;
15891
15892 /* Momentary breakpoints. */
15893 ops = &momentary_breakpoint_ops;
15894 *ops = bkpt_base_breakpoint_ops;
15895 ops->re_set = momentary_bkpt_re_set;
15896 ops->check_status = momentary_bkpt_check_status;
15897 ops->print_it = momentary_bkpt_print_it;
15898 ops->print_mention = momentary_bkpt_print_mention;
15899
e2e4d78b
JK
15900 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15901 ops = &longjmp_breakpoint_ops;
15902 *ops = momentary_breakpoint_ops;
15903 ops->dtor = longjmp_bkpt_dtor;
15904
55aa24fb
SDJ
15905 /* Probe breakpoints. */
15906 ops = &bkpt_probe_breakpoint_ops;
15907 *ops = bkpt_breakpoint_ops;
15908 ops->insert_location = bkpt_probe_insert_location;
15909 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15910 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15911 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15912
2060206e
PA
15913 /* Watchpoints. */
15914 ops = &watchpoint_breakpoint_ops;
15915 *ops = base_breakpoint_ops;
3a5c3e22 15916 ops->dtor = dtor_watchpoint;
2060206e
PA
15917 ops->re_set = re_set_watchpoint;
15918 ops->insert_location = insert_watchpoint;
15919 ops->remove_location = remove_watchpoint;
15920 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15921 ops->check_status = check_status_watchpoint;
15922 ops->resources_needed = resources_needed_watchpoint;
15923 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15924 ops->print_it = print_it_watchpoint;
15925 ops->print_mention = print_mention_watchpoint;
15926 ops->print_recreate = print_recreate_watchpoint;
427cd150 15927 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15928
15929 /* Masked watchpoints. */
15930 ops = &masked_watchpoint_breakpoint_ops;
15931 *ops = watchpoint_breakpoint_ops;
15932 ops->insert_location = insert_masked_watchpoint;
15933 ops->remove_location = remove_masked_watchpoint;
15934 ops->resources_needed = resources_needed_masked_watchpoint;
15935 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15936 ops->print_it = print_it_masked_watchpoint;
15937 ops->print_one_detail = print_one_detail_masked_watchpoint;
15938 ops->print_mention = print_mention_masked_watchpoint;
15939 ops->print_recreate = print_recreate_masked_watchpoint;
15940
15941 /* Tracepoints. */
15942 ops = &tracepoint_breakpoint_ops;
15943 *ops = base_breakpoint_ops;
15944 ops->re_set = tracepoint_re_set;
15945 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15946 ops->print_one_detail = tracepoint_print_one_detail;
15947 ops->print_mention = tracepoint_print_mention;
15948 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15949 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15950 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15951 ops->decode_location = tracepoint_decode_location;
983af33b 15952
55aa24fb
SDJ
15953 /* Probe tracepoints. */
15954 ops = &tracepoint_probe_breakpoint_ops;
15955 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15956 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15957 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15958
983af33b
SDJ
15959 /* Static tracepoints with marker (`-m'). */
15960 ops = &strace_marker_breakpoint_ops;
15961 *ops = tracepoint_breakpoint_ops;
5f700d83 15962 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15963 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15964 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15965
15966 /* Fork catchpoints. */
15967 ops = &catch_fork_breakpoint_ops;
15968 *ops = base_breakpoint_ops;
15969 ops->insert_location = insert_catch_fork;
15970 ops->remove_location = remove_catch_fork;
15971 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15972 ops->print_it = print_it_catch_fork;
15973 ops->print_one = print_one_catch_fork;
15974 ops->print_mention = print_mention_catch_fork;
15975 ops->print_recreate = print_recreate_catch_fork;
15976
15977 /* Vfork catchpoints. */
15978 ops = &catch_vfork_breakpoint_ops;
15979 *ops = base_breakpoint_ops;
15980 ops->insert_location = insert_catch_vfork;
15981 ops->remove_location = remove_catch_vfork;
15982 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15983 ops->print_it = print_it_catch_vfork;
15984 ops->print_one = print_one_catch_vfork;
15985 ops->print_mention = print_mention_catch_vfork;
15986 ops->print_recreate = print_recreate_catch_vfork;
15987
15988 /* Exec catchpoints. */
15989 ops = &catch_exec_breakpoint_ops;
15990 *ops = base_breakpoint_ops;
15991 ops->dtor = dtor_catch_exec;
15992 ops->insert_location = insert_catch_exec;
15993 ops->remove_location = remove_catch_exec;
15994 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15995 ops->print_it = print_it_catch_exec;
15996 ops->print_one = print_one_catch_exec;
15997 ops->print_mention = print_mention_catch_exec;
15998 ops->print_recreate = print_recreate_catch_exec;
15999
edcc5120
TT
16000 /* Solib-related catchpoints. */
16001 ops = &catch_solib_breakpoint_ops;
16002 *ops = base_breakpoint_ops;
16003 ops->dtor = dtor_catch_solib;
16004 ops->insert_location = insert_catch_solib;
16005 ops->remove_location = remove_catch_solib;
16006 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16007 ops->check_status = check_status_catch_solib;
16008 ops->print_it = print_it_catch_solib;
16009 ops->print_one = print_one_catch_solib;
16010 ops->print_mention = print_mention_catch_solib;
16011 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16012
16013 ops = &dprintf_breakpoint_ops;
16014 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16015 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16016 ops->resources_needed = bkpt_resources_needed;
16017 ops->print_it = bkpt_print_it;
16018 ops->print_mention = bkpt_print_mention;
2d9442cc 16019 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16020 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16021 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16022}
16023
8bfd80db
YQ
16024/* Chain containing all defined "enable breakpoint" subcommands. */
16025
16026static struct cmd_list_element *enablebreaklist = NULL;
16027
c906108c 16028void
fba45db2 16029_initialize_breakpoint (void)
c906108c
SS
16030{
16031 struct cmd_list_element *c;
16032
2060206e
PA
16033 initialize_breakpoint_ops ();
16034
84acb35a 16035 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16036 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16037 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16038
55aa24fb
SDJ
16039 breakpoint_objfile_key
16040 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16041
c906108c
SS
16042 breakpoint_chain = 0;
16043 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16044 before a breakpoint is set. */
16045 breakpoint_count = 0;
16046
1042e4c0
SS
16047 tracepoint_count = 0;
16048
1bedd215
AC
16049 add_com ("ignore", class_breakpoint, ignore_command, _("\
16050Set ignore-count of breakpoint number N to COUNT.\n\
16051Usage is `ignore N COUNT'."));
c906108c 16052
1bedd215
AC
16053 add_com ("commands", class_breakpoint, commands_command, _("\
16054Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16055Give breakpoint number as argument after \"commands\".\n\
16056With no argument, the targeted breakpoint is the last one set.\n\
16057The commands themselves follow starting on the next line.\n\
16058Type a line containing \"end\" to indicate the end of them.\n\
16059Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16060then no output is printed when it is hit, except what the commands print."));
c906108c 16061
d55637df 16062 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16063Specify breakpoint number N to break only if COND is true.\n\
c906108c 16064Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16065expression to be evaluated whenever breakpoint N is reached."));
d55637df 16066 set_cmd_completer (c, condition_completer);
c906108c 16067
1bedd215 16068 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16069Set a temporary breakpoint.\n\
c906108c
SS
16070Like \"break\" except the breakpoint is only temporary,\n\
16071so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16072by using \"enable delete\" on the breakpoint number.\n\
16073\n"
16074BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16075 set_cmd_completer (c, location_completer);
c94fdfd0 16076
1bedd215 16077 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16078Set a hardware assisted breakpoint.\n\
c906108c 16079Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16080some target hardware may not have this support.\n\
16081\n"
16082BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16083 set_cmd_completer (c, location_completer);
c906108c 16084
1bedd215 16085 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16086Set a temporary hardware assisted breakpoint.\n\
c906108c 16087Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16088so it will be deleted when hit.\n\
16089\n"
16090BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16091 set_cmd_completer (c, location_completer);
c906108c 16092
1bedd215
AC
16093 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16094Enable some breakpoints.\n\
c906108c
SS
16095Give breakpoint numbers (separated by spaces) as arguments.\n\
16096With no subcommand, breakpoints are enabled until you command otherwise.\n\
16097This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16098With a subcommand you can enable temporarily."),
c906108c 16099 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16100
16101 add_com_alias ("en", "enable", class_breakpoint, 1);
16102
84951ab5 16103 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16104Enable some breakpoints.\n\
c906108c
SS
16105Give breakpoint numbers (separated by spaces) as arguments.\n\
16106This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16107May be abbreviated to simply \"enable\".\n"),
c5aa993b 16108 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16109
1a966eab
AC
16110 add_cmd ("once", no_class, enable_once_command, _("\
16111Enable breakpoints for one hit. Give breakpoint numbers.\n\
16112If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16113 &enablebreaklist);
16114
1a966eab
AC
16115 add_cmd ("delete", no_class, enable_delete_command, _("\
16116Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16117If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16118 &enablebreaklist);
16119
816338b5
SS
16120 add_cmd ("count", no_class, enable_count_command, _("\
16121Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16122If a breakpoint is hit while enabled in this fashion,\n\
16123the count is decremented; when it reaches zero, the breakpoint is disabled."),
16124 &enablebreaklist);
16125
1a966eab
AC
16126 add_cmd ("delete", no_class, enable_delete_command, _("\
16127Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16128If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16129 &enablelist);
16130
1a966eab
AC
16131 add_cmd ("once", no_class, enable_once_command, _("\
16132Enable breakpoints for one hit. Give breakpoint numbers.\n\
16133If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16134 &enablelist);
16135
16136 add_cmd ("count", no_class, enable_count_command, _("\
16137Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16138If a breakpoint is hit while enabled in this fashion,\n\
16139the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16140 &enablelist);
16141
1bedd215
AC
16142 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16143Disable some breakpoints.\n\
c906108c
SS
16144Arguments are breakpoint numbers with spaces in between.\n\
16145To disable all breakpoints, give no argument.\n\
64b9b334 16146A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16147 &disablelist, "disable ", 1, &cmdlist);
16148 add_com_alias ("dis", "disable", class_breakpoint, 1);
16149 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16150
1a966eab
AC
16151 add_cmd ("breakpoints", class_alias, disable_command, _("\
16152Disable some breakpoints.\n\
c906108c
SS
16153Arguments are breakpoint numbers with spaces in between.\n\
16154To disable all breakpoints, give no argument.\n\
64b9b334 16155A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16156This command may be abbreviated \"disable\"."),
c906108c
SS
16157 &disablelist);
16158
1bedd215
AC
16159 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16160Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16161Arguments are breakpoint numbers with spaces in between.\n\
16162To delete all breakpoints, give no argument.\n\
16163\n\
16164Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16165The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16166 &deletelist, "delete ", 1, &cmdlist);
16167 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16168 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16169
1a966eab
AC
16170 add_cmd ("breakpoints", class_alias, delete_command, _("\
16171Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16172Arguments are breakpoint numbers with spaces in between.\n\
16173To delete all breakpoints, give no argument.\n\
1a966eab 16174This command may be abbreviated \"delete\"."),
c906108c
SS
16175 &deletelist);
16176
1bedd215 16177 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16178Clear breakpoint at specified location.\n\
16179Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16180\n\
16181With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16182is executing in.\n"
16183"\n" LOCATION_HELP_STRING "\n\
1bedd215 16184See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16185 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16186
1bedd215 16187 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16188Set breakpoint at specified location.\n"
31e2b00f 16189BREAK_ARGS_HELP ("break")));
5ba2abeb 16190 set_cmd_completer (c, location_completer);
c94fdfd0 16191
c906108c
SS
16192 add_com_alias ("b", "break", class_run, 1);
16193 add_com_alias ("br", "break", class_run, 1);
16194 add_com_alias ("bre", "break", class_run, 1);
16195 add_com_alias ("brea", "break", class_run, 1);
16196
c906108c
SS
16197 if (dbx_commands)
16198 {
1bedd215
AC
16199 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16200Break in function/address or break at a line in the current file."),
c5aa993b
JM
16201 &stoplist, "stop ", 1, &cmdlist);
16202 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16203 _("Break in function or address."), &stoplist);
c5aa993b 16204 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16205 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16206 add_com ("status", class_info, breakpoints_info, _("\
16207Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16208The \"Type\" column indicates one of:\n\
16209\tbreakpoint - normal breakpoint\n\
16210\twatchpoint - watchpoint\n\
16211The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16212the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16213breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16214address and file/line number respectively.\n\
16215\n\
16216Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16217are set to the address of the last breakpoint listed unless the command\n\
16218is prefixed with \"server \".\n\n\
c906108c 16219Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16220breakpoint set."));
c906108c
SS
16221 }
16222
1bedd215 16223 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16224Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16225The \"Type\" column indicates one of:\n\
16226\tbreakpoint - normal breakpoint\n\
16227\twatchpoint - watchpoint\n\
16228The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16229the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16230breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16231address and file/line number respectively.\n\
16232\n\
16233Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16234are set to the address of the last breakpoint listed unless the command\n\
16235is prefixed with \"server \".\n\n\
c906108c 16236Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16237breakpoint set."));
c906108c 16238
6b04bdb7
MS
16239 add_info_alias ("b", "breakpoints", 1);
16240
1a966eab
AC
16241 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16242Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16243The \"Type\" column indicates one of:\n\
16244\tbreakpoint - normal breakpoint\n\
16245\twatchpoint - watchpoint\n\
16246\tlongjmp - internal breakpoint used to step through longjmp()\n\
16247\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16248\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16249\tfinish - internal breakpoint used by the \"finish\" command\n\
16250The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16251the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16252breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16253address and file/line number respectively.\n\
16254\n\
16255Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16256are set to the address of the last breakpoint listed unless the command\n\
16257is prefixed with \"server \".\n\n\
c906108c 16258Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16259breakpoint set."),
c906108c
SS
16260 &maintenanceinfolist);
16261
44feb3ce
TT
16262 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16263Set catchpoints to catch events."),
16264 &catch_cmdlist, "catch ",
16265 0/*allow-unknown*/, &cmdlist);
16266
16267 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16268Set temporary catchpoints to catch events."),
16269 &tcatch_cmdlist, "tcatch ",
16270 0/*allow-unknown*/, &cmdlist);
16271
44feb3ce
TT
16272 add_catch_command ("fork", _("Catch calls to fork."),
16273 catch_fork_command_1,
a96d9b2e 16274 NULL,
44feb3ce
TT
16275 (void *) (uintptr_t) catch_fork_permanent,
16276 (void *) (uintptr_t) catch_fork_temporary);
16277 add_catch_command ("vfork", _("Catch calls to vfork."),
16278 catch_fork_command_1,
a96d9b2e 16279 NULL,
44feb3ce
TT
16280 (void *) (uintptr_t) catch_vfork_permanent,
16281 (void *) (uintptr_t) catch_vfork_temporary);
16282 add_catch_command ("exec", _("Catch calls to exec."),
16283 catch_exec_command_1,
a96d9b2e
SDJ
16284 NULL,
16285 CATCH_PERMANENT,
16286 CATCH_TEMPORARY);
edcc5120
TT
16287 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16288Usage: catch load [REGEX]\n\
16289If REGEX is given, only stop for libraries matching the regular expression."),
16290 catch_load_command_1,
16291 NULL,
16292 CATCH_PERMANENT,
16293 CATCH_TEMPORARY);
16294 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16295Usage: catch unload [REGEX]\n\
16296If REGEX is given, only stop for libraries matching the regular expression."),
16297 catch_unload_command_1,
16298 NULL,
16299 CATCH_PERMANENT,
16300 CATCH_TEMPORARY);
c5aa993b 16301
1bedd215
AC
16302 c = add_com ("watch", class_breakpoint, watch_command, _("\
16303Set a watchpoint for an expression.\n\
06a64a0b 16304Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16305A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16306an expression changes.\n\
16307If -l or -location is given, this evaluates EXPRESSION and watches\n\
16308the memory to which it refers."));
65d12d83 16309 set_cmd_completer (c, expression_completer);
c906108c 16310
1bedd215
AC
16311 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16312Set a read watchpoint for an expression.\n\
06a64a0b 16313Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16314A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16315an expression is read.\n\
16316If -l or -location is given, this evaluates EXPRESSION and watches\n\
16317the memory to which it refers."));
65d12d83 16318 set_cmd_completer (c, expression_completer);
c906108c 16319
1bedd215
AC
16320 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16321Set a watchpoint for an expression.\n\
06a64a0b 16322Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16323A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16324an expression is either read or written.\n\
16325If -l or -location is given, this evaluates EXPRESSION and watches\n\
16326the memory to which it refers."));
65d12d83 16327 set_cmd_completer (c, expression_completer);
c906108c 16328
d77f58be 16329 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16330Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16331
920d2a44
AC
16332 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16333 respond to changes - contrary to the description. */
85c07804
AC
16334 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16335 &can_use_hw_watchpoints, _("\
16336Set debugger's willingness to use watchpoint hardware."), _("\
16337Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16338If zero, gdb will not use hardware for new watchpoints, even if\n\
16339such is available. (However, any hardware watchpoints that were\n\
16340created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16341hardware.)"),
16342 NULL,
920d2a44 16343 show_can_use_hw_watchpoints,
85c07804 16344 &setlist, &showlist);
c906108c
SS
16345
16346 can_use_hw_watchpoints = 1;
fa8d40ab 16347
1042e4c0
SS
16348 /* Tracepoint manipulation commands. */
16349
16350 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16351Set a tracepoint at specified location.\n\
1042e4c0
SS
16352\n"
16353BREAK_ARGS_HELP ("trace") "\n\
16354Do \"help tracepoints\" for info on other tracepoint commands."));
16355 set_cmd_completer (c, location_completer);
16356
16357 add_com_alias ("tp", "trace", class_alias, 0);
16358 add_com_alias ("tr", "trace", class_alias, 1);
16359 add_com_alias ("tra", "trace", class_alias, 1);
16360 add_com_alias ("trac", "trace", class_alias, 1);
16361
7a697b8d 16362 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16363Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16364\n"
16365BREAK_ARGS_HELP ("ftrace") "\n\
16366Do \"help tracepoints\" for info on other tracepoint commands."));
16367 set_cmd_completer (c, location_completer);
16368
0fb4aa4b 16369 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16370Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16371\n\
16372strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16373LOCATION may be a linespec, explicit, or address location (described below) \n\
16374or -m MARKER_ID.\n\n\
16375If a marker id is specified, probe the marker with that name. With\n\
16376no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16377Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16378This collects arbitrary user data passed in the probe point call to the\n\
16379tracing library. You can inspect it when analyzing the trace buffer,\n\
16380by printing the $_sdata variable like any other convenience variable.\n\
16381\n\
16382CONDITION is a boolean expression.\n\
629500fa 16383\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16384Multiple tracepoints at one place are permitted, and useful if their\n\
16385conditions are different.\n\
0fb4aa4b
PA
16386\n\
16387Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16388Do \"help tracepoints\" for info on other tracepoint commands."));
16389 set_cmd_completer (c, location_completer);
16390
1042e4c0 16391 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16392Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16393Convenience variable \"$tpnum\" contains the number of the\n\
16394last tracepoint set."));
16395
16396 add_info_alias ("tp", "tracepoints", 1);
16397
16398 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16399Delete specified tracepoints.\n\
16400Arguments are tracepoint numbers, separated by spaces.\n\
16401No argument means delete all tracepoints."),
16402 &deletelist);
7e20dfcd 16403 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16404
16405 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16406Disable specified tracepoints.\n\
16407Arguments are tracepoint numbers, separated by spaces.\n\
16408No argument means disable all tracepoints."),
16409 &disablelist);
16410 deprecate_cmd (c, "disable");
16411
16412 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16413Enable specified tracepoints.\n\
16414Arguments are tracepoint numbers, separated by spaces.\n\
16415No argument means enable all tracepoints."),
16416 &enablelist);
16417 deprecate_cmd (c, "enable");
16418
16419 add_com ("passcount", class_trace, trace_pass_command, _("\
16420Set the passcount for a tracepoint.\n\
16421The trace will end when the tracepoint has been passed 'count' times.\n\
16422Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16423if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16424
6149aea9
PA
16425 add_prefix_cmd ("save", class_breakpoint, save_command,
16426 _("Save breakpoint definitions as a script."),
16427 &save_cmdlist, "save ",
16428 0/*allow-unknown*/, &cmdlist);
16429
16430 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16431Save current breakpoint definitions as a script.\n\
cce7e648 16432This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16433catchpoints, tracepoints). Use the 'source' command in another debug\n\
16434session to restore them."),
16435 &save_cmdlist);
16436 set_cmd_completer (c, filename_completer);
16437
16438 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16439Save current tracepoint definitions as a script.\n\
6149aea9
PA
16440Use the 'source' command in another debug session to restore them."),
16441 &save_cmdlist);
1042e4c0
SS
16442 set_cmd_completer (c, filename_completer);
16443
6149aea9
PA
16444 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16445 deprecate_cmd (c, "save tracepoints");
16446
1bedd215 16447 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16448Breakpoint specific settings\n\
16449Configure various breakpoint-specific variables such as\n\
1bedd215 16450pending breakpoint behavior"),
fa8d40ab
JJ
16451 &breakpoint_set_cmdlist, "set breakpoint ",
16452 0/*allow-unknown*/, &setlist);
1bedd215 16453 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16454Breakpoint specific settings\n\
16455Configure various breakpoint-specific variables such as\n\
1bedd215 16456pending breakpoint behavior"),
fa8d40ab
JJ
16457 &breakpoint_show_cmdlist, "show breakpoint ",
16458 0/*allow-unknown*/, &showlist);
16459
7915a72c
AC
16460 add_setshow_auto_boolean_cmd ("pending", no_class,
16461 &pending_break_support, _("\
16462Set debugger's behavior regarding pending breakpoints."), _("\
16463Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16464If on, an unrecognized breakpoint location will cause gdb to create a\n\
16465pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16466an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16467user-query to see if a pending breakpoint should be created."),
2c5b56ce 16468 NULL,
920d2a44 16469 show_pending_break_support,
6e1d7d6c
AC
16470 &breakpoint_set_cmdlist,
16471 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16472
16473 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16474
16475 add_setshow_boolean_cmd ("auto-hw", no_class,
16476 &automatic_hardware_breakpoints, _("\
16477Set automatic usage of hardware breakpoints."), _("\
16478Show automatic usage of hardware breakpoints."), _("\
16479If set, the debugger will automatically use hardware breakpoints for\n\
16480breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16481a warning will be emitted for such breakpoints."),
16482 NULL,
16483 show_automatic_hardware_breakpoints,
16484 &breakpoint_set_cmdlist,
16485 &breakpoint_show_cmdlist);
74960c60 16486
a25a5a45
PA
16487 add_setshow_boolean_cmd ("always-inserted", class_support,
16488 &always_inserted_mode, _("\
74960c60
VP
16489Set mode for inserting breakpoints."), _("\
16490Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16491When this mode is on, breakpoints are inserted immediately as soon as\n\
16492they're created, kept inserted even when execution stops, and removed\n\
16493only when the user deletes them. When this mode is off (the default),\n\
16494breakpoints are inserted only when execution continues, and removed\n\
16495when execution stops."),
72d0e2c5
YQ
16496 NULL,
16497 &show_always_inserted_mode,
16498 &breakpoint_set_cmdlist,
16499 &breakpoint_show_cmdlist);
f1310107 16500
b775012e
LM
16501 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16502 condition_evaluation_enums,
16503 &condition_evaluation_mode_1, _("\
16504Set mode of breakpoint condition evaluation."), _("\
16505Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16506When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16507evaluated on the host's side by GDB. When it is set to \"target\",\n\
16508breakpoint conditions will be downloaded to the target (if the target\n\
16509supports such feature) and conditions will be evaluated on the target's side.\n\
16510If this is set to \"auto\" (default), this will be automatically set to\n\
16511\"target\" if it supports condition evaluation, otherwise it will\n\
16512be set to \"gdb\""),
16513 &set_condition_evaluation_mode,
16514 &show_condition_evaluation_mode,
16515 &breakpoint_set_cmdlist,
16516 &breakpoint_show_cmdlist);
16517
f1310107
TJB
16518 add_com ("break-range", class_breakpoint, break_range_command, _("\
16519Set a breakpoint for an address range.\n\
16520break-range START-LOCATION, END-LOCATION\n\
16521where START-LOCATION and END-LOCATION can be one of the following:\n\
16522 LINENUM, for that line in the current file,\n\
16523 FILE:LINENUM, for that line in that file,\n\
16524 +OFFSET, for that number of lines after the current line\n\
16525 or the start of the range\n\
16526 FUNCTION, for the first line in that function,\n\
16527 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16528 *ADDRESS, for the instruction at that address.\n\
16529\n\
16530The breakpoint will stop execution of the inferior whenever it executes\n\
16531an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16532range (including START-LOCATION and END-LOCATION)."));
16533
e7e0cddf 16534 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16535Set a dynamic printf at specified location.\n\
e7e0cddf 16536dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16537location may be a linespec, explicit, or address location.\n"
16538"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16539 set_cmd_completer (c, location_completer);
16540
16541 add_setshow_enum_cmd ("dprintf-style", class_support,
16542 dprintf_style_enums, &dprintf_style, _("\
16543Set the style of usage for dynamic printf."), _("\
16544Show the style of usage for dynamic printf."), _("\
16545This setting chooses how GDB will do a dynamic printf.\n\
16546If the value is \"gdb\", then the printing is done by GDB to its own\n\
16547console, as with the \"printf\" command.\n\
16548If the value is \"call\", the print is done by calling a function in your\n\
16549program; by default printf(), but you can choose a different function or\n\
16550output stream by setting dprintf-function and dprintf-channel."),
16551 update_dprintf_commands, NULL,
16552 &setlist, &showlist);
16553
16554 dprintf_function = xstrdup ("printf");
16555 add_setshow_string_cmd ("dprintf-function", class_support,
16556 &dprintf_function, _("\
16557Set the function to use for dynamic printf"), _("\
16558Show the function to use for dynamic printf"), NULL,
16559 update_dprintf_commands, NULL,
16560 &setlist, &showlist);
16561
16562 dprintf_channel = xstrdup ("");
16563 add_setshow_string_cmd ("dprintf-channel", class_support,
16564 &dprintf_channel, _("\
16565Set the channel to use for dynamic printf"), _("\
16566Show the channel to use for dynamic printf"), NULL,
16567 update_dprintf_commands, NULL,
16568 &setlist, &showlist);
16569
d3ce09f5
SS
16570 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16571 &disconnected_dprintf, _("\
16572Set whether dprintf continues after GDB disconnects."), _("\
16573Show whether dprintf continues after GDB disconnects."), _("\
16574Use this to let dprintf commands continue to hit and produce output\n\
16575even if GDB disconnects or detaches from the target."),
16576 NULL,
16577 NULL,
16578 &setlist, &showlist);
16579
16580 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16581agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16582(target agent only) This is useful for formatted output in user-defined commands."));
16583
765dc015 16584 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16585
16586 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16587 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16588}