gdbserver: on GDB breakpoint reinsertion, also delete the breakpoint's commands.
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
0e9f083f 39#include <string.h>
50f182aa 40#include "gdb-demangle.h"
0ba1096a 41#include "filenames.h"
c906108c
SS
42#include "annotate.h"
43#include "symfile.h"
44#include "objfiles.h"
0378c332 45#include "source.h"
c5f0f3d0 46#include "linespec.h"
c94fdfd0 47#include "completer.h"
5b7f31a4 48#include "gdb.h"
8b93c638 49#include "ui-out.h"
e1507482 50#include "cli/cli-script.h"
0225421b 51#include "gdb_assert.h"
fe898f56 52#include "block.h"
a77053c2 53#include "solib.h"
84acb35a
JJ
54#include "solist.h"
55#include "observer.h"
60250e8b 56#include "exceptions.h"
765dc015 57#include "memattr.h"
f7f9143b 58#include "ada-lang.h"
d1aa2f50 59#include "top.h"
79a45b7d 60#include "valprint.h"
4efc6507 61#include "jit.h"
a96d9b2e 62#include "xml-syscall.h"
65d79d4b 63#include "parser-defs.h"
55aa24fb
SDJ
64#include "gdb_regex.h"
65#include "probe.h"
e9cafbcc 66#include "cli/cli-utils.h"
be34f849 67#include "continuations.h"
1bfeeb0f
JL
68#include "stack.h"
69#include "skip.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"
6dddc817 83#include "extension.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
85d721b8
PA
169static int watchpoint_locations_match (struct bp_location *loc1,
170 struct bp_location *loc2);
171
f1310107
TJB
172static int breakpoint_location_address_match (struct bp_location *bl,
173 struct address_space *aspace,
174 CORE_ADDR addr);
175
a14ed312 176static void breakpoints_info (char *, int);
c906108c 177
d77f58be
SS
178static void watchpoints_info (char *, int);
179
e5a67952
MS
180static int breakpoint_1 (char *, int,
181 int (*) (const struct breakpoint *));
c906108c 182
4efb68b1 183static int breakpoint_cond_eval (void *);
c906108c 184
4efb68b1 185static void cleanup_executing_breakpoints (void *);
c906108c 186
a14ed312 187static void commands_command (char *, int);
c906108c 188
a14ed312 189static void condition_command (char *, int);
c906108c 190
c5aa993b
JM
191typedef enum
192 {
193 mark_inserted,
194 mark_uninserted
195 }
196insertion_state_t;
c906108c 197
0bde7532 198static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 199static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 200
e514a9d6 201static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 202
4efb68b1 203static int watchpoint_check (void *);
c906108c 204
a14ed312 205static void maintenance_info_breakpoints (char *, int);
c906108c 206
a14ed312 207static int hw_breakpoint_used_count (void);
c906108c 208
a1398e0c
PA
209static int hw_watchpoint_use_count (struct breakpoint *);
210
211static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
c906108c 214
a14ed312 215static void hbreak_command (char *, int);
c906108c 216
a14ed312 217static void thbreak_command (char *, int);
c906108c 218
816338b5
SS
219static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
c906108c 221
a14ed312 222static void stop_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopin_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void stopat_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
d03285ec
UW
230static void detach_single_step_breakpoints (void);
231
fe3f5fa8 232static void free_bp_location (struct bp_location *loc);
f431efe5
PA
233static void incref_bp_location (struct bp_location *loc);
234static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 235
39d61571 236static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 237
b60e7edf 238static void update_global_location_list (int);
a5606eee 239
b60e7edf 240static void update_global_location_list_nothrow (int);
74960c60 241
d77f58be 242static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
243
244static void insert_breakpoint_locations (void);
a5606eee 245
a96d9b2e
SDJ
246static int syscall_catchpoint_p (struct breakpoint *b);
247
1042e4c0
SS
248static void tracepoints_info (char *, int);
249
250static void delete_trace_command (char *, int);
251
252static void enable_trace_command (char *, int);
253
254static void disable_trace_command (char *, int);
255
256static void trace_pass_command (char *, int);
257
558a9d82
YQ
258static void set_tracepoint_count (int num);
259
9c06b0b4
TJB
260static int is_masked_watchpoint (const struct breakpoint *b);
261
b775012e
LM
262static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
263
983af33b
SDJ
264/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
265 otherwise. */
266
267static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 268
2060206e
PA
269/* The abstract base class all breakpoint_ops structures inherit
270 from. */
ab04a2af 271struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
272
273/* The breakpoint_ops structure to be inherited by all breakpoint_ops
274 that are implemented on top of software or hardware breakpoints
275 (user breakpoints, internal and momentary breakpoints, etc.). */
276static struct breakpoint_ops bkpt_base_breakpoint_ops;
277
278/* Internal breakpoints class type. */
06edf0c0 279static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
280
281/* Momentary breakpoints class type. */
06edf0c0
PA
282static struct breakpoint_ops momentary_breakpoint_ops;
283
e2e4d78b
JK
284/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
285static struct breakpoint_ops longjmp_breakpoint_ops;
286
2060206e
PA
287/* The breakpoint_ops structure to be used in regular user created
288 breakpoints. */
289struct breakpoint_ops bkpt_breakpoint_ops;
290
55aa24fb
SDJ
291/* Breakpoints set on probes. */
292static struct breakpoint_ops bkpt_probe_breakpoint_ops;
293
e7e0cddf 294/* Dynamic printf class type. */
c5867ab6 295struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 296
9d497a19
PA
297/* One (or perhaps two) breakpoints used for software single
298 stepping. */
299
300static void *single_step_breakpoints[2];
301static struct gdbarch *single_step_gdbarch[2];
302
d3ce09f5
SS
303/* The style in which to perform a dynamic printf. This is a user
304 option because different output options have different tradeoffs;
305 if GDB does the printing, there is better error handling if there
306 is a problem with any of the arguments, but using an inferior
307 function lets you have special-purpose printers and sending of
308 output to the same place as compiled-in print functions. */
309
310static const char dprintf_style_gdb[] = "gdb";
311static const char dprintf_style_call[] = "call";
312static const char dprintf_style_agent[] = "agent";
313static const char *const dprintf_style_enums[] = {
314 dprintf_style_gdb,
315 dprintf_style_call,
316 dprintf_style_agent,
317 NULL
318};
319static const char *dprintf_style = dprintf_style_gdb;
320
321/* The function to use for dynamic printf if the preferred style is to
322 call into the inferior. The value is simply a string that is
323 copied into the command, so it can be anything that GDB can
324 evaluate to a callable address, not necessarily a function name. */
325
326static char *dprintf_function = "";
327
328/* The channel to use for dynamic printf if the preferred style is to
329 call into the inferior; if a nonempty string, it will be passed to
330 the call as the first argument, with the format string as the
331 second. As with the dprintf function, this can be anything that
332 GDB knows how to evaluate, so in addition to common choices like
333 "stderr", this could be an app-specific expression like
334 "mystreams[curlogger]". */
335
336static char *dprintf_channel = "";
337
338/* True if dprintf commands should continue to operate even if GDB
339 has disconnected. */
340static int disconnected_dprintf = 1;
341
5cea2a26
PA
342/* A reference-counted struct command_line. This lets multiple
343 breakpoints share a single command list. */
344struct counted_command_line
345{
346 /* The reference count. */
347 int refc;
348
349 /* The command list. */
350 struct command_line *commands;
351};
352
353struct command_line *
354breakpoint_commands (struct breakpoint *b)
355{
356 return b->commands ? b->commands->commands : NULL;
357}
3daf8fe5 358
f3b1572e
PA
359/* Flag indicating that a command has proceeded the inferior past the
360 current breakpoint. */
361
362static int breakpoint_proceeded;
363
956a9fb9 364const char *
2cec12e5
AR
365bpdisp_text (enum bpdisp disp)
366{
4a64f543
MS
367 /* NOTE: the following values are a part of MI protocol and
368 represent values of 'disp' field returned when inferior stops at
369 a breakpoint. */
bc043ef3 370 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 371
2cec12e5
AR
372 return bpdisps[(int) disp];
373}
c906108c 374
4a64f543 375/* Prototypes for exported functions. */
c906108c 376/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 377 if such is available. */
c906108c
SS
378static int can_use_hw_watchpoints;
379
920d2a44
AC
380static void
381show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
382 struct cmd_list_element *c,
383 const char *value)
384{
3e43a32a
MS
385 fprintf_filtered (file,
386 _("Debugger's willingness to use "
387 "watchpoint hardware is %s.\n"),
920d2a44
AC
388 value);
389}
390
fa8d40ab
JJ
391/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
392 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 393 for unrecognized breakpoint locations.
fa8d40ab
JJ
394 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
395static enum auto_boolean pending_break_support;
920d2a44
AC
396static void
397show_pending_break_support (struct ui_file *file, int from_tty,
398 struct cmd_list_element *c,
399 const char *value)
400{
3e43a32a
MS
401 fprintf_filtered (file,
402 _("Debugger's behavior regarding "
403 "pending breakpoints is %s.\n"),
920d2a44
AC
404 value);
405}
fa8d40ab 406
765dc015 407/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 408 set with "break" but falling in read-only memory.
765dc015
VP
409 If 0, gdb will warn about such breakpoints, but won't automatically
410 use hardware breakpoints. */
411static int automatic_hardware_breakpoints;
412static void
413show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
414 struct cmd_list_element *c,
415 const char *value)
416{
3e43a32a
MS
417 fprintf_filtered (file,
418 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
419 value);
420}
421
33e5cbd6
PA
422/* If on, gdb will keep breakpoints inserted even as inferior is
423 stopped, and immediately insert any new breakpoints. If off, gdb
424 will insert breakpoints into inferior only when resuming it, and
425 will remove breakpoints upon stop. If auto, GDB will behave as ON
426 if in non-stop mode, and as OFF if all-stop mode.*/
427
72d0e2c5
YQ
428static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
429
33e5cbd6 430static void
74960c60 431show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 432 struct cmd_list_element *c, const char *value)
74960c60 433{
72d0e2c5 434 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
435 fprintf_filtered (file,
436 _("Always inserted breakpoint "
437 "mode is %s (currently %s).\n"),
33e5cbd6
PA
438 value,
439 breakpoints_always_inserted_mode () ? "on" : "off");
440 else
3e43a32a
MS
441 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
442 value);
74960c60
VP
443}
444
33e5cbd6
PA
445int
446breakpoints_always_inserted_mode (void)
447{
72d0e2c5
YQ
448 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
449 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 450}
765dc015 451
b775012e
LM
452static const char condition_evaluation_both[] = "host or target";
453
454/* Modes for breakpoint condition evaluation. */
455static const char condition_evaluation_auto[] = "auto";
456static const char condition_evaluation_host[] = "host";
457static const char condition_evaluation_target[] = "target";
458static const char *const condition_evaluation_enums[] = {
459 condition_evaluation_auto,
460 condition_evaluation_host,
461 condition_evaluation_target,
462 NULL
463};
464
465/* Global that holds the current mode for breakpoint condition evaluation. */
466static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
467
468/* Global that we use to display information to the user (gets its value from
469 condition_evaluation_mode_1. */
470static const char *condition_evaluation_mode = condition_evaluation_auto;
471
472/* Translate a condition evaluation mode MODE into either "host"
473 or "target". This is used mostly to translate from "auto" to the
474 real setting that is being used. It returns the translated
475 evaluation mode. */
476
477static const char *
478translate_condition_evaluation_mode (const char *mode)
479{
480 if (mode == condition_evaluation_auto)
481 {
482 if (target_supports_evaluation_of_breakpoint_conditions ())
483 return condition_evaluation_target;
484 else
485 return condition_evaluation_host;
486 }
487 else
488 return mode;
489}
490
491/* Discovers what condition_evaluation_auto translates to. */
492
493static const char *
494breakpoint_condition_evaluation_mode (void)
495{
496 return translate_condition_evaluation_mode (condition_evaluation_mode);
497}
498
499/* Return true if GDB should evaluate breakpoint conditions or false
500 otherwise. */
501
502static int
503gdb_evaluates_breakpoint_condition_p (void)
504{
505 const char *mode = breakpoint_condition_evaluation_mode ();
506
507 return (mode == condition_evaluation_host);
508}
509
a14ed312 510void _initialize_breakpoint (void);
c906108c 511
c906108c
SS
512/* Are we executing breakpoint commands? */
513static int executing_breakpoint_commands;
514
c02f5703
MS
515/* Are overlay event breakpoints enabled? */
516static int overlay_events_enabled;
517
e09342b5
TJB
518/* See description in breakpoint.h. */
519int target_exact_watchpoints = 0;
520
c906108c 521/* Walk the following statement or block through all breakpoints.
e5dd4106 522 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 523 current breakpoint. */
c906108c 524
5c44784c 525#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 526
5c44784c
JM
527#define ALL_BREAKPOINTS_SAFE(B,TMP) \
528 for (B = breakpoint_chain; \
529 B ? (TMP=B->next, 1): 0; \
530 B = TMP)
c906108c 531
4a64f543
MS
532/* Similar iterator for the low-level breakpoints. SAFE variant is
533 not provided so update_global_location_list must not be called
534 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 535
876fa593
JK
536#define ALL_BP_LOCATIONS(B,BP_TMP) \
537 for (BP_TMP = bp_location; \
538 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
539 BP_TMP++)
7cc221ef 540
b775012e
LM
541/* Iterates through locations with address ADDRESS for the currently selected
542 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
543 to where the loop should start from.
544 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
545 appropriate location to start with. */
546
547#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
548 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
549 BP_LOCP_TMP = BP_LOCP_START; \
550 BP_LOCP_START \
551 && (BP_LOCP_TMP < bp_location + bp_location_count \
552 && (*BP_LOCP_TMP)->address == ADDRESS); \
553 BP_LOCP_TMP++)
554
1042e4c0
SS
555/* Iterator for tracepoints only. */
556
557#define ALL_TRACEPOINTS(B) \
558 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 559 if (is_tracepoint (B))
1042e4c0 560
7cc221ef 561/* Chains of all breakpoints defined. */
c906108c
SS
562
563struct breakpoint *breakpoint_chain;
564
876fa593
JK
565/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
566
567static struct bp_location **bp_location;
568
569/* Number of elements of BP_LOCATION. */
570
571static unsigned bp_location_count;
572
4a64f543
MS
573/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
574 ADDRESS for the current elements of BP_LOCATION which get a valid
575 result from bp_location_has_shadow. You can use it for roughly
576 limiting the subrange of BP_LOCATION to scan for shadow bytes for
577 an address you need to read. */
876fa593
JK
578
579static CORE_ADDR bp_location_placed_address_before_address_max;
580
4a64f543
MS
581/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
582 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
583 BP_LOCATION which get a valid result from bp_location_has_shadow.
584 You can use it for roughly limiting the subrange of BP_LOCATION to
585 scan for shadow bytes for an address you need to read. */
876fa593
JK
586
587static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 588
4a64f543
MS
589/* The locations that no longer correspond to any breakpoint, unlinked
590 from bp_location array, but for which a hit may still be reported
591 by a target. */
20874c92
VP
592VEC(bp_location_p) *moribund_locations = NULL;
593
c906108c
SS
594/* Number of last breakpoint made. */
595
95a42b64
TT
596static int breakpoint_count;
597
86b17b60
PA
598/* The value of `breakpoint_count' before the last command that
599 created breakpoints. If the last (break-like) command created more
600 than one breakpoint, then the difference between BREAKPOINT_COUNT
601 and PREV_BREAKPOINT_COUNT is more than one. */
602static int prev_breakpoint_count;
c906108c 603
1042e4c0
SS
604/* Number of last tracepoint made. */
605
95a42b64 606static int tracepoint_count;
1042e4c0 607
6149aea9
PA
608static struct cmd_list_element *breakpoint_set_cmdlist;
609static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 610struct cmd_list_element *save_cmdlist;
6149aea9 611
468d015d
JJ
612/* Return whether a breakpoint is an active enabled breakpoint. */
613static int
614breakpoint_enabled (struct breakpoint *b)
615{
0d381245 616 return (b->enable_state == bp_enabled);
468d015d
JJ
617}
618
c906108c
SS
619/* Set breakpoint count to NUM. */
620
95a42b64 621static void
fba45db2 622set_breakpoint_count (int num)
c906108c 623{
86b17b60 624 prev_breakpoint_count = breakpoint_count;
c906108c 625 breakpoint_count = num;
4fa62494 626 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
627}
628
86b17b60
PA
629/* Used by `start_rbreak_breakpoints' below, to record the current
630 breakpoint count before "rbreak" creates any breakpoint. */
631static int rbreak_start_breakpoint_count;
632
95a42b64
TT
633/* Called at the start an "rbreak" command to record the first
634 breakpoint made. */
86b17b60 635
95a42b64
TT
636void
637start_rbreak_breakpoints (void)
638{
86b17b60 639 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
640}
641
642/* Called at the end of an "rbreak" command to record the last
643 breakpoint made. */
86b17b60 644
95a42b64
TT
645void
646end_rbreak_breakpoints (void)
647{
86b17b60 648 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
649}
650
4a64f543 651/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
652
653void
fba45db2 654clear_breakpoint_hit_counts (void)
c906108c
SS
655{
656 struct breakpoint *b;
657
658 ALL_BREAKPOINTS (b)
659 b->hit_count = 0;
660}
661
9add0f1b
TT
662/* Allocate a new counted_command_line with reference count of 1.
663 The new structure owns COMMANDS. */
664
665static struct counted_command_line *
666alloc_counted_command_line (struct command_line *commands)
667{
668 struct counted_command_line *result
669 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 670
9add0f1b
TT
671 result->refc = 1;
672 result->commands = commands;
673 return result;
674}
675
676/* Increment reference count. This does nothing if CMD is NULL. */
677
678static void
679incref_counted_command_line (struct counted_command_line *cmd)
680{
681 if (cmd)
682 ++cmd->refc;
683}
684
685/* Decrement reference count. If the reference count reaches 0,
686 destroy the counted_command_line. Sets *CMDP to NULL. This does
687 nothing if *CMDP is NULL. */
688
689static void
690decref_counted_command_line (struct counted_command_line **cmdp)
691{
692 if (*cmdp)
693 {
694 if (--(*cmdp)->refc == 0)
695 {
696 free_command_lines (&(*cmdp)->commands);
697 xfree (*cmdp);
698 }
699 *cmdp = NULL;
700 }
701}
702
703/* A cleanup function that calls decref_counted_command_line. */
704
705static void
706do_cleanup_counted_command_line (void *arg)
707{
708 decref_counted_command_line (arg);
709}
710
711/* Create a cleanup that calls decref_counted_command_line on the
712 argument. */
713
714static struct cleanup *
715make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
716{
717 return make_cleanup (do_cleanup_counted_command_line, cmdp);
718}
719
c906108c 720\f
48cb2d85
VP
721/* Return the breakpoint with the specified number, or NULL
722 if the number does not refer to an existing breakpoint. */
723
724struct breakpoint *
725get_breakpoint (int num)
726{
727 struct breakpoint *b;
728
729 ALL_BREAKPOINTS (b)
730 if (b->number == num)
731 return b;
732
733 return NULL;
734}
5c44784c 735
c906108c 736\f
adc36818 737
b775012e
LM
738/* Mark locations as "conditions have changed" in case the target supports
739 evaluating conditions on its side. */
740
741static void
742mark_breakpoint_modified (struct breakpoint *b)
743{
744 struct bp_location *loc;
745
746 /* This is only meaningful if the target is
747 evaluating conditions and if the user has
748 opted for condition evaluation on the target's
749 side. */
750 if (gdb_evaluates_breakpoint_condition_p ()
751 || !target_supports_evaluation_of_breakpoint_conditions ())
752 return;
753
754 if (!is_breakpoint (b))
755 return;
756
757 for (loc = b->loc; loc; loc = loc->next)
758 loc->condition_changed = condition_modified;
759}
760
761/* Mark location as "conditions have changed" in case the target supports
762 evaluating conditions on its side. */
763
764static void
765mark_breakpoint_location_modified (struct bp_location *loc)
766{
767 /* This is only meaningful if the target is
768 evaluating conditions and if the user has
769 opted for condition evaluation on the target's
770 side. */
771 if (gdb_evaluates_breakpoint_condition_p ()
772 || !target_supports_evaluation_of_breakpoint_conditions ())
773
774 return;
775
776 if (!is_breakpoint (loc->owner))
777 return;
778
779 loc->condition_changed = condition_modified;
780}
781
782/* Sets the condition-evaluation mode using the static global
783 condition_evaluation_mode. */
784
785static void
786set_condition_evaluation_mode (char *args, int from_tty,
787 struct cmd_list_element *c)
788{
b775012e
LM
789 const char *old_mode, *new_mode;
790
791 if ((condition_evaluation_mode_1 == condition_evaluation_target)
792 && !target_supports_evaluation_of_breakpoint_conditions ())
793 {
794 condition_evaluation_mode_1 = condition_evaluation_mode;
795 warning (_("Target does not support breakpoint condition evaluation.\n"
796 "Using host evaluation mode instead."));
797 return;
798 }
799
800 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
801 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
802
abf1152a
JK
803 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
804 settings was "auto". */
805 condition_evaluation_mode = condition_evaluation_mode_1;
806
b775012e
LM
807 /* Only update the mode if the user picked a different one. */
808 if (new_mode != old_mode)
809 {
810 struct bp_location *loc, **loc_tmp;
811 /* If the user switched to a different evaluation mode, we
812 need to synch the changes with the target as follows:
813
814 "host" -> "target": Send all (valid) conditions to the target.
815 "target" -> "host": Remove all the conditions from the target.
816 */
817
b775012e
LM
818 if (new_mode == condition_evaluation_target)
819 {
820 /* Mark everything modified and synch conditions with the
821 target. */
822 ALL_BP_LOCATIONS (loc, loc_tmp)
823 mark_breakpoint_location_modified (loc);
824 }
825 else
826 {
827 /* Manually mark non-duplicate locations to synch conditions
828 with the target. We do this to remove all the conditions the
829 target knows about. */
830 ALL_BP_LOCATIONS (loc, loc_tmp)
831 if (is_breakpoint (loc->owner) && loc->inserted)
832 loc->needs_update = 1;
833 }
834
835 /* Do the update. */
836 update_global_location_list (1);
837 }
838
839 return;
840}
841
842/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
843 what "auto" is translating to. */
844
845static void
846show_condition_evaluation_mode (struct ui_file *file, int from_tty,
847 struct cmd_list_element *c, const char *value)
848{
849 if (condition_evaluation_mode == condition_evaluation_auto)
850 fprintf_filtered (file,
851 _("Breakpoint condition evaluation "
852 "mode is %s (currently %s).\n"),
853 value,
854 breakpoint_condition_evaluation_mode ());
855 else
856 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
857 value);
858}
859
860/* A comparison function for bp_location AP and BP that is used by
861 bsearch. This comparison function only cares about addresses, unlike
862 the more general bp_location_compare function. */
863
864static int
865bp_location_compare_addrs (const void *ap, const void *bp)
866{
867 struct bp_location *a = *(void **) ap;
868 struct bp_location *b = *(void **) bp;
869
870 if (a->address == b->address)
871 return 0;
872 else
873 return ((a->address > b->address) - (a->address < b->address));
874}
875
876/* Helper function to skip all bp_locations with addresses
877 less than ADDRESS. It returns the first bp_location that
878 is greater than or equal to ADDRESS. If none is found, just
879 return NULL. */
880
881static struct bp_location **
882get_first_locp_gte_addr (CORE_ADDR address)
883{
884 struct bp_location dummy_loc;
885 struct bp_location *dummy_locp = &dummy_loc;
886 struct bp_location **locp_found = NULL;
887
888 /* Initialize the dummy location's address field. */
889 memset (&dummy_loc, 0, sizeof (struct bp_location));
890 dummy_loc.address = address;
891
892 /* Find a close match to the first location at ADDRESS. */
893 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
894 sizeof (struct bp_location **),
895 bp_location_compare_addrs);
896
897 /* Nothing was found, nothing left to do. */
898 if (locp_found == NULL)
899 return NULL;
900
901 /* We may have found a location that is at ADDRESS but is not the first in the
902 location's list. Go backwards (if possible) and locate the first one. */
903 while ((locp_found - 1) >= bp_location
904 && (*(locp_found - 1))->address == address)
905 locp_found--;
906
907 return locp_found;
908}
909
adc36818
PM
910void
911set_breakpoint_condition (struct breakpoint *b, char *exp,
912 int from_tty)
913{
3a5c3e22
PA
914 xfree (b->cond_string);
915 b->cond_string = NULL;
adc36818 916
3a5c3e22 917 if (is_watchpoint (b))
adc36818 918 {
3a5c3e22
PA
919 struct watchpoint *w = (struct watchpoint *) b;
920
921 xfree (w->cond_exp);
922 w->cond_exp = NULL;
923 }
924 else
925 {
926 struct bp_location *loc;
927
928 for (loc = b->loc; loc; loc = loc->next)
929 {
930 xfree (loc->cond);
931 loc->cond = NULL;
b775012e
LM
932
933 /* No need to free the condition agent expression
934 bytecode (if we have one). We will handle this
935 when we go through update_global_location_list. */
3a5c3e22 936 }
adc36818 937 }
adc36818
PM
938
939 if (*exp == 0)
940 {
941 if (from_tty)
942 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
943 }
944 else
945 {
bbc13ae3 946 const char *arg = exp;
cc59ec59 947
adc36818
PM
948 /* I don't know if it matters whether this is the string the user
949 typed in or the decompiled expression. */
950 b->cond_string = xstrdup (arg);
951 b->condition_not_parsed = 0;
952
953 if (is_watchpoint (b))
954 {
3a5c3e22
PA
955 struct watchpoint *w = (struct watchpoint *) b;
956
adc36818
PM
957 innermost_block = NULL;
958 arg = exp;
1bb9788d 959 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
960 if (*arg)
961 error (_("Junk at end of expression"));
3a5c3e22 962 w->cond_exp_valid_block = innermost_block;
adc36818
PM
963 }
964 else
965 {
3a5c3e22
PA
966 struct bp_location *loc;
967
adc36818
PM
968 for (loc = b->loc; loc; loc = loc->next)
969 {
970 arg = exp;
971 loc->cond =
1bb9788d
TT
972 parse_exp_1 (&arg, loc->address,
973 block_for_pc (loc->address), 0);
adc36818
PM
974 if (*arg)
975 error (_("Junk at end of expression"));
976 }
977 }
978 }
b775012e
LM
979 mark_breakpoint_modified (b);
980
8d3788bd 981 observer_notify_breakpoint_modified (b);
adc36818
PM
982}
983
d55637df
TT
984/* Completion for the "condition" command. */
985
986static VEC (char_ptr) *
6f937416
PA
987condition_completer (struct cmd_list_element *cmd,
988 const char *text, const char *word)
d55637df 989{
6f937416 990 const char *space;
d55637df 991
6f937416
PA
992 text = skip_spaces_const (text);
993 space = skip_to_space_const (text);
d55637df
TT
994 if (*space == '\0')
995 {
996 int len;
997 struct breakpoint *b;
998 VEC (char_ptr) *result = NULL;
999
1000 if (text[0] == '$')
1001 {
1002 /* We don't support completion of history indices. */
1003 if (isdigit (text[1]))
1004 return NULL;
1005 return complete_internalvar (&text[1]);
1006 }
1007
1008 /* We're completing the breakpoint number. */
1009 len = strlen (text);
1010
1011 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1012 {
1013 char number[50];
1014
1015 xsnprintf (number, sizeof (number), "%d", b->number);
1016
1017 if (strncmp (number, text, len) == 0)
1018 VEC_safe_push (char_ptr, result, xstrdup (number));
1019 }
d55637df
TT
1020
1021 return result;
1022 }
1023
1024 /* We're completing the expression part. */
6f937416 1025 text = skip_spaces_const (space);
d55637df
TT
1026 return expression_completer (cmd, text, word);
1027}
1028
c906108c
SS
1029/* condition N EXP -- set break condition of breakpoint N to EXP. */
1030
1031static void
fba45db2 1032condition_command (char *arg, int from_tty)
c906108c 1033{
52f0bd74 1034 struct breakpoint *b;
c906108c 1035 char *p;
52f0bd74 1036 int bnum;
c906108c
SS
1037
1038 if (arg == 0)
e2e0b3e5 1039 error_no_arg (_("breakpoint number"));
c906108c
SS
1040
1041 p = arg;
1042 bnum = get_number (&p);
5c44784c 1043 if (bnum == 0)
8a3fe4f8 1044 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1045
1046 ALL_BREAKPOINTS (b)
1047 if (b->number == bnum)
2f069f6f 1048 {
6dddc817
DE
1049 /* Check if this breakpoint has a "stop" method implemented in an
1050 extension language. This method and conditions entered into GDB
1051 from the CLI are mutually exclusive. */
1052 const struct extension_language_defn *extlang
1053 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1054
1055 if (extlang != NULL)
1056 {
1057 error (_("Only one stop condition allowed. There is currently"
1058 " a %s stop condition defined for this breakpoint."),
1059 ext_lang_capitalized_name (extlang));
1060 }
2566ad2d 1061 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1062
1063 if (is_breakpoint (b))
1064 update_global_location_list (1);
1065
2f069f6f
JB
1066 return;
1067 }
c906108c 1068
8a3fe4f8 1069 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1070}
1071
a7bdde9e
VP
1072/* Check that COMMAND do not contain commands that are suitable
1073 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1074 Throw if any such commands is found. */
1075
a7bdde9e
VP
1076static void
1077check_no_tracepoint_commands (struct command_line *commands)
1078{
1079 struct command_line *c;
cc59ec59 1080
a7bdde9e
VP
1081 for (c = commands; c; c = c->next)
1082 {
1083 int i;
1084
1085 if (c->control_type == while_stepping_control)
3e43a32a
MS
1086 error (_("The 'while-stepping' command can "
1087 "only be used for tracepoints"));
a7bdde9e
VP
1088
1089 for (i = 0; i < c->body_count; ++i)
1090 check_no_tracepoint_commands ((c->body_list)[i]);
1091
1092 /* Not that command parsing removes leading whitespace and comment
4a64f543 1093 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1094 command directly. */
1095 if (strstr (c->line, "collect ") == c->line)
1096 error (_("The 'collect' command can only be used for tracepoints"));
1097
51661e93
VP
1098 if (strstr (c->line, "teval ") == c->line)
1099 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1100 }
1101}
1102
d77f58be
SS
1103/* Encapsulate tests for different types of tracepoints. */
1104
d9b3f62e
PA
1105static int
1106is_tracepoint_type (enum bptype type)
1107{
1108 return (type == bp_tracepoint
1109 || type == bp_fast_tracepoint
1110 || type == bp_static_tracepoint);
1111}
1112
a7bdde9e 1113int
d77f58be 1114is_tracepoint (const struct breakpoint *b)
a7bdde9e 1115{
d9b3f62e 1116 return is_tracepoint_type (b->type);
a7bdde9e 1117}
d9b3f62e 1118
e5dd4106 1119/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1120 breakpoint. This function will throw an exception if a problem is
1121 found. */
48cb2d85 1122
95a42b64
TT
1123static void
1124validate_commands_for_breakpoint (struct breakpoint *b,
1125 struct command_line *commands)
48cb2d85 1126{
d77f58be 1127 if (is_tracepoint (b))
a7bdde9e 1128 {
c9a6ce02 1129 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1130 struct command_line *c;
1131 struct command_line *while_stepping = 0;
c9a6ce02
PA
1132
1133 /* Reset the while-stepping step count. The previous commands
1134 might have included a while-stepping action, while the new
1135 ones might not. */
1136 t->step_count = 0;
1137
1138 /* We need to verify that each top-level element of commands is
1139 valid for tracepoints, that there's at most one
1140 while-stepping element, and that the while-stepping's body
1141 has valid tracing commands excluding nested while-stepping.
1142 We also need to validate the tracepoint action line in the
1143 context of the tracepoint --- validate_actionline actually
1144 has side effects, like setting the tracepoint's
1145 while-stepping STEP_COUNT, in addition to checking if the
1146 collect/teval actions parse and make sense in the
1147 tracepoint's context. */
a7bdde9e
VP
1148 for (c = commands; c; c = c->next)
1149 {
a7bdde9e
VP
1150 if (c->control_type == while_stepping_control)
1151 {
1152 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1153 error (_("The 'while-stepping' command "
1154 "cannot be used for fast tracepoint"));
0fb4aa4b 1155 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1156 error (_("The 'while-stepping' command "
1157 "cannot be used for static tracepoint"));
a7bdde9e
VP
1158
1159 if (while_stepping)
3e43a32a
MS
1160 error (_("The 'while-stepping' command "
1161 "can be used only once"));
a7bdde9e
VP
1162 else
1163 while_stepping = c;
1164 }
c9a6ce02
PA
1165
1166 validate_actionline (c->line, b);
a7bdde9e
VP
1167 }
1168 if (while_stepping)
1169 {
1170 struct command_line *c2;
1171
1172 gdb_assert (while_stepping->body_count == 1);
1173 c2 = while_stepping->body_list[0];
1174 for (; c2; c2 = c2->next)
1175 {
a7bdde9e
VP
1176 if (c2->control_type == while_stepping_control)
1177 error (_("The 'while-stepping' command cannot be nested"));
1178 }
1179 }
1180 }
1181 else
1182 {
1183 check_no_tracepoint_commands (commands);
1184 }
95a42b64
TT
1185}
1186
0fb4aa4b
PA
1187/* Return a vector of all the static tracepoints set at ADDR. The
1188 caller is responsible for releasing the vector. */
1189
1190VEC(breakpoint_p) *
1191static_tracepoints_here (CORE_ADDR addr)
1192{
1193 struct breakpoint *b;
1194 VEC(breakpoint_p) *found = 0;
1195 struct bp_location *loc;
1196
1197 ALL_BREAKPOINTS (b)
1198 if (b->type == bp_static_tracepoint)
1199 {
1200 for (loc = b->loc; loc; loc = loc->next)
1201 if (loc->address == addr)
1202 VEC_safe_push(breakpoint_p, found, b);
1203 }
1204
1205 return found;
1206}
1207
95a42b64 1208/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1209 validate that only allowed commands are included. */
95a42b64
TT
1210
1211void
4a64f543
MS
1212breakpoint_set_commands (struct breakpoint *b,
1213 struct command_line *commands)
95a42b64
TT
1214{
1215 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1216
9add0f1b
TT
1217 decref_counted_command_line (&b->commands);
1218 b->commands = alloc_counted_command_line (commands);
8d3788bd 1219 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1220}
1221
45a43567
TT
1222/* Set the internal `silent' flag on the breakpoint. Note that this
1223 is not the same as the "silent" that may appear in the breakpoint's
1224 commands. */
1225
1226void
1227breakpoint_set_silent (struct breakpoint *b, int silent)
1228{
1229 int old_silent = b->silent;
1230
1231 b->silent = silent;
1232 if (old_silent != silent)
8d3788bd 1233 observer_notify_breakpoint_modified (b);
45a43567
TT
1234}
1235
1236/* Set the thread for this breakpoint. If THREAD is -1, make the
1237 breakpoint work for any thread. */
1238
1239void
1240breakpoint_set_thread (struct breakpoint *b, int thread)
1241{
1242 int old_thread = b->thread;
1243
1244 b->thread = thread;
1245 if (old_thread != thread)
8d3788bd 1246 observer_notify_breakpoint_modified (b);
45a43567
TT
1247}
1248
1249/* Set the task for this breakpoint. If TASK is 0, make the
1250 breakpoint work for any task. */
1251
1252void
1253breakpoint_set_task (struct breakpoint *b, int task)
1254{
1255 int old_task = b->task;
1256
1257 b->task = task;
1258 if (old_task != task)
8d3788bd 1259 observer_notify_breakpoint_modified (b);
45a43567
TT
1260}
1261
95a42b64
TT
1262void
1263check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1264{
1265 struct breakpoint *b = closure;
cc59ec59 1266
6f937416 1267 validate_actionline (line, b);
a7bdde9e
VP
1268}
1269
95a42b64
TT
1270/* A structure used to pass information through
1271 map_breakpoint_numbers. */
1272
1273struct commands_info
1274{
1275 /* True if the command was typed at a tty. */
1276 int from_tty;
86b17b60
PA
1277
1278 /* The breakpoint range spec. */
1279 char *arg;
1280
95a42b64
TT
1281 /* Non-NULL if the body of the commands are being read from this
1282 already-parsed command. */
1283 struct command_line *control;
86b17b60 1284
95a42b64
TT
1285 /* The command lines read from the user, or NULL if they have not
1286 yet been read. */
1287 struct counted_command_line *cmd;
1288};
1289
1290/* A callback for map_breakpoint_numbers that sets the commands for
1291 commands_command. */
1292
c906108c 1293static void
95a42b64 1294do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1295{
95a42b64 1296 struct commands_info *info = data;
c906108c 1297
95a42b64
TT
1298 if (info->cmd == NULL)
1299 {
1300 struct command_line *l;
5c44784c 1301
95a42b64
TT
1302 if (info->control != NULL)
1303 l = copy_command_lines (info->control->body_list[0]);
1304 else
86b17b60
PA
1305 {
1306 struct cleanup *old_chain;
1307 char *str;
c5aa993b 1308
3e43a32a
MS
1309 str = xstrprintf (_("Type commands for breakpoint(s) "
1310 "%s, one per line."),
86b17b60
PA
1311 info->arg);
1312
1313 old_chain = make_cleanup (xfree, str);
1314
1315 l = read_command_lines (str,
1316 info->from_tty, 1,
d77f58be 1317 (is_tracepoint (b)
86b17b60
PA
1318 ? check_tracepoint_command : 0),
1319 b);
1320
1321 do_cleanups (old_chain);
1322 }
a7bdde9e 1323
95a42b64
TT
1324 info->cmd = alloc_counted_command_line (l);
1325 }
1326
1327 /* If a breakpoint was on the list more than once, we don't need to
1328 do anything. */
1329 if (b->commands != info->cmd)
1330 {
1331 validate_commands_for_breakpoint (b, info->cmd->commands);
1332 incref_counted_command_line (info->cmd);
1333 decref_counted_command_line (&b->commands);
1334 b->commands = info->cmd;
8d3788bd 1335 observer_notify_breakpoint_modified (b);
c5aa993b 1336 }
95a42b64
TT
1337}
1338
1339static void
4a64f543
MS
1340commands_command_1 (char *arg, int from_tty,
1341 struct command_line *control)
95a42b64
TT
1342{
1343 struct cleanup *cleanups;
1344 struct commands_info info;
1345
1346 info.from_tty = from_tty;
1347 info.control = control;
1348 info.cmd = NULL;
1349 /* If we read command lines from the user, then `info' will hold an
1350 extra reference to the commands that we must clean up. */
1351 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1352
1353 if (arg == NULL || !*arg)
1354 {
86b17b60 1355 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1356 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1357 breakpoint_count);
95a42b64
TT
1358 else if (breakpoint_count > 0)
1359 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1360 else
1361 {
1362 /* So that we don't try to free the incoming non-NULL
1363 argument in the cleanup below. Mapping breakpoint
1364 numbers will fail in this case. */
1365 arg = NULL;
1366 }
95a42b64 1367 }
9766ced4
SS
1368 else
1369 /* The command loop has some static state, so we need to preserve
1370 our argument. */
1371 arg = xstrdup (arg);
86b17b60
PA
1372
1373 if (arg != NULL)
1374 make_cleanup (xfree, arg);
1375
1376 info.arg = arg;
95a42b64
TT
1377
1378 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1379
1380 if (info.cmd == NULL)
1381 error (_("No breakpoints specified."));
1382
1383 do_cleanups (cleanups);
1384}
1385
1386static void
1387commands_command (char *arg, int from_tty)
1388{
1389 commands_command_1 (arg, from_tty, NULL);
c906108c 1390}
40c03ae8
EZ
1391
1392/* Like commands_command, but instead of reading the commands from
1393 input stream, takes them from an already parsed command structure.
1394
1395 This is used by cli-script.c to DTRT with breakpoint commands
1396 that are part of if and while bodies. */
1397enum command_control_type
1398commands_from_control_command (char *arg, struct command_line *cmd)
1399{
95a42b64
TT
1400 commands_command_1 (arg, 0, cmd);
1401 return simple_control;
40c03ae8 1402}
876fa593
JK
1403
1404/* Return non-zero if BL->TARGET_INFO contains valid information. */
1405
1406static int
1407bp_location_has_shadow (struct bp_location *bl)
1408{
1409 if (bl->loc_type != bp_loc_software_breakpoint)
1410 return 0;
1411 if (!bl->inserted)
1412 return 0;
1413 if (bl->target_info.shadow_len == 0)
e5dd4106 1414 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1415 return 0;
1416 return 1;
1417}
1418
9d497a19
PA
1419/* Update BUF, which is LEN bytes read from the target address
1420 MEMADDR, by replacing a memory breakpoint with its shadowed
1421 contents.
1422
1423 If READBUF is not NULL, this buffer must not overlap with the of
1424 the breakpoint location's shadow_contents buffer. Otherwise, a
1425 failed assertion internal error will be raised. */
1426
1427static void
1428one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1429 const gdb_byte *writebuf_org,
1430 ULONGEST memaddr, LONGEST len,
1431 struct bp_target_info *target_info,
1432 struct gdbarch *gdbarch)
1433{
1434 /* Now do full processing of the found relevant range of elements. */
1435 CORE_ADDR bp_addr = 0;
1436 int bp_size = 0;
1437 int bptoffset = 0;
1438
1439 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1440 current_program_space->aspace, 0))
1441 {
1442 /* The breakpoint is inserted in a different address space. */
1443 return;
1444 }
1445
1446 /* Addresses and length of the part of the breakpoint that
1447 we need to copy. */
1448 bp_addr = target_info->placed_address;
1449 bp_size = target_info->shadow_len;
1450
1451 if (bp_addr + bp_size <= memaddr)
1452 {
1453 /* The breakpoint is entirely before the chunk of memory we are
1454 reading. */
1455 return;
1456 }
1457
1458 if (bp_addr >= memaddr + len)
1459 {
1460 /* The breakpoint is entirely after the chunk of memory we are
1461 reading. */
1462 return;
1463 }
1464
1465 /* Offset within shadow_contents. */
1466 if (bp_addr < memaddr)
1467 {
1468 /* Only copy the second part of the breakpoint. */
1469 bp_size -= memaddr - bp_addr;
1470 bptoffset = memaddr - bp_addr;
1471 bp_addr = memaddr;
1472 }
1473
1474 if (bp_addr + bp_size > memaddr + len)
1475 {
1476 /* Only copy the first part of the breakpoint. */
1477 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1478 }
1479
1480 if (readbuf != NULL)
1481 {
1482 /* Verify that the readbuf buffer does not overlap with the
1483 shadow_contents buffer. */
1484 gdb_assert (target_info->shadow_contents >= readbuf + len
1485 || readbuf >= (target_info->shadow_contents
1486 + target_info->shadow_len));
1487
1488 /* Update the read buffer with this inserted breakpoint's
1489 shadow. */
1490 memcpy (readbuf + bp_addr - memaddr,
1491 target_info->shadow_contents + bptoffset, bp_size);
1492 }
1493 else
1494 {
1495 const unsigned char *bp;
1496 CORE_ADDR placed_address = target_info->placed_address;
1497 int placed_size = target_info->placed_size;
1498
1499 /* Update the shadow with what we want to write to memory. */
1500 memcpy (target_info->shadow_contents + bptoffset,
1501 writebuf_org + bp_addr - memaddr, bp_size);
1502
1503 /* Determine appropriate breakpoint contents and size for this
1504 address. */
1505 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1506
1507 /* Update the final write buffer with this inserted
1508 breakpoint's INSN. */
1509 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1510 }
1511}
1512
8defab1a 1513/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1514 by replacing any memory breakpoints with their shadowed contents.
1515
35c63cd8
JB
1516 If READBUF is not NULL, this buffer must not overlap with any of
1517 the breakpoint location's shadow_contents buffers. Otherwise,
1518 a failed assertion internal error will be raised.
1519
876fa593 1520 The range of shadowed area by each bp_location is:
35df4500
TJB
1521 bl->address - bp_location_placed_address_before_address_max
1522 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1523 The range we were requested to resolve shadows for is:
1524 memaddr ... memaddr + len
1525 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1526 memaddr + len <= (bl->address
1527 - bp_location_placed_address_before_address_max)
876fa593 1528 and:
35df4500 1529 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1530
8defab1a 1531void
f0ba3972
PA
1532breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1533 const gdb_byte *writebuf_org,
1534 ULONGEST memaddr, LONGEST len)
c906108c 1535{
4a64f543
MS
1536 /* Left boundary, right boundary and median element of our binary
1537 search. */
876fa593 1538 unsigned bc_l, bc_r, bc;
9d497a19 1539 size_t i;
876fa593 1540
4a64f543
MS
1541 /* Find BC_L which is a leftmost element which may affect BUF
1542 content. It is safe to report lower value but a failure to
1543 report higher one. */
876fa593
JK
1544
1545 bc_l = 0;
1546 bc_r = bp_location_count;
1547 while (bc_l + 1 < bc_r)
1548 {
35df4500 1549 struct bp_location *bl;
876fa593
JK
1550
1551 bc = (bc_l + bc_r) / 2;
35df4500 1552 bl = bp_location[bc];
876fa593 1553
4a64f543
MS
1554 /* Check first BL->ADDRESS will not overflow due to the added
1555 constant. Then advance the left boundary only if we are sure
1556 the BC element can in no way affect the BUF content (MEMADDR
1557 to MEMADDR + LEN range).
876fa593 1558
4a64f543
MS
1559 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1560 offset so that we cannot miss a breakpoint with its shadow
1561 range tail still reaching MEMADDR. */
c5aa993b 1562
35df4500
TJB
1563 if ((bl->address + bp_location_shadow_len_after_address_max
1564 >= bl->address)
1565 && (bl->address + bp_location_shadow_len_after_address_max
1566 <= memaddr))
876fa593
JK
1567 bc_l = bc;
1568 else
1569 bc_r = bc;
1570 }
1571
128070bb
PA
1572 /* Due to the binary search above, we need to make sure we pick the
1573 first location that's at BC_L's address. E.g., if there are
1574 multiple locations at the same address, BC_L may end up pointing
1575 at a duplicate location, and miss the "master"/"inserted"
1576 location. Say, given locations L1, L2 and L3 at addresses A and
1577 B:
1578
1579 L1@A, L2@A, L3@B, ...
1580
1581 BC_L could end up pointing at location L2, while the "master"
1582 location could be L1. Since the `loc->inserted' flag is only set
1583 on "master" locations, we'd forget to restore the shadow of L1
1584 and L2. */
1585 while (bc_l > 0
1586 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1587 bc_l--;
1588
876fa593
JK
1589 /* Now do full processing of the found relevant range of elements. */
1590
1591 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1592 {
35df4500 1593 struct bp_location *bl = bp_location[bc];
876fa593
JK
1594 CORE_ADDR bp_addr = 0;
1595 int bp_size = 0;
1596 int bptoffset = 0;
1597
35df4500
TJB
1598 /* bp_location array has BL->OWNER always non-NULL. */
1599 if (bl->owner->type == bp_none)
8a3fe4f8 1600 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1601 bl->owner->number);
ffce0d52 1602
e5dd4106 1603 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1604 content. */
1605
35df4500
TJB
1606 if (bl->address >= bp_location_placed_address_before_address_max
1607 && memaddr + len <= (bl->address
1608 - bp_location_placed_address_before_address_max))
876fa593
JK
1609 break;
1610
35df4500 1611 if (!bp_location_has_shadow (bl))
c5aa993b 1612 continue;
6c95b8df 1613
9d497a19
PA
1614 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1615 memaddr, len, &bl->target_info, bl->gdbarch);
1616 }
c5aa993b 1617
9d497a19
PA
1618 /* Now process single-step breakpoints. These are not found in the
1619 bp_location array. */
1620 for (i = 0; i < 2; i++)
1621 {
1622 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1623
9d497a19
PA
1624 if (bp_tgt != NULL)
1625 {
1626 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1627
9d497a19
PA
1628 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1629 memaddr, len, bp_tgt, gdbarch);
1630 }
1631 }
c906108c 1632}
9d497a19 1633
c906108c 1634\f
c5aa993b 1635
b775012e
LM
1636/* Return true if BPT is either a software breakpoint or a hardware
1637 breakpoint. */
1638
1639int
1640is_breakpoint (const struct breakpoint *bpt)
1641{
1642 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1643 || bpt->type == bp_hardware_breakpoint
1644 || bpt->type == bp_dprintf);
b775012e
LM
1645}
1646
60e1c644
PA
1647/* Return true if BPT is of any hardware watchpoint kind. */
1648
a5606eee 1649static int
d77f58be 1650is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1651{
1652 return (bpt->type == bp_hardware_watchpoint
1653 || bpt->type == bp_read_watchpoint
1654 || bpt->type == bp_access_watchpoint);
1655}
7270d8f2 1656
60e1c644
PA
1657/* Return true if BPT is of any watchpoint kind, hardware or
1658 software. */
1659
3a5c3e22 1660int
d77f58be 1661is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1662{
1663 return (is_hardware_watchpoint (bpt)
1664 || bpt->type == bp_watchpoint);
1665}
1666
3a5c3e22
PA
1667/* Returns true if the current thread and its running state are safe
1668 to evaluate or update watchpoint B. Watchpoints on local
1669 expressions need to be evaluated in the context of the thread that
1670 was current when the watchpoint was created, and, that thread needs
1671 to be stopped to be able to select the correct frame context.
1672 Watchpoints on global expressions can be evaluated on any thread,
1673 and in any state. It is presently left to the target allowing
1674 memory accesses when threads are running. */
f6bc2008
PA
1675
1676static int
3a5c3e22 1677watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1678{
d0d8b0c6
JK
1679 return (b->base.pspace == current_program_space
1680 && (ptid_equal (b->watchpoint_thread, null_ptid)
1681 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1682 && !is_executing (inferior_ptid))));
f6bc2008
PA
1683}
1684
d0fb5eae
JK
1685/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1686 associated bp_watchpoint_scope breakpoint. */
1687
1688static void
3a5c3e22 1689watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1690{
3a5c3e22 1691 struct breakpoint *b = &w->base;
d0fb5eae
JK
1692
1693 if (b->related_breakpoint != b)
1694 {
1695 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1696 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1697 b->related_breakpoint->disposition = disp_del_at_next_stop;
1698 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1699 b->related_breakpoint = b;
1700 }
1701 b->disposition = disp_del_at_next_stop;
1702}
1703
567e1b4e
JB
1704/* Assuming that B is a watchpoint:
1705 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1706 - Evaluate expression and store the result in B->val
567e1b4e
JB
1707 - Evaluate the condition if there is one, and store the result
1708 in b->loc->cond.
a5606eee
VP
1709 - Update the list of values that must be watched in B->loc.
1710
4a64f543
MS
1711 If the watchpoint disposition is disp_del_at_next_stop, then do
1712 nothing. If this is local watchpoint that is out of scope, delete
1713 it.
1714
1715 Even with `set breakpoint always-inserted on' the watchpoints are
1716 removed + inserted on each stop here. Normal breakpoints must
1717 never be removed because they might be missed by a running thread
1718 when debugging in non-stop mode. On the other hand, hardware
1719 watchpoints (is_hardware_watchpoint; processed here) are specific
1720 to each LWP since they are stored in each LWP's hardware debug
1721 registers. Therefore, such LWP must be stopped first in order to
1722 be able to modify its hardware watchpoints.
1723
1724 Hardware watchpoints must be reset exactly once after being
1725 presented to the user. It cannot be done sooner, because it would
1726 reset the data used to present the watchpoint hit to the user. And
1727 it must not be done later because it could display the same single
1728 watchpoint hit during multiple GDB stops. Note that the latter is
1729 relevant only to the hardware watchpoint types bp_read_watchpoint
1730 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1731 not user-visible - its hit is suppressed if the memory content has
1732 not changed.
1733
1734 The following constraints influence the location where we can reset
1735 hardware watchpoints:
1736
1737 * target_stopped_by_watchpoint and target_stopped_data_address are
1738 called several times when GDB stops.
1739
1740 [linux]
1741 * Multiple hardware watchpoints can be hit at the same time,
1742 causing GDB to stop. GDB only presents one hardware watchpoint
1743 hit at a time as the reason for stopping, and all the other hits
1744 are presented later, one after the other, each time the user
1745 requests the execution to be resumed. Execution is not resumed
1746 for the threads still having pending hit event stored in
1747 LWP_INFO->STATUS. While the watchpoint is already removed from
1748 the inferior on the first stop the thread hit event is kept being
1749 reported from its cached value by linux_nat_stopped_data_address
1750 until the real thread resume happens after the watchpoint gets
1751 presented and thus its LWP_INFO->STATUS gets reset.
1752
1753 Therefore the hardware watchpoint hit can get safely reset on the
1754 watchpoint removal from inferior. */
a79d3c27 1755
b40ce68a 1756static void
3a5c3e22 1757update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1758{
a5606eee 1759 int within_current_scope;
a5606eee 1760 struct frame_id saved_frame_id;
66076460 1761 int frame_saved;
a5606eee 1762
f6bc2008
PA
1763 /* If this is a local watchpoint, we only want to check if the
1764 watchpoint frame is in scope if the current thread is the thread
1765 that was used to create the watchpoint. */
1766 if (!watchpoint_in_thread_scope (b))
1767 return;
1768
3a5c3e22 1769 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1770 return;
1771
66076460 1772 frame_saved = 0;
a5606eee
VP
1773
1774 /* Determine if the watchpoint is within scope. */
1775 if (b->exp_valid_block == NULL)
1776 within_current_scope = 1;
1777 else
1778 {
b5db5dfc
UW
1779 struct frame_info *fi = get_current_frame ();
1780 struct gdbarch *frame_arch = get_frame_arch (fi);
1781 CORE_ADDR frame_pc = get_frame_pc (fi);
1782
1783 /* If we're in a function epilogue, unwinding may not work
1784 properly, so do not attempt to recreate locations at this
1785 point. See similar comments in watchpoint_check. */
1786 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1787 return;
66076460
DJ
1788
1789 /* Save the current frame's ID so we can restore it after
1790 evaluating the watchpoint expression on its own frame. */
1791 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1792 took a frame parameter, so that we didn't have to change the
1793 selected frame. */
1794 frame_saved = 1;
1795 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1796
a5606eee
VP
1797 fi = frame_find_by_id (b->watchpoint_frame);
1798 within_current_scope = (fi != NULL);
1799 if (within_current_scope)
1800 select_frame (fi);
1801 }
1802
b5db5dfc
UW
1803 /* We don't free locations. They are stored in the bp_location array
1804 and update_global_location_list will eventually delete them and
1805 remove breakpoints if needed. */
3a5c3e22 1806 b->base.loc = NULL;
b5db5dfc 1807
a5606eee
VP
1808 if (within_current_scope && reparse)
1809 {
bbc13ae3 1810 const char *s;
d63d0675 1811
a5606eee
VP
1812 if (b->exp)
1813 {
1814 xfree (b->exp);
1815 b->exp = NULL;
1816 }
d63d0675 1817 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1818 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1819 /* If the meaning of expression itself changed, the old value is
1820 no longer relevant. We don't want to report a watchpoint hit
1821 to the user when the old value and the new value may actually
1822 be completely different objects. */
1823 value_free (b->val);
fa4727a6
DJ
1824 b->val = NULL;
1825 b->val_valid = 0;
60e1c644
PA
1826
1827 /* Note that unlike with breakpoints, the watchpoint's condition
1828 expression is stored in the breakpoint object, not in the
1829 locations (re)created below. */
3a5c3e22 1830 if (b->base.cond_string != NULL)
60e1c644
PA
1831 {
1832 if (b->cond_exp != NULL)
1833 {
1834 xfree (b->cond_exp);
1835 b->cond_exp = NULL;
1836 }
1837
3a5c3e22 1838 s = b->base.cond_string;
1bb9788d 1839 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1840 }
a5606eee 1841 }
a5606eee
VP
1842
1843 /* If we failed to parse the expression, for example because
1844 it refers to a global variable in a not-yet-loaded shared library,
1845 don't try to insert watchpoint. We don't automatically delete
1846 such watchpoint, though, since failure to parse expression
1847 is different from out-of-scope watchpoint. */
e8369a73 1848 if (!target_has_execution)
2d134ed3
PA
1849 {
1850 /* Without execution, memory can't change. No use to try and
1851 set watchpoint locations. The watchpoint will be reset when
1852 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1853 if (!can_use_hw_watchpoints)
1854 {
1855 if (b->base.ops->works_in_software_mode (&b->base))
1856 b->base.type = bp_watchpoint;
1857 else
638aa5a1
AB
1858 error (_("Can't set read/access watchpoint when "
1859 "hardware watchpoints are disabled."));
e8369a73 1860 }
2d134ed3
PA
1861 }
1862 else if (within_current_scope && b->exp)
a5606eee 1863 {
0cf6dd15 1864 int pc = 0;
fa4727a6 1865 struct value *val_chain, *v, *result, *next;
2d134ed3 1866 struct program_space *frame_pspace;
a5606eee 1867
3a1115a0 1868 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1869
a5606eee
VP
1870 /* Avoid setting b->val if it's already set. The meaning of
1871 b->val is 'the last value' user saw, and we should update
1872 it only if we reported that last value to user. As it
9c06b0b4
TJB
1873 happens, the code that reports it updates b->val directly.
1874 We don't keep track of the memory value for masked
1875 watchpoints. */
3a5c3e22 1876 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1877 {
1878 b->val = v;
1879 b->val_valid = 1;
1880 }
a5606eee 1881
2d134ed3
PA
1882 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1883
a5606eee 1884 /* Look at each value on the value chain. */
9fa40276 1885 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1886 {
1887 /* If it's a memory location, and GDB actually needed
1888 its contents to evaluate the expression, then we
fa4727a6
DJ
1889 must watch it. If the first value returned is
1890 still lazy, that means an error occurred reading it;
1891 watch it anyway in case it becomes readable. */
a5606eee 1892 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1893 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1894 {
1895 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1896
a5606eee
VP
1897 /* We only watch structs and arrays if user asked
1898 for it explicitly, never if they just happen to
1899 appear in the middle of some value chain. */
fa4727a6 1900 if (v == result
a5606eee
VP
1901 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1902 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1903 {
1904 CORE_ADDR addr;
744a8059 1905 int type;
a5606eee
VP
1906 struct bp_location *loc, **tmp;
1907
42ae5230 1908 addr = value_address (v);
a5606eee 1909 type = hw_write;
3a5c3e22 1910 if (b->base.type == bp_read_watchpoint)
a5606eee 1911 type = hw_read;
3a5c3e22 1912 else if (b->base.type == bp_access_watchpoint)
a5606eee 1913 type = hw_access;
3a5c3e22
PA
1914
1915 loc = allocate_bp_location (&b->base);
1916 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1917 ;
1918 *tmp = loc;
a6d9a66e 1919 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1920
1921 loc->pspace = frame_pspace;
a5606eee 1922 loc->address = addr;
744a8059 1923 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1924 loc->watchpoint_type = type;
1925 }
1926 }
9fa40276
TJB
1927 }
1928
1929 /* Change the type of breakpoint between hardware assisted or
1930 an ordinary watchpoint depending on the hardware support
1931 and free hardware slots. REPARSE is set when the inferior
1932 is started. */
a9634178 1933 if (reparse)
9fa40276 1934 {
e09342b5 1935 int reg_cnt;
9fa40276
TJB
1936 enum bp_loc_type loc_type;
1937 struct bp_location *bl;
a5606eee 1938
a9634178 1939 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1940
1941 if (reg_cnt)
9fa40276
TJB
1942 {
1943 int i, target_resources_ok, other_type_used;
a1398e0c 1944 enum bptype type;
9fa40276 1945
a9634178
TJB
1946 /* Use an exact watchpoint when there's only one memory region to be
1947 watched, and only one debug register is needed to watch it. */
1948 b->exact = target_exact_watchpoints && reg_cnt == 1;
1949
9fa40276 1950 /* We need to determine how many resources are already
e09342b5
TJB
1951 used for all other hardware watchpoints plus this one
1952 to see if we still have enough resources to also fit
a1398e0c
PA
1953 this watchpoint in as well. */
1954
1955 /* If this is a software watchpoint, we try to turn it
1956 to a hardware one -- count resources as if B was of
1957 hardware watchpoint type. */
1958 type = b->base.type;
1959 if (type == bp_watchpoint)
1960 type = bp_hardware_watchpoint;
1961
1962 /* This watchpoint may or may not have been placed on
1963 the list yet at this point (it won't be in the list
1964 if we're trying to create it for the first time,
1965 through watch_command), so always account for it
1966 manually. */
1967
1968 /* Count resources used by all watchpoints except B. */
1969 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1970
1971 /* Add in the resources needed for B. */
1972 i += hw_watchpoint_use_count (&b->base);
1973
1974 target_resources_ok
1975 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1976 if (target_resources_ok <= 0)
a9634178 1977 {
3a5c3e22 1978 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1979
1980 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1981 error (_("Target does not support this type of "
1982 "hardware watchpoint."));
9c06b0b4
TJB
1983 else if (target_resources_ok < 0 && !sw_mode)
1984 error (_("There are not enough available hardware "
1985 "resources for this watchpoint."));
a1398e0c
PA
1986
1987 /* Downgrade to software watchpoint. */
1988 b->base.type = bp_watchpoint;
1989 }
1990 else
1991 {
1992 /* If this was a software watchpoint, we've just
1993 found we have enough resources to turn it to a
1994 hardware watchpoint. Otherwise, this is a
1995 nop. */
1996 b->base.type = type;
a9634178 1997 }
9fa40276 1998 }
3a5c3e22 1999 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2000 {
2001 if (!can_use_hw_watchpoints)
2002 error (_("Can't set read/access watchpoint when "
2003 "hardware watchpoints are disabled."));
2004 else
2005 error (_("Expression cannot be implemented with "
2006 "read/access watchpoint."));
2007 }
9fa40276 2008 else
3a5c3e22 2009 b->base.type = bp_watchpoint;
9fa40276 2010
3a5c3e22 2011 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2012 : bp_loc_hardware_watchpoint);
3a5c3e22 2013 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2014 bl->loc_type = loc_type;
2015 }
2016
2017 for (v = val_chain; v; v = next)
2018 {
a5606eee
VP
2019 next = value_next (v);
2020 if (v != b->val)
2021 value_free (v);
2022 }
2023
c7437ca6
PA
2024 /* If a software watchpoint is not watching any memory, then the
2025 above left it without any location set up. But,
2026 bpstat_stop_status requires a location to be able to report
2027 stops, so make sure there's at least a dummy one. */
3a5c3e22 2028 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2029 {
3a5c3e22
PA
2030 struct breakpoint *base = &b->base;
2031 base->loc = allocate_bp_location (base);
2032 base->loc->pspace = frame_pspace;
2033 base->loc->address = -1;
2034 base->loc->length = -1;
2035 base->loc->watchpoint_type = -1;
c7437ca6 2036 }
a5606eee
VP
2037 }
2038 else if (!within_current_scope)
7270d8f2 2039 {
ac74f770
MS
2040 printf_filtered (_("\
2041Watchpoint %d deleted because the program has left the block\n\
2042in which its expression is valid.\n"),
3a5c3e22 2043 b->base.number);
d0fb5eae 2044 watchpoint_del_at_next_stop (b);
7270d8f2 2045 }
a5606eee
VP
2046
2047 /* Restore the selected frame. */
66076460
DJ
2048 if (frame_saved)
2049 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2050}
2051
a5606eee 2052
74960c60 2053/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2054 inserted in the inferior. We don't differentiate the type of BL's owner
2055 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2056 breakpoint_ops is not defined, because in insert_bp_location,
2057 tracepoint's insert_location will not be called. */
74960c60 2058static int
35df4500 2059should_be_inserted (struct bp_location *bl)
74960c60 2060{
35df4500 2061 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2062 return 0;
2063
35df4500 2064 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2065 return 0;
2066
35df4500 2067 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2068 return 0;
2069
f8eba3c6
TT
2070 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2071 return 0;
2072
56710373
PA
2073 /* This is set for example, when we're attached to the parent of a
2074 vfork, and have detached from the child. The child is running
2075 free, and we expect it to do an exec or exit, at which point the
2076 OS makes the parent schedulable again (and the target reports
2077 that the vfork is done). Until the child is done with the shared
2078 memory region, do not insert breakpoints in the parent, otherwise
2079 the child could still trip on the parent's breakpoints. Since
2080 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2081 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2082 return 0;
2083
31e77af2
PA
2084 /* Don't insert a breakpoint if we're trying to step past its
2085 location. */
2086 if ((bl->loc_type == bp_loc_software_breakpoint
2087 || bl->loc_type == bp_loc_hardware_breakpoint)
2088 && stepping_past_instruction_at (bl->pspace->aspace,
2089 bl->address))
2090 return 0;
2091
74960c60
VP
2092 return 1;
2093}
2094
934709f0
PW
2095/* Same as should_be_inserted but does the check assuming
2096 that the location is not duplicated. */
2097
2098static int
2099unduplicated_should_be_inserted (struct bp_location *bl)
2100{
2101 int result;
2102 const int save_duplicate = bl->duplicate;
2103
2104 bl->duplicate = 0;
2105 result = should_be_inserted (bl);
2106 bl->duplicate = save_duplicate;
2107 return result;
2108}
2109
b775012e
LM
2110/* Parses a conditional described by an expression COND into an
2111 agent expression bytecode suitable for evaluation
2112 by the bytecode interpreter. Return NULL if there was
2113 any error during parsing. */
2114
2115static struct agent_expr *
2116parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2117{
2118 struct agent_expr *aexpr = NULL;
b775012e
LM
2119 volatile struct gdb_exception ex;
2120
2121 if (!cond)
2122 return NULL;
2123
2124 /* We don't want to stop processing, so catch any errors
2125 that may show up. */
2126 TRY_CATCH (ex, RETURN_MASK_ERROR)
2127 {
2128 aexpr = gen_eval_for_expr (scope, cond);
2129 }
2130
2131 if (ex.reason < 0)
2132 {
2133 /* If we got here, it means the condition could not be parsed to a valid
2134 bytecode expression and thus can't be evaluated on the target's side.
2135 It's no use iterating through the conditions. */
2136 return NULL;
2137 }
2138
2139 /* We have a valid agent expression. */
2140 return aexpr;
2141}
2142
2143/* Based on location BL, create a list of breakpoint conditions to be
2144 passed on to the target. If we have duplicated locations with different
2145 conditions, we will add such conditions to the list. The idea is that the
2146 target will evaluate the list of conditions and will only notify GDB when
2147 one of them is true. */
2148
2149static void
2150build_target_condition_list (struct bp_location *bl)
2151{
2152 struct bp_location **locp = NULL, **loc2p;
2153 int null_condition_or_parse_error = 0;
2154 int modified = bl->needs_update;
2155 struct bp_location *loc;
2156
8b4f3082
PA
2157 /* Release conditions left over from a previous insert. */
2158 VEC_free (agent_expr_p, bl->target_info.conditions);
2159
b775012e
LM
2160 /* This is only meaningful if the target is
2161 evaluating conditions and if the user has
2162 opted for condition evaluation on the target's
2163 side. */
2164 if (gdb_evaluates_breakpoint_condition_p ()
2165 || !target_supports_evaluation_of_breakpoint_conditions ())
2166 return;
2167
2168 /* Do a first pass to check for locations with no assigned
2169 conditions or conditions that fail to parse to a valid agent expression
2170 bytecode. If any of these happen, then it's no use to send conditions
2171 to the target since this location will always trigger and generate a
2172 response back to GDB. */
2173 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2174 {
2175 loc = (*loc2p);
2176 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2177 {
2178 if (modified)
2179 {
2180 struct agent_expr *aexpr;
2181
2182 /* Re-parse the conditions since something changed. In that
2183 case we already freed the condition bytecodes (see
2184 force_breakpoint_reinsertion). We just
2185 need to parse the condition to bytecodes again. */
2186 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2187 loc->cond_bytecode = aexpr;
2188
2189 /* Check if we managed to parse the conditional expression
2190 correctly. If not, we will not send this condition
2191 to the target. */
2192 if (aexpr)
2193 continue;
2194 }
2195
2196 /* If we have a NULL bytecode expression, it means something
2197 went wrong or we have a null condition expression. */
2198 if (!loc->cond_bytecode)
2199 {
2200 null_condition_or_parse_error = 1;
2201 break;
2202 }
2203 }
2204 }
2205
2206 /* If any of these happened, it means we will have to evaluate the conditions
2207 for the location's address on gdb's side. It is no use keeping bytecodes
2208 for all the other duplicate locations, thus we free all of them here.
2209
2210 This is so we have a finer control over which locations' conditions are
2211 being evaluated by GDB or the remote stub. */
2212 if (null_condition_or_parse_error)
2213 {
2214 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2215 {
2216 loc = (*loc2p);
2217 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2218 {
2219 /* Only go as far as the first NULL bytecode is
2220 located. */
2221 if (!loc->cond_bytecode)
2222 return;
2223
2224 free_agent_expr (loc->cond_bytecode);
2225 loc->cond_bytecode = NULL;
2226 }
2227 }
2228 }
2229
2230 /* No NULL conditions or failed bytecode generation. Build a condition list
2231 for this location's address. */
2232 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2233 {
2234 loc = (*loc2p);
2235 if (loc->cond
2236 && is_breakpoint (loc->owner)
2237 && loc->pspace->num == bl->pspace->num
2238 && loc->owner->enable_state == bp_enabled
2239 && loc->enabled)
2240 /* Add the condition to the vector. This will be used later to send the
2241 conditions to the target. */
2242 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2243 loc->cond_bytecode);
2244 }
2245
2246 return;
2247}
2248
d3ce09f5
SS
2249/* Parses a command described by string CMD into an agent expression
2250 bytecode suitable for evaluation by the bytecode interpreter.
2251 Return NULL if there was any error during parsing. */
2252
2253static struct agent_expr *
2254parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2255{
2256 struct cleanup *old_cleanups = 0;
2257 struct expression *expr, **argvec;
2258 struct agent_expr *aexpr = NULL;
d3ce09f5 2259 volatile struct gdb_exception ex;
bbc13ae3
KS
2260 const char *cmdrest;
2261 const char *format_start, *format_end;
d3ce09f5
SS
2262 struct format_piece *fpieces;
2263 int nargs;
2264 struct gdbarch *gdbarch = get_current_arch ();
2265
2266 if (!cmd)
2267 return NULL;
2268
2269 cmdrest = cmd;
2270
2271 if (*cmdrest == ',')
2272 ++cmdrest;
bbc13ae3 2273 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2274
2275 if (*cmdrest++ != '"')
2276 error (_("No format string following the location"));
2277
2278 format_start = cmdrest;
2279
2280 fpieces = parse_format_string (&cmdrest);
2281
2282 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2283
2284 format_end = cmdrest;
2285
2286 if (*cmdrest++ != '"')
2287 error (_("Bad format string, non-terminated '\"'."));
2288
bbc13ae3 2289 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2290
2291 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2292 error (_("Invalid argument syntax"));
2293
2294 if (*cmdrest == ',')
2295 cmdrest++;
bbc13ae3 2296 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2297
2298 /* For each argument, make an expression. */
2299
2300 argvec = (struct expression **) alloca (strlen (cmd)
2301 * sizeof (struct expression *));
2302
2303 nargs = 0;
2304 while (*cmdrest != '\0')
2305 {
bbc13ae3 2306 const char *cmd1;
d3ce09f5
SS
2307
2308 cmd1 = cmdrest;
2309 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2310 argvec[nargs++] = expr;
2311 cmdrest = cmd1;
2312 if (*cmdrest == ',')
2313 ++cmdrest;
2314 }
2315
2316 /* We don't want to stop processing, so catch any errors
2317 that may show up. */
2318 TRY_CATCH (ex, RETURN_MASK_ERROR)
2319 {
2320 aexpr = gen_printf (scope, gdbarch, 0, 0,
2321 format_start, format_end - format_start,
2322 fpieces, nargs, argvec);
2323 }
2324
752eb8b4
TT
2325 do_cleanups (old_cleanups);
2326
d3ce09f5
SS
2327 if (ex.reason < 0)
2328 {
2329 /* If we got here, it means the command could not be parsed to a valid
2330 bytecode expression and thus can't be evaluated on the target's side.
2331 It's no use iterating through the other commands. */
2332 return NULL;
2333 }
2334
d3ce09f5
SS
2335 /* We have a valid agent expression, return it. */
2336 return aexpr;
2337}
2338
2339/* Based on location BL, create a list of breakpoint commands to be
2340 passed on to the target. If we have duplicated locations with
2341 different commands, we will add any such to the list. */
2342
2343static void
2344build_target_command_list (struct bp_location *bl)
2345{
2346 struct bp_location **locp = NULL, **loc2p;
2347 int null_command_or_parse_error = 0;
2348 int modified = bl->needs_update;
2349 struct bp_location *loc;
2350
8b4f3082
PA
2351 /* Release commands left over from a previous insert. */
2352 VEC_free (agent_expr_p, bl->target_info.tcommands);
2353
d3ce09f5
SS
2354 /* For now, limit to agent-style dprintf breakpoints. */
2355 if (bl->owner->type != bp_dprintf
2356 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2357 return;
2358
2359 if (!target_can_run_breakpoint_commands ())
2360 return;
2361
2362 /* Do a first pass to check for locations with no assigned
2363 conditions or conditions that fail to parse to a valid agent expression
2364 bytecode. If any of these happen, then it's no use to send conditions
2365 to the target since this location will always trigger and generate a
2366 response back to GDB. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2371 {
2372 if (modified)
2373 {
2374 struct agent_expr *aexpr;
2375
2376 /* Re-parse the commands since something changed. In that
2377 case we already freed the command bytecodes (see
2378 force_breakpoint_reinsertion). We just
2379 need to parse the command to bytecodes again. */
2380 aexpr = parse_cmd_to_aexpr (bl->address,
2381 loc->owner->extra_string);
2382 loc->cmd_bytecode = aexpr;
2383
2384 if (!aexpr)
2385 continue;
2386 }
2387
2388 /* If we have a NULL bytecode expression, it means something
2389 went wrong or we have a null command expression. */
2390 if (!loc->cmd_bytecode)
2391 {
2392 null_command_or_parse_error = 1;
2393 break;
2394 }
2395 }
2396 }
2397
2398 /* If anything failed, then we're not doing target-side commands,
2399 and so clean up. */
2400 if (null_command_or_parse_error)
2401 {
2402 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2403 {
2404 loc = (*loc2p);
2405 if (is_breakpoint (loc->owner)
2406 && loc->pspace->num == bl->pspace->num)
2407 {
2408 /* Only go as far as the first NULL bytecode is
2409 located. */
40fb6c5e 2410 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2411 return;
2412
40fb6c5e
HZ
2413 free_agent_expr (loc->cmd_bytecode);
2414 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2415 }
2416 }
2417 }
2418
2419 /* No NULL commands or failed bytecode generation. Build a command list
2420 for this location's address. */
2421 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2422 {
2423 loc = (*loc2p);
2424 if (loc->owner->extra_string
2425 && is_breakpoint (loc->owner)
2426 && loc->pspace->num == bl->pspace->num
2427 && loc->owner->enable_state == bp_enabled
2428 && loc->enabled)
2429 /* Add the command to the vector. This will be used later
2430 to send the commands to the target. */
2431 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2432 loc->cmd_bytecode);
2433 }
2434
2435 bl->target_info.persist = 0;
2436 /* Maybe flag this location as persistent. */
2437 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2438 bl->target_info.persist = 1;
2439}
2440
35df4500
TJB
2441/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2442 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2443 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2444 Returns 0 for success, 1 if the bp_location type is not supported or
2445 -1 for failure.
879bfdc2 2446
4a64f543
MS
2447 NOTE drow/2003-09-09: This routine could be broken down to an
2448 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2449static int
35df4500 2450insert_bp_location (struct bp_location *bl,
26bb91f3 2451 struct ui_file *tmp_error_stream,
3fbb6ffa 2452 int *disabled_breaks,
dd61ec5c
MW
2453 int *hw_breakpoint_error,
2454 int *hw_bp_error_explained_already)
879bfdc2 2455{
0000e5cc
PA
2456 enum errors bp_err = GDB_NO_ERROR;
2457 const char *bp_err_message = NULL;
c90a6fb7 2458 volatile struct gdb_exception e;
879bfdc2 2459
b775012e 2460 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2461 return 0;
2462
35c63cd8
JB
2463 /* Note we don't initialize bl->target_info, as that wipes out
2464 the breakpoint location's shadow_contents if the breakpoint
2465 is still inserted at that location. This in turn breaks
2466 target_read_memory which depends on these buffers when
2467 a memory read is requested at the breakpoint location:
2468 Once the target_info has been wiped, we fail to see that
2469 we have a breakpoint inserted at that address and thus
2470 read the breakpoint instead of returning the data saved in
2471 the breakpoint location's shadow contents. */
35df4500
TJB
2472 bl->target_info.placed_address = bl->address;
2473 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2474 bl->target_info.length = bl->length;
8181d85f 2475
b775012e
LM
2476 /* When working with target-side conditions, we must pass all the conditions
2477 for the same breakpoint address down to the target since GDB will not
2478 insert those locations. With a list of breakpoint conditions, the target
2479 can decide when to stop and notify GDB. */
2480
2481 if (is_breakpoint (bl->owner))
2482 {
2483 build_target_condition_list (bl);
d3ce09f5
SS
2484 build_target_command_list (bl);
2485 /* Reset the modification marker. */
b775012e
LM
2486 bl->needs_update = 0;
2487 }
2488
35df4500
TJB
2489 if (bl->loc_type == bp_loc_software_breakpoint
2490 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2491 {
35df4500 2492 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2493 {
2494 /* If the explicitly specified breakpoint type
2495 is not hardware breakpoint, check the memory map to see
2496 if the breakpoint address is in read only memory or not.
4a64f543 2497
765dc015
VP
2498 Two important cases are:
2499 - location type is not hardware breakpoint, memory
2500 is readonly. We change the type of the location to
2501 hardware breakpoint.
4a64f543
MS
2502 - location type is hardware breakpoint, memory is
2503 read-write. This means we've previously made the
2504 location hardware one, but then the memory map changed,
2505 so we undo.
765dc015 2506
4a64f543
MS
2507 When breakpoints are removed, remove_breakpoints will use
2508 location types we've just set here, the only possible
2509 problem is that memory map has changed during running
2510 program, but it's not going to work anyway with current
2511 gdb. */
765dc015 2512 struct mem_region *mr
35df4500 2513 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2514
2515 if (mr)
2516 {
2517 if (automatic_hardware_breakpoints)
2518 {
765dc015
VP
2519 enum bp_loc_type new_type;
2520
2521 if (mr->attrib.mode != MEM_RW)
2522 new_type = bp_loc_hardware_breakpoint;
2523 else
2524 new_type = bp_loc_software_breakpoint;
2525
35df4500 2526 if (new_type != bl->loc_type)
765dc015
VP
2527 {
2528 static int said = 0;
cc59ec59 2529
35df4500 2530 bl->loc_type = new_type;
765dc015
VP
2531 if (!said)
2532 {
3e43a32a
MS
2533 fprintf_filtered (gdb_stdout,
2534 _("Note: automatically using "
2535 "hardware breakpoints for "
2536 "read-only addresses.\n"));
765dc015
VP
2537 said = 1;
2538 }
2539 }
2540 }
35df4500 2541 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2542 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2543 warning (_("cannot set software breakpoint "
2544 "at readonly address %s"),
35df4500 2545 paddress (bl->gdbarch, bl->address));
765dc015
VP
2546 }
2547 }
2548
879bfdc2
DJ
2549 /* First check to see if we have to handle an overlay. */
2550 if (overlay_debugging == ovly_off
35df4500
TJB
2551 || bl->section == NULL
2552 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2553 {
2554 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2555 TRY_CATCH (e, RETURN_MASK_ALL)
2556 {
0000e5cc
PA
2557 int val;
2558
dd61ec5c 2559 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2560 if (val)
2561 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2562 }
2563 if (e.reason < 0)
2564 {
0000e5cc
PA
2565 bp_err = e.error;
2566 bp_err_message = e.message;
dd61ec5c 2567 }
879bfdc2
DJ
2568 }
2569 else
2570 {
4a64f543 2571 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2572 Shall we set a breakpoint at the LMA? */
2573 if (!overlay_events_enabled)
2574 {
2575 /* Yes -- overlay event support is not active,
2576 so we must try to set a breakpoint at the LMA.
2577 This will not work for a hardware breakpoint. */
35df4500 2578 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2579 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2580 bl->owner->number);
879bfdc2
DJ
2581 else
2582 {
35df4500
TJB
2583 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2584 bl->section);
879bfdc2 2585 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2586 bl->overlay_target_info = bl->target_info;
2587 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2588
2589 /* No overlay handling: just set the breakpoint. */
2590 TRY_CATCH (e, RETURN_MASK_ALL)
2591 {
2592 int val;
2593
2594 val = target_insert_breakpoint (bl->gdbarch,
2595 &bl->overlay_target_info);
2596 if (val)
2597 bp_err = GENERIC_ERROR;
2598 }
2599 if (e.reason < 0)
2600 {
2601 bp_err = e.error;
2602 bp_err_message = e.message;
2603 }
2604
2605 if (bp_err != GDB_NO_ERROR)
99361f52 2606 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2607 "Overlay breakpoint %d "
2608 "failed: in ROM?\n",
35df4500 2609 bl->owner->number);
879bfdc2
DJ
2610 }
2611 }
2612 /* Shall we set a breakpoint at the VMA? */
35df4500 2613 if (section_is_mapped (bl->section))
879bfdc2
DJ
2614 {
2615 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2616 TRY_CATCH (e, RETURN_MASK_ALL)
2617 {
0000e5cc
PA
2618 int val;
2619
dd61ec5c 2620 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2621 if (val)
2622 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2623 }
2624 if (e.reason < 0)
2625 {
0000e5cc
PA
2626 bp_err = e.error;
2627 bp_err_message = e.message;
dd61ec5c 2628 }
879bfdc2
DJ
2629 }
2630 else
2631 {
2632 /* No. This breakpoint will not be inserted.
2633 No error, but do not mark the bp as 'inserted'. */
2634 return 0;
2635 }
2636 }
2637
0000e5cc 2638 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2639 {
2640 /* Can't set the breakpoint. */
0000e5cc
PA
2641
2642 /* In some cases, we might not be able to insert a
2643 breakpoint in a shared library that has already been
2644 removed, but we have not yet processed the shlib unload
2645 event. Unfortunately, some targets that implement
076855f9
PA
2646 breakpoint insertion themselves can't tell why the
2647 breakpoint insertion failed (e.g., the remote target
2648 doesn't define error codes), so we must treat generic
2649 errors as memory errors. */
0000e5cc 2650 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2651 && bl->loc_type == bp_loc_software_breakpoint
08351840
PA
2652 && (solib_name_from_address (bl->pspace, bl->address)
2653 || userloaded_objfile_contains_address_p (bl->pspace,
2654 bl->address)))
879bfdc2 2655 {
4a64f543 2656 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2657 bl->shlib_disabled = 1;
8d3788bd 2658 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2659 if (!*disabled_breaks)
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 "Cannot insert breakpoint %d.\n",
2663 bl->owner->number);
2664 fprintf_unfiltered (tmp_error_stream,
2665 "Temporarily disabling shared "
2666 "library breakpoints:\n");
2667 }
2668 *disabled_breaks = 1;
879bfdc2 2669 fprintf_unfiltered (tmp_error_stream,
35df4500 2670 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2671 return 0;
879bfdc2
DJ
2672 }
2673 else
879bfdc2 2674 {
35df4500 2675 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2676 {
0000e5cc
PA
2677 *hw_breakpoint_error = 1;
2678 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2679 fprintf_unfiltered (tmp_error_stream,
2680 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2681 bl->owner->number, bp_err_message ? ":" : ".\n");
2682 if (bp_err_message != NULL)
2683 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2684 }
2685 else
2686 {
0000e5cc
PA
2687 if (bp_err_message == NULL)
2688 {
2689 char *message
2690 = memory_error_message (TARGET_XFER_E_IO,
2691 bl->gdbarch, bl->address);
2692 struct cleanup *old_chain = make_cleanup (xfree, message);
2693
2694 fprintf_unfiltered (tmp_error_stream,
2695 "Cannot insert breakpoint %d.\n"
2696 "%s\n",
2697 bl->owner->number, message);
2698 do_cleanups (old_chain);
2699 }
2700 else
2701 {
2702 fprintf_unfiltered (tmp_error_stream,
2703 "Cannot insert breakpoint %d: %s\n",
2704 bl->owner->number,
2705 bp_err_message);
2706 }
879bfdc2 2707 }
0000e5cc 2708 return 1;
879bfdc2
DJ
2709
2710 }
2711 }
2712 else
35df4500 2713 bl->inserted = 1;
879bfdc2 2714
0000e5cc 2715 return 0;
879bfdc2
DJ
2716 }
2717
35df4500 2718 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2719 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2720 watchpoints. It's not clear that it's necessary... */
35df4500 2721 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2722 {
0000e5cc
PA
2723 int val;
2724
77b06cd7
TJB
2725 gdb_assert (bl->owner->ops != NULL
2726 && bl->owner->ops->insert_location != NULL);
2727
2728 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2729
2730 /* If trying to set a read-watchpoint, and it turns out it's not
2731 supported, try emulating one with an access watchpoint. */
35df4500 2732 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2733 {
2734 struct bp_location *loc, **loc_temp;
2735
2736 /* But don't try to insert it, if there's already another
2737 hw_access location that would be considered a duplicate
2738 of this one. */
2739 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2740 if (loc != bl
85d721b8 2741 && loc->watchpoint_type == hw_access
35df4500 2742 && watchpoint_locations_match (bl, loc))
85d721b8 2743 {
35df4500
TJB
2744 bl->duplicate = 1;
2745 bl->inserted = 1;
2746 bl->target_info = loc->target_info;
2747 bl->watchpoint_type = hw_access;
85d721b8
PA
2748 val = 0;
2749 break;
2750 }
2751
2752 if (val == 1)
2753 {
77b06cd7
TJB
2754 bl->watchpoint_type = hw_access;
2755 val = bl->owner->ops->insert_location (bl);
2756
2757 if (val)
2758 /* Back to the original value. */
2759 bl->watchpoint_type = hw_read;
85d721b8
PA
2760 }
2761 }
2762
35df4500 2763 bl->inserted = (val == 0);
879bfdc2
DJ
2764 }
2765
35df4500 2766 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2767 {
0000e5cc
PA
2768 int val;
2769
77b06cd7
TJB
2770 gdb_assert (bl->owner->ops != NULL
2771 && bl->owner->ops->insert_location != NULL);
2772
2773 val = bl->owner->ops->insert_location (bl);
2774 if (val)
2775 {
2776 bl->owner->enable_state = bp_disabled;
2777
2778 if (val == 1)
2779 warning (_("\
2780Error inserting catchpoint %d: Your system does not support this type\n\
2781of catchpoint."), bl->owner->number);
2782 else
2783 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2784 }
2785
2786 bl->inserted = (val == 0);
1640b821
DJ
2787
2788 /* We've already printed an error message if there was a problem
2789 inserting this catchpoint, and we've disabled the catchpoint,
2790 so just return success. */
2791 return 0;
879bfdc2
DJ
2792 }
2793
2794 return 0;
2795}
2796
6c95b8df
PA
2797/* This function is called when program space PSPACE is about to be
2798 deleted. It takes care of updating breakpoints to not reference
2799 PSPACE anymore. */
2800
2801void
2802breakpoint_program_space_exit (struct program_space *pspace)
2803{
2804 struct breakpoint *b, *b_temp;
876fa593 2805 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2806
2807 /* Remove any breakpoint that was set through this program space. */
2808 ALL_BREAKPOINTS_SAFE (b, b_temp)
2809 {
2810 if (b->pspace == pspace)
2811 delete_breakpoint (b);
2812 }
2813
2814 /* Breakpoints set through other program spaces could have locations
2815 bound to PSPACE as well. Remove those. */
876fa593 2816 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2817 {
2818 struct bp_location *tmp;
2819
2820 if (loc->pspace == pspace)
2821 {
2bdf28a0 2822 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2823 if (loc->owner->loc == loc)
2824 loc->owner->loc = loc->next;
2825 else
2826 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2827 if (tmp->next == loc)
2828 {
2829 tmp->next = loc->next;
2830 break;
2831 }
2832 }
2833 }
2834
2835 /* Now update the global location list to permanently delete the
2836 removed locations above. */
2837 update_global_location_list (0);
2838}
2839
74960c60
VP
2840/* Make sure all breakpoints are inserted in inferior.
2841 Throws exception on any error.
2842 A breakpoint that is already inserted won't be inserted
2843 again, so calling this function twice is safe. */
2844void
2845insert_breakpoints (void)
2846{
2847 struct breakpoint *bpt;
2848
2849 ALL_BREAKPOINTS (bpt)
2850 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2851 {
2852 struct watchpoint *w = (struct watchpoint *) bpt;
2853
2854 update_watchpoint (w, 0 /* don't reparse. */);
2855 }
74960c60 2856
b60e7edf 2857 update_global_location_list (1);
74960c60 2858
c35b1492
PA
2859 /* update_global_location_list does not insert breakpoints when
2860 always_inserted_mode is not enabled. Explicitly insert them
2861 now. */
2862 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2863 insert_breakpoint_locations ();
2864}
2865
20388dd6
YQ
2866/* Invoke CALLBACK for each of bp_location. */
2867
2868void
2869iterate_over_bp_locations (walk_bp_location_callback callback)
2870{
2871 struct bp_location *loc, **loc_tmp;
2872
2873 ALL_BP_LOCATIONS (loc, loc_tmp)
2874 {
2875 callback (loc, NULL);
2876 }
2877}
2878
b775012e
LM
2879/* This is used when we need to synch breakpoint conditions between GDB and the
2880 target. It is the case with deleting and disabling of breakpoints when using
2881 always-inserted mode. */
2882
2883static void
2884update_inserted_breakpoint_locations (void)
2885{
2886 struct bp_location *bl, **blp_tmp;
2887 int error_flag = 0;
2888 int val = 0;
2889 int disabled_breaks = 0;
2890 int hw_breakpoint_error = 0;
dd61ec5c 2891 int hw_bp_details_reported = 0;
b775012e
LM
2892
2893 struct ui_file *tmp_error_stream = mem_fileopen ();
2894 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2895
2896 /* Explicitly mark the warning -- this will only be printed if
2897 there was an error. */
2898 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2899
2900 save_current_space_and_thread ();
2901
2902 ALL_BP_LOCATIONS (bl, blp_tmp)
2903 {
2904 /* We only want to update software breakpoints and hardware
2905 breakpoints. */
2906 if (!is_breakpoint (bl->owner))
2907 continue;
2908
2909 /* We only want to update locations that are already inserted
2910 and need updating. This is to avoid unwanted insertion during
2911 deletion of breakpoints. */
2912 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2913 continue;
2914
2915 switch_to_program_space_and_thread (bl->pspace);
2916
2917 /* For targets that support global breakpoints, there's no need
2918 to select an inferior to insert breakpoint to. In fact, even
2919 if we aren't attached to any process yet, we should still
2920 insert breakpoints. */
f5656ead 2921 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2922 && ptid_equal (inferior_ptid, null_ptid))
2923 continue;
2924
2925 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2926 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2927 if (val)
2928 error_flag = val;
2929 }
2930
2931 if (error_flag)
2932 {
2933 target_terminal_ours_for_output ();
2934 error_stream (tmp_error_stream);
2935 }
2936
2937 do_cleanups (cleanups);
2938}
2939
c30eee59 2940/* Used when starting or continuing the program. */
c906108c 2941
74960c60
VP
2942static void
2943insert_breakpoint_locations (void)
c906108c 2944{
a5606eee 2945 struct breakpoint *bpt;
35df4500 2946 struct bp_location *bl, **blp_tmp;
eacd795a 2947 int error_flag = 0;
c906108c 2948 int val = 0;
3fbb6ffa 2949 int disabled_breaks = 0;
81d0cc19 2950 int hw_breakpoint_error = 0;
dd61ec5c 2951 int hw_bp_error_explained_already = 0;
c906108c 2952
81d0cc19 2953 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2954 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2955
81d0cc19
GS
2956 /* Explicitly mark the warning -- this will only be printed if
2957 there was an error. */
2958 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2959
2960 save_current_space_and_thread ();
2961
35df4500 2962 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2963 {
b775012e 2964 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2965 continue;
2966
4a64f543
MS
2967 /* There is no point inserting thread-specific breakpoints if
2968 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2969 has BL->OWNER always non-NULL. */
35df4500
TJB
2970 if (bl->owner->thread != -1
2971 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2972 continue;
2973
35df4500 2974 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2975
2976 /* For targets that support global breakpoints, there's no need
2977 to select an inferior to insert breakpoint to. In fact, even
2978 if we aren't attached to any process yet, we should still
2979 insert breakpoints. */
f5656ead 2980 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2981 && ptid_equal (inferior_ptid, null_ptid))
2982 continue;
2983
3fbb6ffa 2984 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2985 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2986 if (val)
eacd795a 2987 error_flag = val;
879bfdc2 2988 }
c906108c 2989
4a64f543
MS
2990 /* If we failed to insert all locations of a watchpoint, remove
2991 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2992 ALL_BREAKPOINTS (bpt)
2993 {
2994 int some_failed = 0;
2995 struct bp_location *loc;
2996
2997 if (!is_hardware_watchpoint (bpt))
2998 continue;
2999
d6b74ac4 3000 if (!breakpoint_enabled (bpt))
a5606eee 3001 continue;
74960c60
VP
3002
3003 if (bpt->disposition == disp_del_at_next_stop)
3004 continue;
a5606eee
VP
3005
3006 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3007 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3008 {
3009 some_failed = 1;
3010 break;
3011 }
3012 if (some_failed)
3013 {
3014 for (loc = bpt->loc; loc; loc = loc->next)
3015 if (loc->inserted)
3016 remove_breakpoint (loc, mark_uninserted);
3017
3018 hw_breakpoint_error = 1;
3019 fprintf_unfiltered (tmp_error_stream,
3020 "Could not insert hardware watchpoint %d.\n",
3021 bpt->number);
eacd795a 3022 error_flag = -1;
a5606eee
VP
3023 }
3024 }
3025
eacd795a 3026 if (error_flag)
81d0cc19
GS
3027 {
3028 /* If a hardware breakpoint or watchpoint was inserted, add a
3029 message about possibly exhausted resources. */
dd61ec5c 3030 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3031 {
c6510018
MS
3032 fprintf_unfiltered (tmp_error_stream,
3033 "Could not insert hardware breakpoints:\n\
3034You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3035 }
81d0cc19
GS
3036 target_terminal_ours_for_output ();
3037 error_stream (tmp_error_stream);
3038 }
f7545552
TT
3039
3040 do_cleanups (cleanups);
c906108c
SS
3041}
3042
c30eee59
TJB
3043/* Used when the program stops.
3044 Returns zero if successful, or non-zero if there was a problem
3045 removing a breakpoint location. */
3046
c906108c 3047int
fba45db2 3048remove_breakpoints (void)
c906108c 3049{
35df4500 3050 struct bp_location *bl, **blp_tmp;
3a1bae8e 3051 int val = 0;
c906108c 3052
35df4500 3053 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3054 {
1e4d1764 3055 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3056 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3057 }
3a1bae8e 3058 return val;
c906108c
SS
3059}
3060
49fa26b0
PA
3061/* When a thread exits, remove breakpoints that are related to
3062 that thread. */
3063
3064static void
3065remove_threaded_breakpoints (struct thread_info *tp, int silent)
3066{
3067 struct breakpoint *b, *b_tmp;
3068
3069 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3070 {
96181529 3071 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3072 {
3073 b->disposition = disp_del_at_next_stop;
3074
3075 printf_filtered (_("\
46ecd527 3076Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3077 b->number, tp->num);
3078
3079 /* Hide it from the user. */
3080 b->number = 0;
3081 }
3082 }
3083}
3084
6c95b8df
PA
3085/* Remove breakpoints of process PID. */
3086
3087int
3088remove_breakpoints_pid (int pid)
3089{
35df4500 3090 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3091 int val;
3092 struct inferior *inf = find_inferior_pid (pid);
3093
35df4500 3094 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3095 {
35df4500 3096 if (bl->pspace != inf->pspace)
6c95b8df
PA
3097 continue;
3098
d3ce09f5
SS
3099 if (bl->owner->type == bp_dprintf)
3100 continue;
3101
35df4500 3102 if (bl->inserted)
6c95b8df 3103 {
35df4500 3104 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3105 if (val != 0)
3106 return val;
3107 }
3108 }
3109 return 0;
3110}
3111
c906108c 3112int
fba45db2 3113reattach_breakpoints (int pid)
c906108c 3114{
6c95b8df 3115 struct cleanup *old_chain;
35df4500 3116 struct bp_location *bl, **blp_tmp;
c906108c 3117 int val;
86b887df 3118 struct ui_file *tmp_error_stream;
dd61ec5c 3119 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3120 struct inferior *inf;
3121 struct thread_info *tp;
3122
3123 tp = any_live_thread_of_process (pid);
3124 if (tp == NULL)
3125 return 1;
3126
3127 inf = find_inferior_pid (pid);
3128 old_chain = save_inferior_ptid ();
3129
3130 inferior_ptid = tp->ptid;
a4954f26 3131
86b887df 3132 tmp_error_stream = mem_fileopen ();
a4954f26 3133 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3134
35df4500 3135 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3136 {
35df4500 3137 if (bl->pspace != inf->pspace)
6c95b8df
PA
3138 continue;
3139
35df4500 3140 if (bl->inserted)
c5aa993b 3141 {
35df4500 3142 bl->inserted = 0;
dd61ec5c 3143 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3144 if (val != 0)
3145 {
ce696e05 3146 do_cleanups (old_chain);
c5aa993b
JM
3147 return val;
3148 }
3149 }
3150 }
ce696e05 3151 do_cleanups (old_chain);
c906108c
SS
3152 return 0;
3153}
3154
e58b0e63
PA
3155static int internal_breakpoint_number = -1;
3156
84f4c1fe
PM
3157/* Set the breakpoint number of B, depending on the value of INTERNAL.
3158 If INTERNAL is non-zero, the breakpoint number will be populated
3159 from internal_breakpoint_number and that variable decremented.
e5dd4106 3160 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3161 breakpoint_count and that value incremented. Internal breakpoints
3162 do not set the internal var bpnum. */
3163static void
3164set_breakpoint_number (int internal, struct breakpoint *b)
3165{
3166 if (internal)
3167 b->number = internal_breakpoint_number--;
3168 else
3169 {
3170 set_breakpoint_count (breakpoint_count + 1);
3171 b->number = breakpoint_count;
3172 }
3173}
3174
e62c965a 3175static struct breakpoint *
a6d9a66e 3176create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3177 CORE_ADDR address, enum bptype type,
c0a91b2b 3178 const struct breakpoint_ops *ops)
e62c965a 3179{
e62c965a
PP
3180 struct symtab_and_line sal;
3181 struct breakpoint *b;
3182
4a64f543 3183 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3184
3185 sal.pc = address;
3186 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3187 sal.pspace = current_program_space;
e62c965a 3188
06edf0c0 3189 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3190 b->number = internal_breakpoint_number--;
3191 b->disposition = disp_donttouch;
3192
3193 return b;
3194}
3195
17450429
PP
3196static const char *const longjmp_names[] =
3197 {
3198 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3199 };
3200#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3201
3202/* Per-objfile data private to breakpoint.c. */
3203struct breakpoint_objfile_data
3204{
3205 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3206 struct bound_minimal_symbol overlay_msym;
17450429
PP
3207
3208 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3209 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3210
28106bc2
SDJ
3211 /* True if we have looked for longjmp probes. */
3212 int longjmp_searched;
3213
3214 /* SystemTap probe points for longjmp (if any). */
3215 VEC (probe_p) *longjmp_probes;
3216
17450429 3217 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3218 struct bound_minimal_symbol terminate_msym;
17450429
PP
3219
3220 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3221 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3222
3223 /* True if we have looked for exception probes. */
3224 int exception_searched;
3225
3226 /* SystemTap probe points for unwinding (if any). */
3227 VEC (probe_p) *exception_probes;
17450429
PP
3228};
3229
3230static const struct objfile_data *breakpoint_objfile_key;
3231
3232/* Minimal symbol not found sentinel. */
3233static struct minimal_symbol msym_not_found;
3234
3235/* Returns TRUE if MSYM point to the "not found" sentinel. */
3236
3237static int
3238msym_not_found_p (const struct minimal_symbol *msym)
3239{
3240 return msym == &msym_not_found;
3241}
3242
3243/* Return per-objfile data needed by breakpoint.c.
3244 Allocate the data if necessary. */
3245
3246static struct breakpoint_objfile_data *
3247get_breakpoint_objfile_data (struct objfile *objfile)
3248{
3249 struct breakpoint_objfile_data *bp_objfile_data;
3250
3251 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3252 if (bp_objfile_data == NULL)
3253 {
3254 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3255 sizeof (*bp_objfile_data));
3256
3257 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3258 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3259 }
3260 return bp_objfile_data;
3261}
3262
28106bc2
SDJ
3263static void
3264free_breakpoint_probes (struct objfile *obj, void *data)
3265{
3266 struct breakpoint_objfile_data *bp_objfile_data = data;
3267
3268 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3269 VEC_free (probe_p, bp_objfile_data->exception_probes);
3270}
3271
e62c965a 3272static void
af02033e 3273create_overlay_event_breakpoint (void)
e62c965a 3274{
69de3c6a 3275 struct objfile *objfile;
af02033e 3276 const char *const func_name = "_ovly_debug_event";
e62c965a 3277
69de3c6a
PP
3278 ALL_OBJFILES (objfile)
3279 {
3280 struct breakpoint *b;
17450429
PP
3281 struct breakpoint_objfile_data *bp_objfile_data;
3282 CORE_ADDR addr;
69de3c6a 3283
17450429
PP
3284 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3285
3b7344d5 3286 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3287 continue;
3288
3b7344d5 3289 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3290 {
3b7344d5 3291 struct bound_minimal_symbol m;
17450429
PP
3292
3293 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3294 if (m.minsym == NULL)
17450429
PP
3295 {
3296 /* Avoid future lookups in this objfile. */
3b7344d5 3297 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3298 continue;
3299 }
3300 bp_objfile_data->overlay_msym = m;
3301 }
e62c965a 3302
77e371c0 3303 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3304 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3305 bp_overlay_event,
3306 &internal_breakpoint_ops);
69de3c6a 3307 b->addr_string = xstrdup (func_name);
e62c965a 3308
69de3c6a
PP
3309 if (overlay_debugging == ovly_auto)
3310 {
3311 b->enable_state = bp_enabled;
3312 overlay_events_enabled = 1;
3313 }
3314 else
3315 {
3316 b->enable_state = bp_disabled;
3317 overlay_events_enabled = 0;
3318 }
e62c965a
PP
3319 }
3320 update_global_location_list (1);
3321}
3322
0fd8e87f 3323static void
af02033e 3324create_longjmp_master_breakpoint (void)
0fd8e87f 3325{
6c95b8df 3326 struct program_space *pspace;
6c95b8df
PA
3327 struct cleanup *old_chain;
3328
3329 old_chain = save_current_program_space ();
0fd8e87f 3330
6c95b8df 3331 ALL_PSPACES (pspace)
af02033e
PP
3332 {
3333 struct objfile *objfile;
3334
3335 set_current_program_space (pspace);
3336
3337 ALL_OBJFILES (objfile)
0fd8e87f 3338 {
af02033e
PP
3339 int i;
3340 struct gdbarch *gdbarch;
17450429 3341 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3342
af02033e 3343 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3344
17450429
PP
3345 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3346
28106bc2
SDJ
3347 if (!bp_objfile_data->longjmp_searched)
3348 {
25f9533e
SDJ
3349 VEC (probe_p) *ret;
3350
3351 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3352 if (ret != NULL)
3353 {
3354 /* We are only interested in checking one element. */
3355 struct probe *p = VEC_index (probe_p, ret, 0);
3356
3357 if (!can_evaluate_probe_arguments (p))
3358 {
3359 /* We cannot use the probe interface here, because it does
3360 not know how to evaluate arguments. */
3361 VEC_free (probe_p, ret);
3362 ret = NULL;
3363 }
3364 }
3365 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3366 bp_objfile_data->longjmp_searched = 1;
3367 }
3368
3369 if (bp_objfile_data->longjmp_probes != NULL)
3370 {
3371 int i;
3372 struct probe *probe;
3373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3374
3375 for (i = 0;
3376 VEC_iterate (probe_p,
3377 bp_objfile_data->longjmp_probes,
3378 i, probe);
3379 ++i)
3380 {
3381 struct breakpoint *b;
3382
729662a5
TT
3383 b = create_internal_breakpoint (gdbarch,
3384 get_probe_address (probe,
3385 objfile),
28106bc2
SDJ
3386 bp_longjmp_master,
3387 &internal_breakpoint_ops);
3388 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3389 b->enable_state = bp_disabled;
3390 }
3391
3392 continue;
3393 }
3394
0569175e
TSD
3395 if (!gdbarch_get_longjmp_target_p (gdbarch))
3396 continue;
3397
17450429 3398 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3399 {
3400 struct breakpoint *b;
af02033e 3401 const char *func_name;
17450429 3402 CORE_ADDR addr;
6c95b8df 3403
3b7344d5 3404 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3405 continue;
0fd8e87f 3406
17450429 3407 func_name = longjmp_names[i];
3b7344d5 3408 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3409 {
3b7344d5 3410 struct bound_minimal_symbol m;
17450429
PP
3411
3412 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3413 if (m.minsym == NULL)
17450429
PP
3414 {
3415 /* Prevent future lookups in this objfile. */
3b7344d5 3416 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3417 continue;
3418 }
3419 bp_objfile_data->longjmp_msym[i] = m;
3420 }
3421
77e371c0 3422 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3423 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3424 &internal_breakpoint_ops);
af02033e
PP
3425 b->addr_string = xstrdup (func_name);
3426 b->enable_state = bp_disabled;
3427 }
0fd8e87f 3428 }
af02033e 3429 }
0fd8e87f 3430 update_global_location_list (1);
6c95b8df
PA
3431
3432 do_cleanups (old_chain);
0fd8e87f
UW
3433}
3434
af02033e 3435/* Create a master std::terminate breakpoint. */
aa7d318d 3436static void
af02033e 3437create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3438{
3439 struct program_space *pspace;
aa7d318d 3440 struct cleanup *old_chain;
af02033e 3441 const char *const func_name = "std::terminate()";
aa7d318d
TT
3442
3443 old_chain = save_current_program_space ();
3444
3445 ALL_PSPACES (pspace)
17450429
PP
3446 {
3447 struct objfile *objfile;
3448 CORE_ADDR addr;
3449
3450 set_current_program_space (pspace);
3451
aa7d318d
TT
3452 ALL_OBJFILES (objfile)
3453 {
3454 struct breakpoint *b;
17450429 3455 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3456
17450429 3457 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3458
3b7344d5 3459 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3460 continue;
3461
3b7344d5 3462 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3463 {
3b7344d5 3464 struct bound_minimal_symbol m;
17450429
PP
3465
3466 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3467 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3468 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3469 {
3470 /* Prevent future lookups in this objfile. */
3b7344d5 3471 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3472 continue;
3473 }
3474 bp_objfile_data->terminate_msym = m;
3475 }
aa7d318d 3476
77e371c0 3477 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3478 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3479 bp_std_terminate_master,
3480 &internal_breakpoint_ops);
aa7d318d
TT
3481 b->addr_string = xstrdup (func_name);
3482 b->enable_state = bp_disabled;
3483 }
17450429
PP
3484 }
3485
aa7d318d
TT
3486 update_global_location_list (1);
3487
3488 do_cleanups (old_chain);
3489}
3490
186c406b
TT
3491/* Install a master breakpoint on the unwinder's debug hook. */
3492
70221824 3493static void
186c406b
TT
3494create_exception_master_breakpoint (void)
3495{
3496 struct objfile *objfile;
17450429 3497 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3498
3499 ALL_OBJFILES (objfile)
3500 {
17450429
PP
3501 struct breakpoint *b;
3502 struct gdbarch *gdbarch;
3503 struct breakpoint_objfile_data *bp_objfile_data;
3504 CORE_ADDR addr;
3505
3506 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3507
28106bc2
SDJ
3508 /* We prefer the SystemTap probe point if it exists. */
3509 if (!bp_objfile_data->exception_searched)
3510 {
25f9533e
SDJ
3511 VEC (probe_p) *ret;
3512
3513 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3514
3515 if (ret != NULL)
3516 {
3517 /* We are only interested in checking one element. */
3518 struct probe *p = VEC_index (probe_p, ret, 0);
3519
3520 if (!can_evaluate_probe_arguments (p))
3521 {
3522 /* We cannot use the probe interface here, because it does
3523 not know how to evaluate arguments. */
3524 VEC_free (probe_p, ret);
3525 ret = NULL;
3526 }
3527 }
3528 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3529 bp_objfile_data->exception_searched = 1;
3530 }
3531
3532 if (bp_objfile_data->exception_probes != NULL)
3533 {
3534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3535 int i;
3536 struct probe *probe;
3537
3538 for (i = 0;
3539 VEC_iterate (probe_p,
3540 bp_objfile_data->exception_probes,
3541 i, probe);
3542 ++i)
3543 {
3544 struct breakpoint *b;
3545
729662a5
TT
3546 b = create_internal_breakpoint (gdbarch,
3547 get_probe_address (probe,
3548 objfile),
28106bc2
SDJ
3549 bp_exception_master,
3550 &internal_breakpoint_ops);
3551 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3552 b->enable_state = bp_disabled;
3553 }
3554
3555 continue;
3556 }
3557
3558 /* Otherwise, try the hook function. */
3559
3b7344d5 3560 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3561 continue;
3562
3563 gdbarch = get_objfile_arch (objfile);
186c406b 3564
3b7344d5 3565 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3566 {
3b7344d5 3567 struct bound_minimal_symbol debug_hook;
186c406b 3568
17450429 3569 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3570 if (debug_hook.minsym == NULL)
17450429 3571 {
3b7344d5 3572 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3573 continue;
3574 }
3575
3576 bp_objfile_data->exception_msym = debug_hook;
186c406b 3577 }
17450429 3578
77e371c0 3579 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3580 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3581 &current_target);
06edf0c0
PA
3582 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3583 &internal_breakpoint_ops);
17450429
PP
3584 b->addr_string = xstrdup (func_name);
3585 b->enable_state = bp_disabled;
186c406b
TT
3586 }
3587
3588 update_global_location_list (1);
3589}
3590
c906108c 3591void
fba45db2 3592update_breakpoints_after_exec (void)
c906108c 3593{
35df4500 3594 struct breakpoint *b, *b_tmp;
876fa593 3595 struct bp_location *bploc, **bplocp_tmp;
c906108c 3596
25b22b0a
PA
3597 /* We're about to delete breakpoints from GDB's lists. If the
3598 INSERTED flag is true, GDB will try to lift the breakpoints by
3599 writing the breakpoints' "shadow contents" back into memory. The
3600 "shadow contents" are NOT valid after an exec, so GDB should not
3601 do that. Instead, the target is responsible from marking
3602 breakpoints out as soon as it detects an exec. We don't do that
3603 here instead, because there may be other attempts to delete
3604 breakpoints after detecting an exec and before reaching here. */
876fa593 3605 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3606 if (bploc->pspace == current_program_space)
3607 gdb_assert (!bploc->inserted);
c906108c 3608
35df4500 3609 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3610 {
6c95b8df
PA
3611 if (b->pspace != current_program_space)
3612 continue;
3613
4a64f543 3614 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3615 if (b->type == bp_shlib_event)
3616 {
3617 delete_breakpoint (b);
3618 continue;
3619 }
c906108c 3620
4a64f543 3621 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3622 if (b->type == bp_jit_event)
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
3627
1900040c 3628 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3629 as must overlay event and longjmp master breakpoints. */
3630 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3631 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3632 || b->type == bp_exception_master)
c4093a6a
JM
3633 {
3634 delete_breakpoint (b);
3635 continue;
3636 }
3637
4a64f543 3638 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3639 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3640 {
3641 delete_breakpoint (b);
3642 continue;
3643 }
3644
611c83ae
PA
3645 /* Longjmp and longjmp-resume breakpoints are also meaningless
3646 after an exec. */
186c406b 3647 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3648 || b->type == bp_longjmp_call_dummy
186c406b 3649 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3650 {
3651 delete_breakpoint (b);
3652 continue;
3653 }
3654
ce78b96d
JB
3655 if (b->type == bp_catchpoint)
3656 {
3657 /* For now, none of the bp_catchpoint breakpoints need to
3658 do anything at this point. In the future, if some of
3659 the catchpoints need to something, we will need to add
3660 a new method, and call this method from here. */
3661 continue;
3662 }
3663
c5aa993b
JM
3664 /* bp_finish is a special case. The only way we ought to be able
3665 to see one of these when an exec() has happened, is if the user
3666 caught a vfork, and then said "finish". Ordinarily a finish just
3667 carries them to the call-site of the current callee, by setting
3668 a temporary bp there and resuming. But in this case, the finish
3669 will carry them entirely through the vfork & exec.
3670
3671 We don't want to allow a bp_finish to remain inserted now. But
3672 we can't safely delete it, 'cause finish_command has a handle to
3673 the bp on a bpstat, and will later want to delete it. There's a
3674 chance (and I've seen it happen) that if we delete the bp_finish
3675 here, that its storage will get reused by the time finish_command
3676 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3677 We really must allow finish_command to delete a bp_finish.
3678
e5dd4106 3679 In the absence of a general solution for the "how do we know
53a5351d
JM
3680 it's safe to delete something others may have handles to?"
3681 problem, what we'll do here is just uninsert the bp_finish, and
3682 let finish_command delete it.
3683
3684 (We know the bp_finish is "doomed" in the sense that it's
3685 momentary, and will be deleted as soon as finish_command sees
3686 the inferior stopped. So it doesn't matter that the bp's
3687 address is probably bogus in the new a.out, unlike e.g., the
3688 solib breakpoints.) */
c5aa993b 3689
c5aa993b
JM
3690 if (b->type == bp_finish)
3691 {
3692 continue;
3693 }
3694
3695 /* Without a symbolic address, we have little hope of the
3696 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3697 a.out. */
c5aa993b
JM
3698 if (b->addr_string == NULL)
3699 {
3700 delete_breakpoint (b);
3701 continue;
3702 }
c5aa993b 3703 }
1900040c 3704 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3705 create_overlay_event_breakpoint ();
3706 create_longjmp_master_breakpoint ();
3707 create_std_terminate_master_breakpoint ();
186c406b 3708 create_exception_master_breakpoint ();
c906108c
SS
3709}
3710
3711int
d80ee84f 3712detach_breakpoints (ptid_t ptid)
c906108c 3713{
35df4500 3714 struct bp_location *bl, **blp_tmp;
3a1bae8e 3715 int val = 0;
ce696e05 3716 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3717 struct inferior *inf = current_inferior ();
c5aa993b 3718
dfd4cc63 3719 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3720 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3721
6c95b8df 3722 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3723 inferior_ptid = ptid;
35df4500 3724 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3725 {
35df4500 3726 if (bl->pspace != inf->pspace)
6c95b8df
PA
3727 continue;
3728
bd9673a4
PW
3729 /* This function must physically remove breakpoints locations
3730 from the specified ptid, without modifying the breakpoint
3731 package's state. Locations of type bp_loc_other are only
3732 maintained at GDB side. So, there is no need to remove
3733 these bp_loc_other locations. Moreover, removing these
3734 would modify the breakpoint package's state. */
3735 if (bl->loc_type == bp_loc_other)
3736 continue;
3737
35df4500
TJB
3738 if (bl->inserted)
3739 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3740 }
d03285ec
UW
3741
3742 /* Detach single-step breakpoints as well. */
3743 detach_single_step_breakpoints ();
3744
ce696e05 3745 do_cleanups (old_chain);
3a1bae8e 3746 return val;
c906108c
SS
3747}
3748
35df4500 3749/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3750 Note that this is used to detach breakpoints from a child fork.
3751 When we get here, the child isn't in the inferior list, and neither
3752 do we have objects to represent its address space --- we should
35df4500 3753 *not* look at bl->pspace->aspace here. */
6c95b8df 3754
c906108c 3755static int
35df4500 3756remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3757{
3758 int val;
c5aa993b 3759
35df4500
TJB
3760 /* BL is never in moribund_locations by our callers. */
3761 gdb_assert (bl->owner != NULL);
2bdf28a0 3762
35df4500 3763 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3764 /* Permanent breakpoints cannot be inserted or removed. */
3765 return 0;
3766
74960c60
VP
3767 /* The type of none suggests that owner is actually deleted.
3768 This should not ever happen. */
35df4500 3769 gdb_assert (bl->owner->type != bp_none);
0bde7532 3770
35df4500
TJB
3771 if (bl->loc_type == bp_loc_software_breakpoint
3772 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3773 {
c02f5703
MS
3774 /* "Normal" instruction breakpoint: either the standard
3775 trap-instruction bp (bp_breakpoint), or a
3776 bp_hardware_breakpoint. */
3777
3778 /* First check to see if we have to handle an overlay. */
3779 if (overlay_debugging == ovly_off
35df4500
TJB
3780 || bl->section == NULL
3781 || !(section_is_overlay (bl->section)))
c02f5703
MS
3782 {
3783 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3784
3785 /* If we're trying to uninsert a memory breakpoint that we
3786 know is set in a dynamic object that is marked
3787 shlib_disabled, then either the dynamic object was
3788 removed with "remove-symbol-file" or with
3789 "nosharedlibrary". In the former case, we don't know
3790 whether another dynamic object might have loaded over the
3791 breakpoint's address -- the user might well let us know
3792 about it next with add-symbol-file (the whole point of
3793 OBJF_USERLOADED is letting the user manually maintain a
3794 list of dynamically loaded objects). If we have the
3795 breakpoint's shadow memory, that is, this is a software
3796 breakpoint managed by GDB, check whether the breakpoint
3797 is still inserted in memory, to avoid overwriting wrong
3798 code with stale saved shadow contents. Note that HW
3799 breakpoints don't have shadow memory, as they're
3800 implemented using a mechanism that is not dependent on
3801 being able to modify the target's memory, and as such
3802 they should always be removed. */
3803 if (bl->shlib_disabled
3804 && bl->target_info.shadow_len != 0
3805 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3806 val = 0;
3807 else
3808 val = bl->owner->ops->remove_location (bl);
c02f5703 3809 }
c906108c
SS
3810 else
3811 {
4a64f543 3812 /* This breakpoint is in an overlay section.
c02f5703
MS
3813 Did we set a breakpoint at the LMA? */
3814 if (!overlay_events_enabled)
3815 {
3816 /* Yes -- overlay event support is not active, so we
3817 should have set a breakpoint at the LMA. Remove it.
3818 */
c02f5703
MS
3819 /* Ignore any failures: if the LMA is in ROM, we will
3820 have already warned when we failed to insert it. */
35df4500
TJB
3821 if (bl->loc_type == bp_loc_hardware_breakpoint)
3822 target_remove_hw_breakpoint (bl->gdbarch,
3823 &bl->overlay_target_info);
c02f5703 3824 else
35df4500
TJB
3825 target_remove_breakpoint (bl->gdbarch,
3826 &bl->overlay_target_info);
c02f5703
MS
3827 }
3828 /* Did we set a breakpoint at the VMA?
3829 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3830 if (bl->inserted)
c906108c 3831 {
c02f5703
MS
3832 /* Yes -- remove it. Previously we did not bother to
3833 remove the breakpoint if the section had been
3834 unmapped, but let's not rely on that being safe. We
3835 don't know what the overlay manager might do. */
aa67235e
UW
3836
3837 /* However, we should remove *software* breakpoints only
3838 if the section is still mapped, or else we overwrite
3839 wrong code with the saved shadow contents. */
348d480f
PA
3840 if (bl->loc_type == bp_loc_hardware_breakpoint
3841 || section_is_mapped (bl->section))
3842 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3843 else
3844 val = 0;
c906108c 3845 }
c02f5703
MS
3846 else
3847 {
3848 /* No -- not inserted, so no need to remove. No error. */
3849 val = 0;
3850 }
c906108c 3851 }
879d1e6b 3852
08351840
PA
3853 /* In some cases, we might not be able to remove a breakpoint in
3854 a shared library that has already been removed, but we have
3855 not yet processed the shlib unload event. Similarly for an
3856 unloaded add-symbol-file object - the user might not yet have
3857 had the chance to remove-symbol-file it. shlib_disabled will
3858 be set if the library/object has already been removed, but
3859 the breakpoint hasn't been uninserted yet, e.g., after
3860 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3861 always-inserted mode. */
076855f9 3862 if (val
08351840
PA
3863 && (bl->loc_type == bp_loc_software_breakpoint
3864 && (bl->shlib_disabled
3865 || solib_name_from_address (bl->pspace, bl->address)
3866 || userloaded_objfile_contains_address_p (bl->pspace,
3867 bl->address))))
879d1e6b
UW
3868 val = 0;
3869
c906108c
SS
3870 if (val)
3871 return val;
35df4500 3872 bl->inserted = (is == mark_inserted);
c906108c 3873 }
35df4500 3874 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3875 {
77b06cd7
TJB
3876 gdb_assert (bl->owner->ops != NULL
3877 && bl->owner->ops->remove_location != NULL);
3878
35df4500 3879 bl->inserted = (is == mark_inserted);
77b06cd7 3880 bl->owner->ops->remove_location (bl);
2e70b7b9 3881
c906108c 3882 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3883 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3884 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3885 bl->owner->number);
c906108c 3886 }
35df4500
TJB
3887 else if (bl->owner->type == bp_catchpoint
3888 && breakpoint_enabled (bl->owner)
3889 && !bl->duplicate)
ce78b96d 3890 {
77b06cd7
TJB
3891 gdb_assert (bl->owner->ops != NULL
3892 && bl->owner->ops->remove_location != NULL);
ce78b96d 3893
77b06cd7 3894 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3895 if (val)
3896 return val;
77b06cd7 3897
35df4500 3898 bl->inserted = (is == mark_inserted);
ce78b96d 3899 }
c906108c
SS
3900
3901 return 0;
3902}
3903
6c95b8df 3904static int
35df4500 3905remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3906{
3907 int ret;
3908 struct cleanup *old_chain;
3909
35df4500
TJB
3910 /* BL is never in moribund_locations by our callers. */
3911 gdb_assert (bl->owner != NULL);
2bdf28a0 3912
35df4500 3913 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3914 /* Permanent breakpoints cannot be inserted or removed. */
3915 return 0;
3916
3917 /* The type of none suggests that owner is actually deleted.
3918 This should not ever happen. */
35df4500 3919 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3920
3921 old_chain = save_current_space_and_thread ();
3922
35df4500 3923 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3924
35df4500 3925 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3926
3927 do_cleanups (old_chain);
3928 return ret;
3929}
3930
c906108c
SS
3931/* Clear the "inserted" flag in all breakpoints. */
3932
25b22b0a 3933void
fba45db2 3934mark_breakpoints_out (void)
c906108c 3935{
35df4500 3936 struct bp_location *bl, **blp_tmp;
c906108c 3937
35df4500
TJB
3938 ALL_BP_LOCATIONS (bl, blp_tmp)
3939 if (bl->pspace == current_program_space)
3940 bl->inserted = 0;
c906108c
SS
3941}
3942
53a5351d
JM
3943/* Clear the "inserted" flag in all breakpoints and delete any
3944 breakpoints which should go away between runs of the program.
c906108c
SS
3945
3946 Plus other such housekeeping that has to be done for breakpoints
3947 between runs.
3948
53a5351d
JM
3949 Note: this function gets called at the end of a run (by
3950 generic_mourn_inferior) and when a run begins (by
4a64f543 3951 init_wait_for_inferior). */
c906108c
SS
3952
3953
3954
3955void
fba45db2 3956breakpoint_init_inferior (enum inf_context context)
c906108c 3957{
35df4500
TJB
3958 struct breakpoint *b, *b_tmp;
3959 struct bp_location *bl, **blp_tmp;
1c5cfe86 3960 int ix;
6c95b8df 3961 struct program_space *pspace = current_program_space;
c906108c 3962
50c71eaf
PA
3963 /* If breakpoint locations are shared across processes, then there's
3964 nothing to do. */
f5656ead 3965 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3966 return;
3967
35df4500 3968 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3969 {
35df4500
TJB
3970 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3971 if (bl->pspace == pspace
3972 && bl->owner->enable_state != bp_permanent)
3973 bl->inserted = 0;
6c95b8df 3974 }
075f6582 3975
35df4500 3976 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3977 {
6c95b8df
PA
3978 if (b->loc && b->loc->pspace != pspace)
3979 continue;
3980
c5aa993b
JM
3981 switch (b->type)
3982 {
3983 case bp_call_dummy:
e2e4d78b 3984 case bp_longjmp_call_dummy:
c906108c 3985
c5aa993b 3986 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3987 cause problems when the inferior is rerun, so we better get
3988 rid of it. */
3989
3990 case bp_watchpoint_scope:
3991
3992 /* Also get rid of scope breakpoints. */
3993
3994 case bp_shlib_event:
3995
3996 /* Also remove solib event breakpoints. Their addresses may
3997 have changed since the last time we ran the program.
3998 Actually we may now be debugging against different target;
3999 and so the solib backend that installed this breakpoint may
4000 not be used in by the target. E.g.,
4001
4002 (gdb) file prog-linux
4003 (gdb) run # native linux target
4004 ...
4005 (gdb) kill
4006 (gdb) file prog-win.exe
4007 (gdb) tar rem :9999 # remote Windows gdbserver.
4008 */
c906108c 4009
f59f708a
PA
4010 case bp_step_resume:
4011
4012 /* Also remove step-resume breakpoints. */
4013
c5aa993b
JM
4014 delete_breakpoint (b);
4015 break;
c906108c 4016
c5aa993b
JM
4017 case bp_watchpoint:
4018 case bp_hardware_watchpoint:
4019 case bp_read_watchpoint:
4020 case bp_access_watchpoint:
3a5c3e22
PA
4021 {
4022 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4023
3a5c3e22
PA
4024 /* Likewise for watchpoints on local expressions. */
4025 if (w->exp_valid_block != NULL)
4026 delete_breakpoint (b);
4027 else if (context == inf_starting)
4028 {
4029 /* Reset val field to force reread of starting value in
4030 insert_breakpoints. */
4031 if (w->val)
4032 value_free (w->val);
4033 w->val = NULL;
4034 w->val_valid = 0;
c860120c 4035 }
3a5c3e22 4036 }
c5aa993b
JM
4037 break;
4038 default:
c5aa993b
JM
4039 break;
4040 }
4041 }
1c5cfe86
PA
4042
4043 /* Get rid of the moribund locations. */
35df4500
TJB
4044 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4045 decref_bp_location (&bl);
1c5cfe86 4046 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4047}
4048
6c95b8df
PA
4049/* These functions concern about actual breakpoints inserted in the
4050 target --- to e.g. check if we need to do decr_pc adjustment or if
4051 we need to hop over the bkpt --- so we check for address space
4052 match, not program space. */
4053
c2c6d25f
JM
4054/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4055 exists at PC. It returns ordinary_breakpoint_here if it's an
4056 ordinary breakpoint, or permanent_breakpoint_here if it's a
4057 permanent breakpoint.
4058 - When continuing from a location with an ordinary breakpoint, we
4059 actually single step once before calling insert_breakpoints.
e5dd4106 4060 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4061 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4062 the target, to advance the PC past the breakpoint. */
c906108c 4063
c2c6d25f 4064enum breakpoint_here
6c95b8df 4065breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4066{
35df4500 4067 struct bp_location *bl, **blp_tmp;
c2c6d25f 4068 int any_breakpoint_here = 0;
c906108c 4069
35df4500 4070 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4071 {
35df4500
TJB
4072 if (bl->loc_type != bp_loc_software_breakpoint
4073 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4074 continue;
4075
f1310107 4076 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4077 if ((breakpoint_enabled (bl->owner)
4078 || bl->owner->enable_state == bp_permanent)
f1310107 4079 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4080 {
4081 if (overlay_debugging
35df4500
TJB
4082 && section_is_overlay (bl->section)
4083 && !section_is_mapped (bl->section))
075f6582 4084 continue; /* unmapped overlay -- can't be a match */
35df4500 4085 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4086 return permanent_breakpoint_here;
4087 else
4088 any_breakpoint_here = 1;
4089 }
4090 }
c906108c 4091
c2c6d25f 4092 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4093}
4094
1c5cfe86
PA
4095/* Return true if there's a moribund breakpoint at PC. */
4096
4097int
6c95b8df 4098moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4099{
4100 struct bp_location *loc;
4101 int ix;
4102
4103 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4104 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4105 return 1;
4106
4107 return 0;
4108}
c2c6d25f 4109
c36b740a 4110/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4111 inserted using regular breakpoint_chain / bp_location array
4112 mechanism. This does not check for single-step breakpoints, which
4113 are inserted and removed using direct target manipulation. */
c906108c
SS
4114
4115int
4a64f543
MS
4116regular_breakpoint_inserted_here_p (struct address_space *aspace,
4117 CORE_ADDR pc)
c906108c 4118{
35df4500 4119 struct bp_location *bl, **blp_tmp;
c906108c 4120
35df4500 4121 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4122 {
35df4500
TJB
4123 if (bl->loc_type != bp_loc_software_breakpoint
4124 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4125 continue;
4126
35df4500 4127 if (bl->inserted
f1310107 4128 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4129 {
4130 if (overlay_debugging
35df4500
TJB
4131 && section_is_overlay (bl->section)
4132 && !section_is_mapped (bl->section))
075f6582
DJ
4133 continue; /* unmapped overlay -- can't be a match */
4134 else
4135 return 1;
4136 }
c5aa993b 4137 }
c36b740a
VP
4138 return 0;
4139}
4140
4141/* Returns non-zero iff there's either regular breakpoint
4142 or a single step breakpoint inserted at PC. */
4143
4144int
6c95b8df 4145breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4146{
6c95b8df 4147 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4148 return 1;
c906108c 4149
6c95b8df 4150 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4151 return 1;
4152
c906108c
SS
4153 return 0;
4154}
4155
4fa8626c
DJ
4156/* This function returns non-zero iff there is a software breakpoint
4157 inserted at PC. */
4158
4159int
3e43a32a
MS
4160software_breakpoint_inserted_here_p (struct address_space *aspace,
4161 CORE_ADDR pc)
4fa8626c 4162{
35df4500 4163 struct bp_location *bl, **blp_tmp;
4fa8626c 4164
35df4500 4165 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4166 {
35df4500 4167 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4168 continue;
4169
35df4500
TJB
4170 if (bl->inserted
4171 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4172 aspace, pc))
4fa8626c
DJ
4173 {
4174 if (overlay_debugging
35df4500
TJB
4175 && section_is_overlay (bl->section)
4176 && !section_is_mapped (bl->section))
4fa8626c
DJ
4177 continue; /* unmapped overlay -- can't be a match */
4178 else
4179 return 1;
4180 }
4181 }
4182
1aafd4da 4183 /* Also check for software single-step breakpoints. */
6c95b8df 4184 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4185 return 1;
4186
4fa8626c
DJ
4187 return 0;
4188}
4189
9093389c
PA
4190int
4191hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4192 CORE_ADDR addr, ULONGEST len)
4193{
4194 struct breakpoint *bpt;
4195
4196 ALL_BREAKPOINTS (bpt)
4197 {
4198 struct bp_location *loc;
4199
4200 if (bpt->type != bp_hardware_watchpoint
4201 && bpt->type != bp_access_watchpoint)
4202 continue;
4203
4204 if (!breakpoint_enabled (bpt))
4205 continue;
4206
4207 for (loc = bpt->loc; loc; loc = loc->next)
4208 if (loc->pspace->aspace == aspace && loc->inserted)
4209 {
4210 CORE_ADDR l, h;
4211
4212 /* Check for intersection. */
4213 l = max (loc->address, addr);
4214 h = min (loc->address + loc->length, addr + len);
4215 if (l < h)
4216 return 1;
4217 }
4218 }
4219 return 0;
4220}
4221
075f6582
DJ
4222/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4223 PC is valid for process/thread PTID. */
c906108c
SS
4224
4225int
6c95b8df
PA
4226breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4227 ptid_t ptid)
c906108c 4228{
35df4500 4229 struct bp_location *bl, **blp_tmp;
4a306c9a 4230 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4231 int thread = -1;
4a306c9a 4232 int task = 0;
a6f1cd96 4233
35df4500 4234 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4235 {
35df4500
TJB
4236 if (bl->loc_type != bp_loc_software_breakpoint
4237 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4238 continue;
4239
35df4500
TJB
4240 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4241 if (!breakpoint_enabled (bl->owner)
4242 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4243 continue;
4244
f1310107 4245 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4246 continue;
4247
35df4500 4248 if (bl->owner->thread != -1)
075f6582 4249 {
a6f1cd96
JB
4250 /* This is a thread-specific breakpoint. Check that ptid
4251 matches that thread. If thread hasn't been computed yet,
4252 it is now time to do so. */
4253 if (thread == -1)
4254 thread = pid_to_thread_id (ptid);
35df4500 4255 if (bl->owner->thread != thread)
a6f1cd96 4256 continue;
075f6582 4257 }
a6f1cd96 4258
35df4500 4259 if (bl->owner->task != 0)
4a306c9a
JB
4260 {
4261 /* This is a task-specific breakpoint. Check that ptid
4262 matches that task. If task hasn't been computed yet,
4263 it is now time to do so. */
4264 if (task == 0)
4265 task = ada_get_task_number (ptid);
35df4500 4266 if (bl->owner->task != task)
4a306c9a
JB
4267 continue;
4268 }
4269
a6f1cd96 4270 if (overlay_debugging
35df4500
TJB
4271 && section_is_overlay (bl->section)
4272 && !section_is_mapped (bl->section))
a6f1cd96
JB
4273 continue; /* unmapped overlay -- can't be a match */
4274
4275 return 1;
c5aa993b 4276 }
c906108c
SS
4277
4278 return 0;
4279}
c906108c 4280\f
c5aa993b 4281
c906108c
SS
4282/* bpstat stuff. External routines' interfaces are documented
4283 in breakpoint.h. */
4284
4285int
c326b90e 4286is_catchpoint (struct breakpoint *ep)
c906108c 4287{
533be4dd 4288 return (ep->type == bp_catchpoint);
c906108c
SS
4289}
4290
f431efe5
PA
4291/* Frees any storage that is part of a bpstat. Does not walk the
4292 'next' chain. */
4293
4294static void
198757a8
VP
4295bpstat_free (bpstat bs)
4296{
4297 if (bs->old_val != NULL)
4298 value_free (bs->old_val);
9add0f1b 4299 decref_counted_command_line (&bs->commands);
f431efe5 4300 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4301 xfree (bs);
4302}
4303
c906108c
SS
4304/* Clear a bpstat so that it says we are not at any breakpoint.
4305 Also free any storage that is part of a bpstat. */
4306
4307void
fba45db2 4308bpstat_clear (bpstat *bsp)
c906108c
SS
4309{
4310 bpstat p;
4311 bpstat q;
4312
4313 if (bsp == 0)
4314 return;
4315 p = *bsp;
4316 while (p != NULL)
4317 {
4318 q = p->next;
198757a8 4319 bpstat_free (p);
c906108c
SS
4320 p = q;
4321 }
4322 *bsp = NULL;
4323}
4324
4325/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4326 is part of the bpstat is copied as well. */
4327
4328bpstat
fba45db2 4329bpstat_copy (bpstat bs)
c906108c
SS
4330{
4331 bpstat p = NULL;
4332 bpstat tmp;
4333 bpstat retval = NULL;
4334
4335 if (bs == NULL)
4336 return bs;
4337
4338 for (; bs != NULL; bs = bs->next)
4339 {
4340 tmp = (bpstat) xmalloc (sizeof (*tmp));
4341 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4342 incref_counted_command_line (tmp->commands);
f431efe5 4343 incref_bp_location (tmp->bp_location_at);
31cc81e9 4344 if (bs->old_val != NULL)
3c3185ac
JK
4345 {
4346 tmp->old_val = value_copy (bs->old_val);
4347 release_value (tmp->old_val);
4348 }
31cc81e9 4349
c906108c
SS
4350 if (p == NULL)
4351 /* This is the first thing in the chain. */
4352 retval = tmp;
4353 else
4354 p->next = tmp;
4355 p = tmp;
4356 }
4357 p->next = NULL;
4358 return retval;
4359}
4360
4a64f543 4361/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4362
4363bpstat
fba45db2 4364bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4365{
c5aa993b
JM
4366 if (bsp == NULL)
4367 return NULL;
c906108c 4368
c5aa993b
JM
4369 for (; bsp != NULL; bsp = bsp->next)
4370 {
f431efe5 4371 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4372 return bsp;
4373 }
c906108c
SS
4374 return NULL;
4375}
4376
ab04a2af
TT
4377/* See breakpoint.h. */
4378
47591c29 4379int
427cd150 4380bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4381{
ab04a2af
TT
4382 for (; bsp != NULL; bsp = bsp->next)
4383 {
427cd150
TT
4384 if (bsp->breakpoint_at == NULL)
4385 {
4386 /* A moribund location can never explain a signal other than
4387 GDB_SIGNAL_TRAP. */
4388 if (sig == GDB_SIGNAL_TRAP)
47591c29 4389 return 1;
427cd150
TT
4390 }
4391 else
47591c29
PA
4392 {
4393 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4394 sig))
4395 return 1;
4396 }
ab04a2af
TT
4397 }
4398
47591c29 4399 return 0;
ab04a2af
TT
4400}
4401
4a64f543
MS
4402/* Put in *NUM the breakpoint number of the first breakpoint we are
4403 stopped at. *BSP upon return is a bpstat which points to the
4404 remaining breakpoints stopped at (but which is not guaranteed to be
4405 good for anything but further calls to bpstat_num).
4406
8671a17b
PA
4407 Return 0 if passed a bpstat which does not indicate any breakpoints.
4408 Return -1 if stopped at a breakpoint that has been deleted since
4409 we set it.
4410 Return 1 otherwise. */
c906108c
SS
4411
4412int
8671a17b 4413bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4414{
4415 struct breakpoint *b;
4416
4417 if ((*bsp) == NULL)
4418 return 0; /* No more breakpoint values */
8671a17b 4419
4a64f543
MS
4420 /* We assume we'll never have several bpstats that correspond to a
4421 single breakpoint -- otherwise, this function might return the
4422 same number more than once and this will look ugly. */
f431efe5 4423 b = (*bsp)->breakpoint_at;
8671a17b
PA
4424 *bsp = (*bsp)->next;
4425 if (b == NULL)
4426 return -1; /* breakpoint that's been deleted since */
4427
4428 *num = b->number; /* We have its number */
4429 return 1;
c906108c
SS
4430}
4431
e93ca019 4432/* See breakpoint.h. */
c906108c
SS
4433
4434void
e93ca019 4435bpstat_clear_actions (void)
c906108c 4436{
e93ca019
JK
4437 struct thread_info *tp;
4438 bpstat bs;
4439
4440 if (ptid_equal (inferior_ptid, null_ptid))
4441 return;
4442
4443 tp = find_thread_ptid (inferior_ptid);
4444 if (tp == NULL)
4445 return;
4446
4447 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4448 {
9add0f1b 4449 decref_counted_command_line (&bs->commands);
abf85f46 4450
c906108c
SS
4451 if (bs->old_val != NULL)
4452 {
4453 value_free (bs->old_val);
4454 bs->old_val = NULL;
4455 }
4456 }
4457}
4458
f3b1572e
PA
4459/* Called when a command is about to proceed the inferior. */
4460
4461static void
4462breakpoint_about_to_proceed (void)
4463{
4464 if (!ptid_equal (inferior_ptid, null_ptid))
4465 {
4466 struct thread_info *tp = inferior_thread ();
4467
4468 /* Allow inferior function calls in breakpoint commands to not
4469 interrupt the command list. When the call finishes
4470 successfully, the inferior will be standing at the same
4471 breakpoint as if nothing happened. */
16c381f0 4472 if (tp->control.in_infcall)
f3b1572e
PA
4473 return;
4474 }
4475
4476 breakpoint_proceeded = 1;
4477}
4478
4a64f543
MS
4479/* Stub for cleaning up our state if we error-out of a breakpoint
4480 command. */
c906108c 4481static void
4efb68b1 4482cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4483{
4484 executing_breakpoint_commands = 0;
4485}
4486
abf85f46
JK
4487/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4488 or its equivalent. */
4489
4490static int
4491command_line_is_silent (struct command_line *cmd)
4492{
4493 return cmd && (strcmp ("silent", cmd->line) == 0
4494 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4495}
4496
4a64f543
MS
4497/* Execute all the commands associated with all the breakpoints at
4498 this location. Any of these commands could cause the process to
4499 proceed beyond this point, etc. We look out for such changes by
4500 checking the global "breakpoint_proceeded" after each command.
c906108c 4501
347bddb7
PA
4502 Returns true if a breakpoint command resumed the inferior. In that
4503 case, it is the caller's responsibility to recall it again with the
4504 bpstat of the current thread. */
4505
4506static int
4507bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4508{
4509 bpstat bs;
4510 struct cleanup *old_chain;
347bddb7 4511 int again = 0;
c906108c
SS
4512
4513 /* Avoid endless recursion if a `source' command is contained
4514 in bs->commands. */
4515 if (executing_breakpoint_commands)
347bddb7 4516 return 0;
c906108c
SS
4517
4518 executing_breakpoint_commands = 1;
4519 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4520
cf6c5ffb
TT
4521 prevent_dont_repeat ();
4522
4a64f543 4523 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4524 bs = *bsp;
4525
4526 breakpoint_proceeded = 0;
4527 for (; bs != NULL; bs = bs->next)
4528 {
9add0f1b 4529 struct counted_command_line *ccmd;
6c50ab1c
JB
4530 struct command_line *cmd;
4531 struct cleanup *this_cmd_tree_chain;
4532
4533 /* Take ownership of the BSP's command tree, if it has one.
4534
4535 The command tree could legitimately contain commands like
4536 'step' and 'next', which call clear_proceed_status, which
4537 frees stop_bpstat's command tree. To make sure this doesn't
4538 free the tree we're executing out from under us, we need to
4539 take ownership of the tree ourselves. Since a given bpstat's
4540 commands are only executed once, we don't need to copy it; we
4541 can clear the pointer in the bpstat, and make sure we free
4542 the tree when we're done. */
9add0f1b
TT
4543 ccmd = bs->commands;
4544 bs->commands = NULL;
abf85f46
JK
4545 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4546 cmd = ccmd ? ccmd->commands : NULL;
4547 if (command_line_is_silent (cmd))
4548 {
4549 /* The action has been already done by bpstat_stop_status. */
4550 cmd = cmd->next;
4551 }
6c50ab1c 4552
c906108c
SS
4553 while (cmd != NULL)
4554 {
4555 execute_control_command (cmd);
4556
4557 if (breakpoint_proceeded)
4558 break;
4559 else
4560 cmd = cmd->next;
4561 }
6c50ab1c
JB
4562
4563 /* We can free this command tree now. */
4564 do_cleanups (this_cmd_tree_chain);
4565
c906108c 4566 if (breakpoint_proceeded)
32c1e744
VP
4567 {
4568 if (target_can_async_p ())
347bddb7
PA
4569 /* If we are in async mode, then the target might be still
4570 running, not stopped at any breakpoint, so nothing for
4571 us to do here -- just return to the event loop. */
4572 ;
32c1e744
VP
4573 else
4574 /* In sync mode, when execute_control_command returns
4575 we're already standing on the next breakpoint.
347bddb7
PA
4576 Breakpoint commands for that stop were not run, since
4577 execute_command does not run breakpoint commands --
4578 only command_line_handler does, but that one is not
4579 involved in execution of breakpoint commands. So, we
4580 can now execute breakpoint commands. It should be
4581 noted that making execute_command do bpstat actions is
4582 not an option -- in this case we'll have recursive
4583 invocation of bpstat for each breakpoint with a
4584 command, and can easily blow up GDB stack. Instead, we
4585 return true, which will trigger the caller to recall us
4586 with the new stop_bpstat. */
4587 again = 1;
4588 break;
32c1e744 4589 }
c906108c 4590 }
c2b8ed2c 4591 do_cleanups (old_chain);
347bddb7
PA
4592 return again;
4593}
4594
4595void
4596bpstat_do_actions (void)
4597{
353d1d73
JK
4598 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4599
347bddb7
PA
4600 /* Do any commands attached to breakpoint we are stopped at. */
4601 while (!ptid_equal (inferior_ptid, null_ptid)
4602 && target_has_execution
4603 && !is_exited (inferior_ptid)
4604 && !is_executing (inferior_ptid))
4605 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4606 and only return when it is stopped at the next breakpoint, we
4607 keep doing breakpoint actions until it returns false to
4608 indicate the inferior was not resumed. */
16c381f0 4609 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4610 break;
353d1d73
JK
4611
4612 discard_cleanups (cleanup_if_error);
c906108c
SS
4613}
4614
fa4727a6
DJ
4615/* Print out the (old or new) value associated with a watchpoint. */
4616
4617static void
4618watchpoint_value_print (struct value *val, struct ui_file *stream)
4619{
4620 if (val == NULL)
4621 fprintf_unfiltered (stream, _("<unreadable>"));
4622 else
79a45b7d
TT
4623 {
4624 struct value_print_options opts;
4625 get_user_print_options (&opts);
4626 value_print (val, stream, &opts);
4627 }
fa4727a6
DJ
4628}
4629
e514a9d6 4630/* Generic routine for printing messages indicating why we
4a64f543 4631 stopped. The behavior of this function depends on the value
e514a9d6
JM
4632 'print_it' in the bpstat structure. Under some circumstances we
4633 may decide not to print anything here and delegate the task to
4a64f543 4634 normal_stop(). */
e514a9d6
JM
4635
4636static enum print_stop_action
4637print_bp_stop_message (bpstat bs)
4638{
4639 switch (bs->print_it)
4640 {
4641 case print_it_noop:
4a64f543 4642 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4643 return PRINT_UNKNOWN;
4644 break;
4645
4646 case print_it_done:
4647 /* We still want to print the frame, but we already printed the
4a64f543 4648 relevant messages. */
e514a9d6
JM
4649 return PRINT_SRC_AND_LOC;
4650 break;
4651
4652 case print_it_normal:
4f8d1dc6 4653 {
f431efe5
PA
4654 struct breakpoint *b = bs->breakpoint_at;
4655
1a6a67de
TJB
4656 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4657 which has since been deleted. */
4658 if (b == NULL)
4659 return PRINT_UNKNOWN;
4660
348d480f
PA
4661 /* Normal case. Call the breakpoint's print_it method. */
4662 return b->ops->print_it (bs);
4f8d1dc6 4663 }
348d480f 4664 break;
3086aeae 4665
e514a9d6 4666 default:
8e65ff28 4667 internal_error (__FILE__, __LINE__,
e2e0b3e5 4668 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4669 break;
c906108c 4670 }
c906108c
SS
4671}
4672
edcc5120
TT
4673/* A helper function that prints a shared library stopped event. */
4674
4675static void
4676print_solib_event (int is_catchpoint)
4677{
4678 int any_deleted
4679 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4680 int any_added
4681 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4682
4683 if (!is_catchpoint)
4684 {
4685 if (any_added || any_deleted)
4686 ui_out_text (current_uiout,
4687 _("Stopped due to shared library event:\n"));
4688 else
4689 ui_out_text (current_uiout,
4690 _("Stopped due to shared library event (no "
4691 "libraries added or removed)\n"));
4692 }
4693
4694 if (ui_out_is_mi_like_p (current_uiout))
4695 ui_out_field_string (current_uiout, "reason",
4696 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4697
4698 if (any_deleted)
4699 {
4700 struct cleanup *cleanup;
4701 char *name;
4702 int ix;
4703
4704 ui_out_text (current_uiout, _(" Inferior unloaded "));
4705 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4706 "removed");
4707 for (ix = 0;
4708 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4709 ix, name);
4710 ++ix)
4711 {
4712 if (ix > 0)
4713 ui_out_text (current_uiout, " ");
4714 ui_out_field_string (current_uiout, "library", name);
4715 ui_out_text (current_uiout, "\n");
4716 }
4717
4718 do_cleanups (cleanup);
4719 }
4720
4721 if (any_added)
4722 {
4723 struct so_list *iter;
4724 int ix;
4725 struct cleanup *cleanup;
4726
4727 ui_out_text (current_uiout, _(" Inferior loaded "));
4728 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4729 "added");
4730 for (ix = 0;
4731 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4732 ix, iter);
4733 ++ix)
4734 {
4735 if (ix > 0)
4736 ui_out_text (current_uiout, " ");
4737 ui_out_field_string (current_uiout, "library", iter->so_name);
4738 ui_out_text (current_uiout, "\n");
4739 }
4740
4741 do_cleanups (cleanup);
4742 }
4743}
4744
e514a9d6
JM
4745/* Print a message indicating what happened. This is called from
4746 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4747 list - a list of the eventpoints that caused this stop. KIND is
4748 the target_waitkind for the stopping event. This
e514a9d6
JM
4749 routine calls the generic print routine for printing a message
4750 about reasons for stopping. This will print (for example) the
4751 "Breakpoint n," part of the output. The return value of this
4752 routine is one of:
c906108c 4753
4a64f543 4754 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4755 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4756 code to print the location. An example is
c5aa993b
JM
4757 "Breakpoint 1, " which should be followed by
4758 the location.
917317f4 4759 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4760 to also print the location part of the message.
4761 An example is the catch/throw messages, which
4a64f543 4762 don't require a location appended to the end.
917317f4 4763 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4764 further info to be printed. */
c906108c 4765
917317f4 4766enum print_stop_action
36dfb11c 4767bpstat_print (bpstat bs, int kind)
c906108c
SS
4768{
4769 int val;
c5aa993b 4770
c906108c 4771 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4772 (Currently all watchpoints go on the bpstat whether hit or not.
4773 That probably could (should) be changed, provided care is taken
c906108c 4774 with respect to bpstat_explains_signal). */
e514a9d6
JM
4775 for (; bs; bs = bs->next)
4776 {
4777 val = print_bp_stop_message (bs);
4778 if (val == PRINT_SRC_ONLY
4779 || val == PRINT_SRC_AND_LOC
4780 || val == PRINT_NOTHING)
4781 return val;
4782 }
c906108c 4783
36dfb11c
TT
4784 /* If we had hit a shared library event breakpoint,
4785 print_bp_stop_message would print out this message. If we hit an
4786 OS-level shared library event, do the same thing. */
4787 if (kind == TARGET_WAITKIND_LOADED)
4788 {
edcc5120 4789 print_solib_event (0);
36dfb11c
TT
4790 return PRINT_NOTHING;
4791 }
4792
e514a9d6 4793 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4794 with and nothing was printed. */
917317f4 4795 return PRINT_UNKNOWN;
c906108c
SS
4796}
4797
c42bd95a
DE
4798/* Evaluate the expression EXP and return 1 if value is zero.
4799 This returns the inverse of the condition because it is called
4800 from catch_errors which returns 0 if an exception happened, and if an
4801 exception happens we want execution to stop.
4a64f543 4802 The argument is a "struct expression *" that has been cast to a
c42bd95a 4803 "void *" to make it pass through catch_errors. */
c906108c
SS
4804
4805static int
4efb68b1 4806breakpoint_cond_eval (void *exp)
c906108c 4807{
278cd55f 4808 struct value *mark = value_mark ();
c5aa993b 4809 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4810
c906108c
SS
4811 value_free_to_mark (mark);
4812 return i;
4813}
4814
5760d0ab 4815/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4816
4817static bpstat
5760d0ab 4818bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4819{
4820 bpstat bs;
4821
4822 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4823 bs->next = NULL;
4824 **bs_link_pointer = bs;
4825 *bs_link_pointer = &bs->next;
f431efe5
PA
4826 bs->breakpoint_at = bl->owner;
4827 bs->bp_location_at = bl;
4828 incref_bp_location (bl);
c906108c
SS
4829 /* If the condition is false, etc., don't do the commands. */
4830 bs->commands = NULL;
4831 bs->old_val = NULL;
4832 bs->print_it = print_it_normal;
4833 return bs;
4834}
4835\f
d983da9c
DJ
4836/* The target has stopped with waitstatus WS. Check if any hardware
4837 watchpoints have triggered, according to the target. */
4838
4839int
4840watchpoints_triggered (struct target_waitstatus *ws)
4841{
d92524f1 4842 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4843 CORE_ADDR addr;
4844 struct breakpoint *b;
4845
4846 if (!stopped_by_watchpoint)
4847 {
4848 /* We were not stopped by a watchpoint. Mark all watchpoints
4849 as not triggered. */
4850 ALL_BREAKPOINTS (b)
cc60f2e3 4851 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4852 {
4853 struct watchpoint *w = (struct watchpoint *) b;
4854
4855 w->watchpoint_triggered = watch_triggered_no;
4856 }
d983da9c
DJ
4857
4858 return 0;
4859 }
4860
4861 if (!target_stopped_data_address (&current_target, &addr))
4862 {
4863 /* We were stopped by a watchpoint, but we don't know where.
4864 Mark all watchpoints as unknown. */
4865 ALL_BREAKPOINTS (b)
cc60f2e3 4866 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4867 {
4868 struct watchpoint *w = (struct watchpoint *) b;
4869
4870 w->watchpoint_triggered = watch_triggered_unknown;
4871 }
d983da9c 4872
3c4797ba 4873 return 1;
d983da9c
DJ
4874 }
4875
4876 /* The target could report the data address. Mark watchpoints
4877 affected by this data address as triggered, and all others as not
4878 triggered. */
4879
4880 ALL_BREAKPOINTS (b)
cc60f2e3 4881 if (is_hardware_watchpoint (b))
d983da9c 4882 {
3a5c3e22 4883 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4884 struct bp_location *loc;
d983da9c 4885
3a5c3e22 4886 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4887 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4888 {
3a5c3e22 4889 if (is_masked_watchpoint (b))
9c06b0b4 4890 {
3a5c3e22
PA
4891 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4892 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4893
4894 if (newaddr == start)
4895 {
3a5c3e22 4896 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4897 break;
4898 }
4899 }
4900 /* Exact match not required. Within range is sufficient. */
4901 else if (target_watchpoint_addr_within_range (&current_target,
4902 addr, loc->address,
4903 loc->length))
4904 {
3a5c3e22 4905 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4906 break;
4907 }
4908 }
d983da9c
DJ
4909 }
4910
4911 return 1;
4912}
4913
c906108c
SS
4914/* Possible return values for watchpoint_check (this can't be an enum
4915 because of check_errors). */
4916/* The watchpoint has been deleted. */
4917#define WP_DELETED 1
4918/* The value has changed. */
4919#define WP_VALUE_CHANGED 2
4920/* The value has not changed. */
4921#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4922/* Ignore this watchpoint, no matter if the value changed or not. */
4923#define WP_IGNORE 4
c906108c
SS
4924
4925#define BP_TEMPFLAG 1
4926#define BP_HARDWAREFLAG 2
4927
4a64f543
MS
4928/* Evaluate watchpoint condition expression and check if its value
4929 changed.
553e4c11
JB
4930
4931 P should be a pointer to struct bpstat, but is defined as a void *
4932 in order for this function to be usable with catch_errors. */
c906108c
SS
4933
4934static int
4efb68b1 4935watchpoint_check (void *p)
c906108c
SS
4936{
4937 bpstat bs = (bpstat) p;
3a5c3e22 4938 struct watchpoint *b;
c906108c
SS
4939 struct frame_info *fr;
4940 int within_current_scope;
4941
f431efe5 4942 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4943 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4944 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4945
f6bc2008
PA
4946 /* If this is a local watchpoint, we only want to check if the
4947 watchpoint frame is in scope if the current thread is the thread
4948 that was used to create the watchpoint. */
4949 if (!watchpoint_in_thread_scope (b))
60e1c644 4950 return WP_IGNORE;
f6bc2008 4951
c906108c
SS
4952 if (b->exp_valid_block == NULL)
4953 within_current_scope = 1;
4954 else
4955 {
edb3359d
DJ
4956 struct frame_info *frame = get_current_frame ();
4957 struct gdbarch *frame_arch = get_frame_arch (frame);
4958 CORE_ADDR frame_pc = get_frame_pc (frame);
4959
4a64f543
MS
4960 /* in_function_epilogue_p() returns a non-zero value if we're
4961 still in the function but the stack frame has already been
4962 invalidated. Since we can't rely on the values of local
4963 variables after the stack has been destroyed, we are treating
4964 the watchpoint in that state as `not changed' without further
4965 checking. Don't mark watchpoints as changed if the current
4966 frame is in an epilogue - even if they are in some other
4967 frame, our view of the stack is likely to be wrong and
4968 frame_find_by_id could error out. */
a0f49112 4969 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4970 return WP_IGNORE;
a0f49112 4971
101dcfbe 4972 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4973 within_current_scope = (fr != NULL);
69fbadd5
DJ
4974
4975 /* If we've gotten confused in the unwinder, we might have
4976 returned a frame that can't describe this variable. */
edb3359d
DJ
4977 if (within_current_scope)
4978 {
4979 struct symbol *function;
4980
4981 function = get_frame_function (fr);
4982 if (function == NULL
4983 || !contained_in (b->exp_valid_block,
4984 SYMBOL_BLOCK_VALUE (function)))
4985 within_current_scope = 0;
4986 }
69fbadd5 4987
edb3359d 4988 if (within_current_scope)
c906108c
SS
4989 /* If we end up stopping, the current frame will get selected
4990 in normal_stop. So this call to select_frame won't affect
4991 the user. */
0f7d239c 4992 select_frame (fr);
c906108c 4993 }
c5aa993b 4994
c906108c
SS
4995 if (within_current_scope)
4996 {
4a64f543
MS
4997 /* We use value_{,free_to_}mark because it could be a *long*
4998 time before we return to the command level and call
4999 free_all_values. We can't call free_all_values because we
5000 might be in the middle of evaluating a function call. */
c906108c 5001
0cf6dd15 5002 int pc = 0;
9c06b0b4 5003 struct value *mark;
fa4727a6
DJ
5004 struct value *new_val;
5005
3a5c3e22 5006 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5007 /* Since we don't know the exact trigger address (from
5008 stopped_data_address), just tell the user we've triggered
5009 a mask watchpoint. */
5010 return WP_VALUE_CHANGED;
5011
5012 mark = value_mark ();
3a1115a0 5013 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5014
4a64f543
MS
5015 /* We use value_equal_contents instead of value_equal because
5016 the latter coerces an array to a pointer, thus comparing just
5017 the address of the array instead of its contents. This is
5018 not what we want. */
fa4727a6 5019 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5020 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5021 {
fa4727a6
DJ
5022 if (new_val != NULL)
5023 {
5024 release_value (new_val);
5025 value_free_to_mark (mark);
5026 }
c906108c
SS
5027 bs->old_val = b->val;
5028 b->val = new_val;
fa4727a6 5029 b->val_valid = 1;
c906108c
SS
5030 return WP_VALUE_CHANGED;
5031 }
5032 else
5033 {
60e1c644 5034 /* Nothing changed. */
c906108c 5035 value_free_to_mark (mark);
c906108c
SS
5036 return WP_VALUE_NOT_CHANGED;
5037 }
5038 }
5039 else
5040 {
79a45e25
PA
5041 struct ui_out *uiout = current_uiout;
5042
c906108c 5043 /* This seems like the only logical thing to do because
c5aa993b
JM
5044 if we temporarily ignored the watchpoint, then when
5045 we reenter the block in which it is valid it contains
5046 garbage (in the case of a function, it may have two
5047 garbage values, one before and one after the prologue).
5048 So we can't even detect the first assignment to it and
5049 watch after that (since the garbage may or may not equal
5050 the first value assigned). */
348d480f
PA
5051 /* We print all the stop information in
5052 breakpoint_ops->print_it, but in this case, by the time we
5053 call breakpoint_ops->print_it this bp will be deleted
5054 already. So we have no choice but print the information
5055 here. */
9dc5e2a9 5056 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5057 ui_out_field_string
5058 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5059 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5060 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5061 ui_out_text (uiout,
5062 " deleted because the program has left the block in\n\
8b93c638 5063which its expression is valid.\n");
4ce44c66 5064
cdac0397 5065 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5066 decref_counted_command_line (&b->base.commands);
d0fb5eae 5067 watchpoint_del_at_next_stop (b);
c906108c
SS
5068
5069 return WP_DELETED;
5070 }
5071}
5072
18a18393 5073/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5074 breakpoint location BL. This function does not check if we should
5075 stop, only if BL explains the stop. */
5076
18a18393 5077static int
6c95b8df 5078bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5079 struct address_space *aspace, CORE_ADDR bp_addr,
5080 const struct target_waitstatus *ws)
18a18393
VP
5081{
5082 struct breakpoint *b = bl->owner;
5083
348d480f 5084 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5085 gdb_assert (b != NULL);
5086
09ac7c10 5087 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5088}
5089
3a5c3e22
PA
5090/* Determine if the watched values have actually changed, and we
5091 should stop. If not, set BS->stop to 0. */
5092
18a18393
VP
5093static void
5094bpstat_check_watchpoint (bpstat bs)
5095{
2bdf28a0 5096 const struct bp_location *bl;
3a5c3e22 5097 struct watchpoint *b;
2bdf28a0
JK
5098
5099 /* BS is built for existing struct breakpoint. */
f431efe5 5100 bl = bs->bp_location_at;
2bdf28a0 5101 gdb_assert (bl != NULL);
3a5c3e22 5102 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5103 gdb_assert (b != NULL);
18a18393 5104
18a18393 5105 {
18a18393
VP
5106 int must_check_value = 0;
5107
3a5c3e22 5108 if (b->base.type == bp_watchpoint)
18a18393
VP
5109 /* For a software watchpoint, we must always check the
5110 watched value. */
5111 must_check_value = 1;
5112 else if (b->watchpoint_triggered == watch_triggered_yes)
5113 /* We have a hardware watchpoint (read, write, or access)
5114 and the target earlier reported an address watched by
5115 this watchpoint. */
5116 must_check_value = 1;
5117 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5118 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5119 /* We were stopped by a hardware watchpoint, but the target could
5120 not report the data address. We must check the watchpoint's
5121 value. Access and read watchpoints are out of luck; without
5122 a data address, we can't figure it out. */
5123 must_check_value = 1;
3a5c3e22 5124
18a18393
VP
5125 if (must_check_value)
5126 {
3e43a32a
MS
5127 char *message
5128 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5129 b->base.number);
18a18393
VP
5130 struct cleanup *cleanups = make_cleanup (xfree, message);
5131 int e = catch_errors (watchpoint_check, bs, message,
5132 RETURN_MASK_ALL);
5133 do_cleanups (cleanups);
5134 switch (e)
5135 {
5136 case WP_DELETED:
5137 /* We've already printed what needs to be printed. */
5138 bs->print_it = print_it_done;
5139 /* Stop. */
5140 break;
60e1c644
PA
5141 case WP_IGNORE:
5142 bs->print_it = print_it_noop;
5143 bs->stop = 0;
5144 break;
18a18393 5145 case WP_VALUE_CHANGED:
3a5c3e22 5146 if (b->base.type == bp_read_watchpoint)
18a18393 5147 {
85d721b8
PA
5148 /* There are two cases to consider here:
5149
4a64f543 5150 1. We're watching the triggered memory for reads.
85d721b8
PA
5151 In that case, trust the target, and always report
5152 the watchpoint hit to the user. Even though
5153 reads don't cause value changes, the value may
5154 have changed since the last time it was read, and
5155 since we're not trapping writes, we will not see
5156 those, and as such we should ignore our notion of
5157 old value.
5158
4a64f543 5159 2. We're watching the triggered memory for both
85d721b8
PA
5160 reads and writes. There are two ways this may
5161 happen:
5162
4a64f543 5163 2.1. This is a target that can't break on data
85d721b8
PA
5164 reads only, but can break on accesses (reads or
5165 writes), such as e.g., x86. We detect this case
5166 at the time we try to insert read watchpoints.
5167
4a64f543 5168 2.2. Otherwise, the target supports read
85d721b8
PA
5169 watchpoints, but, the user set an access or write
5170 watchpoint watching the same memory as this read
5171 watchpoint.
5172
5173 If we're watching memory writes as well as reads,
5174 ignore watchpoint hits when we find that the
5175 value hasn't changed, as reads don't cause
5176 changes. This still gives false positives when
5177 the program writes the same value to memory as
5178 what there was already in memory (we will confuse
5179 it for a read), but it's much better than
5180 nothing. */
5181
5182 int other_write_watchpoint = 0;
5183
5184 if (bl->watchpoint_type == hw_read)
5185 {
5186 struct breakpoint *other_b;
5187
5188 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5189 if (other_b->type == bp_hardware_watchpoint
5190 || other_b->type == bp_access_watchpoint)
85d721b8 5191 {
3a5c3e22
PA
5192 struct watchpoint *other_w =
5193 (struct watchpoint *) other_b;
5194
5195 if (other_w->watchpoint_triggered
5196 == watch_triggered_yes)
5197 {
5198 other_write_watchpoint = 1;
5199 break;
5200 }
85d721b8
PA
5201 }
5202 }
5203
5204 if (other_write_watchpoint
5205 || bl->watchpoint_type == hw_access)
5206 {
5207 /* We're watching the same memory for writes,
5208 and the value changed since the last time we
5209 updated it, so this trap must be for a write.
5210 Ignore it. */
5211 bs->print_it = print_it_noop;
5212 bs->stop = 0;
5213 }
18a18393
VP
5214 }
5215 break;
5216 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5217 if (b->base.type == bp_hardware_watchpoint
5218 || b->base.type == bp_watchpoint)
18a18393
VP
5219 {
5220 /* Don't stop: write watchpoints shouldn't fire if
5221 the value hasn't changed. */
5222 bs->print_it = print_it_noop;
5223 bs->stop = 0;
5224 }
5225 /* Stop. */
5226 break;
5227 default:
5228 /* Can't happen. */
5229 case 0:
5230 /* Error from catch_errors. */
3a5c3e22 5231 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5232 watchpoint_del_at_next_stop (b);
18a18393
VP
5233 /* We've already printed what needs to be printed. */
5234 bs->print_it = print_it_done;
5235 break;
5236 }
5237 }
5238 else /* must_check_value == 0 */
5239 {
5240 /* This is a case where some watchpoint(s) triggered, but
5241 not at the address of this watchpoint, or else no
5242 watchpoint triggered after all. So don't print
5243 anything for this watchpoint. */
5244 bs->print_it = print_it_noop;
5245 bs->stop = 0;
5246 }
5247 }
5248}
5249
7d4df6a4
DE
5250/* For breakpoints that are currently marked as telling gdb to stop,
5251 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5252 of breakpoint referred to by BS. If we should not stop for this
5253 breakpoint, set BS->stop to 0. */
f431efe5 5254
18a18393
VP
5255static void
5256bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5257{
2bdf28a0
JK
5258 const struct bp_location *bl;
5259 struct breakpoint *b;
7d4df6a4
DE
5260 int value_is_zero = 0;
5261 struct expression *cond;
5262
5263 gdb_assert (bs->stop);
2bdf28a0
JK
5264
5265 /* BS is built for existing struct breakpoint. */
f431efe5 5266 bl = bs->bp_location_at;
2bdf28a0 5267 gdb_assert (bl != NULL);
f431efe5 5268 b = bs->breakpoint_at;
2bdf28a0 5269 gdb_assert (b != NULL);
18a18393 5270
b775012e
LM
5271 /* Even if the target evaluated the condition on its end and notified GDB, we
5272 need to do so again since GDB does not know if we stopped due to a
5273 breakpoint or a single step breakpoint. */
5274
18a18393 5275 if (frame_id_p (b->frame_id)
edb3359d 5276 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5277 {
7d4df6a4
DE
5278 bs->stop = 0;
5279 return;
5280 }
60e1c644 5281
12ab52e9
PA
5282 /* If this is a thread/task-specific breakpoint, don't waste cpu
5283 evaluating the condition if this isn't the specified
5284 thread/task. */
5285 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5286 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5287
6c1b0f7b
DE
5288 {
5289 bs->stop = 0;
5290 return;
5291 }
5292
6dddc817
DE
5293 /* Evaluate extension language breakpoints that have a "stop" method
5294 implemented. */
5295 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5296
7d4df6a4
DE
5297 if (is_watchpoint (b))
5298 {
5299 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5300
7d4df6a4
DE
5301 cond = w->cond_exp;
5302 }
5303 else
5304 cond = bl->cond;
60e1c644 5305
7d4df6a4
DE
5306 if (cond && b->disposition != disp_del_at_next_stop)
5307 {
5308 int within_current_scope = 1;
5309 struct watchpoint * w;
60e1c644 5310
7d4df6a4
DE
5311 /* We use value_mark and value_free_to_mark because it could
5312 be a long time before we return to the command level and
5313 call free_all_values. We can't call free_all_values
5314 because we might be in the middle of evaluating a
5315 function call. */
5316 struct value *mark = value_mark ();
5317
5318 if (is_watchpoint (b))
5319 w = (struct watchpoint *) b;
5320 else
5321 w = NULL;
5322
5323 /* Need to select the frame, with all that implies so that
5324 the conditions will have the right context. Because we
5325 use the frame, we will not see an inlined function's
5326 variables when we arrive at a breakpoint at the start
5327 of the inlined function; the current frame will be the
5328 call site. */
5329 if (w == NULL || w->cond_exp_valid_block == NULL)
5330 select_frame (get_current_frame ());
5331 else
18a18393 5332 {
7d4df6a4
DE
5333 struct frame_info *frame;
5334
5335 /* For local watchpoint expressions, which particular
5336 instance of a local is being watched matters, so we
5337 keep track of the frame to evaluate the expression
5338 in. To evaluate the condition however, it doesn't
5339 really matter which instantiation of the function
5340 where the condition makes sense triggers the
5341 watchpoint. This allows an expression like "watch
5342 global if q > 10" set in `func', catch writes to
5343 global on all threads that call `func', or catch
5344 writes on all recursive calls of `func' by a single
5345 thread. We simply always evaluate the condition in
5346 the innermost frame that's executing where it makes
5347 sense to evaluate the condition. It seems
5348 intuitive. */
5349 frame = block_innermost_frame (w->cond_exp_valid_block);
5350 if (frame != NULL)
5351 select_frame (frame);
5352 else
5353 within_current_scope = 0;
18a18393 5354 }
7d4df6a4
DE
5355 if (within_current_scope)
5356 value_is_zero
5357 = catch_errors (breakpoint_cond_eval, cond,
5358 "Error in testing breakpoint condition:\n",
5359 RETURN_MASK_ALL);
5360 else
18a18393 5361 {
7d4df6a4
DE
5362 warning (_("Watchpoint condition cannot be tested "
5363 "in the current scope"));
5364 /* If we failed to set the right context for this
5365 watchpoint, unconditionally report it. */
5366 value_is_zero = 0;
18a18393 5367 }
7d4df6a4
DE
5368 /* FIXME-someday, should give breakpoint #. */
5369 value_free_to_mark (mark);
18a18393 5370 }
7d4df6a4
DE
5371
5372 if (cond && value_is_zero)
5373 {
5374 bs->stop = 0;
5375 }
7d4df6a4
DE
5376 else if (b->ignore_count > 0)
5377 {
5378 b->ignore_count--;
5379 bs->stop = 0;
5380 /* Increase the hit count even though we don't stop. */
5381 ++(b->hit_count);
5382 observer_notify_breakpoint_modified (b);
5383 }
18a18393
VP
5384}
5385
5386
9709f61c 5387/* Get a bpstat associated with having just stopped at address
d983da9c 5388 BP_ADDR in thread PTID.
c906108c 5389
d983da9c 5390 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5391 don't understand this stop. Result is a chain of bpstat's such
5392 that:
c906108c 5393
c5aa993b 5394 if we don't understand the stop, the result is a null pointer.
c906108c 5395
c5aa993b 5396 if we understand why we stopped, the result is not null.
c906108c 5397
c5aa993b
JM
5398 Each element of the chain refers to a particular breakpoint or
5399 watchpoint at which we have stopped. (We may have stopped for
5400 several reasons concurrently.)
c906108c 5401
c5aa993b
JM
5402 Each element of the chain has valid next, breakpoint_at,
5403 commands, FIXME??? fields. */
c906108c
SS
5404
5405bpstat
6c95b8df 5406bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5407 CORE_ADDR bp_addr, ptid_t ptid,
5408 const struct target_waitstatus *ws)
c906108c 5409{
0d381245 5410 struct breakpoint *b = NULL;
afe38095 5411 struct bp_location *bl;
20874c92 5412 struct bp_location *loc;
5760d0ab
JK
5413 /* First item of allocated bpstat's. */
5414 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5415 /* Pointer to the last thing in the chain currently. */
5760d0ab 5416 bpstat bs;
20874c92 5417 int ix;
429374b8 5418 int need_remove_insert;
f431efe5 5419 int removed_any;
c906108c 5420
f431efe5
PA
5421 /* First, build the bpstat chain with locations that explain a
5422 target stop, while being careful to not set the target running,
5423 as that may invalidate locations (in particular watchpoint
5424 locations are recreated). Resuming will happen here with
5425 breakpoint conditions or watchpoint expressions that include
5426 inferior function calls. */
c5aa993b 5427
429374b8
JK
5428 ALL_BREAKPOINTS (b)
5429 {
5430 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5431 continue;
a5606eee 5432
429374b8
JK
5433 for (bl = b->loc; bl != NULL; bl = bl->next)
5434 {
4a64f543
MS
5435 /* For hardware watchpoints, we look only at the first
5436 location. The watchpoint_check function will work on the
5437 entire expression, not the individual locations. For
5438 read watchpoints, the watchpoints_triggered function has
5439 checked all locations already. */
429374b8
JK
5440 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5441 break;
18a18393 5442
f6592439 5443 if (!bl->enabled || bl->shlib_disabled)
429374b8 5444 continue;
c5aa993b 5445
09ac7c10 5446 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5447 continue;
c5aa993b 5448
4a64f543
MS
5449 /* Come here if it's a watchpoint, or if the break address
5450 matches. */
c5aa993b 5451
4a64f543
MS
5452 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5453 explain stop. */
c5aa993b 5454
f431efe5
PA
5455 /* Assume we stop. Should we find a watchpoint that is not
5456 actually triggered, or if the condition of the breakpoint
5457 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5458 bs->stop = 1;
5459 bs->print = 1;
d983da9c 5460
f431efe5
PA
5461 /* If this is a scope breakpoint, mark the associated
5462 watchpoint as triggered so that we will handle the
5463 out-of-scope event. We'll get to the watchpoint next
5464 iteration. */
d0fb5eae 5465 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5466 {
5467 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5468
5469 w->watchpoint_triggered = watch_triggered_yes;
5470 }
f431efe5
PA
5471 }
5472 }
5473
5474 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5475 {
f1310107 5476 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5477 {
5760d0ab 5478 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5479 /* For hits of moribund locations, we should just proceed. */
5480 bs->stop = 0;
5481 bs->print = 0;
5482 bs->print_it = print_it_noop;
5483 }
5484 }
5485
edcc5120
TT
5486 /* A bit of special processing for shlib breakpoints. We need to
5487 process solib loading here, so that the lists of loaded and
5488 unloaded libraries are correct before we handle "catch load" and
5489 "catch unload". */
5490 for (bs = bs_head; bs != NULL; bs = bs->next)
5491 {
5d268276 5492 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5493 {
5494 handle_solib_event ();
5495 break;
5496 }
5497 }
5498
f431efe5
PA
5499 /* Now go through the locations that caused the target to stop, and
5500 check whether we're interested in reporting this stop to higher
5501 layers, or whether we should resume the target transparently. */
5502
5503 removed_any = 0;
5504
5760d0ab 5505 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5506 {
5507 if (!bs->stop)
5508 continue;
5509
f431efe5 5510 b = bs->breakpoint_at;
348d480f
PA
5511 b->ops->check_status (bs);
5512 if (bs->stop)
28010a5d 5513 {
348d480f 5514 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5515
429374b8
JK
5516 if (bs->stop)
5517 {
5518 ++(b->hit_count);
8d3788bd 5519 observer_notify_breakpoint_modified (b);
c906108c 5520
4a64f543 5521 /* We will stop here. */
429374b8
JK
5522 if (b->disposition == disp_disable)
5523 {
816338b5
SS
5524 --(b->enable_count);
5525 if (b->enable_count <= 0
5526 && b->enable_state != bp_permanent)
429374b8 5527 b->enable_state = bp_disabled;
f431efe5 5528 removed_any = 1;
429374b8
JK
5529 }
5530 if (b->silent)
5531 bs->print = 0;
5532 bs->commands = b->commands;
9add0f1b 5533 incref_counted_command_line (bs->commands);
abf85f46
JK
5534 if (command_line_is_silent (bs->commands
5535 ? bs->commands->commands : NULL))
5536 bs->print = 0;
9d6e6e84
HZ
5537
5538 b->ops->after_condition_true (bs);
429374b8
JK
5539 }
5540
348d480f 5541 }
a9b3a50f
PA
5542
5543 /* Print nothing for this entry if we don't stop or don't
5544 print. */
5545 if (!bs->stop || !bs->print)
5546 bs->print_it = print_it_noop;
429374b8 5547 }
876fa593 5548
d983da9c
DJ
5549 /* If we aren't stopping, the value of some hardware watchpoint may
5550 not have changed, but the intermediate memory locations we are
5551 watching may have. Don't bother if we're stopping; this will get
5552 done later. */
d832cb68 5553 need_remove_insert = 0;
5760d0ab
JK
5554 if (! bpstat_causes_stop (bs_head))
5555 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5556 if (!bs->stop
f431efe5
PA
5557 && bs->breakpoint_at
5558 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5559 {
3a5c3e22
PA
5560 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5561
5562 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5563 need_remove_insert = 1;
d983da9c
DJ
5564 }
5565
d832cb68 5566 if (need_remove_insert)
2d134ed3 5567 update_global_location_list (1);
f431efe5
PA
5568 else if (removed_any)
5569 update_global_location_list (0);
d832cb68 5570
5760d0ab 5571 return bs_head;
c906108c 5572}
628fe4e4
JK
5573
5574static void
5575handle_jit_event (void)
5576{
5577 struct frame_info *frame;
5578 struct gdbarch *gdbarch;
5579
5580 /* Switch terminal for any messages produced by
5581 breakpoint_re_set. */
5582 target_terminal_ours_for_output ();
5583
5584 frame = get_current_frame ();
5585 gdbarch = get_frame_arch (frame);
5586
5587 jit_event_handler (gdbarch);
5588
5589 target_terminal_inferior ();
5590}
5591
5592/* Prepare WHAT final decision for infrun. */
5593
5594/* Decide what infrun needs to do with this bpstat. */
5595
c906108c 5596struct bpstat_what
0e30163f 5597bpstat_what (bpstat bs_head)
c906108c 5598{
c906108c 5599 struct bpstat_what retval;
628fe4e4 5600 int jit_event = 0;
0e30163f 5601 bpstat bs;
c906108c 5602
628fe4e4 5603 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5604 retval.call_dummy = STOP_NONE;
186c406b 5605 retval.is_longjmp = 0;
628fe4e4 5606
0e30163f 5607 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5608 {
628fe4e4
JK
5609 /* Extract this BS's action. After processing each BS, we check
5610 if its action overrides all we've seem so far. */
5611 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5612 enum bptype bptype;
5613
c906108c 5614 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5615 {
5616 /* I suspect this can happen if it was a momentary
5617 breakpoint which has since been deleted. */
5618 bptype = bp_none;
5619 }
20874c92 5620 else
f431efe5 5621 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5622
5623 switch (bptype)
c906108c
SS
5624 {
5625 case bp_none:
628fe4e4 5626 break;
c906108c
SS
5627 case bp_breakpoint:
5628 case bp_hardware_breakpoint:
5629 case bp_until:
5630 case bp_finish:
a9b3a50f 5631 case bp_shlib_event:
c906108c
SS
5632 if (bs->stop)
5633 {
5634 if (bs->print)
628fe4e4 5635 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5636 else
628fe4e4 5637 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5638 }
5639 else
628fe4e4 5640 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5641 break;
5642 case bp_watchpoint:
5643 case bp_hardware_watchpoint:
5644 case bp_read_watchpoint:
5645 case bp_access_watchpoint:
5646 if (bs->stop)
5647 {
5648 if (bs->print)
628fe4e4 5649 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5650 else
628fe4e4 5651 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5652 }
5653 else
628fe4e4
JK
5654 {
5655 /* There was a watchpoint, but we're not stopping.
5656 This requires no further action. */
5657 }
c906108c
SS
5658 break;
5659 case bp_longjmp:
e2e4d78b 5660 case bp_longjmp_call_dummy:
186c406b 5661 case bp_exception:
628fe4e4 5662 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5663 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5664 break;
5665 case bp_longjmp_resume:
186c406b 5666 case bp_exception_resume:
628fe4e4 5667 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5668 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5669 break;
5670 case bp_step_resume:
5671 if (bs->stop)
628fe4e4
JK
5672 this_action = BPSTAT_WHAT_STEP_RESUME;
5673 else
c906108c 5674 {
628fe4e4
JK
5675 /* It is for the wrong frame. */
5676 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5677 }
c906108c 5678 break;
2c03e5be
PA
5679 case bp_hp_step_resume:
5680 if (bs->stop)
5681 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5682 else
5683 {
5684 /* It is for the wrong frame. */
5685 this_action = BPSTAT_WHAT_SINGLE;
5686 }
5687 break;
c906108c 5688 case bp_watchpoint_scope:
c4093a6a 5689 case bp_thread_event:
1900040c 5690 case bp_overlay_event:
0fd8e87f 5691 case bp_longjmp_master:
aa7d318d 5692 case bp_std_terminate_master:
186c406b 5693 case bp_exception_master:
628fe4e4 5694 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5695 break;
ce78b96d 5696 case bp_catchpoint:
c5aa993b
JM
5697 if (bs->stop)
5698 {
5699 if (bs->print)
628fe4e4 5700 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5701 else
628fe4e4 5702 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5703 }
5704 else
628fe4e4
JK
5705 {
5706 /* There was a catchpoint, but we're not stopping.
5707 This requires no further action. */
5708 }
5709 break;
628fe4e4
JK
5710 case bp_jit_event:
5711 jit_event = 1;
5712 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5713 break;
c906108c 5714 case bp_call_dummy:
53a5351d
JM
5715 /* Make sure the action is stop (silent or noisy),
5716 so infrun.c pops the dummy frame. */
aa7d318d 5717 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5718 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5719 break;
5720 case bp_std_terminate:
5721 /* Make sure the action is stop (silent or noisy),
5722 so infrun.c pops the dummy frame. */
aa7d318d 5723 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5724 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5725 break;
1042e4c0 5726 case bp_tracepoint:
7a697b8d 5727 case bp_fast_tracepoint:
0fb4aa4b 5728 case bp_static_tracepoint:
1042e4c0
SS
5729 /* Tracepoint hits should not be reported back to GDB, and
5730 if one got through somehow, it should have been filtered
5731 out already. */
5732 internal_error (__FILE__, __LINE__,
7a697b8d 5733 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5734 break;
5735 case bp_gnu_ifunc_resolver:
5736 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5737 this_action = BPSTAT_WHAT_SINGLE;
5738 break;
5739 case bp_gnu_ifunc_resolver_return:
5740 /* The breakpoint will be removed, execution will restart from the
5741 PC of the former breakpoint. */
5742 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5743 break;
e7e0cddf
SS
5744
5745 case bp_dprintf:
a11cfd87
HZ
5746 if (bs->stop)
5747 this_action = BPSTAT_WHAT_STOP_SILENT;
5748 else
5749 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5750 break;
5751
628fe4e4
JK
5752 default:
5753 internal_error (__FILE__, __LINE__,
5754 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5755 }
628fe4e4
JK
5756
5757 retval.main_action = max (retval.main_action, this_action);
c906108c 5758 }
628fe4e4 5759
0e30163f
JK
5760 /* These operations may affect the bs->breakpoint_at state so they are
5761 delayed after MAIN_ACTION is decided above. */
5762
628fe4e4
JK
5763 if (jit_event)
5764 {
5765 if (debug_infrun)
5766 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5767
5768 handle_jit_event ();
5769 }
5770
0e30163f
JK
5771 for (bs = bs_head; bs != NULL; bs = bs->next)
5772 {
5773 struct breakpoint *b = bs->breakpoint_at;
5774
5775 if (b == NULL)
5776 continue;
5777 switch (b->type)
5778 {
5779 case bp_gnu_ifunc_resolver:
5780 gnu_ifunc_resolver_stop (b);
5781 break;
5782 case bp_gnu_ifunc_resolver_return:
5783 gnu_ifunc_resolver_return_stop (b);
5784 break;
5785 }
5786 }
5787
c906108c
SS
5788 return retval;
5789}
5790
5791/* Nonzero if we should step constantly (e.g. watchpoints on machines
5792 without hardware support). This isn't related to a specific bpstat,
5793 just to things like whether watchpoints are set. */
5794
c5aa993b 5795int
fba45db2 5796bpstat_should_step (void)
c906108c
SS
5797{
5798 struct breakpoint *b;
cc59ec59 5799
c906108c 5800 ALL_BREAKPOINTS (b)
717a8278 5801 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5802 return 1;
c906108c
SS
5803 return 0;
5804}
5805
67822962
PA
5806int
5807bpstat_causes_stop (bpstat bs)
5808{
5809 for (; bs != NULL; bs = bs->next)
5810 if (bs->stop)
5811 return 1;
5812
5813 return 0;
5814}
5815
c906108c 5816\f
c5aa993b 5817
170b53b2
UW
5818/* Compute a string of spaces suitable to indent the next line
5819 so it starts at the position corresponding to the table column
5820 named COL_NAME in the currently active table of UIOUT. */
5821
5822static char *
5823wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5824{
5825 static char wrap_indent[80];
5826 int i, total_width, width, align;
5827 char *text;
5828
5829 total_width = 0;
5830 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5831 {
5832 if (strcmp (text, col_name) == 0)
5833 {
5834 gdb_assert (total_width < sizeof wrap_indent);
5835 memset (wrap_indent, ' ', total_width);
5836 wrap_indent[total_width] = 0;
5837
5838 return wrap_indent;
5839 }
5840
5841 total_width += width + 1;
5842 }
5843
5844 return NULL;
5845}
5846
b775012e
LM
5847/* Determine if the locations of this breakpoint will have their conditions
5848 evaluated by the target, host or a mix of both. Returns the following:
5849
5850 "host": Host evals condition.
5851 "host or target": Host or Target evals condition.
5852 "target": Target evals condition.
5853*/
5854
5855static const char *
5856bp_condition_evaluator (struct breakpoint *b)
5857{
5858 struct bp_location *bl;
5859 char host_evals = 0;
5860 char target_evals = 0;
5861
5862 if (!b)
5863 return NULL;
5864
5865 if (!is_breakpoint (b))
5866 return NULL;
5867
5868 if (gdb_evaluates_breakpoint_condition_p ()
5869 || !target_supports_evaluation_of_breakpoint_conditions ())
5870 return condition_evaluation_host;
5871
5872 for (bl = b->loc; bl; bl = bl->next)
5873 {
5874 if (bl->cond_bytecode)
5875 target_evals++;
5876 else
5877 host_evals++;
5878 }
5879
5880 if (host_evals && target_evals)
5881 return condition_evaluation_both;
5882 else if (target_evals)
5883 return condition_evaluation_target;
5884 else
5885 return condition_evaluation_host;
5886}
5887
5888/* Determine the breakpoint location's condition evaluator. This is
5889 similar to bp_condition_evaluator, but for locations. */
5890
5891static const char *
5892bp_location_condition_evaluator (struct bp_location *bl)
5893{
5894 if (bl && !is_breakpoint (bl->owner))
5895 return NULL;
5896
5897 if (gdb_evaluates_breakpoint_condition_p ()
5898 || !target_supports_evaluation_of_breakpoint_conditions ())
5899 return condition_evaluation_host;
5900
5901 if (bl && bl->cond_bytecode)
5902 return condition_evaluation_target;
5903 else
5904 return condition_evaluation_host;
5905}
5906
859825b8
JK
5907/* Print the LOC location out of the list of B->LOC locations. */
5908
170b53b2
UW
5909static void
5910print_breakpoint_location (struct breakpoint *b,
5911 struct bp_location *loc)
0d381245 5912{
79a45e25 5913 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5914 struct cleanup *old_chain = save_current_program_space ();
5915
859825b8
JK
5916 if (loc != NULL && loc->shlib_disabled)
5917 loc = NULL;
5918
6c95b8df
PA
5919 if (loc != NULL)
5920 set_current_program_space (loc->pspace);
5921
56435ebe
TT
5922 if (b->display_canonical)
5923 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5924 else if (loc && loc->symtab)
0d381245
VP
5925 {
5926 struct symbol *sym
5927 = find_pc_sect_function (loc->address, loc->section);
5928 if (sym)
5929 {
5930 ui_out_text (uiout, "in ");
5931 ui_out_field_string (uiout, "func",
5932 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5933 ui_out_text (uiout, " ");
5934 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5935 ui_out_text (uiout, "at ");
0d381245 5936 }
05cba821
JK
5937 ui_out_field_string (uiout, "file",
5938 symtab_to_filename_for_display (loc->symtab));
0d381245 5939 ui_out_text (uiout, ":");
05cba821 5940
0d381245 5941 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5942 ui_out_field_string (uiout, "fullname",
5943 symtab_to_fullname (loc->symtab));
0d381245 5944
f8eba3c6 5945 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5946 }
859825b8 5947 else if (loc)
0d381245 5948 {
f99d8bf4
PA
5949 struct ui_file *stb = mem_fileopen ();
5950 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5951
f99d8bf4 5952 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5953 demangle, "");
0d381245 5954 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5955
5956 do_cleanups (stb_chain);
0d381245 5957 }
859825b8
JK
5958 else
5959 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5960
b775012e
LM
5961 if (loc && is_breakpoint (b)
5962 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5963 && bp_condition_evaluator (b) == condition_evaluation_both)
5964 {
5965 ui_out_text (uiout, " (");
5966 ui_out_field_string (uiout, "evaluated-by",
5967 bp_location_condition_evaluator (loc));
5968 ui_out_text (uiout, ")");
5969 }
5970
6c95b8df 5971 do_cleanups (old_chain);
0d381245
VP
5972}
5973
269b11a2
PA
5974static const char *
5975bptype_string (enum bptype type)
c906108c 5976{
c4093a6a
JM
5977 struct ep_type_description
5978 {
5979 enum bptype type;
5980 char *description;
5981 };
5982 static struct ep_type_description bptypes[] =
c906108c 5983 {
c5aa993b
JM
5984 {bp_none, "?deleted?"},
5985 {bp_breakpoint, "breakpoint"},
c906108c 5986 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5987 {bp_until, "until"},
5988 {bp_finish, "finish"},
5989 {bp_watchpoint, "watchpoint"},
c906108c 5990 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5991 {bp_read_watchpoint, "read watchpoint"},
5992 {bp_access_watchpoint, "acc watchpoint"},
5993 {bp_longjmp, "longjmp"},
5994 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5995 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5996 {bp_exception, "exception"},
5997 {bp_exception_resume, "exception resume"},
c5aa993b 5998 {bp_step_resume, "step resume"},
2c03e5be 5999 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6000 {bp_watchpoint_scope, "watchpoint scope"},
6001 {bp_call_dummy, "call dummy"},
aa7d318d 6002 {bp_std_terminate, "std::terminate"},
c5aa993b 6003 {bp_shlib_event, "shlib events"},
c4093a6a 6004 {bp_thread_event, "thread events"},
1900040c 6005 {bp_overlay_event, "overlay events"},
0fd8e87f 6006 {bp_longjmp_master, "longjmp master"},
aa7d318d 6007 {bp_std_terminate_master, "std::terminate master"},
186c406b 6008 {bp_exception_master, "exception master"},
ce78b96d 6009 {bp_catchpoint, "catchpoint"},
1042e4c0 6010 {bp_tracepoint, "tracepoint"},
7a697b8d 6011 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6012 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6013 {bp_dprintf, "dprintf"},
4efc6507 6014 {bp_jit_event, "jit events"},
0e30163f
JK
6015 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6016 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6017 };
269b11a2
PA
6018
6019 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6020 || ((int) type != bptypes[(int) type].type))
6021 internal_error (__FILE__, __LINE__,
6022 _("bptypes table does not describe type #%d."),
6023 (int) type);
6024
6025 return bptypes[(int) type].description;
6026}
6027
998580f1
MK
6028/* For MI, output a field named 'thread-groups' with a list as the value.
6029 For CLI, prefix the list with the string 'inf'. */
6030
6031static void
6032output_thread_groups (struct ui_out *uiout,
6033 const char *field_name,
6034 VEC(int) *inf_num,
6035 int mi_only)
6036{
752eb8b4 6037 struct cleanup *back_to;
998580f1
MK
6038 int is_mi = ui_out_is_mi_like_p (uiout);
6039 int inf;
6040 int i;
6041
6042 /* For backward compatibility, don't display inferiors in CLI unless
6043 there are several. Always display them for MI. */
6044 if (!is_mi && mi_only)
6045 return;
6046
752eb8b4
TT
6047 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6048
998580f1
MK
6049 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6050 {
6051 if (is_mi)
6052 {
6053 char mi_group[10];
6054
6055 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6056 ui_out_field_string (uiout, NULL, mi_group);
6057 }
6058 else
6059 {
6060 if (i == 0)
6061 ui_out_text (uiout, " inf ");
6062 else
6063 ui_out_text (uiout, ", ");
6064
6065 ui_out_text (uiout, plongest (inf));
6066 }
6067 }
6068
6069 do_cleanups (back_to);
6070}
6071
269b11a2
PA
6072/* Print B to gdb_stdout. */
6073
6074static void
6075print_one_breakpoint_location (struct breakpoint *b,
6076 struct bp_location *loc,
6077 int loc_number,
6078 struct bp_location **last_loc,
269b11a2
PA
6079 int allflag)
6080{
6081 struct command_line *l;
c2c6d25f 6082 static char bpenables[] = "nynny";
c906108c 6083
79a45e25 6084 struct ui_out *uiout = current_uiout;
0d381245
VP
6085 int header_of_multiple = 0;
6086 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6087 struct value_print_options opts;
6088
6089 get_user_print_options (&opts);
0d381245
VP
6090
6091 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6092 /* See comment in print_one_breakpoint concerning treatment of
6093 breakpoints with single disabled location. */
0d381245
VP
6094 if (loc == NULL
6095 && (b->loc != NULL
6096 && (b->loc->next != NULL || !b->loc->enabled)))
6097 header_of_multiple = 1;
6098 if (loc == NULL)
6099 loc = b->loc;
6100
c4093a6a
JM
6101 annotate_record ();
6102
6103 /* 1 */
6104 annotate_field (0);
0d381245
VP
6105 if (part_of_multiple)
6106 {
6107 char *formatted;
0c6773c1 6108 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6109 ui_out_field_string (uiout, "number", formatted);
6110 xfree (formatted);
6111 }
6112 else
6113 {
6114 ui_out_field_int (uiout, "number", b->number);
6115 }
c4093a6a
JM
6116
6117 /* 2 */
6118 annotate_field (1);
0d381245
VP
6119 if (part_of_multiple)
6120 ui_out_field_skip (uiout, "type");
269b11a2
PA
6121 else
6122 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6123
6124 /* 3 */
6125 annotate_field (2);
0d381245
VP
6126 if (part_of_multiple)
6127 ui_out_field_skip (uiout, "disp");
6128 else
2cec12e5 6129 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6130
c4093a6a
JM
6131
6132 /* 4 */
6133 annotate_field (3);
0d381245 6134 if (part_of_multiple)
54e52265 6135 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6136 else
4a64f543
MS
6137 ui_out_field_fmt (uiout, "enabled", "%c",
6138 bpenables[(int) b->enable_state]);
54e52265 6139 ui_out_spaces (uiout, 2);
0d381245 6140
c4093a6a
JM
6141
6142 /* 5 and 6 */
3086aeae 6143 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6144 {
4a64f543
MS
6145 /* Although the print_one can possibly print all locations,
6146 calling it here is not likely to get any nice result. So,
6147 make sure there's just one location. */
0d381245 6148 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6149 b->ops->print_one (b, last_loc);
0d381245 6150 }
3086aeae
DJ
6151 else
6152 switch (b->type)
6153 {
6154 case bp_none:
6155 internal_error (__FILE__, __LINE__,
e2e0b3e5 6156 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6157 break;
c906108c 6158
3086aeae
DJ
6159 case bp_watchpoint:
6160 case bp_hardware_watchpoint:
6161 case bp_read_watchpoint:
6162 case bp_access_watchpoint:
3a5c3e22
PA
6163 {
6164 struct watchpoint *w = (struct watchpoint *) b;
6165
6166 /* Field 4, the address, is omitted (which makes the columns
6167 not line up too nicely with the headers, but the effect
6168 is relatively readable). */
6169 if (opts.addressprint)
6170 ui_out_field_skip (uiout, "addr");
6171 annotate_field (5);
6172 ui_out_field_string (uiout, "what", w->exp_string);
6173 }
3086aeae
DJ
6174 break;
6175
3086aeae
DJ
6176 case bp_breakpoint:
6177 case bp_hardware_breakpoint:
6178 case bp_until:
6179 case bp_finish:
6180 case bp_longjmp:
6181 case bp_longjmp_resume:
e2e4d78b 6182 case bp_longjmp_call_dummy:
186c406b
TT
6183 case bp_exception:
6184 case bp_exception_resume:
3086aeae 6185 case bp_step_resume:
2c03e5be 6186 case bp_hp_step_resume:
3086aeae
DJ
6187 case bp_watchpoint_scope:
6188 case bp_call_dummy:
aa7d318d 6189 case bp_std_terminate:
3086aeae
DJ
6190 case bp_shlib_event:
6191 case bp_thread_event:
6192 case bp_overlay_event:
0fd8e87f 6193 case bp_longjmp_master:
aa7d318d 6194 case bp_std_terminate_master:
186c406b 6195 case bp_exception_master:
1042e4c0 6196 case bp_tracepoint:
7a697b8d 6197 case bp_fast_tracepoint:
0fb4aa4b 6198 case bp_static_tracepoint:
e7e0cddf 6199 case bp_dprintf:
4efc6507 6200 case bp_jit_event:
0e30163f
JK
6201 case bp_gnu_ifunc_resolver:
6202 case bp_gnu_ifunc_resolver_return:
79a45b7d 6203 if (opts.addressprint)
3086aeae
DJ
6204 {
6205 annotate_field (4);
54e52265 6206 if (header_of_multiple)
0d381245 6207 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6208 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6209 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6210 else
5af949e3
UW
6211 ui_out_field_core_addr (uiout, "addr",
6212 loc->gdbarch, loc->address);
3086aeae
DJ
6213 }
6214 annotate_field (5);
0d381245 6215 if (!header_of_multiple)
170b53b2 6216 print_breakpoint_location (b, loc);
0d381245 6217 if (b->loc)
a6d9a66e 6218 *last_loc = b->loc;
3086aeae
DJ
6219 break;
6220 }
c906108c 6221
6c95b8df 6222
998580f1 6223 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6224 {
6225 struct inferior *inf;
998580f1
MK
6226 VEC(int) *inf_num = NULL;
6227 int mi_only = 1;
6c95b8df 6228
998580f1 6229 ALL_INFERIORS (inf)
6c95b8df
PA
6230 {
6231 if (inf->pspace == loc->pspace)
998580f1 6232 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6233 }
998580f1
MK
6234
6235 /* For backward compatibility, don't display inferiors in CLI unless
6236 there are several. Always display for MI. */
6237 if (allflag
6238 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6239 && (number_of_program_spaces () > 1
6240 || number_of_inferiors () > 1)
6241 /* LOC is for existing B, it cannot be in
6242 moribund_locations and thus having NULL OWNER. */
6243 && loc->owner->type != bp_catchpoint))
6244 mi_only = 0;
6245 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6246 VEC_free (int, inf_num);
6c95b8df
PA
6247 }
6248
4a306c9a 6249 if (!part_of_multiple)
c4093a6a 6250 {
4a306c9a
JB
6251 if (b->thread != -1)
6252 {
6253 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6254 "stop only in" line a little further down. */
4a306c9a
JB
6255 ui_out_text (uiout, " thread ");
6256 ui_out_field_int (uiout, "thread", b->thread);
6257 }
6258 else if (b->task != 0)
6259 {
6260 ui_out_text (uiout, " task ");
6261 ui_out_field_int (uiout, "task", b->task);
6262 }
c4093a6a 6263 }
f1310107 6264
8b93c638 6265 ui_out_text (uiout, "\n");
f1310107 6266
348d480f 6267 if (!part_of_multiple)
f1310107
TJB
6268 b->ops->print_one_detail (b, uiout);
6269
0d381245 6270 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6271 {
6272 annotate_field (6);
8b93c638 6273 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6274 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6275 the frame ID. */
5af949e3
UW
6276 ui_out_field_core_addr (uiout, "frame",
6277 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6278 ui_out_text (uiout, "\n");
c4093a6a
JM
6279 }
6280
28010a5d 6281 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6282 {
6283 annotate_field (7);
d77f58be 6284 if (is_tracepoint (b))
1042e4c0
SS
6285 ui_out_text (uiout, "\ttrace only if ");
6286 else
6287 ui_out_text (uiout, "\tstop only if ");
0101ce28 6288 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6289
6290 /* Print whether the target is doing the breakpoint's condition
6291 evaluation. If GDB is doing the evaluation, don't print anything. */
6292 if (is_breakpoint (b)
6293 && breakpoint_condition_evaluation_mode ()
6294 == condition_evaluation_target)
6295 {
6296 ui_out_text (uiout, " (");
6297 ui_out_field_string (uiout, "evaluated-by",
6298 bp_condition_evaluator (b));
6299 ui_out_text (uiout, " evals)");
6300 }
0101ce28
JJ
6301 ui_out_text (uiout, "\n");
6302 }
6303
0d381245 6304 if (!part_of_multiple && b->thread != -1)
c4093a6a 6305 {
4a64f543 6306 /* FIXME should make an annotation for this. */
8b93c638
JM
6307 ui_out_text (uiout, "\tstop only in thread ");
6308 ui_out_field_int (uiout, "thread", b->thread);
6309 ui_out_text (uiout, "\n");
c4093a6a
JM
6310 }
6311
556ec64d
YQ
6312 if (!part_of_multiple)
6313 {
6314 if (b->hit_count)
31f56a27
YQ
6315 {
6316 /* FIXME should make an annotation for this. */
6317 if (is_catchpoint (b))
6318 ui_out_text (uiout, "\tcatchpoint");
6319 else if (is_tracepoint (b))
6320 ui_out_text (uiout, "\ttracepoint");
6321 else
6322 ui_out_text (uiout, "\tbreakpoint");
6323 ui_out_text (uiout, " already hit ");
6324 ui_out_field_int (uiout, "times", b->hit_count);
6325 if (b->hit_count == 1)
6326 ui_out_text (uiout, " time\n");
6327 else
6328 ui_out_text (uiout, " times\n");
6329 }
556ec64d
YQ
6330 else
6331 {
31f56a27
YQ
6332 /* Output the count also if it is zero, but only if this is mi. */
6333 if (ui_out_is_mi_like_p (uiout))
6334 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6335 }
6336 }
8b93c638 6337
0d381245 6338 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6339 {
6340 annotate_field (8);
8b93c638
JM
6341 ui_out_text (uiout, "\tignore next ");
6342 ui_out_field_int (uiout, "ignore", b->ignore_count);
6343 ui_out_text (uiout, " hits\n");
c4093a6a 6344 }
059fb39f 6345
816338b5
SS
6346 /* Note that an enable count of 1 corresponds to "enable once"
6347 behavior, which is reported by the combination of enablement and
6348 disposition, so we don't need to mention it here. */
6349 if (!part_of_multiple && b->enable_count > 1)
6350 {
6351 annotate_field (8);
6352 ui_out_text (uiout, "\tdisable after ");
6353 /* Tweak the wording to clarify that ignore and enable counts
6354 are distinct, and have additive effect. */
6355 if (b->ignore_count)
6356 ui_out_text (uiout, "additional ");
6357 else
6358 ui_out_text (uiout, "next ");
6359 ui_out_field_int (uiout, "enable", b->enable_count);
6360 ui_out_text (uiout, " hits\n");
6361 }
6362
f196051f
SS
6363 if (!part_of_multiple && is_tracepoint (b))
6364 {
6365 struct tracepoint *tp = (struct tracepoint *) b;
6366
6367 if (tp->traceframe_usage)
6368 {
6369 ui_out_text (uiout, "\ttrace buffer usage ");
6370 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6371 ui_out_text (uiout, " bytes\n");
6372 }
6373 }
d3ce09f5 6374
9add0f1b 6375 l = b->commands ? b->commands->commands : NULL;
059fb39f 6376 if (!part_of_multiple && l)
c4093a6a 6377 {
3b31d625
EZ
6378 struct cleanup *script_chain;
6379
c4093a6a 6380 annotate_field (9);
3b31d625 6381 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6382 print_command_lines (uiout, l, 4);
3b31d625 6383 do_cleanups (script_chain);
c4093a6a 6384 }
d24317b4 6385
d9b3f62e 6386 if (is_tracepoint (b))
1042e4c0 6387 {
d9b3f62e
PA
6388 struct tracepoint *t = (struct tracepoint *) b;
6389
6390 if (!part_of_multiple && t->pass_count)
6391 {
6392 annotate_field (10);
6393 ui_out_text (uiout, "\tpass count ");
6394 ui_out_field_int (uiout, "pass", t->pass_count);
6395 ui_out_text (uiout, " \n");
6396 }
f2a8bc8a
YQ
6397
6398 /* Don't display it when tracepoint or tracepoint location is
6399 pending. */
6400 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6401 {
6402 annotate_field (11);
6403
6404 if (ui_out_is_mi_like_p (uiout))
6405 ui_out_field_string (uiout, "installed",
6406 loc->inserted ? "y" : "n");
6407 else
6408 {
6409 if (loc->inserted)
6410 ui_out_text (uiout, "\t");
6411 else
6412 ui_out_text (uiout, "\tnot ");
6413 ui_out_text (uiout, "installed on target\n");
6414 }
6415 }
1042e4c0
SS
6416 }
6417
d24317b4
VP
6418 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6419 {
3a5c3e22
PA
6420 if (is_watchpoint (b))
6421 {
6422 struct watchpoint *w = (struct watchpoint *) b;
6423
6424 ui_out_field_string (uiout, "original-location", w->exp_string);
6425 }
6426 else if (b->addr_string)
d24317b4 6427 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6428 }
c4093a6a 6429}
c5aa993b 6430
0d381245
VP
6431static void
6432print_one_breakpoint (struct breakpoint *b,
4a64f543 6433 struct bp_location **last_loc,
6c95b8df 6434 int allflag)
0d381245 6435{
8d3788bd 6436 struct cleanup *bkpt_chain;
79a45e25 6437 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6438
6439 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6440
12c5a436 6441 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6442 do_cleanups (bkpt_chain);
0d381245
VP
6443
6444 /* If this breakpoint has custom print function,
6445 it's already printed. Otherwise, print individual
6446 locations, if any. */
6447 if (b->ops == NULL || b->ops->print_one == NULL)
6448 {
4a64f543
MS
6449 /* If breakpoint has a single location that is disabled, we
6450 print it as if it had several locations, since otherwise it's
6451 hard to represent "breakpoint enabled, location disabled"
6452 situation.
6453
6454 Note that while hardware watchpoints have several locations
a3be7890 6455 internally, that's not a property exposed to user. */
0d381245 6456 if (b->loc
a5606eee 6457 && !is_hardware_watchpoint (b)
8d3788bd 6458 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6459 {
6460 struct bp_location *loc;
6461 int n = 1;
8d3788bd 6462
0d381245 6463 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6464 {
6465 struct cleanup *inner2 =
6466 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6467 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6468 do_cleanups (inner2);
6469 }
0d381245
VP
6470 }
6471 }
6472}
6473
a6d9a66e
UW
6474static int
6475breakpoint_address_bits (struct breakpoint *b)
6476{
6477 int print_address_bits = 0;
6478 struct bp_location *loc;
6479
6480 for (loc = b->loc; loc; loc = loc->next)
6481 {
c7437ca6
PA
6482 int addr_bit;
6483
6484 /* Software watchpoints that aren't watching memory don't have
6485 an address to print. */
6486 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6487 continue;
6488
6489 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6490 if (addr_bit > print_address_bits)
6491 print_address_bits = addr_bit;
6492 }
6493
6494 return print_address_bits;
6495}
0d381245 6496
c4093a6a
JM
6497struct captured_breakpoint_query_args
6498 {
6499 int bnum;
6500 };
c5aa993b 6501
c4093a6a 6502static int
2b65245e 6503do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6504{
6505 struct captured_breakpoint_query_args *args = data;
52f0bd74 6506 struct breakpoint *b;
a6d9a66e 6507 struct bp_location *dummy_loc = NULL;
cc59ec59 6508
c4093a6a
JM
6509 ALL_BREAKPOINTS (b)
6510 {
6511 if (args->bnum == b->number)
c5aa993b 6512 {
12c5a436 6513 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6514 return GDB_RC_OK;
c5aa993b 6515 }
c4093a6a
JM
6516 }
6517 return GDB_RC_NONE;
6518}
c5aa993b 6519
c4093a6a 6520enum gdb_rc
4a64f543
MS
6521gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6522 char **error_message)
c4093a6a
JM
6523{
6524 struct captured_breakpoint_query_args args;
cc59ec59 6525
c4093a6a
JM
6526 args.bnum = bnum;
6527 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6528 an error. */
b0b13bb4
DJ
6529 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6530 error_message, RETURN_MASK_ALL) < 0)
6531 return GDB_RC_FAIL;
6532 else
6533 return GDB_RC_OK;
c4093a6a 6534}
c5aa993b 6535
09d682a4
TT
6536/* Return true if this breakpoint was set by the user, false if it is
6537 internal or momentary. */
6538
6539int
6540user_breakpoint_p (struct breakpoint *b)
6541{
46c6471b 6542 return b->number > 0;
09d682a4
TT
6543}
6544
7f3b0473 6545/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6546 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6547 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6548 FILTER is non-NULL, call it on each breakpoint and only include the
6549 ones for which it returns non-zero. Return the total number of
6550 breakpoints listed. */
c906108c 6551
d77f58be 6552static int
e5a67952 6553breakpoint_1 (char *args, int allflag,
4a64f543 6554 int (*filter) (const struct breakpoint *))
c4093a6a 6555{
52f0bd74 6556 struct breakpoint *b;
a6d9a66e 6557 struct bp_location *last_loc = NULL;
7f3b0473 6558 int nr_printable_breakpoints;
3b31d625 6559 struct cleanup *bkpttbl_chain;
79a45b7d 6560 struct value_print_options opts;
a6d9a66e 6561 int print_address_bits = 0;
269b11a2 6562 int print_type_col_width = 14;
79a45e25 6563 struct ui_out *uiout = current_uiout;
269b11a2 6564
79a45b7d
TT
6565 get_user_print_options (&opts);
6566
4a64f543
MS
6567 /* Compute the number of rows in the table, as well as the size
6568 required for address fields. */
7f3b0473
AC
6569 nr_printable_breakpoints = 0;
6570 ALL_BREAKPOINTS (b)
e5a67952
MS
6571 {
6572 /* If we have a filter, only list the breakpoints it accepts. */
6573 if (filter && !filter (b))
6574 continue;
6575
6576 /* If we have an "args" string, it is a list of breakpoints to
6577 accept. Skip the others. */
6578 if (args != NULL && *args != '\0')
6579 {
6580 if (allflag && parse_and_eval_long (args) != b->number)
6581 continue;
6582 if (!allflag && !number_is_in_list (args, b->number))
6583 continue;
6584 }
269b11a2 6585
e5a67952
MS
6586 if (allflag || user_breakpoint_p (b))
6587 {
6588 int addr_bit, type_len;
a6d9a66e 6589
e5a67952
MS
6590 addr_bit = breakpoint_address_bits (b);
6591 if (addr_bit > print_address_bits)
6592 print_address_bits = addr_bit;
269b11a2 6593
e5a67952
MS
6594 type_len = strlen (bptype_string (b->type));
6595 if (type_len > print_type_col_width)
6596 print_type_col_width = type_len;
6597
6598 nr_printable_breakpoints++;
6599 }
6600 }
7f3b0473 6601
79a45b7d 6602 if (opts.addressprint)
3b31d625 6603 bkpttbl_chain
3e43a32a
MS
6604 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6605 nr_printable_breakpoints,
3b31d625 6606 "BreakpointTable");
8b93c638 6607 else
3b31d625 6608 bkpttbl_chain
3e43a32a
MS
6609 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6610 nr_printable_breakpoints,
3b31d625 6611 "BreakpointTable");
8b93c638 6612
7f3b0473 6613 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6614 annotate_breakpoints_headers ();
6615 if (nr_printable_breakpoints > 0)
6616 annotate_field (0);
4a64f543 6617 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6618 if (nr_printable_breakpoints > 0)
6619 annotate_field (1);
269b11a2 6620 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6621 "type", "Type"); /* 2 */
d7faa9e7
AC
6622 if (nr_printable_breakpoints > 0)
6623 annotate_field (2);
4a64f543 6624 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6625 if (nr_printable_breakpoints > 0)
6626 annotate_field (3);
54e52265 6627 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6628 if (opts.addressprint)
e5a67952
MS
6629 {
6630 if (nr_printable_breakpoints > 0)
6631 annotate_field (4);
6632 if (print_address_bits <= 32)
6633 ui_out_table_header (uiout, 10, ui_left,
6634 "addr", "Address"); /* 5 */
6635 else
6636 ui_out_table_header (uiout, 18, ui_left,
6637 "addr", "Address"); /* 5 */
6638 }
d7faa9e7
AC
6639 if (nr_printable_breakpoints > 0)
6640 annotate_field (5);
6641 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6642 ui_out_table_body (uiout);
6643 if (nr_printable_breakpoints > 0)
6644 annotate_breakpoints_table ();
7f3b0473 6645
c4093a6a 6646 ALL_BREAKPOINTS (b)
e5a67952
MS
6647 {
6648 QUIT;
6649 /* If we have a filter, only list the breakpoints it accepts. */
6650 if (filter && !filter (b))
6651 continue;
6652
6653 /* If we have an "args" string, it is a list of breakpoints to
6654 accept. Skip the others. */
6655
6656 if (args != NULL && *args != '\0')
6657 {
6658 if (allflag) /* maintenance info breakpoint */
6659 {
6660 if (parse_and_eval_long (args) != b->number)
6661 continue;
6662 }
6663 else /* all others */
6664 {
6665 if (!number_is_in_list (args, b->number))
6666 continue;
6667 }
6668 }
6669 /* We only print out user settable breakpoints unless the
6670 allflag is set. */
6671 if (allflag || user_breakpoint_p (b))
12c5a436 6672 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6673 }
6674
3b31d625 6675 do_cleanups (bkpttbl_chain);
698384cd 6676
7f3b0473 6677 if (nr_printable_breakpoints == 0)
c906108c 6678 {
4a64f543
MS
6679 /* If there's a filter, let the caller decide how to report
6680 empty list. */
d77f58be
SS
6681 if (!filter)
6682 {
e5a67952 6683 if (args == NULL || *args == '\0')
d77f58be
SS
6684 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6685 else
4a64f543 6686 ui_out_message (uiout, 0,
e5a67952
MS
6687 "No breakpoint or watchpoint matching '%s'.\n",
6688 args);
d77f58be 6689 }
c906108c
SS
6690 }
6691 else
c4093a6a 6692 {
a6d9a66e
UW
6693 if (last_loc && !server_command)
6694 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6695 }
c906108c 6696
4a64f543 6697 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6698 there have been breakpoints? */
c906108c 6699 annotate_breakpoints_table_end ();
d77f58be
SS
6700
6701 return nr_printable_breakpoints;
c906108c
SS
6702}
6703
ad443146
SS
6704/* Display the value of default-collect in a way that is generally
6705 compatible with the breakpoint list. */
6706
6707static void
6708default_collect_info (void)
6709{
79a45e25
PA
6710 struct ui_out *uiout = current_uiout;
6711
ad443146
SS
6712 /* If it has no value (which is frequently the case), say nothing; a
6713 message like "No default-collect." gets in user's face when it's
6714 not wanted. */
6715 if (!*default_collect)
6716 return;
6717
6718 /* The following phrase lines up nicely with per-tracepoint collect
6719 actions. */
6720 ui_out_text (uiout, "default collect ");
6721 ui_out_field_string (uiout, "default-collect", default_collect);
6722 ui_out_text (uiout, " \n");
6723}
6724
c906108c 6725static void
e5a67952 6726breakpoints_info (char *args, int from_tty)
c906108c 6727{
e5a67952 6728 breakpoint_1 (args, 0, NULL);
ad443146
SS
6729
6730 default_collect_info ();
d77f58be
SS
6731}
6732
6733static void
e5a67952 6734watchpoints_info (char *args, int from_tty)
d77f58be 6735{
e5a67952 6736 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6737 struct ui_out *uiout = current_uiout;
d77f58be
SS
6738
6739 if (num_printed == 0)
6740 {
e5a67952 6741 if (args == NULL || *args == '\0')
d77f58be
SS
6742 ui_out_message (uiout, 0, "No watchpoints.\n");
6743 else
e5a67952 6744 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6745 }
c906108c
SS
6746}
6747
7a292a7a 6748static void
e5a67952 6749maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6750{
e5a67952 6751 breakpoint_1 (args, 1, NULL);
ad443146
SS
6752
6753 default_collect_info ();
c906108c
SS
6754}
6755
0d381245 6756static int
714835d5 6757breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6758 struct program_space *pspace,
714835d5 6759 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6760{
6761 struct bp_location *bl = b->loc;
cc59ec59 6762
0d381245
VP
6763 for (; bl; bl = bl->next)
6764 {
6c95b8df
PA
6765 if (bl->pspace == pspace
6766 && bl->address == pc
0d381245
VP
6767 && (!overlay_debugging || bl->section == section))
6768 return 1;
6769 }
6770 return 0;
6771}
6772
672f9b60 6773/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6774 concerns with logical breakpoints, so we match program spaces, not
6775 address spaces. */
c906108c
SS
6776
6777static void
6c95b8df
PA
6778describe_other_breakpoints (struct gdbarch *gdbarch,
6779 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6780 struct obj_section *section, int thread)
c906108c 6781{
52f0bd74
AC
6782 int others = 0;
6783 struct breakpoint *b;
c906108c
SS
6784
6785 ALL_BREAKPOINTS (b)
672f9b60
KP
6786 others += (user_breakpoint_p (b)
6787 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6788 if (others > 0)
6789 {
a3f17187
AC
6790 if (others == 1)
6791 printf_filtered (_("Note: breakpoint "));
6792 else /* if (others == ???) */
6793 printf_filtered (_("Note: breakpoints "));
c906108c 6794 ALL_BREAKPOINTS (b)
672f9b60 6795 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6796 {
6797 others--;
6798 printf_filtered ("%d", b->number);
6799 if (b->thread == -1 && thread != -1)
6800 printf_filtered (" (all threads)");
6801 else if (b->thread != -1)
6802 printf_filtered (" (thread %d)", b->thread);
6803 printf_filtered ("%s%s ",
059fb39f 6804 ((b->enable_state == bp_disabled
f8eba3c6 6805 || b->enable_state == bp_call_disabled)
0d381245
VP
6806 ? " (disabled)"
6807 : b->enable_state == bp_permanent
6808 ? " (permanent)"
6809 : ""),
6810 (others > 1) ? ","
6811 : ((others == 1) ? " and" : ""));
6812 }
a3f17187 6813 printf_filtered (_("also set at pc "));
5af949e3 6814 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6815 printf_filtered (".\n");
6816 }
6817}
6818\f
c906108c 6819
e4f237da
KB
6820/* Return true iff it is meaningful to use the address member of
6821 BPT. For some breakpoint types, the address member is irrelevant
6822 and it makes no sense to attempt to compare it to other addresses
6823 (or use it for any other purpose either).
6824
4a64f543
MS
6825 More specifically, each of the following breakpoint types will
6826 always have a zero valued address and we don't want to mark
6827 breakpoints of any of these types to be a duplicate of an actual
6828 breakpoint at address zero:
e4f237da
KB
6829
6830 bp_watchpoint
2d134ed3
PA
6831 bp_catchpoint
6832
6833*/
e4f237da
KB
6834
6835static int
6836breakpoint_address_is_meaningful (struct breakpoint *bpt)
6837{
6838 enum bptype type = bpt->type;
6839
2d134ed3
PA
6840 return (type != bp_watchpoint && type != bp_catchpoint);
6841}
6842
6843/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6844 true if LOC1 and LOC2 represent the same watchpoint location. */
6845
6846static int
4a64f543
MS
6847watchpoint_locations_match (struct bp_location *loc1,
6848 struct bp_location *loc2)
2d134ed3 6849{
3a5c3e22
PA
6850 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6851 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6852
6853 /* Both of them must exist. */
6854 gdb_assert (w1 != NULL);
6855 gdb_assert (w2 != NULL);
2bdf28a0 6856
4a64f543
MS
6857 /* If the target can evaluate the condition expression in hardware,
6858 then we we need to insert both watchpoints even if they are at
6859 the same place. Otherwise the watchpoint will only trigger when
6860 the condition of whichever watchpoint was inserted evaluates to
6861 true, not giving a chance for GDB to check the condition of the
6862 other watchpoint. */
3a5c3e22 6863 if ((w1->cond_exp
4a64f543
MS
6864 && target_can_accel_watchpoint_condition (loc1->address,
6865 loc1->length,
0cf6dd15 6866 loc1->watchpoint_type,
3a5c3e22
PA
6867 w1->cond_exp))
6868 || (w2->cond_exp
4a64f543
MS
6869 && target_can_accel_watchpoint_condition (loc2->address,
6870 loc2->length,
0cf6dd15 6871 loc2->watchpoint_type,
3a5c3e22 6872 w2->cond_exp)))
0cf6dd15
TJB
6873 return 0;
6874
85d721b8
PA
6875 /* Note that this checks the owner's type, not the location's. In
6876 case the target does not support read watchpoints, but does
6877 support access watchpoints, we'll have bp_read_watchpoint
6878 watchpoints with hw_access locations. Those should be considered
6879 duplicates of hw_read locations. The hw_read locations will
6880 become hw_access locations later. */
2d134ed3
PA
6881 return (loc1->owner->type == loc2->owner->type
6882 && loc1->pspace->aspace == loc2->pspace->aspace
6883 && loc1->address == loc2->address
6884 && loc1->length == loc2->length);
e4f237da
KB
6885}
6886
31e77af2 6887/* See breakpoint.h. */
6c95b8df 6888
31e77af2 6889int
6c95b8df
PA
6890breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6891 struct address_space *aspace2, CORE_ADDR addr2)
6892{
f5656ead 6893 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6894 || aspace1 == aspace2)
6895 && addr1 == addr2);
6896}
6897
f1310107
TJB
6898/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6899 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6900 matches ASPACE2. On targets that have global breakpoints, the address
6901 space doesn't really matter. */
6902
6903static int
6904breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6905 int len1, struct address_space *aspace2,
6906 CORE_ADDR addr2)
6907{
f5656ead 6908 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6909 || aspace1 == aspace2)
6910 && addr2 >= addr1 && addr2 < addr1 + len1);
6911}
6912
6913/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6914 a ranged breakpoint. In most targets, a match happens only if ASPACE
6915 matches the breakpoint's address space. On targets that have global
6916 breakpoints, the address space doesn't really matter. */
6917
6918static int
6919breakpoint_location_address_match (struct bp_location *bl,
6920 struct address_space *aspace,
6921 CORE_ADDR addr)
6922{
6923 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6924 aspace, addr)
6925 || (bl->length
6926 && breakpoint_address_match_range (bl->pspace->aspace,
6927 bl->address, bl->length,
6928 aspace, addr)));
6929}
6930
1e4d1764
YQ
6931/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6932 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6933 true, otherwise returns false. */
6934
6935static int
6936tracepoint_locations_match (struct bp_location *loc1,
6937 struct bp_location *loc2)
6938{
6939 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6940 /* Since tracepoint locations are never duplicated with others', tracepoint
6941 locations at the same address of different tracepoints are regarded as
6942 different locations. */
6943 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6944 else
6945 return 0;
6946}
6947
2d134ed3
PA
6948/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6949 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6950 represent the same location. */
6951
6952static int
4a64f543
MS
6953breakpoint_locations_match (struct bp_location *loc1,
6954 struct bp_location *loc2)
2d134ed3 6955{
2bdf28a0
JK
6956 int hw_point1, hw_point2;
6957
6958 /* Both of them must not be in moribund_locations. */
6959 gdb_assert (loc1->owner != NULL);
6960 gdb_assert (loc2->owner != NULL);
6961
6962 hw_point1 = is_hardware_watchpoint (loc1->owner);
6963 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6964
6965 if (hw_point1 != hw_point2)
6966 return 0;
6967 else if (hw_point1)
6968 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6969 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6970 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6971 else
f1310107
TJB
6972 /* We compare bp_location.length in order to cover ranged breakpoints. */
6973 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6974 loc2->pspace->aspace, loc2->address)
6975 && loc1->length == loc2->length);
2d134ed3
PA
6976}
6977
76897487
KB
6978static void
6979breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6980 int bnum, int have_bnum)
6981{
f63fbe86
MS
6982 /* The longest string possibly returned by hex_string_custom
6983 is 50 chars. These must be at least that big for safety. */
6984 char astr1[64];
6985 char astr2[64];
76897487 6986
bb599908
PH
6987 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6988 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6989 if (have_bnum)
8a3fe4f8 6990 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6991 bnum, astr1, astr2);
6992 else
8a3fe4f8 6993 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6994}
6995
4a64f543
MS
6996/* Adjust a breakpoint's address to account for architectural
6997 constraints on breakpoint placement. Return the adjusted address.
6998 Note: Very few targets require this kind of adjustment. For most
6999 targets, this function is simply the identity function. */
76897487
KB
7000
7001static CORE_ADDR
a6d9a66e
UW
7002adjust_breakpoint_address (struct gdbarch *gdbarch,
7003 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7004{
a6d9a66e 7005 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7006 {
7007 /* Very few targets need any kind of breakpoint adjustment. */
7008 return bpaddr;
7009 }
88f7da05
KB
7010 else if (bptype == bp_watchpoint
7011 || bptype == bp_hardware_watchpoint
7012 || bptype == bp_read_watchpoint
7013 || bptype == bp_access_watchpoint
fe798b75 7014 || bptype == bp_catchpoint)
88f7da05
KB
7015 {
7016 /* Watchpoints and the various bp_catch_* eventpoints should not
7017 have their addresses modified. */
7018 return bpaddr;
7019 }
76897487
KB
7020 else
7021 {
7022 CORE_ADDR adjusted_bpaddr;
7023
7024 /* Some targets have architectural constraints on the placement
7025 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7026 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7027
7028 /* An adjusted breakpoint address can significantly alter
7029 a user's expectations. Print a warning if an adjustment
7030 is required. */
7031 if (adjusted_bpaddr != bpaddr)
7032 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7033
7034 return adjusted_bpaddr;
7035 }
7036}
7037
28010a5d
PA
7038void
7039init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7040 struct breakpoint *owner)
7cc221ef 7041{
7cc221ef
DJ
7042 memset (loc, 0, sizeof (*loc));
7043
348d480f
PA
7044 gdb_assert (ops != NULL);
7045
28010a5d
PA
7046 loc->ops = ops;
7047 loc->owner = owner;
511a6cd4 7048 loc->cond = NULL;
b775012e 7049 loc->cond_bytecode = NULL;
0d381245
VP
7050 loc->shlib_disabled = 0;
7051 loc->enabled = 1;
e049a4b5 7052
28010a5d 7053 switch (owner->type)
e049a4b5
DJ
7054 {
7055 case bp_breakpoint:
7056 case bp_until:
7057 case bp_finish:
7058 case bp_longjmp:
7059 case bp_longjmp_resume:
e2e4d78b 7060 case bp_longjmp_call_dummy:
186c406b
TT
7061 case bp_exception:
7062 case bp_exception_resume:
e049a4b5 7063 case bp_step_resume:
2c03e5be 7064 case bp_hp_step_resume:
e049a4b5
DJ
7065 case bp_watchpoint_scope:
7066 case bp_call_dummy:
aa7d318d 7067 case bp_std_terminate:
e049a4b5
DJ
7068 case bp_shlib_event:
7069 case bp_thread_event:
7070 case bp_overlay_event:
4efc6507 7071 case bp_jit_event:
0fd8e87f 7072 case bp_longjmp_master:
aa7d318d 7073 case bp_std_terminate_master:
186c406b 7074 case bp_exception_master:
0e30163f
JK
7075 case bp_gnu_ifunc_resolver:
7076 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7077 case bp_dprintf:
e049a4b5 7078 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7079 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7080 break;
7081 case bp_hardware_breakpoint:
7082 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7083 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7084 break;
7085 case bp_hardware_watchpoint:
7086 case bp_read_watchpoint:
7087 case bp_access_watchpoint:
7088 loc->loc_type = bp_loc_hardware_watchpoint;
7089 break;
7090 case bp_watchpoint:
ce78b96d 7091 case bp_catchpoint:
15c3d785
PA
7092 case bp_tracepoint:
7093 case bp_fast_tracepoint:
0fb4aa4b 7094 case bp_static_tracepoint:
e049a4b5
DJ
7095 loc->loc_type = bp_loc_other;
7096 break;
7097 default:
e2e0b3e5 7098 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7099 }
7100
f431efe5 7101 loc->refc = 1;
28010a5d
PA
7102}
7103
7104/* Allocate a struct bp_location. */
7105
7106static struct bp_location *
7107allocate_bp_location (struct breakpoint *bpt)
7108{
348d480f
PA
7109 return bpt->ops->allocate_location (bpt);
7110}
7cc221ef 7111
f431efe5
PA
7112static void
7113free_bp_location (struct bp_location *loc)
fe3f5fa8 7114{
348d480f 7115 loc->ops->dtor (loc);
fe3f5fa8
VP
7116 xfree (loc);
7117}
7118
f431efe5
PA
7119/* Increment reference count. */
7120
7121static void
7122incref_bp_location (struct bp_location *bl)
7123{
7124 ++bl->refc;
7125}
7126
7127/* Decrement reference count. If the reference count reaches 0,
7128 destroy the bp_location. Sets *BLP to NULL. */
7129
7130static void
7131decref_bp_location (struct bp_location **blp)
7132{
0807b50c
PA
7133 gdb_assert ((*blp)->refc > 0);
7134
f431efe5
PA
7135 if (--(*blp)->refc == 0)
7136 free_bp_location (*blp);
7137 *blp = NULL;
7138}
7139
346774a9 7140/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7141
346774a9
PA
7142static void
7143add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7144{
346774a9 7145 struct breakpoint *b1;
c906108c 7146
346774a9
PA
7147 /* Add this breakpoint to the end of the chain so that a list of
7148 breakpoints will come out in order of increasing numbers. */
7149
7150 b1 = breakpoint_chain;
7151 if (b1 == 0)
7152 breakpoint_chain = b;
7153 else
7154 {
7155 while (b1->next)
7156 b1 = b1->next;
7157 b1->next = b;
7158 }
7159}
7160
7161/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7162
7163static void
7164init_raw_breakpoint_without_location (struct breakpoint *b,
7165 struct gdbarch *gdbarch,
28010a5d 7166 enum bptype bptype,
c0a91b2b 7167 const struct breakpoint_ops *ops)
346774a9 7168{
c906108c 7169 memset (b, 0, sizeof (*b));
2219d63c 7170
348d480f
PA
7171 gdb_assert (ops != NULL);
7172
28010a5d 7173 b->ops = ops;
4d28f7a8 7174 b->type = bptype;
a6d9a66e 7175 b->gdbarch = gdbarch;
c906108c
SS
7176 b->language = current_language->la_language;
7177 b->input_radix = input_radix;
7178 b->thread = -1;
b5de0fa7 7179 b->enable_state = bp_enabled;
c906108c
SS
7180 b->next = 0;
7181 b->silent = 0;
7182 b->ignore_count = 0;
7183 b->commands = NULL;
818dd999 7184 b->frame_id = null_frame_id;
0d381245 7185 b->condition_not_parsed = 0;
84f4c1fe 7186 b->py_bp_object = NULL;
d0fb5eae 7187 b->related_breakpoint = b;
346774a9
PA
7188}
7189
7190/* Helper to set_raw_breakpoint below. Creates a breakpoint
7191 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7192
7193static struct breakpoint *
7194set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7195 enum bptype bptype,
c0a91b2b 7196 const struct breakpoint_ops *ops)
346774a9
PA
7197{
7198 struct breakpoint *b = XNEW (struct breakpoint);
7199
348d480f 7200 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7201 add_to_breakpoint_chain (b);
0d381245
VP
7202 return b;
7203}
7204
0e30163f
JK
7205/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7206 resolutions should be made as the user specified the location explicitly
7207 enough. */
7208
0d381245 7209static void
0e30163f 7210set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7211{
2bdf28a0
JK
7212 gdb_assert (loc->owner != NULL);
7213
0d381245 7214 if (loc->owner->type == bp_breakpoint
1042e4c0 7215 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7216 || is_tracepoint (loc->owner))
0d381245 7217 {
0e30163f 7218 int is_gnu_ifunc;
2c02bd72 7219 const char *function_name;
6a3a010b 7220 CORE_ADDR func_addr;
0e30163f 7221
2c02bd72 7222 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7223 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7224
7225 if (is_gnu_ifunc && !explicit_loc)
7226 {
7227 struct breakpoint *b = loc->owner;
7228
7229 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7230 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7231 &loc->requested_address))
7232 {
7233 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7234 loc->address = adjust_breakpoint_address (loc->gdbarch,
7235 loc->requested_address,
7236 b->type);
7237 }
7238 else if (b->type == bp_breakpoint && b->loc == loc
7239 && loc->next == NULL && b->related_breakpoint == b)
7240 {
7241 /* Create only the whole new breakpoint of this type but do not
7242 mess more complicated breakpoints with multiple locations. */
7243 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7244 /* Remember the resolver's address for use by the return
7245 breakpoint. */
7246 loc->related_address = func_addr;
0e30163f
JK
7247 }
7248 }
7249
2c02bd72
DE
7250 if (function_name)
7251 loc->function_name = xstrdup (function_name);
0d381245
VP
7252 }
7253}
7254
a6d9a66e 7255/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7256struct gdbarch *
a6d9a66e
UW
7257get_sal_arch (struct symtab_and_line sal)
7258{
7259 if (sal.section)
7260 return get_objfile_arch (sal.section->objfile);
7261 if (sal.symtab)
7262 return get_objfile_arch (sal.symtab->objfile);
7263
7264 return NULL;
7265}
7266
346774a9
PA
7267/* Low level routine for partially initializing a breakpoint of type
7268 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7269 file name, and line number are provided by SAL.
0d381245
VP
7270
7271 It is expected that the caller will complete the initialization of
7272 the newly created breakpoint struct as well as output any status
c56053d2 7273 information regarding the creation of a new breakpoint. */
0d381245 7274
346774a9
PA
7275static void
7276init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7277 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7278 const struct breakpoint_ops *ops)
0d381245 7279{
28010a5d 7280 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7281
3742cc8b 7282 add_location_to_breakpoint (b, &sal);
0d381245 7283
6c95b8df
PA
7284 if (bptype != bp_catchpoint)
7285 gdb_assert (sal.pspace != NULL);
7286
f8eba3c6
TT
7287 /* Store the program space that was used to set the breakpoint,
7288 except for ordinary breakpoints, which are independent of the
7289 program space. */
7290 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7291 b->pspace = sal.pspace;
346774a9 7292}
c906108c 7293
346774a9
PA
7294/* set_raw_breakpoint is a low level routine for allocating and
7295 partially initializing a breakpoint of type BPTYPE. The newly
7296 created breakpoint's address, section, source file name, and line
7297 number are provided by SAL. The newly created and partially
7298 initialized breakpoint is added to the breakpoint chain and
7299 is also returned as the value of this function.
7300
7301 It is expected that the caller will complete the initialization of
7302 the newly created breakpoint struct as well as output any status
7303 information regarding the creation of a new breakpoint. In
7304 particular, set_raw_breakpoint does NOT set the breakpoint
7305 number! Care should be taken to not allow an error to occur
7306 prior to completing the initialization of the breakpoint. If this
7307 should happen, a bogus breakpoint will be left on the chain. */
7308
7309struct breakpoint *
7310set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7311 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7312 const struct breakpoint_ops *ops)
346774a9
PA
7313{
7314 struct breakpoint *b = XNEW (struct breakpoint);
7315
348d480f 7316 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7317 add_to_breakpoint_chain (b);
c906108c
SS
7318 return b;
7319}
7320
c2c6d25f
JM
7321
7322/* Note that the breakpoint object B describes a permanent breakpoint
7323 instruction, hard-wired into the inferior's code. */
7324void
7325make_breakpoint_permanent (struct breakpoint *b)
7326{
0d381245 7327 struct bp_location *bl;
cc59ec59 7328
b5de0fa7 7329 b->enable_state = bp_permanent;
c2c6d25f 7330
4a64f543
MS
7331 /* By definition, permanent breakpoints are already present in the
7332 code. Mark all locations as inserted. For now,
7333 make_breakpoint_permanent is called in just one place, so it's
7334 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7335 multiple locations or not, but it's easy to implement. */
0d381245
VP
7336 for (bl = b->loc; bl; bl = bl->next)
7337 bl->inserted = 1;
c2c6d25f
JM
7338}
7339
53a5351d 7340/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7341 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7342 initiated the operation. */
c906108c
SS
7343
7344void
186c406b 7345set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7346{
35df4500 7347 struct breakpoint *b, *b_tmp;
186c406b 7348 int thread = tp->num;
0fd8e87f
UW
7349
7350 /* To avoid having to rescan all objfile symbols at every step,
7351 we maintain a list of continually-inserted but always disabled
7352 longjmp "master" breakpoints. Here, we simply create momentary
7353 clones of those and enable them for the requested thread. */
35df4500 7354 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7355 if (b->pspace == current_program_space
186c406b
TT
7356 && (b->type == bp_longjmp_master
7357 || b->type == bp_exception_master))
0fd8e87f 7358 {
06edf0c0
PA
7359 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7360 struct breakpoint *clone;
cc59ec59 7361
e2e4d78b
JK
7362 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7363 after their removal. */
06edf0c0 7364 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7365 &longjmp_breakpoint_ops);
0fd8e87f
UW
7366 clone->thread = thread;
7367 }
186c406b
TT
7368
7369 tp->initiating_frame = frame;
c906108c
SS
7370}
7371
611c83ae 7372/* Delete all longjmp breakpoints from THREAD. */
c906108c 7373void
611c83ae 7374delete_longjmp_breakpoint (int thread)
c906108c 7375{
35df4500 7376 struct breakpoint *b, *b_tmp;
c906108c 7377
35df4500 7378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7379 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7380 {
7381 if (b->thread == thread)
7382 delete_breakpoint (b);
7383 }
c906108c
SS
7384}
7385
f59f708a
PA
7386void
7387delete_longjmp_breakpoint_at_next_stop (int thread)
7388{
7389 struct breakpoint *b, *b_tmp;
7390
7391 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7392 if (b->type == bp_longjmp || b->type == bp_exception)
7393 {
7394 if (b->thread == thread)
7395 b->disposition = disp_del_at_next_stop;
7396 }
7397}
7398
e2e4d78b
JK
7399/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7400 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7401 pointer to any of them. Return NULL if this system cannot place longjmp
7402 breakpoints. */
7403
7404struct breakpoint *
7405set_longjmp_breakpoint_for_call_dummy (void)
7406{
7407 struct breakpoint *b, *retval = NULL;
7408
7409 ALL_BREAKPOINTS (b)
7410 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7411 {
7412 struct breakpoint *new_b;
7413
7414 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7415 &momentary_breakpoint_ops);
7416 new_b->thread = pid_to_thread_id (inferior_ptid);
7417
7418 /* Link NEW_B into the chain of RETVAL breakpoints. */
7419
7420 gdb_assert (new_b->related_breakpoint == new_b);
7421 if (retval == NULL)
7422 retval = new_b;
7423 new_b->related_breakpoint = retval;
7424 while (retval->related_breakpoint != new_b->related_breakpoint)
7425 retval = retval->related_breakpoint;
7426 retval->related_breakpoint = new_b;
7427 }
7428
7429 return retval;
7430}
7431
7432/* Verify all existing dummy frames and their associated breakpoints for
7433 THREAD. Remove those which can no longer be found in the current frame
7434 stack.
7435
7436 You should call this function only at places where it is safe to currently
7437 unwind the whole stack. Failed stack unwind would discard live dummy
7438 frames. */
7439
7440void
7441check_longjmp_breakpoint_for_call_dummy (int thread)
7442{
7443 struct breakpoint *b, *b_tmp;
7444
7445 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7446 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7447 {
7448 struct breakpoint *dummy_b = b->related_breakpoint;
7449
7450 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7451 dummy_b = dummy_b->related_breakpoint;
7452 if (dummy_b->type != bp_call_dummy
7453 || frame_find_by_id (dummy_b->frame_id) != NULL)
7454 continue;
7455
7456 dummy_frame_discard (dummy_b->frame_id);
7457
7458 while (b->related_breakpoint != b)
7459 {
7460 if (b_tmp == b->related_breakpoint)
7461 b_tmp = b->related_breakpoint->next;
7462 delete_breakpoint (b->related_breakpoint);
7463 }
7464 delete_breakpoint (b);
7465 }
7466}
7467
1900040c
MS
7468void
7469enable_overlay_breakpoints (void)
7470{
52f0bd74 7471 struct breakpoint *b;
1900040c
MS
7472
7473 ALL_BREAKPOINTS (b)
7474 if (b->type == bp_overlay_event)
7475 {
7476 b->enable_state = bp_enabled;
b60e7edf 7477 update_global_location_list (1);
c02f5703 7478 overlay_events_enabled = 1;
1900040c
MS
7479 }
7480}
7481
7482void
7483disable_overlay_breakpoints (void)
7484{
52f0bd74 7485 struct breakpoint *b;
1900040c
MS
7486
7487 ALL_BREAKPOINTS (b)
7488 if (b->type == bp_overlay_event)
7489 {
7490 b->enable_state = bp_disabled;
b60e7edf 7491 update_global_location_list (0);
c02f5703 7492 overlay_events_enabled = 0;
1900040c
MS
7493 }
7494}
7495
aa7d318d
TT
7496/* Set an active std::terminate breakpoint for each std::terminate
7497 master breakpoint. */
7498void
7499set_std_terminate_breakpoint (void)
7500{
35df4500 7501 struct breakpoint *b, *b_tmp;
aa7d318d 7502
35df4500 7503 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7504 if (b->pspace == current_program_space
7505 && b->type == bp_std_terminate_master)
7506 {
06edf0c0
PA
7507 momentary_breakpoint_from_master (b, bp_std_terminate,
7508 &momentary_breakpoint_ops);
aa7d318d
TT
7509 }
7510}
7511
7512/* Delete all the std::terminate breakpoints. */
7513void
7514delete_std_terminate_breakpoint (void)
7515{
35df4500 7516 struct breakpoint *b, *b_tmp;
aa7d318d 7517
35df4500 7518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7519 if (b->type == bp_std_terminate)
7520 delete_breakpoint (b);
7521}
7522
c4093a6a 7523struct breakpoint *
a6d9a66e 7524create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7525{
7526 struct breakpoint *b;
c4093a6a 7527
06edf0c0
PA
7528 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7529 &internal_breakpoint_ops);
7530
b5de0fa7 7531 b->enable_state = bp_enabled;
c4093a6a 7532 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7533 b->addr_string
7534 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7535
b60e7edf 7536 update_global_location_list_nothrow (1);
74960c60 7537
c4093a6a
JM
7538 return b;
7539}
7540
7541void
7542remove_thread_event_breakpoints (void)
7543{
35df4500 7544 struct breakpoint *b, *b_tmp;
c4093a6a 7545
35df4500 7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7547 if (b->type == bp_thread_event
7548 && b->loc->pspace == current_program_space)
c4093a6a
JM
7549 delete_breakpoint (b);
7550}
7551
0101ce28
JJ
7552struct lang_and_radix
7553 {
7554 enum language lang;
7555 int radix;
7556 };
7557
4efc6507
DE
7558/* Create a breakpoint for JIT code registration and unregistration. */
7559
7560struct breakpoint *
7561create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7562{
7563 struct breakpoint *b;
7564
06edf0c0
PA
7565 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7566 &internal_breakpoint_ops);
4efc6507
DE
7567 update_global_location_list_nothrow (1);
7568 return b;
7569}
0101ce28 7570
03673fc7
PP
7571/* Remove JIT code registration and unregistration breakpoint(s). */
7572
7573void
7574remove_jit_event_breakpoints (void)
7575{
7576 struct breakpoint *b, *b_tmp;
7577
7578 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7579 if (b->type == bp_jit_event
7580 && b->loc->pspace == current_program_space)
7581 delete_breakpoint (b);
7582}
7583
cae688ec
JJ
7584void
7585remove_solib_event_breakpoints (void)
7586{
35df4500 7587 struct breakpoint *b, *b_tmp;
cae688ec 7588
35df4500 7589 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7590 if (b->type == bp_shlib_event
7591 && b->loc->pspace == current_program_space)
cae688ec
JJ
7592 delete_breakpoint (b);
7593}
7594
7595struct breakpoint *
a6d9a66e 7596create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7597{
7598 struct breakpoint *b;
7599
06edf0c0
PA
7600 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7601 &internal_breakpoint_ops);
b60e7edf 7602 update_global_location_list_nothrow (1);
cae688ec
JJ
7603 return b;
7604}
7605
7606/* Disable any breakpoints that are on code in shared libraries. Only
7607 apply to enabled breakpoints, disabled ones can just stay disabled. */
7608
7609void
cb851954 7610disable_breakpoints_in_shlibs (void)
cae688ec 7611{
876fa593 7612 struct bp_location *loc, **locp_tmp;
cae688ec 7613
876fa593 7614 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7615 {
2bdf28a0 7616 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7617 struct breakpoint *b = loc->owner;
2bdf28a0 7618
4a64f543
MS
7619 /* We apply the check to all breakpoints, including disabled for
7620 those with loc->duplicate set. This is so that when breakpoint
7621 becomes enabled, or the duplicate is removed, gdb will try to
7622 insert all breakpoints. If we don't set shlib_disabled here,
7623 we'll try to insert those breakpoints and fail. */
1042e4c0 7624 if (((b->type == bp_breakpoint)
508ccb1f 7625 || (b->type == bp_jit_event)
1042e4c0 7626 || (b->type == bp_hardware_breakpoint)
d77f58be 7627 || (is_tracepoint (b)))
6c95b8df 7628 && loc->pspace == current_program_space
0d381245 7629 && !loc->shlib_disabled
6c95b8df 7630 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7631 )
0d381245
VP
7632 {
7633 loc->shlib_disabled = 1;
7634 }
cae688ec
JJ
7635 }
7636}
7637
63644780
NB
7638/* Disable any breakpoints and tracepoints that are in SOLIB upon
7639 notification of unloaded_shlib. Only apply to enabled breakpoints,
7640 disabled ones can just stay disabled. */
84acb35a 7641
75149521 7642static void
84acb35a
JJ
7643disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7644{
876fa593 7645 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7646 int disabled_shlib_breaks = 0;
7647
c86cf029
VP
7648 /* SunOS a.out shared libraries are always mapped, so do not
7649 disable breakpoints; they will only be reported as unloaded
7650 through clear_solib when GDB discards its shared library
7651 list. See clear_solib for more information. */
7652 if (exec_bfd != NULL
7653 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7654 return;
7655
876fa593 7656 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7657 {
2bdf28a0 7658 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7659 struct breakpoint *b = loc->owner;
cc59ec59 7660
1e4d1764 7661 if (solib->pspace == loc->pspace
e2dd7057 7662 && !loc->shlib_disabled
1e4d1764
YQ
7663 && (((b->type == bp_breakpoint
7664 || b->type == bp_jit_event
7665 || b->type == bp_hardware_breakpoint)
7666 && (loc->loc_type == bp_loc_hardware_breakpoint
7667 || loc->loc_type == bp_loc_software_breakpoint))
7668 || is_tracepoint (b))
e2dd7057 7669 && solib_contains_address_p (solib, loc->address))
84acb35a 7670 {
e2dd7057
PP
7671 loc->shlib_disabled = 1;
7672 /* At this point, we cannot rely on remove_breakpoint
7673 succeeding so we must mark the breakpoint as not inserted
7674 to prevent future errors occurring in remove_breakpoints. */
7675 loc->inserted = 0;
8d3788bd
VP
7676
7677 /* This may cause duplicate notifications for the same breakpoint. */
7678 observer_notify_breakpoint_modified (b);
7679
e2dd7057
PP
7680 if (!disabled_shlib_breaks)
7681 {
7682 target_terminal_ours_for_output ();
3e43a32a
MS
7683 warning (_("Temporarily disabling breakpoints "
7684 "for unloaded shared library \"%s\""),
e2dd7057 7685 solib->so_name);
84acb35a 7686 }
e2dd7057 7687 disabled_shlib_breaks = 1;
84acb35a
JJ
7688 }
7689 }
84acb35a
JJ
7690}
7691
63644780
NB
7692/* Disable any breakpoints and tracepoints in OBJFILE upon
7693 notification of free_objfile. Only apply to enabled breakpoints,
7694 disabled ones can just stay disabled. */
7695
7696static void
7697disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7698{
7699 struct breakpoint *b;
7700
7701 if (objfile == NULL)
7702 return;
7703
08351840
PA
7704 /* OBJF_USERLOADED are dynamic modules manually managed by the user
7705 with add-symbol-file/remove-symbol-file. Similarly to how
7706 breakpoints in shared libraries are handled in response to
7707 "nosharedlibrary", mark breakpoints in OBJF_USERLOADED modules
7708 shlib_disabled so they end up uninserted on the next global
7709 location list update. Shared libraries not loaded by the user
7710 aren't handled here -- they're already handled in
7711 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7712 solib_unloaded observer. We skip objfiles that are not
7713 OBJF_USERLOADED (nor OBJF_SHARED) as those aren't considered
7714 dynamic objects (e.g. the main objfile). */
7715 if ((objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7716 return;
7717
7718 ALL_BREAKPOINTS (b)
7719 {
7720 struct bp_location *loc;
7721 int bp_modified = 0;
7722
7723 if (!is_breakpoint (b) && !is_tracepoint (b))
7724 continue;
7725
7726 for (loc = b->loc; loc != NULL; loc = loc->next)
7727 {
7728 CORE_ADDR loc_addr = loc->address;
7729
7730 if (loc->loc_type != bp_loc_hardware_breakpoint
7731 && loc->loc_type != bp_loc_software_breakpoint)
7732 continue;
7733
7734 if (loc->shlib_disabled != 0)
7735 continue;
7736
7737 if (objfile->pspace != loc->pspace)
7738 continue;
7739
7740 if (loc->loc_type != bp_loc_hardware_breakpoint
7741 && loc->loc_type != bp_loc_software_breakpoint)
7742 continue;
7743
7744 if (is_addr_in_objfile (loc_addr, objfile))
7745 {
7746 loc->shlib_disabled = 1;
08351840
PA
7747 /* At this point, we don't know whether the object was
7748 unmapped from the inferior or not, so leave the
7749 inserted flag alone. We'll handle failure to
7750 uninsert quietly, in case the object was indeed
7751 unmapped. */
63644780
NB
7752
7753 mark_breakpoint_location_modified (loc);
7754
7755 bp_modified = 1;
7756 }
7757 }
7758
7759 if (bp_modified)
7760 observer_notify_breakpoint_modified (b);
7761 }
7762}
7763
ce78b96d
JB
7764/* FORK & VFORK catchpoints. */
7765
e29a4733
PA
7766/* An instance of this type is used to represent a fork or vfork
7767 catchpoint. It includes a "struct breakpoint" as a kind of base
7768 class; users downcast to "struct breakpoint *" when needed. A
7769 breakpoint is really of this type iff its ops pointer points to
7770 CATCH_FORK_BREAKPOINT_OPS. */
7771
7772struct fork_catchpoint
7773{
7774 /* The base class. */
7775 struct breakpoint base;
7776
7777 /* Process id of a child process whose forking triggered this
7778 catchpoint. This field is only valid immediately after this
7779 catchpoint has triggered. */
7780 ptid_t forked_inferior_pid;
7781};
7782
4a64f543
MS
7783/* Implement the "insert" breakpoint_ops method for fork
7784 catchpoints. */
ce78b96d 7785
77b06cd7
TJB
7786static int
7787insert_catch_fork (struct bp_location *bl)
ce78b96d 7788{
dfd4cc63 7789 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7790}
7791
4a64f543
MS
7792/* Implement the "remove" breakpoint_ops method for fork
7793 catchpoints. */
ce78b96d
JB
7794
7795static int
77b06cd7 7796remove_catch_fork (struct bp_location *bl)
ce78b96d 7797{
dfd4cc63 7798 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7799}
7800
7801/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7802 catchpoints. */
7803
7804static int
f1310107 7805breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7806 struct address_space *aspace, CORE_ADDR bp_addr,
7807 const struct target_waitstatus *ws)
ce78b96d 7808{
e29a4733
PA
7809 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7810
f90263c1
TT
7811 if (ws->kind != TARGET_WAITKIND_FORKED)
7812 return 0;
7813
7814 c->forked_inferior_pid = ws->value.related_pid;
7815 return 1;
ce78b96d
JB
7816}
7817
4a64f543
MS
7818/* Implement the "print_it" breakpoint_ops method for fork
7819 catchpoints. */
ce78b96d
JB
7820
7821static enum print_stop_action
348d480f 7822print_it_catch_fork (bpstat bs)
ce78b96d 7823{
36dfb11c 7824 struct ui_out *uiout = current_uiout;
348d480f
PA
7825 struct breakpoint *b = bs->breakpoint_at;
7826 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7827
ce78b96d 7828 annotate_catchpoint (b->number);
36dfb11c
TT
7829 if (b->disposition == disp_del)
7830 ui_out_text (uiout, "\nTemporary catchpoint ");
7831 else
7832 ui_out_text (uiout, "\nCatchpoint ");
7833 if (ui_out_is_mi_like_p (uiout))
7834 {
7835 ui_out_field_string (uiout, "reason",
7836 async_reason_lookup (EXEC_ASYNC_FORK));
7837 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7838 }
7839 ui_out_field_int (uiout, "bkptno", b->number);
7840 ui_out_text (uiout, " (forked process ");
7841 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7842 ui_out_text (uiout, "), ");
ce78b96d
JB
7843 return PRINT_SRC_AND_LOC;
7844}
7845
4a64f543
MS
7846/* Implement the "print_one" breakpoint_ops method for fork
7847 catchpoints. */
ce78b96d
JB
7848
7849static void
a6d9a66e 7850print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7851{
e29a4733 7852 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7853 struct value_print_options opts;
79a45e25 7854 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7855
7856 get_user_print_options (&opts);
7857
4a64f543
MS
7858 /* Field 4, the address, is omitted (which makes the columns not
7859 line up too nicely with the headers, but the effect is relatively
7860 readable). */
79a45b7d 7861 if (opts.addressprint)
ce78b96d
JB
7862 ui_out_field_skip (uiout, "addr");
7863 annotate_field (5);
7864 ui_out_text (uiout, "fork");
e29a4733 7865 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7866 {
7867 ui_out_text (uiout, ", process ");
7868 ui_out_field_int (uiout, "what",
e29a4733 7869 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7870 ui_out_spaces (uiout, 1);
7871 }
8ac3646f
TT
7872
7873 if (ui_out_is_mi_like_p (uiout))
7874 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7875}
7876
7877/* Implement the "print_mention" breakpoint_ops method for fork
7878 catchpoints. */
7879
7880static void
7881print_mention_catch_fork (struct breakpoint *b)
7882{
7883 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7884}
7885
6149aea9
PA
7886/* Implement the "print_recreate" breakpoint_ops method for fork
7887 catchpoints. */
7888
7889static void
7890print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7891{
7892 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7893 print_recreate_thread (b, fp);
6149aea9
PA
7894}
7895
ce78b96d
JB
7896/* The breakpoint_ops structure to be used in fork catchpoints. */
7897
2060206e 7898static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7899
4a64f543
MS
7900/* Implement the "insert" breakpoint_ops method for vfork
7901 catchpoints. */
ce78b96d 7902
77b06cd7
TJB
7903static int
7904insert_catch_vfork (struct bp_location *bl)
ce78b96d 7905{
dfd4cc63 7906 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7907}
7908
4a64f543
MS
7909/* Implement the "remove" breakpoint_ops method for vfork
7910 catchpoints. */
ce78b96d
JB
7911
7912static int
77b06cd7 7913remove_catch_vfork (struct bp_location *bl)
ce78b96d 7914{
dfd4cc63 7915 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7916}
7917
7918/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7919 catchpoints. */
7920
7921static int
f1310107 7922breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7923 struct address_space *aspace, CORE_ADDR bp_addr,
7924 const struct target_waitstatus *ws)
ce78b96d 7925{
e29a4733
PA
7926 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7927
f90263c1
TT
7928 if (ws->kind != TARGET_WAITKIND_VFORKED)
7929 return 0;
7930
7931 c->forked_inferior_pid = ws->value.related_pid;
7932 return 1;
ce78b96d
JB
7933}
7934
4a64f543
MS
7935/* Implement the "print_it" breakpoint_ops method for vfork
7936 catchpoints. */
ce78b96d
JB
7937
7938static enum print_stop_action
348d480f 7939print_it_catch_vfork (bpstat bs)
ce78b96d 7940{
36dfb11c 7941 struct ui_out *uiout = current_uiout;
348d480f 7942 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7943 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7944
ce78b96d 7945 annotate_catchpoint (b->number);
36dfb11c
TT
7946 if (b->disposition == disp_del)
7947 ui_out_text (uiout, "\nTemporary catchpoint ");
7948 else
7949 ui_out_text (uiout, "\nCatchpoint ");
7950 if (ui_out_is_mi_like_p (uiout))
7951 {
7952 ui_out_field_string (uiout, "reason",
7953 async_reason_lookup (EXEC_ASYNC_VFORK));
7954 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7955 }
7956 ui_out_field_int (uiout, "bkptno", b->number);
7957 ui_out_text (uiout, " (vforked process ");
7958 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7959 ui_out_text (uiout, "), ");
ce78b96d
JB
7960 return PRINT_SRC_AND_LOC;
7961}
7962
4a64f543
MS
7963/* Implement the "print_one" breakpoint_ops method for vfork
7964 catchpoints. */
ce78b96d
JB
7965
7966static void
a6d9a66e 7967print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7968{
e29a4733 7969 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7970 struct value_print_options opts;
79a45e25 7971 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7972
7973 get_user_print_options (&opts);
4a64f543
MS
7974 /* Field 4, the address, is omitted (which makes the columns not
7975 line up too nicely with the headers, but the effect is relatively
7976 readable). */
79a45b7d 7977 if (opts.addressprint)
ce78b96d
JB
7978 ui_out_field_skip (uiout, "addr");
7979 annotate_field (5);
7980 ui_out_text (uiout, "vfork");
e29a4733 7981 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7982 {
7983 ui_out_text (uiout, ", process ");
7984 ui_out_field_int (uiout, "what",
e29a4733 7985 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7986 ui_out_spaces (uiout, 1);
7987 }
8ac3646f
TT
7988
7989 if (ui_out_is_mi_like_p (uiout))
7990 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
7991}
7992
7993/* Implement the "print_mention" breakpoint_ops method for vfork
7994 catchpoints. */
7995
7996static void
7997print_mention_catch_vfork (struct breakpoint *b)
7998{
7999 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8000}
8001
6149aea9
PA
8002/* Implement the "print_recreate" breakpoint_ops method for vfork
8003 catchpoints. */
8004
8005static void
8006print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8007{
8008 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8009 print_recreate_thread (b, fp);
6149aea9
PA
8010}
8011
ce78b96d
JB
8012/* The breakpoint_ops structure to be used in vfork catchpoints. */
8013
2060206e 8014static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8015
edcc5120
TT
8016/* An instance of this type is used to represent an solib catchpoint.
8017 It includes a "struct breakpoint" as a kind of base class; users
8018 downcast to "struct breakpoint *" when needed. A breakpoint is
8019 really of this type iff its ops pointer points to
8020 CATCH_SOLIB_BREAKPOINT_OPS. */
8021
8022struct solib_catchpoint
8023{
8024 /* The base class. */
8025 struct breakpoint base;
8026
8027 /* True for "catch load", false for "catch unload". */
8028 unsigned char is_load;
8029
8030 /* Regular expression to match, if any. COMPILED is only valid when
8031 REGEX is non-NULL. */
8032 char *regex;
8033 regex_t compiled;
8034};
8035
8036static void
8037dtor_catch_solib (struct breakpoint *b)
8038{
8039 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8040
8041 if (self->regex)
8042 regfree (&self->compiled);
8043 xfree (self->regex);
8044
8045 base_breakpoint_ops.dtor (b);
8046}
8047
8048static int
8049insert_catch_solib (struct bp_location *ignore)
8050{
8051 return 0;
8052}
8053
8054static int
8055remove_catch_solib (struct bp_location *ignore)
8056{
8057 return 0;
8058}
8059
8060static int
8061breakpoint_hit_catch_solib (const struct bp_location *bl,
8062 struct address_space *aspace,
8063 CORE_ADDR bp_addr,
8064 const struct target_waitstatus *ws)
8065{
8066 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8067 struct breakpoint *other;
8068
8069 if (ws->kind == TARGET_WAITKIND_LOADED)
8070 return 1;
8071
8072 ALL_BREAKPOINTS (other)
8073 {
8074 struct bp_location *other_bl;
8075
8076 if (other == bl->owner)
8077 continue;
8078
8079 if (other->type != bp_shlib_event)
8080 continue;
8081
8082 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8083 continue;
8084
8085 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8086 {
8087 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8088 return 1;
8089 }
8090 }
8091
8092 return 0;
8093}
8094
8095static void
8096check_status_catch_solib (struct bpstats *bs)
8097{
8098 struct solib_catchpoint *self
8099 = (struct solib_catchpoint *) bs->breakpoint_at;
8100 int ix;
8101
8102 if (self->is_load)
8103 {
8104 struct so_list *iter;
8105
8106 for (ix = 0;
8107 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8108 ix, iter);
8109 ++ix)
8110 {
8111 if (!self->regex
8112 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8113 return;
8114 }
8115 }
8116 else
8117 {
8118 char *iter;
8119
8120 for (ix = 0;
8121 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8122 ix, iter);
8123 ++ix)
8124 {
8125 if (!self->regex
8126 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8127 return;
8128 }
8129 }
8130
8131 bs->stop = 0;
8132 bs->print_it = print_it_noop;
8133}
8134
8135static enum print_stop_action
8136print_it_catch_solib (bpstat bs)
8137{
8138 struct breakpoint *b = bs->breakpoint_at;
8139 struct ui_out *uiout = current_uiout;
8140
8141 annotate_catchpoint (b->number);
8142 if (b->disposition == disp_del)
8143 ui_out_text (uiout, "\nTemporary catchpoint ");
8144 else
8145 ui_out_text (uiout, "\nCatchpoint ");
8146 ui_out_field_int (uiout, "bkptno", b->number);
8147 ui_out_text (uiout, "\n");
8148 if (ui_out_is_mi_like_p (uiout))
8149 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8150 print_solib_event (1);
8151 return PRINT_SRC_AND_LOC;
8152}
8153
8154static void
8155print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8156{
8157 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8158 struct value_print_options opts;
8159 struct ui_out *uiout = current_uiout;
8160 char *msg;
8161
8162 get_user_print_options (&opts);
8163 /* Field 4, the address, is omitted (which makes the columns not
8164 line up too nicely with the headers, but the effect is relatively
8165 readable). */
8166 if (opts.addressprint)
8167 {
8168 annotate_field (4);
8169 ui_out_field_skip (uiout, "addr");
8170 }
8171
8172 annotate_field (5);
8173 if (self->is_load)
8174 {
8175 if (self->regex)
8176 msg = xstrprintf (_("load of library matching %s"), self->regex);
8177 else
8178 msg = xstrdup (_("load of library"));
8179 }
8180 else
8181 {
8182 if (self->regex)
8183 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8184 else
8185 msg = xstrdup (_("unload of library"));
8186 }
8187 ui_out_field_string (uiout, "what", msg);
8188 xfree (msg);
8ac3646f
TT
8189
8190 if (ui_out_is_mi_like_p (uiout))
8191 ui_out_field_string (uiout, "catch-type",
8192 self->is_load ? "load" : "unload");
edcc5120
TT
8193}
8194
8195static void
8196print_mention_catch_solib (struct breakpoint *b)
8197{
8198 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8199
8200 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8201 self->is_load ? "load" : "unload");
8202}
8203
8204static void
8205print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8206{
8207 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8208
8209 fprintf_unfiltered (fp, "%s %s",
8210 b->disposition == disp_del ? "tcatch" : "catch",
8211 self->is_load ? "load" : "unload");
8212 if (self->regex)
8213 fprintf_unfiltered (fp, " %s", self->regex);
8214 fprintf_unfiltered (fp, "\n");
8215}
8216
8217static struct breakpoint_ops catch_solib_breakpoint_ops;
8218
91985142
MG
8219/* Shared helper function (MI and CLI) for creating and installing
8220 a shared object event catchpoint. If IS_LOAD is non-zero then
8221 the events to be caught are load events, otherwise they are
8222 unload events. If IS_TEMP is non-zero the catchpoint is a
8223 temporary one. If ENABLED is non-zero the catchpoint is
8224 created in an enabled state. */
edcc5120 8225
91985142
MG
8226void
8227add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8228{
8229 struct solib_catchpoint *c;
8230 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8231 struct cleanup *cleanup;
8232
edcc5120
TT
8233 if (!arg)
8234 arg = "";
8235 arg = skip_spaces (arg);
8236
8237 c = XCNEW (struct solib_catchpoint);
8238 cleanup = make_cleanup (xfree, c);
8239
8240 if (*arg != '\0')
8241 {
8242 int errcode;
8243
8244 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8245 if (errcode != 0)
8246 {
8247 char *err = get_regcomp_error (errcode, &c->compiled);
8248
8249 make_cleanup (xfree, err);
8250 error (_("Invalid regexp (%s): %s"), err, arg);
8251 }
8252 c->regex = xstrdup (arg);
8253 }
8254
8255 c->is_load = is_load;
91985142 8256 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8257 &catch_solib_breakpoint_ops);
8258
91985142
MG
8259 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8260
edcc5120
TT
8261 discard_cleanups (cleanup);
8262 install_breakpoint (0, &c->base, 1);
8263}
8264
91985142
MG
8265/* A helper function that does all the work for "catch load" and
8266 "catch unload". */
8267
8268static void
8269catch_load_or_unload (char *arg, int from_tty, int is_load,
8270 struct cmd_list_element *command)
8271{
8272 int tempflag;
8273 const int enabled = 1;
8274
8275 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8276
8277 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8278}
8279
edcc5120
TT
8280static void
8281catch_load_command_1 (char *arg, int from_tty,
8282 struct cmd_list_element *command)
8283{
8284 catch_load_or_unload (arg, from_tty, 1, command);
8285}
8286
8287static void
8288catch_unload_command_1 (char *arg, int from_tty,
8289 struct cmd_list_element *command)
8290{
8291 catch_load_or_unload (arg, from_tty, 0, command);
8292}
8293
be5c67c1
PA
8294/* An instance of this type is used to represent a syscall catchpoint.
8295 It includes a "struct breakpoint" as a kind of base class; users
8296 downcast to "struct breakpoint *" when needed. A breakpoint is
8297 really of this type iff its ops pointer points to
8298 CATCH_SYSCALL_BREAKPOINT_OPS. */
8299
8300struct syscall_catchpoint
8301{
8302 /* The base class. */
8303 struct breakpoint base;
8304
8305 /* Syscall numbers used for the 'catch syscall' feature. If no
8306 syscall has been specified for filtering, its value is NULL.
8307 Otherwise, it holds a list of all syscalls to be caught. The
8308 list elements are allocated with xmalloc. */
8309 VEC(int) *syscalls_to_be_caught;
8310};
8311
8312/* Implement the "dtor" breakpoint_ops method for syscall
8313 catchpoints. */
8314
8315static void
8316dtor_catch_syscall (struct breakpoint *b)
8317{
8318 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8319
8320 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8321
2060206e 8322 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8323}
8324
fa3064dd
YQ
8325static const struct inferior_data *catch_syscall_inferior_data = NULL;
8326
8327struct catch_syscall_inferior_data
8328{
8329 /* We keep a count of the number of times the user has requested a
8330 particular syscall to be tracked, and pass this information to the
8331 target. This lets capable targets implement filtering directly. */
8332
8333 /* Number of times that "any" syscall is requested. */
8334 int any_syscall_count;
8335
8336 /* Count of each system call. */
8337 VEC(int) *syscalls_counts;
8338
8339 /* This counts all syscall catch requests, so we can readily determine
8340 if any catching is necessary. */
8341 int total_syscalls_count;
8342};
8343
8344static struct catch_syscall_inferior_data*
8345get_catch_syscall_inferior_data (struct inferior *inf)
8346{
8347 struct catch_syscall_inferior_data *inf_data;
8348
8349 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8350 if (inf_data == NULL)
8351 {
41bf6aca 8352 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8353 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8354 }
8355
8356 return inf_data;
8357}
8358
8359static void
8360catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8361{
8362 xfree (arg);
8363}
8364
8365
a96d9b2e
SDJ
8366/* Implement the "insert" breakpoint_ops method for syscall
8367 catchpoints. */
8368
77b06cd7
TJB
8369static int
8370insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8371{
be5c67c1 8372 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8373 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8374 struct catch_syscall_inferior_data *inf_data
8375 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8376
fa3064dd 8377 ++inf_data->total_syscalls_count;
be5c67c1 8378 if (!c->syscalls_to_be_caught)
fa3064dd 8379 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8380 else
8381 {
8382 int i, iter;
cc59ec59 8383
a96d9b2e 8384 for (i = 0;
be5c67c1 8385 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8386 i++)
8387 {
8388 int elem;
cc59ec59 8389
fa3064dd 8390 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8391 {
fa3064dd 8392 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8393 uintptr_t vec_addr_offset
8394 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8395 uintptr_t vec_addr;
fa3064dd
YQ
8396 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8397 vec_addr = ((uintptr_t) VEC_address (int,
8398 inf_data->syscalls_counts)
8399 + vec_addr_offset);
a96d9b2e
SDJ
8400 memset ((void *) vec_addr, 0,
8401 (iter + 1 - old_size) * sizeof (int));
8402 }
fa3064dd
YQ
8403 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8404 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8405 }
8406 }
8407
dfd4cc63 8408 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8409 inf_data->total_syscalls_count != 0,
8410 inf_data->any_syscall_count,
8411 VEC_length (int,
8412 inf_data->syscalls_counts),
8413 VEC_address (int,
8414 inf_data->syscalls_counts));
a96d9b2e
SDJ
8415}
8416
8417/* Implement the "remove" breakpoint_ops method for syscall
8418 catchpoints. */
8419
8420static int
77b06cd7 8421remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8422{
be5c67c1 8423 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8424 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8425 struct catch_syscall_inferior_data *inf_data
8426 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8427
fa3064dd 8428 --inf_data->total_syscalls_count;
be5c67c1 8429 if (!c->syscalls_to_be_caught)
fa3064dd 8430 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8431 else
8432 {
8433 int i, iter;
cc59ec59 8434
a96d9b2e 8435 for (i = 0;
be5c67c1 8436 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8437 i++)
8438 {
8439 int elem;
fa3064dd 8440 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8441 /* Shouldn't happen. */
8442 continue;
fa3064dd
YQ
8443 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8444 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8445 }
8446 }
8447
dfd4cc63 8448 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8449 inf_data->total_syscalls_count != 0,
8450 inf_data->any_syscall_count,
8451 VEC_length (int,
8452 inf_data->syscalls_counts),
3e43a32a 8453 VEC_address (int,
fa3064dd 8454 inf_data->syscalls_counts));
a96d9b2e
SDJ
8455}
8456
8457/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8458 catchpoints. */
8459
8460static int
f1310107 8461breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8462 struct address_space *aspace, CORE_ADDR bp_addr,
8463 const struct target_waitstatus *ws)
a96d9b2e 8464{
4a64f543
MS
8465 /* We must check if we are catching specific syscalls in this
8466 breakpoint. If we are, then we must guarantee that the called
8467 syscall is the same syscall we are catching. */
a96d9b2e 8468 int syscall_number = 0;
be5c67c1
PA
8469 const struct syscall_catchpoint *c
8470 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8471
f90263c1
TT
8472 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8473 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8474 return 0;
8475
f90263c1
TT
8476 syscall_number = ws->value.syscall_number;
8477
a96d9b2e 8478 /* Now, checking if the syscall is the same. */
be5c67c1 8479 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8480 {
8481 int i, iter;
cc59ec59 8482
a96d9b2e 8483 for (i = 0;
be5c67c1 8484 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8485 i++)
8486 if (syscall_number == iter)
4924df79
GKB
8487 return 1;
8488
8489 return 0;
a96d9b2e
SDJ
8490 }
8491
8492 return 1;
8493}
8494
8495/* Implement the "print_it" breakpoint_ops method for syscall
8496 catchpoints. */
8497
8498static enum print_stop_action
348d480f 8499print_it_catch_syscall (bpstat bs)
a96d9b2e 8500{
36dfb11c 8501 struct ui_out *uiout = current_uiout;
348d480f 8502 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8503 /* These are needed because we want to know in which state a
8504 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8505 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8506 must print "called syscall" or "returned from syscall". */
8507 ptid_t ptid;
8508 struct target_waitstatus last;
8509 struct syscall s;
a96d9b2e
SDJ
8510
8511 get_last_target_status (&ptid, &last);
8512
8513 get_syscall_by_number (last.value.syscall_number, &s);
8514
8515 annotate_catchpoint (b->number);
8516
36dfb11c
TT
8517 if (b->disposition == disp_del)
8518 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8519 else
36dfb11c
TT
8520 ui_out_text (uiout, "\nCatchpoint ");
8521 if (ui_out_is_mi_like_p (uiout))
8522 {
8523 ui_out_field_string (uiout, "reason",
8524 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8525 ? EXEC_ASYNC_SYSCALL_ENTRY
8526 : EXEC_ASYNC_SYSCALL_RETURN));
8527 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8528 }
8529 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8530
8531 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8532 ui_out_text (uiout, " (call to syscall ");
8533 else
8534 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8535
36dfb11c
TT
8536 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8537 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8538 if (s.name != NULL)
8539 ui_out_field_string (uiout, "syscall-name", s.name);
8540
8541 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8542
8543 return PRINT_SRC_AND_LOC;
8544}
8545
8546/* Implement the "print_one" breakpoint_ops method for syscall
8547 catchpoints. */
8548
8549static void
8550print_one_catch_syscall (struct breakpoint *b,
f1310107 8551 struct bp_location **last_loc)
a96d9b2e 8552{
be5c67c1 8553 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8554 struct value_print_options opts;
79a45e25 8555 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8556
8557 get_user_print_options (&opts);
4a64f543
MS
8558 /* Field 4, the address, is omitted (which makes the columns not
8559 line up too nicely with the headers, but the effect is relatively
8560 readable). */
a96d9b2e
SDJ
8561 if (opts.addressprint)
8562 ui_out_field_skip (uiout, "addr");
8563 annotate_field (5);
8564
be5c67c1
PA
8565 if (c->syscalls_to_be_caught
8566 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8567 ui_out_text (uiout, "syscalls \"");
8568 else
8569 ui_out_text (uiout, "syscall \"");
8570
be5c67c1 8571 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8572 {
8573 int i, iter;
8574 char *text = xstrprintf ("%s", "");
cc59ec59 8575
a96d9b2e 8576 for (i = 0;
be5c67c1 8577 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8578 i++)
8579 {
8580 char *x = text;
8581 struct syscall s;
8582 get_syscall_by_number (iter, &s);
8583
8584 if (s.name != NULL)
8585 text = xstrprintf ("%s%s, ", text, s.name);
8586 else
8587 text = xstrprintf ("%s%d, ", text, iter);
8588
8589 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8590 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8591 on every call. */
8592 xfree (x);
8593 }
8594 /* Remove the last comma. */
8595 text[strlen (text) - 2] = '\0';
8596 ui_out_field_string (uiout, "what", text);
8597 }
8598 else
8599 ui_out_field_string (uiout, "what", "<any syscall>");
8600 ui_out_text (uiout, "\" ");
8ac3646f
TT
8601
8602 if (ui_out_is_mi_like_p (uiout))
8603 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8604}
8605
8606/* Implement the "print_mention" breakpoint_ops method for syscall
8607 catchpoints. */
8608
8609static void
8610print_mention_catch_syscall (struct breakpoint *b)
8611{
be5c67c1
PA
8612 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8613
8614 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8615 {
8616 int i, iter;
8617
be5c67c1 8618 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8619 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8620 else
8621 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8622
8623 for (i = 0;
be5c67c1 8624 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8625 i++)
8626 {
8627 struct syscall s;
8628 get_syscall_by_number (iter, &s);
8629
8630 if (s.name)
8631 printf_filtered (" '%s' [%d]", s.name, s.number);
8632 else
8633 printf_filtered (" %d", s.number);
8634 }
8635 printf_filtered (")");
8636 }
8637 else
8638 printf_filtered (_("Catchpoint %d (any syscall)"),
8639 b->number);
8640}
8641
6149aea9
PA
8642/* Implement the "print_recreate" breakpoint_ops method for syscall
8643 catchpoints. */
8644
8645static void
8646print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8647{
be5c67c1
PA
8648 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8649
6149aea9
PA
8650 fprintf_unfiltered (fp, "catch syscall");
8651
be5c67c1 8652 if (c->syscalls_to_be_caught)
6149aea9
PA
8653 {
8654 int i, iter;
8655
8656 for (i = 0;
be5c67c1 8657 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8658 i++)
8659 {
8660 struct syscall s;
8661
8662 get_syscall_by_number (iter, &s);
8663 if (s.name)
8664 fprintf_unfiltered (fp, " %s", s.name);
8665 else
8666 fprintf_unfiltered (fp, " %d", s.number);
8667 }
8668 }
d9b3f62e 8669 print_recreate_thread (b, fp);
6149aea9
PA
8670}
8671
a96d9b2e
SDJ
8672/* The breakpoint_ops structure to be used in syscall catchpoints. */
8673
2060206e 8674static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8675
8676/* Returns non-zero if 'b' is a syscall catchpoint. */
8677
8678static int
8679syscall_catchpoint_p (struct breakpoint *b)
8680{
8681 return (b->ops == &catch_syscall_breakpoint_ops);
8682}
8683
346774a9
PA
8684/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8685 is non-zero, then make the breakpoint temporary. If COND_STRING is
8686 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8687 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8688
ab04a2af 8689void
346774a9
PA
8690init_catchpoint (struct breakpoint *b,
8691 struct gdbarch *gdbarch, int tempflag,
8692 char *cond_string,
c0a91b2b 8693 const struct breakpoint_ops *ops)
c906108c 8694{
c5aa993b 8695 struct symtab_and_line sal;
346774a9 8696
fe39c653 8697 init_sal (&sal);
6c95b8df 8698 sal.pspace = current_program_space;
c5aa993b 8699
28010a5d 8700 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8701
1b36a34b 8702 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8703 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8704}
8705
28010a5d 8706void
3ea46bff 8707install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8708{
8709 add_to_breakpoint_chain (b);
3a5c3e22 8710 set_breakpoint_number (internal, b);
558a9d82
YQ
8711 if (is_tracepoint (b))
8712 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8713 if (!internal)
8714 mention (b);
c56053d2 8715 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8716
8717 if (update_gll)
8718 update_global_location_list (1);
c56053d2
PA
8719}
8720
9b70b993 8721static void
a6d9a66e
UW
8722create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8723 int tempflag, char *cond_string,
c0a91b2b 8724 const struct breakpoint_ops *ops)
c906108c 8725{
e29a4733 8726 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8727
e29a4733
PA
8728 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8729
8730 c->forked_inferior_pid = null_ptid;
8731
3ea46bff 8732 install_breakpoint (0, &c->base, 1);
c906108c
SS
8733}
8734
fe798b75
JB
8735/* Exec catchpoints. */
8736
b4d90040
PA
8737/* An instance of this type is used to represent an exec catchpoint.
8738 It includes a "struct breakpoint" as a kind of base class; users
8739 downcast to "struct breakpoint *" when needed. A breakpoint is
8740 really of this type iff its ops pointer points to
8741 CATCH_EXEC_BREAKPOINT_OPS. */
8742
8743struct exec_catchpoint
8744{
8745 /* The base class. */
8746 struct breakpoint base;
8747
8748 /* Filename of a program whose exec triggered this catchpoint.
8749 This field is only valid immediately after this catchpoint has
8750 triggered. */
8751 char *exec_pathname;
8752};
8753
8754/* Implement the "dtor" breakpoint_ops method for exec
8755 catchpoints. */
8756
8757static void
8758dtor_catch_exec (struct breakpoint *b)
8759{
8760 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8761
8762 xfree (c->exec_pathname);
348d480f 8763
2060206e 8764 base_breakpoint_ops.dtor (b);
b4d90040
PA
8765}
8766
77b06cd7
TJB
8767static int
8768insert_catch_exec (struct bp_location *bl)
c906108c 8769{
dfd4cc63 8770 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8771}
c906108c 8772
fe798b75 8773static int
77b06cd7 8774remove_catch_exec (struct bp_location *bl)
fe798b75 8775{
dfd4cc63 8776 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8777}
c906108c 8778
fe798b75 8779static int
f1310107 8780breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8781 struct address_space *aspace, CORE_ADDR bp_addr,
8782 const struct target_waitstatus *ws)
fe798b75 8783{
b4d90040
PA
8784 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8785
f90263c1
TT
8786 if (ws->kind != TARGET_WAITKIND_EXECD)
8787 return 0;
8788
8789 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8790 return 1;
fe798b75 8791}
c906108c 8792
fe798b75 8793static enum print_stop_action
348d480f 8794print_it_catch_exec (bpstat bs)
fe798b75 8795{
36dfb11c 8796 struct ui_out *uiout = current_uiout;
348d480f 8797 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8798 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8799
fe798b75 8800 annotate_catchpoint (b->number);
36dfb11c
TT
8801 if (b->disposition == disp_del)
8802 ui_out_text (uiout, "\nTemporary catchpoint ");
8803 else
8804 ui_out_text (uiout, "\nCatchpoint ");
8805 if (ui_out_is_mi_like_p (uiout))
8806 {
8807 ui_out_field_string (uiout, "reason",
8808 async_reason_lookup (EXEC_ASYNC_EXEC));
8809 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8810 }
8811 ui_out_field_int (uiout, "bkptno", b->number);
8812 ui_out_text (uiout, " (exec'd ");
8813 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8814 ui_out_text (uiout, "), ");
8815
fe798b75 8816 return PRINT_SRC_AND_LOC;
c906108c
SS
8817}
8818
fe798b75 8819static void
a6d9a66e 8820print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8821{
b4d90040 8822 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8823 struct value_print_options opts;
79a45e25 8824 struct ui_out *uiout = current_uiout;
fe798b75
JB
8825
8826 get_user_print_options (&opts);
8827
8828 /* Field 4, the address, is omitted (which makes the columns
8829 not line up too nicely with the headers, but the effect
8830 is relatively readable). */
8831 if (opts.addressprint)
8832 ui_out_field_skip (uiout, "addr");
8833 annotate_field (5);
8834 ui_out_text (uiout, "exec");
b4d90040 8835 if (c->exec_pathname != NULL)
fe798b75
JB
8836 {
8837 ui_out_text (uiout, ", program \"");
b4d90040 8838 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8839 ui_out_text (uiout, "\" ");
8840 }
8ac3646f
TT
8841
8842 if (ui_out_is_mi_like_p (uiout))
8843 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8844}
8845
8846static void
8847print_mention_catch_exec (struct breakpoint *b)
8848{
8849 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8850}
8851
6149aea9
PA
8852/* Implement the "print_recreate" breakpoint_ops method for exec
8853 catchpoints. */
8854
8855static void
8856print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8857{
8858 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8859 print_recreate_thread (b, fp);
6149aea9
PA
8860}
8861
2060206e 8862static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8863
a96d9b2e
SDJ
8864static void
8865create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8866 const struct breakpoint_ops *ops)
a96d9b2e 8867{
be5c67c1 8868 struct syscall_catchpoint *c;
a96d9b2e 8869 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8870
be5c67c1
PA
8871 c = XNEW (struct syscall_catchpoint);
8872 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8873 c->syscalls_to_be_caught = filter;
a96d9b2e 8874
3ea46bff 8875 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8876}
8877
c906108c 8878static int
fba45db2 8879hw_breakpoint_used_count (void)
c906108c 8880{
c906108c 8881 int i = 0;
f1310107
TJB
8882 struct breakpoint *b;
8883 struct bp_location *bl;
c906108c
SS
8884
8885 ALL_BREAKPOINTS (b)
c5aa993b 8886 {
d6b74ac4 8887 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8888 for (bl = b->loc; bl; bl = bl->next)
8889 {
8890 /* Special types of hardware breakpoints may use more than
8891 one register. */
348d480f 8892 i += b->ops->resources_needed (bl);
f1310107 8893 }
c5aa993b 8894 }
c906108c
SS
8895
8896 return i;
8897}
8898
a1398e0c
PA
8899/* Returns the resources B would use if it were a hardware
8900 watchpoint. */
8901
c906108c 8902static int
a1398e0c 8903hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8904{
c906108c 8905 int i = 0;
e09342b5 8906 struct bp_location *bl;
c906108c 8907
a1398e0c
PA
8908 if (!breakpoint_enabled (b))
8909 return 0;
8910
8911 for (bl = b->loc; bl; bl = bl->next)
8912 {
8913 /* Special types of hardware watchpoints may use more than
8914 one register. */
8915 i += b->ops->resources_needed (bl);
8916 }
8917
8918 return i;
8919}
8920
8921/* Returns the sum the used resources of all hardware watchpoints of
8922 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8923 the sum of the used resources of all hardware watchpoints of other
8924 types _not_ TYPE. */
8925
8926static int
8927hw_watchpoint_used_count_others (struct breakpoint *except,
8928 enum bptype type, int *other_type_used)
8929{
8930 int i = 0;
8931 struct breakpoint *b;
8932
c906108c
SS
8933 *other_type_used = 0;
8934 ALL_BREAKPOINTS (b)
e09342b5 8935 {
a1398e0c
PA
8936 if (b == except)
8937 continue;
e09342b5
TJB
8938 if (!breakpoint_enabled (b))
8939 continue;
8940
a1398e0c
PA
8941 if (b->type == type)
8942 i += hw_watchpoint_use_count (b);
8943 else if (is_hardware_watchpoint (b))
8944 *other_type_used = 1;
e09342b5
TJB
8945 }
8946
c906108c
SS
8947 return i;
8948}
8949
c906108c 8950void
fba45db2 8951disable_watchpoints_before_interactive_call_start (void)
c906108c 8952{
c5aa993b 8953 struct breakpoint *b;
c906108c
SS
8954
8955 ALL_BREAKPOINTS (b)
c5aa993b 8956 {
cc60f2e3 8957 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8958 {
b5de0fa7 8959 b->enable_state = bp_call_disabled;
b60e7edf 8960 update_global_location_list (0);
c5aa993b
JM
8961 }
8962 }
c906108c
SS
8963}
8964
8965void
fba45db2 8966enable_watchpoints_after_interactive_call_stop (void)
c906108c 8967{
c5aa993b 8968 struct breakpoint *b;
c906108c
SS
8969
8970 ALL_BREAKPOINTS (b)
c5aa993b 8971 {
cc60f2e3 8972 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8973 {
b5de0fa7 8974 b->enable_state = bp_enabled;
b60e7edf 8975 update_global_location_list (1);
c5aa993b
JM
8976 }
8977 }
c906108c
SS
8978}
8979
8bea4e01
UW
8980void
8981disable_breakpoints_before_startup (void)
8982{
6c95b8df 8983 current_program_space->executing_startup = 1;
f8eba3c6 8984 update_global_location_list (0);
8bea4e01
UW
8985}
8986
8987void
8988enable_breakpoints_after_startup (void)
8989{
6c95b8df 8990 current_program_space->executing_startup = 0;
f8eba3c6 8991 breakpoint_re_set ();
8bea4e01
UW
8992}
8993
c906108c
SS
8994
8995/* Set a breakpoint that will evaporate an end of command
8996 at address specified by SAL.
8997 Restrict it to frame FRAME if FRAME is nonzero. */
8998
8999struct breakpoint *
a6d9a66e
UW
9000set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9001 struct frame_id frame_id, enum bptype type)
c906108c 9002{
52f0bd74 9003 struct breakpoint *b;
edb3359d 9004
193facb3
JK
9005 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9006 tail-called one. */
9007 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9008
06edf0c0 9009 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9010 b->enable_state = bp_enabled;
9011 b->disposition = disp_donttouch;
818dd999 9012 b->frame_id = frame_id;
c906108c 9013
4a64f543
MS
9014 /* If we're debugging a multi-threaded program, then we want
9015 momentary breakpoints to be active in only a single thread of
9016 control. */
39f77062
KB
9017 if (in_thread_list (inferior_ptid))
9018 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9019
b60e7edf 9020 update_global_location_list_nothrow (1);
74960c60 9021
c906108c
SS
9022 return b;
9023}
611c83ae 9024
06edf0c0
PA
9025/* Make a momentary breakpoint based on the master breakpoint ORIG.
9026 The new breakpoint will have type TYPE, and use OPS as it
9027 breakpoint_ops. */
e58b0e63 9028
06edf0c0
PA
9029static struct breakpoint *
9030momentary_breakpoint_from_master (struct breakpoint *orig,
9031 enum bptype type,
c0a91b2b 9032 const struct breakpoint_ops *ops)
e58b0e63
PA
9033{
9034 struct breakpoint *copy;
9035
06edf0c0 9036 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9037 copy->loc = allocate_bp_location (copy);
0e30163f 9038 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9039
a6d9a66e 9040 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9041 copy->loc->requested_address = orig->loc->requested_address;
9042 copy->loc->address = orig->loc->address;
9043 copy->loc->section = orig->loc->section;
6c95b8df 9044 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9045 copy->loc->probe = orig->loc->probe;
f8eba3c6 9046 copy->loc->line_number = orig->loc->line_number;
2f202fde 9047 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
9048 copy->frame_id = orig->frame_id;
9049 copy->thread = orig->thread;
6c95b8df 9050 copy->pspace = orig->pspace;
e58b0e63
PA
9051
9052 copy->enable_state = bp_enabled;
9053 copy->disposition = disp_donttouch;
9054 copy->number = internal_breakpoint_number--;
9055
9056 update_global_location_list_nothrow (0);
9057 return copy;
9058}
9059
06edf0c0
PA
9060/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9061 ORIG is NULL. */
9062
9063struct breakpoint *
9064clone_momentary_breakpoint (struct breakpoint *orig)
9065{
9066 /* If there's nothing to clone, then return nothing. */
9067 if (orig == NULL)
9068 return NULL;
9069
9070 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
9071}
9072
611c83ae 9073struct breakpoint *
a6d9a66e
UW
9074set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9075 enum bptype type)
611c83ae
PA
9076{
9077 struct symtab_and_line sal;
9078
9079 sal = find_pc_line (pc, 0);
9080 sal.pc = pc;
9081 sal.section = find_pc_overlay (pc);
9082 sal.explicit_pc = 1;
9083
a6d9a66e 9084 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9085}
c906108c 9086\f
c5aa993b 9087
c906108c
SS
9088/* Tell the user we have just set a breakpoint B. */
9089
9090static void
fba45db2 9091mention (struct breakpoint *b)
c906108c 9092{
348d480f 9093 b->ops->print_mention (b);
79a45e25 9094 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9095 return;
c906108c
SS
9096 printf_filtered ("\n");
9097}
c906108c 9098\f
c5aa993b 9099
0d381245 9100static struct bp_location *
39d61571 9101add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9102 const struct symtab_and_line *sal)
9103{
9104 struct bp_location *loc, **tmp;
3742cc8b
YQ
9105 CORE_ADDR adjusted_address;
9106 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9107
9108 if (loc_gdbarch == NULL)
9109 loc_gdbarch = b->gdbarch;
9110
9111 /* Adjust the breakpoint's address prior to allocating a location.
9112 Once we call allocate_bp_location(), that mostly uninitialized
9113 location will be placed on the location chain. Adjustment of the
9114 breakpoint may cause target_read_memory() to be called and we do
9115 not want its scan of the location chain to find a breakpoint and
9116 location that's only been partially initialized. */
9117 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9118 sal->pc, b->type);
0d381245 9119
d30113d4 9120 /* Sort the locations by their ADDRESS. */
39d61571 9121 loc = allocate_bp_location (b);
d30113d4
JK
9122 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9123 tmp = &((*tmp)->next))
0d381245 9124 ;
d30113d4 9125 loc->next = *tmp;
0d381245 9126 *tmp = loc;
3742cc8b 9127
0d381245 9128 loc->requested_address = sal->pc;
3742cc8b 9129 loc->address = adjusted_address;
6c95b8df 9130 loc->pspace = sal->pspace;
729662a5
TT
9131 loc->probe.probe = sal->probe;
9132 loc->probe.objfile = sal->objfile;
6c95b8df 9133 gdb_assert (loc->pspace != NULL);
0d381245 9134 loc->section = sal->section;
3742cc8b 9135 loc->gdbarch = loc_gdbarch;
f8eba3c6 9136 loc->line_number = sal->line;
2f202fde 9137 loc->symtab = sal->symtab;
f8eba3c6 9138
0e30163f
JK
9139 set_breakpoint_location_function (loc,
9140 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9141 return loc;
9142}
514f746b
AR
9143\f
9144
9145/* Return 1 if LOC is pointing to a permanent breakpoint,
9146 return 0 otherwise. */
9147
9148static int
9149bp_loc_is_permanent (struct bp_location *loc)
9150{
9151 int len;
9152 CORE_ADDR addr;
1afeeb75 9153 const gdb_byte *bpoint;
514f746b 9154 gdb_byte *target_mem;
939c61fa
JK
9155 struct cleanup *cleanup;
9156 int retval = 0;
514f746b
AR
9157
9158 gdb_assert (loc != NULL);
9159
9160 addr = loc->address;
1afeeb75 9161 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9162
939c61fa 9163 /* Software breakpoints unsupported? */
1afeeb75 9164 if (bpoint == NULL)
939c61fa
JK
9165 return 0;
9166
514f746b
AR
9167 target_mem = alloca (len);
9168
939c61fa
JK
9169 /* Enable the automatic memory restoration from breakpoints while
9170 we read the memory. Otherwise we could say about our temporary
9171 breakpoints they are permanent. */
6c95b8df
PA
9172 cleanup = save_current_space_and_thread ();
9173
9174 switch_to_program_space_and_thread (loc->pspace);
9175 make_show_memory_breakpoints_cleanup (0);
939c61fa 9176
514f746b 9177 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9178 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9179 retval = 1;
514f746b 9180
939c61fa
JK
9181 do_cleanups (cleanup);
9182
9183 return retval;
514f746b
AR
9184}
9185
e7e0cddf
SS
9186/* Build a command list for the dprintf corresponding to the current
9187 settings of the dprintf style options. */
9188
9189static void
9190update_dprintf_command_list (struct breakpoint *b)
9191{
9192 char *dprintf_args = b->extra_string;
9193 char *printf_line = NULL;
9194
9195 if (!dprintf_args)
9196 return;
9197
9198 dprintf_args = skip_spaces (dprintf_args);
9199
9200 /* Allow a comma, as it may have terminated a location, but don't
9201 insist on it. */
9202 if (*dprintf_args == ',')
9203 ++dprintf_args;
9204 dprintf_args = skip_spaces (dprintf_args);
9205
9206 if (*dprintf_args != '"')
9207 error (_("Bad format string, missing '\"'."));
9208
d3ce09f5 9209 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9210 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9211 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9212 {
9213 if (!dprintf_function)
9214 error (_("No function supplied for dprintf call"));
9215
9216 if (dprintf_channel && strlen (dprintf_channel) > 0)
9217 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9218 dprintf_function,
9219 dprintf_channel,
9220 dprintf_args);
9221 else
9222 printf_line = xstrprintf ("call (void) %s (%s)",
9223 dprintf_function,
9224 dprintf_args);
9225 }
d3ce09f5
SS
9226 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9227 {
9228 if (target_can_run_breakpoint_commands ())
9229 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9230 else
9231 {
9232 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9233 printf_line = xstrprintf ("printf %s", dprintf_args);
9234 }
9235 }
e7e0cddf
SS
9236 else
9237 internal_error (__FILE__, __LINE__,
9238 _("Invalid dprintf style."));
9239
f28045c2 9240 gdb_assert (printf_line != NULL);
9d6e6e84 9241 /* Manufacture a printf sequence. */
f28045c2 9242 {
9d6e6e84
HZ
9243 struct command_line *printf_cmd_line
9244 = xmalloc (sizeof (struct command_line));
e7e0cddf 9245
f28045c2
YQ
9246 printf_cmd_line = xmalloc (sizeof (struct command_line));
9247 printf_cmd_line->control_type = simple_control;
9248 printf_cmd_line->body_count = 0;
9249 printf_cmd_line->body_list = NULL;
9d6e6e84 9250 printf_cmd_line->next = NULL;
f28045c2 9251 printf_cmd_line->line = printf_line;
e7e0cddf 9252
f28045c2
YQ
9253 breakpoint_set_commands (b, printf_cmd_line);
9254 }
e7e0cddf
SS
9255}
9256
9257/* Update all dprintf commands, making their command lists reflect
9258 current style settings. */
9259
9260static void
9261update_dprintf_commands (char *args, int from_tty,
9262 struct cmd_list_element *c)
9263{
9264 struct breakpoint *b;
9265
9266 ALL_BREAKPOINTS (b)
9267 {
9268 if (b->type == bp_dprintf)
9269 update_dprintf_command_list (b);
9270 }
9271}
c3f6f71d 9272
018d34a4
VP
9273/* Create a breakpoint with SAL as location. Use ADDR_STRING
9274 as textual description of the location, and COND_STRING
db107f19 9275 as condition expression. */
018d34a4
VP
9276
9277static void
d9b3f62e
PA
9278init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9279 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9280 char *filter, char *cond_string,
e7e0cddf 9281 char *extra_string,
d9b3f62e
PA
9282 enum bptype type, enum bpdisp disposition,
9283 int thread, int task, int ignore_count,
c0a91b2b 9284 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9285 int enabled, int internal, unsigned flags,
9286 int display_canonical)
018d34a4 9287{
0d381245 9288 int i;
018d34a4
VP
9289
9290 if (type == bp_hardware_breakpoint)
9291 {
fbbd034e
AS
9292 int target_resources_ok;
9293
9294 i = hw_breakpoint_used_count ();
9295 target_resources_ok =
9296 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9297 i + 1, 0);
9298 if (target_resources_ok == 0)
9299 error (_("No hardware breakpoint support in the target."));
9300 else if (target_resources_ok < 0)
9301 error (_("Hardware breakpoints used exceeds limit."));
9302 }
9303
6c95b8df
PA
9304 gdb_assert (sals.nelts > 0);
9305
0d381245
VP
9306 for (i = 0; i < sals.nelts; ++i)
9307 {
9308 struct symtab_and_line sal = sals.sals[i];
9309 struct bp_location *loc;
9310
9311 if (from_tty)
5af949e3
UW
9312 {
9313 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9314 if (!loc_gdbarch)
9315 loc_gdbarch = gdbarch;
9316
9317 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9318 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9319 }
0d381245
VP
9320
9321 if (i == 0)
9322 {
d9b3f62e 9323 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9324 b->thread = thread;
4a306c9a 9325 b->task = task;
855a6e68 9326
0d381245 9327 b->cond_string = cond_string;
e7e0cddf 9328 b->extra_string = extra_string;
0d381245 9329 b->ignore_count = ignore_count;
41447f92 9330 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9331 b->disposition = disposition;
6c95b8df 9332
44f238bb
PA
9333 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9334 b->loc->inserted = 1;
9335
0fb4aa4b
PA
9336 if (type == bp_static_tracepoint)
9337 {
d9b3f62e 9338 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9339 struct static_tracepoint_marker marker;
9340
983af33b 9341 if (strace_marker_p (b))
0fb4aa4b
PA
9342 {
9343 /* We already know the marker exists, otherwise, we
9344 wouldn't see a sal for it. */
9345 char *p = &addr_string[3];
9346 char *endp;
9347 char *marker_str;
0fb4aa4b 9348
e9cafbcc 9349 p = skip_spaces (p);
0fb4aa4b 9350
e9cafbcc 9351 endp = skip_to_space (p);
0fb4aa4b
PA
9352
9353 marker_str = savestring (p, endp - p);
d9b3f62e 9354 t->static_trace_marker_id = marker_str;
0fb4aa4b 9355
3e43a32a
MS
9356 printf_filtered (_("Probed static tracepoint "
9357 "marker \"%s\"\n"),
d9b3f62e 9358 t->static_trace_marker_id);
0fb4aa4b
PA
9359 }
9360 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9361 {
d9b3f62e 9362 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9363 release_static_tracepoint_marker (&marker);
9364
3e43a32a
MS
9365 printf_filtered (_("Probed static tracepoint "
9366 "marker \"%s\"\n"),
d9b3f62e 9367 t->static_trace_marker_id);
0fb4aa4b
PA
9368 }
9369 else
3e43a32a
MS
9370 warning (_("Couldn't determine the static "
9371 "tracepoint marker to probe"));
0fb4aa4b
PA
9372 }
9373
0d381245
VP
9374 loc = b->loc;
9375 }
9376 else
018d34a4 9377 {
39d61571 9378 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9379 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9380 loc->inserted = 1;
0d381245
VP
9381 }
9382
514f746b
AR
9383 if (bp_loc_is_permanent (loc))
9384 make_breakpoint_permanent (b);
9385
0d381245
VP
9386 if (b->cond_string)
9387 {
bbc13ae3
KS
9388 const char *arg = b->cond_string;
9389
1bb9788d
TT
9390 loc->cond = parse_exp_1 (&arg, loc->address,
9391 block_for_pc (loc->address), 0);
0d381245 9392 if (*arg)
588ae58c 9393 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9394 }
e7e0cddf
SS
9395
9396 /* Dynamic printf requires and uses additional arguments on the
9397 command line, otherwise it's an error. */
9398 if (type == bp_dprintf)
9399 {
9400 if (b->extra_string)
9401 update_dprintf_command_list (b);
9402 else
9403 error (_("Format string required"));
9404 }
9405 else if (b->extra_string)
588ae58c 9406 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9407 }
018d34a4 9408
56435ebe 9409 b->display_canonical = display_canonical;
018d34a4
VP
9410 if (addr_string)
9411 b->addr_string = addr_string;
9412 else
9413 /* addr_string has to be used or breakpoint_re_set will delete
9414 me. */
5af949e3
UW
9415 b->addr_string
9416 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9417 b->filter = filter;
d9b3f62e 9418}
018d34a4 9419
d9b3f62e
PA
9420static void
9421create_breakpoint_sal (struct gdbarch *gdbarch,
9422 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9423 char *filter, char *cond_string,
e7e0cddf 9424 char *extra_string,
d9b3f62e
PA
9425 enum bptype type, enum bpdisp disposition,
9426 int thread, int task, int ignore_count,
c0a91b2b 9427 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9428 int enabled, int internal, unsigned flags,
9429 int display_canonical)
d9b3f62e
PA
9430{
9431 struct breakpoint *b;
9432 struct cleanup *old_chain;
9433
9434 if (is_tracepoint_type (type))
9435 {
9436 struct tracepoint *t;
9437
9438 t = XCNEW (struct tracepoint);
9439 b = &t->base;
9440 }
9441 else
9442 b = XNEW (struct breakpoint);
9443
9444 old_chain = make_cleanup (xfree, b);
9445
9446 init_breakpoint_sal (b, gdbarch,
9447 sals, addr_string,
e7e0cddf 9448 filter, cond_string, extra_string,
d9b3f62e
PA
9449 type, disposition,
9450 thread, task, ignore_count,
9451 ops, from_tty,
44f238bb
PA
9452 enabled, internal, flags,
9453 display_canonical);
d9b3f62e
PA
9454 discard_cleanups (old_chain);
9455
3ea46bff 9456 install_breakpoint (internal, b, 0);
018d34a4
VP
9457}
9458
9459/* Add SALS.nelts breakpoints to the breakpoint table. For each
9460 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9461 value. COND_STRING, if not NULL, specified the condition to be
9462 used for all breakpoints. Essentially the only case where
9463 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9464 function. In that case, it's still not possible to specify
9465 separate conditions for different overloaded functions, so
9466 we take just a single condition string.
9467
c3f6f71d 9468 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9469 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9470 array contents). If the function fails (error() is called), the
9471 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9472 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9473
9474static void
8cdf0e15 9475create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9476 struct linespec_result *canonical,
e7e0cddf 9477 char *cond_string, char *extra_string,
8cdf0e15
VP
9478 enum bptype type, enum bpdisp disposition,
9479 int thread, int task, int ignore_count,
c0a91b2b 9480 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9481 int enabled, int internal, unsigned flags)
c906108c 9482{
018d34a4 9483 int i;
f8eba3c6 9484 struct linespec_sals *lsal;
cc59ec59 9485
f8eba3c6
TT
9486 if (canonical->pre_expanded)
9487 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9488
9489 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9490 {
f8eba3c6
TT
9491 /* Note that 'addr_string' can be NULL in the case of a plain
9492 'break', without arguments. */
9493 char *addr_string = (canonical->addr_string
9494 ? xstrdup (canonical->addr_string)
9495 : NULL);
9496 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9497 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9498
f8eba3c6
TT
9499 make_cleanup (xfree, filter_string);
9500 create_breakpoint_sal (gdbarch, lsal->sals,
9501 addr_string,
9502 filter_string,
e7e0cddf
SS
9503 cond_string, extra_string,
9504 type, disposition,
84f4c1fe 9505 thread, task, ignore_count, ops,
44f238bb 9506 from_tty, enabled, internal, flags,
56435ebe 9507 canonical->special_display);
f8eba3c6 9508 discard_cleanups (inner);
c3f6f71d 9509 }
c3f6f71d 9510}
c906108c 9511
9998af43 9512/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9513 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9514 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9515 address strings. ADDRESS points to the end of the SAL.
9516
9517 The array and the line spec strings are allocated on the heap, it is
9518 the caller's responsibility to free them. */
c906108c 9519
b9362cc7 9520static void
c3f6f71d 9521parse_breakpoint_sals (char **address,
58438ac1 9522 struct linespec_result *canonical)
c3f6f71d 9523{
c3f6f71d 9524 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9525 breakpoint. */
c3f6f71d
JM
9526 if ((*address) == NULL
9527 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9528 {
1bfeeb0f
JL
9529 /* The last displayed codepoint, if it's valid, is our default breakpoint
9530 address. */
9531 if (last_displayed_sal_is_valid ())
c906108c 9532 {
f8eba3c6 9533 struct linespec_sals lsal;
c3f6f71d 9534 struct symtab_and_line sal;
1c8cdcb1 9535 CORE_ADDR pc;
cc59ec59 9536
4a64f543 9537 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9538 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9539 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9540
9541 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9542 corresponding to the last call to print_frame_info.
9543 Be sure to reinitialize LINE with NOTCURRENT == 0
9544 as the breakpoint line number is inappropriate otherwise.
9545 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9546 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9547 pc = sal.pc;
9548 sal = find_pc_line (pc, 0);
00903456 9549
4a64f543 9550 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9551 where PC is the last displayed codepoint's address. So
9552 make sure to set sal.explicit_pc to prevent GDB from
9553 trying to expand the list of sals to include all other
9554 instances with the same symtab and line. */
1c8cdcb1 9555 sal.pc = pc;
00903456
JK
9556 sal.explicit_pc = 1;
9557
f8eba3c6
TT
9558 lsal.sals.sals[0] = sal;
9559 lsal.sals.nelts = 1;
9560 lsal.canonical = NULL;
9561
9562 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9563 }
9564 else
8a3fe4f8 9565 error (_("No default breakpoint address now."));
c906108c
SS
9566 }
9567 else
9568 {
cc80f267
JK
9569 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9570
c906108c 9571 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9572 current_source_symtab (which is decode_line_1's default).
9573 This should produce the results we want almost all of the
cc80f267
JK
9574 time while leaving default_breakpoint_* alone.
9575
9576 ObjC: However, don't match an Objective-C method name which
9577 may have a '+' or '-' succeeded by a '['. */
9578 if (last_displayed_sal_is_valid ()
9579 && (!cursal.symtab
9580 || ((strchr ("+-", (*address)[0]) != NULL)
9581 && ((*address)[1] != '['))))
f8eba3c6
TT
9582 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9583 get_last_displayed_symtab (),
9584 get_last_displayed_line (),
9585 canonical, NULL, NULL);
c906108c 9586 else
f8eba3c6 9587 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9588 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9589 }
c3f6f71d 9590}
c906108c 9591
c906108c 9592
c3f6f71d 9593/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9594 inserted as a breakpoint. If it can't throw an error. */
c906108c 9595
b9362cc7 9596static void
23e7acfb 9597breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9598{
9599 int i;
cc59ec59 9600
c3f6f71d 9601 for (i = 0; i < sals->nelts; i++)
ee53e872 9602 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9603}
9604
7a697b8d
SS
9605/* Fast tracepoints may have restrictions on valid locations. For
9606 instance, a fast tracepoint using a jump instead of a trap will
9607 likely have to overwrite more bytes than a trap would, and so can
9608 only be placed where the instruction is longer than the jump, or a
9609 multi-instruction sequence does not have a jump into the middle of
9610 it, etc. */
9611
9612static void
9613check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9614 struct symtabs_and_lines *sals)
9615{
9616 int i, rslt;
9617 struct symtab_and_line *sal;
9618 char *msg;
9619 struct cleanup *old_chain;
9620
9621 for (i = 0; i < sals->nelts; i++)
9622 {
f8eba3c6
TT
9623 struct gdbarch *sarch;
9624
7a697b8d
SS
9625 sal = &sals->sals[i];
9626
f8eba3c6
TT
9627 sarch = get_sal_arch (*sal);
9628 /* We fall back to GDBARCH if there is no architecture
9629 associated with SAL. */
9630 if (sarch == NULL)
9631 sarch = gdbarch;
9632 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9633 NULL, &msg);
9634 old_chain = make_cleanup (xfree, msg);
9635
9636 if (!rslt)
9637 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9638 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9639
9640 do_cleanups (old_chain);
9641 }
9642}
9643
af4908ba
KS
9644/* Issue an invalid thread ID error. */
9645
9646static void ATTRIBUTE_NORETURN
9647invalid_thread_id_error (int id)
9648{
9649 error (_("Unknown thread %d."), id);
9650}
9651
018d34a4
VP
9652/* Given TOK, a string specification of condition and thread, as
9653 accepted by the 'break' command, extract the condition
9654 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9655 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9656 If no condition is found, *COND_STRING is set to NULL.
9657 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9658
9659static void
bbc13ae3 9660find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9661 char **cond_string, int *thread, int *task,
9662 char **rest)
018d34a4
VP
9663{
9664 *cond_string = NULL;
9665 *thread = -1;
ed1d1739
KS
9666 *task = 0;
9667 *rest = NULL;
9668
018d34a4
VP
9669 while (tok && *tok)
9670 {
bbc13ae3 9671 const char *end_tok;
018d34a4 9672 int toklen;
bbc13ae3
KS
9673 const char *cond_start = NULL;
9674 const char *cond_end = NULL;
cc59ec59 9675
bbc13ae3 9676 tok = skip_spaces_const (tok);
e7e0cddf
SS
9677
9678 if ((*tok == '"' || *tok == ',') && rest)
9679 {
9680 *rest = savestring (tok, strlen (tok));
9681 return;
9682 }
9683
bbc13ae3 9684 end_tok = skip_to_space_const (tok);
d634f2de 9685
018d34a4 9686 toklen = end_tok - tok;
d634f2de 9687
018d34a4
VP
9688 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9689 {
f7545552
TT
9690 struct expression *expr;
9691
018d34a4 9692 tok = cond_start = end_tok + 1;
1bb9788d 9693 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9694 xfree (expr);
018d34a4 9695 cond_end = tok;
d634f2de 9696 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9697 }
9698 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9699 {
9700 char *tmptok;
d634f2de 9701
018d34a4 9702 tok = end_tok + 1;
bbc13ae3 9703 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9704 if (tok == tmptok)
9705 error (_("Junk after thread keyword."));
9706 if (!valid_thread_id (*thread))
af4908ba 9707 invalid_thread_id_error (*thread);
bbc13ae3 9708 tok = tmptok;
018d34a4 9709 }
4a306c9a
JB
9710 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9711 {
9712 char *tmptok;
9713
9714 tok = end_tok + 1;
bbc13ae3 9715 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9716 if (tok == tmptok)
9717 error (_("Junk after task keyword."));
9718 if (!valid_task_id (*task))
b6199126 9719 error (_("Unknown task %d."), *task);
bbc13ae3 9720 tok = tmptok;
4a306c9a 9721 }
e7e0cddf
SS
9722 else if (rest)
9723 {
9724 *rest = savestring (tok, strlen (tok));
ccab2054 9725 return;
e7e0cddf 9726 }
018d34a4
VP
9727 else
9728 error (_("Junk at end of arguments."));
9729 }
9730}
9731
0fb4aa4b
PA
9732/* Decode a static tracepoint marker spec. */
9733
9734static struct symtabs_and_lines
9735decode_static_tracepoint_spec (char **arg_p)
9736{
9737 VEC(static_tracepoint_marker_p) *markers = NULL;
9738 struct symtabs_and_lines sals;
0fb4aa4b
PA
9739 struct cleanup *old_chain;
9740 char *p = &(*arg_p)[3];
9741 char *endp;
9742 char *marker_str;
9743 int i;
9744
e9cafbcc 9745 p = skip_spaces (p);
0fb4aa4b 9746
e9cafbcc 9747 endp = skip_to_space (p);
0fb4aa4b
PA
9748
9749 marker_str = savestring (p, endp - p);
9750 old_chain = make_cleanup (xfree, marker_str);
9751
9752 markers = target_static_tracepoint_markers_by_strid (marker_str);
9753 if (VEC_empty(static_tracepoint_marker_p, markers))
9754 error (_("No known static tracepoint marker named %s"), marker_str);
9755
9756 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9757 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9758
9759 for (i = 0; i < sals.nelts; i++)
9760 {
9761 struct static_tracepoint_marker *marker;
9762
9763 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9764
9765 init_sal (&sals.sals[i]);
9766
9767 sals.sals[i] = find_pc_line (marker->address, 0);
9768 sals.sals[i].pc = marker->address;
9769
9770 release_static_tracepoint_marker (marker);
9771 }
9772
9773 do_cleanups (old_chain);
9774
9775 *arg_p = endp;
9776 return sals;
9777}
9778
fd9b8c24
PA
9779/* Set a breakpoint. This function is shared between CLI and MI
9780 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9781 modes of operations, selected by the PARSE_ARG parameter. If
9782 non-zero, the function will parse ARG, extracting location,
9783 condition, thread and extra string. Otherwise, ARG is just the
9784 breakpoint's location, with condition, thread, and extra string
9785 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9786 If INTERNAL is non-zero, the breakpoint number will be allocated
9787 from the internal breakpoint count. Returns true if any breakpoint
9788 was created; false otherwise. */
0101ce28 9789
8cdf0e15
VP
9790int
9791create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9792 char *arg, char *cond_string,
9793 int thread, char *extra_string,
f6de8ec2 9794 int parse_arg,
0fb4aa4b 9795 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9796 int ignore_count,
9797 enum auto_boolean pending_break_support,
c0a91b2b 9798 const struct breakpoint_ops *ops,
44f238bb
PA
9799 int from_tty, int enabled, int internal,
9800 unsigned flags)
c3f6f71d 9801{
b78a6381 9802 volatile struct gdb_exception e;
f8eba3c6 9803 char *copy_arg = NULL;
c3f6f71d 9804 char *addr_start = arg;
7efd8fc2 9805 struct linespec_result canonical;
c3f6f71d 9806 struct cleanup *old_chain;
80c99de1 9807 struct cleanup *bkpt_chain = NULL;
0101ce28 9808 int pending = 0;
4a306c9a 9809 int task = 0;
86b17b60 9810 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9811
348d480f
PA
9812 gdb_assert (ops != NULL);
9813
7efd8fc2 9814 init_linespec_result (&canonical);
c3f6f71d 9815
b78a6381
TT
9816 TRY_CATCH (e, RETURN_MASK_ALL)
9817 {
983af33b
SDJ
9818 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9819 addr_start, &copy_arg);
b78a6381 9820 }
0101ce28
JJ
9821
9822 /* If caller is interested in rc value from parse, set value. */
05ff989b 9823 switch (e.reason)
0101ce28 9824 {
983af33b
SDJ
9825 case GDB_NO_ERROR:
9826 if (VEC_empty (linespec_sals, canonical.sals))
9827 return 0;
9828 break;
05ff989b
AC
9829 case RETURN_ERROR:
9830 switch (e.error)
0101ce28 9831 {
05ff989b 9832 case NOT_FOUND_ERROR:
0101ce28 9833
05ff989b
AC
9834 /* If pending breakpoint support is turned off, throw
9835 error. */
fa8d40ab
JJ
9836
9837 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9838 throw_exception (e);
9839
9840 exception_print (gdb_stderr, e);
fa8d40ab 9841
05ff989b
AC
9842 /* If pending breakpoint support is auto query and the user
9843 selects no, then simply return the error code. */
059fb39f 9844 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9845 && !nquery (_("Make %s pending on future shared library load? "),
9846 bptype_string (type_wanted)))
fd9b8c24 9847 return 0;
fa8d40ab 9848
05ff989b
AC
9849 /* At this point, either the user was queried about setting
9850 a pending breakpoint and selected yes, or pending
9851 breakpoint behavior is on and thus a pending breakpoint
9852 is defaulted on behalf of the user. */
f8eba3c6
TT
9853 {
9854 struct linespec_sals lsal;
9855
9856 copy_arg = xstrdup (addr_start);
9857 lsal.canonical = xstrdup (copy_arg);
9858 lsal.sals.nelts = 1;
9859 lsal.sals.sals = XNEW (struct symtab_and_line);
9860 init_sal (&lsal.sals.sals[0]);
9861 pending = 1;
9862 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9863 }
05ff989b
AC
9864 break;
9865 default:
98deb0da 9866 throw_exception (e);
0101ce28 9867 }
2abae994 9868 break;
05ff989b 9869 default:
983af33b 9870 throw_exception (e);
0101ce28 9871 }
c3f6f71d 9872
4a64f543 9873 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9874 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9875
c3f6f71d
JM
9876 /* ----------------------------- SNIP -----------------------------
9877 Anything added to the cleanup chain beyond this point is assumed
9878 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9879 then the memory is not reclaimed. */
9880 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9881
c3f6f71d
JM
9882 /* Resolve all line numbers to PC's and verify that the addresses
9883 are ok for the target. */
0101ce28 9884 if (!pending)
f8eba3c6
TT
9885 {
9886 int ix;
9887 struct linespec_sals *iter;
9888
9889 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9890 breakpoint_sals_to_pc (&iter->sals);
9891 }
c3f6f71d 9892
7a697b8d 9893 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9894 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9895 {
9896 int ix;
9897 struct linespec_sals *iter;
9898
9899 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9900 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9901 }
7a697b8d 9902
c3f6f71d
JM
9903 /* Verify that condition can be parsed, before setting any
9904 breakpoints. Allocate a separate condition expression for each
4a64f543 9905 breakpoint. */
0101ce28 9906 if (!pending)
c3f6f71d 9907 {
f6de8ec2 9908 if (parse_arg)
72b2ff0e 9909 {
0878d0fa 9910 char *rest;
52d361e1
YQ
9911 struct linespec_sals *lsal;
9912
9913 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9914
0878d0fa
YQ
9915 /* Here we only parse 'arg' to separate condition
9916 from thread number, so parsing in context of first
9917 sal is OK. When setting the breakpoint we'll
9918 re-parse it in context of each sal. */
9919
9920 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9921 &thread, &task, &rest);
9922 if (cond_string)
9923 make_cleanup (xfree, cond_string);
9924 if (rest)
9925 make_cleanup (xfree, rest);
9926 if (rest)
9927 extra_string = rest;
72b2ff0e 9928 }
2f069f6f 9929 else
72b2ff0e 9930 {
0878d0fa
YQ
9931 if (*arg != '\0')
9932 error (_("Garbage '%s' at end of location"), arg);
9933
9934 /* Create a private copy of condition string. */
9935 if (cond_string)
9936 {
9937 cond_string = xstrdup (cond_string);
9938 make_cleanup (xfree, cond_string);
9939 }
9940 /* Create a private copy of any extra string. */
9941 if (extra_string)
9942 {
9943 extra_string = xstrdup (extra_string);
9944 make_cleanup (xfree, extra_string);
9945 }
72b2ff0e 9946 }
0fb4aa4b 9947
52d361e1 9948 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9949 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9950 tempflag ? disp_del : disp_donttouch,
9951 thread, task, ignore_count, ops,
44f238bb 9952 from_tty, enabled, internal, flags);
c906108c 9953 }
0101ce28
JJ
9954 else
9955 {
0101ce28
JJ
9956 struct breakpoint *b;
9957
0101ce28
JJ
9958 make_cleanup (xfree, copy_arg);
9959
bfccc43c
YQ
9960 if (is_tracepoint_type (type_wanted))
9961 {
9962 struct tracepoint *t;
9963
9964 t = XCNEW (struct tracepoint);
9965 b = &t->base;
9966 }
9967 else
9968 b = XNEW (struct breakpoint);
9969
9970 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9971
f8eba3c6 9972 b->addr_string = copy_arg;
f6de8ec2 9973 if (parse_arg)
e12c7713
MK
9974 b->cond_string = NULL;
9975 else
9976 {
9977 /* Create a private copy of condition string. */
9978 if (cond_string)
9979 {
9980 cond_string = xstrdup (cond_string);
9981 make_cleanup (xfree, cond_string);
9982 }
9983 b->cond_string = cond_string;
9984 }
e7e0cddf 9985 b->extra_string = NULL;
0101ce28 9986 b->ignore_count = ignore_count;
0101ce28 9987 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9988 b->condition_not_parsed = 1;
41447f92 9989 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9990 if ((type_wanted != bp_breakpoint
9991 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9992 b->pspace = current_program_space;
8bea4e01 9993
bfccc43c 9994 install_breakpoint (internal, b, 0);
0101ce28
JJ
9995 }
9996
f8eba3c6 9997 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9998 {
3e43a32a
MS
9999 warning (_("Multiple breakpoints were set.\nUse the "
10000 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10001 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10002 }
10003
80c99de1
PA
10004 /* That's it. Discard the cleanups for data inserted into the
10005 breakpoint. */
10006 discard_cleanups (bkpt_chain);
10007 /* But cleanup everything else. */
c3f6f71d 10008 do_cleanups (old_chain);
217dc9e2 10009
80c99de1 10010 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 10011 update_global_location_list (1);
fd9b8c24
PA
10012
10013 return 1;
c3f6f71d 10014}
c906108c 10015
348d480f 10016/* Set a breakpoint.
72b2ff0e
VP
10017 ARG is a string describing breakpoint address,
10018 condition, and thread.
10019 FLAG specifies if a breakpoint is hardware on,
10020 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10021 and BP_TEMPFLAG. */
348d480f 10022
98deb0da 10023static void
72b2ff0e 10024break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10025{
72b2ff0e 10026 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10027 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10028 ? bp_hardware_breakpoint
10029 : bp_breakpoint);
55aa24fb
SDJ
10030 struct breakpoint_ops *ops;
10031 const char *arg_cp = arg;
10032
10033 /* Matching breakpoints on probes. */
10034 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10035 ops = &bkpt_probe_breakpoint_ops;
10036 else
10037 ops = &bkpt_breakpoint_ops;
c3f6f71d 10038
8cdf0e15
VP
10039 create_breakpoint (get_current_arch (),
10040 arg,
e7e0cddf 10041 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10042 tempflag, type_wanted,
8cdf0e15
VP
10043 0 /* Ignore count */,
10044 pending_break_support,
55aa24fb 10045 ops,
8cdf0e15 10046 from_tty,
84f4c1fe 10047 1 /* enabled */,
44f238bb
PA
10048 0 /* internal */,
10049 0);
c906108c
SS
10050}
10051
c906108c
SS
10052/* Helper function for break_command_1 and disassemble_command. */
10053
10054void
fba45db2 10055resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10056{
10057 CORE_ADDR pc;
10058
10059 if (sal->pc == 0 && sal->symtab != NULL)
10060 {
10061 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10062 error (_("No line %d in file \"%s\"."),
05cba821 10063 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10064 sal->pc = pc;
6a048695 10065
4a64f543
MS
10066 /* If this SAL corresponds to a breakpoint inserted using a line
10067 number, then skip the function prologue if necessary. */
6a048695 10068 if (sal->explicit_line)
059acae7 10069 skip_prologue_sal (sal);
c906108c
SS
10070 }
10071
10072 if (sal->section == 0 && sal->symtab != NULL)
10073 {
10074 struct blockvector *bv;
c5aa993b
JM
10075 struct block *b;
10076 struct symbol *sym;
c906108c 10077
801e3a5b 10078 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10079 if (bv != NULL)
10080 {
7f0df278 10081 sym = block_linkage_function (b);
c906108c
SS
10082 if (sym != NULL)
10083 {
10084 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10085 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10086 }
10087 else
10088 {
4a64f543
MS
10089 /* It really is worthwhile to have the section, so we'll
10090 just have to look harder. This case can be executed
10091 if we have line numbers but no functions (as can
10092 happen in assembly source). */
c906108c 10093
7cbd4a93 10094 struct bound_minimal_symbol msym;
6c95b8df
PA
10095 struct cleanup *old_chain = save_current_space_and_thread ();
10096
10097 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10098
10099 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10100 if (msym.minsym)
efd66ac6 10101 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10102
10103 do_cleanups (old_chain);
c906108c
SS
10104 }
10105 }
10106 }
10107}
10108
10109void
fba45db2 10110break_command (char *arg, int from_tty)
c906108c 10111{
db107f19 10112 break_command_1 (arg, 0, from_tty);
c906108c
SS
10113}
10114
c906108c 10115void
fba45db2 10116tbreak_command (char *arg, int from_tty)
c906108c 10117{
db107f19 10118 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10119}
10120
c906108c 10121static void
fba45db2 10122hbreak_command (char *arg, int from_tty)
c906108c 10123{
db107f19 10124 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10125}
10126
10127static void
fba45db2 10128thbreak_command (char *arg, int from_tty)
c906108c 10129{
db107f19 10130 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10131}
10132
10133static void
fba45db2 10134stop_command (char *arg, int from_tty)
c906108c 10135{
a3f17187 10136 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10137Usage: stop in <function | address>\n\
a3f17187 10138 stop at <line>\n"));
c906108c
SS
10139}
10140
10141static void
fba45db2 10142stopin_command (char *arg, int from_tty)
c906108c
SS
10143{
10144 int badInput = 0;
10145
c5aa993b 10146 if (arg == (char *) NULL)
c906108c
SS
10147 badInput = 1;
10148 else if (*arg != '*')
10149 {
10150 char *argptr = arg;
10151 int hasColon = 0;
10152
4a64f543 10153 /* Look for a ':'. If this is a line number specification, then
53a5351d 10154 say it is bad, otherwise, it should be an address or
4a64f543 10155 function/method name. */
c906108c 10156 while (*argptr && !hasColon)
c5aa993b
JM
10157 {
10158 hasColon = (*argptr == ':');
10159 argptr++;
10160 }
c906108c
SS
10161
10162 if (hasColon)
c5aa993b 10163 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10164 else
c5aa993b 10165 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10166 }
10167
10168 if (badInput)
a3f17187 10169 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10170 else
db107f19 10171 break_command_1 (arg, 0, from_tty);
c906108c
SS
10172}
10173
10174static void
fba45db2 10175stopat_command (char *arg, int from_tty)
c906108c
SS
10176{
10177 int badInput = 0;
10178
c5aa993b 10179 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10180 badInput = 1;
10181 else
10182 {
10183 char *argptr = arg;
10184 int hasColon = 0;
10185
4a64f543
MS
10186 /* Look for a ':'. If there is a '::' then get out, otherwise
10187 it is probably a line number. */
c906108c 10188 while (*argptr && !hasColon)
c5aa993b
JM
10189 {
10190 hasColon = (*argptr == ':');
10191 argptr++;
10192 }
c906108c
SS
10193
10194 if (hasColon)
c5aa993b 10195 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10196 else
c5aa993b 10197 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10198 }
10199
10200 if (badInput)
a3f17187 10201 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10202 else
db107f19 10203 break_command_1 (arg, 0, from_tty);
c906108c
SS
10204}
10205
e7e0cddf
SS
10206/* The dynamic printf command is mostly like a regular breakpoint, but
10207 with a prewired command list consisting of a single output command,
10208 built from extra arguments supplied on the dprintf command
10209 line. */
10210
da821c7b 10211static void
e7e0cddf
SS
10212dprintf_command (char *arg, int from_tty)
10213{
10214 create_breakpoint (get_current_arch (),
10215 arg,
10216 NULL, 0, NULL, 1 /* parse arg */,
10217 0, bp_dprintf,
10218 0 /* Ignore count */,
10219 pending_break_support,
10220 &dprintf_breakpoint_ops,
10221 from_tty,
10222 1 /* enabled */,
10223 0 /* internal */,
10224 0);
10225}
10226
d3ce09f5
SS
10227static void
10228agent_printf_command (char *arg, int from_tty)
10229{
10230 error (_("May only run agent-printf on the target"));
10231}
10232
f1310107
TJB
10233/* Implement the "breakpoint_hit" breakpoint_ops method for
10234 ranged breakpoints. */
10235
10236static int
10237breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10238 struct address_space *aspace,
09ac7c10
TT
10239 CORE_ADDR bp_addr,
10240 const struct target_waitstatus *ws)
f1310107 10241{
09ac7c10 10242 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10243 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10244 return 0;
10245
f1310107
TJB
10246 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10247 bl->length, aspace, bp_addr);
10248}
10249
10250/* Implement the "resources_needed" breakpoint_ops method for
10251 ranged breakpoints. */
10252
10253static int
10254resources_needed_ranged_breakpoint (const struct bp_location *bl)
10255{
10256 return target_ranged_break_num_registers ();
10257}
10258
10259/* Implement the "print_it" breakpoint_ops method for
10260 ranged breakpoints. */
10261
10262static enum print_stop_action
348d480f 10263print_it_ranged_breakpoint (bpstat bs)
f1310107 10264{
348d480f 10265 struct breakpoint *b = bs->breakpoint_at;
f1310107 10266 struct bp_location *bl = b->loc;
79a45e25 10267 struct ui_out *uiout = current_uiout;
f1310107
TJB
10268
10269 gdb_assert (b->type == bp_hardware_breakpoint);
10270
10271 /* Ranged breakpoints have only one location. */
10272 gdb_assert (bl && bl->next == NULL);
10273
10274 annotate_breakpoint (b->number);
10275 if (b->disposition == disp_del)
10276 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10277 else
10278 ui_out_text (uiout, "\nRanged breakpoint ");
10279 if (ui_out_is_mi_like_p (uiout))
10280 {
10281 ui_out_field_string (uiout, "reason",
10282 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10283 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10284 }
10285 ui_out_field_int (uiout, "bkptno", b->number);
10286 ui_out_text (uiout, ", ");
10287
10288 return PRINT_SRC_AND_LOC;
10289}
10290
10291/* Implement the "print_one" breakpoint_ops method for
10292 ranged breakpoints. */
10293
10294static void
10295print_one_ranged_breakpoint (struct breakpoint *b,
10296 struct bp_location **last_loc)
10297{
10298 struct bp_location *bl = b->loc;
10299 struct value_print_options opts;
79a45e25 10300 struct ui_out *uiout = current_uiout;
f1310107
TJB
10301
10302 /* Ranged breakpoints have only one location. */
10303 gdb_assert (bl && bl->next == NULL);
10304
10305 get_user_print_options (&opts);
10306
10307 if (opts.addressprint)
10308 /* We don't print the address range here, it will be printed later
10309 by print_one_detail_ranged_breakpoint. */
10310 ui_out_field_skip (uiout, "addr");
10311 annotate_field (5);
10312 print_breakpoint_location (b, bl);
10313 *last_loc = bl;
10314}
10315
10316/* Implement the "print_one_detail" breakpoint_ops method for
10317 ranged breakpoints. */
10318
10319static void
10320print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10321 struct ui_out *uiout)
10322{
10323 CORE_ADDR address_start, address_end;
10324 struct bp_location *bl = b->loc;
f99d8bf4
PA
10325 struct ui_file *stb = mem_fileopen ();
10326 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10327
10328 gdb_assert (bl);
10329
10330 address_start = bl->address;
10331 address_end = address_start + bl->length - 1;
10332
10333 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10334 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10335 print_core_address (bl->gdbarch, address_start),
10336 print_core_address (bl->gdbarch, address_end));
10337 ui_out_field_stream (uiout, "addr", stb);
10338 ui_out_text (uiout, "\n");
10339
10340 do_cleanups (cleanup);
10341}
10342
10343/* Implement the "print_mention" breakpoint_ops method for
10344 ranged breakpoints. */
10345
10346static void
10347print_mention_ranged_breakpoint (struct breakpoint *b)
10348{
10349 struct bp_location *bl = b->loc;
79a45e25 10350 struct ui_out *uiout = current_uiout;
f1310107
TJB
10351
10352 gdb_assert (bl);
10353 gdb_assert (b->type == bp_hardware_breakpoint);
10354
10355 if (ui_out_is_mi_like_p (uiout))
10356 return;
10357
10358 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10359 b->number, paddress (bl->gdbarch, bl->address),
10360 paddress (bl->gdbarch, bl->address + bl->length - 1));
10361}
10362
10363/* Implement the "print_recreate" breakpoint_ops method for
10364 ranged breakpoints. */
10365
10366static void
10367print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10368{
10369 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10370 b->addr_string_range_end);
d9b3f62e 10371 print_recreate_thread (b, fp);
f1310107
TJB
10372}
10373
10374/* The breakpoint_ops structure to be used in ranged breakpoints. */
10375
2060206e 10376static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10377
10378/* Find the address where the end of the breakpoint range should be
10379 placed, given the SAL of the end of the range. This is so that if
10380 the user provides a line number, the end of the range is set to the
10381 last instruction of the given line. */
10382
10383static CORE_ADDR
10384find_breakpoint_range_end (struct symtab_and_line sal)
10385{
10386 CORE_ADDR end;
10387
10388 /* If the user provided a PC value, use it. Otherwise,
10389 find the address of the end of the given location. */
10390 if (sal.explicit_pc)
10391 end = sal.pc;
10392 else
10393 {
10394 int ret;
10395 CORE_ADDR start;
10396
10397 ret = find_line_pc_range (sal, &start, &end);
10398 if (!ret)
10399 error (_("Could not find location of the end of the range."));
10400
10401 /* find_line_pc_range returns the start of the next line. */
10402 end--;
10403 }
10404
10405 return end;
10406}
10407
10408/* Implement the "break-range" CLI command. */
10409
10410static void
10411break_range_command (char *arg, int from_tty)
10412{
10413 char *arg_start, *addr_string_start, *addr_string_end;
10414 struct linespec_result canonical_start, canonical_end;
10415 int bp_count, can_use_bp, length;
10416 CORE_ADDR end;
10417 struct breakpoint *b;
10418 struct symtab_and_line sal_start, sal_end;
f1310107 10419 struct cleanup *cleanup_bkpt;
f8eba3c6 10420 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10421
10422 /* We don't support software ranged breakpoints. */
10423 if (target_ranged_break_num_registers () < 0)
10424 error (_("This target does not support hardware ranged breakpoints."));
10425
10426 bp_count = hw_breakpoint_used_count ();
10427 bp_count += target_ranged_break_num_registers ();
10428 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10429 bp_count, 0);
10430 if (can_use_bp < 0)
10431 error (_("Hardware breakpoints used exceeds limit."));
10432
f8eba3c6 10433 arg = skip_spaces (arg);
f1310107
TJB
10434 if (arg == NULL || arg[0] == '\0')
10435 error(_("No address range specified."));
10436
f1310107
TJB
10437 init_linespec_result (&canonical_start);
10438
f8eba3c6
TT
10439 arg_start = arg;
10440 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10441
f8eba3c6 10442 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10443
10444 if (arg[0] != ',')
10445 error (_("Too few arguments."));
f8eba3c6 10446 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10447 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10448
10449 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10450
10451 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10452 || lsal_start->sals.nelts != 1)
f1310107
TJB
10453 error (_("Cannot create a ranged breakpoint with multiple locations."));
10454
f8eba3c6
TT
10455 sal_start = lsal_start->sals.sals[0];
10456 addr_string_start = savestring (arg_start, arg - arg_start);
10457 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10458
10459 arg++; /* Skip the comma. */
f8eba3c6 10460 arg = skip_spaces (arg);
f1310107
TJB
10461
10462 /* Parse the end location. */
10463
f1310107
TJB
10464 init_linespec_result (&canonical_end);
10465 arg_start = arg;
10466
f8eba3c6 10467 /* We call decode_line_full directly here instead of using
f1310107
TJB
10468 parse_breakpoint_sals because we need to specify the start location's
10469 symtab and line as the default symtab and line for the end of the
10470 range. This makes it possible to have ranges like "foo.c:27, +14",
10471 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10472 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10473 sal_start.symtab, sal_start.line,
10474 &canonical_end, NULL, NULL);
10475
10476 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10477
f8eba3c6 10478 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10479 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10480
10481 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10482 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10483 || lsal_end->sals.nelts != 1)
f1310107
TJB
10484 error (_("Cannot create a ranged breakpoint with multiple locations."));
10485
f8eba3c6
TT
10486 sal_end = lsal_end->sals.sals[0];
10487 addr_string_end = savestring (arg_start, arg - arg_start);
10488 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10489
10490 end = find_breakpoint_range_end (sal_end);
10491 if (sal_start.pc > end)
177b42fe 10492 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10493
10494 length = end - sal_start.pc + 1;
10495 if (length < 0)
10496 /* Length overflowed. */
10497 error (_("Address range too large."));
10498 else if (length == 1)
10499 {
10500 /* This range is simple enough to be handled by
10501 the `hbreak' command. */
10502 hbreak_command (addr_string_start, 1);
10503
10504 do_cleanups (cleanup_bkpt);
10505
10506 return;
10507 }
10508
10509 /* Now set up the breakpoint. */
10510 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10511 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10512 set_breakpoint_count (breakpoint_count + 1);
10513 b->number = breakpoint_count;
10514 b->disposition = disp_donttouch;
f8eba3c6
TT
10515 b->addr_string = xstrdup (addr_string_start);
10516 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10517 b->loc->length = length;
10518
f8eba3c6 10519 do_cleanups (cleanup_bkpt);
f1310107
TJB
10520
10521 mention (b);
8d3788bd 10522 observer_notify_breakpoint_created (b);
f1310107
TJB
10523 update_global_location_list (1);
10524}
10525
4a64f543
MS
10526/* Return non-zero if EXP is verified as constant. Returned zero
10527 means EXP is variable. Also the constant detection may fail for
10528 some constant expressions and in such case still falsely return
10529 zero. */
2e6e3d9c 10530
65d79d4b
SDJ
10531static int
10532watchpoint_exp_is_const (const struct expression *exp)
10533{
10534 int i = exp->nelts;
10535
10536 while (i > 0)
10537 {
10538 int oplenp, argsp;
10539
10540 /* We are only interested in the descriptor of each element. */
10541 operator_length (exp, i, &oplenp, &argsp);
10542 i -= oplenp;
10543
10544 switch (exp->elts[i].opcode)
10545 {
10546 case BINOP_ADD:
10547 case BINOP_SUB:
10548 case BINOP_MUL:
10549 case BINOP_DIV:
10550 case BINOP_REM:
10551 case BINOP_MOD:
10552 case BINOP_LSH:
10553 case BINOP_RSH:
10554 case BINOP_LOGICAL_AND:
10555 case BINOP_LOGICAL_OR:
10556 case BINOP_BITWISE_AND:
10557 case BINOP_BITWISE_IOR:
10558 case BINOP_BITWISE_XOR:
10559 case BINOP_EQUAL:
10560 case BINOP_NOTEQUAL:
10561 case BINOP_LESS:
10562 case BINOP_GTR:
10563 case BINOP_LEQ:
10564 case BINOP_GEQ:
10565 case BINOP_REPEAT:
10566 case BINOP_COMMA:
10567 case BINOP_EXP:
10568 case BINOP_MIN:
10569 case BINOP_MAX:
10570 case BINOP_INTDIV:
10571 case BINOP_CONCAT:
10572 case BINOP_IN:
10573 case BINOP_RANGE:
10574 case TERNOP_COND:
10575 case TERNOP_SLICE:
65d79d4b
SDJ
10576
10577 case OP_LONG:
10578 case OP_DOUBLE:
10579 case OP_DECFLOAT:
10580 case OP_LAST:
10581 case OP_COMPLEX:
10582 case OP_STRING:
65d79d4b
SDJ
10583 case OP_ARRAY:
10584 case OP_TYPE:
608b4967
TT
10585 case OP_TYPEOF:
10586 case OP_DECLTYPE:
6e72ca20 10587 case OP_TYPEID:
65d79d4b
SDJ
10588 case OP_NAME:
10589 case OP_OBJC_NSSTRING:
10590
10591 case UNOP_NEG:
10592 case UNOP_LOGICAL_NOT:
10593 case UNOP_COMPLEMENT:
10594 case UNOP_ADDR:
10595 case UNOP_HIGH:
aeaa2474 10596 case UNOP_CAST:
9eaf6705
TT
10597
10598 case UNOP_CAST_TYPE:
10599 case UNOP_REINTERPRET_CAST:
10600 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10601 /* Unary, binary and ternary operators: We have to check
10602 their operands. If they are constant, then so is the
10603 result of that operation. For instance, if A and B are
10604 determined to be constants, then so is "A + B".
10605
10606 UNOP_IND is one exception to the rule above, because the
10607 value of *ADDR is not necessarily a constant, even when
10608 ADDR is. */
65d79d4b
SDJ
10609 break;
10610
10611 case OP_VAR_VALUE:
10612 /* Check whether the associated symbol is a constant.
4a64f543 10613
65d79d4b 10614 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10615 possible that a buggy compiler could mark a variable as
10616 constant even when it is not, and TYPE_CONST would return
10617 true in this case, while SYMBOL_CLASS wouldn't.
10618
10619 We also have to check for function symbols because they
10620 are always constant. */
65d79d4b
SDJ
10621 {
10622 struct symbol *s = exp->elts[i + 2].symbol;
10623
10624 if (SYMBOL_CLASS (s) != LOC_BLOCK
10625 && SYMBOL_CLASS (s) != LOC_CONST
10626 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10627 return 0;
10628 break;
10629 }
10630
10631 /* The default action is to return 0 because we are using
10632 the optimistic approach here: If we don't know something,
10633 then it is not a constant. */
10634 default:
10635 return 0;
10636 }
10637 }
10638
10639 return 1;
10640}
10641
3a5c3e22
PA
10642/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10643
10644static void
10645dtor_watchpoint (struct breakpoint *self)
10646{
10647 struct watchpoint *w = (struct watchpoint *) self;
10648
10649 xfree (w->cond_exp);
10650 xfree (w->exp);
10651 xfree (w->exp_string);
10652 xfree (w->exp_string_reparse);
10653 value_free (w->val);
10654
10655 base_breakpoint_ops.dtor (self);
10656}
10657
348d480f
PA
10658/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10659
10660static void
10661re_set_watchpoint (struct breakpoint *b)
10662{
3a5c3e22
PA
10663 struct watchpoint *w = (struct watchpoint *) b;
10664
348d480f
PA
10665 /* Watchpoint can be either on expression using entirely global
10666 variables, or it can be on local variables.
10667
10668 Watchpoints of the first kind are never auto-deleted, and even
10669 persist across program restarts. Since they can use variables
10670 from shared libraries, we need to reparse expression as libraries
10671 are loaded and unloaded.
10672
10673 Watchpoints on local variables can also change meaning as result
10674 of solib event. For example, if a watchpoint uses both a local
10675 and a global variables in expression, it's a local watchpoint,
10676 but unloading of a shared library will make the expression
10677 invalid. This is not a very common use case, but we still
10678 re-evaluate expression, to avoid surprises to the user.
10679
10680 Note that for local watchpoints, we re-evaluate it only if
10681 watchpoints frame id is still valid. If it's not, it means the
10682 watchpoint is out of scope and will be deleted soon. In fact,
10683 I'm not sure we'll ever be called in this case.
10684
10685 If a local watchpoint's frame id is still valid, then
3a5c3e22 10686 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10687
3a5c3e22
PA
10688 Don't do anything about disabled watchpoints, since they will be
10689 reevaluated again when enabled. */
10690 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10691}
10692
77b06cd7
TJB
10693/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10694
10695static int
10696insert_watchpoint (struct bp_location *bl)
10697{
3a5c3e22
PA
10698 struct watchpoint *w = (struct watchpoint *) bl->owner;
10699 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10700
10701 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10702 w->cond_exp);
77b06cd7
TJB
10703}
10704
10705/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10706
10707static int
10708remove_watchpoint (struct bp_location *bl)
10709{
3a5c3e22
PA
10710 struct watchpoint *w = (struct watchpoint *) bl->owner;
10711 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10712
10713 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10714 w->cond_exp);
e09342b5
TJB
10715}
10716
e09342b5 10717static int
348d480f 10718breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10719 struct address_space *aspace, CORE_ADDR bp_addr,
10720 const struct target_waitstatus *ws)
e09342b5 10721{
348d480f 10722 struct breakpoint *b = bl->owner;
3a5c3e22 10723 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10724
348d480f
PA
10725 /* Continuable hardware watchpoints are treated as non-existent if the
10726 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10727 some data address). Otherwise gdb won't stop on a break instruction
10728 in the code (not from a breakpoint) when a hardware watchpoint has
10729 been defined. Also skip watchpoints which we know did not trigger
10730 (did not match the data address). */
10731 if (is_hardware_watchpoint (b)
3a5c3e22 10732 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10733 return 0;
9c06b0b4 10734
348d480f 10735 return 1;
9c06b0b4
TJB
10736}
10737
348d480f
PA
10738static void
10739check_status_watchpoint (bpstat bs)
9c06b0b4 10740{
348d480f 10741 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10742
348d480f 10743 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10744}
10745
10746/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10747 hardware watchpoints. */
9c06b0b4
TJB
10748
10749static int
348d480f 10750resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10751{
3a5c3e22
PA
10752 struct watchpoint *w = (struct watchpoint *) bl->owner;
10753 int length = w->exact? 1 : bl->length;
348d480f
PA
10754
10755 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10756}
10757
10758/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10759 hardware watchpoints. */
9c06b0b4
TJB
10760
10761static int
348d480f 10762works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10763{
efa80663
PA
10764 /* Read and access watchpoints only work with hardware support. */
10765 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10766}
10767
9c06b0b4 10768static enum print_stop_action
348d480f 10769print_it_watchpoint (bpstat bs)
9c06b0b4 10770{
348d480f
PA
10771 struct cleanup *old_chain;
10772 struct breakpoint *b;
f99d8bf4 10773 struct ui_file *stb;
348d480f 10774 enum print_stop_action result;
3a5c3e22 10775 struct watchpoint *w;
79a45e25 10776 struct ui_out *uiout = current_uiout;
348d480f
PA
10777
10778 gdb_assert (bs->bp_location_at != NULL);
10779
348d480f 10780 b = bs->breakpoint_at;
3a5c3e22 10781 w = (struct watchpoint *) b;
348d480f 10782
f99d8bf4
PA
10783 stb = mem_fileopen ();
10784 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10785
10786 switch (b->type)
10787 {
348d480f 10788 case bp_watchpoint:
9c06b0b4
TJB
10789 case bp_hardware_watchpoint:
10790 annotate_watchpoint (b->number);
10791 if (ui_out_is_mi_like_p (uiout))
10792 ui_out_field_string
10793 (uiout, "reason",
10794 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10795 mention (b);
10796 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10797 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10798 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10799 ui_out_field_stream (uiout, "old", stb);
10800 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10801 watchpoint_value_print (w->val, stb);
348d480f
PA
10802 ui_out_field_stream (uiout, "new", stb);
10803 ui_out_text (uiout, "\n");
10804 /* More than one watchpoint may have been triggered. */
10805 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10806 break;
10807
10808 case bp_read_watchpoint:
10809 if (ui_out_is_mi_like_p (uiout))
10810 ui_out_field_string
10811 (uiout, "reason",
10812 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10813 mention (b);
10814 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10815 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10816 watchpoint_value_print (w->val, stb);
348d480f
PA
10817 ui_out_field_stream (uiout, "value", stb);
10818 ui_out_text (uiout, "\n");
10819 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10820 break;
10821
10822 case bp_access_watchpoint:
348d480f
PA
10823 if (bs->old_val != NULL)
10824 {
10825 annotate_watchpoint (b->number);
10826 if (ui_out_is_mi_like_p (uiout))
10827 ui_out_field_string
10828 (uiout, "reason",
10829 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10830 mention (b);
10831 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10832 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10833 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10834 ui_out_field_stream (uiout, "old", stb);
10835 ui_out_text (uiout, "\nNew value = ");
10836 }
10837 else
10838 {
10839 mention (b);
10840 if (ui_out_is_mi_like_p (uiout))
10841 ui_out_field_string
10842 (uiout, "reason",
10843 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10844 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10845 ui_out_text (uiout, "\nValue = ");
10846 }
f99d8bf4 10847 watchpoint_value_print (w->val, stb);
348d480f
PA
10848 ui_out_field_stream (uiout, "new", stb);
10849 ui_out_text (uiout, "\n");
10850 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10851 break;
10852 default:
348d480f 10853 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10854 }
10855
348d480f
PA
10856 do_cleanups (old_chain);
10857 return result;
10858}
10859
10860/* Implement the "print_mention" breakpoint_ops method for hardware
10861 watchpoints. */
10862
10863static void
10864print_mention_watchpoint (struct breakpoint *b)
10865{
10866 struct cleanup *ui_out_chain;
3a5c3e22 10867 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10868 struct ui_out *uiout = current_uiout;
348d480f
PA
10869
10870 switch (b->type)
10871 {
10872 case bp_watchpoint:
10873 ui_out_text (uiout, "Watchpoint ");
10874 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10875 break;
10876 case bp_hardware_watchpoint:
10877 ui_out_text (uiout, "Hardware watchpoint ");
10878 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10879 break;
10880 case bp_read_watchpoint:
10881 ui_out_text (uiout, "Hardware read watchpoint ");
10882 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10883 break;
10884 case bp_access_watchpoint:
10885 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10886 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10887 break;
10888 default:
10889 internal_error (__FILE__, __LINE__,
10890 _("Invalid hardware watchpoint type."));
10891 }
10892
10893 ui_out_field_int (uiout, "number", b->number);
10894 ui_out_text (uiout, ": ");
3a5c3e22 10895 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10896 do_cleanups (ui_out_chain);
10897}
10898
10899/* Implement the "print_recreate" breakpoint_ops method for
10900 watchpoints. */
10901
10902static void
10903print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10904{
3a5c3e22
PA
10905 struct watchpoint *w = (struct watchpoint *) b;
10906
348d480f
PA
10907 switch (b->type)
10908 {
10909 case bp_watchpoint:
10910 case bp_hardware_watchpoint:
10911 fprintf_unfiltered (fp, "watch");
10912 break;
10913 case bp_read_watchpoint:
10914 fprintf_unfiltered (fp, "rwatch");
10915 break;
10916 case bp_access_watchpoint:
10917 fprintf_unfiltered (fp, "awatch");
10918 break;
10919 default:
10920 internal_error (__FILE__, __LINE__,
10921 _("Invalid watchpoint type."));
10922 }
10923
3a5c3e22 10924 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10925 print_recreate_thread (b, fp);
348d480f
PA
10926}
10927
427cd150
TT
10928/* Implement the "explains_signal" breakpoint_ops method for
10929 watchpoints. */
10930
47591c29 10931static int
427cd150
TT
10932explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10933{
10934 /* A software watchpoint cannot cause a signal other than
10935 GDB_SIGNAL_TRAP. */
10936 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10937 return 0;
427cd150 10938
47591c29 10939 return 1;
427cd150
TT
10940}
10941
348d480f
PA
10942/* The breakpoint_ops structure to be used in hardware watchpoints. */
10943
2060206e 10944static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10945
10946/* Implement the "insert" breakpoint_ops method for
10947 masked hardware watchpoints. */
10948
10949static int
10950insert_masked_watchpoint (struct bp_location *bl)
10951{
3a5c3e22
PA
10952 struct watchpoint *w = (struct watchpoint *) bl->owner;
10953
10954 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10955 bl->watchpoint_type);
10956}
10957
10958/* Implement the "remove" breakpoint_ops method for
10959 masked hardware watchpoints. */
10960
10961static int
10962remove_masked_watchpoint (struct bp_location *bl)
10963{
3a5c3e22
PA
10964 struct watchpoint *w = (struct watchpoint *) bl->owner;
10965
10966 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10967 bl->watchpoint_type);
10968}
10969
10970/* Implement the "resources_needed" breakpoint_ops method for
10971 masked hardware watchpoints. */
10972
10973static int
10974resources_needed_masked_watchpoint (const struct bp_location *bl)
10975{
3a5c3e22
PA
10976 struct watchpoint *w = (struct watchpoint *) bl->owner;
10977
10978 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10979}
10980
10981/* Implement the "works_in_software_mode" breakpoint_ops method for
10982 masked hardware watchpoints. */
10983
10984static int
10985works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10986{
10987 return 0;
10988}
10989
10990/* Implement the "print_it" breakpoint_ops method for
10991 masked hardware watchpoints. */
10992
10993static enum print_stop_action
10994print_it_masked_watchpoint (bpstat bs)
10995{
10996 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10997 struct ui_out *uiout = current_uiout;
348d480f
PA
10998
10999 /* Masked watchpoints have only one location. */
11000 gdb_assert (b->loc && b->loc->next == NULL);
11001
11002 switch (b->type)
11003 {
11004 case bp_hardware_watchpoint:
11005 annotate_watchpoint (b->number);
11006 if (ui_out_is_mi_like_p (uiout))
11007 ui_out_field_string
11008 (uiout, "reason",
11009 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11010 break;
11011
11012 case bp_read_watchpoint:
11013 if (ui_out_is_mi_like_p (uiout))
11014 ui_out_field_string
11015 (uiout, "reason",
11016 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11017 break;
11018
11019 case bp_access_watchpoint:
11020 if (ui_out_is_mi_like_p (uiout))
11021 ui_out_field_string
11022 (uiout, "reason",
11023 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11024 break;
11025 default:
11026 internal_error (__FILE__, __LINE__,
11027 _("Invalid hardware watchpoint type."));
11028 }
11029
11030 mention (b);
9c06b0b4
TJB
11031 ui_out_text (uiout, _("\n\
11032Check the underlying instruction at PC for the memory\n\
11033address and value which triggered this watchpoint.\n"));
11034 ui_out_text (uiout, "\n");
11035
11036 /* More than one watchpoint may have been triggered. */
11037 return PRINT_UNKNOWN;
11038}
11039
11040/* Implement the "print_one_detail" breakpoint_ops method for
11041 masked hardware watchpoints. */
11042
11043static void
11044print_one_detail_masked_watchpoint (const struct breakpoint *b,
11045 struct ui_out *uiout)
11046{
3a5c3e22
PA
11047 struct watchpoint *w = (struct watchpoint *) b;
11048
9c06b0b4
TJB
11049 /* Masked watchpoints have only one location. */
11050 gdb_assert (b->loc && b->loc->next == NULL);
11051
11052 ui_out_text (uiout, "\tmask ");
3a5c3e22 11053 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11054 ui_out_text (uiout, "\n");
11055}
11056
11057/* Implement the "print_mention" breakpoint_ops method for
11058 masked hardware watchpoints. */
11059
11060static void
11061print_mention_masked_watchpoint (struct breakpoint *b)
11062{
3a5c3e22 11063 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11064 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11065 struct cleanup *ui_out_chain;
11066
11067 switch (b->type)
11068 {
11069 case bp_hardware_watchpoint:
11070 ui_out_text (uiout, "Masked hardware watchpoint ");
11071 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11072 break;
11073 case bp_read_watchpoint:
11074 ui_out_text (uiout, "Masked hardware read watchpoint ");
11075 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11076 break;
11077 case bp_access_watchpoint:
11078 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11080 break;
11081 default:
11082 internal_error (__FILE__, __LINE__,
11083 _("Invalid hardware watchpoint type."));
11084 }
11085
11086 ui_out_field_int (uiout, "number", b->number);
11087 ui_out_text (uiout, ": ");
3a5c3e22 11088 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11089 do_cleanups (ui_out_chain);
11090}
11091
11092/* Implement the "print_recreate" breakpoint_ops method for
11093 masked hardware watchpoints. */
11094
11095static void
11096print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11097{
3a5c3e22 11098 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11099 char tmp[40];
11100
11101 switch (b->type)
11102 {
11103 case bp_hardware_watchpoint:
11104 fprintf_unfiltered (fp, "watch");
11105 break;
11106 case bp_read_watchpoint:
11107 fprintf_unfiltered (fp, "rwatch");
11108 break;
11109 case bp_access_watchpoint:
11110 fprintf_unfiltered (fp, "awatch");
11111 break;
11112 default:
11113 internal_error (__FILE__, __LINE__,
11114 _("Invalid hardware watchpoint type."));
11115 }
11116
3a5c3e22
PA
11117 sprintf_vma (tmp, w->hw_wp_mask);
11118 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11119 print_recreate_thread (b, fp);
9c06b0b4
TJB
11120}
11121
11122/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11123
2060206e 11124static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11125
11126/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11127
11128static int
11129is_masked_watchpoint (const struct breakpoint *b)
11130{
11131 return b->ops == &masked_watchpoint_breakpoint_ops;
11132}
11133
53a5351d
JM
11134/* accessflag: hw_write: watch write,
11135 hw_read: watch read,
11136 hw_access: watch access (read or write) */
c906108c 11137static void
bbc13ae3 11138watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11139 int just_location, int internal)
c906108c 11140{
a9634178 11141 volatile struct gdb_exception e;
d983da9c 11142 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11143 struct expression *exp;
270140bd 11144 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11145 struct value *val, *mark, *result;
c906108c 11146 struct frame_info *frame;
bbc13ae3
KS
11147 const char *exp_start = NULL;
11148 const char *exp_end = NULL;
11149 const char *tok, *end_tok;
9c06b0b4 11150 int toklen = -1;
bbc13ae3
KS
11151 const char *cond_start = NULL;
11152 const char *cond_end = NULL;
c906108c 11153 enum bptype bp_type;
37e4754d 11154 int thread = -1;
0cf6dd15 11155 int pc = 0;
9c06b0b4
TJB
11156 /* Flag to indicate whether we are going to use masks for
11157 the hardware watchpoint. */
11158 int use_mask = 0;
11159 CORE_ADDR mask = 0;
3a5c3e22 11160 struct watchpoint *w;
bbc13ae3
KS
11161 char *expression;
11162 struct cleanup *back_to;
c906108c 11163
37e4754d
LM
11164 /* Make sure that we actually have parameters to parse. */
11165 if (arg != NULL && arg[0] != '\0')
11166 {
bbc13ae3
KS
11167 const char *value_start;
11168
11169 exp_end = arg + strlen (arg);
37e4754d 11170
9c06b0b4
TJB
11171 /* Look for "parameter value" pairs at the end
11172 of the arguments string. */
bbc13ae3 11173 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11174 {
11175 /* Skip whitespace at the end of the argument list. */
11176 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11177 tok--;
11178
11179 /* Find the beginning of the last token.
11180 This is the value of the parameter. */
11181 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11182 tok--;
11183 value_start = tok + 1;
11184
11185 /* Skip whitespace. */
11186 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11187 tok--;
11188
11189 end_tok = tok;
11190
11191 /* Find the beginning of the second to last token.
11192 This is the parameter itself. */
11193 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11194 tok--;
11195 tok++;
11196 toklen = end_tok - tok + 1;
11197
11198 if (toklen == 6 && !strncmp (tok, "thread", 6))
11199 {
11200 /* At this point we've found a "thread" token, which means
11201 the user is trying to set a watchpoint that triggers
11202 only in a specific thread. */
11203 char *endp;
37e4754d 11204
9c06b0b4
TJB
11205 if (thread != -1)
11206 error(_("You can specify only one thread."));
37e4754d 11207
9c06b0b4
TJB
11208 /* Extract the thread ID from the next token. */
11209 thread = strtol (value_start, &endp, 0);
37e4754d 11210
9c06b0b4
TJB
11211 /* Check if the user provided a valid numeric value for the
11212 thread ID. */
11213 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11214 error (_("Invalid thread ID specification %s."), value_start);
11215
11216 /* Check if the thread actually exists. */
11217 if (!valid_thread_id (thread))
af4908ba 11218 invalid_thread_id_error (thread);
9c06b0b4
TJB
11219 }
11220 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11221 {
11222 /* We've found a "mask" token, which means the user wants to
11223 create a hardware watchpoint that is going to have the mask
11224 facility. */
11225 struct value *mask_value, *mark;
37e4754d 11226
9c06b0b4
TJB
11227 if (use_mask)
11228 error(_("You can specify only one mask."));
37e4754d 11229
9c06b0b4 11230 use_mask = just_location = 1;
37e4754d 11231
9c06b0b4
TJB
11232 mark = value_mark ();
11233 mask_value = parse_to_comma_and_eval (&value_start);
11234 mask = value_as_address (mask_value);
11235 value_free_to_mark (mark);
11236 }
11237 else
11238 /* We didn't recognize what we found. We should stop here. */
11239 break;
37e4754d 11240
9c06b0b4
TJB
11241 /* Truncate the string and get rid of the "parameter value" pair before
11242 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11243 exp_end = tok;
9c06b0b4 11244 }
37e4754d 11245 }
bbc13ae3
KS
11246 else
11247 exp_end = arg;
37e4754d 11248
bbc13ae3
KS
11249 /* Parse the rest of the arguments. From here on out, everything
11250 is in terms of a newly allocated string instead of the original
11251 ARG. */
c906108c 11252 innermost_block = NULL;
bbc13ae3
KS
11253 expression = savestring (arg, exp_end - arg);
11254 back_to = make_cleanup (xfree, expression);
11255 exp_start = arg = expression;
1bb9788d 11256 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11257 exp_end = arg;
fa8a61dc
TT
11258 /* Remove trailing whitespace from the expression before saving it.
11259 This makes the eventual display of the expression string a bit
11260 prettier. */
11261 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11262 --exp_end;
11263
65d79d4b
SDJ
11264 /* Checking if the expression is not constant. */
11265 if (watchpoint_exp_is_const (exp))
11266 {
11267 int len;
11268
11269 len = exp_end - exp_start;
11270 while (len > 0 && isspace (exp_start[len - 1]))
11271 len--;
11272 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11273 }
11274
c906108c
SS
11275 exp_valid_block = innermost_block;
11276 mark = value_mark ();
3a1115a0 11277 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11278
11279 if (just_location)
11280 {
9c06b0b4
TJB
11281 int ret;
11282
06a64a0b 11283 exp_valid_block = NULL;
a1442452 11284 val = value_addr (result);
06a64a0b
TT
11285 release_value (val);
11286 value_free_to_mark (mark);
9c06b0b4
TJB
11287
11288 if (use_mask)
11289 {
11290 ret = target_masked_watch_num_registers (value_as_address (val),
11291 mask);
11292 if (ret == -1)
11293 error (_("This target does not support masked watchpoints."));
11294 else if (ret == -2)
11295 error (_("Invalid mask or memory region."));
11296 }
06a64a0b
TT
11297 }
11298 else if (val != NULL)
fa4727a6 11299 release_value (val);
c906108c 11300
bbc13ae3
KS
11301 tok = skip_spaces_const (arg);
11302 end_tok = skip_to_space_const (tok);
c906108c
SS
11303
11304 toklen = end_tok - tok;
11305 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11306 {
2d134ed3
PA
11307 struct expression *cond;
11308
60e1c644 11309 innermost_block = NULL;
c906108c 11310 tok = cond_start = end_tok + 1;
1bb9788d 11311 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11312
11313 /* The watchpoint expression may not be local, but the condition
11314 may still be. E.g.: `watch global if local > 0'. */
11315 cond_exp_valid_block = innermost_block;
11316
2d134ed3 11317 xfree (cond);
c906108c
SS
11318 cond_end = tok;
11319 }
11320 if (*tok)
8a3fe4f8 11321 error (_("Junk at end of command."));
c906108c 11322
d983da9c 11323 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11324
11325 /* If the expression is "local", then set up a "watchpoint scope"
11326 breakpoint at the point where we've left the scope of the watchpoint
11327 expression. Create the scope breakpoint before the watchpoint, so
11328 that we will encounter it first in bpstat_stop_status. */
60e1c644 11329 if (exp_valid_block && frame)
d983da9c 11330 {
edb3359d
DJ
11331 if (frame_id_p (frame_unwind_caller_id (frame)))
11332 {
11333 scope_breakpoint
a6d9a66e
UW
11334 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11335 frame_unwind_caller_pc (frame),
06edf0c0
PA
11336 bp_watchpoint_scope,
11337 &momentary_breakpoint_ops);
d983da9c 11338
edb3359d 11339 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11340
edb3359d
DJ
11341 /* Automatically delete the breakpoint when it hits. */
11342 scope_breakpoint->disposition = disp_del;
d983da9c 11343
edb3359d
DJ
11344 /* Only break in the proper frame (help with recursion). */
11345 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11346
edb3359d 11347 /* Set the address at which we will stop. */
a6d9a66e
UW
11348 scope_breakpoint->loc->gdbarch
11349 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11350 scope_breakpoint->loc->requested_address
11351 = frame_unwind_caller_pc (frame);
11352 scope_breakpoint->loc->address
a6d9a66e
UW
11353 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11354 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11355 scope_breakpoint->type);
11356 }
d983da9c
DJ
11357 }
11358
e8369a73
AB
11359 /* Now set up the breakpoint. We create all watchpoints as hardware
11360 watchpoints here even if hardware watchpoints are turned off, a call
11361 to update_watchpoint later in this function will cause the type to
11362 drop back to bp_watchpoint (software watchpoint) if required. */
11363
11364 if (accessflag == hw_read)
11365 bp_type = bp_read_watchpoint;
11366 else if (accessflag == hw_access)
11367 bp_type = bp_access_watchpoint;
11368 else
11369 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11370
11371 w = XCNEW (struct watchpoint);
11372 b = &w->base;
348d480f 11373 if (use_mask)
3a5c3e22
PA
11374 init_raw_breakpoint_without_location (b, NULL, bp_type,
11375 &masked_watchpoint_breakpoint_ops);
348d480f 11376 else
3a5c3e22
PA
11377 init_raw_breakpoint_without_location (b, NULL, bp_type,
11378 &watchpoint_breakpoint_ops);
37e4754d 11379 b->thread = thread;
b5de0fa7 11380 b->disposition = disp_donttouch;
348d480f 11381 b->pspace = current_program_space;
3a5c3e22
PA
11382 w->exp = exp;
11383 w->exp_valid_block = exp_valid_block;
11384 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11385 if (just_location)
11386 {
11387 struct type *t = value_type (val);
11388 CORE_ADDR addr = value_as_address (val);
11389 char *name;
11390
11391 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11392 name = type_to_string (t);
11393
3a5c3e22 11394 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11395 core_addr_to_string (addr));
06a64a0b
TT
11396 xfree (name);
11397
3a5c3e22 11398 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11399 (int) (exp_end - exp_start), exp_start);
11400
06a64a0b
TT
11401 /* The above expression is in C. */
11402 b->language = language_c;
11403 }
11404 else
3a5c3e22 11405 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11406
11407 if (use_mask)
11408 {
3a5c3e22 11409 w->hw_wp_mask = mask;
9c06b0b4
TJB
11410 }
11411 else
11412 {
3a5c3e22
PA
11413 w->val = val;
11414 w->val_valid = 1;
9c06b0b4 11415 }
77b06cd7 11416
c906108c
SS
11417 if (cond_start)
11418 b->cond_string = savestring (cond_start, cond_end - cond_start);
11419 else
11420 b->cond_string = 0;
c5aa993b 11421
c906108c 11422 if (frame)
f6bc2008 11423 {
3a5c3e22
PA
11424 w->watchpoint_frame = get_frame_id (frame);
11425 w->watchpoint_thread = inferior_ptid;
f6bc2008 11426 }
c906108c 11427 else
f6bc2008 11428 {
3a5c3e22
PA
11429 w->watchpoint_frame = null_frame_id;
11430 w->watchpoint_thread = null_ptid;
f6bc2008 11431 }
c906108c 11432
d983da9c 11433 if (scope_breakpoint != NULL)
c906108c 11434 {
d983da9c
DJ
11435 /* The scope breakpoint is related to the watchpoint. We will
11436 need to act on them together. */
11437 b->related_breakpoint = scope_breakpoint;
11438 scope_breakpoint->related_breakpoint = b;
c906108c 11439 }
d983da9c 11440
06a64a0b
TT
11441 if (!just_location)
11442 value_free_to_mark (mark);
2d134ed3 11443
a9634178
TJB
11444 TRY_CATCH (e, RETURN_MASK_ALL)
11445 {
11446 /* Finally update the new watchpoint. This creates the locations
11447 that should be inserted. */
3a5c3e22 11448 update_watchpoint (w, 1);
a9634178
TJB
11449 }
11450 if (e.reason < 0)
11451 {
11452 delete_breakpoint (b);
11453 throw_exception (e);
11454 }
11455
3ea46bff 11456 install_breakpoint (internal, b, 1);
bbc13ae3 11457 do_cleanups (back_to);
c906108c
SS
11458}
11459
e09342b5 11460/* Return count of debug registers needed to watch the given expression.
e09342b5 11461 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11462
c906108c 11463static int
a9634178 11464can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11465{
11466 int found_memory_cnt = 0;
2e70b7b9 11467 struct value *head = v;
c906108c
SS
11468
11469 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11470 if (!can_use_hw_watchpoints)
c906108c 11471 return 0;
c5aa993b 11472
5c44784c
JM
11473 /* Make sure that the value of the expression depends only upon
11474 memory contents, and values computed from them within GDB. If we
11475 find any register references or function calls, we can't use a
11476 hardware watchpoint.
11477
11478 The idea here is that evaluating an expression generates a series
11479 of values, one holding the value of every subexpression. (The
11480 expression a*b+c has five subexpressions: a, b, a*b, c, and
11481 a*b+c.) GDB's values hold almost enough information to establish
11482 the criteria given above --- they identify memory lvalues,
11483 register lvalues, computed values, etcetera. So we can evaluate
11484 the expression, and then scan the chain of values that leaves
11485 behind to decide whether we can detect any possible change to the
11486 expression's final value using only hardware watchpoints.
11487
11488 However, I don't think that the values returned by inferior
11489 function calls are special in any way. So this function may not
11490 notice that an expression involving an inferior function call
11491 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11492 for (; v; v = value_next (v))
c906108c 11493 {
5c44784c 11494 if (VALUE_LVAL (v) == lval_memory)
c906108c 11495 {
8464be76
DJ
11496 if (v != head && value_lazy (v))
11497 /* A lazy memory lvalue in the chain is one that GDB never
11498 needed to fetch; we either just used its address (e.g.,
11499 `a' in `a.b') or we never needed it at all (e.g., `a'
11500 in `a,b'). This doesn't apply to HEAD; if that is
11501 lazy then it was not readable, but watch it anyway. */
5c44784c 11502 ;
53a5351d 11503 else
5c44784c
JM
11504 {
11505 /* Ahh, memory we actually used! Check if we can cover
11506 it with hardware watchpoints. */
df407dfe 11507 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11508
11509 /* We only watch structs and arrays if user asked for it
11510 explicitly, never if they just happen to appear in a
11511 middle of some value chain. */
11512 if (v == head
11513 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11514 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11515 {
42ae5230 11516 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11517 int len;
11518 int num_regs;
11519
a9634178 11520 len = (target_exact_watchpoints
e09342b5
TJB
11521 && is_scalar_type_recursive (vtype))?
11522 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11523
e09342b5
TJB
11524 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11525 if (!num_regs)
2e70b7b9
MS
11526 return 0;
11527 else
e09342b5 11528 found_memory_cnt += num_regs;
2e70b7b9 11529 }
5c44784c 11530 }
c5aa993b 11531 }
5086187c
AC
11532 else if (VALUE_LVAL (v) != not_lval
11533 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11534 return 0; /* These are values from the history (e.g., $1). */
5086187c 11535 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11536 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11537 }
11538
11539 /* The expression itself looks suitable for using a hardware
11540 watchpoint, but give the target machine a chance to reject it. */
11541 return found_memory_cnt;
11542}
11543
8b93c638 11544void
84f4c1fe 11545watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11546{
84f4c1fe 11547 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11548}
11549
06a64a0b
TT
11550/* A helper function that looks for the "-location" argument and then
11551 calls watch_command_1. */
11552
11553static void
11554watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11555{
11556 int just_location = 0;
11557
11558 if (arg
11559 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11560 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11561 {
e9cafbcc 11562 arg = skip_spaces (arg);
06a64a0b
TT
11563 just_location = 1;
11564 }
11565
84f4c1fe 11566 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11567}
8926118c 11568
c5aa993b 11569static void
fba45db2 11570watch_command (char *arg, int from_tty)
c906108c 11571{
06a64a0b 11572 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11573}
11574
8b93c638 11575void
84f4c1fe 11576rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11577{
84f4c1fe 11578 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11579}
8926118c 11580
c5aa993b 11581static void
fba45db2 11582rwatch_command (char *arg, int from_tty)
c906108c 11583{
06a64a0b 11584 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11585}
11586
8b93c638 11587void
84f4c1fe 11588awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11589{
84f4c1fe 11590 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11591}
8926118c 11592
c5aa993b 11593static void
fba45db2 11594awatch_command (char *arg, int from_tty)
c906108c 11595{
06a64a0b 11596 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11597}
c906108c 11598\f
c5aa993b 11599
43ff13b4 11600/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11601 because it uses the mechanisms of breakpoints. */
11602
bfec99b2
PA
11603struct until_break_command_continuation_args
11604{
11605 struct breakpoint *breakpoint;
11606 struct breakpoint *breakpoint2;
186c406b 11607 int thread_num;
bfec99b2
PA
11608};
11609
43ff13b4 11610/* This function is called by fetch_inferior_event via the
4a64f543 11611 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11612 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11613 command. */
c2c6d25f 11614static void
fa4cd53f 11615until_break_command_continuation (void *arg, int err)
43ff13b4 11616{
bfec99b2
PA
11617 struct until_break_command_continuation_args *a = arg;
11618
11619 delete_breakpoint (a->breakpoint);
11620 if (a->breakpoint2)
11621 delete_breakpoint (a->breakpoint2);
186c406b 11622 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11623}
11624
c906108c 11625void
ae66c1fc 11626until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11627{
11628 struct symtabs_and_lines sals;
11629 struct symtab_and_line sal;
8556afb4
PA
11630 struct frame_info *frame;
11631 struct gdbarch *frame_gdbarch;
11632 struct frame_id stack_frame_id;
11633 struct frame_id caller_frame_id;
c906108c 11634 struct breakpoint *breakpoint;
f107f563 11635 struct breakpoint *breakpoint2 = NULL;
c906108c 11636 struct cleanup *old_chain;
186c406b
TT
11637 int thread;
11638 struct thread_info *tp;
c906108c
SS
11639
11640 clear_proceed_status ();
11641
11642 /* Set a breakpoint where the user wants it and at return from
4a64f543 11643 this function. */
c5aa993b 11644
1bfeeb0f 11645 if (last_displayed_sal_is_valid ())
f8eba3c6 11646 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11647 get_last_displayed_symtab (),
f8eba3c6 11648 get_last_displayed_line ());
c906108c 11649 else
f8eba3c6
TT
11650 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11651 (struct symtab *) NULL, 0);
c5aa993b 11652
c906108c 11653 if (sals.nelts != 1)
8a3fe4f8 11654 error (_("Couldn't get information on specified line."));
c5aa993b 11655
c906108c 11656 sal = sals.sals[0];
4a64f543 11657 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11658
c906108c 11659 if (*arg)
8a3fe4f8 11660 error (_("Junk at end of arguments."));
c5aa993b 11661
c906108c 11662 resolve_sal_pc (&sal);
c5aa993b 11663
186c406b
TT
11664 tp = inferior_thread ();
11665 thread = tp->num;
11666
883bc8d1
PA
11667 old_chain = make_cleanup (null_cleanup, NULL);
11668
8556afb4
PA
11669 /* Note linespec handling above invalidates the frame chain.
11670 Installing a breakpoint also invalidates the frame chain (as it
11671 may need to switch threads), so do any frame handling before
11672 that. */
11673
11674 frame = get_selected_frame (NULL);
11675 frame_gdbarch = get_frame_arch (frame);
11676 stack_frame_id = get_stack_frame_id (frame);
11677 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11678
ae66c1fc
EZ
11679 /* Keep within the current frame, or in frames called by the current
11680 one. */
edb3359d 11681
883bc8d1 11682 if (frame_id_p (caller_frame_id))
c906108c 11683 {
883bc8d1
PA
11684 struct symtab_and_line sal2;
11685
11686 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11687 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11688 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11689 sal2,
11690 caller_frame_id,
f107f563
VP
11691 bp_until);
11692 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11693
883bc8d1 11694 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11695 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11696 }
c5aa993b 11697
c70a6932
JK
11698 /* set_momentary_breakpoint could invalidate FRAME. */
11699 frame = NULL;
11700
883bc8d1
PA
11701 if (anywhere)
11702 /* If the user told us to continue until a specified location,
11703 we don't specify a frame at which we need to stop. */
11704 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11705 null_frame_id, bp_until);
11706 else
11707 /* Otherwise, specify the selected frame, because we want to stop
11708 only at the very same frame. */
11709 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11710 stack_frame_id, bp_until);
11711 make_cleanup_delete_breakpoint (breakpoint);
11712
a493e3e2 11713 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11714
4a64f543
MS
11715 /* If we are running asynchronously, and proceed call above has
11716 actually managed to start the target, arrange for breakpoints to
11717 be deleted when the target stops. Otherwise, we're already
11718 stopped and delete breakpoints via cleanup chain. */
f107f563 11719
8ea051c5 11720 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11721 {
bfec99b2
PA
11722 struct until_break_command_continuation_args *args;
11723 args = xmalloc (sizeof (*args));
f107f563 11724
bfec99b2
PA
11725 args->breakpoint = breakpoint;
11726 args->breakpoint2 = breakpoint2;
186c406b 11727 args->thread_num = thread;
f107f563
VP
11728
11729 discard_cleanups (old_chain);
95e54da7
PA
11730 add_continuation (inferior_thread (),
11731 until_break_command_continuation, args,
604ead4a 11732 xfree);
f107f563
VP
11733 }
11734 else
c5aa993b 11735 do_cleanups (old_chain);
c906108c 11736}
ae66c1fc 11737
c906108c
SS
11738/* This function attempts to parse an optional "if <cond>" clause
11739 from the arg string. If one is not found, it returns NULL.
c5aa993b 11740
c906108c
SS
11741 Else, it returns a pointer to the condition string. (It does not
11742 attempt to evaluate the string against a particular block.) And,
11743 it updates arg to point to the first character following the parsed
4a64f543 11744 if clause in the arg string. */
53a5351d 11745
916703c0 11746char *
fba45db2 11747ep_parse_optional_if_clause (char **arg)
c906108c 11748{
c5aa993b
JM
11749 char *cond_string;
11750
11751 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11752 return NULL;
c5aa993b 11753
4a64f543 11754 /* Skip the "if" keyword. */
c906108c 11755 (*arg) += 2;
c5aa993b 11756
c906108c 11757 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11758 condition string. */
e9cafbcc 11759 *arg = skip_spaces (*arg);
c906108c 11760 cond_string = *arg;
c5aa993b 11761
4a64f543
MS
11762 /* Assume that the condition occupies the remainder of the arg
11763 string. */
c906108c 11764 (*arg) += strlen (cond_string);
c5aa993b 11765
c906108c
SS
11766 return cond_string;
11767}
c5aa993b 11768
c906108c
SS
11769/* Commands to deal with catching events, such as signals, exceptions,
11770 process start/exit, etc. */
c5aa993b
JM
11771
11772typedef enum
11773{
44feb3ce
TT
11774 catch_fork_temporary, catch_vfork_temporary,
11775 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11776}
11777catch_fork_kind;
11778
c906108c 11779static void
cc59ec59
MS
11780catch_fork_command_1 (char *arg, int from_tty,
11781 struct cmd_list_element *command)
c906108c 11782{
a6d9a66e 11783 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11784 char *cond_string = NULL;
44feb3ce
TT
11785 catch_fork_kind fork_kind;
11786 int tempflag;
11787
11788 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11789 tempflag = (fork_kind == catch_fork_temporary
11790 || fork_kind == catch_vfork_temporary);
c5aa993b 11791
44feb3ce
TT
11792 if (!arg)
11793 arg = "";
e9cafbcc 11794 arg = skip_spaces (arg);
c5aa993b 11795
c906108c 11796 /* The allowed syntax is:
c5aa993b
JM
11797 catch [v]fork
11798 catch [v]fork if <cond>
11799
4a64f543 11800 First, check if there's an if clause. */
c906108c 11801 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11802
c906108c 11803 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11804 error (_("Junk at end of arguments."));
c5aa993b 11805
c906108c 11806 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11807 and enable reporting of such events. */
c5aa993b
JM
11808 switch (fork_kind)
11809 {
44feb3ce
TT
11810 case catch_fork_temporary:
11811 case catch_fork_permanent:
a6d9a66e 11812 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11813 &catch_fork_breakpoint_ops);
c906108c 11814 break;
44feb3ce
TT
11815 case catch_vfork_temporary:
11816 case catch_vfork_permanent:
a6d9a66e 11817 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11818 &catch_vfork_breakpoint_ops);
c906108c 11819 break;
c5aa993b 11820 default:
8a3fe4f8 11821 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11822 break;
c5aa993b 11823 }
c906108c
SS
11824}
11825
11826static void
cc59ec59
MS
11827catch_exec_command_1 (char *arg, int from_tty,
11828 struct cmd_list_element *command)
c906108c 11829{
b4d90040 11830 struct exec_catchpoint *c;
a6d9a66e 11831 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11832 int tempflag;
c5aa993b 11833 char *cond_string = NULL;
c906108c 11834
44feb3ce
TT
11835 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11836
11837 if (!arg)
11838 arg = "";
e9cafbcc 11839 arg = skip_spaces (arg);
c906108c
SS
11840
11841 /* The allowed syntax is:
c5aa993b
JM
11842 catch exec
11843 catch exec if <cond>
c906108c 11844
4a64f543 11845 First, check if there's an if clause. */
c906108c
SS
11846 cond_string = ep_parse_optional_if_clause (&arg);
11847
11848 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11849 error (_("Junk at end of arguments."));
c906108c 11850
b4d90040
PA
11851 c = XNEW (struct exec_catchpoint);
11852 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11853 &catch_exec_breakpoint_ops);
11854 c->exec_pathname = NULL;
11855
3ea46bff 11856 install_breakpoint (0, &c->base, 1);
c906108c 11857}
c5aa993b 11858
9ac4176b 11859void
28010a5d
PA
11860init_ada_exception_breakpoint (struct breakpoint *b,
11861 struct gdbarch *gdbarch,
11862 struct symtab_and_line sal,
11863 char *addr_string,
c0a91b2b 11864 const struct breakpoint_ops *ops,
28010a5d 11865 int tempflag,
349774ef 11866 int enabled,
28010a5d 11867 int from_tty)
f7f9143b 11868{
f7f9143b
JB
11869 if (from_tty)
11870 {
5af949e3
UW
11871 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11872 if (!loc_gdbarch)
11873 loc_gdbarch = gdbarch;
11874
6c95b8df
PA
11875 describe_other_breakpoints (loc_gdbarch,
11876 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11877 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11878 version for exception catchpoints, because two catchpoints
11879 used for different exception names will use the same address.
11880 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11881 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11882 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11883 the user what type of catchpoint it is. The above is good
11884 enough for now, though. */
11885 }
11886
28010a5d 11887 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11888
349774ef 11889 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11890 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11891 b->addr_string = addr_string;
11892 b->language = language_ada;
f7f9143b
JB
11893}
11894
a96d9b2e
SDJ
11895/* Splits the argument using space as delimiter. Returns an xmalloc'd
11896 filter list, or NULL if no filtering is required. */
11897static VEC(int) *
11898catch_syscall_split_args (char *arg)
11899{
11900 VEC(int) *result = NULL;
29d0bb3d 11901 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11902
11903 while (*arg != '\0')
11904 {
11905 int i, syscall_number;
11906 char *endptr;
11907 char cur_name[128];
11908 struct syscall s;
11909
11910 /* Skip whitespace. */
529480d0 11911 arg = skip_spaces (arg);
a96d9b2e
SDJ
11912
11913 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11914 cur_name[i] = arg[i];
11915 cur_name[i] = '\0';
11916 arg += i;
11917
11918 /* Check if the user provided a syscall name or a number. */
11919 syscall_number = (int) strtol (cur_name, &endptr, 0);
11920 if (*endptr == '\0')
bccd0dd2 11921 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11922 else
11923 {
11924 /* We have a name. Let's check if it's valid and convert it
11925 to a number. */
11926 get_syscall_by_name (cur_name, &s);
11927
11928 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11929 /* Here we have to issue an error instead of a warning,
11930 because GDB cannot do anything useful if there's no
11931 syscall number to be caught. */
a96d9b2e
SDJ
11932 error (_("Unknown syscall name '%s'."), cur_name);
11933 }
11934
11935 /* Ok, it's valid. */
11936 VEC_safe_push (int, result, s.number);
11937 }
11938
11939 discard_cleanups (cleanup);
11940 return result;
11941}
11942
11943/* Implement the "catch syscall" command. */
11944
11945static void
cc59ec59
MS
11946catch_syscall_command_1 (char *arg, int from_tty,
11947 struct cmd_list_element *command)
a96d9b2e
SDJ
11948{
11949 int tempflag;
11950 VEC(int) *filter;
11951 struct syscall s;
11952 struct gdbarch *gdbarch = get_current_arch ();
11953
11954 /* Checking if the feature if supported. */
11955 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11956 error (_("The feature 'catch syscall' is not supported on \
ea666128 11957this architecture yet."));
a96d9b2e
SDJ
11958
11959 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11960
e9cafbcc 11961 arg = skip_spaces (arg);
a96d9b2e
SDJ
11962
11963 /* We need to do this first "dummy" translation in order
11964 to get the syscall XML file loaded or, most important,
11965 to display a warning to the user if there's no XML file
11966 for his/her architecture. */
11967 get_syscall_by_number (0, &s);
11968
11969 /* The allowed syntax is:
11970 catch syscall
11971 catch syscall <name | number> [<name | number> ... <name | number>]
11972
11973 Let's check if there's a syscall name. */
11974
11975 if (arg != NULL)
11976 filter = catch_syscall_split_args (arg);
11977 else
11978 filter = NULL;
11979
11980 create_syscall_event_catchpoint (tempflag, filter,
11981 &catch_syscall_breakpoint_ops);
11982}
11983
c906108c 11984static void
fba45db2 11985catch_command (char *arg, int from_tty)
c906108c 11986{
44feb3ce 11987 error (_("Catch requires an event name."));
c906108c
SS
11988}
11989\f
11990
11991static void
fba45db2 11992tcatch_command (char *arg, int from_tty)
c906108c 11993{
44feb3ce 11994 error (_("Catch requires an event name."));
c906108c
SS
11995}
11996
8a2c437b
TT
11997/* A qsort comparison function that sorts breakpoints in order. */
11998
11999static int
12000compare_breakpoints (const void *a, const void *b)
12001{
12002 const breakpoint_p *ba = a;
12003 uintptr_t ua = (uintptr_t) *ba;
12004 const breakpoint_p *bb = b;
12005 uintptr_t ub = (uintptr_t) *bb;
12006
12007 if ((*ba)->number < (*bb)->number)
12008 return -1;
12009 else if ((*ba)->number > (*bb)->number)
12010 return 1;
12011
12012 /* Now sort by address, in case we see, e..g, two breakpoints with
12013 the number 0. */
12014 if (ua < ub)
12015 return -1;
94b0e70d 12016 return ua > ub ? 1 : 0;
8a2c437b
TT
12017}
12018
80f8a6eb 12019/* Delete breakpoints by address or line. */
c906108c
SS
12020
12021static void
fba45db2 12022clear_command (char *arg, int from_tty)
c906108c 12023{
8a2c437b 12024 struct breakpoint *b, *prev;
d6e956e5
VP
12025 VEC(breakpoint_p) *found = 0;
12026 int ix;
c906108c
SS
12027 int default_match;
12028 struct symtabs_and_lines sals;
12029 struct symtab_and_line sal;
c906108c 12030 int i;
8a2c437b 12031 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12032
12033 if (arg)
12034 {
39cf75f7
DE
12035 sals = decode_line_with_current_source (arg,
12036 (DECODE_LINE_FUNFIRSTLINE
12037 | DECODE_LINE_LIST_MODE));
cf4ded82 12038 make_cleanup (xfree, sals.sals);
c906108c
SS
12039 default_match = 0;
12040 }
12041 else
12042 {
c5aa993b 12043 sals.sals = (struct symtab_and_line *)
c906108c 12044 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12045 make_cleanup (xfree, sals.sals);
4a64f543 12046 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12047
12048 /* Set sal's line, symtab, pc, and pspace to the values
12049 corresponding to the last call to print_frame_info. If the
12050 codepoint is not valid, this will set all the fields to 0. */
12051 get_last_displayed_sal (&sal);
c906108c 12052 if (sal.symtab == 0)
8a3fe4f8 12053 error (_("No source file specified."));
c906108c
SS
12054
12055 sals.sals[0] = sal;
12056 sals.nelts = 1;
12057
12058 default_match = 1;
12059 }
12060
4a64f543
MS
12061 /* We don't call resolve_sal_pc here. That's not as bad as it
12062 seems, because all existing breakpoints typically have both
12063 file/line and pc set. So, if clear is given file/line, we can
12064 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12065
12066 We only support clearing given the address explicitly
12067 present in breakpoint table. Say, we've set breakpoint
4a64f543 12068 at file:line. There were several PC values for that file:line,
ed0616c6 12069 due to optimization, all in one block.
4a64f543
MS
12070
12071 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12072 PC corresponding to the same file:line, the breakpoint won't
12073 be cleared. We probably can still clear the breakpoint, but
12074 since the other PC value is never presented to user, user
12075 can only find it by guessing, and it does not seem important
12076 to support that. */
12077
4a64f543
MS
12078 /* For each line spec given, delete bps which correspond to it. Do
12079 it in two passes, solely to preserve the current behavior that
12080 from_tty is forced true if we delete more than one
12081 breakpoint. */
c906108c 12082
80f8a6eb 12083 found = NULL;
8a2c437b 12084 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12085 for (i = 0; i < sals.nelts; i++)
12086 {
05cba821
JK
12087 const char *sal_fullname;
12088
c906108c 12089 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12090 If line given (pc == 0), clear all bpts on specified line.
12091 If defaulting, clear all bpts on default line
c906108c 12092 or at default pc.
c5aa993b
JM
12093
12094 defaulting sal.pc != 0 tests to do
12095
12096 0 1 pc
12097 1 1 pc _and_ line
12098 0 0 line
12099 1 0 <can't happen> */
c906108c
SS
12100
12101 sal = sals.sals[i];
05cba821
JK
12102 sal_fullname = (sal.symtab == NULL
12103 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12104
4a64f543 12105 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12106 ALL_BREAKPOINTS (b)
c5aa993b 12107 {
0d381245 12108 int match = 0;
4a64f543 12109 /* Are we going to delete b? */
cc60f2e3 12110 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12111 {
12112 struct bp_location *loc = b->loc;
12113 for (; loc; loc = loc->next)
12114 {
f8eba3c6
TT
12115 /* If the user specified file:line, don't allow a PC
12116 match. This matches historical gdb behavior. */
12117 int pc_match = (!sal.explicit_line
12118 && sal.pc
12119 && (loc->pspace == sal.pspace)
12120 && (loc->address == sal.pc)
12121 && (!section_is_overlay (loc->section)
12122 || loc->section == sal.section));
4aac40c8
TT
12123 int line_match = 0;
12124
12125 if ((default_match || sal.explicit_line)
2f202fde 12126 && loc->symtab != NULL
05cba821 12127 && sal_fullname != NULL
4aac40c8 12128 && sal.pspace == loc->pspace
05cba821
JK
12129 && loc->line_number == sal.line
12130 && filename_cmp (symtab_to_fullname (loc->symtab),
12131 sal_fullname) == 0)
12132 line_match = 1;
4aac40c8 12133
0d381245
VP
12134 if (pc_match || line_match)
12135 {
12136 match = 1;
12137 break;
12138 }
12139 }
12140 }
12141
12142 if (match)
d6e956e5 12143 VEC_safe_push(breakpoint_p, found, b);
c906108c 12144 }
80f8a6eb 12145 }
8a2c437b 12146
80f8a6eb 12147 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12148 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12149 {
12150 if (arg)
8a3fe4f8 12151 error (_("No breakpoint at %s."), arg);
80f8a6eb 12152 else
8a3fe4f8 12153 error (_("No breakpoint at this line."));
80f8a6eb 12154 }
c906108c 12155
8a2c437b
TT
12156 /* Remove duplicates from the vec. */
12157 qsort (VEC_address (breakpoint_p, found),
12158 VEC_length (breakpoint_p, found),
12159 sizeof (breakpoint_p),
12160 compare_breakpoints);
12161 prev = VEC_index (breakpoint_p, found, 0);
12162 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12163 {
12164 if (b == prev)
12165 {
12166 VEC_ordered_remove (breakpoint_p, found, ix);
12167 --ix;
12168 }
12169 }
12170
d6e956e5 12171 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12172 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12173 if (from_tty)
a3f17187 12174 {
d6e956e5 12175 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12176 printf_unfiltered (_("Deleted breakpoint "));
12177 else
12178 printf_unfiltered (_("Deleted breakpoints "));
12179 }
d6e956e5
VP
12180
12181 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12182 {
c5aa993b 12183 if (from_tty)
d6e956e5
VP
12184 printf_unfiltered ("%d ", b->number);
12185 delete_breakpoint (b);
c906108c 12186 }
80f8a6eb
MS
12187 if (from_tty)
12188 putchar_unfiltered ('\n');
8a2c437b
TT
12189
12190 do_cleanups (cleanups);
c906108c
SS
12191}
12192\f
12193/* Delete breakpoint in BS if they are `delete' breakpoints and
12194 all breakpoints that are marked for deletion, whether hit or not.
12195 This is called after any breakpoint is hit, or after errors. */
12196
12197void
fba45db2 12198breakpoint_auto_delete (bpstat bs)
c906108c 12199{
35df4500 12200 struct breakpoint *b, *b_tmp;
c906108c
SS
12201
12202 for (; bs; bs = bs->next)
f431efe5
PA
12203 if (bs->breakpoint_at
12204 && bs->breakpoint_at->disposition == disp_del
c906108c 12205 && bs->stop)
f431efe5 12206 delete_breakpoint (bs->breakpoint_at);
c906108c 12207
35df4500 12208 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12209 {
b5de0fa7 12210 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12211 delete_breakpoint (b);
12212 }
c906108c
SS
12213}
12214
4a64f543
MS
12215/* A comparison function for bp_location AP and BP being interfaced to
12216 qsort. Sort elements primarily by their ADDRESS (no matter what
12217 does breakpoint_address_is_meaningful say for its OWNER),
12218 secondarily by ordering first bp_permanent OWNERed elements and
12219 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12220 qsort being an unstable algorithm. */
876fa593
JK
12221
12222static int
494cfb0f 12223bp_location_compare (const void *ap, const void *bp)
876fa593 12224{
494cfb0f
JK
12225 struct bp_location *a = *(void **) ap;
12226 struct bp_location *b = *(void **) bp;
2bdf28a0 12227 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12228 int a_perm = a->owner->enable_state == bp_permanent;
12229 int b_perm = b->owner->enable_state == bp_permanent;
12230
12231 if (a->address != b->address)
12232 return (a->address > b->address) - (a->address < b->address);
12233
dea2aa5f
LM
12234 /* Sort locations at the same address by their pspace number, keeping
12235 locations of the same inferior (in a multi-inferior environment)
12236 grouped. */
12237
12238 if (a->pspace->num != b->pspace->num)
12239 return ((a->pspace->num > b->pspace->num)
12240 - (a->pspace->num < b->pspace->num));
12241
876fa593
JK
12242 /* Sort permanent breakpoints first. */
12243 if (a_perm != b_perm)
12244 return (a_perm < b_perm) - (a_perm > b_perm);
12245
c56a97f9
JK
12246 /* Make the internal GDB representation stable across GDB runs
12247 where A and B memory inside GDB can differ. Breakpoint locations of
12248 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12249
12250 if (a->owner->number != b->owner->number)
c56a97f9
JK
12251 return ((a->owner->number > b->owner->number)
12252 - (a->owner->number < b->owner->number));
876fa593
JK
12253
12254 return (a > b) - (a < b);
12255}
12256
876fa593 12257/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12258 bp_location_shadow_len_after_address_max according to the current
12259 content of the bp_location array. */
f7545552
TT
12260
12261static void
876fa593 12262bp_location_target_extensions_update (void)
f7545552 12263{
876fa593
JK
12264 struct bp_location *bl, **blp_tmp;
12265
12266 bp_location_placed_address_before_address_max = 0;
12267 bp_location_shadow_len_after_address_max = 0;
12268
12269 ALL_BP_LOCATIONS (bl, blp_tmp)
12270 {
12271 CORE_ADDR start, end, addr;
12272
12273 if (!bp_location_has_shadow (bl))
12274 continue;
12275
12276 start = bl->target_info.placed_address;
12277 end = start + bl->target_info.shadow_len;
12278
12279 gdb_assert (bl->address >= start);
12280 addr = bl->address - start;
12281 if (addr > bp_location_placed_address_before_address_max)
12282 bp_location_placed_address_before_address_max = addr;
12283
12284 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12285
12286 gdb_assert (bl->address < end);
12287 addr = end - bl->address;
12288 if (addr > bp_location_shadow_len_after_address_max)
12289 bp_location_shadow_len_after_address_max = addr;
12290 }
f7545552
TT
12291}
12292
1e4d1764
YQ
12293/* Download tracepoint locations if they haven't been. */
12294
12295static void
12296download_tracepoint_locations (void)
12297{
7ed2c994 12298 struct breakpoint *b;
1e4d1764
YQ
12299 struct cleanup *old_chain;
12300
12301 if (!target_can_download_tracepoint ())
12302 return;
12303
12304 old_chain = save_current_space_and_thread ();
12305
7ed2c994 12306 ALL_TRACEPOINTS (b)
1e4d1764 12307 {
7ed2c994 12308 struct bp_location *bl;
1e4d1764 12309 struct tracepoint *t;
f2a8bc8a 12310 int bp_location_downloaded = 0;
1e4d1764 12311
7ed2c994 12312 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12313 ? !may_insert_fast_tracepoints
12314 : !may_insert_tracepoints))
12315 continue;
12316
7ed2c994
YQ
12317 for (bl = b->loc; bl; bl = bl->next)
12318 {
12319 /* In tracepoint, locations are _never_ duplicated, so
12320 should_be_inserted is equivalent to
12321 unduplicated_should_be_inserted. */
12322 if (!should_be_inserted (bl) || bl->inserted)
12323 continue;
1e4d1764 12324
7ed2c994 12325 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12326
7ed2c994 12327 target_download_tracepoint (bl);
1e4d1764 12328
7ed2c994 12329 bl->inserted = 1;
f2a8bc8a 12330 bp_location_downloaded = 1;
7ed2c994
YQ
12331 }
12332 t = (struct tracepoint *) b;
12333 t->number_on_target = b->number;
f2a8bc8a
YQ
12334 if (bp_location_downloaded)
12335 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12336 }
12337
12338 do_cleanups (old_chain);
12339}
12340
934709f0
PW
12341/* Swap the insertion/duplication state between two locations. */
12342
12343static void
12344swap_insertion (struct bp_location *left, struct bp_location *right)
12345{
12346 const int left_inserted = left->inserted;
12347 const int left_duplicate = left->duplicate;
b775012e 12348 const int left_needs_update = left->needs_update;
934709f0
PW
12349 const struct bp_target_info left_target_info = left->target_info;
12350
1e4d1764
YQ
12351 /* Locations of tracepoints can never be duplicated. */
12352 if (is_tracepoint (left->owner))
12353 gdb_assert (!left->duplicate);
12354 if (is_tracepoint (right->owner))
12355 gdb_assert (!right->duplicate);
12356
934709f0
PW
12357 left->inserted = right->inserted;
12358 left->duplicate = right->duplicate;
b775012e 12359 left->needs_update = right->needs_update;
934709f0
PW
12360 left->target_info = right->target_info;
12361 right->inserted = left_inserted;
12362 right->duplicate = left_duplicate;
b775012e 12363 right->needs_update = left_needs_update;
934709f0
PW
12364 right->target_info = left_target_info;
12365}
12366
b775012e
LM
12367/* Force the re-insertion of the locations at ADDRESS. This is called
12368 once a new/deleted/modified duplicate location is found and we are evaluating
12369 conditions on the target's side. Such conditions need to be updated on
12370 the target. */
12371
12372static void
12373force_breakpoint_reinsertion (struct bp_location *bl)
12374{
12375 struct bp_location **locp = NULL, **loc2p;
12376 struct bp_location *loc;
12377 CORE_ADDR address = 0;
12378 int pspace_num;
12379
12380 address = bl->address;
12381 pspace_num = bl->pspace->num;
12382
12383 /* This is only meaningful if the target is
12384 evaluating conditions and if the user has
12385 opted for condition evaluation on the target's
12386 side. */
12387 if (gdb_evaluates_breakpoint_condition_p ()
12388 || !target_supports_evaluation_of_breakpoint_conditions ())
12389 return;
12390
12391 /* Flag all breakpoint locations with this address and
12392 the same program space as the location
12393 as "its condition has changed". We need to
12394 update the conditions on the target's side. */
12395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12396 {
12397 loc = *loc2p;
12398
12399 if (!is_breakpoint (loc->owner)
12400 || pspace_num != loc->pspace->num)
12401 continue;
12402
12403 /* Flag the location appropriately. We use a different state to
12404 let everyone know that we already updated the set of locations
12405 with addr bl->address and program space bl->pspace. This is so
12406 we don't have to keep calling these functions just to mark locations
12407 that have already been marked. */
12408 loc->condition_changed = condition_updated;
12409
12410 /* Free the agent expression bytecode as well. We will compute
12411 it later on. */
12412 if (loc->cond_bytecode)
12413 {
12414 free_agent_expr (loc->cond_bytecode);
12415 loc->cond_bytecode = NULL;
12416 }
12417 }
12418}
12419
4cd9bd08 12420/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12421 into the inferior, only remove already-inserted locations that no
12422 longer should be inserted. Functions that delete a breakpoint or
12423 breakpoints should pass false, so that deleting a breakpoint
12424 doesn't have the side effect of inserting the locations of other
12425 breakpoints that are marked not-inserted, but should_be_inserted
12426 returns true on them.
12427
12428 This behaviour is useful is situations close to tear-down -- e.g.,
12429 after an exec, while the target still has execution, but breakpoint
12430 shadows of the previous executable image should *NOT* be restored
12431 to the new image; or before detaching, where the target still has
12432 execution and wants to delete breakpoints from GDB's lists, and all
12433 breakpoints had already been removed from the inferior. */
12434
0d381245 12435static void
b60e7edf 12436update_global_location_list (int should_insert)
0d381245 12437{
74960c60 12438 struct breakpoint *b;
876fa593 12439 struct bp_location **locp, *loc;
f7545552 12440 struct cleanup *cleanups;
b775012e
LM
12441 /* Last breakpoint location address that was marked for update. */
12442 CORE_ADDR last_addr = 0;
12443 /* Last breakpoint location program space that was marked for update. */
12444 int last_pspace_num = -1;
f7545552 12445
2d134ed3
PA
12446 /* Used in the duplicates detection below. When iterating over all
12447 bp_locations, points to the first bp_location of a given address.
12448 Breakpoints and watchpoints of different types are never
12449 duplicates of each other. Keep one pointer for each type of
12450 breakpoint/watchpoint, so we only need to loop over all locations
12451 once. */
12452 struct bp_location *bp_loc_first; /* breakpoint */
12453 struct bp_location *wp_loc_first; /* hardware watchpoint */
12454 struct bp_location *awp_loc_first; /* access watchpoint */
12455 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12456
4a64f543
MS
12457 /* Saved former bp_location array which we compare against the newly
12458 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12459 struct bp_location **old_location, **old_locp;
12460 unsigned old_location_count;
12461
12462 old_location = bp_location;
12463 old_location_count = bp_location_count;
12464 bp_location = NULL;
12465 bp_location_count = 0;
12466 cleanups = make_cleanup (xfree, old_location);
0d381245 12467
74960c60 12468 ALL_BREAKPOINTS (b)
876fa593
JK
12469 for (loc = b->loc; loc; loc = loc->next)
12470 bp_location_count++;
12471
12472 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12473 locp = bp_location;
12474 ALL_BREAKPOINTS (b)
12475 for (loc = b->loc; loc; loc = loc->next)
12476 *locp++ = loc;
12477 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12478 bp_location_compare);
876fa593
JK
12479
12480 bp_location_target_extensions_update ();
74960c60 12481
4a64f543
MS
12482 /* Identify bp_location instances that are no longer present in the
12483 new list, and therefore should be freed. Note that it's not
12484 necessary that those locations should be removed from inferior --
12485 if there's another location at the same address (previously
12486 marked as duplicate), we don't need to remove/insert the
12487 location.
876fa593 12488
4a64f543
MS
12489 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12490 and former bp_location array state respectively. */
876fa593
JK
12491
12492 locp = bp_location;
12493 for (old_locp = old_location; old_locp < old_location + old_location_count;
12494 old_locp++)
74960c60 12495 {
876fa593 12496 struct bp_location *old_loc = *old_locp;
c7d46a38 12497 struct bp_location **loc2p;
876fa593 12498
e5dd4106 12499 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12500 not, we have to free it. */
c7d46a38 12501 int found_object = 0;
20874c92
VP
12502 /* Tells if the location should remain inserted in the target. */
12503 int keep_in_target = 0;
12504 int removed = 0;
876fa593 12505
4a64f543
MS
12506 /* Skip LOCP entries which will definitely never be needed.
12507 Stop either at or being the one matching OLD_LOC. */
876fa593 12508 while (locp < bp_location + bp_location_count
c7d46a38 12509 && (*locp)->address < old_loc->address)
876fa593 12510 locp++;
c7d46a38
PA
12511
12512 for (loc2p = locp;
12513 (loc2p < bp_location + bp_location_count
12514 && (*loc2p)->address == old_loc->address);
12515 loc2p++)
12516 {
b775012e
LM
12517 /* Check if this is a new/duplicated location or a duplicated
12518 location that had its condition modified. If so, we want to send
12519 its condition to the target if evaluation of conditions is taking
12520 place there. */
12521 if ((*loc2p)->condition_changed == condition_modified
12522 && (last_addr != old_loc->address
12523 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12524 {
b775012e
LM
12525 force_breakpoint_reinsertion (*loc2p);
12526 last_pspace_num = old_loc->pspace->num;
c7d46a38 12527 }
b775012e
LM
12528
12529 if (*loc2p == old_loc)
12530 found_object = 1;
c7d46a38 12531 }
74960c60 12532
b775012e
LM
12533 /* We have already handled this address, update it so that we don't
12534 have to go through updates again. */
12535 last_addr = old_loc->address;
12536
12537 /* Target-side condition evaluation: Handle deleted locations. */
12538 if (!found_object)
12539 force_breakpoint_reinsertion (old_loc);
12540
4a64f543
MS
12541 /* If this location is no longer present, and inserted, look if
12542 there's maybe a new location at the same address. If so,
12543 mark that one inserted, and don't remove this one. This is
12544 needed so that we don't have a time window where a breakpoint
12545 at certain location is not inserted. */
74960c60 12546
876fa593 12547 if (old_loc->inserted)
0d381245 12548 {
4a64f543
MS
12549 /* If the location is inserted now, we might have to remove
12550 it. */
74960c60 12551
876fa593 12552 if (found_object && should_be_inserted (old_loc))
74960c60 12553 {
4a64f543
MS
12554 /* The location is still present in the location list,
12555 and still should be inserted. Don't do anything. */
20874c92 12556 keep_in_target = 1;
74960c60
VP
12557 }
12558 else
12559 {
b775012e
LM
12560 /* This location still exists, but it won't be kept in the
12561 target since it may have been disabled. We proceed to
12562 remove its target-side condition. */
12563
4a64f543
MS
12564 /* The location is either no longer present, or got
12565 disabled. See if there's another location at the
12566 same address, in which case we don't need to remove
12567 this one from the target. */
876fa593 12568
2bdf28a0 12569 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12570 if (breakpoint_address_is_meaningful (old_loc->owner))
12571 {
876fa593 12572 for (loc2p = locp;
c7d46a38
PA
12573 (loc2p < bp_location + bp_location_count
12574 && (*loc2p)->address == old_loc->address);
876fa593
JK
12575 loc2p++)
12576 {
12577 struct bp_location *loc2 = *loc2p;
12578
2d134ed3 12579 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12580 {
85d721b8
PA
12581 /* Read watchpoint locations are switched to
12582 access watchpoints, if the former are not
12583 supported, but the latter are. */
12584 if (is_hardware_watchpoint (old_loc->owner))
12585 {
12586 gdb_assert (is_hardware_watchpoint (loc2->owner));
12587 loc2->watchpoint_type = old_loc->watchpoint_type;
12588 }
12589
934709f0
PW
12590 /* loc2 is a duplicated location. We need to check
12591 if it should be inserted in case it will be
12592 unduplicated. */
12593 if (loc2 != old_loc
12594 && unduplicated_should_be_inserted (loc2))
c7d46a38 12595 {
934709f0 12596 swap_insertion (old_loc, loc2);
c7d46a38
PA
12597 keep_in_target = 1;
12598 break;
12599 }
876fa593
JK
12600 }
12601 }
12602 }
74960c60
VP
12603 }
12604
20874c92
VP
12605 if (!keep_in_target)
12606 {
876fa593 12607 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12608 {
4a64f543
MS
12609 /* This is just about all we can do. We could keep
12610 this location on the global list, and try to
12611 remove it next time, but there's no particular
12612 reason why we will succeed next time.
20874c92 12613
4a64f543
MS
12614 Note that at this point, old_loc->owner is still
12615 valid, as delete_breakpoint frees the breakpoint
12616 only after calling us. */
3e43a32a
MS
12617 printf_filtered (_("warning: Error removing "
12618 "breakpoint %d\n"),
876fa593 12619 old_loc->owner->number);
20874c92
VP
12620 }
12621 removed = 1;
12622 }
0d381245 12623 }
74960c60
VP
12624
12625 if (!found_object)
1c5cfe86 12626 {
db82e815
PA
12627 if (removed && non_stop
12628 && breakpoint_address_is_meaningful (old_loc->owner)
12629 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12630 {
db82e815
PA
12631 /* This location was removed from the target. In
12632 non-stop mode, a race condition is possible where
12633 we've removed a breakpoint, but stop events for that
12634 breakpoint are already queued and will arrive later.
12635 We apply an heuristic to be able to distinguish such
12636 SIGTRAPs from other random SIGTRAPs: we keep this
12637 breakpoint location for a bit, and will retire it
12638 after we see some number of events. The theory here
12639 is that reporting of events should, "on the average",
12640 be fair, so after a while we'll see events from all
12641 threads that have anything of interest, and no longer
12642 need to keep this breakpoint location around. We
12643 don't hold locations forever so to reduce chances of
12644 mistaking a non-breakpoint SIGTRAP for a breakpoint
12645 SIGTRAP.
12646
12647 The heuristic failing can be disastrous on
12648 decr_pc_after_break targets.
12649
12650 On decr_pc_after_break targets, like e.g., x86-linux,
12651 if we fail to recognize a late breakpoint SIGTRAP,
12652 because events_till_retirement has reached 0 too
12653 soon, we'll fail to do the PC adjustment, and report
12654 a random SIGTRAP to the user. When the user resumes
12655 the inferior, it will most likely immediately crash
2dec564e 12656 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12657 corrupted, because of being resumed e.g., in the
12658 middle of a multi-byte instruction, or skipped a
12659 one-byte instruction. This was actually seen happen
12660 on native x86-linux, and should be less rare on
12661 targets that do not support new thread events, like
12662 remote, due to the heuristic depending on
12663 thread_count.
12664
12665 Mistaking a random SIGTRAP for a breakpoint trap
12666 causes similar symptoms (PC adjustment applied when
12667 it shouldn't), but then again, playing with SIGTRAPs
12668 behind the debugger's back is asking for trouble.
12669
12670 Since hardware watchpoint traps are always
12671 distinguishable from other traps, so we don't need to
12672 apply keep hardware watchpoint moribund locations
12673 around. We simply always ignore hardware watchpoint
12674 traps we can no longer explain. */
12675
876fa593
JK
12676 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12677 old_loc->owner = NULL;
20874c92 12678
876fa593 12679 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12680 }
12681 else
f431efe5
PA
12682 {
12683 old_loc->owner = NULL;
12684 decref_bp_location (&old_loc);
12685 }
20874c92 12686 }
74960c60 12687 }
1c5cfe86 12688
348d480f
PA
12689 /* Rescan breakpoints at the same address and section, marking the
12690 first one as "first" and any others as "duplicates". This is so
12691 that the bpt instruction is only inserted once. If we have a
12692 permanent breakpoint at the same place as BPT, make that one the
12693 official one, and the rest as duplicates. Permanent breakpoints
12694 are sorted first for the same address.
12695
12696 Do the same for hardware watchpoints, but also considering the
12697 watchpoint's type (regular/access/read) and length. */
12698
12699 bp_loc_first = NULL;
12700 wp_loc_first = NULL;
12701 awp_loc_first = NULL;
12702 rwp_loc_first = NULL;
12703 ALL_BP_LOCATIONS (loc, locp)
12704 {
12705 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12706 non-NULL. */
348d480f 12707 struct bp_location **loc_first_p;
d3fbdd86 12708 b = loc->owner;
348d480f 12709
6f380991 12710 if (!unduplicated_should_be_inserted (loc)
348d480f 12711 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12712 /* Don't detect duplicate for tracepoint locations because they are
12713 never duplicated. See the comments in field `duplicate' of
12714 `struct bp_location'. */
348d480f 12715 || is_tracepoint (b))
b775012e
LM
12716 {
12717 /* Clear the condition modification flag. */
12718 loc->condition_changed = condition_unchanged;
12719 continue;
12720 }
348d480f
PA
12721
12722 /* Permanent breakpoint should always be inserted. */
12723 if (b->enable_state == bp_permanent && ! loc->inserted)
12724 internal_error (__FILE__, __LINE__,
12725 _("allegedly permanent breakpoint is not "
12726 "actually inserted"));
12727
12728 if (b->type == bp_hardware_watchpoint)
12729 loc_first_p = &wp_loc_first;
12730 else if (b->type == bp_read_watchpoint)
12731 loc_first_p = &rwp_loc_first;
12732 else if (b->type == bp_access_watchpoint)
12733 loc_first_p = &awp_loc_first;
12734 else
12735 loc_first_p = &bp_loc_first;
12736
12737 if (*loc_first_p == NULL
12738 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12739 || !breakpoint_locations_match (loc, *loc_first_p))
12740 {
12741 *loc_first_p = loc;
12742 loc->duplicate = 0;
b775012e
LM
12743
12744 if (is_breakpoint (loc->owner) && loc->condition_changed)
12745 {
12746 loc->needs_update = 1;
12747 /* Clear the condition modification flag. */
12748 loc->condition_changed = condition_unchanged;
12749 }
348d480f
PA
12750 continue;
12751 }
12752
934709f0
PW
12753
12754 /* This and the above ensure the invariant that the first location
12755 is not duplicated, and is the inserted one.
12756 All following are marked as duplicated, and are not inserted. */
12757 if (loc->inserted)
12758 swap_insertion (loc, *loc_first_p);
348d480f
PA
12759 loc->duplicate = 1;
12760
b775012e
LM
12761 /* Clear the condition modification flag. */
12762 loc->condition_changed = condition_unchanged;
12763
348d480f
PA
12764 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12765 && b->enable_state != bp_permanent)
12766 internal_error (__FILE__, __LINE__,
12767 _("another breakpoint was inserted on top of "
12768 "a permanent breakpoint"));
12769 }
12770
b775012e 12771 if (breakpoints_always_inserted_mode ()
348d480f 12772 && (have_live_inferiors ()
f5656ead 12773 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12774 {
12775 if (should_insert)
12776 insert_breakpoint_locations ();
12777 else
12778 {
12779 /* Though should_insert is false, we may need to update conditions
12780 on the target's side if it is evaluating such conditions. We
12781 only update conditions for locations that are marked
12782 "needs_update". */
12783 update_inserted_breakpoint_locations ();
12784 }
12785 }
348d480f 12786
1e4d1764
YQ
12787 if (should_insert)
12788 download_tracepoint_locations ();
12789
348d480f
PA
12790 do_cleanups (cleanups);
12791}
12792
12793void
12794breakpoint_retire_moribund (void)
12795{
12796 struct bp_location *loc;
12797 int ix;
12798
12799 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12800 if (--(loc->events_till_retirement) == 0)
12801 {
12802 decref_bp_location (&loc);
12803 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12804 --ix;
12805 }
12806}
12807
12808static void
12809update_global_location_list_nothrow (int inserting)
12810{
bfd189b1 12811 volatile struct gdb_exception e;
348d480f
PA
12812
12813 TRY_CATCH (e, RETURN_MASK_ERROR)
12814 update_global_location_list (inserting);
12815}
12816
12817/* Clear BKP from a BPS. */
12818
12819static void
12820bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12821{
12822 bpstat bs;
12823
12824 for (bs = bps; bs; bs = bs->next)
12825 if (bs->breakpoint_at == bpt)
12826 {
12827 bs->breakpoint_at = NULL;
12828 bs->old_val = NULL;
12829 /* bs->commands will be freed later. */
12830 }
12831}
12832
12833/* Callback for iterate_over_threads. */
12834static int
12835bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12836{
12837 struct breakpoint *bpt = data;
12838
12839 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12840 return 0;
12841}
12842
12843/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12844 callbacks. */
12845
12846static void
12847say_where (struct breakpoint *b)
12848{
12849 struct value_print_options opts;
12850
12851 get_user_print_options (&opts);
12852
12853 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12854 single string. */
12855 if (b->loc == NULL)
12856 {
12857 printf_filtered (_(" (%s) pending."), b->addr_string);
12858 }
12859 else
12860 {
2f202fde 12861 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12862 {
12863 printf_filtered (" at ");
12864 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12865 gdb_stdout);
12866 }
2f202fde 12867 if (b->loc->symtab != NULL)
f8eba3c6
TT
12868 {
12869 /* If there is a single location, we can print the location
12870 more nicely. */
12871 if (b->loc->next == NULL)
12872 printf_filtered (": file %s, line %d.",
05cba821
JK
12873 symtab_to_filename_for_display (b->loc->symtab),
12874 b->loc->line_number);
f8eba3c6
TT
12875 else
12876 /* This is not ideal, but each location may have a
12877 different file name, and this at least reflects the
12878 real situation somewhat. */
12879 printf_filtered (": %s.", b->addr_string);
12880 }
348d480f
PA
12881
12882 if (b->loc->next)
12883 {
12884 struct bp_location *loc = b->loc;
12885 int n = 0;
12886 for (; loc; loc = loc->next)
12887 ++n;
12888 printf_filtered (" (%d locations)", n);
12889 }
12890 }
12891}
12892
348d480f
PA
12893/* Default bp_location_ops methods. */
12894
12895static void
12896bp_location_dtor (struct bp_location *self)
12897{
12898 xfree (self->cond);
b775012e
LM
12899 if (self->cond_bytecode)
12900 free_agent_expr (self->cond_bytecode);
348d480f 12901 xfree (self->function_name);
8b4f3082
PA
12902
12903 VEC_free (agent_expr_p, self->target_info.conditions);
12904 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12905}
12906
12907static const struct bp_location_ops bp_location_ops =
12908{
12909 bp_location_dtor
12910};
12911
2060206e
PA
12912/* Default breakpoint_ops methods all breakpoint_ops ultimately
12913 inherit from. */
348d480f 12914
2060206e
PA
12915static void
12916base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12917{
12918 decref_counted_command_line (&self->commands);
12919 xfree (self->cond_string);
fb81d016 12920 xfree (self->extra_string);
348d480f 12921 xfree (self->addr_string);
f8eba3c6 12922 xfree (self->filter);
348d480f 12923 xfree (self->addr_string_range_end);
348d480f
PA
12924}
12925
2060206e
PA
12926static struct bp_location *
12927base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12928{
12929 struct bp_location *loc;
12930
12931 loc = XNEW (struct bp_location);
12932 init_bp_location (loc, &bp_location_ops, self);
12933 return loc;
12934}
12935
2060206e
PA
12936static void
12937base_breakpoint_re_set (struct breakpoint *b)
12938{
12939 /* Nothing to re-set. */
12940}
12941
12942#define internal_error_pure_virtual_called() \
12943 gdb_assert_not_reached ("pure virtual function called")
12944
12945static int
12946base_breakpoint_insert_location (struct bp_location *bl)
12947{
12948 internal_error_pure_virtual_called ();
12949}
12950
12951static int
12952base_breakpoint_remove_location (struct bp_location *bl)
12953{
12954 internal_error_pure_virtual_called ();
12955}
12956
12957static int
12958base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12959 struct address_space *aspace,
09ac7c10
TT
12960 CORE_ADDR bp_addr,
12961 const struct target_waitstatus *ws)
2060206e
PA
12962{
12963 internal_error_pure_virtual_called ();
12964}
12965
12966static void
12967base_breakpoint_check_status (bpstat bs)
12968{
12969 /* Always stop. */
12970}
12971
12972/* A "works_in_software_mode" breakpoint_ops method that just internal
12973 errors. */
12974
12975static int
12976base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12977{
12978 internal_error_pure_virtual_called ();
12979}
12980
12981/* A "resources_needed" breakpoint_ops method that just internal
12982 errors. */
12983
12984static int
12985base_breakpoint_resources_needed (const struct bp_location *bl)
12986{
12987 internal_error_pure_virtual_called ();
12988}
12989
12990static enum print_stop_action
12991base_breakpoint_print_it (bpstat bs)
12992{
12993 internal_error_pure_virtual_called ();
12994}
12995
12996static void
12997base_breakpoint_print_one_detail (const struct breakpoint *self,
12998 struct ui_out *uiout)
12999{
13000 /* nothing */
13001}
13002
13003static void
13004base_breakpoint_print_mention (struct breakpoint *b)
13005{
13006 internal_error_pure_virtual_called ();
13007}
13008
13009static void
13010base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13011{
13012 internal_error_pure_virtual_called ();
13013}
13014
983af33b
SDJ
13015static void
13016base_breakpoint_create_sals_from_address (char **arg,
13017 struct linespec_result *canonical,
13018 enum bptype type_wanted,
13019 char *addr_start,
13020 char **copy_arg)
13021{
13022 internal_error_pure_virtual_called ();
13023}
13024
13025static void
13026base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13027 struct linespec_result *c,
983af33b 13028 char *cond_string,
e7e0cddf 13029 char *extra_string,
983af33b
SDJ
13030 enum bptype type_wanted,
13031 enum bpdisp disposition,
13032 int thread,
13033 int task, int ignore_count,
13034 const struct breakpoint_ops *o,
13035 int from_tty, int enabled,
44f238bb 13036 int internal, unsigned flags)
983af33b
SDJ
13037{
13038 internal_error_pure_virtual_called ();
13039}
13040
13041static void
13042base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13043 struct symtabs_and_lines *sals)
13044{
13045 internal_error_pure_virtual_called ();
13046}
13047
ab04a2af
TT
13048/* The default 'explains_signal' method. */
13049
47591c29 13050static int
427cd150 13051base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13052{
47591c29 13053 return 1;
ab04a2af
TT
13054}
13055
9d6e6e84
HZ
13056/* The default "after_condition_true" method. */
13057
13058static void
13059base_breakpoint_after_condition_true (struct bpstats *bs)
13060{
13061 /* Nothing to do. */
13062}
13063
ab04a2af 13064struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13065{
13066 base_breakpoint_dtor,
13067 base_breakpoint_allocate_location,
13068 base_breakpoint_re_set,
13069 base_breakpoint_insert_location,
13070 base_breakpoint_remove_location,
13071 base_breakpoint_breakpoint_hit,
13072 base_breakpoint_check_status,
13073 base_breakpoint_resources_needed,
13074 base_breakpoint_works_in_software_mode,
13075 base_breakpoint_print_it,
13076 NULL,
13077 base_breakpoint_print_one_detail,
13078 base_breakpoint_print_mention,
983af33b
SDJ
13079 base_breakpoint_print_recreate,
13080 base_breakpoint_create_sals_from_address,
13081 base_breakpoint_create_breakpoints_sal,
13082 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13083 base_breakpoint_explains_signal,
13084 base_breakpoint_after_condition_true,
2060206e
PA
13085};
13086
13087/* Default breakpoint_ops methods. */
13088
13089static void
348d480f
PA
13090bkpt_re_set (struct breakpoint *b)
13091{
06edf0c0
PA
13092 /* FIXME: is this still reachable? */
13093 if (b->addr_string == NULL)
13094 {
13095 /* Anything without a string can't be re-set. */
348d480f 13096 delete_breakpoint (b);
06edf0c0 13097 return;
348d480f 13098 }
06edf0c0
PA
13099
13100 breakpoint_re_set_default (b);
348d480f
PA
13101}
13102
2060206e 13103static int
348d480f
PA
13104bkpt_insert_location (struct bp_location *bl)
13105{
13106 if (bl->loc_type == bp_loc_hardware_breakpoint)
13107 return target_insert_hw_breakpoint (bl->gdbarch,
13108 &bl->target_info);
13109 else
13110 return target_insert_breakpoint (bl->gdbarch,
13111 &bl->target_info);
13112}
13113
2060206e 13114static int
348d480f
PA
13115bkpt_remove_location (struct bp_location *bl)
13116{
13117 if (bl->loc_type == bp_loc_hardware_breakpoint)
13118 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13119 else
13120 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13121}
13122
2060206e 13123static int
348d480f 13124bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13125 struct address_space *aspace, CORE_ADDR bp_addr,
13126 const struct target_waitstatus *ws)
348d480f 13127{
09ac7c10 13128 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13129 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13130 return 0;
13131
348d480f
PA
13132 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13133 aspace, bp_addr))
13134 return 0;
13135
13136 if (overlay_debugging /* unmapped overlay section */
13137 && section_is_overlay (bl->section)
13138 && !section_is_mapped (bl->section))
13139 return 0;
13140
13141 return 1;
13142}
13143
2060206e 13144static int
348d480f
PA
13145bkpt_resources_needed (const struct bp_location *bl)
13146{
13147 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13148
13149 return 1;
13150}
13151
2060206e 13152static enum print_stop_action
348d480f
PA
13153bkpt_print_it (bpstat bs)
13154{
348d480f
PA
13155 struct breakpoint *b;
13156 const struct bp_location *bl;
001c8c33 13157 int bp_temp;
79a45e25 13158 struct ui_out *uiout = current_uiout;
348d480f
PA
13159
13160 gdb_assert (bs->bp_location_at != NULL);
13161
13162 bl = bs->bp_location_at;
13163 b = bs->breakpoint_at;
13164
001c8c33
PA
13165 bp_temp = b->disposition == disp_del;
13166 if (bl->address != bl->requested_address)
13167 breakpoint_adjustment_warning (bl->requested_address,
13168 bl->address,
13169 b->number, 1);
13170 annotate_breakpoint (b->number);
13171 if (bp_temp)
13172 ui_out_text (uiout, "\nTemporary breakpoint ");
13173 else
13174 ui_out_text (uiout, "\nBreakpoint ");
13175 if (ui_out_is_mi_like_p (uiout))
348d480f 13176 {
001c8c33
PA
13177 ui_out_field_string (uiout, "reason",
13178 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13179 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13180 }
001c8c33
PA
13181 ui_out_field_int (uiout, "bkptno", b->number);
13182 ui_out_text (uiout, ", ");
06edf0c0 13183
001c8c33 13184 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13185}
13186
2060206e 13187static void
06edf0c0
PA
13188bkpt_print_mention (struct breakpoint *b)
13189{
79a45e25 13190 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13191 return;
13192
13193 switch (b->type)
13194 {
13195 case bp_breakpoint:
13196 case bp_gnu_ifunc_resolver:
13197 if (b->disposition == disp_del)
13198 printf_filtered (_("Temporary breakpoint"));
13199 else
13200 printf_filtered (_("Breakpoint"));
13201 printf_filtered (_(" %d"), b->number);
13202 if (b->type == bp_gnu_ifunc_resolver)
13203 printf_filtered (_(" at gnu-indirect-function resolver"));
13204 break;
13205 case bp_hardware_breakpoint:
13206 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13207 break;
e7e0cddf
SS
13208 case bp_dprintf:
13209 printf_filtered (_("Dprintf %d"), b->number);
13210 break;
06edf0c0
PA
13211 }
13212
13213 say_where (b);
13214}
13215
2060206e 13216static void
06edf0c0
PA
13217bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13218{
13219 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13220 fprintf_unfiltered (fp, "tbreak");
13221 else if (tp->type == bp_breakpoint)
13222 fprintf_unfiltered (fp, "break");
13223 else if (tp->type == bp_hardware_breakpoint
13224 && tp->disposition == disp_del)
13225 fprintf_unfiltered (fp, "thbreak");
13226 else if (tp->type == bp_hardware_breakpoint)
13227 fprintf_unfiltered (fp, "hbreak");
13228 else
13229 internal_error (__FILE__, __LINE__,
13230 _("unhandled breakpoint type %d"), (int) tp->type);
13231
2060206e 13232 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13233 print_recreate_thread (tp, fp);
06edf0c0
PA
13234}
13235
983af33b
SDJ
13236static void
13237bkpt_create_sals_from_address (char **arg,
13238 struct linespec_result *canonical,
13239 enum bptype type_wanted,
13240 char *addr_start, char **copy_arg)
13241{
13242 create_sals_from_address_default (arg, canonical, type_wanted,
13243 addr_start, copy_arg);
13244}
13245
13246static void
13247bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13248 struct linespec_result *canonical,
983af33b 13249 char *cond_string,
e7e0cddf 13250 char *extra_string,
983af33b
SDJ
13251 enum bptype type_wanted,
13252 enum bpdisp disposition,
13253 int thread,
13254 int task, int ignore_count,
13255 const struct breakpoint_ops *ops,
13256 int from_tty, int enabled,
44f238bb 13257 int internal, unsigned flags)
983af33b 13258{
023fa29b 13259 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13260 cond_string, extra_string,
13261 type_wanted,
983af33b
SDJ
13262 disposition, thread, task,
13263 ignore_count, ops, from_tty,
44f238bb 13264 enabled, internal, flags);
983af33b
SDJ
13265}
13266
13267static void
13268bkpt_decode_linespec (struct breakpoint *b, char **s,
13269 struct symtabs_and_lines *sals)
13270{
13271 decode_linespec_default (b, s, sals);
13272}
13273
06edf0c0
PA
13274/* Virtual table for internal breakpoints. */
13275
13276static void
13277internal_bkpt_re_set (struct breakpoint *b)
13278{
13279 switch (b->type)
13280 {
13281 /* Delete overlay event and longjmp master breakpoints; they
13282 will be reset later by breakpoint_re_set. */
13283 case bp_overlay_event:
13284 case bp_longjmp_master:
13285 case bp_std_terminate_master:
13286 case bp_exception_master:
13287 delete_breakpoint (b);
13288 break;
13289
13290 /* This breakpoint is special, it's set up when the inferior
13291 starts and we really don't want to touch it. */
13292 case bp_shlib_event:
13293
13294 /* Like bp_shlib_event, this breakpoint type is special. Once
13295 it is set up, we do not want to touch it. */
13296 case bp_thread_event:
13297 break;
13298 }
13299}
13300
13301static void
13302internal_bkpt_check_status (bpstat bs)
13303{
a9b3a50f
PA
13304 if (bs->breakpoint_at->type == bp_shlib_event)
13305 {
13306 /* If requested, stop when the dynamic linker notifies GDB of
13307 events. This allows the user to get control and place
13308 breakpoints in initializer routines for dynamically loaded
13309 objects (among other things). */
13310 bs->stop = stop_on_solib_events;
13311 bs->print = stop_on_solib_events;
13312 }
13313 else
13314 bs->stop = 0;
06edf0c0
PA
13315}
13316
13317static enum print_stop_action
13318internal_bkpt_print_it (bpstat bs)
13319{
06edf0c0 13320 struct breakpoint *b;
06edf0c0 13321
06edf0c0
PA
13322 b = bs->breakpoint_at;
13323
06edf0c0
PA
13324 switch (b->type)
13325 {
348d480f
PA
13326 case bp_shlib_event:
13327 /* Did we stop because the user set the stop_on_solib_events
13328 variable? (If so, we report this as a generic, "Stopped due
13329 to shlib event" message.) */
edcc5120 13330 print_solib_event (0);
348d480f
PA
13331 break;
13332
13333 case bp_thread_event:
13334 /* Not sure how we will get here.
13335 GDB should not stop for these breakpoints. */
13336 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13337 break;
13338
13339 case bp_overlay_event:
13340 /* By analogy with the thread event, GDB should not stop for these. */
13341 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13342 break;
13343
13344 case bp_longjmp_master:
13345 /* These should never be enabled. */
13346 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13347 break;
13348
13349 case bp_std_terminate_master:
13350 /* These should never be enabled. */
13351 printf_filtered (_("std::terminate Master Breakpoint: "
13352 "gdb should not stop!\n"));
348d480f
PA
13353 break;
13354
13355 case bp_exception_master:
13356 /* These should never be enabled. */
13357 printf_filtered (_("Exception Master Breakpoint: "
13358 "gdb should not stop!\n"));
06edf0c0
PA
13359 break;
13360 }
13361
001c8c33 13362 return PRINT_NOTHING;
06edf0c0
PA
13363}
13364
13365static void
13366internal_bkpt_print_mention (struct breakpoint *b)
13367{
13368 /* Nothing to mention. These breakpoints are internal. */
13369}
13370
06edf0c0
PA
13371/* Virtual table for momentary breakpoints */
13372
13373static void
13374momentary_bkpt_re_set (struct breakpoint *b)
13375{
13376 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13377 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13378 Otherwise these should have been blown away via the cleanup chain
13379 or by breakpoint_init_inferior when we rerun the executable. */
13380}
13381
13382static void
13383momentary_bkpt_check_status (bpstat bs)
13384{
13385 /* Nothing. The point of these breakpoints is causing a stop. */
13386}
13387
13388static enum print_stop_action
13389momentary_bkpt_print_it (bpstat bs)
13390{
79a45e25
PA
13391 struct ui_out *uiout = current_uiout;
13392
001c8c33 13393 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13394 {
001c8c33 13395 struct breakpoint *b = bs->breakpoint_at;
348d480f 13396
001c8c33
PA
13397 switch (b->type)
13398 {
13399 case bp_finish:
13400 ui_out_field_string
13401 (uiout, "reason",
13402 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13403 break;
348d480f 13404
001c8c33
PA
13405 case bp_until:
13406 ui_out_field_string
13407 (uiout, "reason",
13408 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13409 break;
13410 }
348d480f
PA
13411 }
13412
001c8c33 13413 return PRINT_UNKNOWN;
348d480f
PA
13414}
13415
06edf0c0
PA
13416static void
13417momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13418{
06edf0c0 13419 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13420}
13421
e2e4d78b
JK
13422/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13423
13424 It gets cleared already on the removal of the first one of such placed
13425 breakpoints. This is OK as they get all removed altogether. */
13426
13427static void
13428longjmp_bkpt_dtor (struct breakpoint *self)
13429{
13430 struct thread_info *tp = find_thread_id (self->thread);
13431
13432 if (tp)
13433 tp->initiating_frame = null_frame_id;
13434
13435 momentary_breakpoint_ops.dtor (self);
13436}
13437
55aa24fb
SDJ
13438/* Specific methods for probe breakpoints. */
13439
13440static int
13441bkpt_probe_insert_location (struct bp_location *bl)
13442{
13443 int v = bkpt_insert_location (bl);
13444
13445 if (v == 0)
13446 {
13447 /* The insertion was successful, now let's set the probe's semaphore
13448 if needed. */
729662a5
TT
13449 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13450 bl->probe.objfile,
13451 bl->gdbarch);
55aa24fb
SDJ
13452 }
13453
13454 return v;
13455}
13456
13457static int
13458bkpt_probe_remove_location (struct bp_location *bl)
13459{
13460 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13461 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13462 bl->probe.objfile,
13463 bl->gdbarch);
55aa24fb
SDJ
13464
13465 return bkpt_remove_location (bl);
13466}
13467
13468static void
13469bkpt_probe_create_sals_from_address (char **arg,
13470 struct linespec_result *canonical,
13471 enum bptype type_wanted,
13472 char *addr_start, char **copy_arg)
13473{
13474 struct linespec_sals lsal;
13475
13476 lsal.sals = parse_probes (arg, canonical);
13477
13478 *copy_arg = xstrdup (canonical->addr_string);
13479 lsal.canonical = xstrdup (*copy_arg);
13480
13481 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13482}
13483
13484static void
13485bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13486 struct symtabs_and_lines *sals)
13487{
13488 *sals = parse_probes (s, NULL);
13489 if (!sals->sals)
13490 error (_("probe not found"));
13491}
13492
348d480f 13493/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13494
348d480f
PA
13495static void
13496tracepoint_re_set (struct breakpoint *b)
13497{
13498 breakpoint_re_set_default (b);
13499}
876fa593 13500
348d480f
PA
13501static int
13502tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13503 struct address_space *aspace, CORE_ADDR bp_addr,
13504 const struct target_waitstatus *ws)
348d480f
PA
13505{
13506 /* By definition, the inferior does not report stops at
13507 tracepoints. */
13508 return 0;
74960c60
VP
13509}
13510
13511static void
348d480f
PA
13512tracepoint_print_one_detail (const struct breakpoint *self,
13513 struct ui_out *uiout)
74960c60 13514{
d9b3f62e
PA
13515 struct tracepoint *tp = (struct tracepoint *) self;
13516 if (tp->static_trace_marker_id)
348d480f
PA
13517 {
13518 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13519
348d480f
PA
13520 ui_out_text (uiout, "\tmarker id is ");
13521 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13522 tp->static_trace_marker_id);
348d480f
PA
13523 ui_out_text (uiout, "\n");
13524 }
0d381245
VP
13525}
13526
a474d7c2 13527static void
348d480f 13528tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13529{
79a45e25 13530 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13531 return;
cc59ec59 13532
348d480f
PA
13533 switch (b->type)
13534 {
13535 case bp_tracepoint:
13536 printf_filtered (_("Tracepoint"));
13537 printf_filtered (_(" %d"), b->number);
13538 break;
13539 case bp_fast_tracepoint:
13540 printf_filtered (_("Fast tracepoint"));
13541 printf_filtered (_(" %d"), b->number);
13542 break;
13543 case bp_static_tracepoint:
13544 printf_filtered (_("Static tracepoint"));
13545 printf_filtered (_(" %d"), b->number);
13546 break;
13547 default:
13548 internal_error (__FILE__, __LINE__,
13549 _("unhandled tracepoint type %d"), (int) b->type);
13550 }
13551
13552 say_where (b);
a474d7c2
PA
13553}
13554
348d480f 13555static void
d9b3f62e 13556tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13557{
d9b3f62e
PA
13558 struct tracepoint *tp = (struct tracepoint *) self;
13559
13560 if (self->type == bp_fast_tracepoint)
348d480f 13561 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13562 if (self->type == bp_static_tracepoint)
348d480f 13563 fprintf_unfiltered (fp, "strace");
d9b3f62e 13564 else if (self->type == bp_tracepoint)
348d480f
PA
13565 fprintf_unfiltered (fp, "trace");
13566 else
13567 internal_error (__FILE__, __LINE__,
d9b3f62e 13568 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13569
d9b3f62e
PA
13570 fprintf_unfiltered (fp, " %s", self->addr_string);
13571 print_recreate_thread (self, fp);
13572
13573 if (tp->pass_count)
13574 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13575}
13576
983af33b
SDJ
13577static void
13578tracepoint_create_sals_from_address (char **arg,
13579 struct linespec_result *canonical,
13580 enum bptype type_wanted,
13581 char *addr_start, char **copy_arg)
13582{
13583 create_sals_from_address_default (arg, canonical, type_wanted,
13584 addr_start, copy_arg);
13585}
13586
13587static void
13588tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13589 struct linespec_result *canonical,
983af33b 13590 char *cond_string,
e7e0cddf 13591 char *extra_string,
983af33b
SDJ
13592 enum bptype type_wanted,
13593 enum bpdisp disposition,
13594 int thread,
13595 int task, int ignore_count,
13596 const struct breakpoint_ops *ops,
13597 int from_tty, int enabled,
44f238bb 13598 int internal, unsigned flags)
983af33b 13599{
023fa29b 13600 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13601 cond_string, extra_string,
13602 type_wanted,
983af33b
SDJ
13603 disposition, thread, task,
13604 ignore_count, ops, from_tty,
44f238bb 13605 enabled, internal, flags);
983af33b
SDJ
13606}
13607
13608static void
13609tracepoint_decode_linespec (struct breakpoint *b, char **s,
13610 struct symtabs_and_lines *sals)
13611{
13612 decode_linespec_default (b, s, sals);
13613}
13614
2060206e 13615struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13616
55aa24fb
SDJ
13617/* The breakpoint_ops structure to be use on tracepoints placed in a
13618 static probe. */
13619
13620static void
13621tracepoint_probe_create_sals_from_address (char **arg,
13622 struct linespec_result *canonical,
13623 enum bptype type_wanted,
13624 char *addr_start, char **copy_arg)
13625{
13626 /* We use the same method for breakpoint on probes. */
13627 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13628 addr_start, copy_arg);
13629}
13630
13631static void
13632tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13633 struct symtabs_and_lines *sals)
13634{
13635 /* We use the same method for breakpoint on probes. */
13636 bkpt_probe_decode_linespec (b, s, sals);
13637}
13638
13639static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13640
5c2b4418
HZ
13641/* Dprintf breakpoint_ops methods. */
13642
13643static void
13644dprintf_re_set (struct breakpoint *b)
13645{
13646 breakpoint_re_set_default (b);
13647
13648 /* This breakpoint could have been pending, and be resolved now, and
13649 if so, we should now have the extra string. If we don't, the
13650 dprintf was malformed when created, but we couldn't tell because
13651 we can't extract the extra string until the location is
13652 resolved. */
13653 if (b->loc != NULL && b->extra_string == NULL)
13654 error (_("Format string required"));
13655
13656 /* 1 - connect to target 1, that can run breakpoint commands.
13657 2 - create a dprintf, which resolves fine.
13658 3 - disconnect from target 1
13659 4 - connect to target 2, that can NOT run breakpoint commands.
13660
13661 After steps #3/#4, you'll want the dprintf command list to
13662 be updated, because target 1 and 2 may well return different
13663 answers for target_can_run_breakpoint_commands().
13664 Given absence of finer grained resetting, we get to do
13665 it all the time. */
13666 if (b->extra_string != NULL)
13667 update_dprintf_command_list (b);
13668}
13669
2d9442cc
HZ
13670/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13671
13672static void
13673dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13674{
13675 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13676 tp->extra_string);
13677 print_recreate_thread (tp, fp);
13678}
13679
9d6e6e84
HZ
13680/* Implement the "after_condition_true" breakpoint_ops method for
13681 dprintf.
13682
13683 dprintf's are implemented with regular commands in their command
13684 list, but we run the commands here instead of before presenting the
13685 stop to the user, as dprintf's don't actually cause a stop. This
13686 also makes it so that the commands of multiple dprintfs at the same
13687 address are all handled. */
13688
13689static void
13690dprintf_after_condition_true (struct bpstats *bs)
13691{
13692 struct cleanup *old_chain;
13693 struct bpstats tmp_bs = { NULL };
13694 struct bpstats *tmp_bs_p = &tmp_bs;
13695
13696 /* dprintf's never cause a stop. This wasn't set in the
13697 check_status hook instead because that would make the dprintf's
13698 condition not be evaluated. */
13699 bs->stop = 0;
13700
13701 /* Run the command list here. Take ownership of it instead of
13702 copying. We never want these commands to run later in
13703 bpstat_do_actions, if a breakpoint that causes a stop happens to
13704 be set at same address as this dprintf, or even if running the
13705 commands here throws. */
13706 tmp_bs.commands = bs->commands;
13707 bs->commands = NULL;
13708 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13709
13710 bpstat_do_actions_1 (&tmp_bs_p);
13711
13712 /* 'tmp_bs.commands' will usually be NULL by now, but
13713 bpstat_do_actions_1 may return early without processing the whole
13714 list. */
13715 do_cleanups (old_chain);
13716}
13717
983af33b
SDJ
13718/* The breakpoint_ops structure to be used on static tracepoints with
13719 markers (`-m'). */
13720
13721static void
13722strace_marker_create_sals_from_address (char **arg,
13723 struct linespec_result *canonical,
13724 enum bptype type_wanted,
13725 char *addr_start, char **copy_arg)
13726{
13727 struct linespec_sals lsal;
13728
13729 lsal.sals = decode_static_tracepoint_spec (arg);
13730
13731 *copy_arg = savestring (addr_start, *arg - addr_start);
13732
13733 canonical->addr_string = xstrdup (*copy_arg);
13734 lsal.canonical = xstrdup (*copy_arg);
13735 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13736}
13737
13738static void
13739strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13740 struct linespec_result *canonical,
983af33b 13741 char *cond_string,
e7e0cddf 13742 char *extra_string,
983af33b
SDJ
13743 enum bptype type_wanted,
13744 enum bpdisp disposition,
13745 int thread,
13746 int task, int ignore_count,
13747 const struct breakpoint_ops *ops,
13748 int from_tty, int enabled,
44f238bb 13749 int internal, unsigned flags)
983af33b
SDJ
13750{
13751 int i;
52d361e1
YQ
13752 struct linespec_sals *lsal = VEC_index (linespec_sals,
13753 canonical->sals, 0);
983af33b
SDJ
13754
13755 /* If the user is creating a static tracepoint by marker id
13756 (strace -m MARKER_ID), then store the sals index, so that
13757 breakpoint_re_set can try to match up which of the newly
13758 found markers corresponds to this one, and, don't try to
13759 expand multiple locations for each sal, given than SALS
13760 already should contain all sals for MARKER_ID. */
13761
13762 for (i = 0; i < lsal->sals.nelts; ++i)
13763 {
13764 struct symtabs_and_lines expanded;
13765 struct tracepoint *tp;
13766 struct cleanup *old_chain;
13767 char *addr_string;
13768
13769 expanded.nelts = 1;
13770 expanded.sals = &lsal->sals.sals[i];
13771
13772 addr_string = xstrdup (canonical->addr_string);
13773 old_chain = make_cleanup (xfree, addr_string);
13774
13775 tp = XCNEW (struct tracepoint);
13776 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13777 addr_string, NULL,
e7e0cddf
SS
13778 cond_string, extra_string,
13779 type_wanted, disposition,
983af33b 13780 thread, task, ignore_count, ops,
44f238bb 13781 from_tty, enabled, internal, flags,
983af33b
SDJ
13782 canonical->special_display);
13783 /* Given that its possible to have multiple markers with
13784 the same string id, if the user is creating a static
13785 tracepoint by marker id ("strace -m MARKER_ID"), then
13786 store the sals index, so that breakpoint_re_set can
13787 try to match up which of the newly found markers
13788 corresponds to this one */
13789 tp->static_trace_marker_id_idx = i;
13790
13791 install_breakpoint (internal, &tp->base, 0);
13792
13793 discard_cleanups (old_chain);
13794 }
13795}
13796
13797static void
13798strace_marker_decode_linespec (struct breakpoint *b, char **s,
13799 struct symtabs_and_lines *sals)
13800{
13801 struct tracepoint *tp = (struct tracepoint *) b;
13802
13803 *sals = decode_static_tracepoint_spec (s);
13804 if (sals->nelts > tp->static_trace_marker_id_idx)
13805 {
13806 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13807 sals->nelts = 1;
13808 }
13809 else
13810 error (_("marker %s not found"), tp->static_trace_marker_id);
13811}
13812
13813static struct breakpoint_ops strace_marker_breakpoint_ops;
13814
13815static int
13816strace_marker_p (struct breakpoint *b)
13817{
13818 return b->ops == &strace_marker_breakpoint_ops;
13819}
13820
53a5351d 13821/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13822 structures. */
c906108c
SS
13823
13824void
fba45db2 13825delete_breakpoint (struct breakpoint *bpt)
c906108c 13826{
52f0bd74 13827 struct breakpoint *b;
c906108c 13828
8a3fe4f8 13829 gdb_assert (bpt != NULL);
c906108c 13830
4a64f543
MS
13831 /* Has this bp already been deleted? This can happen because
13832 multiple lists can hold pointers to bp's. bpstat lists are
13833 especial culprits.
13834
13835 One example of this happening is a watchpoint's scope bp. When
13836 the scope bp triggers, we notice that the watchpoint is out of
13837 scope, and delete it. We also delete its scope bp. But the
13838 scope bp is marked "auto-deleting", and is already on a bpstat.
13839 That bpstat is then checked for auto-deleting bp's, which are
13840 deleted.
13841
13842 A real solution to this problem might involve reference counts in
13843 bp's, and/or giving them pointers back to their referencing
13844 bpstat's, and teaching delete_breakpoint to only free a bp's
13845 storage when no more references were extent. A cheaper bandaid
13846 was chosen. */
c906108c
SS
13847 if (bpt->type == bp_none)
13848 return;
13849
4a64f543
MS
13850 /* At least avoid this stale reference until the reference counting
13851 of breakpoints gets resolved. */
d0fb5eae 13852 if (bpt->related_breakpoint != bpt)
e5a0a904 13853 {
d0fb5eae 13854 struct breakpoint *related;
3a5c3e22 13855 struct watchpoint *w;
d0fb5eae
JK
13856
13857 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13858 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13859 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13860 w = (struct watchpoint *) bpt;
13861 else
13862 w = NULL;
13863 if (w != NULL)
13864 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13865
13866 /* Unlink bpt from the bpt->related_breakpoint ring. */
13867 for (related = bpt; related->related_breakpoint != bpt;
13868 related = related->related_breakpoint);
13869 related->related_breakpoint = bpt->related_breakpoint;
13870 bpt->related_breakpoint = bpt;
e5a0a904
JK
13871 }
13872
a9634178
TJB
13873 /* watch_command_1 creates a watchpoint but only sets its number if
13874 update_watchpoint succeeds in creating its bp_locations. If there's
13875 a problem in that process, we'll be asked to delete the half-created
13876 watchpoint. In that case, don't announce the deletion. */
13877 if (bpt->number)
13878 observer_notify_breakpoint_deleted (bpt);
c906108c 13879
c906108c
SS
13880 if (breakpoint_chain == bpt)
13881 breakpoint_chain = bpt->next;
13882
c906108c
SS
13883 ALL_BREAKPOINTS (b)
13884 if (b->next == bpt)
c5aa993b
JM
13885 {
13886 b->next = bpt->next;
13887 break;
13888 }
c906108c 13889
f431efe5
PA
13890 /* Be sure no bpstat's are pointing at the breakpoint after it's
13891 been freed. */
13892 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13893 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13894 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13895 commands are associated with the bpstat; if we remove it here,
13896 then the later call to bpstat_do_actions (&stop_bpstat); in
13897 event-top.c won't do anything, and temporary breakpoints with
13898 commands won't work. */
13899
13900 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13901
4a64f543
MS
13902 /* Now that breakpoint is removed from breakpoint list, update the
13903 global location list. This will remove locations that used to
13904 belong to this breakpoint. Do this before freeing the breakpoint
13905 itself, since remove_breakpoint looks at location's owner. It
13906 might be better design to have location completely
13907 self-contained, but it's not the case now. */
b60e7edf 13908 update_global_location_list (0);
74960c60 13909
348d480f 13910 bpt->ops->dtor (bpt);
4a64f543
MS
13911 /* On the chance that someone will soon try again to delete this
13912 same bp, we mark it as deleted before freeing its storage. */
c906108c 13913 bpt->type = bp_none;
b8c9b27d 13914 xfree (bpt);
c906108c
SS
13915}
13916
4d6140d9
AC
13917static void
13918do_delete_breakpoint_cleanup (void *b)
13919{
13920 delete_breakpoint (b);
13921}
13922
13923struct cleanup *
13924make_cleanup_delete_breakpoint (struct breakpoint *b)
13925{
13926 return make_cleanup (do_delete_breakpoint_cleanup, b);
13927}
13928
51be5b68
PA
13929/* Iterator function to call a user-provided callback function once
13930 for each of B and its related breakpoints. */
13931
13932static void
13933iterate_over_related_breakpoints (struct breakpoint *b,
13934 void (*function) (struct breakpoint *,
13935 void *),
13936 void *data)
13937{
13938 struct breakpoint *related;
13939
13940 related = b;
13941 do
13942 {
13943 struct breakpoint *next;
13944
13945 /* FUNCTION may delete RELATED. */
13946 next = related->related_breakpoint;
13947
13948 if (next == related)
13949 {
13950 /* RELATED is the last ring entry. */
13951 function (related, data);
13952
13953 /* FUNCTION may have deleted it, so we'd never reach back to
13954 B. There's nothing left to do anyway, so just break
13955 out. */
13956 break;
13957 }
13958 else
13959 function (related, data);
13960
13961 related = next;
13962 }
13963 while (related != b);
13964}
95a42b64
TT
13965
13966static void
13967do_delete_breakpoint (struct breakpoint *b, void *ignore)
13968{
13969 delete_breakpoint (b);
13970}
13971
51be5b68
PA
13972/* A callback for map_breakpoint_numbers that calls
13973 delete_breakpoint. */
13974
13975static void
13976do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13977{
13978 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13979}
13980
c906108c 13981void
fba45db2 13982delete_command (char *arg, int from_tty)
c906108c 13983{
35df4500 13984 struct breakpoint *b, *b_tmp;
c906108c 13985
ea9365bb
TT
13986 dont_repeat ();
13987
c906108c
SS
13988 if (arg == 0)
13989 {
13990 int breaks_to_delete = 0;
13991
46c6471b
PA
13992 /* Delete all breakpoints if no argument. Do not delete
13993 internal breakpoints, these have to be deleted with an
13994 explicit breakpoint number argument. */
c5aa993b 13995 ALL_BREAKPOINTS (b)
46c6471b 13996 if (user_breakpoint_p (b))
973d738b
DJ
13997 {
13998 breaks_to_delete = 1;
13999 break;
14000 }
c906108c
SS
14001
14002 /* Ask user only if there are some breakpoints to delete. */
14003 if (!from_tty
e2e0b3e5 14004 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14005 {
35df4500 14006 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14007 if (user_breakpoint_p (b))
c5aa993b 14008 delete_breakpoint (b);
c906108c
SS
14009 }
14010 }
14011 else
51be5b68 14012 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14013}
14014
0d381245
VP
14015static int
14016all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14017{
0d381245 14018 for (; loc; loc = loc->next)
8645ff69
UW
14019 if (!loc->shlib_disabled
14020 && !loc->pspace->executing_startup)
0d381245
VP
14021 return 0;
14022 return 1;
fe3f5fa8
VP
14023}
14024
776592bf
DE
14025/* Subroutine of update_breakpoint_locations to simplify it.
14026 Return non-zero if multiple fns in list LOC have the same name.
14027 Null names are ignored. */
14028
14029static int
14030ambiguous_names_p (struct bp_location *loc)
14031{
14032 struct bp_location *l;
14033 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14034 (int (*) (const void *,
14035 const void *)) streq,
776592bf
DE
14036 NULL, xcalloc, xfree);
14037
14038 for (l = loc; l != NULL; l = l->next)
14039 {
14040 const char **slot;
14041 const char *name = l->function_name;
14042
14043 /* Allow for some names to be NULL, ignore them. */
14044 if (name == NULL)
14045 continue;
14046
14047 slot = (const char **) htab_find_slot (htab, (const void *) name,
14048 INSERT);
4a64f543
MS
14049 /* NOTE: We can assume slot != NULL here because xcalloc never
14050 returns NULL. */
776592bf
DE
14051 if (*slot != NULL)
14052 {
14053 htab_delete (htab);
14054 return 1;
14055 }
14056 *slot = name;
14057 }
14058
14059 htab_delete (htab);
14060 return 0;
14061}
14062
0fb4aa4b
PA
14063/* When symbols change, it probably means the sources changed as well,
14064 and it might mean the static tracepoint markers are no longer at
14065 the same address or line numbers they used to be at last we
14066 checked. Losing your static tracepoints whenever you rebuild is
14067 undesirable. This function tries to resync/rematch gdb static
14068 tracepoints with the markers on the target, for static tracepoints
14069 that have not been set by marker id. Static tracepoint that have
14070 been set by marker id are reset by marker id in breakpoint_re_set.
14071 The heuristic is:
14072
14073 1) For a tracepoint set at a specific address, look for a marker at
14074 the old PC. If one is found there, assume to be the same marker.
14075 If the name / string id of the marker found is different from the
14076 previous known name, assume that means the user renamed the marker
14077 in the sources, and output a warning.
14078
14079 2) For a tracepoint set at a given line number, look for a marker
14080 at the new address of the old line number. If one is found there,
14081 assume to be the same marker. If the name / string id of the
14082 marker found is different from the previous known name, assume that
14083 means the user renamed the marker in the sources, and output a
14084 warning.
14085
14086 3) If a marker is no longer found at the same address or line, it
14087 may mean the marker no longer exists. But it may also just mean
14088 the code changed a bit. Maybe the user added a few lines of code
14089 that made the marker move up or down (in line number terms). Ask
14090 the target for info about the marker with the string id as we knew
14091 it. If found, update line number and address in the matching
14092 static tracepoint. This will get confused if there's more than one
14093 marker with the same ID (possible in UST, although unadvised
14094 precisely because it confuses tools). */
14095
14096static struct symtab_and_line
14097update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14098{
d9b3f62e 14099 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14100 struct static_tracepoint_marker marker;
14101 CORE_ADDR pc;
0fb4aa4b
PA
14102
14103 pc = sal.pc;
14104 if (sal.line)
14105 find_line_pc (sal.symtab, sal.line, &pc);
14106
14107 if (target_static_tracepoint_marker_at (pc, &marker))
14108 {
d9b3f62e 14109 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14110 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14111 b->number,
d9b3f62e 14112 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14113
d9b3f62e
PA
14114 xfree (tp->static_trace_marker_id);
14115 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14116 release_static_tracepoint_marker (&marker);
14117
14118 return sal;
14119 }
14120
14121 /* Old marker wasn't found on target at lineno. Try looking it up
14122 by string ID. */
14123 if (!sal.explicit_pc
14124 && sal.line != 0
14125 && sal.symtab != NULL
d9b3f62e 14126 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14127 {
14128 VEC(static_tracepoint_marker_p) *markers;
14129
14130 markers
d9b3f62e 14131 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14132
14133 if (!VEC_empty(static_tracepoint_marker_p, markers))
14134 {
80e1d417 14135 struct symtab_and_line sal2;
0fb4aa4b 14136 struct symbol *sym;
80e1d417 14137 struct static_tracepoint_marker *tpmarker;
79a45e25 14138 struct ui_out *uiout = current_uiout;
0fb4aa4b 14139
80e1d417 14140 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14141
d9b3f62e 14142 xfree (tp->static_trace_marker_id);
80e1d417 14143 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14144
14145 warning (_("marker for static tracepoint %d (%s) not "
14146 "found at previous line number"),
d9b3f62e 14147 b->number, tp->static_trace_marker_id);
0fb4aa4b 14148
80e1d417 14149 init_sal (&sal2);
0fb4aa4b 14150
80e1d417 14151 sal2.pc = tpmarker->address;
0fb4aa4b 14152
80e1d417
AS
14153 sal2 = find_pc_line (tpmarker->address, 0);
14154 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14155 ui_out_text (uiout, "Now in ");
14156 if (sym)
14157 {
14158 ui_out_field_string (uiout, "func",
14159 SYMBOL_PRINT_NAME (sym));
14160 ui_out_text (uiout, " at ");
14161 }
05cba821
JK
14162 ui_out_field_string (uiout, "file",
14163 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14164 ui_out_text (uiout, ":");
14165
14166 if (ui_out_is_mi_like_p (uiout))
14167 {
0b0865da 14168 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14169
f35a17b5 14170 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14171 }
14172
80e1d417 14173 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14174 ui_out_text (uiout, "\n");
14175
80e1d417 14176 b->loc->line_number = sal2.line;
2f202fde 14177 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14178
14179 xfree (b->addr_string);
14180 b->addr_string = xstrprintf ("%s:%d",
05cba821 14181 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14182 b->loc->line_number);
0fb4aa4b
PA
14183
14184 /* Might be nice to check if function changed, and warn if
14185 so. */
14186
80e1d417 14187 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14188 }
14189 }
14190 return sal;
14191}
14192
8d3788bd
VP
14193/* Returns 1 iff locations A and B are sufficiently same that
14194 we don't need to report breakpoint as changed. */
14195
14196static int
14197locations_are_equal (struct bp_location *a, struct bp_location *b)
14198{
14199 while (a && b)
14200 {
14201 if (a->address != b->address)
14202 return 0;
14203
14204 if (a->shlib_disabled != b->shlib_disabled)
14205 return 0;
14206
14207 if (a->enabled != b->enabled)
14208 return 0;
14209
14210 a = a->next;
14211 b = b->next;
14212 }
14213
14214 if ((a == NULL) != (b == NULL))
14215 return 0;
14216
14217 return 1;
14218}
14219
f1310107
TJB
14220/* Create new breakpoint locations for B (a hardware or software breakpoint)
14221 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14222 a ranged breakpoint. */
14223
0e30163f 14224void
0d381245 14225update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14226 struct symtabs_and_lines sals,
14227 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14228{
14229 int i;
0d381245
VP
14230 struct bp_location *existing_locations = b->loc;
14231
f8eba3c6
TT
14232 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14233 {
14234 /* Ranged breakpoints have only one start location and one end
14235 location. */
14236 b->enable_state = bp_disabled;
14237 update_global_location_list (1);
14238 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14239 "multiple locations found\n"),
14240 b->number);
14241 return;
14242 }
f1310107 14243
4a64f543
MS
14244 /* If there's no new locations, and all existing locations are
14245 pending, don't do anything. This optimizes the common case where
14246 all locations are in the same shared library, that was unloaded.
14247 We'd like to retain the location, so that when the library is
14248 loaded again, we don't loose the enabled/disabled status of the
14249 individual locations. */
0d381245 14250 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14251 return;
14252
fe3f5fa8
VP
14253 b->loc = NULL;
14254
0d381245 14255 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14256 {
f8eba3c6
TT
14257 struct bp_location *new_loc;
14258
14259 switch_to_program_space_and_thread (sals.sals[i].pspace);
14260
14261 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14262
0d381245
VP
14263 /* Reparse conditions, they might contain references to the
14264 old symtab. */
14265 if (b->cond_string != NULL)
14266 {
bbc13ae3 14267 const char *s;
bfd189b1 14268 volatile struct gdb_exception e;
fe3f5fa8 14269
0d381245
VP
14270 s = b->cond_string;
14271 TRY_CATCH (e, RETURN_MASK_ERROR)
14272 {
1bb9788d
TT
14273 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14274 block_for_pc (sals.sals[i].pc),
0d381245
VP
14275 0);
14276 }
14277 if (e.reason < 0)
14278 {
3e43a32a
MS
14279 warning (_("failed to reevaluate condition "
14280 "for breakpoint %d: %s"),
0d381245
VP
14281 b->number, e.message);
14282 new_loc->enabled = 0;
14283 }
14284 }
fe3f5fa8 14285
f1310107
TJB
14286 if (sals_end.nelts)
14287 {
14288 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14289
14290 new_loc->length = end - sals.sals[0].pc + 1;
14291 }
0d381245 14292 }
fe3f5fa8 14293
514f746b
AR
14294 /* Update locations of permanent breakpoints. */
14295 if (b->enable_state == bp_permanent)
14296 make_breakpoint_permanent (b);
14297
4a64f543
MS
14298 /* If possible, carry over 'disable' status from existing
14299 breakpoints. */
0d381245
VP
14300 {
14301 struct bp_location *e = existing_locations;
776592bf
DE
14302 /* If there are multiple breakpoints with the same function name,
14303 e.g. for inline functions, comparing function names won't work.
14304 Instead compare pc addresses; this is just a heuristic as things
14305 may have moved, but in practice it gives the correct answer
14306 often enough until a better solution is found. */
14307 int have_ambiguous_names = ambiguous_names_p (b->loc);
14308
0d381245
VP
14309 for (; e; e = e->next)
14310 {
14311 if (!e->enabled && e->function_name)
14312 {
14313 struct bp_location *l = b->loc;
776592bf
DE
14314 if (have_ambiguous_names)
14315 {
14316 for (; l; l = l->next)
f1310107 14317 if (breakpoint_locations_match (e, l))
776592bf
DE
14318 {
14319 l->enabled = 0;
14320 break;
14321 }
14322 }
14323 else
14324 {
14325 for (; l; l = l->next)
14326 if (l->function_name
14327 && strcmp (e->function_name, l->function_name) == 0)
14328 {
14329 l->enabled = 0;
14330 break;
14331 }
14332 }
0d381245
VP
14333 }
14334 }
14335 }
fe3f5fa8 14336
8d3788bd
VP
14337 if (!locations_are_equal (existing_locations, b->loc))
14338 observer_notify_breakpoint_modified (b);
14339
b60e7edf 14340 update_global_location_list (1);
fe3f5fa8
VP
14341}
14342
ef23e705
TJB
14343/* Find the SaL locations corresponding to the given ADDR_STRING.
14344 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14345
14346static struct symtabs_and_lines
14347addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14348{
14349 char *s;
02d20e4a 14350 struct symtabs_and_lines sals = {0};
f8eba3c6 14351 volatile struct gdb_exception e;
ef23e705 14352
983af33b 14353 gdb_assert (b->ops != NULL);
ef23e705 14354 s = addr_string;
ef23e705
TJB
14355
14356 TRY_CATCH (e, RETURN_MASK_ERROR)
14357 {
983af33b 14358 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14359 }
14360 if (e.reason < 0)
14361 {
14362 int not_found_and_ok = 0;
14363 /* For pending breakpoints, it's expected that parsing will
14364 fail until the right shared library is loaded. User has
14365 already told to create pending breakpoints and don't need
14366 extra messages. If breakpoint is in bp_shlib_disabled
14367 state, then user already saw the message about that
14368 breakpoint being disabled, and don't want to see more
14369 errors. */
58438ac1 14370 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14371 && (b->condition_not_parsed
14372 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14373 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14374 || b->enable_state == bp_disabled))
14375 not_found_and_ok = 1;
14376
14377 if (!not_found_and_ok)
14378 {
14379 /* We surely don't want to warn about the same breakpoint
14380 10 times. One solution, implemented here, is disable
14381 the breakpoint on error. Another solution would be to
14382 have separate 'warning emitted' flag. Since this
14383 happens only when a binary has changed, I don't know
14384 which approach is better. */
14385 b->enable_state = bp_disabled;
14386 throw_exception (e);
14387 }
14388 }
14389
58438ac1 14390 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14391 {
f8eba3c6 14392 int i;
ef23e705 14393
f8eba3c6
TT
14394 for (i = 0; i < sals.nelts; ++i)
14395 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14396 if (b->condition_not_parsed && s && s[0])
14397 {
ed1d1739
KS
14398 char *cond_string, *extra_string;
14399 int thread, task;
ef23e705
TJB
14400
14401 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14402 &cond_string, &thread, &task,
14403 &extra_string);
ef23e705
TJB
14404 if (cond_string)
14405 b->cond_string = cond_string;
14406 b->thread = thread;
14407 b->task = task;
e7e0cddf
SS
14408 if (extra_string)
14409 b->extra_string = extra_string;
ef23e705
TJB
14410 b->condition_not_parsed = 0;
14411 }
14412
983af33b 14413 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14414 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14415
58438ac1
TT
14416 *found = 1;
14417 }
14418 else
14419 *found = 0;
ef23e705
TJB
14420
14421 return sals;
14422}
14423
348d480f
PA
14424/* The default re_set method, for typical hardware or software
14425 breakpoints. Reevaluate the breakpoint and recreate its
14426 locations. */
14427
14428static void
28010a5d 14429breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14430{
14431 int found;
f1310107 14432 struct symtabs_and_lines sals, sals_end;
ef23e705 14433 struct symtabs_and_lines expanded = {0};
f1310107 14434 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14435
14436 sals = addr_string_to_sals (b, b->addr_string, &found);
14437 if (found)
14438 {
14439 make_cleanup (xfree, sals.sals);
f8eba3c6 14440 expanded = sals;
ef23e705
TJB
14441 }
14442
f1310107
TJB
14443 if (b->addr_string_range_end)
14444 {
14445 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14446 if (found)
14447 {
14448 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14449 expanded_end = sals_end;
f1310107
TJB
14450 }
14451 }
14452
14453 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14454}
14455
983af33b
SDJ
14456/* Default method for creating SALs from an address string. It basically
14457 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14458
14459static void
14460create_sals_from_address_default (char **arg,
14461 struct linespec_result *canonical,
14462 enum bptype type_wanted,
14463 char *addr_start, char **copy_arg)
14464{
14465 parse_breakpoint_sals (arg, canonical);
14466}
14467
14468/* Call create_breakpoints_sal for the given arguments. This is the default
14469 function for the `create_breakpoints_sal' method of
14470 breakpoint_ops. */
14471
14472static void
14473create_breakpoints_sal_default (struct gdbarch *gdbarch,
14474 struct linespec_result *canonical,
983af33b 14475 char *cond_string,
e7e0cddf 14476 char *extra_string,
983af33b
SDJ
14477 enum bptype type_wanted,
14478 enum bpdisp disposition,
14479 int thread,
14480 int task, int ignore_count,
14481 const struct breakpoint_ops *ops,
14482 int from_tty, int enabled,
44f238bb 14483 int internal, unsigned flags)
983af33b
SDJ
14484{
14485 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14486 extra_string,
983af33b
SDJ
14487 type_wanted, disposition,
14488 thread, task, ignore_count, ops, from_tty,
44f238bb 14489 enabled, internal, flags);
983af33b
SDJ
14490}
14491
14492/* Decode the line represented by S by calling decode_line_full. This is the
14493 default function for the `decode_linespec' method of breakpoint_ops. */
14494
14495static void
14496decode_linespec_default (struct breakpoint *b, char **s,
14497 struct symtabs_and_lines *sals)
14498{
14499 struct linespec_result canonical;
14500
14501 init_linespec_result (&canonical);
14502 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14503 (struct symtab *) NULL, 0,
14504 &canonical, multiple_symbols_all,
14505 b->filter);
14506
14507 /* We should get 0 or 1 resulting SALs. */
14508 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14509
14510 if (VEC_length (linespec_sals, canonical.sals) > 0)
14511 {
14512 struct linespec_sals *lsal;
14513
14514 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14515 *sals = lsal->sals;
14516 /* Arrange it so the destructor does not free the
14517 contents. */
14518 lsal->sals.sals = NULL;
14519 }
14520
14521 destroy_linespec_result (&canonical);
14522}
14523
28010a5d
PA
14524/* Prepare the global context for a re-set of breakpoint B. */
14525
14526static struct cleanup *
14527prepare_re_set_context (struct breakpoint *b)
14528{
14529 struct cleanup *cleanups;
14530
14531 input_radix = b->input_radix;
14532 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14533 if (b->pspace != NULL)
14534 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14535 set_language (b->language);
14536
14537 return cleanups;
ef23e705
TJB
14538}
14539
c906108c
SS
14540/* Reset a breakpoint given it's struct breakpoint * BINT.
14541 The value we return ends up being the return value from catch_errors.
14542 Unused in this case. */
14543
14544static int
4efb68b1 14545breakpoint_re_set_one (void *bint)
c906108c 14546{
4a64f543 14547 /* Get past catch_errs. */
53a5351d 14548 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14549 struct cleanup *cleanups;
c906108c 14550
348d480f
PA
14551 cleanups = prepare_re_set_context (b);
14552 b->ops->re_set (b);
14553 do_cleanups (cleanups);
c906108c
SS
14554 return 0;
14555}
14556
69de3c6a 14557/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14558void
69de3c6a 14559breakpoint_re_set (void)
c906108c 14560{
35df4500 14561 struct breakpoint *b, *b_tmp;
c906108c
SS
14562 enum language save_language;
14563 int save_input_radix;
6c95b8df 14564 struct cleanup *old_chain;
c5aa993b 14565
c906108c
SS
14566 save_language = current_language->la_language;
14567 save_input_radix = input_radix;
6c95b8df
PA
14568 old_chain = save_current_program_space ();
14569
35df4500 14570 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14571 {
4a64f543 14572 /* Format possible error msg. */
fe3f5fa8 14573 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14574 b->number);
14575 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14576 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14577 do_cleanups (cleanups);
c5aa993b 14578 }
c906108c
SS
14579 set_language (save_language);
14580 input_radix = save_input_radix;
e62c965a 14581
0756c555 14582 jit_breakpoint_re_set ();
4efc6507 14583
6c95b8df
PA
14584 do_cleanups (old_chain);
14585
af02033e
PP
14586 create_overlay_event_breakpoint ();
14587 create_longjmp_master_breakpoint ();
14588 create_std_terminate_master_breakpoint ();
186c406b 14589 create_exception_master_breakpoint ();
c906108c
SS
14590}
14591\f
c906108c
SS
14592/* Reset the thread number of this breakpoint:
14593
14594 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14595 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14596void
fba45db2 14597breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14598{
14599 if (b->thread != -1)
14600 {
39f77062
KB
14601 if (in_thread_list (inferior_ptid))
14602 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14603
14604 /* We're being called after following a fork. The new fork is
14605 selected as current, and unless this was a vfork will have a
14606 different program space from the original thread. Reset that
14607 as well. */
14608 b->loc->pspace = current_program_space;
c906108c
SS
14609 }
14610}
14611
03ac34d5
MS
14612/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14613 If from_tty is nonzero, it prints a message to that effect,
14614 which ends with a period (no newline). */
14615
c906108c 14616void
fba45db2 14617set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14618{
52f0bd74 14619 struct breakpoint *b;
c906108c
SS
14620
14621 if (count < 0)
14622 count = 0;
14623
14624 ALL_BREAKPOINTS (b)
14625 if (b->number == bptnum)
c5aa993b 14626 {
d77f58be
SS
14627 if (is_tracepoint (b))
14628 {
14629 if (from_tty && count != 0)
14630 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14631 bptnum);
14632 return;
14633 }
14634
c5aa993b 14635 b->ignore_count = count;
221ea385
KS
14636 if (from_tty)
14637 {
14638 if (count == 0)
3e43a32a
MS
14639 printf_filtered (_("Will stop next time "
14640 "breakpoint %d is reached."),
221ea385
KS
14641 bptnum);
14642 else if (count == 1)
a3f17187 14643 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14644 bptnum);
14645 else
3e43a32a
MS
14646 printf_filtered (_("Will ignore next %d "
14647 "crossings of breakpoint %d."),
221ea385
KS
14648 count, bptnum);
14649 }
8d3788bd 14650 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14651 return;
14652 }
c906108c 14653
8a3fe4f8 14654 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14655}
14656
c906108c
SS
14657/* Command to set ignore-count of breakpoint N to COUNT. */
14658
14659static void
fba45db2 14660ignore_command (char *args, int from_tty)
c906108c
SS
14661{
14662 char *p = args;
52f0bd74 14663 int num;
c906108c
SS
14664
14665 if (p == 0)
e2e0b3e5 14666 error_no_arg (_("a breakpoint number"));
c5aa993b 14667
c906108c 14668 num = get_number (&p);
5c44784c 14669 if (num == 0)
8a3fe4f8 14670 error (_("bad breakpoint number: '%s'"), args);
c906108c 14671 if (*p == 0)
8a3fe4f8 14672 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14673
14674 set_ignore_count (num,
14675 longest_to_int (value_as_long (parse_and_eval (p))),
14676 from_tty);
221ea385
KS
14677 if (from_tty)
14678 printf_filtered ("\n");
c906108c
SS
14679}
14680\f
14681/* Call FUNCTION on each of the breakpoints
14682 whose numbers are given in ARGS. */
14683
14684static void
95a42b64
TT
14685map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14686 void *),
14687 void *data)
c906108c 14688{
52f0bd74
AC
14689 int num;
14690 struct breakpoint *b, *tmp;
11cf8741 14691 int match;
197f0a60 14692 struct get_number_or_range_state state;
c906108c 14693
197f0a60 14694 if (args == 0)
e2e0b3e5 14695 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14696
197f0a60
TT
14697 init_number_or_range (&state, args);
14698
14699 while (!state.finished)
c906108c 14700 {
197f0a60
TT
14701 char *p = state.string;
14702
11cf8741 14703 match = 0;
c5aa993b 14704
197f0a60 14705 num = get_number_or_range (&state);
5c44784c 14706 if (num == 0)
c5aa993b 14707 {
8a3fe4f8 14708 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14709 }
14710 else
14711 {
14712 ALL_BREAKPOINTS_SAFE (b, tmp)
14713 if (b->number == num)
14714 {
11cf8741 14715 match = 1;
cdac0397 14716 function (b, data);
11cf8741 14717 break;
5c44784c 14718 }
11cf8741 14719 if (match == 0)
a3f17187 14720 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14721 }
c906108c
SS
14722 }
14723}
14724
0d381245
VP
14725static struct bp_location *
14726find_location_by_number (char *number)
14727{
14728 char *dot = strchr (number, '.');
14729 char *p1;
14730 int bp_num;
14731 int loc_num;
14732 struct breakpoint *b;
14733 struct bp_location *loc;
14734
14735 *dot = '\0';
14736
14737 p1 = number;
197f0a60 14738 bp_num = get_number (&p1);
0d381245
VP
14739 if (bp_num == 0)
14740 error (_("Bad breakpoint number '%s'"), number);
14741
14742 ALL_BREAKPOINTS (b)
14743 if (b->number == bp_num)
14744 {
14745 break;
14746 }
14747
14748 if (!b || b->number != bp_num)
14749 error (_("Bad breakpoint number '%s'"), number);
14750
14751 p1 = dot+1;
197f0a60 14752 loc_num = get_number (&p1);
0d381245
VP
14753 if (loc_num == 0)
14754 error (_("Bad breakpoint location number '%s'"), number);
14755
14756 --loc_num;
14757 loc = b->loc;
14758 for (;loc_num && loc; --loc_num, loc = loc->next)
14759 ;
14760 if (!loc)
14761 error (_("Bad breakpoint location number '%s'"), dot+1);
14762
14763 return loc;
14764}
14765
14766
1900040c
MS
14767/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14768 If from_tty is nonzero, it prints a message to that effect,
14769 which ends with a period (no newline). */
14770
c906108c 14771void
fba45db2 14772disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14773{
14774 /* Never disable a watchpoint scope breakpoint; we want to
14775 hit them when we leave scope so we can delete both the
14776 watchpoint and its scope breakpoint at that time. */
14777 if (bpt->type == bp_watchpoint_scope)
14778 return;
14779
c2c6d25f 14780 /* You can't disable permanent breakpoints. */
b5de0fa7 14781 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14782 return;
14783
b5de0fa7 14784 bpt->enable_state = bp_disabled;
c906108c 14785
b775012e
LM
14786 /* Mark breakpoint locations modified. */
14787 mark_breakpoint_modified (bpt);
14788
d248b706
KY
14789 if (target_supports_enable_disable_tracepoint ()
14790 && current_trace_status ()->running && is_tracepoint (bpt))
14791 {
14792 struct bp_location *location;
14793
14794 for (location = bpt->loc; location; location = location->next)
14795 target_disable_tracepoint (location);
14796 }
14797
b60e7edf 14798 update_global_location_list (0);
c906108c 14799
8d3788bd 14800 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14801}
14802
51be5b68
PA
14803/* A callback for iterate_over_related_breakpoints. */
14804
14805static void
14806do_disable_breakpoint (struct breakpoint *b, void *ignore)
14807{
14808 disable_breakpoint (b);
14809}
14810
95a42b64
TT
14811/* A callback for map_breakpoint_numbers that calls
14812 disable_breakpoint. */
14813
14814static void
14815do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14816{
51be5b68 14817 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14818}
14819
c906108c 14820static void
fba45db2 14821disable_command (char *args, int from_tty)
c906108c 14822{
c906108c 14823 if (args == 0)
46c6471b
PA
14824 {
14825 struct breakpoint *bpt;
14826
14827 ALL_BREAKPOINTS (bpt)
14828 if (user_breakpoint_p (bpt))
14829 disable_breakpoint (bpt);
14830 }
9eaabc75 14831 else
0d381245 14832 {
9eaabc75
MW
14833 char *num = extract_arg (&args);
14834
14835 while (num)
d248b706 14836 {
9eaabc75 14837 if (strchr (num, '.'))
b775012e 14838 {
9eaabc75
MW
14839 struct bp_location *loc = find_location_by_number (num);
14840
14841 if (loc)
14842 {
14843 if (loc->enabled)
14844 {
14845 loc->enabled = 0;
14846 mark_breakpoint_location_modified (loc);
14847 }
14848 if (target_supports_enable_disable_tracepoint ()
14849 && current_trace_status ()->running && loc->owner
14850 && is_tracepoint (loc->owner))
14851 target_disable_tracepoint (loc);
14852 }
14853 update_global_location_list (0);
b775012e 14854 }
9eaabc75
MW
14855 else
14856 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14857 num = extract_arg (&args);
d248b706 14858 }
0d381245 14859 }
c906108c
SS
14860}
14861
14862static void
816338b5
SS
14863enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14864 int count)
c906108c 14865{
afe38095 14866 int target_resources_ok;
c906108c
SS
14867
14868 if (bpt->type == bp_hardware_breakpoint)
14869 {
14870 int i;
c5aa993b 14871 i = hw_breakpoint_used_count ();
53a5351d 14872 target_resources_ok =
d92524f1 14873 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14874 i + 1, 0);
c906108c 14875 if (target_resources_ok == 0)
8a3fe4f8 14876 error (_("No hardware breakpoint support in the target."));
c906108c 14877 else if (target_resources_ok < 0)
8a3fe4f8 14878 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14879 }
14880
cc60f2e3 14881 if (is_watchpoint (bpt))
c906108c 14882 {
d07205c2
JK
14883 /* Initialize it just to avoid a GCC false warning. */
14884 enum enable_state orig_enable_state = 0;
bfd189b1 14885 volatile struct gdb_exception e;
dde02812
ES
14886
14887 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14888 {
3a5c3e22
PA
14889 struct watchpoint *w = (struct watchpoint *) bpt;
14890
1e718ff1
TJB
14891 orig_enable_state = bpt->enable_state;
14892 bpt->enable_state = bp_enabled;
3a5c3e22 14893 update_watchpoint (w, 1 /* reparse */);
c906108c 14894 }
dde02812 14895 if (e.reason < 0)
c5aa993b 14896 {
1e718ff1 14897 bpt->enable_state = orig_enable_state;
dde02812
ES
14898 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14899 bpt->number);
14900 return;
c5aa993b 14901 }
c906108c 14902 }
0101ce28 14903
b4c291bb
KH
14904 if (bpt->enable_state != bp_permanent)
14905 bpt->enable_state = bp_enabled;
d248b706 14906
b775012e
LM
14907 bpt->enable_state = bp_enabled;
14908
14909 /* Mark breakpoint locations modified. */
14910 mark_breakpoint_modified (bpt);
14911
d248b706
KY
14912 if (target_supports_enable_disable_tracepoint ()
14913 && current_trace_status ()->running && is_tracepoint (bpt))
14914 {
14915 struct bp_location *location;
14916
14917 for (location = bpt->loc; location; location = location->next)
14918 target_enable_tracepoint (location);
14919 }
14920
b4c291bb 14921 bpt->disposition = disposition;
816338b5 14922 bpt->enable_count = count;
b60e7edf 14923 update_global_location_list (1);
9c97429f 14924
8d3788bd 14925 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14926}
14927
fe3f5fa8 14928
c906108c 14929void
fba45db2 14930enable_breakpoint (struct breakpoint *bpt)
c906108c 14931{
816338b5 14932 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14933}
14934
14935static void
14936do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14937{
14938 enable_breakpoint (bpt);
c906108c
SS
14939}
14940
95a42b64
TT
14941/* A callback for map_breakpoint_numbers that calls
14942 enable_breakpoint. */
14943
14944static void
14945do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14946{
51be5b68 14947 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14948}
14949
c906108c
SS
14950/* The enable command enables the specified breakpoints (or all defined
14951 breakpoints) so they once again become (or continue to be) effective
1272ad14 14952 in stopping the inferior. */
c906108c 14953
c906108c 14954static void
fba45db2 14955enable_command (char *args, int from_tty)
c906108c 14956{
c906108c 14957 if (args == 0)
46c6471b
PA
14958 {
14959 struct breakpoint *bpt;
14960
14961 ALL_BREAKPOINTS (bpt)
14962 if (user_breakpoint_p (bpt))
14963 enable_breakpoint (bpt);
14964 }
9eaabc75 14965 else
0d381245 14966 {
9eaabc75
MW
14967 char *num = extract_arg (&args);
14968
14969 while (num)
d248b706 14970 {
9eaabc75 14971 if (strchr (num, '.'))
b775012e 14972 {
9eaabc75
MW
14973 struct bp_location *loc = find_location_by_number (num);
14974
14975 if (loc)
14976 {
14977 if (!loc->enabled)
14978 {
14979 loc->enabled = 1;
14980 mark_breakpoint_location_modified (loc);
14981 }
14982 if (target_supports_enable_disable_tracepoint ()
14983 && current_trace_status ()->running && loc->owner
14984 && is_tracepoint (loc->owner))
14985 target_enable_tracepoint (loc);
14986 }
14987 update_global_location_list (1);
b775012e 14988 }
9eaabc75
MW
14989 else
14990 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14991 num = extract_arg (&args);
d248b706 14992 }
0d381245 14993 }
c906108c
SS
14994}
14995
816338b5
SS
14996/* This struct packages up disposition data for application to multiple
14997 breakpoints. */
14998
14999struct disp_data
15000{
15001 enum bpdisp disp;
15002 int count;
15003};
15004
c906108c 15005static void
51be5b68
PA
15006do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15007{
816338b5 15008 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15009
816338b5 15010 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15011}
15012
15013static void
15014do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15015{
816338b5 15016 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15017
15018 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15019}
15020
c906108c 15021static void
fba45db2 15022enable_once_command (char *args, int from_tty)
c906108c 15023{
51be5b68 15024 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15025}
15026
816338b5
SS
15027static void
15028do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15029{
15030 struct disp_data disp = { disp_disable, *(int *) countptr };
15031
15032 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15033}
15034
15035static void
15036enable_count_command (char *args, int from_tty)
15037{
15038 int count = get_number (&args);
15039
15040 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15041}
15042
c906108c 15043static void
51be5b68 15044do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15045{
816338b5 15046 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15047
15048 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15049}
15050
c906108c 15051static void
fba45db2 15052enable_delete_command (char *args, int from_tty)
c906108c 15053{
51be5b68 15054 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15055}
15056\f
fa8d40ab
JJ
15057static void
15058set_breakpoint_cmd (char *args, int from_tty)
15059{
15060}
15061
15062static void
15063show_breakpoint_cmd (char *args, int from_tty)
15064{
15065}
15066
1f3b5d1b
PP
15067/* Invalidate last known value of any hardware watchpoint if
15068 the memory which that value represents has been written to by
15069 GDB itself. */
15070
15071static void
8de0566d
YQ
15072invalidate_bp_value_on_memory_change (struct inferior *inferior,
15073 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15074 const bfd_byte *data)
15075{
15076 struct breakpoint *bp;
15077
15078 ALL_BREAKPOINTS (bp)
15079 if (bp->enable_state == bp_enabled
3a5c3e22 15080 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15081 {
3a5c3e22 15082 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15083
3a5c3e22
PA
15084 if (wp->val_valid && wp->val)
15085 {
15086 struct bp_location *loc;
15087
15088 for (loc = bp->loc; loc != NULL; loc = loc->next)
15089 if (loc->loc_type == bp_loc_hardware_watchpoint
15090 && loc->address + loc->length > addr
15091 && addr + len > loc->address)
15092 {
15093 value_free (wp->val);
15094 wp->val = NULL;
15095 wp->val_valid = 0;
15096 }
15097 }
1f3b5d1b
PP
15098 }
15099}
15100
8181d85f
DJ
15101/* Create and insert a raw software breakpoint at PC. Return an
15102 identifier, which should be used to remove the breakpoint later.
15103 In general, places which call this should be using something on the
15104 breakpoint chain instead; this function should be eliminated
15105 someday. */
15106
15107void *
6c95b8df
PA
15108deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15109 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15110{
15111 struct bp_target_info *bp_tgt;
15112
41bf6aca 15113 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15114
6c95b8df 15115 bp_tgt->placed_address_space = aspace;
8181d85f 15116 bp_tgt->placed_address = pc;
6c95b8df 15117
a6d9a66e 15118 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15119 {
15120 /* Could not insert the breakpoint. */
15121 xfree (bp_tgt);
15122 return NULL;
15123 }
15124
15125 return bp_tgt;
15126}
15127
4a64f543
MS
15128/* Remove a breakpoint BP inserted by
15129 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15130
15131int
a6d9a66e 15132deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15133{
15134 struct bp_target_info *bp_tgt = bp;
15135 int ret;
15136
a6d9a66e 15137 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
15138 xfree (bp_tgt);
15139
15140 return ret;
15141}
15142
8181d85f
DJ
15143/* Create and insert a breakpoint for software single step. */
15144
15145void
6c95b8df 15146insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15147 struct address_space *aspace,
15148 CORE_ADDR next_pc)
8181d85f
DJ
15149{
15150 void **bpt_p;
15151
15152 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15153 {
15154 bpt_p = &single_step_breakpoints[0];
15155 single_step_gdbarch[0] = gdbarch;
15156 }
8181d85f
DJ
15157 else
15158 {
15159 gdb_assert (single_step_breakpoints[1] == NULL);
15160 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15161 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15162 }
15163
4a64f543
MS
15164 /* NOTE drow/2006-04-11: A future improvement to this function would
15165 be to only create the breakpoints once, and actually put them on
15166 the breakpoint chain. That would let us use set_raw_breakpoint.
15167 We could adjust the addresses each time they were needed. Doing
15168 this requires corresponding changes elsewhere where single step
15169 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15170
6c95b8df 15171 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15172 if (*bpt_p == NULL)
5af949e3
UW
15173 error (_("Could not insert single-step breakpoint at %s"),
15174 paddress (gdbarch, next_pc));
8181d85f
DJ
15175}
15176
f02253f1
HZ
15177/* Check if the breakpoints used for software single stepping
15178 were inserted or not. */
15179
15180int
15181single_step_breakpoints_inserted (void)
15182{
15183 return (single_step_breakpoints[0] != NULL
15184 || single_step_breakpoints[1] != NULL);
15185}
15186
8181d85f
DJ
15187/* Remove and delete any breakpoints used for software single step. */
15188
15189void
15190remove_single_step_breakpoints (void)
15191{
15192 gdb_assert (single_step_breakpoints[0] != NULL);
15193
15194 /* See insert_single_step_breakpoint for more about this deprecated
15195 call. */
a6d9a66e
UW
15196 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15197 single_step_breakpoints[0]);
15198 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15199 single_step_breakpoints[0] = NULL;
15200
15201 if (single_step_breakpoints[1] != NULL)
15202 {
a6d9a66e
UW
15203 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15204 single_step_breakpoints[1]);
15205 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15206 single_step_breakpoints[1] = NULL;
15207 }
15208}
15209
d03285ec
UW
15210/* Delete software single step breakpoints without removing them from
15211 the inferior. This is intended to be used if the inferior's address
15212 space where they were inserted is already gone, e.g. after exit or
15213 exec. */
15214
15215void
15216cancel_single_step_breakpoints (void)
15217{
15218 int i;
15219
15220 for (i = 0; i < 2; i++)
15221 if (single_step_breakpoints[i])
15222 {
15223 xfree (single_step_breakpoints[i]);
15224 single_step_breakpoints[i] = NULL;
15225 single_step_gdbarch[i] = NULL;
15226 }
15227}
15228
15229/* Detach software single-step breakpoints from INFERIOR_PTID without
15230 removing them. */
15231
15232static void
15233detach_single_step_breakpoints (void)
15234{
15235 int i;
15236
15237 for (i = 0; i < 2; i++)
15238 if (single_step_breakpoints[i])
15239 target_remove_breakpoint (single_step_gdbarch[i],
15240 single_step_breakpoints[i]);
15241}
15242
4a64f543
MS
15243/* Check whether a software single-step breakpoint is inserted at
15244 PC. */
1aafd4da 15245
2adfaa28 15246int
cc59ec59
MS
15247single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15248 CORE_ADDR pc)
1aafd4da
UW
15249{
15250 int i;
15251
15252 for (i = 0; i < 2; i++)
15253 {
15254 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15255 if (bp_tgt
15256 && breakpoint_address_match (bp_tgt->placed_address_space,
15257 bp_tgt->placed_address,
15258 aspace, pc))
1aafd4da
UW
15259 return 1;
15260 }
15261
15262 return 0;
15263}
15264
a96d9b2e
SDJ
15265/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15266 non-zero otherwise. */
15267static int
15268is_syscall_catchpoint_enabled (struct breakpoint *bp)
15269{
15270 if (syscall_catchpoint_p (bp)
15271 && bp->enable_state != bp_disabled
15272 && bp->enable_state != bp_call_disabled)
15273 return 1;
15274 else
15275 return 0;
15276}
15277
15278int
15279catch_syscall_enabled (void)
15280{
fa3064dd
YQ
15281 struct catch_syscall_inferior_data *inf_data
15282 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15283
fa3064dd 15284 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15285}
15286
15287int
15288catching_syscall_number (int syscall_number)
15289{
15290 struct breakpoint *bp;
15291
15292 ALL_BREAKPOINTS (bp)
15293 if (is_syscall_catchpoint_enabled (bp))
15294 {
be5c67c1
PA
15295 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15296
15297 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15298 {
15299 int i, iter;
15300 for (i = 0;
be5c67c1 15301 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15302 i++)
15303 if (syscall_number == iter)
15304 return 1;
15305 }
15306 else
15307 return 1;
15308 }
15309
15310 return 0;
15311}
15312
15313/* Complete syscall names. Used by "catch syscall". */
49c4e619 15314static VEC (char_ptr) *
a96d9b2e 15315catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15316 const char *text, const char *word)
a96d9b2e
SDJ
15317{
15318 const char **list = get_syscall_names ();
49c4e619 15319 VEC (char_ptr) *retlist
b45627a0 15320 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15321
c38eea1a
MS
15322 xfree (list);
15323 return retlist;
a96d9b2e
SDJ
15324}
15325
1042e4c0
SS
15326/* Tracepoint-specific operations. */
15327
15328/* Set tracepoint count to NUM. */
15329static void
15330set_tracepoint_count (int num)
15331{
15332 tracepoint_count = num;
4fa62494 15333 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15334}
15335
70221824 15336static void
1042e4c0
SS
15337trace_command (char *arg, int from_tty)
15338{
55aa24fb
SDJ
15339 struct breakpoint_ops *ops;
15340 const char *arg_cp = arg;
15341
15342 if (arg && probe_linespec_to_ops (&arg_cp))
15343 ops = &tracepoint_probe_breakpoint_ops;
15344 else
15345 ops = &tracepoint_breakpoint_ops;
15346
558a9d82
YQ
15347 create_breakpoint (get_current_arch (),
15348 arg,
15349 NULL, 0, NULL, 1 /* parse arg */,
15350 0 /* tempflag */,
15351 bp_tracepoint /* type_wanted */,
15352 0 /* Ignore count */,
15353 pending_break_support,
15354 ops,
15355 from_tty,
15356 1 /* enabled */,
15357 0 /* internal */, 0);
1042e4c0
SS
15358}
15359
70221824 15360static void
7a697b8d
SS
15361ftrace_command (char *arg, int from_tty)
15362{
558a9d82
YQ
15363 create_breakpoint (get_current_arch (),
15364 arg,
15365 NULL, 0, NULL, 1 /* parse arg */,
15366 0 /* tempflag */,
15367 bp_fast_tracepoint /* type_wanted */,
15368 0 /* Ignore count */,
15369 pending_break_support,
15370 &tracepoint_breakpoint_ops,
15371 from_tty,
15372 1 /* enabled */,
15373 0 /* internal */, 0);
0fb4aa4b
PA
15374}
15375
15376/* strace command implementation. Creates a static tracepoint. */
15377
70221824 15378static void
0fb4aa4b
PA
15379strace_command (char *arg, int from_tty)
15380{
983af33b
SDJ
15381 struct breakpoint_ops *ops;
15382
15383 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15384 or with a normal static tracepoint. */
15385 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15386 ops = &strace_marker_breakpoint_ops;
15387 else
15388 ops = &tracepoint_breakpoint_ops;
15389
558a9d82
YQ
15390 create_breakpoint (get_current_arch (),
15391 arg,
15392 NULL, 0, NULL, 1 /* parse arg */,
15393 0 /* tempflag */,
15394 bp_static_tracepoint /* type_wanted */,
15395 0 /* Ignore count */,
15396 pending_break_support,
15397 ops,
15398 from_tty,
15399 1 /* enabled */,
15400 0 /* internal */, 0);
7a697b8d
SS
15401}
15402
409873ef
SS
15403/* Set up a fake reader function that gets command lines from a linked
15404 list that was acquired during tracepoint uploading. */
15405
15406static struct uploaded_tp *this_utp;
3149d8c1 15407static int next_cmd;
409873ef
SS
15408
15409static char *
15410read_uploaded_action (void)
15411{
15412 char *rslt;
15413
3149d8c1 15414 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15415
3149d8c1 15416 next_cmd++;
409873ef
SS
15417
15418 return rslt;
15419}
15420
00bf0b85
SS
15421/* Given information about a tracepoint as recorded on a target (which
15422 can be either a live system or a trace file), attempt to create an
15423 equivalent GDB tracepoint. This is not a reliable process, since
15424 the target does not necessarily have all the information used when
15425 the tracepoint was originally defined. */
15426
d9b3f62e 15427struct tracepoint *
00bf0b85 15428create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15429{
409873ef 15430 char *addr_str, small_buf[100];
d9b3f62e 15431 struct tracepoint *tp;
fd9b8c24 15432
409873ef
SS
15433 if (utp->at_string)
15434 addr_str = utp->at_string;
15435 else
15436 {
15437 /* In the absence of a source location, fall back to raw
15438 address. Since there is no way to confirm that the address
15439 means the same thing as when the trace was started, warn the
15440 user. */
3e43a32a
MS
15441 warning (_("Uploaded tracepoint %d has no "
15442 "source location, using raw address"),
409873ef 15443 utp->number);
8c042590 15444 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15445 addr_str = small_buf;
15446 }
15447
15448 /* There's not much we can do with a sequence of bytecodes. */
15449 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15450 warning (_("Uploaded tracepoint %d condition "
15451 "has no source form, ignoring it"),
409873ef 15452 utp->number);
d5551862 15453
8cdf0e15 15454 if (!create_breakpoint (get_current_arch (),
409873ef 15455 addr_str,
e7e0cddf
SS
15456 utp->cond_string, -1, NULL,
15457 0 /* parse cond/thread */,
8cdf0e15 15458 0 /* tempflag */,
0fb4aa4b 15459 utp->type /* type_wanted */,
8cdf0e15
VP
15460 0 /* Ignore count */,
15461 pending_break_support,
348d480f 15462 &tracepoint_breakpoint_ops,
8cdf0e15 15463 0 /* from_tty */,
84f4c1fe 15464 utp->enabled /* enabled */,
44f238bb
PA
15465 0 /* internal */,
15466 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15467 return NULL;
15468
409873ef 15469 /* Get the tracepoint we just created. */
fd9b8c24
PA
15470 tp = get_tracepoint (tracepoint_count);
15471 gdb_assert (tp != NULL);
d5551862 15472
00bf0b85
SS
15473 if (utp->pass > 0)
15474 {
8c042590
PM
15475 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15476 tp->base.number);
00bf0b85 15477
409873ef 15478 trace_pass_command (small_buf, 0);
00bf0b85
SS
15479 }
15480
409873ef
SS
15481 /* If we have uploaded versions of the original commands, set up a
15482 special-purpose "reader" function and call the usual command line
15483 reader, then pass the result to the breakpoint command-setting
15484 function. */
3149d8c1 15485 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15486 {
409873ef 15487 struct command_line *cmd_list;
00bf0b85 15488
409873ef 15489 this_utp = utp;
3149d8c1 15490 next_cmd = 0;
d5551862 15491
409873ef
SS
15492 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15493
d9b3f62e 15494 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15495 }
3149d8c1
SS
15496 else if (!VEC_empty (char_ptr, utp->actions)
15497 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15498 warning (_("Uploaded tracepoint %d actions "
15499 "have no source form, ignoring them"),
409873ef 15500 utp->number);
00bf0b85 15501
f196051f
SS
15502 /* Copy any status information that might be available. */
15503 tp->base.hit_count = utp->hit_count;
15504 tp->traceframe_usage = utp->traceframe_usage;
15505
00bf0b85 15506 return tp;
d9b3f62e 15507}
00bf0b85 15508
1042e4c0
SS
15509/* Print information on tracepoint number TPNUM_EXP, or all if
15510 omitted. */
15511
15512static void
e5a67952 15513tracepoints_info (char *args, int from_tty)
1042e4c0 15514{
79a45e25 15515 struct ui_out *uiout = current_uiout;
e5a67952 15516 int num_printed;
1042e4c0 15517
e5a67952 15518 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15519
15520 if (num_printed == 0)
1042e4c0 15521 {
e5a67952 15522 if (args == NULL || *args == '\0')
d77f58be
SS
15523 ui_out_message (uiout, 0, "No tracepoints.\n");
15524 else
e5a67952 15525 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15526 }
ad443146
SS
15527
15528 default_collect_info ();
1042e4c0
SS
15529}
15530
4a64f543 15531/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15532 Not supported by all targets. */
15533static void
15534enable_trace_command (char *args, int from_tty)
15535{
15536 enable_command (args, from_tty);
15537}
15538
4a64f543 15539/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15540 Not supported by all targets. */
15541static void
15542disable_trace_command (char *args, int from_tty)
15543{
15544 disable_command (args, from_tty);
15545}
15546
4a64f543 15547/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15548static void
15549delete_trace_command (char *arg, int from_tty)
15550{
35df4500 15551 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15552
15553 dont_repeat ();
15554
15555 if (arg == 0)
15556 {
15557 int breaks_to_delete = 0;
15558
15559 /* Delete all breakpoints if no argument.
15560 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15561 have to be deleted with an explicit breakpoint number
15562 argument. */
1042e4c0 15563 ALL_TRACEPOINTS (b)
46c6471b 15564 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15565 {
15566 breaks_to_delete = 1;
15567 break;
15568 }
1042e4c0
SS
15569
15570 /* Ask user only if there are some breakpoints to delete. */
15571 if (!from_tty
15572 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15573 {
35df4500 15574 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15575 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15576 delete_breakpoint (b);
1042e4c0
SS
15577 }
15578 }
15579 else
51be5b68 15580 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15581}
15582
197f0a60
TT
15583/* Helper function for trace_pass_command. */
15584
15585static void
d9b3f62e 15586trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15587{
d9b3f62e 15588 tp->pass_count = count;
6f6484cd 15589 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15590 if (from_tty)
15591 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15592 tp->base.number, count);
197f0a60
TT
15593}
15594
1042e4c0
SS
15595/* Set passcount for tracepoint.
15596
15597 First command argument is passcount, second is tracepoint number.
15598 If tracepoint number omitted, apply to most recently defined.
15599 Also accepts special argument "all". */
15600
15601static void
15602trace_pass_command (char *args, int from_tty)
15603{
d9b3f62e 15604 struct tracepoint *t1;
1042e4c0 15605 unsigned int count;
1042e4c0
SS
15606
15607 if (args == 0 || *args == 0)
3e43a32a
MS
15608 error (_("passcount command requires an "
15609 "argument (count + optional TP num)"));
1042e4c0 15610
4a64f543 15611 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15612
529480d0 15613 args = skip_spaces (args);
1042e4c0
SS
15614 if (*args && strncasecmp (args, "all", 3) == 0)
15615 {
d9b3f62e
PA
15616 struct breakpoint *b;
15617
1042e4c0 15618 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15619 if (*args)
15620 error (_("Junk at end of arguments."));
1042e4c0 15621
d9b3f62e 15622 ALL_TRACEPOINTS (b)
197f0a60 15623 {
d9b3f62e 15624 t1 = (struct tracepoint *) b;
197f0a60
TT
15625 trace_pass_set_count (t1, count, from_tty);
15626 }
15627 }
15628 else if (*args == '\0')
1042e4c0 15629 {
5fa1d40e 15630 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15631 if (t1)
197f0a60
TT
15632 trace_pass_set_count (t1, count, from_tty);
15633 }
15634 else
15635 {
15636 struct get_number_or_range_state state;
15637
15638 init_number_or_range (&state, args);
15639 while (!state.finished)
1042e4c0 15640 {
5fa1d40e 15641 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15642 if (t1)
15643 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15644 }
15645 }
1042e4c0
SS
15646}
15647
d9b3f62e 15648struct tracepoint *
1042e4c0
SS
15649get_tracepoint (int num)
15650{
15651 struct breakpoint *t;
15652
15653 ALL_TRACEPOINTS (t)
15654 if (t->number == num)
d9b3f62e 15655 return (struct tracepoint *) t;
1042e4c0
SS
15656
15657 return NULL;
15658}
15659
d5551862
SS
15660/* Find the tracepoint with the given target-side number (which may be
15661 different from the tracepoint number after disconnecting and
15662 reconnecting). */
15663
d9b3f62e 15664struct tracepoint *
d5551862
SS
15665get_tracepoint_by_number_on_target (int num)
15666{
d9b3f62e 15667 struct breakpoint *b;
d5551862 15668
d9b3f62e
PA
15669 ALL_TRACEPOINTS (b)
15670 {
15671 struct tracepoint *t = (struct tracepoint *) b;
15672
15673 if (t->number_on_target == num)
15674 return t;
15675 }
d5551862
SS
15676
15677 return NULL;
15678}
15679
1042e4c0 15680/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15681 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15682 If the argument is missing, the most recent tracepoint
15683 (tracepoint_count) is returned. */
15684
d9b3f62e 15685struct tracepoint *
197f0a60 15686get_tracepoint_by_number (char **arg,
5fa1d40e 15687 struct get_number_or_range_state *state)
1042e4c0 15688{
1042e4c0
SS
15689 struct breakpoint *t;
15690 int tpnum;
15691 char *instring = arg == NULL ? NULL : *arg;
15692
197f0a60
TT
15693 if (state)
15694 {
15695 gdb_assert (!state->finished);
15696 tpnum = get_number_or_range (state);
15697 }
15698 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15699 tpnum = tracepoint_count;
1042e4c0 15700 else
197f0a60 15701 tpnum = get_number (arg);
1042e4c0
SS
15702
15703 if (tpnum <= 0)
15704 {
15705 if (instring && *instring)
15706 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15707 instring);
15708 else
5fa1d40e 15709 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15710 return NULL;
15711 }
15712
15713 ALL_TRACEPOINTS (t)
15714 if (t->number == tpnum)
15715 {
d9b3f62e 15716 return (struct tracepoint *) t;
1042e4c0
SS
15717 }
15718
1042e4c0
SS
15719 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15720 return NULL;
15721}
15722
d9b3f62e
PA
15723void
15724print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15725{
15726 if (b->thread != -1)
15727 fprintf_unfiltered (fp, " thread %d", b->thread);
15728
15729 if (b->task != 0)
15730 fprintf_unfiltered (fp, " task %d", b->task);
15731
15732 fprintf_unfiltered (fp, "\n");
15733}
15734
6149aea9
PA
15735/* Save information on user settable breakpoints (watchpoints, etc) to
15736 a new script file named FILENAME. If FILTER is non-NULL, call it
15737 on each breakpoint and only include the ones for which it returns
15738 non-zero. */
15739
1042e4c0 15740static void
6149aea9
PA
15741save_breakpoints (char *filename, int from_tty,
15742 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15743{
15744 struct breakpoint *tp;
6149aea9 15745 int any = 0;
1042e4c0 15746 struct cleanup *cleanup;
a7bdde9e 15747 struct ui_file *fp;
6149aea9 15748 int extra_trace_bits = 0;
1042e4c0 15749
6149aea9
PA
15750 if (filename == 0 || *filename == 0)
15751 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15752
15753 /* See if we have anything to save. */
6149aea9 15754 ALL_BREAKPOINTS (tp)
1042e4c0 15755 {
6149aea9 15756 /* Skip internal and momentary breakpoints. */
09d682a4 15757 if (!user_breakpoint_p (tp))
6149aea9
PA
15758 continue;
15759
15760 /* If we have a filter, only save the breakpoints it accepts. */
15761 if (filter && !filter (tp))
15762 continue;
15763
15764 any = 1;
15765
15766 if (is_tracepoint (tp))
15767 {
15768 extra_trace_bits = 1;
15769
15770 /* We can stop searching. */
15771 break;
15772 }
1042e4c0 15773 }
6149aea9
PA
15774
15775 if (!any)
1042e4c0 15776 {
6149aea9 15777 warning (_("Nothing to save."));
1042e4c0
SS
15778 return;
15779 }
15780
c718be47
PA
15781 filename = tilde_expand (filename);
15782 cleanup = make_cleanup (xfree, filename);
15783 fp = gdb_fopen (filename, "w");
059fb39f 15784 if (!fp)
6149aea9
PA
15785 error (_("Unable to open file '%s' for saving (%s)"),
15786 filename, safe_strerror (errno));
a7bdde9e 15787 make_cleanup_ui_file_delete (fp);
8bf6485c 15788
6149aea9
PA
15789 if (extra_trace_bits)
15790 save_trace_state_variables (fp);
8bf6485c 15791
6149aea9 15792 ALL_BREAKPOINTS (tp)
1042e4c0 15793 {
6149aea9 15794 /* Skip internal and momentary breakpoints. */
09d682a4 15795 if (!user_breakpoint_p (tp))
6149aea9 15796 continue;
8bf6485c 15797
6149aea9
PA
15798 /* If we have a filter, only save the breakpoints it accepts. */
15799 if (filter && !filter (tp))
15800 continue;
15801
348d480f 15802 tp->ops->print_recreate (tp, fp);
1042e4c0 15803
6149aea9
PA
15804 /* Note, we can't rely on tp->number for anything, as we can't
15805 assume the recreated breakpoint numbers will match. Use $bpnum
15806 instead. */
15807
15808 if (tp->cond_string)
15809 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15810
15811 if (tp->ignore_count)
15812 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15813
2d9442cc 15814 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15815 {
a7bdde9e
VP
15816 volatile struct gdb_exception ex;
15817
6149aea9 15818 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15819
79a45e25 15820 ui_out_redirect (current_uiout, fp);
14dba4b4 15821 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15822 {
79a45e25 15823 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15824 }
79a45e25 15825 ui_out_redirect (current_uiout, NULL);
1042e4c0 15826
a7bdde9e
VP
15827 if (ex.reason < 0)
15828 throw_exception (ex);
1042e4c0 15829
a7bdde9e 15830 fprintf_unfiltered (fp, " end\n");
1042e4c0 15831 }
6149aea9
PA
15832
15833 if (tp->enable_state == bp_disabled)
15834 fprintf_unfiltered (fp, "disable\n");
15835
15836 /* If this is a multi-location breakpoint, check if the locations
15837 should be individually disabled. Watchpoint locations are
15838 special, and not user visible. */
15839 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15840 {
15841 struct bp_location *loc;
15842 int n = 1;
15843
15844 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15845 if (!loc->enabled)
15846 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15847 }
1042e4c0 15848 }
8bf6485c 15849
6149aea9 15850 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15851 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15852
1042e4c0 15853 if (from_tty)
6149aea9 15854 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15855 do_cleanups (cleanup);
6149aea9
PA
15856}
15857
15858/* The `save breakpoints' command. */
15859
15860static void
15861save_breakpoints_command (char *args, int from_tty)
15862{
15863 save_breakpoints (args, from_tty, NULL);
15864}
15865
15866/* The `save tracepoints' command. */
15867
15868static void
15869save_tracepoints_command (char *args, int from_tty)
15870{
15871 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15872}
15873
15874/* Create a vector of all tracepoints. */
15875
15876VEC(breakpoint_p) *
eeae04df 15877all_tracepoints (void)
1042e4c0
SS
15878{
15879 VEC(breakpoint_p) *tp_vec = 0;
15880 struct breakpoint *tp;
15881
15882 ALL_TRACEPOINTS (tp)
15883 {
15884 VEC_safe_push (breakpoint_p, tp_vec, tp);
15885 }
15886
15887 return tp_vec;
15888}
15889
c906108c 15890\f
4a64f543
MS
15891/* This help string is used for the break, hbreak, tbreak and thbreak
15892 commands. It is defined as a macro to prevent duplication.
15893 COMMAND should be a string constant containing the name of the
15894 command. */
31e2b00f 15895#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15896command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15897PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15898probe point. Accepted values are `-probe' (for a generic, automatically\n\
15899guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15900LOCATION may be a line number, function name, or \"*\" and an address.\n\
15901If a line number is specified, break at start of code for that line.\n\
15902If a function is specified, break at start of code for that function.\n\
15903If an address is specified, break at that exact address.\n\
dc10affe
PA
15904With no LOCATION, uses current execution address of the selected\n\
15905stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15906\n\
15907THREADNUM is the number from \"info threads\".\n\
15908CONDITION is a boolean expression.\n\
15909\n\
d41c0fc8
PA
15910Multiple breakpoints at one place are permitted, and useful if their\n\
15911conditions are different.\n\
31e2b00f
AS
15912\n\
15913Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15914
44feb3ce
TT
15915/* List of subcommands for "catch". */
15916static struct cmd_list_element *catch_cmdlist;
15917
15918/* List of subcommands for "tcatch". */
15919static struct cmd_list_element *tcatch_cmdlist;
15920
9ac4176b 15921void
44feb3ce
TT
15922add_catch_command (char *name, char *docstring,
15923 void (*sfunc) (char *args, int from_tty,
15924 struct cmd_list_element *command),
625e8578 15925 completer_ftype *completer,
44feb3ce
TT
15926 void *user_data_catch,
15927 void *user_data_tcatch)
15928{
15929 struct cmd_list_element *command;
15930
15931 command = add_cmd (name, class_breakpoint, NULL, docstring,
15932 &catch_cmdlist);
15933 set_cmd_sfunc (command, sfunc);
15934 set_cmd_context (command, user_data_catch);
a96d9b2e 15935 set_cmd_completer (command, completer);
44feb3ce
TT
15936
15937 command = add_cmd (name, class_breakpoint, NULL, docstring,
15938 &tcatch_cmdlist);
15939 set_cmd_sfunc (command, sfunc);
15940 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15941 set_cmd_completer (command, completer);
44feb3ce
TT
15942}
15943
6c95b8df 15944static void
a79b8f6e 15945clear_syscall_counts (struct inferior *inf)
6c95b8df 15946{
fa3064dd
YQ
15947 struct catch_syscall_inferior_data *inf_data
15948 = get_catch_syscall_inferior_data (inf);
15949
15950 inf_data->total_syscalls_count = 0;
15951 inf_data->any_syscall_count = 0;
15952 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15953}
15954
6149aea9
PA
15955static void
15956save_command (char *arg, int from_tty)
15957{
3e43a32a
MS
15958 printf_unfiltered (_("\"save\" must be followed by "
15959 "the name of a save subcommand.\n"));
6149aea9
PA
15960 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15961}
15962
84f4c1fe
PM
15963struct breakpoint *
15964iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15965 void *data)
15966{
35df4500 15967 struct breakpoint *b, *b_tmp;
84f4c1fe 15968
35df4500 15969 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15970 {
15971 if ((*callback) (b, data))
15972 return b;
15973 }
15974
15975 return NULL;
15976}
15977
0574c78f
GB
15978/* Zero if any of the breakpoint's locations could be a location where
15979 functions have been inlined, nonzero otherwise. */
15980
15981static int
15982is_non_inline_function (struct breakpoint *b)
15983{
15984 /* The shared library event breakpoint is set on the address of a
15985 non-inline function. */
15986 if (b->type == bp_shlib_event)
15987 return 1;
15988
15989 return 0;
15990}
15991
15992/* Nonzero if the specified PC cannot be a location where functions
15993 have been inlined. */
15994
15995int
09ac7c10
TT
15996pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15997 const struct target_waitstatus *ws)
0574c78f
GB
15998{
15999 struct breakpoint *b;
16000 struct bp_location *bl;
16001
16002 ALL_BREAKPOINTS (b)
16003 {
16004 if (!is_non_inline_function (b))
16005 continue;
16006
16007 for (bl = b->loc; bl != NULL; bl = bl->next)
16008 {
16009 if (!bl->shlib_disabled
09ac7c10 16010 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16011 return 1;
16012 }
16013 }
16014
16015 return 0;
16016}
16017
2f202fde
JK
16018/* Remove any references to OBJFILE which is going to be freed. */
16019
16020void
16021breakpoint_free_objfile (struct objfile *objfile)
16022{
16023 struct bp_location **locp, *loc;
16024
16025 ALL_BP_LOCATIONS (loc, locp)
16026 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16027 loc->symtab = NULL;
16028}
16029
2060206e
PA
16030void
16031initialize_breakpoint_ops (void)
16032{
16033 static int initialized = 0;
16034
16035 struct breakpoint_ops *ops;
16036
16037 if (initialized)
16038 return;
16039 initialized = 1;
16040
16041 /* The breakpoint_ops structure to be inherit by all kinds of
16042 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16043 internal and momentary breakpoints, etc.). */
16044 ops = &bkpt_base_breakpoint_ops;
16045 *ops = base_breakpoint_ops;
16046 ops->re_set = bkpt_re_set;
16047 ops->insert_location = bkpt_insert_location;
16048 ops->remove_location = bkpt_remove_location;
16049 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16050 ops->create_sals_from_address = bkpt_create_sals_from_address;
16051 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16052 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16053
16054 /* The breakpoint_ops structure to be used in regular breakpoints. */
16055 ops = &bkpt_breakpoint_ops;
16056 *ops = bkpt_base_breakpoint_ops;
16057 ops->re_set = bkpt_re_set;
16058 ops->resources_needed = bkpt_resources_needed;
16059 ops->print_it = bkpt_print_it;
16060 ops->print_mention = bkpt_print_mention;
16061 ops->print_recreate = bkpt_print_recreate;
16062
16063 /* Ranged breakpoints. */
16064 ops = &ranged_breakpoint_ops;
16065 *ops = bkpt_breakpoint_ops;
16066 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16067 ops->resources_needed = resources_needed_ranged_breakpoint;
16068 ops->print_it = print_it_ranged_breakpoint;
16069 ops->print_one = print_one_ranged_breakpoint;
16070 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16071 ops->print_mention = print_mention_ranged_breakpoint;
16072 ops->print_recreate = print_recreate_ranged_breakpoint;
16073
16074 /* Internal breakpoints. */
16075 ops = &internal_breakpoint_ops;
16076 *ops = bkpt_base_breakpoint_ops;
16077 ops->re_set = internal_bkpt_re_set;
16078 ops->check_status = internal_bkpt_check_status;
16079 ops->print_it = internal_bkpt_print_it;
16080 ops->print_mention = internal_bkpt_print_mention;
16081
16082 /* Momentary breakpoints. */
16083 ops = &momentary_breakpoint_ops;
16084 *ops = bkpt_base_breakpoint_ops;
16085 ops->re_set = momentary_bkpt_re_set;
16086 ops->check_status = momentary_bkpt_check_status;
16087 ops->print_it = momentary_bkpt_print_it;
16088 ops->print_mention = momentary_bkpt_print_mention;
16089
e2e4d78b
JK
16090 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16091 ops = &longjmp_breakpoint_ops;
16092 *ops = momentary_breakpoint_ops;
16093 ops->dtor = longjmp_bkpt_dtor;
16094
55aa24fb
SDJ
16095 /* Probe breakpoints. */
16096 ops = &bkpt_probe_breakpoint_ops;
16097 *ops = bkpt_breakpoint_ops;
16098 ops->insert_location = bkpt_probe_insert_location;
16099 ops->remove_location = bkpt_probe_remove_location;
16100 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16101 ops->decode_linespec = bkpt_probe_decode_linespec;
16102
2060206e
PA
16103 /* Watchpoints. */
16104 ops = &watchpoint_breakpoint_ops;
16105 *ops = base_breakpoint_ops;
3a5c3e22 16106 ops->dtor = dtor_watchpoint;
2060206e
PA
16107 ops->re_set = re_set_watchpoint;
16108 ops->insert_location = insert_watchpoint;
16109 ops->remove_location = remove_watchpoint;
16110 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16111 ops->check_status = check_status_watchpoint;
16112 ops->resources_needed = resources_needed_watchpoint;
16113 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16114 ops->print_it = print_it_watchpoint;
16115 ops->print_mention = print_mention_watchpoint;
16116 ops->print_recreate = print_recreate_watchpoint;
427cd150 16117 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16118
16119 /* Masked watchpoints. */
16120 ops = &masked_watchpoint_breakpoint_ops;
16121 *ops = watchpoint_breakpoint_ops;
16122 ops->insert_location = insert_masked_watchpoint;
16123 ops->remove_location = remove_masked_watchpoint;
16124 ops->resources_needed = resources_needed_masked_watchpoint;
16125 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16126 ops->print_it = print_it_masked_watchpoint;
16127 ops->print_one_detail = print_one_detail_masked_watchpoint;
16128 ops->print_mention = print_mention_masked_watchpoint;
16129 ops->print_recreate = print_recreate_masked_watchpoint;
16130
16131 /* Tracepoints. */
16132 ops = &tracepoint_breakpoint_ops;
16133 *ops = base_breakpoint_ops;
16134 ops->re_set = tracepoint_re_set;
16135 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16136 ops->print_one_detail = tracepoint_print_one_detail;
16137 ops->print_mention = tracepoint_print_mention;
16138 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16139 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16140 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16141 ops->decode_linespec = tracepoint_decode_linespec;
16142
55aa24fb
SDJ
16143 /* Probe tracepoints. */
16144 ops = &tracepoint_probe_breakpoint_ops;
16145 *ops = tracepoint_breakpoint_ops;
16146 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16147 ops->decode_linespec = tracepoint_probe_decode_linespec;
16148
983af33b
SDJ
16149 /* Static tracepoints with marker (`-m'). */
16150 ops = &strace_marker_breakpoint_ops;
16151 *ops = tracepoint_breakpoint_ops;
16152 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16153 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16154 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16155
16156 /* Fork catchpoints. */
16157 ops = &catch_fork_breakpoint_ops;
16158 *ops = base_breakpoint_ops;
16159 ops->insert_location = insert_catch_fork;
16160 ops->remove_location = remove_catch_fork;
16161 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16162 ops->print_it = print_it_catch_fork;
16163 ops->print_one = print_one_catch_fork;
16164 ops->print_mention = print_mention_catch_fork;
16165 ops->print_recreate = print_recreate_catch_fork;
16166
16167 /* Vfork catchpoints. */
16168 ops = &catch_vfork_breakpoint_ops;
16169 *ops = base_breakpoint_ops;
16170 ops->insert_location = insert_catch_vfork;
16171 ops->remove_location = remove_catch_vfork;
16172 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16173 ops->print_it = print_it_catch_vfork;
16174 ops->print_one = print_one_catch_vfork;
16175 ops->print_mention = print_mention_catch_vfork;
16176 ops->print_recreate = print_recreate_catch_vfork;
16177
16178 /* Exec catchpoints. */
16179 ops = &catch_exec_breakpoint_ops;
16180 *ops = base_breakpoint_ops;
16181 ops->dtor = dtor_catch_exec;
16182 ops->insert_location = insert_catch_exec;
16183 ops->remove_location = remove_catch_exec;
16184 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16185 ops->print_it = print_it_catch_exec;
16186 ops->print_one = print_one_catch_exec;
16187 ops->print_mention = print_mention_catch_exec;
16188 ops->print_recreate = print_recreate_catch_exec;
16189
16190 /* Syscall catchpoints. */
16191 ops = &catch_syscall_breakpoint_ops;
16192 *ops = base_breakpoint_ops;
16193 ops->dtor = dtor_catch_syscall;
16194 ops->insert_location = insert_catch_syscall;
16195 ops->remove_location = remove_catch_syscall;
16196 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16197 ops->print_it = print_it_catch_syscall;
16198 ops->print_one = print_one_catch_syscall;
16199 ops->print_mention = print_mention_catch_syscall;
16200 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16201
16202 /* Solib-related catchpoints. */
16203 ops = &catch_solib_breakpoint_ops;
16204 *ops = base_breakpoint_ops;
16205 ops->dtor = dtor_catch_solib;
16206 ops->insert_location = insert_catch_solib;
16207 ops->remove_location = remove_catch_solib;
16208 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16209 ops->check_status = check_status_catch_solib;
16210 ops->print_it = print_it_catch_solib;
16211 ops->print_one = print_one_catch_solib;
16212 ops->print_mention = print_mention_catch_solib;
16213 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16214
16215 ops = &dprintf_breakpoint_ops;
16216 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16217 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16218 ops->resources_needed = bkpt_resources_needed;
16219 ops->print_it = bkpt_print_it;
16220 ops->print_mention = bkpt_print_mention;
2d9442cc 16221 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16222 ops->after_condition_true = dprintf_after_condition_true;
2060206e
PA
16223}
16224
8bfd80db
YQ
16225/* Chain containing all defined "enable breakpoint" subcommands. */
16226
16227static struct cmd_list_element *enablebreaklist = NULL;
16228
c906108c 16229void
fba45db2 16230_initialize_breakpoint (void)
c906108c
SS
16231{
16232 struct cmd_list_element *c;
16233
2060206e
PA
16234 initialize_breakpoint_ops ();
16235
84acb35a 16236 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16237 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16238 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16239 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16240
55aa24fb
SDJ
16241 breakpoint_objfile_key
16242 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16243
fa3064dd 16244 catch_syscall_inferior_data
8e260fc0
TT
16245 = register_inferior_data_with_cleanup (NULL,
16246 catch_syscall_inferior_data_cleanup);
fa3064dd 16247
c906108c
SS
16248 breakpoint_chain = 0;
16249 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16250 before a breakpoint is set. */
16251 breakpoint_count = 0;
16252
1042e4c0
SS
16253 tracepoint_count = 0;
16254
1bedd215
AC
16255 add_com ("ignore", class_breakpoint, ignore_command, _("\
16256Set ignore-count of breakpoint number N to COUNT.\n\
16257Usage is `ignore N COUNT'."));
c906108c 16258 if (xdb_commands)
c5aa993b 16259 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16260
1bedd215
AC
16261 add_com ("commands", class_breakpoint, commands_command, _("\
16262Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16263Give breakpoint number as argument after \"commands\".\n\
16264With no argument, the targeted breakpoint is the last one set.\n\
16265The commands themselves follow starting on the next line.\n\
16266Type a line containing \"end\" to indicate the end of them.\n\
16267Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16268then no output is printed when it is hit, except what the commands print."));
c906108c 16269
d55637df 16270 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16271Specify breakpoint number N to break only if COND is true.\n\
c906108c 16272Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16273expression to be evaluated whenever breakpoint N is reached."));
d55637df 16274 set_cmd_completer (c, condition_completer);
c906108c 16275
1bedd215 16276 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16277Set a temporary breakpoint.\n\
c906108c
SS
16278Like \"break\" except the breakpoint is only temporary,\n\
16279so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16280by using \"enable delete\" on the breakpoint number.\n\
16281\n"
16282BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16283 set_cmd_completer (c, location_completer);
c94fdfd0 16284
1bedd215 16285 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16286Set a hardware assisted breakpoint.\n\
c906108c 16287Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16288some target hardware may not have this support.\n\
16289\n"
16290BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16291 set_cmd_completer (c, location_completer);
c906108c 16292
1bedd215 16293 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16294Set a temporary hardware assisted breakpoint.\n\
c906108c 16295Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16296so it will be deleted when hit.\n\
16297\n"
16298BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16299 set_cmd_completer (c, location_completer);
c906108c 16300
1bedd215
AC
16301 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16302Enable some breakpoints.\n\
c906108c
SS
16303Give breakpoint numbers (separated by spaces) as arguments.\n\
16304With no subcommand, breakpoints are enabled until you command otherwise.\n\
16305This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16306With a subcommand you can enable temporarily."),
c906108c
SS
16307 &enablelist, "enable ", 1, &cmdlist);
16308 if (xdb_commands)
1bedd215
AC
16309 add_com ("ab", class_breakpoint, enable_command, _("\
16310Enable some breakpoints.\n\
c906108c
SS
16311Give breakpoint numbers (separated by spaces) as arguments.\n\
16312With no subcommand, breakpoints are enabled until you command otherwise.\n\
16313This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16314With a subcommand you can enable temporarily."));
c906108c
SS
16315
16316 add_com_alias ("en", "enable", class_breakpoint, 1);
16317
84951ab5 16318 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16319Enable some breakpoints.\n\
c906108c
SS
16320Give breakpoint numbers (separated by spaces) as arguments.\n\
16321This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16322May be abbreviated to simply \"enable\".\n"),
c5aa993b 16323 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16324
1a966eab
AC
16325 add_cmd ("once", no_class, enable_once_command, _("\
16326Enable breakpoints for one hit. Give breakpoint numbers.\n\
16327If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16328 &enablebreaklist);
16329
1a966eab
AC
16330 add_cmd ("delete", no_class, enable_delete_command, _("\
16331Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16332If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16333 &enablebreaklist);
16334
816338b5
SS
16335 add_cmd ("count", no_class, enable_count_command, _("\
16336Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16337If a breakpoint is hit while enabled in this fashion,\n\
16338the count is decremented; when it reaches zero, the breakpoint is disabled."),
16339 &enablebreaklist);
16340
1a966eab
AC
16341 add_cmd ("delete", no_class, enable_delete_command, _("\
16342Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16343If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16344 &enablelist);
16345
1a966eab
AC
16346 add_cmd ("once", no_class, enable_once_command, _("\
16347Enable breakpoints for one hit. Give breakpoint numbers.\n\
16348If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16349 &enablelist);
16350
16351 add_cmd ("count", no_class, enable_count_command, _("\
16352Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16353If a breakpoint is hit while enabled in this fashion,\n\
16354the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16355 &enablelist);
16356
1bedd215
AC
16357 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16358Disable some breakpoints.\n\
c906108c
SS
16359Arguments are breakpoint numbers with spaces in between.\n\
16360To disable all breakpoints, give no argument.\n\
64b9b334 16361A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16362 &disablelist, "disable ", 1, &cmdlist);
16363 add_com_alias ("dis", "disable", class_breakpoint, 1);
16364 add_com_alias ("disa", "disable", class_breakpoint, 1);
16365 if (xdb_commands)
1bedd215
AC
16366 add_com ("sb", class_breakpoint, disable_command, _("\
16367Disable some breakpoints.\n\
c906108c
SS
16368Arguments are breakpoint numbers with spaces in between.\n\
16369To disable all breakpoints, give no argument.\n\
64b9b334 16370A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16371
1a966eab
AC
16372 add_cmd ("breakpoints", class_alias, disable_command, _("\
16373Disable some breakpoints.\n\
c906108c
SS
16374Arguments are breakpoint numbers with spaces in between.\n\
16375To disable all breakpoints, give no argument.\n\
64b9b334 16376A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16377This command may be abbreviated \"disable\"."),
c906108c
SS
16378 &disablelist);
16379
1bedd215
AC
16380 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16381Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16382Arguments are breakpoint numbers with spaces in between.\n\
16383To delete all breakpoints, give no argument.\n\
16384\n\
16385Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16386The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16387 &deletelist, "delete ", 1, &cmdlist);
16388 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16389 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16390 if (xdb_commands)
1bedd215
AC
16391 add_com ("db", class_breakpoint, delete_command, _("\
16392Delete some breakpoints.\n\
c906108c 16393Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16394To delete all breakpoints, give no argument.\n"));
c906108c 16395
1a966eab
AC
16396 add_cmd ("breakpoints", class_alias, delete_command, _("\
16397Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16398Arguments are breakpoint numbers with spaces in between.\n\
16399To delete all breakpoints, give no argument.\n\
1a966eab 16400This command may be abbreviated \"delete\"."),
c906108c
SS
16401 &deletelist);
16402
1bedd215
AC
16403 add_com ("clear", class_breakpoint, clear_command, _("\
16404Clear breakpoint at specified line or function.\n\
c906108c
SS
16405Argument may be line number, function name, or \"*\" and an address.\n\
16406If line number is specified, all breakpoints in that line are cleared.\n\
16407If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16408If an address is specified, breakpoints at that address are cleared.\n\
16409\n\
16410With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16411is executing in.\n\
16412\n\
1bedd215 16413See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16414 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16415
1bedd215 16416 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16417Set breakpoint at specified line or function.\n"
16418BREAK_ARGS_HELP ("break")));
5ba2abeb 16419 set_cmd_completer (c, location_completer);
c94fdfd0 16420
c906108c
SS
16421 add_com_alias ("b", "break", class_run, 1);
16422 add_com_alias ("br", "break", class_run, 1);
16423 add_com_alias ("bre", "break", class_run, 1);
16424 add_com_alias ("brea", "break", class_run, 1);
16425
7681d515
PM
16426 if (xdb_commands)
16427 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16428
16429 if (dbx_commands)
16430 {
1bedd215
AC
16431 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16432Break in function/address or break at a line in the current file."),
c5aa993b
JM
16433 &stoplist, "stop ", 1, &cmdlist);
16434 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16435 _("Break in function or address."), &stoplist);
c5aa993b 16436 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16437 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16438 add_com ("status", class_info, breakpoints_info, _("\
16439Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16440The \"Type\" column indicates one of:\n\
16441\tbreakpoint - normal breakpoint\n\
16442\twatchpoint - watchpoint\n\
16443The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16444the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16445breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16446address and file/line number respectively.\n\
16447\n\
16448Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16449are set to the address of the last breakpoint listed unless the command\n\
16450is prefixed with \"server \".\n\n\
c906108c 16451Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16452breakpoint set."));
c906108c
SS
16453 }
16454
1bedd215 16455 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16456Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16457The \"Type\" column indicates one of:\n\
16458\tbreakpoint - normal breakpoint\n\
16459\twatchpoint - watchpoint\n\
16460The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16461the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16462breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16463address and file/line number respectively.\n\
16464\n\
16465Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16466are set to the address of the last breakpoint listed unless the command\n\
16467is prefixed with \"server \".\n\n\
c906108c 16468Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16469breakpoint set."));
c906108c 16470
6b04bdb7
MS
16471 add_info_alias ("b", "breakpoints", 1);
16472
c906108c 16473 if (xdb_commands)
1bedd215
AC
16474 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16475Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16476The \"Type\" column indicates one of:\n\
16477\tbreakpoint - normal breakpoint\n\
16478\twatchpoint - watchpoint\n\
16479The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16480the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16481breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16482address and file/line number respectively.\n\
16483\n\
16484Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16485are set to the address of the last breakpoint listed unless the command\n\
16486is prefixed with \"server \".\n\n\
c906108c 16487Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16488breakpoint set."));
c906108c 16489
1a966eab
AC
16490 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16491Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16492The \"Type\" column indicates one of:\n\
16493\tbreakpoint - normal breakpoint\n\
16494\twatchpoint - watchpoint\n\
16495\tlongjmp - internal breakpoint used to step through longjmp()\n\
16496\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16497\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16498\tfinish - internal breakpoint used by the \"finish\" command\n\
16499The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16500the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16501breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16502address and file/line number respectively.\n\
16503\n\
16504Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16505are set to the address of the last breakpoint listed unless the command\n\
16506is prefixed with \"server \".\n\n\
c906108c 16507Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16508breakpoint set."),
c906108c
SS
16509 &maintenanceinfolist);
16510
44feb3ce
TT
16511 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16512Set catchpoints to catch events."),
16513 &catch_cmdlist, "catch ",
16514 0/*allow-unknown*/, &cmdlist);
16515
16516 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16517Set temporary catchpoints to catch events."),
16518 &tcatch_cmdlist, "tcatch ",
16519 0/*allow-unknown*/, &cmdlist);
16520
44feb3ce
TT
16521 add_catch_command ("fork", _("Catch calls to fork."),
16522 catch_fork_command_1,
a96d9b2e 16523 NULL,
44feb3ce
TT
16524 (void *) (uintptr_t) catch_fork_permanent,
16525 (void *) (uintptr_t) catch_fork_temporary);
16526 add_catch_command ("vfork", _("Catch calls to vfork."),
16527 catch_fork_command_1,
a96d9b2e 16528 NULL,
44feb3ce
TT
16529 (void *) (uintptr_t) catch_vfork_permanent,
16530 (void *) (uintptr_t) catch_vfork_temporary);
16531 add_catch_command ("exec", _("Catch calls to exec."),
16532 catch_exec_command_1,
a96d9b2e
SDJ
16533 NULL,
16534 CATCH_PERMANENT,
16535 CATCH_TEMPORARY);
edcc5120
TT
16536 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16537Usage: catch load [REGEX]\n\
16538If REGEX is given, only stop for libraries matching the regular expression."),
16539 catch_load_command_1,
16540 NULL,
16541 CATCH_PERMANENT,
16542 CATCH_TEMPORARY);
16543 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16544Usage: catch unload [REGEX]\n\
16545If REGEX is given, only stop for libraries matching the regular expression."),
16546 catch_unload_command_1,
16547 NULL,
16548 CATCH_PERMANENT,
16549 CATCH_TEMPORARY);
a96d9b2e
SDJ
16550 add_catch_command ("syscall", _("\
16551Catch system calls by their names and/or numbers.\n\
16552Arguments say which system calls to catch. If no arguments\n\
16553are given, every system call will be caught.\n\
16554Arguments, if given, should be one or more system call names\n\
16555(if your system supports that), or system call numbers."),
16556 catch_syscall_command_1,
16557 catch_syscall_completer,
44feb3ce
TT
16558 CATCH_PERMANENT,
16559 CATCH_TEMPORARY);
c5aa993b 16560
1bedd215
AC
16561 c = add_com ("watch", class_breakpoint, watch_command, _("\
16562Set a watchpoint for an expression.\n\
06a64a0b 16563Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16564A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16565an expression changes.\n\
16566If -l or -location is given, this evaluates EXPRESSION and watches\n\
16567the memory to which it refers."));
65d12d83 16568 set_cmd_completer (c, expression_completer);
c906108c 16569
1bedd215
AC
16570 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16571Set a read watchpoint for an expression.\n\
06a64a0b 16572Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16573A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16574an expression is read.\n\
16575If -l or -location is given, this evaluates EXPRESSION and watches\n\
16576the memory to which it refers."));
65d12d83 16577 set_cmd_completer (c, expression_completer);
c906108c 16578
1bedd215
AC
16579 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16580Set a watchpoint for an expression.\n\
06a64a0b 16581Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16582A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16583an expression is either read or written.\n\
16584If -l or -location is given, this evaluates EXPRESSION and watches\n\
16585the memory to which it refers."));
65d12d83 16586 set_cmd_completer (c, expression_completer);
c906108c 16587
d77f58be 16588 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16589Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16590
920d2a44
AC
16591 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16592 respond to changes - contrary to the description. */
85c07804
AC
16593 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16594 &can_use_hw_watchpoints, _("\
16595Set debugger's willingness to use watchpoint hardware."), _("\
16596Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16597If zero, gdb will not use hardware for new watchpoints, even if\n\
16598such is available. (However, any hardware watchpoints that were\n\
16599created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16600hardware.)"),
16601 NULL,
920d2a44 16602 show_can_use_hw_watchpoints,
85c07804 16603 &setlist, &showlist);
c906108c
SS
16604
16605 can_use_hw_watchpoints = 1;
fa8d40ab 16606
1042e4c0
SS
16607 /* Tracepoint manipulation commands. */
16608
16609 c = add_com ("trace", class_breakpoint, trace_command, _("\
16610Set a tracepoint at specified line or function.\n\
16611\n"
16612BREAK_ARGS_HELP ("trace") "\n\
16613Do \"help tracepoints\" for info on other tracepoint commands."));
16614 set_cmd_completer (c, location_completer);
16615
16616 add_com_alias ("tp", "trace", class_alias, 0);
16617 add_com_alias ("tr", "trace", class_alias, 1);
16618 add_com_alias ("tra", "trace", class_alias, 1);
16619 add_com_alias ("trac", "trace", class_alias, 1);
16620
7a697b8d
SS
16621 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16622Set a fast tracepoint at specified line or function.\n\
16623\n"
16624BREAK_ARGS_HELP ("ftrace") "\n\
16625Do \"help tracepoints\" for info on other tracepoint commands."));
16626 set_cmd_completer (c, location_completer);
16627
0fb4aa4b
PA
16628 c = add_com ("strace", class_breakpoint, strace_command, _("\
16629Set a static tracepoint at specified line, function or marker.\n\
16630\n\
16631strace [LOCATION] [if CONDITION]\n\
16632LOCATION may be a line number, function name, \"*\" and an address,\n\
16633or -m MARKER_ID.\n\
16634If a line number is specified, probe the marker at start of code\n\
16635for that line. If a function is specified, probe the marker at start\n\
16636of code for that function. If an address is specified, probe the marker\n\
16637at that exact address. If a marker id is specified, probe the marker\n\
16638with that name. With no LOCATION, uses current execution address of\n\
16639the selected stack frame.\n\
16640Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16641This collects arbitrary user data passed in the probe point call to the\n\
16642tracing library. You can inspect it when analyzing the trace buffer,\n\
16643by printing the $_sdata variable like any other convenience variable.\n\
16644\n\
16645CONDITION is a boolean expression.\n\
16646\n\
d41c0fc8
PA
16647Multiple tracepoints at one place are permitted, and useful if their\n\
16648conditions are different.\n\
0fb4aa4b
PA
16649\n\
16650Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16651Do \"help tracepoints\" for info on other tracepoint commands."));
16652 set_cmd_completer (c, location_completer);
16653
1042e4c0 16654 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16655Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16656Convenience variable \"$tpnum\" contains the number of the\n\
16657last tracepoint set."));
16658
16659 add_info_alias ("tp", "tracepoints", 1);
16660
16661 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16662Delete specified tracepoints.\n\
16663Arguments are tracepoint numbers, separated by spaces.\n\
16664No argument means delete all tracepoints."),
16665 &deletelist);
7e20dfcd 16666 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16667
16668 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16669Disable specified tracepoints.\n\
16670Arguments are tracepoint numbers, separated by spaces.\n\
16671No argument means disable all tracepoints."),
16672 &disablelist);
16673 deprecate_cmd (c, "disable");
16674
16675 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16676Enable specified tracepoints.\n\
16677Arguments are tracepoint numbers, separated by spaces.\n\
16678No argument means enable all tracepoints."),
16679 &enablelist);
16680 deprecate_cmd (c, "enable");
16681
16682 add_com ("passcount", class_trace, trace_pass_command, _("\
16683Set the passcount for a tracepoint.\n\
16684The trace will end when the tracepoint has been passed 'count' times.\n\
16685Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16686if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16687
6149aea9
PA
16688 add_prefix_cmd ("save", class_breakpoint, save_command,
16689 _("Save breakpoint definitions as a script."),
16690 &save_cmdlist, "save ",
16691 0/*allow-unknown*/, &cmdlist);
16692
16693 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16694Save current breakpoint definitions as a script.\n\
cce7e648 16695This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16696catchpoints, tracepoints). Use the 'source' command in another debug\n\
16697session to restore them."),
16698 &save_cmdlist);
16699 set_cmd_completer (c, filename_completer);
16700
16701 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16702Save current tracepoint definitions as a script.\n\
6149aea9
PA
16703Use the 'source' command in another debug session to restore them."),
16704 &save_cmdlist);
1042e4c0
SS
16705 set_cmd_completer (c, filename_completer);
16706
6149aea9
PA
16707 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16708 deprecate_cmd (c, "save tracepoints");
16709
1bedd215 16710 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16711Breakpoint specific settings\n\
16712Configure various breakpoint-specific variables such as\n\
1bedd215 16713pending breakpoint behavior"),
fa8d40ab
JJ
16714 &breakpoint_set_cmdlist, "set breakpoint ",
16715 0/*allow-unknown*/, &setlist);
1bedd215 16716 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16717Breakpoint specific settings\n\
16718Configure various breakpoint-specific variables such as\n\
1bedd215 16719pending breakpoint behavior"),
fa8d40ab
JJ
16720 &breakpoint_show_cmdlist, "show breakpoint ",
16721 0/*allow-unknown*/, &showlist);
16722
7915a72c
AC
16723 add_setshow_auto_boolean_cmd ("pending", no_class,
16724 &pending_break_support, _("\
16725Set debugger's behavior regarding pending breakpoints."), _("\
16726Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16727If on, an unrecognized breakpoint location will cause gdb to create a\n\
16728pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16729an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16730user-query to see if a pending breakpoint should be created."),
2c5b56ce 16731 NULL,
920d2a44 16732 show_pending_break_support,
6e1d7d6c
AC
16733 &breakpoint_set_cmdlist,
16734 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16735
16736 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16737
16738 add_setshow_boolean_cmd ("auto-hw", no_class,
16739 &automatic_hardware_breakpoints, _("\
16740Set automatic usage of hardware breakpoints."), _("\
16741Show automatic usage of hardware breakpoints."), _("\
16742If set, the debugger will automatically use hardware breakpoints for\n\
16743breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16744a warning will be emitted for such breakpoints."),
16745 NULL,
16746 show_automatic_hardware_breakpoints,
16747 &breakpoint_set_cmdlist,
16748 &breakpoint_show_cmdlist);
74960c60 16749
72d0e2c5
YQ
16750 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16751 &always_inserted_mode, _("\
74960c60
VP
16752Set mode for inserting breakpoints."), _("\
16753Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16754When this mode is off, breakpoints are inserted in inferior when it is\n\
16755resumed, and removed when execution stops. When this mode is on,\n\
16756breakpoints are inserted immediately and removed only when the user\n\
16757deletes the breakpoint. When this mode is auto (which is the default),\n\
16758the behaviour depends on the non-stop setting (see help set non-stop).\n\
16759In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16760behaves as if always-inserted mode is on; if gdb is controlling the\n\
16761inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16762 NULL,
16763 &show_always_inserted_mode,
16764 &breakpoint_set_cmdlist,
16765 &breakpoint_show_cmdlist);
f1310107 16766
b775012e
LM
16767 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16768 condition_evaluation_enums,
16769 &condition_evaluation_mode_1, _("\
16770Set mode of breakpoint condition evaluation."), _("\
16771Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16772When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16773evaluated on the host's side by GDB. When it is set to \"target\",\n\
16774breakpoint conditions will be downloaded to the target (if the target\n\
16775supports such feature) and conditions will be evaluated on the target's side.\n\
16776If this is set to \"auto\" (default), this will be automatically set to\n\
16777\"target\" if it supports condition evaluation, otherwise it will\n\
16778be set to \"gdb\""),
16779 &set_condition_evaluation_mode,
16780 &show_condition_evaluation_mode,
16781 &breakpoint_set_cmdlist,
16782 &breakpoint_show_cmdlist);
16783
f1310107
TJB
16784 add_com ("break-range", class_breakpoint, break_range_command, _("\
16785Set a breakpoint for an address range.\n\
16786break-range START-LOCATION, END-LOCATION\n\
16787where START-LOCATION and END-LOCATION can be one of the following:\n\
16788 LINENUM, for that line in the current file,\n\
16789 FILE:LINENUM, for that line in that file,\n\
16790 +OFFSET, for that number of lines after the current line\n\
16791 or the start of the range\n\
16792 FUNCTION, for the first line in that function,\n\
16793 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16794 *ADDRESS, for the instruction at that address.\n\
16795\n\
16796The breakpoint will stop execution of the inferior whenever it executes\n\
16797an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16798range (including START-LOCATION and END-LOCATION)."));
16799
e7e0cddf
SS
16800 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16801Set a dynamic printf at specified line or function.\n\
16802dprintf location,format string,arg1,arg2,...\n\
16803location may be a line number, function name, or \"*\" and an address.\n\
16804If a line number is specified, break at start of code for that line.\n\
0e4777df 16805If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16806 set_cmd_completer (c, location_completer);
16807
16808 add_setshow_enum_cmd ("dprintf-style", class_support,
16809 dprintf_style_enums, &dprintf_style, _("\
16810Set the style of usage for dynamic printf."), _("\
16811Show the style of usage for dynamic printf."), _("\
16812This setting chooses how GDB will do a dynamic printf.\n\
16813If the value is \"gdb\", then the printing is done by GDB to its own\n\
16814console, as with the \"printf\" command.\n\
16815If the value is \"call\", the print is done by calling a function in your\n\
16816program; by default printf(), but you can choose a different function or\n\
16817output stream by setting dprintf-function and dprintf-channel."),
16818 update_dprintf_commands, NULL,
16819 &setlist, &showlist);
16820
16821 dprintf_function = xstrdup ("printf");
16822 add_setshow_string_cmd ("dprintf-function", class_support,
16823 &dprintf_function, _("\
16824Set the function to use for dynamic printf"), _("\
16825Show the function to use for dynamic printf"), NULL,
16826 update_dprintf_commands, NULL,
16827 &setlist, &showlist);
16828
16829 dprintf_channel = xstrdup ("");
16830 add_setshow_string_cmd ("dprintf-channel", class_support,
16831 &dprintf_channel, _("\
16832Set the channel to use for dynamic printf"), _("\
16833Show the channel to use for dynamic printf"), NULL,
16834 update_dprintf_commands, NULL,
16835 &setlist, &showlist);
16836
d3ce09f5
SS
16837 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16838 &disconnected_dprintf, _("\
16839Set whether dprintf continues after GDB disconnects."), _("\
16840Show whether dprintf continues after GDB disconnects."), _("\
16841Use this to let dprintf commands continue to hit and produce output\n\
16842even if GDB disconnects or detaches from the target."),
16843 NULL,
16844 NULL,
16845 &setlist, &showlist);
16846
16847 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16848agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16849(target agent only) This is useful for formatted output in user-defined commands."));
16850
765dc015 16851 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16852
16853 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16854 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16855}
This page took 4.128092 seconds and 4 git commands to generate.