This commit fixes a regression introduced by the new $_isvoid convenience
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
28e7fd62 3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
38#include "gdb_string.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
edcc5120 69#include "gdb_regex.h"
b775012e 70#include "ax-gdb.h"
e2e4d78b 71#include "dummy-frame.h"
c906108c 72
d3ce09f5
SS
73#include "format.h"
74
1042e4c0
SS
75/* readline include files */
76#include "readline/readline.h"
77#include "readline/history.h"
78
79/* readline defines this. */
80#undef savestring
81
034dad6f 82#include "mi/mi-common.h"
7371cf6d 83#include "python/python.h"
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
95a42b64
TT
105static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
c906108c 108
a14ed312 109static void ignore_command (char *, int);
c906108c 110
4efb68b1 111static int breakpoint_re_set_one (void *);
c906108c 112
348d480f
PA
113static void breakpoint_re_set_default (struct breakpoint *);
114
983af33b
SDJ
115static void create_sals_from_address_default (char **,
116 struct linespec_result *,
117 enum bptype, char *,
118 char **);
119
120static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
e7e0cddf 122 char *, char *, enum bptype,
983af33b
SDJ
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
44f238bb 126 int, int, int, unsigned);
983af33b
SDJ
127
128static void decode_linespec_default (struct breakpoint *, char **,
129 struct symtabs_and_lines *);
130
a14ed312 131static void clear_command (char *, int);
c906108c 132
a14ed312 133static void catch_command (char *, int);
c906108c 134
a9634178 135static int can_use_hardware_watchpoint (struct value *);
c906108c 136
98deb0da 137static void break_command_1 (char *, int, int);
c906108c 138
a14ed312 139static void mention (struct breakpoint *);
c906108c 140
348d480f
PA
141static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
c0a91b2b 143 const struct breakpoint_ops *);
3742cc8b
YQ
144static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
4a64f543
MS
147/* This function is used in gdbtk sources and thus can not be made
148 static. */
63c252f8 149struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 150 struct symtab_and_line,
c0a91b2b
TT
151 enum bptype,
152 const struct breakpoint_ops *);
c906108c 153
06edf0c0
PA
154static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
c0a91b2b 157 const struct breakpoint_ops *ops);
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
6c95b8df
PA
169static int breakpoint_address_match (struct address_space *aspace1,
170 CORE_ADDR addr1,
171 struct address_space *aspace2,
172 CORE_ADDR addr2);
173
85d721b8
PA
174static int watchpoint_locations_match (struct bp_location *loc1,
175 struct bp_location *loc2);
176
f1310107
TJB
177static int breakpoint_location_address_match (struct bp_location *bl,
178 struct address_space *aspace,
179 CORE_ADDR addr);
180
a14ed312 181static void breakpoints_info (char *, int);
c906108c 182
d77f58be
SS
183static void watchpoints_info (char *, int);
184
e5a67952
MS
185static int breakpoint_1 (char *, int,
186 int (*) (const struct breakpoint *));
c906108c 187
4efb68b1 188static int breakpoint_cond_eval (void *);
c906108c 189
4efb68b1 190static void cleanup_executing_breakpoints (void *);
c906108c 191
a14ed312 192static void commands_command (char *, int);
c906108c 193
a14ed312 194static void condition_command (char *, int);
c906108c 195
c5aa993b
JM
196typedef enum
197 {
198 mark_inserted,
199 mark_uninserted
200 }
201insertion_state_t;
c906108c 202
0bde7532 203static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 204static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 205
e514a9d6 206static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 207
4efb68b1 208static int watchpoint_check (void *);
c906108c 209
a14ed312 210static void maintenance_info_breakpoints (char *, int);
c906108c 211
a14ed312 212static int hw_breakpoint_used_count (void);
c906108c 213
a1398e0c
PA
214static int hw_watchpoint_use_count (struct breakpoint *);
215
216static int hw_watchpoint_used_count_others (struct breakpoint *except,
217 enum bptype type,
218 int *other_type_used);
c906108c 219
a14ed312 220static void hbreak_command (char *, int);
c906108c 221
a14ed312 222static void thbreak_command (char *, int);
c906108c 223
816338b5
SS
224static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
225 int count);
c906108c 226
a14ed312 227static void stop_command (char *arg, int from_tty);
7a292a7a 228
a14ed312 229static void stopin_command (char *arg, int from_tty);
7a292a7a 230
a14ed312 231static void stopat_command (char *arg, int from_tty);
7a292a7a 232
a14ed312 233static void tcatch_command (char *arg, int from_tty);
7a292a7a 234
d03285ec
UW
235static void detach_single_step_breakpoints (void);
236
6c95b8df
PA
237static int single_step_breakpoint_inserted_here_p (struct address_space *,
238 CORE_ADDR pc);
1aafd4da 239
fe3f5fa8 240static void free_bp_location (struct bp_location *loc);
f431efe5
PA
241static void incref_bp_location (struct bp_location *loc);
242static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 243
39d61571 244static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 245
b60e7edf 246static void update_global_location_list (int);
a5606eee 247
b60e7edf 248static void update_global_location_list_nothrow (int);
74960c60 249
d77f58be 250static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
251
252static void insert_breakpoint_locations (void);
a5606eee 253
a96d9b2e
SDJ
254static int syscall_catchpoint_p (struct breakpoint *b);
255
1042e4c0
SS
256static void tracepoints_info (char *, int);
257
258static void delete_trace_command (char *, int);
259
260static void enable_trace_command (char *, int);
261
262static void disable_trace_command (char *, int);
263
264static void trace_pass_command (char *, int);
265
558a9d82
YQ
266static void set_tracepoint_count (int num);
267
9c06b0b4
TJB
268static int is_masked_watchpoint (const struct breakpoint *b);
269
b775012e
LM
270static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
271
983af33b
SDJ
272/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
273 otherwise. */
274
275static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 276
2060206e
PA
277/* The abstract base class all breakpoint_ops structures inherit
278 from. */
ab04a2af 279struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
280
281/* The breakpoint_ops structure to be inherited by all breakpoint_ops
282 that are implemented on top of software or hardware breakpoints
283 (user breakpoints, internal and momentary breakpoints, etc.). */
284static struct breakpoint_ops bkpt_base_breakpoint_ops;
285
286/* Internal breakpoints class type. */
06edf0c0 287static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
288
289/* Momentary breakpoints class type. */
06edf0c0
PA
290static struct breakpoint_ops momentary_breakpoint_ops;
291
e2e4d78b
JK
292/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
293static struct breakpoint_ops longjmp_breakpoint_ops;
294
2060206e
PA
295/* The breakpoint_ops structure to be used in regular user created
296 breakpoints. */
297struct breakpoint_ops bkpt_breakpoint_ops;
298
55aa24fb
SDJ
299/* Breakpoints set on probes. */
300static struct breakpoint_ops bkpt_probe_breakpoint_ops;
301
e7e0cddf 302/* Dynamic printf class type. */
c5867ab6 303struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 304
d3ce09f5
SS
305/* The style in which to perform a dynamic printf. This is a user
306 option because different output options have different tradeoffs;
307 if GDB does the printing, there is better error handling if there
308 is a problem with any of the arguments, but using an inferior
309 function lets you have special-purpose printers and sending of
310 output to the same place as compiled-in print functions. */
311
312static const char dprintf_style_gdb[] = "gdb";
313static const char dprintf_style_call[] = "call";
314static const char dprintf_style_agent[] = "agent";
315static const char *const dprintf_style_enums[] = {
316 dprintf_style_gdb,
317 dprintf_style_call,
318 dprintf_style_agent,
319 NULL
320};
321static const char *dprintf_style = dprintf_style_gdb;
322
323/* The function to use for dynamic printf if the preferred style is to
324 call into the inferior. The value is simply a string that is
325 copied into the command, so it can be anything that GDB can
326 evaluate to a callable address, not necessarily a function name. */
327
328static char *dprintf_function = "";
329
330/* The channel to use for dynamic printf if the preferred style is to
331 call into the inferior; if a nonempty string, it will be passed to
332 the call as the first argument, with the format string as the
333 second. As with the dprintf function, this can be anything that
334 GDB knows how to evaluate, so in addition to common choices like
335 "stderr", this could be an app-specific expression like
336 "mystreams[curlogger]". */
337
338static char *dprintf_channel = "";
339
340/* True if dprintf commands should continue to operate even if GDB
341 has disconnected. */
342static int disconnected_dprintf = 1;
343
5cea2a26
PA
344/* A reference-counted struct command_line. This lets multiple
345 breakpoints share a single command list. */
346struct counted_command_line
347{
348 /* The reference count. */
349 int refc;
350
351 /* The command list. */
352 struct command_line *commands;
353};
354
355struct command_line *
356breakpoint_commands (struct breakpoint *b)
357{
358 return b->commands ? b->commands->commands : NULL;
359}
3daf8fe5 360
f3b1572e
PA
361/* Flag indicating that a command has proceeded the inferior past the
362 current breakpoint. */
363
364static int breakpoint_proceeded;
365
956a9fb9 366const char *
2cec12e5
AR
367bpdisp_text (enum bpdisp disp)
368{
4a64f543
MS
369 /* NOTE: the following values are a part of MI protocol and
370 represent values of 'disp' field returned when inferior stops at
371 a breakpoint. */
bc043ef3 372 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 373
2cec12e5
AR
374 return bpdisps[(int) disp];
375}
c906108c 376
4a64f543 377/* Prototypes for exported functions. */
c906108c 378/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 379 if such is available. */
c906108c
SS
380static int can_use_hw_watchpoints;
381
920d2a44
AC
382static void
383show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
384 struct cmd_list_element *c,
385 const char *value)
386{
3e43a32a
MS
387 fprintf_filtered (file,
388 _("Debugger's willingness to use "
389 "watchpoint hardware is %s.\n"),
920d2a44
AC
390 value);
391}
392
fa8d40ab
JJ
393/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
394 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 395 for unrecognized breakpoint locations.
fa8d40ab
JJ
396 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
397static enum auto_boolean pending_break_support;
920d2a44
AC
398static void
399show_pending_break_support (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402{
3e43a32a
MS
403 fprintf_filtered (file,
404 _("Debugger's behavior regarding "
405 "pending breakpoints is %s.\n"),
920d2a44
AC
406 value);
407}
fa8d40ab 408
765dc015 409/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 410 set with "break" but falling in read-only memory.
765dc015
VP
411 If 0, gdb will warn about such breakpoints, but won't automatically
412 use hardware breakpoints. */
413static int automatic_hardware_breakpoints;
414static void
415show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418{
3e43a32a
MS
419 fprintf_filtered (file,
420 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
421 value);
422}
423
33e5cbd6
PA
424/* If on, gdb will keep breakpoints inserted even as inferior is
425 stopped, and immediately insert any new breakpoints. If off, gdb
426 will insert breakpoints into inferior only when resuming it, and
427 will remove breakpoints upon stop. If auto, GDB will behave as ON
428 if in non-stop mode, and as OFF if all-stop mode.*/
429
72d0e2c5
YQ
430static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
431
33e5cbd6 432static void
74960c60 433show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 434 struct cmd_list_element *c, const char *value)
74960c60 435{
72d0e2c5 436 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
437 fprintf_filtered (file,
438 _("Always inserted breakpoint "
439 "mode is %s (currently %s).\n"),
33e5cbd6
PA
440 value,
441 breakpoints_always_inserted_mode () ? "on" : "off");
442 else
3e43a32a
MS
443 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
444 value);
74960c60
VP
445}
446
33e5cbd6
PA
447int
448breakpoints_always_inserted_mode (void)
449{
72d0e2c5
YQ
450 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
451 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 452}
765dc015 453
b775012e
LM
454static const char condition_evaluation_both[] = "host or target";
455
456/* Modes for breakpoint condition evaluation. */
457static const char condition_evaluation_auto[] = "auto";
458static const char condition_evaluation_host[] = "host";
459static const char condition_evaluation_target[] = "target";
460static const char *const condition_evaluation_enums[] = {
461 condition_evaluation_auto,
462 condition_evaluation_host,
463 condition_evaluation_target,
464 NULL
465};
466
467/* Global that holds the current mode for breakpoint condition evaluation. */
468static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
469
470/* Global that we use to display information to the user (gets its value from
471 condition_evaluation_mode_1. */
472static const char *condition_evaluation_mode = condition_evaluation_auto;
473
474/* Translate a condition evaluation mode MODE into either "host"
475 or "target". This is used mostly to translate from "auto" to the
476 real setting that is being used. It returns the translated
477 evaluation mode. */
478
479static const char *
480translate_condition_evaluation_mode (const char *mode)
481{
482 if (mode == condition_evaluation_auto)
483 {
484 if (target_supports_evaluation_of_breakpoint_conditions ())
485 return condition_evaluation_target;
486 else
487 return condition_evaluation_host;
488 }
489 else
490 return mode;
491}
492
493/* Discovers what condition_evaluation_auto translates to. */
494
495static const char *
496breakpoint_condition_evaluation_mode (void)
497{
498 return translate_condition_evaluation_mode (condition_evaluation_mode);
499}
500
501/* Return true if GDB should evaluate breakpoint conditions or false
502 otherwise. */
503
504static int
505gdb_evaluates_breakpoint_condition_p (void)
506{
507 const char *mode = breakpoint_condition_evaluation_mode ();
508
509 return (mode == condition_evaluation_host);
510}
511
a14ed312 512void _initialize_breakpoint (void);
c906108c 513
c906108c
SS
514/* Are we executing breakpoint commands? */
515static int executing_breakpoint_commands;
516
c02f5703
MS
517/* Are overlay event breakpoints enabled? */
518static int overlay_events_enabled;
519
e09342b5
TJB
520/* See description in breakpoint.h. */
521int target_exact_watchpoints = 0;
522
c906108c 523/* Walk the following statement or block through all breakpoints.
e5dd4106 524 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 525 current breakpoint. */
c906108c 526
5c44784c 527#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 528
5c44784c
JM
529#define ALL_BREAKPOINTS_SAFE(B,TMP) \
530 for (B = breakpoint_chain; \
531 B ? (TMP=B->next, 1): 0; \
532 B = TMP)
c906108c 533
4a64f543
MS
534/* Similar iterator for the low-level breakpoints. SAFE variant is
535 not provided so update_global_location_list must not be called
536 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 537
876fa593
JK
538#define ALL_BP_LOCATIONS(B,BP_TMP) \
539 for (BP_TMP = bp_location; \
540 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
541 BP_TMP++)
7cc221ef 542
b775012e
LM
543/* Iterates through locations with address ADDRESS for the currently selected
544 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
545 to where the loop should start from.
546 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
547 appropriate location to start with. */
548
549#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
550 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
551 BP_LOCP_TMP = BP_LOCP_START; \
552 BP_LOCP_START \
553 && (BP_LOCP_TMP < bp_location + bp_location_count \
554 && (*BP_LOCP_TMP)->address == ADDRESS); \
555 BP_LOCP_TMP++)
556
1042e4c0
SS
557/* Iterator for tracepoints only. */
558
559#define ALL_TRACEPOINTS(B) \
560 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 561 if (is_tracepoint (B))
1042e4c0 562
7cc221ef 563/* Chains of all breakpoints defined. */
c906108c
SS
564
565struct breakpoint *breakpoint_chain;
566
876fa593
JK
567/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
568
569static struct bp_location **bp_location;
570
571/* Number of elements of BP_LOCATION. */
572
573static unsigned bp_location_count;
574
4a64f543
MS
575/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
576 ADDRESS for the current elements of BP_LOCATION which get a valid
577 result from bp_location_has_shadow. You can use it for roughly
578 limiting the subrange of BP_LOCATION to scan for shadow bytes for
579 an address you need to read. */
876fa593
JK
580
581static CORE_ADDR bp_location_placed_address_before_address_max;
582
4a64f543
MS
583/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
584 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
585 BP_LOCATION which get a valid result from bp_location_has_shadow.
586 You can use it for roughly limiting the subrange of BP_LOCATION to
587 scan for shadow bytes for an address you need to read. */
876fa593
JK
588
589static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 590
4a64f543
MS
591/* The locations that no longer correspond to any breakpoint, unlinked
592 from bp_location array, but for which a hit may still be reported
593 by a target. */
20874c92
VP
594VEC(bp_location_p) *moribund_locations = NULL;
595
c906108c
SS
596/* Number of last breakpoint made. */
597
95a42b64
TT
598static int breakpoint_count;
599
86b17b60
PA
600/* The value of `breakpoint_count' before the last command that
601 created breakpoints. If the last (break-like) command created more
602 than one breakpoint, then the difference between BREAKPOINT_COUNT
603 and PREV_BREAKPOINT_COUNT is more than one. */
604static int prev_breakpoint_count;
c906108c 605
1042e4c0
SS
606/* Number of last tracepoint made. */
607
95a42b64 608static int tracepoint_count;
1042e4c0 609
6149aea9
PA
610static struct cmd_list_element *breakpoint_set_cmdlist;
611static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 612struct cmd_list_element *save_cmdlist;
6149aea9 613
468d015d
JJ
614/* Return whether a breakpoint is an active enabled breakpoint. */
615static int
616breakpoint_enabled (struct breakpoint *b)
617{
0d381245 618 return (b->enable_state == bp_enabled);
468d015d
JJ
619}
620
c906108c
SS
621/* Set breakpoint count to NUM. */
622
95a42b64 623static void
fba45db2 624set_breakpoint_count (int num)
c906108c 625{
86b17b60 626 prev_breakpoint_count = breakpoint_count;
c906108c 627 breakpoint_count = num;
4fa62494 628 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
629}
630
86b17b60
PA
631/* Used by `start_rbreak_breakpoints' below, to record the current
632 breakpoint count before "rbreak" creates any breakpoint. */
633static int rbreak_start_breakpoint_count;
634
95a42b64
TT
635/* Called at the start an "rbreak" command to record the first
636 breakpoint made. */
86b17b60 637
95a42b64
TT
638void
639start_rbreak_breakpoints (void)
640{
86b17b60 641 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
642}
643
644/* Called at the end of an "rbreak" command to record the last
645 breakpoint made. */
86b17b60 646
95a42b64
TT
647void
648end_rbreak_breakpoints (void)
649{
86b17b60 650 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
651}
652
4a64f543 653/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
654
655void
fba45db2 656clear_breakpoint_hit_counts (void)
c906108c
SS
657{
658 struct breakpoint *b;
659
660 ALL_BREAKPOINTS (b)
661 b->hit_count = 0;
662}
663
9add0f1b
TT
664/* Allocate a new counted_command_line with reference count of 1.
665 The new structure owns COMMANDS. */
666
667static struct counted_command_line *
668alloc_counted_command_line (struct command_line *commands)
669{
670 struct counted_command_line *result
671 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 672
9add0f1b
TT
673 result->refc = 1;
674 result->commands = commands;
675 return result;
676}
677
678/* Increment reference count. This does nothing if CMD is NULL. */
679
680static void
681incref_counted_command_line (struct counted_command_line *cmd)
682{
683 if (cmd)
684 ++cmd->refc;
685}
686
687/* Decrement reference count. If the reference count reaches 0,
688 destroy the counted_command_line. Sets *CMDP to NULL. This does
689 nothing if *CMDP is NULL. */
690
691static void
692decref_counted_command_line (struct counted_command_line **cmdp)
693{
694 if (*cmdp)
695 {
696 if (--(*cmdp)->refc == 0)
697 {
698 free_command_lines (&(*cmdp)->commands);
699 xfree (*cmdp);
700 }
701 *cmdp = NULL;
702 }
703}
704
705/* A cleanup function that calls decref_counted_command_line. */
706
707static void
708do_cleanup_counted_command_line (void *arg)
709{
710 decref_counted_command_line (arg);
711}
712
713/* Create a cleanup that calls decref_counted_command_line on the
714 argument. */
715
716static struct cleanup *
717make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
718{
719 return make_cleanup (do_cleanup_counted_command_line, cmdp);
720}
721
c906108c 722\f
48cb2d85
VP
723/* Return the breakpoint with the specified number, or NULL
724 if the number does not refer to an existing breakpoint. */
725
726struct breakpoint *
727get_breakpoint (int num)
728{
729 struct breakpoint *b;
730
731 ALL_BREAKPOINTS (b)
732 if (b->number == num)
733 return b;
734
735 return NULL;
736}
5c44784c 737
c906108c 738\f
adc36818 739
b775012e
LM
740/* Mark locations as "conditions have changed" in case the target supports
741 evaluating conditions on its side. */
742
743static void
744mark_breakpoint_modified (struct breakpoint *b)
745{
746 struct bp_location *loc;
747
748 /* This is only meaningful if the target is
749 evaluating conditions and if the user has
750 opted for condition evaluation on the target's
751 side. */
752 if (gdb_evaluates_breakpoint_condition_p ()
753 || !target_supports_evaluation_of_breakpoint_conditions ())
754 return;
755
756 if (!is_breakpoint (b))
757 return;
758
759 for (loc = b->loc; loc; loc = loc->next)
760 loc->condition_changed = condition_modified;
761}
762
763/* Mark location as "conditions have changed" in case the target supports
764 evaluating conditions on its side. */
765
766static void
767mark_breakpoint_location_modified (struct bp_location *loc)
768{
769 /* This is only meaningful if the target is
770 evaluating conditions and if the user has
771 opted for condition evaluation on the target's
772 side. */
773 if (gdb_evaluates_breakpoint_condition_p ()
774 || !target_supports_evaluation_of_breakpoint_conditions ())
775
776 return;
777
778 if (!is_breakpoint (loc->owner))
779 return;
780
781 loc->condition_changed = condition_modified;
782}
783
784/* Sets the condition-evaluation mode using the static global
785 condition_evaluation_mode. */
786
787static void
788set_condition_evaluation_mode (char *args, int from_tty,
789 struct cmd_list_element *c)
790{
b775012e
LM
791 const char *old_mode, *new_mode;
792
793 if ((condition_evaluation_mode_1 == condition_evaluation_target)
794 && !target_supports_evaluation_of_breakpoint_conditions ())
795 {
796 condition_evaluation_mode_1 = condition_evaluation_mode;
797 warning (_("Target does not support breakpoint condition evaluation.\n"
798 "Using host evaluation mode instead."));
799 return;
800 }
801
802 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
803 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
804
abf1152a
JK
805 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
806 settings was "auto". */
807 condition_evaluation_mode = condition_evaluation_mode_1;
808
b775012e
LM
809 /* Only update the mode if the user picked a different one. */
810 if (new_mode != old_mode)
811 {
812 struct bp_location *loc, **loc_tmp;
813 /* If the user switched to a different evaluation mode, we
814 need to synch the changes with the target as follows:
815
816 "host" -> "target": Send all (valid) conditions to the target.
817 "target" -> "host": Remove all the conditions from the target.
818 */
819
b775012e
LM
820 if (new_mode == condition_evaluation_target)
821 {
822 /* Mark everything modified and synch conditions with the
823 target. */
824 ALL_BP_LOCATIONS (loc, loc_tmp)
825 mark_breakpoint_location_modified (loc);
826 }
827 else
828 {
829 /* Manually mark non-duplicate locations to synch conditions
830 with the target. We do this to remove all the conditions the
831 target knows about. */
832 ALL_BP_LOCATIONS (loc, loc_tmp)
833 if (is_breakpoint (loc->owner) && loc->inserted)
834 loc->needs_update = 1;
835 }
836
837 /* Do the update. */
838 update_global_location_list (1);
839 }
840
841 return;
842}
843
844/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
845 what "auto" is translating to. */
846
847static void
848show_condition_evaluation_mode (struct ui_file *file, int from_tty,
849 struct cmd_list_element *c, const char *value)
850{
851 if (condition_evaluation_mode == condition_evaluation_auto)
852 fprintf_filtered (file,
853 _("Breakpoint condition evaluation "
854 "mode is %s (currently %s).\n"),
855 value,
856 breakpoint_condition_evaluation_mode ());
857 else
858 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
859 value);
860}
861
862/* A comparison function for bp_location AP and BP that is used by
863 bsearch. This comparison function only cares about addresses, unlike
864 the more general bp_location_compare function. */
865
866static int
867bp_location_compare_addrs (const void *ap, const void *bp)
868{
869 struct bp_location *a = *(void **) ap;
870 struct bp_location *b = *(void **) bp;
871
872 if (a->address == b->address)
873 return 0;
874 else
875 return ((a->address > b->address) - (a->address < b->address));
876}
877
878/* Helper function to skip all bp_locations with addresses
879 less than ADDRESS. It returns the first bp_location that
880 is greater than or equal to ADDRESS. If none is found, just
881 return NULL. */
882
883static struct bp_location **
884get_first_locp_gte_addr (CORE_ADDR address)
885{
886 struct bp_location dummy_loc;
887 struct bp_location *dummy_locp = &dummy_loc;
888 struct bp_location **locp_found = NULL;
889
890 /* Initialize the dummy location's address field. */
891 memset (&dummy_loc, 0, sizeof (struct bp_location));
892 dummy_loc.address = address;
893
894 /* Find a close match to the first location at ADDRESS. */
895 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
896 sizeof (struct bp_location **),
897 bp_location_compare_addrs);
898
899 /* Nothing was found, nothing left to do. */
900 if (locp_found == NULL)
901 return NULL;
902
903 /* We may have found a location that is at ADDRESS but is not the first in the
904 location's list. Go backwards (if possible) and locate the first one. */
905 while ((locp_found - 1) >= bp_location
906 && (*(locp_found - 1))->address == address)
907 locp_found--;
908
909 return locp_found;
910}
911
adc36818
PM
912void
913set_breakpoint_condition (struct breakpoint *b, char *exp,
914 int from_tty)
915{
3a5c3e22
PA
916 xfree (b->cond_string);
917 b->cond_string = NULL;
adc36818 918
3a5c3e22 919 if (is_watchpoint (b))
adc36818 920 {
3a5c3e22
PA
921 struct watchpoint *w = (struct watchpoint *) b;
922
923 xfree (w->cond_exp);
924 w->cond_exp = NULL;
925 }
926 else
927 {
928 struct bp_location *loc;
929
930 for (loc = b->loc; loc; loc = loc->next)
931 {
932 xfree (loc->cond);
933 loc->cond = NULL;
b775012e
LM
934
935 /* No need to free the condition agent expression
936 bytecode (if we have one). We will handle this
937 when we go through update_global_location_list. */
3a5c3e22 938 }
adc36818 939 }
adc36818
PM
940
941 if (*exp == 0)
942 {
943 if (from_tty)
944 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
945 }
946 else
947 {
bbc13ae3 948 const char *arg = exp;
cc59ec59 949
adc36818
PM
950 /* I don't know if it matters whether this is the string the user
951 typed in or the decompiled expression. */
952 b->cond_string = xstrdup (arg);
953 b->condition_not_parsed = 0;
954
955 if (is_watchpoint (b))
956 {
3a5c3e22
PA
957 struct watchpoint *w = (struct watchpoint *) b;
958
adc36818
PM
959 innermost_block = NULL;
960 arg = exp;
1bb9788d 961 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
962 if (*arg)
963 error (_("Junk at end of expression"));
3a5c3e22 964 w->cond_exp_valid_block = innermost_block;
adc36818
PM
965 }
966 else
967 {
3a5c3e22
PA
968 struct bp_location *loc;
969
adc36818
PM
970 for (loc = b->loc; loc; loc = loc->next)
971 {
972 arg = exp;
973 loc->cond =
1bb9788d
TT
974 parse_exp_1 (&arg, loc->address,
975 block_for_pc (loc->address), 0);
adc36818
PM
976 if (*arg)
977 error (_("Junk at end of expression"));
978 }
979 }
980 }
b775012e
LM
981 mark_breakpoint_modified (b);
982
8d3788bd 983 observer_notify_breakpoint_modified (b);
adc36818
PM
984}
985
d55637df
TT
986/* Completion for the "condition" command. */
987
988static VEC (char_ptr) *
6f937416
PA
989condition_completer (struct cmd_list_element *cmd,
990 const char *text, const char *word)
d55637df 991{
6f937416 992 const char *space;
d55637df 993
6f937416
PA
994 text = skip_spaces_const (text);
995 space = skip_to_space_const (text);
d55637df
TT
996 if (*space == '\0')
997 {
998 int len;
999 struct breakpoint *b;
1000 VEC (char_ptr) *result = NULL;
1001
1002 if (text[0] == '$')
1003 {
1004 /* We don't support completion of history indices. */
1005 if (isdigit (text[1]))
1006 return NULL;
1007 return complete_internalvar (&text[1]);
1008 }
1009
1010 /* We're completing the breakpoint number. */
1011 len = strlen (text);
1012
1013 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1014 {
1015 char number[50];
1016
1017 xsnprintf (number, sizeof (number), "%d", b->number);
1018
1019 if (strncmp (number, text, len) == 0)
1020 VEC_safe_push (char_ptr, result, xstrdup (number));
1021 }
d55637df
TT
1022
1023 return result;
1024 }
1025
1026 /* We're completing the expression part. */
6f937416 1027 text = skip_spaces_const (space);
d55637df
TT
1028 return expression_completer (cmd, text, word);
1029}
1030
c906108c
SS
1031/* condition N EXP -- set break condition of breakpoint N to EXP. */
1032
1033static void
fba45db2 1034condition_command (char *arg, int from_tty)
c906108c 1035{
52f0bd74 1036 struct breakpoint *b;
c906108c 1037 char *p;
52f0bd74 1038 int bnum;
c906108c
SS
1039
1040 if (arg == 0)
e2e0b3e5 1041 error_no_arg (_("breakpoint number"));
c906108c
SS
1042
1043 p = arg;
1044 bnum = get_number (&p);
5c44784c 1045 if (bnum == 0)
8a3fe4f8 1046 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1047
1048 ALL_BREAKPOINTS (b)
1049 if (b->number == bnum)
2f069f6f 1050 {
7371cf6d
PM
1051 /* Check if this breakpoint has a Python object assigned to
1052 it, and if it has a definition of the "stop"
1053 method. This method and conditions entered into GDB from
1054 the CLI are mutually exclusive. */
1055 if (b->py_bp_object
1056 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1057 error (_("Cannot set a condition where a Python 'stop' "
1058 "method has been defined in the breakpoint."));
2566ad2d 1059 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1060
1061 if (is_breakpoint (b))
1062 update_global_location_list (1);
1063
2f069f6f
JB
1064 return;
1065 }
c906108c 1066
8a3fe4f8 1067 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1068}
1069
a7bdde9e
VP
1070/* Check that COMMAND do not contain commands that are suitable
1071 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1072 Throw if any such commands is found. */
1073
a7bdde9e
VP
1074static void
1075check_no_tracepoint_commands (struct command_line *commands)
1076{
1077 struct command_line *c;
cc59ec59 1078
a7bdde9e
VP
1079 for (c = commands; c; c = c->next)
1080 {
1081 int i;
1082
1083 if (c->control_type == while_stepping_control)
3e43a32a
MS
1084 error (_("The 'while-stepping' command can "
1085 "only be used for tracepoints"));
a7bdde9e
VP
1086
1087 for (i = 0; i < c->body_count; ++i)
1088 check_no_tracepoint_commands ((c->body_list)[i]);
1089
1090 /* Not that command parsing removes leading whitespace and comment
4a64f543 1091 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1092 command directly. */
1093 if (strstr (c->line, "collect ") == c->line)
1094 error (_("The 'collect' command can only be used for tracepoints"));
1095
51661e93
VP
1096 if (strstr (c->line, "teval ") == c->line)
1097 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1098 }
1099}
1100
d77f58be
SS
1101/* Encapsulate tests for different types of tracepoints. */
1102
d9b3f62e
PA
1103static int
1104is_tracepoint_type (enum bptype type)
1105{
1106 return (type == bp_tracepoint
1107 || type == bp_fast_tracepoint
1108 || type == bp_static_tracepoint);
1109}
1110
a7bdde9e 1111int
d77f58be 1112is_tracepoint (const struct breakpoint *b)
a7bdde9e 1113{
d9b3f62e 1114 return is_tracepoint_type (b->type);
a7bdde9e 1115}
d9b3f62e 1116
e5dd4106 1117/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1118 breakpoint. This function will throw an exception if a problem is
1119 found. */
48cb2d85 1120
95a42b64
TT
1121static void
1122validate_commands_for_breakpoint (struct breakpoint *b,
1123 struct command_line *commands)
48cb2d85 1124{
d77f58be 1125 if (is_tracepoint (b))
a7bdde9e 1126 {
c9a6ce02 1127 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1128 struct command_line *c;
1129 struct command_line *while_stepping = 0;
c9a6ce02
PA
1130
1131 /* Reset the while-stepping step count. The previous commands
1132 might have included a while-stepping action, while the new
1133 ones might not. */
1134 t->step_count = 0;
1135
1136 /* We need to verify that each top-level element of commands is
1137 valid for tracepoints, that there's at most one
1138 while-stepping element, and that the while-stepping's body
1139 has valid tracing commands excluding nested while-stepping.
1140 We also need to validate the tracepoint action line in the
1141 context of the tracepoint --- validate_actionline actually
1142 has side effects, like setting the tracepoint's
1143 while-stepping STEP_COUNT, in addition to checking if the
1144 collect/teval actions parse and make sense in the
1145 tracepoint's context. */
a7bdde9e
VP
1146 for (c = commands; c; c = c->next)
1147 {
a7bdde9e
VP
1148 if (c->control_type == while_stepping_control)
1149 {
1150 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1151 error (_("The 'while-stepping' command "
1152 "cannot be used for fast tracepoint"));
0fb4aa4b 1153 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1154 error (_("The 'while-stepping' command "
1155 "cannot be used for static tracepoint"));
a7bdde9e
VP
1156
1157 if (while_stepping)
3e43a32a
MS
1158 error (_("The 'while-stepping' command "
1159 "can be used only once"));
a7bdde9e
VP
1160 else
1161 while_stepping = c;
1162 }
c9a6ce02
PA
1163
1164 validate_actionline (c->line, b);
a7bdde9e
VP
1165 }
1166 if (while_stepping)
1167 {
1168 struct command_line *c2;
1169
1170 gdb_assert (while_stepping->body_count == 1);
1171 c2 = while_stepping->body_list[0];
1172 for (; c2; c2 = c2->next)
1173 {
a7bdde9e
VP
1174 if (c2->control_type == while_stepping_control)
1175 error (_("The 'while-stepping' command cannot be nested"));
1176 }
1177 }
1178 }
1179 else
1180 {
1181 check_no_tracepoint_commands (commands);
1182 }
95a42b64
TT
1183}
1184
0fb4aa4b
PA
1185/* Return a vector of all the static tracepoints set at ADDR. The
1186 caller is responsible for releasing the vector. */
1187
1188VEC(breakpoint_p) *
1189static_tracepoints_here (CORE_ADDR addr)
1190{
1191 struct breakpoint *b;
1192 VEC(breakpoint_p) *found = 0;
1193 struct bp_location *loc;
1194
1195 ALL_BREAKPOINTS (b)
1196 if (b->type == bp_static_tracepoint)
1197 {
1198 for (loc = b->loc; loc; loc = loc->next)
1199 if (loc->address == addr)
1200 VEC_safe_push(breakpoint_p, found, b);
1201 }
1202
1203 return found;
1204}
1205
95a42b64 1206/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1207 validate that only allowed commands are included. */
95a42b64
TT
1208
1209void
4a64f543
MS
1210breakpoint_set_commands (struct breakpoint *b,
1211 struct command_line *commands)
95a42b64
TT
1212{
1213 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1214
9add0f1b
TT
1215 decref_counted_command_line (&b->commands);
1216 b->commands = alloc_counted_command_line (commands);
8d3788bd 1217 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1218}
1219
45a43567
TT
1220/* Set the internal `silent' flag on the breakpoint. Note that this
1221 is not the same as the "silent" that may appear in the breakpoint's
1222 commands. */
1223
1224void
1225breakpoint_set_silent (struct breakpoint *b, int silent)
1226{
1227 int old_silent = b->silent;
1228
1229 b->silent = silent;
1230 if (old_silent != silent)
8d3788bd 1231 observer_notify_breakpoint_modified (b);
45a43567
TT
1232}
1233
1234/* Set the thread for this breakpoint. If THREAD is -1, make the
1235 breakpoint work for any thread. */
1236
1237void
1238breakpoint_set_thread (struct breakpoint *b, int thread)
1239{
1240 int old_thread = b->thread;
1241
1242 b->thread = thread;
1243 if (old_thread != thread)
8d3788bd 1244 observer_notify_breakpoint_modified (b);
45a43567
TT
1245}
1246
1247/* Set the task for this breakpoint. If TASK is 0, make the
1248 breakpoint work for any task. */
1249
1250void
1251breakpoint_set_task (struct breakpoint *b, int task)
1252{
1253 int old_task = b->task;
1254
1255 b->task = task;
1256 if (old_task != task)
8d3788bd 1257 observer_notify_breakpoint_modified (b);
45a43567
TT
1258}
1259
95a42b64
TT
1260void
1261check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1262{
1263 struct breakpoint *b = closure;
cc59ec59 1264
6f937416 1265 validate_actionline (line, b);
a7bdde9e
VP
1266}
1267
95a42b64
TT
1268/* A structure used to pass information through
1269 map_breakpoint_numbers. */
1270
1271struct commands_info
1272{
1273 /* True if the command was typed at a tty. */
1274 int from_tty;
86b17b60
PA
1275
1276 /* The breakpoint range spec. */
1277 char *arg;
1278
95a42b64
TT
1279 /* Non-NULL if the body of the commands are being read from this
1280 already-parsed command. */
1281 struct command_line *control;
86b17b60 1282
95a42b64
TT
1283 /* The command lines read from the user, or NULL if they have not
1284 yet been read. */
1285 struct counted_command_line *cmd;
1286};
1287
1288/* A callback for map_breakpoint_numbers that sets the commands for
1289 commands_command. */
1290
c906108c 1291static void
95a42b64 1292do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1293{
95a42b64 1294 struct commands_info *info = data;
c906108c 1295
95a42b64
TT
1296 if (info->cmd == NULL)
1297 {
1298 struct command_line *l;
5c44784c 1299
95a42b64
TT
1300 if (info->control != NULL)
1301 l = copy_command_lines (info->control->body_list[0]);
1302 else
86b17b60
PA
1303 {
1304 struct cleanup *old_chain;
1305 char *str;
c5aa993b 1306
3e43a32a
MS
1307 str = xstrprintf (_("Type commands for breakpoint(s) "
1308 "%s, one per line."),
86b17b60
PA
1309 info->arg);
1310
1311 old_chain = make_cleanup (xfree, str);
1312
1313 l = read_command_lines (str,
1314 info->from_tty, 1,
d77f58be 1315 (is_tracepoint (b)
86b17b60
PA
1316 ? check_tracepoint_command : 0),
1317 b);
1318
1319 do_cleanups (old_chain);
1320 }
a7bdde9e 1321
95a42b64
TT
1322 info->cmd = alloc_counted_command_line (l);
1323 }
1324
1325 /* If a breakpoint was on the list more than once, we don't need to
1326 do anything. */
1327 if (b->commands != info->cmd)
1328 {
1329 validate_commands_for_breakpoint (b, info->cmd->commands);
1330 incref_counted_command_line (info->cmd);
1331 decref_counted_command_line (&b->commands);
1332 b->commands = info->cmd;
8d3788bd 1333 observer_notify_breakpoint_modified (b);
c5aa993b 1334 }
95a42b64
TT
1335}
1336
1337static void
4a64f543
MS
1338commands_command_1 (char *arg, int from_tty,
1339 struct command_line *control)
95a42b64
TT
1340{
1341 struct cleanup *cleanups;
1342 struct commands_info info;
1343
1344 info.from_tty = from_tty;
1345 info.control = control;
1346 info.cmd = NULL;
1347 /* If we read command lines from the user, then `info' will hold an
1348 extra reference to the commands that we must clean up. */
1349 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1350
1351 if (arg == NULL || !*arg)
1352 {
86b17b60 1353 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1354 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1355 breakpoint_count);
95a42b64
TT
1356 else if (breakpoint_count > 0)
1357 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1358 else
1359 {
1360 /* So that we don't try to free the incoming non-NULL
1361 argument in the cleanup below. Mapping breakpoint
1362 numbers will fail in this case. */
1363 arg = NULL;
1364 }
95a42b64 1365 }
9766ced4
SS
1366 else
1367 /* The command loop has some static state, so we need to preserve
1368 our argument. */
1369 arg = xstrdup (arg);
86b17b60
PA
1370
1371 if (arg != NULL)
1372 make_cleanup (xfree, arg);
1373
1374 info.arg = arg;
95a42b64
TT
1375
1376 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1377
1378 if (info.cmd == NULL)
1379 error (_("No breakpoints specified."));
1380
1381 do_cleanups (cleanups);
1382}
1383
1384static void
1385commands_command (char *arg, int from_tty)
1386{
1387 commands_command_1 (arg, from_tty, NULL);
c906108c 1388}
40c03ae8
EZ
1389
1390/* Like commands_command, but instead of reading the commands from
1391 input stream, takes them from an already parsed command structure.
1392
1393 This is used by cli-script.c to DTRT with breakpoint commands
1394 that are part of if and while bodies. */
1395enum command_control_type
1396commands_from_control_command (char *arg, struct command_line *cmd)
1397{
95a42b64
TT
1398 commands_command_1 (arg, 0, cmd);
1399 return simple_control;
40c03ae8 1400}
876fa593
JK
1401
1402/* Return non-zero if BL->TARGET_INFO contains valid information. */
1403
1404static int
1405bp_location_has_shadow (struct bp_location *bl)
1406{
1407 if (bl->loc_type != bp_loc_software_breakpoint)
1408 return 0;
1409 if (!bl->inserted)
1410 return 0;
1411 if (bl->target_info.shadow_len == 0)
e5dd4106 1412 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1413 return 0;
1414 return 1;
1415}
1416
8defab1a 1417/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1418 by replacing any memory breakpoints with their shadowed contents.
1419
35c63cd8
JB
1420 If READBUF is not NULL, this buffer must not overlap with any of
1421 the breakpoint location's shadow_contents buffers. Otherwise,
1422 a failed assertion internal error will be raised.
1423
876fa593 1424 The range of shadowed area by each bp_location is:
35df4500
TJB
1425 bl->address - bp_location_placed_address_before_address_max
1426 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1427 The range we were requested to resolve shadows for is:
1428 memaddr ... memaddr + len
1429 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1430 memaddr + len <= (bl->address
1431 - bp_location_placed_address_before_address_max)
876fa593 1432 and:
35df4500 1433 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1434
8defab1a 1435void
f0ba3972
PA
1436breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1437 const gdb_byte *writebuf_org,
1438 ULONGEST memaddr, LONGEST len)
c906108c 1439{
4a64f543
MS
1440 /* Left boundary, right boundary and median element of our binary
1441 search. */
876fa593
JK
1442 unsigned bc_l, bc_r, bc;
1443
4a64f543
MS
1444 /* Find BC_L which is a leftmost element which may affect BUF
1445 content. It is safe to report lower value but a failure to
1446 report higher one. */
876fa593
JK
1447
1448 bc_l = 0;
1449 bc_r = bp_location_count;
1450 while (bc_l + 1 < bc_r)
1451 {
35df4500 1452 struct bp_location *bl;
876fa593
JK
1453
1454 bc = (bc_l + bc_r) / 2;
35df4500 1455 bl = bp_location[bc];
876fa593 1456
4a64f543
MS
1457 /* Check first BL->ADDRESS will not overflow due to the added
1458 constant. Then advance the left boundary only if we are sure
1459 the BC element can in no way affect the BUF content (MEMADDR
1460 to MEMADDR + LEN range).
876fa593 1461
4a64f543
MS
1462 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1463 offset so that we cannot miss a breakpoint with its shadow
1464 range tail still reaching MEMADDR. */
c5aa993b 1465
35df4500
TJB
1466 if ((bl->address + bp_location_shadow_len_after_address_max
1467 >= bl->address)
1468 && (bl->address + bp_location_shadow_len_after_address_max
1469 <= memaddr))
876fa593
JK
1470 bc_l = bc;
1471 else
1472 bc_r = bc;
1473 }
1474
128070bb
PA
1475 /* Due to the binary search above, we need to make sure we pick the
1476 first location that's at BC_L's address. E.g., if there are
1477 multiple locations at the same address, BC_L may end up pointing
1478 at a duplicate location, and miss the "master"/"inserted"
1479 location. Say, given locations L1, L2 and L3 at addresses A and
1480 B:
1481
1482 L1@A, L2@A, L3@B, ...
1483
1484 BC_L could end up pointing at location L2, while the "master"
1485 location could be L1. Since the `loc->inserted' flag is only set
1486 on "master" locations, we'd forget to restore the shadow of L1
1487 and L2. */
1488 while (bc_l > 0
1489 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1490 bc_l--;
1491
876fa593
JK
1492 /* Now do full processing of the found relevant range of elements. */
1493
1494 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1495 {
35df4500 1496 struct bp_location *bl = bp_location[bc];
876fa593
JK
1497 CORE_ADDR bp_addr = 0;
1498 int bp_size = 0;
1499 int bptoffset = 0;
1500
35df4500
TJB
1501 /* bp_location array has BL->OWNER always non-NULL. */
1502 if (bl->owner->type == bp_none)
8a3fe4f8 1503 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1504 bl->owner->number);
ffce0d52 1505
e5dd4106 1506 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1507 content. */
1508
35df4500
TJB
1509 if (bl->address >= bp_location_placed_address_before_address_max
1510 && memaddr + len <= (bl->address
1511 - bp_location_placed_address_before_address_max))
876fa593
JK
1512 break;
1513
35df4500 1514 if (!bp_location_has_shadow (bl))
c5aa993b 1515 continue;
35df4500 1516 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1517 current_program_space->aspace, 0))
1518 continue;
1519
c5aa993b
JM
1520 /* Addresses and length of the part of the breakpoint that
1521 we need to copy. */
35df4500
TJB
1522 bp_addr = bl->target_info.placed_address;
1523 bp_size = bl->target_info.shadow_len;
8defab1a 1524
c5aa993b
JM
1525 if (bp_addr + bp_size <= memaddr)
1526 /* The breakpoint is entirely before the chunk of memory we
1527 are reading. */
1528 continue;
8defab1a 1529
c5aa993b
JM
1530 if (bp_addr >= memaddr + len)
1531 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1532 reading. */
c5aa993b 1533 continue;
c5aa993b 1534
8defab1a
DJ
1535 /* Offset within shadow_contents. */
1536 if (bp_addr < memaddr)
1537 {
1538 /* Only copy the second part of the breakpoint. */
1539 bp_size -= memaddr - bp_addr;
1540 bptoffset = memaddr - bp_addr;
1541 bp_addr = memaddr;
1542 }
c5aa993b 1543
8defab1a
DJ
1544 if (bp_addr + bp_size > memaddr + len)
1545 {
1546 /* Only copy the first part of the breakpoint. */
1547 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1548 }
c5aa993b 1549
f0ba3972
PA
1550 if (readbuf != NULL)
1551 {
35c63cd8
JB
1552 /* Verify that the readbuf buffer does not overlap with
1553 the shadow_contents buffer. */
1554 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1555 || readbuf >= (bl->target_info.shadow_contents
1556 + bl->target_info.shadow_len));
1557
f0ba3972
PA
1558 /* Update the read buffer with this inserted breakpoint's
1559 shadow. */
1560 memcpy (readbuf + bp_addr - memaddr,
1561 bl->target_info.shadow_contents + bptoffset, bp_size);
1562 }
1563 else
1564 {
1565 struct gdbarch *gdbarch = bl->gdbarch;
1566 const unsigned char *bp;
1567 CORE_ADDR placed_address = bl->target_info.placed_address;
20ced3e4 1568 int placed_size = bl->target_info.placed_size;
f0ba3972
PA
1569
1570 /* Update the shadow with what we want to write to memory. */
1571 memcpy (bl->target_info.shadow_contents + bptoffset,
1572 writebuf_org + bp_addr - memaddr, bp_size);
1573
1574 /* Determine appropriate breakpoint contents and size for this
1575 address. */
1576 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1577
1578 /* Update the final write buffer with this inserted
1579 breakpoint's INSN. */
1580 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1581 }
c5aa993b 1582 }
c906108c 1583}
c906108c 1584\f
c5aa993b 1585
b775012e
LM
1586/* Return true if BPT is either a software breakpoint or a hardware
1587 breakpoint. */
1588
1589int
1590is_breakpoint (const struct breakpoint *bpt)
1591{
1592 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1593 || bpt->type == bp_hardware_breakpoint
1594 || bpt->type == bp_dprintf);
b775012e
LM
1595}
1596
60e1c644
PA
1597/* Return true if BPT is of any hardware watchpoint kind. */
1598
a5606eee 1599static int
d77f58be 1600is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1601{
1602 return (bpt->type == bp_hardware_watchpoint
1603 || bpt->type == bp_read_watchpoint
1604 || bpt->type == bp_access_watchpoint);
1605}
7270d8f2 1606
60e1c644
PA
1607/* Return true if BPT is of any watchpoint kind, hardware or
1608 software. */
1609
3a5c3e22 1610int
d77f58be 1611is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1612{
1613 return (is_hardware_watchpoint (bpt)
1614 || bpt->type == bp_watchpoint);
1615}
1616
3a5c3e22
PA
1617/* Returns true if the current thread and its running state are safe
1618 to evaluate or update watchpoint B. Watchpoints on local
1619 expressions need to be evaluated in the context of the thread that
1620 was current when the watchpoint was created, and, that thread needs
1621 to be stopped to be able to select the correct frame context.
1622 Watchpoints on global expressions can be evaluated on any thread,
1623 and in any state. It is presently left to the target allowing
1624 memory accesses when threads are running. */
f6bc2008
PA
1625
1626static int
3a5c3e22 1627watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1628{
d0d8b0c6
JK
1629 return (b->base.pspace == current_program_space
1630 && (ptid_equal (b->watchpoint_thread, null_ptid)
1631 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1632 && !is_executing (inferior_ptid))));
f6bc2008
PA
1633}
1634
d0fb5eae
JK
1635/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1636 associated bp_watchpoint_scope breakpoint. */
1637
1638static void
3a5c3e22 1639watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1640{
3a5c3e22 1641 struct breakpoint *b = &w->base;
d0fb5eae
JK
1642
1643 if (b->related_breakpoint != b)
1644 {
1645 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1646 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1647 b->related_breakpoint->disposition = disp_del_at_next_stop;
1648 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1649 b->related_breakpoint = b;
1650 }
1651 b->disposition = disp_del_at_next_stop;
1652}
1653
567e1b4e
JB
1654/* Assuming that B is a watchpoint:
1655 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1656 - Evaluate expression and store the result in B->val
567e1b4e
JB
1657 - Evaluate the condition if there is one, and store the result
1658 in b->loc->cond.
a5606eee
VP
1659 - Update the list of values that must be watched in B->loc.
1660
4a64f543
MS
1661 If the watchpoint disposition is disp_del_at_next_stop, then do
1662 nothing. If this is local watchpoint that is out of scope, delete
1663 it.
1664
1665 Even with `set breakpoint always-inserted on' the watchpoints are
1666 removed + inserted on each stop here. Normal breakpoints must
1667 never be removed because they might be missed by a running thread
1668 when debugging in non-stop mode. On the other hand, hardware
1669 watchpoints (is_hardware_watchpoint; processed here) are specific
1670 to each LWP since they are stored in each LWP's hardware debug
1671 registers. Therefore, such LWP must be stopped first in order to
1672 be able to modify its hardware watchpoints.
1673
1674 Hardware watchpoints must be reset exactly once after being
1675 presented to the user. It cannot be done sooner, because it would
1676 reset the data used to present the watchpoint hit to the user. And
1677 it must not be done later because it could display the same single
1678 watchpoint hit during multiple GDB stops. Note that the latter is
1679 relevant only to the hardware watchpoint types bp_read_watchpoint
1680 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1681 not user-visible - its hit is suppressed if the memory content has
1682 not changed.
1683
1684 The following constraints influence the location where we can reset
1685 hardware watchpoints:
1686
1687 * target_stopped_by_watchpoint and target_stopped_data_address are
1688 called several times when GDB stops.
1689
1690 [linux]
1691 * Multiple hardware watchpoints can be hit at the same time,
1692 causing GDB to stop. GDB only presents one hardware watchpoint
1693 hit at a time as the reason for stopping, and all the other hits
1694 are presented later, one after the other, each time the user
1695 requests the execution to be resumed. Execution is not resumed
1696 for the threads still having pending hit event stored in
1697 LWP_INFO->STATUS. While the watchpoint is already removed from
1698 the inferior on the first stop the thread hit event is kept being
1699 reported from its cached value by linux_nat_stopped_data_address
1700 until the real thread resume happens after the watchpoint gets
1701 presented and thus its LWP_INFO->STATUS gets reset.
1702
1703 Therefore the hardware watchpoint hit can get safely reset on the
1704 watchpoint removal from inferior. */
a79d3c27 1705
b40ce68a 1706static void
3a5c3e22 1707update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1708{
a5606eee 1709 int within_current_scope;
a5606eee 1710 struct frame_id saved_frame_id;
66076460 1711 int frame_saved;
a5606eee 1712
f6bc2008
PA
1713 /* If this is a local watchpoint, we only want to check if the
1714 watchpoint frame is in scope if the current thread is the thread
1715 that was used to create the watchpoint. */
1716 if (!watchpoint_in_thread_scope (b))
1717 return;
1718
3a5c3e22 1719 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1720 return;
1721
66076460 1722 frame_saved = 0;
a5606eee
VP
1723
1724 /* Determine if the watchpoint is within scope. */
1725 if (b->exp_valid_block == NULL)
1726 within_current_scope = 1;
1727 else
1728 {
b5db5dfc
UW
1729 struct frame_info *fi = get_current_frame ();
1730 struct gdbarch *frame_arch = get_frame_arch (fi);
1731 CORE_ADDR frame_pc = get_frame_pc (fi);
1732
1733 /* If we're in a function epilogue, unwinding may not work
1734 properly, so do not attempt to recreate locations at this
1735 point. See similar comments in watchpoint_check. */
1736 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1737 return;
66076460
DJ
1738
1739 /* Save the current frame's ID so we can restore it after
1740 evaluating the watchpoint expression on its own frame. */
1741 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1742 took a frame parameter, so that we didn't have to change the
1743 selected frame. */
1744 frame_saved = 1;
1745 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1746
a5606eee
VP
1747 fi = frame_find_by_id (b->watchpoint_frame);
1748 within_current_scope = (fi != NULL);
1749 if (within_current_scope)
1750 select_frame (fi);
1751 }
1752
b5db5dfc
UW
1753 /* We don't free locations. They are stored in the bp_location array
1754 and update_global_location_list will eventually delete them and
1755 remove breakpoints if needed. */
3a5c3e22 1756 b->base.loc = NULL;
b5db5dfc 1757
a5606eee
VP
1758 if (within_current_scope && reparse)
1759 {
bbc13ae3 1760 const char *s;
d63d0675 1761
a5606eee
VP
1762 if (b->exp)
1763 {
1764 xfree (b->exp);
1765 b->exp = NULL;
1766 }
d63d0675 1767 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1768 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1769 /* If the meaning of expression itself changed, the old value is
1770 no longer relevant. We don't want to report a watchpoint hit
1771 to the user when the old value and the new value may actually
1772 be completely different objects. */
1773 value_free (b->val);
fa4727a6
DJ
1774 b->val = NULL;
1775 b->val_valid = 0;
60e1c644
PA
1776
1777 /* Note that unlike with breakpoints, the watchpoint's condition
1778 expression is stored in the breakpoint object, not in the
1779 locations (re)created below. */
3a5c3e22 1780 if (b->base.cond_string != NULL)
60e1c644
PA
1781 {
1782 if (b->cond_exp != NULL)
1783 {
1784 xfree (b->cond_exp);
1785 b->cond_exp = NULL;
1786 }
1787
3a5c3e22 1788 s = b->base.cond_string;
1bb9788d 1789 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1790 }
a5606eee 1791 }
a5606eee
VP
1792
1793 /* If we failed to parse the expression, for example because
1794 it refers to a global variable in a not-yet-loaded shared library,
1795 don't try to insert watchpoint. We don't automatically delete
1796 such watchpoint, though, since failure to parse expression
1797 is different from out-of-scope watchpoint. */
2d134ed3
PA
1798 if ( !target_has_execution)
1799 {
1800 /* Without execution, memory can't change. No use to try and
1801 set watchpoint locations. The watchpoint will be reset when
1802 the target gains execution, through breakpoint_re_set. */
1803 }
1804 else if (within_current_scope && b->exp)
a5606eee 1805 {
0cf6dd15 1806 int pc = 0;
fa4727a6 1807 struct value *val_chain, *v, *result, *next;
2d134ed3 1808 struct program_space *frame_pspace;
a5606eee 1809
3a1115a0 1810 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1811
a5606eee
VP
1812 /* Avoid setting b->val if it's already set. The meaning of
1813 b->val is 'the last value' user saw, and we should update
1814 it only if we reported that last value to user. As it
9c06b0b4
TJB
1815 happens, the code that reports it updates b->val directly.
1816 We don't keep track of the memory value for masked
1817 watchpoints. */
3a5c3e22 1818 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1819 {
1820 b->val = v;
1821 b->val_valid = 1;
1822 }
a5606eee 1823
2d134ed3
PA
1824 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1825
a5606eee 1826 /* Look at each value on the value chain. */
9fa40276 1827 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1828 {
1829 /* If it's a memory location, and GDB actually needed
1830 its contents to evaluate the expression, then we
fa4727a6
DJ
1831 must watch it. If the first value returned is
1832 still lazy, that means an error occurred reading it;
1833 watch it anyway in case it becomes readable. */
a5606eee 1834 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1835 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1836 {
1837 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1838
a5606eee
VP
1839 /* We only watch structs and arrays if user asked
1840 for it explicitly, never if they just happen to
1841 appear in the middle of some value chain. */
fa4727a6 1842 if (v == result
a5606eee
VP
1843 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1844 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1845 {
1846 CORE_ADDR addr;
744a8059 1847 int type;
a5606eee
VP
1848 struct bp_location *loc, **tmp;
1849
42ae5230 1850 addr = value_address (v);
a5606eee 1851 type = hw_write;
3a5c3e22 1852 if (b->base.type == bp_read_watchpoint)
a5606eee 1853 type = hw_read;
3a5c3e22 1854 else if (b->base.type == bp_access_watchpoint)
a5606eee 1855 type = hw_access;
3a5c3e22
PA
1856
1857 loc = allocate_bp_location (&b->base);
1858 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1859 ;
1860 *tmp = loc;
a6d9a66e 1861 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1862
1863 loc->pspace = frame_pspace;
a5606eee 1864 loc->address = addr;
744a8059 1865 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1866 loc->watchpoint_type = type;
1867 }
1868 }
9fa40276
TJB
1869 }
1870
1871 /* Change the type of breakpoint between hardware assisted or
1872 an ordinary watchpoint depending on the hardware support
1873 and free hardware slots. REPARSE is set when the inferior
1874 is started. */
a9634178 1875 if (reparse)
9fa40276 1876 {
e09342b5 1877 int reg_cnt;
9fa40276
TJB
1878 enum bp_loc_type loc_type;
1879 struct bp_location *bl;
a5606eee 1880
a9634178 1881 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1882
1883 if (reg_cnt)
9fa40276
TJB
1884 {
1885 int i, target_resources_ok, other_type_used;
a1398e0c 1886 enum bptype type;
9fa40276 1887
a9634178
TJB
1888 /* Use an exact watchpoint when there's only one memory region to be
1889 watched, and only one debug register is needed to watch it. */
1890 b->exact = target_exact_watchpoints && reg_cnt == 1;
1891
9fa40276 1892 /* We need to determine how many resources are already
e09342b5
TJB
1893 used for all other hardware watchpoints plus this one
1894 to see if we still have enough resources to also fit
a1398e0c
PA
1895 this watchpoint in as well. */
1896
1897 /* If this is a software watchpoint, we try to turn it
1898 to a hardware one -- count resources as if B was of
1899 hardware watchpoint type. */
1900 type = b->base.type;
1901 if (type == bp_watchpoint)
1902 type = bp_hardware_watchpoint;
1903
1904 /* This watchpoint may or may not have been placed on
1905 the list yet at this point (it won't be in the list
1906 if we're trying to create it for the first time,
1907 through watch_command), so always account for it
1908 manually. */
1909
1910 /* Count resources used by all watchpoints except B. */
1911 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1912
1913 /* Add in the resources needed for B. */
1914 i += hw_watchpoint_use_count (&b->base);
1915
1916 target_resources_ok
1917 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1918 if (target_resources_ok <= 0)
a9634178 1919 {
3a5c3e22 1920 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1921
1922 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1923 error (_("Target does not support this type of "
1924 "hardware watchpoint."));
9c06b0b4
TJB
1925 else if (target_resources_ok < 0 && !sw_mode)
1926 error (_("There are not enough available hardware "
1927 "resources for this watchpoint."));
a1398e0c
PA
1928
1929 /* Downgrade to software watchpoint. */
1930 b->base.type = bp_watchpoint;
1931 }
1932 else
1933 {
1934 /* If this was a software watchpoint, we've just
1935 found we have enough resources to turn it to a
1936 hardware watchpoint. Otherwise, this is a
1937 nop. */
1938 b->base.type = type;
a9634178 1939 }
9fa40276 1940 }
3a5c3e22 1941 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1942 error (_("Expression cannot be implemented with "
1943 "read/access watchpoint."));
9fa40276 1944 else
3a5c3e22 1945 b->base.type = bp_watchpoint;
9fa40276 1946
3a5c3e22 1947 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1948 : bp_loc_hardware_watchpoint);
3a5c3e22 1949 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1950 bl->loc_type = loc_type;
1951 }
1952
1953 for (v = val_chain; v; v = next)
1954 {
a5606eee
VP
1955 next = value_next (v);
1956 if (v != b->val)
1957 value_free (v);
1958 }
1959
c7437ca6
PA
1960 /* If a software watchpoint is not watching any memory, then the
1961 above left it without any location set up. But,
1962 bpstat_stop_status requires a location to be able to report
1963 stops, so make sure there's at least a dummy one. */
3a5c3e22 1964 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1965 {
3a5c3e22
PA
1966 struct breakpoint *base = &b->base;
1967 base->loc = allocate_bp_location (base);
1968 base->loc->pspace = frame_pspace;
1969 base->loc->address = -1;
1970 base->loc->length = -1;
1971 base->loc->watchpoint_type = -1;
c7437ca6 1972 }
a5606eee
VP
1973 }
1974 else if (!within_current_scope)
7270d8f2 1975 {
ac74f770
MS
1976 printf_filtered (_("\
1977Watchpoint %d deleted because the program has left the block\n\
1978in which its expression is valid.\n"),
3a5c3e22 1979 b->base.number);
d0fb5eae 1980 watchpoint_del_at_next_stop (b);
7270d8f2 1981 }
a5606eee
VP
1982
1983 /* Restore the selected frame. */
66076460
DJ
1984 if (frame_saved)
1985 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1986}
1987
a5606eee 1988
74960c60 1989/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1990 inserted in the inferior. We don't differentiate the type of BL's owner
1991 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1992 breakpoint_ops is not defined, because in insert_bp_location,
1993 tracepoint's insert_location will not be called. */
74960c60 1994static int
35df4500 1995should_be_inserted (struct bp_location *bl)
74960c60 1996{
35df4500 1997 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1998 return 0;
1999
35df4500 2000 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2001 return 0;
2002
35df4500 2003 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2004 return 0;
2005
f8eba3c6
TT
2006 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2007 return 0;
2008
56710373
PA
2009 /* This is set for example, when we're attached to the parent of a
2010 vfork, and have detached from the child. The child is running
2011 free, and we expect it to do an exec or exit, at which point the
2012 OS makes the parent schedulable again (and the target reports
2013 that the vfork is done). Until the child is done with the shared
2014 memory region, do not insert breakpoints in the parent, otherwise
2015 the child could still trip on the parent's breakpoints. Since
2016 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2017 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2018 return 0;
2019
74960c60
VP
2020 return 1;
2021}
2022
934709f0
PW
2023/* Same as should_be_inserted but does the check assuming
2024 that the location is not duplicated. */
2025
2026static int
2027unduplicated_should_be_inserted (struct bp_location *bl)
2028{
2029 int result;
2030 const int save_duplicate = bl->duplicate;
2031
2032 bl->duplicate = 0;
2033 result = should_be_inserted (bl);
2034 bl->duplicate = save_duplicate;
2035 return result;
2036}
2037
b775012e
LM
2038/* Parses a conditional described by an expression COND into an
2039 agent expression bytecode suitable for evaluation
2040 by the bytecode interpreter. Return NULL if there was
2041 any error during parsing. */
2042
2043static struct agent_expr *
2044parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2045{
2046 struct agent_expr *aexpr = NULL;
b775012e
LM
2047 volatile struct gdb_exception ex;
2048
2049 if (!cond)
2050 return NULL;
2051
2052 /* We don't want to stop processing, so catch any errors
2053 that may show up. */
2054 TRY_CATCH (ex, RETURN_MASK_ERROR)
2055 {
2056 aexpr = gen_eval_for_expr (scope, cond);
2057 }
2058
2059 if (ex.reason < 0)
2060 {
2061 /* If we got here, it means the condition could not be parsed to a valid
2062 bytecode expression and thus can't be evaluated on the target's side.
2063 It's no use iterating through the conditions. */
2064 return NULL;
2065 }
2066
2067 /* We have a valid agent expression. */
2068 return aexpr;
2069}
2070
2071/* Based on location BL, create a list of breakpoint conditions to be
2072 passed on to the target. If we have duplicated locations with different
2073 conditions, we will add such conditions to the list. The idea is that the
2074 target will evaluate the list of conditions and will only notify GDB when
2075 one of them is true. */
2076
2077static void
2078build_target_condition_list (struct bp_location *bl)
2079{
2080 struct bp_location **locp = NULL, **loc2p;
2081 int null_condition_or_parse_error = 0;
2082 int modified = bl->needs_update;
2083 struct bp_location *loc;
2084
2085 /* This is only meaningful if the target is
2086 evaluating conditions and if the user has
2087 opted for condition evaluation on the target's
2088 side. */
2089 if (gdb_evaluates_breakpoint_condition_p ()
2090 || !target_supports_evaluation_of_breakpoint_conditions ())
2091 return;
2092
2093 /* Do a first pass to check for locations with no assigned
2094 conditions or conditions that fail to parse to a valid agent expression
2095 bytecode. If any of these happen, then it's no use to send conditions
2096 to the target since this location will always trigger and generate a
2097 response back to GDB. */
2098 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2099 {
2100 loc = (*loc2p);
2101 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2102 {
2103 if (modified)
2104 {
2105 struct agent_expr *aexpr;
2106
2107 /* Re-parse the conditions since something changed. In that
2108 case we already freed the condition bytecodes (see
2109 force_breakpoint_reinsertion). We just
2110 need to parse the condition to bytecodes again. */
2111 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2112 loc->cond_bytecode = aexpr;
2113
2114 /* Check if we managed to parse the conditional expression
2115 correctly. If not, we will not send this condition
2116 to the target. */
2117 if (aexpr)
2118 continue;
2119 }
2120
2121 /* If we have a NULL bytecode expression, it means something
2122 went wrong or we have a null condition expression. */
2123 if (!loc->cond_bytecode)
2124 {
2125 null_condition_or_parse_error = 1;
2126 break;
2127 }
2128 }
2129 }
2130
2131 /* If any of these happened, it means we will have to evaluate the conditions
2132 for the location's address on gdb's side. It is no use keeping bytecodes
2133 for all the other duplicate locations, thus we free all of them here.
2134
2135 This is so we have a finer control over which locations' conditions are
2136 being evaluated by GDB or the remote stub. */
2137 if (null_condition_or_parse_error)
2138 {
2139 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2140 {
2141 loc = (*loc2p);
2142 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2143 {
2144 /* Only go as far as the first NULL bytecode is
2145 located. */
2146 if (!loc->cond_bytecode)
2147 return;
2148
2149 free_agent_expr (loc->cond_bytecode);
2150 loc->cond_bytecode = NULL;
2151 }
2152 }
2153 }
2154
2155 /* No NULL conditions or failed bytecode generation. Build a condition list
2156 for this location's address. */
2157 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2158 {
2159 loc = (*loc2p);
2160 if (loc->cond
2161 && is_breakpoint (loc->owner)
2162 && loc->pspace->num == bl->pspace->num
2163 && loc->owner->enable_state == bp_enabled
2164 && loc->enabled)
2165 /* Add the condition to the vector. This will be used later to send the
2166 conditions to the target. */
2167 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2168 loc->cond_bytecode);
2169 }
2170
2171 return;
2172}
2173
d3ce09f5
SS
2174/* Parses a command described by string CMD into an agent expression
2175 bytecode suitable for evaluation by the bytecode interpreter.
2176 Return NULL if there was any error during parsing. */
2177
2178static struct agent_expr *
2179parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2180{
2181 struct cleanup *old_cleanups = 0;
2182 struct expression *expr, **argvec;
2183 struct agent_expr *aexpr = NULL;
d3ce09f5 2184 volatile struct gdb_exception ex;
bbc13ae3
KS
2185 const char *cmdrest;
2186 const char *format_start, *format_end;
d3ce09f5
SS
2187 struct format_piece *fpieces;
2188 int nargs;
2189 struct gdbarch *gdbarch = get_current_arch ();
2190
2191 if (!cmd)
2192 return NULL;
2193
2194 cmdrest = cmd;
2195
2196 if (*cmdrest == ',')
2197 ++cmdrest;
bbc13ae3 2198 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2199
2200 if (*cmdrest++ != '"')
2201 error (_("No format string following the location"));
2202
2203 format_start = cmdrest;
2204
2205 fpieces = parse_format_string (&cmdrest);
2206
2207 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2208
2209 format_end = cmdrest;
2210
2211 if (*cmdrest++ != '"')
2212 error (_("Bad format string, non-terminated '\"'."));
2213
bbc13ae3 2214 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2215
2216 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2217 error (_("Invalid argument syntax"));
2218
2219 if (*cmdrest == ',')
2220 cmdrest++;
bbc13ae3 2221 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2222
2223 /* For each argument, make an expression. */
2224
2225 argvec = (struct expression **) alloca (strlen (cmd)
2226 * sizeof (struct expression *));
2227
2228 nargs = 0;
2229 while (*cmdrest != '\0')
2230 {
bbc13ae3 2231 const char *cmd1;
d3ce09f5
SS
2232
2233 cmd1 = cmdrest;
2234 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2235 argvec[nargs++] = expr;
2236 cmdrest = cmd1;
2237 if (*cmdrest == ',')
2238 ++cmdrest;
2239 }
2240
2241 /* We don't want to stop processing, so catch any errors
2242 that may show up. */
2243 TRY_CATCH (ex, RETURN_MASK_ERROR)
2244 {
2245 aexpr = gen_printf (scope, gdbarch, 0, 0,
2246 format_start, format_end - format_start,
2247 fpieces, nargs, argvec);
2248 }
2249
752eb8b4
TT
2250 do_cleanups (old_cleanups);
2251
d3ce09f5
SS
2252 if (ex.reason < 0)
2253 {
2254 /* If we got here, it means the command could not be parsed to a valid
2255 bytecode expression and thus can't be evaluated on the target's side.
2256 It's no use iterating through the other commands. */
2257 return NULL;
2258 }
2259
d3ce09f5
SS
2260 /* We have a valid agent expression, return it. */
2261 return aexpr;
2262}
2263
2264/* Based on location BL, create a list of breakpoint commands to be
2265 passed on to the target. If we have duplicated locations with
2266 different commands, we will add any such to the list. */
2267
2268static void
2269build_target_command_list (struct bp_location *bl)
2270{
2271 struct bp_location **locp = NULL, **loc2p;
2272 int null_command_or_parse_error = 0;
2273 int modified = bl->needs_update;
2274 struct bp_location *loc;
2275
2276 /* For now, limit to agent-style dprintf breakpoints. */
2277 if (bl->owner->type != bp_dprintf
2278 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2279 return;
2280
2281 if (!target_can_run_breakpoint_commands ())
2282 return;
2283
2284 /* Do a first pass to check for locations with no assigned
2285 conditions or conditions that fail to parse to a valid agent expression
2286 bytecode. If any of these happen, then it's no use to send conditions
2287 to the target since this location will always trigger and generate a
2288 response back to GDB. */
2289 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2290 {
2291 loc = (*loc2p);
2292 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2293 {
2294 if (modified)
2295 {
2296 struct agent_expr *aexpr;
2297
2298 /* Re-parse the commands since something changed. In that
2299 case we already freed the command bytecodes (see
2300 force_breakpoint_reinsertion). We just
2301 need to parse the command to bytecodes again. */
2302 aexpr = parse_cmd_to_aexpr (bl->address,
2303 loc->owner->extra_string);
2304 loc->cmd_bytecode = aexpr;
2305
2306 if (!aexpr)
2307 continue;
2308 }
2309
2310 /* If we have a NULL bytecode expression, it means something
2311 went wrong or we have a null command expression. */
2312 if (!loc->cmd_bytecode)
2313 {
2314 null_command_or_parse_error = 1;
2315 break;
2316 }
2317 }
2318 }
2319
2320 /* If anything failed, then we're not doing target-side commands,
2321 and so clean up. */
2322 if (null_command_or_parse_error)
2323 {
2324 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2325 {
2326 loc = (*loc2p);
2327 if (is_breakpoint (loc->owner)
2328 && loc->pspace->num == bl->pspace->num)
2329 {
2330 /* Only go as far as the first NULL bytecode is
2331 located. */
40fb6c5e 2332 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2333 return;
2334
40fb6c5e
HZ
2335 free_agent_expr (loc->cmd_bytecode);
2336 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2337 }
2338 }
2339 }
2340
2341 /* No NULL commands or failed bytecode generation. Build a command list
2342 for this location's address. */
2343 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344 {
2345 loc = (*loc2p);
2346 if (loc->owner->extra_string
2347 && is_breakpoint (loc->owner)
2348 && loc->pspace->num == bl->pspace->num
2349 && loc->owner->enable_state == bp_enabled
2350 && loc->enabled)
2351 /* Add the command to the vector. This will be used later
2352 to send the commands to the target. */
2353 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2354 loc->cmd_bytecode);
2355 }
2356
2357 bl->target_info.persist = 0;
2358 /* Maybe flag this location as persistent. */
2359 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2360 bl->target_info.persist = 1;
2361}
2362
35df4500
TJB
2363/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2364 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2365 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2366 Returns 0 for success, 1 if the bp_location type is not supported or
2367 -1 for failure.
879bfdc2 2368
4a64f543
MS
2369 NOTE drow/2003-09-09: This routine could be broken down to an
2370 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2371static int
35df4500 2372insert_bp_location (struct bp_location *bl,
26bb91f3 2373 struct ui_file *tmp_error_stream,
3fbb6ffa 2374 int *disabled_breaks,
dd61ec5c
MW
2375 int *hw_breakpoint_error,
2376 int *hw_bp_error_explained_already)
879bfdc2
DJ
2377{
2378 int val = 0;
dd61ec5c
MW
2379 char *hw_bp_err_string = NULL;
2380 struct gdb_exception e;
879bfdc2 2381
b775012e 2382 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2383 return 0;
2384
35c63cd8
JB
2385 /* Note we don't initialize bl->target_info, as that wipes out
2386 the breakpoint location's shadow_contents if the breakpoint
2387 is still inserted at that location. This in turn breaks
2388 target_read_memory which depends on these buffers when
2389 a memory read is requested at the breakpoint location:
2390 Once the target_info has been wiped, we fail to see that
2391 we have a breakpoint inserted at that address and thus
2392 read the breakpoint instead of returning the data saved in
2393 the breakpoint location's shadow contents. */
35df4500
TJB
2394 bl->target_info.placed_address = bl->address;
2395 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2396 bl->target_info.length = bl->length;
8181d85f 2397
b775012e
LM
2398 /* When working with target-side conditions, we must pass all the conditions
2399 for the same breakpoint address down to the target since GDB will not
2400 insert those locations. With a list of breakpoint conditions, the target
2401 can decide when to stop and notify GDB. */
2402
2403 if (is_breakpoint (bl->owner))
2404 {
2405 build_target_condition_list (bl);
d3ce09f5
SS
2406 build_target_command_list (bl);
2407 /* Reset the modification marker. */
b775012e
LM
2408 bl->needs_update = 0;
2409 }
2410
35df4500
TJB
2411 if (bl->loc_type == bp_loc_software_breakpoint
2412 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2413 {
35df4500 2414 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2415 {
2416 /* If the explicitly specified breakpoint type
2417 is not hardware breakpoint, check the memory map to see
2418 if the breakpoint address is in read only memory or not.
4a64f543 2419
765dc015
VP
2420 Two important cases are:
2421 - location type is not hardware breakpoint, memory
2422 is readonly. We change the type of the location to
2423 hardware breakpoint.
4a64f543
MS
2424 - location type is hardware breakpoint, memory is
2425 read-write. This means we've previously made the
2426 location hardware one, but then the memory map changed,
2427 so we undo.
765dc015 2428
4a64f543
MS
2429 When breakpoints are removed, remove_breakpoints will use
2430 location types we've just set here, the only possible
2431 problem is that memory map has changed during running
2432 program, but it's not going to work anyway with current
2433 gdb. */
765dc015 2434 struct mem_region *mr
35df4500 2435 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2436
2437 if (mr)
2438 {
2439 if (automatic_hardware_breakpoints)
2440 {
765dc015
VP
2441 enum bp_loc_type new_type;
2442
2443 if (mr->attrib.mode != MEM_RW)
2444 new_type = bp_loc_hardware_breakpoint;
2445 else
2446 new_type = bp_loc_software_breakpoint;
2447
35df4500 2448 if (new_type != bl->loc_type)
765dc015
VP
2449 {
2450 static int said = 0;
cc59ec59 2451
35df4500 2452 bl->loc_type = new_type;
765dc015
VP
2453 if (!said)
2454 {
3e43a32a
MS
2455 fprintf_filtered (gdb_stdout,
2456 _("Note: automatically using "
2457 "hardware breakpoints for "
2458 "read-only addresses.\n"));
765dc015
VP
2459 said = 1;
2460 }
2461 }
2462 }
35df4500 2463 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2464 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2465 warning (_("cannot set software breakpoint "
2466 "at readonly address %s"),
35df4500 2467 paddress (bl->gdbarch, bl->address));
765dc015
VP
2468 }
2469 }
2470
879bfdc2
DJ
2471 /* First check to see if we have to handle an overlay. */
2472 if (overlay_debugging == ovly_off
35df4500
TJB
2473 || bl->section == NULL
2474 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2475 {
2476 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2477 TRY_CATCH (e, RETURN_MASK_ALL)
2478 {
2479 val = bl->owner->ops->insert_location (bl);
2480 }
2481 if (e.reason < 0)
2482 {
2483 val = 1;
2484 hw_bp_err_string = (char *) e.message;
2485 }
879bfdc2
DJ
2486 }
2487 else
2488 {
4a64f543 2489 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2490 Shall we set a breakpoint at the LMA? */
2491 if (!overlay_events_enabled)
2492 {
2493 /* Yes -- overlay event support is not active,
2494 so we must try to set a breakpoint at the LMA.
2495 This will not work for a hardware breakpoint. */
35df4500 2496 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2497 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2498 bl->owner->number);
879bfdc2
DJ
2499 else
2500 {
35df4500
TJB
2501 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2502 bl->section);
879bfdc2 2503 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2504 bl->overlay_target_info = bl->target_info;
2505 bl->overlay_target_info.placed_address = addr;
2506 val = target_insert_breakpoint (bl->gdbarch,
2507 &bl->overlay_target_info);
879bfdc2 2508 if (val != 0)
99361f52 2509 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2510 "Overlay breakpoint %d "
2511 "failed: in ROM?\n",
35df4500 2512 bl->owner->number);
879bfdc2
DJ
2513 }
2514 }
2515 /* Shall we set a breakpoint at the VMA? */
35df4500 2516 if (section_is_mapped (bl->section))
879bfdc2
DJ
2517 {
2518 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2519 TRY_CATCH (e, RETURN_MASK_ALL)
2520 {
2521 val = bl->owner->ops->insert_location (bl);
2522 }
2523 if (e.reason < 0)
2524 {
2525 val = 1;
2526 hw_bp_err_string = (char *) e.message;
2527 }
879bfdc2
DJ
2528 }
2529 else
2530 {
2531 /* No. This breakpoint will not be inserted.
2532 No error, but do not mark the bp as 'inserted'. */
2533 return 0;
2534 }
2535 }
2536
2537 if (val)
2538 {
2539 /* Can't set the breakpoint. */
35df4500 2540 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2541 {
4a64f543 2542 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2543 val = 0;
35df4500 2544 bl->shlib_disabled = 1;
8d3788bd 2545 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2546 if (!*disabled_breaks)
2547 {
2548 fprintf_unfiltered (tmp_error_stream,
2549 "Cannot insert breakpoint %d.\n",
2550 bl->owner->number);
2551 fprintf_unfiltered (tmp_error_stream,
2552 "Temporarily disabling shared "
2553 "library breakpoints:\n");
2554 }
2555 *disabled_breaks = 1;
879bfdc2 2556 fprintf_unfiltered (tmp_error_stream,
35df4500 2557 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2558 }
2559 else
879bfdc2 2560 {
35df4500 2561 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2562 {
dd61ec5c
MW
2563 *hw_breakpoint_error = 1;
2564 *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2565 fprintf_unfiltered (tmp_error_stream,
2566 "Cannot insert hardware breakpoint %d%s",
2567 bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2568 if (hw_bp_err_string)
2569 fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
879bfdc2
DJ
2570 }
2571 else
2572 {
2573 fprintf_unfiltered (tmp_error_stream,
2574 "Cannot insert breakpoint %d.\n",
35df4500 2575 bl->owner->number);
879bfdc2
DJ
2576 fprintf_filtered (tmp_error_stream,
2577 "Error accessing memory address ");
35df4500 2578 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2579 tmp_error_stream);
879bfdc2
DJ
2580 fprintf_filtered (tmp_error_stream, ": %s.\n",
2581 safe_strerror (val));
2582 }
2583
2584 }
2585 }
2586 else
35df4500 2587 bl->inserted = 1;
879bfdc2
DJ
2588
2589 return val;
2590 }
2591
35df4500 2592 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2593 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2594 watchpoints. It's not clear that it's necessary... */
35df4500 2595 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2596 {
77b06cd7
TJB
2597 gdb_assert (bl->owner->ops != NULL
2598 && bl->owner->ops->insert_location != NULL);
2599
2600 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2601
2602 /* If trying to set a read-watchpoint, and it turns out it's not
2603 supported, try emulating one with an access watchpoint. */
35df4500 2604 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2605 {
2606 struct bp_location *loc, **loc_temp;
2607
2608 /* But don't try to insert it, if there's already another
2609 hw_access location that would be considered a duplicate
2610 of this one. */
2611 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2612 if (loc != bl
85d721b8 2613 && loc->watchpoint_type == hw_access
35df4500 2614 && watchpoint_locations_match (bl, loc))
85d721b8 2615 {
35df4500
TJB
2616 bl->duplicate = 1;
2617 bl->inserted = 1;
2618 bl->target_info = loc->target_info;
2619 bl->watchpoint_type = hw_access;
85d721b8
PA
2620 val = 0;
2621 break;
2622 }
2623
2624 if (val == 1)
2625 {
77b06cd7
TJB
2626 bl->watchpoint_type = hw_access;
2627 val = bl->owner->ops->insert_location (bl);
2628
2629 if (val)
2630 /* Back to the original value. */
2631 bl->watchpoint_type = hw_read;
85d721b8
PA
2632 }
2633 }
2634
35df4500 2635 bl->inserted = (val == 0);
879bfdc2
DJ
2636 }
2637
35df4500 2638 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2639 {
77b06cd7
TJB
2640 gdb_assert (bl->owner->ops != NULL
2641 && bl->owner->ops->insert_location != NULL);
2642
2643 val = bl->owner->ops->insert_location (bl);
2644 if (val)
2645 {
2646 bl->owner->enable_state = bp_disabled;
2647
2648 if (val == 1)
2649 warning (_("\
2650Error inserting catchpoint %d: Your system does not support this type\n\
2651of catchpoint."), bl->owner->number);
2652 else
2653 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2654 }
2655
2656 bl->inserted = (val == 0);
1640b821
DJ
2657
2658 /* We've already printed an error message if there was a problem
2659 inserting this catchpoint, and we've disabled the catchpoint,
2660 so just return success. */
2661 return 0;
879bfdc2
DJ
2662 }
2663
2664 return 0;
2665}
2666
6c95b8df
PA
2667/* This function is called when program space PSPACE is about to be
2668 deleted. It takes care of updating breakpoints to not reference
2669 PSPACE anymore. */
2670
2671void
2672breakpoint_program_space_exit (struct program_space *pspace)
2673{
2674 struct breakpoint *b, *b_temp;
876fa593 2675 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2676
2677 /* Remove any breakpoint that was set through this program space. */
2678 ALL_BREAKPOINTS_SAFE (b, b_temp)
2679 {
2680 if (b->pspace == pspace)
2681 delete_breakpoint (b);
2682 }
2683
2684 /* Breakpoints set through other program spaces could have locations
2685 bound to PSPACE as well. Remove those. */
876fa593 2686 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2687 {
2688 struct bp_location *tmp;
2689
2690 if (loc->pspace == pspace)
2691 {
2bdf28a0 2692 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2693 if (loc->owner->loc == loc)
2694 loc->owner->loc = loc->next;
2695 else
2696 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2697 if (tmp->next == loc)
2698 {
2699 tmp->next = loc->next;
2700 break;
2701 }
2702 }
2703 }
2704
2705 /* Now update the global location list to permanently delete the
2706 removed locations above. */
2707 update_global_location_list (0);
2708}
2709
74960c60
VP
2710/* Make sure all breakpoints are inserted in inferior.
2711 Throws exception on any error.
2712 A breakpoint that is already inserted won't be inserted
2713 again, so calling this function twice is safe. */
2714void
2715insert_breakpoints (void)
2716{
2717 struct breakpoint *bpt;
2718
2719 ALL_BREAKPOINTS (bpt)
2720 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2721 {
2722 struct watchpoint *w = (struct watchpoint *) bpt;
2723
2724 update_watchpoint (w, 0 /* don't reparse. */);
2725 }
74960c60 2726
b60e7edf 2727 update_global_location_list (1);
74960c60 2728
c35b1492
PA
2729 /* update_global_location_list does not insert breakpoints when
2730 always_inserted_mode is not enabled. Explicitly insert them
2731 now. */
2732 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2733 insert_breakpoint_locations ();
2734}
2735
20388dd6
YQ
2736/* Invoke CALLBACK for each of bp_location. */
2737
2738void
2739iterate_over_bp_locations (walk_bp_location_callback callback)
2740{
2741 struct bp_location *loc, **loc_tmp;
2742
2743 ALL_BP_LOCATIONS (loc, loc_tmp)
2744 {
2745 callback (loc, NULL);
2746 }
2747}
2748
b775012e
LM
2749/* This is used when we need to synch breakpoint conditions between GDB and the
2750 target. It is the case with deleting and disabling of breakpoints when using
2751 always-inserted mode. */
2752
2753static void
2754update_inserted_breakpoint_locations (void)
2755{
2756 struct bp_location *bl, **blp_tmp;
2757 int error_flag = 0;
2758 int val = 0;
2759 int disabled_breaks = 0;
2760 int hw_breakpoint_error = 0;
dd61ec5c 2761 int hw_bp_details_reported = 0;
b775012e
LM
2762
2763 struct ui_file *tmp_error_stream = mem_fileopen ();
2764 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2765
2766 /* Explicitly mark the warning -- this will only be printed if
2767 there was an error. */
2768 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2769
2770 save_current_space_and_thread ();
2771
2772 ALL_BP_LOCATIONS (bl, blp_tmp)
2773 {
2774 /* We only want to update software breakpoints and hardware
2775 breakpoints. */
2776 if (!is_breakpoint (bl->owner))
2777 continue;
2778
2779 /* We only want to update locations that are already inserted
2780 and need updating. This is to avoid unwanted insertion during
2781 deletion of breakpoints. */
2782 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2783 continue;
2784
2785 switch_to_program_space_and_thread (bl->pspace);
2786
2787 /* For targets that support global breakpoints, there's no need
2788 to select an inferior to insert breakpoint to. In fact, even
2789 if we aren't attached to any process yet, we should still
2790 insert breakpoints. */
f5656ead 2791 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2792 && ptid_equal (inferior_ptid, null_ptid))
2793 continue;
2794
2795 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2796 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2797 if (val)
2798 error_flag = val;
2799 }
2800
2801 if (error_flag)
2802 {
2803 target_terminal_ours_for_output ();
2804 error_stream (tmp_error_stream);
2805 }
2806
2807 do_cleanups (cleanups);
2808}
2809
c30eee59 2810/* Used when starting or continuing the program. */
c906108c 2811
74960c60
VP
2812static void
2813insert_breakpoint_locations (void)
c906108c 2814{
a5606eee 2815 struct breakpoint *bpt;
35df4500 2816 struct bp_location *bl, **blp_tmp;
eacd795a 2817 int error_flag = 0;
c906108c 2818 int val = 0;
3fbb6ffa 2819 int disabled_breaks = 0;
81d0cc19 2820 int hw_breakpoint_error = 0;
dd61ec5c 2821 int hw_bp_error_explained_already = 0;
c906108c 2822
81d0cc19 2823 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2824 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2825
81d0cc19
GS
2826 /* Explicitly mark the warning -- this will only be printed if
2827 there was an error. */
2828 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2829
2830 save_current_space_and_thread ();
2831
35df4500 2832 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2833 {
b775012e 2834 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2835 continue;
2836
4a64f543
MS
2837 /* There is no point inserting thread-specific breakpoints if
2838 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2839 has BL->OWNER always non-NULL. */
35df4500
TJB
2840 if (bl->owner->thread != -1
2841 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2842 continue;
2843
35df4500 2844 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2845
2846 /* For targets that support global breakpoints, there's no need
2847 to select an inferior to insert breakpoint to. In fact, even
2848 if we aren't attached to any process yet, we should still
2849 insert breakpoints. */
f5656ead 2850 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2851 && ptid_equal (inferior_ptid, null_ptid))
2852 continue;
2853
3fbb6ffa 2854 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2855 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2856 if (val)
eacd795a 2857 error_flag = val;
879bfdc2 2858 }
c906108c 2859
4a64f543
MS
2860 /* If we failed to insert all locations of a watchpoint, remove
2861 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2862 ALL_BREAKPOINTS (bpt)
2863 {
2864 int some_failed = 0;
2865 struct bp_location *loc;
2866
2867 if (!is_hardware_watchpoint (bpt))
2868 continue;
2869
d6b74ac4 2870 if (!breakpoint_enabled (bpt))
a5606eee 2871 continue;
74960c60
VP
2872
2873 if (bpt->disposition == disp_del_at_next_stop)
2874 continue;
a5606eee
VP
2875
2876 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2877 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2878 {
2879 some_failed = 1;
2880 break;
2881 }
2882 if (some_failed)
2883 {
2884 for (loc = bpt->loc; loc; loc = loc->next)
2885 if (loc->inserted)
2886 remove_breakpoint (loc, mark_uninserted);
2887
2888 hw_breakpoint_error = 1;
2889 fprintf_unfiltered (tmp_error_stream,
2890 "Could not insert hardware watchpoint %d.\n",
2891 bpt->number);
eacd795a 2892 error_flag = -1;
a5606eee
VP
2893 }
2894 }
2895
eacd795a 2896 if (error_flag)
81d0cc19
GS
2897 {
2898 /* If a hardware breakpoint or watchpoint was inserted, add a
2899 message about possibly exhausted resources. */
dd61ec5c 2900 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 2901 {
c6510018
MS
2902 fprintf_unfiltered (tmp_error_stream,
2903 "Could not insert hardware breakpoints:\n\
2904You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2905 }
81d0cc19
GS
2906 target_terminal_ours_for_output ();
2907 error_stream (tmp_error_stream);
2908 }
f7545552
TT
2909
2910 do_cleanups (cleanups);
c906108c
SS
2911}
2912
c30eee59
TJB
2913/* Used when the program stops.
2914 Returns zero if successful, or non-zero if there was a problem
2915 removing a breakpoint location. */
2916
c906108c 2917int
fba45db2 2918remove_breakpoints (void)
c906108c 2919{
35df4500 2920 struct bp_location *bl, **blp_tmp;
3a1bae8e 2921 int val = 0;
c906108c 2922
35df4500 2923 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2924 {
1e4d1764 2925 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2926 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2927 }
3a1bae8e 2928 return val;
c906108c
SS
2929}
2930
6c95b8df
PA
2931/* Remove breakpoints of process PID. */
2932
2933int
2934remove_breakpoints_pid (int pid)
2935{
35df4500 2936 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2937 int val;
2938 struct inferior *inf = find_inferior_pid (pid);
2939
35df4500 2940 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2941 {
35df4500 2942 if (bl->pspace != inf->pspace)
6c95b8df
PA
2943 continue;
2944
d3ce09f5
SS
2945 if (bl->owner->type == bp_dprintf)
2946 continue;
2947
35df4500 2948 if (bl->inserted)
6c95b8df 2949 {
35df4500 2950 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2951 if (val != 0)
2952 return val;
2953 }
2954 }
2955 return 0;
2956}
2957
c906108c 2958int
fba45db2 2959reattach_breakpoints (int pid)
c906108c 2960{
6c95b8df 2961 struct cleanup *old_chain;
35df4500 2962 struct bp_location *bl, **blp_tmp;
c906108c 2963 int val;
86b887df 2964 struct ui_file *tmp_error_stream;
dd61ec5c 2965 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
2966 struct inferior *inf;
2967 struct thread_info *tp;
2968
2969 tp = any_live_thread_of_process (pid);
2970 if (tp == NULL)
2971 return 1;
2972
2973 inf = find_inferior_pid (pid);
2974 old_chain = save_inferior_ptid ();
2975
2976 inferior_ptid = tp->ptid;
a4954f26 2977
86b887df 2978 tmp_error_stream = mem_fileopen ();
a4954f26 2979 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2980
35df4500 2981 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2982 {
35df4500 2983 if (bl->pspace != inf->pspace)
6c95b8df
PA
2984 continue;
2985
35df4500 2986 if (bl->inserted)
c5aa993b 2987 {
35df4500 2988 bl->inserted = 0;
dd61ec5c 2989 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
2990 if (val != 0)
2991 {
ce696e05 2992 do_cleanups (old_chain);
c5aa993b
JM
2993 return val;
2994 }
2995 }
2996 }
ce696e05 2997 do_cleanups (old_chain);
c906108c
SS
2998 return 0;
2999}
3000
e58b0e63
PA
3001static int internal_breakpoint_number = -1;
3002
84f4c1fe
PM
3003/* Set the breakpoint number of B, depending on the value of INTERNAL.
3004 If INTERNAL is non-zero, the breakpoint number will be populated
3005 from internal_breakpoint_number and that variable decremented.
e5dd4106 3006 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3007 breakpoint_count and that value incremented. Internal breakpoints
3008 do not set the internal var bpnum. */
3009static void
3010set_breakpoint_number (int internal, struct breakpoint *b)
3011{
3012 if (internal)
3013 b->number = internal_breakpoint_number--;
3014 else
3015 {
3016 set_breakpoint_count (breakpoint_count + 1);
3017 b->number = breakpoint_count;
3018 }
3019}
3020
e62c965a 3021static struct breakpoint *
a6d9a66e 3022create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3023 CORE_ADDR address, enum bptype type,
c0a91b2b 3024 const struct breakpoint_ops *ops)
e62c965a 3025{
e62c965a
PP
3026 struct symtab_and_line sal;
3027 struct breakpoint *b;
3028
4a64f543 3029 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3030
3031 sal.pc = address;
3032 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3033 sal.pspace = current_program_space;
e62c965a 3034
06edf0c0 3035 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3036 b->number = internal_breakpoint_number--;
3037 b->disposition = disp_donttouch;
3038
3039 return b;
3040}
3041
17450429
PP
3042static const char *const longjmp_names[] =
3043 {
3044 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3045 };
3046#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3047
3048/* Per-objfile data private to breakpoint.c. */
3049struct breakpoint_objfile_data
3050{
3051 /* Minimal symbol for "_ovly_debug_event" (if any). */
3052 struct minimal_symbol *overlay_msym;
3053
3054 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3055 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3056
28106bc2
SDJ
3057 /* True if we have looked for longjmp probes. */
3058 int longjmp_searched;
3059
3060 /* SystemTap probe points for longjmp (if any). */
3061 VEC (probe_p) *longjmp_probes;
3062
17450429
PP
3063 /* Minimal symbol for "std::terminate()" (if any). */
3064 struct minimal_symbol *terminate_msym;
3065
3066 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3067 struct minimal_symbol *exception_msym;
28106bc2
SDJ
3068
3069 /* True if we have looked for exception probes. */
3070 int exception_searched;
3071
3072 /* SystemTap probe points for unwinding (if any). */
3073 VEC (probe_p) *exception_probes;
17450429
PP
3074};
3075
3076static const struct objfile_data *breakpoint_objfile_key;
3077
3078/* Minimal symbol not found sentinel. */
3079static struct minimal_symbol msym_not_found;
3080
3081/* Returns TRUE if MSYM point to the "not found" sentinel. */
3082
3083static int
3084msym_not_found_p (const struct minimal_symbol *msym)
3085{
3086 return msym == &msym_not_found;
3087}
3088
3089/* Return per-objfile data needed by breakpoint.c.
3090 Allocate the data if necessary. */
3091
3092static struct breakpoint_objfile_data *
3093get_breakpoint_objfile_data (struct objfile *objfile)
3094{
3095 struct breakpoint_objfile_data *bp_objfile_data;
3096
3097 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3098 if (bp_objfile_data == NULL)
3099 {
3100 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3101 sizeof (*bp_objfile_data));
3102
3103 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3104 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3105 }
3106 return bp_objfile_data;
3107}
3108
28106bc2
SDJ
3109static void
3110free_breakpoint_probes (struct objfile *obj, void *data)
3111{
3112 struct breakpoint_objfile_data *bp_objfile_data = data;
3113
3114 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3115 VEC_free (probe_p, bp_objfile_data->exception_probes);
3116}
3117
e62c965a 3118static void
af02033e 3119create_overlay_event_breakpoint (void)
e62c965a 3120{
69de3c6a 3121 struct objfile *objfile;
af02033e 3122 const char *const func_name = "_ovly_debug_event";
e62c965a 3123
69de3c6a
PP
3124 ALL_OBJFILES (objfile)
3125 {
3126 struct breakpoint *b;
17450429
PP
3127 struct breakpoint_objfile_data *bp_objfile_data;
3128 CORE_ADDR addr;
69de3c6a 3129
17450429
PP
3130 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3131
3132 if (msym_not_found_p (bp_objfile_data->overlay_msym))
3133 continue;
3134
3135 if (bp_objfile_data->overlay_msym == NULL)
3136 {
3137 struct minimal_symbol *m;
3138
3139 m = lookup_minimal_symbol_text (func_name, objfile);
3140 if (m == NULL)
3141 {
3142 /* Avoid future lookups in this objfile. */
3143 bp_objfile_data->overlay_msym = &msym_not_found;
3144 continue;
3145 }
3146 bp_objfile_data->overlay_msym = m;
3147 }
e62c965a 3148
17450429
PP
3149 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3150 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3151 bp_overlay_event,
3152 &internal_breakpoint_ops);
69de3c6a 3153 b->addr_string = xstrdup (func_name);
e62c965a 3154
69de3c6a
PP
3155 if (overlay_debugging == ovly_auto)
3156 {
3157 b->enable_state = bp_enabled;
3158 overlay_events_enabled = 1;
3159 }
3160 else
3161 {
3162 b->enable_state = bp_disabled;
3163 overlay_events_enabled = 0;
3164 }
e62c965a
PP
3165 }
3166 update_global_location_list (1);
3167}
3168
0fd8e87f 3169static void
af02033e 3170create_longjmp_master_breakpoint (void)
0fd8e87f 3171{
6c95b8df 3172 struct program_space *pspace;
6c95b8df
PA
3173 struct cleanup *old_chain;
3174
3175 old_chain = save_current_program_space ();
0fd8e87f 3176
6c95b8df 3177 ALL_PSPACES (pspace)
af02033e
PP
3178 {
3179 struct objfile *objfile;
3180
3181 set_current_program_space (pspace);
3182
3183 ALL_OBJFILES (objfile)
0fd8e87f 3184 {
af02033e
PP
3185 int i;
3186 struct gdbarch *gdbarch;
17450429 3187 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3188
af02033e
PP
3189 gdbarch = get_objfile_arch (objfile);
3190 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
3191 continue;
3192
17450429
PP
3193 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3194
28106bc2
SDJ
3195 if (!bp_objfile_data->longjmp_searched)
3196 {
25f9533e
SDJ
3197 VEC (probe_p) *ret;
3198
3199 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3200 if (ret != NULL)
3201 {
3202 /* We are only interested in checking one element. */
3203 struct probe *p = VEC_index (probe_p, ret, 0);
3204
3205 if (!can_evaluate_probe_arguments (p))
3206 {
3207 /* We cannot use the probe interface here, because it does
3208 not know how to evaluate arguments. */
3209 VEC_free (probe_p, ret);
3210 ret = NULL;
3211 }
3212 }
3213 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3214 bp_objfile_data->longjmp_searched = 1;
3215 }
3216
3217 if (bp_objfile_data->longjmp_probes != NULL)
3218 {
3219 int i;
3220 struct probe *probe;
3221 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3222
3223 for (i = 0;
3224 VEC_iterate (probe_p,
3225 bp_objfile_data->longjmp_probes,
3226 i, probe);
3227 ++i)
3228 {
3229 struct breakpoint *b;
3230
3231 b = create_internal_breakpoint (gdbarch, probe->address,
3232 bp_longjmp_master,
3233 &internal_breakpoint_ops);
3234 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3235 b->enable_state = bp_disabled;
3236 }
3237
3238 continue;
3239 }
3240
17450429 3241 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3242 {
3243 struct breakpoint *b;
af02033e 3244 const char *func_name;
17450429 3245 CORE_ADDR addr;
6c95b8df 3246
17450429 3247 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 3248 continue;
0fd8e87f 3249
17450429
PP
3250 func_name = longjmp_names[i];
3251 if (bp_objfile_data->longjmp_msym[i] == NULL)
3252 {
3253 struct minimal_symbol *m;
3254
3255 m = lookup_minimal_symbol_text (func_name, objfile);
3256 if (m == NULL)
3257 {
3258 /* Prevent future lookups in this objfile. */
3259 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3260 continue;
3261 }
3262 bp_objfile_data->longjmp_msym[i] = m;
3263 }
3264
3265 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3266 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3267 &internal_breakpoint_ops);
af02033e
PP
3268 b->addr_string = xstrdup (func_name);
3269 b->enable_state = bp_disabled;
3270 }
0fd8e87f 3271 }
af02033e 3272 }
0fd8e87f 3273 update_global_location_list (1);
6c95b8df
PA
3274
3275 do_cleanups (old_chain);
0fd8e87f
UW
3276}
3277
af02033e 3278/* Create a master std::terminate breakpoint. */
aa7d318d 3279static void
af02033e 3280create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3281{
3282 struct program_space *pspace;
aa7d318d 3283 struct cleanup *old_chain;
af02033e 3284 const char *const func_name = "std::terminate()";
aa7d318d
TT
3285
3286 old_chain = save_current_program_space ();
3287
3288 ALL_PSPACES (pspace)
17450429
PP
3289 {
3290 struct objfile *objfile;
3291 CORE_ADDR addr;
3292
3293 set_current_program_space (pspace);
3294
aa7d318d
TT
3295 ALL_OBJFILES (objfile)
3296 {
3297 struct breakpoint *b;
17450429 3298 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3299
17450429 3300 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3301
17450429
PP
3302 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3303 continue;
3304
3305 if (bp_objfile_data->terminate_msym == NULL)
3306 {
3307 struct minimal_symbol *m;
3308
3309 m = lookup_minimal_symbol (func_name, NULL, objfile);
3310 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3311 && MSYMBOL_TYPE (m) != mst_file_text))
3312 {
3313 /* Prevent future lookups in this objfile. */
3314 bp_objfile_data->terminate_msym = &msym_not_found;
3315 continue;
3316 }
3317 bp_objfile_data->terminate_msym = m;
3318 }
aa7d318d 3319
17450429
PP
3320 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3321 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3322 bp_std_terminate_master,
3323 &internal_breakpoint_ops);
aa7d318d
TT
3324 b->addr_string = xstrdup (func_name);
3325 b->enable_state = bp_disabled;
3326 }
17450429
PP
3327 }
3328
aa7d318d
TT
3329 update_global_location_list (1);
3330
3331 do_cleanups (old_chain);
3332}
3333
186c406b
TT
3334/* Install a master breakpoint on the unwinder's debug hook. */
3335
70221824 3336static void
186c406b
TT
3337create_exception_master_breakpoint (void)
3338{
3339 struct objfile *objfile;
17450429 3340 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3341
3342 ALL_OBJFILES (objfile)
3343 {
17450429
PP
3344 struct breakpoint *b;
3345 struct gdbarch *gdbarch;
3346 struct breakpoint_objfile_data *bp_objfile_data;
3347 CORE_ADDR addr;
3348
3349 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3350
28106bc2
SDJ
3351 /* We prefer the SystemTap probe point if it exists. */
3352 if (!bp_objfile_data->exception_searched)
3353 {
25f9533e
SDJ
3354 VEC (probe_p) *ret;
3355
3356 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3357
3358 if (ret != NULL)
3359 {
3360 /* We are only interested in checking one element. */
3361 struct probe *p = VEC_index (probe_p, ret, 0);
3362
3363 if (!can_evaluate_probe_arguments (p))
3364 {
3365 /* We cannot use the probe interface here, because it does
3366 not know how to evaluate arguments. */
3367 VEC_free (probe_p, ret);
3368 ret = NULL;
3369 }
3370 }
3371 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3372 bp_objfile_data->exception_searched = 1;
3373 }
3374
3375 if (bp_objfile_data->exception_probes != NULL)
3376 {
3377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3378 int i;
3379 struct probe *probe;
3380
3381 for (i = 0;
3382 VEC_iterate (probe_p,
3383 bp_objfile_data->exception_probes,
3384 i, probe);
3385 ++i)
3386 {
3387 struct breakpoint *b;
3388
3389 b = create_internal_breakpoint (gdbarch, probe->address,
3390 bp_exception_master,
3391 &internal_breakpoint_ops);
3392 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3393 b->enable_state = bp_disabled;
3394 }
3395
3396 continue;
3397 }
3398
3399 /* Otherwise, try the hook function. */
3400
17450429
PP
3401 if (msym_not_found_p (bp_objfile_data->exception_msym))
3402 continue;
3403
3404 gdbarch = get_objfile_arch (objfile);
186c406b 3405
17450429 3406 if (bp_objfile_data->exception_msym == NULL)
186c406b 3407 {
17450429 3408 struct minimal_symbol *debug_hook;
186c406b 3409
17450429
PP
3410 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3411 if (debug_hook == NULL)
3412 {
3413 bp_objfile_data->exception_msym = &msym_not_found;
3414 continue;
3415 }
3416
3417 bp_objfile_data->exception_msym = debug_hook;
186c406b 3418 }
17450429
PP
3419
3420 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3421 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3422 &current_target);
06edf0c0
PA
3423 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3424 &internal_breakpoint_ops);
17450429
PP
3425 b->addr_string = xstrdup (func_name);
3426 b->enable_state = bp_disabled;
186c406b
TT
3427 }
3428
3429 update_global_location_list (1);
3430}
3431
c906108c 3432void
fba45db2 3433update_breakpoints_after_exec (void)
c906108c 3434{
35df4500 3435 struct breakpoint *b, *b_tmp;
876fa593 3436 struct bp_location *bploc, **bplocp_tmp;
c906108c 3437
25b22b0a
PA
3438 /* We're about to delete breakpoints from GDB's lists. If the
3439 INSERTED flag is true, GDB will try to lift the breakpoints by
3440 writing the breakpoints' "shadow contents" back into memory. The
3441 "shadow contents" are NOT valid after an exec, so GDB should not
3442 do that. Instead, the target is responsible from marking
3443 breakpoints out as soon as it detects an exec. We don't do that
3444 here instead, because there may be other attempts to delete
3445 breakpoints after detecting an exec and before reaching here. */
876fa593 3446 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3447 if (bploc->pspace == current_program_space)
3448 gdb_assert (!bploc->inserted);
c906108c 3449
35df4500 3450 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3451 {
6c95b8df
PA
3452 if (b->pspace != current_program_space)
3453 continue;
3454
4a64f543 3455 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3456 if (b->type == bp_shlib_event)
3457 {
3458 delete_breakpoint (b);
3459 continue;
3460 }
c906108c 3461
4a64f543 3462 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3463 if (b->type == bp_jit_event)
3464 {
3465 delete_breakpoint (b);
3466 continue;
3467 }
3468
1900040c 3469 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3470 as must overlay event and longjmp master breakpoints. */
3471 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3472 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3473 || b->type == bp_exception_master)
c4093a6a
JM
3474 {
3475 delete_breakpoint (b);
3476 continue;
3477 }
3478
4a64f543 3479 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3480 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3481 {
3482 delete_breakpoint (b);
3483 continue;
3484 }
3485
611c83ae
PA
3486 /* Longjmp and longjmp-resume breakpoints are also meaningless
3487 after an exec. */
186c406b 3488 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3489 || b->type == bp_longjmp_call_dummy
186c406b 3490 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3491 {
3492 delete_breakpoint (b);
3493 continue;
3494 }
3495
ce78b96d
JB
3496 if (b->type == bp_catchpoint)
3497 {
3498 /* For now, none of the bp_catchpoint breakpoints need to
3499 do anything at this point. In the future, if some of
3500 the catchpoints need to something, we will need to add
3501 a new method, and call this method from here. */
3502 continue;
3503 }
3504
c5aa993b
JM
3505 /* bp_finish is a special case. The only way we ought to be able
3506 to see one of these when an exec() has happened, is if the user
3507 caught a vfork, and then said "finish". Ordinarily a finish just
3508 carries them to the call-site of the current callee, by setting
3509 a temporary bp there and resuming. But in this case, the finish
3510 will carry them entirely through the vfork & exec.
3511
3512 We don't want to allow a bp_finish to remain inserted now. But
3513 we can't safely delete it, 'cause finish_command has a handle to
3514 the bp on a bpstat, and will later want to delete it. There's a
3515 chance (and I've seen it happen) that if we delete the bp_finish
3516 here, that its storage will get reused by the time finish_command
3517 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3518 We really must allow finish_command to delete a bp_finish.
3519
e5dd4106 3520 In the absence of a general solution for the "how do we know
53a5351d
JM
3521 it's safe to delete something others may have handles to?"
3522 problem, what we'll do here is just uninsert the bp_finish, and
3523 let finish_command delete it.
3524
3525 (We know the bp_finish is "doomed" in the sense that it's
3526 momentary, and will be deleted as soon as finish_command sees
3527 the inferior stopped. So it doesn't matter that the bp's
3528 address is probably bogus in the new a.out, unlike e.g., the
3529 solib breakpoints.) */
c5aa993b 3530
c5aa993b
JM
3531 if (b->type == bp_finish)
3532 {
3533 continue;
3534 }
3535
3536 /* Without a symbolic address, we have little hope of the
3537 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3538 a.out. */
c5aa993b
JM
3539 if (b->addr_string == NULL)
3540 {
3541 delete_breakpoint (b);
3542 continue;
3543 }
c5aa993b 3544 }
1900040c 3545 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3546 create_overlay_event_breakpoint ();
3547 create_longjmp_master_breakpoint ();
3548 create_std_terminate_master_breakpoint ();
186c406b 3549 create_exception_master_breakpoint ();
c906108c
SS
3550}
3551
3552int
d80ee84f 3553detach_breakpoints (ptid_t ptid)
c906108c 3554{
35df4500 3555 struct bp_location *bl, **blp_tmp;
3a1bae8e 3556 int val = 0;
ce696e05 3557 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3558 struct inferior *inf = current_inferior ();
c5aa993b 3559
d80ee84f 3560 if (PIDGET (ptid) == PIDGET (inferior_ptid))
8a3fe4f8 3561 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3562
6c95b8df 3563 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3564 inferior_ptid = ptid;
35df4500 3565 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3566 {
35df4500 3567 if (bl->pspace != inf->pspace)
6c95b8df
PA
3568 continue;
3569
bd9673a4
PW
3570 /* This function must physically remove breakpoints locations
3571 from the specified ptid, without modifying the breakpoint
3572 package's state. Locations of type bp_loc_other are only
3573 maintained at GDB side. So, there is no need to remove
3574 these bp_loc_other locations. Moreover, removing these
3575 would modify the breakpoint package's state. */
3576 if (bl->loc_type == bp_loc_other)
3577 continue;
3578
35df4500
TJB
3579 if (bl->inserted)
3580 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3581 }
d03285ec
UW
3582
3583 /* Detach single-step breakpoints as well. */
3584 detach_single_step_breakpoints ();
3585
ce696e05 3586 do_cleanups (old_chain);
3a1bae8e 3587 return val;
c906108c
SS
3588}
3589
35df4500 3590/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3591 Note that this is used to detach breakpoints from a child fork.
3592 When we get here, the child isn't in the inferior list, and neither
3593 do we have objects to represent its address space --- we should
35df4500 3594 *not* look at bl->pspace->aspace here. */
6c95b8df 3595
c906108c 3596static int
35df4500 3597remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3598{
3599 int val;
c5aa993b 3600
35df4500
TJB
3601 /* BL is never in moribund_locations by our callers. */
3602 gdb_assert (bl->owner != NULL);
2bdf28a0 3603
35df4500 3604 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3605 /* Permanent breakpoints cannot be inserted or removed. */
3606 return 0;
3607
74960c60
VP
3608 /* The type of none suggests that owner is actually deleted.
3609 This should not ever happen. */
35df4500 3610 gdb_assert (bl->owner->type != bp_none);
0bde7532 3611
35df4500
TJB
3612 if (bl->loc_type == bp_loc_software_breakpoint
3613 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3614 {
c02f5703
MS
3615 /* "Normal" instruction breakpoint: either the standard
3616 trap-instruction bp (bp_breakpoint), or a
3617 bp_hardware_breakpoint. */
3618
3619 /* First check to see if we have to handle an overlay. */
3620 if (overlay_debugging == ovly_off
35df4500
TJB
3621 || bl->section == NULL
3622 || !(section_is_overlay (bl->section)))
c02f5703
MS
3623 {
3624 /* No overlay handling: just remove the breakpoint. */
348d480f 3625 val = bl->owner->ops->remove_location (bl);
c02f5703 3626 }
c906108c
SS
3627 else
3628 {
4a64f543 3629 /* This breakpoint is in an overlay section.
c02f5703
MS
3630 Did we set a breakpoint at the LMA? */
3631 if (!overlay_events_enabled)
3632 {
3633 /* Yes -- overlay event support is not active, so we
3634 should have set a breakpoint at the LMA. Remove it.
3635 */
c02f5703
MS
3636 /* Ignore any failures: if the LMA is in ROM, we will
3637 have already warned when we failed to insert it. */
35df4500
TJB
3638 if (bl->loc_type == bp_loc_hardware_breakpoint)
3639 target_remove_hw_breakpoint (bl->gdbarch,
3640 &bl->overlay_target_info);
c02f5703 3641 else
35df4500
TJB
3642 target_remove_breakpoint (bl->gdbarch,
3643 &bl->overlay_target_info);
c02f5703
MS
3644 }
3645 /* Did we set a breakpoint at the VMA?
3646 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3647 if (bl->inserted)
c906108c 3648 {
c02f5703
MS
3649 /* Yes -- remove it. Previously we did not bother to
3650 remove the breakpoint if the section had been
3651 unmapped, but let's not rely on that being safe. We
3652 don't know what the overlay manager might do. */
aa67235e
UW
3653
3654 /* However, we should remove *software* breakpoints only
3655 if the section is still mapped, or else we overwrite
3656 wrong code with the saved shadow contents. */
348d480f
PA
3657 if (bl->loc_type == bp_loc_hardware_breakpoint
3658 || section_is_mapped (bl->section))
3659 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3660 else
3661 val = 0;
c906108c 3662 }
c02f5703
MS
3663 else
3664 {
3665 /* No -- not inserted, so no need to remove. No error. */
3666 val = 0;
3667 }
c906108c 3668 }
879d1e6b
UW
3669
3670 /* In some cases, we might not be able to remove a breakpoint
3671 in a shared library that has already been removed, but we
3672 have not yet processed the shlib unload event. */
35df4500 3673 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3674 val = 0;
3675
c906108c
SS
3676 if (val)
3677 return val;
35df4500 3678 bl->inserted = (is == mark_inserted);
c906108c 3679 }
35df4500 3680 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3681 {
77b06cd7
TJB
3682 gdb_assert (bl->owner->ops != NULL
3683 && bl->owner->ops->remove_location != NULL);
3684
35df4500 3685 bl->inserted = (is == mark_inserted);
77b06cd7 3686 bl->owner->ops->remove_location (bl);
2e70b7b9 3687
c906108c 3688 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3689 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3690 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3691 bl->owner->number);
c906108c 3692 }
35df4500
TJB
3693 else if (bl->owner->type == bp_catchpoint
3694 && breakpoint_enabled (bl->owner)
3695 && !bl->duplicate)
ce78b96d 3696 {
77b06cd7
TJB
3697 gdb_assert (bl->owner->ops != NULL
3698 && bl->owner->ops->remove_location != NULL);
ce78b96d 3699
77b06cd7 3700 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3701 if (val)
3702 return val;
77b06cd7 3703
35df4500 3704 bl->inserted = (is == mark_inserted);
ce78b96d 3705 }
c906108c
SS
3706
3707 return 0;
3708}
3709
6c95b8df 3710static int
35df4500 3711remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3712{
3713 int ret;
3714 struct cleanup *old_chain;
3715
35df4500
TJB
3716 /* BL is never in moribund_locations by our callers. */
3717 gdb_assert (bl->owner != NULL);
2bdf28a0 3718
35df4500 3719 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3720 /* Permanent breakpoints cannot be inserted or removed. */
3721 return 0;
3722
3723 /* The type of none suggests that owner is actually deleted.
3724 This should not ever happen. */
35df4500 3725 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3726
3727 old_chain = save_current_space_and_thread ();
3728
35df4500 3729 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3730
35df4500 3731 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3732
3733 do_cleanups (old_chain);
3734 return ret;
3735}
3736
c906108c
SS
3737/* Clear the "inserted" flag in all breakpoints. */
3738
25b22b0a 3739void
fba45db2 3740mark_breakpoints_out (void)
c906108c 3741{
35df4500 3742 struct bp_location *bl, **blp_tmp;
c906108c 3743
35df4500
TJB
3744 ALL_BP_LOCATIONS (bl, blp_tmp)
3745 if (bl->pspace == current_program_space)
3746 bl->inserted = 0;
c906108c
SS
3747}
3748
53a5351d
JM
3749/* Clear the "inserted" flag in all breakpoints and delete any
3750 breakpoints which should go away between runs of the program.
c906108c
SS
3751
3752 Plus other such housekeeping that has to be done for breakpoints
3753 between runs.
3754
53a5351d
JM
3755 Note: this function gets called at the end of a run (by
3756 generic_mourn_inferior) and when a run begins (by
4a64f543 3757 init_wait_for_inferior). */
c906108c
SS
3758
3759
3760
3761void
fba45db2 3762breakpoint_init_inferior (enum inf_context context)
c906108c 3763{
35df4500
TJB
3764 struct breakpoint *b, *b_tmp;
3765 struct bp_location *bl, **blp_tmp;
1c5cfe86 3766 int ix;
6c95b8df 3767 struct program_space *pspace = current_program_space;
c906108c 3768
50c71eaf
PA
3769 /* If breakpoint locations are shared across processes, then there's
3770 nothing to do. */
f5656ead 3771 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3772 return;
3773
35df4500 3774 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3775 {
35df4500
TJB
3776 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3777 if (bl->pspace == pspace
3778 && bl->owner->enable_state != bp_permanent)
3779 bl->inserted = 0;
6c95b8df 3780 }
075f6582 3781
35df4500 3782 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3783 {
6c95b8df
PA
3784 if (b->loc && b->loc->pspace != pspace)
3785 continue;
3786
c5aa993b
JM
3787 switch (b->type)
3788 {
3789 case bp_call_dummy:
e2e4d78b 3790 case bp_longjmp_call_dummy:
c906108c 3791
c5aa993b 3792 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3793 cause problems when the inferior is rerun, so we better get
3794 rid of it. */
3795
3796 case bp_watchpoint_scope:
3797
3798 /* Also get rid of scope breakpoints. */
3799
3800 case bp_shlib_event:
3801
3802 /* Also remove solib event breakpoints. Their addresses may
3803 have changed since the last time we ran the program.
3804 Actually we may now be debugging against different target;
3805 and so the solib backend that installed this breakpoint may
3806 not be used in by the target. E.g.,
3807
3808 (gdb) file prog-linux
3809 (gdb) run # native linux target
3810 ...
3811 (gdb) kill
3812 (gdb) file prog-win.exe
3813 (gdb) tar rem :9999 # remote Windows gdbserver.
3814 */
c906108c 3815
f59f708a
PA
3816 case bp_step_resume:
3817
3818 /* Also remove step-resume breakpoints. */
3819
c5aa993b
JM
3820 delete_breakpoint (b);
3821 break;
c906108c 3822
c5aa993b
JM
3823 case bp_watchpoint:
3824 case bp_hardware_watchpoint:
3825 case bp_read_watchpoint:
3826 case bp_access_watchpoint:
3a5c3e22
PA
3827 {
3828 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3829
3a5c3e22
PA
3830 /* Likewise for watchpoints on local expressions. */
3831 if (w->exp_valid_block != NULL)
3832 delete_breakpoint (b);
3833 else if (context == inf_starting)
3834 {
3835 /* Reset val field to force reread of starting value in
3836 insert_breakpoints. */
3837 if (w->val)
3838 value_free (w->val);
3839 w->val = NULL;
3840 w->val_valid = 0;
c860120c 3841 }
3a5c3e22 3842 }
c5aa993b
JM
3843 break;
3844 default:
c5aa993b
JM
3845 break;
3846 }
3847 }
1c5cfe86
PA
3848
3849 /* Get rid of the moribund locations. */
35df4500
TJB
3850 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3851 decref_bp_location (&bl);
1c5cfe86 3852 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3853}
3854
6c95b8df
PA
3855/* These functions concern about actual breakpoints inserted in the
3856 target --- to e.g. check if we need to do decr_pc adjustment or if
3857 we need to hop over the bkpt --- so we check for address space
3858 match, not program space. */
3859
c2c6d25f
JM
3860/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3861 exists at PC. It returns ordinary_breakpoint_here if it's an
3862 ordinary breakpoint, or permanent_breakpoint_here if it's a
3863 permanent breakpoint.
3864 - When continuing from a location with an ordinary breakpoint, we
3865 actually single step once before calling insert_breakpoints.
e5dd4106 3866 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3867 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3868 the target, to advance the PC past the breakpoint. */
c906108c 3869
c2c6d25f 3870enum breakpoint_here
6c95b8df 3871breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3872{
35df4500 3873 struct bp_location *bl, **blp_tmp;
c2c6d25f 3874 int any_breakpoint_here = 0;
c906108c 3875
35df4500 3876 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3877 {
35df4500
TJB
3878 if (bl->loc_type != bp_loc_software_breakpoint
3879 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3880 continue;
3881
f1310107 3882 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3883 if ((breakpoint_enabled (bl->owner)
3884 || bl->owner->enable_state == bp_permanent)
f1310107 3885 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3886 {
3887 if (overlay_debugging
35df4500
TJB
3888 && section_is_overlay (bl->section)
3889 && !section_is_mapped (bl->section))
075f6582 3890 continue; /* unmapped overlay -- can't be a match */
35df4500 3891 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3892 return permanent_breakpoint_here;
3893 else
3894 any_breakpoint_here = 1;
3895 }
3896 }
c906108c 3897
c2c6d25f 3898 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3899}
3900
1c5cfe86
PA
3901/* Return true if there's a moribund breakpoint at PC. */
3902
3903int
6c95b8df 3904moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3905{
3906 struct bp_location *loc;
3907 int ix;
3908
3909 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3910 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3911 return 1;
3912
3913 return 0;
3914}
c2c6d25f 3915
c36b740a 3916/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3917 inserted using regular breakpoint_chain / bp_location array
3918 mechanism. This does not check for single-step breakpoints, which
3919 are inserted and removed using direct target manipulation. */
c906108c
SS
3920
3921int
4a64f543
MS
3922regular_breakpoint_inserted_here_p (struct address_space *aspace,
3923 CORE_ADDR pc)
c906108c 3924{
35df4500 3925 struct bp_location *bl, **blp_tmp;
c906108c 3926
35df4500 3927 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3928 {
35df4500
TJB
3929 if (bl->loc_type != bp_loc_software_breakpoint
3930 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3931 continue;
3932
35df4500 3933 if (bl->inserted
f1310107 3934 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3935 {
3936 if (overlay_debugging
35df4500
TJB
3937 && section_is_overlay (bl->section)
3938 && !section_is_mapped (bl->section))
075f6582
DJ
3939 continue; /* unmapped overlay -- can't be a match */
3940 else
3941 return 1;
3942 }
c5aa993b 3943 }
c36b740a
VP
3944 return 0;
3945}
3946
3947/* Returns non-zero iff there's either regular breakpoint
3948 or a single step breakpoint inserted at PC. */
3949
3950int
6c95b8df 3951breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3952{
6c95b8df 3953 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3954 return 1;
c906108c 3955
6c95b8df 3956 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3957 return 1;
3958
c906108c
SS
3959 return 0;
3960}
3961
4fa8626c
DJ
3962/* This function returns non-zero iff there is a software breakpoint
3963 inserted at PC. */
3964
3965int
3e43a32a
MS
3966software_breakpoint_inserted_here_p (struct address_space *aspace,
3967 CORE_ADDR pc)
4fa8626c 3968{
35df4500 3969 struct bp_location *bl, **blp_tmp;
4fa8626c 3970
35df4500 3971 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3972 {
35df4500 3973 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3974 continue;
3975
35df4500
TJB
3976 if (bl->inserted
3977 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3978 aspace, pc))
4fa8626c
DJ
3979 {
3980 if (overlay_debugging
35df4500
TJB
3981 && section_is_overlay (bl->section)
3982 && !section_is_mapped (bl->section))
4fa8626c
DJ
3983 continue; /* unmapped overlay -- can't be a match */
3984 else
3985 return 1;
3986 }
3987 }
3988
1aafd4da 3989 /* Also check for software single-step breakpoints. */
6c95b8df 3990 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3991 return 1;
3992
4fa8626c
DJ
3993 return 0;
3994}
3995
9093389c
PA
3996int
3997hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3998 CORE_ADDR addr, ULONGEST len)
3999{
4000 struct breakpoint *bpt;
4001
4002 ALL_BREAKPOINTS (bpt)
4003 {
4004 struct bp_location *loc;
4005
4006 if (bpt->type != bp_hardware_watchpoint
4007 && bpt->type != bp_access_watchpoint)
4008 continue;
4009
4010 if (!breakpoint_enabled (bpt))
4011 continue;
4012
4013 for (loc = bpt->loc; loc; loc = loc->next)
4014 if (loc->pspace->aspace == aspace && loc->inserted)
4015 {
4016 CORE_ADDR l, h;
4017
4018 /* Check for intersection. */
4019 l = max (loc->address, addr);
4020 h = min (loc->address + loc->length, addr + len);
4021 if (l < h)
4022 return 1;
4023 }
4024 }
4025 return 0;
4026}
4027
075f6582
DJ
4028/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4029 PC is valid for process/thread PTID. */
c906108c
SS
4030
4031int
6c95b8df
PA
4032breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4033 ptid_t ptid)
c906108c 4034{
35df4500 4035 struct bp_location *bl, **blp_tmp;
4a306c9a 4036 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4037 int thread = -1;
4a306c9a 4038 int task = 0;
a6f1cd96 4039
35df4500 4040 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4041 {
35df4500
TJB
4042 if (bl->loc_type != bp_loc_software_breakpoint
4043 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4044 continue;
4045
35df4500
TJB
4046 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4047 if (!breakpoint_enabled (bl->owner)
4048 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4049 continue;
4050
f1310107 4051 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4052 continue;
4053
35df4500 4054 if (bl->owner->thread != -1)
075f6582 4055 {
a6f1cd96
JB
4056 /* This is a thread-specific breakpoint. Check that ptid
4057 matches that thread. If thread hasn't been computed yet,
4058 it is now time to do so. */
4059 if (thread == -1)
4060 thread = pid_to_thread_id (ptid);
35df4500 4061 if (bl->owner->thread != thread)
a6f1cd96 4062 continue;
075f6582 4063 }
a6f1cd96 4064
35df4500 4065 if (bl->owner->task != 0)
4a306c9a
JB
4066 {
4067 /* This is a task-specific breakpoint. Check that ptid
4068 matches that task. If task hasn't been computed yet,
4069 it is now time to do so. */
4070 if (task == 0)
4071 task = ada_get_task_number (ptid);
35df4500 4072 if (bl->owner->task != task)
4a306c9a
JB
4073 continue;
4074 }
4075
a6f1cd96 4076 if (overlay_debugging
35df4500
TJB
4077 && section_is_overlay (bl->section)
4078 && !section_is_mapped (bl->section))
a6f1cd96
JB
4079 continue; /* unmapped overlay -- can't be a match */
4080
4081 return 1;
c5aa993b 4082 }
c906108c
SS
4083
4084 return 0;
4085}
c906108c 4086\f
c5aa993b 4087
c906108c
SS
4088/* bpstat stuff. External routines' interfaces are documented
4089 in breakpoint.h. */
4090
4091int
c326b90e 4092is_catchpoint (struct breakpoint *ep)
c906108c 4093{
533be4dd 4094 return (ep->type == bp_catchpoint);
c906108c
SS
4095}
4096
f431efe5
PA
4097/* Frees any storage that is part of a bpstat. Does not walk the
4098 'next' chain. */
4099
4100static void
198757a8
VP
4101bpstat_free (bpstat bs)
4102{
4103 if (bs->old_val != NULL)
4104 value_free (bs->old_val);
9add0f1b 4105 decref_counted_command_line (&bs->commands);
f431efe5 4106 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4107 xfree (bs);
4108}
4109
c906108c
SS
4110/* Clear a bpstat so that it says we are not at any breakpoint.
4111 Also free any storage that is part of a bpstat. */
4112
4113void
fba45db2 4114bpstat_clear (bpstat *bsp)
c906108c
SS
4115{
4116 bpstat p;
4117 bpstat q;
4118
4119 if (bsp == 0)
4120 return;
4121 p = *bsp;
4122 while (p != NULL)
4123 {
4124 q = p->next;
198757a8 4125 bpstat_free (p);
c906108c
SS
4126 p = q;
4127 }
4128 *bsp = NULL;
4129}
4130
4131/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4132 is part of the bpstat is copied as well. */
4133
4134bpstat
fba45db2 4135bpstat_copy (bpstat bs)
c906108c
SS
4136{
4137 bpstat p = NULL;
4138 bpstat tmp;
4139 bpstat retval = NULL;
4140
4141 if (bs == NULL)
4142 return bs;
4143
4144 for (; bs != NULL; bs = bs->next)
4145 {
4146 tmp = (bpstat) xmalloc (sizeof (*tmp));
4147 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4148 incref_counted_command_line (tmp->commands);
f431efe5 4149 incref_bp_location (tmp->bp_location_at);
31cc81e9 4150 if (bs->old_val != NULL)
3c3185ac
JK
4151 {
4152 tmp->old_val = value_copy (bs->old_val);
4153 release_value (tmp->old_val);
4154 }
31cc81e9 4155
c906108c
SS
4156 if (p == NULL)
4157 /* This is the first thing in the chain. */
4158 retval = tmp;
4159 else
4160 p->next = tmp;
4161 p = tmp;
4162 }
4163 p->next = NULL;
4164 return retval;
4165}
4166
4a64f543 4167/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4168
4169bpstat
fba45db2 4170bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4171{
c5aa993b
JM
4172 if (bsp == NULL)
4173 return NULL;
c906108c 4174
c5aa993b
JM
4175 for (; bsp != NULL; bsp = bsp->next)
4176 {
f431efe5 4177 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4178 return bsp;
4179 }
c906108c
SS
4180 return NULL;
4181}
4182
ab04a2af
TT
4183/* See breakpoint.h. */
4184
4185enum bpstat_signal_value
427cd150 4186bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af
TT
4187{
4188 enum bpstat_signal_value result = BPSTAT_SIGNAL_NO;
4189
4190 for (; bsp != NULL; bsp = bsp->next)
4191 {
4192 /* Ensure that, if we ever entered this loop, then we at least
4193 return BPSTAT_SIGNAL_HIDE. */
427cd150 4194 enum bpstat_signal_value newval;
ab04a2af 4195
427cd150
TT
4196 if (bsp->breakpoint_at == NULL)
4197 {
4198 /* A moribund location can never explain a signal other than
4199 GDB_SIGNAL_TRAP. */
4200 if (sig == GDB_SIGNAL_TRAP)
4201 newval = BPSTAT_SIGNAL_HIDE;
4202 else
4203 newval = BPSTAT_SIGNAL_NO;
4204 }
4205 else
4206 newval = bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4207 sig);
ab04a2af
TT
4208
4209 if (newval > result)
4210 result = newval;
4211 }
4212
4213 return result;
4214}
4215
4a64f543
MS
4216/* Put in *NUM the breakpoint number of the first breakpoint we are
4217 stopped at. *BSP upon return is a bpstat which points to the
4218 remaining breakpoints stopped at (but which is not guaranteed to be
4219 good for anything but further calls to bpstat_num).
4220
8671a17b
PA
4221 Return 0 if passed a bpstat which does not indicate any breakpoints.
4222 Return -1 if stopped at a breakpoint that has been deleted since
4223 we set it.
4224 Return 1 otherwise. */
c906108c
SS
4225
4226int
8671a17b 4227bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4228{
4229 struct breakpoint *b;
4230
4231 if ((*bsp) == NULL)
4232 return 0; /* No more breakpoint values */
8671a17b 4233
4a64f543
MS
4234 /* We assume we'll never have several bpstats that correspond to a
4235 single breakpoint -- otherwise, this function might return the
4236 same number more than once and this will look ugly. */
f431efe5 4237 b = (*bsp)->breakpoint_at;
8671a17b
PA
4238 *bsp = (*bsp)->next;
4239 if (b == NULL)
4240 return -1; /* breakpoint that's been deleted since */
4241
4242 *num = b->number; /* We have its number */
4243 return 1;
c906108c
SS
4244}
4245
e93ca019 4246/* See breakpoint.h. */
c906108c
SS
4247
4248void
e93ca019 4249bpstat_clear_actions (void)
c906108c 4250{
e93ca019
JK
4251 struct thread_info *tp;
4252 bpstat bs;
4253
4254 if (ptid_equal (inferior_ptid, null_ptid))
4255 return;
4256
4257 tp = find_thread_ptid (inferior_ptid);
4258 if (tp == NULL)
4259 return;
4260
4261 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4262 {
9add0f1b 4263 decref_counted_command_line (&bs->commands);
abf85f46 4264
c906108c
SS
4265 if (bs->old_val != NULL)
4266 {
4267 value_free (bs->old_val);
4268 bs->old_val = NULL;
4269 }
4270 }
4271}
4272
f3b1572e
PA
4273/* Called when a command is about to proceed the inferior. */
4274
4275static void
4276breakpoint_about_to_proceed (void)
4277{
4278 if (!ptid_equal (inferior_ptid, null_ptid))
4279 {
4280 struct thread_info *tp = inferior_thread ();
4281
4282 /* Allow inferior function calls in breakpoint commands to not
4283 interrupt the command list. When the call finishes
4284 successfully, the inferior will be standing at the same
4285 breakpoint as if nothing happened. */
16c381f0 4286 if (tp->control.in_infcall)
f3b1572e
PA
4287 return;
4288 }
4289
4290 breakpoint_proceeded = 1;
4291}
4292
4a64f543
MS
4293/* Stub for cleaning up our state if we error-out of a breakpoint
4294 command. */
c906108c 4295static void
4efb68b1 4296cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4297{
4298 executing_breakpoint_commands = 0;
4299}
4300
abf85f46
JK
4301/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4302 or its equivalent. */
4303
4304static int
4305command_line_is_silent (struct command_line *cmd)
4306{
4307 return cmd && (strcmp ("silent", cmd->line) == 0
4308 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4309}
4310
4a64f543
MS
4311/* Execute all the commands associated with all the breakpoints at
4312 this location. Any of these commands could cause the process to
4313 proceed beyond this point, etc. We look out for such changes by
4314 checking the global "breakpoint_proceeded" after each command.
c906108c 4315
347bddb7
PA
4316 Returns true if a breakpoint command resumed the inferior. In that
4317 case, it is the caller's responsibility to recall it again with the
4318 bpstat of the current thread. */
4319
4320static int
4321bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4322{
4323 bpstat bs;
4324 struct cleanup *old_chain;
347bddb7 4325 int again = 0;
c906108c
SS
4326
4327 /* Avoid endless recursion if a `source' command is contained
4328 in bs->commands. */
4329 if (executing_breakpoint_commands)
347bddb7 4330 return 0;
c906108c
SS
4331
4332 executing_breakpoint_commands = 1;
4333 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4334
cf6c5ffb
TT
4335 prevent_dont_repeat ();
4336
4a64f543 4337 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4338 bs = *bsp;
4339
4340 breakpoint_proceeded = 0;
4341 for (; bs != NULL; bs = bs->next)
4342 {
9add0f1b 4343 struct counted_command_line *ccmd;
6c50ab1c
JB
4344 struct command_line *cmd;
4345 struct cleanup *this_cmd_tree_chain;
4346
4347 /* Take ownership of the BSP's command tree, if it has one.
4348
4349 The command tree could legitimately contain commands like
4350 'step' and 'next', which call clear_proceed_status, which
4351 frees stop_bpstat's command tree. To make sure this doesn't
4352 free the tree we're executing out from under us, we need to
4353 take ownership of the tree ourselves. Since a given bpstat's
4354 commands are only executed once, we don't need to copy it; we
4355 can clear the pointer in the bpstat, and make sure we free
4356 the tree when we're done. */
9add0f1b
TT
4357 ccmd = bs->commands;
4358 bs->commands = NULL;
abf85f46
JK
4359 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4360 cmd = ccmd ? ccmd->commands : NULL;
4361 if (command_line_is_silent (cmd))
4362 {
4363 /* The action has been already done by bpstat_stop_status. */
4364 cmd = cmd->next;
4365 }
6c50ab1c 4366
c906108c
SS
4367 while (cmd != NULL)
4368 {
4369 execute_control_command (cmd);
4370
4371 if (breakpoint_proceeded)
4372 break;
4373 else
4374 cmd = cmd->next;
4375 }
6c50ab1c
JB
4376
4377 /* We can free this command tree now. */
4378 do_cleanups (this_cmd_tree_chain);
4379
c906108c 4380 if (breakpoint_proceeded)
32c1e744
VP
4381 {
4382 if (target_can_async_p ())
347bddb7
PA
4383 /* If we are in async mode, then the target might be still
4384 running, not stopped at any breakpoint, so nothing for
4385 us to do here -- just return to the event loop. */
4386 ;
32c1e744
VP
4387 else
4388 /* In sync mode, when execute_control_command returns
4389 we're already standing on the next breakpoint.
347bddb7
PA
4390 Breakpoint commands for that stop were not run, since
4391 execute_command does not run breakpoint commands --
4392 only command_line_handler does, but that one is not
4393 involved in execution of breakpoint commands. So, we
4394 can now execute breakpoint commands. It should be
4395 noted that making execute_command do bpstat actions is
4396 not an option -- in this case we'll have recursive
4397 invocation of bpstat for each breakpoint with a
4398 command, and can easily blow up GDB stack. Instead, we
4399 return true, which will trigger the caller to recall us
4400 with the new stop_bpstat. */
4401 again = 1;
4402 break;
32c1e744 4403 }
c906108c 4404 }
c2b8ed2c 4405 do_cleanups (old_chain);
347bddb7
PA
4406 return again;
4407}
4408
4409void
4410bpstat_do_actions (void)
4411{
353d1d73
JK
4412 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4413
347bddb7
PA
4414 /* Do any commands attached to breakpoint we are stopped at. */
4415 while (!ptid_equal (inferior_ptid, null_ptid)
4416 && target_has_execution
4417 && !is_exited (inferior_ptid)
4418 && !is_executing (inferior_ptid))
4419 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4420 and only return when it is stopped at the next breakpoint, we
4421 keep doing breakpoint actions until it returns false to
4422 indicate the inferior was not resumed. */
16c381f0 4423 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4424 break;
353d1d73
JK
4425
4426 discard_cleanups (cleanup_if_error);
c906108c
SS
4427}
4428
fa4727a6
DJ
4429/* Print out the (old or new) value associated with a watchpoint. */
4430
4431static void
4432watchpoint_value_print (struct value *val, struct ui_file *stream)
4433{
4434 if (val == NULL)
4435 fprintf_unfiltered (stream, _("<unreadable>"));
4436 else
79a45b7d
TT
4437 {
4438 struct value_print_options opts;
4439 get_user_print_options (&opts);
4440 value_print (val, stream, &opts);
4441 }
fa4727a6
DJ
4442}
4443
e514a9d6 4444/* Generic routine for printing messages indicating why we
4a64f543 4445 stopped. The behavior of this function depends on the value
e514a9d6
JM
4446 'print_it' in the bpstat structure. Under some circumstances we
4447 may decide not to print anything here and delegate the task to
4a64f543 4448 normal_stop(). */
e514a9d6
JM
4449
4450static enum print_stop_action
4451print_bp_stop_message (bpstat bs)
4452{
4453 switch (bs->print_it)
4454 {
4455 case print_it_noop:
4a64f543 4456 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4457 return PRINT_UNKNOWN;
4458 break;
4459
4460 case print_it_done:
4461 /* We still want to print the frame, but we already printed the
4a64f543 4462 relevant messages. */
e514a9d6
JM
4463 return PRINT_SRC_AND_LOC;
4464 break;
4465
4466 case print_it_normal:
4f8d1dc6 4467 {
f431efe5
PA
4468 struct breakpoint *b = bs->breakpoint_at;
4469
1a6a67de
TJB
4470 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4471 which has since been deleted. */
4472 if (b == NULL)
4473 return PRINT_UNKNOWN;
4474
348d480f
PA
4475 /* Normal case. Call the breakpoint's print_it method. */
4476 return b->ops->print_it (bs);
4f8d1dc6 4477 }
348d480f 4478 break;
3086aeae 4479
e514a9d6 4480 default:
8e65ff28 4481 internal_error (__FILE__, __LINE__,
e2e0b3e5 4482 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4483 break;
c906108c 4484 }
c906108c
SS
4485}
4486
edcc5120
TT
4487/* A helper function that prints a shared library stopped event. */
4488
4489static void
4490print_solib_event (int is_catchpoint)
4491{
4492 int any_deleted
4493 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4494 int any_added
4495 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4496
4497 if (!is_catchpoint)
4498 {
4499 if (any_added || any_deleted)
4500 ui_out_text (current_uiout,
4501 _("Stopped due to shared library event:\n"));
4502 else
4503 ui_out_text (current_uiout,
4504 _("Stopped due to shared library event (no "
4505 "libraries added or removed)\n"));
4506 }
4507
4508 if (ui_out_is_mi_like_p (current_uiout))
4509 ui_out_field_string (current_uiout, "reason",
4510 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4511
4512 if (any_deleted)
4513 {
4514 struct cleanup *cleanup;
4515 char *name;
4516 int ix;
4517
4518 ui_out_text (current_uiout, _(" Inferior unloaded "));
4519 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4520 "removed");
4521 for (ix = 0;
4522 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4523 ix, name);
4524 ++ix)
4525 {
4526 if (ix > 0)
4527 ui_out_text (current_uiout, " ");
4528 ui_out_field_string (current_uiout, "library", name);
4529 ui_out_text (current_uiout, "\n");
4530 }
4531
4532 do_cleanups (cleanup);
4533 }
4534
4535 if (any_added)
4536 {
4537 struct so_list *iter;
4538 int ix;
4539 struct cleanup *cleanup;
4540
4541 ui_out_text (current_uiout, _(" Inferior loaded "));
4542 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4543 "added");
4544 for (ix = 0;
4545 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4546 ix, iter);
4547 ++ix)
4548 {
4549 if (ix > 0)
4550 ui_out_text (current_uiout, " ");
4551 ui_out_field_string (current_uiout, "library", iter->so_name);
4552 ui_out_text (current_uiout, "\n");
4553 }
4554
4555 do_cleanups (cleanup);
4556 }
4557}
4558
e514a9d6
JM
4559/* Print a message indicating what happened. This is called from
4560 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4561 list - a list of the eventpoints that caused this stop. KIND is
4562 the target_waitkind for the stopping event. This
e514a9d6
JM
4563 routine calls the generic print routine for printing a message
4564 about reasons for stopping. This will print (for example) the
4565 "Breakpoint n," part of the output. The return value of this
4566 routine is one of:
c906108c 4567
4a64f543 4568 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4569 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4570 code to print the location. An example is
c5aa993b
JM
4571 "Breakpoint 1, " which should be followed by
4572 the location.
917317f4 4573 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4574 to also print the location part of the message.
4575 An example is the catch/throw messages, which
4a64f543 4576 don't require a location appended to the end.
917317f4 4577 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4578 further info to be printed. */
c906108c 4579
917317f4 4580enum print_stop_action
36dfb11c 4581bpstat_print (bpstat bs, int kind)
c906108c
SS
4582{
4583 int val;
c5aa993b 4584
c906108c 4585 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4586 (Currently all watchpoints go on the bpstat whether hit or not.
4587 That probably could (should) be changed, provided care is taken
c906108c 4588 with respect to bpstat_explains_signal). */
e514a9d6
JM
4589 for (; bs; bs = bs->next)
4590 {
4591 val = print_bp_stop_message (bs);
4592 if (val == PRINT_SRC_ONLY
4593 || val == PRINT_SRC_AND_LOC
4594 || val == PRINT_NOTHING)
4595 return val;
4596 }
c906108c 4597
36dfb11c
TT
4598 /* If we had hit a shared library event breakpoint,
4599 print_bp_stop_message would print out this message. If we hit an
4600 OS-level shared library event, do the same thing. */
4601 if (kind == TARGET_WAITKIND_LOADED)
4602 {
edcc5120 4603 print_solib_event (0);
36dfb11c
TT
4604 return PRINT_NOTHING;
4605 }
4606
e514a9d6 4607 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4608 with and nothing was printed. */
917317f4 4609 return PRINT_UNKNOWN;
c906108c
SS
4610}
4611
4a64f543
MS
4612/* Evaluate the expression EXP and return 1 if value is zero. This is
4613 used inside a catch_errors to evaluate the breakpoint condition.
4614 The argument is a "struct expression *" that has been cast to a
4615 "char *" to make it pass through catch_errors. */
c906108c
SS
4616
4617static int
4efb68b1 4618breakpoint_cond_eval (void *exp)
c906108c 4619{
278cd55f 4620 struct value *mark = value_mark ();
c5aa993b 4621 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4622
c906108c
SS
4623 value_free_to_mark (mark);
4624 return i;
4625}
4626
5760d0ab 4627/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4628
4629static bpstat
5760d0ab 4630bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4631{
4632 bpstat bs;
4633
4634 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4635 bs->next = NULL;
4636 **bs_link_pointer = bs;
4637 *bs_link_pointer = &bs->next;
f431efe5
PA
4638 bs->breakpoint_at = bl->owner;
4639 bs->bp_location_at = bl;
4640 incref_bp_location (bl);
c906108c
SS
4641 /* If the condition is false, etc., don't do the commands. */
4642 bs->commands = NULL;
4643 bs->old_val = NULL;
4644 bs->print_it = print_it_normal;
4645 return bs;
4646}
4647\f
d983da9c
DJ
4648/* The target has stopped with waitstatus WS. Check if any hardware
4649 watchpoints have triggered, according to the target. */
4650
4651int
4652watchpoints_triggered (struct target_waitstatus *ws)
4653{
d92524f1 4654 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4655 CORE_ADDR addr;
4656 struct breakpoint *b;
4657
4658 if (!stopped_by_watchpoint)
4659 {
4660 /* We were not stopped by a watchpoint. Mark all watchpoints
4661 as not triggered. */
4662 ALL_BREAKPOINTS (b)
cc60f2e3 4663 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4664 {
4665 struct watchpoint *w = (struct watchpoint *) b;
4666
4667 w->watchpoint_triggered = watch_triggered_no;
4668 }
d983da9c
DJ
4669
4670 return 0;
4671 }
4672
4673 if (!target_stopped_data_address (&current_target, &addr))
4674 {
4675 /* We were stopped by a watchpoint, but we don't know where.
4676 Mark all watchpoints as unknown. */
4677 ALL_BREAKPOINTS (b)
cc60f2e3 4678 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4679 {
4680 struct watchpoint *w = (struct watchpoint *) b;
4681
4682 w->watchpoint_triggered = watch_triggered_unknown;
4683 }
d983da9c
DJ
4684
4685 return stopped_by_watchpoint;
4686 }
4687
4688 /* The target could report the data address. Mark watchpoints
4689 affected by this data address as triggered, and all others as not
4690 triggered. */
4691
4692 ALL_BREAKPOINTS (b)
cc60f2e3 4693 if (is_hardware_watchpoint (b))
d983da9c 4694 {
3a5c3e22 4695 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4696 struct bp_location *loc;
d983da9c 4697
3a5c3e22 4698 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4699 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4700 {
3a5c3e22 4701 if (is_masked_watchpoint (b))
9c06b0b4 4702 {
3a5c3e22
PA
4703 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4704 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4705
4706 if (newaddr == start)
4707 {
3a5c3e22 4708 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4709 break;
4710 }
4711 }
4712 /* Exact match not required. Within range is sufficient. */
4713 else if (target_watchpoint_addr_within_range (&current_target,
4714 addr, loc->address,
4715 loc->length))
4716 {
3a5c3e22 4717 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4718 break;
4719 }
4720 }
d983da9c
DJ
4721 }
4722
4723 return 1;
4724}
4725
c906108c
SS
4726/* Possible return values for watchpoint_check (this can't be an enum
4727 because of check_errors). */
4728/* The watchpoint has been deleted. */
4729#define WP_DELETED 1
4730/* The value has changed. */
4731#define WP_VALUE_CHANGED 2
4732/* The value has not changed. */
4733#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4734/* Ignore this watchpoint, no matter if the value changed or not. */
4735#define WP_IGNORE 4
c906108c
SS
4736
4737#define BP_TEMPFLAG 1
4738#define BP_HARDWAREFLAG 2
4739
4a64f543
MS
4740/* Evaluate watchpoint condition expression and check if its value
4741 changed.
553e4c11
JB
4742
4743 P should be a pointer to struct bpstat, but is defined as a void *
4744 in order for this function to be usable with catch_errors. */
c906108c
SS
4745
4746static int
4efb68b1 4747watchpoint_check (void *p)
c906108c
SS
4748{
4749 bpstat bs = (bpstat) p;
3a5c3e22 4750 struct watchpoint *b;
c906108c
SS
4751 struct frame_info *fr;
4752 int within_current_scope;
4753
f431efe5 4754 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4755 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4756 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4757
f6bc2008
PA
4758 /* If this is a local watchpoint, we only want to check if the
4759 watchpoint frame is in scope if the current thread is the thread
4760 that was used to create the watchpoint. */
4761 if (!watchpoint_in_thread_scope (b))
60e1c644 4762 return WP_IGNORE;
f6bc2008 4763
c906108c
SS
4764 if (b->exp_valid_block == NULL)
4765 within_current_scope = 1;
4766 else
4767 {
edb3359d
DJ
4768 struct frame_info *frame = get_current_frame ();
4769 struct gdbarch *frame_arch = get_frame_arch (frame);
4770 CORE_ADDR frame_pc = get_frame_pc (frame);
4771
4a64f543
MS
4772 /* in_function_epilogue_p() returns a non-zero value if we're
4773 still in the function but the stack frame has already been
4774 invalidated. Since we can't rely on the values of local
4775 variables after the stack has been destroyed, we are treating
4776 the watchpoint in that state as `not changed' without further
4777 checking. Don't mark watchpoints as changed if the current
4778 frame is in an epilogue - even if they are in some other
4779 frame, our view of the stack is likely to be wrong and
4780 frame_find_by_id could error out. */
a0f49112 4781 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4782 return WP_IGNORE;
a0f49112 4783
101dcfbe 4784 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4785 within_current_scope = (fr != NULL);
69fbadd5
DJ
4786
4787 /* If we've gotten confused in the unwinder, we might have
4788 returned a frame that can't describe this variable. */
edb3359d
DJ
4789 if (within_current_scope)
4790 {
4791 struct symbol *function;
4792
4793 function = get_frame_function (fr);
4794 if (function == NULL
4795 || !contained_in (b->exp_valid_block,
4796 SYMBOL_BLOCK_VALUE (function)))
4797 within_current_scope = 0;
4798 }
69fbadd5 4799
edb3359d 4800 if (within_current_scope)
c906108c
SS
4801 /* If we end up stopping, the current frame will get selected
4802 in normal_stop. So this call to select_frame won't affect
4803 the user. */
0f7d239c 4804 select_frame (fr);
c906108c 4805 }
c5aa993b 4806
c906108c
SS
4807 if (within_current_scope)
4808 {
4a64f543
MS
4809 /* We use value_{,free_to_}mark because it could be a *long*
4810 time before we return to the command level and call
4811 free_all_values. We can't call free_all_values because we
4812 might be in the middle of evaluating a function call. */
c906108c 4813
0cf6dd15 4814 int pc = 0;
9c06b0b4 4815 struct value *mark;
fa4727a6
DJ
4816 struct value *new_val;
4817
3a5c3e22 4818 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4819 /* Since we don't know the exact trigger address (from
4820 stopped_data_address), just tell the user we've triggered
4821 a mask watchpoint. */
4822 return WP_VALUE_CHANGED;
4823
4824 mark = value_mark ();
3a1115a0 4825 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 4826
4a64f543
MS
4827 /* We use value_equal_contents instead of value_equal because
4828 the latter coerces an array to a pointer, thus comparing just
4829 the address of the array instead of its contents. This is
4830 not what we want. */
fa4727a6 4831 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4832 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4833 {
fa4727a6
DJ
4834 if (new_val != NULL)
4835 {
4836 release_value (new_val);
4837 value_free_to_mark (mark);
4838 }
c906108c
SS
4839 bs->old_val = b->val;
4840 b->val = new_val;
fa4727a6 4841 b->val_valid = 1;
c906108c
SS
4842 return WP_VALUE_CHANGED;
4843 }
4844 else
4845 {
60e1c644 4846 /* Nothing changed. */
c906108c 4847 value_free_to_mark (mark);
c906108c
SS
4848 return WP_VALUE_NOT_CHANGED;
4849 }
4850 }
4851 else
4852 {
79a45e25
PA
4853 struct ui_out *uiout = current_uiout;
4854
c906108c 4855 /* This seems like the only logical thing to do because
c5aa993b
JM
4856 if we temporarily ignored the watchpoint, then when
4857 we reenter the block in which it is valid it contains
4858 garbage (in the case of a function, it may have two
4859 garbage values, one before and one after the prologue).
4860 So we can't even detect the first assignment to it and
4861 watch after that (since the garbage may or may not equal
4862 the first value assigned). */
348d480f
PA
4863 /* We print all the stop information in
4864 breakpoint_ops->print_it, but in this case, by the time we
4865 call breakpoint_ops->print_it this bp will be deleted
4866 already. So we have no choice but print the information
4867 here. */
9dc5e2a9 4868 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4869 ui_out_field_string
4870 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4871 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4872 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4873 ui_out_text (uiout,
4874 " deleted because the program has left the block in\n\
8b93c638 4875which its expression is valid.\n");
4ce44c66 4876
cdac0397 4877 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4878 decref_counted_command_line (&b->base.commands);
d0fb5eae 4879 watchpoint_del_at_next_stop (b);
c906108c
SS
4880
4881 return WP_DELETED;
4882 }
4883}
4884
18a18393 4885/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4886 breakpoint location BL. This function does not check if we should
4887 stop, only if BL explains the stop. */
4888
18a18393 4889static int
6c95b8df 4890bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4891 struct address_space *aspace, CORE_ADDR bp_addr,
4892 const struct target_waitstatus *ws)
18a18393
VP
4893{
4894 struct breakpoint *b = bl->owner;
4895
348d480f 4896 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4897 gdb_assert (b != NULL);
4898
09ac7c10 4899 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4900}
4901
3a5c3e22
PA
4902/* Determine if the watched values have actually changed, and we
4903 should stop. If not, set BS->stop to 0. */
4904
18a18393
VP
4905static void
4906bpstat_check_watchpoint (bpstat bs)
4907{
2bdf28a0 4908 const struct bp_location *bl;
3a5c3e22 4909 struct watchpoint *b;
2bdf28a0
JK
4910
4911 /* BS is built for existing struct breakpoint. */
f431efe5 4912 bl = bs->bp_location_at;
2bdf28a0 4913 gdb_assert (bl != NULL);
3a5c3e22 4914 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4915 gdb_assert (b != NULL);
18a18393 4916
18a18393 4917 {
18a18393
VP
4918 int must_check_value = 0;
4919
3a5c3e22 4920 if (b->base.type == bp_watchpoint)
18a18393
VP
4921 /* For a software watchpoint, we must always check the
4922 watched value. */
4923 must_check_value = 1;
4924 else if (b->watchpoint_triggered == watch_triggered_yes)
4925 /* We have a hardware watchpoint (read, write, or access)
4926 and the target earlier reported an address watched by
4927 this watchpoint. */
4928 must_check_value = 1;
4929 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4930 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4931 /* We were stopped by a hardware watchpoint, but the target could
4932 not report the data address. We must check the watchpoint's
4933 value. Access and read watchpoints are out of luck; without
4934 a data address, we can't figure it out. */
4935 must_check_value = 1;
3a5c3e22 4936
18a18393
VP
4937 if (must_check_value)
4938 {
3e43a32a
MS
4939 char *message
4940 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4941 b->base.number);
18a18393
VP
4942 struct cleanup *cleanups = make_cleanup (xfree, message);
4943 int e = catch_errors (watchpoint_check, bs, message,
4944 RETURN_MASK_ALL);
4945 do_cleanups (cleanups);
4946 switch (e)
4947 {
4948 case WP_DELETED:
4949 /* We've already printed what needs to be printed. */
4950 bs->print_it = print_it_done;
4951 /* Stop. */
4952 break;
60e1c644
PA
4953 case WP_IGNORE:
4954 bs->print_it = print_it_noop;
4955 bs->stop = 0;
4956 break;
18a18393 4957 case WP_VALUE_CHANGED:
3a5c3e22 4958 if (b->base.type == bp_read_watchpoint)
18a18393 4959 {
85d721b8
PA
4960 /* There are two cases to consider here:
4961
4a64f543 4962 1. We're watching the triggered memory for reads.
85d721b8
PA
4963 In that case, trust the target, and always report
4964 the watchpoint hit to the user. Even though
4965 reads don't cause value changes, the value may
4966 have changed since the last time it was read, and
4967 since we're not trapping writes, we will not see
4968 those, and as such we should ignore our notion of
4969 old value.
4970
4a64f543 4971 2. We're watching the triggered memory for both
85d721b8
PA
4972 reads and writes. There are two ways this may
4973 happen:
4974
4a64f543 4975 2.1. This is a target that can't break on data
85d721b8
PA
4976 reads only, but can break on accesses (reads or
4977 writes), such as e.g., x86. We detect this case
4978 at the time we try to insert read watchpoints.
4979
4a64f543 4980 2.2. Otherwise, the target supports read
85d721b8
PA
4981 watchpoints, but, the user set an access or write
4982 watchpoint watching the same memory as this read
4983 watchpoint.
4984
4985 If we're watching memory writes as well as reads,
4986 ignore watchpoint hits when we find that the
4987 value hasn't changed, as reads don't cause
4988 changes. This still gives false positives when
4989 the program writes the same value to memory as
4990 what there was already in memory (we will confuse
4991 it for a read), but it's much better than
4992 nothing. */
4993
4994 int other_write_watchpoint = 0;
4995
4996 if (bl->watchpoint_type == hw_read)
4997 {
4998 struct breakpoint *other_b;
4999
5000 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5001 if (other_b->type == bp_hardware_watchpoint
5002 || other_b->type == bp_access_watchpoint)
85d721b8 5003 {
3a5c3e22
PA
5004 struct watchpoint *other_w =
5005 (struct watchpoint *) other_b;
5006
5007 if (other_w->watchpoint_triggered
5008 == watch_triggered_yes)
5009 {
5010 other_write_watchpoint = 1;
5011 break;
5012 }
85d721b8
PA
5013 }
5014 }
5015
5016 if (other_write_watchpoint
5017 || bl->watchpoint_type == hw_access)
5018 {
5019 /* We're watching the same memory for writes,
5020 and the value changed since the last time we
5021 updated it, so this trap must be for a write.
5022 Ignore it. */
5023 bs->print_it = print_it_noop;
5024 bs->stop = 0;
5025 }
18a18393
VP
5026 }
5027 break;
5028 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5029 if (b->base.type == bp_hardware_watchpoint
5030 || b->base.type == bp_watchpoint)
18a18393
VP
5031 {
5032 /* Don't stop: write watchpoints shouldn't fire if
5033 the value hasn't changed. */
5034 bs->print_it = print_it_noop;
5035 bs->stop = 0;
5036 }
5037 /* Stop. */
5038 break;
5039 default:
5040 /* Can't happen. */
5041 case 0:
5042 /* Error from catch_errors. */
3a5c3e22 5043 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5044 watchpoint_del_at_next_stop (b);
18a18393
VP
5045 /* We've already printed what needs to be printed. */
5046 bs->print_it = print_it_done;
5047 break;
5048 }
5049 }
5050 else /* must_check_value == 0 */
5051 {
5052 /* This is a case where some watchpoint(s) triggered, but
5053 not at the address of this watchpoint, or else no
5054 watchpoint triggered after all. So don't print
5055 anything for this watchpoint. */
5056 bs->print_it = print_it_noop;
5057 bs->stop = 0;
5058 }
5059 }
5060}
5061
5062
5063/* Check conditions (condition proper, frame, thread and ignore count)
5064 of breakpoint referred to by BS. If we should not stop for this
5065 breakpoint, set BS->stop to 0. */
f431efe5 5066
18a18393
VP
5067static void
5068bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5069{
5070 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
5071 const struct bp_location *bl;
5072 struct breakpoint *b;
5073
5074 /* BS is built for existing struct breakpoint. */
f431efe5 5075 bl = bs->bp_location_at;
2bdf28a0 5076 gdb_assert (bl != NULL);
f431efe5 5077 b = bs->breakpoint_at;
2bdf28a0 5078 gdb_assert (b != NULL);
18a18393 5079
b775012e
LM
5080 /* Even if the target evaluated the condition on its end and notified GDB, we
5081 need to do so again since GDB does not know if we stopped due to a
5082 breakpoint or a single step breakpoint. */
5083
18a18393 5084 if (frame_id_p (b->frame_id)
edb3359d 5085 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
5086 bs->stop = 0;
5087 else if (bs->stop)
5088 {
5089 int value_is_zero = 0;
60e1c644
PA
5090 struct expression *cond;
5091
7371cf6d
PM
5092 /* Evaluate Python breakpoints that have a "stop"
5093 method implemented. */
5094 if (b->py_bp_object)
5095 bs->stop = gdbpy_should_stop (b->py_bp_object);
5096
60e1c644 5097 if (is_watchpoint (b))
3a5c3e22
PA
5098 {
5099 struct watchpoint *w = (struct watchpoint *) b;
5100
5101 cond = w->cond_exp;
5102 }
60e1c644
PA
5103 else
5104 cond = bl->cond;
5105
f431efe5 5106 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 5107 {
60e1c644 5108 int within_current_scope = 1;
3a5c3e22 5109 struct watchpoint * w;
60e1c644 5110
c5bc3a77
DJ
5111 /* We use value_mark and value_free_to_mark because it could
5112 be a long time before we return to the command level and
5113 call free_all_values. We can't call free_all_values
5114 because we might be in the middle of evaluating a
5115 function call. */
5116 struct value *mark = value_mark ();
5117
3a5c3e22
PA
5118 if (is_watchpoint (b))
5119 w = (struct watchpoint *) b;
5120 else
5121 w = NULL;
5122
edb3359d
DJ
5123 /* Need to select the frame, with all that implies so that
5124 the conditions will have the right context. Because we
5125 use the frame, we will not see an inlined function's
5126 variables when we arrive at a breakpoint at the start
5127 of the inlined function; the current frame will be the
5128 call site. */
3a5c3e22 5129 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
5130 select_frame (get_current_frame ());
5131 else
5132 {
5133 struct frame_info *frame;
5134
5135 /* For local watchpoint expressions, which particular
5136 instance of a local is being watched matters, so we
5137 keep track of the frame to evaluate the expression
5138 in. To evaluate the condition however, it doesn't
5139 really matter which instantiation of the function
5140 where the condition makes sense triggers the
5141 watchpoint. This allows an expression like "watch
5142 global if q > 10" set in `func', catch writes to
5143 global on all threads that call `func', or catch
5144 writes on all recursive calls of `func' by a single
5145 thread. We simply always evaluate the condition in
5146 the innermost frame that's executing where it makes
5147 sense to evaluate the condition. It seems
5148 intuitive. */
3a5c3e22 5149 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
5150 if (frame != NULL)
5151 select_frame (frame);
5152 else
5153 within_current_scope = 0;
5154 }
5155 if (within_current_scope)
5156 value_is_zero
5157 = catch_errors (breakpoint_cond_eval, cond,
5158 "Error in testing breakpoint condition:\n",
5159 RETURN_MASK_ALL);
5160 else
5161 {
5162 warning (_("Watchpoint condition cannot be tested "
5163 "in the current scope"));
5164 /* If we failed to set the right context for this
5165 watchpoint, unconditionally report it. */
5166 value_is_zero = 0;
5167 }
4a64f543 5168 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 5169 value_free_to_mark (mark);
18a18393 5170 }
60e1c644
PA
5171
5172 if (cond && value_is_zero)
18a18393
VP
5173 {
5174 bs->stop = 0;
5175 }
5176 else if (b->thread != -1 && b->thread != thread_id)
5177 {
5178 bs->stop = 0;
5179 }
5180 else if (b->ignore_count > 0)
5181 {
5182 b->ignore_count--;
18a18393 5183 bs->stop = 0;
4a64f543 5184 /* Increase the hit count even though we don't stop. */
18a18393 5185 ++(b->hit_count);
8d3788bd 5186 observer_notify_breakpoint_modified (b);
18a18393
VP
5187 }
5188 }
5189}
5190
5191
9709f61c 5192/* Get a bpstat associated with having just stopped at address
d983da9c 5193 BP_ADDR in thread PTID.
c906108c 5194
d983da9c 5195 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5196 don't understand this stop. Result is a chain of bpstat's such
5197 that:
c906108c 5198
c5aa993b 5199 if we don't understand the stop, the result is a null pointer.
c906108c 5200
c5aa993b 5201 if we understand why we stopped, the result is not null.
c906108c 5202
c5aa993b
JM
5203 Each element of the chain refers to a particular breakpoint or
5204 watchpoint at which we have stopped. (We may have stopped for
5205 several reasons concurrently.)
c906108c 5206
c5aa993b
JM
5207 Each element of the chain has valid next, breakpoint_at,
5208 commands, FIXME??? fields. */
c906108c
SS
5209
5210bpstat
6c95b8df 5211bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5212 CORE_ADDR bp_addr, ptid_t ptid,
5213 const struct target_waitstatus *ws)
c906108c 5214{
0d381245 5215 struct breakpoint *b = NULL;
afe38095 5216 struct bp_location *bl;
20874c92 5217 struct bp_location *loc;
5760d0ab
JK
5218 /* First item of allocated bpstat's. */
5219 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5220 /* Pointer to the last thing in the chain currently. */
5760d0ab 5221 bpstat bs;
20874c92 5222 int ix;
429374b8 5223 int need_remove_insert;
f431efe5 5224 int removed_any;
c906108c 5225
f431efe5
PA
5226 /* First, build the bpstat chain with locations that explain a
5227 target stop, while being careful to not set the target running,
5228 as that may invalidate locations (in particular watchpoint
5229 locations are recreated). Resuming will happen here with
5230 breakpoint conditions or watchpoint expressions that include
5231 inferior function calls. */
c5aa993b 5232
429374b8
JK
5233 ALL_BREAKPOINTS (b)
5234 {
5235 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5236 continue;
a5606eee 5237
429374b8
JK
5238 for (bl = b->loc; bl != NULL; bl = bl->next)
5239 {
4a64f543
MS
5240 /* For hardware watchpoints, we look only at the first
5241 location. The watchpoint_check function will work on the
5242 entire expression, not the individual locations. For
5243 read watchpoints, the watchpoints_triggered function has
5244 checked all locations already. */
429374b8
JK
5245 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5246 break;
18a18393 5247
f6592439 5248 if (!bl->enabled || bl->shlib_disabled)
429374b8 5249 continue;
c5aa993b 5250
09ac7c10 5251 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5252 continue;
c5aa993b 5253
4a64f543
MS
5254 /* Come here if it's a watchpoint, or if the break address
5255 matches. */
c5aa993b 5256
4a64f543
MS
5257 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5258 explain stop. */
c5aa993b 5259
f431efe5
PA
5260 /* Assume we stop. Should we find a watchpoint that is not
5261 actually triggered, or if the condition of the breakpoint
5262 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5263 bs->stop = 1;
5264 bs->print = 1;
d983da9c 5265
f431efe5
PA
5266 /* If this is a scope breakpoint, mark the associated
5267 watchpoint as triggered so that we will handle the
5268 out-of-scope event. We'll get to the watchpoint next
5269 iteration. */
d0fb5eae 5270 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5271 {
5272 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5273
5274 w->watchpoint_triggered = watch_triggered_yes;
5275 }
f431efe5
PA
5276 }
5277 }
5278
5279 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5280 {
f1310107 5281 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5282 {
5760d0ab 5283 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5284 /* For hits of moribund locations, we should just proceed. */
5285 bs->stop = 0;
5286 bs->print = 0;
5287 bs->print_it = print_it_noop;
5288 }
5289 }
5290
edcc5120
TT
5291 /* A bit of special processing for shlib breakpoints. We need to
5292 process solib loading here, so that the lists of loaded and
5293 unloaded libraries are correct before we handle "catch load" and
5294 "catch unload". */
5295 for (bs = bs_head; bs != NULL; bs = bs->next)
5296 {
5d268276 5297 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5298 {
5299 handle_solib_event ();
5300 break;
5301 }
5302 }
5303
f431efe5
PA
5304 /* Now go through the locations that caused the target to stop, and
5305 check whether we're interested in reporting this stop to higher
5306 layers, or whether we should resume the target transparently. */
5307
5308 removed_any = 0;
5309
5760d0ab 5310 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5311 {
5312 if (!bs->stop)
5313 continue;
5314
f431efe5 5315 b = bs->breakpoint_at;
348d480f
PA
5316 b->ops->check_status (bs);
5317 if (bs->stop)
28010a5d 5318 {
348d480f 5319 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5320
429374b8
JK
5321 if (bs->stop)
5322 {
5323 ++(b->hit_count);
8d3788bd 5324 observer_notify_breakpoint_modified (b);
c906108c 5325
4a64f543 5326 /* We will stop here. */
429374b8
JK
5327 if (b->disposition == disp_disable)
5328 {
816338b5
SS
5329 --(b->enable_count);
5330 if (b->enable_count <= 0
5331 && b->enable_state != bp_permanent)
429374b8 5332 b->enable_state = bp_disabled;
f431efe5 5333 removed_any = 1;
429374b8
JK
5334 }
5335 if (b->silent)
5336 bs->print = 0;
5337 bs->commands = b->commands;
9add0f1b 5338 incref_counted_command_line (bs->commands);
abf85f46
JK
5339 if (command_line_is_silent (bs->commands
5340 ? bs->commands->commands : NULL))
5341 bs->print = 0;
9d6e6e84
HZ
5342
5343 b->ops->after_condition_true (bs);
429374b8
JK
5344 }
5345
348d480f 5346 }
a9b3a50f
PA
5347
5348 /* Print nothing for this entry if we don't stop or don't
5349 print. */
5350 if (!bs->stop || !bs->print)
5351 bs->print_it = print_it_noop;
429374b8 5352 }
876fa593 5353
d983da9c
DJ
5354 /* If we aren't stopping, the value of some hardware watchpoint may
5355 not have changed, but the intermediate memory locations we are
5356 watching may have. Don't bother if we're stopping; this will get
5357 done later. */
d832cb68 5358 need_remove_insert = 0;
5760d0ab
JK
5359 if (! bpstat_causes_stop (bs_head))
5360 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5361 if (!bs->stop
f431efe5
PA
5362 && bs->breakpoint_at
5363 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5364 {
3a5c3e22
PA
5365 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5366
5367 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5368 need_remove_insert = 1;
d983da9c
DJ
5369 }
5370
d832cb68 5371 if (need_remove_insert)
2d134ed3 5372 update_global_location_list (1);
f431efe5
PA
5373 else if (removed_any)
5374 update_global_location_list (0);
d832cb68 5375
5760d0ab 5376 return bs_head;
c906108c 5377}
628fe4e4
JK
5378
5379static void
5380handle_jit_event (void)
5381{
5382 struct frame_info *frame;
5383 struct gdbarch *gdbarch;
5384
5385 /* Switch terminal for any messages produced by
5386 breakpoint_re_set. */
5387 target_terminal_ours_for_output ();
5388
5389 frame = get_current_frame ();
5390 gdbarch = get_frame_arch (frame);
5391
5392 jit_event_handler (gdbarch);
5393
5394 target_terminal_inferior ();
5395}
5396
5397/* Prepare WHAT final decision for infrun. */
5398
5399/* Decide what infrun needs to do with this bpstat. */
5400
c906108c 5401struct bpstat_what
0e30163f 5402bpstat_what (bpstat bs_head)
c906108c 5403{
c906108c 5404 struct bpstat_what retval;
628fe4e4 5405 int jit_event = 0;
0e30163f 5406 bpstat bs;
c906108c 5407
628fe4e4 5408 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5409 retval.call_dummy = STOP_NONE;
186c406b 5410 retval.is_longjmp = 0;
628fe4e4 5411
0e30163f 5412 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5413 {
628fe4e4
JK
5414 /* Extract this BS's action. After processing each BS, we check
5415 if its action overrides all we've seem so far. */
5416 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5417 enum bptype bptype;
5418
c906108c 5419 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5420 {
5421 /* I suspect this can happen if it was a momentary
5422 breakpoint which has since been deleted. */
5423 bptype = bp_none;
5424 }
20874c92 5425 else
f431efe5 5426 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5427
5428 switch (bptype)
c906108c
SS
5429 {
5430 case bp_none:
628fe4e4 5431 break;
c906108c
SS
5432 case bp_breakpoint:
5433 case bp_hardware_breakpoint:
5434 case bp_until:
5435 case bp_finish:
a9b3a50f 5436 case bp_shlib_event:
c906108c
SS
5437 if (bs->stop)
5438 {
5439 if (bs->print)
628fe4e4 5440 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5441 else
628fe4e4 5442 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5443 }
5444 else
628fe4e4 5445 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5446 break;
5447 case bp_watchpoint:
5448 case bp_hardware_watchpoint:
5449 case bp_read_watchpoint:
5450 case bp_access_watchpoint:
5451 if (bs->stop)
5452 {
5453 if (bs->print)
628fe4e4 5454 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5455 else
628fe4e4 5456 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5457 }
5458 else
628fe4e4
JK
5459 {
5460 /* There was a watchpoint, but we're not stopping.
5461 This requires no further action. */
5462 }
c906108c
SS
5463 break;
5464 case bp_longjmp:
e2e4d78b 5465 case bp_longjmp_call_dummy:
186c406b 5466 case bp_exception:
628fe4e4 5467 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5468 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5469 break;
5470 case bp_longjmp_resume:
186c406b 5471 case bp_exception_resume:
628fe4e4 5472 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5473 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5474 break;
5475 case bp_step_resume:
5476 if (bs->stop)
628fe4e4
JK
5477 this_action = BPSTAT_WHAT_STEP_RESUME;
5478 else
c906108c 5479 {
628fe4e4
JK
5480 /* It is for the wrong frame. */
5481 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5482 }
c906108c 5483 break;
2c03e5be
PA
5484 case bp_hp_step_resume:
5485 if (bs->stop)
5486 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5487 else
5488 {
5489 /* It is for the wrong frame. */
5490 this_action = BPSTAT_WHAT_SINGLE;
5491 }
5492 break;
c906108c 5493 case bp_watchpoint_scope:
c4093a6a 5494 case bp_thread_event:
1900040c 5495 case bp_overlay_event:
0fd8e87f 5496 case bp_longjmp_master:
aa7d318d 5497 case bp_std_terminate_master:
186c406b 5498 case bp_exception_master:
628fe4e4 5499 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5500 break;
ce78b96d 5501 case bp_catchpoint:
c5aa993b
JM
5502 if (bs->stop)
5503 {
5504 if (bs->print)
628fe4e4 5505 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5506 else
628fe4e4 5507 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5508 }
5509 else
628fe4e4
JK
5510 {
5511 /* There was a catchpoint, but we're not stopping.
5512 This requires no further action. */
5513 }
5514 break;
628fe4e4
JK
5515 case bp_jit_event:
5516 jit_event = 1;
5517 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5518 break;
c906108c 5519 case bp_call_dummy:
53a5351d
JM
5520 /* Make sure the action is stop (silent or noisy),
5521 so infrun.c pops the dummy frame. */
aa7d318d 5522 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5523 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5524 break;
5525 case bp_std_terminate:
5526 /* Make sure the action is stop (silent or noisy),
5527 so infrun.c pops the dummy frame. */
aa7d318d 5528 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5529 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5530 break;
1042e4c0 5531 case bp_tracepoint:
7a697b8d 5532 case bp_fast_tracepoint:
0fb4aa4b 5533 case bp_static_tracepoint:
1042e4c0
SS
5534 /* Tracepoint hits should not be reported back to GDB, and
5535 if one got through somehow, it should have been filtered
5536 out already. */
5537 internal_error (__FILE__, __LINE__,
7a697b8d 5538 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5539 break;
5540 case bp_gnu_ifunc_resolver:
5541 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5542 this_action = BPSTAT_WHAT_SINGLE;
5543 break;
5544 case bp_gnu_ifunc_resolver_return:
5545 /* The breakpoint will be removed, execution will restart from the
5546 PC of the former breakpoint. */
5547 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5548 break;
e7e0cddf
SS
5549
5550 case bp_dprintf:
a11cfd87
HZ
5551 if (bs->stop)
5552 this_action = BPSTAT_WHAT_STOP_SILENT;
5553 else
5554 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5555 break;
5556
628fe4e4
JK
5557 default:
5558 internal_error (__FILE__, __LINE__,
5559 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5560 }
628fe4e4
JK
5561
5562 retval.main_action = max (retval.main_action, this_action);
c906108c 5563 }
628fe4e4 5564
0e30163f
JK
5565 /* These operations may affect the bs->breakpoint_at state so they are
5566 delayed after MAIN_ACTION is decided above. */
5567
628fe4e4
JK
5568 if (jit_event)
5569 {
5570 if (debug_infrun)
5571 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5572
5573 handle_jit_event ();
5574 }
5575
0e30163f
JK
5576 for (bs = bs_head; bs != NULL; bs = bs->next)
5577 {
5578 struct breakpoint *b = bs->breakpoint_at;
5579
5580 if (b == NULL)
5581 continue;
5582 switch (b->type)
5583 {
5584 case bp_gnu_ifunc_resolver:
5585 gnu_ifunc_resolver_stop (b);
5586 break;
5587 case bp_gnu_ifunc_resolver_return:
5588 gnu_ifunc_resolver_return_stop (b);
5589 break;
5590 }
5591 }
5592
c906108c
SS
5593 return retval;
5594}
5595
5596/* Nonzero if we should step constantly (e.g. watchpoints on machines
5597 without hardware support). This isn't related to a specific bpstat,
5598 just to things like whether watchpoints are set. */
5599
c5aa993b 5600int
fba45db2 5601bpstat_should_step (void)
c906108c
SS
5602{
5603 struct breakpoint *b;
cc59ec59 5604
c906108c 5605 ALL_BREAKPOINTS (b)
717a8278 5606 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5607 return 1;
c906108c
SS
5608 return 0;
5609}
5610
67822962
PA
5611int
5612bpstat_causes_stop (bpstat bs)
5613{
5614 for (; bs != NULL; bs = bs->next)
5615 if (bs->stop)
5616 return 1;
5617
5618 return 0;
5619}
5620
c906108c 5621\f
c5aa993b 5622
170b53b2
UW
5623/* Compute a string of spaces suitable to indent the next line
5624 so it starts at the position corresponding to the table column
5625 named COL_NAME in the currently active table of UIOUT. */
5626
5627static char *
5628wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5629{
5630 static char wrap_indent[80];
5631 int i, total_width, width, align;
5632 char *text;
5633
5634 total_width = 0;
5635 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5636 {
5637 if (strcmp (text, col_name) == 0)
5638 {
5639 gdb_assert (total_width < sizeof wrap_indent);
5640 memset (wrap_indent, ' ', total_width);
5641 wrap_indent[total_width] = 0;
5642
5643 return wrap_indent;
5644 }
5645
5646 total_width += width + 1;
5647 }
5648
5649 return NULL;
5650}
5651
b775012e
LM
5652/* Determine if the locations of this breakpoint will have their conditions
5653 evaluated by the target, host or a mix of both. Returns the following:
5654
5655 "host": Host evals condition.
5656 "host or target": Host or Target evals condition.
5657 "target": Target evals condition.
5658*/
5659
5660static const char *
5661bp_condition_evaluator (struct breakpoint *b)
5662{
5663 struct bp_location *bl;
5664 char host_evals = 0;
5665 char target_evals = 0;
5666
5667 if (!b)
5668 return NULL;
5669
5670 if (!is_breakpoint (b))
5671 return NULL;
5672
5673 if (gdb_evaluates_breakpoint_condition_p ()
5674 || !target_supports_evaluation_of_breakpoint_conditions ())
5675 return condition_evaluation_host;
5676
5677 for (bl = b->loc; bl; bl = bl->next)
5678 {
5679 if (bl->cond_bytecode)
5680 target_evals++;
5681 else
5682 host_evals++;
5683 }
5684
5685 if (host_evals && target_evals)
5686 return condition_evaluation_both;
5687 else if (target_evals)
5688 return condition_evaluation_target;
5689 else
5690 return condition_evaluation_host;
5691}
5692
5693/* Determine the breakpoint location's condition evaluator. This is
5694 similar to bp_condition_evaluator, but for locations. */
5695
5696static const char *
5697bp_location_condition_evaluator (struct bp_location *bl)
5698{
5699 if (bl && !is_breakpoint (bl->owner))
5700 return NULL;
5701
5702 if (gdb_evaluates_breakpoint_condition_p ()
5703 || !target_supports_evaluation_of_breakpoint_conditions ())
5704 return condition_evaluation_host;
5705
5706 if (bl && bl->cond_bytecode)
5707 return condition_evaluation_target;
5708 else
5709 return condition_evaluation_host;
5710}
5711
859825b8
JK
5712/* Print the LOC location out of the list of B->LOC locations. */
5713
170b53b2
UW
5714static void
5715print_breakpoint_location (struct breakpoint *b,
5716 struct bp_location *loc)
0d381245 5717{
79a45e25 5718 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5719 struct cleanup *old_chain = save_current_program_space ();
5720
859825b8
JK
5721 if (loc != NULL && loc->shlib_disabled)
5722 loc = NULL;
5723
6c95b8df
PA
5724 if (loc != NULL)
5725 set_current_program_space (loc->pspace);
5726
56435ebe
TT
5727 if (b->display_canonical)
5728 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5729 else if (loc && loc->symtab)
0d381245
VP
5730 {
5731 struct symbol *sym
5732 = find_pc_sect_function (loc->address, loc->section);
5733 if (sym)
5734 {
5735 ui_out_text (uiout, "in ");
5736 ui_out_field_string (uiout, "func",
5737 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5738 ui_out_text (uiout, " ");
5739 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5740 ui_out_text (uiout, "at ");
0d381245 5741 }
05cba821
JK
5742 ui_out_field_string (uiout, "file",
5743 symtab_to_filename_for_display (loc->symtab));
0d381245 5744 ui_out_text (uiout, ":");
05cba821 5745
0d381245 5746 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5747 ui_out_field_string (uiout, "fullname",
5748 symtab_to_fullname (loc->symtab));
0d381245 5749
f8eba3c6 5750 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5751 }
859825b8 5752 else if (loc)
0d381245 5753 {
f99d8bf4
PA
5754 struct ui_file *stb = mem_fileopen ();
5755 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5756
f99d8bf4 5757 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5758 demangle, "");
0d381245 5759 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5760
5761 do_cleanups (stb_chain);
0d381245 5762 }
859825b8
JK
5763 else
5764 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5765
b775012e
LM
5766 if (loc && is_breakpoint (b)
5767 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5768 && bp_condition_evaluator (b) == condition_evaluation_both)
5769 {
5770 ui_out_text (uiout, " (");
5771 ui_out_field_string (uiout, "evaluated-by",
5772 bp_location_condition_evaluator (loc));
5773 ui_out_text (uiout, ")");
5774 }
5775
6c95b8df 5776 do_cleanups (old_chain);
0d381245
VP
5777}
5778
269b11a2
PA
5779static const char *
5780bptype_string (enum bptype type)
c906108c 5781{
c4093a6a
JM
5782 struct ep_type_description
5783 {
5784 enum bptype type;
5785 char *description;
5786 };
5787 static struct ep_type_description bptypes[] =
c906108c 5788 {
c5aa993b
JM
5789 {bp_none, "?deleted?"},
5790 {bp_breakpoint, "breakpoint"},
c906108c 5791 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5792 {bp_until, "until"},
5793 {bp_finish, "finish"},
5794 {bp_watchpoint, "watchpoint"},
c906108c 5795 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5796 {bp_read_watchpoint, "read watchpoint"},
5797 {bp_access_watchpoint, "acc watchpoint"},
5798 {bp_longjmp, "longjmp"},
5799 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5800 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5801 {bp_exception, "exception"},
5802 {bp_exception_resume, "exception resume"},
c5aa993b 5803 {bp_step_resume, "step resume"},
2c03e5be 5804 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5805 {bp_watchpoint_scope, "watchpoint scope"},
5806 {bp_call_dummy, "call dummy"},
aa7d318d 5807 {bp_std_terminate, "std::terminate"},
c5aa993b 5808 {bp_shlib_event, "shlib events"},
c4093a6a 5809 {bp_thread_event, "thread events"},
1900040c 5810 {bp_overlay_event, "overlay events"},
0fd8e87f 5811 {bp_longjmp_master, "longjmp master"},
aa7d318d 5812 {bp_std_terminate_master, "std::terminate master"},
186c406b 5813 {bp_exception_master, "exception master"},
ce78b96d 5814 {bp_catchpoint, "catchpoint"},
1042e4c0 5815 {bp_tracepoint, "tracepoint"},
7a697b8d 5816 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5817 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5818 {bp_dprintf, "dprintf"},
4efc6507 5819 {bp_jit_event, "jit events"},
0e30163f
JK
5820 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5821 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5822 };
269b11a2
PA
5823
5824 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5825 || ((int) type != bptypes[(int) type].type))
5826 internal_error (__FILE__, __LINE__,
5827 _("bptypes table does not describe type #%d."),
5828 (int) type);
5829
5830 return bptypes[(int) type].description;
5831}
5832
998580f1
MK
5833/* For MI, output a field named 'thread-groups' with a list as the value.
5834 For CLI, prefix the list with the string 'inf'. */
5835
5836static void
5837output_thread_groups (struct ui_out *uiout,
5838 const char *field_name,
5839 VEC(int) *inf_num,
5840 int mi_only)
5841{
752eb8b4 5842 struct cleanup *back_to;
998580f1
MK
5843 int is_mi = ui_out_is_mi_like_p (uiout);
5844 int inf;
5845 int i;
5846
5847 /* For backward compatibility, don't display inferiors in CLI unless
5848 there are several. Always display them for MI. */
5849 if (!is_mi && mi_only)
5850 return;
5851
752eb8b4
TT
5852 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
5853
998580f1
MK
5854 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5855 {
5856 if (is_mi)
5857 {
5858 char mi_group[10];
5859
5860 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5861 ui_out_field_string (uiout, NULL, mi_group);
5862 }
5863 else
5864 {
5865 if (i == 0)
5866 ui_out_text (uiout, " inf ");
5867 else
5868 ui_out_text (uiout, ", ");
5869
5870 ui_out_text (uiout, plongest (inf));
5871 }
5872 }
5873
5874 do_cleanups (back_to);
5875}
5876
269b11a2
PA
5877/* Print B to gdb_stdout. */
5878
5879static void
5880print_one_breakpoint_location (struct breakpoint *b,
5881 struct bp_location *loc,
5882 int loc_number,
5883 struct bp_location **last_loc,
269b11a2
PA
5884 int allflag)
5885{
5886 struct command_line *l;
c2c6d25f 5887 static char bpenables[] = "nynny";
c906108c 5888
79a45e25 5889 struct ui_out *uiout = current_uiout;
0d381245
VP
5890 int header_of_multiple = 0;
5891 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5892 struct value_print_options opts;
5893
5894 get_user_print_options (&opts);
0d381245
VP
5895
5896 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5897 /* See comment in print_one_breakpoint concerning treatment of
5898 breakpoints with single disabled location. */
0d381245
VP
5899 if (loc == NULL
5900 && (b->loc != NULL
5901 && (b->loc->next != NULL || !b->loc->enabled)))
5902 header_of_multiple = 1;
5903 if (loc == NULL)
5904 loc = b->loc;
5905
c4093a6a
JM
5906 annotate_record ();
5907
5908 /* 1 */
5909 annotate_field (0);
0d381245
VP
5910 if (part_of_multiple)
5911 {
5912 char *formatted;
0c6773c1 5913 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5914 ui_out_field_string (uiout, "number", formatted);
5915 xfree (formatted);
5916 }
5917 else
5918 {
5919 ui_out_field_int (uiout, "number", b->number);
5920 }
c4093a6a
JM
5921
5922 /* 2 */
5923 annotate_field (1);
0d381245
VP
5924 if (part_of_multiple)
5925 ui_out_field_skip (uiout, "type");
269b11a2
PA
5926 else
5927 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5928
5929 /* 3 */
5930 annotate_field (2);
0d381245
VP
5931 if (part_of_multiple)
5932 ui_out_field_skip (uiout, "disp");
5933 else
2cec12e5 5934 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5935
c4093a6a
JM
5936
5937 /* 4 */
5938 annotate_field (3);
0d381245 5939 if (part_of_multiple)
54e52265 5940 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5941 else
4a64f543
MS
5942 ui_out_field_fmt (uiout, "enabled", "%c",
5943 bpenables[(int) b->enable_state]);
54e52265 5944 ui_out_spaces (uiout, 2);
0d381245 5945
c4093a6a
JM
5946
5947 /* 5 and 6 */
3086aeae 5948 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5949 {
4a64f543
MS
5950 /* Although the print_one can possibly print all locations,
5951 calling it here is not likely to get any nice result. So,
5952 make sure there's just one location. */
0d381245 5953 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5954 b->ops->print_one (b, last_loc);
0d381245 5955 }
3086aeae
DJ
5956 else
5957 switch (b->type)
5958 {
5959 case bp_none:
5960 internal_error (__FILE__, __LINE__,
e2e0b3e5 5961 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5962 break;
c906108c 5963
3086aeae
DJ
5964 case bp_watchpoint:
5965 case bp_hardware_watchpoint:
5966 case bp_read_watchpoint:
5967 case bp_access_watchpoint:
3a5c3e22
PA
5968 {
5969 struct watchpoint *w = (struct watchpoint *) b;
5970
5971 /* Field 4, the address, is omitted (which makes the columns
5972 not line up too nicely with the headers, but the effect
5973 is relatively readable). */
5974 if (opts.addressprint)
5975 ui_out_field_skip (uiout, "addr");
5976 annotate_field (5);
5977 ui_out_field_string (uiout, "what", w->exp_string);
5978 }
3086aeae
DJ
5979 break;
5980
3086aeae
DJ
5981 case bp_breakpoint:
5982 case bp_hardware_breakpoint:
5983 case bp_until:
5984 case bp_finish:
5985 case bp_longjmp:
5986 case bp_longjmp_resume:
e2e4d78b 5987 case bp_longjmp_call_dummy:
186c406b
TT
5988 case bp_exception:
5989 case bp_exception_resume:
3086aeae 5990 case bp_step_resume:
2c03e5be 5991 case bp_hp_step_resume:
3086aeae
DJ
5992 case bp_watchpoint_scope:
5993 case bp_call_dummy:
aa7d318d 5994 case bp_std_terminate:
3086aeae
DJ
5995 case bp_shlib_event:
5996 case bp_thread_event:
5997 case bp_overlay_event:
0fd8e87f 5998 case bp_longjmp_master:
aa7d318d 5999 case bp_std_terminate_master:
186c406b 6000 case bp_exception_master:
1042e4c0 6001 case bp_tracepoint:
7a697b8d 6002 case bp_fast_tracepoint:
0fb4aa4b 6003 case bp_static_tracepoint:
e7e0cddf 6004 case bp_dprintf:
4efc6507 6005 case bp_jit_event:
0e30163f
JK
6006 case bp_gnu_ifunc_resolver:
6007 case bp_gnu_ifunc_resolver_return:
79a45b7d 6008 if (opts.addressprint)
3086aeae
DJ
6009 {
6010 annotate_field (4);
54e52265 6011 if (header_of_multiple)
0d381245 6012 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6013 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6014 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6015 else
5af949e3
UW
6016 ui_out_field_core_addr (uiout, "addr",
6017 loc->gdbarch, loc->address);
3086aeae
DJ
6018 }
6019 annotate_field (5);
0d381245 6020 if (!header_of_multiple)
170b53b2 6021 print_breakpoint_location (b, loc);
0d381245 6022 if (b->loc)
a6d9a66e 6023 *last_loc = b->loc;
3086aeae
DJ
6024 break;
6025 }
c906108c 6026
6c95b8df 6027
998580f1 6028 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6029 {
6030 struct inferior *inf;
998580f1
MK
6031 VEC(int) *inf_num = NULL;
6032 int mi_only = 1;
6c95b8df 6033
998580f1 6034 ALL_INFERIORS (inf)
6c95b8df
PA
6035 {
6036 if (inf->pspace == loc->pspace)
998580f1 6037 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6038 }
998580f1
MK
6039
6040 /* For backward compatibility, don't display inferiors in CLI unless
6041 there are several. Always display for MI. */
6042 if (allflag
6043 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6044 && (number_of_program_spaces () > 1
6045 || number_of_inferiors () > 1)
6046 /* LOC is for existing B, it cannot be in
6047 moribund_locations and thus having NULL OWNER. */
6048 && loc->owner->type != bp_catchpoint))
6049 mi_only = 0;
6050 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6051 VEC_free (int, inf_num);
6c95b8df
PA
6052 }
6053
4a306c9a 6054 if (!part_of_multiple)
c4093a6a 6055 {
4a306c9a
JB
6056 if (b->thread != -1)
6057 {
6058 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6059 "stop only in" line a little further down. */
4a306c9a
JB
6060 ui_out_text (uiout, " thread ");
6061 ui_out_field_int (uiout, "thread", b->thread);
6062 }
6063 else if (b->task != 0)
6064 {
6065 ui_out_text (uiout, " task ");
6066 ui_out_field_int (uiout, "task", b->task);
6067 }
c4093a6a 6068 }
f1310107 6069
8b93c638 6070 ui_out_text (uiout, "\n");
f1310107 6071
348d480f 6072 if (!part_of_multiple)
f1310107
TJB
6073 b->ops->print_one_detail (b, uiout);
6074
0d381245 6075 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6076 {
6077 annotate_field (6);
8b93c638 6078 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6079 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6080 the frame ID. */
5af949e3
UW
6081 ui_out_field_core_addr (uiout, "frame",
6082 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6083 ui_out_text (uiout, "\n");
c4093a6a
JM
6084 }
6085
28010a5d 6086 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6087 {
6088 annotate_field (7);
d77f58be 6089 if (is_tracepoint (b))
1042e4c0
SS
6090 ui_out_text (uiout, "\ttrace only if ");
6091 else
6092 ui_out_text (uiout, "\tstop only if ");
0101ce28 6093 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6094
6095 /* Print whether the target is doing the breakpoint's condition
6096 evaluation. If GDB is doing the evaluation, don't print anything. */
6097 if (is_breakpoint (b)
6098 && breakpoint_condition_evaluation_mode ()
6099 == condition_evaluation_target)
6100 {
6101 ui_out_text (uiout, " (");
6102 ui_out_field_string (uiout, "evaluated-by",
6103 bp_condition_evaluator (b));
6104 ui_out_text (uiout, " evals)");
6105 }
0101ce28
JJ
6106 ui_out_text (uiout, "\n");
6107 }
6108
0d381245 6109 if (!part_of_multiple && b->thread != -1)
c4093a6a 6110 {
4a64f543 6111 /* FIXME should make an annotation for this. */
8b93c638
JM
6112 ui_out_text (uiout, "\tstop only in thread ");
6113 ui_out_field_int (uiout, "thread", b->thread);
6114 ui_out_text (uiout, "\n");
c4093a6a
JM
6115 }
6116
556ec64d
YQ
6117 if (!part_of_multiple)
6118 {
6119 if (b->hit_count)
31f56a27
YQ
6120 {
6121 /* FIXME should make an annotation for this. */
6122 if (is_catchpoint (b))
6123 ui_out_text (uiout, "\tcatchpoint");
6124 else if (is_tracepoint (b))
6125 ui_out_text (uiout, "\ttracepoint");
6126 else
6127 ui_out_text (uiout, "\tbreakpoint");
6128 ui_out_text (uiout, " already hit ");
6129 ui_out_field_int (uiout, "times", b->hit_count);
6130 if (b->hit_count == 1)
6131 ui_out_text (uiout, " time\n");
6132 else
6133 ui_out_text (uiout, " times\n");
6134 }
556ec64d
YQ
6135 else
6136 {
31f56a27
YQ
6137 /* Output the count also if it is zero, but only if this is mi. */
6138 if (ui_out_is_mi_like_p (uiout))
6139 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6140 }
6141 }
8b93c638 6142
0d381245 6143 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6144 {
6145 annotate_field (8);
8b93c638
JM
6146 ui_out_text (uiout, "\tignore next ");
6147 ui_out_field_int (uiout, "ignore", b->ignore_count);
6148 ui_out_text (uiout, " hits\n");
c4093a6a 6149 }
059fb39f 6150
816338b5
SS
6151 /* Note that an enable count of 1 corresponds to "enable once"
6152 behavior, which is reported by the combination of enablement and
6153 disposition, so we don't need to mention it here. */
6154 if (!part_of_multiple && b->enable_count > 1)
6155 {
6156 annotate_field (8);
6157 ui_out_text (uiout, "\tdisable after ");
6158 /* Tweak the wording to clarify that ignore and enable counts
6159 are distinct, and have additive effect. */
6160 if (b->ignore_count)
6161 ui_out_text (uiout, "additional ");
6162 else
6163 ui_out_text (uiout, "next ");
6164 ui_out_field_int (uiout, "enable", b->enable_count);
6165 ui_out_text (uiout, " hits\n");
6166 }
6167
f196051f
SS
6168 if (!part_of_multiple && is_tracepoint (b))
6169 {
6170 struct tracepoint *tp = (struct tracepoint *) b;
6171
6172 if (tp->traceframe_usage)
6173 {
6174 ui_out_text (uiout, "\ttrace buffer usage ");
6175 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6176 ui_out_text (uiout, " bytes\n");
6177 }
6178 }
d3ce09f5 6179
9add0f1b 6180 l = b->commands ? b->commands->commands : NULL;
059fb39f 6181 if (!part_of_multiple && l)
c4093a6a 6182 {
3b31d625
EZ
6183 struct cleanup *script_chain;
6184
c4093a6a 6185 annotate_field (9);
3b31d625 6186 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6187 print_command_lines (uiout, l, 4);
3b31d625 6188 do_cleanups (script_chain);
c4093a6a 6189 }
d24317b4 6190
d9b3f62e 6191 if (is_tracepoint (b))
1042e4c0 6192 {
d9b3f62e
PA
6193 struct tracepoint *t = (struct tracepoint *) b;
6194
6195 if (!part_of_multiple && t->pass_count)
6196 {
6197 annotate_field (10);
6198 ui_out_text (uiout, "\tpass count ");
6199 ui_out_field_int (uiout, "pass", t->pass_count);
6200 ui_out_text (uiout, " \n");
6201 }
f2a8bc8a
YQ
6202
6203 /* Don't display it when tracepoint or tracepoint location is
6204 pending. */
6205 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6206 {
6207 annotate_field (11);
6208
6209 if (ui_out_is_mi_like_p (uiout))
6210 ui_out_field_string (uiout, "installed",
6211 loc->inserted ? "y" : "n");
6212 else
6213 {
6214 if (loc->inserted)
6215 ui_out_text (uiout, "\t");
6216 else
6217 ui_out_text (uiout, "\tnot ");
6218 ui_out_text (uiout, "installed on target\n");
6219 }
6220 }
1042e4c0
SS
6221 }
6222
d24317b4
VP
6223 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6224 {
3a5c3e22
PA
6225 if (is_watchpoint (b))
6226 {
6227 struct watchpoint *w = (struct watchpoint *) b;
6228
6229 ui_out_field_string (uiout, "original-location", w->exp_string);
6230 }
6231 else if (b->addr_string)
d24317b4 6232 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6233 }
c4093a6a 6234}
c5aa993b 6235
0d381245
VP
6236static void
6237print_one_breakpoint (struct breakpoint *b,
4a64f543 6238 struct bp_location **last_loc,
6c95b8df 6239 int allflag)
0d381245 6240{
8d3788bd 6241 struct cleanup *bkpt_chain;
79a45e25 6242 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6243
6244 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6245
12c5a436 6246 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6247 do_cleanups (bkpt_chain);
0d381245
VP
6248
6249 /* If this breakpoint has custom print function,
6250 it's already printed. Otherwise, print individual
6251 locations, if any. */
6252 if (b->ops == NULL || b->ops->print_one == NULL)
6253 {
4a64f543
MS
6254 /* If breakpoint has a single location that is disabled, we
6255 print it as if it had several locations, since otherwise it's
6256 hard to represent "breakpoint enabled, location disabled"
6257 situation.
6258
6259 Note that while hardware watchpoints have several locations
a3be7890 6260 internally, that's not a property exposed to user. */
0d381245 6261 if (b->loc
a5606eee 6262 && !is_hardware_watchpoint (b)
8d3788bd 6263 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6264 {
6265 struct bp_location *loc;
6266 int n = 1;
8d3788bd 6267
0d381245 6268 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6269 {
6270 struct cleanup *inner2 =
6271 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6272 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6273 do_cleanups (inner2);
6274 }
0d381245
VP
6275 }
6276 }
6277}
6278
a6d9a66e
UW
6279static int
6280breakpoint_address_bits (struct breakpoint *b)
6281{
6282 int print_address_bits = 0;
6283 struct bp_location *loc;
6284
6285 for (loc = b->loc; loc; loc = loc->next)
6286 {
c7437ca6
PA
6287 int addr_bit;
6288
6289 /* Software watchpoints that aren't watching memory don't have
6290 an address to print. */
6291 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6292 continue;
6293
6294 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6295 if (addr_bit > print_address_bits)
6296 print_address_bits = addr_bit;
6297 }
6298
6299 return print_address_bits;
6300}
0d381245 6301
c4093a6a
JM
6302struct captured_breakpoint_query_args
6303 {
6304 int bnum;
6305 };
c5aa993b 6306
c4093a6a 6307static int
2b65245e 6308do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6309{
6310 struct captured_breakpoint_query_args *args = data;
52f0bd74 6311 struct breakpoint *b;
a6d9a66e 6312 struct bp_location *dummy_loc = NULL;
cc59ec59 6313
c4093a6a
JM
6314 ALL_BREAKPOINTS (b)
6315 {
6316 if (args->bnum == b->number)
c5aa993b 6317 {
12c5a436 6318 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6319 return GDB_RC_OK;
c5aa993b 6320 }
c4093a6a
JM
6321 }
6322 return GDB_RC_NONE;
6323}
c5aa993b 6324
c4093a6a 6325enum gdb_rc
4a64f543
MS
6326gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6327 char **error_message)
c4093a6a
JM
6328{
6329 struct captured_breakpoint_query_args args;
cc59ec59 6330
c4093a6a
JM
6331 args.bnum = bnum;
6332 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6333 an error. */
b0b13bb4
DJ
6334 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6335 error_message, RETURN_MASK_ALL) < 0)
6336 return GDB_RC_FAIL;
6337 else
6338 return GDB_RC_OK;
c4093a6a 6339}
c5aa993b 6340
09d682a4
TT
6341/* Return true if this breakpoint was set by the user, false if it is
6342 internal or momentary. */
6343
6344int
6345user_breakpoint_p (struct breakpoint *b)
6346{
46c6471b 6347 return b->number > 0;
09d682a4
TT
6348}
6349
7f3b0473 6350/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6351 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6352 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6353 FILTER is non-NULL, call it on each breakpoint and only include the
6354 ones for which it returns non-zero. Return the total number of
6355 breakpoints listed. */
c906108c 6356
d77f58be 6357static int
e5a67952 6358breakpoint_1 (char *args, int allflag,
4a64f543 6359 int (*filter) (const struct breakpoint *))
c4093a6a 6360{
52f0bd74 6361 struct breakpoint *b;
a6d9a66e 6362 struct bp_location *last_loc = NULL;
7f3b0473 6363 int nr_printable_breakpoints;
3b31d625 6364 struct cleanup *bkpttbl_chain;
79a45b7d 6365 struct value_print_options opts;
a6d9a66e 6366 int print_address_bits = 0;
269b11a2 6367 int print_type_col_width = 14;
79a45e25 6368 struct ui_out *uiout = current_uiout;
269b11a2 6369
79a45b7d
TT
6370 get_user_print_options (&opts);
6371
4a64f543
MS
6372 /* Compute the number of rows in the table, as well as the size
6373 required for address fields. */
7f3b0473
AC
6374 nr_printable_breakpoints = 0;
6375 ALL_BREAKPOINTS (b)
e5a67952
MS
6376 {
6377 /* If we have a filter, only list the breakpoints it accepts. */
6378 if (filter && !filter (b))
6379 continue;
6380
6381 /* If we have an "args" string, it is a list of breakpoints to
6382 accept. Skip the others. */
6383 if (args != NULL && *args != '\0')
6384 {
6385 if (allflag && parse_and_eval_long (args) != b->number)
6386 continue;
6387 if (!allflag && !number_is_in_list (args, b->number))
6388 continue;
6389 }
269b11a2 6390
e5a67952
MS
6391 if (allflag || user_breakpoint_p (b))
6392 {
6393 int addr_bit, type_len;
a6d9a66e 6394
e5a67952
MS
6395 addr_bit = breakpoint_address_bits (b);
6396 if (addr_bit > print_address_bits)
6397 print_address_bits = addr_bit;
269b11a2 6398
e5a67952
MS
6399 type_len = strlen (bptype_string (b->type));
6400 if (type_len > print_type_col_width)
6401 print_type_col_width = type_len;
6402
6403 nr_printable_breakpoints++;
6404 }
6405 }
7f3b0473 6406
79a45b7d 6407 if (opts.addressprint)
3b31d625 6408 bkpttbl_chain
3e43a32a
MS
6409 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6410 nr_printable_breakpoints,
3b31d625 6411 "BreakpointTable");
8b93c638 6412 else
3b31d625 6413 bkpttbl_chain
3e43a32a
MS
6414 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6415 nr_printable_breakpoints,
3b31d625 6416 "BreakpointTable");
8b93c638 6417
7f3b0473 6418 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6419 annotate_breakpoints_headers ();
6420 if (nr_printable_breakpoints > 0)
6421 annotate_field (0);
4a64f543 6422 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6423 if (nr_printable_breakpoints > 0)
6424 annotate_field (1);
269b11a2 6425 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6426 "type", "Type"); /* 2 */
d7faa9e7
AC
6427 if (nr_printable_breakpoints > 0)
6428 annotate_field (2);
4a64f543 6429 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6430 if (nr_printable_breakpoints > 0)
6431 annotate_field (3);
54e52265 6432 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6433 if (opts.addressprint)
e5a67952
MS
6434 {
6435 if (nr_printable_breakpoints > 0)
6436 annotate_field (4);
6437 if (print_address_bits <= 32)
6438 ui_out_table_header (uiout, 10, ui_left,
6439 "addr", "Address"); /* 5 */
6440 else
6441 ui_out_table_header (uiout, 18, ui_left,
6442 "addr", "Address"); /* 5 */
6443 }
d7faa9e7
AC
6444 if (nr_printable_breakpoints > 0)
6445 annotate_field (5);
6446 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6447 ui_out_table_body (uiout);
6448 if (nr_printable_breakpoints > 0)
6449 annotate_breakpoints_table ();
7f3b0473 6450
c4093a6a 6451 ALL_BREAKPOINTS (b)
e5a67952
MS
6452 {
6453 QUIT;
6454 /* If we have a filter, only list the breakpoints it accepts. */
6455 if (filter && !filter (b))
6456 continue;
6457
6458 /* If we have an "args" string, it is a list of breakpoints to
6459 accept. Skip the others. */
6460
6461 if (args != NULL && *args != '\0')
6462 {
6463 if (allflag) /* maintenance info breakpoint */
6464 {
6465 if (parse_and_eval_long (args) != b->number)
6466 continue;
6467 }
6468 else /* all others */
6469 {
6470 if (!number_is_in_list (args, b->number))
6471 continue;
6472 }
6473 }
6474 /* We only print out user settable breakpoints unless the
6475 allflag is set. */
6476 if (allflag || user_breakpoint_p (b))
12c5a436 6477 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6478 }
6479
3b31d625 6480 do_cleanups (bkpttbl_chain);
698384cd 6481
7f3b0473 6482 if (nr_printable_breakpoints == 0)
c906108c 6483 {
4a64f543
MS
6484 /* If there's a filter, let the caller decide how to report
6485 empty list. */
d77f58be
SS
6486 if (!filter)
6487 {
e5a67952 6488 if (args == NULL || *args == '\0')
d77f58be
SS
6489 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6490 else
4a64f543 6491 ui_out_message (uiout, 0,
e5a67952
MS
6492 "No breakpoint or watchpoint matching '%s'.\n",
6493 args);
d77f58be 6494 }
c906108c
SS
6495 }
6496 else
c4093a6a 6497 {
a6d9a66e
UW
6498 if (last_loc && !server_command)
6499 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6500 }
c906108c 6501
4a64f543 6502 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6503 there have been breakpoints? */
c906108c 6504 annotate_breakpoints_table_end ();
d77f58be
SS
6505
6506 return nr_printable_breakpoints;
c906108c
SS
6507}
6508
ad443146
SS
6509/* Display the value of default-collect in a way that is generally
6510 compatible with the breakpoint list. */
6511
6512static void
6513default_collect_info (void)
6514{
79a45e25
PA
6515 struct ui_out *uiout = current_uiout;
6516
ad443146
SS
6517 /* If it has no value (which is frequently the case), say nothing; a
6518 message like "No default-collect." gets in user's face when it's
6519 not wanted. */
6520 if (!*default_collect)
6521 return;
6522
6523 /* The following phrase lines up nicely with per-tracepoint collect
6524 actions. */
6525 ui_out_text (uiout, "default collect ");
6526 ui_out_field_string (uiout, "default-collect", default_collect);
6527 ui_out_text (uiout, " \n");
6528}
6529
c906108c 6530static void
e5a67952 6531breakpoints_info (char *args, int from_tty)
c906108c 6532{
e5a67952 6533 breakpoint_1 (args, 0, NULL);
ad443146
SS
6534
6535 default_collect_info ();
d77f58be
SS
6536}
6537
6538static void
e5a67952 6539watchpoints_info (char *args, int from_tty)
d77f58be 6540{
e5a67952 6541 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6542 struct ui_out *uiout = current_uiout;
d77f58be
SS
6543
6544 if (num_printed == 0)
6545 {
e5a67952 6546 if (args == NULL || *args == '\0')
d77f58be
SS
6547 ui_out_message (uiout, 0, "No watchpoints.\n");
6548 else
e5a67952 6549 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6550 }
c906108c
SS
6551}
6552
7a292a7a 6553static void
e5a67952 6554maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6555{
e5a67952 6556 breakpoint_1 (args, 1, NULL);
ad443146
SS
6557
6558 default_collect_info ();
c906108c
SS
6559}
6560
0d381245 6561static int
714835d5 6562breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6563 struct program_space *pspace,
714835d5 6564 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6565{
6566 struct bp_location *bl = b->loc;
cc59ec59 6567
0d381245
VP
6568 for (; bl; bl = bl->next)
6569 {
6c95b8df
PA
6570 if (bl->pspace == pspace
6571 && bl->address == pc
0d381245
VP
6572 && (!overlay_debugging || bl->section == section))
6573 return 1;
6574 }
6575 return 0;
6576}
6577
672f9b60 6578/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6579 concerns with logical breakpoints, so we match program spaces, not
6580 address spaces. */
c906108c
SS
6581
6582static void
6c95b8df
PA
6583describe_other_breakpoints (struct gdbarch *gdbarch,
6584 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6585 struct obj_section *section, int thread)
c906108c 6586{
52f0bd74
AC
6587 int others = 0;
6588 struct breakpoint *b;
c906108c
SS
6589
6590 ALL_BREAKPOINTS (b)
672f9b60
KP
6591 others += (user_breakpoint_p (b)
6592 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6593 if (others > 0)
6594 {
a3f17187
AC
6595 if (others == 1)
6596 printf_filtered (_("Note: breakpoint "));
6597 else /* if (others == ???) */
6598 printf_filtered (_("Note: breakpoints "));
c906108c 6599 ALL_BREAKPOINTS (b)
672f9b60 6600 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6601 {
6602 others--;
6603 printf_filtered ("%d", b->number);
6604 if (b->thread == -1 && thread != -1)
6605 printf_filtered (" (all threads)");
6606 else if (b->thread != -1)
6607 printf_filtered (" (thread %d)", b->thread);
6608 printf_filtered ("%s%s ",
059fb39f 6609 ((b->enable_state == bp_disabled
f8eba3c6 6610 || b->enable_state == bp_call_disabled)
0d381245
VP
6611 ? " (disabled)"
6612 : b->enable_state == bp_permanent
6613 ? " (permanent)"
6614 : ""),
6615 (others > 1) ? ","
6616 : ((others == 1) ? " and" : ""));
6617 }
a3f17187 6618 printf_filtered (_("also set at pc "));
5af949e3 6619 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6620 printf_filtered (".\n");
6621 }
6622}
6623\f
c906108c 6624
e4f237da
KB
6625/* Return true iff it is meaningful to use the address member of
6626 BPT. For some breakpoint types, the address member is irrelevant
6627 and it makes no sense to attempt to compare it to other addresses
6628 (or use it for any other purpose either).
6629
4a64f543
MS
6630 More specifically, each of the following breakpoint types will
6631 always have a zero valued address and we don't want to mark
6632 breakpoints of any of these types to be a duplicate of an actual
6633 breakpoint at address zero:
e4f237da
KB
6634
6635 bp_watchpoint
2d134ed3
PA
6636 bp_catchpoint
6637
6638*/
e4f237da
KB
6639
6640static int
6641breakpoint_address_is_meaningful (struct breakpoint *bpt)
6642{
6643 enum bptype type = bpt->type;
6644
2d134ed3
PA
6645 return (type != bp_watchpoint && type != bp_catchpoint);
6646}
6647
6648/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6649 true if LOC1 and LOC2 represent the same watchpoint location. */
6650
6651static int
4a64f543
MS
6652watchpoint_locations_match (struct bp_location *loc1,
6653 struct bp_location *loc2)
2d134ed3 6654{
3a5c3e22
PA
6655 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6656 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6657
6658 /* Both of them must exist. */
6659 gdb_assert (w1 != NULL);
6660 gdb_assert (w2 != NULL);
2bdf28a0 6661
4a64f543
MS
6662 /* If the target can evaluate the condition expression in hardware,
6663 then we we need to insert both watchpoints even if they are at
6664 the same place. Otherwise the watchpoint will only trigger when
6665 the condition of whichever watchpoint was inserted evaluates to
6666 true, not giving a chance for GDB to check the condition of the
6667 other watchpoint. */
3a5c3e22 6668 if ((w1->cond_exp
4a64f543
MS
6669 && target_can_accel_watchpoint_condition (loc1->address,
6670 loc1->length,
0cf6dd15 6671 loc1->watchpoint_type,
3a5c3e22
PA
6672 w1->cond_exp))
6673 || (w2->cond_exp
4a64f543
MS
6674 && target_can_accel_watchpoint_condition (loc2->address,
6675 loc2->length,
0cf6dd15 6676 loc2->watchpoint_type,
3a5c3e22 6677 w2->cond_exp)))
0cf6dd15
TJB
6678 return 0;
6679
85d721b8
PA
6680 /* Note that this checks the owner's type, not the location's. In
6681 case the target does not support read watchpoints, but does
6682 support access watchpoints, we'll have bp_read_watchpoint
6683 watchpoints with hw_access locations. Those should be considered
6684 duplicates of hw_read locations. The hw_read locations will
6685 become hw_access locations later. */
2d134ed3
PA
6686 return (loc1->owner->type == loc2->owner->type
6687 && loc1->pspace->aspace == loc2->pspace->aspace
6688 && loc1->address == loc2->address
6689 && loc1->length == loc2->length);
e4f237da
KB
6690}
6691
6c95b8df
PA
6692/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6693 same breakpoint location. In most targets, this can only be true
6694 if ASPACE1 matches ASPACE2. On targets that have global
6695 breakpoints, the address space doesn't really matter. */
6696
6697static int
6698breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6699 struct address_space *aspace2, CORE_ADDR addr2)
6700{
f5656ead 6701 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6702 || aspace1 == aspace2)
6703 && addr1 == addr2);
6704}
6705
f1310107
TJB
6706/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6707 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6708 matches ASPACE2. On targets that have global breakpoints, the address
6709 space doesn't really matter. */
6710
6711static int
6712breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6713 int len1, struct address_space *aspace2,
6714 CORE_ADDR addr2)
6715{
f5656ead 6716 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6717 || aspace1 == aspace2)
6718 && addr2 >= addr1 && addr2 < addr1 + len1);
6719}
6720
6721/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6722 a ranged breakpoint. In most targets, a match happens only if ASPACE
6723 matches the breakpoint's address space. On targets that have global
6724 breakpoints, the address space doesn't really matter. */
6725
6726static int
6727breakpoint_location_address_match (struct bp_location *bl,
6728 struct address_space *aspace,
6729 CORE_ADDR addr)
6730{
6731 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6732 aspace, addr)
6733 || (bl->length
6734 && breakpoint_address_match_range (bl->pspace->aspace,
6735 bl->address, bl->length,
6736 aspace, addr)));
6737}
6738
1e4d1764
YQ
6739/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6740 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6741 true, otherwise returns false. */
6742
6743static int
6744tracepoint_locations_match (struct bp_location *loc1,
6745 struct bp_location *loc2)
6746{
6747 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6748 /* Since tracepoint locations are never duplicated with others', tracepoint
6749 locations at the same address of different tracepoints are regarded as
6750 different locations. */
6751 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6752 else
6753 return 0;
6754}
6755
2d134ed3
PA
6756/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6757 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6758 represent the same location. */
6759
6760static int
4a64f543
MS
6761breakpoint_locations_match (struct bp_location *loc1,
6762 struct bp_location *loc2)
2d134ed3 6763{
2bdf28a0
JK
6764 int hw_point1, hw_point2;
6765
6766 /* Both of them must not be in moribund_locations. */
6767 gdb_assert (loc1->owner != NULL);
6768 gdb_assert (loc2->owner != NULL);
6769
6770 hw_point1 = is_hardware_watchpoint (loc1->owner);
6771 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6772
6773 if (hw_point1 != hw_point2)
6774 return 0;
6775 else if (hw_point1)
6776 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6777 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6778 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6779 else
f1310107
TJB
6780 /* We compare bp_location.length in order to cover ranged breakpoints. */
6781 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6782 loc2->pspace->aspace, loc2->address)
6783 && loc1->length == loc2->length);
2d134ed3
PA
6784}
6785
76897487
KB
6786static void
6787breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6788 int bnum, int have_bnum)
6789{
f63fbe86
MS
6790 /* The longest string possibly returned by hex_string_custom
6791 is 50 chars. These must be at least that big for safety. */
6792 char astr1[64];
6793 char astr2[64];
76897487 6794
bb599908
PH
6795 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6796 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6797 if (have_bnum)
8a3fe4f8 6798 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6799 bnum, astr1, astr2);
6800 else
8a3fe4f8 6801 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6802}
6803
4a64f543
MS
6804/* Adjust a breakpoint's address to account for architectural
6805 constraints on breakpoint placement. Return the adjusted address.
6806 Note: Very few targets require this kind of adjustment. For most
6807 targets, this function is simply the identity function. */
76897487
KB
6808
6809static CORE_ADDR
a6d9a66e
UW
6810adjust_breakpoint_address (struct gdbarch *gdbarch,
6811 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6812{
a6d9a66e 6813 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6814 {
6815 /* Very few targets need any kind of breakpoint adjustment. */
6816 return bpaddr;
6817 }
88f7da05
KB
6818 else if (bptype == bp_watchpoint
6819 || bptype == bp_hardware_watchpoint
6820 || bptype == bp_read_watchpoint
6821 || bptype == bp_access_watchpoint
fe798b75 6822 || bptype == bp_catchpoint)
88f7da05
KB
6823 {
6824 /* Watchpoints and the various bp_catch_* eventpoints should not
6825 have their addresses modified. */
6826 return bpaddr;
6827 }
76897487
KB
6828 else
6829 {
6830 CORE_ADDR adjusted_bpaddr;
6831
6832 /* Some targets have architectural constraints on the placement
6833 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6834 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6835
6836 /* An adjusted breakpoint address can significantly alter
6837 a user's expectations. Print a warning if an adjustment
6838 is required. */
6839 if (adjusted_bpaddr != bpaddr)
6840 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6841
6842 return adjusted_bpaddr;
6843 }
6844}
6845
28010a5d
PA
6846void
6847init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6848 struct breakpoint *owner)
7cc221ef 6849{
7cc221ef
DJ
6850 memset (loc, 0, sizeof (*loc));
6851
348d480f
PA
6852 gdb_assert (ops != NULL);
6853
28010a5d
PA
6854 loc->ops = ops;
6855 loc->owner = owner;
511a6cd4 6856 loc->cond = NULL;
b775012e 6857 loc->cond_bytecode = NULL;
0d381245
VP
6858 loc->shlib_disabled = 0;
6859 loc->enabled = 1;
e049a4b5 6860
28010a5d 6861 switch (owner->type)
e049a4b5
DJ
6862 {
6863 case bp_breakpoint:
6864 case bp_until:
6865 case bp_finish:
6866 case bp_longjmp:
6867 case bp_longjmp_resume:
e2e4d78b 6868 case bp_longjmp_call_dummy:
186c406b
TT
6869 case bp_exception:
6870 case bp_exception_resume:
e049a4b5 6871 case bp_step_resume:
2c03e5be 6872 case bp_hp_step_resume:
e049a4b5
DJ
6873 case bp_watchpoint_scope:
6874 case bp_call_dummy:
aa7d318d 6875 case bp_std_terminate:
e049a4b5
DJ
6876 case bp_shlib_event:
6877 case bp_thread_event:
6878 case bp_overlay_event:
4efc6507 6879 case bp_jit_event:
0fd8e87f 6880 case bp_longjmp_master:
aa7d318d 6881 case bp_std_terminate_master:
186c406b 6882 case bp_exception_master:
0e30163f
JK
6883 case bp_gnu_ifunc_resolver:
6884 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6885 case bp_dprintf:
e049a4b5 6886 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6887 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6888 break;
6889 case bp_hardware_breakpoint:
6890 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6891 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6892 break;
6893 case bp_hardware_watchpoint:
6894 case bp_read_watchpoint:
6895 case bp_access_watchpoint:
6896 loc->loc_type = bp_loc_hardware_watchpoint;
6897 break;
6898 case bp_watchpoint:
ce78b96d 6899 case bp_catchpoint:
15c3d785
PA
6900 case bp_tracepoint:
6901 case bp_fast_tracepoint:
0fb4aa4b 6902 case bp_static_tracepoint:
e049a4b5
DJ
6903 loc->loc_type = bp_loc_other;
6904 break;
6905 default:
e2e0b3e5 6906 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6907 }
6908
f431efe5 6909 loc->refc = 1;
28010a5d
PA
6910}
6911
6912/* Allocate a struct bp_location. */
6913
6914static struct bp_location *
6915allocate_bp_location (struct breakpoint *bpt)
6916{
348d480f
PA
6917 return bpt->ops->allocate_location (bpt);
6918}
7cc221ef 6919
f431efe5
PA
6920static void
6921free_bp_location (struct bp_location *loc)
fe3f5fa8 6922{
348d480f 6923 loc->ops->dtor (loc);
fe3f5fa8
VP
6924 xfree (loc);
6925}
6926
f431efe5
PA
6927/* Increment reference count. */
6928
6929static void
6930incref_bp_location (struct bp_location *bl)
6931{
6932 ++bl->refc;
6933}
6934
6935/* Decrement reference count. If the reference count reaches 0,
6936 destroy the bp_location. Sets *BLP to NULL. */
6937
6938static void
6939decref_bp_location (struct bp_location **blp)
6940{
0807b50c
PA
6941 gdb_assert ((*blp)->refc > 0);
6942
f431efe5
PA
6943 if (--(*blp)->refc == 0)
6944 free_bp_location (*blp);
6945 *blp = NULL;
6946}
6947
346774a9 6948/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6949
346774a9
PA
6950static void
6951add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6952{
346774a9 6953 struct breakpoint *b1;
c906108c 6954
346774a9
PA
6955 /* Add this breakpoint to the end of the chain so that a list of
6956 breakpoints will come out in order of increasing numbers. */
6957
6958 b1 = breakpoint_chain;
6959 if (b1 == 0)
6960 breakpoint_chain = b;
6961 else
6962 {
6963 while (b1->next)
6964 b1 = b1->next;
6965 b1->next = b;
6966 }
6967}
6968
6969/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6970
6971static void
6972init_raw_breakpoint_without_location (struct breakpoint *b,
6973 struct gdbarch *gdbarch,
28010a5d 6974 enum bptype bptype,
c0a91b2b 6975 const struct breakpoint_ops *ops)
346774a9 6976{
c906108c 6977 memset (b, 0, sizeof (*b));
2219d63c 6978
348d480f
PA
6979 gdb_assert (ops != NULL);
6980
28010a5d 6981 b->ops = ops;
4d28f7a8 6982 b->type = bptype;
a6d9a66e 6983 b->gdbarch = gdbarch;
c906108c
SS
6984 b->language = current_language->la_language;
6985 b->input_radix = input_radix;
6986 b->thread = -1;
b5de0fa7 6987 b->enable_state = bp_enabled;
c906108c
SS
6988 b->next = 0;
6989 b->silent = 0;
6990 b->ignore_count = 0;
6991 b->commands = NULL;
818dd999 6992 b->frame_id = null_frame_id;
0d381245 6993 b->condition_not_parsed = 0;
84f4c1fe 6994 b->py_bp_object = NULL;
d0fb5eae 6995 b->related_breakpoint = b;
346774a9
PA
6996}
6997
6998/* Helper to set_raw_breakpoint below. Creates a breakpoint
6999 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7000
7001static struct breakpoint *
7002set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7003 enum bptype bptype,
c0a91b2b 7004 const struct breakpoint_ops *ops)
346774a9
PA
7005{
7006 struct breakpoint *b = XNEW (struct breakpoint);
7007
348d480f 7008 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7009 add_to_breakpoint_chain (b);
0d381245
VP
7010 return b;
7011}
7012
0e30163f
JK
7013/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7014 resolutions should be made as the user specified the location explicitly
7015 enough. */
7016
0d381245 7017static void
0e30163f 7018set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7019{
2bdf28a0
JK
7020 gdb_assert (loc->owner != NULL);
7021
0d381245 7022 if (loc->owner->type == bp_breakpoint
1042e4c0 7023 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7024 || is_tracepoint (loc->owner))
0d381245 7025 {
0e30163f 7026 int is_gnu_ifunc;
2c02bd72 7027 const char *function_name;
6a3a010b 7028 CORE_ADDR func_addr;
0e30163f 7029
2c02bd72 7030 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7031 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7032
7033 if (is_gnu_ifunc && !explicit_loc)
7034 {
7035 struct breakpoint *b = loc->owner;
7036
7037 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7038 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7039 &loc->requested_address))
7040 {
7041 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7042 loc->address = adjust_breakpoint_address (loc->gdbarch,
7043 loc->requested_address,
7044 b->type);
7045 }
7046 else if (b->type == bp_breakpoint && b->loc == loc
7047 && loc->next == NULL && b->related_breakpoint == b)
7048 {
7049 /* Create only the whole new breakpoint of this type but do not
7050 mess more complicated breakpoints with multiple locations. */
7051 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7052 /* Remember the resolver's address for use by the return
7053 breakpoint. */
7054 loc->related_address = func_addr;
0e30163f
JK
7055 }
7056 }
7057
2c02bd72
DE
7058 if (function_name)
7059 loc->function_name = xstrdup (function_name);
0d381245
VP
7060 }
7061}
7062
a6d9a66e 7063/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7064struct gdbarch *
a6d9a66e
UW
7065get_sal_arch (struct symtab_and_line sal)
7066{
7067 if (sal.section)
7068 return get_objfile_arch (sal.section->objfile);
7069 if (sal.symtab)
7070 return get_objfile_arch (sal.symtab->objfile);
7071
7072 return NULL;
7073}
7074
346774a9
PA
7075/* Low level routine for partially initializing a breakpoint of type
7076 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7077 file name, and line number are provided by SAL.
0d381245
VP
7078
7079 It is expected that the caller will complete the initialization of
7080 the newly created breakpoint struct as well as output any status
c56053d2 7081 information regarding the creation of a new breakpoint. */
0d381245 7082
346774a9
PA
7083static void
7084init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7085 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7086 const struct breakpoint_ops *ops)
0d381245 7087{
28010a5d 7088 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7089
3742cc8b 7090 add_location_to_breakpoint (b, &sal);
0d381245 7091
6c95b8df
PA
7092 if (bptype != bp_catchpoint)
7093 gdb_assert (sal.pspace != NULL);
7094
f8eba3c6
TT
7095 /* Store the program space that was used to set the breakpoint,
7096 except for ordinary breakpoints, which are independent of the
7097 program space. */
7098 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7099 b->pspace = sal.pspace;
346774a9 7100}
c906108c 7101
346774a9
PA
7102/* set_raw_breakpoint is a low level routine for allocating and
7103 partially initializing a breakpoint of type BPTYPE. The newly
7104 created breakpoint's address, section, source file name, and line
7105 number are provided by SAL. The newly created and partially
7106 initialized breakpoint is added to the breakpoint chain and
7107 is also returned as the value of this function.
7108
7109 It is expected that the caller will complete the initialization of
7110 the newly created breakpoint struct as well as output any status
7111 information regarding the creation of a new breakpoint. In
7112 particular, set_raw_breakpoint does NOT set the breakpoint
7113 number! Care should be taken to not allow an error to occur
7114 prior to completing the initialization of the breakpoint. If this
7115 should happen, a bogus breakpoint will be left on the chain. */
7116
7117struct breakpoint *
7118set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7119 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7120 const struct breakpoint_ops *ops)
346774a9
PA
7121{
7122 struct breakpoint *b = XNEW (struct breakpoint);
7123
348d480f 7124 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7125 add_to_breakpoint_chain (b);
c906108c
SS
7126 return b;
7127}
7128
c2c6d25f
JM
7129
7130/* Note that the breakpoint object B describes a permanent breakpoint
7131 instruction, hard-wired into the inferior's code. */
7132void
7133make_breakpoint_permanent (struct breakpoint *b)
7134{
0d381245 7135 struct bp_location *bl;
cc59ec59 7136
b5de0fa7 7137 b->enable_state = bp_permanent;
c2c6d25f 7138
4a64f543
MS
7139 /* By definition, permanent breakpoints are already present in the
7140 code. Mark all locations as inserted. For now,
7141 make_breakpoint_permanent is called in just one place, so it's
7142 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7143 multiple locations or not, but it's easy to implement. */
0d381245
VP
7144 for (bl = b->loc; bl; bl = bl->next)
7145 bl->inserted = 1;
c2c6d25f
JM
7146}
7147
53a5351d 7148/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7149 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7150 initiated the operation. */
c906108c
SS
7151
7152void
186c406b 7153set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7154{
35df4500 7155 struct breakpoint *b, *b_tmp;
186c406b 7156 int thread = tp->num;
0fd8e87f
UW
7157
7158 /* To avoid having to rescan all objfile symbols at every step,
7159 we maintain a list of continually-inserted but always disabled
7160 longjmp "master" breakpoints. Here, we simply create momentary
7161 clones of those and enable them for the requested thread. */
35df4500 7162 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7163 if (b->pspace == current_program_space
186c406b
TT
7164 && (b->type == bp_longjmp_master
7165 || b->type == bp_exception_master))
0fd8e87f 7166 {
06edf0c0
PA
7167 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7168 struct breakpoint *clone;
cc59ec59 7169
e2e4d78b
JK
7170 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7171 after their removal. */
06edf0c0 7172 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7173 &longjmp_breakpoint_ops);
0fd8e87f
UW
7174 clone->thread = thread;
7175 }
186c406b
TT
7176
7177 tp->initiating_frame = frame;
c906108c
SS
7178}
7179
611c83ae 7180/* Delete all longjmp breakpoints from THREAD. */
c906108c 7181void
611c83ae 7182delete_longjmp_breakpoint (int thread)
c906108c 7183{
35df4500 7184 struct breakpoint *b, *b_tmp;
c906108c 7185
35df4500 7186 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7187 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7188 {
7189 if (b->thread == thread)
7190 delete_breakpoint (b);
7191 }
c906108c
SS
7192}
7193
f59f708a
PA
7194void
7195delete_longjmp_breakpoint_at_next_stop (int thread)
7196{
7197 struct breakpoint *b, *b_tmp;
7198
7199 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7200 if (b->type == bp_longjmp || b->type == bp_exception)
7201 {
7202 if (b->thread == thread)
7203 b->disposition = disp_del_at_next_stop;
7204 }
7205}
7206
e2e4d78b
JK
7207/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7208 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7209 pointer to any of them. Return NULL if this system cannot place longjmp
7210 breakpoints. */
7211
7212struct breakpoint *
7213set_longjmp_breakpoint_for_call_dummy (void)
7214{
7215 struct breakpoint *b, *retval = NULL;
7216
7217 ALL_BREAKPOINTS (b)
7218 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7219 {
7220 struct breakpoint *new_b;
7221
7222 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7223 &momentary_breakpoint_ops);
7224 new_b->thread = pid_to_thread_id (inferior_ptid);
7225
7226 /* Link NEW_B into the chain of RETVAL breakpoints. */
7227
7228 gdb_assert (new_b->related_breakpoint == new_b);
7229 if (retval == NULL)
7230 retval = new_b;
7231 new_b->related_breakpoint = retval;
7232 while (retval->related_breakpoint != new_b->related_breakpoint)
7233 retval = retval->related_breakpoint;
7234 retval->related_breakpoint = new_b;
7235 }
7236
7237 return retval;
7238}
7239
7240/* Verify all existing dummy frames and their associated breakpoints for
7241 THREAD. Remove those which can no longer be found in the current frame
7242 stack.
7243
7244 You should call this function only at places where it is safe to currently
7245 unwind the whole stack. Failed stack unwind would discard live dummy
7246 frames. */
7247
7248void
7249check_longjmp_breakpoint_for_call_dummy (int thread)
7250{
7251 struct breakpoint *b, *b_tmp;
7252
7253 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7254 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7255 {
7256 struct breakpoint *dummy_b = b->related_breakpoint;
7257
7258 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7259 dummy_b = dummy_b->related_breakpoint;
7260 if (dummy_b->type != bp_call_dummy
7261 || frame_find_by_id (dummy_b->frame_id) != NULL)
7262 continue;
7263
7264 dummy_frame_discard (dummy_b->frame_id);
7265
7266 while (b->related_breakpoint != b)
7267 {
7268 if (b_tmp == b->related_breakpoint)
7269 b_tmp = b->related_breakpoint->next;
7270 delete_breakpoint (b->related_breakpoint);
7271 }
7272 delete_breakpoint (b);
7273 }
7274}
7275
1900040c
MS
7276void
7277enable_overlay_breakpoints (void)
7278{
52f0bd74 7279 struct breakpoint *b;
1900040c
MS
7280
7281 ALL_BREAKPOINTS (b)
7282 if (b->type == bp_overlay_event)
7283 {
7284 b->enable_state = bp_enabled;
b60e7edf 7285 update_global_location_list (1);
c02f5703 7286 overlay_events_enabled = 1;
1900040c
MS
7287 }
7288}
7289
7290void
7291disable_overlay_breakpoints (void)
7292{
52f0bd74 7293 struct breakpoint *b;
1900040c
MS
7294
7295 ALL_BREAKPOINTS (b)
7296 if (b->type == bp_overlay_event)
7297 {
7298 b->enable_state = bp_disabled;
b60e7edf 7299 update_global_location_list (0);
c02f5703 7300 overlay_events_enabled = 0;
1900040c
MS
7301 }
7302}
7303
aa7d318d
TT
7304/* Set an active std::terminate breakpoint for each std::terminate
7305 master breakpoint. */
7306void
7307set_std_terminate_breakpoint (void)
7308{
35df4500 7309 struct breakpoint *b, *b_tmp;
aa7d318d 7310
35df4500 7311 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7312 if (b->pspace == current_program_space
7313 && b->type == bp_std_terminate_master)
7314 {
06edf0c0
PA
7315 momentary_breakpoint_from_master (b, bp_std_terminate,
7316 &momentary_breakpoint_ops);
aa7d318d
TT
7317 }
7318}
7319
7320/* Delete all the std::terminate breakpoints. */
7321void
7322delete_std_terminate_breakpoint (void)
7323{
35df4500 7324 struct breakpoint *b, *b_tmp;
aa7d318d 7325
35df4500 7326 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7327 if (b->type == bp_std_terminate)
7328 delete_breakpoint (b);
7329}
7330
c4093a6a 7331struct breakpoint *
a6d9a66e 7332create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7333{
7334 struct breakpoint *b;
c4093a6a 7335
06edf0c0
PA
7336 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7337 &internal_breakpoint_ops);
7338
b5de0fa7 7339 b->enable_state = bp_enabled;
c4093a6a 7340 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7341 b->addr_string
7342 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7343
b60e7edf 7344 update_global_location_list_nothrow (1);
74960c60 7345
c4093a6a
JM
7346 return b;
7347}
7348
7349void
7350remove_thread_event_breakpoints (void)
7351{
35df4500 7352 struct breakpoint *b, *b_tmp;
c4093a6a 7353
35df4500 7354 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7355 if (b->type == bp_thread_event
7356 && b->loc->pspace == current_program_space)
c4093a6a
JM
7357 delete_breakpoint (b);
7358}
7359
0101ce28
JJ
7360struct lang_and_radix
7361 {
7362 enum language lang;
7363 int radix;
7364 };
7365
4efc6507
DE
7366/* Create a breakpoint for JIT code registration and unregistration. */
7367
7368struct breakpoint *
7369create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7370{
7371 struct breakpoint *b;
7372
06edf0c0
PA
7373 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7374 &internal_breakpoint_ops);
4efc6507
DE
7375 update_global_location_list_nothrow (1);
7376 return b;
7377}
0101ce28 7378
03673fc7
PP
7379/* Remove JIT code registration and unregistration breakpoint(s). */
7380
7381void
7382remove_jit_event_breakpoints (void)
7383{
7384 struct breakpoint *b, *b_tmp;
7385
7386 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7387 if (b->type == bp_jit_event
7388 && b->loc->pspace == current_program_space)
7389 delete_breakpoint (b);
7390}
7391
cae688ec
JJ
7392void
7393remove_solib_event_breakpoints (void)
7394{
35df4500 7395 struct breakpoint *b, *b_tmp;
cae688ec 7396
35df4500 7397 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7398 if (b->type == bp_shlib_event
7399 && b->loc->pspace == current_program_space)
cae688ec
JJ
7400 delete_breakpoint (b);
7401}
7402
7403struct breakpoint *
a6d9a66e 7404create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7405{
7406 struct breakpoint *b;
7407
06edf0c0
PA
7408 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7409 &internal_breakpoint_ops);
b60e7edf 7410 update_global_location_list_nothrow (1);
cae688ec
JJ
7411 return b;
7412}
7413
7414/* Disable any breakpoints that are on code in shared libraries. Only
7415 apply to enabled breakpoints, disabled ones can just stay disabled. */
7416
7417void
cb851954 7418disable_breakpoints_in_shlibs (void)
cae688ec 7419{
876fa593 7420 struct bp_location *loc, **locp_tmp;
cae688ec 7421
876fa593 7422 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7423 {
2bdf28a0 7424 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7425 struct breakpoint *b = loc->owner;
2bdf28a0 7426
4a64f543
MS
7427 /* We apply the check to all breakpoints, including disabled for
7428 those with loc->duplicate set. This is so that when breakpoint
7429 becomes enabled, or the duplicate is removed, gdb will try to
7430 insert all breakpoints. If we don't set shlib_disabled here,
7431 we'll try to insert those breakpoints and fail. */
1042e4c0 7432 if (((b->type == bp_breakpoint)
508ccb1f 7433 || (b->type == bp_jit_event)
1042e4c0 7434 || (b->type == bp_hardware_breakpoint)
d77f58be 7435 || (is_tracepoint (b)))
6c95b8df 7436 && loc->pspace == current_program_space
0d381245 7437 && !loc->shlib_disabled
6c95b8df 7438 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7439 )
0d381245
VP
7440 {
7441 loc->shlib_disabled = 1;
7442 }
cae688ec
JJ
7443 }
7444}
7445
1e4d1764
YQ
7446/* Disable any breakpoints and tracepoints that are in an unloaded shared
7447 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 7448 disabled. */
84acb35a 7449
75149521 7450static void
84acb35a
JJ
7451disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7452{
876fa593 7453 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7454 int disabled_shlib_breaks = 0;
7455
c86cf029
VP
7456 /* SunOS a.out shared libraries are always mapped, so do not
7457 disable breakpoints; they will only be reported as unloaded
7458 through clear_solib when GDB discards its shared library
7459 list. See clear_solib for more information. */
7460 if (exec_bfd != NULL
7461 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7462 return;
7463
876fa593 7464 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7465 {
2bdf28a0 7466 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7467 struct breakpoint *b = loc->owner;
cc59ec59 7468
1e4d1764 7469 if (solib->pspace == loc->pspace
e2dd7057 7470 && !loc->shlib_disabled
1e4d1764
YQ
7471 && (((b->type == bp_breakpoint
7472 || b->type == bp_jit_event
7473 || b->type == bp_hardware_breakpoint)
7474 && (loc->loc_type == bp_loc_hardware_breakpoint
7475 || loc->loc_type == bp_loc_software_breakpoint))
7476 || is_tracepoint (b))
e2dd7057 7477 && solib_contains_address_p (solib, loc->address))
84acb35a 7478 {
e2dd7057
PP
7479 loc->shlib_disabled = 1;
7480 /* At this point, we cannot rely on remove_breakpoint
7481 succeeding so we must mark the breakpoint as not inserted
7482 to prevent future errors occurring in remove_breakpoints. */
7483 loc->inserted = 0;
8d3788bd
VP
7484
7485 /* This may cause duplicate notifications for the same breakpoint. */
7486 observer_notify_breakpoint_modified (b);
7487
e2dd7057
PP
7488 if (!disabled_shlib_breaks)
7489 {
7490 target_terminal_ours_for_output ();
3e43a32a
MS
7491 warning (_("Temporarily disabling breakpoints "
7492 "for unloaded shared library \"%s\""),
e2dd7057 7493 solib->so_name);
84acb35a 7494 }
e2dd7057 7495 disabled_shlib_breaks = 1;
84acb35a
JJ
7496 }
7497 }
84acb35a
JJ
7498}
7499
ce78b96d
JB
7500/* FORK & VFORK catchpoints. */
7501
e29a4733
PA
7502/* An instance of this type is used to represent a fork or vfork
7503 catchpoint. It includes a "struct breakpoint" as a kind of base
7504 class; users downcast to "struct breakpoint *" when needed. A
7505 breakpoint is really of this type iff its ops pointer points to
7506 CATCH_FORK_BREAKPOINT_OPS. */
7507
7508struct fork_catchpoint
7509{
7510 /* The base class. */
7511 struct breakpoint base;
7512
7513 /* Process id of a child process whose forking triggered this
7514 catchpoint. This field is only valid immediately after this
7515 catchpoint has triggered. */
7516 ptid_t forked_inferior_pid;
7517};
7518
4a64f543
MS
7519/* Implement the "insert" breakpoint_ops method for fork
7520 catchpoints. */
ce78b96d 7521
77b06cd7
TJB
7522static int
7523insert_catch_fork (struct bp_location *bl)
ce78b96d 7524{
77b06cd7 7525 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7526}
7527
4a64f543
MS
7528/* Implement the "remove" breakpoint_ops method for fork
7529 catchpoints. */
ce78b96d
JB
7530
7531static int
77b06cd7 7532remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7533{
7534 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7535}
7536
7537/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7538 catchpoints. */
7539
7540static int
f1310107 7541breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7542 struct address_space *aspace, CORE_ADDR bp_addr,
7543 const struct target_waitstatus *ws)
ce78b96d 7544{
e29a4733
PA
7545 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7546
f90263c1
TT
7547 if (ws->kind != TARGET_WAITKIND_FORKED)
7548 return 0;
7549
7550 c->forked_inferior_pid = ws->value.related_pid;
7551 return 1;
ce78b96d
JB
7552}
7553
4a64f543
MS
7554/* Implement the "print_it" breakpoint_ops method for fork
7555 catchpoints. */
ce78b96d
JB
7556
7557static enum print_stop_action
348d480f 7558print_it_catch_fork (bpstat bs)
ce78b96d 7559{
36dfb11c 7560 struct ui_out *uiout = current_uiout;
348d480f
PA
7561 struct breakpoint *b = bs->breakpoint_at;
7562 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7563
ce78b96d 7564 annotate_catchpoint (b->number);
36dfb11c
TT
7565 if (b->disposition == disp_del)
7566 ui_out_text (uiout, "\nTemporary catchpoint ");
7567 else
7568 ui_out_text (uiout, "\nCatchpoint ");
7569 if (ui_out_is_mi_like_p (uiout))
7570 {
7571 ui_out_field_string (uiout, "reason",
7572 async_reason_lookup (EXEC_ASYNC_FORK));
7573 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7574 }
7575 ui_out_field_int (uiout, "bkptno", b->number);
7576 ui_out_text (uiout, " (forked process ");
7577 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7578 ui_out_text (uiout, "), ");
ce78b96d
JB
7579 return PRINT_SRC_AND_LOC;
7580}
7581
4a64f543
MS
7582/* Implement the "print_one" breakpoint_ops method for fork
7583 catchpoints. */
ce78b96d
JB
7584
7585static void
a6d9a66e 7586print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7587{
e29a4733 7588 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7589 struct value_print_options opts;
79a45e25 7590 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7591
7592 get_user_print_options (&opts);
7593
4a64f543
MS
7594 /* Field 4, the address, is omitted (which makes the columns not
7595 line up too nicely with the headers, but the effect is relatively
7596 readable). */
79a45b7d 7597 if (opts.addressprint)
ce78b96d
JB
7598 ui_out_field_skip (uiout, "addr");
7599 annotate_field (5);
7600 ui_out_text (uiout, "fork");
e29a4733 7601 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7602 {
7603 ui_out_text (uiout, ", process ");
7604 ui_out_field_int (uiout, "what",
e29a4733 7605 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7606 ui_out_spaces (uiout, 1);
7607 }
8ac3646f
TT
7608
7609 if (ui_out_is_mi_like_p (uiout))
7610 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7611}
7612
7613/* Implement the "print_mention" breakpoint_ops method for fork
7614 catchpoints. */
7615
7616static void
7617print_mention_catch_fork (struct breakpoint *b)
7618{
7619 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7620}
7621
6149aea9
PA
7622/* Implement the "print_recreate" breakpoint_ops method for fork
7623 catchpoints. */
7624
7625static void
7626print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7627{
7628 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7629 print_recreate_thread (b, fp);
6149aea9
PA
7630}
7631
ce78b96d
JB
7632/* The breakpoint_ops structure to be used in fork catchpoints. */
7633
2060206e 7634static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7635
4a64f543
MS
7636/* Implement the "insert" breakpoint_ops method for vfork
7637 catchpoints. */
ce78b96d 7638
77b06cd7
TJB
7639static int
7640insert_catch_vfork (struct bp_location *bl)
ce78b96d 7641{
77b06cd7 7642 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7643}
7644
4a64f543
MS
7645/* Implement the "remove" breakpoint_ops method for vfork
7646 catchpoints. */
ce78b96d
JB
7647
7648static int
77b06cd7 7649remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7650{
7651 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7652}
7653
7654/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7655 catchpoints. */
7656
7657static int
f1310107 7658breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7659 struct address_space *aspace, CORE_ADDR bp_addr,
7660 const struct target_waitstatus *ws)
ce78b96d 7661{
e29a4733
PA
7662 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7663
f90263c1
TT
7664 if (ws->kind != TARGET_WAITKIND_VFORKED)
7665 return 0;
7666
7667 c->forked_inferior_pid = ws->value.related_pid;
7668 return 1;
ce78b96d
JB
7669}
7670
4a64f543
MS
7671/* Implement the "print_it" breakpoint_ops method for vfork
7672 catchpoints. */
ce78b96d
JB
7673
7674static enum print_stop_action
348d480f 7675print_it_catch_vfork (bpstat bs)
ce78b96d 7676{
36dfb11c 7677 struct ui_out *uiout = current_uiout;
348d480f 7678 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7679 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7680
ce78b96d 7681 annotate_catchpoint (b->number);
36dfb11c
TT
7682 if (b->disposition == disp_del)
7683 ui_out_text (uiout, "\nTemporary catchpoint ");
7684 else
7685 ui_out_text (uiout, "\nCatchpoint ");
7686 if (ui_out_is_mi_like_p (uiout))
7687 {
7688 ui_out_field_string (uiout, "reason",
7689 async_reason_lookup (EXEC_ASYNC_VFORK));
7690 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7691 }
7692 ui_out_field_int (uiout, "bkptno", b->number);
7693 ui_out_text (uiout, " (vforked process ");
7694 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7695 ui_out_text (uiout, "), ");
ce78b96d
JB
7696 return PRINT_SRC_AND_LOC;
7697}
7698
4a64f543
MS
7699/* Implement the "print_one" breakpoint_ops method for vfork
7700 catchpoints. */
ce78b96d
JB
7701
7702static void
a6d9a66e 7703print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7704{
e29a4733 7705 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7706 struct value_print_options opts;
79a45e25 7707 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7708
7709 get_user_print_options (&opts);
4a64f543
MS
7710 /* Field 4, the address, is omitted (which makes the columns not
7711 line up too nicely with the headers, but the effect is relatively
7712 readable). */
79a45b7d 7713 if (opts.addressprint)
ce78b96d
JB
7714 ui_out_field_skip (uiout, "addr");
7715 annotate_field (5);
7716 ui_out_text (uiout, "vfork");
e29a4733 7717 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7718 {
7719 ui_out_text (uiout, ", process ");
7720 ui_out_field_int (uiout, "what",
e29a4733 7721 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7722 ui_out_spaces (uiout, 1);
7723 }
8ac3646f
TT
7724
7725 if (ui_out_is_mi_like_p (uiout))
7726 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
7727}
7728
7729/* Implement the "print_mention" breakpoint_ops method for vfork
7730 catchpoints. */
7731
7732static void
7733print_mention_catch_vfork (struct breakpoint *b)
7734{
7735 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7736}
7737
6149aea9
PA
7738/* Implement the "print_recreate" breakpoint_ops method for vfork
7739 catchpoints. */
7740
7741static void
7742print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7743{
7744 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7745 print_recreate_thread (b, fp);
6149aea9
PA
7746}
7747
ce78b96d
JB
7748/* The breakpoint_ops structure to be used in vfork catchpoints. */
7749
2060206e 7750static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7751
edcc5120
TT
7752/* An instance of this type is used to represent an solib catchpoint.
7753 It includes a "struct breakpoint" as a kind of base class; users
7754 downcast to "struct breakpoint *" when needed. A breakpoint is
7755 really of this type iff its ops pointer points to
7756 CATCH_SOLIB_BREAKPOINT_OPS. */
7757
7758struct solib_catchpoint
7759{
7760 /* The base class. */
7761 struct breakpoint base;
7762
7763 /* True for "catch load", false for "catch unload". */
7764 unsigned char is_load;
7765
7766 /* Regular expression to match, if any. COMPILED is only valid when
7767 REGEX is non-NULL. */
7768 char *regex;
7769 regex_t compiled;
7770};
7771
7772static void
7773dtor_catch_solib (struct breakpoint *b)
7774{
7775 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7776
7777 if (self->regex)
7778 regfree (&self->compiled);
7779 xfree (self->regex);
7780
7781 base_breakpoint_ops.dtor (b);
7782}
7783
7784static int
7785insert_catch_solib (struct bp_location *ignore)
7786{
7787 return 0;
7788}
7789
7790static int
7791remove_catch_solib (struct bp_location *ignore)
7792{
7793 return 0;
7794}
7795
7796static int
7797breakpoint_hit_catch_solib (const struct bp_location *bl,
7798 struct address_space *aspace,
7799 CORE_ADDR bp_addr,
7800 const struct target_waitstatus *ws)
7801{
7802 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7803 struct breakpoint *other;
7804
7805 if (ws->kind == TARGET_WAITKIND_LOADED)
7806 return 1;
7807
7808 ALL_BREAKPOINTS (other)
7809 {
7810 struct bp_location *other_bl;
7811
7812 if (other == bl->owner)
7813 continue;
7814
7815 if (other->type != bp_shlib_event)
7816 continue;
7817
7818 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7819 continue;
7820
7821 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7822 {
7823 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7824 return 1;
7825 }
7826 }
7827
7828 return 0;
7829}
7830
7831static void
7832check_status_catch_solib (struct bpstats *bs)
7833{
7834 struct solib_catchpoint *self
7835 = (struct solib_catchpoint *) bs->breakpoint_at;
7836 int ix;
7837
7838 if (self->is_load)
7839 {
7840 struct so_list *iter;
7841
7842 for (ix = 0;
7843 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7844 ix, iter);
7845 ++ix)
7846 {
7847 if (!self->regex
7848 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7849 return;
7850 }
7851 }
7852 else
7853 {
7854 char *iter;
7855
7856 for (ix = 0;
7857 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7858 ix, iter);
7859 ++ix)
7860 {
7861 if (!self->regex
7862 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7863 return;
7864 }
7865 }
7866
7867 bs->stop = 0;
7868 bs->print_it = print_it_noop;
7869}
7870
7871static enum print_stop_action
7872print_it_catch_solib (bpstat bs)
7873{
7874 struct breakpoint *b = bs->breakpoint_at;
7875 struct ui_out *uiout = current_uiout;
7876
7877 annotate_catchpoint (b->number);
7878 if (b->disposition == disp_del)
7879 ui_out_text (uiout, "\nTemporary catchpoint ");
7880 else
7881 ui_out_text (uiout, "\nCatchpoint ");
7882 ui_out_field_int (uiout, "bkptno", b->number);
7883 ui_out_text (uiout, "\n");
7884 if (ui_out_is_mi_like_p (uiout))
7885 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7886 print_solib_event (1);
7887 return PRINT_SRC_AND_LOC;
7888}
7889
7890static void
7891print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7892{
7893 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7894 struct value_print_options opts;
7895 struct ui_out *uiout = current_uiout;
7896 char *msg;
7897
7898 get_user_print_options (&opts);
7899 /* Field 4, the address, is omitted (which makes the columns not
7900 line up too nicely with the headers, but the effect is relatively
7901 readable). */
7902 if (opts.addressprint)
7903 {
7904 annotate_field (4);
7905 ui_out_field_skip (uiout, "addr");
7906 }
7907
7908 annotate_field (5);
7909 if (self->is_load)
7910 {
7911 if (self->regex)
7912 msg = xstrprintf (_("load of library matching %s"), self->regex);
7913 else
7914 msg = xstrdup (_("load of library"));
7915 }
7916 else
7917 {
7918 if (self->regex)
7919 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7920 else
7921 msg = xstrdup (_("unload of library"));
7922 }
7923 ui_out_field_string (uiout, "what", msg);
7924 xfree (msg);
8ac3646f
TT
7925
7926 if (ui_out_is_mi_like_p (uiout))
7927 ui_out_field_string (uiout, "catch-type",
7928 self->is_load ? "load" : "unload");
edcc5120
TT
7929}
7930
7931static void
7932print_mention_catch_solib (struct breakpoint *b)
7933{
7934 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7935
7936 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7937 self->is_load ? "load" : "unload");
7938}
7939
7940static void
7941print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7942{
7943 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7944
7945 fprintf_unfiltered (fp, "%s %s",
7946 b->disposition == disp_del ? "tcatch" : "catch",
7947 self->is_load ? "load" : "unload");
7948 if (self->regex)
7949 fprintf_unfiltered (fp, " %s", self->regex);
7950 fprintf_unfiltered (fp, "\n");
7951}
7952
7953static struct breakpoint_ops catch_solib_breakpoint_ops;
7954
91985142
MG
7955/* Shared helper function (MI and CLI) for creating and installing
7956 a shared object event catchpoint. If IS_LOAD is non-zero then
7957 the events to be caught are load events, otherwise they are
7958 unload events. If IS_TEMP is non-zero the catchpoint is a
7959 temporary one. If ENABLED is non-zero the catchpoint is
7960 created in an enabled state. */
edcc5120 7961
91985142
MG
7962void
7963add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
7964{
7965 struct solib_catchpoint *c;
7966 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
7967 struct cleanup *cleanup;
7968
edcc5120
TT
7969 if (!arg)
7970 arg = "";
7971 arg = skip_spaces (arg);
7972
7973 c = XCNEW (struct solib_catchpoint);
7974 cleanup = make_cleanup (xfree, c);
7975
7976 if (*arg != '\0')
7977 {
7978 int errcode;
7979
7980 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7981 if (errcode != 0)
7982 {
7983 char *err = get_regcomp_error (errcode, &c->compiled);
7984
7985 make_cleanup (xfree, err);
7986 error (_("Invalid regexp (%s): %s"), err, arg);
7987 }
7988 c->regex = xstrdup (arg);
7989 }
7990
7991 c->is_load = is_load;
91985142 7992 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
7993 &catch_solib_breakpoint_ops);
7994
91985142
MG
7995 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
7996
edcc5120
TT
7997 discard_cleanups (cleanup);
7998 install_breakpoint (0, &c->base, 1);
7999}
8000
91985142
MG
8001/* A helper function that does all the work for "catch load" and
8002 "catch unload". */
8003
8004static void
8005catch_load_or_unload (char *arg, int from_tty, int is_load,
8006 struct cmd_list_element *command)
8007{
8008 int tempflag;
8009 const int enabled = 1;
8010
8011 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8012
8013 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8014}
8015
edcc5120
TT
8016static void
8017catch_load_command_1 (char *arg, int from_tty,
8018 struct cmd_list_element *command)
8019{
8020 catch_load_or_unload (arg, from_tty, 1, command);
8021}
8022
8023static void
8024catch_unload_command_1 (char *arg, int from_tty,
8025 struct cmd_list_element *command)
8026{
8027 catch_load_or_unload (arg, from_tty, 0, command);
8028}
8029
be5c67c1
PA
8030/* An instance of this type is used to represent a syscall catchpoint.
8031 It includes a "struct breakpoint" as a kind of base class; users
8032 downcast to "struct breakpoint *" when needed. A breakpoint is
8033 really of this type iff its ops pointer points to
8034 CATCH_SYSCALL_BREAKPOINT_OPS. */
8035
8036struct syscall_catchpoint
8037{
8038 /* The base class. */
8039 struct breakpoint base;
8040
8041 /* Syscall numbers used for the 'catch syscall' feature. If no
8042 syscall has been specified for filtering, its value is NULL.
8043 Otherwise, it holds a list of all syscalls to be caught. The
8044 list elements are allocated with xmalloc. */
8045 VEC(int) *syscalls_to_be_caught;
8046};
8047
8048/* Implement the "dtor" breakpoint_ops method for syscall
8049 catchpoints. */
8050
8051static void
8052dtor_catch_syscall (struct breakpoint *b)
8053{
8054 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8055
8056 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8057
2060206e 8058 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8059}
8060
fa3064dd
YQ
8061static const struct inferior_data *catch_syscall_inferior_data = NULL;
8062
8063struct catch_syscall_inferior_data
8064{
8065 /* We keep a count of the number of times the user has requested a
8066 particular syscall to be tracked, and pass this information to the
8067 target. This lets capable targets implement filtering directly. */
8068
8069 /* Number of times that "any" syscall is requested. */
8070 int any_syscall_count;
8071
8072 /* Count of each system call. */
8073 VEC(int) *syscalls_counts;
8074
8075 /* This counts all syscall catch requests, so we can readily determine
8076 if any catching is necessary. */
8077 int total_syscalls_count;
8078};
8079
8080static struct catch_syscall_inferior_data*
8081get_catch_syscall_inferior_data (struct inferior *inf)
8082{
8083 struct catch_syscall_inferior_data *inf_data;
8084
8085 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8086 if (inf_data == NULL)
8087 {
8088 inf_data = XZALLOC (struct catch_syscall_inferior_data);
8089 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8090 }
8091
8092 return inf_data;
8093}
8094
8095static void
8096catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8097{
8098 xfree (arg);
8099}
8100
8101
a96d9b2e
SDJ
8102/* Implement the "insert" breakpoint_ops method for syscall
8103 catchpoints. */
8104
77b06cd7
TJB
8105static int
8106insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8107{
be5c67c1 8108 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8109 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8110 struct catch_syscall_inferior_data *inf_data
8111 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8112
fa3064dd 8113 ++inf_data->total_syscalls_count;
be5c67c1 8114 if (!c->syscalls_to_be_caught)
fa3064dd 8115 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8116 else
8117 {
8118 int i, iter;
cc59ec59 8119
a96d9b2e 8120 for (i = 0;
be5c67c1 8121 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8122 i++)
8123 {
8124 int elem;
cc59ec59 8125
fa3064dd 8126 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8127 {
fa3064dd 8128 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8129 uintptr_t vec_addr_offset
8130 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8131 uintptr_t vec_addr;
fa3064dd
YQ
8132 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8133 vec_addr = ((uintptr_t) VEC_address (int,
8134 inf_data->syscalls_counts)
8135 + vec_addr_offset);
a96d9b2e
SDJ
8136 memset ((void *) vec_addr, 0,
8137 (iter + 1 - old_size) * sizeof (int));
8138 }
fa3064dd
YQ
8139 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8140 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8141 }
8142 }
8143
77b06cd7 8144 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
8145 inf_data->total_syscalls_count != 0,
8146 inf_data->any_syscall_count,
8147 VEC_length (int,
8148 inf_data->syscalls_counts),
8149 VEC_address (int,
8150 inf_data->syscalls_counts));
a96d9b2e
SDJ
8151}
8152
8153/* Implement the "remove" breakpoint_ops method for syscall
8154 catchpoints. */
8155
8156static int
77b06cd7 8157remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8158{
be5c67c1 8159 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8160 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8161 struct catch_syscall_inferior_data *inf_data
8162 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8163
fa3064dd 8164 --inf_data->total_syscalls_count;
be5c67c1 8165 if (!c->syscalls_to_be_caught)
fa3064dd 8166 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8167 else
8168 {
8169 int i, iter;
cc59ec59 8170
a96d9b2e 8171 for (i = 0;
be5c67c1 8172 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8173 i++)
8174 {
8175 int elem;
fa3064dd 8176 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8177 /* Shouldn't happen. */
8178 continue;
fa3064dd
YQ
8179 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8180 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8181 }
8182 }
8183
8184 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
8185 inf_data->total_syscalls_count != 0,
8186 inf_data->any_syscall_count,
8187 VEC_length (int,
8188 inf_data->syscalls_counts),
3e43a32a 8189 VEC_address (int,
fa3064dd 8190 inf_data->syscalls_counts));
a96d9b2e
SDJ
8191}
8192
8193/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8194 catchpoints. */
8195
8196static int
f1310107 8197breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8198 struct address_space *aspace, CORE_ADDR bp_addr,
8199 const struct target_waitstatus *ws)
a96d9b2e 8200{
4a64f543
MS
8201 /* We must check if we are catching specific syscalls in this
8202 breakpoint. If we are, then we must guarantee that the called
8203 syscall is the same syscall we are catching. */
a96d9b2e 8204 int syscall_number = 0;
be5c67c1
PA
8205 const struct syscall_catchpoint *c
8206 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8207
f90263c1
TT
8208 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8209 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8210 return 0;
8211
f90263c1
TT
8212 syscall_number = ws->value.syscall_number;
8213
a96d9b2e 8214 /* Now, checking if the syscall is the same. */
be5c67c1 8215 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8216 {
8217 int i, iter;
cc59ec59 8218
a96d9b2e 8219 for (i = 0;
be5c67c1 8220 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8221 i++)
8222 if (syscall_number == iter)
8223 break;
8224 /* Not the same. */
8225 if (!iter)
8226 return 0;
8227 }
8228
8229 return 1;
8230}
8231
8232/* Implement the "print_it" breakpoint_ops method for syscall
8233 catchpoints. */
8234
8235static enum print_stop_action
348d480f 8236print_it_catch_syscall (bpstat bs)
a96d9b2e 8237{
36dfb11c 8238 struct ui_out *uiout = current_uiout;
348d480f 8239 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8240 /* These are needed because we want to know in which state a
8241 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8242 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8243 must print "called syscall" or "returned from syscall". */
8244 ptid_t ptid;
8245 struct target_waitstatus last;
8246 struct syscall s;
a96d9b2e
SDJ
8247
8248 get_last_target_status (&ptid, &last);
8249
8250 get_syscall_by_number (last.value.syscall_number, &s);
8251
8252 annotate_catchpoint (b->number);
8253
36dfb11c
TT
8254 if (b->disposition == disp_del)
8255 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8256 else
36dfb11c
TT
8257 ui_out_text (uiout, "\nCatchpoint ");
8258 if (ui_out_is_mi_like_p (uiout))
8259 {
8260 ui_out_field_string (uiout, "reason",
8261 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8262 ? EXEC_ASYNC_SYSCALL_ENTRY
8263 : EXEC_ASYNC_SYSCALL_RETURN));
8264 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8265 }
8266 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8267
8268 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8269 ui_out_text (uiout, " (call to syscall ");
8270 else
8271 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8272
36dfb11c
TT
8273 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8274 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8275 if (s.name != NULL)
8276 ui_out_field_string (uiout, "syscall-name", s.name);
8277
8278 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8279
8280 return PRINT_SRC_AND_LOC;
8281}
8282
8283/* Implement the "print_one" breakpoint_ops method for syscall
8284 catchpoints. */
8285
8286static void
8287print_one_catch_syscall (struct breakpoint *b,
f1310107 8288 struct bp_location **last_loc)
a96d9b2e 8289{
be5c67c1 8290 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8291 struct value_print_options opts;
79a45e25 8292 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8293
8294 get_user_print_options (&opts);
4a64f543
MS
8295 /* Field 4, the address, is omitted (which makes the columns not
8296 line up too nicely with the headers, but the effect is relatively
8297 readable). */
a96d9b2e
SDJ
8298 if (opts.addressprint)
8299 ui_out_field_skip (uiout, "addr");
8300 annotate_field (5);
8301
be5c67c1
PA
8302 if (c->syscalls_to_be_caught
8303 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8304 ui_out_text (uiout, "syscalls \"");
8305 else
8306 ui_out_text (uiout, "syscall \"");
8307
be5c67c1 8308 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8309 {
8310 int i, iter;
8311 char *text = xstrprintf ("%s", "");
cc59ec59 8312
a96d9b2e 8313 for (i = 0;
be5c67c1 8314 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8315 i++)
8316 {
8317 char *x = text;
8318 struct syscall s;
8319 get_syscall_by_number (iter, &s);
8320
8321 if (s.name != NULL)
8322 text = xstrprintf ("%s%s, ", text, s.name);
8323 else
8324 text = xstrprintf ("%s%d, ", text, iter);
8325
8326 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8327 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8328 on every call. */
8329 xfree (x);
8330 }
8331 /* Remove the last comma. */
8332 text[strlen (text) - 2] = '\0';
8333 ui_out_field_string (uiout, "what", text);
8334 }
8335 else
8336 ui_out_field_string (uiout, "what", "<any syscall>");
8337 ui_out_text (uiout, "\" ");
8ac3646f
TT
8338
8339 if (ui_out_is_mi_like_p (uiout))
8340 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8341}
8342
8343/* Implement the "print_mention" breakpoint_ops method for syscall
8344 catchpoints. */
8345
8346static void
8347print_mention_catch_syscall (struct breakpoint *b)
8348{
be5c67c1
PA
8349 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8350
8351 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8352 {
8353 int i, iter;
8354
be5c67c1 8355 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8356 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8357 else
8358 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8359
8360 for (i = 0;
be5c67c1 8361 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8362 i++)
8363 {
8364 struct syscall s;
8365 get_syscall_by_number (iter, &s);
8366
8367 if (s.name)
8368 printf_filtered (" '%s' [%d]", s.name, s.number);
8369 else
8370 printf_filtered (" %d", s.number);
8371 }
8372 printf_filtered (")");
8373 }
8374 else
8375 printf_filtered (_("Catchpoint %d (any syscall)"),
8376 b->number);
8377}
8378
6149aea9
PA
8379/* Implement the "print_recreate" breakpoint_ops method for syscall
8380 catchpoints. */
8381
8382static void
8383print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8384{
be5c67c1
PA
8385 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8386
6149aea9
PA
8387 fprintf_unfiltered (fp, "catch syscall");
8388
be5c67c1 8389 if (c->syscalls_to_be_caught)
6149aea9
PA
8390 {
8391 int i, iter;
8392
8393 for (i = 0;
be5c67c1 8394 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8395 i++)
8396 {
8397 struct syscall s;
8398
8399 get_syscall_by_number (iter, &s);
8400 if (s.name)
8401 fprintf_unfiltered (fp, " %s", s.name);
8402 else
8403 fprintf_unfiltered (fp, " %d", s.number);
8404 }
8405 }
d9b3f62e 8406 print_recreate_thread (b, fp);
6149aea9
PA
8407}
8408
a96d9b2e
SDJ
8409/* The breakpoint_ops structure to be used in syscall catchpoints. */
8410
2060206e 8411static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8412
8413/* Returns non-zero if 'b' is a syscall catchpoint. */
8414
8415static int
8416syscall_catchpoint_p (struct breakpoint *b)
8417{
8418 return (b->ops == &catch_syscall_breakpoint_ops);
8419}
8420
346774a9
PA
8421/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8422 is non-zero, then make the breakpoint temporary. If COND_STRING is
8423 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8424 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8425
ab04a2af 8426void
346774a9
PA
8427init_catchpoint (struct breakpoint *b,
8428 struct gdbarch *gdbarch, int tempflag,
8429 char *cond_string,
c0a91b2b 8430 const struct breakpoint_ops *ops)
c906108c 8431{
c5aa993b 8432 struct symtab_and_line sal;
346774a9 8433
fe39c653 8434 init_sal (&sal);
6c95b8df 8435 sal.pspace = current_program_space;
c5aa993b 8436
28010a5d 8437 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8438
1b36a34b 8439 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8440 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8441}
8442
28010a5d 8443void
3ea46bff 8444install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8445{
8446 add_to_breakpoint_chain (b);
3a5c3e22 8447 set_breakpoint_number (internal, b);
558a9d82
YQ
8448 if (is_tracepoint (b))
8449 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8450 if (!internal)
8451 mention (b);
c56053d2 8452 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8453
8454 if (update_gll)
8455 update_global_location_list (1);
c56053d2
PA
8456}
8457
9b70b993 8458static void
a6d9a66e
UW
8459create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8460 int tempflag, char *cond_string,
c0a91b2b 8461 const struct breakpoint_ops *ops)
c906108c 8462{
e29a4733 8463 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8464
e29a4733
PA
8465 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8466
8467 c->forked_inferior_pid = null_ptid;
8468
3ea46bff 8469 install_breakpoint (0, &c->base, 1);
c906108c
SS
8470}
8471
fe798b75
JB
8472/* Exec catchpoints. */
8473
b4d90040
PA
8474/* An instance of this type is used to represent an exec catchpoint.
8475 It includes a "struct breakpoint" as a kind of base class; users
8476 downcast to "struct breakpoint *" when needed. A breakpoint is
8477 really of this type iff its ops pointer points to
8478 CATCH_EXEC_BREAKPOINT_OPS. */
8479
8480struct exec_catchpoint
8481{
8482 /* The base class. */
8483 struct breakpoint base;
8484
8485 /* Filename of a program whose exec triggered this catchpoint.
8486 This field is only valid immediately after this catchpoint has
8487 triggered. */
8488 char *exec_pathname;
8489};
8490
8491/* Implement the "dtor" breakpoint_ops method for exec
8492 catchpoints. */
8493
8494static void
8495dtor_catch_exec (struct breakpoint *b)
8496{
8497 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8498
8499 xfree (c->exec_pathname);
348d480f 8500
2060206e 8501 base_breakpoint_ops.dtor (b);
b4d90040
PA
8502}
8503
77b06cd7
TJB
8504static int
8505insert_catch_exec (struct bp_location *bl)
c906108c 8506{
77b06cd7 8507 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 8508}
c906108c 8509
fe798b75 8510static int
77b06cd7 8511remove_catch_exec (struct bp_location *bl)
fe798b75
JB
8512{
8513 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8514}
c906108c 8515
fe798b75 8516static int
f1310107 8517breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8518 struct address_space *aspace, CORE_ADDR bp_addr,
8519 const struct target_waitstatus *ws)
fe798b75 8520{
b4d90040
PA
8521 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8522
f90263c1
TT
8523 if (ws->kind != TARGET_WAITKIND_EXECD)
8524 return 0;
8525
8526 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8527 return 1;
fe798b75 8528}
c906108c 8529
fe798b75 8530static enum print_stop_action
348d480f 8531print_it_catch_exec (bpstat bs)
fe798b75 8532{
36dfb11c 8533 struct ui_out *uiout = current_uiout;
348d480f 8534 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8535 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8536
fe798b75 8537 annotate_catchpoint (b->number);
36dfb11c
TT
8538 if (b->disposition == disp_del)
8539 ui_out_text (uiout, "\nTemporary catchpoint ");
8540 else
8541 ui_out_text (uiout, "\nCatchpoint ");
8542 if (ui_out_is_mi_like_p (uiout))
8543 {
8544 ui_out_field_string (uiout, "reason",
8545 async_reason_lookup (EXEC_ASYNC_EXEC));
8546 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8547 }
8548 ui_out_field_int (uiout, "bkptno", b->number);
8549 ui_out_text (uiout, " (exec'd ");
8550 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8551 ui_out_text (uiout, "), ");
8552
fe798b75 8553 return PRINT_SRC_AND_LOC;
c906108c
SS
8554}
8555
fe798b75 8556static void
a6d9a66e 8557print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8558{
b4d90040 8559 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8560 struct value_print_options opts;
79a45e25 8561 struct ui_out *uiout = current_uiout;
fe798b75
JB
8562
8563 get_user_print_options (&opts);
8564
8565 /* Field 4, the address, is omitted (which makes the columns
8566 not line up too nicely with the headers, but the effect
8567 is relatively readable). */
8568 if (opts.addressprint)
8569 ui_out_field_skip (uiout, "addr");
8570 annotate_field (5);
8571 ui_out_text (uiout, "exec");
b4d90040 8572 if (c->exec_pathname != NULL)
fe798b75
JB
8573 {
8574 ui_out_text (uiout, ", program \"");
b4d90040 8575 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8576 ui_out_text (uiout, "\" ");
8577 }
8ac3646f
TT
8578
8579 if (ui_out_is_mi_like_p (uiout))
8580 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8581}
8582
8583static void
8584print_mention_catch_exec (struct breakpoint *b)
8585{
8586 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8587}
8588
6149aea9
PA
8589/* Implement the "print_recreate" breakpoint_ops method for exec
8590 catchpoints. */
8591
8592static void
8593print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8594{
8595 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8596 print_recreate_thread (b, fp);
6149aea9
PA
8597}
8598
2060206e 8599static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8600
a96d9b2e
SDJ
8601static void
8602create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8603 const struct breakpoint_ops *ops)
a96d9b2e 8604{
be5c67c1 8605 struct syscall_catchpoint *c;
a96d9b2e 8606 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8607
be5c67c1
PA
8608 c = XNEW (struct syscall_catchpoint);
8609 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8610 c->syscalls_to_be_caught = filter;
a96d9b2e 8611
3ea46bff 8612 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8613}
8614
c906108c 8615static int
fba45db2 8616hw_breakpoint_used_count (void)
c906108c 8617{
c906108c 8618 int i = 0;
f1310107
TJB
8619 struct breakpoint *b;
8620 struct bp_location *bl;
c906108c
SS
8621
8622 ALL_BREAKPOINTS (b)
c5aa993b 8623 {
d6b74ac4 8624 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8625 for (bl = b->loc; bl; bl = bl->next)
8626 {
8627 /* Special types of hardware breakpoints may use more than
8628 one register. */
348d480f 8629 i += b->ops->resources_needed (bl);
f1310107 8630 }
c5aa993b 8631 }
c906108c
SS
8632
8633 return i;
8634}
8635
a1398e0c
PA
8636/* Returns the resources B would use if it were a hardware
8637 watchpoint. */
8638
c906108c 8639static int
a1398e0c 8640hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8641{
c906108c 8642 int i = 0;
e09342b5 8643 struct bp_location *bl;
c906108c 8644
a1398e0c
PA
8645 if (!breakpoint_enabled (b))
8646 return 0;
8647
8648 for (bl = b->loc; bl; bl = bl->next)
8649 {
8650 /* Special types of hardware watchpoints may use more than
8651 one register. */
8652 i += b->ops->resources_needed (bl);
8653 }
8654
8655 return i;
8656}
8657
8658/* Returns the sum the used resources of all hardware watchpoints of
8659 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8660 the sum of the used resources of all hardware watchpoints of other
8661 types _not_ TYPE. */
8662
8663static int
8664hw_watchpoint_used_count_others (struct breakpoint *except,
8665 enum bptype type, int *other_type_used)
8666{
8667 int i = 0;
8668 struct breakpoint *b;
8669
c906108c
SS
8670 *other_type_used = 0;
8671 ALL_BREAKPOINTS (b)
e09342b5 8672 {
a1398e0c
PA
8673 if (b == except)
8674 continue;
e09342b5
TJB
8675 if (!breakpoint_enabled (b))
8676 continue;
8677
a1398e0c
PA
8678 if (b->type == type)
8679 i += hw_watchpoint_use_count (b);
8680 else if (is_hardware_watchpoint (b))
8681 *other_type_used = 1;
e09342b5
TJB
8682 }
8683
c906108c
SS
8684 return i;
8685}
8686
c906108c 8687void
fba45db2 8688disable_watchpoints_before_interactive_call_start (void)
c906108c 8689{
c5aa993b 8690 struct breakpoint *b;
c906108c
SS
8691
8692 ALL_BREAKPOINTS (b)
c5aa993b 8693 {
cc60f2e3 8694 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8695 {
b5de0fa7 8696 b->enable_state = bp_call_disabled;
b60e7edf 8697 update_global_location_list (0);
c5aa993b
JM
8698 }
8699 }
c906108c
SS
8700}
8701
8702void
fba45db2 8703enable_watchpoints_after_interactive_call_stop (void)
c906108c 8704{
c5aa993b 8705 struct breakpoint *b;
c906108c
SS
8706
8707 ALL_BREAKPOINTS (b)
c5aa993b 8708 {
cc60f2e3 8709 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8710 {
b5de0fa7 8711 b->enable_state = bp_enabled;
b60e7edf 8712 update_global_location_list (1);
c5aa993b
JM
8713 }
8714 }
c906108c
SS
8715}
8716
8bea4e01
UW
8717void
8718disable_breakpoints_before_startup (void)
8719{
6c95b8df 8720 current_program_space->executing_startup = 1;
f8eba3c6 8721 update_global_location_list (0);
8bea4e01
UW
8722}
8723
8724void
8725enable_breakpoints_after_startup (void)
8726{
6c95b8df 8727 current_program_space->executing_startup = 0;
f8eba3c6 8728 breakpoint_re_set ();
8bea4e01
UW
8729}
8730
c906108c
SS
8731
8732/* Set a breakpoint that will evaporate an end of command
8733 at address specified by SAL.
8734 Restrict it to frame FRAME if FRAME is nonzero. */
8735
8736struct breakpoint *
a6d9a66e
UW
8737set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8738 struct frame_id frame_id, enum bptype type)
c906108c 8739{
52f0bd74 8740 struct breakpoint *b;
edb3359d 8741
193facb3
JK
8742 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8743 tail-called one. */
8744 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8745
06edf0c0 8746 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8747 b->enable_state = bp_enabled;
8748 b->disposition = disp_donttouch;
818dd999 8749 b->frame_id = frame_id;
c906108c 8750
4a64f543
MS
8751 /* If we're debugging a multi-threaded program, then we want
8752 momentary breakpoints to be active in only a single thread of
8753 control. */
39f77062
KB
8754 if (in_thread_list (inferior_ptid))
8755 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8756
b60e7edf 8757 update_global_location_list_nothrow (1);
74960c60 8758
c906108c
SS
8759 return b;
8760}
611c83ae 8761
06edf0c0
PA
8762/* Make a momentary breakpoint based on the master breakpoint ORIG.
8763 The new breakpoint will have type TYPE, and use OPS as it
8764 breakpoint_ops. */
e58b0e63 8765
06edf0c0
PA
8766static struct breakpoint *
8767momentary_breakpoint_from_master (struct breakpoint *orig,
8768 enum bptype type,
c0a91b2b 8769 const struct breakpoint_ops *ops)
e58b0e63
PA
8770{
8771 struct breakpoint *copy;
8772
06edf0c0 8773 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8774 copy->loc = allocate_bp_location (copy);
0e30163f 8775 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8776
a6d9a66e 8777 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8778 copy->loc->requested_address = orig->loc->requested_address;
8779 copy->loc->address = orig->loc->address;
8780 copy->loc->section = orig->loc->section;
6c95b8df 8781 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8782 copy->loc->probe = orig->loc->probe;
f8eba3c6 8783 copy->loc->line_number = orig->loc->line_number;
2f202fde 8784 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
8785 copy->frame_id = orig->frame_id;
8786 copy->thread = orig->thread;
6c95b8df 8787 copy->pspace = orig->pspace;
e58b0e63
PA
8788
8789 copy->enable_state = bp_enabled;
8790 copy->disposition = disp_donttouch;
8791 copy->number = internal_breakpoint_number--;
8792
8793 update_global_location_list_nothrow (0);
8794 return copy;
8795}
8796
06edf0c0
PA
8797/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8798 ORIG is NULL. */
8799
8800struct breakpoint *
8801clone_momentary_breakpoint (struct breakpoint *orig)
8802{
8803 /* If there's nothing to clone, then return nothing. */
8804 if (orig == NULL)
8805 return NULL;
8806
8807 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8808}
8809
611c83ae 8810struct breakpoint *
a6d9a66e
UW
8811set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8812 enum bptype type)
611c83ae
PA
8813{
8814 struct symtab_and_line sal;
8815
8816 sal = find_pc_line (pc, 0);
8817 sal.pc = pc;
8818 sal.section = find_pc_overlay (pc);
8819 sal.explicit_pc = 1;
8820
a6d9a66e 8821 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8822}
c906108c 8823\f
c5aa993b 8824
c906108c
SS
8825/* Tell the user we have just set a breakpoint B. */
8826
8827static void
fba45db2 8828mention (struct breakpoint *b)
c906108c 8829{
348d480f 8830 b->ops->print_mention (b);
79a45e25 8831 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8832 return;
c906108c
SS
8833 printf_filtered ("\n");
8834}
c906108c 8835\f
c5aa993b 8836
0d381245 8837static struct bp_location *
39d61571 8838add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8839 const struct symtab_and_line *sal)
8840{
8841 struct bp_location *loc, **tmp;
3742cc8b
YQ
8842 CORE_ADDR adjusted_address;
8843 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8844
8845 if (loc_gdbarch == NULL)
8846 loc_gdbarch = b->gdbarch;
8847
8848 /* Adjust the breakpoint's address prior to allocating a location.
8849 Once we call allocate_bp_location(), that mostly uninitialized
8850 location will be placed on the location chain. Adjustment of the
8851 breakpoint may cause target_read_memory() to be called and we do
8852 not want its scan of the location chain to find a breakpoint and
8853 location that's only been partially initialized. */
8854 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8855 sal->pc, b->type);
0d381245 8856
d30113d4 8857 /* Sort the locations by their ADDRESS. */
39d61571 8858 loc = allocate_bp_location (b);
d30113d4
JK
8859 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8860 tmp = &((*tmp)->next))
0d381245 8861 ;
d30113d4 8862 loc->next = *tmp;
0d381245 8863 *tmp = loc;
3742cc8b 8864
0d381245 8865 loc->requested_address = sal->pc;
3742cc8b 8866 loc->address = adjusted_address;
6c95b8df 8867 loc->pspace = sal->pspace;
55aa24fb 8868 loc->probe = sal->probe;
6c95b8df 8869 gdb_assert (loc->pspace != NULL);
0d381245 8870 loc->section = sal->section;
3742cc8b 8871 loc->gdbarch = loc_gdbarch;
f8eba3c6 8872 loc->line_number = sal->line;
2f202fde 8873 loc->symtab = sal->symtab;
f8eba3c6 8874
0e30163f
JK
8875 set_breakpoint_location_function (loc,
8876 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8877 return loc;
8878}
514f746b
AR
8879\f
8880
8881/* Return 1 if LOC is pointing to a permanent breakpoint,
8882 return 0 otherwise. */
8883
8884static int
8885bp_loc_is_permanent (struct bp_location *loc)
8886{
8887 int len;
8888 CORE_ADDR addr;
1afeeb75 8889 const gdb_byte *bpoint;
514f746b 8890 gdb_byte *target_mem;
939c61fa
JK
8891 struct cleanup *cleanup;
8892 int retval = 0;
514f746b
AR
8893
8894 gdb_assert (loc != NULL);
8895
8896 addr = loc->address;
1afeeb75 8897 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8898
939c61fa 8899 /* Software breakpoints unsupported? */
1afeeb75 8900 if (bpoint == NULL)
939c61fa
JK
8901 return 0;
8902
514f746b
AR
8903 target_mem = alloca (len);
8904
939c61fa
JK
8905 /* Enable the automatic memory restoration from breakpoints while
8906 we read the memory. Otherwise we could say about our temporary
8907 breakpoints they are permanent. */
6c95b8df
PA
8908 cleanup = save_current_space_and_thread ();
8909
8910 switch_to_program_space_and_thread (loc->pspace);
8911 make_show_memory_breakpoints_cleanup (0);
939c61fa 8912
514f746b 8913 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8914 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8915 retval = 1;
514f746b 8916
939c61fa
JK
8917 do_cleanups (cleanup);
8918
8919 return retval;
514f746b
AR
8920}
8921
e7e0cddf
SS
8922/* Build a command list for the dprintf corresponding to the current
8923 settings of the dprintf style options. */
8924
8925static void
8926update_dprintf_command_list (struct breakpoint *b)
8927{
8928 char *dprintf_args = b->extra_string;
8929 char *printf_line = NULL;
8930
8931 if (!dprintf_args)
8932 return;
8933
8934 dprintf_args = skip_spaces (dprintf_args);
8935
8936 /* Allow a comma, as it may have terminated a location, but don't
8937 insist on it. */
8938 if (*dprintf_args == ',')
8939 ++dprintf_args;
8940 dprintf_args = skip_spaces (dprintf_args);
8941
8942 if (*dprintf_args != '"')
8943 error (_("Bad format string, missing '\"'."));
8944
d3ce09f5 8945 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8946 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8947 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8948 {
8949 if (!dprintf_function)
8950 error (_("No function supplied for dprintf call"));
8951
8952 if (dprintf_channel && strlen (dprintf_channel) > 0)
8953 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8954 dprintf_function,
8955 dprintf_channel,
8956 dprintf_args);
8957 else
8958 printf_line = xstrprintf ("call (void) %s (%s)",
8959 dprintf_function,
8960 dprintf_args);
8961 }
d3ce09f5
SS
8962 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8963 {
8964 if (target_can_run_breakpoint_commands ())
8965 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8966 else
8967 {
8968 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8969 printf_line = xstrprintf ("printf %s", dprintf_args);
8970 }
8971 }
e7e0cddf
SS
8972 else
8973 internal_error (__FILE__, __LINE__,
8974 _("Invalid dprintf style."));
8975
f28045c2 8976 gdb_assert (printf_line != NULL);
9d6e6e84 8977 /* Manufacture a printf sequence. */
f28045c2 8978 {
9d6e6e84
HZ
8979 struct command_line *printf_cmd_line
8980 = xmalloc (sizeof (struct command_line));
e7e0cddf 8981
f28045c2
YQ
8982 printf_cmd_line = xmalloc (sizeof (struct command_line));
8983 printf_cmd_line->control_type = simple_control;
8984 printf_cmd_line->body_count = 0;
8985 printf_cmd_line->body_list = NULL;
9d6e6e84 8986 printf_cmd_line->next = NULL;
f28045c2 8987 printf_cmd_line->line = printf_line;
e7e0cddf 8988
f28045c2
YQ
8989 breakpoint_set_commands (b, printf_cmd_line);
8990 }
e7e0cddf
SS
8991}
8992
8993/* Update all dprintf commands, making their command lists reflect
8994 current style settings. */
8995
8996static void
8997update_dprintf_commands (char *args, int from_tty,
8998 struct cmd_list_element *c)
8999{
9000 struct breakpoint *b;
9001
9002 ALL_BREAKPOINTS (b)
9003 {
9004 if (b->type == bp_dprintf)
9005 update_dprintf_command_list (b);
9006 }
9007}
c3f6f71d 9008
018d34a4
VP
9009/* Create a breakpoint with SAL as location. Use ADDR_STRING
9010 as textual description of the location, and COND_STRING
db107f19 9011 as condition expression. */
018d34a4
VP
9012
9013static void
d9b3f62e
PA
9014init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9015 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9016 char *filter, char *cond_string,
e7e0cddf 9017 char *extra_string,
d9b3f62e
PA
9018 enum bptype type, enum bpdisp disposition,
9019 int thread, int task, int ignore_count,
c0a91b2b 9020 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9021 int enabled, int internal, unsigned flags,
9022 int display_canonical)
018d34a4 9023{
0d381245 9024 int i;
018d34a4
VP
9025
9026 if (type == bp_hardware_breakpoint)
9027 {
fbbd034e
AS
9028 int target_resources_ok;
9029
9030 i = hw_breakpoint_used_count ();
9031 target_resources_ok =
9032 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9033 i + 1, 0);
9034 if (target_resources_ok == 0)
9035 error (_("No hardware breakpoint support in the target."));
9036 else if (target_resources_ok < 0)
9037 error (_("Hardware breakpoints used exceeds limit."));
9038 }
9039
6c95b8df
PA
9040 gdb_assert (sals.nelts > 0);
9041
0d381245
VP
9042 for (i = 0; i < sals.nelts; ++i)
9043 {
9044 struct symtab_and_line sal = sals.sals[i];
9045 struct bp_location *loc;
9046
9047 if (from_tty)
5af949e3
UW
9048 {
9049 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9050 if (!loc_gdbarch)
9051 loc_gdbarch = gdbarch;
9052
9053 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9054 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9055 }
0d381245
VP
9056
9057 if (i == 0)
9058 {
d9b3f62e 9059 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9060 b->thread = thread;
4a306c9a 9061 b->task = task;
855a6e68 9062
0d381245 9063 b->cond_string = cond_string;
e7e0cddf 9064 b->extra_string = extra_string;
0d381245 9065 b->ignore_count = ignore_count;
41447f92 9066 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9067 b->disposition = disposition;
6c95b8df 9068
44f238bb
PA
9069 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9070 b->loc->inserted = 1;
9071
0fb4aa4b
PA
9072 if (type == bp_static_tracepoint)
9073 {
d9b3f62e 9074 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9075 struct static_tracepoint_marker marker;
9076
983af33b 9077 if (strace_marker_p (b))
0fb4aa4b
PA
9078 {
9079 /* We already know the marker exists, otherwise, we
9080 wouldn't see a sal for it. */
9081 char *p = &addr_string[3];
9082 char *endp;
9083 char *marker_str;
0fb4aa4b 9084
e9cafbcc 9085 p = skip_spaces (p);
0fb4aa4b 9086
e9cafbcc 9087 endp = skip_to_space (p);
0fb4aa4b
PA
9088
9089 marker_str = savestring (p, endp - p);
d9b3f62e 9090 t->static_trace_marker_id = marker_str;
0fb4aa4b 9091
3e43a32a
MS
9092 printf_filtered (_("Probed static tracepoint "
9093 "marker \"%s\"\n"),
d9b3f62e 9094 t->static_trace_marker_id);
0fb4aa4b
PA
9095 }
9096 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9097 {
d9b3f62e 9098 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9099 release_static_tracepoint_marker (&marker);
9100
3e43a32a
MS
9101 printf_filtered (_("Probed static tracepoint "
9102 "marker \"%s\"\n"),
d9b3f62e 9103 t->static_trace_marker_id);
0fb4aa4b
PA
9104 }
9105 else
3e43a32a
MS
9106 warning (_("Couldn't determine the static "
9107 "tracepoint marker to probe"));
0fb4aa4b
PA
9108 }
9109
0d381245
VP
9110 loc = b->loc;
9111 }
9112 else
018d34a4 9113 {
39d61571 9114 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9115 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9116 loc->inserted = 1;
0d381245
VP
9117 }
9118
514f746b
AR
9119 if (bp_loc_is_permanent (loc))
9120 make_breakpoint_permanent (b);
9121
0d381245
VP
9122 if (b->cond_string)
9123 {
bbc13ae3
KS
9124 const char *arg = b->cond_string;
9125
1bb9788d
TT
9126 loc->cond = parse_exp_1 (&arg, loc->address,
9127 block_for_pc (loc->address), 0);
0d381245 9128 if (*arg)
588ae58c 9129 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9130 }
e7e0cddf
SS
9131
9132 /* Dynamic printf requires and uses additional arguments on the
9133 command line, otherwise it's an error. */
9134 if (type == bp_dprintf)
9135 {
9136 if (b->extra_string)
9137 update_dprintf_command_list (b);
9138 else
9139 error (_("Format string required"));
9140 }
9141 else if (b->extra_string)
588ae58c 9142 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9143 }
018d34a4 9144
56435ebe 9145 b->display_canonical = display_canonical;
018d34a4
VP
9146 if (addr_string)
9147 b->addr_string = addr_string;
9148 else
9149 /* addr_string has to be used or breakpoint_re_set will delete
9150 me. */
5af949e3
UW
9151 b->addr_string
9152 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9153 b->filter = filter;
d9b3f62e 9154}
018d34a4 9155
d9b3f62e
PA
9156static void
9157create_breakpoint_sal (struct gdbarch *gdbarch,
9158 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9159 char *filter, char *cond_string,
e7e0cddf 9160 char *extra_string,
d9b3f62e
PA
9161 enum bptype type, enum bpdisp disposition,
9162 int thread, int task, int ignore_count,
c0a91b2b 9163 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9164 int enabled, int internal, unsigned flags,
9165 int display_canonical)
d9b3f62e
PA
9166{
9167 struct breakpoint *b;
9168 struct cleanup *old_chain;
9169
9170 if (is_tracepoint_type (type))
9171 {
9172 struct tracepoint *t;
9173
9174 t = XCNEW (struct tracepoint);
9175 b = &t->base;
9176 }
9177 else
9178 b = XNEW (struct breakpoint);
9179
9180 old_chain = make_cleanup (xfree, b);
9181
9182 init_breakpoint_sal (b, gdbarch,
9183 sals, addr_string,
e7e0cddf 9184 filter, cond_string, extra_string,
d9b3f62e
PA
9185 type, disposition,
9186 thread, task, ignore_count,
9187 ops, from_tty,
44f238bb
PA
9188 enabled, internal, flags,
9189 display_canonical);
d9b3f62e
PA
9190 discard_cleanups (old_chain);
9191
3ea46bff 9192 install_breakpoint (internal, b, 0);
018d34a4
VP
9193}
9194
9195/* Add SALS.nelts breakpoints to the breakpoint table. For each
9196 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9197 value. COND_STRING, if not NULL, specified the condition to be
9198 used for all breakpoints. Essentially the only case where
9199 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9200 function. In that case, it's still not possible to specify
9201 separate conditions for different overloaded functions, so
9202 we take just a single condition string.
9203
c3f6f71d 9204 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9205 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9206 array contents). If the function fails (error() is called), the
9207 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9208 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9209
9210static void
8cdf0e15 9211create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9212 struct linespec_result *canonical,
e7e0cddf 9213 char *cond_string, char *extra_string,
8cdf0e15
VP
9214 enum bptype type, enum bpdisp disposition,
9215 int thread, int task, int ignore_count,
c0a91b2b 9216 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9217 int enabled, int internal, unsigned flags)
c906108c 9218{
018d34a4 9219 int i;
f8eba3c6 9220 struct linespec_sals *lsal;
cc59ec59 9221
f8eba3c6
TT
9222 if (canonical->pre_expanded)
9223 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9224
9225 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9226 {
f8eba3c6
TT
9227 /* Note that 'addr_string' can be NULL in the case of a plain
9228 'break', without arguments. */
9229 char *addr_string = (canonical->addr_string
9230 ? xstrdup (canonical->addr_string)
9231 : NULL);
9232 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9233 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9234
f8eba3c6
TT
9235 make_cleanup (xfree, filter_string);
9236 create_breakpoint_sal (gdbarch, lsal->sals,
9237 addr_string,
9238 filter_string,
e7e0cddf
SS
9239 cond_string, extra_string,
9240 type, disposition,
84f4c1fe 9241 thread, task, ignore_count, ops,
44f238bb 9242 from_tty, enabled, internal, flags,
56435ebe 9243 canonical->special_display);
f8eba3c6 9244 discard_cleanups (inner);
c3f6f71d 9245 }
c3f6f71d 9246}
c906108c 9247
9998af43 9248/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9249 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9250 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9251 address strings. ADDRESS points to the end of the SAL.
9252
9253 The array and the line spec strings are allocated on the heap, it is
9254 the caller's responsibility to free them. */
c906108c 9255
b9362cc7 9256static void
c3f6f71d 9257parse_breakpoint_sals (char **address,
58438ac1 9258 struct linespec_result *canonical)
c3f6f71d 9259{
c3f6f71d 9260 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9261 breakpoint. */
c3f6f71d
JM
9262 if ((*address) == NULL
9263 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9264 {
1bfeeb0f
JL
9265 /* The last displayed codepoint, if it's valid, is our default breakpoint
9266 address. */
9267 if (last_displayed_sal_is_valid ())
c906108c 9268 {
f8eba3c6 9269 struct linespec_sals lsal;
c3f6f71d 9270 struct symtab_and_line sal;
1c8cdcb1 9271 CORE_ADDR pc;
cc59ec59 9272
4a64f543 9273 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9274 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9275 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9276
9277 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9278 corresponding to the last call to print_frame_info.
9279 Be sure to reinitialize LINE with NOTCURRENT == 0
9280 as the breakpoint line number is inappropriate otherwise.
9281 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9282 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9283 pc = sal.pc;
9284 sal = find_pc_line (pc, 0);
00903456 9285
4a64f543 9286 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9287 where PC is the last displayed codepoint's address. So
9288 make sure to set sal.explicit_pc to prevent GDB from
9289 trying to expand the list of sals to include all other
9290 instances with the same symtab and line. */
1c8cdcb1 9291 sal.pc = pc;
00903456
JK
9292 sal.explicit_pc = 1;
9293
f8eba3c6
TT
9294 lsal.sals.sals[0] = sal;
9295 lsal.sals.nelts = 1;
9296 lsal.canonical = NULL;
9297
9298 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9299 }
9300 else
8a3fe4f8 9301 error (_("No default breakpoint address now."));
c906108c
SS
9302 }
9303 else
9304 {
cc80f267
JK
9305 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9306
c906108c 9307 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9308 current_source_symtab (which is decode_line_1's default).
9309 This should produce the results we want almost all of the
cc80f267
JK
9310 time while leaving default_breakpoint_* alone.
9311
9312 ObjC: However, don't match an Objective-C method name which
9313 may have a '+' or '-' succeeded by a '['. */
9314 if (last_displayed_sal_is_valid ()
9315 && (!cursal.symtab
9316 || ((strchr ("+-", (*address)[0]) != NULL)
9317 && ((*address)[1] != '['))))
f8eba3c6
TT
9318 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9319 get_last_displayed_symtab (),
9320 get_last_displayed_line (),
9321 canonical, NULL, NULL);
c906108c 9322 else
f8eba3c6 9323 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9324 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9325 }
c3f6f71d 9326}
c906108c 9327
c906108c 9328
c3f6f71d 9329/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9330 inserted as a breakpoint. If it can't throw an error. */
c906108c 9331
b9362cc7 9332static void
23e7acfb 9333breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9334{
9335 int i;
cc59ec59 9336
c3f6f71d 9337 for (i = 0; i < sals->nelts; i++)
ee53e872 9338 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9339}
9340
7a697b8d
SS
9341/* Fast tracepoints may have restrictions on valid locations. For
9342 instance, a fast tracepoint using a jump instead of a trap will
9343 likely have to overwrite more bytes than a trap would, and so can
9344 only be placed where the instruction is longer than the jump, or a
9345 multi-instruction sequence does not have a jump into the middle of
9346 it, etc. */
9347
9348static void
9349check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9350 struct symtabs_and_lines *sals)
9351{
9352 int i, rslt;
9353 struct symtab_and_line *sal;
9354 char *msg;
9355 struct cleanup *old_chain;
9356
9357 for (i = 0; i < sals->nelts; i++)
9358 {
f8eba3c6
TT
9359 struct gdbarch *sarch;
9360
7a697b8d
SS
9361 sal = &sals->sals[i];
9362
f8eba3c6
TT
9363 sarch = get_sal_arch (*sal);
9364 /* We fall back to GDBARCH if there is no architecture
9365 associated with SAL. */
9366 if (sarch == NULL)
9367 sarch = gdbarch;
9368 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9369 NULL, &msg);
9370 old_chain = make_cleanup (xfree, msg);
9371
9372 if (!rslt)
9373 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9374 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9375
9376 do_cleanups (old_chain);
9377 }
9378}
9379
af4908ba
KS
9380/* Issue an invalid thread ID error. */
9381
9382static void ATTRIBUTE_NORETURN
9383invalid_thread_id_error (int id)
9384{
9385 error (_("Unknown thread %d."), id);
9386}
9387
018d34a4
VP
9388/* Given TOK, a string specification of condition and thread, as
9389 accepted by the 'break' command, extract the condition
9390 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9391 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9392 If no condition is found, *COND_STRING is set to NULL.
9393 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9394
9395static void
bbc13ae3 9396find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9397 char **cond_string, int *thread, int *task,
9398 char **rest)
018d34a4
VP
9399{
9400 *cond_string = NULL;
9401 *thread = -1;
ed1d1739
KS
9402 *task = 0;
9403 *rest = NULL;
9404
018d34a4
VP
9405 while (tok && *tok)
9406 {
bbc13ae3 9407 const char *end_tok;
018d34a4 9408 int toklen;
bbc13ae3
KS
9409 const char *cond_start = NULL;
9410 const char *cond_end = NULL;
cc59ec59 9411
bbc13ae3 9412 tok = skip_spaces_const (tok);
e7e0cddf
SS
9413
9414 if ((*tok == '"' || *tok == ',') && rest)
9415 {
9416 *rest = savestring (tok, strlen (tok));
9417 return;
9418 }
9419
bbc13ae3 9420 end_tok = skip_to_space_const (tok);
d634f2de 9421
018d34a4 9422 toklen = end_tok - tok;
d634f2de 9423
018d34a4
VP
9424 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9425 {
f7545552
TT
9426 struct expression *expr;
9427
018d34a4 9428 tok = cond_start = end_tok + 1;
1bb9788d 9429 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9430 xfree (expr);
018d34a4 9431 cond_end = tok;
d634f2de 9432 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9433 }
9434 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9435 {
9436 char *tmptok;
d634f2de 9437
018d34a4 9438 tok = end_tok + 1;
bbc13ae3 9439 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9440 if (tok == tmptok)
9441 error (_("Junk after thread keyword."));
9442 if (!valid_thread_id (*thread))
af4908ba 9443 invalid_thread_id_error (*thread);
bbc13ae3 9444 tok = tmptok;
018d34a4 9445 }
4a306c9a
JB
9446 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9447 {
9448 char *tmptok;
9449
9450 tok = end_tok + 1;
bbc13ae3 9451 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9452 if (tok == tmptok)
9453 error (_("Junk after task keyword."));
9454 if (!valid_task_id (*task))
b6199126 9455 error (_("Unknown task %d."), *task);
bbc13ae3 9456 tok = tmptok;
4a306c9a 9457 }
e7e0cddf
SS
9458 else if (rest)
9459 {
9460 *rest = savestring (tok, strlen (tok));
ccab2054 9461 return;
e7e0cddf 9462 }
018d34a4
VP
9463 else
9464 error (_("Junk at end of arguments."));
9465 }
9466}
9467
0fb4aa4b
PA
9468/* Decode a static tracepoint marker spec. */
9469
9470static struct symtabs_and_lines
9471decode_static_tracepoint_spec (char **arg_p)
9472{
9473 VEC(static_tracepoint_marker_p) *markers = NULL;
9474 struct symtabs_and_lines sals;
0fb4aa4b
PA
9475 struct cleanup *old_chain;
9476 char *p = &(*arg_p)[3];
9477 char *endp;
9478 char *marker_str;
9479 int i;
9480
e9cafbcc 9481 p = skip_spaces (p);
0fb4aa4b 9482
e9cafbcc 9483 endp = skip_to_space (p);
0fb4aa4b
PA
9484
9485 marker_str = savestring (p, endp - p);
9486 old_chain = make_cleanup (xfree, marker_str);
9487
9488 markers = target_static_tracepoint_markers_by_strid (marker_str);
9489 if (VEC_empty(static_tracepoint_marker_p, markers))
9490 error (_("No known static tracepoint marker named %s"), marker_str);
9491
9492 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9493 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9494
9495 for (i = 0; i < sals.nelts; i++)
9496 {
9497 struct static_tracepoint_marker *marker;
9498
9499 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9500
9501 init_sal (&sals.sals[i]);
9502
9503 sals.sals[i] = find_pc_line (marker->address, 0);
9504 sals.sals[i].pc = marker->address;
9505
9506 release_static_tracepoint_marker (marker);
9507 }
9508
9509 do_cleanups (old_chain);
9510
9511 *arg_p = endp;
9512 return sals;
9513}
9514
fd9b8c24
PA
9515/* Set a breakpoint. This function is shared between CLI and MI
9516 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9517 modes of operations, selected by the PARSE_ARG parameter. If
9518 non-zero, the function will parse ARG, extracting location,
9519 condition, thread and extra string. Otherwise, ARG is just the
9520 breakpoint's location, with condition, thread, and extra string
9521 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9522 If INTERNAL is non-zero, the breakpoint number will be allocated
9523 from the internal breakpoint count. Returns true if any breakpoint
9524 was created; false otherwise. */
0101ce28 9525
8cdf0e15
VP
9526int
9527create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9528 char *arg, char *cond_string,
9529 int thread, char *extra_string,
f6de8ec2 9530 int parse_arg,
0fb4aa4b 9531 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9532 int ignore_count,
9533 enum auto_boolean pending_break_support,
c0a91b2b 9534 const struct breakpoint_ops *ops,
44f238bb
PA
9535 int from_tty, int enabled, int internal,
9536 unsigned flags)
c3f6f71d 9537{
b78a6381 9538 volatile struct gdb_exception e;
f8eba3c6 9539 char *copy_arg = NULL;
c3f6f71d 9540 char *addr_start = arg;
7efd8fc2 9541 struct linespec_result canonical;
c3f6f71d 9542 struct cleanup *old_chain;
80c99de1 9543 struct cleanup *bkpt_chain = NULL;
0101ce28 9544 int pending = 0;
4a306c9a 9545 int task = 0;
86b17b60 9546 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9547
348d480f
PA
9548 gdb_assert (ops != NULL);
9549
7efd8fc2 9550 init_linespec_result (&canonical);
c3f6f71d 9551
b78a6381
TT
9552 TRY_CATCH (e, RETURN_MASK_ALL)
9553 {
983af33b
SDJ
9554 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9555 addr_start, &copy_arg);
b78a6381 9556 }
0101ce28
JJ
9557
9558 /* If caller is interested in rc value from parse, set value. */
05ff989b 9559 switch (e.reason)
0101ce28 9560 {
983af33b
SDJ
9561 case GDB_NO_ERROR:
9562 if (VEC_empty (linespec_sals, canonical.sals))
9563 return 0;
9564 break;
05ff989b
AC
9565 case RETURN_ERROR:
9566 switch (e.error)
0101ce28 9567 {
05ff989b 9568 case NOT_FOUND_ERROR:
0101ce28 9569
05ff989b
AC
9570 /* If pending breakpoint support is turned off, throw
9571 error. */
fa8d40ab
JJ
9572
9573 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9574 throw_exception (e);
9575
9576 exception_print (gdb_stderr, e);
fa8d40ab 9577
05ff989b
AC
9578 /* If pending breakpoint support is auto query and the user
9579 selects no, then simply return the error code. */
059fb39f 9580 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9581 && !nquery (_("Make %s pending on future shared library load? "),
9582 bptype_string (type_wanted)))
fd9b8c24 9583 return 0;
fa8d40ab 9584
05ff989b
AC
9585 /* At this point, either the user was queried about setting
9586 a pending breakpoint and selected yes, or pending
9587 breakpoint behavior is on and thus a pending breakpoint
9588 is defaulted on behalf of the user. */
f8eba3c6
TT
9589 {
9590 struct linespec_sals lsal;
9591
9592 copy_arg = xstrdup (addr_start);
9593 lsal.canonical = xstrdup (copy_arg);
9594 lsal.sals.nelts = 1;
9595 lsal.sals.sals = XNEW (struct symtab_and_line);
9596 init_sal (&lsal.sals.sals[0]);
9597 pending = 1;
9598 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9599 }
05ff989b
AC
9600 break;
9601 default:
98deb0da 9602 throw_exception (e);
0101ce28 9603 }
2abae994 9604 break;
05ff989b 9605 default:
983af33b 9606 throw_exception (e);
0101ce28 9607 }
c3f6f71d 9608
4a64f543 9609 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9610 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9611
c3f6f71d
JM
9612 /* ----------------------------- SNIP -----------------------------
9613 Anything added to the cleanup chain beyond this point is assumed
9614 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9615 then the memory is not reclaimed. */
9616 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9617
c3f6f71d
JM
9618 /* Resolve all line numbers to PC's and verify that the addresses
9619 are ok for the target. */
0101ce28 9620 if (!pending)
f8eba3c6
TT
9621 {
9622 int ix;
9623 struct linespec_sals *iter;
9624
9625 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9626 breakpoint_sals_to_pc (&iter->sals);
9627 }
c3f6f71d 9628
7a697b8d 9629 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9630 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9631 {
9632 int ix;
9633 struct linespec_sals *iter;
9634
9635 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9636 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9637 }
7a697b8d 9638
c3f6f71d
JM
9639 /* Verify that condition can be parsed, before setting any
9640 breakpoints. Allocate a separate condition expression for each
4a64f543 9641 breakpoint. */
0101ce28 9642 if (!pending)
c3f6f71d 9643 {
f6de8ec2 9644 if (parse_arg)
72b2ff0e 9645 {
0878d0fa 9646 char *rest;
52d361e1
YQ
9647 struct linespec_sals *lsal;
9648
9649 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9650
0878d0fa
YQ
9651 /* Here we only parse 'arg' to separate condition
9652 from thread number, so parsing in context of first
9653 sal is OK. When setting the breakpoint we'll
9654 re-parse it in context of each sal. */
9655
9656 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9657 &thread, &task, &rest);
9658 if (cond_string)
9659 make_cleanup (xfree, cond_string);
9660 if (rest)
9661 make_cleanup (xfree, rest);
9662 if (rest)
9663 extra_string = rest;
72b2ff0e 9664 }
2f069f6f 9665 else
72b2ff0e 9666 {
0878d0fa
YQ
9667 if (*arg != '\0')
9668 error (_("Garbage '%s' at end of location"), arg);
9669
9670 /* Create a private copy of condition string. */
9671 if (cond_string)
9672 {
9673 cond_string = xstrdup (cond_string);
9674 make_cleanup (xfree, cond_string);
9675 }
9676 /* Create a private copy of any extra string. */
9677 if (extra_string)
9678 {
9679 extra_string = xstrdup (extra_string);
9680 make_cleanup (xfree, extra_string);
9681 }
72b2ff0e 9682 }
0fb4aa4b 9683
52d361e1 9684 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9685 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9686 tempflag ? disp_del : disp_donttouch,
9687 thread, task, ignore_count, ops,
44f238bb 9688 from_tty, enabled, internal, flags);
c906108c 9689 }
0101ce28
JJ
9690 else
9691 {
0101ce28
JJ
9692 struct breakpoint *b;
9693
0101ce28
JJ
9694 make_cleanup (xfree, copy_arg);
9695
bfccc43c
YQ
9696 if (is_tracepoint_type (type_wanted))
9697 {
9698 struct tracepoint *t;
9699
9700 t = XCNEW (struct tracepoint);
9701 b = &t->base;
9702 }
9703 else
9704 b = XNEW (struct breakpoint);
9705
9706 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9707
f8eba3c6 9708 b->addr_string = copy_arg;
f6de8ec2 9709 if (parse_arg)
e12c7713
MK
9710 b->cond_string = NULL;
9711 else
9712 {
9713 /* Create a private copy of condition string. */
9714 if (cond_string)
9715 {
9716 cond_string = xstrdup (cond_string);
9717 make_cleanup (xfree, cond_string);
9718 }
9719 b->cond_string = cond_string;
9720 }
e7e0cddf 9721 b->extra_string = NULL;
0101ce28 9722 b->ignore_count = ignore_count;
0101ce28 9723 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9724 b->condition_not_parsed = 1;
41447f92 9725 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9726 if ((type_wanted != bp_breakpoint
9727 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9728 b->pspace = current_program_space;
8bea4e01 9729
bfccc43c 9730 install_breakpoint (internal, b, 0);
0101ce28
JJ
9731 }
9732
f8eba3c6 9733 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9734 {
3e43a32a
MS
9735 warning (_("Multiple breakpoints were set.\nUse the "
9736 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9737 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9738 }
9739
80c99de1
PA
9740 /* That's it. Discard the cleanups for data inserted into the
9741 breakpoint. */
9742 discard_cleanups (bkpt_chain);
9743 /* But cleanup everything else. */
c3f6f71d 9744 do_cleanups (old_chain);
217dc9e2 9745
80c99de1 9746 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9747 update_global_location_list (1);
fd9b8c24
PA
9748
9749 return 1;
c3f6f71d 9750}
c906108c 9751
348d480f 9752/* Set a breakpoint.
72b2ff0e
VP
9753 ARG is a string describing breakpoint address,
9754 condition, and thread.
9755 FLAG specifies if a breakpoint is hardware on,
9756 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9757 and BP_TEMPFLAG. */
348d480f 9758
98deb0da 9759static void
72b2ff0e 9760break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9761{
72b2ff0e 9762 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9763 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9764 ? bp_hardware_breakpoint
9765 : bp_breakpoint);
55aa24fb
SDJ
9766 struct breakpoint_ops *ops;
9767 const char *arg_cp = arg;
9768
9769 /* Matching breakpoints on probes. */
9770 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9771 ops = &bkpt_probe_breakpoint_ops;
9772 else
9773 ops = &bkpt_breakpoint_ops;
c3f6f71d 9774
8cdf0e15
VP
9775 create_breakpoint (get_current_arch (),
9776 arg,
e7e0cddf 9777 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9778 tempflag, type_wanted,
8cdf0e15
VP
9779 0 /* Ignore count */,
9780 pending_break_support,
55aa24fb 9781 ops,
8cdf0e15 9782 from_tty,
84f4c1fe 9783 1 /* enabled */,
44f238bb
PA
9784 0 /* internal */,
9785 0);
c906108c
SS
9786}
9787
c906108c
SS
9788/* Helper function for break_command_1 and disassemble_command. */
9789
9790void
fba45db2 9791resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9792{
9793 CORE_ADDR pc;
9794
9795 if (sal->pc == 0 && sal->symtab != NULL)
9796 {
9797 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9798 error (_("No line %d in file \"%s\"."),
05cba821 9799 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9800 sal->pc = pc;
6a048695 9801
4a64f543
MS
9802 /* If this SAL corresponds to a breakpoint inserted using a line
9803 number, then skip the function prologue if necessary. */
6a048695 9804 if (sal->explicit_line)
059acae7 9805 skip_prologue_sal (sal);
c906108c
SS
9806 }
9807
9808 if (sal->section == 0 && sal->symtab != NULL)
9809 {
9810 struct blockvector *bv;
c5aa993b
JM
9811 struct block *b;
9812 struct symbol *sym;
c906108c 9813
801e3a5b 9814 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9815 if (bv != NULL)
9816 {
7f0df278 9817 sym = block_linkage_function (b);
c906108c
SS
9818 if (sym != NULL)
9819 {
9820 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 9821 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
9822 }
9823 else
9824 {
4a64f543
MS
9825 /* It really is worthwhile to have the section, so we'll
9826 just have to look harder. This case can be executed
9827 if we have line numbers but no functions (as can
9828 happen in assembly source). */
c906108c 9829
7cbd4a93 9830 struct bound_minimal_symbol msym;
6c95b8df
PA
9831 struct cleanup *old_chain = save_current_space_and_thread ();
9832
9833 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9834
9835 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9836 if (msym.minsym)
e27d198c 9837 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9838
9839 do_cleanups (old_chain);
c906108c
SS
9840 }
9841 }
9842 }
9843}
9844
9845void
fba45db2 9846break_command (char *arg, int from_tty)
c906108c 9847{
db107f19 9848 break_command_1 (arg, 0, from_tty);
c906108c
SS
9849}
9850
c906108c 9851void
fba45db2 9852tbreak_command (char *arg, int from_tty)
c906108c 9853{
db107f19 9854 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9855}
9856
c906108c 9857static void
fba45db2 9858hbreak_command (char *arg, int from_tty)
c906108c 9859{
db107f19 9860 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9861}
9862
9863static void
fba45db2 9864thbreak_command (char *arg, int from_tty)
c906108c 9865{
db107f19 9866 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9867}
9868
9869static void
fba45db2 9870stop_command (char *arg, int from_tty)
c906108c 9871{
a3f17187 9872 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9873Usage: stop in <function | address>\n\
a3f17187 9874 stop at <line>\n"));
c906108c
SS
9875}
9876
9877static void
fba45db2 9878stopin_command (char *arg, int from_tty)
c906108c
SS
9879{
9880 int badInput = 0;
9881
c5aa993b 9882 if (arg == (char *) NULL)
c906108c
SS
9883 badInput = 1;
9884 else if (*arg != '*')
9885 {
9886 char *argptr = arg;
9887 int hasColon = 0;
9888
4a64f543 9889 /* Look for a ':'. If this is a line number specification, then
53a5351d 9890 say it is bad, otherwise, it should be an address or
4a64f543 9891 function/method name. */
c906108c 9892 while (*argptr && !hasColon)
c5aa993b
JM
9893 {
9894 hasColon = (*argptr == ':');
9895 argptr++;
9896 }
c906108c
SS
9897
9898 if (hasColon)
c5aa993b 9899 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9900 else
c5aa993b 9901 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9902 }
9903
9904 if (badInput)
a3f17187 9905 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9906 else
db107f19 9907 break_command_1 (arg, 0, from_tty);
c906108c
SS
9908}
9909
9910static void
fba45db2 9911stopat_command (char *arg, int from_tty)
c906108c
SS
9912{
9913 int badInput = 0;
9914
c5aa993b 9915 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9916 badInput = 1;
9917 else
9918 {
9919 char *argptr = arg;
9920 int hasColon = 0;
9921
4a64f543
MS
9922 /* Look for a ':'. If there is a '::' then get out, otherwise
9923 it is probably a line number. */
c906108c 9924 while (*argptr && !hasColon)
c5aa993b
JM
9925 {
9926 hasColon = (*argptr == ':');
9927 argptr++;
9928 }
c906108c
SS
9929
9930 if (hasColon)
c5aa993b 9931 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9932 else
c5aa993b 9933 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9934 }
9935
9936 if (badInput)
a3f17187 9937 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9938 else
db107f19 9939 break_command_1 (arg, 0, from_tty);
c906108c
SS
9940}
9941
e7e0cddf
SS
9942/* The dynamic printf command is mostly like a regular breakpoint, but
9943 with a prewired command list consisting of a single output command,
9944 built from extra arguments supplied on the dprintf command
9945 line. */
9946
da821c7b 9947static void
e7e0cddf
SS
9948dprintf_command (char *arg, int from_tty)
9949{
9950 create_breakpoint (get_current_arch (),
9951 arg,
9952 NULL, 0, NULL, 1 /* parse arg */,
9953 0, bp_dprintf,
9954 0 /* Ignore count */,
9955 pending_break_support,
9956 &dprintf_breakpoint_ops,
9957 from_tty,
9958 1 /* enabled */,
9959 0 /* internal */,
9960 0);
9961}
9962
d3ce09f5
SS
9963static void
9964agent_printf_command (char *arg, int from_tty)
9965{
9966 error (_("May only run agent-printf on the target"));
9967}
9968
f1310107
TJB
9969/* Implement the "breakpoint_hit" breakpoint_ops method for
9970 ranged breakpoints. */
9971
9972static int
9973breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9974 struct address_space *aspace,
09ac7c10
TT
9975 CORE_ADDR bp_addr,
9976 const struct target_waitstatus *ws)
f1310107 9977{
09ac7c10 9978 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9979 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9980 return 0;
9981
f1310107
TJB
9982 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9983 bl->length, aspace, bp_addr);
9984}
9985
9986/* Implement the "resources_needed" breakpoint_ops method for
9987 ranged breakpoints. */
9988
9989static int
9990resources_needed_ranged_breakpoint (const struct bp_location *bl)
9991{
9992 return target_ranged_break_num_registers ();
9993}
9994
9995/* Implement the "print_it" breakpoint_ops method for
9996 ranged breakpoints. */
9997
9998static enum print_stop_action
348d480f 9999print_it_ranged_breakpoint (bpstat bs)
f1310107 10000{
348d480f 10001 struct breakpoint *b = bs->breakpoint_at;
f1310107 10002 struct bp_location *bl = b->loc;
79a45e25 10003 struct ui_out *uiout = current_uiout;
f1310107
TJB
10004
10005 gdb_assert (b->type == bp_hardware_breakpoint);
10006
10007 /* Ranged breakpoints have only one location. */
10008 gdb_assert (bl && bl->next == NULL);
10009
10010 annotate_breakpoint (b->number);
10011 if (b->disposition == disp_del)
10012 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10013 else
10014 ui_out_text (uiout, "\nRanged breakpoint ");
10015 if (ui_out_is_mi_like_p (uiout))
10016 {
10017 ui_out_field_string (uiout, "reason",
10018 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10019 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10020 }
10021 ui_out_field_int (uiout, "bkptno", b->number);
10022 ui_out_text (uiout, ", ");
10023
10024 return PRINT_SRC_AND_LOC;
10025}
10026
10027/* Implement the "print_one" breakpoint_ops method for
10028 ranged breakpoints. */
10029
10030static void
10031print_one_ranged_breakpoint (struct breakpoint *b,
10032 struct bp_location **last_loc)
10033{
10034 struct bp_location *bl = b->loc;
10035 struct value_print_options opts;
79a45e25 10036 struct ui_out *uiout = current_uiout;
f1310107
TJB
10037
10038 /* Ranged breakpoints have only one location. */
10039 gdb_assert (bl && bl->next == NULL);
10040
10041 get_user_print_options (&opts);
10042
10043 if (opts.addressprint)
10044 /* We don't print the address range here, it will be printed later
10045 by print_one_detail_ranged_breakpoint. */
10046 ui_out_field_skip (uiout, "addr");
10047 annotate_field (5);
10048 print_breakpoint_location (b, bl);
10049 *last_loc = bl;
10050}
10051
10052/* Implement the "print_one_detail" breakpoint_ops method for
10053 ranged breakpoints. */
10054
10055static void
10056print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10057 struct ui_out *uiout)
10058{
10059 CORE_ADDR address_start, address_end;
10060 struct bp_location *bl = b->loc;
f99d8bf4
PA
10061 struct ui_file *stb = mem_fileopen ();
10062 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10063
10064 gdb_assert (bl);
10065
10066 address_start = bl->address;
10067 address_end = address_start + bl->length - 1;
10068
10069 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10070 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10071 print_core_address (bl->gdbarch, address_start),
10072 print_core_address (bl->gdbarch, address_end));
10073 ui_out_field_stream (uiout, "addr", stb);
10074 ui_out_text (uiout, "\n");
10075
10076 do_cleanups (cleanup);
10077}
10078
10079/* Implement the "print_mention" breakpoint_ops method for
10080 ranged breakpoints. */
10081
10082static void
10083print_mention_ranged_breakpoint (struct breakpoint *b)
10084{
10085 struct bp_location *bl = b->loc;
79a45e25 10086 struct ui_out *uiout = current_uiout;
f1310107
TJB
10087
10088 gdb_assert (bl);
10089 gdb_assert (b->type == bp_hardware_breakpoint);
10090
10091 if (ui_out_is_mi_like_p (uiout))
10092 return;
10093
10094 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10095 b->number, paddress (bl->gdbarch, bl->address),
10096 paddress (bl->gdbarch, bl->address + bl->length - 1));
10097}
10098
10099/* Implement the "print_recreate" breakpoint_ops method for
10100 ranged breakpoints. */
10101
10102static void
10103print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10104{
10105 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10106 b->addr_string_range_end);
d9b3f62e 10107 print_recreate_thread (b, fp);
f1310107
TJB
10108}
10109
10110/* The breakpoint_ops structure to be used in ranged breakpoints. */
10111
2060206e 10112static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10113
10114/* Find the address where the end of the breakpoint range should be
10115 placed, given the SAL of the end of the range. This is so that if
10116 the user provides a line number, the end of the range is set to the
10117 last instruction of the given line. */
10118
10119static CORE_ADDR
10120find_breakpoint_range_end (struct symtab_and_line sal)
10121{
10122 CORE_ADDR end;
10123
10124 /* If the user provided a PC value, use it. Otherwise,
10125 find the address of the end of the given location. */
10126 if (sal.explicit_pc)
10127 end = sal.pc;
10128 else
10129 {
10130 int ret;
10131 CORE_ADDR start;
10132
10133 ret = find_line_pc_range (sal, &start, &end);
10134 if (!ret)
10135 error (_("Could not find location of the end of the range."));
10136
10137 /* find_line_pc_range returns the start of the next line. */
10138 end--;
10139 }
10140
10141 return end;
10142}
10143
10144/* Implement the "break-range" CLI command. */
10145
10146static void
10147break_range_command (char *arg, int from_tty)
10148{
10149 char *arg_start, *addr_string_start, *addr_string_end;
10150 struct linespec_result canonical_start, canonical_end;
10151 int bp_count, can_use_bp, length;
10152 CORE_ADDR end;
10153 struct breakpoint *b;
10154 struct symtab_and_line sal_start, sal_end;
f1310107 10155 struct cleanup *cleanup_bkpt;
f8eba3c6 10156 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10157
10158 /* We don't support software ranged breakpoints. */
10159 if (target_ranged_break_num_registers () < 0)
10160 error (_("This target does not support hardware ranged breakpoints."));
10161
10162 bp_count = hw_breakpoint_used_count ();
10163 bp_count += target_ranged_break_num_registers ();
10164 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10165 bp_count, 0);
10166 if (can_use_bp < 0)
10167 error (_("Hardware breakpoints used exceeds limit."));
10168
f8eba3c6 10169 arg = skip_spaces (arg);
f1310107
TJB
10170 if (arg == NULL || arg[0] == '\0')
10171 error(_("No address range specified."));
10172
f1310107
TJB
10173 init_linespec_result (&canonical_start);
10174
f8eba3c6
TT
10175 arg_start = arg;
10176 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10177
f8eba3c6 10178 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10179
10180 if (arg[0] != ',')
10181 error (_("Too few arguments."));
f8eba3c6 10182 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10183 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10184
10185 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10186
10187 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10188 || lsal_start->sals.nelts != 1)
f1310107
TJB
10189 error (_("Cannot create a ranged breakpoint with multiple locations."));
10190
f8eba3c6
TT
10191 sal_start = lsal_start->sals.sals[0];
10192 addr_string_start = savestring (arg_start, arg - arg_start);
10193 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10194
10195 arg++; /* Skip the comma. */
f8eba3c6 10196 arg = skip_spaces (arg);
f1310107
TJB
10197
10198 /* Parse the end location. */
10199
f1310107
TJB
10200 init_linespec_result (&canonical_end);
10201 arg_start = arg;
10202
f8eba3c6 10203 /* We call decode_line_full directly here instead of using
f1310107
TJB
10204 parse_breakpoint_sals because we need to specify the start location's
10205 symtab and line as the default symtab and line for the end of the
10206 range. This makes it possible to have ranges like "foo.c:27, +14",
10207 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10208 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10209 sal_start.symtab, sal_start.line,
10210 &canonical_end, NULL, NULL);
10211
10212 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10213
f8eba3c6 10214 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10215 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10216
10217 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10218 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10219 || lsal_end->sals.nelts != 1)
f1310107
TJB
10220 error (_("Cannot create a ranged breakpoint with multiple locations."));
10221
f8eba3c6
TT
10222 sal_end = lsal_end->sals.sals[0];
10223 addr_string_end = savestring (arg_start, arg - arg_start);
10224 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10225
10226 end = find_breakpoint_range_end (sal_end);
10227 if (sal_start.pc > end)
177b42fe 10228 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10229
10230 length = end - sal_start.pc + 1;
10231 if (length < 0)
10232 /* Length overflowed. */
10233 error (_("Address range too large."));
10234 else if (length == 1)
10235 {
10236 /* This range is simple enough to be handled by
10237 the `hbreak' command. */
10238 hbreak_command (addr_string_start, 1);
10239
10240 do_cleanups (cleanup_bkpt);
10241
10242 return;
10243 }
10244
10245 /* Now set up the breakpoint. */
10246 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10247 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10248 set_breakpoint_count (breakpoint_count + 1);
10249 b->number = breakpoint_count;
10250 b->disposition = disp_donttouch;
f8eba3c6
TT
10251 b->addr_string = xstrdup (addr_string_start);
10252 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10253 b->loc->length = length;
10254
f8eba3c6 10255 do_cleanups (cleanup_bkpt);
f1310107
TJB
10256
10257 mention (b);
8d3788bd 10258 observer_notify_breakpoint_created (b);
f1310107
TJB
10259 update_global_location_list (1);
10260}
10261
4a64f543
MS
10262/* Return non-zero if EXP is verified as constant. Returned zero
10263 means EXP is variable. Also the constant detection may fail for
10264 some constant expressions and in such case still falsely return
10265 zero. */
2e6e3d9c 10266
65d79d4b
SDJ
10267static int
10268watchpoint_exp_is_const (const struct expression *exp)
10269{
10270 int i = exp->nelts;
10271
10272 while (i > 0)
10273 {
10274 int oplenp, argsp;
10275
10276 /* We are only interested in the descriptor of each element. */
10277 operator_length (exp, i, &oplenp, &argsp);
10278 i -= oplenp;
10279
10280 switch (exp->elts[i].opcode)
10281 {
10282 case BINOP_ADD:
10283 case BINOP_SUB:
10284 case BINOP_MUL:
10285 case BINOP_DIV:
10286 case BINOP_REM:
10287 case BINOP_MOD:
10288 case BINOP_LSH:
10289 case BINOP_RSH:
10290 case BINOP_LOGICAL_AND:
10291 case BINOP_LOGICAL_OR:
10292 case BINOP_BITWISE_AND:
10293 case BINOP_BITWISE_IOR:
10294 case BINOP_BITWISE_XOR:
10295 case BINOP_EQUAL:
10296 case BINOP_NOTEQUAL:
10297 case BINOP_LESS:
10298 case BINOP_GTR:
10299 case BINOP_LEQ:
10300 case BINOP_GEQ:
10301 case BINOP_REPEAT:
10302 case BINOP_COMMA:
10303 case BINOP_EXP:
10304 case BINOP_MIN:
10305 case BINOP_MAX:
10306 case BINOP_INTDIV:
10307 case BINOP_CONCAT:
10308 case BINOP_IN:
10309 case BINOP_RANGE:
10310 case TERNOP_COND:
10311 case TERNOP_SLICE:
65d79d4b
SDJ
10312
10313 case OP_LONG:
10314 case OP_DOUBLE:
10315 case OP_DECFLOAT:
10316 case OP_LAST:
10317 case OP_COMPLEX:
10318 case OP_STRING:
65d79d4b
SDJ
10319 case OP_ARRAY:
10320 case OP_TYPE:
608b4967
TT
10321 case OP_TYPEOF:
10322 case OP_DECLTYPE:
6e72ca20 10323 case OP_TYPEID:
65d79d4b
SDJ
10324 case OP_NAME:
10325 case OP_OBJC_NSSTRING:
10326
10327 case UNOP_NEG:
10328 case UNOP_LOGICAL_NOT:
10329 case UNOP_COMPLEMENT:
10330 case UNOP_ADDR:
10331 case UNOP_HIGH:
aeaa2474 10332 case UNOP_CAST:
9eaf6705
TT
10333
10334 case UNOP_CAST_TYPE:
10335 case UNOP_REINTERPRET_CAST:
10336 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10337 /* Unary, binary and ternary operators: We have to check
10338 their operands. If they are constant, then so is the
10339 result of that operation. For instance, if A and B are
10340 determined to be constants, then so is "A + B".
10341
10342 UNOP_IND is one exception to the rule above, because the
10343 value of *ADDR is not necessarily a constant, even when
10344 ADDR is. */
65d79d4b
SDJ
10345 break;
10346
10347 case OP_VAR_VALUE:
10348 /* Check whether the associated symbol is a constant.
4a64f543 10349
65d79d4b 10350 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10351 possible that a buggy compiler could mark a variable as
10352 constant even when it is not, and TYPE_CONST would return
10353 true in this case, while SYMBOL_CLASS wouldn't.
10354
10355 We also have to check for function symbols because they
10356 are always constant. */
65d79d4b
SDJ
10357 {
10358 struct symbol *s = exp->elts[i + 2].symbol;
10359
10360 if (SYMBOL_CLASS (s) != LOC_BLOCK
10361 && SYMBOL_CLASS (s) != LOC_CONST
10362 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10363 return 0;
10364 break;
10365 }
10366
10367 /* The default action is to return 0 because we are using
10368 the optimistic approach here: If we don't know something,
10369 then it is not a constant. */
10370 default:
10371 return 0;
10372 }
10373 }
10374
10375 return 1;
10376}
10377
3a5c3e22
PA
10378/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10379
10380static void
10381dtor_watchpoint (struct breakpoint *self)
10382{
10383 struct watchpoint *w = (struct watchpoint *) self;
10384
10385 xfree (w->cond_exp);
10386 xfree (w->exp);
10387 xfree (w->exp_string);
10388 xfree (w->exp_string_reparse);
10389 value_free (w->val);
10390
10391 base_breakpoint_ops.dtor (self);
10392}
10393
348d480f
PA
10394/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10395
10396static void
10397re_set_watchpoint (struct breakpoint *b)
10398{
3a5c3e22
PA
10399 struct watchpoint *w = (struct watchpoint *) b;
10400
348d480f
PA
10401 /* Watchpoint can be either on expression using entirely global
10402 variables, or it can be on local variables.
10403
10404 Watchpoints of the first kind are never auto-deleted, and even
10405 persist across program restarts. Since they can use variables
10406 from shared libraries, we need to reparse expression as libraries
10407 are loaded and unloaded.
10408
10409 Watchpoints on local variables can also change meaning as result
10410 of solib event. For example, if a watchpoint uses both a local
10411 and a global variables in expression, it's a local watchpoint,
10412 but unloading of a shared library will make the expression
10413 invalid. This is not a very common use case, but we still
10414 re-evaluate expression, to avoid surprises to the user.
10415
10416 Note that for local watchpoints, we re-evaluate it only if
10417 watchpoints frame id is still valid. If it's not, it means the
10418 watchpoint is out of scope and will be deleted soon. In fact,
10419 I'm not sure we'll ever be called in this case.
10420
10421 If a local watchpoint's frame id is still valid, then
3a5c3e22 10422 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10423
3a5c3e22
PA
10424 Don't do anything about disabled watchpoints, since they will be
10425 reevaluated again when enabled. */
10426 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10427}
10428
77b06cd7
TJB
10429/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10430
10431static int
10432insert_watchpoint (struct bp_location *bl)
10433{
3a5c3e22
PA
10434 struct watchpoint *w = (struct watchpoint *) bl->owner;
10435 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10436
10437 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10438 w->cond_exp);
77b06cd7
TJB
10439}
10440
10441/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10442
10443static int
10444remove_watchpoint (struct bp_location *bl)
10445{
3a5c3e22
PA
10446 struct watchpoint *w = (struct watchpoint *) bl->owner;
10447 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10448
10449 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10450 w->cond_exp);
e09342b5
TJB
10451}
10452
e09342b5 10453static int
348d480f 10454breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10455 struct address_space *aspace, CORE_ADDR bp_addr,
10456 const struct target_waitstatus *ws)
e09342b5 10457{
348d480f 10458 struct breakpoint *b = bl->owner;
3a5c3e22 10459 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10460
348d480f
PA
10461 /* Continuable hardware watchpoints are treated as non-existent if the
10462 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10463 some data address). Otherwise gdb won't stop on a break instruction
10464 in the code (not from a breakpoint) when a hardware watchpoint has
10465 been defined. Also skip watchpoints which we know did not trigger
10466 (did not match the data address). */
10467 if (is_hardware_watchpoint (b)
3a5c3e22 10468 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10469 return 0;
9c06b0b4 10470
348d480f 10471 return 1;
9c06b0b4
TJB
10472}
10473
348d480f
PA
10474static void
10475check_status_watchpoint (bpstat bs)
9c06b0b4 10476{
348d480f 10477 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10478
348d480f 10479 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10480}
10481
10482/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10483 hardware watchpoints. */
9c06b0b4
TJB
10484
10485static int
348d480f 10486resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10487{
3a5c3e22
PA
10488 struct watchpoint *w = (struct watchpoint *) bl->owner;
10489 int length = w->exact? 1 : bl->length;
348d480f
PA
10490
10491 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10492}
10493
10494/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10495 hardware watchpoints. */
9c06b0b4
TJB
10496
10497static int
348d480f 10498works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10499{
efa80663
PA
10500 /* Read and access watchpoints only work with hardware support. */
10501 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10502}
10503
9c06b0b4 10504static enum print_stop_action
348d480f 10505print_it_watchpoint (bpstat bs)
9c06b0b4 10506{
348d480f
PA
10507 struct cleanup *old_chain;
10508 struct breakpoint *b;
f99d8bf4 10509 struct ui_file *stb;
348d480f 10510 enum print_stop_action result;
3a5c3e22 10511 struct watchpoint *w;
79a45e25 10512 struct ui_out *uiout = current_uiout;
348d480f
PA
10513
10514 gdb_assert (bs->bp_location_at != NULL);
10515
348d480f 10516 b = bs->breakpoint_at;
3a5c3e22 10517 w = (struct watchpoint *) b;
348d480f 10518
f99d8bf4
PA
10519 stb = mem_fileopen ();
10520 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10521
10522 switch (b->type)
10523 {
348d480f 10524 case bp_watchpoint:
9c06b0b4
TJB
10525 case bp_hardware_watchpoint:
10526 annotate_watchpoint (b->number);
10527 if (ui_out_is_mi_like_p (uiout))
10528 ui_out_field_string
10529 (uiout, "reason",
10530 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10531 mention (b);
10532 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10533 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10534 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10535 ui_out_field_stream (uiout, "old", stb);
10536 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10537 watchpoint_value_print (w->val, stb);
348d480f
PA
10538 ui_out_field_stream (uiout, "new", stb);
10539 ui_out_text (uiout, "\n");
10540 /* More than one watchpoint may have been triggered. */
10541 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10542 break;
10543
10544 case bp_read_watchpoint:
10545 if (ui_out_is_mi_like_p (uiout))
10546 ui_out_field_string
10547 (uiout, "reason",
10548 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10549 mention (b);
10550 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10551 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10552 watchpoint_value_print (w->val, stb);
348d480f
PA
10553 ui_out_field_stream (uiout, "value", stb);
10554 ui_out_text (uiout, "\n");
10555 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10556 break;
10557
10558 case bp_access_watchpoint:
348d480f
PA
10559 if (bs->old_val != NULL)
10560 {
10561 annotate_watchpoint (b->number);
10562 if (ui_out_is_mi_like_p (uiout))
10563 ui_out_field_string
10564 (uiout, "reason",
10565 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10566 mention (b);
10567 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10568 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10569 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10570 ui_out_field_stream (uiout, "old", stb);
10571 ui_out_text (uiout, "\nNew value = ");
10572 }
10573 else
10574 {
10575 mention (b);
10576 if (ui_out_is_mi_like_p (uiout))
10577 ui_out_field_string
10578 (uiout, "reason",
10579 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10580 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10581 ui_out_text (uiout, "\nValue = ");
10582 }
f99d8bf4 10583 watchpoint_value_print (w->val, stb);
348d480f
PA
10584 ui_out_field_stream (uiout, "new", stb);
10585 ui_out_text (uiout, "\n");
10586 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10587 break;
10588 default:
348d480f 10589 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10590 }
10591
348d480f
PA
10592 do_cleanups (old_chain);
10593 return result;
10594}
10595
10596/* Implement the "print_mention" breakpoint_ops method for hardware
10597 watchpoints. */
10598
10599static void
10600print_mention_watchpoint (struct breakpoint *b)
10601{
10602 struct cleanup *ui_out_chain;
3a5c3e22 10603 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10604 struct ui_out *uiout = current_uiout;
348d480f
PA
10605
10606 switch (b->type)
10607 {
10608 case bp_watchpoint:
10609 ui_out_text (uiout, "Watchpoint ");
10610 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10611 break;
10612 case bp_hardware_watchpoint:
10613 ui_out_text (uiout, "Hardware watchpoint ");
10614 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10615 break;
10616 case bp_read_watchpoint:
10617 ui_out_text (uiout, "Hardware read watchpoint ");
10618 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10619 break;
10620 case bp_access_watchpoint:
10621 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10622 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10623 break;
10624 default:
10625 internal_error (__FILE__, __LINE__,
10626 _("Invalid hardware watchpoint type."));
10627 }
10628
10629 ui_out_field_int (uiout, "number", b->number);
10630 ui_out_text (uiout, ": ");
3a5c3e22 10631 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10632 do_cleanups (ui_out_chain);
10633}
10634
10635/* Implement the "print_recreate" breakpoint_ops method for
10636 watchpoints. */
10637
10638static void
10639print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10640{
3a5c3e22
PA
10641 struct watchpoint *w = (struct watchpoint *) b;
10642
348d480f
PA
10643 switch (b->type)
10644 {
10645 case bp_watchpoint:
10646 case bp_hardware_watchpoint:
10647 fprintf_unfiltered (fp, "watch");
10648 break;
10649 case bp_read_watchpoint:
10650 fprintf_unfiltered (fp, "rwatch");
10651 break;
10652 case bp_access_watchpoint:
10653 fprintf_unfiltered (fp, "awatch");
10654 break;
10655 default:
10656 internal_error (__FILE__, __LINE__,
10657 _("Invalid watchpoint type."));
10658 }
10659
3a5c3e22 10660 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10661 print_recreate_thread (b, fp);
348d480f
PA
10662}
10663
427cd150
TT
10664/* Implement the "explains_signal" breakpoint_ops method for
10665 watchpoints. */
10666
10667static enum bpstat_signal_value
10668explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10669{
10670 /* A software watchpoint cannot cause a signal other than
10671 GDB_SIGNAL_TRAP. */
10672 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10673 return BPSTAT_SIGNAL_NO;
10674
10675 return BPSTAT_SIGNAL_HIDE;
10676}
10677
348d480f
PA
10678/* The breakpoint_ops structure to be used in hardware watchpoints. */
10679
2060206e 10680static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10681
10682/* Implement the "insert" breakpoint_ops method for
10683 masked hardware watchpoints. */
10684
10685static int
10686insert_masked_watchpoint (struct bp_location *bl)
10687{
3a5c3e22
PA
10688 struct watchpoint *w = (struct watchpoint *) bl->owner;
10689
10690 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10691 bl->watchpoint_type);
10692}
10693
10694/* Implement the "remove" breakpoint_ops method for
10695 masked hardware watchpoints. */
10696
10697static int
10698remove_masked_watchpoint (struct bp_location *bl)
10699{
3a5c3e22
PA
10700 struct watchpoint *w = (struct watchpoint *) bl->owner;
10701
10702 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10703 bl->watchpoint_type);
10704}
10705
10706/* Implement the "resources_needed" breakpoint_ops method for
10707 masked hardware watchpoints. */
10708
10709static int
10710resources_needed_masked_watchpoint (const struct bp_location *bl)
10711{
3a5c3e22
PA
10712 struct watchpoint *w = (struct watchpoint *) bl->owner;
10713
10714 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10715}
10716
10717/* Implement the "works_in_software_mode" breakpoint_ops method for
10718 masked hardware watchpoints. */
10719
10720static int
10721works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10722{
10723 return 0;
10724}
10725
10726/* Implement the "print_it" breakpoint_ops method for
10727 masked hardware watchpoints. */
10728
10729static enum print_stop_action
10730print_it_masked_watchpoint (bpstat bs)
10731{
10732 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10733 struct ui_out *uiout = current_uiout;
348d480f
PA
10734
10735 /* Masked watchpoints have only one location. */
10736 gdb_assert (b->loc && b->loc->next == NULL);
10737
10738 switch (b->type)
10739 {
10740 case bp_hardware_watchpoint:
10741 annotate_watchpoint (b->number);
10742 if (ui_out_is_mi_like_p (uiout))
10743 ui_out_field_string
10744 (uiout, "reason",
10745 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10746 break;
10747
10748 case bp_read_watchpoint:
10749 if (ui_out_is_mi_like_p (uiout))
10750 ui_out_field_string
10751 (uiout, "reason",
10752 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10753 break;
10754
10755 case bp_access_watchpoint:
10756 if (ui_out_is_mi_like_p (uiout))
10757 ui_out_field_string
10758 (uiout, "reason",
10759 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10760 break;
10761 default:
10762 internal_error (__FILE__, __LINE__,
10763 _("Invalid hardware watchpoint type."));
10764 }
10765
10766 mention (b);
9c06b0b4
TJB
10767 ui_out_text (uiout, _("\n\
10768Check the underlying instruction at PC for the memory\n\
10769address and value which triggered this watchpoint.\n"));
10770 ui_out_text (uiout, "\n");
10771
10772 /* More than one watchpoint may have been triggered. */
10773 return PRINT_UNKNOWN;
10774}
10775
10776/* Implement the "print_one_detail" breakpoint_ops method for
10777 masked hardware watchpoints. */
10778
10779static void
10780print_one_detail_masked_watchpoint (const struct breakpoint *b,
10781 struct ui_out *uiout)
10782{
3a5c3e22
PA
10783 struct watchpoint *w = (struct watchpoint *) b;
10784
9c06b0b4
TJB
10785 /* Masked watchpoints have only one location. */
10786 gdb_assert (b->loc && b->loc->next == NULL);
10787
10788 ui_out_text (uiout, "\tmask ");
3a5c3e22 10789 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10790 ui_out_text (uiout, "\n");
10791}
10792
10793/* Implement the "print_mention" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796static void
10797print_mention_masked_watchpoint (struct breakpoint *b)
10798{
3a5c3e22 10799 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10800 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10801 struct cleanup *ui_out_chain;
10802
10803 switch (b->type)
10804 {
10805 case bp_hardware_watchpoint:
10806 ui_out_text (uiout, "Masked hardware watchpoint ");
10807 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10808 break;
10809 case bp_read_watchpoint:
10810 ui_out_text (uiout, "Masked hardware read watchpoint ");
10811 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10812 break;
10813 case bp_access_watchpoint:
10814 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10815 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10816 break;
10817 default:
10818 internal_error (__FILE__, __LINE__,
10819 _("Invalid hardware watchpoint type."));
10820 }
10821
10822 ui_out_field_int (uiout, "number", b->number);
10823 ui_out_text (uiout, ": ");
3a5c3e22 10824 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10825 do_cleanups (ui_out_chain);
10826}
10827
10828/* Implement the "print_recreate" breakpoint_ops method for
10829 masked hardware watchpoints. */
10830
10831static void
10832print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10833{
3a5c3e22 10834 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10835 char tmp[40];
10836
10837 switch (b->type)
10838 {
10839 case bp_hardware_watchpoint:
10840 fprintf_unfiltered (fp, "watch");
10841 break;
10842 case bp_read_watchpoint:
10843 fprintf_unfiltered (fp, "rwatch");
10844 break;
10845 case bp_access_watchpoint:
10846 fprintf_unfiltered (fp, "awatch");
10847 break;
10848 default:
10849 internal_error (__FILE__, __LINE__,
10850 _("Invalid hardware watchpoint type."));
10851 }
10852
3a5c3e22
PA
10853 sprintf_vma (tmp, w->hw_wp_mask);
10854 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10855 print_recreate_thread (b, fp);
9c06b0b4
TJB
10856}
10857
10858/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10859
2060206e 10860static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10861
10862/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10863
10864static int
10865is_masked_watchpoint (const struct breakpoint *b)
10866{
10867 return b->ops == &masked_watchpoint_breakpoint_ops;
10868}
10869
53a5351d
JM
10870/* accessflag: hw_write: watch write,
10871 hw_read: watch read,
10872 hw_access: watch access (read or write) */
c906108c 10873static void
bbc13ae3 10874watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10875 int just_location, int internal)
c906108c 10876{
a9634178 10877 volatile struct gdb_exception e;
d983da9c 10878 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10879 struct expression *exp;
270140bd 10880 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10881 struct value *val, *mark, *result;
c906108c 10882 struct frame_info *frame;
bbc13ae3
KS
10883 const char *exp_start = NULL;
10884 const char *exp_end = NULL;
10885 const char *tok, *end_tok;
9c06b0b4 10886 int toklen = -1;
bbc13ae3
KS
10887 const char *cond_start = NULL;
10888 const char *cond_end = NULL;
c906108c 10889 enum bptype bp_type;
37e4754d 10890 int thread = -1;
0cf6dd15 10891 int pc = 0;
9c06b0b4
TJB
10892 /* Flag to indicate whether we are going to use masks for
10893 the hardware watchpoint. */
10894 int use_mask = 0;
10895 CORE_ADDR mask = 0;
3a5c3e22 10896 struct watchpoint *w;
bbc13ae3
KS
10897 char *expression;
10898 struct cleanup *back_to;
c906108c 10899
37e4754d
LM
10900 /* Make sure that we actually have parameters to parse. */
10901 if (arg != NULL && arg[0] != '\0')
10902 {
bbc13ae3
KS
10903 const char *value_start;
10904
10905 exp_end = arg + strlen (arg);
37e4754d 10906
9c06b0b4
TJB
10907 /* Look for "parameter value" pairs at the end
10908 of the arguments string. */
bbc13ae3 10909 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10910 {
10911 /* Skip whitespace at the end of the argument list. */
10912 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10913 tok--;
10914
10915 /* Find the beginning of the last token.
10916 This is the value of the parameter. */
10917 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10918 tok--;
10919 value_start = tok + 1;
10920
10921 /* Skip whitespace. */
10922 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10923 tok--;
10924
10925 end_tok = tok;
10926
10927 /* Find the beginning of the second to last token.
10928 This is the parameter itself. */
10929 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10930 tok--;
10931 tok++;
10932 toklen = end_tok - tok + 1;
10933
10934 if (toklen == 6 && !strncmp (tok, "thread", 6))
10935 {
10936 /* At this point we've found a "thread" token, which means
10937 the user is trying to set a watchpoint that triggers
10938 only in a specific thread. */
10939 char *endp;
37e4754d 10940
9c06b0b4
TJB
10941 if (thread != -1)
10942 error(_("You can specify only one thread."));
37e4754d 10943
9c06b0b4
TJB
10944 /* Extract the thread ID from the next token. */
10945 thread = strtol (value_start, &endp, 0);
37e4754d 10946
9c06b0b4
TJB
10947 /* Check if the user provided a valid numeric value for the
10948 thread ID. */
10949 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10950 error (_("Invalid thread ID specification %s."), value_start);
10951
10952 /* Check if the thread actually exists. */
10953 if (!valid_thread_id (thread))
af4908ba 10954 invalid_thread_id_error (thread);
9c06b0b4
TJB
10955 }
10956 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10957 {
10958 /* We've found a "mask" token, which means the user wants to
10959 create a hardware watchpoint that is going to have the mask
10960 facility. */
10961 struct value *mask_value, *mark;
37e4754d 10962
9c06b0b4
TJB
10963 if (use_mask)
10964 error(_("You can specify only one mask."));
37e4754d 10965
9c06b0b4 10966 use_mask = just_location = 1;
37e4754d 10967
9c06b0b4
TJB
10968 mark = value_mark ();
10969 mask_value = parse_to_comma_and_eval (&value_start);
10970 mask = value_as_address (mask_value);
10971 value_free_to_mark (mark);
10972 }
10973 else
10974 /* We didn't recognize what we found. We should stop here. */
10975 break;
37e4754d 10976
9c06b0b4
TJB
10977 /* Truncate the string and get rid of the "parameter value" pair before
10978 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10979 exp_end = tok;
9c06b0b4 10980 }
37e4754d 10981 }
bbc13ae3
KS
10982 else
10983 exp_end = arg;
37e4754d 10984
bbc13ae3
KS
10985 /* Parse the rest of the arguments. From here on out, everything
10986 is in terms of a newly allocated string instead of the original
10987 ARG. */
c906108c 10988 innermost_block = NULL;
bbc13ae3
KS
10989 expression = savestring (arg, exp_end - arg);
10990 back_to = make_cleanup (xfree, expression);
10991 exp_start = arg = expression;
1bb9788d 10992 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10993 exp_end = arg;
fa8a61dc
TT
10994 /* Remove trailing whitespace from the expression before saving it.
10995 This makes the eventual display of the expression string a bit
10996 prettier. */
10997 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10998 --exp_end;
10999
65d79d4b
SDJ
11000 /* Checking if the expression is not constant. */
11001 if (watchpoint_exp_is_const (exp))
11002 {
11003 int len;
11004
11005 len = exp_end - exp_start;
11006 while (len > 0 && isspace (exp_start[len - 1]))
11007 len--;
11008 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11009 }
11010
c906108c
SS
11011 exp_valid_block = innermost_block;
11012 mark = value_mark ();
3a1115a0 11013 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11014
11015 if (just_location)
11016 {
9c06b0b4
TJB
11017 int ret;
11018
06a64a0b 11019 exp_valid_block = NULL;
a1442452 11020 val = value_addr (result);
06a64a0b
TT
11021 release_value (val);
11022 value_free_to_mark (mark);
9c06b0b4
TJB
11023
11024 if (use_mask)
11025 {
11026 ret = target_masked_watch_num_registers (value_as_address (val),
11027 mask);
11028 if (ret == -1)
11029 error (_("This target does not support masked watchpoints."));
11030 else if (ret == -2)
11031 error (_("Invalid mask or memory region."));
11032 }
06a64a0b
TT
11033 }
11034 else if (val != NULL)
fa4727a6 11035 release_value (val);
c906108c 11036
bbc13ae3
KS
11037 tok = skip_spaces_const (arg);
11038 end_tok = skip_to_space_const (tok);
c906108c
SS
11039
11040 toklen = end_tok - tok;
11041 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11042 {
2d134ed3
PA
11043 struct expression *cond;
11044
60e1c644 11045 innermost_block = NULL;
c906108c 11046 tok = cond_start = end_tok + 1;
1bb9788d 11047 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11048
11049 /* The watchpoint expression may not be local, but the condition
11050 may still be. E.g.: `watch global if local > 0'. */
11051 cond_exp_valid_block = innermost_block;
11052
2d134ed3 11053 xfree (cond);
c906108c
SS
11054 cond_end = tok;
11055 }
11056 if (*tok)
8a3fe4f8 11057 error (_("Junk at end of command."));
c906108c 11058
53a5351d 11059 if (accessflag == hw_read)
c5aa993b 11060 bp_type = bp_read_watchpoint;
53a5351d 11061 else if (accessflag == hw_access)
c5aa993b
JM
11062 bp_type = bp_access_watchpoint;
11063 else
11064 bp_type = bp_hardware_watchpoint;
c906108c 11065
d983da9c 11066 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11067
11068 /* If the expression is "local", then set up a "watchpoint scope"
11069 breakpoint at the point where we've left the scope of the watchpoint
11070 expression. Create the scope breakpoint before the watchpoint, so
11071 that we will encounter it first in bpstat_stop_status. */
60e1c644 11072 if (exp_valid_block && frame)
d983da9c 11073 {
edb3359d
DJ
11074 if (frame_id_p (frame_unwind_caller_id (frame)))
11075 {
11076 scope_breakpoint
a6d9a66e
UW
11077 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11078 frame_unwind_caller_pc (frame),
06edf0c0
PA
11079 bp_watchpoint_scope,
11080 &momentary_breakpoint_ops);
d983da9c 11081
edb3359d 11082 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11083
edb3359d
DJ
11084 /* Automatically delete the breakpoint when it hits. */
11085 scope_breakpoint->disposition = disp_del;
d983da9c 11086
edb3359d
DJ
11087 /* Only break in the proper frame (help with recursion). */
11088 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11089
edb3359d 11090 /* Set the address at which we will stop. */
a6d9a66e
UW
11091 scope_breakpoint->loc->gdbarch
11092 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11093 scope_breakpoint->loc->requested_address
11094 = frame_unwind_caller_pc (frame);
11095 scope_breakpoint->loc->address
a6d9a66e
UW
11096 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11097 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11098 scope_breakpoint->type);
11099 }
d983da9c
DJ
11100 }
11101
c906108c 11102 /* Now set up the breakpoint. */
3a5c3e22
PA
11103
11104 w = XCNEW (struct watchpoint);
11105 b = &w->base;
348d480f 11106 if (use_mask)
3a5c3e22
PA
11107 init_raw_breakpoint_without_location (b, NULL, bp_type,
11108 &masked_watchpoint_breakpoint_ops);
348d480f 11109 else
3a5c3e22
PA
11110 init_raw_breakpoint_without_location (b, NULL, bp_type,
11111 &watchpoint_breakpoint_ops);
37e4754d 11112 b->thread = thread;
b5de0fa7 11113 b->disposition = disp_donttouch;
348d480f 11114 b->pspace = current_program_space;
3a5c3e22
PA
11115 w->exp = exp;
11116 w->exp_valid_block = exp_valid_block;
11117 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11118 if (just_location)
11119 {
11120 struct type *t = value_type (val);
11121 CORE_ADDR addr = value_as_address (val);
11122 char *name;
11123
11124 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11125 name = type_to_string (t);
11126
3a5c3e22 11127 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11128 core_addr_to_string (addr));
06a64a0b
TT
11129 xfree (name);
11130
3a5c3e22 11131 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11132 (int) (exp_end - exp_start), exp_start);
11133
06a64a0b
TT
11134 /* The above expression is in C. */
11135 b->language = language_c;
11136 }
11137 else
3a5c3e22 11138 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11139
11140 if (use_mask)
11141 {
3a5c3e22 11142 w->hw_wp_mask = mask;
9c06b0b4
TJB
11143 }
11144 else
11145 {
3a5c3e22
PA
11146 w->val = val;
11147 w->val_valid = 1;
9c06b0b4 11148 }
77b06cd7 11149
c906108c
SS
11150 if (cond_start)
11151 b->cond_string = savestring (cond_start, cond_end - cond_start);
11152 else
11153 b->cond_string = 0;
c5aa993b 11154
c906108c 11155 if (frame)
f6bc2008 11156 {
3a5c3e22
PA
11157 w->watchpoint_frame = get_frame_id (frame);
11158 w->watchpoint_thread = inferior_ptid;
f6bc2008 11159 }
c906108c 11160 else
f6bc2008 11161 {
3a5c3e22
PA
11162 w->watchpoint_frame = null_frame_id;
11163 w->watchpoint_thread = null_ptid;
f6bc2008 11164 }
c906108c 11165
d983da9c 11166 if (scope_breakpoint != NULL)
c906108c 11167 {
d983da9c
DJ
11168 /* The scope breakpoint is related to the watchpoint. We will
11169 need to act on them together. */
11170 b->related_breakpoint = scope_breakpoint;
11171 scope_breakpoint->related_breakpoint = b;
c906108c 11172 }
d983da9c 11173
06a64a0b
TT
11174 if (!just_location)
11175 value_free_to_mark (mark);
2d134ed3 11176
a9634178
TJB
11177 TRY_CATCH (e, RETURN_MASK_ALL)
11178 {
11179 /* Finally update the new watchpoint. This creates the locations
11180 that should be inserted. */
3a5c3e22 11181 update_watchpoint (w, 1);
a9634178
TJB
11182 }
11183 if (e.reason < 0)
11184 {
11185 delete_breakpoint (b);
11186 throw_exception (e);
11187 }
11188
3ea46bff 11189 install_breakpoint (internal, b, 1);
bbc13ae3 11190 do_cleanups (back_to);
c906108c
SS
11191}
11192
e09342b5 11193/* Return count of debug registers needed to watch the given expression.
e09342b5 11194 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11195
c906108c 11196static int
a9634178 11197can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11198{
11199 int found_memory_cnt = 0;
2e70b7b9 11200 struct value *head = v;
c906108c
SS
11201
11202 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11203 if (!can_use_hw_watchpoints)
c906108c 11204 return 0;
c5aa993b 11205
5c44784c
JM
11206 /* Make sure that the value of the expression depends only upon
11207 memory contents, and values computed from them within GDB. If we
11208 find any register references or function calls, we can't use a
11209 hardware watchpoint.
11210
11211 The idea here is that evaluating an expression generates a series
11212 of values, one holding the value of every subexpression. (The
11213 expression a*b+c has five subexpressions: a, b, a*b, c, and
11214 a*b+c.) GDB's values hold almost enough information to establish
11215 the criteria given above --- they identify memory lvalues,
11216 register lvalues, computed values, etcetera. So we can evaluate
11217 the expression, and then scan the chain of values that leaves
11218 behind to decide whether we can detect any possible change to the
11219 expression's final value using only hardware watchpoints.
11220
11221 However, I don't think that the values returned by inferior
11222 function calls are special in any way. So this function may not
11223 notice that an expression involving an inferior function call
11224 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11225 for (; v; v = value_next (v))
c906108c 11226 {
5c44784c 11227 if (VALUE_LVAL (v) == lval_memory)
c906108c 11228 {
8464be76
DJ
11229 if (v != head && value_lazy (v))
11230 /* A lazy memory lvalue in the chain is one that GDB never
11231 needed to fetch; we either just used its address (e.g.,
11232 `a' in `a.b') or we never needed it at all (e.g., `a'
11233 in `a,b'). This doesn't apply to HEAD; if that is
11234 lazy then it was not readable, but watch it anyway. */
5c44784c 11235 ;
53a5351d 11236 else
5c44784c
JM
11237 {
11238 /* Ahh, memory we actually used! Check if we can cover
11239 it with hardware watchpoints. */
df407dfe 11240 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11241
11242 /* We only watch structs and arrays if user asked for it
11243 explicitly, never if they just happen to appear in a
11244 middle of some value chain. */
11245 if (v == head
11246 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11247 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11248 {
42ae5230 11249 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11250 int len;
11251 int num_regs;
11252
a9634178 11253 len = (target_exact_watchpoints
e09342b5
TJB
11254 && is_scalar_type_recursive (vtype))?
11255 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11256
e09342b5
TJB
11257 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11258 if (!num_regs)
2e70b7b9
MS
11259 return 0;
11260 else
e09342b5 11261 found_memory_cnt += num_regs;
2e70b7b9 11262 }
5c44784c 11263 }
c5aa993b 11264 }
5086187c
AC
11265 else if (VALUE_LVAL (v) != not_lval
11266 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11267 return 0; /* These are values from the history (e.g., $1). */
5086187c 11268 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11269 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11270 }
11271
11272 /* The expression itself looks suitable for using a hardware
11273 watchpoint, but give the target machine a chance to reject it. */
11274 return found_memory_cnt;
11275}
11276
8b93c638 11277void
84f4c1fe 11278watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11279{
84f4c1fe 11280 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11281}
11282
06a64a0b
TT
11283/* A helper function that looks for the "-location" argument and then
11284 calls watch_command_1. */
11285
11286static void
11287watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11288{
11289 int just_location = 0;
11290
11291 if (arg
11292 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11293 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11294 {
e9cafbcc 11295 arg = skip_spaces (arg);
06a64a0b
TT
11296 just_location = 1;
11297 }
11298
84f4c1fe 11299 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11300}
8926118c 11301
c5aa993b 11302static void
fba45db2 11303watch_command (char *arg, int from_tty)
c906108c 11304{
06a64a0b 11305 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11306}
11307
8b93c638 11308void
84f4c1fe 11309rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11310{
84f4c1fe 11311 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11312}
8926118c 11313
c5aa993b 11314static void
fba45db2 11315rwatch_command (char *arg, int from_tty)
c906108c 11316{
06a64a0b 11317 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11318}
11319
8b93c638 11320void
84f4c1fe 11321awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11322{
84f4c1fe 11323 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11324}
8926118c 11325
c5aa993b 11326static void
fba45db2 11327awatch_command (char *arg, int from_tty)
c906108c 11328{
06a64a0b 11329 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11330}
c906108c 11331\f
c5aa993b 11332
43ff13b4 11333/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11334 because it uses the mechanisms of breakpoints. */
11335
bfec99b2
PA
11336struct until_break_command_continuation_args
11337{
11338 struct breakpoint *breakpoint;
11339 struct breakpoint *breakpoint2;
186c406b 11340 int thread_num;
bfec99b2
PA
11341};
11342
43ff13b4 11343/* This function is called by fetch_inferior_event via the
4a64f543 11344 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11345 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11346 command. */
c2c6d25f 11347static void
fa4cd53f 11348until_break_command_continuation (void *arg, int err)
43ff13b4 11349{
bfec99b2
PA
11350 struct until_break_command_continuation_args *a = arg;
11351
11352 delete_breakpoint (a->breakpoint);
11353 if (a->breakpoint2)
11354 delete_breakpoint (a->breakpoint2);
186c406b 11355 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11356}
11357
c906108c 11358void
ae66c1fc 11359until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11360{
11361 struct symtabs_and_lines sals;
11362 struct symtab_and_line sal;
8556afb4
PA
11363 struct frame_info *frame;
11364 struct gdbarch *frame_gdbarch;
11365 struct frame_id stack_frame_id;
11366 struct frame_id caller_frame_id;
c906108c 11367 struct breakpoint *breakpoint;
f107f563 11368 struct breakpoint *breakpoint2 = NULL;
c906108c 11369 struct cleanup *old_chain;
186c406b
TT
11370 int thread;
11371 struct thread_info *tp;
c906108c
SS
11372
11373 clear_proceed_status ();
11374
11375 /* Set a breakpoint where the user wants it and at return from
4a64f543 11376 this function. */
c5aa993b 11377
1bfeeb0f 11378 if (last_displayed_sal_is_valid ())
f8eba3c6 11379 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11380 get_last_displayed_symtab (),
f8eba3c6 11381 get_last_displayed_line ());
c906108c 11382 else
f8eba3c6
TT
11383 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11384 (struct symtab *) NULL, 0);
c5aa993b 11385
c906108c 11386 if (sals.nelts != 1)
8a3fe4f8 11387 error (_("Couldn't get information on specified line."));
c5aa993b 11388
c906108c 11389 sal = sals.sals[0];
4a64f543 11390 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11391
c906108c 11392 if (*arg)
8a3fe4f8 11393 error (_("Junk at end of arguments."));
c5aa993b 11394
c906108c 11395 resolve_sal_pc (&sal);
c5aa993b 11396
186c406b
TT
11397 tp = inferior_thread ();
11398 thread = tp->num;
11399
883bc8d1
PA
11400 old_chain = make_cleanup (null_cleanup, NULL);
11401
8556afb4
PA
11402 /* Note linespec handling above invalidates the frame chain.
11403 Installing a breakpoint also invalidates the frame chain (as it
11404 may need to switch threads), so do any frame handling before
11405 that. */
11406
11407 frame = get_selected_frame (NULL);
11408 frame_gdbarch = get_frame_arch (frame);
11409 stack_frame_id = get_stack_frame_id (frame);
11410 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11411
ae66c1fc
EZ
11412 /* Keep within the current frame, or in frames called by the current
11413 one. */
edb3359d 11414
883bc8d1 11415 if (frame_id_p (caller_frame_id))
c906108c 11416 {
883bc8d1
PA
11417 struct symtab_and_line sal2;
11418
11419 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11420 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11421 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11422 sal2,
11423 caller_frame_id,
f107f563
VP
11424 bp_until);
11425 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11426
883bc8d1 11427 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11428 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11429 }
c5aa993b 11430
c70a6932
JK
11431 /* set_momentary_breakpoint could invalidate FRAME. */
11432 frame = NULL;
11433
883bc8d1
PA
11434 if (anywhere)
11435 /* If the user told us to continue until a specified location,
11436 we don't specify a frame at which we need to stop. */
11437 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11438 null_frame_id, bp_until);
11439 else
11440 /* Otherwise, specify the selected frame, because we want to stop
11441 only at the very same frame. */
11442 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11443 stack_frame_id, bp_until);
11444 make_cleanup_delete_breakpoint (breakpoint);
11445
a493e3e2 11446 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11447
4a64f543
MS
11448 /* If we are running asynchronously, and proceed call above has
11449 actually managed to start the target, arrange for breakpoints to
11450 be deleted when the target stops. Otherwise, we're already
11451 stopped and delete breakpoints via cleanup chain. */
f107f563 11452
8ea051c5 11453 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11454 {
bfec99b2
PA
11455 struct until_break_command_continuation_args *args;
11456 args = xmalloc (sizeof (*args));
f107f563 11457
bfec99b2
PA
11458 args->breakpoint = breakpoint;
11459 args->breakpoint2 = breakpoint2;
186c406b 11460 args->thread_num = thread;
f107f563
VP
11461
11462 discard_cleanups (old_chain);
95e54da7
PA
11463 add_continuation (inferior_thread (),
11464 until_break_command_continuation, args,
604ead4a 11465 xfree);
f107f563
VP
11466 }
11467 else
c5aa993b 11468 do_cleanups (old_chain);
c906108c 11469}
ae66c1fc 11470
c906108c
SS
11471/* This function attempts to parse an optional "if <cond>" clause
11472 from the arg string. If one is not found, it returns NULL.
c5aa993b 11473
c906108c
SS
11474 Else, it returns a pointer to the condition string. (It does not
11475 attempt to evaluate the string against a particular block.) And,
11476 it updates arg to point to the first character following the parsed
4a64f543 11477 if clause in the arg string. */
53a5351d 11478
916703c0 11479char *
fba45db2 11480ep_parse_optional_if_clause (char **arg)
c906108c 11481{
c5aa993b
JM
11482 char *cond_string;
11483
11484 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11485 return NULL;
c5aa993b 11486
4a64f543 11487 /* Skip the "if" keyword. */
c906108c 11488 (*arg) += 2;
c5aa993b 11489
c906108c 11490 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11491 condition string. */
e9cafbcc 11492 *arg = skip_spaces (*arg);
c906108c 11493 cond_string = *arg;
c5aa993b 11494
4a64f543
MS
11495 /* Assume that the condition occupies the remainder of the arg
11496 string. */
c906108c 11497 (*arg) += strlen (cond_string);
c5aa993b 11498
c906108c
SS
11499 return cond_string;
11500}
c5aa993b 11501
c906108c
SS
11502/* Commands to deal with catching events, such as signals, exceptions,
11503 process start/exit, etc. */
c5aa993b
JM
11504
11505typedef enum
11506{
44feb3ce
TT
11507 catch_fork_temporary, catch_vfork_temporary,
11508 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11509}
11510catch_fork_kind;
11511
c906108c 11512static void
cc59ec59
MS
11513catch_fork_command_1 (char *arg, int from_tty,
11514 struct cmd_list_element *command)
c906108c 11515{
a6d9a66e 11516 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11517 char *cond_string = NULL;
44feb3ce
TT
11518 catch_fork_kind fork_kind;
11519 int tempflag;
11520
11521 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11522 tempflag = (fork_kind == catch_fork_temporary
11523 || fork_kind == catch_vfork_temporary);
c5aa993b 11524
44feb3ce
TT
11525 if (!arg)
11526 arg = "";
e9cafbcc 11527 arg = skip_spaces (arg);
c5aa993b 11528
c906108c 11529 /* The allowed syntax is:
c5aa993b
JM
11530 catch [v]fork
11531 catch [v]fork if <cond>
11532
4a64f543 11533 First, check if there's an if clause. */
c906108c 11534 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11535
c906108c 11536 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11537 error (_("Junk at end of arguments."));
c5aa993b 11538
c906108c 11539 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11540 and enable reporting of such events. */
c5aa993b
JM
11541 switch (fork_kind)
11542 {
44feb3ce
TT
11543 case catch_fork_temporary:
11544 case catch_fork_permanent:
a6d9a66e 11545 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11546 &catch_fork_breakpoint_ops);
c906108c 11547 break;
44feb3ce
TT
11548 case catch_vfork_temporary:
11549 case catch_vfork_permanent:
a6d9a66e 11550 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11551 &catch_vfork_breakpoint_ops);
c906108c 11552 break;
c5aa993b 11553 default:
8a3fe4f8 11554 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11555 break;
c5aa993b 11556 }
c906108c
SS
11557}
11558
11559static void
cc59ec59
MS
11560catch_exec_command_1 (char *arg, int from_tty,
11561 struct cmd_list_element *command)
c906108c 11562{
b4d90040 11563 struct exec_catchpoint *c;
a6d9a66e 11564 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11565 int tempflag;
c5aa993b 11566 char *cond_string = NULL;
c906108c 11567
44feb3ce
TT
11568 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11569
11570 if (!arg)
11571 arg = "";
e9cafbcc 11572 arg = skip_spaces (arg);
c906108c
SS
11573
11574 /* The allowed syntax is:
c5aa993b
JM
11575 catch exec
11576 catch exec if <cond>
c906108c 11577
4a64f543 11578 First, check if there's an if clause. */
c906108c
SS
11579 cond_string = ep_parse_optional_if_clause (&arg);
11580
11581 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11582 error (_("Junk at end of arguments."));
c906108c 11583
b4d90040
PA
11584 c = XNEW (struct exec_catchpoint);
11585 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11586 &catch_exec_breakpoint_ops);
11587 c->exec_pathname = NULL;
11588
3ea46bff 11589 install_breakpoint (0, &c->base, 1);
c906108c 11590}
c5aa993b 11591
9ac4176b 11592void
28010a5d
PA
11593init_ada_exception_breakpoint (struct breakpoint *b,
11594 struct gdbarch *gdbarch,
11595 struct symtab_and_line sal,
11596 char *addr_string,
c0a91b2b 11597 const struct breakpoint_ops *ops,
28010a5d
PA
11598 int tempflag,
11599 int from_tty)
f7f9143b 11600{
f7f9143b
JB
11601 if (from_tty)
11602 {
5af949e3
UW
11603 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11604 if (!loc_gdbarch)
11605 loc_gdbarch = gdbarch;
11606
6c95b8df
PA
11607 describe_other_breakpoints (loc_gdbarch,
11608 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11609 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11610 version for exception catchpoints, because two catchpoints
11611 used for different exception names will use the same address.
11612 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11613 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11614 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11615 the user what type of catchpoint it is. The above is good
11616 enough for now, though. */
11617 }
11618
28010a5d 11619 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11620
11621 b->enable_state = bp_enabled;
11622 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11623 b->addr_string = addr_string;
11624 b->language = language_ada;
f7f9143b
JB
11625}
11626
a96d9b2e
SDJ
11627/* Splits the argument using space as delimiter. Returns an xmalloc'd
11628 filter list, or NULL if no filtering is required. */
11629static VEC(int) *
11630catch_syscall_split_args (char *arg)
11631{
11632 VEC(int) *result = NULL;
29d0bb3d 11633 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11634
11635 while (*arg != '\0')
11636 {
11637 int i, syscall_number;
11638 char *endptr;
11639 char cur_name[128];
11640 struct syscall s;
11641
11642 /* Skip whitespace. */
529480d0 11643 arg = skip_spaces (arg);
a96d9b2e
SDJ
11644
11645 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11646 cur_name[i] = arg[i];
11647 cur_name[i] = '\0';
11648 arg += i;
11649
11650 /* Check if the user provided a syscall name or a number. */
11651 syscall_number = (int) strtol (cur_name, &endptr, 0);
11652 if (*endptr == '\0')
bccd0dd2 11653 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11654 else
11655 {
11656 /* We have a name. Let's check if it's valid and convert it
11657 to a number. */
11658 get_syscall_by_name (cur_name, &s);
11659
11660 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11661 /* Here we have to issue an error instead of a warning,
11662 because GDB cannot do anything useful if there's no
11663 syscall number to be caught. */
a96d9b2e
SDJ
11664 error (_("Unknown syscall name '%s'."), cur_name);
11665 }
11666
11667 /* Ok, it's valid. */
11668 VEC_safe_push (int, result, s.number);
11669 }
11670
11671 discard_cleanups (cleanup);
11672 return result;
11673}
11674
11675/* Implement the "catch syscall" command. */
11676
11677static void
cc59ec59
MS
11678catch_syscall_command_1 (char *arg, int from_tty,
11679 struct cmd_list_element *command)
a96d9b2e
SDJ
11680{
11681 int tempflag;
11682 VEC(int) *filter;
11683 struct syscall s;
11684 struct gdbarch *gdbarch = get_current_arch ();
11685
11686 /* Checking if the feature if supported. */
11687 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11688 error (_("The feature 'catch syscall' is not supported on \
ea666128 11689this architecture yet."));
a96d9b2e
SDJ
11690
11691 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11692
e9cafbcc 11693 arg = skip_spaces (arg);
a96d9b2e
SDJ
11694
11695 /* We need to do this first "dummy" translation in order
11696 to get the syscall XML file loaded or, most important,
11697 to display a warning to the user if there's no XML file
11698 for his/her architecture. */
11699 get_syscall_by_number (0, &s);
11700
11701 /* The allowed syntax is:
11702 catch syscall
11703 catch syscall <name | number> [<name | number> ... <name | number>]
11704
11705 Let's check if there's a syscall name. */
11706
11707 if (arg != NULL)
11708 filter = catch_syscall_split_args (arg);
11709 else
11710 filter = NULL;
11711
11712 create_syscall_event_catchpoint (tempflag, filter,
11713 &catch_syscall_breakpoint_ops);
11714}
11715
c906108c 11716static void
fba45db2 11717catch_command (char *arg, int from_tty)
c906108c 11718{
44feb3ce 11719 error (_("Catch requires an event name."));
c906108c
SS
11720}
11721\f
11722
11723static void
fba45db2 11724tcatch_command (char *arg, int from_tty)
c906108c 11725{
44feb3ce 11726 error (_("Catch requires an event name."));
c906108c
SS
11727}
11728
8a2c437b
TT
11729/* A qsort comparison function that sorts breakpoints in order. */
11730
11731static int
11732compare_breakpoints (const void *a, const void *b)
11733{
11734 const breakpoint_p *ba = a;
11735 uintptr_t ua = (uintptr_t) *ba;
11736 const breakpoint_p *bb = b;
11737 uintptr_t ub = (uintptr_t) *bb;
11738
11739 if ((*ba)->number < (*bb)->number)
11740 return -1;
11741 else if ((*ba)->number > (*bb)->number)
11742 return 1;
11743
11744 /* Now sort by address, in case we see, e..g, two breakpoints with
11745 the number 0. */
11746 if (ua < ub)
11747 return -1;
94b0e70d 11748 return ua > ub ? 1 : 0;
8a2c437b
TT
11749}
11750
80f8a6eb 11751/* Delete breakpoints by address or line. */
c906108c
SS
11752
11753static void
fba45db2 11754clear_command (char *arg, int from_tty)
c906108c 11755{
8a2c437b 11756 struct breakpoint *b, *prev;
d6e956e5
VP
11757 VEC(breakpoint_p) *found = 0;
11758 int ix;
c906108c
SS
11759 int default_match;
11760 struct symtabs_and_lines sals;
11761 struct symtab_and_line sal;
c906108c 11762 int i;
8a2c437b 11763 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11764
11765 if (arg)
11766 {
39cf75f7
DE
11767 sals = decode_line_with_current_source (arg,
11768 (DECODE_LINE_FUNFIRSTLINE
11769 | DECODE_LINE_LIST_MODE));
cf4ded82 11770 make_cleanup (xfree, sals.sals);
c906108c
SS
11771 default_match = 0;
11772 }
11773 else
11774 {
c5aa993b 11775 sals.sals = (struct symtab_and_line *)
c906108c 11776 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11777 make_cleanup (xfree, sals.sals);
4a64f543 11778 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11779
11780 /* Set sal's line, symtab, pc, and pspace to the values
11781 corresponding to the last call to print_frame_info. If the
11782 codepoint is not valid, this will set all the fields to 0. */
11783 get_last_displayed_sal (&sal);
c906108c 11784 if (sal.symtab == 0)
8a3fe4f8 11785 error (_("No source file specified."));
c906108c
SS
11786
11787 sals.sals[0] = sal;
11788 sals.nelts = 1;
11789
11790 default_match = 1;
11791 }
11792
4a64f543
MS
11793 /* We don't call resolve_sal_pc here. That's not as bad as it
11794 seems, because all existing breakpoints typically have both
11795 file/line and pc set. So, if clear is given file/line, we can
11796 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11797
11798 We only support clearing given the address explicitly
11799 present in breakpoint table. Say, we've set breakpoint
4a64f543 11800 at file:line. There were several PC values for that file:line,
ed0616c6 11801 due to optimization, all in one block.
4a64f543
MS
11802
11803 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11804 PC corresponding to the same file:line, the breakpoint won't
11805 be cleared. We probably can still clear the breakpoint, but
11806 since the other PC value is never presented to user, user
11807 can only find it by guessing, and it does not seem important
11808 to support that. */
11809
4a64f543
MS
11810 /* For each line spec given, delete bps which correspond to it. Do
11811 it in two passes, solely to preserve the current behavior that
11812 from_tty is forced true if we delete more than one
11813 breakpoint. */
c906108c 11814
80f8a6eb 11815 found = NULL;
8a2c437b 11816 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11817 for (i = 0; i < sals.nelts; i++)
11818 {
05cba821
JK
11819 const char *sal_fullname;
11820
c906108c 11821 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11822 If line given (pc == 0), clear all bpts on specified line.
11823 If defaulting, clear all bpts on default line
c906108c 11824 or at default pc.
c5aa993b
JM
11825
11826 defaulting sal.pc != 0 tests to do
11827
11828 0 1 pc
11829 1 1 pc _and_ line
11830 0 0 line
11831 1 0 <can't happen> */
c906108c
SS
11832
11833 sal = sals.sals[i];
05cba821
JK
11834 sal_fullname = (sal.symtab == NULL
11835 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11836
4a64f543 11837 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11838 ALL_BREAKPOINTS (b)
c5aa993b 11839 {
0d381245 11840 int match = 0;
4a64f543 11841 /* Are we going to delete b? */
cc60f2e3 11842 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11843 {
11844 struct bp_location *loc = b->loc;
11845 for (; loc; loc = loc->next)
11846 {
f8eba3c6
TT
11847 /* If the user specified file:line, don't allow a PC
11848 match. This matches historical gdb behavior. */
11849 int pc_match = (!sal.explicit_line
11850 && sal.pc
11851 && (loc->pspace == sal.pspace)
11852 && (loc->address == sal.pc)
11853 && (!section_is_overlay (loc->section)
11854 || loc->section == sal.section));
4aac40c8
TT
11855 int line_match = 0;
11856
11857 if ((default_match || sal.explicit_line)
2f202fde 11858 && loc->symtab != NULL
05cba821 11859 && sal_fullname != NULL
4aac40c8 11860 && sal.pspace == loc->pspace
05cba821
JK
11861 && loc->line_number == sal.line
11862 && filename_cmp (symtab_to_fullname (loc->symtab),
11863 sal_fullname) == 0)
11864 line_match = 1;
4aac40c8 11865
0d381245
VP
11866 if (pc_match || line_match)
11867 {
11868 match = 1;
11869 break;
11870 }
11871 }
11872 }
11873
11874 if (match)
d6e956e5 11875 VEC_safe_push(breakpoint_p, found, b);
c906108c 11876 }
80f8a6eb 11877 }
8a2c437b 11878
80f8a6eb 11879 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11880 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11881 {
11882 if (arg)
8a3fe4f8 11883 error (_("No breakpoint at %s."), arg);
80f8a6eb 11884 else
8a3fe4f8 11885 error (_("No breakpoint at this line."));
80f8a6eb 11886 }
c906108c 11887
8a2c437b
TT
11888 /* Remove duplicates from the vec. */
11889 qsort (VEC_address (breakpoint_p, found),
11890 VEC_length (breakpoint_p, found),
11891 sizeof (breakpoint_p),
11892 compare_breakpoints);
11893 prev = VEC_index (breakpoint_p, found, 0);
11894 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11895 {
11896 if (b == prev)
11897 {
11898 VEC_ordered_remove (breakpoint_p, found, ix);
11899 --ix;
11900 }
11901 }
11902
d6e956e5 11903 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11904 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11905 if (from_tty)
a3f17187 11906 {
d6e956e5 11907 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11908 printf_unfiltered (_("Deleted breakpoint "));
11909 else
11910 printf_unfiltered (_("Deleted breakpoints "));
11911 }
d6e956e5
VP
11912
11913 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11914 {
c5aa993b 11915 if (from_tty)
d6e956e5
VP
11916 printf_unfiltered ("%d ", b->number);
11917 delete_breakpoint (b);
c906108c 11918 }
80f8a6eb
MS
11919 if (from_tty)
11920 putchar_unfiltered ('\n');
8a2c437b
TT
11921
11922 do_cleanups (cleanups);
c906108c
SS
11923}
11924\f
11925/* Delete breakpoint in BS if they are `delete' breakpoints and
11926 all breakpoints that are marked for deletion, whether hit or not.
11927 This is called after any breakpoint is hit, or after errors. */
11928
11929void
fba45db2 11930breakpoint_auto_delete (bpstat bs)
c906108c 11931{
35df4500 11932 struct breakpoint *b, *b_tmp;
c906108c
SS
11933
11934 for (; bs; bs = bs->next)
f431efe5
PA
11935 if (bs->breakpoint_at
11936 && bs->breakpoint_at->disposition == disp_del
c906108c 11937 && bs->stop)
f431efe5 11938 delete_breakpoint (bs->breakpoint_at);
c906108c 11939
35df4500 11940 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11941 {
b5de0fa7 11942 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11943 delete_breakpoint (b);
11944 }
c906108c
SS
11945}
11946
4a64f543
MS
11947/* A comparison function for bp_location AP and BP being interfaced to
11948 qsort. Sort elements primarily by their ADDRESS (no matter what
11949 does breakpoint_address_is_meaningful say for its OWNER),
11950 secondarily by ordering first bp_permanent OWNERed elements and
11951 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11952 qsort being an unstable algorithm. */
876fa593
JK
11953
11954static int
494cfb0f 11955bp_location_compare (const void *ap, const void *bp)
876fa593 11956{
494cfb0f
JK
11957 struct bp_location *a = *(void **) ap;
11958 struct bp_location *b = *(void **) bp;
2bdf28a0 11959 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11960 int a_perm = a->owner->enable_state == bp_permanent;
11961 int b_perm = b->owner->enable_state == bp_permanent;
11962
11963 if (a->address != b->address)
11964 return (a->address > b->address) - (a->address < b->address);
11965
dea2aa5f
LM
11966 /* Sort locations at the same address by their pspace number, keeping
11967 locations of the same inferior (in a multi-inferior environment)
11968 grouped. */
11969
11970 if (a->pspace->num != b->pspace->num)
11971 return ((a->pspace->num > b->pspace->num)
11972 - (a->pspace->num < b->pspace->num));
11973
876fa593
JK
11974 /* Sort permanent breakpoints first. */
11975 if (a_perm != b_perm)
11976 return (a_perm < b_perm) - (a_perm > b_perm);
11977
c56a97f9
JK
11978 /* Make the internal GDB representation stable across GDB runs
11979 where A and B memory inside GDB can differ. Breakpoint locations of
11980 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11981
11982 if (a->owner->number != b->owner->number)
c56a97f9
JK
11983 return ((a->owner->number > b->owner->number)
11984 - (a->owner->number < b->owner->number));
876fa593
JK
11985
11986 return (a > b) - (a < b);
11987}
11988
876fa593 11989/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11990 bp_location_shadow_len_after_address_max according to the current
11991 content of the bp_location array. */
f7545552
TT
11992
11993static void
876fa593 11994bp_location_target_extensions_update (void)
f7545552 11995{
876fa593
JK
11996 struct bp_location *bl, **blp_tmp;
11997
11998 bp_location_placed_address_before_address_max = 0;
11999 bp_location_shadow_len_after_address_max = 0;
12000
12001 ALL_BP_LOCATIONS (bl, blp_tmp)
12002 {
12003 CORE_ADDR start, end, addr;
12004
12005 if (!bp_location_has_shadow (bl))
12006 continue;
12007
12008 start = bl->target_info.placed_address;
12009 end = start + bl->target_info.shadow_len;
12010
12011 gdb_assert (bl->address >= start);
12012 addr = bl->address - start;
12013 if (addr > bp_location_placed_address_before_address_max)
12014 bp_location_placed_address_before_address_max = addr;
12015
12016 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12017
12018 gdb_assert (bl->address < end);
12019 addr = end - bl->address;
12020 if (addr > bp_location_shadow_len_after_address_max)
12021 bp_location_shadow_len_after_address_max = addr;
12022 }
f7545552
TT
12023}
12024
1e4d1764
YQ
12025/* Download tracepoint locations if they haven't been. */
12026
12027static void
12028download_tracepoint_locations (void)
12029{
7ed2c994 12030 struct breakpoint *b;
1e4d1764
YQ
12031 struct cleanup *old_chain;
12032
12033 if (!target_can_download_tracepoint ())
12034 return;
12035
12036 old_chain = save_current_space_and_thread ();
12037
7ed2c994 12038 ALL_TRACEPOINTS (b)
1e4d1764 12039 {
7ed2c994 12040 struct bp_location *bl;
1e4d1764 12041 struct tracepoint *t;
f2a8bc8a 12042 int bp_location_downloaded = 0;
1e4d1764 12043
7ed2c994 12044 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12045 ? !may_insert_fast_tracepoints
12046 : !may_insert_tracepoints))
12047 continue;
12048
7ed2c994
YQ
12049 for (bl = b->loc; bl; bl = bl->next)
12050 {
12051 /* In tracepoint, locations are _never_ duplicated, so
12052 should_be_inserted is equivalent to
12053 unduplicated_should_be_inserted. */
12054 if (!should_be_inserted (bl) || bl->inserted)
12055 continue;
1e4d1764 12056
7ed2c994 12057 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12058
7ed2c994 12059 target_download_tracepoint (bl);
1e4d1764 12060
7ed2c994 12061 bl->inserted = 1;
f2a8bc8a 12062 bp_location_downloaded = 1;
7ed2c994
YQ
12063 }
12064 t = (struct tracepoint *) b;
12065 t->number_on_target = b->number;
f2a8bc8a
YQ
12066 if (bp_location_downloaded)
12067 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12068 }
12069
12070 do_cleanups (old_chain);
12071}
12072
934709f0
PW
12073/* Swap the insertion/duplication state between two locations. */
12074
12075static void
12076swap_insertion (struct bp_location *left, struct bp_location *right)
12077{
12078 const int left_inserted = left->inserted;
12079 const int left_duplicate = left->duplicate;
b775012e 12080 const int left_needs_update = left->needs_update;
934709f0
PW
12081 const struct bp_target_info left_target_info = left->target_info;
12082
1e4d1764
YQ
12083 /* Locations of tracepoints can never be duplicated. */
12084 if (is_tracepoint (left->owner))
12085 gdb_assert (!left->duplicate);
12086 if (is_tracepoint (right->owner))
12087 gdb_assert (!right->duplicate);
12088
934709f0
PW
12089 left->inserted = right->inserted;
12090 left->duplicate = right->duplicate;
b775012e 12091 left->needs_update = right->needs_update;
934709f0
PW
12092 left->target_info = right->target_info;
12093 right->inserted = left_inserted;
12094 right->duplicate = left_duplicate;
b775012e 12095 right->needs_update = left_needs_update;
934709f0
PW
12096 right->target_info = left_target_info;
12097}
12098
b775012e
LM
12099/* Force the re-insertion of the locations at ADDRESS. This is called
12100 once a new/deleted/modified duplicate location is found and we are evaluating
12101 conditions on the target's side. Such conditions need to be updated on
12102 the target. */
12103
12104static void
12105force_breakpoint_reinsertion (struct bp_location *bl)
12106{
12107 struct bp_location **locp = NULL, **loc2p;
12108 struct bp_location *loc;
12109 CORE_ADDR address = 0;
12110 int pspace_num;
12111
12112 address = bl->address;
12113 pspace_num = bl->pspace->num;
12114
12115 /* This is only meaningful if the target is
12116 evaluating conditions and if the user has
12117 opted for condition evaluation on the target's
12118 side. */
12119 if (gdb_evaluates_breakpoint_condition_p ()
12120 || !target_supports_evaluation_of_breakpoint_conditions ())
12121 return;
12122
12123 /* Flag all breakpoint locations with this address and
12124 the same program space as the location
12125 as "its condition has changed". We need to
12126 update the conditions on the target's side. */
12127 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12128 {
12129 loc = *loc2p;
12130
12131 if (!is_breakpoint (loc->owner)
12132 || pspace_num != loc->pspace->num)
12133 continue;
12134
12135 /* Flag the location appropriately. We use a different state to
12136 let everyone know that we already updated the set of locations
12137 with addr bl->address and program space bl->pspace. This is so
12138 we don't have to keep calling these functions just to mark locations
12139 that have already been marked. */
12140 loc->condition_changed = condition_updated;
12141
12142 /* Free the agent expression bytecode as well. We will compute
12143 it later on. */
12144 if (loc->cond_bytecode)
12145 {
12146 free_agent_expr (loc->cond_bytecode);
12147 loc->cond_bytecode = NULL;
12148 }
12149 }
12150}
12151
4cd9bd08 12152/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12153 into the inferior, only remove already-inserted locations that no
12154 longer should be inserted. Functions that delete a breakpoint or
12155 breakpoints should pass false, so that deleting a breakpoint
12156 doesn't have the side effect of inserting the locations of other
12157 breakpoints that are marked not-inserted, but should_be_inserted
12158 returns true on them.
12159
12160 This behaviour is useful is situations close to tear-down -- e.g.,
12161 after an exec, while the target still has execution, but breakpoint
12162 shadows of the previous executable image should *NOT* be restored
12163 to the new image; or before detaching, where the target still has
12164 execution and wants to delete breakpoints from GDB's lists, and all
12165 breakpoints had already been removed from the inferior. */
12166
0d381245 12167static void
b60e7edf 12168update_global_location_list (int should_insert)
0d381245 12169{
74960c60 12170 struct breakpoint *b;
876fa593 12171 struct bp_location **locp, *loc;
f7545552 12172 struct cleanup *cleanups;
b775012e
LM
12173 /* Last breakpoint location address that was marked for update. */
12174 CORE_ADDR last_addr = 0;
12175 /* Last breakpoint location program space that was marked for update. */
12176 int last_pspace_num = -1;
f7545552 12177
2d134ed3
PA
12178 /* Used in the duplicates detection below. When iterating over all
12179 bp_locations, points to the first bp_location of a given address.
12180 Breakpoints and watchpoints of different types are never
12181 duplicates of each other. Keep one pointer for each type of
12182 breakpoint/watchpoint, so we only need to loop over all locations
12183 once. */
12184 struct bp_location *bp_loc_first; /* breakpoint */
12185 struct bp_location *wp_loc_first; /* hardware watchpoint */
12186 struct bp_location *awp_loc_first; /* access watchpoint */
12187 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12188
4a64f543
MS
12189 /* Saved former bp_location array which we compare against the newly
12190 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12191 struct bp_location **old_location, **old_locp;
12192 unsigned old_location_count;
12193
12194 old_location = bp_location;
12195 old_location_count = bp_location_count;
12196 bp_location = NULL;
12197 bp_location_count = 0;
12198 cleanups = make_cleanup (xfree, old_location);
0d381245 12199
74960c60 12200 ALL_BREAKPOINTS (b)
876fa593
JK
12201 for (loc = b->loc; loc; loc = loc->next)
12202 bp_location_count++;
12203
12204 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12205 locp = bp_location;
12206 ALL_BREAKPOINTS (b)
12207 for (loc = b->loc; loc; loc = loc->next)
12208 *locp++ = loc;
12209 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12210 bp_location_compare);
876fa593
JK
12211
12212 bp_location_target_extensions_update ();
74960c60 12213
4a64f543
MS
12214 /* Identify bp_location instances that are no longer present in the
12215 new list, and therefore should be freed. Note that it's not
12216 necessary that those locations should be removed from inferior --
12217 if there's another location at the same address (previously
12218 marked as duplicate), we don't need to remove/insert the
12219 location.
876fa593 12220
4a64f543
MS
12221 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12222 and former bp_location array state respectively. */
876fa593
JK
12223
12224 locp = bp_location;
12225 for (old_locp = old_location; old_locp < old_location + old_location_count;
12226 old_locp++)
74960c60 12227 {
876fa593 12228 struct bp_location *old_loc = *old_locp;
c7d46a38 12229 struct bp_location **loc2p;
876fa593 12230
e5dd4106 12231 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12232 not, we have to free it. */
c7d46a38 12233 int found_object = 0;
20874c92
VP
12234 /* Tells if the location should remain inserted in the target. */
12235 int keep_in_target = 0;
12236 int removed = 0;
876fa593 12237
4a64f543
MS
12238 /* Skip LOCP entries which will definitely never be needed.
12239 Stop either at or being the one matching OLD_LOC. */
876fa593 12240 while (locp < bp_location + bp_location_count
c7d46a38 12241 && (*locp)->address < old_loc->address)
876fa593 12242 locp++;
c7d46a38
PA
12243
12244 for (loc2p = locp;
12245 (loc2p < bp_location + bp_location_count
12246 && (*loc2p)->address == old_loc->address);
12247 loc2p++)
12248 {
b775012e
LM
12249 /* Check if this is a new/duplicated location or a duplicated
12250 location that had its condition modified. If so, we want to send
12251 its condition to the target if evaluation of conditions is taking
12252 place there. */
12253 if ((*loc2p)->condition_changed == condition_modified
12254 && (last_addr != old_loc->address
12255 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12256 {
b775012e
LM
12257 force_breakpoint_reinsertion (*loc2p);
12258 last_pspace_num = old_loc->pspace->num;
c7d46a38 12259 }
b775012e
LM
12260
12261 if (*loc2p == old_loc)
12262 found_object = 1;
c7d46a38 12263 }
74960c60 12264
b775012e
LM
12265 /* We have already handled this address, update it so that we don't
12266 have to go through updates again. */
12267 last_addr = old_loc->address;
12268
12269 /* Target-side condition evaluation: Handle deleted locations. */
12270 if (!found_object)
12271 force_breakpoint_reinsertion (old_loc);
12272
4a64f543
MS
12273 /* If this location is no longer present, and inserted, look if
12274 there's maybe a new location at the same address. If so,
12275 mark that one inserted, and don't remove this one. This is
12276 needed so that we don't have a time window where a breakpoint
12277 at certain location is not inserted. */
74960c60 12278
876fa593 12279 if (old_loc->inserted)
0d381245 12280 {
4a64f543
MS
12281 /* If the location is inserted now, we might have to remove
12282 it. */
74960c60 12283
876fa593 12284 if (found_object && should_be_inserted (old_loc))
74960c60 12285 {
4a64f543
MS
12286 /* The location is still present in the location list,
12287 and still should be inserted. Don't do anything. */
20874c92 12288 keep_in_target = 1;
74960c60
VP
12289 }
12290 else
12291 {
b775012e
LM
12292 /* This location still exists, but it won't be kept in the
12293 target since it may have been disabled. We proceed to
12294 remove its target-side condition. */
12295
4a64f543
MS
12296 /* The location is either no longer present, or got
12297 disabled. See if there's another location at the
12298 same address, in which case we don't need to remove
12299 this one from the target. */
876fa593 12300
2bdf28a0 12301 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12302 if (breakpoint_address_is_meaningful (old_loc->owner))
12303 {
876fa593 12304 for (loc2p = locp;
c7d46a38
PA
12305 (loc2p < bp_location + bp_location_count
12306 && (*loc2p)->address == old_loc->address);
876fa593
JK
12307 loc2p++)
12308 {
12309 struct bp_location *loc2 = *loc2p;
12310
2d134ed3 12311 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12312 {
85d721b8
PA
12313 /* Read watchpoint locations are switched to
12314 access watchpoints, if the former are not
12315 supported, but the latter are. */
12316 if (is_hardware_watchpoint (old_loc->owner))
12317 {
12318 gdb_assert (is_hardware_watchpoint (loc2->owner));
12319 loc2->watchpoint_type = old_loc->watchpoint_type;
12320 }
12321
934709f0
PW
12322 /* loc2 is a duplicated location. We need to check
12323 if it should be inserted in case it will be
12324 unduplicated. */
12325 if (loc2 != old_loc
12326 && unduplicated_should_be_inserted (loc2))
c7d46a38 12327 {
934709f0 12328 swap_insertion (old_loc, loc2);
c7d46a38
PA
12329 keep_in_target = 1;
12330 break;
12331 }
876fa593
JK
12332 }
12333 }
12334 }
74960c60
VP
12335 }
12336
20874c92
VP
12337 if (!keep_in_target)
12338 {
876fa593 12339 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12340 {
4a64f543
MS
12341 /* This is just about all we can do. We could keep
12342 this location on the global list, and try to
12343 remove it next time, but there's no particular
12344 reason why we will succeed next time.
20874c92 12345
4a64f543
MS
12346 Note that at this point, old_loc->owner is still
12347 valid, as delete_breakpoint frees the breakpoint
12348 only after calling us. */
3e43a32a
MS
12349 printf_filtered (_("warning: Error removing "
12350 "breakpoint %d\n"),
876fa593 12351 old_loc->owner->number);
20874c92
VP
12352 }
12353 removed = 1;
12354 }
0d381245 12355 }
74960c60
VP
12356
12357 if (!found_object)
1c5cfe86 12358 {
db82e815
PA
12359 if (removed && non_stop
12360 && breakpoint_address_is_meaningful (old_loc->owner)
12361 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12362 {
db82e815
PA
12363 /* This location was removed from the target. In
12364 non-stop mode, a race condition is possible where
12365 we've removed a breakpoint, but stop events for that
12366 breakpoint are already queued and will arrive later.
12367 We apply an heuristic to be able to distinguish such
12368 SIGTRAPs from other random SIGTRAPs: we keep this
12369 breakpoint location for a bit, and will retire it
12370 after we see some number of events. The theory here
12371 is that reporting of events should, "on the average",
12372 be fair, so after a while we'll see events from all
12373 threads that have anything of interest, and no longer
12374 need to keep this breakpoint location around. We
12375 don't hold locations forever so to reduce chances of
12376 mistaking a non-breakpoint SIGTRAP for a breakpoint
12377 SIGTRAP.
12378
12379 The heuristic failing can be disastrous on
12380 decr_pc_after_break targets.
12381
12382 On decr_pc_after_break targets, like e.g., x86-linux,
12383 if we fail to recognize a late breakpoint SIGTRAP,
12384 because events_till_retirement has reached 0 too
12385 soon, we'll fail to do the PC adjustment, and report
12386 a random SIGTRAP to the user. When the user resumes
12387 the inferior, it will most likely immediately crash
2dec564e 12388 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12389 corrupted, because of being resumed e.g., in the
12390 middle of a multi-byte instruction, or skipped a
12391 one-byte instruction. This was actually seen happen
12392 on native x86-linux, and should be less rare on
12393 targets that do not support new thread events, like
12394 remote, due to the heuristic depending on
12395 thread_count.
12396
12397 Mistaking a random SIGTRAP for a breakpoint trap
12398 causes similar symptoms (PC adjustment applied when
12399 it shouldn't), but then again, playing with SIGTRAPs
12400 behind the debugger's back is asking for trouble.
12401
12402 Since hardware watchpoint traps are always
12403 distinguishable from other traps, so we don't need to
12404 apply keep hardware watchpoint moribund locations
12405 around. We simply always ignore hardware watchpoint
12406 traps we can no longer explain. */
12407
876fa593
JK
12408 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12409 old_loc->owner = NULL;
20874c92 12410
876fa593 12411 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12412 }
12413 else
f431efe5
PA
12414 {
12415 old_loc->owner = NULL;
12416 decref_bp_location (&old_loc);
12417 }
20874c92 12418 }
74960c60 12419 }
1c5cfe86 12420
348d480f
PA
12421 /* Rescan breakpoints at the same address and section, marking the
12422 first one as "first" and any others as "duplicates". This is so
12423 that the bpt instruction is only inserted once. If we have a
12424 permanent breakpoint at the same place as BPT, make that one the
12425 official one, and the rest as duplicates. Permanent breakpoints
12426 are sorted first for the same address.
12427
12428 Do the same for hardware watchpoints, but also considering the
12429 watchpoint's type (regular/access/read) and length. */
12430
12431 bp_loc_first = NULL;
12432 wp_loc_first = NULL;
12433 awp_loc_first = NULL;
12434 rwp_loc_first = NULL;
12435 ALL_BP_LOCATIONS (loc, locp)
12436 {
12437 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12438 non-NULL. */
348d480f 12439 struct bp_location **loc_first_p;
d3fbdd86 12440 b = loc->owner;
348d480f 12441
6f380991 12442 if (!unduplicated_should_be_inserted (loc)
348d480f 12443 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12444 /* Don't detect duplicate for tracepoint locations because they are
12445 never duplicated. See the comments in field `duplicate' of
12446 `struct bp_location'. */
348d480f 12447 || is_tracepoint (b))
b775012e
LM
12448 {
12449 /* Clear the condition modification flag. */
12450 loc->condition_changed = condition_unchanged;
12451 continue;
12452 }
348d480f
PA
12453
12454 /* Permanent breakpoint should always be inserted. */
12455 if (b->enable_state == bp_permanent && ! loc->inserted)
12456 internal_error (__FILE__, __LINE__,
12457 _("allegedly permanent breakpoint is not "
12458 "actually inserted"));
12459
12460 if (b->type == bp_hardware_watchpoint)
12461 loc_first_p = &wp_loc_first;
12462 else if (b->type == bp_read_watchpoint)
12463 loc_first_p = &rwp_loc_first;
12464 else if (b->type == bp_access_watchpoint)
12465 loc_first_p = &awp_loc_first;
12466 else
12467 loc_first_p = &bp_loc_first;
12468
12469 if (*loc_first_p == NULL
12470 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12471 || !breakpoint_locations_match (loc, *loc_first_p))
12472 {
12473 *loc_first_p = loc;
12474 loc->duplicate = 0;
b775012e
LM
12475
12476 if (is_breakpoint (loc->owner) && loc->condition_changed)
12477 {
12478 loc->needs_update = 1;
12479 /* Clear the condition modification flag. */
12480 loc->condition_changed = condition_unchanged;
12481 }
348d480f
PA
12482 continue;
12483 }
12484
934709f0
PW
12485
12486 /* This and the above ensure the invariant that the first location
12487 is not duplicated, and is the inserted one.
12488 All following are marked as duplicated, and are not inserted. */
12489 if (loc->inserted)
12490 swap_insertion (loc, *loc_first_p);
348d480f
PA
12491 loc->duplicate = 1;
12492
b775012e
LM
12493 /* Clear the condition modification flag. */
12494 loc->condition_changed = condition_unchanged;
12495
348d480f
PA
12496 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12497 && b->enable_state != bp_permanent)
12498 internal_error (__FILE__, __LINE__,
12499 _("another breakpoint was inserted on top of "
12500 "a permanent breakpoint"));
12501 }
12502
b775012e 12503 if (breakpoints_always_inserted_mode ()
348d480f 12504 && (have_live_inferiors ()
f5656ead 12505 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12506 {
12507 if (should_insert)
12508 insert_breakpoint_locations ();
12509 else
12510 {
12511 /* Though should_insert is false, we may need to update conditions
12512 on the target's side if it is evaluating such conditions. We
12513 only update conditions for locations that are marked
12514 "needs_update". */
12515 update_inserted_breakpoint_locations ();
12516 }
12517 }
348d480f 12518
1e4d1764
YQ
12519 if (should_insert)
12520 download_tracepoint_locations ();
12521
348d480f
PA
12522 do_cleanups (cleanups);
12523}
12524
12525void
12526breakpoint_retire_moribund (void)
12527{
12528 struct bp_location *loc;
12529 int ix;
12530
12531 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12532 if (--(loc->events_till_retirement) == 0)
12533 {
12534 decref_bp_location (&loc);
12535 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12536 --ix;
12537 }
12538}
12539
12540static void
12541update_global_location_list_nothrow (int inserting)
12542{
bfd189b1 12543 volatile struct gdb_exception e;
348d480f
PA
12544
12545 TRY_CATCH (e, RETURN_MASK_ERROR)
12546 update_global_location_list (inserting);
12547}
12548
12549/* Clear BKP from a BPS. */
12550
12551static void
12552bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12553{
12554 bpstat bs;
12555
12556 for (bs = bps; bs; bs = bs->next)
12557 if (bs->breakpoint_at == bpt)
12558 {
12559 bs->breakpoint_at = NULL;
12560 bs->old_val = NULL;
12561 /* bs->commands will be freed later. */
12562 }
12563}
12564
12565/* Callback for iterate_over_threads. */
12566static int
12567bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12568{
12569 struct breakpoint *bpt = data;
12570
12571 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12572 return 0;
12573}
12574
12575/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12576 callbacks. */
12577
12578static void
12579say_where (struct breakpoint *b)
12580{
12581 struct value_print_options opts;
12582
12583 get_user_print_options (&opts);
12584
12585 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12586 single string. */
12587 if (b->loc == NULL)
12588 {
12589 printf_filtered (_(" (%s) pending."), b->addr_string);
12590 }
12591 else
12592 {
2f202fde 12593 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12594 {
12595 printf_filtered (" at ");
12596 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12597 gdb_stdout);
12598 }
2f202fde 12599 if (b->loc->symtab != NULL)
f8eba3c6
TT
12600 {
12601 /* If there is a single location, we can print the location
12602 more nicely. */
12603 if (b->loc->next == NULL)
12604 printf_filtered (": file %s, line %d.",
05cba821
JK
12605 symtab_to_filename_for_display (b->loc->symtab),
12606 b->loc->line_number);
f8eba3c6
TT
12607 else
12608 /* This is not ideal, but each location may have a
12609 different file name, and this at least reflects the
12610 real situation somewhat. */
12611 printf_filtered (": %s.", b->addr_string);
12612 }
348d480f
PA
12613
12614 if (b->loc->next)
12615 {
12616 struct bp_location *loc = b->loc;
12617 int n = 0;
12618 for (; loc; loc = loc->next)
12619 ++n;
12620 printf_filtered (" (%d locations)", n);
12621 }
12622 }
12623}
12624
348d480f
PA
12625/* Default bp_location_ops methods. */
12626
12627static void
12628bp_location_dtor (struct bp_location *self)
12629{
12630 xfree (self->cond);
b775012e
LM
12631 if (self->cond_bytecode)
12632 free_agent_expr (self->cond_bytecode);
348d480f
PA
12633 xfree (self->function_name);
12634}
12635
12636static const struct bp_location_ops bp_location_ops =
12637{
12638 bp_location_dtor
12639};
12640
2060206e
PA
12641/* Default breakpoint_ops methods all breakpoint_ops ultimately
12642 inherit from. */
348d480f 12643
2060206e
PA
12644static void
12645base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12646{
12647 decref_counted_command_line (&self->commands);
12648 xfree (self->cond_string);
fb81d016 12649 xfree (self->extra_string);
348d480f 12650 xfree (self->addr_string);
f8eba3c6 12651 xfree (self->filter);
348d480f 12652 xfree (self->addr_string_range_end);
348d480f
PA
12653}
12654
2060206e
PA
12655static struct bp_location *
12656base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12657{
12658 struct bp_location *loc;
12659
12660 loc = XNEW (struct bp_location);
12661 init_bp_location (loc, &bp_location_ops, self);
12662 return loc;
12663}
12664
2060206e
PA
12665static void
12666base_breakpoint_re_set (struct breakpoint *b)
12667{
12668 /* Nothing to re-set. */
12669}
12670
12671#define internal_error_pure_virtual_called() \
12672 gdb_assert_not_reached ("pure virtual function called")
12673
12674static int
12675base_breakpoint_insert_location (struct bp_location *bl)
12676{
12677 internal_error_pure_virtual_called ();
12678}
12679
12680static int
12681base_breakpoint_remove_location (struct bp_location *bl)
12682{
12683 internal_error_pure_virtual_called ();
12684}
12685
12686static int
12687base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12688 struct address_space *aspace,
09ac7c10
TT
12689 CORE_ADDR bp_addr,
12690 const struct target_waitstatus *ws)
2060206e
PA
12691{
12692 internal_error_pure_virtual_called ();
12693}
12694
12695static void
12696base_breakpoint_check_status (bpstat bs)
12697{
12698 /* Always stop. */
12699}
12700
12701/* A "works_in_software_mode" breakpoint_ops method that just internal
12702 errors. */
12703
12704static int
12705base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12706{
12707 internal_error_pure_virtual_called ();
12708}
12709
12710/* A "resources_needed" breakpoint_ops method that just internal
12711 errors. */
12712
12713static int
12714base_breakpoint_resources_needed (const struct bp_location *bl)
12715{
12716 internal_error_pure_virtual_called ();
12717}
12718
12719static enum print_stop_action
12720base_breakpoint_print_it (bpstat bs)
12721{
12722 internal_error_pure_virtual_called ();
12723}
12724
12725static void
12726base_breakpoint_print_one_detail (const struct breakpoint *self,
12727 struct ui_out *uiout)
12728{
12729 /* nothing */
12730}
12731
12732static void
12733base_breakpoint_print_mention (struct breakpoint *b)
12734{
12735 internal_error_pure_virtual_called ();
12736}
12737
12738static void
12739base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12740{
12741 internal_error_pure_virtual_called ();
12742}
12743
983af33b
SDJ
12744static void
12745base_breakpoint_create_sals_from_address (char **arg,
12746 struct linespec_result *canonical,
12747 enum bptype type_wanted,
12748 char *addr_start,
12749 char **copy_arg)
12750{
12751 internal_error_pure_virtual_called ();
12752}
12753
12754static void
12755base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12756 struct linespec_result *c,
983af33b 12757 char *cond_string,
e7e0cddf 12758 char *extra_string,
983af33b
SDJ
12759 enum bptype type_wanted,
12760 enum bpdisp disposition,
12761 int thread,
12762 int task, int ignore_count,
12763 const struct breakpoint_ops *o,
12764 int from_tty, int enabled,
44f238bb 12765 int internal, unsigned flags)
983af33b
SDJ
12766{
12767 internal_error_pure_virtual_called ();
12768}
12769
12770static void
12771base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12772 struct symtabs_and_lines *sals)
12773{
12774 internal_error_pure_virtual_called ();
12775}
12776
ab04a2af
TT
12777/* The default 'explains_signal' method. */
12778
12779static enum bpstat_signal_value
427cd150 12780base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af
TT
12781{
12782 return BPSTAT_SIGNAL_HIDE;
12783}
12784
9d6e6e84
HZ
12785/* The default "after_condition_true" method. */
12786
12787static void
12788base_breakpoint_after_condition_true (struct bpstats *bs)
12789{
12790 /* Nothing to do. */
12791}
12792
ab04a2af 12793struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12794{
12795 base_breakpoint_dtor,
12796 base_breakpoint_allocate_location,
12797 base_breakpoint_re_set,
12798 base_breakpoint_insert_location,
12799 base_breakpoint_remove_location,
12800 base_breakpoint_breakpoint_hit,
12801 base_breakpoint_check_status,
12802 base_breakpoint_resources_needed,
12803 base_breakpoint_works_in_software_mode,
12804 base_breakpoint_print_it,
12805 NULL,
12806 base_breakpoint_print_one_detail,
12807 base_breakpoint_print_mention,
983af33b
SDJ
12808 base_breakpoint_print_recreate,
12809 base_breakpoint_create_sals_from_address,
12810 base_breakpoint_create_breakpoints_sal,
12811 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12812 base_breakpoint_explains_signal,
12813 base_breakpoint_after_condition_true,
2060206e
PA
12814};
12815
12816/* Default breakpoint_ops methods. */
12817
12818static void
348d480f
PA
12819bkpt_re_set (struct breakpoint *b)
12820{
06edf0c0
PA
12821 /* FIXME: is this still reachable? */
12822 if (b->addr_string == NULL)
12823 {
12824 /* Anything without a string can't be re-set. */
348d480f 12825 delete_breakpoint (b);
06edf0c0 12826 return;
348d480f 12827 }
06edf0c0
PA
12828
12829 breakpoint_re_set_default (b);
348d480f
PA
12830}
12831
2060206e 12832static int
348d480f
PA
12833bkpt_insert_location (struct bp_location *bl)
12834{
12835 if (bl->loc_type == bp_loc_hardware_breakpoint)
12836 return target_insert_hw_breakpoint (bl->gdbarch,
12837 &bl->target_info);
12838 else
12839 return target_insert_breakpoint (bl->gdbarch,
12840 &bl->target_info);
12841}
12842
2060206e 12843static int
348d480f
PA
12844bkpt_remove_location (struct bp_location *bl)
12845{
12846 if (bl->loc_type == bp_loc_hardware_breakpoint)
12847 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12848 else
12849 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12850}
12851
2060206e 12852static int
348d480f 12853bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12854 struct address_space *aspace, CORE_ADDR bp_addr,
12855 const struct target_waitstatus *ws)
348d480f 12856{
09ac7c10 12857 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12858 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12859 return 0;
12860
348d480f
PA
12861 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12862 aspace, bp_addr))
12863 return 0;
12864
12865 if (overlay_debugging /* unmapped overlay section */
12866 && section_is_overlay (bl->section)
12867 && !section_is_mapped (bl->section))
12868 return 0;
12869
12870 return 1;
12871}
12872
2060206e 12873static int
348d480f
PA
12874bkpt_resources_needed (const struct bp_location *bl)
12875{
12876 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12877
12878 return 1;
12879}
12880
2060206e 12881static enum print_stop_action
348d480f
PA
12882bkpt_print_it (bpstat bs)
12883{
348d480f
PA
12884 struct breakpoint *b;
12885 const struct bp_location *bl;
001c8c33 12886 int bp_temp;
79a45e25 12887 struct ui_out *uiout = current_uiout;
348d480f
PA
12888
12889 gdb_assert (bs->bp_location_at != NULL);
12890
12891 bl = bs->bp_location_at;
12892 b = bs->breakpoint_at;
12893
001c8c33
PA
12894 bp_temp = b->disposition == disp_del;
12895 if (bl->address != bl->requested_address)
12896 breakpoint_adjustment_warning (bl->requested_address,
12897 bl->address,
12898 b->number, 1);
12899 annotate_breakpoint (b->number);
12900 if (bp_temp)
12901 ui_out_text (uiout, "\nTemporary breakpoint ");
12902 else
12903 ui_out_text (uiout, "\nBreakpoint ");
12904 if (ui_out_is_mi_like_p (uiout))
348d480f 12905 {
001c8c33
PA
12906 ui_out_field_string (uiout, "reason",
12907 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12908 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12909 }
001c8c33
PA
12910 ui_out_field_int (uiout, "bkptno", b->number);
12911 ui_out_text (uiout, ", ");
06edf0c0 12912
001c8c33 12913 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12914}
12915
2060206e 12916static void
06edf0c0
PA
12917bkpt_print_mention (struct breakpoint *b)
12918{
79a45e25 12919 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12920 return;
12921
12922 switch (b->type)
12923 {
12924 case bp_breakpoint:
12925 case bp_gnu_ifunc_resolver:
12926 if (b->disposition == disp_del)
12927 printf_filtered (_("Temporary breakpoint"));
12928 else
12929 printf_filtered (_("Breakpoint"));
12930 printf_filtered (_(" %d"), b->number);
12931 if (b->type == bp_gnu_ifunc_resolver)
12932 printf_filtered (_(" at gnu-indirect-function resolver"));
12933 break;
12934 case bp_hardware_breakpoint:
12935 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12936 break;
e7e0cddf
SS
12937 case bp_dprintf:
12938 printf_filtered (_("Dprintf %d"), b->number);
12939 break;
06edf0c0
PA
12940 }
12941
12942 say_where (b);
12943}
12944
2060206e 12945static void
06edf0c0
PA
12946bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12947{
12948 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12949 fprintf_unfiltered (fp, "tbreak");
12950 else if (tp->type == bp_breakpoint)
12951 fprintf_unfiltered (fp, "break");
12952 else if (tp->type == bp_hardware_breakpoint
12953 && tp->disposition == disp_del)
12954 fprintf_unfiltered (fp, "thbreak");
12955 else if (tp->type == bp_hardware_breakpoint)
12956 fprintf_unfiltered (fp, "hbreak");
12957 else
12958 internal_error (__FILE__, __LINE__,
12959 _("unhandled breakpoint type %d"), (int) tp->type);
12960
2060206e 12961 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12962 print_recreate_thread (tp, fp);
06edf0c0
PA
12963}
12964
983af33b
SDJ
12965static void
12966bkpt_create_sals_from_address (char **arg,
12967 struct linespec_result *canonical,
12968 enum bptype type_wanted,
12969 char *addr_start, char **copy_arg)
12970{
12971 create_sals_from_address_default (arg, canonical, type_wanted,
12972 addr_start, copy_arg);
12973}
12974
12975static void
12976bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12977 struct linespec_result *canonical,
983af33b 12978 char *cond_string,
e7e0cddf 12979 char *extra_string,
983af33b
SDJ
12980 enum bptype type_wanted,
12981 enum bpdisp disposition,
12982 int thread,
12983 int task, int ignore_count,
12984 const struct breakpoint_ops *ops,
12985 int from_tty, int enabled,
44f238bb 12986 int internal, unsigned flags)
983af33b 12987{
023fa29b 12988 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
12989 cond_string, extra_string,
12990 type_wanted,
983af33b
SDJ
12991 disposition, thread, task,
12992 ignore_count, ops, from_tty,
44f238bb 12993 enabled, internal, flags);
983af33b
SDJ
12994}
12995
12996static void
12997bkpt_decode_linespec (struct breakpoint *b, char **s,
12998 struct symtabs_and_lines *sals)
12999{
13000 decode_linespec_default (b, s, sals);
13001}
13002
06edf0c0
PA
13003/* Virtual table for internal breakpoints. */
13004
13005static void
13006internal_bkpt_re_set (struct breakpoint *b)
13007{
13008 switch (b->type)
13009 {
13010 /* Delete overlay event and longjmp master breakpoints; they
13011 will be reset later by breakpoint_re_set. */
13012 case bp_overlay_event:
13013 case bp_longjmp_master:
13014 case bp_std_terminate_master:
13015 case bp_exception_master:
13016 delete_breakpoint (b);
13017 break;
13018
13019 /* This breakpoint is special, it's set up when the inferior
13020 starts and we really don't want to touch it. */
13021 case bp_shlib_event:
13022
13023 /* Like bp_shlib_event, this breakpoint type is special. Once
13024 it is set up, we do not want to touch it. */
13025 case bp_thread_event:
13026 break;
13027 }
13028}
13029
13030static void
13031internal_bkpt_check_status (bpstat bs)
13032{
a9b3a50f
PA
13033 if (bs->breakpoint_at->type == bp_shlib_event)
13034 {
13035 /* If requested, stop when the dynamic linker notifies GDB of
13036 events. This allows the user to get control and place
13037 breakpoints in initializer routines for dynamically loaded
13038 objects (among other things). */
13039 bs->stop = stop_on_solib_events;
13040 bs->print = stop_on_solib_events;
13041 }
13042 else
13043 bs->stop = 0;
06edf0c0
PA
13044}
13045
13046static enum print_stop_action
13047internal_bkpt_print_it (bpstat bs)
13048{
06edf0c0 13049 struct breakpoint *b;
06edf0c0 13050
06edf0c0
PA
13051 b = bs->breakpoint_at;
13052
06edf0c0
PA
13053 switch (b->type)
13054 {
348d480f
PA
13055 case bp_shlib_event:
13056 /* Did we stop because the user set the stop_on_solib_events
13057 variable? (If so, we report this as a generic, "Stopped due
13058 to shlib event" message.) */
edcc5120 13059 print_solib_event (0);
348d480f
PA
13060 break;
13061
13062 case bp_thread_event:
13063 /* Not sure how we will get here.
13064 GDB should not stop for these breakpoints. */
13065 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13066 break;
13067
13068 case bp_overlay_event:
13069 /* By analogy with the thread event, GDB should not stop for these. */
13070 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13071 break;
13072
13073 case bp_longjmp_master:
13074 /* These should never be enabled. */
13075 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13076 break;
13077
13078 case bp_std_terminate_master:
13079 /* These should never be enabled. */
13080 printf_filtered (_("std::terminate Master Breakpoint: "
13081 "gdb should not stop!\n"));
348d480f
PA
13082 break;
13083
13084 case bp_exception_master:
13085 /* These should never be enabled. */
13086 printf_filtered (_("Exception Master Breakpoint: "
13087 "gdb should not stop!\n"));
06edf0c0
PA
13088 break;
13089 }
13090
001c8c33 13091 return PRINT_NOTHING;
06edf0c0
PA
13092}
13093
13094static void
13095internal_bkpt_print_mention (struct breakpoint *b)
13096{
13097 /* Nothing to mention. These breakpoints are internal. */
13098}
13099
06edf0c0
PA
13100/* Virtual table for momentary breakpoints */
13101
13102static void
13103momentary_bkpt_re_set (struct breakpoint *b)
13104{
13105 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13106 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13107 Otherwise these should have been blown away via the cleanup chain
13108 or by breakpoint_init_inferior when we rerun the executable. */
13109}
13110
13111static void
13112momentary_bkpt_check_status (bpstat bs)
13113{
13114 /* Nothing. The point of these breakpoints is causing a stop. */
13115}
13116
13117static enum print_stop_action
13118momentary_bkpt_print_it (bpstat bs)
13119{
79a45e25
PA
13120 struct ui_out *uiout = current_uiout;
13121
001c8c33 13122 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13123 {
001c8c33 13124 struct breakpoint *b = bs->breakpoint_at;
348d480f 13125
001c8c33
PA
13126 switch (b->type)
13127 {
13128 case bp_finish:
13129 ui_out_field_string
13130 (uiout, "reason",
13131 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13132 break;
348d480f 13133
001c8c33
PA
13134 case bp_until:
13135 ui_out_field_string
13136 (uiout, "reason",
13137 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13138 break;
13139 }
348d480f
PA
13140 }
13141
001c8c33 13142 return PRINT_UNKNOWN;
348d480f
PA
13143}
13144
06edf0c0
PA
13145static void
13146momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13147{
06edf0c0 13148 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13149}
13150
e2e4d78b
JK
13151/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13152
13153 It gets cleared already on the removal of the first one of such placed
13154 breakpoints. This is OK as they get all removed altogether. */
13155
13156static void
13157longjmp_bkpt_dtor (struct breakpoint *self)
13158{
13159 struct thread_info *tp = find_thread_id (self->thread);
13160
13161 if (tp)
13162 tp->initiating_frame = null_frame_id;
13163
13164 momentary_breakpoint_ops.dtor (self);
13165}
13166
55aa24fb
SDJ
13167/* Specific methods for probe breakpoints. */
13168
13169static int
13170bkpt_probe_insert_location (struct bp_location *bl)
13171{
13172 int v = bkpt_insert_location (bl);
13173
13174 if (v == 0)
13175 {
13176 /* The insertion was successful, now let's set the probe's semaphore
13177 if needed. */
13178 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13179 }
13180
13181 return v;
13182}
13183
13184static int
13185bkpt_probe_remove_location (struct bp_location *bl)
13186{
13187 /* Let's clear the semaphore before removing the location. */
13188 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13189
13190 return bkpt_remove_location (bl);
13191}
13192
13193static void
13194bkpt_probe_create_sals_from_address (char **arg,
13195 struct linespec_result *canonical,
13196 enum bptype type_wanted,
13197 char *addr_start, char **copy_arg)
13198{
13199 struct linespec_sals lsal;
13200
13201 lsal.sals = parse_probes (arg, canonical);
13202
13203 *copy_arg = xstrdup (canonical->addr_string);
13204 lsal.canonical = xstrdup (*copy_arg);
13205
13206 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13207}
13208
13209static void
13210bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13211 struct symtabs_and_lines *sals)
13212{
13213 *sals = parse_probes (s, NULL);
13214 if (!sals->sals)
13215 error (_("probe not found"));
13216}
13217
348d480f 13218/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13219
348d480f
PA
13220static void
13221tracepoint_re_set (struct breakpoint *b)
13222{
13223 breakpoint_re_set_default (b);
13224}
876fa593 13225
348d480f
PA
13226static int
13227tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13228 struct address_space *aspace, CORE_ADDR bp_addr,
13229 const struct target_waitstatus *ws)
348d480f
PA
13230{
13231 /* By definition, the inferior does not report stops at
13232 tracepoints. */
13233 return 0;
74960c60
VP
13234}
13235
13236static void
348d480f
PA
13237tracepoint_print_one_detail (const struct breakpoint *self,
13238 struct ui_out *uiout)
74960c60 13239{
d9b3f62e
PA
13240 struct tracepoint *tp = (struct tracepoint *) self;
13241 if (tp->static_trace_marker_id)
348d480f
PA
13242 {
13243 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13244
348d480f
PA
13245 ui_out_text (uiout, "\tmarker id is ");
13246 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13247 tp->static_trace_marker_id);
348d480f
PA
13248 ui_out_text (uiout, "\n");
13249 }
0d381245
VP
13250}
13251
a474d7c2 13252static void
348d480f 13253tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13254{
79a45e25 13255 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13256 return;
cc59ec59 13257
348d480f
PA
13258 switch (b->type)
13259 {
13260 case bp_tracepoint:
13261 printf_filtered (_("Tracepoint"));
13262 printf_filtered (_(" %d"), b->number);
13263 break;
13264 case bp_fast_tracepoint:
13265 printf_filtered (_("Fast tracepoint"));
13266 printf_filtered (_(" %d"), b->number);
13267 break;
13268 case bp_static_tracepoint:
13269 printf_filtered (_("Static tracepoint"));
13270 printf_filtered (_(" %d"), b->number);
13271 break;
13272 default:
13273 internal_error (__FILE__, __LINE__,
13274 _("unhandled tracepoint type %d"), (int) b->type);
13275 }
13276
13277 say_where (b);
a474d7c2
PA
13278}
13279
348d480f 13280static void
d9b3f62e 13281tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13282{
d9b3f62e
PA
13283 struct tracepoint *tp = (struct tracepoint *) self;
13284
13285 if (self->type == bp_fast_tracepoint)
348d480f 13286 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13287 if (self->type == bp_static_tracepoint)
348d480f 13288 fprintf_unfiltered (fp, "strace");
d9b3f62e 13289 else if (self->type == bp_tracepoint)
348d480f
PA
13290 fprintf_unfiltered (fp, "trace");
13291 else
13292 internal_error (__FILE__, __LINE__,
d9b3f62e 13293 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13294
d9b3f62e
PA
13295 fprintf_unfiltered (fp, " %s", self->addr_string);
13296 print_recreate_thread (self, fp);
13297
13298 if (tp->pass_count)
13299 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13300}
13301
983af33b
SDJ
13302static void
13303tracepoint_create_sals_from_address (char **arg,
13304 struct linespec_result *canonical,
13305 enum bptype type_wanted,
13306 char *addr_start, char **copy_arg)
13307{
13308 create_sals_from_address_default (arg, canonical, type_wanted,
13309 addr_start, copy_arg);
13310}
13311
13312static void
13313tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13314 struct linespec_result *canonical,
983af33b 13315 char *cond_string,
e7e0cddf 13316 char *extra_string,
983af33b
SDJ
13317 enum bptype type_wanted,
13318 enum bpdisp disposition,
13319 int thread,
13320 int task, int ignore_count,
13321 const struct breakpoint_ops *ops,
13322 int from_tty, int enabled,
44f238bb 13323 int internal, unsigned flags)
983af33b 13324{
023fa29b 13325 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13326 cond_string, extra_string,
13327 type_wanted,
983af33b
SDJ
13328 disposition, thread, task,
13329 ignore_count, ops, from_tty,
44f238bb 13330 enabled, internal, flags);
983af33b
SDJ
13331}
13332
13333static void
13334tracepoint_decode_linespec (struct breakpoint *b, char **s,
13335 struct symtabs_and_lines *sals)
13336{
13337 decode_linespec_default (b, s, sals);
13338}
13339
2060206e 13340struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13341
55aa24fb
SDJ
13342/* The breakpoint_ops structure to be use on tracepoints placed in a
13343 static probe. */
13344
13345static void
13346tracepoint_probe_create_sals_from_address (char **arg,
13347 struct linespec_result *canonical,
13348 enum bptype type_wanted,
13349 char *addr_start, char **copy_arg)
13350{
13351 /* We use the same method for breakpoint on probes. */
13352 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13353 addr_start, copy_arg);
13354}
13355
13356static void
13357tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13358 struct symtabs_and_lines *sals)
13359{
13360 /* We use the same method for breakpoint on probes. */
13361 bkpt_probe_decode_linespec (b, s, sals);
13362}
13363
13364static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13365
5c2b4418
HZ
13366/* Dprintf breakpoint_ops methods. */
13367
13368static void
13369dprintf_re_set (struct breakpoint *b)
13370{
13371 breakpoint_re_set_default (b);
13372
13373 /* This breakpoint could have been pending, and be resolved now, and
13374 if so, we should now have the extra string. If we don't, the
13375 dprintf was malformed when created, but we couldn't tell because
13376 we can't extract the extra string until the location is
13377 resolved. */
13378 if (b->loc != NULL && b->extra_string == NULL)
13379 error (_("Format string required"));
13380
13381 /* 1 - connect to target 1, that can run breakpoint commands.
13382 2 - create a dprintf, which resolves fine.
13383 3 - disconnect from target 1
13384 4 - connect to target 2, that can NOT run breakpoint commands.
13385
13386 After steps #3/#4, you'll want the dprintf command list to
13387 be updated, because target 1 and 2 may well return different
13388 answers for target_can_run_breakpoint_commands().
13389 Given absence of finer grained resetting, we get to do
13390 it all the time. */
13391 if (b->extra_string != NULL)
13392 update_dprintf_command_list (b);
13393}
13394
2d9442cc
HZ
13395/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13396
13397static void
13398dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13399{
13400 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13401 tp->extra_string);
13402 print_recreate_thread (tp, fp);
13403}
13404
9d6e6e84
HZ
13405/* Implement the "after_condition_true" breakpoint_ops method for
13406 dprintf.
13407
13408 dprintf's are implemented with regular commands in their command
13409 list, but we run the commands here instead of before presenting the
13410 stop to the user, as dprintf's don't actually cause a stop. This
13411 also makes it so that the commands of multiple dprintfs at the same
13412 address are all handled. */
13413
13414static void
13415dprintf_after_condition_true (struct bpstats *bs)
13416{
13417 struct cleanup *old_chain;
13418 struct bpstats tmp_bs = { NULL };
13419 struct bpstats *tmp_bs_p = &tmp_bs;
13420
13421 /* dprintf's never cause a stop. This wasn't set in the
13422 check_status hook instead because that would make the dprintf's
13423 condition not be evaluated. */
13424 bs->stop = 0;
13425
13426 /* Run the command list here. Take ownership of it instead of
13427 copying. We never want these commands to run later in
13428 bpstat_do_actions, if a breakpoint that causes a stop happens to
13429 be set at same address as this dprintf, or even if running the
13430 commands here throws. */
13431 tmp_bs.commands = bs->commands;
13432 bs->commands = NULL;
13433 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13434
13435 bpstat_do_actions_1 (&tmp_bs_p);
13436
13437 /* 'tmp_bs.commands' will usually be NULL by now, but
13438 bpstat_do_actions_1 may return early without processing the whole
13439 list. */
13440 do_cleanups (old_chain);
13441}
13442
983af33b
SDJ
13443/* The breakpoint_ops structure to be used on static tracepoints with
13444 markers (`-m'). */
13445
13446static void
13447strace_marker_create_sals_from_address (char **arg,
13448 struct linespec_result *canonical,
13449 enum bptype type_wanted,
13450 char *addr_start, char **copy_arg)
13451{
13452 struct linespec_sals lsal;
13453
13454 lsal.sals = decode_static_tracepoint_spec (arg);
13455
13456 *copy_arg = savestring (addr_start, *arg - addr_start);
13457
13458 canonical->addr_string = xstrdup (*copy_arg);
13459 lsal.canonical = xstrdup (*copy_arg);
13460 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13461}
13462
13463static void
13464strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13465 struct linespec_result *canonical,
983af33b 13466 char *cond_string,
e7e0cddf 13467 char *extra_string,
983af33b
SDJ
13468 enum bptype type_wanted,
13469 enum bpdisp disposition,
13470 int thread,
13471 int task, int ignore_count,
13472 const struct breakpoint_ops *ops,
13473 int from_tty, int enabled,
44f238bb 13474 int internal, unsigned flags)
983af33b
SDJ
13475{
13476 int i;
52d361e1
YQ
13477 struct linespec_sals *lsal = VEC_index (linespec_sals,
13478 canonical->sals, 0);
983af33b
SDJ
13479
13480 /* If the user is creating a static tracepoint by marker id
13481 (strace -m MARKER_ID), then store the sals index, so that
13482 breakpoint_re_set can try to match up which of the newly
13483 found markers corresponds to this one, and, don't try to
13484 expand multiple locations for each sal, given than SALS
13485 already should contain all sals for MARKER_ID. */
13486
13487 for (i = 0; i < lsal->sals.nelts; ++i)
13488 {
13489 struct symtabs_and_lines expanded;
13490 struct tracepoint *tp;
13491 struct cleanup *old_chain;
13492 char *addr_string;
13493
13494 expanded.nelts = 1;
13495 expanded.sals = &lsal->sals.sals[i];
13496
13497 addr_string = xstrdup (canonical->addr_string);
13498 old_chain = make_cleanup (xfree, addr_string);
13499
13500 tp = XCNEW (struct tracepoint);
13501 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13502 addr_string, NULL,
e7e0cddf
SS
13503 cond_string, extra_string,
13504 type_wanted, disposition,
983af33b 13505 thread, task, ignore_count, ops,
44f238bb 13506 from_tty, enabled, internal, flags,
983af33b
SDJ
13507 canonical->special_display);
13508 /* Given that its possible to have multiple markers with
13509 the same string id, if the user is creating a static
13510 tracepoint by marker id ("strace -m MARKER_ID"), then
13511 store the sals index, so that breakpoint_re_set can
13512 try to match up which of the newly found markers
13513 corresponds to this one */
13514 tp->static_trace_marker_id_idx = i;
13515
13516 install_breakpoint (internal, &tp->base, 0);
13517
13518 discard_cleanups (old_chain);
13519 }
13520}
13521
13522static void
13523strace_marker_decode_linespec (struct breakpoint *b, char **s,
13524 struct symtabs_and_lines *sals)
13525{
13526 struct tracepoint *tp = (struct tracepoint *) b;
13527
13528 *sals = decode_static_tracepoint_spec (s);
13529 if (sals->nelts > tp->static_trace_marker_id_idx)
13530 {
13531 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13532 sals->nelts = 1;
13533 }
13534 else
13535 error (_("marker %s not found"), tp->static_trace_marker_id);
13536}
13537
13538static struct breakpoint_ops strace_marker_breakpoint_ops;
13539
13540static int
13541strace_marker_p (struct breakpoint *b)
13542{
13543 return b->ops == &strace_marker_breakpoint_ops;
13544}
13545
53a5351d 13546/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13547 structures. */
c906108c
SS
13548
13549void
fba45db2 13550delete_breakpoint (struct breakpoint *bpt)
c906108c 13551{
52f0bd74 13552 struct breakpoint *b;
c906108c 13553
8a3fe4f8 13554 gdb_assert (bpt != NULL);
c906108c 13555
4a64f543
MS
13556 /* Has this bp already been deleted? This can happen because
13557 multiple lists can hold pointers to bp's. bpstat lists are
13558 especial culprits.
13559
13560 One example of this happening is a watchpoint's scope bp. When
13561 the scope bp triggers, we notice that the watchpoint is out of
13562 scope, and delete it. We also delete its scope bp. But the
13563 scope bp is marked "auto-deleting", and is already on a bpstat.
13564 That bpstat is then checked for auto-deleting bp's, which are
13565 deleted.
13566
13567 A real solution to this problem might involve reference counts in
13568 bp's, and/or giving them pointers back to their referencing
13569 bpstat's, and teaching delete_breakpoint to only free a bp's
13570 storage when no more references were extent. A cheaper bandaid
13571 was chosen. */
c906108c
SS
13572 if (bpt->type == bp_none)
13573 return;
13574
4a64f543
MS
13575 /* At least avoid this stale reference until the reference counting
13576 of breakpoints gets resolved. */
d0fb5eae 13577 if (bpt->related_breakpoint != bpt)
e5a0a904 13578 {
d0fb5eae 13579 struct breakpoint *related;
3a5c3e22 13580 struct watchpoint *w;
d0fb5eae
JK
13581
13582 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13583 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13584 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13585 w = (struct watchpoint *) bpt;
13586 else
13587 w = NULL;
13588 if (w != NULL)
13589 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13590
13591 /* Unlink bpt from the bpt->related_breakpoint ring. */
13592 for (related = bpt; related->related_breakpoint != bpt;
13593 related = related->related_breakpoint);
13594 related->related_breakpoint = bpt->related_breakpoint;
13595 bpt->related_breakpoint = bpt;
e5a0a904
JK
13596 }
13597
a9634178
TJB
13598 /* watch_command_1 creates a watchpoint but only sets its number if
13599 update_watchpoint succeeds in creating its bp_locations. If there's
13600 a problem in that process, we'll be asked to delete the half-created
13601 watchpoint. In that case, don't announce the deletion. */
13602 if (bpt->number)
13603 observer_notify_breakpoint_deleted (bpt);
c906108c 13604
c906108c
SS
13605 if (breakpoint_chain == bpt)
13606 breakpoint_chain = bpt->next;
13607
c906108c
SS
13608 ALL_BREAKPOINTS (b)
13609 if (b->next == bpt)
c5aa993b
JM
13610 {
13611 b->next = bpt->next;
13612 break;
13613 }
c906108c 13614
f431efe5
PA
13615 /* Be sure no bpstat's are pointing at the breakpoint after it's
13616 been freed. */
13617 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13618 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13619 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13620 commands are associated with the bpstat; if we remove it here,
13621 then the later call to bpstat_do_actions (&stop_bpstat); in
13622 event-top.c won't do anything, and temporary breakpoints with
13623 commands won't work. */
13624
13625 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13626
4a64f543
MS
13627 /* Now that breakpoint is removed from breakpoint list, update the
13628 global location list. This will remove locations that used to
13629 belong to this breakpoint. Do this before freeing the breakpoint
13630 itself, since remove_breakpoint looks at location's owner. It
13631 might be better design to have location completely
13632 self-contained, but it's not the case now. */
b60e7edf 13633 update_global_location_list (0);
74960c60 13634
348d480f 13635 bpt->ops->dtor (bpt);
4a64f543
MS
13636 /* On the chance that someone will soon try again to delete this
13637 same bp, we mark it as deleted before freeing its storage. */
c906108c 13638 bpt->type = bp_none;
b8c9b27d 13639 xfree (bpt);
c906108c
SS
13640}
13641
4d6140d9
AC
13642static void
13643do_delete_breakpoint_cleanup (void *b)
13644{
13645 delete_breakpoint (b);
13646}
13647
13648struct cleanup *
13649make_cleanup_delete_breakpoint (struct breakpoint *b)
13650{
13651 return make_cleanup (do_delete_breakpoint_cleanup, b);
13652}
13653
51be5b68
PA
13654/* Iterator function to call a user-provided callback function once
13655 for each of B and its related breakpoints. */
13656
13657static void
13658iterate_over_related_breakpoints (struct breakpoint *b,
13659 void (*function) (struct breakpoint *,
13660 void *),
13661 void *data)
13662{
13663 struct breakpoint *related;
13664
13665 related = b;
13666 do
13667 {
13668 struct breakpoint *next;
13669
13670 /* FUNCTION may delete RELATED. */
13671 next = related->related_breakpoint;
13672
13673 if (next == related)
13674 {
13675 /* RELATED is the last ring entry. */
13676 function (related, data);
13677
13678 /* FUNCTION may have deleted it, so we'd never reach back to
13679 B. There's nothing left to do anyway, so just break
13680 out. */
13681 break;
13682 }
13683 else
13684 function (related, data);
13685
13686 related = next;
13687 }
13688 while (related != b);
13689}
95a42b64
TT
13690
13691static void
13692do_delete_breakpoint (struct breakpoint *b, void *ignore)
13693{
13694 delete_breakpoint (b);
13695}
13696
51be5b68
PA
13697/* A callback for map_breakpoint_numbers that calls
13698 delete_breakpoint. */
13699
13700static void
13701do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13702{
13703 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13704}
13705
c906108c 13706void
fba45db2 13707delete_command (char *arg, int from_tty)
c906108c 13708{
35df4500 13709 struct breakpoint *b, *b_tmp;
c906108c 13710
ea9365bb
TT
13711 dont_repeat ();
13712
c906108c
SS
13713 if (arg == 0)
13714 {
13715 int breaks_to_delete = 0;
13716
46c6471b
PA
13717 /* Delete all breakpoints if no argument. Do not delete
13718 internal breakpoints, these have to be deleted with an
13719 explicit breakpoint number argument. */
c5aa993b 13720 ALL_BREAKPOINTS (b)
46c6471b 13721 if (user_breakpoint_p (b))
973d738b
DJ
13722 {
13723 breaks_to_delete = 1;
13724 break;
13725 }
c906108c
SS
13726
13727 /* Ask user only if there are some breakpoints to delete. */
13728 if (!from_tty
e2e0b3e5 13729 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13730 {
35df4500 13731 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13732 if (user_breakpoint_p (b))
c5aa993b 13733 delete_breakpoint (b);
c906108c
SS
13734 }
13735 }
13736 else
51be5b68 13737 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13738}
13739
0d381245
VP
13740static int
13741all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13742{
0d381245 13743 for (; loc; loc = loc->next)
8645ff69
UW
13744 if (!loc->shlib_disabled
13745 && !loc->pspace->executing_startup)
0d381245
VP
13746 return 0;
13747 return 1;
fe3f5fa8
VP
13748}
13749
776592bf
DE
13750/* Subroutine of update_breakpoint_locations to simplify it.
13751 Return non-zero if multiple fns in list LOC have the same name.
13752 Null names are ignored. */
13753
13754static int
13755ambiguous_names_p (struct bp_location *loc)
13756{
13757 struct bp_location *l;
13758 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13759 (int (*) (const void *,
13760 const void *)) streq,
776592bf
DE
13761 NULL, xcalloc, xfree);
13762
13763 for (l = loc; l != NULL; l = l->next)
13764 {
13765 const char **slot;
13766 const char *name = l->function_name;
13767
13768 /* Allow for some names to be NULL, ignore them. */
13769 if (name == NULL)
13770 continue;
13771
13772 slot = (const char **) htab_find_slot (htab, (const void *) name,
13773 INSERT);
4a64f543
MS
13774 /* NOTE: We can assume slot != NULL here because xcalloc never
13775 returns NULL. */
776592bf
DE
13776 if (*slot != NULL)
13777 {
13778 htab_delete (htab);
13779 return 1;
13780 }
13781 *slot = name;
13782 }
13783
13784 htab_delete (htab);
13785 return 0;
13786}
13787
0fb4aa4b
PA
13788/* When symbols change, it probably means the sources changed as well,
13789 and it might mean the static tracepoint markers are no longer at
13790 the same address or line numbers they used to be at last we
13791 checked. Losing your static tracepoints whenever you rebuild is
13792 undesirable. This function tries to resync/rematch gdb static
13793 tracepoints with the markers on the target, for static tracepoints
13794 that have not been set by marker id. Static tracepoint that have
13795 been set by marker id are reset by marker id in breakpoint_re_set.
13796 The heuristic is:
13797
13798 1) For a tracepoint set at a specific address, look for a marker at
13799 the old PC. If one is found there, assume to be the same marker.
13800 If the name / string id of the marker found is different from the
13801 previous known name, assume that means the user renamed the marker
13802 in the sources, and output a warning.
13803
13804 2) For a tracepoint set at a given line number, look for a marker
13805 at the new address of the old line number. If one is found there,
13806 assume to be the same marker. If the name / string id of the
13807 marker found is different from the previous known name, assume that
13808 means the user renamed the marker in the sources, and output a
13809 warning.
13810
13811 3) If a marker is no longer found at the same address or line, it
13812 may mean the marker no longer exists. But it may also just mean
13813 the code changed a bit. Maybe the user added a few lines of code
13814 that made the marker move up or down (in line number terms). Ask
13815 the target for info about the marker with the string id as we knew
13816 it. If found, update line number and address in the matching
13817 static tracepoint. This will get confused if there's more than one
13818 marker with the same ID (possible in UST, although unadvised
13819 precisely because it confuses tools). */
13820
13821static struct symtab_and_line
13822update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13823{
d9b3f62e 13824 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13825 struct static_tracepoint_marker marker;
13826 CORE_ADDR pc;
0fb4aa4b
PA
13827
13828 pc = sal.pc;
13829 if (sal.line)
13830 find_line_pc (sal.symtab, sal.line, &pc);
13831
13832 if (target_static_tracepoint_marker_at (pc, &marker))
13833 {
d9b3f62e 13834 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13835 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13836 b->number,
d9b3f62e 13837 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13838
d9b3f62e
PA
13839 xfree (tp->static_trace_marker_id);
13840 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13841 release_static_tracepoint_marker (&marker);
13842
13843 return sal;
13844 }
13845
13846 /* Old marker wasn't found on target at lineno. Try looking it up
13847 by string ID. */
13848 if (!sal.explicit_pc
13849 && sal.line != 0
13850 && sal.symtab != NULL
d9b3f62e 13851 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13852 {
13853 VEC(static_tracepoint_marker_p) *markers;
13854
13855 markers
d9b3f62e 13856 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13857
13858 if (!VEC_empty(static_tracepoint_marker_p, markers))
13859 {
80e1d417 13860 struct symtab_and_line sal2;
0fb4aa4b 13861 struct symbol *sym;
80e1d417 13862 struct static_tracepoint_marker *tpmarker;
79a45e25 13863 struct ui_out *uiout = current_uiout;
0fb4aa4b 13864
80e1d417 13865 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13866
d9b3f62e 13867 xfree (tp->static_trace_marker_id);
80e1d417 13868 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13869
13870 warning (_("marker for static tracepoint %d (%s) not "
13871 "found at previous line number"),
d9b3f62e 13872 b->number, tp->static_trace_marker_id);
0fb4aa4b 13873
80e1d417 13874 init_sal (&sal2);
0fb4aa4b 13875
80e1d417 13876 sal2.pc = tpmarker->address;
0fb4aa4b 13877
80e1d417
AS
13878 sal2 = find_pc_line (tpmarker->address, 0);
13879 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13880 ui_out_text (uiout, "Now in ");
13881 if (sym)
13882 {
13883 ui_out_field_string (uiout, "func",
13884 SYMBOL_PRINT_NAME (sym));
13885 ui_out_text (uiout, " at ");
13886 }
05cba821
JK
13887 ui_out_field_string (uiout, "file",
13888 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13889 ui_out_text (uiout, ":");
13890
13891 if (ui_out_is_mi_like_p (uiout))
13892 {
0b0865da 13893 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13894
f35a17b5 13895 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13896 }
13897
80e1d417 13898 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13899 ui_out_text (uiout, "\n");
13900
80e1d417 13901 b->loc->line_number = sal2.line;
2f202fde 13902 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
13903
13904 xfree (b->addr_string);
13905 b->addr_string = xstrprintf ("%s:%d",
05cba821 13906 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 13907 b->loc->line_number);
0fb4aa4b
PA
13908
13909 /* Might be nice to check if function changed, and warn if
13910 so. */
13911
80e1d417 13912 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13913 }
13914 }
13915 return sal;
13916}
13917
8d3788bd
VP
13918/* Returns 1 iff locations A and B are sufficiently same that
13919 we don't need to report breakpoint as changed. */
13920
13921static int
13922locations_are_equal (struct bp_location *a, struct bp_location *b)
13923{
13924 while (a && b)
13925 {
13926 if (a->address != b->address)
13927 return 0;
13928
13929 if (a->shlib_disabled != b->shlib_disabled)
13930 return 0;
13931
13932 if (a->enabled != b->enabled)
13933 return 0;
13934
13935 a = a->next;
13936 b = b->next;
13937 }
13938
13939 if ((a == NULL) != (b == NULL))
13940 return 0;
13941
13942 return 1;
13943}
13944
f1310107
TJB
13945/* Create new breakpoint locations for B (a hardware or software breakpoint)
13946 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13947 a ranged breakpoint. */
13948
0e30163f 13949void
0d381245 13950update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13951 struct symtabs_and_lines sals,
13952 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13953{
13954 int i;
0d381245
VP
13955 struct bp_location *existing_locations = b->loc;
13956
f8eba3c6
TT
13957 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13958 {
13959 /* Ranged breakpoints have only one start location and one end
13960 location. */
13961 b->enable_state = bp_disabled;
13962 update_global_location_list (1);
13963 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13964 "multiple locations found\n"),
13965 b->number);
13966 return;
13967 }
f1310107 13968
4a64f543
MS
13969 /* If there's no new locations, and all existing locations are
13970 pending, don't do anything. This optimizes the common case where
13971 all locations are in the same shared library, that was unloaded.
13972 We'd like to retain the location, so that when the library is
13973 loaded again, we don't loose the enabled/disabled status of the
13974 individual locations. */
0d381245 13975 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13976 return;
13977
fe3f5fa8
VP
13978 b->loc = NULL;
13979
0d381245 13980 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13981 {
f8eba3c6
TT
13982 struct bp_location *new_loc;
13983
13984 switch_to_program_space_and_thread (sals.sals[i].pspace);
13985
13986 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13987
0d381245
VP
13988 /* Reparse conditions, they might contain references to the
13989 old symtab. */
13990 if (b->cond_string != NULL)
13991 {
bbc13ae3 13992 const char *s;
bfd189b1 13993 volatile struct gdb_exception e;
fe3f5fa8 13994
0d381245
VP
13995 s = b->cond_string;
13996 TRY_CATCH (e, RETURN_MASK_ERROR)
13997 {
1bb9788d
TT
13998 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
13999 block_for_pc (sals.sals[i].pc),
0d381245
VP
14000 0);
14001 }
14002 if (e.reason < 0)
14003 {
3e43a32a
MS
14004 warning (_("failed to reevaluate condition "
14005 "for breakpoint %d: %s"),
0d381245
VP
14006 b->number, e.message);
14007 new_loc->enabled = 0;
14008 }
14009 }
fe3f5fa8 14010
f1310107
TJB
14011 if (sals_end.nelts)
14012 {
14013 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14014
14015 new_loc->length = end - sals.sals[0].pc + 1;
14016 }
0d381245 14017 }
fe3f5fa8 14018
514f746b
AR
14019 /* Update locations of permanent breakpoints. */
14020 if (b->enable_state == bp_permanent)
14021 make_breakpoint_permanent (b);
14022
4a64f543
MS
14023 /* If possible, carry over 'disable' status from existing
14024 breakpoints. */
0d381245
VP
14025 {
14026 struct bp_location *e = existing_locations;
776592bf
DE
14027 /* If there are multiple breakpoints with the same function name,
14028 e.g. for inline functions, comparing function names won't work.
14029 Instead compare pc addresses; this is just a heuristic as things
14030 may have moved, but in practice it gives the correct answer
14031 often enough until a better solution is found. */
14032 int have_ambiguous_names = ambiguous_names_p (b->loc);
14033
0d381245
VP
14034 for (; e; e = e->next)
14035 {
14036 if (!e->enabled && e->function_name)
14037 {
14038 struct bp_location *l = b->loc;
776592bf
DE
14039 if (have_ambiguous_names)
14040 {
14041 for (; l; l = l->next)
f1310107 14042 if (breakpoint_locations_match (e, l))
776592bf
DE
14043 {
14044 l->enabled = 0;
14045 break;
14046 }
14047 }
14048 else
14049 {
14050 for (; l; l = l->next)
14051 if (l->function_name
14052 && strcmp (e->function_name, l->function_name) == 0)
14053 {
14054 l->enabled = 0;
14055 break;
14056 }
14057 }
0d381245
VP
14058 }
14059 }
14060 }
fe3f5fa8 14061
8d3788bd
VP
14062 if (!locations_are_equal (existing_locations, b->loc))
14063 observer_notify_breakpoint_modified (b);
14064
b60e7edf 14065 update_global_location_list (1);
fe3f5fa8
VP
14066}
14067
ef23e705
TJB
14068/* Find the SaL locations corresponding to the given ADDR_STRING.
14069 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14070
14071static struct symtabs_and_lines
14072addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14073{
14074 char *s;
02d20e4a 14075 struct symtabs_and_lines sals = {0};
f8eba3c6 14076 volatile struct gdb_exception e;
ef23e705 14077
983af33b 14078 gdb_assert (b->ops != NULL);
ef23e705 14079 s = addr_string;
ef23e705
TJB
14080
14081 TRY_CATCH (e, RETURN_MASK_ERROR)
14082 {
983af33b 14083 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14084 }
14085 if (e.reason < 0)
14086 {
14087 int not_found_and_ok = 0;
14088 /* For pending breakpoints, it's expected that parsing will
14089 fail until the right shared library is loaded. User has
14090 already told to create pending breakpoints and don't need
14091 extra messages. If breakpoint is in bp_shlib_disabled
14092 state, then user already saw the message about that
14093 breakpoint being disabled, and don't want to see more
14094 errors. */
58438ac1 14095 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14096 && (b->condition_not_parsed
14097 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14098 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14099 || b->enable_state == bp_disabled))
14100 not_found_and_ok = 1;
14101
14102 if (!not_found_and_ok)
14103 {
14104 /* We surely don't want to warn about the same breakpoint
14105 10 times. One solution, implemented here, is disable
14106 the breakpoint on error. Another solution would be to
14107 have separate 'warning emitted' flag. Since this
14108 happens only when a binary has changed, I don't know
14109 which approach is better. */
14110 b->enable_state = bp_disabled;
14111 throw_exception (e);
14112 }
14113 }
14114
58438ac1 14115 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14116 {
f8eba3c6 14117 int i;
ef23e705 14118
f8eba3c6
TT
14119 for (i = 0; i < sals.nelts; ++i)
14120 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14121 if (b->condition_not_parsed && s && s[0])
14122 {
ed1d1739
KS
14123 char *cond_string, *extra_string;
14124 int thread, task;
ef23e705
TJB
14125
14126 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14127 &cond_string, &thread, &task,
14128 &extra_string);
ef23e705
TJB
14129 if (cond_string)
14130 b->cond_string = cond_string;
14131 b->thread = thread;
14132 b->task = task;
e7e0cddf
SS
14133 if (extra_string)
14134 b->extra_string = extra_string;
ef23e705
TJB
14135 b->condition_not_parsed = 0;
14136 }
14137
983af33b 14138 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14139 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14140
58438ac1
TT
14141 *found = 1;
14142 }
14143 else
14144 *found = 0;
ef23e705
TJB
14145
14146 return sals;
14147}
14148
348d480f
PA
14149/* The default re_set method, for typical hardware or software
14150 breakpoints. Reevaluate the breakpoint and recreate its
14151 locations. */
14152
14153static void
28010a5d 14154breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14155{
14156 int found;
f1310107 14157 struct symtabs_and_lines sals, sals_end;
ef23e705 14158 struct symtabs_and_lines expanded = {0};
f1310107 14159 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14160
14161 sals = addr_string_to_sals (b, b->addr_string, &found);
14162 if (found)
14163 {
14164 make_cleanup (xfree, sals.sals);
f8eba3c6 14165 expanded = sals;
ef23e705
TJB
14166 }
14167
f1310107
TJB
14168 if (b->addr_string_range_end)
14169 {
14170 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14171 if (found)
14172 {
14173 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14174 expanded_end = sals_end;
f1310107
TJB
14175 }
14176 }
14177
14178 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14179}
14180
983af33b
SDJ
14181/* Default method for creating SALs from an address string. It basically
14182 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14183
14184static void
14185create_sals_from_address_default (char **arg,
14186 struct linespec_result *canonical,
14187 enum bptype type_wanted,
14188 char *addr_start, char **copy_arg)
14189{
14190 parse_breakpoint_sals (arg, canonical);
14191}
14192
14193/* Call create_breakpoints_sal for the given arguments. This is the default
14194 function for the `create_breakpoints_sal' method of
14195 breakpoint_ops. */
14196
14197static void
14198create_breakpoints_sal_default (struct gdbarch *gdbarch,
14199 struct linespec_result *canonical,
983af33b 14200 char *cond_string,
e7e0cddf 14201 char *extra_string,
983af33b
SDJ
14202 enum bptype type_wanted,
14203 enum bpdisp disposition,
14204 int thread,
14205 int task, int ignore_count,
14206 const struct breakpoint_ops *ops,
14207 int from_tty, int enabled,
44f238bb 14208 int internal, unsigned flags)
983af33b
SDJ
14209{
14210 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14211 extra_string,
983af33b
SDJ
14212 type_wanted, disposition,
14213 thread, task, ignore_count, ops, from_tty,
44f238bb 14214 enabled, internal, flags);
983af33b
SDJ
14215}
14216
14217/* Decode the line represented by S by calling decode_line_full. This is the
14218 default function for the `decode_linespec' method of breakpoint_ops. */
14219
14220static void
14221decode_linespec_default (struct breakpoint *b, char **s,
14222 struct symtabs_and_lines *sals)
14223{
14224 struct linespec_result canonical;
14225
14226 init_linespec_result (&canonical);
14227 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14228 (struct symtab *) NULL, 0,
14229 &canonical, multiple_symbols_all,
14230 b->filter);
14231
14232 /* We should get 0 or 1 resulting SALs. */
14233 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14234
14235 if (VEC_length (linespec_sals, canonical.sals) > 0)
14236 {
14237 struct linespec_sals *lsal;
14238
14239 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14240 *sals = lsal->sals;
14241 /* Arrange it so the destructor does not free the
14242 contents. */
14243 lsal->sals.sals = NULL;
14244 }
14245
14246 destroy_linespec_result (&canonical);
14247}
14248
28010a5d
PA
14249/* Prepare the global context for a re-set of breakpoint B. */
14250
14251static struct cleanup *
14252prepare_re_set_context (struct breakpoint *b)
14253{
14254 struct cleanup *cleanups;
14255
14256 input_radix = b->input_radix;
14257 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14258 if (b->pspace != NULL)
14259 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14260 set_language (b->language);
14261
14262 return cleanups;
ef23e705
TJB
14263}
14264
c906108c
SS
14265/* Reset a breakpoint given it's struct breakpoint * BINT.
14266 The value we return ends up being the return value from catch_errors.
14267 Unused in this case. */
14268
14269static int
4efb68b1 14270breakpoint_re_set_one (void *bint)
c906108c 14271{
4a64f543 14272 /* Get past catch_errs. */
53a5351d 14273 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14274 struct cleanup *cleanups;
c906108c 14275
348d480f
PA
14276 cleanups = prepare_re_set_context (b);
14277 b->ops->re_set (b);
14278 do_cleanups (cleanups);
c906108c
SS
14279 return 0;
14280}
14281
69de3c6a 14282/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14283void
69de3c6a 14284breakpoint_re_set (void)
c906108c 14285{
35df4500 14286 struct breakpoint *b, *b_tmp;
c906108c
SS
14287 enum language save_language;
14288 int save_input_radix;
6c95b8df 14289 struct cleanup *old_chain;
c5aa993b 14290
c906108c
SS
14291 save_language = current_language->la_language;
14292 save_input_radix = input_radix;
6c95b8df
PA
14293 old_chain = save_current_program_space ();
14294
35df4500 14295 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14296 {
4a64f543 14297 /* Format possible error msg. */
fe3f5fa8 14298 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14299 b->number);
14300 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14301 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14302 do_cleanups (cleanups);
c5aa993b 14303 }
c906108c
SS
14304 set_language (save_language);
14305 input_radix = save_input_radix;
e62c965a 14306
0756c555 14307 jit_breakpoint_re_set ();
4efc6507 14308
6c95b8df
PA
14309 do_cleanups (old_chain);
14310
af02033e
PP
14311 create_overlay_event_breakpoint ();
14312 create_longjmp_master_breakpoint ();
14313 create_std_terminate_master_breakpoint ();
186c406b 14314 create_exception_master_breakpoint ();
c906108c
SS
14315}
14316\f
c906108c
SS
14317/* Reset the thread number of this breakpoint:
14318
14319 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14320 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14321void
fba45db2 14322breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14323{
14324 if (b->thread != -1)
14325 {
39f77062
KB
14326 if (in_thread_list (inferior_ptid))
14327 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14328
14329 /* We're being called after following a fork. The new fork is
14330 selected as current, and unless this was a vfork will have a
14331 different program space from the original thread. Reset that
14332 as well. */
14333 b->loc->pspace = current_program_space;
c906108c
SS
14334 }
14335}
14336
03ac34d5
MS
14337/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14338 If from_tty is nonzero, it prints a message to that effect,
14339 which ends with a period (no newline). */
14340
c906108c 14341void
fba45db2 14342set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14343{
52f0bd74 14344 struct breakpoint *b;
c906108c
SS
14345
14346 if (count < 0)
14347 count = 0;
14348
14349 ALL_BREAKPOINTS (b)
14350 if (b->number == bptnum)
c5aa993b 14351 {
d77f58be
SS
14352 if (is_tracepoint (b))
14353 {
14354 if (from_tty && count != 0)
14355 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14356 bptnum);
14357 return;
14358 }
14359
c5aa993b 14360 b->ignore_count = count;
221ea385
KS
14361 if (from_tty)
14362 {
14363 if (count == 0)
3e43a32a
MS
14364 printf_filtered (_("Will stop next time "
14365 "breakpoint %d is reached."),
221ea385
KS
14366 bptnum);
14367 else if (count == 1)
a3f17187 14368 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14369 bptnum);
14370 else
3e43a32a
MS
14371 printf_filtered (_("Will ignore next %d "
14372 "crossings of breakpoint %d."),
221ea385
KS
14373 count, bptnum);
14374 }
8d3788bd 14375 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14376 return;
14377 }
c906108c 14378
8a3fe4f8 14379 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14380}
14381
c906108c
SS
14382/* Command to set ignore-count of breakpoint N to COUNT. */
14383
14384static void
fba45db2 14385ignore_command (char *args, int from_tty)
c906108c
SS
14386{
14387 char *p = args;
52f0bd74 14388 int num;
c906108c
SS
14389
14390 if (p == 0)
e2e0b3e5 14391 error_no_arg (_("a breakpoint number"));
c5aa993b 14392
c906108c 14393 num = get_number (&p);
5c44784c 14394 if (num == 0)
8a3fe4f8 14395 error (_("bad breakpoint number: '%s'"), args);
c906108c 14396 if (*p == 0)
8a3fe4f8 14397 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14398
14399 set_ignore_count (num,
14400 longest_to_int (value_as_long (parse_and_eval (p))),
14401 from_tty);
221ea385
KS
14402 if (from_tty)
14403 printf_filtered ("\n");
c906108c
SS
14404}
14405\f
14406/* Call FUNCTION on each of the breakpoints
14407 whose numbers are given in ARGS. */
14408
14409static void
95a42b64
TT
14410map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14411 void *),
14412 void *data)
c906108c 14413{
52f0bd74
AC
14414 int num;
14415 struct breakpoint *b, *tmp;
11cf8741 14416 int match;
197f0a60 14417 struct get_number_or_range_state state;
c906108c 14418
197f0a60 14419 if (args == 0)
e2e0b3e5 14420 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14421
197f0a60
TT
14422 init_number_or_range (&state, args);
14423
14424 while (!state.finished)
c906108c 14425 {
197f0a60
TT
14426 char *p = state.string;
14427
11cf8741 14428 match = 0;
c5aa993b 14429
197f0a60 14430 num = get_number_or_range (&state);
5c44784c 14431 if (num == 0)
c5aa993b 14432 {
8a3fe4f8 14433 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14434 }
14435 else
14436 {
14437 ALL_BREAKPOINTS_SAFE (b, tmp)
14438 if (b->number == num)
14439 {
11cf8741 14440 match = 1;
cdac0397 14441 function (b, data);
11cf8741 14442 break;
5c44784c 14443 }
11cf8741 14444 if (match == 0)
a3f17187 14445 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14446 }
c906108c
SS
14447 }
14448}
14449
0d381245
VP
14450static struct bp_location *
14451find_location_by_number (char *number)
14452{
14453 char *dot = strchr (number, '.');
14454 char *p1;
14455 int bp_num;
14456 int loc_num;
14457 struct breakpoint *b;
14458 struct bp_location *loc;
14459
14460 *dot = '\0';
14461
14462 p1 = number;
197f0a60 14463 bp_num = get_number (&p1);
0d381245
VP
14464 if (bp_num == 0)
14465 error (_("Bad breakpoint number '%s'"), number);
14466
14467 ALL_BREAKPOINTS (b)
14468 if (b->number == bp_num)
14469 {
14470 break;
14471 }
14472
14473 if (!b || b->number != bp_num)
14474 error (_("Bad breakpoint number '%s'"), number);
14475
14476 p1 = dot+1;
197f0a60 14477 loc_num = get_number (&p1);
0d381245
VP
14478 if (loc_num == 0)
14479 error (_("Bad breakpoint location number '%s'"), number);
14480
14481 --loc_num;
14482 loc = b->loc;
14483 for (;loc_num && loc; --loc_num, loc = loc->next)
14484 ;
14485 if (!loc)
14486 error (_("Bad breakpoint location number '%s'"), dot+1);
14487
14488 return loc;
14489}
14490
14491
1900040c
MS
14492/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14493 If from_tty is nonzero, it prints a message to that effect,
14494 which ends with a period (no newline). */
14495
c906108c 14496void
fba45db2 14497disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14498{
14499 /* Never disable a watchpoint scope breakpoint; we want to
14500 hit them when we leave scope so we can delete both the
14501 watchpoint and its scope breakpoint at that time. */
14502 if (bpt->type == bp_watchpoint_scope)
14503 return;
14504
c2c6d25f 14505 /* You can't disable permanent breakpoints. */
b5de0fa7 14506 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14507 return;
14508
b5de0fa7 14509 bpt->enable_state = bp_disabled;
c906108c 14510
b775012e
LM
14511 /* Mark breakpoint locations modified. */
14512 mark_breakpoint_modified (bpt);
14513
d248b706
KY
14514 if (target_supports_enable_disable_tracepoint ()
14515 && current_trace_status ()->running && is_tracepoint (bpt))
14516 {
14517 struct bp_location *location;
14518
14519 for (location = bpt->loc; location; location = location->next)
14520 target_disable_tracepoint (location);
14521 }
14522
b60e7edf 14523 update_global_location_list (0);
c906108c 14524
8d3788bd 14525 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14526}
14527
51be5b68
PA
14528/* A callback for iterate_over_related_breakpoints. */
14529
14530static void
14531do_disable_breakpoint (struct breakpoint *b, void *ignore)
14532{
14533 disable_breakpoint (b);
14534}
14535
95a42b64
TT
14536/* A callback for map_breakpoint_numbers that calls
14537 disable_breakpoint. */
14538
14539static void
14540do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14541{
51be5b68 14542 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14543}
14544
c906108c 14545static void
fba45db2 14546disable_command (char *args, int from_tty)
c906108c 14547{
c906108c 14548 if (args == 0)
46c6471b
PA
14549 {
14550 struct breakpoint *bpt;
14551
14552 ALL_BREAKPOINTS (bpt)
14553 if (user_breakpoint_p (bpt))
14554 disable_breakpoint (bpt);
14555 }
9eaabc75 14556 else
0d381245 14557 {
9eaabc75
MW
14558 char *num = extract_arg (&args);
14559
14560 while (num)
d248b706 14561 {
9eaabc75 14562 if (strchr (num, '.'))
b775012e 14563 {
9eaabc75
MW
14564 struct bp_location *loc = find_location_by_number (num);
14565
14566 if (loc)
14567 {
14568 if (loc->enabled)
14569 {
14570 loc->enabled = 0;
14571 mark_breakpoint_location_modified (loc);
14572 }
14573 if (target_supports_enable_disable_tracepoint ()
14574 && current_trace_status ()->running && loc->owner
14575 && is_tracepoint (loc->owner))
14576 target_disable_tracepoint (loc);
14577 }
14578 update_global_location_list (0);
b775012e 14579 }
9eaabc75
MW
14580 else
14581 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14582 num = extract_arg (&args);
d248b706 14583 }
0d381245 14584 }
c906108c
SS
14585}
14586
14587static void
816338b5
SS
14588enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14589 int count)
c906108c 14590{
afe38095 14591 int target_resources_ok;
c906108c
SS
14592
14593 if (bpt->type == bp_hardware_breakpoint)
14594 {
14595 int i;
c5aa993b 14596 i = hw_breakpoint_used_count ();
53a5351d 14597 target_resources_ok =
d92524f1 14598 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14599 i + 1, 0);
c906108c 14600 if (target_resources_ok == 0)
8a3fe4f8 14601 error (_("No hardware breakpoint support in the target."));
c906108c 14602 else if (target_resources_ok < 0)
8a3fe4f8 14603 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14604 }
14605
cc60f2e3 14606 if (is_watchpoint (bpt))
c906108c 14607 {
d07205c2
JK
14608 /* Initialize it just to avoid a GCC false warning. */
14609 enum enable_state orig_enable_state = 0;
bfd189b1 14610 volatile struct gdb_exception e;
dde02812
ES
14611
14612 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14613 {
3a5c3e22
PA
14614 struct watchpoint *w = (struct watchpoint *) bpt;
14615
1e718ff1
TJB
14616 orig_enable_state = bpt->enable_state;
14617 bpt->enable_state = bp_enabled;
3a5c3e22 14618 update_watchpoint (w, 1 /* reparse */);
c906108c 14619 }
dde02812 14620 if (e.reason < 0)
c5aa993b 14621 {
1e718ff1 14622 bpt->enable_state = orig_enable_state;
dde02812
ES
14623 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14624 bpt->number);
14625 return;
c5aa993b 14626 }
c906108c 14627 }
0101ce28 14628
b4c291bb
KH
14629 if (bpt->enable_state != bp_permanent)
14630 bpt->enable_state = bp_enabled;
d248b706 14631
b775012e
LM
14632 bpt->enable_state = bp_enabled;
14633
14634 /* Mark breakpoint locations modified. */
14635 mark_breakpoint_modified (bpt);
14636
d248b706
KY
14637 if (target_supports_enable_disable_tracepoint ()
14638 && current_trace_status ()->running && is_tracepoint (bpt))
14639 {
14640 struct bp_location *location;
14641
14642 for (location = bpt->loc; location; location = location->next)
14643 target_enable_tracepoint (location);
14644 }
14645
b4c291bb 14646 bpt->disposition = disposition;
816338b5 14647 bpt->enable_count = count;
b60e7edf 14648 update_global_location_list (1);
9c97429f 14649
8d3788bd 14650 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14651}
14652
fe3f5fa8 14653
c906108c 14654void
fba45db2 14655enable_breakpoint (struct breakpoint *bpt)
c906108c 14656{
816338b5 14657 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14658}
14659
14660static void
14661do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14662{
14663 enable_breakpoint (bpt);
c906108c
SS
14664}
14665
95a42b64
TT
14666/* A callback for map_breakpoint_numbers that calls
14667 enable_breakpoint. */
14668
14669static void
14670do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14671{
51be5b68 14672 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14673}
14674
c906108c
SS
14675/* The enable command enables the specified breakpoints (or all defined
14676 breakpoints) so they once again become (or continue to be) effective
1272ad14 14677 in stopping the inferior. */
c906108c 14678
c906108c 14679static void
fba45db2 14680enable_command (char *args, int from_tty)
c906108c 14681{
c906108c 14682 if (args == 0)
46c6471b
PA
14683 {
14684 struct breakpoint *bpt;
14685
14686 ALL_BREAKPOINTS (bpt)
14687 if (user_breakpoint_p (bpt))
14688 enable_breakpoint (bpt);
14689 }
9eaabc75 14690 else
0d381245 14691 {
9eaabc75
MW
14692 char *num = extract_arg (&args);
14693
14694 while (num)
d248b706 14695 {
9eaabc75 14696 if (strchr (num, '.'))
b775012e 14697 {
9eaabc75
MW
14698 struct bp_location *loc = find_location_by_number (num);
14699
14700 if (loc)
14701 {
14702 if (!loc->enabled)
14703 {
14704 loc->enabled = 1;
14705 mark_breakpoint_location_modified (loc);
14706 }
14707 if (target_supports_enable_disable_tracepoint ()
14708 && current_trace_status ()->running && loc->owner
14709 && is_tracepoint (loc->owner))
14710 target_enable_tracepoint (loc);
14711 }
14712 update_global_location_list (1);
b775012e 14713 }
9eaabc75
MW
14714 else
14715 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14716 num = extract_arg (&args);
d248b706 14717 }
0d381245 14718 }
c906108c
SS
14719}
14720
816338b5
SS
14721/* This struct packages up disposition data for application to multiple
14722 breakpoints. */
14723
14724struct disp_data
14725{
14726 enum bpdisp disp;
14727 int count;
14728};
14729
c906108c 14730static void
51be5b68
PA
14731do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14732{
816338b5 14733 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14734
816338b5 14735 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14736}
14737
14738static void
14739do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14740{
816338b5 14741 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14742
14743 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14744}
14745
c906108c 14746static void
fba45db2 14747enable_once_command (char *args, int from_tty)
c906108c 14748{
51be5b68 14749 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14750}
14751
816338b5
SS
14752static void
14753do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14754{
14755 struct disp_data disp = { disp_disable, *(int *) countptr };
14756
14757 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14758}
14759
14760static void
14761enable_count_command (char *args, int from_tty)
14762{
14763 int count = get_number (&args);
14764
14765 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14766}
14767
c906108c 14768static void
51be5b68 14769do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14770{
816338b5 14771 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14772
14773 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14774}
14775
c906108c 14776static void
fba45db2 14777enable_delete_command (char *args, int from_tty)
c906108c 14778{
51be5b68 14779 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14780}
14781\f
fa8d40ab
JJ
14782static void
14783set_breakpoint_cmd (char *args, int from_tty)
14784{
14785}
14786
14787static void
14788show_breakpoint_cmd (char *args, int from_tty)
14789{
14790}
14791
1f3b5d1b
PP
14792/* Invalidate last known value of any hardware watchpoint if
14793 the memory which that value represents has been written to by
14794 GDB itself. */
14795
14796static void
8de0566d
YQ
14797invalidate_bp_value_on_memory_change (struct inferior *inferior,
14798 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14799 const bfd_byte *data)
14800{
14801 struct breakpoint *bp;
14802
14803 ALL_BREAKPOINTS (bp)
14804 if (bp->enable_state == bp_enabled
3a5c3e22 14805 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14806 {
3a5c3e22 14807 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14808
3a5c3e22
PA
14809 if (wp->val_valid && wp->val)
14810 {
14811 struct bp_location *loc;
14812
14813 for (loc = bp->loc; loc != NULL; loc = loc->next)
14814 if (loc->loc_type == bp_loc_hardware_watchpoint
14815 && loc->address + loc->length > addr
14816 && addr + len > loc->address)
14817 {
14818 value_free (wp->val);
14819 wp->val = NULL;
14820 wp->val_valid = 0;
14821 }
14822 }
1f3b5d1b
PP
14823 }
14824}
14825
8181d85f
DJ
14826/* Create and insert a raw software breakpoint at PC. Return an
14827 identifier, which should be used to remove the breakpoint later.
14828 In general, places which call this should be using something on the
14829 breakpoint chain instead; this function should be eliminated
14830 someday. */
14831
14832void *
6c95b8df
PA
14833deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14834 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14835{
14836 struct bp_target_info *bp_tgt;
14837
6c95b8df 14838 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14839
6c95b8df 14840 bp_tgt->placed_address_space = aspace;
8181d85f 14841 bp_tgt->placed_address = pc;
6c95b8df 14842
a6d9a66e 14843 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14844 {
14845 /* Could not insert the breakpoint. */
14846 xfree (bp_tgt);
14847 return NULL;
14848 }
14849
14850 return bp_tgt;
14851}
14852
4a64f543
MS
14853/* Remove a breakpoint BP inserted by
14854 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14855
14856int
a6d9a66e 14857deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14858{
14859 struct bp_target_info *bp_tgt = bp;
14860 int ret;
14861
a6d9a66e 14862 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14863 xfree (bp_tgt);
14864
14865 return ret;
14866}
14867
4a64f543
MS
14868/* One (or perhaps two) breakpoints used for software single
14869 stepping. */
8181d85f
DJ
14870
14871static void *single_step_breakpoints[2];
a6d9a66e 14872static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14873
14874/* Create and insert a breakpoint for software single step. */
14875
14876void
6c95b8df 14877insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14878 struct address_space *aspace,
14879 CORE_ADDR next_pc)
8181d85f
DJ
14880{
14881 void **bpt_p;
14882
14883 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14884 {
14885 bpt_p = &single_step_breakpoints[0];
14886 single_step_gdbarch[0] = gdbarch;
14887 }
8181d85f
DJ
14888 else
14889 {
14890 gdb_assert (single_step_breakpoints[1] == NULL);
14891 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14892 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14893 }
14894
4a64f543
MS
14895 /* NOTE drow/2006-04-11: A future improvement to this function would
14896 be to only create the breakpoints once, and actually put them on
14897 the breakpoint chain. That would let us use set_raw_breakpoint.
14898 We could adjust the addresses each time they were needed. Doing
14899 this requires corresponding changes elsewhere where single step
14900 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14901
6c95b8df 14902 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14903 if (*bpt_p == NULL)
5af949e3
UW
14904 error (_("Could not insert single-step breakpoint at %s"),
14905 paddress (gdbarch, next_pc));
8181d85f
DJ
14906}
14907
f02253f1
HZ
14908/* Check if the breakpoints used for software single stepping
14909 were inserted or not. */
14910
14911int
14912single_step_breakpoints_inserted (void)
14913{
14914 return (single_step_breakpoints[0] != NULL
14915 || single_step_breakpoints[1] != NULL);
14916}
14917
8181d85f
DJ
14918/* Remove and delete any breakpoints used for software single step. */
14919
14920void
14921remove_single_step_breakpoints (void)
14922{
14923 gdb_assert (single_step_breakpoints[0] != NULL);
14924
14925 /* See insert_single_step_breakpoint for more about this deprecated
14926 call. */
a6d9a66e
UW
14927 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14928 single_step_breakpoints[0]);
14929 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14930 single_step_breakpoints[0] = NULL;
14931
14932 if (single_step_breakpoints[1] != NULL)
14933 {
a6d9a66e
UW
14934 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14935 single_step_breakpoints[1]);
14936 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14937 single_step_breakpoints[1] = NULL;
14938 }
14939}
14940
d03285ec
UW
14941/* Delete software single step breakpoints without removing them from
14942 the inferior. This is intended to be used if the inferior's address
14943 space where they were inserted is already gone, e.g. after exit or
14944 exec. */
14945
14946void
14947cancel_single_step_breakpoints (void)
14948{
14949 int i;
14950
14951 for (i = 0; i < 2; i++)
14952 if (single_step_breakpoints[i])
14953 {
14954 xfree (single_step_breakpoints[i]);
14955 single_step_breakpoints[i] = NULL;
14956 single_step_gdbarch[i] = NULL;
14957 }
14958}
14959
14960/* Detach software single-step breakpoints from INFERIOR_PTID without
14961 removing them. */
14962
14963static void
14964detach_single_step_breakpoints (void)
14965{
14966 int i;
14967
14968 for (i = 0; i < 2; i++)
14969 if (single_step_breakpoints[i])
14970 target_remove_breakpoint (single_step_gdbarch[i],
14971 single_step_breakpoints[i]);
14972}
14973
4a64f543
MS
14974/* Check whether a software single-step breakpoint is inserted at
14975 PC. */
1aafd4da
UW
14976
14977static int
cc59ec59
MS
14978single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14979 CORE_ADDR pc)
1aafd4da
UW
14980{
14981 int i;
14982
14983 for (i = 0; i < 2; i++)
14984 {
14985 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14986 if (bp_tgt
14987 && breakpoint_address_match (bp_tgt->placed_address_space,
14988 bp_tgt->placed_address,
14989 aspace, pc))
1aafd4da
UW
14990 return 1;
14991 }
14992
14993 return 0;
14994}
14995
a96d9b2e
SDJ
14996/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14997 non-zero otherwise. */
14998static int
14999is_syscall_catchpoint_enabled (struct breakpoint *bp)
15000{
15001 if (syscall_catchpoint_p (bp)
15002 && bp->enable_state != bp_disabled
15003 && bp->enable_state != bp_call_disabled)
15004 return 1;
15005 else
15006 return 0;
15007}
15008
15009int
15010catch_syscall_enabled (void)
15011{
fa3064dd
YQ
15012 struct catch_syscall_inferior_data *inf_data
15013 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15014
fa3064dd 15015 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15016}
15017
15018int
15019catching_syscall_number (int syscall_number)
15020{
15021 struct breakpoint *bp;
15022
15023 ALL_BREAKPOINTS (bp)
15024 if (is_syscall_catchpoint_enabled (bp))
15025 {
be5c67c1
PA
15026 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15027
15028 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15029 {
15030 int i, iter;
15031 for (i = 0;
be5c67c1 15032 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15033 i++)
15034 if (syscall_number == iter)
15035 return 1;
15036 }
15037 else
15038 return 1;
15039 }
15040
15041 return 0;
15042}
15043
15044/* Complete syscall names. Used by "catch syscall". */
49c4e619 15045static VEC (char_ptr) *
a96d9b2e 15046catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15047 const char *text, const char *word)
a96d9b2e
SDJ
15048{
15049 const char **list = get_syscall_names ();
49c4e619 15050 VEC (char_ptr) *retlist
b45627a0 15051 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15052
c38eea1a
MS
15053 xfree (list);
15054 return retlist;
a96d9b2e
SDJ
15055}
15056
1042e4c0
SS
15057/* Tracepoint-specific operations. */
15058
15059/* Set tracepoint count to NUM. */
15060static void
15061set_tracepoint_count (int num)
15062{
15063 tracepoint_count = num;
4fa62494 15064 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15065}
15066
70221824 15067static void
1042e4c0
SS
15068trace_command (char *arg, int from_tty)
15069{
55aa24fb
SDJ
15070 struct breakpoint_ops *ops;
15071 const char *arg_cp = arg;
15072
15073 if (arg && probe_linespec_to_ops (&arg_cp))
15074 ops = &tracepoint_probe_breakpoint_ops;
15075 else
15076 ops = &tracepoint_breakpoint_ops;
15077
558a9d82
YQ
15078 create_breakpoint (get_current_arch (),
15079 arg,
15080 NULL, 0, NULL, 1 /* parse arg */,
15081 0 /* tempflag */,
15082 bp_tracepoint /* type_wanted */,
15083 0 /* Ignore count */,
15084 pending_break_support,
15085 ops,
15086 from_tty,
15087 1 /* enabled */,
15088 0 /* internal */, 0);
1042e4c0
SS
15089}
15090
70221824 15091static void
7a697b8d
SS
15092ftrace_command (char *arg, int from_tty)
15093{
558a9d82
YQ
15094 create_breakpoint (get_current_arch (),
15095 arg,
15096 NULL, 0, NULL, 1 /* parse arg */,
15097 0 /* tempflag */,
15098 bp_fast_tracepoint /* type_wanted */,
15099 0 /* Ignore count */,
15100 pending_break_support,
15101 &tracepoint_breakpoint_ops,
15102 from_tty,
15103 1 /* enabled */,
15104 0 /* internal */, 0);
0fb4aa4b
PA
15105}
15106
15107/* strace command implementation. Creates a static tracepoint. */
15108
70221824 15109static void
0fb4aa4b
PA
15110strace_command (char *arg, int from_tty)
15111{
983af33b
SDJ
15112 struct breakpoint_ops *ops;
15113
15114 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15115 or with a normal static tracepoint. */
15116 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15117 ops = &strace_marker_breakpoint_ops;
15118 else
15119 ops = &tracepoint_breakpoint_ops;
15120
558a9d82
YQ
15121 create_breakpoint (get_current_arch (),
15122 arg,
15123 NULL, 0, NULL, 1 /* parse arg */,
15124 0 /* tempflag */,
15125 bp_static_tracepoint /* type_wanted */,
15126 0 /* Ignore count */,
15127 pending_break_support,
15128 ops,
15129 from_tty,
15130 1 /* enabled */,
15131 0 /* internal */, 0);
7a697b8d
SS
15132}
15133
409873ef
SS
15134/* Set up a fake reader function that gets command lines from a linked
15135 list that was acquired during tracepoint uploading. */
15136
15137static struct uploaded_tp *this_utp;
3149d8c1 15138static int next_cmd;
409873ef
SS
15139
15140static char *
15141read_uploaded_action (void)
15142{
15143 char *rslt;
15144
3149d8c1 15145 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15146
3149d8c1 15147 next_cmd++;
409873ef
SS
15148
15149 return rslt;
15150}
15151
00bf0b85
SS
15152/* Given information about a tracepoint as recorded on a target (which
15153 can be either a live system or a trace file), attempt to create an
15154 equivalent GDB tracepoint. This is not a reliable process, since
15155 the target does not necessarily have all the information used when
15156 the tracepoint was originally defined. */
15157
d9b3f62e 15158struct tracepoint *
00bf0b85 15159create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15160{
409873ef 15161 char *addr_str, small_buf[100];
d9b3f62e 15162 struct tracepoint *tp;
fd9b8c24 15163
409873ef
SS
15164 if (utp->at_string)
15165 addr_str = utp->at_string;
15166 else
15167 {
15168 /* In the absence of a source location, fall back to raw
15169 address. Since there is no way to confirm that the address
15170 means the same thing as when the trace was started, warn the
15171 user. */
3e43a32a
MS
15172 warning (_("Uploaded tracepoint %d has no "
15173 "source location, using raw address"),
409873ef 15174 utp->number);
8c042590 15175 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15176 addr_str = small_buf;
15177 }
15178
15179 /* There's not much we can do with a sequence of bytecodes. */
15180 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15181 warning (_("Uploaded tracepoint %d condition "
15182 "has no source form, ignoring it"),
409873ef 15183 utp->number);
d5551862 15184
8cdf0e15 15185 if (!create_breakpoint (get_current_arch (),
409873ef 15186 addr_str,
e7e0cddf
SS
15187 utp->cond_string, -1, NULL,
15188 0 /* parse cond/thread */,
8cdf0e15 15189 0 /* tempflag */,
0fb4aa4b 15190 utp->type /* type_wanted */,
8cdf0e15
VP
15191 0 /* Ignore count */,
15192 pending_break_support,
348d480f 15193 &tracepoint_breakpoint_ops,
8cdf0e15 15194 0 /* from_tty */,
84f4c1fe 15195 utp->enabled /* enabled */,
44f238bb
PA
15196 0 /* internal */,
15197 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15198 return NULL;
15199
409873ef 15200 /* Get the tracepoint we just created. */
fd9b8c24
PA
15201 tp = get_tracepoint (tracepoint_count);
15202 gdb_assert (tp != NULL);
d5551862 15203
00bf0b85
SS
15204 if (utp->pass > 0)
15205 {
8c042590
PM
15206 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15207 tp->base.number);
00bf0b85 15208
409873ef 15209 trace_pass_command (small_buf, 0);
00bf0b85
SS
15210 }
15211
409873ef
SS
15212 /* If we have uploaded versions of the original commands, set up a
15213 special-purpose "reader" function and call the usual command line
15214 reader, then pass the result to the breakpoint command-setting
15215 function. */
3149d8c1 15216 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15217 {
409873ef 15218 struct command_line *cmd_list;
00bf0b85 15219
409873ef 15220 this_utp = utp;
3149d8c1 15221 next_cmd = 0;
d5551862 15222
409873ef
SS
15223 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15224
d9b3f62e 15225 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15226 }
3149d8c1
SS
15227 else if (!VEC_empty (char_ptr, utp->actions)
15228 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15229 warning (_("Uploaded tracepoint %d actions "
15230 "have no source form, ignoring them"),
409873ef 15231 utp->number);
00bf0b85 15232
f196051f
SS
15233 /* Copy any status information that might be available. */
15234 tp->base.hit_count = utp->hit_count;
15235 tp->traceframe_usage = utp->traceframe_usage;
15236
00bf0b85 15237 return tp;
d9b3f62e 15238}
00bf0b85 15239
1042e4c0
SS
15240/* Print information on tracepoint number TPNUM_EXP, or all if
15241 omitted. */
15242
15243static void
e5a67952 15244tracepoints_info (char *args, int from_tty)
1042e4c0 15245{
79a45e25 15246 struct ui_out *uiout = current_uiout;
e5a67952 15247 int num_printed;
1042e4c0 15248
e5a67952 15249 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15250
15251 if (num_printed == 0)
1042e4c0 15252 {
e5a67952 15253 if (args == NULL || *args == '\0')
d77f58be
SS
15254 ui_out_message (uiout, 0, "No tracepoints.\n");
15255 else
e5a67952 15256 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15257 }
ad443146
SS
15258
15259 default_collect_info ();
1042e4c0
SS
15260}
15261
4a64f543 15262/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15263 Not supported by all targets. */
15264static void
15265enable_trace_command (char *args, int from_tty)
15266{
15267 enable_command (args, from_tty);
15268}
15269
4a64f543 15270/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15271 Not supported by all targets. */
15272static void
15273disable_trace_command (char *args, int from_tty)
15274{
15275 disable_command (args, from_tty);
15276}
15277
4a64f543 15278/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15279static void
15280delete_trace_command (char *arg, int from_tty)
15281{
35df4500 15282 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15283
15284 dont_repeat ();
15285
15286 if (arg == 0)
15287 {
15288 int breaks_to_delete = 0;
15289
15290 /* Delete all breakpoints if no argument.
15291 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15292 have to be deleted with an explicit breakpoint number
15293 argument. */
1042e4c0 15294 ALL_TRACEPOINTS (b)
46c6471b 15295 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15296 {
15297 breaks_to_delete = 1;
15298 break;
15299 }
1042e4c0
SS
15300
15301 /* Ask user only if there are some breakpoints to delete. */
15302 if (!from_tty
15303 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15304 {
35df4500 15305 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15306 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15307 delete_breakpoint (b);
1042e4c0
SS
15308 }
15309 }
15310 else
51be5b68 15311 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15312}
15313
197f0a60
TT
15314/* Helper function for trace_pass_command. */
15315
15316static void
d9b3f62e 15317trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15318{
d9b3f62e 15319 tp->pass_count = count;
6f6484cd 15320 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15321 if (from_tty)
15322 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15323 tp->base.number, count);
197f0a60
TT
15324}
15325
1042e4c0
SS
15326/* Set passcount for tracepoint.
15327
15328 First command argument is passcount, second is tracepoint number.
15329 If tracepoint number omitted, apply to most recently defined.
15330 Also accepts special argument "all". */
15331
15332static void
15333trace_pass_command (char *args, int from_tty)
15334{
d9b3f62e 15335 struct tracepoint *t1;
1042e4c0 15336 unsigned int count;
1042e4c0
SS
15337
15338 if (args == 0 || *args == 0)
3e43a32a
MS
15339 error (_("passcount command requires an "
15340 "argument (count + optional TP num)"));
1042e4c0 15341
4a64f543 15342 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15343
529480d0 15344 args = skip_spaces (args);
1042e4c0
SS
15345 if (*args && strncasecmp (args, "all", 3) == 0)
15346 {
d9b3f62e
PA
15347 struct breakpoint *b;
15348
1042e4c0 15349 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15350 if (*args)
15351 error (_("Junk at end of arguments."));
1042e4c0 15352
d9b3f62e 15353 ALL_TRACEPOINTS (b)
197f0a60 15354 {
d9b3f62e 15355 t1 = (struct tracepoint *) b;
197f0a60
TT
15356 trace_pass_set_count (t1, count, from_tty);
15357 }
15358 }
15359 else if (*args == '\0')
1042e4c0 15360 {
197f0a60 15361 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15362 if (t1)
197f0a60
TT
15363 trace_pass_set_count (t1, count, from_tty);
15364 }
15365 else
15366 {
15367 struct get_number_or_range_state state;
15368
15369 init_number_or_range (&state, args);
15370 while (!state.finished)
1042e4c0 15371 {
197f0a60
TT
15372 t1 = get_tracepoint_by_number (&args, &state, 1);
15373 if (t1)
15374 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15375 }
15376 }
1042e4c0
SS
15377}
15378
d9b3f62e 15379struct tracepoint *
1042e4c0
SS
15380get_tracepoint (int num)
15381{
15382 struct breakpoint *t;
15383
15384 ALL_TRACEPOINTS (t)
15385 if (t->number == num)
d9b3f62e 15386 return (struct tracepoint *) t;
1042e4c0
SS
15387
15388 return NULL;
15389}
15390
d5551862
SS
15391/* Find the tracepoint with the given target-side number (which may be
15392 different from the tracepoint number after disconnecting and
15393 reconnecting). */
15394
d9b3f62e 15395struct tracepoint *
d5551862
SS
15396get_tracepoint_by_number_on_target (int num)
15397{
d9b3f62e 15398 struct breakpoint *b;
d5551862 15399
d9b3f62e
PA
15400 ALL_TRACEPOINTS (b)
15401 {
15402 struct tracepoint *t = (struct tracepoint *) b;
15403
15404 if (t->number_on_target == num)
15405 return t;
15406 }
d5551862
SS
15407
15408 return NULL;
15409}
15410
1042e4c0 15411/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15412 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15413 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15414 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15415struct tracepoint *
197f0a60
TT
15416get_tracepoint_by_number (char **arg,
15417 struct get_number_or_range_state *state,
15418 int optional_p)
1042e4c0 15419{
1042e4c0
SS
15420 struct breakpoint *t;
15421 int tpnum;
15422 char *instring = arg == NULL ? NULL : *arg;
15423
197f0a60
TT
15424 if (state)
15425 {
15426 gdb_assert (!state->finished);
15427 tpnum = get_number_or_range (state);
15428 }
15429 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15430 {
15431 if (optional_p)
15432 tpnum = tracepoint_count;
15433 else
15434 error_no_arg (_("tracepoint number"));
15435 }
15436 else
197f0a60 15437 tpnum = get_number (arg);
1042e4c0
SS
15438
15439 if (tpnum <= 0)
15440 {
15441 if (instring && *instring)
15442 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15443 instring);
15444 else
3e43a32a
MS
15445 printf_filtered (_("Tracepoint argument missing "
15446 "and no previous tracepoint\n"));
1042e4c0
SS
15447 return NULL;
15448 }
15449
15450 ALL_TRACEPOINTS (t)
15451 if (t->number == tpnum)
15452 {
d9b3f62e 15453 return (struct tracepoint *) t;
1042e4c0
SS
15454 }
15455
1042e4c0
SS
15456 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15457 return NULL;
15458}
15459
d9b3f62e
PA
15460void
15461print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15462{
15463 if (b->thread != -1)
15464 fprintf_unfiltered (fp, " thread %d", b->thread);
15465
15466 if (b->task != 0)
15467 fprintf_unfiltered (fp, " task %d", b->task);
15468
15469 fprintf_unfiltered (fp, "\n");
15470}
15471
6149aea9
PA
15472/* Save information on user settable breakpoints (watchpoints, etc) to
15473 a new script file named FILENAME. If FILTER is non-NULL, call it
15474 on each breakpoint and only include the ones for which it returns
15475 non-zero. */
15476
1042e4c0 15477static void
6149aea9
PA
15478save_breakpoints (char *filename, int from_tty,
15479 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15480{
15481 struct breakpoint *tp;
6149aea9 15482 int any = 0;
1042e4c0 15483 struct cleanup *cleanup;
a7bdde9e 15484 struct ui_file *fp;
6149aea9 15485 int extra_trace_bits = 0;
1042e4c0 15486
6149aea9
PA
15487 if (filename == 0 || *filename == 0)
15488 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15489
15490 /* See if we have anything to save. */
6149aea9 15491 ALL_BREAKPOINTS (tp)
1042e4c0 15492 {
6149aea9 15493 /* Skip internal and momentary breakpoints. */
09d682a4 15494 if (!user_breakpoint_p (tp))
6149aea9
PA
15495 continue;
15496
15497 /* If we have a filter, only save the breakpoints it accepts. */
15498 if (filter && !filter (tp))
15499 continue;
15500
15501 any = 1;
15502
15503 if (is_tracepoint (tp))
15504 {
15505 extra_trace_bits = 1;
15506
15507 /* We can stop searching. */
15508 break;
15509 }
1042e4c0 15510 }
6149aea9
PA
15511
15512 if (!any)
1042e4c0 15513 {
6149aea9 15514 warning (_("Nothing to save."));
1042e4c0
SS
15515 return;
15516 }
15517
c718be47
PA
15518 filename = tilde_expand (filename);
15519 cleanup = make_cleanup (xfree, filename);
15520 fp = gdb_fopen (filename, "w");
059fb39f 15521 if (!fp)
6149aea9
PA
15522 error (_("Unable to open file '%s' for saving (%s)"),
15523 filename, safe_strerror (errno));
a7bdde9e 15524 make_cleanup_ui_file_delete (fp);
8bf6485c 15525
6149aea9
PA
15526 if (extra_trace_bits)
15527 save_trace_state_variables (fp);
8bf6485c 15528
6149aea9 15529 ALL_BREAKPOINTS (tp)
1042e4c0 15530 {
6149aea9 15531 /* Skip internal and momentary breakpoints. */
09d682a4 15532 if (!user_breakpoint_p (tp))
6149aea9 15533 continue;
8bf6485c 15534
6149aea9
PA
15535 /* If we have a filter, only save the breakpoints it accepts. */
15536 if (filter && !filter (tp))
15537 continue;
15538
348d480f 15539 tp->ops->print_recreate (tp, fp);
1042e4c0 15540
6149aea9
PA
15541 /* Note, we can't rely on tp->number for anything, as we can't
15542 assume the recreated breakpoint numbers will match. Use $bpnum
15543 instead. */
15544
15545 if (tp->cond_string)
15546 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15547
15548 if (tp->ignore_count)
15549 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15550
2d9442cc 15551 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15552 {
a7bdde9e
VP
15553 volatile struct gdb_exception ex;
15554
6149aea9 15555 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15556
79a45e25 15557 ui_out_redirect (current_uiout, fp);
14dba4b4 15558 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15559 {
79a45e25 15560 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15561 }
79a45e25 15562 ui_out_redirect (current_uiout, NULL);
1042e4c0 15563
a7bdde9e
VP
15564 if (ex.reason < 0)
15565 throw_exception (ex);
1042e4c0 15566
a7bdde9e 15567 fprintf_unfiltered (fp, " end\n");
1042e4c0 15568 }
6149aea9
PA
15569
15570 if (tp->enable_state == bp_disabled)
15571 fprintf_unfiltered (fp, "disable\n");
15572
15573 /* If this is a multi-location breakpoint, check if the locations
15574 should be individually disabled. Watchpoint locations are
15575 special, and not user visible. */
15576 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15577 {
15578 struct bp_location *loc;
15579 int n = 1;
15580
15581 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15582 if (!loc->enabled)
15583 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15584 }
1042e4c0 15585 }
8bf6485c 15586
6149aea9 15587 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15588 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15589
1042e4c0 15590 if (from_tty)
6149aea9 15591 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15592 do_cleanups (cleanup);
6149aea9
PA
15593}
15594
15595/* The `save breakpoints' command. */
15596
15597static void
15598save_breakpoints_command (char *args, int from_tty)
15599{
15600 save_breakpoints (args, from_tty, NULL);
15601}
15602
15603/* The `save tracepoints' command. */
15604
15605static void
15606save_tracepoints_command (char *args, int from_tty)
15607{
15608 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15609}
15610
15611/* Create a vector of all tracepoints. */
15612
15613VEC(breakpoint_p) *
eeae04df 15614all_tracepoints (void)
1042e4c0
SS
15615{
15616 VEC(breakpoint_p) *tp_vec = 0;
15617 struct breakpoint *tp;
15618
15619 ALL_TRACEPOINTS (tp)
15620 {
15621 VEC_safe_push (breakpoint_p, tp_vec, tp);
15622 }
15623
15624 return tp_vec;
15625}
15626
c906108c 15627\f
4a64f543
MS
15628/* This help string is used for the break, hbreak, tbreak and thbreak
15629 commands. It is defined as a macro to prevent duplication.
15630 COMMAND should be a string constant containing the name of the
15631 command. */
31e2b00f 15632#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15633command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15634PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15635probe point. Accepted values are `-probe' (for a generic, automatically\n\
15636guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15637LOCATION may be a line number, function name, or \"*\" and an address.\n\
15638If a line number is specified, break at start of code for that line.\n\
15639If a function is specified, break at start of code for that function.\n\
15640If an address is specified, break at that exact address.\n\
dc10affe
PA
15641With no LOCATION, uses current execution address of the selected\n\
15642stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15643\n\
15644THREADNUM is the number from \"info threads\".\n\
15645CONDITION is a boolean expression.\n\
15646\n\
d41c0fc8
PA
15647Multiple breakpoints at one place are permitted, and useful if their\n\
15648conditions are different.\n\
31e2b00f
AS
15649\n\
15650Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15651
44feb3ce
TT
15652/* List of subcommands for "catch". */
15653static struct cmd_list_element *catch_cmdlist;
15654
15655/* List of subcommands for "tcatch". */
15656static struct cmd_list_element *tcatch_cmdlist;
15657
9ac4176b 15658void
44feb3ce
TT
15659add_catch_command (char *name, char *docstring,
15660 void (*sfunc) (char *args, int from_tty,
15661 struct cmd_list_element *command),
625e8578 15662 completer_ftype *completer,
44feb3ce
TT
15663 void *user_data_catch,
15664 void *user_data_tcatch)
15665{
15666 struct cmd_list_element *command;
15667
15668 command = add_cmd (name, class_breakpoint, NULL, docstring,
15669 &catch_cmdlist);
15670 set_cmd_sfunc (command, sfunc);
15671 set_cmd_context (command, user_data_catch);
a96d9b2e 15672 set_cmd_completer (command, completer);
44feb3ce
TT
15673
15674 command = add_cmd (name, class_breakpoint, NULL, docstring,
15675 &tcatch_cmdlist);
15676 set_cmd_sfunc (command, sfunc);
15677 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15678 set_cmd_completer (command, completer);
44feb3ce
TT
15679}
15680
6c95b8df 15681static void
a79b8f6e 15682clear_syscall_counts (struct inferior *inf)
6c95b8df 15683{
fa3064dd
YQ
15684 struct catch_syscall_inferior_data *inf_data
15685 = get_catch_syscall_inferior_data (inf);
15686
15687 inf_data->total_syscalls_count = 0;
15688 inf_data->any_syscall_count = 0;
15689 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15690}
15691
6149aea9
PA
15692static void
15693save_command (char *arg, int from_tty)
15694{
3e43a32a
MS
15695 printf_unfiltered (_("\"save\" must be followed by "
15696 "the name of a save subcommand.\n"));
6149aea9
PA
15697 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15698}
15699
84f4c1fe
PM
15700struct breakpoint *
15701iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15702 void *data)
15703{
35df4500 15704 struct breakpoint *b, *b_tmp;
84f4c1fe 15705
35df4500 15706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15707 {
15708 if ((*callback) (b, data))
15709 return b;
15710 }
15711
15712 return NULL;
15713}
15714
0574c78f
GB
15715/* Zero if any of the breakpoint's locations could be a location where
15716 functions have been inlined, nonzero otherwise. */
15717
15718static int
15719is_non_inline_function (struct breakpoint *b)
15720{
15721 /* The shared library event breakpoint is set on the address of a
15722 non-inline function. */
15723 if (b->type == bp_shlib_event)
15724 return 1;
15725
15726 return 0;
15727}
15728
15729/* Nonzero if the specified PC cannot be a location where functions
15730 have been inlined. */
15731
15732int
09ac7c10
TT
15733pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15734 const struct target_waitstatus *ws)
0574c78f
GB
15735{
15736 struct breakpoint *b;
15737 struct bp_location *bl;
15738
15739 ALL_BREAKPOINTS (b)
15740 {
15741 if (!is_non_inline_function (b))
15742 continue;
15743
15744 for (bl = b->loc; bl != NULL; bl = bl->next)
15745 {
15746 if (!bl->shlib_disabled
09ac7c10 15747 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15748 return 1;
15749 }
15750 }
15751
15752 return 0;
15753}
15754
2f202fde
JK
15755/* Remove any references to OBJFILE which is going to be freed. */
15756
15757void
15758breakpoint_free_objfile (struct objfile *objfile)
15759{
15760 struct bp_location **locp, *loc;
15761
15762 ALL_BP_LOCATIONS (loc, locp)
15763 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15764 loc->symtab = NULL;
15765}
15766
2060206e
PA
15767void
15768initialize_breakpoint_ops (void)
15769{
15770 static int initialized = 0;
15771
15772 struct breakpoint_ops *ops;
15773
15774 if (initialized)
15775 return;
15776 initialized = 1;
15777
15778 /* The breakpoint_ops structure to be inherit by all kinds of
15779 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15780 internal and momentary breakpoints, etc.). */
15781 ops = &bkpt_base_breakpoint_ops;
15782 *ops = base_breakpoint_ops;
15783 ops->re_set = bkpt_re_set;
15784 ops->insert_location = bkpt_insert_location;
15785 ops->remove_location = bkpt_remove_location;
15786 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15787 ops->create_sals_from_address = bkpt_create_sals_from_address;
15788 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15789 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15790
15791 /* The breakpoint_ops structure to be used in regular breakpoints. */
15792 ops = &bkpt_breakpoint_ops;
15793 *ops = bkpt_base_breakpoint_ops;
15794 ops->re_set = bkpt_re_set;
15795 ops->resources_needed = bkpt_resources_needed;
15796 ops->print_it = bkpt_print_it;
15797 ops->print_mention = bkpt_print_mention;
15798 ops->print_recreate = bkpt_print_recreate;
15799
15800 /* Ranged breakpoints. */
15801 ops = &ranged_breakpoint_ops;
15802 *ops = bkpt_breakpoint_ops;
15803 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15804 ops->resources_needed = resources_needed_ranged_breakpoint;
15805 ops->print_it = print_it_ranged_breakpoint;
15806 ops->print_one = print_one_ranged_breakpoint;
15807 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15808 ops->print_mention = print_mention_ranged_breakpoint;
15809 ops->print_recreate = print_recreate_ranged_breakpoint;
15810
15811 /* Internal breakpoints. */
15812 ops = &internal_breakpoint_ops;
15813 *ops = bkpt_base_breakpoint_ops;
15814 ops->re_set = internal_bkpt_re_set;
15815 ops->check_status = internal_bkpt_check_status;
15816 ops->print_it = internal_bkpt_print_it;
15817 ops->print_mention = internal_bkpt_print_mention;
15818
15819 /* Momentary breakpoints. */
15820 ops = &momentary_breakpoint_ops;
15821 *ops = bkpt_base_breakpoint_ops;
15822 ops->re_set = momentary_bkpt_re_set;
15823 ops->check_status = momentary_bkpt_check_status;
15824 ops->print_it = momentary_bkpt_print_it;
15825 ops->print_mention = momentary_bkpt_print_mention;
15826
e2e4d78b
JK
15827 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15828 ops = &longjmp_breakpoint_ops;
15829 *ops = momentary_breakpoint_ops;
15830 ops->dtor = longjmp_bkpt_dtor;
15831
55aa24fb
SDJ
15832 /* Probe breakpoints. */
15833 ops = &bkpt_probe_breakpoint_ops;
15834 *ops = bkpt_breakpoint_ops;
15835 ops->insert_location = bkpt_probe_insert_location;
15836 ops->remove_location = bkpt_probe_remove_location;
15837 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15838 ops->decode_linespec = bkpt_probe_decode_linespec;
15839
2060206e
PA
15840 /* Watchpoints. */
15841 ops = &watchpoint_breakpoint_ops;
15842 *ops = base_breakpoint_ops;
3a5c3e22 15843 ops->dtor = dtor_watchpoint;
2060206e
PA
15844 ops->re_set = re_set_watchpoint;
15845 ops->insert_location = insert_watchpoint;
15846 ops->remove_location = remove_watchpoint;
15847 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15848 ops->check_status = check_status_watchpoint;
15849 ops->resources_needed = resources_needed_watchpoint;
15850 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15851 ops->print_it = print_it_watchpoint;
15852 ops->print_mention = print_mention_watchpoint;
15853 ops->print_recreate = print_recreate_watchpoint;
427cd150 15854 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15855
15856 /* Masked watchpoints. */
15857 ops = &masked_watchpoint_breakpoint_ops;
15858 *ops = watchpoint_breakpoint_ops;
15859 ops->insert_location = insert_masked_watchpoint;
15860 ops->remove_location = remove_masked_watchpoint;
15861 ops->resources_needed = resources_needed_masked_watchpoint;
15862 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15863 ops->print_it = print_it_masked_watchpoint;
15864 ops->print_one_detail = print_one_detail_masked_watchpoint;
15865 ops->print_mention = print_mention_masked_watchpoint;
15866 ops->print_recreate = print_recreate_masked_watchpoint;
15867
15868 /* Tracepoints. */
15869 ops = &tracepoint_breakpoint_ops;
15870 *ops = base_breakpoint_ops;
15871 ops->re_set = tracepoint_re_set;
15872 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15873 ops->print_one_detail = tracepoint_print_one_detail;
15874 ops->print_mention = tracepoint_print_mention;
15875 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15876 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15877 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15878 ops->decode_linespec = tracepoint_decode_linespec;
15879
55aa24fb
SDJ
15880 /* Probe tracepoints. */
15881 ops = &tracepoint_probe_breakpoint_ops;
15882 *ops = tracepoint_breakpoint_ops;
15883 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15884 ops->decode_linespec = tracepoint_probe_decode_linespec;
15885
983af33b
SDJ
15886 /* Static tracepoints with marker (`-m'). */
15887 ops = &strace_marker_breakpoint_ops;
15888 *ops = tracepoint_breakpoint_ops;
15889 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15890 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15891 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15892
15893 /* Fork catchpoints. */
15894 ops = &catch_fork_breakpoint_ops;
15895 *ops = base_breakpoint_ops;
15896 ops->insert_location = insert_catch_fork;
15897 ops->remove_location = remove_catch_fork;
15898 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15899 ops->print_it = print_it_catch_fork;
15900 ops->print_one = print_one_catch_fork;
15901 ops->print_mention = print_mention_catch_fork;
15902 ops->print_recreate = print_recreate_catch_fork;
15903
15904 /* Vfork catchpoints. */
15905 ops = &catch_vfork_breakpoint_ops;
15906 *ops = base_breakpoint_ops;
15907 ops->insert_location = insert_catch_vfork;
15908 ops->remove_location = remove_catch_vfork;
15909 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15910 ops->print_it = print_it_catch_vfork;
15911 ops->print_one = print_one_catch_vfork;
15912 ops->print_mention = print_mention_catch_vfork;
15913 ops->print_recreate = print_recreate_catch_vfork;
15914
15915 /* Exec catchpoints. */
15916 ops = &catch_exec_breakpoint_ops;
15917 *ops = base_breakpoint_ops;
15918 ops->dtor = dtor_catch_exec;
15919 ops->insert_location = insert_catch_exec;
15920 ops->remove_location = remove_catch_exec;
15921 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15922 ops->print_it = print_it_catch_exec;
15923 ops->print_one = print_one_catch_exec;
15924 ops->print_mention = print_mention_catch_exec;
15925 ops->print_recreate = print_recreate_catch_exec;
15926
15927 /* Syscall catchpoints. */
15928 ops = &catch_syscall_breakpoint_ops;
15929 *ops = base_breakpoint_ops;
15930 ops->dtor = dtor_catch_syscall;
15931 ops->insert_location = insert_catch_syscall;
15932 ops->remove_location = remove_catch_syscall;
15933 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15934 ops->print_it = print_it_catch_syscall;
15935 ops->print_one = print_one_catch_syscall;
15936 ops->print_mention = print_mention_catch_syscall;
15937 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15938
15939 /* Solib-related catchpoints. */
15940 ops = &catch_solib_breakpoint_ops;
15941 *ops = base_breakpoint_ops;
15942 ops->dtor = dtor_catch_solib;
15943 ops->insert_location = insert_catch_solib;
15944 ops->remove_location = remove_catch_solib;
15945 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15946 ops->check_status = check_status_catch_solib;
15947 ops->print_it = print_it_catch_solib;
15948 ops->print_one = print_one_catch_solib;
15949 ops->print_mention = print_mention_catch_solib;
15950 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15951
15952 ops = &dprintf_breakpoint_ops;
15953 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15954 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15955 ops->resources_needed = bkpt_resources_needed;
15956 ops->print_it = bkpt_print_it;
15957 ops->print_mention = bkpt_print_mention;
2d9442cc 15958 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15959 ops->after_condition_true = dprintf_after_condition_true;
2060206e
PA
15960}
15961
8bfd80db
YQ
15962/* Chain containing all defined "enable breakpoint" subcommands. */
15963
15964static struct cmd_list_element *enablebreaklist = NULL;
15965
c906108c 15966void
fba45db2 15967_initialize_breakpoint (void)
c906108c
SS
15968{
15969 struct cmd_list_element *c;
15970
2060206e
PA
15971 initialize_breakpoint_ops ();
15972
84acb35a 15973 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15974 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15975 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15976
55aa24fb
SDJ
15977 breakpoint_objfile_key
15978 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15979
fa3064dd 15980 catch_syscall_inferior_data
8e260fc0
TT
15981 = register_inferior_data_with_cleanup (NULL,
15982 catch_syscall_inferior_data_cleanup);
fa3064dd 15983
c906108c
SS
15984 breakpoint_chain = 0;
15985 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15986 before a breakpoint is set. */
15987 breakpoint_count = 0;
15988
1042e4c0
SS
15989 tracepoint_count = 0;
15990
1bedd215
AC
15991 add_com ("ignore", class_breakpoint, ignore_command, _("\
15992Set ignore-count of breakpoint number N to COUNT.\n\
15993Usage is `ignore N COUNT'."));
c906108c 15994 if (xdb_commands)
c5aa993b 15995 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15996
1bedd215
AC
15997 add_com ("commands", class_breakpoint, commands_command, _("\
15998Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15999Give breakpoint number as argument after \"commands\".\n\
16000With no argument, the targeted breakpoint is the last one set.\n\
16001The commands themselves follow starting on the next line.\n\
16002Type a line containing \"end\" to indicate the end of them.\n\
16003Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16004then no output is printed when it is hit, except what the commands print."));
c906108c 16005
d55637df 16006 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16007Specify breakpoint number N to break only if COND is true.\n\
c906108c 16008Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16009expression to be evaluated whenever breakpoint N is reached."));
d55637df 16010 set_cmd_completer (c, condition_completer);
c906108c 16011
1bedd215 16012 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16013Set a temporary breakpoint.\n\
c906108c
SS
16014Like \"break\" except the breakpoint is only temporary,\n\
16015so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16016by using \"enable delete\" on the breakpoint number.\n\
16017\n"
16018BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16019 set_cmd_completer (c, location_completer);
c94fdfd0 16020
1bedd215 16021 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16022Set a hardware assisted breakpoint.\n\
c906108c 16023Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16024some target hardware may not have this support.\n\
16025\n"
16026BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16027 set_cmd_completer (c, location_completer);
c906108c 16028
1bedd215 16029 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16030Set a temporary hardware assisted breakpoint.\n\
c906108c 16031Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16032so it will be deleted when hit.\n\
16033\n"
16034BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16035 set_cmd_completer (c, location_completer);
c906108c 16036
1bedd215
AC
16037 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16038Enable some breakpoints.\n\
c906108c
SS
16039Give breakpoint numbers (separated by spaces) as arguments.\n\
16040With no subcommand, breakpoints are enabled until you command otherwise.\n\
16041This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16042With a subcommand you can enable temporarily."),
c906108c
SS
16043 &enablelist, "enable ", 1, &cmdlist);
16044 if (xdb_commands)
1bedd215
AC
16045 add_com ("ab", class_breakpoint, enable_command, _("\
16046Enable some breakpoints.\n\
c906108c
SS
16047Give breakpoint numbers (separated by spaces) as arguments.\n\
16048With no subcommand, breakpoints are enabled until you command otherwise.\n\
16049This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16050With a subcommand you can enable temporarily."));
c906108c
SS
16051
16052 add_com_alias ("en", "enable", class_breakpoint, 1);
16053
84951ab5 16054 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16055Enable some breakpoints.\n\
c906108c
SS
16056Give breakpoint numbers (separated by spaces) as arguments.\n\
16057This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16058May be abbreviated to simply \"enable\".\n"),
c5aa993b 16059 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16060
1a966eab
AC
16061 add_cmd ("once", no_class, enable_once_command, _("\
16062Enable breakpoints for one hit. Give breakpoint numbers.\n\
16063If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16064 &enablebreaklist);
16065
1a966eab
AC
16066 add_cmd ("delete", no_class, enable_delete_command, _("\
16067Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16068If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16069 &enablebreaklist);
16070
816338b5
SS
16071 add_cmd ("count", no_class, enable_count_command, _("\
16072Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16073If a breakpoint is hit while enabled in this fashion,\n\
16074the count is decremented; when it reaches zero, the breakpoint is disabled."),
16075 &enablebreaklist);
16076
1a966eab
AC
16077 add_cmd ("delete", no_class, enable_delete_command, _("\
16078Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16079If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16080 &enablelist);
16081
1a966eab
AC
16082 add_cmd ("once", no_class, enable_once_command, _("\
16083Enable breakpoints for one hit. Give breakpoint numbers.\n\
16084If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16085 &enablelist);
16086
16087 add_cmd ("count", no_class, enable_count_command, _("\
16088Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16089If a breakpoint is hit while enabled in this fashion,\n\
16090the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16091 &enablelist);
16092
1bedd215
AC
16093 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16094Disable some breakpoints.\n\
c906108c
SS
16095Arguments are breakpoint numbers with spaces in between.\n\
16096To disable all breakpoints, give no argument.\n\
64b9b334 16097A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16098 &disablelist, "disable ", 1, &cmdlist);
16099 add_com_alias ("dis", "disable", class_breakpoint, 1);
16100 add_com_alias ("disa", "disable", class_breakpoint, 1);
16101 if (xdb_commands)
1bedd215
AC
16102 add_com ("sb", class_breakpoint, disable_command, _("\
16103Disable some breakpoints.\n\
c906108c
SS
16104Arguments are breakpoint numbers with spaces in between.\n\
16105To disable all breakpoints, give no argument.\n\
64b9b334 16106A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16107
1a966eab
AC
16108 add_cmd ("breakpoints", class_alias, disable_command, _("\
16109Disable some breakpoints.\n\
c906108c
SS
16110Arguments are breakpoint numbers with spaces in between.\n\
16111To disable all breakpoints, give no argument.\n\
64b9b334 16112A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16113This command may be abbreviated \"disable\"."),
c906108c
SS
16114 &disablelist);
16115
1bedd215
AC
16116 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16117Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16118Arguments are breakpoint numbers with spaces in between.\n\
16119To delete all breakpoints, give no argument.\n\
16120\n\
16121Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16122The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16123 &deletelist, "delete ", 1, &cmdlist);
16124 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16125 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16126 if (xdb_commands)
1bedd215
AC
16127 add_com ("db", class_breakpoint, delete_command, _("\
16128Delete some breakpoints.\n\
c906108c 16129Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16130To delete all breakpoints, give no argument.\n"));
c906108c 16131
1a966eab
AC
16132 add_cmd ("breakpoints", class_alias, delete_command, _("\
16133Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16134Arguments are breakpoint numbers with spaces in between.\n\
16135To delete all breakpoints, give no argument.\n\
1a966eab 16136This command may be abbreviated \"delete\"."),
c906108c
SS
16137 &deletelist);
16138
1bedd215
AC
16139 add_com ("clear", class_breakpoint, clear_command, _("\
16140Clear breakpoint at specified line or function.\n\
c906108c
SS
16141Argument may be line number, function name, or \"*\" and an address.\n\
16142If line number is specified, all breakpoints in that line are cleared.\n\
16143If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16144If an address is specified, breakpoints at that address are cleared.\n\
16145\n\
16146With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16147is executing in.\n\
16148\n\
1bedd215 16149See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16150 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16151
1bedd215 16152 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16153Set breakpoint at specified line or function.\n"
16154BREAK_ARGS_HELP ("break")));
5ba2abeb 16155 set_cmd_completer (c, location_completer);
c94fdfd0 16156
c906108c
SS
16157 add_com_alias ("b", "break", class_run, 1);
16158 add_com_alias ("br", "break", class_run, 1);
16159 add_com_alias ("bre", "break", class_run, 1);
16160 add_com_alias ("brea", "break", class_run, 1);
16161
7681d515
PM
16162 if (xdb_commands)
16163 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16164
16165 if (dbx_commands)
16166 {
1bedd215
AC
16167 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16168Break in function/address or break at a line in the current file."),
c5aa993b
JM
16169 &stoplist, "stop ", 1, &cmdlist);
16170 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16171 _("Break in function or address."), &stoplist);
c5aa993b 16172 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16173 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16174 add_com ("status", class_info, breakpoints_info, _("\
16175Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16176The \"Type\" column indicates one of:\n\
16177\tbreakpoint - normal breakpoint\n\
16178\twatchpoint - watchpoint\n\
16179The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16180the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16181breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16182address and file/line number respectively.\n\
16183\n\
16184Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16185are set to the address of the last breakpoint listed unless the command\n\
16186is prefixed with \"server \".\n\n\
c906108c 16187Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16188breakpoint set."));
c906108c
SS
16189 }
16190
1bedd215 16191 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16192Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16193The \"Type\" column indicates one of:\n\
16194\tbreakpoint - normal breakpoint\n\
16195\twatchpoint - watchpoint\n\
16196The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16197the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16198breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16199address and file/line number respectively.\n\
16200\n\
16201Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16202are set to the address of the last breakpoint listed unless the command\n\
16203is prefixed with \"server \".\n\n\
c906108c 16204Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16205breakpoint set."));
c906108c 16206
6b04bdb7
MS
16207 add_info_alias ("b", "breakpoints", 1);
16208
c906108c 16209 if (xdb_commands)
1bedd215
AC
16210 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16211Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16212The \"Type\" column indicates one of:\n\
16213\tbreakpoint - normal breakpoint\n\
16214\twatchpoint - watchpoint\n\
16215The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16216the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16217breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16218address and file/line number respectively.\n\
16219\n\
16220Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16221are set to the address of the last breakpoint listed unless the command\n\
16222is prefixed with \"server \".\n\n\
c906108c 16223Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16224breakpoint set."));
c906108c 16225
1a966eab
AC
16226 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16227Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16228The \"Type\" column indicates one of:\n\
16229\tbreakpoint - normal breakpoint\n\
16230\twatchpoint - watchpoint\n\
16231\tlongjmp - internal breakpoint used to step through longjmp()\n\
16232\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16233\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16234\tfinish - internal breakpoint used by the \"finish\" command\n\
16235The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16236the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16237breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16238address and file/line number respectively.\n\
16239\n\
16240Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16241are set to the address of the last breakpoint listed unless the command\n\
16242is prefixed with \"server \".\n\n\
c906108c 16243Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16244breakpoint set."),
c906108c
SS
16245 &maintenanceinfolist);
16246
44feb3ce
TT
16247 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16248Set catchpoints to catch events."),
16249 &catch_cmdlist, "catch ",
16250 0/*allow-unknown*/, &cmdlist);
16251
16252 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16253Set temporary catchpoints to catch events."),
16254 &tcatch_cmdlist, "tcatch ",
16255 0/*allow-unknown*/, &cmdlist);
16256
44feb3ce
TT
16257 add_catch_command ("fork", _("Catch calls to fork."),
16258 catch_fork_command_1,
a96d9b2e 16259 NULL,
44feb3ce
TT
16260 (void *) (uintptr_t) catch_fork_permanent,
16261 (void *) (uintptr_t) catch_fork_temporary);
16262 add_catch_command ("vfork", _("Catch calls to vfork."),
16263 catch_fork_command_1,
a96d9b2e 16264 NULL,
44feb3ce
TT
16265 (void *) (uintptr_t) catch_vfork_permanent,
16266 (void *) (uintptr_t) catch_vfork_temporary);
16267 add_catch_command ("exec", _("Catch calls to exec."),
16268 catch_exec_command_1,
a96d9b2e
SDJ
16269 NULL,
16270 CATCH_PERMANENT,
16271 CATCH_TEMPORARY);
edcc5120
TT
16272 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16273Usage: catch load [REGEX]\n\
16274If REGEX is given, only stop for libraries matching the regular expression."),
16275 catch_load_command_1,
16276 NULL,
16277 CATCH_PERMANENT,
16278 CATCH_TEMPORARY);
16279 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16280Usage: catch unload [REGEX]\n\
16281If REGEX is given, only stop for libraries matching the regular expression."),
16282 catch_unload_command_1,
16283 NULL,
16284 CATCH_PERMANENT,
16285 CATCH_TEMPORARY);
a96d9b2e
SDJ
16286 add_catch_command ("syscall", _("\
16287Catch system calls by their names and/or numbers.\n\
16288Arguments say which system calls to catch. If no arguments\n\
16289are given, every system call will be caught.\n\
16290Arguments, if given, should be one or more system call names\n\
16291(if your system supports that), or system call numbers."),
16292 catch_syscall_command_1,
16293 catch_syscall_completer,
44feb3ce
TT
16294 CATCH_PERMANENT,
16295 CATCH_TEMPORARY);
c5aa993b 16296
1bedd215
AC
16297 c = add_com ("watch", class_breakpoint, watch_command, _("\
16298Set a watchpoint for an expression.\n\
06a64a0b 16299Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16300A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16301an expression changes.\n\
16302If -l or -location is given, this evaluates EXPRESSION and watches\n\
16303the memory to which it refers."));
65d12d83 16304 set_cmd_completer (c, expression_completer);
c906108c 16305
1bedd215
AC
16306 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16307Set a read watchpoint for an expression.\n\
06a64a0b 16308Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16309A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16310an expression is read.\n\
16311If -l or -location is given, this evaluates EXPRESSION and watches\n\
16312the memory to which it refers."));
65d12d83 16313 set_cmd_completer (c, expression_completer);
c906108c 16314
1bedd215
AC
16315 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16316Set a watchpoint for an expression.\n\
06a64a0b 16317Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16318A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16319an expression is either read or written.\n\
16320If -l or -location is given, this evaluates EXPRESSION and watches\n\
16321the memory to which it refers."));
65d12d83 16322 set_cmd_completer (c, expression_completer);
c906108c 16323
d77f58be 16324 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16325Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16326
920d2a44
AC
16327 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16328 respond to changes - contrary to the description. */
85c07804
AC
16329 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16330 &can_use_hw_watchpoints, _("\
16331Set debugger's willingness to use watchpoint hardware."), _("\
16332Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16333If zero, gdb will not use hardware for new watchpoints, even if\n\
16334such is available. (However, any hardware watchpoints that were\n\
16335created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16336hardware.)"),
16337 NULL,
920d2a44 16338 show_can_use_hw_watchpoints,
85c07804 16339 &setlist, &showlist);
c906108c
SS
16340
16341 can_use_hw_watchpoints = 1;
fa8d40ab 16342
1042e4c0
SS
16343 /* Tracepoint manipulation commands. */
16344
16345 c = add_com ("trace", class_breakpoint, trace_command, _("\
16346Set a tracepoint at specified line or function.\n\
16347\n"
16348BREAK_ARGS_HELP ("trace") "\n\
16349Do \"help tracepoints\" for info on other tracepoint commands."));
16350 set_cmd_completer (c, location_completer);
16351
16352 add_com_alias ("tp", "trace", class_alias, 0);
16353 add_com_alias ("tr", "trace", class_alias, 1);
16354 add_com_alias ("tra", "trace", class_alias, 1);
16355 add_com_alias ("trac", "trace", class_alias, 1);
16356
7a697b8d
SS
16357 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16358Set a fast tracepoint at specified line or function.\n\
16359\n"
16360BREAK_ARGS_HELP ("ftrace") "\n\
16361Do \"help tracepoints\" for info on other tracepoint commands."));
16362 set_cmd_completer (c, location_completer);
16363
0fb4aa4b
PA
16364 c = add_com ("strace", class_breakpoint, strace_command, _("\
16365Set a static tracepoint at specified line, function or marker.\n\
16366\n\
16367strace [LOCATION] [if CONDITION]\n\
16368LOCATION may be a line number, function name, \"*\" and an address,\n\
16369or -m MARKER_ID.\n\
16370If a line number is specified, probe the marker at start of code\n\
16371for that line. If a function is specified, probe the marker at start\n\
16372of code for that function. If an address is specified, probe the marker\n\
16373at that exact address. If a marker id is specified, probe the marker\n\
16374with that name. With no LOCATION, uses current execution address of\n\
16375the selected stack frame.\n\
16376Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16377This collects arbitrary user data passed in the probe point call to the\n\
16378tracing library. You can inspect it when analyzing the trace buffer,\n\
16379by printing the $_sdata variable like any other convenience variable.\n\
16380\n\
16381CONDITION is a boolean expression.\n\
16382\n\
d41c0fc8
PA
16383Multiple tracepoints at one place are permitted, and useful if their\n\
16384conditions are different.\n\
0fb4aa4b
PA
16385\n\
16386Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16387Do \"help tracepoints\" for info on other tracepoint commands."));
16388 set_cmd_completer (c, location_completer);
16389
1042e4c0 16390 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16391Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16392Convenience variable \"$tpnum\" contains the number of the\n\
16393last tracepoint set."));
16394
16395 add_info_alias ("tp", "tracepoints", 1);
16396
16397 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16398Delete specified tracepoints.\n\
16399Arguments are tracepoint numbers, separated by spaces.\n\
16400No argument means delete all tracepoints."),
16401 &deletelist);
7e20dfcd 16402 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16403
16404 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16405Disable specified tracepoints.\n\
16406Arguments are tracepoint numbers, separated by spaces.\n\
16407No argument means disable all tracepoints."),
16408 &disablelist);
16409 deprecate_cmd (c, "disable");
16410
16411 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16412Enable specified tracepoints.\n\
16413Arguments are tracepoint numbers, separated by spaces.\n\
16414No argument means enable all tracepoints."),
16415 &enablelist);
16416 deprecate_cmd (c, "enable");
16417
16418 add_com ("passcount", class_trace, trace_pass_command, _("\
16419Set the passcount for a tracepoint.\n\
16420The trace will end when the tracepoint has been passed 'count' times.\n\
16421Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16422if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16423
6149aea9
PA
16424 add_prefix_cmd ("save", class_breakpoint, save_command,
16425 _("Save breakpoint definitions as a script."),
16426 &save_cmdlist, "save ",
16427 0/*allow-unknown*/, &cmdlist);
16428
16429 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16430Save current breakpoint definitions as a script.\n\
cce7e648 16431This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16432catchpoints, tracepoints). Use the 'source' command in another debug\n\
16433session to restore them."),
16434 &save_cmdlist);
16435 set_cmd_completer (c, filename_completer);
16436
16437 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16438Save current tracepoint definitions as a script.\n\
6149aea9
PA
16439Use the 'source' command in another debug session to restore them."),
16440 &save_cmdlist);
1042e4c0
SS
16441 set_cmd_completer (c, filename_completer);
16442
6149aea9
PA
16443 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16444 deprecate_cmd (c, "save tracepoints");
16445
1bedd215 16446 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16447Breakpoint specific settings\n\
16448Configure various breakpoint-specific variables such as\n\
1bedd215 16449pending breakpoint behavior"),
fa8d40ab
JJ
16450 &breakpoint_set_cmdlist, "set breakpoint ",
16451 0/*allow-unknown*/, &setlist);
1bedd215 16452 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16453Breakpoint specific settings\n\
16454Configure various breakpoint-specific variables such as\n\
1bedd215 16455pending breakpoint behavior"),
fa8d40ab
JJ
16456 &breakpoint_show_cmdlist, "show breakpoint ",
16457 0/*allow-unknown*/, &showlist);
16458
7915a72c
AC
16459 add_setshow_auto_boolean_cmd ("pending", no_class,
16460 &pending_break_support, _("\
16461Set debugger's behavior regarding pending breakpoints."), _("\
16462Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16463If on, an unrecognized breakpoint location will cause gdb to create a\n\
16464pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16465an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16466user-query to see if a pending breakpoint should be created."),
2c5b56ce 16467 NULL,
920d2a44 16468 show_pending_break_support,
6e1d7d6c
AC
16469 &breakpoint_set_cmdlist,
16470 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16471
16472 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16473
16474 add_setshow_boolean_cmd ("auto-hw", no_class,
16475 &automatic_hardware_breakpoints, _("\
16476Set automatic usage of hardware breakpoints."), _("\
16477Show automatic usage of hardware breakpoints."), _("\
16478If set, the debugger will automatically use hardware breakpoints for\n\
16479breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16480a warning will be emitted for such breakpoints."),
16481 NULL,
16482 show_automatic_hardware_breakpoints,
16483 &breakpoint_set_cmdlist,
16484 &breakpoint_show_cmdlist);
74960c60 16485
72d0e2c5
YQ
16486 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16487 &always_inserted_mode, _("\
74960c60
VP
16488Set mode for inserting breakpoints."), _("\
16489Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16490When this mode is off, breakpoints are inserted in inferior when it is\n\
16491resumed, and removed when execution stops. When this mode is on,\n\
16492breakpoints are inserted immediately and removed only when the user\n\
16493deletes the breakpoint. When this mode is auto (which is the default),\n\
16494the behaviour depends on the non-stop setting (see help set non-stop).\n\
16495In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16496behaves as if always-inserted mode is on; if gdb is controlling the\n\
16497inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16498 NULL,
16499 &show_always_inserted_mode,
16500 &breakpoint_set_cmdlist,
16501 &breakpoint_show_cmdlist);
f1310107 16502
b775012e
LM
16503 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16504 condition_evaluation_enums,
16505 &condition_evaluation_mode_1, _("\
16506Set mode of breakpoint condition evaluation."), _("\
16507Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16508When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16509evaluated on the host's side by GDB. When it is set to \"target\",\n\
16510breakpoint conditions will be downloaded to the target (if the target\n\
16511supports such feature) and conditions will be evaluated on the target's side.\n\
16512If this is set to \"auto\" (default), this will be automatically set to\n\
16513\"target\" if it supports condition evaluation, otherwise it will\n\
16514be set to \"gdb\""),
16515 &set_condition_evaluation_mode,
16516 &show_condition_evaluation_mode,
16517 &breakpoint_set_cmdlist,
16518 &breakpoint_show_cmdlist);
16519
f1310107
TJB
16520 add_com ("break-range", class_breakpoint, break_range_command, _("\
16521Set a breakpoint for an address range.\n\
16522break-range START-LOCATION, END-LOCATION\n\
16523where START-LOCATION and END-LOCATION can be one of the following:\n\
16524 LINENUM, for that line in the current file,\n\
16525 FILE:LINENUM, for that line in that file,\n\
16526 +OFFSET, for that number of lines after the current line\n\
16527 or the start of the range\n\
16528 FUNCTION, for the first line in that function,\n\
16529 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16530 *ADDRESS, for the instruction at that address.\n\
16531\n\
16532The breakpoint will stop execution of the inferior whenever it executes\n\
16533an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16534range (including START-LOCATION and END-LOCATION)."));
16535
e7e0cddf
SS
16536 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16537Set a dynamic printf at specified line or function.\n\
16538dprintf location,format string,arg1,arg2,...\n\
16539location may be a line number, function name, or \"*\" and an address.\n\
16540If a line number is specified, break at start of code for that line.\n\
0e4777df 16541If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16542 set_cmd_completer (c, location_completer);
16543
16544 add_setshow_enum_cmd ("dprintf-style", class_support,
16545 dprintf_style_enums, &dprintf_style, _("\
16546Set the style of usage for dynamic printf."), _("\
16547Show the style of usage for dynamic printf."), _("\
16548This setting chooses how GDB will do a dynamic printf.\n\
16549If the value is \"gdb\", then the printing is done by GDB to its own\n\
16550console, as with the \"printf\" command.\n\
16551If the value is \"call\", the print is done by calling a function in your\n\
16552program; by default printf(), but you can choose a different function or\n\
16553output stream by setting dprintf-function and dprintf-channel."),
16554 update_dprintf_commands, NULL,
16555 &setlist, &showlist);
16556
16557 dprintf_function = xstrdup ("printf");
16558 add_setshow_string_cmd ("dprintf-function", class_support,
16559 &dprintf_function, _("\
16560Set the function to use for dynamic printf"), _("\
16561Show the function to use for dynamic printf"), NULL,
16562 update_dprintf_commands, NULL,
16563 &setlist, &showlist);
16564
16565 dprintf_channel = xstrdup ("");
16566 add_setshow_string_cmd ("dprintf-channel", class_support,
16567 &dprintf_channel, _("\
16568Set the channel to use for dynamic printf"), _("\
16569Show the channel to use for dynamic printf"), NULL,
16570 update_dprintf_commands, NULL,
16571 &setlist, &showlist);
16572
d3ce09f5
SS
16573 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16574 &disconnected_dprintf, _("\
16575Set whether dprintf continues after GDB disconnects."), _("\
16576Show whether dprintf continues after GDB disconnects."), _("\
16577Use this to let dprintf commands continue to hit and produce output\n\
16578even if GDB disconnects or detaches from the target."),
16579 NULL,
16580 NULL,
16581 &setlist, &showlist);
16582
16583 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16584agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16585(target agent only) This is useful for formatted output in user-defined commands."));
16586
765dc015 16587 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16588
16589 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16590}
This page took 2.510704 seconds and 4 git commands to generate.